blob: 172c821743971df8012d8c709659bc39650fcbfc [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>
23#include <linux/netdevice.h> /* FIXME: required by linux/etherdevice.h */
24#include <linux/etherdevice.h> /* for random_ether_addr() */
25
26#include "usbatm.h"
27
28
29#define XUSBATM_DRIVERS_MAX 8
30
31#define XUSBATM_PARM(name, type, parmtype, desc) \
32 static type name[XUSBATM_DRIVERS_MAX]; \
33 static int num_##name; \
34 module_param_array(name, parmtype, &num_##name, 0444); \
35 MODULE_PARM_DESC(name, desc)
36
37XUSBATM_PARM(vendor, unsigned short, ushort, "USB device vendor");
38XUSBATM_PARM(product, unsigned short, ushort, "USB device product");
39
40XUSBATM_PARM(rx_endpoint, unsigned char, byte, "rx endpoint number");
41XUSBATM_PARM(tx_endpoint, unsigned char, byte, "tx endpoint number");
42XUSBATM_PARM(rx_padding, unsigned char, byte, "rx padding (default 0)");
43XUSBATM_PARM(tx_padding, unsigned char, byte, "tx padding (default 0)");
44
45static const char xusbatm_driver_name[] = "xusbatm";
46
47static struct usbatm_driver xusbatm_drivers[XUSBATM_DRIVERS_MAX];
48static struct usb_device_id xusbatm_usb_ids[XUSBATM_DRIVERS_MAX + 1];
49static struct usb_driver xusbatm_usb_driver;
50
51static int usb_intf_has_ep(const struct usb_interface *intf, u8 ep)
52{
53 int i, j;
54
55 for (i = 0; i < intf->num_altsetting; i++) {
56 struct usb_host_interface *alt = intf->altsetting;
57 for (j = 0; j < alt->desc.bNumEndpoints; j++)
58 if ((alt->endpoint[i].desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK) == ep)
59 return 1;
60 }
61 return 0;
62}
63
Duncan Sands0ec3c7e2006-01-17 11:15:13 +010064static int xusbatm_bind(struct usbatm_data *usbatm,
Duncan Sands35644b02006-01-17 11:16:13 +010065 struct usb_interface *intf, const struct usb_device_id *id)
Duncan Sands0bb3cf32005-05-11 20:17:09 +020066{
67 struct usb_device *usb_dev = interface_to_usbdev(intf);
68 int drv_ix = id - xusbatm_usb_ids;
69 int rx_ep_present = usb_intf_has_ep(intf, rx_endpoint[drv_ix]);
70 int tx_ep_present = usb_intf_has_ep(intf, tx_endpoint[drv_ix]);
71 u8 searched_ep = rx_ep_present ? tx_endpoint[drv_ix] : rx_endpoint[drv_ix];
72 int i, ret;
73
Duncan Sands0ec3c7e2006-01-17 11:15:13 +010074 usb_dbg(usbatm, "%s: binding driver %d: vendor %04x product %04x"
75 " rx: ep %02x padd %d tx: ep %02x padd %d\n",
Duncan Sands0bb3cf32005-05-11 20:17:09 +020076 __func__, drv_ix, vendor[drv_ix], product[drv_ix],
77 rx_endpoint[drv_ix], rx_padding[drv_ix],
78 tx_endpoint[drv_ix], tx_padding[drv_ix]);
79
80 if (!rx_ep_present && !tx_ep_present) {
Duncan Sands0ec3c7e2006-01-17 11:15:13 +010081 usb_dbg(usbatm, "%s: intf #%d has neither rx (%#x) nor tx (%#x) endpoint\n",
Duncan Sands0bb3cf32005-05-11 20:17:09 +020082 __func__, intf->altsetting->desc.bInterfaceNumber,
83 rx_endpoint[drv_ix], tx_endpoint[drv_ix]);
84 return -ENODEV;
85 }
86
87 if (rx_ep_present && tx_ep_present)
88 return 0;
89
90 for(i = 0; i < usb_dev->actconfig->desc.bNumInterfaces; i++) {
91 struct usb_interface *cur_if = usb_dev->actconfig->interface[i];
92
93 if (cur_if != intf && usb_intf_has_ep(cur_if, searched_ep)) {
94 ret = usb_driver_claim_interface(&xusbatm_usb_driver,
Duncan Sands0ec3c7e2006-01-17 11:15:13 +010095 cur_if, usbatm);
Duncan Sands0bb3cf32005-05-11 20:17:09 +020096 if (!ret)
Duncan Sands0ec3c7e2006-01-17 11:15:13 +010097 usb_err(usbatm, "%s: failed to claim interface #%d (%d)\n",
Duncan Sands0bb3cf32005-05-11 20:17:09 +020098 __func__, cur_if->altsetting->desc.bInterfaceNumber, ret);
99 return ret;
100 }
101 }
102
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100103 usb_err(usbatm, "%s: no interface has endpoint %#x\n",
Duncan Sands0bb3cf32005-05-11 20:17:09 +0200104 __func__, searched_ep);
105 return -ENODEV;
106}
107
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100108static void xusbatm_unbind(struct usbatm_data *usbatm,
Duncan Sands0bb3cf32005-05-11 20:17:09 +0200109 struct usb_interface *intf)
110{
111 struct usb_device *usb_dev = interface_to_usbdev(intf);
112 int i;
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100113
114 usb_dbg(usbatm, "%s entered\n", __func__);
Duncan Sands0bb3cf32005-05-11 20:17:09 +0200115
116 for(i = 0; i < usb_dev->actconfig->desc.bNumInterfaces; i++) {
117 struct usb_interface *cur_if = usb_dev->actconfig->interface[i];
118 usb_set_intfdata(cur_if, NULL);
119 usb_driver_release_interface(&xusbatm_usb_driver, cur_if);
120 }
121}
122
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100123static int xusbatm_atm_start(struct usbatm_data *usbatm,
Duncan Sands0bb3cf32005-05-11 20:17:09 +0200124 struct atm_dev *atm_dev)
125{
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100126 atm_dbg(usbatm, "%s entered\n", __func__);
Duncan Sands0bb3cf32005-05-11 20:17:09 +0200127
128 /* use random MAC as we've no way to get it from the device */
129 random_ether_addr(atm_dev->esi);
130
131 return 0;
132}
133
134
135static int xusbatm_usb_probe(struct usb_interface *intf,
136 const struct usb_device_id *id)
137{
138 return usbatm_usb_probe(intf, id,
139 xusbatm_drivers + (id - xusbatm_usb_ids));
140}
141
142static struct usb_driver xusbatm_usb_driver = {
Duncan Sands0bb3cf32005-05-11 20:17:09 +0200143 .name = xusbatm_driver_name,
144 .probe = xusbatm_usb_probe,
145 .disconnect = usbatm_usb_disconnect,
146 .id_table = xusbatm_usb_ids
147};
148
149static int __init xusbatm_init(void)
150{
151 int i;
152
153 dbg("xusbatm_init");
154
155 if (!num_vendor ||
156 num_vendor != num_product ||
157 num_vendor != num_rx_endpoint ||
158 num_vendor != num_tx_endpoint) {
159 warn("malformed module parameters");
160 return -EINVAL;
161 }
162
163 for (i = 0; i < num_vendor; i++) {
164 xusbatm_usb_ids[i].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
165 xusbatm_usb_ids[i].idVendor = vendor[i];
166 xusbatm_usb_ids[i].idProduct = product[i];
167
168
169 xusbatm_drivers[i].owner = THIS_MODULE;
170 xusbatm_drivers[i].driver_name = xusbatm_driver_name;
171 xusbatm_drivers[i].bind = xusbatm_bind;
172 xusbatm_drivers[i].unbind = xusbatm_unbind;
173 xusbatm_drivers[i].atm_start = xusbatm_atm_start;
174 xusbatm_drivers[i].in = rx_endpoint[i];
175 xusbatm_drivers[i].out = tx_endpoint[i];
176 xusbatm_drivers[i].rx_padding = rx_padding[i];
177 xusbatm_drivers[i].tx_padding = tx_padding[i];
178 }
179
180 return usb_register(&xusbatm_usb_driver);
181}
182module_init(xusbatm_init);
183
184static void __exit xusbatm_exit(void)
185{
186 dbg("xusbatm_exit entered");
187
188 usb_deregister(&xusbatm_usb_driver);
189}
190module_exit(xusbatm_exit);
191
192MODULE_AUTHOR("Roman Kagan, Duncan Sands");
193MODULE_DESCRIPTION("Driver for USB ADSL modems initialized in userspace");
194MODULE_LICENSE("GPL");
195MODULE_VERSION("0.1");