Linux 4.1.18
[linux/fpc-iii.git] / include / media / lirc_dev.h
blob05e7ad5d2c8b6650f3f69152c3b38252e2b8921d
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 #define mod(n, div) ((n) % (div))
17 #include <linux/slab.h>
18 #include <linux/fs.h>
19 #include <linux/ioctl.h>
20 #include <linux/poll.h>
21 #include <linux/kfifo.h>
22 #include <media/lirc.h>
24 struct lirc_buffer {
25 wait_queue_head_t wait_poll;
26 spinlock_t fifo_lock;
27 unsigned int chunk_size;
28 unsigned int size; /* in chunks */
29 /* Using chunks instead of bytes pretends to simplify boundary checking
30 * And should allow for some performance fine tunning later */
31 struct kfifo fifo;
34 static inline void lirc_buffer_clear(struct lirc_buffer *buf)
36 unsigned long flags;
38 if (kfifo_initialized(&buf->fifo)) {
39 spin_lock_irqsave(&buf->fifo_lock, flags);
40 kfifo_reset(&buf->fifo);
41 spin_unlock_irqrestore(&buf->fifo_lock, flags);
42 } else
43 WARN(1, "calling %s on an uninitialized lirc_buffer\n",
44 __func__);
47 static inline int lirc_buffer_init(struct lirc_buffer *buf,
48 unsigned int chunk_size,
49 unsigned int size)
51 int ret;
53 init_waitqueue_head(&buf->wait_poll);
54 spin_lock_init(&buf->fifo_lock);
55 buf->chunk_size = chunk_size;
56 buf->size = size;
57 ret = kfifo_alloc(&buf->fifo, size * chunk_size, GFP_KERNEL);
59 return ret;
62 static inline void lirc_buffer_free(struct lirc_buffer *buf)
64 if (kfifo_initialized(&buf->fifo)) {
65 kfifo_free(&buf->fifo);
66 } else
67 WARN(1, "calling %s on an uninitialized lirc_buffer\n",
68 __func__);
71 static inline int lirc_buffer_len(struct lirc_buffer *buf)
73 int len;
74 unsigned long flags;
76 spin_lock_irqsave(&buf->fifo_lock, flags);
77 len = kfifo_len(&buf->fifo);
78 spin_unlock_irqrestore(&buf->fifo_lock, flags);
80 return len;
83 static inline int lirc_buffer_full(struct lirc_buffer *buf)
85 return lirc_buffer_len(buf) == buf->size * buf->chunk_size;
88 static inline int lirc_buffer_empty(struct lirc_buffer *buf)
90 return !lirc_buffer_len(buf);
93 static inline int lirc_buffer_available(struct lirc_buffer *buf)
95 return buf->size - (lirc_buffer_len(buf) / buf->chunk_size);
98 static inline unsigned int lirc_buffer_read(struct lirc_buffer *buf,
99 unsigned char *dest)
101 unsigned int ret = 0;
103 if (lirc_buffer_len(buf) >= buf->chunk_size)
104 ret = kfifo_out_locked(&buf->fifo, dest, buf->chunk_size,
105 &buf->fifo_lock);
106 return ret;
110 static inline unsigned int lirc_buffer_write(struct lirc_buffer *buf,
111 unsigned char *orig)
113 unsigned int ret;
115 ret = kfifo_in_locked(&buf->fifo, orig, buf->chunk_size,
116 &buf->fifo_lock);
118 return ret;
121 struct lirc_driver {
122 char name[40];
123 int minor;
124 __u32 code_length;
125 unsigned int buffer_size; /* in chunks holding one code each */
126 int sample_rate;
127 __u32 features;
129 unsigned int chunk_size;
131 void *data;
132 int min_timeout;
133 int max_timeout;
134 int (*add_to_buf) (void *data, struct lirc_buffer *buf);
135 struct lirc_buffer *rbuf;
136 int (*set_use_inc) (void *data);
137 void (*set_use_dec) (void *data);
138 struct rc_dev *rdev;
139 const struct file_operations *fops;
140 struct device *dev;
141 struct module *owner;
144 /* name:
145 * this string will be used for logs
147 * minor:
148 * indicates minor device (/dev/lirc) number for registered driver
149 * if caller fills it with negative value, then the first free minor
150 * number will be used (if available)
152 * code_length:
153 * length of the remote control key code expressed in bits
155 * sample_rate:
157 * data:
158 * it may point to any driver data and this pointer will be passed to
159 * all callback functions
161 * add_to_buf:
162 * add_to_buf will be called after specified period of the time or
163 * triggered by the external event, this behavior depends on value of
164 * the sample_rate this function will be called in user context. This
165 * routine should return 0 if data was added to the buffer and
166 * -ENODATA if none was available. This should add some number of bits
167 * evenly divisible by code_length to the buffer
169 * rbuf:
170 * if not NULL, it will be used as a read buffer, you will have to
171 * write to the buffer by other means, like irq's (see also
172 * lirc_serial.c).
174 * set_use_inc:
175 * set_use_inc will be called after device is opened
177 * set_use_dec:
178 * set_use_dec will be called after device is closed
180 * fops:
181 * file_operations for drivers which don't fit the current driver model.
183 * Some ioctl's can be directly handled by lirc_dev if the driver's
184 * ioctl function is NULL or if it returns -ENOIOCTLCMD (see also
185 * lirc_serial.c).
187 * owner:
188 * the module owning this struct
193 /* following functions can be called ONLY from user context
195 * returns negative value on error or minor number
196 * of the registered device if success
197 * contents of the structure pointed by p is copied
199 extern int lirc_register_driver(struct lirc_driver *d);
201 /* returns negative value on error or 0 if success
203 extern int lirc_unregister_driver(int minor);
205 /* Returns the private data stored in the lirc_driver
206 * associated with the given device file pointer.
208 void *lirc_get_pdata(struct file *file);
210 /* default file operations
211 * used by drivers if they override only some operations
213 int lirc_dev_fop_open(struct inode *inode, struct file *file);
214 int lirc_dev_fop_close(struct inode *inode, struct file *file);
215 unsigned int lirc_dev_fop_poll(struct file *file, poll_table *wait);
216 long lirc_dev_fop_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
217 ssize_t lirc_dev_fop_read(struct file *file, char __user *buffer, size_t length,
218 loff_t *ppos);
219 ssize_t lirc_dev_fop_write(struct file *file, const char __user *buffer,
220 size_t length, loff_t *ppos);
222 #endif