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 | |
Dan Carpenter | 5c769a6 | 2010-11-17 02:12:23 -0300 | [diff] [blame] | 525 | if (!ir->attached) |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 526 | return POLLERR; |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 527 | |
| 528 | poll_wait(file, &ir->buf->wait_poll, wait); |
| 529 | |
| 530 | if (ir->buf) |
| 531 | if (lirc_buffer_empty(ir->buf)) |
| 532 | ret = 0; |
| 533 | else |
| 534 | ret = POLLIN | POLLRDNORM; |
| 535 | else |
| 536 | ret = POLLERR; |
| 537 | |
| 538 | dev_dbg(ir->d.dev, LOGHEAD "poll result = %d\n", |
| 539 | ir->d.name, ir->d.minor, ret); |
| 540 | |
| 541 | return ret; |
| 542 | } |
| 543 | EXPORT_SYMBOL(lirc_dev_fop_poll); |
| 544 | |
Arnd Bergmann | 044e587 | 2010-08-02 15:43:35 -0300 | [diff] [blame] | 545 | 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] | 546 | { |
Jarod Wilson | be1f985 | 2010-10-08 17:24:21 -0300 | [diff] [blame] | 547 | __u32 mode; |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 548 | int result = 0; |
Jarod Wilson | d889a13 | 2010-10-16 21:36:43 -0300 | [diff] [blame] | 549 | struct irctl *ir = irctls[iminor(file->f_dentry->d_inode)]; |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 550 | |
Jarod Wilson | 8be292c | 2010-10-09 15:07:06 -0300 | [diff] [blame] | 551 | if (!ir) { |
| 552 | printk(KERN_ERR "lirc_dev: %s: no irctl found!\n", __func__); |
| 553 | return -ENODEV; |
| 554 | } |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 555 | |
| 556 | dev_dbg(ir->d.dev, LOGHEAD "ioctl called (0x%x)\n", |
| 557 | ir->d.name, ir->d.minor, cmd); |
| 558 | |
| 559 | if (ir->d.minor == NOPLUG || !ir->attached) { |
| 560 | dev_dbg(ir->d.dev, LOGHEAD "ioctl result = -ENODEV\n", |
| 561 | ir->d.name, ir->d.minor); |
| 562 | return -ENODEV; |
| 563 | } |
| 564 | |
| 565 | mutex_lock(&ir->irctl_lock); |
| 566 | |
| 567 | switch (cmd) { |
| 568 | case LIRC_GET_FEATURES: |
Jarod Wilson | be1f985 | 2010-10-08 17:24:21 -0300 | [diff] [blame] | 569 | result = put_user(ir->d.features, (__u32 *)arg); |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 570 | break; |
| 571 | case LIRC_GET_REC_MODE: |
| 572 | if (!(ir->d.features & LIRC_CAN_REC_MASK)) { |
| 573 | result = -ENOSYS; |
| 574 | break; |
| 575 | } |
| 576 | |
| 577 | result = put_user(LIRC_REC2MODE |
| 578 | (ir->d.features & LIRC_CAN_REC_MASK), |
Jarod Wilson | be1f985 | 2010-10-08 17:24:21 -0300 | [diff] [blame] | 579 | (__u32 *)arg); |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 580 | break; |
| 581 | case LIRC_SET_REC_MODE: |
| 582 | if (!(ir->d.features & LIRC_CAN_REC_MASK)) { |
| 583 | result = -ENOSYS; |
| 584 | break; |
| 585 | } |
| 586 | |
Jarod Wilson | be1f985 | 2010-10-08 17:24:21 -0300 | [diff] [blame] | 587 | result = get_user(mode, (__u32 *)arg); |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 588 | if (!result && !(LIRC_MODE2REC(mode) & ir->d.features)) |
| 589 | result = -EINVAL; |
| 590 | /* |
| 591 | * FIXME: We should actually set the mode somehow but |
| 592 | * for now, lirc_serial doesn't support mode changing either |
| 593 | */ |
| 594 | break; |
| 595 | case LIRC_GET_LENGTH: |
Jarod Wilson | be1f985 | 2010-10-08 17:24:21 -0300 | [diff] [blame] | 596 | result = put_user(ir->d.code_length, (__u32 *)arg); |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 597 | break; |
| 598 | case LIRC_GET_MIN_TIMEOUT: |
| 599 | if (!(ir->d.features & LIRC_CAN_SET_REC_TIMEOUT) || |
| 600 | ir->d.min_timeout == 0) { |
| 601 | result = -ENOSYS; |
| 602 | break; |
| 603 | } |
| 604 | |
Jarod Wilson | be1f985 | 2010-10-08 17:24:21 -0300 | [diff] [blame] | 605 | result = put_user(ir->d.min_timeout, (__u32 *)arg); |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 606 | break; |
| 607 | case LIRC_GET_MAX_TIMEOUT: |
| 608 | if (!(ir->d.features & LIRC_CAN_SET_REC_TIMEOUT) || |
| 609 | ir->d.max_timeout == 0) { |
| 610 | result = -ENOSYS; |
| 611 | break; |
| 612 | } |
| 613 | |
Jarod Wilson | be1f985 | 2010-10-08 17:24:21 -0300 | [diff] [blame] | 614 | result = put_user(ir->d.max_timeout, (__u32 *)arg); |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 615 | break; |
| 616 | default: |
| 617 | result = -EINVAL; |
| 618 | } |
| 619 | |
| 620 | dev_dbg(ir->d.dev, LOGHEAD "ioctl result = %d\n", |
| 621 | ir->d.name, ir->d.minor, result); |
| 622 | |
| 623 | mutex_unlock(&ir->irctl_lock); |
| 624 | |
| 625 | return result; |
| 626 | } |
| 627 | EXPORT_SYMBOL(lirc_dev_fop_ioctl); |
| 628 | |
| 629 | ssize_t lirc_dev_fop_read(struct file *file, |
Dan Carpenter | 0e83508 | 2010-11-17 02:13:39 -0300 | [diff] [blame^] | 630 | char __user *buffer, |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 631 | size_t length, |
| 632 | loff_t *ppos) |
| 633 | { |
| 634 | struct irctl *ir = irctls[iminor(file->f_dentry->d_inode)]; |
Jarod Wilson | 715d29a | 2010-10-18 12:02:01 -0300 | [diff] [blame] | 635 | unsigned char *buf; |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 636 | int ret = 0, written = 0; |
| 637 | DECLARE_WAITQUEUE(wait, current); |
| 638 | |
Jarod Wilson | 715d29a | 2010-10-18 12:02:01 -0300 | [diff] [blame] | 639 | if (!ir) { |
| 640 | printk(KERN_ERR "%s: called with invalid irctl\n", __func__); |
| 641 | return -ENODEV; |
| 642 | } |
| 643 | |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 644 | dev_dbg(ir->d.dev, LOGHEAD "read called\n", ir->d.name, ir->d.minor); |
| 645 | |
Jarod Wilson | 715d29a | 2010-10-18 12:02:01 -0300 | [diff] [blame] | 646 | buf = kzalloc(ir->chunk_size, GFP_KERNEL); |
| 647 | if (!buf) |
| 648 | return -ENOMEM; |
| 649 | |
Dan Carpenter | 250f7a5 | 2010-11-17 02:20:15 -0300 | [diff] [blame] | 650 | if (mutex_lock_interruptible(&ir->irctl_lock)) { |
| 651 | ret = -ERESTARTSYS; |
| 652 | goto out_unlocked; |
| 653 | } |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 654 | if (!ir->attached) { |
Dan Carpenter | 250f7a5 | 2010-11-17 02:20:15 -0300 | [diff] [blame] | 655 | ret = -ENODEV; |
| 656 | goto out_locked; |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | if (length % ir->chunk_size) { |
Dan Carpenter | 250f7a5 | 2010-11-17 02:20:15 -0300 | [diff] [blame] | 660 | ret = -EINVAL; |
| 661 | goto out_locked; |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | /* |
| 665 | * we add ourselves to the task queue before buffer check |
| 666 | * to avoid losing scan code (in case when queue is awaken somewhere |
| 667 | * between while condition checking and scheduling) |
| 668 | */ |
| 669 | add_wait_queue(&ir->buf->wait_poll, &wait); |
| 670 | set_current_state(TASK_INTERRUPTIBLE); |
| 671 | |
| 672 | /* |
| 673 | * while we didn't provide 'length' bytes, device is opened in blocking |
| 674 | * mode and 'copy_to_user' is happy, wait for data. |
| 675 | */ |
| 676 | while (written < length && ret == 0) { |
| 677 | if (lirc_buffer_empty(ir->buf)) { |
| 678 | /* According to the read(2) man page, 'written' can be |
| 679 | * returned as less than 'length', instead of blocking |
| 680 | * again, returning -EWOULDBLOCK, or returning |
| 681 | * -ERESTARTSYS */ |
| 682 | if (written) |
| 683 | break; |
| 684 | if (file->f_flags & O_NONBLOCK) { |
| 685 | ret = -EWOULDBLOCK; |
| 686 | break; |
| 687 | } |
| 688 | if (signal_pending(current)) { |
| 689 | ret = -ERESTARTSYS; |
| 690 | break; |
| 691 | } |
| 692 | |
| 693 | mutex_unlock(&ir->irctl_lock); |
| 694 | schedule(); |
| 695 | set_current_state(TASK_INTERRUPTIBLE); |
| 696 | |
| 697 | if (mutex_lock_interruptible(&ir->irctl_lock)) { |
| 698 | ret = -ERESTARTSYS; |
Jarod Wilson | 69c271f | 2010-07-07 11:29:44 -0300 | [diff] [blame] | 699 | remove_wait_queue(&ir->buf->wait_poll, &wait); |
| 700 | set_current_state(TASK_RUNNING); |
| 701 | goto out_unlocked; |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | if (!ir->attached) { |
| 705 | ret = -ENODEV; |
| 706 | break; |
| 707 | } |
| 708 | } else { |
| 709 | lirc_buffer_read(ir->buf, buf); |
| 710 | ret = copy_to_user((void *)buffer+written, buf, |
| 711 | ir->buf->chunk_size); |
Dan Carpenter | 250f7a5 | 2010-11-17 02:20:15 -0300 | [diff] [blame] | 712 | if (!ret) |
| 713 | written += ir->buf->chunk_size; |
| 714 | else |
| 715 | ret = -EFAULT; |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 716 | } |
| 717 | } |
| 718 | |
| 719 | remove_wait_queue(&ir->buf->wait_poll, &wait); |
| 720 | set_current_state(TASK_RUNNING); |
Dan Carpenter | 250f7a5 | 2010-11-17 02:20:15 -0300 | [diff] [blame] | 721 | |
| 722 | out_locked: |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 723 | mutex_unlock(&ir->irctl_lock); |
| 724 | |
Jarod Wilson | 69c271f | 2010-07-07 11:29:44 -0300 | [diff] [blame] | 725 | out_unlocked: |
Jarod Wilson | 715d29a | 2010-10-18 12:02:01 -0300 | [diff] [blame] | 726 | kfree(buf); |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 727 | dev_dbg(ir->d.dev, LOGHEAD "read result = %s (%d)\n", |
Dan Carpenter | 250f7a5 | 2010-11-17 02:20:15 -0300 | [diff] [blame] | 728 | ir->d.name, ir->d.minor, ret ? "<fail>" : "<ok>", ret); |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 729 | |
| 730 | return ret ? ret : written; |
| 731 | } |
| 732 | EXPORT_SYMBOL(lirc_dev_fop_read); |
| 733 | |
| 734 | void *lirc_get_pdata(struct file *file) |
| 735 | { |
| 736 | void *data = NULL; |
| 737 | |
| 738 | if (file && file->f_dentry && file->f_dentry->d_inode && |
| 739 | file->f_dentry->d_inode->i_rdev) { |
| 740 | struct irctl *ir; |
| 741 | ir = irctls[iminor(file->f_dentry->d_inode)]; |
| 742 | data = ir->d.data; |
| 743 | } |
| 744 | |
| 745 | return data; |
| 746 | } |
| 747 | EXPORT_SYMBOL(lirc_get_pdata); |
| 748 | |
| 749 | |
Dan Carpenter | 0e83508 | 2010-11-17 02:13:39 -0300 | [diff] [blame^] | 750 | ssize_t lirc_dev_fop_write(struct file *file, const char __user *buffer, |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 751 | size_t length, loff_t *ppos) |
| 752 | { |
| 753 | struct irctl *ir = irctls[iminor(file->f_dentry->d_inode)]; |
| 754 | |
Jarod Wilson | 715d29a | 2010-10-18 12:02:01 -0300 | [diff] [blame] | 755 | if (!ir) { |
| 756 | printk(KERN_ERR "%s: called with invalid irctl\n", __func__); |
| 757 | return -ENODEV; |
| 758 | } |
| 759 | |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 760 | dev_dbg(ir->d.dev, LOGHEAD "write called\n", ir->d.name, ir->d.minor); |
| 761 | |
| 762 | if (!ir->attached) |
| 763 | return -ENODEV; |
| 764 | |
| 765 | return -EINVAL; |
| 766 | } |
| 767 | EXPORT_SYMBOL(lirc_dev_fop_write); |
| 768 | |
| 769 | |
| 770 | static int __init lirc_dev_init(void) |
| 771 | { |
| 772 | int retval; |
| 773 | |
| 774 | lirc_class = class_create(THIS_MODULE, "lirc"); |
| 775 | if (IS_ERR(lirc_class)) { |
| 776 | retval = PTR_ERR(lirc_class); |
| 777 | printk(KERN_ERR "lirc_dev: class_create failed\n"); |
| 778 | goto error; |
| 779 | } |
| 780 | |
| 781 | retval = alloc_chrdev_region(&lirc_base_dev, 0, MAX_IRCTL_DEVICES, |
| 782 | IRCTL_DEV_NAME); |
| 783 | if (retval) { |
| 784 | class_destroy(lirc_class); |
| 785 | printk(KERN_ERR "lirc_dev: alloc_chrdev_region failed\n"); |
| 786 | goto error; |
| 787 | } |
| 788 | |
| 789 | |
| 790 | printk(KERN_INFO "lirc_dev: IR Remote Control driver registered, " |
| 791 | "major %d \n", MAJOR(lirc_base_dev)); |
| 792 | |
| 793 | error: |
| 794 | return retval; |
| 795 | } |
| 796 | |
| 797 | |
| 798 | |
| 799 | static void __exit lirc_dev_exit(void) |
| 800 | { |
| 801 | class_destroy(lirc_class); |
| 802 | unregister_chrdev_region(lirc_base_dev, MAX_IRCTL_DEVICES); |
| 803 | printk(KERN_INFO "lirc_dev: module unloaded\n"); |
| 804 | } |
| 805 | |
| 806 | module_init(lirc_dev_init); |
| 807 | module_exit(lirc_dev_exit); |
| 808 | |
| 809 | MODULE_DESCRIPTION("LIRC base driver module"); |
| 810 | MODULE_AUTHOR("Artur Lipowski"); |
| 811 | MODULE_LICENSE("GPL"); |
| 812 | |
| 813 | module_param(debug, bool, S_IRUGO | S_IWUSR); |
| 814 | MODULE_PARM_DESC(debug, "Enable debugging messages"); |