blob: 8618aba152c6d03e6c7fd6303602a56ea7751a42 [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)
48{
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;
52
53 /* 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);
63 IR_dprintk(2, "delivering reset sync space to lirc_dev\n");
64
65 /* Carrier reports */
66 } else if (ev.carrier_report) {
67 sample = LIRC_FREQUENCY(ev.carrier);
68 IR_dprintk(2, "carrier report (freq: %d)\n", sample);
69
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);
80 IR_dprintk(2, "timeout report (duration: %d)\n", sample);
81
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);
103 IR_dprintk(2, "delivering %uus %s to lirc_dev\n",
104 TO_US(ev.duration), TO_STR(ev.pulse));
105 }
106
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))
112 wake_up_poll(&fh->wait_poll, POLLIN | POLLRDNORM);
113 }
114 spin_unlock_irqrestore(&dev->lirc_fh_lock, flags);
Sean Young42e04422017-11-02 16:39:16 -0400115}
116
117/**
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)
124{
Sean Young7e45d662017-11-02 17:21:13 -0400125 unsigned long flags;
126 struct lirc_fh *fh;
127
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))
133 wake_up_poll(&fh->wait_poll, POLLIN | POLLRDNORM);
134 }
135 spin_unlock_irqrestore(&dev->lirc_fh_lock, flags);
Sean Young42e04422017-11-02 16:39:16 -0400136}
137EXPORT_SYMBOL_GPL(ir_lirc_scancode_event);
138
139static int ir_lirc_open(struct inode *inode, struct file *file)
140{
141 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;
Sean Young42e04422017-11-02 16:39:16 -0400145 int retval;
146
Sean Young7e45d662017-11-02 17:21:13 -0400147 if (!fh)
148 return -ENOMEM;
Sean Young42e04422017-11-02 16:39:16 -0400149
Sean Young7e45d662017-11-02 17:21:13 -0400150 get_device(&dev->dev);
Sean Young42e04422017-11-02 16:39:16 -0400151
152 if (!dev->registered) {
153 retval = -ENODEV;
Sean Young7e45d662017-11-02 17:21:13 -0400154 goto out_fh;
Sean Young42e04422017-11-02 16:39:16 -0400155 }
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 }
Sean Young42e04422017-11-02 16:39:16 -0400162 }
163
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 }
169 }
Sean Young42e04422017-11-02 16:39:16 -0400170
Sean Young7e45d662017-11-02 17:21:13 -0400171 fh->send_mode = LIRC_MODE_PULSE;
172 fh->rc = dev;
173 fh->send_timeout_reports = true;
174
175 if (dev->driver_type == RC_DRIVER_SCANCODE)
176 fh->rec_mode = LIRC_MODE_SCANCODE;
177 else
178 fh->rec_mode = LIRC_MODE_MODE2;
179
180 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
191 nonseekable_open(inode, file);
Sean Young42e04422017-11-02 16:39:16 -0400192
193 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);
Sean Young42e04422017-11-02 16:39:16 -0400203
Sean Young42e04422017-11-02 16:39:16 -0400204 return retval;
205}
206
207static int ir_lirc_close(struct inode *inode, struct file *file)
208{
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;
Sean Young42e04422017-11-02 16:39:16 -0400212
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);
216
217 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);
Sean Young42e04422017-11-02 16:39:16 -0400222
223 rc_close(dev);
Sean Young7e45d662017-11-02 17:21:13 -0400224 put_device(&dev->dev);
Sean Young42e04422017-11-02 16:39:16 -0400225
226 return 0;
227}
228
229static ssize_t ir_lirc_transmit_ir(struct file *file, const char __user *buf,
230 size_t n, loff_t *ppos)
231{
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;
242
Sean Young49571332017-11-04 08:30:45 -0400243 ret = mutex_lock_interruptible(&dev->lock);
244 if (ret)
245 return ret;
246
247 if (!dev->registered) {
248 ret = -ENODEV;
Sean Younga74b2bf2017-12-13 16:09:21 -0500249 goto out_unlock;
Sean Young49571332017-11-04 08:30:45 -0400250 }
Sean Young42e04422017-11-02 16:39:16 -0400251
252 start = ktime_get();
253
254 if (!dev->tx_ir) {
255 ret = -EINVAL;
Sean Younga74b2bf2017-12-13 16:09:21 -0500256 goto out_unlock;
Sean Young42e04422017-11-02 16:39:16 -0400257 }
258
Sean Young7e45d662017-11-02 17:21:13 -0400259 if (fh->send_mode == LIRC_MODE_SCANCODE) {
Sean Young42e04422017-11-02 16:39:16 -0400260 struct lirc_scancode scan;
261
Sean Young49571332017-11-04 08:30:45 -0400262 if (n != sizeof(scan)) {
263 ret = -EINVAL;
Sean Younga74b2bf2017-12-13 16:09:21 -0500264 goto out_unlock;
Sean Young49571332017-11-04 08:30:45 -0400265 }
Sean Young42e04422017-11-02 16:39:16 -0400266
Sean Young49571332017-11-04 08:30:45 -0400267 if (copy_from_user(&scan, buf, sizeof(scan))) {
268 ret = -EFAULT;
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 Young49571332017-11-04 08:30:45 -0400272 if (scan.flags || scan.keycode || scan.timestamp) {
273 ret = -EINVAL;
Sean Younga74b2bf2017-12-13 16:09:21 -0500274 goto out_unlock;
Sean Young49571332017-11-04 08:30:45 -0400275 }
Sean Young42e04422017-11-02 16:39:16 -0400276
277 /*
278 * The scancode field in lirc_scancode is 64-bit simply
279 * to future-proof it, since there are IR protocols encode
280 * use more than 32 bits. For now only 32-bit protocols
281 * are supported.
282 */
283 if (scan.scancode > U32_MAX ||
Sean Young49571332017-11-04 08:30:45 -0400284 !rc_validate_scancode(scan.rc_proto, scan.scancode)) {
285 ret = -EINVAL;
Sean Younga74b2bf2017-12-13 16:09:21 -0500286 goto out_unlock;
Sean Young49571332017-11-04 08:30:45 -0400287 }
Sean Young42e04422017-11-02 16:39:16 -0400288
289 raw = kmalloc_array(LIRCBUF_SIZE, sizeof(*raw), GFP_KERNEL);
Sean Young49571332017-11-04 08:30:45 -0400290 if (!raw) {
291 ret = -ENOMEM;
Sean Younga74b2bf2017-12-13 16:09:21 -0500292 goto out_unlock;
Sean Young49571332017-11-04 08:30:45 -0400293 }
Sean Young42e04422017-11-02 16:39:16 -0400294
295 ret = ir_raw_encode_scancode(scan.rc_proto, scan.scancode,
296 raw, LIRCBUF_SIZE);
297 if (ret < 0)
Sean Younga74b2bf2017-12-13 16:09:21 -0500298 goto out_kfree;
Sean Young42e04422017-11-02 16:39:16 -0400299
300 count = ret;
301
302 txbuf = kmalloc_array(count, sizeof(unsigned int), GFP_KERNEL);
303 if (!txbuf) {
304 ret = -ENOMEM;
Sean Younga74b2bf2017-12-13 16:09:21 -0500305 goto out_kfree;
Sean Young42e04422017-11-02 16:39:16 -0400306 }
307
308 for (i = 0; i < count; i++)
309 /* Convert from NS to US */
310 txbuf[i] = DIV_ROUND_UP(raw[i].duration, 1000);
311
312 if (dev->s_tx_carrier) {
313 int carrier = ir_raw_encode_carrier(scan.rc_proto);
314
315 if (carrier > 0)
316 dev->s_tx_carrier(dev, carrier);
317 }
318 } else {
Sean Young49571332017-11-04 08:30:45 -0400319 if (n < sizeof(unsigned int) || n % sizeof(unsigned int)) {
320 ret = -EINVAL;
Sean Younga74b2bf2017-12-13 16:09:21 -0500321 goto out_unlock;
Sean Young49571332017-11-04 08:30:45 -0400322 }
Sean Young42e04422017-11-02 16:39:16 -0400323
324 count = n / sizeof(unsigned int);
Sean Young49571332017-11-04 08:30:45 -0400325 if (count > LIRCBUF_SIZE || count % 2 == 0) {
326 ret = -EINVAL;
Sean Younga74b2bf2017-12-13 16:09:21 -0500327 goto out_unlock;
Sean Young49571332017-11-04 08:30:45 -0400328 }
Sean Young42e04422017-11-02 16:39:16 -0400329
330 txbuf = memdup_user(buf, n);
Sean Young49571332017-11-04 08:30:45 -0400331 if (IS_ERR(txbuf)) {
332 ret = PTR_ERR(txbuf);
Sean Younga74b2bf2017-12-13 16:09:21 -0500333 goto out_unlock;
Sean Young49571332017-11-04 08:30:45 -0400334 }
Sean Young42e04422017-11-02 16:39:16 -0400335 }
336
337 for (i = 0; i < count; i++) {
338 if (txbuf[i] > IR_MAX_DURATION / 1000 - duration || !txbuf[i]) {
339 ret = -EINVAL;
Sean Younga74b2bf2017-12-13 16:09:21 -0500340 goto out_kfree;
Sean Young42e04422017-11-02 16:39:16 -0400341 }
342
343 duration += txbuf[i];
344 }
345
346 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 Young7e45d662017-11-02 17:21:13 -0400350 if (fh->send_mode == LIRC_MODE_SCANCODE) {
Sean Young42e04422017-11-02 16:39:16 -0400351 ret = n;
352 } else {
353 for (duration = i = 0; i < ret; i++)
354 duration += txbuf[i];
355
356 ret *= sizeof(unsigned int);
Sean Youngdde7edf2017-12-11 17:12:09 -0500357 }
Sean Young42e04422017-11-02 16:39:16 -0400358
Sean Youngdde7edf2017-12-11 17:12:09 -0500359 /*
360 * The lircd gap calculation expects the write function to
361 * wait for the actual IR signal to be transmitted before
362 * returning.
363 */
364 towait = ktime_us_delta(ktime_add_us(start, duration),
365 ktime_get());
366 if (towait > 0) {
367 set_current_state(TASK_INTERRUPTIBLE);
368 schedule_timeout(usecs_to_jiffies(towait));
Sean Young42e04422017-11-02 16:39:16 -0400369 }
370
Sean Younga74b2bf2017-12-13 16:09:21 -0500371out_kfree:
Sean Young42e04422017-11-02 16:39:16 -0400372 kfree(txbuf);
373 kfree(raw);
Sean Younga74b2bf2017-12-13 16:09:21 -0500374out_unlock:
375 mutex_unlock(&dev->lock);
Sean Young42e04422017-11-02 16:39:16 -0400376 return ret;
377}
378
Sean Young7e45d662017-11-02 17:21:13 -0400379static long ir_lirc_ioctl(struct file *file, unsigned int cmd,
Sean Young42e04422017-11-02 16:39:16 -0400380 unsigned long arg)
381{
Sean Young7e45d662017-11-02 17:21:13 -0400382 struct lirc_fh *fh = file->private_data;
383 struct rc_dev *dev = fh->rc;
Sean Young42e04422017-11-02 16:39:16 -0400384 u32 __user *argp = (u32 __user *)(arg);
Sean Young49571332017-11-04 08:30:45 -0400385 u32 val = 0;
386 int ret;
Sean Young42e04422017-11-02 16:39:16 -0400387
388 if (_IOC_DIR(cmd) & _IOC_WRITE) {
389 ret = get_user(val, argp);
390 if (ret)
391 return ret;
392 }
393
Sean Young49571332017-11-04 08:30:45 -0400394 ret = mutex_lock_interruptible(&dev->lock);
395 if (ret)
396 return ret;
397
398 if (!dev->registered) {
399 ret = -ENODEV;
400 goto out;
401 }
Sean Young42e04422017-11-02 16:39:16 -0400402
403 switch (cmd) {
404 case LIRC_GET_FEATURES:
405 if (dev->driver_type == RC_DRIVER_SCANCODE)
406 val |= LIRC_CAN_REC_SCANCODE;
407
408 if (dev->driver_type == RC_DRIVER_IR_RAW) {
409 val |= LIRC_CAN_REC_MODE2 | LIRC_CAN_REC_SCANCODE;
410 if (dev->rx_resolution)
411 val |= LIRC_CAN_GET_REC_RESOLUTION;
412 }
413
414 if (dev->tx_ir) {
415 val |= LIRC_CAN_SEND_PULSE | LIRC_CAN_SEND_SCANCODE;
416 if (dev->s_tx_mask)
417 val |= LIRC_CAN_SET_TRANSMITTER_MASK;
418 if (dev->s_tx_carrier)
419 val |= LIRC_CAN_SET_SEND_CARRIER;
420 if (dev->s_tx_duty_cycle)
421 val |= LIRC_CAN_SET_SEND_DUTY_CYCLE;
422 }
423
424 if (dev->s_rx_carrier_range)
425 val |= LIRC_CAN_SET_REC_CARRIER |
426 LIRC_CAN_SET_REC_CARRIER_RANGE;
427
428 if (dev->s_learning_mode)
429 val |= LIRC_CAN_USE_WIDEBAND_RECEIVER;
430
431 if (dev->s_carrier_report)
432 val |= LIRC_CAN_MEASURE_CARRIER;
433
434 if (dev->max_timeout)
435 val |= LIRC_CAN_SET_REC_TIMEOUT;
436
437 break;
438
439 /* mode support */
440 case LIRC_GET_REC_MODE:
441 if (dev->driver_type == RC_DRIVER_IR_RAW_TX)
Sean Young49571332017-11-04 08:30:45 -0400442 ret = -ENOTTY;
443 else
444 val = fh->rec_mode;
Sean Young42e04422017-11-02 16:39:16 -0400445 break;
446
447 case LIRC_SET_REC_MODE:
448 switch (dev->driver_type) {
449 case RC_DRIVER_IR_RAW_TX:
Sean Young49571332017-11-04 08:30:45 -0400450 ret = -ENOTTY;
451 break;
Sean Young42e04422017-11-02 16:39:16 -0400452 case RC_DRIVER_SCANCODE:
453 if (val != LIRC_MODE_SCANCODE)
Sean Young49571332017-11-04 08:30:45 -0400454 ret = -EINVAL;
Sean Young42e04422017-11-02 16:39:16 -0400455 break;
456 case RC_DRIVER_IR_RAW:
457 if (!(val == LIRC_MODE_MODE2 ||
458 val == LIRC_MODE_SCANCODE))
Sean Young49571332017-11-04 08:30:45 -0400459 ret = -EINVAL;
Sean Young42e04422017-11-02 16:39:16 -0400460 break;
461 }
462
Sean Young49571332017-11-04 08:30:45 -0400463 if (!ret)
464 fh->rec_mode = val;
465 break;
Sean Young42e04422017-11-02 16:39:16 -0400466
467 case LIRC_GET_SEND_MODE:
468 if (!dev->tx_ir)
Sean Young49571332017-11-04 08:30:45 -0400469 ret = -ENOTTY;
470 else
471 val = fh->send_mode;
Sean Young42e04422017-11-02 16:39:16 -0400472 break;
473
474 case LIRC_SET_SEND_MODE:
475 if (!dev->tx_ir)
Sean Young49571332017-11-04 08:30:45 -0400476 ret = -ENOTTY;
477 else if (!(val == LIRC_MODE_PULSE || val == LIRC_MODE_SCANCODE))
478 ret = -EINVAL;
479 else
480 fh->send_mode = val;
481 break;
Sean Young42e04422017-11-02 16:39:16 -0400482
483 /* TX settings */
484 case LIRC_SET_TRANSMITTER_MASK:
485 if (!dev->s_tx_mask)
Sean Young49571332017-11-04 08:30:45 -0400486 ret = -ENOTTY;
487 else
488 ret = dev->s_tx_mask(dev, val);
489 break;
Sean Young42e04422017-11-02 16:39:16 -0400490
491 case LIRC_SET_SEND_CARRIER:
492 if (!dev->s_tx_carrier)
Sean Young49571332017-11-04 08:30:45 -0400493 ret = -ENOTTY;
494 else
495 ret = dev->s_tx_carrier(dev, val);
496 break;
Sean Young42e04422017-11-02 16:39:16 -0400497
498 case LIRC_SET_SEND_DUTY_CYCLE:
499 if (!dev->s_tx_duty_cycle)
Sean Young49571332017-11-04 08:30:45 -0400500 ret = -ENOTTY;
501 else if (val <= 0 || val >= 100)
502 ret = -EINVAL;
503 else
504 ret = dev->s_tx_duty_cycle(dev, val);
505 break;
Sean Young42e04422017-11-02 16:39:16 -0400506
507 /* RX settings */
508 case LIRC_SET_REC_CARRIER:
509 if (!dev->s_rx_carrier_range)
Sean Young49571332017-11-04 08:30:45 -0400510 ret = -ENOTTY;
511 else if (val <= 0)
512 ret = -EINVAL;
513 else
514 ret = dev->s_rx_carrier_range(dev, fh->carrier_low,
515 val);
516 break;
Sean Young42e04422017-11-02 16:39:16 -0400517
518 case LIRC_SET_REC_CARRIER_RANGE:
519 if (!dev->s_rx_carrier_range)
Sean Young49571332017-11-04 08:30:45 -0400520 ret = -ENOTTY;
521 else if (val <= 0)
522 ret = -EINVAL;
523 else
524 fh->carrier_low = val;
525 break;
Sean Young42e04422017-11-02 16:39:16 -0400526
527 case LIRC_GET_REC_RESOLUTION:
528 if (!dev->rx_resolution)
Sean Young49571332017-11-04 08:30:45 -0400529 ret = -ENOTTY;
530 else
531 val = dev->rx_resolution / 1000;
Sean Young42e04422017-11-02 16:39:16 -0400532 break;
533
534 case LIRC_SET_WIDEBAND_RECEIVER:
535 if (!dev->s_learning_mode)
Sean Young49571332017-11-04 08:30:45 -0400536 ret = -ENOTTY;
537 else
538 ret = dev->s_learning_mode(dev, !!val);
539 break;
Sean Young42e04422017-11-02 16:39:16 -0400540
541 case LIRC_SET_MEASURE_CARRIER_MODE:
542 if (!dev->s_carrier_report)
Sean Young49571332017-11-04 08:30:45 -0400543 ret = -ENOTTY;
544 else
545 ret = dev->s_carrier_report(dev, !!val);
546 break;
Sean Young42e04422017-11-02 16:39:16 -0400547
548 /* Generic timeout support */
549 case LIRC_GET_MIN_TIMEOUT:
550 if (!dev->max_timeout)
Sean Young49571332017-11-04 08:30:45 -0400551 ret = -ENOTTY;
552 else
553 val = DIV_ROUND_UP(dev->min_timeout, 1000);
Sean Young42e04422017-11-02 16:39:16 -0400554 break;
555
556 case LIRC_GET_MAX_TIMEOUT:
557 if (!dev->max_timeout)
Sean Young49571332017-11-04 08:30:45 -0400558 ret = -ENOTTY;
559 else
560 val = dev->max_timeout / 1000;
Sean Young42e04422017-11-02 16:39:16 -0400561 break;
562
563 case LIRC_SET_REC_TIMEOUT:
Sean Young49571332017-11-04 08:30:45 -0400564 if (!dev->max_timeout) {
565 ret = -ENOTTY;
566 } else if (val > U32_MAX / 1000) {
567 /* Check for multiply overflow */
568 ret = -EINVAL;
569 } else {
570 u32 tmp = val * 1000;
Sean Young42e04422017-11-02 16:39:16 -0400571
Sean Young49571332017-11-04 08:30:45 -0400572 if (tmp < dev->min_timeout || tmp > dev->max_timeout)
573 ret = -EINVAL;
574 else if (dev->s_timeout)
575 ret = dev->s_timeout(dev, tmp);
576 else if (!ret)
577 dev->timeout = tmp;
578 }
Sean Young42e04422017-11-02 16:39:16 -0400579 break;
580
581 case LIRC_SET_REC_TIMEOUT_REPORTS:
582 if (!dev->timeout)
Sean Young49571332017-11-04 08:30:45 -0400583 ret = -ENOTTY;
584 else
585 fh->send_timeout_reports = !!val;
Sean Young42e04422017-11-02 16:39:16 -0400586 break;
587
588 default:
Sean Young49571332017-11-04 08:30:45 -0400589 ret = -ENOTTY;
Sean Young42e04422017-11-02 16:39:16 -0400590 }
591
Sean Young49571332017-11-04 08:30:45 -0400592 if (!ret && _IOC_DIR(cmd) & _IOC_READ)
Sean Young42e04422017-11-02 16:39:16 -0400593 ret = put_user(val, argp);
594
Sean Young49571332017-11-04 08:30:45 -0400595out:
596 mutex_unlock(&dev->lock);
Sean Young42e04422017-11-02 16:39:16 -0400597 return ret;
598}
599
600static unsigned int ir_lirc_poll(struct file *file,
601 struct poll_table_struct *wait)
602{
Sean Young7e45d662017-11-02 17:21:13 -0400603 struct lirc_fh *fh = file->private_data;
604 struct rc_dev *rcdev = fh->rc;
Sean Young42e04422017-11-02 16:39:16 -0400605 unsigned int events = 0;
606
Sean Young7e45d662017-11-02 17:21:13 -0400607 poll_wait(file, &fh->wait_poll, wait);
Sean Young42e04422017-11-02 16:39:16 -0400608
609 if (!rcdev->registered) {
610 events = POLLHUP | POLLERR;
611 } else if (rcdev->driver_type != RC_DRIVER_IR_RAW_TX) {
Sean Young7e45d662017-11-02 17:21:13 -0400612 if (fh->rec_mode == LIRC_MODE_SCANCODE &&
613 !kfifo_is_empty(&fh->scancodes))
Sean Young42e04422017-11-02 16:39:16 -0400614 events = POLLIN | POLLRDNORM;
615
Sean Young7e45d662017-11-02 17:21:13 -0400616 if (fh->rec_mode == LIRC_MODE_MODE2 &&
617 !kfifo_is_empty(&fh->rawir))
Sean Young42e04422017-11-02 16:39:16 -0400618 events = POLLIN | POLLRDNORM;
619 }
620
621 return events;
622}
623
624static ssize_t ir_lirc_read_mode2(struct file *file, char __user *buffer,
625 size_t length)
626{
Sean Young7e45d662017-11-02 17:21:13 -0400627 struct lirc_fh *fh = file->private_data;
628 struct rc_dev *rcdev = fh->rc;
Sean Young42e04422017-11-02 16:39:16 -0400629 unsigned int copied;
630 int ret;
631
632 if (length < sizeof(unsigned int) || length % sizeof(unsigned int))
633 return -EINVAL;
634
635 do {
Sean Young7e45d662017-11-02 17:21:13 -0400636 if (kfifo_is_empty(&fh->rawir)) {
Sean Young42e04422017-11-02 16:39:16 -0400637 if (file->f_flags & O_NONBLOCK)
638 return -EAGAIN;
639
Sean Young7e45d662017-11-02 17:21:13 -0400640 ret = wait_event_interruptible(fh->wait_poll,
641 !kfifo_is_empty(&fh->rawir) ||
Sean Young42e04422017-11-02 16:39:16 -0400642 !rcdev->registered);
643 if (ret)
644 return ret;
645 }
646
647 if (!rcdev->registered)
648 return -ENODEV;
649
650 ret = mutex_lock_interruptible(&rcdev->lock);
651 if (ret)
652 return ret;
Sean Young7e45d662017-11-02 17:21:13 -0400653 ret = kfifo_to_user(&fh->rawir, buffer, length, &copied);
Sean Young42e04422017-11-02 16:39:16 -0400654 mutex_unlock(&rcdev->lock);
655 if (ret)
656 return ret;
657 } while (copied == 0);
658
659 return copied;
660}
661
662static ssize_t ir_lirc_read_scancode(struct file *file, char __user *buffer,
663 size_t length)
664{
Sean Young7e45d662017-11-02 17:21:13 -0400665 struct lirc_fh *fh = file->private_data;
666 struct rc_dev *rcdev = fh->rc;
Sean Young42e04422017-11-02 16:39:16 -0400667 unsigned int copied;
668 int ret;
669
670 if (length < sizeof(struct lirc_scancode) ||
671 length % sizeof(struct lirc_scancode))
672 return -EINVAL;
673
674 do {
Sean Young7e45d662017-11-02 17:21:13 -0400675 if (kfifo_is_empty(&fh->scancodes)) {
Sean Young42e04422017-11-02 16:39:16 -0400676 if (file->f_flags & O_NONBLOCK)
677 return -EAGAIN;
678
Sean Young7e45d662017-11-02 17:21:13 -0400679 ret = wait_event_interruptible(fh->wait_poll,
680 !kfifo_is_empty(&fh->scancodes) ||
Sean Young42e04422017-11-02 16:39:16 -0400681 !rcdev->registered);
682 if (ret)
683 return ret;
684 }
685
686 if (!rcdev->registered)
687 return -ENODEV;
688
689 ret = mutex_lock_interruptible(&rcdev->lock);
690 if (ret)
691 return ret;
Sean Young7e45d662017-11-02 17:21:13 -0400692 ret = kfifo_to_user(&fh->scancodes, buffer, length, &copied);
Sean Young42e04422017-11-02 16:39:16 -0400693 mutex_unlock(&rcdev->lock);
694 if (ret)
695 return ret;
696 } while (copied == 0);
697
698 return copied;
699}
700
701static ssize_t ir_lirc_read(struct file *file, char __user *buffer,
702 size_t length, loff_t *ppos)
703{
Sean Young7e45d662017-11-02 17:21:13 -0400704 struct lirc_fh *fh = file->private_data;
705 struct rc_dev *rcdev = fh->rc;
Sean Young42e04422017-11-02 16:39:16 -0400706
707 if (rcdev->driver_type == RC_DRIVER_IR_RAW_TX)
708 return -EINVAL;
709
710 if (!rcdev->registered)
711 return -ENODEV;
712
Sean Young7e45d662017-11-02 17:21:13 -0400713 if (fh->rec_mode == LIRC_MODE_MODE2)
Sean Young42e04422017-11-02 16:39:16 -0400714 return ir_lirc_read_mode2(file, buffer, length);
715 else /* LIRC_MODE_SCANCODE */
716 return ir_lirc_read_scancode(file, buffer, length);
717}
718
719static const struct file_operations lirc_fops = {
720 .owner = THIS_MODULE,
721 .write = ir_lirc_transmit_ir,
722 .unlocked_ioctl = ir_lirc_ioctl,
723#ifdef CONFIG_COMPAT
724 .compat_ioctl = ir_lirc_ioctl,
725#endif
726 .read = ir_lirc_read,
727 .poll = ir_lirc_poll,
728 .open = ir_lirc_open,
729 .release = ir_lirc_close,
730 .llseek = no_llseek,
731};
732
David Härdemanb15e3932017-06-25 09:32:36 -0300733static void lirc_release_device(struct device *ld)
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300734{
Sean Younga6ddd4f2017-09-26 09:34:47 -0400735 struct rc_dev *rcdev = container_of(ld, struct rc_dev, lirc_dev);
Sean Younga607f512017-08-04 10:12:03 -0400736
Sean Younga6ddd4f2017-09-26 09:34:47 -0400737 put_device(&rcdev->dev);
Andi Shyti6fa99e12016-07-06 06:01:13 -0300738}
739
Sean Younga6ddd4f2017-09-26 09:34:47 -0400740int ir_lirc_register(struct rc_dev *dev)
David Härdeman6ecccc32017-06-25 09:32:15 -0300741{
Sean Younga6ddd4f2017-09-26 09:34:47 -0400742 int err, minor;
David Härdemanb15e3932017-06-25 09:32:36 -0300743
Sean Young7e45d662017-11-02 17:21:13 -0400744 minor = ida_simple_get(&lirc_ida, 0, RC_DEV_MAX, GFP_KERNEL);
745 if (minor < 0)
746 return minor;
747
Sean Younga6ddd4f2017-09-26 09:34:47 -0400748 device_initialize(&dev->lirc_dev);
749 dev->lirc_dev.class = lirc_class;
Sean Younga6ddd4f2017-09-26 09:34:47 -0400750 dev->lirc_dev.parent = &dev->dev;
Sean Young7e45d662017-11-02 17:21:13 -0400751 dev->lirc_dev.release = lirc_release_device;
Sean Younga6ddd4f2017-09-26 09:34:47 -0400752 dev->lirc_dev.devt = MKDEV(MAJOR(lirc_base_dev), minor);
753 dev_set_name(&dev->lirc_dev, "lirc%d", minor);
Sean Younga607f512017-08-04 10:12:03 -0400754
Sean Young7e45d662017-11-02 17:21:13 -0400755 INIT_LIST_HEAD(&dev->lirc_fh);
756 spin_lock_init(&dev->lirc_fh_lock);
757
Sean Younga6ddd4f2017-09-26 09:34:47 -0400758 cdev_init(&dev->lirc_cdev, &lirc_fops);
759
760 err = cdev_device_add(&dev->lirc_cdev, &dev->lirc_dev);
761 if (err)
762 goto out_ida;
763
764 get_device(&dev->dev);
765
766 dev_info(&dev->dev, "lirc_dev: driver %s registered at minor = %d",
767 dev->driver_name, minor);
David Härdeman56481f02017-05-01 13:04:11 -0300768
David Härdemanc3c6dd72017-06-25 09:31:24 -0300769 return 0;
Sean Younga6ddd4f2017-09-26 09:34:47 -0400770
771out_ida:
772 ida_simple_remove(&lirc_ida, minor);
Sean Younga6ddd4f2017-09-26 09:34:47 -0400773 return err;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300774}
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300775
Sean Younga6ddd4f2017-09-26 09:34:47 -0400776void ir_lirc_unregister(struct rc_dev *dev)
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300777{
Sean Young7e45d662017-11-02 17:21:13 -0400778 unsigned long flags;
779 struct lirc_fh *fh;
780
Sean Younga6ddd4f2017-09-26 09:34:47 -0400781 dev_dbg(&dev->dev, "lirc_dev: driver %s unregistered from minor = %d\n",
782 dev->driver_name, MINOR(dev->lirc_dev.devt));
Sean Young71695af2017-09-23 14:44:18 -0400783
Sean Young7e45d662017-11-02 17:21:13 -0400784 spin_lock_irqsave(&dev->lirc_fh_lock, flags);
785 list_for_each_entry(fh, &dev->lirc_fh, list)
786 wake_up_poll(&fh->wait_poll, POLLHUP | POLLERR);
787 spin_unlock_irqrestore(&dev->lirc_fh_lock, flags);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300788
Sean Younga6ddd4f2017-09-26 09:34:47 -0400789 cdev_device_del(&dev->lirc_cdev, &dev->lirc_dev);
790 ida_simple_remove(&lirc_ida, MINOR(dev->lirc_dev.devt));
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300791}
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300792
Sean Younga60d64b2017-09-23 10:41:13 -0400793int __init lirc_dev_init(void)
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300794{
795 int retval;
796
797 lirc_class = class_create(THIS_MODULE, "lirc");
798 if (IS_ERR(lirc_class)) {
Andi Shyti3fac0312016-07-06 06:01:16 -0300799 pr_err("class_create failed\n");
Andi Shyti54fceca2016-07-06 06:01:17 -0300800 return PTR_ERR(lirc_class);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300801 }
802
Sean Younga6ddd4f2017-09-26 09:34:47 -0400803 retval = alloc_chrdev_region(&lirc_base_dev, 0, RC_DEV_MAX,
David Härdeman463015d2017-05-01 13:04:47 -0300804 "BaseRemoteCtl");
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300805 if (retval) {
806 class_destroy(lirc_class);
Andi Shyti3fac0312016-07-06 06:01:16 -0300807 pr_err("alloc_chrdev_region failed\n");
Andi Shyti54fceca2016-07-06 06:01:17 -0300808 return retval;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300809 }
810
Andi Shyti3fac0312016-07-06 06:01:16 -0300811 pr_info("IR Remote Control driver registered, major %d\n",
812 MAJOR(lirc_base_dev));
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300813
Andi Shyti54fceca2016-07-06 06:01:17 -0300814 return 0;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300815}
816
Sean Younga60d64b2017-09-23 10:41:13 -0400817void __exit lirc_dev_exit(void)
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300818{
819 class_destroy(lirc_class);
Sean Younga6ddd4f2017-09-26 09:34:47 -0400820 unregister_chrdev_region(lirc_base_dev, RC_DEV_MAX);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300821}