History

media: rc: move ir-lirc-codec.c contents into lirc_dev.c

Since removing the lirc kapi, ir-lirc-codec.c only contains lirc fops so the file name is no longer correct. By moving its content into lirc_dev.c the ugly extern struct lirc_fops is not longer needed, and everything lirc related is in one file.

Sean Young committed 9 years ago

Commit 42e04421 parent 8a866fe4 files · +605 −626Browse files

Unified · Split

4 files
  1. drivers/media/rc/Makefile +1 −1
  2. drivers/media/rc/ir-lirc-codec.c +0 −623
  3. drivers/media/rc/lirc_dev.c +604 −0
  4. drivers/media/rc/rc-core-priv.h +0 −2
drivers/media/rc/Makefile+1−1
@@ -4,7 +4,7 @@ obj-y += keymaps/ · 3 unmodified lines
4 4
5obj-$(CONFIG_RC_CORE) += rc-core.o5obj-$(CONFIG_RC_CORE) += rc-core.o
6rc-core-y := rc-main.o rc-ir-raw.o6rc-core-y := rc-main.o rc-ir-raw.o
7rc-core-$(CONFIG_LIRC) += lirc_dev.o ir-lirc-codec.o7rc-core-$(CONFIG_LIRC) += lirc_dev.o
8obj-$(CONFIG_IR_NEC_DECODER) += ir-nec-decoder.o8obj-$(CONFIG_IR_NEC_DECODER) += ir-nec-decoder.o
9obj-$(CONFIG_IR_RC5_DECODER) += ir-rc5-decoder.o9obj-$(CONFIG_IR_RC5_DECODER) += ir-rc5-decoder.o
10obj-$(CONFIG_IR_RC6_DECODER) += ir-rc6-decoder.o10obj-$(CONFIG_IR_RC6_DECODER) += ir-rc6-decoder.o
drivers/media/rc/ir-lirc-codec.c+0−623
@@ -1,623 +0,0 @@
1/* ir-lirc-codec.c - rc-core to classic lirc interface bridge
2 *
3 * Copyright (C) 2010 by Jarod Wilson <jarod@redhat.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation version 2 of the License.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14
15#include <linux/poll.h>
16#include <linux/sched.h>
17#include <linux/wait.h>
18#include <media/lirc.h>
19#include <media/rc-core.h>
20#include "rc-core-priv.h"
21
22#define LIRCBUF_SIZE 256
23
24/**
25 * ir_lirc_raw_event() - Send raw IR data to lirc to be relayed to userspace
26 *
27 * @dev: the struct rc_dev descriptor of the device
28 * @ev: the struct ir_raw_event descriptor of the pulse/space
29 */
30void ir_lirc_raw_event(struct rc_dev *dev, struct ir_raw_event ev)
31{
32 int sample;
33
34 /* Packet start */
35 if (ev.reset) {
36 /* Userspace expects a long space event before the start of
37 * the signal to use as a sync. This may be done with repeat
38 * packets and normal samples. But if a reset has been sent
39 * then we assume that a long time has passed, so we send a
40 * space with the maximum time value. */
41 sample = LIRC_SPACE(LIRC_VALUE_MASK);
42 IR_dprintk(2, "delivering reset sync space to lirc_dev\n");
43
44 /* Carrier reports */
45 } else if (ev.carrier_report) {
46 sample = LIRC_FREQUENCY(ev.carrier);
47 IR_dprintk(2, "carrier report (freq: %d)\n", sample);
48
49 /* Packet end */
50 } else if (ev.timeout) {
51
52 if (dev->gap)
53 return;
54
55 dev->gap_start = ktime_get();
56 dev->gap = true;
57 dev->gap_duration = ev.duration;
58
59 if (!dev->send_timeout_reports)
60 return;
61
62 sample = LIRC_TIMEOUT(ev.duration / 1000);
63 IR_dprintk(2, "timeout report (duration: %d)\n", sample);
64
65 /* Normal sample */
66 } else {
67
68 if (dev->gap) {
69 dev->gap_duration += ktime_to_ns(ktime_sub(ktime_get(),
70 dev->gap_start));
71
72 /* Convert to ms and cap by LIRC_VALUE_MASK */
73 do_div(dev->gap_duration, 1000);
74 dev->gap_duration = min_t(u64, dev->gap_duration,
75 LIRC_VALUE_MASK);
76
77 kfifo_put(&dev->rawir, LIRC_SPACE(dev->gap_duration));
78 dev->gap = false;
79 }
80
81 sample = ev.pulse ? LIRC_PULSE(ev.duration / 1000) :
82 LIRC_SPACE(ev.duration / 1000);
83 IR_dprintk(2, "delivering %uus %s to lirc_dev\n",
84 TO_US(ev.duration), TO_STR(ev.pulse));
85 }
86
87 kfifo_put(&dev->rawir, sample);
88 wake_up_poll(&dev->wait_poll, POLLIN | POLLRDNORM);
89}
90
91/**
92 * ir_lirc_scancode_event() - Send scancode data to lirc to be relayed to
93 * userspace
94 * @dev: the struct rc_dev descriptor of the device
95 * @lsc: the struct lirc_scancode describing the decoded scancode
96 */
97void ir_lirc_scancode_event(struct rc_dev *dev, struct lirc_scancode *lsc)
98{
99 lsc->timestamp = ktime_get_ns();
100
101 if (kfifo_put(&dev->scancodes, *lsc))
102 wake_up_poll(&dev->wait_poll, POLLIN | POLLRDNORM);
103}
104EXPORT_SYMBOL_GPL(ir_lirc_scancode_event);
105
106static int ir_lirc_open(struct inode *inode, struct file *file)
107{
108 struct rc_dev *dev = container_of(inode->i_cdev, struct rc_dev,
109 lirc_cdev);
110 int retval;
111
112 retval = rc_open(dev);
113 if (retval)
114 return retval;
115
116 retval = mutex_lock_interruptible(&dev->lock);
117 if (retval)
118 goto out_rc;
119
120 if (!dev->registered) {
121 retval = -ENODEV;
122 goto out_unlock;
123 }
124
125 if (dev->lirc_open) {
126 retval = -EBUSY;
127 goto out_unlock;
128 }
129
130 if (dev->driver_type == RC_DRIVER_IR_RAW)
131 kfifo_reset_out(&dev->rawir);
132 if (dev->driver_type != RC_DRIVER_IR_RAW_TX)
133 kfifo_reset_out(&dev->scancodes);
134
135 dev->lirc_open++;
136 file->private_data = dev;
137
138 nonseekable_open(inode, file);
139 mutex_unlock(&dev->lock);
140
141 return 0;
142
143out_unlock:
144 mutex_unlock(&dev->lock);
145out_rc:
146 rc_close(dev);
147 return retval;
148}
149
150static int ir_lirc_close(struct inode *inode, struct file *file)
151{
152 struct rc_dev *dev = file->private_data;
153
154 mutex_lock(&dev->lock);
155 dev->lirc_open--;
156 mutex_unlock(&dev->lock);
157
158 rc_close(dev);
159
160 return 0;
161}
162
163static ssize_t ir_lirc_transmit_ir(struct file *file, const char __user *buf,
164 size_t n, loff_t *ppos)
165{
166 struct rc_dev *dev = file->private_data;
167 unsigned int *txbuf = NULL;
168 struct ir_raw_event *raw = NULL;
169 ssize_t ret = -EINVAL;
170 size_t count;
171 ktime_t start;
172 s64 towait;
173 unsigned int duration = 0; /* signal duration in us */
174 int i;
175
176 if (!dev->registered)
177 return -ENODEV;
178
179 start = ktime_get();
180
181 if (!dev->tx_ir) {
182 ret = -EINVAL;
183 goto out;
184 }
185
186 if (dev->send_mode == LIRC_MODE_SCANCODE) {
187 struct lirc_scancode scan;
188
189 if (n != sizeof(scan))
190 return -EINVAL;
191
192 if (copy_from_user(&scan, buf, sizeof(scan)))
193 return -EFAULT;
194
195 if (scan.flags || scan.keycode || scan.timestamp)
196 return -EINVAL;
197
198 /*
199 * The scancode field in lirc_scancode is 64-bit simply
200 * to future-proof it, since there are IR protocols encode
201 * use more than 32 bits. For now only 32-bit protocols
202 * are supported.
203 */
204 if (scan.scancode > U32_MAX ||
205 !rc_validate_scancode(scan.rc_proto, scan.scancode))
206 return -EINVAL;
207
208 raw = kmalloc_array(LIRCBUF_SIZE, sizeof(*raw), GFP_KERNEL);
209 if (!raw)
210 return -ENOMEM;
211
212 ret = ir_raw_encode_scancode(scan.rc_proto, scan.scancode,
213 raw, LIRCBUF_SIZE);
214 if (ret < 0)
215 goto out;
216
217 count = ret;
218
219 txbuf = kmalloc_array(count, sizeof(unsigned int), GFP_KERNEL);
220 if (!txbuf) {
221 ret = -ENOMEM;
222 goto out;
223 }
224
225 for (i = 0; i < count; i++)
226 /* Convert from NS to US */
227 txbuf[i] = DIV_ROUND_UP(raw[i].duration, 1000);
228
229 if (dev->s_tx_carrier) {
230 int carrier = ir_raw_encode_carrier(scan.rc_proto);
231
232 if (carrier > 0)
233 dev->s_tx_carrier(dev, carrier);
234 }
235 } else {
236 if (n < sizeof(unsigned int) || n % sizeof(unsigned int))
237 return -EINVAL;
238
239 count = n / sizeof(unsigned int);
240 if (count > LIRCBUF_SIZE || count % 2 == 0)
241 return -EINVAL;
242
243 txbuf = memdup_user(buf, n);
244 if (IS_ERR(txbuf))
245 return PTR_ERR(txbuf);
246 }
247
248 for (i = 0; i < count; i++) {
249 if (txbuf[i] > IR_MAX_DURATION / 1000 - duration || !txbuf[i]) {
250 ret = -EINVAL;
251 goto out;
252 }
253
254 duration += txbuf[i];
255 }
256
257 ret = dev->tx_ir(dev, txbuf, count);
258 if (ret < 0)
259 goto out;
260
261 if (dev->send_mode == LIRC_MODE_SCANCODE) {
262 ret = n;
263 } else {
264 for (duration = i = 0; i < ret; i++)
265 duration += txbuf[i];
266
267 ret *= sizeof(unsigned int);
268
269 /*
270 * The lircd gap calculation expects the write function to
271 * wait for the actual IR signal to be transmitted before
272 * returning.
273 */
274 towait = ktime_us_delta(ktime_add_us(start, duration),
275 ktime_get());
276 if (towait > 0) {
277 set_current_state(TASK_INTERRUPTIBLE);
278 schedule_timeout(usecs_to_jiffies(towait));
279 }
280 }
281
282out:
283 kfree(txbuf);
284 kfree(raw);
285 return ret;
286}
287
288static long ir_lirc_ioctl(struct file *filep, unsigned int cmd,
289 unsigned long arg)
290{
291 struct rc_dev *dev = filep->private_data;
292 u32 __user *argp = (u32 __user *)(arg);
293 int ret = 0;
294 __u32 val = 0, tmp;
295
296 if (_IOC_DIR(cmd) & _IOC_WRITE) {
297 ret = get_user(val, argp);
298 if (ret)
299 return ret;
300 }
301
302 if (!dev->registered)
303 return -ENODEV;
304
305 switch (cmd) {
306 case LIRC_GET_FEATURES:
307 if (dev->driver_type == RC_DRIVER_SCANCODE)
308 val |= LIRC_CAN_REC_SCANCODE;
309
310 if (dev->driver_type == RC_DRIVER_IR_RAW) {
311 val |= LIRC_CAN_REC_MODE2 | LIRC_CAN_REC_SCANCODE;
312 if (dev->rx_resolution)
313 val |= LIRC_CAN_GET_REC_RESOLUTION;
314 }
315
316 if (dev->tx_ir) {
317 val |= LIRC_CAN_SEND_PULSE | LIRC_CAN_SEND_SCANCODE;
318 if (dev->s_tx_mask)
319 val |= LIRC_CAN_SET_TRANSMITTER_MASK;
320 if (dev->s_tx_carrier)
321 val |= LIRC_CAN_SET_SEND_CARRIER;
322 if (dev->s_tx_duty_cycle)
323 val |= LIRC_CAN_SET_SEND_DUTY_CYCLE;
324 }
325
326 if (dev->s_rx_carrier_range)
327 val |= LIRC_CAN_SET_REC_CARRIER |
328 LIRC_CAN_SET_REC_CARRIER_RANGE;
329
330 if (dev->s_learning_mode)
331 val |= LIRC_CAN_USE_WIDEBAND_RECEIVER;
332
333 if (dev->s_carrier_report)
334 val |= LIRC_CAN_MEASURE_CARRIER;
335
336 if (dev->max_timeout)
337 val |= LIRC_CAN_SET_REC_TIMEOUT;
338
339 break;
340
341 /* mode support */
342 case LIRC_GET_REC_MODE:
343 if (dev->driver_type == RC_DRIVER_IR_RAW_TX)
344 return -ENOTTY;
345
346 val = dev->rec_mode;
347 break;
348
349 case LIRC_SET_REC_MODE:
350 switch (dev->driver_type) {
351 case RC_DRIVER_IR_RAW_TX:
352 return -ENOTTY;
353 case RC_DRIVER_SCANCODE:
354 if (val != LIRC_MODE_SCANCODE)
355 return -EINVAL;
356 break;
357 case RC_DRIVER_IR_RAW:
358 if (!(val == LIRC_MODE_MODE2 ||
359 val == LIRC_MODE_SCANCODE))
360 return -EINVAL;
361 break;
362 }
363
364 dev->rec_mode = val;
365 return 0;
366
367 case LIRC_GET_SEND_MODE:
368 if (!dev->tx_ir)
369 return -ENOTTY;
370
371 val = dev->send_mode;
372 break;
373
374 case LIRC_SET_SEND_MODE:
375 if (!dev->tx_ir)
376 return -ENOTTY;
377
378 if (!(val == LIRC_MODE_PULSE || val == LIRC_MODE_SCANCODE))
379 return -EINVAL;
380
381 dev->send_mode = val;
382 return 0;
383
384 /* TX settings */
385 case LIRC_SET_TRANSMITTER_MASK:
386 if (!dev->s_tx_mask)
387 return -ENOTTY;
388
389 return dev->s_tx_mask(dev, val);
390
391 case LIRC_SET_SEND_CARRIER:
392 if (!dev->s_tx_carrier)
393 return -ENOTTY;
394
395 return dev->s_tx_carrier(dev, val);
396
397 case LIRC_SET_SEND_DUTY_CYCLE:
398 if (!dev->s_tx_duty_cycle)
399 return -ENOTTY;
400
401 if (val <= 0 || val >= 100)
402 return -EINVAL;
403
404 return dev->s_tx_duty_cycle(dev, val);
405
406 /* RX settings */
407 case LIRC_SET_REC_CARRIER:
408 if (!dev->s_rx_carrier_range)
409 return -ENOTTY;
410
411 if (val <= 0)
412 return -EINVAL;
413
414 return dev->s_rx_carrier_range(dev,
415 dev->carrier_low,
416 val);
417
418 case LIRC_SET_REC_CARRIER_RANGE:
419 if (!dev->s_rx_carrier_range)
420 return -ENOTTY;
421
422 if (val <= 0)
423 return -EINVAL;
424
425 dev->carrier_low = val;
426 return 0;
427
428 case LIRC_GET_REC_RESOLUTION:
429 if (!dev->rx_resolution)
430 return -ENOTTY;
431
432 val = dev->rx_resolution / 1000;
433 break;
434
435 case LIRC_SET_WIDEBAND_RECEIVER:
436 if (!dev->s_learning_mode)
437 return -ENOTTY;
438
439 return dev->s_learning_mode(dev, !!val);
440
441 case LIRC_SET_MEASURE_CARRIER_MODE:
442 if (!dev->s_carrier_report)
443 return -ENOTTY;
444
445 return dev->s_carrier_report(dev, !!val);
446
447 /* Generic timeout support */
448 case LIRC_GET_MIN_TIMEOUT:
449 if (!dev->max_timeout)
450 return -ENOTTY;
451 val = DIV_ROUND_UP(dev->min_timeout, 1000);
452 break;
453
454 case LIRC_GET_MAX_TIMEOUT:
455 if (!dev->max_timeout)
456 return -ENOTTY;
457 val = dev->max_timeout / 1000;
458 break;
459
460 case LIRC_SET_REC_TIMEOUT:
461 if (!dev->max_timeout)
462 return -ENOTTY;
463
464 /* Check for multiply overflow */
465 if (val > U32_MAX / 1000)
466 return -EINVAL;
467
468 tmp = val * 1000;
469
470 if (tmp < dev->min_timeout || tmp > dev->max_timeout)
471 return -EINVAL;
472
473 if (dev->s_timeout)
474 ret = dev->s_timeout(dev, tmp);
475 if (!ret)
476 dev->timeout = tmp;
477 break;
478
479 case LIRC_SET_REC_TIMEOUT_REPORTS:
480 if (!dev->timeout)
481 return -ENOTTY;
482
483 dev->send_timeout_reports = !!val;
484 break;
485
486 default:
487 return -ENOTTY;
488 }
489
490 if (_IOC_DIR(cmd) & _IOC_READ)
491 ret = put_user(val, argp);
492
493 return ret;
494}
495
496static unsigned int ir_lirc_poll(struct file *file,
497 struct poll_table_struct *wait)
498{
499 struct rc_dev *rcdev = file->private_data;
500 unsigned int events = 0;
501
502 poll_wait(file, &rcdev->wait_poll, wait);
503
504 if (!rcdev->registered) {
505 events = POLLHUP | POLLERR;
506 } else if (rcdev->driver_type != RC_DRIVER_IR_RAW_TX) {
507 if (rcdev->rec_mode == LIRC_MODE_SCANCODE &&
508 !kfifo_is_empty(&rcdev->scancodes))
509 events = POLLIN | POLLRDNORM;
510
511 if (rcdev->rec_mode == LIRC_MODE_MODE2 &&
512 !kfifo_is_empty(&rcdev->rawir))
513 events = POLLIN | POLLRDNORM;
514 }
515
516 return events;
517}
518
519static ssize_t ir_lirc_read_mode2(struct file *file, char __user *buffer,
520 size_t length)
521{
522 struct rc_dev *rcdev = file->private_data;
523 unsigned int copied;
524 int ret;
525
526 if (length < sizeof(unsigned int) || length % sizeof(unsigned int))
527 return -EINVAL;
528
529 do {
530 if (kfifo_is_empty(&rcdev->rawir)) {
531 if (file->f_flags & O_NONBLOCK)
532 return -EAGAIN;
533
534 ret = wait_event_interruptible(rcdev->wait_poll,
535 !kfifo_is_empty(&rcdev->rawir) ||
536 !rcdev->registered);
537 if (ret)
538 return ret;
539 }
540
541 if (!rcdev->registered)
542 return -ENODEV;
543
544 ret = mutex_lock_interruptible(&rcdev->lock);
545 if (ret)
546 return ret;
547 ret = kfifo_to_user(&rcdev->rawir, buffer, length, &copied);
548 mutex_unlock(&rcdev->lock);
549 if (ret)
550 return ret;
551 } while (copied == 0);
552
553 return copied;
554}
555
556static ssize_t ir_lirc_read_scancode(struct file *file, char __user *buffer,
557 size_t length)
558{
559 struct rc_dev *rcdev = file->private_data;
560 unsigned int copied;
561 int ret;
562
563 if (length < sizeof(struct lirc_scancode) ||
564 length % sizeof(struct lirc_scancode))
565 return -EINVAL;
566
567 do {
568 if (kfifo_is_empty(&rcdev->scancodes)) {
569 if (file->f_flags & O_NONBLOCK)
570 return -EAGAIN;
571
572 ret = wait_event_interruptible(rcdev->wait_poll,
573 !kfifo_is_empty(&rcdev->scancodes) ||
574 !rcdev->registered);
575 if (ret)
576 return ret;
577 }
578
579 if (!rcdev->registered)
580 return -ENODEV;
581
582 ret = mutex_lock_interruptible(&rcdev->lock);
583 if (ret)
584 return ret;
585 ret = kfifo_to_user(&rcdev->scancodes, buffer, length, &copied);
586 mutex_unlock(&rcdev->lock);
587 if (ret)
588 return ret;
589 } while (copied == 0);
590
591 return copied;
592}
593
594static ssize_t ir_lirc_read(struct file *file, char __user *buffer,
595 size_t length, loff_t *ppos)
596{
597 struct rc_dev *rcdev = file->private_data;
598
599 if (rcdev->driver_type == RC_DRIVER_IR_RAW_TX)
600 return -EINVAL;
601
602 if (!rcdev->registered)
603 return -ENODEV;
604
605 if (rcdev->rec_mode == LIRC_MODE_MODE2)
606 return ir_lirc_read_mode2(file, buffer, length);
607 else /* LIRC_MODE_SCANCODE */
608 return ir_lirc_read_scancode(file, buffer, length);
609}
610
611const struct file_operations lirc_fops = {
612 .owner = THIS_MODULE,
613 .write = ir_lirc_transmit_ir,
614 .unlocked_ioctl = ir_lirc_ioctl,
615#ifdef CONFIG_COMPAT
616 .compat_ioctl = ir_lirc_ioctl,
617#endif
618 .read = ir_lirc_read,
619 .poll = ir_lirc_poll,
620 .open = ir_lirc_open,
621 .release = ir_lirc_close,
622 .llseek = no_llseek,
623};
drivers/media/rc/lirc_dev.c+604−0
@@ -22,11 +22,14 @@ · 21 unmodified lines
22#include <linux/device.h>22#include <linux/device.h>
23#include <linux/idr.h>23#include <linux/idr.h>
24#include <linux/poll.h>24#include <linux/poll.h>
25#include <linux/sched.h>
26#include <linux/wait.h>
25 27
26#include "rc-core-priv.h"28#include "rc-core-priv.h"
27#include <media/lirc.h>29#include <media/lirc.h>
28 30
29#define LOGHEAD "lirc_dev (%s[%d]): "31#define LOGHEAD "lirc_dev (%s[%d]): "
32#define LIRCBUF_SIZE 256
30 33
31static dev_t lirc_base_dev;34static dev_t lirc_base_dev;
32 35
@@ -36,6 +39,607 @@ static DEFINE_IDA(lirc_ida); · 3 unmodified lines
36/* Only used for sysfs but defined to void otherwise */39/* Only used for sysfs but defined to void otherwise */
37static struct class *lirc_class;40static struct class *lirc_class;
38 41
42/**
43 * ir_lirc_raw_event() - Send raw IR data to lirc to be relayed to userspace
44 *
45 * @dev: the struct rc_dev descriptor of the device
46 * @ev: the struct ir_raw_event descriptor of the pulse/space
47 */
48void ir_lirc_raw_event(struct rc_dev *dev, struct ir_raw_event ev)
49{
50 int sample;
51
52 /* Packet start */
53 if (ev.reset) {
54 /*
55 * Userspace expects a long space event before the start of
56 * the signal to use as a sync. This may be done with repeat
57 * packets and normal samples. But if a reset has been sent
58 * then we assume that a long time has passed, so we send a
59 * space with the maximum time value.
60 */
61 sample = LIRC_SPACE(LIRC_VALUE_MASK);
62 IR_dprintk(2, "delivering reset sync space to lirc_dev\n");
63
64 /* Carrier reports */
65 } else if (ev.carrier_report) {
66 sample = LIRC_FREQUENCY(ev.carrier);
67 IR_dprintk(2, "carrier report (freq: %d)\n", sample);
68
69 /* Packet end */
70 } else if (ev.timeout) {
71 if (dev->gap)
72 return;
73
74 dev->gap_start = ktime_get();
75 dev->gap = true;
76 dev->gap_duration = ev.duration;
77
78 if (!dev->send_timeout_reports)
79 return;
80
81 sample = LIRC_TIMEOUT(ev.duration / 1000);
82 IR_dprintk(2, "timeout report (duration: %d)\n", sample);
83
84 /* Normal sample */
85 } else {
86 if (dev->gap) {
87 dev->gap_duration += ktime_to_ns(ktime_sub(ktime_get(),
88 dev->gap_start));
89
90 /* Convert to ms and cap by LIRC_VALUE_MASK */
91 do_div(dev->gap_duration, 1000);
92 dev->gap_duration = min_t(u64, dev->gap_duration,
93 LIRC_VALUE_MASK);
94
95 kfifo_put(&dev->rawir, LIRC_SPACE(dev->gap_duration));
96 dev->gap = false;
97 }
98
99 sample = ev.pulse ? LIRC_PULSE(ev.duration / 1000) :
100 LIRC_SPACE(ev.duration / 1000);
101 IR_dprintk(2, "delivering %uus %s to lirc_dev\n",
102 TO_US(ev.duration), TO_STR(ev.pulse));
103 }
104
105 kfifo_put(&dev->rawir, sample);
106 wake_up_poll(&dev->wait_poll, POLLIN | POLLRDNORM);
107}
108
109/**
110 * ir_lirc_scancode_event() - Send scancode data to lirc to be relayed to
111 * userspace
112 * @dev: the struct rc_dev descriptor of the device
113 * @lsc: the struct lirc_scancode describing the decoded scancode
114 */
115void ir_lirc_scancode_event(struct rc_dev *dev, struct lirc_scancode *lsc)
116{
117 lsc->timestamp = ktime_get_ns();
118
119 if (kfifo_put(&dev->scancodes, *lsc))
120 wake_up_poll(&dev->wait_poll, POLLIN | POLLRDNORM);
121}
122EXPORT_SYMBOL_GPL(ir_lirc_scancode_event);
123
124static int ir_lirc_open(struct inode *inode, struct file *file)
125{
126 struct rc_dev *dev = container_of(inode->i_cdev, struct rc_dev,
127 lirc_cdev);
128 int retval;
129
130 retval = rc_open(dev);
131 if (retval)
132 return retval;
133
134 retval = mutex_lock_interruptible(&dev->lock);
135 if (retval)
136 goto out_rc;
137
138 if (!dev->registered) {
139 retval = -ENODEV;
140 goto out_unlock;
141 }
142
143 if (dev->lirc_open) {
144 retval = -EBUSY;
145 goto out_unlock;
146 }
147
148 if (dev->driver_type == RC_DRIVER_IR_RAW)
149 kfifo_reset_out(&dev->rawir);
150 if (dev->driver_type != RC_DRIVER_IR_RAW_TX)
151 kfifo_reset_out(&dev->scancodes);
152
153 dev->lirc_open++;
154 file->private_data = dev;
155
156 nonseekable_open(inode, file);
157 mutex_unlock(&dev->lock);
158
159 return 0;
160
161out_unlock:
162 mutex_unlock(&dev->lock);
163out_rc:
164 rc_close(dev);
165 return retval;
166}
167
168static int ir_lirc_close(struct inode *inode, struct file *file)
169{
170 struct rc_dev *dev = file->private_data;
171
172 mutex_lock(&dev->lock);
173 dev->lirc_open--;
174 mutex_unlock(&dev->lock);
175
176 rc_close(dev);
177
178 return 0;
179}
180
181static ssize_t ir_lirc_transmit_ir(struct file *file, const char __user *buf,
182 size_t n, loff_t *ppos)
183{
184 struct rc_dev *dev = file->private_data;
185 unsigned int *txbuf = NULL;
186 struct ir_raw_event *raw = NULL;
187 ssize_t ret = -EINVAL;
188 size_t count;
189 ktime_t start;
190 s64 towait;
191 unsigned int duration = 0; /* signal duration in us */
192 int i;
193
194 if (!dev->registered)
195 return -ENODEV;
196
197 start = ktime_get();
198
199 if (!dev->tx_ir) {
200 ret = -EINVAL;
201 goto out;
202 }
203
204 if (dev->send_mode == LIRC_MODE_SCANCODE) {
205 struct lirc_scancode scan;
206
207 if (n != sizeof(scan))
208 return -EINVAL;
209
210 if (copy_from_user(&scan, buf, sizeof(scan)))
211 return -EFAULT;
212
213 if (scan.flags || scan.keycode || scan.timestamp)
214 return -EINVAL;
215
216 /*
217 * The scancode field in lirc_scancode is 64-bit simply
218 * to future-proof it, since there are IR protocols encode
219 * use more than 32 bits. For now only 32-bit protocols
220 * are supported.
221 */
222 if (scan.scancode > U32_MAX ||
223 !rc_validate_scancode(scan.rc_proto, scan.scancode))
224 return -EINVAL;
225
226 raw = kmalloc_array(LIRCBUF_SIZE, sizeof(*raw), GFP_KERNEL);
227 if (!raw)
228 return -ENOMEM;
229
230 ret = ir_raw_encode_scancode(scan.rc_proto, scan.scancode,
231 raw, LIRCBUF_SIZE);
232 if (ret < 0)
233 goto out;
234
235 count = ret;
236
237 txbuf = kmalloc_array(count, sizeof(unsigned int), GFP_KERNEL);
238 if (!txbuf) {
239 ret = -ENOMEM;
240 goto out;
241 }
242
243 for (i = 0; i < count; i++)
244 /* Convert from NS to US */
245 txbuf[i] = DIV_ROUND_UP(raw[i].duration, 1000);
246
247 if (dev->s_tx_carrier) {
248 int carrier = ir_raw_encode_carrier(scan.rc_proto);
249
250 if (carrier > 0)
251 dev->s_tx_carrier(dev, carrier);
252 }
253 } else {
254 if (n < sizeof(unsigned int) || n % sizeof(unsigned int))
255 return -EINVAL;
256
257 count = n / sizeof(unsigned int);
258 if (count > LIRCBUF_SIZE || count % 2 == 0)
259 return -EINVAL;
260
261 txbuf = memdup_user(buf, n);
262 if (IS_ERR(txbuf))
263 return PTR_ERR(txbuf);
264 }
265
266 for (i = 0; i < count; i++) {
267 if (txbuf[i] > IR_MAX_DURATION / 1000 - duration || !txbuf[i]) {
268 ret = -EINVAL;
269 goto out;
270 }
271
272 duration += txbuf[i];
273 }
274
275 ret = dev->tx_ir(dev, txbuf, count);
276 if (ret < 0)
277 goto out;
278
279 if (dev->send_mode == LIRC_MODE_SCANCODE) {
280 ret = n;
281 } else {
282 for (duration = i = 0; i < ret; i++)
283 duration += txbuf[i];
284
285 ret *= sizeof(unsigned int);
286
287 /*
288 * The lircd gap calculation expects the write function to
289 * wait for the actual IR signal to be transmitted before
290 * returning.
291 */
292 towait = ktime_us_delta(ktime_add_us(start, duration),
293 ktime_get());
294 if (towait > 0) {
295 set_current_state(TASK_INTERRUPTIBLE);
296 schedule_timeout(usecs_to_jiffies(towait));
297 }
298 }
299
300out:
301 kfree(txbuf);
302 kfree(raw);
303 return ret;
304}
305
306static long ir_lirc_ioctl(struct file *filep, unsigned int cmd,
307 unsigned long arg)
308{
309 struct rc_dev *dev = filep->private_data;
310 u32 __user *argp = (u32 __user *)(arg);
311 int ret = 0;
312 __u32 val = 0, tmp;
313
314 if (_IOC_DIR(cmd) & _IOC_WRITE) {
315 ret = get_user(val, argp);
316 if (ret)
317 return ret;
318 }
319
320 if (!dev->registered)
321 return -ENODEV;
322
323 switch (cmd) {
324 case LIRC_GET_FEATURES:
325 if (dev->driver_type == RC_DRIVER_SCANCODE)
326 val |= LIRC_CAN_REC_SCANCODE;
327
328 if (dev->driver_type == RC_DRIVER_IR_RAW) {
329 val |= LIRC_CAN_REC_MODE2 | LIRC_CAN_REC_SCANCODE;
330 if (dev->rx_resolution)
331 val |= LIRC_CAN_GET_REC_RESOLUTION;
332 }
333
334 if (dev->tx_ir) {
335 val |= LIRC_CAN_SEND_PULSE | LIRC_CAN_SEND_SCANCODE;
336 if (dev->s_tx_mask)
337 val |= LIRC_CAN_SET_TRANSMITTER_MASK;
338 if (dev->s_tx_carrier)
339 val |= LIRC_CAN_SET_SEND_CARRIER;
340 if (dev->s_tx_duty_cycle)
341 val |= LIRC_CAN_SET_SEND_DUTY_CYCLE;
342 }
343
344 if (dev->s_rx_carrier_range)
345 val |= LIRC_CAN_SET_REC_CARRIER |
346 LIRC_CAN_SET_REC_CARRIER_RANGE;
347
348 if (dev->s_learning_mode)
349 val |= LIRC_CAN_USE_WIDEBAND_RECEIVER;
350
351 if (dev->s_carrier_report)
352 val |= LIRC_CAN_MEASURE_CARRIER;
353
354 if (dev->max_timeout)
355 val |= LIRC_CAN_SET_REC_TIMEOUT;
356
357 break;
358
359 /* mode support */
360 case LIRC_GET_REC_MODE:
361 if (dev->driver_type == RC_DRIVER_IR_RAW_TX)
362 return -ENOTTY;
363
364 val = dev->rec_mode;
365 break;
366
367 case LIRC_SET_REC_MODE:
368 switch (dev->driver_type) {
369 case RC_DRIVER_IR_RAW_TX:
370 return -ENOTTY;
371 case RC_DRIVER_SCANCODE:
372 if (val != LIRC_MODE_SCANCODE)
373 return -EINVAL;
374 break;
375 case RC_DRIVER_IR_RAW:
376 if (!(val == LIRC_MODE_MODE2 ||
377 val == LIRC_MODE_SCANCODE))
378 return -EINVAL;
379 break;
380 }
381
382 dev->rec_mode = val;
383 return 0;
384
385 case LIRC_GET_SEND_MODE:
386 if (!dev->tx_ir)
387 return -ENOTTY;
388
389 val = dev->send_mode;
390 break;
391
392 case LIRC_SET_SEND_MODE:
393 if (!dev->tx_ir)
394 return -ENOTTY;
395
396 if (!(val == LIRC_MODE_PULSE || val == LIRC_MODE_SCANCODE))
397 return -EINVAL;
398
399 dev->send_mode = val;
400 return 0;
401
402 /* TX settings */
403 case LIRC_SET_TRANSMITTER_MASK:
404 if (!dev->s_tx_mask)
405 return -ENOTTY;
406
407 return dev->s_tx_mask(dev, val);
408
409 case LIRC_SET_SEND_CARRIER:
410 if (!dev->s_tx_carrier)
411 return -ENOTTY;
412
413 return dev->s_tx_carrier(dev, val);
414
415 case LIRC_SET_SEND_DUTY_CYCLE:
416 if (!dev->s_tx_duty_cycle)
417 return -ENOTTY;
418
419 if (val <= 0 || val >= 100)
420 return -EINVAL;
421
422 return dev->s_tx_duty_cycle(dev, val);
423
424 /* RX settings */
425 case LIRC_SET_REC_CARRIER:
426 if (!dev->s_rx_carrier_range)
427 return -ENOTTY;
428
429 if (val <= 0)
430 return -EINVAL;
431
432 return dev->s_rx_carrier_range(dev,
433 dev->carrier_low,
434 val);
435
436 case LIRC_SET_REC_CARRIER_RANGE:
437 if (!dev->s_rx_carrier_range)
438 return -ENOTTY;
439
440 if (val <= 0)
441 return -EINVAL;
442
443 dev->carrier_low = val;
444 return 0;
445
446 case LIRC_GET_REC_RESOLUTION:
447 if (!dev->rx_resolution)
448 return -ENOTTY;
449
450 val = dev->rx_resolution / 1000;
451 break;
452
453 case LIRC_SET_WIDEBAND_RECEIVER:
454 if (!dev->s_learning_mode)
455 return -ENOTTY;
456
457 return dev->s_learning_mode(dev, !!val);
458
459 case LIRC_SET_MEASURE_CARRIER_MODE:
460 if (!dev->s_carrier_report)
461 return -ENOTTY;
462
463 return dev->s_carrier_report(dev, !!val);
464
465 /* Generic timeout support */
466 case LIRC_GET_MIN_TIMEOUT:
467 if (!dev->max_timeout)
468 return -ENOTTY;
469 val = DIV_ROUND_UP(dev->min_timeout, 1000);
470 break;
471
472 case LIRC_GET_MAX_TIMEOUT:
473 if (!dev->max_timeout)
474 return -ENOTTY;
475 val = dev->max_timeout / 1000;
476 break;
477
478 case LIRC_SET_REC_TIMEOUT:
479 if (!dev->max_timeout)
480 return -ENOTTY;
481
482 /* Check for multiply overflow */
483 if (val > U32_MAX / 1000)
484 return -EINVAL;
485
486 tmp = val * 1000;
487
488 if (tmp < dev->min_timeout || tmp > dev->max_timeout)
489 return -EINVAL;
490
491 if (dev->s_timeout)
492 ret = dev->s_timeout(dev, tmp);
493 if (!ret)
494 dev->timeout = tmp;
495 break;
496
497 case LIRC_SET_REC_TIMEOUT_REPORTS:
498 if (!dev->timeout)
499 return -ENOTTY;
500
501 dev->send_timeout_reports = !!val;
502 break;
503
504 default:
505 return -ENOTTY;
506 }
507
508 if (_IOC_DIR(cmd) & _IOC_READ)
509 ret = put_user(val, argp);
510
511 return ret;
512}
513
514static unsigned int ir_lirc_poll(struct file *file,
515 struct poll_table_struct *wait)
516{
517 struct rc_dev *rcdev = file->private_data;
518 unsigned int events = 0;
519
520 poll_wait(file, &rcdev->wait_poll, wait);
521
522 if (!rcdev->registered) {
523 events = POLLHUP | POLLERR;
524 } else if (rcdev->driver_type != RC_DRIVER_IR_RAW_TX) {
525 if (rcdev->rec_mode == LIRC_MODE_SCANCODE &&
526 !kfifo_is_empty(&rcdev->scancodes))
527 events = POLLIN | POLLRDNORM;
528
529 if (rcdev->rec_mode == LIRC_MODE_MODE2 &&
530 !kfifo_is_empty(&rcdev->rawir))
531 events = POLLIN | POLLRDNORM;
532 }
533
534 return events;
535}
536
537static ssize_t ir_lirc_read_mode2(struct file *file, char __user *buffer,
538 size_t length)
539{
540 struct rc_dev *rcdev = file->private_data;
541 unsigned int copied;
542 int ret;
543
544 if (length < sizeof(unsigned int) || length % sizeof(unsigned int))
545 return -EINVAL;
546
547 do {
548 if (kfifo_is_empty(&rcdev->rawir)) {
549 if (file->f_flags & O_NONBLOCK)
550 return -EAGAIN;
551
552 ret = wait_event_interruptible(rcdev->wait_poll,
553 !kfifo_is_empty(&rcdev->rawir) ||
554 !rcdev->registered);
555 if (ret)
556 return ret;
557 }
558
559 if (!rcdev->registered)
560 return -ENODEV;
561
562 ret = mutex_lock_interruptible(&rcdev->lock);
563 if (ret)
564 return ret;
565 ret = kfifo_to_user(&rcdev->rawir, buffer, length, &copied);
566 mutex_unlock(&rcdev->lock);
567 if (ret)
568 return ret;
569 } while (copied == 0);
570
571 return copied;
572}
573
574static ssize_t ir_lirc_read_scancode(struct file *file, char __user *buffer,
575 size_t length)
576{
577 struct rc_dev *rcdev = file->private_data;
578 unsigned int copied;
579 int ret;
580
581 if (length < sizeof(struct lirc_scancode) ||
582 length % sizeof(struct lirc_scancode))
583 return -EINVAL;
584
585 do {
586 if (kfifo_is_empty(&rcdev->scancodes)) {
587 if (file->f_flags & O_NONBLOCK)
588 return -EAGAIN;
589
590 ret = wait_event_interruptible(rcdev->wait_poll,
591 !kfifo_is_empty(&rcdev->scancodes) ||
592 !rcdev->registered);
593 if (ret)
594 return ret;
595 }
596
597 if (!rcdev->registered)
598 return -ENODEV;
599
600 ret = mutex_lock_interruptible(&rcdev->lock);
601 if (ret)
602 return ret;
603 ret = kfifo_to_user(&rcdev->scancodes, buffer, length, &copied);
604 mutex_unlock(&rcdev->lock);
605 if (ret)
606 return ret;
607 } while (copied == 0);
608
609 return copied;
610}
611
612static ssize_t ir_lirc_read(struct file *file, char __user *buffer,
613 size_t length, loff_t *ppos)
614{
615 struct rc_dev *rcdev = file->private_data;
616
617 if (rcdev->driver_type == RC_DRIVER_IR_RAW_TX)
618 return -EINVAL;
619
620 if (!rcdev->registered)
621 return -ENODEV;
622
623 if (rcdev->rec_mode == LIRC_MODE_MODE2)
624 return ir_lirc_read_mode2(file, buffer, length);
625 else /* LIRC_MODE_SCANCODE */
626 return ir_lirc_read_scancode(file, buffer, length);
627}
628
629static const struct file_operations lirc_fops = {
630 .owner = THIS_MODULE,
631 .write = ir_lirc_transmit_ir,
632 .unlocked_ioctl = ir_lirc_ioctl,
633#ifdef CONFIG_COMPAT
634 .compat_ioctl = ir_lirc_ioctl,
635#endif
636 .read = ir_lirc_read,
637 .poll = ir_lirc_poll,
638 .open = ir_lirc_open,
639 .release = ir_lirc_close,
640 .llseek = no_llseek,
641};
642
39static void lirc_release_device(struct device *ld)643static void lirc_release_device(struct device *ld)
40{644{
41 struct rc_dev *rcdev = container_of(ld, struct rc_dev, lirc_dev);645 struct rc_dev *rcdev = container_of(ld, struct rc_dev, lirc_dev);
drivers/media/rc/rc-core-priv.h+0−2
@@ -279,8 +279,6 @@ void ir_lirc_raw_event(struct rc_dev *dev, struct ir_raw_event ev); · 278 unmodified lines
279void ir_lirc_scancode_event(struct rc_dev *dev, struct lirc_scancode *lsc);279void ir_lirc_scancode_event(struct rc_dev *dev, struct lirc_scancode *lsc);
280int ir_lirc_register(struct rc_dev *dev);280int ir_lirc_register(struct rc_dev *dev);
281void ir_lirc_unregister(struct rc_dev *dev);281void ir_lirc_unregister(struct rc_dev *dev);
282
283extern const struct file_operations lirc_fops;
284#else282#else
285static inline int lirc_dev_init(void) { return 0; }283static inline int lirc_dev_init(void) { return 0; }
286static inline void lirc_dev_exit(void) {}284static inline void lirc_dev_exit(void) {}