1 /*****************************************************************************/
3 * auerswald.c -- Auerswald PBX/System Telephone usb driver.
5 * Copyright (C) 2001 Wolfgang Mües (wolfgang@iksw-muees.de)
7 * Very much code of this driver is borrowed from dabusb.c (Deti Fliegl)
8 * and from the USB Skeleton driver (Greg Kroah-Hartman). Thank you.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 /*****************************************************************************/
26 /* Standard Linux module include files */
27 #include <asm/uaccess.h>
28 #include <asm/byteorder.h>
29 #include <linux/slab.h>
30 #include <linux/module.h>
31 #include <linux/init.h>
32 #undef DEBUG /* include debug macros until it's done */
33 #include <linux/usb.h>
35 /*-------------------------------------------------------------------*/
38 #define dump( adr, len) \
41 printk (KERN_DEBUG); \
42 for (u = 0; u < len; u++) \
43 printk (" %02X", adr[u] & 0xFF); \
47 #define dump( adr, len)
50 /*-------------------------------------------------------------------*/
51 /* Version Information */
52 #define DRIVER_VERSION "0.9.11"
53 #define DRIVER_AUTHOR "Wolfgang Mües <wolfgang@iksw-muees.de>"
54 #define DRIVER_DESC "Auerswald PBX/System Telephone usb driver"
56 /*-------------------------------------------------------------------*/
57 /* Private declarations for Auerswald USB driver */
59 /* Auerswald Vendor ID */
60 #define ID_AUERSWALD 0x09BF
62 #define AUER_MINOR_BASE 112 /* auerswald driver minor number */
64 /* we can have up to this number of device plugged in at once */
65 #define AUER_MAX_DEVICES 16
68 /* Number of read buffers for each device */
69 #define AU_RBUFFERS 10
71 /* Number of chain elements for each control chain */
72 #define AUCH_ELEMENTS 20
74 /* Number of retries in communication */
77 /*-------------------------------------------------------------------*/
78 /* vendor specific protocol */
80 #define AUH_INDIRMASK 0x80 /* mask for direct/indirect bit */
81 #define AUH_DIRECT 0x00 /* data is for USB device */
82 #define AUH_INDIRECT 0x80 /* USB device is relay */
84 #define AUH_SPLITMASK 0x40 /* mask for split bit */
85 #define AUH_UNSPLIT 0x00 /* data block is full-size */
86 #define AUH_SPLIT 0x40 /* data block is part of a larger one,
89 #define AUH_TYPEMASK 0x3F /* mask for type of data transfer */
90 #define AUH_TYPESIZE 0x40 /* different types */
91 #define AUH_DCHANNEL 0x00 /* D channel data */
92 #define AUH_B1CHANNEL 0x01 /* B1 channel transparent */
93 #define AUH_B2CHANNEL 0x02 /* B2 channel transparent */
94 /* 0x03..0x0F reserved for driver internal use */
95 #define AUH_COMMAND 0x10 /* Command channel */
96 #define AUH_BPROT 0x11 /* Configuration block protocol */
97 #define AUH_DPROTANA 0x12 /* D channel protocol analyzer */
98 #define AUH_TAPI 0x13 /* telephone api data (ATD) */
99 /* 0x14..0x3F reserved for other protocols */
100 #define AUH_UNASSIGNED 0xFF /* if char device has no assigned service */
101 #define AUH_FIRSTUSERCH 0x11 /* first channel which is available for driver users */
103 #define AUH_SIZE 1 /* Size of Header Byte */
105 /* Split Byte. Only present if split bit in header byte set.*/
106 #define AUS_STARTMASK 0x80 /* mask for first block of splitted frame */
107 #define AUS_FIRST 0x80 /* first block */
108 #define AUS_FOLLOW 0x00 /* following block */
110 #define AUS_ENDMASK 0x40 /* mask for last block of splitted frame */
111 #define AUS_END 0x40 /* last block */
112 #define AUS_NOEND 0x00 /* not the last block */
114 #define AUS_LENMASK 0x3F /* mask for block length information */
117 #define AUT_RREQ (USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_OTHER) /* Read Request */
118 #define AUT_WREQ (USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER) /* Write Request */
120 /* Vendor Requests */
121 #define AUV_GETINFO 0x00 /* GetDeviceInfo */
122 #define AUV_WBLOCK 0x01 /* Write Block */
123 #define AUV_RBLOCK 0x02 /* Read Block */
124 #define AUV_CHANNELCTL 0x03 /* Channel Control */
125 #define AUV_DUMMY 0x04 /* Dummy Out for retry */
127 /* Device Info Types */
128 #define AUDI_NUMBCH 0x0000 /* Number of supported B channels */
129 #define AUDI_OUTFSIZE 0x0001 /* Size of OUT B channel fifos */
130 #define AUDI_MBCTRANS 0x0002 /* max. Blocklength of control transfer */
132 /* Interrupt endpoint definitions */
133 #define AU_IRQENDP 1 /* Endpoint number */
134 #define AU_IRQCMDID 16 /* Command-block ID */
135 #define AU_BLOCKRDY 0 /* Command: Block data ready on ctl endpoint */
136 #define AU_IRQMINSIZE 5 /* Nr. of bytes decoded in this driver */
138 /* Device String Descriptors */
139 #define AUSI_VENDOR 1 /* "Auerswald GmbH & Co. KG" */
140 #define AUSI_DEVICE 2 /* Name of the Device */
141 #define AUSI_SERIALNR 3 /* Serial Number */
142 #define AUSI_MSN 4 /* "MSN ..." (first) Multiple Subscriber Number */
144 #define AUSI_DLEN 100 /* Max. Length of Device Description */
146 #define AUV_RETRY 0x101 /* First Firmware version which can do control retries */
148 /*-------------------------------------------------------------------*/
149 /* External data structures / Interface */
152 char __user *buf; /* return buffer for string contents */
153 unsigned int bsize; /* size of return buffer */
154 } audevinfo_t,*paudevinfo_t;
157 #define IOCTL_AU_SLEN _IOR( 'U', 0xF0, int) /* return the max. string descriptor length */
158 #define IOCTL_AU_DEVINFO _IOWR('U', 0xF1, audevinfo_t) /* get name of a specific device */
159 #define IOCTL_AU_SERVREQ _IOW( 'U', 0xF2, int) /* request a service channel */
160 #define IOCTL_AU_BUFLEN _IOR( 'U', 0xF3, int) /* return the max. buffer length for the device */
161 #define IOCTL_AU_RXAVAIL _IOR( 'U', 0xF4, int) /* return != 0 if Receive Data available */
162 #define IOCTL_AU_CONNECT _IOR( 'U', 0xF5, int) /* return != 0 if connected to a service channel */
163 #define IOCTL_AU_TXREADY _IOR( 'U', 0xF6, int) /* return != 0 if Transmitt channel ready to send */
164 /* 'U' 0xF7..0xFF reseved */
166 /*-------------------------------------------------------------------*/
167 /* Internal data structures */
169 /* ..................................................................*/
170 /* urb chain element */
171 struct auerchain; /* forward for circular reference */
174 struct auerchain *chain; /* pointer to the chain to which this element belongs */
175 struct urb * urbp; /* pointer to attached urb */
176 void *context; /* saved URB context */
177 usb_complete_t complete; /* saved URB completion function */
178 struct list_head list; /* to include element into a list */
179 } auerchainelement_t,*pauerchainelement_t;
182 typedef struct auerchain
184 pauerchainelement_t active; /* element which is submitted to urb */
185 spinlock_t lock; /* protection agains interrupts */
186 struct list_head waiting_list; /* list of waiting elements */
187 struct list_head free_list; /* list of available elements */
188 } auerchain_t,*pauerchain_t;
190 /* urb blocking completion helper struct */
193 wait_queue_head_t wqh; /* wait for completion */
194 unsigned int done; /* completion flag */
195 } auerchain_chs_t,*pauerchain_chs_t;
197 /* ...................................................................*/
199 struct auerbufctl; /* forward */
202 char *bufp; /* reference to allocated data buffer */
203 unsigned int len; /* number of characters in data buffer */
204 unsigned int retries; /* for urb retries */
205 struct usb_ctrlrequest *dr; /* for setup data in control messages */
206 struct urb * urbp; /* USB urb */
207 struct auerbufctl *list; /* pointer to list */
208 struct list_head buff_list; /* reference to next buffer in list */
209 } auerbuf_t,*pauerbuf_t;
211 /* buffer list control block */
212 typedef struct auerbufctl
214 spinlock_t lock; /* protection in interrupt */
215 struct list_head free_buff_list;/* free buffers */
216 struct list_head rec_buff_list; /* buffers with receive data */
217 } auerbufctl_t,*pauerbufctl_t;
219 /* ...................................................................*/
220 /* service context */
221 struct auerscon; /* forward */
222 typedef void (*auer_dispatch_t)(struct auerscon*, pauerbuf_t);
223 typedef void (*auer_disconn_t) (struct auerscon*);
224 typedef struct auerscon
226 unsigned int id; /* protocol service id AUH_xxxx */
227 auer_dispatch_t dispatch; /* dispatch read buffer */
228 auer_disconn_t disconnect; /* disconnect from device, wake up all char readers */
229 } auerscon_t,*pauerscon_t;
231 /* ...................................................................*/
232 /* USB device context */
235 struct semaphore mutex; /* protection in user context */
236 char name[20]; /* name of the /dev/usb entry */
237 unsigned int dtindex; /* index in the device table */
238 struct usb_device * usbdev; /* USB device handle */
239 int open_count; /* count the number of open character channels */
240 char dev_desc[AUSI_DLEN];/* for storing a textual description */
241 unsigned int maxControlLength; /* max. Length of control paket (without header) */
242 struct urb * inturbp; /* interrupt urb */
243 char * intbufp; /* data buffer for interrupt urb */
244 unsigned int irqsize; /* size of interrupt endpoint 1 */
245 struct auerchain controlchain; /* for chaining of control messages */
246 auerbufctl_t bufctl; /* Buffer control for control transfers */
247 pauerscon_t services[AUH_TYPESIZE];/* context pointers for each service */
248 unsigned int version; /* Version of the device */
249 wait_queue_head_t bufferwait; /* wait for a control buffer */
250 } auerswald_t,*pauerswald_t;
252 /* ................................................................... */
253 /* character device context */
256 struct semaphore mutex; /* protection in user context */
257 pauerswald_t auerdev; /* context pointer of assigned device */
258 auerbufctl_t bufctl; /* controls the buffer chain */
259 auerscon_t scontext; /* service context */
260 wait_queue_head_t readwait; /* for synchronous reading */
261 struct semaphore readmutex; /* protection against multiple reads */
262 pauerbuf_t readbuf; /* buffer held for partial reading */
263 unsigned int readoffset; /* current offset in readbuf */
264 unsigned int removed; /* is != 0 if device is removed */
265 } auerchar_t,*pauerchar_t;
268 /*-------------------------------------------------------------------*/
270 static void auerswald_ctrlread_complete (struct urb * urb, struct pt_regs *regs);
271 static void auerswald_removeservice (pauerswald_t cp, pauerscon_t scp);
272 static struct usb_driver auerswald_driver;
275 /*-------------------------------------------------------------------*/
276 /* USB chain helper functions */
277 /* -------------------------- */
279 /* completion function for chained urbs */
280 static void auerchain_complete (struct urb * urb, struct pt_regs *regs)
285 /* get pointer to element and to chain */
286 pauerchainelement_t acep = (pauerchainelement_t) urb->context;
287 pauerchain_t acp = acep->chain;
289 /* restore original entries in urb */
290 urb->context = acep->context;
291 urb->complete = acep->complete;
293 dbg ("auerchain_complete called");
295 /* call original completion function
296 NOTE: this function may lead to more urbs submitted into the chain.
297 (no chain lock at calling complete()!)
298 acp->active != NULL is protecting us against recursion.*/
299 urb->complete (urb, regs);
301 /* detach element from chain data structure */
302 spin_lock_irqsave (&acp->lock, flags);
303 if (acp->active != acep) /* paranoia debug check */
304 dbg ("auerchain_complete: completion on non-active element called!");
308 /* add the used chain element to the list of free elements */
309 list_add_tail (&acep->list, &acp->free_list);
312 /* is there a new element waiting in the chain? */
313 if (!acp->active && !list_empty (&acp->waiting_list)) {
314 /* yes: get the entry */
315 struct list_head *tmp = acp->waiting_list.next;
317 acep = list_entry (tmp, auerchainelement_t, list);
320 spin_unlock_irqrestore (&acp->lock, flags);
322 /* submit the new urb */
325 dbg ("auerchain_complete: submitting next urb from chain");
326 urb->status = 0; /* needed! */
327 result = usb_submit_urb(urb, GFP_ATOMIC);
329 /* check for submit errors */
331 urb->status = result;
332 dbg("auerchain_complete: usb_submit_urb with error code %d", result);
333 /* and do error handling via *this* completion function (recursive) */
334 auerchain_complete( urb, NULL);
337 /* simple return without submitting a new urb.
338 The empty chain is detected with acp->active == NULL. */
343 /* submit function for chained urbs
344 this function may be called from completion context or from user space!
345 early = 1 -> submit in front of chain
347 static int auerchain_submit_urb_list (pauerchain_t acp, struct urb * urb, int early)
351 pauerchainelement_t acep = NULL;
353 dbg ("auerchain_submit_urb called");
355 /* try to get a chain element */
356 spin_lock_irqsave (&acp->lock, flags);
357 if (!list_empty (&acp->free_list)) {
358 /* yes: get the entry */
359 struct list_head *tmp = acp->free_list.next;
361 acep = list_entry (tmp, auerchainelement_t, list);
363 spin_unlock_irqrestore (&acp->lock, flags);
365 /* if no chain element available: return with error */
370 /* fill in the new chain element values */
372 acep->context = urb->context;
373 acep->complete = urb->complete;
375 INIT_LIST_HEAD (&acep->list);
379 urb->complete = auerchain_complete;
380 urb->status = -EINPROGRESS; /* usb_submit_urb does this, too */
382 /* add element to chain - or start it immediately */
383 spin_lock_irqsave (&acp->lock, flags);
385 /* there is traffic in the chain, simple add element to chain */
387 dbg ("adding new urb to head of chain");
388 list_add (&acep->list, &acp->waiting_list);
390 dbg ("adding new urb to end of chain");
391 list_add_tail (&acep->list, &acp->waiting_list);
395 /* the chain is empty. Prepare restart */
398 /* Spin has to be removed before usb_submit_urb! */
399 spin_unlock_irqrestore (&acp->lock, flags);
401 /* Submit urb if immediate restart */
403 dbg("submitting urb immediate");
404 urb->status = 0; /* needed! */
405 result = usb_submit_urb(urb, GFP_ATOMIC);
406 /* check for submit errors */
408 urb->status = result;
409 dbg("auerchain_submit_urb: usb_submit_urb with error code %d", result);
410 /* and do error handling via completion function */
411 auerchain_complete( urb, NULL);
418 /* submit function for chained urbs
419 this function may be called from completion context or from user space!
421 static int auerchain_submit_urb (pauerchain_t acp, struct urb * urb)
423 return auerchain_submit_urb_list (acp, urb, 0);
426 /* cancel an urb which is submitted to the chain
427 the result is 0 if the urb is cancelled, or -EINPROGRESS if
428 URB_ASYNC_UNLINK is set and the function is successfully started.
430 static int auerchain_unlink_urb (pauerchain_t acp, struct urb * urb)
434 pauerchainelement_t acep;
435 struct list_head *tmp;
437 dbg ("auerchain_unlink_urb called");
439 /* search the chain of waiting elements */
440 spin_lock_irqsave (&acp->lock, flags);
441 list_for_each (tmp, &acp->waiting_list) {
442 acep = list_entry (tmp, auerchainelement_t, list);
443 if (acep->urbp == urb) {
445 urb->context = acep->context;
446 urb->complete = acep->complete;
447 list_add_tail (&acep->list, &acp->free_list);
448 spin_unlock_irqrestore (&acp->lock, flags);
449 dbg ("unlink waiting urb");
450 urb->status = -ENOENT;
451 urb->complete (urb, NULL);
456 spin_unlock_irqrestore (&acp->lock, flags);
458 /* get the active urb */
463 /* check if we have to cancel the active urb */
465 /* note that there is a race condition between the check above
466 and the unlink() call because of no lock. This race is harmless,
467 because the usb module will detect the unlink() after completion.
468 We can't use the acp->lock here because the completion function
471 dbg ("unlink active urb");
472 return usb_unlink_urb (urbp);
477 ... is some kind of success
479 dbg ("urb to unlink not found in chain");
483 /* cancel all urbs which are in the chain.
484 this function must not be called from interrupt or completion handler.
486 static void auerchain_unlink_all (pauerchain_t acp)
490 pauerchainelement_t acep;
492 dbg ("auerchain_unlink_all called");
494 /* clear the chain of waiting elements */
495 spin_lock_irqsave (&acp->lock, flags);
496 while (!list_empty (&acp->waiting_list)) {
497 /* get the next entry */
498 struct list_head *tmp = acp->waiting_list.next;
500 acep = list_entry (tmp, auerchainelement_t, list);
502 urbp->context = acep->context;
503 urbp->complete = acep->complete;
504 list_add_tail (&acep->list, &acp->free_list);
505 spin_unlock_irqrestore (&acp->lock, flags);
506 dbg ("unlink waiting urb");
507 urbp->status = -ENOENT;
508 urbp->complete (urbp, NULL);
509 spin_lock_irqsave (&acp->lock, flags);
511 spin_unlock_irqrestore (&acp->lock, flags);
513 /* clear the active urb */
517 urbp->transfer_flags &= ~URB_ASYNC_UNLINK;
518 dbg ("unlink active urb");
525 this function must not be called from interrupt or completion handler.
527 static void auerchain_free (pauerchain_t acp)
530 pauerchainelement_t acep;
532 dbg ("auerchain_free called");
534 /* first, cancel all pending urbs */
535 auerchain_unlink_all (acp);
537 /* free the elements */
538 spin_lock_irqsave (&acp->lock, flags);
539 while (!list_empty (&acp->free_list)) {
540 /* get the next entry */
541 struct list_head *tmp = acp->free_list.next;
543 spin_unlock_irqrestore (&acp->lock, flags);
544 acep = list_entry (tmp, auerchainelement_t, list);
546 spin_lock_irqsave (&acp->lock, flags);
548 spin_unlock_irqrestore (&acp->lock, flags);
552 /* Init the chain control structure */
553 static void auerchain_init (pauerchain_t acp)
555 /* init the chain data structure */
557 spin_lock_init (&acp->lock);
558 INIT_LIST_HEAD (&acp->waiting_list);
559 INIT_LIST_HEAD (&acp->free_list);
563 It is assumed that there is no concurrency while setting up the chain
564 requirement: auerchain_init()
566 static int auerchain_setup (pauerchain_t acp, unsigned int numElements)
568 pauerchainelement_t acep;
570 dbg ("auerchain_setup called with %d elements", numElements);
572 /* fill the list of free elements */
573 for (;numElements; numElements--) {
574 acep = (pauerchainelement_t) kmalloc (sizeof (auerchainelement_t), GFP_KERNEL);
577 memset (acep, 0, sizeof (auerchainelement_t));
578 INIT_LIST_HEAD (&acep->list);
579 list_add_tail (&acep->list, &acp->free_list);
583 ac_fail:/* free the elements */
584 while (!list_empty (&acp->free_list)) {
585 /* get the next entry */
586 struct list_head *tmp = acp->free_list.next;
588 acep = list_entry (tmp, auerchainelement_t, list);
595 /* completion handler for synchronous chained URBs */
596 static void auerchain_blocking_completion (struct urb *urb, struct pt_regs *regs)
598 pauerchain_chs_t pchs = (pauerchain_chs_t)urb->context;
601 wake_up (&pchs->wqh);
605 /* Starts chained urb and waits for completion or timeout */
606 static int auerchain_start_wait_urb (pauerchain_t acp, struct urb *urb, int timeout, int* actual_length)
608 DECLARE_WAITQUEUE (wait, current);
612 dbg ("auerchain_start_wait_urb called");
613 init_waitqueue_head (&chs.wqh);
616 set_current_state (TASK_UNINTERRUPTIBLE);
617 add_wait_queue (&chs.wqh, &wait);
619 status = auerchain_submit_urb (acp, urb);
621 /* something went wrong */
622 set_current_state (TASK_RUNNING);
623 remove_wait_queue (&chs.wqh, &wait);
627 while (timeout && !chs.done)
629 timeout = schedule_timeout (timeout);
630 set_current_state(TASK_UNINTERRUPTIBLE);
634 set_current_state (TASK_RUNNING);
635 remove_wait_queue (&chs.wqh, &wait);
637 if (!timeout && !chs.done) {
638 if (urb->status != -EINPROGRESS) { /* No callback?!! */
639 dbg ("auerchain_start_wait_urb: raced timeout");
640 status = urb->status;
642 dbg ("auerchain_start_wait_urb: timeout");
643 auerchain_unlink_urb (acp, urb); /* remove urb safely */
647 status = urb->status;
650 *actual_length = urb->actual_length;
656 /* auerchain_control_msg - Builds a control urb, sends it off and waits for completion
657 acp: pointer to the auerchain
658 dev: pointer to the usb device to send the message to
659 pipe: endpoint "pipe" to send the message to
660 request: USB message request value
661 requesttype: USB message request type value
662 value: USB message value
663 index: USB message index value
664 data: pointer to the data to send
665 size: length in bytes of the data to send
666 timeout: time to wait for the message to complete before timing out (if 0 the wait is forever)
668 This function sends a simple control message to a specified endpoint
669 and waits for the message to complete, or timeout.
671 If successful, it returns the transferred length, otherwise a negative error number.
673 Don't use this function from within an interrupt context, like a
674 bottom half handler. If you need an asynchronous message, or need to send
675 a message from within interrupt context, use auerchain_submit_urb()
677 static int auerchain_control_msg (pauerchain_t acp, struct usb_device *dev, unsigned int pipe, __u8 request, __u8 requesttype,
678 __u16 value, __u16 index, void *data, __u16 size, int timeout)
681 struct usb_ctrlrequest *dr;
685 dbg ("auerchain_control_msg");
686 dr = kmalloc (sizeof (struct usb_ctrlrequest), GFP_KERNEL);
689 urb = usb_alloc_urb (0, GFP_KERNEL);
695 dr->bRequestType = requesttype;
696 dr->bRequest = request;
697 dr->wValue = cpu_to_le16 (value);
698 dr->wIndex = cpu_to_le16 (index);
699 dr->wLength = cpu_to_le16 (size);
701 usb_fill_control_urb (urb, dev, pipe, (unsigned char*)dr, data, size, /* build urb */
702 auerchain_blocking_completion, NULL);
703 ret = auerchain_start_wait_urb (acp, urb, timeout, &length);
715 /*-------------------------------------------------------------------*/
716 /* Buffer List helper functions */
718 /* free a single auerbuf */
719 static void auerbuf_free (pauerbuf_t bp)
728 usb_free_urb (bp->urbp);
733 /* free the buffers from an auerbuf list */
734 static void auerbuf_free_list (struct list_head *q)
736 struct list_head *tmp;
740 dbg ("auerbuf_free_list");
741 for (p = q->next; p != q;) {
742 bp = list_entry (p, auerbuf_t, buff_list);
750 /* init the members of a list control block */
751 static void auerbuf_init (pauerbufctl_t bcp)
753 dbg ("auerbuf_init");
754 spin_lock_init (&bcp->lock);
755 INIT_LIST_HEAD (&bcp->free_buff_list);
756 INIT_LIST_HEAD (&bcp->rec_buff_list);
759 /* free all buffers from an auerbuf chain */
760 static void auerbuf_free_buffers (pauerbufctl_t bcp)
763 dbg ("auerbuf_free_buffers");
765 spin_lock_irqsave (&bcp->lock, flags);
767 auerbuf_free_list (&bcp->free_buff_list);
768 auerbuf_free_list (&bcp->rec_buff_list);
770 spin_unlock_irqrestore (&bcp->lock, flags);
773 /* setup a list of buffers */
774 /* requirement: auerbuf_init() */
775 static int auerbuf_setup (pauerbufctl_t bcp, unsigned int numElements, unsigned int bufsize)
777 pauerbuf_t bep = NULL;
779 dbg ("auerbuf_setup called with %d elements of %d bytes", numElements, bufsize);
781 /* fill the list of free elements */
782 for (;numElements; numElements--) {
783 bep = (pauerbuf_t) kmalloc (sizeof (auerbuf_t), GFP_KERNEL);
786 memset (bep, 0, sizeof (auerbuf_t));
788 INIT_LIST_HEAD (&bep->buff_list);
789 bep->bufp = (char *) kmalloc (bufsize, GFP_KERNEL);
792 bep->dr = (struct usb_ctrlrequest *) kmalloc (sizeof (struct usb_ctrlrequest), GFP_KERNEL);
795 bep->urbp = usb_alloc_urb (0, GFP_KERNEL);
798 list_add_tail (&bep->buff_list, &bcp->free_buff_list);
802 bl_fail:/* not enough memory. Free allocated elements */
803 dbg ("auerbuf_setup: no more memory");
805 auerbuf_free_buffers (bcp);
809 /* insert a used buffer into the free list */
810 static void auerbuf_releasebuf( pauerbuf_t bp)
813 pauerbufctl_t bcp = bp->list;
816 dbg ("auerbuf_releasebuf called");
817 spin_lock_irqsave (&bcp->lock, flags);
818 list_add_tail (&bp->buff_list, &bcp->free_buff_list);
819 spin_unlock_irqrestore (&bcp->lock, flags);
823 /*-------------------------------------------------------------------*/
824 /* Completion handlers */
826 /* Values of urb->status or results of usb_submit_urb():
828 -EINPROGRESS during submission until end
829 -ENOENT if urb is unlinked
830 -ETIMEDOUT Transfer timed out, NAK
831 -ENOMEM Memory Overflow
832 -ENODEV Specified USB-device or bus doesn't exist
833 -ENXIO URB already queued
834 -EINVAL a) Invalid transfer type specified (or not supported)
835 b) Invalid interrupt interval (0n256)
836 -EAGAIN a) Specified ISO start frame too early
837 b) (using ISO-ASAP) Too much scheduled for the future wait some time and try again.
838 -EFBIG Too much ISO frames requested (currently uhci900)
839 -EPIPE Specified pipe-handle/Endpoint is already stalled
840 -EMSGSIZE Endpoint message size is zero, do interface/alternate setting
841 -EPROTO a) Bitstuff error
845 -EREMOTEIO Short packet detected
846 -EXDEV ISO transfer only partially completed look at individual frame status for details
847 -EINVAL ISO madness, if this happens: Log off and go home
851 /* check if a status code allows a retry */
852 static int auerswald_status_retry (int status)
864 return 1; /* do a retry */
866 return 0; /* no retry possible */
869 /* Completion of asynchronous write block */
870 static void auerchar_ctrlwrite_complete (struct urb * urb, struct pt_regs *regs)
872 pauerbuf_t bp = (pauerbuf_t) urb->context;
873 pauerswald_t cp = ((pauerswald_t)((char *)(bp->list)-(unsigned long)(&((pauerswald_t)0)->bufctl)));
874 dbg ("auerchar_ctrlwrite_complete called");
876 /* reuse the buffer */
877 auerbuf_releasebuf (bp);
878 /* Wake up all processes waiting for a buffer */
879 wake_up (&cp->bufferwait);
882 /* Completion handler for dummy retry packet */
883 static void auerswald_ctrlread_wretcomplete (struct urb * urb, struct pt_regs *regs)
885 pauerbuf_t bp = (pauerbuf_t) urb->context;
888 dbg ("auerswald_ctrlread_wretcomplete called");
889 dbg ("complete with status: %d", urb->status);
890 cp = ((pauerswald_t)((char *)(bp->list)-(unsigned long)(&((pauerswald_t)0)->bufctl)));
892 /* check if it is possible to advance */
893 if (!auerswald_status_retry (urb->status) || !cp->usbdev) {
894 /* reuse the buffer */
895 err ("control dummy: transmission error %d, can not retry", urb->status);
896 auerbuf_releasebuf (bp);
897 /* Wake up all processes waiting for a buffer */
898 wake_up (&cp->bufferwait);
902 /* fill the control message */
903 bp->dr->bRequestType = AUT_RREQ;
904 bp->dr->bRequest = AUV_RBLOCK;
905 bp->dr->wLength = bp->dr->wValue; /* temporary stored */
906 bp->dr->wValue = cpu_to_le16 (1); /* Retry Flag */
907 /* bp->dr->index = channel id; remains */
908 usb_fill_control_urb (bp->urbp, cp->usbdev, usb_rcvctrlpipe (cp->usbdev, 0),
909 (unsigned char*)bp->dr, bp->bufp, le16_to_cpu (bp->dr->wLength),
910 auerswald_ctrlread_complete,bp);
912 /* submit the control msg as next paket */
913 ret = auerchain_submit_urb_list (&cp->controlchain, bp->urbp, 1);
915 dbg ("auerswald_ctrlread_complete: nonzero result of auerchain_submit_urb_list %d", ret);
916 bp->urbp->status = ret;
917 auerswald_ctrlread_complete (bp->urbp, NULL);
921 /* completion handler for receiving of control messages */
922 static void auerswald_ctrlread_complete (struct urb * urb, struct pt_regs *regs)
924 unsigned int serviceid;
927 pauerbuf_t bp = (pauerbuf_t) urb->context;
929 dbg ("auerswald_ctrlread_complete called");
931 cp = ((pauerswald_t)((char *)(bp->list)-(unsigned long)(&((pauerswald_t)0)->bufctl)));
933 /* check if there is valid data in this urb */
935 dbg ("complete with non-zero status: %d", urb->status);
936 /* should we do a retry? */
937 if (!auerswald_status_retry (urb->status)
939 || (cp->version < AUV_RETRY)
940 || (bp->retries >= AU_RETRIES)) {
941 /* reuse the buffer */
942 err ("control read: transmission error %d, can not retry", urb->status);
943 auerbuf_releasebuf (bp);
944 /* Wake up all processes waiting for a buffer */
945 wake_up (&cp->bufferwait);
949 dbg ("Retry count = %d", bp->retries);
950 /* send a long dummy control-write-message to allow device firmware to react */
951 bp->dr->bRequestType = AUT_WREQ;
952 bp->dr->bRequest = AUV_DUMMY;
953 bp->dr->wValue = bp->dr->wLength; /* temporary storage */
954 // bp->dr->wIndex channel ID remains
955 bp->dr->wLength = cpu_to_le16 (32); /* >= 8 bytes */
956 usb_fill_control_urb (bp->urbp, cp->usbdev, usb_sndctrlpipe (cp->usbdev, 0),
957 (unsigned char*)bp->dr, bp->bufp, 32,
958 auerswald_ctrlread_wretcomplete,bp);
960 /* submit the control msg as next paket */
961 ret = auerchain_submit_urb_list (&cp->controlchain, bp->urbp, 1);
963 dbg ("auerswald_ctrlread_complete: nonzero result of auerchain_submit_urb_list %d", ret);
964 bp->urbp->status = ret;
965 auerswald_ctrlread_wretcomplete (bp->urbp, regs);
970 /* get the actual bytecount (incl. headerbyte) */
971 bp->len = urb->actual_length;
972 serviceid = bp->bufp[0] & AUH_TYPEMASK;
973 dbg ("Paket with serviceid %d and %d bytes received", serviceid, bp->len);
975 /* dispatch the paket */
976 scp = cp->services[serviceid];
978 /* look, Ma, a listener! */
979 scp->dispatch (scp, bp);
982 /* release the paket */
983 auerbuf_releasebuf (bp);
984 /* Wake up all processes waiting for a buffer */
985 wake_up (&cp->bufferwait);
988 /*-------------------------------------------------------------------*/
989 /* Handling of Interrupt Endpoint */
990 /* This interrupt Endpoint is used to inform the host about waiting
991 messages from the USB device.
993 /* int completion handler. */
994 static void auerswald_int_complete (struct urb * urb, struct pt_regs *regs)
997 unsigned int channelid;
998 unsigned int bytecount;
1000 pauerbuf_t bp = NULL;
1001 pauerswald_t cp = (pauerswald_t) urb->context;
1003 dbg ("%s called", __FUNCTION__);
1005 switch (urb->status) {
1012 /* this urb is terminated, clean up */
1013 dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status);
1016 dbg("%s - nonzero urb status received: %d", __FUNCTION__, urb->status);
1020 /* check if all needed data was received */
1021 if (urb->actual_length < AU_IRQMINSIZE) {
1022 dbg ("invalid data length received: %d bytes", urb->actual_length);
1026 /* check the command code */
1027 if (cp->intbufp[0] != AU_IRQCMDID) {
1028 dbg ("invalid command received: %d", cp->intbufp[0]);
1032 /* check the command type */
1033 if (cp->intbufp[1] != AU_BLOCKRDY) {
1034 dbg ("invalid command type received: %d", cp->intbufp[1]);
1038 /* now extract the information */
1039 channelid = cp->intbufp[2];
1040 bytecount = (unsigned char)cp->intbufp[3];
1041 bytecount |= (unsigned char)cp->intbufp[4] << 8;
1043 /* check the channel id */
1044 if (channelid >= AUH_TYPESIZE) {
1045 dbg ("invalid channel id received: %d", channelid);
1049 /* check the byte count */
1050 if (bytecount > (cp->maxControlLength+AUH_SIZE)) {
1051 dbg ("invalid byte count received: %d", bytecount);
1054 dbg ("Service Channel = %d", channelid);
1055 dbg ("Byte Count = %d", bytecount);
1057 /* get a buffer for the next data paket */
1058 spin_lock_irqsave (&cp->bufctl.lock, flags);
1059 if (!list_empty (&cp->bufctl.free_buff_list)) {
1060 /* yes: get the entry */
1061 struct list_head *tmp = cp->bufctl.free_buff_list.next;
1063 bp = list_entry (tmp, auerbuf_t, buff_list);
1065 spin_unlock_irqrestore (&cp->bufctl.lock, flags);
1067 /* if no buffer available: skip it */
1069 dbg ("auerswald_int_complete: no data buffer available");
1070 /* can we do something more?
1071 This is a big problem: if this int packet is ignored, the
1072 device will wait forever and not signal any more data.
1073 The only real solution is: having enough buffers!
1074 Or perhaps temporary disabling the int endpoint?
1079 /* fill the control message */
1080 bp->dr->bRequestType = AUT_RREQ;
1081 bp->dr->bRequest = AUV_RBLOCK;
1082 bp->dr->wValue = cpu_to_le16 (0);
1083 bp->dr->wIndex = cpu_to_le16 (channelid | AUH_DIRECT | AUH_UNSPLIT);
1084 bp->dr->wLength = cpu_to_le16 (bytecount);
1085 usb_fill_control_urb (bp->urbp, cp->usbdev, usb_rcvctrlpipe (cp->usbdev, 0),
1086 (unsigned char*)bp->dr, bp->bufp, bytecount,
1087 auerswald_ctrlread_complete,bp);
1089 /* submit the control msg */
1090 ret = auerchain_submit_urb (&cp->controlchain, bp->urbp);
1092 dbg ("auerswald_int_complete: nonzero result of auerchain_submit_urb %d", ret);
1093 bp->urbp->status = ret;
1094 auerswald_ctrlread_complete( bp->urbp, NULL);
1095 /* here applies the same problem as above: device locking! */
1098 ret = usb_submit_urb (urb, GFP_ATOMIC);
1100 err ("%s - usb_submit_urb failed with result %d",
1104 /* int memory deallocation
1105 NOTE: no mutex please!
1107 static void auerswald_int_free (pauerswald_t cp)
1110 usb_free_urb (cp->inturbp);
1114 kfree (cp->intbufp);
1119 /* This function is called to activate the interrupt
1120 endpoint. This function returns 0 if successful or an error code.
1121 NOTE: no mutex please!
1123 static int auerswald_int_open (pauerswald_t cp)
1126 struct usb_endpoint_descriptor *ep;
1128 dbg ("auerswald_int_open");
1130 ep = usb_epnum_to_ep_desc (cp->usbdev, USB_DIR_IN | AU_IRQENDP);
1135 irqsize = ep->wMaxPacketSize;
1136 cp->irqsize = irqsize;
1138 /* allocate the urb and data buffer */
1140 cp->inturbp = usb_alloc_urb (0, GFP_KERNEL);
1147 cp->intbufp = (char *) kmalloc (irqsize, GFP_KERNEL);
1154 usb_fill_int_urb (cp->inturbp, cp->usbdev, usb_rcvintpipe (cp->usbdev,AU_IRQENDP), cp->intbufp, irqsize, auerswald_int_complete, cp, ep->bInterval);
1156 cp->inturbp->status = 0; /* needed! */
1157 ret = usb_submit_urb (cp->inturbp, GFP_KERNEL);
1161 /* activation of interrupt endpoint has failed. Now clean up. */
1162 dbg ("auerswald_int_open: activation of int endpoint failed");
1164 /* deallocate memory */
1165 auerswald_int_free (cp);
1170 /* This function is called to deactivate the interrupt
1171 endpoint. This function returns 0 if successful or an error code.
1172 NOTE: no mutex please!
1174 static void auerswald_int_release (pauerswald_t cp)
1176 dbg ("auerswald_int_release");
1178 /* stop the int endpoint */
1180 usb_kill_urb (cp->inturbp);
1182 /* deallocate memory */
1183 auerswald_int_free (cp);
1186 /* --------------------------------------------------------------------- */
1187 /* Helper functions */
1189 /* wake up waiting readers */
1190 static void auerchar_disconnect (pauerscon_t scp)
1192 pauerchar_t ccp = ((pauerchar_t)((char *)(scp)-(unsigned long)(&((pauerchar_t)0)->scontext)));
1193 dbg ("auerchar_disconnect called");
1195 wake_up (&ccp->readwait);
1199 /* dispatch a read paket to a waiting character device */
1200 static void auerchar_ctrlread_dispatch (pauerscon_t scp, pauerbuf_t bp)
1202 unsigned long flags;
1204 pauerbuf_t newbp = NULL;
1206 dbg ("auerchar_ctrlread_dispatch called");
1207 ccp = ((pauerchar_t)((char *)(scp)-(unsigned long)(&((pauerchar_t)0)->scontext)));
1209 /* get a read buffer from character device context */
1210 spin_lock_irqsave (&ccp->bufctl.lock, flags);
1211 if (!list_empty (&ccp->bufctl.free_buff_list)) {
1212 /* yes: get the entry */
1213 struct list_head *tmp = ccp->bufctl.free_buff_list.next;
1215 newbp = list_entry (tmp, auerbuf_t, buff_list);
1217 spin_unlock_irqrestore (&ccp->bufctl.lock, flags);
1220 dbg ("No read buffer available, discard paket!");
1221 return; /* no buffer, no dispatch */
1224 /* copy information to new buffer element
1225 (all buffers have the same length) */
1226 charp = newbp->bufp;
1227 newbp->bufp = bp->bufp;
1229 newbp->len = bp->len;
1231 /* insert new buffer in read list */
1232 spin_lock_irqsave (&ccp->bufctl.lock, flags);
1233 list_add_tail (&newbp->buff_list, &ccp->bufctl.rec_buff_list);
1234 spin_unlock_irqrestore (&ccp->bufctl.lock, flags);
1235 dbg ("read buffer appended to rec_list");
1237 /* wake up pending synchronous reads */
1238 wake_up (&ccp->readwait);
1242 /* Delete an auerswald driver context */
1243 static void auerswald_delete( pauerswald_t cp)
1245 dbg( "auerswald_delete");
1249 /* Wake up all processes waiting for a buffer */
1250 wake_up (&cp->bufferwait);
1253 auerswald_int_release (cp);
1254 auerchain_free (&cp->controlchain);
1255 auerbuf_free_buffers (&cp->bufctl);
1257 /* release the memory */
1262 /* Delete an auerswald character context */
1263 static void auerchar_delete( pauerchar_t ccp)
1265 dbg ("auerchar_delete");
1269 /* wake up pending synchronous reads */
1271 wake_up (&ccp->readwait);
1273 /* remove the read buffer */
1275 auerbuf_releasebuf (ccp->readbuf);
1276 ccp->readbuf = NULL;
1279 /* remove the character buffers */
1280 auerbuf_free_buffers (&ccp->bufctl);
1282 /* release the memory */
1287 /* add a new service to the device
1288 scp->id must be set!
1289 return: 0 if OK, else error code
1291 static int auerswald_addservice (pauerswald_t cp, pauerscon_t scp)
1295 /* is the device available? */
1297 dbg ("usbdev == NULL");
1298 return -EIO; /*no: can not add a service, sorry*/
1301 /* is the service available? */
1302 if (cp->services[scp->id]) {
1303 dbg ("service is busy");
1307 /* device is available, service is free */
1308 cp->services[scp->id] = scp;
1310 /* register service in device */
1311 ret = auerchain_control_msg(
1312 &cp->controlchain, /* pointer to control chain */
1313 cp->usbdev, /* pointer to device */
1314 usb_sndctrlpipe (cp->usbdev, 0), /* pipe to control endpoint */
1315 AUV_CHANNELCTL, /* USB message request value */
1316 AUT_WREQ, /* USB message request type value */
1317 0x01, /* open USB message value */
1318 scp->id, /* USB message index value */
1319 NULL, /* pointer to the data to send */
1320 0, /* length in bytes of the data to send */
1321 HZ * 2); /* time to wait for the message to complete before timing out */
1323 dbg ("auerswald_addservice: auerchain_control_msg returned error code %d", ret);
1324 /* undo above actions */
1325 cp->services[scp->id] = NULL;
1329 dbg ("auerswald_addservice: channel open OK");
1334 /* remove a service from the the device
1335 scp->id must be set! */
1336 static void auerswald_removeservice (pauerswald_t cp, pauerscon_t scp)
1338 dbg ("auerswald_removeservice called");
1340 /* check if we have a service allocated */
1341 if (scp->id == AUH_UNASSIGNED)
1344 /* If there is a device: close the channel */
1346 /* Close the service channel inside the device */
1347 int ret = auerchain_control_msg(
1348 &cp->controlchain, /* pointer to control chain */
1349 cp->usbdev, /* pointer to device */
1350 usb_sndctrlpipe (cp->usbdev, 0), /* pipe to control endpoint */
1351 AUV_CHANNELCTL, /* USB message request value */
1352 AUT_WREQ, /* USB message request type value */
1353 0x00, // close /* USB message value */
1354 scp->id, /* USB message index value */
1355 NULL, /* pointer to the data to send */
1356 0, /* length in bytes of the data to send */
1357 HZ * 2); /* time to wait for the message to complete before timing out */
1359 dbg ("auerswald_removeservice: auerchain_control_msg returned error code %d", ret);
1362 dbg ("auerswald_removeservice: channel close OK");
1366 /* remove the service from the device */
1367 cp->services[scp->id] = NULL;
1368 scp->id = AUH_UNASSIGNED;
1372 /* --------------------------------------------------------------------- */
1373 /* Char device functions */
1375 /* Open a new character device */
1376 static int auerchar_open (struct inode *inode, struct file *file)
1378 int dtindex = iminor(inode);
1379 pauerswald_t cp = NULL;
1380 pauerchar_t ccp = NULL;
1381 struct usb_interface *intf;
1384 /* minor number in range? */
1388 intf = usb_find_interface(&auerswald_driver, dtindex);
1393 /* usb device available? */
1394 cp = usb_get_intfdata (intf);
1398 if (down_interruptible (&cp->mutex)) {
1399 return -ERESTARTSYS;
1402 /* we have access to the device. Now lets allocate memory */
1403 ccp = (pauerchar_t) kmalloc(sizeof(auerchar_t), GFP_KERNEL);
1405 err ("out of memory");
1410 /* Initialize device descriptor */
1411 memset( ccp, 0, sizeof(auerchar_t));
1412 init_MUTEX( &ccp->mutex);
1413 init_MUTEX( &ccp->readmutex);
1414 auerbuf_init (&ccp->bufctl);
1415 ccp->scontext.id = AUH_UNASSIGNED;
1416 ccp->scontext.dispatch = auerchar_ctrlread_dispatch;
1417 ccp->scontext.disconnect = auerchar_disconnect;
1418 init_waitqueue_head (&ccp->readwait);
1420 ret = auerbuf_setup (&ccp->bufctl, AU_RBUFFERS, cp->maxControlLength+AUH_SIZE);
1427 dbg("open %s as /dev/%s", cp->dev_desc, cp->name);
1432 file->private_data = ccp;
1433 return nonseekable_open(inode, file);
1436 ofail: up (&cp->mutex);
1437 auerchar_delete (ccp);
1442 /* IOCTL functions */
1443 static int auerchar_ioctl (struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
1445 pauerchar_t ccp = (pauerchar_t) file->private_data;
1447 audevinfo_t devinfo;
1448 pauerswald_t cp = NULL;
1450 unsigned int __user *user_arg = (unsigned int __user *)arg;
1454 /* get the mutexes */
1455 if (down_interruptible (&ccp->mutex)) {
1456 return -ERESTARTSYS;
1463 if (down_interruptible (&cp->mutex)) {
1465 return -ERESTARTSYS;
1468 /* Check for removal */
1477 /* return != 0 if Transmitt channel ready to send */
1478 case IOCTL_AU_TXREADY:
1479 dbg ("IOCTL_AU_TXREADY");
1481 && (ccp->scontext.id != AUH_UNASSIGNED)
1482 && !list_empty (&cp->bufctl.free_buff_list);
1483 ret = put_user (u, user_arg);
1486 /* return != 0 if connected to a service channel */
1487 case IOCTL_AU_CONNECT:
1488 dbg ("IOCTL_AU_CONNECT");
1489 u = (ccp->scontext.id != AUH_UNASSIGNED);
1490 ret = put_user (u, user_arg);
1493 /* return != 0 if Receive Data available */
1494 case IOCTL_AU_RXAVAIL:
1495 dbg ("IOCTL_AU_RXAVAIL");
1496 if (ccp->scontext.id == AUH_UNASSIGNED) {
1500 u = 0; /* no data */
1502 int restlen = ccp->readbuf->len - ccp->readoffset;
1507 if (!list_empty (&ccp->bufctl.rec_buff_list)) {
1511 ret = put_user (u, user_arg);
1514 /* return the max. buffer length for the device */
1515 case IOCTL_AU_BUFLEN:
1516 dbg ("IOCTL_AU_BUFLEN");
1517 u = cp->maxControlLength;
1518 ret = put_user (u, user_arg);
1521 /* requesting a service channel */
1522 case IOCTL_AU_SERVREQ:
1523 dbg ("IOCTL_AU_SERVREQ");
1524 /* requesting a service means: release the previous one first */
1525 auerswald_removeservice (cp, &ccp->scontext);
1526 /* get the channel number */
1527 ret = get_user (u, user_arg);
1531 if ((u < AUH_FIRSTUSERCH) || (u >= AUH_TYPESIZE)) {
1535 dbg ("auerchar service request parameters are ok");
1536 ccp->scontext.id = u;
1538 /* request the service now */
1539 ret = auerswald_addservice (cp, &ccp->scontext);
1541 /* no: revert service entry */
1542 ccp->scontext.id = AUH_UNASSIGNED;
1546 /* get a string descriptor for the device */
1547 case IOCTL_AU_DEVINFO:
1548 dbg ("IOCTL_AU_DEVINFO");
1549 if (copy_from_user (&devinfo, (void __user *) arg, sizeof (audevinfo_t))) {
1553 u = strlen(cp->dev_desc)+1;
1554 if (u > devinfo.bsize) {
1557 ret = copy_to_user(devinfo.buf, cp->dev_desc, u) ? -EFAULT : 0;
1560 /* get the max. string descriptor length */
1562 dbg ("IOCTL_AU_SLEN");
1564 ret = put_user (u, user_arg);
1568 dbg ("IOCTL_AU_UNKNOWN");
1572 /* release the mutexes */
1578 /* Read data from the device */
1579 static ssize_t auerchar_read (struct file *file, char __user *buf, size_t count, loff_t * ppos)
1581 unsigned long flags;
1582 pauerchar_t ccp = (pauerchar_t) file->private_data;
1583 pauerbuf_t bp = NULL;
1586 dbg ("auerchar_read");
1588 /* Error checking */
1597 if (down_interruptible (&ccp->mutex))
1598 return -ERESTARTSYS;
1600 /* Can we expect to read something? */
1601 if (ccp->scontext.id == AUH_UNASSIGNED) {
1606 /* only one reader per device allowed */
1607 if (down_interruptible (&ccp->readmutex)) {
1609 return -ERESTARTSYS;
1612 /* read data from readbuf, if available */
1616 /* read the maximum bytes */
1617 int restlen = bp->len - ccp->readoffset;
1620 if (count > restlen)
1623 if (copy_to_user (buf, bp->bufp+ccp->readoffset, count)) {
1624 dbg ("auerswald_read: copy_to_user failed");
1625 up (&ccp->readmutex);
1630 /* advance the read offset */
1631 ccp->readoffset += count;
1633 // reuse the read buffer
1635 auerbuf_releasebuf (bp);
1636 ccp->readbuf = NULL;
1638 /* return with number of bytes read */
1640 up (&ccp->readmutex);
1646 /* a read buffer is not available. Try to get the next data block. */
1648 /* Preparing for sleep */
1649 init_waitqueue_entry (&wait, current);
1650 set_current_state (TASK_INTERRUPTIBLE);
1651 add_wait_queue (&ccp->readwait, &wait);
1654 spin_lock_irqsave (&ccp->bufctl.lock, flags);
1655 if (!list_empty (&ccp->bufctl.rec_buff_list)) {
1656 /* yes: get the entry */
1657 struct list_head *tmp = ccp->bufctl.rec_buff_list.next;
1659 bp = list_entry (tmp, auerbuf_t, buff_list);
1661 spin_unlock_irqrestore (&ccp->bufctl.lock, flags);
1663 /* have we got data? */
1666 ccp->readoffset = AUH_SIZE; /* for headerbyte */
1667 set_current_state (TASK_RUNNING);
1668 remove_wait_queue (&ccp->readwait, &wait);
1669 goto doreadbuf; /* now we can read! */
1672 /* no data available. Should we wait? */
1673 if (file->f_flags & O_NONBLOCK) {
1674 dbg ("No read buffer available, returning -EAGAIN");
1675 set_current_state (TASK_RUNNING);
1676 remove_wait_queue (&ccp->readwait, &wait);
1677 up (&ccp->readmutex);
1679 return -EAGAIN; /* nonblocking, no data available */
1682 /* yes, we should wait! */
1683 up (&ccp->mutex); /* allow other operations while we wait */
1685 remove_wait_queue (&ccp->readwait, &wait);
1686 if (signal_pending (current)) {
1687 /* waked up by a signal */
1688 up (&ccp->readmutex);
1689 return -ERESTARTSYS;
1692 /* Anything left to read? */
1693 if ((ccp->scontext.id == AUH_UNASSIGNED) || ccp->removed) {
1694 up (&ccp->readmutex);
1698 if (down_interruptible (&ccp->mutex)) {
1699 up (&ccp->readmutex);
1700 return -ERESTARTSYS;
1703 /* try to read the incoming data again */
1708 /* Write a data block into the right service channel of the device */
1709 static ssize_t auerchar_write (struct file *file, const char __user *buf, size_t len, loff_t *ppos)
1711 pauerchar_t ccp = (pauerchar_t) file->private_data;
1712 pauerswald_t cp = NULL;
1714 unsigned long flags;
1718 dbg ("auerchar_write %d bytes", len);
1720 /* Error checking */
1730 if (down_interruptible (&ccp->mutex))
1731 return -ERESTARTSYS;
1733 /* Can we expect to write something? */
1734 if (ccp->scontext.id == AUH_UNASSIGNED) {
1742 return -ERESTARTSYS;
1744 if (down_interruptible (&cp->mutex)) {
1746 return -ERESTARTSYS;
1753 /* Prepare for sleep */
1754 init_waitqueue_entry (&wait, current);
1755 set_current_state (TASK_INTERRUPTIBLE);
1756 add_wait_queue (&cp->bufferwait, &wait);
1758 /* Try to get a buffer from the device pool.
1759 We can't use a buffer from ccp->bufctl because the write
1760 command will last beond a release() */
1762 spin_lock_irqsave (&cp->bufctl.lock, flags);
1763 if (!list_empty (&cp->bufctl.free_buff_list)) {
1764 /* yes: get the entry */
1765 struct list_head *tmp = cp->bufctl.free_buff_list.next;
1767 bp = list_entry (tmp, auerbuf_t, buff_list);
1769 spin_unlock_irqrestore (&cp->bufctl.lock, flags);
1771 /* are there any buffers left? */
1776 /* NONBLOCK: don't wait */
1777 if (file->f_flags & O_NONBLOCK) {
1778 set_current_state (TASK_RUNNING);
1779 remove_wait_queue (&cp->bufferwait, &wait);
1783 /* BLOCKING: wait */
1785 remove_wait_queue (&cp->bufferwait, &wait);
1786 if (signal_pending (current)) {
1787 /* waked up by a signal */
1788 return -ERESTARTSYS;
1792 set_current_state (TASK_RUNNING);
1793 remove_wait_queue (&cp->bufferwait, &wait);
1796 /* protect against too big write requests */
1797 if (len > cp->maxControlLength)
1798 len = cp->maxControlLength;
1800 /* Fill the buffer */
1801 if (copy_from_user ( bp->bufp+AUH_SIZE, buf, len)) {
1802 dbg ("copy_from_user failed");
1803 auerbuf_releasebuf (bp);
1804 /* Wake up all processes waiting for a buffer */
1805 wake_up (&cp->bufferwait);
1811 /* set the header byte */
1812 *(bp->bufp) = ccp->scontext.id | AUH_DIRECT | AUH_UNSPLIT;
1814 /* Set the transfer Parameters */
1815 bp->len = len+AUH_SIZE;
1816 bp->dr->bRequestType = AUT_WREQ;
1817 bp->dr->bRequest = AUV_WBLOCK;
1818 bp->dr->wValue = cpu_to_le16 (0);
1819 bp->dr->wIndex = cpu_to_le16 (ccp->scontext.id | AUH_DIRECT | AUH_UNSPLIT);
1820 bp->dr->wLength = cpu_to_le16 (len+AUH_SIZE);
1821 usb_fill_control_urb (bp->urbp, cp->usbdev, usb_sndctrlpipe (cp->usbdev, 0),
1822 (unsigned char*)bp->dr, bp->bufp, len+AUH_SIZE,
1823 auerchar_ctrlwrite_complete, bp);
1825 ret = auerchain_submit_urb (&cp->controlchain, bp->urbp);
1828 dbg ("auerchar_write: nonzero result of auerchain_submit_urb %d", ret);
1829 auerbuf_releasebuf (bp);
1830 /* Wake up all processes waiting for a buffer */
1831 wake_up (&cp->bufferwait);
1836 dbg ("auerchar_write: Write OK");
1843 /* Close a character device */
1844 static int auerchar_release (struct inode *inode, struct file *file)
1846 pauerchar_t ccp = (pauerchar_t) file->private_data;
1850 /* get the mutexes */
1851 if (down_interruptible (&ccp->mutex)) {
1852 return -ERESTARTSYS;
1856 if (down_interruptible (&cp->mutex)) {
1858 return -ERESTARTSYS;
1860 /* remove an open service */
1861 auerswald_removeservice (cp, &ccp->scontext);
1862 /* detach from device */
1863 if ((--cp->open_count <= 0) && (cp->usbdev == NULL)) {
1864 /* usb device waits for removal */
1866 auerswald_delete (cp);
1871 ccp->auerdev = NULL;
1874 auerchar_delete (ccp);
1880 /*----------------------------------------------------------------------*/
1881 /* File operation structure */
1882 static struct file_operations auerswald_fops =
1884 .owner = THIS_MODULE,
1885 .llseek = no_llseek,
1886 .read = auerchar_read,
1887 .write = auerchar_write,
1888 .ioctl = auerchar_ioctl,
1889 .open = auerchar_open,
1890 .release = auerchar_release,
1893 static struct usb_class_driver auerswald_class = {
1894 .name = "usb/auer%d",
1895 .fops = &auerswald_fops,
1896 .mode = S_IFCHR | S_IRUGO | S_IWUGO,
1897 .minor_base = AUER_MINOR_BASE,
1901 /* --------------------------------------------------------------------- */
1902 /* Special USB driver functions */
1904 /* Probe if this driver wants to serve an USB device
1906 This entry point is called whenever a new device is attached to the bus.
1907 Then the device driver has to create a new instance of its internal data
1908 structures for the new device.
1910 The dev argument specifies the device context, which contains pointers
1911 to all USB descriptors. The interface argument specifies the interface
1912 number. If a USB driver wants to bind itself to a particular device and
1913 interface it has to return a pointer. This pointer normally references
1914 the device driver's context structure.
1916 Probing normally is done by checking the vendor and product identifications
1917 or the class and subclass definitions. If they match the interface number
1918 is compared with the ones supported by the driver. When probing is done
1919 class based it might be necessary to parse some more USB descriptors because
1920 the device properties can differ in a wide range.
1922 static int auerswald_probe (struct usb_interface *intf,
1923 const struct usb_device_id *id)
1925 struct usb_device *usbdev = interface_to_usbdev(intf);
1926 pauerswald_t cp = NULL;
1931 dbg ("probe: vendor id 0x%x, device id 0x%x",
1932 usbdev->descriptor.idVendor, usbdev->descriptor.idProduct);
1934 /* See if the device offered us matches that we can accept */
1935 if (usbdev->descriptor.idVendor != ID_AUERSWALD)
1938 /* we use only the first -and only- interface */
1939 if (intf->altsetting->desc.bInterfaceNumber != 0)
1942 /* allocate memory for our device and initialize it */
1943 cp = kmalloc (sizeof(auerswald_t), GFP_KERNEL);
1945 err ("out of memory");
1949 /* Initialize device descriptor */
1950 memset (cp, 0, sizeof(auerswald_t));
1951 init_MUTEX (&cp->mutex);
1952 cp->usbdev = usbdev;
1953 auerchain_init (&cp->controlchain);
1954 auerbuf_init (&cp->bufctl);
1955 init_waitqueue_head (&cp->bufferwait);
1957 ret = usb_register_dev(intf, &auerswald_class);
1959 err ("Not able to get a minor for this device.");
1963 /* Give the device a name */
1964 sprintf (cp->name, "usb/auer%d", intf->minor);
1966 /* Store the index */
1967 cp->dtindex = intf->minor;
1969 /* Get the usb version of the device */
1970 cp->version = cp->usbdev->descriptor.bcdDevice;
1971 dbg ("Version is %X", cp->version);
1973 /* allow some time to settle the device */
1976 /* Try to get a suitable textual description of the device */
1978 ret = usb_string( cp->usbdev, AUSI_DEVICE, cp->dev_desc, AUSI_DLEN-1);
1981 /* Append Serial Number */
1982 memcpy(&cp->dev_desc[u], ",Ser# ", 6);
1984 ret = usb_string( cp->usbdev, AUSI_SERIALNR, &cp->dev_desc[u], AUSI_DLEN-u-1);
1987 /* Append subscriber number */
1988 memcpy(&cp->dev_desc[u], ", ", 2);
1990 ret = usb_string( cp->usbdev, AUSI_MSN, &cp->dev_desc[u], AUSI_DLEN-u-1);
1996 cp->dev_desc[u] = '\0';
1997 info("device is a %s", cp->dev_desc);
1999 /* get the maximum allowed control transfer length */
2000 pbuf = (__le16 *) kmalloc (2, GFP_KERNEL); /* use an allocated buffer because of urb target */
2002 err( "out of memory");
2005 ret = usb_control_msg(cp->usbdev, /* pointer to device */
2006 usb_rcvctrlpipe( cp->usbdev, 0 ), /* pipe to control endpoint */
2007 AUV_GETINFO, /* USB message request value */
2008 AUT_RREQ, /* USB message request type value */
2009 0, /* USB message value */
2010 AUDI_MBCTRANS, /* USB message index value */
2011 pbuf, /* pointer to the receive buffer */
2012 2, /* length of the buffer */
2013 HZ * 2); /* time to wait for the message to complete before timing out */
2015 cp->maxControlLength = le16_to_cpup(pbuf);
2017 dbg("setup: max. allowed control transfersize is %d bytes", cp->maxControlLength);
2020 err("setup: getting max. allowed control transfer length failed with error %d", ret);
2024 /* allocate a chain for the control messages */
2025 if (auerchain_setup (&cp->controlchain, AUCH_ELEMENTS)) {
2026 err ("out of memory");
2030 /* allocate buffers for control messages */
2031 if (auerbuf_setup (&cp->bufctl, AU_RBUFFERS, cp->maxControlLength+AUH_SIZE)) {
2032 err ("out of memory");
2036 /* start the interrupt endpoint */
2037 if (auerswald_int_open (cp)) {
2038 err ("int endpoint failed");
2043 usb_set_intfdata (intf, cp);
2046 /* Error exit: clean up the memory */
2047 pfail: auerswald_delete (cp);
2052 /* Disconnect driver from a served device
2054 This function is called whenever a device which was served by this driver
2057 The argument dev specifies the device context and the driver_context
2058 returns a pointer to the previously registered driver_context of the
2059 probe function. After returning from the disconnect function the USB
2060 framework completely deallocates all data structures associated with
2061 this device. So especially the usb_device structure must not be used
2062 any longer by the usb driver.
2064 static void auerswald_disconnect (struct usb_interface *intf)
2066 pauerswald_t cp = usb_get_intfdata (intf);
2069 usb_set_intfdata (intf, NULL);
2074 info ("device /dev/%s now disconnecting", cp->name);
2076 /* give back our USB minor number */
2077 usb_deregister_dev(intf, &auerswald_class);
2079 /* Stop the interrupt endpoint */
2080 auerswald_int_release (cp);
2082 /* remove the control chain allocated in auerswald_probe
2083 This has the benefit of
2084 a) all pending (a)synchronous urbs are unlinked
2085 b) all buffers dealing with urbs are reclaimed
2087 auerchain_free (&cp->controlchain);
2089 if (cp->open_count == 0) {
2090 /* nobody is using this device. So we can clean up now */
2091 up (&cp->mutex);/* up() is possible here because no other task
2092 can open the device (see above). I don't want
2093 to kfree() a locked mutex. */
2094 auerswald_delete (cp);
2096 /* device is used. Remove the pointer to the
2097 usb device (it's not valid any more). The last
2098 release() will do the clean up */
2101 /* Terminate waiting writers */
2102 wake_up (&cp->bufferwait);
2103 /* Inform all waiting readers */
2104 for ( u = 0; u < AUH_TYPESIZE; u++) {
2105 pauerscon_t scp = cp->services[u];
2107 scp->disconnect( scp);
2112 /* Descriptor for the devices which are served by this driver.
2113 NOTE: this struct is parsed by the usbmanager install scripts.
2114 Don't change without caution!
2116 static struct usb_device_id auerswald_ids [] = {
2117 { USB_DEVICE (ID_AUERSWALD, 0x00C0) }, /* COMpact 2104 USB */
2118 { USB_DEVICE (ID_AUERSWALD, 0x00DB) }, /* COMpact 4410/2206 USB */
2119 { USB_DEVICE (ID_AUERSWALD, 0x00F1) }, /* Comfort 2000 System Telephone */
2120 { USB_DEVICE (ID_AUERSWALD, 0x00F2) }, /* Comfort 1200 System Telephone */
2121 { } /* Terminating entry */
2124 /* Standard module device table */
2125 MODULE_DEVICE_TABLE (usb, auerswald_ids);
2127 /* Standard usb driver struct */
2128 static struct usb_driver auerswald_driver = {
2129 .owner = THIS_MODULE,
2130 .name = "auerswald",
2131 .probe = auerswald_probe,
2132 .disconnect = auerswald_disconnect,
2133 .id_table = auerswald_ids,
2137 /* --------------------------------------------------------------------- */
2138 /* Module loading/unloading */
2140 /* Driver initialisation. Called after module loading.
2141 NOTE: there is no concurrency at _init
2143 static int __init auerswald_init (void)
2148 /* register driver at the USB subsystem */
2149 result = usb_register (&auerswald_driver);
2151 err ("driver could not be registered");
2157 /* Driver deinit. Called before module removal.
2158 NOTE: there is no concurrency at _cleanup
2160 static void __exit auerswald_cleanup (void)
2163 usb_deregister (&auerswald_driver);
2166 /* --------------------------------------------------------------------- */
2167 /* Linux device driver module description */
2169 MODULE_AUTHOR (DRIVER_AUTHOR);
2170 MODULE_DESCRIPTION (DRIVER_DESC);
2171 MODULE_LICENSE ("GPL");
2173 module_init (auerswald_init);
2174 module_exit (auerswald_cleanup);
2176 /* --------------------------------------------------------------------- */