1 /******************************************************************************
2 * speedtouch.c - Alcatel SpeedTouch USB xDSL modem driver
3 *
4 * Copyright (C) 2001, Alcatel
5 * Copyright (C) 2003, Duncan Sands
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc., 59
19 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 *
21 ******************************************************************************/
22
23 /*
24 * Written by Johan Verrept, maintained by Duncan Sands (duncan.sands@free.fr)
25 *
26 * 1.7+: - See the check-in logs
27 *
28 * 1.6: - No longer opens a connection if the firmware is not loaded
29 * - Added support for the speedtouch 330
30 * - Removed the limit on the number of devices
31 * - Module now autoloads on device plugin
32 * - Merged relevant parts of sarlib
33 * - Replaced the kernel thread with a tasklet
34 * - New packet transmission code
35 * - Changed proc file contents
36 * - Fixed all known SMP races
37 * - Many fixes and cleanups
38 * - Various fixes by Oliver Neukum (oliver@neukum.name)
39 *
40 * 1.5A: - Backport of version for inclusion in 2.5 series kernel
41 * - Modifications by Richard Purdie (rpurdie@rpsys.net)
42 * - made compatible with kernel 2.5.6 onwards by changing
43 * udsl_usb_send_data_context->urb to a pointer and adding code
44 * to alloc and free it
45 * - remove_wait_queue() added to udsl_atm_processqueue_thread()
46 *
47 * 1.5: - fixed memory leak when atmsar_decode_aal5 returned NULL.
48 * (reported by stephen.robinson@zen.co.uk)
49 *
50 * 1.4: - changed the spin_lock() under interrupt to spin_lock_irqsave()
51 * - unlink all active send urbs of a vcc that is being closed.
52 *
53 * 1.3.1: - added the version number
54 *
55 * 1.3: - Added multiple send urb support
56 * - fixed memory leak and vcc->tx_inuse starvation bug
57 * when not enough memory left in vcc.
58 *
59 * 1.2: - Fixed race condition in udsl_usb_send_data()
60 * 1.1: - Turned off packet debugging
61 *
62 */
63
64 #include <asm/semaphore.h>
65 #include <linux/module.h>
66 #include <linux/kernel.h>
67 #include <linux/sched.h>
68 #include <linux/timer.h>
69 #include <linux/errno.h>
70 #include <linux/proc_fs.h>
71 #include <linux/slab.h>
72 #include <linux/list.h>
73 #include <asm/uaccess.h>
74 #include <linux/smp_lock.h>
75 #include <linux/interrupt.h>
76 #include <linux/atm.h>
77 #include <linux/atmdev.h>
78 #include <linux/crc32.h>
79 #include <linux/init.h>
80
81 /*
82 #define DEBUG
83 #define VERBOSE_DEBUG
84 */
85
86 #if !defined (DEBUG) && defined (CONFIG_USB_DEBUG)
87 # define DEBUG
88 #endif
89
90 #include <linux/usb.h>
91
92 #ifdef DEBUG
93 #define DEBUG_ON(x) BUG_ON(x)
94 #else
95 #define DEBUG_ON(x) do { if (x); } while (0)
96 #endif
97
98 #ifdef VERBOSE_DEBUG
99 static int udsl_print_packet (const unsigned char *data, int len);
100 #define PACKETDEBUG(arg...) udsl_print_packet (arg)
101 #define vdbg(arg...) dbg (arg)
102 #else
103 #define PACKETDEBUG(arg...)
104 #define vdbg(arg...)
105 #endif
106
107 #define DRIVER_AUTHOR "Johan Verrept, Duncan Sands <duncan.sands@free.fr>"
108 #define DRIVER_VERSION "1.8"
109 #define DRIVER_DESC "Alcatel SpeedTouch USB driver version " DRIVER_VERSION
110
111 static const char udsl_driver_name [] = "speedtch";
112
113 #define SPEEDTOUCH_VENDORID 0x06b9
114 #define SPEEDTOUCH_PRODUCTID 0x4061
115
116 #define UDSL_MAX_RCV_URBS 4
117 #define UDSL_MAX_SND_URBS 4
118 #define UDSL_MAX_RCV_BUFS 8
119 #define UDSL_MAX_SND_BUFS 8
120 #define UDSL_MAX_RCV_BUF_SIZE 1024 /* ATM cells */
121 #define UDSL_MAX_SND_BUF_SIZE 1024 /* ATM cells */
122 #define UDSL_DEFAULT_RCV_URBS 1
123 #define UDSL_DEFAULT_SND_URBS 1
124 #define UDSL_DEFAULT_RCV_BUFS 2
125 #define UDSL_DEFAULT_SND_BUFS 2
126 #define UDSL_DEFAULT_RCV_BUF_SIZE 64 /* ATM cells */
127 #define UDSL_DEFAULT_SND_BUF_SIZE 64 /* ATM cells */
128
129 static unsigned int num_rcv_urbs = UDSL_DEFAULT_RCV_URBS;
130 static unsigned int num_snd_urbs = UDSL_DEFAULT_SND_URBS;
131 static unsigned int num_rcv_bufs = UDSL_DEFAULT_RCV_BUFS;
132 static unsigned int num_snd_bufs = UDSL_DEFAULT_SND_BUFS;
133 static unsigned int rcv_buf_size = UDSL_DEFAULT_RCV_BUF_SIZE;
134 static unsigned int snd_buf_size = UDSL_DEFAULT_SND_BUF_SIZE;
135
136 MODULE_PARM (num_rcv_urbs, "i");
137 MODULE_PARM_DESC (num_rcv_urbs, "Number of urbs used for reception (range: 0-" __MODULE_STRING (UDSL_MAX_RCV_URBS) ", default: " __MODULE_STRING (UDSL_DEFAULT_RCV_URBS) ")");
138
139 MODULE_PARM (num_snd_urbs, "i");
140 MODULE_PARM_DESC (num_snd_urbs, "Number of urbs used for transmission (range: 0-" __MODULE_STRING (UDSL_MAX_SND_URBS) ", default: " __MODULE_STRING (UDSL_DEFAULT_SND_URBS) ")");
141
142 MODULE_PARM (num_rcv_bufs, "i");
143 MODULE_PARM_DESC (num_rcv_bufs, "Number of buffers used for reception (range: 0-" __MODULE_STRING (UDSL_MAX_RCV_BUFS) ", default: " __MODULE_STRING (UDSL_DEFAULT_RCV_BUFS) ")");
144
145 MODULE_PARM (num_snd_bufs, "i");
146 MODULE_PARM_DESC (num_snd_bufs, "Number of buffers used for transmission (range: 0-" __MODULE_STRING (UDSL_MAX_SND_BUFS) ", default: " __MODULE_STRING (UDSL_DEFAULT_SND_BUFS) ")");
147
148 MODULE_PARM (rcv_buf_size, "i");
149 MODULE_PARM_DESC (rcv_buf_size, "Size of the buffers used for reception (range: 0-" __MODULE_STRING (UDSL_MAX_RCV_BUF_SIZE) ", default: " __MODULE_STRING (UDSL_DEFAULT_RCV_BUF_SIZE) ")");
150
151 MODULE_PARM (snd_buf_size, "i");
152 MODULE_PARM_DESC (snd_buf_size, "Size of the buffers used for transmission (range: 0-" __MODULE_STRING (UDSL_MAX_SND_BUF_SIZE) ", default: " __MODULE_STRING (UDSL_DEFAULT_SND_BUF_SIZE) ")");
153
154 #define UDSL_IOCTL_LINE_UP 1
155 #define UDSL_IOCTL_LINE_DOWN 2
156
157 #define UDSL_ENDPOINT_DATA_OUT 0x07
158 #define UDSL_ENDPOINT_DATA_IN 0x87
159
160 #define ATM_CELL_HEADER (ATM_CELL_SIZE - ATM_CELL_PAYLOAD)
161 #define UDSL_NUM_CELLS(x) (((x) + ATM_AAL5_TRAILER + ATM_CELL_PAYLOAD - 1) / ATM_CELL_PAYLOAD)
162
163 #define hex2int(c) ( (c >= '0') && (c <= '9') ? (c - '0') : ((c & 0xf) + 9) )
164
165 static struct usb_device_id udsl_usb_ids [] = {
166 { USB_DEVICE (SPEEDTOUCH_VENDORID, SPEEDTOUCH_PRODUCTID) },
167 { }
168 };
169
170 MODULE_DEVICE_TABLE (usb, udsl_usb_ids);
171
172 /* receive */
173
174 struct udsl_receive_buffer {
175 struct list_head list;
176 unsigned char *base;
177 unsigned int filled_cells;
178 };
179
180 struct udsl_receiver {
181 struct list_head list;
182 struct udsl_receive_buffer *buffer;
183 struct urb *urb;
184 struct udsl_instance_data *instance;
185 };
186
187 struct udsl_vcc_data {
188 /* vpi/vci lookup */
189 struct list_head list;
190 short vpi;
191 int vci;
192 struct atm_vcc *vcc;
193
194 /* raw cell reassembly */
195 struct sk_buff *sarb;
196 };
197
198 /* send */
199
200 struct udsl_send_buffer {
201 struct list_head list;
202 unsigned char *base;
203 unsigned char *free_start;
204 unsigned int free_cells;
205 };
206
207 struct udsl_sender {
208 struct list_head list;
209 struct udsl_send_buffer *buffer;
210 struct urb *urb;
211 struct udsl_instance_data *instance;
212 };
213
214 struct udsl_control {
215 struct atm_skb_data atm_data;
216 unsigned int num_cells;
217 unsigned int num_entire;
218 unsigned int pdu_padding;
219 unsigned char cell_header [ATM_CELL_HEADER];
220 unsigned char aal5_trailer [ATM_AAL5_TRAILER];
221 };
222
223 #define UDSL_SKB(x) ((struct udsl_control *)(x)->cb)
224
225 /* main driver data */
226
227 struct udsl_instance_data {
228 struct semaphore serialize;
229
230 /* USB device part */
231 struct usb_device *usb_dev;
232 char description [64];
233 int firmware_loaded;
234
235 /* ATM device part */
236 struct atm_dev *atm_dev;
237 struct list_head vcc_list;
238
239 /* receive */
240 struct udsl_receiver receivers [UDSL_MAX_RCV_URBS];
241 struct udsl_receive_buffer receive_buffers [UDSL_MAX_RCV_BUFS];
242
243 spinlock_t receive_lock;
244 struct list_head spare_receivers;
245 struct list_head filled_receive_buffers;
246
247 struct tasklet_struct receive_tasklet;
248 struct list_head spare_receive_buffers;
249
250 /* send */
251 struct udsl_sender senders [UDSL_MAX_SND_URBS];
252 struct udsl_send_buffer send_buffers [UDSL_MAX_SND_BUFS];
253
254 struct sk_buff_head sndqueue;
255
256 spinlock_t send_lock;
257 struct list_head spare_senders;
258 struct list_head spare_send_buffers;
259
260 struct tasklet_struct send_tasklet;
261 struct sk_buff *current_skb; /* being emptied */
262 struct udsl_send_buffer *current_buffer; /* being filled */
263 struct list_head filled_send_buffers;
264 };
265
266 /* ATM */
267
268 static void udsl_atm_dev_close (struct atm_dev *dev);
269 static int udsl_atm_open (struct atm_vcc *vcc, short vpi, int vci);
270 static void udsl_atm_close (struct atm_vcc *vcc);
271 static int udsl_atm_ioctl (struct atm_dev *dev, unsigned int cmd, void *arg);
272 static int udsl_atm_send (struct atm_vcc *vcc, struct sk_buff *skb);
273 static int udsl_atm_proc_read (struct atm_dev *atm_dev, loff_t *pos, char *page);
274
275 static struct atmdev_ops udsl_atm_devops = {
276 dev_close: udsl_atm_dev_close,
277 open: udsl_atm_open,
278 close: udsl_atm_close,
279 ioctl: udsl_atm_ioctl,
280 send: udsl_atm_send,
281 proc_read: udsl_atm_proc_read,
282 owner: THIS_MODULE,
283 };
284
285 /* USB */
286
287 static void *udsl_usb_probe (struct usb_device *dev, unsigned int ifnum, const struct usb_device_id *id);
288 static void udsl_usb_disconnect (struct usb_device *dev, void *ptr);
289 static int udsl_usb_ioctl (struct usb_device *hub, unsigned int code, void *user_data);
290
291 static struct usb_driver udsl_usb_driver = {
292 owner: THIS_MODULE,
293 name: udsl_driver_name,
294 probe: udsl_usb_probe,
295 disconnect: udsl_usb_disconnect,
296 ioctl: udsl_usb_ioctl,
297 id_table: udsl_usb_ids,
298 };
299
300
301 /***********
302 ** misc **
303 ***********/
304
udsl_pop(struct atm_vcc * vcc,struct sk_buff * skb)305 static inline void udsl_pop (struct atm_vcc *vcc, struct sk_buff *skb)
306 {
307 if (vcc->pop)
308 vcc->pop (vcc, skb);
309 else
310 dev_kfree_skb (skb);
311 }
312
313
314 /*************
315 ** decode **
316 *************/
317
udsl_find_vcc(struct udsl_instance_data * instance,short vpi,int vci)318 static inline struct udsl_vcc_data *udsl_find_vcc (struct udsl_instance_data *instance, short vpi, int vci)
319 {
320 struct udsl_vcc_data *vcc;
321
322 list_for_each_entry (vcc, &instance->vcc_list, list)
323 if ((vcc->vci == vci) && (vcc->vpi == vpi))
324 return vcc;
325 return NULL;
326 }
327
udsl_extract_cells(struct udsl_instance_data * instance,unsigned char * source,unsigned int howmany)328 static void udsl_extract_cells (struct udsl_instance_data *instance, unsigned char *source, unsigned int howmany)
329 {
330 struct udsl_vcc_data *cached_vcc = NULL;
331 struct atm_vcc *vcc;
332 struct sk_buff *sarb;
333 struct udsl_vcc_data *vcc_data;
334 int cached_vci = 0;
335 unsigned int i;
336 int pti;
337 int vci;
338 short cached_vpi = 0;
339 short vpi;
340
341 for (i = 0; i < howmany; i++, source += ATM_CELL_SIZE) {
342 vpi = ((source [0] & 0x0f) << 4) | (source [1] >> 4);
343 vci = ((source [1] & 0x0f) << 12) | (source [2] << 4) | (source [3] >> 4);
344 pti = (source [3] & 0x2) != 0;
345
346 vdbg ("udsl_extract_cells: vpi %hd, vci %d, pti %d", vpi, vci, pti);
347
348 if (cached_vcc && (vci == cached_vci) && (vpi == cached_vpi))
349 vcc_data = cached_vcc;
350 else if ((vcc_data = udsl_find_vcc (instance, vpi, vci))) {
351 cached_vcc = vcc_data;
352 cached_vpi = vpi;
353 cached_vci = vci;
354 } else {
355 dbg ("udsl_extract_cells: unknown vpi/vci (%hd/%d)!", vpi, vci);
356 continue;
357 }
358
359 vcc = vcc_data->vcc;
360 sarb = vcc_data->sarb;
361
362 if (sarb->tail + ATM_CELL_PAYLOAD > sarb->end) {
363 dbg ("udsl_extract_cells: buffer overrun (sarb->len %u, vcc: 0x%p)!", sarb->len, vcc);
364 /* discard cells already received */
365 skb_trim (sarb, 0);
366 }
367
368 memcpy (sarb->tail, source + ATM_CELL_HEADER, ATM_CELL_PAYLOAD);
369 __skb_put (sarb, ATM_CELL_PAYLOAD);
370
371 if (pti) {
372 struct sk_buff *skb;
373 unsigned int length;
374 unsigned int pdu_length;
375
376 length = (source [ATM_CELL_SIZE - 6] << 8) + source [ATM_CELL_SIZE - 5];
377
378 /* guard against overflow */
379 if (length > ATM_MAX_AAL5_PDU) {
380 dbg ("udsl_extract_cells: bogus length %u (vcc: 0x%p)!", length, vcc);
381 atomic_inc (&vcc->stats->rx_err);
382 goto out;
383 }
384
385 pdu_length = UDSL_NUM_CELLS (length) * ATM_CELL_PAYLOAD;
386
387 if (sarb->len < pdu_length) {
388 dbg ("udsl_extract_cells: bogus pdu_length %u (sarb->len: %u, vcc: 0x%p)!", pdu_length, sarb->len, vcc);
389 atomic_inc (&vcc->stats->rx_err);
390 goto out;
391 }
392
393 if (crc32_be (~0, sarb->tail - pdu_length, pdu_length) != 0xc704dd7b) {
394 dbg ("udsl_extract_cells: packet failed crc check (vcc: 0x%p)!", vcc);
395 atomic_inc (&vcc->stats->rx_err);
396 goto out;
397 }
398
399 vdbg ("udsl_extract_cells: got packet (length: %u, pdu_length: %u, vcc: 0x%p)", length, pdu_length, vcc);
400
401 if (!(skb = dev_alloc_skb (length))) {
402 dbg ("udsl_extract_cells: no memory for skb (length: %u)!", length);
403 atomic_inc (&vcc->stats->rx_drop);
404 goto out;
405 }
406
407 vdbg ("udsl_extract_cells: allocated new sk_buff (skb: 0x%p, skb->truesize: %u)", skb, skb->truesize);
408
409 if (!atm_charge (vcc, skb->truesize)) {
410 dbg ("udsl_extract_cells: failed atm_charge (skb->truesize: %u)!", skb->truesize);
411 dev_kfree_skb (skb);
412 goto out; /* atm_charge increments rx_drop */
413 }
414
415 memcpy (skb->data, sarb->tail - pdu_length, length);
416 __skb_put (skb, length);
417
418 vdbg ("udsl_extract_cells: sending skb 0x%p, skb->len %u, skb->truesize %u", skb, skb->len, skb->truesize);
419
420 PACKETDEBUG (skb->data, skb->len);
421
422 vcc->push (vcc, skb);
423
424 atomic_inc (&vcc->stats->rx);
425 out:
426 skb_trim (sarb, 0);
427 }
428 }
429 }
430
431
432 /*************
433 ** encode **
434 *************/
435
436 static const unsigned char zeros [ATM_CELL_PAYLOAD];
437
udsl_groom_skb(struct atm_vcc * vcc,struct sk_buff * skb)438 static void udsl_groom_skb (struct atm_vcc *vcc, struct sk_buff *skb)
439 {
440 struct udsl_control *ctrl = UDSL_SKB (skb);
441 unsigned int zero_padding;
442 u32 crc;
443
444 ctrl->atm_data.vcc = vcc;
445 ctrl->cell_header [0] = vcc->vpi >> 4;
446 ctrl->cell_header [1] = (vcc->vpi << 4) | (vcc->vci >> 12);
447 ctrl->cell_header [2] = vcc->vci >> 4;
448 ctrl->cell_header [3] = vcc->vci << 4;
449 ctrl->cell_header [4] = 0xec;
450
451 ctrl->num_cells = UDSL_NUM_CELLS (skb->len);
452 ctrl->num_entire = skb->len / ATM_CELL_PAYLOAD;
453
454 zero_padding = ctrl->num_cells * ATM_CELL_PAYLOAD - skb->len - ATM_AAL5_TRAILER;
455
456 if (ctrl->num_entire + 1 < ctrl->num_cells)
457 ctrl->pdu_padding = zero_padding - (ATM_CELL_PAYLOAD - ATM_AAL5_TRAILER);
458 else
459 ctrl->pdu_padding = zero_padding;
460
461 ctrl->aal5_trailer [0] = 0; /* UU = 0 */
462 ctrl->aal5_trailer [1] = 0; /* CPI = 0 */
463 ctrl->aal5_trailer [2] = skb->len >> 8;
464 ctrl->aal5_trailer [3] = skb->len;
465
466 crc = crc32_be (~0, skb->data, skb->len);
467 crc = crc32_be (crc, zeros, zero_padding);
468 crc = crc32_be (crc, ctrl->aal5_trailer, 4);
469 crc = ~crc;
470
471 ctrl->aal5_trailer [4] = crc >> 24;
472 ctrl->aal5_trailer [5] = crc >> 16;
473 ctrl->aal5_trailer [6] = crc >> 8;
474 ctrl->aal5_trailer [7] = crc;
475 }
476
udsl_write_cells(unsigned int howmany,struct sk_buff * skb,unsigned char ** target_p)477 static unsigned int udsl_write_cells (unsigned int howmany, struct sk_buff *skb, unsigned char **target_p)
478 {
479 struct udsl_control *ctrl = UDSL_SKB (skb);
480 unsigned char *target = *target_p;
481 unsigned int nc, ne, i;
482
483 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);
484
485 nc = ctrl->num_cells;
486 ne = min (howmany, ctrl->num_entire);
487
488 for (i = 0; i < ne; i++) {
489 memcpy (target, ctrl->cell_header, ATM_CELL_HEADER);
490 target += ATM_CELL_HEADER;
491 memcpy (target, skb->data, ATM_CELL_PAYLOAD);
492 target += ATM_CELL_PAYLOAD;
493 __skb_pull (skb, ATM_CELL_PAYLOAD);
494 }
495
496 ctrl->num_entire -= ne;
497
498 if (!(ctrl->num_cells -= ne) || !(howmany -= ne))
499 goto out;
500
501 memcpy (target, ctrl->cell_header, ATM_CELL_HEADER);
502 target += ATM_CELL_HEADER;
503 memcpy (target, skb->data, skb->len);
504 target += skb->len;
505 __skb_pull (skb, skb->len);
506 memset (target, 0, ctrl->pdu_padding);
507 target += ctrl->pdu_padding;
508
509 if (--ctrl->num_cells) {
510 if (!--howmany) {
511 ctrl->pdu_padding = ATM_CELL_PAYLOAD - ATM_AAL5_TRAILER;
512 goto out;
513 }
514
515 memcpy (target, ctrl->cell_header, ATM_CELL_HEADER);
516 target += ATM_CELL_HEADER;
517 memset (target, 0, ATM_CELL_PAYLOAD - ATM_AAL5_TRAILER);
518 target += ATM_CELL_PAYLOAD - ATM_AAL5_TRAILER;
519
520 DEBUG_ON (--ctrl->num_cells);
521 }
522
523 memcpy (target, ctrl->aal5_trailer, ATM_AAL5_TRAILER);
524 target += ATM_AAL5_TRAILER;
525 /* set pti bit in last cell */
526 *(target + 3 - ATM_CELL_SIZE) |= 0x2;
527
528 out:
529 *target_p = target;
530 return nc - ctrl->num_cells;
531 }
532
533
534 /**************
535 ** receive **
536 **************/
537
udsl_complete_receive(struct urb * urb)538 static void udsl_complete_receive (struct urb *urb)
539 {
540 struct udsl_receive_buffer *buf;
541 struct udsl_instance_data *instance;
542 struct udsl_receiver *rcv;
543 unsigned long flags;
544
545 if (!urb || !(rcv = urb->context)) {
546 dbg ("udsl_complete_receive: bad urb!");
547 return;
548 }
549
550 instance = rcv->instance;
551 buf = rcv->buffer;
552
553 buf->filled_cells = urb->actual_length / ATM_CELL_SIZE;
554
555 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);
556
557 DEBUG_ON (buf->filled_cells > rcv_buf_size);
558
559 /* may not be in_interrupt() */
560 spin_lock_irqsave (&instance->receive_lock, flags);
561 list_add (&rcv->list, &instance->spare_receivers);
562 list_add_tail (&buf->list, &instance->filled_receive_buffers);
563 if (likely (!urb->status))
564 tasklet_schedule (&instance->receive_tasklet);
565 spin_unlock_irqrestore (&instance->receive_lock, flags);
566 }
567
udsl_process_receive(unsigned long data)568 static void udsl_process_receive (unsigned long data)
569 {
570 struct udsl_receive_buffer *buf;
571 struct udsl_instance_data *instance = (struct udsl_instance_data *) data;
572 struct udsl_receiver *rcv;
573 int err;
574
575 made_progress:
576 while (!list_empty (&instance->spare_receive_buffers)) {
577 spin_lock_irq (&instance->receive_lock);
578 if (list_empty (&instance->spare_receivers)) {
579 spin_unlock_irq (&instance->receive_lock);
580 break;
581 }
582 rcv = list_entry (instance->spare_receivers.next, struct udsl_receiver, list);
583 list_del (&rcv->list);
584 spin_unlock_irq (&instance->receive_lock);
585
586 buf = list_entry (instance->spare_receive_buffers.next, struct udsl_receive_buffer, list);
587 list_del (&buf->list);
588
589 rcv->buffer = buf;
590
591 FILL_BULK_URB (rcv->urb,
592 instance->usb_dev,
593 usb_rcvbulkpipe (instance->usb_dev, UDSL_ENDPOINT_DATA_IN),
594 buf->base,
595 rcv_buf_size * ATM_CELL_SIZE,
596 udsl_complete_receive,
597 rcv);
598 rcv->urb->transfer_flags |= USB_QUEUE_BULK;
599
600 vdbg ("udsl_process_receive: sending urb 0x%p, rcv 0x%p, buf 0x%p", rcv->urb, rcv, buf);
601
602 if ((err = usb_submit_urb(rcv->urb)) < 0) {
603 dbg ("udsl_process_receive: urb submission failed (%d)!", err);
604 list_add (&buf->list, &instance->spare_receive_buffers);
605 spin_lock_irq (&instance->receive_lock);
606 list_add (&rcv->list, &instance->spare_receivers);
607 spin_unlock_irq (&instance->receive_lock);
608 break;
609 }
610 }
611
612 spin_lock_irq (&instance->receive_lock);
613 if (list_empty (&instance->filled_receive_buffers)) {
614 spin_unlock_irq (&instance->receive_lock);
615 return; /* done - no more buffers */
616 }
617 buf = list_entry (instance->filled_receive_buffers.next, struct udsl_receive_buffer, list);
618 list_del (&buf->list);
619 spin_unlock_irq (&instance->receive_lock);
620 vdbg ("udsl_process_receive: processing buf 0x%p", buf);
621 udsl_extract_cells (instance, buf->base, buf->filled_cells);
622 list_add (&buf->list, &instance->spare_receive_buffers);
623 goto made_progress;
624 }
625
626
627 /***********
628 ** send **
629 ***********/
630
udsl_complete_send(struct urb * urb)631 static void udsl_complete_send (struct urb *urb)
632 {
633 struct udsl_instance_data *instance;
634 struct udsl_sender *snd;
635 unsigned long flags;
636
637 if (!urb || !(snd = urb->context) || !(instance = snd->instance)) {
638 dbg ("udsl_complete_send: bad urb!");
639 return;
640 }
641
642 vdbg ("udsl_complete_send: urb 0x%p, status %d, snd 0x%p, buf 0x%p", urb, urb->status, snd, snd->buffer);
643
644 /* may not be in_interrupt() */
645 spin_lock_irqsave (&instance->send_lock, flags);
646 list_add (&snd->list, &instance->spare_senders);
647 list_add (&snd->buffer->list, &instance->spare_send_buffers);
648 tasklet_schedule (&instance->send_tasklet);
649 spin_unlock_irqrestore (&instance->send_lock, flags);
650 }
651
udsl_process_send(unsigned long data)652 static void udsl_process_send (unsigned long data)
653 {
654 struct udsl_send_buffer *buf;
655 struct udsl_instance_data *instance = (struct udsl_instance_data *) data;
656 struct sk_buff *skb;
657 struct udsl_sender *snd;
658 int err;
659 unsigned int num_written;
660
661 made_progress:
662 spin_lock_irq (&instance->send_lock);
663 while (!list_empty (&instance->spare_senders)) {
664 if (!list_empty (&instance->filled_send_buffers)) {
665 buf = list_entry (instance->filled_send_buffers.next, struct udsl_send_buffer, list);
666 list_del (&buf->list);
667 } else if ((buf = instance->current_buffer)) {
668 instance->current_buffer = NULL;
669 } else /* all buffers empty */
670 break;
671
672 snd = list_entry (instance->spare_senders.next, struct udsl_sender, list);
673 list_del (&snd->list);
674 spin_unlock_irq (&instance->send_lock);
675
676 snd->buffer = buf;
677 FILL_BULK_URB (snd->urb,
678 instance->usb_dev,
679 usb_sndbulkpipe (instance->usb_dev, UDSL_ENDPOINT_DATA_OUT),
680 buf->base,
681 (snd_buf_size - buf->free_cells) * ATM_CELL_SIZE,
682 udsl_complete_send,
683 snd);
684 snd->urb->transfer_flags |= USB_QUEUE_BULK;
685
686 vdbg ("udsl_process_send: submitting urb 0x%p (%d cells), snd 0x%p, buf 0x%p", snd->urb, snd_buf_size - buf->free_cells, snd, buf);
687
688 if ((err = usb_submit_urb(snd->urb)) < 0) {
689 dbg ("udsl_process_send: urb submission failed (%d)!", err);
690 spin_lock_irq (&instance->send_lock);
691 list_add (&snd->list, &instance->spare_senders);
692 spin_unlock_irq (&instance->send_lock);
693 list_add (&buf->list, &instance->filled_send_buffers);
694 return; /* bail out */
695 }
696
697 spin_lock_irq (&instance->send_lock);
698 } /* while */
699 spin_unlock_irq (&instance->send_lock);
700
701 if (!instance->current_skb && !(instance->current_skb = skb_dequeue (&instance->sndqueue)))
702 return; /* done - no more skbs */
703
704 skb = instance->current_skb;
705
706 if (!(buf = instance->current_buffer)) {
707 spin_lock_irq (&instance->send_lock);
708 if (list_empty (&instance->spare_send_buffers)) {
709 instance->current_buffer = NULL;
710 spin_unlock_irq (&instance->send_lock);
711 return; /* done - no more buffers */
712 }
713 buf = list_entry (instance->spare_send_buffers.next, struct udsl_send_buffer, list);
714 list_del (&buf->list);
715 spin_unlock_irq (&instance->send_lock);
716
717 buf->free_start = buf->base;
718 buf->free_cells = snd_buf_size;
719
720 instance->current_buffer = buf;
721 }
722
723 num_written = udsl_write_cells (buf->free_cells, skb, &buf->free_start);
724
725 vdbg ("udsl_process_send: wrote %u cells from skb 0x%p to buffer 0x%p", num_written, skb, buf);
726
727 if (!(buf->free_cells -= num_written)) {
728 list_add_tail (&buf->list, &instance->filled_send_buffers);
729 instance->current_buffer = NULL;
730 }
731
732 vdbg ("udsl_process_send: buffer contains %d cells, %d left", snd_buf_size - buf->free_cells, buf->free_cells);
733
734 if (!UDSL_SKB (skb)->num_cells) {
735 struct atm_vcc *vcc = UDSL_SKB (skb)->atm_data.vcc;
736
737 udsl_pop (vcc, skb);
738 instance->current_skb = NULL;
739
740 atomic_inc (&vcc->stats->tx);
741 }
742
743 goto made_progress;
744 }
745
udsl_cancel_send(struct udsl_instance_data * instance,struct atm_vcc * vcc)746 static void udsl_cancel_send (struct udsl_instance_data *instance, struct atm_vcc *vcc)
747 {
748 struct sk_buff *skb, *n;
749
750 dbg ("udsl_cancel_send entered");
751 spin_lock_irq (&instance->sndqueue.lock);
752 for (skb = instance->sndqueue.next, n = skb->next; skb != (struct sk_buff *)&instance->sndqueue; skb = n, n = skb->next)
753 if (UDSL_SKB (skb)->atm_data.vcc == vcc) {
754 dbg ("udsl_cancel_send: popping skb 0x%p", skb);
755 __skb_unlink (skb, &instance->sndqueue);
756 udsl_pop (vcc, skb);
757 }
758 spin_unlock_irq (&instance->sndqueue.lock);
759
760 tasklet_disable (&instance->send_tasklet);
761 if ((skb = instance->current_skb) && (UDSL_SKB (skb)->atm_data.vcc == vcc)) {
762 dbg ("udsl_cancel_send: popping current skb (0x%p)", skb);
763 instance->current_skb = NULL;
764 udsl_pop (vcc, skb);
765 }
766 tasklet_enable (&instance->send_tasklet);
767 dbg ("udsl_cancel_send done");
768 }
769
udsl_atm_send(struct atm_vcc * vcc,struct sk_buff * skb)770 static int udsl_atm_send (struct atm_vcc *vcc, struct sk_buff *skb)
771 {
772 struct udsl_instance_data *instance = vcc->dev->dev_data;
773 int err;
774
775 vdbg ("udsl_atm_send called (skb 0x%p, len %u)", skb, skb->len);
776
777 if (!instance || !instance->usb_dev) {
778 dbg ("udsl_atm_send: NULL data!");
779 err = -ENODEV;
780 goto fail;
781 }
782
783 if (vcc->qos.aal != ATM_AAL5) {
784 dbg ("udsl_atm_send: unsupported ATM type %d!", vcc->qos.aal);
785 err = -EINVAL;
786 goto fail;
787 }
788
789 if (skb->len > ATM_MAX_AAL5_PDU) {
790 dbg ("udsl_atm_send: packet too long (%d vs %d)!", skb->len, ATM_MAX_AAL5_PDU);
791 err = -EINVAL;
792 goto fail;
793 }
794
795 PACKETDEBUG (skb->data, skb->len);
796
797 udsl_groom_skb (vcc, skb);
798 skb_queue_tail (&instance->sndqueue, skb);
799 tasklet_schedule (&instance->send_tasklet);
800
801 return 0;
802
803 fail:
804 udsl_pop (vcc, skb);
805 return err;
806 }
807
808
809 /**********
810 ** ATM **
811 **********/
812
udsl_atm_dev_close(struct atm_dev * dev)813 static void udsl_atm_dev_close (struct atm_dev *dev)
814 {
815 struct udsl_instance_data *instance = dev->dev_data;
816
817 if (!instance) {
818 dbg ("udsl_atm_dev_close: NULL instance!");
819 return;
820 }
821
822 dbg ("udsl_atm_dev_close: queue has %u elements", instance->sndqueue.qlen);
823
824 tasklet_kill (&instance->receive_tasklet);
825 tasklet_kill (&instance->send_tasklet);
826 kfree (instance);
827 dev->dev_data = NULL;
828 }
829
udsl_atm_proc_read(struct atm_dev * atm_dev,loff_t * pos,char * page)830 static int udsl_atm_proc_read (struct atm_dev *atm_dev, loff_t *pos, char *page)
831 {
832 struct udsl_instance_data *instance = atm_dev->dev_data;
833 int left = *pos;
834
835 if (!instance) {
836 dbg ("udsl_atm_proc_read: NULL instance!");
837 return -ENODEV;
838 }
839
840 if (!left--)
841 return sprintf (page, "%s\n", instance->description);
842
843 if (!left--)
844 return sprintf (page, "MAC: %02x:%02x:%02x:%02x:%02x:%02x\n",
845 atm_dev->esi [0], atm_dev->esi [1], atm_dev->esi [2],
846 atm_dev->esi [3], atm_dev->esi [4], atm_dev->esi [5]);
847
848 if (!left--)
849 return sprintf (page, "AAL5: tx %d ( %d err ), rx %d ( %d err, %d drop )\n",
850 atomic_read (&atm_dev->stats.aal5.tx),
851 atomic_read (&atm_dev->stats.aal5.tx_err),
852 atomic_read (&atm_dev->stats.aal5.rx),
853 atomic_read (&atm_dev->stats.aal5.rx_err),
854 atomic_read (&atm_dev->stats.aal5.rx_drop));
855
856 if (!left--) {
857 switch (atm_dev->signal) {
858 case ATM_PHY_SIG_FOUND:
859 sprintf (page, "Line up");
860 break;
861 case ATM_PHY_SIG_LOST:
862 sprintf (page, "Line down");
863 break;
864 default:
865 sprintf (page, "Line state unknown");
866 break;
867 }
868
869 if (instance->usb_dev) {
870 if (!instance->firmware_loaded)
871 strcat (page, ", no firmware\n");
872 else
873 strcat (page, ", firmware loaded\n");
874 } else
875 strcat (page, ", disconnected\n");
876
877 return strlen (page);
878 }
879
880 return 0;
881 }
882
udsl_atm_open(struct atm_vcc * vcc,short vpi,int vci)883 static int udsl_atm_open (struct atm_vcc *vcc, short vpi, int vci)
884 {
885 struct udsl_instance_data *instance = vcc->dev->dev_data;
886 struct udsl_vcc_data *new;
887 unsigned int max_pdu;
888
889 dbg ("udsl_atm_open: vpi %hd, vci %d", vpi, vci);
890
891 if (!instance || !instance->usb_dev) {
892 dbg ("udsl_atm_open: NULL data!");
893 return -ENODEV;
894 }
895
896 if ((vpi == ATM_VPI_ANY) || (vci == ATM_VCI_ANY))
897 return -EINVAL;
898
899 /* only support AAL5 */
900 if ((vcc->qos.aal != ATM_AAL5) || (vcc->qos.rxtp.max_sdu < 0) || (vcc->qos.rxtp.max_sdu > ATM_MAX_AAL5_PDU)) {
901 dbg ("udsl_atm_open: unsupported ATM type %d!", vcc->qos.aal);
902 return -EINVAL;
903 }
904
905 if (!instance->firmware_loaded) {
906 dbg ("udsl_atm_open: firmware not loaded!");
907 return -EAGAIN;
908 }
909
910 down (&instance->serialize); /* vs self, udsl_atm_close */
911
912 if (udsl_find_vcc (instance, vpi, vci)) {
913 dbg ("udsl_atm_open: %hd/%d already in use!", vpi, vci);
914 up (&instance->serialize);
915 return -EADDRINUSE;
916 }
917
918 if (!(new = kmalloc (sizeof (struct udsl_vcc_data), GFP_KERNEL))) {
919 dbg ("udsl_atm_open: no memory for vcc_data!");
920 up (&instance->serialize);
921 return -ENOMEM;
922 }
923
924 memset (new, 0, sizeof (struct udsl_vcc_data));
925 new->vcc = vcc;
926 new->vpi = vpi;
927 new->vci = vci;
928
929 /* udsl_extract_cells requires at least one cell */
930 max_pdu = max (1, UDSL_NUM_CELLS (vcc->qos.rxtp.max_sdu)) * ATM_CELL_PAYLOAD;
931 if (!(new->sarb = alloc_skb (max_pdu, GFP_KERNEL))) {
932 dbg ("udsl_atm_open: no memory for SAR buffer!");
933 kfree (new);
934 up (&instance->serialize);
935 return -ENOMEM;
936 }
937
938 vcc->dev_data = new;
939 vcc->vpi = vpi;
940 vcc->vci = vci;
941
942 tasklet_disable (&instance->receive_tasklet);
943 list_add (&new->list, &instance->vcc_list);
944 tasklet_enable (&instance->receive_tasklet);
945
946 set_bit (ATM_VF_ADDR, &vcc->flags);
947 set_bit (ATM_VF_PARTIAL, &vcc->flags);
948 set_bit (ATM_VF_READY, &vcc->flags);
949
950 up (&instance->serialize);
951
952 tasklet_schedule (&instance->receive_tasklet);
953
954 dbg ("udsl_atm_open: allocated vcc data 0x%p (max_pdu: %u)", new, max_pdu);
955
956 return 0;
957 }
958
udsl_atm_close(struct atm_vcc * vcc)959 static void udsl_atm_close (struct atm_vcc *vcc)
960 {
961 struct udsl_instance_data *instance = vcc->dev->dev_data;
962 struct udsl_vcc_data *vcc_data = vcc->dev_data;
963
964 dbg ("udsl_atm_close called");
965
966 if (!instance || !vcc_data) {
967 dbg ("udsl_atm_close: NULL data!");
968 return;
969 }
970
971 dbg ("udsl_atm_close: deallocating vcc 0x%p with vpi %d vci %d", vcc_data, vcc_data->vpi, vcc_data->vci);
972
973 udsl_cancel_send (instance, vcc);
974
975 down (&instance->serialize); /* vs self, udsl_atm_open */
976
977 tasklet_disable (&instance->receive_tasklet);
978 list_del (&vcc_data->list);
979 tasklet_enable (&instance->receive_tasklet);
980
981 kfree_skb (vcc_data->sarb);
982 vcc_data->sarb = NULL;
983
984 kfree (vcc_data);
985 vcc->dev_data = NULL;
986
987 vcc->vpi = ATM_VPI_UNSPEC;
988 vcc->vci = ATM_VCI_UNSPEC;
989 clear_bit (ATM_VF_READY, &vcc->flags);
990 clear_bit (ATM_VF_PARTIAL, &vcc->flags);
991 clear_bit (ATM_VF_ADDR, &vcc->flags);
992
993 up (&instance->serialize);
994
995 dbg ("udsl_atm_close successful");
996 }
997
udsl_atm_ioctl(struct atm_dev * dev,unsigned int cmd,void * arg)998 static int udsl_atm_ioctl (struct atm_dev *dev, unsigned int cmd, void *arg)
999 {
1000 switch (cmd) {
1001 case ATM_QUERYLOOP:
1002 return put_user (ATM_LM_NONE, (int *) arg) ? -EFAULT : 0;
1003 default:
1004 return -ENOIOCTLCMD;
1005 }
1006 }
1007
1008
1009 /**********
1010 ** USB **
1011 **********/
1012
udsl_set_alternate(struct udsl_instance_data * instance)1013 static int udsl_set_alternate (struct udsl_instance_data *instance)
1014 {
1015 down (&instance->serialize); /* vs self */
1016 if (!instance->firmware_loaded) {
1017 int ret;
1018
1019 if ((ret = usb_set_interface (instance->usb_dev, 1, 1)) < 0) {
1020 dbg ("udsl_set_alternate: usb_set_interface returned %d!", ret);
1021 up (&instance->serialize);
1022 return ret;
1023 }
1024 instance->firmware_loaded = 1;
1025 }
1026 up (&instance->serialize);
1027
1028 tasklet_schedule (&instance->receive_tasklet);
1029
1030 return 0;
1031 }
1032
udsl_usb_ioctl(struct usb_device * dev,unsigned int code,void * user_data)1033 static int udsl_usb_ioctl (struct usb_device *dev, unsigned int code, void *user_data)
1034 {
1035 struct usb_interface *intf = usb_ifnum_to_if (dev, 1);
1036 struct udsl_instance_data *instance = intf->private_data;
1037
1038 dbg ("udsl_usb_ioctl entered");
1039
1040 if (!instance) {
1041 dbg ("udsl_usb_ioctl: NULL instance!");
1042 return -ENODEV;
1043 }
1044
1045 switch (code) {
1046 case UDSL_IOCTL_LINE_UP:
1047 instance->atm_dev->signal = ATM_PHY_SIG_FOUND;
1048 return udsl_set_alternate (instance);
1049 case UDSL_IOCTL_LINE_DOWN:
1050 instance->atm_dev->signal = ATM_PHY_SIG_LOST;
1051 return 0;
1052 default:
1053 return -ENOTTY;
1054 }
1055 }
1056
udsl_usb_probe(struct usb_device * dev,unsigned int ifnum,const struct usb_device_id * id)1057 static void *udsl_usb_probe (struct usb_device *dev, unsigned int ifnum, const struct usb_device_id *id)
1058 {
1059 struct udsl_instance_data *instance;
1060 unsigned char mac_str [13];
1061 int i, length;
1062 char *buf;
1063
1064 dbg ("udsl_usb_probe: trying device with vendor=0x%x, product=0x%x, ifnum %d",
1065 dev->descriptor.idVendor, dev->descriptor.idProduct, ifnum);
1066
1067 if ((dev->descriptor.bDeviceClass != USB_CLASS_VENDOR_SPEC) ||
1068 (dev->descriptor.idVendor != SPEEDTOUCH_VENDORID) ||
1069 (dev->descriptor.idProduct != SPEEDTOUCH_PRODUCTID) || (ifnum != 1))
1070 return NULL;
1071
1072 dbg ("udsl_usb_probe: device accepted");
1073
1074 /* instance init */
1075 if (!(instance = kmalloc (sizeof (struct udsl_instance_data), GFP_KERNEL))) {
1076 dbg ("udsl_usb_probe: no memory for instance data!");
1077 return NULL;
1078 }
1079
1080 memset (instance, 0, sizeof (struct udsl_instance_data));
1081
1082 init_MUTEX (&instance->serialize);
1083
1084 instance->usb_dev = dev;
1085
1086 INIT_LIST_HEAD (&instance->vcc_list);
1087
1088 spin_lock_init (&instance->receive_lock);
1089 INIT_LIST_HEAD (&instance->spare_receivers);
1090 INIT_LIST_HEAD (&instance->filled_receive_buffers);
1091
1092 tasklet_init (&instance->receive_tasklet, udsl_process_receive, (unsigned long) instance);
1093 INIT_LIST_HEAD (&instance->spare_receive_buffers);
1094
1095 skb_queue_head_init (&instance->sndqueue);
1096
1097 spin_lock_init (&instance->send_lock);
1098 INIT_LIST_HEAD (&instance->spare_senders);
1099 INIT_LIST_HEAD (&instance->spare_send_buffers);
1100
1101 tasklet_init (&instance->send_tasklet, udsl_process_send, (unsigned long) instance);
1102 INIT_LIST_HEAD (&instance->filled_send_buffers);
1103
1104 /* receive init */
1105 for (i = 0; i < num_rcv_urbs; i++) {
1106 struct udsl_receiver *rcv = &(instance->receivers [i]);
1107
1108 if (!(rcv->urb = usb_alloc_urb (0))) {
1109 dbg ("udsl_usb_probe: no memory for receive urb %d!", i);
1110 goto fail;
1111 }
1112
1113 rcv->instance = instance;
1114
1115 list_add (&rcv->list, &instance->spare_receivers);
1116 }
1117
1118 for (i = 0; i < num_rcv_bufs; i++) {
1119 struct udsl_receive_buffer *buf = &(instance->receive_buffers [i]);
1120
1121 if (!(buf->base = kmalloc (rcv_buf_size * ATM_CELL_SIZE, GFP_KERNEL))) {
1122 dbg ("udsl_usb_probe: no memory for receive buffer %d!", i);
1123 goto fail;
1124 }
1125
1126 list_add (&buf->list, &instance->spare_receive_buffers);
1127 }
1128
1129 /* send init */
1130 for (i = 0; i < num_snd_urbs; i++) {
1131 struct udsl_sender *snd = &(instance->senders [i]);
1132
1133 if (!(snd->urb = usb_alloc_urb (0))) {
1134 dbg ("udsl_usb_probe: no memory for send urb %d!", i);
1135 goto fail;
1136 }
1137
1138 snd->instance = instance;
1139
1140 list_add (&snd->list, &instance->spare_senders);
1141 }
1142
1143 for (i = 0; i < num_snd_bufs; i++) {
1144 struct udsl_send_buffer *buf = &(instance->send_buffers [i]);
1145
1146 if (!(buf->base = kmalloc (snd_buf_size * ATM_CELL_SIZE, GFP_KERNEL))) {
1147 dbg ("udsl_usb_probe: no memory for send buffer %d!", i);
1148 goto fail;
1149 }
1150
1151 list_add (&buf->list, &instance->spare_send_buffers);
1152 }
1153
1154 /* ATM init */
1155 if (!(instance->atm_dev = atm_dev_register (udsl_driver_name, &udsl_atm_devops, -1, 0))) {
1156 dbg ("udsl_usb_probe: failed to register ATM device!");
1157 goto fail;
1158 }
1159
1160 instance->atm_dev->ci_range.vpi_bits = ATM_CI_MAX;
1161 instance->atm_dev->ci_range.vci_bits = ATM_CI_MAX;
1162 instance->atm_dev->signal = ATM_PHY_SIG_UNKNOWN;
1163
1164 /* temp init ATM device, set to 128kbit */
1165 instance->atm_dev->link_rate = 128 * 1000 / 424;
1166
1167 /* set MAC address, it is stored in the serial number */
1168 memset (instance->atm_dev->esi, 0, sizeof (instance->atm_dev->esi));
1169 if (usb_string (dev, dev->descriptor.iSerialNumber, mac_str, sizeof (mac_str)) == 12)
1170 for (i = 0; i < 6; i++)
1171 instance->atm_dev->esi [i] = (hex2int (mac_str [i * 2]) * 16) + (hex2int (mac_str [i * 2 + 1]));
1172
1173 /* device description */
1174 buf = instance->description;
1175 length = sizeof (instance->description);
1176
1177 if ((i = usb_string (dev, dev->descriptor.iProduct, buf, length)) < 0)
1178 goto finish;
1179
1180 buf += i;
1181 length -= i;
1182
1183 i = snprintf (buf, length, " (");
1184 buf += i;
1185 length -= i;
1186
1187 if (length <= 0 || (i = usb_make_path (dev, buf, length)) < 0)
1188 goto finish;
1189
1190 buf += i;
1191 length -= i;
1192
1193 snprintf (buf, length, ")");
1194
1195 finish:
1196 /* ready for ATM callbacks */
1197 wmb ();
1198 instance->atm_dev->dev_data = instance;
1199
1200 return instance;
1201
1202 fail:
1203 for (i = 0; i < num_snd_bufs; i++)
1204 kfree (instance->send_buffers [i].base);
1205
1206 for (i = 0; i < num_snd_urbs; i++)
1207 usb_free_urb (instance->senders [i].urb);
1208
1209 for (i = 0; i < num_rcv_bufs; i++)
1210 kfree (instance->receive_buffers [i].base);
1211
1212 for (i = 0; i < num_rcv_urbs; i++)
1213 usb_free_urb (instance->receivers [i].urb);
1214
1215 kfree (instance);
1216
1217 return NULL;
1218 }
1219
udsl_usb_disconnect(struct usb_device * dev,void * ptr)1220 static void udsl_usb_disconnect (struct usb_device *dev, void *ptr)
1221 {
1222 struct udsl_instance_data *instance = ptr;
1223 struct list_head *pos;
1224 unsigned int count;
1225 int result, i;
1226
1227 dbg ("udsl_usb_disconnect entered");
1228
1229 if (!instance) {
1230 dbg ("udsl_usb_disconnect: NULL instance!");
1231 return;
1232 }
1233
1234 /* receive finalize */
1235 tasklet_disable (&instance->receive_tasklet);
1236
1237 for (i = 0; i < num_rcv_urbs; i++)
1238 if ((result = usb_unlink_urb (instance->receivers [i].urb)) < 0)
1239 dbg ("udsl_usb_disconnect: usb_unlink_urb on receive urb %d returned %d!", i, result);
1240
1241 /* wait for completion handlers to finish */
1242 do {
1243 count = 0;
1244 spin_lock_irq (&instance->receive_lock);
1245 list_for_each (pos, &instance->spare_receivers)
1246 DEBUG_ON (++count > num_rcv_urbs);
1247 spin_unlock_irq (&instance->receive_lock);
1248
1249 dbg ("udsl_usb_disconnect: found %u spare receivers", count);
1250
1251 if (count == num_rcv_urbs)
1252 break;
1253
1254 set_current_state (TASK_RUNNING);
1255 schedule ();
1256 } while (1);
1257
1258 /* no need to take the spinlock */
1259 INIT_LIST_HEAD (&instance->filled_receive_buffers);
1260 INIT_LIST_HEAD (&instance->spare_receive_buffers);
1261
1262 tasklet_enable (&instance->receive_tasklet);
1263
1264 for (i = 0; i < num_rcv_urbs; i++)
1265 usb_free_urb (instance->receivers [i].urb);
1266
1267 for (i = 0; i < num_rcv_bufs; i++)
1268 kfree (instance->receive_buffers [i].base);
1269
1270 /* send finalize */
1271 tasklet_disable (&instance->send_tasklet);
1272
1273 for (i = 0; i < num_snd_urbs; i++)
1274 if ((result = usb_unlink_urb (instance->senders [i].urb)) < 0)
1275 dbg ("udsl_usb_disconnect: usb_unlink_urb on send urb %d returned %d!", i, result);
1276
1277 /* wait for completion handlers to finish */
1278 do {
1279 count = 0;
1280 spin_lock_irq (&instance->send_lock);
1281 list_for_each (pos, &instance->spare_senders)
1282 DEBUG_ON (++count > num_snd_urbs);
1283 spin_unlock_irq (&instance->send_lock);
1284
1285 dbg ("udsl_usb_disconnect: found %u spare senders", count);
1286
1287 if (count == num_snd_urbs)
1288 break;
1289
1290 set_current_state (TASK_RUNNING);
1291 schedule ();
1292 } while (1);
1293
1294 /* no need to take the spinlock */
1295 INIT_LIST_HEAD (&instance->spare_senders);
1296 INIT_LIST_HEAD (&instance->spare_send_buffers);
1297 instance->current_buffer = NULL;
1298
1299 tasklet_enable (&instance->send_tasklet);
1300
1301 for (i = 0; i < num_snd_urbs; i++)
1302 usb_free_urb (instance->senders [i].urb);
1303
1304 for (i = 0; i < num_snd_bufs; i++)
1305 kfree (instance->send_buffers [i].base);
1306
1307 wmb ();
1308 instance->usb_dev = NULL;
1309
1310 /* ATM finalize */
1311 shutdown_atm_dev (instance->atm_dev); /* frees instance, kills tasklets */
1312 }
1313
1314
1315 /***********
1316 ** init **
1317 ***********/
1318
udsl_usb_init(void)1319 static int __init udsl_usb_init (void)
1320 {
1321 dbg ("udsl_usb_init: driver version " DRIVER_VERSION);
1322
1323 if (sizeof (struct udsl_control) > sizeof (((struct sk_buff *)0)->cb)) {
1324 printk (KERN_ERR __FILE__ ": unusable with this kernel!\n");
1325 return -EIO;
1326 }
1327
1328 if ((num_rcv_urbs > UDSL_MAX_RCV_URBS) || (num_snd_urbs > UDSL_MAX_SND_URBS) ||
1329 (num_rcv_bufs > UDSL_MAX_RCV_BUFS) || (num_snd_bufs > UDSL_MAX_SND_BUFS) ||
1330 (rcv_buf_size > UDSL_MAX_RCV_BUF_SIZE) || (snd_buf_size > UDSL_MAX_SND_BUF_SIZE))
1331 return -EINVAL;
1332
1333 return usb_register (&udsl_usb_driver);
1334 }
1335
udsl_usb_cleanup(void)1336 static void __exit udsl_usb_cleanup (void)
1337 {
1338 dbg ("udsl_usb_cleanup entered");
1339
1340 usb_deregister (&udsl_usb_driver);
1341 }
1342
1343 module_init (udsl_usb_init);
1344 module_exit (udsl_usb_cleanup);
1345
1346 MODULE_AUTHOR (DRIVER_AUTHOR);
1347 MODULE_DESCRIPTION (DRIVER_DESC);
1348 MODULE_LICENSE ("GPL");
1349
1350
1351 /************
1352 ** debug **
1353 ************/
1354
1355 #ifdef VERBOSE_DEBUG
udsl_print_packet(const unsigned char * data,int len)1356 static int udsl_print_packet (const unsigned char *data, int len)
1357 {
1358 unsigned char buffer [256];
1359 int i = 0, j = 0;
1360
1361 for (i = 0; i < len;) {
1362 buffer [0] = '\0';
1363 sprintf (buffer, "%.3d :", i);
1364 for (j = 0; (j < 16) && (i < len); j++, i++) {
1365 sprintf (buffer, "%s %2.2x", buffer, data [i]);
1366 }
1367 dbg ("%s", buffer);
1368 }
1369 return i;
1370 }
1371 #endif
1372