Bartlomiej Zolnierkiewicz | 0127854 | 2008-10-10 22:39:38 +0200 | [diff] [blame] | 1 | /* |
| 2 | * ide-floppy IOCTLs handling. |
| 3 | */ |
| 4 | |
| 5 | #include <linux/kernel.h> |
| 6 | #include <linux/ide.h> |
| 7 | #include <linux/cdrom.h> |
Arnd Bergmann | 8a6cfeb | 2010-07-08 10:18:46 +0200 | [diff] [blame] | 8 | #include <linux/smp_lock.h> |
Bartlomiej Zolnierkiewicz | 0127854 | 2008-10-10 22:39:38 +0200 | [diff] [blame] | 9 | |
| 10 | #include <asm/unaligned.h> |
| 11 | |
| 12 | #include <scsi/scsi_ioctl.h> |
| 13 | |
| 14 | #include "ide-floppy.h" |
| 15 | |
| 16 | /* |
| 17 | * Obtain the list of formattable capacities. |
| 18 | * Very similar to ide_floppy_get_capacity, except that we push the capacity |
| 19 | * descriptors to userland, instead of our own structures. |
| 20 | * |
| 21 | * Userland gives us the following structure: |
| 22 | * |
| 23 | * struct idefloppy_format_capacities { |
| 24 | * int nformats; |
| 25 | * struct { |
| 26 | * int nblocks; |
| 27 | * int blocksize; |
| 28 | * } formats[]; |
| 29 | * }; |
| 30 | * |
| 31 | * userland initializes nformats to the number of allocated formats[] records. |
| 32 | * On exit we set nformats to the number of records we've actually initialized. |
| 33 | */ |
| 34 | |
Linus Torvalds | 07bd3f4 | 2009-01-02 16:12:51 +0100 | [diff] [blame] | 35 | static int ide_floppy_get_format_capacities(ide_drive_t *drive, |
| 36 | struct ide_atapi_pc *pc, |
| 37 | int __user *arg) |
Bartlomiej Zolnierkiewicz | 0127854 | 2008-10-10 22:39:38 +0200 | [diff] [blame] | 38 | { |
Bartlomiej Zolnierkiewicz | 806f80a | 2008-10-17 18:09:14 +0200 | [diff] [blame] | 39 | struct ide_disk_obj *floppy = drive->driver_data; |
Bartlomiej Zolnierkiewicz | 0127854 | 2008-10-10 22:39:38 +0200 | [diff] [blame] | 40 | int i, blocks, length, u_array_size, u_index; |
| 41 | int __user *argp; |
Bartlomiej Zolnierkiewicz | 41fa9f8 | 2009-03-31 20:15:25 +0200 | [diff] [blame] | 42 | u8 pc_buf[256], header_len, desc_cnt; |
Bartlomiej Zolnierkiewicz | 0127854 | 2008-10-10 22:39:38 +0200 | [diff] [blame] | 43 | |
| 44 | if (get_user(u_array_size, arg)) |
| 45 | return -EFAULT; |
| 46 | |
| 47 | if (u_array_size <= 0) |
| 48 | return -EINVAL; |
| 49 | |
Linus Torvalds | 07bd3f4 | 2009-01-02 16:12:51 +0100 | [diff] [blame] | 50 | ide_floppy_create_read_capacity_cmd(pc); |
Bartlomiej Zolnierkiewicz | 41fa9f8 | 2009-03-31 20:15:25 +0200 | [diff] [blame] | 51 | |
Borislav Petkov | b13345f | 2009-05-02 10:26:12 +0200 | [diff] [blame] | 52 | if (ide_queue_pc_tail(drive, floppy->disk, pc, pc_buf, pc->req_xfer)) { |
Bartlomiej Zolnierkiewicz | 0127854 | 2008-10-10 22:39:38 +0200 | [diff] [blame] | 53 | printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n"); |
| 54 | return -EIO; |
| 55 | } |
| 56 | |
Borislav Petkov | b13345f | 2009-05-02 10:26:12 +0200 | [diff] [blame] | 57 | header_len = pc_buf[3]; |
Bartlomiej Zolnierkiewicz | 0127854 | 2008-10-10 22:39:38 +0200 | [diff] [blame] | 58 | desc_cnt = header_len / 8; /* capacity descriptor of 8 bytes */ |
| 59 | |
| 60 | u_index = 0; |
| 61 | argp = arg + 1; |
| 62 | |
| 63 | /* |
| 64 | * We always skip the first capacity descriptor. That's the current |
| 65 | * capacity. We are interested in the remaining descriptors, the |
| 66 | * formattable capacities. |
| 67 | */ |
| 68 | for (i = 1; i < desc_cnt; i++) { |
| 69 | unsigned int desc_start = 4 + i*8; |
| 70 | |
| 71 | if (u_index >= u_array_size) |
| 72 | break; /* User-supplied buffer too small */ |
| 73 | |
Borislav Petkov | b13345f | 2009-05-02 10:26:12 +0200 | [diff] [blame] | 74 | blocks = be32_to_cpup((__be32 *)&pc_buf[desc_start]); |
| 75 | length = be16_to_cpup((__be16 *)&pc_buf[desc_start + 6]); |
Bartlomiej Zolnierkiewicz | 0127854 | 2008-10-10 22:39:38 +0200 | [diff] [blame] | 76 | |
| 77 | if (put_user(blocks, argp)) |
| 78 | return -EFAULT; |
| 79 | |
| 80 | ++argp; |
| 81 | |
| 82 | if (put_user(length, argp)) |
| 83 | return -EFAULT; |
| 84 | |
| 85 | ++argp; |
| 86 | |
| 87 | ++u_index; |
| 88 | } |
| 89 | |
| 90 | if (put_user(u_index, arg)) |
| 91 | return -EFAULT; |
| 92 | |
| 93 | return 0; |
| 94 | } |
| 95 | |
Borislav Petkov | 5122e51 | 2009-05-02 10:53:10 +0200 | [diff] [blame] | 96 | static void ide_floppy_create_format_unit_cmd(struct ide_atapi_pc *pc, |
| 97 | u8 *buf, int b, int l, |
| 98 | int flags) |
Bartlomiej Zolnierkiewicz | 0127854 | 2008-10-10 22:39:38 +0200 | [diff] [blame] | 99 | { |
| 100 | ide_init_pc(pc); |
| 101 | pc->c[0] = GPCMD_FORMAT_UNIT; |
| 102 | pc->c[1] = 0x17; |
| 103 | |
Borislav Petkov | 5122e51 | 2009-05-02 10:53:10 +0200 | [diff] [blame] | 104 | memset(buf, 0, 12); |
| 105 | buf[1] = 0xA2; |
Bartlomiej Zolnierkiewicz | 0127854 | 2008-10-10 22:39:38 +0200 | [diff] [blame] | 106 | /* Default format list header, u8 1: FOV/DCRT/IMM bits set */ |
| 107 | |
| 108 | if (flags & 1) /* Verify bit on... */ |
Borislav Petkov | 5122e51 | 2009-05-02 10:53:10 +0200 | [diff] [blame] | 109 | buf[1] ^= 0x20; /* ... turn off DCRT bit */ |
| 110 | buf[3] = 8; |
Bartlomiej Zolnierkiewicz | 0127854 | 2008-10-10 22:39:38 +0200 | [diff] [blame] | 111 | |
Borislav Petkov | 5122e51 | 2009-05-02 10:53:10 +0200 | [diff] [blame] | 112 | put_unaligned(cpu_to_be32(b), (unsigned int *)(&buf[4])); |
| 113 | put_unaligned(cpu_to_be32(l), (unsigned int *)(&buf[8])); |
| 114 | pc->req_xfer = 12; |
Bartlomiej Zolnierkiewicz | 0127854 | 2008-10-10 22:39:38 +0200 | [diff] [blame] | 115 | pc->flags |= PC_FLAG_WRITING; |
| 116 | } |
| 117 | |
Linus Torvalds | 07bd3f4 | 2009-01-02 16:12:51 +0100 | [diff] [blame] | 118 | static int ide_floppy_get_sfrp_bit(ide_drive_t *drive, struct ide_atapi_pc *pc) |
Bartlomiej Zolnierkiewicz | 0127854 | 2008-10-10 22:39:38 +0200 | [diff] [blame] | 119 | { |
Bartlomiej Zolnierkiewicz | 0df9627 | 2008-10-17 18:09:16 +0200 | [diff] [blame] | 120 | struct ide_disk_obj *floppy = drive->driver_data; |
Borislav Petkov | 802e663 | 2009-05-02 10:45:17 +0200 | [diff] [blame] | 121 | u8 buf[20]; |
Bartlomiej Zolnierkiewicz | 0127854 | 2008-10-10 22:39:38 +0200 | [diff] [blame] | 122 | |
| 123 | drive->atapi_flags &= ~IDE_AFLAG_SRFP; |
| 124 | |
Linus Torvalds | 07bd3f4 | 2009-01-02 16:12:51 +0100 | [diff] [blame] | 125 | ide_floppy_create_mode_sense_cmd(pc, IDEFLOPPY_CAPABILITIES_PAGE); |
| 126 | pc->flags |= PC_FLAG_SUPPRESS_ERROR; |
Bartlomiej Zolnierkiewicz | 0127854 | 2008-10-10 22:39:38 +0200 | [diff] [blame] | 127 | |
Borislav Petkov | 802e663 | 2009-05-02 10:45:17 +0200 | [diff] [blame] | 128 | if (ide_queue_pc_tail(drive, floppy->disk, pc, buf, pc->req_xfer)) |
Bartlomiej Zolnierkiewicz | 0127854 | 2008-10-10 22:39:38 +0200 | [diff] [blame] | 129 | return 1; |
| 130 | |
Borislav Petkov | 802e663 | 2009-05-02 10:45:17 +0200 | [diff] [blame] | 131 | if (buf[8 + 2] & 0x40) |
Bartlomiej Zolnierkiewicz | 0127854 | 2008-10-10 22:39:38 +0200 | [diff] [blame] | 132 | drive->atapi_flags |= IDE_AFLAG_SRFP; |
| 133 | |
| 134 | return 0; |
| 135 | } |
| 136 | |
Linus Torvalds | 07bd3f4 | 2009-01-02 16:12:51 +0100 | [diff] [blame] | 137 | static int ide_floppy_format_unit(ide_drive_t *drive, struct ide_atapi_pc *pc, |
| 138 | int __user *arg) |
Bartlomiej Zolnierkiewicz | 0127854 | 2008-10-10 22:39:38 +0200 | [diff] [blame] | 139 | { |
Bartlomiej Zolnierkiewicz | 0df9627 | 2008-10-17 18:09:16 +0200 | [diff] [blame] | 140 | struct ide_disk_obj *floppy = drive->driver_data; |
Borislav Petkov | 5122e51 | 2009-05-02 10:53:10 +0200 | [diff] [blame] | 141 | u8 buf[12]; |
Bartlomiej Zolnierkiewicz | 0127854 | 2008-10-10 22:39:38 +0200 | [diff] [blame] | 142 | int blocks, length, flags, err = 0; |
| 143 | |
| 144 | if (floppy->openers > 1) { |
| 145 | /* Don't format if someone is using the disk */ |
Bartlomiej Zolnierkiewicz | e012862 | 2008-10-17 18:09:11 +0200 | [diff] [blame] | 146 | drive->dev_flags &= ~IDE_DFLAG_FORMAT_IN_PROGRESS; |
Bartlomiej Zolnierkiewicz | 0127854 | 2008-10-10 22:39:38 +0200 | [diff] [blame] | 147 | return -EBUSY; |
| 148 | } |
| 149 | |
Bartlomiej Zolnierkiewicz | e012862 | 2008-10-17 18:09:11 +0200 | [diff] [blame] | 150 | drive->dev_flags |= IDE_DFLAG_FORMAT_IN_PROGRESS; |
Bartlomiej Zolnierkiewicz | 0127854 | 2008-10-10 22:39:38 +0200 | [diff] [blame] | 151 | |
| 152 | /* |
| 153 | * Send ATAPI_FORMAT_UNIT to the drive. |
| 154 | * |
| 155 | * Userland gives us the following structure: |
| 156 | * |
| 157 | * struct idefloppy_format_command { |
| 158 | * int nblocks; |
| 159 | * int blocksize; |
| 160 | * int flags; |
| 161 | * } ; |
| 162 | * |
| 163 | * flags is a bitmask, currently, the only defined flag is: |
| 164 | * |
| 165 | * 0x01 - verify media after format. |
| 166 | */ |
| 167 | if (get_user(blocks, arg) || |
| 168 | get_user(length, arg+1) || |
| 169 | get_user(flags, arg+2)) { |
| 170 | err = -EFAULT; |
| 171 | goto out; |
| 172 | } |
| 173 | |
Linus Torvalds | 07bd3f4 | 2009-01-02 16:12:51 +0100 | [diff] [blame] | 174 | ide_floppy_get_sfrp_bit(drive, pc); |
Borislav Petkov | 5122e51 | 2009-05-02 10:53:10 +0200 | [diff] [blame] | 175 | ide_floppy_create_format_unit_cmd(pc, buf, blocks, length, flags); |
Bartlomiej Zolnierkiewicz | 0127854 | 2008-10-10 22:39:38 +0200 | [diff] [blame] | 176 | |
Borislav Petkov | 5122e51 | 2009-05-02 10:53:10 +0200 | [diff] [blame] | 177 | if (ide_queue_pc_tail(drive, floppy->disk, pc, buf, pc->req_xfer)) |
Bartlomiej Zolnierkiewicz | 0127854 | 2008-10-10 22:39:38 +0200 | [diff] [blame] | 178 | err = -EIO; |
| 179 | |
| 180 | out: |
| 181 | if (err) |
Bartlomiej Zolnierkiewicz | e012862 | 2008-10-17 18:09:11 +0200 | [diff] [blame] | 182 | drive->dev_flags &= ~IDE_DFLAG_FORMAT_IN_PROGRESS; |
Bartlomiej Zolnierkiewicz | 0127854 | 2008-10-10 22:39:38 +0200 | [diff] [blame] | 183 | return err; |
| 184 | } |
| 185 | |
| 186 | /* |
| 187 | * Get ATAPI_FORMAT_UNIT progress indication. |
| 188 | * |
| 189 | * Userland gives a pointer to an int. The int is set to a progress |
| 190 | * indicator 0-65536, with 65536=100%. |
| 191 | * |
| 192 | * If the drive does not support format progress indication, we just check |
| 193 | * the dsc bit, and return either 0 or 65536. |
| 194 | */ |
| 195 | |
Linus Torvalds | 07bd3f4 | 2009-01-02 16:12:51 +0100 | [diff] [blame] | 196 | static int ide_floppy_get_format_progress(ide_drive_t *drive, |
| 197 | struct ide_atapi_pc *pc, |
| 198 | int __user *arg) |
Bartlomiej Zolnierkiewicz | 0127854 | 2008-10-10 22:39:38 +0200 | [diff] [blame] | 199 | { |
Bartlomiej Zolnierkiewicz | 0df9627 | 2008-10-17 18:09:16 +0200 | [diff] [blame] | 200 | struct ide_disk_obj *floppy = drive->driver_data; |
Borislav Petkov | 60cfab8 | 2009-05-12 08:59:49 +0200 | [diff] [blame] | 201 | u8 sense_buf[18]; |
Bartlomiej Zolnierkiewicz | 0127854 | 2008-10-10 22:39:38 +0200 | [diff] [blame] | 202 | int progress_indication = 0x10000; |
| 203 | |
| 204 | if (drive->atapi_flags & IDE_AFLAG_SRFP) { |
Linus Torvalds | 07bd3f4 | 2009-01-02 16:12:51 +0100 | [diff] [blame] | 205 | ide_create_request_sense_cmd(drive, pc); |
Borislav Petkov | 60cfab8 | 2009-05-12 08:59:49 +0200 | [diff] [blame] | 206 | if (ide_queue_pc_tail(drive, floppy->disk, pc, sense_buf, |
Borislav Petkov | b13345f | 2009-05-02 10:26:12 +0200 | [diff] [blame] | 207 | pc->req_xfer)) |
Bartlomiej Zolnierkiewicz | 0127854 | 2008-10-10 22:39:38 +0200 | [diff] [blame] | 208 | return -EIO; |
| 209 | |
| 210 | if (floppy->sense_key == 2 && |
| 211 | floppy->asc == 4 && |
| 212 | floppy->ascq == 4) |
| 213 | progress_indication = floppy->progress_indication; |
| 214 | |
| 215 | /* Else assume format_unit has finished, and we're at 0x10000 */ |
| 216 | } else { |
| 217 | ide_hwif_t *hwif = drive->hwif; |
| 218 | unsigned long flags; |
| 219 | u8 stat; |
| 220 | |
| 221 | local_irq_save(flags); |
| 222 | stat = hwif->tp_ops->read_status(hwif); |
| 223 | local_irq_restore(flags); |
| 224 | |
| 225 | progress_indication = ((stat & ATA_DSC) == 0) ? 0 : 0x10000; |
| 226 | } |
| 227 | |
| 228 | if (put_user(progress_indication, arg)) |
| 229 | return -EFAULT; |
| 230 | |
| 231 | return 0; |
| 232 | } |
| 233 | |
Bartlomiej Zolnierkiewicz | 5bb1536 | 2008-10-13 21:39:44 +0200 | [diff] [blame] | 234 | static int ide_floppy_lockdoor(ide_drive_t *drive, struct ide_atapi_pc *pc, |
| 235 | unsigned long arg, unsigned int cmd) |
| 236 | { |
Bartlomiej Zolnierkiewicz | 0df9627 | 2008-10-17 18:09:16 +0200 | [diff] [blame] | 237 | struct ide_disk_obj *floppy = drive->driver_data; |
Bartlomiej Zolnierkiewicz | 5bb1536 | 2008-10-13 21:39:44 +0200 | [diff] [blame] | 238 | struct gendisk *disk = floppy->disk; |
| 239 | int prevent = (arg && cmd != CDROMEJECT) ? 1 : 0; |
| 240 | |
| 241 | if (floppy->openers > 1) |
| 242 | return -EBUSY; |
| 243 | |
| 244 | ide_set_media_lock(drive, disk, prevent); |
| 245 | |
| 246 | if (cmd == CDROMEJECT) |
| 247 | ide_do_start_stop(drive, disk, 2); |
| 248 | |
| 249 | return 0; |
| 250 | } |
| 251 | |
Linus Torvalds | 07bd3f4 | 2009-01-02 16:12:51 +0100 | [diff] [blame] | 252 | static int ide_floppy_format_ioctl(ide_drive_t *drive, struct ide_atapi_pc *pc, |
| 253 | fmode_t mode, unsigned int cmd, |
| 254 | void __user *argp) |
Bartlomiej Zolnierkiewicz | 0127854 | 2008-10-10 22:39:38 +0200 | [diff] [blame] | 255 | { |
| 256 | switch (cmd) { |
| 257 | case IDEFLOPPY_IOCTL_FORMAT_SUPPORTED: |
| 258 | return 0; |
| 259 | case IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY: |
Linus Torvalds | 07bd3f4 | 2009-01-02 16:12:51 +0100 | [diff] [blame] | 260 | return ide_floppy_get_format_capacities(drive, pc, argp); |
Bartlomiej Zolnierkiewicz | 0127854 | 2008-10-10 22:39:38 +0200 | [diff] [blame] | 261 | case IDEFLOPPY_IOCTL_FORMAT_START: |
Al Viro | badf808 | 2008-10-16 10:23:20 -0400 | [diff] [blame] | 262 | if (!(mode & FMODE_WRITE)) |
Bartlomiej Zolnierkiewicz | 0127854 | 2008-10-10 22:39:38 +0200 | [diff] [blame] | 263 | return -EPERM; |
Linus Torvalds | 07bd3f4 | 2009-01-02 16:12:51 +0100 | [diff] [blame] | 264 | return ide_floppy_format_unit(drive, pc, (int __user *)argp); |
Bartlomiej Zolnierkiewicz | 0127854 | 2008-10-10 22:39:38 +0200 | [diff] [blame] | 265 | case IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS: |
Linus Torvalds | 07bd3f4 | 2009-01-02 16:12:51 +0100 | [diff] [blame] | 266 | return ide_floppy_get_format_progress(drive, pc, argp); |
Bartlomiej Zolnierkiewicz | 0127854 | 2008-10-10 22:39:38 +0200 | [diff] [blame] | 267 | default: |
| 268 | return -ENOTTY; |
| 269 | } |
| 270 | } |
Bartlomiej Zolnierkiewicz | 5bb1536 | 2008-10-13 21:39:44 +0200 | [diff] [blame] | 271 | |
Al Viro | badf808 | 2008-10-16 10:23:20 -0400 | [diff] [blame] | 272 | int ide_floppy_ioctl(ide_drive_t *drive, struct block_device *bdev, |
| 273 | fmode_t mode, unsigned int cmd, unsigned long arg) |
Bartlomiej Zolnierkiewicz | 5bb1536 | 2008-10-13 21:39:44 +0200 | [diff] [blame] | 274 | { |
Bartlomiej Zolnierkiewicz | 5bb1536 | 2008-10-13 21:39:44 +0200 | [diff] [blame] | 275 | struct ide_atapi_pc pc; |
| 276 | void __user *argp = (void __user *)arg; |
| 277 | int err; |
| 278 | |
Arnd Bergmann | 8a6cfeb | 2010-07-08 10:18:46 +0200 | [diff] [blame] | 279 | lock_kernel(); |
| 280 | if (cmd == CDROMEJECT || cmd == CDROM_LOCKDOOR) { |
| 281 | err = ide_floppy_lockdoor(drive, &pc, arg, cmd); |
| 282 | goto out; |
| 283 | } |
Bartlomiej Zolnierkiewicz | 5bb1536 | 2008-10-13 21:39:44 +0200 | [diff] [blame] | 284 | |
Linus Torvalds | 07bd3f4 | 2009-01-02 16:12:51 +0100 | [diff] [blame] | 285 | err = ide_floppy_format_ioctl(drive, &pc, mode, cmd, argp); |
Bartlomiej Zolnierkiewicz | 5bb1536 | 2008-10-13 21:39:44 +0200 | [diff] [blame] | 286 | if (err != -ENOTTY) |
Arnd Bergmann | 8a6cfeb | 2010-07-08 10:18:46 +0200 | [diff] [blame] | 287 | goto out; |
Bartlomiej Zolnierkiewicz | 5bb1536 | 2008-10-13 21:39:44 +0200 | [diff] [blame] | 288 | |
| 289 | /* |
| 290 | * skip SCSI_IOCTL_SEND_COMMAND (deprecated) |
| 291 | * and CDROM_SEND_PACKET (legacy) ioctls |
| 292 | */ |
| 293 | if (cmd != CDROM_SEND_PACKET && cmd != SCSI_IOCTL_SEND_COMMAND) |
Al Viro | 74f3c8a | 2007-08-27 15:38:10 -0400 | [diff] [blame] | 294 | err = scsi_cmd_ioctl(bdev->bd_disk->queue, bdev->bd_disk, |
Al Viro | badf808 | 2008-10-16 10:23:20 -0400 | [diff] [blame] | 295 | mode, cmd, argp); |
Bartlomiej Zolnierkiewicz | 5bb1536 | 2008-10-13 21:39:44 +0200 | [diff] [blame] | 296 | |
| 297 | if (err == -ENOTTY) |
Al Viro | 1bddd9e | 2008-09-02 17:19:43 -0400 | [diff] [blame] | 298 | err = generic_ide_ioctl(drive, bdev, cmd, arg); |
Bartlomiej Zolnierkiewicz | 5bb1536 | 2008-10-13 21:39:44 +0200 | [diff] [blame] | 299 | |
Arnd Bergmann | 8a6cfeb | 2010-07-08 10:18:46 +0200 | [diff] [blame] | 300 | out: |
| 301 | unlock_kernel(); |
Bartlomiej Zolnierkiewicz | 5bb1536 | 2008-10-13 21:39:44 +0200 | [diff] [blame] | 302 | return err; |
| 303 | } |