blob: 39a790ac6cc305b023874a1c2af78a98a38c579c [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +02002/*
3 * ide-floppy IOCTLs handling.
4 */
5
6#include <linux/kernel.h>
7#include <linux/ide.h>
Arnd Bergmannc103d6e2019-03-15 17:26:56 +01008#include <linux/compat.h>
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +02009#include <linux/cdrom.h>
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020010#include <linux/mutex.h>
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +020011
12#include <asm/unaligned.h>
13
14#include <scsi/scsi_ioctl.h>
15
16#include "ide-floppy.h"
17
18/*
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.
22 *
23 * Userland gives us the following structure:
24 *
25 * struct idefloppy_format_capacities {
26 * int nformats;
27 * struct {
28 * int nblocks;
29 * int blocksize;
30 * } formats[];
31 * };
32 *
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.
35 */
36
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020037static DEFINE_MUTEX(ide_floppy_ioctl_mutex);
Linus Torvalds07bd3f42009-01-02 16:12:51 +010038static int ide_floppy_get_format_capacities(ide_drive_t *drive,
39 struct ide_atapi_pc *pc,
40 int __user *arg)
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +020041{
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +020042 struct ide_disk_obj *floppy = drive->driver_data;
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +020043 int i, blocks, length, u_array_size, u_index;
44 int __user *argp;
Bartlomiej Zolnierkiewicz41fa9f82009-03-31 20:15:25 +020045 u8 pc_buf[256], header_len, desc_cnt;
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +020046
47 if (get_user(u_array_size, arg))
48 return -EFAULT;
49
50 if (u_array_size <= 0)
51 return -EINVAL;
52
Linus Torvalds07bd3f42009-01-02 16:12:51 +010053 ide_floppy_create_read_capacity_cmd(pc);
Bartlomiej Zolnierkiewicz41fa9f82009-03-31 20:15:25 +020054
Borislav Petkovb13345f2009-05-02 10:26:12 +020055 if (ide_queue_pc_tail(drive, floppy->disk, pc, pc_buf, pc->req_xfer)) {
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +020056 printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
57 return -EIO;
58 }
59
Borislav Petkovb13345f2009-05-02 10:26:12 +020060 header_len = pc_buf[3];
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +020061 desc_cnt = header_len / 8; /* capacity descriptor of 8 bytes */
62
63 u_index = 0;
64 argp = arg + 1;
65
66 /*
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.
70 */
71 for (i = 1; i < desc_cnt; i++) {
72 unsigned int desc_start = 4 + i*8;
73
74 if (u_index >= u_array_size)
75 break; /* User-supplied buffer too small */
76
Borislav Petkovb13345f2009-05-02 10:26:12 +020077 blocks = be32_to_cpup((__be32 *)&pc_buf[desc_start]);
78 length = be16_to_cpup((__be16 *)&pc_buf[desc_start + 6]);
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +020079
80 if (put_user(blocks, argp))
81 return -EFAULT;
82
83 ++argp;
84
85 if (put_user(length, argp))
86 return -EFAULT;
87
88 ++argp;
89
90 ++u_index;
91 }
92
93 if (put_user(u_index, arg))
94 return -EFAULT;
95
96 return 0;
97}
98
Borislav Petkov5122e512009-05-02 10:53:10 +020099static void ide_floppy_create_format_unit_cmd(struct ide_atapi_pc *pc,
100 u8 *buf, int b, int l,
101 int flags)
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200102{
103 ide_init_pc(pc);
104 pc->c[0] = GPCMD_FORMAT_UNIT;
105 pc->c[1] = 0x17;
106
Borislav Petkov5122e512009-05-02 10:53:10 +0200107 memset(buf, 0, 12);
108 buf[1] = 0xA2;
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200109 /* Default format list header, u8 1: FOV/DCRT/IMM bits set */
110
111 if (flags & 1) /* Verify bit on... */
Borislav Petkov5122e512009-05-02 10:53:10 +0200112 buf[1] ^= 0x20; /* ... turn off DCRT bit */
113 buf[3] = 8;
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200114
Borislav Petkov5122e512009-05-02 10:53:10 +0200115 put_unaligned(cpu_to_be32(b), (unsigned int *)(&buf[4]));
116 put_unaligned(cpu_to_be32(l), (unsigned int *)(&buf[8]));
117 pc->req_xfer = 12;
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200118 pc->flags |= PC_FLAG_WRITING;
119}
120
Linus Torvalds07bd3f42009-01-02 16:12:51 +0100121static int ide_floppy_get_sfrp_bit(ide_drive_t *drive, struct ide_atapi_pc *pc)
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200122{
Bartlomiej Zolnierkiewicz0df96272008-10-17 18:09:16 +0200123 struct ide_disk_obj *floppy = drive->driver_data;
Borislav Petkov802e66342009-05-02 10:45:17 +0200124 u8 buf[20];
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200125
126 drive->atapi_flags &= ~IDE_AFLAG_SRFP;
127
Linus Torvalds07bd3f42009-01-02 16:12:51 +0100128 ide_floppy_create_mode_sense_cmd(pc, IDEFLOPPY_CAPABILITIES_PAGE);
129 pc->flags |= PC_FLAG_SUPPRESS_ERROR;
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200130
Borislav Petkov802e66342009-05-02 10:45:17 +0200131 if (ide_queue_pc_tail(drive, floppy->disk, pc, buf, pc->req_xfer))
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200132 return 1;
133
Borislav Petkov802e66342009-05-02 10:45:17 +0200134 if (buf[8 + 2] & 0x40)
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200135 drive->atapi_flags |= IDE_AFLAG_SRFP;
136
137 return 0;
138}
139
Linus Torvalds07bd3f42009-01-02 16:12:51 +0100140static int ide_floppy_format_unit(ide_drive_t *drive, struct ide_atapi_pc *pc,
141 int __user *arg)
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200142{
Bartlomiej Zolnierkiewicz0df96272008-10-17 18:09:16 +0200143 struct ide_disk_obj *floppy = drive->driver_data;
Borislav Petkov5122e512009-05-02 10:53:10 +0200144 u8 buf[12];
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200145 int blocks, length, flags, err = 0;
146
147 if (floppy->openers > 1) {
148 /* Don't format if someone is using the disk */
Bartlomiej Zolnierkiewicze0128622008-10-17 18:09:11 +0200149 drive->dev_flags &= ~IDE_DFLAG_FORMAT_IN_PROGRESS;
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200150 return -EBUSY;
151 }
152
Bartlomiej Zolnierkiewicze0128622008-10-17 18:09:11 +0200153 drive->dev_flags |= IDE_DFLAG_FORMAT_IN_PROGRESS;
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200154
155 /*
156 * Send ATAPI_FORMAT_UNIT to the drive.
157 *
158 * Userland gives us the following structure:
159 *
160 * struct idefloppy_format_command {
161 * int nblocks;
162 * int blocksize;
163 * int flags;
164 * } ;
165 *
166 * flags is a bitmask, currently, the only defined flag is:
167 *
168 * 0x01 - verify media after format.
169 */
170 if (get_user(blocks, arg) ||
171 get_user(length, arg+1) ||
172 get_user(flags, arg+2)) {
173 err = -EFAULT;
174 goto out;
175 }
176
Linus Torvalds07bd3f42009-01-02 16:12:51 +0100177 ide_floppy_get_sfrp_bit(drive, pc);
Borislav Petkov5122e512009-05-02 10:53:10 +0200178 ide_floppy_create_format_unit_cmd(pc, buf, blocks, length, flags);
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200179
Borislav Petkov5122e512009-05-02 10:53:10 +0200180 if (ide_queue_pc_tail(drive, floppy->disk, pc, buf, pc->req_xfer))
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200181 err = -EIO;
182
183out:
184 if (err)
Bartlomiej Zolnierkiewicze0128622008-10-17 18:09:11 +0200185 drive->dev_flags &= ~IDE_DFLAG_FORMAT_IN_PROGRESS;
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200186 return err;
187}
188
189/*
190 * Get ATAPI_FORMAT_UNIT progress indication.
191 *
192 * Userland gives a pointer to an int. The int is set to a progress
193 * indicator 0-65536, with 65536=100%.
194 *
195 * If the drive does not support format progress indication, we just check
196 * the dsc bit, and return either 0 or 65536.
197 */
198
Linus Torvalds07bd3f42009-01-02 16:12:51 +0100199static int ide_floppy_get_format_progress(ide_drive_t *drive,
200 struct ide_atapi_pc *pc,
201 int __user *arg)
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200202{
Bartlomiej Zolnierkiewicz0df96272008-10-17 18:09:16 +0200203 struct ide_disk_obj *floppy = drive->driver_data;
Borislav Petkov60cfab82009-05-12 08:59:49 +0200204 u8 sense_buf[18];
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200205 int progress_indication = 0x10000;
206
207 if (drive->atapi_flags & IDE_AFLAG_SRFP) {
Linus Torvalds07bd3f42009-01-02 16:12:51 +0100208 ide_create_request_sense_cmd(drive, pc);
Borislav Petkov60cfab82009-05-12 08:59:49 +0200209 if (ide_queue_pc_tail(drive, floppy->disk, pc, sense_buf,
Borislav Petkovb13345f2009-05-02 10:26:12 +0200210 pc->req_xfer))
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200211 return -EIO;
212
213 if (floppy->sense_key == 2 &&
214 floppy->asc == 4 &&
215 floppy->ascq == 4)
216 progress_indication = floppy->progress_indication;
217
218 /* Else assume format_unit has finished, and we're at 0x10000 */
219 } else {
220 ide_hwif_t *hwif = drive->hwif;
221 unsigned long flags;
222 u8 stat;
223
224 local_irq_save(flags);
225 stat = hwif->tp_ops->read_status(hwif);
226 local_irq_restore(flags);
227
228 progress_indication = ((stat & ATA_DSC) == 0) ? 0 : 0x10000;
229 }
230
231 if (put_user(progress_indication, arg))
232 return -EFAULT;
233
234 return 0;
235}
236
Bartlomiej Zolnierkiewicz5bb15362008-10-13 21:39:44 +0200237static int ide_floppy_lockdoor(ide_drive_t *drive, struct ide_atapi_pc *pc,
238 unsigned long arg, unsigned int cmd)
239{
Bartlomiej Zolnierkiewicz0df96272008-10-17 18:09:16 +0200240 struct ide_disk_obj *floppy = drive->driver_data;
Bartlomiej Zolnierkiewicz5bb15362008-10-13 21:39:44 +0200241 struct gendisk *disk = floppy->disk;
242 int prevent = (arg && cmd != CDROMEJECT) ? 1 : 0;
243
244 if (floppy->openers > 1)
245 return -EBUSY;
246
247 ide_set_media_lock(drive, disk, prevent);
248
249 if (cmd == CDROMEJECT)
250 ide_do_start_stop(drive, disk, 2);
251
252 return 0;
253}
254
Linus Torvalds07bd3f42009-01-02 16:12:51 +0100255static int ide_floppy_format_ioctl(ide_drive_t *drive, struct ide_atapi_pc *pc,
256 fmode_t mode, unsigned int cmd,
257 void __user *argp)
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200258{
259 switch (cmd) {
260 case IDEFLOPPY_IOCTL_FORMAT_SUPPORTED:
261 return 0;
262 case IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY:
Linus Torvalds07bd3f42009-01-02 16:12:51 +0100263 return ide_floppy_get_format_capacities(drive, pc, argp);
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200264 case IDEFLOPPY_IOCTL_FORMAT_START:
Al Virobadf8082008-10-16 10:23:20 -0400265 if (!(mode & FMODE_WRITE))
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200266 return -EPERM;
Linus Torvalds07bd3f42009-01-02 16:12:51 +0100267 return ide_floppy_format_unit(drive, pc, (int __user *)argp);
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200268 case IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS:
Linus Torvalds07bd3f42009-01-02 16:12:51 +0100269 return ide_floppy_get_format_progress(drive, pc, argp);
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200270 default:
271 return -ENOTTY;
272 }
273}
Bartlomiej Zolnierkiewicz5bb15362008-10-13 21:39:44 +0200274
Al Virobadf8082008-10-16 10:23:20 -0400275int ide_floppy_ioctl(ide_drive_t *drive, struct block_device *bdev,
276 fmode_t mode, unsigned int cmd, unsigned long arg)
Bartlomiej Zolnierkiewicz5bb15362008-10-13 21:39:44 +0200277{
Bartlomiej Zolnierkiewicz5bb15362008-10-13 21:39:44 +0200278 struct ide_atapi_pc pc;
279 void __user *argp = (void __user *)arg;
280 int err;
281
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200282 mutex_lock(&ide_floppy_ioctl_mutex);
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200283 if (cmd == CDROMEJECT || cmd == CDROM_LOCKDOOR) {
284 err = ide_floppy_lockdoor(drive, &pc, arg, cmd);
285 goto out;
286 }
Bartlomiej Zolnierkiewicz5bb15362008-10-13 21:39:44 +0200287
Linus Torvalds07bd3f42009-01-02 16:12:51 +0100288 err = ide_floppy_format_ioctl(drive, &pc, mode, cmd, argp);
Bartlomiej Zolnierkiewicz5bb15362008-10-13 21:39:44 +0200289 if (err != -ENOTTY)
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200290 goto out;
Bartlomiej Zolnierkiewicz5bb15362008-10-13 21:39:44 +0200291
292 /*
293 * skip SCSI_IOCTL_SEND_COMMAND (deprecated)
294 * and CDROM_SEND_PACKET (legacy) ioctls
295 */
296 if (cmd != CDROM_SEND_PACKET && cmd != SCSI_IOCTL_SEND_COMMAND)
Paolo Bonzini577ebb32012-01-12 16:01:27 +0100297 err = scsi_cmd_blk_ioctl(bdev, mode, cmd, argp);
Bartlomiej Zolnierkiewicz5bb15362008-10-13 21:39:44 +0200298
299 if (err == -ENOTTY)
Al Viro1bddd9e2008-09-02 17:19:43 -0400300 err = generic_ide_ioctl(drive, bdev, cmd, arg);
Bartlomiej Zolnierkiewicz5bb15362008-10-13 21:39:44 +0200301
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200302out:
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200303 mutex_unlock(&ide_floppy_ioctl_mutex);
Bartlomiej Zolnierkiewicz5bb15362008-10-13 21:39:44 +0200304 return err;
305}
Arnd Bergmannc103d6e2019-03-15 17:26:56 +0100306
307#ifdef CONFIG_COMPAT
308int ide_floppy_compat_ioctl(ide_drive_t *drive, struct block_device *bdev,
309 fmode_t mode, unsigned int cmd, unsigned long arg)
310{
311 struct ide_atapi_pc pc;
312 void __user *argp = compat_ptr(arg);
313 int err;
314
315 mutex_lock(&ide_floppy_ioctl_mutex);
316 if (cmd == CDROMEJECT || cmd == CDROM_LOCKDOOR) {
317 err = ide_floppy_lockdoor(drive, &pc, arg, cmd);
318 goto out;
319 }
320
321 err = ide_floppy_format_ioctl(drive, &pc, mode, cmd, argp);
322 if (err != -ENOTTY)
323 goto out;
324
325 /*
326 * skip SCSI_IOCTL_SEND_COMMAND (deprecated)
327 * and CDROM_SEND_PACKET (legacy) ioctls
328 */
329 if (cmd != CDROM_SEND_PACKET && cmd != SCSI_IOCTL_SEND_COMMAND)
330 err = scsi_cmd_blk_ioctl(bdev, mode, cmd, argp);
331
Arnd Bergmann1df23c62019-11-28 17:14:59 +0100332 if (err == -ENOTTY)
333 err = generic_ide_ioctl(drive, bdev, cmd, arg);
334
Arnd Bergmannc103d6e2019-03-15 17:26:56 +0100335out:
336 mutex_unlock(&ide_floppy_ioctl_mutex);
337 return err;
338}
339#endif