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