yealink.c 25 KB

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