yealink.c 25 KB

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