yealink.c 25 KB

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