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