67 #include <asm/uaccess.h>
69 #include <linux/errno.h>
72 #include <linux/kernel.h>
73 #include <linux/module.h>
75 #include <linux/netdevice.h>
77 #include <linux/sched.h>
78 #include <linux/signal.h>
79 #include <linux/slab.h>
80 #include <linux/stat.h>
82 #include <linux/wait.h>
87 static int usbatm_print_packet(
struct usbatm_data *instance,
const unsigned char *
data,
int len);
88 #define PACKETDEBUG(arg...) usbatm_print_packet(arg)
89 #define vdbg(arg...) dev_dbg(arg)
91 #define PACKETDEBUG(arg...)
96 #define DRIVER_VERSION "1.10"
97 #define DRIVER_DESC "Generic USB ATM/DSL I/O, version " DRIVER_VERSION
99 static const char usbatm_driver_name[] =
"usbatm";
101 #define UDSL_MAX_RCV_URBS 16
102 #define UDSL_MAX_SND_URBS 16
103 #define UDSL_MAX_BUF_SIZE 65536
104 #define UDSL_DEFAULT_RCV_URBS 4
105 #define UDSL_DEFAULT_SND_URBS 4
106 #define UDSL_DEFAULT_RCV_BUF_SIZE 3392
107 #define UDSL_DEFAULT_SND_BUF_SIZE 3392
109 #define ATM_CELL_HEADER (ATM_CELL_SIZE - ATM_CELL_PAYLOAD)
111 #define THROTTLE_MSECS 100
120 "Number of urbs used for reception (range: 0-"
126 "Number of urbs used for transmission (range: 0-"
132 "Size of the buffers used for reception, in bytes (range: 1-"
138 "Size of the buffers used for transmission, in bytes (range: 1-"
165 #define UDSL_SKB(x) ((struct usbatm_control *)(x)->cb)
171 static int usbatm_atm_open(
struct atm_vcc *
vcc);
172 static void usbatm_atm_close(
struct atm_vcc *
vcc);
177 static struct atmdev_ops usbatm_atm_devops = {
178 .dev_close = usbatm_atm_dev_close,
179 .open = usbatm_atm_open,
180 .close = usbatm_atm_close,
181 .ioctl = usbatm_atm_ioctl,
182 .send = usbatm_atm_send,
183 .proc_read = usbatm_atm_proc_read,
192 static inline unsigned int usbatm_pdu_length(
unsigned int length)
215 spin_lock_irq(&channel->
lock);
216 if (list_empty(&channel->
list)) {
217 spin_unlock_irq(&channel->
lock);
223 spin_unlock_irq(&channel->
lock);
228 static int usbatm_submit_urb(
struct urb *urb)
238 if (printk_ratelimit())
239 atm_warn(channel->
usbatm,
"%s: urb 0x%p submission failed (%d)!\n",
244 spin_lock_irq(&channel->
lock);
247 list_add(&urb->urb_list, &channel->
list);
249 spin_unlock_irq(&channel->
lock);
258 static void usbatm_complete(
struct urb *urb)
273 spin_unlock_irqrestore(&channel->
lock, flags);
281 if (printk_ratelimit())
283 __func__, urb, status);
287 tasklet_schedule(&channel->
tasklet);
301 if ((vcc_data->vci == vci) && (vcc_data->vpi == vpi))
310 short vpi = ((source[0] & 0x0f) << 4) | (source[1] >> 4);
311 int vci = ((source[1] & 0x0f) << 12) | (source[2] << 4) | (source[3] >> 4);
312 u8 pti = ((source[3] & 0xe) >> 1);
314 vdbg(&instance->usb_intf->dev,
"%s: vpi %hd, vci %d, pti %d", __func__, vpi, vci, pti);
316 if ((vci != instance->cached_vci) || (vpi != instance->cached_vpi)) {
317 instance->cached_vpi = vpi;
318 instance->cached_vci = vci;
320 instance->cached_vcc = usbatm_find_vcc(instance, vpi, vci);
322 if (!instance->cached_vcc)
323 atm_rldbg(instance,
"%s: unknown vpi/vci (%hd/%d)!\n", __func__, vpi, vci);
326 if (!instance->cached_vcc)
329 vcc = instance->cached_vcc->vcc;
333 if (printk_ratelimit())
334 atm_warn(instance,
"%s: OAM not supported (vpi %d, vci %d)!\n",
340 sarb = instance->cached_vcc->sarb;
343 atm_rldbg(instance,
"%s: buffer overrun (sarb->len %u, vcc: 0x%p)!\n",
344 __func__, sarb->
len, vcc);
362 atm_rldbg(instance,
"%s: bogus length %u (vcc: 0x%p)!\n",
363 __func__, length, vcc);
368 pdu_length = usbatm_pdu_length(length);
370 if (sarb->
len < pdu_length) {
371 atm_rldbg(instance,
"%s: bogus pdu_length %u (sarb->len: %u, vcc: 0x%p)!\n",
372 __func__, pdu_length, sarb->
len, vcc);
377 if (
crc32_be(~0, skb_tail_pointer(sarb) - pdu_length, pdu_length) != 0xc704dd7b) {
378 atm_rldbg(instance,
"%s: packet failed crc check (vcc: 0x%p)!\n",
384 vdbg(&instance->usb_intf->dev,
385 "%s: got packet (length: %u, pdu_length: %u, vcc: 0x%p)",
386 __func__, length, pdu_length, vcc);
388 if (!(skb = dev_alloc_skb(length))) {
389 if (printk_ratelimit())
390 atm_err(instance,
"%s: no memory for skb (length: %u)!\n",
396 vdbg(&instance->usb_intf->dev,
397 "%s: allocated new sk_buff (skb: 0x%p, skb->truesize: %u)",
401 atm_rldbg(instance,
"%s: failed atm_charge (skb->truesize: %u)!\n",
407 skb_copy_to_linear_data(skb,
408 skb_tail_pointer(sarb) - pdu_length,
410 __skb_put(skb, length);
412 vdbg(&instance->usb_intf->dev,
413 "%s: sending skb 0x%p, skb->len %u, skb->truesize %u",
426 static void usbatm_extract_cells(
struct usbatm_data *instance,
427 unsigned char *source,
unsigned int avail_data)
429 unsigned int stride = instance->
rx_channel.stride;
430 unsigned int buf_usage = instance->
buf_usage;
438 unsigned int space_left = stride - buf_usage;
442 if (avail_data >= space_left) {
444 memcpy(cell_buf + buf_usage, source, space_left);
445 source += space_left;
446 avail_data -= space_left;
447 usbatm_extract_one_cell(instance, cell_buf);
451 memcpy(cell_buf + buf_usage, source, avail_data);
452 instance->
buf_usage = buf_usage + avail_data;
457 for (; avail_data >= stride; avail_data -= stride, source += stride)
458 usbatm_extract_one_cell(instance, source);
460 if (avail_data > 0) {
473 static unsigned int usbatm_write_cells(
struct usbatm_data *instance,
475 u8 *
target,
unsigned int avail_space)
479 unsigned int bytes_written;
480 unsigned int stride = instance->
tx_channel.stride;
482 vdbg(&instance->
usb_intf->dev,
"%s: skb->len=%d, avail_space=%u",
483 __func__, skb->
len, avail_space);
486 for (bytes_written = 0; bytes_written < avail_space && ctrl->
len;
487 bytes_written += stride, target += stride) {
492 ptr[0] = vcc->
vpi >> 4;
493 ptr[1] = (vcc->
vpi << 4) | (vcc->
vci >> 12);
494 ptr[2] = vcc->
vci >> 4;
495 ptr[3] = vcc->
vci << 4;
499 skb_copy_from_linear_data(skb, ptr, data_len);
501 __skb_pull(skb, data_len);
512 trailer[2] = ctrl->
len >> 8;
513 trailer[3] = ctrl->
len;
517 trailer[4] = ctrl->
crc >> 24;
518 trailer[5] = ctrl->
crc >> 16;
519 trailer[6] = ctrl->
crc >> 8;
520 trailer[7] = ctrl->
crc;
529 return bytes_written;
537 static void usbatm_rx_process(
unsigned long data)
542 while ((urb = usbatm_pop_urb(&instance->
rx_channel))) {
544 "%s: processing urb 0x%p", __func__, urb);
546 if (usb_pipeisoc(urb->pipe)) {
547 unsigned char *merge_start =
NULL;
548 unsigned int merge_length = 0;
549 const unsigned int packet_size = instance->
rx_channel.packet_size;
552 for (i = 0; i < urb->number_of_packets; i++) {
553 if (!urb->iso_frame_desc[i].status) {
554 unsigned int actual_length = urb->iso_frame_desc[
i].actual_length;
556 UDSL_ASSERT(instance, actual_length <= packet_size);
559 merge_start = (
unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset;
560 merge_length += actual_length;
561 if (merge_length && (actual_length < packet_size)) {
562 usbatm_extract_cells(instance, merge_start, merge_length);
566 atm_rldbg(instance,
"%s: status %d in frame %d!\n", __func__, urb->status, i);
568 usbatm_extract_cells(instance, merge_start, merge_length);
575 usbatm_extract_cells(instance, merge_start, merge_length);
578 usbatm_extract_cells(instance, urb->transfer_buffer, urb->actual_length);
582 if (usbatm_submit_urb(urb))
592 static void usbatm_tx_process(
unsigned long data)
596 struct urb *urb =
NULL;
598 unsigned int bytes_written = 0;
609 buffer = urb->transfer_buffer;
610 bytes_written = (urb->status == -
EAGAIN) ?
611 urb->transfer_buffer_length : 0;
614 bytes_written += usbatm_write_cells(instance, skb,
615 buffer + bytes_written,
616 buf_size - bytes_written);
619 "%s: wrote %u bytes from skb 0x%p to urb 0x%p",
620 __func__, bytes_written, skb, urb);
625 usbatm_pop(vcc, skb);
631 if (bytes_written == buf_size || (!skb && bytes_written)) {
632 urb->transfer_buffer_length = bytes_written;
634 if (usbatm_submit_urb(urb))
643 static void usbatm_cancel_send(
struct usbatm_data *instance,
648 atm_dbg(instance,
"%s entered\n", __func__);
649 spin_lock_irq(&instance->
sndqueue.lock);
650 skb_queue_walk_safe(&instance->
sndqueue, skb, n) {
651 if (
UDSL_SKB(skb)->atm.vcc == vcc) {
652 atm_dbg(instance,
"%s: popping skb 0x%p\n", __func__, skb);
653 __skb_unlink(skb, &instance->
sndqueue);
654 usbatm_pop(vcc, skb);
657 spin_unlock_irq(&instance->
sndqueue.lock);
659 tasklet_disable(&instance->
tx_channel.tasklet);
661 atm_dbg(instance,
"%s: popping current skb (0x%p)\n", __func__, skb);
663 usbatm_pop(vcc, skb);
665 tasklet_enable(&instance->
tx_channel.tasklet);
666 atm_dbg(instance,
"%s done\n", __func__);
669 static int usbatm_atm_send(
struct atm_vcc *vcc,
struct sk_buff *skb)
675 vdbg(&instance->
usb_intf->dev,
"%s called (skb 0x%p, len %u)", __func__,
688 atm_rldbg(instance,
"%s: unsupported ATM type %d!\n", __func__, vcc->
qos.aal);
694 atm_rldbg(instance,
"%s: packet too long (%d vs %d)!\n",
708 tasklet_schedule(&instance->
tx_channel.tasklet);
713 usbatm_pop(vcc, skb);
722 static void usbatm_destroy_instance(
struct kref *
kref)
726 usb_dbg(instance,
"%s\n", __func__);
734 static void usbatm_get_instance(
struct usbatm_data *instance)
736 usb_dbg(instance,
"%s\n", __func__);
741 static void usbatm_put_instance(
struct usbatm_data *instance)
743 usb_dbg(instance,
"%s\n", __func__);
745 kref_put(&instance->
refcount, usbatm_destroy_instance);
760 usb_dbg(instance,
"%s\n", __func__);
762 usbatm_put_instance(instance);
765 static int usbatm_atm_proc_read(
struct atm_dev *atm_dev, loff_t *
pos,
char *
page)
777 return sprintf(page,
"MAC: %pM\n", atm_dev->
esi);
781 "AAL5: tx %d ( %d err ), rx %d ( %d err, %d drop )\n",
790 return sprintf(page,
"Disconnected\n");
792 switch (atm_dev->
signal) {
794 return sprintf(page,
"Line up\n");
796 return sprintf(page,
"Line down\n");
798 return sprintf(page,
"Line state unknown\n");
805 static int usbatm_atm_open(
struct atm_vcc *vcc)
811 short vpi = vcc->
vpi;
816 atm_dbg(instance,
"%s: vpi %hd, vci %d\n", __func__, vpi, vci);
820 atm_warn(instance,
"%s: unsupported ATM type %d!\n", __func__, vcc->
qos.aal);
826 atm_dbg(instance,
"%s: max_sdu %d out of range!\n", __func__, vcc->
qos.rxtp.max_sdu);
833 atm_dbg(instance,
"%s: disconnected!\n", __func__);
838 if (usbatm_find_vcc(instance, vpi, vci)) {
839 atm_dbg(instance,
"%s: %hd/%d already in use!\n", __func__, vpi, vci);
845 atm_err(instance,
"%s: no memory for vcc_data!\n", __func__);
854 new->sarb = alloc_skb(usbatm_pdu_length(vcc->
qos.rxtp.max_sdu),
GFP_KERNEL);
856 atm_err(instance,
"%s: no memory for SAR buffer!\n", __func__);
863 tasklet_disable(&instance->
rx_channel.tasklet);
867 list_add(&new->list, &instance->
vcc_list);
868 tasklet_enable(&instance->
rx_channel.tasklet);
876 atm_dbg(instance,
"%s: allocated vcc data 0x%p\n", __func__,
new);
886 static void usbatm_atm_close(
struct atm_vcc *vcc)
891 if (!instance || !vcc_data)
894 atm_dbg(instance,
"%s entered\n", __func__);
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);
899 usbatm_cancel_send(instance, vcc);
903 tasklet_disable(&instance->
rx_channel.tasklet);
910 tasklet_enable(&instance->
rx_channel.tasklet);
926 atm_dbg(instance,
"%s successful\n", __func__);
929 static int usbatm_atm_ioctl(
struct atm_dev *atm_dev,
unsigned int cmd,
945 static int usbatm_atm_init(
struct usbatm_data *instance)
947 struct atm_dev *atm_dev;
955 &instance->
usb_intf->dev, &usbatm_atm_devops,
958 usb_err(instance,
"%s: failed to register ATM device!\n", __func__);
971 if (instance->
driver->atm_start && ((ret = instance->
driver->atm_start(instance, atm_dev)) < 0)) {
972 atm_err(instance,
"%s: atm_start failed: %d!\n", __func__, ret);
976 usbatm_get_instance(instance);
983 for (i = 0; i < num_rcv_urbs; i++)
984 usbatm_submit_urb(instance->
urbs[i]);
999 static int usbatm_do_heavy_init(
void *arg)
1007 ret = instance->
driver->heavy_init(instance, instance->
usb_intf);
1010 ret = usbatm_atm_init(instance);
1019 static int usbatm_heavy_init(
struct usbatm_data *instance)
1024 instance->
driver->driver_name);
1026 usb_err(instance,
"%s: failed to create kernel_thread (%ld)!\n",
1027 __func__, PTR_ERR(t));
1038 static void usbatm_tasklet_schedule(
unsigned long data)
1046 INIT_LIST_HEAD(&channel->
list);
1047 channel->
delay.function = usbatm_tasklet_schedule;
1056 struct usb_device *usb_dev = interface_to_usbdev(intf);
1061 unsigned int maxpacket, num_packets;
1063 dev_dbg(dev,
"%s: trying driver %s with vendor=%04x, product=%04x, ifnum %2d\n",
1067 intf->altsetting->desc.bInterfaceNumber);
1070 instance = kzalloc(
sizeof(*instance) +
sizeof(
struct urb *) * (num_rcv_urbs + num_snd_urbs),
GFP_KERNEL);
1072 dev_err(dev,
"%s: no memory for instance data!\n", __func__);
1087 if ((i =
usb_string(usb_dev, usb_dev->descriptor.iProduct, buf, length)) < 0)
1097 if (length <= 0 || (i = usb_make_path(usb_dev, buf, length)) < 0)
1106 if (driver->
bind && (error = driver->
bind(instance, intf,
id)) < 0) {
1107 dev_err(dev,
"%s: bind failed: %d!\n", __func__, error);
1120 INIT_LIST_HEAD(&instance->
vcc_list);
1121 skb_queue_head_init(&instance->
sndqueue);
1126 tasklet_init(&instance->tx_channel.tasklet, usbatm_tx_process, (
unsigned long)instance);
1129 instance->rx_channel.usbatm = instance->tx_channel.usbatm = instance;
1132 instance->rx_channel.endpoint = usb_rcvisocpipe(usb_dev, driver->
isoc_in);
1134 instance->rx_channel.endpoint = usb_rcvbulkpipe(usb_dev, driver->
bulk_in);
1136 instance->tx_channel.endpoint = usb_sndbulkpipe(usb_dev, driver->
bulk_out);
1139 instance->tx_channel.buf_size =
max(instance->tx_channel.stride,
1140 snd_buf_bytes - (snd_buf_bytes % instance->tx_channel.stride));
1143 maxpacket = usb_maxpacket(usb_dev, instance->rx_channel.endpoint, 0);
1146 dev_err(dev,
"%s: invalid endpoint %02x!\n", __func__,
1147 usb_pipeendpoint(instance->rx_channel.endpoint));
1152 num_packets =
max(1
U, (rcv_buf_bytes + maxpacket / 2) / maxpacket);
1157 instance->rx_channel.buf_size = num_packets * maxpacket;
1158 instance->rx_channel.packet_size = maxpacket;
1161 for (i = 0; i < 2; i++) {
1163 &instance->tx_channel : &instance->rx_channel;
1165 dev_dbg(dev,
"%s: using %d byte buffer for %s channel 0x%p\n", __func__, channel->
buf_size, i ?
"tx" :
"rx", channel);
1171 for (i = 0; i < num_rcv_urbs + num_snd_urbs; i++) {
1174 &instance->rx_channel : &instance->tx_channel;
1182 dev_err(dev,
"%s: no memory for urb %d!\n", __func__, i);
1187 instance->urbs[
i] =
urb;
1192 dev_err(dev,
"%s: no memory for buffer %d!\n", __func__, i);
1197 usb_fill_bulk_urb(urb, instance->usb_dev, channel->
endpoint,
1198 buffer, channel->
buf_size, usbatm_complete, channel);
1202 urb->transfer_flags = URB_ISO_ASAP;
1203 urb->number_of_packets = iso_packets;
1204 for (j = 0; j < iso_packets; j++) {
1211 if (i >= num_rcv_urbs)
1214 vdbg(&intf->dev,
"%s: alloced buffer 0x%p buf size %u urb 0x%p",
1215 __func__, urb->transfer_buffer, urb->transfer_buffer_length, urb);
1222 if (!instance->cell_buf) {
1223 dev_err(dev,
"%s: no memory for cell buffer!\n", __func__);
1229 error = usbatm_heavy_init(instance);
1231 complete(&instance->thread_exited);
1232 error = usbatm_atm_init(instance);
1239 usb_set_intfdata(intf, instance);
1244 if (instance->driver->unbind)
1245 instance->driver->unbind(instance, intf);
1247 kfree(instance->cell_buf);
1249 for (i = 0; i < num_rcv_urbs + num_snd_urbs; i++) {
1250 if (instance->urbs[i])
1251 kfree(instance->urbs[i]->transfer_buffer);
1264 struct usbatm_data *instance = usb_get_intfdata(intf);
1268 dev_dbg(dev,
"%s entered\n", __func__);
1271 dev_dbg(dev,
"%s: NULL instance!\n", __func__);
1275 usb_set_intfdata(intf, NULL);
1279 if (instance->
thread != NULL)
1290 tasklet_disable(&instance->
rx_channel.tasklet);
1291 tasklet_disable(&instance->
tx_channel.tasklet);
1293 for (i = 0; i < num_rcv_urbs + num_snd_urbs; i++)
1304 tasklet_enable(&instance->
rx_channel.tasklet);
1305 tasklet_enable(&instance->
tx_channel.tasklet);
1310 if (instance->
driver->unbind)
1311 instance->
driver->unbind(instance, intf);
1315 for (i = 0; i < num_rcv_urbs + num_snd_urbs; i++) {
1316 kfree(instance->
urbs[i]->transfer_buffer);
1328 usbatm_put_instance(instance);
1337 static int __init usbatm_usb_init(
void)
1340 printk(
KERN_ERR "%s unusable with this kernel!\n", usbatm_driver_name);
1346 || (rcv_buf_bytes < 1)
1348 || (snd_buf_bytes < 1)
1356 static void __exit usbatm_usb_exit(
void)
1370 #ifdef VERBOSE_DEBUG
1371 static int usbatm_print_packet(
struct usbatm_data *instance,
1372 const unsigned char *data,
int len)
1374 unsigned char buffer[256];
1377 for (i = 0; i < len;) {
1380 for (
j = 0; (
j < 16) && (i < len);
j++, i++)
1381 sprintf(buffer,
"%s %2.2x", buffer, data[i]);