Added lirc.
[irreco.git] / lirc-0.8.4a / drivers / lirc_dev / lirc_dev.h
blobf9246a102823b5f70bdfabf14d30e8fc66d8f7aa
1 /*
2 * LIRC base driver
4 * (L) by Artur Lipowski <alipowski@interia.pl>
5 * This code is licensed under GNU GPL
7 * $Id: lirc_dev.h,v 1.22 2008/01/13 10:45:02 lirc Exp $
9 */
11 #ifndef _LINUX_LIRC_DEV_H
12 #define _LINUX_LIRC_DEV_H
14 #define MAX_IRCTL_DEVICES 4
15 #define BUFLEN 16
17 /* #define LIRC_BUFF_POWER_OF_2 */
18 #ifdef LIRC_BUFF_POWER_OF_2
19 #define mod(n, div) ((n) & ((div) - 1))
20 #else
21 #define mod(n, div) ((n) % (div))
22 #endif
23 #include <linux/slab.h>
24 #include <linux/fs.h>
26 struct lirc_buffer {
27 wait_queue_head_t wait_poll;
28 spinlock_t lock;
30 unsigned char *data;
31 unsigned int chunk_size;
32 unsigned int size; /* in chunks */
33 unsigned int fill; /* in chunks */
34 int head, tail; /* in chunks */
35 /* Using chunks instead of bytes pretends to simplify boundary checking
36 * And should allow for some performance fine tunning later */
38 static inline void _lirc_buffer_clear(struct lirc_buffer *buf)
40 buf->head = 0;
41 buf->tail = 0;
42 buf->fill = 0;
44 static inline int lirc_buffer_init(struct lirc_buffer *buf,
45 unsigned int chunk_size,
46 unsigned int size)
48 /* Adjusting size to the next power of 2 would allow for
49 * inconditional LIRC_BUFF_POWER_OF_2 optimization */
50 init_waitqueue_head(&buf->wait_poll);
51 spin_lock_init(&buf->lock);
52 _lirc_buffer_clear(buf);
53 buf->chunk_size = chunk_size;
54 buf->size = size;
55 buf->data = kmalloc(size*chunk_size, GFP_KERNEL);
56 if (buf->data == NULL)
57 return -1;
58 memset(buf->data, 0, size*chunk_size);
59 return 0;
61 static inline void lirc_buffer_free(struct lirc_buffer *buf)
63 kfree(buf->data);
64 buf->data = NULL;
65 buf->head = 0;
66 buf->tail = 0;
67 buf->fill = 0;
68 buf->chunk_size = 0;
69 buf->size = 0;
71 static inline int lirc_buffer_full(struct lirc_buffer *buf)
73 return (buf->fill >= buf->size);
75 static inline int lirc_buffer_empty(struct lirc_buffer *buf)
77 return !(buf->fill);
79 static inline int lirc_buffer_available(struct lirc_buffer *buf)
81 return (buf->size - buf->fill);
83 static inline void lirc_buffer_lock(struct lirc_buffer *buf,
84 unsigned long *flags)
86 spin_lock_irqsave(&buf->lock, *flags);
88 static inline void lirc_buffer_unlock(struct lirc_buffer *buf,
89 unsigned long *flags)
91 spin_unlock_irqrestore(&buf->lock, *flags);
93 static inline void lirc_buffer_clear(struct lirc_buffer *buf)
95 unsigned long flags;
96 lirc_buffer_lock(buf, &flags);
97 _lirc_buffer_clear(buf);
98 lirc_buffer_unlock(buf, &flags);
100 static inline void _lirc_buffer_remove_1(struct lirc_buffer *buf)
102 buf->head = mod(buf->head+1, buf->size);
103 buf->fill -= 1;
105 static inline void lirc_buffer_remove_1(struct lirc_buffer *buf)
107 unsigned long flags;
108 lirc_buffer_lock(buf, &flags);
109 _lirc_buffer_remove_1(buf);
110 lirc_buffer_unlock(buf, &flags);
112 static inline void _lirc_buffer_read_1(struct lirc_buffer *buf,
113 unsigned char *dest)
115 memcpy(dest, &buf->data[buf->head*buf->chunk_size], buf->chunk_size);
116 buf->head = mod(buf->head+1, buf->size);
117 buf->fill -= 1;
119 static inline void lirc_buffer_read_1(struct lirc_buffer *buf,
120 unsigned char *dest)
122 unsigned long flags;
123 lirc_buffer_lock(buf, &flags);
124 _lirc_buffer_read_1(buf, dest);
125 lirc_buffer_unlock(buf, &flags);
127 static inline void _lirc_buffer_write_1(struct lirc_buffer *buf,
128 unsigned char *orig)
130 memcpy(&buf->data[buf->tail*buf->chunk_size], orig, buf->chunk_size);
131 buf->tail = mod(buf->tail+1, buf->size);
132 buf->fill++;
134 static inline void lirc_buffer_write_1(struct lirc_buffer *buf,
135 unsigned char *orig)
137 unsigned long flags;
138 lirc_buffer_lock(buf, &flags);
139 _lirc_buffer_write_1(buf, orig);
140 lirc_buffer_unlock(buf, &flags);
142 static inline void _lirc_buffer_write_n(struct lirc_buffer *buf,
143 unsigned char *orig, int count)
145 memcpy(&buf->data[buf->tail * buf->chunk_size], orig,
146 count * buf->chunk_size);
147 buf->tail = mod(buf->tail + count, buf->size);
148 buf->fill += count;
150 static inline void lirc_buffer_write_n(struct lirc_buffer *buf,
151 unsigned char *orig, int count)
153 unsigned long flags;
154 int space1;
156 lirc_buffer_lock(buf, &flags);
157 if (buf->head > buf->tail)
158 space1 = buf->head - buf->tail;
159 else
160 space1 = buf->size - buf->tail;
162 if (count > space1) {
163 _lirc_buffer_write_n(buf, orig, space1);
164 _lirc_buffer_write_n(buf, orig+(space1*buf->chunk_size),
165 count-space1);
166 } else {
167 _lirc_buffer_write_n(buf, orig, count);
169 lirc_buffer_unlock(buf, &flags);
172 struct lirc_plugin {
173 char name[40];
174 int minor;
175 int code_length;
176 int sample_rate;
177 unsigned long features;
178 void *data;
179 int (*add_to_buf) (void *data, struct lirc_buffer *buf);
180 wait_queue_head_t* (*get_queue) (void *data);
181 struct lirc_buffer *rbuf;
182 int (*set_use_inc) (void *data);
183 void (*set_use_dec) (void *data);
184 int (*ioctl) (struct inode *, struct file *, unsigned int,
185 unsigned long);
186 struct file_operations *fops;
187 struct device *dev;
188 struct module *owner;
190 /* name:
191 * this string will be used for logs
193 * minor:
194 * indicates minor device (/dev/lirc) number for registered plugin
195 * if caller fills it with negative value, then the first free minor
196 * number will be used (if available)
198 * code_length:
199 * length of the remote control key code expressed in bits
201 * sample_rate:
202 * sample_rate equal to 0 means that no polling will be performed and
203 * add_to_buf will be triggered by external events (through task queue
204 * returned by get_queue)
206 * data:
207 * it may point to any plugin data and this pointer will be passed to
208 * all callback functions
210 * add_to_buf:
211 * add_to_buf will be called after specified period of the time or
212 * triggered by the external event, this behavior depends on value of
213 * the sample_rate this function will be called in user context. This
214 * routine should return 0 if data was added to the buffer and
215 * -ENODATA if none was available. This should add some number of bits
216 * evenly divisible by code_length to the buffer
218 * get_queue:
219 * this callback should return a pointer to the task queue which will
220 * be used for external event waiting
222 * rbuf:
223 * if not NULL, it will be used as a read buffer, you will have to
224 * write to the buffer by other means, like irq's (see also
225 * lirc_serial.c).
227 * set_use_inc:
228 * set_use_inc will be called after device is opened
230 * set_use_dec:
231 * set_use_dec will be called after device is closed
233 * ioctl:
234 * Some ioctl's can be directly handled by lirc_dev but will be
235 * forwared here if not NULL and only handled if it returns
236 * -ENOIOCTLCMD (see also lirc_serial.c).
238 * fops:
239 * file_operations for drivers which don't fit the current plugin model.
241 * owner:
242 * the module owning this struct
247 /* following functions can be called ONLY from user context
249 * returns negative value on error or minor number
250 * of the registered device if success
251 * contens of the structure pointed by p is copied
253 extern int lirc_register_plugin(struct lirc_plugin *p);
255 /* returns negative value on error or 0 if success
257 extern int lirc_unregister_plugin(int minor);
259 /* Returns the private data stored in the lirc_plugin
260 * associated with the given device file pointer.
262 void *lirc_get_pdata(struct file *file);
264 #endif