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