blob: 116daf90c858c0ccda3e0b3865218ab57ae453f5 [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Jarod Wilson4a62a5a2010-07-03 01:06:57 -03002/*
3 * LIRC base driver
4 *
5 * by Artur Lipowski <alipowski@interia.pl>
Jarod Wilson4a62a5a2010-07-03 01:06:57 -03006 */
7
Andi Shyti3fac0312016-07-06 06:01:16 -03008#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030010#include <linux/module.h>
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030011#include <linux/mutex.h>
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030012#include <linux/device.h>
Sean Youngf4364dc2018-05-27 12:24:09 +010013#include <linux/file.h>
David Härdeman46c8f472017-06-25 09:32:05 -030014#include <linux/idr.h>
Sean Younga6ddd4f2017-09-26 09:34:47 -040015#include <linux/poll.h>
Sean Young42e04422017-11-02 16:39:16 -040016#include <linux/sched.h>
17#include <linux/wait.h>
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030018
Sean Younga60d64b2017-09-23 10:41:13 -040019#include "rc-core-priv.h"
Sean Youngaefb5e32017-11-02 16:44:21 -040020#include <uapi/linux/lirc.h>
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030021
Sean Young5c4c8b42019-06-13 04:49:26 -040022#define LIRCBUF_SIZE 1024
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030023
24static dev_t lirc_base_dev;
25
David Härdeman46c8f472017-06-25 09:32:05 -030026/* Used to keep track of allocated lirc devices */
David Härdeman46c8f472017-06-25 09:32:05 -030027static DEFINE_IDA(lirc_ida);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030028
29/* Only used for sysfs but defined to void otherwise */
30static struct class *lirc_class;
31
Sean Young42e04422017-11-02 16:39:16 -040032/**
Sean Young75992a42020-08-24 21:10:45 +020033 * lirc_raw_event() - Send raw IR data to lirc to be relayed to userspace
Sean Young42e04422017-11-02 16:39:16 -040034 *
35 * @dev: the struct rc_dev descriptor of the device
36 * @ev: the struct ir_raw_event descriptor of the pulse/space
37 */
Sean Young75992a42020-08-24 21:10:45 +020038void lirc_raw_event(struct rc_dev *dev, struct ir_raw_event ev)
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030039{
Sean Young7e45d662017-11-02 17:21:13 -040040 unsigned long flags;
41 struct lirc_fh *fh;
Sean Young42e04422017-11-02 16:39:16 -040042 int sample;
Sean Younga607f512017-08-04 10:12:03 -040043
Sean Young42e04422017-11-02 16:39:16 -040044 /* Packet start */
45 if (ev.reset) {
46 /*
47 * Userspace expects a long space event before the start of
48 * the signal to use as a sync. This may be done with repeat
49 * packets and normal samples. But if a reset has been sent
50 * then we assume that a long time has passed, so we send a
51 * space with the maximum time value.
52 */
53 sample = LIRC_SPACE(LIRC_VALUE_MASK);
Sean Young1f17f682018-02-12 07:27:50 -050054 dev_dbg(&dev->dev, "delivering reset sync space to lirc_dev\n");
David Härdemanb15e3932017-06-25 09:32:36 -030055
Sean Young42e04422017-11-02 16:39:16 -040056 /* Carrier reports */
57 } else if (ev.carrier_report) {
58 sample = LIRC_FREQUENCY(ev.carrier);
Sean Young1f17f682018-02-12 07:27:50 -050059 dev_dbg(&dev->dev, "carrier report (freq: %d)\n", sample);
Sean Young42e04422017-11-02 16:39:16 -040060
61 /* Packet end */
62 } else if (ev.timeout) {
63 if (dev->gap)
64 return;
65
66 dev->gap_start = ktime_get();
67 dev->gap = true;
68 dev->gap_duration = ev.duration;
69
Sean Young528222d82020-08-23 19:23:05 +020070 sample = LIRC_TIMEOUT(ev.duration);
Sean Young1f17f682018-02-12 07:27:50 -050071 dev_dbg(&dev->dev, "timeout report (duration: %d)\n", sample);
Sean Young42e04422017-11-02 16:39:16 -040072
73 /* Normal sample */
74 } else {
75 if (dev->gap) {
Sean Young528222d82020-08-23 19:23:05 +020076 dev->gap_duration += ktime_to_us(ktime_sub(ktime_get(),
Sean Young42e04422017-11-02 16:39:16 -040077 dev->gap_start));
78
Sean Young528222d82020-08-23 19:23:05 +020079 /* Cap by LIRC_VALUE_MASK */
Sean Young42e04422017-11-02 16:39:16 -040080 dev->gap_duration = min_t(u64, dev->gap_duration,
81 LIRC_VALUE_MASK);
82
Sean Young7e45d662017-11-02 17:21:13 -040083 spin_lock_irqsave(&dev->lirc_fh_lock, flags);
84 list_for_each_entry(fh, &dev->lirc_fh, list)
85 kfifo_put(&fh->rawir,
86 LIRC_SPACE(dev->gap_duration));
87 spin_unlock_irqrestore(&dev->lirc_fh_lock, flags);
Sean Young42e04422017-11-02 16:39:16 -040088 dev->gap = false;
89 }
90
Sean Young528222d82020-08-23 19:23:05 +020091 sample = ev.pulse ? LIRC_PULSE(ev.duration) :
92 LIRC_SPACE(ev.duration);
Sean Young1f17f682018-02-12 07:27:50 -050093 dev_dbg(&dev->dev, "delivering %uus %s to lirc_dev\n",
Sean Young528222d82020-08-23 19:23:05 +020094 ev.duration, TO_STR(ev.pulse));
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030095 }
Sean Young42e04422017-11-02 16:39:16 -040096
Sean Youngf4364dc2018-05-27 12:24:09 +010097 /*
98 * bpf does not care about the gap generated above; that exists
99 * for backwards compatibility
100 */
101 lirc_bpf_run(dev, sample);
102
Sean Young7e45d662017-11-02 17:21:13 -0400103 spin_lock_irqsave(&dev->lirc_fh_lock, flags);
104 list_for_each_entry(fh, &dev->lirc_fh, list) {
105 if (LIRC_IS_TIMEOUT(sample) && !fh->send_timeout_reports)
106 continue;
107 if (kfifo_put(&fh->rawir, sample))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800108 wake_up_poll(&fh->wait_poll, EPOLLIN | EPOLLRDNORM);
Sean Young7e45d662017-11-02 17:21:13 -0400109 }
110 spin_unlock_irqrestore(&dev->lirc_fh_lock, flags);
David Härdeman3381b772017-06-30 05:41:57 -0300111}
112
Sean Young42e04422017-11-02 16:39:16 -0400113/**
Sean Young75992a42020-08-24 21:10:45 +0200114 * lirc_scancode_event() - Send scancode data to lirc to be relayed to
Sean Young7e45d662017-11-02 17:21:13 -0400115 * userspace. This can be called in atomic context.
Sean Young42e04422017-11-02 16:39:16 -0400116 * @dev: the struct rc_dev descriptor of the device
117 * @lsc: the struct lirc_scancode describing the decoded scancode
118 */
Sean Young75992a42020-08-24 21:10:45 +0200119void lirc_scancode_event(struct rc_dev *dev, struct lirc_scancode *lsc)
David Härdeman3381b772017-06-30 05:41:57 -0300120{
Sean Young7e45d662017-11-02 17:21:13 -0400121 unsigned long flags;
122 struct lirc_fh *fh;
Sean Young74c839b2017-01-30 13:49:58 -0200123
Sean Young42e04422017-11-02 16:39:16 -0400124 lsc->timestamp = ktime_get_ns();
125
Sean Young7e45d662017-11-02 17:21:13 -0400126 spin_lock_irqsave(&dev->lirc_fh_lock, flags);
127 list_for_each_entry(fh, &dev->lirc_fh, list) {
128 if (kfifo_put(&fh->scancodes, *lsc))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800129 wake_up_poll(&fh->wait_poll, EPOLLIN | EPOLLRDNORM);
Andi Shyti6fa99e12016-07-06 06:01:13 -0300130 }
Sean Young7e45d662017-11-02 17:21:13 -0400131 spin_unlock_irqrestore(&dev->lirc_fh_lock, flags);
Andi Shyti6fa99e12016-07-06 06:01:13 -0300132}
Sean Young75992a42020-08-24 21:10:45 +0200133EXPORT_SYMBOL_GPL(lirc_scancode_event);
Andi Shyti6fa99e12016-07-06 06:01:13 -0300134
Sean Young75992a42020-08-24 21:10:45 +0200135static int lirc_open(struct inode *inode, struct file *file)
David Härdeman6ecccc32017-06-25 09:32:15 -0300136{
Sean Young42e04422017-11-02 16:39:16 -0400137 struct rc_dev *dev = container_of(inode->i_cdev, struct rc_dev,
138 lirc_cdev);
Sean Young7e45d662017-11-02 17:21:13 -0400139 struct lirc_fh *fh = kzalloc(sizeof(*fh), GFP_KERNEL);
140 unsigned long flags;
David Härdemande226ec2017-06-25 09:31:19 -0300141 int retval;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300142
Sean Young7e45d662017-11-02 17:21:13 -0400143 if (!fh)
144 return -ENOMEM;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300145
Sean Young7e45d662017-11-02 17:21:13 -0400146 get_device(&dev->dev);
David Härdeman3381b772017-06-30 05:41:57 -0300147
Sean Young42e04422017-11-02 16:39:16 -0400148 if (!dev->registered) {
David Härdeman3381b772017-06-30 05:41:57 -0300149 retval = -ENODEV;
Sean Young7e45d662017-11-02 17:21:13 -0400150 goto out_fh;
David Härdeman3381b772017-06-30 05:41:57 -0300151 }
152
Sean Young7e45d662017-11-02 17:21:13 -0400153 if (dev->driver_type == RC_DRIVER_IR_RAW) {
154 if (kfifo_alloc(&fh->rawir, MAX_IR_EVENT_SIZE, GFP_KERNEL)) {
155 retval = -ENOMEM;
156 goto out_fh;
157 }
David Härdeman3381b772017-06-30 05:41:57 -0300158 }
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300159
Sean Young7e45d662017-11-02 17:21:13 -0400160 if (dev->driver_type != RC_DRIVER_IR_RAW_TX) {
161 if (kfifo_alloc(&fh->scancodes, 32, GFP_KERNEL)) {
162 retval = -ENOMEM;
163 goto out_rawir;
164 }
Srinivas Kandagatlaca7a7222013-07-22 04:23:07 -0300165 }
166
Sean Young7e45d662017-11-02 17:21:13 -0400167 fh->send_mode = LIRC_MODE_PULSE;
168 fh->rc = dev;
169 fh->send_timeout_reports = true;
David Härdeman2c5a1f442017-05-01 13:03:46 -0300170
Sean Young7e45d662017-11-02 17:21:13 -0400171 if (dev->driver_type == RC_DRIVER_SCANCODE)
172 fh->rec_mode = LIRC_MODE_SCANCODE;
173 else
174 fh->rec_mode = LIRC_MODE_MODE2;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300175
Sean Young7e45d662017-11-02 17:21:13 -0400176 retval = rc_open(dev);
177 if (retval)
178 goto out_kfifo;
179
180 init_waitqueue_head(&fh->wait_poll);
181
182 file->private_data = fh;
183 spin_lock_irqsave(&dev->lirc_fh_lock, flags);
184 list_add(&fh->list, &dev->lirc_fh);
185 spin_unlock_irqrestore(&dev->lirc_fh_lock, flags);
Sean Young42e04422017-11-02 16:39:16 -0400186
Kirill Smelkovc5bf68f2019-03-26 23:51:19 +0300187 stream_open(inode, file);
Arnd Bergmannd9d2e9d2010-08-15 18:51:56 +0200188
David Härdemande226ec2017-06-25 09:31:19 -0300189 return 0;
Sean Young7e45d662017-11-02 17:21:13 -0400190out_kfifo:
191 if (dev->driver_type != RC_DRIVER_IR_RAW_TX)
192 kfifo_free(&fh->scancodes);
193out_rawir:
194 if (dev->driver_type == RC_DRIVER_IR_RAW)
195 kfifo_free(&fh->rawir);
196out_fh:
197 kfree(fh);
198 put_device(&dev->dev);
David Härdeman3381b772017-06-30 05:41:57 -0300199
David Härdeman3381b772017-06-30 05:41:57 -0300200 return retval;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300201}
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300202
Sean Young75992a42020-08-24 21:10:45 +0200203static int lirc_close(struct inode *inode, struct file *file)
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300204{
Sean Young7e45d662017-11-02 17:21:13 -0400205 struct lirc_fh *fh = file->private_data;
206 struct rc_dev *dev = fh->rc;
207 unsigned long flags;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300208
Sean Young7e45d662017-11-02 17:21:13 -0400209 spin_lock_irqsave(&dev->lirc_fh_lock, flags);
210 list_del(&fh->list);
211 spin_unlock_irqrestore(&dev->lirc_fh_lock, flags);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300212
Sean Young7e45d662017-11-02 17:21:13 -0400213 if (dev->driver_type == RC_DRIVER_IR_RAW)
214 kfifo_free(&fh->rawir);
215 if (dev->driver_type != RC_DRIVER_IR_RAW_TX)
216 kfifo_free(&fh->scancodes);
217 kfree(fh);
David Härdeman3381b772017-06-30 05:41:57 -0300218
Sean Young42e04422017-11-02 16:39:16 -0400219 rc_close(dev);
Sean Young7e45d662017-11-02 17:21:13 -0400220 put_device(&dev->dev);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300221
222 return 0;
223}
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300224
Sean Young75992a42020-08-24 21:10:45 +0200225static ssize_t lirc_transmit(struct file *file, const char __user *buf,
226 size_t n, loff_t *ppos)
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300227{
Sean Young7e45d662017-11-02 17:21:13 -0400228 struct lirc_fh *fh = file->private_data;
229 struct rc_dev *dev = fh->rc;
Sean Younga74b2bf2017-12-13 16:09:21 -0500230 unsigned int *txbuf;
Sean Young42e04422017-11-02 16:39:16 -0400231 struct ir_raw_event *raw = NULL;
Sean Young49571332017-11-04 08:30:45 -0400232 ssize_t ret;
Sean Young42e04422017-11-02 16:39:16 -0400233 size_t count;
234 ktime_t start;
235 s64 towait;
236 unsigned int duration = 0; /* signal duration in us */
237 int i;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300238
Sean Young49571332017-11-04 08:30:45 -0400239 ret = mutex_lock_interruptible(&dev->lock);
240 if (ret)
241 return ret;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300242
Sean Young49571332017-11-04 08:30:45 -0400243 if (!dev->registered) {
244 ret = -ENODEV;
Sean Younga74b2bf2017-12-13 16:09:21 -0500245 goto out_unlock;
David Härdemanb15e3932017-06-25 09:32:36 -0300246 }
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300247
Sean Young42e04422017-11-02 16:39:16 -0400248 if (!dev->tx_ir) {
249 ret = -EINVAL;
Sean Younga74b2bf2017-12-13 16:09:21 -0500250 goto out_unlock;
Sean Young42e04422017-11-02 16:39:16 -0400251 }
252
Sean Young7e45d662017-11-02 17:21:13 -0400253 if (fh->send_mode == LIRC_MODE_SCANCODE) {
Sean Young42e04422017-11-02 16:39:16 -0400254 struct lirc_scancode scan;
255
Sean Young49571332017-11-04 08:30:45 -0400256 if (n != sizeof(scan)) {
257 ret = -EINVAL;
Sean Younga74b2bf2017-12-13 16:09:21 -0500258 goto out_unlock;
Sean Young49571332017-11-04 08:30:45 -0400259 }
Sean Young42e04422017-11-02 16:39:16 -0400260
Sean Young49571332017-11-04 08:30:45 -0400261 if (copy_from_user(&scan, buf, sizeof(scan))) {
262 ret = -EFAULT;
Sean Younga74b2bf2017-12-13 16:09:21 -0500263 goto out_unlock;
Sean Young49571332017-11-04 08:30:45 -0400264 }
Sean Young42e04422017-11-02 16:39:16 -0400265
Dan Carpenter72e637f2020-10-30 12:52:30 +0100266 if (scan.flags || scan.keycode || scan.timestamp ||
267 scan.rc_proto > RC_PROTO_MAX) {
Sean Young49571332017-11-04 08:30:45 -0400268 ret = -EINVAL;
Sean Younga74b2bf2017-12-13 16:09:21 -0500269 goto out_unlock;
Sean Young49571332017-11-04 08:30:45 -0400270 }
Sean Young42e04422017-11-02 16:39:16 -0400271
Sean Younge6c6d7d2020-01-17 17:46:36 +0100272 /* We only have encoders for 32-bit protocols. */
Sean Young42e04422017-11-02 16:39:16 -0400273 if (scan.scancode > U32_MAX ||
Sean Young49571332017-11-04 08:30:45 -0400274 !rc_validate_scancode(scan.rc_proto, scan.scancode)) {
275 ret = -EINVAL;
Sean Younga74b2bf2017-12-13 16:09:21 -0500276 goto out_unlock;
Sean Young49571332017-11-04 08:30:45 -0400277 }
Sean Young42e04422017-11-02 16:39:16 -0400278
279 raw = kmalloc_array(LIRCBUF_SIZE, sizeof(*raw), GFP_KERNEL);
Sean Young49571332017-11-04 08:30:45 -0400280 if (!raw) {
281 ret = -ENOMEM;
Sean Younga74b2bf2017-12-13 16:09:21 -0500282 goto out_unlock;
Sean Young49571332017-11-04 08:30:45 -0400283 }
Sean Young42e04422017-11-02 16:39:16 -0400284
285 ret = ir_raw_encode_scancode(scan.rc_proto, scan.scancode,
286 raw, LIRCBUF_SIZE);
287 if (ret < 0)
Colin Ian King8d25e152017-12-19 11:48:25 -0500288 goto out_kfree_raw;
Sean Young42e04422017-11-02 16:39:16 -0400289
290 count = ret;
291
292 txbuf = kmalloc_array(count, sizeof(unsigned int), GFP_KERNEL);
293 if (!txbuf) {
294 ret = -ENOMEM;
Colin Ian King8d25e152017-12-19 11:48:25 -0500295 goto out_kfree_raw;
Sean Young42e04422017-11-02 16:39:16 -0400296 }
297
298 for (i = 0; i < count; i++)
Sean Young528222d82020-08-23 19:23:05 +0200299 txbuf[i] = raw[i].duration;
Sean Young42e04422017-11-02 16:39:16 -0400300
301 if (dev->s_tx_carrier) {
302 int carrier = ir_raw_encode_carrier(scan.rc_proto);
303
304 if (carrier > 0)
305 dev->s_tx_carrier(dev, carrier);
306 }
307 } else {
Sean Young49571332017-11-04 08:30:45 -0400308 if (n < sizeof(unsigned int) || n % sizeof(unsigned int)) {
309 ret = -EINVAL;
Sean Younga74b2bf2017-12-13 16:09:21 -0500310 goto out_unlock;
Sean Young49571332017-11-04 08:30:45 -0400311 }
Sean Young42e04422017-11-02 16:39:16 -0400312
313 count = n / sizeof(unsigned int);
Sean Young49571332017-11-04 08:30:45 -0400314 if (count > LIRCBUF_SIZE || count % 2 == 0) {
315 ret = -EINVAL;
Sean Younga74b2bf2017-12-13 16:09:21 -0500316 goto out_unlock;
Sean Young49571332017-11-04 08:30:45 -0400317 }
Sean Young42e04422017-11-02 16:39:16 -0400318
319 txbuf = memdup_user(buf, n);
Sean Young49571332017-11-04 08:30:45 -0400320 if (IS_ERR(txbuf)) {
321 ret = PTR_ERR(txbuf);
Sean Younga74b2bf2017-12-13 16:09:21 -0500322 goto out_unlock;
Sean Young49571332017-11-04 08:30:45 -0400323 }
Sean Young42e04422017-11-02 16:39:16 -0400324 }
325
326 for (i = 0; i < count; i++) {
Sean Young528222d82020-08-23 19:23:05 +0200327 if (txbuf[i] > IR_MAX_DURATION - duration || !txbuf[i]) {
Sean Young42e04422017-11-02 16:39:16 -0400328 ret = -EINVAL;
Sean Younga74b2bf2017-12-13 16:09:21 -0500329 goto out_kfree;
Sean Young42e04422017-11-02 16:39:16 -0400330 }
331
332 duration += txbuf[i];
333 }
334
Sean Young29422732018-02-12 09:00:28 -0500335 start = ktime_get();
336
Sean Young42e04422017-11-02 16:39:16 -0400337 ret = dev->tx_ir(dev, txbuf, count);
338 if (ret < 0)
Sean Younga74b2bf2017-12-13 16:09:21 -0500339 goto out_kfree;
Sean Young42e04422017-11-02 16:39:16 -0400340
Sean Youngf81a8152017-12-13 16:30:22 -0500341 kfree(txbuf);
342 kfree(raw);
343 mutex_unlock(&dev->lock);
344
Sean Youngdde7edf2017-12-11 17:12:09 -0500345 /*
346 * The lircd gap calculation expects the write function to
347 * wait for the actual IR signal to be transmitted before
348 * returning.
349 */
350 towait = ktime_us_delta(ktime_add_us(start, duration),
351 ktime_get());
352 if (towait > 0) {
353 set_current_state(TASK_INTERRUPTIBLE);
354 schedule_timeout(usecs_to_jiffies(towait));
Sean Young42e04422017-11-02 16:39:16 -0400355 }
356
Sean Youngf81a8152017-12-13 16:30:22 -0500357 return n;
Sean Younga74b2bf2017-12-13 16:09:21 -0500358out_kfree:
Sean Young42e04422017-11-02 16:39:16 -0400359 kfree(txbuf);
Colin Ian King8d25e152017-12-19 11:48:25 -0500360out_kfree_raw:
Sean Young42e04422017-11-02 16:39:16 -0400361 kfree(raw);
Sean Younga74b2bf2017-12-13 16:09:21 -0500362out_unlock:
363 mutex_unlock(&dev->lock);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300364 return ret;
365}
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300366
Sean Young75992a42020-08-24 21:10:45 +0200367static long lirc_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300368{
Sean Young7e45d662017-11-02 17:21:13 -0400369 struct lirc_fh *fh = file->private_data;
370 struct rc_dev *dev = fh->rc;
Sean Young42e04422017-11-02 16:39:16 -0400371 u32 __user *argp = (u32 __user *)(arg);
Sean Young49571332017-11-04 08:30:45 -0400372 u32 val = 0;
373 int ret;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300374
Sean Young42e04422017-11-02 16:39:16 -0400375 if (_IOC_DIR(cmd) & _IOC_WRITE) {
376 ret = get_user(val, argp);
377 if (ret)
378 return ret;
379 }
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300380
Sean Young49571332017-11-04 08:30:45 -0400381 ret = mutex_lock_interruptible(&dev->lock);
382 if (ret)
383 return ret;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300384
Sean Young49571332017-11-04 08:30:45 -0400385 if (!dev->registered) {
386 ret = -ENODEV;
David Härdeman3381b772017-06-30 05:41:57 -0300387 goto out;
388 }
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300389
390 switch (cmd) {
391 case LIRC_GET_FEATURES:
Sean Young42e04422017-11-02 16:39:16 -0400392 if (dev->driver_type == RC_DRIVER_SCANCODE)
393 val |= LIRC_CAN_REC_SCANCODE;
394
395 if (dev->driver_type == RC_DRIVER_IR_RAW) {
Sean Young02d742f2017-12-28 14:58:26 -0500396 val |= LIRC_CAN_REC_MODE2;
Sean Young42e04422017-11-02 16:39:16 -0400397 if (dev->rx_resolution)
398 val |= LIRC_CAN_GET_REC_RESOLUTION;
399 }
400
401 if (dev->tx_ir) {
Sean Young02d742f2017-12-28 14:58:26 -0500402 val |= LIRC_CAN_SEND_PULSE;
Sean Young42e04422017-11-02 16:39:16 -0400403 if (dev->s_tx_mask)
404 val |= LIRC_CAN_SET_TRANSMITTER_MASK;
405 if (dev->s_tx_carrier)
406 val |= LIRC_CAN_SET_SEND_CARRIER;
407 if (dev->s_tx_duty_cycle)
408 val |= LIRC_CAN_SET_SEND_DUTY_CYCLE;
409 }
410
411 if (dev->s_rx_carrier_range)
412 val |= LIRC_CAN_SET_REC_CARRIER |
413 LIRC_CAN_SET_REC_CARRIER_RANGE;
414
415 if (dev->s_learning_mode)
416 val |= LIRC_CAN_USE_WIDEBAND_RECEIVER;
417
418 if (dev->s_carrier_report)
419 val |= LIRC_CAN_MEASURE_CARRIER;
420
421 if (dev->max_timeout)
422 val |= LIRC_CAN_SET_REC_TIMEOUT;
423
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300424 break;
Sean Young42e04422017-11-02 16:39:16 -0400425
426 /* mode support */
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300427 case LIRC_GET_REC_MODE:
Sean Young42e04422017-11-02 16:39:16 -0400428 if (dev->driver_type == RC_DRIVER_IR_RAW_TX)
Sean Young49571332017-11-04 08:30:45 -0400429 ret = -ENOTTY;
430 else
431 val = fh->rec_mode;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300432 break;
Sean Young42e04422017-11-02 16:39:16 -0400433
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300434 case LIRC_SET_REC_MODE:
Sean Young42e04422017-11-02 16:39:16 -0400435 switch (dev->driver_type) {
436 case RC_DRIVER_IR_RAW_TX:
Sean Young49571332017-11-04 08:30:45 -0400437 ret = -ENOTTY;
438 break;
Sean Young42e04422017-11-02 16:39:16 -0400439 case RC_DRIVER_SCANCODE:
440 if (val != LIRC_MODE_SCANCODE)
Sean Young49571332017-11-04 08:30:45 -0400441 ret = -EINVAL;
Sean Young42e04422017-11-02 16:39:16 -0400442 break;
443 case RC_DRIVER_IR_RAW:
444 if (!(val == LIRC_MODE_MODE2 ||
445 val == LIRC_MODE_SCANCODE))
Sean Young49571332017-11-04 08:30:45 -0400446 ret = -EINVAL;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300447 break;
448 }
449
Sean Young49571332017-11-04 08:30:45 -0400450 if (!ret)
451 fh->rec_mode = val;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300452 break;
Sean Young42e04422017-11-02 16:39:16 -0400453
454 case LIRC_GET_SEND_MODE:
455 if (!dev->tx_ir)
Sean Young49571332017-11-04 08:30:45 -0400456 ret = -ENOTTY;
457 else
458 val = fh->send_mode;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300459 break;
Sean Young42e04422017-11-02 16:39:16 -0400460
461 case LIRC_SET_SEND_MODE:
462 if (!dev->tx_ir)
Sean Young49571332017-11-04 08:30:45 -0400463 ret = -ENOTTY;
464 else if (!(val == LIRC_MODE_PULSE || val == LIRC_MODE_SCANCODE))
465 ret = -EINVAL;
466 else
467 fh->send_mode = val;
468 break;
Sean Young42e04422017-11-02 16:39:16 -0400469
470 /* TX settings */
471 case LIRC_SET_TRANSMITTER_MASK:
472 if (!dev->s_tx_mask)
Sean Young49571332017-11-04 08:30:45 -0400473 ret = -ENOTTY;
474 else
475 ret = dev->s_tx_mask(dev, val);
476 break;
Sean Young42e04422017-11-02 16:39:16 -0400477
478 case LIRC_SET_SEND_CARRIER:
479 if (!dev->s_tx_carrier)
Sean Young49571332017-11-04 08:30:45 -0400480 ret = -ENOTTY;
481 else
482 ret = dev->s_tx_carrier(dev, val);
483 break;
Sean Young42e04422017-11-02 16:39:16 -0400484
485 case LIRC_SET_SEND_DUTY_CYCLE:
486 if (!dev->s_tx_duty_cycle)
Sean Young49571332017-11-04 08:30:45 -0400487 ret = -ENOTTY;
488 else if (val <= 0 || val >= 100)
489 ret = -EINVAL;
490 else
491 ret = dev->s_tx_duty_cycle(dev, val);
492 break;
Sean Young42e04422017-11-02 16:39:16 -0400493
494 /* RX settings */
495 case LIRC_SET_REC_CARRIER:
496 if (!dev->s_rx_carrier_range)
Sean Young49571332017-11-04 08:30:45 -0400497 ret = -ENOTTY;
498 else if (val <= 0)
499 ret = -EINVAL;
500 else
501 ret = dev->s_rx_carrier_range(dev, fh->carrier_low,
502 val);
503 break;
Sean Young42e04422017-11-02 16:39:16 -0400504
505 case LIRC_SET_REC_CARRIER_RANGE:
506 if (!dev->s_rx_carrier_range)
Sean Young49571332017-11-04 08:30:45 -0400507 ret = -ENOTTY;
508 else if (val <= 0)
509 ret = -EINVAL;
510 else
511 fh->carrier_low = val;
512 break;
Sean Young42e04422017-11-02 16:39:16 -0400513
514 case LIRC_GET_REC_RESOLUTION:
515 if (!dev->rx_resolution)
Sean Young49571332017-11-04 08:30:45 -0400516 ret = -ENOTTY;
517 else
Sean Young528222d82020-08-23 19:23:05 +0200518 val = dev->rx_resolution;
Sean Young42e04422017-11-02 16:39:16 -0400519 break;
520
521 case LIRC_SET_WIDEBAND_RECEIVER:
522 if (!dev->s_learning_mode)
Sean Young49571332017-11-04 08:30:45 -0400523 ret = -ENOTTY;
524 else
525 ret = dev->s_learning_mode(dev, !!val);
526 break;
Sean Young42e04422017-11-02 16:39:16 -0400527
528 case LIRC_SET_MEASURE_CARRIER_MODE:
529 if (!dev->s_carrier_report)
Sean Young49571332017-11-04 08:30:45 -0400530 ret = -ENOTTY;
531 else
532 ret = dev->s_carrier_report(dev, !!val);
533 break;
Sean Young42e04422017-11-02 16:39:16 -0400534
535 /* Generic timeout support */
536 case LIRC_GET_MIN_TIMEOUT:
537 if (!dev->max_timeout)
Sean Young49571332017-11-04 08:30:45 -0400538 ret = -ENOTTY;
539 else
Sean Young528222d82020-08-23 19:23:05 +0200540 val = dev->min_timeout;
Sean Young42e04422017-11-02 16:39:16 -0400541 break;
542
543 case LIRC_GET_MAX_TIMEOUT:
544 if (!dev->max_timeout)
Sean Young49571332017-11-04 08:30:45 -0400545 ret = -ENOTTY;
546 else
Sean Young528222d82020-08-23 19:23:05 +0200547 val = dev->max_timeout;
Sean Young42e04422017-11-02 16:39:16 -0400548 break;
549
550 case LIRC_SET_REC_TIMEOUT:
Sean Young49571332017-11-04 08:30:45 -0400551 if (!dev->max_timeout) {
552 ret = -ENOTTY;
Sean Young49571332017-11-04 08:30:45 -0400553 } else {
Sean Young528222d82020-08-23 19:23:05 +0200554 if (val < dev->min_timeout || val > dev->max_timeout)
Sean Young49571332017-11-04 08:30:45 -0400555 ret = -EINVAL;
556 else if (dev->s_timeout)
Sean Young528222d82020-08-23 19:23:05 +0200557 ret = dev->s_timeout(dev, val);
Sean Youngc2837ad2018-02-12 08:59:00 -0500558 else
Sean Young528222d82020-08-23 19:23:05 +0200559 dev->timeout = val;
Sean Young49571332017-11-04 08:30:45 -0400560 }
Sean Young42e04422017-11-02 16:39:16 -0400561 break;
562
Sean Young95d15442018-03-23 16:59:52 -0400563 case LIRC_GET_REC_TIMEOUT:
Sean Young42e04422017-11-02 16:39:16 -0400564 if (!dev->timeout)
Sean Young49571332017-11-04 08:30:45 -0400565 ret = -ENOTTY;
566 else
Sean Young528222d82020-08-23 19:23:05 +0200567 val = dev->timeout;
Sean Young95d15442018-03-23 16:59:52 -0400568 break;
569
Sean Young42e04422017-11-02 16:39:16 -0400570 case LIRC_SET_REC_TIMEOUT_REPORTS:
Sean Young28492252018-03-24 08:02:48 -0400571 if (dev->driver_type != RC_DRIVER_IR_RAW)
Sean Young49571332017-11-04 08:30:45 -0400572 ret = -ENOTTY;
573 else
574 fh->send_timeout_reports = !!val;
Sean Young42e04422017-11-02 16:39:16 -0400575 break;
576
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300577 default:
Sean Young49571332017-11-04 08:30:45 -0400578 ret = -ENOTTY;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300579 }
580
Sean Young49571332017-11-04 08:30:45 -0400581 if (!ret && _IOC_DIR(cmd) & _IOC_READ)
Sean Young42e04422017-11-02 16:39:16 -0400582 ret = put_user(val, argp);
583
David Härdeman3381b772017-06-30 05:41:57 -0300584out:
Sean Young49571332017-11-04 08:30:45 -0400585 mutex_unlock(&dev->lock);
Sean Young42e04422017-11-02 16:39:16 -0400586 return ret;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300587}
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300588
Sean Young75992a42020-08-24 21:10:45 +0200589static __poll_t lirc_poll(struct file *file, struct poll_table_struct *wait)
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300590{
Sean Young7e45d662017-11-02 17:21:13 -0400591 struct lirc_fh *fh = file->private_data;
592 struct rc_dev *rcdev = fh->rc;
Linus Torvalds68c57352018-02-06 11:27:48 -0800593 __poll_t events = 0;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300594
Sean Young7e45d662017-11-02 17:21:13 -0400595 poll_wait(file, &fh->wait_poll, wait);
Jarod Wilson715d29a72010-10-18 12:02:01 -0300596
Sean Young42e04422017-11-02 16:39:16 -0400597 if (!rcdev->registered) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800598 events = EPOLLHUP | EPOLLERR;
Sean Young42e04422017-11-02 16:39:16 -0400599 } else if (rcdev->driver_type != RC_DRIVER_IR_RAW_TX) {
Sean Young7e45d662017-11-02 17:21:13 -0400600 if (fh->rec_mode == LIRC_MODE_SCANCODE &&
601 !kfifo_is_empty(&fh->scancodes))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800602 events = EPOLLIN | EPOLLRDNORM;
David Härdemanb15e3932017-06-25 09:32:36 -0300603
Sean Young7e45d662017-11-02 17:21:13 -0400604 if (fh->rec_mode == LIRC_MODE_MODE2 &&
605 !kfifo_is_empty(&fh->rawir))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800606 events = EPOLLIN | EPOLLRDNORM;
Dan Carpenter250f7a52010-11-17 02:20:15 -0300607 }
David Härdeman3381b772017-06-30 05:41:57 -0300608
Sean Young42e04422017-11-02 16:39:16 -0400609 return events;
610}
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300611
Sean Young75992a42020-08-24 21:10:45 +0200612static ssize_t lirc_read_mode2(struct file *file, char __user *buffer,
613 size_t length)
Sean Young42e04422017-11-02 16:39:16 -0400614{
Sean Young7e45d662017-11-02 17:21:13 -0400615 struct lirc_fh *fh = file->private_data;
616 struct rc_dev *rcdev = fh->rc;
Sean Young42e04422017-11-02 16:39:16 -0400617 unsigned int copied;
618 int ret;
David Härdeman3381b772017-06-30 05:41:57 -0300619
Sean Young42e04422017-11-02 16:39:16 -0400620 if (length < sizeof(unsigned int) || length % sizeof(unsigned int))
621 return -EINVAL;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300622
Sean Young42e04422017-11-02 16:39:16 -0400623 do {
Sean Young7e45d662017-11-02 17:21:13 -0400624 if (kfifo_is_empty(&fh->rawir)) {
Sean Young42e04422017-11-02 16:39:16 -0400625 if (file->f_flags & O_NONBLOCK)
626 return -EAGAIN;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300627
Sean Young7e45d662017-11-02 17:21:13 -0400628 ret = wait_event_interruptible(fh->wait_poll,
629 !kfifo_is_empty(&fh->rawir) ||
Sean Young42e04422017-11-02 16:39:16 -0400630 !rcdev->registered);
631 if (ret)
632 return ret;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300633 }
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300634
Sean Young42e04422017-11-02 16:39:16 -0400635 if (!rcdev->registered)
636 return -ENODEV;
Dan Carpenter250f7a52010-11-17 02:20:15 -0300637
Sean Young42e04422017-11-02 16:39:16 -0400638 ret = mutex_lock_interruptible(&rcdev->lock);
639 if (ret)
640 return ret;
Sean Young7e45d662017-11-02 17:21:13 -0400641 ret = kfifo_to_user(&fh->rawir, buffer, length, &copied);
Sean Young42e04422017-11-02 16:39:16 -0400642 mutex_unlock(&rcdev->lock);
643 if (ret)
644 return ret;
645 } while (copied == 0);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300646
Sean Young42e04422017-11-02 16:39:16 -0400647 return copied;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300648}
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300649
Sean Young75992a42020-08-24 21:10:45 +0200650static ssize_t lirc_read_scancode(struct file *file, char __user *buffer,
651 size_t length)
David Härdeman615cd3f2017-06-25 09:31:40 -0300652{
Sean Young7e45d662017-11-02 17:21:13 -0400653 struct lirc_fh *fh = file->private_data;
654 struct rc_dev *rcdev = fh->rc;
Sean Young42e04422017-11-02 16:39:16 -0400655 unsigned int copied;
656 int ret;
David Härdeman615cd3f2017-06-25 09:31:40 -0300657
Sean Young42e04422017-11-02 16:39:16 -0400658 if (length < sizeof(struct lirc_scancode) ||
659 length % sizeof(struct lirc_scancode))
660 return -EINVAL;
661
662 do {
Sean Young7e45d662017-11-02 17:21:13 -0400663 if (kfifo_is_empty(&fh->scancodes)) {
Sean Young42e04422017-11-02 16:39:16 -0400664 if (file->f_flags & O_NONBLOCK)
665 return -EAGAIN;
666
Sean Young7e45d662017-11-02 17:21:13 -0400667 ret = wait_event_interruptible(fh->wait_poll,
668 !kfifo_is_empty(&fh->scancodes) ||
Sean Young42e04422017-11-02 16:39:16 -0400669 !rcdev->registered);
670 if (ret)
671 return ret;
672 }
673
674 if (!rcdev->registered)
675 return -ENODEV;
676
677 ret = mutex_lock_interruptible(&rcdev->lock);
678 if (ret)
679 return ret;
Sean Young7e45d662017-11-02 17:21:13 -0400680 ret = kfifo_to_user(&fh->scancodes, buffer, length, &copied);
Sean Young42e04422017-11-02 16:39:16 -0400681 mutex_unlock(&rcdev->lock);
682 if (ret)
683 return ret;
684 } while (copied == 0);
685
686 return copied;
David Härdeman615cd3f2017-06-25 09:31:40 -0300687}
David Härdeman615cd3f2017-06-25 09:31:40 -0300688
Sean Young75992a42020-08-24 21:10:45 +0200689static ssize_t lirc_read(struct file *file, char __user *buffer, size_t length,
690 loff_t *ppos)
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300691{
Sean Young7e45d662017-11-02 17:21:13 -0400692 struct lirc_fh *fh = file->private_data;
693 struct rc_dev *rcdev = fh->rc;
David Härdeman615cd3f2017-06-25 09:31:40 -0300694
Sean Young42e04422017-11-02 16:39:16 -0400695 if (rcdev->driver_type == RC_DRIVER_IR_RAW_TX)
696 return -EINVAL;
697
698 if (!rcdev->registered)
699 return -ENODEV;
700
Sean Young7e45d662017-11-02 17:21:13 -0400701 if (fh->rec_mode == LIRC_MODE_MODE2)
Sean Young75992a42020-08-24 21:10:45 +0200702 return lirc_read_mode2(file, buffer, length);
Sean Young42e04422017-11-02 16:39:16 -0400703 else /* LIRC_MODE_SCANCODE */
Sean Young75992a42020-08-24 21:10:45 +0200704 return lirc_read_scancode(file, buffer, length);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300705}
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300706
Sean Young42e04422017-11-02 16:39:16 -0400707static const struct file_operations lirc_fops = {
708 .owner = THIS_MODULE,
Sean Young75992a42020-08-24 21:10:45 +0200709 .write = lirc_transmit,
710 .unlocked_ioctl = lirc_ioctl,
Arnd Bergmann1832f2d2018-09-11 21:59:08 +0200711 .compat_ioctl = compat_ptr_ioctl,
Sean Young75992a42020-08-24 21:10:45 +0200712 .read = lirc_read,
713 .poll = lirc_poll,
714 .open = lirc_open,
715 .release = lirc_close,
Sean Young42e04422017-11-02 16:39:16 -0400716 .llseek = no_llseek,
717};
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300718
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300719static void lirc_release_device(struct device *ld)
720{
Sean Younga6ddd4f2017-09-26 09:34:47 -0400721 struct rc_dev *rcdev = container_of(ld, struct rc_dev, lirc_dev);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300722
Sean Younga6ddd4f2017-09-26 09:34:47 -0400723 put_device(&rcdev->dev);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300724}
725
Sean Young75992a42020-08-24 21:10:45 +0200726int lirc_register(struct rc_dev *dev)
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300727{
Sean Younged8c34d2018-03-27 11:24:05 -0400728 const char *rx_type, *tx_type;
Sean Younga6ddd4f2017-09-26 09:34:47 -0400729 int err, minor;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300730
Sean Young7e45d662017-11-02 17:21:13 -0400731 minor = ida_simple_get(&lirc_ida, 0, RC_DEV_MAX, GFP_KERNEL);
732 if (minor < 0)
733 return minor;
734
Sean Younga6ddd4f2017-09-26 09:34:47 -0400735 device_initialize(&dev->lirc_dev);
736 dev->lirc_dev.class = lirc_class;
Sean Younga6ddd4f2017-09-26 09:34:47 -0400737 dev->lirc_dev.parent = &dev->dev;
Sean Young7e45d662017-11-02 17:21:13 -0400738 dev->lirc_dev.release = lirc_release_device;
Sean Younga6ddd4f2017-09-26 09:34:47 -0400739 dev->lirc_dev.devt = MKDEV(MAJOR(lirc_base_dev), minor);
740 dev_set_name(&dev->lirc_dev, "lirc%d", minor);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300741
Sean Young7e45d662017-11-02 17:21:13 -0400742 INIT_LIST_HEAD(&dev->lirc_fh);
743 spin_lock_init(&dev->lirc_fh_lock);
744
Sean Younga6ddd4f2017-09-26 09:34:47 -0400745 cdev_init(&dev->lirc_cdev, &lirc_fops);
746
747 err = cdev_device_add(&dev->lirc_cdev, &dev->lirc_dev);
748 if (err)
749 goto out_ida;
750
751 get_device(&dev->dev);
752
Sean Younged8c34d2018-03-27 11:24:05 -0400753 switch (dev->driver_type) {
754 case RC_DRIVER_SCANCODE:
755 rx_type = "scancode";
756 break;
757 case RC_DRIVER_IR_RAW:
758 rx_type = "raw IR";
759 break;
760 default:
761 rx_type = "no";
762 break;
763 }
764
765 if (dev->tx_ir)
766 tx_type = "raw IR";
767 else
768 tx_type = "no";
769
770 dev_info(&dev->dev, "lirc_dev: driver %s registered at minor = %d, %s receiver, %s transmitter",
771 dev->driver_name, minor, rx_type, tx_type);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300772
773 return 0;
Sean Younga6ddd4f2017-09-26 09:34:47 -0400774
775out_ida:
776 ida_simple_remove(&lirc_ida, minor);
Sean Younga6ddd4f2017-09-26 09:34:47 -0400777 return err;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300778}
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300779
Sean Young75992a42020-08-24 21:10:45 +0200780void lirc_unregister(struct rc_dev *dev)
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300781{
Sean Young7e45d662017-11-02 17:21:13 -0400782 unsigned long flags;
783 struct lirc_fh *fh;
784
Sean Younga6ddd4f2017-09-26 09:34:47 -0400785 dev_dbg(&dev->dev, "lirc_dev: driver %s unregistered from minor = %d\n",
786 dev->driver_name, MINOR(dev->lirc_dev.devt));
Sean Young71695af2017-09-23 14:44:18 -0400787
Sean Young7e45d662017-11-02 17:21:13 -0400788 spin_lock_irqsave(&dev->lirc_fh_lock, flags);
789 list_for_each_entry(fh, &dev->lirc_fh, list)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800790 wake_up_poll(&fh->wait_poll, EPOLLHUP | EPOLLERR);
Sean Young7e45d662017-11-02 17:21:13 -0400791 spin_unlock_irqrestore(&dev->lirc_fh_lock, flags);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300792
Sean Younga6ddd4f2017-09-26 09:34:47 -0400793 cdev_device_del(&dev->lirc_cdev, &dev->lirc_dev);
794 ida_simple_remove(&lirc_ida, MINOR(dev->lirc_dev.devt));
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300795}
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300796
Sean Younga60d64b2017-09-23 10:41:13 -0400797int __init lirc_dev_init(void)
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300798{
799 int retval;
800
801 lirc_class = class_create(THIS_MODULE, "lirc");
802 if (IS_ERR(lirc_class)) {
Andi Shyti3fac0312016-07-06 06:01:16 -0300803 pr_err("class_create failed\n");
Andi Shyti54fceca2016-07-06 06:01:17 -0300804 return PTR_ERR(lirc_class);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300805 }
806
Sean Young32c3db32020-08-23 14:32:41 +0200807 retval = alloc_chrdev_region(&lirc_base_dev, 0, RC_DEV_MAX, "lirc");
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300808 if (retval) {
809 class_destroy(lirc_class);
Andi Shyti3fac0312016-07-06 06:01:16 -0300810 pr_err("alloc_chrdev_region failed\n");
Andi Shyti54fceca2016-07-06 06:01:17 -0300811 return retval;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300812 }
813
Sean Young5817b3d2018-02-13 06:11:35 -0500814 pr_debug("IR Remote Control driver registered, major %d\n",
815 MAJOR(lirc_base_dev));
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300816
Andi Shyti54fceca2016-07-06 06:01:17 -0300817 return 0;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300818}
819
Sean Younga60d64b2017-09-23 10:41:13 -0400820void __exit lirc_dev_exit(void)
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300821{
822 class_destroy(lirc_class);
Sean Younga6ddd4f2017-09-26 09:34:47 -0400823 unregister_chrdev_region(lirc_base_dev, RC_DEV_MAX);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300824}
825
Sean Youngf4364dc2018-05-27 12:24:09 +0100826struct rc_dev *rc_dev_get_from_fd(int fd)
827{
828 struct fd f = fdget(fd);
829 struct lirc_fh *fh;
830 struct rc_dev *dev;
831
832 if (!f.file)
833 return ERR_PTR(-EBADF);
834
835 if (f.file->f_op != &lirc_fops) {
836 fdput(f);
837 return ERR_PTR(-EINVAL);
838 }
839
840 fh = f.file->private_data;
841 dev = fh->rc;
842
843 get_device(&dev->dev);
844 fdput(f);
845
846 return dev;
847}
848
Sean Young04d0e8d2017-12-28 14:45:12 -0500849MODULE_ALIAS("lirc_dev");