ir-lirc-codec.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. /* ir-lirc-codec.c - rc-core to classic lirc interface bridge
  2. *
  3. * Copyright (C) 2010 by Jarod Wilson <jarod@redhat.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation version 2 of the License.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <linux/poll.h>
  15. #include <linux/sched.h>
  16. #include <linux/wait.h>
  17. #include <media/lirc.h>
  18. #include <media/rc-core.h>
  19. #include "rc-core-priv.h"
  20. #define LIRCBUF_SIZE 256
  21. /**
  22. * ir_lirc_raw_event() - Send raw IR data to lirc to be relayed to userspace
  23. *
  24. * @dev: the struct rc_dev descriptor of the device
  25. * @ev: the struct ir_raw_event descriptor of the pulse/space
  26. */
  27. void ir_lirc_raw_event(struct rc_dev *dev, struct ir_raw_event ev)
  28. {
  29. int sample;
  30. /* Packet start */
  31. if (ev.reset) {
  32. /* Userspace expects a long space event before the start of
  33. * the signal to use as a sync. This may be done with repeat
  34. * packets and normal samples. But if a reset has been sent
  35. * then we assume that a long time has passed, so we send a
  36. * space with the maximum time value. */
  37. sample = LIRC_SPACE(LIRC_VALUE_MASK);
  38. IR_dprintk(2, "delivering reset sync space to lirc_dev\n");
  39. /* Carrier reports */
  40. } else if (ev.carrier_report) {
  41. sample = LIRC_FREQUENCY(ev.carrier);
  42. IR_dprintk(2, "carrier report (freq: %d)\n", sample);
  43. /* Packet end */
  44. } else if (ev.timeout) {
  45. if (dev->gap)
  46. return;
  47. dev->gap_start = ktime_get();
  48. dev->gap = true;
  49. dev->gap_duration = ev.duration;
  50. if (!dev->send_timeout_reports)
  51. return;
  52. sample = LIRC_TIMEOUT(ev.duration / 1000);
  53. IR_dprintk(2, "timeout report (duration: %d)\n", sample);
  54. /* Normal sample */
  55. } else {
  56. if (dev->gap) {
  57. dev->gap_duration += ktime_to_ns(ktime_sub(ktime_get(),
  58. dev->gap_start));
  59. /* Convert to ms and cap by LIRC_VALUE_MASK */
  60. do_div(dev->gap_duration, 1000);
  61. dev->gap_duration = min_t(u64, dev->gap_duration,
  62. LIRC_VALUE_MASK);
  63. kfifo_put(&dev->rawir, LIRC_SPACE(dev->gap_duration));
  64. dev->gap = false;
  65. }
  66. sample = ev.pulse ? LIRC_PULSE(ev.duration / 1000) :
  67. LIRC_SPACE(ev.duration / 1000);
  68. IR_dprintk(2, "delivering %uus %s to lirc_dev\n",
  69. TO_US(ev.duration), TO_STR(ev.pulse));
  70. }
  71. kfifo_put(&dev->rawir, sample);
  72. wake_up_poll(&dev->wait_poll, POLLIN | POLLRDNORM);
  73. }
  74. static int ir_lirc_open(struct inode *inode, struct file *file)
  75. {
  76. struct rc_dev *dev = container_of(inode->i_cdev, struct rc_dev,
  77. lirc_cdev);
  78. int retval;
  79. retval = rc_open(dev);
  80. if (retval)
  81. return retval;
  82. retval = mutex_lock_interruptible(&dev->lock);
  83. if (retval)
  84. goto out_rc;
  85. if (!dev->registered) {
  86. retval = -ENODEV;
  87. goto out_unlock;
  88. }
  89. if (dev->lirc_open) {
  90. retval = -EBUSY;
  91. goto out_unlock;
  92. }
  93. if (dev->driver_type == RC_DRIVER_IR_RAW)
  94. kfifo_reset_out(&dev->rawir);
  95. dev->lirc_open++;
  96. file->private_data = dev;
  97. nonseekable_open(inode, file);
  98. mutex_unlock(&dev->lock);
  99. return 0;
  100. out_unlock:
  101. mutex_unlock(&dev->lock);
  102. out_rc:
  103. rc_close(dev);
  104. return retval;
  105. }
  106. static int ir_lirc_close(struct inode *inode, struct file *file)
  107. {
  108. struct rc_dev *dev = file->private_data;
  109. mutex_lock(&dev->lock);
  110. dev->lirc_open--;
  111. mutex_unlock(&dev->lock);
  112. rc_close(dev);
  113. return 0;
  114. }
  115. static ssize_t ir_lirc_transmit_ir(struct file *file, const char __user *buf,
  116. size_t n, loff_t *ppos)
  117. {
  118. struct rc_dev *dev = file->private_data;
  119. unsigned int *txbuf = NULL;
  120. struct ir_raw_event *raw = NULL;
  121. ssize_t ret = -EINVAL;
  122. size_t count;
  123. ktime_t start;
  124. s64 towait;
  125. unsigned int duration = 0; /* signal duration in us */
  126. int i;
  127. if (!dev->registered)
  128. return -ENODEV;
  129. start = ktime_get();
  130. if (!dev->tx_ir) {
  131. ret = -EINVAL;
  132. goto out;
  133. }
  134. if (dev->send_mode == LIRC_MODE_SCANCODE) {
  135. struct lirc_scancode scan;
  136. if (n != sizeof(scan))
  137. return -EINVAL;
  138. if (copy_from_user(&scan, buf, sizeof(scan)))
  139. return -EFAULT;
  140. if (scan.flags || scan.keycode || scan.timestamp)
  141. return -EINVAL;
  142. /*
  143. * The scancode field in lirc_scancode is 64-bit simply
  144. * to future-proof it, since there are IR protocols encode
  145. * use more than 32 bits. For now only 32-bit protocols
  146. * are supported.
  147. */
  148. if (scan.scancode > U32_MAX ||
  149. !rc_validate_scancode(scan.rc_proto, scan.scancode))
  150. return -EINVAL;
  151. raw = kmalloc_array(LIRCBUF_SIZE, sizeof(*raw), GFP_KERNEL);
  152. if (!raw)
  153. return -ENOMEM;
  154. ret = ir_raw_encode_scancode(scan.rc_proto, scan.scancode,
  155. raw, LIRCBUF_SIZE);
  156. if (ret < 0)
  157. goto out;
  158. count = ret;
  159. txbuf = kmalloc_array(count, sizeof(unsigned int), GFP_KERNEL);
  160. if (!txbuf) {
  161. ret = -ENOMEM;
  162. goto out;
  163. }
  164. for (i = 0; i < count; i++)
  165. /* Convert from NS to US */
  166. txbuf[i] = DIV_ROUND_UP(raw[i].duration, 1000);
  167. if (dev->s_tx_carrier) {
  168. int carrier = ir_raw_encode_carrier(scan.rc_proto);
  169. if (carrier > 0)
  170. dev->s_tx_carrier(dev, carrier);
  171. }
  172. } else {
  173. if (n < sizeof(unsigned int) || n % sizeof(unsigned int))
  174. return -EINVAL;
  175. count = n / sizeof(unsigned int);
  176. if (count > LIRCBUF_SIZE || count % 2 == 0)
  177. return -EINVAL;
  178. txbuf = memdup_user(buf, n);
  179. if (IS_ERR(txbuf))
  180. return PTR_ERR(txbuf);
  181. }
  182. for (i = 0; i < count; i++) {
  183. if (txbuf[i] > IR_MAX_DURATION / 1000 - duration || !txbuf[i]) {
  184. ret = -EINVAL;
  185. goto out;
  186. }
  187. duration += txbuf[i];
  188. }
  189. ret = dev->tx_ir(dev, txbuf, count);
  190. if (ret < 0)
  191. goto out;
  192. if (dev->send_mode == LIRC_MODE_SCANCODE) {
  193. ret = n;
  194. } else {
  195. for (duration = i = 0; i < ret; i++)
  196. duration += txbuf[i];
  197. ret *= sizeof(unsigned int);
  198. /*
  199. * The lircd gap calculation expects the write function to
  200. * wait for the actual IR signal to be transmitted before
  201. * returning.
  202. */
  203. towait = ktime_us_delta(ktime_add_us(start, duration),
  204. ktime_get());
  205. if (towait > 0) {
  206. set_current_state(TASK_INTERRUPTIBLE);
  207. schedule_timeout(usecs_to_jiffies(towait));
  208. }
  209. }
  210. out:
  211. kfree(txbuf);
  212. kfree(raw);
  213. return ret;
  214. }
  215. static long ir_lirc_ioctl(struct file *filep, unsigned int cmd,
  216. unsigned long arg)
  217. {
  218. struct rc_dev *dev = filep->private_data;
  219. u32 __user *argp = (u32 __user *)(arg);
  220. int ret = 0;
  221. __u32 val = 0, tmp;
  222. if (_IOC_DIR(cmd) & _IOC_WRITE) {
  223. ret = get_user(val, argp);
  224. if (ret)
  225. return ret;
  226. }
  227. if (!dev->registered)
  228. return -ENODEV;
  229. switch (cmd) {
  230. case LIRC_GET_FEATURES:
  231. if (dev->driver_type == RC_DRIVER_IR_RAW) {
  232. val |= LIRC_CAN_REC_MODE2;
  233. if (dev->rx_resolution)
  234. val |= LIRC_CAN_GET_REC_RESOLUTION;
  235. }
  236. if (dev->tx_ir) {
  237. val |= LIRC_CAN_SEND_PULSE | LIRC_CAN_SEND_SCANCODE;
  238. if (dev->s_tx_mask)
  239. val |= LIRC_CAN_SET_TRANSMITTER_MASK;
  240. if (dev->s_tx_carrier)
  241. val |= LIRC_CAN_SET_SEND_CARRIER;
  242. if (dev->s_tx_duty_cycle)
  243. val |= LIRC_CAN_SET_SEND_DUTY_CYCLE;
  244. }
  245. if (dev->s_rx_carrier_range)
  246. val |= LIRC_CAN_SET_REC_CARRIER |
  247. LIRC_CAN_SET_REC_CARRIER_RANGE;
  248. if (dev->s_learning_mode)
  249. val |= LIRC_CAN_USE_WIDEBAND_RECEIVER;
  250. if (dev->s_carrier_report)
  251. val |= LIRC_CAN_MEASURE_CARRIER;
  252. if (dev->max_timeout)
  253. val |= LIRC_CAN_SET_REC_TIMEOUT;
  254. break;
  255. /* mode support */
  256. case LIRC_GET_REC_MODE:
  257. if (dev->driver_type == RC_DRIVER_IR_RAW_TX)
  258. return -ENOTTY;
  259. val = LIRC_MODE_MODE2;
  260. break;
  261. case LIRC_SET_REC_MODE:
  262. if (dev->driver_type == RC_DRIVER_IR_RAW_TX)
  263. return -ENOTTY;
  264. if (val != LIRC_MODE_MODE2)
  265. return -EINVAL;
  266. return 0;
  267. case LIRC_GET_SEND_MODE:
  268. if (!dev->tx_ir)
  269. return -ENOTTY;
  270. val = dev->send_mode;
  271. break;
  272. case LIRC_SET_SEND_MODE:
  273. if (!dev->tx_ir)
  274. return -ENOTTY;
  275. if (!(val == LIRC_MODE_PULSE || val == LIRC_MODE_SCANCODE))
  276. return -EINVAL;
  277. dev->send_mode = val;
  278. return 0;
  279. /* TX settings */
  280. case LIRC_SET_TRANSMITTER_MASK:
  281. if (!dev->s_tx_mask)
  282. return -ENOTTY;
  283. return dev->s_tx_mask(dev, val);
  284. case LIRC_SET_SEND_CARRIER:
  285. if (!dev->s_tx_carrier)
  286. return -ENOTTY;
  287. return dev->s_tx_carrier(dev, val);
  288. case LIRC_SET_SEND_DUTY_CYCLE:
  289. if (!dev->s_tx_duty_cycle)
  290. return -ENOTTY;
  291. if (val <= 0 || val >= 100)
  292. return -EINVAL;
  293. return dev->s_tx_duty_cycle(dev, val);
  294. /* RX settings */
  295. case LIRC_SET_REC_CARRIER:
  296. if (!dev->s_rx_carrier_range)
  297. return -ENOTTY;
  298. if (val <= 0)
  299. return -EINVAL;
  300. return dev->s_rx_carrier_range(dev,
  301. dev->carrier_low,
  302. val);
  303. case LIRC_SET_REC_CARRIER_RANGE:
  304. if (!dev->s_rx_carrier_range)
  305. return -ENOTTY;
  306. if (val <= 0)
  307. return -EINVAL;
  308. dev->carrier_low = val;
  309. return 0;
  310. case LIRC_GET_REC_RESOLUTION:
  311. if (!dev->rx_resolution)
  312. return -ENOTTY;
  313. val = dev->rx_resolution / 1000;
  314. break;
  315. case LIRC_SET_WIDEBAND_RECEIVER:
  316. if (!dev->s_learning_mode)
  317. return -ENOTTY;
  318. return dev->s_learning_mode(dev, !!val);
  319. case LIRC_SET_MEASURE_CARRIER_MODE:
  320. if (!dev->s_carrier_report)
  321. return -ENOTTY;
  322. return dev->s_carrier_report(dev, !!val);
  323. /* Generic timeout support */
  324. case LIRC_GET_MIN_TIMEOUT:
  325. if (!dev->max_timeout)
  326. return -ENOTTY;
  327. val = DIV_ROUND_UP(dev->min_timeout, 1000);
  328. break;
  329. case LIRC_GET_MAX_TIMEOUT:
  330. if (!dev->max_timeout)
  331. return -ENOTTY;
  332. val = dev->max_timeout / 1000;
  333. break;
  334. case LIRC_SET_REC_TIMEOUT:
  335. if (!dev->max_timeout)
  336. return -ENOTTY;
  337. /* Check for multiply overflow */
  338. if (val > U32_MAX / 1000)
  339. return -EINVAL;
  340. tmp = val * 1000;
  341. if (tmp < dev->min_timeout || tmp > dev->max_timeout)
  342. return -EINVAL;
  343. if (dev->s_timeout)
  344. ret = dev->s_timeout(dev, tmp);
  345. if (!ret)
  346. dev->timeout = tmp;
  347. break;
  348. case LIRC_SET_REC_TIMEOUT_REPORTS:
  349. if (!dev->timeout)
  350. return -ENOTTY;
  351. dev->send_timeout_reports = !!val;
  352. break;
  353. default:
  354. return -ENOTTY;
  355. }
  356. if (_IOC_DIR(cmd) & _IOC_READ)
  357. ret = put_user(val, argp);
  358. return ret;
  359. }
  360. static unsigned int ir_lirc_poll(struct file *file,
  361. struct poll_table_struct *wait)
  362. {
  363. struct rc_dev *rcdev = file->private_data;
  364. unsigned int events = 0;
  365. poll_wait(file, &rcdev->wait_poll, wait);
  366. if (!rcdev->registered)
  367. events = POLLHUP | POLLERR;
  368. else if (rcdev->driver_type == RC_DRIVER_IR_RAW &&
  369. !kfifo_is_empty(&rcdev->rawir))
  370. events = POLLIN | POLLRDNORM;
  371. return events;
  372. }
  373. static ssize_t ir_lirc_read(struct file *file, char __user *buffer,
  374. size_t length, loff_t *ppos)
  375. {
  376. struct rc_dev *rcdev = file->private_data;
  377. unsigned int copied;
  378. int ret;
  379. if (rcdev->driver_type == RC_DRIVER_IR_RAW_TX)
  380. return -EINVAL;
  381. if (length < sizeof(unsigned int) || length % sizeof(unsigned int))
  382. return -EINVAL;
  383. if (!rcdev->registered)
  384. return -ENODEV;
  385. do {
  386. if (kfifo_is_empty(&rcdev->rawir)) {
  387. if (file->f_flags & O_NONBLOCK)
  388. return -EAGAIN;
  389. ret = wait_event_interruptible(rcdev->wait_poll,
  390. !kfifo_is_empty(&rcdev->rawir) ||
  391. !rcdev->registered);
  392. if (ret)
  393. return ret;
  394. }
  395. if (!rcdev->registered)
  396. return -ENODEV;
  397. ret = mutex_lock_interruptible(&rcdev->lock);
  398. if (ret)
  399. return ret;
  400. ret = kfifo_to_user(&rcdev->rawir, buffer, length, &copied);
  401. mutex_unlock(&rcdev->lock);
  402. if (ret)
  403. return ret;
  404. } while (copied == 0);
  405. return copied;
  406. }
  407. const struct file_operations lirc_fops = {
  408. .owner = THIS_MODULE,
  409. .write = ir_lirc_transmit_ir,
  410. .unlocked_ioctl = ir_lirc_ioctl,
  411. #ifdef CONFIG_COMPAT
  412. .compat_ioctl = ir_lirc_ioctl,
  413. #endif
  414. .read = ir_lirc_read,
  415. .poll = ir_lirc_poll,
  416. .open = ir_lirc_open,
  417. .release = ir_lirc_close,
  418. .llseek = no_llseek,
  419. };