lirc_dev.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  1. /*
  2. * LIRC base driver
  3. *
  4. * by Artur Lipowski <alipowski@interia.pl>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. */
  17. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  18. #include <linux/module.h>
  19. #include <linux/mutex.h>
  20. #include <linux/device.h>
  21. #include <linux/idr.h>
  22. #include <linux/poll.h>
  23. #include <linux/sched.h>
  24. #include <linux/wait.h>
  25. #include "rc-core-priv.h"
  26. #include <uapi/linux/lirc.h>
  27. #define LIRCBUF_SIZE 256
  28. static dev_t lirc_base_dev;
  29. /* Used to keep track of allocated lirc devices */
  30. static DEFINE_IDA(lirc_ida);
  31. /* Only used for sysfs but defined to void otherwise */
  32. static struct class *lirc_class;
  33. /**
  34. * ir_lirc_raw_event() - Send raw IR data to lirc to be relayed to userspace
  35. *
  36. * @dev: the struct rc_dev descriptor of the device
  37. * @ev: the struct ir_raw_event descriptor of the pulse/space
  38. */
  39. void ir_lirc_raw_event(struct rc_dev *dev, struct ir_raw_event ev)
  40. {
  41. unsigned long flags;
  42. struct lirc_fh *fh;
  43. int sample;
  44. /* Packet start */
  45. if (ev.reset) {
  46. /*
  47. * Userspace expects a long space event before the start of
  48. * the signal to use as a sync. This may be done with repeat
  49. * packets and normal samples. But if a reset has been sent
  50. * then we assume that a long time has passed, so we send a
  51. * space with the maximum time value.
  52. */
  53. sample = LIRC_SPACE(LIRC_VALUE_MASK);
  54. IR_dprintk(2, "delivering reset sync space to lirc_dev\n");
  55. /* Carrier reports */
  56. } else if (ev.carrier_report) {
  57. sample = LIRC_FREQUENCY(ev.carrier);
  58. IR_dprintk(2, "carrier report (freq: %d)\n", sample);
  59. /* Packet end */
  60. } else if (ev.timeout) {
  61. if (dev->gap)
  62. return;
  63. dev->gap_start = ktime_get();
  64. dev->gap = true;
  65. dev->gap_duration = ev.duration;
  66. sample = LIRC_TIMEOUT(ev.duration / 1000);
  67. IR_dprintk(2, "timeout report (duration: %d)\n", sample);
  68. /* Normal sample */
  69. } else {
  70. if (dev->gap) {
  71. dev->gap_duration += ktime_to_ns(ktime_sub(ktime_get(),
  72. dev->gap_start));
  73. /* Convert to ms and cap by LIRC_VALUE_MASK */
  74. do_div(dev->gap_duration, 1000);
  75. dev->gap_duration = min_t(u64, dev->gap_duration,
  76. LIRC_VALUE_MASK);
  77. spin_lock_irqsave(&dev->lirc_fh_lock, flags);
  78. list_for_each_entry(fh, &dev->lirc_fh, list)
  79. kfifo_put(&fh->rawir,
  80. LIRC_SPACE(dev->gap_duration));
  81. spin_unlock_irqrestore(&dev->lirc_fh_lock, flags);
  82. dev->gap = false;
  83. }
  84. sample = ev.pulse ? LIRC_PULSE(ev.duration / 1000) :
  85. LIRC_SPACE(ev.duration / 1000);
  86. IR_dprintk(2, "delivering %uus %s to lirc_dev\n",
  87. TO_US(ev.duration), TO_STR(ev.pulse));
  88. }
  89. spin_lock_irqsave(&dev->lirc_fh_lock, flags);
  90. list_for_each_entry(fh, &dev->lirc_fh, list) {
  91. if (LIRC_IS_TIMEOUT(sample) && !fh->send_timeout_reports)
  92. continue;
  93. if (kfifo_put(&fh->rawir, sample))
  94. wake_up_poll(&fh->wait_poll, POLLIN | POLLRDNORM);
  95. }
  96. spin_unlock_irqrestore(&dev->lirc_fh_lock, flags);
  97. }
  98. /**
  99. * ir_lirc_scancode_event() - Send scancode data to lirc to be relayed to
  100. * userspace. This can be called in atomic context.
  101. * @dev: the struct rc_dev descriptor of the device
  102. * @lsc: the struct lirc_scancode describing the decoded scancode
  103. */
  104. void ir_lirc_scancode_event(struct rc_dev *dev, struct lirc_scancode *lsc)
  105. {
  106. unsigned long flags;
  107. struct lirc_fh *fh;
  108. lsc->timestamp = ktime_get_ns();
  109. spin_lock_irqsave(&dev->lirc_fh_lock, flags);
  110. list_for_each_entry(fh, &dev->lirc_fh, list) {
  111. if (kfifo_put(&fh->scancodes, *lsc))
  112. wake_up_poll(&fh->wait_poll, POLLIN | POLLRDNORM);
  113. }
  114. spin_unlock_irqrestore(&dev->lirc_fh_lock, flags);
  115. }
  116. EXPORT_SYMBOL_GPL(ir_lirc_scancode_event);
  117. static int ir_lirc_open(struct inode *inode, struct file *file)
  118. {
  119. struct rc_dev *dev = container_of(inode->i_cdev, struct rc_dev,
  120. lirc_cdev);
  121. struct lirc_fh *fh = kzalloc(sizeof(*fh), GFP_KERNEL);
  122. unsigned long flags;
  123. int retval;
  124. if (!fh)
  125. return -ENOMEM;
  126. get_device(&dev->dev);
  127. if (!dev->registered) {
  128. retval = -ENODEV;
  129. goto out_fh;
  130. }
  131. if (dev->driver_type == RC_DRIVER_IR_RAW) {
  132. if (kfifo_alloc(&fh->rawir, MAX_IR_EVENT_SIZE, GFP_KERNEL)) {
  133. retval = -ENOMEM;
  134. goto out_fh;
  135. }
  136. }
  137. if (dev->driver_type != RC_DRIVER_IR_RAW_TX) {
  138. if (kfifo_alloc(&fh->scancodes, 32, GFP_KERNEL)) {
  139. retval = -ENOMEM;
  140. goto out_rawir;
  141. }
  142. }
  143. fh->send_mode = LIRC_MODE_PULSE;
  144. fh->rc = dev;
  145. fh->send_timeout_reports = true;
  146. if (dev->driver_type == RC_DRIVER_SCANCODE)
  147. fh->rec_mode = LIRC_MODE_SCANCODE;
  148. else
  149. fh->rec_mode = LIRC_MODE_MODE2;
  150. retval = rc_open(dev);
  151. if (retval)
  152. goto out_kfifo;
  153. init_waitqueue_head(&fh->wait_poll);
  154. file->private_data = fh;
  155. spin_lock_irqsave(&dev->lirc_fh_lock, flags);
  156. list_add(&fh->list, &dev->lirc_fh);
  157. spin_unlock_irqrestore(&dev->lirc_fh_lock, flags);
  158. nonseekable_open(inode, file);
  159. return 0;
  160. out_kfifo:
  161. if (dev->driver_type != RC_DRIVER_IR_RAW_TX)
  162. kfifo_free(&fh->scancodes);
  163. out_rawir:
  164. if (dev->driver_type == RC_DRIVER_IR_RAW)
  165. kfifo_free(&fh->rawir);
  166. out_fh:
  167. kfree(fh);
  168. put_device(&dev->dev);
  169. return retval;
  170. }
  171. static int ir_lirc_close(struct inode *inode, struct file *file)
  172. {
  173. struct lirc_fh *fh = file->private_data;
  174. struct rc_dev *dev = fh->rc;
  175. unsigned long flags;
  176. spin_lock_irqsave(&dev->lirc_fh_lock, flags);
  177. list_del(&fh->list);
  178. spin_unlock_irqrestore(&dev->lirc_fh_lock, flags);
  179. if (dev->driver_type == RC_DRIVER_IR_RAW)
  180. kfifo_free(&fh->rawir);
  181. if (dev->driver_type != RC_DRIVER_IR_RAW_TX)
  182. kfifo_free(&fh->scancodes);
  183. kfree(fh);
  184. rc_close(dev);
  185. put_device(&dev->dev);
  186. return 0;
  187. }
  188. static ssize_t ir_lirc_transmit_ir(struct file *file, const char __user *buf,
  189. size_t n, loff_t *ppos)
  190. {
  191. struct lirc_fh *fh = file->private_data;
  192. struct rc_dev *dev = fh->rc;
  193. unsigned int *txbuf;
  194. struct ir_raw_event *raw = NULL;
  195. ssize_t ret;
  196. size_t count;
  197. ktime_t start;
  198. s64 towait;
  199. unsigned int duration = 0; /* signal duration in us */
  200. int i;
  201. ret = mutex_lock_interruptible(&dev->lock);
  202. if (ret)
  203. return ret;
  204. if (!dev->registered) {
  205. ret = -ENODEV;
  206. goto out_unlock;
  207. }
  208. start = ktime_get();
  209. if (!dev->tx_ir) {
  210. ret = -EINVAL;
  211. goto out_unlock;
  212. }
  213. if (fh->send_mode == LIRC_MODE_SCANCODE) {
  214. struct lirc_scancode scan;
  215. if (n != sizeof(scan)) {
  216. ret = -EINVAL;
  217. goto out_unlock;
  218. }
  219. if (copy_from_user(&scan, buf, sizeof(scan))) {
  220. ret = -EFAULT;
  221. goto out_unlock;
  222. }
  223. if (scan.flags || scan.keycode || scan.timestamp) {
  224. ret = -EINVAL;
  225. goto out_unlock;
  226. }
  227. /*
  228. * The scancode field in lirc_scancode is 64-bit simply
  229. * to future-proof it, since there are IR protocols encode
  230. * use more than 32 bits. For now only 32-bit protocols
  231. * are supported.
  232. */
  233. if (scan.scancode > U32_MAX ||
  234. !rc_validate_scancode(scan.rc_proto, scan.scancode)) {
  235. ret = -EINVAL;
  236. goto out_unlock;
  237. }
  238. raw = kmalloc_array(LIRCBUF_SIZE, sizeof(*raw), GFP_KERNEL);
  239. if (!raw) {
  240. ret = -ENOMEM;
  241. goto out_unlock;
  242. }
  243. ret = ir_raw_encode_scancode(scan.rc_proto, scan.scancode,
  244. raw, LIRCBUF_SIZE);
  245. if (ret < 0)
  246. goto out_kfree;
  247. count = ret;
  248. txbuf = kmalloc_array(count, sizeof(unsigned int), GFP_KERNEL);
  249. if (!txbuf) {
  250. ret = -ENOMEM;
  251. goto out_kfree;
  252. }
  253. for (i = 0; i < count; i++)
  254. /* Convert from NS to US */
  255. txbuf[i] = DIV_ROUND_UP(raw[i].duration, 1000);
  256. if (dev->s_tx_carrier) {
  257. int carrier = ir_raw_encode_carrier(scan.rc_proto);
  258. if (carrier > 0)
  259. dev->s_tx_carrier(dev, carrier);
  260. }
  261. } else {
  262. if (n < sizeof(unsigned int) || n % sizeof(unsigned int)) {
  263. ret = -EINVAL;
  264. goto out_unlock;
  265. }
  266. count = n / sizeof(unsigned int);
  267. if (count > LIRCBUF_SIZE || count % 2 == 0) {
  268. ret = -EINVAL;
  269. goto out_unlock;
  270. }
  271. txbuf = memdup_user(buf, n);
  272. if (IS_ERR(txbuf)) {
  273. ret = PTR_ERR(txbuf);
  274. goto out_unlock;
  275. }
  276. }
  277. for (i = 0; i < count; i++) {
  278. if (txbuf[i] > IR_MAX_DURATION / 1000 - duration || !txbuf[i]) {
  279. ret = -EINVAL;
  280. goto out_kfree;
  281. }
  282. duration += txbuf[i];
  283. }
  284. ret = dev->tx_ir(dev, txbuf, count);
  285. if (ret < 0)
  286. goto out_kfree;
  287. if (fh->send_mode == LIRC_MODE_SCANCODE) {
  288. ret = n;
  289. } else {
  290. for (duration = i = 0; i < ret; i++)
  291. duration += txbuf[i];
  292. ret *= sizeof(unsigned int);
  293. }
  294. /*
  295. * The lircd gap calculation expects the write function to
  296. * wait for the actual IR signal to be transmitted before
  297. * returning.
  298. */
  299. towait = ktime_us_delta(ktime_add_us(start, duration),
  300. ktime_get());
  301. if (towait > 0) {
  302. set_current_state(TASK_INTERRUPTIBLE);
  303. schedule_timeout(usecs_to_jiffies(towait));
  304. }
  305. out_kfree:
  306. kfree(txbuf);
  307. kfree(raw);
  308. out_unlock:
  309. mutex_unlock(&dev->lock);
  310. return ret;
  311. }
  312. static long ir_lirc_ioctl(struct file *file, unsigned int cmd,
  313. unsigned long arg)
  314. {
  315. struct lirc_fh *fh = file->private_data;
  316. struct rc_dev *dev = fh->rc;
  317. u32 __user *argp = (u32 __user *)(arg);
  318. u32 val = 0;
  319. int ret;
  320. if (_IOC_DIR(cmd) & _IOC_WRITE) {
  321. ret = get_user(val, argp);
  322. if (ret)
  323. return ret;
  324. }
  325. ret = mutex_lock_interruptible(&dev->lock);
  326. if (ret)
  327. return ret;
  328. if (!dev->registered) {
  329. ret = -ENODEV;
  330. goto out;
  331. }
  332. switch (cmd) {
  333. case LIRC_GET_FEATURES:
  334. if (dev->driver_type == RC_DRIVER_SCANCODE)
  335. val |= LIRC_CAN_REC_SCANCODE;
  336. if (dev->driver_type == RC_DRIVER_IR_RAW) {
  337. val |= LIRC_CAN_REC_MODE2 | LIRC_CAN_REC_SCANCODE;
  338. if (dev->rx_resolution)
  339. val |= LIRC_CAN_GET_REC_RESOLUTION;
  340. }
  341. if (dev->tx_ir) {
  342. val |= LIRC_CAN_SEND_PULSE | LIRC_CAN_SEND_SCANCODE;
  343. if (dev->s_tx_mask)
  344. val |= LIRC_CAN_SET_TRANSMITTER_MASK;
  345. if (dev->s_tx_carrier)
  346. val |= LIRC_CAN_SET_SEND_CARRIER;
  347. if (dev->s_tx_duty_cycle)
  348. val |= LIRC_CAN_SET_SEND_DUTY_CYCLE;
  349. }
  350. if (dev->s_rx_carrier_range)
  351. val |= LIRC_CAN_SET_REC_CARRIER |
  352. LIRC_CAN_SET_REC_CARRIER_RANGE;
  353. if (dev->s_learning_mode)
  354. val |= LIRC_CAN_USE_WIDEBAND_RECEIVER;
  355. if (dev->s_carrier_report)
  356. val |= LIRC_CAN_MEASURE_CARRIER;
  357. if (dev->max_timeout)
  358. val |= LIRC_CAN_SET_REC_TIMEOUT;
  359. break;
  360. /* mode support */
  361. case LIRC_GET_REC_MODE:
  362. if (dev->driver_type == RC_DRIVER_IR_RAW_TX)
  363. ret = -ENOTTY;
  364. else
  365. val = fh->rec_mode;
  366. break;
  367. case LIRC_SET_REC_MODE:
  368. switch (dev->driver_type) {
  369. case RC_DRIVER_IR_RAW_TX:
  370. ret = -ENOTTY;
  371. break;
  372. case RC_DRIVER_SCANCODE:
  373. if (val != LIRC_MODE_SCANCODE)
  374. ret = -EINVAL;
  375. break;
  376. case RC_DRIVER_IR_RAW:
  377. if (!(val == LIRC_MODE_MODE2 ||
  378. val == LIRC_MODE_SCANCODE))
  379. ret = -EINVAL;
  380. break;
  381. }
  382. if (!ret)
  383. fh->rec_mode = val;
  384. break;
  385. case LIRC_GET_SEND_MODE:
  386. if (!dev->tx_ir)
  387. ret = -ENOTTY;
  388. else
  389. val = fh->send_mode;
  390. break;
  391. case LIRC_SET_SEND_MODE:
  392. if (!dev->tx_ir)
  393. ret = -ENOTTY;
  394. else if (!(val == LIRC_MODE_PULSE || val == LIRC_MODE_SCANCODE))
  395. ret = -EINVAL;
  396. else
  397. fh->send_mode = val;
  398. break;
  399. /* TX settings */
  400. case LIRC_SET_TRANSMITTER_MASK:
  401. if (!dev->s_tx_mask)
  402. ret = -ENOTTY;
  403. else
  404. ret = dev->s_tx_mask(dev, val);
  405. break;
  406. case LIRC_SET_SEND_CARRIER:
  407. if (!dev->s_tx_carrier)
  408. ret = -ENOTTY;
  409. else
  410. ret = dev->s_tx_carrier(dev, val);
  411. break;
  412. case LIRC_SET_SEND_DUTY_CYCLE:
  413. if (!dev->s_tx_duty_cycle)
  414. ret = -ENOTTY;
  415. else if (val <= 0 || val >= 100)
  416. ret = -EINVAL;
  417. else
  418. ret = dev->s_tx_duty_cycle(dev, val);
  419. break;
  420. /* RX settings */
  421. case LIRC_SET_REC_CARRIER:
  422. if (!dev->s_rx_carrier_range)
  423. ret = -ENOTTY;
  424. else if (val <= 0)
  425. ret = -EINVAL;
  426. else
  427. ret = dev->s_rx_carrier_range(dev, fh->carrier_low,
  428. val);
  429. break;
  430. case LIRC_SET_REC_CARRIER_RANGE:
  431. if (!dev->s_rx_carrier_range)
  432. ret = -ENOTTY;
  433. else if (val <= 0)
  434. ret = -EINVAL;
  435. else
  436. fh->carrier_low = val;
  437. break;
  438. case LIRC_GET_REC_RESOLUTION:
  439. if (!dev->rx_resolution)
  440. ret = -ENOTTY;
  441. else
  442. val = dev->rx_resolution / 1000;
  443. break;
  444. case LIRC_SET_WIDEBAND_RECEIVER:
  445. if (!dev->s_learning_mode)
  446. ret = -ENOTTY;
  447. else
  448. ret = dev->s_learning_mode(dev, !!val);
  449. break;
  450. case LIRC_SET_MEASURE_CARRIER_MODE:
  451. if (!dev->s_carrier_report)
  452. ret = -ENOTTY;
  453. else
  454. ret = dev->s_carrier_report(dev, !!val);
  455. break;
  456. /* Generic timeout support */
  457. case LIRC_GET_MIN_TIMEOUT:
  458. if (!dev->max_timeout)
  459. ret = -ENOTTY;
  460. else
  461. val = DIV_ROUND_UP(dev->min_timeout, 1000);
  462. break;
  463. case LIRC_GET_MAX_TIMEOUT:
  464. if (!dev->max_timeout)
  465. ret = -ENOTTY;
  466. else
  467. val = dev->max_timeout / 1000;
  468. break;
  469. case LIRC_SET_REC_TIMEOUT:
  470. if (!dev->max_timeout) {
  471. ret = -ENOTTY;
  472. } else if (val > U32_MAX / 1000) {
  473. /* Check for multiply overflow */
  474. ret = -EINVAL;
  475. } else {
  476. u32 tmp = val * 1000;
  477. if (tmp < dev->min_timeout || tmp > dev->max_timeout)
  478. ret = -EINVAL;
  479. else if (dev->s_timeout)
  480. ret = dev->s_timeout(dev, tmp);
  481. else if (!ret)
  482. dev->timeout = tmp;
  483. }
  484. break;
  485. case LIRC_SET_REC_TIMEOUT_REPORTS:
  486. if (!dev->timeout)
  487. ret = -ENOTTY;
  488. else
  489. fh->send_timeout_reports = !!val;
  490. break;
  491. default:
  492. ret = -ENOTTY;
  493. }
  494. if (!ret && _IOC_DIR(cmd) & _IOC_READ)
  495. ret = put_user(val, argp);
  496. out:
  497. mutex_unlock(&dev->lock);
  498. return ret;
  499. }
  500. static unsigned int ir_lirc_poll(struct file *file,
  501. struct poll_table_struct *wait)
  502. {
  503. struct lirc_fh *fh = file->private_data;
  504. struct rc_dev *rcdev = fh->rc;
  505. unsigned int events = 0;
  506. poll_wait(file, &fh->wait_poll, wait);
  507. if (!rcdev->registered) {
  508. events = POLLHUP | POLLERR;
  509. } else if (rcdev->driver_type != RC_DRIVER_IR_RAW_TX) {
  510. if (fh->rec_mode == LIRC_MODE_SCANCODE &&
  511. !kfifo_is_empty(&fh->scancodes))
  512. events = POLLIN | POLLRDNORM;
  513. if (fh->rec_mode == LIRC_MODE_MODE2 &&
  514. !kfifo_is_empty(&fh->rawir))
  515. events = POLLIN | POLLRDNORM;
  516. }
  517. return events;
  518. }
  519. static ssize_t ir_lirc_read_mode2(struct file *file, char __user *buffer,
  520. size_t length)
  521. {
  522. struct lirc_fh *fh = file->private_data;
  523. struct rc_dev *rcdev = fh->rc;
  524. unsigned int copied;
  525. int ret;
  526. if (length < sizeof(unsigned int) || length % sizeof(unsigned int))
  527. return -EINVAL;
  528. do {
  529. if (kfifo_is_empty(&fh->rawir)) {
  530. if (file->f_flags & O_NONBLOCK)
  531. return -EAGAIN;
  532. ret = wait_event_interruptible(fh->wait_poll,
  533. !kfifo_is_empty(&fh->rawir) ||
  534. !rcdev->registered);
  535. if (ret)
  536. return ret;
  537. }
  538. if (!rcdev->registered)
  539. return -ENODEV;
  540. ret = mutex_lock_interruptible(&rcdev->lock);
  541. if (ret)
  542. return ret;
  543. ret = kfifo_to_user(&fh->rawir, buffer, length, &copied);
  544. mutex_unlock(&rcdev->lock);
  545. if (ret)
  546. return ret;
  547. } while (copied == 0);
  548. return copied;
  549. }
  550. static ssize_t ir_lirc_read_scancode(struct file *file, char __user *buffer,
  551. size_t length)
  552. {
  553. struct lirc_fh *fh = file->private_data;
  554. struct rc_dev *rcdev = fh->rc;
  555. unsigned int copied;
  556. int ret;
  557. if (length < sizeof(struct lirc_scancode) ||
  558. length % sizeof(struct lirc_scancode))
  559. return -EINVAL;
  560. do {
  561. if (kfifo_is_empty(&fh->scancodes)) {
  562. if (file->f_flags & O_NONBLOCK)
  563. return -EAGAIN;
  564. ret = wait_event_interruptible(fh->wait_poll,
  565. !kfifo_is_empty(&fh->scancodes) ||
  566. !rcdev->registered);
  567. if (ret)
  568. return ret;
  569. }
  570. if (!rcdev->registered)
  571. return -ENODEV;
  572. ret = mutex_lock_interruptible(&rcdev->lock);
  573. if (ret)
  574. return ret;
  575. ret = kfifo_to_user(&fh->scancodes, buffer, length, &copied);
  576. mutex_unlock(&rcdev->lock);
  577. if (ret)
  578. return ret;
  579. } while (copied == 0);
  580. return copied;
  581. }
  582. static ssize_t ir_lirc_read(struct file *file, char __user *buffer,
  583. size_t length, loff_t *ppos)
  584. {
  585. struct lirc_fh *fh = file->private_data;
  586. struct rc_dev *rcdev = fh->rc;
  587. if (rcdev->driver_type == RC_DRIVER_IR_RAW_TX)
  588. return -EINVAL;
  589. if (!rcdev->registered)
  590. return -ENODEV;
  591. if (fh->rec_mode == LIRC_MODE_MODE2)
  592. return ir_lirc_read_mode2(file, buffer, length);
  593. else /* LIRC_MODE_SCANCODE */
  594. return ir_lirc_read_scancode(file, buffer, length);
  595. }
  596. static const struct file_operations lirc_fops = {
  597. .owner = THIS_MODULE,
  598. .write = ir_lirc_transmit_ir,
  599. .unlocked_ioctl = ir_lirc_ioctl,
  600. #ifdef CONFIG_COMPAT
  601. .compat_ioctl = ir_lirc_ioctl,
  602. #endif
  603. .read = ir_lirc_read,
  604. .poll = ir_lirc_poll,
  605. .open = ir_lirc_open,
  606. .release = ir_lirc_close,
  607. .llseek = no_llseek,
  608. };
  609. static void lirc_release_device(struct device *ld)
  610. {
  611. struct rc_dev *rcdev = container_of(ld, struct rc_dev, lirc_dev);
  612. put_device(&rcdev->dev);
  613. }
  614. int ir_lirc_register(struct rc_dev *dev)
  615. {
  616. int err, minor;
  617. minor = ida_simple_get(&lirc_ida, 0, RC_DEV_MAX, GFP_KERNEL);
  618. if (minor < 0)
  619. return minor;
  620. device_initialize(&dev->lirc_dev);
  621. dev->lirc_dev.class = lirc_class;
  622. dev->lirc_dev.parent = &dev->dev;
  623. dev->lirc_dev.release = lirc_release_device;
  624. dev->lirc_dev.devt = MKDEV(MAJOR(lirc_base_dev), minor);
  625. dev_set_name(&dev->lirc_dev, "lirc%d", minor);
  626. INIT_LIST_HEAD(&dev->lirc_fh);
  627. spin_lock_init(&dev->lirc_fh_lock);
  628. cdev_init(&dev->lirc_cdev, &lirc_fops);
  629. err = cdev_device_add(&dev->lirc_cdev, &dev->lirc_dev);
  630. if (err)
  631. goto out_ida;
  632. get_device(&dev->dev);
  633. dev_info(&dev->dev, "lirc_dev: driver %s registered at minor = %d",
  634. dev->driver_name, minor);
  635. return 0;
  636. out_ida:
  637. ida_simple_remove(&lirc_ida, minor);
  638. return err;
  639. }
  640. void ir_lirc_unregister(struct rc_dev *dev)
  641. {
  642. unsigned long flags;
  643. struct lirc_fh *fh;
  644. dev_dbg(&dev->dev, "lirc_dev: driver %s unregistered from minor = %d\n",
  645. dev->driver_name, MINOR(dev->lirc_dev.devt));
  646. spin_lock_irqsave(&dev->lirc_fh_lock, flags);
  647. list_for_each_entry(fh, &dev->lirc_fh, list)
  648. wake_up_poll(&fh->wait_poll, POLLHUP | POLLERR);
  649. spin_unlock_irqrestore(&dev->lirc_fh_lock, flags);
  650. cdev_device_del(&dev->lirc_cdev, &dev->lirc_dev);
  651. ida_simple_remove(&lirc_ida, MINOR(dev->lirc_dev.devt));
  652. }
  653. int __init lirc_dev_init(void)
  654. {
  655. int retval;
  656. lirc_class = class_create(THIS_MODULE, "lirc");
  657. if (IS_ERR(lirc_class)) {
  658. pr_err("class_create failed\n");
  659. return PTR_ERR(lirc_class);
  660. }
  661. retval = alloc_chrdev_region(&lirc_base_dev, 0, RC_DEV_MAX,
  662. "BaseRemoteCtl");
  663. if (retval) {
  664. class_destroy(lirc_class);
  665. pr_err("alloc_chrdev_region failed\n");
  666. return retval;
  667. }
  668. pr_info("IR Remote Control driver registered, major %d\n",
  669. MAJOR(lirc_base_dev));
  670. return 0;
  671. }
  672. void __exit lirc_dev_exit(void)
  673. {
  674. class_destroy(lirc_class);
  675. unregister_chrdev_region(lirc_base_dev, RC_DEV_MAX);
  676. }