lirc_dev.c 16 KB

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