2
* Written by Oron Peled <oron@actcom.co.il>
3
* Copyright (C) 2004-2006, Xorcom
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.
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.
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.
22
#include <linux/version.h>
24
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
25
# warning "This module is tested only with 2.6 kernels"
28
#include <linux/kernel.h>
29
#include <linux/errno.h>
30
#include <linux/module.h>
31
#include <linux/init.h>
32
#include <linux/errno.h>
33
#include <linux/interrupt.h>
34
#include <linux/delay.h> /* for udelay */
35
#include <linux/seq_file.h>
36
#include <asm/uaccess.h>
37
#include <asm/atomic.h>
38
#include <asm/timex.h>
39
#include <linux/proc_fs.h>
40
#include <linux/usb.h>
41
#include <version.h> /* For zaptel version */
44
#include "xbus-core.h"
50
static const char rcsid[] = "$Id: xpp_usb.c 1457 2006-09-09 15:24:12Z tzafrir $";
52
DEF_PARM(int, print_dbg, 0, "Print DBG statements"); /* must be before zap_debug.h */
54
#include "zap_debug.h"
56
/* FIXME: A flag that was deprecated at some point, and rather useless */
57
/* anyway. Only used in the code or-ed to other flags */
58
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,14)
59
# define URB_ASYNC_UNLINK 0
62
/* Get a minor range for your devices from the usb maintainer */
63
#define USB_SKEL_MINOR_BASE 192
66
#define PROC_USBXPP_SUMMARY "xpp_usb"
69
#ifdef DEBUG_PCM_TIMING
70
static cycles_t stamp_last_pcm_read;
71
static cycles_t accumulate_diff;
74
struct xusb_model_info;
76
struct xusb_endpoint {
79
usb_complete_t callback;
82
static int xusb_packet_send(xbus_t *xbus, xpacket_t *pack);
84
xbus_ops_t xusb_ops = {
85
.packet_send = xusb_packet_send,
86
.packet_new = NULL, // Default allocator
87
.packet_free = NULL, // Default deallocator
99
#define XUSB_COUNTER(xusb, counter) ((xusb)->counters[XUSB_N_ ## counter])
101
#define C_(x) [ XUSB_N_ ## x ] = { #x }
103
static struct xusb_counters {
105
} xusb_counters[] = {
116
#define XUSB_COUNTER_MAX ARRAY_SIZE(xusb_counters)
118
#define MAX_PENDING_WRITES 100
126
* USB XPP Bus (a USB Device)
128
typedef struct xpp_usb_bus {
130
struct usb_device *udev; /* save off the usb device pointer */
131
struct usb_interface *interface; /* the interface for this device */
132
unsigned char minor; /* the starting minor number for this device */
134
struct xusb_model_info *model_info;
135
struct xusb_endpoint endpoints[2]; /* RECV/SEND endpoints */
137
struct urb *read_urb;
139
struct completion write_finished; /* wait for the write to finish */
141
int present; /* if the device is not disconnected */
142
int reading; /* is the read_urb reading (listening) */
143
atomic_t pending_writes; /* submited but not out yet */
144
struct semaphore sem; /* locks this structure */
145
int counters[XUSB_COUNTER_MAX];
148
static spinlock_t xusb_lock = SPIN_LOCK_UNLOCKED;
149
static xusb_t *xusb_array[USBDEV_MAX] = {};
150
static unsigned bus_count = 0;
153
/* prevent races between open() and disconnect() */
154
static DECLARE_MUTEX (disconnect_sem);
157
* Function Prototypes
160
static ssize_t xusb_read (struct file *file, char *buffer, size_t count, loff_t *ppos);
161
static ssize_t xusb_write (struct file *file, const char *buffer, size_t count, loff_t *ppos);
162
static int xusb_ioctl (struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg);
163
static int xusb_open (struct inode *inode, struct file *file);
164
static int xusb_release (struct inode *inode, struct file *file);
165
static void xusb_write_bulk_callback (struct urb *urb, struct pt_regs *regs);
167
static void xpp_urb_delete(struct urb *urb);
168
static struct urb *xpp_urb_new(xusb_t *dev, enum xusb_dir dir, size_t size);
169
static void xpp_send_callback(struct urb *urb, struct pt_regs *regs);
170
static void xpp_receive_callback(struct urb *urb, struct pt_regs *regs);
172
static int xusb_probe (struct usb_interface *interface, const struct usb_device_id *id);
173
static void xusb_disconnect (struct usb_interface *interface);
174
static int xusb_read_proc(char *page, char **start, off_t off, int count, int *eof, void *data);
176
/*------------------------------------------------------------------*/
180
* Allocates a new XPP packet.
181
* @xbus The XPP bus in which the packet will flow (for counters
183
* @flags Flags for kernel memory allocation.
184
* @returns A pointer to the new packet, or NULL in case of failure.
187
* Packet allocation/deallocation:
189
* - Allocated by protocol commands
190
* - Deallocated by xmus_xmitter
192
* - Allocated/deallocated by xbus_xmiter
194
xpacket_t *xusb_packet_new(xbus_t *xbus, int flags)
198
/* To avoid races we increament counter in advance and decrement it later
199
* in case of failure */
200
atomic_inc(&xbus->packet_counter);
201
//DBG("Incremented packet_counter of bus %s (new packet) to %d\n",
202
// xbus->busname, atomic_read(&xbus->packet_counter));
203
pack = kmem_cache_alloc(packet_cache, flags);
205
memset(pack, 0, sizeof(xpacket_t));
206
atomic_inc(&xpacket_count);
208
atomic_dec(&xbus->packet_counter);
209
//DBG("Decremented packet_counter of bus %s (failed new packet) to %d\n",
210
// xbus->busname, atomic_read(&xbus->packet_counter));
215
void xusb_packet_free(xbus_t *xbus, xpacket_t *p)
217
kmem_cache_free(packet_cache, p);
218
atomic_dec(&xpacket_count);
219
atomic_dec(&xbus->packet_counter);
220
//DBG("Decremented packet_counter of bus %s (freed packet) to %d\n",
221
// xbus->busname, atomic_read(&xbus->packet_counter));
228
#define packet_debug(m, x, p)
232
static void packet_debug(const char msg[], xusb_t *xusb, xpacket_t *pack)
234
char title[XBUS_DESCLEN];
236
if(pack->content.opcode == XPROTO_NAME(GLOBAL,PCM_READ)) {
237
#ifdef DEBUG_PCM_TIMING
239
* DEBUG: high-res timing of PCM_READ to PCM_WRITE
241
stamp_last_pcm_read = get_cycles();
244
// fill_beep((u_char *)&PACKET_FIELD(pack, PCM_READS, pcm), 2); // Debugging BEEP
245
static int rate_limit;
246
if((rate_limit++ % 1000) < 10)
247
dump_packet("USB RECEIVE PCM", pack, print_dbg);
250
} else if(pack->content.opcode == XPROTO_NAME(GLOBAL,PCM_WRITE)) {
251
#ifdef DEBUG_PCM_TIMING
253
* DEBUG: high-res timing of PCM_READ to PCM_WRITE
255
cycles_t diff = get_cycles() - stamp_last_pcm_read;
256
accumulate_diff += diff;
259
static int rate_limit;
260
if((rate_limit++ % 1000) < 10)
261
dump_packet("USB SEND PCM", pack, print_dbg);
264
} else if(pack->content.opcode == XPROTO_NAME(FXS, SLIC_WRITE)) {
267
sc = &RPACKET_FIELD(pack, FXS, SLIC_WRITE, slic_cmd);
268
if(sc->bytes == 2 && sc->content.direct.reg_num == 0x06 && sc->content.direct.read) /* ignore SLIC_QUERY */
270
if(sc->bytes == 2 && sc->content.direct.reg_num == DAA_VBAT_REGISTER && sc->content.direct.read) /* ignore DAA_QUERY */
272
} else if(pack->content.opcode == XPROTO_NAME(FXS, SLIC_REPLY)) {
275
snprintf(title, XBUS_DESCLEN, "%s: %s", msg, xusb->xbus->busname);
276
dump_packet(title, pack, print_dbg);
280
static int xusb_packet_send(xbus_t *xbus, xpacket_t *pack)
286
struct xusb_endpoint *xusb_ep;
293
NOTICE("tried to send packets to non-exitant USB device. Ignored\n");
298
* If something really bad happend, do not overflow the USB stack
300
if(atomic_read(&xusb->pending_writes) > MAX_PENDING_WRITES) {
301
static int rate_limit;
303
if((rate_limit++ % 1000) < 10)
304
ERR("%s: %s: more than %d pending writes. Dropping.\n", __FUNCTION__, xbus->busname, MAX_PENDING_WRITES);
308
size = PACKET_LEN(pack);
309
xusb_ep = &xusb->endpoints[XUSB_SEND];
310
urb = xpp_urb_new(xusb, XUSB_SEND, size);
312
ERR("No free urbs available\n");
316
packet_debug("USB_PACKET_SEND", xusb, pack);
318
/* FIXME: FIXME: FIXME: we use copy+free until low-level drivers allocate memory themselves */
319
memcpy(urb->transfer_buffer, &pack->content, size);
321
ret = usb_submit_urb(urb, GFP_ATOMIC);
323
static int rate_limit;
325
if((rate_limit++ % 1000) < 5)
326
ERR("%s: failed submit_urb: %d\n", __FUNCTION__, ret);
331
atomic_inc(&xusb->pending_writes);
332
if(pack->content.opcode == XPROTO_NAME(GLOBAL,PCM_WRITE))
333
XUSB_COUNTER(xusb, PCM_WRITES)++;
335
xbus->ops->packet_free(xbus, pack); // FIXME: eventually will be done in the urb callback
337
XUSB_COUNTER(xusb, TX_ERRORS)++;
341
static void xpp_urb_delete(struct urb *urb)
344
// DBG("%s: (%d) %p %X", __FUNCTION__, urb->transfer_buffer_length, urb->transfer_buffer, urb->transfer_dma);
345
usb_buffer_free (urb->dev, urb->transfer_buffer_length,
346
urb->transfer_buffer,
351
static struct urb *xpp_urb_new(xusb_t *xusb, enum xusb_dir dir, size_t size)
353
struct usb_device *udev = xusb->udev;
354
struct xusb_endpoint *xusb_ep = &xusb->endpoints[dir];
355
unsigned int ep_addr = xusb_ep->ep_addr;
356
usb_complete_t urb_cb = xusb_ep->callback;
358
unsigned char *buffer; /* the buffer to send data */
359
unsigned int epnum = ep_addr & USB_ENDPOINT_NUMBER_MASK;
360
int pipe = usb_pipein(ep_addr)
361
? usb_rcvbulkpipe(udev, epnum)
362
: usb_sndbulkpipe(udev, epnum);
364
if(size > xusb_ep->max_size)
366
urb = usb_alloc_urb(0, GFP_ATOMIC);
368
err("No free urbs available");
372
/* on some platforms using this kind of buffer alloc
373
* call eliminates a dma "bounce buffer".
375
* NOTE: you'd normally want i/o buffers that hold
376
* more than one packet, so that i/o delays between
377
* packets don't hurt throughput. (Probably applies only to isochronous
380
urb->transfer_flags = (URB_NO_TRANSFER_DMA_MAP | URB_ASYNC_UNLINK);
381
buffer = usb_buffer_alloc(udev, size, GFP_ATOMIC, &urb->transfer_dma);
382
// DBG("(%d) %p / %x", size, buffer, urb->transfer_dma);
384
err("Couldn't allocate buffer");
388
usb_fill_bulk_urb(urb, udev, pipe, buffer, size, urb_cb, xusb);
392
/*------------------------- XPP USB Bus Handling -------------------*/
394
#define XUSB_MODEL(ep_in,ep_out,type,str) \
396
.in = { .ep_addr = (ep_in) }, \
397
.out = { .ep_addr = (ep_out) }, \
398
.bus_type = (type), \
402
static const struct xusb_model_info {
404
struct xusb_endpoint in;
405
struct xusb_endpoint out;
406
xbus_type_t bus_type;
408
XUSB_MODEL(0x86, 0x02, FIRMWARE_LOOPBACK, "bulkloop.hex"),
409
XUSB_MODEL(0x86, 0x02, FIRMWARE_LOOPBACK, "FPGA_bulkloop.hex"),
410
XUSB_MODEL(0x86, 0x02, FIRMWARE_XPP, "FPGA_XPD.hex"),
413
/* table of devices that work with this driver */
414
static const struct usb_device_id xusb_table [] = {
415
// { USB_DEVICE(0x04B4, 0x8613) }, // default of cypress
416
{ USB_DEVICE(0xE4E4, 0x2211), .driver_info=(kernel_ulong_t)&model_table[2] }, // FPGA_XPD.hex
417
{ USB_DEVICE(0xE4E4, 0x1132), .driver_info=(kernel_ulong_t)&model_table[2] }, // FPGA_XPD.hex
418
/* "Gadget Zero" firmware runs under Linux */
419
//{ USB_DEVICE(0x0525, 0xa4a0) },
420
{ } /* Terminating entry */
423
MODULE_DEVICE_TABLE (usb, xusb_table);
426
/* usb specific object needed to register this driver with the usb subsystem */
427
static struct usb_driver xusb_driver = {
428
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,16)
429
.owner = THIS_MODULE,
433
.disconnect = xusb_disconnect,
434
.id_table = xusb_table,
438
* File operations needed when we register this driver.
439
* This assumes that this driver NEEDS file operations,
440
* of course, which means that the driver is expected
441
* to have a node in the /dev directory. If the USB
442
* device were for a network interface then the driver
443
* would use "struct net_driver" instead, and a serial
444
* device would use "struct tty_driver".
446
static struct file_operations xusb_fops = {
448
* The owner field is part of the module-locking
449
* mechanism. The idea is that the kernel knows
450
* which module to increment the use-counter of
451
* BEFORE it calls the device's open() function.
452
* This also means that the kernel can decrement
453
* the use-counter again before calling release()
454
* or should the open() function fail.
456
.owner = THIS_MODULE,
460
* usb class driver info in order to get a minor number from the usb core,
461
* and to have the device registered with devfs and the driver core
463
static struct usb_class_driver xusb_class = {
464
.name = "usb/xpp_usb%d",
466
/* FIXME: The sysfs class interfase seems to have chaged around here */
467
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,15)
468
.mode = S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH,
470
.minor_base = USB_SKEL_MINOR_BASE,
474
* set up the endpoint information
475
* check out the endpoints
476
* FIXME: Should be simplified (above 2.6.10) to use usb_dev->ep_in[0..16] and usb_dev->ep_out[0..16]
478
static int set_endpoints(xusb_t *xusb, struct usb_interface *interface, struct xusb_model_info *model_info)
480
struct usb_host_interface *iface_desc;
481
struct usb_endpoint_descriptor *endpoint;
482
struct xusb_endpoint *xusb_ep;
486
iface_desc = &interface->altsetting[0];
487
DBG("Found interface. Endpoints: %d\n", iface_desc->desc.bNumEndpoints);
489
#define BULK_ENDPOINT(ep) (((ep)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_BULK)
491
for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
492
endpoint = &iface_desc->endpoint[i].desc;
493
ep_addr = endpoint->bEndpointAddress;
495
if(!BULK_ENDPOINT(endpoint)) {
496
DBG("endpoint 0x%x is not bulk: mbAttributes=0x%X\n",
497
ep_addr, endpoint->bmAttributes);
500
if(usb_pipein(ep_addr)) { // Input
501
if(ep_addr == model_info->in.ep_addr) {
502
if(endpoint->wMaxPacketSize < sizeof(xpacket_raw_t)) {
503
NOTICE("USB input endpoint 0x%X support only wMaxPacketSize=%d (need USB-2)\n", ep_addr, endpoint->wMaxPacketSize);
505
xusb_ep = &xusb->endpoints[XUSB_RECV];
506
xusb_ep->ep_addr = ep_addr;
507
xusb_ep->max_size = endpoint->wMaxPacketSize;
508
xusb_ep->callback = xpp_receive_callback;
511
if(ep_addr == model_info->out.ep_addr) {
512
if(endpoint->wMaxPacketSize < sizeof(xpacket_raw_t)) {
513
NOTICE("USB output endpoint 0x%X support only wMaxPacketSize=%d (need USB-2)\n", ep_addr, endpoint->wMaxPacketSize);
515
xusb_ep = &xusb->endpoints[XUSB_SEND];
516
xusb_ep->ep_addr = ep_addr;
517
xusb_ep->max_size = endpoint->wMaxPacketSize;
518
xusb_ep->callback = xpp_send_callback;
522
if (!xusb->endpoints[XUSB_RECV].ep_addr || !xusb->endpoints[XUSB_SEND].ep_addr) {
523
ERR("Couldn't find bulk-in or bulk-out endpoints\n");
526
DBG("in=0x%02X out=0x%02X\n", xusb->endpoints[XUSB_RECV].ep_addr, xusb->endpoints[XUSB_SEND].ep_addr);
533
* Called by the usb core when a new device is connected that it thinks
534
* this driver might be interested in.
536
static int xusb_probe(struct usb_interface *interface, const struct usb_device_id *id)
538
struct usb_device *udev = interface_to_usbdev(interface);
540
struct xusb_model_info *model_info = (struct xusb_model_info*)id->driver_info;
541
struct proc_dir_entry *procsummary = NULL;
543
struct xusb_endpoint *xusb_ep;
545
int retval = -ENOMEM;
548
INFO("New XUSB device MODEL=%s bus_type=%d\n", model_info->desc, model_info->bus_type);
550
if((retval = usb_reset_device(udev)) < 0) {
551
ERR("usb_reset_device failed: %d\n", retval);
555
ERR("Missing endpoint setup for this device %d:%d\n",
556
udev->descriptor.idVendor,udev->descriptor.idProduct);
561
/* allocate memory for our device state and initialize it */
562
xusb = kmalloc(sizeof(xusb_t), GFP_KERNEL);
564
ERR("xpp_usb: Unable to allocate new xpp usb bus\n");
568
memset(xusb, 0, sizeof(xusb_t));
570
init_MUTEX (&xusb->sem);
571
atomic_set(&xusb->pending_writes, 0);
573
xusb->interface = interface;
574
xusb->model_info = model_info;
576
if(!set_endpoints(xusb, interface, model_info)) {
580
xusb_ep = &xusb->endpoints[XUSB_RECV];
581
xusb->read_urb = xpp_urb_new(xusb, XUSB_RECV, xusb_ep->max_size);
582
if (!xusb->read_urb) {
583
ERR("No free urbs available\n");
588
/* allow device read, write and ioctl */
591
/* we can register the device now, as it is ready */
592
usb_set_intfdata (interface, xusb);
593
retval = usb_register_dev (interface, &xusb_class);
595
/* something prevented us from registering this driver */
596
ERR ("Not able to get a minor for this device.\n");
600
xusb->minor = interface->minor;
602
/* let the user know what node this device is now attached to */
603
INFO ("USB XPP device now attached to minor %d\n", xusb->minor);
605
/* Allocate high level structures */
606
xbus = xbus_new(&xusb_ops);
611
xbus->bus_type = model_info->bus_type;
612
xbus->max_packet_size = min(xusb->endpoints[XUSB_SEND].max_size , xusb->endpoints[XUSB_RECV].max_size);
614
spin_lock_irqsave(&xusb_lock, flags);
615
for(i = 0; i < USBDEV_MAX; i++) {
616
if(xusb_array[i] == NULL)
619
spin_unlock_irqrestore(&xusb_lock, flags);
620
if(i >= USBDEV_MAX) {
621
ERR("xpp_usb: Too many XPP USB buses\n");
626
char path[XBUS_DESCLEN];
628
usb_make_path(udev, path, XBUS_DESCLEN); // May trunacte... ignore
629
snprintf(xbus->busdesc, XBUS_DESCLEN, "%s", path);
632
DBG("GOT XPP USB BUS #%d: %s (type=%d)\n", i, xbus->busdesc, xbus->bus_type);
634
xusb_array[i] = xusb;
637
#ifdef CONFIG_PROC_FS
638
DBG("Creating proc entry " PROC_USBXPP_SUMMARY " in bus proc dir.\n");
639
procsummary = create_proc_read_entry(PROC_USBXPP_SUMMARY, 0444, xbus->proc_xbus_dir,
640
xusb_read_proc, xusb);
642
ERR("Failed to create proc read entry for xbus %s\n", xbus->busname);
643
// FIXME: better error handling
647
procsummary->owner = THIS_MODULE;
650
retval = usb_submit_urb(xusb->read_urb, GFP_ATOMIC);
652
ERR("%s: Failed to submit the receive URB errno=%d\n", __FUNCTION__, retval);
659
ERR("Failed to initialize xpp usb bus: %d\n", retval);
660
usb_set_intfdata (interface, NULL);
663
xpp_urb_delete(xusb->read_urb);
664
if(xusb->minor) // passed registration phase
665
usb_deregister_dev(interface, &xusb_class);
670
DBG("Remove proc_entry: " PROC_USBXPP_SUMMARY "\n");
671
remove_proc_entry(PROC_USBXPP_SUMMARY, xbus->proc_xbus_dir);
674
xbus_disconnect(xbus); // Blocking until fully deactivated!
682
* Called by the usb core when the device is removed from the system.
684
* This routine guarantees that the driver will not submit any more urbs
685
* by clearing dev->udev. It is also supposed to terminate any currently
686
* active urbs. Unfortunately, usb_bulk_msg(), used in xusb_read(), does
687
* not provide any way to do this. But at least we can cancel an active
690
static void xusb_disconnect(struct usb_interface *interface)
698
/* prevent races with open() */
699
down (&disconnect_sem);
701
xusb = usb_get_intfdata (interface);
702
usb_set_intfdata (interface, NULL);
706
for(i = 0; i < USBDEV_MAX; i++) {
707
if(xusb_array[i] == xusb)
710
BUG_ON(i >= USBDEV_MAX);
711
xusb_array[i] = NULL;
713
#ifdef CONFIG_PROC_FS
714
if(xbus->proc_xbus_dir) {
715
DBG("Remove proc_entry: " PROC_USBXPP_SUMMARY "\n");
716
remove_proc_entry(PROC_USBXPP_SUMMARY, xbus->proc_xbus_dir);
720
xbus_disconnect(xbus); // Blocking until fully deactivated!
726
/* give back our minor */
727
usb_deregister_dev (interface, &xusb_class);
729
/* terminate an ongoing read */
730
/* terminate an ongoing write */
731
// FIXME: Does it really kill pending URB's?
734
xpp_urb_delete(xusb->read_urb);
737
DBG("Semaphore released\n");
741
up (&disconnect_sem);
742
INFO("XUSB #%d now disconnected\n", minor);
745
static void xpp_send_callback(struct urb *urb, struct pt_regs *regs)
747
xusb_t *xusb = (xusb_t *)urb->context;
748
xbus_t *xbus = xusb->xbus;
751
atomic_dec(&xusb->pending_writes);
752
/* sync/async unlink faults aren't errors */
753
if (urb->status && !(urb->status == -ENOENT || urb->status == -ECONNRESET)) {
754
static int rate_limit;
755
if((rate_limit++ % 1000) < 10)
756
DBG("nonzero read bulk status received: %d\n", urb->status);
757
XUSB_COUNTER(xusb, TX_ERRORS)++;
761
ERR("A packet from non-connected device?\n");
764
/* allow device read, write and ioctl */
765
XUSB_COUNTER(xusb, TX_PACKETS)++;
768
static void xpp_receive_callback(struct urb *urb, struct pt_regs *regs)
770
xusb_t *xusb = (xusb_t *)urb->context;
775
bool do_resubmit = 1;
781
NOTICE("spurious URB\n");
785
DBG("nonzero read bulk status received: %d\n", urb->status);
786
XUSB_COUNTER(xusb, RX_ERRORS)++;
787
/* Free old URB, allocate a fresh one */
789
xpp_urb_delete(xusb->read_urb);
790
xusb->read_urb = xpp_urb_new(xusb, XUSB_RECV, xusb->endpoints[XUSB_RECV].max_size);
791
if (!xusb->read_urb) {
792
ERR("URB allocation failed\n");
797
if(!down_read_trylock(&xbus->in_use)) {
798
ERR("%s: xbus is going down\n", __FUNCTION__);
804
ERR("A packet from non-connected device?\n");
808
pack = xbus->ops->packet_new(xbus, GFP_ATOMIC);
810
ERR("%s: Not enough memory for packets. Dropping\n", __FUNCTION__);
814
size = urb->actual_length;
815
memcpy(&pack->content, urb->transfer_buffer, size);
817
pack->datalen = size - sizeof(xpd_addr_t) - 1; // opcode size
818
// DBG("datalen of new packet: %d\n", pack->datalen);
820
packet_debug("USB_PACKET_RECEIVE", xusb, pack);
821
XUSB_COUNTER(xusb, RX_PACKETS)++;
822
if(pack->content.opcode == XPROTO_NAME(GLOBAL,PCM_READ))
823
XUSB_COUNTER(xusb, PCM_READS)++;
825
packet_receive(xbus, pack);
828
up_read(&xbus->in_use);
830
retval = usb_submit_urb(urb, GFP_ATOMIC);
832
ERR("failed re-submitting read urb, error %d\n", retval);
839
/*------------------------- Initialization -------------------------*/
841
int __init xpp_usb_init(void)
846
INFO("%s revision %s\n", THIS_MODULE->name, ZAPTEL_VERSION);
848
/* register this driver with the USB subsystem */
849
result = usb_register(&xusb_driver);
851
ERR("usb_register failed. Error number %d\n", result);
858
void __exit xpp_usb_cleanup(void)
861
/* deregister this driver with the USB subsystem */
862
usb_deregister(&xusb_driver);
867
#ifdef CONFIG_PROC_FS
869
static int xusb_read_proc(char *page, char **start, off_t off, int count, int *eof, void *data)
874
//unsigned long stamp = jiffies;
879
// TODO: probably needs a per-xusb lock:
880
spin_lock_irqsave(&xusb_lock, flags);
881
len += sprintf(page + len, "device: %d, #altsettings: %d, minor: %d\n"
882
"\tModel Info: Bus Type=%d (%s)\n"
883
"\tIn: 0x%02X - Size: %d)\n"
884
"\tOut: 0x%02X - Size: %d)\n",
886
xusb->interface->num_altsetting,
888
xusb->model_info->bus_type,
889
xusb->model_info->desc,
890
xusb->endpoints[XUSB_RECV].ep_addr,
891
xusb->endpoints[XUSB_RECV].max_size,
892
xusb->endpoints[XUSB_SEND].ep_addr,
893
xusb->endpoints[XUSB_SEND].max_size
895
len += sprintf(page + len, "\npending_writes=%d\n", atomic_read(&xusb->pending_writes));
896
#ifdef DEBUG_PCM_TIMING
897
len += sprintf(page + len, "\nstamp_last_pcm_read=%lld accumulate_diff=%lld\n", stamp_last_pcm_read, accumulate_diff);
899
len += sprintf(page + len, "\nCOUNTERS:\n");
900
for(i = 0; i < XUSB_COUNTER_MAX; i++) {
901
len += sprintf(page + len, "\t%-15s = %d\n", xusb_counters[i].name, xusb->counters[i]);
904
len += sprintf(page + len, "<-- len=%d\n", len);
906
spin_unlock_irqrestore(&xusb_lock, flags);
908
if (len <= off+count)
924
MODULE_DESCRIPTION("XPP USB Driver");
925
MODULE_AUTHOR("Oron Peled <oron@actcom.co.il>");
926
MODULE_LICENSE("GPL");
927
MODULE_VERSION(ZAPTEL_VERSION);
929
module_init(xpp_usb_init);
930
module_exit(xpp_usb_cleanup);