blob: 86d15a9b6c01b3694979bddc9d9b05844fd0f7ce [file] [log] [blame]
Jarod Wilson4a62a5a2010-07-03 01:06:57 -03001/*
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 Wilson8de111e22011-05-27 16:56:50 -030012#define MAX_IRCTL_DEVICES 8
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030013#define BUFLEN 16
14
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030015#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
22struct 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 Wilson4a62a5a2010-07-03 01:06:57 -030030};
31
32static inline void lirc_buffer_clear(struct lirc_buffer *buf)
33{
34 unsigned long flags;
35
Martin Kaiser77d381a2014-10-28 18:51:01 -030036 if (kfifo_initialized(&buf->fifo)) {
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030037 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
45static 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 Wilson4a62a5a2010-07-03 01:06:57 -030056
57 return ret;
58}
59
60static inline void lirc_buffer_free(struct lirc_buffer *buf)
61{
Martin Kaiser77d381a2014-10-28 18:51:01 -030062 if (kfifo_initialized(&buf->fifo)) {
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030063 kfifo_free(&buf->fifo);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030064 } else
65 WARN(1, "calling %s on an uninitialized lirc_buffer\n",
66 __func__);
67}
68
69static 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
81static inline int lirc_buffer_full(struct lirc_buffer *buf)
82{
83 return lirc_buffer_len(buf) == buf->size * buf->chunk_size;
84}
85
86static inline int lirc_buffer_empty(struct lirc_buffer *buf)
87{
88 return !lirc_buffer_len(buf);
89}
90
Jarod Wilson4a62a5a2010-07-03 01:06:57 -030091static 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
103static 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 Chehabbe14c5c2015-10-05 13:35:37 -0300114/**
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 Chehabbe14c5c2015-10-05 13:35:37 -0300129 * @features: lirc compatible hardware features, like LIRC_MODE_RAW,
Mauro Carvalho Chehab5b6137dc2016-07-17 09:16:57 -0300130 * LIRC_CAN\_\*, as defined at include/media/lirc.h.
Mauro Carvalho Chehabbe14c5c2015-10-05 13:35:37 -0300131 *
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 Chehabbe14c5c2015-10-05 13:35:37 -0300143 * @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 Chehabbe14c5c2015-10-05 13:35:37 -0300147 * @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 Wilson4a62a5a2010-07-03 01:06:57 -0300161struct lirc_driver {
162 char name[40];
163 int minor;
Jarod Wilsonbe1f9852010-10-08 17:24:21 -0300164 __u32 code_length;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300165 unsigned int buffer_size; /* in chunks holding one code each */
Jarod Wilsonbe1f9852010-10-08 17:24:21 -0300166 __u32 features;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300167
168 unsigned int chunk_size;
169
170 void *data;
171 int min_timeout;
172 int max_timeout;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300173 struct lirc_buffer *rbuf;
Srinivas Kandagatlaca7a7222013-07-22 04:23:07 -0300174 struct rc_dev *rdev;
Geert Uytterhoeven22c00852010-09-30 16:55:07 -0300175 const struct file_operations *fops;
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300176 struct device *dev;
177 struct module *owner;
178};
179
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300180/* 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 */
186extern int lirc_register_driver(struct lirc_driver *d);
187
188/* returns negative value on error or 0 if success
189*/
190extern 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 */
195void *lirc_get_pdata(struct file *file);
196
197/* default file operations
198 * used by drivers if they override only some operations
199 */
200int lirc_dev_fop_open(struct inode *inode, struct file *file);
201int lirc_dev_fop_close(struct inode *inode, struct file *file);
202unsigned int lirc_dev_fop_poll(struct file *file, poll_table *wait);
Arnd Bergmann044e5872010-08-02 15:43:35 -0300203long lirc_dev_fop_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
Dan Carpenter0e835082010-11-17 02:13:39 -0300204ssize_t lirc_dev_fop_read(struct file *file, char __user *buffer, size_t length,
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300205 loff_t *ppos);
Jarod Wilson4a62a5a2010-07-03 01:06:57 -0300206#endif