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 $
11 #ifndef _LINUX_LIRC_DEV_H
12 #define _LINUX_LIRC_DEV_H
14 #define MAX_IRCTL_DEVICES 4
17 /* #define LIRC_BUFF_POWER_OF_2 */
18 #ifdef LIRC_BUFF_POWER_OF_2
19 #define mod(n, div) ((n) & ((div) - 1))
21 #define mod(n, div) ((n) % (div))
23 #include <linux/slab.h>
27 wait_queue_head_t wait_poll
;
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
)
44 static inline int lirc_buffer_init(struct lirc_buffer
*buf
,
45 unsigned int chunk_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
;
55 buf
->data
= kmalloc(size
*chunk_size
, GFP_KERNEL
);
56 if (buf
->data
== NULL
)
58 memset(buf
->data
, 0, size
*chunk_size
);
61 static inline void lirc_buffer_free(struct lirc_buffer
*buf
)
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
)
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
,
86 spin_lock_irqsave(&buf
->lock
, *flags
);
88 static inline void lirc_buffer_unlock(struct lirc_buffer
*buf
,
91 spin_unlock_irqrestore(&buf
->lock
, *flags
);
93 static inline void lirc_buffer_clear(struct lirc_buffer
*buf
)
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
);
105 static inline void lirc_buffer_remove_1(struct lirc_buffer
*buf
)
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
,
115 memcpy(dest
, &buf
->data
[buf
->head
*buf
->chunk_size
], buf
->chunk_size
);
116 buf
->head
= mod(buf
->head
+1, buf
->size
);
119 static inline void lirc_buffer_read_1(struct lirc_buffer
*buf
,
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
,
130 memcpy(&buf
->data
[buf
->tail
*buf
->chunk_size
], orig
, buf
->chunk_size
);
131 buf
->tail
= mod(buf
->tail
+1, buf
->size
);
134 static inline void lirc_buffer_write_1(struct lirc_buffer
*buf
,
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
);
150 static inline void lirc_buffer_write_n(struct lirc_buffer
*buf
,
151 unsigned char *orig
, int count
)
156 lirc_buffer_lock(buf
, &flags
);
157 if (buf
->head
> buf
->tail
)
158 space1
= buf
->head
- buf
->tail
;
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
),
167 _lirc_buffer_write_n(buf
, orig
, count
);
169 lirc_buffer_unlock(buf
, &flags
);
177 unsigned long features
;
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,
186 struct file_operations
*fops
;
188 struct module
*owner
;
191 * this string will be used for logs
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)
199 * length of the remote control key code expressed in bits
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)
207 * it may point to any plugin data and this pointer will be passed to
208 * all callback functions
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
219 * this callback should return a pointer to the task queue which will
220 * be used for external event waiting
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
228 * set_use_inc will be called after device is opened
231 * set_use_dec will be called after device is closed
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).
239 * file_operations for drivers which don't fit the current plugin model.
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
);