1 /******************************************************************************
2 * usb_atm.c - Generic USB xDSL driver core
4 * Copyright (C) 2001, Alcatel
5 * Copyright (C) 2003, Duncan Sands, SolNegro, Josep Comas
6 * Copyright (C) 2004, David Woodhouse
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)
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
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.
22 ******************************************************************************/
25 * Written by Johan Verrept, maintained by Duncan Sands (duncan.sands@free.fr)
27 * 1.7+: - See the check-in logs
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)
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()
48 * 1.5: - fixed memory leak when atmsar_decode_aal5 returned NULL.
49 * (reported by stephen.robinson@zen.co.uk)
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.
54 * 1.3.1: - added the version number
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.
60 * 1.2: - Fixed race condition in udsl_usb_send_data()
61 * 1.1: - Turned off packet debugging
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>
87 static int udsl_print_packet(const unsigned char *data, int len);
88 #define PACKETDEBUG(arg...) udsl_print_packet (arg)
89 #define vdbg(arg...) dbg (arg)
91 #define PACKETDEBUG(arg...)
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
99 static unsigned int num_rcv_urbs = UDSL_DEFAULT_RCV_URBS;
100 static unsigned int num_snd_urbs = UDSL_DEFAULT_SND_URBS;
101 static unsigned int num_rcv_bufs = UDSL_DEFAULT_RCV_BUFS;
102 static unsigned int num_snd_bufs = UDSL_DEFAULT_SND_BUFS;
103 static unsigned int rcv_buf_size = UDSL_DEFAULT_RCV_BUF_SIZE;
104 static unsigned int snd_buf_size = UDSL_DEFAULT_SND_BUF_SIZE;
106 module_param(num_rcv_urbs, uint, 0444);
107 MODULE_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) ")");
112 module_param(num_snd_urbs, uint, 0444);
113 MODULE_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) ")");
118 module_param(num_rcv_bufs, uint, 0444);
119 MODULE_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) ")");
124 module_param(num_snd_bufs, uint, 0444);
125 MODULE_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) ")");
130 module_param(rcv_buf_size, uint, 0444);
131 MODULE_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) ")");
136 module_param(snd_buf_size, uint, 0444);
137 MODULE_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) ")");
144 static void udsl_atm_dev_close(struct atm_dev *dev);
145 static int udsl_atm_open(struct atm_vcc *vcc);
146 static void udsl_atm_close(struct atm_vcc *vcc);
147 static int udsl_atm_ioctl(struct atm_dev *dev, unsigned int cmd, void __user * arg);
148 static int udsl_atm_send(struct atm_vcc *vcc, struct sk_buff *skb);
149 static int udsl_atm_proc_read(struct atm_dev *atm_dev, loff_t * pos, char *page);
151 static 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,
165 static inline void udsl_pop(struct atm_vcc *vcc, struct sk_buff *skb)
177 static inline struct udsl_vcc_data *udsl_find_vcc(struct udsl_instance_data *instance,
180 struct udsl_vcc_data *vcc;
182 list_for_each_entry(vcc, &instance->vcc_list, list)
183 if ((vcc->vci == vci) && (vcc->vpi == vpi))
188 static void udsl_extract_cells(struct udsl_instance_data *instance,
189 unsigned char *source, unsigned int howmany)
191 struct udsl_vcc_data *cached_vcc = NULL;
193 struct sk_buff *sarb;
194 struct udsl_vcc_data *vcc_data;
199 short cached_vpi = 0;
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;
208 vdbg("udsl_extract_cells: vpi %hd, vci %d, pti %d", vpi, vci, pti);
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;
217 dbg("udsl_extract_cells: unknown vpi/vci (%hd/%d)!", vpi, vci);
222 sarb = vcc_data->sarb;
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 */
230 memcpy(sarb->tail, source + ATM_CELL_HEADER, ATM_CELL_PAYLOAD);
231 __skb_put(sarb, ATM_CELL_PAYLOAD);
236 unsigned int pdu_length;
238 length = (source[ATM_CELL_SIZE - 6] << 8) + source[ATM_CELL_SIZE - 5];
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);
247 pdu_length = UDSL_NUM_CELLS(length) * ATM_CELL_PAYLOAD;
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);
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);
261 vdbg("udsl_extract_cells: got packet (length: %u, pdu_length: %u, vcc: 0x%p)", length, pdu_length, vcc);
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);
269 vdbg("udsl_extract_cells: allocated new sk_buff (skb: 0x%p, skb->truesize: %u)", skb, skb->truesize);
271 if (!atm_charge(vcc, skb->truesize)) {
272 dbg("udsl_extract_cells: failed atm_charge (skb->truesize: %u)!", skb->truesize);
274 goto out; /* atm_charge increments rx_drop */
277 memcpy(skb->data, sarb->tail - pdu_length, length);
278 __skb_put(skb, length);
280 vdbg("udsl_extract_cells: sending skb 0x%p, skb->len %u, skb->truesize %u", skb, skb->len, skb->truesize);
282 PACKETDEBUG(skb->data, skb->len);
286 atomic_inc(&vcc->stats->rx);
297 static inline void udsl_fill_cell_header(unsigned char *target, struct atm_vcc *vcc)
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;
306 static const unsigned char zeros[ATM_CELL_PAYLOAD];
308 static void udsl_groom_skb(struct atm_vcc *vcc, struct sk_buff *skb)
310 struct udsl_control *ctrl = UDSL_SKB(skb);
311 unsigned int zero_padding;
314 ctrl->atm_data.vcc = vcc;
316 ctrl->num_cells = UDSL_NUM_CELLS(skb->len);
317 ctrl->num_entire = skb->len / ATM_CELL_PAYLOAD;
319 zero_padding = ctrl->num_cells * ATM_CELL_PAYLOAD - skb->len - ATM_AAL5_TRAILER;
321 if (ctrl->num_entire + 1 < ctrl->num_cells)
322 ctrl->pdu_padding = zero_padding - (ATM_CELL_PAYLOAD - ATM_AAL5_TRAILER);
324 ctrl->pdu_padding = zero_padding;
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;
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);
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;
342 static unsigned int udsl_write_cells(struct udsl_instance_data *instance,
343 unsigned int howmany, struct sk_buff *skb,
344 unsigned char **target_p)
346 struct udsl_control *ctrl = UDSL_SKB(skb);
347 unsigned char *target = *target_p;
348 unsigned int nc, ne, i;
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);
352 nc = ctrl->num_cells;
353 ne = min(howmany, ctrl->num_entire);
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;
364 __skb_pull(skb, ATM_CELL_PAYLOAD);
367 ctrl->num_entire -= ne;
369 if (!(ctrl->num_cells -= ne) || !(howmany -= ne))
372 udsl_fill_cell_header(target, ctrl->atm_data.vcc);
373 target += ATM_CELL_HEADER;
374 memcpy(target, skb->data, skb->len);
376 __skb_pull(skb, skb->len);
377 memset(target, 0, ctrl->pdu_padding);
378 target += ctrl->pdu_padding;
380 if (--ctrl->num_cells) {
382 ctrl->pdu_padding = ATM_CELL_PAYLOAD - ATM_AAL5_TRAILER;
386 if (instance->snd_padding) {
387 memset(target, 0, instance->snd_padding);
388 target += instance->snd_padding;
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;
396 UDSL_ASSERT(!ctrl->num_cells);
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;
409 return nc - ctrl->num_cells;
416 static void udsl_complete_receive(struct urb *urb, struct pt_regs *regs)
418 struct udsl_receive_buffer *buf;
419 struct udsl_instance_data *instance;
420 struct udsl_receiver *rcv;
423 if (!urb || !(rcv = urb->context)) {
424 dbg("udsl_complete_receive: bad urb!");
428 instance = rcv->instance;
431 buf->filled_cells = urb->actual_length / (ATM_CELL_SIZE + instance->rcv_padding);
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);
435 UDSL_ASSERT(buf->filled_cells <= rcv_buf_size);
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);
446 static void udsl_process_receive(unsigned long data)
448 struct udsl_receive_buffer *buf;
449 struct udsl_instance_data *instance = (struct udsl_instance_data *)data;
450 struct udsl_receiver *rcv;
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);
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);
465 buf = list_entry(instance->spare_receive_buffers.next,
466 struct udsl_receive_buffer, list);
467 list_del(&buf->list);
471 usb_fill_bulk_urb(rcv->urb, instance->usb_dev,
472 usb_rcvbulkpipe(instance->usb_dev, instance->data_endpoint),
474 rcv_buf_size * (ATM_CELL_SIZE + instance->rcv_padding),
475 udsl_complete_receive, rcv);
477 vdbg("udsl_process_receive: sending urb 0x%p, rcv 0x%p, buf 0x%p",
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);
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 */
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);
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);
510 static void udsl_complete_send(struct urb *urb, struct pt_regs *regs)
512 struct udsl_instance_data *instance;
513 struct udsl_sender *snd;
516 if (!urb || !(snd = urb->context) || !(instance = snd->instance)) {
517 dbg("udsl_complete_send: bad urb!");
521 vdbg("udsl_complete_send: urb 0x%p, status %d, snd 0x%p, buf 0x%p", urb,
522 urb->status, snd, snd->buffer);
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);
532 static void udsl_process_send(unsigned long data)
534 struct udsl_send_buffer *buf;
535 struct udsl_instance_data *instance = (struct udsl_instance_data *)data;
537 struct udsl_sender *snd;
539 unsigned int num_written;
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 */
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);
559 usb_fill_bulk_urb(snd->urb, instance->usb_dev,
560 usb_sndbulkpipe(instance->usb_dev, instance->data_endpoint),
562 (snd_buf_size - buf->free_cells) * (ATM_CELL_SIZE + instance->snd_padding),
563 udsl_complete_send, snd);
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);
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 */
577 spin_lock_irq(&instance->send_lock);
579 spin_unlock_irq(&instance->send_lock);
581 if (!instance->current_skb)
582 instance->current_skb = skb_dequeue(&instance->sndqueue);
583 if (!instance->current_skb)
584 return; /* done - no more skbs */
586 skb = instance->current_skb;
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 */
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);
600 buf->free_start = buf->base;
601 buf->free_cells = snd_buf_size;
603 instance->current_buffer = buf;
606 num_written = udsl_write_cells(instance, buf->free_cells, skb, &buf->free_start);
608 vdbg("udsl_process_send: wrote %u cells from skb 0x%p to buffer 0x%p",
609 num_written, skb, buf);
611 if (!(buf->free_cells -= num_written)) {
612 list_add_tail(&buf->list, &instance->filled_send_buffers);
613 instance->current_buffer = NULL;
616 vdbg("udsl_process_send: buffer contains %d cells, %d left",
617 snd_buf_size - buf->free_cells, buf->free_cells);
619 if (!UDSL_SKB(skb)->num_cells) {
620 struct atm_vcc *vcc = UDSL_SKB(skb)->atm_data.vcc;
623 instance->current_skb = NULL;
625 atomic_inc(&vcc->stats->tx);
631 static void udsl_cancel_send(struct udsl_instance_data *instance,
634 struct sk_buff *skb, *n;
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);
646 spin_unlock_irq(&instance->sndqueue.lock);
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;
654 tasklet_enable(&instance->send_tasklet);
655 dbg("udsl_cancel_send done");
658 static int udsl_atm_send(struct atm_vcc *vcc, struct sk_buff *skb)
660 struct udsl_instance_data *instance = vcc->dev->dev_data;
663 vdbg("udsl_atm_send called (skb 0x%p, len %u)", skb, skb->len);
666 dbg("udsl_atm_send: NULL data!");
671 if (vcc->qos.aal != ATM_AAL5) {
672 dbg("udsl_atm_send: unsupported ATM type %d!", vcc->qos.aal);
677 if (skb->len > ATM_MAX_AAL5_PDU) {
678 dbg("udsl_atm_send: packet too long (%d vs %d)!", skb->len,
684 PACKETDEBUG(skb->data, skb->len);
686 udsl_groom_skb(vcc, skb);
687 skb_queue_tail(&instance->sndqueue, skb);
688 tasklet_schedule(&instance->send_tasklet);
697 /********************
699 ********************/
701 static void udsl_destroy_instance(struct kref *kref)
703 struct udsl_instance_data *instance =
704 container_of(kref, struct udsl_instance_data, refcount);
706 tasklet_kill(&instance->receive_tasklet);
707 tasklet_kill(&instance->send_tasklet);
708 usb_put_dev(instance->usb_dev);
712 void udsl_get_instance(struct udsl_instance_data *instance)
714 kref_get(&instance->refcount);
717 void udsl_put_instance(struct udsl_instance_data *instance)
719 kref_put(&instance->refcount, udsl_destroy_instance);
726 static void udsl_atm_dev_close(struct atm_dev *dev)
728 struct udsl_instance_data *instance = dev->dev_data;
730 dev->dev_data = NULL;
731 udsl_put_instance(instance);
734 static int udsl_atm_proc_read(struct atm_dev *atm_dev, loff_t * pos, char *page)
736 struct udsl_instance_data *instance = atm_dev->dev_data;
740 dbg("udsl_atm_proc_read: NULL instance!");
745 return sprintf(page, "%s\n", instance->description);
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]);
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));
763 switch (atm_dev->signal) {
764 case ATM_PHY_SIG_FOUND:
765 sprintf(page, "Line up");
767 case ATM_PHY_SIG_LOST:
768 sprintf(page, "Line down");
771 sprintf(page, "Line state unknown");
775 if (instance->usb_dev->state == USB_STATE_NOTATTACHED)
776 strcat(page, ", disconnected\n");
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");
783 strcat(page, ", no firmware\n");
792 static int udsl_atm_open(struct atm_vcc *vcc)
794 struct udsl_instance_data *instance = vcc->dev->dev_data;
795 struct udsl_vcc_data *new;
796 unsigned int max_pdu;
798 short vpi = vcc->vpi;
801 dbg("udsl_atm_open: vpi %hd, vci %d", vpi, vci);
804 dbg("udsl_atm_open: NULL data!");
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);
815 if (instance->firmware_wait &&
816 (err = instance->firmware_wait(instance)) < 0) {
817 dbg("udsl_atm_open: firmware not loaded (%d)!", err);
821 down(&instance->serialize); /* vs self, udsl_atm_close */
823 if (udsl_find_vcc(instance, vpi, vci)) {
824 dbg("udsl_atm_open: %hd/%d already in use!", vpi, vci);
825 up(&instance->serialize);
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);
835 memset(new, 0, sizeof(struct udsl_vcc_data));
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!");
845 up(&instance->serialize);
851 tasklet_disable(&instance->receive_tasklet);
852 list_add(&new->list, &instance->vcc_list);
853 tasklet_enable(&instance->receive_tasklet);
855 set_bit(ATM_VF_ADDR, &vcc->flags);
856 set_bit(ATM_VF_PARTIAL, &vcc->flags);
857 set_bit(ATM_VF_READY, &vcc->flags);
859 up(&instance->serialize);
861 tasklet_schedule(&instance->receive_tasklet);
863 dbg("udsl_atm_open: allocated vcc data 0x%p (max_pdu: %u)", new, max_pdu);
868 static void udsl_atm_close(struct atm_vcc *vcc)
870 struct udsl_instance_data *instance = vcc->dev->dev_data;
871 struct udsl_vcc_data *vcc_data = vcc->dev_data;
873 dbg("udsl_atm_close called");
875 if (!instance || !vcc_data) {
876 dbg("udsl_atm_close: NULL data!");
880 dbg("udsl_atm_close: deallocating vcc 0x%p with vpi %d vci %d",
881 vcc_data, vcc_data->vpi, vcc_data->vci);
883 udsl_cancel_send(instance, vcc);
885 down(&instance->serialize); /* vs self, udsl_atm_open */
887 tasklet_disable(&instance->receive_tasklet);
888 list_del(&vcc_data->list);
889 tasklet_enable(&instance->receive_tasklet);
891 kfree_skb(vcc_data->sarb);
892 vcc_data->sarb = NULL;
895 vcc->dev_data = NULL;
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);
903 up(&instance->serialize);
905 dbg("udsl_atm_close successful");
908 static int udsl_atm_ioctl(struct atm_dev *dev, unsigned int cmd,
913 return put_user(ATM_LM_NONE, (int __user *)arg) ? -EFAULT : 0;
923 int udsl_instance_setup(struct usb_device *dev,
924 struct udsl_instance_data *instance)
929 kref_init(&instance->refcount); /* one for USB */
930 udsl_get_instance(instance); /* one for ATM */
932 init_MUTEX(&instance->serialize);
934 instance->usb_dev = dev;
936 INIT_LIST_HEAD(&instance->vcc_list);
938 instance->status = UDSL_NO_FIRMWARE;
939 init_waitqueue_head(&instance->firmware_waiters);
941 spin_lock_init(&instance->receive_lock);
942 INIT_LIST_HEAD(&instance->spare_receivers);
943 INIT_LIST_HEAD(&instance->filled_receive_buffers);
945 tasklet_init(&instance->receive_tasklet, udsl_process_receive, (unsigned long)instance);
946 INIT_LIST_HEAD(&instance->spare_receive_buffers);
948 skb_queue_head_init(&instance->sndqueue);
950 spin_lock_init(&instance->send_lock);
951 INIT_LIST_HEAD(&instance->spare_senders);
952 INIT_LIST_HEAD(&instance->spare_send_buffers);
954 tasklet_init(&instance->send_tasklet, udsl_process_send,
955 (unsigned long)instance);
956 INIT_LIST_HEAD(&instance->filled_send_buffers);
959 for (i = 0; i < num_rcv_urbs; i++) {
960 struct udsl_receiver *rcv = &(instance->receivers[i]);
962 if (!(rcv->urb = usb_alloc_urb(0, GFP_KERNEL))) {
963 dbg("udsl_usb_probe: no memory for receive urb %d!", i);
967 rcv->instance = instance;
969 list_add(&rcv->list, &instance->spare_receivers);
972 for (i = 0; i < num_rcv_bufs; i++) {
973 struct udsl_receive_buffer *buf =
974 &(instance->receive_buffers[i]);
976 buf->base = kmalloc(rcv_buf_size * (ATM_CELL_SIZE + instance->rcv_padding),
979 dbg("udsl_usb_probe: no memory for receive buffer %d!", i);
983 list_add(&buf->list, &instance->spare_receive_buffers);
987 for (i = 0; i < num_snd_urbs; i++) {
988 struct udsl_sender *snd = &(instance->senders[i]);
990 if (!(snd->urb = usb_alloc_urb(0, GFP_KERNEL))) {
991 dbg("udsl_usb_probe: no memory for send urb %d!", i);
995 snd->instance = instance;
997 list_add(&snd->list, &instance->spare_senders);
1000 for (i = 0; i < num_snd_bufs; i++) {
1001 struct udsl_send_buffer *buf = &(instance->send_buffers[i]);
1003 buf->base = kmalloc(snd_buf_size * (ATM_CELL_SIZE + instance->snd_padding),
1006 dbg("udsl_usb_probe: no memory for send buffer %d!", i);
1010 list_add(&buf->list, &instance->spare_send_buffers);
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!");
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;
1025 /* temp init ATM device, set to 128kbit */
1026 instance->atm_dev->link_rate = 128 * 1000 / 424;
1028 /* device description */
1029 buf = instance->description;
1030 length = sizeof(instance->description);
1032 if ((i = usb_string(dev, dev->descriptor.iProduct, buf, length)) < 0)
1038 i = scnprintf(buf, length, " (");
1042 if (length <= 0 || (i = usb_make_path(dev, buf, length)) < 0)
1048 snprintf(buf, length, ")");
1051 /* ready for ATM callbacks */
1053 instance->atm_dev->dev_data = instance;
1060 for (i = 0; i < num_snd_bufs; i++)
1061 kfree(instance->send_buffers[i].base);
1063 for (i = 0; i < num_snd_urbs; i++)
1064 usb_free_urb(instance->senders[i].urb);
1066 for (i = 0; i < num_rcv_bufs; i++)
1067 kfree(instance->receive_buffers[i].base);
1069 for (i = 0; i < num_rcv_urbs; i++)
1070 usb_free_urb(instance->receivers[i].urb);
1075 void udsl_instance_disconnect(struct udsl_instance_data *instance)
1079 dbg("udsl_instance_disconnect entered");
1082 dbg("udsl_instance_disconnect: NULL instance!");
1086 /* receive finalize */
1087 tasklet_disable(&instance->receive_tasklet);
1089 for (i = 0; i < num_rcv_urbs; i++)
1090 usb_kill_urb(instance->receivers[i].urb);
1092 /* no need to take the spinlock */
1093 INIT_LIST_HEAD(&instance->filled_receive_buffers);
1094 INIT_LIST_HEAD(&instance->spare_receive_buffers);
1096 tasklet_enable(&instance->receive_tasklet);
1098 for (i = 0; i < num_rcv_urbs; i++)
1099 usb_free_urb(instance->receivers[i].urb);
1101 for (i = 0; i < num_rcv_bufs; i++)
1102 kfree(instance->receive_buffers[i].base);
1105 tasklet_disable(&instance->send_tasklet);
1107 for (i = 0; i < num_snd_urbs; i++)
1108 usb_kill_urb(instance->senders[i].urb);
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;
1115 tasklet_enable(&instance->send_tasklet);
1117 for (i = 0; i < num_snd_urbs; i++)
1118 usb_free_urb(instance->senders[i].urb);
1120 for (i = 0; i < num_snd_bufs; i++)
1121 kfree(instance->send_buffers[i].base);
1124 shutdown_atm_dev(instance->atm_dev);
1127 EXPORT_SYMBOL_GPL(udsl_get_instance);
1128 EXPORT_SYMBOL_GPL(udsl_put_instance);
1129 EXPORT_SYMBOL_GPL(udsl_instance_setup);
1130 EXPORT_SYMBOL_GPL(udsl_instance_disconnect);
1136 static int __init udsl_usb_init(void)
1138 dbg("udsl_usb_init: driver version " DRIVER_VERSION);
1140 if (sizeof(struct udsl_control) > sizeof(((struct sk_buff *) 0)->cb)) {
1141 printk(KERN_ERR __FILE__ ": unusable with this kernel!\n");
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))
1156 static void __exit udsl_usb_exit(void)
1160 module_init(udsl_usb_init);
1161 module_exit(udsl_usb_exit);
1163 MODULE_AUTHOR(DRIVER_AUTHOR);
1164 MODULE_DESCRIPTION(DRIVER_DESC);
1165 MODULE_LICENSE("GPL");
1166 MODULE_VERSION(DRIVER_VERSION);
1172 #ifdef VERBOSE_DEBUG
1173 static int udsl_print_packet(const unsigned char *data, int len)
1175 unsigned char buffer[256];
1178 for (i = 0; i < len;) {
1180 sprintf(buffer, "%.3d :", i);
1181 for (j = 0; (j < 16) && (i < len); j++, i++) {
1182 sprintf(buffer, "%s %2.2x", buffer, data[i]);