Merge branch 'atmel'
[cascardo/linux.git] / drivers / usb / input / yealink.c
1 /*
2  * drivers/usb/input/yealink.c
3  *
4  * Copyright (c) 2005 Henk Vergonet <Henk.Vergonet@gmail.com>
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; either version 2 of
9  * the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */
20 /*
21  * Description:
22  *   Driver for the USB-P1K voip usb phone.
23  *   This device is produced by Yealink Network Technology Co Ltd
24  *   but may be branded under several names:
25  *      - Yealink usb-p1k
26  *      - Tiptel 115
27  *      - ...
28  *
29  * This driver is based on:
30  *   - the usbb2k-api   http://savannah.nongnu.org/projects/usbb2k-api/
31  *   - information from http://memeteau.free.fr/usbb2k
32  *   - the xpad-driver  drivers/usb/input/xpad.c
33  *
34  * Thanks to:
35  *   - Olivier Vandorpe, for providing the usbb2k-api.
36  *   - Martin Diehl, for spotting my memory allocation bug.
37  *
38  * History:
39  *   20050527 henk      First version, functional keyboard. Keyboard events
40  *                      will pop-up on the ../input/eventX bus.
41  *   20050531 henk      Added led, LCD, dialtone and sysfs interface.
42  *   20050610 henk      Cleanups, make it ready for public consumption.
43  *   20050630 henk      Cleanups, fixes in response to comments.
44  *   20050701 henk      sysfs write serialisation, fix potential unload races
45  *   20050801 henk      Added ringtone, restructure USB
46  *   20050816 henk      Merge 2.6.13-rc6
47  */
48
49 #include <linux/config.h>
50 #include <linux/kernel.h>
51 #include <linux/input.h>
52 #include <linux/init.h>
53 #include <linux/slab.h>
54 #include <linux/module.h>
55 #include <linux/rwsem.h>
56 #include <linux/usb.h>
57 #include <linux/usb_input.h>
58
59 #include "map_to_7segment.h"
60 #include "yealink.h"
61
62 #define DRIVER_VERSION "yld-20050816"
63 #define DRIVER_AUTHOR "Henk Vergonet"
64 #define DRIVER_DESC "Yealink phone driver"
65
66 #define YEALINK_POLLING_FREQUENCY       10      /* in [Hz] */
67
68 struct yld_status {
69         u8      lcd[24];
70         u8      led;
71         u8      dialtone;
72         u8      ringtone;
73         u8      keynum;
74 } __attribute__ ((packed));
75
76 /*
77  * Register the LCD segment and icon map
78  */
79 #define _LOC(k,l)       { .a = (k), .m = (l) }
80 #define _SEG(t, a, am, b, bm, c, cm, d, dm, e, em, f, fm, g, gm)        \
81         { .type = (t),                                                  \
82           .u = { .s = { _LOC(a, am), _LOC(b, bm), _LOC(c, cm),          \
83                         _LOC(d, dm), _LOC(e, em), _LOC(g, gm),          \
84                         _LOC(f, fm) } } }
85 #define _PIC(t, h, hm, n)                                               \
86         { .type = (t),                                                  \
87           .u = { .p = { .name = (n), .a = (h), .m = (hm) } } }
88
89 static const struct lcd_segment_map {
90         char    type;
91         union {
92                 struct pictogram_map {
93                         u8      a,m;
94                         char    name[10];
95                 }       p;
96                 struct segment_map {
97                         u8      a,m;
98                 } s[7];
99         } u;
100 } lcdMap[] = {
101 #include "yealink.h"
102 };
103
104 struct yealink_dev {
105         struct input_dev *idev;         /* input device */
106         struct usb_device *udev;        /* usb device */
107
108         /* irq input channel */
109         struct yld_ctl_packet   *irq_data;
110         dma_addr_t              irq_dma;
111         struct urb              *urb_irq;
112
113         /* control output channel */
114         struct yld_ctl_packet   *ctl_data;
115         dma_addr_t              ctl_dma;
116         struct usb_ctrlrequest  *ctl_req;
117         dma_addr_t              ctl_req_dma;
118         struct urb              *urb_ctl;
119
120         char phys[64];                  /* physical device path */
121
122         u8 lcdMap[ARRAY_SIZE(lcdMap)];  /* state of LCD, LED ... */
123         int key_code;                   /* last reported key     */
124
125         int     stat_ix;
126         union {
127                 struct yld_status s;
128                 u8                b[sizeof(struct yld_status)];
129         } master, copy;
130 };
131
132
133 /*******************************************************************************
134  * Yealink lcd interface
135  ******************************************************************************/
136
137 /*
138  * Register a default 7 segment character set
139  */
140 static SEG7_DEFAULT_MAP(map_seg7);
141
142  /* Display a char,
143   * char '\9' and '\n' are placeholders and do not overwrite the original text.
144   * A space will always hide an icon.
145   */
146 static int setChar(struct yealink_dev *yld, int el, int chr)
147 {
148         int i, a, m, val;
149
150         if (el >= ARRAY_SIZE(lcdMap))
151                 return -EINVAL;
152
153         if (chr == '\t' || chr == '\n')
154             return 0;
155
156         yld->lcdMap[el] = chr;
157
158         if (lcdMap[el].type == '.') {
159                 a = lcdMap[el].u.p.a;
160                 m = lcdMap[el].u.p.m;
161                 if (chr != ' ')
162                         yld->master.b[a] |= m;
163                 else
164                         yld->master.b[a] &= ~m;
165                 return 0;
166         }
167
168         val = map_to_seg7(&map_seg7, chr);
169         for (i = 0; i < ARRAY_SIZE(lcdMap[0].u.s); i++) {
170                 m = lcdMap[el].u.s[i].m;
171
172                 if (m == 0)
173                         continue;
174
175                 a = lcdMap[el].u.s[i].a;
176                 if (val & 1)
177                         yld->master.b[a] |= m;
178                 else
179                         yld->master.b[a] &= ~m;
180                 val = val >> 1;
181         }
182         return 0;
183 };
184
185 /*******************************************************************************
186  * Yealink key interface
187  ******************************************************************************/
188
189 /* Map device buttons to internal key events.
190  *
191  * USB-P1K button layout:
192  *
193  *             up
194  *       IN           OUT
195  *            down
196  *
197  *     pickup   C    hangup
198  *       1      2      3
199  *       4      5      6
200  *       7      8      9
201  *       *      0      #
202  *
203  * The "up" and "down" keys, are symbolised by arrows on the button.
204  * The "pickup" and "hangup" keys are symbolised by a green and red phone
205  * on the button.
206  */
207 static int map_p1k_to_key(int scancode)
208 {
209         switch(scancode) {              /* phone key:   */
210         case 0x23: return KEY_LEFT;     /*   IN         */
211         case 0x33: return KEY_UP;       /*   up         */
212         case 0x04: return KEY_RIGHT;    /*   OUT        */
213         case 0x24: return KEY_DOWN;     /*   down       */
214         case 0x03: return KEY_ENTER;    /*   pickup     */
215         case 0x14: return KEY_BACKSPACE; /*  C          */
216         case 0x13: return KEY_ESC;      /*   hangup     */
217         case 0x00: return KEY_1;        /*   1          */
218         case 0x01: return KEY_2;        /*   2          */
219         case 0x02: return KEY_3;        /*   3          */
220         case 0x10: return KEY_4;        /*   4          */
221         case 0x11: return KEY_5;        /*   5          */
222         case 0x12: return KEY_6;        /*   6          */
223         case 0x20: return KEY_7;        /*   7          */
224         case 0x21: return KEY_8;        /*   8          */
225         case 0x22: return KEY_9;        /*   9          */
226         case 0x30: return KEY_KPASTERISK; /* *          */
227         case 0x31: return KEY_0;        /*   0          */
228         case 0x32: return KEY_LEFTSHIFT |
229                           KEY_3 << 8;   /*   #          */
230         }
231         return -EINVAL;
232 }
233
234 /* Completes a request by converting the data into events for the
235  * input subsystem.
236  *
237  * The key parameter can be cascaded: key2 << 8 | key1
238  */
239 static void report_key(struct yealink_dev *yld, int key, struct pt_regs *regs)
240 {
241         struct input_dev *idev = yld->idev;
242
243         input_regs(idev, regs);
244         if (yld->key_code >= 0) {
245                 /* old key up */
246                 input_report_key(idev, yld->key_code & 0xff, 0);
247                 if (yld->key_code >> 8)
248                         input_report_key(idev, yld->key_code >> 8, 0);
249         }
250
251         yld->key_code = key;
252         if (key >= 0) {
253                 /* new valid key */
254                 input_report_key(idev, key & 0xff, 1);
255                 if (key >> 8)
256                         input_report_key(idev, key >> 8, 1);
257         }
258         input_sync(idev);
259 }
260
261 /*******************************************************************************
262  * Yealink usb communication interface
263  ******************************************************************************/
264
265 static int yealink_cmd(struct yealink_dev *yld, struct yld_ctl_packet *p)
266 {
267         u8      *buf = (u8 *)p;
268         int     i;
269         u8      sum = 0;
270
271         for(i=0; i<USB_PKT_LEN-1; i++)
272                 sum -= buf[i];
273         p->sum = sum;
274         return usb_control_msg(yld->udev,
275                         usb_sndctrlpipe(yld->udev, 0),
276                         USB_REQ_SET_CONFIGURATION,
277                         USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
278                         0x200, 3,
279                         p, sizeof(*p),
280                         USB_CTRL_SET_TIMEOUT);
281 }
282
283 static u8 default_ringtone[] = {
284         0xEF,                   /* volume [0-255] */
285         0xFB, 0x1E, 0x00, 0x0C, /* 1250 [hz], 12/100 [s] */
286         0xFC, 0x18, 0x00, 0x0C, /* 1000 [hz], 12/100 [s] */
287         0xFB, 0x1E, 0x00, 0x0C,
288         0xFC, 0x18, 0x00, 0x0C,
289         0xFB, 0x1E, 0x00, 0x0C,
290         0xFC, 0x18, 0x00, 0x0C,
291         0xFB, 0x1E, 0x00, 0x0C,
292         0xFC, 0x18, 0x00, 0x0C,
293         0xFF, 0xFF, 0x01, 0x90, /* silent, 400/100 [s] */
294         0x00, 0x00              /* end of sequence */
295 };
296
297 static int yealink_set_ringtone(struct yealink_dev *yld, u8 *buf, size_t size)
298 {
299         struct yld_ctl_packet *p = yld->ctl_data;
300         int     ix, len;
301
302         if (size <= 0)
303                 return -EINVAL;
304
305         /* Set the ringtone volume */
306         memset(yld->ctl_data, 0, sizeof(*(yld->ctl_data)));
307         yld->ctl_data->cmd      = CMD_RING_VOLUME;
308         yld->ctl_data->size     = 1;
309         yld->ctl_data->data[0]  = buf[0];
310         yealink_cmd(yld, p);
311
312         buf++;
313         size--;
314
315         p->cmd = CMD_RING_NOTE;
316         ix = 0;
317         while (size != ix) {
318                 len = size - ix;
319                 if (len > sizeof(p->data))
320                         len = sizeof(p->data);
321                 p->size   = len;
322                 p->offset = cpu_to_be16(ix);
323                 memcpy(p->data, &buf[ix], len);
324                 yealink_cmd(yld, p);
325                 ix += len;
326         }
327         return 0;
328 }
329
330 /* keep stat_master & stat_copy in sync.
331  */
332 static int yealink_do_idle_tasks(struct yealink_dev *yld)
333 {
334         u8 val;
335         int i, ix, len;
336
337         ix = yld->stat_ix;
338
339         memset(yld->ctl_data, 0, sizeof(*(yld->ctl_data)));
340         yld->ctl_data->cmd  = CMD_KEYPRESS;
341         yld->ctl_data->size = 1;
342         yld->ctl_data->sum  = 0xff - CMD_KEYPRESS;
343
344         /* If state update pointer wraps do a KEYPRESS first. */
345         if (ix >= sizeof(yld->master)) {
346                 yld->stat_ix = 0;
347                 return 0;
348         }
349
350         /* find update candidates: copy != master */
351         do {
352                 val = yld->master.b[ix];
353                 if (val != yld->copy.b[ix])
354                         goto send_update;
355         } while (++ix < sizeof(yld->master));
356
357         /* nothing todo, wait a bit and poll for a KEYPRESS */
358         yld->stat_ix = 0;
359         /* TODO how can we wait abit. ??
360          * msleep_interruptible(1000 / YEALINK_POLLING_FREQUENCY);
361          */
362         return 0;
363
364 send_update:
365
366         /* Setup an appropriate update request */
367         yld->copy.b[ix] = val;
368         yld->ctl_data->data[0] = val;
369
370         switch(ix) {
371         case offsetof(struct yld_status, led):
372                 yld->ctl_data->cmd      = CMD_LED;
373                 yld->ctl_data->sum      = -1 - CMD_LED - val;
374                 break;
375         case offsetof(struct yld_status, dialtone):
376                 yld->ctl_data->cmd      = CMD_DIALTONE;
377                 yld->ctl_data->sum      = -1 - CMD_DIALTONE - val;
378                 break;
379         case offsetof(struct yld_status, ringtone):
380                 yld->ctl_data->cmd      = CMD_RINGTONE;
381                 yld->ctl_data->sum      = -1 - CMD_RINGTONE - val;
382                 break;
383         case offsetof(struct yld_status, keynum):
384                 val--;
385                 val &= 0x1f;
386                 yld->ctl_data->cmd      = CMD_SCANCODE;
387                 yld->ctl_data->offset   = cpu_to_be16(val);
388                 yld->ctl_data->data[0]  = 0;
389                 yld->ctl_data->sum      = -1 - CMD_SCANCODE - val;
390                 break;
391         default:
392                 len = sizeof(yld->master.s.lcd) - ix;
393                 if (len > sizeof(yld->ctl_data->data))
394                         len = sizeof(yld->ctl_data->data);
395
396                 /* Combine up to <len> consecutive LCD bytes in a singe request
397                  */
398                 yld->ctl_data->cmd      = CMD_LCD;
399                 yld->ctl_data->offset   = cpu_to_be16(ix);
400                 yld->ctl_data->size     = len;
401                 yld->ctl_data->sum      = -CMD_LCD - ix - val - len;
402                 for(i=1; i<len; i++) {
403                         ix++;
404                         val = yld->master.b[ix];
405                         yld->copy.b[ix]         = val;
406                         yld->ctl_data->data[i]  = val;
407                         yld->ctl_data->sum     -= val;
408                 }
409         }
410         yld->stat_ix = ix + 1;
411         return 1;
412 }
413
414 /* Decide on how to handle responses
415  *
416  * The state transition diagram is somethhing like:
417  *
418  *          syncState<--+
419  *               |      |
420  *               |    idle
421  *              \|/     |
422  * init --ok--> waitForKey --ok--> getKey
423  *  ^               ^                |
424  *  |               +-------ok-------+
425  * error,start
426  *
427  */
428 static void urb_irq_callback(struct urb *urb, struct pt_regs *regs)
429 {
430         struct yealink_dev *yld = urb->context;
431         int ret;
432
433         if (urb->status)
434                 err("%s - urb status %d", __FUNCTION__, urb->status);
435
436         switch (yld->irq_data->cmd) {
437         case CMD_KEYPRESS:
438
439                 yld->master.s.keynum = yld->irq_data->data[0];
440                 break;
441
442         case CMD_SCANCODE:
443                 dbg("get scancode %x", yld->irq_data->data[0]);
444
445                 report_key(yld, map_p1k_to_key(yld->irq_data->data[0]), regs);
446                 break;
447
448         default:
449                 err("unexpected response %x", yld->irq_data->cmd);
450         }
451
452         yealink_do_idle_tasks(yld);
453
454         ret = usb_submit_urb(yld->urb_ctl, GFP_ATOMIC);
455         if (ret)
456                 err("%s - usb_submit_urb failed %d", __FUNCTION__, ret);
457 }
458
459 static void urb_ctl_callback(struct urb *urb, struct pt_regs *regs)
460 {
461         struct yealink_dev *yld = urb->context;
462         int ret;
463
464         if (urb->status)
465                 err("%s - urb status %d", __FUNCTION__, urb->status);
466
467         switch (yld->ctl_data->cmd) {
468         case CMD_KEYPRESS:
469         case CMD_SCANCODE:
470                 /* ask for a response */
471                 ret = usb_submit_urb(yld->urb_irq, GFP_ATOMIC);
472                 break;
473         default:
474                 /* send new command */
475                 yealink_do_idle_tasks(yld);
476                 ret = usb_submit_urb(yld->urb_ctl, GFP_ATOMIC);
477         }
478
479         if (ret)
480                 err("%s - usb_submit_urb failed %d", __FUNCTION__, ret);
481 }
482
483 /*******************************************************************************
484  * input event interface
485  ******************************************************************************/
486
487 /* TODO should we issue a ringtone on a SND_BELL event?
488 static int input_ev(struct input_dev *dev, unsigned int type,
489                 unsigned int code, int value)
490 {
491
492         if (type != EV_SND)
493                 return -EINVAL;
494
495         switch (code) {
496         case SND_BELL:
497         case SND_TONE:
498                 break;
499         default:
500                 return -EINVAL;
501         }
502
503         return 0;
504 }
505 */
506
507 static int input_open(struct input_dev *dev)
508 {
509         struct yealink_dev *yld = dev->private;
510         int i, ret;
511
512         dbg("%s", __FUNCTION__);
513
514         /* force updates to device */
515         for (i = 0; i<sizeof(yld->master); i++)
516                 yld->copy.b[i] = ~yld->master.b[i];
517         yld->key_code = -1;     /* no keys pressed */
518
519         yealink_set_ringtone(yld, default_ringtone, sizeof(default_ringtone));
520
521         /* issue INIT */
522         memset(yld->ctl_data, 0, sizeof(*(yld->ctl_data)));
523         yld->ctl_data->cmd      = CMD_INIT;
524         yld->ctl_data->size     = 10;
525         yld->ctl_data->sum      = 0x100-CMD_INIT-10;
526         if ((ret = usb_submit_urb(yld->urb_ctl, GFP_KERNEL)) != 0) {
527                 dbg("%s - usb_submit_urb failed with result %d",
528                      __FUNCTION__, ret);
529                 return ret;
530         }
531         return 0;
532 }
533
534 static void input_close(struct input_dev *dev)
535 {
536         struct yealink_dev *yld = dev->private;
537
538         usb_kill_urb(yld->urb_ctl);
539         usb_kill_urb(yld->urb_irq);
540 }
541
542 /*******************************************************************************
543  * sysfs interface
544  ******************************************************************************/
545
546 static DECLARE_RWSEM(sysfs_rwsema);
547
548 /* Interface to the 7-segments translation table aka. char set.
549  */
550 static ssize_t show_map(struct device *dev, struct device_attribute *attr,
551                                 char *buf)
552 {
553         memcpy(buf, &map_seg7, sizeof(map_seg7));
554         return sizeof(map_seg7);
555 }
556
557 static ssize_t store_map(struct device *dev, struct device_attribute *attr,
558                                 const char *buf, size_t cnt)
559 {
560         if (cnt != sizeof(map_seg7))
561                 return -EINVAL;
562         memcpy(&map_seg7, buf, sizeof(map_seg7));
563         return sizeof(map_seg7);
564 }
565
566 /* Interface to the LCD.
567  */
568
569 /* Reading /sys/../lineX will return the format string with its settings:
570  *
571  * Example:
572  * cat ./line3
573  * 888888888888
574  * Linux Rocks!
575  */
576 static ssize_t show_line(struct device *dev, char *buf, int a, int b)
577 {
578         struct yealink_dev *yld;
579         int i;
580
581         down_read(&sysfs_rwsema);
582         yld = dev_get_drvdata(dev);
583         if (yld == NULL) {
584                 up_read(&sysfs_rwsema);
585                 return -ENODEV;
586         }
587
588         for (i = a; i < b; i++)
589                 *buf++ = lcdMap[i].type;
590         *buf++ = '\n';
591         for (i = a; i < b; i++)
592                 *buf++ = yld->lcdMap[i];
593         *buf++ = '\n';
594         *buf = 0;
595
596         up_read(&sysfs_rwsema);
597         return 3 + ((b - a) << 1);
598 }
599
600 static ssize_t show_line1(struct device *dev, struct device_attribute *attr,
601                         char *buf)
602 {
603         return show_line(dev, buf, LCD_LINE1_OFFSET, LCD_LINE2_OFFSET);
604 }
605
606 static ssize_t show_line2(struct device *dev, struct device_attribute *attr,
607                         char *buf)
608 {
609         return show_line(dev, buf, LCD_LINE2_OFFSET, LCD_LINE3_OFFSET);
610 }
611
612 static ssize_t show_line3(struct device *dev, struct device_attribute *attr,
613                         char *buf)
614 {
615         return show_line(dev, buf, LCD_LINE3_OFFSET, LCD_LINE4_OFFSET);
616 }
617
618 /* Writing to /sys/../lineX will set the coresponding LCD line.
619  * - Excess characters are ignored.
620  * - If less characters are written than allowed, the remaining digits are
621  *   unchanged.
622  * - The '\n' or '\t' char is a placeholder, it does not overwrite the
623  *   original content.
624  */
625 static ssize_t store_line(struct device *dev, const char *buf, size_t count,
626                 int el, size_t len)
627 {
628         struct yealink_dev *yld;
629         int i;
630
631         down_write(&sysfs_rwsema);
632         yld = dev_get_drvdata(dev);
633         if (yld == NULL) {
634                 up_write(&sysfs_rwsema);
635                 return -ENODEV;
636         }
637
638         if (len > count)
639                 len = count;
640         for (i = 0; i < len; i++)
641                 setChar(yld, el++, buf[i]);
642
643         up_write(&sysfs_rwsema);
644         return count;
645 }
646
647 static ssize_t store_line1(struct device *dev, struct device_attribute *attr,
648                                 const char *buf, size_t count)
649 {
650         return store_line(dev, buf, count, LCD_LINE1_OFFSET, LCD_LINE1_SIZE);
651 }
652
653 static ssize_t store_line2(struct device *dev, struct device_attribute *attr,
654                                 const char *buf, size_t count)
655 {
656         return store_line(dev, buf, count, LCD_LINE2_OFFSET, LCD_LINE2_SIZE);
657 }
658
659 static ssize_t store_line3(struct device *dev, struct device_attribute *attr,
660                                 const char *buf, size_t count)
661 {
662         return store_line(dev, buf, count, LCD_LINE3_OFFSET, LCD_LINE3_SIZE);
663 }
664
665 /* Interface to visible and audible "icons", these include:
666  * pictures on the LCD, the LED, and the dialtone signal.
667  */
668
669 /* Get a list of "switchable elements" with their current state. */
670 static ssize_t get_icons(struct device *dev, struct device_attribute *attr,
671                         char *buf)
672 {
673         struct yealink_dev *yld;
674         int i, ret = 1;
675
676         down_read(&sysfs_rwsema);
677         yld = dev_get_drvdata(dev);
678         if (yld == NULL) {
679                 up_read(&sysfs_rwsema);
680                 return -ENODEV;
681         }
682
683         for (i = 0; i < ARRAY_SIZE(lcdMap); i++) {
684                 if (lcdMap[i].type != '.')
685                         continue;
686                 ret += sprintf(&buf[ret], "%s %s\n",
687                                 yld->lcdMap[i] == ' ' ? "  " : "on",
688                                 lcdMap[i].u.p.name);
689         }
690         up_read(&sysfs_rwsema);
691         return ret;
692 }
693
694 /* Change the visibility of a particular element. */
695 static ssize_t set_icon(struct device *dev, const char *buf, size_t count,
696                         int chr)
697 {
698         struct yealink_dev *yld;
699         int i;
700
701         down_write(&sysfs_rwsema);
702         yld = dev_get_drvdata(dev);
703         if (yld == NULL) {
704                 up_write(&sysfs_rwsema);
705                 return -ENODEV;
706         }
707
708         for (i = 0; i < ARRAY_SIZE(lcdMap); i++) {
709                 if (lcdMap[i].type != '.')
710                         continue;
711                 if (strncmp(buf, lcdMap[i].u.p.name, count) == 0) {
712                         setChar(yld, i, chr);
713                         break;
714                 }
715         }
716
717         up_write(&sysfs_rwsema);
718         return count;
719 }
720
721 static ssize_t show_icon(struct device *dev, struct device_attribute *attr,
722                 const char *buf, size_t count)
723 {
724         return set_icon(dev, buf, count, buf[0]);
725 }
726
727 static ssize_t hide_icon(struct device *dev, struct device_attribute *attr,
728                 const char *buf, size_t count)
729 {
730         return set_icon(dev, buf, count, ' ');
731 }
732
733 /* Upload a ringtone to the device.
734  */
735
736 /* Stores raw ringtone data in the phone */
737 static ssize_t store_ringtone(struct device *dev,
738                 struct device_attribute *attr,
739                 const char *buf, size_t count)
740 {
741         struct yealink_dev *yld;
742
743         down_write(&sysfs_rwsema);
744         yld = dev_get_drvdata(dev);
745         if (yld == NULL) {
746                 up_write(&sysfs_rwsema);
747                 return -ENODEV;
748         }
749
750         /* TODO locking with async usb control interface??? */
751         yealink_set_ringtone(yld, (char *)buf, count);
752         up_write(&sysfs_rwsema);
753         return count;
754 }
755
756 #define _M444   S_IRUGO
757 #define _M664   S_IRUGO|S_IWUSR|S_IWGRP
758 #define _M220   S_IWUSR|S_IWGRP
759
760 static DEVICE_ATTR(map_seg7     , _M664, show_map       , store_map     );
761 static DEVICE_ATTR(line1        , _M664, show_line1     , store_line1   );
762 static DEVICE_ATTR(line2        , _M664, show_line2     , store_line2   );
763 static DEVICE_ATTR(line3        , _M664, show_line3     , store_line3   );
764 static DEVICE_ATTR(get_icons    , _M444, get_icons      , NULL          );
765 static DEVICE_ATTR(show_icon    , _M220, NULL           , show_icon     );
766 static DEVICE_ATTR(hide_icon    , _M220, NULL           , hide_icon     );
767 static DEVICE_ATTR(ringtone     , _M220, NULL           , store_ringtone);
768
769 static struct attribute *yld_attributes[] = {
770         &dev_attr_line1.attr,
771         &dev_attr_line2.attr,
772         &dev_attr_line3.attr,
773         &dev_attr_get_icons.attr,
774         &dev_attr_show_icon.attr,
775         &dev_attr_hide_icon.attr,
776         &dev_attr_map_seg7.attr,
777         &dev_attr_ringtone.attr,
778         NULL
779 };
780
781 static struct attribute_group yld_attr_group = {
782         .attrs = yld_attributes
783 };
784
785 /*******************************************************************************
786  * Linux interface and usb initialisation
787  ******************************************************************************/
788
789 static const struct yld_device {
790         u16 idVendor;
791         u16 idProduct;
792         char *name;
793 } yld_device[] = {
794         { 0x6993, 0xb001, "Yealink usb-p1k" },
795 };
796
797 static struct usb_device_id usb_table [] = {
798         { USB_INTERFACE_INFO(USB_CLASS_HID, 0, 0) },
799         { }
800 };
801
802 static int usb_cleanup(struct yealink_dev *yld, int err)
803 {
804         if (yld == NULL)
805                 return err;
806
807         if (yld->urb_irq) {
808                 usb_kill_urb(yld->urb_irq);
809                 usb_free_urb(yld->urb_irq);
810         }
811         if (yld->urb_ctl)
812                 usb_free_urb(yld->urb_ctl);
813         if (yld->idev) {
814                 if (err)
815                         input_free_device(yld->idev);
816                 else
817                         input_unregister_device(yld->idev);
818         }
819         if (yld->ctl_req)
820                 usb_buffer_free(yld->udev, sizeof(*(yld->ctl_req)),
821                                 yld->ctl_req, yld->ctl_req_dma);
822         if (yld->ctl_data)
823                 usb_buffer_free(yld->udev, USB_PKT_LEN,
824                                 yld->ctl_data, yld->ctl_dma);
825         if (yld->irq_data)
826                 usb_buffer_free(yld->udev, USB_PKT_LEN,
827                                 yld->irq_data, yld->irq_dma);
828         kfree(yld);
829         return err;
830 }
831
832 static void usb_disconnect(struct usb_interface *intf)
833 {
834         struct yealink_dev *yld;
835
836         down_write(&sysfs_rwsema);
837         yld = usb_get_intfdata(intf);
838         sysfs_remove_group(&intf->dev.kobj, &yld_attr_group);
839         usb_set_intfdata(intf, NULL);
840         up_write(&sysfs_rwsema);
841
842         usb_cleanup(yld, 0);
843 }
844
845 static int usb_match(struct usb_device *udev)
846 {
847         int i;
848         u16 idVendor = le16_to_cpu(udev->descriptor.idVendor);
849         u16 idProduct = le16_to_cpu(udev->descriptor.idProduct);
850
851         for (i = 0; i < ARRAY_SIZE(yld_device); i++) {
852                 if ((idVendor == yld_device[i].idVendor) &&
853                     (idProduct == yld_device[i].idProduct))
854                         return i;
855         }
856         return -ENODEV;
857 }
858
859 static int usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
860 {
861         struct usb_device *udev = interface_to_usbdev (intf);
862         struct usb_host_interface *interface;
863         struct usb_endpoint_descriptor *endpoint;
864         struct yealink_dev *yld;
865         struct input_dev *input_dev;
866         int ret, pipe, i;
867
868         i = usb_match(udev);
869         if (i < 0)
870                 return -ENODEV;
871
872         interface = intf->cur_altsetting;
873         endpoint = &interface->endpoint[0].desc;
874         if (!(endpoint->bEndpointAddress & USB_DIR_IN))
875                 return -EIO;
876         if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
877                 return -EIO;
878
879         yld = kzalloc(sizeof(struct yealink_dev), GFP_KERNEL);
880         if (!yld)
881                 return -ENOMEM;
882
883         yld->udev = udev;
884
885         yld->idev = input_dev = input_allocate_device();
886         if (!input_dev)
887                 return usb_cleanup(yld, -ENOMEM);
888
889         /* allocate usb buffers */
890         yld->irq_data = usb_buffer_alloc(udev, USB_PKT_LEN,
891                                         SLAB_ATOMIC, &yld->irq_dma);
892         if (yld->irq_data == NULL)
893                 return usb_cleanup(yld, -ENOMEM);
894
895         yld->ctl_data = usb_buffer_alloc(udev, USB_PKT_LEN,
896                                         SLAB_ATOMIC, &yld->ctl_dma);
897         if (!yld->ctl_data)
898                 return usb_cleanup(yld, -ENOMEM);
899
900         yld->ctl_req = usb_buffer_alloc(udev, sizeof(*(yld->ctl_req)),
901                                         SLAB_ATOMIC, &yld->ctl_req_dma);
902         if (yld->ctl_req == NULL)
903                 return usb_cleanup(yld, -ENOMEM);
904
905         /* allocate urb structures */
906         yld->urb_irq = usb_alloc_urb(0, GFP_KERNEL);
907         if (yld->urb_irq == NULL)
908                 return usb_cleanup(yld, -ENOMEM);
909
910         yld->urb_ctl = usb_alloc_urb(0, GFP_KERNEL);
911         if (yld->urb_ctl == NULL)
912                 return usb_cleanup(yld, -ENOMEM);
913
914         /* get a handle to the interrupt data pipe */
915         pipe = usb_rcvintpipe(udev, endpoint->bEndpointAddress);
916         ret = usb_maxpacket(udev, pipe, usb_pipeout(pipe));
917         if (ret != USB_PKT_LEN)
918                 err("invalid payload size %d, expected %d", ret, USB_PKT_LEN);
919
920         /* initialise irq urb */
921         usb_fill_int_urb(yld->urb_irq, udev, pipe, yld->irq_data,
922                         USB_PKT_LEN,
923                         urb_irq_callback,
924                         yld, endpoint->bInterval);
925         yld->urb_irq->transfer_dma = yld->irq_dma;
926         yld->urb_irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
927         yld->urb_irq->dev = udev;
928
929         /* initialise ctl urb */
930         yld->ctl_req->bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE |
931                                       USB_DIR_OUT;
932         yld->ctl_req->bRequest  = USB_REQ_SET_CONFIGURATION;
933         yld->ctl_req->wValue    = cpu_to_le16(0x200);
934         yld->ctl_req->wIndex    = cpu_to_le16(interface->desc.bInterfaceNumber);
935         yld->ctl_req->wLength   = cpu_to_le16(USB_PKT_LEN);
936
937         usb_fill_control_urb(yld->urb_ctl, udev, usb_sndctrlpipe(udev, 0),
938                         (void *)yld->ctl_req, yld->ctl_data, USB_PKT_LEN,
939                         urb_ctl_callback, yld);
940         yld->urb_ctl->setup_dma = yld->ctl_req_dma;
941         yld->urb_ctl->transfer_dma      = yld->ctl_dma;
942         yld->urb_ctl->transfer_flags    |= URB_NO_SETUP_DMA_MAP |
943                                         URB_NO_TRANSFER_DMA_MAP;
944         yld->urb_ctl->dev = udev;
945
946         /* find out the physical bus location */
947         usb_make_path(udev, yld->phys, sizeof(yld->phys));
948         strlcat(yld->phys,  "/input0", sizeof(yld->phys));
949
950         /* register settings for the input device */
951         input_dev->name = yld_device[i].name;
952         input_dev->phys = yld->phys;
953         usb_to_input_id(udev, &input_dev->id);
954         input_dev->cdev.dev = &intf->dev;
955
956         input_dev->private = yld;
957         input_dev->open = input_open;
958         input_dev->close = input_close;
959         /* input_dev->event = input_ev; TODO */
960
961         /* register available key events */
962         input_dev->evbit[0] = BIT(EV_KEY);
963         for (i = 0; i < 256; i++) {
964                 int k = map_p1k_to_key(i);
965                 if (k >= 0) {
966                         set_bit(k & 0xff, input_dev->keybit);
967                         if (k >> 8)
968                                 set_bit(k >> 8, input_dev->keybit);
969                 }
970         }
971
972         input_register_device(yld->idev);
973
974         usb_set_intfdata(intf, yld);
975
976         /* clear visible elements */
977         for (i = 0; i < ARRAY_SIZE(lcdMap); i++)
978                 setChar(yld, i, ' ');
979
980         /* display driver version on LCD line 3 */
981         store_line3(&intf->dev, NULL,
982                         DRIVER_VERSION, sizeof(DRIVER_VERSION));
983
984         /* Register sysfs hooks (don't care about failure) */
985         sysfs_create_group(&intf->dev.kobj, &yld_attr_group);
986         return 0;
987 }
988
989 static struct usb_driver yealink_driver = {
990         .owner          = THIS_MODULE,
991         .name           = "yealink",
992         .probe          = usb_probe,
993         .disconnect     = usb_disconnect,
994         .id_table       = usb_table,
995 };
996
997 static int __init yealink_dev_init(void)
998 {
999         int ret = usb_register(&yealink_driver);
1000         if (ret == 0)
1001                 info(DRIVER_DESC ":" DRIVER_VERSION);
1002         return ret;
1003 }
1004
1005 static void __exit yealink_dev_exit(void)
1006 {
1007         usb_deregister(&yealink_driver);
1008 }
1009
1010 module_init(yealink_dev_init);
1011 module_exit(yealink_dev_exit);
1012
1013 MODULE_DEVICE_TABLE (usb, usb_table);
1014
1015 MODULE_AUTHOR(DRIVER_AUTHOR);
1016 MODULE_DESCRIPTION(DRIVER_DESC);
1017 MODULE_LICENSE("GPL");