aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/usb/atm
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
committerLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
commit1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 (patch)
tree0bba044c4ce775e45a88a51686b5d9f90697ea9d /drivers/usb/atm
Linux-2.6.12-rc2v2.6.12-rc2
Initial git repository build. I'm not bothering with the full history, even though we have it. We can create a separate "historical" git archive of that later if we want to, and in the meantime it's about 3.2GB when imported into git - space that would just make the early git days unnecessarily complicated, when we don't have a lot of good infrastructure for it. Let it rip!
Diffstat (limited to 'drivers/usb/atm')
-rw-r--r--drivers/usb/atm/Kconfig30
-rw-r--r--drivers/usb/atm/Makefile7
-rw-r--r--drivers/usb/atm/speedtch.c847
-rw-r--r--drivers/usb/atm/usb_atm.c1188
-rw-r--r--drivers/usb/atm/usb_atm.h176
5 files changed, 2248 insertions, 0 deletions
diff --git a/drivers/usb/atm/Kconfig b/drivers/usb/atm/Kconfig
new file mode 100644
index 000000000000..0d9f5379b8cf
--- /dev/null
+++ b/drivers/usb/atm/Kconfig
@@ -0,0 +1,30 @@
1#
2# USB ATM driver configuration
3#
4comment "USB ATM/DSL drivers"
5 depends on USB
6
7config USB_ATM
8 tristate "Generic USB ATM/DSL core I/O support"
9 depends on USB && ATM
10 select CRC32
11 default n
12 help
13 This provides a library which is used for packet I/O by USB DSL
14 modems, such as the SpeedTouch driver below.
15
16 To compile this driver as a module, choose M here: the
17 module will be called usb_atm.
18
19config USB_SPEEDTOUCH
20 tristate "Alcatel Speedtouch USB support"
21 depends on USB && ATM
22 select USB_ATM
23 help
24 Say Y here if you have an Alcatel SpeedTouch USB or SpeedTouch 330
25 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
27 <http://www.linux-usb.org/SpeedTouch/> for details.
28
29 To compile this driver as a module, choose M here: the
30 module will be called speedtch.
diff --git a/drivers/usb/atm/Makefile b/drivers/usb/atm/Makefile
new file mode 100644
index 000000000000..9213b8b97587
--- /dev/null
+++ b/drivers/usb/atm/Makefile
@@ -0,0 +1,7 @@
1#
2# Makefile for the rest of the USB drivers
3# (the ones that don't fit into any other categories)
4#
5
6obj-$(CONFIG_USB_ATM) += usb_atm.o
7obj-$(CONFIG_USB_SPEEDTOUCH) += speedtch.o
diff --git a/drivers/usb/atm/speedtch.c b/drivers/usb/atm/speedtch.c
new file mode 100644
index 000000000000..233f9229badb
--- /dev/null
+++ b/drivers/usb/atm/speedtch.c
@@ -0,0 +1,847 @@
1/******************************************************************************
2 * speedtch.c - Alcatel SpeedTouch USB xDSL modem driver
3 *
4 * Copyright (C) 2001, Alcatel
5 * Copyright (C) 2003, Duncan Sands
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/module.h>
25#include <linux/moduleparam.h>
26#include <linux/gfp.h>
27#include <linux/kernel.h>
28#include <linux/sched.h>
29#include <linux/timer.h>
30#include <linux/errno.h>
31#include <linux/proc_fs.h>
32#include <linux/slab.h>
33#include <linux/wait.h>
34#include <linux/list.h>
35#include <asm/processor.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
47#if defined(CONFIG_FW_LOADER) || defined(CONFIG_FW_LOADER_MODULE)
48# define USE_FW_LOADER
49#endif
50
51#define DRIVER_AUTHOR "Johan Verrept, Duncan Sands <duncan.sands@free.fr>"
52#define DRIVER_VERSION "1.8"
53#define DRIVER_DESC "Alcatel SpeedTouch USB driver version " DRIVER_VERSION
54
55static const char speedtch_driver_name[] = "speedtch";
56
57#define SPEEDTOUCH_VENDORID 0x06b9
58#define SPEEDTOUCH_PRODUCTID 0x4061
59
60/* Timeout in jiffies */
61#define CTRL_TIMEOUT 2000
62#define DATA_TIMEOUT 2000
63
64#define OFFSET_7 0 /* size 1 */
65#define OFFSET_b 1 /* size 8 */
66#define OFFSET_d 9 /* size 4 */
67#define OFFSET_e 13 /* size 1 */
68#define OFFSET_f 14 /* size 1 */
69#define TOTAL 15
70
71#define SIZE_7 1
72#define SIZE_b 8
73#define SIZE_d 4
74#define SIZE_e 1
75#define SIZE_f 1
76
77static int dl_512_first = 0;
78static int sw_buffering = 0;
79
80module_param(dl_512_first, bool, 0444);
81MODULE_PARM_DESC(dl_512_first, "Read 512 bytes before sending firmware");
82
83module_param(sw_buffering, uint, 0444);
84MODULE_PARM_DESC(sw_buffering, "Enable software buffering");
85
86#define UDSL_IOCTL_LINE_UP 1
87#define UDSL_IOCTL_LINE_DOWN 2
88
89#define SPEEDTCH_ENDPOINT_INT 0x81
90#define SPEEDTCH_ENDPOINT_DATA 0x07
91#define SPEEDTCH_ENDPOINT_FIRMWARE 0x05
92
93#define hex2int(c) ( (c >= '0') && (c <= '9') ? (c - '0') : ((c & 0xf) + 9) )
94
95static struct usb_device_id speedtch_usb_ids[] = {
96 {USB_DEVICE(SPEEDTOUCH_VENDORID, SPEEDTOUCH_PRODUCTID)},
97 {}
98};
99
100MODULE_DEVICE_TABLE(usb, speedtch_usb_ids);
101
102struct speedtch_instance_data {
103 struct udsl_instance_data u;
104
105 /* Status */
106 struct urb *int_urb;
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
121static struct usb_driver speedtch_usb_driver = {
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};
129
130/***************
131** firmware **
132***************/
133
134static void speedtch_got_firmware(struct speedtch_instance_data *instance,
135 int got_it)
136{
137 int err;
138 struct usb_interface *intf;
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;
190
191 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
192 0x32, 0x40, state ? 0x01 : 0x00,
193 0x00, NULL, 0, 100);
194 if (ret < 0) {
195 printk("Warning: %sabling SW buffering: usb_control_msg returned %d\n",
196 state ? "En" : "Dis", ret);
197 return ret;
198 }
199
200 dbg("speedtch_set_swbuff: %sbled SW buffering", state ? "En" : "Dis");
201 return 0;
202}
203
204static void speedtch_test_sequence(struct speedtch_instance_data *instance)
205{
206 struct usb_device *dev = instance->u.usb_dev;
207 unsigned char buf[10];
208 int ret;
209
210 /* URB 147 */
211 buf[0] = 0x1c;
212 buf[1] = 0x50;
213 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
214 0x01, 0x40, 0x0b, 0x00, buf, 2, 100);
215 if (ret < 0)
216 printk(KERN_WARNING "%s failed on URB147: %d\n", __func__, ret);
217
218 /* URB 148 */
219 buf[0] = 0x32;
220 buf[1] = 0x00;
221 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
222 0x01, 0x40, 0x02, 0x00, buf, 2, 100);
223 if (ret < 0)
224 printk(KERN_WARNING "%s failed on URB148: %d\n", __func__, ret);
225
226 /* URB 149 */
227 buf[0] = 0x01;
228 buf[1] = 0x00;
229 buf[2] = 0x01;
230 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
231 0x01, 0x40, 0x03, 0x00, buf, 3, 100);
232 if (ret < 0)
233 printk(KERN_WARNING "%s failed on URB149: %d\n", __func__, ret);
234
235 /* URB 150 */
236 buf[0] = 0x01;
237 buf[1] = 0x00;
238 buf[2] = 0x01;
239 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
240 0x01, 0x40, 0x04, 0x00, buf, 3, 100);
241 if (ret < 0)
242 printk(KERN_WARNING "%s failed on URB150: %d\n", __func__, ret);
243}
244
245static int speedtch_start_synchro(struct speedtch_instance_data *instance)
246{
247 struct usb_device *dev = instance->u.usb_dev;
248 unsigned char buf[2];
249 int ret;
250
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) {
255 printk(KERN_WARNING "SpeedTouch: Failed to start ADSL synchronisation: %d\n", ret);
256 return ret;
257 }
258
259 dbg("speedtch_start_synchro: modem prodded. %d Bytes returned: %02x %02x", ret, buf[0], buf[1]);
260 return 0;
261}
262
263static void speedtch_handle_int(struct urb *urb, struct pt_regs *regs)
264{
265 struct speedtch_instance_data *instance = urb->context;
266 unsigned int count = urb->actual_length;
267 int ret;
268
269 /* The magic interrupt for "up state" */
270 const static unsigned char up_int[6] = { 0xa1, 0x00, 0x01, 0x00, 0x00, 0x00 };
271 /* The magic interrupt for "down state" */
272 const static unsigned char down_int[6] = { 0xa1, 0x00, 0x00, 0x00, 0x00, 0x00 };
273
274 switch (urb->status) {
275 case 0:
276 /* success */
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
289 if (count < 6) {
290 dbg("%s - int packet too short", __func__);
291 goto exit;
292 }
293
294 if (!memcmp(up_int, instance->int_data, 6)) {
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
302 printk(KERN_DEBUG "Unknown interrupt packet of %d bytes:", count);
303 for (i = 0; i < count; i++)
304 printk(" %02x", instance->int_data[i]);
305 printk("\n");
306 }
307 schedule_work(&instance->poll_work);
308
309 exit:
310 rmb();
311 if (!instance->int_urb)
312 return;
313
314 ret = usb_submit_urb(urb, GFP_ATOMIC);
315 if (ret)
316 err("%s - usb_submit_urb failed with result %d", __func__, ret);
317}
318
319static int speedtch_get_status(struct speedtch_instance_data *instance,
320 unsigned char *buf)
321{
322 struct usb_device *dev = instance->u.usb_dev;
323 int ret;
324
325 memset(buf, 0, TOTAL);
326
327 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
328 0x12, 0xc0, 0x07, 0x00, buf + OFFSET_7, SIZE_7,
329 CTRL_TIMEOUT);
330 if (ret < 0) {
331 dbg("MSG 7 failed");
332 return ret;
333 }
334
335 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
336 0x12, 0xc0, 0x0b, 0x00, buf + OFFSET_b, SIZE_b,
337 CTRL_TIMEOUT);
338 if (ret < 0) {
339 dbg("MSG B failed");
340 return ret;
341 }
342
343 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
344 0x12, 0xc0, 0x0d, 0x00, buf + OFFSET_d, SIZE_d,
345 CTRL_TIMEOUT);
346 if (ret < 0) {
347 dbg("MSG D failed");
348 return ret;
349 }
350
351 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
352 0x01, 0xc0, 0x0e, 0x00, buf + OFFSET_e, SIZE_e,
353 CTRL_TIMEOUT);
354 if (ret < 0) {
355 dbg("MSG E failed");
356 return ret;
357 }
358
359 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
360 0x01, 0xc0, 0x0f, 0x00, buf + OFFSET_f, SIZE_f,
361 CTRL_TIMEOUT);
362 if (ret < 0) {
363 dbg("MSG F failed");
364 return ret;
365 }
366
367 return 0;
368}
369
370static void speedtch_poll_status(struct speedtch_instance_data *instance)
371{
372 unsigned char buf[TOTAL];
373 int ret;
374
375 ret = speedtch_get_status(instance, buf);
376 if (ret) {
377 printk(KERN_WARNING
378 "SpeedTouch: Error %d fetching device status\n", ret);
379 return;
380 }
381
382 dbg("Line state %02x", buf[OFFSET_7]);
383
384 switch (buf[OFFSET_7]) {
385 case 0:
386 if (instance->u.atm_dev->signal != ATM_PHY_SIG_LOST) {
387 instance->u.atm_dev->signal = ATM_PHY_SIG_LOST;
388 printk(KERN_NOTICE "ADSL line is down\n");
389 }
390 break;
391
392 case 0x08:
393 if (instance->u.atm_dev->signal != ATM_PHY_SIG_UNKNOWN) {
394 instance->u.atm_dev->signal = ATM_PHY_SIG_UNKNOWN;
395 printk(KERN_NOTICE "ADSL line is blocked?\n");
396 }
397 break;
398
399 case 0x10:
400 if (instance->u.atm_dev->signal != ATM_PHY_SIG_LOST) {
401 instance->u.atm_dev->signal = ATM_PHY_SIG_LOST;
402 printk(KERN_NOTICE "ADSL line is synchronising\n");
403 }
404 break;
405
406 case 0x20:
407 if (instance->u.atm_dev->signal != ATM_PHY_SIG_FOUND) {
408 int down_speed = buf[OFFSET_b] | (buf[OFFSET_b + 1] << 8)
409 | (buf[OFFSET_b + 2] << 16) | (buf[OFFSET_b + 3] << 24);
410 int up_speed = buf[OFFSET_b + 4] | (buf[OFFSET_b + 5] << 8)
411 | (buf[OFFSET_b + 6] << 16) | (buf[OFFSET_b + 7] << 24);
412
413 if (!(down_speed & 0x0000ffff) &&
414 !(up_speed & 0x0000ffff)) {
415 down_speed >>= 16;
416 up_speed >>= 16;
417 }
418 instance->u.atm_dev->link_rate = down_speed * 1000 / 424;
419 instance->u.atm_dev->signal = ATM_PHY_SIG_FOUND;
420
421 printk(KERN_NOTICE
422 "ADSL line is up (%d Kib/s down | %d Kib/s up)\n",
423 down_speed, up_speed);
424 }
425 break;
426
427 default:
428 if (instance->u.atm_dev->signal != ATM_PHY_SIG_UNKNOWN) {
429 instance->u.atm_dev->signal = ATM_PHY_SIG_UNKNOWN;
430 printk(KERN_NOTICE "Unknown line state %02x\n", buf[OFFSET_7]);
431 }
432 break;
433 }
434}
435
436static void speedtch_timer_poll(unsigned long data)
437{
438 struct speedtch_instance_data *instance = (void *)data;
439
440 schedule_work(&instance->poll_work);
441 mod_timer(&instance->poll_timer, jiffies + (5 * HZ));
442}
443
444#ifdef USE_FW_LOADER
445static void speedtch_upload_firmware(struct speedtch_instance_data *instance,
446 const struct firmware *fw1,
447 const struct firmware *fw2)
448{
449 unsigned char *buffer;
450 struct usb_device *usb_dev = instance->u.usb_dev;
451 struct usb_interface *intf;
452 int actual_length, ret;
453 int offset;
454
455 dbg("speedtch_upload_firmware");
456
457 if (!(intf = usb_ifnum_to_if(usb_dev, 2))) {
458 dbg("speedtch_upload_firmware: interface not found!");
459 goto fail;
460 }
461
462 if (!(buffer = (unsigned char *)__get_free_page(GFP_KERNEL))) {
463 dbg("speedtch_upload_firmware: no memory for buffer!");
464 goto fail;
465 }
466
467 /* A user-space firmware loader may already have claimed interface #2 */
468 if ((ret =
469 usb_driver_claim_interface(&speedtch_usb_driver, intf, NULL)) < 0) {
470 dbg("speedtch_upload_firmware: interface in use (%d)!", ret);
471 goto fail_free;
472 }
473
474 /* URB 7 */
475 if (dl_512_first) { /* some modems need a read before writing the firmware */
476 ret = usb_bulk_msg(usb_dev, usb_rcvbulkpipe(usb_dev, SPEEDTCH_ENDPOINT_FIRMWARE),
477 buffer, 0x200, &actual_length, 2000);
478
479 if (ret < 0 && ret != -ETIMEDOUT)
480 dbg("speedtch_upload_firmware: read BLOCK0 from modem failed (%d)!", ret);
481 else
482 dbg("speedtch_upload_firmware: BLOCK0 downloaded (%d bytes)", ret);
483 }
484
485 /* URB 8 : both leds are static green */
486 for (offset = 0; offset < fw1->size; offset += PAGE_SIZE) {
487 int thislen = min_t(int, PAGE_SIZE, fw1->size - offset);
488 memcpy(buffer, fw1->data + offset, thislen);
489
490 ret = usb_bulk_msg(usb_dev, usb_sndbulkpipe(usb_dev, SPEEDTCH_ENDPOINT_FIRMWARE),
491 buffer, thislen, &actual_length, DATA_TIMEOUT);
492
493 if (ret < 0) {
494 dbg("speedtch_upload_firmware: write BLOCK1 to modem failed (%d)!", ret);
495 goto fail_release;
496 }
497 dbg("speedtch_upload_firmware: BLOCK1 uploaded (%zu bytes)", fw1->size);
498 }
499
500 /* USB led blinking green, ADSL led off */
501
502 /* URB 11 */
503 ret = usb_bulk_msg(usb_dev, usb_rcvbulkpipe(usb_dev, SPEEDTCH_ENDPOINT_FIRMWARE),
504 buffer, 0x200, &actual_length, DATA_TIMEOUT);
505
506 if (ret < 0) {
507 dbg("speedtch_upload_firmware: read BLOCK2 from modem failed (%d)!", ret);
508 goto fail_release;
509 }
510 dbg("speedtch_upload_firmware: BLOCK2 downloaded (%d bytes)", actual_length);
511
512 /* URBs 12 to 139 - USB led blinking green, ADSL led off */
513 for (offset = 0; offset < fw2->size; offset += PAGE_SIZE) {
514 int thislen = min_t(int, PAGE_SIZE, fw2->size - offset);
515 memcpy(buffer, fw2->data + offset, thislen);
516
517 ret = usb_bulk_msg(usb_dev, usb_sndbulkpipe(usb_dev, SPEEDTCH_ENDPOINT_FIRMWARE),
518 buffer, thislen, &actual_length, DATA_TIMEOUT);
519
520 if (ret < 0) {
521 dbg("speedtch_upload_firmware: write BLOCK3 to modem failed (%d)!", ret);
522 goto fail_release;
523 }
524 }
525 dbg("speedtch_upload_firmware: BLOCK3 uploaded (%zu bytes)", fw2->size);
526
527 /* USB led static green, ADSL led static red */
528
529 /* URB 142 */
530 ret = usb_bulk_msg(usb_dev, usb_rcvbulkpipe(usb_dev, SPEEDTCH_ENDPOINT_FIRMWARE),
531 buffer, 0x200, &actual_length, DATA_TIMEOUT);
532
533 if (ret < 0) {
534 dbg("speedtch_upload_firmware: read BLOCK4 from modem failed (%d)!", ret);
535 goto fail_release;
536 }
537
538 /* success */
539 dbg("speedtch_upload_firmware: BLOCK4 downloaded (%d bytes)", actual_length);
540
541 /* Delay to allow firmware to start up. We can do this here
542 because we're in our own kernel thread anyway. */
543 msleep(1000);
544
545 /* Enable software buffering, if requested */
546 if (sw_buffering)
547 speedtch_set_swbuff(instance, 1);
548
549 /* Magic spell; don't ask us what this does */
550 speedtch_test_sequence(instance);
551
552 /* Start modem synchronisation */
553 if (speedtch_start_synchro(instance))
554 dbg("speedtch_start_synchro: failed");
555
556 speedtch_got_firmware(instance, 1);
557
558 free_page((unsigned long)buffer);
559 return;
560
561 fail_release:
562 /* Only release interface #2 if uploading failed; we don't release it
563 we succeeded. This prevents the userspace tools from trying to load
564 the firmware themselves */
565 usb_driver_release_interface(&speedtch_usb_driver, intf);
566 fail_free:
567 free_page((unsigned long)buffer);
568 fail:
569 speedtch_got_firmware(instance, 0);
570}
571
572static int speedtch_find_firmware(struct speedtch_instance_data
573 *instance, int phase,
574 const struct firmware **fw_p)
575{
576 char buf[24];
577 const u16 bcdDevice = le16_to_cpu(instance->u.usb_dev->descriptor.bcdDevice);
578 const u8 major_revision = bcdDevice >> 8;
579 const u8 minor_revision = bcdDevice & 0xff;
580
581 sprintf(buf, "speedtch-%d.bin.%x.%02x", phase, major_revision, minor_revision);
582 dbg("speedtch_find_firmware: looking for %s", buf);
583
584 if (request_firmware(fw_p, buf, &instance->u.usb_dev->dev)) {
585 sprintf(buf, "speedtch-%d.bin.%x", phase, major_revision);
586 dbg("speedtch_find_firmware: looking for %s", buf);
587
588 if (request_firmware(fw_p, buf, &instance->u.usb_dev->dev)) {
589 sprintf(buf, "speedtch-%d.bin", phase);
590 dbg("speedtch_find_firmware: looking for %s", buf);
591
592 if (request_firmware(fw_p, buf, &instance->u.usb_dev->dev)) {
593 dev_warn(&instance->u.usb_dev->dev, "no stage %d firmware found!", phase);
594 return -ENOENT;
595 }
596 }
597 }
598
599 dev_info(&instance->u.usb_dev->dev, "found stage %d firmware %s\n", phase, buf);
600
601 return 0;
602}
603
604static int speedtch_load_firmware(void *arg)
605{
606 const struct firmware *fw1, *fw2;
607 struct speedtch_instance_data *instance = arg;
608
609 BUG_ON(!instance);
610
611 daemonize("firmware/speedtch");
612
613 if (!speedtch_find_firmware(instance, 1, &fw1)) {
614 if (!speedtch_find_firmware(instance, 2, &fw2)) {
615 speedtch_upload_firmware(instance, fw1, fw2);
616 release_firmware(fw2);
617 }
618 release_firmware(fw1);
619 }
620
621 /* In case we failed, set state back to NO_FIRMWARE so that
622 another later attempt may work. Otherwise, we never actually
623 manage to recover if, for example, the firmware is on /usr and
624 we look for it too early. */
625 speedtch_got_firmware(instance, 0);
626
627 module_put(THIS_MODULE);
628 udsl_put_instance(&instance->u);
629 return 0;
630}
631#endif /* USE_FW_LOADER */
632
633static void speedtch_firmware_start(struct speedtch_instance_data *instance)
634{
635#ifdef USE_FW_LOADER
636 int ret;
637#endif
638
639 dbg("speedtch_firmware_start");
640
641 down(&instance->u.serialize); /* vs self, speedtch_got_firmware */
642
643 if (instance->u.status >= UDSL_LOADING_FIRMWARE) {
644 up(&instance->u.serialize);
645 return;
646 }
647
648 instance->u.status = UDSL_LOADING_FIRMWARE;
649 up(&instance->u.serialize);
650
651#ifdef USE_FW_LOADER
652 udsl_get_instance(&instance->u);
653 try_module_get(THIS_MODULE);
654
655 ret = kernel_thread(speedtch_load_firmware, instance,
656 CLONE_FS | CLONE_FILES);
657
658 if (ret >= 0)
659 return; /* OK */
660
661 dbg("speedtch_firmware_start: kernel_thread failed (%d)!", ret);
662
663 module_put(THIS_MODULE);
664 udsl_put_instance(&instance->u);
665 /* Just pretend it never happened... hope modem_run happens */
666#endif /* USE_FW_LOADER */
667
668 speedtch_got_firmware(instance, 0);
669}
670
671static int speedtch_firmware_wait(struct udsl_instance_data *instance)
672{
673 speedtch_firmware_start((void *)instance);
674
675 if (wait_event_interruptible(instance->firmware_waiters, instance->status != UDSL_LOADING_FIRMWARE) < 0)
676 return -ERESTARTSYS;
677
678 return (instance->status == UDSL_LOADED_FIRMWARE) ? 0 : -EAGAIN;
679}
680
681/**********
682** USB **
683**********/
684
685static int speedtch_usb_ioctl(struct usb_interface *intf, unsigned int code,
686 void *user_data)
687{
688 struct speedtch_instance_data *instance = usb_get_intfdata(intf);
689
690 dbg("speedtch_usb_ioctl entered");
691
692 if (!instance) {
693 dbg("speedtch_usb_ioctl: NULL instance!");
694 return -ENODEV;
695 }
696
697 switch (code) {
698 case UDSL_IOCTL_LINE_UP:
699 instance->u.atm_dev->signal = ATM_PHY_SIG_FOUND;
700 speedtch_got_firmware(instance, 1);
701 return (instance->u.status == UDSL_LOADED_FIRMWARE) ? 0 : -EIO;
702 case UDSL_IOCTL_LINE_DOWN:
703 instance->u.atm_dev->signal = ATM_PHY_SIG_LOST;
704 return 0;
705 default:
706 return -ENOTTY;
707 }
708}
709
710static int speedtch_usb_probe(struct usb_interface *intf,
711 const struct usb_device_id *id)
712{
713 struct usb_device *dev = interface_to_usbdev(intf);
714 int ifnum = intf->altsetting->desc.bInterfaceNumber;
715 struct speedtch_instance_data *instance;
716 unsigned char mac_str[13];
717 int ret, i;
718 char buf7[SIZE_7];
719
720 dbg("speedtch_usb_probe: trying device with vendor=0x%x, product=0x%x, ifnum %d",
721 le16_to_cpu(dev->descriptor.idVendor),
722 le16_to_cpu(dev->descriptor.idProduct), ifnum);
723
724 if ((dev->descriptor.bDeviceClass != USB_CLASS_VENDOR_SPEC) ||
725 (ifnum != 1))
726 return -ENODEV;
727
728 dbg("speedtch_usb_probe: device accepted");
729
730 /* instance init */
731 instance = kmalloc(sizeof(*instance), GFP_KERNEL);
732 if (!instance) {
733 dbg("speedtch_usb_probe: no memory for instance data!");
734 return -ENOMEM;
735 }
736
737 memset(instance, 0, sizeof(struct speedtch_instance_data));
738
739 if ((ret = usb_set_interface(dev, 0, 0)) < 0)
740 goto fail;
741
742 if ((ret = usb_set_interface(dev, 2, 0)) < 0)
743 goto fail;
744
745 instance->u.data_endpoint = SPEEDTCH_ENDPOINT_DATA;
746 instance->u.firmware_wait = speedtch_firmware_wait;
747 instance->u.driver_name = speedtch_driver_name;
748
749 ret = udsl_instance_setup(dev, &instance->u);
750 if (ret)
751 goto fail;
752
753 init_timer(&instance->poll_timer);
754 instance->poll_timer.function = speedtch_timer_poll;
755 instance->poll_timer.data = (unsigned long)instance;
756
757 INIT_WORK(&instance->poll_work, (void *)speedtch_poll_status, instance);
758
759 /* set MAC address, it is stored in the serial number */
760 memset(instance->u.atm_dev->esi, 0, sizeof(instance->u.atm_dev->esi));
761 if (usb_string(dev, dev->descriptor.iSerialNumber, mac_str, sizeof(mac_str)) == 12) {
762 for (i = 0; i < 6; i++)
763 instance->u.atm_dev->esi[i] =
764 (hex2int(mac_str[i * 2]) * 16) + (hex2int(mac_str[i * 2 + 1]));
765 }
766
767 /* First check whether the modem already seems to be alive */
768 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
769 0x12, 0xc0, 0x07, 0x00, buf7, SIZE_7, 500);
770
771 if (ret == SIZE_7) {
772 dbg("firmware appears to be already loaded");
773 speedtch_got_firmware(instance, 1);
774 speedtch_poll_status(instance);
775 } else {
776 speedtch_firmware_start(instance);
777 }
778
779 usb_set_intfdata(intf, instance);
780
781 return 0;
782
783 fail:
784 kfree(instance);
785
786 return -ENOMEM;
787}
788
789static void speedtch_usb_disconnect(struct usb_interface *intf)
790{
791 struct speedtch_instance_data *instance = usb_get_intfdata(intf);
792
793 dbg("speedtch_usb_disconnect entered");
794
795 if (!instance) {
796 dbg("speedtch_usb_disconnect: NULL instance!");
797 return;
798 }
799
800/*QQ need to handle disconnects on interface #2 while uploading firmware */
801/*QQ and what about interface #1? */
802
803 if (instance->int_urb) {
804 struct urb *int_urb = instance->int_urb;
805 instance->int_urb = NULL;
806 wmb();
807 usb_unlink_urb(int_urb);
808 usb_free_urb(int_urb);
809 }
810
811 instance->int_data[0] = 1;
812 del_timer_sync(&instance->poll_timer);
813 wmb();
814 flush_scheduled_work();
815
816 udsl_instance_disconnect(&instance->u);
817
818 /* clean up */
819 usb_set_intfdata(intf, NULL);
820 udsl_put_instance(&instance->u);
821}
822
823/***********
824** init **
825***********/
826
827static int __init speedtch_usb_init(void)
828{
829 dbg("speedtch_usb_init: driver version " DRIVER_VERSION);
830
831 return usb_register(&speedtch_usb_driver);
832}
833
834static void __exit speedtch_usb_cleanup(void)
835{
836 dbg("speedtch_usb_cleanup entered");
837
838 usb_deregister(&speedtch_usb_driver);
839}
840
841module_init(speedtch_usb_init);
842module_exit(speedtch_usb_cleanup);
843
844MODULE_AUTHOR(DRIVER_AUTHOR);
845MODULE_DESCRIPTION(DRIVER_DESC);
846MODULE_LICENSE("GPL");
847MODULE_VERSION(DRIVER_VERSION);
diff --git a/drivers/usb/atm/usb_atm.c b/drivers/usb/atm/usb_atm.c
new file mode 100644
index 000000000000..a4cd4476d49a
--- /dev/null
+++ b/drivers/usb/atm/usb_atm.c
@@ -0,0 +1,1188 @@
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
new file mode 100644
index 000000000000..cf8c53283530
--- /dev/null
+++ b/drivers/usb/atm/usb_atm.h
@@ -0,0 +1,176 @@
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);