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