lirc_dev.c 16 KB

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