0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051 #include "usbatm.h"
0052
0053 #include <linux/uaccess.h>
0054 #include <linux/crc32.h>
0055 #include <linux/errno.h>
0056 #include <linux/init.h>
0057 #include <linux/interrupt.h>
0058 #include <linux/kernel.h>
0059 #include <linux/module.h>
0060 #include <linux/moduleparam.h>
0061 #include <linux/netdevice.h>
0062 #include <linux/proc_fs.h>
0063 #include <linux/sched/signal.h>
0064 #include <linux/signal.h>
0065 #include <linux/slab.h>
0066 #include <linux/stat.h>
0067 #include <linux/timer.h>
0068 #include <linux/wait.h>
0069 #include <linux/kthread.h>
0070 #include <linux/ratelimit.h>
0071
0072 #ifdef VERBOSE_DEBUG
0073 static int usbatm_print_packet(struct usbatm_data *instance, const unsigned char *data, int len);
0074 #define PACKETDEBUG(arg...) usbatm_print_packet(arg)
0075 #define vdbg(arg...) dev_dbg(arg)
0076 #else
0077 #define PACKETDEBUG(arg...)
0078 #define vdbg(arg...)
0079 #endif
0080
0081 #define DRIVER_AUTHOR "Johan Verrept, Duncan Sands <duncan.sands@free.fr>"
0082 #define DRIVER_DESC "Generic USB ATM/DSL I/O"
0083
0084 static const char usbatm_driver_name[] = "usbatm";
0085
0086 #define UDSL_MAX_RCV_URBS 16
0087 #define UDSL_MAX_SND_URBS 16
0088 #define UDSL_MAX_BUF_SIZE 65536
0089 #define UDSL_DEFAULT_RCV_URBS 4
0090 #define UDSL_DEFAULT_SND_URBS 4
0091 #define UDSL_DEFAULT_RCV_BUF_SIZE 3392
0092 #define UDSL_DEFAULT_SND_BUF_SIZE 3392
0093
0094 #define ATM_CELL_HEADER (ATM_CELL_SIZE - ATM_CELL_PAYLOAD)
0095
0096 #define THROTTLE_MSECS 100
0097
0098 static unsigned int num_rcv_urbs = UDSL_DEFAULT_RCV_URBS;
0099 static unsigned int num_snd_urbs = UDSL_DEFAULT_SND_URBS;
0100 static unsigned int rcv_buf_bytes = UDSL_DEFAULT_RCV_BUF_SIZE;
0101 static unsigned int snd_buf_bytes = UDSL_DEFAULT_SND_BUF_SIZE;
0102
0103 module_param(num_rcv_urbs, uint, S_IRUGO);
0104 MODULE_PARM_DESC(num_rcv_urbs,
0105 "Number of urbs used for reception (range: 0-"
0106 __MODULE_STRING(UDSL_MAX_RCV_URBS) ", default: "
0107 __MODULE_STRING(UDSL_DEFAULT_RCV_URBS) ")");
0108
0109 module_param(num_snd_urbs, uint, S_IRUGO);
0110 MODULE_PARM_DESC(num_snd_urbs,
0111 "Number of urbs used for transmission (range: 0-"
0112 __MODULE_STRING(UDSL_MAX_SND_URBS) ", default: "
0113 __MODULE_STRING(UDSL_DEFAULT_SND_URBS) ")");
0114
0115 module_param(rcv_buf_bytes, uint, S_IRUGO);
0116 MODULE_PARM_DESC(rcv_buf_bytes,
0117 "Size of the buffers used for reception, in bytes (range: 1-"
0118 __MODULE_STRING(UDSL_MAX_BUF_SIZE) ", default: "
0119 __MODULE_STRING(UDSL_DEFAULT_RCV_BUF_SIZE) ")");
0120
0121 module_param(snd_buf_bytes, uint, S_IRUGO);
0122 MODULE_PARM_DESC(snd_buf_bytes,
0123 "Size of the buffers used for transmission, in bytes (range: 1-"
0124 __MODULE_STRING(UDSL_MAX_BUF_SIZE) ", default: "
0125 __MODULE_STRING(UDSL_DEFAULT_SND_BUF_SIZE) ")");
0126
0127
0128
0129
0130 struct usbatm_vcc_data {
0131
0132 struct list_head list;
0133 short vpi;
0134 int vci;
0135 struct atm_vcc *vcc;
0136
0137
0138 struct sk_buff *sarb;
0139 };
0140
0141
0142
0143
0144 struct usbatm_control {
0145 struct atm_skb_data atm;
0146 u32 len;
0147 u32 crc;
0148 };
0149
0150 #define UDSL_SKB(x) ((struct usbatm_control *)(x)->cb)
0151
0152
0153
0154
0155 static void usbatm_atm_dev_close(struct atm_dev *atm_dev);
0156 static int usbatm_atm_open(struct atm_vcc *vcc);
0157 static void usbatm_atm_close(struct atm_vcc *vcc);
0158 static int usbatm_atm_ioctl(struct atm_dev *atm_dev, unsigned int cmd, void __user *arg);
0159 static int usbatm_atm_send(struct atm_vcc *vcc, struct sk_buff *skb);
0160 static int usbatm_atm_proc_read(struct atm_dev *atm_dev, loff_t *pos, char *page);
0161
0162 static const struct atmdev_ops usbatm_atm_devops = {
0163 .dev_close = usbatm_atm_dev_close,
0164 .open = usbatm_atm_open,
0165 .close = usbatm_atm_close,
0166 .ioctl = usbatm_atm_ioctl,
0167 .send = usbatm_atm_send,
0168 .proc_read = usbatm_atm_proc_read,
0169 .owner = THIS_MODULE,
0170 };
0171
0172
0173
0174
0175
0176
0177 static inline unsigned int usbatm_pdu_length(unsigned int length)
0178 {
0179 length += ATM_CELL_PAYLOAD - 1 + ATM_AAL5_TRAILER;
0180 return length - length % ATM_CELL_PAYLOAD;
0181 }
0182
0183 static inline void usbatm_pop(struct atm_vcc *vcc, struct sk_buff *skb)
0184 {
0185 if (vcc->pop)
0186 vcc->pop(vcc, skb);
0187 else
0188 dev_kfree_skb_any(skb);
0189 }
0190
0191
0192
0193
0194
0195
0196 static struct urb *usbatm_pop_urb(struct usbatm_channel *channel)
0197 {
0198 struct urb *urb;
0199
0200 spin_lock_irq(&channel->lock);
0201 if (list_empty(&channel->list)) {
0202 spin_unlock_irq(&channel->lock);
0203 return NULL;
0204 }
0205
0206 urb = list_entry(channel->list.next, struct urb, urb_list);
0207 list_del(&urb->urb_list);
0208 spin_unlock_irq(&channel->lock);
0209
0210 return urb;
0211 }
0212
0213 static int usbatm_submit_urb(struct urb *urb)
0214 {
0215 struct usbatm_channel *channel = urb->context;
0216 int ret;
0217
0218
0219
0220
0221 ret = usb_submit_urb(urb, GFP_ATOMIC);
0222 if (ret) {
0223 if (printk_ratelimit())
0224 atm_warn(channel->usbatm, "%s: urb 0x%p submission failed (%d)!\n",
0225 __func__, urb, ret);
0226
0227
0228 urb->status = -EAGAIN;
0229 spin_lock_irq(&channel->lock);
0230
0231
0232 list_add(&urb->urb_list, &channel->list);
0233
0234 spin_unlock_irq(&channel->lock);
0235
0236
0237 mod_timer(&channel->delay, jiffies + msecs_to_jiffies(THROTTLE_MSECS));
0238 }
0239
0240 return ret;
0241 }
0242
0243 static void usbatm_complete(struct urb *urb)
0244 {
0245 struct usbatm_channel *channel = urb->context;
0246 unsigned long flags;
0247 int status = urb->status;
0248
0249
0250
0251
0252
0253 spin_lock_irqsave(&channel->lock, flags);
0254
0255
0256 list_add_tail(&urb->urb_list, &channel->list);
0257
0258 spin_unlock_irqrestore(&channel->lock, flags);
0259
0260 if (unlikely(status) &&
0261 (!(channel->usbatm->flags & UDSL_IGNORE_EILSEQ) ||
0262 status != -EILSEQ)) {
0263 if (status == -ESHUTDOWN)
0264 return;
0265
0266 if (printk_ratelimit())
0267 atm_warn(channel->usbatm, "%s: urb 0x%p failed (%d)!\n",
0268 __func__, urb, status);
0269
0270 mod_timer(&channel->delay, jiffies + msecs_to_jiffies(THROTTLE_MSECS));
0271 } else
0272 tasklet_schedule(&channel->tasklet);
0273 }
0274
0275
0276
0277
0278
0279
0280 static inline struct usbatm_vcc_data *usbatm_find_vcc(struct usbatm_data *instance,
0281 short vpi, int vci)
0282 {
0283 struct usbatm_vcc_data *vcc_data;
0284
0285 list_for_each_entry(vcc_data, &instance->vcc_list, list)
0286 if ((vcc_data->vci == vci) && (vcc_data->vpi == vpi))
0287 return vcc_data;
0288 return NULL;
0289 }
0290
0291 static void usbatm_extract_one_cell(struct usbatm_data *instance, unsigned char *source)
0292 {
0293 struct atm_vcc *vcc;
0294 struct sk_buff *sarb;
0295 short vpi = ((source[0] & 0x0f) << 4) | (source[1] >> 4);
0296 int vci = ((source[1] & 0x0f) << 12) | (source[2] << 4) | (source[3] >> 4);
0297 u8 pti = ((source[3] & 0xe) >> 1);
0298
0299 if ((vci != instance->cached_vci) || (vpi != instance->cached_vpi)) {
0300 instance->cached_vpi = vpi;
0301 instance->cached_vci = vci;
0302
0303 instance->cached_vcc = usbatm_find_vcc(instance, vpi, vci);
0304
0305 if (!instance->cached_vcc)
0306 atm_rldbg(instance, "%s: unknown vpi/vci (%hd/%d)!\n", __func__, vpi, vci);
0307 }
0308
0309 if (!instance->cached_vcc)
0310 return;
0311
0312 vcc = instance->cached_vcc->vcc;
0313
0314
0315 if (pti == ATM_PTI_E2EF5) {
0316 if (printk_ratelimit())
0317 atm_warn(instance, "%s: OAM not supported (vpi %d, vci %d)!\n",
0318 __func__, vpi, vci);
0319 atomic_inc(&vcc->stats->rx_err);
0320 return;
0321 }
0322
0323 sarb = instance->cached_vcc->sarb;
0324
0325 if (sarb->tail + ATM_CELL_PAYLOAD > sarb->end) {
0326 atm_rldbg(instance, "%s: buffer overrun (sarb->len %u, vcc: 0x%p)!\n",
0327 __func__, sarb->len, vcc);
0328
0329 skb_trim(sarb, 0);
0330 }
0331
0332 memcpy(skb_tail_pointer(sarb), source + ATM_CELL_HEADER, ATM_CELL_PAYLOAD);
0333 __skb_put(sarb, ATM_CELL_PAYLOAD);
0334
0335 if (pti & 1) {
0336 struct sk_buff *skb;
0337 unsigned int length;
0338 unsigned int pdu_length;
0339
0340 length = (source[ATM_CELL_SIZE - 6] << 8) + source[ATM_CELL_SIZE - 5];
0341
0342
0343 if (length > ATM_MAX_AAL5_PDU) {
0344 atm_rldbg(instance, "%s: bogus length %u (vcc: 0x%p)!\n",
0345 __func__, length, vcc);
0346 atomic_inc(&vcc->stats->rx_err);
0347 goto out;
0348 }
0349
0350 pdu_length = usbatm_pdu_length(length);
0351
0352 if (sarb->len < pdu_length) {
0353 atm_rldbg(instance, "%s: bogus pdu_length %u (sarb->len: %u, vcc: 0x%p)!\n",
0354 __func__, pdu_length, sarb->len, vcc);
0355 atomic_inc(&vcc->stats->rx_err);
0356 goto out;
0357 }
0358
0359 if (crc32_be(~0, skb_tail_pointer(sarb) - pdu_length, pdu_length) != 0xc704dd7b) {
0360 atm_rldbg(instance, "%s: packet failed crc check (vcc: 0x%p)!\n",
0361 __func__, vcc);
0362 atomic_inc(&vcc->stats->rx_err);
0363 goto out;
0364 }
0365
0366 vdbg(&instance->usb_intf->dev,
0367 "%s: got packet (length: %u, pdu_length: %u, vcc: 0x%p)",
0368 __func__, length, pdu_length, vcc);
0369
0370 skb = dev_alloc_skb(length);
0371 if (!skb) {
0372 if (printk_ratelimit())
0373 atm_err(instance, "%s: no memory for skb (length: %u)!\n",
0374 __func__, length);
0375 atomic_inc(&vcc->stats->rx_drop);
0376 goto out;
0377 }
0378
0379 vdbg(&instance->usb_intf->dev,
0380 "%s: allocated new sk_buff (skb: 0x%p, skb->truesize: %u)",
0381 __func__, skb, skb->truesize);
0382
0383 if (!atm_charge(vcc, skb->truesize)) {
0384 atm_rldbg(instance, "%s: failed atm_charge (skb->truesize: %u)!\n",
0385 __func__, skb->truesize);
0386 dev_kfree_skb_any(skb);
0387 goto out;
0388 }
0389
0390 skb_copy_to_linear_data(skb,
0391 skb_tail_pointer(sarb) - pdu_length,
0392 length);
0393 __skb_put(skb, length);
0394
0395 vdbg(&instance->usb_intf->dev,
0396 "%s: sending skb 0x%p, skb->len %u, skb->truesize %u",
0397 __func__, skb, skb->len, skb->truesize);
0398
0399 PACKETDEBUG(instance, skb->data, skb->len);
0400
0401 vcc->push(vcc, skb);
0402
0403 atomic_inc(&vcc->stats->rx);
0404 out:
0405 skb_trim(sarb, 0);
0406 }
0407 }
0408
0409 static void usbatm_extract_cells(struct usbatm_data *instance,
0410 unsigned char *source, unsigned int avail_data)
0411 {
0412 unsigned int stride = instance->rx_channel.stride;
0413 unsigned int buf_usage = instance->buf_usage;
0414
0415
0416
0417
0418 if (buf_usage > 0) {
0419
0420 unsigned char *cell_buf = instance->cell_buf;
0421 unsigned int space_left = stride - buf_usage;
0422
0423 if (avail_data >= space_left) {
0424
0425 memcpy(cell_buf + buf_usage, source, space_left);
0426 source += space_left;
0427 avail_data -= space_left;
0428 usbatm_extract_one_cell(instance, cell_buf);
0429 instance->buf_usage = 0;
0430 } else {
0431
0432 memcpy(cell_buf + buf_usage, source, avail_data);
0433 instance->buf_usage = buf_usage + avail_data;
0434 return;
0435 }
0436 }
0437
0438 for (; avail_data >= stride; avail_data -= stride, source += stride)
0439 usbatm_extract_one_cell(instance, source);
0440
0441 if (avail_data > 0) {
0442
0443
0444 memcpy(instance->cell_buf, source, avail_data);
0445 instance->buf_usage = avail_data;
0446 }
0447 }
0448
0449
0450
0451
0452
0453
0454 static unsigned int usbatm_write_cells(struct usbatm_data *instance,
0455 struct sk_buff *skb,
0456 u8 *target, unsigned int avail_space)
0457 {
0458 struct usbatm_control *ctrl = UDSL_SKB(skb);
0459 struct atm_vcc *vcc = ctrl->atm.vcc;
0460 unsigned int bytes_written;
0461 unsigned int stride = instance->tx_channel.stride;
0462
0463 for (bytes_written = 0; bytes_written < avail_space && ctrl->len;
0464 bytes_written += stride, target += stride) {
0465 unsigned int data_len = min_t(unsigned int, skb->len, ATM_CELL_PAYLOAD);
0466 unsigned int left = ATM_CELL_PAYLOAD - data_len;
0467 u8 *ptr = target;
0468
0469 ptr[0] = vcc->vpi >> 4;
0470 ptr[1] = (vcc->vpi << 4) | (vcc->vci >> 12);
0471 ptr[2] = vcc->vci >> 4;
0472 ptr[3] = vcc->vci << 4;
0473 ptr[4] = 0xec;
0474 ptr += ATM_CELL_HEADER;
0475
0476 skb_copy_from_linear_data(skb, ptr, data_len);
0477 ptr += data_len;
0478 __skb_pull(skb, data_len);
0479
0480 if (!left)
0481 continue;
0482
0483 memset(ptr, 0, left);
0484
0485 if (left >= ATM_AAL5_TRAILER) {
0486 u8 *trailer = target + ATM_CELL_SIZE - ATM_AAL5_TRAILER;
0487
0488
0489 trailer[2] = ctrl->len >> 8;
0490 trailer[3] = ctrl->len;
0491
0492 ctrl->crc = ~crc32_be(ctrl->crc, ptr, left - 4);
0493
0494 trailer[4] = ctrl->crc >> 24;
0495 trailer[5] = ctrl->crc >> 16;
0496 trailer[6] = ctrl->crc >> 8;
0497 trailer[7] = ctrl->crc;
0498
0499 target[3] |= 0x2;
0500
0501 ctrl->len = 0;
0502 } else
0503 ctrl->crc = crc32_be(ctrl->crc, ptr, left);
0504 }
0505
0506 return bytes_written;
0507 }
0508
0509
0510
0511
0512
0513
0514 static void usbatm_rx_process(struct tasklet_struct *t)
0515 {
0516 struct usbatm_data *instance = from_tasklet(instance, t,
0517 rx_channel.tasklet);
0518 struct urb *urb;
0519
0520 while ((urb = usbatm_pop_urb(&instance->rx_channel))) {
0521 vdbg(&instance->usb_intf->dev,
0522 "%s: processing urb 0x%p", __func__, urb);
0523
0524 if (usb_pipeisoc(urb->pipe)) {
0525 unsigned char *merge_start = NULL;
0526 unsigned int merge_length = 0;
0527 const unsigned int packet_size = instance->rx_channel.packet_size;
0528 int i;
0529
0530 for (i = 0; i < urb->number_of_packets; i++) {
0531 if (!urb->iso_frame_desc[i].status) {
0532 unsigned int actual_length = urb->iso_frame_desc[i].actual_length;
0533
0534 if (!merge_length)
0535 merge_start = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset;
0536 merge_length += actual_length;
0537 if (merge_length && (actual_length < packet_size)) {
0538 usbatm_extract_cells(instance, merge_start, merge_length);
0539 merge_length = 0;
0540 }
0541 } else {
0542 atm_rldbg(instance, "%s: status %d in frame %d!\n", __func__, urb->status, i);
0543 if (merge_length)
0544 usbatm_extract_cells(instance, merge_start, merge_length);
0545 merge_length = 0;
0546 instance->buf_usage = 0;
0547 }
0548 }
0549
0550 if (merge_length)
0551 usbatm_extract_cells(instance, merge_start, merge_length);
0552 } else
0553 if (!urb->status)
0554 usbatm_extract_cells(instance, urb->transfer_buffer, urb->actual_length);
0555 else
0556 instance->buf_usage = 0;
0557
0558 if (usbatm_submit_urb(urb))
0559 return;
0560 }
0561 }
0562
0563
0564
0565
0566
0567
0568 static void usbatm_tx_process(struct tasklet_struct *t)
0569 {
0570 struct usbatm_data *instance = from_tasklet(instance, t,
0571 tx_channel.tasklet);
0572 struct sk_buff *skb = instance->current_skb;
0573 struct urb *urb = NULL;
0574 const unsigned int buf_size = instance->tx_channel.buf_size;
0575 unsigned int bytes_written = 0;
0576 u8 *buffer = NULL;
0577
0578 if (!skb)
0579 skb = skb_dequeue(&instance->sndqueue);
0580
0581 while (skb) {
0582 if (!urb) {
0583 urb = usbatm_pop_urb(&instance->tx_channel);
0584 if (!urb)
0585 break;
0586 buffer = urb->transfer_buffer;
0587 bytes_written = (urb->status == -EAGAIN) ?
0588 urb->transfer_buffer_length : 0;
0589 }
0590
0591 bytes_written += usbatm_write_cells(instance, skb,
0592 buffer + bytes_written,
0593 buf_size - bytes_written);
0594
0595 vdbg(&instance->usb_intf->dev,
0596 "%s: wrote %u bytes from skb 0x%p to urb 0x%p",
0597 __func__, bytes_written, skb, urb);
0598
0599 if (!UDSL_SKB(skb)->len) {
0600 struct atm_vcc *vcc = UDSL_SKB(skb)->atm.vcc;
0601
0602 usbatm_pop(vcc, skb);
0603 atomic_inc(&vcc->stats->tx);
0604
0605 skb = skb_dequeue(&instance->sndqueue);
0606 }
0607
0608 if (bytes_written == buf_size || (!skb && bytes_written)) {
0609 urb->transfer_buffer_length = bytes_written;
0610
0611 if (usbatm_submit_urb(urb))
0612 break;
0613 urb = NULL;
0614 }
0615 }
0616
0617 instance->current_skb = skb;
0618 }
0619
0620 static void usbatm_cancel_send(struct usbatm_data *instance,
0621 struct atm_vcc *vcc)
0622 {
0623 struct sk_buff *skb, *n;
0624
0625 spin_lock_irq(&instance->sndqueue.lock);
0626 skb_queue_walk_safe(&instance->sndqueue, skb, n) {
0627 if (UDSL_SKB(skb)->atm.vcc == vcc) {
0628 atm_dbg(instance, "%s: popping skb 0x%p\n", __func__, skb);
0629 __skb_unlink(skb, &instance->sndqueue);
0630 usbatm_pop(vcc, skb);
0631 }
0632 }
0633 spin_unlock_irq(&instance->sndqueue.lock);
0634
0635 tasklet_disable(&instance->tx_channel.tasklet);
0636 if ((skb = instance->current_skb) && (UDSL_SKB(skb)->atm.vcc == vcc)) {
0637 atm_dbg(instance, "%s: popping current skb (0x%p)\n", __func__, skb);
0638 instance->current_skb = NULL;
0639 usbatm_pop(vcc, skb);
0640 }
0641 tasklet_enable(&instance->tx_channel.tasklet);
0642 }
0643
0644 static int usbatm_atm_send(struct atm_vcc *vcc, struct sk_buff *skb)
0645 {
0646 struct usbatm_data *instance = vcc->dev->dev_data;
0647 struct usbatm_control *ctrl = UDSL_SKB(skb);
0648 int err;
0649
0650
0651 if (!instance || instance->disconnected) {
0652 #ifdef VERBOSE_DEBUG
0653 printk_ratelimited(KERN_DEBUG "%s: %s!\n", __func__, instance ? "disconnected" : "NULL instance");
0654 #endif
0655 err = -ENODEV;
0656 goto fail;
0657 }
0658
0659 if (vcc->qos.aal != ATM_AAL5) {
0660 atm_rldbg(instance, "%s: unsupported ATM type %d!\n", __func__, vcc->qos.aal);
0661 err = -EINVAL;
0662 goto fail;
0663 }
0664
0665 if (skb->len > ATM_MAX_AAL5_PDU) {
0666 atm_rldbg(instance, "%s: packet too long (%d vs %d)!\n",
0667 __func__, skb->len, ATM_MAX_AAL5_PDU);
0668 err = -EINVAL;
0669 goto fail;
0670 }
0671
0672 PACKETDEBUG(instance, skb->data, skb->len);
0673
0674
0675 ctrl->atm.vcc = vcc;
0676 ctrl->len = skb->len;
0677 ctrl->crc = crc32_be(~0, skb->data, skb->len);
0678
0679 skb_queue_tail(&instance->sndqueue, skb);
0680 tasklet_schedule(&instance->tx_channel.tasklet);
0681
0682 return 0;
0683
0684 fail:
0685 usbatm_pop(vcc, skb);
0686 return err;
0687 }
0688
0689
0690
0691
0692
0693
0694 static void usbatm_destroy_instance(struct kref *kref)
0695 {
0696 struct usbatm_data *instance = container_of(kref, struct usbatm_data, refcount);
0697
0698 tasklet_kill(&instance->rx_channel.tasklet);
0699 tasklet_kill(&instance->tx_channel.tasklet);
0700 usb_put_dev(instance->usb_dev);
0701 kfree(instance);
0702 }
0703
0704 static void usbatm_get_instance(struct usbatm_data *instance)
0705 {
0706 kref_get(&instance->refcount);
0707 }
0708
0709 static void usbatm_put_instance(struct usbatm_data *instance)
0710 {
0711 kref_put(&instance->refcount, usbatm_destroy_instance);
0712 }
0713
0714
0715
0716
0717
0718
0719 static void usbatm_atm_dev_close(struct atm_dev *atm_dev)
0720 {
0721 struct usbatm_data *instance = atm_dev->dev_data;
0722
0723 if (!instance)
0724 return;
0725
0726 atm_dev->dev_data = NULL;
0727 usbatm_put_instance(instance);
0728 }
0729
0730 static int usbatm_atm_proc_read(struct atm_dev *atm_dev, loff_t *pos, char *page)
0731 {
0732 struct usbatm_data *instance = atm_dev->dev_data;
0733 int left = *pos;
0734
0735 if (!instance)
0736 return -ENODEV;
0737
0738 if (!left--)
0739 return sprintf(page, "%s\n", instance->description);
0740
0741 if (!left--)
0742 return sprintf(page, "MAC: %pM\n", atm_dev->esi);
0743
0744 if (!left--)
0745 return sprintf(page,
0746 "AAL5: tx %d ( %d err ), rx %d ( %d err, %d drop )\n",
0747 atomic_read(&atm_dev->stats.aal5.tx),
0748 atomic_read(&atm_dev->stats.aal5.tx_err),
0749 atomic_read(&atm_dev->stats.aal5.rx),
0750 atomic_read(&atm_dev->stats.aal5.rx_err),
0751 atomic_read(&atm_dev->stats.aal5.rx_drop));
0752
0753 if (!left--) {
0754 if (instance->disconnected)
0755 return sprintf(page, "Disconnected\n");
0756 else
0757 switch (atm_dev->signal) {
0758 case ATM_PHY_SIG_FOUND:
0759 return sprintf(page, "Line up\n");
0760 case ATM_PHY_SIG_LOST:
0761 return sprintf(page, "Line down\n");
0762 default:
0763 return sprintf(page, "Line state unknown\n");
0764 }
0765 }
0766
0767 return 0;
0768 }
0769
0770 static int usbatm_atm_open(struct atm_vcc *vcc)
0771 {
0772 struct usbatm_data *instance = vcc->dev->dev_data;
0773 struct usbatm_vcc_data *new = NULL;
0774 int ret;
0775 int vci = vcc->vci;
0776 short vpi = vcc->vpi;
0777
0778 if (!instance)
0779 return -ENODEV;
0780
0781
0782 if ((vcc->qos.aal != ATM_AAL5)) {
0783 atm_warn(instance, "%s: unsupported ATM type %d!\n", __func__, vcc->qos.aal);
0784 return -EINVAL;
0785 }
0786
0787
0788 if ((vcc->qos.rxtp.max_sdu < 0) || (vcc->qos.rxtp.max_sdu > ATM_MAX_AAL5_PDU)) {
0789 atm_dbg(instance, "%s: max_sdu %d out of range!\n", __func__, vcc->qos.rxtp.max_sdu);
0790 return -EINVAL;
0791 }
0792
0793 mutex_lock(&instance->serialize);
0794
0795 if (instance->disconnected) {
0796 atm_dbg(instance, "%s: disconnected!\n", __func__);
0797 ret = -ENODEV;
0798 goto fail;
0799 }
0800
0801 if (usbatm_find_vcc(instance, vpi, vci)) {
0802 atm_dbg(instance, "%s: %hd/%d already in use!\n", __func__, vpi, vci);
0803 ret = -EADDRINUSE;
0804 goto fail;
0805 }
0806
0807 new = kzalloc(sizeof(struct usbatm_vcc_data), GFP_KERNEL);
0808 if (!new) {
0809 ret = -ENOMEM;
0810 goto fail;
0811 }
0812
0813 new->vcc = vcc;
0814 new->vpi = vpi;
0815 new->vci = vci;
0816
0817 new->sarb = alloc_skb(usbatm_pdu_length(vcc->qos.rxtp.max_sdu), GFP_KERNEL);
0818 if (!new->sarb) {
0819 atm_err(instance, "%s: no memory for SAR buffer!\n", __func__);
0820 ret = -ENOMEM;
0821 goto fail;
0822 }
0823
0824 vcc->dev_data = new;
0825
0826 tasklet_disable(&instance->rx_channel.tasklet);
0827 instance->cached_vcc = new;
0828 instance->cached_vpi = vpi;
0829 instance->cached_vci = vci;
0830 list_add(&new->list, &instance->vcc_list);
0831 tasklet_enable(&instance->rx_channel.tasklet);
0832
0833 set_bit(ATM_VF_ADDR, &vcc->flags);
0834 set_bit(ATM_VF_PARTIAL, &vcc->flags);
0835 set_bit(ATM_VF_READY, &vcc->flags);
0836
0837 mutex_unlock(&instance->serialize);
0838
0839 atm_dbg(instance, "%s: allocated vcc data 0x%p\n", __func__, new);
0840
0841 return 0;
0842
0843 fail:
0844 kfree(new);
0845 mutex_unlock(&instance->serialize);
0846 return ret;
0847 }
0848
0849 static void usbatm_atm_close(struct atm_vcc *vcc)
0850 {
0851 struct usbatm_data *instance = vcc->dev->dev_data;
0852 struct usbatm_vcc_data *vcc_data = vcc->dev_data;
0853
0854 if (!instance || !vcc_data)
0855 return;
0856
0857 usbatm_cancel_send(instance, vcc);
0858
0859 mutex_lock(&instance->serialize);
0860
0861 tasklet_disable(&instance->rx_channel.tasklet);
0862 if (instance->cached_vcc == vcc_data) {
0863 instance->cached_vcc = NULL;
0864 instance->cached_vpi = ATM_VPI_UNSPEC;
0865 instance->cached_vci = ATM_VCI_UNSPEC;
0866 }
0867 list_del(&vcc_data->list);
0868 tasklet_enable(&instance->rx_channel.tasklet);
0869
0870 kfree_skb(vcc_data->sarb);
0871 vcc_data->sarb = NULL;
0872
0873 kfree(vcc_data);
0874 vcc->dev_data = NULL;
0875
0876 vcc->vpi = ATM_VPI_UNSPEC;
0877 vcc->vci = ATM_VCI_UNSPEC;
0878 clear_bit(ATM_VF_READY, &vcc->flags);
0879 clear_bit(ATM_VF_PARTIAL, &vcc->flags);
0880 clear_bit(ATM_VF_ADDR, &vcc->flags);
0881
0882 mutex_unlock(&instance->serialize);
0883 }
0884
0885 static int usbatm_atm_ioctl(struct atm_dev *atm_dev, unsigned int cmd,
0886 void __user *arg)
0887 {
0888 struct usbatm_data *instance = atm_dev->dev_data;
0889
0890 if (!instance || instance->disconnected)
0891 return -ENODEV;
0892
0893 switch (cmd) {
0894 case ATM_QUERYLOOP:
0895 return put_user(ATM_LM_NONE, (int __user *)arg) ? -EFAULT : 0;
0896 default:
0897 return -ENOIOCTLCMD;
0898 }
0899 }
0900
0901 static int usbatm_atm_init(struct usbatm_data *instance)
0902 {
0903 struct atm_dev *atm_dev;
0904 int ret, i;
0905
0906
0907
0908
0909
0910 atm_dev = atm_dev_register(instance->driver_name,
0911 &instance->usb_intf->dev, &usbatm_atm_devops,
0912 -1, NULL);
0913 if (!atm_dev) {
0914 usb_err(instance, "%s: failed to register ATM device!\n", __func__);
0915 return -1;
0916 }
0917
0918 instance->atm_dev = atm_dev;
0919
0920 atm_dev->ci_range.vpi_bits = ATM_CI_MAX;
0921 atm_dev->ci_range.vci_bits = ATM_CI_MAX;
0922 atm_dev->signal = ATM_PHY_SIG_UNKNOWN;
0923
0924
0925 atm_dev->link_rate = 128 * 1000 / 424;
0926
0927 if (instance->driver->atm_start && ((ret = instance->driver->atm_start(instance, atm_dev)) < 0)) {
0928 atm_err(instance, "%s: atm_start failed: %d!\n", __func__, ret);
0929 goto fail;
0930 }
0931
0932 usbatm_get_instance(instance);
0933
0934
0935 mb();
0936 atm_dev->dev_data = instance;
0937
0938
0939 for (i = 0; i < num_rcv_urbs; i++)
0940 usbatm_submit_urb(instance->urbs[i]);
0941
0942 return 0;
0943
0944 fail:
0945 instance->atm_dev = NULL;
0946 atm_dev_deregister(atm_dev);
0947 return ret;
0948 }
0949
0950
0951
0952
0953
0954
0955 static int usbatm_do_heavy_init(void *arg)
0956 {
0957 struct usbatm_data *instance = arg;
0958 int ret;
0959
0960 allow_signal(SIGTERM);
0961 complete(&instance->thread_started);
0962
0963 ret = instance->driver->heavy_init(instance, instance->usb_intf);
0964
0965 if (!ret)
0966 ret = usbatm_atm_init(instance);
0967
0968 mutex_lock(&instance->serialize);
0969 instance->thread = NULL;
0970 mutex_unlock(&instance->serialize);
0971
0972 kthread_complete_and_exit(&instance->thread_exited, ret);
0973 }
0974
0975 static int usbatm_heavy_init(struct usbatm_data *instance)
0976 {
0977 struct task_struct *t;
0978
0979 t = kthread_create(usbatm_do_heavy_init, instance, "%s",
0980 instance->driver->driver_name);
0981 if (IS_ERR(t)) {
0982 usb_err(instance, "%s: failed to create kernel_thread (%ld)!\n",
0983 __func__, PTR_ERR(t));
0984 return PTR_ERR(t);
0985 }
0986
0987 instance->thread = t;
0988 wake_up_process(t);
0989 wait_for_completion(&instance->thread_started);
0990
0991 return 0;
0992 }
0993
0994 static void usbatm_tasklet_schedule(struct timer_list *t)
0995 {
0996 struct usbatm_channel *channel = from_timer(channel, t, delay);
0997
0998 tasklet_schedule(&channel->tasklet);
0999 }
1000
1001 static void usbatm_init_channel(struct usbatm_channel *channel)
1002 {
1003 spin_lock_init(&channel->lock);
1004 INIT_LIST_HEAD(&channel->list);
1005 timer_setup(&channel->delay, usbatm_tasklet_schedule, 0);
1006 }
1007
1008 int usbatm_usb_probe(struct usb_interface *intf, const struct usb_device_id *id,
1009 struct usbatm_driver *driver)
1010 {
1011 struct device *dev = &intf->dev;
1012 struct usb_device *usb_dev = interface_to_usbdev(intf);
1013 struct usbatm_data *instance;
1014 char *buf;
1015 int error = -ENOMEM;
1016 int i, length;
1017 unsigned int maxpacket, num_packets;
1018 size_t size;
1019
1020
1021 size = struct_size(instance, urbs, num_rcv_urbs + num_snd_urbs);
1022 instance = kzalloc(size, GFP_KERNEL);
1023 if (!instance)
1024 return -ENOMEM;
1025
1026
1027
1028 instance->driver = driver;
1029 strlcpy(instance->driver_name, driver->driver_name,
1030 sizeof(instance->driver_name));
1031
1032 instance->usb_dev = usb_dev;
1033 instance->usb_intf = intf;
1034
1035 buf = instance->description;
1036 length = sizeof(instance->description);
1037
1038 if ((i = usb_string(usb_dev, usb_dev->descriptor.iProduct, buf, length)) < 0)
1039 goto bind;
1040
1041 buf += i;
1042 length -= i;
1043
1044 i = scnprintf(buf, length, " (");
1045 buf += i;
1046 length -= i;
1047
1048 if (length <= 0 || (i = usb_make_path(usb_dev, buf, length)) < 0)
1049 goto bind;
1050
1051 buf += i;
1052 length -= i;
1053
1054 snprintf(buf, length, ")");
1055
1056 bind:
1057 if (driver->bind && (error = driver->bind(instance, intf, id)) < 0) {
1058 dev_err(dev, "%s: bind failed: %d!\n", __func__, error);
1059 goto fail_free;
1060 }
1061
1062
1063
1064 kref_init(&instance->refcount);
1065 mutex_init(&instance->serialize);
1066
1067 instance->thread = NULL;
1068 init_completion(&instance->thread_started);
1069 init_completion(&instance->thread_exited);
1070
1071 INIT_LIST_HEAD(&instance->vcc_list);
1072 skb_queue_head_init(&instance->sndqueue);
1073
1074 usbatm_init_channel(&instance->rx_channel);
1075 usbatm_init_channel(&instance->tx_channel);
1076 tasklet_setup(&instance->rx_channel.tasklet, usbatm_rx_process);
1077 tasklet_setup(&instance->tx_channel.tasklet, usbatm_tx_process);
1078 instance->rx_channel.stride = ATM_CELL_SIZE + driver->rx_padding;
1079 instance->tx_channel.stride = ATM_CELL_SIZE + driver->tx_padding;
1080 instance->rx_channel.usbatm = instance->tx_channel.usbatm = instance;
1081
1082 if ((instance->flags & UDSL_USE_ISOC) && driver->isoc_in)
1083 instance->rx_channel.endpoint = usb_rcvisocpipe(usb_dev, driver->isoc_in);
1084 else
1085 instance->rx_channel.endpoint = usb_rcvbulkpipe(usb_dev, driver->bulk_in);
1086
1087 instance->tx_channel.endpoint = usb_sndbulkpipe(usb_dev, driver->bulk_out);
1088
1089
1090 instance->tx_channel.buf_size = max(instance->tx_channel.stride,
1091 snd_buf_bytes - (snd_buf_bytes % instance->tx_channel.stride));
1092
1093
1094 maxpacket = usb_maxpacket(usb_dev, instance->rx_channel.endpoint);
1095
1096 if ((maxpacket < 1) || (maxpacket > UDSL_MAX_BUF_SIZE)) {
1097 dev_err(dev, "%s: invalid endpoint %02x!\n", __func__,
1098 usb_pipeendpoint(instance->rx_channel.endpoint));
1099 error = -EINVAL;
1100 goto fail_unbind;
1101 }
1102
1103 num_packets = max(1U, (rcv_buf_bytes + maxpacket / 2) / maxpacket);
1104
1105 if (num_packets * maxpacket > UDSL_MAX_BUF_SIZE)
1106 num_packets--;
1107
1108 instance->rx_channel.buf_size = num_packets * maxpacket;
1109 instance->rx_channel.packet_size = maxpacket;
1110
1111 for (i = 0; i < 2; i++) {
1112 struct usbatm_channel *channel = i ?
1113 &instance->tx_channel : &instance->rx_channel;
1114
1115 dev_dbg(dev, "%s: using %d byte buffer for %s channel 0x%p\n",
1116 __func__, channel->buf_size, i ? "tx" : "rx", channel);
1117 }
1118
1119
1120
1121 for (i = 0; i < num_rcv_urbs + num_snd_urbs; i++) {
1122 u8 *buffer;
1123 struct usbatm_channel *channel = i < num_rcv_urbs ?
1124 &instance->rx_channel : &instance->tx_channel;
1125 struct urb *urb;
1126 unsigned int iso_packets = usb_pipeisoc(channel->endpoint) ? channel->buf_size / channel->packet_size : 0;
1127
1128 urb = usb_alloc_urb(iso_packets, GFP_KERNEL);
1129 if (!urb) {
1130 error = -ENOMEM;
1131 goto fail_unbind;
1132 }
1133
1134 instance->urbs[i] = urb;
1135
1136
1137 buffer = kzalloc(channel->buf_size, GFP_KERNEL);
1138 if (!buffer) {
1139 error = -ENOMEM;
1140 goto fail_unbind;
1141 }
1142
1143 usb_fill_bulk_urb(urb, instance->usb_dev, channel->endpoint,
1144 buffer, channel->buf_size, usbatm_complete, channel);
1145 if (iso_packets) {
1146 int j;
1147 urb->interval = 1;
1148 urb->transfer_flags = URB_ISO_ASAP;
1149 urb->number_of_packets = iso_packets;
1150 for (j = 0; j < iso_packets; j++) {
1151 urb->iso_frame_desc[j].offset = channel->packet_size * j;
1152 urb->iso_frame_desc[j].length = channel->packet_size;
1153 }
1154 }
1155
1156
1157 if (i >= num_rcv_urbs)
1158 list_add_tail(&urb->urb_list, &channel->list);
1159
1160 vdbg(&intf->dev, "%s: alloced buffer 0x%p buf size %u urb 0x%p",
1161 __func__, urb->transfer_buffer, urb->transfer_buffer_length, urb);
1162 }
1163
1164 instance->cached_vpi = ATM_VPI_UNSPEC;
1165 instance->cached_vci = ATM_VCI_UNSPEC;
1166 instance->cell_buf = kmalloc(instance->rx_channel.stride, GFP_KERNEL);
1167
1168 if (!instance->cell_buf) {
1169 error = -ENOMEM;
1170 goto fail_unbind;
1171 }
1172
1173 if (!(instance->flags & UDSL_SKIP_HEAVY_INIT) && driver->heavy_init) {
1174 error = usbatm_heavy_init(instance);
1175 } else {
1176 complete(&instance->thread_exited);
1177 error = usbatm_atm_init(instance);
1178 }
1179
1180 if (error < 0)
1181 goto fail_unbind;
1182
1183 usb_get_dev(usb_dev);
1184 usb_set_intfdata(intf, instance);
1185
1186 return 0;
1187
1188 fail_unbind:
1189 if (instance->driver->unbind)
1190 instance->driver->unbind(instance, intf);
1191 fail_free:
1192 kfree(instance->cell_buf);
1193
1194 for (i = 0; i < num_rcv_urbs + num_snd_urbs; i++) {
1195 if (instance->urbs[i])
1196 kfree(instance->urbs[i]->transfer_buffer);
1197 usb_free_urb(instance->urbs[i]);
1198 }
1199
1200 kfree(instance);
1201
1202 return error;
1203 }
1204 EXPORT_SYMBOL_GPL(usbatm_usb_probe);
1205
1206 void usbatm_usb_disconnect(struct usb_interface *intf)
1207 {
1208 struct device *dev = &intf->dev;
1209 struct usbatm_data *instance = usb_get_intfdata(intf);
1210 struct usbatm_vcc_data *vcc_data;
1211 int i;
1212
1213 if (!instance) {
1214 dev_dbg(dev, "%s: NULL instance!\n", __func__);
1215 return;
1216 }
1217
1218 usb_set_intfdata(intf, NULL);
1219
1220 mutex_lock(&instance->serialize);
1221 instance->disconnected = 1;
1222 if (instance->thread != NULL)
1223 send_sig(SIGTERM, instance->thread, 1);
1224 mutex_unlock(&instance->serialize);
1225
1226 wait_for_completion(&instance->thread_exited);
1227
1228 mutex_lock(&instance->serialize);
1229 list_for_each_entry(vcc_data, &instance->vcc_list, list)
1230 vcc_release_async(vcc_data->vcc, -EPIPE);
1231 mutex_unlock(&instance->serialize);
1232
1233 tasklet_disable(&instance->rx_channel.tasklet);
1234 tasklet_disable(&instance->tx_channel.tasklet);
1235
1236 for (i = 0; i < num_rcv_urbs + num_snd_urbs; i++)
1237 usb_kill_urb(instance->urbs[i]);
1238
1239 del_timer_sync(&instance->rx_channel.delay);
1240 del_timer_sync(&instance->tx_channel.delay);
1241
1242
1243
1244 INIT_LIST_HEAD(&instance->rx_channel.list);
1245 INIT_LIST_HEAD(&instance->tx_channel.list);
1246
1247 tasklet_enable(&instance->rx_channel.tasklet);
1248 tasklet_enable(&instance->tx_channel.tasklet);
1249
1250 if (instance->atm_dev && instance->driver->atm_stop)
1251 instance->driver->atm_stop(instance, instance->atm_dev);
1252
1253 if (instance->driver->unbind)
1254 instance->driver->unbind(instance, intf);
1255
1256 instance->driver_data = NULL;
1257
1258 for (i = 0; i < num_rcv_urbs + num_snd_urbs; i++) {
1259 kfree(instance->urbs[i]->transfer_buffer);
1260 usb_free_urb(instance->urbs[i]);
1261 }
1262
1263 kfree(instance->cell_buf);
1264
1265
1266 if (instance->atm_dev) {
1267 atm_dev_deregister(instance->atm_dev);
1268 instance->atm_dev = NULL;
1269 }
1270
1271 usbatm_put_instance(instance);
1272 }
1273 EXPORT_SYMBOL_GPL(usbatm_usb_disconnect);
1274
1275
1276
1277
1278
1279
1280 static int __init usbatm_usb_init(void)
1281 {
1282 if (sizeof(struct usbatm_control) > sizeof_field(struct sk_buff, cb)) {
1283 pr_err("%s unusable with this kernel!\n", usbatm_driver_name);
1284 return -EIO;
1285 }
1286
1287 if ((num_rcv_urbs > UDSL_MAX_RCV_URBS)
1288 || (num_snd_urbs > UDSL_MAX_SND_URBS)
1289 || (rcv_buf_bytes < 1)
1290 || (rcv_buf_bytes > UDSL_MAX_BUF_SIZE)
1291 || (snd_buf_bytes < 1)
1292 || (snd_buf_bytes > UDSL_MAX_BUF_SIZE))
1293 return -EINVAL;
1294
1295 return 0;
1296 }
1297 module_init(usbatm_usb_init);
1298
1299 static void __exit usbatm_usb_exit(void)
1300 {
1301 }
1302 module_exit(usbatm_usb_exit);
1303
1304 MODULE_AUTHOR(DRIVER_AUTHOR);
1305 MODULE_DESCRIPTION(DRIVER_DESC);
1306 MODULE_LICENSE("GPL");
1307
1308
1309
1310
1311
1312 #ifdef VERBOSE_DEBUG
1313 static int usbatm_print_packet(struct usbatm_data *instance,
1314 const unsigned char *data, int len)
1315 {
1316 unsigned char buffer[256];
1317 int i = 0, j = 0;
1318
1319 for (i = 0; i < len;) {
1320 buffer[0] = '\0';
1321 sprintf(buffer, "%.3d :", i);
1322 for (j = 0; (j < 16) && (i < len); j++, i++)
1323 sprintf(buffer, "%s %2.2x", buffer, data[i]);
1324 dev_dbg(&instance->usb_intf->dev, "%s", buffer);
1325 }
1326 return i;
1327 }
1328 #endif