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 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/kernel.h> |
| 24 | #include <linux/sched.h> |
| 25 | #include <linux/errno.h> |
| 26 | #include <linux/ioctl.h> |
| 27 | #include <linux/fs.h> |
| 28 | #include <linux/poll.h> |
| 29 | #include <linux/completion.h> |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 30 | #include <linux/mutex.h> |
| 31 | #include <linux/wait.h> |
| 32 | #include <linux/unistd.h> |
| 33 | #include <linux/kthread.h> |
| 34 | #include <linux/bitops.h> |
| 35 | #include <linux/device.h> |
| 36 | #include <linux/cdev.h> |
| 37 | |
| 38 | #include <media/lirc.h> |
Jarod Wilson | 5690085 | 2010-07-16 14:25:33 -0300 | [diff] [blame] | 39 | #include <media/lirc_dev.h> |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 40 | |
| 41 | static int debug; |
| 42 | |
| 43 | #define IRCTL_DEV_NAME "BaseRemoteCtl" |
| 44 | #define NOPLUG -1 |
| 45 | #define LOGHEAD "lirc_dev (%s[%d]): " |
| 46 | |
| 47 | static dev_t lirc_base_dev; |
| 48 | |
| 49 | struct irctl { |
| 50 | struct lirc_driver d; |
| 51 | int attached; |
| 52 | int open; |
| 53 | |
| 54 | struct mutex irctl_lock; |
| 55 | struct lirc_buffer *buf; |
| 56 | unsigned int chunk_size; |
| 57 | |
| 58 | struct task_struct *task; |
| 59 | long jiffies_to_wait; |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | static DEFINE_MUTEX(lirc_dev_lock); |
| 63 | |
| 64 | static struct irctl *irctls[MAX_IRCTL_DEVICES]; |
Jarod Wilson | c1cbb70 | 2010-10-18 18:39:20 -0300 | [diff] [blame] | 65 | static struct cdev cdevs[MAX_IRCTL_DEVICES]; |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 66 | |
| 67 | /* Only used for sysfs but defined to void otherwise */ |
| 68 | static struct class *lirc_class; |
| 69 | |
| 70 | /* helper function |
| 71 | * initializes the irctl structure |
| 72 | */ |
Jarod Wilson | 578fcb8 | 2010-10-16 21:29:50 -0300 | [diff] [blame] | 73 | static void lirc_irctl_init(struct irctl *ir) |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 74 | { |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 75 | mutex_init(&ir->irctl_lock); |
| 76 | ir->d.minor = NOPLUG; |
| 77 | } |
| 78 | |
Jarod Wilson | 578fcb8 | 2010-10-16 21:29:50 -0300 | [diff] [blame] | 79 | static void lirc_irctl_cleanup(struct irctl *ir) |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 80 | { |
| 81 | dev_dbg(ir->d.dev, LOGHEAD "cleaning up\n", ir->d.name, ir->d.minor); |
| 82 | |
| 83 | device_destroy(lirc_class, MKDEV(MAJOR(lirc_base_dev), ir->d.minor)); |
| 84 | |
| 85 | if (ir->buf != ir->d.rbuf) { |
| 86 | lirc_buffer_free(ir->buf); |
| 87 | kfree(ir->buf); |
| 88 | } |
| 89 | ir->buf = NULL; |
| 90 | } |
| 91 | |
| 92 | /* helper function |
| 93 | * reads key codes from driver and puts them into buffer |
| 94 | * returns 0 on success |
| 95 | */ |
Jarod Wilson | 578fcb8 | 2010-10-16 21:29:50 -0300 | [diff] [blame] | 96 | static int lirc_add_to_buf(struct irctl *ir) |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 97 | { |
| 98 | if (ir->d.add_to_buf) { |
| 99 | int res = -ENODATA; |
| 100 | int got_data = 0; |
| 101 | |
| 102 | /* |
| 103 | * service the device as long as it is returning |
| 104 | * data and we have space |
| 105 | */ |
| 106 | get_data: |
| 107 | res = ir->d.add_to_buf(ir->d.data, ir->buf); |
| 108 | if (res == 0) { |
| 109 | got_data++; |
| 110 | goto get_data; |
| 111 | } |
| 112 | |
| 113 | if (res == -ENODEV) |
| 114 | kthread_stop(ir->task); |
| 115 | |
| 116 | return got_data ? 0 : res; |
| 117 | } |
| 118 | |
| 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | /* main function of the polling thread |
| 123 | */ |
| 124 | static int lirc_thread(void *irctl) |
| 125 | { |
| 126 | struct irctl *ir = irctl; |
| 127 | |
| 128 | dev_dbg(ir->d.dev, LOGHEAD "poll thread started\n", |
| 129 | ir->d.name, ir->d.minor); |
| 130 | |
| 131 | do { |
| 132 | if (ir->open) { |
| 133 | if (ir->jiffies_to_wait) { |
| 134 | set_current_state(TASK_INTERRUPTIBLE); |
| 135 | schedule_timeout(ir->jiffies_to_wait); |
| 136 | } |
| 137 | if (kthread_should_stop()) |
| 138 | break; |
Jarod Wilson | 578fcb8 | 2010-10-16 21:29:50 -0300 | [diff] [blame] | 139 | if (!lirc_add_to_buf(ir)) |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 140 | wake_up_interruptible(&ir->buf->wait_poll); |
| 141 | } else { |
| 142 | set_current_state(TASK_INTERRUPTIBLE); |
| 143 | schedule(); |
| 144 | } |
| 145 | } while (!kthread_should_stop()); |
| 146 | |
| 147 | dev_dbg(ir->d.dev, LOGHEAD "poll thread ended\n", |
| 148 | ir->d.name, ir->d.minor); |
| 149 | |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | |
Jarod Wilson | 578fcb8 | 2010-10-16 21:29:50 -0300 | [diff] [blame] | 154 | static struct file_operations lirc_dev_fops = { |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 155 | .owner = THIS_MODULE, |
| 156 | .read = lirc_dev_fop_read, |
| 157 | .write = lirc_dev_fop_write, |
| 158 | .poll = lirc_dev_fop_poll, |
Arnd Bergmann | 044e587 | 2010-08-02 15:43:35 -0300 | [diff] [blame] | 159 | .unlocked_ioctl = lirc_dev_fop_ioctl, |
Jarod Wilson | 8be292c | 2010-10-09 15:07:06 -0300 | [diff] [blame] | 160 | #ifdef CONFIG_COMPAT |
| 161 | .compat_ioctl = lirc_dev_fop_ioctl, |
| 162 | #endif |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 163 | .open = lirc_dev_fop_open, |
| 164 | .release = lirc_dev_fop_close, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 165 | .llseek = noop_llseek, |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 166 | }; |
| 167 | |
| 168 | static int lirc_cdev_add(struct irctl *ir) |
| 169 | { |
| 170 | int retval; |
| 171 | struct lirc_driver *d = &ir->d; |
Jarod Wilson | c1cbb70 | 2010-10-18 18:39:20 -0300 | [diff] [blame] | 172 | struct cdev *cdev = &cdevs[d->minor]; |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 173 | |
| 174 | if (d->fops) { |
Jarod Wilson | c1cbb70 | 2010-10-18 18:39:20 -0300 | [diff] [blame] | 175 | cdev_init(cdev, d->fops); |
| 176 | cdev->owner = d->owner; |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 177 | } else { |
Jarod Wilson | c1cbb70 | 2010-10-18 18:39:20 -0300 | [diff] [blame] | 178 | cdev_init(cdev, &lirc_dev_fops); |
| 179 | cdev->owner = THIS_MODULE; |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 180 | } |
Jarod Wilson | c1cbb70 | 2010-10-18 18:39:20 -0300 | [diff] [blame] | 181 | kobject_set_name(&cdev->kobj, "lirc%d", d->minor); |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 182 | |
Jarod Wilson | c1cbb70 | 2010-10-18 18:39:20 -0300 | [diff] [blame] | 183 | retval = cdev_add(cdev, MKDEV(MAJOR(lirc_base_dev), d->minor), 1); |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 184 | if (retval) |
Jarod Wilson | c1cbb70 | 2010-10-18 18:39:20 -0300 | [diff] [blame] | 185 | kobject_put(&cdev->kobj); |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 186 | |
| 187 | return retval; |
| 188 | } |
| 189 | |
| 190 | int lirc_register_driver(struct lirc_driver *d) |
| 191 | { |
| 192 | struct irctl *ir; |
| 193 | int minor; |
| 194 | int bytes_in_key; |
| 195 | unsigned int chunk_size; |
| 196 | unsigned int buffer_size; |
| 197 | int err; |
| 198 | |
| 199 | if (!d) { |
| 200 | printk(KERN_ERR "lirc_dev: lirc_register_driver: " |
| 201 | "driver pointer must be not NULL!\n"); |
| 202 | err = -EBADRQC; |
| 203 | goto out; |
| 204 | } |
| 205 | |
Jarod Wilson | 715d29a | 2010-10-18 12:02:01 -0300 | [diff] [blame] | 206 | if (!d->dev) { |
| 207 | printk(KERN_ERR "%s: dev pointer not filled in!\n", __func__); |
| 208 | err = -EINVAL; |
| 209 | goto out; |
| 210 | } |
| 211 | |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 212 | if (MAX_IRCTL_DEVICES <= d->minor) { |
| 213 | dev_err(d->dev, "lirc_dev: lirc_register_driver: " |
| 214 | "\"minor\" must be between 0 and %d (%d)!\n", |
| 215 | MAX_IRCTL_DEVICES-1, d->minor); |
| 216 | err = -EBADRQC; |
| 217 | goto out; |
| 218 | } |
| 219 | |
| 220 | if (1 > d->code_length || (BUFLEN * 8) < d->code_length) { |
| 221 | dev_err(d->dev, "lirc_dev: lirc_register_driver: " |
| 222 | "code length in bits for minor (%d) " |
| 223 | "must be less than %d!\n", |
| 224 | d->minor, BUFLEN * 8); |
| 225 | err = -EBADRQC; |
| 226 | goto out; |
| 227 | } |
| 228 | |
| 229 | dev_dbg(d->dev, "lirc_dev: lirc_register_driver: sample_rate: %d\n", |
| 230 | d->sample_rate); |
| 231 | if (d->sample_rate) { |
| 232 | if (2 > d->sample_rate || HZ < d->sample_rate) { |
| 233 | dev_err(d->dev, "lirc_dev: lirc_register_driver: " |
| 234 | "sample_rate must be between 2 and %d!\n", HZ); |
| 235 | err = -EBADRQC; |
| 236 | goto out; |
| 237 | } |
| 238 | if (!d->add_to_buf) { |
| 239 | dev_err(d->dev, "lirc_dev: lirc_register_driver: " |
| 240 | "add_to_buf cannot be NULL when " |
| 241 | "sample_rate is set\n"); |
| 242 | err = -EBADRQC; |
| 243 | goto out; |
| 244 | } |
| 245 | } else if (!(d->fops && d->fops->read) && !d->rbuf) { |
| 246 | dev_err(d->dev, "lirc_dev: lirc_register_driver: " |
| 247 | "fops->read and rbuf cannot all be NULL!\n"); |
| 248 | err = -EBADRQC; |
| 249 | goto out; |
| 250 | } else if (!d->rbuf) { |
| 251 | if (!(d->fops && d->fops->read && d->fops->poll && |
Arnd Bergmann | 044e587 | 2010-08-02 15:43:35 -0300 | [diff] [blame] | 252 | d->fops->unlocked_ioctl)) { |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 253 | dev_err(d->dev, "lirc_dev: lirc_register_driver: " |
Arnd Bergmann | 044e587 | 2010-08-02 15:43:35 -0300 | [diff] [blame] | 254 | "neither read, poll nor unlocked_ioctl can be NULL!\n"); |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 255 | err = -EBADRQC; |
| 256 | goto out; |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | mutex_lock(&lirc_dev_lock); |
| 261 | |
| 262 | minor = d->minor; |
| 263 | |
| 264 | if (minor < 0) { |
| 265 | /* find first free slot for driver */ |
| 266 | for (minor = 0; minor < MAX_IRCTL_DEVICES; minor++) |
| 267 | if (!irctls[minor]) |
| 268 | break; |
| 269 | if (MAX_IRCTL_DEVICES == minor) { |
| 270 | dev_err(d->dev, "lirc_dev: lirc_register_driver: " |
| 271 | "no free slots for drivers!\n"); |
| 272 | err = -ENOMEM; |
| 273 | goto out_lock; |
| 274 | } |
| 275 | } else if (irctls[minor]) { |
| 276 | dev_err(d->dev, "lirc_dev: lirc_register_driver: " |
| 277 | "minor (%d) just registered!\n", minor); |
| 278 | err = -EBUSY; |
| 279 | goto out_lock; |
| 280 | } |
| 281 | |
| 282 | ir = kzalloc(sizeof(struct irctl), GFP_KERNEL); |
| 283 | if (!ir) { |
| 284 | err = -ENOMEM; |
| 285 | goto out_lock; |
| 286 | } |
Jarod Wilson | 578fcb8 | 2010-10-16 21:29:50 -0300 | [diff] [blame] | 287 | lirc_irctl_init(ir); |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 288 | irctls[minor] = ir; |
| 289 | d->minor = minor; |
| 290 | |
| 291 | if (d->sample_rate) { |
| 292 | ir->jiffies_to_wait = HZ / d->sample_rate; |
| 293 | } else { |
| 294 | /* it means - wait for external event in task queue */ |
| 295 | ir->jiffies_to_wait = 0; |
| 296 | } |
| 297 | |
| 298 | /* some safety check 8-) */ |
| 299 | d->name[sizeof(d->name)-1] = '\0'; |
| 300 | |
| 301 | bytes_in_key = BITS_TO_LONGS(d->code_length) + |
| 302 | (d->code_length % 8 ? 1 : 0); |
| 303 | buffer_size = d->buffer_size ? d->buffer_size : BUFLEN / bytes_in_key; |
| 304 | chunk_size = d->chunk_size ? d->chunk_size : bytes_in_key; |
| 305 | |
| 306 | if (d->rbuf) { |
| 307 | ir->buf = d->rbuf; |
| 308 | } else { |
| 309 | ir->buf = kmalloc(sizeof(struct lirc_buffer), GFP_KERNEL); |
| 310 | if (!ir->buf) { |
| 311 | err = -ENOMEM; |
| 312 | goto out_lock; |
| 313 | } |
| 314 | err = lirc_buffer_init(ir->buf, chunk_size, buffer_size); |
| 315 | if (err) { |
| 316 | kfree(ir->buf); |
| 317 | goto out_lock; |
| 318 | } |
| 319 | } |
| 320 | ir->chunk_size = ir->buf->chunk_size; |
| 321 | |
| 322 | if (d->features == 0) |
| 323 | d->features = LIRC_CAN_REC_LIRCCODE; |
| 324 | |
| 325 | ir->d = *d; |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 326 | |
| 327 | device_create(lirc_class, ir->d.dev, |
| 328 | MKDEV(MAJOR(lirc_base_dev), ir->d.minor), NULL, |
| 329 | "lirc%u", ir->d.minor); |
| 330 | |
| 331 | if (d->sample_rate) { |
| 332 | /* try to fire up polling thread */ |
| 333 | ir->task = kthread_run(lirc_thread, (void *)ir, "lirc_dev"); |
| 334 | if (IS_ERR(ir->task)) { |
| 335 | dev_err(d->dev, "lirc_dev: lirc_register_driver: " |
| 336 | "cannot run poll thread for minor = %d\n", |
| 337 | d->minor); |
| 338 | err = -ECHILD; |
| 339 | goto out_sysfs; |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | err = lirc_cdev_add(ir); |
| 344 | if (err) |
| 345 | goto out_sysfs; |
| 346 | |
| 347 | ir->attached = 1; |
| 348 | mutex_unlock(&lirc_dev_lock); |
| 349 | |
| 350 | dev_info(ir->d.dev, "lirc_dev: driver %s registered at minor = %d\n", |
| 351 | ir->d.name, ir->d.minor); |
| 352 | return minor; |
| 353 | |
| 354 | out_sysfs: |
| 355 | device_destroy(lirc_class, MKDEV(MAJOR(lirc_base_dev), ir->d.minor)); |
| 356 | out_lock: |
| 357 | mutex_unlock(&lirc_dev_lock); |
| 358 | out: |
| 359 | return err; |
| 360 | } |
| 361 | EXPORT_SYMBOL(lirc_register_driver); |
| 362 | |
| 363 | int lirc_unregister_driver(int minor) |
| 364 | { |
| 365 | struct irctl *ir; |
Jarod Wilson | c1cbb70 | 2010-10-18 18:39:20 -0300 | [diff] [blame] | 366 | struct cdev *cdev; |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 367 | |
| 368 | if (minor < 0 || minor >= MAX_IRCTL_DEVICES) { |
Jarod Wilson | 4fc2154 | 2010-10-09 15:17:03 -0300 | [diff] [blame] | 369 | printk(KERN_ERR "lirc_dev: %s: minor (%d) must be between " |
| 370 | "0 and %d!\n", __func__, minor, MAX_IRCTL_DEVICES-1); |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 371 | return -EBADRQC; |
| 372 | } |
| 373 | |
| 374 | ir = irctls[minor]; |
Jarod Wilson | df1868e | 2010-09-17 18:12:31 -0300 | [diff] [blame] | 375 | if (!ir) { |
Jarod Wilson | 4fc2154 | 2010-10-09 15:17:03 -0300 | [diff] [blame] | 376 | printk(KERN_ERR "lirc_dev: %s: failed to get irctl struct " |
| 377 | "for minor %d!\n", __func__, minor); |
Jarod Wilson | df1868e | 2010-09-17 18:12:31 -0300 | [diff] [blame] | 378 | return -ENOENT; |
| 379 | } |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 380 | |
Jarod Wilson | c1cbb70 | 2010-10-18 18:39:20 -0300 | [diff] [blame] | 381 | cdev = &cdevs[minor]; |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 382 | |
| 383 | mutex_lock(&lirc_dev_lock); |
| 384 | |
| 385 | if (ir->d.minor != minor) { |
Jarod Wilson | 4fc2154 | 2010-10-09 15:17:03 -0300 | [diff] [blame] | 386 | printk(KERN_ERR "lirc_dev: %s: minor (%d) device not " |
| 387 | "registered!\n", __func__, minor); |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 388 | mutex_unlock(&lirc_dev_lock); |
| 389 | return -ENOENT; |
| 390 | } |
| 391 | |
| 392 | /* end up polling thread */ |
| 393 | if (ir->task) |
| 394 | kthread_stop(ir->task); |
| 395 | |
| 396 | dev_dbg(ir->d.dev, "lirc_dev: driver %s unregistered from minor = %d\n", |
| 397 | ir->d.name, ir->d.minor); |
| 398 | |
| 399 | ir->attached = 0; |
| 400 | if (ir->open) { |
| 401 | dev_dbg(ir->d.dev, LOGHEAD "releasing opened driver\n", |
| 402 | ir->d.name, ir->d.minor); |
| 403 | wake_up_interruptible(&ir->buf->wait_poll); |
| 404 | mutex_lock(&ir->irctl_lock); |
| 405 | ir->d.set_use_dec(ir->d.data); |
Jarod Wilson | c1cbb70 | 2010-10-18 18:39:20 -0300 | [diff] [blame] | 406 | module_put(cdev->owner); |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 407 | mutex_unlock(&ir->irctl_lock); |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 408 | } else { |
Jarod Wilson | 578fcb8 | 2010-10-16 21:29:50 -0300 | [diff] [blame] | 409 | lirc_irctl_cleanup(ir); |
Jarod Wilson | c1cbb70 | 2010-10-18 18:39:20 -0300 | [diff] [blame] | 410 | cdev_del(cdev); |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 411 | kfree(ir); |
| 412 | irctls[minor] = NULL; |
| 413 | } |
| 414 | |
| 415 | mutex_unlock(&lirc_dev_lock); |
| 416 | |
| 417 | return 0; |
| 418 | } |
| 419 | EXPORT_SYMBOL(lirc_unregister_driver); |
| 420 | |
| 421 | int lirc_dev_fop_open(struct inode *inode, struct file *file) |
| 422 | { |
| 423 | struct irctl *ir; |
Jarod Wilson | c1cbb70 | 2010-10-18 18:39:20 -0300 | [diff] [blame] | 424 | struct cdev *cdev; |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 425 | int retval = 0; |
| 426 | |
| 427 | if (iminor(inode) >= MAX_IRCTL_DEVICES) { |
| 428 | printk(KERN_WARNING "lirc_dev [%d]: open result = -ENODEV\n", |
| 429 | iminor(inode)); |
| 430 | return -ENODEV; |
| 431 | } |
| 432 | |
| 433 | if (mutex_lock_interruptible(&lirc_dev_lock)) |
| 434 | return -ERESTARTSYS; |
| 435 | |
| 436 | ir = irctls[iminor(inode)]; |
| 437 | if (!ir) { |
| 438 | retval = -ENODEV; |
| 439 | goto error; |
| 440 | } |
| 441 | |
| 442 | dev_dbg(ir->d.dev, LOGHEAD "open called\n", ir->d.name, ir->d.minor); |
| 443 | |
| 444 | if (ir->d.minor == NOPLUG) { |
| 445 | retval = -ENODEV; |
| 446 | goto error; |
| 447 | } |
| 448 | |
| 449 | if (ir->open) { |
| 450 | retval = -EBUSY; |
| 451 | goto error; |
| 452 | } |
| 453 | |
Jarod Wilson | c1cbb70 | 2010-10-18 18:39:20 -0300 | [diff] [blame] | 454 | cdev = &cdevs[iminor(inode)]; |
| 455 | if (try_module_get(cdev->owner)) { |
| 456 | ir->open++; |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 457 | retval = ir->d.set_use_inc(ir->d.data); |
| 458 | |
| 459 | if (retval) { |
Jarod Wilson | c1cbb70 | 2010-10-18 18:39:20 -0300 | [diff] [blame] | 460 | module_put(cdev->owner); |
| 461 | ir->open--; |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 462 | } else { |
| 463 | lirc_buffer_clear(ir->buf); |
| 464 | } |
| 465 | if (ir->task) |
| 466 | wake_up_process(ir->task); |
| 467 | } |
| 468 | |
| 469 | error: |
| 470 | if (ir) |
| 471 | dev_dbg(ir->d.dev, LOGHEAD "open result = %d\n", |
| 472 | ir->d.name, ir->d.minor, retval); |
| 473 | |
| 474 | mutex_unlock(&lirc_dev_lock); |
| 475 | |
Arnd Bergmann | d9d2e9d | 2010-08-15 18:51:56 +0200 | [diff] [blame] | 476 | nonseekable_open(inode, file); |
| 477 | |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 478 | return retval; |
| 479 | } |
| 480 | EXPORT_SYMBOL(lirc_dev_fop_open); |
| 481 | |
| 482 | int lirc_dev_fop_close(struct inode *inode, struct file *file) |
| 483 | { |
| 484 | struct irctl *ir = irctls[iminor(inode)]; |
Jarod Wilson | c1cbb70 | 2010-10-18 18:39:20 -0300 | [diff] [blame] | 485 | struct cdev *cdev = &cdevs[iminor(inode)]; |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 486 | |
Jarod Wilson | 715d29a | 2010-10-18 12:02:01 -0300 | [diff] [blame] | 487 | if (!ir) { |
| 488 | printk(KERN_ERR "%s: called with invalid irctl\n", __func__); |
| 489 | return -EINVAL; |
| 490 | } |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 491 | |
| 492 | dev_dbg(ir->d.dev, LOGHEAD "close called\n", ir->d.name, ir->d.minor); |
| 493 | |
| 494 | WARN_ON(mutex_lock_killable(&lirc_dev_lock)); |
| 495 | |
Jarod Wilson | 715d29a | 2010-10-18 12:02:01 -0300 | [diff] [blame] | 496 | ir->open--; |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 497 | if (ir->attached) { |
| 498 | ir->d.set_use_dec(ir->d.data); |
Jarod Wilson | c1cbb70 | 2010-10-18 18:39:20 -0300 | [diff] [blame] | 499 | module_put(cdev->owner); |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 500 | } else { |
Jarod Wilson | 578fcb8 | 2010-10-16 21:29:50 -0300 | [diff] [blame] | 501 | lirc_irctl_cleanup(ir); |
Jarod Wilson | c1cbb70 | 2010-10-18 18:39:20 -0300 | [diff] [blame] | 502 | cdev_del(cdev); |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 503 | irctls[ir->d.minor] = NULL; |
| 504 | kfree(ir); |
| 505 | } |
| 506 | |
| 507 | mutex_unlock(&lirc_dev_lock); |
| 508 | |
| 509 | return 0; |
| 510 | } |
| 511 | EXPORT_SYMBOL(lirc_dev_fop_close); |
| 512 | |
| 513 | unsigned int lirc_dev_fop_poll(struct file *file, poll_table *wait) |
| 514 | { |
| 515 | struct irctl *ir = irctls[iminor(file->f_dentry->d_inode)]; |
| 516 | unsigned int ret; |
| 517 | |
Jarod Wilson | 715d29a | 2010-10-18 12:02:01 -0300 | [diff] [blame] | 518 | if (!ir) { |
| 519 | printk(KERN_ERR "%s: called with invalid irctl\n", __func__); |
| 520 | return POLLERR; |
| 521 | } |
| 522 | |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 523 | dev_dbg(ir->d.dev, LOGHEAD "poll called\n", ir->d.name, ir->d.minor); |
| 524 | |
| 525 | if (!ir->attached) { |
| 526 | mutex_unlock(&ir->irctl_lock); |
| 527 | return POLLERR; |
| 528 | } |
| 529 | |
| 530 | poll_wait(file, &ir->buf->wait_poll, wait); |
| 531 | |
| 532 | if (ir->buf) |
| 533 | if (lirc_buffer_empty(ir->buf)) |
| 534 | ret = 0; |
| 535 | else |
| 536 | ret = POLLIN | POLLRDNORM; |
| 537 | else |
| 538 | ret = POLLERR; |
| 539 | |
| 540 | dev_dbg(ir->d.dev, LOGHEAD "poll result = %d\n", |
| 541 | ir->d.name, ir->d.minor, ret); |
| 542 | |
| 543 | return ret; |
| 544 | } |
| 545 | EXPORT_SYMBOL(lirc_dev_fop_poll); |
| 546 | |
Arnd Bergmann | 044e587 | 2010-08-02 15:43:35 -0300 | [diff] [blame] | 547 | long lirc_dev_fop_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 548 | { |
Jarod Wilson | be1f985 | 2010-10-08 17:24:21 -0300 | [diff] [blame] | 549 | __u32 mode; |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 550 | int result = 0; |
Jarod Wilson | d889a13 | 2010-10-16 21:36:43 -0300 | [diff] [blame] | 551 | struct irctl *ir = irctls[iminor(file->f_dentry->d_inode)]; |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 552 | |
Jarod Wilson | 8be292c | 2010-10-09 15:07:06 -0300 | [diff] [blame] | 553 | if (!ir) { |
| 554 | printk(KERN_ERR "lirc_dev: %s: no irctl found!\n", __func__); |
| 555 | return -ENODEV; |
| 556 | } |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 557 | |
| 558 | dev_dbg(ir->d.dev, LOGHEAD "ioctl called (0x%x)\n", |
| 559 | ir->d.name, ir->d.minor, cmd); |
| 560 | |
| 561 | if (ir->d.minor == NOPLUG || !ir->attached) { |
| 562 | dev_dbg(ir->d.dev, LOGHEAD "ioctl result = -ENODEV\n", |
| 563 | ir->d.name, ir->d.minor); |
| 564 | return -ENODEV; |
| 565 | } |
| 566 | |
| 567 | mutex_lock(&ir->irctl_lock); |
| 568 | |
| 569 | switch (cmd) { |
| 570 | case LIRC_GET_FEATURES: |
Jarod Wilson | be1f985 | 2010-10-08 17:24:21 -0300 | [diff] [blame] | 571 | result = put_user(ir->d.features, (__u32 *)arg); |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 572 | break; |
| 573 | case LIRC_GET_REC_MODE: |
| 574 | if (!(ir->d.features & LIRC_CAN_REC_MASK)) { |
| 575 | result = -ENOSYS; |
| 576 | break; |
| 577 | } |
| 578 | |
| 579 | result = put_user(LIRC_REC2MODE |
| 580 | (ir->d.features & LIRC_CAN_REC_MASK), |
Jarod Wilson | be1f985 | 2010-10-08 17:24:21 -0300 | [diff] [blame] | 581 | (__u32 *)arg); |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 582 | break; |
| 583 | case LIRC_SET_REC_MODE: |
| 584 | if (!(ir->d.features & LIRC_CAN_REC_MASK)) { |
| 585 | result = -ENOSYS; |
| 586 | break; |
| 587 | } |
| 588 | |
Jarod Wilson | be1f985 | 2010-10-08 17:24:21 -0300 | [diff] [blame] | 589 | result = get_user(mode, (__u32 *)arg); |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 590 | if (!result && !(LIRC_MODE2REC(mode) & ir->d.features)) |
| 591 | result = -EINVAL; |
| 592 | /* |
| 593 | * FIXME: We should actually set the mode somehow but |
| 594 | * for now, lirc_serial doesn't support mode changing either |
| 595 | */ |
| 596 | break; |
| 597 | case LIRC_GET_LENGTH: |
Jarod Wilson | be1f985 | 2010-10-08 17:24:21 -0300 | [diff] [blame] | 598 | result = put_user(ir->d.code_length, (__u32 *)arg); |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 599 | break; |
| 600 | case LIRC_GET_MIN_TIMEOUT: |
| 601 | if (!(ir->d.features & LIRC_CAN_SET_REC_TIMEOUT) || |
| 602 | ir->d.min_timeout == 0) { |
| 603 | result = -ENOSYS; |
| 604 | break; |
| 605 | } |
| 606 | |
Jarod Wilson | be1f985 | 2010-10-08 17:24:21 -0300 | [diff] [blame] | 607 | result = put_user(ir->d.min_timeout, (__u32 *)arg); |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 608 | break; |
| 609 | case LIRC_GET_MAX_TIMEOUT: |
| 610 | if (!(ir->d.features & LIRC_CAN_SET_REC_TIMEOUT) || |
| 611 | ir->d.max_timeout == 0) { |
| 612 | result = -ENOSYS; |
| 613 | break; |
| 614 | } |
| 615 | |
Jarod Wilson | be1f985 | 2010-10-08 17:24:21 -0300 | [diff] [blame] | 616 | result = put_user(ir->d.max_timeout, (__u32 *)arg); |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 617 | break; |
| 618 | default: |
| 619 | result = -EINVAL; |
| 620 | } |
| 621 | |
| 622 | dev_dbg(ir->d.dev, LOGHEAD "ioctl result = %d\n", |
| 623 | ir->d.name, ir->d.minor, result); |
| 624 | |
| 625 | mutex_unlock(&ir->irctl_lock); |
| 626 | |
| 627 | return result; |
| 628 | } |
| 629 | EXPORT_SYMBOL(lirc_dev_fop_ioctl); |
| 630 | |
| 631 | ssize_t lirc_dev_fop_read(struct file *file, |
| 632 | char *buffer, |
| 633 | size_t length, |
| 634 | loff_t *ppos) |
| 635 | { |
| 636 | struct irctl *ir = irctls[iminor(file->f_dentry->d_inode)]; |
Jarod Wilson | 715d29a | 2010-10-18 12:02:01 -0300 | [diff] [blame] | 637 | unsigned char *buf; |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 638 | int ret = 0, written = 0; |
| 639 | DECLARE_WAITQUEUE(wait, current); |
| 640 | |
Jarod Wilson | 715d29a | 2010-10-18 12:02:01 -0300 | [diff] [blame] | 641 | if (!ir) { |
| 642 | printk(KERN_ERR "%s: called with invalid irctl\n", __func__); |
| 643 | return -ENODEV; |
| 644 | } |
| 645 | |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 646 | dev_dbg(ir->d.dev, LOGHEAD "read called\n", ir->d.name, ir->d.minor); |
| 647 | |
Jarod Wilson | 715d29a | 2010-10-18 12:02:01 -0300 | [diff] [blame] | 648 | buf = kzalloc(ir->chunk_size, GFP_KERNEL); |
| 649 | if (!buf) |
| 650 | return -ENOMEM; |
| 651 | |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 652 | if (mutex_lock_interruptible(&ir->irctl_lock)) |
| 653 | return -ERESTARTSYS; |
| 654 | if (!ir->attached) { |
| 655 | mutex_unlock(&ir->irctl_lock); |
| 656 | return -ENODEV; |
| 657 | } |
| 658 | |
| 659 | if (length % ir->chunk_size) { |
| 660 | dev_dbg(ir->d.dev, LOGHEAD "read result = -EINVAL\n", |
| 661 | ir->d.name, ir->d.minor); |
| 662 | mutex_unlock(&ir->irctl_lock); |
| 663 | return -EINVAL; |
| 664 | } |
| 665 | |
| 666 | /* |
| 667 | * we add ourselves to the task queue before buffer check |
| 668 | * to avoid losing scan code (in case when queue is awaken somewhere |
| 669 | * between while condition checking and scheduling) |
| 670 | */ |
| 671 | add_wait_queue(&ir->buf->wait_poll, &wait); |
| 672 | set_current_state(TASK_INTERRUPTIBLE); |
| 673 | |
| 674 | /* |
| 675 | * while we didn't provide 'length' bytes, device is opened in blocking |
| 676 | * mode and 'copy_to_user' is happy, wait for data. |
| 677 | */ |
| 678 | while (written < length && ret == 0) { |
| 679 | if (lirc_buffer_empty(ir->buf)) { |
| 680 | /* According to the read(2) man page, 'written' can be |
| 681 | * returned as less than 'length', instead of blocking |
| 682 | * again, returning -EWOULDBLOCK, or returning |
| 683 | * -ERESTARTSYS */ |
| 684 | if (written) |
| 685 | break; |
| 686 | if (file->f_flags & O_NONBLOCK) { |
| 687 | ret = -EWOULDBLOCK; |
| 688 | break; |
| 689 | } |
| 690 | if (signal_pending(current)) { |
| 691 | ret = -ERESTARTSYS; |
| 692 | break; |
| 693 | } |
| 694 | |
| 695 | mutex_unlock(&ir->irctl_lock); |
| 696 | schedule(); |
| 697 | set_current_state(TASK_INTERRUPTIBLE); |
| 698 | |
| 699 | if (mutex_lock_interruptible(&ir->irctl_lock)) { |
| 700 | ret = -ERESTARTSYS; |
Jarod Wilson | 69c271f | 2010-07-07 11:29:44 -0300 | [diff] [blame] | 701 | remove_wait_queue(&ir->buf->wait_poll, &wait); |
| 702 | set_current_state(TASK_RUNNING); |
| 703 | goto out_unlocked; |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | if (!ir->attached) { |
| 707 | ret = -ENODEV; |
| 708 | break; |
| 709 | } |
| 710 | } else { |
| 711 | lirc_buffer_read(ir->buf, buf); |
| 712 | ret = copy_to_user((void *)buffer+written, buf, |
| 713 | ir->buf->chunk_size); |
| 714 | written += ir->buf->chunk_size; |
| 715 | } |
| 716 | } |
| 717 | |
| 718 | remove_wait_queue(&ir->buf->wait_poll, &wait); |
| 719 | set_current_state(TASK_RUNNING); |
| 720 | mutex_unlock(&ir->irctl_lock); |
| 721 | |
Jarod Wilson | 69c271f | 2010-07-07 11:29:44 -0300 | [diff] [blame] | 722 | out_unlocked: |
Jarod Wilson | 715d29a | 2010-10-18 12:02:01 -0300 | [diff] [blame] | 723 | kfree(buf); |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 724 | dev_dbg(ir->d.dev, LOGHEAD "read result = %s (%d)\n", |
| 725 | ir->d.name, ir->d.minor, ret ? "-EFAULT" : "OK", ret); |
| 726 | |
| 727 | return ret ? ret : written; |
| 728 | } |
| 729 | EXPORT_SYMBOL(lirc_dev_fop_read); |
| 730 | |
| 731 | void *lirc_get_pdata(struct file *file) |
| 732 | { |
| 733 | void *data = NULL; |
| 734 | |
| 735 | if (file && file->f_dentry && file->f_dentry->d_inode && |
| 736 | file->f_dentry->d_inode->i_rdev) { |
| 737 | struct irctl *ir; |
| 738 | ir = irctls[iminor(file->f_dentry->d_inode)]; |
| 739 | data = ir->d.data; |
| 740 | } |
| 741 | |
| 742 | return data; |
| 743 | } |
| 744 | EXPORT_SYMBOL(lirc_get_pdata); |
| 745 | |
| 746 | |
| 747 | ssize_t lirc_dev_fop_write(struct file *file, const char *buffer, |
| 748 | size_t length, loff_t *ppos) |
| 749 | { |
| 750 | struct irctl *ir = irctls[iminor(file->f_dentry->d_inode)]; |
| 751 | |
Jarod Wilson | 715d29a | 2010-10-18 12:02:01 -0300 | [diff] [blame] | 752 | if (!ir) { |
| 753 | printk(KERN_ERR "%s: called with invalid irctl\n", __func__); |
| 754 | return -ENODEV; |
| 755 | } |
| 756 | |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 757 | dev_dbg(ir->d.dev, LOGHEAD "write called\n", ir->d.name, ir->d.minor); |
| 758 | |
| 759 | if (!ir->attached) |
| 760 | return -ENODEV; |
| 761 | |
| 762 | return -EINVAL; |
| 763 | } |
| 764 | EXPORT_SYMBOL(lirc_dev_fop_write); |
| 765 | |
| 766 | |
| 767 | static int __init lirc_dev_init(void) |
| 768 | { |
| 769 | int retval; |
| 770 | |
| 771 | lirc_class = class_create(THIS_MODULE, "lirc"); |
| 772 | if (IS_ERR(lirc_class)) { |
| 773 | retval = PTR_ERR(lirc_class); |
| 774 | printk(KERN_ERR "lirc_dev: class_create failed\n"); |
| 775 | goto error; |
| 776 | } |
| 777 | |
| 778 | retval = alloc_chrdev_region(&lirc_base_dev, 0, MAX_IRCTL_DEVICES, |
| 779 | IRCTL_DEV_NAME); |
| 780 | if (retval) { |
| 781 | class_destroy(lirc_class); |
| 782 | printk(KERN_ERR "lirc_dev: alloc_chrdev_region failed\n"); |
| 783 | goto error; |
| 784 | } |
| 785 | |
| 786 | |
| 787 | printk(KERN_INFO "lirc_dev: IR Remote Control driver registered, " |
| 788 | "major %d \n", MAJOR(lirc_base_dev)); |
| 789 | |
| 790 | error: |
| 791 | return retval; |
| 792 | } |
| 793 | |
| 794 | |
| 795 | |
| 796 | static void __exit lirc_dev_exit(void) |
| 797 | { |
| 798 | class_destroy(lirc_class); |
| 799 | unregister_chrdev_region(lirc_base_dev, MAX_IRCTL_DEVICES); |
| 800 | printk(KERN_INFO "lirc_dev: module unloaded\n"); |
| 801 | } |
| 802 | |
| 803 | module_init(lirc_dev_init); |
| 804 | module_exit(lirc_dev_exit); |
| 805 | |
| 806 | MODULE_DESCRIPTION("LIRC base driver module"); |
| 807 | MODULE_AUTHOR("Artur Lipowski"); |
| 808 | MODULE_LICENSE("GPL"); |
| 809 | |
| 810 | module_param(debug, bool, S_IRUGO | S_IWUSR); |
| 811 | MODULE_PARM_DESC(debug, "Enable debugging messages"); |