xusbatm.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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/netdevice.h> /* FIXME: required by linux/etherdevice.h */
  23. #include <linux/etherdevice.h> /* for random_ether_addr() */
  24. #include "usbatm.h"
  25. #define XUSBATM_DRIVERS_MAX 8
  26. #define XUSBATM_PARM(name, type, parmtype, desc) \
  27. static type name[XUSBATM_DRIVERS_MAX]; \
  28. static int num_##name; \
  29. module_param_array(name, parmtype, &num_##name, 0444); \
  30. MODULE_PARM_DESC(name, desc)
  31. XUSBATM_PARM(vendor, unsigned short, ushort, "USB device vendor");
  32. XUSBATM_PARM(product, unsigned short, ushort, "USB device product");
  33. XUSBATM_PARM(rx_endpoint, unsigned char, byte, "rx endpoint number");
  34. XUSBATM_PARM(tx_endpoint, unsigned char, byte, "tx endpoint number");
  35. XUSBATM_PARM(rx_padding, unsigned char, byte, "rx padding (default 0)");
  36. XUSBATM_PARM(tx_padding, unsigned char, byte, "tx padding (default 0)");
  37. static const char xusbatm_driver_name[] = "xusbatm";
  38. static struct usbatm_driver xusbatm_drivers[XUSBATM_DRIVERS_MAX];
  39. static struct usb_device_id xusbatm_usb_ids[XUSBATM_DRIVERS_MAX + 1];
  40. static struct usb_driver xusbatm_usb_driver;
  41. static int usb_intf_has_ep(const struct usb_interface *intf, u8 ep)
  42. {
  43. int i, j;
  44. for (i = 0; i < intf->num_altsetting; i++) {
  45. struct usb_host_interface *alt = intf->altsetting;
  46. for (j = 0; j < alt->desc.bNumEndpoints; j++)
  47. if ((alt->endpoint[i].desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK) == ep)
  48. return 1;
  49. }
  50. return 0;
  51. }
  52. static int xusbatm_bind(struct usbatm_data *usbatm_instance,
  53. struct usb_interface *intf, const struct usb_device_id *id,
  54. int *need_heavy_init)
  55. {
  56. struct usb_device *usb_dev = interface_to_usbdev(intf);
  57. int drv_ix = id - xusbatm_usb_ids;
  58. int rx_ep_present = usb_intf_has_ep(intf, rx_endpoint[drv_ix]);
  59. int tx_ep_present = usb_intf_has_ep(intf, tx_endpoint[drv_ix]);
  60. u8 searched_ep = rx_ep_present ? tx_endpoint[drv_ix] : rx_endpoint[drv_ix];
  61. int i, ret;
  62. usb_dbg(usbatm_instance, "%s: binding driver %d: vendor %#x product %#x"
  63. " rx: ep %#x padd %d tx: ep %#x padd %d\n",
  64. __func__, drv_ix, vendor[drv_ix], product[drv_ix],
  65. rx_endpoint[drv_ix], rx_padding[drv_ix],
  66. tx_endpoint[drv_ix], tx_padding[drv_ix]);
  67. if (!rx_ep_present && !tx_ep_present) {
  68. usb_dbg(usbatm_instance, "%s: intf #%d has neither rx (%#x) nor tx (%#x) endpoint\n",
  69. __func__, intf->altsetting->desc.bInterfaceNumber,
  70. rx_endpoint[drv_ix], tx_endpoint[drv_ix]);
  71. return -ENODEV;
  72. }
  73. if (rx_ep_present && tx_ep_present)
  74. return 0;
  75. for(i = 0; i < usb_dev->actconfig->desc.bNumInterfaces; i++) {
  76. struct usb_interface *cur_if = usb_dev->actconfig->interface[i];
  77. if (cur_if != intf && usb_intf_has_ep(cur_if, searched_ep)) {
  78. ret = usb_driver_claim_interface(&xusbatm_usb_driver,
  79. cur_if, usbatm_instance);
  80. if (!ret)
  81. usb_err(usbatm_instance, "%s: failed to claim interface #%d (%d)\n",
  82. __func__, cur_if->altsetting->desc.bInterfaceNumber, ret);
  83. return ret;
  84. }
  85. }
  86. usb_err(usbatm_instance, "%s: no interface has endpoint %#x\n",
  87. __func__, searched_ep);
  88. return -ENODEV;
  89. }
  90. static void xusbatm_unbind(struct usbatm_data *usbatm_instance,
  91. struct usb_interface *intf)
  92. {
  93. struct usb_device *usb_dev = interface_to_usbdev(intf);
  94. int i;
  95. usb_dbg(usbatm_instance, "%s entered\n", __func__);
  96. for(i = 0; i < usb_dev->actconfig->desc.bNumInterfaces; i++) {
  97. struct usb_interface *cur_if = usb_dev->actconfig->interface[i];
  98. usb_set_intfdata(cur_if, NULL);
  99. usb_driver_release_interface(&xusbatm_usb_driver, cur_if);
  100. }
  101. }
  102. static int xusbatm_atm_start(struct usbatm_data *usbatm_instance,
  103. struct atm_dev *atm_dev)
  104. {
  105. atm_dbg(usbatm_instance, "%s entered\n", __func__);
  106. /* use random MAC as we've no way to get it from the device */
  107. random_ether_addr(atm_dev->esi);
  108. return 0;
  109. }
  110. static int xusbatm_usb_probe(struct usb_interface *intf,
  111. const struct usb_device_id *id)
  112. {
  113. return usbatm_usb_probe(intf, id,
  114. xusbatm_drivers + (id - xusbatm_usb_ids));
  115. }
  116. static struct usb_driver xusbatm_usb_driver = {
  117. .owner = THIS_MODULE,
  118. .name = xusbatm_driver_name,
  119. .probe = xusbatm_usb_probe,
  120. .disconnect = usbatm_usb_disconnect,
  121. .id_table = xusbatm_usb_ids
  122. };
  123. static int __init xusbatm_init(void)
  124. {
  125. int i;
  126. dbg("xusbatm_init");
  127. if (!num_vendor ||
  128. num_vendor != num_product ||
  129. num_vendor != num_rx_endpoint ||
  130. num_vendor != num_tx_endpoint) {
  131. warn("malformed module parameters");
  132. return -EINVAL;
  133. }
  134. for (i = 0; i < num_vendor; i++) {
  135. xusbatm_usb_ids[i].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
  136. xusbatm_usb_ids[i].idVendor = vendor[i];
  137. xusbatm_usb_ids[i].idProduct = product[i];
  138. xusbatm_drivers[i].owner = THIS_MODULE;
  139. xusbatm_drivers[i].driver_name = xusbatm_driver_name;
  140. xusbatm_drivers[i].bind = xusbatm_bind;
  141. xusbatm_drivers[i].unbind = xusbatm_unbind;
  142. xusbatm_drivers[i].atm_start = xusbatm_atm_start;
  143. xusbatm_drivers[i].in = rx_endpoint[i];
  144. xusbatm_drivers[i].out = tx_endpoint[i];
  145. xusbatm_drivers[i].rx_padding = rx_padding[i];
  146. xusbatm_drivers[i].tx_padding = tx_padding[i];
  147. }
  148. return usb_register(&xusbatm_usb_driver);
  149. }
  150. module_init(xusbatm_init);
  151. static void __exit xusbatm_exit(void)
  152. {
  153. dbg("xusbatm_exit entered");
  154. usb_deregister(&xusbatm_usb_driver);
  155. }
  156. module_exit(xusbatm_exit);
  157. MODULE_AUTHOR("Roman Kagan, Duncan Sands");
  158. MODULE_DESCRIPTION("Driver for USB ADSL modems initialized in userspace");
  159. MODULE_LICENSE("GPL");
  160. MODULE_VERSION("0.1");