Debian Codegraph @

drivers/dahdi/xpp/xpp_usb.c

raw symbols SCIP
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 ktime_t 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 static void xpp_send_callback(struct urb *urb);
226 static void xpp_receive_callback(struct urb *urb);
227 static int xusb_probe(struct usb_interface *interface,
228 const struct usb_device_id *id);
229 static void xusb_disconnect(struct usb_interface *interface);
230 #ifdef CONFIG_PROC_FS
231 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 6, 0)
232 static const struct proc_ops xusb_read_proc_ops;
233 #else
234 static const struct file_operations xusb_read_proc_ops;
235 #endif
236 #endif
237
238 /*------------------------------------------------------------------*/
239
240 /*
241 * Updates the urb+xframe metadata from the uframe information.
242 */
243 static void uframe_recompute(struct uframe *uframe, enum xusb_dir dir)
244 {
245 struct urb *urb = &uframe->urb;
246 xusb_t *xusb = uframe->xusb;
247 struct usb_device *udev = xusb->udev;
248 struct xusb_endpoint *xusb_ep = &xusb->endpoints[dir];
249 unsigned int ep_addr = xusb_ep->ep_addr;
250 usb_complete_t urb_cb = xusb_ep->callback;
251 unsigned int epnum = ep_addr & USB_ENDPOINT_NUMBER_MASK;
252 int pipe = usb_pipein(ep_addr)
253 ? usb_rcvbulkpipe(udev, epnum)
254 : usb_sndbulkpipe(udev, epnum);
255
256 BUG_ON(uframe->uframe_magic != UFRAME_MAGIC);
257 usb_fill_bulk_urb(urb, udev, pipe, uframe->transfer_buffer,
258 uframe->transfer_buffer_length, urb_cb, uframe);
259 urb->transfer_flags = (URB_NO_TRANSFER_DMA_MAP);
260 }
261
262 static xframe_t *alloc_xframe(xbus_t *xbus, gfp_t gfp_flags)
263 {
264 struct uframe *uframe;
265 xusb_t *xusb;
266 void *p;
267 int size;
268 static int rate_limit;
269
270 BUG_ON(!xbus);
271 xusb = xusb_of(xbus);
272 BUG_ON(!xusb);
273 if (!xusb->present) {
274 if ((rate_limit++ % 1003) == 0)
275 XUSB_ERR(xusb,
276 "abort allocations during "
277 "device disconnect (%d)\n",
278 rate_limit);
279 return NULL;
280 }
281 size =
282 min(xusb->endpoints[XUSB_SEND].max_size,
283 xusb->endpoints[XUSB_RECV].max_size);
284 uframe = kmem_cache_alloc(xusb_cache, gfp_flags);
285 if (!uframe) {
286 if ((rate_limit++ % 1003) == 0)
287 XUSB_ERR(xusb, "frame allocation failed (%d)\n",
288 rate_limit);
289 return NULL;
290 }
291 usb_init_urb(&uframe->urb);
292 p = usb_alloc_coherent(xusb->udev, size, gfp_flags,
293 &uframe->urb.transfer_dma);
294 if (!p) {
295 if ((rate_limit++ % 1003) == 0)
296 XUSB_ERR(xusb, "buffer allocation failed (%d)\n",
297 rate_limit);
298 kmem_cache_free(xusb_cache, uframe);
299 return NULL;
300 }
301 uframe->uframe_magic = UFRAME_MAGIC;
302 uframe->transfer_buffer_length = size;
303 uframe->transfer_buffer = p;
304 uframe->xusb = xusb;
305 xframe_init(xbus, &uframe->xframe, uframe->transfer_buffer,
306 uframe->transfer_buffer_length, uframe);
307 return &uframe->xframe;
308 }
309
310 static void free_xframe(xbus_t *xbus, xframe_t *xframe)
311 {
312 struct uframe *uframe = xframe_to_uframe(xframe);
313 struct urb *urb = &uframe->urb;
314
315 BUG_ON(xbus->transport.priv != uframe->xusb);
316 //XUSB_INFO(uframe->xusb, "frame_free\n");
317 usb_free_coherent(urb->dev, uframe->transfer_buffer_length,
318 urb->transfer_buffer, urb->transfer_dma);
319 memset(uframe, 0, sizeof(*uframe));
320 kmem_cache_free(xusb_cache, uframe);
321 }
322
323 /*------------------------------------------------------------------*/
324
325 /*
326 * Actuall frame sending -- both PCM and commands.
327 */
328 static int do_send_xframe(xbus_t *xbus, xframe_t *xframe)
329 {
330 struct urb *urb;
331 xusb_t *xusb;
332 int ret = 0;
333 struct uframe *uframe;
334
335 BUG_ON(!xframe);
336 BUG_ON(xframe->xframe_magic != XFRAME_MAGIC);
337 xusb = xusb_of(xbus);
338 BUG_ON(!xusb);
339 if (!xusb->present) {
340 static int rate_limit;
341
342 if ((rate_limit++ % 1003) == 0)
343 XUSB_ERR(xusb,
344 "abort do_send_xframe during "
345 "device disconnect (%d)\n",
346 rate_limit);
347 ret = -ENODEV;
348 goto failure;
349 }
350 /*
351 * If something really bad happend, do not overflow the USB stack
352 */
353 if (atomic_read(&xusb->pending_writes) > MAX_PENDING_WRITES) {
354 static int rate_limit;
355
356 if ((rate_limit++ % 5000) == 0)
357 XUSB_ERR(xusb,
358 "USB device is totaly stuck. "
359 "Dropping packets (#%d).\n",
360 rate_limit);
361 ret = -ENODEV;
362 goto failure;
363 }
364 uframe = xframe->priv;
365 BUG_ON(!uframe);
366 BUG_ON(uframe->uframe_magic != UFRAME_MAGIC);
367 uframe_recompute(uframe, XUSB_SEND);
368 urb = &uframe->urb;
369 BUG_ON(!urb);
370 /* update urb length */
371 urb->transfer_buffer_length = XFRAME_LEN(xframe);
372 xframe->kt_submitted = ktime_get();
373 ret = usb_submit_urb(urb, GFP_ATOMIC);
374 if (ret < 0) {
375 static int rate_limit;
376
377 if ((rate_limit++ % 1000) == 0)
378 XBUS_ERR(xbus, "%s: usb_submit_urb failed: %d\n",
379 __func__, ret);
380 ret = -EBADF;
381 goto failure;
382 }
383 // if (debug)
384 // dump_xframe("USB_FRAME_SEND", xbus, xframe, debug);
385 atomic_inc(&xusb->pending_writes);
386 return 0;
387 failure:
388 XUSB_COUNTER(xusb, TX_ERRORS)++;
389 FREE_SEND_XFRAME(xbus, xframe); /* return to pool */
390 return ret;
391 }
392
393 /*
394 * PCM wrapper
395 */
396 static int xframe_send_pcm(xbus_t *xbus, xframe_t *xframe)
397 {
398 xusb_t *xusb;
399
400 BUG_ON(!xbus);
401 BUG_ON(!xframe);
402 xusb = xusb_of(xbus);
403 BUG_ON(!xusb);
404 if (xusb->drop_pcm) {
405 static int rate_limit;
406
407 if ((rate_limit++ % 1000) == 0)
408 XUSB_ERR(xusb, "Sluggish USB: drop tx-pcm (%d)\n",
409 rate_limit);
410 /* Let trickle of TX-PCM, so Astribank will not reset */
411 if (sluggish_pcm_keepalive &&
412 ((rate_limit % sluggish_pcm_keepalive) != 0)) {
413 XUSB_COUNTER(xusb, TX_DROPS)++;
414 goto err;
415 }
416 }
417 return do_send_xframe(xbus, xframe);
418 err:
419 FREE_SEND_XFRAME(xbus, xframe); /* return to pool */
420 return -EIO;
421 }
422
423 /*
424 * commands wrapper
425 */
426 static int xframe_send_cmd(xbus_t *xbus, xframe_t *xframe)
427 {
428 BUG_ON(!xbus);
429 BUG_ON(!xframe);
430 //XBUS_INFO(xbus, "%s:\n", __func__);
431 return do_send_xframe(xbus, xframe);
432 }
433
434 /*
435 * get a urb from the receive_pool and submit it on the read endpoint.
436 */
437 static bool xusb_listen(xusb_t *xusb)
438 {
439 xbus_t *xbus = xbus_num(xusb->xbus_num);
440 xframe_t *xframe;
441 struct uframe *uframe;
442 int ret = 0;
443
444 BUG_ON(!xbus);
445 xframe = ALLOC_RECV_XFRAME(xbus);
446 if (!xframe) {
447 XBUS_ERR(xbus, "Empty receive_pool\n");
448 goto out;
449 }
450 uframe = xframe_to_uframe(xframe);
451 uframe_recompute(uframe, XUSB_RECV);
452 ret = usb_submit_urb(&uframe->urb, GFP_ATOMIC);
453 if (ret < 0) {
454 static int rate_limit;
455
456 if ((rate_limit++ % 1000) == 0)
457 XBUS_ERR(xbus, "%s: usb_submit_urb failed: %d\n",
458 __func__, ret);
459 FREE_RECV_XFRAME(xbus, xframe);
460 goto out;
461 }
462 atomic_inc(&xusb->pending_reads);
463 ret = 1;
464 out:
465 return ret;
466 }
467
468 /*------------------------- XPP USB Bus Handling -------------------*/
469
470 enum XUSB_MODELS {
471 MODEL_FPGA_XPD
472 };
473
474 static const struct xusb_model_info {
475 const char *desc;
476 int iface_num;
477 struct xusb_endpoint in;
478 struct xusb_endpoint out;
479 } model_table[] = {
480 [MODEL_FPGA_XPD] = {
481 .iface_num = 0,
482 .in = { .ep_addr = 0x86 },
483 .out = { .ep_addr = 0x02 },
484 .desc = "FPGA_XPD"
485 },
486 };
487
488 /* table of devices that work with this driver */
489 static const struct usb_device_id xusb_table[] = {
490 /* FPGA_FXS */ {USB_DEVICE(0xE4E4, 0x1132),
491 .driver_info = (kernel_ulong_t)&model_table[MODEL_FPGA_XPD]},
492 /* FPGA_1141 */ {USB_DEVICE(0xE4E4, 0x1142),
493 .driver_info = (kernel_ulong_t)&model_table[MODEL_FPGA_XPD]},
494 /* FPGA_1151 */ {USB_DEVICE(0xE4E4, 0x1152),
495 .driver_info = (kernel_ulong_t)&model_table[MODEL_FPGA_XPD]},
496 /* FPGA_1161 */ {USB_DEVICE(0xE4E4, 0x1162),
497 .driver_info = (kernel_ulong_t)&model_table[MODEL_FPGA_XPD]},
498 /* Terminate */ {}
499 };
500
501 MODULE_DEVICE_TABLE(usb, xusb_table);
502
503 /*
504 * USB specific object needed to register this driver
505 * with the usb subsystem
506 */
507 static struct usb_driver xusb_driver = {
508 .name = "xpp_usb",
509 .probe = xusb_probe,
510 .disconnect = xusb_disconnect,
511 .id_table = xusb_table,
512 };
513
514 /*
515 * File operations needed when we register this driver.
516 * This assumes that this driver NEEDS file operations,
517 * of course, which means that the driver is expected
518 * to have a node in the /dev directory. If the USB
519 * device were for a network interface then the driver
520 * would use "struct net_driver" instead, and a serial
521 * device would use "struct tty_driver".
522 */
523 static const struct file_operations xusb_fops = {
524 /*
525 * The owner field is part of the module-locking
526 * mechanism. The idea is that the kernel knows
527 * which module to increment the use-counter of
528 * BEFORE it calls the device's open() function.
529 * This also means that the kernel can decrement
530 * the use-counter again before calling release()
531 * or should the open() function fail.
532 */
533 .owner = THIS_MODULE,
534 };
535
536 /*
537 * usb class driver info in order to get a minor number from the usb core,
538 * and to have the device registered with devfs and the driver core
539 */
540 static struct usb_class_driver xusb_class = {
541 .name = "usb/xpp_usb%d",
542 .fops = &xusb_fops,
543 .minor_base = USB_SKEL_MINOR_BASE,
544 };
545
546 /*
547 * Check that an endpoint's wMaxPacketSize attribute is 512. This
548 * indicates that it is a USB2's high speed end point.
549 *
550 * If it is 64, it means we have a USB1 controller. By default we do not
551 * support it and just fail the probe of the device. However if the user
552 * has set usb1=1, we continue and just put a notice.
553 *
554 * Returns true if all OK, false otherwise.
555 */
556 static int check_usb1(struct usb_endpoint_descriptor *endpoint)
557 {
558 const char *msg =
559 (usb_pipein(endpoint->bEndpointAddress)) ? "input" : "output";
560
561 if (endpoint->wMaxPacketSize >= sizeof(xpacket_t))
562 return 1;
563
564 if (usb1) {
565 NOTICE("USB1 endpoint detected: "
566 "USB %s endpoint 0x%X support only wMaxPacketSize=%d\n",
567 msg,
568 endpoint->bEndpointAddress,
569 endpoint->wMaxPacketSize);
570 return 1;
571 }
572 NOTICE("USB1 endpoint detected: "
573 "Device disabled. To enable: usb1=1, and read docs. "
574 "(%s, endpoint %d, size %d)\n",
575 msg, endpoint->bEndpointAddress, endpoint->wMaxPacketSize);
576 return 0;
577 }
578
579 /*
580 * set up the endpoint information
581 * check out the endpoints
582 * FIXME: Should be simplified (above 2.6.10) to use
583 * usb_dev->ep_in[0..16] and usb_dev->ep_out[0..16]
584 */
585 static int set_endpoints(xusb_t *xusb, struct usb_host_interface *iface_desc,
586 struct xusb_model_info *model_info)
587 {
588 struct usb_endpoint_descriptor *endpoint;
589 struct xusb_endpoint *xusb_ep;
590 int ep_addr;
591 int i;
592
593 #define BULK_ENDPOINT(ep) \
594 (((ep)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == \
595 USB_ENDPOINT_XFER_BULK)
596
597 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
598 endpoint = &iface_desc->endpoint[i].desc;
599 ep_addr = endpoint->bEndpointAddress;
600
601 if (!BULK_ENDPOINT(endpoint)) {
602 DBG(DEVICES,
603 "endpoint 0x%x is not bulk: mbAttributes=0x%X\n",
604 ep_addr, endpoint->bmAttributes);
605 continue;
606 }
607 if (usb_pipein(ep_addr)) { /* Input */
608 if (ep_addr == model_info->in.ep_addr) {
609 if (!check_usb1(endpoint))
610 return 0;
611 xusb_ep = &xusb->endpoints[XUSB_RECV];
612 xusb_ep->ep_addr = ep_addr;
613 xusb_ep->max_size = endpoint->wMaxPacketSize;
614 xusb_ep->callback = xpp_receive_callback;
615 }
616 } else { /* Output */
617 if (ep_addr == model_info->out.ep_addr) {
618 if (!check_usb1(endpoint))
619 return 0;
620 xusb_ep = &xusb->endpoints[XUSB_SEND];
621 xusb_ep->ep_addr = ep_addr;
622 xusb_ep->max_size = endpoint->wMaxPacketSize;
623 xusb_ep->callback = xpp_send_callback;
624 }
625 }
626 }
627 if (!xusb->endpoints[XUSB_RECV].ep_addr
628 || !xusb->endpoints[XUSB_SEND].ep_addr) {
629 XUSB_ERR(xusb, "Couldn't find bulk-in or bulk-out endpoints\n");
630 return 0;
631 }
632 DBG(DEVICES, "in=0x%02X out=0x%02X\n",
633 xusb->endpoints[XUSB_RECV].ep_addr,
634 xusb->endpoints[XUSB_SEND].ep_addr);
635 return 1;
636 }
637
638 /**
639 * xusb_probe
640 *
641 * Called by the usb core when a new device is connected that it thinks
642 * this driver might be interested in.
643 */
644 static int xusb_probe(struct usb_interface *interface,
645 const struct usb_device_id *id)
646 {
647 struct usb_device *udev = interface_to_usbdev(interface);
648 struct usb_host_interface *iface_desc =
649 usb_altnum_to_altsetting(interface, 0);
650 xusb_t *xusb = NULL;
651 struct xusb_model_info *model_info =
652 (struct xusb_model_info *)id->driver_info;
653 #ifdef CONFIG_PROC_FS
654 struct proc_dir_entry *procsummary = NULL;
655 #endif
656 xbus_t *xbus = NULL;
657 unsigned long flags;
658 int retval = -ENOMEM;
659 int i;
660
661 DBG(DEVICES, "New XUSB device MODEL=%s\n", model_info->desc);
662 if (iface_desc->desc.bInterfaceNumber != model_info->iface_num) {
663 DBG(DEVICES, "Skip interface #%d != #%d\n",
664 iface_desc->desc.bInterfaceNumber, model_info->iface_num);
665 return -ENODEV;
666 }
667 mutex_lock(&protect_xusb_devices);
668 if ((retval = usb_reset_device(udev)) < 0) {
669 ERR("usb_reset_device failed: %d\n", retval);
670 goto probe_failed;
671 }
672 if (!model_info) {
673 ERR("Missing endpoint setup for this device %d:%d\n",
674 udev->descriptor.idVendor, udev->descriptor.idProduct);
675 retval = -ENODEV;
676 goto probe_failed;
677 }
678
679 /* allocate memory for our device state and initialize it */
680 xusb = KZALLOC(sizeof(xusb_t), GFP_KERNEL);
681 if (xusb == NULL) {
682 ERR("xpp_usb: Unable to allocate new xpp usb bus\n");
683 retval = -ENOMEM;
684 goto probe_failed;
685 }
686 sema_init(&xusb->sem, 1);
687 atomic_set(&xusb->pending_writes, 0);
688 atomic_set(&xusb->pending_reads, 0);
689 atomic_set(&xusb->usb_sluggish_count, 0);
690 xusb->udev = udev;
691 xusb->interface = interface;
692 xusb->model_info = model_info;
693
694 if (!set_endpoints(xusb, iface_desc, model_info)) {
695 retval = -ENODEV;
696 goto probe_failed;
697 }
698 xusb->serial = udev->serial;
699 xusb->manufacturer = udev->manufacturer;
700 xusb->product = udev->product;
701 xusb->interface_name = iface_desc->string;
702 INFO("XUSB: %s -- %s -- %s\n", xusb->manufacturer, xusb->product,
703 xusb->interface_name);
704
705 /* allow device read, write and ioctl */
706 xusb->present = 1;
707
708 /* we can register the device now, as it is ready */
709 usb_set_intfdata(interface, xusb);
710 retval = usb_register_dev(interface, &xusb_class);
711 if (retval) {
712 /* something prevented us from registering this driver */
713 ERR("Not able to get a minor for this device.\n");
714 goto probe_failed;
715 }
716
717 xusb->minor = interface->minor;
718
719 /* let the user know what node this device is now attached to */
720 DBG(DEVICES, "USB XPP device now attached to minor %d\n", xusb->minor);
721 xbus =
722 xbus_new(&xusb_ops,
723 min(xusb->endpoints[XUSB_SEND].max_size,
724 xusb->endpoints[XUSB_RECV].max_size), &udev->dev,
725 xusb);
726 if (!xbus) {
727 retval = -ENOMEM;
728 goto probe_failed;
729 }
730 snprintf(xbus->transport.model_string,
731 ARRAY_SIZE(xbus->transport.model_string), "usb:%04x/%04x/%x",
732 udev->descriptor.idVendor, udev->descriptor.idProduct,
733 udev->descriptor.bcdDevice);
734 spin_lock_irqsave(&xusb_lock, flags);
735 for (i = 0; i < MAX_BUSES; i++) {
736 if (xusb_array[i] == NULL)
737 break;
738 }
739 spin_unlock_irqrestore(&xusb_lock, flags);
740 if (i >= MAX_BUSES) {
741 ERR("xpp_usb: Too many XPP USB buses\n");
742 retval = -ENOMEM;
743 goto probe_failed;
744 }
745 /* May trunacte... ignore */
746 usb_make_path(udev, xusb->path, XBUS_DESCLEN);
747 snprintf(xbus->connector, XBUS_DESCLEN, "%s", xusb->path);
748 if (xusb->serial && xusb->serial[0])
749 snprintf(xbus->label, LABEL_SIZE, "usb:%s", xusb->serial);
750 xusb->index = i;
751 xusb_array[i] = xusb;
752 XUSB_DBG(DEVICES, xusb, "GOT XPP USB BUS: %s\n", xbus->connector);
753
754 #ifdef CONFIG_PROC_FS
755 DBG(PROC,
756 "Creating proc entry " PROC_USBXPP_SUMMARY " in bus proc dir.\n");
757 procsummary = proc_create_data(PROC_USBXPP_SUMMARY, 0444,
758 xbus->proc_xbus_dir, &xusb_read_proc_ops,
759 xusb);
760 if (!procsummary) {
761 XBUS_ERR(xbus, "Failed to create proc file '%s'\n",
762 PROC_USBXPP_SUMMARY);
763 // FIXME: better error handling
764 retval = -EIO;
765 goto probe_failed;
766 }
767 #endif
768 bus_count++;
769 xusb->xbus_num = xbus->num;
770 /* prepare several pending frames for receive side */
771 for (i = 0; i < 10; i++)
772 xusb_listen(xusb);
773 xbus_connect(xbus);
774 mutex_unlock(&protect_xusb_devices);
775 return retval;
776 probe_failed:
777 ERR("Failed to initialize xpp usb bus: %d\n", retval);
778 usb_set_intfdata(interface, NULL);
779 if (xusb) {
780 if (xusb->minor) { /* passed registration phase */
781 ERR("Calling usb_deregister_dev()\n");
782 usb_deregister_dev(interface, &xusb_class);
783 }
784 ERR("Removing failed xusb\n");
785 KZFREE(xusb);
786 }
787 if (xbus) {
788 #ifdef CONFIG_PROC_FS
789 if (procsummary) {
790 XBUS_DBG(PROC, xbus,
791 "Remove proc_entry: " PROC_USBXPP_SUMMARY
792 "\n");
793 remove_proc_entry(PROC_USBXPP_SUMMARY,
794 xbus->proc_xbus_dir);
795 procsummary = NULL;
796 }
797 #endif
798 ERR("Calling xbus_disconnect()\n");
799 xbus_disconnect(xbus); // Blocking until fully deactivated!
800 }
801 mutex_unlock(&protect_xusb_devices);
802 return retval;
803 }
804
805 /**
806 * xusb_disconnect
807 *
808 * Called by the usb core when the device is removed from the system.
809 *
810 * This routine guarantees that the driver will not submit any more urbs
811 * by clearing dev->udev. It is also supposed to terminate any currently
812 * active urbs. Unfortunately, usb_bulk_msg(), used in xusb_read(), does
813 * not provide any way to do this. But at least we can cancel an active
814 * write.
815 */
816 static void xusb_disconnect(struct usb_interface *interface)
817 {
818 struct usb_host_interface *iface_desc =
819 usb_altnum_to_altsetting(interface, 0);
820 xusb_t *xusb;
821 xbus_t *xbus;
822 int i;
823
824 DBG(DEVICES, "CALLED on interface #%d\n",
825 iface_desc->desc.bInterfaceNumber);
826 /* prevent races with open() */
827 mutex_lock(&protect_xusb_devices);
828
829 xusb = usb_get_intfdata(interface);
830 usb_set_intfdata(interface, NULL);
831 xusb->present = 0;
832 xbus = xbus_num(xusb->xbus_num);
833
834 /* find our xusb */
835 for (i = 0; i < MAX_BUSES; i++) {
836 if (xusb_array[i] == xusb)
837 break;
838 }
839 BUG_ON(i >= MAX_BUSES);
840 xusb_array[i] = NULL;
841
842 #ifdef CONFIG_PROC_FS
843 if (xbus->proc_xbus_dir) {
844 XBUS_DBG(PROC, xbus,
845 "Remove proc_entry: " PROC_USBXPP_SUMMARY "\n");
846 remove_proc_entry(PROC_USBXPP_SUMMARY, xbus->proc_xbus_dir);
847 }
848 #endif
849 xbus_disconnect(xbus); // Blocking until fully deactivated!
850
851 down(&xusb->sem);
852
853 /* give back our minor */
854 usb_deregister_dev(interface, &xusb_class);
855
856 up(&xusb->sem);
857 DBG(DEVICES, "Semaphore released\n");
858 XUSB_INFO(xusb, "now disconnected\n");
859 KZFREE(xusb);
860
861 mutex_unlock(&protect_xusb_devices);
862 }
863
864 static void xpp_send_callback(struct urb *urb)
865 {
866 struct uframe *uframe = urb_to_uframe(urb);
867 xframe_t *xframe = &uframe->xframe;
868 xusb_t *xusb = uframe->xusb;
869 xbus_t *xbus = xbus_num(xusb->xbus_num);
870 ktime_t now;
871 s64 usec;
872 int writes = atomic_read(&xusb->pending_writes);
873 int i;
874
875 if (!xbus) {
876 XUSB_ERR(xusb,
877 "Sent URB does not belong to a valid xbus...\n");
878 return;
879 }
880 //flip_parport_bit(6);
881 atomic_dec(&xusb->pending_writes);
882 now = ktime_get();
883 xusb->last_tx = xframe->kt_submitted;
884 usec = ktime_us_delta(now, xframe->kt_submitted);
885 if (usec < 0)
886 usec = 0; /* System clock jumped */
887 if (usec > xusb->max_tx_delay)
888 xusb->max_tx_delay = usec;
889 i = div_s64(usec, USEC_BUCKET);
890 if (i >= NUM_BUCKETS)
891 i = NUM_BUCKETS - 1;
892 xusb->usb_tx_delay[i]++;
893 if (unlikely(usec > tx_sluggish)) {
894 if (xusb->sluggish_debounce++ > drop_pcm_after) {
895 static int rate_limit;
896
897 /* skip first messages */
898 if ((rate_limit++ % 1003) == 500)
899 XUSB_NOTICE(xusb,
900 "Sluggish USB. Dropping next PCM frame "
901 "(pending_writes=%d)\n",
902 writes);
903 atomic_inc(&xusb->usb_sluggish_count);
904 xusb->drop_pcm = 1;
905 xusb->sluggish_debounce = 0;
906 }
907 } else {
908 xusb->sluggish_debounce = 0;
909 xusb->drop_pcm = 0;
910 }
911 /* sync/async unlink faults aren't errors */
912 if (urb->status
913 && !(urb->status == -ENOENT || urb->status == -ECONNRESET)) {
914 static int rate_limit;
915 if ((rate_limit++ % 1000) < 10) {
916 XUSB_ERR(xusb,
917 "nonzero write bulk status received: "
918 "%d (pending_writes=%d)\n",
919 urb->status, writes);
920 dump_xframe("usb-write-error", xbus, xframe, DBG_ANY);
921 }
922 XUSB_COUNTER(xusb, TX_ERRORS)++;
923 } else
924 XUSB_COUNTER(xusb, TX_FRAMES)++;
925 FREE_SEND_XFRAME(xbus, xframe);
926 if (!xusb->present)
927 XUSB_ERR(xusb, "A urb from non-connected device?\n");
928 }
929
930 static void xpp_receive_callback(struct urb *urb)
931 {
932 struct uframe *uframe = urb_to_uframe(urb);
933 xframe_t *xframe = &uframe->xframe;
934 xusb_t *xusb = uframe->xusb;
935 xbus_t *xbus = xbus_num(xusb->xbus_num);
936 size_t size;
937 bool do_resubmit = 1;
938 ktime_t now = ktime_get();
939
940 atomic_dec(&xusb->pending_reads);
941 if (!xbus) {
942 XUSB_ERR(xusb,
943 "Received URB does not belong to a valid xbus...\n");
944 return;
945 }
946 if (!xusb->present) {
947 do_resubmit = 0;
948 goto err;
949 }
950 if (urb->status) {
951 DBG(GENERAL, "nonzero read bulk status received: %d\n",
952 urb->status);
953 XUSB_COUNTER(xusb, RX_ERRORS)++;
954 goto err;
955 }
956 size = urb->actual_length;
957 if (size == 0) {
958 static int rate_limit;
959
960 if ((rate_limit++ % 5003) == 0)
961 XUSB_NOTICE(xusb, "Received a zero length URBs (%d)\n",
962 rate_limit);
963 XUSB_COUNTER(xusb, RCV_ZERO_LEN)++;
964 goto err;
965 }
966 atomic_set(&xframe->frame_len, size);
967 xframe->kt_received = now;
968
969 // if (debug)
970 // dump_xframe("USB_FRAME_RECEIVE", xbus, xframe, debug);
971 XUSB_COUNTER(xusb, RX_FRAMES)++;
972 if (xusb->drop_pcm) {
973 /* some protocol analysis */
974 static int rate_limit;
975 xpacket_t *pack = (xpacket_t *)(xframe->packets);
976 bool is_pcm = XPACKET_IS_PCM(pack);
977
978 if (is_pcm) {
979 if ((rate_limit++ % 1000) == 0)
980 XUSB_ERR(xusb,
981 "Sluggish USB: drop rx-pcm (%d)\n",
982 rate_limit);
983 /* Let trickle of RX-PCM, so Astribank will not reset */
984 if (sluggish_pcm_keepalive &&
985 ((rate_limit % sluggish_pcm_keepalive)
986 != 0)) {
987 XUSB_COUNTER(xusb, RX_DROPS)++;
988 goto err;
989 }
990 }
991 }
992 /* Send UP */
993 xbus_receive_xframe(xbus, xframe);
994 end:
995 if (do_resubmit)
996 xusb_listen(xusb);
997 return;
998 err:
999 FREE_RECV_XFRAME(xbus, xframe);
1000 goto end;
1001 }
1002
1003 /*------------------------- Initialization -------------------------*/
1004
1005 static void xpp_usb_cleanup(void)
1006 {
1007 if (xusb_cache) {
1008 kmem_cache_destroy(xusb_cache);
1009 xusb_cache = NULL;
1010 }
1011 }
1012
1013 static int __init xpp_usb_init(void)
1014 {
1015 int ret;
1016 //xusb_t *xusb;
1017
1018 xusb_cache =
1019 kmem_cache_create("xusb_cache", sizeof(xframe_t) + XFRAME_DATASIZE,
1020 0, 0, NULL);
1021 if (!xusb_cache) {
1022 ret = -ENOMEM;
1023 goto failure;
1024 }
1025
1026 /* register this driver with the USB subsystem */
1027 ret = usb_register(&xusb_driver);
1028 if (ret) {
1029 ERR("usb_register failed. Error number %d\n", ret);
1030 goto failure;
1031 }
1032 return 0;
1033 failure:
1034 xpp_usb_cleanup();
1035 return ret;
1036 }
1037
1038 static void __exit xpp_usb_shutdown(void)
1039 {
1040 DBG(GENERAL, "\n");
1041 /* deregister this driver with the USB subsystem */
1042 usb_deregister(&xusb_driver);
1043 xpp_usb_cleanup();
1044 }
1045
1046 #ifdef CONFIG_PROC_FS
1047
1048 static int xusb_read_proc_show(struct seq_file *sfile, void *data)
1049 {
1050 unsigned long flags;
1051 int i;
1052 //unsigned long stamp = jiffies;
1053 xusb_t *xusb = sfile->private;
1054 uint usb_tx_delay[NUM_BUCKETS];
1055 const int mark_limit = tx_sluggish / USEC_BUCKET;
1056
1057 if (!xusb)
1058 return 0;
1059
1060 // TODO: probably needs a per-xusb lock:
1061 spin_lock_irqsave(&xusb_lock, flags);
1062 seq_printf(sfile, "Device: %03d/%03d\n", xusb->udev->bus->busnum,
1063 xusb->udev->devnum);
1064 seq_printf(sfile, "USB: manufacturer=%s\n", xusb->manufacturer);
1065 seq_printf(sfile, "USB: product=%s\n", xusb->product);
1066 seq_printf(sfile, "USB: serial=%s\n", xusb->serial);
1067 seq_printf(sfile, "Minor: %d\nModel Info: %s\n", xusb->minor,
1068 xusb->model_info->desc);
1069 seq_printf(sfile,
1070 "Endpoints:\n" "\tIn: 0x%02X - Size: %d)\n"
1071 "\tOut: 0x%02X - Size: %d)\n",
1072 xusb->endpoints[XUSB_RECV].ep_addr,
1073 xusb->endpoints[XUSB_RECV].max_size,
1074 xusb->endpoints[XUSB_SEND].ep_addr,
1075 xusb->endpoints[XUSB_SEND].max_size);
1076 seq_printf(sfile, "\npending_writes=%d\n",
1077 atomic_read(&xusb->pending_writes));
1078 seq_printf(sfile, "pending_reads=%d\n",
1079 atomic_read(&xusb->pending_reads));
1080 seq_printf(sfile, "max_tx_delay=%d\n", xusb->max_tx_delay);
1081 xusb->max_tx_delay = 0;
1082 #ifdef DEBUG_PCM_TIMING
1083 seq_printf(sfile,
1084 "\nstamp_last_pcm_read=%lld accumulate_diff=%lld\n",
1085 stamp_last_pcm_read, accumulate_diff);
1086 #endif
1087 memcpy(usb_tx_delay, xusb->usb_tx_delay, sizeof(usb_tx_delay));
1088 seq_printf(sfile, "usb_tx_delay[%dus - %dus]: ",
1089 USEC_BUCKET * BUCKET_START,
1090 USEC_BUCKET * NUM_BUCKETS);
1091 for (i = BUCKET_START; i < NUM_BUCKETS; i++) {
1092 seq_printf(sfile, "%6d ", usb_tx_delay[i]);
1093 if (i == mark_limit)
1094 seq_printf(sfile, "| ");
1095 }
1096 seq_printf(sfile, "\nSluggish events: %d\n",
1097 atomic_read(&xusb->usb_sluggish_count));
1098 seq_printf(sfile, "\nCOUNTERS:\n");
1099 for (i = 0; i < XUSB_COUNTER_MAX; i++) {
1100 seq_printf(sfile, "\t%-15s = %d\n", xusb_counters[i].name,
1101 xusb->counters[i]);
1102 }
1103 #if 0
1104 seq_printf(sfile, "<-- len=%d\n", len);
1105 #endif
1106 spin_unlock_irqrestore(&xusb_lock, flags);
1107 return 0;
1108 }
1109
1110 static int xusb_read_proc_open(struct inode *inode, struct file *file)
1111 {
1112 return single_open(file, xusb_read_proc_show, PDE_DATA(inode));
1113 }
1114
1115 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 6, 0)
1116 static const struct proc_ops xusb_read_proc_ops = {
1117 .proc_open = xusb_read_proc_open,
1118 .proc_read = seq_read,
1119 .proc_lseek = seq_lseek,
1120 .proc_release = single_release,
1121 };
1122 #else
1123 static const struct file_operations xusb_read_proc_ops = {
1124 .owner = THIS_MODULE,
1125 .open = xusb_read_proc_open,
1126 .read = seq_read,
1127 .llseek = seq_lseek,
1128 .release = single_release,
1129 };
1130 #endif
1131
1132
1133 #endif
1134
1135 MODULE_DESCRIPTION("XPP USB Transport Driver");
1136 MODULE_AUTHOR("Oron Peled <[email protected]>");
1137 MODULE_LICENSE("GPL");
1138
1139 module_init(xpp_usb_init);
1140 module_exit(xpp_usb_shutdown);