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

Showing the changes to drivers/media/rc/lirc_dev.c (1 of 4 files). Show all

Unified · Split

drivers/media/rc/lirc_dev.c+604−0
@@ -22,11 +22,14 @@ · 21 unmodified lines
2222 #include <linux/device.h>
2323 #include <linux/idr.h>
2424 #include <linux/poll.h>
25+#include <linux/sched.h>
26+#include <linux/wait.h>
2527
2628 #include "rc-core-priv.h"
2729 #include <media/lirc.h>
2830
2931 #define LOGHEAD "lirc_dev (%s[%d]): "
32+#define LIRCBUF_SIZE 256
3033
3134 static dev_t lirc_base_dev;
3235
@@ -36,6 +39,607 @@ static DEFINE_IDA(lirc_ida); · 3 unmodified lines
3639 /* Only used for sysfs but defined to void otherwise */
3740 static struct class *lirc_class;
3841
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+ */
48+void 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+ */
115+void 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+}
122+EXPORT_SYMBOL_GPL(ir_lirc_scancode_event);
123+
124+static 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+
161+out_unlock:
162+ mutex_unlock(&dev->lock);
163+out_rc:
164+ rc_close(dev);
165+ return retval;
166+}
167+
168+static 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+
181+static 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+
300+out:
301+ kfree(txbuf);
302+ kfree(raw);
303+ return ret;
304+}
305+
306+static 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+
514+static 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+
537+static 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+
574+static 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+
612+static 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+
629+static 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+
39643 static void lirc_release_device(struct device *ld)
40644 {
41645 struct rc_dev *rcdev = container_of(ld, struct rc_dev, lirc_dev);