1 // SPDX-License-Identifier: GPL-2.0
3 * ide-floppy IOCTLs handling.
6 #include <linux/kernel.h>
8 #include <linux/compat.h>
9 #include <linux/cdrom.h>
10 #include <linux/mutex.h>
12 #include <asm/unaligned.h>
14 #include <scsi/scsi_ioctl.h>
16 #include "ide-floppy.h"
19 * Obtain the list of formattable capacities.
20 * Very similar to ide_floppy_get_capacity, except that we push the capacity
21 * descriptors to userland, instead of our own structures.
23 * Userland gives us the following structure:
25 * struct idefloppy_format_capacities {
33 * userland initializes nformats to the number of allocated formats[] records.
34 * On exit we set nformats to the number of records we've actually initialized.
37 static DEFINE_MUTEX(ide_floppy_ioctl_mutex
);
38 static int ide_floppy_get_format_capacities(ide_drive_t
*drive
,
39 struct ide_atapi_pc
*pc
,
42 struct ide_disk_obj
*floppy
= drive
->driver_data
;
43 int i
, blocks
, length
, u_array_size
, u_index
;
45 u8 pc_buf
[256], header_len
, desc_cnt
;
47 if (get_user(u_array_size
, arg
))
50 if (u_array_size
<= 0)
53 ide_floppy_create_read_capacity_cmd(pc
);
55 if (ide_queue_pc_tail(drive
, floppy
->disk
, pc
, pc_buf
, pc
->req_xfer
)) {
56 printk(KERN_ERR
"ide-floppy: Can't get floppy parameters\n");
60 header_len
= pc_buf
[3];
61 desc_cnt
= header_len
/ 8; /* capacity descriptor of 8 bytes */
67 * We always skip the first capacity descriptor. That's the current
68 * capacity. We are interested in the remaining descriptors, the
69 * formattable capacities.
71 for (i
= 1; i
< desc_cnt
; i
++) {
72 unsigned int desc_start
= 4 + i
*8;
74 if (u_index
>= u_array_size
)
75 break; /* User-supplied buffer too small */
77 blocks
= be32_to_cpup((__be32
*)&pc_buf
[desc_start
]);
78 length
= be16_to_cpup((__be16
*)&pc_buf
[desc_start
+ 6]);
80 if (put_user(blocks
, argp
))
85 if (put_user(length
, argp
))
93 if (put_user(u_index
, arg
))
99 static void ide_floppy_create_format_unit_cmd(struct ide_atapi_pc
*pc
,
100 u8
*buf
, int b
, int l
,
104 pc
->c
[0] = GPCMD_FORMAT_UNIT
;
109 /* Default format list header, u8 1: FOV/DCRT/IMM bits set */
111 if (flags
& 1) /* Verify bit on... */
112 buf
[1] ^= 0x20; /* ... turn off DCRT bit */
115 put_unaligned(cpu_to_be32(b
), (unsigned int *)(&buf
[4]));
116 put_unaligned(cpu_to_be32(l
), (unsigned int *)(&buf
[8]));
118 pc
->flags
|= PC_FLAG_WRITING
;
121 static int ide_floppy_get_sfrp_bit(ide_drive_t
*drive
, struct ide_atapi_pc
*pc
)
123 struct ide_disk_obj
*floppy
= drive
->driver_data
;
126 drive
->atapi_flags
&= ~IDE_AFLAG_SRFP
;
128 ide_floppy_create_mode_sense_cmd(pc
, IDEFLOPPY_CAPABILITIES_PAGE
);
129 pc
->flags
|= PC_FLAG_SUPPRESS_ERROR
;
131 if (ide_queue_pc_tail(drive
, floppy
->disk
, pc
, buf
, pc
->req_xfer
))
134 if (buf
[8 + 2] & 0x40)
135 drive
->atapi_flags
|= IDE_AFLAG_SRFP
;
140 static int ide_floppy_format_unit(ide_drive_t
*drive
, struct ide_atapi_pc
*pc
,
143 struct ide_disk_obj
*floppy
= drive
->driver_data
;
145 int blocks
, length
, flags
, err
= 0;
147 if (floppy
->openers
> 1) {
148 /* Don't format if someone is using the disk */
149 drive
->dev_flags
&= ~IDE_DFLAG_FORMAT_IN_PROGRESS
;
153 drive
->dev_flags
|= IDE_DFLAG_FORMAT_IN_PROGRESS
;
156 * Send ATAPI_FORMAT_UNIT to the drive.
158 * Userland gives us the following structure:
160 * struct idefloppy_format_command {
166 * flags is a bitmask, currently, the only defined flag is:
168 * 0x01 - verify media after format.
170 if (get_user(blocks
, arg
) ||
171 get_user(length
, arg
+1) ||
172 get_user(flags
, arg
+2)) {
177 ide_floppy_get_sfrp_bit(drive
, pc
);
178 ide_floppy_create_format_unit_cmd(pc
, buf
, blocks
, length
, flags
);
180 if (ide_queue_pc_tail(drive
, floppy
->disk
, pc
, buf
, pc
->req_xfer
))
185 drive
->dev_flags
&= ~IDE_DFLAG_FORMAT_IN_PROGRESS
;
190 * Get ATAPI_FORMAT_UNIT progress indication.
192 * Userland gives a pointer to an int. The int is set to a progress
193 * indicator 0-65536, with 65536=100%.
195 * If the drive does not support format progress indication, we just check
196 * the dsc bit, and return either 0 or 65536.
199 static int ide_floppy_get_format_progress(ide_drive_t
*drive
,
200 struct ide_atapi_pc
*pc
,
203 struct ide_disk_obj
*floppy
= drive
->driver_data
;
205 int progress_indication
= 0x10000;
207 if (drive
->atapi_flags
& IDE_AFLAG_SRFP
) {
208 ide_create_request_sense_cmd(drive
, pc
);
209 if (ide_queue_pc_tail(drive
, floppy
->disk
, pc
, sense_buf
,
213 if (floppy
->sense_key
== 2 &&
216 progress_indication
= floppy
->progress_indication
;
218 /* Else assume format_unit has finished, and we're at 0x10000 */
220 ide_hwif_t
*hwif
= drive
->hwif
;
224 local_irq_save(flags
);
225 stat
= hwif
->tp_ops
->read_status(hwif
);
226 local_irq_restore(flags
);
228 progress_indication
= ((stat
& ATA_DSC
) == 0) ? 0 : 0x10000;
231 if (put_user(progress_indication
, arg
))
237 static int ide_floppy_lockdoor(ide_drive_t
*drive
, struct ide_atapi_pc
*pc
,
238 unsigned long arg
, unsigned int cmd
)
240 struct ide_disk_obj
*floppy
= drive
->driver_data
;
241 struct gendisk
*disk
= floppy
->disk
;
242 int prevent
= (arg
&& cmd
!= CDROMEJECT
) ? 1 : 0;
244 if (floppy
->openers
> 1)
247 ide_set_media_lock(drive
, disk
, prevent
);
249 if (cmd
== CDROMEJECT
)
250 ide_do_start_stop(drive
, disk
, 2);
255 static int ide_floppy_format_ioctl(ide_drive_t
*drive
, struct ide_atapi_pc
*pc
,
256 fmode_t mode
, unsigned int cmd
,
260 case IDEFLOPPY_IOCTL_FORMAT_SUPPORTED
:
262 case IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY
:
263 return ide_floppy_get_format_capacities(drive
, pc
, argp
);
264 case IDEFLOPPY_IOCTL_FORMAT_START
:
265 if (!(mode
& FMODE_WRITE
))
267 return ide_floppy_format_unit(drive
, pc
, (int __user
*)argp
);
268 case IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS
:
269 return ide_floppy_get_format_progress(drive
, pc
, argp
);
275 int ide_floppy_ioctl(ide_drive_t
*drive
, struct block_device
*bdev
,
276 fmode_t mode
, unsigned int cmd
, unsigned long arg
)
278 struct ide_atapi_pc pc
;
279 void __user
*argp
= (void __user
*)arg
;
282 mutex_lock(&ide_floppy_ioctl_mutex
);
283 if (cmd
== CDROMEJECT
|| cmd
== CDROM_LOCKDOOR
) {
284 err
= ide_floppy_lockdoor(drive
, &pc
, arg
, cmd
);
288 err
= ide_floppy_format_ioctl(drive
, &pc
, mode
, cmd
, argp
);
293 * skip SCSI_IOCTL_SEND_COMMAND (deprecated)
294 * and CDROM_SEND_PACKET (legacy) ioctls
296 if (cmd
!= CDROM_SEND_PACKET
&& cmd
!= SCSI_IOCTL_SEND_COMMAND
)
297 err
= scsi_cmd_blk_ioctl(bdev
, mode
, cmd
, argp
);
300 err
= generic_ide_ioctl(drive
, bdev
, cmd
, arg
);
303 mutex_unlock(&ide_floppy_ioctl_mutex
);
308 int ide_floppy_compat_ioctl(ide_drive_t
*drive
, struct block_device
*bdev
,
309 fmode_t mode
, unsigned int cmd
, unsigned long arg
)
311 struct ide_atapi_pc pc
;
312 void __user
*argp
= compat_ptr(arg
);
315 mutex_lock(&ide_floppy_ioctl_mutex
);
316 if (cmd
== CDROMEJECT
|| cmd
== CDROM_LOCKDOOR
) {
317 err
= ide_floppy_lockdoor(drive
, &pc
, arg
, cmd
);
321 err
= ide_floppy_format_ioctl(drive
, &pc
, mode
, cmd
, argp
);
326 * skip SCSI_IOCTL_SEND_COMMAND (deprecated)
327 * and CDROM_SEND_PACKET (legacy) ioctls
329 if (cmd
!= CDROM_SEND_PACKET
&& cmd
!= SCSI_IOCTL_SEND_COMMAND
)
330 err
= scsi_cmd_blk_ioctl(bdev
, mode
, cmd
, argp
);
333 err
= generic_ide_ioctl(drive
, bdev
, cmd
, arg
);
336 mutex_unlock(&ide_floppy_ioctl_mutex
);