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 | } |
Vasiliy Kulikov | b395cba | 2010-11-26 14:06:41 -0300 | [diff] [blame^] | 181 | retval = kobject_set_name(&cdev->kobj, "lirc%d", d->minor); |
| 182 | if (retval) |
| 183 | return retval; |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 184 | |
Jarod Wilson | c1cbb70 | 2010-10-18 18:39:20 -0300 | [diff] [blame] | 185 | retval = cdev_add(cdev, MKDEV(MAJOR(lirc_base_dev), d->minor), 1); |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 186 | if (retval) |
Jarod Wilson | c1cbb70 | 2010-10-18 18:39:20 -0300 | [diff] [blame] | 187 | kobject_put(&cdev->kobj); |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 188 | |
| 189 | return retval; |
| 190 | } |
| 191 | |
| 192 | int lirc_register_driver(struct lirc_driver *d) |
| 193 | { |
| 194 | struct irctl *ir; |
| 195 | int minor; |
| 196 | int bytes_in_key; |
| 197 | unsigned int chunk_size; |
| 198 | unsigned int buffer_size; |
| 199 | int err; |
| 200 | |
| 201 | if (!d) { |
| 202 | printk(KERN_ERR "lirc_dev: lirc_register_driver: " |
| 203 | "driver pointer must be not NULL!\n"); |
| 204 | err = -EBADRQC; |
| 205 | goto out; |
| 206 | } |
| 207 | |
Jarod Wilson | 715d29a | 2010-10-18 12:02:01 -0300 | [diff] [blame] | 208 | if (!d->dev) { |
| 209 | printk(KERN_ERR "%s: dev pointer not filled in!\n", __func__); |
| 210 | err = -EINVAL; |
| 211 | goto out; |
| 212 | } |
| 213 | |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 214 | if (MAX_IRCTL_DEVICES <= d->minor) { |
| 215 | dev_err(d->dev, "lirc_dev: lirc_register_driver: " |
| 216 | "\"minor\" must be between 0 and %d (%d)!\n", |
| 217 | MAX_IRCTL_DEVICES-1, d->minor); |
| 218 | err = -EBADRQC; |
| 219 | goto out; |
| 220 | } |
| 221 | |
| 222 | if (1 > d->code_length || (BUFLEN * 8) < d->code_length) { |
| 223 | dev_err(d->dev, "lirc_dev: lirc_register_driver: " |
| 224 | "code length in bits for minor (%d) " |
| 225 | "must be less than %d!\n", |
| 226 | d->minor, BUFLEN * 8); |
| 227 | err = -EBADRQC; |
| 228 | goto out; |
| 229 | } |
| 230 | |
| 231 | dev_dbg(d->dev, "lirc_dev: lirc_register_driver: sample_rate: %d\n", |
| 232 | d->sample_rate); |
| 233 | if (d->sample_rate) { |
| 234 | if (2 > d->sample_rate || HZ < d->sample_rate) { |
| 235 | dev_err(d->dev, "lirc_dev: lirc_register_driver: " |
| 236 | "sample_rate must be between 2 and %d!\n", HZ); |
| 237 | err = -EBADRQC; |
| 238 | goto out; |
| 239 | } |
| 240 | if (!d->add_to_buf) { |
| 241 | dev_err(d->dev, "lirc_dev: lirc_register_driver: " |
| 242 | "add_to_buf cannot be NULL when " |
| 243 | "sample_rate is set\n"); |
| 244 | err = -EBADRQC; |
| 245 | goto out; |
| 246 | } |
| 247 | } else if (!(d->fops && d->fops->read) && !d->rbuf) { |
| 248 | dev_err(d->dev, "lirc_dev: lirc_register_driver: " |
| 249 | "fops->read and rbuf cannot all be NULL!\n"); |
| 250 | err = -EBADRQC; |
| 251 | goto out; |
| 252 | } else if (!d->rbuf) { |
| 253 | if (!(d->fops && d->fops->read && d->fops->poll && |
Arnd Bergmann | 044e587 | 2010-08-02 15:43:35 -0300 | [diff] [blame] | 254 | d->fops->unlocked_ioctl)) { |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 255 | dev_err(d->dev, "lirc_dev: lirc_register_driver: " |
Arnd Bergmann | 044e587 | 2010-08-02 15:43:35 -0300 | [diff] [blame] | 256 | "neither read, poll nor unlocked_ioctl can be NULL!\n"); |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 257 | err = -EBADRQC; |
| 258 | goto out; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | mutex_lock(&lirc_dev_lock); |
| 263 | |
| 264 | minor = d->minor; |
| 265 | |
| 266 | if (minor < 0) { |
| 267 | /* find first free slot for driver */ |
| 268 | for (minor = 0; minor < MAX_IRCTL_DEVICES; minor++) |
| 269 | if (!irctls[minor]) |
| 270 | break; |
| 271 | if (MAX_IRCTL_DEVICES == minor) { |
| 272 | dev_err(d->dev, "lirc_dev: lirc_register_driver: " |
| 273 | "no free slots for drivers!\n"); |
| 274 | err = -ENOMEM; |
| 275 | goto out_lock; |
| 276 | } |
| 277 | } else if (irctls[minor]) { |
| 278 | dev_err(d->dev, "lirc_dev: lirc_register_driver: " |
| 279 | "minor (%d) just registered!\n", minor); |
| 280 | err = -EBUSY; |
| 281 | goto out_lock; |
| 282 | } |
| 283 | |
| 284 | ir = kzalloc(sizeof(struct irctl), GFP_KERNEL); |
| 285 | if (!ir) { |
| 286 | err = -ENOMEM; |
| 287 | goto out_lock; |
| 288 | } |
Jarod Wilson | 578fcb8 | 2010-10-16 21:29:50 -0300 | [diff] [blame] | 289 | lirc_irctl_init(ir); |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 290 | irctls[minor] = ir; |
| 291 | d->minor = minor; |
| 292 | |
| 293 | if (d->sample_rate) { |
| 294 | ir->jiffies_to_wait = HZ / d->sample_rate; |
| 295 | } else { |
| 296 | /* it means - wait for external event in task queue */ |
| 297 | ir->jiffies_to_wait = 0; |
| 298 | } |
| 299 | |
| 300 | /* some safety check 8-) */ |
| 301 | d->name[sizeof(d->name)-1] = '\0'; |
| 302 | |
| 303 | bytes_in_key = BITS_TO_LONGS(d->code_length) + |
| 304 | (d->code_length % 8 ? 1 : 0); |
| 305 | buffer_size = d->buffer_size ? d->buffer_size : BUFLEN / bytes_in_key; |
| 306 | chunk_size = d->chunk_size ? d->chunk_size : bytes_in_key; |
| 307 | |
| 308 | if (d->rbuf) { |
| 309 | ir->buf = d->rbuf; |
| 310 | } else { |
| 311 | ir->buf = kmalloc(sizeof(struct lirc_buffer), GFP_KERNEL); |
| 312 | if (!ir->buf) { |
| 313 | err = -ENOMEM; |
| 314 | goto out_lock; |
| 315 | } |
| 316 | err = lirc_buffer_init(ir->buf, chunk_size, buffer_size); |
| 317 | if (err) { |
| 318 | kfree(ir->buf); |
| 319 | goto out_lock; |
| 320 | } |
| 321 | } |
| 322 | ir->chunk_size = ir->buf->chunk_size; |
| 323 | |
| 324 | if (d->features == 0) |
| 325 | d->features = LIRC_CAN_REC_LIRCCODE; |
| 326 | |
| 327 | ir->d = *d; |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 328 | |
| 329 | device_create(lirc_class, ir->d.dev, |
| 330 | MKDEV(MAJOR(lirc_base_dev), ir->d.minor), NULL, |
| 331 | "lirc%u", ir->d.minor); |
| 332 | |
| 333 | if (d->sample_rate) { |
| 334 | /* try to fire up polling thread */ |
| 335 | ir->task = kthread_run(lirc_thread, (void *)ir, "lirc_dev"); |
| 336 | if (IS_ERR(ir->task)) { |
| 337 | dev_err(d->dev, "lirc_dev: lirc_register_driver: " |
| 338 | "cannot run poll thread for minor = %d\n", |
| 339 | d->minor); |
| 340 | err = -ECHILD; |
| 341 | goto out_sysfs; |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | err = lirc_cdev_add(ir); |
| 346 | if (err) |
| 347 | goto out_sysfs; |
| 348 | |
| 349 | ir->attached = 1; |
| 350 | mutex_unlock(&lirc_dev_lock); |
| 351 | |
| 352 | dev_info(ir->d.dev, "lirc_dev: driver %s registered at minor = %d\n", |
| 353 | ir->d.name, ir->d.minor); |
| 354 | return minor; |
| 355 | |
| 356 | out_sysfs: |
| 357 | device_destroy(lirc_class, MKDEV(MAJOR(lirc_base_dev), ir->d.minor)); |
| 358 | out_lock: |
| 359 | mutex_unlock(&lirc_dev_lock); |
| 360 | out: |
| 361 | return err; |
| 362 | } |
| 363 | EXPORT_SYMBOL(lirc_register_driver); |
| 364 | |
| 365 | int lirc_unregister_driver(int minor) |
| 366 | { |
| 367 | struct irctl *ir; |
Jarod Wilson | c1cbb70 | 2010-10-18 18:39:20 -0300 | [diff] [blame] | 368 | struct cdev *cdev; |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 369 | |
| 370 | if (minor < 0 || minor >= MAX_IRCTL_DEVICES) { |
Jarod Wilson | 4fc2154 | 2010-10-09 15:17:03 -0300 | [diff] [blame] | 371 | printk(KERN_ERR "lirc_dev: %s: minor (%d) must be between " |
| 372 | "0 and %d!\n", __func__, minor, MAX_IRCTL_DEVICES-1); |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 373 | return -EBADRQC; |
| 374 | } |
| 375 | |
| 376 | ir = irctls[minor]; |
Jarod Wilson | df1868e | 2010-09-17 18:12:31 -0300 | [diff] [blame] | 377 | if (!ir) { |
Jarod Wilson | 4fc2154 | 2010-10-09 15:17:03 -0300 | [diff] [blame] | 378 | printk(KERN_ERR "lirc_dev: %s: failed to get irctl struct " |
| 379 | "for minor %d!\n", __func__, minor); |
Jarod Wilson | df1868e | 2010-09-17 18:12:31 -0300 | [diff] [blame] | 380 | return -ENOENT; |
| 381 | } |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 382 | |
Jarod Wilson | c1cbb70 | 2010-10-18 18:39:20 -0300 | [diff] [blame] | 383 | cdev = &cdevs[minor]; |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 384 | |
| 385 | mutex_lock(&lirc_dev_lock); |
| 386 | |
| 387 | if (ir->d.minor != minor) { |
Jarod Wilson | 4fc2154 | 2010-10-09 15:17:03 -0300 | [diff] [blame] | 388 | printk(KERN_ERR "lirc_dev: %s: minor (%d) device not " |
| 389 | "registered!\n", __func__, minor); |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 390 | mutex_unlock(&lirc_dev_lock); |
| 391 | return -ENOENT; |
| 392 | } |
| 393 | |
| 394 | /* end up polling thread */ |
| 395 | if (ir->task) |
| 396 | kthread_stop(ir->task); |
| 397 | |
| 398 | dev_dbg(ir->d.dev, "lirc_dev: driver %s unregistered from minor = %d\n", |
| 399 | ir->d.name, ir->d.minor); |
| 400 | |
| 401 | ir->attached = 0; |
| 402 | if (ir->open) { |
| 403 | dev_dbg(ir->d.dev, LOGHEAD "releasing opened driver\n", |
| 404 | ir->d.name, ir->d.minor); |
| 405 | wake_up_interruptible(&ir->buf->wait_poll); |
| 406 | mutex_lock(&ir->irctl_lock); |
| 407 | ir->d.set_use_dec(ir->d.data); |
Jarod Wilson | c1cbb70 | 2010-10-18 18:39:20 -0300 | [diff] [blame] | 408 | module_put(cdev->owner); |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 409 | mutex_unlock(&ir->irctl_lock); |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 410 | } else { |
Jarod Wilson | 578fcb8 | 2010-10-16 21:29:50 -0300 | [diff] [blame] | 411 | lirc_irctl_cleanup(ir); |
Jarod Wilson | c1cbb70 | 2010-10-18 18:39:20 -0300 | [diff] [blame] | 412 | cdev_del(cdev); |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 413 | kfree(ir); |
| 414 | irctls[minor] = NULL; |
| 415 | } |
| 416 | |
| 417 | mutex_unlock(&lirc_dev_lock); |
| 418 | |
| 419 | return 0; |
| 420 | } |
| 421 | EXPORT_SYMBOL(lirc_unregister_driver); |
| 422 | |
| 423 | int lirc_dev_fop_open(struct inode *inode, struct file *file) |
| 424 | { |
| 425 | struct irctl *ir; |
Jarod Wilson | c1cbb70 | 2010-10-18 18:39:20 -0300 | [diff] [blame] | 426 | struct cdev *cdev; |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 427 | int retval = 0; |
| 428 | |
| 429 | if (iminor(inode) >= MAX_IRCTL_DEVICES) { |
| 430 | printk(KERN_WARNING "lirc_dev [%d]: open result = -ENODEV\n", |
| 431 | iminor(inode)); |
| 432 | return -ENODEV; |
| 433 | } |
| 434 | |
| 435 | if (mutex_lock_interruptible(&lirc_dev_lock)) |
| 436 | return -ERESTARTSYS; |
| 437 | |
| 438 | ir = irctls[iminor(inode)]; |
| 439 | if (!ir) { |
| 440 | retval = -ENODEV; |
| 441 | goto error; |
| 442 | } |
| 443 | |
| 444 | dev_dbg(ir->d.dev, LOGHEAD "open called\n", ir->d.name, ir->d.minor); |
| 445 | |
| 446 | if (ir->d.minor == NOPLUG) { |
| 447 | retval = -ENODEV; |
| 448 | goto error; |
| 449 | } |
| 450 | |
| 451 | if (ir->open) { |
| 452 | retval = -EBUSY; |
| 453 | goto error; |
| 454 | } |
| 455 | |
Jarod Wilson | c1cbb70 | 2010-10-18 18:39:20 -0300 | [diff] [blame] | 456 | cdev = &cdevs[iminor(inode)]; |
| 457 | if (try_module_get(cdev->owner)) { |
| 458 | ir->open++; |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 459 | retval = ir->d.set_use_inc(ir->d.data); |
| 460 | |
| 461 | if (retval) { |
Jarod Wilson | c1cbb70 | 2010-10-18 18:39:20 -0300 | [diff] [blame] | 462 | module_put(cdev->owner); |
| 463 | ir->open--; |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 464 | } else { |
| 465 | lirc_buffer_clear(ir->buf); |
| 466 | } |
| 467 | if (ir->task) |
| 468 | wake_up_process(ir->task); |
| 469 | } |
| 470 | |
| 471 | error: |
| 472 | if (ir) |
| 473 | dev_dbg(ir->d.dev, LOGHEAD "open result = %d\n", |
| 474 | ir->d.name, ir->d.minor, retval); |
| 475 | |
| 476 | mutex_unlock(&lirc_dev_lock); |
| 477 | |
Arnd Bergmann | d9d2e9d | 2010-08-15 18:51:56 +0200 | [diff] [blame] | 478 | nonseekable_open(inode, file); |
| 479 | |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 480 | return retval; |
| 481 | } |
| 482 | EXPORT_SYMBOL(lirc_dev_fop_open); |
| 483 | |
| 484 | int lirc_dev_fop_close(struct inode *inode, struct file *file) |
| 485 | { |
| 486 | struct irctl *ir = irctls[iminor(inode)]; |
Jarod Wilson | c1cbb70 | 2010-10-18 18:39:20 -0300 | [diff] [blame] | 487 | struct cdev *cdev = &cdevs[iminor(inode)]; |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 488 | |
Jarod Wilson | 715d29a | 2010-10-18 12:02:01 -0300 | [diff] [blame] | 489 | if (!ir) { |
| 490 | printk(KERN_ERR "%s: called with invalid irctl\n", __func__); |
| 491 | return -EINVAL; |
| 492 | } |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 493 | |
| 494 | dev_dbg(ir->d.dev, LOGHEAD "close called\n", ir->d.name, ir->d.minor); |
| 495 | |
| 496 | WARN_ON(mutex_lock_killable(&lirc_dev_lock)); |
| 497 | |
Jarod Wilson | 715d29a | 2010-10-18 12:02:01 -0300 | [diff] [blame] | 498 | ir->open--; |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 499 | if (ir->attached) { |
| 500 | ir->d.set_use_dec(ir->d.data); |
Jarod Wilson | c1cbb70 | 2010-10-18 18:39:20 -0300 | [diff] [blame] | 501 | module_put(cdev->owner); |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 502 | } else { |
Jarod Wilson | 578fcb8 | 2010-10-16 21:29:50 -0300 | [diff] [blame] | 503 | lirc_irctl_cleanup(ir); |
Jarod Wilson | c1cbb70 | 2010-10-18 18:39:20 -0300 | [diff] [blame] | 504 | cdev_del(cdev); |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 505 | irctls[ir->d.minor] = NULL; |
| 506 | kfree(ir); |
| 507 | } |
| 508 | |
| 509 | mutex_unlock(&lirc_dev_lock); |
| 510 | |
| 511 | return 0; |
| 512 | } |
| 513 | EXPORT_SYMBOL(lirc_dev_fop_close); |
| 514 | |
| 515 | unsigned int lirc_dev_fop_poll(struct file *file, poll_table *wait) |
| 516 | { |
| 517 | struct irctl *ir = irctls[iminor(file->f_dentry->d_inode)]; |
| 518 | unsigned int ret; |
| 519 | |
Jarod Wilson | 715d29a | 2010-10-18 12:02:01 -0300 | [diff] [blame] | 520 | if (!ir) { |
| 521 | printk(KERN_ERR "%s: called with invalid irctl\n", __func__); |
| 522 | return POLLERR; |
| 523 | } |
| 524 | |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 525 | dev_dbg(ir->d.dev, LOGHEAD "poll called\n", ir->d.name, ir->d.minor); |
| 526 | |
Dan Carpenter | 5c769a6 | 2010-11-17 02:12:23 -0300 | [diff] [blame] | 527 | if (!ir->attached) |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 528 | return POLLERR; |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 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, |
Dan Carpenter | 0e83508 | 2010-11-17 02:13:39 -0300 | [diff] [blame] | 632 | char __user *buffer, |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 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 | |
Dan Carpenter | 250f7a5 | 2010-11-17 02:20:15 -0300 | [diff] [blame] | 652 | if (mutex_lock_interruptible(&ir->irctl_lock)) { |
| 653 | ret = -ERESTARTSYS; |
| 654 | goto out_unlocked; |
| 655 | } |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 656 | if (!ir->attached) { |
Dan Carpenter | 250f7a5 | 2010-11-17 02:20:15 -0300 | [diff] [blame] | 657 | ret = -ENODEV; |
| 658 | goto out_locked; |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | if (length % ir->chunk_size) { |
Dan Carpenter | 250f7a5 | 2010-11-17 02:20:15 -0300 | [diff] [blame] | 662 | ret = -EINVAL; |
| 663 | goto out_locked; |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 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); |
Dan Carpenter | 250f7a5 | 2010-11-17 02:20:15 -0300 | [diff] [blame] | 714 | if (!ret) |
| 715 | written += ir->buf->chunk_size; |
| 716 | else |
| 717 | ret = -EFAULT; |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 718 | } |
| 719 | } |
| 720 | |
| 721 | remove_wait_queue(&ir->buf->wait_poll, &wait); |
| 722 | set_current_state(TASK_RUNNING); |
Dan Carpenter | 250f7a5 | 2010-11-17 02:20:15 -0300 | [diff] [blame] | 723 | |
| 724 | out_locked: |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 725 | mutex_unlock(&ir->irctl_lock); |
| 726 | |
Jarod Wilson | 69c271f | 2010-07-07 11:29:44 -0300 | [diff] [blame] | 727 | out_unlocked: |
Jarod Wilson | 715d29a | 2010-10-18 12:02:01 -0300 | [diff] [blame] | 728 | kfree(buf); |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 729 | dev_dbg(ir->d.dev, LOGHEAD "read result = %s (%d)\n", |
Dan Carpenter | 250f7a5 | 2010-11-17 02:20:15 -0300 | [diff] [blame] | 730 | ir->d.name, ir->d.minor, ret ? "<fail>" : "<ok>", ret); |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 731 | |
| 732 | return ret ? ret : written; |
| 733 | } |
| 734 | EXPORT_SYMBOL(lirc_dev_fop_read); |
| 735 | |
| 736 | void *lirc_get_pdata(struct file *file) |
| 737 | { |
| 738 | void *data = NULL; |
| 739 | |
| 740 | if (file && file->f_dentry && file->f_dentry->d_inode && |
| 741 | file->f_dentry->d_inode->i_rdev) { |
| 742 | struct irctl *ir; |
| 743 | ir = irctls[iminor(file->f_dentry->d_inode)]; |
| 744 | data = ir->d.data; |
| 745 | } |
| 746 | |
| 747 | return data; |
| 748 | } |
| 749 | EXPORT_SYMBOL(lirc_get_pdata); |
| 750 | |
| 751 | |
Dan Carpenter | 0e83508 | 2010-11-17 02:13:39 -0300 | [diff] [blame] | 752 | 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] | 753 | size_t length, loff_t *ppos) |
| 754 | { |
| 755 | struct irctl *ir = irctls[iminor(file->f_dentry->d_inode)]; |
| 756 | |
Jarod Wilson | 715d29a | 2010-10-18 12:02:01 -0300 | [diff] [blame] | 757 | if (!ir) { |
| 758 | printk(KERN_ERR "%s: called with invalid irctl\n", __func__); |
| 759 | return -ENODEV; |
| 760 | } |
| 761 | |
Jarod Wilson | 4a62a5a | 2010-07-03 01:06:57 -0300 | [diff] [blame] | 762 | dev_dbg(ir->d.dev, LOGHEAD "write called\n", ir->d.name, ir->d.minor); |
| 763 | |
| 764 | if (!ir->attached) |
| 765 | return -ENODEV; |
| 766 | |
| 767 | return -EINVAL; |
| 768 | } |
| 769 | EXPORT_SYMBOL(lirc_dev_fop_write); |
| 770 | |
| 771 | |
| 772 | static int __init lirc_dev_init(void) |
| 773 | { |
| 774 | int retval; |
| 775 | |
| 776 | lirc_class = class_create(THIS_MODULE, "lirc"); |
| 777 | if (IS_ERR(lirc_class)) { |
| 778 | retval = PTR_ERR(lirc_class); |
| 779 | printk(KERN_ERR "lirc_dev: class_create failed\n"); |
| 780 | goto error; |
| 781 | } |
| 782 | |
| 783 | retval = alloc_chrdev_region(&lirc_base_dev, 0, MAX_IRCTL_DEVICES, |
| 784 | IRCTL_DEV_NAME); |
| 785 | if (retval) { |
| 786 | class_destroy(lirc_class); |
| 787 | printk(KERN_ERR "lirc_dev: alloc_chrdev_region failed\n"); |
| 788 | goto error; |
| 789 | } |
| 790 | |
| 791 | |
| 792 | printk(KERN_INFO "lirc_dev: IR Remote Control driver registered, " |
| 793 | "major %d \n", MAJOR(lirc_base_dev)); |
| 794 | |
| 795 | error: |
| 796 | return retval; |
| 797 | } |
| 798 | |
| 799 | |
| 800 | |
| 801 | static void __exit lirc_dev_exit(void) |
| 802 | { |
| 803 | class_destroy(lirc_class); |
| 804 | unregister_chrdev_region(lirc_base_dev, MAX_IRCTL_DEVICES); |
| 805 | printk(KERN_INFO "lirc_dev: module unloaded\n"); |
| 806 | } |
| 807 | |
| 808 | module_init(lirc_dev_init); |
| 809 | module_exit(lirc_dev_exit); |
| 810 | |
| 811 | MODULE_DESCRIPTION("LIRC base driver module"); |
| 812 | MODULE_AUTHOR("Artur Lipowski"); |
| 813 | MODULE_LICENSE("GPL"); |
| 814 | |
| 815 | module_param(debug, bool, S_IRUGO | S_IWUSR); |
| 816 | MODULE_PARM_DESC(debug, "Enable debugging messages"); |