779f86ecdd76a76403fa9fb43b09a41711504c4e
[safe/jmp/linux-2.6] / 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/proc_fs.h>
76 #include <linux/sched.h>
77 #include <linux/signal.h>
78 #include <linux/slab.h>
79 #include <linux/smp_lock.h>
80 #include <linux/stat.h>
81 #include <linux/timer.h>
82 #include <linux/wait.h>
83
84 #ifdef VERBOSE_DEBUG
85 static int usbatm_print_packet(const unsigned char *data, int len);
86 #define PACKETDEBUG(arg...)     usbatm_print_packet (arg)
87 #define vdbg(arg...)            dbg (arg)
88 #else
89 #define PACKETDEBUG(arg...)
90 #define vdbg(arg...)
91 #endif
92
93 #define DRIVER_AUTHOR   "Johan Verrept, Duncan Sands <duncan.sands@free.fr>"
94 #define DRIVER_VERSION  "1.9"
95 #define DRIVER_DESC     "Generic USB ATM/DSL I/O, version " DRIVER_VERSION
96
97 static const char usbatm_driver_name[] = "usbatm";
98
99 #define UDSL_MAX_RCV_URBS               16
100 #define UDSL_MAX_SND_URBS               16
101 #define UDSL_MAX_RCV_BUF_SIZE           1024    /* ATM cells */
102 #define UDSL_MAX_SND_BUF_SIZE           1024    /* ATM cells */
103 #define UDSL_DEFAULT_RCV_URBS           4
104 #define UDSL_DEFAULT_SND_URBS           4
105 #define UDSL_DEFAULT_RCV_BUF_SIZE       64      /* ATM cells */
106 #define UDSL_DEFAULT_SND_BUF_SIZE       64      /* ATM cells */
107
108 #define ATM_CELL_HEADER                 (ATM_CELL_SIZE - ATM_CELL_PAYLOAD)
109
110 #define THROTTLE_MSECS                  100     /* delay to recover processing after urb submission fails */
111
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_size = UDSL_DEFAULT_RCV_BUF_SIZE;
115 static unsigned int snd_buf_size = 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_size, uint, S_IRUGO);
130 MODULE_PARM_DESC(rcv_buf_size,
131                  "Size of the buffers used for reception in ATM cells (range: 1-"
132                  __MODULE_STRING(UDSL_MAX_RCV_BUF_SIZE) ", default: "
133                  __MODULE_STRING(UDSL_DEFAULT_RCV_BUF_SIZE) ")");
134
135 module_param(snd_buf_size, uint, S_IRUGO);
136 MODULE_PARM_DESC(snd_buf_size,
137                  "Size of the buffers used for transmission in ATM cells (range: 1-"
138                  __MODULE_STRING(UDSL_MAX_SND_BUF_SIZE) ", default: "
139                  __MODULE_STRING(UDSL_DEFAULT_SND_BUF_SIZE) ")");
140
141
142 /* receive */
143
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(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, struct pt_regs *regs)
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                 if (printk_ratelimit())
275                         atm_warn(channel->usbatm, "%s: urb 0x%p failed (%d)!\n",
276                                 __func__, urb, urb->status);
277                 /* throttle processing in case of an error */
278                 mod_timer(&channel->delay, jiffies + msecs_to_jiffies(THROTTLE_MSECS));
279         } else
280                 tasklet_schedule(&channel->tasklet);
281 }
282
283
284 /*************
285 **  decode  **
286 *************/
287
288 static inline struct usbatm_vcc_data *usbatm_find_vcc(struct usbatm_data *instance,
289                                                   short vpi, int vci)
290 {
291         struct usbatm_vcc_data *vcc_data;
292
293         list_for_each_entry(vcc_data, &instance->vcc_list, list)
294                 if ((vcc_data->vci == vci) && (vcc_data->vpi == vpi))
295                         return vcc_data;
296         return NULL;
297 }
298
299 static void usbatm_extract_cells(struct usbatm_data *instance,
300                                unsigned char *source, unsigned int avail_data)
301 {
302         struct usbatm_vcc_data *cached_vcc = NULL;
303         struct atm_vcc *vcc;
304         struct sk_buff *sarb;
305         unsigned int stride = instance->rx_channel.stride;
306         int vci, cached_vci = 0;
307         short vpi, cached_vpi = 0;
308         u8 pti;
309
310         for (; avail_data >= stride; avail_data -= stride, source += stride) {
311                 vpi = ((source[0] & 0x0f) << 4)  | (source[1] >> 4);
312                 vci = ((source[1] & 0x0f) << 12) | (source[2] << 4) | (source[3] >> 4);
313                 pti = ((source[3] & 0xe) >> 1);
314
315                 vdbg("%s: vpi %hd, vci %d, pti %d", __func__, vpi, vci, pti);
316
317                 if ((vci != cached_vci) || (vpi != cached_vpi)) {
318                         cached_vpi = vpi;
319                         cached_vci = vci;
320
321                         cached_vcc = usbatm_find_vcc(instance, vpi, vci);
322
323                         if (!cached_vcc)
324                                 atm_rldbg(instance, "%s: unknown vpi/vci (%hd/%d)!\n", __func__, vpi, vci);
325                 }
326
327                 if (!cached_vcc)
328                         continue;
329
330                 vcc = cached_vcc->vcc;
331
332                 /* OAM F5 end-to-end */
333                 if (pti == ATM_PTI_E2EF5) {
334                         if (printk_ratelimit())
335                                 atm_warn(instance, "%s: OAM not supported (vpi %d, vci %d)!\n",
336                                         __func__, vpi, vci);
337                         atomic_inc(&vcc->stats->rx_err);
338                         continue;
339                 }
340
341                 sarb = cached_vcc->sarb;
342
343                 if (sarb->tail + ATM_CELL_PAYLOAD > sarb->end) {
344                         atm_rldbg(instance, "%s: buffer overrun (sarb->len %u, vcc: 0x%p)!\n",
345                                         __func__, sarb->len, vcc);
346                         /* discard cells already received */
347                         skb_trim(sarb, 0);
348                         UDSL_ASSERT(sarb->tail + ATM_CELL_PAYLOAD <= sarb->end);
349                 }
350
351                 memcpy(sarb->tail, source + ATM_CELL_HEADER, ATM_CELL_PAYLOAD);
352                 __skb_put(sarb, ATM_CELL_PAYLOAD);
353
354                 if (pti & 1) {
355                         struct sk_buff *skb;
356                         unsigned int length;
357                         unsigned int pdu_length;
358
359                         length = (source[ATM_CELL_SIZE - 6] << 8) + source[ATM_CELL_SIZE - 5];
360
361                         /* guard against overflow */
362                         if (length > ATM_MAX_AAL5_PDU) {
363                                 atm_rldbg(instance, "%s: bogus length %u (vcc: 0x%p)!\n",
364                                                 __func__, length, vcc);
365                                 atomic_inc(&vcc->stats->rx_err);
366                                 goto out;
367                         }
368
369                         pdu_length = usbatm_pdu_length(length);
370
371                         if (sarb->len < pdu_length) {
372                                 atm_rldbg(instance, "%s: bogus pdu_length %u (sarb->len: %u, vcc: 0x%p)!\n",
373                                                 __func__, pdu_length, sarb->len, vcc);
374                                 atomic_inc(&vcc->stats->rx_err);
375                                 goto out;
376                         }
377
378                         if (crc32_be(~0, sarb->tail - pdu_length, pdu_length) != 0xc704dd7b) {
379                                 atm_rldbg(instance, "%s: packet failed crc check (vcc: 0x%p)!\n",
380                                                 __func__, vcc);
381                                 atomic_inc(&vcc->stats->rx_err);
382                                 goto out;
383                         }
384
385                         vdbg("%s: got packet (length: %u, pdu_length: %u, vcc: 0x%p)", __func__, length, pdu_length, vcc);
386
387                         if (!(skb = dev_alloc_skb(length))) {
388                                 if (printk_ratelimit())
389                                         atm_err(instance, "%s: no memory for skb (length: %u)!\n",
390                                                         __func__, length);
391                                 atomic_inc(&vcc->stats->rx_drop);
392                                 goto out;
393                         }
394
395                         vdbg("%s: allocated new sk_buff (skb: 0x%p, skb->truesize: %u)", __func__, skb, skb->truesize);
396
397                         if (!atm_charge(vcc, skb->truesize)) {
398                                 atm_rldbg(instance, "%s: failed atm_charge (skb->truesize: %u)!\n",
399                                                 __func__, skb->truesize);
400                                 dev_kfree_skb(skb);
401                                 goto out;       /* atm_charge increments rx_drop */
402                         }
403
404                         memcpy(skb->data, sarb->tail - pdu_length, length);
405                         __skb_put(skb, length);
406
407                         vdbg("%s: sending skb 0x%p, skb->len %u, skb->truesize %u",
408                              __func__, skb, skb->len, skb->truesize);
409
410                         PACKETDEBUG(skb->data, skb->len);
411
412                         vcc->push(vcc, skb);
413
414                         atomic_inc(&vcc->stats->rx);
415                 out:
416                         skb_trim(sarb, 0);
417                 }
418         }
419 }
420
421
422 /*************
423 **  encode  **
424 *************/
425
426 static unsigned int usbatm_write_cells(struct usbatm_data *instance,
427                                        struct sk_buff *skb,
428                                        u8 *target, unsigned int avail_space)
429 {
430         struct usbatm_control *ctrl = UDSL_SKB(skb);
431         struct atm_vcc *vcc = ctrl->atm.vcc;
432         unsigned int num_written;
433         unsigned int stride = instance->tx_channel.stride;
434
435         vdbg("%s: skb->len=%d, avail_space=%u", __func__, skb->len, avail_space);
436         UDSL_ASSERT(!(avail_space % stride));
437
438         for (num_written = 0; num_written < avail_space && ctrl->len;
439              num_written += stride, target += stride) {
440                 unsigned int data_len = min_t(unsigned int, skb->len, ATM_CELL_PAYLOAD);
441                 unsigned int left = ATM_CELL_PAYLOAD - data_len;
442                 u8 *ptr = target;
443
444                 ptr[0] = vcc->vpi >> 4;
445                 ptr[1] = (vcc->vpi << 4) | (vcc->vci >> 12);
446                 ptr[2] = vcc->vci >> 4;
447                 ptr[3] = vcc->vci << 4;
448                 ptr[4] = 0xec;
449                 ptr += ATM_CELL_HEADER;
450
451                 memcpy(ptr, skb->data, data_len);
452                 ptr += data_len;
453                 __skb_pull(skb, data_len);
454
455                 if(!left)
456                         continue;
457
458                 memset(ptr, 0, left);
459
460                 if (left >= ATM_AAL5_TRAILER) { /* trailer will go in this cell */
461                         u8 *trailer = target + ATM_CELL_SIZE - ATM_AAL5_TRAILER;
462                         /* trailer[0] = 0;              UU = 0 */
463                         /* trailer[1] = 0;              CPI = 0 */
464                         trailer[2] = ctrl->len >> 8;
465                         trailer[3] = ctrl->len;
466
467                         ctrl->crc = ~ crc32_be(ctrl->crc, ptr, left - 4);
468
469                         trailer[4] = ctrl->crc >> 24;
470                         trailer[5] = ctrl->crc >> 16;
471                         trailer[6] = ctrl->crc >> 8;
472                         trailer[7] = ctrl->crc;
473
474                         target[3] |= 0x2;       /* adjust PTI */
475
476                         ctrl->len = 0;          /* tag this skb finished */
477                 }
478                 else
479                         ctrl->crc = crc32_be(ctrl->crc, ptr, left);
480         }
481
482         return num_written;
483 }
484
485
486 /**************
487 **  receive  **
488 **************/
489
490 static void usbatm_rx_process(unsigned long data)
491 {
492         struct usbatm_data *instance = (struct usbatm_data *)data;
493         struct urb *urb;
494
495         while ((urb = usbatm_pop_urb(&instance->rx_channel))) {
496                 vdbg("%s: processing urb 0x%p", __func__, urb);
497
498                 if (usb_pipeisoc(urb->pipe)) {
499                         int i;
500                         for (i = 0; i < urb->number_of_packets; i++)
501                                 if (!urb->iso_frame_desc[i].status)
502                                         usbatm_extract_cells(instance,
503                                                              (u8 *)urb->transfer_buffer + urb->iso_frame_desc[i].offset,
504                                                              urb->iso_frame_desc[i].actual_length);
505                 }
506                 else
507                         if (!urb->status)
508                                 usbatm_extract_cells(instance, urb->transfer_buffer, urb->actual_length);
509
510                 if (usbatm_submit_urb(urb))
511                         return;
512         }
513 }
514
515
516 /***********
517 **  send  **
518 ***********/
519
520 static void usbatm_tx_process(unsigned long data)
521 {
522         struct usbatm_data *instance = (struct usbatm_data *)data;
523         struct sk_buff *skb = instance->current_skb;
524         struct urb *urb = NULL;
525         const unsigned int buf_size = instance->tx_channel.buf_size;
526         unsigned int num_written = 0;
527         u8 *buffer = NULL;
528
529         if (!skb)
530                 skb = skb_dequeue(&instance->sndqueue);
531
532         while (skb) {
533                 if (!urb) {
534                         urb = usbatm_pop_urb(&instance->tx_channel);
535                         if (!urb)
536                                 break;          /* no more senders */
537                         buffer = urb->transfer_buffer;
538                         num_written = (urb->status == -EAGAIN) ?
539                                 urb->transfer_buffer_length : 0;
540                 }
541
542                 num_written += usbatm_write_cells(instance, skb,
543                                                   buffer + num_written,
544                                                   buf_size - num_written);
545
546                 vdbg("%s: wrote %u bytes from skb 0x%p to urb 0x%p",
547                      __func__, num_written, skb, urb);
548
549                 if (!UDSL_SKB(skb)->len) {
550                         struct atm_vcc *vcc = UDSL_SKB(skb)->atm.vcc;
551
552                         usbatm_pop(vcc, skb);
553                         atomic_inc(&vcc->stats->tx);
554
555                         skb = skb_dequeue(&instance->sndqueue);
556                 }
557
558                 if (num_written == buf_size || (!skb && num_written)) {
559                         urb->transfer_buffer_length = num_written;
560
561                         if (usbatm_submit_urb(urb))
562                                 break;
563                         urb = NULL;
564                 }
565         }
566
567         instance->current_skb = skb;
568 }
569
570 static void usbatm_cancel_send(struct usbatm_data *instance,
571                                struct atm_vcc *vcc)
572 {
573         struct sk_buff *skb, *n;
574
575         atm_dbg(instance, "%s entered\n", __func__);
576         spin_lock_irq(&instance->sndqueue.lock);
577         for (skb = instance->sndqueue.next, n = skb->next;
578              skb != (struct sk_buff *)&instance->sndqueue;
579              skb = n, n = skb->next)
580                 if (UDSL_SKB(skb)->atm.vcc == vcc) {
581                         atm_dbg(instance, "%s: popping skb 0x%p\n", __func__, skb);
582                         __skb_unlink(skb, &instance->sndqueue);
583                         usbatm_pop(vcc, skb);
584                 }
585         spin_unlock_irq(&instance->sndqueue.lock);
586
587         tasklet_disable(&instance->tx_channel.tasklet);
588         if ((skb = instance->current_skb) && (UDSL_SKB(skb)->atm.vcc == vcc)) {
589                 atm_dbg(instance, "%s: popping current skb (0x%p)\n", __func__, skb);
590                 instance->current_skb = NULL;
591                 usbatm_pop(vcc, skb);
592         }
593         tasklet_enable(&instance->tx_channel.tasklet);
594         atm_dbg(instance, "%s done\n", __func__);
595 }
596
597 static int usbatm_atm_send(struct atm_vcc *vcc, struct sk_buff *skb)
598 {
599         struct usbatm_data *instance = vcc->dev->dev_data;
600         struct usbatm_control *ctrl = UDSL_SKB(skb);
601         int err;
602
603         vdbg("%s called (skb 0x%p, len %u)", __func__, skb, skb->len);
604
605         if (!instance) {
606                 dbg("%s: NULL data!", __func__);
607                 err = -ENODEV;
608                 goto fail;
609         }
610
611         if (vcc->qos.aal != ATM_AAL5) {
612                 atm_rldbg(instance, "%s: unsupported ATM type %d!\n", __func__, vcc->qos.aal);
613                 err = -EINVAL;
614                 goto fail;
615         }
616
617         if (skb->len > ATM_MAX_AAL5_PDU) {
618                 atm_rldbg(instance, "%s: packet too long (%d vs %d)!\n",
619                                 __func__, skb->len, ATM_MAX_AAL5_PDU);
620                 err = -EINVAL;
621                 goto fail;
622         }
623
624         PACKETDEBUG(skb->data, skb->len);
625
626         /* initialize the control block */
627         ctrl->atm.vcc = vcc;
628         ctrl->len = skb->len;
629         ctrl->crc = crc32_be(~0, skb->data, skb->len);
630
631         skb_queue_tail(&instance->sndqueue, skb);
632         tasklet_schedule(&instance->tx_channel.tasklet);
633
634         return 0;
635
636  fail:
637         usbatm_pop(vcc, skb);
638         return err;
639 }
640
641
642 /********************
643 **  bean counting  **
644 ********************/
645
646 static void usbatm_destroy_instance(struct kref *kref)
647 {
648         struct usbatm_data *instance = container_of(kref, struct usbatm_data, refcount);
649
650         dbg("%s", __func__);
651
652         tasklet_kill(&instance->rx_channel.tasklet);
653         tasklet_kill(&instance->tx_channel.tasklet);
654         usb_put_dev(instance->usb_dev);
655         kfree(instance);
656 }
657
658 static void usbatm_get_instance(struct usbatm_data *instance)
659 {
660         dbg("%s", __func__);
661
662         kref_get(&instance->refcount);
663 }
664
665 static void usbatm_put_instance(struct usbatm_data *instance)
666 {
667         dbg("%s", __func__);
668
669         kref_put(&instance->refcount, usbatm_destroy_instance);
670 }
671
672
673 /**********
674 **  ATM  **
675 **********/
676
677 static void usbatm_atm_dev_close(struct atm_dev *atm_dev)
678 {
679         struct usbatm_data *instance = atm_dev->dev_data;
680
681         dbg("%s", __func__);
682
683         if (!instance)
684                 return;
685
686         atm_dev->dev_data = NULL; /* catch bugs */
687         usbatm_put_instance(instance);  /* taken in usbatm_atm_init */
688 }
689
690 static int usbatm_atm_proc_read(struct atm_dev *atm_dev, loff_t * pos, char *page)
691 {
692         struct usbatm_data *instance = atm_dev->dev_data;
693         int left = *pos;
694
695         if (!instance) {
696                 dbg("%s: NULL instance!", __func__);
697                 return -ENODEV;
698         }
699
700         if (!left--)
701                 return sprintf(page, "%s\n", instance->description);
702
703         if (!left--)
704                 return sprintf(page, "MAC: %02x:%02x:%02x:%02x:%02x:%02x\n",
705                                atm_dev->esi[0], atm_dev->esi[1],
706                                atm_dev->esi[2], atm_dev->esi[3],
707                                atm_dev->esi[4], atm_dev->esi[5]);
708
709         if (!left--)
710                 return sprintf(page,
711                                "AAL5: tx %d ( %d err ), rx %d ( %d err, %d drop )\n",
712                                atomic_read(&atm_dev->stats.aal5.tx),
713                                atomic_read(&atm_dev->stats.aal5.tx_err),
714                                atomic_read(&atm_dev->stats.aal5.rx),
715                                atomic_read(&atm_dev->stats.aal5.rx_err),
716                                atomic_read(&atm_dev->stats.aal5.rx_drop));
717
718         if (!left--)
719                 switch (atm_dev->signal) {
720                 case ATM_PHY_SIG_FOUND:
721                         return sprintf(page, "Line up\n");
722                 case ATM_PHY_SIG_LOST:
723                         return sprintf(page, "Line down\n");
724                 default:
725                         return sprintf(page, "Line state unknown\n");
726                 }
727
728         return 0;
729 }
730
731 static int usbatm_atm_open(struct atm_vcc *vcc)
732 {
733         struct usbatm_data *instance = vcc->dev->dev_data;
734         struct usbatm_vcc_data *new = NULL;
735         int ret;
736         int vci = vcc->vci;
737         short vpi = vcc->vpi;
738
739         if (!instance) {
740                 dbg("%s: NULL data!", __func__);
741                 return -ENODEV;
742         }
743
744         atm_dbg(instance, "%s: vpi %hd, vci %d\n", __func__, vpi, vci);
745
746         /* only support AAL5 */
747         if ((vcc->qos.aal != ATM_AAL5)) {
748                 atm_warn(instance, "%s: unsupported ATM type %d!\n", __func__, vcc->qos.aal);
749                 return -EINVAL;
750         }
751
752         /* sanity checks */
753         if ((vcc->qos.rxtp.max_sdu < 0) || (vcc->qos.rxtp.max_sdu > ATM_MAX_AAL5_PDU)) {
754                 atm_dbg(instance, "%s: max_sdu %d out of range!\n", __func__, vcc->qos.rxtp.max_sdu);
755                 return -EINVAL;
756         }
757
758         down(&instance->serialize);     /* vs self, usbatm_atm_close, usbatm_usb_disconnect */
759
760         if (usbatm_find_vcc(instance, vpi, vci)) {
761                 atm_dbg(instance, "%s: %hd/%d already in use!\n", __func__, vpi, vci);
762                 ret = -EADDRINUSE;
763                 goto fail;
764         }
765
766         if (!(new = kmalloc(sizeof(struct usbatm_vcc_data), GFP_KERNEL))) {
767                 atm_err(instance, "%s: no memory for vcc_data!\n", __func__);
768                 ret = -ENOMEM;
769                 goto fail;
770         }
771
772         memset(new, 0, sizeof(struct usbatm_vcc_data));
773         new->vcc = vcc;
774         new->vpi = vpi;
775         new->vci = vci;
776
777         new->sarb = alloc_skb(usbatm_pdu_length(vcc->qos.rxtp.max_sdu), GFP_KERNEL);
778         if (!new->sarb) {
779                 atm_err(instance, "%s: no memory for SAR buffer!\n", __func__);
780                 ret = -ENOMEM;
781                 goto fail;
782         }
783
784         vcc->dev_data = new;
785
786         tasklet_disable(&instance->rx_channel.tasklet);
787         list_add(&new->list, &instance->vcc_list);
788         tasklet_enable(&instance->rx_channel.tasklet);
789
790         set_bit(ATM_VF_ADDR, &vcc->flags);
791         set_bit(ATM_VF_PARTIAL, &vcc->flags);
792         set_bit(ATM_VF_READY, &vcc->flags);
793
794         up(&instance->serialize);
795
796         atm_dbg(instance, "%s: allocated vcc data 0x%p\n", __func__, new);
797
798         return 0;
799
800 fail:
801         kfree(new);
802         up(&instance->serialize);
803         return ret;
804 }
805
806 static void usbatm_atm_close(struct atm_vcc *vcc)
807 {
808         struct usbatm_data *instance = vcc->dev->dev_data;
809         struct usbatm_vcc_data *vcc_data = vcc->dev_data;
810
811         if (!instance || !vcc_data) {
812                 dbg("%s: NULL data!", __func__);
813                 return;
814         }
815
816         atm_dbg(instance, "%s entered\n", __func__);
817
818         atm_dbg(instance, "%s: deallocating vcc 0x%p with vpi %d vci %d\n",
819                 __func__, vcc_data, vcc_data->vpi, vcc_data->vci);
820
821         usbatm_cancel_send(instance, vcc);
822
823         down(&instance->serialize);     /* vs self, usbatm_atm_open, usbatm_usb_disconnect */
824
825         tasklet_disable(&instance->rx_channel.tasklet);
826         list_del(&vcc_data->list);
827         tasklet_enable(&instance->rx_channel.tasklet);
828
829         kfree_skb(vcc_data->sarb);
830         vcc_data->sarb = NULL;
831
832         kfree(vcc_data);
833         vcc->dev_data = NULL;
834
835         vcc->vpi = ATM_VPI_UNSPEC;
836         vcc->vci = ATM_VCI_UNSPEC;
837         clear_bit(ATM_VF_READY, &vcc->flags);
838         clear_bit(ATM_VF_PARTIAL, &vcc->flags);
839         clear_bit(ATM_VF_ADDR, &vcc->flags);
840
841         up(&instance->serialize);
842
843         atm_dbg(instance, "%s successful\n", __func__);
844 }
845
846 static int usbatm_atm_ioctl(struct atm_dev *atm_dev, unsigned int cmd,
847                           void __user * arg)
848 {
849         switch (cmd) {
850         case ATM_QUERYLOOP:
851                 return put_user(ATM_LM_NONE, (int __user *)arg) ? -EFAULT : 0;
852         default:
853                 return -ENOIOCTLCMD;
854         }
855 }
856
857 static int usbatm_atm_init(struct usbatm_data *instance)
858 {
859         struct atm_dev *atm_dev;
860         int ret, i;
861
862         /* ATM init.  The ATM initialization scheme suffers from an intrinsic race
863          * condition: callbacks we register can be executed at once, before we have
864          * initialized the struct atm_dev.  To protect against this, all callbacks
865          * abort if atm_dev->dev_data is NULL. */
866         atm_dev = atm_dev_register(instance->driver_name, &usbatm_atm_devops, -1, NULL);
867         if (!atm_dev) {
868                 usb_err(instance, "%s: failed to register ATM device!\n", __func__);
869                 return -1;
870         }
871
872         instance->atm_dev = atm_dev;
873
874         atm_dev->ci_range.vpi_bits = ATM_CI_MAX;
875         atm_dev->ci_range.vci_bits = ATM_CI_MAX;
876         atm_dev->signal = ATM_PHY_SIG_UNKNOWN;
877
878         /* temp init ATM device, set to 128kbit */
879         atm_dev->link_rate = 128 * 1000 / 424;
880
881         if (instance->driver->atm_start && ((ret = instance->driver->atm_start(instance, atm_dev)) < 0)) {
882                 atm_err(instance, "%s: atm_start failed: %d!\n", __func__, ret);
883                 goto fail;
884         }
885
886         usbatm_get_instance(instance);  /* dropped in usbatm_atm_dev_close */
887
888         /* ready for ATM callbacks */
889         mb();
890         atm_dev->dev_data = instance;
891
892         /* submit all rx URBs */
893         for (i = 0; i < num_rcv_urbs; i++)
894                 usbatm_submit_urb(instance->urbs[i]);
895
896         return 0;
897
898  fail:
899         instance->atm_dev = NULL;
900         atm_dev_deregister(atm_dev); /* usbatm_atm_dev_close will eventually be called */
901         return ret;
902 }
903
904
905 /**********
906 **  USB  **
907 **********/
908
909 static int usbatm_do_heavy_init(void *arg)
910 {
911         struct usbatm_data *instance = arg;
912         int ret;
913
914         daemonize(instance->driver->driver_name);
915         allow_signal(SIGTERM);
916
917         complete(&instance->thread_started);
918
919         ret = instance->driver->heavy_init(instance, instance->usb_intf);
920
921         if (!ret)
922                 ret = usbatm_atm_init(instance);
923
924         down(&instance->serialize);
925         instance->thread_pid = -1;
926         up(&instance->serialize);
927
928         complete_and_exit(&instance->thread_exited, ret);
929 }
930
931 static int usbatm_heavy_init(struct usbatm_data *instance)
932 {
933         int ret = kernel_thread(usbatm_do_heavy_init, instance, CLONE_KERNEL);
934
935         if (ret < 0) {
936                 usb_err(instance, "%s: failed to create kernel_thread (%d)!\n", __func__, ret);
937                 return ret;
938         }
939
940         down(&instance->serialize);
941         instance->thread_pid = ret;
942         up(&instance->serialize);
943
944         wait_for_completion(&instance->thread_started);
945
946         return 0;
947 }
948
949 static void usbatm_tasklet_schedule(unsigned long data)
950 {
951         tasklet_schedule((struct tasklet_struct *) data);
952 }
953
954 static inline void usbatm_init_channel(struct usbatm_channel *channel)
955 {
956         spin_lock_init(&channel->lock);
957         INIT_LIST_HEAD(&channel->list);
958         channel->delay.function = usbatm_tasklet_schedule;
959         channel->delay.data = (unsigned long) &channel->tasklet;
960         init_timer(&channel->delay);
961 }
962
963 int usbatm_usb_probe(struct usb_interface *intf, const struct usb_device_id *id,
964                      struct usbatm_driver *driver)
965 {
966         struct device *dev = &intf->dev;
967         struct usb_device *usb_dev = interface_to_usbdev(intf);
968         struct usbatm_data *instance;
969         char *buf;
970         int error = -ENOMEM;
971         int i, length;
972         int need_heavy;
973
974         dev_dbg(dev, "%s: trying driver %s with vendor=%04x, product=%04x, ifnum %2d\n",
975                         __func__, driver->driver_name,
976                         le16_to_cpu(usb_dev->descriptor.idVendor),
977                         le16_to_cpu(usb_dev->descriptor.idProduct),
978                         intf->altsetting->desc.bInterfaceNumber);
979
980         /* instance init */
981         instance = kzalloc(sizeof(*instance) + sizeof(struct urb *) * (num_rcv_urbs + num_snd_urbs), GFP_KERNEL);
982         if (!instance) {
983                 dev_err(dev, "%s: no memory for instance data!\n", __func__);
984                 return -ENOMEM;
985         }
986
987         /* public fields */
988
989         instance->driver = driver;
990         snprintf(instance->driver_name, sizeof(instance->driver_name), driver->driver_name);
991
992         instance->usb_dev = usb_dev;
993         instance->usb_intf = intf;
994
995         buf = instance->description;
996         length = sizeof(instance->description);
997
998         if ((i = usb_string(usb_dev, usb_dev->descriptor.iProduct, buf, length)) < 0)
999                 goto bind;
1000
1001         buf += i;
1002         length -= i;
1003
1004         i = scnprintf(buf, length, " (");
1005         buf += i;
1006         length -= i;
1007
1008         if (length <= 0 || (i = usb_make_path(usb_dev, buf, length)) < 0)
1009                 goto bind;
1010
1011         buf += i;
1012         length -= i;
1013
1014         snprintf(buf, length, ")");
1015
1016  bind:
1017         need_heavy = 1;
1018         if (driver->bind && (error = driver->bind(instance, intf, id, &need_heavy)) < 0) {
1019                         dev_err(dev, "%s: bind failed: %d!\n", __func__, error);
1020                         goto fail_free;
1021         }
1022
1023         /* private fields */
1024
1025         kref_init(&instance->refcount);         /* dropped in usbatm_usb_disconnect */
1026         init_MUTEX(&instance->serialize);
1027
1028         instance->thread_pid = -1;
1029         init_completion(&instance->thread_started);
1030         init_completion(&instance->thread_exited);
1031
1032         INIT_LIST_HEAD(&instance->vcc_list);
1033
1034         usbatm_init_channel(&instance->rx_channel);
1035         usbatm_init_channel(&instance->tx_channel);
1036         tasklet_init(&instance->rx_channel.tasklet, usbatm_rx_process, (unsigned long)instance);
1037         tasklet_init(&instance->tx_channel.tasklet, usbatm_tx_process, (unsigned long)instance);
1038         instance->rx_channel.endpoint = usb_rcvbulkpipe(usb_dev, driver->in);
1039         instance->tx_channel.endpoint = usb_sndbulkpipe(usb_dev, driver->out);
1040         instance->rx_channel.stride = ATM_CELL_SIZE + driver->rx_padding;
1041         instance->tx_channel.stride = ATM_CELL_SIZE + driver->tx_padding;
1042         instance->rx_channel.buf_size = rcv_buf_size * instance->rx_channel.stride;
1043         instance->tx_channel.buf_size = snd_buf_size * instance->tx_channel.stride;
1044         instance->rx_channel.usbatm = instance->tx_channel.usbatm = instance;
1045
1046         skb_queue_head_init(&instance->sndqueue);
1047
1048         for (i = 0; i < num_rcv_urbs + num_snd_urbs; i++) {
1049                 struct urb *urb;
1050                 u8 *buffer;
1051                 unsigned int iso_packets = 0, iso_size = 0;
1052                 struct usbatm_channel *channel = i < num_rcv_urbs ?
1053                         &instance->rx_channel : &instance->tx_channel;
1054
1055                 if (usb_pipeisoc(channel->endpoint)) {
1056                         /* don't expect iso out endpoints */
1057                         iso_size = usb_maxpacket(instance->usb_dev, channel->endpoint, 0);
1058                         iso_size -= iso_size % channel->stride; /* alignment */
1059                         BUG_ON(!iso_size);
1060                         iso_packets = (channel->buf_size - 1) / iso_size + 1;
1061                 }
1062
1063                 urb = usb_alloc_urb(iso_packets, GFP_KERNEL);
1064                 if (!urb) {
1065                         dev_err(dev, "%s: no memory for urb %d!\n", __func__, i);
1066                         goto fail_unbind;
1067                 }
1068
1069                 instance->urbs[i] = urb;
1070
1071                 buffer = kmalloc(channel->buf_size, GFP_KERNEL);
1072                 if (!buffer) {
1073                         dev_err(dev, "%s: no memory for buffer %d!\n", __func__, i);
1074                         goto fail_unbind;
1075                 }
1076                 /* zero the tx padding to avoid leaking information */
1077                 memset(buffer, 0, channel->buf_size);
1078
1079                 usb_fill_bulk_urb(urb, instance->usb_dev, channel->endpoint,
1080                                   buffer, channel->buf_size, usbatm_complete, channel);
1081                 if (iso_packets) {
1082                         int j;
1083                         urb->interval = 1;
1084                         urb->transfer_flags = URB_ISO_ASAP;
1085                         urb->number_of_packets = iso_packets;
1086                         for (j = 0; j < iso_packets; j++) {
1087                                 urb->iso_frame_desc[j].offset = iso_size * j;
1088                                 urb->iso_frame_desc[j].length = min_t(int, iso_size,
1089                                                                       channel->buf_size - urb->iso_frame_desc[j].offset);
1090                         }
1091                 }
1092
1093                 /* put all tx URBs on the list of spares */
1094                 if (i >= num_rcv_urbs)
1095                         list_add_tail(&urb->urb_list, &channel->list);
1096
1097                 vdbg("%s: alloced buffer 0x%p buf size %u urb 0x%p",
1098                      __func__, urb->transfer_buffer, urb->transfer_buffer_length, urb);
1099         }
1100
1101         if (need_heavy && driver->heavy_init) {
1102                 error = usbatm_heavy_init(instance);
1103         } else {
1104                 complete(&instance->thread_exited);     /* pretend that heavy_init was run */
1105                 error = usbatm_atm_init(instance);
1106         }
1107
1108         if (error < 0)
1109                 goto fail_unbind;
1110
1111         usb_get_dev(usb_dev);
1112         usb_set_intfdata(intf, instance);
1113
1114         return 0;
1115
1116  fail_unbind:
1117         if (instance->driver->unbind)
1118                 instance->driver->unbind(instance, intf);
1119  fail_free:
1120         for (i = 0; i < num_rcv_urbs + num_snd_urbs; i++) {
1121                 if (instance->urbs[i])
1122                         kfree(instance->urbs[i]->transfer_buffer);
1123                 usb_free_urb(instance->urbs[i]);
1124         }
1125
1126         kfree (instance);
1127
1128         return error;
1129 }
1130 EXPORT_SYMBOL_GPL(usbatm_usb_probe);
1131
1132 void usbatm_usb_disconnect(struct usb_interface *intf)
1133 {
1134         struct device *dev = &intf->dev;
1135         struct usbatm_data *instance = usb_get_intfdata(intf);
1136         int i;
1137
1138         dev_dbg(dev, "%s entered\n", __func__);
1139
1140         if (!instance) {
1141                 dev_dbg(dev, "%s: NULL instance!\n", __func__);
1142                 return;
1143         }
1144
1145         usb_set_intfdata(intf, NULL);
1146
1147         down(&instance->serialize);
1148         if (instance->thread_pid >= 0)
1149                 kill_proc(instance->thread_pid, SIGTERM, 1);
1150         up(&instance->serialize);
1151
1152         wait_for_completion(&instance->thread_exited);
1153
1154         tasklet_disable(&instance->rx_channel.tasklet);
1155         tasklet_disable(&instance->tx_channel.tasklet);
1156
1157         for (i = 0; i < num_rcv_urbs + num_snd_urbs; i++)
1158                 usb_kill_urb(instance->urbs[i]);
1159
1160         del_timer_sync(&instance->rx_channel.delay);
1161         del_timer_sync(&instance->tx_channel.delay);
1162
1163         if (instance->atm_dev && instance->driver->atm_stop)
1164                 instance->driver->atm_stop(instance, instance->atm_dev);
1165
1166         if (instance->driver->unbind)
1167                 instance->driver->unbind(instance, intf);
1168
1169         instance->driver_data = NULL;
1170
1171         /* turn usbatm_[rt]x_process into noop */
1172         /* no need to take the spinlock */
1173         INIT_LIST_HEAD(&instance->rx_channel.list);
1174         INIT_LIST_HEAD(&instance->tx_channel.list);
1175
1176         tasklet_enable(&instance->rx_channel.tasklet);
1177         tasklet_enable(&instance->tx_channel.tasklet);
1178
1179         for (i = 0; i < num_rcv_urbs + num_snd_urbs; i++) {
1180                 kfree(instance->urbs[i]->transfer_buffer);
1181                 usb_free_urb(instance->urbs[i]);
1182         }
1183
1184         /* ATM finalize */
1185         if (instance->atm_dev)
1186                 atm_dev_deregister(instance->atm_dev);
1187
1188         usbatm_put_instance(instance);  /* taken in usbatm_usb_probe */
1189 }
1190 EXPORT_SYMBOL_GPL(usbatm_usb_disconnect);
1191
1192
1193 /***********
1194 **  init  **
1195 ***********/
1196
1197 static int __init usbatm_usb_init(void)
1198 {
1199         dbg("%s: driver version %s", __func__, DRIVER_VERSION);
1200
1201         if (sizeof(struct usbatm_control) > sizeof(((struct sk_buff *) 0)->cb)) {
1202                 printk(KERN_ERR "%s unusable with this kernel!\n", usbatm_driver_name);
1203                 return -EIO;
1204         }
1205
1206         if ((num_rcv_urbs > UDSL_MAX_RCV_URBS)
1207             || (num_snd_urbs > UDSL_MAX_SND_URBS)
1208             || (rcv_buf_size < 1)
1209             || (rcv_buf_size > UDSL_MAX_RCV_BUF_SIZE)
1210             || (snd_buf_size < 1)
1211             || (snd_buf_size > UDSL_MAX_SND_BUF_SIZE))
1212                 return -EINVAL;
1213
1214         return 0;
1215 }
1216 module_init(usbatm_usb_init);
1217
1218 static void __exit usbatm_usb_exit(void)
1219 {
1220         dbg("%s", __func__);
1221 }
1222 module_exit(usbatm_usb_exit);
1223
1224 MODULE_AUTHOR(DRIVER_AUTHOR);
1225 MODULE_DESCRIPTION(DRIVER_DESC);
1226 MODULE_LICENSE("GPL");
1227 MODULE_VERSION(DRIVER_VERSION);
1228
1229 /************
1230 **  debug  **
1231 ************/
1232
1233 #ifdef VERBOSE_DEBUG
1234 static int usbatm_print_packet(const unsigned char *data, int len)
1235 {
1236         unsigned char buffer[256];
1237         int i = 0, j = 0;
1238
1239         for (i = 0; i < len;) {
1240                 buffer[0] = '\0';
1241                 sprintf(buffer, "%.3d :", i);
1242                 for (j = 0; (j < 16) && (i < len); j++, i++) {
1243                         sprintf(buffer, "%s %2.2x", buffer, data[i]);
1244                 }
1245                 dbg("%s", buffer);
1246         }
1247         return i;
1248 }
1249 #endif