Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/drivers/char/raw.c |
| 3 | * |
| 4 | * Front-end raw character devices. These can be bound to any block |
| 5 | * devices to provide genuine Unix raw character device semantics. |
| 6 | * |
| 7 | * We reserve minor number 0 for a control interface. ioctl()s on this |
| 8 | * device are used to bind the other minor numbers to block devices. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/init.h> |
| 12 | #include <linux/fs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/major.h> |
| 14 | #include <linux/blkdev.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/raw.h> |
| 17 | #include <linux/capability.h> |
| 18 | #include <linux/uio.h> |
| 19 | #include <linux/cdev.h> |
| 20 | #include <linux/device.h> |
Arjan van de Ven | 8ed965d | 2006-03-23 03:00:21 -0800 | [diff] [blame] | 21 | #include <linux/mutex.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 22 | #include <linux/gfp.h> |
Al Viro | c4a0472 | 2009-08-24 22:42:56 +0000 | [diff] [blame] | 23 | #include <linux/compat.h> |
Jan Kara | 0078bff | 2011-04-29 00:24:29 +0200 | [diff] [blame] | 24 | #include <linux/vmalloc.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
| 26 | #include <asm/uaccess.h> |
| 27 | |
| 28 | struct raw_device_data { |
| 29 | struct block_device *binding; |
| 30 | int inuse; |
| 31 | }; |
| 32 | |
gregkh@suse.de | ca8eca6 | 2005-03-23 09:53:09 -0800 | [diff] [blame] | 33 | static struct class *raw_class; |
Jan Kara | 0078bff | 2011-04-29 00:24:29 +0200 | [diff] [blame] | 34 | static struct raw_device_data *raw_devices; |
Arjan van de Ven | 8ed965d | 2006-03-23 03:00:21 -0800 | [diff] [blame] | 35 | static DEFINE_MUTEX(raw_mutex); |
Arjan van de Ven | 62322d2 | 2006-07-03 00:24:21 -0700 | [diff] [blame] | 36 | static const struct file_operations raw_ctl_fops; /* forward declaration */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
Jan Kara | 0078bff | 2011-04-29 00:24:29 +0200 | [diff] [blame] | 38 | static int max_raw_minors = MAX_RAW_MINORS; |
| 39 | |
| 40 | module_param(max_raw_minors, int, 0); |
| 41 | MODULE_PARM_DESC(max_raw_minors, "Maximum number of raw devices (1-65536)"); |
| 42 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | /* |
| 44 | * Open/close code for raw IO. |
| 45 | * |
| 46 | * We just rewrite the i_mapping for the /dev/raw/rawN file descriptor to |
| 47 | * point at the blockdev's address_space and set the file handle to use |
| 48 | * O_DIRECT. |
| 49 | * |
| 50 | * Set the device's soft blocksize to the minimum possible. This gives the |
| 51 | * finest possible alignment and has no adverse impact on performance. |
| 52 | */ |
| 53 | static int raw_open(struct inode *inode, struct file *filp) |
| 54 | { |
| 55 | const int minor = iminor(inode); |
| 56 | struct block_device *bdev; |
| 57 | int err; |
| 58 | |
| 59 | if (minor == 0) { /* It is the control device */ |
| 60 | filp->f_op = &raw_ctl_fops; |
| 61 | return 0; |
| 62 | } |
| 63 | |
Arjan van de Ven | 8ed965d | 2006-03-23 03:00:21 -0800 | [diff] [blame] | 64 | mutex_lock(&raw_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | |
| 66 | /* |
| 67 | * All we need to do on open is check that the device is bound. |
| 68 | */ |
| 69 | bdev = raw_devices[minor].binding; |
| 70 | err = -ENODEV; |
| 71 | if (!bdev) |
| 72 | goto out; |
| 73 | igrab(bdev->bd_inode); |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 74 | err = blkdev_get(bdev, filp->f_mode | FMODE_EXCL, raw_open); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | if (err) |
| 76 | goto out; |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 77 | err = set_blocksize(bdev, bdev_logical_block_size(bdev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | if (err) |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 79 | goto out1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | filp->f_flags |= O_DIRECT; |
| 81 | filp->f_mapping = bdev->bd_inode->i_mapping; |
| 82 | if (++raw_devices[minor].inuse == 1) |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 83 | file_inode(filp)->i_mapping = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | bdev->bd_inode->i_mapping; |
| 85 | filp->private_data = bdev; |
Arjan van de Ven | 8ed965d | 2006-03-23 03:00:21 -0800 | [diff] [blame] | 86 | mutex_unlock(&raw_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | return 0; |
| 88 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | out1: |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 90 | blkdev_put(bdev, filp->f_mode | FMODE_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | out: |
Arjan van de Ven | 8ed965d | 2006-03-23 03:00:21 -0800 | [diff] [blame] | 92 | mutex_unlock(&raw_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | return err; |
| 94 | } |
| 95 | |
| 96 | /* |
| 97 | * When the final fd which refers to this character-special node is closed, we |
| 98 | * make its ->mapping point back at its own i_data. |
| 99 | */ |
| 100 | static int raw_release(struct inode *inode, struct file *filp) |
| 101 | { |
| 102 | const int minor= iminor(inode); |
| 103 | struct block_device *bdev; |
| 104 | |
Arjan van de Ven | 8ed965d | 2006-03-23 03:00:21 -0800 | [diff] [blame] | 105 | mutex_lock(&raw_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | bdev = raw_devices[minor].binding; |
Christoph Hellwig | b83ae6d | 2015-01-14 10:42:37 +0100 | [diff] [blame] | 107 | if (--raw_devices[minor].inuse == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | /* Here inode->i_mapping == bdev->bd_inode->i_mapping */ |
| 109 | inode->i_mapping = &inode->i_data; |
Arjan van de Ven | 8ed965d | 2006-03-23 03:00:21 -0800 | [diff] [blame] | 110 | mutex_unlock(&raw_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 112 | blkdev_put(bdev, filp->f_mode | FMODE_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | /* |
| 117 | * Forward ioctls to the underlying block device. |
| 118 | */ |
Arnd Bergmann | 5592933 | 2010-04-27 00:24:05 +0200 | [diff] [blame] | 119 | static long |
| 120 | raw_ioctl(struct file *filp, unsigned int command, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | { |
| 122 | struct block_device *bdev = filp->private_data; |
Al Viro | c4a0472 | 2009-08-24 22:42:56 +0000 | [diff] [blame] | 123 | return blkdev_ioctl(bdev, 0, command, arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Al Viro | c4a0472 | 2009-08-24 22:42:56 +0000 | [diff] [blame] | 126 | static int bind_set(int number, u64 major, u64 minor) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | { |
Al Viro | c4a0472 | 2009-08-24 22:42:56 +0000 | [diff] [blame] | 128 | dev_t dev = MKDEV(major, minor); |
| 129 | struct raw_device_data *rawdev; |
| 130 | int err = 0; |
| 131 | |
Jan Kara | 0078bff | 2011-04-29 00:24:29 +0200 | [diff] [blame] | 132 | if (number <= 0 || number >= max_raw_minors) |
Al Viro | c4a0472 | 2009-08-24 22:42:56 +0000 | [diff] [blame] | 133 | return -EINVAL; |
| 134 | |
| 135 | if (MAJOR(dev) != major || MINOR(dev) != minor) |
| 136 | return -EINVAL; |
| 137 | |
| 138 | rawdev = &raw_devices[number]; |
| 139 | |
| 140 | /* |
| 141 | * This is like making block devices, so demand the |
| 142 | * same capability |
| 143 | */ |
| 144 | if (!capable(CAP_SYS_ADMIN)) |
| 145 | return -EPERM; |
| 146 | |
| 147 | /* |
| 148 | * For now, we don't need to check that the underlying |
| 149 | * block device is present or not: we can do that when |
| 150 | * the raw device is opened. Just check that the |
| 151 | * major/minor numbers make sense. |
| 152 | */ |
| 153 | |
| 154 | if (MAJOR(dev) == 0 && dev != 0) |
| 155 | return -EINVAL; |
| 156 | |
| 157 | mutex_lock(&raw_mutex); |
| 158 | if (rawdev->inuse) { |
| 159 | mutex_unlock(&raw_mutex); |
| 160 | return -EBUSY; |
| 161 | } |
| 162 | if (rawdev->binding) { |
| 163 | bdput(rawdev->binding); |
| 164 | module_put(THIS_MODULE); |
| 165 | } |
| 166 | if (!dev) { |
| 167 | /* unbind */ |
| 168 | rawdev->binding = NULL; |
| 169 | device_destroy(raw_class, MKDEV(RAW_MAJOR, number)); |
| 170 | } else { |
| 171 | rawdev->binding = bdget(dev); |
| 172 | if (rawdev->binding == NULL) { |
| 173 | err = -ENOMEM; |
| 174 | } else { |
| 175 | dev_t raw = MKDEV(RAW_MAJOR, number); |
| 176 | __module_get(THIS_MODULE); |
| 177 | device_destroy(raw_class, raw); |
| 178 | device_create(raw_class, NULL, raw, NULL, |
| 179 | "raw%d", number); |
| 180 | } |
| 181 | } |
| 182 | mutex_unlock(&raw_mutex); |
| 183 | return err; |
| 184 | } |
| 185 | |
| 186 | static int bind_get(int number, dev_t *dev) |
| 187 | { |
| 188 | struct raw_device_data *rawdev; |
| 189 | struct block_device *bdev; |
| 190 | |
Paul Bolle | 5bbb2ae | 2014-02-04 23:23:12 +0100 | [diff] [blame] | 191 | if (number <= 0 || number >= max_raw_minors) |
Al Viro | c4a0472 | 2009-08-24 22:42:56 +0000 | [diff] [blame] | 192 | return -EINVAL; |
| 193 | |
| 194 | rawdev = &raw_devices[number]; |
| 195 | |
| 196 | mutex_lock(&raw_mutex); |
| 197 | bdev = rawdev->binding; |
| 198 | *dev = bdev ? bdev->bd_dev : 0; |
| 199 | mutex_unlock(&raw_mutex); |
| 200 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | /* |
| 204 | * Deal with ioctls against the raw-device control interface, to bind |
| 205 | * and unbind other raw devices. |
| 206 | */ |
Arnd Bergmann | 5592933 | 2010-04-27 00:24:05 +0200 | [diff] [blame] | 207 | static long raw_ctl_ioctl(struct file *filp, unsigned int command, |
| 208 | unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | { |
| 210 | struct raw_config_request rq; |
Al Viro | c4a0472 | 2009-08-24 22:42:56 +0000 | [diff] [blame] | 211 | dev_t dev; |
| 212 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | |
| 214 | switch (command) { |
| 215 | case RAW_SETBIND: |
Al Viro | c4a0472 | 2009-08-24 22:42:56 +0000 | [diff] [blame] | 216 | if (copy_from_user(&rq, (void __user *) arg, sizeof(rq))) |
| 217 | return -EFAULT; |
| 218 | |
| 219 | return bind_set(rq.raw_minor, rq.block_major, rq.block_minor); |
| 220 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | case RAW_GETBIND: |
Al Viro | c4a0472 | 2009-08-24 22:42:56 +0000 | [diff] [blame] | 222 | if (copy_from_user(&rq, (void __user *) arg, sizeof(rq))) |
| 223 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | |
Al Viro | c4a0472 | 2009-08-24 22:42:56 +0000 | [diff] [blame] | 225 | err = bind_get(rq.raw_minor, &dev); |
| 226 | if (err) |
| 227 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | |
Al Viro | c4a0472 | 2009-08-24 22:42:56 +0000 | [diff] [blame] | 229 | rq.block_major = MAJOR(dev); |
| 230 | rq.block_minor = MINOR(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | |
Al Viro | c4a0472 | 2009-08-24 22:42:56 +0000 | [diff] [blame] | 232 | if (copy_to_user((void __user *)arg, &rq, sizeof(rq))) |
| 233 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | |
Al Viro | c4a0472 | 2009-08-24 22:42:56 +0000 | [diff] [blame] | 235 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | } |
Al Viro | c4a0472 | 2009-08-24 22:42:56 +0000 | [diff] [blame] | 237 | |
| 238 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Al Viro | c4a0472 | 2009-08-24 22:42:56 +0000 | [diff] [blame] | 241 | #ifdef CONFIG_COMPAT |
| 242 | struct raw32_config_request { |
| 243 | compat_int_t raw_minor; |
| 244 | compat_u64 block_major; |
| 245 | compat_u64 block_minor; |
| 246 | }; |
| 247 | |
| 248 | static long raw_ctl_compat_ioctl(struct file *file, unsigned int cmd, |
| 249 | unsigned long arg) |
| 250 | { |
| 251 | struct raw32_config_request __user *user_req = compat_ptr(arg); |
| 252 | struct raw32_config_request rq; |
| 253 | dev_t dev; |
| 254 | int err = 0; |
| 255 | |
| 256 | switch (cmd) { |
| 257 | case RAW_SETBIND: |
| 258 | if (copy_from_user(&rq, user_req, sizeof(rq))) |
| 259 | return -EFAULT; |
| 260 | |
| 261 | return bind_set(rq.raw_minor, rq.block_major, rq.block_minor); |
| 262 | |
| 263 | case RAW_GETBIND: |
| 264 | if (copy_from_user(&rq, user_req, sizeof(rq))) |
| 265 | return -EFAULT; |
| 266 | |
| 267 | err = bind_get(rq.raw_minor, &dev); |
| 268 | if (err) |
| 269 | return err; |
| 270 | |
| 271 | rq.block_major = MAJOR(dev); |
| 272 | rq.block_minor = MINOR(dev); |
| 273 | |
| 274 | if (copy_to_user(user_req, &rq, sizeof(rq))) |
| 275 | return -EFAULT; |
| 276 | |
| 277 | return 0; |
| 278 | } |
| 279 | |
| 280 | return -EINVAL; |
| 281 | } |
| 282 | #endif |
| 283 | |
Arjan van de Ven | 62322d2 | 2006-07-03 00:24:21 -0700 | [diff] [blame] | 284 | static const struct file_operations raw_fops = { |
David Jeffery | b2de525 | 2014-09-29 10:21:10 -0400 | [diff] [blame] | 285 | .read_iter = blkdev_read_iter, |
Al Viro | 1456c0a | 2014-04-03 03:21:50 -0400 | [diff] [blame] | 286 | .write_iter = blkdev_write_iter, |
Arnd Bergmann | 5592933 | 2010-04-27 00:24:05 +0200 | [diff] [blame] | 287 | .fsync = blkdev_fsync, |
| 288 | .open = raw_open, |
| 289 | .release = raw_release, |
| 290 | .unlocked_ioctl = raw_ioctl, |
Arnd Bergmann | cb3b9cf | 2010-07-06 23:03:55 +0200 | [diff] [blame] | 291 | .llseek = default_llseek, |
Arnd Bergmann | 5592933 | 2010-04-27 00:24:05 +0200 | [diff] [blame] | 292 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | }; |
| 294 | |
Arjan van de Ven | 62322d2 | 2006-07-03 00:24:21 -0700 | [diff] [blame] | 295 | static const struct file_operations raw_ctl_fops = { |
Arnd Bergmann | 5592933 | 2010-04-27 00:24:05 +0200 | [diff] [blame] | 296 | .unlocked_ioctl = raw_ctl_ioctl, |
Al Viro | c4a0472 | 2009-08-24 22:42:56 +0000 | [diff] [blame] | 297 | #ifdef CONFIG_COMPAT |
| 298 | .compat_ioctl = raw_ctl_compat_ioctl, |
| 299 | #endif |
Arnd Bergmann | 5592933 | 2010-04-27 00:24:05 +0200 | [diff] [blame] | 300 | .open = raw_open, |
| 301 | .owner = THIS_MODULE, |
Arnd Bergmann | cb3b9cf | 2010-07-06 23:03:55 +0200 | [diff] [blame] | 302 | .llseek = noop_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | }; |
| 304 | |
Greg Kroah-Hartman | 7e7654a | 2007-09-12 15:06:57 -0700 | [diff] [blame] | 305 | static struct cdev raw_cdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | |
Al Viro | 2c9ede5 | 2011-07-23 20:24:48 -0400 | [diff] [blame] | 307 | static char *raw_devnode(struct device *dev, umode_t *mode) |
Kay Sievers | 6fd4693 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 308 | { |
| 309 | return kasprintf(GFP_KERNEL, "raw/%s", dev_name(dev)); |
| 310 | } |
| 311 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | static int __init raw_init(void) |
| 313 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | dev_t dev = MKDEV(RAW_MAJOR, 0); |
Rolf Eike Beer | 3e26a42 | 2006-09-29 02:00:46 -0700 | [diff] [blame] | 315 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | |
Jan Kara | 0078bff | 2011-04-29 00:24:29 +0200 | [diff] [blame] | 317 | if (max_raw_minors < 1 || max_raw_minors > 65536) { |
| 318 | printk(KERN_WARNING "raw: invalid max_raw_minors (must be" |
| 319 | " between 1 and 65536), using %d\n", MAX_RAW_MINORS); |
| 320 | max_raw_minors = MAX_RAW_MINORS; |
| 321 | } |
| 322 | |
Joe Perches | 8e03bd6 | 2011-05-28 10:36:25 -0700 | [diff] [blame] | 323 | raw_devices = vzalloc(sizeof(struct raw_device_data) * max_raw_minors); |
Jan Kara | 0078bff | 2011-04-29 00:24:29 +0200 | [diff] [blame] | 324 | if (!raw_devices) { |
| 325 | printk(KERN_ERR "Not enough memory for raw device structures\n"); |
| 326 | ret = -ENOMEM; |
| 327 | goto error; |
| 328 | } |
Jan Kara | 0078bff | 2011-04-29 00:24:29 +0200 | [diff] [blame] | 329 | |
| 330 | ret = register_chrdev_region(dev, max_raw_minors, "raw"); |
Rolf Eike Beer | 3e26a42 | 2006-09-29 02:00:46 -0700 | [diff] [blame] | 331 | if (ret) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | goto error; |
| 333 | |
| 334 | cdev_init(&raw_cdev, &raw_fops); |
Jan Kara | 0078bff | 2011-04-29 00:24:29 +0200 | [diff] [blame] | 335 | ret = cdev_add(&raw_cdev, dev, max_raw_minors); |
Rolf Eike Beer | 3e26a42 | 2006-09-29 02:00:46 -0700 | [diff] [blame] | 336 | if (ret) { |
Rolf Eike Beer | 3e26a42 | 2006-09-29 02:00:46 -0700 | [diff] [blame] | 337 | goto error_region; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | } |
| 339 | |
gregkh@suse.de | ca8eca6 | 2005-03-23 09:53:09 -0800 | [diff] [blame] | 340 | raw_class = class_create(THIS_MODULE, "raw"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | if (IS_ERR(raw_class)) { |
| 342 | printk(KERN_ERR "Error creating raw class.\n"); |
| 343 | cdev_del(&raw_cdev); |
Rolf Eike Beer | 3e26a42 | 2006-09-29 02:00:46 -0700 | [diff] [blame] | 344 | ret = PTR_ERR(raw_class); |
| 345 | goto error_region; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | } |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 347 | raw_class->devnode = raw_devnode; |
Greg Kroah-Hartman | 03457cd | 2008-07-21 20:03:34 -0700 | [diff] [blame] | 348 | device_create(raw_class, NULL, MKDEV(RAW_MAJOR, 0), NULL, "rawctl"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | return 0; |
| 351 | |
Rolf Eike Beer | 3e26a42 | 2006-09-29 02:00:46 -0700 | [diff] [blame] | 352 | error_region: |
Jan Kara | 0078bff | 2011-04-29 00:24:29 +0200 | [diff] [blame] | 353 | unregister_chrdev_region(dev, max_raw_minors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | error: |
Jan Kara | 0078bff | 2011-04-29 00:24:29 +0200 | [diff] [blame] | 355 | vfree(raw_devices); |
Rolf Eike Beer | 3e26a42 | 2006-09-29 02:00:46 -0700 | [diff] [blame] | 356 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | static void __exit raw_exit(void) |
| 360 | { |
Greg Kroah-Hartman | 38ca6c3 | 2006-08-07 22:19:37 -0700 | [diff] [blame] | 361 | device_destroy(raw_class, MKDEV(RAW_MAJOR, 0)); |
gregkh@suse.de | ca8eca6 | 2005-03-23 09:53:09 -0800 | [diff] [blame] | 362 | class_destroy(raw_class); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | cdev_del(&raw_cdev); |
Jan Kara | 0078bff | 2011-04-29 00:24:29 +0200 | [diff] [blame] | 364 | unregister_chrdev_region(MKDEV(RAW_MAJOR, 0), max_raw_minors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | module_init(raw_init); |
| 368 | module_exit(raw_exit); |
| 369 | MODULE_LICENSE("GPL"); |