xusbatm.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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,
  53. struct usb_interface *intf, const struct usb_device_id *id)
  54. {
  55. struct usb_device *usb_dev = interface_to_usbdev(intf);
  56. int drv_ix = id - xusbatm_usb_ids;
  57. int rx_ep_present = usb_intf_has_ep(intf, rx_endpoint[drv_ix]);
  58. int tx_ep_present = usb_intf_has_ep(intf, tx_endpoint[drv_ix]);
  59. u8 searched_ep = rx_ep_present ? tx_endpoint[drv_ix] : rx_endpoint[drv_ix];
  60. int i, ret;
  61. usb_dbg(usbatm, "%s: binding driver %d: vendor %04x product %04x"
  62. " rx: ep %02x padd %d tx: ep %02x padd %d\n",
  63. __func__, drv_ix, vendor[drv_ix], product[drv_ix],
  64. rx_endpoint[drv_ix], rx_padding[drv_ix],
  65. tx_endpoint[drv_ix], tx_padding[drv_ix]);
  66. if (!rx_ep_present && !tx_ep_present) {
  67. usb_dbg(usbatm, "%s: intf #%d has neither rx (%#x) nor tx (%#x) endpoint\n",
  68. __func__, intf->altsetting->desc.bInterfaceNumber,
  69. rx_endpoint[drv_ix], tx_endpoint[drv_ix]);
  70. return -ENODEV;
  71. }
  72. if (rx_ep_present && tx_ep_present)
  73. return 0;
  74. for(i = 0; i < usb_dev->actconfig->desc.bNumInterfaces; i++) {
  75. struct usb_interface *cur_if = usb_dev->actconfig->interface[i];
  76. if (cur_if != intf && usb_intf_has_ep(cur_if, searched_ep)) {
  77. ret = usb_driver_claim_interface(&xusbatm_usb_driver,
  78. cur_if, usbatm);
  79. if (!ret)
  80. usb_err(usbatm, "%s: failed to claim interface #%d (%d)\n",
  81. __func__, cur_if->altsetting->desc.bInterfaceNumber, ret);
  82. return ret;
  83. }
  84. }
  85. usb_err(usbatm, "%s: no interface has endpoint %#x\n",
  86. __func__, searched_ep);
  87. return -ENODEV;
  88. }
  89. static void xusbatm_unbind(struct usbatm_data *usbatm,
  90. struct usb_interface *intf)
  91. {
  92. struct usb_device *usb_dev = interface_to_usbdev(intf);
  93. int i;
  94. usb_dbg(usbatm, "%s entered\n", __func__);
  95. for(i = 0; i < usb_dev->actconfig->desc.bNumInterfaces; i++) {
  96. struct usb_interface *cur_if = usb_dev->actconfig->interface[i];
  97. usb_set_intfdata(cur_if, NULL);
  98. usb_driver_release_interface(&xusbatm_usb_driver, cur_if);
  99. }
  100. }
  101. static int xusbatm_atm_start(struct usbatm_data *usbatm,
  102. struct atm_dev *atm_dev)
  103. {
  104. atm_dbg(usbatm, "%s entered\n", __func__);
  105. /* use random MAC as we've no way to get it from the device */
  106. random_ether_addr(atm_dev->esi);
  107. return 0;
  108. }
  109. static int xusbatm_usb_probe(struct usb_interface *intf,
  110. const struct usb_device_id *id)
  111. {
  112. return usbatm_usb_probe(intf, id,
  113. xusbatm_drivers + (id - xusbatm_usb_ids));
  114. }
  115. static struct usb_driver xusbatm_usb_driver = {
  116. .name = xusbatm_driver_name,
  117. .probe = xusbatm_usb_probe,
  118. .disconnect = usbatm_usb_disconnect,
  119. .id_table = xusbatm_usb_ids
  120. };
  121. static int __init xusbatm_init(void)
  122. {
  123. int i;
  124. dbg("xusbatm_init");
  125. if (!num_vendor ||
  126. num_vendor != num_product ||
  127. num_vendor != num_rx_endpoint ||
  128. num_vendor != num_tx_endpoint) {
  129. warn("malformed module parameters");
  130. return -EINVAL;
  131. }
  132. for (i = 0; i < num_vendor; i++) {
  133. xusbatm_usb_ids[i].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
  134. xusbatm_usb_ids[i].idVendor = vendor[i];
  135. xusbatm_usb_ids[i].idProduct = product[i];
  136. xusbatm_drivers[i].owner = THIS_MODULE;
  137. xusbatm_drivers[i].driver_name = xusbatm_driver_name;
  138. xusbatm_drivers[i].bind = xusbatm_bind;
  139. xusbatm_drivers[i].unbind = xusbatm_unbind;
  140. xusbatm_drivers[i].atm_start = xusbatm_atm_start;
  141. xusbatm_drivers[i].in = rx_endpoint[i];
  142. xusbatm_drivers[i].out = tx_endpoint[i];
  143. xusbatm_drivers[i].rx_padding = rx_padding[i];
  144. xusbatm_drivers[i].tx_padding = tx_padding[i];
  145. }
  146. return usb_register(&xusbatm_usb_driver);
  147. }
  148. module_init(xusbatm_init);
  149. static void __exit xusbatm_exit(void)
  150. {
  151. dbg("xusbatm_exit entered");
  152. usb_deregister(&xusbatm_usb_driver);
  153. }
  154. module_exit(xusbatm_exit);
  155. MODULE_AUTHOR("Roman Kagan, Duncan Sands");
  156. MODULE_DESCRIPTION("Driver for USB ADSL modems initialized in userspace");
  157. MODULE_LICENSE("GPL");
  158. MODULE_VERSION("0.1");