cx23885-input.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /*
  2. * Driver for the Conexant CX23885/7/8 PCIe bridge
  3. *
  4. * Infrared remote control input device
  5. *
  6. * Most of this file is
  7. *
  8. * Copyright (C) 2009 Andy Walls <awalls@radix.net>
  9. *
  10. * However, the cx23885_input_{init,fini} functions contained herein are
  11. * derived from Linux kernel files linux/media/video/.../...-input.c marked as:
  12. *
  13. * Copyright (C) 2008 <srinivasa.deevi at conexant dot com>
  14. * Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
  15. * Markus Rechberger <mrechberger@gmail.com>
  16. * Mauro Carvalho Chehab <mchehab@infradead.org>
  17. * Sascha Sommer <saschasommer@freenet.de>
  18. * Copyright (C) 2004, 2005 Chris Pascoe
  19. * Copyright (C) 2003, 2004 Gerd Knorr
  20. * Copyright (C) 2003 Pavel Machek
  21. *
  22. * This program is free software; you can redistribute it and/or
  23. * modify it under the terms of the GNU General Public License
  24. * as published by the Free Software Foundation; either version 2
  25. * of the License, or (at your option) any later version.
  26. *
  27. * This program is distributed in the hope that it will be useful,
  28. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  29. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  30. * GNU General Public License for more details.
  31. *
  32. * You should have received a copy of the GNU General Public License
  33. * along with this program; if not, write to the Free Software
  34. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  35. * 02110-1301, USA.
  36. */
  37. #include <linux/input.h>
  38. #include <linux/slab.h>
  39. #include <media/ir-common.h>
  40. #include <media/v4l2-subdev.h>
  41. #include "cx23885.h"
  42. #define RC5_BITS 14
  43. #define RC5_HALF_BITS (2*RC5_BITS)
  44. #define RC5_HALF_BITS_MASK ((1 << RC5_HALF_BITS) - 1)
  45. #define RC5_START_BITS_NORMAL 0x3 /* Command range 0 - 63 */
  46. #define RC5_START_BITS_EXTENDED 0x2 /* Command range 64 - 127 */
  47. #define RC5_EXTENDED_COMMAND_OFFSET 64
  48. static inline unsigned int rc5_command(u32 rc5_baseband)
  49. {
  50. return RC5_INSTR(rc5_baseband) +
  51. ((RC5_START(rc5_baseband) == RC5_START_BITS_EXTENDED)
  52. ? RC5_EXTENDED_COMMAND_OFFSET : 0);
  53. }
  54. static void cx23885_input_process_raw_rc5(struct cx23885_dev *dev)
  55. {
  56. struct card_ir *ir_input = dev->ir_input;
  57. unsigned int code, command;
  58. u32 rc5;
  59. /* Ignore codes that are too short to be valid RC-5 */
  60. if (ir_input->last_bit < (RC5_HALF_BITS - 1))
  61. return;
  62. /* The library has the manchester coding backwards; XOR to adapt. */
  63. code = (ir_input->code & RC5_HALF_BITS_MASK) ^ RC5_HALF_BITS_MASK;
  64. rc5 = ir_rc5_decode(code);
  65. switch (RC5_START(rc5)) {
  66. case RC5_START_BITS_NORMAL:
  67. break;
  68. case RC5_START_BITS_EXTENDED:
  69. /* Don't allow if the remote only emits standard commands */
  70. if (ir_input->start == RC5_START_BITS_NORMAL)
  71. return;
  72. break;
  73. default:
  74. return;
  75. }
  76. if (ir_input->addr != RC5_ADDR(rc5))
  77. return;
  78. /* Don't generate a keypress for RC-5 auto-repeated keypresses */
  79. command = rc5_command(rc5);
  80. if (RC5_TOGGLE(rc5) != RC5_TOGGLE(ir_input->last_rc5) ||
  81. command != rc5_command(ir_input->last_rc5) ||
  82. /* Catch T == 0, CMD == 0 (e.g. '0') as first keypress after init */
  83. RC5_START(ir_input->last_rc5) == 0) {
  84. /* This keypress is differnet: not an auto repeat */
  85. ir_input_nokey(ir_input->dev, &ir_input->ir);
  86. ir_input_keydown(ir_input->dev, &ir_input->ir, command);
  87. }
  88. ir_input->last_rc5 = rc5;
  89. /* Schedule when we should do the key up event: ir_input_nokey() */
  90. mod_timer(&ir_input->timer_keyup,
  91. jiffies + msecs_to_jiffies(ir_input->rc5_key_timeout));
  92. }
  93. static void cx23885_input_next_pulse_width_rc5(struct cx23885_dev *dev,
  94. u32 ns_pulse)
  95. {
  96. const int rc5_quarterbit_ns = 444444; /* 32 cycles/36 kHz/2 = 444 us */
  97. struct card_ir *ir_input = dev->ir_input;
  98. int i, level, quarterbits, halfbits;
  99. if (!ir_input->active) {
  100. ir_input->active = 1;
  101. /* assume an initial space that we may not detect or measure */
  102. ir_input->code = 0;
  103. ir_input->last_bit = 0;
  104. }
  105. if (ns_pulse == V4L2_SUBDEV_IR_PULSE_RX_SEQ_END) {
  106. ir_input->last_bit++; /* Account for the final space */
  107. ir_input->active = 0;
  108. cx23885_input_process_raw_rc5(dev);
  109. return;
  110. }
  111. level = (ns_pulse & V4L2_SUBDEV_IR_PULSE_LEVEL_MASK) ? 1 : 0;
  112. /* Skip any leading space to sync to the start bit */
  113. if (ir_input->last_bit == 0 && level == 0)
  114. return;
  115. /*
  116. * With valid RC-5 we can get up to two consecutive half-bits in a
  117. * single pulse measurment. Experiments have shown that the duration
  118. * of a half-bit can vary. Make sure we always end up with an even
  119. * number of quarter bits at the same level (mark or space).
  120. */
  121. ns_pulse &= V4L2_SUBDEV_IR_PULSE_MAX_WIDTH_NS;
  122. quarterbits = ns_pulse / rc5_quarterbit_ns;
  123. if (quarterbits & 1)
  124. quarterbits++;
  125. halfbits = quarterbits / 2;
  126. for (i = 0; i < halfbits; i++) {
  127. ir_input->last_bit++;
  128. ir_input->code |= (level << ir_input->last_bit);
  129. if (ir_input->last_bit >= RC5_HALF_BITS-1) {
  130. ir_input->active = 0;
  131. cx23885_input_process_raw_rc5(dev);
  132. /*
  133. * If level is 1, a leading mark is invalid for RC5.
  134. * If level is 0, we scan past extra intial space.
  135. * Either way we don't want to reactivate collecting
  136. * marks or spaces here with any left over half-bits.
  137. */
  138. break;
  139. }
  140. }
  141. }
  142. static void cx23885_input_process_pulse_widths_rc5(struct cx23885_dev *dev,
  143. bool add_eom)
  144. {
  145. struct card_ir *ir_input = dev->ir_input;
  146. struct ir_input_state *ir_input_state = &ir_input->ir;
  147. u32 ns_pulse[RC5_HALF_BITS+1];
  148. ssize_t num = 0;
  149. int count, i;
  150. do {
  151. v4l2_subdev_call(dev->sd_ir, ir, rx_read, (u8 *) ns_pulse,
  152. sizeof(ns_pulse), &num);
  153. count = num / sizeof(u32);
  154. /* Append an end of Rx seq, if the caller requested */
  155. if (add_eom && count < ARRAY_SIZE(ns_pulse)) {
  156. ns_pulse[count] = V4L2_SUBDEV_IR_PULSE_RX_SEQ_END;
  157. count++;
  158. }
  159. /* Just drain the Rx FIFO, if we're called, but not RC-5 */
  160. if (ir_input_state->ir_type != IR_TYPE_RC5)
  161. continue;
  162. for (i = 0; i < count; i++)
  163. cx23885_input_next_pulse_width_rc5(dev, ns_pulse[i]);
  164. } while (num != 0);
  165. }
  166. void cx23885_input_rx_work_handler(struct cx23885_dev *dev, u32 events)
  167. {
  168. struct v4l2_subdev_ir_parameters params;
  169. int overrun, data_available;
  170. if (dev->sd_ir == NULL || events == 0)
  171. return;
  172. switch (dev->board) {
  173. case CX23885_BOARD_HAUPPAUGE_HVR1850:
  174. case CX23885_BOARD_HAUPPAUGE_HVR1290:
  175. /*
  176. * The only board we handle right now. However other boards
  177. * using the CX2388x integrated IR controller should be similar
  178. */
  179. break;
  180. default:
  181. return;
  182. }
  183. overrun = events & (V4L2_SUBDEV_IR_RX_SW_FIFO_OVERRUN |
  184. V4L2_SUBDEV_IR_RX_HW_FIFO_OVERRUN);
  185. data_available = events & (V4L2_SUBDEV_IR_RX_END_OF_RX_DETECTED |
  186. V4L2_SUBDEV_IR_RX_FIFO_SERVICE_REQ);
  187. if (overrun) {
  188. /* If there was a FIFO overrun, stop the device */
  189. v4l2_subdev_call(dev->sd_ir, ir, rx_g_parameters, &params);
  190. params.enable = false;
  191. /* Mitigate race with cx23885_input_ir_stop() */
  192. params.shutdown = atomic_read(&dev->ir_input_stopping);
  193. v4l2_subdev_call(dev->sd_ir, ir, rx_s_parameters, &params);
  194. }
  195. if (data_available)
  196. cx23885_input_process_pulse_widths_rc5(dev, overrun);
  197. if (overrun) {
  198. /* If there was a FIFO overrun, clear & restart the device */
  199. params.enable = true;
  200. /* Mitigate race with cx23885_input_ir_stop() */
  201. params.shutdown = atomic_read(&dev->ir_input_stopping);
  202. v4l2_subdev_call(dev->sd_ir, ir, rx_s_parameters, &params);
  203. }
  204. }
  205. static void cx23885_input_ir_start(struct cx23885_dev *dev)
  206. {
  207. struct card_ir *ir_input = dev->ir_input;
  208. struct ir_input_state *ir_input_state = &ir_input->ir;
  209. struct v4l2_subdev_ir_parameters params;
  210. if (dev->sd_ir == NULL)
  211. return;
  212. atomic_set(&dev->ir_input_stopping, 0);
  213. /* keyup timer set up, if needed */
  214. switch (dev->board) {
  215. case CX23885_BOARD_HAUPPAUGE_HVR1850:
  216. case CX23885_BOARD_HAUPPAUGE_HVR1290:
  217. setup_timer(&ir_input->timer_keyup,
  218. ir_rc5_timer_keyup, /* Not actually RC-5 specific */
  219. (unsigned long) ir_input);
  220. if (ir_input_state->ir_type == IR_TYPE_RC5) {
  221. /*
  222. * RC-5 repeats a held key every
  223. * 64 bits * (2 * 32/36000) sec/bit = 113.778 ms
  224. */
  225. ir_input->rc5_key_timeout = 115;
  226. }
  227. break;
  228. }
  229. v4l2_subdev_call(dev->sd_ir, ir, rx_g_parameters, &params);
  230. switch (dev->board) {
  231. case CX23885_BOARD_HAUPPAUGE_HVR1850:
  232. case CX23885_BOARD_HAUPPAUGE_HVR1290:
  233. /*
  234. * The IR controller on this board only returns pulse widths.
  235. * Any other mode setting will fail to set up the device.
  236. */
  237. params.mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH;
  238. params.enable = true;
  239. params.interrupt_enable = true;
  240. params.shutdown = false;
  241. /* Setup for baseband compatible with both RC-5 and RC-6A */
  242. params.modulation = false;
  243. /* RC-5: 2,222,222 ns = 1/36 kHz * 32 cycles * 2 marks * 1.25*/
  244. /* RC-6A: 3,333,333 ns = 1/36 kHz * 16 cycles * 6 marks * 1.25*/
  245. params.max_pulse_width = 3333333; /* ns */
  246. /* RC-5: 666,667 ns = 1/36 kHz * 32 cycles * 1 mark * 0.75 */
  247. /* RC-6A: 333,333 ns = 1/36 kHz * 16 cycles * 1 mark * 0.75 */
  248. params.noise_filter_min_width = 333333; /* ns */
  249. /*
  250. * This board has inverted receive sense:
  251. * mark is received as low logic level;
  252. * falling edges are detected as rising edges; etc.
  253. */
  254. params.invert = true;
  255. break;
  256. }
  257. v4l2_subdev_call(dev->sd_ir, ir, rx_s_parameters, &params);
  258. }
  259. static void cx23885_input_ir_stop(struct cx23885_dev *dev)
  260. {
  261. struct card_ir *ir_input = dev->ir_input;
  262. struct v4l2_subdev_ir_parameters params;
  263. if (dev->sd_ir == NULL)
  264. return;
  265. /*
  266. * Stop the sd_ir subdevice from generating notifications and
  267. * scheduling work.
  268. * It is shutdown this way in order to mitigate a race with
  269. * cx23885_input_rx_work_handler() in the overrun case, which could
  270. * re-enable the subdevice.
  271. */
  272. atomic_set(&dev->ir_input_stopping, 1);
  273. v4l2_subdev_call(dev->sd_ir, ir, rx_g_parameters, &params);
  274. while (params.shutdown == false) {
  275. params.enable = false;
  276. params.interrupt_enable = false;
  277. params.shutdown = true;
  278. v4l2_subdev_call(dev->sd_ir, ir, rx_s_parameters, &params);
  279. v4l2_subdev_call(dev->sd_ir, ir, rx_g_parameters, &params);
  280. }
  281. flush_scheduled_work();
  282. switch (dev->board) {
  283. case CX23885_BOARD_HAUPPAUGE_HVR1850:
  284. case CX23885_BOARD_HAUPPAUGE_HVR1290:
  285. del_timer_sync(&ir_input->timer_keyup);
  286. break;
  287. }
  288. }
  289. int cx23885_input_init(struct cx23885_dev *dev)
  290. {
  291. struct card_ir *ir;
  292. struct input_dev *input_dev;
  293. struct ir_scancode_table *ir_codes = NULL;
  294. int ir_type, ir_addr, ir_start;
  295. int ret;
  296. /*
  297. * If the IR device (hardware registers, chip, GPIO lines, etc.) isn't
  298. * encapsulated in a v4l2_subdev, then I'm not going to deal with it.
  299. */
  300. if (dev->sd_ir == NULL)
  301. return -ENODEV;
  302. switch (dev->board) {
  303. case CX23885_BOARD_HAUPPAUGE_HVR1850:
  304. case CX23885_BOARD_HAUPPAUGE_HVR1290:
  305. /* Parameters for the grey Hauppauge remote for the HVR-1850 */
  306. ir_codes = &ir_codes_hauppauge_new_table;
  307. ir_type = IR_TYPE_RC5;
  308. ir_addr = 0x1e; /* RC-5 system bits emitted by the remote */
  309. ir_start = RC5_START_BITS_NORMAL; /* A basic RC-5 remote */
  310. break;
  311. }
  312. if (ir_codes == NULL)
  313. return -ENODEV;
  314. ir = kzalloc(sizeof(*ir), GFP_KERNEL);
  315. input_dev = input_allocate_device();
  316. if (!ir || !input_dev) {
  317. ret = -ENOMEM;
  318. goto err_out_free;
  319. }
  320. ir->dev = input_dev;
  321. ir->addr = ir_addr;
  322. ir->start = ir_start;
  323. /* init input device */
  324. snprintf(ir->name, sizeof(ir->name), "cx23885 IR (%s)",
  325. cx23885_boards[dev->board].name);
  326. snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0", pci_name(dev->pci));
  327. ret = ir_input_init(input_dev, &ir->ir, ir_type);
  328. if (ret < 0)
  329. goto err_out_free;
  330. input_dev->name = ir->name;
  331. input_dev->phys = ir->phys;
  332. input_dev->id.bustype = BUS_PCI;
  333. input_dev->id.version = 1;
  334. if (dev->pci->subsystem_vendor) {
  335. input_dev->id.vendor = dev->pci->subsystem_vendor;
  336. input_dev->id.product = dev->pci->subsystem_device;
  337. } else {
  338. input_dev->id.vendor = dev->pci->vendor;
  339. input_dev->id.product = dev->pci->device;
  340. }
  341. input_dev->dev.parent = &dev->pci->dev;
  342. dev->ir_input = ir;
  343. cx23885_input_ir_start(dev);
  344. ret = ir_input_register(ir->dev, ir_codes, NULL);
  345. if (ret)
  346. goto err_out_stop;
  347. return 0;
  348. err_out_stop:
  349. cx23885_input_ir_stop(dev);
  350. dev->ir_input = NULL;
  351. err_out_free:
  352. kfree(ir);
  353. return ret;
  354. }
  355. void cx23885_input_fini(struct cx23885_dev *dev)
  356. {
  357. /* Always stop the IR hardware from generating interrupts */
  358. cx23885_input_ir_stop(dev);
  359. if (dev->ir_input == NULL)
  360. return;
  361. ir_input_unregister(dev->ir_input->dev);
  362. kfree(dev->ir_input);
  363. dev->ir_input = NULL;
  364. }