]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/usb/atm/usbatm.c
746d30ffb732aaeb6802976cc67295552e7eccbc
[linux-2.6-omap-h63xx.git] / drivers / usb / atm / usbatm.c
1 /******************************************************************************
2  *  usbatm.c - Generic USB xDSL driver core
3  *
4  *  Copyright (C) 2001, Alcatel
5  *  Copyright (C) 2003, Duncan Sands, SolNegro, Josep Comas
6  *  Copyright (C) 2004, David Woodhouse, Roman Kagan
7  *
8  *  This program is free software; you can redistribute it and/or modify it
9  *  under the terms of the GNU General Public License as published by the Free
10  *  Software Foundation; either version 2 of the License, or (at your option)
11  *  any later version.
12  *
13  *  This program is distributed in the hope that it will be useful, but WITHOUT
14  *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
16  *  more details.
17  *
18  *  You should have received a copy of the GNU General Public License along with
19  *  this program; if not, write to the Free Software Foundation, Inc., 59
20  *  Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  *
22  ******************************************************************************/
23
24 /*
25  *  Written by Johan Verrept, Duncan Sands (duncan.sands@free.fr) and David Woodhouse
26  *
27  *  1.7+:       - See the check-in logs
28  *
29  *  1.6:        - No longer opens a connection if the firmware is not loaded
30  *              - Added support for the speedtouch 330
31  *              - Removed the limit on the number of devices
32  *              - Module now autoloads on device plugin
33  *              - Merged relevant parts of sarlib
34  *              - Replaced the kernel thread with a tasklet
35  *              - New packet transmission code
36  *              - Changed proc file contents
37  *              - Fixed all known SMP races
38  *              - Many fixes and cleanups
39  *              - Various fixes by Oliver Neukum (oliver@neukum.name)
40  *
41  *  1.5A:       - Version for inclusion in 2.5 series kernel
42  *              - Modifications by Richard Purdie (rpurdie@rpsys.net)
43  *              - made compatible with kernel 2.5.6 onwards by changing
44  *              usbatm_usb_send_data_context->urb to a pointer and adding code
45  *              to alloc and free it
46  *              - remove_wait_queue() added to usbatm_atm_processqueue_thread()
47  *
48  *  1.5:        - fixed memory leak when atmsar_decode_aal5 returned NULL.
49  *              (reported by stephen.robinson@zen.co.uk)
50  *
51  *  1.4:        - changed the spin_lock() under interrupt to spin_lock_irqsave()
52  *              - unlink all active send urbs of a vcc that is being closed.
53  *
54  *  1.3.1:      - added the version number
55  *
56  *  1.3:        - Added multiple send urb support
57  *              - fixed memory leak and vcc->tx_inuse starvation bug
58  *                when not enough memory left in vcc.
59  *
60  *  1.2:        - Fixed race condition in usbatm_usb_send_data()
61  *  1.1:        - Turned off packet debugging
62  *
63  */
64
65 #include "usbatm.h"
66
67 #include <asm/uaccess.h>
68 #include <linux/crc32.h>
69 #include <linux/errno.h>
70 #include <linux/init.h>
71 #include <linux/interrupt.h>
72 #include <linux/kernel.h>
73 #include <linux/module.h>
74 #include <linux/moduleparam.h>
75 #include <linux/netdevice.h>
76 #include <linux/proc_fs.h>
77 #include <linux/sched.h>
78 #include <linux/signal.h>
79 #include <linux/slab.h>
80 #include <linux/smp_lock.h>
81 #include <linux/stat.h>
82 #include <linux/timer.h>
83 #include <linux/wait.h>
84
85 #ifdef VERBOSE_DEBUG
86 static int usbatm_print_packet(const unsigned char *data, int len);
87 #define PACKETDEBUG(arg...)     usbatm_print_packet (arg)
88 #define vdbg(arg...)            dbg (arg)
89 #else
90 #define PACKETDEBUG(arg...)
91 #define vdbg(arg...)
92 #endif
93
94 #define DRIVER_AUTHOR   "Johan Verrept, Duncan Sands <duncan.sands@free.fr>"
95 #define DRIVER_VERSION  "1.10"
96 #define DRIVER_DESC     "Generic USB ATM/DSL I/O, version " DRIVER_VERSION
97
98 static const char usbatm_driver_name[] = "usbatm";
99
100 #define UDSL_MAX_RCV_URBS               16
101 #define UDSL_MAX_SND_URBS               16
102 #define UDSL_MAX_BUF_SIZE               65536
103 #define UDSL_DEFAULT_RCV_URBS           4
104 #define UDSL_DEFAULT_SND_URBS           4
105 #define UDSL_DEFAULT_RCV_BUF_SIZE       3392    /* 64 * ATM_CELL_SIZE */
106 #define UDSL_DEFAULT_SND_BUF_SIZE       3392    /* 64 * ATM_CELL_SIZE */
107
108 #define ATM_CELL_HEADER                 (ATM_CELL_SIZE - ATM_CELL_PAYLOAD)
109
110 #define THROTTLE_MSECS                  100     /* delay to recover processing after urb submission fails */
111
112 static unsigned int num_rcv_urbs = UDSL_DEFAULT_RCV_URBS;
113 static unsigned int num_snd_urbs = UDSL_DEFAULT_SND_URBS;
114 static unsigned int rcv_buf_bytes = UDSL_DEFAULT_RCV_BUF_SIZE;
115 static unsigned int snd_buf_bytes = UDSL_DEFAULT_SND_BUF_SIZE;
116
117 module_param(num_rcv_urbs, uint, S_IRUGO);
118 MODULE_PARM_DESC(num_rcv_urbs,
119                  "Number of urbs used for reception (range: 0-"
120                  __MODULE_STRING(UDSL_MAX_RCV_URBS) ", default: "
121                  __MODULE_STRING(UDSL_DEFAULT_RCV_URBS) ")");
122
123 module_param(num_snd_urbs, uint, S_IRUGO);
124 MODULE_PARM_DESC(num_snd_urbs,
125                  "Number of urbs used for transmission (range: 0-"
126                  __MODULE_STRING(UDSL_MAX_SND_URBS) ", default: "
127                  __MODULE_STRING(UDSL_DEFAULT_SND_URBS) ")");
128
129 module_param(rcv_buf_bytes, uint, S_IRUGO);
130 MODULE_PARM_DESC(rcv_buf_bytes,
131                  "Size of the buffers used for reception, in bytes (range: 1-"
132                  __MODULE_STRING(UDSL_MAX_BUF_SIZE) ", default: "
133                  __MODULE_STRING(UDSL_DEFAULT_RCV_BUF_SIZE) ")");
134
135 module_param(snd_buf_bytes, uint, S_IRUGO);
136 MODULE_PARM_DESC(snd_buf_bytes,
137                  "Size of the buffers used for transmission, in bytes (range: 1-"
138                  __MODULE_STRING(UDSL_MAX_BUF_SIZE) ", default: "
139                  __MODULE_STRING(UDSL_DEFAULT_SND_BUF_SIZE) ")");
140
141
142 /* receive */
143
144 struct usbatm_vcc_data {
145         /* vpi/vci lookup */
146         struct list_head list;
147         short vpi;
148         int vci;
149         struct atm_vcc *vcc;
150
151         /* raw cell reassembly */
152         struct sk_buff *sarb;
153 };
154
155
156 /* send */
157
158 struct usbatm_control {
159         struct atm_skb_data atm;
160         u32 len;
161         u32 crc;
162 };
163
164 #define UDSL_SKB(x)             ((struct usbatm_control *)(x)->cb)
165
166
167 /* ATM */
168
169 static void usbatm_atm_dev_close(struct atm_dev *atm_dev);
170 static int usbatm_atm_open(struct atm_vcc *vcc);
171 static void usbatm_atm_close(struct atm_vcc *vcc);
172 static int usbatm_atm_ioctl(struct atm_dev *atm_dev, unsigned int cmd, void __user * arg);
173 static int usbatm_atm_send(struct atm_vcc *vcc, struct sk_buff *skb);
174 static int usbatm_atm_proc_read(struct atm_dev *atm_dev, loff_t * pos, char *page);
175
176 static struct atmdev_ops usbatm_atm_devops = {
177         .dev_close      = usbatm_atm_dev_close,
178         .open           = usbatm_atm_open,
179         .close          = usbatm_atm_close,
180         .ioctl          = usbatm_atm_ioctl,
181         .send           = usbatm_atm_send,
182         .proc_read      = usbatm_atm_proc_read,
183         .owner          = THIS_MODULE,
184 };
185
186
187 /***********
188 **  misc  **
189 ***********/
190
191 static inline unsigned int usbatm_pdu_length(unsigned int length)
192 {
193         length += ATM_CELL_PAYLOAD - 1 + ATM_AAL5_TRAILER;
194         return length - length % ATM_CELL_PAYLOAD;
195 }
196
197 static inline void usbatm_pop(struct atm_vcc *vcc, struct sk_buff *skb)
198 {
199         if (vcc->pop)
200                 vcc->pop(vcc, skb);
201         else
202                 dev_kfree_skb_any(skb);
203 }
204
205
206 /***********
207 **  urbs  **
208 ************/
209
210 static struct urb *usbatm_pop_urb(struct usbatm_channel *channel)
211 {
212         struct urb *urb;
213
214         spin_lock_irq(&channel->lock);
215         if (list_empty(&channel->list)) {
216                 spin_unlock_irq(&channel->lock);
217                 return NULL;
218         }
219
220         urb = list_entry(channel->list.next, struct urb, urb_list);
221         list_del(&urb->urb_list);
222         spin_unlock_irq(&channel->lock);
223
224         return urb;
225 }
226
227 static int usbatm_submit_urb(struct urb *urb)
228 {
229         struct usbatm_channel *channel = urb->context;
230         int ret;
231
232         vdbg("%s: submitting urb 0x%p, size %u",
233              __func__, urb, urb->transfer_buffer_length);
234
235         ret = usb_submit_urb(urb, GFP_ATOMIC);
236         if (ret) {
237                 if (printk_ratelimit())
238                         atm_warn(channel->usbatm, "%s: urb 0x%p submission failed (%d)!\n",
239                                 __func__, urb, ret);
240
241                 /* consider all errors transient and return the buffer back to the queue */
242                 urb->status = -EAGAIN;
243                 spin_lock_irq(&channel->lock);
244
245                 /* must add to the front when sending; doesn't matter when receiving */
246                 list_add(&urb->urb_list, &channel->list);
247
248                 spin_unlock_irq(&channel->lock);
249
250                 /* make sure the channel doesn't stall */
251                 mod_timer(&channel->delay, jiffies + msecs_to_jiffies(THROTTLE_MSECS));
252         }
253
254         return ret;
255 }
256
257 static void usbatm_complete(struct urb *urb)
258 {
259         struct usbatm_channel *channel = urb->context;
260         unsigned long flags;
261
262         vdbg("%s: urb 0x%p, status %d, actual_length %d",
263              __func__, urb, urb->status, urb->actual_length);
264
265         /* usually in_interrupt(), but not always */
266         spin_lock_irqsave(&channel->lock, flags);
267
268         /* must add to the back when receiving; doesn't matter when sending */
269         list_add_tail(&urb->urb_list, &channel->list);
270
271         spin_unlock_irqrestore(&channel->lock, flags);
272
273         if (unlikely(urb->status) &&
274                         (!(channel->usbatm->flags & UDSL_IGNORE_EILSEQ) ||
275                          urb->status != -EILSEQ ))
276         {
277                 if (printk_ratelimit())
278                         atm_warn(channel->usbatm, "%s: urb 0x%p failed (%d)!\n",
279                                 __func__, urb, urb->status);
280                 /* throttle processing in case of an error */
281                 mod_timer(&channel->delay, jiffies + msecs_to_jiffies(THROTTLE_MSECS));
282         } else
283                 tasklet_schedule(&channel->tasklet);
284 }
285
286
287 /*************
288 **  decode  **
289 *************/
290
291 static inline struct usbatm_vcc_data *usbatm_find_vcc(struct usbatm_data *instance,
292                                                   short vpi, int vci)
293 {
294         struct usbatm_vcc_data *vcc_data;
295
296         list_for_each_entry(vcc_data, &instance->vcc_list, list)
297                 if ((vcc_data->vci == vci) && (vcc_data->vpi == vpi))
298                         return vcc_data;
299         return NULL;
300 }
301
302 static void usbatm_extract_one_cell(struct usbatm_data *instance, unsigned char *source)
303 {
304         struct atm_vcc *vcc;
305         struct sk_buff *sarb;
306         short vpi = ((source[0] & 0x0f) << 4)  | (source[1] >> 4);
307         int vci = ((source[1] & 0x0f) << 12) | (source[2] << 4) | (source[3] >> 4);
308         u8 pti = ((source[3] & 0xe) >> 1);
309
310         vdbg("%s: vpi %hd, vci %d, pti %d", __func__, vpi, vci, pti);
311
312         if ((vci != instance->cached_vci) || (vpi != instance->cached_vpi)) {
313                 instance->cached_vpi = vpi;
314                 instance->cached_vci = vci;
315
316                 instance->cached_vcc = usbatm_find_vcc(instance, vpi, vci);
317
318                 if (!instance->cached_vcc)
319                         atm_rldbg(instance, "%s: unknown vpi/vci (%hd/%d)!\n", __func__, vpi, vci);
320         }
321
322         if (!instance->cached_vcc)
323                 return;
324
325         vcc = instance->cached_vcc->vcc;
326
327         /* OAM F5 end-to-end */
328         if (pti == ATM_PTI_E2EF5) {
329                 if (printk_ratelimit())
330                         atm_warn(instance, "%s: OAM not supported (vpi %d, vci %d)!\n",
331                                 __func__, vpi, vci);
332                 atomic_inc(&vcc->stats->rx_err);
333                 return;
334         }
335
336         sarb = instance->cached_vcc->sarb;
337
338         if (sarb->tail + ATM_CELL_PAYLOAD > sarb->end) {
339                 atm_rldbg(instance, "%s: buffer overrun (sarb->len %u, vcc: 0x%p)!\n",
340                                 __func__, sarb->len, vcc);
341                 /* discard cells already received */
342                 skb_trim(sarb, 0);
343                 UDSL_ASSERT(sarb->tail + ATM_CELL_PAYLOAD <= sarb->end);
344         }
345
346         memcpy(skb_tail_pointer(sarb), source + ATM_CELL_HEADER, ATM_CELL_PAYLOAD);
347         __skb_put(sarb, ATM_CELL_PAYLOAD);
348
349         if (pti & 1) {
350                 struct sk_buff *skb;
351                 unsigned int length;
352                 unsigned int pdu_length;
353
354                 length = (source[ATM_CELL_SIZE - 6] << 8) + source[ATM_CELL_SIZE - 5];
355
356                 /* guard against overflow */
357                 if (length > ATM_MAX_AAL5_PDU) {
358                         atm_rldbg(instance, "%s: bogus length %u (vcc: 0x%p)!\n",
359                                   __func__, length, vcc);
360                         atomic_inc(&vcc->stats->rx_err);
361                         goto out;
362                 }
363
364                 pdu_length = usbatm_pdu_length(length);
365
366                 if (sarb->len < pdu_length) {
367                         atm_rldbg(instance, "%s: bogus pdu_length %u (sarb->len: %u, vcc: 0x%p)!\n",
368                                   __func__, pdu_length, sarb->len, vcc);
369                         atomic_inc(&vcc->stats->rx_err);
370                         goto out;
371                 }
372
373                 if (crc32_be(~0, skb_tail_pointer(sarb) - pdu_length, pdu_length) != 0xc704dd7b) {
374                         atm_rldbg(instance, "%s: packet failed crc check (vcc: 0x%p)!\n",
375                                   __func__, vcc);
376                         atomic_inc(&vcc->stats->rx_err);
377                         goto out;
378                 }
379
380                 vdbg("%s: got packet (length: %u, pdu_length: %u, vcc: 0x%p)", __func__, length, pdu_length, vcc);
381
382                 if (!(skb = dev_alloc_skb(length))) {
383                         if (printk_ratelimit())
384                                 atm_err(instance, "%s: no memory for skb (length: %u)!\n",
385                                         __func__, length);
386                         atomic_inc(&vcc->stats->rx_drop);
387                         goto out;
388                 }
389
390                 vdbg("%s: allocated new sk_buff (skb: 0x%p, skb->truesize: %u)", __func__, skb, skb->truesize);
391
392                 if (!atm_charge(vcc, skb->truesize)) {
393                         atm_rldbg(instance, "%s: failed atm_charge (skb->truesize: %u)!\n",
394                                   __func__, skb->truesize);
395                         dev_kfree_skb_any(skb);
396                         goto out;       /* atm_charge increments rx_drop */
397                 }
398
399                 skb_copy_to_linear_data(skb,
400                                         skb_tail_pointer(sarb) - pdu_length,
401                                         length);
402                 __skb_put(skb, length);
403
404                 vdbg("%s: sending skb 0x%p, skb->len %u, skb->truesize %u",
405                      __func__, skb, skb->len, skb->truesize);
406
407                 PACKETDEBUG(skb->data, skb->len);
408
409                 vcc->push(vcc, skb);
410
411                 atomic_inc(&vcc->stats->rx);
412         out:
413                 skb_trim(sarb, 0);
414         }
415 }
416
417 static void usbatm_extract_cells(struct usbatm_data *instance,
418                 unsigned char *source, unsigned int avail_data)
419 {
420         unsigned int stride = instance->rx_channel.stride;
421         unsigned int buf_usage = instance->buf_usage;
422
423         /* extract cells from incoming data, taking into account that
424          * the length of avail data may not be a multiple of stride */
425
426         if (buf_usage > 0) {
427                 /* we have a partially received atm cell */
428                 unsigned char *cell_buf = instance->cell_buf;
429                 unsigned int space_left = stride - buf_usage;
430
431                 UDSL_ASSERT(buf_usage <= stride);
432
433                 if (avail_data >= space_left) {
434                         /* add new data and process cell */
435                         memcpy(cell_buf + buf_usage, source, space_left);
436                         source += space_left;
437                         avail_data -= space_left;
438                         usbatm_extract_one_cell(instance, cell_buf);
439                         instance->buf_usage = 0;
440                 } else {
441                         /* not enough data to fill the cell */
442                         memcpy(cell_buf + buf_usage, source, avail_data);
443                         instance->buf_usage = buf_usage + avail_data;
444                         return;
445                 }
446         }
447
448         for (; avail_data >= stride; avail_data -= stride, source += stride)
449                 usbatm_extract_one_cell(instance, source);
450
451         if (avail_data > 0) {
452                 /* length was not a multiple of stride -
453                  * save remaining data for next call */
454                 memcpy(instance->cell_buf, source, avail_data);
455                 instance->buf_usage = avail_data;
456         }
457 }
458
459
460 /*************
461 **  encode  **
462 *************/
463
464 static unsigned int usbatm_write_cells(struct usbatm_data *instance,
465                                        struct sk_buff *skb,
466                                        u8 *target, unsigned int avail_space)
467 {
468         struct usbatm_control *ctrl = UDSL_SKB(skb);
469         struct atm_vcc *vcc = ctrl->atm.vcc;
470         unsigned int bytes_written;
471         unsigned int stride = instance->tx_channel.stride;
472
473         vdbg("%s: skb->len=%d, avail_space=%u", __func__, skb->len, avail_space);
474         UDSL_ASSERT(!(avail_space % stride));
475
476         for (bytes_written = 0; bytes_written < avail_space && ctrl->len;
477              bytes_written += stride, target += stride) {
478                 unsigned int data_len = min_t(unsigned int, skb->len, ATM_CELL_PAYLOAD);
479                 unsigned int left = ATM_CELL_PAYLOAD - data_len;
480                 u8 *ptr = target;
481
482                 ptr[0] = vcc->vpi >> 4;
483                 ptr[1] = (vcc->vpi << 4) | (vcc->vci >> 12);
484                 ptr[2] = vcc->vci >> 4;
485                 ptr[3] = vcc->vci << 4;
486                 ptr[4] = 0xec;
487                 ptr += ATM_CELL_HEADER;
488
489                 skb_copy_from_linear_data(skb, ptr, data_len);
490                 ptr += data_len;
491                 __skb_pull(skb, data_len);
492
493                 if(!left)
494                         continue;
495
496                 memset(ptr, 0, left);
497
498                 if (left >= ATM_AAL5_TRAILER) { /* trailer will go in this cell */
499                         u8 *trailer = target + ATM_CELL_SIZE - ATM_AAL5_TRAILER;
500                         /* trailer[0] = 0;              UU = 0 */
501                         /* trailer[1] = 0;              CPI = 0 */
502                         trailer[2] = ctrl->len >> 8;
503                         trailer[3] = ctrl->len;
504
505                         ctrl->crc = ~ crc32_be(ctrl->crc, ptr, left - 4);
506
507                         trailer[4] = ctrl->crc >> 24;
508                         trailer[5] = ctrl->crc >> 16;
509                         trailer[6] = ctrl->crc >> 8;
510                         trailer[7] = ctrl->crc;
511
512                         target[3] |= 0x2;       /* adjust PTI */
513
514                         ctrl->len = 0;          /* tag this skb finished */
515                 }
516                 else
517                         ctrl->crc = crc32_be(ctrl->crc, ptr, left);
518         }
519
520         return bytes_written;
521 }
522
523
524 /**************
525 **  receive  **
526 **************/
527
528 static void usbatm_rx_process(unsigned long data)
529 {
530         struct usbatm_data *instance = (struct usbatm_data *)data;
531         struct urb *urb;
532
533         while ((urb = usbatm_pop_urb(&instance->rx_channel))) {
534                 vdbg("%s: processing urb 0x%p", __func__, urb);
535
536                 if (usb_pipeisoc(urb->pipe)) {
537                         unsigned char *merge_start = NULL;
538                         unsigned int merge_length = 0;
539                         const unsigned int packet_size = instance->rx_channel.packet_size;
540                         int i;
541
542                         for (i = 0; i < urb->number_of_packets; i++) {
543                                 if (!urb->iso_frame_desc[i].status) {
544                                         unsigned int actual_length = urb->iso_frame_desc[i].actual_length;
545
546                                         UDSL_ASSERT(actual_length <= packet_size);
547
548                                         if (!merge_length)
549                                                 merge_start = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset;
550                                         merge_length += actual_length;
551                                         if (merge_length && (actual_length < packet_size)) {
552                                                 usbatm_extract_cells(instance, merge_start, merge_length);
553                                                 merge_length = 0;
554                                         }
555                                 } else {
556                                         atm_rldbg(instance, "%s: status %d in frame %d!\n", __func__, urb->status, i);
557                                         if (merge_length)
558                                                 usbatm_extract_cells(instance, merge_start, merge_length);
559                                         merge_length = 0;
560                                         instance->buf_usage = 0;
561                                 }
562                         }
563
564                         if (merge_length)
565                                 usbatm_extract_cells(instance, merge_start, merge_length);
566                 } else
567                         if (!urb->status)
568                                 usbatm_extract_cells(instance, urb->transfer_buffer, urb->actual_length);
569                         else
570                                 instance->buf_usage = 0;
571
572                 if (usbatm_submit_urb(urb))
573                         return;
574         }
575 }
576
577
578 /***********
579 **  send  **
580 ***********/
581
582 static void usbatm_tx_process(unsigned long data)
583 {
584         struct usbatm_data *instance = (struct usbatm_data *)data;
585         struct sk_buff *skb = instance->current_skb;
586         struct urb *urb = NULL;
587         const unsigned int buf_size = instance->tx_channel.buf_size;
588         unsigned int bytes_written = 0;
589         u8 *buffer = NULL;
590
591         if (!skb)
592                 skb = skb_dequeue(&instance->sndqueue);
593
594         while (skb) {
595                 if (!urb) {
596                         urb = usbatm_pop_urb(&instance->tx_channel);
597                         if (!urb)
598                                 break;          /* no more senders */
599                         buffer = urb->transfer_buffer;
600                         bytes_written = (urb->status == -EAGAIN) ?
601                                 urb->transfer_buffer_length : 0;
602                 }
603
604                 bytes_written += usbatm_write_cells(instance, skb,
605                                                   buffer + bytes_written,
606                                                   buf_size - bytes_written);
607
608                 vdbg("%s: wrote %u bytes from skb 0x%p to urb 0x%p",
609                      __func__, bytes_written, skb, urb);
610
611                 if (!UDSL_SKB(skb)->len) {
612                         struct atm_vcc *vcc = UDSL_SKB(skb)->atm.vcc;
613
614                         usbatm_pop(vcc, skb);
615                         atomic_inc(&vcc->stats->tx);
616
617                         skb = skb_dequeue(&instance->sndqueue);
618                 }
619
620                 if (bytes_written == buf_size || (!skb && bytes_written)) {
621                         urb->transfer_buffer_length = bytes_written;
622
623                         if (usbatm_submit_urb(urb))
624                                 break;
625                         urb = NULL;
626                 }
627         }
628
629         instance->current_skb = skb;
630 }
631
632 static void usbatm_cancel_send(struct usbatm_data *instance,
633                                struct atm_vcc *vcc)
634 {
635         struct sk_buff *skb, *n;
636
637         atm_dbg(instance, "%s entered\n", __func__);
638         spin_lock_irq(&instance->sndqueue.lock);
639         for (skb = instance->sndqueue.next, n = skb->next;
640              skb != (struct sk_buff *)&instance->sndqueue;
641              skb = n, n = skb->next)
642                 if (UDSL_SKB(skb)->atm.vcc == vcc) {
643                         atm_dbg(instance, "%s: popping skb 0x%p\n", __func__, skb);
644                         __skb_unlink(skb, &instance->sndqueue);
645                         usbatm_pop(vcc, skb);
646                 }
647         spin_unlock_irq(&instance->sndqueue.lock);
648
649         tasklet_disable(&instance->tx_channel.tasklet);
650         if ((skb = instance->current_skb) && (UDSL_SKB(skb)->atm.vcc == vcc)) {
651                 atm_dbg(instance, "%s: popping current skb (0x%p)\n", __func__, skb);
652                 instance->current_skb = NULL;
653                 usbatm_pop(vcc, skb);
654         }
655         tasklet_enable(&instance->tx_channel.tasklet);
656         atm_dbg(instance, "%s done\n", __func__);
657 }
658
659 static int usbatm_atm_send(struct atm_vcc *vcc, struct sk_buff *skb)
660 {
661         struct usbatm_data *instance = vcc->dev->dev_data;
662         struct usbatm_control *ctrl = UDSL_SKB(skb);
663         int err;
664
665         vdbg("%s called (skb 0x%p, len %u)", __func__, skb, skb->len);
666
667         /* racy disconnection check - fine */
668         if (!instance || instance->disconnected) {
669 #ifdef DEBUG
670                 if (printk_ratelimit())
671                         printk(KERN_DEBUG "%s: %s!\n", __func__, instance ? "disconnected" : "NULL instance");
672 #endif
673                 err = -ENODEV;
674                 goto fail;
675         }
676
677         if (vcc->qos.aal != ATM_AAL5) {
678                 atm_rldbg(instance, "%s: unsupported ATM type %d!\n", __func__, vcc->qos.aal);
679                 err = -EINVAL;
680                 goto fail;
681         }
682
683         if (skb->len > ATM_MAX_AAL5_PDU) {
684                 atm_rldbg(instance, "%s: packet too long (%d vs %d)!\n",
685                                 __func__, skb->len, ATM_MAX_AAL5_PDU);
686                 err = -EINVAL;
687                 goto fail;
688         }
689
690         PACKETDEBUG(skb->data, skb->len);
691
692         /* initialize the control block */
693         ctrl->atm.vcc = vcc;
694         ctrl->len = skb->len;
695         ctrl->crc = crc32_be(~0, skb->data, skb->len);
696
697         skb_queue_tail(&instance->sndqueue, skb);
698         tasklet_schedule(&instance->tx_channel.tasklet);
699
700         return 0;
701
702  fail:
703         usbatm_pop(vcc, skb);
704         return err;
705 }
706
707
708 /********************
709 **  bean counting  **
710 ********************/
711
712 static void usbatm_destroy_instance(struct kref *kref)
713 {
714         struct usbatm_data *instance = container_of(kref, struct usbatm_data, refcount);
715
716         dbg("%s", __func__);
717
718         tasklet_kill(&instance->rx_channel.tasklet);
719         tasklet_kill(&instance->tx_channel.tasklet);
720         usb_put_dev(instance->usb_dev);
721         kfree(instance);
722 }
723
724 static void usbatm_get_instance(struct usbatm_data *instance)
725 {
726         dbg("%s", __func__);
727
728         kref_get(&instance->refcount);
729 }
730
731 static void usbatm_put_instance(struct usbatm_data *instance)
732 {
733         dbg("%s", __func__);
734
735         kref_put(&instance->refcount, usbatm_destroy_instance);
736 }
737
738
739 /**********
740 **  ATM  **
741 **********/
742
743 static void usbatm_atm_dev_close(struct atm_dev *atm_dev)
744 {
745         struct usbatm_data *instance = atm_dev->dev_data;
746
747         dbg("%s", __func__);
748
749         if (!instance)
750                 return;
751
752         atm_dev->dev_data = NULL; /* catch bugs */
753         usbatm_put_instance(instance);  /* taken in usbatm_atm_init */
754 }
755
756 static int usbatm_atm_proc_read(struct atm_dev *atm_dev, loff_t * pos, char *page)
757 {
758         struct usbatm_data *instance = atm_dev->dev_data;
759         int left = *pos;
760
761         if (!instance) {
762                 dbg("%s: NULL instance!", __func__);
763                 return -ENODEV;
764         }
765
766         if (!left--)
767                 return sprintf(page, "%s\n", instance->description);
768
769         if (!left--)
770                 return sprintf(page, "MAC: %02x:%02x:%02x:%02x:%02x:%02x\n",
771                                atm_dev->esi[0], atm_dev->esi[1],
772                                atm_dev->esi[2], atm_dev->esi[3],
773                                atm_dev->esi[4], atm_dev->esi[5]);
774
775         if (!left--)
776                 return sprintf(page,
777                                "AAL5: tx %d ( %d err ), rx %d ( %d err, %d drop )\n",
778                                atomic_read(&atm_dev->stats.aal5.tx),
779                                atomic_read(&atm_dev->stats.aal5.tx_err),
780                                atomic_read(&atm_dev->stats.aal5.rx),
781                                atomic_read(&atm_dev->stats.aal5.rx_err),
782                                atomic_read(&atm_dev->stats.aal5.rx_drop));
783
784         if (!left--) {
785                 if (instance->disconnected)
786                         return sprintf(page, "Disconnected\n");
787                 else
788                         switch (atm_dev->signal) {
789                         case ATM_PHY_SIG_FOUND:
790                                 return sprintf(page, "Line up\n");
791                         case ATM_PHY_SIG_LOST:
792                                 return sprintf(page, "Line down\n");
793                         default:
794                                 return sprintf(page, "Line state unknown\n");
795                         }
796         }
797
798         return 0;
799 }
800
801 static int usbatm_atm_open(struct atm_vcc *vcc)
802 {
803         struct usbatm_data *instance = vcc->dev->dev_data;
804         struct usbatm_vcc_data *new = NULL;
805         int ret;
806         int vci = vcc->vci;
807         short vpi = vcc->vpi;
808
809         if (!instance) {
810                 dbg("%s: NULL data!", __func__);
811                 return -ENODEV;
812         }
813
814         atm_dbg(instance, "%s: vpi %hd, vci %d\n", __func__, vpi, vci);
815
816         /* only support AAL5 */
817         if ((vcc->qos.aal != ATM_AAL5)) {
818                 atm_warn(instance, "%s: unsupported ATM type %d!\n", __func__, vcc->qos.aal);
819                 return -EINVAL;
820         }
821
822         /* sanity checks */
823         if ((vcc->qos.rxtp.max_sdu < 0) || (vcc->qos.rxtp.max_sdu > ATM_MAX_AAL5_PDU)) {
824                 atm_dbg(instance, "%s: max_sdu %d out of range!\n", __func__, vcc->qos.rxtp.max_sdu);
825                 return -EINVAL;
826         }
827
828         mutex_lock(&instance->serialize);       /* vs self, usbatm_atm_close, usbatm_usb_disconnect */
829
830         if (instance->disconnected) {
831                 atm_dbg(instance, "%s: disconnected!\n", __func__);
832                 ret = -ENODEV;
833                 goto fail;
834         }
835
836         if (usbatm_find_vcc(instance, vpi, vci)) {
837                 atm_dbg(instance, "%s: %hd/%d already in use!\n", __func__, vpi, vci);
838                 ret = -EADDRINUSE;
839                 goto fail;
840         }
841
842         if (!(new = kzalloc(sizeof(struct usbatm_vcc_data), GFP_KERNEL))) {
843                 atm_err(instance, "%s: no memory for vcc_data!\n", __func__);
844                 ret = -ENOMEM;
845                 goto fail;
846         }
847
848         new->vcc = vcc;
849         new->vpi = vpi;
850         new->vci = vci;
851
852         new->sarb = alloc_skb(usbatm_pdu_length(vcc->qos.rxtp.max_sdu), GFP_KERNEL);
853         if (!new->sarb) {
854                 atm_err(instance, "%s: no memory for SAR buffer!\n", __func__);
855                 ret = -ENOMEM;
856                 goto fail;
857         }
858
859         vcc->dev_data = new;
860
861         tasklet_disable(&instance->rx_channel.tasklet);
862         instance->cached_vcc = new;
863         instance->cached_vpi = vpi;
864         instance->cached_vci = vci;
865         list_add(&new->list, &instance->vcc_list);
866         tasklet_enable(&instance->rx_channel.tasklet);
867
868         set_bit(ATM_VF_ADDR, &vcc->flags);
869         set_bit(ATM_VF_PARTIAL, &vcc->flags);
870         set_bit(ATM_VF_READY, &vcc->flags);
871
872         mutex_unlock(&instance->serialize);
873
874         atm_dbg(instance, "%s: allocated vcc data 0x%p\n", __func__, new);
875
876         return 0;
877
878 fail:
879         kfree(new);
880         mutex_unlock(&instance->serialize);
881         return ret;
882 }
883
884 static void usbatm_atm_close(struct atm_vcc *vcc)
885 {
886         struct usbatm_data *instance = vcc->dev->dev_data;
887         struct usbatm_vcc_data *vcc_data = vcc->dev_data;
888
889         if (!instance || !vcc_data) {
890                 dbg("%s: NULL data!", __func__);
891                 return;
892         }
893
894         atm_dbg(instance, "%s entered\n", __func__);
895
896         atm_dbg(instance, "%s: deallocating vcc 0x%p with vpi %d vci %d\n",
897                 __func__, vcc_data, vcc_data->vpi, vcc_data->vci);
898
899         usbatm_cancel_send(instance, vcc);
900
901         mutex_lock(&instance->serialize);       /* vs self, usbatm_atm_open, usbatm_usb_disconnect */
902
903         tasklet_disable(&instance->rx_channel.tasklet);
904         if (instance->cached_vcc == vcc_data) {
905                 instance->cached_vcc = NULL;
906                 instance->cached_vpi = ATM_VPI_UNSPEC;
907                 instance->cached_vci = ATM_VCI_UNSPEC;
908         }
909         list_del(&vcc_data->list);
910         tasklet_enable(&instance->rx_channel.tasklet);
911
912         kfree_skb(vcc_data->sarb);
913         vcc_data->sarb = NULL;
914
915         kfree(vcc_data);
916         vcc->dev_data = NULL;
917
918         vcc->vpi = ATM_VPI_UNSPEC;
919         vcc->vci = ATM_VCI_UNSPEC;
920         clear_bit(ATM_VF_READY, &vcc->flags);
921         clear_bit(ATM_VF_PARTIAL, &vcc->flags);
922         clear_bit(ATM_VF_ADDR, &vcc->flags);
923
924         mutex_unlock(&instance->serialize);
925
926         atm_dbg(instance, "%s successful\n", __func__);
927 }
928
929 static int usbatm_atm_ioctl(struct atm_dev *atm_dev, unsigned int cmd,
930                           void __user * arg)
931 {
932         struct usbatm_data *instance = atm_dev->dev_data;
933
934         if (!instance || instance->disconnected) {
935                 dbg("%s: %s!", __func__, instance ? "disconnected" : "NULL instance");
936                 return -ENODEV;
937         }
938
939         switch (cmd) {
940         case ATM_QUERYLOOP:
941                 return put_user(ATM_LM_NONE, (int __user *)arg) ? -EFAULT : 0;
942         default:
943                 return -ENOIOCTLCMD;
944         }
945 }
946
947 static int usbatm_atm_init(struct usbatm_data *instance)
948 {
949         struct atm_dev *atm_dev;
950         int ret, i;
951
952         /* ATM init.  The ATM initialization scheme suffers from an intrinsic race
953          * condition: callbacks we register can be executed at once, before we have
954          * initialized the struct atm_dev.  To protect against this, all callbacks
955          * abort if atm_dev->dev_data is NULL. */
956         atm_dev = atm_dev_register(instance->driver_name, &usbatm_atm_devops, -1, NULL);
957         if (!atm_dev) {
958                 usb_err(instance, "%s: failed to register ATM device!\n", __func__);
959                 return -1;
960         }
961
962         instance->atm_dev = atm_dev;
963
964         atm_dev->ci_range.vpi_bits = ATM_CI_MAX;
965         atm_dev->ci_range.vci_bits = ATM_CI_MAX;
966         atm_dev->signal = ATM_PHY_SIG_UNKNOWN;
967
968         /* temp init ATM device, set to 128kbit */
969         atm_dev->link_rate = 128 * 1000 / 424;
970
971         ret = sysfs_create_link(&atm_dev->class_dev.kobj,
972                                 &instance->usb_intf->dev.kobj, "device");
973         if (ret) {
974                 atm_err(instance, "%s: sysfs_create_link failed: %d\n",
975                                         __func__, ret);
976                 goto fail_sysfs;
977         }
978
979         if (instance->driver->atm_start && ((ret = instance->driver->atm_start(instance, atm_dev)) < 0)) {
980                 atm_err(instance, "%s: atm_start failed: %d!\n", __func__, ret);
981                 goto fail;
982         }
983
984         usbatm_get_instance(instance);  /* dropped in usbatm_atm_dev_close */
985
986         /* ready for ATM callbacks */
987         mb();
988         atm_dev->dev_data = instance;
989
990         /* submit all rx URBs */
991         for (i = 0; i < num_rcv_urbs; i++)
992                 usbatm_submit_urb(instance->urbs[i]);
993
994         return 0;
995
996  fail:
997         sysfs_remove_link(&atm_dev->class_dev.kobj, "device");
998  fail_sysfs:
999         instance->atm_dev = NULL;
1000         atm_dev_deregister(atm_dev); /* usbatm_atm_dev_close will eventually be called */
1001         return ret;
1002 }
1003
1004
1005 /**********
1006 **  USB  **
1007 **********/
1008
1009 static int usbatm_do_heavy_init(void *arg)
1010 {
1011         struct usbatm_data *instance = arg;
1012         int ret;
1013
1014         daemonize(instance->driver->driver_name);
1015         allow_signal(SIGTERM);
1016         instance->thread_pid = current->pid;
1017
1018         complete(&instance->thread_started);
1019
1020         ret = instance->driver->heavy_init(instance, instance->usb_intf);
1021
1022         if (!ret)
1023                 ret = usbatm_atm_init(instance);
1024
1025         mutex_lock(&instance->serialize);
1026         instance->thread_pid = -1;
1027         mutex_unlock(&instance->serialize);
1028
1029         complete_and_exit(&instance->thread_exited, ret);
1030 }
1031
1032 static int usbatm_heavy_init(struct usbatm_data *instance)
1033 {
1034         int ret = kernel_thread(usbatm_do_heavy_init, instance, CLONE_KERNEL);
1035
1036         if (ret < 0) {
1037                 usb_err(instance, "%s: failed to create kernel_thread (%d)!\n", __func__, ret);
1038                 return ret;
1039         }
1040
1041         wait_for_completion(&instance->thread_started);
1042
1043         return 0;
1044 }
1045
1046 static void usbatm_tasklet_schedule(unsigned long data)
1047 {
1048         tasklet_schedule((struct tasklet_struct *) data);
1049 }
1050
1051 static void usbatm_init_channel(struct usbatm_channel *channel)
1052 {
1053         spin_lock_init(&channel->lock);
1054         INIT_LIST_HEAD(&channel->list);
1055         channel->delay.function = usbatm_tasklet_schedule;
1056         channel->delay.data = (unsigned long) &channel->tasklet;
1057         init_timer(&channel->delay);
1058 }
1059
1060 int usbatm_usb_probe(struct usb_interface *intf, const struct usb_device_id *id,
1061                      struct usbatm_driver *driver)
1062 {
1063         struct device *dev = &intf->dev;
1064         struct usb_device *usb_dev = interface_to_usbdev(intf);
1065         struct usbatm_data *instance;
1066         char *buf;
1067         int error = -ENOMEM;
1068         int i, length;
1069         unsigned int maxpacket, num_packets;
1070
1071         dev_dbg(dev, "%s: trying driver %s with vendor=%04x, product=%04x, ifnum %2d\n",
1072                         __func__, driver->driver_name,
1073                         le16_to_cpu(usb_dev->descriptor.idVendor),
1074                         le16_to_cpu(usb_dev->descriptor.idProduct),
1075                         intf->altsetting->desc.bInterfaceNumber);
1076
1077         /* instance init */
1078         instance = kzalloc(sizeof(*instance) + sizeof(struct urb *) * (num_rcv_urbs + num_snd_urbs), GFP_KERNEL);
1079         if (!instance) {
1080                 dev_err(dev, "%s: no memory for instance data!\n", __func__);
1081                 return -ENOMEM;
1082         }
1083
1084         /* public fields */
1085
1086         instance->driver = driver;
1087         snprintf(instance->driver_name, sizeof(instance->driver_name), driver->driver_name);
1088
1089         instance->usb_dev = usb_dev;
1090         instance->usb_intf = intf;
1091
1092         buf = instance->description;
1093         length = sizeof(instance->description);
1094
1095         if ((i = usb_string(usb_dev, usb_dev->descriptor.iProduct, buf, length)) < 0)
1096                 goto bind;
1097
1098         buf += i;
1099         length -= i;
1100
1101         i = scnprintf(buf, length, " (");
1102         buf += i;
1103         length -= i;
1104
1105         if (length <= 0 || (i = usb_make_path(usb_dev, buf, length)) < 0)
1106                 goto bind;
1107
1108         buf += i;
1109         length -= i;
1110
1111         snprintf(buf, length, ")");
1112
1113  bind:
1114         if (driver->bind && (error = driver->bind(instance, intf, id)) < 0) {
1115                         dev_err(dev, "%s: bind failed: %d!\n", __func__, error);
1116                         goto fail_free;
1117         }
1118
1119         /* private fields */
1120
1121         kref_init(&instance->refcount);         /* dropped in usbatm_usb_disconnect */
1122         mutex_init(&instance->serialize);
1123
1124         instance->thread_pid = -1;
1125         init_completion(&instance->thread_started);
1126         init_completion(&instance->thread_exited);
1127
1128         INIT_LIST_HEAD(&instance->vcc_list);
1129         skb_queue_head_init(&instance->sndqueue);
1130
1131         usbatm_init_channel(&instance->rx_channel);
1132         usbatm_init_channel(&instance->tx_channel);
1133         tasklet_init(&instance->rx_channel.tasklet, usbatm_rx_process, (unsigned long)instance);
1134         tasklet_init(&instance->tx_channel.tasklet, usbatm_tx_process, (unsigned long)instance);
1135         instance->rx_channel.stride = ATM_CELL_SIZE + driver->rx_padding;
1136         instance->tx_channel.stride = ATM_CELL_SIZE + driver->tx_padding;
1137         instance->rx_channel.usbatm = instance->tx_channel.usbatm = instance;
1138
1139         if ((instance->flags & UDSL_USE_ISOC) && driver->isoc_in)
1140                 instance->rx_channel.endpoint = usb_rcvisocpipe(usb_dev, driver->isoc_in);
1141         else
1142                 instance->rx_channel.endpoint = usb_rcvbulkpipe(usb_dev, driver->bulk_in);
1143
1144         instance->tx_channel.endpoint = usb_sndbulkpipe(usb_dev, driver->bulk_out);
1145
1146         /* tx buffer size must be a positive multiple of the stride */
1147         instance->tx_channel.buf_size = max (instance->tx_channel.stride,
1148                         snd_buf_bytes - (snd_buf_bytes % instance->tx_channel.stride));
1149
1150         /* rx buffer size must be a positive multiple of the endpoint maxpacket */
1151         maxpacket = usb_maxpacket(usb_dev, instance->rx_channel.endpoint, 0);
1152
1153         if ((maxpacket < 1) || (maxpacket > UDSL_MAX_BUF_SIZE)) {
1154                 dev_err(dev, "%s: invalid endpoint %02x!\n", __func__,
1155                                 usb_pipeendpoint(instance->rx_channel.endpoint));
1156                 error = -EINVAL;
1157                 goto fail_unbind;
1158         }
1159
1160         num_packets = max (1U, (rcv_buf_bytes + maxpacket / 2) / maxpacket); /* round */
1161
1162         if (num_packets * maxpacket > UDSL_MAX_BUF_SIZE)
1163                 num_packets--;
1164
1165         instance->rx_channel.buf_size = num_packets * maxpacket;
1166         instance->rx_channel.packet_size = maxpacket;
1167
1168 #ifdef DEBUG
1169         for (i = 0; i < 2; i++) {
1170                 struct usbatm_channel *channel = i ?
1171                         &instance->tx_channel : &instance->rx_channel;
1172
1173                 dev_dbg(dev, "%s: using %d byte buffer for %s channel 0x%p\n", __func__, channel->buf_size, i ? "tx" : "rx", channel);
1174         }
1175 #endif
1176
1177         /* initialize urbs */
1178
1179         for (i = 0; i < num_rcv_urbs + num_snd_urbs; i++) {
1180                 u8 *buffer;
1181                 struct usbatm_channel *channel = i < num_rcv_urbs ?
1182                         &instance->rx_channel : &instance->tx_channel;
1183                 struct urb *urb;
1184                 unsigned int iso_packets = usb_pipeisoc(channel->endpoint) ? channel->buf_size / channel->packet_size : 0;
1185
1186                 UDSL_ASSERT(!usb_pipeisoc(channel->endpoint) || usb_pipein(channel->endpoint));
1187
1188                 urb = usb_alloc_urb(iso_packets, GFP_KERNEL);
1189                 if (!urb) {
1190                         dev_err(dev, "%s: no memory for urb %d!\n", __func__, i);
1191                         error = -ENOMEM;
1192                         goto fail_unbind;
1193                 }
1194
1195                 instance->urbs[i] = urb;
1196
1197                 /* zero the tx padding to avoid leaking information */
1198                 buffer = kzalloc(channel->buf_size, GFP_KERNEL);
1199                 if (!buffer) {
1200                         dev_err(dev, "%s: no memory for buffer %d!\n", __func__, i);
1201                         error = -ENOMEM;
1202                         goto fail_unbind;
1203                 }
1204
1205                 usb_fill_bulk_urb(urb, instance->usb_dev, channel->endpoint,
1206                                   buffer, channel->buf_size, usbatm_complete, channel);
1207                 if (iso_packets) {
1208                         int j;
1209                         urb->interval = 1;
1210                         urb->transfer_flags = URB_ISO_ASAP;
1211                         urb->number_of_packets = iso_packets;
1212                         for (j = 0; j < iso_packets; j++) {
1213                                 urb->iso_frame_desc[j].offset = channel->packet_size * j;
1214                                 urb->iso_frame_desc[j].length = channel->packet_size;
1215                         }
1216                 }
1217
1218                 /* put all tx URBs on the list of spares */
1219                 if (i >= num_rcv_urbs)
1220                         list_add_tail(&urb->urb_list, &channel->list);
1221
1222                 vdbg("%s: alloced buffer 0x%p buf size %u urb 0x%p",
1223                      __func__, urb->transfer_buffer, urb->transfer_buffer_length, urb);
1224         }
1225
1226         instance->cached_vpi = ATM_VPI_UNSPEC;
1227         instance->cached_vci = ATM_VCI_UNSPEC;
1228         instance->cell_buf = kmalloc(instance->rx_channel.stride, GFP_KERNEL);
1229
1230         if (!instance->cell_buf) {
1231                 dev_err(dev, "%s: no memory for cell buffer!\n", __func__);
1232                 error = -ENOMEM;
1233                 goto fail_unbind;
1234         }
1235
1236         if (!(instance->flags & UDSL_SKIP_HEAVY_INIT) && driver->heavy_init) {
1237                 error = usbatm_heavy_init(instance);
1238         } else {
1239                 complete(&instance->thread_exited);     /* pretend that heavy_init was run */
1240                 error = usbatm_atm_init(instance);
1241         }
1242
1243         if (error < 0)
1244                 goto fail_unbind;
1245
1246         usb_get_dev(usb_dev);
1247         usb_set_intfdata(intf, instance);
1248
1249         return 0;
1250
1251  fail_unbind:
1252         if (instance->driver->unbind)
1253                 instance->driver->unbind(instance, intf);
1254  fail_free:
1255         kfree(instance->cell_buf);
1256
1257         for (i = 0; i < num_rcv_urbs + num_snd_urbs; i++) {
1258                 if (instance->urbs[i])
1259                         kfree(instance->urbs[i]->transfer_buffer);
1260                 usb_free_urb(instance->urbs[i]);
1261         }
1262
1263         kfree (instance);
1264
1265         return error;
1266 }
1267 EXPORT_SYMBOL_GPL(usbatm_usb_probe);
1268
1269 void usbatm_usb_disconnect(struct usb_interface *intf)
1270 {
1271         struct device *dev = &intf->dev;
1272         struct usbatm_data *instance = usb_get_intfdata(intf);
1273         struct usbatm_vcc_data *vcc_data;
1274         int i;
1275
1276         dev_dbg(dev, "%s entered\n", __func__);
1277
1278         if (!instance) {
1279                 dev_dbg(dev, "%s: NULL instance!\n", __func__);
1280                 return;
1281         }
1282
1283         usb_set_intfdata(intf, NULL);
1284
1285         mutex_lock(&instance->serialize);
1286         instance->disconnected = 1;
1287         if (instance->thread_pid >= 0)
1288                 kill_proc(instance->thread_pid, SIGTERM, 1);
1289         mutex_unlock(&instance->serialize);
1290
1291         wait_for_completion(&instance->thread_exited);
1292
1293         mutex_lock(&instance->serialize);
1294         list_for_each_entry(vcc_data, &instance->vcc_list, list)
1295                 vcc_release_async(vcc_data->vcc, -EPIPE);
1296         mutex_unlock(&instance->serialize);
1297
1298         tasklet_disable(&instance->rx_channel.tasklet);
1299         tasklet_disable(&instance->tx_channel.tasklet);
1300
1301         for (i = 0; i < num_rcv_urbs + num_snd_urbs; i++)
1302                 usb_kill_urb(instance->urbs[i]);
1303
1304         del_timer_sync(&instance->rx_channel.delay);
1305         del_timer_sync(&instance->tx_channel.delay);
1306
1307         /* turn usbatm_[rt]x_process into something close to a no-op */
1308         /* no need to take the spinlock */
1309         INIT_LIST_HEAD(&instance->rx_channel.list);
1310         INIT_LIST_HEAD(&instance->tx_channel.list);
1311
1312         tasklet_enable(&instance->rx_channel.tasklet);
1313         tasklet_enable(&instance->tx_channel.tasklet);
1314
1315         if (instance->atm_dev && instance->driver->atm_stop)
1316                 instance->driver->atm_stop(instance, instance->atm_dev);
1317
1318         if (instance->driver->unbind)
1319                 instance->driver->unbind(instance, intf);
1320
1321         instance->driver_data = NULL;
1322
1323         for (i = 0; i < num_rcv_urbs + num_snd_urbs; i++) {
1324                 kfree(instance->urbs[i]->transfer_buffer);
1325                 usb_free_urb(instance->urbs[i]);
1326         }
1327
1328         kfree(instance->cell_buf);
1329
1330         /* ATM finalize */
1331         if (instance->atm_dev) {
1332                 sysfs_remove_link(&instance->atm_dev->class_dev.kobj, "device");
1333                 atm_dev_deregister(instance->atm_dev);
1334         }
1335
1336         usbatm_put_instance(instance);  /* taken in usbatm_usb_probe */
1337 }
1338 EXPORT_SYMBOL_GPL(usbatm_usb_disconnect);
1339
1340
1341 /***********
1342 **  init  **
1343 ***********/
1344
1345 static int __init usbatm_usb_init(void)
1346 {
1347         dbg("%s: driver version %s", __func__, DRIVER_VERSION);
1348
1349         if (sizeof(struct usbatm_control) > sizeof(((struct sk_buff *) 0)->cb)) {
1350                 printk(KERN_ERR "%s unusable with this kernel!\n", usbatm_driver_name);
1351                 return -EIO;
1352         }
1353
1354         if ((num_rcv_urbs > UDSL_MAX_RCV_URBS)
1355             || (num_snd_urbs > UDSL_MAX_SND_URBS)
1356             || (rcv_buf_bytes < 1)
1357             || (rcv_buf_bytes > UDSL_MAX_BUF_SIZE)
1358             || (snd_buf_bytes < 1)
1359             || (snd_buf_bytes > UDSL_MAX_BUF_SIZE))
1360                 return -EINVAL;
1361
1362         return 0;
1363 }
1364 module_init(usbatm_usb_init);
1365
1366 static void __exit usbatm_usb_exit(void)
1367 {
1368         dbg("%s", __func__);
1369 }
1370 module_exit(usbatm_usb_exit);
1371
1372 MODULE_AUTHOR(DRIVER_AUTHOR);
1373 MODULE_DESCRIPTION(DRIVER_DESC);
1374 MODULE_LICENSE("GPL");
1375 MODULE_VERSION(DRIVER_VERSION);
1376
1377 /************
1378 **  debug  **
1379 ************/
1380
1381 #ifdef VERBOSE_DEBUG
1382 static int usbatm_print_packet(const unsigned char *data, int len)
1383 {
1384         unsigned char buffer[256];
1385         int i = 0, j = 0;
1386
1387         for (i = 0; i < len;) {
1388                 buffer[0] = '\0';
1389                 sprintf(buffer, "%.3d :", i);
1390                 for (j = 0; (j < 16) && (i < len); j++, i++) {
1391                         sprintf(buffer, "%s %2.2x", buffer, data[i]);
1392                 }
1393                 dbg("%s", buffer);
1394         }
1395         return i;
1396 }
1397 #endif