blob: 0ab59a571fee834416883f8d84d685269424489a [file] [log] [blame]
Jarod Wilson4a62a5a2010-07-03 01:06:57 -03001/*
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
Jarod Wilson8de111e22011-05-27 16:56:50 -030012#define MAX_IRCTL_DEVICES 8
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030013#define BUFLEN 16
14
15#define mod(n, div) ((n) % (div))
16
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>
23
24struct 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;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030032};
33
34static inline void lirc_buffer_clear(struct lirc_buffer *buf)
35{
36 unsigned long flags;
37
Martin Kaiser77d381a2014-10-28 18:51:01 -030038 if (kfifo_initialized(&buf->fifo)) {
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030039 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__);
45}
46
47static inline int lirc_buffer_init(struct lirc_buffer *buf,
48 unsigned int chunk_size,
49 unsigned int size)
50{
51 int ret;
52
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);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030058
59 return ret;
60}
61
62static inline void lirc_buffer_free(struct lirc_buffer *buf)
63{
Martin Kaiser77d381a2014-10-28 18:51:01 -030064 if (kfifo_initialized(&buf->fifo)) {
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030065 kfifo_free(&buf->fifo);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030066 } else
67 WARN(1, "calling %s on an uninitialized lirc_buffer\n",
68 __func__);
69}
70
71static inline int lirc_buffer_len(struct lirc_buffer *buf)
72{
73 int len;
74 unsigned long flags;
75
76 spin_lock_irqsave(&buf->fifo_lock, flags);
77 len = kfifo_len(&buf->fifo);
78 spin_unlock_irqrestore(&buf->fifo_lock, flags);
79
80 return len;
81}
82
83static inline int lirc_buffer_full(struct lirc_buffer *buf)
84{
85 return lirc_buffer_len(buf) == buf->size * buf->chunk_size;
86}
87
88static inline int lirc_buffer_empty(struct lirc_buffer *buf)
89{
90 return !lirc_buffer_len(buf);
91}
92
93static inline int lirc_buffer_available(struct lirc_buffer *buf)
94{
95 return buf->size - (lirc_buffer_len(buf) / buf->chunk_size);
96}
97
98static inline unsigned int lirc_buffer_read(struct lirc_buffer *buf,
99 unsigned char *dest)
100{
101 unsigned int ret = 0;
102
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;
107
108}
109
110static inline unsigned int lirc_buffer_write(struct lirc_buffer *buf,
111 unsigned char *orig)
112{
113 unsigned int ret;
114
115 ret = kfifo_in_locked(&buf->fifo, orig, buf->chunk_size,
116 &buf->fifo_lock);
117
118 return ret;
119}
120
Mauro Carvalho Chehabbe14c5c2015-10-05 13:35:37 -0300121/**
122 * struct lirc_driver - Defines the parameters on a LIRC driver
123 *
124 * @name: this string will be used for logs
125 *
126 * @minor: indicates minor device (/dev/lirc) number for
127 * registered driver if caller fills it with negative
128 * value, then the first free minor number will be used
129 * (if available).
130 *
131 * @code_length: length of the remote control key code expressed in bits.
132 *
133 * @buffer_size: Number of FIFO buffers with @chunk_size size. If zero,
134 * creates a buffer with BUFLEN size (16 bytes).
135 *
136 * @sample_rate: if zero, the device will wait for an event with a new
137 * code to be parsed. Otherwise, specifies the sample
138 * rate for polling. Value should be between 0
139 * and HZ. If equal to HZ, it would mean one polling per
140 * second.
141 *
142 * @features: lirc compatible hardware features, like LIRC_MODE_RAW,
143 * LIRC_CAN_*, as defined at include/media/lirc.h.
144 *
145 * @chunk_size: Size of each FIFO buffer.
146 *
147 * @data: it may point to any driver data and this pointer will
148 * be passed to all callback functions.
149 *
150 * @min_timeout: Minimum timeout for record. Valid only if
151 * LIRC_CAN_SET_REC_TIMEOUT is defined.
152 *
153 * @max_timeout: Maximum timeout for record. Valid only if
154 * LIRC_CAN_SET_REC_TIMEOUT is defined.
155 *
156 * @add_to_buf: add_to_buf will be called after specified period of the
157 * time or triggered by the external event, this behavior
158 * depends on value of the sample_rate this function will
159 * be called in user context. This routine should return
160 * 0 if data was added to the buffer and -ENODATA if none
161 * was available. This should add some number of bits
162 * evenly divisible by code_length to the buffer.
163 *
164 * @rbuf: if not NULL, it will be used as a read buffer, you will
165 * have to write to the buffer by other means, like irq's
166 * (see also lirc_serial.c).
167 *
168 * @set_use_inc: set_use_inc will be called after device is opened
169 *
170 * @set_use_dec: set_use_dec will be called after device is closed
171 *
172 * @rdev: Pointed to struct rc_dev associated with the LIRC
173 * device.
174 *
175 * @fops: file_operations for drivers which don't fit the current
176 * driver model.
177 * Some ioctl's can be directly handled by lirc_dev if the
178 * driver's ioctl function is NULL or if it returns
179 * -ENOIOCTLCMD (see also lirc_serial.c).
180 *
181 * @dev: pointer to the struct device associated with the LIRC
182 * device.
183 *
184 * @owner: the module owning this struct
185 */
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300186struct lirc_driver {
187 char name[40];
188 int minor;
Jarod Wilsonbe1f9852010-10-08 17:24:21 -0300189 __u32 code_length;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300190 unsigned int buffer_size; /* in chunks holding one code each */
191 int sample_rate;
Jarod Wilsonbe1f9852010-10-08 17:24:21 -0300192 __u32 features;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300193
194 unsigned int chunk_size;
195
196 void *data;
197 int min_timeout;
198 int max_timeout;
Mauro Carvalho Chehab70964102015-10-05 13:50:42 -0300199 int (*add_to_buf)(void *data, struct lirc_buffer *buf);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300200 struct lirc_buffer *rbuf;
Mauro Carvalho Chehab70964102015-10-05 13:50:42 -0300201 int (*set_use_inc)(void *data);
202 void (*set_use_dec)(void *data);
Srinivas Kandagatlaca7a7222013-07-22 04:23:07 -0300203 struct rc_dev *rdev;
Geert Uytterhoeven22c00852010-09-30 16:55:07 -0300204 const struct file_operations *fops;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300205 struct device *dev;
206 struct module *owner;
207};
208
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300209/* following functions can be called ONLY from user context
210 *
211 * returns negative value on error or minor number
212 * of the registered device if success
213 * contents of the structure pointed by p is copied
214 */
215extern int lirc_register_driver(struct lirc_driver *d);
216
217/* returns negative value on error or 0 if success
218*/
219extern int lirc_unregister_driver(int minor);
220
221/* Returns the private data stored in the lirc_driver
222 * associated with the given device file pointer.
223 */
224void *lirc_get_pdata(struct file *file);
225
226/* default file operations
227 * used by drivers if they override only some operations
228 */
229int lirc_dev_fop_open(struct inode *inode, struct file *file);
230int lirc_dev_fop_close(struct inode *inode, struct file *file);
231unsigned int lirc_dev_fop_poll(struct file *file, poll_table *wait);
Arnd Bergmann044e5872010-08-02 15:43:35 -0300232long lirc_dev_fop_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
Dan Carpenter0e835082010-11-17 02:13:39 -0300233ssize_t lirc_dev_fop_read(struct file *file, char __user *buffer, size_t length,
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300234 loff_t *ppos);
Dan Carpenter0e835082010-11-17 02:13:39 -0300235ssize_t lirc_dev_fop_write(struct file *file, const char __user *buffer,
236 size_t length, loff_t *ppos);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300237
238#endif