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 #include <linux/wait.h>
33 #undef DEBUG /* include debug macros until it's done */
34 #include <linux/usb.h>
36 /*-------------------------------------------------------------------*/
39 #define dump( adr, len) \
42 printk (KERN_DEBUG); \
43 for (u = 0; u < len; u++) \
44 printk (" %02X", adr[u] & 0xFF); \
48 #define dump( adr, len)
51 /*-------------------------------------------------------------------*/
52 /* Version Information */
53 #define DRIVER_VERSION "0.9.11"
54 #define DRIVER_AUTHOR "Wolfgang Mües <wolfgang@iksw-muees.de>"
55 #define DRIVER_DESC "Auerswald PBX/System Telephone usb driver"
57 /*-------------------------------------------------------------------*/
58 /* Private declarations for Auerswald USB driver */
60 /* Auerswald Vendor ID */
61 #define ID_AUERSWALD 0x09BF
63 #define AUER_MINOR_BASE 112 /* auerswald driver minor number */
65 /* we can have up to this number of device plugged in at once */
66 #define AUER_MAX_DEVICES 16
69 /* Number of read buffers for each device */
70 #define AU_RBUFFERS 10
72 /* Number of chain elements for each control chain */
73 #define AUCH_ELEMENTS 20
75 /* Number of retries in communication */
78 /*-------------------------------------------------------------------*/
79 /* vendor specific protocol */
81 #define AUH_INDIRMASK 0x80 /* mask for direct/indirect bit */
82 #define AUH_DIRECT 0x00 /* data is for USB device */
83 #define AUH_INDIRECT 0x80 /* USB device is relay */
85 #define AUH_SPLITMASK 0x40 /* mask for split bit */
86 #define AUH_UNSPLIT 0x00 /* data block is full-size */
87 #define AUH_SPLIT 0x40 /* data block is part of a larger one,
90 #define AUH_TYPEMASK 0x3F /* mask for type of data transfer */
91 #define AUH_TYPESIZE 0x40 /* different types */
92 #define AUH_DCHANNEL 0x00 /* D channel data */
93 #define AUH_B1CHANNEL 0x01 /* B1 channel transparent */
94 #define AUH_B2CHANNEL 0x02 /* B2 channel transparent */
95 /* 0x03..0x0F reserved for driver internal use */
96 #define AUH_COMMAND 0x10 /* Command channel */
97 #define AUH_BPROT 0x11 /* Configuration block protocol */
98 #define AUH_DPROTANA 0x12 /* D channel protocol analyzer */
99 #define AUH_TAPI 0x13 /* telephone api data (ATD) */
100 /* 0x14..0x3F reserved for other protocols */
101 #define AUH_UNASSIGNED 0xFF /* if char device has no assigned service */
102 #define AUH_FIRSTUSERCH 0x11 /* first channel which is available for driver users */
104 #define AUH_SIZE 1 /* Size of Header Byte */
106 /* Split Byte. Only present if split bit in header byte set.*/
107 #define AUS_STARTMASK 0x80 /* mask for first block of splitted frame */
108 #define AUS_FIRST 0x80 /* first block */
109 #define AUS_FOLLOW 0x00 /* following block */
111 #define AUS_ENDMASK 0x40 /* mask for last block of splitted frame */
112 #define AUS_END 0x40 /* last block */
113 #define AUS_NOEND 0x00 /* not the last block */
115 #define AUS_LENMASK 0x3F /* mask for block length information */
118 #define AUT_RREQ (USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_OTHER) /* Read Request */
119 #define AUT_WREQ (USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER) /* Write Request */
121 /* Vendor Requests */
122 #define AUV_GETINFO 0x00 /* GetDeviceInfo */
123 #define AUV_WBLOCK 0x01 /* Write Block */
124 #define AUV_RBLOCK 0x02 /* Read Block */
125 #define AUV_CHANNELCTL 0x03 /* Channel Control */
126 #define AUV_DUMMY 0x04 /* Dummy Out for retry */
128 /* Device Info Types */
129 #define AUDI_NUMBCH 0x0000 /* Number of supported B channels */
130 #define AUDI_OUTFSIZE 0x0001 /* Size of OUT B channel fifos */
131 #define AUDI_MBCTRANS 0x0002 /* max. Blocklength of control transfer */
133 /* Interrupt endpoint definitions */
134 #define AU_IRQENDP 1 /* Endpoint number */
135 #define AU_IRQCMDID 16 /* Command-block ID */
136 #define AU_BLOCKRDY 0 /* Command: Block data ready on ctl endpoint */
137 #define AU_IRQMINSIZE 5 /* Nr. of bytes decoded in this driver */
139 /* Device String Descriptors */
140 #define AUSI_VENDOR 1 /* "Auerswald GmbH & Co. KG" */
141 #define AUSI_DEVICE 2 /* Name of the Device */
142 #define AUSI_SERIALNR 3 /* Serial Number */
143 #define AUSI_MSN 4 /* "MSN ..." (first) Multiple Subscriber Number */
145 #define AUSI_DLEN 100 /* Max. Length of Device Description */
147 #define AUV_RETRY 0x101 /* First Firmware version which can do control retries */
149 /*-------------------------------------------------------------------*/
150 /* External data structures / Interface */
153 char __user *buf; /* return buffer for string contents */
154 unsigned int bsize; /* size of return buffer */
155 } audevinfo_t,*paudevinfo_t;
158 #define IOCTL_AU_SLEN _IOR( 'U', 0xF0, int) /* return the max. string descriptor length */
159 #define IOCTL_AU_DEVINFO _IOWR('U', 0xF1, audevinfo_t) /* get name of a specific device */
160 #define IOCTL_AU_SERVREQ _IOW( 'U', 0xF2, int) /* request a service channel */
161 #define IOCTL_AU_BUFLEN _IOR( 'U', 0xF3, int) /* return the max. buffer length for the device */
162 #define IOCTL_AU_RXAVAIL _IOR( 'U', 0xF4, int) /* return != 0 if Receive Data available */
163 #define IOCTL_AU_CONNECT _IOR( 'U', 0xF5, int) /* return != 0 if connected to a service channel */
164 #define IOCTL_AU_TXREADY _IOR( 'U', 0xF6, int) /* return != 0 if Transmitt channel ready to send */
165 /* 'U' 0xF7..0xFF reseved */
167 /*-------------------------------------------------------------------*/
168 /* Internal data structures */
170 /* ..................................................................*/
171 /* urb chain element */
172 struct auerchain; /* forward for circular reference */
175 struct auerchain *chain; /* pointer to the chain to which this element belongs */
176 struct urb * urbp; /* pointer to attached urb */
177 void *context; /* saved URB context */
178 usb_complete_t complete; /* saved URB completion function */
179 struct list_head list; /* to include element into a list */
180 } auerchainelement_t,*pauerchainelement_t;
183 typedef struct auerchain
185 pauerchainelement_t active; /* element which is submitted to urb */
186 spinlock_t lock; /* protection agains interrupts */
187 struct list_head waiting_list; /* list of waiting elements */
188 struct list_head free_list; /* list of available elements */
189 } auerchain_t,*pauerchain_t;
191 /* urb blocking completion helper struct */
194 wait_queue_head_t wqh; /* wait for completion */
195 unsigned int done; /* completion flag */
196 } auerchain_chs_t,*pauerchain_chs_t;
198 /* ...................................................................*/
200 struct auerbufctl; /* forward */
203 char *bufp; /* reference to allocated data buffer */
204 unsigned int len; /* number of characters in data buffer */
205 unsigned int retries; /* for urb retries */
206 struct usb_ctrlrequest *dr; /* for setup data in control messages */
207 struct urb * urbp; /* USB urb */
208 struct auerbufctl *list; /* pointer to list */
209 struct list_head buff_list; /* reference to next buffer in list */
210 } auerbuf_t,*pauerbuf_t;
212 /* buffer list control block */
213 typedef struct auerbufctl
215 spinlock_t lock; /* protection in interrupt */
216 struct list_head free_buff_list;/* free buffers */
217 struct list_head rec_buff_list; /* buffers with receive data */
218 } auerbufctl_t,*pauerbufctl_t;
220 /* ...................................................................*/
221 /* service context */
222 struct auerscon; /* forward */
223 typedef void (*auer_dispatch_t)(struct auerscon*, pauerbuf_t);
224 typedef void (*auer_disconn_t) (struct auerscon*);
225 typedef struct auerscon
227 unsigned int id; /* protocol service id AUH_xxxx */
228 auer_dispatch_t dispatch; /* dispatch read buffer */
229 auer_disconn_t disconnect; /* disconnect from device, wake up all char readers */
230 } auerscon_t,*pauerscon_t;
232 /* ...................................................................*/
233 /* USB device context */
236 struct semaphore mutex; /* protection in user context */
237 char name[20]; /* name of the /dev/usb entry */
238 unsigned int dtindex; /* index in the device table */
239 struct usb_device * usbdev; /* USB device handle */
240 int open_count; /* count the number of open character channels */
241 char dev_desc[AUSI_DLEN];/* for storing a textual description */
242 unsigned int maxControlLength; /* max. Length of control paket (without header) */
243 struct urb * inturbp; /* interrupt urb */
244 char * intbufp; /* data buffer for interrupt urb */
245 unsigned int irqsize; /* size of interrupt endpoint 1 */
246 struct auerchain controlchain; /* for chaining of control messages */
247 auerbufctl_t bufctl; /* Buffer control for control transfers */
248 pauerscon_t services[AUH_TYPESIZE];/* context pointers for each service */
249 unsigned int version; /* Version of the device */
250 wait_queue_head_t bufferwait; /* wait for a control buffer */
251 } auerswald_t,*pauerswald_t;
253 /* ................................................................... */
254 /* character device context */
257 struct semaphore mutex; /* protection in user context */
258 pauerswald_t auerdev; /* context pointer of assigned device */
259 auerbufctl_t bufctl; /* controls the buffer chain */
260 auerscon_t scontext; /* service context */
261 wait_queue_head_t readwait; /* for synchronous reading */
262 struct semaphore readmutex; /* protection against multiple reads */
263 pauerbuf_t readbuf; /* buffer held for partial reading */
264 unsigned int readoffset; /* current offset in readbuf */
265 unsigned int removed; /* is != 0 if device is removed */
266 } auerchar_t,*pauerchar_t;
269 /*-------------------------------------------------------------------*/
271 static void auerswald_ctrlread_complete (struct urb * urb, struct pt_regs *regs);
272 static void auerswald_removeservice (pauerswald_t cp, pauerscon_t scp);
273 static struct usb_driver auerswald_driver;
276 /*-------------------------------------------------------------------*/
277 /* USB chain helper functions */
278 /* -------------------------- */
280 /* completion function for chained urbs */
281 static void auerchain_complete (struct urb * urb, struct pt_regs *regs)
286 /* get pointer to element and to chain */
287 pauerchainelement_t acep = (pauerchainelement_t) urb->context;
288 pauerchain_t acp = acep->chain;
290 /* restore original entries in urb */
291 urb->context = acep->context;
292 urb->complete = acep->complete;
294 dbg ("auerchain_complete called");
296 /* call original completion function
297 NOTE: this function may lead to more urbs submitted into the chain.
298 (no chain lock at calling complete()!)
299 acp->active != NULL is protecting us against recursion.*/
300 urb->complete (urb, regs);
302 /* detach element from chain data structure */
303 spin_lock_irqsave (&acp->lock, flags);
304 if (acp->active != acep) /* paranoia debug check */
305 dbg ("auerchain_complete: completion on non-active element called!");
309 /* add the used chain element to the list of free elements */
310 list_add_tail (&acep->list, &acp->free_list);
313 /* is there a new element waiting in the chain? */
314 if (!acp->active && !list_empty (&acp->waiting_list)) {
315 /* yes: get the entry */
316 struct list_head *tmp = acp->waiting_list.next;
318 acep = list_entry (tmp, auerchainelement_t, list);
321 spin_unlock_irqrestore (&acp->lock, flags);
323 /* submit the new urb */
326 dbg ("auerchain_complete: submitting next urb from chain");
327 urb->status = 0; /* needed! */
328 result = usb_submit_urb(urb, GFP_ATOMIC);
330 /* check for submit errors */
332 urb->status = result;
333 dbg("auerchain_complete: usb_submit_urb with error code %d", result);
334 /* and do error handling via *this* completion function (recursive) */
335 auerchain_complete( urb, NULL);
338 /* simple return without submitting a new urb.
339 The empty chain is detected with acp->active == NULL. */
344 /* submit function for chained urbs
345 this function may be called from completion context or from user space!
346 early = 1 -> submit in front of chain
348 static int auerchain_submit_urb_list (pauerchain_t acp, struct urb * urb, int early)
352 pauerchainelement_t acep = NULL;
354 dbg ("auerchain_submit_urb called");
356 /* try to get a chain element */
357 spin_lock_irqsave (&acp->lock, flags);
358 if (!list_empty (&acp->free_list)) {
359 /* yes: get the entry */
360 struct list_head *tmp = acp->free_list.next;
362 acep = list_entry (tmp, auerchainelement_t, list);
364 spin_unlock_irqrestore (&acp->lock, flags);
366 /* if no chain element available: return with error */
371 /* fill in the new chain element values */
373 acep->context = urb->context;
374 acep->complete = urb->complete;
376 INIT_LIST_HEAD (&acep->list);
380 urb->complete = auerchain_complete;
381 urb->status = -EINPROGRESS; /* usb_submit_urb does this, too */
383 /* add element to chain - or start it immediately */
384 spin_lock_irqsave (&acp->lock, flags);
386 /* there is traffic in the chain, simple add element to chain */
388 dbg ("adding new urb to head of chain");
389 list_add (&acep->list, &acp->waiting_list);
391 dbg ("adding new urb to end of chain");
392 list_add_tail (&acep->list, &acp->waiting_list);
396 /* the chain is empty. Prepare restart */
399 /* Spin has to be removed before usb_submit_urb! */
400 spin_unlock_irqrestore (&acp->lock, flags);
402 /* Submit urb if immediate restart */
404 dbg("submitting urb immediate");
405 urb->status = 0; /* needed! */
406 result = usb_submit_urb(urb, GFP_ATOMIC);
407 /* check for submit errors */
409 urb->status = result;
410 dbg("auerchain_submit_urb: usb_submit_urb with error code %d", result);
411 /* and do error handling via completion function */
412 auerchain_complete( urb, NULL);
419 /* submit function for chained urbs
420 this function may be called from completion context or from user space!
422 static int auerchain_submit_urb (pauerchain_t acp, struct urb * urb)
424 return auerchain_submit_urb_list (acp, urb, 0);
427 /* cancel an urb which is submitted to the chain
428 the result is 0 if the urb is cancelled, or -EINPROGRESS if
429 URB_ASYNC_UNLINK is set and the function is successfully started.
431 static int auerchain_unlink_urb (pauerchain_t acp, struct urb * urb)
435 pauerchainelement_t acep;
436 struct list_head *tmp;
438 dbg ("auerchain_unlink_urb called");
440 /* search the chain of waiting elements */
441 spin_lock_irqsave (&acp->lock, flags);
442 list_for_each (tmp, &acp->waiting_list) {
443 acep = list_entry (tmp, auerchainelement_t, list);
444 if (acep->urbp == urb) {
446 urb->context = acep->context;
447 urb->complete = acep->complete;
448 list_add_tail (&acep->list, &acp->free_list);
449 spin_unlock_irqrestore (&acp->lock, flags);
450 dbg ("unlink waiting urb");
451 urb->status = -ENOENT;
452 urb->complete (urb, NULL);
457 spin_unlock_irqrestore (&acp->lock, flags);
459 /* get the active urb */
464 /* check if we have to cancel the active urb */
466 /* note that there is a race condition between the check above
467 and the unlink() call because of no lock. This race is harmless,
468 because the usb module will detect the unlink() after completion.
469 We can't use the acp->lock here because the completion function
472 dbg ("unlink active urb");
473 return usb_unlink_urb (urbp);
478 ... is some kind of success
480 dbg ("urb to unlink not found in chain");
484 /* cancel all urbs which are in the chain.
485 this function must not be called from interrupt or completion handler.
487 static void auerchain_unlink_all (pauerchain_t acp)
491 pauerchainelement_t acep;
493 dbg ("auerchain_unlink_all called");
495 /* clear the chain of waiting elements */
496 spin_lock_irqsave (&acp->lock, flags);
497 while (!list_empty (&acp->waiting_list)) {
498 /* get the next entry */
499 struct list_head *tmp = acp->waiting_list.next;
501 acep = list_entry (tmp, auerchainelement_t, list);
503 urbp->context = acep->context;
504 urbp->complete = acep->complete;
505 list_add_tail (&acep->list, &acp->free_list);
506 spin_unlock_irqrestore (&acp->lock, flags);
507 dbg ("unlink waiting urb");
508 urbp->status = -ENOENT;
509 urbp->complete (urbp, NULL);
510 spin_lock_irqsave (&acp->lock, flags);
512 spin_unlock_irqrestore (&acp->lock, flags);
514 /* clear the active urb */
518 urbp->transfer_flags &= ~URB_ASYNC_UNLINK;
519 dbg ("unlink active urb");
526 this function must not be called from interrupt or completion handler.
528 static void auerchain_free (pauerchain_t acp)
531 pauerchainelement_t acep;
533 dbg ("auerchain_free called");
535 /* first, cancel all pending urbs */
536 auerchain_unlink_all (acp);
538 /* free the elements */
539 spin_lock_irqsave (&acp->lock, flags);
540 while (!list_empty (&acp->free_list)) {
541 /* get the next entry */
542 struct list_head *tmp = acp->free_list.next;
544 spin_unlock_irqrestore (&acp->lock, flags);
545 acep = list_entry (tmp, auerchainelement_t, list);
547 spin_lock_irqsave (&acp->lock, flags);
549 spin_unlock_irqrestore (&acp->lock, flags);
553 /* Init the chain control structure */
554 static void auerchain_init (pauerchain_t acp)
556 /* init the chain data structure */
558 spin_lock_init (&acp->lock);
559 INIT_LIST_HEAD (&acp->waiting_list);
560 INIT_LIST_HEAD (&acp->free_list);
564 It is assumed that there is no concurrency while setting up the chain
565 requirement: auerchain_init()
567 static int auerchain_setup (pauerchain_t acp, unsigned int numElements)
569 pauerchainelement_t acep;
571 dbg ("auerchain_setup called with %d elements", numElements);
573 /* fill the list of free elements */
574 for (;numElements; numElements--) {
575 acep = (pauerchainelement_t) kmalloc (sizeof (auerchainelement_t), GFP_KERNEL);
578 memset (acep, 0, sizeof (auerchainelement_t));
579 INIT_LIST_HEAD (&acep->list);
580 list_add_tail (&acep->list, &acp->free_list);
584 ac_fail:/* free the elements */
585 while (!list_empty (&acp->free_list)) {
586 /* get the next entry */
587 struct list_head *tmp = acp->free_list.next;
589 acep = list_entry (tmp, auerchainelement_t, list);
596 /* completion handler for synchronous chained URBs */
597 static void auerchain_blocking_completion (struct urb *urb, struct pt_regs *regs)
599 pauerchain_chs_t pchs = (pauerchain_chs_t)urb->context;
602 wake_up (&pchs->wqh);
606 /* Starts chained urb and waits for completion or timeout */
607 static int auerchain_start_wait_urb (pauerchain_t acp, struct urb *urb, int timeout, int* actual_length)
612 dbg ("auerchain_start_wait_urb called");
613 init_waitqueue_head (&chs.wqh);
617 status = auerchain_submit_urb (acp, urb);
619 /* something went wrong */
622 timeout = wait_event_timeout(chs.wqh, chs.done, timeout);
624 if (!timeout && !chs.done) {
625 if (urb->status != -EINPROGRESS) { /* No callback?!! */
626 dbg ("auerchain_start_wait_urb: raced timeout");
627 status = urb->status;
629 dbg ("auerchain_start_wait_urb: timeout");
630 auerchain_unlink_urb (acp, urb); /* remove urb safely */
634 status = urb->status;
637 *actual_length = urb->actual_length;
643 /* auerchain_control_msg - Builds a control urb, sends it off and waits for completion
644 acp: pointer to the auerchain
645 dev: pointer to the usb device to send the message to
646 pipe: endpoint "pipe" to send the message to
647 request: USB message request value
648 requesttype: USB message request type value
649 value: USB message value
650 index: USB message index value
651 data: pointer to the data to send
652 size: length in bytes of the data to send
653 timeout: time to wait for the message to complete before timing out (if 0 the wait is forever)
655 This function sends a simple control message to a specified endpoint
656 and waits for the message to complete, or timeout.
658 If successful, it returns the transferred length, otherwise a negative error number.
660 Don't use this function from within an interrupt context, like a
661 bottom half handler. If you need an asynchronous message, or need to send
662 a message from within interrupt context, use auerchain_submit_urb()
664 static int auerchain_control_msg (pauerchain_t acp, struct usb_device *dev, unsigned int pipe, __u8 request, __u8 requesttype,
665 __u16 value, __u16 index, void *data, __u16 size, int timeout)
668 struct usb_ctrlrequest *dr;
672 dbg ("auerchain_control_msg");
673 dr = kmalloc (sizeof (struct usb_ctrlrequest), GFP_KERNEL);
676 urb = usb_alloc_urb (0, GFP_KERNEL);
682 dr->bRequestType = requesttype;
683 dr->bRequest = request;
684 dr->wValue = cpu_to_le16 (value);
685 dr->wIndex = cpu_to_le16 (index);
686 dr->wLength = cpu_to_le16 (size);
688 usb_fill_control_urb (urb, dev, pipe, (unsigned char*)dr, data, size, /* build urb */
689 auerchain_blocking_completion, NULL);
690 ret = auerchain_start_wait_urb (acp, urb, timeout, &length);
702 /*-------------------------------------------------------------------*/
703 /* Buffer List helper functions */
705 /* free a single auerbuf */
706 static void auerbuf_free (pauerbuf_t bp)
715 usb_free_urb (bp->urbp);
720 /* free the buffers from an auerbuf list */
721 static void auerbuf_free_list (struct list_head *q)
723 struct list_head *tmp;
727 dbg ("auerbuf_free_list");
728 for (p = q->next; p != q;) {
729 bp = list_entry (p, auerbuf_t, buff_list);
737 /* init the members of a list control block */
738 static void auerbuf_init (pauerbufctl_t bcp)
740 dbg ("auerbuf_init");
741 spin_lock_init (&bcp->lock);
742 INIT_LIST_HEAD (&bcp->free_buff_list);
743 INIT_LIST_HEAD (&bcp->rec_buff_list);
746 /* free all buffers from an auerbuf chain */
747 static void auerbuf_free_buffers (pauerbufctl_t bcp)
750 dbg ("auerbuf_free_buffers");
752 spin_lock_irqsave (&bcp->lock, flags);
754 auerbuf_free_list (&bcp->free_buff_list);
755 auerbuf_free_list (&bcp->rec_buff_list);
757 spin_unlock_irqrestore (&bcp->lock, flags);
760 /* setup a list of buffers */
761 /* requirement: auerbuf_init() */
762 static int auerbuf_setup (pauerbufctl_t bcp, unsigned int numElements, unsigned int bufsize)
764 pauerbuf_t bep = NULL;
766 dbg ("auerbuf_setup called with %d elements of %d bytes", numElements, bufsize);
768 /* fill the list of free elements */
769 for (;numElements; numElements--) {
770 bep = (pauerbuf_t) kmalloc (sizeof (auerbuf_t), GFP_KERNEL);
773 memset (bep, 0, sizeof (auerbuf_t));
775 INIT_LIST_HEAD (&bep->buff_list);
776 bep->bufp = (char *) kmalloc (bufsize, GFP_KERNEL);
779 bep->dr = (struct usb_ctrlrequest *) kmalloc (sizeof (struct usb_ctrlrequest), GFP_KERNEL);
782 bep->urbp = usb_alloc_urb (0, GFP_KERNEL);
785 list_add_tail (&bep->buff_list, &bcp->free_buff_list);
789 bl_fail:/* not enough memory. Free allocated elements */
790 dbg ("auerbuf_setup: no more memory");
792 auerbuf_free_buffers (bcp);
796 /* insert a used buffer into the free list */
797 static void auerbuf_releasebuf( pauerbuf_t bp)
800 pauerbufctl_t bcp = bp->list;
803 dbg ("auerbuf_releasebuf called");
804 spin_lock_irqsave (&bcp->lock, flags);
805 list_add_tail (&bp->buff_list, &bcp->free_buff_list);
806 spin_unlock_irqrestore (&bcp->lock, flags);
810 /*-------------------------------------------------------------------*/
811 /* Completion handlers */
813 /* Values of urb->status or results of usb_submit_urb():
815 -EINPROGRESS during submission until end
816 -ENOENT if urb is unlinked
817 -ETIMEDOUT Transfer timed out, NAK
818 -ENOMEM Memory Overflow
819 -ENODEV Specified USB-device or bus doesn't exist
820 -ENXIO URB already queued
821 -EINVAL a) Invalid transfer type specified (or not supported)
822 b) Invalid interrupt interval (0n256)
823 -EAGAIN a) Specified ISO start frame too early
824 b) (using ISO-ASAP) Too much scheduled for the future wait some time and try again.
825 -EFBIG Too much ISO frames requested (currently uhci900)
826 -EPIPE Specified pipe-handle/Endpoint is already stalled
827 -EMSGSIZE Endpoint message size is zero, do interface/alternate setting
828 -EPROTO a) Bitstuff error
832 -EREMOTEIO Short packet detected
833 -EXDEV ISO transfer only partially completed look at individual frame status for details
834 -EINVAL ISO madness, if this happens: Log off and go home
838 /* check if a status code allows a retry */
839 static int auerswald_status_retry (int status)
851 return 1; /* do a retry */
853 return 0; /* no retry possible */
856 /* Completion of asynchronous write block */
857 static void auerchar_ctrlwrite_complete (struct urb * urb, struct pt_regs *regs)
859 pauerbuf_t bp = (pauerbuf_t) urb->context;
860 pauerswald_t cp = ((pauerswald_t)((char *)(bp->list)-(unsigned long)(&((pauerswald_t)0)->bufctl)));
861 dbg ("auerchar_ctrlwrite_complete called");
863 /* reuse the buffer */
864 auerbuf_releasebuf (bp);
865 /* Wake up all processes waiting for a buffer */
866 wake_up (&cp->bufferwait);
869 /* Completion handler for dummy retry packet */
870 static void auerswald_ctrlread_wretcomplete (struct urb * urb, struct pt_regs *regs)
872 pauerbuf_t bp = (pauerbuf_t) urb->context;
875 dbg ("auerswald_ctrlread_wretcomplete called");
876 dbg ("complete with status: %d", urb->status);
877 cp = ((pauerswald_t)((char *)(bp->list)-(unsigned long)(&((pauerswald_t)0)->bufctl)));
879 /* check if it is possible to advance */
880 if (!auerswald_status_retry (urb->status) || !cp->usbdev) {
881 /* reuse the buffer */
882 err ("control dummy: transmission error %d, can not retry", urb->status);
883 auerbuf_releasebuf (bp);
884 /* Wake up all processes waiting for a buffer */
885 wake_up (&cp->bufferwait);
889 /* fill the control message */
890 bp->dr->bRequestType = AUT_RREQ;
891 bp->dr->bRequest = AUV_RBLOCK;
892 bp->dr->wLength = bp->dr->wValue; /* temporary stored */
893 bp->dr->wValue = cpu_to_le16 (1); /* Retry Flag */
894 /* bp->dr->index = channel id; remains */
895 usb_fill_control_urb (bp->urbp, cp->usbdev, usb_rcvctrlpipe (cp->usbdev, 0),
896 (unsigned char*)bp->dr, bp->bufp, le16_to_cpu (bp->dr->wLength),
897 auerswald_ctrlread_complete,bp);
899 /* submit the control msg as next paket */
900 ret = auerchain_submit_urb_list (&cp->controlchain, bp->urbp, 1);
902 dbg ("auerswald_ctrlread_complete: nonzero result of auerchain_submit_urb_list %d", ret);
903 bp->urbp->status = ret;
904 auerswald_ctrlread_complete (bp->urbp, NULL);
908 /* completion handler for receiving of control messages */
909 static void auerswald_ctrlread_complete (struct urb * urb, struct pt_regs *regs)
911 unsigned int serviceid;
914 pauerbuf_t bp = (pauerbuf_t) urb->context;
916 dbg ("auerswald_ctrlread_complete called");
918 cp = ((pauerswald_t)((char *)(bp->list)-(unsigned long)(&((pauerswald_t)0)->bufctl)));
920 /* check if there is valid data in this urb */
922 dbg ("complete with non-zero status: %d", urb->status);
923 /* should we do a retry? */
924 if (!auerswald_status_retry (urb->status)
926 || (cp->version < AUV_RETRY)
927 || (bp->retries >= AU_RETRIES)) {
928 /* reuse the buffer */
929 err ("control read: transmission error %d, can not retry", urb->status);
930 auerbuf_releasebuf (bp);
931 /* Wake up all processes waiting for a buffer */
932 wake_up (&cp->bufferwait);
936 dbg ("Retry count = %d", bp->retries);
937 /* send a long dummy control-write-message to allow device firmware to react */
938 bp->dr->bRequestType = AUT_WREQ;
939 bp->dr->bRequest = AUV_DUMMY;
940 bp->dr->wValue = bp->dr->wLength; /* temporary storage */
941 // bp->dr->wIndex channel ID remains
942 bp->dr->wLength = cpu_to_le16 (32); /* >= 8 bytes */
943 usb_fill_control_urb (bp->urbp, cp->usbdev, usb_sndctrlpipe (cp->usbdev, 0),
944 (unsigned char*)bp->dr, bp->bufp, 32,
945 auerswald_ctrlread_wretcomplete,bp);
947 /* submit the control msg as next paket */
948 ret = auerchain_submit_urb_list (&cp->controlchain, bp->urbp, 1);
950 dbg ("auerswald_ctrlread_complete: nonzero result of auerchain_submit_urb_list %d", ret);
951 bp->urbp->status = ret;
952 auerswald_ctrlread_wretcomplete (bp->urbp, regs);
957 /* get the actual bytecount (incl. headerbyte) */
958 bp->len = urb->actual_length;
959 serviceid = bp->bufp[0] & AUH_TYPEMASK;
960 dbg ("Paket with serviceid %d and %d bytes received", serviceid, bp->len);
962 /* dispatch the paket */
963 scp = cp->services[serviceid];
965 /* look, Ma, a listener! */
966 scp->dispatch (scp, bp);
969 /* release the paket */
970 auerbuf_releasebuf (bp);
971 /* Wake up all processes waiting for a buffer */
972 wake_up (&cp->bufferwait);
975 /*-------------------------------------------------------------------*/
976 /* Handling of Interrupt Endpoint */
977 /* This interrupt Endpoint is used to inform the host about waiting
978 messages from the USB device.
980 /* int completion handler. */
981 static void auerswald_int_complete (struct urb * urb, struct pt_regs *regs)
984 unsigned int channelid;
985 unsigned int bytecount;
987 pauerbuf_t bp = NULL;
988 pauerswald_t cp = (pauerswald_t) urb->context;
990 dbg ("%s called", __FUNCTION__);
992 switch (urb->status) {
999 /* this urb is terminated, clean up */
1000 dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status);
1003 dbg("%s - nonzero urb status received: %d", __FUNCTION__, urb->status);
1007 /* check if all needed data was received */
1008 if (urb->actual_length < AU_IRQMINSIZE) {
1009 dbg ("invalid data length received: %d bytes", urb->actual_length);
1013 /* check the command code */
1014 if (cp->intbufp[0] != AU_IRQCMDID) {
1015 dbg ("invalid command received: %d", cp->intbufp[0]);
1019 /* check the command type */
1020 if (cp->intbufp[1] != AU_BLOCKRDY) {
1021 dbg ("invalid command type received: %d", cp->intbufp[1]);
1025 /* now extract the information */
1026 channelid = cp->intbufp[2];
1027 bytecount = (unsigned char)cp->intbufp[3];
1028 bytecount |= (unsigned char)cp->intbufp[4] << 8;
1030 /* check the channel id */
1031 if (channelid >= AUH_TYPESIZE) {
1032 dbg ("invalid channel id received: %d", channelid);
1036 /* check the byte count */
1037 if (bytecount > (cp->maxControlLength+AUH_SIZE)) {
1038 dbg ("invalid byte count received: %d", bytecount);
1041 dbg ("Service Channel = %d", channelid);
1042 dbg ("Byte Count = %d", bytecount);
1044 /* get a buffer for the next data paket */
1045 spin_lock_irqsave (&cp->bufctl.lock, flags);
1046 if (!list_empty (&cp->bufctl.free_buff_list)) {
1047 /* yes: get the entry */
1048 struct list_head *tmp = cp->bufctl.free_buff_list.next;
1050 bp = list_entry (tmp, auerbuf_t, buff_list);
1052 spin_unlock_irqrestore (&cp->bufctl.lock, flags);
1054 /* if no buffer available: skip it */
1056 dbg ("auerswald_int_complete: no data buffer available");
1057 /* can we do something more?
1058 This is a big problem: if this int packet is ignored, the
1059 device will wait forever and not signal any more data.
1060 The only real solution is: having enough buffers!
1061 Or perhaps temporary disabling the int endpoint?
1066 /* fill the control message */
1067 bp->dr->bRequestType = AUT_RREQ;
1068 bp->dr->bRequest = AUV_RBLOCK;
1069 bp->dr->wValue = cpu_to_le16 (0);
1070 bp->dr->wIndex = cpu_to_le16 (channelid | AUH_DIRECT | AUH_UNSPLIT);
1071 bp->dr->wLength = cpu_to_le16 (bytecount);
1072 usb_fill_control_urb (bp->urbp, cp->usbdev, usb_rcvctrlpipe (cp->usbdev, 0),
1073 (unsigned char*)bp->dr, bp->bufp, bytecount,
1074 auerswald_ctrlread_complete,bp);
1076 /* submit the control msg */
1077 ret = auerchain_submit_urb (&cp->controlchain, bp->urbp);
1079 dbg ("auerswald_int_complete: nonzero result of auerchain_submit_urb %d", ret);
1080 bp->urbp->status = ret;
1081 auerswald_ctrlread_complete( bp->urbp, NULL);
1082 /* here applies the same problem as above: device locking! */
1085 ret = usb_submit_urb (urb, GFP_ATOMIC);
1087 err ("%s - usb_submit_urb failed with result %d",
1091 /* int memory deallocation
1092 NOTE: no mutex please!
1094 static void auerswald_int_free (pauerswald_t cp)
1097 usb_free_urb (cp->inturbp);
1101 kfree (cp->intbufp);
1106 /* This function is called to activate the interrupt
1107 endpoint. This function returns 0 if successful or an error code.
1108 NOTE: no mutex please!
1110 static int auerswald_int_open (pauerswald_t cp)
1113 struct usb_host_endpoint *ep;
1115 dbg ("auerswald_int_open");
1117 ep = cp->usbdev->ep_in[AU_IRQENDP];
1122 irqsize = le16_to_cpu(ep->desc.wMaxPacketSize);
1123 cp->irqsize = irqsize;
1125 /* allocate the urb and data buffer */
1127 cp->inturbp = usb_alloc_urb (0, GFP_KERNEL);
1134 cp->intbufp = (char *) kmalloc (irqsize, GFP_KERNEL);
1141 usb_fill_int_urb (cp->inturbp, cp->usbdev,
1142 usb_rcvintpipe (cp->usbdev,AU_IRQENDP), cp->intbufp,
1143 irqsize, auerswald_int_complete, cp, ep->desc.bInterval);
1145 cp->inturbp->status = 0; /* needed! */
1146 ret = usb_submit_urb (cp->inturbp, GFP_KERNEL);
1150 /* activation of interrupt endpoint has failed. Now clean up. */
1151 dbg ("auerswald_int_open: activation of int endpoint failed");
1153 /* deallocate memory */
1154 auerswald_int_free (cp);
1159 /* This function is called to deactivate the interrupt
1160 endpoint. This function returns 0 if successful or an error code.
1161 NOTE: no mutex please!
1163 static void auerswald_int_release (pauerswald_t cp)
1165 dbg ("auerswald_int_release");
1167 /* stop the int endpoint */
1169 usb_kill_urb (cp->inturbp);
1171 /* deallocate memory */
1172 auerswald_int_free (cp);
1175 /* --------------------------------------------------------------------- */
1176 /* Helper functions */
1178 /* wake up waiting readers */
1179 static void auerchar_disconnect (pauerscon_t scp)
1181 pauerchar_t ccp = ((pauerchar_t)((char *)(scp)-(unsigned long)(&((pauerchar_t)0)->scontext)));
1182 dbg ("auerchar_disconnect called");
1184 wake_up (&ccp->readwait);
1188 /* dispatch a read paket to a waiting character device */
1189 static void auerchar_ctrlread_dispatch (pauerscon_t scp, pauerbuf_t bp)
1191 unsigned long flags;
1193 pauerbuf_t newbp = NULL;
1195 dbg ("auerchar_ctrlread_dispatch called");
1196 ccp = ((pauerchar_t)((char *)(scp)-(unsigned long)(&((pauerchar_t)0)->scontext)));
1198 /* get a read buffer from character device context */
1199 spin_lock_irqsave (&ccp->bufctl.lock, flags);
1200 if (!list_empty (&ccp->bufctl.free_buff_list)) {
1201 /* yes: get the entry */
1202 struct list_head *tmp = ccp->bufctl.free_buff_list.next;
1204 newbp = list_entry (tmp, auerbuf_t, buff_list);
1206 spin_unlock_irqrestore (&ccp->bufctl.lock, flags);
1209 dbg ("No read buffer available, discard paket!");
1210 return; /* no buffer, no dispatch */
1213 /* copy information to new buffer element
1214 (all buffers have the same length) */
1215 charp = newbp->bufp;
1216 newbp->bufp = bp->bufp;
1218 newbp->len = bp->len;
1220 /* insert new buffer in read list */
1221 spin_lock_irqsave (&ccp->bufctl.lock, flags);
1222 list_add_tail (&newbp->buff_list, &ccp->bufctl.rec_buff_list);
1223 spin_unlock_irqrestore (&ccp->bufctl.lock, flags);
1224 dbg ("read buffer appended to rec_list");
1226 /* wake up pending synchronous reads */
1227 wake_up (&ccp->readwait);
1231 /* Delete an auerswald driver context */
1232 static void auerswald_delete( pauerswald_t cp)
1234 dbg( "auerswald_delete");
1238 /* Wake up all processes waiting for a buffer */
1239 wake_up (&cp->bufferwait);
1242 auerswald_int_release (cp);
1243 auerchain_free (&cp->controlchain);
1244 auerbuf_free_buffers (&cp->bufctl);
1246 /* release the memory */
1251 /* Delete an auerswald character context */
1252 static void auerchar_delete( pauerchar_t ccp)
1254 dbg ("auerchar_delete");
1258 /* wake up pending synchronous reads */
1260 wake_up (&ccp->readwait);
1262 /* remove the read buffer */
1264 auerbuf_releasebuf (ccp->readbuf);
1265 ccp->readbuf = NULL;
1268 /* remove the character buffers */
1269 auerbuf_free_buffers (&ccp->bufctl);
1271 /* release the memory */
1276 /* add a new service to the device
1277 scp->id must be set!
1278 return: 0 if OK, else error code
1280 static int auerswald_addservice (pauerswald_t cp, pauerscon_t scp)
1284 /* is the device available? */
1286 dbg ("usbdev == NULL");
1287 return -EIO; /*no: can not add a service, sorry*/
1290 /* is the service available? */
1291 if (cp->services[scp->id]) {
1292 dbg ("service is busy");
1296 /* device is available, service is free */
1297 cp->services[scp->id] = scp;
1299 /* register service in device */
1300 ret = auerchain_control_msg(
1301 &cp->controlchain, /* pointer to control chain */
1302 cp->usbdev, /* pointer to device */
1303 usb_sndctrlpipe (cp->usbdev, 0), /* pipe to control endpoint */
1304 AUV_CHANNELCTL, /* USB message request value */
1305 AUT_WREQ, /* USB message request type value */
1306 0x01, /* open USB message value */
1307 scp->id, /* USB message index value */
1308 NULL, /* pointer to the data to send */
1309 0, /* length in bytes of the data to send */
1310 HZ * 2); /* time to wait for the message to complete before timing out */
1312 dbg ("auerswald_addservice: auerchain_control_msg returned error code %d", ret);
1313 /* undo above actions */
1314 cp->services[scp->id] = NULL;
1318 dbg ("auerswald_addservice: channel open OK");
1323 /* remove a service from the the device
1324 scp->id must be set! */
1325 static void auerswald_removeservice (pauerswald_t cp, pauerscon_t scp)
1327 dbg ("auerswald_removeservice called");
1329 /* check if we have a service allocated */
1330 if (scp->id == AUH_UNASSIGNED)
1333 /* If there is a device: close the channel */
1335 /* Close the service channel inside the device */
1336 int ret = auerchain_control_msg(
1337 &cp->controlchain, /* pointer to control chain */
1338 cp->usbdev, /* pointer to device */
1339 usb_sndctrlpipe (cp->usbdev, 0), /* pipe to control endpoint */
1340 AUV_CHANNELCTL, /* USB message request value */
1341 AUT_WREQ, /* USB message request type value */
1342 0x00, // close /* USB message value */
1343 scp->id, /* USB message index value */
1344 NULL, /* pointer to the data to send */
1345 0, /* length in bytes of the data to send */
1346 HZ * 2); /* time to wait for the message to complete before timing out */
1348 dbg ("auerswald_removeservice: auerchain_control_msg returned error code %d", ret);
1351 dbg ("auerswald_removeservice: channel close OK");
1355 /* remove the service from the device */
1356 cp->services[scp->id] = NULL;
1357 scp->id = AUH_UNASSIGNED;
1361 /* --------------------------------------------------------------------- */
1362 /* Char device functions */
1364 /* Open a new character device */
1365 static int auerchar_open (struct inode *inode, struct file *file)
1367 int dtindex = iminor(inode);
1368 pauerswald_t cp = NULL;
1369 pauerchar_t ccp = NULL;
1370 struct usb_interface *intf;
1373 /* minor number in range? */
1377 intf = usb_find_interface(&auerswald_driver, dtindex);
1382 /* usb device available? */
1383 cp = usb_get_intfdata (intf);
1387 if (down_interruptible (&cp->mutex)) {
1388 return -ERESTARTSYS;
1391 /* we have access to the device. Now lets allocate memory */
1392 ccp = (pauerchar_t) kmalloc(sizeof(auerchar_t), GFP_KERNEL);
1394 err ("out of memory");
1399 /* Initialize device descriptor */
1400 memset( ccp, 0, sizeof(auerchar_t));
1401 init_MUTEX( &ccp->mutex);
1402 init_MUTEX( &ccp->readmutex);
1403 auerbuf_init (&ccp->bufctl);
1404 ccp->scontext.id = AUH_UNASSIGNED;
1405 ccp->scontext.dispatch = auerchar_ctrlread_dispatch;
1406 ccp->scontext.disconnect = auerchar_disconnect;
1407 init_waitqueue_head (&ccp->readwait);
1409 ret = auerbuf_setup (&ccp->bufctl, AU_RBUFFERS, cp->maxControlLength+AUH_SIZE);
1416 dbg("open %s as /dev/%s", cp->dev_desc, cp->name);
1421 file->private_data = ccp;
1422 return nonseekable_open(inode, file);
1425 ofail: up (&cp->mutex);
1426 auerchar_delete (ccp);
1431 /* IOCTL functions */
1432 static int auerchar_ioctl (struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
1434 pauerchar_t ccp = (pauerchar_t) file->private_data;
1436 audevinfo_t devinfo;
1437 pauerswald_t cp = NULL;
1439 unsigned int __user *user_arg = (unsigned int __user *)arg;
1443 /* get the mutexes */
1444 if (down_interruptible (&ccp->mutex)) {
1445 return -ERESTARTSYS;
1452 if (down_interruptible (&cp->mutex)) {
1454 return -ERESTARTSYS;
1457 /* Check for removal */
1466 /* return != 0 if Transmitt channel ready to send */
1467 case IOCTL_AU_TXREADY:
1468 dbg ("IOCTL_AU_TXREADY");
1470 && (ccp->scontext.id != AUH_UNASSIGNED)
1471 && !list_empty (&cp->bufctl.free_buff_list);
1472 ret = put_user (u, user_arg);
1475 /* return != 0 if connected to a service channel */
1476 case IOCTL_AU_CONNECT:
1477 dbg ("IOCTL_AU_CONNECT");
1478 u = (ccp->scontext.id != AUH_UNASSIGNED);
1479 ret = put_user (u, user_arg);
1482 /* return != 0 if Receive Data available */
1483 case IOCTL_AU_RXAVAIL:
1484 dbg ("IOCTL_AU_RXAVAIL");
1485 if (ccp->scontext.id == AUH_UNASSIGNED) {
1489 u = 0; /* no data */
1491 int restlen = ccp->readbuf->len - ccp->readoffset;
1496 if (!list_empty (&ccp->bufctl.rec_buff_list)) {
1500 ret = put_user (u, user_arg);
1503 /* return the max. buffer length for the device */
1504 case IOCTL_AU_BUFLEN:
1505 dbg ("IOCTL_AU_BUFLEN");
1506 u = cp->maxControlLength;
1507 ret = put_user (u, user_arg);
1510 /* requesting a service channel */
1511 case IOCTL_AU_SERVREQ:
1512 dbg ("IOCTL_AU_SERVREQ");
1513 /* requesting a service means: release the previous one first */
1514 auerswald_removeservice (cp, &ccp->scontext);
1515 /* get the channel number */
1516 ret = get_user (u, user_arg);
1520 if ((u < AUH_FIRSTUSERCH) || (u >= AUH_TYPESIZE)) {
1524 dbg ("auerchar service request parameters are ok");
1525 ccp->scontext.id = u;
1527 /* request the service now */
1528 ret = auerswald_addservice (cp, &ccp->scontext);
1530 /* no: revert service entry */
1531 ccp->scontext.id = AUH_UNASSIGNED;
1535 /* get a string descriptor for the device */
1536 case IOCTL_AU_DEVINFO:
1537 dbg ("IOCTL_AU_DEVINFO");
1538 if (copy_from_user (&devinfo, (void __user *) arg, sizeof (audevinfo_t))) {
1542 u = strlen(cp->dev_desc)+1;
1543 if (u > devinfo.bsize) {
1546 ret = copy_to_user(devinfo.buf, cp->dev_desc, u) ? -EFAULT : 0;
1549 /* get the max. string descriptor length */
1551 dbg ("IOCTL_AU_SLEN");
1553 ret = put_user (u, user_arg);
1557 dbg ("IOCTL_AU_UNKNOWN");
1561 /* release the mutexes */
1567 /* Read data from the device */
1568 static ssize_t auerchar_read (struct file *file, char __user *buf, size_t count, loff_t * ppos)
1570 unsigned long flags;
1571 pauerchar_t ccp = (pauerchar_t) file->private_data;
1572 pauerbuf_t bp = NULL;
1575 dbg ("auerchar_read");
1577 /* Error checking */
1586 if (down_interruptible (&ccp->mutex))
1587 return -ERESTARTSYS;
1589 /* Can we expect to read something? */
1590 if (ccp->scontext.id == AUH_UNASSIGNED) {
1595 /* only one reader per device allowed */
1596 if (down_interruptible (&ccp->readmutex)) {
1598 return -ERESTARTSYS;
1601 /* read data from readbuf, if available */
1605 /* read the maximum bytes */
1606 int restlen = bp->len - ccp->readoffset;
1609 if (count > restlen)
1612 if (copy_to_user (buf, bp->bufp+ccp->readoffset, count)) {
1613 dbg ("auerswald_read: copy_to_user failed");
1614 up (&ccp->readmutex);
1619 /* advance the read offset */
1620 ccp->readoffset += count;
1622 // reuse the read buffer
1624 auerbuf_releasebuf (bp);
1625 ccp->readbuf = NULL;
1627 /* return with number of bytes read */
1629 up (&ccp->readmutex);
1635 /* a read buffer is not available. Try to get the next data block. */
1637 /* Preparing for sleep */
1638 init_waitqueue_entry (&wait, current);
1639 set_current_state (TASK_INTERRUPTIBLE);
1640 add_wait_queue (&ccp->readwait, &wait);
1643 spin_lock_irqsave (&ccp->bufctl.lock, flags);
1644 if (!list_empty (&ccp->bufctl.rec_buff_list)) {
1645 /* yes: get the entry */
1646 struct list_head *tmp = ccp->bufctl.rec_buff_list.next;
1648 bp = list_entry (tmp, auerbuf_t, buff_list);
1650 spin_unlock_irqrestore (&ccp->bufctl.lock, flags);
1652 /* have we got data? */
1655 ccp->readoffset = AUH_SIZE; /* for headerbyte */
1656 set_current_state (TASK_RUNNING);
1657 remove_wait_queue (&ccp->readwait, &wait);
1658 goto doreadbuf; /* now we can read! */
1661 /* no data available. Should we wait? */
1662 if (file->f_flags & O_NONBLOCK) {
1663 dbg ("No read buffer available, returning -EAGAIN");
1664 set_current_state (TASK_RUNNING);
1665 remove_wait_queue (&ccp->readwait, &wait);
1666 up (&ccp->readmutex);
1668 return -EAGAIN; /* nonblocking, no data available */
1671 /* yes, we should wait! */
1672 up (&ccp->mutex); /* allow other operations while we wait */
1674 remove_wait_queue (&ccp->readwait, &wait);
1675 if (signal_pending (current)) {
1676 /* waked up by a signal */
1677 up (&ccp->readmutex);
1678 return -ERESTARTSYS;
1681 /* Anything left to read? */
1682 if ((ccp->scontext.id == AUH_UNASSIGNED) || ccp->removed) {
1683 up (&ccp->readmutex);
1687 if (down_interruptible (&ccp->mutex)) {
1688 up (&ccp->readmutex);
1689 return -ERESTARTSYS;
1692 /* try to read the incoming data again */
1697 /* Write a data block into the right service channel of the device */
1698 static ssize_t auerchar_write (struct file *file, const char __user *buf, size_t len, loff_t *ppos)
1700 pauerchar_t ccp = (pauerchar_t) file->private_data;
1701 pauerswald_t cp = NULL;
1703 unsigned long flags;
1707 dbg ("auerchar_write %d bytes", len);
1709 /* Error checking */
1719 if (down_interruptible (&ccp->mutex))
1720 return -ERESTARTSYS;
1722 /* Can we expect to write something? */
1723 if (ccp->scontext.id == AUH_UNASSIGNED) {
1731 return -ERESTARTSYS;
1733 if (down_interruptible (&cp->mutex)) {
1735 return -ERESTARTSYS;
1742 /* Prepare for sleep */
1743 init_waitqueue_entry (&wait, current);
1744 set_current_state (TASK_INTERRUPTIBLE);
1745 add_wait_queue (&cp->bufferwait, &wait);
1747 /* Try to get a buffer from the device pool.
1748 We can't use a buffer from ccp->bufctl because the write
1749 command will last beond a release() */
1751 spin_lock_irqsave (&cp->bufctl.lock, flags);
1752 if (!list_empty (&cp->bufctl.free_buff_list)) {
1753 /* yes: get the entry */
1754 struct list_head *tmp = cp->bufctl.free_buff_list.next;
1756 bp = list_entry (tmp, auerbuf_t, buff_list);
1758 spin_unlock_irqrestore (&cp->bufctl.lock, flags);
1760 /* are there any buffers left? */
1765 /* NONBLOCK: don't wait */
1766 if (file->f_flags & O_NONBLOCK) {
1767 set_current_state (TASK_RUNNING);
1768 remove_wait_queue (&cp->bufferwait, &wait);
1772 /* BLOCKING: wait */
1774 remove_wait_queue (&cp->bufferwait, &wait);
1775 if (signal_pending (current)) {
1776 /* waked up by a signal */
1777 return -ERESTARTSYS;
1781 set_current_state (TASK_RUNNING);
1782 remove_wait_queue (&cp->bufferwait, &wait);
1785 /* protect against too big write requests */
1786 if (len > cp->maxControlLength)
1787 len = cp->maxControlLength;
1789 /* Fill the buffer */
1790 if (copy_from_user ( bp->bufp+AUH_SIZE, buf, len)) {
1791 dbg ("copy_from_user failed");
1792 auerbuf_releasebuf (bp);
1793 /* Wake up all processes waiting for a buffer */
1794 wake_up (&cp->bufferwait);
1800 /* set the header byte */
1801 *(bp->bufp) = ccp->scontext.id | AUH_DIRECT | AUH_UNSPLIT;
1803 /* Set the transfer Parameters */
1804 bp->len = len+AUH_SIZE;
1805 bp->dr->bRequestType = AUT_WREQ;
1806 bp->dr->bRequest = AUV_WBLOCK;
1807 bp->dr->wValue = cpu_to_le16 (0);
1808 bp->dr->wIndex = cpu_to_le16 (ccp->scontext.id | AUH_DIRECT | AUH_UNSPLIT);
1809 bp->dr->wLength = cpu_to_le16 (len+AUH_SIZE);
1810 usb_fill_control_urb (bp->urbp, cp->usbdev, usb_sndctrlpipe (cp->usbdev, 0),
1811 (unsigned char*)bp->dr, bp->bufp, len+AUH_SIZE,
1812 auerchar_ctrlwrite_complete, bp);
1814 ret = auerchain_submit_urb (&cp->controlchain, bp->urbp);
1817 dbg ("auerchar_write: nonzero result of auerchain_submit_urb %d", ret);
1818 auerbuf_releasebuf (bp);
1819 /* Wake up all processes waiting for a buffer */
1820 wake_up (&cp->bufferwait);
1825 dbg ("auerchar_write: Write OK");
1832 /* Close a character device */
1833 static int auerchar_release (struct inode *inode, struct file *file)
1835 pauerchar_t ccp = (pauerchar_t) file->private_data;
1839 /* get the mutexes */
1840 if (down_interruptible (&ccp->mutex)) {
1841 return -ERESTARTSYS;
1845 if (down_interruptible (&cp->mutex)) {
1847 return -ERESTARTSYS;
1849 /* remove an open service */
1850 auerswald_removeservice (cp, &ccp->scontext);
1851 /* detach from device */
1852 if ((--cp->open_count <= 0) && (cp->usbdev == NULL)) {
1853 /* usb device waits for removal */
1855 auerswald_delete (cp);
1860 ccp->auerdev = NULL;
1863 auerchar_delete (ccp);
1869 /*----------------------------------------------------------------------*/
1870 /* File operation structure */
1871 static struct file_operations auerswald_fops =
1873 .owner = THIS_MODULE,
1874 .llseek = no_llseek,
1875 .read = auerchar_read,
1876 .write = auerchar_write,
1877 .ioctl = auerchar_ioctl,
1878 .open = auerchar_open,
1879 .release = auerchar_release,
1882 static struct usb_class_driver auerswald_class = {
1883 .name = "usb/auer%d",
1884 .fops = &auerswald_fops,
1885 .mode = S_IFCHR | S_IRUGO | S_IWUGO,
1886 .minor_base = AUER_MINOR_BASE,
1890 /* --------------------------------------------------------------------- */
1891 /* Special USB driver functions */
1893 /* Probe if this driver wants to serve an USB device
1895 This entry point is called whenever a new device is attached to the bus.
1896 Then the device driver has to create a new instance of its internal data
1897 structures for the new device.
1899 The dev argument specifies the device context, which contains pointers
1900 to all USB descriptors. The interface argument specifies the interface
1901 number. If a USB driver wants to bind itself to a particular device and
1902 interface it has to return a pointer. This pointer normally references
1903 the device driver's context structure.
1905 Probing normally is done by checking the vendor and product identifications
1906 or the class and subclass definitions. If they match the interface number
1907 is compared with the ones supported by the driver. When probing is done
1908 class based it might be necessary to parse some more USB descriptors because
1909 the device properties can differ in a wide range.
1911 static int auerswald_probe (struct usb_interface *intf,
1912 const struct usb_device_id *id)
1914 struct usb_device *usbdev = interface_to_usbdev(intf);
1915 pauerswald_t cp = NULL;
1920 dbg ("probe: vendor id 0x%x, device id 0x%x",
1921 le16_to_cpu(usbdev->descriptor.idVendor),
1922 le16_to_cpu(usbdev->descriptor.idProduct));
1924 /* we use only the first -and only- interface */
1925 if (intf->altsetting->desc.bInterfaceNumber != 0)
1928 /* allocate memory for our device and initialize it */
1929 cp = kmalloc (sizeof(auerswald_t), GFP_KERNEL);
1931 err ("out of memory");
1935 /* Initialize device descriptor */
1936 memset (cp, 0, sizeof(auerswald_t));
1937 init_MUTEX (&cp->mutex);
1938 cp->usbdev = usbdev;
1939 auerchain_init (&cp->controlchain);
1940 auerbuf_init (&cp->bufctl);
1941 init_waitqueue_head (&cp->bufferwait);
1943 ret = usb_register_dev(intf, &auerswald_class);
1945 err ("Not able to get a minor for this device.");
1949 /* Give the device a name */
1950 sprintf (cp->name, "usb/auer%d", intf->minor);
1952 /* Store the index */
1953 cp->dtindex = intf->minor;
1955 /* Get the usb version of the device */
1956 cp->version = le16_to_cpu(cp->usbdev->descriptor.bcdDevice);
1957 dbg ("Version is %X", cp->version);
1959 /* allow some time to settle the device */
1962 /* Try to get a suitable textual description of the device */
1964 ret = usb_string( cp->usbdev, AUSI_DEVICE, cp->dev_desc, AUSI_DLEN-1);
1967 /* Append Serial Number */
1968 memcpy(&cp->dev_desc[u], ",Ser# ", 6);
1970 ret = usb_string( cp->usbdev, AUSI_SERIALNR, &cp->dev_desc[u], AUSI_DLEN-u-1);
1973 /* Append subscriber number */
1974 memcpy(&cp->dev_desc[u], ", ", 2);
1976 ret = usb_string( cp->usbdev, AUSI_MSN, &cp->dev_desc[u], AUSI_DLEN-u-1);
1982 cp->dev_desc[u] = '\0';
1983 info("device is a %s", cp->dev_desc);
1985 /* get the maximum allowed control transfer length */
1986 pbuf = (__le16 *) kmalloc (2, GFP_KERNEL); /* use an allocated buffer because of urb target */
1988 err( "out of memory");
1991 ret = usb_control_msg(cp->usbdev, /* pointer to device */
1992 usb_rcvctrlpipe( cp->usbdev, 0 ), /* pipe to control endpoint */
1993 AUV_GETINFO, /* USB message request value */
1994 AUT_RREQ, /* USB message request type value */
1995 0, /* USB message value */
1996 AUDI_MBCTRANS, /* USB message index value */
1997 pbuf, /* pointer to the receive buffer */
1998 2, /* length of the buffer */
1999 2000); /* time to wait for the message to complete before timing out */
2001 cp->maxControlLength = le16_to_cpup(pbuf);
2003 dbg("setup: max. allowed control transfersize is %d bytes", cp->maxControlLength);
2006 err("setup: getting max. allowed control transfer length failed with error %d", ret);
2010 /* allocate a chain for the control messages */
2011 if (auerchain_setup (&cp->controlchain, AUCH_ELEMENTS)) {
2012 err ("out of memory");
2016 /* allocate buffers for control messages */
2017 if (auerbuf_setup (&cp->bufctl, AU_RBUFFERS, cp->maxControlLength+AUH_SIZE)) {
2018 err ("out of memory");
2022 /* start the interrupt endpoint */
2023 if (auerswald_int_open (cp)) {
2024 err ("int endpoint failed");
2029 usb_set_intfdata (intf, cp);
2032 /* Error exit: clean up the memory */
2033 pfail: auerswald_delete (cp);
2038 /* Disconnect driver from a served device
2040 This function is called whenever a device which was served by this driver
2043 The argument dev specifies the device context and the driver_context
2044 returns a pointer to the previously registered driver_context of the
2045 probe function. After returning from the disconnect function the USB
2046 framework completely deallocates all data structures associated with
2047 this device. So especially the usb_device structure must not be used
2048 any longer by the usb driver.
2050 static void auerswald_disconnect (struct usb_interface *intf)
2052 pauerswald_t cp = usb_get_intfdata (intf);
2055 usb_set_intfdata (intf, NULL);
2060 info ("device /dev/%s now disconnecting", cp->name);
2062 /* give back our USB minor number */
2063 usb_deregister_dev(intf, &auerswald_class);
2065 /* Stop the interrupt endpoint */
2066 auerswald_int_release (cp);
2068 /* remove the control chain allocated in auerswald_probe
2069 This has the benefit of
2070 a) all pending (a)synchronous urbs are unlinked
2071 b) all buffers dealing with urbs are reclaimed
2073 auerchain_free (&cp->controlchain);
2075 if (cp->open_count == 0) {
2076 /* nobody is using this device. So we can clean up now */
2077 up (&cp->mutex);/* up() is possible here because no other task
2078 can open the device (see above). I don't want
2079 to kfree() a locked mutex. */
2080 auerswald_delete (cp);
2082 /* device is used. Remove the pointer to the
2083 usb device (it's not valid any more). The last
2084 release() will do the clean up */
2087 /* Terminate waiting writers */
2088 wake_up (&cp->bufferwait);
2089 /* Inform all waiting readers */
2090 for ( u = 0; u < AUH_TYPESIZE; u++) {
2091 pauerscon_t scp = cp->services[u];
2093 scp->disconnect( scp);
2098 /* Descriptor for the devices which are served by this driver.
2099 NOTE: this struct is parsed by the usbmanager install scripts.
2100 Don't change without caution!
2102 static struct usb_device_id auerswald_ids [] = {
2103 { USB_DEVICE (ID_AUERSWALD, 0x00C0) }, /* COMpact 2104 USB */
2104 { USB_DEVICE (ID_AUERSWALD, 0x00DB) }, /* COMpact 4410/2206 USB */
2105 { USB_DEVICE (ID_AUERSWALD, 0x00F1) }, /* Comfort 2000 System Telephone */
2106 { USB_DEVICE (ID_AUERSWALD, 0x00F2) }, /* Comfort 1200 System Telephone */
2107 { } /* Terminating entry */
2110 /* Standard module device table */
2111 MODULE_DEVICE_TABLE (usb, auerswald_ids);
2113 /* Standard usb driver struct */
2114 static struct usb_driver auerswald_driver = {
2115 .owner = THIS_MODULE,
2116 .name = "auerswald",
2117 .probe = auerswald_probe,
2118 .disconnect = auerswald_disconnect,
2119 .id_table = auerswald_ids,
2123 /* --------------------------------------------------------------------- */
2124 /* Module loading/unloading */
2126 /* Driver initialisation. Called after module loading.
2127 NOTE: there is no concurrency at _init
2129 static int __init auerswald_init (void)
2134 /* register driver at the USB subsystem */
2135 result = usb_register (&auerswald_driver);
2137 err ("driver could not be registered");
2143 /* Driver deinit. Called before module removal.
2144 NOTE: there is no concurrency at _cleanup
2146 static void __exit auerswald_cleanup (void)
2149 usb_deregister (&auerswald_driver);
2152 /* --------------------------------------------------------------------- */
2153 /* Linux device driver module description */
2155 MODULE_AUTHOR (DRIVER_AUTHOR);
2156 MODULE_DESCRIPTION (DRIVER_DESC);
2157 MODULE_LICENSE ("GPL");
2159 module_init (auerswald_init);
2160 module_exit (auerswald_cleanup);
2162 /* --------------------------------------------------------------------- */