Linux 4.14.5
[linux/fpc-iii.git] / include / media / lirc_dev.h
blob86d15a9b6c01b3694979bddc9d9b05844fd0f7ce
1 /*
2 * LIRC base driver
4 * by Artur Lipowski <alipowski@interia.pl>
5 * This code is licensed under GNU GPL
7 */
9 #ifndef _LINUX_LIRC_DEV_H
10 #define _LINUX_LIRC_DEV_H
12 #define MAX_IRCTL_DEVICES 8
13 #define BUFLEN 16
15 #include <linux/slab.h>
16 #include <linux/fs.h>
17 #include <linux/ioctl.h>
18 #include <linux/poll.h>
19 #include <linux/kfifo.h>
20 #include <media/lirc.h>
22 struct lirc_buffer {
23 wait_queue_head_t wait_poll;
24 spinlock_t fifo_lock;
25 unsigned int chunk_size;
26 unsigned int size; /* in chunks */
27 /* Using chunks instead of bytes pretends to simplify boundary checking
28 * And should allow for some performance fine tunning later */
29 struct kfifo fifo;
32 static inline void lirc_buffer_clear(struct lirc_buffer *buf)
34 unsigned long flags;
36 if (kfifo_initialized(&buf->fifo)) {
37 spin_lock_irqsave(&buf->fifo_lock, flags);
38 kfifo_reset(&buf->fifo);
39 spin_unlock_irqrestore(&buf->fifo_lock, flags);
40 } else
41 WARN(1, "calling %s on an uninitialized lirc_buffer\n",
42 __func__);
45 static inline int lirc_buffer_init(struct lirc_buffer *buf,
46 unsigned int chunk_size,
47 unsigned int size)
49 int ret;
51 init_waitqueue_head(&buf->wait_poll);
52 spin_lock_init(&buf->fifo_lock);
53 buf->chunk_size = chunk_size;
54 buf->size = size;
55 ret = kfifo_alloc(&buf->fifo, size * chunk_size, GFP_KERNEL);
57 return ret;
60 static inline void lirc_buffer_free(struct lirc_buffer *buf)
62 if (kfifo_initialized(&buf->fifo)) {
63 kfifo_free(&buf->fifo);
64 } else
65 WARN(1, "calling %s on an uninitialized lirc_buffer\n",
66 __func__);
69 static inline int lirc_buffer_len(struct lirc_buffer *buf)
71 int len;
72 unsigned long flags;
74 spin_lock_irqsave(&buf->fifo_lock, flags);
75 len = kfifo_len(&buf->fifo);
76 spin_unlock_irqrestore(&buf->fifo_lock, flags);
78 return len;
81 static inline int lirc_buffer_full(struct lirc_buffer *buf)
83 return lirc_buffer_len(buf) == buf->size * buf->chunk_size;
86 static inline int lirc_buffer_empty(struct lirc_buffer *buf)
88 return !lirc_buffer_len(buf);
91 static inline unsigned int lirc_buffer_read(struct lirc_buffer *buf,
92 unsigned char *dest)
94 unsigned int ret = 0;
96 if (lirc_buffer_len(buf) >= buf->chunk_size)
97 ret = kfifo_out_locked(&buf->fifo, dest, buf->chunk_size,
98 &buf->fifo_lock);
99 return ret;
103 static inline unsigned int lirc_buffer_write(struct lirc_buffer *buf,
104 unsigned char *orig)
106 unsigned int ret;
108 ret = kfifo_in_locked(&buf->fifo, orig, buf->chunk_size,
109 &buf->fifo_lock);
111 return ret;
115 * struct lirc_driver - Defines the parameters on a LIRC driver
117 * @name: this string will be used for logs
119 * @minor: indicates minor device (/dev/lirc) number for
120 * registered driver if caller fills it with negative
121 * value, then the first free minor number will be used
122 * (if available).
124 * @code_length: length of the remote control key code expressed in bits.
126 * @buffer_size: Number of FIFO buffers with @chunk_size size. If zero,
127 * creates a buffer with BUFLEN size (16 bytes).
129 * @features: lirc compatible hardware features, like LIRC_MODE_RAW,
130 * LIRC_CAN\_\*, as defined at include/media/lirc.h.
132 * @chunk_size: Size of each FIFO buffer.
134 * @data: it may point to any driver data and this pointer will
135 * be passed to all callback functions.
137 * @min_timeout: Minimum timeout for record. Valid only if
138 * LIRC_CAN_SET_REC_TIMEOUT is defined.
140 * @max_timeout: Maximum timeout for record. Valid only if
141 * LIRC_CAN_SET_REC_TIMEOUT is defined.
143 * @rbuf: if not NULL, it will be used as a read buffer, you will
144 * have to write to the buffer by other means, like irq's
145 * (see also lirc_serial.c).
147 * @rdev: Pointed to struct rc_dev associated with the LIRC
148 * device.
150 * @fops: file_operations for drivers which don't fit the current
151 * driver model.
152 * Some ioctl's can be directly handled by lirc_dev if the
153 * driver's ioctl function is NULL or if it returns
154 * -ENOIOCTLCMD (see also lirc_serial.c).
156 * @dev: pointer to the struct device associated with the LIRC
157 * device.
159 * @owner: the module owning this struct
161 struct lirc_driver {
162 char name[40];
163 int minor;
164 __u32 code_length;
165 unsigned int buffer_size; /* in chunks holding one code each */
166 __u32 features;
168 unsigned int chunk_size;
170 void *data;
171 int min_timeout;
172 int max_timeout;
173 struct lirc_buffer *rbuf;
174 struct rc_dev *rdev;
175 const struct file_operations *fops;
176 struct device *dev;
177 struct module *owner;
180 /* following functions can be called ONLY from user context
182 * returns negative value on error or minor number
183 * of the registered device if success
184 * contents of the structure pointed by p is copied
186 extern int lirc_register_driver(struct lirc_driver *d);
188 /* returns negative value on error or 0 if success
190 extern int lirc_unregister_driver(int minor);
192 /* Returns the private data stored in the lirc_driver
193 * associated with the given device file pointer.
195 void *lirc_get_pdata(struct file *file);
197 /* default file operations
198 * used by drivers if they override only some operations
200 int lirc_dev_fop_open(struct inode *inode, struct file *file);
201 int lirc_dev_fop_close(struct inode *inode, struct file *file);
202 unsigned int lirc_dev_fop_poll(struct file *file, poll_table *wait);
203 long lirc_dev_fop_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
204 ssize_t lirc_dev_fop_read(struct file *file, char __user *buffer, size_t length,
205 loff_t *ppos);
206 #endif