lirc_dev.c 18 KB

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