Fix occurrences of "the the "
[deliverable/linux.git] / drivers / usb / misc / auerswald.c
CommitLineData
1da177e4
LT
1/*****************************************************************************/
2/*
3 * auerswald.c -- Auerswald PBX/System Telephone usb driver.
4 *
5 * Copyright (C) 2001 Wolfgang Mües (wolfgang@iksw-muees.de)
6 *
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.
9 *
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.
14 *
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.
19 *
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.
23 */
24 /*****************************************************************************/
25
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>
1da177e4
LT
33#include <linux/usb.h>
34
35/*-------------------------------------------------------------------*/
36/* Debug support */
37#ifdef DEBUG
38#define dump( adr, len) \
39do { \
40 unsigned int u; \
41 printk (KERN_DEBUG); \
42 for (u = 0; u < len; u++) \
43 printk (" %02X", adr[u] & 0xFF); \
44 printk ("\n"); \
45} while (0)
46#else
47#define dump( adr, len)
48#endif
49
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"
55
56/*-------------------------------------------------------------------*/
57/* Private declarations for Auerswald USB driver */
58
59/* Auerswald Vendor ID */
60#define ID_AUERSWALD 0x09BF
61
62#define AUER_MINOR_BASE 112 /* auerswald driver minor number */
63
64/* we can have up to this number of device plugged in at once */
65#define AUER_MAX_DEVICES 16
66
67
68/* Number of read buffers for each device */
69#define AU_RBUFFERS 10
70
71/* Number of chain elements for each control chain */
72#define AUCH_ELEMENTS 20
73
74/* Number of retries in communication */
75#define AU_RETRIES 10
76
77/*-------------------------------------------------------------------*/
78/* vendor specific protocol */
79/* Header Byte */
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 */
83
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,
87 split-byte follows */
88
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 */
102
103#define AUH_SIZE 1 /* Size of Header Byte */
104
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 */
109
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 */
113
114#define AUS_LENMASK 0x3F /* mask for block length information */
115
116/* Request types */
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 */
119
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 */
126
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 */
131
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 */
137
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 */
143
144#define AUSI_DLEN 100 /* Max. Length of Device Description */
145
146#define AUV_RETRY 0x101 /* First Firmware version which can do control retries */
147
148/*-------------------------------------------------------------------*/
149/* External data structures / Interface */
150typedef struct
151{
152 char __user *buf; /* return buffer for string contents */
153 unsigned int bsize; /* size of return buffer */
154} audevinfo_t,*paudevinfo_t;
155
156/* IO controls */
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 */
165
166/*-------------------------------------------------------------------*/
167/* Internal data structures */
168
169/* ..................................................................*/
170/* urb chain element */
171struct auerchain; /* forward for circular reference */
172typedef struct
173{
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;
180
181/* urb chain */
182typedef struct auerchain
183{
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;
189
190/* urb blocking completion helper struct */
191typedef struct
192{
193 wait_queue_head_t wqh; /* wait for completion */
194 unsigned int done; /* completion flag */
195} auerchain_chs_t,*pauerchain_chs_t;
196
197/* ...................................................................*/
198/* buffer element */
199struct auerbufctl; /* forward */
200typedef struct
201{
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;
210
211/* buffer list control block */
212typedef struct auerbufctl
213{
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;
218
219/* ...................................................................*/
220/* service context */
221struct auerscon; /* forward */
222typedef void (*auer_dispatch_t)(struct auerscon*, pauerbuf_t);
223typedef void (*auer_disconn_t) (struct auerscon*);
224typedef struct auerscon
225{
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;
230
231/* ...................................................................*/
232/* USB device context */
233typedef struct
234{
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;
251
252/* ................................................................... */
253/* character device context */
254typedef struct
255{
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;
266
267
268/*-------------------------------------------------------------------*/
269/* Forwards */
7d12e780 270static void auerswald_ctrlread_complete (struct urb * urb);
1da177e4
LT
271static void auerswald_removeservice (pauerswald_t cp, pauerscon_t scp);
272static struct usb_driver auerswald_driver;
273
274
275/*-------------------------------------------------------------------*/
276/* USB chain helper functions */
277/* -------------------------- */
278
279/* completion function for chained urbs */
7d12e780 280static void auerchain_complete (struct urb * urb)
1da177e4
LT
281{
282 unsigned long flags;
283 int result;
284
285 /* get pointer to element and to chain */
286 pauerchainelement_t acep = (pauerchainelement_t) urb->context;
287 pauerchain_t acp = acep->chain;
288
289 /* restore original entries in urb */
290 urb->context = acep->context;
291 urb->complete = acep->complete;
292
293 dbg ("auerchain_complete called");
294
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.*/
7d12e780 299 urb->complete (urb);
1da177e4
LT
300
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!");
305 else
306 acp->active = NULL;
307
308 /* add the used chain element to the list of free elements */
309 list_add_tail (&acep->list, &acp->free_list);
310 acep = NULL;
311
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;
316 list_del (tmp);
317 acep = list_entry (tmp, auerchainelement_t, list);
318 acp->active = acep;
319 }
320 spin_unlock_irqrestore (&acp->lock, flags);
321
322 /* submit the new urb */
323 if (acep) {
324 urb = acep->urbp;
325 dbg ("auerchain_complete: submitting next urb from chain");
326 urb->status = 0; /* needed! */
327 result = usb_submit_urb(urb, GFP_ATOMIC);
328
329 /* check for submit errors */
330 if (result) {
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) */
7d12e780 334 auerchain_complete( urb);
1da177e4
LT
335 }
336 } else {
337 /* simple return without submitting a new urb.
338 The empty chain is detected with acp->active == NULL. */
339 };
340}
341
342
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
346*/
347static int auerchain_submit_urb_list (pauerchain_t acp, struct urb * urb, int early)
348{
349 int result;
350 unsigned long flags;
351 pauerchainelement_t acep = NULL;
352
353 dbg ("auerchain_submit_urb called");
354
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;
360 list_del (tmp);
361 acep = list_entry (tmp, auerchainelement_t, list);
362 }
363 spin_unlock_irqrestore (&acp->lock, flags);
364
365 /* if no chain element available: return with error */
366 if (!acep) {
367 return -ENOMEM;
368 }
369
370 /* fill in the new chain element values */
371 acep->chain = acp;
372 acep->context = urb->context;
373 acep->complete = urb->complete;
374 acep->urbp = urb;
375 INIT_LIST_HEAD (&acep->list);
376
377 /* modify urb */
378 urb->context = acep;
379 urb->complete = auerchain_complete;
380 urb->status = -EINPROGRESS; /* usb_submit_urb does this, too */
381
382 /* add element to chain - or start it immediately */
383 spin_lock_irqsave (&acp->lock, flags);
384 if (acp->active) {
385 /* there is traffic in the chain, simple add element to chain */
386 if (early) {
387 dbg ("adding new urb to head of chain");
388 list_add (&acep->list, &acp->waiting_list);
389 } else {
390 dbg ("adding new urb to end of chain");
391 list_add_tail (&acep->list, &acp->waiting_list);
392 }
393 acep = NULL;
394 } else {
395 /* the chain is empty. Prepare restart */
396 acp->active = acep;
397 }
398 /* Spin has to be removed before usb_submit_urb! */
399 spin_unlock_irqrestore (&acp->lock, flags);
400
401 /* Submit urb if immediate restart */
402 if (acep) {
403 dbg("submitting urb immediate");
404 urb->status = 0; /* needed! */
405 result = usb_submit_urb(urb, GFP_ATOMIC);
406 /* check for submit errors */
407 if (result) {
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 */
7d12e780 411 auerchain_complete( urb);
1da177e4
LT
412 }
413 }
414
415 return 0;
416}
417
418/* submit function for chained urbs
419 this function may be called from completion context or from user space!
420*/
421static int auerchain_submit_urb (pauerchain_t acp, struct urb * urb)
422{
423 return auerchain_submit_urb_list (acp, urb, 0);
424}
425
426/* cancel an urb which is submitted to the chain
427 the result is 0 if the urb is cancelled, or -EINPROGRESS if
b375a049 428 the function is successfully started.
1da177e4
LT
429*/
430static int auerchain_unlink_urb (pauerchain_t acp, struct urb * urb)
431{
432 unsigned long flags;
433 struct urb * urbp;
434 pauerchainelement_t acep;
435 struct list_head *tmp;
436
437 dbg ("auerchain_unlink_urb called");
438
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) {
444 list_del (tmp);
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;
7d12e780 451 urb->complete (urb);
1da177e4
LT
452 return 0;
453 }
454 }
455 /* not found. */
456 spin_unlock_irqrestore (&acp->lock, flags);
457
458 /* get the active urb */
459 acep = acp->active;
460 if (acep) {
461 urbp = acep->urbp;
462
463 /* check if we have to cancel the active urb */
464 if (urbp == 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
469 wants to grab it.
470 */
471 dbg ("unlink active urb");
472 return usb_unlink_urb (urbp);
473 }
474 }
475
476 /* not found anyway
477 ... is some kind of success
478 */
479 dbg ("urb to unlink not found in chain");
480 return 0;
481}
482
483/* cancel all urbs which are in the chain.
484 this function must not be called from interrupt or completion handler.
485*/
486static void auerchain_unlink_all (pauerchain_t acp)
487{
488 unsigned long flags;
489 struct urb * urbp;
490 pauerchainelement_t acep;
491
492 dbg ("auerchain_unlink_all called");
493
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;
499 list_del (tmp);
500 acep = list_entry (tmp, auerchainelement_t, list);
501 urbp = acep->urbp;
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;
7d12e780 508 urbp->complete (urbp);
1da177e4
LT
509 spin_lock_irqsave (&acp->lock, flags);
510 }
511 spin_unlock_irqrestore (&acp->lock, flags);
512
513 /* clear the active urb */
514 acep = acp->active;
515 if (acep) {
516 urbp = acep->urbp;
1da177e4
LT
517 dbg ("unlink active urb");
518 usb_kill_urb (urbp);
519 }
520}
521
522
523/* free the chain.
524 this function must not be called from interrupt or completion handler.
525*/
526static void auerchain_free (pauerchain_t acp)
527{
528 unsigned long flags;
529 pauerchainelement_t acep;
530
531 dbg ("auerchain_free called");
532
533 /* first, cancel all pending urbs */
534 auerchain_unlink_all (acp);
535
536 /* free the elements */
537 spin_lock_irqsave (&acp->lock, flags);
538 while (!list_empty (&acp->free_list)) {
539 /* get the next entry */
540 struct list_head *tmp = acp->free_list.next;
541 list_del (tmp);
542 spin_unlock_irqrestore (&acp->lock, flags);
543 acep = list_entry (tmp, auerchainelement_t, list);
544 kfree (acep);
545 spin_lock_irqsave (&acp->lock, flags);
546 }
547 spin_unlock_irqrestore (&acp->lock, flags);
548}
549
550
551/* Init the chain control structure */
552static void auerchain_init (pauerchain_t acp)
553{
554 /* init the chain data structure */
555 acp->active = NULL;
556 spin_lock_init (&acp->lock);
557 INIT_LIST_HEAD (&acp->waiting_list);
558 INIT_LIST_HEAD (&acp->free_list);
559}
560
561/* setup a chain.
562 It is assumed that there is no concurrency while setting up the chain
563 requirement: auerchain_init()
564*/
565static int auerchain_setup (pauerchain_t acp, unsigned int numElements)
566{
567 pauerchainelement_t acep;
568
569 dbg ("auerchain_setup called with %d elements", numElements);
570
571 /* fill the list of free elements */
572 for (;numElements; numElements--) {
80b6ca48 573 acep = kzalloc(sizeof(auerchainelement_t), GFP_KERNEL);
1da177e4
LT
574 if (!acep)
575 goto ac_fail;
1da177e4
LT
576 INIT_LIST_HEAD (&acep->list);
577 list_add_tail (&acep->list, &acp->free_list);
578 }
579 return 0;
580
581ac_fail:/* free the elements */
582 while (!list_empty (&acp->free_list)) {
583 /* get the next entry */
584 struct list_head *tmp = acp->free_list.next;
585 list_del (tmp);
586 acep = list_entry (tmp, auerchainelement_t, list);
587 kfree (acep);
588 }
589 return -ENOMEM;
590}
591
592
593/* completion handler for synchronous chained URBs */
7d12e780 594static void auerchain_blocking_completion (struct urb *urb)
1da177e4
LT
595{
596 pauerchain_chs_t pchs = (pauerchain_chs_t)urb->context;
597 pchs->done = 1;
598 wmb();
599 wake_up (&pchs->wqh);
600}
601
602
603/* Starts chained urb and waits for completion or timeout */
604static int auerchain_start_wait_urb (pauerchain_t acp, struct urb *urb, int timeout, int* actual_length)
605{
606 auerchain_chs_t chs;
607 int status;
608
609 dbg ("auerchain_start_wait_urb called");
610 init_waitqueue_head (&chs.wqh);
611 chs.done = 0;
612
613 urb->context = &chs;
614 status = auerchain_submit_urb (acp, urb);
615 if (status)
616 /* something went wrong */
617 return status;
618
619 timeout = wait_event_timeout(chs.wqh, chs.done, timeout);
620
621 if (!timeout && !chs.done) {
622 if (urb->status != -EINPROGRESS) { /* No callback?!! */
623 dbg ("auerchain_start_wait_urb: raced timeout");
624 status = urb->status;
625 } else {
626 dbg ("auerchain_start_wait_urb: timeout");
627 auerchain_unlink_urb (acp, urb); /* remove urb safely */
628 status = -ETIMEDOUT;
629 }
630 } else
631 status = urb->status;
632
633 if (actual_length)
634 *actual_length = urb->actual_length;
635
636 return status;
637}
638
639
640/* auerchain_control_msg - Builds a control urb, sends it off and waits for completion
641 acp: pointer to the auerchain
642 dev: pointer to the usb device to send the message to
643 pipe: endpoint "pipe" to send the message to
644 request: USB message request value
645 requesttype: USB message request type value
646 value: USB message value
647 index: USB message index value
648 data: pointer to the data to send
649 size: length in bytes of the data to send
650 timeout: time to wait for the message to complete before timing out (if 0 the wait is forever)
651
652 This function sends a simple control message to a specified endpoint
653 and waits for the message to complete, or timeout.
654
655 If successful, it returns the transferred length, otherwise a negative error number.
656
657 Don't use this function from within an interrupt context, like a
658 bottom half handler. If you need an asynchronous message, or need to send
659 a message from within interrupt context, use auerchain_submit_urb()
660*/
661static int auerchain_control_msg (pauerchain_t acp, struct usb_device *dev, unsigned int pipe, __u8 request, __u8 requesttype,
662 __u16 value, __u16 index, void *data, __u16 size, int timeout)
663{
664 int ret;
665 struct usb_ctrlrequest *dr;
666 struct urb *urb;
667 int length;
668
669 dbg ("auerchain_control_msg");
670 dr = kmalloc (sizeof (struct usb_ctrlrequest), GFP_KERNEL);
671 if (!dr)
672 return -ENOMEM;
673 urb = usb_alloc_urb (0, GFP_KERNEL);
674 if (!urb) {
675 kfree (dr);
676 return -ENOMEM;
677 }
678
679 dr->bRequestType = requesttype;
680 dr->bRequest = request;
681 dr->wValue = cpu_to_le16 (value);
682 dr->wIndex = cpu_to_le16 (index);
683 dr->wLength = cpu_to_le16 (size);
684
685 usb_fill_control_urb (urb, dev, pipe, (unsigned char*)dr, data, size, /* build urb */
686 auerchain_blocking_completion, NULL);
687 ret = auerchain_start_wait_urb (acp, urb, timeout, &length);
688
689 usb_free_urb (urb);
690 kfree (dr);
691
692 if (ret < 0)
693 return ret;
694 else
695 return length;
696}
697
698
699/*-------------------------------------------------------------------*/
700/* Buffer List helper functions */
701
702/* free a single auerbuf */
703static void auerbuf_free (pauerbuf_t bp)
704{
1bc3c9e1
JJ
705 kfree(bp->bufp);
706 kfree(bp->dr);
2891a51c 707 usb_free_urb(bp->urbp);
1bc3c9e1 708 kfree(bp);
1da177e4
LT
709}
710
711/* free the buffers from an auerbuf list */
712static void auerbuf_free_list (struct list_head *q)
713{
714 struct list_head *tmp;
715 struct list_head *p;
716 pauerbuf_t bp;
717
718 dbg ("auerbuf_free_list");
719 for (p = q->next; p != q;) {
720 bp = list_entry (p, auerbuf_t, buff_list);
721 tmp = p->next;
722 list_del (p);
723 p = tmp;
724 auerbuf_free (bp);
725 }
726}
727
728/* init the members of a list control block */
729static void auerbuf_init (pauerbufctl_t bcp)
730{
731 dbg ("auerbuf_init");
732 spin_lock_init (&bcp->lock);
733 INIT_LIST_HEAD (&bcp->free_buff_list);
734 INIT_LIST_HEAD (&bcp->rec_buff_list);
735}
736
737/* free all buffers from an auerbuf chain */
738static void auerbuf_free_buffers (pauerbufctl_t bcp)
739{
740 unsigned long flags;
741 dbg ("auerbuf_free_buffers");
742
743 spin_lock_irqsave (&bcp->lock, flags);
744
745 auerbuf_free_list (&bcp->free_buff_list);
746 auerbuf_free_list (&bcp->rec_buff_list);
747
748 spin_unlock_irqrestore (&bcp->lock, flags);
749}
750
751/* setup a list of buffers */
752/* requirement: auerbuf_init() */
753static int auerbuf_setup (pauerbufctl_t bcp, unsigned int numElements, unsigned int bufsize)
754{
755 pauerbuf_t bep = NULL;
756
757 dbg ("auerbuf_setup called with %d elements of %d bytes", numElements, bufsize);
758
759 /* fill the list of free elements */
760 for (;numElements; numElements--) {
80b6ca48 761 bep = kzalloc(sizeof(auerbuf_t), GFP_KERNEL);
1da177e4
LT
762 if (!bep)
763 goto bl_fail;
1da177e4
LT
764 bep->list = bcp;
765 INIT_LIST_HEAD (&bep->buff_list);
0e8eb0f0 766 bep->bufp = kmalloc (bufsize, GFP_KERNEL);
1da177e4
LT
767 if (!bep->bufp)
768 goto bl_fail;
5cbded58 769 bep->dr = kmalloc(sizeof (struct usb_ctrlrequest), GFP_KERNEL);
1da177e4
LT
770 if (!bep->dr)
771 goto bl_fail;
772 bep->urbp = usb_alloc_urb (0, GFP_KERNEL);
773 if (!bep->urbp)
774 goto bl_fail;
775 list_add_tail (&bep->buff_list, &bcp->free_buff_list);
776 }
777 return 0;
778
779bl_fail:/* not enough memory. Free allocated elements */
780 dbg ("auerbuf_setup: no more memory");
5a3fcf5c 781 auerbuf_free(bep);
1da177e4
LT
782 auerbuf_free_buffers (bcp);
783 return -ENOMEM;
784}
785
786/* insert a used buffer into the free list */
787static void auerbuf_releasebuf( pauerbuf_t bp)
788{
789 unsigned long flags;
790 pauerbufctl_t bcp = bp->list;
791 bp->retries = 0;
792
793 dbg ("auerbuf_releasebuf called");
794 spin_lock_irqsave (&bcp->lock, flags);
795 list_add_tail (&bp->buff_list, &bcp->free_buff_list);
796 spin_unlock_irqrestore (&bcp->lock, flags);
797}
798
799
800/*-------------------------------------------------------------------*/
801/* Completion handlers */
802
803/* Values of urb->status or results of usb_submit_urb():
8040 Initial, OK
805-EINPROGRESS during submission until end
806-ENOENT if urb is unlinked
38e2bfc9 807-ETIME Device did not respond
1da177e4
LT
808-ENOMEM Memory Overflow
809-ENODEV Specified USB-device or bus doesn't exist
810-ENXIO URB already queued
811-EINVAL a) Invalid transfer type specified (or not supported)
812 b) Invalid interrupt interval (0n256)
813-EAGAIN a) Specified ISO start frame too early
814 b) (using ISO-ASAP) Too much scheduled for the future wait some time and try again.
815-EFBIG Too much ISO frames requested (currently uhci900)
816-EPIPE Specified pipe-handle/Endpoint is already stalled
817-EMSGSIZE Endpoint message size is zero, do interface/alternate setting
818-EPROTO a) Bitstuff error
819 b) Unknown USB error
820-EILSEQ CRC mismatch
821-ENOSR Buffer error
822-EREMOTEIO Short packet detected
823-EXDEV ISO transfer only partially completed look at individual frame status for details
824-EINVAL ISO madness, if this happens: Log off and go home
825-EOVERFLOW babble
826*/
827
828/* check if a status code allows a retry */
829static int auerswald_status_retry (int status)
830{
831 switch (status) {
832 case 0:
38e2bfc9 833 case -ETIME:
1da177e4
LT
834 case -EOVERFLOW:
835 case -EAGAIN:
836 case -EPIPE:
837 case -EPROTO:
838 case -EILSEQ:
839 case -ENOSR:
840 case -EREMOTEIO:
841 return 1; /* do a retry */
842 }
843 return 0; /* no retry possible */
844}
845
846/* Completion of asynchronous write block */
7d12e780 847static void auerchar_ctrlwrite_complete (struct urb * urb)
1da177e4
LT
848{
849 pauerbuf_t bp = (pauerbuf_t) urb->context;
850 pauerswald_t cp = ((pauerswald_t)((char *)(bp->list)-(unsigned long)(&((pauerswald_t)0)->bufctl)));
851 dbg ("auerchar_ctrlwrite_complete called");
852
853 /* reuse the buffer */
854 auerbuf_releasebuf (bp);
855 /* Wake up all processes waiting for a buffer */
856 wake_up (&cp->bufferwait);
857}
858
859/* Completion handler for dummy retry packet */
7d12e780 860static void auerswald_ctrlread_wretcomplete (struct urb * urb)
1da177e4
LT
861{
862 pauerbuf_t bp = (pauerbuf_t) urb->context;
863 pauerswald_t cp;
864 int ret;
865 dbg ("auerswald_ctrlread_wretcomplete called");
866 dbg ("complete with status: %d", urb->status);
867 cp = ((pauerswald_t)((char *)(bp->list)-(unsigned long)(&((pauerswald_t)0)->bufctl)));
868
869 /* check if it is possible to advance */
870 if (!auerswald_status_retry (urb->status) || !cp->usbdev) {
871 /* reuse the buffer */
872 err ("control dummy: transmission error %d, can not retry", urb->status);
873 auerbuf_releasebuf (bp);
874 /* Wake up all processes waiting for a buffer */
875 wake_up (&cp->bufferwait);
876 return;
877 }
878
879 /* fill the control message */
880 bp->dr->bRequestType = AUT_RREQ;
881 bp->dr->bRequest = AUV_RBLOCK;
882 bp->dr->wLength = bp->dr->wValue; /* temporary stored */
883 bp->dr->wValue = cpu_to_le16 (1); /* Retry Flag */
884 /* bp->dr->index = channel id; remains */
885 usb_fill_control_urb (bp->urbp, cp->usbdev, usb_rcvctrlpipe (cp->usbdev, 0),
886 (unsigned char*)bp->dr, bp->bufp, le16_to_cpu (bp->dr->wLength),
887 auerswald_ctrlread_complete,bp);
888
889 /* submit the control msg as next paket */
890 ret = auerchain_submit_urb_list (&cp->controlchain, bp->urbp, 1);
891 if (ret) {
892 dbg ("auerswald_ctrlread_complete: nonzero result of auerchain_submit_urb_list %d", ret);
893 bp->urbp->status = ret;
7d12e780 894 auerswald_ctrlread_complete (bp->urbp);
1da177e4
LT
895 }
896}
897
898/* completion handler for receiving of control messages */
7d12e780 899static void auerswald_ctrlread_complete (struct urb * urb)
1da177e4
LT
900{
901 unsigned int serviceid;
902 pauerswald_t cp;
903 pauerscon_t scp;
904 pauerbuf_t bp = (pauerbuf_t) urb->context;
905 int ret;
906 dbg ("auerswald_ctrlread_complete called");
907
908 cp = ((pauerswald_t)((char *)(bp->list)-(unsigned long)(&((pauerswald_t)0)->bufctl)));
909
910 /* check if there is valid data in this urb */
911 if (urb->status) {
912 dbg ("complete with non-zero status: %d", urb->status);
913 /* should we do a retry? */
914 if (!auerswald_status_retry (urb->status)
915 || !cp->usbdev
916 || (cp->version < AUV_RETRY)
917 || (bp->retries >= AU_RETRIES)) {
918 /* reuse the buffer */
919 err ("control read: transmission error %d, can not retry", urb->status);
920 auerbuf_releasebuf (bp);
921 /* Wake up all processes waiting for a buffer */
922 wake_up (&cp->bufferwait);
923 return;
924 }
925 bp->retries++;
926 dbg ("Retry count = %d", bp->retries);
927 /* send a long dummy control-write-message to allow device firmware to react */
928 bp->dr->bRequestType = AUT_WREQ;
929 bp->dr->bRequest = AUV_DUMMY;
930 bp->dr->wValue = bp->dr->wLength; /* temporary storage */
931 // bp->dr->wIndex channel ID remains
932 bp->dr->wLength = cpu_to_le16 (32); /* >= 8 bytes */
933 usb_fill_control_urb (bp->urbp, cp->usbdev, usb_sndctrlpipe (cp->usbdev, 0),
934 (unsigned char*)bp->dr, bp->bufp, 32,
935 auerswald_ctrlread_wretcomplete,bp);
936
937 /* submit the control msg as next paket */
938 ret = auerchain_submit_urb_list (&cp->controlchain, bp->urbp, 1);
939 if (ret) {
940 dbg ("auerswald_ctrlread_complete: nonzero result of auerchain_submit_urb_list %d", ret);
941 bp->urbp->status = ret;
7d12e780 942 auerswald_ctrlread_wretcomplete (bp->urbp);
1da177e4
LT
943 }
944 return;
945 }
946
947 /* get the actual bytecount (incl. headerbyte) */
948 bp->len = urb->actual_length;
949 serviceid = bp->bufp[0] & AUH_TYPEMASK;
950 dbg ("Paket with serviceid %d and %d bytes received", serviceid, bp->len);
951
952 /* dispatch the paket */
953 scp = cp->services[serviceid];
954 if (scp) {
955 /* look, Ma, a listener! */
956 scp->dispatch (scp, bp);
957 }
958
959 /* release the paket */
960 auerbuf_releasebuf (bp);
961 /* Wake up all processes waiting for a buffer */
962 wake_up (&cp->bufferwait);
963}
964
965/*-------------------------------------------------------------------*/
966/* Handling of Interrupt Endpoint */
967/* This interrupt Endpoint is used to inform the host about waiting
968 messages from the USB device.
969*/
970/* int completion handler. */
7d12e780 971static void auerswald_int_complete (struct urb * urb)
1da177e4
LT
972{
973 unsigned long flags;
974 unsigned int channelid;
975 unsigned int bytecount;
976 int ret;
977 pauerbuf_t bp = NULL;
978 pauerswald_t cp = (pauerswald_t) urb->context;
979
980 dbg ("%s called", __FUNCTION__);
981
982 switch (urb->status) {
983 case 0:
984 /* success */
985 break;
986 case -ECONNRESET:
987 case -ENOENT:
988 case -ESHUTDOWN:
989 /* this urb is terminated, clean up */
990 dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status);
991 return;
992 default:
993 dbg("%s - nonzero urb status received: %d", __FUNCTION__, urb->status);
994 goto exit;
995 }
996
997 /* check if all needed data was received */
998 if (urb->actual_length < AU_IRQMINSIZE) {
999 dbg ("invalid data length received: %d bytes", urb->actual_length);
1000 goto exit;
1001 }
1002
1003 /* check the command code */
1004 if (cp->intbufp[0] != AU_IRQCMDID) {
1005 dbg ("invalid command received: %d", cp->intbufp[0]);
1006 goto exit;
1007 }
1008
1009 /* check the command type */
1010 if (cp->intbufp[1] != AU_BLOCKRDY) {
1011 dbg ("invalid command type received: %d", cp->intbufp[1]);
1012 goto exit;
1013 }
1014
1015 /* now extract the information */
1016 channelid = cp->intbufp[2];
1017 bytecount = (unsigned char)cp->intbufp[3];
1018 bytecount |= (unsigned char)cp->intbufp[4] << 8;
1019
1020 /* check the channel id */
1021 if (channelid >= AUH_TYPESIZE) {
1022 dbg ("invalid channel id received: %d", channelid);
1023 goto exit;
1024 }
1025
1026 /* check the byte count */
1027 if (bytecount > (cp->maxControlLength+AUH_SIZE)) {
1028 dbg ("invalid byte count received: %d", bytecount);
1029 goto exit;
1030 }
1031 dbg ("Service Channel = %d", channelid);
1032 dbg ("Byte Count = %d", bytecount);
1033
1034 /* get a buffer for the next data paket */
1035 spin_lock_irqsave (&cp->bufctl.lock, flags);
1036 if (!list_empty (&cp->bufctl.free_buff_list)) {
1037 /* yes: get the entry */
1038 struct list_head *tmp = cp->bufctl.free_buff_list.next;
1039 list_del (tmp);
1040 bp = list_entry (tmp, auerbuf_t, buff_list);
1041 }
1042 spin_unlock_irqrestore (&cp->bufctl.lock, flags);
1043
1044 /* if no buffer available: skip it */
1045 if (!bp) {
1046 dbg ("auerswald_int_complete: no data buffer available");
1047 /* can we do something more?
1048 This is a big problem: if this int packet is ignored, the
1049 device will wait forever and not signal any more data.
1050 The only real solution is: having enough buffers!
1051 Or perhaps temporary disabling the int endpoint?
1052 */
1053 goto exit;
1054 }
1055
1056 /* fill the control message */
1057 bp->dr->bRequestType = AUT_RREQ;
1058 bp->dr->bRequest = AUV_RBLOCK;
1059 bp->dr->wValue = cpu_to_le16 (0);
1060 bp->dr->wIndex = cpu_to_le16 (channelid | AUH_DIRECT | AUH_UNSPLIT);
1061 bp->dr->wLength = cpu_to_le16 (bytecount);
1062 usb_fill_control_urb (bp->urbp, cp->usbdev, usb_rcvctrlpipe (cp->usbdev, 0),
1063 (unsigned char*)bp->dr, bp->bufp, bytecount,
1064 auerswald_ctrlread_complete,bp);
1065
1066 /* submit the control msg */
1067 ret = auerchain_submit_urb (&cp->controlchain, bp->urbp);
1068 if (ret) {
1069 dbg ("auerswald_int_complete: nonzero result of auerchain_submit_urb %d", ret);
1070 bp->urbp->status = ret;
7d12e780 1071 auerswald_ctrlread_complete( bp->urbp);
1da177e4
LT
1072 /* here applies the same problem as above: device locking! */
1073 }
1074exit:
1075 ret = usb_submit_urb (urb, GFP_ATOMIC);
1076 if (ret)
1077 err ("%s - usb_submit_urb failed with result %d",
1078 __FUNCTION__, ret);
1079}
1080
1081/* int memory deallocation
1082 NOTE: no mutex please!
1083*/
1084static void auerswald_int_free (pauerswald_t cp)
1085{
1bc3c9e1
JJ
1086 if (cp->inturbp) {
1087 usb_free_urb(cp->inturbp);
1088 cp->inturbp = NULL;
1089 }
1090 kfree(cp->intbufp);
1091 cp->intbufp = NULL;
1da177e4
LT
1092}
1093
1094/* This function is called to activate the interrupt
1095 endpoint. This function returns 0 if successful or an error code.
1096 NOTE: no mutex please!
1097*/
1098static int auerswald_int_open (pauerswald_t cp)
1099{
1100 int ret;
1101 struct usb_host_endpoint *ep;
1102 int irqsize;
1103 dbg ("auerswald_int_open");
1104
1105 ep = cp->usbdev->ep_in[AU_IRQENDP];
1106 if (!ep) {
1107 ret = -EFAULT;
1108 goto intoend;
1109 }
1110 irqsize = le16_to_cpu(ep->desc.wMaxPacketSize);
1111 cp->irqsize = irqsize;
1112
1113 /* allocate the urb and data buffer */
1114 if (!cp->inturbp) {
1115 cp->inturbp = usb_alloc_urb (0, GFP_KERNEL);
1116 if (!cp->inturbp) {
1117 ret = -ENOMEM;
1118 goto intoend;
1119 }
1120 }
1121 if (!cp->intbufp) {
0e8eb0f0 1122 cp->intbufp = kmalloc (irqsize, GFP_KERNEL);
1da177e4
LT
1123 if (!cp->intbufp) {
1124 ret = -ENOMEM;
1125 goto intoend;
1126 }
1127 }
1128 /* setup urb */
1129 usb_fill_int_urb (cp->inturbp, cp->usbdev,
1130 usb_rcvintpipe (cp->usbdev,AU_IRQENDP), cp->intbufp,
1131 irqsize, auerswald_int_complete, cp, ep->desc.bInterval);
1132 /* start the urb */
1133 cp->inturbp->status = 0; /* needed! */
1134 ret = usb_submit_urb (cp->inturbp, GFP_KERNEL);
1135
1136intoend:
1137 if (ret < 0) {
1138 /* activation of interrupt endpoint has failed. Now clean up. */
1139 dbg ("auerswald_int_open: activation of int endpoint failed");
1140
1141 /* deallocate memory */
1142 auerswald_int_free (cp);
1143 }
1144 return ret;
1145}
1146
1147/* This function is called to deactivate the interrupt
1148 endpoint. This function returns 0 if successful or an error code.
1149 NOTE: no mutex please!
1150*/
1151static void auerswald_int_release (pauerswald_t cp)
1152{
1153 dbg ("auerswald_int_release");
1154
1155 /* stop the int endpoint */
2891a51c 1156 usb_kill_urb (cp->inturbp);
1da177e4
LT
1157
1158 /* deallocate memory */
1159 auerswald_int_free (cp);
1160}
1161
1162/* --------------------------------------------------------------------- */
1163/* Helper functions */
1164
1165/* wake up waiting readers */
1166static void auerchar_disconnect (pauerscon_t scp)
1167{
1168 pauerchar_t ccp = ((pauerchar_t)((char *)(scp)-(unsigned long)(&((pauerchar_t)0)->scontext)));
1169 dbg ("auerchar_disconnect called");
1170 ccp->removed = 1;
1171 wake_up (&ccp->readwait);
1172}
1173
1174
1175/* dispatch a read paket to a waiting character device */
1176static void auerchar_ctrlread_dispatch (pauerscon_t scp, pauerbuf_t bp)
1177{
1178 unsigned long flags;
1179 pauerchar_t ccp;
1180 pauerbuf_t newbp = NULL;
1181 char * charp;
1182 dbg ("auerchar_ctrlread_dispatch called");
1183 ccp = ((pauerchar_t)((char *)(scp)-(unsigned long)(&((pauerchar_t)0)->scontext)));
1184
1185 /* get a read buffer from character device context */
1186 spin_lock_irqsave (&ccp->bufctl.lock, flags);
1187 if (!list_empty (&ccp->bufctl.free_buff_list)) {
1188 /* yes: get the entry */
1189 struct list_head *tmp = ccp->bufctl.free_buff_list.next;
1190 list_del (tmp);
1191 newbp = list_entry (tmp, auerbuf_t, buff_list);
1192 }
1193 spin_unlock_irqrestore (&ccp->bufctl.lock, flags);
1194
1195 if (!newbp) {
1196 dbg ("No read buffer available, discard paket!");
1197 return; /* no buffer, no dispatch */
1198 }
1199
1200 /* copy information to new buffer element
1201 (all buffers have the same length) */
1202 charp = newbp->bufp;
1203 newbp->bufp = bp->bufp;
1204 bp->bufp = charp;
1205 newbp->len = bp->len;
1206
1207 /* insert new buffer in read list */
1208 spin_lock_irqsave (&ccp->bufctl.lock, flags);
1209 list_add_tail (&newbp->buff_list, &ccp->bufctl.rec_buff_list);
1210 spin_unlock_irqrestore (&ccp->bufctl.lock, flags);
1211 dbg ("read buffer appended to rec_list");
1212
1213 /* wake up pending synchronous reads */
1214 wake_up (&ccp->readwait);
1215}
1216
1217
1218/* Delete an auerswald driver context */
1219static void auerswald_delete( pauerswald_t cp)
1220{
1221 dbg( "auerswald_delete");
1222 if (cp == NULL)
1223 return;
1224
1225 /* Wake up all processes waiting for a buffer */
1226 wake_up (&cp->bufferwait);
1227
1228 /* Cleaning up */
1229 auerswald_int_release (cp);
1230 auerchain_free (&cp->controlchain);
1231 auerbuf_free_buffers (&cp->bufctl);
1232
1233 /* release the memory */
1234 kfree( cp);
1235}
1236
1237
1238/* Delete an auerswald character context */
1239static void auerchar_delete( pauerchar_t ccp)
1240{
1241 dbg ("auerchar_delete");
1242 if (ccp == NULL)
1243 return;
1244
1245 /* wake up pending synchronous reads */
1246 ccp->removed = 1;
1247 wake_up (&ccp->readwait);
1248
1249 /* remove the read buffer */
1250 if (ccp->readbuf) {
1251 auerbuf_releasebuf (ccp->readbuf);
1252 ccp->readbuf = NULL;
1253 }
1254
1255 /* remove the character buffers */
1256 auerbuf_free_buffers (&ccp->bufctl);
1257
1258 /* release the memory */
1259 kfree( ccp);
1260}
1261
1262
1263/* add a new service to the device
1264 scp->id must be set!
1265 return: 0 if OK, else error code
1266*/
1267static int auerswald_addservice (pauerswald_t cp, pauerscon_t scp)
1268{
1269 int ret;
1270
1271 /* is the device available? */
1272 if (!cp->usbdev) {
1273 dbg ("usbdev == NULL");
1274 return -EIO; /*no: can not add a service, sorry*/
1275 }
1276
1277 /* is the service available? */
1278 if (cp->services[scp->id]) {
1279 dbg ("service is busy");
1280 return -EBUSY;
1281 }
1282
1283 /* device is available, service is free */
1284 cp->services[scp->id] = scp;
1285
1286 /* register service in device */
1287 ret = auerchain_control_msg(
1288 &cp->controlchain, /* pointer to control chain */
1289 cp->usbdev, /* pointer to device */
1290 usb_sndctrlpipe (cp->usbdev, 0), /* pipe to control endpoint */
1291 AUV_CHANNELCTL, /* USB message request value */
1292 AUT_WREQ, /* USB message request type value */
1293 0x01, /* open USB message value */
1294 scp->id, /* USB message index value */
1295 NULL, /* pointer to the data to send */
1296 0, /* length in bytes of the data to send */
1297 HZ * 2); /* time to wait for the message to complete before timing out */
1298 if (ret < 0) {
1299 dbg ("auerswald_addservice: auerchain_control_msg returned error code %d", ret);
1300 /* undo above actions */
1301 cp->services[scp->id] = NULL;
1302 return ret;
1303 }
1304
1305 dbg ("auerswald_addservice: channel open OK");
1306 return 0;
1307}
1308
1309
59c51591 1310/* remove a service from the device
1da177e4
LT
1311 scp->id must be set! */
1312static void auerswald_removeservice (pauerswald_t cp, pauerscon_t scp)
1313{
1314 dbg ("auerswald_removeservice called");
1315
1316 /* check if we have a service allocated */
1317 if (scp->id == AUH_UNASSIGNED)
1318 return;
1319
1320 /* If there is a device: close the channel */
1321 if (cp->usbdev) {
1322 /* Close the service channel inside the device */
1323 int ret = auerchain_control_msg(
1324 &cp->controlchain, /* pointer to control chain */
1325 cp->usbdev, /* pointer to device */
1326 usb_sndctrlpipe (cp->usbdev, 0), /* pipe to control endpoint */
1327 AUV_CHANNELCTL, /* USB message request value */
1328 AUT_WREQ, /* USB message request type value */
1329 0x00, // close /* USB message value */
1330 scp->id, /* USB message index value */
1331 NULL, /* pointer to the data to send */
1332 0, /* length in bytes of the data to send */
1333 HZ * 2); /* time to wait for the message to complete before timing out */
1334 if (ret < 0) {
1335 dbg ("auerswald_removeservice: auerchain_control_msg returned error code %d", ret);
1336 }
1337 else {
1338 dbg ("auerswald_removeservice: channel close OK");
1339 }
1340 }
1341
1342 /* remove the service from the device */
1343 cp->services[scp->id] = NULL;
1344 scp->id = AUH_UNASSIGNED;
1345}
1346
1347
1348/* --------------------------------------------------------------------- */
1349/* Char device functions */
1350
1351/* Open a new character device */
1352static int auerchar_open (struct inode *inode, struct file *file)
1353{
1354 int dtindex = iminor(inode);
1355 pauerswald_t cp = NULL;
1356 pauerchar_t ccp = NULL;
1357 struct usb_interface *intf;
1358 int ret;
1359
1360 /* minor number in range? */
1361 if (dtindex < 0) {
1362 return -ENODEV;
1363 }
1364 intf = usb_find_interface(&auerswald_driver, dtindex);
1365 if (!intf) {
1366 return -ENODEV;
1367 }
1368
1369 /* usb device available? */
1370 cp = usb_get_intfdata (intf);
1371 if (cp == NULL) {
1372 return -ENODEV;
1373 }
1374 if (down_interruptible (&cp->mutex)) {
1375 return -ERESTARTSYS;
1376 }
1377
1378 /* we have access to the device. Now lets allocate memory */
66eb2e93 1379 ccp = kzalloc(sizeof(auerchar_t), GFP_KERNEL);
1da177e4
LT
1380 if (ccp == NULL) {
1381 err ("out of memory");
1382 ret = -ENOMEM;
1383 goto ofail;
1384 }
1385
1386 /* Initialize device descriptor */
1da177e4
LT
1387 init_MUTEX( &ccp->mutex);
1388 init_MUTEX( &ccp->readmutex);
1389 auerbuf_init (&ccp->bufctl);
1390 ccp->scontext.id = AUH_UNASSIGNED;
1391 ccp->scontext.dispatch = auerchar_ctrlread_dispatch;
1392 ccp->scontext.disconnect = auerchar_disconnect;
1393 init_waitqueue_head (&ccp->readwait);
1394
1395 ret = auerbuf_setup (&ccp->bufctl, AU_RBUFFERS, cp->maxControlLength+AUH_SIZE);
1396 if (ret) {
1397 goto ofail;
1398 }
1399
1400 cp->open_count++;
1401 ccp->auerdev = cp;
1402 dbg("open %s as /dev/%s", cp->dev_desc, cp->name);
1403 up (&cp->mutex);
1404
1405 /* file IO stuff */
1406 file->f_pos = 0;
1407 file->private_data = ccp;
1408 return nonseekable_open(inode, file);
1409
1410 /* Error exit */
1411ofail: up (&cp->mutex);
1412 auerchar_delete (ccp);
1413 return ret;
1414}
1415
1416
1417/* IOCTL functions */
1418static int auerchar_ioctl (struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
1419{
1420 pauerchar_t ccp = (pauerchar_t) file->private_data;
1421 int ret = 0;
1422 audevinfo_t devinfo;
1423 pauerswald_t cp = NULL;
1424 unsigned int u;
1425 unsigned int __user *user_arg = (unsigned int __user *)arg;
1426
1427 dbg ("ioctl");
1428
1429 /* get the mutexes */
1430 if (down_interruptible (&ccp->mutex)) {
1431 return -ERESTARTSYS;
1432 }
1433 cp = ccp->auerdev;
1434 if (!cp) {
1435 up (&ccp->mutex);
1436 return -ENODEV;
1437 }
1438 if (down_interruptible (&cp->mutex)) {
1439 up(&ccp->mutex);
1440 return -ERESTARTSYS;
1441 }
1442
1443 /* Check for removal */
1444 if (!cp->usbdev) {
1445 up(&cp->mutex);
1446 up(&ccp->mutex);
1447 return -ENODEV;
1448 }
1449
1450 switch (cmd) {
1451
1452 /* return != 0 if Transmitt channel ready to send */
1453 case IOCTL_AU_TXREADY:
1454 dbg ("IOCTL_AU_TXREADY");
1455 u = ccp->auerdev
1456 && (ccp->scontext.id != AUH_UNASSIGNED)
1457 && !list_empty (&cp->bufctl.free_buff_list);
1458 ret = put_user (u, user_arg);
1459 break;
1460
1461 /* return != 0 if connected to a service channel */
1462 case IOCTL_AU_CONNECT:
1463 dbg ("IOCTL_AU_CONNECT");
1464 u = (ccp->scontext.id != AUH_UNASSIGNED);
1465 ret = put_user (u, user_arg);
1466 break;
1467
1468 /* return != 0 if Receive Data available */
1469 case IOCTL_AU_RXAVAIL:
1470 dbg ("IOCTL_AU_RXAVAIL");
1471 if (ccp->scontext.id == AUH_UNASSIGNED) {
1472 ret = -EIO;
1473 break;
1474 }
1475 u = 0; /* no data */
1476 if (ccp->readbuf) {
1477 int restlen = ccp->readbuf->len - ccp->readoffset;
1478 if (restlen > 0)
1479 u = 1;
1480 }
1481 if (!u) {
1482 if (!list_empty (&ccp->bufctl.rec_buff_list)) {
1483 u = 1;
1484 }
1485 }
1486 ret = put_user (u, user_arg);
1487 break;
1488
1489 /* return the max. buffer length for the device */
1490 case IOCTL_AU_BUFLEN:
1491 dbg ("IOCTL_AU_BUFLEN");
1492 u = cp->maxControlLength;
1493 ret = put_user (u, user_arg);
1494 break;
1495
1496 /* requesting a service channel */
1497 case IOCTL_AU_SERVREQ:
1498 dbg ("IOCTL_AU_SERVREQ");
1499 /* requesting a service means: release the previous one first */
1500 auerswald_removeservice (cp, &ccp->scontext);
1501 /* get the channel number */
1502 ret = get_user (u, user_arg);
1503 if (ret) {
1504 break;
1505 }
1506 if ((u < AUH_FIRSTUSERCH) || (u >= AUH_TYPESIZE)) {
1507 ret = -EIO;
1508 break;
1509 }
1510 dbg ("auerchar service request parameters are ok");
1511 ccp->scontext.id = u;
1512
1513 /* request the service now */
1514 ret = auerswald_addservice (cp, &ccp->scontext);
1515 if (ret) {
1516 /* no: revert service entry */
1517 ccp->scontext.id = AUH_UNASSIGNED;
1518 }
1519 break;
1520
1521 /* get a string descriptor for the device */
1522 case IOCTL_AU_DEVINFO:
1523 dbg ("IOCTL_AU_DEVINFO");
1524 if (copy_from_user (&devinfo, (void __user *) arg, sizeof (audevinfo_t))) {
1525 ret = -EFAULT;
1526 break;
1527 }
1528 u = strlen(cp->dev_desc)+1;
1529 if (u > devinfo.bsize) {
1530 u = devinfo.bsize;
1531 }
1532 ret = copy_to_user(devinfo.buf, cp->dev_desc, u) ? -EFAULT : 0;
1533 break;
1534
1535 /* get the max. string descriptor length */
1536 case IOCTL_AU_SLEN:
1537 dbg ("IOCTL_AU_SLEN");
1538 u = AUSI_DLEN;
1539 ret = put_user (u, user_arg);
1540 break;
1541
1542 default:
1543 dbg ("IOCTL_AU_UNKNOWN");
1544 ret = -ENOIOCTLCMD;
1545 break;
1546 }
1547 /* release the mutexes */
1548 up(&cp->mutex);
1549 up(&ccp->mutex);
1550 return ret;
1551}
1552
1553/* Read data from the device */
1554static ssize_t auerchar_read (struct file *file, char __user *buf, size_t count, loff_t * ppos)
1555{
1556 unsigned long flags;
1557 pauerchar_t ccp = (pauerchar_t) file->private_data;
1558 pauerbuf_t bp = NULL;
1559 wait_queue_t wait;
1560
1561 dbg ("auerchar_read");
1562
1563 /* Error checking */
1564 if (!ccp)
1565 return -EIO;
1566 if (*ppos)
1567 return -ESPIPE;
1568 if (count == 0)
1569 return 0;
1570
1571 /* get the mutex */
1572 if (down_interruptible (&ccp->mutex))
1573 return -ERESTARTSYS;
1574
1575 /* Can we expect to read something? */
1576 if (ccp->scontext.id == AUH_UNASSIGNED) {
1577 up (&ccp->mutex);
1578 return -EIO;
1579 }
1580
1581 /* only one reader per device allowed */
1582 if (down_interruptible (&ccp->readmutex)) {
1583 up (&ccp->mutex);
1584 return -ERESTARTSYS;
1585 }
1586
1587 /* read data from readbuf, if available */
1588doreadbuf:
1589 bp = ccp->readbuf;
1590 if (bp) {
1591 /* read the maximum bytes */
1592 int restlen = bp->len - ccp->readoffset;
1593 if (restlen < 0)
1594 restlen = 0;
1595 if (count > restlen)
1596 count = restlen;
1597 if (count) {
1598 if (copy_to_user (buf, bp->bufp+ccp->readoffset, count)) {
1599 dbg ("auerswald_read: copy_to_user failed");
1600 up (&ccp->readmutex);
1601 up (&ccp->mutex);
1602 return -EFAULT;
1603 }
1604 }
1605 /* advance the read offset */
1606 ccp->readoffset += count;
1607 restlen -= count;
1608 // reuse the read buffer
1609 if (restlen <= 0) {
1610 auerbuf_releasebuf (bp);
1611 ccp->readbuf = NULL;
1612 }
1613 /* return with number of bytes read */
1614 if (count) {
1615 up (&ccp->readmutex);
1616 up (&ccp->mutex);
1617 return count;
1618 }
1619 }
1620
1621 /* a read buffer is not available. Try to get the next data block. */
1622doreadlist:
1623 /* Preparing for sleep */
1624 init_waitqueue_entry (&wait, current);
1625 set_current_state (TASK_INTERRUPTIBLE);
1626 add_wait_queue (&ccp->readwait, &wait);
1627
1628 bp = NULL;
1629 spin_lock_irqsave (&ccp->bufctl.lock, flags);
1630 if (!list_empty (&ccp->bufctl.rec_buff_list)) {
1631 /* yes: get the entry */
1632 struct list_head *tmp = ccp->bufctl.rec_buff_list.next;
1633 list_del (tmp);
1634 bp = list_entry (tmp, auerbuf_t, buff_list);
1635 }
1636 spin_unlock_irqrestore (&ccp->bufctl.lock, flags);
1637
1638 /* have we got data? */
1639 if (bp) {
1640 ccp->readbuf = bp;
1641 ccp->readoffset = AUH_SIZE; /* for headerbyte */
1642 set_current_state (TASK_RUNNING);
1643 remove_wait_queue (&ccp->readwait, &wait);
1644 goto doreadbuf; /* now we can read! */
1645 }
1646
1647 /* no data available. Should we wait? */
1648 if (file->f_flags & O_NONBLOCK) {
1649 dbg ("No read buffer available, returning -EAGAIN");
1650 set_current_state (TASK_RUNNING);
1651 remove_wait_queue (&ccp->readwait, &wait);
1652 up (&ccp->readmutex);
1653 up (&ccp->mutex);
1654 return -EAGAIN; /* nonblocking, no data available */
1655 }
1656
1657 /* yes, we should wait! */
1658 up (&ccp->mutex); /* allow other operations while we wait */
1659 schedule();
1660 remove_wait_queue (&ccp->readwait, &wait);
1661 if (signal_pending (current)) {
1662 /* waked up by a signal */
1663 up (&ccp->readmutex);
1664 return -ERESTARTSYS;
1665 }
1666
1667 /* Anything left to read? */
1668 if ((ccp->scontext.id == AUH_UNASSIGNED) || ccp->removed) {
1669 up (&ccp->readmutex);
1670 return -EIO;
1671 }
1672
1673 if (down_interruptible (&ccp->mutex)) {
1674 up (&ccp->readmutex);
1675 return -ERESTARTSYS;
1676 }
1677
1678 /* try to read the incoming data again */
1679 goto doreadlist;
1680}
1681
1682
1683/* Write a data block into the right service channel of the device */
1684static ssize_t auerchar_write (struct file *file, const char __user *buf, size_t len, loff_t *ppos)
1685{
1686 pauerchar_t ccp = (pauerchar_t) file->private_data;
1687 pauerswald_t cp = NULL;
1688 pauerbuf_t bp;
1689 unsigned long flags;
1690 int ret;
1691 wait_queue_t wait;
1692
53b3de1e 1693 dbg ("auerchar_write %zd bytes", len);
1da177e4
LT
1694
1695 /* Error checking */
1696 if (!ccp)
1697 return -EIO;
1698 if (*ppos)
1699 return -ESPIPE;
1700 if (len == 0)
1701 return 0;
1702
1703write_again:
1704 /* get the mutex */
1705 if (down_interruptible (&ccp->mutex))
1706 return -ERESTARTSYS;
1707
1708 /* Can we expect to write something? */
1709 if (ccp->scontext.id == AUH_UNASSIGNED) {
1710 up (&ccp->mutex);
1711 return -EIO;
1712 }
1713
1714 cp = ccp->auerdev;
1715 if (!cp) {
1716 up (&ccp->mutex);
1717 return -ERESTARTSYS;
1718 }
1719 if (down_interruptible (&cp->mutex)) {
1720 up (&ccp->mutex);
1721 return -ERESTARTSYS;
1722 }
1723 if (!cp->usbdev) {
1724 up (&cp->mutex);
1725 up (&ccp->mutex);
1726 return -EIO;
1727 }
1728 /* Prepare for sleep */
1729 init_waitqueue_entry (&wait, current);
1730 set_current_state (TASK_INTERRUPTIBLE);
1731 add_wait_queue (&cp->bufferwait, &wait);
1732
1733 /* Try to get a buffer from the device pool.
1734 We can't use a buffer from ccp->bufctl because the write
1735 command will last beond a release() */
1736 bp = NULL;
1737 spin_lock_irqsave (&cp->bufctl.lock, flags);
1738 if (!list_empty (&cp->bufctl.free_buff_list)) {
1739 /* yes: get the entry */
1740 struct list_head *tmp = cp->bufctl.free_buff_list.next;
1741 list_del (tmp);
1742 bp = list_entry (tmp, auerbuf_t, buff_list);
1743 }
1744 spin_unlock_irqrestore (&cp->bufctl.lock, flags);
1745
1746 /* are there any buffers left? */
1747 if (!bp) {
1748 up (&cp->mutex);
1749 up (&ccp->mutex);
1750
1751 /* NONBLOCK: don't wait */
1752 if (file->f_flags & O_NONBLOCK) {
1753 set_current_state (TASK_RUNNING);
1754 remove_wait_queue (&cp->bufferwait, &wait);
1755 return -EAGAIN;
1756 }
1757
1758 /* BLOCKING: wait */
1759 schedule();
1760 remove_wait_queue (&cp->bufferwait, &wait);
1761 if (signal_pending (current)) {
1762 /* waked up by a signal */
1763 return -ERESTARTSYS;
1764 }
1765 goto write_again;
1766 } else {
1767 set_current_state (TASK_RUNNING);
1768 remove_wait_queue (&cp->bufferwait, &wait);
1769 }
1770
1771 /* protect against too big write requests */
1772 if (len > cp->maxControlLength)
1773 len = cp->maxControlLength;
1774
1775 /* Fill the buffer */
1776 if (copy_from_user ( bp->bufp+AUH_SIZE, buf, len)) {
1777 dbg ("copy_from_user failed");
1778 auerbuf_releasebuf (bp);
1779 /* Wake up all processes waiting for a buffer */
1780 wake_up (&cp->bufferwait);
1781 up (&cp->mutex);
1782 up (&ccp->mutex);
1783 return -EFAULT;
1784 }
1785
1786 /* set the header byte */
1787 *(bp->bufp) = ccp->scontext.id | AUH_DIRECT | AUH_UNSPLIT;
1788
1789 /* Set the transfer Parameters */
1790 bp->len = len+AUH_SIZE;
1791 bp->dr->bRequestType = AUT_WREQ;
1792 bp->dr->bRequest = AUV_WBLOCK;
1793 bp->dr->wValue = cpu_to_le16 (0);
1794 bp->dr->wIndex = cpu_to_le16 (ccp->scontext.id | AUH_DIRECT | AUH_UNSPLIT);
1795 bp->dr->wLength = cpu_to_le16 (len+AUH_SIZE);
1796 usb_fill_control_urb (bp->urbp, cp->usbdev, usb_sndctrlpipe (cp->usbdev, 0),
1797 (unsigned char*)bp->dr, bp->bufp, len+AUH_SIZE,
1798 auerchar_ctrlwrite_complete, bp);
1799 /* up we go */
1800 ret = auerchain_submit_urb (&cp->controlchain, bp->urbp);
1801 up (&cp->mutex);
1802 if (ret) {
1803 dbg ("auerchar_write: nonzero result of auerchain_submit_urb %d", ret);
1804 auerbuf_releasebuf (bp);
1805 /* Wake up all processes waiting for a buffer */
1806 wake_up (&cp->bufferwait);
1807 up (&ccp->mutex);
1808 return -EIO;
1809 }
1810 else {
1811 dbg ("auerchar_write: Write OK");
1812 up (&ccp->mutex);
1813 return len;
1814 }
1815}
1816
1817
1818/* Close a character device */
1819static int auerchar_release (struct inode *inode, struct file *file)
1820{
1821 pauerchar_t ccp = (pauerchar_t) file->private_data;
1822 pauerswald_t cp;
1823 dbg("release");
1824
1825 /* get the mutexes */
1826 if (down_interruptible (&ccp->mutex)) {
1827 return -ERESTARTSYS;
1828 }
1829 cp = ccp->auerdev;
1830 if (cp) {
1831 if (down_interruptible (&cp->mutex)) {
1832 up (&ccp->mutex);
1833 return -ERESTARTSYS;
1834 }
1835 /* remove an open service */
1836 auerswald_removeservice (cp, &ccp->scontext);
1837 /* detach from device */
1838 if ((--cp->open_count <= 0) && (cp->usbdev == NULL)) {
1839 /* usb device waits for removal */
1840 up (&cp->mutex);
1841 auerswald_delete (cp);
1842 } else {
1843 up (&cp->mutex);
1844 }
1845 cp = NULL;
1846 ccp->auerdev = NULL;
1847 }
1848 up (&ccp->mutex);
1849 auerchar_delete (ccp);
1850
1851 return 0;
1852}
1853
1854
1855/*----------------------------------------------------------------------*/
1856/* File operation structure */
066202dd 1857static const struct file_operations auerswald_fops =
1da177e4
LT
1858{
1859 .owner = THIS_MODULE,
1860 .llseek = no_llseek,
1861 .read = auerchar_read,
1862 .write = auerchar_write,
1863 .ioctl = auerchar_ioctl,
1864 .open = auerchar_open,
1865 .release = auerchar_release,
1866};
1867
1868static struct usb_class_driver auerswald_class = {
d6e5bcf4 1869 .name = "auer%d",
1da177e4 1870 .fops = &auerswald_fops,
1da177e4
LT
1871 .minor_base = AUER_MINOR_BASE,
1872};
1873
1874
1875/* --------------------------------------------------------------------- */
1876/* Special USB driver functions */
1877
1878/* Probe if this driver wants to serve an USB device
1879
1880 This entry point is called whenever a new device is attached to the bus.
1881 Then the device driver has to create a new instance of its internal data
1882 structures for the new device.
1883
1884 The dev argument specifies the device context, which contains pointers
1885 to all USB descriptors. The interface argument specifies the interface
1886 number. If a USB driver wants to bind itself to a particular device and
1887 interface it has to return a pointer. This pointer normally references
1888 the device driver's context structure.
1889
1890 Probing normally is done by checking the vendor and product identifications
1891 or the class and subclass definitions. If they match the interface number
1892 is compared with the ones supported by the driver. When probing is done
1893 class based it might be necessary to parse some more USB descriptors because
1894 the device properties can differ in a wide range.
1895*/
1896static int auerswald_probe (struct usb_interface *intf,
1897 const struct usb_device_id *id)
1898{
1899 struct usb_device *usbdev = interface_to_usbdev(intf);
1900 pauerswald_t cp = NULL;
1901 unsigned int u = 0;
1902 __le16 *pbuf;
1903 int ret;
1904
1905 dbg ("probe: vendor id 0x%x, device id 0x%x",
1906 le16_to_cpu(usbdev->descriptor.idVendor),
1907 le16_to_cpu(usbdev->descriptor.idProduct));
1908
1909 /* we use only the first -and only- interface */
1910 if (intf->altsetting->desc.bInterfaceNumber != 0)
1911 return -ENODEV;
1912
1913 /* allocate memory for our device and initialize it */
66eb2e93 1914 cp = kzalloc (sizeof(auerswald_t), GFP_KERNEL);
1da177e4
LT
1915 if (cp == NULL) {
1916 err ("out of memory");
1917 goto pfail;
1918 }
1919
1920 /* Initialize device descriptor */
1da177e4
LT
1921 init_MUTEX (&cp->mutex);
1922 cp->usbdev = usbdev;
1923 auerchain_init (&cp->controlchain);
1924 auerbuf_init (&cp->bufctl);
1925 init_waitqueue_head (&cp->bufferwait);
1926
1927 ret = usb_register_dev(intf, &auerswald_class);
1928 if (ret) {
1929 err ("Not able to get a minor for this device.");
1930 goto pfail;
1931 }
1932
1933 /* Give the device a name */
1934 sprintf (cp->name, "usb/auer%d", intf->minor);
1935
1936 /* Store the index */
1937 cp->dtindex = intf->minor;
1938
1939 /* Get the usb version of the device */
1940 cp->version = le16_to_cpu(cp->usbdev->descriptor.bcdDevice);
1941 dbg ("Version is %X", cp->version);
1942
1943 /* allow some time to settle the device */
1944 msleep(334);
1945
1946 /* Try to get a suitable textual description of the device */
1947 /* Device name:*/
1948 ret = usb_string( cp->usbdev, AUSI_DEVICE, cp->dev_desc, AUSI_DLEN-1);
1949 if (ret >= 0) {
1950 u += ret;
1951 /* Append Serial Number */
1952 memcpy(&cp->dev_desc[u], ",Ser# ", 6);
1953 u += 6;
1954 ret = usb_string( cp->usbdev, AUSI_SERIALNR, &cp->dev_desc[u], AUSI_DLEN-u-1);
1955 if (ret >= 0) {
1956 u += ret;
1957 /* Append subscriber number */
1958 memcpy(&cp->dev_desc[u], ", ", 2);
1959 u += 2;
1960 ret = usb_string( cp->usbdev, AUSI_MSN, &cp->dev_desc[u], AUSI_DLEN-u-1);
1961 if (ret >= 0) {
1962 u += ret;
1963 }
1964 }
1965 }
1966 cp->dev_desc[u] = '\0';
1967 info("device is a %s", cp->dev_desc);
1968
1969 /* get the maximum allowed control transfer length */
5cbded58 1970 pbuf = kmalloc(2, GFP_KERNEL); /* use an allocated buffer because of urb target */
1da177e4
LT
1971 if (!pbuf) {
1972 err( "out of memory");
1973 goto pfail;
1974 }
1975 ret = usb_control_msg(cp->usbdev, /* pointer to device */
1976 usb_rcvctrlpipe( cp->usbdev, 0 ), /* pipe to control endpoint */
1977 AUV_GETINFO, /* USB message request value */
1978 AUT_RREQ, /* USB message request type value */
1979 0, /* USB message value */
1980 AUDI_MBCTRANS, /* USB message index value */
1981 pbuf, /* pointer to the receive buffer */
1982 2, /* length of the buffer */
1983 2000); /* time to wait for the message to complete before timing out */
1984 if (ret == 2) {
1985 cp->maxControlLength = le16_to_cpup(pbuf);
1986 kfree(pbuf);
1987 dbg("setup: max. allowed control transfersize is %d bytes", cp->maxControlLength);
1988 } else {
1989 kfree(pbuf);
1990 err("setup: getting max. allowed control transfer length failed with error %d", ret);
1991 goto pfail;
1992 }
1993
1994 /* allocate a chain for the control messages */
1995 if (auerchain_setup (&cp->controlchain, AUCH_ELEMENTS)) {
1996 err ("out of memory");
1997 goto pfail;
1998 }
1999
2000 /* allocate buffers for control messages */
2001 if (auerbuf_setup (&cp->bufctl, AU_RBUFFERS, cp->maxControlLength+AUH_SIZE)) {
2002 err ("out of memory");
2003 goto pfail;
2004 }
2005
2006 /* start the interrupt endpoint */
2007 if (auerswald_int_open (cp)) {
2008 err ("int endpoint failed");
2009 goto pfail;
2010 }
2011
2012 /* all OK */
2013 usb_set_intfdata (intf, cp);
2014 return 0;
2015
2016 /* Error exit: clean up the memory */
2017pfail: auerswald_delete (cp);
2018 return -EIO;
2019}
2020
2021
2022/* Disconnect driver from a served device
2023
2024 This function is called whenever a device which was served by this driver
2025 is disconnected.
2026
2027 The argument dev specifies the device context and the driver_context
2028 returns a pointer to the previously registered driver_context of the
2029 probe function. After returning from the disconnect function the USB
2030 framework completely deallocates all data structures associated with
2031 this device. So especially the usb_device structure must not be used
2032 any longer by the usb driver.
2033*/
2034static void auerswald_disconnect (struct usb_interface *intf)
2035{
2036 pauerswald_t cp = usb_get_intfdata (intf);
2037 unsigned int u;
2038
2039 usb_set_intfdata (intf, NULL);
2040 if (!cp)
2041 return;
2042
2043 down (&cp->mutex);
2044 info ("device /dev/%s now disconnecting", cp->name);
2045
2046 /* give back our USB minor number */
2047 usb_deregister_dev(intf, &auerswald_class);
2048
2049 /* Stop the interrupt endpoint */
2050 auerswald_int_release (cp);
2051
2052 /* remove the control chain allocated in auerswald_probe
2053 This has the benefit of
2054 a) all pending (a)synchronous urbs are unlinked
2055 b) all buffers dealing with urbs are reclaimed
2056 */
2057 auerchain_free (&cp->controlchain);
2058
2059 if (cp->open_count == 0) {
2060 /* nobody is using this device. So we can clean up now */
2061 up (&cp->mutex);/* up() is possible here because no other task
2062 can open the device (see above). I don't want
2063 to kfree() a locked mutex. */
2064 auerswald_delete (cp);
2065 } else {
2066 /* device is used. Remove the pointer to the
2067 usb device (it's not valid any more). The last
2068 release() will do the clean up */
2069 cp->usbdev = NULL;
2070 up (&cp->mutex);
2071 /* Terminate waiting writers */
2072 wake_up (&cp->bufferwait);
2073 /* Inform all waiting readers */
2074 for ( u = 0; u < AUH_TYPESIZE; u++) {
2075 pauerscon_t scp = cp->services[u];
2076 if (scp)
2077 scp->disconnect( scp);
2078 }
2079 }
2080}
2081
2082/* Descriptor for the devices which are served by this driver.
2083 NOTE: this struct is parsed by the usbmanager install scripts.
2084 Don't change without caution!
2085*/
2086static struct usb_device_id auerswald_ids [] = {
2087 { USB_DEVICE (ID_AUERSWALD, 0x00C0) }, /* COMpact 2104 USB */
2088 { USB_DEVICE (ID_AUERSWALD, 0x00DB) }, /* COMpact 4410/2206 USB */
885e7743
AM
2089 { USB_DEVICE (ID_AUERSWALD, 0x00DC) }, /* COMpact 4406 DSL */
2090 { USB_DEVICE (ID_AUERSWALD, 0x00DD) }, /* COMpact 2204 USB */
1da177e4
LT
2091 { USB_DEVICE (ID_AUERSWALD, 0x00F1) }, /* Comfort 2000 System Telephone */
2092 { USB_DEVICE (ID_AUERSWALD, 0x00F2) }, /* Comfort 1200 System Telephone */
2093 { } /* Terminating entry */
2094};
2095
2096/* Standard module device table */
2097MODULE_DEVICE_TABLE (usb, auerswald_ids);
2098
2099/* Standard usb driver struct */
2100static struct usb_driver auerswald_driver = {
1da177e4
LT
2101 .name = "auerswald",
2102 .probe = auerswald_probe,
2103 .disconnect = auerswald_disconnect,
2104 .id_table = auerswald_ids,
2105};
2106
2107
2108/* --------------------------------------------------------------------- */
2109/* Module loading/unloading */
2110
2111/* Driver initialisation. Called after module loading.
2112 NOTE: there is no concurrency at _init
2113*/
2114static int __init auerswald_init (void)
2115{
2116 int result;
2117 dbg ("init");
2118
2119 /* register driver at the USB subsystem */
2120 result = usb_register (&auerswald_driver);
2121 if (result < 0) {
2122 err ("driver could not be registered");
2123 return -1;
2124 }
2125 return 0;
2126}
2127
2128/* Driver deinit. Called before module removal.
2129 NOTE: there is no concurrency at _cleanup
2130*/
2131static void __exit auerswald_cleanup (void)
2132{
2133 dbg ("cleanup");
2134 usb_deregister (&auerswald_driver);
2135}
2136
2137/* --------------------------------------------------------------------- */
2138/* Linux device driver module description */
2139
2140MODULE_AUTHOR (DRIVER_AUTHOR);
2141MODULE_DESCRIPTION (DRIVER_DESC);
2142MODULE_LICENSE ("GPL");
2143
2144module_init (auerswald_init);
2145module_exit (auerswald_cleanup);
2146
2147/* --------------------------------------------------------------------- */
2148
This page took 0.387023 seconds and 5 git commands to generate.