]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - include/media/lirc_dev.h
[media] media: lirc_dev: use an IDA instead of an array to keep track of registered...
[mirror_ubuntu-bionic-kernel.git] / include / media / lirc_dev.h
CommitLineData
4a62a5ab
JW
1/*
2 * LIRC base driver
3 *
4 * by Artur Lipowski <alipowski@interia.pl>
5 * This code is licensed under GNU GPL
6 *
7 */
8
9#ifndef _LINUX_LIRC_DEV_H
10#define _LINUX_LIRC_DEV_H
11
4a62a5ab
JW
12#define BUFLEN 16
13
4a62a5ab
JW
14#include <linux/slab.h>
15#include <linux/fs.h>
16#include <linux/ioctl.h>
17#include <linux/poll.h>
18#include <linux/kfifo.h>
19#include <media/lirc.h>
20
21struct lirc_buffer {
22 wait_queue_head_t wait_poll;
23 spinlock_t fifo_lock;
24 unsigned int chunk_size;
25 unsigned int size; /* in chunks */
26 /* Using chunks instead of bytes pretends to simplify boundary checking
27 * And should allow for some performance fine tunning later */
28 struct kfifo fifo;
4a62a5ab
JW
29};
30
31static inline void lirc_buffer_clear(struct lirc_buffer *buf)
32{
33 unsigned long flags;
34
77d381af 35 if (kfifo_initialized(&buf->fifo)) {
4a62a5ab
JW
36 spin_lock_irqsave(&buf->fifo_lock, flags);
37 kfifo_reset(&buf->fifo);
38 spin_unlock_irqrestore(&buf->fifo_lock, flags);
39 } else
40 WARN(1, "calling %s on an uninitialized lirc_buffer\n",
41 __func__);
42}
43
44static inline int lirc_buffer_init(struct lirc_buffer *buf,
45 unsigned int chunk_size,
46 unsigned int size)
47{
48 int ret;
49
50 init_waitqueue_head(&buf->wait_poll);
51 spin_lock_init(&buf->fifo_lock);
52 buf->chunk_size = chunk_size;
53 buf->size = size;
54 ret = kfifo_alloc(&buf->fifo, size * chunk_size, GFP_KERNEL);
4a62a5ab
JW
55
56 return ret;
57}
58
59static inline void lirc_buffer_free(struct lirc_buffer *buf)
60{
77d381af 61 if (kfifo_initialized(&buf->fifo)) {
4a62a5ab 62 kfifo_free(&buf->fifo);
4a62a5ab
JW
63 } else
64 WARN(1, "calling %s on an uninitialized lirc_buffer\n",
65 __func__);
66}
67
68static inline int lirc_buffer_len(struct lirc_buffer *buf)
69{
70 int len;
71 unsigned long flags;
72
73 spin_lock_irqsave(&buf->fifo_lock, flags);
74 len = kfifo_len(&buf->fifo);
75 spin_unlock_irqrestore(&buf->fifo_lock, flags);
76
77 return len;
78}
79
80static inline int lirc_buffer_full(struct lirc_buffer *buf)
81{
82 return lirc_buffer_len(buf) == buf->size * buf->chunk_size;
83}
84
85static inline int lirc_buffer_empty(struct lirc_buffer *buf)
86{
87 return !lirc_buffer_len(buf);
88}
89
4a62a5ab
JW
90static inline unsigned int lirc_buffer_read(struct lirc_buffer *buf,
91 unsigned char *dest)
92{
93 unsigned int ret = 0;
94
95 if (lirc_buffer_len(buf) >= buf->chunk_size)
96 ret = kfifo_out_locked(&buf->fifo, dest, buf->chunk_size,
97 &buf->fifo_lock);
98 return ret;
99
100}
101
102static inline unsigned int lirc_buffer_write(struct lirc_buffer *buf,
103 unsigned char *orig)
104{
105 unsigned int ret;
106
107 ret = kfifo_in_locked(&buf->fifo, orig, buf->chunk_size,
108 &buf->fifo_lock);
109
110 return ret;
111}
112
be14c5cd
MCC
113/**
114 * struct lirc_driver - Defines the parameters on a LIRC driver
115 *
116 * @name: this string will be used for logs
117 *
c3c6dd75
DH
118 * @minor: the minor device (/dev/lircX) number for a registered
119 * driver.
be14c5cd
MCC
120 *
121 * @code_length: length of the remote control key code expressed in bits.
122 *
be14c5cd 123 * @features: lirc compatible hardware features, like LIRC_MODE_RAW,
5b6137dc 124 * LIRC_CAN\_\*, as defined at include/media/lirc.h.
be14c5cd 125 *
b145ef94
DH
126 * @buffer_size: Number of FIFO buffers with @chunk_size size.
127 * Only used if @rbuf is NULL.
128 *
be14c5cd 129 * @chunk_size: Size of each FIFO buffer.
b145ef94 130 * Only used if @rbuf is NULL.
be14c5cd
MCC
131 *
132 * @data: it may point to any driver data and this pointer will
133 * be passed to all callback functions.
134 *
135 * @min_timeout: Minimum timeout for record. Valid only if
136 * LIRC_CAN_SET_REC_TIMEOUT is defined.
137 *
138 * @max_timeout: Maximum timeout for record. Valid only if
139 * LIRC_CAN_SET_REC_TIMEOUT is defined.
140 *
be14c5cd
MCC
141 * @rbuf: if not NULL, it will be used as a read buffer, you will
142 * have to write to the buffer by other means, like irq's
143 * (see also lirc_serial.c).
144 *
be14c5cd
MCC
145 * @rdev: Pointed to struct rc_dev associated with the LIRC
146 * device.
147 *
148 * @fops: file_operations for drivers which don't fit the current
149 * driver model.
150 * Some ioctl's can be directly handled by lirc_dev if the
151 * driver's ioctl function is NULL or if it returns
152 * -ENOIOCTLCMD (see also lirc_serial.c).
153 *
154 * @dev: pointer to the struct device associated with the LIRC
155 * device.
156 *
157 * @owner: the module owning this struct
c3c6dd75
DH
158 *
159 * @irctl: the struct irctl for this LIRC device.
be14c5cd 160 */
4a62a5ab
JW
161struct lirc_driver {
162 char name[40];
c3c6dd75 163 unsigned int minor;
be1f985f 164 __u32 code_length;
be1f985f 165 __u32 features;
4a62a5ab 166
b145ef94 167 unsigned int buffer_size; /* in chunks holding one code each */
4a62a5ab
JW
168 unsigned int chunk_size;
169
170 void *data;
171 int min_timeout;
172 int max_timeout;
4a62a5ab 173 struct lirc_buffer *rbuf;
ca7a722d 174 struct rc_dev *rdev;
22c00854 175 const struct file_operations *fops;
4a62a5ab
JW
176 struct device *dev;
177 struct module *owner;
c3c6dd75 178 struct irctl *irctl;
4a62a5ab
JW
179};
180
4a62a5ab
JW
181/* following functions can be called ONLY from user context
182 *
c3c6dd75 183 * returns negative value on error or zero
4a62a5ab
JW
184 * contents of the structure pointed by p is copied
185 */
c3c6dd75 186int lirc_register_driver(struct lirc_driver *d);
4a62a5ab 187
c3c6dd75 188void lirc_unregister_driver(struct lirc_driver *d);
4a62a5ab 189
615cd3fe
DH
190/* Must be called in the open fop before lirc_get_pdata() can be used */
191void lirc_init_pdata(struct inode *inode, struct file *file);
192
4a62a5ab
JW
193/* Returns the private data stored in the lirc_driver
194 * associated with the given device file pointer.
195 */
196void *lirc_get_pdata(struct file *file);
197
198/* default file operations
199 * used by drivers if they override only some operations
200 */
201int lirc_dev_fop_open(struct inode *inode, struct file *file);
202int lirc_dev_fop_close(struct inode *inode, struct file *file);
203unsigned int lirc_dev_fop_poll(struct file *file, poll_table *wait);
044e5878 204long lirc_dev_fop_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
0e835087 205ssize_t lirc_dev_fop_read(struct file *file, char __user *buffer, size_t length,
4a62a5ab 206 loff_t *ppos);
4a62a5ab 207#endif