4 * by Artur Lipowski <alipowski@interia.pl>
5 * This code is licensed under GNU GPL
9 #ifndef _LINUX_LIRC_DEV_H
10 #define _LINUX_LIRC_DEV_H
12 #define MAX_IRCTL_DEVICES 4
15 #define mod(n, div) ((n) % (div))
17 #include <linux/slab.h>
19 #include <linux/ioctl.h>
20 #include <linux/poll.h>
21 #include <linux/kfifo.h>
22 #include <media/lirc.h>
25 wait_queue_head_t wait_poll
;
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 */
35 static inline void lirc_buffer_clear(struct lirc_buffer
*buf
)
39 if (buf
->fifo_initialized
) {
40 spin_lock_irqsave(&buf
->fifo_lock
, flags
);
41 kfifo_reset(&buf
->fifo
);
42 spin_unlock_irqrestore(&buf
->fifo_lock
, flags
);
44 WARN(1, "calling %s on an uninitialized lirc_buffer\n",
48 static inline int lirc_buffer_init(struct lirc_buffer
*buf
,
49 unsigned int chunk_size
,
54 init_waitqueue_head(&buf
->wait_poll
);
55 spin_lock_init(&buf
->fifo_lock
);
56 buf
->chunk_size
= chunk_size
;
58 ret
= kfifo_alloc(&buf
->fifo
, size
* chunk_size
, GFP_KERNEL
);
60 buf
->fifo_initialized
= 1;
65 static inline void lirc_buffer_free(struct lirc_buffer
*buf
)
67 if (buf
->fifo_initialized
) {
68 kfifo_free(&buf
->fifo
);
69 buf
->fifo_initialized
= 0;
71 WARN(1, "calling %s on an uninitialized lirc_buffer\n",
75 static inline int lirc_buffer_len(struct lirc_buffer
*buf
)
80 spin_lock_irqsave(&buf
->fifo_lock
, flags
);
81 len
= kfifo_len(&buf
->fifo
);
82 spin_unlock_irqrestore(&buf
->fifo_lock
, flags
);
87 static inline int lirc_buffer_full(struct lirc_buffer
*buf
)
89 return lirc_buffer_len(buf
) == buf
->size
* buf
->chunk_size
;
92 static inline int lirc_buffer_empty(struct lirc_buffer
*buf
)
94 return !lirc_buffer_len(buf
);
97 static inline int lirc_buffer_available(struct lirc_buffer
*buf
)
99 return buf
->size
- (lirc_buffer_len(buf
) / buf
->chunk_size
);
102 static inline unsigned int lirc_buffer_read(struct lirc_buffer
*buf
,
105 unsigned int ret
= 0;
107 if (lirc_buffer_len(buf
) >= buf
->chunk_size
)
108 ret
= kfifo_out_locked(&buf
->fifo
, dest
, buf
->chunk_size
,
114 static inline unsigned int lirc_buffer_write(struct lirc_buffer
*buf
,
119 ret
= kfifo_in_locked(&buf
->fifo
, orig
, buf
->chunk_size
,
128 unsigned long code_length
;
129 unsigned int buffer_size
; /* in chunks holding one code each */
131 unsigned long features
;
133 unsigned int chunk_size
;
138 int (*add_to_buf
) (void *data
, struct lirc_buffer
*buf
);
139 struct lirc_buffer
*rbuf
;
140 int (*set_use_inc
) (void *data
);
141 void (*set_use_dec
) (void *data
);
142 struct file_operations
*fops
;
144 struct module
*owner
;
148 * this string will be used for logs
151 * indicates minor device (/dev/lirc) number for registered driver
152 * if caller fills it with negative value, then the first free minor
153 * number will be used (if available)
156 * length of the remote control key code expressed in bits
161 * it may point to any driver data and this pointer will be passed to
162 * all callback functions
165 * add_to_buf will be called after specified period of the time or
166 * triggered by the external event, this behavior depends on value of
167 * the sample_rate this function will be called in user context. This
168 * routine should return 0 if data was added to the buffer and
169 * -ENODATA if none was available. This should add some number of bits
170 * evenly divisible by code_length to the buffer
173 * if not NULL, it will be used as a read buffer, you will have to
174 * write to the buffer by other means, like irq's (see also
178 * set_use_inc will be called after device is opened
181 * set_use_dec will be called after device is closed
184 * file_operations for drivers which don't fit the current driver model.
186 * Some ioctl's can be directly handled by lirc_dev if the driver's
187 * ioctl function is NULL or if it returns -ENOIOCTLCMD (see also
191 * the module owning this struct
196 /* following functions can be called ONLY from user context
198 * returns negative value on error or minor number
199 * of the registered device if success
200 * contents of the structure pointed by p is copied
202 extern int lirc_register_driver(struct lirc_driver
*d
);
204 /* returns negative value on error or 0 if success
206 extern int lirc_unregister_driver(int minor
);
208 /* Returns the private data stored in the lirc_driver
209 * associated with the given device file pointer.
211 void *lirc_get_pdata(struct file
*file
);
213 /* default file operations
214 * used by drivers if they override only some operations
216 int lirc_dev_fop_open(struct inode
*inode
, struct file
*file
);
217 int lirc_dev_fop_close(struct inode
*inode
, struct file
*file
);
218 unsigned int lirc_dev_fop_poll(struct file
*file
, poll_table
*wait
);
219 long lirc_dev_fop_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
);
220 ssize_t
lirc_dev_fop_read(struct file
*file
, char *buffer
, size_t length
,
222 ssize_t
lirc_dev_fop_write(struct file
*file
, const char *buffer
, size_t length
,