~ubuntu-branches/ubuntu/quantal/zaptel/quantal

« back to all changes in this revision

Viewing changes to xpp/xpp_usb.c

  • Committer: Bazaar Package Importer
  • Author(s): Tzafrir Cohen
  • Date: 2008-08-28 22:58:23 UTC
  • mfrom: (11.1.11 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080828225823-r8bdunirm8hmc76m
Tags: 1:1.4.11~dfsg-2
* Patch xpp_fxs_power: Fixed an issue with hook detection of the Astribank
  FXS module.
* Don't fail init.d script if fxotune fails. This may happen if running it
  when Asterisk is already running.
* Bump standards version to 3.8.0.0 .
* Ignore false lintian warning ("m-a a-i" has "a a").
* Patch xpp_fxo_cid_always: do always pass PCM if that's what the user
  asked.
* Patch vzaphfc_proc_root_dir: fix vzaphfc on 2.6.26.
* Patch wcte12xp_flags: Proper time for irq save flags.
* Patch headers_2627: Fix location of semaphore.h for 2.6.27 .
* Patch xpp_fxs_dtmf_leak: Don't play DTMFs to the wrong channel.
* Patch wctdm_fix_alarm: Fix sending channel alarms.
* Patch device_class_2626: Fix building 2.6.26 (Closes: #493397).
* Using dh_lintian for lintian overrides, hence requiring debhelper 6.0.7.
* Lintian: we know we have direct changes. Too bad we're half-upstream :-(
* Fix doc-base section names. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Written by Oron Peled <oron@actcom.co.il>
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
 
 
24
 
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
25
 
#  warning "This module is tested only with 2.6 kernels"
26
 
#endif
27
 
 
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 */
42
 
#include "xpd.h"
43
 
#include "xproto.h"
44
 
#include "xbus-core.h"
45
 
#ifdef  DEBUG
46
 
#include "card_fxs.h"
47
 
#include "card_fxo.h"
48
 
#endif
49
 
 
50
 
static const char rcsid[] = "$Id: xpp_usb.c 1457 2006-09-09 15:24:12Z tzafrir $";
51
 
 
52
 
DEF_PARM(int, print_dbg, 0, "Print DBG statements");    /* must be before zap_debug.h */
53
 
 
54
 
#include "zap_debug.h"
55
 
 
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
60
 
#endif
61
 
#define USBDEV_MAX      10
62
 
/* Get a minor range for your devices from the usb maintainer */
63
 
#define USB_SKEL_MINOR_BASE     192
64
 
 
65
 
#ifdef CONFIG_PROC_FS
66
 
#define PROC_USBXPP_SUMMARY     "xpp_usb"
67
 
#endif
68
 
 
69
 
#ifdef  DEBUG_PCM_TIMING
70
 
static cycles_t stamp_last_pcm_read;
71
 
static cycles_t accumulate_diff;
72
 
#endif
73
 
 
74
 
struct xusb_model_info;
75
 
 
76
 
struct xusb_endpoint {
77
 
        int             ep_addr;
78
 
        int             max_size;
79
 
        usb_complete_t  callback;
80
 
};
81
 
 
82
 
static int xusb_packet_send(xbus_t *xbus, xpacket_t *pack);
83
 
 
84
 
xbus_ops_t      xusb_ops = {
85
 
        .packet_send = xusb_packet_send,
86
 
        .packet_new = NULL,                     // Default allocator
87
 
        .packet_free = NULL,                    // Default deallocator
88
 
};
89
 
 
90
 
enum {
91
 
        XUSB_N_RX_PACKETS,
92
 
        XUSB_N_TX_PACKETS,
93
 
        XUSB_N_RX_ERRORS,
94
 
        XUSB_N_TX_ERRORS,
95
 
        XUSB_N_PCM_READS,
96
 
        XUSB_N_PCM_WRITES,
97
 
};
98
 
 
99
 
#define XUSB_COUNTER(xusb, counter)     ((xusb)->counters[XUSB_N_ ## counter])
100
 
 
101
 
#define C_(x)   [ XUSB_N_ ## x ] = { #x }
102
 
 
103
 
static struct xusb_counters {
104
 
        char    *name;
105
 
} xusb_counters[] = {
106
 
        C_(RX_PACKETS),
107
 
        C_(TX_PACKETS),
108
 
        C_(RX_ERRORS),
109
 
        C_(TX_ERRORS),
110
 
        C_(PCM_READS),
111
 
        C_(PCM_WRITES),
112
 
};
113
 
 
114
 
#undef C_
115
 
 
116
 
#define XUSB_COUNTER_MAX        ARRAY_SIZE(xusb_counters)
117
 
 
118
 
#define MAX_PENDING_WRITES      100
119
 
 
120
 
enum xusb_dir {
121
 
        XUSB_RECV = 0,
122
 
        XUSB_SEND = 1,
123
 
};
124
 
 
125
 
/*
126
 
 * USB XPP Bus (a USB Device)
127
 
 */
128
 
typedef struct xpp_usb_bus {
129
 
        xbus_t                  *xbus;
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 */
133
 
 
134
 
        struct xusb_model_info  *model_info;
135
 
        struct xusb_endpoint    endpoints[2];           /* RECV/SEND endpoints */
136
 
 
137
 
        struct urb              *read_urb;
138
 
 
139
 
        struct completion       write_finished;         /* wait for the write to finish */
140
 
 
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];
146
 
} xusb_t;
147
 
 
148
 
static  spinlock_t      xusb_lock = SPIN_LOCK_UNLOCKED;
149
 
static xusb_t *xusb_array[USBDEV_MAX] = {};
150
 
static unsigned bus_count = 0;
151
 
 
152
 
 
153
 
/* prevent races between open() and disconnect() */
154
 
static DECLARE_MUTEX (disconnect_sem);
155
 
 
156
 
/*
157
 
 * Function Prototypes
158
 
 */
159
 
#if 0
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);
166
 
#endif
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);
171
 
 
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);
175
 
 
176
 
/*------------------------------------------------------------------*/
177
 
 
178
 
#if 0
179
 
/**
180
 
 * Allocates a new XPP packet.
181
 
 * @xbus The XPP bus in which the packet will flow (for counters 
182
 
 *       maintenance)
183
 
 * @flags Flags for kernel memory allocation.
184
 
 * @returns A pointer to the new packet, or NULL in case of failure.
185
 
 * 
186
 
 * 
187
 
 * Packet allocation/deallocation:
188
 
 *      Sent packets:
189
 
 *        - Allocated by protocol commands
190
 
 *        - Deallocated by xmus_xmitter
191
 
 *      Receive packets:
192
 
 *        - Allocated/deallocated by xbus_xmiter
193
 
 */
194
 
xpacket_t       *xusb_packet_new(xbus_t *xbus, int flags)
195
 
{
196
 
        xpacket_t       *pack;
197
 
 
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);
204
 
        if (pack) {
205
 
                memset(pack, 0, sizeof(xpacket_t));
206
 
                atomic_inc(&xpacket_count);
207
 
        } else {
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));
211
 
        }
212
 
        return pack;
213
 
}
214
 
 
215
 
void xusb_packet_free(xbus_t *xbus, xpacket_t *p)
216
 
{
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));
222
 
}
223
 
 
224
 
#endif
225
 
 
226
 
#ifndef DEBUG
227
 
 
228
 
#define packet_debug(m, x, p)
229
 
 
230
 
#else
231
 
 
232
 
static void packet_debug(const char msg[], xusb_t *xusb, xpacket_t *pack)
233
 
{
234
 
        char    title[XBUS_DESCLEN];
235
 
 
236
 
        if(pack->content.opcode == XPROTO_NAME(GLOBAL,PCM_READ)) {
237
 
#ifdef  DEBUG_PCM_TIMING
238
 
                /*
239
 
                 * DEBUG: high-res timing of PCM_READ to PCM_WRITE
240
 
                 */
241
 
                stamp_last_pcm_read = get_cycles();
242
 
#endif
243
 
#if 0
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);
248
 
#endif
249
 
                return;
250
 
        } else if(pack->content.opcode == XPROTO_NAME(GLOBAL,PCM_WRITE)) {
251
 
#ifdef  DEBUG_PCM_TIMING
252
 
                /*
253
 
                 * DEBUG: high-res timing of PCM_READ to PCM_WRITE
254
 
                 */
255
 
                cycles_t        diff = get_cycles() - stamp_last_pcm_read;
256
 
                accumulate_diff += diff;
257
 
#endif
258
 
#if 0
259
 
                static  int rate_limit;
260
 
                if((rate_limit++ % 1000) < 10)
261
 
                        dump_packet("USB SEND PCM", pack, print_dbg);
262
 
#endif
263
 
                return;
264
 
        } else if(pack->content.opcode == XPROTO_NAME(FXS, SLIC_WRITE)) {
265
 
                slic_cmd_t      *sc;
266
 
 
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 */
269
 
                        return;
270
 
                if(sc->bytes == 2 && sc->content.direct.reg_num == DAA_VBAT_REGISTER && sc->content.direct.read)        /* ignore DAA_QUERY */
271
 
                        return;
272
 
        } else if(pack->content.opcode == XPROTO_NAME(FXS, SLIC_REPLY)) {
273
 
                        return;
274
 
        }
275
 
        snprintf(title, XBUS_DESCLEN, "%s: %s", msg, xusb->xbus->busname);
276
 
        dump_packet(title, pack, print_dbg);
277
 
}
278
 
#endif
279
 
 
280
 
static int xusb_packet_send(xbus_t *xbus, xpacket_t *pack)
281
 
{
282
 
        xusb_t                  *xusb;
283
 
        struct urb              *urb;
284
 
        int                     ret = 0;
285
 
        size_t                  size;
286
 
        struct xusb_endpoint    *xusb_ep;
287
 
 
288
 
        BUG_ON(!pack);
289
 
        BUG_ON(!xbus);
290
 
        xusb = xbus->priv;
291
 
        BUG_ON(!xusb);
292
 
        if(!xusb->present) {
293
 
                NOTICE("tried to send packets to non-exitant USB device. Ignored\n");
294
 
                ret = -ENODEV;
295
 
                goto freepack;
296
 
        }
297
 
        /*
298
 
         * If something really bad happend, do not overflow the USB stack
299
 
         */
300
 
        if(atomic_read(&xusb->pending_writes) > MAX_PENDING_WRITES) {
301
 
                static  int rate_limit;
302
 
 
303
 
                if((rate_limit++ % 1000) < 10)
304
 
                        ERR("%s: %s: more than %d pending writes. Dropping.\n", __FUNCTION__, xbus->busname, MAX_PENDING_WRITES);
305
 
                ret = -ENODEV;
306
 
                goto freepack;
307
 
        }
308
 
        size = PACKET_LEN(pack);
309
 
        xusb_ep = &xusb->endpoints[XUSB_SEND];
310
 
        urb = xpp_urb_new(xusb, XUSB_SEND, size);
311
 
        if (!urb) {
312
 
                ERR("No free urbs available\n");
313
 
                ret = -ENOMEM;
314
 
                goto freepack;
315
 
        }
316
 
        packet_debug("USB_PACKET_SEND", xusb, pack);
317
 
 
318
 
        /* FIXME: FIXME: FIXME: we use copy+free until low-level drivers allocate memory themselves */
319
 
        memcpy(urb->transfer_buffer, &pack->content, size);
320
 
 
321
 
        ret = usb_submit_urb(urb, GFP_ATOMIC);
322
 
        if(ret < 0) {
323
 
                static int rate_limit;
324
 
 
325
 
                if((rate_limit++ % 1000) < 5)
326
 
                        ERR("%s: failed submit_urb: %d\n", __FUNCTION__, ret);
327
 
                xpp_urb_delete(urb);
328
 
                ret = -EBADF;
329
 
                goto freepack;
330
 
        }
331
 
        atomic_inc(&xusb->pending_writes);
332
 
        if(pack->content.opcode == XPROTO_NAME(GLOBAL,PCM_WRITE))
333
 
                XUSB_COUNTER(xusb, PCM_WRITES)++;
334
 
freepack:
335
 
        xbus->ops->packet_free(xbus, pack);     // FIXME: eventually will be done in the urb callback
336
 
        if(ret < 0)
337
 
                XUSB_COUNTER(xusb, TX_ERRORS)++;
338
 
        return ret;
339
 
}
340
 
 
341
 
static void xpp_urb_delete(struct urb *urb)
342
 
{
343
 
        BUG_ON(!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,
347
 
                        urb->transfer_dma);
348
 
        usb_free_urb(urb);
349
 
}
350
 
 
351
 
static struct urb *xpp_urb_new(xusb_t *xusb, enum xusb_dir dir, size_t size)
352
 
{
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;
357
 
        struct urb      *urb;
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);
363
 
                 
364
 
        if(size > xusb_ep->max_size)
365
 
                return NULL;
366
 
        urb = usb_alloc_urb(0, GFP_ATOMIC);
367
 
        if (!urb) {
368
 
                err("No free urbs available");
369
 
                return NULL;
370
 
        }
371
 
 
372
 
        /* on some platforms using this kind of buffer alloc
373
 
         * call eliminates a dma "bounce buffer".
374
 
         *
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 
378
 
         * transfers)
379
 
         */
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);
383
 
        if (!buffer) {
384
 
                err("Couldn't allocate buffer");
385
 
                usb_free_urb(urb);
386
 
                return NULL;
387
 
        }
388
 
        usb_fill_bulk_urb(urb, udev, pipe, buffer, size, urb_cb, xusb);
389
 
        return urb;
390
 
}
391
 
 
392
 
/*------------------------- XPP USB Bus Handling -------------------*/
393
 
 
394
 
#define XUSB_MODEL(ep_in,ep_out,type,str)       \
395
 
        {                                       \
396
 
                .in = { .ep_addr = (ep_in) },   \
397
 
                .out = { .ep_addr = (ep_out) }, \
398
 
                .bus_type = (type),             \
399
 
                .desc = (str)                   \
400
 
        }
401
 
 
402
 
static const struct xusb_model_info {
403
 
        const char              *desc;
404
 
        struct xusb_endpoint    in;
405
 
        struct xusb_endpoint    out;
406
 
        xbus_type_t             bus_type;
407
 
} model_table[] = {
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"),
411
 
};
412
 
 
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 */
421
 
};
422
 
 
423
 
MODULE_DEVICE_TABLE (usb, xusb_table);
424
 
 
425
 
 
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,
430
 
#endif
431
 
        .name =         "xpp_usb",
432
 
        .probe =        xusb_probe,
433
 
        .disconnect =   xusb_disconnect,
434
 
        .id_table =     xusb_table,
435
 
};
436
 
 
437
 
/*
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".
445
 
 */
446
 
static struct file_operations xusb_fops = {
447
 
        /*
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.
455
 
         */
456
 
        .owner =        THIS_MODULE,
457
 
};
458
 
 
459
 
/* 
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
462
 
 */
463
 
static struct usb_class_driver xusb_class = {
464
 
        .name =         "usb/xpp_usb%d",
465
 
        .fops =         &xusb_fops,
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,
469
 
#endif
470
 
        .minor_base =   USB_SKEL_MINOR_BASE,
471
 
};
472
 
 
473
 
/*
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]
477
 
 */
478
 
static int set_endpoints(xusb_t *xusb, struct usb_interface *interface, struct xusb_model_info *model_info)
479
 
{
480
 
        struct usb_host_interface       *iface_desc;
481
 
        struct usb_endpoint_descriptor  *endpoint;
482
 
        struct xusb_endpoint            *xusb_ep;
483
 
        int                             ep_addr;
484
 
        int i;
485
 
 
486
 
        iface_desc = &interface->altsetting[0];
487
 
        DBG("Found interface. Endpoints: %d\n", iface_desc->desc.bNumEndpoints);
488
 
 
489
 
#define BULK_ENDPOINT(ep) (((ep)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_BULK)
490
 
 
491
 
        for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
492
 
                endpoint = &iface_desc->endpoint[i].desc;
493
 
                ep_addr = endpoint->bEndpointAddress;
494
 
                
495
 
                if(!BULK_ENDPOINT(endpoint)) {
496
 
                        DBG("endpoint 0x%x is not bulk: mbAttributes=0x%X\n",
497
 
                                        ep_addr, endpoint->bmAttributes);
498
 
                        continue;
499
 
                }
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);
504
 
                                }
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;
509
 
                        }
510
 
                } else {                        // Output
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);
514
 
                                }
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;
519
 
                        }
520
 
                }
521
 
        }
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");
524
 
                return 0;
525
 
        }
526
 
        DBG("in=0x%02X out=0x%02X\n", xusb->endpoints[XUSB_RECV].ep_addr, xusb->endpoints[XUSB_SEND].ep_addr);
527
 
        return 1;
528
 
}
529
 
 
530
 
/**
531
 
 *      xusb_probe
532
 
 *
533
 
 *      Called by the usb core when a new device is connected that it thinks
534
 
 *      this driver might be interested in.
535
 
 */
536
 
static int xusb_probe(struct usb_interface *interface, const struct usb_device_id *id)
537
 
{
538
 
        struct usb_device       *udev = interface_to_usbdev(interface);
539
 
        xusb_t                  *xusb = NULL;
540
 
        struct xusb_model_info  *model_info = (struct xusb_model_info*)id->driver_info;
541
 
        struct proc_dir_entry   *procsummary = NULL;
542
 
        xbus_t                  *xbus = NULL;
543
 
        struct xusb_endpoint    *xusb_ep;
544
 
        unsigned long           flags;
545
 
        int                     retval = -ENOMEM;
546
 
        int                     i;
547
 
        
548
 
        INFO("New XUSB device MODEL=%s bus_type=%d\n", model_info->desc, model_info->bus_type);
549
 
 
550
 
        if((retval = usb_reset_device(udev)) < 0) {
551
 
                ERR("usb_reset_device failed: %d\n", retval);
552
 
                goto probe_failed;
553
 
        }
554
 
        if (!model_info) {
555
 
                ERR("Missing endpoint setup for this device %d:%d\n", 
556
 
                                udev->descriptor.idVendor,udev->descriptor.idProduct);
557
 
                retval = -ENODEV;
558
 
                goto probe_failed;
559
 
        }
560
 
 
561
 
        /* allocate memory for our device state and initialize it */
562
 
        xusb = kmalloc(sizeof(xusb_t), GFP_KERNEL);
563
 
        if (xusb == NULL) {
564
 
                ERR("xpp_usb: Unable to allocate new xpp usb bus\n");
565
 
                retval = -ENOMEM;
566
 
                goto probe_failed;
567
 
        }
568
 
        memset(xusb, 0, sizeof(xusb_t));
569
 
 
570
 
        init_MUTEX (&xusb->sem);
571
 
        atomic_set(&xusb->pending_writes, 0);
572
 
        xusb->udev = udev;
573
 
        xusb->interface = interface;
574
 
        xusb->model_info = model_info;
575
 
 
576
 
        if(!set_endpoints(xusb, interface, model_info)) {
577
 
                retval = -ENODEV;
578
 
                goto probe_failed;
579
 
        }
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");
584
 
                retval = -ENOMEM;
585
 
                goto probe_failed;
586
 
        }
587
 
 
588
 
        /* allow device read, write and ioctl */
589
 
        xusb->present = 1;
590
 
 
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);
594
 
        if (retval) {
595
 
                /* something prevented us from registering this driver */
596
 
                ERR ("Not able to get a minor for this device.\n");
597
 
                goto probe_failed;
598
 
        }
599
 
 
600
 
        xusb->minor = interface->minor;
601
 
 
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);
604
 
 
605
 
        /* Allocate high level structures */
606
 
        xbus = xbus_new(&xusb_ops);
607
 
        if(!xbus) {
608
 
                retval = -ENOMEM;
609
 
                goto probe_failed;
610
 
        }
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);
613
 
 
614
 
        spin_lock_irqsave(&xusb_lock, flags);
615
 
        for(i = 0; i < USBDEV_MAX; i++) {
616
 
                if(xusb_array[i] == NULL)
617
 
                        break;
618
 
        }
619
 
        spin_unlock_irqrestore(&xusb_lock, flags);
620
 
        if(i >= USBDEV_MAX) {
621
 
                ERR("xpp_usb: Too many XPP USB buses\n");
622
 
                retval = -ENOMEM;
623
 
                goto probe_failed;
624
 
        }
625
 
        {
626
 
                char    path[XBUS_DESCLEN];
627
 
 
628
 
                usb_make_path(udev, path, XBUS_DESCLEN);        // May trunacte... ignore
629
 
                snprintf(xbus->busdesc, XBUS_DESCLEN, "%s", path);
630
 
        }
631
 
 
632
 
        DBG("GOT XPP USB BUS #%d: %s (type=%d)\n", i, xbus->busdesc, xbus->bus_type);
633
 
 
634
 
        xusb_array[i] = xusb;
635
 
 
636
 
 
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);
641
 
        if (!procsummary) {
642
 
                ERR("Failed to create proc read entry for xbus %s\n", xbus->busname);
643
 
                // FIXME: better error handling
644
 
                retval = -EIO;
645
 
                goto probe_failed;
646
 
        }
647
 
        procsummary->owner = THIS_MODULE;
648
 
#endif
649
 
        bus_count++;
650
 
        retval = usb_submit_urb(xusb->read_urb, GFP_ATOMIC);
651
 
        if(retval < 0) {
652
 
                ERR("%s: Failed to submit the receive URB errno=%d\n", __FUNCTION__, retval);
653
 
        }
654
 
        xusb->xbus = xbus;
655
 
        xbus->priv = xusb;
656
 
        xbus_activate(xbus);
657
 
        return retval;
658
 
probe_failed:
659
 
        ERR("Failed to initialize xpp usb bus: %d\n", retval);
660
 
        usb_set_intfdata (interface, NULL);
661
 
        if(xusb) {
662
 
                if(xusb->read_urb)
663
 
                        xpp_urb_delete(xusb->read_urb);
664
 
                if(xusb->minor) // passed registration phase
665
 
                        usb_deregister_dev(interface, &xusb_class);
666
 
                kfree(xusb);
667
 
        }
668
 
        if(xbus) {
669
 
                if(procsummary) {
670
 
                        DBG("Remove proc_entry: " PROC_USBXPP_SUMMARY "\n");
671
 
                        remove_proc_entry(PROC_USBXPP_SUMMARY, xbus->proc_xbus_dir);
672
 
                        procsummary = NULL;
673
 
                }
674
 
                xbus_disconnect(xbus);          // Blocking until fully deactivated!
675
 
        }
676
 
        return retval;
677
 
}
678
 
 
679
 
/**
680
 
 *      xusb_disconnect
681
 
 *
682
 
 *      Called by the usb core when the device is removed from the system.
683
 
 *
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
688
 
 *      write.
689
 
 */
690
 
static void xusb_disconnect(struct usb_interface *interface)
691
 
{
692
 
        xusb_t                  *xusb;
693
 
        xbus_t                  *xbus;
694
 
        int                     minor;
695
 
        int                     i;
696
 
 
697
 
        DBG("CALLED\n");
698
 
        /* prevent races with open() */
699
 
        down (&disconnect_sem);
700
 
 
701
 
        xusb = usb_get_intfdata (interface);
702
 
        usb_set_intfdata (interface, NULL);
703
 
        xbus = xusb->xbus;
704
 
 
705
 
        /* find our xusb */
706
 
        for(i = 0; i < USBDEV_MAX; i++) {
707
 
                if(xusb_array[i] == xusb)
708
 
                        break;
709
 
        }
710
 
        BUG_ON(i >= USBDEV_MAX);
711
 
        xusb_array[i] = NULL;
712
 
 
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);
717
 
        }
718
 
#endif
719
 
        xusb->present = 0;
720
 
        xbus_disconnect(xbus);          // Blocking until fully deactivated!
721
 
 
722
 
        down (&xusb->sem);
723
 
 
724
 
        minor = xusb->minor;
725
 
 
726
 
        /* give back our minor */
727
 
        usb_deregister_dev (interface, &xusb_class);
728
 
 
729
 
        /* terminate an ongoing read */
730
 
        /* terminate an ongoing write */
731
 
        // FIXME: Does it really kill pending URB's?
732
 
 
733
 
        if(xusb->read_urb)
734
 
                xpp_urb_delete(xusb->read_urb);
735
 
 
736
 
        up (&xusb->sem);
737
 
        DBG("Semaphore released\n");
738
 
 
739
 
        kfree(xusb);
740
 
 
741
 
        up (&disconnect_sem);
742
 
        INFO("XUSB #%d now disconnected\n", minor);
743
 
}
744
 
 
745
 
static void xpp_send_callback(struct urb *urb, struct pt_regs *regs)
746
 
{
747
 
        xusb_t                  *xusb = (xusb_t *)urb->context;
748
 
        xbus_t                  *xbus = xusb->xbus;
749
 
 
750
 
        BUG_ON(!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)++;
758
 
        }
759
 
        xpp_urb_delete(urb);
760
 
        if(!xusb->present) {
761
 
                ERR("A packet from non-connected device?\n");
762
 
                return;
763
 
        }
764
 
        /* allow device read, write and ioctl */
765
 
        XUSB_COUNTER(xusb, TX_PACKETS)++;
766
 
}
767
 
 
768
 
static void xpp_receive_callback(struct urb *urb, struct pt_regs *regs)
769
 
{
770
 
        xusb_t          *xusb = (xusb_t *)urb->context;
771
 
        xbus_t          *xbus;
772
 
        xpacket_t       *pack;
773
 
        size_t          size;
774
 
        int             retval;
775
 
        bool            do_resubmit = 1;
776
 
        bool            is_inuse = 0;
777
 
 
778
 
        BUG_ON(!xusb);
779
 
        xbus = xusb->xbus;
780
 
        if(!xbus) {
781
 
                NOTICE("spurious URB\n");
782
 
                return;
783
 
        }
784
 
        if (urb->status) {
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 */
788
 
                if(xusb->read_urb)
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");
793
 
                        do_resubmit = 0;;
794
 
                }
795
 
                goto end;
796
 
        }
797
 
        if(!down_read_trylock(&xbus->in_use)) {
798
 
                ERR("%s: xbus is going down\n", __FUNCTION__);
799
 
                do_resubmit = 0;
800
 
                goto end;
801
 
        }
802
 
        is_inuse = 1;
803
 
        if(!xusb->present) {
804
 
                ERR("A packet from non-connected device?\n");
805
 
                do_resubmit = 0;
806
 
                goto end;
807
 
        }
808
 
        pack = xbus->ops->packet_new(xbus, GFP_ATOMIC);
809
 
        if(!pack) {
810
 
                ERR("%s: Not enough memory for packets. Dropping\n", __FUNCTION__);
811
 
                goto end;
812
 
        }
813
 
 
814
 
        size = urb->actual_length;
815
 
        memcpy(&pack->content, urb->transfer_buffer, size);
816
 
 
817
 
        pack->datalen = size - sizeof(xpd_addr_t) - 1;  // opcode size
818
 
        // DBG("datalen of new packet: %d\n", pack->datalen);
819
 
 
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)++;
824
 
        // Send UP
825
 
        packet_receive(xbus, pack);
826
 
end:    
827
 
        if(is_inuse)
828
 
                up_read(&xbus->in_use);
829
 
        if(do_resubmit) {
830
 
                retval = usb_submit_urb(urb, GFP_ATOMIC);
831
 
                if (retval < 0) {
832
 
                        ERR("failed re-submitting read urb, error %d\n", retval);
833
 
                        return;
834
 
                }
835
 
        }
836
 
}
837
 
 
838
 
 
839
 
/*------------------------- Initialization -------------------------*/
840
 
 
841
 
int __init xpp_usb_init(void)
842
 
{
843
 
        int result;
844
 
        //xusb_t *xusb;
845
 
 
846
 
        INFO("%s revision %s\n", THIS_MODULE->name, ZAPTEL_VERSION);
847
 
 
848
 
        /* register this driver with the USB subsystem */
849
 
        result = usb_register(&xusb_driver);
850
 
        if (result) {
851
 
                ERR("usb_register failed. Error number %d\n", result);
852
 
                return result;
853
 
        }
854
 
        return 0;
855
 
}
856
 
 
857
 
 
858
 
void __exit xpp_usb_cleanup(void)
859
 
{
860
 
        DBG("\n");
861
 
        /* deregister this driver with the USB subsystem */
862
 
        usb_deregister(&xusb_driver);
863
 
}
864
 
 
865
 
 
866
 
 
867
 
#ifdef CONFIG_PROC_FS
868
 
 
869
 
static int xusb_read_proc(char *page, char **start, off_t off, int count, int *eof, void *data)
870
 
{
871
 
        int             len = 0;
872
 
        unsigned long   flags;
873
 
        int             i;
874
 
        //unsigned long stamp = jiffies;
875
 
        xusb_t          *xusb = data;
876
 
 
877
 
        if(!xusb)
878
 
                goto out;
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",
885
 
                xusb->udev->devnum,
886
 
                xusb->interface->num_altsetting,
887
 
                xusb->minor,
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
894
 
        );
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);
898
 
#endif
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]);
902
 
        }
903
 
#if 0
904
 
        len += sprintf(page + len, "<-- len=%d\n", len);
905
 
#endif
906
 
        spin_unlock_irqrestore(&xusb_lock, flags);
907
 
out:
908
 
        if (len <= off+count)
909
 
                *eof = 1;
910
 
        *start = page + off;
911
 
        len -= off;
912
 
        if (len > count)
913
 
                len = count;
914
 
        if (len < 0)
915
 
                len = 0;
916
 
        return len;
917
 
 
918
 
}
919
 
 
920
 
#endif
921
 
 
922
 
 
923
 
 
924
 
MODULE_DESCRIPTION("XPP USB Driver");
925
 
MODULE_AUTHOR("Oron Peled <oron@actcom.co.il>");
926
 
MODULE_LICENSE("GPL");
927
 
MODULE_VERSION(ZAPTEL_VERSION);
928
 
 
929
 
module_init(xpp_usb_init);
930
 
module_exit(xpp_usb_cleanup);