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