Stefan Achatz | 206f5f2 | 2010-05-19 18:55:16 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Roccat driver for Linux |
| 3 | * |
| 4 | * Copyright (c) 2010 Stefan Achatz <erazor_de@users.sourceforge.net> |
| 5 | */ |
| 6 | |
| 7 | /* |
| 8 | * This program is free software; you can redistribute it and/or modify it |
| 9 | * under the terms of the GNU General Public License as published by the Free |
| 10 | * Software Foundation; either version 2 of the License, or (at your option) |
| 11 | * any later version. |
| 12 | */ |
| 13 | |
| 14 | /* |
| 15 | * Module roccat is a char device used to report special events of roccat |
| 16 | * hardware to userland. These events include requests for on-screen-display of |
| 17 | * profile or dpi settings or requests for execution of macro sequences that are |
| 18 | * not stored in device. The information in these events depends on hid device |
| 19 | * implementation and contains data that is not available in a single hid event |
| 20 | * or else hidraw could have been used. |
| 21 | * It is inspired by hidraw, but uses only one circular buffer for all readers. |
| 22 | */ |
| 23 | |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 24 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 25 | |
Stefan Achatz | 206f5f2 | 2010-05-19 18:55:16 +0200 | [diff] [blame] | 26 | #include <linux/cdev.h> |
| 27 | #include <linux/poll.h> |
| 28 | #include <linux/sched.h> |
Stefan Achatz | 5dc0c98 | 2011-02-03 16:14:43 +0100 | [diff] [blame] | 29 | #include <linux/hid-roccat.h> |
Paul Gortmaker | 8f86a2c | 2011-07-03 13:39:48 -0400 | [diff] [blame] | 30 | #include <linux/module.h> |
Stefan Achatz | 206f5f2 | 2010-05-19 18:55:16 +0200 | [diff] [blame] | 31 | |
| 32 | #define ROCCAT_FIRST_MINOR 0 |
| 33 | #define ROCCAT_MAX_DEVICES 8 |
| 34 | |
| 35 | /* should be a power of 2 for performance reason */ |
| 36 | #define ROCCAT_CBUF_SIZE 16 |
| 37 | |
| 38 | struct roccat_report { |
| 39 | uint8_t *value; |
Stefan Achatz | 206f5f2 | 2010-05-19 18:55:16 +0200 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | struct roccat_device { |
| 43 | unsigned int minor; |
Stefan Achatz | 8211e46 | 2011-01-30 13:38:25 +0100 | [diff] [blame] | 44 | int report_size; |
Stefan Achatz | 206f5f2 | 2010-05-19 18:55:16 +0200 | [diff] [blame] | 45 | int open; |
| 46 | int exist; |
| 47 | wait_queue_head_t wait; |
| 48 | struct device *dev; |
| 49 | struct hid_device *hid; |
| 50 | struct list_head readers; |
| 51 | /* protects modifications of readers list */ |
| 52 | struct mutex readers_lock; |
| 53 | |
| 54 | /* |
| 55 | * circular_buffer has one writer and multiple readers with their own |
| 56 | * read pointers |
| 57 | */ |
| 58 | struct roccat_report cbuf[ROCCAT_CBUF_SIZE]; |
| 59 | int cbuf_end; |
| 60 | struct mutex cbuf_lock; |
| 61 | }; |
| 62 | |
| 63 | struct roccat_reader { |
| 64 | struct list_head node; |
| 65 | struct roccat_device *device; |
| 66 | int cbuf_start; |
| 67 | }; |
| 68 | |
| 69 | static int roccat_major; |
Stefan Achatz | 206f5f2 | 2010-05-19 18:55:16 +0200 | [diff] [blame] | 70 | static struct cdev roccat_cdev; |
| 71 | |
| 72 | static struct roccat_device *devices[ROCCAT_MAX_DEVICES]; |
| 73 | /* protects modifications of devices array */ |
| 74 | static DEFINE_MUTEX(devices_lock); |
| 75 | |
| 76 | static ssize_t roccat_read(struct file *file, char __user *buffer, |
| 77 | size_t count, loff_t *ppos) |
| 78 | { |
| 79 | struct roccat_reader *reader = file->private_data; |
| 80 | struct roccat_device *device = reader->device; |
| 81 | struct roccat_report *report; |
| 82 | ssize_t retval = 0, len; |
| 83 | DECLARE_WAITQUEUE(wait, current); |
| 84 | |
| 85 | mutex_lock(&device->cbuf_lock); |
| 86 | |
| 87 | /* no data? */ |
| 88 | if (reader->cbuf_start == device->cbuf_end) { |
| 89 | add_wait_queue(&device->wait, &wait); |
| 90 | set_current_state(TASK_INTERRUPTIBLE); |
| 91 | |
| 92 | /* wait for data */ |
| 93 | while (reader->cbuf_start == device->cbuf_end) { |
| 94 | if (file->f_flags & O_NONBLOCK) { |
| 95 | retval = -EAGAIN; |
| 96 | break; |
| 97 | } |
| 98 | if (signal_pending(current)) { |
| 99 | retval = -ERESTARTSYS; |
| 100 | break; |
| 101 | } |
| 102 | if (!device->exist) { |
| 103 | retval = -EIO; |
| 104 | break; |
| 105 | } |
| 106 | |
| 107 | mutex_unlock(&device->cbuf_lock); |
| 108 | schedule(); |
| 109 | mutex_lock(&device->cbuf_lock); |
| 110 | set_current_state(TASK_INTERRUPTIBLE); |
| 111 | } |
| 112 | |
| 113 | set_current_state(TASK_RUNNING); |
| 114 | remove_wait_queue(&device->wait, &wait); |
| 115 | } |
| 116 | |
| 117 | /* here we either have data or a reason to return if retval is set */ |
| 118 | if (retval) |
| 119 | goto exit_unlock; |
| 120 | |
| 121 | report = &device->cbuf[reader->cbuf_start]; |
| 122 | /* |
| 123 | * If report is larger than requested amount of data, rest of report |
| 124 | * is lost! |
| 125 | */ |
Stefan Achatz | 8211e46 | 2011-01-30 13:38:25 +0100 | [diff] [blame] | 126 | len = device->report_size > count ? count : device->report_size; |
Stefan Achatz | 206f5f2 | 2010-05-19 18:55:16 +0200 | [diff] [blame] | 127 | |
| 128 | if (copy_to_user(buffer, report->value, len)) { |
| 129 | retval = -EFAULT; |
| 130 | goto exit_unlock; |
| 131 | } |
| 132 | retval += len; |
| 133 | reader->cbuf_start = (reader->cbuf_start + 1) % ROCCAT_CBUF_SIZE; |
| 134 | |
| 135 | exit_unlock: |
| 136 | mutex_unlock(&device->cbuf_lock); |
| 137 | return retval; |
| 138 | } |
| 139 | |
| 140 | static unsigned int roccat_poll(struct file *file, poll_table *wait) |
| 141 | { |
| 142 | struct roccat_reader *reader = file->private_data; |
| 143 | poll_wait(file, &reader->device->wait, wait); |
| 144 | if (reader->cbuf_start != reader->device->cbuf_end) |
| 145 | return POLLIN | POLLRDNORM; |
| 146 | if (!reader->device->exist) |
| 147 | return POLLERR | POLLHUP; |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | static int roccat_open(struct inode *inode, struct file *file) |
| 152 | { |
| 153 | unsigned int minor = iminor(inode); |
| 154 | struct roccat_reader *reader; |
| 155 | struct roccat_device *device; |
| 156 | int error = 0; |
| 157 | |
| 158 | reader = kzalloc(sizeof(struct roccat_reader), GFP_KERNEL); |
| 159 | if (!reader) |
| 160 | return -ENOMEM; |
| 161 | |
| 162 | mutex_lock(&devices_lock); |
| 163 | |
| 164 | device = devices[minor]; |
| 165 | |
Stefan Achatz | 206f5f2 | 2010-05-19 18:55:16 +0200 | [diff] [blame] | 166 | if (!device) { |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 167 | pr_emerg("roccat device with minor %d doesn't exist\n", minor); |
Stefan Achatz | 206f5f2 | 2010-05-19 18:55:16 +0200 | [diff] [blame] | 168 | error = -ENODEV; |
Julia Lawall | 8052ee5 | 2011-11-01 15:13:04 +0100 | [diff] [blame] | 169 | goto exit_err_devices; |
Stefan Achatz | 206f5f2 | 2010-05-19 18:55:16 +0200 | [diff] [blame] | 170 | } |
| 171 | |
Julia Lawall | 8052ee5 | 2011-11-01 15:13:04 +0100 | [diff] [blame] | 172 | mutex_lock(&device->readers_lock); |
| 173 | |
Stefan Achatz | 206f5f2 | 2010-05-19 18:55:16 +0200 | [diff] [blame] | 174 | if (!device->open++) { |
| 175 | /* power on device on adding first reader */ |
Dmitry Torokhov | 5bea766 | 2010-12-07 23:02:48 -0800 | [diff] [blame] | 176 | error = hid_hw_power(device->hid, PM_HINT_FULLON); |
Stefan Achatz | 206f5f2 | 2010-05-19 18:55:16 +0200 | [diff] [blame] | 177 | if (error < 0) { |
Dmitry Torokhov | 5bea766 | 2010-12-07 23:02:48 -0800 | [diff] [blame] | 178 | --device->open; |
Julia Lawall | 8052ee5 | 2011-11-01 15:13:04 +0100 | [diff] [blame] | 179 | goto exit_err_readers; |
Dmitry Torokhov | 5bea766 | 2010-12-07 23:02:48 -0800 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | error = hid_hw_open(device->hid); |
| 183 | if (error < 0) { |
| 184 | hid_hw_power(device->hid, PM_HINT_NORMAL); |
Stefan Achatz | 206f5f2 | 2010-05-19 18:55:16 +0200 | [diff] [blame] | 185 | --device->open; |
Julia Lawall | 8052ee5 | 2011-11-01 15:13:04 +0100 | [diff] [blame] | 186 | goto exit_err_readers; |
Stefan Achatz | 206f5f2 | 2010-05-19 18:55:16 +0200 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | |
| 190 | reader->device = device; |
| 191 | /* new reader doesn't get old events */ |
| 192 | reader->cbuf_start = device->cbuf_end; |
| 193 | |
| 194 | list_add_tail(&reader->node, &device->readers); |
| 195 | file->private_data = reader; |
| 196 | |
Julia Lawall | 8052ee5 | 2011-11-01 15:13:04 +0100 | [diff] [blame] | 197 | exit_err_readers: |
Stefan Achatz | 206f5f2 | 2010-05-19 18:55:16 +0200 | [diff] [blame] | 198 | mutex_unlock(&device->readers_lock); |
Julia Lawall | 8052ee5 | 2011-11-01 15:13:04 +0100 | [diff] [blame] | 199 | exit_err_devices: |
Stefan Achatz | 206f5f2 | 2010-05-19 18:55:16 +0200 | [diff] [blame] | 200 | mutex_unlock(&devices_lock); |
Julia Lawall | 8052ee5 | 2011-11-01 15:13:04 +0100 | [diff] [blame] | 201 | if (error) |
| 202 | kfree(reader); |
Stefan Achatz | 206f5f2 | 2010-05-19 18:55:16 +0200 | [diff] [blame] | 203 | return error; |
Stefan Achatz | 206f5f2 | 2010-05-19 18:55:16 +0200 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | static int roccat_release(struct inode *inode, struct file *file) |
| 207 | { |
| 208 | unsigned int minor = iminor(inode); |
| 209 | struct roccat_reader *reader = file->private_data; |
| 210 | struct roccat_device *device; |
| 211 | |
| 212 | mutex_lock(&devices_lock); |
| 213 | |
| 214 | device = devices[minor]; |
| 215 | if (!device) { |
| 216 | mutex_unlock(&devices_lock); |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 217 | pr_emerg("roccat device with minor %d doesn't exist\n", minor); |
Stefan Achatz | 206f5f2 | 2010-05-19 18:55:16 +0200 | [diff] [blame] | 218 | return -ENODEV; |
| 219 | } |
| 220 | |
| 221 | mutex_lock(&device->readers_lock); |
| 222 | list_del(&reader->node); |
| 223 | mutex_unlock(&device->readers_lock); |
| 224 | kfree(reader); |
| 225 | |
| 226 | if (!--device->open) { |
| 227 | /* removing last reader */ |
| 228 | if (device->exist) { |
Dmitry Torokhov | 5bea766 | 2010-12-07 23:02:48 -0800 | [diff] [blame] | 229 | hid_hw_power(device->hid, PM_HINT_NORMAL); |
| 230 | hid_hw_close(device->hid); |
Stefan Achatz | 206f5f2 | 2010-05-19 18:55:16 +0200 | [diff] [blame] | 231 | } else { |
| 232 | kfree(device); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | mutex_unlock(&devices_lock); |
| 237 | |
| 238 | return 0; |
| 239 | } |
| 240 | |
| 241 | /* |
| 242 | * roccat_report_event() - output data to readers |
| 243 | * @minor: minor device number returned by roccat_connect() |
| 244 | * @data: pointer to data |
Stefan Achatz | 206f5f2 | 2010-05-19 18:55:16 +0200 | [diff] [blame] | 245 | * |
| 246 | * Return value is zero on success, a negative error code on failure. |
| 247 | * |
| 248 | * This is called from interrupt handler. |
| 249 | */ |
Stefan Achatz | 8211e46 | 2011-01-30 13:38:25 +0100 | [diff] [blame] | 250 | int roccat_report_event(int minor, u8 const *data) |
Stefan Achatz | 206f5f2 | 2010-05-19 18:55:16 +0200 | [diff] [blame] | 251 | { |
| 252 | struct roccat_device *device; |
| 253 | struct roccat_reader *reader; |
| 254 | struct roccat_report *report; |
| 255 | uint8_t *new_value; |
| 256 | |
Stefan Achatz | 8211e46 | 2011-01-30 13:38:25 +0100 | [diff] [blame] | 257 | device = devices[minor]; |
| 258 | |
| 259 | new_value = kmemdup(data, device->report_size, GFP_ATOMIC); |
Stefan Achatz | 206f5f2 | 2010-05-19 18:55:16 +0200 | [diff] [blame] | 260 | if (!new_value) |
| 261 | return -ENOMEM; |
| 262 | |
Stefan Achatz | 206f5f2 | 2010-05-19 18:55:16 +0200 | [diff] [blame] | 263 | report = &device->cbuf[device->cbuf_end]; |
| 264 | |
| 265 | /* passing NULL is safe */ |
| 266 | kfree(report->value); |
| 267 | |
| 268 | report->value = new_value; |
Stefan Achatz | 206f5f2 | 2010-05-19 18:55:16 +0200 | [diff] [blame] | 269 | device->cbuf_end = (device->cbuf_end + 1) % ROCCAT_CBUF_SIZE; |
| 270 | |
| 271 | list_for_each_entry(reader, &device->readers, node) { |
| 272 | /* |
| 273 | * As we already inserted one element, the buffer can't be |
| 274 | * empty. If start and end are equal, buffer is full and we |
| 275 | * increase start, so that slow reader misses one event, but |
| 276 | * gets the newer ones in the right order. |
| 277 | */ |
| 278 | if (reader->cbuf_start == device->cbuf_end) |
| 279 | reader->cbuf_start = (reader->cbuf_start + 1) % ROCCAT_CBUF_SIZE; |
| 280 | } |
| 281 | |
| 282 | wake_up_interruptible(&device->wait); |
| 283 | return 0; |
| 284 | } |
| 285 | EXPORT_SYMBOL_GPL(roccat_report_event); |
| 286 | |
| 287 | /* |
| 288 | * roccat_connect() - create a char device for special event output |
Stefan Achatz | 5012aad | 2010-11-26 19:57:33 +0000 | [diff] [blame] | 289 | * @class: the class thats used to create the device. Meant to hold device |
| 290 | * specific sysfs attributes. |
Stefan Achatz | 206f5f2 | 2010-05-19 18:55:16 +0200 | [diff] [blame] | 291 | * @hid: the hid device the char device should be connected to. |
Stefan Achatz | 0206004 | 2013-03-10 12:33:04 +0100 | [diff] [blame] | 292 | * @report_size: size of reports |
Stefan Achatz | 206f5f2 | 2010-05-19 18:55:16 +0200 | [diff] [blame] | 293 | * |
| 294 | * Return value is minor device number in Range [0, ROCCAT_MAX_DEVICES] on |
| 295 | * success, a negative error code on failure. |
| 296 | */ |
Stefan Achatz | 8211e46 | 2011-01-30 13:38:25 +0100 | [diff] [blame] | 297 | int roccat_connect(struct class *klass, struct hid_device *hid, int report_size) |
Stefan Achatz | 206f5f2 | 2010-05-19 18:55:16 +0200 | [diff] [blame] | 298 | { |
| 299 | unsigned int minor; |
| 300 | struct roccat_device *device; |
| 301 | int temp; |
| 302 | |
| 303 | device = kzalloc(sizeof(struct roccat_device), GFP_KERNEL); |
| 304 | if (!device) |
| 305 | return -ENOMEM; |
| 306 | |
| 307 | mutex_lock(&devices_lock); |
| 308 | |
| 309 | for (minor = 0; minor < ROCCAT_MAX_DEVICES; ++minor) { |
| 310 | if (devices[minor]) |
| 311 | continue; |
| 312 | break; |
| 313 | } |
| 314 | |
| 315 | if (minor < ROCCAT_MAX_DEVICES) { |
| 316 | devices[minor] = device; |
| 317 | } else { |
| 318 | mutex_unlock(&devices_lock); |
| 319 | kfree(device); |
| 320 | return -EINVAL; |
| 321 | } |
| 322 | |
Stefan Achatz | 5012aad | 2010-11-26 19:57:33 +0000 | [diff] [blame] | 323 | device->dev = device_create(klass, &hid->dev, |
Stefan Achatz | 206f5f2 | 2010-05-19 18:55:16 +0200 | [diff] [blame] | 324 | MKDEV(roccat_major, minor), NULL, |
| 325 | "%s%s%d", "roccat", hid->driver->name, minor); |
| 326 | |
| 327 | if (IS_ERR(device->dev)) { |
| 328 | devices[minor] = NULL; |
| 329 | mutex_unlock(&devices_lock); |
| 330 | temp = PTR_ERR(device->dev); |
| 331 | kfree(device); |
| 332 | return temp; |
| 333 | } |
| 334 | |
| 335 | mutex_unlock(&devices_lock); |
| 336 | |
| 337 | init_waitqueue_head(&device->wait); |
| 338 | INIT_LIST_HEAD(&device->readers); |
| 339 | mutex_init(&device->readers_lock); |
| 340 | mutex_init(&device->cbuf_lock); |
| 341 | device->minor = minor; |
| 342 | device->hid = hid; |
| 343 | device->exist = 1; |
| 344 | device->cbuf_end = 0; |
Stefan Achatz | 8211e46 | 2011-01-30 13:38:25 +0100 | [diff] [blame] | 345 | device->report_size = report_size; |
Stefan Achatz | 206f5f2 | 2010-05-19 18:55:16 +0200 | [diff] [blame] | 346 | |
| 347 | return minor; |
| 348 | } |
| 349 | EXPORT_SYMBOL_GPL(roccat_connect); |
| 350 | |
| 351 | /* roccat_disconnect() - remove char device from hid device |
| 352 | * @minor: the minor device number returned by roccat_connect() |
| 353 | */ |
| 354 | void roccat_disconnect(int minor) |
| 355 | { |
| 356 | struct roccat_device *device; |
| 357 | |
| 358 | mutex_lock(&devices_lock); |
| 359 | device = devices[minor]; |
Stefan Achatz | 206f5f2 | 2010-05-19 18:55:16 +0200 | [diff] [blame] | 360 | mutex_unlock(&devices_lock); |
| 361 | |
| 362 | device->exist = 0; /* TODO exist maybe not needed */ |
| 363 | |
Stefan Achatz | 5012aad | 2010-11-26 19:57:33 +0000 | [diff] [blame] | 364 | device_destroy(device->dev->class, MKDEV(roccat_major, minor)); |
Stefan Achatz | 206f5f2 | 2010-05-19 18:55:16 +0200 | [diff] [blame] | 365 | |
Stefan Achatz | e6fa47a | 2011-01-30 13:38:30 +0100 | [diff] [blame] | 366 | mutex_lock(&devices_lock); |
| 367 | devices[minor] = NULL; |
| 368 | mutex_unlock(&devices_lock); |
Michael Rissi | a0be10c | 2013-06-14 17:16:37 +0200 | [diff] [blame] | 369 | |
Stefan Achatz | 206f5f2 | 2010-05-19 18:55:16 +0200 | [diff] [blame] | 370 | if (device->open) { |
Dmitry Torokhov | 5bea766 | 2010-12-07 23:02:48 -0800 | [diff] [blame] | 371 | hid_hw_close(device->hid); |
Stefan Achatz | 206f5f2 | 2010-05-19 18:55:16 +0200 | [diff] [blame] | 372 | wake_up_interruptible(&device->wait); |
| 373 | } else { |
| 374 | kfree(device); |
| 375 | } |
| 376 | } |
| 377 | EXPORT_SYMBOL_GPL(roccat_disconnect); |
| 378 | |
Stefan Achatz | 8211e46 | 2011-01-30 13:38:25 +0100 | [diff] [blame] | 379 | static long roccat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 380 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 381 | struct inode *inode = file_inode(file); |
Stefan Achatz | 8211e46 | 2011-01-30 13:38:25 +0100 | [diff] [blame] | 382 | struct roccat_device *device; |
| 383 | unsigned int minor = iminor(inode); |
| 384 | long retval = 0; |
| 385 | |
| 386 | mutex_lock(&devices_lock); |
| 387 | |
| 388 | device = devices[minor]; |
| 389 | if (!device) { |
| 390 | retval = -ENODEV; |
| 391 | goto out; |
| 392 | } |
| 393 | |
| 394 | switch (cmd) { |
| 395 | case ROCCATIOCGREPSIZE: |
| 396 | if (put_user(device->report_size, (int __user *)arg)) |
| 397 | retval = -EFAULT; |
| 398 | break; |
| 399 | default: |
| 400 | retval = -ENOTTY; |
| 401 | } |
| 402 | out: |
| 403 | mutex_unlock(&devices_lock); |
| 404 | return retval; |
| 405 | } |
| 406 | |
Stefan Achatz | 206f5f2 | 2010-05-19 18:55:16 +0200 | [diff] [blame] | 407 | static const struct file_operations roccat_ops = { |
| 408 | .owner = THIS_MODULE, |
| 409 | .read = roccat_read, |
| 410 | .poll = roccat_poll, |
| 411 | .open = roccat_open, |
| 412 | .release = roccat_release, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 413 | .llseek = noop_llseek, |
Stefan Achatz | 8211e46 | 2011-01-30 13:38:25 +0100 | [diff] [blame] | 414 | .unlocked_ioctl = roccat_ioctl, |
Stefan Achatz | 206f5f2 | 2010-05-19 18:55:16 +0200 | [diff] [blame] | 415 | }; |
| 416 | |
| 417 | static int __init roccat_init(void) |
| 418 | { |
| 419 | int retval; |
| 420 | dev_t dev_id; |
| 421 | |
| 422 | retval = alloc_chrdev_region(&dev_id, ROCCAT_FIRST_MINOR, |
| 423 | ROCCAT_MAX_DEVICES, "roccat"); |
| 424 | |
| 425 | roccat_major = MAJOR(dev_id); |
| 426 | |
| 427 | if (retval < 0) { |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 428 | pr_warn("can't get major number\n"); |
Michael Rissi | a0be10c | 2013-06-14 17:16:37 +0200 | [diff] [blame] | 429 | goto error; |
Stefan Achatz | 206f5f2 | 2010-05-19 18:55:16 +0200 | [diff] [blame] | 430 | } |
| 431 | |
Stefan Achatz | 206f5f2 | 2010-05-19 18:55:16 +0200 | [diff] [blame] | 432 | cdev_init(&roccat_cdev, &roccat_ops); |
Michael Rissi | a0be10c | 2013-06-14 17:16:37 +0200 | [diff] [blame] | 433 | retval = cdev_add(&roccat_cdev, dev_id, ROCCAT_MAX_DEVICES); |
Stefan Achatz | 206f5f2 | 2010-05-19 18:55:16 +0200 | [diff] [blame] | 434 | |
Michael Rissi | a0be10c | 2013-06-14 17:16:37 +0200 | [diff] [blame] | 435 | if (retval < 0) { |
| 436 | pr_warn("cannot add cdev\n"); |
| 437 | goto cleanup_alloc_chrdev_region; |
| 438 | } |
Stefan Achatz | 206f5f2 | 2010-05-19 18:55:16 +0200 | [diff] [blame] | 439 | return 0; |
Michael Rissi | a0be10c | 2013-06-14 17:16:37 +0200 | [diff] [blame] | 440 | |
| 441 | |
| 442 | cleanup_alloc_chrdev_region: |
| 443 | unregister_chrdev_region(dev_id, ROCCAT_MAX_DEVICES); |
| 444 | error: |
| 445 | return retval; |
Stefan Achatz | 206f5f2 | 2010-05-19 18:55:16 +0200 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | static void __exit roccat_exit(void) |
| 449 | { |
| 450 | dev_t dev_id = MKDEV(roccat_major, 0); |
| 451 | |
| 452 | cdev_del(&roccat_cdev); |
Stefan Achatz | 206f5f2 | 2010-05-19 18:55:16 +0200 | [diff] [blame] | 453 | unregister_chrdev_region(dev_id, ROCCAT_MAX_DEVICES); |
| 454 | } |
| 455 | |
| 456 | module_init(roccat_init); |
| 457 | module_exit(roccat_exit); |
| 458 | |
| 459 | MODULE_AUTHOR("Stefan Achatz"); |
| 460 | MODULE_DESCRIPTION("USB Roccat char device"); |
| 461 | MODULE_LICENSE("GPL v2"); |