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