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