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