Debian Codegraph @

drivers/dahdi/xpp/xpp_usb.c

raw
1 /*
2 * Written by Oron Peled <[email protected]>
3 * Copyright (C) 2004-2006, Xorcom
4 *
5 * All rights reserved.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 *
21 */
22 #include <linux/version.h>
23 #include <linux/kernel.h>
24 #include <linux/errno.h>
25 #include <linux/module.h>
26 #include <linux/init.h>
27 #include <linux/interrupt.h>
28 #include <linux/delay.h> /* for udelay */
29 #include <linux/seq_file.h>
30 #include <asm/uaccess.h>
31 #include <asm/atomic.h>
32 #include <asm/timex.h>
33 #include <linux/proc_fs.h>
34 #include <linux/usb.h>
35 #include "xpd.h"
36 #include "xproto.h"
37 #include "xbus-core.h"
38 #include "xframe_queue.h"
39 #ifdef DEBUG
40 #include "card_fxs.h"
41 #include "card_fxo.h"
42 #endif
43 #include "parport_debug.h"
44
45 static const char rcsid[] = "$Id$";
46
47 /* must be before dahdi_debug.h */
48 static DEF_PARM(int, debug, 0, 0644, "Print DBG statements");
49 static DEF_PARM(int, usb1, 0, 0644, "Allow using USB 1.1 interfaces");
50 static DEF_PARM(uint, tx_sluggish, 2000, 0644, "A sluggish transmit (usec)");
51 static DEF_PARM(uint, drop_pcm_after, 6, 0644,
52 "Number of consecutive tx_sluggish to start dropping PCM");
53 static DEF_PARM(uint, sluggish_pcm_keepalive, 50, 0644,
54 "During sluggish -- Keep-alive PCM (1 every #)");
55
56 #include "dahdi_debug.h"
57
58 #define XUSB_PRINTK(level, xusb, fmt, ...) \
59 printk(KERN_ ## level "%s-%s: xusb-%d (%s) [%s]: " fmt, #level, \
60 THIS_MODULE->name, (xusb)->index, xusb->path, \
61 xusb->serial, ## __VA_ARGS__)
62
63 #define XUSB_DBG(bits, xusb, fmt, ...) \
64 ((void)((debug & (DBG_ ## bits)) && XUSB_PRINTK(DEBUG, \
65 xusb, "%s: " fmt, __func__, ## __VA_ARGS__)))
66 #define XUSB_ERR(xusb, fmt, ...) \
67 XUSB_PRINTK(ERR, xusb, fmt, ## __VA_ARGS__)
68 #define XUSB_NOTICE(xusb, fmt, ...) \
69 XUSB_PRINTK(NOTICE, xusb, fmt, ## __VA_ARGS__)
70 #define XUSB_INFO(xusb, fmt, ...) \
71 XUSB_PRINTK(INFO, xusb, fmt, ## __VA_ARGS__)
72
73 /* Get a minor range for your devices from the usb maintainer */
74 #define USB_SKEL_MINOR_BASE 192
75
76 #ifdef CONFIG_PROC_FS
77 #define PROC_USBXPP_SUMMARY "xpp_usb"
78 #endif
79
80 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 34)
81 #define usb_alloc_coherent(dev, size, mem_flags, dma) \
82 usb_buffer_alloc(dev, size, mem_flags, dma)
83 #define usb_free_coherent(dev, size, addr, dma) \
84 usb_buffer_free(dev, size, addr, dma)
85 #endif
86
87 #ifdef DEBUG_PCM_TIMING
88 static cycles_t stamp_last_pcm_read;
89 static cycles_t accumulate_diff;
90 #endif
91
92 struct xusb_model_info;
93
94 struct xusb_endpoint {
95 int ep_addr;
96 int max_size;
97 usb_complete_t callback;
98 };
99
100 enum xusb_dir {
101 XUSB_RECV = 0,
102 XUSB_SEND = 1,
103 };
104
105 static __must_check int xframe_send_pcm(xbus_t *xbus, xframe_t *xframe);
106 static __must_check int xframe_send_cmd(xbus_t *xbus, xframe_t *xframe);
107 static __must_check xframe_t *alloc_xframe(xbus_t *xbus, gfp_t flags);
108 static void free_xframe(xbus_t *xbus, xframe_t *frm);
109
110 static struct xbus_ops xusb_ops = {
111 .xframe_send_pcm = xframe_send_pcm,
112 .xframe_send_cmd = xframe_send_cmd,
113 .alloc_xframe = alloc_xframe,
114 .free_xframe = free_xframe,
115 };
116
117 enum {
118 XUSB_N_RX_FRAMES,
119 XUSB_N_TX_FRAMES,
120 XUSB_N_RX_ERRORS,
121 XUSB_N_TX_ERRORS,
122 XUSB_N_RX_DROPS,
123 XUSB_N_TX_DROPS,
124 XUSB_N_RCV_ZERO_LEN,
125 };
126
127 #define XUSB_COUNTER(xusb, counter) ((xusb)->counters[XUSB_N_ ## counter])
128
129 #define C_(x) [ XUSB_N_ ## x ] = { #x }
130
131 static struct xusb_counters {
132 char *name;
133 } xusb_counters[] = {
134 C_(RX_FRAMES),
135 C_(TX_FRAMES),
136 C_(RX_ERRORS),
137 C_(TX_ERRORS),
138 C_(RX_DROPS),
139 C_(TX_DROPS),
140 C_(RCV_ZERO_LEN),
141 };
142
143 #undef C_
144
145 #define XUSB_COUNTER_MAX ARRAY_SIZE(xusb_counters)
146
147 #define MAX_PENDING_WRITES 100
148
149 static KMEM_CACHE_T *xusb_cache;
150
151 typedef struct xusb xusb_t;
152
153 /*
154 * A uframe is our low level representation of a frame.
155 *
156 * It contains the metadata for the usb stack (a urb)
157 * and the metadata for the xbus-core (an xframe)
158 * as well as pointing to the data (transfer_buffer, transfer_buffer_length)
159 * directionality (send/receive) and ownership (xusb).
160 */
161 struct uframe {
162 unsigned long uframe_magic;
163 #define UFRAME_MAGIC 654321L
164 struct urb urb;
165 xframe_t xframe;
166 size_t transfer_buffer_length;
167 void *transfer_buffer; /* max XFRAME_DATASIZE */
168 xusb_t *xusb;
169 };
170
171 #define urb_to_uframe(urb) \
172 container_of(urb, struct uframe, urb)
173 #define xframe_to_uframe(xframe) \
174 container_of(xframe, struct uframe, xframe)
175 #define xusb_of(xbus) \
176 ((xusb_t *)((xbus)->transport.priv))
177
178 #define USEC_BUCKET 100 /* usec */
179 #define NUM_BUCKETS 15
180 #define BUCKET_START (500/USEC_BUCKET) /* skip uninteresting */
181
182 /*
183 * USB XPP Bus (a USB Device)
184 */
185 struct xusb {
186 uint xbus_num;
187 struct usb_device *udev; /* save off the usb device pointer */
188 struct usb_interface *interface; /* the interface for this device */
189 unsigned char minor; /* the starting minor number for this device */
190 uint index;
191 char path[XBUS_DESCLEN]; /* a unique path */
192
193 struct xusb_model_info *model_info;
194 struct xusb_endpoint endpoints[2]; /* RECV/SEND endpoints */
195
196 int present; /* if the device is not disconnected */
197 atomic_t pending_writes; /* submited but not out yet */
198 atomic_t pending_reads; /* submited but not in yet */
199 struct semaphore sem; /* locks this structure */
200 int counters[XUSB_COUNTER_MAX];
201
202 /* metrics */
203 struct timeval last_tx;
204 unsigned int max_tx_delay;
205 uint usb_tx_delay[NUM_BUCKETS];
206 uint sluggish_debounce;
207 bool drop_pcm; /* due to sluggishness */
208 atomic_t usb_sluggish_count;
209
210 const char *manufacturer;
211 const char *product;
212 const char *serial;
213 const char *interface_name;
214
215 };
216
217 static DEFINE_SPINLOCK(xusb_lock);
218 static xusb_t *xusb_array[MAX_BUSES] = { };
219
220 static unsigned bus_count;
221
222 /* prevent races between open() and disconnect() */
223 static DEFINE_MUTEX(protect_xusb_devices);
224
225 /*
226 * AsteriskNow kernel has backported the "lean" callback from 2.6.20
227 * to 2.6.19 without any macro to notify of this fact -- how lovely.
228 * Debian-Etch and Centos5 are using 2.6.18 for now (lucky for us).
229 * Fedora6 jumped from 2.6.18 to 2.6.20. So far luck is on our side ;-)
230 */
231 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19)
232 #define USB_PASS_CB(u) struct urb *u, struct pt_regs *regs
233 #else
234 #define USB_PASS_CB(u) struct urb *u
235 #endif
236
237 static void xpp_send_callback(USB_PASS_CB(urb));
238 static void xpp_receive_callback(USB_PASS_CB(urb));
239 static int xusb_probe(struct usb_interface *interface,
240 const struct usb_device_id *id);
241 static void xusb_disconnect(struct usb_interface *interface);
242 #ifdef CONFIG_PROC_FS
243 static const struct file_operations xusb_read_proc_ops;
244 #endif
245
246 /*------------------------------------------------------------------*/
247
248 /*
249 * Updates the urb+xframe metadata from the uframe information.
250 */
251 static void uframe_recompute(struct uframe *uframe, enum xusb_dir dir)
252 {
253 struct urb *urb = &uframe->urb;
254 xusb_t *xusb = uframe->xusb;
255 struct usb_device *udev = xusb->udev;
256 struct xusb_endpoint *xusb_ep = &xusb->endpoints[dir];
257 unsigned int ep_addr = xusb_ep->ep_addr;
258 usb_complete_t urb_cb = xusb_ep->callback;
259 unsigned int epnum = ep_addr & USB_ENDPOINT_NUMBER_MASK;
260 int pipe = usb_pipein(ep_addr)
261 ? usb_rcvbulkpipe(udev, epnum)
262 : usb_sndbulkpipe(udev, epnum);
263
264 BUG_ON(uframe->uframe_magic != UFRAME_MAGIC);
265 usb_fill_bulk_urb(urb, udev, pipe, uframe->transfer_buffer,
266 uframe->transfer_buffer_length, urb_cb, uframe);
267 urb->transfer_flags = (URB_NO_TRANSFER_DMA_MAP);
268 }
269
270 static xframe_t *alloc_xframe(xbus_t *xbus, gfp_t gfp_flags)
271 {
272 struct uframe *uframe;
273 xusb_t *xusb;
274 void *p;
275 int size;
276 static int rate_limit;
277
278 BUG_ON(!xbus);
279 xusb = xusb_of(xbus);
280 BUG_ON(!xusb);
281 if (!xusb->present) {
282 if ((rate_limit++ % 1003) == 0)
283 XUSB_ERR(xusb,
284 "abort allocations during "
285 "device disconnect (%d)\n",
286 rate_limit);
287 return NULL;
288 }
289 size =
290 min(xusb->endpoints[XUSB_SEND].max_size,
291 xusb->endpoints[XUSB_RECV].max_size);
292 uframe = kmem_cache_alloc(xusb_cache, gfp_flags);
293 if (!uframe) {
294 if ((rate_limit++ % 1003) == 0)
295 XUSB_ERR(xusb, "frame allocation failed (%d)\n",
296 rate_limit);
297 return NULL;
298 }
299 usb_init_urb(&uframe->urb);
300 p = usb_alloc_coherent(xusb->udev, size, gfp_flags,
301 &uframe->urb.transfer_dma);
302 if (!p) {
303 if ((rate_limit++ % 1003) == 0)
304 XUSB_ERR(xusb, "buffer allocation failed (%d)\n",
305 rate_limit);
306 kmem_cache_free(xusb_cache, uframe);
307 return NULL;
308 }
309 uframe->uframe_magic = UFRAME_MAGIC;
310 uframe->transfer_buffer_length = size;
311 uframe->transfer_buffer = p;
312 uframe->xusb = xusb;
313 xframe_init(xbus, &uframe->xframe, uframe->transfer_buffer,
314 uframe->transfer_buffer_length, uframe);
315 return &uframe->xframe;
316 }
317
318 static void free_xframe(xbus_t *xbus, xframe_t *xframe)
319 {
320 struct uframe *uframe = xframe_to_uframe(xframe);
321 struct urb *urb = &uframe->urb;
322
323 BUG_ON(xbus->transport.priv != uframe->xusb);
324 //XUSB_INFO(uframe->xusb, "frame_free\n");
325 usb_free_coherent(urb->dev, uframe->transfer_buffer_length,
326 urb->transfer_buffer, urb->transfer_dma);
327 memset(uframe, 0, sizeof(*uframe));
328 kmem_cache_free(xusb_cache, uframe);
329 }
330
331 /*------------------------------------------------------------------*/
332
333 /*
334 * Actuall frame sending -- both PCM and commands.
335 */
336 static int do_send_xframe(xbus_t *xbus, xframe_t *xframe)
337 {
338 struct urb *urb;
339 xusb_t *xusb;
340 int ret = 0;
341 struct uframe *uframe;
342
343 BUG_ON(!xframe);
344 BUG_ON(xframe->xframe_magic != XFRAME_MAGIC);
345 xusb = xusb_of(xbus);
346 BUG_ON(!xusb);
347 if (!xusb->present) {
348 static int rate_limit;
349
350 if ((rate_limit++ % 1003) == 0)
351 XUSB_ERR(xusb,
352 "abort do_send_xframe during "
353 "device disconnect (%d)\n",
354 rate_limit);
355 ret = -ENODEV;
356 goto failure;
357 }
358 /*
359 * If something really bad happend, do not overflow the USB stack
360 */
361 if (atomic_read(&xusb->pending_writes) > MAX_PENDING_WRITES) {
362 static int rate_limit;
363
364 if ((rate_limit++ % 5000) == 0)
365 XUSB_ERR(xusb,
366 "USB device is totaly stuck. "
367 "Dropping packets (#%d).\n",
368 rate_limit);
369 ret = -ENODEV;
370 goto failure;
371 }
372 uframe = xframe->priv;
373 BUG_ON(!uframe);
374 BUG_ON(uframe->uframe_magic != UFRAME_MAGIC);
375 uframe_recompute(uframe, XUSB_SEND);
376 urb = &uframe->urb;
377 BUG_ON(!urb);
378 /* update urb length */
379 urb->transfer_buffer_length = XFRAME_LEN(xframe);
380 do_gettimeofday(&xframe->tv_submitted);
381 ret = usb_submit_urb(urb, GFP_ATOMIC);
382 if (ret < 0) {
383 static int rate_limit;
384
385 if ((rate_limit++ % 1000) == 0)
386 XBUS_ERR(xbus, "%s: usb_submit_urb failed: %d\n",
387 __func__, ret);
388 ret = -EBADF;
389 goto failure;
390 }
391 // if (debug)
392 // dump_xframe("USB_FRAME_SEND", xbus, xframe, debug);
393 atomic_inc(&xusb->pending_writes);
394 return 0;
395 failure:
396 XUSB_COUNTER(xusb, TX_ERRORS)++;
397 FREE_SEND_XFRAME(xbus, xframe); /* return to pool */
398 return ret;
399 }
400
401 /*
402 * PCM wrapper
403 */
404 static int xframe_send_pcm(xbus_t *xbus, xframe_t *xframe)
405 {
406 xusb_t *xusb;
407
408 BUG_ON(!xbus);
409 BUG_ON(!xframe);
410 xusb = xusb_of(xbus);
411 BUG_ON(!xusb);
412 if (xusb->drop_pcm) {
413 static int rate_limit;
414
415 if ((rate_limit++ % 1000) == 0)
416 XUSB_ERR(xusb, "Sluggish USB: drop tx-pcm (%d)\n",
417 rate_limit);
418 /* Let trickle of TX-PCM, so Astribank will not reset */
419 if (sluggish_pcm_keepalive &&
420 ((rate_limit % sluggish_pcm_keepalive) != 0)) {
421 XUSB_COUNTER(xusb, TX_DROPS)++;
422 goto err;
423 }
424 }
425 return do_send_xframe(xbus, xframe);
426 err:
427 FREE_SEND_XFRAME(xbus, xframe); /* return to pool */
428 return -EIO;
429 }
430
431 /*
432 * commands wrapper
433 */
434 static int xframe_send_cmd(xbus_t *xbus, xframe_t *xframe)
435 {
436 BUG_ON(!xbus);
437 BUG_ON(!xframe);
438 //XBUS_INFO(xbus, "%s:\n", __func__);
439 return do_send_xframe(xbus, xframe);
440 }
441
442 /*
443 * get a urb from the receive_pool and submit it on the read endpoint.
444 */
445 static bool xusb_listen(xusb_t *xusb)
446 {
447 xbus_t *xbus = xbus_num(xusb->xbus_num);
448 xframe_t *xframe;
449 struct uframe *uframe;
450 int ret = 0;
451
452 BUG_ON(!xbus);
453 xframe = ALLOC_RECV_XFRAME(xbus);
454 if (!xframe) {
455 XBUS_ERR(xbus, "Empty receive_pool\n");
456 goto out;
457 }
458 uframe = xframe_to_uframe(xframe);
459 uframe_recompute(uframe, XUSB_RECV);
460 ret = usb_submit_urb(&uframe->urb, GFP_ATOMIC);
461 if (ret < 0) {
462 static int rate_limit;
463
464 if ((rate_limit++ % 1000) == 0)
465 XBUS_ERR(xbus, "%s: usb_submit_urb failed: %d\n",
466 __func__, ret);
467 FREE_RECV_XFRAME(xbus, xframe);
468 goto out;
469 }
470 atomic_inc(&xusb->pending_reads);
471 ret = 1;
472 out:
473 return ret;
474 }
475
476 /*------------------------- XPP USB Bus Handling -------------------*/
477
478 enum XUSB_MODELS {
479 MODEL_FPGA_XPD
480 };
481
482 static const struct xusb_model_info {
483 const char *desc;
484 int iface_num;
485 struct xusb_endpoint in;
486 struct xusb_endpoint out;
487 } model_table[] = {
488 [MODEL_FPGA_XPD] = {
489 .iface_num = 0,
490 .in = { .ep_addr = 0x86 },
491 .out = { .ep_addr = 0x02 },
492 .desc = "FPGA_XPD"
493 },
494 };
495
496 /* table of devices that work with this driver */
497 static const struct usb_device_id xusb_table[] = {
498 /* FPGA_FXS */ {USB_DEVICE(0xE4E4, 0x1132),
499 .driver_info = (kernel_ulong_t)&model_table[MODEL_FPGA_XPD]},
500 /* FPGA_1141 */ {USB_DEVICE(0xE4E4, 0x1142),
501 .driver_info = (kernel_ulong_t)&model_table[MODEL_FPGA_XPD]},
502 /* FPGA_1151 */ {USB_DEVICE(0xE4E4, 0x1152),
503 .driver_info = (kernel_ulong_t)&model_table[MODEL_FPGA_XPD]},
504 /* FPGA_1161 */ {USB_DEVICE(0xE4E4, 0x1162),
505 .driver_info = (kernel_ulong_t)&model_table[MODEL_FPGA_XPD]},
506 /* Terminate */ {}
507 };
508
509 MODULE_DEVICE_TABLE(usb, xusb_table);
510
511 /*
512 * USB specific object needed to register this driver
513 * with the usb subsystem
514 */
515 static struct usb_driver xusb_driver = {
516 .name = "xpp_usb",
517 .probe = xusb_probe,
518 .disconnect = xusb_disconnect,
519 .id_table = xusb_table,
520 };
521
522 /*
523 * File operations needed when we register this driver.
524 * This assumes that this driver NEEDS file operations,
525 * of course, which means that the driver is expected
526 * to have a node in the /dev directory. If the USB
527 * device were for a network interface then the driver
528 * would use "struct net_driver" instead, and a serial
529 * device would use "struct tty_driver".
530 */
531 static const struct file_operations xusb_fops = {
532 /*
533 * The owner field is part of the module-locking
534 * mechanism. The idea is that the kernel knows
535 * which module to increment the use-counter of
536 * BEFORE it calls the device's open() function.
537 * This also means that the kernel can decrement
538 * the use-counter again before calling release()
539 * or should the open() function fail.
540 */
541 .owner = THIS_MODULE,
542 };
543
544 /*
545 * usb class driver info in order to get a minor number from the usb core,
546 * and to have the device registered with devfs and the driver core
547 */
548 static struct usb_class_driver xusb_class = {
549 .name = "usb/xpp_usb%d",
550 .fops = &xusb_fops,
551 .minor_base = USB_SKEL_MINOR_BASE,
552 };
553
554 /*
555 * Check that an endpoint's wMaxPacketSize attribute is 512. This
556 * indicates that it is a USB2's high speed end point.
557 *
558 * If it is 64, it means we have a USB1 controller. By default we do not
559 * support it and just fail the probe of the device. However if the user
560 * has set usb1=1, we continue and just put a notice.
561 *
562 * Returns true if all OK, false otherwise.
563 */
564 static int check_usb1(struct usb_endpoint_descriptor *endpoint)
565 {
566 const char *msg =
567 (usb_pipein(endpoint->bEndpointAddress)) ? "input" : "output";
568
569 if (endpoint->wMaxPacketSize >= sizeof(xpacket_t))
570 return 1;
571
572 if (usb1) {
573 NOTICE("USB1 endpoint detected: "
574 "USB %s endpoint 0x%X support only wMaxPacketSize=%d\n",
575 msg,
576 endpoint->bEndpointAddress,
577 endpoint->wMaxPacketSize);
578 return 1;
579 }
580 NOTICE("USB1 endpoint detected: "
581 "Device disabled. To enable: usb1=1, and read docs. "
582 "(%s, endpoint %d, size %d)\n",
583 msg, endpoint->bEndpointAddress, endpoint->wMaxPacketSize);
584 return 0;
585 }
586
587 /*
588 * set up the endpoint information
589 * check out the endpoints
590 * FIXME: Should be simplified (above 2.6.10) to use
591 * usb_dev->ep_in[0..16] and usb_dev->ep_out[0..16]
592 */
593 static int set_endpoints(xusb_t *xusb, struct usb_host_interface *iface_desc,
594 struct xusb_model_info *model_info)
595 {
596 struct usb_endpoint_descriptor *endpoint;
597 struct xusb_endpoint *xusb_ep;
598 int ep_addr;
599 int i;
600
601 #define BULK_ENDPOINT(ep) \
602 (((ep)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == \
603 USB_ENDPOINT_XFER_BULK)
604
605 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
606 endpoint = &iface_desc->endpoint[i].desc;
607 ep_addr = endpoint->bEndpointAddress;
608
609 if (!BULK_ENDPOINT(endpoint)) {
610 DBG(DEVICES,
611 "endpoint 0x%x is not bulk: mbAttributes=0x%X\n",
612 ep_addr, endpoint->bmAttributes);
613 continue;
614 }
615 if (usb_pipein(ep_addr)) { /* Input */
616 if (ep_addr == model_info->in.ep_addr) {
617 if (!check_usb1(endpoint))
618 return 0;
619 xusb_ep = &xusb->endpoints[XUSB_RECV];
620 xusb_ep->ep_addr = ep_addr;
621 xusb_ep->max_size = endpoint->wMaxPacketSize;
622 xusb_ep->callback = xpp_receive_callback;
623 }
624 } else { /* Output */
625 if (ep_addr == model_info->out.ep_addr) {
626 if (!check_usb1(endpoint))
627 return 0;
628 xusb_ep = &xusb->endpoints[XUSB_SEND];
629 xusb_ep->ep_addr = ep_addr;
630 xusb_ep->max_size = endpoint->wMaxPacketSize;
631 xusb_ep->callback = xpp_send_callback;
632 }
633 }
634 }
635 if (!xusb->endpoints[XUSB_RECV].ep_addr
636 || !xusb->endpoints[XUSB_SEND].ep_addr) {
637 XUSB_ERR(xusb, "Couldn't find bulk-in or bulk-out endpoints\n");
638 return 0;
639 }
640 DBG(DEVICES, "in=0x%02X out=0x%02X\n",
641 xusb->endpoints[XUSB_RECV].ep_addr,
642 xusb->endpoints[XUSB_SEND].ep_addr);
643 return 1;
644 }
645
646 /**
647 * xusb_probe
648 *
649 * Called by the usb core when a new device is connected that it thinks
650 * this driver might be interested in.
651 */
652 static int xusb_probe(struct usb_interface *interface,
653 const struct usb_device_id *id)
654 {
655 struct usb_device *udev = interface_to_usbdev(interface);
656 struct usb_host_interface *iface_desc =
657 usb_altnum_to_altsetting(interface, 0);
658 xusb_t *xusb = NULL;
659 struct xusb_model_info *model_info =
660 (struct xusb_model_info *)id->driver_info;
661 #ifdef CONFIG_PROC_FS
662 struct proc_dir_entry *procsummary = NULL;
663 #endif
664 xbus_t *xbus = NULL;
665 unsigned long flags;
666 int retval = -ENOMEM;
667 int i;
668
669 DBG(DEVICES, "New XUSB device MODEL=%s\n", model_info->desc);
670 if (iface_desc->desc.bInterfaceNumber != model_info->iface_num) {
671 DBG(DEVICES, "Skip interface #%d != #%d\n",
672 iface_desc->desc.bInterfaceNumber, model_info->iface_num);
673 return -ENODEV;
674 }
675 mutex_lock(&protect_xusb_devices);
676 if ((retval = usb_reset_device(udev)) < 0) {
677 ERR("usb_reset_device failed: %d\n", retval);
678 goto probe_failed;
679 }
680 if (!model_info) {
681 ERR("Missing endpoint setup for this device %d:%d\n",
682 udev->descriptor.idVendor, udev->descriptor.idProduct);
683 retval = -ENODEV;
684 goto probe_failed;
685 }
686
687 /* allocate memory for our device state and initialize it */
688 xusb = KZALLOC(sizeof(xusb_t), GFP_KERNEL);
689 if (xusb == NULL) {
690 ERR("xpp_usb: Unable to allocate new xpp usb bus\n");
691 retval = -ENOMEM;
692 goto probe_failed;
693 }
694 sema_init(&xusb->sem, 1);
695 atomic_set(&xusb->pending_writes, 0);
696 atomic_set(&xusb->pending_reads, 0);
697 atomic_set(&xusb->usb_sluggish_count, 0);
698 xusb->udev = udev;
699 xusb->interface = interface;
700 xusb->model_info = model_info;
701
702 if (!set_endpoints(xusb, iface_desc, model_info)) {
703 retval = -ENODEV;
704 goto probe_failed;
705 }
706 xusb->serial = udev->serial;
707 xusb->manufacturer = udev->manufacturer;
708 xusb->product = udev->product;
709 xusb->interface_name = iface_desc->string;
710 INFO("XUSB: %s -- %s -- %s\n", xusb->manufacturer, xusb->product,
711 xusb->interface_name);
712
713 /* allow device read, write and ioctl */
714 xusb->present = 1;
715
716 /* we can register the device now, as it is ready */
717 usb_set_intfdata(interface, xusb);
718 retval = usb_register_dev(interface, &xusb_class);
719 if (retval) {
720 /* something prevented us from registering this driver */
721 ERR("Not able to get a minor for this device.\n");
722 goto probe_failed;
723 }
724
725 xusb->minor = interface->minor;
726
727 /* let the user know what node this device is now attached to */
728 DBG(DEVICES, "USB XPP device now attached to minor %d\n", xusb->minor);
729 xbus =
730 xbus_new(&xusb_ops,
731 min(xusb->endpoints[XUSB_SEND].max_size,
732 xusb->endpoints[XUSB_RECV].max_size), &udev->dev,
733 xusb);
734 if (!xbus) {
735 retval = -ENOMEM;
736 goto probe_failed;
737 }
738 snprintf(xbus->transport.model_string,
739 ARRAY_SIZE(xbus->transport.model_string), "usb:%04x/%04x/%x",
740 udev->descriptor.idVendor, udev->descriptor.idProduct,
741 udev->descriptor.bcdDevice);
742 spin_lock_irqsave(&xusb_lock, flags);
743 for (i = 0; i < MAX_BUSES; i++) {
744 if (xusb_array[i] == NULL)
745 break;
746 }
747 spin_unlock_irqrestore(&xusb_lock, flags);
748 if (i >= MAX_BUSES) {
749 ERR("xpp_usb: Too many XPP USB buses\n");
750 retval = -ENOMEM;
751 goto probe_failed;
752 }
753 /* May trunacte... ignore */
754 usb_make_path(udev, xusb->path, XBUS_DESCLEN);
755 snprintf(xbus->connector, XBUS_DESCLEN, "%s", xusb->path);
756 if (xusb->serial && xusb->serial[0])
757 snprintf(xbus->label, LABEL_SIZE, "usb:%s", xusb->serial);
758 xusb->index = i;
759 xusb_array[i] = xusb;
760 XUSB_DBG(DEVICES, xusb, "GOT XPP USB BUS: %s\n", xbus->connector);
761
762 #ifdef CONFIG_PROC_FS
763 DBG(PROC,
764 "Creating proc entry " PROC_USBXPP_SUMMARY " in bus proc dir.\n");
765 procsummary = proc_create_data(PROC_USBXPP_SUMMARY, 0444,
766 xbus->proc_xbus_dir, &xusb_read_proc_ops,
767 xusb);
768 if (!procsummary) {
769 XBUS_ERR(xbus, "Failed to create proc file '%s'\n",
770 PROC_USBXPP_SUMMARY);
771 // FIXME: better error handling
772 retval = -EIO;
773 goto probe_failed;
774 }
775 #endif
776 bus_count++;
777 xusb->xbus_num = xbus->num;
778 /* prepare several pending frames for receive side */
779 for (i = 0; i < 10; i++)
780 xusb_listen(xusb);
781 xbus_connect(xbus);
782 mutex_unlock(&protect_xusb_devices);
783 return retval;
784 probe_failed:
785 ERR("Failed to initialize xpp usb bus: %d\n", retval);
786 usb_set_intfdata(interface, NULL);
787 if (xusb) {
788 if (xusb->minor) { /* passed registration phase */
789 ERR("Calling usb_deregister_dev()\n");
790 usb_deregister_dev(interface, &xusb_class);
791 }
792 ERR("Removing failed xusb\n");
793 KZFREE(xusb);
794 }
795 if (xbus) {
796 #ifdef CONFIG_PROC_FS
797 if (procsummary) {
798 XBUS_DBG(PROC, xbus,
799 "Remove proc_entry: " PROC_USBXPP_SUMMARY
800 "\n");
801 remove_proc_entry(PROC_USBXPP_SUMMARY,
802 xbus->proc_xbus_dir);
803 procsummary = NULL;
804 }
805 #endif
806 ERR("Calling xbus_disconnect()\n");
807 xbus_disconnect(xbus); // Blocking until fully deactivated!
808 }
809 mutex_unlock(&protect_xusb_devices);
810 return retval;
811 }
812
813 /**
814 * xusb_disconnect
815 *
816 * Called by the usb core when the device is removed from the system.
817 *
818 * This routine guarantees that the driver will not submit any more urbs
819 * by clearing dev->udev. It is also supposed to terminate any currently
820 * active urbs. Unfortunately, usb_bulk_msg(), used in xusb_read(), does
821 * not provide any way to do this. But at least we can cancel an active
822 * write.
823 */
824 static void xusb_disconnect(struct usb_interface *interface)
825 {
826 struct usb_host_interface *iface_desc =
827 usb_altnum_to_altsetting(interface, 0);
828 xusb_t *xusb;
829 xbus_t *xbus;
830 int i;
831
832 DBG(DEVICES, "CALLED on interface #%d\n",
833 iface_desc->desc.bInterfaceNumber);
834 /* prevent races with open() */
835 mutex_lock(&protect_xusb_devices);
836
837 xusb = usb_get_intfdata(interface);
838 usb_set_intfdata(interface, NULL);
839 xusb->present = 0;
840 xbus = xbus_num(xusb->xbus_num);
841
842 /* find our xusb */
843 for (i = 0; i < MAX_BUSES; i++) {
844 if (xusb_array[i] == xusb)
845 break;
846 }
847 BUG_ON(i >= MAX_BUSES);
848 xusb_array[i] = NULL;
849
850 #ifdef CONFIG_PROC_FS
851 if (xbus->proc_xbus_dir) {
852 XBUS_DBG(PROC, xbus,
853 "Remove proc_entry: " PROC_USBXPP_SUMMARY "\n");
854 remove_proc_entry(PROC_USBXPP_SUMMARY, xbus->proc_xbus_dir);
855 }
856 #endif
857 xbus_disconnect(xbus); // Blocking until fully deactivated!
858
859 down(&xusb->sem);
860
861 /* give back our minor */
862 usb_deregister_dev(interface, &xusb_class);
863
864 up(&xusb->sem);
865 DBG(DEVICES, "Semaphore released\n");
866 XUSB_INFO(xusb, "now disconnected\n");
867 KZFREE(xusb);
868
869 mutex_unlock(&protect_xusb_devices);
870 }
871
872 static void xpp_send_callback(USB_PASS_CB(urb))
873 {
874 struct uframe *uframe = urb_to_uframe(urb);
875 xframe_t *xframe = &uframe->xframe;
876 xusb_t *xusb = uframe->xusb;
877 xbus_t *xbus = xbus_num(xusb->xbus_num);
878 struct timeval now;
879 long usec;
880 int writes = atomic_read(&xusb->pending_writes);
881 int i;
882
883 if (!xbus) {
884 XUSB_ERR(xusb,
885 "Sent URB does not belong to a valid xbus...\n");
886 return;
887 }
888 //flip_parport_bit(6);
889 atomic_dec(&xusb->pending_writes);
890 do_gettimeofday(&now);
891 xusb->last_tx = xframe->tv_submitted;
892 usec = usec_diff(&now, &xframe->tv_submitted);
893 if (usec < 0)
894 usec = 0; /* System clock jumped */
895 if (usec > xusb->max_tx_delay)
896 xusb->max_tx_delay = usec;
897 i = usec / USEC_BUCKET;
898 if (i >= NUM_BUCKETS)
899 i = NUM_BUCKETS - 1;
900 xusb->usb_tx_delay[i]++;
901 if (unlikely(usec > tx_sluggish)) {
902 if (xusb->sluggish_debounce++ > drop_pcm_after) {
903 static int rate_limit;
904
905 /* skip first messages */
906 if ((rate_limit++ % 1003) == 500)
907 XUSB_NOTICE(xusb,
908 "Sluggish USB. Dropping next PCM frame "
909 "(pending_writes=%d)\n",
910 writes);
911 atomic_inc(&xusb->usb_sluggish_count);
912 xusb->drop_pcm = 1;
913 xusb->sluggish_debounce = 0;
914 }
915 } else {
916 xusb->sluggish_debounce = 0;
917 xusb->drop_pcm = 0;
918 }
919 /* sync/async unlink faults aren't errors */
920 if (urb->status
921 && !(urb->status == -ENOENT || urb->status == -ECONNRESET)) {
922 static int rate_limit;
923 if ((rate_limit++ % 1000) < 10) {
924 XUSB_ERR(xusb,
925 "nonzero write bulk status received: "
926 "%d (pending_writes=%d)\n",
927 urb->status, writes);
928 dump_xframe("usb-write-error", xbus, xframe, DBG_ANY);
929 }
930 XUSB_COUNTER(xusb, TX_ERRORS)++;
931 } else
932 XUSB_COUNTER(xusb, TX_FRAMES)++;
933 FREE_SEND_XFRAME(xbus, xframe);
934 if (!xusb->present)
935 XUSB_ERR(xusb, "A urb from non-connected device?\n");
936 }
937
938 static void xpp_receive_callback(USB_PASS_CB(urb))
939 {
940 struct uframe *uframe = urb_to_uframe(urb);
941 xframe_t *xframe = &uframe->xframe;
942 xusb_t *xusb = uframe->xusb;
943 xbus_t *xbus = xbus_num(xusb->xbus_num);
944 size_t size;
945 bool do_resubmit = 1;
946 struct timeval now;
947
948 do_gettimeofday(&now);
949 atomic_dec(&xusb->pending_reads);
950 if (!xbus) {
951 XUSB_ERR(xusb,
952 "Received URB does not belong to a valid xbus...\n");
953 return;
954 }
955 if (!xusb->present) {
956 do_resubmit = 0;
957 goto err;
958 }
959 if (urb->status) {
960 DBG(GENERAL, "nonzero read bulk status received: %d\n",
961 urb->status);
962 XUSB_COUNTER(xusb, RX_ERRORS)++;
963 goto err;
964 }
965 size = urb->actual_length;
966 if (size == 0) {
967 static int rate_limit;
968
969 if ((rate_limit++ % 5003) == 0)
970 XUSB_NOTICE(xusb, "Received a zero length URBs (%d)\n",
971 rate_limit);
972 XUSB_COUNTER(xusb, RCV_ZERO_LEN)++;
973 goto err;
974 }
975 atomic_set(&xframe->frame_len, size);
976 xframe->tv_received = now;
977
978 // if (debug)
979 // dump_xframe("USB_FRAME_RECEIVE", xbus, xframe, debug);
980 XUSB_COUNTER(xusb, RX_FRAMES)++;
981 if (xusb->drop_pcm) {
982 /* some protocol analysis */
983 static int rate_limit;
984 xpacket_t *pack = (xpacket_t *)(xframe->packets);
985 bool is_pcm = XPACKET_IS_PCM(pack);
986
987 if (is_pcm) {
988 if ((rate_limit++ % 1000) == 0)
989 XUSB_ERR(xusb,
990 "Sluggish USB: drop rx-pcm (%d)\n",
991 rate_limit);
992 /* Let trickle of RX-PCM, so Astribank will not reset */
993 if (sluggish_pcm_keepalive &&
994 ((rate_limit % sluggish_pcm_keepalive)
995 != 0)) {
996 XUSB_COUNTER(xusb, RX_DROPS)++;
997 goto err;
998 }
999 }
1000 }
1001 /* Send UP */
1002 xbus_receive_xframe(xbus, xframe);
1003 end:
1004 if (do_resubmit)
1005 xusb_listen(xusb);
1006 return;
1007 err:
1008 FREE_RECV_XFRAME(xbus, xframe);
1009 goto end;
1010 }
1011
1012 /*------------------------- Initialization -------------------------*/
1013
1014 static void xpp_usb_cleanup(void)
1015 {
1016 if (xusb_cache) {
1017 kmem_cache_destroy(xusb_cache);
1018 xusb_cache = NULL;
1019 }
1020 }
1021
1022 static int __init xpp_usb_init(void)
1023 {
1024 int ret;
1025 //xusb_t *xusb;
1026
1027 xusb_cache =
1028 kmem_cache_create("xusb_cache", sizeof(xframe_t) + XFRAME_DATASIZE,
1029 #if (LINUX_VERSION_CODE == KERNEL_VERSION(2, 6, 22)) && defined(CONFIG_SLUB)
1030 0, SLAB_STORE_USER,
1031 #else
1032 0, 0,
1033 #endif
1034 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 23)
1035 NULL,
1036 #endif
1037 NULL);
1038 if (!xusb_cache) {
1039 ret = -ENOMEM;
1040 goto failure;
1041 }
1042
1043 /* register this driver with the USB subsystem */
1044 ret = usb_register(&xusb_driver);
1045 if (ret) {
1046 ERR("usb_register failed. Error number %d\n", ret);
1047 goto failure;
1048 }
1049 return 0;
1050 failure:
1051 xpp_usb_cleanup();
1052 return ret;
1053 }
1054
1055 static void __exit xpp_usb_shutdown(void)
1056 {
1057 DBG(GENERAL, "\n");
1058 /* deregister this driver with the USB subsystem */
1059 usb_deregister(&xusb_driver);
1060 xpp_usb_cleanup();
1061 }
1062
1063 #ifdef CONFIG_PROC_FS
1064
1065 static int xusb_read_proc_show(struct seq_file *sfile, void *data)
1066 {
1067 unsigned long flags;
1068 int i;
1069 //unsigned long stamp = jiffies;
1070 xusb_t *xusb = sfile->private;
1071 uint usb_tx_delay[NUM_BUCKETS];
1072 const int mark_limit = tx_sluggish / USEC_BUCKET;
1073
1074 if (!xusb)
1075 return 0;
1076
1077 // TODO: probably needs a per-xusb lock:
1078 spin_lock_irqsave(&xusb_lock, flags);
1079 seq_printf(sfile, "Device: %03d/%03d\n", xusb->udev->bus->busnum,
1080 xusb->udev->devnum);
1081 seq_printf(sfile, "USB: manufacturer=%s\n", xusb->manufacturer);
1082 seq_printf(sfile, "USB: product=%s\n", xusb->product);
1083 seq_printf(sfile, "USB: serial=%s\n", xusb->serial);
1084 seq_printf(sfile, "Minor: %d\nModel Info: %s\n", xusb->minor,
1085 xusb->model_info->desc);
1086 seq_printf(sfile,
1087 "Endpoints:\n" "\tIn: 0x%02X - Size: %d)\n"
1088 "\tOut: 0x%02X - Size: %d)\n",
1089 xusb->endpoints[XUSB_RECV].ep_addr,
1090 xusb->endpoints[XUSB_RECV].max_size,
1091 xusb->endpoints[XUSB_SEND].ep_addr,
1092 xusb->endpoints[XUSB_SEND].max_size);
1093 seq_printf(sfile, "\npending_writes=%d\n",
1094 atomic_read(&xusb->pending_writes));
1095 seq_printf(sfile, "pending_reads=%d\n",
1096 atomic_read(&xusb->pending_reads));
1097 seq_printf(sfile, "max_tx_delay=%d\n", xusb->max_tx_delay);
1098 xusb->max_tx_delay = 0;
1099 #ifdef DEBUG_PCM_TIMING
1100 seq_printf(sfile,
1101 "\nstamp_last_pcm_read=%lld accumulate_diff=%lld\n",
1102 stamp_last_pcm_read, accumulate_diff);
1103 #endif
1104 memcpy(usb_tx_delay, xusb->usb_tx_delay, sizeof(usb_tx_delay));
1105 seq_printf(sfile, "usb_tx_delay[%dus - %dus]: ",
1106 USEC_BUCKET * BUCKET_START,
1107 USEC_BUCKET * NUM_BUCKETS);
1108 for (i = BUCKET_START; i < NUM_BUCKETS; i++) {
1109 seq_printf(sfile, "%6d ", usb_tx_delay[i]);
1110 if (i == mark_limit)
1111 seq_printf(sfile, "| ");
1112 }
1113 seq_printf(sfile, "\nSluggish events: %d\n",
1114 atomic_read(&xusb->usb_sluggish_count));
1115 seq_printf(sfile, "\nCOUNTERS:\n");
1116 for (i = 0; i < XUSB_COUNTER_MAX; i++) {
1117 seq_printf(sfile, "\t%-15s = %d\n", xusb_counters[i].name,
1118 xusb->counters[i]);
1119 }
1120 #if 0
1121 seq_printf(sfile, "<-- len=%d\n", len);
1122 #endif
1123 spin_unlock_irqrestore(&xusb_lock, flags);
1124 return 0;
1125 }
1126
1127 static int xusb_read_proc_open(struct inode *inode, struct file *file)
1128 {
1129 return single_open(file, xusb_read_proc_show, PDE_DATA(inode));
1130 }
1131
1132 static const struct file_operations xusb_read_proc_ops = {
1133 .owner = THIS_MODULE,
1134 .open = xusb_read_proc_open,
1135 .read = seq_read,
1136 .llseek = seq_lseek,
1137 .release = single_release,
1138 };
1139
1140
1141 #endif
1142
1143 MODULE_DESCRIPTION("XPP USB Transport Driver");
1144 MODULE_AUTHOR("Oron Peled <[email protected]>");
1145 MODULE_LICENSE("GPL");
1146
1147 module_init(xpp_usb_init);
1148 module_exit(xpp_usb_shutdown);