yealink.c 25 KB

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