cm109.c 22 KB

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