yealink.c 25 KB

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