yealink.c 24 KB

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