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