2 * ide-floppy IOCTLs handling.
5 #include <linux/kernel.h>
7 #include <linux/cdrom.h>
9 #include <asm/unaligned.h>
11 #include <scsi/scsi_ioctl.h>
13 #include "ide-floppy.h"
16 * Obtain the list of formattable capacities.
17 * Very similar to ide_floppy_get_capacity, except that we push the capacity
18 * descriptors to userland, instead of our own structures.
20 * Userland gives us the following structure:
22 * struct idefloppy_format_capacities {
30 * userland initializes nformats to the number of allocated formats[] records.
31 * On exit we set nformats to the number of records we've actually initialized.
34 static int ide_floppy_get_format_capacities(ide_drive_t *drive,
35 struct ide_atapi_pc *pc,
38 struct ide_disk_obj *floppy = drive->driver_data;
39 int i, blocks, length, u_array_size, u_index;
41 u8 pc_buf[256], header_len, desc_cnt;
43 if (get_user(u_array_size, arg))
46 if (u_array_size <= 0)
49 ide_floppy_create_read_capacity_cmd(pc);
50 pc->buf_size = sizeof(pc_buf);
52 if (ide_queue_pc_tail(drive, floppy->disk, pc, pc_buf, pc->req_xfer)) {
53 printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
57 header_len = pc_buf[3];
58 desc_cnt = header_len / 8; /* capacity descriptor of 8 bytes */
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.
68 for (i = 1; i < desc_cnt; i++) {
69 unsigned int desc_start = 4 + i*8;
71 if (u_index >= u_array_size)
72 break; /* User-supplied buffer too small */
74 blocks = be32_to_cpup((__be32 *)&pc_buf[desc_start]);
75 length = be16_to_cpup((__be16 *)&pc_buf[desc_start + 6]);
77 if (put_user(blocks, argp))
82 if (put_user(length, argp))
90 if (put_user(u_index, arg))
96 static void ide_floppy_create_format_unit_cmd(struct ide_atapi_pc *pc,
97 u8 *buf, int b, int l,
101 pc->c[0] = GPCMD_FORMAT_UNIT;
106 /* Default format list header, u8 1: FOV/DCRT/IMM bits set */
108 if (flags & 1) /* Verify bit on... */
109 buf[1] ^= 0x20; /* ... turn off DCRT bit */
112 put_unaligned(cpu_to_be32(b), (unsigned int *)(&buf[4]));
113 put_unaligned(cpu_to_be32(l), (unsigned int *)(&buf[8]));
115 pc->flags |= PC_FLAG_WRITING;
118 static int ide_floppy_get_sfrp_bit(ide_drive_t *drive, struct ide_atapi_pc *pc)
120 struct ide_disk_obj *floppy = drive->driver_data;
123 drive->atapi_flags &= ~IDE_AFLAG_SRFP;
125 ide_floppy_create_mode_sense_cmd(pc, IDEFLOPPY_CAPABILITIES_PAGE);
126 pc->flags |= PC_FLAG_SUPPRESS_ERROR;
128 if (ide_queue_pc_tail(drive, floppy->disk, pc, buf, pc->req_xfer))
131 if (buf[8 + 2] & 0x40)
132 drive->atapi_flags |= IDE_AFLAG_SRFP;
137 static int ide_floppy_format_unit(ide_drive_t *drive, struct ide_atapi_pc *pc,
140 struct ide_disk_obj *floppy = drive->driver_data;
142 int blocks, length, flags, err = 0;
144 if (floppy->openers > 1) {
145 /* Don't format if someone is using the disk */
146 drive->dev_flags &= ~IDE_DFLAG_FORMAT_IN_PROGRESS;
150 drive->dev_flags |= IDE_DFLAG_FORMAT_IN_PROGRESS;
153 * Send ATAPI_FORMAT_UNIT to the drive.
155 * Userland gives us the following structure:
157 * struct idefloppy_format_command {
163 * flags is a bitmask, currently, the only defined flag is:
165 * 0x01 - verify media after format.
167 if (get_user(blocks, arg) ||
168 get_user(length, arg+1) ||
169 get_user(flags, arg+2)) {
174 ide_floppy_get_sfrp_bit(drive, pc);
175 ide_floppy_create_format_unit_cmd(pc, buf, blocks, length, flags);
177 if (ide_queue_pc_tail(drive, floppy->disk, pc, buf, pc->req_xfer))
182 drive->dev_flags &= ~IDE_DFLAG_FORMAT_IN_PROGRESS;
187 * Get ATAPI_FORMAT_UNIT progress indication.
189 * Userland gives a pointer to an int. The int is set to a progress
190 * indicator 0-65536, with 65536=100%.
192 * If the drive does not support format progress indication, we just check
193 * the dsc bit, and return either 0 or 65536.
196 static int ide_floppy_get_format_progress(ide_drive_t *drive,
197 struct ide_atapi_pc *pc,
200 struct ide_disk_obj *floppy = drive->driver_data;
202 int progress_indication = 0x10000;
204 if (drive->atapi_flags & IDE_AFLAG_SRFP) {
205 ide_create_request_sense_cmd(drive, pc);
206 if (ide_queue_pc_tail(drive, floppy->disk, pc, sense_buf,
210 if (floppy->sense_key == 2 &&
213 progress_indication = floppy->progress_indication;
215 /* Else assume format_unit has finished, and we're at 0x10000 */
217 ide_hwif_t *hwif = drive->hwif;
221 local_irq_save(flags);
222 stat = hwif->tp_ops->read_status(hwif);
223 local_irq_restore(flags);
225 progress_indication = ((stat & ATA_DSC) == 0) ? 0 : 0x10000;
228 if (put_user(progress_indication, arg))
234 static int ide_floppy_lockdoor(ide_drive_t *drive, struct ide_atapi_pc *pc,
235 unsigned long arg, unsigned int cmd)
237 struct ide_disk_obj *floppy = drive->driver_data;
238 struct gendisk *disk = floppy->disk;
239 int prevent = (arg && cmd != CDROMEJECT) ? 1 : 0;
241 if (floppy->openers > 1)
244 ide_set_media_lock(drive, disk, prevent);
246 if (cmd == CDROMEJECT)
247 ide_do_start_stop(drive, disk, 2);
252 static int ide_floppy_format_ioctl(ide_drive_t *drive, struct ide_atapi_pc *pc,
253 fmode_t mode, unsigned int cmd,
257 case IDEFLOPPY_IOCTL_FORMAT_SUPPORTED:
259 case IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY:
260 return ide_floppy_get_format_capacities(drive, pc, argp);
261 case IDEFLOPPY_IOCTL_FORMAT_START:
262 if (!(mode & FMODE_WRITE))
264 return ide_floppy_format_unit(drive, pc, (int __user *)argp);
265 case IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS:
266 return ide_floppy_get_format_progress(drive, pc, argp);
272 int ide_floppy_ioctl(ide_drive_t *drive, struct block_device *bdev,
273 fmode_t mode, unsigned int cmd, unsigned long arg)
275 struct ide_atapi_pc pc;
276 void __user *argp = (void __user *)arg;
279 if (cmd == CDROMEJECT || cmd == CDROM_LOCKDOOR)
280 return ide_floppy_lockdoor(drive, &pc, arg, cmd);
282 err = ide_floppy_format_ioctl(drive, &pc, mode, cmd, argp);
287 * skip SCSI_IOCTL_SEND_COMMAND (deprecated)
288 * and CDROM_SEND_PACKET (legacy) ioctls
290 if (cmd != CDROM_SEND_PACKET && cmd != SCSI_IOCTL_SEND_COMMAND)
291 err = scsi_cmd_ioctl(bdev->bd_disk->queue, bdev->bd_disk,
295 err = generic_ide_ioctl(drive, bdev, cmd, arg);