cm109.c 22 KB

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