cm109.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  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 16 special keys */
  71. KEYMAP_SIZE = 256 + 16,
  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_MUTE;
  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. * CM109 usb communication interface
  281. *****************************************************************************/
  282. static void cm109_submit_buzz_toggle(struct cm109_dev *dev)
  283. {
  284. int error;
  285. if (dev->buzzer_state)
  286. dev->ctl_data->byte[HID_OR0] |= BUZZER_ON;
  287. else
  288. dev->ctl_data->byte[HID_OR0] &= ~BUZZER_ON;
  289. error = usb_submit_urb(dev->urb_ctl, GFP_ATOMIC);
  290. if (error)
  291. dev_err(&dev->intf->dev,
  292. "%s: usb_submit_urb (urb_ctl) failed %d\n",
  293. __func__, error);
  294. }
  295. /*
  296. * IRQ handler
  297. */
  298. static void cm109_urb_irq_callback(struct urb *urb)
  299. {
  300. struct cm109_dev *dev = urb->context;
  301. const int status = urb->status;
  302. int error;
  303. dev_dbg(&dev->intf->dev, "### URB IRQ: [0x%02x 0x%02x 0x%02x 0x%02x] keybit=0x%02x\n",
  304. dev->irq_data->byte[0],
  305. dev->irq_data->byte[1],
  306. dev->irq_data->byte[2],
  307. dev->irq_data->byte[3],
  308. dev->keybit);
  309. if (status) {
  310. if (status == -ESHUTDOWN)
  311. return;
  312. dev_err_ratelimited(&dev->intf->dev, "%s: urb status %d\n",
  313. __func__, status);
  314. goto out;
  315. }
  316. /* Special keys */
  317. if (dev->irq_data->byte[HID_IR0] & 0x0f) {
  318. const int code = (dev->irq_data->byte[HID_IR0] & 0x0f);
  319. report_key(dev, dev->keymap[0xff + code]);
  320. }
  321. /* Scan key column */
  322. if (dev->keybit == 0xf) {
  323. /* Any changes ? */
  324. if ((dev->gpi & 0xf0) == (dev->irq_data->byte[HID_IR1] & 0xf0))
  325. goto out;
  326. dev->gpi = dev->irq_data->byte[HID_IR1] & 0xf0;
  327. dev->keybit = 0x1;
  328. } else {
  329. report_key(dev, dev->keymap[dev->irq_data->byte[HID_IR1]]);
  330. dev->keybit <<= 1;
  331. if (dev->keybit > 0x8)
  332. dev->keybit = 0xf;
  333. }
  334. out:
  335. spin_lock(&dev->ctl_submit_lock);
  336. dev->irq_urb_pending = 0;
  337. if (likely(!dev->shutdown)) {
  338. if (dev->buzzer_state)
  339. dev->ctl_data->byte[HID_OR0] |= BUZZER_ON;
  340. else
  341. dev->ctl_data->byte[HID_OR0] &= ~BUZZER_ON;
  342. dev->ctl_data->byte[HID_OR1] = dev->keybit;
  343. dev->ctl_data->byte[HID_OR2] = dev->keybit;
  344. dev->buzzer_pending = 0;
  345. dev->ctl_urb_pending = 1;
  346. error = usb_submit_urb(dev->urb_ctl, GFP_ATOMIC);
  347. if (error)
  348. dev_err(&dev->intf->dev,
  349. "%s: usb_submit_urb (urb_ctl) failed %d\n",
  350. __func__, error);
  351. }
  352. spin_unlock(&dev->ctl_submit_lock);
  353. }
  354. static void cm109_urb_ctl_callback(struct urb *urb)
  355. {
  356. struct cm109_dev *dev = urb->context;
  357. const int status = urb->status;
  358. int error;
  359. dev_dbg(&dev->intf->dev, "### URB CTL: [0x%02x 0x%02x 0x%02x 0x%02x]\n",
  360. dev->ctl_data->byte[0],
  361. dev->ctl_data->byte[1],
  362. dev->ctl_data->byte[2],
  363. dev->ctl_data->byte[3]);
  364. if (status) {
  365. if (status == -ESHUTDOWN)
  366. return;
  367. dev_err_ratelimited(&dev->intf->dev, "%s: urb status %d\n",
  368. __func__, status);
  369. }
  370. spin_lock(&dev->ctl_submit_lock);
  371. dev->ctl_urb_pending = 0;
  372. if (likely(!dev->shutdown)) {
  373. if (dev->buzzer_pending || status) {
  374. dev->buzzer_pending = 0;
  375. dev->ctl_urb_pending = 1;
  376. cm109_submit_buzz_toggle(dev);
  377. } else if (likely(!dev->irq_urb_pending)) {
  378. /* ask for key data */
  379. dev->irq_urb_pending = 1;
  380. error = usb_submit_urb(dev->urb_irq, GFP_ATOMIC);
  381. if (error)
  382. dev_err(&dev->intf->dev,
  383. "%s: usb_submit_urb (urb_irq) failed %d\n",
  384. __func__, error);
  385. }
  386. }
  387. spin_unlock(&dev->ctl_submit_lock);
  388. }
  389. static void cm109_toggle_buzzer_async(struct cm109_dev *dev)
  390. {
  391. unsigned long flags;
  392. spin_lock_irqsave(&dev->ctl_submit_lock, flags);
  393. if (dev->ctl_urb_pending) {
  394. /* URB completion will resubmit */
  395. dev->buzzer_pending = 1;
  396. } else {
  397. dev->ctl_urb_pending = 1;
  398. cm109_submit_buzz_toggle(dev);
  399. }
  400. spin_unlock_irqrestore(&dev->ctl_submit_lock, flags);
  401. }
  402. static void cm109_toggle_buzzer_sync(struct cm109_dev *dev, int on)
  403. {
  404. int error;
  405. if (on)
  406. dev->ctl_data->byte[HID_OR0] |= BUZZER_ON;
  407. else
  408. dev->ctl_data->byte[HID_OR0] &= ~BUZZER_ON;
  409. error = usb_control_msg(dev->udev,
  410. usb_sndctrlpipe(dev->udev, 0),
  411. dev->ctl_req->bRequest,
  412. dev->ctl_req->bRequestType,
  413. le16_to_cpu(dev->ctl_req->wValue),
  414. le16_to_cpu(dev->ctl_req->wIndex),
  415. dev->ctl_data,
  416. USB_PKT_LEN, USB_CTRL_SET_TIMEOUT);
  417. if (error < 0 && error != -EINTR)
  418. dev_err(&dev->intf->dev, "%s: usb_control_msg() failed %d\n",
  419. __func__, error);
  420. }
  421. static void cm109_stop_traffic(struct cm109_dev *dev)
  422. {
  423. dev->shutdown = 1;
  424. /*
  425. * Make sure other CPUs see this
  426. */
  427. smp_wmb();
  428. usb_kill_urb(dev->urb_ctl);
  429. usb_kill_urb(dev->urb_irq);
  430. cm109_toggle_buzzer_sync(dev, 0);
  431. dev->shutdown = 0;
  432. smp_wmb();
  433. }
  434. static void cm109_restore_state(struct cm109_dev *dev)
  435. {
  436. if (dev->open) {
  437. /*
  438. * Restore buzzer state.
  439. * This will also kick regular URB submission
  440. */
  441. cm109_toggle_buzzer_async(dev);
  442. }
  443. }
  444. /******************************************************************************
  445. * input event interface
  446. *****************************************************************************/
  447. static int cm109_input_open(struct input_dev *idev)
  448. {
  449. struct cm109_dev *dev = input_get_drvdata(idev);
  450. int error;
  451. error = usb_autopm_get_interface(dev->intf);
  452. if (error < 0) {
  453. dev_err(&idev->dev, "%s - cannot autoresume, result %d\n",
  454. __func__, error);
  455. return error;
  456. }
  457. mutex_lock(&dev->pm_mutex);
  458. dev->buzzer_state = 0;
  459. dev->key_code = -1; /* no keys pressed */
  460. dev->keybit = 0xf;
  461. /* issue INIT */
  462. dev->ctl_data->byte[HID_OR0] = HID_OR_GPO_BUZ_SPDIF;
  463. dev->ctl_data->byte[HID_OR1] = dev->keybit;
  464. dev->ctl_data->byte[HID_OR2] = dev->keybit;
  465. dev->ctl_data->byte[HID_OR3] = 0x00;
  466. error = usb_submit_urb(dev->urb_ctl, GFP_KERNEL);
  467. if (error)
  468. dev_err(&dev->intf->dev, "%s: usb_submit_urb (urb_ctl) failed %d\n",
  469. __func__, error);
  470. else
  471. dev->open = 1;
  472. mutex_unlock(&dev->pm_mutex);
  473. if (error)
  474. usb_autopm_put_interface(dev->intf);
  475. return error;
  476. }
  477. static void cm109_input_close(struct input_dev *idev)
  478. {
  479. struct cm109_dev *dev = input_get_drvdata(idev);
  480. mutex_lock(&dev->pm_mutex);
  481. /*
  482. * Once we are here event delivery is stopped so we
  483. * don't need to worry about someone starting buzzer
  484. * again
  485. */
  486. cm109_stop_traffic(dev);
  487. dev->open = 0;
  488. mutex_unlock(&dev->pm_mutex);
  489. usb_autopm_put_interface(dev->intf);
  490. }
  491. static int cm109_input_ev(struct input_dev *idev, unsigned int type,
  492. unsigned int code, int value)
  493. {
  494. struct cm109_dev *dev = input_get_drvdata(idev);
  495. dev_dbg(&dev->intf->dev,
  496. "input_ev: type=%u code=%u value=%d\n", type, code, value);
  497. if (type != EV_SND)
  498. return -EINVAL;
  499. switch (code) {
  500. case SND_TONE:
  501. case SND_BELL:
  502. dev->buzzer_state = !!value;
  503. if (!dev->resetting)
  504. cm109_toggle_buzzer_async(dev);
  505. return 0;
  506. default:
  507. return -EINVAL;
  508. }
  509. }
  510. /******************************************************************************
  511. * Linux interface and usb initialisation
  512. *****************************************************************************/
  513. struct driver_info {
  514. char *name;
  515. };
  516. static const struct driver_info info_cm109 = {
  517. .name = "CM109 USB driver",
  518. };
  519. enum {
  520. VENDOR_ID = 0x0d8c, /* C-Media Electronics */
  521. PRODUCT_ID_CM109 = 0x000e, /* CM109 defines range 0x0008 - 0x000f */
  522. };
  523. /* table of devices that work with this driver */
  524. static const struct usb_device_id cm109_usb_table[] = {
  525. {
  526. .match_flags = USB_DEVICE_ID_MATCH_DEVICE |
  527. USB_DEVICE_ID_MATCH_INT_INFO,
  528. .idVendor = VENDOR_ID,
  529. .idProduct = PRODUCT_ID_CM109,
  530. .bInterfaceClass = USB_CLASS_HID,
  531. .bInterfaceSubClass = 0,
  532. .bInterfaceProtocol = 0,
  533. .driver_info = (kernel_ulong_t) &info_cm109
  534. },
  535. /* you can add more devices here with product ID 0x0008 - 0x000f */
  536. { }
  537. };
  538. static void cm109_usb_cleanup(struct cm109_dev *dev)
  539. {
  540. kfree(dev->ctl_req);
  541. if (dev->ctl_data)
  542. usb_free_coherent(dev->udev, USB_PKT_LEN,
  543. dev->ctl_data, dev->ctl_dma);
  544. if (dev->irq_data)
  545. usb_free_coherent(dev->udev, USB_PKT_LEN,
  546. dev->irq_data, dev->irq_dma);
  547. usb_free_urb(dev->urb_irq); /* parameter validation in core/urb */
  548. usb_free_urb(dev->urb_ctl); /* parameter validation in core/urb */
  549. kfree(dev);
  550. }
  551. static void cm109_usb_disconnect(struct usb_interface *interface)
  552. {
  553. struct cm109_dev *dev = usb_get_intfdata(interface);
  554. usb_set_intfdata(interface, NULL);
  555. input_unregister_device(dev->idev);
  556. cm109_usb_cleanup(dev);
  557. }
  558. static int cm109_usb_probe(struct usb_interface *intf,
  559. const struct usb_device_id *id)
  560. {
  561. struct usb_device *udev = interface_to_usbdev(intf);
  562. struct driver_info *nfo = (struct driver_info *)id->driver_info;
  563. struct usb_host_interface *interface;
  564. struct usb_endpoint_descriptor *endpoint;
  565. struct cm109_dev *dev;
  566. struct input_dev *input_dev = NULL;
  567. int ret, pipe, i;
  568. int error = -ENOMEM;
  569. interface = intf->cur_altsetting;
  570. if (interface->desc.bNumEndpoints < 1)
  571. return -ENODEV;
  572. endpoint = &interface->endpoint[0].desc;
  573. if (!usb_endpoint_is_int_in(endpoint))
  574. return -ENODEV;
  575. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  576. if (!dev)
  577. return -ENOMEM;
  578. spin_lock_init(&dev->ctl_submit_lock);
  579. mutex_init(&dev->pm_mutex);
  580. dev->udev = udev;
  581. dev->intf = intf;
  582. dev->idev = input_dev = input_allocate_device();
  583. if (!input_dev)
  584. goto err_out;
  585. /* allocate usb buffers */
  586. dev->irq_data = usb_alloc_coherent(udev, USB_PKT_LEN,
  587. GFP_KERNEL, &dev->irq_dma);
  588. if (!dev->irq_data)
  589. goto err_out;
  590. dev->ctl_data = usb_alloc_coherent(udev, USB_PKT_LEN,
  591. GFP_KERNEL, &dev->ctl_dma);
  592. if (!dev->ctl_data)
  593. goto err_out;
  594. dev->ctl_req = kmalloc(sizeof(*(dev->ctl_req)), GFP_KERNEL);
  595. if (!dev->ctl_req)
  596. goto err_out;
  597. /* allocate urb structures */
  598. dev->urb_irq = usb_alloc_urb(0, GFP_KERNEL);
  599. if (!dev->urb_irq)
  600. goto err_out;
  601. dev->urb_ctl = usb_alloc_urb(0, GFP_KERNEL);
  602. if (!dev->urb_ctl)
  603. goto err_out;
  604. /* get a handle to the interrupt data pipe */
  605. pipe = usb_rcvintpipe(udev, endpoint->bEndpointAddress);
  606. ret = usb_maxpacket(udev, pipe, usb_pipeout(pipe));
  607. if (ret != USB_PKT_LEN)
  608. dev_err(&intf->dev, "invalid payload size %d, expected %d\n",
  609. ret, USB_PKT_LEN);
  610. /* initialise irq urb */
  611. usb_fill_int_urb(dev->urb_irq, udev, pipe, dev->irq_data,
  612. USB_PKT_LEN,
  613. cm109_urb_irq_callback, dev, endpoint->bInterval);
  614. dev->urb_irq->transfer_dma = dev->irq_dma;
  615. dev->urb_irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  616. dev->urb_irq->dev = udev;
  617. /* initialise ctl urb */
  618. dev->ctl_req->bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE |
  619. USB_DIR_OUT;
  620. dev->ctl_req->bRequest = USB_REQ_SET_CONFIGURATION;
  621. dev->ctl_req->wValue = cpu_to_le16(0x200);
  622. dev->ctl_req->wIndex = cpu_to_le16(interface->desc.bInterfaceNumber);
  623. dev->ctl_req->wLength = cpu_to_le16(USB_PKT_LEN);
  624. usb_fill_control_urb(dev->urb_ctl, udev, usb_sndctrlpipe(udev, 0),
  625. (void *)dev->ctl_req, dev->ctl_data, USB_PKT_LEN,
  626. cm109_urb_ctl_callback, dev);
  627. dev->urb_ctl->transfer_dma = dev->ctl_dma;
  628. dev->urb_ctl->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  629. dev->urb_ctl->dev = udev;
  630. /* find out the physical bus location */
  631. usb_make_path(udev, dev->phys, sizeof(dev->phys));
  632. strlcat(dev->phys, "/input0", sizeof(dev->phys));
  633. /* register settings for the input device */
  634. input_dev->name = nfo->name;
  635. input_dev->phys = dev->phys;
  636. usb_to_input_id(udev, &input_dev->id);
  637. input_dev->dev.parent = &intf->dev;
  638. input_set_drvdata(input_dev, dev);
  639. input_dev->open = cm109_input_open;
  640. input_dev->close = cm109_input_close;
  641. input_dev->event = cm109_input_ev;
  642. input_dev->keycode = dev->keymap;
  643. input_dev->keycodesize = sizeof(unsigned char);
  644. input_dev->keycodemax = ARRAY_SIZE(dev->keymap);
  645. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_SND);
  646. input_dev->sndbit[0] = BIT_MASK(SND_BELL) | BIT_MASK(SND_TONE);
  647. /* register available key events */
  648. for (i = 0; i < KEYMAP_SIZE; i++) {
  649. unsigned short k = keymap(i);
  650. dev->keymap[i] = k;
  651. __set_bit(k, input_dev->keybit);
  652. }
  653. __clear_bit(KEY_RESERVED, input_dev->keybit);
  654. error = input_register_device(dev->idev);
  655. if (error)
  656. goto err_out;
  657. usb_set_intfdata(intf, dev);
  658. return 0;
  659. err_out:
  660. input_free_device(input_dev);
  661. cm109_usb_cleanup(dev);
  662. return error;
  663. }
  664. static int cm109_usb_suspend(struct usb_interface *intf, pm_message_t message)
  665. {
  666. struct cm109_dev *dev = usb_get_intfdata(intf);
  667. dev_info(&intf->dev, "cm109: usb_suspend (event=%d)\n", message.event);
  668. mutex_lock(&dev->pm_mutex);
  669. cm109_stop_traffic(dev);
  670. mutex_unlock(&dev->pm_mutex);
  671. return 0;
  672. }
  673. static int cm109_usb_resume(struct usb_interface *intf)
  674. {
  675. struct cm109_dev *dev = usb_get_intfdata(intf);
  676. dev_info(&intf->dev, "cm109: usb_resume\n");
  677. mutex_lock(&dev->pm_mutex);
  678. cm109_restore_state(dev);
  679. mutex_unlock(&dev->pm_mutex);
  680. return 0;
  681. }
  682. static int cm109_usb_pre_reset(struct usb_interface *intf)
  683. {
  684. struct cm109_dev *dev = usb_get_intfdata(intf);
  685. mutex_lock(&dev->pm_mutex);
  686. /*
  687. * Make sure input events don't try to toggle buzzer
  688. * while we are resetting
  689. */
  690. dev->resetting = 1;
  691. smp_wmb();
  692. cm109_stop_traffic(dev);
  693. return 0;
  694. }
  695. static int cm109_usb_post_reset(struct usb_interface *intf)
  696. {
  697. struct cm109_dev *dev = usb_get_intfdata(intf);
  698. dev->resetting = 0;
  699. smp_wmb();
  700. cm109_restore_state(dev);
  701. mutex_unlock(&dev->pm_mutex);
  702. return 0;
  703. }
  704. static struct usb_driver cm109_driver = {
  705. .name = "cm109",
  706. .probe = cm109_usb_probe,
  707. .disconnect = cm109_usb_disconnect,
  708. .suspend = cm109_usb_suspend,
  709. .resume = cm109_usb_resume,
  710. .reset_resume = cm109_usb_resume,
  711. .pre_reset = cm109_usb_pre_reset,
  712. .post_reset = cm109_usb_post_reset,
  713. .id_table = cm109_usb_table,
  714. .supports_autosuspend = 1,
  715. };
  716. static int __init cm109_select_keymap(void)
  717. {
  718. /* Load the phone keymap */
  719. if (!strcasecmp(phone, "kip1000")) {
  720. keymap = keymap_kip1000;
  721. printk(KERN_INFO KBUILD_MODNAME ": "
  722. "Keymap for Komunikate KIP1000 phone loaded\n");
  723. } else if (!strcasecmp(phone, "gtalk")) {
  724. keymap = keymap_gtalk;
  725. printk(KERN_INFO KBUILD_MODNAME ": "
  726. "Keymap for Genius G-talk phone loaded\n");
  727. } else if (!strcasecmp(phone, "usbph01")) {
  728. keymap = keymap_usbph01;
  729. printk(KERN_INFO KBUILD_MODNAME ": "
  730. "Keymap for Allied-Telesis Corega USBPH01 phone loaded\n");
  731. } else if (!strcasecmp(phone, "atcom")) {
  732. keymap = keymap_atcom;
  733. printk(KERN_INFO KBUILD_MODNAME ": "
  734. "Keymap for ATCom AU-100 phone loaded\n");
  735. } else {
  736. printk(KERN_ERR KBUILD_MODNAME ": "
  737. "Unsupported phone: %s\n", phone);
  738. return -EINVAL;
  739. }
  740. return 0;
  741. }
  742. static int __init cm109_init(void)
  743. {
  744. int err;
  745. err = cm109_select_keymap();
  746. if (err)
  747. return err;
  748. err = usb_register(&cm109_driver);
  749. if (err)
  750. return err;
  751. printk(KERN_INFO KBUILD_MODNAME ": "
  752. DRIVER_DESC ": " DRIVER_VERSION " (C) " DRIVER_AUTHOR "\n");
  753. return 0;
  754. }
  755. static void __exit cm109_exit(void)
  756. {
  757. usb_deregister(&cm109_driver);
  758. }
  759. module_init(cm109_init);
  760. module_exit(cm109_exit);
  761. MODULE_DEVICE_TABLE(usb, cm109_usb_table);
  762. MODULE_AUTHOR(DRIVER_AUTHOR);
  763. MODULE_DESCRIPTION(DRIVER_DESC);
  764. MODULE_LICENSE("GPL");