xusbatm.c 6.9 KB

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