yealink.c 24 KB

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