cx23885-input.c 10 KB

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