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/usb/input/xpad.c
  33. *
  34. * Thanks to:
  35. * - Olivier Vandorpe, for providing the usbb2k-api.
  36. * - Martin Diehl, for spotting my memory allocation bug.
  37. *
  38. * History:
  39. * 20050527 henk First version, functional keyboard. Keyboard events
  40. * will pop-up on the ../input/eventX bus.
  41. * 20050531 henk Added led, LCD, dialtone and sysfs interface.
  42. * 20050610 henk Cleanups, make it ready for public consumption.
  43. * 20050630 henk Cleanups, fixes in response to comments.
  44. * 20050701 henk sysfs write serialisation, fix potential unload races
  45. * 20050801 henk Added ringtone, restructure USB
  46. * 20050816 henk Merge 2.6.13-rc6
  47. */
  48. #include <linux/kernel.h>
  49. #include <linux/init.h>
  50. #include <linux/slab.h>
  51. #include <linux/module.h>
  52. #include <linux/rwsem.h>
  53. #include <linux/usb/input.h>
  54. #include "map_to_7segment.h"
  55. #include "yealink.h"
  56. #define DRIVER_VERSION "yld-20051230"
  57. #define DRIVER_AUTHOR "Henk Vergonet"
  58. #define DRIVER_DESC "Yealink phone driver"
  59. #define YEALINK_POLLING_FREQUENCY 10 /* in [Hz] */
  60. struct yld_status {
  61. u8 lcd[24];
  62. u8 led;
  63. u8 dialtone;
  64. u8 ringtone;
  65. u8 keynum;
  66. } __attribute__ ((packed));
  67. /*
  68. * Register the LCD segment and icon map
  69. */
  70. #define _LOC(k,l) { .a = (k), .m = (l) }
  71. #define _SEG(t, a, am, b, bm, c, cm, d, dm, e, em, f, fm, g, gm) \
  72. { .type = (t), \
  73. .u = { .s = { _LOC(a, am), _LOC(b, bm), _LOC(c, cm), \
  74. _LOC(d, dm), _LOC(e, em), _LOC(g, gm), \
  75. _LOC(f, fm) } } }
  76. #define _PIC(t, h, hm, n) \
  77. { .type = (t), \
  78. .u = { .p = { .name = (n), .a = (h), .m = (hm) } } }
  79. static const struct lcd_segment_map {
  80. char type;
  81. union {
  82. struct pictogram_map {
  83. u8 a,m;
  84. char name[10];
  85. } p;
  86. struct segment_map {
  87. u8 a,m;
  88. } s[7];
  89. } u;
  90. } lcdMap[] = {
  91. #include "yealink.h"
  92. };
  93. struct yealink_dev {
  94. struct input_dev *idev; /* input device */
  95. struct usb_device *udev; /* usb device */
  96. /* irq input channel */
  97. struct yld_ctl_packet *irq_data;
  98. dma_addr_t irq_dma;
  99. struct urb *urb_irq;
  100. /* control output channel */
  101. struct yld_ctl_packet *ctl_data;
  102. dma_addr_t ctl_dma;
  103. struct usb_ctrlrequest *ctl_req;
  104. dma_addr_t ctl_req_dma;
  105. struct urb *urb_ctl;
  106. char phys[64]; /* physical device path */
  107. u8 lcdMap[ARRAY_SIZE(lcdMap)]; /* state of LCD, LED ... */
  108. int key_code; /* last reported key */
  109. int stat_ix;
  110. union {
  111. struct yld_status s;
  112. u8 b[sizeof(struct yld_status)];
  113. } master, copy;
  114. };
  115. /*******************************************************************************
  116. * Yealink lcd interface
  117. ******************************************************************************/
  118. /*
  119. * Register a default 7 segment character set
  120. */
  121. static SEG7_DEFAULT_MAP(map_seg7);
  122. /* Display a char,
  123. * char '\9' and '\n' are placeholders and do not overwrite the original text.
  124. * A space will always hide an icon.
  125. */
  126. static int setChar(struct yealink_dev *yld, int el, int chr)
  127. {
  128. int i, a, m, val;
  129. if (el >= ARRAY_SIZE(lcdMap))
  130. return -EINVAL;
  131. if (chr == '\t' || chr == '\n')
  132. return 0;
  133. yld->lcdMap[el] = chr;
  134. if (lcdMap[el].type == '.') {
  135. a = lcdMap[el].u.p.a;
  136. m = lcdMap[el].u.p.m;
  137. if (chr != ' ')
  138. yld->master.b[a] |= m;
  139. else
  140. yld->master.b[a] &= ~m;
  141. return 0;
  142. }
  143. val = map_to_seg7(&map_seg7, chr);
  144. for (i = 0; i < ARRAY_SIZE(lcdMap[0].u.s); i++) {
  145. m = lcdMap[el].u.s[i].m;
  146. if (m == 0)
  147. continue;
  148. a = lcdMap[el].u.s[i].a;
  149. if (val & 1)
  150. yld->master.b[a] |= m;
  151. else
  152. yld->master.b[a] &= ~m;
  153. val = val >> 1;
  154. }
  155. return 0;
  156. };
  157. /*******************************************************************************
  158. * Yealink key interface
  159. ******************************************************************************/
  160. /* Map device buttons to internal key events.
  161. *
  162. * USB-P1K button layout:
  163. *
  164. * up
  165. * IN OUT
  166. * down
  167. *
  168. * pickup C hangup
  169. * 1 2 3
  170. * 4 5 6
  171. * 7 8 9
  172. * * 0 #
  173. *
  174. * The "up" and "down" keys, are symbolised by arrows on the button.
  175. * The "pickup" and "hangup" keys are symbolised by a green and red phone
  176. * on the button.
  177. */
  178. static int map_p1k_to_key(int scancode)
  179. {
  180. switch(scancode) { /* phone key: */
  181. case 0x23: return KEY_LEFT; /* IN */
  182. case 0x33: return KEY_UP; /* up */
  183. case 0x04: return KEY_RIGHT; /* OUT */
  184. case 0x24: return KEY_DOWN; /* down */
  185. case 0x03: return KEY_ENTER; /* pickup */
  186. case 0x14: return KEY_BACKSPACE; /* C */
  187. case 0x13: return KEY_ESC; /* hangup */
  188. case 0x00: return KEY_1; /* 1 */
  189. case 0x01: return KEY_2; /* 2 */
  190. case 0x02: return KEY_3; /* 3 */
  191. case 0x10: return KEY_4; /* 4 */
  192. case 0x11: return KEY_5; /* 5 */
  193. case 0x12: return KEY_6; /* 6 */
  194. case 0x20: return KEY_7; /* 7 */
  195. case 0x21: return KEY_8; /* 8 */
  196. case 0x22: return KEY_9; /* 9 */
  197. case 0x30: return KEY_KPASTERISK; /* * */
  198. case 0x31: return KEY_0; /* 0 */
  199. case 0x32: return KEY_LEFTSHIFT |
  200. KEY_3 << 8; /* # */
  201. }
  202. return -EINVAL;
  203. }
  204. /* Completes a request by converting the data into events for the
  205. * input subsystem.
  206. *
  207. * The key parameter can be cascaded: key2 << 8 | key1
  208. */
  209. static void report_key(struct yealink_dev *yld, int key)
  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;
  379. if (urb->status)
  380. err("%s - urb status %d", __FUNCTION__, urb->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. ret = usb_submit_urb(yld->urb_ctl, GFP_ATOMIC);
  394. if (ret)
  395. err("%s - usb_submit_urb failed %d", __FUNCTION__, ret);
  396. }
  397. static void urb_ctl_callback(struct urb *urb)
  398. {
  399. struct yealink_dev *yld = urb->context;
  400. int ret;
  401. if (urb->status)
  402. err("%s - urb status %d", __FUNCTION__, urb->status);
  403. switch (yld->ctl_data->cmd) {
  404. case CMD_KEYPRESS:
  405. case CMD_SCANCODE:
  406. /* ask for a response */
  407. ret = usb_submit_urb(yld->urb_irq, GFP_ATOMIC);
  408. break;
  409. default:
  410. /* send new command */
  411. yealink_do_idle_tasks(yld);
  412. ret = usb_submit_urb(yld->urb_ctl, GFP_ATOMIC);
  413. }
  414. if (ret)
  415. err("%s - usb_submit_urb failed %d", __FUNCTION__, ret);
  416. }
  417. /*******************************************************************************
  418. * input event interface
  419. ******************************************************************************/
  420. /* TODO should we issue a ringtone on a SND_BELL event?
  421. static int input_ev(struct input_dev *dev, unsigned int type,
  422. unsigned int code, int value)
  423. {
  424. if (type != EV_SND)
  425. return -EINVAL;
  426. switch (code) {
  427. case SND_BELL:
  428. case SND_TONE:
  429. break;
  430. default:
  431. return -EINVAL;
  432. }
  433. return 0;
  434. }
  435. */
  436. static int input_open(struct input_dev *dev)
  437. {
  438. struct yealink_dev *yld = dev->private;
  439. int i, ret;
  440. dbg("%s", __FUNCTION__);
  441. /* force updates to device */
  442. for (i = 0; i<sizeof(yld->master); i++)
  443. yld->copy.b[i] = ~yld->master.b[i];
  444. yld->key_code = -1; /* no keys pressed */
  445. yealink_set_ringtone(yld, default_ringtone, sizeof(default_ringtone));
  446. /* issue INIT */
  447. memset(yld->ctl_data, 0, sizeof(*(yld->ctl_data)));
  448. yld->ctl_data->cmd = CMD_INIT;
  449. yld->ctl_data->size = 10;
  450. yld->ctl_data->sum = 0x100-CMD_INIT-10;
  451. if ((ret = usb_submit_urb(yld->urb_ctl, GFP_KERNEL)) != 0) {
  452. dbg("%s - usb_submit_urb failed with result %d",
  453. __FUNCTION__, ret);
  454. return ret;
  455. }
  456. return 0;
  457. }
  458. static void input_close(struct input_dev *dev)
  459. {
  460. struct yealink_dev *yld = dev->private;
  461. usb_kill_urb(yld->urb_ctl);
  462. usb_kill_urb(yld->urb_irq);
  463. }
  464. /*******************************************************************************
  465. * sysfs interface
  466. ******************************************************************************/
  467. static DECLARE_RWSEM(sysfs_rwsema);
  468. /* Interface to the 7-segments translation table aka. char set.
  469. */
  470. static ssize_t show_map(struct device *dev, struct device_attribute *attr,
  471. char *buf)
  472. {
  473. memcpy(buf, &map_seg7, sizeof(map_seg7));
  474. return sizeof(map_seg7);
  475. }
  476. static ssize_t store_map(struct device *dev, struct device_attribute *attr,
  477. const char *buf, size_t cnt)
  478. {
  479. if (cnt != sizeof(map_seg7))
  480. return -EINVAL;
  481. memcpy(&map_seg7, buf, sizeof(map_seg7));
  482. return sizeof(map_seg7);
  483. }
  484. /* Interface to the LCD.
  485. */
  486. /* Reading /sys/../lineX will return the format string with its settings:
  487. *
  488. * Example:
  489. * cat ./line3
  490. * 888888888888
  491. * Linux Rocks!
  492. */
  493. static ssize_t show_line(struct device *dev, char *buf, int a, int b)
  494. {
  495. struct yealink_dev *yld;
  496. int i;
  497. down_read(&sysfs_rwsema);
  498. yld = dev_get_drvdata(dev);
  499. if (yld == NULL) {
  500. up_read(&sysfs_rwsema);
  501. return -ENODEV;
  502. }
  503. for (i = a; i < b; i++)
  504. *buf++ = lcdMap[i].type;
  505. *buf++ = '\n';
  506. for (i = a; i < b; i++)
  507. *buf++ = yld->lcdMap[i];
  508. *buf++ = '\n';
  509. *buf = 0;
  510. up_read(&sysfs_rwsema);
  511. return 3 + ((b - a) << 1);
  512. }
  513. static ssize_t show_line1(struct device *dev, struct device_attribute *attr,
  514. char *buf)
  515. {
  516. return show_line(dev, buf, LCD_LINE1_OFFSET, LCD_LINE2_OFFSET);
  517. }
  518. static ssize_t show_line2(struct device *dev, struct device_attribute *attr,
  519. char *buf)
  520. {
  521. return show_line(dev, buf, LCD_LINE2_OFFSET, LCD_LINE3_OFFSET);
  522. }
  523. static ssize_t show_line3(struct device *dev, struct device_attribute *attr,
  524. char *buf)
  525. {
  526. return show_line(dev, buf, LCD_LINE3_OFFSET, LCD_LINE4_OFFSET);
  527. }
  528. /* Writing to /sys/../lineX will set the coresponding LCD line.
  529. * - Excess characters are ignored.
  530. * - If less characters are written than allowed, the remaining digits are
  531. * unchanged.
  532. * - The '\n' or '\t' char is a placeholder, it does not overwrite the
  533. * original content.
  534. */
  535. static ssize_t store_line(struct device *dev, const char *buf, size_t count,
  536. int el, size_t len)
  537. {
  538. struct yealink_dev *yld;
  539. int i;
  540. down_write(&sysfs_rwsema);
  541. yld = dev_get_drvdata(dev);
  542. if (yld == NULL) {
  543. up_write(&sysfs_rwsema);
  544. return -ENODEV;
  545. }
  546. if (len > count)
  547. len = count;
  548. for (i = 0; i < len; i++)
  549. setChar(yld, el++, buf[i]);
  550. up_write(&sysfs_rwsema);
  551. return count;
  552. }
  553. static ssize_t store_line1(struct device *dev, struct device_attribute *attr,
  554. const char *buf, size_t count)
  555. {
  556. return store_line(dev, buf, count, LCD_LINE1_OFFSET, LCD_LINE1_SIZE);
  557. }
  558. static ssize_t store_line2(struct device *dev, struct device_attribute *attr,
  559. const char *buf, size_t count)
  560. {
  561. return store_line(dev, buf, count, LCD_LINE2_OFFSET, LCD_LINE2_SIZE);
  562. }
  563. static ssize_t store_line3(struct device *dev, struct device_attribute *attr,
  564. const char *buf, size_t count)
  565. {
  566. return store_line(dev, buf, count, LCD_LINE3_OFFSET, LCD_LINE3_SIZE);
  567. }
  568. /* Interface to visible and audible "icons", these include:
  569. * pictures on the LCD, the LED, and the dialtone signal.
  570. */
  571. /* Get a list of "switchable elements" with their current state. */
  572. static ssize_t get_icons(struct device *dev, struct device_attribute *attr,
  573. char *buf)
  574. {
  575. struct yealink_dev *yld;
  576. int i, ret = 1;
  577. down_read(&sysfs_rwsema);
  578. yld = dev_get_drvdata(dev);
  579. if (yld == NULL) {
  580. up_read(&sysfs_rwsema);
  581. return -ENODEV;
  582. }
  583. for (i = 0; i < ARRAY_SIZE(lcdMap); i++) {
  584. if (lcdMap[i].type != '.')
  585. continue;
  586. ret += sprintf(&buf[ret], "%s %s\n",
  587. yld->lcdMap[i] == ' ' ? " " : "on",
  588. lcdMap[i].u.p.name);
  589. }
  590. up_read(&sysfs_rwsema);
  591. return ret;
  592. }
  593. /* Change the visibility of a particular element. */
  594. static ssize_t set_icon(struct device *dev, const char *buf, size_t count,
  595. int chr)
  596. {
  597. struct yealink_dev *yld;
  598. int i;
  599. down_write(&sysfs_rwsema);
  600. yld = dev_get_drvdata(dev);
  601. if (yld == NULL) {
  602. up_write(&sysfs_rwsema);
  603. return -ENODEV;
  604. }
  605. for (i = 0; i < ARRAY_SIZE(lcdMap); i++) {
  606. if (lcdMap[i].type != '.')
  607. continue;
  608. if (strncmp(buf, lcdMap[i].u.p.name, count) == 0) {
  609. setChar(yld, i, chr);
  610. break;
  611. }
  612. }
  613. up_write(&sysfs_rwsema);
  614. return count;
  615. }
  616. static ssize_t show_icon(struct device *dev, struct device_attribute *attr,
  617. const char *buf, size_t count)
  618. {
  619. return set_icon(dev, buf, count, buf[0]);
  620. }
  621. static ssize_t hide_icon(struct device *dev, struct device_attribute *attr,
  622. const char *buf, size_t count)
  623. {
  624. return set_icon(dev, buf, count, ' ');
  625. }
  626. /* Upload a ringtone to the device.
  627. */
  628. /* Stores raw ringtone data in the phone */
  629. static ssize_t store_ringtone(struct device *dev,
  630. struct device_attribute *attr,
  631. const char *buf, size_t count)
  632. {
  633. struct yealink_dev *yld;
  634. down_write(&sysfs_rwsema);
  635. yld = dev_get_drvdata(dev);
  636. if (yld == NULL) {
  637. up_write(&sysfs_rwsema);
  638. return -ENODEV;
  639. }
  640. /* TODO locking with async usb control interface??? */
  641. yealink_set_ringtone(yld, (char *)buf, count);
  642. up_write(&sysfs_rwsema);
  643. return count;
  644. }
  645. #define _M444 S_IRUGO
  646. #define _M664 S_IRUGO|S_IWUSR|S_IWGRP
  647. #define _M220 S_IWUSR|S_IWGRP
  648. static DEVICE_ATTR(map_seg7 , _M664, show_map , store_map );
  649. static DEVICE_ATTR(line1 , _M664, show_line1 , store_line1 );
  650. static DEVICE_ATTR(line2 , _M664, show_line2 , store_line2 );
  651. static DEVICE_ATTR(line3 , _M664, show_line3 , store_line3 );
  652. static DEVICE_ATTR(get_icons , _M444, get_icons , NULL );
  653. static DEVICE_ATTR(show_icon , _M220, NULL , show_icon );
  654. static DEVICE_ATTR(hide_icon , _M220, NULL , hide_icon );
  655. static DEVICE_ATTR(ringtone , _M220, NULL , store_ringtone);
  656. static struct attribute *yld_attributes[] = {
  657. &dev_attr_line1.attr,
  658. &dev_attr_line2.attr,
  659. &dev_attr_line3.attr,
  660. &dev_attr_get_icons.attr,
  661. &dev_attr_show_icon.attr,
  662. &dev_attr_hide_icon.attr,
  663. &dev_attr_map_seg7.attr,
  664. &dev_attr_ringtone.attr,
  665. NULL
  666. };
  667. static struct attribute_group yld_attr_group = {
  668. .attrs = yld_attributes
  669. };
  670. /*******************************************************************************
  671. * Linux interface and usb initialisation
  672. ******************************************************************************/
  673. struct driver_info {
  674. char *name;
  675. };
  676. static const struct driver_info info_P1K = {
  677. .name = "Yealink usb-p1k",
  678. };
  679. static const struct usb_device_id usb_table [] = {
  680. {
  681. .match_flags = USB_DEVICE_ID_MATCH_DEVICE |
  682. USB_DEVICE_ID_MATCH_INT_INFO,
  683. .idVendor = 0x6993,
  684. .idProduct = 0xb001,
  685. .bInterfaceClass = USB_CLASS_HID,
  686. .bInterfaceSubClass = 0,
  687. .bInterfaceProtocol = 0,
  688. .driver_info = (kernel_ulong_t)&info_P1K
  689. },
  690. { }
  691. };
  692. static int usb_cleanup(struct yealink_dev *yld, int err)
  693. {
  694. if (yld == NULL)
  695. return err;
  696. usb_kill_urb(yld->urb_irq); /* parameter validation in core/urb */
  697. usb_kill_urb(yld->urb_ctl); /* parameter validation in core/urb */
  698. if (yld->idev) {
  699. if (err)
  700. input_free_device(yld->idev);
  701. else
  702. input_unregister_device(yld->idev);
  703. }
  704. if (yld->ctl_req)
  705. usb_buffer_free(yld->udev, sizeof(*(yld->ctl_req)),
  706. yld->ctl_req, yld->ctl_req_dma);
  707. if (yld->ctl_data)
  708. usb_buffer_free(yld->udev, USB_PKT_LEN,
  709. yld->ctl_data, yld->ctl_dma);
  710. if (yld->irq_data)
  711. usb_buffer_free(yld->udev, USB_PKT_LEN,
  712. yld->irq_data, yld->irq_dma);
  713. usb_free_urb(yld->urb_irq); /* parameter validation in core/urb */
  714. usb_free_urb(yld->urb_ctl); /* parameter validation in core/urb */
  715. kfree(yld);
  716. return err;
  717. }
  718. static void usb_disconnect(struct usb_interface *intf)
  719. {
  720. struct yealink_dev *yld;
  721. down_write(&sysfs_rwsema);
  722. yld = usb_get_intfdata(intf);
  723. sysfs_remove_group(&intf->dev.kobj, &yld_attr_group);
  724. usb_set_intfdata(intf, NULL);
  725. up_write(&sysfs_rwsema);
  726. usb_cleanup(yld, 0);
  727. }
  728. static int usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
  729. {
  730. struct usb_device *udev = interface_to_usbdev (intf);
  731. struct driver_info *nfo = (struct driver_info *)id->driver_info;
  732. struct usb_host_interface *interface;
  733. struct usb_endpoint_descriptor *endpoint;
  734. struct yealink_dev *yld;
  735. struct input_dev *input_dev;
  736. int ret, pipe, i;
  737. interface = intf->cur_altsetting;
  738. endpoint = &interface->endpoint[0].desc;
  739. if (!(endpoint->bEndpointAddress & USB_DIR_IN))
  740. return -EIO;
  741. if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
  742. return -EIO;
  743. yld = kzalloc(sizeof(struct yealink_dev), GFP_KERNEL);
  744. if (!yld)
  745. return -ENOMEM;
  746. yld->udev = udev;
  747. yld->idev = input_dev = input_allocate_device();
  748. if (!input_dev)
  749. return usb_cleanup(yld, -ENOMEM);
  750. /* allocate usb buffers */
  751. yld->irq_data = usb_buffer_alloc(udev, USB_PKT_LEN,
  752. SLAB_ATOMIC, &yld->irq_dma);
  753. if (yld->irq_data == NULL)
  754. return usb_cleanup(yld, -ENOMEM);
  755. yld->ctl_data = usb_buffer_alloc(udev, USB_PKT_LEN,
  756. SLAB_ATOMIC, &yld->ctl_dma);
  757. if (!yld->ctl_data)
  758. return usb_cleanup(yld, -ENOMEM);
  759. yld->ctl_req = usb_buffer_alloc(udev, sizeof(*(yld->ctl_req)),
  760. SLAB_ATOMIC, &yld->ctl_req_dma);
  761. if (yld->ctl_req == NULL)
  762. return usb_cleanup(yld, -ENOMEM);
  763. /* allocate urb structures */
  764. yld->urb_irq = usb_alloc_urb(0, GFP_KERNEL);
  765. if (yld->urb_irq == NULL)
  766. return usb_cleanup(yld, -ENOMEM);
  767. yld->urb_ctl = usb_alloc_urb(0, GFP_KERNEL);
  768. if (yld->urb_ctl == NULL)
  769. return usb_cleanup(yld, -ENOMEM);
  770. /* get a handle to the interrupt data pipe */
  771. pipe = usb_rcvintpipe(udev, endpoint->bEndpointAddress);
  772. ret = usb_maxpacket(udev, pipe, usb_pipeout(pipe));
  773. if (ret != USB_PKT_LEN)
  774. err("invalid payload size %d, expected %zd", ret, USB_PKT_LEN);
  775. /* initialise irq urb */
  776. usb_fill_int_urb(yld->urb_irq, udev, pipe, yld->irq_data,
  777. USB_PKT_LEN,
  778. urb_irq_callback,
  779. yld, endpoint->bInterval);
  780. yld->urb_irq->transfer_dma = yld->irq_dma;
  781. yld->urb_irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  782. yld->urb_irq->dev = udev;
  783. /* initialise ctl urb */
  784. yld->ctl_req->bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE |
  785. USB_DIR_OUT;
  786. yld->ctl_req->bRequest = USB_REQ_SET_CONFIGURATION;
  787. yld->ctl_req->wValue = cpu_to_le16(0x200);
  788. yld->ctl_req->wIndex = cpu_to_le16(interface->desc.bInterfaceNumber);
  789. yld->ctl_req->wLength = cpu_to_le16(USB_PKT_LEN);
  790. usb_fill_control_urb(yld->urb_ctl, udev, usb_sndctrlpipe(udev, 0),
  791. (void *)yld->ctl_req, yld->ctl_data, USB_PKT_LEN,
  792. urb_ctl_callback, yld);
  793. yld->urb_ctl->setup_dma = yld->ctl_req_dma;
  794. yld->urb_ctl->transfer_dma = yld->ctl_dma;
  795. yld->urb_ctl->transfer_flags |= URB_NO_SETUP_DMA_MAP |
  796. 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->cdev.dev = &intf->dev;
  806. input_dev->private = 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(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. input_register_device(yld->idev);
  821. usb_set_intfdata(intf, yld);
  822. /* clear visible elements */
  823. for (i = 0; i < ARRAY_SIZE(lcdMap); i++)
  824. setChar(yld, i, ' ');
  825. /* display driver version on LCD line 3 */
  826. store_line3(&intf->dev, NULL,
  827. DRIVER_VERSION, sizeof(DRIVER_VERSION));
  828. /* Register sysfs hooks (don't care about failure) */
  829. ret = sysfs_create_group(&intf->dev.kobj, &yld_attr_group);
  830. return 0;
  831. }
  832. static struct usb_driver yealink_driver = {
  833. .name = "yealink",
  834. .probe = usb_probe,
  835. .disconnect = usb_disconnect,
  836. .id_table = usb_table,
  837. };
  838. static int __init yealink_dev_init(void)
  839. {
  840. int ret = usb_register(&yealink_driver);
  841. if (ret == 0)
  842. info(DRIVER_DESC ":" DRIVER_VERSION);
  843. return ret;
  844. }
  845. static void __exit yealink_dev_exit(void)
  846. {
  847. usb_deregister(&yealink_driver);
  848. }
  849. module_init(yealink_dev_init);
  850. module_exit(yealink_dev_exit);
  851. MODULE_DEVICE_TABLE (usb, usb_table);
  852. MODULE_AUTHOR(DRIVER_AUTHOR);
  853. MODULE_DESCRIPTION(DRIVER_DESC);
  854. MODULE_LICENSE("GPL");