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