• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
12 #define MAX_IRCTL_DEVICES 8
13 #define BUFLEN            16
14 
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;
30 };
31 
lirc_buffer_clear(struct lirc_buffer * buf)32 static inline void lirc_buffer_clear(struct lirc_buffer *buf)
33 {
34 	unsigned long flags;
35 
36 	if (kfifo_initialized(&buf->fifo)) {
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 
lirc_buffer_init(struct lirc_buffer * buf,unsigned int chunk_size,unsigned int size)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);
56 
57 	return ret;
58 }
59 
lirc_buffer_free(struct lirc_buffer * buf)60 static inline void lirc_buffer_free(struct lirc_buffer *buf)
61 {
62 	if (kfifo_initialized(&buf->fifo)) {
63 		kfifo_free(&buf->fifo);
64 	} else
65 		WARN(1, "calling %s on an uninitialized lirc_buffer\n",
66 		     __func__);
67 }
68 
lirc_buffer_len(struct lirc_buffer * buf)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 
lirc_buffer_full(struct lirc_buffer * buf)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 
lirc_buffer_empty(struct lirc_buffer * buf)86 static inline int lirc_buffer_empty(struct lirc_buffer *buf)
87 {
88 	return !lirc_buffer_len(buf);
89 }
90 
lirc_buffer_read(struct lirc_buffer * buf,unsigned char * dest)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 
lirc_buffer_write(struct lirc_buffer * buf,unsigned char * orig)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 
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  *
129  * @features:		lirc compatible hardware features, like LIRC_MODE_RAW,
130  *			LIRC_CAN\_\*, as defined at include/media/lirc.h.
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  *
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  *
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  */
161 struct lirc_driver {
162 	char name[40];
163 	int minor;
164 	__u32 code_length;
165 	unsigned int buffer_size; /* in chunks holding one code each */
166 	__u32 features;
167 
168 	unsigned int chunk_size;
169 
170 	void *data;
171 	int min_timeout;
172 	int max_timeout;
173 	struct lirc_buffer *rbuf;
174 	struct rc_dev *rdev;
175 	const struct file_operations *fops;
176 	struct device *dev;
177 	struct module *owner;
178 };
179 
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);
203 long lirc_dev_fop_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
204 ssize_t lirc_dev_fop_read(struct file *file, char __user *buffer, size_t length,
205 			  loff_t *ppos);
206 #endif
207