nilfs2: unbreak compat ioctl
[zen-stable.git] / drivers / input / misc / cm109.c
blobab860511f01690a73ed2fd3dbefb49d11aaa3f73
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 16 special keys */
80 KEYMAP_SIZE = 256 + 16,
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_MUTE;
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);
315 /******************************************************************************
316 * CM109 usb communication interface
317 *****************************************************************************/
319 static void cm109_submit_buzz_toggle(struct cm109_dev *dev)
321 int error;
323 if (dev->buzzer_state)
324 dev->ctl_data->byte[HID_OR0] |= BUZZER_ON;
325 else
326 dev->ctl_data->byte[HID_OR0] &= ~BUZZER_ON;
328 error = usb_submit_urb(dev->urb_ctl, GFP_ATOMIC);
329 if (error)
330 err("%s: usb_submit_urb (urb_ctl) failed %d", __func__, error);
334 * IRQ handler
336 static void cm109_urb_irq_callback(struct urb *urb)
338 struct cm109_dev *dev = urb->context;
339 const int status = urb->status;
340 int error;
342 dev_dbg(&urb->dev->dev, "### URB IRQ: [0x%02x 0x%02x 0x%02x 0x%02x] keybit=0x%02x\n",
343 dev->irq_data->byte[0],
344 dev->irq_data->byte[1],
345 dev->irq_data->byte[2],
346 dev->irq_data->byte[3],
347 dev->keybit);
349 if (status) {
350 if (status == -ESHUTDOWN)
351 return;
352 err("%s: urb status %d", __func__, status);
355 /* Special keys */
356 if (dev->irq_data->byte[HID_IR0] & 0x0f) {
357 const int code = (dev->irq_data->byte[HID_IR0] & 0x0f);
358 report_key(dev, dev->keymap[0xff + code]);
361 /* Scan key column */
362 if (dev->keybit == 0xf) {
364 /* Any changes ? */
365 if ((dev->gpi & 0xf0) == (dev->irq_data->byte[HID_IR1] & 0xf0))
366 goto out;
368 dev->gpi = dev->irq_data->byte[HID_IR1] & 0xf0;
369 dev->keybit = 0x1;
370 } else {
371 report_key(dev, dev->keymap[dev->irq_data->byte[HID_IR1]]);
373 dev->keybit <<= 1;
374 if (dev->keybit > 0x8)
375 dev->keybit = 0xf;
378 out:
380 spin_lock(&dev->ctl_submit_lock);
382 dev->irq_urb_pending = 0;
384 if (likely(!dev->shutdown)) {
386 if (dev->buzzer_state)
387 dev->ctl_data->byte[HID_OR0] |= BUZZER_ON;
388 else
389 dev->ctl_data->byte[HID_OR0] &= ~BUZZER_ON;
391 dev->ctl_data->byte[HID_OR1] = dev->keybit;
392 dev->ctl_data->byte[HID_OR2] = dev->keybit;
394 dev->buzzer_pending = 0;
395 dev->ctl_urb_pending = 1;
397 error = usb_submit_urb(dev->urb_ctl, GFP_ATOMIC);
398 if (error)
399 err("%s: usb_submit_urb (urb_ctl) failed %d",
400 __func__, error);
403 spin_unlock(&dev->ctl_submit_lock);
406 static void cm109_urb_ctl_callback(struct urb *urb)
408 struct cm109_dev *dev = urb->context;
409 const int status = urb->status;
410 int error;
412 dev_dbg(&urb->dev->dev, "### URB CTL: [0x%02x 0x%02x 0x%02x 0x%02x]\n",
413 dev->ctl_data->byte[0],
414 dev->ctl_data->byte[1],
415 dev->ctl_data->byte[2],
416 dev->ctl_data->byte[3]);
418 if (status)
419 err("%s: urb status %d", __func__, status);
421 spin_lock(&dev->ctl_submit_lock);
423 dev->ctl_urb_pending = 0;
425 if (likely(!dev->shutdown)) {
427 if (dev->buzzer_pending) {
428 dev->buzzer_pending = 0;
429 dev->ctl_urb_pending = 1;
430 cm109_submit_buzz_toggle(dev);
431 } else if (likely(!dev->irq_urb_pending)) {
432 /* ask for key data */
433 dev->irq_urb_pending = 1;
434 error = usb_submit_urb(dev->urb_irq, GFP_ATOMIC);
435 if (error)
436 err("%s: usb_submit_urb (urb_irq) failed %d",
437 __func__, error);
441 spin_unlock(&dev->ctl_submit_lock);
444 static void cm109_toggle_buzzer_async(struct cm109_dev *dev)
446 unsigned long flags;
448 spin_lock_irqsave(&dev->ctl_submit_lock, flags);
450 if (dev->ctl_urb_pending) {
451 /* URB completion will resubmit */
452 dev->buzzer_pending = 1;
453 } else {
454 dev->ctl_urb_pending = 1;
455 cm109_submit_buzz_toggle(dev);
458 spin_unlock_irqrestore(&dev->ctl_submit_lock, flags);
461 static void cm109_toggle_buzzer_sync(struct cm109_dev *dev, int on)
463 int error;
465 if (on)
466 dev->ctl_data->byte[HID_OR0] |= BUZZER_ON;
467 else
468 dev->ctl_data->byte[HID_OR0] &= ~BUZZER_ON;
470 error = usb_control_msg(dev->udev,
471 usb_sndctrlpipe(dev->udev, 0),
472 dev->ctl_req->bRequest,
473 dev->ctl_req->bRequestType,
474 le16_to_cpu(dev->ctl_req->wValue),
475 le16_to_cpu(dev->ctl_req->wIndex),
476 dev->ctl_data,
477 USB_PKT_LEN, USB_CTRL_SET_TIMEOUT);
478 if (error < 0 && error != -EINTR)
479 err("%s: usb_control_msg() failed %d", __func__, error);
482 static void cm109_stop_traffic(struct cm109_dev *dev)
484 dev->shutdown = 1;
486 * Make sure other CPUs see this
488 smp_wmb();
490 usb_kill_urb(dev->urb_ctl);
491 usb_kill_urb(dev->urb_irq);
493 cm109_toggle_buzzer_sync(dev, 0);
495 dev->shutdown = 0;
496 smp_wmb();
499 static void cm109_restore_state(struct cm109_dev *dev)
501 if (dev->open) {
503 * Restore buzzer state.
504 * This will also kick regular URB submission
506 cm109_toggle_buzzer_async(dev);
510 /******************************************************************************
511 * input event interface
512 *****************************************************************************/
514 static int cm109_input_open(struct input_dev *idev)
516 struct cm109_dev *dev = input_get_drvdata(idev);
517 int error;
519 error = usb_autopm_get_interface(dev->intf);
520 if (error < 0) {
521 err("%s - cannot autoresume, result %d",
522 __func__, error);
523 return error;
526 mutex_lock(&dev->pm_mutex);
528 dev->buzzer_state = 0;
529 dev->key_code = -1; /* no keys pressed */
530 dev->keybit = 0xf;
532 /* issue INIT */
533 dev->ctl_data->byte[HID_OR0] = HID_OR_GPO_BUZ_SPDIF;
534 dev->ctl_data->byte[HID_OR1] = dev->keybit;
535 dev->ctl_data->byte[HID_OR2] = dev->keybit;
536 dev->ctl_data->byte[HID_OR3] = 0x00;
538 error = usb_submit_urb(dev->urb_ctl, GFP_KERNEL);
539 if (error)
540 err("%s: usb_submit_urb (urb_ctl) failed %d", __func__, error);
541 else
542 dev->open = 1;
544 mutex_unlock(&dev->pm_mutex);
546 if (error)
547 usb_autopm_put_interface(dev->intf);
549 return error;
552 static void cm109_input_close(struct input_dev *idev)
554 struct cm109_dev *dev = input_get_drvdata(idev);
556 mutex_lock(&dev->pm_mutex);
559 * Once we are here event delivery is stopped so we
560 * don't need to worry about someone starting buzzer
561 * again
563 cm109_stop_traffic(dev);
564 dev->open = 0;
566 mutex_unlock(&dev->pm_mutex);
568 usb_autopm_put_interface(dev->intf);
571 static int cm109_input_ev(struct input_dev *idev, unsigned int type,
572 unsigned int code, int value)
574 struct cm109_dev *dev = input_get_drvdata(idev);
576 dev_dbg(&dev->udev->dev,
577 "input_ev: type=%u code=%u value=%d\n", type, code, value);
579 if (type != EV_SND)
580 return -EINVAL;
582 switch (code) {
583 case SND_TONE:
584 case SND_BELL:
585 dev->buzzer_state = !!value;
586 if (!dev->resetting)
587 cm109_toggle_buzzer_async(dev);
588 return 0;
590 default:
591 return -EINVAL;
596 /******************************************************************************
597 * Linux interface and usb initialisation
598 *****************************************************************************/
600 struct driver_info {
601 char *name;
604 static const struct driver_info info_cm109 = {
605 .name = "CM109 USB driver",
608 enum {
609 VENDOR_ID = 0x0d8c, /* C-Media Electronics */
610 PRODUCT_ID_CM109 = 0x000e, /* CM109 defines range 0x0008 - 0x000f */
613 /* table of devices that work with this driver */
614 static const struct usb_device_id cm109_usb_table[] = {
616 .match_flags = USB_DEVICE_ID_MATCH_DEVICE |
617 USB_DEVICE_ID_MATCH_INT_INFO,
618 .idVendor = VENDOR_ID,
619 .idProduct = PRODUCT_ID_CM109,
620 .bInterfaceClass = USB_CLASS_HID,
621 .bInterfaceSubClass = 0,
622 .bInterfaceProtocol = 0,
623 .driver_info = (kernel_ulong_t) &info_cm109
625 /* you can add more devices here with product ID 0x0008 - 0x000f */
629 static void cm109_usb_cleanup(struct cm109_dev *dev)
631 kfree(dev->ctl_req);
632 if (dev->ctl_data)
633 usb_free_coherent(dev->udev, USB_PKT_LEN,
634 dev->ctl_data, dev->ctl_dma);
635 if (dev->irq_data)
636 usb_free_coherent(dev->udev, USB_PKT_LEN,
637 dev->irq_data, dev->irq_dma);
639 usb_free_urb(dev->urb_irq); /* parameter validation in core/urb */
640 usb_free_urb(dev->urb_ctl); /* parameter validation in core/urb */
641 kfree(dev);
644 static void cm109_usb_disconnect(struct usb_interface *interface)
646 struct cm109_dev *dev = usb_get_intfdata(interface);
648 usb_set_intfdata(interface, NULL);
649 input_unregister_device(dev->idev);
650 cm109_usb_cleanup(dev);
653 static int cm109_usb_probe(struct usb_interface *intf,
654 const struct usb_device_id *id)
656 struct usb_device *udev = interface_to_usbdev(intf);
657 struct driver_info *nfo = (struct driver_info *)id->driver_info;
658 struct usb_host_interface *interface;
659 struct usb_endpoint_descriptor *endpoint;
660 struct cm109_dev *dev;
661 struct input_dev *input_dev = NULL;
662 int ret, pipe, i;
663 int error = -ENOMEM;
665 interface = intf->cur_altsetting;
666 endpoint = &interface->endpoint[0].desc;
668 if (!usb_endpoint_is_int_in(endpoint))
669 return -ENODEV;
671 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
672 if (!dev)
673 return -ENOMEM;
675 spin_lock_init(&dev->ctl_submit_lock);
676 mutex_init(&dev->pm_mutex);
678 dev->udev = udev;
679 dev->intf = intf;
681 dev->idev = input_dev = input_allocate_device();
682 if (!input_dev)
683 goto err_out;
685 /* allocate usb buffers */
686 dev->irq_data = usb_alloc_coherent(udev, USB_PKT_LEN,
687 GFP_KERNEL, &dev->irq_dma);
688 if (!dev->irq_data)
689 goto err_out;
691 dev->ctl_data = usb_alloc_coherent(udev, USB_PKT_LEN,
692 GFP_KERNEL, &dev->ctl_dma);
693 if (!dev->ctl_data)
694 goto err_out;
696 dev->ctl_req = kmalloc(sizeof(*(dev->ctl_req)), GFP_KERNEL);
697 if (!dev->ctl_req)
698 goto err_out;
700 /* allocate urb structures */
701 dev->urb_irq = usb_alloc_urb(0, GFP_KERNEL);
702 if (!dev->urb_irq)
703 goto err_out;
705 dev->urb_ctl = usb_alloc_urb(0, GFP_KERNEL);
706 if (!dev->urb_ctl)
707 goto err_out;
709 /* get a handle to the interrupt data pipe */
710 pipe = usb_rcvintpipe(udev, endpoint->bEndpointAddress);
711 ret = usb_maxpacket(udev, pipe, usb_pipeout(pipe));
712 if (ret != USB_PKT_LEN)
713 err("invalid payload size %d, expected %d", ret, USB_PKT_LEN);
715 /* initialise irq urb */
716 usb_fill_int_urb(dev->urb_irq, udev, pipe, dev->irq_data,
717 USB_PKT_LEN,
718 cm109_urb_irq_callback, dev, endpoint->bInterval);
719 dev->urb_irq->transfer_dma = dev->irq_dma;
720 dev->urb_irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
721 dev->urb_irq->dev = udev;
723 /* initialise ctl urb */
724 dev->ctl_req->bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE |
725 USB_DIR_OUT;
726 dev->ctl_req->bRequest = USB_REQ_SET_CONFIGURATION;
727 dev->ctl_req->wValue = cpu_to_le16(0x200);
728 dev->ctl_req->wIndex = cpu_to_le16(interface->desc.bInterfaceNumber);
729 dev->ctl_req->wLength = cpu_to_le16(USB_PKT_LEN);
731 usb_fill_control_urb(dev->urb_ctl, udev, usb_sndctrlpipe(udev, 0),
732 (void *)dev->ctl_req, dev->ctl_data, USB_PKT_LEN,
733 cm109_urb_ctl_callback, dev);
734 dev->urb_ctl->transfer_dma = dev->ctl_dma;
735 dev->urb_ctl->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
736 dev->urb_ctl->dev = udev;
738 /* find out the physical bus location */
739 usb_make_path(udev, dev->phys, sizeof(dev->phys));
740 strlcat(dev->phys, "/input0", sizeof(dev->phys));
742 /* register settings for the input device */
743 input_dev->name = nfo->name;
744 input_dev->phys = dev->phys;
745 usb_to_input_id(udev, &input_dev->id);
746 input_dev->dev.parent = &intf->dev;
748 input_set_drvdata(input_dev, dev);
749 input_dev->open = cm109_input_open;
750 input_dev->close = cm109_input_close;
751 input_dev->event = cm109_input_ev;
753 input_dev->keycode = dev->keymap;
754 input_dev->keycodesize = sizeof(unsigned char);
755 input_dev->keycodemax = ARRAY_SIZE(dev->keymap);
757 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_SND);
758 input_dev->sndbit[0] = BIT_MASK(SND_BELL) | BIT_MASK(SND_TONE);
760 /* register available key events */
761 for (i = 0; i < KEYMAP_SIZE; i++) {
762 unsigned short k = keymap(i);
763 dev->keymap[i] = k;
764 __set_bit(k, input_dev->keybit);
766 __clear_bit(KEY_RESERVED, input_dev->keybit);
768 error = input_register_device(dev->idev);
769 if (error)
770 goto err_out;
772 usb_set_intfdata(intf, dev);
774 return 0;
776 err_out:
777 input_free_device(input_dev);
778 cm109_usb_cleanup(dev);
779 return error;
782 static int cm109_usb_suspend(struct usb_interface *intf, pm_message_t message)
784 struct cm109_dev *dev = usb_get_intfdata(intf);
786 dev_info(&intf->dev, "cm109: usb_suspend (event=%d)\n", message.event);
788 mutex_lock(&dev->pm_mutex);
789 cm109_stop_traffic(dev);
790 mutex_unlock(&dev->pm_mutex);
792 return 0;
795 static int cm109_usb_resume(struct usb_interface *intf)
797 struct cm109_dev *dev = usb_get_intfdata(intf);
799 dev_info(&intf->dev, "cm109: usb_resume\n");
801 mutex_lock(&dev->pm_mutex);
802 cm109_restore_state(dev);
803 mutex_unlock(&dev->pm_mutex);
805 return 0;
808 static int cm109_usb_pre_reset(struct usb_interface *intf)
810 struct cm109_dev *dev = usb_get_intfdata(intf);
812 mutex_lock(&dev->pm_mutex);
815 * Make sure input events don't try to toggle buzzer
816 * while we are resetting
818 dev->resetting = 1;
819 smp_wmb();
821 cm109_stop_traffic(dev);
823 return 0;
826 static int cm109_usb_post_reset(struct usb_interface *intf)
828 struct cm109_dev *dev = usb_get_intfdata(intf);
830 dev->resetting = 0;
831 smp_wmb();
833 cm109_restore_state(dev);
835 mutex_unlock(&dev->pm_mutex);
837 return 0;
840 static struct usb_driver cm109_driver = {
841 .name = "cm109",
842 .probe = cm109_usb_probe,
843 .disconnect = cm109_usb_disconnect,
844 .suspend = cm109_usb_suspend,
845 .resume = cm109_usb_resume,
846 .reset_resume = cm109_usb_resume,
847 .pre_reset = cm109_usb_pre_reset,
848 .post_reset = cm109_usb_post_reset,
849 .id_table = cm109_usb_table,
850 .supports_autosuspend = 1,
853 static int __init cm109_select_keymap(void)
855 /* Load the phone keymap */
856 if (!strcasecmp(phone, "kip1000")) {
857 keymap = keymap_kip1000;
858 printk(KERN_INFO KBUILD_MODNAME ": "
859 "Keymap for Komunikate KIP1000 phone loaded\n");
860 } else if (!strcasecmp(phone, "gtalk")) {
861 keymap = keymap_gtalk;
862 printk(KERN_INFO KBUILD_MODNAME ": "
863 "Keymap for Genius G-talk phone loaded\n");
864 } else if (!strcasecmp(phone, "usbph01")) {
865 keymap = keymap_usbph01;
866 printk(KERN_INFO KBUILD_MODNAME ": "
867 "Keymap for Allied-Telesis Corega USBPH01 phone loaded\n");
868 } else if (!strcasecmp(phone, "atcom")) {
869 keymap = keymap_atcom;
870 printk(KERN_INFO KBUILD_MODNAME ": "
871 "Keymap for ATCom AU-100 phone loaded\n");
872 } else {
873 printk(KERN_ERR KBUILD_MODNAME ": "
874 "Unsupported phone: %s\n", phone);
875 return -EINVAL;
878 return 0;
881 static int __init cm109_init(void)
883 int err;
885 err = cm109_select_keymap();
886 if (err)
887 return err;
889 err = usb_register(&cm109_driver);
890 if (err)
891 return err;
893 printk(KERN_INFO KBUILD_MODNAME ": "
894 DRIVER_DESC ": " DRIVER_VERSION " (C) " DRIVER_AUTHOR "\n");
896 return 0;
899 static void __exit cm109_exit(void)
901 usb_deregister(&cm109_driver);
904 module_init(cm109_init);
905 module_exit(cm109_exit);
907 MODULE_DEVICE_TABLE(usb, cm109_usb_table);
909 MODULE_AUTHOR(DRIVER_AUTHOR);
910 MODULE_DESCRIPTION(DRIVER_DESC);
911 MODULE_LICENSE("GPL");