Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * character device frontend for tape device driver |
| 3 | * |
| 4 | * S390 and zSeries version |
Heiko Carstens | a53c8fa | 2012-07-20 11:15:04 +0200 | [diff] [blame] | 5 | * Copyright IBM Corp. 2001, 2006 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * Author(s): Carsten Otte <cotte@de.ibm.com> |
| 7 | * Michael Holzheu <holzheu@de.ibm.com> |
| 8 | * Tuan Ngo-Anh <ngoanh@de.ibm.com> |
| 9 | * Martin Schwidefsky <schwidefsky@de.ibm.com> |
| 10 | */ |
| 11 | |
Michael Holzheu | bb50991 | 2009-12-18 17:43:21 +0100 | [diff] [blame] | 12 | #define KMSG_COMPONENT "tape" |
| 13 | #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt |
| 14 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/module.h> |
| 16 | #include <linux/types.h> |
| 17 | #include <linux/proc_fs.h> |
| 18 | #include <linux/mtio.h> |
Heiko Carstens | c540607 | 2010-01-13 20:44:44 +0100 | [diff] [blame] | 19 | #include <linux/compat.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 21 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
| 23 | #define TAPE_DBF_AREA tape_core_dbf |
| 24 | |
| 25 | #include "tape.h" |
| 26 | #include "tape_std.h" |
| 27 | #include "tape_class.h" |
| 28 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #define TAPECHAR_MAJOR 0 /* get dynamic major */ |
| 30 | |
| 31 | /* |
| 32 | * file operation structure for tape character frontend |
| 33 | */ |
| 34 | static ssize_t tapechar_read(struct file *, char __user *, size_t, loff_t *); |
| 35 | static ssize_t tapechar_write(struct file *, const char __user *, size_t, loff_t *); |
| 36 | static int tapechar_open(struct inode *,struct file *); |
| 37 | static int tapechar_release(struct inode *,struct file *); |
Martin Schwidefsky | 369a463 | 2009-12-07 12:52:04 +0100 | [diff] [blame] | 38 | static long tapechar_ioctl(struct file *, unsigned int, unsigned long); |
Heiko Carstens | c540607 | 2010-01-13 20:44:44 +0100 | [diff] [blame] | 39 | #ifdef CONFIG_COMPAT |
| 40 | static long tapechar_compat_ioctl(struct file *, unsigned int, unsigned long); |
| 41 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
Arjan van de Ven | d54b1fd | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 43 | static const struct file_operations tape_fops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | { |
| 45 | .owner = THIS_MODULE, |
| 46 | .read = tapechar_read, |
| 47 | .write = tapechar_write, |
Martin Schwidefsky | 369a463 | 2009-12-07 12:52:04 +0100 | [diff] [blame] | 48 | .unlocked_ioctl = tapechar_ioctl, |
Heiko Carstens | c540607 | 2010-01-13 20:44:44 +0100 | [diff] [blame] | 49 | #ifdef CONFIG_COMPAT |
Christoph Hellwig | f042e0f | 2006-01-09 20:52:07 -0800 | [diff] [blame] | 50 | .compat_ioctl = tapechar_compat_ioctl, |
Heiko Carstens | c540607 | 2010-01-13 20:44:44 +0100 | [diff] [blame] | 51 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | .open = tapechar_open, |
| 53 | .release = tapechar_release, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 54 | .llseek = no_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | static int tapechar_major = TAPECHAR_MAJOR; |
| 58 | |
| 59 | /* |
| 60 | * This function is called for every new tapedevice |
| 61 | */ |
| 62 | int |
| 63 | tapechar_setup_device(struct tape_device * device) |
| 64 | { |
| 65 | char device_name[20]; |
| 66 | |
| 67 | sprintf(device_name, "ntibm%i", device->first_minor / 2); |
| 68 | device->nt = register_tape_dev( |
| 69 | &device->cdev->dev, |
| 70 | MKDEV(tapechar_major, device->first_minor), |
| 71 | &tape_fops, |
| 72 | device_name, |
| 73 | "non-rewinding" |
| 74 | ); |
| 75 | device_name[0] = 'r'; |
| 76 | device->rt = register_tape_dev( |
| 77 | &device->cdev->dev, |
| 78 | MKDEV(tapechar_major, device->first_minor + 1), |
| 79 | &tape_fops, |
| 80 | device_name, |
| 81 | "rewinding" |
| 82 | ); |
| 83 | |
| 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | void |
| 88 | tapechar_cleanup_device(struct tape_device *device) |
| 89 | { |
Michael Holzheu | 92bf435 | 2008-04-17 07:46:05 +0200 | [diff] [blame] | 90 | unregister_tape_dev(&device->cdev->dev, device->rt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | device->rt = NULL; |
Michael Holzheu | 92bf435 | 2008-04-17 07:46:05 +0200 | [diff] [blame] | 92 | unregister_tape_dev(&device->cdev->dev, device->nt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | device->nt = NULL; |
| 94 | } |
| 95 | |
Heiko Carstens | 4d284ca | 2007-02-05 21:18:53 +0100 | [diff] [blame] | 96 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | tapechar_check_idalbuffer(struct tape_device *device, size_t block_size) |
| 98 | { |
| 99 | struct idal_buffer *new; |
| 100 | |
| 101 | if (device->char_data.idal_buf != NULL && |
| 102 | device->char_data.idal_buf->size == block_size) |
| 103 | return 0; |
| 104 | |
| 105 | if (block_size > MAX_BLOCKSIZE) { |
| 106 | DBF_EVENT(3, "Invalid blocksize (%zd > %d)\n", |
| 107 | block_size, MAX_BLOCKSIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | return -EINVAL; |
| 109 | } |
| 110 | |
| 111 | /* The current idal buffer is not correct. Allocate a new one. */ |
| 112 | new = idal_buffer_alloc(block_size, 0); |
Julien Brunel | 0983e56 | 2008-08-21 19:46:30 +0200 | [diff] [blame] | 113 | if (IS_ERR(new)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | return -ENOMEM; |
| 115 | |
| 116 | if (device->char_data.idal_buf != NULL) |
| 117 | idal_buffer_free(device->char_data.idal_buf); |
| 118 | |
| 119 | device->char_data.idal_buf = new; |
| 120 | |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | /* |
| 125 | * Tape device read function |
| 126 | */ |
Heiko Carstens | 2b67fc4 | 2007-02-05 21:16:47 +0100 | [diff] [blame] | 127 | static ssize_t |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | tapechar_read(struct file *filp, char __user *data, size_t count, loff_t *ppos) |
| 129 | { |
| 130 | struct tape_device *device; |
| 131 | struct tape_request *request; |
| 132 | size_t block_size; |
| 133 | int rc; |
| 134 | |
| 135 | DBF_EVENT(6, "TCHAR:read\n"); |
| 136 | device = (struct tape_device *) filp->private_data; |
| 137 | |
| 138 | /* |
| 139 | * If the tape isn't terminated yet, do it now. And since we then |
| 140 | * are at the end of the tape there wouldn't be anything to read |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 141 | * anyways. So we return immediately. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | */ |
| 143 | if(device->required_tapemarks) { |
| 144 | return tape_std_terminate_write(device); |
| 145 | } |
| 146 | |
| 147 | /* Find out block size to use */ |
| 148 | if (device->char_data.block_size != 0) { |
| 149 | if (count < device->char_data.block_size) { |
| 150 | DBF_EVENT(3, "TCHAR:read smaller than block " |
| 151 | "size was requested\n"); |
| 152 | return -EINVAL; |
| 153 | } |
| 154 | block_size = device->char_data.block_size; |
| 155 | } else { |
| 156 | block_size = count; |
| 157 | } |
| 158 | |
| 159 | rc = tapechar_check_idalbuffer(device, block_size); |
| 160 | if (rc) |
| 161 | return rc; |
| 162 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | DBF_EVENT(6, "TCHAR:nbytes: %lx\n", block_size); |
| 164 | /* Let the discipline build the ccw chain. */ |
| 165 | request = device->discipline->read_block(device, block_size); |
| 166 | if (IS_ERR(request)) |
| 167 | return PTR_ERR(request); |
| 168 | /* Execute it. */ |
| 169 | rc = tape_do_io(device, request); |
| 170 | if (rc == 0) { |
| 171 | rc = block_size - request->rescnt; |
| 172 | DBF_EVENT(6, "TCHAR:rbytes: %x\n", rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | /* Copy data from idal buffer to user space. */ |
| 174 | if (idal_buffer_to_user(device->char_data.idal_buf, |
| 175 | data, rc) != 0) |
| 176 | rc = -EFAULT; |
| 177 | } |
| 178 | tape_free_request(request); |
| 179 | return rc; |
| 180 | } |
| 181 | |
| 182 | /* |
| 183 | * Tape device write function |
| 184 | */ |
Heiko Carstens | 2b67fc4 | 2007-02-05 21:16:47 +0100 | [diff] [blame] | 185 | static ssize_t |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | tapechar_write(struct file *filp, const char __user *data, size_t count, loff_t *ppos) |
| 187 | { |
| 188 | struct tape_device *device; |
| 189 | struct tape_request *request; |
| 190 | size_t block_size; |
| 191 | size_t written; |
| 192 | int nblocks; |
| 193 | int i, rc; |
| 194 | |
| 195 | DBF_EVENT(6, "TCHAR:write\n"); |
| 196 | device = (struct tape_device *) filp->private_data; |
| 197 | /* Find out block size and number of blocks */ |
| 198 | if (device->char_data.block_size != 0) { |
| 199 | if (count < device->char_data.block_size) { |
| 200 | DBF_EVENT(3, "TCHAR:write smaller than block " |
| 201 | "size was requested\n"); |
| 202 | return -EINVAL; |
| 203 | } |
| 204 | block_size = device->char_data.block_size; |
| 205 | nblocks = count / block_size; |
| 206 | } else { |
| 207 | block_size = count; |
| 208 | nblocks = 1; |
| 209 | } |
| 210 | |
| 211 | rc = tapechar_check_idalbuffer(device, block_size); |
| 212 | if (rc) |
| 213 | return rc; |
| 214 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | DBF_EVENT(6,"TCHAR:nbytes: %lx\n", block_size); |
| 216 | DBF_EVENT(6, "TCHAR:nblocks: %x\n", nblocks); |
| 217 | /* Let the discipline build the ccw chain. */ |
| 218 | request = device->discipline->write_block(device, block_size); |
| 219 | if (IS_ERR(request)) |
| 220 | return PTR_ERR(request); |
| 221 | rc = 0; |
| 222 | written = 0; |
| 223 | for (i = 0; i < nblocks; i++) { |
| 224 | /* Copy data from user space to idal buffer. */ |
| 225 | if (idal_buffer_from_user(device->char_data.idal_buf, |
| 226 | data, block_size)) { |
| 227 | rc = -EFAULT; |
| 228 | break; |
| 229 | } |
| 230 | rc = tape_do_io(device, request); |
| 231 | if (rc) |
| 232 | break; |
| 233 | DBF_EVENT(6, "TCHAR:wbytes: %lx\n", |
| 234 | block_size - request->rescnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | written += block_size - request->rescnt; |
| 236 | if (request->rescnt != 0) |
| 237 | break; |
| 238 | data += block_size; |
| 239 | } |
| 240 | tape_free_request(request); |
| 241 | if (rc == -ENOSPC) { |
| 242 | /* |
| 243 | * Ok, the device has no more space. It has NOT written |
| 244 | * the block. |
| 245 | */ |
| 246 | if (device->discipline->process_eov) |
| 247 | device->discipline->process_eov(device); |
| 248 | if (written > 0) |
| 249 | rc = 0; |
| 250 | |
| 251 | } |
| 252 | |
| 253 | /* |
| 254 | * After doing a write we always need two tapemarks to correctly |
| 255 | * terminate the tape (one to terminate the file, the second to |
| 256 | * flag the end of recorded data. |
| 257 | * Since process_eov positions the tape in front of the written |
| 258 | * tapemark it doesn't hurt to write two marks again. |
| 259 | */ |
| 260 | if (!rc) |
| 261 | device->required_tapemarks = 2; |
| 262 | |
| 263 | return rc ? rc : written; |
| 264 | } |
| 265 | |
| 266 | /* |
| 267 | * Character frontend tape device open function. |
| 268 | */ |
Heiko Carstens | 2b67fc4 | 2007-02-05 21:16:47 +0100 | [diff] [blame] | 269 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | tapechar_open (struct inode *inode, struct file *filp) |
| 271 | { |
| 272 | struct tape_device *device; |
| 273 | int minor, rc; |
| 274 | |
| 275 | DBF_EVENT(6, "TCHAR:open: %i:%i\n", |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 276 | imajor(file_inode(filp)), |
| 277 | iminor(file_inode(filp))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 279 | if (imajor(file_inode(filp)) != tapechar_major) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | return -ENODEV; |
| 281 | |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 282 | minor = iminor(file_inode(filp)); |
Martin Schwidefsky | 8fd138c | 2009-12-07 12:52:03 +0100 | [diff] [blame] | 283 | device = tape_find_device(minor / TAPE_MINORS_PER_DEV); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | if (IS_ERR(device)) { |
Martin Schwidefsky | 8fd138c | 2009-12-07 12:52:03 +0100 | [diff] [blame] | 285 | DBF_EVENT(3, "TCHAR:open: tape_find_device() failed\n"); |
Martin Schwidefsky | 369a463 | 2009-12-07 12:52:04 +0100 | [diff] [blame] | 286 | return PTR_ERR(device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | rc = tape_open(device); |
| 290 | if (rc == 0) { |
| 291 | filp->private_data = device; |
Martin Schwidefsky | 369a463 | 2009-12-07 12:52:04 +0100 | [diff] [blame] | 292 | nonseekable_open(inode, filp); |
| 293 | } else |
Jonathan Corbet | 764a4a8 | 2008-05-15 10:01:17 -0600 | [diff] [blame] | 294 | tape_put_device(device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | |
| 296 | return rc; |
| 297 | } |
| 298 | |
| 299 | /* |
| 300 | * Character frontend tape device release function. |
| 301 | */ |
| 302 | |
Heiko Carstens | 2b67fc4 | 2007-02-05 21:16:47 +0100 | [diff] [blame] | 303 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | tapechar_release(struct inode *inode, struct file *filp) |
| 305 | { |
| 306 | struct tape_device *device; |
| 307 | |
| 308 | DBF_EVENT(6, "TCHAR:release: %x\n", iminor(inode)); |
| 309 | device = (struct tape_device *) filp->private_data; |
| 310 | |
| 311 | /* |
| 312 | * If this is the rewinding tape minor then rewind. In that case we |
| 313 | * write all required tapemarks. Otherwise only one to terminate the |
| 314 | * file. |
| 315 | */ |
| 316 | if ((iminor(inode) & 1) != 0) { |
| 317 | if (device->required_tapemarks) |
| 318 | tape_std_terminate_write(device); |
| 319 | tape_mtop(device, MTREW, 1); |
| 320 | } else { |
| 321 | if (device->required_tapemarks > 1) { |
| 322 | if (tape_mtop(device, MTWEOF, 1) == 0) |
| 323 | device->required_tapemarks--; |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | if (device->char_data.idal_buf != NULL) { |
| 328 | idal_buffer_free(device->char_data.idal_buf); |
| 329 | device->char_data.idal_buf = NULL; |
| 330 | } |
| 331 | tape_release(device); |
Martin Schwidefsky | 8fd138c | 2009-12-07 12:52:03 +0100 | [diff] [blame] | 332 | filp->private_data = NULL; |
| 333 | tape_put_device(device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | |
| 335 | return 0; |
| 336 | } |
| 337 | |
| 338 | /* |
| 339 | * Tape device io controls. |
| 340 | */ |
| 341 | static int |
Martin Schwidefsky | 369a463 | 2009-12-07 12:52:04 +0100 | [diff] [blame] | 342 | __tapechar_ioctl(struct tape_device *device, |
| 343 | unsigned int no, unsigned long data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | int rc; |
| 346 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | if (no == MTIOCTOP) { |
| 348 | struct mtop op; |
| 349 | |
| 350 | if (copy_from_user(&op, (char __user *) data, sizeof(op)) != 0) |
| 351 | return -EFAULT; |
| 352 | if (op.mt_count < 0) |
| 353 | return -EINVAL; |
| 354 | |
| 355 | /* |
| 356 | * Operations that change tape position should write final |
| 357 | * tapemarks. |
| 358 | */ |
| 359 | switch (op.mt_op) { |
| 360 | case MTFSF: |
| 361 | case MTBSF: |
| 362 | case MTFSR: |
| 363 | case MTBSR: |
| 364 | case MTREW: |
| 365 | case MTOFFL: |
| 366 | case MTEOM: |
| 367 | case MTRETEN: |
| 368 | case MTBSFM: |
| 369 | case MTFSFM: |
| 370 | case MTSEEK: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | if (device->required_tapemarks) |
| 372 | tape_std_terminate_write(device); |
| 373 | default: |
| 374 | ; |
| 375 | } |
| 376 | rc = tape_mtop(device, op.mt_op, op.mt_count); |
| 377 | |
| 378 | if (op.mt_op == MTWEOF && rc == 0) { |
| 379 | if (op.mt_count > device->required_tapemarks) |
| 380 | device->required_tapemarks = 0; |
| 381 | else |
| 382 | device->required_tapemarks -= op.mt_count; |
| 383 | } |
| 384 | return rc; |
| 385 | } |
| 386 | if (no == MTIOCPOS) { |
| 387 | /* MTIOCPOS: query the tape position. */ |
| 388 | struct mtpos pos; |
| 389 | |
| 390 | rc = tape_mtop(device, MTTELL, 1); |
| 391 | if (rc < 0) |
| 392 | return rc; |
| 393 | pos.mt_blkno = rc; |
| 394 | if (copy_to_user((char __user *) data, &pos, sizeof(pos)) != 0) |
| 395 | return -EFAULT; |
| 396 | return 0; |
| 397 | } |
| 398 | if (no == MTIOCGET) { |
| 399 | /* MTIOCGET: query the tape drive status. */ |
| 400 | struct mtget get; |
| 401 | |
| 402 | memset(&get, 0, sizeof(get)); |
| 403 | get.mt_type = MT_ISUNKNOWN; |
| 404 | get.mt_resid = 0 /* device->devstat.rescnt */; |
Stefan Haberland | 9fc98ad | 2014-09-16 11:02:24 +0200 | [diff] [blame] | 405 | get.mt_dsreg = |
| 406 | ((device->char_data.block_size << MT_ST_BLKSIZE_SHIFT) |
| 407 | & MT_ST_BLKSIZE_MASK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | /* FIXME: mt_gstat, mt_erreg, mt_fileno */ |
| 409 | get.mt_gstat = 0; |
| 410 | get.mt_erreg = 0; |
| 411 | get.mt_fileno = 0; |
| 412 | get.mt_gstat = device->tape_generic_status; |
| 413 | |
| 414 | if (device->medium_state == MS_LOADED) { |
| 415 | rc = tape_mtop(device, MTTELL, 1); |
| 416 | |
| 417 | if (rc < 0) |
| 418 | return rc; |
| 419 | |
| 420 | if (rc == 0) |
| 421 | get.mt_gstat |= GMT_BOT(~0); |
| 422 | |
| 423 | get.mt_blkno = rc; |
| 424 | } |
| 425 | |
| 426 | if (copy_to_user((char __user *) data, &get, sizeof(get)) != 0) |
| 427 | return -EFAULT; |
| 428 | |
| 429 | return 0; |
| 430 | } |
| 431 | /* Try the discipline ioctl function. */ |
| 432 | if (device->discipline->ioctl_fn == NULL) |
| 433 | return -EINVAL; |
| 434 | return device->discipline->ioctl_fn(device, no, data); |
| 435 | } |
| 436 | |
Christoph Hellwig | f042e0f | 2006-01-09 20:52:07 -0800 | [diff] [blame] | 437 | static long |
Martin Schwidefsky | 369a463 | 2009-12-07 12:52:04 +0100 | [diff] [blame] | 438 | tapechar_ioctl(struct file *filp, unsigned int no, unsigned long data) |
| 439 | { |
| 440 | struct tape_device *device; |
| 441 | long rc; |
| 442 | |
| 443 | DBF_EVENT(6, "TCHAR:ioct\n"); |
| 444 | |
| 445 | device = (struct tape_device *) filp->private_data; |
| 446 | mutex_lock(&device->mutex); |
| 447 | rc = __tapechar_ioctl(device, no, data); |
| 448 | mutex_unlock(&device->mutex); |
| 449 | return rc; |
| 450 | } |
| 451 | |
Heiko Carstens | c540607 | 2010-01-13 20:44:44 +0100 | [diff] [blame] | 452 | #ifdef CONFIG_COMPAT |
Martin Schwidefsky | 369a463 | 2009-12-07 12:52:04 +0100 | [diff] [blame] | 453 | static long |
Christoph Hellwig | f042e0f | 2006-01-09 20:52:07 -0800 | [diff] [blame] | 454 | tapechar_compat_ioctl(struct file *filp, unsigned int no, unsigned long data) |
| 455 | { |
| 456 | struct tape_device *device = filp->private_data; |
| 457 | int rval = -ENOIOCTLCMD; |
Heiko Carstens | c540607 | 2010-01-13 20:44:44 +0100 | [diff] [blame] | 458 | unsigned long argp; |
Christoph Hellwig | f042e0f | 2006-01-09 20:52:07 -0800 | [diff] [blame] | 459 | |
Heiko Carstens | c540607 | 2010-01-13 20:44:44 +0100 | [diff] [blame] | 460 | /* The 'arg' argument of any ioctl function may only be used for |
| 461 | * pointers because of the compat pointer conversion. |
| 462 | * Consider this when adding new ioctls. |
| 463 | */ |
| 464 | argp = (unsigned long) compat_ptr(data); |
Christoph Hellwig | f042e0f | 2006-01-09 20:52:07 -0800 | [diff] [blame] | 465 | if (device->discipline->ioctl_fn) { |
Martin Schwidefsky | 369a463 | 2009-12-07 12:52:04 +0100 | [diff] [blame] | 466 | mutex_lock(&device->mutex); |
Heiko Carstens | c540607 | 2010-01-13 20:44:44 +0100 | [diff] [blame] | 467 | rval = device->discipline->ioctl_fn(device, no, argp); |
Martin Schwidefsky | 369a463 | 2009-12-07 12:52:04 +0100 | [diff] [blame] | 468 | mutex_unlock(&device->mutex); |
Christoph Hellwig | f042e0f | 2006-01-09 20:52:07 -0800 | [diff] [blame] | 469 | if (rval == -EINVAL) |
| 470 | rval = -ENOIOCTLCMD; |
| 471 | } |
| 472 | |
| 473 | return rval; |
| 474 | } |
Heiko Carstens | c540607 | 2010-01-13 20:44:44 +0100 | [diff] [blame] | 475 | #endif /* CONFIG_COMPAT */ |
Christoph Hellwig | f042e0f | 2006-01-09 20:52:07 -0800 | [diff] [blame] | 476 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | /* |
| 478 | * Initialize character device frontend. |
| 479 | */ |
| 480 | int |
| 481 | tapechar_init (void) |
| 482 | { |
| 483 | dev_t dev; |
| 484 | |
| 485 | if (alloc_chrdev_region(&dev, 0, 256, "tape") != 0) |
| 486 | return -1; |
| 487 | |
| 488 | tapechar_major = MAJOR(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | |
| 490 | return 0; |
| 491 | } |
| 492 | |
| 493 | /* |
| 494 | * cleanup |
| 495 | */ |
| 496 | void |
| 497 | tapechar_exit(void) |
| 498 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | unregister_chrdev_region(MKDEV(tapechar_major, 0), 256); |
| 500 | } |