yealink.c 25 KB

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