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