cx23885-input.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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. #include <linux/slab.h>
  33. #include <media/rc-core.h>
  34. #include <media/v4l2-subdev.h>
  35. #include "cx23885.h"
  36. #include "cx23885-input.h"
  37. #define MODULE_NAME "cx23885"
  38. static void cx23885_input_process_measurements(struct cx23885_dev *dev,
  39. bool overrun)
  40. {
  41. struct cx23885_kernel_ir *kernel_ir = dev->kernel_ir;
  42. ssize_t num;
  43. int count, i;
  44. bool handle = false;
  45. struct ir_raw_event ir_core_event[64];
  46. do {
  47. num = 0;
  48. v4l2_subdev_call(dev->sd_ir, ir, rx_read, (u8 *) ir_core_event,
  49. sizeof(ir_core_event), &num);
  50. count = num / sizeof(struct ir_raw_event);
  51. for (i = 0; i < count; i++) {
  52. ir_raw_event_store(kernel_ir->rc,
  53. &ir_core_event[i]);
  54. handle = true;
  55. }
  56. } while (num != 0);
  57. if (overrun)
  58. ir_raw_event_reset(kernel_ir->rc);
  59. else if (handle)
  60. ir_raw_event_handle(kernel_ir->rc);
  61. }
  62. void cx23885_input_rx_work_handler(struct cx23885_dev *dev, u32 events)
  63. {
  64. struct v4l2_subdev_ir_parameters params;
  65. int overrun, data_available;
  66. if (dev->sd_ir == NULL || events == 0)
  67. return;
  68. switch (dev->board) {
  69. case CX23885_BOARD_HAUPPAUGE_HVR1270:
  70. case CX23885_BOARD_HAUPPAUGE_HVR1850:
  71. case CX23885_BOARD_HAUPPAUGE_HVR1290:
  72. case CX23885_BOARD_TERRATEC_CINERGY_T_PCIE_DUAL:
  73. case CX23885_BOARD_TEVII_S470:
  74. case CX23885_BOARD_HAUPPAUGE_HVR1250:
  75. case CX23885_BOARD_MYGICA_X8507:
  76. case CX23885_BOARD_TBS_6980:
  77. case CX23885_BOARD_TBS_6981:
  78. case CX23885_BOARD_DVBSKY_T9580:
  79. case CX23885_BOARD_DVBSKY_T980C:
  80. case CX23885_BOARD_DVBSKY_S950C:
  81. case CX23885_BOARD_TT_CT2_4500_CI:
  82. case CX23885_BOARD_DVBSKY_S950:
  83. case CX23885_BOARD_DVBSKY_S952:
  84. case CX23885_BOARD_DVBSKY_T982:
  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_HVR1270:
  124. case CX23885_BOARD_HAUPPAUGE_HVR1850:
  125. case CX23885_BOARD_HAUPPAUGE_HVR1290:
  126. case CX23885_BOARD_HAUPPAUGE_HVR1250:
  127. case CX23885_BOARD_MYGICA_X8507:
  128. case CX23885_BOARD_DVBSKY_T9580:
  129. case CX23885_BOARD_DVBSKY_T980C:
  130. case CX23885_BOARD_DVBSKY_S950C:
  131. case CX23885_BOARD_TT_CT2_4500_CI:
  132. case CX23885_BOARD_DVBSKY_S950:
  133. case CX23885_BOARD_DVBSKY_S952:
  134. case CX23885_BOARD_DVBSKY_T982:
  135. /*
  136. * The IR controller on this board only returns pulse widths.
  137. * Any other mode setting will fail to set up the device.
  138. */
  139. params.mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH;
  140. params.enable = true;
  141. params.interrupt_enable = true;
  142. params.shutdown = false;
  143. /* Setup for baseband compatible with both RC-5 and RC-6A */
  144. params.modulation = false;
  145. /* RC-5: 2,222,222 ns = 1/36 kHz * 32 cycles * 2 marks * 1.25*/
  146. /* RC-6A: 3,333,333 ns = 1/36 kHz * 16 cycles * 6 marks * 1.25*/
  147. params.max_pulse_width = 3333333; /* ns */
  148. /* RC-5: 666,667 ns = 1/36 kHz * 32 cycles * 1 mark * 0.75 */
  149. /* RC-6A: 333,333 ns = 1/36 kHz * 16 cycles * 1 mark * 0.75 */
  150. params.noise_filter_min_width = 333333; /* ns */
  151. /*
  152. * This board has inverted receive sense:
  153. * mark is received as low logic level;
  154. * falling edges are detected as rising edges; etc.
  155. */
  156. params.invert_level = true;
  157. break;
  158. case CX23885_BOARD_TERRATEC_CINERGY_T_PCIE_DUAL:
  159. case CX23885_BOARD_TEVII_S470:
  160. case CX23885_BOARD_TBS_6980:
  161. case CX23885_BOARD_TBS_6981:
  162. /*
  163. * The IR controller on this board only returns pulse widths.
  164. * Any other mode setting will fail to set up the device.
  165. */
  166. params.mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH;
  167. params.enable = true;
  168. params.interrupt_enable = true;
  169. params.shutdown = false;
  170. /* Setup for a standard NEC protocol */
  171. params.carrier_freq = 37917; /* Hz, 455 kHz/12 for NEC */
  172. params.carrier_range_lower = 33000; /* Hz */
  173. params.carrier_range_upper = 43000; /* Hz */
  174. params.duty_cycle = 33; /* percent, 33 percent for NEC */
  175. /*
  176. * NEC max pulse width: (64/3)/(455 kHz/12) * 16 nec_units
  177. * (64/3)/(455 kHz/12) * 16 nec_units * 1.375 = 12378022 ns
  178. */
  179. params.max_pulse_width = 12378022; /* ns */
  180. /*
  181. * NEC noise filter min width: (64/3)/(455 kHz/12) * 1 nec_unit
  182. * (64/3)/(455 kHz/12) * 1 nec_units * 0.625 = 351648 ns
  183. */
  184. params.noise_filter_min_width = 351648; /* ns */
  185. params.modulation = false;
  186. params.invert_level = true;
  187. break;
  188. }
  189. v4l2_subdev_call(dev->sd_ir, ir, rx_s_parameters, &params);
  190. return 0;
  191. }
  192. static int cx23885_input_ir_open(struct rc_dev *rc)
  193. {
  194. struct cx23885_kernel_ir *kernel_ir = rc->priv;
  195. if (kernel_ir->cx == NULL)
  196. return -ENODEV;
  197. return cx23885_input_ir_start(kernel_ir->cx);
  198. }
  199. static void cx23885_input_ir_stop(struct cx23885_dev *dev)
  200. {
  201. struct v4l2_subdev_ir_parameters params;
  202. if (dev->sd_ir == NULL)
  203. return;
  204. /*
  205. * Stop the sd_ir subdevice from generating notifications and
  206. * scheduling work.
  207. * It is shutdown this way in order to mitigate a race with
  208. * cx23885_input_rx_work_handler() in the overrun case, which could
  209. * re-enable the subdevice.
  210. */
  211. atomic_set(&dev->ir_input_stopping, 1);
  212. v4l2_subdev_call(dev->sd_ir, ir, rx_g_parameters, &params);
  213. while (params.shutdown == false) {
  214. params.enable = false;
  215. params.interrupt_enable = false;
  216. params.shutdown = true;
  217. v4l2_subdev_call(dev->sd_ir, ir, rx_s_parameters, &params);
  218. v4l2_subdev_call(dev->sd_ir, ir, rx_g_parameters, &params);
  219. }
  220. flush_work(&dev->cx25840_work);
  221. flush_work(&dev->ir_rx_work);
  222. flush_work(&dev->ir_tx_work);
  223. }
  224. static void cx23885_input_ir_close(struct rc_dev *rc)
  225. {
  226. struct cx23885_kernel_ir *kernel_ir = rc->priv;
  227. if (kernel_ir->cx != NULL)
  228. cx23885_input_ir_stop(kernel_ir->cx);
  229. }
  230. int cx23885_input_init(struct cx23885_dev *dev)
  231. {
  232. struct cx23885_kernel_ir *kernel_ir;
  233. struct rc_dev *rc;
  234. char *rc_map;
  235. enum rc_driver_type driver_type;
  236. unsigned long allowed_protos;
  237. int ret;
  238. /*
  239. * If the IR device (hardware registers, chip, GPIO lines, etc.) isn't
  240. * encapsulated in a v4l2_subdev, then I'm not going to deal with it.
  241. */
  242. if (dev->sd_ir == NULL)
  243. return -ENODEV;
  244. switch (dev->board) {
  245. case CX23885_BOARD_HAUPPAUGE_HVR1270:
  246. case CX23885_BOARD_HAUPPAUGE_HVR1850:
  247. case CX23885_BOARD_HAUPPAUGE_HVR1290:
  248. case CX23885_BOARD_HAUPPAUGE_HVR1250:
  249. /* Integrated CX2388[58] IR controller */
  250. driver_type = RC_DRIVER_IR_RAW;
  251. allowed_protos = RC_BIT_ALL;
  252. /* The grey Hauppauge RC-5 remote */
  253. rc_map = RC_MAP_HAUPPAUGE;
  254. break;
  255. case CX23885_BOARD_TERRATEC_CINERGY_T_PCIE_DUAL:
  256. /* Integrated CX23885 IR controller */
  257. driver_type = RC_DRIVER_IR_RAW;
  258. allowed_protos = RC_BIT_NEC;
  259. /* The grey Terratec remote with orange buttons */
  260. rc_map = RC_MAP_NEC_TERRATEC_CINERGY_XS;
  261. break;
  262. case CX23885_BOARD_TEVII_S470:
  263. /* Integrated CX23885 IR controller */
  264. driver_type = RC_DRIVER_IR_RAW;
  265. allowed_protos = RC_BIT_ALL;
  266. /* A guess at the remote */
  267. rc_map = RC_MAP_TEVII_NEC;
  268. break;
  269. case CX23885_BOARD_MYGICA_X8507:
  270. /* Integrated CX23885 IR controller */
  271. driver_type = RC_DRIVER_IR_RAW;
  272. allowed_protos = RC_BIT_ALL;
  273. /* A guess at the remote */
  274. rc_map = RC_MAP_TOTAL_MEDIA_IN_HAND_02;
  275. break;
  276. case CX23885_BOARD_TBS_6980:
  277. case CX23885_BOARD_TBS_6981:
  278. /* Integrated CX23885 IR controller */
  279. driver_type = RC_DRIVER_IR_RAW;
  280. allowed_protos = RC_BIT_ALL;
  281. /* A guess at the remote */
  282. rc_map = RC_MAP_TBS_NEC;
  283. break;
  284. case CX23885_BOARD_DVBSKY_T9580:
  285. case CX23885_BOARD_DVBSKY_T980C:
  286. case CX23885_BOARD_DVBSKY_S950C:
  287. case CX23885_BOARD_DVBSKY_S950:
  288. case CX23885_BOARD_DVBSKY_S952:
  289. case CX23885_BOARD_DVBSKY_T982:
  290. /* Integrated CX23885 IR controller */
  291. driver_type = RC_DRIVER_IR_RAW;
  292. allowed_protos = RC_BIT_ALL;
  293. rc_map = RC_MAP_DVBSKY;
  294. break;
  295. case CX23885_BOARD_TT_CT2_4500_CI:
  296. /* Integrated CX23885 IR controller */
  297. driver_type = RC_DRIVER_IR_RAW;
  298. allowed_protos = RC_BIT_ALL;
  299. rc_map = RC_MAP_TT_1500;
  300. break;
  301. default:
  302. return -ENODEV;
  303. }
  304. /* cx23885 board instance kernel IR state */
  305. kernel_ir = kzalloc(sizeof(struct cx23885_kernel_ir), GFP_KERNEL);
  306. if (kernel_ir == NULL)
  307. return -ENOMEM;
  308. kernel_ir->cx = dev;
  309. kernel_ir->name = kasprintf(GFP_KERNEL, "cx23885 IR (%s)",
  310. cx23885_boards[dev->board].name);
  311. kernel_ir->phys = kasprintf(GFP_KERNEL, "pci-%s/ir0",
  312. pci_name(dev->pci));
  313. /* input device */
  314. rc = rc_allocate_device();
  315. if (!rc) {
  316. ret = -ENOMEM;
  317. goto err_out_free;
  318. }
  319. kernel_ir->rc = rc;
  320. rc->input_name = kernel_ir->name;
  321. rc->input_phys = kernel_ir->phys;
  322. rc->input_id.bustype = BUS_PCI;
  323. rc->input_id.version = 1;
  324. if (dev->pci->subsystem_vendor) {
  325. rc->input_id.vendor = dev->pci->subsystem_vendor;
  326. rc->input_id.product = dev->pci->subsystem_device;
  327. } else {
  328. rc->input_id.vendor = dev->pci->vendor;
  329. rc->input_id.product = dev->pci->device;
  330. }
  331. rc->dev.parent = &dev->pci->dev;
  332. rc->driver_type = driver_type;
  333. rc->allowed_protocols = allowed_protos;
  334. rc->priv = kernel_ir;
  335. rc->open = cx23885_input_ir_open;
  336. rc->close = cx23885_input_ir_close;
  337. rc->map_name = rc_map;
  338. rc->driver_name = MODULE_NAME;
  339. /* Go */
  340. dev->kernel_ir = kernel_ir;
  341. ret = rc_register_device(rc);
  342. if (ret)
  343. goto err_out_stop;
  344. return 0;
  345. err_out_stop:
  346. cx23885_input_ir_stop(dev);
  347. dev->kernel_ir = NULL;
  348. rc_free_device(rc);
  349. err_out_free:
  350. kfree(kernel_ir->phys);
  351. kfree(kernel_ir->name);
  352. kfree(kernel_ir);
  353. return ret;
  354. }
  355. void cx23885_input_fini(struct cx23885_dev *dev)
  356. {
  357. /* Always stop the IR hardware from generating interrupts */
  358. cx23885_input_ir_stop(dev);
  359. if (dev->kernel_ir == NULL)
  360. return;
  361. rc_unregister_device(dev->kernel_ir->rc);
  362. kfree(dev->kernel_ir->phys);
  363. kfree(dev->kernel_ir->name);
  364. kfree(dev->kernel_ir);
  365. dev->kernel_ir = NULL;
  366. }