5 * This code is licensed under GNU GPL
9 #ifndef _LINUX_LIRC_DEV_H
10 #define _LINUX_LIRC_DEV_H
14 #include <linux/slab.h>
16 #include <linux/ioctl.h>
17 #include <linux/poll.h>
18 #include <linux/kfifo.h>
19 #include <media/lirc.h>
20 #include <linux/device.h>
21 #include <linux/cdev.h>
24 wait_queue_head_t wait_poll;
26 unsigned int chunk_size;
27 unsigned int size; /* in chunks */
28 /* Using chunks instead of bytes pretends to simplify boundary checking
29 * And should allow for some performance fine tunning later */
33 static inline void lirc_buffer_clear(struct lirc_buffer *buf)
37 if (kfifo_initialized(&buf->fifo)) {
38 spin_lock_irqsave(&buf->fifo_lock, flags);
39 kfifo_reset(&buf->fifo);
40 spin_unlock_irqrestore(&buf->fifo_lock, flags);
42 WARN(1, "calling %s on an uninitialized lirc_buffer\n",
46 static inline int lirc_buffer_init(struct lirc_buffer *buf,
47 unsigned int chunk_size,
52 init_waitqueue_head(&buf->wait_poll);
53 spin_lock_init(&buf->fifo_lock);
54 buf->chunk_size = chunk_size;
56 ret = kfifo_alloc(&buf->fifo, size * chunk_size, GFP_KERNEL);
61 static inline void lirc_buffer_free(struct lirc_buffer *buf)
63 if (kfifo_initialized(&buf->fifo)) {
64 kfifo_free(&buf->fifo);
66 WARN(1, "calling %s on an uninitialized lirc_buffer\n",
70 static inline int lirc_buffer_len(struct lirc_buffer *buf)
75 spin_lock_irqsave(&buf->fifo_lock, flags);
76 len = kfifo_len(&buf->fifo);
77 spin_unlock_irqrestore(&buf->fifo_lock, flags);
82 static inline int lirc_buffer_full(struct lirc_buffer *buf)
84 return lirc_buffer_len(buf) == buf->size * buf->chunk_size;
87 static inline int lirc_buffer_empty(struct lirc_buffer *buf)
89 return !lirc_buffer_len(buf);
92 static inline unsigned int lirc_buffer_read(struct lirc_buffer *buf,
97 if (lirc_buffer_len(buf) >= buf->chunk_size)
98 ret = kfifo_out_locked(&buf->fifo, dest, buf->chunk_size,
104 static inline unsigned int lirc_buffer_write(struct lirc_buffer *buf,
109 ret = kfifo_in_locked(&buf->fifo, orig, buf->chunk_size,
116 * struct lirc_dev - represents a LIRC device
118 * @name: used for logging
119 * @minor: the minor device (/dev/lircX) number for the device
120 * @code_length: length of a remote control key code expressed in bits
121 * @features: lirc compatible hardware features, like LIRC_MODE_RAW,
122 * LIRC_CAN\_\*, as defined at include/media/lirc.h.
123 * @buffer_size: Number of FIFO buffers with @chunk_size size.
124 * Only used if @rbuf is NULL.
125 * @chunk_size: Size of each FIFO buffer.
126 * Only used if @rbuf is NULL.
127 * @data: private per-driver data
128 * @buf: if %NULL, lirc_dev will allocate and manage the buffer,
129 * otherwise allocated by the caller which will
130 * have to write to the buffer by other means, like irq's
131 * (see also lirc_serial.c).
132 * @buf_internal: whether lirc_dev has allocated the read buffer or not
133 * @rdev: &struct rc_dev associated with the device
134 * @fops: &struct file_operations for the device
135 * @owner: the module owning this struct
136 * @attached: if the device is still live
137 * @open: open count for the device's chardev
138 * @mutex: serialises file_operations calls
139 * @dev: &struct device assigned to the device
140 * @cdev: &struct cdev assigned to the device
148 unsigned int buffer_size; /* in chunks holding one code each */
149 unsigned int chunk_size;
150 struct lirc_buffer *buf;
155 const struct file_operations *fops;
156 struct module *owner;
161 struct mutex mutex; /* protect from simultaneous accesses */
167 struct lirc_dev *lirc_allocate_device(void);
169 void lirc_free_device(struct lirc_dev *d);
171 int lirc_register_device(struct lirc_dev *d);
173 void lirc_unregister_device(struct lirc_dev *d);
175 /* Must be called in the open fop before lirc_get_pdata() can be used */
176 void lirc_init_pdata(struct inode *inode, struct file *file);
178 /* Returns the private data stored in the lirc_dev
179 * associated with the given device file pointer.
181 void *lirc_get_pdata(struct file *file);
183 /* default file operations
184 * used by drivers if they override only some operations
186 int lirc_dev_fop_open(struct inode *inode, struct file *file);
187 int lirc_dev_fop_close(struct inode *inode, struct file *file);
188 unsigned int lirc_dev_fop_poll(struct file *file, poll_table *wait);
189 long lirc_dev_fop_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
190 ssize_t lirc_dev_fop_read(struct file *file, char __user *buffer, size_t length,