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