blob: 1365cd94ed9b17d6da99a5ede442930552892135 [file] [log] [blame]
Henkaca951a2005-08-16 16:17:43 +02001/*
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
Dmitry Torokhovba0acb52007-05-07 17:31:32 -040032 * - the xpad-driver drivers/input/joystick/xpad.c
Henkaca951a2005-08-16 16:17:43 +020033 *
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
Henkaca951a2005-08-16 16:17:43 +020049#include <linux/kernel.h>
Henkaca951a2005-08-16 16:17:43 +020050#include <linux/slab.h>
51#include <linux/module.h>
52#include <linux/rwsem.h>
David Brownellae0dadc2006-06-13 10:04:34 -070053#include <linux/usb/input.h>
Atsushi Nemotof9da8d12008-10-10 23:14:14 -040054#include <linux/map_to_7segment.h>
Henkaca951a2005-08-16 16:17:43 +020055
Henkaca951a2005-08-16 16:17:43 +020056#include "yealink.h"
57
Henk8e2ce4f2005-12-30 19:41:11 +010058#define DRIVER_VERSION "yld-20051230"
Henkaca951a2005-08-16 16:17:43 +020059
60#define YEALINK_POLLING_FREQUENCY 10 /* in [Hz] */
61
62struct yld_status {
63 u8 lcd[24];
64 u8 led;
65 u8 dialtone;
66 u8 ringtone;
67 u8 keynum;
68} __attribute__ ((packed));
69
70/*
71 * Register the LCD segment and icon map
72 */
73#define _LOC(k,l) { .a = (k), .m = (l) }
74#define _SEG(t, a, am, b, bm, c, cm, d, dm, e, em, f, fm, g, gm) \
75 { .type = (t), \
76 .u = { .s = { _LOC(a, am), _LOC(b, bm), _LOC(c, cm), \
77 _LOC(d, dm), _LOC(e, em), _LOC(g, gm), \
78 _LOC(f, fm) } } }
79#define _PIC(t, h, hm, n) \
80 { .type = (t), \
81 .u = { .p = { .name = (n), .a = (h), .m = (hm) } } }
82
83static const struct lcd_segment_map {
84 char type;
85 union {
86 struct pictogram_map {
87 u8 a,m;
88 char name[10];
89 } p;
90 struct segment_map {
91 u8 a,m;
92 } s[7];
93 } u;
94} lcdMap[] = {
95#include "yealink.h"
96};
97
98struct yealink_dev {
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -050099 struct input_dev *idev; /* input device */
Henkaca951a2005-08-16 16:17:43 +0200100 struct usb_device *udev; /* usb device */
Greg Kroah-Hartman73e66ce2012-05-04 15:33:01 -0700101 struct usb_interface *intf; /* usb interface */
Henkaca951a2005-08-16 16:17:43 +0200102
103 /* irq input channel */
104 struct yld_ctl_packet *irq_data;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500105 dma_addr_t irq_dma;
Henkaca951a2005-08-16 16:17:43 +0200106 struct urb *urb_irq;
107
108 /* control output channel */
109 struct yld_ctl_packet *ctl_data;
110 dma_addr_t ctl_dma;
111 struct usb_ctrlrequest *ctl_req;
Henkaca951a2005-08-16 16:17:43 +0200112 struct urb *urb_ctl;
113
114 char phys[64]; /* physical device path */
115
116 u8 lcdMap[ARRAY_SIZE(lcdMap)]; /* state of LCD, LED ... */
117 int key_code; /* last reported key */
118
Oliver Neukumb4ecda32008-07-03 12:02:03 -0400119 unsigned int shutdown:1;
120
Henkaca951a2005-08-16 16:17:43 +0200121 int stat_ix;
122 union {
123 struct yld_status s;
124 u8 b[sizeof(struct yld_status)];
125 } master, copy;
126};
127
128
129/*******************************************************************************
130 * Yealink lcd interface
131 ******************************************************************************/
132
133/*
134 * Register a default 7 segment character set
135 */
136static SEG7_DEFAULT_MAP(map_seg7);
137
138 /* Display a char,
139 * char '\9' and '\n' are placeholders and do not overwrite the original text.
140 * A space will always hide an icon.
141 */
142static int setChar(struct yealink_dev *yld, int el, int chr)
143{
144 int i, a, m, val;
145
146 if (el >= ARRAY_SIZE(lcdMap))
147 return -EINVAL;
148
149 if (chr == '\t' || chr == '\n')
150 return 0;
151
152 yld->lcdMap[el] = chr;
153
154 if (lcdMap[el].type == '.') {
155 a = lcdMap[el].u.p.a;
156 m = lcdMap[el].u.p.m;
157 if (chr != ' ')
158 yld->master.b[a] |= m;
159 else
160 yld->master.b[a] &= ~m;
161 return 0;
162 }
163
164 val = map_to_seg7(&map_seg7, chr);
165 for (i = 0; i < ARRAY_SIZE(lcdMap[0].u.s); i++) {
166 m = lcdMap[el].u.s[i].m;
167
168 if (m == 0)
169 continue;
170
171 a = lcdMap[el].u.s[i].a;
172 if (val & 1)
173 yld->master.b[a] |= m;
174 else
175 yld->master.b[a] &= ~m;
176 val = val >> 1;
177 }
178 return 0;
179};
180
181/*******************************************************************************
182 * Yealink key interface
183 ******************************************************************************/
184
185/* Map device buttons to internal key events.
186 *
187 * USB-P1K button layout:
188 *
189 * up
190 * IN OUT
191 * down
192 *
193 * pickup C hangup
194 * 1 2 3
195 * 4 5 6
196 * 7 8 9
197 * * 0 #
198 *
199 * The "up" and "down" keys, are symbolised by arrows on the button.
200 * The "pickup" and "hangup" keys are symbolised by a green and red phone
201 * on the button.
202 */
203static int map_p1k_to_key(int scancode)
204{
205 switch(scancode) { /* phone key: */
206 case 0x23: return KEY_LEFT; /* IN */
207 case 0x33: return KEY_UP; /* up */
208 case 0x04: return KEY_RIGHT; /* OUT */
209 case 0x24: return KEY_DOWN; /* down */
210 case 0x03: return KEY_ENTER; /* pickup */
211 case 0x14: return KEY_BACKSPACE; /* C */
212 case 0x13: return KEY_ESC; /* hangup */
213 case 0x00: return KEY_1; /* 1 */
214 case 0x01: return KEY_2; /* 2 */
215 case 0x02: return KEY_3; /* 3 */
216 case 0x10: return KEY_4; /* 4 */
217 case 0x11: return KEY_5; /* 5 */
218 case 0x12: return KEY_6; /* 6 */
219 case 0x20: return KEY_7; /* 7 */
220 case 0x21: return KEY_8; /* 8 */
221 case 0x22: return KEY_9; /* 9 */
222 case 0x30: return KEY_KPASTERISK; /* * */
223 case 0x31: return KEY_0; /* 0 */
224 case 0x32: return KEY_LEFTSHIFT |
225 KEY_3 << 8; /* # */
226 }
227 return -EINVAL;
228}
229
230/* Completes a request by converting the data into events for the
231 * input subsystem.
232 *
233 * The key parameter can be cascaded: key2 << 8 | key1
234 */
David Howells7d12e782006-10-05 14:55:46 +0100235static void report_key(struct yealink_dev *yld, int key)
Henkaca951a2005-08-16 16:17:43 +0200236{
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500237 struct input_dev *idev = yld->idev;
Henkaca951a2005-08-16 16:17:43 +0200238
Henkaca951a2005-08-16 16:17:43 +0200239 if (yld->key_code >= 0) {
240 /* old key up */
241 input_report_key(idev, yld->key_code & 0xff, 0);
242 if (yld->key_code >> 8)
243 input_report_key(idev, yld->key_code >> 8, 0);
244 }
245
246 yld->key_code = key;
247 if (key >= 0) {
248 /* new valid key */
249 input_report_key(idev, key & 0xff, 1);
250 if (key >> 8)
251 input_report_key(idev, key >> 8, 1);
252 }
253 input_sync(idev);
254}
255
256/*******************************************************************************
257 * Yealink usb communication interface
258 ******************************************************************************/
259
260static int yealink_cmd(struct yealink_dev *yld, struct yld_ctl_packet *p)
261{
262 u8 *buf = (u8 *)p;
263 int i;
264 u8 sum = 0;
265
266 for(i=0; i<USB_PKT_LEN-1; i++)
267 sum -= buf[i];
268 p->sum = sum;
269 return usb_control_msg(yld->udev,
270 usb_sndctrlpipe(yld->udev, 0),
271 USB_REQ_SET_CONFIGURATION,
272 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
273 0x200, 3,
274 p, sizeof(*p),
275 USB_CTRL_SET_TIMEOUT);
276}
277
278static u8 default_ringtone[] = {
279 0xEF, /* volume [0-255] */
280 0xFB, 0x1E, 0x00, 0x0C, /* 1250 [hz], 12/100 [s] */
281 0xFC, 0x18, 0x00, 0x0C, /* 1000 [hz], 12/100 [s] */
282 0xFB, 0x1E, 0x00, 0x0C,
283 0xFC, 0x18, 0x00, 0x0C,
284 0xFB, 0x1E, 0x00, 0x0C,
285 0xFC, 0x18, 0x00, 0x0C,
286 0xFB, 0x1E, 0x00, 0x0C,
287 0xFC, 0x18, 0x00, 0x0C,
288 0xFF, 0xFF, 0x01, 0x90, /* silent, 400/100 [s] */
289 0x00, 0x00 /* end of sequence */
290};
291
292static int yealink_set_ringtone(struct yealink_dev *yld, u8 *buf, size_t size)
293{
294 struct yld_ctl_packet *p = yld->ctl_data;
295 int ix, len;
296
297 if (size <= 0)
298 return -EINVAL;
299
300 /* Set the ringtone volume */
301 memset(yld->ctl_data, 0, sizeof(*(yld->ctl_data)));
302 yld->ctl_data->cmd = CMD_RING_VOLUME;
303 yld->ctl_data->size = 1;
304 yld->ctl_data->data[0] = buf[0];
305 yealink_cmd(yld, p);
306
307 buf++;
308 size--;
309
310 p->cmd = CMD_RING_NOTE;
311 ix = 0;
312 while (size != ix) {
313 len = size - ix;
314 if (len > sizeof(p->data))
315 len = sizeof(p->data);
316 p->size = len;
Henkb71e3182005-08-17 10:40:26 +0200317 p->offset = cpu_to_be16(ix);
Henkaca951a2005-08-16 16:17:43 +0200318 memcpy(p->data, &buf[ix], len);
319 yealink_cmd(yld, p);
320 ix += len;
321 }
322 return 0;
323}
324
325/* keep stat_master & stat_copy in sync.
326 */
327static int yealink_do_idle_tasks(struct yealink_dev *yld)
328{
329 u8 val;
330 int i, ix, len;
331
332 ix = yld->stat_ix;
333
334 memset(yld->ctl_data, 0, sizeof(*(yld->ctl_data)));
335 yld->ctl_data->cmd = CMD_KEYPRESS;
336 yld->ctl_data->size = 1;
337 yld->ctl_data->sum = 0xff - CMD_KEYPRESS;
338
339 /* If state update pointer wraps do a KEYPRESS first. */
340 if (ix >= sizeof(yld->master)) {
341 yld->stat_ix = 0;
342 return 0;
343 }
344
345 /* find update candidates: copy != master */
346 do {
347 val = yld->master.b[ix];
348 if (val != yld->copy.b[ix])
349 goto send_update;
350 } while (++ix < sizeof(yld->master));
351
352 /* nothing todo, wait a bit and poll for a KEYPRESS */
353 yld->stat_ix = 0;
354 /* TODO how can we wait abit. ??
355 * msleep_interruptible(1000 / YEALINK_POLLING_FREQUENCY);
356 */
357 return 0;
358
359send_update:
360
361 /* Setup an appropriate update request */
362 yld->copy.b[ix] = val;
363 yld->ctl_data->data[0] = val;
364
365 switch(ix) {
366 case offsetof(struct yld_status, led):
367 yld->ctl_data->cmd = CMD_LED;
368 yld->ctl_data->sum = -1 - CMD_LED - val;
369 break;
370 case offsetof(struct yld_status, dialtone):
371 yld->ctl_data->cmd = CMD_DIALTONE;
372 yld->ctl_data->sum = -1 - CMD_DIALTONE - val;
373 break;
374 case offsetof(struct yld_status, ringtone):
375 yld->ctl_data->cmd = CMD_RINGTONE;
376 yld->ctl_data->sum = -1 - CMD_RINGTONE - val;
377 break;
378 case offsetof(struct yld_status, keynum):
379 val--;
380 val &= 0x1f;
381 yld->ctl_data->cmd = CMD_SCANCODE;
Henkb71e3182005-08-17 10:40:26 +0200382 yld->ctl_data->offset = cpu_to_be16(val);
Henkaca951a2005-08-16 16:17:43 +0200383 yld->ctl_data->data[0] = 0;
384 yld->ctl_data->sum = -1 - CMD_SCANCODE - val;
385 break;
386 default:
387 len = sizeof(yld->master.s.lcd) - ix;
388 if (len > sizeof(yld->ctl_data->data))
389 len = sizeof(yld->ctl_data->data);
390
391 /* Combine up to <len> consecutive LCD bytes in a singe request
392 */
393 yld->ctl_data->cmd = CMD_LCD;
Henkb71e3182005-08-17 10:40:26 +0200394 yld->ctl_data->offset = cpu_to_be16(ix);
Henkaca951a2005-08-16 16:17:43 +0200395 yld->ctl_data->size = len;
396 yld->ctl_data->sum = -CMD_LCD - ix - val - len;
397 for(i=1; i<len; i++) {
398 ix++;
399 val = yld->master.b[ix];
400 yld->copy.b[ix] = val;
401 yld->ctl_data->data[i] = val;
402 yld->ctl_data->sum -= val;
403 }
404 }
405 yld->stat_ix = ix + 1;
406 return 1;
407}
408
409/* Decide on how to handle responses
410 *
411 * The state transition diagram is somethhing like:
412 *
413 * syncState<--+
414 * | |
415 * | idle
416 * \|/ |
417 * init --ok--> waitForKey --ok--> getKey
418 * ^ ^ |
419 * | +-------ok-------+
420 * error,start
421 *
422 */
David Howells7d12e782006-10-05 14:55:46 +0100423static void urb_irq_callback(struct urb *urb)
Henkaca951a2005-08-16 16:17:43 +0200424{
425 struct yealink_dev *yld = urb->context;
Oliver Neukumb4ecda32008-07-03 12:02:03 -0400426 int ret, status = urb->status;
Henkaca951a2005-08-16 16:17:43 +0200427
Oliver Neukumb4ecda32008-07-03 12:02:03 -0400428 if (status)
Greg Kroah-Hartman73e66ce2012-05-04 15:33:01 -0700429 dev_err(&yld->intf->dev, "%s - urb status %d\n",
Greg Kroah-Hartman08813d32012-04-25 14:48:31 -0700430 __func__, status);
Henkaca951a2005-08-16 16:17:43 +0200431
432 switch (yld->irq_data->cmd) {
433 case CMD_KEYPRESS:
434
435 yld->master.s.keynum = yld->irq_data->data[0];
436 break;
437
438 case CMD_SCANCODE:
Greg Kroah-Hartman73e66ce2012-05-04 15:33:01 -0700439 dev_dbg(&yld->intf->dev, "get scancode %x\n",
Greg Kroah-Hartman938476f2012-05-01 21:33:03 -0700440 yld->irq_data->data[0]);
Henkaca951a2005-08-16 16:17:43 +0200441
David Howells7d12e782006-10-05 14:55:46 +0100442 report_key(yld, map_p1k_to_key(yld->irq_data->data[0]));
Henkaca951a2005-08-16 16:17:43 +0200443 break;
444
445 default:
Greg Kroah-Hartman73e66ce2012-05-04 15:33:01 -0700446 dev_err(&yld->intf->dev, "unexpected response %x\n",
Greg Kroah-Hartman08813d32012-04-25 14:48:31 -0700447 yld->irq_data->cmd);
Henkaca951a2005-08-16 16:17:43 +0200448 }
449
450 yealink_do_idle_tasks(yld);
451
Oliver Neukumb4ecda32008-07-03 12:02:03 -0400452 if (!yld->shutdown) {
453 ret = usb_submit_urb(yld->urb_ctl, GFP_ATOMIC);
454 if (ret && ret != -EPERM)
Greg Kroah-Hartman73e66ce2012-05-04 15:33:01 -0700455 dev_err(&yld->intf->dev,
Greg Kroah-Hartman08813d32012-04-25 14:48:31 -0700456 "%s - usb_submit_urb failed %d\n",
457 __func__, ret);
Oliver Neukumb4ecda32008-07-03 12:02:03 -0400458 }
Henkaca951a2005-08-16 16:17:43 +0200459}
460
David Howells7d12e782006-10-05 14:55:46 +0100461static void urb_ctl_callback(struct urb *urb)
Henkaca951a2005-08-16 16:17:43 +0200462{
463 struct yealink_dev *yld = urb->context;
Oliver Neukumb4ecda32008-07-03 12:02:03 -0400464 int ret = 0, status = urb->status;
Henkaca951a2005-08-16 16:17:43 +0200465
Oliver Neukumb4ecda32008-07-03 12:02:03 -0400466 if (status)
Greg Kroah-Hartman73e66ce2012-05-04 15:33:01 -0700467 dev_err(&yld->intf->dev, "%s - urb status %d\n",
Greg Kroah-Hartman08813d32012-04-25 14:48:31 -0700468 __func__, status);
Henkaca951a2005-08-16 16:17:43 +0200469
470 switch (yld->ctl_data->cmd) {
471 case CMD_KEYPRESS:
472 case CMD_SCANCODE:
473 /* ask for a response */
Oliver Neukumb4ecda32008-07-03 12:02:03 -0400474 if (!yld->shutdown)
475 ret = usb_submit_urb(yld->urb_irq, GFP_ATOMIC);
Henkaca951a2005-08-16 16:17:43 +0200476 break;
477 default:
478 /* send new command */
479 yealink_do_idle_tasks(yld);
Oliver Neukumb4ecda32008-07-03 12:02:03 -0400480 if (!yld->shutdown)
481 ret = usb_submit_urb(yld->urb_ctl, GFP_ATOMIC);
482 break;
Henkaca951a2005-08-16 16:17:43 +0200483 }
484
Oliver Neukumb4ecda32008-07-03 12:02:03 -0400485 if (ret && ret != -EPERM)
Greg Kroah-Hartman73e66ce2012-05-04 15:33:01 -0700486 dev_err(&yld->intf->dev, "%s - usb_submit_urb failed %d\n",
Greg Kroah-Hartman08813d32012-04-25 14:48:31 -0700487 __func__, ret);
Henkaca951a2005-08-16 16:17:43 +0200488}
489
490/*******************************************************************************
491 * input event interface
492 ******************************************************************************/
493
494/* TODO should we issue a ringtone on a SND_BELL event?
495static int input_ev(struct input_dev *dev, unsigned int type,
496 unsigned int code, int value)
497{
498
499 if (type != EV_SND)
500 return -EINVAL;
501
502 switch (code) {
503 case SND_BELL:
504 case SND_TONE:
505 break;
506 default:
507 return -EINVAL;
508 }
509
510 return 0;
511}
512*/
513
514static int input_open(struct input_dev *dev)
515{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400516 struct yealink_dev *yld = input_get_drvdata(dev);
Henkaca951a2005-08-16 16:17:43 +0200517 int i, ret;
518
Greg Kroah-Hartman73e66ce2012-05-04 15:33:01 -0700519 dev_dbg(&yld->intf->dev, "%s\n", __func__);
Henkaca951a2005-08-16 16:17:43 +0200520
521 /* force updates to device */
522 for (i = 0; i<sizeof(yld->master); i++)
523 yld->copy.b[i] = ~yld->master.b[i];
524 yld->key_code = -1; /* no keys pressed */
525
526 yealink_set_ringtone(yld, default_ringtone, sizeof(default_ringtone));
527
528 /* issue INIT */
529 memset(yld->ctl_data, 0, sizeof(*(yld->ctl_data)));
530 yld->ctl_data->cmd = CMD_INIT;
531 yld->ctl_data->size = 10;
532 yld->ctl_data->sum = 0x100-CMD_INIT-10;
533 if ((ret = usb_submit_urb(yld->urb_ctl, GFP_KERNEL)) != 0) {
Greg Kroah-Hartman73e66ce2012-05-04 15:33:01 -0700534 dev_dbg(&yld->intf->dev,
Greg Kroah-Hartman938476f2012-05-01 21:33:03 -0700535 "%s - usb_submit_urb failed with result %d\n",
536 __func__, ret);
Henkaca951a2005-08-16 16:17:43 +0200537 return ret;
538 }
539 return 0;
540}
541
542static void input_close(struct input_dev *dev)
543{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400544 struct yealink_dev *yld = input_get_drvdata(dev);
Henkaca951a2005-08-16 16:17:43 +0200545
Oliver Neukumb4ecda32008-07-03 12:02:03 -0400546 yld->shutdown = 1;
547 /*
548 * Make sure the flag is seen by other CPUs before we start
549 * killing URBs so new URBs won't be submitted
550 */
551 smp_wmb();
552
Henkaca951a2005-08-16 16:17:43 +0200553 usb_kill_urb(yld->urb_ctl);
554 usb_kill_urb(yld->urb_irq);
Oliver Neukumb4ecda32008-07-03 12:02:03 -0400555
556 yld->shutdown = 0;
557 smp_wmb();
Henkaca951a2005-08-16 16:17:43 +0200558}
559
560/*******************************************************************************
561 * sysfs interface
562 ******************************************************************************/
563
564static DECLARE_RWSEM(sysfs_rwsema);
565
566/* Interface to the 7-segments translation table aka. char set.
567 */
568static ssize_t show_map(struct device *dev, struct device_attribute *attr,
569 char *buf)
570{
571 memcpy(buf, &map_seg7, sizeof(map_seg7));
572 return sizeof(map_seg7);
573}
574
575static ssize_t store_map(struct device *dev, struct device_attribute *attr,
576 const char *buf, size_t cnt)
577{
578 if (cnt != sizeof(map_seg7))
579 return -EINVAL;
580 memcpy(&map_seg7, buf, sizeof(map_seg7));
581 return sizeof(map_seg7);
582}
583
584/* Interface to the LCD.
585 */
586
587/* Reading /sys/../lineX will return the format string with its settings:
588 *
589 * Example:
590 * cat ./line3
591 * 888888888888
592 * Linux Rocks!
593 */
594static ssize_t show_line(struct device *dev, char *buf, int a, int b)
595{
596 struct yealink_dev *yld;
597 int i;
598
599 down_read(&sysfs_rwsema);
600 yld = dev_get_drvdata(dev);
601 if (yld == NULL) {
602 up_read(&sysfs_rwsema);
603 return -ENODEV;
604 }
605
606 for (i = a; i < b; i++)
607 *buf++ = lcdMap[i].type;
608 *buf++ = '\n';
609 for (i = a; i < b; i++)
610 *buf++ = yld->lcdMap[i];
611 *buf++ = '\n';
612 *buf = 0;
613
614 up_read(&sysfs_rwsema);
615 return 3 + ((b - a) << 1);
616}
617
618static ssize_t show_line1(struct device *dev, struct device_attribute *attr,
619 char *buf)
620{
621 return show_line(dev, buf, LCD_LINE1_OFFSET, LCD_LINE2_OFFSET);
622}
623
624static ssize_t show_line2(struct device *dev, struct device_attribute *attr,
625 char *buf)
626{
627 return show_line(dev, buf, LCD_LINE2_OFFSET, LCD_LINE3_OFFSET);
628}
629
630static ssize_t show_line3(struct device *dev, struct device_attribute *attr,
631 char *buf)
632{
633 return show_line(dev, buf, LCD_LINE3_OFFSET, LCD_LINE4_OFFSET);
634}
635
636/* Writing to /sys/../lineX will set the coresponding LCD line.
637 * - Excess characters are ignored.
638 * - If less characters are written than allowed, the remaining digits are
639 * unchanged.
640 * - The '\n' or '\t' char is a placeholder, it does not overwrite the
641 * original content.
642 */
643static ssize_t store_line(struct device *dev, const char *buf, size_t count,
644 int el, size_t len)
645{
646 struct yealink_dev *yld;
647 int i;
648
649 down_write(&sysfs_rwsema);
650 yld = dev_get_drvdata(dev);
651 if (yld == NULL) {
652 up_write(&sysfs_rwsema);
653 return -ENODEV;
654 }
655
656 if (len > count)
657 len = count;
658 for (i = 0; i < len; i++)
659 setChar(yld, el++, buf[i]);
660
661 up_write(&sysfs_rwsema);
662 return count;
663}
664
665static ssize_t store_line1(struct device *dev, struct device_attribute *attr,
666 const char *buf, size_t count)
667{
668 return store_line(dev, buf, count, LCD_LINE1_OFFSET, LCD_LINE1_SIZE);
669}
670
671static ssize_t store_line2(struct device *dev, struct device_attribute *attr,
672 const char *buf, size_t count)
673{
674 return store_line(dev, buf, count, LCD_LINE2_OFFSET, LCD_LINE2_SIZE);
675}
676
677static ssize_t store_line3(struct device *dev, struct device_attribute *attr,
678 const char *buf, size_t count)
679{
680 return store_line(dev, buf, count, LCD_LINE3_OFFSET, LCD_LINE3_SIZE);
681}
682
683/* Interface to visible and audible "icons", these include:
684 * pictures on the LCD, the LED, and the dialtone signal.
685 */
686
687/* Get a list of "switchable elements" with their current state. */
688static ssize_t get_icons(struct device *dev, struct device_attribute *attr,
689 char *buf)
690{
691 struct yealink_dev *yld;
692 int i, ret = 1;
693
694 down_read(&sysfs_rwsema);
695 yld = dev_get_drvdata(dev);
696 if (yld == NULL) {
697 up_read(&sysfs_rwsema);
698 return -ENODEV;
699 }
700
701 for (i = 0; i < ARRAY_SIZE(lcdMap); i++) {
702 if (lcdMap[i].type != '.')
703 continue;
704 ret += sprintf(&buf[ret], "%s %s\n",
705 yld->lcdMap[i] == ' ' ? " " : "on",
706 lcdMap[i].u.p.name);
707 }
708 up_read(&sysfs_rwsema);
709 return ret;
710}
711
712/* Change the visibility of a particular element. */
713static ssize_t set_icon(struct device *dev, const char *buf, size_t count,
714 int chr)
715{
716 struct yealink_dev *yld;
717 int i;
718
719 down_write(&sysfs_rwsema);
720 yld = dev_get_drvdata(dev);
721 if (yld == NULL) {
722 up_write(&sysfs_rwsema);
723 return -ENODEV;
724 }
725
726 for (i = 0; i < ARRAY_SIZE(lcdMap); i++) {
727 if (lcdMap[i].type != '.')
728 continue;
729 if (strncmp(buf, lcdMap[i].u.p.name, count) == 0) {
730 setChar(yld, i, chr);
731 break;
732 }
733 }
734
735 up_write(&sysfs_rwsema);
736 return count;
737}
738
739static ssize_t show_icon(struct device *dev, struct device_attribute *attr,
740 const char *buf, size_t count)
741{
742 return set_icon(dev, buf, count, buf[0]);
743}
744
745static ssize_t hide_icon(struct device *dev, struct device_attribute *attr,
746 const char *buf, size_t count)
747{
748 return set_icon(dev, buf, count, ' ');
749}
750
751/* Upload a ringtone to the device.
752 */
753
754/* Stores raw ringtone data in the phone */
755static ssize_t store_ringtone(struct device *dev,
756 struct device_attribute *attr,
757 const char *buf, size_t count)
758{
759 struct yealink_dev *yld;
760
761 down_write(&sysfs_rwsema);
762 yld = dev_get_drvdata(dev);
763 if (yld == NULL) {
764 up_write(&sysfs_rwsema);
765 return -ENODEV;
766 }
767
768 /* TODO locking with async usb control interface??? */
769 yealink_set_ringtone(yld, (char *)buf, count);
770 up_write(&sysfs_rwsema);
771 return count;
772}
773
774#define _M444 S_IRUGO
775#define _M664 S_IRUGO|S_IWUSR|S_IWGRP
776#define _M220 S_IWUSR|S_IWGRP
777
778static DEVICE_ATTR(map_seg7 , _M664, show_map , store_map );
779static DEVICE_ATTR(line1 , _M664, show_line1 , store_line1 );
780static DEVICE_ATTR(line2 , _M664, show_line2 , store_line2 );
781static DEVICE_ATTR(line3 , _M664, show_line3 , store_line3 );
782static DEVICE_ATTR(get_icons , _M444, get_icons , NULL );
783static DEVICE_ATTR(show_icon , _M220, NULL , show_icon );
784static DEVICE_ATTR(hide_icon , _M220, NULL , hide_icon );
785static DEVICE_ATTR(ringtone , _M220, NULL , store_ringtone);
786
787static struct attribute *yld_attributes[] = {
788 &dev_attr_line1.attr,
789 &dev_attr_line2.attr,
790 &dev_attr_line3.attr,
791 &dev_attr_get_icons.attr,
792 &dev_attr_show_icon.attr,
793 &dev_attr_hide_icon.attr,
794 &dev_attr_map_seg7.attr,
795 &dev_attr_ringtone.attr,
796 NULL
797};
798
Arvind Yadavaa335072017-07-10 20:21:06 -0700799static const struct attribute_group yld_attr_group = {
Henkaca951a2005-08-16 16:17:43 +0200800 .attrs = yld_attributes
801};
802
803/*******************************************************************************
804 * Linux interface and usb initialisation
805 ******************************************************************************/
806
Henk8e2ce4f2005-12-30 19:41:11 +0100807struct driver_info {
Henkaca951a2005-08-16 16:17:43 +0200808 char *name;
Henkaca951a2005-08-16 16:17:43 +0200809};
810
Henk8e2ce4f2005-12-30 19:41:11 +0100811static const struct driver_info info_P1K = {
812 .name = "Yealink usb-p1k",
813};
814
815static const struct usb_device_id usb_table [] = {
816 {
817 .match_flags = USB_DEVICE_ID_MATCH_DEVICE |
818 USB_DEVICE_ID_MATCH_INT_INFO,
819 .idVendor = 0x6993,
820 .idProduct = 0xb001,
821 .bInterfaceClass = USB_CLASS_HID,
822 .bInterfaceSubClass = 0,
823 .bInterfaceProtocol = 0,
824 .driver_info = (kernel_ulong_t)&info_P1K
825 },
Henkaca951a2005-08-16 16:17:43 +0200826 { }
827};
828
829static int usb_cleanup(struct yealink_dev *yld, int err)
830{
831 if (yld == NULL)
832 return err;
833
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500834 if (yld->idev) {
835 if (err)
836 input_free_device(yld->idev);
837 else
838 input_unregister_device(yld->idev);
839 }
Henk Vergonet3e2aac32006-09-08 02:21:01 +0200840
Dmitry Torokhove37a97d2007-05-03 00:57:29 -0400841 usb_free_urb(yld->urb_irq);
842 usb_free_urb(yld->urb_ctl);
843
Alan Stern0ede76f2010-03-05 15:10:17 -0500844 kfree(yld->ctl_req);
Daniel Mack997ea582010-04-12 13:17:25 +0200845 usb_free_coherent(yld->udev, USB_PKT_LEN, yld->ctl_data, yld->ctl_dma);
846 usb_free_coherent(yld->udev, USB_PKT_LEN, yld->irq_data, yld->irq_dma);
Dmitry Torokhove37a97d2007-05-03 00:57:29 -0400847
Henkaca951a2005-08-16 16:17:43 +0200848 kfree(yld);
849 return err;
850}
851
852static void usb_disconnect(struct usb_interface *intf)
853{
854 struct yealink_dev *yld;
855
856 down_write(&sysfs_rwsema);
857 yld = usb_get_intfdata(intf);
858 sysfs_remove_group(&intf->dev.kobj, &yld_attr_group);
859 usb_set_intfdata(intf, NULL);
860 up_write(&sysfs_rwsema);
861
862 usb_cleanup(yld, 0);
863}
864
Henkaca951a2005-08-16 16:17:43 +0200865static int usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
866{
867 struct usb_device *udev = interface_to_usbdev (intf);
Henk8e2ce4f2005-12-30 19:41:11 +0100868 struct driver_info *nfo = (struct driver_info *)id->driver_info;
Henkaca951a2005-08-16 16:17:43 +0200869 struct usb_host_interface *interface;
870 struct usb_endpoint_descriptor *endpoint;
871 struct yealink_dev *yld;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500872 struct input_dev *input_dev;
Henkaca951a2005-08-16 16:17:43 +0200873 int ret, pipe, i;
874
Henkaca951a2005-08-16 16:17:43 +0200875 interface = intf->cur_altsetting;
Johan Hovold5cc4a1a2017-03-16 11:37:01 -0700876
877 if (interface->desc.bNumEndpoints < 1)
878 return -ENODEV;
879
Henkaca951a2005-08-16 16:17:43 +0200880 endpoint = &interface->endpoint[0].desc;
Luiz Fernando N. Capitulino6f7cd442006-10-26 13:03:04 -0300881 if (!usb_endpoint_is_int_in(endpoint))
882 return -ENODEV;
Henkaca951a2005-08-16 16:17:43 +0200883
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500884 yld = kzalloc(sizeof(struct yealink_dev), GFP_KERNEL);
885 if (!yld)
Henkaca951a2005-08-16 16:17:43 +0200886 return -ENOMEM;
887
Henkaca951a2005-08-16 16:17:43 +0200888 yld->udev = udev;
Greg Kroah-Hartman73e66ce2012-05-04 15:33:01 -0700889 yld->intf = intf;
Henkaca951a2005-08-16 16:17:43 +0200890
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500891 yld->idev = input_dev = input_allocate_device();
892 if (!input_dev)
893 return usb_cleanup(yld, -ENOMEM);
894
Henkaca951a2005-08-16 16:17:43 +0200895 /* allocate usb buffers */
Daniel Mack997ea582010-04-12 13:17:25 +0200896 yld->irq_data = usb_alloc_coherent(udev, USB_PKT_LEN,
Jia-Ju Bai434ca102018-07-27 11:34:13 -0700897 GFP_KERNEL, &yld->irq_dma);
Henkaca951a2005-08-16 16:17:43 +0200898 if (yld->irq_data == NULL)
899 return usb_cleanup(yld, -ENOMEM);
900
Daniel Mack997ea582010-04-12 13:17:25 +0200901 yld->ctl_data = usb_alloc_coherent(udev, USB_PKT_LEN,
Jia-Ju Bai434ca102018-07-27 11:34:13 -0700902 GFP_KERNEL, &yld->ctl_dma);
Henkaca951a2005-08-16 16:17:43 +0200903 if (!yld->ctl_data)
904 return usb_cleanup(yld, -ENOMEM);
905
Alan Stern0ede76f2010-03-05 15:10:17 -0500906 yld->ctl_req = kmalloc(sizeof(*(yld->ctl_req)), GFP_KERNEL);
Henkaca951a2005-08-16 16:17:43 +0200907 if (yld->ctl_req == NULL)
908 return usb_cleanup(yld, -ENOMEM);
909
910 /* allocate urb structures */
911 yld->urb_irq = usb_alloc_urb(0, GFP_KERNEL);
912 if (yld->urb_irq == NULL)
913 return usb_cleanup(yld, -ENOMEM);
914
915 yld->urb_ctl = usb_alloc_urb(0, GFP_KERNEL);
916 if (yld->urb_ctl == NULL)
917 return usb_cleanup(yld, -ENOMEM);
918
919 /* get a handle to the interrupt data pipe */
920 pipe = usb_rcvintpipe(udev, endpoint->bEndpointAddress);
921 ret = usb_maxpacket(udev, pipe, usb_pipeout(pipe));
922 if (ret != USB_PKT_LEN)
Greg Kroah-Hartman08813d32012-04-25 14:48:31 -0700923 dev_err(&intf->dev, "invalid payload size %d, expected %zd\n",
924 ret, USB_PKT_LEN);
Henkaca951a2005-08-16 16:17:43 +0200925
926 /* initialise irq urb */
927 usb_fill_int_urb(yld->urb_irq, udev, pipe, yld->irq_data,
928 USB_PKT_LEN,
929 urb_irq_callback,
930 yld, endpoint->bInterval);
931 yld->urb_irq->transfer_dma = yld->irq_dma;
932 yld->urb_irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
933 yld->urb_irq->dev = udev;
934
935 /* initialise ctl urb */
936 yld->ctl_req->bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE |
937 USB_DIR_OUT;
938 yld->ctl_req->bRequest = USB_REQ_SET_CONFIGURATION;
939 yld->ctl_req->wValue = cpu_to_le16(0x200);
940 yld->ctl_req->wIndex = cpu_to_le16(interface->desc.bInterfaceNumber);
941 yld->ctl_req->wLength = cpu_to_le16(USB_PKT_LEN);
942
943 usb_fill_control_urb(yld->urb_ctl, udev, usb_sndctrlpipe(udev, 0),
944 (void *)yld->ctl_req, yld->ctl_data, USB_PKT_LEN,
945 urb_ctl_callback, yld);
Henkaca951a2005-08-16 16:17:43 +0200946 yld->urb_ctl->transfer_dma = yld->ctl_dma;
Alan Stern0ede76f2010-03-05 15:10:17 -0500947 yld->urb_ctl->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
Henkaca951a2005-08-16 16:17:43 +0200948 yld->urb_ctl->dev = udev;
949
950 /* find out the physical bus location */
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500951 usb_make_path(udev, yld->phys, sizeof(yld->phys));
952 strlcat(yld->phys, "/input0", sizeof(yld->phys));
Henkaca951a2005-08-16 16:17:43 +0200953
954 /* register settings for the input device */
Henk8e2ce4f2005-12-30 19:41:11 +0100955 input_dev->name = nfo->name;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500956 input_dev->phys = yld->phys;
957 usb_to_input_id(udev, &input_dev->id);
Dmitry Torokhovc0f82d52007-04-12 01:35:03 -0400958 input_dev->dev.parent = &intf->dev;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500959
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400960 input_set_drvdata(input_dev, yld);
961
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500962 input_dev->open = input_open;
963 input_dev->close = input_close;
964 /* input_dev->event = input_ev; TODO */
Henkaca951a2005-08-16 16:17:43 +0200965
966 /* register available key events */
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700967 input_dev->evbit[0] = BIT_MASK(EV_KEY);
Henkaca951a2005-08-16 16:17:43 +0200968 for (i = 0; i < 256; i++) {
969 int k = map_p1k_to_key(i);
970 if (k >= 0) {
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500971 set_bit(k & 0xff, input_dev->keybit);
Henkaca951a2005-08-16 16:17:43 +0200972 if (k >> 8)
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500973 set_bit(k >> 8, input_dev->keybit);
Henkaca951a2005-08-16 16:17:43 +0200974 }
975 }
976
Dmitry Torokhov50141862007-04-12 01:33:39 -0400977 ret = input_register_device(yld->idev);
978 if (ret)
979 return usb_cleanup(yld, ret);
Henkaca951a2005-08-16 16:17:43 +0200980
981 usb_set_intfdata(intf, yld);
982
983 /* clear visible elements */
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500984 for (i = 0; i < ARRAY_SIZE(lcdMap); i++)
Henkaca951a2005-08-16 16:17:43 +0200985 setChar(yld, i, ' ');
986
987 /* display driver version on LCD line 3 */
988 store_line3(&intf->dev, NULL,
989 DRIVER_VERSION, sizeof(DRIVER_VERSION));
990
991 /* Register sysfs hooks (don't care about failure) */
Greg Kroah-Hartman657b6712006-08-28 11:43:25 -0700992 ret = sysfs_create_group(&intf->dev.kobj, &yld_attr_group);
Henkaca951a2005-08-16 16:17:43 +0200993 return 0;
994}
995
996static struct usb_driver yealink_driver = {
Henkaca951a2005-08-16 16:17:43 +0200997 .name = "yealink",
998 .probe = usb_probe,
999 .disconnect = usb_disconnect,
1000 .id_table = usb_table,
1001};
1002
Greg Kroah-Hartman08642e72011-11-18 09:48:31 -08001003module_usb_driver(yealink_driver);
Henkaca951a2005-08-16 16:17:43 +02001004
1005MODULE_DEVICE_TABLE (usb, usb_table);
1006
Julia Lawall698c03b2018-01-16 16:56:54 -08001007MODULE_AUTHOR("Henk Vergonet");
1008MODULE_DESCRIPTION("Yealink phone driver");
Henkaca951a2005-08-16 16:17:43 +02001009MODULE_LICENSE("GPL");