Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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