4
4
* (L) by Artur Lipowski <alipowski@interia.pl>
5
5
* This code is licensed under GNU GPL
7
* $Id: lirc_dev.h,v 1.22 2008/01/13 10:45:02 lirc Exp $
7
* $Id: lirc_dev.h,v 1.37 2009/03/15 09:34:00 lirc Exp $
11
11
#ifndef _LINUX_LIRC_DEV_H
12
12
#define _LINUX_LIRC_DEV_H
14
#ifndef LIRC_REMOVE_DURING_EXPORT
15
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 11)
16
/* when was it really introduced? */
17
#define LIRC_HAVE_KFIFO
14
20
#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
23
#define mod(n, div) ((n) % (div))
23
25
#include <linux/slab.h>
24
26
#include <linux/fs.h>
27
#ifdef LIRC_HAVE_KFIFO
28
#include <linux/kfifo.h>
26
31
struct lirc_buffer {
27
32
wait_queue_head_t wait_poll;
31
34
unsigned int chunk_size;
32
35
unsigned int size; /* in chunks */
36
/* Using chunks instead of bytes pretends to simplify boundary checking
37
* And should allow for some performance fine tunning later */
38
#ifdef LIRC_HAVE_KFIFO
33
41
unsigned int fill; /* in chunks */
34
42
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 */
46
#ifndef LIRC_HAVE_KFIFO
47
static inline void lirc_buffer_lock(struct lirc_buffer *buf,
50
spin_lock_irqsave(&buf->lock, *flags);
52
static inline void lirc_buffer_unlock(struct lirc_buffer *buf,
55
spin_unlock_irqrestore(&buf->lock, *flags);
38
57
static inline void _lirc_buffer_clear(struct lirc_buffer *buf)
64
static inline void lirc_buffer_clear(struct lirc_buffer *buf)
66
#ifdef LIRC_HAVE_KFIFO
68
kfifo_reset(buf->fifo);
71
lirc_buffer_lock(buf, &flags);
72
_lirc_buffer_clear(buf);
73
lirc_buffer_unlock(buf, &flags);
44
76
static inline int lirc_buffer_init(struct lirc_buffer *buf,
45
77
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
80
init_waitqueue_head(&buf->wait_poll);
51
81
spin_lock_init(&buf->lock);
82
#ifndef LIRC_HAVE_KFIFO
52
83
_lirc_buffer_clear(buf);
53
85
buf->chunk_size = chunk_size;
87
#ifdef LIRC_HAVE_KFIFO
88
buf->fifo = kfifo_alloc(size*chunk_size, GFP_KERNEL, &buf->lock);
55
92
buf->data = kmalloc(size*chunk_size, GFP_KERNEL);
56
93
if (buf->data == NULL)
58
95
memset(buf->data, 0, size*chunk_size);
61
99
static inline void lirc_buffer_free(struct lirc_buffer *buf)
101
#ifdef LIRC_HAVE_KFIFO
103
kfifo_free(buf->fifo);
68
110
buf->chunk_size = 0;
114
#ifndef LIRC_HAVE_KFIFO
115
static inline int _lirc_buffer_full(struct lirc_buffer *buf)
117
return (buf->fill >= buf->size);
71
120
static inline int lirc_buffer_full(struct lirc_buffer *buf)
73
return (buf->fill >= buf->size);
122
#ifdef LIRC_HAVE_KFIFO
123
return kfifo_len(buf->fifo) == buf->size * buf->chunk_size;
127
lirc_buffer_lock(buf, &flags);
128
ret = _lirc_buffer_full(buf);
129
lirc_buffer_unlock(buf, &flags);
133
#ifndef LIRC_HAVE_KFIFO
134
static inline int _lirc_buffer_empty(struct lirc_buffer *buf)
75
139
static inline int lirc_buffer_empty(struct lirc_buffer *buf)
141
#ifdef LIRC_HAVE_KFIFO
142
return !kfifo_len(buf->fifo);
146
lirc_buffer_lock(buf, &flags);
147
ret = _lirc_buffer_empty(buf);
148
lirc_buffer_unlock(buf, &flags);
152
#ifndef LIRC_HAVE_KFIFO
153
static inline int _lirc_buffer_available(struct lirc_buffer *buf)
155
return (buf->size - buf->fill);
79
158
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);
160
#ifdef LIRC_HAVE_KFIFO
161
return buf->size - (kfifo_len(buf->fifo) / buf->chunk_size);
165
lirc_buffer_lock(buf, &flags);
166
ret = _lirc_buffer_available(buf);
167
lirc_buffer_unlock(buf, &flags);
171
#ifndef LIRC_HAVE_KFIFO
112
172
static inline void _lirc_buffer_read_1(struct lirc_buffer *buf,
115
175
memcpy(dest, &buf->data[buf->head*buf->chunk_size], buf->chunk_size);
116
176
buf->head = mod(buf->head+1, buf->size);
119
static inline void lirc_buffer_read_1(struct lirc_buffer *buf,
180
static inline void lirc_buffer_read(struct lirc_buffer *buf,
183
#ifdef LIRC_HAVE_KFIFO
184
if (kfifo_len(buf->fifo) >= buf->chunk_size)
185
kfifo_get(buf->fifo, dest, buf->chunk_size);
122
187
unsigned long flags;
123
188
lirc_buffer_lock(buf, &flags);
124
189
_lirc_buffer_read_1(buf, dest);
125
190
lirc_buffer_unlock(buf, &flags);
193
#ifndef LIRC_HAVE_KFIFO
127
194
static inline void _lirc_buffer_write_1(struct lirc_buffer *buf,
128
195
unsigned char *orig)
131
198
buf->tail = mod(buf->tail+1, buf->size);
134
static inline void lirc_buffer_write_1(struct lirc_buffer *buf,
202
static inline void lirc_buffer_write(struct lirc_buffer *buf,
205
#ifdef LIRC_HAVE_KFIFO
206
kfifo_put(buf->fifo, orig, buf->chunk_size);
137
208
unsigned long flags;
138
209
lirc_buffer_lock(buf, &flags);
139
210
_lirc_buffer_write_1(buf, orig);
140
211
lirc_buffer_unlock(buf, &flags);
214
#ifndef LIRC_HAVE_KFIFO
142
215
static inline void _lirc_buffer_write_n(struct lirc_buffer *buf,
143
216
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
219
if (buf->head > buf->tail)
158
220
space1 = buf->head - buf->tail;
160
222
space1 = buf->size - buf->tail;
162
224
if (count > space1) {
163
_lirc_buffer_write_n(buf, orig, space1);
164
_lirc_buffer_write_n(buf, orig+(space1*buf->chunk_size),
225
memcpy(&buf->data[buf->tail * buf->chunk_size], orig,
226
space1 * buf->chunk_size);
227
memcpy(&buf->data[0], orig + (space1 * buf->chunk_size),
228
(count - space1) * buf->chunk_size);
167
_lirc_buffer_write_n(buf, orig, count);
230
memcpy(&buf->data[buf->tail * buf->chunk_size], orig,
231
count * buf->chunk_size);
233
buf->tail = mod(buf->tail + count, buf->size);
237
static inline void lirc_buffer_write_n(struct lirc_buffer *buf,
238
unsigned char *orig, int count)
240
#ifdef LIRC_HAVE_KFIFO
241
kfifo_put(buf->fifo, orig, count * buf->chunk_size);
244
lirc_buffer_lock(buf, &flags);
245
_lirc_buffer_write_n(buf, orig, count);
169
246
lirc_buffer_unlock(buf, &flags);
253
unsigned long code_length;
254
unsigned int buffer_size; /* in chunks holding one code each */
177
256
unsigned long features;
179
258
int (*add_to_buf) (void *data, struct lirc_buffer *buf);
259
#ifndef LIRC_REMOVE_DURING_EXPORT
180
260
wait_queue_head_t* (*get_queue) (void *data);
181
262
struct lirc_buffer *rbuf;
182
263
int (*set_use_inc) (void *data);
183
264
void (*set_use_dec) (void *data);
184
int (*ioctl) (struct inode *, struct file *, unsigned int,
186
265
struct file_operations *fops;
187
266
struct device *dev;
188
267
struct module *owner;
231
310
* 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.
313
* file_operations for drivers which don't fit the current driver model.
315
* Some ioctl's can be directly handled by lirc_dev if the driver's
316
* ioctl function is NULL or if it returns -ENOIOCTLCMD (see also
242
320
* the module owning this struct
249
327
* returns negative value on error or minor number
250
328
* of the registered device if success
251
* contens of the structure pointed by p is copied
329
* contents of the structure pointed by d is copied
253
extern int lirc_register_plugin(struct lirc_plugin *p);
331
extern int lirc_register_driver(struct lirc_driver *d);
255
333
/* returns negative value on error or 0 if success
257
extern int lirc_unregister_plugin(int minor);
335
extern int lirc_unregister_driver(int minor);
259
/* Returns the private data stored in the lirc_plugin
337
/* Returns the private data stored in the lirc_driver
260
338
* associated with the given device file pointer.
262
340
void *lirc_get_pdata(struct file *file);