yealink.c 25 KB

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