tm6000-input.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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_urb_received(struct urb *urb)
  133. {
  134. struct tm6000_core *dev = urb->context;
  135. struct tm6000_IR *ir = dev->ir;
  136. struct tm6000_ir_poll_result poll_result;
  137. char *buf;
  138. dprintk(2, "%s\n",__func__);
  139. if (urb->status < 0 || urb->actual_length <= 0) {
  140. printk(KERN_INFO "tm6000: IR URB failure: status: %i, length %i\n",
  141. urb->status, urb->actual_length);
  142. ir->submit_urb = 1;
  143. schedule_delayed_work(&ir->work, msecs_to_jiffies(URB_SUBMIT_DELAY));
  144. return;
  145. }
  146. buf = urb->transfer_buffer;
  147. if (ir_debug)
  148. print_hex_dump(KERN_DEBUG, "tm6000: IR data: ",
  149. DUMP_PREFIX_OFFSET,16, 1,
  150. buf, urb->actual_length, false);
  151. poll_result.rc_data = buf[0];
  152. if (urb->actual_length > 1)
  153. poll_result.rc_data |= buf[1] << 8;
  154. dprintk(1, "%s, scancode: 0x%04x\n",__func__, poll_result.rc_data);
  155. rc_keydown(ir->rc, poll_result.rc_data, 0);
  156. usb_submit_urb(urb, GFP_ATOMIC);
  157. /*
  158. * Flash the led. We can't do it here, as it is running on IRQ context.
  159. * So, use the scheduler to do it, in a few ms.
  160. */
  161. ir->pwled = 2;
  162. schedule_delayed_work(&ir->work, msecs_to_jiffies(10));
  163. }
  164. static void tm6000_ir_handle_key(struct work_struct *work)
  165. {
  166. struct tm6000_IR *ir = container_of(work, struct tm6000_IR, work.work);
  167. struct tm6000_core *dev = ir->dev;
  168. struct tm6000_ir_poll_result poll_result;
  169. int rc;
  170. u8 buf[2];
  171. if (ir->wait)
  172. return;
  173. dprintk(3, "%s\n",__func__);
  174. rc = tm6000_read_write_usb(dev, USB_DIR_IN |
  175. USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  176. REQ_02_GET_IR_CODE, 0, 0, buf, 2);
  177. if (rc < 0)
  178. return;
  179. if (rc > 1)
  180. poll_result.rc_data = buf[0] | buf[1] << 8;
  181. else
  182. poll_result.rc_data = buf[0];
  183. /* Check if something was read */
  184. if ((poll_result.rc_data & 0xff) == 0xff) {
  185. if (!ir->pwled) {
  186. tm6000_flash_led(dev, 1);
  187. ir->pwled = 1;
  188. }
  189. return;
  190. }
  191. dprintk(1, "%s, scancode: 0x%04x\n",__func__, poll_result.rc_data);
  192. rc_keydown(ir->rc, poll_result.rc_data, 0);
  193. tm6000_flash_led(dev, 0);
  194. ir->pwled = 0;
  195. /* Re-schedule polling */
  196. schedule_delayed_work(&ir->work, msecs_to_jiffies(ir->polling));
  197. }
  198. static void tm6000_ir_int_work(struct work_struct *work)
  199. {
  200. struct tm6000_IR *ir = container_of(work, struct tm6000_IR, work.work);
  201. struct tm6000_core *dev = ir->dev;
  202. int rc;
  203. dprintk(3, "%s, submit_urb = %d, pwled = %d\n",__func__, ir->submit_urb,
  204. ir->pwled);
  205. if (ir->submit_urb) {
  206. dprintk(3, "Resubmit urb\n");
  207. tm6000_set_reg(dev, REQ_04_EN_DISABLE_MCU_INT, 2, 0);
  208. rc = usb_submit_urb(ir->int_urb, GFP_ATOMIC);
  209. if (rc < 0) {
  210. printk(KERN_ERR "tm6000: Can't submit an IR interrupt. Error %i\n",
  211. rc);
  212. /* Retry in 100 ms */
  213. schedule_delayed_work(&ir->work, msecs_to_jiffies(URB_SUBMIT_DELAY));
  214. return;
  215. }
  216. ir->submit_urb = 0;
  217. }
  218. /* Led is enabled only if USB submit doesn't fail */
  219. if (ir->pwled == 2) {
  220. tm6000_flash_led(dev, 0);
  221. ir->pwled = 0;
  222. schedule_delayed_work(&ir->work, msecs_to_jiffies(URB_INT_LED_DELAY));
  223. } else if (!ir->pwled) {
  224. tm6000_flash_led(dev, 1);
  225. ir->pwled = 1;
  226. }
  227. }
  228. static int tm6000_ir_start(struct rc_dev *rc)
  229. {
  230. struct tm6000_IR *ir = rc->priv;
  231. dprintk(2, "%s\n",__func__);
  232. schedule_delayed_work(&ir->work, 0);
  233. return 0;
  234. }
  235. static void tm6000_ir_stop(struct rc_dev *rc)
  236. {
  237. struct tm6000_IR *ir = rc->priv;
  238. dprintk(2, "%s\n",__func__);
  239. cancel_delayed_work_sync(&ir->work);
  240. }
  241. static int tm6000_ir_change_protocol(struct rc_dev *rc, u64 *rc_type)
  242. {
  243. struct tm6000_IR *ir = rc->priv;
  244. if (!ir)
  245. return 0;
  246. dprintk(2, "%s\n",__func__);
  247. if ((rc->rc_map.scan) && (*rc_type == RC_BIT_NEC))
  248. ir->key_addr = ((rc->rc_map.scan[0].scancode >> 8) & 0xffff);
  249. ir->rc_type = *rc_type;
  250. tm6000_ir_config(ir);
  251. /* TODO */
  252. return 0;
  253. }
  254. static int __tm6000_ir_int_start(struct rc_dev *rc)
  255. {
  256. struct tm6000_IR *ir = rc->priv;
  257. struct tm6000_core *dev;
  258. int pipe, size;
  259. int err = -ENOMEM;
  260. if (!ir)
  261. return -ENODEV;
  262. dev = ir->dev;
  263. dprintk(2, "%s\n",__func__);
  264. ir->int_urb = usb_alloc_urb(0, GFP_ATOMIC);
  265. if (!ir->int_urb)
  266. return -ENOMEM;
  267. pipe = usb_rcvintpipe(dev->udev,
  268. dev->int_in.endp->desc.bEndpointAddress
  269. & USB_ENDPOINT_NUMBER_MASK);
  270. size = usb_maxpacket(dev->udev, pipe, usb_pipeout(pipe));
  271. dprintk(1, "IR max size: %d\n", size);
  272. ir->int_urb->transfer_buffer = kzalloc(size, GFP_ATOMIC);
  273. if (ir->int_urb->transfer_buffer == NULL) {
  274. usb_free_urb(ir->int_urb);
  275. return err;
  276. }
  277. dprintk(1, "int interval: %d\n", dev->int_in.endp->desc.bInterval);
  278. usb_fill_int_urb(ir->int_urb, dev->udev, pipe,
  279. ir->int_urb->transfer_buffer, size,
  280. tm6000_ir_urb_received, dev,
  281. dev->int_in.endp->desc.bInterval);
  282. ir->submit_urb = 1;
  283. schedule_delayed_work(&ir->work, msecs_to_jiffies(URB_SUBMIT_DELAY));
  284. return 0;
  285. }
  286. static void __tm6000_ir_int_stop(struct rc_dev *rc)
  287. {
  288. struct tm6000_IR *ir = rc->priv;
  289. if (!ir || !ir->int_urb)
  290. return;
  291. dprintk(2, "%s\n",__func__);
  292. usb_kill_urb(ir->int_urb);
  293. kfree(ir->int_urb->transfer_buffer);
  294. usb_free_urb(ir->int_urb);
  295. ir->int_urb = NULL;
  296. }
  297. int tm6000_ir_int_start(struct tm6000_core *dev)
  298. {
  299. struct tm6000_IR *ir = dev->ir;
  300. if (!ir)
  301. return 0;
  302. return __tm6000_ir_int_start(ir->rc);
  303. }
  304. void tm6000_ir_int_stop(struct tm6000_core *dev)
  305. {
  306. struct tm6000_IR *ir = dev->ir;
  307. if (!ir || !ir->rc)
  308. return;
  309. __tm6000_ir_int_stop(ir->rc);
  310. }
  311. int tm6000_ir_init(struct tm6000_core *dev)
  312. {
  313. struct tm6000_IR *ir;
  314. struct rc_dev *rc;
  315. int err = -ENOMEM;
  316. u64 rc_type;
  317. if (!enable_ir)
  318. return -ENODEV;
  319. if (!dev->caps.has_remote)
  320. return 0;
  321. if (!dev->ir_codes)
  322. return 0;
  323. ir = kzalloc(sizeof(*ir), GFP_ATOMIC);
  324. rc = rc_allocate_device();
  325. if (!ir || !rc)
  326. goto out;
  327. dprintk(2, "%s\n", __func__);
  328. /* record handles to ourself */
  329. ir->dev = dev;
  330. dev->ir = ir;
  331. ir->rc = rc;
  332. /* input setup */
  333. rc->allowed_protos = RC_BIT_RC5 | RC_BIT_NEC;
  334. /* Neded, in order to support NEC remotes with 24 or 32 bits */
  335. rc->scanmask = 0xffff;
  336. rc->priv = ir;
  337. rc->change_protocol = tm6000_ir_change_protocol;
  338. if (dev->int_in.endp) {
  339. rc->open = __tm6000_ir_int_start;
  340. rc->close = __tm6000_ir_int_stop;
  341. INIT_DELAYED_WORK(&ir->work, tm6000_ir_int_work);
  342. } else {
  343. rc->open = tm6000_ir_start;
  344. rc->close = tm6000_ir_stop;
  345. ir->polling = 50;
  346. INIT_DELAYED_WORK(&ir->work, tm6000_ir_handle_key);
  347. }
  348. rc->driver_type = RC_DRIVER_SCANCODE;
  349. snprintf(ir->name, sizeof(ir->name), "tm5600/60x0 IR (%s)",
  350. dev->name);
  351. usb_make_path(dev->udev, ir->phys, sizeof(ir->phys));
  352. strlcat(ir->phys, "/input0", sizeof(ir->phys));
  353. rc_type = RC_BIT_UNKNOWN;
  354. tm6000_ir_change_protocol(rc, &rc_type);
  355. rc->input_name = ir->name;
  356. rc->input_phys = ir->phys;
  357. rc->input_id.bustype = BUS_USB;
  358. rc->input_id.version = 1;
  359. rc->input_id.vendor = le16_to_cpu(dev->udev->descriptor.idVendor);
  360. rc->input_id.product = le16_to_cpu(dev->udev->descriptor.idProduct);
  361. rc->map_name = dev->ir_codes;
  362. rc->driver_name = "tm6000";
  363. rc->dev.parent = &dev->udev->dev;
  364. /* ir register */
  365. err = rc_register_device(rc);
  366. if (err)
  367. goto out;
  368. return 0;
  369. out:
  370. dev->ir = NULL;
  371. rc_free_device(rc);
  372. kfree(ir);
  373. return err;
  374. }
  375. int tm6000_ir_fini(struct tm6000_core *dev)
  376. {
  377. struct tm6000_IR *ir = dev->ir;
  378. /* skip detach on non attached board */
  379. if (!ir)
  380. return 0;
  381. dprintk(2, "%s\n",__func__);
  382. if (!ir->polling)
  383. __tm6000_ir_int_stop(ir->rc);
  384. tm6000_ir_stop(ir->rc);
  385. /* Turn off the led */
  386. tm6000_flash_led(dev, 0);
  387. ir->pwled = 0;
  388. rc_unregister_device(ir->rc);
  389. kfree(ir);
  390. dev->ir = NULL;
  391. return 0;
  392. }