cx23885-input.c 12 KB

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