tm6000-input.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. /*
  2. * tm6000-input.c - driver for TM5600/TM6000/TM6010 USB video capture devices
  3. *
  4. * Copyright (C) 2010 Stefan Ringel <stefan.ringel@arcor.de>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation version 2
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/module.h>
  16. #include <linux/init.h>
  17. #include <linux/delay.h>
  18. #include <linux/input.h>
  19. #include <linux/usb.h>
  20. #include <media/rc-core.h>
  21. #include "tm6000.h"
  22. #include "tm6000-regs.h"
  23. static unsigned int ir_debug;
  24. module_param(ir_debug, int, 0644);
  25. MODULE_PARM_DESC(ir_debug, "debug message level");
  26. static unsigned int enable_ir = 1;
  27. module_param(enable_ir, int, 0644);
  28. MODULE_PARM_DESC(enable_ir, "enable ir (default is enable)");
  29. static unsigned int ir_clock_mhz = 12;
  30. module_param(ir_clock_mhz, int, 0644);
  31. MODULE_PARM_DESC(ir_clock_mhz, "ir clock, in MHz");
  32. #define URB_SUBMIT_DELAY 100 /* ms - Delay to submit an URB request on retrial and init */
  33. #define URB_INT_LED_DELAY 100 /* ms - Delay to turn led on again on int mode */
  34. #undef dprintk
  35. #define dprintk(level, fmt, arg...) do {\
  36. if (ir_debug >= level) \
  37. printk(KERN_DEBUG "%s/ir: " fmt, ir->name , ## arg); \
  38. } while (0)
  39. struct tm6000_ir_poll_result {
  40. u16 rc_data;
  41. };
  42. struct tm6000_IR {
  43. struct tm6000_core *dev;
  44. struct rc_dev *rc;
  45. char name[32];
  46. char phys[32];
  47. /* poll expernal decoder */
  48. int polling;
  49. struct delayed_work work;
  50. u8 wait:1;
  51. u8 pwled:2;
  52. u8 submit_urb:1;
  53. u16 key_addr;
  54. struct urb *int_urb;
  55. /* IR device properties */
  56. u64 rc_type;
  57. };
  58. void tm6000_ir_wait(struct tm6000_core *dev, u8 state)
  59. {
  60. struct tm6000_IR *ir = dev->ir;
  61. if (!dev->ir)
  62. return;
  63. dprintk(2, "%s: %i\n",__func__, ir->wait);
  64. if (state)
  65. ir->wait = 1;
  66. else
  67. ir->wait = 0;
  68. }
  69. static int tm6000_ir_config(struct tm6000_IR *ir)
  70. {
  71. struct tm6000_core *dev = ir->dev;
  72. u32 pulse = 0, leader = 0;
  73. dprintk(2, "%s\n",__func__);
  74. /*
  75. * The IR decoder supports RC-5 or NEC, with a configurable timing.
  76. * The timing configuration there is not that accurate, as it uses
  77. * approximate values. The NEC spec mentions a 562.5 unit period,
  78. * and RC-5 uses a 888.8 period.
  79. * Currently, driver assumes a clock provided by a 12 MHz XTAL, but
  80. * a modprobe parameter can adjust it.
  81. * Adjustments are required for other timings.
  82. * It seems that the 900ms timing for NEC is used to detect a RC-5
  83. * IR, in order to discard such decoding
  84. */
  85. switch (ir->rc_type) {
  86. case RC_BIT_NEC:
  87. leader = 900; /* ms */
  88. pulse = 700; /* ms - the actual value would be 562 */
  89. break;
  90. default:
  91. case RC_BIT_RC5:
  92. leader = 900; /* ms - from the NEC decoding */
  93. pulse = 1780; /* ms - The actual value would be 1776 */
  94. break;
  95. }
  96. pulse = ir_clock_mhz * pulse;
  97. leader = ir_clock_mhz * leader;
  98. if (ir->rc_type == RC_BIT_NEC)
  99. leader = leader | 0x8000;
  100. dprintk(2, "%s: %s, %d MHz, leader = 0x%04x, pulse = 0x%06x \n",
  101. __func__,
  102. (ir->rc_type == RC_BIT_NEC) ? "NEC" : "RC-5",
  103. ir_clock_mhz, leader, pulse);
  104. /* Remote WAKEUP = enable, normal mode, from IR decoder output */
  105. tm6000_set_reg(dev, TM6010_REQ07_RE5_REMOTE_WAKEUP, 0xfe);
  106. /* Enable IR reception on non-busrt mode */
  107. tm6000_set_reg(dev, TM6010_REQ07_RD8_IR, 0x2f);
  108. /* IR_WKUP_SEL = Low byte in decoded IR data */
  109. tm6000_set_reg(dev, TM6010_REQ07_RDA_IR_WAKEUP_SEL, 0xff);
  110. /* IR_WKU_ADD code */
  111. tm6000_set_reg(dev, TM6010_REQ07_RDB_IR_WAKEUP_ADD, 0xff);
  112. tm6000_set_reg(dev, TM6010_REQ07_RDC_IR_LEADER1, leader >> 8);
  113. tm6000_set_reg(dev, TM6010_REQ07_RDD_IR_LEADER0, leader);
  114. tm6000_set_reg(dev, TM6010_REQ07_RDE_IR_PULSE_CNT1, pulse >> 8);
  115. tm6000_set_reg(dev, TM6010_REQ07_RDF_IR_PULSE_CNT0, pulse);
  116. if (!ir->polling)
  117. tm6000_set_reg(dev, REQ_04_EN_DISABLE_MCU_INT, 2, 0);
  118. else
  119. tm6000_set_reg(dev, REQ_04_EN_DISABLE_MCU_INT, 2, 1);
  120. msleep(10);
  121. /* Shows that IR is working via the LED */
  122. tm6000_flash_led(dev, 0);
  123. msleep(100);
  124. tm6000_flash_led(dev, 1);
  125. ir->pwled = 1;
  126. return 0;
  127. }
  128. static void tm6000_ir_keydown(struct tm6000_IR *ir,
  129. const char *buf, unsigned int len)
  130. {
  131. u8 device, command;
  132. u32 scancode;
  133. enum rc_type protocol;
  134. if (len < 1)
  135. return;
  136. command = buf[0];
  137. device = (len > 1 ? buf[1] : 0x0);
  138. switch (ir->rc_type) {
  139. case RC_BIT_RC5:
  140. protocol = RC_TYPE_RC5;
  141. scancode = RC_SCANCODE_RC5(device, command);
  142. break;
  143. case RC_BIT_NEC:
  144. protocol = RC_TYPE_NEC;
  145. scancode = RC_SCANCODE_NEC(device, command);
  146. break;
  147. default:
  148. protocol = RC_TYPE_OTHER;
  149. scancode = RC_SCANCODE_OTHER(device << 8 | command);
  150. break;
  151. }
  152. dprintk(1, "%s, protocol: 0x%04x, scancode: 0x%08x\n",
  153. __func__, protocol, scancode);
  154. rc_keydown(ir->rc, protocol, scancode, 0);
  155. }
  156. static void tm6000_ir_urb_received(struct urb *urb)
  157. {
  158. struct tm6000_core *dev = urb->context;
  159. struct tm6000_IR *ir = dev->ir;
  160. char *buf;
  161. dprintk(2, "%s\n",__func__);
  162. if (urb->status < 0 || urb->actual_length <= 0) {
  163. printk(KERN_INFO "tm6000: IR URB failure: status: %i, length %i\n",
  164. urb->status, urb->actual_length);
  165. ir->submit_urb = 1;
  166. schedule_delayed_work(&ir->work, msecs_to_jiffies(URB_SUBMIT_DELAY));
  167. return;
  168. }
  169. buf = urb->transfer_buffer;
  170. if (ir_debug)
  171. print_hex_dump(KERN_DEBUG, "tm6000: IR data: ",
  172. DUMP_PREFIX_OFFSET,16, 1,
  173. buf, urb->actual_length, false);
  174. tm6000_ir_keydown(ir, urb->transfer_buffer, urb->actual_length);
  175. usb_submit_urb(urb, GFP_ATOMIC);
  176. /*
  177. * Flash the led. We can't do it here, as it is running on IRQ context.
  178. * So, use the scheduler to do it, in a few ms.
  179. */
  180. ir->pwled = 2;
  181. schedule_delayed_work(&ir->work, msecs_to_jiffies(10));
  182. }
  183. static void tm6000_ir_handle_key(struct work_struct *work)
  184. {
  185. struct tm6000_IR *ir = container_of(work, struct tm6000_IR, work.work);
  186. struct tm6000_core *dev = ir->dev;
  187. int rc;
  188. u8 buf[2];
  189. if (ir->wait)
  190. return;
  191. dprintk(3, "%s\n",__func__);
  192. rc = tm6000_read_write_usb(dev, USB_DIR_IN |
  193. USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  194. REQ_02_GET_IR_CODE, 0, 0, buf, 2);
  195. if (rc < 0)
  196. return;
  197. /* Check if something was read */
  198. if ((buf[0] & 0xff) == 0xff) {
  199. if (!ir->pwled) {
  200. tm6000_flash_led(dev, 1);
  201. ir->pwled = 1;
  202. }
  203. return;
  204. }
  205. tm6000_ir_keydown(ir, buf, rc);
  206. tm6000_flash_led(dev, 0);
  207. ir->pwled = 0;
  208. /* Re-schedule polling */
  209. schedule_delayed_work(&ir->work, msecs_to_jiffies(ir->polling));
  210. }
  211. static void tm6000_ir_int_work(struct work_struct *work)
  212. {
  213. struct tm6000_IR *ir = container_of(work, struct tm6000_IR, work.work);
  214. struct tm6000_core *dev = ir->dev;
  215. int rc;
  216. dprintk(3, "%s, submit_urb = %d, pwled = %d\n",__func__, ir->submit_urb,
  217. ir->pwled);
  218. if (ir->submit_urb) {
  219. dprintk(3, "Resubmit urb\n");
  220. tm6000_set_reg(dev, REQ_04_EN_DISABLE_MCU_INT, 2, 0);
  221. rc = usb_submit_urb(ir->int_urb, GFP_ATOMIC);
  222. if (rc < 0) {
  223. printk(KERN_ERR "tm6000: Can't submit an IR interrupt. Error %i\n",
  224. rc);
  225. /* Retry in 100 ms */
  226. schedule_delayed_work(&ir->work, msecs_to_jiffies(URB_SUBMIT_DELAY));
  227. return;
  228. }
  229. ir->submit_urb = 0;
  230. }
  231. /* Led is enabled only if USB submit doesn't fail */
  232. if (ir->pwled == 2) {
  233. tm6000_flash_led(dev, 0);
  234. ir->pwled = 0;
  235. schedule_delayed_work(&ir->work, msecs_to_jiffies(URB_INT_LED_DELAY));
  236. } else if (!ir->pwled) {
  237. tm6000_flash_led(dev, 1);
  238. ir->pwled = 1;
  239. }
  240. }
  241. static int tm6000_ir_start(struct rc_dev *rc)
  242. {
  243. struct tm6000_IR *ir = rc->priv;
  244. dprintk(2, "%s\n",__func__);
  245. schedule_delayed_work(&ir->work, 0);
  246. return 0;
  247. }
  248. static void tm6000_ir_stop(struct rc_dev *rc)
  249. {
  250. struct tm6000_IR *ir = rc->priv;
  251. dprintk(2, "%s\n",__func__);
  252. cancel_delayed_work_sync(&ir->work);
  253. }
  254. static int tm6000_ir_change_protocol(struct rc_dev *rc, u64 *rc_type)
  255. {
  256. struct tm6000_IR *ir = rc->priv;
  257. if (!ir)
  258. return 0;
  259. dprintk(2, "%s\n",__func__);
  260. if ((rc->rc_map.scan) && (*rc_type == RC_BIT_NEC))
  261. ir->key_addr = ((rc->rc_map.scan[0].scancode >> 8) & 0xffff);
  262. ir->rc_type = *rc_type;
  263. tm6000_ir_config(ir);
  264. /* TODO */
  265. return 0;
  266. }
  267. static int __tm6000_ir_int_start(struct rc_dev *rc)
  268. {
  269. struct tm6000_IR *ir = rc->priv;
  270. struct tm6000_core *dev;
  271. int pipe, size;
  272. int err = -ENOMEM;
  273. if (!ir)
  274. return -ENODEV;
  275. dev = ir->dev;
  276. dprintk(2, "%s\n",__func__);
  277. ir->int_urb = usb_alloc_urb(0, GFP_ATOMIC);
  278. if (!ir->int_urb)
  279. return -ENOMEM;
  280. pipe = usb_rcvintpipe(dev->udev,
  281. dev->int_in.endp->desc.bEndpointAddress
  282. & USB_ENDPOINT_NUMBER_MASK);
  283. size = usb_maxpacket(dev->udev, pipe, usb_pipeout(pipe));
  284. dprintk(1, "IR max size: %d\n", size);
  285. ir->int_urb->transfer_buffer = kzalloc(size, GFP_ATOMIC);
  286. if (ir->int_urb->transfer_buffer == NULL) {
  287. usb_free_urb(ir->int_urb);
  288. return err;
  289. }
  290. dprintk(1, "int interval: %d\n", dev->int_in.endp->desc.bInterval);
  291. usb_fill_int_urb(ir->int_urb, dev->udev, pipe,
  292. ir->int_urb->transfer_buffer, size,
  293. tm6000_ir_urb_received, dev,
  294. dev->int_in.endp->desc.bInterval);
  295. ir->submit_urb = 1;
  296. schedule_delayed_work(&ir->work, msecs_to_jiffies(URB_SUBMIT_DELAY));
  297. return 0;
  298. }
  299. static void __tm6000_ir_int_stop(struct rc_dev *rc)
  300. {
  301. struct tm6000_IR *ir = rc->priv;
  302. if (!ir || !ir->int_urb)
  303. return;
  304. dprintk(2, "%s\n",__func__);
  305. usb_kill_urb(ir->int_urb);
  306. kfree(ir->int_urb->transfer_buffer);
  307. usb_free_urb(ir->int_urb);
  308. ir->int_urb = NULL;
  309. }
  310. int tm6000_ir_int_start(struct tm6000_core *dev)
  311. {
  312. struct tm6000_IR *ir = dev->ir;
  313. if (!ir)
  314. return 0;
  315. return __tm6000_ir_int_start(ir->rc);
  316. }
  317. void tm6000_ir_int_stop(struct tm6000_core *dev)
  318. {
  319. struct tm6000_IR *ir = dev->ir;
  320. if (!ir || !ir->rc)
  321. return;
  322. __tm6000_ir_int_stop(ir->rc);
  323. }
  324. int tm6000_ir_init(struct tm6000_core *dev)
  325. {
  326. struct tm6000_IR *ir;
  327. struct rc_dev *rc;
  328. int err = -ENOMEM;
  329. u64 rc_type;
  330. if (!enable_ir)
  331. return -ENODEV;
  332. if (!dev->caps.has_remote)
  333. return 0;
  334. if (!dev->ir_codes)
  335. return 0;
  336. ir = kzalloc(sizeof(*ir), GFP_ATOMIC);
  337. rc = rc_allocate_device(RC_DRIVER_SCANCODE);
  338. if (!ir || !rc)
  339. goto out;
  340. dprintk(2, "%s\n", __func__);
  341. /* record handles to ourself */
  342. ir->dev = dev;
  343. dev->ir = ir;
  344. ir->rc = rc;
  345. /* input setup */
  346. rc->allowed_protocols = RC_BIT_RC5 | RC_BIT_NEC;
  347. /* Needed, in order to support NEC remotes with 24 or 32 bits */
  348. rc->scancode_mask = 0xffff;
  349. rc->priv = ir;
  350. rc->change_protocol = tm6000_ir_change_protocol;
  351. if (dev->int_in.endp) {
  352. rc->open = __tm6000_ir_int_start;
  353. rc->close = __tm6000_ir_int_stop;
  354. INIT_DELAYED_WORK(&ir->work, tm6000_ir_int_work);
  355. } else {
  356. rc->open = tm6000_ir_start;
  357. rc->close = tm6000_ir_stop;
  358. ir->polling = 50;
  359. INIT_DELAYED_WORK(&ir->work, tm6000_ir_handle_key);
  360. }
  361. snprintf(ir->name, sizeof(ir->name), "tm5600/60x0 IR (%s)",
  362. dev->name);
  363. usb_make_path(dev->udev, ir->phys, sizeof(ir->phys));
  364. strlcat(ir->phys, "/input0", sizeof(ir->phys));
  365. rc_type = RC_BIT_UNKNOWN;
  366. tm6000_ir_change_protocol(rc, &rc_type);
  367. rc->input_name = ir->name;
  368. rc->input_phys = ir->phys;
  369. rc->input_id.bustype = BUS_USB;
  370. rc->input_id.version = 1;
  371. rc->input_id.vendor = le16_to_cpu(dev->udev->descriptor.idVendor);
  372. rc->input_id.product = le16_to_cpu(dev->udev->descriptor.idProduct);
  373. rc->map_name = dev->ir_codes;
  374. rc->driver_name = "tm6000";
  375. rc->dev.parent = &dev->udev->dev;
  376. /* ir register */
  377. err = rc_register_device(rc);
  378. if (err)
  379. goto out;
  380. return 0;
  381. out:
  382. dev->ir = NULL;
  383. rc_free_device(rc);
  384. kfree(ir);
  385. return err;
  386. }
  387. int tm6000_ir_fini(struct tm6000_core *dev)
  388. {
  389. struct tm6000_IR *ir = dev->ir;
  390. /* skip detach on non attached board */
  391. if (!ir)
  392. return 0;
  393. dprintk(2, "%s\n",__func__);
  394. if (!ir->polling)
  395. __tm6000_ir_int_stop(ir->rc);
  396. tm6000_ir_stop(ir->rc);
  397. /* Turn off the led */
  398. tm6000_flash_led(dev, 0);
  399. ir->pwled = 0;
  400. rc_unregister_device(ir->rc);
  401. kfree(ir);
  402. dev->ir = NULL;
  403. return 0;
  404. }