xusbatm.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 eth_random_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. ret = usb_set_interface(usb_dev, ifnum, altsetting);
  64. if (ret) {
  65. usb_err(usbatm, "%s: altsetting %2d for interface %2d failed (%d)!\n", __func__, altsetting, ifnum, ret);
  66. return ret;
  67. }
  68. return 0;
  69. }
  70. static void xusbatm_release_intf(struct usb_device *usb_dev, struct usb_interface *intf, int claimed)
  71. {
  72. if (claimed) {
  73. usb_set_intfdata(intf, NULL);
  74. usb_driver_release_interface(&xusbatm_usb_driver, intf);
  75. }
  76. }
  77. static int xusbatm_bind(struct usbatm_data *usbatm,
  78. struct usb_interface *intf, const struct usb_device_id *id)
  79. {
  80. struct usb_device *usb_dev = interface_to_usbdev(intf);
  81. int drv_ix = id - xusbatm_usb_ids;
  82. int rx_alt = rx_altsetting[drv_ix];
  83. int tx_alt = tx_altsetting[drv_ix];
  84. struct usb_interface *rx_intf = xusbatm_find_intf(usb_dev, rx_alt, rx_endpoint[drv_ix]);
  85. struct usb_interface *tx_intf = xusbatm_find_intf(usb_dev, tx_alt, tx_endpoint[drv_ix]);
  86. int ret;
  87. usb_dbg(usbatm, "%s: binding driver %d: vendor %04x product %04x"
  88. " rx: ep %02x padd %d alt %2d tx: ep %02x padd %d alt %2d\n",
  89. __func__, drv_ix, vendor[drv_ix], product[drv_ix],
  90. rx_endpoint[drv_ix], rx_padding[drv_ix], rx_alt,
  91. tx_endpoint[drv_ix], tx_padding[drv_ix], tx_alt);
  92. if (!rx_intf || !tx_intf) {
  93. if (!rx_intf)
  94. usb_dbg(usbatm, "%s: no interface contains endpoint %02x in altsetting %2d\n",
  95. __func__, rx_endpoint[drv_ix], rx_alt);
  96. if (!tx_intf)
  97. usb_dbg(usbatm, "%s: no interface contains endpoint %02x in altsetting %2d\n",
  98. __func__, tx_endpoint[drv_ix], tx_alt);
  99. return -ENODEV;
  100. }
  101. if ((rx_intf != intf) && (tx_intf != intf))
  102. return -ENODEV;
  103. if ((rx_intf == tx_intf) && (rx_alt != tx_alt)) {
  104. usb_err(usbatm, "%s: altsettings clash on interface %2d (%2d vs %2d)!\n", __func__,
  105. rx_intf->altsetting->desc.bInterfaceNumber, rx_alt, tx_alt);
  106. return -EINVAL;
  107. }
  108. usb_dbg(usbatm, "%s: rx If#=%2d; tx If#=%2d\n", __func__,
  109. rx_intf->altsetting->desc.bInterfaceNumber,
  110. tx_intf->altsetting->desc.bInterfaceNumber);
  111. ret = xusbatm_capture_intf(usbatm, usb_dev, rx_intf, rx_alt, rx_intf != intf);
  112. if (ret)
  113. return ret;
  114. if ((tx_intf != rx_intf) && (ret = xusbatm_capture_intf(usbatm, usb_dev, tx_intf, tx_alt, tx_intf != intf))) {
  115. xusbatm_release_intf(usb_dev, rx_intf, rx_intf != intf);
  116. return ret;
  117. }
  118. return 0;
  119. }
  120. static void xusbatm_unbind(struct usbatm_data *usbatm,
  121. struct usb_interface *intf)
  122. {
  123. struct usb_device *usb_dev = interface_to_usbdev(intf);
  124. int i;
  125. usb_dbg(usbatm, "%s entered\n", __func__);
  126. for (i = 0; i < usb_dev->actconfig->desc.bNumInterfaces; i++) {
  127. struct usb_interface *cur_intf = usb_dev->actconfig->interface[i];
  128. if (cur_intf && (usb_get_intfdata(cur_intf) == usbatm)) {
  129. usb_set_intfdata(cur_intf, NULL);
  130. usb_driver_release_interface(&xusbatm_usb_driver, cur_intf);
  131. }
  132. }
  133. }
  134. static int xusbatm_atm_start(struct usbatm_data *usbatm,
  135. struct atm_dev *atm_dev)
  136. {
  137. atm_dbg(usbatm, "%s entered\n", __func__);
  138. /* use random MAC as we've no way to get it from the device */
  139. eth_random_addr(atm_dev->esi);
  140. return 0;
  141. }
  142. static int xusbatm_usb_probe(struct usb_interface *intf,
  143. const struct usb_device_id *id)
  144. {
  145. return usbatm_usb_probe(intf, id,
  146. xusbatm_drivers + (id - xusbatm_usb_ids));
  147. }
  148. static struct usb_driver xusbatm_usb_driver = {
  149. .name = xusbatm_driver_name,
  150. .probe = xusbatm_usb_probe,
  151. .disconnect = usbatm_usb_disconnect,
  152. .id_table = xusbatm_usb_ids
  153. };
  154. static int __init xusbatm_init(void)
  155. {
  156. int i;
  157. if (!num_vendor ||
  158. num_vendor != num_product ||
  159. num_vendor != num_rx_endpoint ||
  160. num_vendor != num_tx_endpoint) {
  161. printk(KERN_WARNING "xusbatm: malformed module parameters\n");
  162. return -EINVAL;
  163. }
  164. for (i = 0; i < num_vendor; i++) {
  165. rx_endpoint[i] |= USB_DIR_IN;
  166. tx_endpoint[i] &= USB_ENDPOINT_NUMBER_MASK;
  167. xusbatm_usb_ids[i].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
  168. xusbatm_usb_ids[i].idVendor = vendor[i];
  169. xusbatm_usb_ids[i].idProduct = product[i];
  170. xusbatm_drivers[i].driver_name = xusbatm_driver_name;
  171. xusbatm_drivers[i].bind = xusbatm_bind;
  172. xusbatm_drivers[i].unbind = xusbatm_unbind;
  173. xusbatm_drivers[i].atm_start = xusbatm_atm_start;
  174. xusbatm_drivers[i].bulk_in = rx_endpoint[i];
  175. xusbatm_drivers[i].bulk_out = tx_endpoint[i];
  176. xusbatm_drivers[i].rx_padding = rx_padding[i];
  177. xusbatm_drivers[i].tx_padding = tx_padding[i];
  178. }
  179. return usb_register(&xusbatm_usb_driver);
  180. }
  181. module_init(xusbatm_init);
  182. static void __exit xusbatm_exit(void)
  183. {
  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");