Linux 4.16.11
[linux/fpc-iii.git] / drivers / usb / atm / xusbatm.c
blobffc9810070a3f697903a5eca57d43b93e92ffc4a
1 // SPDX-License-Identifier: GPL-2.0+
2 /******************************************************************************
3 * xusbatm.c - dumb usbatm-based driver for modems initialized in userspace
5 * Copyright (C) 2005 Duncan Sands, Roman Kagan (rkagan % mail ! ru)
6 ******************************************************************************/
8 #include <linux/module.h>
9 #include <linux/etherdevice.h> /* for eth_random_addr() */
11 #include "usbatm.h"
14 #define XUSBATM_DRIVERS_MAX 8
16 #define XUSBATM_PARM(name, type, parmtype, desc) \
17 static type name[XUSBATM_DRIVERS_MAX]; \
18 static unsigned int num_##name; \
19 module_param_array(name, parmtype, &num_##name, 0444); \
20 MODULE_PARM_DESC(name, desc)
22 XUSBATM_PARM(vendor, unsigned short, ushort, "USB device vendor");
23 XUSBATM_PARM(product, unsigned short, ushort, "USB device product");
25 XUSBATM_PARM(rx_endpoint, unsigned char, byte, "rx endpoint number");
26 XUSBATM_PARM(tx_endpoint, unsigned char, byte, "tx endpoint number");
27 XUSBATM_PARM(rx_padding, unsigned char, byte, "rx padding (default 0)");
28 XUSBATM_PARM(tx_padding, unsigned char, byte, "tx padding (default 0)");
29 XUSBATM_PARM(rx_altsetting, unsigned char, byte, "rx altsetting (default 0)");
30 XUSBATM_PARM(tx_altsetting, unsigned char, byte, "rx altsetting (default 0)");
32 static const char xusbatm_driver_name[] = "xusbatm";
34 static struct usbatm_driver xusbatm_drivers[XUSBATM_DRIVERS_MAX];
35 static struct usb_device_id xusbatm_usb_ids[XUSBATM_DRIVERS_MAX + 1];
36 static struct usb_driver xusbatm_usb_driver;
38 static struct usb_interface *xusbatm_find_intf(struct usb_device *usb_dev, int altsetting, u8 ep)
40 struct usb_host_interface *alt;
41 struct usb_interface *intf;
42 int i, j;
44 for (i = 0; i < usb_dev->actconfig->desc.bNumInterfaces; i++)
45 if ((intf = usb_dev->actconfig->interface[i]) && (alt = usb_altnum_to_altsetting(intf, altsetting)))
46 for (j = 0; j < alt->desc.bNumEndpoints; j++)
47 if (alt->endpoint[j].desc.bEndpointAddress == ep)
48 return intf;
49 return NULL;
52 static int xusbatm_capture_intf(struct usbatm_data *usbatm, struct usb_device *usb_dev,
53 struct usb_interface *intf, int altsetting, int claim)
55 int ifnum = intf->altsetting->desc.bInterfaceNumber;
56 int ret;
58 if (claim && (ret = usb_driver_claim_interface(&xusbatm_usb_driver, intf, usbatm))) {
59 usb_err(usbatm, "%s: failed to claim interface %2d (%d)!\n", __func__, ifnum, ret);
60 return ret;
62 ret = usb_set_interface(usb_dev, ifnum, altsetting);
63 if (ret) {
64 usb_err(usbatm, "%s: altsetting %2d for interface %2d failed (%d)!\n", __func__, altsetting, ifnum, ret);
65 return ret;
67 return 0;
70 static void xusbatm_release_intf(struct usb_device *usb_dev, struct usb_interface *intf, int claimed)
72 if (claimed) {
73 usb_set_intfdata(intf, NULL);
74 usb_driver_release_interface(&xusbatm_usb_driver, intf);
78 static int xusbatm_bind(struct usbatm_data *usbatm,
79 struct usb_interface *intf, const struct usb_device_id *id)
81 struct usb_device *usb_dev = interface_to_usbdev(intf);
82 int drv_ix = id - xusbatm_usb_ids;
83 int rx_alt = rx_altsetting[drv_ix];
84 int tx_alt = tx_altsetting[drv_ix];
85 struct usb_interface *rx_intf = xusbatm_find_intf(usb_dev, rx_alt, rx_endpoint[drv_ix]);
86 struct usb_interface *tx_intf = xusbatm_find_intf(usb_dev, tx_alt, tx_endpoint[drv_ix]);
87 int ret;
89 usb_dbg(usbatm, "%s: binding driver %d: vendor %04x product %04x"
90 " rx: ep %02x padd %d alt %2d tx: ep %02x padd %d alt %2d\n",
91 __func__, drv_ix, vendor[drv_ix], product[drv_ix],
92 rx_endpoint[drv_ix], rx_padding[drv_ix], rx_alt,
93 tx_endpoint[drv_ix], tx_padding[drv_ix], tx_alt);
95 if (!rx_intf || !tx_intf) {
96 if (!rx_intf)
97 usb_dbg(usbatm, "%s: no interface contains endpoint %02x in altsetting %2d\n",
98 __func__, rx_endpoint[drv_ix], rx_alt);
99 if (!tx_intf)
100 usb_dbg(usbatm, "%s: no interface contains endpoint %02x in altsetting %2d\n",
101 __func__, tx_endpoint[drv_ix], tx_alt);
102 return -ENODEV;
105 if ((rx_intf != intf) && (tx_intf != intf))
106 return -ENODEV;
108 if ((rx_intf == tx_intf) && (rx_alt != tx_alt)) {
109 usb_err(usbatm, "%s: altsettings clash on interface %2d (%2d vs %2d)!\n", __func__,
110 rx_intf->altsetting->desc.bInterfaceNumber, rx_alt, tx_alt);
111 return -EINVAL;
114 usb_dbg(usbatm, "%s: rx If#=%2d; tx If#=%2d\n", __func__,
115 rx_intf->altsetting->desc.bInterfaceNumber,
116 tx_intf->altsetting->desc.bInterfaceNumber);
118 ret = xusbatm_capture_intf(usbatm, usb_dev, rx_intf, rx_alt, rx_intf != intf);
119 if (ret)
120 return ret;
122 if ((tx_intf != rx_intf) && (ret = xusbatm_capture_intf(usbatm, usb_dev, tx_intf, tx_alt, tx_intf != intf))) {
123 xusbatm_release_intf(usb_dev, rx_intf, rx_intf != intf);
124 return ret;
127 return 0;
130 static void xusbatm_unbind(struct usbatm_data *usbatm,
131 struct usb_interface *intf)
133 struct usb_device *usb_dev = interface_to_usbdev(intf);
134 int i;
136 usb_dbg(usbatm, "%s entered\n", __func__);
138 for (i = 0; i < usb_dev->actconfig->desc.bNumInterfaces; i++) {
139 struct usb_interface *cur_intf = usb_dev->actconfig->interface[i];
141 if (cur_intf && (usb_get_intfdata(cur_intf) == usbatm)) {
142 usb_set_intfdata(cur_intf, NULL);
143 usb_driver_release_interface(&xusbatm_usb_driver, cur_intf);
148 static int xusbatm_atm_start(struct usbatm_data *usbatm,
149 struct atm_dev *atm_dev)
151 atm_dbg(usbatm, "%s entered\n", __func__);
153 /* use random MAC as we've no way to get it from the device */
154 eth_random_addr(atm_dev->esi);
156 return 0;
160 static int xusbatm_usb_probe(struct usb_interface *intf,
161 const struct usb_device_id *id)
163 return usbatm_usb_probe(intf, id,
164 xusbatm_drivers + (id - xusbatm_usb_ids));
167 static struct usb_driver xusbatm_usb_driver = {
168 .name = xusbatm_driver_name,
169 .probe = xusbatm_usb_probe,
170 .disconnect = usbatm_usb_disconnect,
171 .id_table = xusbatm_usb_ids
174 static int __init xusbatm_init(void)
176 int i;
178 if (!num_vendor ||
179 num_vendor != num_product ||
180 num_vendor != num_rx_endpoint ||
181 num_vendor != num_tx_endpoint) {
182 printk(KERN_WARNING "xusbatm: malformed module parameters\n");
183 return -EINVAL;
186 for (i = 0; i < num_vendor; i++) {
187 rx_endpoint[i] |= USB_DIR_IN;
188 tx_endpoint[i] &= USB_ENDPOINT_NUMBER_MASK;
190 xusbatm_usb_ids[i].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
191 xusbatm_usb_ids[i].idVendor = vendor[i];
192 xusbatm_usb_ids[i].idProduct = product[i];
194 xusbatm_drivers[i].driver_name = xusbatm_driver_name;
195 xusbatm_drivers[i].bind = xusbatm_bind;
196 xusbatm_drivers[i].unbind = xusbatm_unbind;
197 xusbatm_drivers[i].atm_start = xusbatm_atm_start;
198 xusbatm_drivers[i].bulk_in = rx_endpoint[i];
199 xusbatm_drivers[i].bulk_out = tx_endpoint[i];
200 xusbatm_drivers[i].rx_padding = rx_padding[i];
201 xusbatm_drivers[i].tx_padding = tx_padding[i];
204 return usb_register(&xusbatm_usb_driver);
206 module_init(xusbatm_init);
208 static void __exit xusbatm_exit(void)
210 usb_deregister(&xusbatm_usb_driver);
212 module_exit(xusbatm_exit);
214 MODULE_AUTHOR("Roman Kagan, Duncan Sands");
215 MODULE_DESCRIPTION("Driver for USB ADSL modems initialized in userspace");
216 MODULE_LICENSE("GPL");