blob: 247e6fc3dc0c7e041c170499803ac2dec1f38569 [file] [log] [blame]
Jarod Wilson4a62a5a2010-07-03 01:06:57 -03001/*
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 *
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030016 */
17
Andi Shyti3fac0312016-07-06 06:01:16 -030018#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030020#include <linux/module.h>
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030021#include <linux/mutex.h>
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030022#include <linux/device.h>
David Härdeman46c8f472017-06-25 09:32:05 -030023#include <linux/idr.h>
Sean Younga6ddd4f2017-09-26 09:34:47 -040024#include <linux/poll.h>
Sean Young42e04422017-11-02 16:39:16 -040025#include <linux/sched.h>
26#include <linux/wait.h>
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030027
Sean Younga60d64b2017-09-23 10:41:13 -040028#include "rc-core-priv.h"
Sean Youngaefb5e32017-11-02 16:44:21 -040029#include <uapi/linux/lirc.h>
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030030
Sean Young42e04422017-11-02 16:39:16 -040031#define LIRCBUF_SIZE 256
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030032
33static dev_t lirc_base_dev;
34
David Härdeman46c8f472017-06-25 09:32:05 -030035/* Used to keep track of allocated lirc devices */
David Härdeman46c8f472017-06-25 09:32:05 -030036static DEFINE_IDA(lirc_ida);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030037
38/* Only used for sysfs but defined to void otherwise */
39static struct class *lirc_class;
40
Sean Young42e04422017-11-02 16:39:16 -040041/**
42 * ir_lirc_raw_event() - Send raw IR data to lirc to be relayed to userspace
43 *
44 * @dev: the struct rc_dev descriptor of the device
45 * @ev: the struct ir_raw_event descriptor of the pulse/space
46 */
47void ir_lirc_raw_event(struct rc_dev *dev, struct ir_raw_event ev)
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030048{
Sean Young7e45d662017-11-02 17:21:13 -040049 unsigned long flags;
50 struct lirc_fh *fh;
Sean Young42e04422017-11-02 16:39:16 -040051 int sample;
Sean Younga607f512017-08-04 10:12:03 -040052
Sean Young42e04422017-11-02 16:39:16 -040053 /* Packet start */
54 if (ev.reset) {
55 /*
56 * Userspace expects a long space event before the start of
57 * the signal to use as a sync. This may be done with repeat
58 * packets and normal samples. But if a reset has been sent
59 * then we assume that a long time has passed, so we send a
60 * space with the maximum time value.
61 */
62 sample = LIRC_SPACE(LIRC_VALUE_MASK);
Sean Young1f17f682018-02-12 07:27:50 -050063 dev_dbg(&dev->dev, "delivering reset sync space to lirc_dev\n");
David Härdemanb15e3932017-06-25 09:32:36 -030064
Sean Young42e04422017-11-02 16:39:16 -040065 /* Carrier reports */
66 } else if (ev.carrier_report) {
67 sample = LIRC_FREQUENCY(ev.carrier);
Sean Young1f17f682018-02-12 07:27:50 -050068 dev_dbg(&dev->dev, "carrier report (freq: %d)\n", sample);
Sean Young42e04422017-11-02 16:39:16 -040069
70 /* Packet end */
71 } else if (ev.timeout) {
72 if (dev->gap)
73 return;
74
75 dev->gap_start = ktime_get();
76 dev->gap = true;
77 dev->gap_duration = ev.duration;
78
Sean Young42e04422017-11-02 16:39:16 -040079 sample = LIRC_TIMEOUT(ev.duration / 1000);
Sean Young1f17f682018-02-12 07:27:50 -050080 dev_dbg(&dev->dev, "timeout report (duration: %d)\n", sample);
Sean Young42e04422017-11-02 16:39:16 -040081
82 /* Normal sample */
83 } else {
84 if (dev->gap) {
85 dev->gap_duration += ktime_to_ns(ktime_sub(ktime_get(),
86 dev->gap_start));
87
88 /* Convert to ms and cap by LIRC_VALUE_MASK */
89 do_div(dev->gap_duration, 1000);
90 dev->gap_duration = min_t(u64, dev->gap_duration,
91 LIRC_VALUE_MASK);
92
Sean Young7e45d662017-11-02 17:21:13 -040093 spin_lock_irqsave(&dev->lirc_fh_lock, flags);
94 list_for_each_entry(fh, &dev->lirc_fh, list)
95 kfifo_put(&fh->rawir,
96 LIRC_SPACE(dev->gap_duration));
97 spin_unlock_irqrestore(&dev->lirc_fh_lock, flags);
Sean Young42e04422017-11-02 16:39:16 -040098 dev->gap = false;
99 }
100
101 sample = ev.pulse ? LIRC_PULSE(ev.duration / 1000) :
102 LIRC_SPACE(ev.duration / 1000);
Sean Young1f17f682018-02-12 07:27:50 -0500103 dev_dbg(&dev->dev, "delivering %uus %s to lirc_dev\n",
104 TO_US(ev.duration), TO_STR(ev.pulse));
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300105 }
Sean Young42e04422017-11-02 16:39:16 -0400106
Sean Young7e45d662017-11-02 17:21:13 -0400107 spin_lock_irqsave(&dev->lirc_fh_lock, flags);
108 list_for_each_entry(fh, &dev->lirc_fh, list) {
109 if (LIRC_IS_TIMEOUT(sample) && !fh->send_timeout_reports)
110 continue;
111 if (kfifo_put(&fh->rawir, sample))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800112 wake_up_poll(&fh->wait_poll, EPOLLIN | EPOLLRDNORM);
Sean Young7e45d662017-11-02 17:21:13 -0400113 }
114 spin_unlock_irqrestore(&dev->lirc_fh_lock, flags);
David Härdeman3381b772017-06-30 05:41:57 -0300115}
116
Sean Young42e04422017-11-02 16:39:16 -0400117/**
118 * ir_lirc_scancode_event() - Send scancode data to lirc to be relayed to
Sean Young7e45d662017-11-02 17:21:13 -0400119 * userspace. This can be called in atomic context.
Sean Young42e04422017-11-02 16:39:16 -0400120 * @dev: the struct rc_dev descriptor of the device
121 * @lsc: the struct lirc_scancode describing the decoded scancode
122 */
123void ir_lirc_scancode_event(struct rc_dev *dev, struct lirc_scancode *lsc)
David Härdeman3381b772017-06-30 05:41:57 -0300124{
Sean Young7e45d662017-11-02 17:21:13 -0400125 unsigned long flags;
126 struct lirc_fh *fh;
Sean Young74c839b2017-01-30 13:49:58 -0200127
Sean Young42e04422017-11-02 16:39:16 -0400128 lsc->timestamp = ktime_get_ns();
129
Sean Young7e45d662017-11-02 17:21:13 -0400130 spin_lock_irqsave(&dev->lirc_fh_lock, flags);
131 list_for_each_entry(fh, &dev->lirc_fh, list) {
132 if (kfifo_put(&fh->scancodes, *lsc))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800133 wake_up_poll(&fh->wait_poll, EPOLLIN | EPOLLRDNORM);
Andi Shyti6fa99e12016-07-06 06:01:13 -0300134 }
Sean Young7e45d662017-11-02 17:21:13 -0400135 spin_unlock_irqrestore(&dev->lirc_fh_lock, flags);
Andi Shyti6fa99e12016-07-06 06:01:13 -0300136}
Sean Young42e04422017-11-02 16:39:16 -0400137EXPORT_SYMBOL_GPL(ir_lirc_scancode_event);
Andi Shyti6fa99e12016-07-06 06:01:13 -0300138
Sean Young42e04422017-11-02 16:39:16 -0400139static int ir_lirc_open(struct inode *inode, struct file *file)
David Härdeman6ecccc32017-06-25 09:32:15 -0300140{
Sean Young42e04422017-11-02 16:39:16 -0400141 struct rc_dev *dev = container_of(inode->i_cdev, struct rc_dev,
142 lirc_cdev);
Sean Young7e45d662017-11-02 17:21:13 -0400143 struct lirc_fh *fh = kzalloc(sizeof(*fh), GFP_KERNEL);
144 unsigned long flags;
David Härdemande226ec2017-06-25 09:31:19 -0300145 int retval;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300146
Sean Young7e45d662017-11-02 17:21:13 -0400147 if (!fh)
148 return -ENOMEM;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300149
Sean Young7e45d662017-11-02 17:21:13 -0400150 get_device(&dev->dev);
David Härdeman3381b772017-06-30 05:41:57 -0300151
Sean Young42e04422017-11-02 16:39:16 -0400152 if (!dev->registered) {
David Härdeman3381b772017-06-30 05:41:57 -0300153 retval = -ENODEV;
Sean Young7e45d662017-11-02 17:21:13 -0400154 goto out_fh;
David Härdeman3381b772017-06-30 05:41:57 -0300155 }
156
Sean Young7e45d662017-11-02 17:21:13 -0400157 if (dev->driver_type == RC_DRIVER_IR_RAW) {
158 if (kfifo_alloc(&fh->rawir, MAX_IR_EVENT_SIZE, GFP_KERNEL)) {
159 retval = -ENOMEM;
160 goto out_fh;
161 }
David Härdeman3381b772017-06-30 05:41:57 -0300162 }
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300163
Sean Young7e45d662017-11-02 17:21:13 -0400164 if (dev->driver_type != RC_DRIVER_IR_RAW_TX) {
165 if (kfifo_alloc(&fh->scancodes, 32, GFP_KERNEL)) {
166 retval = -ENOMEM;
167 goto out_rawir;
168 }
Srinivas Kandagatlaca7a7222013-07-22 04:23:07 -0300169 }
170
Sean Young7e45d662017-11-02 17:21:13 -0400171 fh->send_mode = LIRC_MODE_PULSE;
172 fh->rc = dev;
173 fh->send_timeout_reports = true;
David Härdeman2c5a1f442017-05-01 13:03:46 -0300174
Sean Young7e45d662017-11-02 17:21:13 -0400175 if (dev->driver_type == RC_DRIVER_SCANCODE)
176 fh->rec_mode = LIRC_MODE_SCANCODE;
177 else
178 fh->rec_mode = LIRC_MODE_MODE2;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300179
Sean Young7e45d662017-11-02 17:21:13 -0400180 retval = rc_open(dev);
181 if (retval)
182 goto out_kfifo;
183
184 init_waitqueue_head(&fh->wait_poll);
185
186 file->private_data = fh;
187 spin_lock_irqsave(&dev->lirc_fh_lock, flags);
188 list_add(&fh->list, &dev->lirc_fh);
189 spin_unlock_irqrestore(&dev->lirc_fh_lock, flags);
Sean Young42e04422017-11-02 16:39:16 -0400190
Arnd Bergmannd9d2e9d2010-08-15 18:51:56 +0200191 nonseekable_open(inode, file);
192
David Härdemande226ec2017-06-25 09:31:19 -0300193 return 0;
Sean Young7e45d662017-11-02 17:21:13 -0400194out_kfifo:
195 if (dev->driver_type != RC_DRIVER_IR_RAW_TX)
196 kfifo_free(&fh->scancodes);
197out_rawir:
198 if (dev->driver_type == RC_DRIVER_IR_RAW)
199 kfifo_free(&fh->rawir);
200out_fh:
201 kfree(fh);
202 put_device(&dev->dev);
David Härdeman3381b772017-06-30 05:41:57 -0300203
David Härdeman3381b772017-06-30 05:41:57 -0300204 return retval;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300205}
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300206
Sean Young42e04422017-11-02 16:39:16 -0400207static int ir_lirc_close(struct inode *inode, struct file *file)
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300208{
Sean Young7e45d662017-11-02 17:21:13 -0400209 struct lirc_fh *fh = file->private_data;
210 struct rc_dev *dev = fh->rc;
211 unsigned long flags;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300212
Sean Young7e45d662017-11-02 17:21:13 -0400213 spin_lock_irqsave(&dev->lirc_fh_lock, flags);
214 list_del(&fh->list);
215 spin_unlock_irqrestore(&dev->lirc_fh_lock, flags);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300216
Sean Young7e45d662017-11-02 17:21:13 -0400217 if (dev->driver_type == RC_DRIVER_IR_RAW)
218 kfifo_free(&fh->rawir);
219 if (dev->driver_type != RC_DRIVER_IR_RAW_TX)
220 kfifo_free(&fh->scancodes);
221 kfree(fh);
David Härdeman3381b772017-06-30 05:41:57 -0300222
Sean Young42e04422017-11-02 16:39:16 -0400223 rc_close(dev);
Sean Young7e45d662017-11-02 17:21:13 -0400224 put_device(&dev->dev);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300225
226 return 0;
227}
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300228
Sean Young42e04422017-11-02 16:39:16 -0400229static ssize_t ir_lirc_transmit_ir(struct file *file, const char __user *buf,
230 size_t n, loff_t *ppos)
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300231{
Sean Young7e45d662017-11-02 17:21:13 -0400232 struct lirc_fh *fh = file->private_data;
233 struct rc_dev *dev = fh->rc;
Sean Younga74b2bf2017-12-13 16:09:21 -0500234 unsigned int *txbuf;
Sean Young42e04422017-11-02 16:39:16 -0400235 struct ir_raw_event *raw = NULL;
Sean Young49571332017-11-04 08:30:45 -0400236 ssize_t ret;
Sean Young42e04422017-11-02 16:39:16 -0400237 size_t count;
238 ktime_t start;
239 s64 towait;
240 unsigned int duration = 0; /* signal duration in us */
241 int i;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300242
Sean Young49571332017-11-04 08:30:45 -0400243 ret = mutex_lock_interruptible(&dev->lock);
244 if (ret)
245 return ret;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300246
Sean Young49571332017-11-04 08:30:45 -0400247 if (!dev->registered) {
248 ret = -ENODEV;
Sean Younga74b2bf2017-12-13 16:09:21 -0500249 goto out_unlock;
David Härdemanb15e3932017-06-25 09:32:36 -0300250 }
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300251
Sean Young42e04422017-11-02 16:39:16 -0400252 if (!dev->tx_ir) {
253 ret = -EINVAL;
Sean Younga74b2bf2017-12-13 16:09:21 -0500254 goto out_unlock;
Sean Young42e04422017-11-02 16:39:16 -0400255 }
256
Sean Young7e45d662017-11-02 17:21:13 -0400257 if (fh->send_mode == LIRC_MODE_SCANCODE) {
Sean Young42e04422017-11-02 16:39:16 -0400258 struct lirc_scancode scan;
259
Sean Young49571332017-11-04 08:30:45 -0400260 if (n != sizeof(scan)) {
261 ret = -EINVAL;
Sean Younga74b2bf2017-12-13 16:09:21 -0500262 goto out_unlock;
Sean Young49571332017-11-04 08:30:45 -0400263 }
Sean Young42e04422017-11-02 16:39:16 -0400264
Sean Young49571332017-11-04 08:30:45 -0400265 if (copy_from_user(&scan, buf, sizeof(scan))) {
266 ret = -EFAULT;
Sean Younga74b2bf2017-12-13 16:09:21 -0500267 goto out_unlock;
Sean Young49571332017-11-04 08:30:45 -0400268 }
Sean Young42e04422017-11-02 16:39:16 -0400269
Sean Young49571332017-11-04 08:30:45 -0400270 if (scan.flags || scan.keycode || scan.timestamp) {
271 ret = -EINVAL;
Sean Younga74b2bf2017-12-13 16:09:21 -0500272 goto out_unlock;
Sean Young49571332017-11-04 08:30:45 -0400273 }
Sean Young42e04422017-11-02 16:39:16 -0400274
275 /*
276 * The scancode field in lirc_scancode is 64-bit simply
277 * to future-proof it, since there are IR protocols encode
278 * use more than 32 bits. For now only 32-bit protocols
279 * are supported.
280 */
281 if (scan.scancode > U32_MAX ||
Sean Young49571332017-11-04 08:30:45 -0400282 !rc_validate_scancode(scan.rc_proto, scan.scancode)) {
283 ret = -EINVAL;
Sean Younga74b2bf2017-12-13 16:09:21 -0500284 goto out_unlock;
Sean Young49571332017-11-04 08:30:45 -0400285 }
Sean Young42e04422017-11-02 16:39:16 -0400286
287 raw = kmalloc_array(LIRCBUF_SIZE, sizeof(*raw), GFP_KERNEL);
Sean Young49571332017-11-04 08:30:45 -0400288 if (!raw) {
289 ret = -ENOMEM;
Sean Younga74b2bf2017-12-13 16:09:21 -0500290 goto out_unlock;
Sean Young49571332017-11-04 08:30:45 -0400291 }
Sean Young42e04422017-11-02 16:39:16 -0400292
293 ret = ir_raw_encode_scancode(scan.rc_proto, scan.scancode,
294 raw, LIRCBUF_SIZE);
295 if (ret < 0)
Colin Ian King8d25e152017-12-19 11:48:25 -0500296 goto out_kfree_raw;
Sean Young42e04422017-11-02 16:39:16 -0400297
298 count = ret;
299
300 txbuf = kmalloc_array(count, sizeof(unsigned int), GFP_KERNEL);
301 if (!txbuf) {
302 ret = -ENOMEM;
Colin Ian King8d25e152017-12-19 11:48:25 -0500303 goto out_kfree_raw;
Sean Young42e04422017-11-02 16:39:16 -0400304 }
305
306 for (i = 0; i < count; i++)
307 /* Convert from NS to US */
308 txbuf[i] = DIV_ROUND_UP(raw[i].duration, 1000);
309
310 if (dev->s_tx_carrier) {
311 int carrier = ir_raw_encode_carrier(scan.rc_proto);
312
313 if (carrier > 0)
314 dev->s_tx_carrier(dev, carrier);
315 }
316 } else {
Sean Young49571332017-11-04 08:30:45 -0400317 if (n < sizeof(unsigned int) || n % sizeof(unsigned int)) {
318 ret = -EINVAL;
Sean Younga74b2bf2017-12-13 16:09:21 -0500319 goto out_unlock;
Sean Young49571332017-11-04 08:30:45 -0400320 }
Sean Young42e04422017-11-02 16:39:16 -0400321
322 count = n / sizeof(unsigned int);
Sean Young49571332017-11-04 08:30:45 -0400323 if (count > LIRCBUF_SIZE || count % 2 == 0) {
324 ret = -EINVAL;
Sean Younga74b2bf2017-12-13 16:09:21 -0500325 goto out_unlock;
Sean Young49571332017-11-04 08:30:45 -0400326 }
Sean Young42e04422017-11-02 16:39:16 -0400327
328 txbuf = memdup_user(buf, n);
Sean Young49571332017-11-04 08:30:45 -0400329 if (IS_ERR(txbuf)) {
330 ret = PTR_ERR(txbuf);
Sean Younga74b2bf2017-12-13 16:09:21 -0500331 goto out_unlock;
Sean Young49571332017-11-04 08:30:45 -0400332 }
Sean Young42e04422017-11-02 16:39:16 -0400333 }
334
335 for (i = 0; i < count; i++) {
336 if (txbuf[i] > IR_MAX_DURATION / 1000 - duration || !txbuf[i]) {
337 ret = -EINVAL;
Sean Younga74b2bf2017-12-13 16:09:21 -0500338 goto out_kfree;
Sean Young42e04422017-11-02 16:39:16 -0400339 }
340
341 duration += txbuf[i];
342 }
343
Sean Young29422732018-02-12 09:00:28 -0500344 start = ktime_get();
345
Sean Young42e04422017-11-02 16:39:16 -0400346 ret = dev->tx_ir(dev, txbuf, count);
347 if (ret < 0)
Sean Younga74b2bf2017-12-13 16:09:21 -0500348 goto out_kfree;
Sean Young42e04422017-11-02 16:39:16 -0400349
Sean Youngf81a8152017-12-13 16:30:22 -0500350 kfree(txbuf);
351 kfree(raw);
352 mutex_unlock(&dev->lock);
353
Sean Youngdde7edf2017-12-11 17:12:09 -0500354 /*
355 * The lircd gap calculation expects the write function to
356 * wait for the actual IR signal to be transmitted before
357 * returning.
358 */
359 towait = ktime_us_delta(ktime_add_us(start, duration),
360 ktime_get());
361 if (towait > 0) {
362 set_current_state(TASK_INTERRUPTIBLE);
363 schedule_timeout(usecs_to_jiffies(towait));
Sean Young42e04422017-11-02 16:39:16 -0400364 }
365
Sean Youngf81a8152017-12-13 16:30:22 -0500366 return n;
Sean Younga74b2bf2017-12-13 16:09:21 -0500367out_kfree:
Sean Young42e04422017-11-02 16:39:16 -0400368 kfree(txbuf);
Colin Ian King8d25e152017-12-19 11:48:25 -0500369out_kfree_raw:
Sean Young42e04422017-11-02 16:39:16 -0400370 kfree(raw);
Sean Younga74b2bf2017-12-13 16:09:21 -0500371out_unlock:
372 mutex_unlock(&dev->lock);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300373 return ret;
374}
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300375
Sean Young7e45d662017-11-02 17:21:13 -0400376static long ir_lirc_ioctl(struct file *file, unsigned int cmd,
Sean Young42e04422017-11-02 16:39:16 -0400377 unsigned long arg)
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300378{
Sean Young7e45d662017-11-02 17:21:13 -0400379 struct lirc_fh *fh = file->private_data;
380 struct rc_dev *dev = fh->rc;
Sean Young42e04422017-11-02 16:39:16 -0400381 u32 __user *argp = (u32 __user *)(arg);
Sean Young49571332017-11-04 08:30:45 -0400382 u32 val = 0;
383 int ret;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300384
Sean Young42e04422017-11-02 16:39:16 -0400385 if (_IOC_DIR(cmd) & _IOC_WRITE) {
386 ret = get_user(val, argp);
387 if (ret)
388 return ret;
389 }
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300390
Sean Young49571332017-11-04 08:30:45 -0400391 ret = mutex_lock_interruptible(&dev->lock);
392 if (ret)
393 return ret;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300394
Sean Young49571332017-11-04 08:30:45 -0400395 if (!dev->registered) {
396 ret = -ENODEV;
David Härdeman3381b772017-06-30 05:41:57 -0300397 goto out;
398 }
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300399
400 switch (cmd) {
401 case LIRC_GET_FEATURES:
Sean Young42e04422017-11-02 16:39:16 -0400402 if (dev->driver_type == RC_DRIVER_SCANCODE)
403 val |= LIRC_CAN_REC_SCANCODE;
404
405 if (dev->driver_type == RC_DRIVER_IR_RAW) {
Sean Young02d742f2017-12-28 14:58:26 -0500406 val |= LIRC_CAN_REC_MODE2;
Sean Young42e04422017-11-02 16:39:16 -0400407 if (dev->rx_resolution)
408 val |= LIRC_CAN_GET_REC_RESOLUTION;
409 }
410
411 if (dev->tx_ir) {
Sean Young02d742f2017-12-28 14:58:26 -0500412 val |= LIRC_CAN_SEND_PULSE;
Sean Young42e04422017-11-02 16:39:16 -0400413 if (dev->s_tx_mask)
414 val |= LIRC_CAN_SET_TRANSMITTER_MASK;
415 if (dev->s_tx_carrier)
416 val |= LIRC_CAN_SET_SEND_CARRIER;
417 if (dev->s_tx_duty_cycle)
418 val |= LIRC_CAN_SET_SEND_DUTY_CYCLE;
419 }
420
421 if (dev->s_rx_carrier_range)
422 val |= LIRC_CAN_SET_REC_CARRIER |
423 LIRC_CAN_SET_REC_CARRIER_RANGE;
424
425 if (dev->s_learning_mode)
426 val |= LIRC_CAN_USE_WIDEBAND_RECEIVER;
427
428 if (dev->s_carrier_report)
429 val |= LIRC_CAN_MEASURE_CARRIER;
430
431 if (dev->max_timeout)
432 val |= LIRC_CAN_SET_REC_TIMEOUT;
433
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300434 break;
Sean Young42e04422017-11-02 16:39:16 -0400435
436 /* mode support */
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300437 case LIRC_GET_REC_MODE:
Sean Young42e04422017-11-02 16:39:16 -0400438 if (dev->driver_type == RC_DRIVER_IR_RAW_TX)
Sean Young49571332017-11-04 08:30:45 -0400439 ret = -ENOTTY;
440 else
441 val = fh->rec_mode;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300442 break;
Sean Young42e04422017-11-02 16:39:16 -0400443
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300444 case LIRC_SET_REC_MODE:
Sean Young42e04422017-11-02 16:39:16 -0400445 switch (dev->driver_type) {
446 case RC_DRIVER_IR_RAW_TX:
Sean Young49571332017-11-04 08:30:45 -0400447 ret = -ENOTTY;
448 break;
Sean Young42e04422017-11-02 16:39:16 -0400449 case RC_DRIVER_SCANCODE:
450 if (val != LIRC_MODE_SCANCODE)
Sean Young49571332017-11-04 08:30:45 -0400451 ret = -EINVAL;
Sean Young42e04422017-11-02 16:39:16 -0400452 break;
453 case RC_DRIVER_IR_RAW:
454 if (!(val == LIRC_MODE_MODE2 ||
455 val == LIRC_MODE_SCANCODE))
Sean Young49571332017-11-04 08:30:45 -0400456 ret = -EINVAL;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300457 break;
458 }
459
Sean Young49571332017-11-04 08:30:45 -0400460 if (!ret)
461 fh->rec_mode = val;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300462 break;
Sean Young42e04422017-11-02 16:39:16 -0400463
464 case LIRC_GET_SEND_MODE:
465 if (!dev->tx_ir)
Sean Young49571332017-11-04 08:30:45 -0400466 ret = -ENOTTY;
467 else
468 val = fh->send_mode;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300469 break;
Sean Young42e04422017-11-02 16:39:16 -0400470
471 case LIRC_SET_SEND_MODE:
472 if (!dev->tx_ir)
Sean Young49571332017-11-04 08:30:45 -0400473 ret = -ENOTTY;
474 else if (!(val == LIRC_MODE_PULSE || val == LIRC_MODE_SCANCODE))
475 ret = -EINVAL;
476 else
477 fh->send_mode = val;
478 break;
Sean Young42e04422017-11-02 16:39:16 -0400479
480 /* TX settings */
481 case LIRC_SET_TRANSMITTER_MASK:
482 if (!dev->s_tx_mask)
Sean Young49571332017-11-04 08:30:45 -0400483 ret = -ENOTTY;
484 else
485 ret = dev->s_tx_mask(dev, val);
486 break;
Sean Young42e04422017-11-02 16:39:16 -0400487
488 case LIRC_SET_SEND_CARRIER:
489 if (!dev->s_tx_carrier)
Sean Young49571332017-11-04 08:30:45 -0400490 ret = -ENOTTY;
491 else
492 ret = dev->s_tx_carrier(dev, val);
493 break;
Sean Young42e04422017-11-02 16:39:16 -0400494
495 case LIRC_SET_SEND_DUTY_CYCLE:
496 if (!dev->s_tx_duty_cycle)
Sean Young49571332017-11-04 08:30:45 -0400497 ret = -ENOTTY;
498 else if (val <= 0 || val >= 100)
499 ret = -EINVAL;
500 else
501 ret = dev->s_tx_duty_cycle(dev, val);
502 break;
Sean Young42e04422017-11-02 16:39:16 -0400503
504 /* RX settings */
505 case LIRC_SET_REC_CARRIER:
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 ret = dev->s_rx_carrier_range(dev, fh->carrier_low,
512 val);
513 break;
Sean Young42e04422017-11-02 16:39:16 -0400514
515 case LIRC_SET_REC_CARRIER_RANGE:
516 if (!dev->s_rx_carrier_range)
Sean Young49571332017-11-04 08:30:45 -0400517 ret = -ENOTTY;
518 else if (val <= 0)
519 ret = -EINVAL;
520 else
521 fh->carrier_low = val;
522 break;
Sean Young42e04422017-11-02 16:39:16 -0400523
524 case LIRC_GET_REC_RESOLUTION:
525 if (!dev->rx_resolution)
Sean Young49571332017-11-04 08:30:45 -0400526 ret = -ENOTTY;
527 else
528 val = dev->rx_resolution / 1000;
Sean Young42e04422017-11-02 16:39:16 -0400529 break;
530
531 case LIRC_SET_WIDEBAND_RECEIVER:
532 if (!dev->s_learning_mode)
Sean Young49571332017-11-04 08:30:45 -0400533 ret = -ENOTTY;
534 else
535 ret = dev->s_learning_mode(dev, !!val);
536 break;
Sean Young42e04422017-11-02 16:39:16 -0400537
538 case LIRC_SET_MEASURE_CARRIER_MODE:
539 if (!dev->s_carrier_report)
Sean Young49571332017-11-04 08:30:45 -0400540 ret = -ENOTTY;
541 else
542 ret = dev->s_carrier_report(dev, !!val);
543 break;
Sean Young42e04422017-11-02 16:39:16 -0400544
545 /* Generic timeout support */
546 case LIRC_GET_MIN_TIMEOUT:
547 if (!dev->max_timeout)
Sean Young49571332017-11-04 08:30:45 -0400548 ret = -ENOTTY;
549 else
550 val = DIV_ROUND_UP(dev->min_timeout, 1000);
Sean Young42e04422017-11-02 16:39:16 -0400551 break;
552
553 case LIRC_GET_MAX_TIMEOUT:
554 if (!dev->max_timeout)
Sean Young49571332017-11-04 08:30:45 -0400555 ret = -ENOTTY;
556 else
557 val = dev->max_timeout / 1000;
Sean Young42e04422017-11-02 16:39:16 -0400558 break;
559
560 case LIRC_SET_REC_TIMEOUT:
Sean Young49571332017-11-04 08:30:45 -0400561 if (!dev->max_timeout) {
562 ret = -ENOTTY;
563 } else if (val > U32_MAX / 1000) {
564 /* Check for multiply overflow */
565 ret = -EINVAL;
566 } else {
567 u32 tmp = val * 1000;
Sean Young42e04422017-11-02 16:39:16 -0400568
Sean Young49571332017-11-04 08:30:45 -0400569 if (tmp < dev->min_timeout || tmp > dev->max_timeout)
570 ret = -EINVAL;
571 else if (dev->s_timeout)
572 ret = dev->s_timeout(dev, tmp);
Sean Youngc2837ad2018-02-12 08:59:00 -0500573 else
Sean Young49571332017-11-04 08:30:45 -0400574 dev->timeout = tmp;
575 }
Sean Young42e04422017-11-02 16:39:16 -0400576 break;
577
578 case LIRC_SET_REC_TIMEOUT_REPORTS:
579 if (!dev->timeout)
Sean Young49571332017-11-04 08:30:45 -0400580 ret = -ENOTTY;
581 else
582 fh->send_timeout_reports = !!val;
Sean Young42e04422017-11-02 16:39:16 -0400583 break;
584
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300585 default:
Sean Young49571332017-11-04 08:30:45 -0400586 ret = -ENOTTY;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300587 }
588
Sean Young49571332017-11-04 08:30:45 -0400589 if (!ret && _IOC_DIR(cmd) & _IOC_READ)
Sean Young42e04422017-11-02 16:39:16 -0400590 ret = put_user(val, argp);
591
David Härdeman3381b772017-06-30 05:41:57 -0300592out:
Sean Young49571332017-11-04 08:30:45 -0400593 mutex_unlock(&dev->lock);
Sean Young42e04422017-11-02 16:39:16 -0400594 return ret;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300595}
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300596
Linus Torvalds68c57352018-02-06 11:27:48 -0800597static __poll_t ir_lirc_poll(struct file *file, struct poll_table_struct *wait)
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300598{
Sean Young7e45d662017-11-02 17:21:13 -0400599 struct lirc_fh *fh = file->private_data;
600 struct rc_dev *rcdev = fh->rc;
Linus Torvalds68c57352018-02-06 11:27:48 -0800601 __poll_t events = 0;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300602
Sean Young7e45d662017-11-02 17:21:13 -0400603 poll_wait(file, &fh->wait_poll, wait);
Jarod Wilson715d29a72010-10-18 12:02:01 -0300604
Sean Young42e04422017-11-02 16:39:16 -0400605 if (!rcdev->registered) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800606 events = EPOLLHUP | EPOLLERR;
Sean Young42e04422017-11-02 16:39:16 -0400607 } else if (rcdev->driver_type != RC_DRIVER_IR_RAW_TX) {
Sean Young7e45d662017-11-02 17:21:13 -0400608 if (fh->rec_mode == LIRC_MODE_SCANCODE &&
609 !kfifo_is_empty(&fh->scancodes))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800610 events = EPOLLIN | EPOLLRDNORM;
David Härdemanb15e3932017-06-25 09:32:36 -0300611
Sean Young7e45d662017-11-02 17:21:13 -0400612 if (fh->rec_mode == LIRC_MODE_MODE2 &&
613 !kfifo_is_empty(&fh->rawir))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800614 events = EPOLLIN | EPOLLRDNORM;
Dan Carpenter250f7a52010-11-17 02:20:15 -0300615 }
David Härdeman3381b772017-06-30 05:41:57 -0300616
Sean Young42e04422017-11-02 16:39:16 -0400617 return events;
618}
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300619
Sean Young42e04422017-11-02 16:39:16 -0400620static ssize_t ir_lirc_read_mode2(struct file *file, char __user *buffer,
621 size_t length)
622{
Sean Young7e45d662017-11-02 17:21:13 -0400623 struct lirc_fh *fh = file->private_data;
624 struct rc_dev *rcdev = fh->rc;
Sean Young42e04422017-11-02 16:39:16 -0400625 unsigned int copied;
626 int ret;
David Härdeman3381b772017-06-30 05:41:57 -0300627
Sean Young42e04422017-11-02 16:39:16 -0400628 if (length < sizeof(unsigned int) || length % sizeof(unsigned int))
629 return -EINVAL;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300630
Sean Young42e04422017-11-02 16:39:16 -0400631 do {
Sean Young7e45d662017-11-02 17:21:13 -0400632 if (kfifo_is_empty(&fh->rawir)) {
Sean Young42e04422017-11-02 16:39:16 -0400633 if (file->f_flags & O_NONBLOCK)
634 return -EAGAIN;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300635
Sean Young7e45d662017-11-02 17:21:13 -0400636 ret = wait_event_interruptible(fh->wait_poll,
637 !kfifo_is_empty(&fh->rawir) ||
Sean Young42e04422017-11-02 16:39:16 -0400638 !rcdev->registered);
639 if (ret)
640 return ret;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300641 }
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300642
Sean Young42e04422017-11-02 16:39:16 -0400643 if (!rcdev->registered)
644 return -ENODEV;
Dan Carpenter250f7a52010-11-17 02:20:15 -0300645
Sean Young42e04422017-11-02 16:39:16 -0400646 ret = mutex_lock_interruptible(&rcdev->lock);
647 if (ret)
648 return ret;
Sean Young7e45d662017-11-02 17:21:13 -0400649 ret = kfifo_to_user(&fh->rawir, buffer, length, &copied);
Sean Young42e04422017-11-02 16:39:16 -0400650 mutex_unlock(&rcdev->lock);
651 if (ret)
652 return ret;
653 } while (copied == 0);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300654
Sean Young42e04422017-11-02 16:39:16 -0400655 return copied;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300656}
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300657
Sean Young42e04422017-11-02 16:39:16 -0400658static ssize_t ir_lirc_read_scancode(struct file *file, char __user *buffer,
659 size_t length)
David Härdeman615cd3f2017-06-25 09:31:40 -0300660{
Sean Young7e45d662017-11-02 17:21:13 -0400661 struct lirc_fh *fh = file->private_data;
662 struct rc_dev *rcdev = fh->rc;
Sean Young42e04422017-11-02 16:39:16 -0400663 unsigned int copied;
664 int ret;
David Härdeman615cd3f2017-06-25 09:31:40 -0300665
Sean Young42e04422017-11-02 16:39:16 -0400666 if (length < sizeof(struct lirc_scancode) ||
667 length % sizeof(struct lirc_scancode))
668 return -EINVAL;
669
670 do {
Sean Young7e45d662017-11-02 17:21:13 -0400671 if (kfifo_is_empty(&fh->scancodes)) {
Sean Young42e04422017-11-02 16:39:16 -0400672 if (file->f_flags & O_NONBLOCK)
673 return -EAGAIN;
674
Sean Young7e45d662017-11-02 17:21:13 -0400675 ret = wait_event_interruptible(fh->wait_poll,
676 !kfifo_is_empty(&fh->scancodes) ||
Sean Young42e04422017-11-02 16:39:16 -0400677 !rcdev->registered);
678 if (ret)
679 return ret;
680 }
681
682 if (!rcdev->registered)
683 return -ENODEV;
684
685 ret = mutex_lock_interruptible(&rcdev->lock);
686 if (ret)
687 return ret;
Sean Young7e45d662017-11-02 17:21:13 -0400688 ret = kfifo_to_user(&fh->scancodes, buffer, length, &copied);
Sean Young42e04422017-11-02 16:39:16 -0400689 mutex_unlock(&rcdev->lock);
690 if (ret)
691 return ret;
692 } while (copied == 0);
693
694 return copied;
David Härdeman615cd3f2017-06-25 09:31:40 -0300695}
David Härdeman615cd3f2017-06-25 09:31:40 -0300696
Sean Young42e04422017-11-02 16:39:16 -0400697static ssize_t ir_lirc_read(struct file *file, char __user *buffer,
698 size_t length, loff_t *ppos)
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300699{
Sean Young7e45d662017-11-02 17:21:13 -0400700 struct lirc_fh *fh = file->private_data;
701 struct rc_dev *rcdev = fh->rc;
David Härdeman615cd3f2017-06-25 09:31:40 -0300702
Sean Young42e04422017-11-02 16:39:16 -0400703 if (rcdev->driver_type == RC_DRIVER_IR_RAW_TX)
704 return -EINVAL;
705
706 if (!rcdev->registered)
707 return -ENODEV;
708
Sean Young7e45d662017-11-02 17:21:13 -0400709 if (fh->rec_mode == LIRC_MODE_MODE2)
Sean Young42e04422017-11-02 16:39:16 -0400710 return ir_lirc_read_mode2(file, buffer, length);
711 else /* LIRC_MODE_SCANCODE */
712 return ir_lirc_read_scancode(file, buffer, length);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300713}
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300714
Sean Young42e04422017-11-02 16:39:16 -0400715static const struct file_operations lirc_fops = {
716 .owner = THIS_MODULE,
717 .write = ir_lirc_transmit_ir,
718 .unlocked_ioctl = ir_lirc_ioctl,
719#ifdef CONFIG_COMPAT
720 .compat_ioctl = ir_lirc_ioctl,
721#endif
722 .read = ir_lirc_read,
723 .poll = ir_lirc_poll,
724 .open = ir_lirc_open,
725 .release = ir_lirc_close,
726 .llseek = no_llseek,
727};
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300728
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300729static void lirc_release_device(struct device *ld)
730{
Sean Younga6ddd4f2017-09-26 09:34:47 -0400731 struct rc_dev *rcdev = container_of(ld, struct rc_dev, lirc_dev);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300732
Sean Younga6ddd4f2017-09-26 09:34:47 -0400733 put_device(&rcdev->dev);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300734}
735
Sean Younga6ddd4f2017-09-26 09:34:47 -0400736int ir_lirc_register(struct rc_dev *dev)
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300737{
Sean Younged8c34d2018-03-27 11:24:05 -0400738 const char *rx_type, *tx_type;
Sean Younga6ddd4f2017-09-26 09:34:47 -0400739 int err, minor;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300740
Sean Young7e45d662017-11-02 17:21:13 -0400741 minor = ida_simple_get(&lirc_ida, 0, RC_DEV_MAX, GFP_KERNEL);
742 if (minor < 0)
743 return minor;
744
Sean Younga6ddd4f2017-09-26 09:34:47 -0400745 device_initialize(&dev->lirc_dev);
746 dev->lirc_dev.class = lirc_class;
Sean Younga6ddd4f2017-09-26 09:34:47 -0400747 dev->lirc_dev.parent = &dev->dev;
Sean Young7e45d662017-11-02 17:21:13 -0400748 dev->lirc_dev.release = lirc_release_device;
Sean Younga6ddd4f2017-09-26 09:34:47 -0400749 dev->lirc_dev.devt = MKDEV(MAJOR(lirc_base_dev), minor);
750 dev_set_name(&dev->lirc_dev, "lirc%d", minor);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300751
Sean Young7e45d662017-11-02 17:21:13 -0400752 INIT_LIST_HEAD(&dev->lirc_fh);
753 spin_lock_init(&dev->lirc_fh_lock);
754
Sean Younga6ddd4f2017-09-26 09:34:47 -0400755 cdev_init(&dev->lirc_cdev, &lirc_fops);
756
757 err = cdev_device_add(&dev->lirc_cdev, &dev->lirc_dev);
758 if (err)
759 goto out_ida;
760
761 get_device(&dev->dev);
762
Sean Younged8c34d2018-03-27 11:24:05 -0400763 switch (dev->driver_type) {
764 case RC_DRIVER_SCANCODE:
765 rx_type = "scancode";
766 break;
767 case RC_DRIVER_IR_RAW:
768 rx_type = "raw IR";
769 break;
770 default:
771 rx_type = "no";
772 break;
773 }
774
775 if (dev->tx_ir)
776 tx_type = "raw IR";
777 else
778 tx_type = "no";
779
780 dev_info(&dev->dev, "lirc_dev: driver %s registered at minor = %d, %s receiver, %s transmitter",
781 dev->driver_name, minor, rx_type, tx_type);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300782
783 return 0;
Sean Younga6ddd4f2017-09-26 09:34:47 -0400784
785out_ida:
786 ida_simple_remove(&lirc_ida, minor);
Sean Younga6ddd4f2017-09-26 09:34:47 -0400787 return err;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300788}
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300789
Sean Younga6ddd4f2017-09-26 09:34:47 -0400790void ir_lirc_unregister(struct rc_dev *dev)
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300791{
Sean Young7e45d662017-11-02 17:21:13 -0400792 unsigned long flags;
793 struct lirc_fh *fh;
794
Sean Younga6ddd4f2017-09-26 09:34:47 -0400795 dev_dbg(&dev->dev, "lirc_dev: driver %s unregistered from minor = %d\n",
796 dev->driver_name, MINOR(dev->lirc_dev.devt));
Sean Young71695af2017-09-23 14:44:18 -0400797
Sean Young7e45d662017-11-02 17:21:13 -0400798 spin_lock_irqsave(&dev->lirc_fh_lock, flags);
799 list_for_each_entry(fh, &dev->lirc_fh, list)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800800 wake_up_poll(&fh->wait_poll, EPOLLHUP | EPOLLERR);
Sean Young7e45d662017-11-02 17:21:13 -0400801 spin_unlock_irqrestore(&dev->lirc_fh_lock, flags);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300802
Sean Younga6ddd4f2017-09-26 09:34:47 -0400803 cdev_device_del(&dev->lirc_cdev, &dev->lirc_dev);
804 ida_simple_remove(&lirc_ida, MINOR(dev->lirc_dev.devt));
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300805}
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300806
Sean Younga60d64b2017-09-23 10:41:13 -0400807int __init lirc_dev_init(void)
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300808{
809 int retval;
810
811 lirc_class = class_create(THIS_MODULE, "lirc");
812 if (IS_ERR(lirc_class)) {
Andi Shyti3fac0312016-07-06 06:01:16 -0300813 pr_err("class_create failed\n");
Andi Shyti54fceca2016-07-06 06:01:17 -0300814 return PTR_ERR(lirc_class);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300815 }
816
Sean Younga6ddd4f2017-09-26 09:34:47 -0400817 retval = alloc_chrdev_region(&lirc_base_dev, 0, RC_DEV_MAX,
David Härdeman463015d2017-05-01 13:04:47 -0300818 "BaseRemoteCtl");
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300819 if (retval) {
820 class_destroy(lirc_class);
Andi Shyti3fac0312016-07-06 06:01:16 -0300821 pr_err("alloc_chrdev_region failed\n");
Andi Shyti54fceca2016-07-06 06:01:17 -0300822 return retval;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300823 }
824
Sean Young5817b3d2018-02-13 06:11:35 -0500825 pr_debug("IR Remote Control driver registered, major %d\n",
826 MAJOR(lirc_base_dev));
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300827
Andi Shyti54fceca2016-07-06 06:01:17 -0300828 return 0;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300829}
830
Sean Younga60d64b2017-09-23 10:41:13 -0400831void __exit lirc_dev_exit(void)
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300832{
833 class_destroy(lirc_class);
Sean Younga6ddd4f2017-09-26 09:34:47 -0400834 unregister_chrdev_region(lirc_base_dev, RC_DEV_MAX);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300835}
836
Sean Young04d0e8d2017-12-28 14:45:12 -0500837MODULE_ALIAS("lirc_dev");