lirc_dev.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  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. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. */
  21. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  22. #include <linux/module.h>
  23. #include <linux/kernel.h>
  24. #include <linux/sched.h>
  25. #include <linux/errno.h>
  26. #include <linux/ioctl.h>
  27. #include <linux/fs.h>
  28. #include <linux/poll.h>
  29. #include <linux/completion.h>
  30. #include <linux/mutex.h>
  31. #include <linux/wait.h>
  32. #include <linux/unistd.h>
  33. #include <linux/kthread.h>
  34. #include <linux/bitops.h>
  35. #include <linux/device.h>
  36. #include <linux/cdev.h>
  37. #include <media/rc-core.h>
  38. #include <media/lirc.h>
  39. #include <media/lirc_dev.h>
  40. static bool debug;
  41. #define IRCTL_DEV_NAME "BaseRemoteCtl"
  42. #define NOPLUG -1
  43. #define LOGHEAD "lirc_dev (%s[%d]): "
  44. static dev_t lirc_base_dev;
  45. struct irctl {
  46. struct lirc_driver d;
  47. int attached;
  48. int open;
  49. struct mutex irctl_lock;
  50. struct lirc_buffer *buf;
  51. unsigned int chunk_size;
  52. struct cdev *cdev;
  53. struct task_struct *task;
  54. long jiffies_to_wait;
  55. };
  56. static DEFINE_MUTEX(lirc_dev_lock);
  57. static struct irctl *irctls[MAX_IRCTL_DEVICES];
  58. /* Only used for sysfs but defined to void otherwise */
  59. static struct class *lirc_class;
  60. /* helper function
  61. * initializes the irctl structure
  62. */
  63. static void lirc_irctl_init(struct irctl *ir)
  64. {
  65. mutex_init(&ir->irctl_lock);
  66. ir->d.minor = NOPLUG;
  67. }
  68. static void lirc_irctl_cleanup(struct irctl *ir)
  69. {
  70. device_destroy(lirc_class, MKDEV(MAJOR(lirc_base_dev), ir->d.minor));
  71. if (ir->buf != ir->d.rbuf) {
  72. lirc_buffer_free(ir->buf);
  73. kfree(ir->buf);
  74. }
  75. ir->buf = NULL;
  76. }
  77. /* helper function
  78. * reads key codes from driver and puts them into buffer
  79. * returns 0 on success
  80. */
  81. static int lirc_add_to_buf(struct irctl *ir)
  82. {
  83. int res;
  84. int got_data = -1;
  85. if (!ir->d.add_to_buf)
  86. return 0;
  87. /*
  88. * service the device as long as it is returning
  89. * data and we have space
  90. */
  91. do {
  92. got_data++;
  93. res = ir->d.add_to_buf(ir->d.data, ir->buf);
  94. } while (!res);
  95. if (res == -ENODEV)
  96. kthread_stop(ir->task);
  97. return got_data ? 0 : res;
  98. }
  99. /* main function of the polling thread
  100. */
  101. static int lirc_thread(void *irctl)
  102. {
  103. struct irctl *ir = irctl;
  104. do {
  105. if (ir->open) {
  106. if (ir->jiffies_to_wait) {
  107. set_current_state(TASK_INTERRUPTIBLE);
  108. schedule_timeout(ir->jiffies_to_wait);
  109. }
  110. if (kthread_should_stop())
  111. break;
  112. if (!lirc_add_to_buf(ir))
  113. wake_up_interruptible(&ir->buf->wait_poll);
  114. } else {
  115. set_current_state(TASK_INTERRUPTIBLE);
  116. schedule();
  117. }
  118. } while (!kthread_should_stop());
  119. return 0;
  120. }
  121. static const struct file_operations lirc_dev_fops = {
  122. .owner = THIS_MODULE,
  123. .read = lirc_dev_fop_read,
  124. .write = lirc_dev_fop_write,
  125. .poll = lirc_dev_fop_poll,
  126. .unlocked_ioctl = lirc_dev_fop_ioctl,
  127. #ifdef CONFIG_COMPAT
  128. .compat_ioctl = lirc_dev_fop_ioctl,
  129. #endif
  130. .open = lirc_dev_fop_open,
  131. .release = lirc_dev_fop_close,
  132. .llseek = noop_llseek,
  133. };
  134. static int lirc_cdev_add(struct irctl *ir)
  135. {
  136. int retval = -ENOMEM;
  137. struct lirc_driver *d = &ir->d;
  138. struct cdev *cdev;
  139. cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
  140. if (!cdev)
  141. goto err_out;
  142. if (d->fops) {
  143. cdev_init(cdev, d->fops);
  144. cdev->owner = d->owner;
  145. } else {
  146. cdev_init(cdev, &lirc_dev_fops);
  147. cdev->owner = THIS_MODULE;
  148. }
  149. retval = kobject_set_name(&cdev->kobj, "lirc%d", d->minor);
  150. if (retval)
  151. goto err_out;
  152. retval = cdev_add(cdev, MKDEV(MAJOR(lirc_base_dev), d->minor), 1);
  153. if (retval) {
  154. kobject_put(&cdev->kobj);
  155. goto err_out;
  156. }
  157. ir->cdev = cdev;
  158. return 0;
  159. err_out:
  160. kfree(cdev);
  161. return retval;
  162. }
  163. static int lirc_allocate_buffer(struct irctl *ir)
  164. {
  165. int err = 0;
  166. int bytes_in_key;
  167. unsigned int chunk_size;
  168. unsigned int buffer_size;
  169. struct lirc_driver *d = &ir->d;
  170. mutex_lock(&lirc_dev_lock);
  171. bytes_in_key = BITS_TO_LONGS(d->code_length) +
  172. (d->code_length % 8 ? 1 : 0);
  173. buffer_size = d->buffer_size ? d->buffer_size : BUFLEN / bytes_in_key;
  174. chunk_size = d->chunk_size ? d->chunk_size : bytes_in_key;
  175. if (d->rbuf) {
  176. ir->buf = d->rbuf;
  177. } else {
  178. ir->buf = kmalloc(sizeof(struct lirc_buffer), GFP_KERNEL);
  179. if (!ir->buf) {
  180. err = -ENOMEM;
  181. goto out;
  182. }
  183. err = lirc_buffer_init(ir->buf, chunk_size, buffer_size);
  184. if (err) {
  185. kfree(ir->buf);
  186. goto out;
  187. }
  188. }
  189. ir->chunk_size = ir->buf->chunk_size;
  190. out:
  191. mutex_unlock(&lirc_dev_lock);
  192. return err;
  193. }
  194. static int lirc_allocate_driver(struct lirc_driver *d)
  195. {
  196. struct irctl *ir;
  197. int minor;
  198. int err;
  199. if (!d) {
  200. pr_err("driver pointer must be not NULL!\n");
  201. return -EBADRQC;
  202. }
  203. if (!d->dev) {
  204. pr_err("dev pointer not filled in!\n");
  205. return -EINVAL;
  206. }
  207. if (d->minor >= MAX_IRCTL_DEVICES) {
  208. dev_err(d->dev, "minor must be between 0 and %d!\n",
  209. MAX_IRCTL_DEVICES - 1);
  210. return -EBADRQC;
  211. }
  212. if (d->code_length < 1 || d->code_length > (BUFLEN * 8)) {
  213. dev_err(d->dev, "code length must be less than %d bits\n",
  214. BUFLEN * 8);
  215. return -EBADRQC;
  216. }
  217. if (d->sample_rate) {
  218. if (2 > d->sample_rate || HZ < d->sample_rate) {
  219. dev_err(d->dev, "invalid %d sample rate\n",
  220. d->sample_rate);
  221. return -EBADRQC;
  222. }
  223. if (!d->add_to_buf) {
  224. dev_err(d->dev, "add_to_buf not set\n");
  225. return -EBADRQC;
  226. }
  227. } else if (!d->rbuf && !(d->fops && d->fops->read &&
  228. d->fops->poll && d->fops->unlocked_ioctl)) {
  229. dev_err(d->dev, "undefined read, poll, ioctl\n");
  230. return -EBADRQC;
  231. }
  232. mutex_lock(&lirc_dev_lock);
  233. minor = d->minor;
  234. if (minor < 0) {
  235. /* find first free slot for driver */
  236. for (minor = 0; minor < MAX_IRCTL_DEVICES; minor++)
  237. if (!irctls[minor])
  238. break;
  239. if (minor == MAX_IRCTL_DEVICES) {
  240. dev_err(d->dev, "no free slots for drivers!\n");
  241. err = -ENOMEM;
  242. goto out_lock;
  243. }
  244. } else if (irctls[minor]) {
  245. dev_err(d->dev, "minor (%d) just registered!\n", minor);
  246. err = -EBUSY;
  247. goto out_lock;
  248. }
  249. ir = kzalloc(sizeof(struct irctl), GFP_KERNEL);
  250. if (!ir) {
  251. err = -ENOMEM;
  252. goto out_lock;
  253. }
  254. lirc_irctl_init(ir);
  255. irctls[minor] = ir;
  256. d->minor = minor;
  257. /* some safety check 8-) */
  258. d->name[sizeof(d->name)-1] = '\0';
  259. if (d->features == 0)
  260. d->features = LIRC_CAN_REC_LIRCCODE;
  261. ir->d = *d;
  262. device_create(lirc_class, ir->d.dev,
  263. MKDEV(MAJOR(lirc_base_dev), ir->d.minor), NULL,
  264. "lirc%u", ir->d.minor);
  265. if (d->sample_rate) {
  266. ir->jiffies_to_wait = HZ / d->sample_rate;
  267. /* try to fire up polling thread */
  268. ir->task = kthread_run(lirc_thread, (void *)ir, "lirc_dev");
  269. if (IS_ERR(ir->task)) {
  270. dev_err(d->dev, "cannot run thread for minor = %d\n",
  271. d->minor);
  272. err = -ECHILD;
  273. goto out_sysfs;
  274. }
  275. } else {
  276. /* it means - wait for external event in task queue */
  277. ir->jiffies_to_wait = 0;
  278. }
  279. err = lirc_cdev_add(ir);
  280. if (err)
  281. goto out_sysfs;
  282. ir->attached = 1;
  283. mutex_unlock(&lirc_dev_lock);
  284. dev_info(ir->d.dev, "lirc_dev: driver %s registered at minor = %d\n",
  285. ir->d.name, ir->d.minor);
  286. return minor;
  287. out_sysfs:
  288. device_destroy(lirc_class, MKDEV(MAJOR(lirc_base_dev), ir->d.minor));
  289. out_lock:
  290. mutex_unlock(&lirc_dev_lock);
  291. return err;
  292. }
  293. int lirc_register_driver(struct lirc_driver *d)
  294. {
  295. int minor, err = 0;
  296. minor = lirc_allocate_driver(d);
  297. if (minor < 0)
  298. return minor;
  299. if (LIRC_CAN_REC(d->features)) {
  300. err = lirc_allocate_buffer(irctls[minor]);
  301. if (err)
  302. lirc_unregister_driver(minor);
  303. }
  304. return err ? err : minor;
  305. }
  306. EXPORT_SYMBOL(lirc_register_driver);
  307. int lirc_unregister_driver(int minor)
  308. {
  309. struct irctl *ir;
  310. struct cdev *cdev;
  311. if (minor < 0 || minor >= MAX_IRCTL_DEVICES) {
  312. pr_err("minor (%d) must be between 0 and %d!\n",
  313. minor, MAX_IRCTL_DEVICES - 1);
  314. return -EBADRQC;
  315. }
  316. ir = irctls[minor];
  317. if (!ir) {
  318. pr_err("failed to get irctl\n");
  319. return -ENOENT;
  320. }
  321. cdev = ir->cdev;
  322. mutex_lock(&lirc_dev_lock);
  323. if (ir->d.minor != minor) {
  324. dev_err(ir->d.dev, "lirc_dev: minor %d device not registered\n",
  325. minor);
  326. mutex_unlock(&lirc_dev_lock);
  327. return -ENOENT;
  328. }
  329. /* end up polling thread */
  330. if (ir->task)
  331. kthread_stop(ir->task);
  332. dev_dbg(ir->d.dev, "lirc_dev: driver %s unregistered from minor = %d\n",
  333. ir->d.name, ir->d.minor);
  334. ir->attached = 0;
  335. if (ir->open) {
  336. dev_dbg(ir->d.dev, LOGHEAD "releasing opened driver\n",
  337. ir->d.name, ir->d.minor);
  338. wake_up_interruptible(&ir->buf->wait_poll);
  339. mutex_lock(&ir->irctl_lock);
  340. if (ir->d.set_use_dec)
  341. ir->d.set_use_dec(ir->d.data);
  342. module_put(cdev->owner);
  343. mutex_unlock(&ir->irctl_lock);
  344. } else {
  345. lirc_irctl_cleanup(ir);
  346. cdev_del(cdev);
  347. kfree(cdev);
  348. kfree(ir);
  349. irctls[minor] = NULL;
  350. }
  351. mutex_unlock(&lirc_dev_lock);
  352. return 0;
  353. }
  354. EXPORT_SYMBOL(lirc_unregister_driver);
  355. int lirc_dev_fop_open(struct inode *inode, struct file *file)
  356. {
  357. struct irctl *ir;
  358. struct cdev *cdev;
  359. int retval = 0;
  360. if (iminor(inode) >= MAX_IRCTL_DEVICES) {
  361. pr_err("open result for %d is -ENODEV\n", iminor(inode));
  362. return -ENODEV;
  363. }
  364. if (mutex_lock_interruptible(&lirc_dev_lock))
  365. return -ERESTARTSYS;
  366. ir = irctls[iminor(inode)];
  367. mutex_unlock(&lirc_dev_lock);
  368. if (!ir) {
  369. retval = -ENODEV;
  370. goto error;
  371. }
  372. dev_dbg(ir->d.dev, LOGHEAD "open called\n", ir->d.name, ir->d.minor);
  373. if (ir->d.minor == NOPLUG) {
  374. retval = -ENODEV;
  375. goto error;
  376. }
  377. if (ir->open) {
  378. retval = -EBUSY;
  379. goto error;
  380. }
  381. if (ir->d.rdev) {
  382. retval = rc_open(ir->d.rdev);
  383. if (retval)
  384. goto error;
  385. }
  386. cdev = ir->cdev;
  387. if (try_module_get(cdev->owner)) {
  388. ir->open++;
  389. if (ir->d.set_use_inc)
  390. retval = ir->d.set_use_inc(ir->d.data);
  391. if (retval) {
  392. module_put(cdev->owner);
  393. ir->open--;
  394. } else {
  395. lirc_buffer_clear(ir->buf);
  396. }
  397. if (ir->task)
  398. wake_up_process(ir->task);
  399. }
  400. error:
  401. nonseekable_open(inode, file);
  402. return retval;
  403. }
  404. EXPORT_SYMBOL(lirc_dev_fop_open);
  405. int lirc_dev_fop_close(struct inode *inode, struct file *file)
  406. {
  407. struct irctl *ir = irctls[iminor(inode)];
  408. struct cdev *cdev;
  409. int ret;
  410. if (!ir) {
  411. pr_err("called with invalid irctl\n");
  412. return -EINVAL;
  413. }
  414. cdev = ir->cdev;
  415. ret = mutex_lock_killable(&lirc_dev_lock);
  416. WARN_ON(ret);
  417. rc_close(ir->d.rdev);
  418. ir->open--;
  419. if (ir->attached) {
  420. if (ir->d.set_use_dec)
  421. ir->d.set_use_dec(ir->d.data);
  422. module_put(cdev->owner);
  423. } else {
  424. lirc_irctl_cleanup(ir);
  425. cdev_del(cdev);
  426. irctls[ir->d.minor] = NULL;
  427. kfree(cdev);
  428. kfree(ir);
  429. }
  430. if (!ret)
  431. mutex_unlock(&lirc_dev_lock);
  432. return 0;
  433. }
  434. EXPORT_SYMBOL(lirc_dev_fop_close);
  435. unsigned int lirc_dev_fop_poll(struct file *file, poll_table *wait)
  436. {
  437. struct irctl *ir = irctls[iminor(file_inode(file))];
  438. unsigned int ret;
  439. if (!ir) {
  440. pr_err("called with invalid irctl\n");
  441. return POLLERR;
  442. }
  443. if (!ir->attached)
  444. return POLLERR;
  445. if (ir->buf) {
  446. poll_wait(file, &ir->buf->wait_poll, wait);
  447. if (lirc_buffer_empty(ir->buf))
  448. ret = 0;
  449. else
  450. ret = POLLIN | POLLRDNORM;
  451. } else
  452. ret = POLLERR;
  453. dev_dbg(ir->d.dev, LOGHEAD "poll result = %d\n",
  454. ir->d.name, ir->d.minor, ret);
  455. return ret;
  456. }
  457. EXPORT_SYMBOL(lirc_dev_fop_poll);
  458. long lirc_dev_fop_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  459. {
  460. __u32 mode;
  461. int result = 0;
  462. struct irctl *ir = irctls[iminor(file_inode(file))];
  463. if (!ir) {
  464. pr_err("no irctl found!\n");
  465. return -ENODEV;
  466. }
  467. dev_dbg(ir->d.dev, LOGHEAD "ioctl called (0x%x)\n",
  468. ir->d.name, ir->d.minor, cmd);
  469. if (ir->d.minor == NOPLUG || !ir->attached) {
  470. dev_err(ir->d.dev, LOGHEAD "ioctl result = -ENODEV\n",
  471. ir->d.name, ir->d.minor);
  472. return -ENODEV;
  473. }
  474. mutex_lock(&ir->irctl_lock);
  475. switch (cmd) {
  476. case LIRC_GET_FEATURES:
  477. result = put_user(ir->d.features, (__u32 __user *)arg);
  478. break;
  479. case LIRC_GET_REC_MODE:
  480. if (!LIRC_CAN_REC(ir->d.features)) {
  481. result = -ENOTTY;
  482. break;
  483. }
  484. result = put_user(LIRC_REC2MODE
  485. (ir->d.features & LIRC_CAN_REC_MASK),
  486. (__u32 __user *)arg);
  487. break;
  488. case LIRC_SET_REC_MODE:
  489. if (!LIRC_CAN_REC(ir->d.features)) {
  490. result = -ENOTTY;
  491. break;
  492. }
  493. result = get_user(mode, (__u32 __user *)arg);
  494. if (!result && !(LIRC_MODE2REC(mode) & ir->d.features))
  495. result = -EINVAL;
  496. /*
  497. * FIXME: We should actually set the mode somehow but
  498. * for now, lirc_serial doesn't support mode changing either
  499. */
  500. break;
  501. case LIRC_GET_LENGTH:
  502. result = put_user(ir->d.code_length, (__u32 __user *)arg);
  503. break;
  504. case LIRC_GET_MIN_TIMEOUT:
  505. if (!(ir->d.features & LIRC_CAN_SET_REC_TIMEOUT) ||
  506. ir->d.min_timeout == 0) {
  507. result = -ENOTTY;
  508. break;
  509. }
  510. result = put_user(ir->d.min_timeout, (__u32 __user *)arg);
  511. break;
  512. case LIRC_GET_MAX_TIMEOUT:
  513. if (!(ir->d.features & LIRC_CAN_SET_REC_TIMEOUT) ||
  514. ir->d.max_timeout == 0) {
  515. result = -ENOTTY;
  516. break;
  517. }
  518. result = put_user(ir->d.max_timeout, (__u32 __user *)arg);
  519. break;
  520. default:
  521. result = -EINVAL;
  522. }
  523. mutex_unlock(&ir->irctl_lock);
  524. return result;
  525. }
  526. EXPORT_SYMBOL(lirc_dev_fop_ioctl);
  527. ssize_t lirc_dev_fop_read(struct file *file,
  528. char __user *buffer,
  529. size_t length,
  530. loff_t *ppos)
  531. {
  532. struct irctl *ir = irctls[iminor(file_inode(file))];
  533. unsigned char *buf;
  534. int ret = 0, written = 0;
  535. DECLARE_WAITQUEUE(wait, current);
  536. if (!ir) {
  537. pr_err("called with invalid irctl\n");
  538. return -ENODEV;
  539. }
  540. dev_dbg(ir->d.dev, LOGHEAD "read called\n", ir->d.name, ir->d.minor);
  541. buf = kzalloc(ir->chunk_size, GFP_KERNEL);
  542. if (!buf)
  543. return -ENOMEM;
  544. if (mutex_lock_interruptible(&ir->irctl_lock)) {
  545. ret = -ERESTARTSYS;
  546. goto out_unlocked;
  547. }
  548. if (!ir->attached) {
  549. ret = -ENODEV;
  550. goto out_locked;
  551. }
  552. if (length % ir->chunk_size) {
  553. ret = -EINVAL;
  554. goto out_locked;
  555. }
  556. /*
  557. * we add ourselves to the task queue before buffer check
  558. * to avoid losing scan code (in case when queue is awaken somewhere
  559. * between while condition checking and scheduling)
  560. */
  561. add_wait_queue(&ir->buf->wait_poll, &wait);
  562. set_current_state(TASK_INTERRUPTIBLE);
  563. /*
  564. * while we didn't provide 'length' bytes, device is opened in blocking
  565. * mode and 'copy_to_user' is happy, wait for data.
  566. */
  567. while (written < length && ret == 0) {
  568. if (lirc_buffer_empty(ir->buf)) {
  569. /* According to the read(2) man page, 'written' can be
  570. * returned as less than 'length', instead of blocking
  571. * again, returning -EWOULDBLOCK, or returning
  572. * -ERESTARTSYS
  573. */
  574. if (written)
  575. break;
  576. if (file->f_flags & O_NONBLOCK) {
  577. ret = -EWOULDBLOCK;
  578. break;
  579. }
  580. if (signal_pending(current)) {
  581. ret = -ERESTARTSYS;
  582. break;
  583. }
  584. mutex_unlock(&ir->irctl_lock);
  585. schedule();
  586. set_current_state(TASK_INTERRUPTIBLE);
  587. if (mutex_lock_interruptible(&ir->irctl_lock)) {
  588. ret = -ERESTARTSYS;
  589. remove_wait_queue(&ir->buf->wait_poll, &wait);
  590. set_current_state(TASK_RUNNING);
  591. goto out_unlocked;
  592. }
  593. if (!ir->attached) {
  594. ret = -ENODEV;
  595. break;
  596. }
  597. } else {
  598. lirc_buffer_read(ir->buf, buf);
  599. ret = copy_to_user((void __user *)buffer+written, buf,
  600. ir->buf->chunk_size);
  601. if (!ret)
  602. written += ir->buf->chunk_size;
  603. else
  604. ret = -EFAULT;
  605. }
  606. }
  607. remove_wait_queue(&ir->buf->wait_poll, &wait);
  608. set_current_state(TASK_RUNNING);
  609. out_locked:
  610. mutex_unlock(&ir->irctl_lock);
  611. out_unlocked:
  612. kfree(buf);
  613. return ret ? ret : written;
  614. }
  615. EXPORT_SYMBOL(lirc_dev_fop_read);
  616. void *lirc_get_pdata(struct file *file)
  617. {
  618. return irctls[iminor(file_inode(file))]->d.data;
  619. }
  620. EXPORT_SYMBOL(lirc_get_pdata);
  621. ssize_t lirc_dev_fop_write(struct file *file, const char __user *buffer,
  622. size_t length, loff_t *ppos)
  623. {
  624. struct irctl *ir = irctls[iminor(file_inode(file))];
  625. if (!ir) {
  626. pr_err("called with invalid irctl\n");
  627. return -ENODEV;
  628. }
  629. if (!ir->attached)
  630. return -ENODEV;
  631. return -EINVAL;
  632. }
  633. EXPORT_SYMBOL(lirc_dev_fop_write);
  634. static int __init lirc_dev_init(void)
  635. {
  636. int retval;
  637. lirc_class = class_create(THIS_MODULE, "lirc");
  638. if (IS_ERR(lirc_class)) {
  639. pr_err("class_create failed\n");
  640. return PTR_ERR(lirc_class);
  641. }
  642. retval = alloc_chrdev_region(&lirc_base_dev, 0, MAX_IRCTL_DEVICES,
  643. IRCTL_DEV_NAME);
  644. if (retval) {
  645. class_destroy(lirc_class);
  646. pr_err("alloc_chrdev_region failed\n");
  647. return retval;
  648. }
  649. pr_info("IR Remote Control driver registered, major %d\n",
  650. MAJOR(lirc_base_dev));
  651. return 0;
  652. }
  653. static void __exit lirc_dev_exit(void)
  654. {
  655. class_destroy(lirc_class);
  656. unregister_chrdev_region(lirc_base_dev, MAX_IRCTL_DEVICES);
  657. pr_info("module unloaded\n");
  658. }
  659. module_init(lirc_dev_init);
  660. module_exit(lirc_dev_exit);
  661. MODULE_DESCRIPTION("LIRC base driver module");
  662. MODULE_AUTHOR("Artur Lipowski");
  663. MODULE_LICENSE("GPL");
  664. module_param(debug, bool, S_IRUGO | S_IWUSR);
  665. MODULE_PARM_DESC(debug, "Enable debugging messages");