cm109.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  1. /*
  2. * Driver for the VoIP USB phones with CM109 chipsets.
  3. *
  4. * Copyright (C) 2007 - 2008 Alfred E. Heggestad <aeh@db.org>
  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, version 2.
  9. */
  10. /*
  11. * Tested devices:
  12. * - Komunikate KIP1000
  13. * - Genius G-talk
  14. * - Allied-Telesis Corega USBPH01
  15. * - ...
  16. *
  17. * This driver is based on the yealink.c driver
  18. *
  19. * Thanks to:
  20. * - Authors of yealink.c
  21. * - Thomas Reitmayr
  22. * - Oliver Neukum for good review comments and code
  23. * - Shaun Jackman <sjackman@gmail.com> for Genius G-talk keymap
  24. * - Dmitry Torokhov for valuable input and review
  25. *
  26. * Todo:
  27. * - Read/write EEPROM
  28. */
  29. #include <linux/kernel.h>
  30. #include <linux/init.h>
  31. #include <linux/slab.h>
  32. #include <linux/module.h>
  33. #include <linux/moduleparam.h>
  34. #include <linux/rwsem.h>
  35. #include <linux/usb/input.h>
  36. #define DRIVER_VERSION "20080805"
  37. #define DRIVER_AUTHOR "Alfred E. Heggestad"
  38. #define DRIVER_DESC "CM109 phone driver"
  39. static char *phone = "kip1000";
  40. module_param(phone, charp, S_IRUSR);
  41. MODULE_PARM_DESC(phone, "Phone name {kip1000, gtalk, usbph01, atcom}");
  42. enum {
  43. /* HID Registers */
  44. HID_IR0 = 0x00, /* Record/Playback-mute button, Volume up/down */
  45. HID_IR1 = 0x01, /* GPI, generic registers or EEPROM_DATA0 */
  46. HID_IR2 = 0x02, /* Generic registers or EEPROM_DATA1 */
  47. HID_IR3 = 0x03, /* Generic registers or EEPROM_CTRL */
  48. HID_OR0 = 0x00, /* Mapping control, buzzer, SPDIF (offset 0x04) */
  49. HID_OR1 = 0x01, /* GPO - General Purpose Output */
  50. HID_OR2 = 0x02, /* Set GPIO to input/output mode */
  51. HID_OR3 = 0x03, /* SPDIF status channel or EEPROM_CTRL */
  52. /* HID_IR0 */
  53. RECORD_MUTE = 1 << 3,
  54. PLAYBACK_MUTE = 1 << 2,
  55. VOLUME_DOWN = 1 << 1,
  56. VOLUME_UP = 1 << 0,
  57. /* HID_OR0 */
  58. /* bits 7-6
  59. 0: HID_OR1-2 are used for GPO; HID_OR0, 3 are used for buzzer
  60. and SPDIF
  61. 1: HID_OR0-3 are used as generic HID registers
  62. 2: Values written to HID_OR0-3 are also mapped to MCU_CTRL,
  63. EEPROM_DATA0-1, EEPROM_CTRL (see Note)
  64. 3: Reserved
  65. */
  66. HID_OR_GPO_BUZ_SPDIF = 0 << 6,
  67. HID_OR_GENERIC_HID_REG = 1 << 6,
  68. HID_OR_MAP_MCU_EEPROM = 2 << 6,
  69. BUZZER_ON = 1 << 5,
  70. /* up to 256 normal keys, up to 15 special key combinations */
  71. KEYMAP_SIZE = 256 + 15,
  72. };
  73. /* CM109 protocol packet */
  74. struct cm109_ctl_packet {
  75. u8 byte[4];
  76. } __attribute__ ((packed));
  77. enum { USB_PKT_LEN = sizeof(struct cm109_ctl_packet) };
  78. /* CM109 device structure */
  79. struct cm109_dev {
  80. struct input_dev *idev; /* input device */
  81. struct usb_device *udev; /* usb device */
  82. struct usb_interface *intf;
  83. /* irq input channel */
  84. struct cm109_ctl_packet *irq_data;
  85. dma_addr_t irq_dma;
  86. struct urb *urb_irq;
  87. /* control output channel */
  88. struct cm109_ctl_packet *ctl_data;
  89. dma_addr_t ctl_dma;
  90. struct usb_ctrlrequest *ctl_req;
  91. struct urb *urb_ctl;
  92. /*
  93. * The 3 bitfields below are protected by ctl_submit_lock.
  94. * They have to be separate since they are accessed from IRQ
  95. * context.
  96. */
  97. unsigned irq_urb_pending:1; /* irq_urb is in flight */
  98. unsigned ctl_urb_pending:1; /* ctl_urb is in flight */
  99. unsigned buzzer_pending:1; /* need to issue buzz command */
  100. spinlock_t ctl_submit_lock;
  101. unsigned char buzzer_state; /* on/off */
  102. /* flags */
  103. unsigned open:1;
  104. unsigned resetting:1;
  105. unsigned shutdown:1;
  106. /* This mutex protects writes to the above flags */
  107. struct mutex pm_mutex;
  108. unsigned short keymap[KEYMAP_SIZE];
  109. char phys[64]; /* physical device path */
  110. int key_code; /* last reported key */
  111. int keybit; /* 0=new scan 1,2,4,8=scan columns */
  112. u8 gpi; /* Cached value of GPI (high nibble) */
  113. };
  114. /******************************************************************************
  115. * CM109 key interface
  116. *****************************************************************************/
  117. static unsigned short special_keymap(int code)
  118. {
  119. if (code > 0xff) {
  120. switch (code - 0xff) {
  121. case RECORD_MUTE: return KEY_MICMUTE;
  122. case PLAYBACK_MUTE: return KEY_MUTE;
  123. case VOLUME_DOWN: return KEY_VOLUMEDOWN;
  124. case VOLUME_UP: return KEY_VOLUMEUP;
  125. }
  126. }
  127. return KEY_RESERVED;
  128. }
  129. /* Map device buttons to internal key events.
  130. *
  131. * The "up" and "down" keys, are symbolised by arrows on the button.
  132. * The "pickup" and "hangup" keys are symbolised by a green and red phone
  133. * on the button.
  134. Komunikate KIP1000 Keyboard Matrix
  135. -> -- 1 -- 2 -- 3 --> GPI pin 4 (0x10)
  136. | | | |
  137. <- -- 4 -- 5 -- 6 --> GPI pin 5 (0x20)
  138. | | | |
  139. END - 7 -- 8 -- 9 --> GPI pin 6 (0x40)
  140. | | | |
  141. OK -- * -- 0 -- # --> GPI pin 7 (0x80)
  142. | | | |
  143. /|\ /|\ /|\ /|\
  144. | | | |
  145. GPO
  146. pin: 3 2 1 0
  147. 0x8 0x4 0x2 0x1
  148. */
  149. static unsigned short keymap_kip1000(int scancode)
  150. {
  151. switch (scancode) { /* phone key: */
  152. case 0x82: return KEY_NUMERIC_0; /* 0 */
  153. case 0x14: return KEY_NUMERIC_1; /* 1 */
  154. case 0x12: return KEY_NUMERIC_2; /* 2 */
  155. case 0x11: return KEY_NUMERIC_3; /* 3 */
  156. case 0x24: return KEY_NUMERIC_4; /* 4 */
  157. case 0x22: return KEY_NUMERIC_5; /* 5 */
  158. case 0x21: return KEY_NUMERIC_6; /* 6 */
  159. case 0x44: return KEY_NUMERIC_7; /* 7 */
  160. case 0x42: return KEY_NUMERIC_8; /* 8 */
  161. case 0x41: return KEY_NUMERIC_9; /* 9 */
  162. case 0x81: return KEY_NUMERIC_POUND; /* # */
  163. case 0x84: return KEY_NUMERIC_STAR; /* * */
  164. case 0x88: return KEY_ENTER; /* pickup */
  165. case 0x48: return KEY_ESC; /* hangup */
  166. case 0x28: return KEY_LEFT; /* IN */
  167. case 0x18: return KEY_RIGHT; /* OUT */
  168. default: return special_keymap(scancode);
  169. }
  170. }
  171. /*
  172. Contributed by Shaun Jackman <sjackman@gmail.com>
  173. Genius G-Talk keyboard matrix
  174. 0 1 2 3
  175. 4: 0 4 8 Talk
  176. 5: 1 5 9 End
  177. 6: 2 6 # Up
  178. 7: 3 7 * Down
  179. */
  180. static unsigned short keymap_gtalk(int scancode)
  181. {
  182. switch (scancode) {
  183. case 0x11: return KEY_NUMERIC_0;
  184. case 0x21: return KEY_NUMERIC_1;
  185. case 0x41: return KEY_NUMERIC_2;
  186. case 0x81: return KEY_NUMERIC_3;
  187. case 0x12: return KEY_NUMERIC_4;
  188. case 0x22: return KEY_NUMERIC_5;
  189. case 0x42: return KEY_NUMERIC_6;
  190. case 0x82: return KEY_NUMERIC_7;
  191. case 0x14: return KEY_NUMERIC_8;
  192. case 0x24: return KEY_NUMERIC_9;
  193. case 0x44: return KEY_NUMERIC_POUND; /* # */
  194. case 0x84: return KEY_NUMERIC_STAR; /* * */
  195. case 0x18: return KEY_ENTER; /* Talk (green handset) */
  196. case 0x28: return KEY_ESC; /* End (red handset) */
  197. case 0x48: return KEY_UP; /* Menu up (rocker switch) */
  198. case 0x88: return KEY_DOWN; /* Menu down (rocker switch) */
  199. default: return special_keymap(scancode);
  200. }
  201. }
  202. /*
  203. * Keymap for Allied-Telesis Corega USBPH01
  204. * http://www.alliedtelesis-corega.com/2/1344/1437/1360/chprd.html
  205. *
  206. * Contributed by july@nat.bg
  207. */
  208. static unsigned short keymap_usbph01(int scancode)
  209. {
  210. switch (scancode) {
  211. case 0x11: return KEY_NUMERIC_0; /* 0 */
  212. case 0x21: return KEY_NUMERIC_1; /* 1 */
  213. case 0x41: return KEY_NUMERIC_2; /* 2 */
  214. case 0x81: return KEY_NUMERIC_3; /* 3 */
  215. case 0x12: return KEY_NUMERIC_4; /* 4 */
  216. case 0x22: return KEY_NUMERIC_5; /* 5 */
  217. case 0x42: return KEY_NUMERIC_6; /* 6 */
  218. case 0x82: return KEY_NUMERIC_7; /* 7 */
  219. case 0x14: return KEY_NUMERIC_8; /* 8 */
  220. case 0x24: return KEY_NUMERIC_9; /* 9 */
  221. case 0x44: return KEY_NUMERIC_POUND; /* # */
  222. case 0x84: return KEY_NUMERIC_STAR; /* * */
  223. case 0x18: return KEY_ENTER; /* pickup */
  224. case 0x28: return KEY_ESC; /* hangup */
  225. case 0x48: return KEY_LEFT; /* IN */
  226. case 0x88: return KEY_RIGHT; /* OUT */
  227. default: return special_keymap(scancode);
  228. }
  229. }
  230. /*
  231. * Keymap for ATCom AU-100
  232. * http://www.atcom.cn/products.html
  233. * http://www.packetizer.com/products/au100/
  234. * http://www.voip-info.org/wiki/view/AU-100
  235. *
  236. * Contributed by daniel@gimpelevich.san-francisco.ca.us
  237. */
  238. static unsigned short keymap_atcom(int scancode)
  239. {
  240. switch (scancode) { /* phone key: */
  241. case 0x82: return KEY_NUMERIC_0; /* 0 */
  242. case 0x11: return KEY_NUMERIC_1; /* 1 */
  243. case 0x12: return KEY_NUMERIC_2; /* 2 */
  244. case 0x14: return KEY_NUMERIC_3; /* 3 */
  245. case 0x21: return KEY_NUMERIC_4; /* 4 */
  246. case 0x22: return KEY_NUMERIC_5; /* 5 */
  247. case 0x24: return KEY_NUMERIC_6; /* 6 */
  248. case 0x41: return KEY_NUMERIC_7; /* 7 */
  249. case 0x42: return KEY_NUMERIC_8; /* 8 */
  250. case 0x44: return KEY_NUMERIC_9; /* 9 */
  251. case 0x84: return KEY_NUMERIC_POUND; /* # */
  252. case 0x81: return KEY_NUMERIC_STAR; /* * */
  253. case 0x18: return KEY_ENTER; /* pickup */
  254. case 0x28: return KEY_ESC; /* hangup */
  255. case 0x48: return KEY_LEFT; /* left arrow */
  256. case 0x88: return KEY_RIGHT; /* right arrow */
  257. default: return special_keymap(scancode);
  258. }
  259. }
  260. static unsigned short (*keymap)(int) = keymap_kip1000;
  261. /*
  262. * Completes a request by converting the data into events for the
  263. * input subsystem.
  264. */
  265. static void report_key(struct cm109_dev *dev, int key)
  266. {
  267. struct input_dev *idev = dev->idev;
  268. if (dev->key_code >= 0) {
  269. /* old key up */
  270. input_report_key(idev, dev->key_code, 0);
  271. }
  272. dev->key_code = key;
  273. if (key >= 0) {
  274. /* new valid key */
  275. input_report_key(idev, key, 1);
  276. }
  277. input_sync(idev);
  278. }
  279. /*
  280. * Converts data of special key presses (volume, mute) into events
  281. * for the input subsystem, sends press-n-release for mute keys.
  282. */
  283. static void cm109_report_special(struct cm109_dev *dev)
  284. {
  285. static const u8 autorelease = RECORD_MUTE | PLAYBACK_MUTE;
  286. struct input_dev *idev = dev->idev;
  287. u8 data = dev->irq_data->byte[HID_IR0];
  288. unsigned short keycode;
  289. int i;
  290. for (i = 0; i < 4; i++) {
  291. keycode = dev->keymap[0xff + BIT(i)];
  292. if (keycode == KEY_RESERVED)
  293. continue;
  294. input_report_key(idev, keycode, data & BIT(i));
  295. if (data & autorelease & BIT(i)) {
  296. input_sync(idev);
  297. input_report_key(idev, keycode, 0);
  298. }
  299. }
  300. input_sync(idev);
  301. }
  302. /******************************************************************************
  303. * CM109 usb communication interface
  304. *****************************************************************************/
  305. static void cm109_submit_buzz_toggle(struct cm109_dev *dev)
  306. {
  307. int error;
  308. if (dev->buzzer_state)
  309. dev->ctl_data->byte[HID_OR0] |= BUZZER_ON;
  310. else
  311. dev->ctl_data->byte[HID_OR0] &= ~BUZZER_ON;
  312. error = usb_submit_urb(dev->urb_ctl, GFP_ATOMIC);
  313. if (error)
  314. dev_err(&dev->intf->dev,
  315. "%s: usb_submit_urb (urb_ctl) failed %d\n",
  316. __func__, error);
  317. }
  318. /*
  319. * IRQ handler
  320. */
  321. static void cm109_urb_irq_callback(struct urb *urb)
  322. {
  323. struct cm109_dev *dev = urb->context;
  324. const int status = urb->status;
  325. int error;
  326. unsigned long flags;
  327. dev_dbg(&dev->intf->dev, "### URB IRQ: [0x%02x 0x%02x 0x%02x 0x%02x] keybit=0x%02x\n",
  328. dev->irq_data->byte[0],
  329. dev->irq_data->byte[1],
  330. dev->irq_data->byte[2],
  331. dev->irq_data->byte[3],
  332. dev->keybit);
  333. if (status) {
  334. if (status == -ESHUTDOWN)
  335. return;
  336. dev_err_ratelimited(&dev->intf->dev, "%s: urb status %d\n",
  337. __func__, status);
  338. goto out;
  339. }
  340. /* Special keys */
  341. cm109_report_special(dev);
  342. /* Scan key column */
  343. if (dev->keybit == 0xf) {
  344. /* Any changes ? */
  345. if ((dev->gpi & 0xf0) == (dev->irq_data->byte[HID_IR1] & 0xf0))
  346. goto out;
  347. dev->gpi = dev->irq_data->byte[HID_IR1] & 0xf0;
  348. dev->keybit = 0x1;
  349. } else {
  350. report_key(dev, dev->keymap[dev->irq_data->byte[HID_IR1]]);
  351. dev->keybit <<= 1;
  352. if (dev->keybit > 0x8)
  353. dev->keybit = 0xf;
  354. }
  355. out:
  356. spin_lock_irqsave(&dev->ctl_submit_lock, flags);
  357. dev->irq_urb_pending = 0;
  358. if (likely(!dev->shutdown)) {
  359. if (dev->buzzer_state)
  360. dev->ctl_data->byte[HID_OR0] |= BUZZER_ON;
  361. else
  362. dev->ctl_data->byte[HID_OR0] &= ~BUZZER_ON;
  363. dev->ctl_data->byte[HID_OR1] = dev->keybit;
  364. dev->ctl_data->byte[HID_OR2] = dev->keybit;
  365. dev->buzzer_pending = 0;
  366. dev->ctl_urb_pending = 1;
  367. error = usb_submit_urb(dev->urb_ctl, GFP_ATOMIC);
  368. if (error)
  369. dev_err(&dev->intf->dev,
  370. "%s: usb_submit_urb (urb_ctl) failed %d\n",
  371. __func__, error);
  372. }
  373. spin_unlock_irqrestore(&dev->ctl_submit_lock, flags);
  374. }
  375. static void cm109_urb_ctl_callback(struct urb *urb)
  376. {
  377. struct cm109_dev *dev = urb->context;
  378. const int status = urb->status;
  379. int error;
  380. unsigned long flags;
  381. dev_dbg(&dev->intf->dev, "### URB CTL: [0x%02x 0x%02x 0x%02x 0x%02x]\n",
  382. dev->ctl_data->byte[0],
  383. dev->ctl_data->byte[1],
  384. dev->ctl_data->byte[2],
  385. dev->ctl_data->byte[3]);
  386. if (status) {
  387. if (status == -ESHUTDOWN)
  388. return;
  389. dev_err_ratelimited(&dev->intf->dev, "%s: urb status %d\n",
  390. __func__, status);
  391. }
  392. spin_lock_irqsave(&dev->ctl_submit_lock, flags);
  393. dev->ctl_urb_pending = 0;
  394. if (likely(!dev->shutdown)) {
  395. if (dev->buzzer_pending || status) {
  396. dev->buzzer_pending = 0;
  397. dev->ctl_urb_pending = 1;
  398. cm109_submit_buzz_toggle(dev);
  399. } else if (likely(!dev->irq_urb_pending)) {
  400. /* ask for key data */
  401. dev->irq_urb_pending = 1;
  402. error = usb_submit_urb(dev->urb_irq, GFP_ATOMIC);
  403. if (error)
  404. dev_err(&dev->intf->dev,
  405. "%s: usb_submit_urb (urb_irq) failed %d\n",
  406. __func__, error);
  407. }
  408. }
  409. spin_unlock_irqrestore(&dev->ctl_submit_lock, flags);
  410. }
  411. static void cm109_toggle_buzzer_async(struct cm109_dev *dev)
  412. {
  413. unsigned long flags;
  414. spin_lock_irqsave(&dev->ctl_submit_lock, flags);
  415. if (dev->ctl_urb_pending) {
  416. /* URB completion will resubmit */
  417. dev->buzzer_pending = 1;
  418. } else {
  419. dev->ctl_urb_pending = 1;
  420. cm109_submit_buzz_toggle(dev);
  421. }
  422. spin_unlock_irqrestore(&dev->ctl_submit_lock, flags);
  423. }
  424. static void cm109_toggle_buzzer_sync(struct cm109_dev *dev, int on)
  425. {
  426. int error;
  427. if (on)
  428. dev->ctl_data->byte[HID_OR0] |= BUZZER_ON;
  429. else
  430. dev->ctl_data->byte[HID_OR0] &= ~BUZZER_ON;
  431. error = usb_control_msg(dev->udev,
  432. usb_sndctrlpipe(dev->udev, 0),
  433. dev->ctl_req->bRequest,
  434. dev->ctl_req->bRequestType,
  435. le16_to_cpu(dev->ctl_req->wValue),
  436. le16_to_cpu(dev->ctl_req->wIndex),
  437. dev->ctl_data,
  438. USB_PKT_LEN, USB_CTRL_SET_TIMEOUT);
  439. if (error < 0 && error != -EINTR)
  440. dev_err(&dev->intf->dev, "%s: usb_control_msg() failed %d\n",
  441. __func__, error);
  442. }
  443. static void cm109_stop_traffic(struct cm109_dev *dev)
  444. {
  445. dev->shutdown = 1;
  446. /*
  447. * Make sure other CPUs see this
  448. */
  449. smp_wmb();
  450. usb_kill_urb(dev->urb_ctl);
  451. usb_kill_urb(dev->urb_irq);
  452. cm109_toggle_buzzer_sync(dev, 0);
  453. dev->shutdown = 0;
  454. smp_wmb();
  455. }
  456. static void cm109_restore_state(struct cm109_dev *dev)
  457. {
  458. if (dev->open) {
  459. /*
  460. * Restore buzzer state.
  461. * This will also kick regular URB submission
  462. */
  463. cm109_toggle_buzzer_async(dev);
  464. }
  465. }
  466. /******************************************************************************
  467. * input event interface
  468. *****************************************************************************/
  469. static int cm109_input_open(struct input_dev *idev)
  470. {
  471. struct cm109_dev *dev = input_get_drvdata(idev);
  472. int error;
  473. error = usb_autopm_get_interface(dev->intf);
  474. if (error < 0) {
  475. dev_err(&idev->dev, "%s - cannot autoresume, result %d\n",
  476. __func__, error);
  477. return error;
  478. }
  479. mutex_lock(&dev->pm_mutex);
  480. dev->buzzer_state = 0;
  481. dev->key_code = -1; /* no keys pressed */
  482. dev->keybit = 0xf;
  483. /* issue INIT */
  484. dev->ctl_data->byte[HID_OR0] = HID_OR_GPO_BUZ_SPDIF;
  485. dev->ctl_data->byte[HID_OR1] = dev->keybit;
  486. dev->ctl_data->byte[HID_OR2] = dev->keybit;
  487. dev->ctl_data->byte[HID_OR3] = 0x00;
  488. error = usb_submit_urb(dev->urb_ctl, GFP_KERNEL);
  489. if (error)
  490. dev_err(&dev->intf->dev, "%s: usb_submit_urb (urb_ctl) failed %d\n",
  491. __func__, error);
  492. else
  493. dev->open = 1;
  494. mutex_unlock(&dev->pm_mutex);
  495. if (error)
  496. usb_autopm_put_interface(dev->intf);
  497. return error;
  498. }
  499. static void cm109_input_close(struct input_dev *idev)
  500. {
  501. struct cm109_dev *dev = input_get_drvdata(idev);
  502. mutex_lock(&dev->pm_mutex);
  503. /*
  504. * Once we are here event delivery is stopped so we
  505. * don't need to worry about someone starting buzzer
  506. * again
  507. */
  508. cm109_stop_traffic(dev);
  509. dev->open = 0;
  510. mutex_unlock(&dev->pm_mutex);
  511. usb_autopm_put_interface(dev->intf);
  512. }
  513. static int cm109_input_ev(struct input_dev *idev, unsigned int type,
  514. unsigned int code, int value)
  515. {
  516. struct cm109_dev *dev = input_get_drvdata(idev);
  517. dev_dbg(&dev->intf->dev,
  518. "input_ev: type=%u code=%u value=%d\n", type, code, value);
  519. if (type != EV_SND)
  520. return -EINVAL;
  521. switch (code) {
  522. case SND_TONE:
  523. case SND_BELL:
  524. dev->buzzer_state = !!value;
  525. if (!dev->resetting)
  526. cm109_toggle_buzzer_async(dev);
  527. return 0;
  528. default:
  529. return -EINVAL;
  530. }
  531. }
  532. /******************************************************************************
  533. * Linux interface and usb initialisation
  534. *****************************************************************************/
  535. struct driver_info {
  536. char *name;
  537. };
  538. static const struct driver_info info_cm109 = {
  539. .name = "CM109 USB driver",
  540. };
  541. enum {
  542. VENDOR_ID = 0x0d8c, /* C-Media Electronics */
  543. PRODUCT_ID_CM109 = 0x000e, /* CM109 defines range 0x0008 - 0x000f */
  544. };
  545. /* table of devices that work with this driver */
  546. static const struct usb_device_id cm109_usb_table[] = {
  547. {
  548. .match_flags = USB_DEVICE_ID_MATCH_DEVICE |
  549. USB_DEVICE_ID_MATCH_INT_INFO,
  550. .idVendor = VENDOR_ID,
  551. .idProduct = PRODUCT_ID_CM109,
  552. .bInterfaceClass = USB_CLASS_HID,
  553. .bInterfaceSubClass = 0,
  554. .bInterfaceProtocol = 0,
  555. .driver_info = (kernel_ulong_t) &info_cm109
  556. },
  557. /* you can add more devices here with product ID 0x0008 - 0x000f */
  558. { }
  559. };
  560. static void cm109_usb_cleanup(struct cm109_dev *dev)
  561. {
  562. kfree(dev->ctl_req);
  563. if (dev->ctl_data)
  564. usb_free_coherent(dev->udev, USB_PKT_LEN,
  565. dev->ctl_data, dev->ctl_dma);
  566. if (dev->irq_data)
  567. usb_free_coherent(dev->udev, USB_PKT_LEN,
  568. dev->irq_data, dev->irq_dma);
  569. usb_free_urb(dev->urb_irq); /* parameter validation in core/urb */
  570. usb_free_urb(dev->urb_ctl); /* parameter validation in core/urb */
  571. kfree(dev);
  572. }
  573. static void cm109_usb_disconnect(struct usb_interface *interface)
  574. {
  575. struct cm109_dev *dev = usb_get_intfdata(interface);
  576. usb_set_intfdata(interface, NULL);
  577. input_unregister_device(dev->idev);
  578. cm109_usb_cleanup(dev);
  579. }
  580. static int cm109_usb_probe(struct usb_interface *intf,
  581. const struct usb_device_id *id)
  582. {
  583. struct usb_device *udev = interface_to_usbdev(intf);
  584. struct driver_info *nfo = (struct driver_info *)id->driver_info;
  585. struct usb_host_interface *interface;
  586. struct usb_endpoint_descriptor *endpoint;
  587. struct cm109_dev *dev;
  588. struct input_dev *input_dev = NULL;
  589. int ret, pipe, i;
  590. int error = -ENOMEM;
  591. interface = intf->cur_altsetting;
  592. if (interface->desc.bNumEndpoints < 1)
  593. return -ENODEV;
  594. endpoint = &interface->endpoint[0].desc;
  595. if (!usb_endpoint_is_int_in(endpoint))
  596. return -ENODEV;
  597. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  598. if (!dev)
  599. return -ENOMEM;
  600. spin_lock_init(&dev->ctl_submit_lock);
  601. mutex_init(&dev->pm_mutex);
  602. dev->udev = udev;
  603. dev->intf = intf;
  604. dev->idev = input_dev = input_allocate_device();
  605. if (!input_dev)
  606. goto err_out;
  607. /* allocate usb buffers */
  608. dev->irq_data = usb_alloc_coherent(udev, USB_PKT_LEN,
  609. GFP_KERNEL, &dev->irq_dma);
  610. if (!dev->irq_data)
  611. goto err_out;
  612. dev->ctl_data = usb_alloc_coherent(udev, USB_PKT_LEN,
  613. GFP_KERNEL, &dev->ctl_dma);
  614. if (!dev->ctl_data)
  615. goto err_out;
  616. dev->ctl_req = kmalloc(sizeof(*(dev->ctl_req)), GFP_KERNEL);
  617. if (!dev->ctl_req)
  618. goto err_out;
  619. /* allocate urb structures */
  620. dev->urb_irq = usb_alloc_urb(0, GFP_KERNEL);
  621. if (!dev->urb_irq)
  622. goto err_out;
  623. dev->urb_ctl = usb_alloc_urb(0, GFP_KERNEL);
  624. if (!dev->urb_ctl)
  625. goto err_out;
  626. /* get a handle to the interrupt data pipe */
  627. pipe = usb_rcvintpipe(udev, endpoint->bEndpointAddress);
  628. ret = usb_maxpacket(udev, pipe, usb_pipeout(pipe));
  629. if (ret != USB_PKT_LEN)
  630. dev_err(&intf->dev, "invalid payload size %d, expected %d\n",
  631. ret, USB_PKT_LEN);
  632. /* initialise irq urb */
  633. usb_fill_int_urb(dev->urb_irq, udev, pipe, dev->irq_data,
  634. USB_PKT_LEN,
  635. cm109_urb_irq_callback, dev, endpoint->bInterval);
  636. dev->urb_irq->transfer_dma = dev->irq_dma;
  637. dev->urb_irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  638. dev->urb_irq->dev = udev;
  639. /* initialise ctl urb */
  640. dev->ctl_req->bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE |
  641. USB_DIR_OUT;
  642. dev->ctl_req->bRequest = USB_REQ_SET_CONFIGURATION;
  643. dev->ctl_req->wValue = cpu_to_le16(0x200);
  644. dev->ctl_req->wIndex = cpu_to_le16(interface->desc.bInterfaceNumber);
  645. dev->ctl_req->wLength = cpu_to_le16(USB_PKT_LEN);
  646. usb_fill_control_urb(dev->urb_ctl, udev, usb_sndctrlpipe(udev, 0),
  647. (void *)dev->ctl_req, dev->ctl_data, USB_PKT_LEN,
  648. cm109_urb_ctl_callback, dev);
  649. dev->urb_ctl->transfer_dma = dev->ctl_dma;
  650. dev->urb_ctl->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  651. dev->urb_ctl->dev = udev;
  652. /* find out the physical bus location */
  653. usb_make_path(udev, dev->phys, sizeof(dev->phys));
  654. strlcat(dev->phys, "/input0", sizeof(dev->phys));
  655. /* register settings for the input device */
  656. input_dev->name = nfo->name;
  657. input_dev->phys = dev->phys;
  658. usb_to_input_id(udev, &input_dev->id);
  659. input_dev->dev.parent = &intf->dev;
  660. input_set_drvdata(input_dev, dev);
  661. input_dev->open = cm109_input_open;
  662. input_dev->close = cm109_input_close;
  663. input_dev->event = cm109_input_ev;
  664. input_dev->keycode = dev->keymap;
  665. input_dev->keycodesize = sizeof(unsigned char);
  666. input_dev->keycodemax = ARRAY_SIZE(dev->keymap);
  667. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_SND);
  668. input_dev->sndbit[0] = BIT_MASK(SND_BELL) | BIT_MASK(SND_TONE);
  669. /* register available key events */
  670. for (i = 0; i < KEYMAP_SIZE; i++) {
  671. unsigned short k = keymap(i);
  672. dev->keymap[i] = k;
  673. __set_bit(k, input_dev->keybit);
  674. }
  675. __clear_bit(KEY_RESERVED, input_dev->keybit);
  676. error = input_register_device(dev->idev);
  677. if (error)
  678. goto err_out;
  679. usb_set_intfdata(intf, dev);
  680. return 0;
  681. err_out:
  682. input_free_device(input_dev);
  683. cm109_usb_cleanup(dev);
  684. return error;
  685. }
  686. static int cm109_usb_suspend(struct usb_interface *intf, pm_message_t message)
  687. {
  688. struct cm109_dev *dev = usb_get_intfdata(intf);
  689. dev_info(&intf->dev, "cm109: usb_suspend (event=%d)\n", message.event);
  690. mutex_lock(&dev->pm_mutex);
  691. cm109_stop_traffic(dev);
  692. mutex_unlock(&dev->pm_mutex);
  693. return 0;
  694. }
  695. static int cm109_usb_resume(struct usb_interface *intf)
  696. {
  697. struct cm109_dev *dev = usb_get_intfdata(intf);
  698. dev_info(&intf->dev, "cm109: usb_resume\n");
  699. mutex_lock(&dev->pm_mutex);
  700. cm109_restore_state(dev);
  701. mutex_unlock(&dev->pm_mutex);
  702. return 0;
  703. }
  704. static int cm109_usb_pre_reset(struct usb_interface *intf)
  705. {
  706. struct cm109_dev *dev = usb_get_intfdata(intf);
  707. mutex_lock(&dev->pm_mutex);
  708. /*
  709. * Make sure input events don't try to toggle buzzer
  710. * while we are resetting
  711. */
  712. dev->resetting = 1;
  713. smp_wmb();
  714. cm109_stop_traffic(dev);
  715. return 0;
  716. }
  717. static int cm109_usb_post_reset(struct usb_interface *intf)
  718. {
  719. struct cm109_dev *dev = usb_get_intfdata(intf);
  720. dev->resetting = 0;
  721. smp_wmb();
  722. cm109_restore_state(dev);
  723. mutex_unlock(&dev->pm_mutex);
  724. return 0;
  725. }
  726. static struct usb_driver cm109_driver = {
  727. .name = "cm109",
  728. .probe = cm109_usb_probe,
  729. .disconnect = cm109_usb_disconnect,
  730. .suspend = cm109_usb_suspend,
  731. .resume = cm109_usb_resume,
  732. .reset_resume = cm109_usb_resume,
  733. .pre_reset = cm109_usb_pre_reset,
  734. .post_reset = cm109_usb_post_reset,
  735. .id_table = cm109_usb_table,
  736. .supports_autosuspend = 1,
  737. };
  738. static int __init cm109_select_keymap(void)
  739. {
  740. /* Load the phone keymap */
  741. if (!strcasecmp(phone, "kip1000")) {
  742. keymap = keymap_kip1000;
  743. printk(KERN_INFO KBUILD_MODNAME ": "
  744. "Keymap for Komunikate KIP1000 phone loaded\n");
  745. } else if (!strcasecmp(phone, "gtalk")) {
  746. keymap = keymap_gtalk;
  747. printk(KERN_INFO KBUILD_MODNAME ": "
  748. "Keymap for Genius G-talk phone loaded\n");
  749. } else if (!strcasecmp(phone, "usbph01")) {
  750. keymap = keymap_usbph01;
  751. printk(KERN_INFO KBUILD_MODNAME ": "
  752. "Keymap for Allied-Telesis Corega USBPH01 phone loaded\n");
  753. } else if (!strcasecmp(phone, "atcom")) {
  754. keymap = keymap_atcom;
  755. printk(KERN_INFO KBUILD_MODNAME ": "
  756. "Keymap for ATCom AU-100 phone loaded\n");
  757. } else {
  758. printk(KERN_ERR KBUILD_MODNAME ": "
  759. "Unsupported phone: %s\n", phone);
  760. return -EINVAL;
  761. }
  762. return 0;
  763. }
  764. static int __init cm109_init(void)
  765. {
  766. int err;
  767. err = cm109_select_keymap();
  768. if (err)
  769. return err;
  770. err = usb_register(&cm109_driver);
  771. if (err)
  772. return err;
  773. printk(KERN_INFO KBUILD_MODNAME ": "
  774. DRIVER_DESC ": " DRIVER_VERSION " (C) " DRIVER_AUTHOR "\n");
  775. return 0;
  776. }
  777. static void __exit cm109_exit(void)
  778. {
  779. usb_deregister(&cm109_driver);
  780. }
  781. module_init(cm109_init);
  782. module_exit(cm109_exit);
  783. MODULE_DEVICE_TABLE(usb, cm109_usb_table);
  784. MODULE_AUTHOR(DRIVER_AUTHOR);
  785. MODULE_DESCRIPTION(DRIVER_DESC);
  786. MODULE_LICENSE("GPL");