lirc_dev.c 11 KB

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