yealink.c 25 KB

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