tm6000-input.c 12 KB

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