]>
Commit | Line | Data |
---|---|---|
4a62a5ab JW |
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 | ||
4a62a5ab JW |
12 | #define BUFLEN 16 |
13 | ||
4a62a5ab JW |
14 | #include <linux/slab.h> |
15 | #include <linux/fs.h> | |
16 | #include <linux/ioctl.h> | |
17 | #include <linux/poll.h> | |
18 | #include <linux/kfifo.h> | |
19 | #include <media/lirc.h> | |
20 | ||
21 | struct lirc_buffer { | |
22 | wait_queue_head_t wait_poll; | |
23 | spinlock_t fifo_lock; | |
24 | unsigned int chunk_size; | |
25 | unsigned int size; /* in chunks */ | |
26 | /* Using chunks instead of bytes pretends to simplify boundary checking | |
27 | * And should allow for some performance fine tunning later */ | |
28 | struct kfifo fifo; | |
4a62a5ab JW |
29 | }; |
30 | ||
31 | static inline void lirc_buffer_clear(struct lirc_buffer *buf) | |
32 | { | |
33 | unsigned long flags; | |
34 | ||
77d381af | 35 | if (kfifo_initialized(&buf->fifo)) { |
4a62a5ab JW |
36 | spin_lock_irqsave(&buf->fifo_lock, flags); |
37 | kfifo_reset(&buf->fifo); | |
38 | spin_unlock_irqrestore(&buf->fifo_lock, flags); | |
39 | } else | |
40 | WARN(1, "calling %s on an uninitialized lirc_buffer\n", | |
41 | __func__); | |
42 | } | |
43 | ||
44 | static inline int lirc_buffer_init(struct lirc_buffer *buf, | |
45 | unsigned int chunk_size, | |
46 | unsigned int size) | |
47 | { | |
48 | int ret; | |
49 | ||
50 | init_waitqueue_head(&buf->wait_poll); | |
51 | spin_lock_init(&buf->fifo_lock); | |
52 | buf->chunk_size = chunk_size; | |
53 | buf->size = size; | |
54 | ret = kfifo_alloc(&buf->fifo, size * chunk_size, GFP_KERNEL); | |
4a62a5ab JW |
55 | |
56 | return ret; | |
57 | } | |
58 | ||
59 | static inline void lirc_buffer_free(struct lirc_buffer *buf) | |
60 | { | |
77d381af | 61 | if (kfifo_initialized(&buf->fifo)) { |
4a62a5ab | 62 | kfifo_free(&buf->fifo); |
4a62a5ab JW |
63 | } else |
64 | WARN(1, "calling %s on an uninitialized lirc_buffer\n", | |
65 | __func__); | |
66 | } | |
67 | ||
68 | static inline int lirc_buffer_len(struct lirc_buffer *buf) | |
69 | { | |
70 | int len; | |
71 | unsigned long flags; | |
72 | ||
73 | spin_lock_irqsave(&buf->fifo_lock, flags); | |
74 | len = kfifo_len(&buf->fifo); | |
75 | spin_unlock_irqrestore(&buf->fifo_lock, flags); | |
76 | ||
77 | return len; | |
78 | } | |
79 | ||
80 | static inline int lirc_buffer_full(struct lirc_buffer *buf) | |
81 | { | |
82 | return lirc_buffer_len(buf) == buf->size * buf->chunk_size; | |
83 | } | |
84 | ||
85 | static inline int lirc_buffer_empty(struct lirc_buffer *buf) | |
86 | { | |
87 | return !lirc_buffer_len(buf); | |
88 | } | |
89 | ||
4a62a5ab JW |
90 | static inline unsigned int lirc_buffer_read(struct lirc_buffer *buf, |
91 | unsigned char *dest) | |
92 | { | |
93 | unsigned int ret = 0; | |
94 | ||
95 | if (lirc_buffer_len(buf) >= buf->chunk_size) | |
96 | ret = kfifo_out_locked(&buf->fifo, dest, buf->chunk_size, | |
97 | &buf->fifo_lock); | |
98 | return ret; | |
99 | ||
100 | } | |
101 | ||
102 | static inline unsigned int lirc_buffer_write(struct lirc_buffer *buf, | |
103 | unsigned char *orig) | |
104 | { | |
105 | unsigned int ret; | |
106 | ||
107 | ret = kfifo_in_locked(&buf->fifo, orig, buf->chunk_size, | |
108 | &buf->fifo_lock); | |
109 | ||
110 | return ret; | |
111 | } | |
112 | ||
be14c5cd | 113 | /** |
5ddc9c09 | 114 | * struct lirc_dev - represents a LIRC device |
be14c5cd | 115 | * |
5ddc9c09 DH |
116 | * @name: used for logging |
117 | * @minor: the minor device (/dev/lircX) number for the device | |
118 | * @code_length: length of a remote control key code expressed in bits | |
be14c5cd | 119 | * @features: lirc compatible hardware features, like LIRC_MODE_RAW, |
5b6137dc | 120 | * LIRC_CAN\_\*, as defined at include/media/lirc.h. |
b145ef94 DH |
121 | * @buffer_size: Number of FIFO buffers with @chunk_size size. |
122 | * Only used if @rbuf is NULL. | |
be14c5cd | 123 | * @chunk_size: Size of each FIFO buffer. |
b145ef94 | 124 | * Only used if @rbuf is NULL. |
5ddc9c09 | 125 | * @data: private per-driver data |
be14c5cd MCC |
126 | * @min_timeout: Minimum timeout for record. Valid only if |
127 | * LIRC_CAN_SET_REC_TIMEOUT is defined. | |
be14c5cd MCC |
128 | * @max_timeout: Maximum timeout for record. Valid only if |
129 | * LIRC_CAN_SET_REC_TIMEOUT is defined. | |
be14c5cd MCC |
130 | * @rbuf: if not NULL, it will be used as a read buffer, you will |
131 | * have to write to the buffer by other means, like irq's | |
132 | * (see also lirc_serial.c). | |
5ddc9c09 DH |
133 | * @rdev: &struct rc_dev associated with the device |
134 | * @fops: &struct file_operations for the device | |
135 | * @dev: &struct device assigned to the device | |
be14c5cd | 136 | * @owner: the module owning this struct |
5ddc9c09 | 137 | * @irctl: &struct irctl assigned to the device |
be14c5cd | 138 | */ |
5ddc9c09 | 139 | struct lirc_dev { |
4a62a5ab | 140 | char name[40]; |
c3c6dd75 | 141 | unsigned int minor; |
be1f985f | 142 | __u32 code_length; |
be1f985f | 143 | __u32 features; |
4a62a5ab | 144 | |
b145ef94 | 145 | unsigned int buffer_size; /* in chunks holding one code each */ |
4a62a5ab JW |
146 | unsigned int chunk_size; |
147 | ||
148 | void *data; | |
149 | int min_timeout; | |
150 | int max_timeout; | |
4a62a5ab | 151 | struct lirc_buffer *rbuf; |
ca7a722d | 152 | struct rc_dev *rdev; |
22c00854 | 153 | const struct file_operations *fops; |
4a62a5ab JW |
154 | struct device *dev; |
155 | struct module *owner; | |
c3c6dd75 | 156 | struct irctl *irctl; |
4a62a5ab JW |
157 | }; |
158 | ||
4a62a5ab JW |
159 | /* following functions can be called ONLY from user context |
160 | * | |
c3c6dd75 | 161 | * returns negative value on error or zero |
4a62a5ab JW |
162 | * contents of the structure pointed by p is copied |
163 | */ | |
5ddc9c09 | 164 | int lirc_register_device(struct lirc_dev *d); |
4a62a5ab | 165 | |
5ddc9c09 | 166 | void lirc_unregister_device(struct lirc_dev *d); |
4a62a5ab | 167 | |
615cd3fe DH |
168 | /* Must be called in the open fop before lirc_get_pdata() can be used */ |
169 | void lirc_init_pdata(struct inode *inode, struct file *file); | |
170 | ||
5ddc9c09 | 171 | /* Returns the private data stored in the lirc_dev |
4a62a5ab JW |
172 | * associated with the given device file pointer. |
173 | */ | |
174 | void *lirc_get_pdata(struct file *file); | |
175 | ||
176 | /* default file operations | |
177 | * used by drivers if they override only some operations | |
178 | */ | |
179 | int lirc_dev_fop_open(struct inode *inode, struct file *file); | |
180 | int lirc_dev_fop_close(struct inode *inode, struct file *file); | |
181 | unsigned int lirc_dev_fop_poll(struct file *file, poll_table *wait); | |
044e5878 | 182 | long lirc_dev_fop_ioctl(struct file *file, unsigned int cmd, unsigned long arg); |
0e835087 | 183 | ssize_t lirc_dev_fop_read(struct file *file, char __user *buffer, size_t length, |
4a62a5ab | 184 | loff_t *ppos); |
4a62a5ab | 185 | #endif |