cx23885-input.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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 <media/ir-common.h>
  39. #include <media/v4l2-subdev.h>
  40. #include "cx23885.h"
  41. #define RC5_BITS 14
  42. #define RC5_HALF_BITS (2*RC5_BITS)
  43. #define RC5_HALF_BITS_MASK ((1 << RC5_HALF_BITS) - 1)
  44. #define RC5_START_BITS_NORMAL 0x3 /* Command range 0 - 63 */
  45. #define RC5_START_BITS_EXTENDED 0x2 /* Command range 64 - 127 */
  46. #define RC5_EXTENDED_COMMAND_OFFSET 64
  47. static inline unsigned int rc5_command(u32 rc5_baseband)
  48. {
  49. return RC5_INSTR(rc5_baseband) +
  50. ((RC5_START(rc5_baseband) == RC5_START_BITS_EXTENDED)
  51. ? RC5_EXTENDED_COMMAND_OFFSET : 0);
  52. }
  53. static void cx23885_input_process_raw_rc5(struct cx23885_dev *dev)
  54. {
  55. struct card_ir *ir_input = dev->ir_input;
  56. unsigned int code, command;
  57. u32 rc5;
  58. /* Ignore codes that are too short to be valid RC-5 */
  59. if (ir_input->last_bit < (RC5_HALF_BITS - 1))
  60. return;
  61. /* The library has the manchester coding backwards; XOR to adapt. */
  62. code = (ir_input->code & RC5_HALF_BITS_MASK) ^ RC5_HALF_BITS_MASK;
  63. rc5 = ir_rc5_decode(code);
  64. switch (RC5_START(rc5)) {
  65. case RC5_START_BITS_NORMAL:
  66. break;
  67. case RC5_START_BITS_EXTENDED:
  68. /* Don't allow if the remote only emits standard commands */
  69. if (ir_input->start == RC5_START_BITS_NORMAL)
  70. return;
  71. break;
  72. default:
  73. return;
  74. }
  75. if (ir_input->addr != RC5_ADDR(rc5))
  76. return;
  77. /* Don't generate a keypress for RC-5 auto-repeated keypresses */
  78. command = rc5_command(rc5);
  79. if (RC5_TOGGLE(rc5) != RC5_TOGGLE(ir_input->last_rc5) ||
  80. command != rc5_command(ir_input->last_rc5) ||
  81. /* Catch T == 0, CMD == 0 (e.g. '0') as first keypress after init */
  82. RC5_START(ir_input->last_rc5) == 0) {
  83. /* This keypress is differnet: not an auto repeat */
  84. ir_input_nokey(ir_input->dev, &ir_input->ir);
  85. ir_input_keydown(ir_input->dev, &ir_input->ir,
  86. command, ir_input->code);
  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. /*
  175. * The only board we handle right now. However other boards
  176. * using the CX2388x integrated IR controller should be similar
  177. */
  178. break;
  179. default:
  180. return;
  181. }
  182. overrun = events & (V4L2_SUBDEV_IR_RX_SW_FIFO_OVERRUN |
  183. V4L2_SUBDEV_IR_RX_HW_FIFO_OVERRUN);
  184. data_available = events & (V4L2_SUBDEV_IR_RX_END_OF_RX_DETECTED |
  185. V4L2_SUBDEV_IR_RX_FIFO_SERVICE_REQ);
  186. if (overrun) {
  187. /* If there was a FIFO overrun, stop the device */
  188. v4l2_subdev_call(dev->sd_ir, ir, rx_g_parameters, &params);
  189. params.enable = false;
  190. /* Mitigate race with cx23885_input_ir_stop() */
  191. params.shutdown = atomic_read(&dev->ir_input_stopping);
  192. v4l2_subdev_call(dev->sd_ir, ir, rx_s_parameters, &params);
  193. }
  194. if (data_available)
  195. cx23885_input_process_pulse_widths_rc5(dev, overrun);
  196. if (overrun) {
  197. /* If there was a FIFO overrun, clear & restart the device */
  198. params.enable = true;
  199. /* Mitigate race with cx23885_input_ir_stop() */
  200. params.shutdown = atomic_read(&dev->ir_input_stopping);
  201. v4l2_subdev_call(dev->sd_ir, ir, rx_s_parameters, &params);
  202. }
  203. }
  204. static void cx23885_input_ir_start(struct cx23885_dev *dev)
  205. {
  206. struct card_ir *ir_input = dev->ir_input;
  207. struct ir_input_state *ir_input_state = &ir_input->ir;
  208. struct v4l2_subdev_ir_parameters params;
  209. if (dev->sd_ir == NULL)
  210. return;
  211. atomic_set(&dev->ir_input_stopping, 0);
  212. /* keyup timer set up, if needed */
  213. switch (dev->board) {
  214. case CX23885_BOARD_HAUPPAUGE_HVR1850:
  215. setup_timer(&ir_input->timer_keyup,
  216. ir_rc5_timer_keyup, /* Not actually RC-5 specific */
  217. (unsigned long) ir_input);
  218. if (ir_input_state->ir_type == IR_TYPE_RC5) {
  219. /*
  220. * RC-5 repeats a held key every
  221. * 64 bits * (2 * 32/36000) sec/bit = 113.778 ms
  222. */
  223. ir_input->rc5_key_timeout = 115;
  224. }
  225. break;
  226. }
  227. v4l2_subdev_call(dev->sd_ir, ir, rx_g_parameters, &params);
  228. switch (dev->board) {
  229. case CX23885_BOARD_HAUPPAUGE_HVR1850:
  230. /*
  231. * The IR controller on this board only returns pulse widths.
  232. * Any other mode setting will fail to set up the device.
  233. */
  234. params.mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH;
  235. params.enable = true;
  236. params.interrupt_enable = true;
  237. params.shutdown = false;
  238. /* Setup for baseband compatible with both RC-5 and RC-6A */
  239. params.modulation = false;
  240. /* RC-5: 2,222,222 ns = 1/36 kHz * 32 cycles * 2 marks * 1.25*/
  241. /* RC-6A: 3,333,333 ns = 1/36 kHz * 16 cycles * 6 marks * 1.25*/
  242. params.max_pulse_width = 3333333; /* ns */
  243. /* RC-5: 666,667 ns = 1/36 kHz * 32 cycles * 1 mark * 0.75 */
  244. /* RC-6A: 333,333 ns = 1/36 kHz * 16 cycles * 1 mark * 0.75 */
  245. params.noise_filter_min_width = 333333; /* ns */
  246. /*
  247. * This board has inverted receive sense:
  248. * mark is received as low logic level;
  249. * falling edges are detected as rising edges; etc.
  250. */
  251. params.invert = true;
  252. break;
  253. }
  254. v4l2_subdev_call(dev->sd_ir, ir, rx_s_parameters, &params);
  255. }
  256. static void cx23885_input_ir_stop(struct cx23885_dev *dev)
  257. {
  258. struct card_ir *ir_input = dev->ir_input;
  259. struct v4l2_subdev_ir_parameters params;
  260. if (dev->sd_ir == NULL)
  261. return;
  262. /*
  263. * Stop the sd_ir subdevice from generating notifications and
  264. * scheduling work.
  265. * It is shutdown this way in order to mitigate a race with
  266. * cx23885_input_rx_work_handler() in the overrun case, which could
  267. * re-enable the subdevice.
  268. */
  269. atomic_set(&dev->ir_input_stopping, 1);
  270. v4l2_subdev_call(dev->sd_ir, ir, rx_g_parameters, &params);
  271. while (params.shutdown == false) {
  272. params.enable = false;
  273. params.interrupt_enable = false;
  274. params.shutdown = true;
  275. v4l2_subdev_call(dev->sd_ir, ir, rx_s_parameters, &params);
  276. v4l2_subdev_call(dev->sd_ir, ir, rx_g_parameters, &params);
  277. }
  278. flush_scheduled_work();
  279. switch (dev->board) {
  280. case CX23885_BOARD_HAUPPAUGE_HVR1850:
  281. del_timer_sync(&ir_input->timer_keyup);
  282. break;
  283. }
  284. }
  285. int cx23885_input_init(struct cx23885_dev *dev)
  286. {
  287. struct card_ir *ir;
  288. struct input_dev *input_dev;
  289. struct ir_scancode_table *ir_codes = NULL;
  290. int ir_type, ir_addr, ir_start;
  291. int ret;
  292. /*
  293. * If the IR device (hardware registers, chip, GPIO lines, etc.) isn't
  294. * encapsulated in a v4l2_subdev, then I'm not going to deal with it.
  295. */
  296. if (dev->sd_ir == NULL)
  297. return -ENODEV;
  298. switch (dev->board) {
  299. case CX23885_BOARD_HAUPPAUGE_HVR1850:
  300. /* Parameters for the grey Hauppauge remote for the HVR-1850 */
  301. ir_codes = &ir_codes_hauppauge_new_table;
  302. ir_type = IR_TYPE_RC5;
  303. ir_addr = 0x1e; /* RC-5 system bits emitted by the remote */
  304. ir_start = RC5_START_BITS_NORMAL; /* A basic RC-5 remote */
  305. break;
  306. }
  307. if (ir_codes == NULL)
  308. return -ENODEV;
  309. ir = kzalloc(sizeof(*ir), GFP_KERNEL);
  310. input_dev = input_allocate_device();
  311. if (!ir || !input_dev) {
  312. ret = -ENOMEM;
  313. goto err_out_free;
  314. }
  315. ir->dev = input_dev;
  316. ir->addr = ir_addr;
  317. ir->start = ir_start;
  318. /* init input device */
  319. snprintf(ir->name, sizeof(ir->name), "cx23885 IR (%s)",
  320. cx23885_boards[dev->board].name);
  321. snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0", pci_name(dev->pci));
  322. ir_input_init(input_dev, &ir->ir, ir_type, ir_codes);
  323. input_dev->name = ir->name;
  324. input_dev->phys = ir->phys;
  325. input_dev->id.bustype = BUS_PCI;
  326. input_dev->id.version = 1;
  327. if (dev->pci->subsystem_vendor) {
  328. input_dev->id.vendor = dev->pci->subsystem_vendor;
  329. input_dev->id.product = dev->pci->subsystem_device;
  330. } else {
  331. input_dev->id.vendor = dev->pci->vendor;
  332. input_dev->id.product = dev->pci->device;
  333. }
  334. input_dev->dev.parent = &dev->pci->dev;
  335. dev->ir_input = ir;
  336. cx23885_input_ir_start(dev);
  337. ret = input_register_device(ir->dev);
  338. if (ret)
  339. goto err_out_stop;
  340. return 0;
  341. err_out_stop:
  342. cx23885_input_ir_stop(dev);
  343. dev->ir_input = NULL;
  344. err_out_free:
  345. input_free_device(input_dev);
  346. kfree(ir);
  347. return ret;
  348. }
  349. void cx23885_input_fini(struct cx23885_dev *dev)
  350. {
  351. /* Always stop the IR hardware from generating interrupts */
  352. cx23885_input_ir_stop(dev);
  353. if (dev->ir_input == NULL)
  354. return;
  355. input_unregister_device(dev->ir_input->dev);
  356. kfree(dev->ir_input);
  357. dev->ir_input = NULL;
  358. }