1 /* 2 * Driver for the VoIP USB phones with CM109 chipsets. 3 * 4 * Copyright (C) 2007 - 2008 Alfred E. Heggestad <aeh@db.org> 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License as 8 * published by the Free Software Foundation, version 2. 9 */ 10 11 /* 12 * Tested devices: 13 * - Komunikate KIP1000 14 * - Genius G-talk 15 * - Allied-Telesis Corega USBPH01 16 * - ... 17 * 18 * This driver is based on the yealink.c driver 19 * 20 * Thanks to: 21 * - Authors of yealink.c 22 * - Thomas Reitmayr 23 * - Oliver Neukum for good review comments and code 24 * - Shaun Jackman <sjackman@gmail.com> for Genius G-talk keymap 25 * - Dmitry Torokhov for valuable input and review 26 * 27 * Todo: 28 * - Read/write EEPROM 29 */ 30 31 #include <linux/kernel.h> 32 #include <linux/init.h> 33 #include <linux/slab.h> 34 #include <linux/module.h> 35 #include <linux/moduleparam.h> 36 #include <linux/rwsem.h> 37 #include <linux/usb/input.h> 38 39 #define CM109_DEBUG 0 40 41 #define DRIVER_VERSION "20080805" 42 #define DRIVER_AUTHOR "Alfred E. Heggestad" 43 #define DRIVER_DESC "CM109 phone driver" 44 45 static char *phone = "kip1000"; 46 module_param(phone, charp, S_IRUSR); 47 MODULE_PARM_DESC(phone, "Phone name {kip1000, gtalk, usbph01}"); 48 49 enum { 50 /* HID Registers */ 51 HID_IR0 = 0x00, /* Record/Playback-mute button, Volume up/down */ 52 HID_IR1 = 0x01, /* GPI, generic registers or EEPROM_DATA0 */ 53 HID_IR2 = 0x02, /* Generic registers or EEPROM_DATA1 */ 54 HID_IR3 = 0x03, /* Generic registers or EEPROM_CTRL */ 55 HID_OR0 = 0x00, /* Mapping control, buzzer, SPDIF (offset 0x04) */ 56 HID_OR1 = 0x01, /* GPO - General Purpose Output */ 57 HID_OR2 = 0x02, /* Set GPIO to input/output mode */ 58 HID_OR3 = 0x03, /* SPDIF status channel or EEPROM_CTRL */ 59 60 /* HID_IR0 */ 61 RECORD_MUTE = 1 << 3, 62 PLAYBACK_MUTE = 1 << 2, 63 VOLUME_DOWN = 1 << 1, 64 VOLUME_UP = 1 << 0, 65 66 /* HID_OR0 */ 67 /* bits 7-6 68 0: HID_OR1-2 are used for GPO; HID_OR0, 3 are used for buzzer 69 and SPDIF 70 1: HID_OR0-3 are used as generic HID registers 71 2: Values written to HID_OR0-3 are also mapped to MCU_CTRL, 72 EEPROM_DATA0-1, EEPROM_CTRL (see Note) 73 3: Reserved 74 */ 75 HID_OR_GPO_BUZ_SPDIF = 0 << 6, 76 HID_OR_GENERIC_HID_REG = 1 << 6, 77 HID_OR_MAP_MCU_EEPROM = 2 << 6, 78 79 BUZZER_ON = 1 << 5, 80 81 /* up to 256 normal keys, up to 16 special keys */ 82 KEYMAP_SIZE = 256 + 16, 83 }; 84 85 /* CM109 protocol packet */ 86 struct cm109_ctl_packet { 87 u8 byte[4]; 88 } __attribute__ ((packed)); 89 90 enum { USB_PKT_LEN = sizeof(struct cm109_ctl_packet) }; 91 92 /* CM109 device structure */ 93 struct cm109_dev { 94 struct input_dev *idev; /* input device */ 95 struct usb_device *udev; /* usb device */ 96 struct usb_interface *intf; 97 98 /* irq input channel */ 99 struct cm109_ctl_packet *irq_data; 100 dma_addr_t irq_dma; 101 struct urb *urb_irq; 102 103 /* control output channel */ 104 struct cm109_ctl_packet *ctl_data; 105 dma_addr_t ctl_dma; 106 struct usb_ctrlrequest *ctl_req; 107 dma_addr_t ctl_req_dma; 108 struct urb *urb_ctl; 109 /* 110 * The 3 bitfields below are protected by ctl_submit_lock. 111 * They have to be separate since they are accessed from IRQ 112 * context. 113 */ 114 unsigned irq_urb_pending:1; /* irq_urb is in flight */ 115 unsigned ctl_urb_pending:1; /* ctl_urb is in flight */ 116 unsigned buzzer_pending:1; /* need to issue buzz command */ 117 spinlock_t ctl_submit_lock; 118 119 unsigned char buzzer_state; /* on/off */ 120 121 /* flags */ 122 unsigned open:1; 123 unsigned resetting:1; 124 unsigned shutdown:1; 125 126 /* This mutex protects writes to the above flags */ 127 struct mutex pm_mutex; 128 129 unsigned short keymap[KEYMAP_SIZE]; 130 131 char phys[64]; /* physical device path */ 132 int key_code; /* last reported key */ 133 int keybit; /* 0=new scan 1,2,4,8=scan columns */ 134 u8 gpi; /* Cached value of GPI (high nibble) */ 135 }; 136 137 /****************************************************************************** 138 * CM109 key interface 139 *****************************************************************************/ 140 141 static unsigned short special_keymap(int code) 142 { 143 if (code > 0xff) { 144 switch (code - 0xff) { 145 case RECORD_MUTE: return KEY_MUTE; 146 case PLAYBACK_MUTE: return KEY_MUTE; 147 case VOLUME_DOWN: return KEY_VOLUMEDOWN; 148 case VOLUME_UP: return KEY_VOLUMEUP; 149 } 150 } 151 return KEY_RESERVED; 152 } 153 154 /* Map device buttons to internal key events. 155 * 156 * The "up" and "down" keys, are symbolised by arrows on the button. 157 * The "pickup" and "hangup" keys are symbolised by a green and red phone 158 * on the button. 159 160 Komunikate KIP1000 Keyboard Matrix 161 162 -> -- 1 -- 2 -- 3 --> GPI pin 4 (0x10) 163 | | | | 164 <- -- 4 -- 5 -- 6 --> GPI pin 5 (0x20) 165 | | | | 166 END - 7 -- 8 -- 9 --> GPI pin 6 (0x40) 167 | | | | 168 OK -- * -- 0 -- # --> GPI pin 7 (0x80) 169 | | | | 170 171 /|\ /|\ /|\ /|\ 172 | | | | 173 GPO 174 pin: 3 2 1 0 175 0x8 0x4 0x2 0x1 176 177 */ 178 static unsigned short keymap_kip1000(int scancode) 179 { 180 switch (scancode) { /* phone key: */ 181 case 0x82: return KEY_NUMERIC_0; /* 0 */ 182 case 0x14: return KEY_NUMERIC_1; /* 1 */ 183 case 0x12: return KEY_NUMERIC_2; /* 2 */ 184 case 0x11: return KEY_NUMERIC_3; /* 3 */ 185 case 0x24: return KEY_NUMERIC_4; /* 4 */ 186 case 0x22: return KEY_NUMERIC_5; /* 5 */ 187 case 0x21: return KEY_NUMERIC_6; /* 6 */ 188 case 0x44: return KEY_NUMERIC_7; /* 7 */ 189 case 0x42: return KEY_NUMERIC_8; /* 8 */ 190 case 0x41: return KEY_NUMERIC_9; /* 9 */ 191 case 0x81: return KEY_NUMERIC_POUND; /* # */ 192 case 0x84: return KEY_NUMERIC_STAR; /* * */ 193 case 0x88: return KEY_ENTER; /* pickup */ 194 case 0x48: return KEY_ESC; /* hangup */ 195 case 0x28: return KEY_LEFT; /* IN */ 196 case 0x18: return KEY_RIGHT; /* OUT */ 197 default: return special_keymap(scancode); 198 } 199 } 200 201 /* 202 Contributed by Shaun Jackman <sjackman@gmail.com> 203 204 Genius G-Talk keyboard matrix 205 0 1 2 3 206 4: 0 4 8 Talk 207 5: 1 5 9 End 208 6: 2 6 # Up 209 7: 3 7 * Down 210 */ 211 static unsigned short keymap_gtalk(int scancode) 212 { 213 switch (scancode) { 214 case 0x11: return KEY_NUMERIC_0; 215 case 0x21: return KEY_NUMERIC_1; 216 case 0x41: return KEY_NUMERIC_2; 217 case 0x81: return KEY_NUMERIC_3; 218 case 0x12: return KEY_NUMERIC_4; 219 case 0x22: return KEY_NUMERIC_5; 220 case 0x42: return KEY_NUMERIC_6; 221 case 0x82: return KEY_NUMERIC_7; 222 case 0x14: return KEY_NUMERIC_8; 223 case 0x24: return KEY_NUMERIC_9; 224 case 0x44: return KEY_NUMERIC_POUND; /* # */ 225 case 0x84: return KEY_NUMERIC_STAR; /* * */ 226 case 0x18: return KEY_ENTER; /* Talk (green handset) */ 227 case 0x28: return KEY_ESC; /* End (red handset) */ 228 case 0x48: return KEY_UP; /* Menu up (rocker switch) */ 229 case 0x88: return KEY_DOWN; /* Menu down (rocker switch) */ 230 default: return special_keymap(scancode); 231 } 232 } 233 234 /* 235 * Keymap for Allied-Telesis Corega USBPH01 236 * http://www.alliedtelesis-corega.com/2/1344/1437/1360/chprd.html 237 * 238 * Contributed by july@nat.bg 239 */ 240 static unsigned short keymap_usbph01(int scancode) 241 { 242 switch (scancode) { 243 case 0x11: return KEY_NUMERIC_0; /* 0 */ 244 case 0x21: return KEY_NUMERIC_1; /* 1 */ 245 case 0x41: return KEY_NUMERIC_2; /* 2 */ 246 case 0x81: return KEY_NUMERIC_3; /* 3 */ 247 case 0x12: return KEY_NUMERIC_4; /* 4 */ 248 case 0x22: return KEY_NUMERIC_5; /* 5 */ 249 case 0x42: return KEY_NUMERIC_6; /* 6 */ 250 case 0x82: return KEY_NUMERIC_7; /* 7 */ 251 case 0x14: return KEY_NUMERIC_8; /* 8 */ 252 case 0x24: return KEY_NUMERIC_9; /* 9 */ 253 case 0x44: return KEY_NUMERIC_POUND; /* # */ 254 case 0x84: return KEY_NUMERIC_STAR; /* * */ 255 case 0x18: return KEY_ENTER; /* pickup */ 256 case 0x28: return KEY_ESC; /* hangup */ 257 case 0x48: return KEY_LEFT; /* IN */ 258 case 0x88: return KEY_RIGHT; /* OUT */ 259 default: return special_keymap(scancode); 260 } 261 } 262 263 static unsigned short (*keymap)(int) = keymap_kip1000; 264 265 /* 266 * Completes a request by converting the data into events for the 267 * input subsystem. 268 */ 269 static void report_key(struct cm109_dev *dev, int key) 270 { 271 struct input_dev *idev = dev->idev; 272 273 if (dev->key_code >= 0) { 274 /* old key up */ 275 input_report_key(idev, dev->key_code, 0); 276 } 277 278 dev->key_code = key; 279 if (key >= 0) { 280 /* new valid key */ 281 input_report_key(idev, key, 1); 282 } 283 284 input_sync(idev); 285 } 286 287 /****************************************************************************** 288 * CM109 usb communication interface 289 *****************************************************************************/ 290 291 static void cm109_submit_buzz_toggle(struct cm109_dev *dev) 292 { 293 int error; 294 295 if (dev->buzzer_state) 296 dev->ctl_data->byte[HID_OR0] |= BUZZER_ON; 297 else 298 dev->ctl_data->byte[HID_OR0] &= ~BUZZER_ON; 299 300 error = usb_submit_urb(dev->urb_ctl, GFP_ATOMIC); 301 if (error) 302 err("%s: usb_submit_urb (urb_ctl) failed %d", __func__, error); 303 } 304 305 /* 306 * IRQ handler 307 */ 308 static void cm109_urb_irq_callback(struct urb *urb) 309 { 310 struct cm109_dev *dev = urb->context; 311 const int status = urb->status; 312 int error; 313 314 #if CM109_DEBUG 315 info("### URB IRQ: [0x%02x 0x%02x 0x%02x 0x%02x] keybit=0x%02x", 316 dev->irq_data->byte[0], 317 dev->irq_data->byte[1], 318 dev->irq_data->byte[2], 319 dev->irq_data->byte[3], 320 dev->keybit); 321 #endif 322 323 if (status) { 324 if (status == -ESHUTDOWN) 325 return; 326 err("%s: urb status %d", __func__, status); 327 } 328 329 /* Special keys */ 330 if (dev->irq_data->byte[HID_IR0] & 0x0f) { 331 const int code = (dev->irq_data->byte[HID_IR0] & 0x0f); 332 report_key(dev, dev->keymap[0xff + code]); 333 } 334 335 /* Scan key column */ 336 if (dev->keybit == 0xf) { 337 338 /* Any changes ? */ 339 if ((dev->gpi & 0xf0) == (dev->irq_data->byte[HID_IR1] & 0xf0)) 340 goto out; 341 342 dev->gpi = dev->irq_data->byte[HID_IR1] & 0xf0; 343 dev->keybit = 0x1; 344 } else { 345 report_key(dev, dev->keymap[dev->irq_data->byte[HID_IR1]]); 346 347 dev->keybit <<= 1; 348 if (dev->keybit > 0x8) 349 dev->keybit = 0xf; 350 } 351 352 out: 353 354 spin_lock(&dev->ctl_submit_lock); 355 356 dev->irq_urb_pending = 0; 357 358 if (likely(!dev->shutdown)) { 359 360 if (dev->buzzer_state) 361 dev->ctl_data->byte[HID_OR0] |= BUZZER_ON; 362 else 363 dev->ctl_data->byte[HID_OR0] &= ~BUZZER_ON; 364 365 dev->ctl_data->byte[HID_OR1] = dev->keybit; 366 dev->ctl_data->byte[HID_OR2] = dev->keybit; 367 368 dev->buzzer_pending = 0; 369 dev->ctl_urb_pending = 1; 370 371 error = usb_submit_urb(dev->urb_ctl, GFP_ATOMIC); 372 if (error) 373 err("%s: usb_submit_urb (urb_ctl) failed %d", 374 __func__, error); 375 } 376 377 spin_unlock(&dev->ctl_submit_lock); 378 } 379 380 static void cm109_urb_ctl_callback(struct urb *urb) 381 { 382 struct cm109_dev *dev = urb->context; 383 const int status = urb->status; 384 int error; 385 386 #if CM109_DEBUG 387 info("### URB CTL: [0x%02x 0x%02x 0x%02x 0x%02x]", 388 dev->ctl_data->byte[0], 389 dev->ctl_data->byte[1], 390 dev->ctl_data->byte[2], 391 dev->ctl_data->byte[3]); 392 #endif 393 394 if (status) 395 err("%s: urb status %d", __func__, status); 396 397 spin_lock(&dev->ctl_submit_lock); 398 399 dev->ctl_urb_pending = 0; 400 401 if (likely(!dev->shutdown)) { 402 403 if (dev->buzzer_pending) { 404 dev->buzzer_pending = 0; 405 dev->ctl_urb_pending = 1; 406 cm109_submit_buzz_toggle(dev); 407 } else if (likely(!dev->irq_urb_pending)) { 408 /* ask for key data */ 409 dev->irq_urb_pending = 1; 410 error = usb_submit_urb(dev->urb_irq, GFP_ATOMIC); 411 if (error) 412 err("%s: usb_submit_urb (urb_irq) failed %d", 413 __func__, error); 414 } 415 } 416 417 spin_unlock(&dev->ctl_submit_lock); 418 } 419 420 static void cm109_toggle_buzzer_async(struct cm109_dev *dev) 421 { 422 unsigned long flags; 423 424 spin_lock_irqsave(&dev->ctl_submit_lock, flags); 425 426 if (dev->ctl_urb_pending) { 427 /* URB completion will resubmit */ 428 dev->buzzer_pending = 1; 429 } else { 430 dev->ctl_urb_pending = 1; 431 cm109_submit_buzz_toggle(dev); 432 } 433 434 spin_unlock_irqrestore(&dev->ctl_submit_lock, flags); 435 } 436 437 static void cm109_toggle_buzzer_sync(struct cm109_dev *dev, int on) 438 { 439 int error; 440 441 if (on) 442 dev->ctl_data->byte[HID_OR0] |= BUZZER_ON; 443 else 444 dev->ctl_data->byte[HID_OR0] &= ~BUZZER_ON; 445 446 error = usb_control_msg(dev->udev, 447 usb_sndctrlpipe(dev->udev, 0), 448 dev->ctl_req->bRequest, 449 dev->ctl_req->bRequestType, 450 le16_to_cpu(dev->ctl_req->wValue), 451 le16_to_cpu(dev->ctl_req->wIndex), 452 dev->ctl_data, 453 USB_PKT_LEN, USB_CTRL_SET_TIMEOUT); 454 if (error && error != EINTR) 455 err("%s: usb_control_msg() failed %d", __func__, error); 456 } 457 458 static void cm109_stop_traffic(struct cm109_dev *dev) 459 { 460 dev->shutdown = 1; 461 /* 462 * Make sure other CPUs see this 463 */ 464 smp_wmb(); 465 466 usb_kill_urb(dev->urb_ctl); 467 usb_kill_urb(dev->urb_irq); 468 469 cm109_toggle_buzzer_sync(dev, 0); 470 471 dev->shutdown = 0; 472 smp_wmb(); 473 } 474 475 static void cm109_restore_state(struct cm109_dev *dev) 476 { 477 if (dev->open) { 478 /* 479 * Restore buzzer state. 480 * This will also kick regular URB submission 481 */ 482 cm109_toggle_buzzer_async(dev); 483 } 484 } 485 486 /****************************************************************************** 487 * input event interface 488 *****************************************************************************/ 489 490 static int cm109_input_open(struct input_dev *idev) 491 { 492 struct cm109_dev *dev = input_get_drvdata(idev); 493 int error; 494 495 error = usb_autopm_get_interface(dev->intf); 496 if (error < 0) { 497 err("%s - cannot autoresume, result %d", 498 __func__, error); 499 return error; 500 } 501 502 mutex_lock(&dev->pm_mutex); 503 504 dev->buzzer_state = 0; 505 dev->key_code = -1; /* no keys pressed */ 506 dev->keybit = 0xf; 507 508 /* issue INIT */ 509 dev->ctl_data->byte[HID_OR0] = HID_OR_GPO_BUZ_SPDIF; 510 dev->ctl_data->byte[HID_OR1] = dev->keybit; 511 dev->ctl_data->byte[HID_OR2] = dev->keybit; 512 dev->ctl_data->byte[HID_OR3] = 0x00; 513 514 error = usb_submit_urb(dev->urb_ctl, GFP_KERNEL); 515 if (error) 516 err("%s: usb_submit_urb (urb_ctl) failed %d", __func__, error); 517 else 518 dev->open = 1; 519 520 mutex_unlock(&dev->pm_mutex); 521 522 if (error) 523 usb_autopm_put_interface(dev->intf); 524 525 return error; 526 } 527 528 static void cm109_input_close(struct input_dev *idev) 529 { 530 struct cm109_dev *dev = input_get_drvdata(idev); 531 532 mutex_lock(&dev->pm_mutex); 533 534 /* 535 * Once we are here event delivery is stopped so we 536 * don't need to worry about someone starting buzzer 537 * again 538 */ 539 cm109_stop_traffic(dev); 540 dev->open = 0; 541 542 mutex_unlock(&dev->pm_mutex); 543 544 usb_autopm_put_interface(dev->intf); 545 } 546 547 static int cm109_input_ev(struct input_dev *idev, unsigned int type, 548 unsigned int code, int value) 549 { 550 struct cm109_dev *dev = input_get_drvdata(idev); 551 552 #if CM109_DEBUG 553 info("input_ev: type=%u code=%u value=%d", type, code, value); 554 #endif 555 556 if (type != EV_SND) 557 return -EINVAL; 558 559 switch (code) { 560 case SND_TONE: 561 case SND_BELL: 562 dev->buzzer_state = !!value; 563 if (!dev->resetting) 564 cm109_toggle_buzzer_async(dev); 565 return 0; 566 567 default: 568 return -EINVAL; 569 } 570 } 571 572 573 /****************************************************************************** 574 * Linux interface and usb initialisation 575 *****************************************************************************/ 576 577 struct driver_info { 578 char *name; 579 }; 580 581 static const struct driver_info info_cm109 = { 582 .name = "CM109 USB driver", 583 }; 584 585 enum { 586 VENDOR_ID = 0x0d8c, /* C-Media Electronics */ 587 PRODUCT_ID_CM109 = 0x000e, /* CM109 defines range 0x0008 - 0x000f */ 588 }; 589 590 /* table of devices that work with this driver */ 591 static const struct usb_device_id cm109_usb_table[] = { 592 { 593 .match_flags = USB_DEVICE_ID_MATCH_DEVICE | 594 USB_DEVICE_ID_MATCH_INT_INFO, 595 .idVendor = VENDOR_ID, 596 .idProduct = PRODUCT_ID_CM109, 597 .bInterfaceClass = USB_CLASS_HID, 598 .bInterfaceSubClass = 0, 599 .bInterfaceProtocol = 0, 600 .driver_info = (kernel_ulong_t) &info_cm109 601 }, 602 /* you can add more devices here with product ID 0x0008 - 0x000f */ 603 { } 604 }; 605 606 static void cm109_usb_cleanup(struct cm109_dev *dev) 607 { 608 if (dev->ctl_req) 609 usb_buffer_free(dev->udev, sizeof(*(dev->ctl_req)), 610 dev->ctl_req, dev->ctl_req_dma); 611 if (dev->ctl_data) 612 usb_buffer_free(dev->udev, USB_PKT_LEN, 613 dev->ctl_data, dev->ctl_dma); 614 if (dev->irq_data) 615 usb_buffer_free(dev->udev, USB_PKT_LEN, 616 dev->irq_data, dev->irq_dma); 617 618 usb_free_urb(dev->urb_irq); /* parameter validation in core/urb */ 619 usb_free_urb(dev->urb_ctl); /* parameter validation in core/urb */ 620 kfree(dev); 621 } 622 623 static void cm109_usb_disconnect(struct usb_interface *interface) 624 { 625 struct cm109_dev *dev = usb_get_intfdata(interface); 626 627 usb_set_intfdata(interface, NULL); 628 input_unregister_device(dev->idev); 629 cm109_usb_cleanup(dev); 630 } 631 632 static int cm109_usb_probe(struct usb_interface *intf, 633 const struct usb_device_id *id) 634 { 635 struct usb_device *udev = interface_to_usbdev(intf); 636 struct driver_info *nfo = (struct driver_info *)id->driver_info; 637 struct usb_host_interface *interface; 638 struct usb_endpoint_descriptor *endpoint; 639 struct cm109_dev *dev; 640 struct input_dev *input_dev = NULL; 641 int ret, pipe, i; 642 int error = -ENOMEM; 643 644 interface = intf->cur_altsetting; 645 endpoint = &interface->endpoint[0].desc; 646 647 if (!usb_endpoint_is_int_in(endpoint)) 648 return -ENODEV; 649 650 dev = kzalloc(sizeof(*dev), GFP_KERNEL); 651 if (!dev) 652 return -ENOMEM; 653 654 spin_lock_init(&dev->ctl_submit_lock); 655 mutex_init(&dev->pm_mutex); 656 657 dev->udev = udev; 658 dev->intf = intf; 659 660 dev->idev = input_dev = input_allocate_device(); 661 if (!input_dev) 662 goto err_out; 663 664 /* allocate usb buffers */ 665 dev->irq_data = usb_buffer_alloc(udev, USB_PKT_LEN, 666 GFP_KERNEL, &dev->irq_dma); 667 if (!dev->irq_data) 668 goto err_out; 669 670 dev->ctl_data = usb_buffer_alloc(udev, USB_PKT_LEN, 671 GFP_KERNEL, &dev->ctl_dma); 672 if (!dev->ctl_data) 673 goto err_out; 674 675 dev->ctl_req = usb_buffer_alloc(udev, sizeof(*(dev->ctl_req)), 676 GFP_KERNEL, &dev->ctl_req_dma); 677 if (!dev->ctl_req) 678 goto err_out; 679 680 /* allocate urb structures */ 681 dev->urb_irq = usb_alloc_urb(0, GFP_KERNEL); 682 if (!dev->urb_irq) 683 goto err_out; 684 685 dev->urb_ctl = usb_alloc_urb(0, GFP_KERNEL); 686 if (!dev->urb_ctl) 687 goto err_out; 688 689 /* get a handle to the interrupt data pipe */ 690 pipe = usb_rcvintpipe(udev, endpoint->bEndpointAddress); 691 ret = usb_maxpacket(udev, pipe, usb_pipeout(pipe)); 692 if (ret != USB_PKT_LEN) 693 err("invalid payload size %d, expected %d", ret, USB_PKT_LEN); 694 695 /* initialise irq urb */ 696 usb_fill_int_urb(dev->urb_irq, udev, pipe, dev->irq_data, 697 USB_PKT_LEN, 698 cm109_urb_irq_callback, dev, endpoint->bInterval); 699 dev->urb_irq->transfer_dma = dev->irq_dma; 700 dev->urb_irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; 701 dev->urb_irq->dev = udev; 702 703 /* initialise ctl urb */ 704 dev->ctl_req->bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE | 705 USB_DIR_OUT; 706 dev->ctl_req->bRequest = USB_REQ_SET_CONFIGURATION; 707 dev->ctl_req->wValue = cpu_to_le16(0x200); 708 dev->ctl_req->wIndex = cpu_to_le16(interface->desc.bInterfaceNumber); 709 dev->ctl_req->wLength = cpu_to_le16(USB_PKT_LEN); 710 711 usb_fill_control_urb(dev->urb_ctl, udev, usb_sndctrlpipe(udev, 0), 712 (void *)dev->ctl_req, dev->ctl_data, USB_PKT_LEN, 713 cm109_urb_ctl_callback, dev); 714 dev->urb_ctl->setup_dma = dev->ctl_req_dma; 715 dev->urb_ctl->transfer_dma = dev->ctl_dma; 716 dev->urb_ctl->transfer_flags |= URB_NO_SETUP_DMA_MAP | 717 URB_NO_TRANSFER_DMA_MAP; 718 dev->urb_ctl->dev = udev; 719 720 /* find out the physical bus location */ 721 usb_make_path(udev, dev->phys, sizeof(dev->phys)); 722 strlcat(dev->phys, "/input0", sizeof(dev->phys)); 723 724 /* register settings for the input device */ 725 input_dev->name = nfo->name; 726 input_dev->phys = dev->phys; 727 usb_to_input_id(udev, &input_dev->id); 728 input_dev->dev.parent = &intf->dev; 729 730 input_set_drvdata(input_dev, dev); 731 input_dev->open = cm109_input_open; 732 input_dev->close = cm109_input_close; 733 input_dev->event = cm109_input_ev; 734 735 input_dev->keycode = dev->keymap; 736 input_dev->keycodesize = sizeof(unsigned char); 737 input_dev->keycodemax = ARRAY_SIZE(dev->keymap); 738 739 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_SND); 740 input_dev->sndbit[0] = BIT_MASK(SND_BELL) | BIT_MASK(SND_TONE); 741 742 /* register available key events */ 743 for (i = 0; i < KEYMAP_SIZE; i++) { 744 unsigned short k = keymap(i); 745 dev->keymap[i] = k; 746 __set_bit(k, input_dev->keybit); 747 } 748 __clear_bit(KEY_RESERVED, input_dev->keybit); 749 750 error = input_register_device(dev->idev); 751 if (error) 752 goto err_out; 753 754 usb_set_intfdata(intf, dev); 755 756 return 0; 757 758 err_out: 759 input_free_device(input_dev); 760 cm109_usb_cleanup(dev); 761 return error; 762 } 763 764 static int cm109_usb_suspend(struct usb_interface *intf, pm_message_t message) 765 { 766 struct cm109_dev *dev = usb_get_intfdata(intf); 767 768 info("cm109: usb_suspend (event=%d)", message.event); 769 770 mutex_lock(&dev->pm_mutex); 771 cm109_stop_traffic(dev); 772 mutex_unlock(&dev->pm_mutex); 773 774 return 0; 775 } 776 777 static int cm109_usb_resume(struct usb_interface *intf) 778 { 779 struct cm109_dev *dev = usb_get_intfdata(intf); 780 781 info("cm109: usb_resume"); 782 783 mutex_lock(&dev->pm_mutex); 784 cm109_restore_state(dev); 785 mutex_unlock(&dev->pm_mutex); 786 787 return 0; 788 } 789 790 static int cm109_usb_pre_reset(struct usb_interface *intf) 791 { 792 struct cm109_dev *dev = usb_get_intfdata(intf); 793 794 mutex_lock(&dev->pm_mutex); 795 796 /* 797 * Make sure input events don't try to toggle buzzer 798 * while we are resetting 799 */ 800 dev->resetting = 1; 801 smp_wmb(); 802 803 cm109_stop_traffic(dev); 804 805 return 0; 806 } 807 808 static int cm109_usb_post_reset(struct usb_interface *intf) 809 { 810 struct cm109_dev *dev = usb_get_intfdata(intf); 811 812 dev->resetting = 0; 813 smp_wmb(); 814 815 cm109_restore_state(dev); 816 817 mutex_unlock(&dev->pm_mutex); 818 819 return 0; 820 } 821 822 static struct usb_driver cm109_driver = { 823 .name = "cm109", 824 .probe = cm109_usb_probe, 825 .disconnect = cm109_usb_disconnect, 826 .suspend = cm109_usb_suspend, 827 .resume = cm109_usb_resume, 828 .reset_resume = cm109_usb_resume, 829 .pre_reset = cm109_usb_pre_reset, 830 .post_reset = cm109_usb_post_reset, 831 .id_table = cm109_usb_table, 832 .supports_autosuspend = 1, 833 }; 834 835 static int __init cm109_select_keymap(void) 836 { 837 /* Load the phone keymap */ 838 if (!strcasecmp(phone, "kip1000")) { 839 keymap = keymap_kip1000; 840 info("Keymap for Komunikate KIP1000 phone loaded"); 841 } else if (!strcasecmp(phone, "gtalk")) { 842 keymap = keymap_gtalk; 843 info("Keymap for Genius G-talk phone loaded"); 844 } else if (!strcasecmp(phone, "usbph01")) { 845 keymap = keymap_usbph01; 846 info("Keymap for Allied-Telesis Corega USBPH01 phone loaded"); 847 } else { 848 err("Unsupported phone: %s", phone); 849 return -EINVAL; 850 } 851 852 return 0; 853 } 854 855 static int __init cm109_init(void) 856 { 857 int err; 858 859 err = cm109_select_keymap(); 860 if (err) 861 return err; 862 863 err = usb_register(&cm109_driver); 864 if (err) 865 return err; 866 867 info(DRIVER_DESC ": " DRIVER_VERSION " (C) " DRIVER_AUTHOR); 868 869 return 0; 870 } 871 872 static void __exit cm109_exit(void) 873 { 874 usb_deregister(&cm109_driver); 875 } 876 877 module_init(cm109_init); 878 module_exit(cm109_exit); 879 880 MODULE_DEVICE_TABLE(usb, cm109_usb_table); 881 882 MODULE_AUTHOR(DRIVER_AUTHOR); 883 MODULE_DESCRIPTION(DRIVER_DESC); 884 MODULE_LICENSE("GPL"); 885