yealink.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005
  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/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 "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. /* 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. dma_addr_t ctl_req_dma;
  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. 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, struct pt_regs *regs)
  210. {
  211. struct input_dev *idev = yld->idev;
  212. input_regs(idev, regs);
  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, struct pt_regs *regs)
  377. {
  378. struct yealink_dev *yld = urb->context;
  379. int ret;
  380. if (urb->status)
  381. err("%s - urb status %d", __FUNCTION__, urb->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. dbg("get scancode %x", yld->irq_data->data[0]);
  388. report_key(yld, map_p1k_to_key(yld->irq_data->data[0]), regs);
  389. break;
  390. default:
  391. err("unexpected response %x", yld->irq_data->cmd);
  392. }
  393. yealink_do_idle_tasks(yld);
  394. ret = usb_submit_urb(yld->urb_ctl, GFP_ATOMIC);
  395. if (ret)
  396. err("%s - usb_submit_urb failed %d", __FUNCTION__, ret);
  397. }
  398. static void urb_ctl_callback(struct urb *urb, struct pt_regs *regs)
  399. {
  400. struct yealink_dev *yld = urb->context;
  401. int ret;
  402. if (urb->status)
  403. err("%s - urb status %d", __FUNCTION__, urb->status);
  404. switch (yld->ctl_data->cmd) {
  405. case CMD_KEYPRESS:
  406. case CMD_SCANCODE:
  407. /* ask for a response */
  408. ret = usb_submit_urb(yld->urb_irq, GFP_ATOMIC);
  409. break;
  410. default:
  411. /* send new command */
  412. yealink_do_idle_tasks(yld);
  413. ret = usb_submit_urb(yld->urb_ctl, GFP_ATOMIC);
  414. }
  415. if (ret)
  416. err("%s - usb_submit_urb failed %d", __FUNCTION__, ret);
  417. }
  418. /*******************************************************************************
  419. * input event interface
  420. ******************************************************************************/
  421. /* TODO should we issue a ringtone on a SND_BELL event?
  422. static int input_ev(struct input_dev *dev, unsigned int type,
  423. unsigned int code, int value)
  424. {
  425. if (type != EV_SND)
  426. return -EINVAL;
  427. switch (code) {
  428. case SND_BELL:
  429. case SND_TONE:
  430. break;
  431. default:
  432. return -EINVAL;
  433. }
  434. return 0;
  435. }
  436. */
  437. static int input_open(struct input_dev *dev)
  438. {
  439. struct yealink_dev *yld = dev->private;
  440. int i, ret;
  441. dbg("%s", __FUNCTION__);
  442. /* force updates to device */
  443. for (i = 0; i<sizeof(yld->master); i++)
  444. yld->copy.b[i] = ~yld->master.b[i];
  445. yld->key_code = -1; /* no keys pressed */
  446. yealink_set_ringtone(yld, default_ringtone, sizeof(default_ringtone));
  447. /* issue INIT */
  448. memset(yld->ctl_data, 0, sizeof(*(yld->ctl_data)));
  449. yld->ctl_data->cmd = CMD_INIT;
  450. yld->ctl_data->size = 10;
  451. yld->ctl_data->sum = 0x100-CMD_INIT-10;
  452. if ((ret = usb_submit_urb(yld->urb_ctl, GFP_KERNEL)) != 0) {
  453. dbg("%s - usb_submit_urb failed with result %d",
  454. __FUNCTION__, ret);
  455. return ret;
  456. }
  457. return 0;
  458. }
  459. static void input_close(struct input_dev *dev)
  460. {
  461. struct yealink_dev *yld = dev->private;
  462. usb_kill_urb(yld->urb_ctl);
  463. usb_kill_urb(yld->urb_irq);
  464. }
  465. /*******************************************************************************
  466. * sysfs interface
  467. ******************************************************************************/
  468. static DECLARE_RWSEM(sysfs_rwsema);
  469. /* Interface to the 7-segments translation table aka. char set.
  470. */
  471. static ssize_t show_map(struct device *dev, struct device_attribute *attr,
  472. char *buf)
  473. {
  474. memcpy(buf, &map_seg7, sizeof(map_seg7));
  475. return sizeof(map_seg7);
  476. }
  477. static ssize_t store_map(struct device *dev, struct device_attribute *attr,
  478. const char *buf, size_t cnt)
  479. {
  480. if (cnt != sizeof(map_seg7))
  481. return -EINVAL;
  482. memcpy(&map_seg7, buf, sizeof(map_seg7));
  483. return sizeof(map_seg7);
  484. }
  485. /* Interface to the LCD.
  486. */
  487. /* Reading /sys/../lineX will return the format string with its settings:
  488. *
  489. * Example:
  490. * cat ./line3
  491. * 888888888888
  492. * Linux Rocks!
  493. */
  494. static ssize_t show_line(struct device *dev, char *buf, int a, int b)
  495. {
  496. struct yealink_dev *yld;
  497. int i;
  498. down_read(&sysfs_rwsema);
  499. yld = dev_get_drvdata(dev);
  500. if (yld == NULL) {
  501. up_read(&sysfs_rwsema);
  502. return -ENODEV;
  503. }
  504. for (i = a; i < b; i++)
  505. *buf++ = lcdMap[i].type;
  506. *buf++ = '\n';
  507. for (i = a; i < b; i++)
  508. *buf++ = yld->lcdMap[i];
  509. *buf++ = '\n';
  510. *buf = 0;
  511. up_read(&sysfs_rwsema);
  512. return 3 + ((b - a) << 1);
  513. }
  514. static ssize_t show_line1(struct device *dev, struct device_attribute *attr,
  515. char *buf)
  516. {
  517. return show_line(dev, buf, LCD_LINE1_OFFSET, LCD_LINE2_OFFSET);
  518. }
  519. static ssize_t show_line2(struct device *dev, struct device_attribute *attr,
  520. char *buf)
  521. {
  522. return show_line(dev, buf, LCD_LINE2_OFFSET, LCD_LINE3_OFFSET);
  523. }
  524. static ssize_t show_line3(struct device *dev, struct device_attribute *attr,
  525. char *buf)
  526. {
  527. return show_line(dev, buf, LCD_LINE3_OFFSET, LCD_LINE4_OFFSET);
  528. }
  529. /* Writing to /sys/../lineX will set the coresponding LCD line.
  530. * - Excess characters are ignored.
  531. * - If less characters are written than allowed, the remaining digits are
  532. * unchanged.
  533. * - The '\n' or '\t' char is a placeholder, it does not overwrite the
  534. * original content.
  535. */
  536. static ssize_t store_line(struct device *dev, const char *buf, size_t count,
  537. int el, size_t len)
  538. {
  539. struct yealink_dev *yld;
  540. int i;
  541. down_write(&sysfs_rwsema);
  542. yld = dev_get_drvdata(dev);
  543. if (yld == NULL) {
  544. up_write(&sysfs_rwsema);
  545. return -ENODEV;
  546. }
  547. if (len > count)
  548. len = count;
  549. for (i = 0; i < len; i++)
  550. setChar(yld, el++, buf[i]);
  551. up_write(&sysfs_rwsema);
  552. return count;
  553. }
  554. static ssize_t store_line1(struct device *dev, struct device_attribute *attr,
  555. const char *buf, size_t count)
  556. {
  557. return store_line(dev, buf, count, LCD_LINE1_OFFSET, LCD_LINE1_SIZE);
  558. }
  559. static ssize_t store_line2(struct device *dev, struct device_attribute *attr,
  560. const char *buf, size_t count)
  561. {
  562. return store_line(dev, buf, count, LCD_LINE2_OFFSET, LCD_LINE2_SIZE);
  563. }
  564. static ssize_t store_line3(struct device *dev, struct device_attribute *attr,
  565. const char *buf, size_t count)
  566. {
  567. return store_line(dev, buf, count, LCD_LINE3_OFFSET, LCD_LINE3_SIZE);
  568. }
  569. /* Interface to visible and audible "icons", these include:
  570. * pictures on the LCD, the LED, and the dialtone signal.
  571. */
  572. /* Get a list of "switchable elements" with their current state. */
  573. static ssize_t get_icons(struct device *dev, struct device_attribute *attr,
  574. char *buf)
  575. {
  576. struct yealink_dev *yld;
  577. int i, ret = 1;
  578. down_read(&sysfs_rwsema);
  579. yld = dev_get_drvdata(dev);
  580. if (yld == NULL) {
  581. up_read(&sysfs_rwsema);
  582. return -ENODEV;
  583. }
  584. for (i = 0; i < ARRAY_SIZE(lcdMap); i++) {
  585. if (lcdMap[i].type != '.')
  586. continue;
  587. ret += sprintf(&buf[ret], "%s %s\n",
  588. yld->lcdMap[i] == ' ' ? " " : "on",
  589. lcdMap[i].u.p.name);
  590. }
  591. up_read(&sysfs_rwsema);
  592. return ret;
  593. }
  594. /* Change the visibility of a particular element. */
  595. static ssize_t set_icon(struct device *dev, const char *buf, size_t count,
  596. int chr)
  597. {
  598. struct yealink_dev *yld;
  599. int i;
  600. down_write(&sysfs_rwsema);
  601. yld = dev_get_drvdata(dev);
  602. if (yld == NULL) {
  603. up_write(&sysfs_rwsema);
  604. return -ENODEV;
  605. }
  606. for (i = 0; i < ARRAY_SIZE(lcdMap); i++) {
  607. if (lcdMap[i].type != '.')
  608. continue;
  609. if (strncmp(buf, lcdMap[i].u.p.name, count) == 0) {
  610. setChar(yld, i, chr);
  611. break;
  612. }
  613. }
  614. up_write(&sysfs_rwsema);
  615. return count;
  616. }
  617. static ssize_t show_icon(struct device *dev, struct device_attribute *attr,
  618. const char *buf, size_t count)
  619. {
  620. return set_icon(dev, buf, count, buf[0]);
  621. }
  622. static ssize_t hide_icon(struct device *dev, struct device_attribute *attr,
  623. const char *buf, size_t count)
  624. {
  625. return set_icon(dev, buf, count, ' ');
  626. }
  627. /* Upload a ringtone to the device.
  628. */
  629. /* Stores raw ringtone data in the phone */
  630. static ssize_t store_ringtone(struct device *dev,
  631. struct device_attribute *attr,
  632. const char *buf, size_t count)
  633. {
  634. struct yealink_dev *yld;
  635. down_write(&sysfs_rwsema);
  636. yld = dev_get_drvdata(dev);
  637. if (yld == NULL) {
  638. up_write(&sysfs_rwsema);
  639. return -ENODEV;
  640. }
  641. /* TODO locking with async usb control interface??? */
  642. yealink_set_ringtone(yld, (char *)buf, count);
  643. up_write(&sysfs_rwsema);
  644. return count;
  645. }
  646. #define _M444 S_IRUGO
  647. #define _M664 S_IRUGO|S_IWUSR|S_IWGRP
  648. #define _M220 S_IWUSR|S_IWGRP
  649. static DEVICE_ATTR(map_seg7 , _M664, show_map , store_map );
  650. static DEVICE_ATTR(line1 , _M664, show_line1 , store_line1 );
  651. static DEVICE_ATTR(line2 , _M664, show_line2 , store_line2 );
  652. static DEVICE_ATTR(line3 , _M664, show_line3 , store_line3 );
  653. static DEVICE_ATTR(get_icons , _M444, get_icons , NULL );
  654. static DEVICE_ATTR(show_icon , _M220, NULL , show_icon );
  655. static DEVICE_ATTR(hide_icon , _M220, NULL , hide_icon );
  656. static DEVICE_ATTR(ringtone , _M220, NULL , store_ringtone);
  657. static struct attribute *yld_attributes[] = {
  658. &dev_attr_line1.attr,
  659. &dev_attr_line2.attr,
  660. &dev_attr_line3.attr,
  661. &dev_attr_get_icons.attr,
  662. &dev_attr_show_icon.attr,
  663. &dev_attr_hide_icon.attr,
  664. &dev_attr_map_seg7.attr,
  665. &dev_attr_ringtone.attr,
  666. NULL
  667. };
  668. static struct attribute_group yld_attr_group = {
  669. .attrs = yld_attributes
  670. };
  671. /*******************************************************************************
  672. * Linux interface and usb initialisation
  673. ******************************************************************************/
  674. struct driver_info {
  675. char *name;
  676. };
  677. static const struct driver_info info_P1K = {
  678. .name = "Yealink usb-p1k",
  679. };
  680. static const struct usb_device_id usb_table [] = {
  681. {
  682. .match_flags = USB_DEVICE_ID_MATCH_DEVICE |
  683. USB_DEVICE_ID_MATCH_INT_INFO,
  684. .idVendor = 0x6993,
  685. .idProduct = 0xb001,
  686. .bInterfaceClass = USB_CLASS_HID,
  687. .bInterfaceSubClass = 0,
  688. .bInterfaceProtocol = 0,
  689. .driver_info = (kernel_ulong_t)&info_P1K
  690. },
  691. { }
  692. };
  693. static int usb_cleanup(struct yealink_dev *yld, int err)
  694. {
  695. if (yld == NULL)
  696. return err;
  697. usb_kill_urb(yld->urb_irq); /* parameter validation in core/urb */
  698. usb_kill_urb(yld->urb_ctl); /* parameter validation in core/urb */
  699. if (yld->idev) {
  700. if (err)
  701. input_free_device(yld->idev);
  702. else
  703. input_unregister_device(yld->idev);
  704. }
  705. if (yld->ctl_req)
  706. usb_buffer_free(yld->udev, sizeof(*(yld->ctl_req)),
  707. yld->ctl_req, yld->ctl_req_dma);
  708. if (yld->ctl_data)
  709. usb_buffer_free(yld->udev, USB_PKT_LEN,
  710. yld->ctl_data, yld->ctl_dma);
  711. if (yld->irq_data)
  712. usb_buffer_free(yld->udev, USB_PKT_LEN,
  713. yld->irq_data, yld->irq_dma);
  714. usb_free_urb(yld->urb_irq); /* parameter validation in core/urb */
  715. usb_free_urb(yld->urb_ctl); /* parameter validation in core/urb */
  716. kfree(yld);
  717. return err;
  718. }
  719. static void usb_disconnect(struct usb_interface *intf)
  720. {
  721. struct yealink_dev *yld;
  722. down_write(&sysfs_rwsema);
  723. yld = usb_get_intfdata(intf);
  724. sysfs_remove_group(&intf->dev.kobj, &yld_attr_group);
  725. usb_set_intfdata(intf, NULL);
  726. up_write(&sysfs_rwsema);
  727. usb_cleanup(yld, 0);
  728. }
  729. static int usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
  730. {
  731. struct usb_device *udev = interface_to_usbdev (intf);
  732. struct driver_info *nfo = (struct driver_info *)id->driver_info;
  733. struct usb_host_interface *interface;
  734. struct usb_endpoint_descriptor *endpoint;
  735. struct yealink_dev *yld;
  736. struct input_dev *input_dev;
  737. int ret, pipe, i;
  738. interface = intf->cur_altsetting;
  739. endpoint = &interface->endpoint[0].desc;
  740. if (!(endpoint->bEndpointAddress & USB_DIR_IN))
  741. return -EIO;
  742. if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
  743. return -EIO;
  744. yld = kzalloc(sizeof(struct yealink_dev), GFP_KERNEL);
  745. if (!yld)
  746. return -ENOMEM;
  747. yld->udev = udev;
  748. yld->idev = input_dev = input_allocate_device();
  749. if (!input_dev)
  750. return usb_cleanup(yld, -ENOMEM);
  751. /* allocate usb buffers */
  752. yld->irq_data = usb_buffer_alloc(udev, USB_PKT_LEN,
  753. SLAB_ATOMIC, &yld->irq_dma);
  754. if (yld->irq_data == NULL)
  755. return usb_cleanup(yld, -ENOMEM);
  756. yld->ctl_data = usb_buffer_alloc(udev, USB_PKT_LEN,
  757. SLAB_ATOMIC, &yld->ctl_dma);
  758. if (!yld->ctl_data)
  759. return usb_cleanup(yld, -ENOMEM);
  760. yld->ctl_req = usb_buffer_alloc(udev, sizeof(*(yld->ctl_req)),
  761. SLAB_ATOMIC, &yld->ctl_req_dma);
  762. if (yld->ctl_req == NULL)
  763. return usb_cleanup(yld, -ENOMEM);
  764. /* allocate urb structures */
  765. yld->urb_irq = usb_alloc_urb(0, GFP_KERNEL);
  766. if (yld->urb_irq == NULL)
  767. return usb_cleanup(yld, -ENOMEM);
  768. yld->urb_ctl = usb_alloc_urb(0, GFP_KERNEL);
  769. if (yld->urb_ctl == NULL)
  770. return usb_cleanup(yld, -ENOMEM);
  771. /* get a handle to the interrupt data pipe */
  772. pipe = usb_rcvintpipe(udev, endpoint->bEndpointAddress);
  773. ret = usb_maxpacket(udev, pipe, usb_pipeout(pipe));
  774. if (ret != USB_PKT_LEN)
  775. err("invalid payload size %d, expected %zd", ret, USB_PKT_LEN);
  776. /* initialise irq urb */
  777. usb_fill_int_urb(yld->urb_irq, udev, pipe, yld->irq_data,
  778. USB_PKT_LEN,
  779. urb_irq_callback,
  780. yld, endpoint->bInterval);
  781. yld->urb_irq->transfer_dma = yld->irq_dma;
  782. yld->urb_irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  783. yld->urb_irq->dev = udev;
  784. /* initialise ctl urb */
  785. yld->ctl_req->bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE |
  786. USB_DIR_OUT;
  787. yld->ctl_req->bRequest = USB_REQ_SET_CONFIGURATION;
  788. yld->ctl_req->wValue = cpu_to_le16(0x200);
  789. yld->ctl_req->wIndex = cpu_to_le16(interface->desc.bInterfaceNumber);
  790. yld->ctl_req->wLength = cpu_to_le16(USB_PKT_LEN);
  791. usb_fill_control_urb(yld->urb_ctl, udev, usb_sndctrlpipe(udev, 0),
  792. (void *)yld->ctl_req, yld->ctl_data, USB_PKT_LEN,
  793. urb_ctl_callback, yld);
  794. yld->urb_ctl->setup_dma = yld->ctl_req_dma;
  795. yld->urb_ctl->transfer_dma = yld->ctl_dma;
  796. yld->urb_ctl->transfer_flags |= URB_NO_SETUP_DMA_MAP |
  797. URB_NO_TRANSFER_DMA_MAP;
  798. yld->urb_ctl->dev = udev;
  799. /* find out the physical bus location */
  800. usb_make_path(udev, yld->phys, sizeof(yld->phys));
  801. strlcat(yld->phys, "/input0", sizeof(yld->phys));
  802. /* register settings for the input device */
  803. input_dev->name = nfo->name;
  804. input_dev->phys = yld->phys;
  805. usb_to_input_id(udev, &input_dev->id);
  806. input_dev->cdev.dev = &intf->dev;
  807. input_dev->private = yld;
  808. input_dev->open = input_open;
  809. input_dev->close = input_close;
  810. /* input_dev->event = input_ev; TODO */
  811. /* register available key events */
  812. input_dev->evbit[0] = BIT(EV_KEY);
  813. for (i = 0; i < 256; i++) {
  814. int k = map_p1k_to_key(i);
  815. if (k >= 0) {
  816. set_bit(k & 0xff, input_dev->keybit);
  817. if (k >> 8)
  818. set_bit(k >> 8, input_dev->keybit);
  819. }
  820. }
  821. input_register_device(yld->idev);
  822. usb_set_intfdata(intf, yld);
  823. /* clear visible elements */
  824. for (i = 0; i < ARRAY_SIZE(lcdMap); i++)
  825. setChar(yld, i, ' ');
  826. /* display driver version on LCD line 3 */
  827. store_line3(&intf->dev, NULL,
  828. DRIVER_VERSION, sizeof(DRIVER_VERSION));
  829. /* Register sysfs hooks (don't care about failure) */
  830. sysfs_create_group(&intf->dev.kobj, &yld_attr_group);
  831. return 0;
  832. }
  833. static struct usb_driver yealink_driver = {
  834. .name = "yealink",
  835. .probe = usb_probe,
  836. .disconnect = usb_disconnect,
  837. .id_table = usb_table,
  838. };
  839. static int __init yealink_dev_init(void)
  840. {
  841. int ret = usb_register(&yealink_driver);
  842. if (ret == 0)
  843. info(DRIVER_DESC ":" DRIVER_VERSION);
  844. return ret;
  845. }
  846. static void __exit yealink_dev_exit(void)
  847. {
  848. usb_deregister(&yealink_driver);
  849. }
  850. module_init(yealink_dev_init);
  851. module_exit(yealink_dev_exit);
  852. MODULE_DEVICE_TABLE (usb, usb_table);
  853. MODULE_AUTHOR(DRIVER_AUTHOR);
  854. MODULE_DESCRIPTION(DRIVER_DESC);
  855. MODULE_LICENSE("GPL");