cx23885-input.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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/slab.h>
  38. #include <media/rc-core.h>
  39. #include <media/v4l2-subdev.h>
  40. #include "cx23885.h"
  41. #define MODULE_NAME "cx23885"
  42. static void cx23885_input_process_measurements(struct cx23885_dev *dev,
  43. bool overrun)
  44. {
  45. struct cx23885_kernel_ir *kernel_ir = dev->kernel_ir;
  46. ssize_t num;
  47. int count, i;
  48. bool handle = false;
  49. struct ir_raw_event ir_core_event[64];
  50. do {
  51. num = 0;
  52. v4l2_subdev_call(dev->sd_ir, ir, rx_read, (u8 *) ir_core_event,
  53. sizeof(ir_core_event), &num);
  54. count = num / sizeof(struct ir_raw_event);
  55. for (i = 0; i < count; i++) {
  56. ir_raw_event_store(kernel_ir->rc,
  57. &ir_core_event[i]);
  58. handle = true;
  59. }
  60. } while (num != 0);
  61. if (overrun)
  62. ir_raw_event_reset(kernel_ir->rc);
  63. else if (handle)
  64. ir_raw_event_handle(kernel_ir->rc);
  65. }
  66. void cx23885_input_rx_work_handler(struct cx23885_dev *dev, u32 events)
  67. {
  68. struct v4l2_subdev_ir_parameters params;
  69. int overrun, data_available;
  70. if (dev->sd_ir == NULL || events == 0)
  71. return;
  72. switch (dev->board) {
  73. case CX23885_BOARD_HAUPPAUGE_HVR1850:
  74. case CX23885_BOARD_HAUPPAUGE_HVR1290:
  75. case CX23885_BOARD_TEVII_S470:
  76. case CX23885_BOARD_HAUPPAUGE_HVR1250:
  77. /*
  78. * The only boards we handle right now. However other boards
  79. * using the CX2388x integrated IR controller should be similar
  80. */
  81. break;
  82. default:
  83. return;
  84. }
  85. overrun = events & (V4L2_SUBDEV_IR_RX_SW_FIFO_OVERRUN |
  86. V4L2_SUBDEV_IR_RX_HW_FIFO_OVERRUN);
  87. data_available = events & (V4L2_SUBDEV_IR_RX_END_OF_RX_DETECTED |
  88. V4L2_SUBDEV_IR_RX_FIFO_SERVICE_REQ);
  89. if (overrun) {
  90. /* If there was a FIFO overrun, stop the device */
  91. v4l2_subdev_call(dev->sd_ir, ir, rx_g_parameters, &params);
  92. params.enable = false;
  93. /* Mitigate race with cx23885_input_ir_stop() */
  94. params.shutdown = atomic_read(&dev->ir_input_stopping);
  95. v4l2_subdev_call(dev->sd_ir, ir, rx_s_parameters, &params);
  96. }
  97. if (data_available)
  98. cx23885_input_process_measurements(dev, overrun);
  99. if (overrun) {
  100. /* If there was a FIFO overrun, clear & restart the device */
  101. params.enable = true;
  102. /* Mitigate race with cx23885_input_ir_stop() */
  103. params.shutdown = atomic_read(&dev->ir_input_stopping);
  104. v4l2_subdev_call(dev->sd_ir, ir, rx_s_parameters, &params);
  105. }
  106. }
  107. static int cx23885_input_ir_start(struct cx23885_dev *dev)
  108. {
  109. struct v4l2_subdev_ir_parameters params;
  110. if (dev->sd_ir == NULL)
  111. return -ENODEV;
  112. atomic_set(&dev->ir_input_stopping, 0);
  113. v4l2_subdev_call(dev->sd_ir, ir, rx_g_parameters, &params);
  114. switch (dev->board) {
  115. case CX23885_BOARD_HAUPPAUGE_HVR1850:
  116. case CX23885_BOARD_HAUPPAUGE_HVR1290:
  117. case CX23885_BOARD_HAUPPAUGE_HVR1250:
  118. /*
  119. * The IR controller on this board only returns pulse widths.
  120. * Any other mode setting will fail to set up the device.
  121. */
  122. params.mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH;
  123. params.enable = true;
  124. params.interrupt_enable = true;
  125. params.shutdown = false;
  126. /* Setup for baseband compatible with both RC-5 and RC-6A */
  127. params.modulation = false;
  128. /* RC-5: 2,222,222 ns = 1/36 kHz * 32 cycles * 2 marks * 1.25*/
  129. /* RC-6A: 3,333,333 ns = 1/36 kHz * 16 cycles * 6 marks * 1.25*/
  130. params.max_pulse_width = 3333333; /* ns */
  131. /* RC-5: 666,667 ns = 1/36 kHz * 32 cycles * 1 mark * 0.75 */
  132. /* RC-6A: 333,333 ns = 1/36 kHz * 16 cycles * 1 mark * 0.75 */
  133. params.noise_filter_min_width = 333333; /* ns */
  134. /*
  135. * This board has inverted receive sense:
  136. * mark is received as low logic level;
  137. * falling edges are detected as rising edges; etc.
  138. */
  139. params.invert_level = true;
  140. break;
  141. case CX23885_BOARD_TEVII_S470:
  142. /*
  143. * The IR controller on this board only returns pulse widths.
  144. * Any other mode setting will fail to set up the device.
  145. */
  146. params.mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH;
  147. params.enable = true;
  148. params.interrupt_enable = true;
  149. params.shutdown = false;
  150. /* Setup for a standard NEC protocol */
  151. params.carrier_freq = 37917; /* Hz, 455 kHz/12 for NEC */
  152. params.carrier_range_lower = 33000; /* Hz */
  153. params.carrier_range_upper = 43000; /* Hz */
  154. params.duty_cycle = 33; /* percent, 33 percent for NEC */
  155. /*
  156. * NEC max pulse width: (64/3)/(455 kHz/12) * 16 nec_units
  157. * (64/3)/(455 kHz/12) * 16 nec_units * 1.375 = 12378022 ns
  158. */
  159. params.max_pulse_width = 12378022; /* ns */
  160. /*
  161. * NEC noise filter min width: (64/3)/(455 kHz/12) * 1 nec_unit
  162. * (64/3)/(455 kHz/12) * 1 nec_units * 0.625 = 351648 ns
  163. */
  164. params.noise_filter_min_width = 351648; /* ns */
  165. params.modulation = false;
  166. params.invert_level = true;
  167. break;
  168. }
  169. v4l2_subdev_call(dev->sd_ir, ir, rx_s_parameters, &params);
  170. return 0;
  171. }
  172. static int cx23885_input_ir_open(struct rc_dev *rc)
  173. {
  174. struct cx23885_kernel_ir *kernel_ir = rc->priv;
  175. if (kernel_ir->cx == NULL)
  176. return -ENODEV;
  177. return cx23885_input_ir_start(kernel_ir->cx);
  178. }
  179. static void cx23885_input_ir_stop(struct cx23885_dev *dev)
  180. {
  181. struct v4l2_subdev_ir_parameters params;
  182. if (dev->sd_ir == NULL)
  183. return;
  184. /*
  185. * Stop the sd_ir subdevice from generating notifications and
  186. * scheduling work.
  187. * It is shutdown this way in order to mitigate a race with
  188. * cx23885_input_rx_work_handler() in the overrun case, which could
  189. * re-enable the subdevice.
  190. */
  191. atomic_set(&dev->ir_input_stopping, 1);
  192. v4l2_subdev_call(dev->sd_ir, ir, rx_g_parameters, &params);
  193. while (params.shutdown == false) {
  194. params.enable = false;
  195. params.interrupt_enable = false;
  196. params.shutdown = true;
  197. v4l2_subdev_call(dev->sd_ir, ir, rx_s_parameters, &params);
  198. v4l2_subdev_call(dev->sd_ir, ir, rx_g_parameters, &params);
  199. }
  200. }
  201. static void cx23885_input_ir_close(struct rc_dev *rc)
  202. {
  203. struct cx23885_kernel_ir *kernel_ir = rc->priv;
  204. if (kernel_ir->cx != NULL)
  205. cx23885_input_ir_stop(kernel_ir->cx);
  206. }
  207. int cx23885_input_init(struct cx23885_dev *dev)
  208. {
  209. struct cx23885_kernel_ir *kernel_ir;
  210. struct rc_dev *rc;
  211. char *rc_map;
  212. enum rc_driver_type driver_type;
  213. unsigned long allowed_protos;
  214. int ret;
  215. /*
  216. * If the IR device (hardware registers, chip, GPIO lines, etc.) isn't
  217. * encapsulated in a v4l2_subdev, then I'm not going to deal with it.
  218. */
  219. if (dev->sd_ir == NULL)
  220. return -ENODEV;
  221. switch (dev->board) {
  222. case CX23885_BOARD_HAUPPAUGE_HVR1850:
  223. case CX23885_BOARD_HAUPPAUGE_HVR1290:
  224. case CX23885_BOARD_HAUPPAUGE_HVR1250:
  225. /* Integrated CX2388[58] IR controller */
  226. driver_type = RC_DRIVER_IR_RAW;
  227. allowed_protos = RC_TYPE_ALL;
  228. /* The grey Hauppauge RC-5 remote */
  229. rc_map = RC_MAP_HAUPPAUGE;
  230. break;
  231. case CX23885_BOARD_TEVII_S470:
  232. /* Integrated CX23885 IR controller */
  233. driver_type = RC_DRIVER_IR_RAW;
  234. allowed_protos = RC_TYPE_ALL;
  235. /* A guess at the remote */
  236. rc_map = RC_MAP_TEVII_NEC;
  237. break;
  238. default:
  239. return -ENODEV;
  240. }
  241. /* cx23885 board instance kernel IR state */
  242. kernel_ir = kzalloc(sizeof(struct cx23885_kernel_ir), GFP_KERNEL);
  243. if (kernel_ir == NULL)
  244. return -ENOMEM;
  245. kernel_ir->cx = dev;
  246. kernel_ir->name = kasprintf(GFP_KERNEL, "cx23885 IR (%s)",
  247. cx23885_boards[dev->board].name);
  248. kernel_ir->phys = kasprintf(GFP_KERNEL, "pci-%s/ir0",
  249. pci_name(dev->pci));
  250. /* input device */
  251. rc = rc_allocate_device();
  252. if (!rc) {
  253. ret = -ENOMEM;
  254. goto err_out_free;
  255. }
  256. kernel_ir->rc = rc;
  257. rc->input_name = kernel_ir->name;
  258. rc->input_phys = kernel_ir->phys;
  259. rc->input_id.bustype = BUS_PCI;
  260. rc->input_id.version = 1;
  261. if (dev->pci->subsystem_vendor) {
  262. rc->input_id.vendor = dev->pci->subsystem_vendor;
  263. rc->input_id.product = dev->pci->subsystem_device;
  264. } else {
  265. rc->input_id.vendor = dev->pci->vendor;
  266. rc->input_id.product = dev->pci->device;
  267. }
  268. rc->dev.parent = &dev->pci->dev;
  269. rc->driver_type = driver_type;
  270. rc->allowed_protos = allowed_protos;
  271. rc->priv = kernel_ir;
  272. rc->open = cx23885_input_ir_open;
  273. rc->close = cx23885_input_ir_close;
  274. rc->map_name = rc_map;
  275. rc->driver_name = MODULE_NAME;
  276. /* Go */
  277. dev->kernel_ir = kernel_ir;
  278. ret = rc_register_device(rc);
  279. if (ret)
  280. goto err_out_stop;
  281. return 0;
  282. err_out_stop:
  283. cx23885_input_ir_stop(dev);
  284. dev->kernel_ir = NULL;
  285. rc_free_device(rc);
  286. err_out_free:
  287. kfree(kernel_ir->phys);
  288. kfree(kernel_ir->name);
  289. kfree(kernel_ir);
  290. return ret;
  291. }
  292. void cx23885_input_fini(struct cx23885_dev *dev)
  293. {
  294. /* Always stop the IR hardware from generating interrupts */
  295. cx23885_input_ir_stop(dev);
  296. if (dev->kernel_ir == NULL)
  297. return;
  298. rc_unregister_device(dev->kernel_ir->rc);
  299. kfree(dev->kernel_ir->phys);
  300. kfree(dev->kernel_ir->name);
  301. kfree(dev->kernel_ir);
  302. dev->kernel_ir = NULL;
  303. }