Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 1 | /* |
| 2 | * LIRC base driver |
| 3 | * |
| 4 | * by Artur Lipowski <alipowski@interia.pl> |
| 5 | * This code is licensed under GNU GPL |
| 6 | * |
| 7 | */ |
| 8 | |
| 9 | #ifndef _LINUX_LIRC_DEV_H |
| 10 | #define _LINUX_LIRC_DEV_H |
| 11 | |
Jarod Wilson | 8de111e2 | 2011-05-27 16:56:50 -0300 | [diff] [blame] | 12 | #define MAX_IRCTL_DEVICES 8 |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 13 | #define BUFLEN 16 |
| 14 | |
| 15 | #define mod(n, div) ((n) % (div)) |
| 16 | |
| 17 | #include <linux/slab.h> |
| 18 | #include <linux/fs.h> |
| 19 | #include <linux/ioctl.h> |
| 20 | #include <linux/poll.h> |
| 21 | #include <linux/kfifo.h> |
| 22 | #include <media/lirc.h> |
| 23 | |
| 24 | struct lirc_buffer { |
| 25 | wait_queue_head_t wait_poll; |
| 26 | spinlock_t fifo_lock; |
| 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 */ |
| 31 | struct kfifo fifo; |
| 32 | u8 fifo_initialized; |
| 33 | }; |
| 34 | |
| 35 | static inline void lirc_buffer_clear(struct lirc_buffer *buf) |
| 36 | { |
| 37 | unsigned long flags; |
| 38 | |
| 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); |
| 43 | } else |
| 44 | WARN(1, "calling %s on an uninitialized lirc_buffer\n", |
| 45 | __func__); |
| 46 | } |
| 47 | |
| 48 | static inline int lirc_buffer_init(struct lirc_buffer *buf, |
| 49 | unsigned int chunk_size, |
| 50 | unsigned int size) |
| 51 | { |
| 52 | int ret; |
| 53 | |
| 54 | init_waitqueue_head(&buf->wait_poll); |
| 55 | spin_lock_init(&buf->fifo_lock); |
| 56 | buf->chunk_size = chunk_size; |
| 57 | buf->size = size; |
| 58 | ret = kfifo_alloc(&buf->fifo, size * chunk_size, GFP_KERNEL); |
| 59 | if (ret == 0) |
| 60 | buf->fifo_initialized = 1; |
| 61 | |
| 62 | return ret; |
| 63 | } |
| 64 | |
| 65 | static inline void lirc_buffer_free(struct lirc_buffer *buf) |
| 66 | { |
| 67 | if (buf->fifo_initialized) { |
| 68 | kfifo_free(&buf->fifo); |
| 69 | buf->fifo_initialized = 0; |
| 70 | } else |
| 71 | WARN(1, "calling %s on an uninitialized lirc_buffer\n", |
| 72 | __func__); |
| 73 | } |
| 74 | |
| 75 | static inline int lirc_buffer_len(struct lirc_buffer *buf) |
| 76 | { |
| 77 | int len; |
| 78 | unsigned long flags; |
| 79 | |
| 80 | spin_lock_irqsave(&buf->fifo_lock, flags); |
| 81 | len = kfifo_len(&buf->fifo); |
| 82 | spin_unlock_irqrestore(&buf->fifo_lock, flags); |
| 83 | |
| 84 | return len; |
| 85 | } |
| 86 | |
| 87 | static inline int lirc_buffer_full(struct lirc_buffer *buf) |
| 88 | { |
| 89 | return lirc_buffer_len(buf) == buf->size * buf->chunk_size; |
| 90 | } |
| 91 | |
| 92 | static inline int lirc_buffer_empty(struct lirc_buffer *buf) |
| 93 | { |
| 94 | return !lirc_buffer_len(buf); |
| 95 | } |
| 96 | |
| 97 | static inline int lirc_buffer_available(struct lirc_buffer *buf) |
| 98 | { |
| 99 | return buf->size - (lirc_buffer_len(buf) / buf->chunk_size); |
| 100 | } |
| 101 | |
| 102 | static inline unsigned int lirc_buffer_read(struct lirc_buffer *buf, |
| 103 | unsigned char *dest) |
| 104 | { |
| 105 | unsigned int ret = 0; |
| 106 | |
| 107 | if (lirc_buffer_len(buf) >= buf->chunk_size) |
| 108 | ret = kfifo_out_locked(&buf->fifo, dest, buf->chunk_size, |
| 109 | &buf->fifo_lock); |
| 110 | return ret; |
| 111 | |
| 112 | } |
| 113 | |
| 114 | static inline unsigned int lirc_buffer_write(struct lirc_buffer *buf, |
| 115 | unsigned char *orig) |
| 116 | { |
| 117 | unsigned int ret; |
| 118 | |
| 119 | ret = kfifo_in_locked(&buf->fifo, orig, buf->chunk_size, |
| 120 | &buf->fifo_lock); |
| 121 | |
| 122 | return ret; |
| 123 | } |
| 124 | |
| 125 | struct lirc_driver { |
| 126 | char name[40]; |
| 127 | int minor; |
Jarod Wilson | be1f985 | 2010-10-08 17:24:21 -0300 | [diff] [blame] | 128 | __u32 code_length; |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 129 | unsigned int buffer_size; /* in chunks holding one code each */ |
| 130 | int sample_rate; |
Jarod Wilson | be1f985 | 2010-10-08 17:24:21 -0300 | [diff] [blame] | 131 | __u32 features; |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 132 | |
| 133 | unsigned int chunk_size; |
| 134 | |
| 135 | void *data; |
| 136 | int min_timeout; |
| 137 | int max_timeout; |
| 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); |
Srinivas Kandagatla | ca7a722 | 2013-07-22 04:23:07 -0300 | [diff] [blame] | 142 | struct rc_dev *rdev; |
Geert Uytterhoeven | 22c0085 | 2010-09-30 16:55:07 -0300 | [diff] [blame] | 143 | const struct file_operations *fops; |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 144 | struct device *dev; |
| 145 | struct module *owner; |
| 146 | }; |
| 147 | |
| 148 | /* name: |
| 149 | * this string will be used for logs |
| 150 | * |
| 151 | * minor: |
| 152 | * indicates minor device (/dev/lirc) number for registered driver |
| 153 | * if caller fills it with negative value, then the first free minor |
| 154 | * number will be used (if available) |
| 155 | * |
| 156 | * code_length: |
| 157 | * length of the remote control key code expressed in bits |
| 158 | * |
| 159 | * sample_rate: |
| 160 | * |
| 161 | * data: |
| 162 | * it may point to any driver data and this pointer will be passed to |
| 163 | * all callback functions |
| 164 | * |
| 165 | * add_to_buf: |
| 166 | * add_to_buf will be called after specified period of the time or |
| 167 | * triggered by the external event, this behavior depends on value of |
| 168 | * the sample_rate this function will be called in user context. This |
| 169 | * routine should return 0 if data was added to the buffer and |
| 170 | * -ENODATA if none was available. This should add some number of bits |
| 171 | * evenly divisible by code_length to the buffer |
| 172 | * |
| 173 | * rbuf: |
| 174 | * if not NULL, it will be used as a read buffer, you will have to |
| 175 | * write to the buffer by other means, like irq's (see also |
| 176 | * lirc_serial.c). |
| 177 | * |
| 178 | * set_use_inc: |
| 179 | * set_use_inc will be called after device is opened |
| 180 | * |
| 181 | * set_use_dec: |
| 182 | * set_use_dec will be called after device is closed |
| 183 | * |
| 184 | * fops: |
| 185 | * file_operations for drivers which don't fit the current driver model. |
| 186 | * |
| 187 | * Some ioctl's can be directly handled by lirc_dev if the driver's |
| 188 | * ioctl function is NULL or if it returns -ENOIOCTLCMD (see also |
| 189 | * lirc_serial.c). |
| 190 | * |
| 191 | * owner: |
| 192 | * the module owning this struct |
| 193 | * |
| 194 | */ |
| 195 | |
| 196 | |
| 197 | /* following functions can be called ONLY from user context |
| 198 | * |
| 199 | * returns negative value on error or minor number |
| 200 | * of the registered device if success |
| 201 | * contents of the structure pointed by p is copied |
| 202 | */ |
| 203 | extern int lirc_register_driver(struct lirc_driver *d); |
| 204 | |
| 205 | /* returns negative value on error or 0 if success |
| 206 | */ |
| 207 | extern int lirc_unregister_driver(int minor); |
| 208 | |
| 209 | /* Returns the private data stored in the lirc_driver |
| 210 | * associated with the given device file pointer. |
| 211 | */ |
| 212 | void *lirc_get_pdata(struct file *file); |
| 213 | |
| 214 | /* default file operations |
| 215 | * used by drivers if they override only some operations |
| 216 | */ |
| 217 | int lirc_dev_fop_open(struct inode *inode, struct file *file); |
| 218 | int lirc_dev_fop_close(struct inode *inode, struct file *file); |
| 219 | unsigned int lirc_dev_fop_poll(struct file *file, poll_table *wait); |
Arnd Bergmann | 044e587 | 2010-08-02 15:43:35 -0300 | [diff] [blame] | 220 | long lirc_dev_fop_ioctl(struct file *file, unsigned int cmd, unsigned long arg); |
Dan Carpenter | 0e83508 | 2010-11-17 02:13:39 -0300 | [diff] [blame] | 221 | ssize_t lirc_dev_fop_read(struct file *file, char __user *buffer, size_t length, |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 222 | loff_t *ppos); |
Dan Carpenter | 0e83508 | 2010-11-17 02:13:39 -0300 | [diff] [blame] | 223 | ssize_t lirc_dev_fop_write(struct file *file, const char __user *buffer, |
| 224 | size_t length, loff_t *ppos); |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 225 | |
| 226 | #endif |