xusbatm.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /******************************************************************************
  2. * xusbatm.c - dumb usbatm-based driver for modems initialized in userspace
  3. *
  4. * Copyright (C) 2005 Duncan Sands, Roman Kagan (rkagan % mail ! ru)
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation; either version 2 of the License, or (at your option)
  9. * any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * this program; if not, write to the Free Software Foundation, Inc., 59
  18. * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. *
  20. ******************************************************************************/
  21. #include <linux/module.h>
  22. #include <linux/etherdevice.h> /* for random_ether_addr() */
  23. #include "usbatm.h"
  24. #define XUSBATM_DRIVERS_MAX 8
  25. #define XUSBATM_PARM(name, type, parmtype, desc) \
  26. static type name[XUSBATM_DRIVERS_MAX]; \
  27. static unsigned int num_##name; \
  28. module_param_array(name, parmtype, &num_##name, 0444); \
  29. MODULE_PARM_DESC(name, desc)
  30. XUSBATM_PARM(vendor, unsigned short, ushort, "USB device vendor");
  31. XUSBATM_PARM(product, unsigned short, ushort, "USB device product");
  32. XUSBATM_PARM(rx_endpoint, unsigned char, byte, "rx endpoint number");
  33. XUSBATM_PARM(tx_endpoint, unsigned char, byte, "tx endpoint number");
  34. XUSBATM_PARM(rx_padding, unsigned char, byte, "rx padding (default 0)");
  35. XUSBATM_PARM(tx_padding, unsigned char, byte, "tx padding (default 0)");
  36. XUSBATM_PARM(rx_altsetting, unsigned char, byte, "rx altsetting (default 0)");
  37. XUSBATM_PARM(tx_altsetting, unsigned char, byte, "rx altsetting (default 0)");
  38. static const char xusbatm_driver_name[] = "xusbatm";
  39. static struct usbatm_driver xusbatm_drivers[XUSBATM_DRIVERS_MAX];
  40. static struct usb_device_id xusbatm_usb_ids[XUSBATM_DRIVERS_MAX + 1];
  41. static struct usb_driver xusbatm_usb_driver;
  42. static struct usb_interface *xusbatm_find_intf(struct usb_device *usb_dev, int altsetting, u8 ep)
  43. {
  44. struct usb_host_interface *alt;
  45. struct usb_interface *intf;
  46. int i, j;
  47. for (i = 0; i < usb_dev->actconfig->desc.bNumInterfaces; i++)
  48. if ((intf = usb_dev->actconfig->interface[i]) && (alt = usb_altnum_to_altsetting(intf, altsetting)))
  49. for (j = 0; j < alt->desc.bNumEndpoints; j++)
  50. if (alt->endpoint[j].desc.bEndpointAddress == ep)
  51. return intf;
  52. return NULL;
  53. }
  54. static int xusbatm_capture_intf(struct usbatm_data *usbatm, struct usb_device *usb_dev,
  55. struct usb_interface *intf, int altsetting, int claim)
  56. {
  57. int ifnum = intf->altsetting->desc.bInterfaceNumber;
  58. int ret;
  59. if (claim && (ret = usb_driver_claim_interface(&xusbatm_usb_driver, intf, usbatm))) {
  60. usb_err(usbatm, "%s: failed to claim interface %2d (%d)!\n", __func__, ifnum, ret);
  61. return ret;
  62. }
  63. if ((ret = usb_set_interface(usb_dev, ifnum, altsetting))) {
  64. usb_err(usbatm, "%s: altsetting %2d for interface %2d failed (%d)!\n", __func__, altsetting, ifnum, ret);
  65. return ret;
  66. }
  67. return 0;
  68. }
  69. static void xusbatm_release_intf(struct usb_device *usb_dev, struct usb_interface *intf, int claimed)
  70. {
  71. if (claimed) {
  72. usb_set_intfdata(intf, NULL);
  73. usb_driver_release_interface(&xusbatm_usb_driver, intf);
  74. }
  75. }
  76. static int xusbatm_bind(struct usbatm_data *usbatm,
  77. struct usb_interface *intf, const struct usb_device_id *id)
  78. {
  79. struct usb_device *usb_dev = interface_to_usbdev(intf);
  80. int drv_ix = id - xusbatm_usb_ids;
  81. int rx_alt = rx_altsetting[drv_ix];
  82. int tx_alt = tx_altsetting[drv_ix];
  83. struct usb_interface *rx_intf = xusbatm_find_intf(usb_dev, rx_alt, rx_endpoint[drv_ix]);
  84. struct usb_interface *tx_intf = xusbatm_find_intf(usb_dev, tx_alt, tx_endpoint[drv_ix]);
  85. int ret;
  86. usb_dbg(usbatm, "%s: binding driver %d: vendor %04x product %04x"
  87. " rx: ep %02x padd %d alt %2d tx: ep %02x padd %d alt %2d\n",
  88. __func__, drv_ix, vendor[drv_ix], product[drv_ix],
  89. rx_endpoint[drv_ix], rx_padding[drv_ix], rx_alt,
  90. tx_endpoint[drv_ix], tx_padding[drv_ix], tx_alt);
  91. if (!rx_intf || !tx_intf) {
  92. if (!rx_intf)
  93. usb_dbg(usbatm, "%s: no interface contains endpoint %02x in altsetting %2d\n",
  94. __func__, rx_endpoint[drv_ix], rx_alt);
  95. if (!tx_intf)
  96. usb_dbg(usbatm, "%s: no interface contains endpoint %02x in altsetting %2d\n",
  97. __func__, tx_endpoint[drv_ix], tx_alt);
  98. return -ENODEV;
  99. }
  100. if ((rx_intf != intf) && (tx_intf != intf))
  101. return -ENODEV;
  102. if ((rx_intf == tx_intf) && (rx_alt != tx_alt)) {
  103. usb_err(usbatm, "%s: altsettings clash on interface %2d (%2d vs %2d)!\n", __func__,
  104. rx_intf->altsetting->desc.bInterfaceNumber, rx_alt, tx_alt);
  105. return -EINVAL;
  106. }
  107. usb_dbg(usbatm, "%s: rx If#=%2d; tx If#=%2d\n", __func__,
  108. rx_intf->altsetting->desc.bInterfaceNumber,
  109. tx_intf->altsetting->desc.bInterfaceNumber);
  110. if ((ret = xusbatm_capture_intf(usbatm, usb_dev, rx_intf, rx_alt, rx_intf != intf)))
  111. return ret;
  112. if ((tx_intf != rx_intf) && (ret = xusbatm_capture_intf(usbatm, usb_dev, tx_intf, tx_alt, tx_intf != intf))) {
  113. xusbatm_release_intf(usb_dev, rx_intf, rx_intf != intf);
  114. return ret;
  115. }
  116. return 0;
  117. }
  118. static void xusbatm_unbind(struct usbatm_data *usbatm,
  119. struct usb_interface *intf)
  120. {
  121. struct usb_device *usb_dev = interface_to_usbdev(intf);
  122. int i;
  123. usb_dbg(usbatm, "%s entered\n", __func__);
  124. for (i = 0; i < usb_dev->actconfig->desc.bNumInterfaces; i++) {
  125. struct usb_interface *cur_intf = usb_dev->actconfig->interface[i];
  126. if (cur_intf && (usb_get_intfdata(cur_intf) == usbatm)) {
  127. usb_set_intfdata(cur_intf, NULL);
  128. usb_driver_release_interface(&xusbatm_usb_driver, cur_intf);
  129. }
  130. }
  131. }
  132. static int xusbatm_atm_start(struct usbatm_data *usbatm,
  133. struct atm_dev *atm_dev)
  134. {
  135. atm_dbg(usbatm, "%s entered\n", __func__);
  136. /* use random MAC as we've no way to get it from the device */
  137. random_ether_addr(atm_dev->esi);
  138. return 0;
  139. }
  140. static int xusbatm_usb_probe(struct usb_interface *intf,
  141. const struct usb_device_id *id)
  142. {
  143. return usbatm_usb_probe(intf, id,
  144. xusbatm_drivers + (id - xusbatm_usb_ids));
  145. }
  146. static struct usb_driver xusbatm_usb_driver = {
  147. .name = xusbatm_driver_name,
  148. .probe = xusbatm_usb_probe,
  149. .disconnect = usbatm_usb_disconnect,
  150. .id_table = xusbatm_usb_ids
  151. };
  152. static int __init xusbatm_init(void)
  153. {
  154. int i;
  155. dbg("xusbatm_init");
  156. if (!num_vendor ||
  157. num_vendor != num_product ||
  158. num_vendor != num_rx_endpoint ||
  159. num_vendor != num_tx_endpoint) {
  160. printk(KERN_WARNING "xusbatm: malformed module parameters\n");
  161. return -EINVAL;
  162. }
  163. for (i = 0; i < num_vendor; i++) {
  164. rx_endpoint[i] |= USB_DIR_IN;
  165. tx_endpoint[i] &= USB_ENDPOINT_NUMBER_MASK;
  166. xusbatm_usb_ids[i].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
  167. xusbatm_usb_ids[i].idVendor = vendor[i];
  168. xusbatm_usb_ids[i].idProduct = product[i];
  169. xusbatm_drivers[i].driver_name = xusbatm_driver_name;
  170. xusbatm_drivers[i].bind = xusbatm_bind;
  171. xusbatm_drivers[i].unbind = xusbatm_unbind;
  172. xusbatm_drivers[i].atm_start = xusbatm_atm_start;
  173. xusbatm_drivers[i].bulk_in = rx_endpoint[i];
  174. xusbatm_drivers[i].bulk_out = tx_endpoint[i];
  175. xusbatm_drivers[i].rx_padding = rx_padding[i];
  176. xusbatm_drivers[i].tx_padding = tx_padding[i];
  177. }
  178. return usb_register(&xusbatm_usb_driver);
  179. }
  180. module_init(xusbatm_init);
  181. static void __exit xusbatm_exit(void)
  182. {
  183. dbg("xusbatm_exit entered");
  184. usb_deregister(&xusbatm_usb_driver);
  185. }
  186. module_exit(xusbatm_exit);
  187. MODULE_AUTHOR("Roman Kagan, Duncan Sands");
  188. MODULE_DESCRIPTION("Driver for USB ADSL modems initialized in userspace");
  189. MODULE_LICENSE("GPL");
  190. MODULE_VERSION("0.1");