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