blob: 83848ee2c6fe28ed1fb55fa67826399724e6602a [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)");
Duncan Sands233c08e2006-01-13 09:48:36 +010044XUSBATM_PARM(rx_altsetting, unsigned char, byte, "rx altsetting (default 0)");
45XUSBATM_PARM(tx_altsetting, unsigned char, byte, "rx altsetting (default 0)");
Duncan Sands0bb3cf32005-05-11 20:17:09 +020046
47static const char xusbatm_driver_name[] = "xusbatm";
48
49static struct usbatm_driver xusbatm_drivers[XUSBATM_DRIVERS_MAX];
50static struct usb_device_id xusbatm_usb_ids[XUSBATM_DRIVERS_MAX + 1];
51static struct usb_driver xusbatm_usb_driver;
52
Duncan Sands233c08e2006-01-13 09:48:36 +010053static struct usb_interface *xusbatm_find_intf (struct usb_device *usb_dev, int altsetting, u8 ep)
Duncan Sands0bb3cf32005-05-11 20:17:09 +020054{
Duncan Sands233c08e2006-01-13 09:48:36 +010055 struct usb_host_interface *alt;
56 struct usb_interface *intf;
Duncan Sands0bb3cf32005-05-11 20:17:09 +020057 int i, j;
58
Duncan Sands233c08e2006-01-13 09:48:36 +010059 for(i = 0; i < usb_dev->actconfig->desc.bNumInterfaces; i++)
60 if ((intf = usb_dev->actconfig->interface[i]) && (alt = usb_altnum_to_altsetting(intf, altsetting)))
61 for (j = 0; j < alt->desc.bNumEndpoints; j++)
62 if (alt->endpoint[j].desc.bEndpointAddress == ep)
63 return intf;
64 return NULL;
65}
66
67static int xusbatm_capture_intf (struct usbatm_data *usbatm, struct usb_device *usb_dev,
68 struct usb_interface *intf, int altsetting, int claim)
69{
70 int ifnum = intf->altsetting->desc.bInterfaceNumber;
71 int ret;
72
73 if (claim && (ret = usb_driver_claim_interface(&xusbatm_usb_driver, intf, usbatm))) {
74 usb_err(usbatm, "%s: failed to claim interface %2d (%d)!\n", __func__, ifnum, ret);
75 return ret;
76 }
77 if ((ret = usb_set_interface(usb_dev, ifnum, altsetting))) {
78 usb_err(usbatm, "%s: altsetting %2d for interface %2d failed (%d)!\n", __func__, altsetting, ifnum, ret);
79 return ret;
Duncan Sands0bb3cf32005-05-11 20:17:09 +020080 }
81 return 0;
82}
83
Duncan Sands233c08e2006-01-13 09:48:36 +010084static void xusbatm_release_intf (struct usb_device *usb_dev, struct usb_interface *intf, int claimed)
85{
86 if (claimed) {
87 usb_set_intfdata(intf, NULL);
88 usb_driver_release_interface(&xusbatm_usb_driver, intf);
89 }
90}
91
Duncan Sands0ec3c7e2006-01-17 11:15:13 +010092static int xusbatm_bind(struct usbatm_data *usbatm,
Duncan Sands35644b02006-01-17 11:16:13 +010093 struct usb_interface *intf, const struct usb_device_id *id)
Duncan Sands0bb3cf32005-05-11 20:17:09 +020094{
95 struct usb_device *usb_dev = interface_to_usbdev(intf);
96 int drv_ix = id - xusbatm_usb_ids;
Duncan Sands233c08e2006-01-13 09:48:36 +010097 int rx_alt = rx_altsetting[drv_ix];
98 int tx_alt = tx_altsetting[drv_ix];
99 struct usb_interface *rx_intf = xusbatm_find_intf(usb_dev, rx_alt, rx_endpoint[drv_ix]);
100 struct usb_interface *tx_intf = xusbatm_find_intf(usb_dev, tx_alt, tx_endpoint[drv_ix]);
101 int ret;
Duncan Sands0bb3cf32005-05-11 20:17:09 +0200102
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100103 usb_dbg(usbatm, "%s: binding driver %d: vendor %04x product %04x"
Duncan Sands233c08e2006-01-13 09:48:36 +0100104 " rx: ep %02x padd %d alt %2d tx: ep %02x padd %d alt %2d\n",
Duncan Sands0bb3cf32005-05-11 20:17:09 +0200105 __func__, drv_ix, vendor[drv_ix], product[drv_ix],
Duncan Sands233c08e2006-01-13 09:48:36 +0100106 rx_endpoint[drv_ix], rx_padding[drv_ix], rx_alt,
107 tx_endpoint[drv_ix], tx_padding[drv_ix], tx_alt);
Duncan Sands0bb3cf32005-05-11 20:17:09 +0200108
Duncan Sands233c08e2006-01-13 09:48:36 +0100109 if (!rx_intf || !tx_intf) {
110 if (!rx_intf)
111 usb_dbg(usbatm, "%s: no interface contains endpoint %02x in altsetting %2d\n",
112 __func__, rx_endpoint[drv_ix], rx_alt);
113 if (!tx_intf)
114 usb_dbg(usbatm, "%s: no interface contains endpoint %02x in altsetting %2d\n",
115 __func__, tx_endpoint[drv_ix], tx_alt);
Duncan Sands0bb3cf32005-05-11 20:17:09 +0200116 return -ENODEV;
117 }
118
Duncan Sands233c08e2006-01-13 09:48:36 +0100119 if ((rx_intf != intf) && (tx_intf != intf))
120 return -ENODEV;
Duncan Sands0bb3cf32005-05-11 20:17:09 +0200121
Duncan Sands233c08e2006-01-13 09:48:36 +0100122 if ((rx_intf == tx_intf) && (rx_alt != tx_alt)) {
123 usb_err(usbatm, "%s: altsettings clash on interface %2d (%2d vs %2d)!\n", __func__,
124 rx_intf->altsetting->desc.bInterfaceNumber, rx_alt, tx_alt);
125 return -EINVAL;
Duncan Sands0bb3cf32005-05-11 20:17:09 +0200126 }
127
Duncan Sands233c08e2006-01-13 09:48:36 +0100128 usb_dbg(usbatm, "%s: rx If#=%2d; tx If#=%2d\n", __func__,
129 rx_intf->altsetting->desc.bInterfaceNumber,
130 tx_intf->altsetting->desc.bInterfaceNumber);
131
132 if ((ret = xusbatm_capture_intf(usbatm, usb_dev, rx_intf, rx_alt, rx_intf != intf)))
133 return ret;
134
135 if ((tx_intf != rx_intf) && (ret = xusbatm_capture_intf(usbatm, usb_dev, tx_intf, tx_alt, tx_intf != intf))) {
136 xusbatm_release_intf(usb_dev, rx_intf, rx_intf != intf);
137 return ret;
138 }
139
140 return 0;
Duncan Sands0bb3cf32005-05-11 20:17:09 +0200141}
142
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100143static void xusbatm_unbind(struct usbatm_data *usbatm,
Duncan Sands0bb3cf32005-05-11 20:17:09 +0200144 struct usb_interface *intf)
145{
146 struct usb_device *usb_dev = interface_to_usbdev(intf);
147 int i;
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100148
149 usb_dbg(usbatm, "%s entered\n", __func__);
Duncan Sands0bb3cf32005-05-11 20:17:09 +0200150
151 for(i = 0; i < usb_dev->actconfig->desc.bNumInterfaces; i++) {
Duncan Sands233c08e2006-01-13 09:48:36 +0100152 struct usb_interface *cur_intf = usb_dev->actconfig->interface[i];
153
154 if (cur_intf && (usb_get_intfdata(cur_intf) == usbatm)) {
155 usb_set_intfdata(cur_intf, NULL);
156 usb_driver_release_interface(&xusbatm_usb_driver, cur_intf);
157 }
Duncan Sands0bb3cf32005-05-11 20:17:09 +0200158 }
159}
160
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100161static int xusbatm_atm_start(struct usbatm_data *usbatm,
Duncan Sands0bb3cf32005-05-11 20:17:09 +0200162 struct atm_dev *atm_dev)
163{
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100164 atm_dbg(usbatm, "%s entered\n", __func__);
Duncan Sands0bb3cf32005-05-11 20:17:09 +0200165
166 /* use random MAC as we've no way to get it from the device */
167 random_ether_addr(atm_dev->esi);
168
169 return 0;
170}
171
172
173static int xusbatm_usb_probe(struct usb_interface *intf,
174 const struct usb_device_id *id)
175{
176 return usbatm_usb_probe(intf, id,
177 xusbatm_drivers + (id - xusbatm_usb_ids));
178}
179
180static struct usb_driver xusbatm_usb_driver = {
Duncan Sands0bb3cf32005-05-11 20:17:09 +0200181 .name = xusbatm_driver_name,
182 .probe = xusbatm_usb_probe,
183 .disconnect = usbatm_usb_disconnect,
184 .id_table = xusbatm_usb_ids
185};
186
187static int __init xusbatm_init(void)
188{
189 int i;
190
191 dbg("xusbatm_init");
192
193 if (!num_vendor ||
194 num_vendor != num_product ||
195 num_vendor != num_rx_endpoint ||
196 num_vendor != num_tx_endpoint) {
197 warn("malformed module parameters");
198 return -EINVAL;
199 }
200
201 for (i = 0; i < num_vendor; i++) {
Duncan Sands233c08e2006-01-13 09:48:36 +0100202 rx_endpoint[i] |= USB_DIR_IN;
203 tx_endpoint[i] &= USB_ENDPOINT_NUMBER_MASK;
204
Duncan Sands0bb3cf32005-05-11 20:17:09 +0200205 xusbatm_usb_ids[i].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
206 xusbatm_usb_ids[i].idVendor = vendor[i];
207 xusbatm_usb_ids[i].idProduct = product[i];
208
Duncan Sands0bb3cf32005-05-11 20:17:09 +0200209 xusbatm_drivers[i].driver_name = xusbatm_driver_name;
210 xusbatm_drivers[i].bind = xusbatm_bind;
211 xusbatm_drivers[i].unbind = xusbatm_unbind;
212 xusbatm_drivers[i].atm_start = xusbatm_atm_start;
213 xusbatm_drivers[i].in = rx_endpoint[i];
214 xusbatm_drivers[i].out = tx_endpoint[i];
215 xusbatm_drivers[i].rx_padding = rx_padding[i];
216 xusbatm_drivers[i].tx_padding = tx_padding[i];
217 }
218
219 return usb_register(&xusbatm_usb_driver);
220}
221module_init(xusbatm_init);
222
223static void __exit xusbatm_exit(void)
224{
225 dbg("xusbatm_exit entered");
226
227 usb_deregister(&xusbatm_usb_driver);
228}
229module_exit(xusbatm_exit);
230
231MODULE_AUTHOR("Roman Kagan, Duncan Sands");
232MODULE_DESCRIPTION("Driver for USB ADSL modems initialized in userspace");
233MODULE_LICENSE("GPL");
234MODULE_VERSION("0.1");