lirc_dev.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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/sched/signal.h>
  20. #include <linux/ioctl.h>
  21. #include <linux/poll.h>
  22. #include <linux/mutex.h>
  23. #include <linux/device.h>
  24. #include <linux/cdev.h>
  25. #include <linux/idr.h>
  26. #include "rc-core-priv.h"
  27. #include <media/lirc.h>
  28. #include <media/lirc_dev.h>
  29. #define LOGHEAD "lirc_dev (%s[%d]): "
  30. static dev_t lirc_base_dev;
  31. /* Used to keep track of allocated lirc devices */
  32. #define LIRC_MAX_DEVICES 256
  33. static DEFINE_IDA(lirc_ida);
  34. /* Only used for sysfs but defined to void otherwise */
  35. static struct class *lirc_class;
  36. static void lirc_release_device(struct device *ld)
  37. {
  38. struct lirc_dev *d = container_of(ld, struct lirc_dev, dev);
  39. put_device(d->dev.parent);
  40. if (d->buf_internal) {
  41. lirc_buffer_free(d->buf);
  42. kfree(d->buf);
  43. d->buf = NULL;
  44. }
  45. kfree(d);
  46. module_put(THIS_MODULE);
  47. }
  48. static int lirc_allocate_buffer(struct lirc_dev *d)
  49. {
  50. int err;
  51. if (d->buf) {
  52. d->buf_internal = false;
  53. return 0;
  54. }
  55. d->buf = kmalloc(sizeof(*d->buf), GFP_KERNEL);
  56. if (!d->buf)
  57. return -ENOMEM;
  58. err = lirc_buffer_init(d->buf, d->chunk_size, d->buffer_size);
  59. if (err) {
  60. kfree(d->buf);
  61. d->buf = NULL;
  62. return err;
  63. }
  64. d->buf_internal = true;
  65. return 0;
  66. }
  67. struct lirc_dev *
  68. lirc_allocate_device(void)
  69. {
  70. struct lirc_dev *d;
  71. d = kzalloc(sizeof(*d), GFP_KERNEL);
  72. if (d) {
  73. mutex_init(&d->mutex);
  74. device_initialize(&d->dev);
  75. d->dev.class = lirc_class;
  76. d->dev.release = lirc_release_device;
  77. __module_get(THIS_MODULE);
  78. }
  79. return d;
  80. }
  81. EXPORT_SYMBOL(lirc_allocate_device);
  82. void lirc_free_device(struct lirc_dev *d)
  83. {
  84. if (!d)
  85. return;
  86. put_device(&d->dev);
  87. }
  88. EXPORT_SYMBOL(lirc_free_device);
  89. int lirc_register_device(struct lirc_dev *d)
  90. {
  91. int minor;
  92. int err;
  93. if (!d) {
  94. pr_err("driver pointer must be not NULL!\n");
  95. return -EBADRQC;
  96. }
  97. if (!d->dev.parent) {
  98. pr_err("dev parent pointer not filled in!\n");
  99. return -EINVAL;
  100. }
  101. if (!d->fops) {
  102. pr_err("fops pointer not filled in!\n");
  103. return -EINVAL;
  104. }
  105. if (!d->buf && d->chunk_size < 1) {
  106. pr_err("chunk_size must be set!\n");
  107. return -EINVAL;
  108. }
  109. if (!d->buf && d->buffer_size < 1) {
  110. pr_err("buffer_size must be set!\n");
  111. return -EINVAL;
  112. }
  113. if (!d->buf && !(d->fops && d->fops->read &&
  114. d->fops->poll && d->fops->unlocked_ioctl)) {
  115. dev_err(&d->dev, "undefined read, poll, ioctl\n");
  116. return -EBADRQC;
  117. }
  118. /* some safety check 8-) */
  119. d->name[sizeof(d->name) - 1] = '\0';
  120. if (LIRC_CAN_REC(d->features)) {
  121. err = lirc_allocate_buffer(d);
  122. if (err)
  123. return err;
  124. }
  125. minor = ida_simple_get(&lirc_ida, 0, LIRC_MAX_DEVICES, GFP_KERNEL);
  126. if (minor < 0)
  127. return minor;
  128. d->minor = minor;
  129. d->dev.devt = MKDEV(MAJOR(lirc_base_dev), d->minor);
  130. dev_set_name(&d->dev, "lirc%d", d->minor);
  131. cdev_init(&d->cdev, d->fops);
  132. d->cdev.owner = d->owner;
  133. d->attached = true;
  134. err = cdev_device_add(&d->cdev, &d->dev);
  135. if (err) {
  136. ida_simple_remove(&lirc_ida, minor);
  137. return err;
  138. }
  139. get_device(d->dev.parent);
  140. dev_info(&d->dev, "lirc_dev: driver %s registered at minor = %d\n",
  141. d->name, d->minor);
  142. return 0;
  143. }
  144. EXPORT_SYMBOL(lirc_register_device);
  145. void lirc_unregister_device(struct lirc_dev *d)
  146. {
  147. if (!d)
  148. return;
  149. dev_dbg(&d->dev, "lirc_dev: driver %s unregistered from minor = %d\n",
  150. d->name, d->minor);
  151. mutex_lock(&d->mutex);
  152. d->attached = false;
  153. if (d->open) {
  154. dev_dbg(&d->dev, LOGHEAD "releasing opened driver\n",
  155. d->name, d->minor);
  156. wake_up_interruptible(&d->buf->wait_poll);
  157. }
  158. mutex_unlock(&d->mutex);
  159. cdev_device_del(&d->cdev, &d->dev);
  160. ida_simple_remove(&lirc_ida, d->minor);
  161. put_device(&d->dev);
  162. }
  163. EXPORT_SYMBOL(lirc_unregister_device);
  164. int lirc_dev_fop_open(struct inode *inode, struct file *file)
  165. {
  166. struct lirc_dev *d = container_of(inode->i_cdev, struct lirc_dev, cdev);
  167. int retval;
  168. dev_dbg(&d->dev, LOGHEAD "open called\n", d->name, d->minor);
  169. retval = mutex_lock_interruptible(&d->mutex);
  170. if (retval)
  171. return retval;
  172. if (!d->attached) {
  173. retval = -ENODEV;
  174. goto out;
  175. }
  176. if (d->open) {
  177. retval = -EBUSY;
  178. goto out;
  179. }
  180. if (d->rdev) {
  181. retval = rc_open(d->rdev);
  182. if (retval)
  183. goto out;
  184. }
  185. if (d->buf)
  186. lirc_buffer_clear(d->buf);
  187. d->open++;
  188. file->private_data = d->rdev;
  189. nonseekable_open(inode, file);
  190. mutex_unlock(&d->mutex);
  191. return 0;
  192. out:
  193. mutex_unlock(&d->mutex);
  194. return retval;
  195. }
  196. EXPORT_SYMBOL(lirc_dev_fop_open);
  197. int lirc_dev_fop_close(struct inode *inode, struct file *file)
  198. {
  199. struct rc_dev *rcdev = file->private_data;
  200. struct lirc_dev *d = rcdev->lirc_dev;
  201. mutex_lock(&d->mutex);
  202. rc_close(rcdev);
  203. d->open--;
  204. mutex_unlock(&d->mutex);
  205. return 0;
  206. }
  207. EXPORT_SYMBOL(lirc_dev_fop_close);
  208. unsigned int lirc_dev_fop_poll(struct file *file, poll_table *wait)
  209. {
  210. struct rc_dev *rcdev = file->private_data;
  211. struct lirc_dev *d = rcdev->lirc_dev;
  212. unsigned int ret;
  213. if (!d->attached)
  214. return POLLHUP | POLLERR;
  215. if (d->buf) {
  216. poll_wait(file, &d->buf->wait_poll, wait);
  217. if (lirc_buffer_empty(d->buf))
  218. ret = 0;
  219. else
  220. ret = POLLIN | POLLRDNORM;
  221. } else {
  222. ret = POLLERR;
  223. }
  224. dev_dbg(&d->dev, LOGHEAD "poll result = %d\n", d->name, d->minor, ret);
  225. return ret;
  226. }
  227. EXPORT_SYMBOL(lirc_dev_fop_poll);
  228. long lirc_dev_fop_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  229. {
  230. struct rc_dev *rcdev = file->private_data;
  231. struct lirc_dev *d = rcdev->lirc_dev;
  232. __u32 mode;
  233. int result;
  234. dev_dbg(&d->dev, LOGHEAD "ioctl called (0x%x)\n",
  235. d->name, d->minor, cmd);
  236. result = mutex_lock_interruptible(&d->mutex);
  237. if (result)
  238. return result;
  239. if (!d->attached) {
  240. result = -ENODEV;
  241. goto out;
  242. }
  243. switch (cmd) {
  244. case LIRC_GET_FEATURES:
  245. result = put_user(d->features, (__u32 __user *)arg);
  246. break;
  247. case LIRC_GET_REC_MODE:
  248. if (!LIRC_CAN_REC(d->features)) {
  249. result = -ENOTTY;
  250. break;
  251. }
  252. result = put_user(LIRC_REC2MODE
  253. (d->features & LIRC_CAN_REC_MASK),
  254. (__u32 __user *)arg);
  255. break;
  256. case LIRC_SET_REC_MODE:
  257. if (!LIRC_CAN_REC(d->features)) {
  258. result = -ENOTTY;
  259. break;
  260. }
  261. result = get_user(mode, (__u32 __user *)arg);
  262. if (!result && !(LIRC_MODE2REC(mode) & d->features))
  263. result = -EINVAL;
  264. /*
  265. * FIXME: We should actually set the mode somehow but
  266. * for now, lirc_serial doesn't support mode changing either
  267. */
  268. break;
  269. default:
  270. result = -ENOTTY;
  271. }
  272. out:
  273. mutex_unlock(&d->mutex);
  274. return result;
  275. }
  276. EXPORT_SYMBOL(lirc_dev_fop_ioctl);
  277. ssize_t lirc_dev_fop_read(struct file *file,
  278. char __user *buffer,
  279. size_t length,
  280. loff_t *ppos)
  281. {
  282. struct rc_dev *rcdev = file->private_data;
  283. struct lirc_dev *d = rcdev->lirc_dev;
  284. unsigned char *buf;
  285. int ret, written = 0;
  286. DECLARE_WAITQUEUE(wait, current);
  287. buf = kzalloc(d->buf->chunk_size, GFP_KERNEL);
  288. if (!buf)
  289. return -ENOMEM;
  290. dev_dbg(&d->dev, LOGHEAD "read called\n", d->name, d->minor);
  291. ret = mutex_lock_interruptible(&d->mutex);
  292. if (ret) {
  293. kfree(buf);
  294. return ret;
  295. }
  296. if (!d->attached) {
  297. ret = -ENODEV;
  298. goto out_locked;
  299. }
  300. if (!LIRC_CAN_REC(d->features)) {
  301. ret = -EINVAL;
  302. goto out_locked;
  303. }
  304. if (length % d->buf->chunk_size) {
  305. ret = -EINVAL;
  306. goto out_locked;
  307. }
  308. /*
  309. * we add ourselves to the task queue before buffer check
  310. * to avoid losing scan code (in case when queue is awaken somewhere
  311. * between while condition checking and scheduling)
  312. */
  313. add_wait_queue(&d->buf->wait_poll, &wait);
  314. /*
  315. * while we didn't provide 'length' bytes, device is opened in blocking
  316. * mode and 'copy_to_user' is happy, wait for data.
  317. */
  318. while (written < length && ret == 0) {
  319. if (lirc_buffer_empty(d->buf)) {
  320. /* According to the read(2) man page, 'written' can be
  321. * returned as less than 'length', instead of blocking
  322. * again, returning -EWOULDBLOCK, or returning
  323. * -ERESTARTSYS
  324. */
  325. if (written)
  326. break;
  327. if (file->f_flags & O_NONBLOCK) {
  328. ret = -EWOULDBLOCK;
  329. break;
  330. }
  331. if (signal_pending(current)) {
  332. ret = -ERESTARTSYS;
  333. break;
  334. }
  335. mutex_unlock(&d->mutex);
  336. set_current_state(TASK_INTERRUPTIBLE);
  337. schedule();
  338. set_current_state(TASK_RUNNING);
  339. ret = mutex_lock_interruptible(&d->mutex);
  340. if (ret) {
  341. remove_wait_queue(&d->buf->wait_poll, &wait);
  342. goto out_unlocked;
  343. }
  344. if (!d->attached) {
  345. ret = -ENODEV;
  346. goto out_locked;
  347. }
  348. } else {
  349. lirc_buffer_read(d->buf, buf);
  350. ret = copy_to_user((void __user *)buffer+written, buf,
  351. d->buf->chunk_size);
  352. if (!ret)
  353. written += d->buf->chunk_size;
  354. else
  355. ret = -EFAULT;
  356. }
  357. }
  358. remove_wait_queue(&d->buf->wait_poll, &wait);
  359. out_locked:
  360. mutex_unlock(&d->mutex);
  361. out_unlocked:
  362. kfree(buf);
  363. return ret ? ret : written;
  364. }
  365. EXPORT_SYMBOL(lirc_dev_fop_read);
  366. int __init lirc_dev_init(void)
  367. {
  368. int retval;
  369. lirc_class = class_create(THIS_MODULE, "lirc");
  370. if (IS_ERR(lirc_class)) {
  371. pr_err("class_create failed\n");
  372. return PTR_ERR(lirc_class);
  373. }
  374. retval = alloc_chrdev_region(&lirc_base_dev, 0, LIRC_MAX_DEVICES,
  375. "BaseRemoteCtl");
  376. if (retval) {
  377. class_destroy(lirc_class);
  378. pr_err("alloc_chrdev_region failed\n");
  379. return retval;
  380. }
  381. pr_info("IR Remote Control driver registered, major %d\n",
  382. MAJOR(lirc_base_dev));
  383. return 0;
  384. }
  385. void __exit lirc_dev_exit(void)
  386. {
  387. class_destroy(lirc_class);
  388. unregister_chrdev_region(lirc_base_dev, LIRC_MAX_DEVICES);
  389. }