tm6000-input.c 12 KB

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