]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/input/misc/cm109.c
ftrace: prevent recursion
[linux-2.6-omap-h63xx.git] / 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}");
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 static unsigned short (*keymap)(int) = keymap_kip1000;
262
263 /*
264  * Completes a request by converting the data into events for the
265  * input subsystem.
266  */
267 static void report_key(struct cm109_dev *dev, int key)
268 {
269         struct input_dev *idev = dev->idev;
270
271         if (dev->key_code >= 0) {
272                 /* old key up */
273                 input_report_key(idev, dev->key_code, 0);
274         }
275
276         dev->key_code = key;
277         if (key >= 0) {
278                 /* new valid key */
279                 input_report_key(idev, key, 1);
280         }
281
282         input_sync(idev);
283 }
284
285 /******************************************************************************
286  * CM109 usb communication interface
287  *****************************************************************************/
288
289 static void cm109_submit_buzz_toggle(struct cm109_dev *dev)
290 {
291         int error;
292
293         if (dev->buzzer_state)
294                 dev->ctl_data->byte[HID_OR0] |= BUZZER_ON;
295         else
296                 dev->ctl_data->byte[HID_OR0] &= ~BUZZER_ON;
297
298         error = usb_submit_urb(dev->urb_ctl, GFP_ATOMIC);
299         if (error)
300                 err("%s: usb_submit_urb (urb_ctl) failed %d", __func__, error);
301 }
302
303 /*
304  * IRQ handler
305  */
306 static void cm109_urb_irq_callback(struct urb *urb)
307 {
308         struct cm109_dev *dev = urb->context;
309         const int status = urb->status;
310         int error;
311
312         dev_dbg(&urb->dev->dev, "### URB IRQ: [0x%02x 0x%02x 0x%02x 0x%02x] keybit=0x%02x\n",
313              dev->irq_data->byte[0],
314              dev->irq_data->byte[1],
315              dev->irq_data->byte[2],
316              dev->irq_data->byte[3],
317              dev->keybit);
318
319         if (status) {
320                 if (status == -ESHUTDOWN)
321                         return;
322                 err("%s: urb status %d", __func__, status);
323         }
324
325         /* Special keys */
326         if (dev->irq_data->byte[HID_IR0] & 0x0f) {
327                 const int code = (dev->irq_data->byte[HID_IR0] & 0x0f);
328                 report_key(dev, dev->keymap[0xff + code]);
329         }
330
331         /* Scan key column */
332         if (dev->keybit == 0xf) {
333
334                 /* Any changes ? */
335                 if ((dev->gpi & 0xf0) == (dev->irq_data->byte[HID_IR1] & 0xf0))
336                         goto out;
337
338                 dev->gpi = dev->irq_data->byte[HID_IR1] & 0xf0;
339                 dev->keybit = 0x1;
340         } else {
341                 report_key(dev, dev->keymap[dev->irq_data->byte[HID_IR1]]);
342
343                 dev->keybit <<= 1;
344                 if (dev->keybit > 0x8)
345                         dev->keybit = 0xf;
346         }
347
348  out:
349
350         spin_lock(&dev->ctl_submit_lock);
351
352         dev->irq_urb_pending = 0;
353
354         if (likely(!dev->shutdown)) {
355
356                 if (dev->buzzer_state)
357                         dev->ctl_data->byte[HID_OR0] |= BUZZER_ON;
358                 else
359                         dev->ctl_data->byte[HID_OR0] &= ~BUZZER_ON;
360
361                 dev->ctl_data->byte[HID_OR1] = dev->keybit;
362                 dev->ctl_data->byte[HID_OR2] = dev->keybit;
363
364                 dev->buzzer_pending = 0;
365                 dev->ctl_urb_pending = 1;
366
367                 error = usb_submit_urb(dev->urb_ctl, GFP_ATOMIC);
368                 if (error)
369                         err("%s: usb_submit_urb (urb_ctl) failed %d",
370                                 __func__, error);
371         }
372
373         spin_unlock(&dev->ctl_submit_lock);
374 }
375
376 static void cm109_urb_ctl_callback(struct urb *urb)
377 {
378         struct cm109_dev *dev = urb->context;
379         const int status = urb->status;
380         int error;
381
382         dev_dbg(&urb->dev->dev, "### URB CTL: [0x%02x 0x%02x 0x%02x 0x%02x]\n",
383              dev->ctl_data->byte[0],
384              dev->ctl_data->byte[1],
385              dev->ctl_data->byte[2],
386              dev->ctl_data->byte[3]);
387
388         if (status)
389                 err("%s: urb status %d", __func__, status);
390
391         spin_lock(&dev->ctl_submit_lock);
392
393         dev->ctl_urb_pending = 0;
394
395         if (likely(!dev->shutdown)) {
396
397                 if (dev->buzzer_pending) {
398                         dev->buzzer_pending = 0;
399                         dev->ctl_urb_pending = 1;
400                         cm109_submit_buzz_toggle(dev);
401                 } else if (likely(!dev->irq_urb_pending)) {
402                         /* ask for key data */
403                         dev->irq_urb_pending = 1;
404                         error = usb_submit_urb(dev->urb_irq, GFP_ATOMIC);
405                         if (error)
406                                 err("%s: usb_submit_urb (urb_irq) failed %d",
407                                         __func__, error);
408                 }
409         }
410
411         spin_unlock(&dev->ctl_submit_lock);
412 }
413
414 static void cm109_toggle_buzzer_async(struct cm109_dev *dev)
415 {
416         unsigned long flags;
417
418         spin_lock_irqsave(&dev->ctl_submit_lock, flags);
419
420         if (dev->ctl_urb_pending) {
421                 /* URB completion will resubmit */
422                 dev->buzzer_pending = 1;
423         } else {
424                 dev->ctl_urb_pending = 1;
425                 cm109_submit_buzz_toggle(dev);
426         }
427
428         spin_unlock_irqrestore(&dev->ctl_submit_lock, flags);
429 }
430
431 static void cm109_toggle_buzzer_sync(struct cm109_dev *dev, int on)
432 {
433         int error;
434
435         if (on)
436                 dev->ctl_data->byte[HID_OR0] |= BUZZER_ON;
437         else
438                 dev->ctl_data->byte[HID_OR0] &= ~BUZZER_ON;
439
440         error = usb_control_msg(dev->udev,
441                                 usb_sndctrlpipe(dev->udev, 0),
442                                 dev->ctl_req->bRequest,
443                                 dev->ctl_req->bRequestType,
444                                 le16_to_cpu(dev->ctl_req->wValue),
445                                 le16_to_cpu(dev->ctl_req->wIndex),
446                                 dev->ctl_data,
447                                 USB_PKT_LEN, USB_CTRL_SET_TIMEOUT);
448         if (error && error != EINTR)
449                 err("%s: usb_control_msg() failed %d", __func__, error);
450 }
451
452 static void cm109_stop_traffic(struct cm109_dev *dev)
453 {
454         dev->shutdown = 1;
455         /*
456          * Make sure other CPUs see this
457          */
458         smp_wmb();
459
460         usb_kill_urb(dev->urb_ctl);
461         usb_kill_urb(dev->urb_irq);
462
463         cm109_toggle_buzzer_sync(dev, 0);
464
465         dev->shutdown = 0;
466         smp_wmb();
467 }
468
469 static void cm109_restore_state(struct cm109_dev *dev)
470 {
471         if (dev->open) {
472                 /*
473                  * Restore buzzer state.
474                  * This will also kick regular URB submission
475                  */
476                 cm109_toggle_buzzer_async(dev);
477         }
478 }
479
480 /******************************************************************************
481  * input event interface
482  *****************************************************************************/
483
484 static int cm109_input_open(struct input_dev *idev)
485 {
486         struct cm109_dev *dev = input_get_drvdata(idev);
487         int error;
488
489         error = usb_autopm_get_interface(dev->intf);
490         if (error < 0) {
491                 err("%s - cannot autoresume, result %d",
492                     __func__, error);
493                 return error;
494         }
495
496         mutex_lock(&dev->pm_mutex);
497
498         dev->buzzer_state = 0;
499         dev->key_code = -1;     /* no keys pressed */
500         dev->keybit = 0xf;
501
502         /* issue INIT */
503         dev->ctl_data->byte[HID_OR0] = HID_OR_GPO_BUZ_SPDIF;
504         dev->ctl_data->byte[HID_OR1] = dev->keybit;
505         dev->ctl_data->byte[HID_OR2] = dev->keybit;
506         dev->ctl_data->byte[HID_OR3] = 0x00;
507
508         error = usb_submit_urb(dev->urb_ctl, GFP_KERNEL);
509         if (error)
510                 err("%s: usb_submit_urb (urb_ctl) failed %d", __func__, error);
511         else
512                 dev->open = 1;
513
514         mutex_unlock(&dev->pm_mutex);
515
516         if (error)
517                 usb_autopm_put_interface(dev->intf);
518
519         return error;
520 }
521
522 static void cm109_input_close(struct input_dev *idev)
523 {
524         struct cm109_dev *dev = input_get_drvdata(idev);
525
526         mutex_lock(&dev->pm_mutex);
527
528         /*
529          * Once we are here event delivery is stopped so we
530          * don't need to worry about someone starting buzzer
531          * again
532          */
533         cm109_stop_traffic(dev);
534         dev->open = 0;
535
536         mutex_unlock(&dev->pm_mutex);
537
538         usb_autopm_put_interface(dev->intf);
539 }
540
541 static int cm109_input_ev(struct input_dev *idev, unsigned int type,
542                           unsigned int code, int value)
543 {
544         struct cm109_dev *dev = input_get_drvdata(idev);
545
546         dev_dbg(&dev->udev->dev,
547                 "input_ev: type=%u code=%u value=%d\n", type, code, value);
548
549         if (type != EV_SND)
550                 return -EINVAL;
551
552         switch (code) {
553         case SND_TONE:
554         case SND_BELL:
555                 dev->buzzer_state = !!value;
556                 if (!dev->resetting)
557                         cm109_toggle_buzzer_async(dev);
558                 return 0;
559
560         default:
561                 return -EINVAL;
562         }
563 }
564
565
566 /******************************************************************************
567  * Linux interface and usb initialisation
568  *****************************************************************************/
569
570 struct driver_info {
571         char *name;
572 };
573
574 static const struct driver_info info_cm109 = {
575         .name = "CM109 USB driver",
576 };
577
578 enum {
579         VENDOR_ID        = 0x0d8c, /* C-Media Electronics */
580         PRODUCT_ID_CM109 = 0x000e, /* CM109 defines range 0x0008 - 0x000f */
581 };
582
583 /* table of devices that work with this driver */
584 static const struct usb_device_id cm109_usb_table[] = {
585         {
586                 .match_flags = USB_DEVICE_ID_MATCH_DEVICE |
587                                 USB_DEVICE_ID_MATCH_INT_INFO,
588                 .idVendor = VENDOR_ID,
589                 .idProduct = PRODUCT_ID_CM109,
590                 .bInterfaceClass = USB_CLASS_HID,
591                 .bInterfaceSubClass = 0,
592                 .bInterfaceProtocol = 0,
593                 .driver_info = (kernel_ulong_t) &info_cm109
594         },
595         /* you can add more devices here with product ID 0x0008 - 0x000f */
596         { }
597 };
598
599 static void cm109_usb_cleanup(struct cm109_dev *dev)
600 {
601         if (dev->ctl_req)
602                 usb_buffer_free(dev->udev, sizeof(*(dev->ctl_req)),
603                                 dev->ctl_req, dev->ctl_req_dma);
604         if (dev->ctl_data)
605                 usb_buffer_free(dev->udev, USB_PKT_LEN,
606                                 dev->ctl_data, dev->ctl_dma);
607         if (dev->irq_data)
608                 usb_buffer_free(dev->udev, USB_PKT_LEN,
609                                 dev->irq_data, dev->irq_dma);
610
611         usb_free_urb(dev->urb_irq);     /* parameter validation in core/urb */
612         usb_free_urb(dev->urb_ctl);     /* parameter validation in core/urb */
613         kfree(dev);
614 }
615
616 static void cm109_usb_disconnect(struct usb_interface *interface)
617 {
618         struct cm109_dev *dev = usb_get_intfdata(interface);
619
620         usb_set_intfdata(interface, NULL);
621         input_unregister_device(dev->idev);
622         cm109_usb_cleanup(dev);
623 }
624
625 static int cm109_usb_probe(struct usb_interface *intf,
626                            const struct usb_device_id *id)
627 {
628         struct usb_device *udev = interface_to_usbdev(intf);
629         struct driver_info *nfo = (struct driver_info *)id->driver_info;
630         struct usb_host_interface *interface;
631         struct usb_endpoint_descriptor *endpoint;
632         struct cm109_dev *dev;
633         struct input_dev *input_dev = NULL;
634         int ret, pipe, i;
635         int error = -ENOMEM;
636
637         interface = intf->cur_altsetting;
638         endpoint = &interface->endpoint[0].desc;
639
640         if (!usb_endpoint_is_int_in(endpoint))
641                 return -ENODEV;
642
643         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
644         if (!dev)
645                 return -ENOMEM;
646
647         spin_lock_init(&dev->ctl_submit_lock);
648         mutex_init(&dev->pm_mutex);
649
650         dev->udev = udev;
651         dev->intf = intf;
652
653         dev->idev = input_dev = input_allocate_device();
654         if (!input_dev)
655                 goto err_out;
656
657         /* allocate usb buffers */
658         dev->irq_data = usb_buffer_alloc(udev, USB_PKT_LEN,
659                                          GFP_KERNEL, &dev->irq_dma);
660         if (!dev->irq_data)
661                 goto err_out;
662
663         dev->ctl_data = usb_buffer_alloc(udev, USB_PKT_LEN,
664                                          GFP_KERNEL, &dev->ctl_dma);
665         if (!dev->ctl_data)
666                 goto err_out;
667
668         dev->ctl_req = usb_buffer_alloc(udev, sizeof(*(dev->ctl_req)),
669                                         GFP_KERNEL, &dev->ctl_req_dma);
670         if (!dev->ctl_req)
671                 goto err_out;
672
673         /* allocate urb structures */
674         dev->urb_irq = usb_alloc_urb(0, GFP_KERNEL);
675         if (!dev->urb_irq)
676                 goto err_out;
677
678         dev->urb_ctl = usb_alloc_urb(0, GFP_KERNEL);
679         if (!dev->urb_ctl)
680                 goto err_out;
681
682         /* get a handle to the interrupt data pipe */
683         pipe = usb_rcvintpipe(udev, endpoint->bEndpointAddress);
684         ret = usb_maxpacket(udev, pipe, usb_pipeout(pipe));
685         if (ret != USB_PKT_LEN)
686                 err("invalid payload size %d, expected %d", ret, USB_PKT_LEN);
687
688         /* initialise irq urb */
689         usb_fill_int_urb(dev->urb_irq, udev, pipe, dev->irq_data,
690                          USB_PKT_LEN,
691                          cm109_urb_irq_callback, dev, endpoint->bInterval);
692         dev->urb_irq->transfer_dma = dev->irq_dma;
693         dev->urb_irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
694         dev->urb_irq->dev = udev;
695
696         /* initialise ctl urb */
697         dev->ctl_req->bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE |
698                                         USB_DIR_OUT;
699         dev->ctl_req->bRequest = USB_REQ_SET_CONFIGURATION;
700         dev->ctl_req->wValue = cpu_to_le16(0x200);
701         dev->ctl_req->wIndex = cpu_to_le16(interface->desc.bInterfaceNumber);
702         dev->ctl_req->wLength = cpu_to_le16(USB_PKT_LEN);
703
704         usb_fill_control_urb(dev->urb_ctl, udev, usb_sndctrlpipe(udev, 0),
705                              (void *)dev->ctl_req, dev->ctl_data, USB_PKT_LEN,
706                              cm109_urb_ctl_callback, dev);
707         dev->urb_ctl->setup_dma = dev->ctl_req_dma;
708         dev->urb_ctl->transfer_dma = dev->ctl_dma;
709         dev->urb_ctl->transfer_flags |= URB_NO_SETUP_DMA_MAP |
710                                         URB_NO_TRANSFER_DMA_MAP;
711         dev->urb_ctl->dev = udev;
712
713         /* find out the physical bus location */
714         usb_make_path(udev, dev->phys, sizeof(dev->phys));
715         strlcat(dev->phys, "/input0", sizeof(dev->phys));
716
717         /* register settings for the input device */
718         input_dev->name = nfo->name;
719         input_dev->phys = dev->phys;
720         usb_to_input_id(udev, &input_dev->id);
721         input_dev->dev.parent = &intf->dev;
722
723         input_set_drvdata(input_dev, dev);
724         input_dev->open = cm109_input_open;
725         input_dev->close = cm109_input_close;
726         input_dev->event = cm109_input_ev;
727
728         input_dev->keycode = dev->keymap;
729         input_dev->keycodesize = sizeof(unsigned char);
730         input_dev->keycodemax = ARRAY_SIZE(dev->keymap);
731
732         input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_SND);
733         input_dev->sndbit[0] = BIT_MASK(SND_BELL) | BIT_MASK(SND_TONE);
734
735         /* register available key events */
736         for (i = 0; i < KEYMAP_SIZE; i++) {
737                 unsigned short k = keymap(i);
738                 dev->keymap[i] = k;
739                 __set_bit(k, input_dev->keybit);
740         }
741         __clear_bit(KEY_RESERVED, input_dev->keybit);
742
743         error = input_register_device(dev->idev);
744         if (error)
745                 goto err_out;
746
747         usb_set_intfdata(intf, dev);
748
749         return 0;
750
751  err_out:
752         input_free_device(input_dev);
753         cm109_usb_cleanup(dev);
754         return error;
755 }
756
757 static int cm109_usb_suspend(struct usb_interface *intf, pm_message_t message)
758 {
759         struct cm109_dev *dev = usb_get_intfdata(intf);
760
761         dev_info(&intf->dev, "cm109: usb_suspend (event=%d)\n", message.event);
762
763         mutex_lock(&dev->pm_mutex);
764         cm109_stop_traffic(dev);
765         mutex_unlock(&dev->pm_mutex);
766
767         return 0;
768 }
769
770 static int cm109_usb_resume(struct usb_interface *intf)
771 {
772         struct cm109_dev *dev = usb_get_intfdata(intf);
773
774         dev_info(&intf->dev, "cm109: usb_resume\n");
775
776         mutex_lock(&dev->pm_mutex);
777         cm109_restore_state(dev);
778         mutex_unlock(&dev->pm_mutex);
779
780         return 0;
781 }
782
783 static int cm109_usb_pre_reset(struct usb_interface *intf)
784 {
785         struct cm109_dev *dev = usb_get_intfdata(intf);
786
787         mutex_lock(&dev->pm_mutex);
788
789         /*
790          * Make sure input events don't try to toggle buzzer
791          * while we are resetting
792          */
793         dev->resetting = 1;
794         smp_wmb();
795
796         cm109_stop_traffic(dev);
797
798         return 0;
799 }
800
801 static int cm109_usb_post_reset(struct usb_interface *intf)
802 {
803         struct cm109_dev *dev = usb_get_intfdata(intf);
804
805         dev->resetting = 0;
806         smp_wmb();
807
808         cm109_restore_state(dev);
809
810         mutex_unlock(&dev->pm_mutex);
811
812         return 0;
813 }
814
815 static struct usb_driver cm109_driver = {
816         .name           = "cm109",
817         .probe          = cm109_usb_probe,
818         .disconnect     = cm109_usb_disconnect,
819         .suspend        = cm109_usb_suspend,
820         .resume         = cm109_usb_resume,
821         .reset_resume   = cm109_usb_resume,
822         .pre_reset      = cm109_usb_pre_reset,
823         .post_reset     = cm109_usb_post_reset,
824         .id_table       = cm109_usb_table,
825         .supports_autosuspend = 1,
826 };
827
828 static int __init cm109_select_keymap(void)
829 {
830         /* Load the phone keymap */
831         if (!strcasecmp(phone, "kip1000")) {
832                 keymap = keymap_kip1000;
833                 printk(KERN_INFO KBUILD_MODNAME ": "
834                         "Keymap for Komunikate KIP1000 phone loaded\n");
835         } else if (!strcasecmp(phone, "gtalk")) {
836                 keymap = keymap_gtalk;
837                 printk(KERN_INFO KBUILD_MODNAME ": "
838                         "Keymap for Genius G-talk phone loaded\n");
839         } else if (!strcasecmp(phone, "usbph01")) {
840                 keymap = keymap_usbph01;
841                 printk(KERN_INFO KBUILD_MODNAME ": "
842                         "Keymap for Allied-Telesis Corega USBPH01 phone loaded\n");
843         } else {
844                 printk(KERN_ERR KBUILD_MODNAME ": "
845                         "Unsupported phone: %s\n", phone);
846                 return -EINVAL;
847         }
848
849         return 0;
850 }
851
852 static int __init cm109_init(void)
853 {
854         int err;
855
856         err = cm109_select_keymap();
857         if (err)
858                 return err;
859
860         err = usb_register(&cm109_driver);
861         if (err)
862                 return err;
863
864         printk(KERN_INFO KBUILD_MODNAME ": "
865                 DRIVER_DESC ": " DRIVER_VERSION " (C) " DRIVER_AUTHOR "\n");
866
867         return 0;
868 }
869
870 static void __exit cm109_exit(void)
871 {
872         usb_deregister(&cm109_driver);
873 }
874
875 module_init(cm109_init);
876 module_exit(cm109_exit);
877
878 MODULE_DEVICE_TABLE(usb, cm109_usb_table);
879
880 MODULE_AUTHOR(DRIVER_AUTHOR);
881 MODULE_DESCRIPTION(DRIVER_DESC);
882 MODULE_LICENSE("GPL");