aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/usb/atm
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/usb/atm')
-rw-r--r--drivers/usb/atm/Kconfig50
-rw-r--r--drivers/usb/atm/Makefile7
-rw-r--r--drivers/usb/atm/cxacru.c878
-rw-r--r--drivers/usb/atm/speedtch.c1085
-rw-r--r--drivers/usb/atm/usb_atm.c1188
-rw-r--r--drivers/usb/atm/usb_atm.h176
-rw-r--r--drivers/usb/atm/usbatm.c1230
-rw-r--r--drivers/usb/atm/usbatm.h184
-rw-r--r--drivers/usb/atm/xusbatm.c196
9 files changed, 3064 insertions, 1930 deletions
diff --git a/drivers/usb/atm/Kconfig b/drivers/usb/atm/Kconfig
index 0d9f5379b8cf..f429862e0974 100644
--- a/drivers/usb/atm/Kconfig
+++ b/drivers/usb/atm/Kconfig
@@ -1,30 +1,60 @@
1# 1#
2# USB ATM driver configuration 2# USB/ATM DSL configuration
3# 3#
4comment "USB ATM/DSL drivers" 4
5menu "USB DSL modem support"
5 depends on USB 6 depends on USB
6 7
7config USB_ATM 8config USB_ATM
8 tristate "Generic USB ATM/DSL core I/O support" 9 tristate "USB DSL modem support"
9 depends on USB && ATM 10 depends on USB && ATM
10 select CRC32 11 select CRC32
11 default n 12 default n
12 help 13 help
13 This provides a library which is used for packet I/O by USB DSL 14 Say Y here if you want to connect a USB Digital Subscriber Line (DSL)
14 modems, such as the SpeedTouch driver below. 15 modem to your computer's USB port. You will then need to choose your
16 modem from the list below.
15 17
16 To compile this driver as a module, choose M here: the 18 To compile this driver as a module, choose M here: the
17 module will be called usb_atm. 19 module will be called usbatm.
18 20
19config USB_SPEEDTOUCH 21config USB_SPEEDTOUCH
20 tristate "Alcatel Speedtouch USB support" 22 tristate "Speedtouch USB support"
21 depends on USB && ATM 23 depends on USB_ATM
22 select USB_ATM 24 select FW_LOADER
23 help 25 help
24 Say Y here if you have an Alcatel SpeedTouch USB or SpeedTouch 330 26 Say Y here if you have an SpeedTouch USB or SpeedTouch 330
25 modem. In order to use your modem you will need to install the 27 modem. In order to use your modem you will need to install the
26 two parts of the firmware, extracted by the user space tools; see 28 two parts of the firmware, extracted by the user space tools; see
27 <http://www.linux-usb.org/SpeedTouch/> for details. 29 <http://www.linux-usb.org/SpeedTouch/> for details.
28 30
29 To compile this driver as a module, choose M here: the 31 To compile this driver as a module, choose M here: the
30 module will be called speedtch. 32 module will be called speedtch.
33
34config USB_CXACRU
35 tristate "Conexant AccessRunner USB support"
36 depends on USB_ATM
37 select FW_LOADER
38 help
39 Say Y here if you have an ADSL USB modem based on the Conexant
40 AccessRunner chipset. In order to use your modem you will need to
41 install the firmware, extracted by the user space tools; see
42 <http://accessrunner.sourceforge.net/> for details.
43
44 To compile this driver as a module, choose M here: the
45 module will be called cxacru.
46
47config USB_XUSBATM
48 tristate "Other USB DSL modem support"
49 depends on USB_ATM
50 help
51 Say Y here if you have a DSL USB modem not explicitly supported by
52 another USB DSL drivers. In order to use your modem you will need to
53 pass the vendor ID, product ID, and endpoint numbers for transmission
54 and reception as module parameters. You may need to initialize the
55 the modem using a user space utility (a firmware loader for example).
56
57 To compile this driver as a module, choose M here: the
58 module will be called xusbatm.
59
60endmenu
diff --git a/drivers/usb/atm/Makefile b/drivers/usb/atm/Makefile
index 9213b8b97587..751f297be2ef 100644
--- a/drivers/usb/atm/Makefile
+++ b/drivers/usb/atm/Makefile
@@ -1,7 +1,8 @@
1# 1#
2# Makefile for the rest of the USB drivers 2# Makefile for USB ATM/xDSL drivers
3# (the ones that don't fit into any other categories)
4# 3#
5 4
6obj-$(CONFIG_USB_ATM) += usb_atm.o 5obj-$(CONFIG_USB_CXACRU) += cxacru.o
7obj-$(CONFIG_USB_SPEEDTOUCH) += speedtch.o 6obj-$(CONFIG_USB_SPEEDTOUCH) += speedtch.o
7obj-$(CONFIG_USB_ATM) += usbatm.o
8obj-$(CONFIG_USB_XUSBATM) += xusbatm.o
diff --git a/drivers/usb/atm/cxacru.c b/drivers/usb/atm/cxacru.c
new file mode 100644
index 000000000000..cbd4a7d25d0b
--- /dev/null
+++ b/drivers/usb/atm/cxacru.c
@@ -0,0 +1,878 @@
1/******************************************************************************
2 * cxacru.c - driver for USB ADSL modems based on
3 * Conexant AccessRunner chipset
4 *
5 * Copyright (C) 2004 David Woodhouse, Duncan Sands, Roman Kagan
6 * Copyright (C) 2005 Duncan Sands, Roman Kagan (rkagan % mail ! ru)
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 2 of the License, or (at your option)
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * more details.
17 *
18 * You should have received a copy of the GNU General Public License along with
19 * this program; if not, write to the Free Software Foundation, Inc., 59
20 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 *
22 ******************************************************************************/
23
24/*
25 * Credit is due for Josep Comas, who created the original patch to speedtch.c
26 * to support the different padding used by the AccessRunner (now generalized
27 * into usbatm), and the userspace firmware loading utility.
28 */
29
30#include <linux/module.h>
31#include <linux/moduleparam.h>
32#include <linux/kernel.h>
33#include <linux/timer.h>
34#include <linux/errno.h>
35#include <linux/slab.h>
36#include <linux/init.h>
37#include <linux/device.h> /* FIXME: linux/firmware.h should include it itself */
38#include <linux/firmware.h>
39
40#include "usbatm.h"
41
42#define DRIVER_AUTHOR "Roman Kagan, David Woodhouse, Duncan Sands"
43#define DRIVER_VERSION "0.2"
44#define DRIVER_DESC "Conexant AccessRunner ADSL USB modem driver"
45
46static const char cxacru_driver_name[] = "cxacru";
47
48#define CXACRU_EP_CMD 0x01 /* Bulk/interrupt in/out */
49#define CXACRU_EP_DATA 0x02 /* Bulk in/out */
50
51#define CMD_PACKET_SIZE 64 /* Should be maxpacket(ep)? */
52
53/* Addresses */
54#define PLLFCLK_ADDR 0x00350068
55#define PLLBCLK_ADDR 0x0035006c
56#define SDRAMEN_ADDR 0x00350010
57#define FW_ADDR 0x00801000
58#define BR_ADDR 0x00180600
59#define SIG_ADDR 0x00180500
60#define BR_STACK_ADDR 0x00187f10
61
62/* Values */
63#define SDRAM_ENA 0x1
64
65#define CMD_TIMEOUT 2000 /* msecs */
66#define POLL_INTERVAL 5000 /* msecs */
67
68/* commands for interaction with the modem through the control channel before
69 * firmware is loaded */
70enum cxacru_fw_request {
71 FW_CMD_ERR,
72 FW_GET_VER,
73 FW_READ_MEM,
74 FW_WRITE_MEM,
75 FW_RMW_MEM,
76 FW_CHECKSUM_MEM,
77 FW_GOTO_MEM,
78};
79
80/* commands for interaction with the modem through the control channel once
81 * firmware is loaded */
82enum cxacru_cm_request {
83 CM_REQUEST_UNDEFINED = 0x80,
84 CM_REQUEST_TEST,
85 CM_REQUEST_CHIP_GET_MAC_ADDRESS,
86 CM_REQUEST_CHIP_GET_DP_VERSIONS,
87 CM_REQUEST_CHIP_ADSL_LINE_START,
88 CM_REQUEST_CHIP_ADSL_LINE_STOP,
89 CM_REQUEST_CHIP_ADSL_LINE_GET_STATUS,
90 CM_REQUEST_CHIP_ADSL_LINE_GET_SPEED,
91 CM_REQUEST_CARD_INFO_GET,
92 CM_REQUEST_CARD_DATA_GET,
93 CM_REQUEST_CARD_DATA_SET,
94 CM_REQUEST_COMMAND_HW_IO,
95 CM_REQUEST_INTERFACE_HW_IO,
96 CM_REQUEST_CARD_SERIAL_DATA_PATH_GET,
97 CM_REQUEST_CARD_SERIAL_DATA_PATH_SET,
98 CM_REQUEST_CARD_CONTROLLER_VERSION_GET,
99 CM_REQUEST_CARD_GET_STATUS,
100 CM_REQUEST_CARD_GET_MAC_ADDRESS,
101 CM_REQUEST_CARD_GET_DATA_LINK_STATUS,
102 CM_REQUEST_MAX,
103};
104
105/* reply codes to the commands above */
106enum cxacru_cm_status {
107 CM_STATUS_UNDEFINED,
108 CM_STATUS_SUCCESS,
109 CM_STATUS_ERROR,
110 CM_STATUS_UNSUPPORTED,
111 CM_STATUS_UNIMPLEMENTED,
112 CM_STATUS_PARAMETER_ERROR,
113 CM_STATUS_DBG_LOOPBACK,
114 CM_STATUS_MAX,
115};
116
117/* indices into CARD_INFO_GET return array */
118enum cxacru_info_idx {
119 CXINF_DOWNSTREAM_RATE,
120 CXINF_UPSTREAM_RATE,
121 CXINF_LINK_STATUS,
122 CXINF_LINE_STATUS,
123 CXINF_MAC_ADDRESS_HIGH,
124 CXINF_MAC_ADDRESS_LOW,
125 CXINF_UPSTREAM_SNR_MARGIN,
126 CXINF_DOWNSTREAM_SNR_MARGIN,
127 CXINF_UPSTREAM_ATTENUATION,
128 CXINF_DOWNSTREAM_ATTENUATION,
129 CXINF_TRANSMITTER_POWER,
130 CXINF_UPSTREAM_BITS_PER_FRAME,
131 CXINF_DOWNSTREAM_BITS_PER_FRAME,
132 CXINF_STARTUP_ATTEMPTS,
133 CXINF_UPSTREAM_CRC_ERRORS,
134 CXINF_DOWNSTREAM_CRC_ERRORS,
135 CXINF_UPSTREAM_FEC_ERRORS,
136 CXINF_DOWNSTREAM_FEC_ERRORS,
137 CXINF_UPSTREAM_HEC_ERRORS,
138 CXINF_DOWNSTREAM_HEC_ERRORS,
139 CXINF_LINE_STARTABLE,
140 CXINF_MODULATION,
141 CXINF_ADSL_HEADEND,
142 CXINF_ADSL_HEADEND_ENVIRONMENT,
143 CXINF_CONTROLLER_VERSION,
144 /* dunno what the missing two mean */
145 CXINF_MAX = 0x1c,
146};
147
148struct cxacru_modem_type {
149 u32 pll_f_clk;
150 u32 pll_b_clk;
151 int boot_rom_patch;
152};
153
154struct cxacru_data {
155 struct usbatm_data *usbatm;
156
157 const struct cxacru_modem_type *modem_type;
158
159 int line_status;
160 struct work_struct poll_work;
161
162 /* contol handles */
163 struct semaphore cm_serialize;
164 u8 *rcv_buf;
165 u8 *snd_buf;
166 struct urb *rcv_urb;
167 struct urb *snd_urb;
168 struct completion rcv_done;
169 struct completion snd_done;
170};
171
172/* the following three functions are stolen from drivers/usb/core/message.c */
173static void cxacru_blocking_completion(struct urb *urb, struct pt_regs *regs)
174{
175 complete((struct completion *)urb->context);
176}
177
178static void cxacru_timeout_kill(unsigned long data)
179{
180 usb_unlink_urb((struct urb *) data);
181}
182
183static int cxacru_start_wait_urb(struct urb *urb, struct completion *done,
184 int* actual_length)
185{
186 struct timer_list timer;
187 int status;
188
189 init_timer(&timer);
190 timer.expires = jiffies + msecs_to_jiffies(CMD_TIMEOUT);
191 timer.data = (unsigned long) urb;
192 timer.function = cxacru_timeout_kill;
193 add_timer(&timer);
194 wait_for_completion(done);
195 status = urb->status;
196 if (status == -ECONNRESET)
197 status = -ETIMEDOUT;
198 del_timer_sync(&timer);
199
200 if (actual_length)
201 *actual_length = urb->actual_length;
202 return status;
203}
204
205static int cxacru_cm(struct cxacru_data *instance, enum cxacru_cm_request cm,
206 u8 *wdata, int wsize, u8 *rdata, int rsize)
207{
208 int ret, actlen;
209 int offb, offd;
210 const int stride = CMD_PACKET_SIZE - 4;
211 u8 *wbuf = instance->snd_buf;
212 u8 *rbuf = instance->rcv_buf;
213 int wbuflen = ((wsize - 1) / stride + 1) * CMD_PACKET_SIZE;
214 int rbuflen = ((rsize - 1) / stride + 1) * CMD_PACKET_SIZE;
215
216 if (wbuflen > PAGE_SIZE || rbuflen > PAGE_SIZE) {
217 dbg("too big transfer requested");
218 ret = -ENOMEM;
219 goto fail;
220 }
221
222 down(&instance->cm_serialize);
223
224 /* submit reading urb before the writing one */
225 init_completion(&instance->rcv_done);
226 ret = usb_submit_urb(instance->rcv_urb, GFP_KERNEL);
227 if (ret < 0) {
228 dbg("submitting read urb for cm %#x failed", cm);
229 ret = ret;
230 goto fail;
231 }
232
233 memset(wbuf, 0, wbuflen);
234 /* handle wsize == 0 */
235 wbuf[0] = cm;
236 for (offb = offd = 0; offd < wsize; offd += stride, offb += CMD_PACKET_SIZE) {
237 wbuf[offb] = cm;
238 memcpy(wbuf + offb + 4, wdata + offd, min_t(int, stride, wsize - offd));
239 }
240
241 instance->snd_urb->transfer_buffer_length = wbuflen;
242 init_completion(&instance->snd_done);
243 ret = usb_submit_urb(instance->snd_urb, GFP_KERNEL);
244 if (ret < 0) {
245 dbg("submitting write urb for cm %#x failed", cm);
246 ret = ret;
247 goto fail;
248 }
249
250 ret = cxacru_start_wait_urb(instance->snd_urb, &instance->snd_done, NULL);
251 if (ret < 0) {
252 dbg("sending cm %#x failed", cm);
253 ret = ret;
254 goto fail;
255 }
256
257 ret = cxacru_start_wait_urb(instance->rcv_urb, &instance->rcv_done, &actlen);
258 if (ret < 0) {
259 dbg("receiving cm %#x failed", cm);
260 ret = ret;
261 goto fail;
262 }
263 if (actlen % CMD_PACKET_SIZE || !actlen) {
264 dbg("response is not a positive multiple of %d: %#x",
265 CMD_PACKET_SIZE, actlen);
266 ret = -EIO;
267 goto fail;
268 }
269
270 /* check the return status and copy the data to the output buffer, if needed */
271 for (offb = offd = 0; offd < rsize && offb < actlen; offb += CMD_PACKET_SIZE) {
272 if (rbuf[offb] != cm) {
273 dbg("wrong cm %#x in response", rbuf[offb]);
274 ret = -EIO;
275 goto fail;
276 }
277 if (rbuf[offb + 1] != CM_STATUS_SUCCESS) {
278 dbg("response failed: %#x", rbuf[offb + 1]);
279 ret = -EIO;
280 goto fail;
281 }
282 if (offd >= rsize)
283 break;
284 memcpy(rdata + offd, rbuf + offb + 4, min_t(int, stride, rsize - offd));
285 offd += stride;
286 }
287
288 ret = offd;
289 dbg("cm %#x", cm);
290fail:
291 up(&instance->cm_serialize);
292 return ret;
293}
294
295static int cxacru_cm_get_array(struct cxacru_data *instance, enum cxacru_cm_request cm,
296 u32 *data, int size)
297{
298 int ret, len;
299 u32 *buf;
300 int offb, offd;
301 const int stride = CMD_PACKET_SIZE / (4 * 2) - 1;
302 int buflen = ((size - 1) / stride + 1 + size * 2) * 4;
303
304 buf = kmalloc(buflen, GFP_KERNEL);
305 if (!buf)
306 return -ENOMEM;
307
308 ret = cxacru_cm(instance, cm, NULL, 0, (u8 *) buf, buflen);
309 if (ret < 0)
310 goto cleanup;
311
312 /* len > 0 && len % 4 == 0 guaranteed by cxacru_cm() */
313 len = ret / 4;
314 for (offb = 0; offb < len; ) {
315 int l = le32_to_cpu(buf[offb++]);
316 if (l > stride || l > (len - offb) / 2) {
317 dbg("wrong data length %#x in response", l);
318 ret = -EIO;
319 goto cleanup;
320 }
321 while (l--) {
322 offd = le32_to_cpu(buf[offb++]);
323 if (offd >= size) {
324 dbg("wrong index %#x in response", offd);
325 ret = -EIO;
326 goto cleanup;
327 }
328 data[offd] = le32_to_cpu(buf[offb++]);
329 }
330 }
331
332 ret = 0;
333
334cleanup:
335 kfree(buf);
336 return ret;
337}
338
339static int cxacru_card_status(struct cxacru_data *instance)
340{
341 int ret = cxacru_cm(instance, CM_REQUEST_CARD_GET_STATUS, NULL, 0, NULL, 0);
342 if (ret < 0) { /* firmware not loaded */
343 dbg("cxacru_adsl_start: CARD_GET_STATUS returned %d", ret);
344 return ret;
345 }
346 return 0;
347}
348
349static void cxacru_poll_status(struct cxacru_data *instance);
350
351static int cxacru_atm_start(struct usbatm_data *usbatm_instance,
352 struct atm_dev *atm_dev)
353{
354 struct cxacru_data *instance = usbatm_instance->driver_data;
355 struct device *dev = &usbatm_instance->usb_intf->dev;
356 /*
357 struct atm_dev *atm_dev = usbatm_instance->atm_dev;
358 */
359 int ret;
360
361 dbg("cxacru_atm_start");
362
363 /* Read MAC address */
364 ret = cxacru_cm(instance, CM_REQUEST_CARD_GET_MAC_ADDRESS, NULL, 0,
365 atm_dev->esi, sizeof(atm_dev->esi));
366 if (ret < 0) {
367 dev_err(dev, "cxacru_atm_start: CARD_GET_MAC_ADDRESS returned %d\n", ret);
368 return ret;
369 }
370
371 /* start ADSL */
372 ret = cxacru_cm(instance, CM_REQUEST_CHIP_ADSL_LINE_START, NULL, 0, NULL, 0);
373 if (ret < 0) {
374 dev_err(dev, "cxacru_atm_start: CHIP_ADSL_LINE_START returned %d\n", ret);
375 return ret;
376 }
377
378 /* Start status polling */
379 cxacru_poll_status(instance);
380 return 0;
381}
382
383static void cxacru_poll_status(struct cxacru_data *instance)
384{
385 u32 buf[CXINF_MAX] = {};
386 struct device *dev = &instance->usbatm->usb_intf->dev;
387 struct atm_dev *atm_dev = instance->usbatm->atm_dev;
388 int ret;
389
390 ret = cxacru_cm_get_array(instance, CM_REQUEST_CARD_INFO_GET, buf, CXINF_MAX);
391 if (ret < 0) {
392 dev_warn(dev, "poll status: error %d\n", ret);
393 goto reschedule;
394 }
395
396 if (instance->line_status == buf[CXINF_LINE_STATUS])
397 goto reschedule;
398
399 instance->line_status = buf[CXINF_LINE_STATUS];
400 switch (instance->line_status) {
401 case 0:
402 atm_dev->signal = ATM_PHY_SIG_LOST;
403 dev_info(dev, "ADSL line: down\n");
404 break;
405
406 case 1:
407 atm_dev->signal = ATM_PHY_SIG_LOST;
408 dev_info(dev, "ADSL line: attemtping to activate\n");
409 break;
410
411 case 2:
412 atm_dev->signal = ATM_PHY_SIG_LOST;
413 dev_info(dev, "ADSL line: training\n");
414 break;
415
416 case 3:
417 atm_dev->signal = ATM_PHY_SIG_LOST;
418 dev_info(dev, "ADSL line: channel analysis\n");
419 break;
420
421 case 4:
422 atm_dev->signal = ATM_PHY_SIG_LOST;
423 dev_info(dev, "ADSL line: exchange\n");
424 break;
425
426 case 5:
427 atm_dev->link_rate = buf[CXINF_DOWNSTREAM_RATE] * 1000 / 424;
428 atm_dev->signal = ATM_PHY_SIG_FOUND;
429
430 dev_info(dev, "ADSL line: up (%d Kib/s down | %d Kib/s up)\n",
431 buf[CXINF_DOWNSTREAM_RATE], buf[CXINF_UPSTREAM_RATE]);
432 break;
433
434 case 6:
435 atm_dev->signal = ATM_PHY_SIG_LOST;
436 dev_info(dev, "ADSL line: waiting\n");
437 break;
438
439 case 7:
440 atm_dev->signal = ATM_PHY_SIG_LOST;
441 dev_info(dev, "ADSL line: initializing\n");
442 break;
443
444 default:
445 atm_dev->signal = ATM_PHY_SIG_UNKNOWN;
446 dev_info(dev, "Unknown line state %02x\n", instance->line_status);
447 break;
448 }
449reschedule:
450 schedule_delayed_work(&instance->poll_work, msecs_to_jiffies(POLL_INTERVAL));
451}
452
453static int cxacru_fw(struct usb_device *usb_dev, enum cxacru_fw_request fw,
454 u8 code1, u8 code2, u32 addr, u8 *data, int size)
455{
456 int ret;
457 u8 *buf;
458 int offd, offb;
459 const int stride = CMD_PACKET_SIZE - 8;
460
461 buf = (u8 *) __get_free_page(GFP_KERNEL);
462 if (!buf)
463 return -ENOMEM;
464
465 offb = offd = 0;
466 do {
467 int l = min_t(int, stride, size - offd);
468 buf[offb++] = fw;
469 buf[offb++] = l;
470 buf[offb++] = code1;
471 buf[offb++] = code2;
472 *((u32 *) (buf + offb)) = cpu_to_le32(addr);
473 offb += 4;
474 addr += l;
475 if(l)
476 memcpy(buf + offb, data + offd, l);
477 if (l < stride)
478 memset(buf + offb + l, 0, stride - l);
479 offb += stride;
480 offd += stride;
481 if ((offb >= PAGE_SIZE) || (offd >= size)) {
482 ret = usb_bulk_msg(usb_dev, usb_sndbulkpipe(usb_dev, CXACRU_EP_CMD),
483 buf, offb, NULL, CMD_TIMEOUT);
484 if (ret < 0) {
485 dbg("sending fw %#x failed", fw);
486 goto cleanup;
487 }
488 offb = 0;
489 }
490 } while(offd < size);
491 dbg("sent fw %#x", fw);
492
493 ret = 0;
494
495cleanup:
496 free_page((unsigned long) buf);
497 return ret;
498}
499
500static void cxacru_upload_firmware(struct cxacru_data *instance,
501 const struct firmware *fw,
502 const struct firmware *bp,
503 const struct firmware *cf)
504{
505 int ret;
506 int off;
507 struct usb_device *usb_dev = instance->usbatm->usb_dev;
508 struct device *dev = &instance->usbatm->usb_intf->dev;
509 u16 signature[] = { usb_dev->descriptor.idVendor, usb_dev->descriptor.idProduct };
510 u32 val;
511
512 dbg("cxacru_upload_firmware");
513
514 /* FirmwarePllFClkValue */
515 val = cpu_to_le32(instance->modem_type->pll_f_clk);
516 ret = cxacru_fw(usb_dev, FW_WRITE_MEM, 0x2, 0x0, PLLFCLK_ADDR, (u8 *) &val, 4);
517 if (ret) {
518 dev_err(dev, "FirmwarePllFClkValue failed: %d\n", ret);
519 return;
520 }
521
522 /* FirmwarePllBClkValue */
523 val = cpu_to_le32(instance->modem_type->pll_b_clk);
524 ret = cxacru_fw(usb_dev, FW_WRITE_MEM, 0x2, 0x0, PLLBCLK_ADDR, (u8 *) &val, 4);
525 if (ret) {
526 dev_err(dev, "FirmwarePllBClkValue failed: %d\n", ret);
527 return;
528 }
529
530 /* Enable SDRAM */
531 val = cpu_to_le32(SDRAM_ENA);
532 ret = cxacru_fw(usb_dev, FW_WRITE_MEM, 0x2, 0x0, SDRAMEN_ADDR, (u8 *) &val, 4);
533 if (ret) {
534 dev_err(dev, "Enable SDRAM failed: %d\n", ret);
535 return;
536 }
537
538 /* Firmware */
539 ret = cxacru_fw(usb_dev, FW_WRITE_MEM, 0x2, 0x0, FW_ADDR, fw->data, fw->size);
540 if (ret) {
541 dev_err(dev, "Firmware upload failed: %d\n", ret);
542 return;
543 }
544
545 /* Boot ROM patch */
546 if (instance->modem_type->boot_rom_patch) {
547 ret = cxacru_fw(usb_dev, FW_WRITE_MEM, 0x2, 0x0, BR_ADDR, bp->data, bp->size);
548 if (ret) {
549 dev_err(dev, "Boot ROM patching failed: %d\n", ret);
550 return;
551 }
552 }
553
554 /* Signature */
555 ret = cxacru_fw(usb_dev, FW_WRITE_MEM, 0x2, 0x0, SIG_ADDR, (u8 *) signature, 4);
556 if (ret) {
557 dev_err(dev, "Signature storing failed: %d\n", ret);
558 return;
559 }
560
561 if (instance->modem_type->boot_rom_patch) {
562 val = cpu_to_le32(BR_ADDR);
563 ret = cxacru_fw(usb_dev, FW_WRITE_MEM, 0x2, 0x0, BR_STACK_ADDR, (u8 *) &val, 4);
564 }
565 else {
566 ret = cxacru_fw(usb_dev, FW_GOTO_MEM, 0x0, 0x0, FW_ADDR, NULL, 0);
567 }
568 if (ret) {
569 dev_err(dev, "Passing control to firmware failed: %d\n", ret);
570 return;
571 }
572
573 /* Delay to allow firmware to start up. */
574 msleep_interruptible(1000);
575
576 usb_clear_halt(usb_dev, usb_sndbulkpipe(usb_dev, CXACRU_EP_CMD));
577 usb_clear_halt(usb_dev, usb_rcvbulkpipe(usb_dev, CXACRU_EP_CMD));
578 usb_clear_halt(usb_dev, usb_sndbulkpipe(usb_dev, CXACRU_EP_DATA));
579 usb_clear_halt(usb_dev, usb_rcvbulkpipe(usb_dev, CXACRU_EP_DATA));
580
581 ret = cxacru_cm(instance, CM_REQUEST_CARD_GET_STATUS, NULL, 0, NULL, 0);
582 if (ret < 0) {
583 dev_err(dev, "modem failed to initialize: %d\n", ret);
584 return;
585 }
586
587 /* Load config data (le32), doing one packet at a time */
588 if (cf)
589 for (off = 0; off < cf->size / 4; ) {
590 u32 buf[CMD_PACKET_SIZE / 4 - 1];
591 int i, len = min_t(int, cf->size / 4 - off, CMD_PACKET_SIZE / 4 / 2 - 1);
592 buf[0] = cpu_to_le32(len);
593 for (i = 0; i < len; i++, off++) {
594 buf[i * 2 + 1] = cpu_to_le32(off);
595 memcpy(buf + i * 2 + 2, cf->data + off * 4, 4);
596 }
597 ret = cxacru_cm(instance, CM_REQUEST_CARD_DATA_SET,
598 (u8 *) buf, len, NULL, 0);
599 if (ret < 0) {
600 dev_err(dev, "load config data failed: %d\n", ret);
601 return;
602 }
603 }
604
605 msleep_interruptible(4000);
606}
607
608static int cxacru_find_firmware(struct cxacru_data *instance,
609 char* phase, const struct firmware **fw_p)
610{
611 struct device *dev = &instance->usbatm->usb_intf->dev;
612 char buf[16];
613
614 sprintf(buf, "cxacru-%s.bin", phase);
615 dbg("cxacru_find_firmware: looking for %s", buf);
616
617 if (request_firmware(fw_p, buf, dev)) {
618 dev_dbg(dev, "no stage %s firmware found\n", phase);
619 return -ENOENT;
620 }
621
622 dev_info(dev, "found firmware %s\n", buf);
623
624 return 0;
625}
626
627static int cxacru_heavy_init(struct usbatm_data *usbatm_instance,
628 struct usb_interface *usb_intf)
629{
630 struct device *dev = &usbatm_instance->usb_intf->dev;
631 const struct firmware *fw, *bp, *cf;
632 struct cxacru_data *instance = usbatm_instance->driver_data;
633
634 int ret = cxacru_find_firmware(instance, "fw", &fw);
635 if (ret) {
636 dev_warn(dev, "firmware (cxacru-fw.bin) unavailable (hotplug misconfiguration?)\n");
637 return ret;
638 }
639
640 if (instance->modem_type->boot_rom_patch) {
641 ret = cxacru_find_firmware(instance, "bp", &bp);
642 if (ret) {
643 dev_warn(dev, "boot ROM patch (cxacru-bp.bin) unavailable (hotplug misconfiguration?)\n");
644 release_firmware(fw);
645 return ret;
646 }
647 }
648
649 if (cxacru_find_firmware(instance, "cf", &cf)) /* optional */
650 cf = NULL;
651
652 cxacru_upload_firmware(instance, fw, bp, cf);
653
654 if (cf)
655 release_firmware(cf);
656 if (instance->modem_type->boot_rom_patch)
657 release_firmware(bp);
658 release_firmware(fw);
659
660 ret = cxacru_card_status(instance);
661 if (ret)
662 dbg("modem initialisation failed");
663 else
664 dbg("done setting up the modem");
665
666 return ret;
667}
668
669static int cxacru_bind(struct usbatm_data *usbatm_instance,
670 struct usb_interface *intf, const struct usb_device_id *id,
671 int *need_heavy_init)
672{
673 struct cxacru_data *instance;
674 struct usb_device *usb_dev = interface_to_usbdev(intf);
675 int ret;
676
677 /* instance init */
678 instance = kmalloc(sizeof(*instance), GFP_KERNEL);
679 if (!instance) {
680 dbg("cxacru_bind: no memory for instance data");
681 return -ENOMEM;
682 }
683
684 memset(instance, 0, sizeof(*instance));
685
686 instance->usbatm = usbatm_instance;
687 instance->modem_type = (struct cxacru_modem_type *) id->driver_info;
688
689 instance->rcv_buf = (u8 *) __get_free_page(GFP_KERNEL);
690 if (!instance->rcv_buf) {
691 dbg("cxacru_bind: no memory for rcv_buf");
692 ret = -ENOMEM;
693 goto fail;
694 }
695 instance->snd_buf = (u8 *) __get_free_page(GFP_KERNEL);
696 if (!instance->snd_buf) {
697 dbg("cxacru_bind: no memory for snd_buf");
698 ret = -ENOMEM;
699 goto fail;
700 }
701 instance->rcv_urb = usb_alloc_urb(0, GFP_KERNEL);
702 if (!instance->rcv_urb) {
703 dbg("cxacru_bind: no memory for rcv_urb");
704 ret = -ENOMEM;
705 goto fail;
706 }
707 instance->snd_urb = usb_alloc_urb(0, GFP_KERNEL);
708 if (!instance->snd_urb) {
709 dbg("cxacru_bind: no memory for snd_urb");
710 ret = -ENOMEM;
711 goto fail;
712 }
713
714 usb_fill_int_urb(instance->rcv_urb,
715 usb_dev, usb_rcvintpipe(usb_dev, CXACRU_EP_CMD),
716 instance->rcv_buf, PAGE_SIZE,
717 cxacru_blocking_completion, &instance->rcv_done, 1);
718 instance->rcv_urb->transfer_flags |= URB_ASYNC_UNLINK;
719
720 usb_fill_int_urb(instance->snd_urb,
721 usb_dev, usb_sndintpipe(usb_dev, CXACRU_EP_CMD),
722 instance->snd_buf, PAGE_SIZE,
723 cxacru_blocking_completion, &instance->snd_done, 4);
724 instance->snd_urb->transfer_flags |= URB_ASYNC_UNLINK;
725
726 init_MUTEX(&instance->cm_serialize);
727
728 INIT_WORK(&instance->poll_work, (void *)cxacru_poll_status, instance);
729
730 usbatm_instance->driver_data = instance;
731
732 *need_heavy_init = cxacru_card_status(instance);
733
734 return 0;
735
736 fail:
737 free_page((unsigned long) instance->snd_buf);
738 free_page((unsigned long) instance->rcv_buf);
739 usb_free_urb(instance->snd_urb);
740 usb_free_urb(instance->rcv_urb);
741 kfree(instance);
742
743 return ret;
744}
745
746static void cxacru_unbind(struct usbatm_data *usbatm_instance,
747 struct usb_interface *intf)
748{
749 struct cxacru_data *instance = usbatm_instance->driver_data;
750
751 dbg("cxacru_unbind entered");
752
753 if (!instance) {
754 dbg("cxacru_unbind: NULL instance!");
755 return;
756 }
757
758 while (!cancel_delayed_work(&instance->poll_work))
759 flush_scheduled_work();
760
761 usb_kill_urb(instance->snd_urb);
762 usb_kill_urb(instance->rcv_urb);
763 usb_free_urb(instance->snd_urb);
764 usb_free_urb(instance->rcv_urb);
765
766 free_page((unsigned long) instance->snd_buf);
767 free_page((unsigned long) instance->rcv_buf);
768 kfree(instance);
769
770 usbatm_instance->driver_data = NULL;
771}
772
773static const struct cxacru_modem_type cxacru_cafe = {
774 .pll_f_clk = 0x02d874df,
775 .pll_b_clk = 0x0196a51a,
776 .boot_rom_patch = 1,
777};
778
779static const struct cxacru_modem_type cxacru_cb00 = {
780 .pll_f_clk = 0x5,
781 .pll_b_clk = 0x3,
782 .boot_rom_patch = 0,
783};
784
785static const struct usb_device_id cxacru_usb_ids[] = {
786 { /* V = Conexant P = ADSL modem (Euphrates project) */
787 USB_DEVICE(0x0572, 0xcafe), .driver_info = (unsigned long) &cxacru_cafe
788 },
789 { /* V = Conexant P = ADSL modem (Hasbani project) */
790 USB_DEVICE(0x0572, 0xcb00), .driver_info = (unsigned long) &cxacru_cb00
791 },
792 { /* V = Conexant P = ADSL modem */
793 USB_DEVICE(0x0572, 0xcb01), .driver_info = (unsigned long) &cxacru_cb00
794 },
795 { /* V = Conexant P = ADSL modem */
796 USB_DEVICE(0x0572, 0xcb06), .driver_info = (unsigned long) &cxacru_cb00
797 },
798 { /* V = Olitec P = ADSL modem version 2 */
799 USB_DEVICE(0x08e3, 0x0100), .driver_info = (unsigned long) &cxacru_cafe
800 },
801 { /* V = Olitec P = ADSL modem version 3 */
802 USB_DEVICE(0x08e3, 0x0102), .driver_info = (unsigned long) &cxacru_cb00
803 },
804 { /* V = Trust/Amigo Technology Co. P = AMX-CA86U */
805 USB_DEVICE(0x0eb0, 0x3457), .driver_info = (unsigned long) &cxacru_cafe
806 },
807 { /* V = Zoom P = 5510 */
808 USB_DEVICE(0x1803, 0x5510), .driver_info = (unsigned long) &cxacru_cb00
809 },
810 { /* V = Draytek P = Vigor 318 */
811 USB_DEVICE(0x0675, 0x0200), .driver_info = (unsigned long) &cxacru_cb00
812 },
813 { /* V = Zyxel P = 630-C1 aka OMNI ADSL USB (Annex A) */
814 USB_DEVICE(0x0586, 0x330a), .driver_info = (unsigned long) &cxacru_cb00
815 },
816 { /* V = Zyxel P = 630-C3 aka OMNI ADSL USB (Annex B) */
817 USB_DEVICE(0x0586, 0x330b), .driver_info = (unsigned long) &cxacru_cb00
818 },
819 { /* V = Aethra P = Starmodem UM1020 */
820 USB_DEVICE(0x0659, 0x0020), .driver_info = (unsigned long) &cxacru_cb00
821 },
822 { /* V = Aztech Systems P = ? AKA Pirelli AUA-010 */
823 USB_DEVICE(0x0509, 0x0812), .driver_info = (unsigned long) &cxacru_cb00
824 },
825 { /* V = Netopia P = Cayman 3341(Annex A)/3351(Annex B) */
826 USB_DEVICE(0x100d, 0xcb01), .driver_info = (unsigned long) &cxacru_cb00
827 },
828 { /* V = Netopia P = Cayman 3342(Annex A)/3352(Annex B) */
829 USB_DEVICE(0x100d, 0x3342), .driver_info = (unsigned long) &cxacru_cb00
830 },
831 {}
832};
833
834MODULE_DEVICE_TABLE(usb, cxacru_usb_ids);
835
836static struct usbatm_driver cxacru_driver = {
837 .owner = THIS_MODULE,
838 .driver_name = cxacru_driver_name,
839 .bind = cxacru_bind,
840 .heavy_init = cxacru_heavy_init,
841 .unbind = cxacru_unbind,
842 .atm_start = cxacru_atm_start,
843 .in = CXACRU_EP_DATA,
844 .out = CXACRU_EP_DATA,
845 .rx_padding = 3,
846 .tx_padding = 11,
847};
848
849static int cxacru_usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
850{
851 return usbatm_usb_probe(intf, id, &cxacru_driver);
852}
853
854static struct usb_driver cxacru_usb_driver = {
855 .owner = THIS_MODULE,
856 .name = cxacru_driver_name,
857 .probe = cxacru_usb_probe,
858 .disconnect = usbatm_usb_disconnect,
859 .id_table = cxacru_usb_ids
860};
861
862static int __init cxacru_init(void)
863{
864 return usb_register(&cxacru_usb_driver);
865}
866
867static void __exit cxacru_cleanup(void)
868{
869 usb_deregister(&cxacru_usb_driver);
870}
871
872module_init(cxacru_init);
873module_exit(cxacru_cleanup);
874
875MODULE_AUTHOR(DRIVER_AUTHOR);
876MODULE_DESCRIPTION(DRIVER_DESC);
877MODULE_LICENSE("GPL");
878MODULE_VERSION(DRIVER_VERSION);
diff --git a/drivers/usb/atm/speedtch.c b/drivers/usb/atm/speedtch.c
index 2a1697bfd695..6a6eaa2a3b1c 100644
--- a/drivers/usb/atm/speedtch.c
+++ b/drivers/usb/atm/speedtch.c
@@ -5,6 +5,8 @@
5 * Copyright (C) 2003, Duncan Sands 5 * Copyright (C) 2003, Duncan Sands
6 * Copyright (C) 2004, David Woodhouse 6 * Copyright (C) 2004, David Woodhouse
7 * 7 *
8 * Based on "modem_run.c", copyright (C) 2001, Benoit Papillault
9 *
8 * This program is free software; you can redistribute it and/or modify it 10 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the Free 11 * under the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 2 of the License, or (at your option) 12 * Software Foundation; either version 2 of the License, or (at your option)
@@ -21,821 +23,798 @@
21 * 23 *
22 ******************************************************************************/ 24 ******************************************************************************/
23 25
24#include <linux/module.h> 26#include <asm/page.h>
25#include <linux/moduleparam.h> 27#include <linux/device.h>
28#include <linux/errno.h>
29#include <linux/firmware.h>
26#include <linux/gfp.h> 30#include <linux/gfp.h>
31#include <linux/init.h>
27#include <linux/kernel.h> 32#include <linux/kernel.h>
28#include <linux/sched.h> 33#include <linux/module.h>
29#include <linux/timer.h> 34#include <linux/moduleparam.h>
30#include <linux/errno.h>
31#include <linux/proc_fs.h>
32#include <linux/slab.h> 35#include <linux/slab.h>
33#include <linux/wait.h> 36#include <linux/stat.h>
34#include <linux/list.h> 37#include <linux/timer.h>
35#include <asm/processor.h> 38#include <linux/workqueue.h>
36#include <asm/uaccess.h>
37#include <linux/smp_lock.h>
38#include <linux/interrupt.h>
39#include <linux/atm.h>
40#include <linux/atmdev.h>
41#include <linux/crc32.h>
42#include <linux/init.h>
43#include <linux/firmware.h>
44
45#include "usb_atm.h"
46 39
47#if defined(CONFIG_FW_LOADER) || defined(CONFIG_FW_LOADER_MODULE) 40#include "usbatm.h"
48# define USE_FW_LOADER
49#endif
50 41
51#define DRIVER_AUTHOR "Johan Verrept, Duncan Sands <duncan.sands@free.fr>" 42#define DRIVER_AUTHOR "Johan Verrept, Duncan Sands <duncan.sands@free.fr>"
52#define DRIVER_VERSION "1.8" 43#define DRIVER_VERSION "1.9"
53#define DRIVER_DESC "Alcatel SpeedTouch USB driver version " DRIVER_VERSION 44#define DRIVER_DESC "Alcatel SpeedTouch USB driver version " DRIVER_VERSION
54 45
55static const char speedtch_driver_name[] = "speedtch"; 46static const char speedtch_driver_name[] = "speedtch";
56 47
57#define SPEEDTOUCH_VENDORID 0x06b9 48#define CTRL_TIMEOUT 2000 /* milliseconds */
58#define SPEEDTOUCH_PRODUCTID 0x4061 49#define DATA_TIMEOUT 2000 /* milliseconds */
59 50
60/* Timeout in jiffies */ 51#define OFFSET_7 0 /* size 1 */
61#define CTRL_TIMEOUT 2000 52#define OFFSET_b 1 /* size 8 */
62#define DATA_TIMEOUT 2000 53#define OFFSET_d 9 /* size 4 */
54#define OFFSET_e 13 /* size 1 */
55#define OFFSET_f 14 /* size 1 */
56#define TOTAL 15
63 57
64#define OFFSET_7 0 /* size 1 */ 58#define SIZE_7 1
65#define OFFSET_b 1 /* size 8 */ 59#define SIZE_b 8
66#define OFFSET_d 9 /* size 4 */ 60#define SIZE_d 4
67#define OFFSET_e 13 /* size 1 */ 61#define SIZE_e 1
68#define OFFSET_f 14 /* size 1 */ 62#define SIZE_f 1
69#define TOTAL 15
70 63
71#define SIZE_7 1 64#define MIN_POLL_DELAY 5000 /* milliseconds */
72#define SIZE_b 8 65#define MAX_POLL_DELAY 60000 /* milliseconds */
73#define SIZE_d 4
74#define SIZE_e 1
75#define SIZE_f 1
76 66
77static int dl_512_first = 0; 67#define RESUBMIT_DELAY 1000 /* milliseconds */
78static int sw_buffering = 0;
79 68
80module_param(dl_512_first, bool, 0444); 69#define DEFAULT_ALTSETTING 1
81MODULE_PARM_DESC(dl_512_first, "Read 512 bytes before sending firmware"); 70#define DEFAULT_DL_512_FIRST 0
71#define DEFAULT_SW_BUFFERING 0
82 72
83module_param(sw_buffering, uint, 0444); 73static int altsetting = DEFAULT_ALTSETTING;
84MODULE_PARM_DESC(sw_buffering, "Enable software buffering"); 74static int dl_512_first = DEFAULT_DL_512_FIRST;
75static int sw_buffering = DEFAULT_SW_BUFFERING;
85 76
86#define UDSL_IOCTL_LINE_UP 1 77module_param(altsetting, int, S_IRUGO | S_IWUSR);
87#define UDSL_IOCTL_LINE_DOWN 2 78MODULE_PARM_DESC(altsetting,
79 "Alternative setting for data interface (default: "
80 __MODULE_STRING(DEFAULT_ALTSETTING) ")");
88 81
89#define SPEEDTCH_ENDPOINT_INT 0x81 82module_param(dl_512_first, bool, S_IRUGO | S_IWUSR);
90#define SPEEDTCH_ENDPOINT_DATA 0x07 83MODULE_PARM_DESC(dl_512_first,
91#define SPEEDTCH_ENDPOINT_FIRMWARE 0x05 84 "Read 512 bytes before sending firmware (default: "
85 __MODULE_STRING(DEFAULT_DL_512_FIRST) ")");
92 86
93#define hex2int(c) ( (c >= '0') && (c <= '9') ? (c - '0') : ((c & 0xf) + 9) ) 87module_param(sw_buffering, bool, S_IRUGO | S_IWUSR);
88MODULE_PARM_DESC(sw_buffering,
89 "Enable software buffering (default: "
90 __MODULE_STRING(DEFAULT_SW_BUFFERING) ")");
94 91
95static struct usb_device_id speedtch_usb_ids[] = { 92#define ENDPOINT_INT 0x81
96 {USB_DEVICE(SPEEDTOUCH_VENDORID, SPEEDTOUCH_PRODUCTID)}, 93#define ENDPOINT_DATA 0x07
97 {} 94#define ENDPOINT_FIRMWARE 0x05
98};
99 95
100MODULE_DEVICE_TABLE(usb, speedtch_usb_ids); 96#define hex2int(c) ( (c >= '0') && (c <= '9') ? (c - '0') : ((c & 0xf) + 9) )
101 97
102struct speedtch_instance_data { 98struct speedtch_instance_data {
103 struct udsl_instance_data u; 99 struct usbatm_data *usbatm;
100
101 struct work_struct status_checker;
104 102
105 /* Status */ 103 int poll_delay; /* milliseconds */
104
105 struct timer_list resubmit_timer;
106 struct urb *int_urb; 106 struct urb *int_urb;
107 unsigned char int_data[16]; 107 unsigned char int_data[16];
108 struct work_struct poll_work;
109 struct timer_list poll_timer;
110};
111/* USB */
112
113static int speedtch_usb_probe(struct usb_interface *intf,
114 const struct usb_device_id *id);
115static void speedtch_usb_disconnect(struct usb_interface *intf);
116static int speedtch_usb_ioctl(struct usb_interface *intf, unsigned int code,
117 void *user_data);
118static void speedtch_handle_int(struct urb *urb, struct pt_regs *regs);
119static void speedtch_poll_status(struct speedtch_instance_data *instance);
120 108
121static struct usb_driver speedtch_usb_driver = { 109 unsigned char scratch_buffer[TOTAL];
122 .owner = THIS_MODULE,
123 .name = speedtch_driver_name,
124 .probe = speedtch_usb_probe,
125 .disconnect = speedtch_usb_disconnect,
126 .ioctl = speedtch_usb_ioctl,
127 .id_table = speedtch_usb_ids,
128}; 110};
129 111
130/*************** 112/***************
131** firmware ** 113** firmware **
132***************/ 114***************/
133 115
134static void speedtch_got_firmware(struct speedtch_instance_data *instance, 116static void speedtch_set_swbuff(struct speedtch_instance_data *instance, int state)
135 int got_it)
136{ 117{
137 int err; 118 struct usbatm_data *usbatm = instance->usbatm;
138 struct usb_interface *intf; 119 struct usb_device *usb_dev = usbatm->usb_dev;
139
140 down(&instance->u.serialize); /* vs self, speedtch_firmware_start */
141 if (instance->u.status == UDSL_LOADED_FIRMWARE)
142 goto out;
143 if (!got_it) {
144 instance->u.status = UDSL_NO_FIRMWARE;
145 goto out;
146 }
147 if ((err = usb_set_interface(instance->u.usb_dev, 1, 1)) < 0) {
148 dbg("speedtch_got_firmware: usb_set_interface returned %d!", err);
149 instance->u.status = UDSL_NO_FIRMWARE;
150 goto out;
151 }
152
153 /* Set up interrupt endpoint */
154 intf = usb_ifnum_to_if(instance->u.usb_dev, 0);
155 if (intf && !usb_driver_claim_interface(&speedtch_usb_driver, intf, NULL)) {
156
157 instance->int_urb = usb_alloc_urb(0, GFP_KERNEL);
158 if (instance->int_urb) {
159
160 usb_fill_int_urb(instance->int_urb, instance->u.usb_dev,
161 usb_rcvintpipe(instance->u.usb_dev, SPEEDTCH_ENDPOINT_INT),
162 instance->int_data,
163 sizeof(instance->int_data),
164 speedtch_handle_int, instance, 50);
165 err = usb_submit_urb(instance->int_urb, GFP_KERNEL);
166 if (err) {
167 /* Doesn't matter; we'll poll anyway */
168 dbg("speedtch_got_firmware: Submission of interrupt URB failed %d", err);
169 usb_free_urb(instance->int_urb);
170 instance->int_urb = NULL;
171 usb_driver_release_interface(&speedtch_usb_driver, intf);
172 }
173 }
174 }
175 /* Start status polling */
176 mod_timer(&instance->poll_timer, jiffies + (1 * HZ));
177
178 instance->u.status = UDSL_LOADED_FIRMWARE;
179 tasklet_schedule(&instance->u.receive_tasklet);
180 out:
181 up(&instance->u.serialize);
182 wake_up_interruptible(&instance->u.firmware_waiters);
183}
184
185static int speedtch_set_swbuff(struct speedtch_instance_data *instance,
186 int state)
187{
188 struct usb_device *dev = instance->u.usb_dev;
189 int ret; 120 int ret;
190 121
191 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), 122 ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
192 0x32, 0x40, state ? 0x01 : 0x00, 123 0x32, 0x40, state ? 0x01 : 0x00, 0x00, NULL, 0, CTRL_TIMEOUT);
193 0x00, NULL, 0, 100); 124 if (ret < 0)
194 if (ret < 0) { 125 usb_warn(usbatm,
195 printk("Warning: %sabling SW buffering: usb_control_msg returned %d\n", 126 "%sabling SW buffering: usb_control_msg returned %d\n",
196 state ? "En" : "Dis", ret); 127 state ? "En" : "Dis", ret);
197 return ret; 128 else
198 } 129 dbg("speedtch_set_swbuff: %sbled SW buffering", state ? "En" : "Dis");
199
200 dbg("speedtch_set_swbuff: %sbled SW buffering", state ? "En" : "Dis");
201 return 0;
202} 130}
203 131
204static void speedtch_test_sequence(struct speedtch_instance_data *instance) 132static void speedtch_test_sequence(struct speedtch_instance_data *instance)
205{ 133{
206 struct usb_device *dev = instance->u.usb_dev; 134 struct usbatm_data *usbatm = instance->usbatm;
207 unsigned char buf[10]; 135 struct usb_device *usb_dev = usbatm->usb_dev;
136 unsigned char *buf = instance->scratch_buffer;
208 int ret; 137 int ret;
209 138
210 /* URB 147 */ 139 /* URB 147 */
211 buf[0] = 0x1c; 140 buf[0] = 0x1c;
212 buf[1] = 0x50; 141 buf[1] = 0x50;
213 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), 142 ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
214 0x01, 0x40, 0x0b, 0x00, buf, 2, 100); 143 0x01, 0x40, 0x0b, 0x00, buf, 2, CTRL_TIMEOUT);
215 if (ret < 0) 144 if (ret < 0)
216 printk(KERN_WARNING "%s failed on URB147: %d\n", __func__, ret); 145 usb_warn(usbatm, "%s failed on URB147: %d\n", __func__, ret);
217 146
218 /* URB 148 */ 147 /* URB 148 */
219 buf[0] = 0x32; 148 buf[0] = 0x32;
220 buf[1] = 0x00; 149 buf[1] = 0x00;
221 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), 150 ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
222 0x01, 0x40, 0x02, 0x00, buf, 2, 100); 151 0x01, 0x40, 0x02, 0x00, buf, 2, CTRL_TIMEOUT);
223 if (ret < 0) 152 if (ret < 0)
224 printk(KERN_WARNING "%s failed on URB148: %d\n", __func__, ret); 153 usb_warn(usbatm, "%s failed on URB148: %d\n", __func__, ret);
225 154
226 /* URB 149 */ 155 /* URB 149 */
227 buf[0] = 0x01; 156 buf[0] = 0x01;
228 buf[1] = 0x00; 157 buf[1] = 0x00;
229 buf[2] = 0x01; 158 buf[2] = 0x01;
230 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), 159 ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
231 0x01, 0x40, 0x03, 0x00, buf, 3, 100); 160 0x01, 0x40, 0x03, 0x00, buf, 3, CTRL_TIMEOUT);
232 if (ret < 0) 161 if (ret < 0)
233 printk(KERN_WARNING "%s failed on URB149: %d\n", __func__, ret); 162 usb_warn(usbatm, "%s failed on URB149: %d\n", __func__, ret);
234 163
235 /* URB 150 */ 164 /* URB 150 */
236 buf[0] = 0x01; 165 buf[0] = 0x01;
237 buf[1] = 0x00; 166 buf[1] = 0x00;
238 buf[2] = 0x01; 167 buf[2] = 0x01;
239 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), 168 ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
240 0x01, 0x40, 0x04, 0x00, buf, 3, 100); 169 0x01, 0x40, 0x04, 0x00, buf, 3, CTRL_TIMEOUT);
241 if (ret < 0) 170 if (ret < 0)
242 printk(KERN_WARNING "%s failed on URB150: %d\n", __func__, ret); 171 usb_warn(usbatm, "%s failed on URB150: %d\n", __func__, ret);
243} 172}
244 173
245static int speedtch_start_synchro(struct speedtch_instance_data *instance) 174static int speedtch_upload_firmware(struct speedtch_instance_data *instance,
175 const struct firmware *fw1,
176 const struct firmware *fw2)
246{ 177{
247 struct usb_device *dev = instance->u.usb_dev; 178 unsigned char *buffer;
248 unsigned char buf[2]; 179 struct usbatm_data *usbatm = instance->usbatm;
249 int ret; 180 struct usb_interface *intf;
181 struct usb_device *usb_dev = usbatm->usb_dev;
182 int actual_length;
183 int ret = 0;
184 int offset;
185
186 usb_dbg(usbatm, "%s entered\n", __func__);
187
188 if (!(buffer = (unsigned char *)__get_free_page(GFP_KERNEL))) {
189 ret = -ENOMEM;
190 usb_dbg(usbatm, "%s: no memory for buffer!\n", __func__);
191 goto out;
192 }
193
194 if (!(intf = usb_ifnum_to_if(usb_dev, 2))) {
195 ret = -ENODEV;
196 usb_dbg(usbatm, "%s: interface not found!\n", __func__);
197 goto out_free;
198 }
199
200 /* URB 7 */
201 if (dl_512_first) { /* some modems need a read before writing the firmware */
202 ret = usb_bulk_msg(usb_dev, usb_rcvbulkpipe(usb_dev, ENDPOINT_FIRMWARE),
203 buffer, 0x200, &actual_length, 2000);
204
205 if (ret < 0 && ret != -ETIMEDOUT)
206 usb_dbg(usbatm, "%s: read BLOCK0 from modem failed (%d)!\n", __func__, ret);
207 else
208 usb_dbg(usbatm, "%s: BLOCK0 downloaded (%d bytes)\n", __func__, ret);
209 }
210
211 /* URB 8 : both leds are static green */
212 for (offset = 0; offset < fw1->size; offset += PAGE_SIZE) {
213 int thislen = min_t(int, PAGE_SIZE, fw1->size - offset);
214 memcpy(buffer, fw1->data + offset, thislen);
215
216 ret = usb_bulk_msg(usb_dev, usb_sndbulkpipe(usb_dev, ENDPOINT_FIRMWARE),
217 buffer, thislen, &actual_length, DATA_TIMEOUT);
218
219 if (ret < 0) {
220 usb_dbg(usbatm, "%s: write BLOCK1 to modem failed (%d)!\n", __func__, ret);
221 goto out_free;
222 }
223 usb_dbg(usbatm, "%s: BLOCK1 uploaded (%zu bytes)\n", __func__, fw1->size);
224 }
225
226 /* USB led blinking green, ADSL led off */
227
228 /* URB 11 */
229 ret = usb_bulk_msg(usb_dev, usb_rcvbulkpipe(usb_dev, ENDPOINT_FIRMWARE),
230 buffer, 0x200, &actual_length, DATA_TIMEOUT);
250 231
251 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
252 0x12, 0xc0, 0x04, 0x00,
253 buf, sizeof(buf), CTRL_TIMEOUT);
254 if (ret < 0) { 232 if (ret < 0) {
255 printk(KERN_WARNING "SpeedTouch: Failed to start ADSL synchronisation: %d\n", ret); 233 usb_dbg(usbatm, "%s: read BLOCK2 from modem failed (%d)!\n", __func__, ret);
256 return ret; 234 goto out_free;
257 } 235 }
236 usb_dbg(usbatm, "%s: BLOCK2 downloaded (%d bytes)\n", __func__, actual_length);
258 237
259 dbg("speedtch_start_synchro: modem prodded. %d Bytes returned: %02x %02x", ret, buf[0], buf[1]); 238 /* URBs 12 to 139 - USB led blinking green, ADSL led off */
260 return 0; 239 for (offset = 0; offset < fw2->size; offset += PAGE_SIZE) {
240 int thislen = min_t(int, PAGE_SIZE, fw2->size - offset);
241 memcpy(buffer, fw2->data + offset, thislen);
242
243 ret = usb_bulk_msg(usb_dev, usb_sndbulkpipe(usb_dev, ENDPOINT_FIRMWARE),
244 buffer, thislen, &actual_length, DATA_TIMEOUT);
245
246 if (ret < 0) {
247 usb_dbg(usbatm, "%s: write BLOCK3 to modem failed (%d)!\n", __func__, ret);
248 goto out_free;
249 }
250 }
251 usb_dbg(usbatm, "%s: BLOCK3 uploaded (%zu bytes)\n", __func__, fw2->size);
252
253 /* USB led static green, ADSL led static red */
254
255 /* URB 142 */
256 ret = usb_bulk_msg(usb_dev, usb_rcvbulkpipe(usb_dev, ENDPOINT_FIRMWARE),
257 buffer, 0x200, &actual_length, DATA_TIMEOUT);
258
259 if (ret < 0) {
260 usb_dbg(usbatm, "%s: read BLOCK4 from modem failed (%d)!\n", __func__, ret);
261 goto out_free;
262 }
263
264 /* success */
265 usb_dbg(usbatm, "%s: BLOCK4 downloaded (%d bytes)\n", __func__, actual_length);
266
267 /* Delay to allow firmware to start up. We can do this here
268 because we're in our own kernel thread anyway. */
269 msleep_interruptible(1000);
270
271 /* Enable software buffering, if requested */
272 if (sw_buffering)
273 speedtch_set_swbuff(instance, 1);
274
275 /* Magic spell; don't ask us what this does */
276 speedtch_test_sequence(instance);
277
278 ret = 0;
279
280out_free:
281 free_page((unsigned long)buffer);
282out:
283 return ret;
261} 284}
262 285
263static void speedtch_handle_int(struct urb *urb, struct pt_regs *regs) 286static int speedtch_find_firmware(struct usb_interface *intf, int phase,
287 const struct firmware **fw_p)
264{ 288{
265 struct speedtch_instance_data *instance = urb->context; 289 struct device *dev = &intf->dev;
266 unsigned int count = urb->actual_length; 290 const u16 bcdDevice = le16_to_cpu(interface_to_usbdev(intf)->descriptor.bcdDevice);
267 int ret; 291 const u8 major_revision = bcdDevice >> 8;
292 const u8 minor_revision = bcdDevice & 0xff;
293 char buf[24];
268 294
269 /* The magic interrupt for "up state" */ 295 sprintf(buf, "speedtch-%d.bin.%x.%02x", phase, major_revision, minor_revision);
270 const static unsigned char up_int[6] = { 0xa1, 0x00, 0x01, 0x00, 0x00, 0x00 }; 296 dev_dbg(dev, "%s: looking for %s\n", __func__, buf);
271 /* The magic interrupt for "down state" */
272 const static unsigned char down_int[6] = { 0xa1, 0x00, 0x00, 0x00, 0x00, 0x00 };
273 297
274 switch (urb->status) { 298 if (request_firmware(fw_p, buf, dev)) {
275 case 0: 299 sprintf(buf, "speedtch-%d.bin.%x", phase, major_revision);
276 /* success */ 300 dev_dbg(dev, "%s: looking for %s\n", __func__, buf);
277 break;
278 case -ECONNRESET:
279 case -ENOENT:
280 case -ESHUTDOWN:
281 /* this urb is terminated; clean up */
282 dbg("%s - urb shutting down with status: %d", __func__, urb->status);
283 return;
284 default:
285 dbg("%s - nonzero urb status received: %d", __func__, urb->status);
286 goto exit;
287 }
288 301
289 if (count < 6) { 302 if (request_firmware(fw_p, buf, dev)) {
290 dbg("%s - int packet too short", __func__); 303 sprintf(buf, "speedtch-%d.bin", phase);
291 goto exit; 304 dev_dbg(dev, "%s: looking for %s\n", __func__, buf);
305
306 if (request_firmware(fw_p, buf, dev)) {
307 dev_warn(dev, "no stage %d firmware found!\n", phase);
308 return -ENOENT;
309 }
310 }
292 } 311 }
293 312
294 if (!memcmp(up_int, instance->int_data, 6)) { 313 dev_info(dev, "found stage %d firmware %s\n", phase, buf);
295 del_timer(&instance->poll_timer);
296 printk(KERN_NOTICE "DSL line goes up\n");
297 } else if (!memcmp(down_int, instance->int_data, 6)) {
298 printk(KERN_NOTICE "DSL line goes down\n");
299 } else {
300 int i;
301 314
302 printk(KERN_DEBUG "Unknown interrupt packet of %d bytes:", count); 315 return 0;
303 for (i = 0; i < count; i++) 316}
304 printk(" %02x", instance->int_data[i]); 317
305 printk("\n"); 318static int speedtch_heavy_init(struct usbatm_data *usbatm, struct usb_interface *intf)
319{
320 const struct firmware *fw1, *fw2;
321 struct speedtch_instance_data *instance = usbatm->driver_data;
322 int ret;
323
324 if ((ret = speedtch_find_firmware(intf, 1, &fw1)) < 0)
325 return ret;
326
327 if ((ret = speedtch_find_firmware(intf, 2, &fw2)) < 0) {
328 release_firmware(fw1);
329 return ret;
306 } 330 }
307 schedule_work(&instance->poll_work);
308 331
309 exit: 332 ret = speedtch_upload_firmware(instance, fw1, fw2);
310 rmb(); 333
311 if (!instance->int_urb) 334 release_firmware(fw2);
312 return; 335 release_firmware(fw1);
313 336
314 ret = usb_submit_urb(urb, GFP_ATOMIC); 337 return ret;
315 if (ret)
316 err("%s - usb_submit_urb failed with result %d", __func__, ret);
317} 338}
318 339
319static int speedtch_get_status(struct speedtch_instance_data *instance, 340
320 unsigned char *buf) 341/**********
342** ATM **
343**********/
344
345static int speedtch_read_status(struct speedtch_instance_data *instance)
321{ 346{
322 struct usb_device *dev = instance->u.usb_dev; 347 struct usbatm_data *usbatm = instance->usbatm;
348 struct usb_device *usb_dev = usbatm->usb_dev;
349 unsigned char *buf = instance->scratch_buffer;
323 int ret; 350 int ret;
324 351
325 memset(buf, 0, TOTAL); 352 memset(buf, 0, TOTAL);
326 353
327 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), 354 ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
328 0x12, 0xc0, 0x07, 0x00, buf + OFFSET_7, SIZE_7, 355 0x12, 0xc0, 0x07, 0x00, buf + OFFSET_7, SIZE_7,
329 CTRL_TIMEOUT); 356 CTRL_TIMEOUT);
330 if (ret < 0) { 357 if (ret < 0) {
331 dbg("MSG 7 failed"); 358 atm_dbg(usbatm, "%s: MSG 7 failed\n", __func__);
332 return ret; 359 return ret;
333 } 360 }
334 361
335 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), 362 ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
336 0x12, 0xc0, 0x0b, 0x00, buf + OFFSET_b, SIZE_b, 363 0x12, 0xc0, 0x0b, 0x00, buf + OFFSET_b, SIZE_b,
337 CTRL_TIMEOUT); 364 CTRL_TIMEOUT);
338 if (ret < 0) { 365 if (ret < 0) {
339 dbg("MSG B failed"); 366 atm_dbg(usbatm, "%s: MSG B failed\n", __func__);
340 return ret; 367 return ret;
341 } 368 }
342 369
343 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), 370 ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
344 0x12, 0xc0, 0x0d, 0x00, buf + OFFSET_d, SIZE_d, 371 0x12, 0xc0, 0x0d, 0x00, buf + OFFSET_d, SIZE_d,
345 CTRL_TIMEOUT); 372 CTRL_TIMEOUT);
346 if (ret < 0) { 373 if (ret < 0) {
347 dbg("MSG D failed"); 374 atm_dbg(usbatm, "%s: MSG D failed\n", __func__);
348 return ret; 375 return ret;
349 } 376 }
350 377
351 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), 378 ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
352 0x01, 0xc0, 0x0e, 0x00, buf + OFFSET_e, SIZE_e, 379 0x01, 0xc0, 0x0e, 0x00, buf + OFFSET_e, SIZE_e,
353 CTRL_TIMEOUT); 380 CTRL_TIMEOUT);
354 if (ret < 0) { 381 if (ret < 0) {
355 dbg("MSG E failed"); 382 atm_dbg(usbatm, "%s: MSG E failed\n", __func__);
356 return ret; 383 return ret;
357 } 384 }
358 385
359 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), 386 ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
360 0x01, 0xc0, 0x0f, 0x00, buf + OFFSET_f, SIZE_f, 387 0x01, 0xc0, 0x0f, 0x00, buf + OFFSET_f, SIZE_f,
361 CTRL_TIMEOUT); 388 CTRL_TIMEOUT);
362 if (ret < 0) { 389 if (ret < 0) {
363 dbg("MSG F failed"); 390 atm_dbg(usbatm, "%s: MSG F failed\n", __func__);
364 return ret; 391 return ret;
365 } 392 }
366 393
367 return 0; 394 return 0;
368} 395}
369 396
370static void speedtch_poll_status(struct speedtch_instance_data *instance) 397static int speedtch_start_synchro(struct speedtch_instance_data *instance)
371{ 398{
372 unsigned char buf[TOTAL]; 399 struct usbatm_data *usbatm = instance->usbatm;
400 struct usb_device *usb_dev = usbatm->usb_dev;
401 unsigned char *buf = instance->scratch_buffer;
373 int ret; 402 int ret;
374 403
375 ret = speedtch_get_status(instance, buf); 404 atm_dbg(usbatm, "%s entered\n", __func__);
376 if (ret) { 405
377 printk(KERN_WARNING 406 memset(buf, 0, 2);
378 "SpeedTouch: Error %d fetching device status\n", ret); 407
408 ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
409 0x12, 0xc0, 0x04, 0x00,
410 buf, 2, CTRL_TIMEOUT);
411
412 if (ret < 0)
413 atm_warn(usbatm, "failed to start ADSL synchronisation: %d\n", ret);
414 else
415 atm_dbg(usbatm, "%s: modem prodded. %d bytes returned: %02x %02x\n",
416 __func__, ret, buf[0], buf[1]);
417
418 return ret;
419}
420
421static void speedtch_check_status(struct speedtch_instance_data *instance)
422{
423 struct usbatm_data *usbatm = instance->usbatm;
424 struct atm_dev *atm_dev = usbatm->atm_dev;
425 unsigned char *buf = instance->scratch_buffer;
426 int ret;
427
428 atm_dbg(usbatm, "%s entered\n", __func__);
429
430 ret = speedtch_read_status(instance);
431 if (ret < 0) {
432 atm_warn(usbatm, "error %d fetching device status\n", ret);
433 if (instance->poll_delay < MAX_POLL_DELAY)
434 instance->poll_delay *= 2;
379 return; 435 return;
380 } 436 }
381 437
382 dbg("Line state %02x", buf[OFFSET_7]); 438 if (instance->poll_delay > MIN_POLL_DELAY)
439 instance->poll_delay /= 2;
440
441 atm_dbg(usbatm, "%s: line state %02x\n", __func__, buf[OFFSET_7]);
383 442
384 switch (buf[OFFSET_7]) { 443 switch (buf[OFFSET_7]) {
385 case 0: 444 case 0:
386 if (instance->u.atm_dev->signal != ATM_PHY_SIG_LOST) { 445 if (atm_dev->signal != ATM_PHY_SIG_LOST) {
387 instance->u.atm_dev->signal = ATM_PHY_SIG_LOST; 446 atm_dev->signal = ATM_PHY_SIG_LOST;
388 printk(KERN_NOTICE "ADSL line is down\n"); 447 atm_info(usbatm, "ADSL line is down\n");
389 /* It'll never resync again unless we ask it to... */ 448 /* It'll never resync again unless we ask it to... */
390 speedtch_start_synchro(instance); 449 ret = speedtch_start_synchro(instance);
391 } 450 }
392 break; 451 break;
393 452
394 case 0x08: 453 case 0x08:
395 if (instance->u.atm_dev->signal != ATM_PHY_SIG_UNKNOWN) { 454 if (atm_dev->signal != ATM_PHY_SIG_UNKNOWN) {
396 instance->u.atm_dev->signal = ATM_PHY_SIG_UNKNOWN; 455 atm_dev->signal = ATM_PHY_SIG_UNKNOWN;
397 printk(KERN_NOTICE "ADSL line is blocked?\n"); 456 atm_info(usbatm, "ADSL line is blocked?\n");
398 } 457 }
399 break; 458 break;
400 459
401 case 0x10: 460 case 0x10:
402 if (instance->u.atm_dev->signal != ATM_PHY_SIG_LOST) { 461 if (atm_dev->signal != ATM_PHY_SIG_LOST) {
403 instance->u.atm_dev->signal = ATM_PHY_SIG_LOST; 462 atm_dev->signal = ATM_PHY_SIG_LOST;
404 printk(KERN_NOTICE "ADSL line is synchronising\n"); 463 atm_info(usbatm, "ADSL line is synchronising\n");
405 } 464 }
406 break; 465 break;
407 466
408 case 0x20: 467 case 0x20:
409 if (instance->u.atm_dev->signal != ATM_PHY_SIG_FOUND) { 468 if (atm_dev->signal != ATM_PHY_SIG_FOUND) {
410 int down_speed = buf[OFFSET_b] | (buf[OFFSET_b + 1] << 8) 469 int down_speed = buf[OFFSET_b] | (buf[OFFSET_b + 1] << 8)
411 | (buf[OFFSET_b + 2] << 16) | (buf[OFFSET_b + 3] << 24); 470 | (buf[OFFSET_b + 2] << 16) | (buf[OFFSET_b + 3] << 24);
412 int up_speed = buf[OFFSET_b + 4] | (buf[OFFSET_b + 5] << 8) 471 int up_speed = buf[OFFSET_b + 4] | (buf[OFFSET_b + 5] << 8)
413 | (buf[OFFSET_b + 6] << 16) | (buf[OFFSET_b + 7] << 24); 472 | (buf[OFFSET_b + 6] << 16) | (buf[OFFSET_b + 7] << 24);
414 473
415 if (!(down_speed & 0x0000ffff) && 474 if (!(down_speed & 0x0000ffff) && !(up_speed & 0x0000ffff)) {
416 !(up_speed & 0x0000ffff)) {
417 down_speed >>= 16; 475 down_speed >>= 16;
418 up_speed >>= 16; 476 up_speed >>= 16;
419 } 477 }
420 instance->u.atm_dev->link_rate = down_speed * 1000 / 424;
421 instance->u.atm_dev->signal = ATM_PHY_SIG_FOUND;
422 478
423 printk(KERN_NOTICE 479 atm_dev->link_rate = down_speed * 1000 / 424;
424 "ADSL line is up (%d Kib/s down | %d Kib/s up)\n", 480 atm_dev->signal = ATM_PHY_SIG_FOUND;
425 down_speed, up_speed); 481
482 atm_info(usbatm,
483 "ADSL line is up (%d Kib/s down | %d Kib/s up)\n",
484 down_speed, up_speed);
426 } 485 }
427 break; 486 break;
428 487
429 default: 488 default:
430 if (instance->u.atm_dev->signal != ATM_PHY_SIG_UNKNOWN) { 489 if (atm_dev->signal != ATM_PHY_SIG_UNKNOWN) {
431 instance->u.atm_dev->signal = ATM_PHY_SIG_UNKNOWN; 490 atm_dev->signal = ATM_PHY_SIG_UNKNOWN;
432 printk(KERN_NOTICE "Unknown line state %02x\n", buf[OFFSET_7]); 491 atm_info(usbatm, "Unknown line state %02x\n", buf[OFFSET_7]);
433 } 492 }
434 break; 493 break;
435 } 494 }
436} 495}
437 496
438static void speedtch_timer_poll(unsigned long data) 497static void speedtch_status_poll(unsigned long data)
439{ 498{
440 struct speedtch_instance_data *instance = (void *)data; 499 struct speedtch_instance_data *instance = (void *)data;
441 500
442 schedule_work(&instance->poll_work); 501 schedule_work(&instance->status_checker);
443 mod_timer(&instance->poll_timer, jiffies + (5 * HZ)); 502
503 /* The following check is racy, but the race is harmless */
504 if (instance->poll_delay < MAX_POLL_DELAY)
505 mod_timer(&instance->status_checker.timer, jiffies + msecs_to_jiffies(instance->poll_delay));
506 else
507 atm_warn(instance->usbatm, "Too many failures - disabling line status polling\n");
444} 508}
445 509
446#ifdef USE_FW_LOADER 510static void speedtch_resubmit_int(unsigned long data)
447static void speedtch_upload_firmware(struct speedtch_instance_data *instance,
448 const struct firmware *fw1,
449 const struct firmware *fw2)
450{ 511{
451 unsigned char *buffer; 512 struct speedtch_instance_data *instance = (void *)data;
452 struct usb_device *usb_dev = instance->u.usb_dev; 513 struct urb *int_urb = instance->int_urb;
453 struct usb_interface *intf; 514 int ret;
454 int actual_length, ret;
455 int offset;
456
457 dbg("speedtch_upload_firmware");
458
459 if (!(intf = usb_ifnum_to_if(usb_dev, 2))) {
460 dbg("speedtch_upload_firmware: interface not found!");
461 goto fail;
462 }
463
464 if (!(buffer = (unsigned char *)__get_free_page(GFP_KERNEL))) {
465 dbg("speedtch_upload_firmware: no memory for buffer!");
466 goto fail;
467 }
468
469 /* A user-space firmware loader may already have claimed interface #2 */
470 if ((ret =
471 usb_driver_claim_interface(&speedtch_usb_driver, intf, NULL)) < 0) {
472 dbg("speedtch_upload_firmware: interface in use (%d)!", ret);
473 goto fail_free;
474 }
475
476 /* URB 7 */
477 if (dl_512_first) { /* some modems need a read before writing the firmware */
478 ret = usb_bulk_msg(usb_dev, usb_rcvbulkpipe(usb_dev, SPEEDTCH_ENDPOINT_FIRMWARE),
479 buffer, 0x200, &actual_length, 2000);
480
481 if (ret < 0 && ret != -ETIMEDOUT)
482 dbg("speedtch_upload_firmware: read BLOCK0 from modem failed (%d)!", ret);
483 else
484 dbg("speedtch_upload_firmware: BLOCK0 downloaded (%d bytes)", ret);
485 }
486
487 /* URB 8 : both leds are static green */
488 for (offset = 0; offset < fw1->size; offset += PAGE_SIZE) {
489 int thislen = min_t(int, PAGE_SIZE, fw1->size - offset);
490 memcpy(buffer, fw1->data + offset, thislen);
491 515
492 ret = usb_bulk_msg(usb_dev, usb_sndbulkpipe(usb_dev, SPEEDTCH_ENDPOINT_FIRMWARE), 516 atm_dbg(instance->usbatm, "%s entered\n", __func__);
493 buffer, thislen, &actual_length, DATA_TIMEOUT);
494 517
495 if (ret < 0) { 518 if (int_urb) {
496 dbg("speedtch_upload_firmware: write BLOCK1 to modem failed (%d)!", ret); 519 ret = usb_submit_urb(int_urb, GFP_ATOMIC);
497 goto fail_release; 520 if (!ret)
521 schedule_work(&instance->status_checker);
522 else {
523 atm_dbg(instance->usbatm, "%s: usb_submit_urb failed with result %d\n", __func__, ret);
524 mod_timer(&instance->resubmit_timer, jiffies + msecs_to_jiffies(RESUBMIT_DELAY));
498 } 525 }
499 dbg("speedtch_upload_firmware: BLOCK1 uploaded (%zu bytes)", fw1->size);
500 } 526 }
527}
501 528
502 /* USB led blinking green, ADSL led off */ 529static void speedtch_handle_int(struct urb *int_urb, struct pt_regs *regs)
530{
531 struct speedtch_instance_data *instance = int_urb->context;
532 struct usbatm_data *usbatm = instance->usbatm;
533 unsigned int count = int_urb->actual_length;
534 int ret = int_urb->status;
503 535
504 /* URB 11 */ 536 /* The magic interrupt for "up state" */
505 ret = usb_bulk_msg(usb_dev, usb_rcvbulkpipe(usb_dev, SPEEDTCH_ENDPOINT_FIRMWARE), 537 const static unsigned char up_int[6] = { 0xa1, 0x00, 0x01, 0x00, 0x00, 0x00 };
506 buffer, 0x200, &actual_length, DATA_TIMEOUT); 538 /* The magic interrupt for "down state" */
539 const static unsigned char down_int[6] = { 0xa1, 0x00, 0x00, 0x00, 0x00, 0x00 };
540
541 atm_dbg(usbatm, "%s entered\n", __func__);
507 542
508 if (ret < 0) { 543 if (ret < 0) {
509 dbg("speedtch_upload_firmware: read BLOCK2 from modem failed (%d)!", ret); 544 atm_dbg(usbatm, "%s: nonzero urb status %d!\n", __func__, ret);
510 goto fail_release; 545 goto fail;
511 } 546 }
512 dbg("speedtch_upload_firmware: BLOCK2 downloaded (%d bytes)", actual_length);
513 547
514 /* URBs 12 to 139 - USB led blinking green, ADSL led off */ 548 if ((count == 6) && !memcmp(up_int, instance->int_data, 6)) {
515 for (offset = 0; offset < fw2->size; offset += PAGE_SIZE) { 549 del_timer(&instance->status_checker.timer);
516 int thislen = min_t(int, PAGE_SIZE, fw2->size - offset); 550 atm_info(usbatm, "DSL line goes up\n");
517 memcpy(buffer, fw2->data + offset, thislen); 551 } else if ((count == 6) && !memcmp(down_int, instance->int_data, 6)) {
552 atm_info(usbatm, "DSL line goes down\n");
553 } else {
554 int i;
518 555
519 ret = usb_bulk_msg(usb_dev, usb_sndbulkpipe(usb_dev, SPEEDTCH_ENDPOINT_FIRMWARE), 556 atm_dbg(usbatm, "%s: unknown interrupt packet of length %d:", __func__, count);
520 buffer, thislen, &actual_length, DATA_TIMEOUT); 557 for (i = 0; i < count; i++)
558 printk(" %02x", instance->int_data[i]);
559 printk("\n");
560 goto fail;
561 }
521 562
563 if ((int_urb = instance->int_urb)) {
564 ret = usb_submit_urb(int_urb, GFP_ATOMIC);
565 schedule_work(&instance->status_checker);
522 if (ret < 0) { 566 if (ret < 0) {
523 dbg("speedtch_upload_firmware: write BLOCK3 to modem failed (%d)!", ret); 567 atm_dbg(usbatm, "%s: usb_submit_urb failed with result %d\n", __func__, ret);
524 goto fail_release; 568 goto fail;
525 } 569 }
526 } 570 }
527 dbg("speedtch_upload_firmware: BLOCK3 uploaded (%zu bytes)", fw2->size);
528
529 /* USB led static green, ADSL led static red */
530
531 /* URB 142 */
532 ret = usb_bulk_msg(usb_dev, usb_rcvbulkpipe(usb_dev, SPEEDTCH_ENDPOINT_FIRMWARE),
533 buffer, 0x200, &actual_length, DATA_TIMEOUT);
534
535 if (ret < 0) {
536 dbg("speedtch_upload_firmware: read BLOCK4 from modem failed (%d)!", ret);
537 goto fail_release;
538 }
539
540 /* success */
541 dbg("speedtch_upload_firmware: BLOCK4 downloaded (%d bytes)", actual_length);
542
543 /* Delay to allow firmware to start up. We can do this here
544 because we're in our own kernel thread anyway. */
545 msleep(1000);
546
547 /* Enable software buffering, if requested */
548 if (sw_buffering)
549 speedtch_set_swbuff(instance, 1);
550
551 /* Magic spell; don't ask us what this does */
552 speedtch_test_sequence(instance);
553
554 /* Start modem synchronisation */
555 if (speedtch_start_synchro(instance))
556 dbg("speedtch_start_synchro: failed");
557
558 speedtch_got_firmware(instance, 1);
559 571
560 free_page((unsigned long)buffer);
561 return; 572 return;
562 573
563 fail_release: 574fail:
564 /* Only release interface #2 if uploading failed; we don't release it 575 if ((int_urb = instance->int_urb))
565 we succeeded. This prevents the userspace tools from trying to load 576 mod_timer(&instance->resubmit_timer, jiffies + msecs_to_jiffies(RESUBMIT_DELAY));
566 the firmware themselves */
567 usb_driver_release_interface(&speedtch_usb_driver, intf);
568 fail_free:
569 free_page((unsigned long)buffer);
570 fail:
571 speedtch_got_firmware(instance, 0);
572} 577}
573 578
574static int speedtch_find_firmware(struct speedtch_instance_data 579static int speedtch_atm_start(struct usbatm_data *usbatm, struct atm_dev *atm_dev)
575 *instance, int phase,
576 const struct firmware **fw_p)
577{ 580{
578 char buf[24]; 581 struct usb_device *usb_dev = usbatm->usb_dev;
579 const u16 bcdDevice = le16_to_cpu(instance->u.usb_dev->descriptor.bcdDevice); 582 struct speedtch_instance_data *instance = usbatm->driver_data;
580 const u8 major_revision = bcdDevice >> 8; 583 int i, ret;
581 const u8 minor_revision = bcdDevice & 0xff; 584 unsigned char mac_str[13];
582
583 sprintf(buf, "speedtch-%d.bin.%x.%02x", phase, major_revision, minor_revision);
584 dbg("speedtch_find_firmware: looking for %s", buf);
585
586 if (request_firmware(fw_p, buf, &instance->u.usb_dev->dev)) {
587 sprintf(buf, "speedtch-%d.bin.%x", phase, major_revision);
588 dbg("speedtch_find_firmware: looking for %s", buf);
589 585
590 if (request_firmware(fw_p, buf, &instance->u.usb_dev->dev)) { 586 atm_dbg(usbatm, "%s entered\n", __func__);
591 sprintf(buf, "speedtch-%d.bin", phase);
592 dbg("speedtch_find_firmware: looking for %s", buf);
593 587
594 if (request_firmware(fw_p, buf, &instance->u.usb_dev->dev)) { 588 if ((ret = usb_set_interface(usb_dev, 1, altsetting)) < 0) {
595 dev_warn(&instance->u.usb_dev->dev, "no stage %d firmware found!", phase); 589 atm_dbg(usbatm, "%s: usb_set_interface returned %d!\n", __func__, ret);
596 return -ENOENT; 590 return ret;
597 }
598 }
599 } 591 }
600 592
601 dev_info(&instance->u.usb_dev->dev, "found stage %d firmware %s\n", phase, buf); 593 /* Set MAC address, it is stored in the serial number */
602 594 memset(atm_dev->esi, 0, sizeof(atm_dev->esi));
603 return 0; 595 if (usb_string(usb_dev, usb_dev->descriptor.iSerialNumber, mac_str, sizeof(mac_str)) == 12) {
604} 596 for (i = 0; i < 6; i++)
605 597 atm_dev->esi[i] = (hex2int(mac_str[i * 2]) * 16) + (hex2int(mac_str[i * 2 + 1]));
606static int speedtch_load_firmware(void *arg) 598 }
607{
608 const struct firmware *fw1, *fw2;
609 struct speedtch_instance_data *instance = arg;
610
611 BUG_ON(!instance);
612 599
613 daemonize("firmware/speedtch"); 600 /* Start modem synchronisation */
601 ret = speedtch_start_synchro(instance);
614 602
615 if (!speedtch_find_firmware(instance, 1, &fw1)) { 603 /* Set up interrupt endpoint */
616 if (!speedtch_find_firmware(instance, 2, &fw2)) { 604 if (instance->int_urb) {
617 speedtch_upload_firmware(instance, fw1, fw2); 605 ret = usb_submit_urb(instance->int_urb, GFP_KERNEL);
618 release_firmware(fw2); 606 if (ret < 0) {
607 /* Doesn't matter; we'll poll anyway */
608 atm_dbg(usbatm, "%s: submission of interrupt URB failed (%d)!\n", __func__, ret);
609 usb_free_urb(instance->int_urb);
610 instance->int_urb = NULL;
619 } 611 }
620 release_firmware(fw1);
621 } 612 }
622 613
623 /* In case we failed, set state back to NO_FIRMWARE so that 614 /* Start status polling */
624 another later attempt may work. Otherwise, we never actually 615 mod_timer(&instance->status_checker.timer, jiffies + msecs_to_jiffies(1000));
625 manage to recover if, for example, the firmware is on /usr and
626 we look for it too early. */
627 speedtch_got_firmware(instance, 0);
628 616
629 module_put(THIS_MODULE);
630 udsl_put_instance(&instance->u);
631 return 0; 617 return 0;
632} 618}
633#endif /* USE_FW_LOADER */
634 619
635static void speedtch_firmware_start(struct speedtch_instance_data *instance) 620static void speedtch_atm_stop(struct usbatm_data *usbatm, struct atm_dev *atm_dev)
636{ 621{
637#ifdef USE_FW_LOADER 622 struct speedtch_instance_data *instance = usbatm->driver_data;
638 int ret; 623 struct urb *int_urb = instance->int_urb;
639#endif 624
640 625 atm_dbg(usbatm, "%s entered\n", __func__);
641 dbg("speedtch_firmware_start"); 626
642 627 del_timer_sync(&instance->status_checker.timer);
643 down(&instance->u.serialize); /* vs self, speedtch_got_firmware */ 628
644 629 /*
645 if (instance->u.status >= UDSL_LOADING_FIRMWARE) { 630 * Since resubmit_timer and int_urb can schedule themselves and
646 up(&instance->u.serialize); 631 * each other, shutting them down correctly takes some care
647 return; 632 */
648 } 633 instance->int_urb = NULL; /* signal shutdown */
634 mb();
635 usb_kill_urb(int_urb);
636 del_timer_sync(&instance->resubmit_timer);
637 /*
638 * At this point, speedtch_handle_int and speedtch_resubmit_int
639 * can run or be running, but instance->int_urb == NULL means that
640 * they will not reschedule
641 */
642 usb_kill_urb(int_urb);
643 del_timer_sync(&instance->resubmit_timer);
644 usb_free_urb(int_urb);
649 645
650 instance->u.status = UDSL_LOADING_FIRMWARE; 646 flush_scheduled_work();
651 up(&instance->u.serialize); 647}
652 648
653#ifdef USE_FW_LOADER
654 udsl_get_instance(&instance->u);
655 try_module_get(THIS_MODULE);
656 649
657 ret = kernel_thread(speedtch_load_firmware, instance, 650/**********
658 CLONE_FS | CLONE_FILES); 651** USB **
652**********/
659 653
660 if (ret >= 0) 654static struct usb_device_id speedtch_usb_ids[] = {
661 return; /* OK */ 655 {USB_DEVICE(0x06b9, 0x4061)},
656 {}
657};
662 658
663 dbg("speedtch_firmware_start: kernel_thread failed (%d)!", ret); 659MODULE_DEVICE_TABLE(usb, speedtch_usb_ids);
664 660
665 module_put(THIS_MODULE); 661static int speedtch_usb_probe(struct usb_interface *, const struct usb_device_id *);
666 udsl_put_instance(&instance->u);
667 /* Just pretend it never happened... hope modem_run happens */
668#endif /* USE_FW_LOADER */
669 662
670 speedtch_got_firmware(instance, 0); 663static struct usb_driver speedtch_usb_driver = {
671} 664 .owner = THIS_MODULE,
672 665 .name = speedtch_driver_name,
673static int speedtch_firmware_wait(struct udsl_instance_data *instance) 666 .probe = speedtch_usb_probe,
674{ 667 .disconnect = usbatm_usb_disconnect,
675 speedtch_firmware_start((void *)instance); 668 .id_table = speedtch_usb_ids
669};
676 670
677 if (wait_event_interruptible(instance->firmware_waiters, instance->status != UDSL_LOADING_FIRMWARE) < 0) 671static void speedtch_release_interfaces(struct usb_device *usb_dev, int num_interfaces) {
678 return -ERESTARTSYS; 672 struct usb_interface *cur_intf;
673 int i;
679 674
680 return (instance->status == UDSL_LOADED_FIRMWARE) ? 0 : -EAGAIN; 675 for(i = 0; i < num_interfaces; i++)
676 if ((cur_intf = usb_ifnum_to_if(usb_dev, i))) {
677 usb_set_intfdata(cur_intf, NULL);
678 usb_driver_release_interface(&speedtch_usb_driver, cur_intf);
679 }
681} 680}
682 681
683/********** 682static int speedtch_bind(struct usbatm_data *usbatm,
684** USB ** 683 struct usb_interface *intf,
685**********/ 684 const struct usb_device_id *id,
686 685 int *need_heavy_init)
687static int speedtch_usb_ioctl(struct usb_interface *intf, unsigned int code,
688 void *user_data)
689{ 686{
690 struct speedtch_instance_data *instance = usb_get_intfdata(intf); 687 struct usb_device *usb_dev = interface_to_usbdev(intf);
688 struct usb_interface *cur_intf;
689 struct speedtch_instance_data *instance;
690 int ifnum = intf->altsetting->desc.bInterfaceNumber;
691 int num_interfaces = usb_dev->actconfig->desc.bNumInterfaces;
692 int i, ret;
691 693
692 dbg("speedtch_usb_ioctl entered"); 694 usb_dbg(usbatm, "%s entered\n", __func__);
693 695
694 if (!instance) { 696 if (usb_dev->descriptor.bDeviceClass != USB_CLASS_VENDOR_SPEC) {
695 dbg("speedtch_usb_ioctl: NULL instance!"); 697 usb_dbg(usbatm, "%s: wrong device class %d\n", __func__, usb_dev->descriptor.bDeviceClass);
696 return -ENODEV; 698 return -ENODEV;
697 } 699 }
698 700
699 switch (code) { 701 /* claim all interfaces */
700 case UDSL_IOCTL_LINE_UP:
701 instance->u.atm_dev->signal = ATM_PHY_SIG_FOUND;
702 speedtch_got_firmware(instance, 1);
703 return (instance->u.status == UDSL_LOADED_FIRMWARE) ? 0 : -EIO;
704 case UDSL_IOCTL_LINE_DOWN:
705 instance->u.atm_dev->signal = ATM_PHY_SIG_LOST;
706 return 0;
707 default:
708 return -ENOTTY;
709 }
710}
711 702
712static int speedtch_usb_probe(struct usb_interface *intf, 703 for (i=0; i < num_interfaces; i++) {
713 const struct usb_device_id *id) 704 cur_intf = usb_ifnum_to_if(usb_dev, i);
714{
715 struct usb_device *dev = interface_to_usbdev(intf);
716 int ifnum = intf->altsetting->desc.bInterfaceNumber;
717 struct speedtch_instance_data *instance;
718 unsigned char mac_str[13];
719 int ret, i;
720 char buf7[SIZE_7];
721 705
722 dbg("speedtch_usb_probe: trying device with vendor=0x%x, product=0x%x, ifnum %d", 706 if ((i != ifnum) && cur_intf) {
723 le16_to_cpu(dev->descriptor.idVendor), 707 ret = usb_driver_claim_interface(&speedtch_usb_driver, cur_intf, usbatm);
724 le16_to_cpu(dev->descriptor.idProduct), ifnum);
725 708
726 if ((dev->descriptor.bDeviceClass != USB_CLASS_VENDOR_SPEC) || 709 if (ret < 0) {
727 (ifnum != 1)) 710 usb_dbg(usbatm, "%s: failed to claim interface %d (%d)\n", __func__, i, ret);
728 return -ENODEV; 711 speedtch_release_interfaces(usb_dev, i);
729 712 return ret;
730 dbg("speedtch_usb_probe: device accepted"); 713 }
714 }
715 }
731 716
732 /* instance init */
733 instance = kmalloc(sizeof(*instance), GFP_KERNEL); 717 instance = kmalloc(sizeof(*instance), GFP_KERNEL);
718
734 if (!instance) { 719 if (!instance) {
735 dbg("speedtch_usb_probe: no memory for instance data!"); 720 usb_dbg(usbatm, "%s: no memory for instance data!\n", __func__);
736 return -ENOMEM; 721 ret = -ENOMEM;
722 goto fail_release;
737 } 723 }
738 724
739 memset(instance, 0, sizeof(struct speedtch_instance_data)); 725 memset(instance, 0, sizeof(struct speedtch_instance_data));
740 726
741 if ((ret = usb_set_interface(dev, 0, 0)) < 0) 727 instance->usbatm = usbatm;
742 goto fail;
743 728
744 if ((ret = usb_set_interface(dev, 2, 0)) < 0) 729 INIT_WORK(&instance->status_checker, (void *)speedtch_check_status, instance);
745 goto fail;
746 730
747 instance->u.data_endpoint = SPEEDTCH_ENDPOINT_DATA; 731 instance->status_checker.timer.function = speedtch_status_poll;
748 instance->u.firmware_wait = speedtch_firmware_wait; 732 instance->status_checker.timer.data = (unsigned long)instance;
749 instance->u.driver_name = speedtch_driver_name; 733 instance->poll_delay = MIN_POLL_DELAY;
750 734
751 ret = udsl_instance_setup(dev, &instance->u); 735 init_timer(&instance->resubmit_timer);
752 if (ret) 736 instance->resubmit_timer.function = speedtch_resubmit_int;
753 goto fail; 737 instance->resubmit_timer.data = (unsigned long)instance;
754 738
755 init_timer(&instance->poll_timer); 739 instance->int_urb = usb_alloc_urb(0, GFP_KERNEL);
756 instance->poll_timer.function = speedtch_timer_poll;
757 instance->poll_timer.data = (unsigned long)instance;
758 740
759 INIT_WORK(&instance->poll_work, (void *)speedtch_poll_status, instance); 741 if (instance->int_urb)
742 usb_fill_int_urb(instance->int_urb, usb_dev,
743 usb_rcvintpipe(usb_dev, ENDPOINT_INT),
744 instance->int_data, sizeof(instance->int_data),
745 speedtch_handle_int, instance, 50);
746 else
747 usb_dbg(usbatm, "%s: no memory for interrupt urb!\n", __func__);
760 748
761 /* set MAC address, it is stored in the serial number */ 749 /* check whether the modem already seems to be alive */
762 memset(instance->u.atm_dev->esi, 0, sizeof(instance->u.atm_dev->esi)); 750 ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
763 if (usb_string(dev, dev->descriptor.iSerialNumber, mac_str, sizeof(mac_str)) == 12) { 751 0x12, 0xc0, 0x07, 0x00,
764 for (i = 0; i < 6; i++) 752 instance->scratch_buffer + OFFSET_7, SIZE_7, 500);
765 instance->u.atm_dev->esi[i] =
766 (hex2int(mac_str[i * 2]) * 16) + (hex2int(mac_str[i * 2 + 1]));
767 }
768 753
769 /* First check whether the modem already seems to be alive */ 754 *need_heavy_init = (ret != SIZE_7);
770 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
771 0x12, 0xc0, 0x07, 0x00, buf7, SIZE_7, 500);
772 755
773 if (ret == SIZE_7) { 756 usb_dbg(usbatm, "%s: firmware %s loaded\n", __func__, need_heavy_init ? "not" : "already");
774 dbg("firmware appears to be already loaded"); 757
775 speedtch_got_firmware(instance, 1); 758 if (*need_heavy_init)
776 speedtch_poll_status(instance); 759 if ((ret = usb_reset_device(usb_dev)) < 0)
777 } else { 760 goto fail_free;
778 speedtch_firmware_start(instance);
779 }
780 761
781 usb_set_intfdata(intf, instance); 762 usbatm->driver_data = instance;
782 763
783 return 0; 764 return 0;
784 765
785 fail: 766fail_free:
767 usb_free_urb(instance->int_urb);
786 kfree(instance); 768 kfree(instance);
787 769fail_release:
788 return -ENOMEM; 770 speedtch_release_interfaces(usb_dev, num_interfaces);
771 return ret;
789} 772}
790 773
791static void speedtch_usb_disconnect(struct usb_interface *intf) 774static void speedtch_unbind(struct usbatm_data *usbatm, struct usb_interface *intf)
792{ 775{
793 struct speedtch_instance_data *instance = usb_get_intfdata(intf); 776 struct usb_device *usb_dev = interface_to_usbdev(intf);
794 777 struct speedtch_instance_data *instance = usbatm->driver_data;
795 dbg("speedtch_usb_disconnect entered");
796
797 if (!instance) {
798 dbg("speedtch_usb_disconnect: NULL instance!");
799 return;
800 }
801 778
802/*QQ need to handle disconnects on interface #2 while uploading firmware */ 779 usb_dbg(usbatm, "%s entered\n", __func__);
803/*QQ and what about interface #1? */
804
805 if (instance->int_urb) {
806 struct urb *int_urb = instance->int_urb;
807 instance->int_urb = NULL;
808 wmb();
809 usb_unlink_urb(int_urb);
810 usb_free_urb(int_urb);
811 }
812 780
813 instance->int_data[0] = 1; 781 speedtch_release_interfaces(usb_dev, usb_dev->actconfig->desc.bNumInterfaces);
814 del_timer_sync(&instance->poll_timer); 782 usb_free_urb(instance->int_urb);
815 wmb(); 783 kfree(instance);
816 flush_scheduled_work();
817
818 udsl_instance_disconnect(&instance->u);
819
820 /* clean up */
821 usb_set_intfdata(intf, NULL);
822 udsl_put_instance(&instance->u);
823} 784}
824 785
786
825/*********** 787/***********
826** init ** 788** init **
827***********/ 789***********/
828 790
791static struct usbatm_driver speedtch_usbatm_driver = {
792 .owner = THIS_MODULE,
793 .driver_name = speedtch_driver_name,
794 .bind = speedtch_bind,
795 .heavy_init = speedtch_heavy_init,
796 .unbind = speedtch_unbind,
797 .atm_start = speedtch_atm_start,
798 .atm_stop = speedtch_atm_stop,
799 .in = ENDPOINT_DATA,
800 .out = ENDPOINT_DATA
801};
802
803static int speedtch_usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
804{
805 return usbatm_usb_probe(intf, id, &speedtch_usbatm_driver);
806}
807
829static int __init speedtch_usb_init(void) 808static int __init speedtch_usb_init(void)
830{ 809{
831 dbg("speedtch_usb_init: driver version " DRIVER_VERSION); 810 dbg("%s: driver version %s", __func__, DRIVER_VERSION);
832 811
833 return usb_register(&speedtch_usb_driver); 812 return usb_register(&speedtch_usb_driver);
834} 813}
835 814
836static void __exit speedtch_usb_cleanup(void) 815static void __exit speedtch_usb_cleanup(void)
837{ 816{
838 dbg("speedtch_usb_cleanup entered"); 817 dbg("%s", __func__);
839 818
840 usb_deregister(&speedtch_usb_driver); 819 usb_deregister(&speedtch_usb_driver);
841} 820}
diff --git a/drivers/usb/atm/usb_atm.c b/drivers/usb/atm/usb_atm.c
deleted file mode 100644
index a4cd4476d49a..000000000000
--- a/drivers/usb/atm/usb_atm.c
+++ /dev/null
@@ -1,1188 +0,0 @@
1/******************************************************************************
2 * usb_atm.c - Generic USB xDSL driver core
3 *
4 * Copyright (C) 2001, Alcatel
5 * Copyright (C) 2003, Duncan Sands, SolNegro, Josep Comas
6 * Copyright (C) 2004, David Woodhouse
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 2 of the License, or (at your option)
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * more details.
17 *
18 * You should have received a copy of the GNU General Public License along with
19 * this program; if not, write to the Free Software Foundation, Inc., 59
20 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 *
22 ******************************************************************************/
23
24/*
25 * Written by Johan Verrept, maintained by Duncan Sands (duncan.sands@free.fr)
26 *
27 * 1.7+: - See the check-in logs
28 *
29 * 1.6: - No longer opens a connection if the firmware is not loaded
30 * - Added support for the speedtouch 330
31 * - Removed the limit on the number of devices
32 * - Module now autoloads on device plugin
33 * - Merged relevant parts of sarlib
34 * - Replaced the kernel thread with a tasklet
35 * - New packet transmission code
36 * - Changed proc file contents
37 * - Fixed all known SMP races
38 * - Many fixes and cleanups
39 * - Various fixes by Oliver Neukum (oliver@neukum.name)
40 *
41 * 1.5A: - Version for inclusion in 2.5 series kernel
42 * - Modifications by Richard Purdie (rpurdie@rpsys.net)
43 * - made compatible with kernel 2.5.6 onwards by changing
44 * udsl_usb_send_data_context->urb to a pointer and adding code
45 * to alloc and free it
46 * - remove_wait_queue() added to udsl_atm_processqueue_thread()
47 *
48 * 1.5: - fixed memory leak when atmsar_decode_aal5 returned NULL.
49 * (reported by stephen.robinson@zen.co.uk)
50 *
51 * 1.4: - changed the spin_lock() under interrupt to spin_lock_irqsave()
52 * - unlink all active send urbs of a vcc that is being closed.
53 *
54 * 1.3.1: - added the version number
55 *
56 * 1.3: - Added multiple send urb support
57 * - fixed memory leak and vcc->tx_inuse starvation bug
58 * when not enough memory left in vcc.
59 *
60 * 1.2: - Fixed race condition in udsl_usb_send_data()
61 * 1.1: - Turned off packet debugging
62 *
63 */
64
65#include <linux/module.h>
66#include <linux/moduleparam.h>
67#include <linux/kernel.h>
68#include <linux/sched.h>
69#include <linux/timer.h>
70#include <linux/errno.h>
71#include <linux/proc_fs.h>
72#include <linux/slab.h>
73#include <linux/wait.h>
74#include <linux/list.h>
75#include <asm/uaccess.h>
76#include <linux/smp_lock.h>
77#include <linux/interrupt.h>
78#include <linux/atm.h>
79#include <linux/atmdev.h>
80#include <linux/crc32.h>
81#include <linux/init.h>
82#include <linux/firmware.h>
83
84#include "usb_atm.h"
85
86#ifdef VERBOSE_DEBUG
87static int udsl_print_packet(const unsigned char *data, int len);
88#define PACKETDEBUG(arg...) udsl_print_packet (arg)
89#define vdbg(arg...) dbg (arg)
90#else
91#define PACKETDEBUG(arg...)
92#define vdbg(arg...)
93#endif
94
95#define DRIVER_AUTHOR "Johan Verrept, Duncan Sands <duncan.sands@free.fr>"
96#define DRIVER_VERSION "1.8"
97#define DRIVER_DESC "Generic USB ATM/DSL I/O, version " DRIVER_VERSION
98
99static unsigned int num_rcv_urbs = UDSL_DEFAULT_RCV_URBS;
100static unsigned int num_snd_urbs = UDSL_DEFAULT_SND_URBS;
101static unsigned int num_rcv_bufs = UDSL_DEFAULT_RCV_BUFS;
102static unsigned int num_snd_bufs = UDSL_DEFAULT_SND_BUFS;
103static unsigned int rcv_buf_size = UDSL_DEFAULT_RCV_BUF_SIZE;
104static unsigned int snd_buf_size = UDSL_DEFAULT_SND_BUF_SIZE;
105
106module_param(num_rcv_urbs, uint, 0444);
107MODULE_PARM_DESC(num_rcv_urbs,
108 "Number of urbs used for reception (range: 0-"
109 __MODULE_STRING(UDSL_MAX_RCV_URBS) ", default: "
110 __MODULE_STRING(UDSL_DEFAULT_RCV_URBS) ")");
111
112module_param(num_snd_urbs, uint, 0444);
113MODULE_PARM_DESC(num_snd_urbs,
114 "Number of urbs used for transmission (range: 0-"
115 __MODULE_STRING(UDSL_MAX_SND_URBS) ", default: "
116 __MODULE_STRING(UDSL_DEFAULT_SND_URBS) ")");
117
118module_param(num_rcv_bufs, uint, 0444);
119MODULE_PARM_DESC(num_rcv_bufs,
120 "Number of buffers used for reception (range: 0-"
121 __MODULE_STRING(UDSL_MAX_RCV_BUFS) ", default: "
122 __MODULE_STRING(UDSL_DEFAULT_RCV_BUFS) ")");
123
124module_param(num_snd_bufs, uint, 0444);
125MODULE_PARM_DESC(num_snd_bufs,
126 "Number of buffers used for transmission (range: 0-"
127 __MODULE_STRING(UDSL_MAX_SND_BUFS) ", default: "
128 __MODULE_STRING(UDSL_DEFAULT_SND_BUFS) ")");
129
130module_param(rcv_buf_size, uint, 0444);
131MODULE_PARM_DESC(rcv_buf_size,
132 "Size of the buffers used for reception (range: 0-"
133 __MODULE_STRING(UDSL_MAX_RCV_BUF_SIZE) ", default: "
134 __MODULE_STRING(UDSL_DEFAULT_RCV_BUF_SIZE) ")");
135
136module_param(snd_buf_size, uint, 0444);
137MODULE_PARM_DESC(snd_buf_size,
138 "Size of the buffers used for transmission (range: 0-"
139 __MODULE_STRING(UDSL_MAX_SND_BUF_SIZE) ", default: "
140 __MODULE_STRING(UDSL_DEFAULT_SND_BUF_SIZE) ")");
141
142/* ATM */
143
144static void udsl_atm_dev_close(struct atm_dev *dev);
145static int udsl_atm_open(struct atm_vcc *vcc);
146static void udsl_atm_close(struct atm_vcc *vcc);
147static int udsl_atm_ioctl(struct atm_dev *dev, unsigned int cmd, void __user * arg);
148static int udsl_atm_send(struct atm_vcc *vcc, struct sk_buff *skb);
149static int udsl_atm_proc_read(struct atm_dev *atm_dev, loff_t * pos, char *page);
150
151static struct atmdev_ops udsl_atm_devops = {
152 .dev_close = udsl_atm_dev_close,
153 .open = udsl_atm_open,
154 .close = udsl_atm_close,
155 .ioctl = udsl_atm_ioctl,
156 .send = udsl_atm_send,
157 .proc_read = udsl_atm_proc_read,
158 .owner = THIS_MODULE,
159};
160
161/***********
162** misc **
163***********/
164
165static inline void udsl_pop(struct atm_vcc *vcc, struct sk_buff *skb)
166{
167 if (vcc->pop)
168 vcc->pop(vcc, skb);
169 else
170 dev_kfree_skb(skb);
171}
172
173/*************
174** decode **
175*************/
176
177static inline struct udsl_vcc_data *udsl_find_vcc(struct udsl_instance_data *instance,
178 short vpi, int vci)
179{
180 struct udsl_vcc_data *vcc;
181
182 list_for_each_entry(vcc, &instance->vcc_list, list)
183 if ((vcc->vci == vci) && (vcc->vpi == vpi))
184 return vcc;
185 return NULL;
186}
187
188static void udsl_extract_cells(struct udsl_instance_data *instance,
189 unsigned char *source, unsigned int howmany)
190{
191 struct udsl_vcc_data *cached_vcc = NULL;
192 struct atm_vcc *vcc;
193 struct sk_buff *sarb;
194 struct udsl_vcc_data *vcc_data;
195 int cached_vci = 0;
196 unsigned int i;
197 int pti;
198 int vci;
199 short cached_vpi = 0;
200 short vpi;
201
202 for (i = 0; i < howmany;
203 i++, source += ATM_CELL_SIZE + instance->rcv_padding) {
204 vpi = ((source[0] & 0x0f) << 4) | (source[1] >> 4);
205 vci = ((source[1] & 0x0f) << 12) | (source[2] << 4) | (source[3] >> 4);
206 pti = (source[3] & 0x2) != 0;
207
208 vdbg("udsl_extract_cells: vpi %hd, vci %d, pti %d", vpi, vci, pti);
209
210 if (cached_vcc && (vci == cached_vci) && (vpi == cached_vpi))
211 vcc_data = cached_vcc;
212 else if ((vcc_data = udsl_find_vcc(instance, vpi, vci))) {
213 cached_vcc = vcc_data;
214 cached_vpi = vpi;
215 cached_vci = vci;
216 } else {
217 dbg("udsl_extract_cells: unknown vpi/vci (%hd/%d)!", vpi, vci);
218 continue;
219 }
220
221 vcc = vcc_data->vcc;
222 sarb = vcc_data->sarb;
223
224 if (sarb->tail + ATM_CELL_PAYLOAD > sarb->end) {
225 dbg("udsl_extract_cells: buffer overrun (sarb->len %u, vcc: 0x%p)!", sarb->len, vcc);
226 /* discard cells already received */
227 skb_trim(sarb, 0);
228 }
229
230 memcpy(sarb->tail, source + ATM_CELL_HEADER, ATM_CELL_PAYLOAD);
231 __skb_put(sarb, ATM_CELL_PAYLOAD);
232
233 if (pti) {
234 struct sk_buff *skb;
235 unsigned int length;
236 unsigned int pdu_length;
237
238 length = (source[ATM_CELL_SIZE - 6] << 8) + source[ATM_CELL_SIZE - 5];
239
240 /* guard against overflow */
241 if (length > ATM_MAX_AAL5_PDU) {
242 dbg("udsl_extract_cells: bogus length %u (vcc: 0x%p)!", length, vcc);
243 atomic_inc(&vcc->stats->rx_err);
244 goto out;
245 }
246
247 pdu_length = UDSL_NUM_CELLS(length) * ATM_CELL_PAYLOAD;
248
249 if (sarb->len < pdu_length) {
250 dbg("udsl_extract_cells: bogus pdu_length %u (sarb->len: %u, vcc: 0x%p)!", pdu_length, sarb->len, vcc);
251 atomic_inc(&vcc->stats->rx_err);
252 goto out;
253 }
254
255 if (crc32_be(~0, sarb->tail - pdu_length, pdu_length) != 0xc704dd7b) {
256 dbg("udsl_extract_cells: packet failed crc check (vcc: 0x%p)!", vcc);
257 atomic_inc(&vcc->stats->rx_err);
258 goto out;
259 }
260
261 vdbg("udsl_extract_cells: got packet (length: %u, pdu_length: %u, vcc: 0x%p)", length, pdu_length, vcc);
262
263 if (!(skb = dev_alloc_skb(length))) {
264 dbg("udsl_extract_cells: no memory for skb (length: %u)!", length);
265 atomic_inc(&vcc->stats->rx_drop);
266 goto out;
267 }
268
269 vdbg("udsl_extract_cells: allocated new sk_buff (skb: 0x%p, skb->truesize: %u)", skb, skb->truesize);
270
271 if (!atm_charge(vcc, skb->truesize)) {
272 dbg("udsl_extract_cells: failed atm_charge (skb->truesize: %u)!", skb->truesize);
273 dev_kfree_skb(skb);
274 goto out; /* atm_charge increments rx_drop */
275 }
276
277 memcpy(skb->data, sarb->tail - pdu_length, length);
278 __skb_put(skb, length);
279
280 vdbg("udsl_extract_cells: sending skb 0x%p, skb->len %u, skb->truesize %u", skb, skb->len, skb->truesize);
281
282 PACKETDEBUG(skb->data, skb->len);
283
284 vcc->push(vcc, skb);
285
286 atomic_inc(&vcc->stats->rx);
287 out:
288 skb_trim(sarb, 0);
289 }
290 }
291}
292
293/*************
294** encode **
295*************/
296
297static inline void udsl_fill_cell_header(unsigned char *target, struct atm_vcc *vcc)
298{
299 target[0] = vcc->vpi >> 4;
300 target[1] = (vcc->vpi << 4) | (vcc->vci >> 12);
301 target[2] = vcc->vci >> 4;
302 target[3] = vcc->vci << 4;
303 target[4] = 0xec;
304}
305
306static const unsigned char zeros[ATM_CELL_PAYLOAD];
307
308static void udsl_groom_skb(struct atm_vcc *vcc, struct sk_buff *skb)
309{
310 struct udsl_control *ctrl = UDSL_SKB(skb);
311 unsigned int zero_padding;
312 u32 crc;
313
314 ctrl->atm_data.vcc = vcc;
315
316 ctrl->num_cells = UDSL_NUM_CELLS(skb->len);
317 ctrl->num_entire = skb->len / ATM_CELL_PAYLOAD;
318
319 zero_padding = ctrl->num_cells * ATM_CELL_PAYLOAD - skb->len - ATM_AAL5_TRAILER;
320
321 if (ctrl->num_entire + 1 < ctrl->num_cells)
322 ctrl->pdu_padding = zero_padding - (ATM_CELL_PAYLOAD - ATM_AAL5_TRAILER);
323 else
324 ctrl->pdu_padding = zero_padding;
325
326 ctrl->aal5_trailer[0] = 0; /* UU = 0 */
327 ctrl->aal5_trailer[1] = 0; /* CPI = 0 */
328 ctrl->aal5_trailer[2] = skb->len >> 8;
329 ctrl->aal5_trailer[3] = skb->len;
330
331 crc = crc32_be(~0, skb->data, skb->len);
332 crc = crc32_be(crc, zeros, zero_padding);
333 crc = crc32_be(crc, ctrl->aal5_trailer, 4);
334 crc = ~crc;
335
336 ctrl->aal5_trailer[4] = crc >> 24;
337 ctrl->aal5_trailer[5] = crc >> 16;
338 ctrl->aal5_trailer[6] = crc >> 8;
339 ctrl->aal5_trailer[7] = crc;
340}
341
342static unsigned int udsl_write_cells(struct udsl_instance_data *instance,
343 unsigned int howmany, struct sk_buff *skb,
344 unsigned char **target_p)
345{
346 struct udsl_control *ctrl = UDSL_SKB(skb);
347 unsigned char *target = *target_p;
348 unsigned int nc, ne, i;
349
350 vdbg("udsl_write_cells: howmany=%u, skb->len=%d, num_cells=%u, num_entire=%u, pdu_padding=%u", howmany, skb->len, ctrl->num_cells, ctrl->num_entire, ctrl->pdu_padding);
351
352 nc = ctrl->num_cells;
353 ne = min(howmany, ctrl->num_entire);
354
355 for (i = 0; i < ne; i++) {
356 udsl_fill_cell_header(target, ctrl->atm_data.vcc);
357 target += ATM_CELL_HEADER;
358 memcpy(target, skb->data, ATM_CELL_PAYLOAD);
359 target += ATM_CELL_PAYLOAD;
360 if (instance->snd_padding) {
361 memset(target, 0, instance->snd_padding);
362 target += instance->snd_padding;
363 }
364 __skb_pull(skb, ATM_CELL_PAYLOAD);
365 }
366
367 ctrl->num_entire -= ne;
368
369 if (!(ctrl->num_cells -= ne) || !(howmany -= ne))
370 goto out;
371
372 udsl_fill_cell_header(target, ctrl->atm_data.vcc);
373 target += ATM_CELL_HEADER;
374 memcpy(target, skb->data, skb->len);
375 target += skb->len;
376 __skb_pull(skb, skb->len);
377 memset(target, 0, ctrl->pdu_padding);
378 target += ctrl->pdu_padding;
379
380 if (--ctrl->num_cells) {
381 if (!--howmany) {
382 ctrl->pdu_padding = ATM_CELL_PAYLOAD - ATM_AAL5_TRAILER;
383 goto out;
384 }
385
386 if (instance->snd_padding) {
387 memset(target, 0, instance->snd_padding);
388 target += instance->snd_padding;
389 }
390 udsl_fill_cell_header(target, ctrl->atm_data.vcc);
391 target += ATM_CELL_HEADER;
392 memset(target, 0, ATM_CELL_PAYLOAD - ATM_AAL5_TRAILER);
393 target += ATM_CELL_PAYLOAD - ATM_AAL5_TRAILER;
394
395 --ctrl->num_cells;
396 UDSL_ASSERT(!ctrl->num_cells);
397 }
398
399 memcpy(target, ctrl->aal5_trailer, ATM_AAL5_TRAILER);
400 target += ATM_AAL5_TRAILER;
401 /* set pti bit in last cell */
402 *(target + 3 - ATM_CELL_SIZE) |= 0x2;
403 if (instance->snd_padding) {
404 memset(target, 0, instance->snd_padding);
405 target += instance->snd_padding;
406 }
407 out:
408 *target_p = target;
409 return nc - ctrl->num_cells;
410}
411
412/**************
413** receive **
414**************/
415
416static void udsl_complete_receive(struct urb *urb, struct pt_regs *regs)
417{
418 struct udsl_receive_buffer *buf;
419 struct udsl_instance_data *instance;
420 struct udsl_receiver *rcv;
421 unsigned long flags;
422
423 if (!urb || !(rcv = urb->context)) {
424 dbg("udsl_complete_receive: bad urb!");
425 return;
426 }
427
428 instance = rcv->instance;
429 buf = rcv->buffer;
430
431 buf->filled_cells = urb->actual_length / (ATM_CELL_SIZE + instance->rcv_padding);
432
433 vdbg("udsl_complete_receive: urb 0x%p, status %d, actual_length %d, filled_cells %u, rcv 0x%p, buf 0x%p", urb, urb->status, urb->actual_length, buf->filled_cells, rcv, buf);
434
435 UDSL_ASSERT(buf->filled_cells <= rcv_buf_size);
436
437 /* may not be in_interrupt() */
438 spin_lock_irqsave(&instance->receive_lock, flags);
439 list_add(&rcv->list, &instance->spare_receivers);
440 list_add_tail(&buf->list, &instance->filled_receive_buffers);
441 if (likely(!urb->status))
442 tasklet_schedule(&instance->receive_tasklet);
443 spin_unlock_irqrestore(&instance->receive_lock, flags);
444}
445
446static void udsl_process_receive(unsigned long data)
447{
448 struct udsl_receive_buffer *buf;
449 struct udsl_instance_data *instance = (struct udsl_instance_data *)data;
450 struct udsl_receiver *rcv;
451 int err;
452
453 made_progress:
454 while (!list_empty(&instance->spare_receive_buffers)) {
455 spin_lock_irq(&instance->receive_lock);
456 if (list_empty(&instance->spare_receivers)) {
457 spin_unlock_irq(&instance->receive_lock);
458 break;
459 }
460 rcv = list_entry(instance->spare_receivers.next,
461 struct udsl_receiver, list);
462 list_del(&rcv->list);
463 spin_unlock_irq(&instance->receive_lock);
464
465 buf = list_entry(instance->spare_receive_buffers.next,
466 struct udsl_receive_buffer, list);
467 list_del(&buf->list);
468
469 rcv->buffer = buf;
470
471 usb_fill_bulk_urb(rcv->urb, instance->usb_dev,
472 usb_rcvbulkpipe(instance->usb_dev, instance->data_endpoint),
473 buf->base,
474 rcv_buf_size * (ATM_CELL_SIZE + instance->rcv_padding),
475 udsl_complete_receive, rcv);
476
477 vdbg("udsl_process_receive: sending urb 0x%p, rcv 0x%p, buf 0x%p",
478 rcv->urb, rcv, buf);
479
480 if ((err = usb_submit_urb(rcv->urb, GFP_ATOMIC)) < 0) {
481 dbg("udsl_process_receive: urb submission failed (%d)!", err);
482 list_add(&buf->list, &instance->spare_receive_buffers);
483 spin_lock_irq(&instance->receive_lock);
484 list_add(&rcv->list, &instance->spare_receivers);
485 spin_unlock_irq(&instance->receive_lock);
486 break;
487 }
488 }
489
490 spin_lock_irq(&instance->receive_lock);
491 if (list_empty(&instance->filled_receive_buffers)) {
492 spin_unlock_irq(&instance->receive_lock);
493 return; /* done - no more buffers */
494 }
495 buf = list_entry(instance->filled_receive_buffers.next,
496 struct udsl_receive_buffer, list);
497 list_del(&buf->list);
498 spin_unlock_irq(&instance->receive_lock);
499
500 vdbg("udsl_process_receive: processing buf 0x%p", buf);
501 udsl_extract_cells(instance, buf->base, buf->filled_cells);
502 list_add(&buf->list, &instance->spare_receive_buffers);
503 goto made_progress;
504}
505
506/***********
507** send **
508***********/
509
510static void udsl_complete_send(struct urb *urb, struct pt_regs *regs)
511{
512 struct udsl_instance_data *instance;
513 struct udsl_sender *snd;
514 unsigned long flags;
515
516 if (!urb || !(snd = urb->context) || !(instance = snd->instance)) {
517 dbg("udsl_complete_send: bad urb!");
518 return;
519 }
520
521 vdbg("udsl_complete_send: urb 0x%p, status %d, snd 0x%p, buf 0x%p", urb,
522 urb->status, snd, snd->buffer);
523
524 /* may not be in_interrupt() */
525 spin_lock_irqsave(&instance->send_lock, flags);
526 list_add(&snd->list, &instance->spare_senders);
527 list_add(&snd->buffer->list, &instance->spare_send_buffers);
528 tasklet_schedule(&instance->send_tasklet);
529 spin_unlock_irqrestore(&instance->send_lock, flags);
530}
531
532static void udsl_process_send(unsigned long data)
533{
534 struct udsl_send_buffer *buf;
535 struct udsl_instance_data *instance = (struct udsl_instance_data *)data;
536 struct sk_buff *skb;
537 struct udsl_sender *snd;
538 int err;
539 unsigned int num_written;
540
541 made_progress:
542 spin_lock_irq(&instance->send_lock);
543 while (!list_empty(&instance->spare_senders)) {
544 if (!list_empty(&instance->filled_send_buffers)) {
545 buf = list_entry(instance->filled_send_buffers.next,
546 struct udsl_send_buffer, list);
547 list_del(&buf->list);
548 } else if ((buf = instance->current_buffer)) {
549 instance->current_buffer = NULL;
550 } else /* all buffers empty */
551 break;
552
553 snd = list_entry(instance->spare_senders.next,
554 struct udsl_sender, list);
555 list_del(&snd->list);
556 spin_unlock_irq(&instance->send_lock);
557
558 snd->buffer = buf;
559 usb_fill_bulk_urb(snd->urb, instance->usb_dev,
560 usb_sndbulkpipe(instance->usb_dev, instance->data_endpoint),
561 buf->base,
562 (snd_buf_size - buf->free_cells) * (ATM_CELL_SIZE + instance->snd_padding),
563 udsl_complete_send, snd);
564
565 vdbg("udsl_process_send: submitting urb 0x%p (%d cells), snd 0x%p, buf 0x%p",
566 snd->urb, snd_buf_size - buf->free_cells, snd, buf);
567
568 if ((err = usb_submit_urb(snd->urb, GFP_ATOMIC)) < 0) {
569 dbg("udsl_process_send: urb submission failed (%d)!", err);
570 spin_lock_irq(&instance->send_lock);
571 list_add(&snd->list, &instance->spare_senders);
572 spin_unlock_irq(&instance->send_lock);
573 list_add(&buf->list, &instance->filled_send_buffers);
574 return; /* bail out */
575 }
576
577 spin_lock_irq(&instance->send_lock);
578 } /* while */
579 spin_unlock_irq(&instance->send_lock);
580
581 if (!instance->current_skb)
582 instance->current_skb = skb_dequeue(&instance->sndqueue);
583 if (!instance->current_skb)
584 return; /* done - no more skbs */
585
586 skb = instance->current_skb;
587
588 if (!(buf = instance->current_buffer)) {
589 spin_lock_irq(&instance->send_lock);
590 if (list_empty(&instance->spare_send_buffers)) {
591 instance->current_buffer = NULL;
592 spin_unlock_irq(&instance->send_lock);
593 return; /* done - no more buffers */
594 }
595 buf = list_entry(instance->spare_send_buffers.next,
596 struct udsl_send_buffer, list);
597 list_del(&buf->list);
598 spin_unlock_irq(&instance->send_lock);
599
600 buf->free_start = buf->base;
601 buf->free_cells = snd_buf_size;
602
603 instance->current_buffer = buf;
604 }
605
606 num_written = udsl_write_cells(instance, buf->free_cells, skb, &buf->free_start);
607
608 vdbg("udsl_process_send: wrote %u cells from skb 0x%p to buffer 0x%p",
609 num_written, skb, buf);
610
611 if (!(buf->free_cells -= num_written)) {
612 list_add_tail(&buf->list, &instance->filled_send_buffers);
613 instance->current_buffer = NULL;
614 }
615
616 vdbg("udsl_process_send: buffer contains %d cells, %d left",
617 snd_buf_size - buf->free_cells, buf->free_cells);
618
619 if (!UDSL_SKB(skb)->num_cells) {
620 struct atm_vcc *vcc = UDSL_SKB(skb)->atm_data.vcc;
621
622 udsl_pop(vcc, skb);
623 instance->current_skb = NULL;
624
625 atomic_inc(&vcc->stats->tx);
626 }
627
628 goto made_progress;
629}
630
631static void udsl_cancel_send(struct udsl_instance_data *instance,
632 struct atm_vcc *vcc)
633{
634 struct sk_buff *skb, *n;
635
636 dbg("udsl_cancel_send entered");
637 spin_lock_irq(&instance->sndqueue.lock);
638 for (skb = instance->sndqueue.next, n = skb->next;
639 skb != (struct sk_buff *)&instance->sndqueue;
640 skb = n, n = skb->next)
641 if (UDSL_SKB(skb)->atm_data.vcc == vcc) {
642 dbg("udsl_cancel_send: popping skb 0x%p", skb);
643 __skb_unlink(skb, &instance->sndqueue);
644 udsl_pop(vcc, skb);
645 }
646 spin_unlock_irq(&instance->sndqueue.lock);
647
648 tasklet_disable(&instance->send_tasklet);
649 if ((skb = instance->current_skb) && (UDSL_SKB(skb)->atm_data.vcc == vcc)) {
650 dbg("udsl_cancel_send: popping current skb (0x%p)", skb);
651 instance->current_skb = NULL;
652 udsl_pop(vcc, skb);
653 }
654 tasklet_enable(&instance->send_tasklet);
655 dbg("udsl_cancel_send done");
656}
657
658static int udsl_atm_send(struct atm_vcc *vcc, struct sk_buff *skb)
659{
660 struct udsl_instance_data *instance = vcc->dev->dev_data;
661 int err;
662
663 vdbg("udsl_atm_send called (skb 0x%p, len %u)", skb, skb->len);
664
665 if (!instance) {
666 dbg("udsl_atm_send: NULL data!");
667 err = -ENODEV;
668 goto fail;
669 }
670
671 if (vcc->qos.aal != ATM_AAL5) {
672 dbg("udsl_atm_send: unsupported ATM type %d!", vcc->qos.aal);
673 err = -EINVAL;
674 goto fail;
675 }
676
677 if (skb->len > ATM_MAX_AAL5_PDU) {
678 dbg("udsl_atm_send: packet too long (%d vs %d)!", skb->len,
679 ATM_MAX_AAL5_PDU);
680 err = -EINVAL;
681 goto fail;
682 }
683
684 PACKETDEBUG(skb->data, skb->len);
685
686 udsl_groom_skb(vcc, skb);
687 skb_queue_tail(&instance->sndqueue, skb);
688 tasklet_schedule(&instance->send_tasklet);
689
690 return 0;
691
692 fail:
693 udsl_pop(vcc, skb);
694 return err;
695}
696
697/********************
698** bean counting **
699********************/
700
701static void udsl_destroy_instance(struct kref *kref)
702{
703 struct udsl_instance_data *instance =
704 container_of(kref, struct udsl_instance_data, refcount);
705
706 tasklet_kill(&instance->receive_tasklet);
707 tasklet_kill(&instance->send_tasklet);
708 usb_put_dev(instance->usb_dev);
709 kfree(instance);
710}
711
712void udsl_get_instance(struct udsl_instance_data *instance)
713{
714 kref_get(&instance->refcount);
715}
716
717void udsl_put_instance(struct udsl_instance_data *instance)
718{
719 kref_put(&instance->refcount, udsl_destroy_instance);
720}
721
722/**********
723** ATM **
724**********/
725
726static void udsl_atm_dev_close(struct atm_dev *dev)
727{
728 struct udsl_instance_data *instance = dev->dev_data;
729
730 dev->dev_data = NULL;
731 udsl_put_instance(instance);
732}
733
734static int udsl_atm_proc_read(struct atm_dev *atm_dev, loff_t * pos, char *page)
735{
736 struct udsl_instance_data *instance = atm_dev->dev_data;
737 int left = *pos;
738
739 if (!instance) {
740 dbg("udsl_atm_proc_read: NULL instance!");
741 return -ENODEV;
742 }
743
744 if (!left--)
745 return sprintf(page, "%s\n", instance->description);
746
747 if (!left--)
748 return sprintf(page, "MAC: %02x:%02x:%02x:%02x:%02x:%02x\n",
749 atm_dev->esi[0], atm_dev->esi[1],
750 atm_dev->esi[2], atm_dev->esi[3],
751 atm_dev->esi[4], atm_dev->esi[5]);
752
753 if (!left--)
754 return sprintf(page,
755 "AAL5: tx %d ( %d err ), rx %d ( %d err, %d drop )\n",
756 atomic_read(&atm_dev->stats.aal5.tx),
757 atomic_read(&atm_dev->stats.aal5.tx_err),
758 atomic_read(&atm_dev->stats.aal5.rx),
759 atomic_read(&atm_dev->stats.aal5.rx_err),
760 atomic_read(&atm_dev->stats.aal5.rx_drop));
761
762 if (!left--) {
763 switch (atm_dev->signal) {
764 case ATM_PHY_SIG_FOUND:
765 sprintf(page, "Line up");
766 break;
767 case ATM_PHY_SIG_LOST:
768 sprintf(page, "Line down");
769 break;
770 default:
771 sprintf(page, "Line state unknown");
772 break;
773 }
774
775 if (instance->usb_dev->state == USB_STATE_NOTATTACHED)
776 strcat(page, ", disconnected\n");
777 else {
778 if (instance->status == UDSL_LOADED_FIRMWARE)
779 strcat(page, ", firmware loaded\n");
780 else if (instance->status == UDSL_LOADING_FIRMWARE)
781 strcat(page, ", firmware loading\n");
782 else
783 strcat(page, ", no firmware\n");
784 }
785
786 return strlen(page);
787 }
788
789 return 0;
790}
791
792static int udsl_atm_open(struct atm_vcc *vcc)
793{
794 struct udsl_instance_data *instance = vcc->dev->dev_data;
795 struct udsl_vcc_data *new;
796 unsigned int max_pdu;
797 int vci = vcc->vci;
798 short vpi = vcc->vpi;
799 int err;
800
801 dbg("udsl_atm_open: vpi %hd, vci %d", vpi, vci);
802
803 if (!instance) {
804 dbg("udsl_atm_open: NULL data!");
805 return -ENODEV;
806 }
807
808 /* only support AAL5 */
809 if ((vcc->qos.aal != ATM_AAL5) || (vcc->qos.rxtp.max_sdu < 0)
810 || (vcc->qos.rxtp.max_sdu > ATM_MAX_AAL5_PDU)) {
811 dbg("udsl_atm_open: unsupported ATM type %d!", vcc->qos.aal);
812 return -EINVAL;
813 }
814
815 if (instance->firmware_wait &&
816 (err = instance->firmware_wait(instance)) < 0) {
817 dbg("udsl_atm_open: firmware not loaded (%d)!", err);
818 return err;
819 }
820
821 down(&instance->serialize); /* vs self, udsl_atm_close */
822
823 if (udsl_find_vcc(instance, vpi, vci)) {
824 dbg("udsl_atm_open: %hd/%d already in use!", vpi, vci);
825 up(&instance->serialize);
826 return -EADDRINUSE;
827 }
828
829 if (!(new = kmalloc(sizeof(struct udsl_vcc_data), GFP_KERNEL))) {
830 dbg("udsl_atm_open: no memory for vcc_data!");
831 up(&instance->serialize);
832 return -ENOMEM;
833 }
834
835 memset(new, 0, sizeof(struct udsl_vcc_data));
836 new->vcc = vcc;
837 new->vpi = vpi;
838 new->vci = vci;
839
840 /* udsl_extract_cells requires at least one cell */
841 max_pdu = max(1, UDSL_NUM_CELLS(vcc->qos.rxtp.max_sdu)) * ATM_CELL_PAYLOAD;
842 if (!(new->sarb = alloc_skb(max_pdu, GFP_KERNEL))) {
843 dbg("udsl_atm_open: no memory for SAR buffer!");
844 kfree(new);
845 up(&instance->serialize);
846 return -ENOMEM;
847 }
848
849 vcc->dev_data = new;
850
851 tasklet_disable(&instance->receive_tasklet);
852 list_add(&new->list, &instance->vcc_list);
853 tasklet_enable(&instance->receive_tasklet);
854
855 set_bit(ATM_VF_ADDR, &vcc->flags);
856 set_bit(ATM_VF_PARTIAL, &vcc->flags);
857 set_bit(ATM_VF_READY, &vcc->flags);
858
859 up(&instance->serialize);
860
861 tasklet_schedule(&instance->receive_tasklet);
862
863 dbg("udsl_atm_open: allocated vcc data 0x%p (max_pdu: %u)", new, max_pdu);
864
865 return 0;
866}
867
868static void udsl_atm_close(struct atm_vcc *vcc)
869{
870 struct udsl_instance_data *instance = vcc->dev->dev_data;
871 struct udsl_vcc_data *vcc_data = vcc->dev_data;
872
873 dbg("udsl_atm_close called");
874
875 if (!instance || !vcc_data) {
876 dbg("udsl_atm_close: NULL data!");
877 return;
878 }
879
880 dbg("udsl_atm_close: deallocating vcc 0x%p with vpi %d vci %d",
881 vcc_data, vcc_data->vpi, vcc_data->vci);
882
883 udsl_cancel_send(instance, vcc);
884
885 down(&instance->serialize); /* vs self, udsl_atm_open */
886
887 tasklet_disable(&instance->receive_tasklet);
888 list_del(&vcc_data->list);
889 tasklet_enable(&instance->receive_tasklet);
890
891 kfree_skb(vcc_data->sarb);
892 vcc_data->sarb = NULL;
893
894 kfree(vcc_data);
895 vcc->dev_data = NULL;
896
897 vcc->vpi = ATM_VPI_UNSPEC;
898 vcc->vci = ATM_VCI_UNSPEC;
899 clear_bit(ATM_VF_READY, &vcc->flags);
900 clear_bit(ATM_VF_PARTIAL, &vcc->flags);
901 clear_bit(ATM_VF_ADDR, &vcc->flags);
902
903 up(&instance->serialize);
904
905 dbg("udsl_atm_close successful");
906}
907
908static int udsl_atm_ioctl(struct atm_dev *dev, unsigned int cmd,
909 void __user * arg)
910{
911 switch (cmd) {
912 case ATM_QUERYLOOP:
913 return put_user(ATM_LM_NONE, (int __user *)arg) ? -EFAULT : 0;
914 default:
915 return -ENOIOCTLCMD;
916 }
917}
918
919/**********
920** USB **
921**********/
922
923int udsl_instance_setup(struct usb_device *dev,
924 struct udsl_instance_data *instance)
925{
926 char *buf;
927 int i, length;
928
929 kref_init(&instance->refcount); /* one for USB */
930 udsl_get_instance(instance); /* one for ATM */
931
932 init_MUTEX(&instance->serialize);
933
934 instance->usb_dev = dev;
935
936 INIT_LIST_HEAD(&instance->vcc_list);
937
938 instance->status = UDSL_NO_FIRMWARE;
939 init_waitqueue_head(&instance->firmware_waiters);
940
941 spin_lock_init(&instance->receive_lock);
942 INIT_LIST_HEAD(&instance->spare_receivers);
943 INIT_LIST_HEAD(&instance->filled_receive_buffers);
944
945 tasklet_init(&instance->receive_tasklet, udsl_process_receive, (unsigned long)instance);
946 INIT_LIST_HEAD(&instance->spare_receive_buffers);
947
948 skb_queue_head_init(&instance->sndqueue);
949
950 spin_lock_init(&instance->send_lock);
951 INIT_LIST_HEAD(&instance->spare_senders);
952 INIT_LIST_HEAD(&instance->spare_send_buffers);
953
954 tasklet_init(&instance->send_tasklet, udsl_process_send,
955 (unsigned long)instance);
956 INIT_LIST_HEAD(&instance->filled_send_buffers);
957
958 /* receive init */
959 for (i = 0; i < num_rcv_urbs; i++) {
960 struct udsl_receiver *rcv = &(instance->receivers[i]);
961
962 if (!(rcv->urb = usb_alloc_urb(0, GFP_KERNEL))) {
963 dbg("udsl_usb_probe: no memory for receive urb %d!", i);
964 goto fail;
965 }
966
967 rcv->instance = instance;
968
969 list_add(&rcv->list, &instance->spare_receivers);
970 }
971
972 for (i = 0; i < num_rcv_bufs; i++) {
973 struct udsl_receive_buffer *buf =
974 &(instance->receive_buffers[i]);
975
976 buf->base = kmalloc(rcv_buf_size * (ATM_CELL_SIZE + instance->rcv_padding),
977 GFP_KERNEL);
978 if (!buf->base) {
979 dbg("udsl_usb_probe: no memory for receive buffer %d!", i);
980 goto fail;
981 }
982
983 list_add(&buf->list, &instance->spare_receive_buffers);
984 }
985
986 /* send init */
987 for (i = 0; i < num_snd_urbs; i++) {
988 struct udsl_sender *snd = &(instance->senders[i]);
989
990 if (!(snd->urb = usb_alloc_urb(0, GFP_KERNEL))) {
991 dbg("udsl_usb_probe: no memory for send urb %d!", i);
992 goto fail;
993 }
994
995 snd->instance = instance;
996
997 list_add(&snd->list, &instance->spare_senders);
998 }
999
1000 for (i = 0; i < num_snd_bufs; i++) {
1001 struct udsl_send_buffer *buf = &(instance->send_buffers[i]);
1002
1003 buf->base = kmalloc(snd_buf_size * (ATM_CELL_SIZE + instance->snd_padding),
1004 GFP_KERNEL);
1005 if (!buf->base) {
1006 dbg("udsl_usb_probe: no memory for send buffer %d!", i);
1007 goto fail;
1008 }
1009
1010 list_add(&buf->list, &instance->spare_send_buffers);
1011 }
1012
1013 /* ATM init */
1014 instance->atm_dev = atm_dev_register(instance->driver_name,
1015 &udsl_atm_devops, -1, NULL);
1016 if (!instance->atm_dev) {
1017 dbg("udsl_usb_probe: failed to register ATM device!");
1018 goto fail;
1019 }
1020
1021 instance->atm_dev->ci_range.vpi_bits = ATM_CI_MAX;
1022 instance->atm_dev->ci_range.vci_bits = ATM_CI_MAX;
1023 instance->atm_dev->signal = ATM_PHY_SIG_UNKNOWN;
1024
1025 /* temp init ATM device, set to 128kbit */
1026 instance->atm_dev->link_rate = 128 * 1000 / 424;
1027
1028 /* device description */
1029 buf = instance->description;
1030 length = sizeof(instance->description);
1031
1032 if ((i = usb_string(dev, dev->descriptor.iProduct, buf, length)) < 0)
1033 goto finish;
1034
1035 buf += i;
1036 length -= i;
1037
1038 i = scnprintf(buf, length, " (");
1039 buf += i;
1040 length -= i;
1041
1042 if (length <= 0 || (i = usb_make_path(dev, buf, length)) < 0)
1043 goto finish;
1044
1045 buf += i;
1046 length -= i;
1047
1048 snprintf(buf, length, ")");
1049
1050 finish:
1051 /* ready for ATM callbacks */
1052 wmb();
1053 instance->atm_dev->dev_data = instance;
1054
1055 usb_get_dev(dev);
1056
1057 return 0;
1058
1059 fail:
1060 for (i = 0; i < num_snd_bufs; i++)
1061 kfree(instance->send_buffers[i].base);
1062
1063 for (i = 0; i < num_snd_urbs; i++)
1064 usb_free_urb(instance->senders[i].urb);
1065
1066 for (i = 0; i < num_rcv_bufs; i++)
1067 kfree(instance->receive_buffers[i].base);
1068
1069 for (i = 0; i < num_rcv_urbs; i++)
1070 usb_free_urb(instance->receivers[i].urb);
1071
1072 return -ENOMEM;
1073}
1074
1075void udsl_instance_disconnect(struct udsl_instance_data *instance)
1076{
1077 int i;
1078
1079 dbg("udsl_instance_disconnect entered");
1080
1081 if (!instance) {
1082 dbg("udsl_instance_disconnect: NULL instance!");
1083 return;
1084 }
1085
1086 /* receive finalize */
1087 tasklet_disable(&instance->receive_tasklet);
1088
1089 for (i = 0; i < num_rcv_urbs; i++)
1090 usb_kill_urb(instance->receivers[i].urb);
1091
1092 /* no need to take the spinlock */
1093 INIT_LIST_HEAD(&instance->filled_receive_buffers);
1094 INIT_LIST_HEAD(&instance->spare_receive_buffers);
1095
1096 tasklet_enable(&instance->receive_tasklet);
1097
1098 for (i = 0; i < num_rcv_urbs; i++)
1099 usb_free_urb(instance->receivers[i].urb);
1100
1101 for (i = 0; i < num_rcv_bufs; i++)
1102 kfree(instance->receive_buffers[i].base);
1103
1104 /* send finalize */
1105 tasklet_disable(&instance->send_tasklet);
1106
1107 for (i = 0; i < num_snd_urbs; i++)
1108 usb_kill_urb(instance->senders[i].urb);
1109
1110 /* no need to take the spinlock */
1111 INIT_LIST_HEAD(&instance->spare_senders);
1112 INIT_LIST_HEAD(&instance->spare_send_buffers);
1113 instance->current_buffer = NULL;
1114
1115 tasklet_enable(&instance->send_tasklet);
1116
1117 for (i = 0; i < num_snd_urbs; i++)
1118 usb_free_urb(instance->senders[i].urb);
1119
1120 for (i = 0; i < num_snd_bufs; i++)
1121 kfree(instance->send_buffers[i].base);
1122
1123 /* ATM finalize */
1124 shutdown_atm_dev(instance->atm_dev);
1125}
1126
1127EXPORT_SYMBOL_GPL(udsl_get_instance);
1128EXPORT_SYMBOL_GPL(udsl_put_instance);
1129EXPORT_SYMBOL_GPL(udsl_instance_setup);
1130EXPORT_SYMBOL_GPL(udsl_instance_disconnect);
1131
1132/***********
1133** init **
1134***********/
1135
1136static int __init udsl_usb_init(void)
1137{
1138 dbg("udsl_usb_init: driver version " DRIVER_VERSION);
1139
1140 if (sizeof(struct udsl_control) > sizeof(((struct sk_buff *) 0)->cb)) {
1141 printk(KERN_ERR __FILE__ ": unusable with this kernel!\n");
1142 return -EIO;
1143 }
1144
1145 if ((num_rcv_urbs > UDSL_MAX_RCV_URBS)
1146 || (num_snd_urbs > UDSL_MAX_SND_URBS)
1147 || (num_rcv_bufs > UDSL_MAX_RCV_BUFS)
1148 || (num_snd_bufs > UDSL_MAX_SND_BUFS)
1149 || (rcv_buf_size > UDSL_MAX_RCV_BUF_SIZE)
1150 || (snd_buf_size > UDSL_MAX_SND_BUF_SIZE))
1151 return -EINVAL;
1152
1153 return 0;
1154}
1155
1156static void __exit udsl_usb_exit(void)
1157{
1158}
1159
1160module_init(udsl_usb_init);
1161module_exit(udsl_usb_exit);
1162
1163MODULE_AUTHOR(DRIVER_AUTHOR);
1164MODULE_DESCRIPTION(DRIVER_DESC);
1165MODULE_LICENSE("GPL");
1166MODULE_VERSION(DRIVER_VERSION);
1167
1168/************
1169** debug **
1170************/
1171
1172#ifdef VERBOSE_DEBUG
1173static int udsl_print_packet(const unsigned char *data, int len)
1174{
1175 unsigned char buffer[256];
1176 int i = 0, j = 0;
1177
1178 for (i = 0; i < len;) {
1179 buffer[0] = '\0';
1180 sprintf(buffer, "%.3d :", i);
1181 for (j = 0; (j < 16) && (i < len); j++, i++) {
1182 sprintf(buffer, "%s %2.2x", buffer, data[i]);
1183 }
1184 dbg("%s", buffer);
1185 }
1186 return i;
1187}
1188#endif
diff --git a/drivers/usb/atm/usb_atm.h b/drivers/usb/atm/usb_atm.h
deleted file mode 100644
index cf8c53283530..000000000000
--- a/drivers/usb/atm/usb_atm.h
+++ /dev/null
@@ -1,176 +0,0 @@
1/******************************************************************************
2 * usb_atm.h - Generic USB xDSL driver core
3 *
4 * Copyright (C) 2001, Alcatel
5 * Copyright (C) 2003, Duncan Sands, SolNegro, Josep Comas
6 * Copyright (C) 2004, David Woodhouse
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 2 of the License, or (at your option)
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * more details.
17 *
18 * You should have received a copy of the GNU General Public License along with
19 * this program; if not, write to the Free Software Foundation, Inc., 59
20 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 *
22 ******************************************************************************/
23
24#include <linux/config.h>
25#include <linux/list.h>
26#include <linux/kref.h>
27#include <linux/atm.h>
28#include <linux/atmdev.h>
29#include <asm/semaphore.h>
30
31/*
32#define DEBUG
33#define VERBOSE_DEBUG
34*/
35
36#if !defined (DEBUG) && defined (CONFIG_USB_DEBUG)
37# define DEBUG
38#endif
39
40#include <linux/usb.h>
41
42#ifdef DEBUG
43#define UDSL_ASSERT(x) BUG_ON(!(x))
44#else
45#define UDSL_ASSERT(x) do { if (!(x)) warn("failed assertion '" #x "' at line %d", __LINE__); } while(0)
46#endif
47
48#define UDSL_MAX_RCV_URBS 4
49#define UDSL_MAX_SND_URBS 4
50#define UDSL_MAX_RCV_BUFS 8
51#define UDSL_MAX_SND_BUFS 8
52#define UDSL_MAX_RCV_BUF_SIZE 1024 /* ATM cells */
53#define UDSL_MAX_SND_BUF_SIZE 1024 /* ATM cells */
54#define UDSL_DEFAULT_RCV_URBS 2
55#define UDSL_DEFAULT_SND_URBS 2
56#define UDSL_DEFAULT_RCV_BUFS 4
57#define UDSL_DEFAULT_SND_BUFS 4
58#define UDSL_DEFAULT_RCV_BUF_SIZE 64 /* ATM cells */
59#define UDSL_DEFAULT_SND_BUF_SIZE 64 /* ATM cells */
60
61#define ATM_CELL_HEADER (ATM_CELL_SIZE - ATM_CELL_PAYLOAD)
62#define UDSL_NUM_CELLS(x) (((x) + ATM_AAL5_TRAILER + ATM_CELL_PAYLOAD - 1) / ATM_CELL_PAYLOAD)
63
64/* receive */
65
66struct udsl_receive_buffer {
67 struct list_head list;
68 unsigned char *base;
69 unsigned int filled_cells;
70};
71
72struct udsl_receiver {
73 struct list_head list;
74 struct udsl_receive_buffer *buffer;
75 struct urb *urb;
76 struct udsl_instance_data *instance;
77};
78
79struct udsl_vcc_data {
80 /* vpi/vci lookup */
81 struct list_head list;
82 short vpi;
83 int vci;
84 struct atm_vcc *vcc;
85
86 /* raw cell reassembly */
87 struct sk_buff *sarb;
88};
89
90/* send */
91
92struct udsl_send_buffer {
93 struct list_head list;
94 unsigned char *base;
95 unsigned char *free_start;
96 unsigned int free_cells;
97};
98
99struct udsl_sender {
100 struct list_head list;
101 struct udsl_send_buffer *buffer;
102 struct urb *urb;
103 struct udsl_instance_data *instance;
104};
105
106struct udsl_control {
107 struct atm_skb_data atm_data;
108 unsigned int num_cells;
109 unsigned int num_entire;
110 unsigned int pdu_padding;
111 unsigned char aal5_trailer[ATM_AAL5_TRAILER];
112};
113
114#define UDSL_SKB(x) ((struct udsl_control *)(x)->cb)
115
116/* main driver data */
117
118enum udsl_status {
119 UDSL_NO_FIRMWARE,
120 UDSL_LOADING_FIRMWARE,
121 UDSL_LOADED_FIRMWARE
122};
123
124struct udsl_instance_data {
125 struct kref refcount;
126 struct semaphore serialize;
127
128 /* USB device part */
129 struct usb_device *usb_dev;
130 char description[64];
131 int data_endpoint;
132 int snd_padding;
133 int rcv_padding;
134 const char *driver_name;
135
136 /* ATM device part */
137 struct atm_dev *atm_dev;
138 struct list_head vcc_list;
139
140 /* firmware */
141 int (*firmware_wait) (struct udsl_instance_data *);
142 enum udsl_status status;
143 wait_queue_head_t firmware_waiters;
144
145 /* receive */
146 struct udsl_receiver receivers[UDSL_MAX_RCV_URBS];
147 struct udsl_receive_buffer receive_buffers[UDSL_MAX_RCV_BUFS];
148
149 spinlock_t receive_lock;
150 struct list_head spare_receivers;
151 struct list_head filled_receive_buffers;
152
153 struct tasklet_struct receive_tasklet;
154 struct list_head spare_receive_buffers;
155
156 /* send */
157 struct udsl_sender senders[UDSL_MAX_SND_URBS];
158 struct udsl_send_buffer send_buffers[UDSL_MAX_SND_BUFS];
159
160 struct sk_buff_head sndqueue;
161
162 spinlock_t send_lock;
163 struct list_head spare_senders;
164 struct list_head spare_send_buffers;
165
166 struct tasklet_struct send_tasklet;
167 struct sk_buff *current_skb; /* being emptied */
168 struct udsl_send_buffer *current_buffer; /* being filled */
169 struct list_head filled_send_buffers;
170};
171
172extern int udsl_instance_setup(struct usb_device *dev,
173 struct udsl_instance_data *instance);
174extern void udsl_instance_disconnect(struct udsl_instance_data *instance);
175extern void udsl_get_instance(struct udsl_instance_data *instance);
176extern void udsl_put_instance(struct udsl_instance_data *instance);
diff --git a/drivers/usb/atm/usbatm.c b/drivers/usb/atm/usbatm.c
new file mode 100644
index 000000000000..bb1db1959854
--- /dev/null
+++ b/drivers/usb/atm/usbatm.c
@@ -0,0 +1,1230 @@
1/******************************************************************************
2 * usbatm.c - Generic USB xDSL driver core
3 *
4 * Copyright (C) 2001, Alcatel
5 * Copyright (C) 2003, Duncan Sands, SolNegro, Josep Comas
6 * Copyright (C) 2004, David Woodhouse, Roman Kagan
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 2 of the License, or (at your option)
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * more details.
17 *
18 * You should have received a copy of the GNU General Public License along with
19 * this program; if not, write to the Free Software Foundation, Inc., 59
20 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 *
22 ******************************************************************************/
23
24/*
25 * Written by Johan Verrept, Duncan Sands (duncan.sands@free.fr) and David Woodhouse
26 *
27 * 1.7+: - See the check-in logs
28 *
29 * 1.6: - No longer opens a connection if the firmware is not loaded
30 * - Added support for the speedtouch 330
31 * - Removed the limit on the number of devices
32 * - Module now autoloads on device plugin
33 * - Merged relevant parts of sarlib
34 * - Replaced the kernel thread with a tasklet
35 * - New packet transmission code
36 * - Changed proc file contents
37 * - Fixed all known SMP races
38 * - Many fixes and cleanups
39 * - Various fixes by Oliver Neukum (oliver@neukum.name)
40 *
41 * 1.5A: - Version for inclusion in 2.5 series kernel
42 * - Modifications by Richard Purdie (rpurdie@rpsys.net)
43 * - made compatible with kernel 2.5.6 onwards by changing
44 * usbatm_usb_send_data_context->urb to a pointer and adding code
45 * to alloc and free it
46 * - remove_wait_queue() added to usbatm_atm_processqueue_thread()
47 *
48 * 1.5: - fixed memory leak when atmsar_decode_aal5 returned NULL.
49 * (reported by stephen.robinson@zen.co.uk)
50 *
51 * 1.4: - changed the spin_lock() under interrupt to spin_lock_irqsave()
52 * - unlink all active send urbs of a vcc that is being closed.
53 *
54 * 1.3.1: - added the version number
55 *
56 * 1.3: - Added multiple send urb support
57 * - fixed memory leak and vcc->tx_inuse starvation bug
58 * when not enough memory left in vcc.
59 *
60 * 1.2: - Fixed race condition in usbatm_usb_send_data()
61 * 1.1: - Turned off packet debugging
62 *
63 */
64
65#include "usbatm.h"
66
67#include <asm/uaccess.h>
68#include <linux/crc32.h>
69#include <linux/errno.h>
70#include <linux/init.h>
71#include <linux/interrupt.h>
72#include <linux/kernel.h>
73#include <linux/module.h>
74#include <linux/moduleparam.h>
75#include <linux/proc_fs.h>
76#include <linux/sched.h>
77#include <linux/signal.h>
78#include <linux/slab.h>
79#include <linux/smp_lock.h>
80#include <linux/stat.h>
81#include <linux/timer.h>
82#include <linux/wait.h>
83
84#ifdef VERBOSE_DEBUG
85static int usbatm_print_packet(const unsigned char *data, int len);
86#define PACKETDEBUG(arg...) usbatm_print_packet (arg)
87#define vdbg(arg...) dbg (arg)
88#else
89#define PACKETDEBUG(arg...)
90#define vdbg(arg...)
91#endif
92
93#define DRIVER_AUTHOR "Johan Verrept, Duncan Sands <duncan.sands@free.fr>"
94#define DRIVER_VERSION "1.9"
95#define DRIVER_DESC "Generic USB ATM/DSL I/O, version " DRIVER_VERSION
96
97static const char usbatm_driver_name[] = "usbatm";
98
99#define UDSL_MAX_RCV_URBS 16
100#define UDSL_MAX_SND_URBS 16
101#define UDSL_MAX_RCV_BUF_SIZE 1024 /* ATM cells */
102#define UDSL_MAX_SND_BUF_SIZE 1024 /* ATM cells */
103#define UDSL_DEFAULT_RCV_URBS 4
104#define UDSL_DEFAULT_SND_URBS 4
105#define UDSL_DEFAULT_RCV_BUF_SIZE 64 /* ATM cells */
106#define UDSL_DEFAULT_SND_BUF_SIZE 64 /* ATM cells */
107
108#define ATM_CELL_HEADER (ATM_CELL_SIZE - ATM_CELL_PAYLOAD)
109
110#define THROTTLE_MSECS 100 /* delay to recover processing after urb submission fails */
111
112static unsigned int num_rcv_urbs = UDSL_DEFAULT_RCV_URBS;
113static unsigned int num_snd_urbs = UDSL_DEFAULT_SND_URBS;
114static unsigned int rcv_buf_size = UDSL_DEFAULT_RCV_BUF_SIZE;
115static unsigned int snd_buf_size = UDSL_DEFAULT_SND_BUF_SIZE;
116
117module_param(num_rcv_urbs, uint, S_IRUGO);
118MODULE_PARM_DESC(num_rcv_urbs,
119 "Number of urbs used for reception (range: 0-"
120 __MODULE_STRING(UDSL_MAX_RCV_URBS) ", default: "
121 __MODULE_STRING(UDSL_DEFAULT_RCV_URBS) ")");
122
123module_param(num_snd_urbs, uint, S_IRUGO);
124MODULE_PARM_DESC(num_snd_urbs,
125 "Number of urbs used for transmission (range: 0-"
126 __MODULE_STRING(UDSL_MAX_SND_URBS) ", default: "
127 __MODULE_STRING(UDSL_DEFAULT_SND_URBS) ")");
128
129module_param(rcv_buf_size, uint, S_IRUGO);
130MODULE_PARM_DESC(rcv_buf_size,
131 "Size of the buffers used for reception in ATM cells (range: 1-"
132 __MODULE_STRING(UDSL_MAX_RCV_BUF_SIZE) ", default: "
133 __MODULE_STRING(UDSL_DEFAULT_RCV_BUF_SIZE) ")");
134
135module_param(snd_buf_size, uint, S_IRUGO);
136MODULE_PARM_DESC(snd_buf_size,
137 "Size of the buffers used for transmission in ATM cells (range: 1-"
138 __MODULE_STRING(UDSL_MAX_SND_BUF_SIZE) ", default: "
139 __MODULE_STRING(UDSL_DEFAULT_SND_BUF_SIZE) ")");
140
141
142/* receive */
143
144struct usbatm_vcc_data {
145 /* vpi/vci lookup */
146 struct list_head list;
147 short vpi;
148 int vci;
149 struct atm_vcc *vcc;
150
151 /* raw cell reassembly */
152 struct sk_buff *sarb;
153};
154
155
156/* send */
157
158struct usbatm_control {
159 struct atm_skb_data atm;
160 u32 len;
161 u32 crc;
162};
163
164#define UDSL_SKB(x) ((struct usbatm_control *)(x)->cb)
165
166
167/* ATM */
168
169static void usbatm_atm_dev_close(struct atm_dev *dev);
170static int usbatm_atm_open(struct atm_vcc *vcc);
171static void usbatm_atm_close(struct atm_vcc *vcc);
172static int usbatm_atm_ioctl(struct atm_dev *dev, unsigned int cmd, void __user * arg);
173static int usbatm_atm_send(struct atm_vcc *vcc, struct sk_buff *skb);
174static int usbatm_atm_proc_read(struct atm_dev *atm_dev, loff_t * pos, char *page);
175
176static struct atmdev_ops usbatm_atm_devops = {
177 .dev_close = usbatm_atm_dev_close,
178 .open = usbatm_atm_open,
179 .close = usbatm_atm_close,
180 .ioctl = usbatm_atm_ioctl,
181 .send = usbatm_atm_send,
182 .proc_read = usbatm_atm_proc_read,
183 .owner = THIS_MODULE,
184};
185
186
187/***********
188** misc **
189***********/
190
191static inline unsigned int usbatm_pdu_length(unsigned int length)
192{
193 length += ATM_CELL_PAYLOAD - 1 + ATM_AAL5_TRAILER;
194 return length - length % ATM_CELL_PAYLOAD;
195}
196
197static inline void usbatm_pop(struct atm_vcc *vcc, struct sk_buff *skb)
198{
199 if (vcc->pop)
200 vcc->pop(vcc, skb);
201 else
202 dev_kfree_skb(skb);
203}
204
205
206/***********
207** urbs **
208************/
209
210static inline struct urb *usbatm_pop_urb(struct usbatm_channel *channel)
211{
212 struct urb *urb;
213
214 spin_lock_irq(&channel->lock);
215 if (list_empty(&channel->list)) {
216 spin_unlock_irq(&channel->lock);
217 return NULL;
218 }
219
220 urb = list_entry(channel->list.next, struct urb, urb_list);
221 list_del(&urb->urb_list);
222 spin_unlock_irq(&channel->lock);
223
224 return urb;
225}
226
227static inline int usbatm_submit_urb(struct urb *urb)
228{
229 struct usbatm_channel *channel = urb->context;
230 int ret;
231
232 vdbg("%s: submitting urb 0x%p, size %u",
233 __func__, urb, urb->transfer_buffer_length);
234
235 ret = usb_submit_urb(urb, GFP_ATOMIC);
236 if (ret) {
237 atm_dbg(channel->usbatm, "%s: urb 0x%p submission failed (%d)!\n",
238 __func__, urb, ret);
239
240 /* consider all errors transient and return the buffer back to the queue */
241 urb->status = -EAGAIN;
242 spin_lock_irq(&channel->lock);
243
244 /* must add to the front when sending; doesn't matter when receiving */
245 list_add(&urb->urb_list, &channel->list);
246
247 spin_unlock_irq(&channel->lock);
248
249 /* make sure the channel doesn't stall */
250 mod_timer(&channel->delay, jiffies + msecs_to_jiffies(THROTTLE_MSECS));
251 }
252
253 return ret;
254}
255
256static void usbatm_complete(struct urb *urb, struct pt_regs *regs)
257{
258 struct usbatm_channel *channel = urb->context;
259 unsigned long flags;
260
261 vdbg("%s: urb 0x%p, status %d, actual_length %d",
262 __func__, urb, urb->status, urb->actual_length);
263
264 /* usually in_interrupt(), but not always */
265 spin_lock_irqsave(&channel->lock, flags);
266
267 /* must add to the back when receiving; doesn't matter when sending */
268 list_add_tail(&urb->urb_list, &channel->list);
269
270 spin_unlock_irqrestore(&channel->lock, flags);
271
272 if (unlikely(urb->status))
273 /* throttle processing in case of an error */
274 mod_timer(&channel->delay, jiffies + msecs_to_jiffies(THROTTLE_MSECS));
275 else
276 tasklet_schedule(&channel->tasklet);
277}
278
279
280/*************
281** decode **
282*************/
283
284static inline struct usbatm_vcc_data *usbatm_find_vcc(struct usbatm_data *instance,
285 short vpi, int vci)
286{
287 struct usbatm_vcc_data *vcc;
288
289 list_for_each_entry(vcc, &instance->vcc_list, list)
290 if ((vcc->vci == vci) && (vcc->vpi == vpi))
291 return vcc;
292 return NULL;
293}
294
295static void usbatm_extract_cells(struct usbatm_data *instance,
296 unsigned char *source, unsigned int avail_data)
297{
298 struct usbatm_vcc_data *cached_vcc = NULL;
299 struct atm_vcc *vcc;
300 struct sk_buff *sarb;
301 unsigned int stride = instance->rx_channel.stride;
302 int vci, cached_vci = 0;
303 short vpi, cached_vpi = 0;
304 u8 pti;
305
306 for (; avail_data >= stride; avail_data -= stride, source += stride) {
307 vpi = ((source[0] & 0x0f) << 4) | (source[1] >> 4);
308 vci = ((source[1] & 0x0f) << 12) | (source[2] << 4) | (source[3] >> 4);
309 pti = ((source[3] & 0xe) >> 1);
310
311 vdbg("%s: vpi %hd, vci %d, pti %d", __func__, vpi, vci, pti);
312
313 if ((vci != cached_vci) || (vpi != cached_vpi)) {
314 cached_vpi = vpi;
315 cached_vci = vci;
316
317 cached_vcc = usbatm_find_vcc(instance, vpi, vci);
318
319 if (!cached_vcc)
320 atm_dbg(instance, "%s: unknown vpi/vci (%hd/%d)!\n", __func__, vpi, vci);
321 }
322
323 if (!cached_vcc)
324 continue;
325
326 vcc = cached_vcc->vcc;
327
328 /* OAM F5 end-to-end */
329 if (pti == ATM_PTI_E2EF5) {
330 atm_warn(instance, "%s: OAM not supported (vpi %d, vci %d)!\n", __func__, vpi, vci);
331 atomic_inc(&vcc->stats->rx_err);
332 continue;
333 }
334
335 sarb = cached_vcc->sarb;
336
337 if (sarb->tail + ATM_CELL_PAYLOAD > sarb->end) {
338 atm_dbg(instance, "%s: buffer overrun (sarb->len %u, vcc: 0x%p)!\n",
339 __func__, sarb->len, vcc);
340 /* discard cells already received */
341 skb_trim(sarb, 0);
342 UDSL_ASSERT(sarb->tail + ATM_CELL_PAYLOAD <= sarb->end);
343 }
344
345 memcpy(sarb->tail, source + ATM_CELL_HEADER, ATM_CELL_PAYLOAD);
346 __skb_put(sarb, ATM_CELL_PAYLOAD);
347
348 if (pti & 1) {
349 struct sk_buff *skb;
350 unsigned int length;
351 unsigned int pdu_length;
352
353 length = (source[ATM_CELL_SIZE - 6] << 8) + source[ATM_CELL_SIZE - 5];
354
355 /* guard against overflow */
356 if (length > ATM_MAX_AAL5_PDU) {
357 atm_dbg(instance, "%s: bogus length %u (vcc: 0x%p)!\n",
358 __func__, length, vcc);
359 atomic_inc(&vcc->stats->rx_err);
360 goto out;
361 }
362
363 pdu_length = usbatm_pdu_length(length);
364
365 if (sarb->len < pdu_length) {
366 atm_dbg(instance, "%s: bogus pdu_length %u (sarb->len: %u, vcc: 0x%p)!\n",
367 __func__, pdu_length, sarb->len, vcc);
368 atomic_inc(&vcc->stats->rx_err);
369 goto out;
370 }
371
372 if (crc32_be(~0, sarb->tail - pdu_length, pdu_length) != 0xc704dd7b) {
373 atm_dbg(instance, "%s: packet failed crc check (vcc: 0x%p)!\n",
374 __func__, vcc);
375 atomic_inc(&vcc->stats->rx_err);
376 goto out;
377 }
378
379 vdbg("%s: got packet (length: %u, pdu_length: %u, vcc: 0x%p)", __func__, length, pdu_length, vcc);
380
381 if (!(skb = dev_alloc_skb(length))) {
382 atm_dbg(instance, "%s: no memory for skb (length: %u)!\n", __func__, length);
383 atomic_inc(&vcc->stats->rx_drop);
384 goto out;
385 }
386
387 vdbg("%s: allocated new sk_buff (skb: 0x%p, skb->truesize: %u)", __func__, skb, skb->truesize);
388
389 if (!atm_charge(vcc, skb->truesize)) {
390 atm_dbg(instance, "%s: failed atm_charge (skb->truesize: %u)!\n", __func__, skb->truesize);
391 dev_kfree_skb(skb);
392 goto out; /* atm_charge increments rx_drop */
393 }
394
395 memcpy(skb->data, sarb->tail - pdu_length, length);
396 __skb_put(skb, length);
397
398 vdbg("%s: sending skb 0x%p, skb->len %u, skb->truesize %u",
399 __func__, skb, skb->len, skb->truesize);
400
401 PACKETDEBUG(skb->data, skb->len);
402
403 vcc->push(vcc, skb);
404
405 atomic_inc(&vcc->stats->rx);
406 out:
407 skb_trim(sarb, 0);
408 }
409 }
410}
411
412
413/*************
414** encode **
415*************/
416
417static unsigned int usbatm_write_cells(struct usbatm_data *instance,
418 struct sk_buff *skb,
419 u8 *target, unsigned int avail_space)
420{
421 struct usbatm_control *ctrl = UDSL_SKB(skb);
422 struct atm_vcc *vcc = ctrl->atm.vcc;
423 unsigned int num_written;
424 unsigned int stride = instance->tx_channel.stride;
425
426 vdbg("%s: skb->len=%d, avail_space=%u", __func__, skb->len, avail_space);
427 UDSL_ASSERT(!(avail_space % stride));
428
429 for (num_written = 0; num_written < avail_space && ctrl->len;
430 num_written += stride, target += stride) {
431 unsigned int data_len = min_t(unsigned int, skb->len, ATM_CELL_PAYLOAD);
432 unsigned int left = ATM_CELL_PAYLOAD - data_len;
433 u8 *ptr = target;
434
435 ptr[0] = vcc->vpi >> 4;
436 ptr[1] = (vcc->vpi << 4) | (vcc->vci >> 12);
437 ptr[2] = vcc->vci >> 4;
438 ptr[3] = vcc->vci << 4;
439 ptr[4] = 0xec;
440 ptr += ATM_CELL_HEADER;
441
442 memcpy(ptr, skb->data, data_len);
443 ptr += data_len;
444 __skb_pull(skb, data_len);
445
446 if(!left)
447 continue;
448
449 memset(ptr, 0, left);
450
451 if (left >= ATM_AAL5_TRAILER) { /* trailer will go in this cell */
452 u8 *trailer = target + ATM_CELL_SIZE - ATM_AAL5_TRAILER;
453 /* trailer[0] = 0; UU = 0 */
454 /* trailer[1] = 0; CPI = 0 */
455 trailer[2] = ctrl->len >> 8;
456 trailer[3] = ctrl->len;
457
458 ctrl->crc = ~ crc32_be(ctrl->crc, ptr, left - 4);
459
460 trailer[4] = ctrl->crc >> 24;
461 trailer[5] = ctrl->crc >> 16;
462 trailer[6] = ctrl->crc >> 8;
463 trailer[7] = ctrl->crc;
464
465 target[3] |= 0x2; /* adjust PTI */
466
467 ctrl->len = 0; /* tag this skb finished */
468 }
469 else
470 ctrl->crc = crc32_be(ctrl->crc, ptr, left);
471 }
472
473 return num_written;
474}
475
476
477/**************
478** receive **
479**************/
480
481static void usbatm_rx_process(unsigned long data)
482{
483 struct usbatm_data *instance = (struct usbatm_data *)data;
484 struct urb *urb;
485
486 while ((urb = usbatm_pop_urb(&instance->rx_channel))) {
487 vdbg("%s: processing urb 0x%p", __func__, urb);
488
489 if (usb_pipeisoc(urb->pipe)) {
490 int i;
491 for (i = 0; i < urb->number_of_packets; i++)
492 if (!urb->iso_frame_desc[i].status)
493 usbatm_extract_cells(instance,
494 (u8 *)urb->transfer_buffer + urb->iso_frame_desc[i].offset,
495 urb->iso_frame_desc[i].actual_length);
496 }
497 else
498 if (!urb->status)
499 usbatm_extract_cells(instance, urb->transfer_buffer, urb->actual_length);
500
501 if (usbatm_submit_urb(urb))
502 return;
503 }
504}
505
506
507/***********
508** send **
509***********/
510
511static void usbatm_tx_process(unsigned long data)
512{
513 struct usbatm_data *instance = (struct usbatm_data *)data;
514 struct sk_buff *skb = instance->current_skb;
515 struct urb *urb = NULL;
516 const unsigned int buf_size = instance->tx_channel.buf_size;
517 unsigned int num_written = 0;
518 u8 *buffer = NULL;
519
520 if (!skb)
521 skb = skb_dequeue(&instance->sndqueue);
522
523 while (skb) {
524 if (!urb) {
525 urb = usbatm_pop_urb(&instance->tx_channel);
526 if (!urb)
527 break; /* no more senders */
528 buffer = urb->transfer_buffer;
529 num_written = (urb->status == -EAGAIN) ?
530 urb->transfer_buffer_length : 0;
531 }
532
533 num_written += usbatm_write_cells(instance, skb,
534 buffer + num_written,
535 buf_size - num_written);
536
537 vdbg("%s: wrote %u bytes from skb 0x%p to urb 0x%p",
538 __func__, num_written, skb, urb);
539
540 if (!UDSL_SKB(skb)->len) {
541 struct atm_vcc *vcc = UDSL_SKB(skb)->atm.vcc;
542
543 usbatm_pop(vcc, skb);
544 atomic_inc(&vcc->stats->tx);
545
546 skb = skb_dequeue(&instance->sndqueue);
547 }
548
549 if (num_written == buf_size || (!skb && num_written)) {
550 urb->transfer_buffer_length = num_written;
551
552 if (usbatm_submit_urb(urb))
553 break;
554 urb = NULL;
555 }
556 }
557
558 instance->current_skb = skb;
559}
560
561static void usbatm_cancel_send(struct usbatm_data *instance,
562 struct atm_vcc *vcc)
563{
564 struct sk_buff *skb, *n;
565
566 atm_dbg(instance, "%s entered\n", __func__);
567 spin_lock_irq(&instance->sndqueue.lock);
568 for (skb = instance->sndqueue.next, n = skb->next;
569 skb != (struct sk_buff *)&instance->sndqueue;
570 skb = n, n = skb->next)
571 if (UDSL_SKB(skb)->atm.vcc == vcc) {
572 atm_dbg(instance, "%s: popping skb 0x%p\n", __func__, skb);
573 __skb_unlink(skb, &instance->sndqueue);
574 usbatm_pop(vcc, skb);
575 }
576 spin_unlock_irq(&instance->sndqueue.lock);
577
578 tasklet_disable(&instance->tx_channel.tasklet);
579 if ((skb = instance->current_skb) && (UDSL_SKB(skb)->atm.vcc == vcc)) {
580 atm_dbg(instance, "%s: popping current skb (0x%p)\n", __func__, skb);
581 instance->current_skb = NULL;
582 usbatm_pop(vcc, skb);
583 }
584 tasklet_enable(&instance->tx_channel.tasklet);
585 atm_dbg(instance, "%s done\n", __func__);
586}
587
588static int usbatm_atm_send(struct atm_vcc *vcc, struct sk_buff *skb)
589{
590 struct usbatm_data *instance = vcc->dev->dev_data;
591 struct usbatm_control *ctrl = UDSL_SKB(skb);
592 int err;
593
594 vdbg("%s called (skb 0x%p, len %u)", __func__, skb, skb->len);
595
596 if (!instance) {
597 dbg("%s: NULL data!", __func__);
598 err = -ENODEV;
599 goto fail;
600 }
601
602 if (vcc->qos.aal != ATM_AAL5) {
603 atm_dbg(instance, "%s: unsupported ATM type %d!\n", __func__, vcc->qos.aal);
604 err = -EINVAL;
605 goto fail;
606 }
607
608 if (skb->len > ATM_MAX_AAL5_PDU) {
609 atm_dbg(instance, "%s: packet too long (%d vs %d)!\n",
610 __func__, skb->len, ATM_MAX_AAL5_PDU);
611 err = -EINVAL;
612 goto fail;
613 }
614
615 PACKETDEBUG(skb->data, skb->len);
616
617 /* initialize the control block */
618 ctrl->atm.vcc = vcc;
619 ctrl->len = skb->len;
620 ctrl->crc = crc32_be(~0, skb->data, skb->len);
621
622 skb_queue_tail(&instance->sndqueue, skb);
623 tasklet_schedule(&instance->tx_channel.tasklet);
624
625 return 0;
626
627 fail:
628 usbatm_pop(vcc, skb);
629 return err;
630}
631
632
633/********************
634** bean counting **
635********************/
636
637static void usbatm_destroy_instance(struct kref *kref)
638{
639 struct usbatm_data *instance = container_of(kref, struct usbatm_data, refcount);
640
641 dbg("%s", __func__);
642
643 tasklet_kill(&instance->rx_channel.tasklet);
644 tasklet_kill(&instance->tx_channel.tasklet);
645 usb_put_dev(instance->usb_dev);
646 kfree(instance);
647}
648
649void usbatm_get_instance(struct usbatm_data *instance)
650{
651 dbg("%s", __func__);
652
653 kref_get(&instance->refcount);
654}
655
656void usbatm_put_instance(struct usbatm_data *instance)
657{
658 dbg("%s", __func__);
659
660 kref_put(&instance->refcount, usbatm_destroy_instance);
661}
662
663
664/**********
665** ATM **
666**********/
667
668static void usbatm_atm_dev_close(struct atm_dev *dev)
669{
670 struct usbatm_data *instance = dev->dev_data;
671
672 dbg("%s", __func__);
673
674 if (!instance)
675 return;
676
677 dev->dev_data = NULL;
678 usbatm_put_instance(instance); /* taken in usbatm_atm_init */
679}
680
681static int usbatm_atm_proc_read(struct atm_dev *atm_dev, loff_t * pos, char *page)
682{
683 struct usbatm_data *instance = atm_dev->dev_data;
684 int left = *pos;
685
686 if (!instance) {
687 dbg("%s: NULL instance!", __func__);
688 return -ENODEV;
689 }
690
691 if (!left--)
692 return sprintf(page, "%s\n", instance->description);
693
694 if (!left--)
695 return sprintf(page, "MAC: %02x:%02x:%02x:%02x:%02x:%02x\n",
696 atm_dev->esi[0], atm_dev->esi[1],
697 atm_dev->esi[2], atm_dev->esi[3],
698 atm_dev->esi[4], atm_dev->esi[5]);
699
700 if (!left--)
701 return sprintf(page,
702 "AAL5: tx %d ( %d err ), rx %d ( %d err, %d drop )\n",
703 atomic_read(&atm_dev->stats.aal5.tx),
704 atomic_read(&atm_dev->stats.aal5.tx_err),
705 atomic_read(&atm_dev->stats.aal5.rx),
706 atomic_read(&atm_dev->stats.aal5.rx_err),
707 atomic_read(&atm_dev->stats.aal5.rx_drop));
708
709 if (!left--)
710 switch (atm_dev->signal) {
711 case ATM_PHY_SIG_FOUND:
712 return sprintf(page, "Line up\n");
713 case ATM_PHY_SIG_LOST:
714 return sprintf(page, "Line down\n");
715 default:
716 return sprintf(page, "Line state unknown\n");
717 }
718
719 return 0;
720}
721
722static int usbatm_atm_open(struct atm_vcc *vcc)
723{
724 struct usbatm_data *instance = vcc->dev->dev_data;
725 struct usbatm_vcc_data *new = NULL;
726 int ret;
727 int vci = vcc->vci;
728 short vpi = vcc->vpi;
729
730 if (!instance) {
731 dbg("%s: NULL data!", __func__);
732 return -ENODEV;
733 }
734
735 atm_dbg(instance, "%s: vpi %hd, vci %d\n", __func__, vpi, vci);
736
737 /* only support AAL5 */
738 if ((vcc->qos.aal != ATM_AAL5) || (vcc->qos.rxtp.max_sdu < 0)
739 || (vcc->qos.rxtp.max_sdu > ATM_MAX_AAL5_PDU)) {
740 atm_dbg(instance, "%s: unsupported ATM type %d!\n", __func__, vcc->qos.aal);
741 return -EINVAL;
742 }
743
744 down(&instance->serialize); /* vs self, usbatm_atm_close */
745
746 if (usbatm_find_vcc(instance, vpi, vci)) {
747 atm_dbg(instance, "%s: %hd/%d already in use!\n", __func__, vpi, vci);
748 ret = -EADDRINUSE;
749 goto fail;
750 }
751
752 if (!(new = kmalloc(sizeof(struct usbatm_vcc_data), GFP_KERNEL))) {
753 atm_dbg(instance, "%s: no memory for vcc_data!\n", __func__);
754 ret = -ENOMEM;
755 goto fail;
756 }
757
758 memset(new, 0, sizeof(struct usbatm_vcc_data));
759 new->vcc = vcc;
760 new->vpi = vpi;
761 new->vci = vci;
762
763 new->sarb = alloc_skb(usbatm_pdu_length(vcc->qos.rxtp.max_sdu), GFP_KERNEL);
764 if (!new->sarb) {
765 atm_dbg(instance, "%s: no memory for SAR buffer!\n", __func__);
766 ret = -ENOMEM;
767 goto fail;
768 }
769
770 vcc->dev_data = new;
771
772 tasklet_disable(&instance->rx_channel.tasklet);
773 list_add(&new->list, &instance->vcc_list);
774 tasklet_enable(&instance->rx_channel.tasklet);
775
776 set_bit(ATM_VF_ADDR, &vcc->flags);
777 set_bit(ATM_VF_PARTIAL, &vcc->flags);
778 set_bit(ATM_VF_READY, &vcc->flags);
779
780 up(&instance->serialize);
781
782 atm_dbg(instance, "%s: allocated vcc data 0x%p\n", __func__, new);
783
784 return 0;
785
786fail:
787 kfree(new);
788 up(&instance->serialize);
789 return ret;
790}
791
792static void usbatm_atm_close(struct atm_vcc *vcc)
793{
794 struct usbatm_data *instance = vcc->dev->dev_data;
795 struct usbatm_vcc_data *vcc_data = vcc->dev_data;
796
797 if (!instance || !vcc_data) {
798 dbg("%s: NULL data!", __func__);
799 return;
800 }
801
802 atm_dbg(instance, "%s entered\n", __func__);
803
804 atm_dbg(instance, "%s: deallocating vcc 0x%p with vpi %d vci %d\n",
805 __func__, vcc_data, vcc_data->vpi, vcc_data->vci);
806
807 usbatm_cancel_send(instance, vcc);
808
809 down(&instance->serialize); /* vs self, usbatm_atm_open */
810
811 tasklet_disable(&instance->rx_channel.tasklet);
812 list_del(&vcc_data->list);
813 tasklet_enable(&instance->rx_channel.tasklet);
814
815 kfree_skb(vcc_data->sarb);
816 vcc_data->sarb = NULL;
817
818 kfree(vcc_data);
819 vcc->dev_data = NULL;
820
821 vcc->vpi = ATM_VPI_UNSPEC;
822 vcc->vci = ATM_VCI_UNSPEC;
823 clear_bit(ATM_VF_READY, &vcc->flags);
824 clear_bit(ATM_VF_PARTIAL, &vcc->flags);
825 clear_bit(ATM_VF_ADDR, &vcc->flags);
826
827 up(&instance->serialize);
828
829 atm_dbg(instance, "%s successful\n", __func__);
830}
831
832static int usbatm_atm_ioctl(struct atm_dev *dev, unsigned int cmd,
833 void __user * arg)
834{
835 switch (cmd) {
836 case ATM_QUERYLOOP:
837 return put_user(ATM_LM_NONE, (int __user *)arg) ? -EFAULT : 0;
838 default:
839 return -ENOIOCTLCMD;
840 }
841}
842
843static int usbatm_atm_init(struct usbatm_data *instance)
844{
845 struct atm_dev *atm_dev;
846 int ret, i;
847
848 /* ATM init */
849 atm_dev = atm_dev_register(instance->driver_name, &usbatm_atm_devops, -1, NULL);
850 if (!atm_dev) {
851 usb_dbg(instance, "%s: failed to register ATM device!\n", __func__);
852 return -1;
853 }
854
855 instance->atm_dev = atm_dev;
856
857 atm_dev->ci_range.vpi_bits = ATM_CI_MAX;
858 atm_dev->ci_range.vci_bits = ATM_CI_MAX;
859 atm_dev->signal = ATM_PHY_SIG_UNKNOWN;
860
861 /* temp init ATM device, set to 128kbit */
862 atm_dev->link_rate = 128 * 1000 / 424;
863
864 if (instance->driver->atm_start && ((ret = instance->driver->atm_start(instance, atm_dev)) < 0)) {
865 atm_dbg(instance, "%s: atm_start failed: %d!\n", __func__, ret);
866 goto fail;
867 }
868
869 /* ready for ATM callbacks */
870 usbatm_get_instance(instance); /* dropped in usbatm_atm_dev_close */
871 mb();
872 atm_dev->dev_data = instance;
873
874 /* submit all rx URBs */
875 for (i = 0; i < num_rcv_urbs; i++)
876 usbatm_submit_urb(instance->urbs[i]);
877
878 return 0;
879
880 fail:
881 instance->atm_dev = NULL;
882 shutdown_atm_dev(atm_dev); /* usbatm_atm_dev_close will eventually be called */
883 return ret;
884}
885
886
887/**********
888** USB **
889**********/
890
891static int usbatm_do_heavy_init(void *arg)
892{
893 struct usbatm_data *instance = arg;
894 int ret;
895
896 daemonize(instance->driver->driver_name);
897 allow_signal(SIGTERM);
898
899 complete(&instance->thread_started);
900
901 ret = instance->driver->heavy_init(instance, instance->usb_intf);
902
903 if (!ret)
904 ret = usbatm_atm_init(instance);
905
906 down(&instance->serialize);
907 instance->thread_pid = -1;
908 up(&instance->serialize);
909
910 complete_and_exit(&instance->thread_exited, ret);
911}
912
913static int usbatm_heavy_init(struct usbatm_data *instance)
914{
915 int ret = kernel_thread(usbatm_do_heavy_init, instance, CLONE_KERNEL);
916
917 if (ret < 0) {
918 usb_dbg(instance, "%s: failed to create kernel_thread (%d)!\n", __func__, ret);
919 return ret;
920 }
921
922 down(&instance->serialize);
923 instance->thread_pid = ret;
924 up(&instance->serialize);
925
926 wait_for_completion(&instance->thread_started);
927
928 return 0;
929}
930
931static void usbatm_tasklet_schedule(unsigned long data)
932{
933 tasklet_schedule((struct tasklet_struct *) data);
934}
935
936static inline void usbatm_init_channel(struct usbatm_channel *channel)
937{
938 spin_lock_init(&channel->lock);
939 INIT_LIST_HEAD(&channel->list);
940 channel->delay.function = usbatm_tasklet_schedule;
941 channel->delay.data = (unsigned long) &channel->tasklet;
942 init_timer(&channel->delay);
943}
944
945int usbatm_usb_probe(struct usb_interface *intf, const struct usb_device_id *id,
946 struct usbatm_driver *driver)
947{
948 struct device *dev = &intf->dev;
949 struct usb_device *usb_dev = interface_to_usbdev(intf);
950 struct usbatm_data *instance;
951 char *buf;
952 int error = -ENOMEM;
953 int i, length;
954 int need_heavy;
955
956 dev_dbg(dev, "%s: trying driver %s with vendor=0x%x, product=0x%x, ifnum %d\n",
957 __func__, driver->driver_name,
958 le16_to_cpu(usb_dev->descriptor.idVendor),
959 le16_to_cpu(usb_dev->descriptor.idProduct),
960 intf->altsetting->desc.bInterfaceNumber);
961
962 /* instance init */
963 instance = kcalloc(1, sizeof(*instance) + sizeof(struct urb *) * (num_rcv_urbs + num_snd_urbs), GFP_KERNEL);
964 if (!instance) {
965 dev_dbg(dev, "%s: no memory for instance data!\n", __func__);
966 return -ENOMEM;
967 }
968
969 /* public fields */
970
971 instance->driver = driver;
972 snprintf(instance->driver_name, sizeof(instance->driver_name), driver->driver_name);
973
974 instance->usb_dev = usb_dev;
975 instance->usb_intf = intf;
976
977 buf = instance->description;
978 length = sizeof(instance->description);
979
980 if ((i = usb_string(usb_dev, usb_dev->descriptor.iProduct, buf, length)) < 0)
981 goto bind;
982
983 buf += i;
984 length -= i;
985
986 i = scnprintf(buf, length, " (");
987 buf += i;
988 length -= i;
989
990 if (length <= 0 || (i = usb_make_path(usb_dev, buf, length)) < 0)
991 goto bind;
992
993 buf += i;
994 length -= i;
995
996 snprintf(buf, length, ")");
997
998 bind:
999 need_heavy = 1;
1000 if (driver->bind && (error = driver->bind(instance, intf, id, &need_heavy)) < 0) {
1001 dev_dbg(dev, "%s: bind failed: %d!\n", __func__, error);
1002 goto fail_free;
1003 }
1004
1005 /* private fields */
1006
1007 kref_init(&instance->refcount); /* dropped in usbatm_usb_disconnect */
1008 init_MUTEX(&instance->serialize);
1009
1010 instance->thread_pid = -1;
1011 init_completion(&instance->thread_started);
1012 init_completion(&instance->thread_exited);
1013
1014 INIT_LIST_HEAD(&instance->vcc_list);
1015
1016 usbatm_init_channel(&instance->rx_channel);
1017 usbatm_init_channel(&instance->tx_channel);
1018 tasklet_init(&instance->rx_channel.tasklet, usbatm_rx_process, (unsigned long)instance);
1019 tasklet_init(&instance->tx_channel.tasklet, usbatm_tx_process, (unsigned long)instance);
1020 instance->rx_channel.endpoint = usb_rcvbulkpipe(usb_dev, driver->in);
1021 instance->tx_channel.endpoint = usb_sndbulkpipe(usb_dev, driver->out);
1022 instance->rx_channel.stride = ATM_CELL_SIZE + driver->rx_padding;
1023 instance->tx_channel.stride = ATM_CELL_SIZE + driver->tx_padding;
1024 instance->rx_channel.buf_size = rcv_buf_size * instance->rx_channel.stride;
1025 instance->tx_channel.buf_size = snd_buf_size * instance->tx_channel.stride;
1026 instance->rx_channel.usbatm = instance->tx_channel.usbatm = instance;
1027
1028 skb_queue_head_init(&instance->sndqueue);
1029
1030 for (i = 0; i < num_rcv_urbs + num_snd_urbs; i++) {
1031 struct urb *urb;
1032 u8 *buffer;
1033 unsigned int iso_packets = 0, iso_size = 0;
1034 struct usbatm_channel *channel = i < num_rcv_urbs ?
1035 &instance->rx_channel : &instance->tx_channel;
1036
1037 if (usb_pipeisoc(channel->endpoint)) {
1038 /* don't expect iso out endpoints */
1039 iso_size = usb_maxpacket(instance->usb_dev, channel->endpoint, 0);
1040 iso_size -= iso_size % channel->stride; /* alignment */
1041 BUG_ON(!iso_size);
1042 iso_packets = (channel->buf_size - 1) / iso_size + 1;
1043 }
1044
1045 urb = usb_alloc_urb(iso_packets, GFP_KERNEL);
1046 if (!urb) {
1047 dev_dbg(dev, "%s: no memory for urb %d!\n", __func__, i);
1048 goto fail_unbind;
1049 }
1050
1051 instance->urbs[i] = urb;
1052
1053 buffer = kmalloc(channel->buf_size, GFP_KERNEL);
1054 if (!buffer) {
1055 dev_dbg(dev, "%s: no memory for buffer %d!\n", __func__, i);
1056 goto fail_unbind;
1057 }
1058 memset(buffer, 0, channel->buf_size);
1059
1060 usb_fill_bulk_urb(urb, instance->usb_dev, channel->endpoint,
1061 buffer, channel->buf_size, usbatm_complete, channel);
1062 if (iso_packets) {
1063 int j;
1064 urb->interval = 1;
1065 urb->transfer_flags = URB_ISO_ASAP;
1066 urb->number_of_packets = iso_packets;
1067 for (j = 0; j < iso_packets; j++) {
1068 urb->iso_frame_desc[j].offset = iso_size * j;
1069 urb->iso_frame_desc[j].length = min_t(int, iso_size,
1070 channel->buf_size - urb->iso_frame_desc[j].offset);
1071 }
1072 }
1073
1074 /* put all tx URBs on the list of spares */
1075 if (i >= num_rcv_urbs)
1076 list_add_tail(&urb->urb_list, &channel->list);
1077
1078 vdbg("%s: alloced buffer 0x%p buf size %u urb 0x%p",
1079 __func__, urb->transfer_buffer, urb->transfer_buffer_length, urb);
1080 }
1081
1082 if (need_heavy && driver->heavy_init) {
1083 error = usbatm_heavy_init(instance);
1084 } else {
1085 complete(&instance->thread_exited); /* pretend that heavy_init was run */
1086 error = usbatm_atm_init(instance);
1087 }
1088
1089 if (error < 0)
1090 goto fail_unbind;
1091
1092 usb_get_dev(usb_dev);
1093 usb_set_intfdata(intf, instance);
1094
1095 return 0;
1096
1097 fail_unbind:
1098 if (instance->driver->unbind)
1099 instance->driver->unbind(instance, intf);
1100 fail_free:
1101 for (i = 0; i < num_rcv_urbs + num_snd_urbs; i++) {
1102 if (instance->urbs[i])
1103 kfree(instance->urbs[i]->transfer_buffer);
1104 usb_free_urb(instance->urbs[i]);
1105 }
1106
1107 kfree (instance);
1108
1109 return error;
1110}
1111EXPORT_SYMBOL_GPL(usbatm_usb_probe);
1112
1113void usbatm_usb_disconnect(struct usb_interface *intf)
1114{
1115 struct device *dev = &intf->dev;
1116 struct usbatm_data *instance = usb_get_intfdata(intf);
1117 int i;
1118
1119 dev_dbg(dev, "%s entered\n", __func__);
1120
1121 if (!instance) {
1122 dev_dbg(dev, "%s: NULL instance!\n", __func__);
1123 return;
1124 }
1125
1126 usb_set_intfdata(intf, NULL);
1127
1128 down(&instance->serialize);
1129 if (instance->thread_pid >= 0)
1130 kill_proc(instance->thread_pid, SIGTERM, 1);
1131 up(&instance->serialize);
1132
1133 wait_for_completion(&instance->thread_exited);
1134
1135 tasklet_disable(&instance->rx_channel.tasklet);
1136 tasklet_disable(&instance->tx_channel.tasklet);
1137
1138 for (i = 0; i < num_rcv_urbs + num_snd_urbs; i++)
1139 usb_kill_urb(instance->urbs[i]);
1140
1141 del_timer_sync(&instance->rx_channel.delay);
1142 del_timer_sync(&instance->tx_channel.delay);
1143
1144 if (instance->atm_dev && instance->driver->atm_stop)
1145 instance->driver->atm_stop(instance, instance->atm_dev);
1146
1147 if (instance->driver->unbind)
1148 instance->driver->unbind(instance, intf);
1149
1150 instance->driver_data = NULL;
1151
1152 /* turn usbatm_[rt]x_process into noop */
1153 /* no need to take the spinlock */
1154 INIT_LIST_HEAD(&instance->rx_channel.list);
1155 INIT_LIST_HEAD(&instance->tx_channel.list);
1156
1157 tasklet_enable(&instance->rx_channel.tasklet);
1158 tasklet_enable(&instance->tx_channel.tasklet);
1159
1160 for (i = 0; i < num_rcv_urbs + num_snd_urbs; i++) {
1161 kfree(instance->urbs[i]->transfer_buffer);
1162 usb_free_urb(instance->urbs[i]);
1163 }
1164
1165 /* ATM finalize */
1166 if (instance->atm_dev)
1167 shutdown_atm_dev(instance->atm_dev);
1168
1169 usbatm_put_instance(instance); /* taken in usbatm_usb_probe */
1170}
1171EXPORT_SYMBOL_GPL(usbatm_usb_disconnect);
1172
1173
1174/***********
1175** init **
1176***********/
1177
1178static int __init usbatm_usb_init(void)
1179{
1180 dbg("%s: driver version %s", __func__, DRIVER_VERSION);
1181
1182 if (sizeof(struct usbatm_control) > sizeof(((struct sk_buff *) 0)->cb)) {
1183 printk(KERN_ERR "%s unusable with this kernel!\n", usbatm_driver_name);
1184 return -EIO;
1185 }
1186
1187 if ((num_rcv_urbs > UDSL_MAX_RCV_URBS)
1188 || (num_snd_urbs > UDSL_MAX_SND_URBS)
1189 || (rcv_buf_size < 1)
1190 || (rcv_buf_size > UDSL_MAX_RCV_BUF_SIZE)
1191 || (snd_buf_size < 1)
1192 || (snd_buf_size > UDSL_MAX_SND_BUF_SIZE))
1193 return -EINVAL;
1194
1195 return 0;
1196}
1197module_init(usbatm_usb_init);
1198
1199static void __exit usbatm_usb_exit(void)
1200{
1201 dbg("%s", __func__);
1202}
1203module_exit(usbatm_usb_exit);
1204
1205MODULE_AUTHOR(DRIVER_AUTHOR);
1206MODULE_DESCRIPTION(DRIVER_DESC);
1207MODULE_LICENSE("GPL");
1208MODULE_VERSION(DRIVER_VERSION);
1209
1210/************
1211** debug **
1212************/
1213
1214#ifdef VERBOSE_DEBUG
1215static int usbatm_print_packet(const unsigned char *data, int len)
1216{
1217 unsigned char buffer[256];
1218 int i = 0, j = 0;
1219
1220 for (i = 0; i < len;) {
1221 buffer[0] = '\0';
1222 sprintf(buffer, "%.3d :", i);
1223 for (j = 0; (j < 16) && (i < len); j++, i++) {
1224 sprintf(buffer, "%s %2.2x", buffer, data[i]);
1225 }
1226 dbg("%s", buffer);
1227 }
1228 return i;
1229}
1230#endif
diff --git a/drivers/usb/atm/usbatm.h b/drivers/usb/atm/usbatm.h
new file mode 100644
index 000000000000..936646457935
--- /dev/null
+++ b/drivers/usb/atm/usbatm.h
@@ -0,0 +1,184 @@
1/******************************************************************************
2 * usbatm.h - Generic USB xDSL driver core
3 *
4 * Copyright (C) 2001, Alcatel
5 * Copyright (C) 2003, Duncan Sands, SolNegro, Josep Comas
6 * Copyright (C) 2004, David Woodhouse
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 2 of the License, or (at your option)
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * more details.
17 *
18 * You should have received a copy of the GNU General Public License along with
19 * this program; if not, write to the Free Software Foundation, Inc., 59
20 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 *
22 ******************************************************************************/
23
24#ifndef _USBATM_H_
25#define _USBATM_H_
26
27#include <linux/config.h>
28
29/*
30#define DEBUG
31#define VERBOSE_DEBUG
32*/
33
34#if !defined (DEBUG) && defined (CONFIG_USB_DEBUG)
35# define DEBUG
36#endif
37
38#include <asm/semaphore.h>
39#include <linux/atm.h>
40#include <linux/atmdev.h>
41#include <linux/completion.h>
42#include <linux/device.h>
43#include <linux/kref.h>
44#include <linux/list.h>
45#include <linux/stringify.h>
46#include <linux/usb.h>
47
48#ifdef DEBUG
49#define UDSL_ASSERT(x) BUG_ON(!(x))
50#else
51#define UDSL_ASSERT(x) do { if (!(x)) warn("failed assertion '%s' at line %d", __stringify(x), __LINE__); } while(0)
52#endif
53
54#define usb_err(instance, format, arg...) \
55 dev_err(&(instance)->usb_intf->dev , format , ## arg)
56#define usb_info(instance, format, arg...) \
57 dev_info(&(instance)->usb_intf->dev , format , ## arg)
58#define usb_warn(instance, format, arg...) \
59 dev_warn(&(instance)->usb_intf->dev , format , ## arg)
60#define usb_dbg(instance, format, arg...) \
61 dev_dbg(&(instance)->usb_intf->dev , format , ## arg)
62
63/* FIXME: move to dev_* once ATM is driver model aware */
64#define atm_printk(level, instance, format, arg...) \
65 printk(level "ATM dev %d: " format , \
66 (instance)->atm_dev->number , ## arg)
67
68#define atm_err(instance, format, arg...) \
69 atm_printk(KERN_ERR, instance , format , ## arg)
70#define atm_info(instance, format, arg...) \
71 atm_printk(KERN_INFO, instance , format , ## arg)
72#define atm_warn(instance, format, arg...) \
73 atm_printk(KERN_WARNING, instance , format , ## arg)
74#ifdef DEBUG
75#define atm_dbg(instance, format, arg...) \
76 atm_printk(KERN_DEBUG, instance , format , ## arg)
77#else
78#define atm_dbg(instance, format, arg...) \
79 do {} while (0)
80#endif
81
82
83/* mini driver */
84
85struct usbatm_data;
86
87/*
88* Assuming all methods exist and succeed, they are called in this order:
89*
90* bind, heavy_init, atm_start, ..., atm_stop, unbind
91*/
92
93struct usbatm_driver {
94 struct module *owner;
95
96 const char *driver_name;
97
98 /*
99 * init device ... can sleep, or cause probe() failure. Drivers with a heavy_init
100 * method can avoid having it called by setting need_heavy_init to zero.
101 */
102 int (*bind) (struct usbatm_data *, struct usb_interface *,
103 const struct usb_device_id *id, int *need_heavy_init);
104
105 /* additional device initialization that is too slow to be done in probe() */
106 int (*heavy_init) (struct usbatm_data *, struct usb_interface *);
107
108 /* cleanup device ... can sleep, but can't fail */
109 void (*unbind) (struct usbatm_data *, struct usb_interface *);
110
111 /* init ATM device ... can sleep, or cause ATM initialization failure */
112 int (*atm_start) (struct usbatm_data *, struct atm_dev *);
113
114 /* cleanup ATM device ... can sleep, but can't fail */
115 void (*atm_stop) (struct usbatm_data *, struct atm_dev *);
116
117 int in; /* rx endpoint */
118 int out; /* tx endpoint */
119
120 unsigned rx_padding;
121 unsigned tx_padding;
122};
123
124extern int usbatm_usb_probe(struct usb_interface *intf, const struct usb_device_id *id,
125 struct usbatm_driver *driver);
126extern void usbatm_usb_disconnect(struct usb_interface *intf);
127
128
129struct usbatm_channel {
130 int endpoint; /* usb pipe */
131 unsigned int stride; /* ATM cell size + padding */
132 unsigned int buf_size; /* urb buffer size */
133 spinlock_t lock;
134 struct list_head list;
135 struct tasklet_struct tasklet;
136 struct timer_list delay;
137 struct usbatm_data *usbatm;
138};
139
140/* main driver data */
141
142struct usbatm_data {
143 /******************
144 * public fields *
145 ******************/
146
147 /* mini driver */
148 struct usbatm_driver *driver;
149 void *driver_data;
150 char driver_name[16];
151
152 /* USB device */
153 struct usb_device *usb_dev;
154 struct usb_interface *usb_intf;
155 char description[64];
156
157 /* ATM device */
158 struct atm_dev *atm_dev;
159
160 /********************************
161 * private fields - do not use *
162 ********************************/
163
164 struct kref refcount;
165 struct semaphore serialize;
166
167 /* heavy init */
168 int thread_pid;
169 struct completion thread_started;
170 struct completion thread_exited;
171
172 /* ATM device */
173 struct list_head vcc_list;
174
175 struct usbatm_channel rx_channel;
176 struct usbatm_channel tx_channel;
177
178 struct sk_buff_head sndqueue;
179 struct sk_buff *current_skb; /* being emptied */
180
181 struct urb *urbs[0];
182};
183
184#endif /* _USBATM_H_ */
diff --git a/drivers/usb/atm/xusbatm.c b/drivers/usb/atm/xusbatm.c
new file mode 100644
index 000000000000..7fe7fb484d10
--- /dev/null
+++ b/drivers/usb/atm/xusbatm.c
@@ -0,0 +1,196 @@
1/******************************************************************************
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
64static int xusbatm_bind(struct usbatm_data *usbatm_instance,
65 struct usb_interface *intf, const struct usb_device_id *id,
66 int *need_heavy_init)
67{
68 struct usb_device *usb_dev = interface_to_usbdev(intf);
69 int drv_ix = id - xusbatm_usb_ids;
70 int rx_ep_present = usb_intf_has_ep(intf, rx_endpoint[drv_ix]);
71 int tx_ep_present = usb_intf_has_ep(intf, tx_endpoint[drv_ix]);
72 u8 searched_ep = rx_ep_present ? tx_endpoint[drv_ix] : rx_endpoint[drv_ix];
73 int i, ret;
74
75 usb_dbg(usbatm_instance, "%s: binding driver %d: vendor %#x product %#x"
76 " rx: ep %#x padd %d tx: ep %#x padd %d\n",
77 __func__, drv_ix, vendor[drv_ix], product[drv_ix],
78 rx_endpoint[drv_ix], rx_padding[drv_ix],
79 tx_endpoint[drv_ix], tx_padding[drv_ix]);
80
81 if (!rx_ep_present && !tx_ep_present) {
82 usb_dbg(usbatm_instance, "%s: intf #%d has neither rx (%#x) nor tx (%#x) endpoint\n",
83 __func__, intf->altsetting->desc.bInterfaceNumber,
84 rx_endpoint[drv_ix], tx_endpoint[drv_ix]);
85 return -ENODEV;
86 }
87
88 if (rx_ep_present && tx_ep_present)
89 return 0;
90
91 for(i = 0; i < usb_dev->actconfig->desc.bNumInterfaces; i++) {
92 struct usb_interface *cur_if = usb_dev->actconfig->interface[i];
93
94 if (cur_if != intf && usb_intf_has_ep(cur_if, searched_ep)) {
95 ret = usb_driver_claim_interface(&xusbatm_usb_driver,
96 cur_if, usbatm_instance);
97 if (!ret)
98 usb_err(usbatm_instance, "%s: failed to claim interface #%d (%d)\n",
99 __func__, cur_if->altsetting->desc.bInterfaceNumber, ret);
100 return ret;
101 }
102 }
103
104 usb_err(usbatm_instance, "%s: no interface has endpoint %#x\n",
105 __func__, searched_ep);
106 return -ENODEV;
107}
108
109static void xusbatm_unbind(struct usbatm_data *usbatm_instance,
110 struct usb_interface *intf)
111{
112 struct usb_device *usb_dev = interface_to_usbdev(intf);
113 int i;
114 usb_dbg(usbatm_instance, "%s entered\n", __func__);
115
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
123static int xusbatm_atm_start(struct usbatm_data *usbatm_instance,
124 struct atm_dev *atm_dev)
125{
126 atm_dbg(usbatm_instance, "%s entered\n", __func__);
127
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 = {
143 .owner = THIS_MODULE,
144 .name = xusbatm_driver_name,
145 .probe = xusbatm_usb_probe,
146 .disconnect = usbatm_usb_disconnect,
147 .id_table = xusbatm_usb_ids
148};
149
150static int __init xusbatm_init(void)
151{
152 int i;
153
154 dbg("xusbatm_init");
155
156 if (!num_vendor ||
157 num_vendor != num_product ||
158 num_vendor != num_rx_endpoint ||
159 num_vendor != num_tx_endpoint) {
160 warn("malformed module parameters");
161 return -EINVAL;
162 }
163
164 for (i = 0; i < num_vendor; i++) {
165 xusbatm_usb_ids[i].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
166 xusbatm_usb_ids[i].idVendor = vendor[i];
167 xusbatm_usb_ids[i].idProduct = product[i];
168
169
170 xusbatm_drivers[i].owner = THIS_MODULE;
171 xusbatm_drivers[i].driver_name = xusbatm_driver_name;
172 xusbatm_drivers[i].bind = xusbatm_bind;
173 xusbatm_drivers[i].unbind = xusbatm_unbind;
174 xusbatm_drivers[i].atm_start = xusbatm_atm_start;
175 xusbatm_drivers[i].in = rx_endpoint[i];
176 xusbatm_drivers[i].out = tx_endpoint[i];
177 xusbatm_drivers[i].rx_padding = rx_padding[i];
178 xusbatm_drivers[i].tx_padding = tx_padding[i];
179 }
180
181 return usb_register(&xusbatm_usb_driver);
182}
183module_init(xusbatm_init);
184
185static void __exit xusbatm_exit(void)
186{
187 dbg("xusbatm_exit entered");
188
189 usb_deregister(&xusbatm_usb_driver);
190}
191module_exit(xusbatm_exit);
192
193MODULE_AUTHOR("Roman Kagan, Duncan Sands");
194MODULE_DESCRIPTION("Driver for USB ADSL modems initialized in userspace");
195MODULE_LICENSE("GPL");
196MODULE_VERSION("0.1");