tm6000-input.c 12 KB

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