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 | |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 15 | #include <linux/slab.h> |
| 16 | #include <linux/fs.h> |
| 17 | #include <linux/ioctl.h> |
| 18 | #include <linux/poll.h> |
| 19 | #include <linux/kfifo.h> |
| 20 | #include <media/lirc.h> |
| 21 | |
| 22 | struct lirc_buffer { |
| 23 | wait_queue_head_t wait_poll; |
| 24 | spinlock_t fifo_lock; |
| 25 | unsigned int chunk_size; |
| 26 | unsigned int size; /* in chunks */ |
| 27 | /* Using chunks instead of bytes pretends to simplify boundary checking |
| 28 | * And should allow for some performance fine tunning later */ |
| 29 | struct kfifo fifo; |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 30 | }; |
| 31 | |
| 32 | static inline void lirc_buffer_clear(struct lirc_buffer *buf) |
| 33 | { |
| 34 | unsigned long flags; |
| 35 | |
Martin Kaiser | 77d381a | 2014-10-28 18:51:01 -0300 | [diff] [blame] | 36 | if (kfifo_initialized(&buf->fifo)) { |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 37 | spin_lock_irqsave(&buf->fifo_lock, flags); |
| 38 | kfifo_reset(&buf->fifo); |
| 39 | spin_unlock_irqrestore(&buf->fifo_lock, flags); |
| 40 | } else |
| 41 | WARN(1, "calling %s on an uninitialized lirc_buffer\n", |
| 42 | __func__); |
| 43 | } |
| 44 | |
| 45 | static inline int lirc_buffer_init(struct lirc_buffer *buf, |
| 46 | unsigned int chunk_size, |
| 47 | unsigned int size) |
| 48 | { |
| 49 | int ret; |
| 50 | |
| 51 | init_waitqueue_head(&buf->wait_poll); |
| 52 | spin_lock_init(&buf->fifo_lock); |
| 53 | buf->chunk_size = chunk_size; |
| 54 | buf->size = size; |
| 55 | ret = kfifo_alloc(&buf->fifo, size * chunk_size, GFP_KERNEL); |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 56 | |
| 57 | return ret; |
| 58 | } |
| 59 | |
| 60 | static inline void lirc_buffer_free(struct lirc_buffer *buf) |
| 61 | { |
Martin Kaiser | 77d381a | 2014-10-28 18:51:01 -0300 | [diff] [blame] | 62 | if (kfifo_initialized(&buf->fifo)) { |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 63 | kfifo_free(&buf->fifo); |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 64 | } else |
| 65 | WARN(1, "calling %s on an uninitialized lirc_buffer\n", |
| 66 | __func__); |
| 67 | } |
| 68 | |
| 69 | static inline int lirc_buffer_len(struct lirc_buffer *buf) |
| 70 | { |
| 71 | int len; |
| 72 | unsigned long flags; |
| 73 | |
| 74 | spin_lock_irqsave(&buf->fifo_lock, flags); |
| 75 | len = kfifo_len(&buf->fifo); |
| 76 | spin_unlock_irqrestore(&buf->fifo_lock, flags); |
| 77 | |
| 78 | return len; |
| 79 | } |
| 80 | |
| 81 | static inline int lirc_buffer_full(struct lirc_buffer *buf) |
| 82 | { |
| 83 | return lirc_buffer_len(buf) == buf->size * buf->chunk_size; |
| 84 | } |
| 85 | |
| 86 | static inline int lirc_buffer_empty(struct lirc_buffer *buf) |
| 87 | { |
| 88 | return !lirc_buffer_len(buf); |
| 89 | } |
| 90 | |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 91 | static inline unsigned int lirc_buffer_read(struct lirc_buffer *buf, |
| 92 | unsigned char *dest) |
| 93 | { |
| 94 | unsigned int ret = 0; |
| 95 | |
| 96 | if (lirc_buffer_len(buf) >= buf->chunk_size) |
| 97 | ret = kfifo_out_locked(&buf->fifo, dest, buf->chunk_size, |
| 98 | &buf->fifo_lock); |
| 99 | return ret; |
| 100 | |
| 101 | } |
| 102 | |
| 103 | static inline unsigned int lirc_buffer_write(struct lirc_buffer *buf, |
| 104 | unsigned char *orig) |
| 105 | { |
| 106 | unsigned int ret; |
| 107 | |
| 108 | ret = kfifo_in_locked(&buf->fifo, orig, buf->chunk_size, |
| 109 | &buf->fifo_lock); |
| 110 | |
| 111 | return ret; |
| 112 | } |
| 113 | |
Mauro Carvalho Chehab | be14c5c | 2015-10-05 13:35:37 -0300 | [diff] [blame] | 114 | /** |
| 115 | * struct lirc_driver - Defines the parameters on a LIRC driver |
| 116 | * |
| 117 | * @name: this string will be used for logs |
| 118 | * |
| 119 | * @minor: indicates minor device (/dev/lirc) number for |
| 120 | * registered driver if caller fills it with negative |
| 121 | * value, then the first free minor number will be used |
| 122 | * (if available). |
| 123 | * |
| 124 | * @code_length: length of the remote control key code expressed in bits. |
| 125 | * |
| 126 | * @buffer_size: Number of FIFO buffers with @chunk_size size. If zero, |
| 127 | * creates a buffer with BUFLEN size (16 bytes). |
| 128 | * |
Mauro Carvalho Chehab | be14c5c | 2015-10-05 13:35:37 -0300 | [diff] [blame] | 129 | * @features: lirc compatible hardware features, like LIRC_MODE_RAW, |
Mauro Carvalho Chehab | 5b6137dc | 2016-07-17 09:16:57 -0300 | [diff] [blame] | 130 | * LIRC_CAN\_\*, as defined at include/media/lirc.h. |
Mauro Carvalho Chehab | be14c5c | 2015-10-05 13:35:37 -0300 | [diff] [blame] | 131 | * |
| 132 | * @chunk_size: Size of each FIFO buffer. |
| 133 | * |
| 134 | * @data: it may point to any driver data and this pointer will |
| 135 | * be passed to all callback functions. |
| 136 | * |
| 137 | * @min_timeout: Minimum timeout for record. Valid only if |
| 138 | * LIRC_CAN_SET_REC_TIMEOUT is defined. |
| 139 | * |
| 140 | * @max_timeout: Maximum timeout for record. Valid only if |
| 141 | * LIRC_CAN_SET_REC_TIMEOUT is defined. |
| 142 | * |
Mauro Carvalho Chehab | be14c5c | 2015-10-05 13:35:37 -0300 | [diff] [blame] | 143 | * @rbuf: if not NULL, it will be used as a read buffer, you will |
| 144 | * have to write to the buffer by other means, like irq's |
| 145 | * (see also lirc_serial.c). |
| 146 | * |
Mauro Carvalho Chehab | be14c5c | 2015-10-05 13:35:37 -0300 | [diff] [blame] | 147 | * @rdev: Pointed to struct rc_dev associated with the LIRC |
| 148 | * device. |
| 149 | * |
| 150 | * @fops: file_operations for drivers which don't fit the current |
| 151 | * driver model. |
| 152 | * Some ioctl's can be directly handled by lirc_dev if the |
| 153 | * driver's ioctl function is NULL or if it returns |
| 154 | * -ENOIOCTLCMD (see also lirc_serial.c). |
| 155 | * |
| 156 | * @dev: pointer to the struct device associated with the LIRC |
| 157 | * device. |
| 158 | * |
| 159 | * @owner: the module owning this struct |
| 160 | */ |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 161 | struct lirc_driver { |
| 162 | char name[40]; |
| 163 | int minor; |
Jarod Wilson | be1f985 | 2010-10-08 17:24:21 -0300 | [diff] [blame] | 164 | __u32 code_length; |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 165 | unsigned int buffer_size; /* in chunks holding one code each */ |
Jarod Wilson | be1f985 | 2010-10-08 17:24:21 -0300 | [diff] [blame] | 166 | __u32 features; |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 167 | |
| 168 | unsigned int chunk_size; |
| 169 | |
| 170 | void *data; |
| 171 | int min_timeout; |
| 172 | int max_timeout; |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 173 | struct lirc_buffer *rbuf; |
Srinivas Kandagatla | ca7a722 | 2013-07-22 04:23:07 -0300 | [diff] [blame] | 174 | struct rc_dev *rdev; |
Geert Uytterhoeven | 22c0085 | 2010-09-30 16:55:07 -0300 | [diff] [blame] | 175 | const struct file_operations *fops; |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 176 | struct device *dev; |
| 177 | struct module *owner; |
| 178 | }; |
| 179 | |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 180 | /* following functions can be called ONLY from user context |
| 181 | * |
| 182 | * returns negative value on error or minor number |
| 183 | * of the registered device if success |
| 184 | * contents of the structure pointed by p is copied |
| 185 | */ |
| 186 | extern int lirc_register_driver(struct lirc_driver *d); |
| 187 | |
| 188 | /* returns negative value on error or 0 if success |
| 189 | */ |
| 190 | extern int lirc_unregister_driver(int minor); |
| 191 | |
| 192 | /* Returns the private data stored in the lirc_driver |
| 193 | * associated with the given device file pointer. |
| 194 | */ |
| 195 | void *lirc_get_pdata(struct file *file); |
| 196 | |
| 197 | /* default file operations |
| 198 | * used by drivers if they override only some operations |
| 199 | */ |
| 200 | int lirc_dev_fop_open(struct inode *inode, struct file *file); |
| 201 | int lirc_dev_fop_close(struct inode *inode, struct file *file); |
| 202 | unsigned int lirc_dev_fop_poll(struct file *file, poll_table *wait); |
Arnd Bergmann | 044e587 | 2010-08-02 15:43:35 -0300 | [diff] [blame] | 203 | 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] | 204 | 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] | 205 | loff_t *ppos); |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 206 | #endif |