blob: b3b1bb78b2ef458bb1bd78b3825f9895e24762a8 [file] [log] [blame]
Duncan Sands0bb3cf32005-05-11 20:17:09 +02001/******************************************************************************
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
22#include <linux/module.h>
Joe Perches006c9132012-07-12 22:33:11 -070023#include <linux/etherdevice.h> /* for eth_random_addr() */
Duncan Sands0bb3cf32005-05-11 20:17:09 +020024
25#include "usbatm.h"
26
27
28#define XUSBATM_DRIVERS_MAX 8
29
30#define XUSBATM_PARM(name, type, parmtype, desc) \
31 static type name[XUSBATM_DRIVERS_MAX]; \
Al Viro64a6f952007-10-14 19:35:30 +010032 static unsigned int num_##name; \
Duncan Sands0bb3cf32005-05-11 20:17:09 +020033 module_param_array(name, parmtype, &num_##name, 0444); \
34 MODULE_PARM_DESC(name, desc)
35
36XUSBATM_PARM(vendor, unsigned short, ushort, "USB device vendor");
37XUSBATM_PARM(product, unsigned short, ushort, "USB device product");
38
39XUSBATM_PARM(rx_endpoint, unsigned char, byte, "rx endpoint number");
40XUSBATM_PARM(tx_endpoint, unsigned char, byte, "tx endpoint number");
41XUSBATM_PARM(rx_padding, unsigned char, byte, "rx padding (default 0)");
42XUSBATM_PARM(tx_padding, unsigned char, byte, "tx padding (default 0)");
Duncan Sands233c08e2006-01-13 09:48:36 +010043XUSBATM_PARM(rx_altsetting, unsigned char, byte, "rx altsetting (default 0)");
44XUSBATM_PARM(tx_altsetting, unsigned char, byte, "rx altsetting (default 0)");
Duncan Sands0bb3cf32005-05-11 20:17:09 +020045
46static const char xusbatm_driver_name[] = "xusbatm";
47
48static struct usbatm_driver xusbatm_drivers[XUSBATM_DRIVERS_MAX];
49static struct usb_device_id xusbatm_usb_ids[XUSBATM_DRIVERS_MAX + 1];
50static struct usb_driver xusbatm_usb_driver;
51
Nicolas Kaiser16f76a72010-06-17 11:55:49 +020052static struct usb_interface *xusbatm_find_intf(struct usb_device *usb_dev, int altsetting, u8 ep)
Duncan Sands0bb3cf32005-05-11 20:17:09 +020053{
Duncan Sands233c08e2006-01-13 09:48:36 +010054 struct usb_host_interface *alt;
55 struct usb_interface *intf;
Duncan Sands0bb3cf32005-05-11 20:17:09 +020056 int i, j;
57
Nicolas Kaiser16f76a72010-06-17 11:55:49 +020058 for (i = 0; i < usb_dev->actconfig->desc.bNumInterfaces; i++)
Duncan Sands233c08e2006-01-13 09:48:36 +010059 if ((intf = usb_dev->actconfig->interface[i]) && (alt = usb_altnum_to_altsetting(intf, altsetting)))
60 for (j = 0; j < alt->desc.bNumEndpoints; j++)
61 if (alt->endpoint[j].desc.bEndpointAddress == ep)
62 return intf;
63 return NULL;
64}
65
Nicolas Kaiser16f76a72010-06-17 11:55:49 +020066static int xusbatm_capture_intf(struct usbatm_data *usbatm, struct usb_device *usb_dev,
Duncan Sands233c08e2006-01-13 09:48:36 +010067 struct usb_interface *intf, int altsetting, int claim)
68{
69 int ifnum = intf->altsetting->desc.bInterfaceNumber;
70 int ret;
71
72 if (claim && (ret = usb_driver_claim_interface(&xusbatm_usb_driver, intf, usbatm))) {
73 usb_err(usbatm, "%s: failed to claim interface %2d (%d)!\n", __func__, ifnum, ret);
74 return ret;
75 }
76 if ((ret = usb_set_interface(usb_dev, ifnum, altsetting))) {
77 usb_err(usbatm, "%s: altsetting %2d for interface %2d failed (%d)!\n", __func__, altsetting, ifnum, ret);
78 return ret;
Duncan Sands0bb3cf32005-05-11 20:17:09 +020079 }
80 return 0;
81}
82
Nicolas Kaiser16f76a72010-06-17 11:55:49 +020083static void xusbatm_release_intf(struct usb_device *usb_dev, struct usb_interface *intf, int claimed)
Duncan Sands233c08e2006-01-13 09:48:36 +010084{
85 if (claimed) {
86 usb_set_intfdata(intf, NULL);
87 usb_driver_release_interface(&xusbatm_usb_driver, intf);
88 }
89}
90
Duncan Sands0ec3c7e2006-01-17 11:15:13 +010091static int xusbatm_bind(struct usbatm_data *usbatm,
Duncan Sands35644b02006-01-17 11:16:13 +010092 struct usb_interface *intf, const struct usb_device_id *id)
Duncan Sands0bb3cf32005-05-11 20:17:09 +020093{
94 struct usb_device *usb_dev = interface_to_usbdev(intf);
95 int drv_ix = id - xusbatm_usb_ids;
Duncan Sands233c08e2006-01-13 09:48:36 +010096 int rx_alt = rx_altsetting[drv_ix];
97 int tx_alt = tx_altsetting[drv_ix];
98 struct usb_interface *rx_intf = xusbatm_find_intf(usb_dev, rx_alt, rx_endpoint[drv_ix]);
99 struct usb_interface *tx_intf = xusbatm_find_intf(usb_dev, tx_alt, tx_endpoint[drv_ix]);
100 int ret;
Duncan Sands0bb3cf32005-05-11 20:17:09 +0200101
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100102 usb_dbg(usbatm, "%s: binding driver %d: vendor %04x product %04x"
Duncan Sands233c08e2006-01-13 09:48:36 +0100103 " rx: ep %02x padd %d alt %2d tx: ep %02x padd %d alt %2d\n",
Duncan Sands0bb3cf32005-05-11 20:17:09 +0200104 __func__, drv_ix, vendor[drv_ix], product[drv_ix],
Duncan Sands233c08e2006-01-13 09:48:36 +0100105 rx_endpoint[drv_ix], rx_padding[drv_ix], rx_alt,
106 tx_endpoint[drv_ix], tx_padding[drv_ix], tx_alt);
Duncan Sands0bb3cf32005-05-11 20:17:09 +0200107
Duncan Sands233c08e2006-01-13 09:48:36 +0100108 if (!rx_intf || !tx_intf) {
109 if (!rx_intf)
110 usb_dbg(usbatm, "%s: no interface contains endpoint %02x in altsetting %2d\n",
111 __func__, rx_endpoint[drv_ix], rx_alt);
112 if (!tx_intf)
113 usb_dbg(usbatm, "%s: no interface contains endpoint %02x in altsetting %2d\n",
114 __func__, tx_endpoint[drv_ix], tx_alt);
Duncan Sands0bb3cf32005-05-11 20:17:09 +0200115 return -ENODEV;
116 }
117
Duncan Sands233c08e2006-01-13 09:48:36 +0100118 if ((rx_intf != intf) && (tx_intf != intf))
119 return -ENODEV;
Duncan Sands0bb3cf32005-05-11 20:17:09 +0200120
Duncan Sands233c08e2006-01-13 09:48:36 +0100121 if ((rx_intf == tx_intf) && (rx_alt != tx_alt)) {
122 usb_err(usbatm, "%s: altsettings clash on interface %2d (%2d vs %2d)!\n", __func__,
123 rx_intf->altsetting->desc.bInterfaceNumber, rx_alt, tx_alt);
124 return -EINVAL;
Duncan Sands0bb3cf32005-05-11 20:17:09 +0200125 }
126
Duncan Sands233c08e2006-01-13 09:48:36 +0100127 usb_dbg(usbatm, "%s: rx If#=%2d; tx If#=%2d\n", __func__,
128 rx_intf->altsetting->desc.bInterfaceNumber,
129 tx_intf->altsetting->desc.bInterfaceNumber);
130
131 if ((ret = xusbatm_capture_intf(usbatm, usb_dev, rx_intf, rx_alt, rx_intf != intf)))
132 return ret;
133
134 if ((tx_intf != rx_intf) && (ret = xusbatm_capture_intf(usbatm, usb_dev, tx_intf, tx_alt, tx_intf != intf))) {
135 xusbatm_release_intf(usb_dev, rx_intf, rx_intf != intf);
136 return ret;
137 }
138
139 return 0;
Duncan Sands0bb3cf32005-05-11 20:17:09 +0200140}
141
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100142static void xusbatm_unbind(struct usbatm_data *usbatm,
Duncan Sands0bb3cf32005-05-11 20:17:09 +0200143 struct usb_interface *intf)
144{
145 struct usb_device *usb_dev = interface_to_usbdev(intf);
146 int i;
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100147
148 usb_dbg(usbatm, "%s entered\n", __func__);
Duncan Sands0bb3cf32005-05-11 20:17:09 +0200149
Nicolas Kaiser16f76a72010-06-17 11:55:49 +0200150 for (i = 0; i < usb_dev->actconfig->desc.bNumInterfaces; i++) {
Duncan Sands233c08e2006-01-13 09:48:36 +0100151 struct usb_interface *cur_intf = usb_dev->actconfig->interface[i];
152
153 if (cur_intf && (usb_get_intfdata(cur_intf) == usbatm)) {
154 usb_set_intfdata(cur_intf, NULL);
155 usb_driver_release_interface(&xusbatm_usb_driver, cur_intf);
156 }
Duncan Sands0bb3cf32005-05-11 20:17:09 +0200157 }
158}
159
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100160static int xusbatm_atm_start(struct usbatm_data *usbatm,
Duncan Sands0bb3cf32005-05-11 20:17:09 +0200161 struct atm_dev *atm_dev)
162{
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100163 atm_dbg(usbatm, "%s entered\n", __func__);
Duncan Sands0bb3cf32005-05-11 20:17:09 +0200164
165 /* use random MAC as we've no way to get it from the device */
Joe Perches006c9132012-07-12 22:33:11 -0700166 eth_random_addr(atm_dev->esi);
Duncan Sands0bb3cf32005-05-11 20:17:09 +0200167
168 return 0;
169}
170
171
172static int xusbatm_usb_probe(struct usb_interface *intf,
173 const struct usb_device_id *id)
174{
175 return usbatm_usb_probe(intf, id,
176 xusbatm_drivers + (id - xusbatm_usb_ids));
177}
178
179static struct usb_driver xusbatm_usb_driver = {
Duncan Sands0bb3cf32005-05-11 20:17:09 +0200180 .name = xusbatm_driver_name,
181 .probe = xusbatm_usb_probe,
182 .disconnect = usbatm_usb_disconnect,
183 .id_table = xusbatm_usb_ids
184};
185
186static int __init xusbatm_init(void)
187{
188 int i;
189
Duncan Sands0bb3cf32005-05-11 20:17:09 +0200190 if (!num_vendor ||
191 num_vendor != num_product ||
192 num_vendor != num_rx_endpoint ||
193 num_vendor != num_tx_endpoint) {
Greg Kroah-Hartman3b6004f2008-08-14 09:37:34 -0700194 printk(KERN_WARNING "xusbatm: malformed module parameters\n");
Duncan Sands0bb3cf32005-05-11 20:17:09 +0200195 return -EINVAL;
196 }
197
198 for (i = 0; i < num_vendor; i++) {
Duncan Sands233c08e2006-01-13 09:48:36 +0100199 rx_endpoint[i] |= USB_DIR_IN;
200 tx_endpoint[i] &= USB_ENDPOINT_NUMBER_MASK;
201
Duncan Sands0bb3cf32005-05-11 20:17:09 +0200202 xusbatm_usb_ids[i].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
203 xusbatm_usb_ids[i].idVendor = vendor[i];
204 xusbatm_usb_ids[i].idProduct = product[i];
205
Duncan Sands0bb3cf32005-05-11 20:17:09 +0200206 xusbatm_drivers[i].driver_name = xusbatm_driver_name;
207 xusbatm_drivers[i].bind = xusbatm_bind;
208 xusbatm_drivers[i].unbind = xusbatm_unbind;
209 xusbatm_drivers[i].atm_start = xusbatm_atm_start;
Duncan Sands80aae7a2006-01-13 10:59:23 +0100210 xusbatm_drivers[i].bulk_in = rx_endpoint[i];
211 xusbatm_drivers[i].bulk_out = tx_endpoint[i];
Duncan Sands0bb3cf32005-05-11 20:17:09 +0200212 xusbatm_drivers[i].rx_padding = rx_padding[i];
213 xusbatm_drivers[i].tx_padding = tx_padding[i];
214 }
215
216 return usb_register(&xusbatm_usb_driver);
217}
218module_init(xusbatm_init);
219
220static void __exit xusbatm_exit(void)
221{
Duncan Sands0bb3cf32005-05-11 20:17:09 +0200222 usb_deregister(&xusbatm_usb_driver);
223}
224module_exit(xusbatm_exit);
225
226MODULE_AUTHOR("Roman Kagan, Duncan Sands");
227MODULE_DESCRIPTION("Driver for USB ADSL modems initialized in userspace");
228MODULE_LICENSE("GPL");
229MODULE_VERSION("0.1");