rcu: Maintain special bits at bottom of ->dynticks counter
[linux/fpc-iii.git] / drivers / input / misc / cm109.c
blob9cc6d057c302a1753f33e9072ab5f5384f1eec40
1 /*
2 * Driver for the VoIP USB phones with CM109 chipsets.
4 * Copyright (C) 2007 - 2008 Alfred E. Heggestad <aeh@db.org>
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 */
12 * Tested devices:
13 * - Komunikate KIP1000
14 * - Genius G-talk
15 * - Allied-Telesis Corega USBPH01
16 * - ...
18 * This driver is based on the yealink.c driver
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
27 * Todo:
28 * - Read/write EEPROM
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>
39 #define DRIVER_VERSION "20080805"
40 #define DRIVER_AUTHOR "Alfred E. Heggestad"
41 #define DRIVER_DESC "CM109 phone driver"
43 static char *phone = "kip1000";
44 module_param(phone, charp, S_IRUSR);
45 MODULE_PARM_DESC(phone, "Phone name {kip1000, gtalk, usbph01, atcom}");
47 enum {
48 /* HID Registers */
49 HID_IR0 = 0x00, /* Record/Playback-mute button, Volume up/down */
50 HID_IR1 = 0x01, /* GPI, generic registers or EEPROM_DATA0 */
51 HID_IR2 = 0x02, /* Generic registers or EEPROM_DATA1 */
52 HID_IR3 = 0x03, /* Generic registers or EEPROM_CTRL */
53 HID_OR0 = 0x00, /* Mapping control, buzzer, SPDIF (offset 0x04) */
54 HID_OR1 = 0x01, /* GPO - General Purpose Output */
55 HID_OR2 = 0x02, /* Set GPIO to input/output mode */
56 HID_OR3 = 0x03, /* SPDIF status channel or EEPROM_CTRL */
58 /* HID_IR0 */
59 RECORD_MUTE = 1 << 3,
60 PLAYBACK_MUTE = 1 << 2,
61 VOLUME_DOWN = 1 << 1,
62 VOLUME_UP = 1 << 0,
64 /* HID_OR0 */
65 /* bits 7-6
66 0: HID_OR1-2 are used for GPO; HID_OR0, 3 are used for buzzer
67 and SPDIF
68 1: HID_OR0-3 are used as generic HID registers
69 2: Values written to HID_OR0-3 are also mapped to MCU_CTRL,
70 EEPROM_DATA0-1, EEPROM_CTRL (see Note)
71 3: Reserved
73 HID_OR_GPO_BUZ_SPDIF = 0 << 6,
74 HID_OR_GENERIC_HID_REG = 1 << 6,
75 HID_OR_MAP_MCU_EEPROM = 2 << 6,
77 BUZZER_ON = 1 << 5,
79 /* up to 256 normal keys, up to 15 special key combinations */
80 KEYMAP_SIZE = 256 + 15,
83 /* CM109 protocol packet */
84 struct cm109_ctl_packet {
85 u8 byte[4];
86 } __attribute__ ((packed));
88 enum { USB_PKT_LEN = sizeof(struct cm109_ctl_packet) };
90 /* CM109 device structure */
91 struct cm109_dev {
92 struct input_dev *idev; /* input device */
93 struct usb_device *udev; /* usb device */
94 struct usb_interface *intf;
96 /* irq input channel */
97 struct cm109_ctl_packet *irq_data;
98 dma_addr_t irq_dma;
99 struct urb *urb_irq;
101 /* control output channel */
102 struct cm109_ctl_packet *ctl_data;
103 dma_addr_t ctl_dma;
104 struct usb_ctrlrequest *ctl_req;
105 struct urb *urb_ctl;
107 * The 3 bitfields below are protected by ctl_submit_lock.
108 * They have to be separate since they are accessed from IRQ
109 * context.
111 unsigned irq_urb_pending:1; /* irq_urb is in flight */
112 unsigned ctl_urb_pending:1; /* ctl_urb is in flight */
113 unsigned buzzer_pending:1; /* need to issue buzz command */
114 spinlock_t ctl_submit_lock;
116 unsigned char buzzer_state; /* on/off */
118 /* flags */
119 unsigned open:1;
120 unsigned resetting:1;
121 unsigned shutdown:1;
123 /* This mutex protects writes to the above flags */
124 struct mutex pm_mutex;
126 unsigned short keymap[KEYMAP_SIZE];
128 char phys[64]; /* physical device path */
129 int key_code; /* last reported key */
130 int keybit; /* 0=new scan 1,2,4,8=scan columns */
131 u8 gpi; /* Cached value of GPI (high nibble) */
134 /******************************************************************************
135 * CM109 key interface
136 *****************************************************************************/
138 static unsigned short special_keymap(int code)
140 if (code > 0xff) {
141 switch (code - 0xff) {
142 case RECORD_MUTE: return KEY_MICMUTE;
143 case PLAYBACK_MUTE: return KEY_MUTE;
144 case VOLUME_DOWN: return KEY_VOLUMEDOWN;
145 case VOLUME_UP: return KEY_VOLUMEUP;
148 return KEY_RESERVED;
151 /* Map device buttons to internal key events.
153 * The "up" and "down" keys, are symbolised by arrows on the button.
154 * The "pickup" and "hangup" keys are symbolised by a green and red phone
155 * on the button.
157 Komunikate KIP1000 Keyboard Matrix
159 -> -- 1 -- 2 -- 3 --> GPI pin 4 (0x10)
160 | | | |
161 <- -- 4 -- 5 -- 6 --> GPI pin 5 (0x20)
162 | | | |
163 END - 7 -- 8 -- 9 --> GPI pin 6 (0x40)
164 | | | |
165 OK -- * -- 0 -- # --> GPI pin 7 (0x80)
166 | | | |
168 /|\ /|\ /|\ /|\
169 | | | |
171 pin: 3 2 1 0
172 0x8 0x4 0x2 0x1
175 static unsigned short keymap_kip1000(int scancode)
177 switch (scancode) { /* phone key: */
178 case 0x82: return KEY_NUMERIC_0; /* 0 */
179 case 0x14: return KEY_NUMERIC_1; /* 1 */
180 case 0x12: return KEY_NUMERIC_2; /* 2 */
181 case 0x11: return KEY_NUMERIC_3; /* 3 */
182 case 0x24: return KEY_NUMERIC_4; /* 4 */
183 case 0x22: return KEY_NUMERIC_5; /* 5 */
184 case 0x21: return KEY_NUMERIC_6; /* 6 */
185 case 0x44: return KEY_NUMERIC_7; /* 7 */
186 case 0x42: return KEY_NUMERIC_8; /* 8 */
187 case 0x41: return KEY_NUMERIC_9; /* 9 */
188 case 0x81: return KEY_NUMERIC_POUND; /* # */
189 case 0x84: return KEY_NUMERIC_STAR; /* * */
190 case 0x88: return KEY_ENTER; /* pickup */
191 case 0x48: return KEY_ESC; /* hangup */
192 case 0x28: return KEY_LEFT; /* IN */
193 case 0x18: return KEY_RIGHT; /* OUT */
194 default: return special_keymap(scancode);
199 Contributed by Shaun Jackman <sjackman@gmail.com>
201 Genius G-Talk keyboard matrix
202 0 1 2 3
203 4: 0 4 8 Talk
204 5: 1 5 9 End
205 6: 2 6 # Up
206 7: 3 7 * Down
208 static unsigned short keymap_gtalk(int scancode)
210 switch (scancode) {
211 case 0x11: return KEY_NUMERIC_0;
212 case 0x21: return KEY_NUMERIC_1;
213 case 0x41: return KEY_NUMERIC_2;
214 case 0x81: return KEY_NUMERIC_3;
215 case 0x12: return KEY_NUMERIC_4;
216 case 0x22: return KEY_NUMERIC_5;
217 case 0x42: return KEY_NUMERIC_6;
218 case 0x82: return KEY_NUMERIC_7;
219 case 0x14: return KEY_NUMERIC_8;
220 case 0x24: return KEY_NUMERIC_9;
221 case 0x44: return KEY_NUMERIC_POUND; /* # */
222 case 0x84: return KEY_NUMERIC_STAR; /* * */
223 case 0x18: return KEY_ENTER; /* Talk (green handset) */
224 case 0x28: return KEY_ESC; /* End (red handset) */
225 case 0x48: return KEY_UP; /* Menu up (rocker switch) */
226 case 0x88: return KEY_DOWN; /* Menu down (rocker switch) */
227 default: return special_keymap(scancode);
232 * Keymap for Allied-Telesis Corega USBPH01
233 * http://www.alliedtelesis-corega.com/2/1344/1437/1360/chprd.html
235 * Contributed by july@nat.bg
237 static unsigned short keymap_usbph01(int scancode)
239 switch (scancode) {
240 case 0x11: return KEY_NUMERIC_0; /* 0 */
241 case 0x21: return KEY_NUMERIC_1; /* 1 */
242 case 0x41: return KEY_NUMERIC_2; /* 2 */
243 case 0x81: return KEY_NUMERIC_3; /* 3 */
244 case 0x12: return KEY_NUMERIC_4; /* 4 */
245 case 0x22: return KEY_NUMERIC_5; /* 5 */
246 case 0x42: return KEY_NUMERIC_6; /* 6 */
247 case 0x82: return KEY_NUMERIC_7; /* 7 */
248 case 0x14: return KEY_NUMERIC_8; /* 8 */
249 case 0x24: return KEY_NUMERIC_9; /* 9 */
250 case 0x44: return KEY_NUMERIC_POUND; /* # */
251 case 0x84: return KEY_NUMERIC_STAR; /* * */
252 case 0x18: return KEY_ENTER; /* pickup */
253 case 0x28: return KEY_ESC; /* hangup */
254 case 0x48: return KEY_LEFT; /* IN */
255 case 0x88: return KEY_RIGHT; /* OUT */
256 default: return special_keymap(scancode);
261 * Keymap for ATCom AU-100
262 * http://www.atcom.cn/products.html
263 * http://www.packetizer.com/products/au100/
264 * http://www.voip-info.org/wiki/view/AU-100
266 * Contributed by daniel@gimpelevich.san-francisco.ca.us
268 static unsigned short keymap_atcom(int scancode)
270 switch (scancode) { /* phone key: */
271 case 0x82: return KEY_NUMERIC_0; /* 0 */
272 case 0x11: return KEY_NUMERIC_1; /* 1 */
273 case 0x12: return KEY_NUMERIC_2; /* 2 */
274 case 0x14: return KEY_NUMERIC_3; /* 3 */
275 case 0x21: return KEY_NUMERIC_4; /* 4 */
276 case 0x22: return KEY_NUMERIC_5; /* 5 */
277 case 0x24: return KEY_NUMERIC_6; /* 6 */
278 case 0x41: return KEY_NUMERIC_7; /* 7 */
279 case 0x42: return KEY_NUMERIC_8; /* 8 */
280 case 0x44: return KEY_NUMERIC_9; /* 9 */
281 case 0x84: return KEY_NUMERIC_POUND; /* # */
282 case 0x81: return KEY_NUMERIC_STAR; /* * */
283 case 0x18: return KEY_ENTER; /* pickup */
284 case 0x28: return KEY_ESC; /* hangup */
285 case 0x48: return KEY_LEFT; /* left arrow */
286 case 0x88: return KEY_RIGHT; /* right arrow */
287 default: return special_keymap(scancode);
291 static unsigned short (*keymap)(int) = keymap_kip1000;
294 * Completes a request by converting the data into events for the
295 * input subsystem.
297 static void report_key(struct cm109_dev *dev, int key)
299 struct input_dev *idev = dev->idev;
301 if (dev->key_code >= 0) {
302 /* old key up */
303 input_report_key(idev, dev->key_code, 0);
306 dev->key_code = key;
307 if (key >= 0) {
308 /* new valid key */
309 input_report_key(idev, key, 1);
312 input_sync(idev);
316 * Converts data of special key presses (volume, mute) into events
317 * for the input subsystem, sends press-n-release for mute keys.
319 static void cm109_report_special(struct cm109_dev *dev)
321 static const u8 autorelease = RECORD_MUTE | PLAYBACK_MUTE;
322 struct input_dev *idev = dev->idev;
323 u8 data = dev->irq_data->byte[HID_IR0];
324 unsigned short keycode;
325 int i;
327 for (i = 0; i < 4; i++) {
328 keycode = dev->keymap[0xff + BIT(i)];
329 if (keycode == KEY_RESERVED)
330 continue;
332 input_report_key(idev, keycode, data & BIT(i));
333 if (data & autorelease & BIT(i)) {
334 input_sync(idev);
335 input_report_key(idev, keycode, 0);
338 input_sync(idev);
341 /******************************************************************************
342 * CM109 usb communication interface
343 *****************************************************************************/
345 static void cm109_submit_buzz_toggle(struct cm109_dev *dev)
347 int error;
349 if (dev->buzzer_state)
350 dev->ctl_data->byte[HID_OR0] |= BUZZER_ON;
351 else
352 dev->ctl_data->byte[HID_OR0] &= ~BUZZER_ON;
354 error = usb_submit_urb(dev->urb_ctl, GFP_ATOMIC);
355 if (error)
356 dev_err(&dev->intf->dev,
357 "%s: usb_submit_urb (urb_ctl) failed %d\n",
358 __func__, error);
362 * IRQ handler
364 static void cm109_urb_irq_callback(struct urb *urb)
366 struct cm109_dev *dev = urb->context;
367 const int status = urb->status;
368 int error;
369 unsigned long flags;
371 dev_dbg(&dev->intf->dev, "### URB IRQ: [0x%02x 0x%02x 0x%02x 0x%02x] keybit=0x%02x\n",
372 dev->irq_data->byte[0],
373 dev->irq_data->byte[1],
374 dev->irq_data->byte[2],
375 dev->irq_data->byte[3],
376 dev->keybit);
378 if (status) {
379 if (status == -ESHUTDOWN)
380 return;
381 dev_err_ratelimited(&dev->intf->dev, "%s: urb status %d\n",
382 __func__, status);
383 goto out;
386 /* Special keys */
387 cm109_report_special(dev);
389 /* Scan key column */
390 if (dev->keybit == 0xf) {
392 /* Any changes ? */
393 if ((dev->gpi & 0xf0) == (dev->irq_data->byte[HID_IR1] & 0xf0))
394 goto out;
396 dev->gpi = dev->irq_data->byte[HID_IR1] & 0xf0;
397 dev->keybit = 0x1;
398 } else {
399 report_key(dev, dev->keymap[dev->irq_data->byte[HID_IR1]]);
401 dev->keybit <<= 1;
402 if (dev->keybit > 0x8)
403 dev->keybit = 0xf;
406 out:
408 spin_lock_irqsave(&dev->ctl_submit_lock, flags);
410 dev->irq_urb_pending = 0;
412 if (likely(!dev->shutdown)) {
414 if (dev->buzzer_state)
415 dev->ctl_data->byte[HID_OR0] |= BUZZER_ON;
416 else
417 dev->ctl_data->byte[HID_OR0] &= ~BUZZER_ON;
419 dev->ctl_data->byte[HID_OR1] = dev->keybit;
420 dev->ctl_data->byte[HID_OR2] = dev->keybit;
422 dev->buzzer_pending = 0;
423 dev->ctl_urb_pending = 1;
425 error = usb_submit_urb(dev->urb_ctl, GFP_ATOMIC);
426 if (error)
427 dev_err(&dev->intf->dev,
428 "%s: usb_submit_urb (urb_ctl) failed %d\n",
429 __func__, error);
432 spin_unlock_irqrestore(&dev->ctl_submit_lock, flags);
435 static void cm109_urb_ctl_callback(struct urb *urb)
437 struct cm109_dev *dev = urb->context;
438 const int status = urb->status;
439 int error;
440 unsigned long flags;
442 dev_dbg(&dev->intf->dev, "### URB CTL: [0x%02x 0x%02x 0x%02x 0x%02x]\n",
443 dev->ctl_data->byte[0],
444 dev->ctl_data->byte[1],
445 dev->ctl_data->byte[2],
446 dev->ctl_data->byte[3]);
448 if (status) {
449 if (status == -ESHUTDOWN)
450 return;
451 dev_err_ratelimited(&dev->intf->dev, "%s: urb status %d\n",
452 __func__, status);
455 spin_lock_irqsave(&dev->ctl_submit_lock, flags);
457 dev->ctl_urb_pending = 0;
459 if (likely(!dev->shutdown)) {
461 if (dev->buzzer_pending || status) {
462 dev->buzzer_pending = 0;
463 dev->ctl_urb_pending = 1;
464 cm109_submit_buzz_toggle(dev);
465 } else if (likely(!dev->irq_urb_pending)) {
466 /* ask for key data */
467 dev->irq_urb_pending = 1;
468 error = usb_submit_urb(dev->urb_irq, GFP_ATOMIC);
469 if (error)
470 dev_err(&dev->intf->dev,
471 "%s: usb_submit_urb (urb_irq) failed %d\n",
472 __func__, error);
476 spin_unlock_irqrestore(&dev->ctl_submit_lock, flags);
479 static void cm109_toggle_buzzer_async(struct cm109_dev *dev)
481 unsigned long flags;
483 spin_lock_irqsave(&dev->ctl_submit_lock, flags);
485 if (dev->ctl_urb_pending) {
486 /* URB completion will resubmit */
487 dev->buzzer_pending = 1;
488 } else {
489 dev->ctl_urb_pending = 1;
490 cm109_submit_buzz_toggle(dev);
493 spin_unlock_irqrestore(&dev->ctl_submit_lock, flags);
496 static void cm109_toggle_buzzer_sync(struct cm109_dev *dev, int on)
498 int error;
500 if (on)
501 dev->ctl_data->byte[HID_OR0] |= BUZZER_ON;
502 else
503 dev->ctl_data->byte[HID_OR0] &= ~BUZZER_ON;
505 error = usb_control_msg(dev->udev,
506 usb_sndctrlpipe(dev->udev, 0),
507 dev->ctl_req->bRequest,
508 dev->ctl_req->bRequestType,
509 le16_to_cpu(dev->ctl_req->wValue),
510 le16_to_cpu(dev->ctl_req->wIndex),
511 dev->ctl_data,
512 USB_PKT_LEN, USB_CTRL_SET_TIMEOUT);
513 if (error < 0 && error != -EINTR)
514 dev_err(&dev->intf->dev, "%s: usb_control_msg() failed %d\n",
515 __func__, error);
518 static void cm109_stop_traffic(struct cm109_dev *dev)
520 dev->shutdown = 1;
522 * Make sure other CPUs see this
524 smp_wmb();
526 usb_kill_urb(dev->urb_ctl);
527 usb_kill_urb(dev->urb_irq);
529 cm109_toggle_buzzer_sync(dev, 0);
531 dev->shutdown = 0;
532 smp_wmb();
535 static void cm109_restore_state(struct cm109_dev *dev)
537 if (dev->open) {
539 * Restore buzzer state.
540 * This will also kick regular URB submission
542 cm109_toggle_buzzer_async(dev);
546 /******************************************************************************
547 * input event interface
548 *****************************************************************************/
550 static int cm109_input_open(struct input_dev *idev)
552 struct cm109_dev *dev = input_get_drvdata(idev);
553 int error;
555 error = usb_autopm_get_interface(dev->intf);
556 if (error < 0) {
557 dev_err(&idev->dev, "%s - cannot autoresume, result %d\n",
558 __func__, error);
559 return error;
562 mutex_lock(&dev->pm_mutex);
564 dev->buzzer_state = 0;
565 dev->key_code = -1; /* no keys pressed */
566 dev->keybit = 0xf;
568 /* issue INIT */
569 dev->ctl_data->byte[HID_OR0] = HID_OR_GPO_BUZ_SPDIF;
570 dev->ctl_data->byte[HID_OR1] = dev->keybit;
571 dev->ctl_data->byte[HID_OR2] = dev->keybit;
572 dev->ctl_data->byte[HID_OR3] = 0x00;
574 error = usb_submit_urb(dev->urb_ctl, GFP_KERNEL);
575 if (error)
576 dev_err(&dev->intf->dev, "%s: usb_submit_urb (urb_ctl) failed %d\n",
577 __func__, error);
578 else
579 dev->open = 1;
581 mutex_unlock(&dev->pm_mutex);
583 if (error)
584 usb_autopm_put_interface(dev->intf);
586 return error;
589 static void cm109_input_close(struct input_dev *idev)
591 struct cm109_dev *dev = input_get_drvdata(idev);
593 mutex_lock(&dev->pm_mutex);
596 * Once we are here event delivery is stopped so we
597 * don't need to worry about someone starting buzzer
598 * again
600 cm109_stop_traffic(dev);
601 dev->open = 0;
603 mutex_unlock(&dev->pm_mutex);
605 usb_autopm_put_interface(dev->intf);
608 static int cm109_input_ev(struct input_dev *idev, unsigned int type,
609 unsigned int code, int value)
611 struct cm109_dev *dev = input_get_drvdata(idev);
613 dev_dbg(&dev->intf->dev,
614 "input_ev: type=%u code=%u value=%d\n", type, code, value);
616 if (type != EV_SND)
617 return -EINVAL;
619 switch (code) {
620 case SND_TONE:
621 case SND_BELL:
622 dev->buzzer_state = !!value;
623 if (!dev->resetting)
624 cm109_toggle_buzzer_async(dev);
625 return 0;
627 default:
628 return -EINVAL;
633 /******************************************************************************
634 * Linux interface and usb initialisation
635 *****************************************************************************/
637 struct driver_info {
638 char *name;
641 static const struct driver_info info_cm109 = {
642 .name = "CM109 USB driver",
645 enum {
646 VENDOR_ID = 0x0d8c, /* C-Media Electronics */
647 PRODUCT_ID_CM109 = 0x000e, /* CM109 defines range 0x0008 - 0x000f */
650 /* table of devices that work with this driver */
651 static const struct usb_device_id cm109_usb_table[] = {
653 .match_flags = USB_DEVICE_ID_MATCH_DEVICE |
654 USB_DEVICE_ID_MATCH_INT_INFO,
655 .idVendor = VENDOR_ID,
656 .idProduct = PRODUCT_ID_CM109,
657 .bInterfaceClass = USB_CLASS_HID,
658 .bInterfaceSubClass = 0,
659 .bInterfaceProtocol = 0,
660 .driver_info = (kernel_ulong_t) &info_cm109
662 /* you can add more devices here with product ID 0x0008 - 0x000f */
666 static void cm109_usb_cleanup(struct cm109_dev *dev)
668 kfree(dev->ctl_req);
669 if (dev->ctl_data)
670 usb_free_coherent(dev->udev, USB_PKT_LEN,
671 dev->ctl_data, dev->ctl_dma);
672 if (dev->irq_data)
673 usb_free_coherent(dev->udev, USB_PKT_LEN,
674 dev->irq_data, dev->irq_dma);
676 usb_free_urb(dev->urb_irq); /* parameter validation in core/urb */
677 usb_free_urb(dev->urb_ctl); /* parameter validation in core/urb */
678 kfree(dev);
681 static void cm109_usb_disconnect(struct usb_interface *interface)
683 struct cm109_dev *dev = usb_get_intfdata(interface);
685 usb_set_intfdata(interface, NULL);
686 input_unregister_device(dev->idev);
687 cm109_usb_cleanup(dev);
690 static int cm109_usb_probe(struct usb_interface *intf,
691 const struct usb_device_id *id)
693 struct usb_device *udev = interface_to_usbdev(intf);
694 struct driver_info *nfo = (struct driver_info *)id->driver_info;
695 struct usb_host_interface *interface;
696 struct usb_endpoint_descriptor *endpoint;
697 struct cm109_dev *dev;
698 struct input_dev *input_dev = NULL;
699 int ret, pipe, i;
700 int error = -ENOMEM;
702 interface = intf->cur_altsetting;
703 endpoint = &interface->endpoint[0].desc;
705 if (!usb_endpoint_is_int_in(endpoint))
706 return -ENODEV;
708 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
709 if (!dev)
710 return -ENOMEM;
712 spin_lock_init(&dev->ctl_submit_lock);
713 mutex_init(&dev->pm_mutex);
715 dev->udev = udev;
716 dev->intf = intf;
718 dev->idev = input_dev = input_allocate_device();
719 if (!input_dev)
720 goto err_out;
722 /* allocate usb buffers */
723 dev->irq_data = usb_alloc_coherent(udev, USB_PKT_LEN,
724 GFP_KERNEL, &dev->irq_dma);
725 if (!dev->irq_data)
726 goto err_out;
728 dev->ctl_data = usb_alloc_coherent(udev, USB_PKT_LEN,
729 GFP_KERNEL, &dev->ctl_dma);
730 if (!dev->ctl_data)
731 goto err_out;
733 dev->ctl_req = kmalloc(sizeof(*(dev->ctl_req)), GFP_KERNEL);
734 if (!dev->ctl_req)
735 goto err_out;
737 /* allocate urb structures */
738 dev->urb_irq = usb_alloc_urb(0, GFP_KERNEL);
739 if (!dev->urb_irq)
740 goto err_out;
742 dev->urb_ctl = usb_alloc_urb(0, GFP_KERNEL);
743 if (!dev->urb_ctl)
744 goto err_out;
746 /* get a handle to the interrupt data pipe */
747 pipe = usb_rcvintpipe(udev, endpoint->bEndpointAddress);
748 ret = usb_maxpacket(udev, pipe, usb_pipeout(pipe));
749 if (ret != USB_PKT_LEN)
750 dev_err(&intf->dev, "invalid payload size %d, expected %d\n",
751 ret, USB_PKT_LEN);
753 /* initialise irq urb */
754 usb_fill_int_urb(dev->urb_irq, udev, pipe, dev->irq_data,
755 USB_PKT_LEN,
756 cm109_urb_irq_callback, dev, endpoint->bInterval);
757 dev->urb_irq->transfer_dma = dev->irq_dma;
758 dev->urb_irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
759 dev->urb_irq->dev = udev;
761 /* initialise ctl urb */
762 dev->ctl_req->bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE |
763 USB_DIR_OUT;
764 dev->ctl_req->bRequest = USB_REQ_SET_CONFIGURATION;
765 dev->ctl_req->wValue = cpu_to_le16(0x200);
766 dev->ctl_req->wIndex = cpu_to_le16(interface->desc.bInterfaceNumber);
767 dev->ctl_req->wLength = cpu_to_le16(USB_PKT_LEN);
769 usb_fill_control_urb(dev->urb_ctl, udev, usb_sndctrlpipe(udev, 0),
770 (void *)dev->ctl_req, dev->ctl_data, USB_PKT_LEN,
771 cm109_urb_ctl_callback, dev);
772 dev->urb_ctl->transfer_dma = dev->ctl_dma;
773 dev->urb_ctl->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
774 dev->urb_ctl->dev = udev;
776 /* find out the physical bus location */
777 usb_make_path(udev, dev->phys, sizeof(dev->phys));
778 strlcat(dev->phys, "/input0", sizeof(dev->phys));
780 /* register settings for the input device */
781 input_dev->name = nfo->name;
782 input_dev->phys = dev->phys;
783 usb_to_input_id(udev, &input_dev->id);
784 input_dev->dev.parent = &intf->dev;
786 input_set_drvdata(input_dev, dev);
787 input_dev->open = cm109_input_open;
788 input_dev->close = cm109_input_close;
789 input_dev->event = cm109_input_ev;
791 input_dev->keycode = dev->keymap;
792 input_dev->keycodesize = sizeof(unsigned char);
793 input_dev->keycodemax = ARRAY_SIZE(dev->keymap);
795 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_SND);
796 input_dev->sndbit[0] = BIT_MASK(SND_BELL) | BIT_MASK(SND_TONE);
798 /* register available key events */
799 for (i = 0; i < KEYMAP_SIZE; i++) {
800 unsigned short k = keymap(i);
801 dev->keymap[i] = k;
802 __set_bit(k, input_dev->keybit);
804 __clear_bit(KEY_RESERVED, input_dev->keybit);
806 error = input_register_device(dev->idev);
807 if (error)
808 goto err_out;
810 usb_set_intfdata(intf, dev);
812 return 0;
814 err_out:
815 input_free_device(input_dev);
816 cm109_usb_cleanup(dev);
817 return error;
820 static int cm109_usb_suspend(struct usb_interface *intf, pm_message_t message)
822 struct cm109_dev *dev = usb_get_intfdata(intf);
824 dev_info(&intf->dev, "cm109: usb_suspend (event=%d)\n", message.event);
826 mutex_lock(&dev->pm_mutex);
827 cm109_stop_traffic(dev);
828 mutex_unlock(&dev->pm_mutex);
830 return 0;
833 static int cm109_usb_resume(struct usb_interface *intf)
835 struct cm109_dev *dev = usb_get_intfdata(intf);
837 dev_info(&intf->dev, "cm109: usb_resume\n");
839 mutex_lock(&dev->pm_mutex);
840 cm109_restore_state(dev);
841 mutex_unlock(&dev->pm_mutex);
843 return 0;
846 static int cm109_usb_pre_reset(struct usb_interface *intf)
848 struct cm109_dev *dev = usb_get_intfdata(intf);
850 mutex_lock(&dev->pm_mutex);
853 * Make sure input events don't try to toggle buzzer
854 * while we are resetting
856 dev->resetting = 1;
857 smp_wmb();
859 cm109_stop_traffic(dev);
861 return 0;
864 static int cm109_usb_post_reset(struct usb_interface *intf)
866 struct cm109_dev *dev = usb_get_intfdata(intf);
868 dev->resetting = 0;
869 smp_wmb();
871 cm109_restore_state(dev);
873 mutex_unlock(&dev->pm_mutex);
875 return 0;
878 static struct usb_driver cm109_driver = {
879 .name = "cm109",
880 .probe = cm109_usb_probe,
881 .disconnect = cm109_usb_disconnect,
882 .suspend = cm109_usb_suspend,
883 .resume = cm109_usb_resume,
884 .reset_resume = cm109_usb_resume,
885 .pre_reset = cm109_usb_pre_reset,
886 .post_reset = cm109_usb_post_reset,
887 .id_table = cm109_usb_table,
888 .supports_autosuspend = 1,
891 static int __init cm109_select_keymap(void)
893 /* Load the phone keymap */
894 if (!strcasecmp(phone, "kip1000")) {
895 keymap = keymap_kip1000;
896 printk(KERN_INFO KBUILD_MODNAME ": "
897 "Keymap for Komunikate KIP1000 phone loaded\n");
898 } else if (!strcasecmp(phone, "gtalk")) {
899 keymap = keymap_gtalk;
900 printk(KERN_INFO KBUILD_MODNAME ": "
901 "Keymap for Genius G-talk phone loaded\n");
902 } else if (!strcasecmp(phone, "usbph01")) {
903 keymap = keymap_usbph01;
904 printk(KERN_INFO KBUILD_MODNAME ": "
905 "Keymap for Allied-Telesis Corega USBPH01 phone loaded\n");
906 } else if (!strcasecmp(phone, "atcom")) {
907 keymap = keymap_atcom;
908 printk(KERN_INFO KBUILD_MODNAME ": "
909 "Keymap for ATCom AU-100 phone loaded\n");
910 } else {
911 printk(KERN_ERR KBUILD_MODNAME ": "
912 "Unsupported phone: %s\n", phone);
913 return -EINVAL;
916 return 0;
919 static int __init cm109_init(void)
921 int err;
923 err = cm109_select_keymap();
924 if (err)
925 return err;
927 err = usb_register(&cm109_driver);
928 if (err)
929 return err;
931 printk(KERN_INFO KBUILD_MODNAME ": "
932 DRIVER_DESC ": " DRIVER_VERSION " (C) " DRIVER_AUTHOR "\n");
934 return 0;
937 static void __exit cm109_exit(void)
939 usb_deregister(&cm109_driver);
942 module_init(cm109_init);
943 module_exit(cm109_exit);
945 MODULE_DEVICE_TABLE(usb, cm109_usb_table);
947 MODULE_AUTHOR(DRIVER_AUTHOR);
948 MODULE_DESCRIPTION(DRIVER_DESC);
949 MODULE_LICENSE("GPL");