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