yealink.c 25 KB

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