cx23885-input.c 9.0 KB

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