blob: 7a03d34bc2403a9bd38ff3529ab75ed1de28107f [file] [log] [blame]
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +02001/*
2 * ide-floppy IOCTLs handling.
3 */
4
5#include <linux/kernel.h>
6#include <linux/ide.h>
7#include <linux/cdrom.h>
8
9#include <asm/unaligned.h>
10
11#include <scsi/scsi_ioctl.h>
12
13#include "ide-floppy.h"
14
15/*
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.
19 *
20 * Userland gives us the following structure:
21 *
22 * struct idefloppy_format_capacities {
23 * int nformats;
24 * struct {
25 * int nblocks;
26 * int blocksize;
27 * } formats[];
28 * };
29 *
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.
32 */
33
Linus Torvalds07bd3f42009-01-02 16:12:51 +010034static int ide_floppy_get_format_capacities(ide_drive_t *drive,
35 struct ide_atapi_pc *pc,
36 int __user *arg)
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +020037{
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +020038 struct ide_disk_obj *floppy = drive->driver_data;
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +020039 int i, blocks, length, u_array_size, u_index;
40 int __user *argp;
Bartlomiej Zolnierkiewicz41fa9f82009-03-31 20:15:25 +020041 u8 pc_buf[256], header_len, desc_cnt;
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +020042
43 if (get_user(u_array_size, arg))
44 return -EFAULT;
45
46 if (u_array_size <= 0)
47 return -EINVAL;
48
Linus Torvalds07bd3f42009-01-02 16:12:51 +010049 ide_floppy_create_read_capacity_cmd(pc);
Bartlomiej Zolnierkiewicz41fa9f82009-03-31 20:15:25 +020050 pc->buf_size = sizeof(pc_buf);
51
Borislav Petkovb13345f2009-05-02 10:26:12 +020052 if (ide_queue_pc_tail(drive, floppy->disk, pc, pc_buf, pc->req_xfer)) {
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +020053 printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
54 return -EIO;
55 }
56
Borislav Petkovb13345f2009-05-02 10:26:12 +020057 header_len = pc_buf[3];
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +020058 desc_cnt = header_len / 8; /* capacity descriptor of 8 bytes */
59
60 u_index = 0;
61 argp = arg + 1;
62
63 /*
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.
67 */
68 for (i = 1; i < desc_cnt; i++) {
69 unsigned int desc_start = 4 + i*8;
70
71 if (u_index >= u_array_size)
72 break; /* User-supplied buffer too small */
73
Borislav Petkovb13345f2009-05-02 10:26:12 +020074 blocks = be32_to_cpup((__be32 *)&pc_buf[desc_start]);
75 length = be16_to_cpup((__be16 *)&pc_buf[desc_start + 6]);
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +020076
77 if (put_user(blocks, argp))
78 return -EFAULT;
79
80 ++argp;
81
82 if (put_user(length, argp))
83 return -EFAULT;
84
85 ++argp;
86
87 ++u_index;
88 }
89
90 if (put_user(u_index, arg))
91 return -EFAULT;
92
93 return 0;
94}
95
Borislav Petkov5122e512009-05-02 10:53:10 +020096static void ide_floppy_create_format_unit_cmd(struct ide_atapi_pc *pc,
97 u8 *buf, int b, int l,
98 int flags)
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +020099{
100 ide_init_pc(pc);
101 pc->c[0] = GPCMD_FORMAT_UNIT;
102 pc->c[1] = 0x17;
103
Borislav Petkov5122e512009-05-02 10:53:10 +0200104 memset(buf, 0, 12);
105 buf[1] = 0xA2;
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200106 /* Default format list header, u8 1: FOV/DCRT/IMM bits set */
107
108 if (flags & 1) /* Verify bit on... */
Borislav Petkov5122e512009-05-02 10:53:10 +0200109 buf[1] ^= 0x20; /* ... turn off DCRT bit */
110 buf[3] = 8;
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200111
Borislav Petkov5122e512009-05-02 10:53:10 +0200112 put_unaligned(cpu_to_be32(b), (unsigned int *)(&buf[4]));
113 put_unaligned(cpu_to_be32(l), (unsigned int *)(&buf[8]));
114 pc->req_xfer = 12;
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200115 pc->flags |= PC_FLAG_WRITING;
116}
117
Linus Torvalds07bd3f42009-01-02 16:12:51 +0100118static int ide_floppy_get_sfrp_bit(ide_drive_t *drive, struct ide_atapi_pc *pc)
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200119{
Bartlomiej Zolnierkiewicz0df96272008-10-17 18:09:16 +0200120 struct ide_disk_obj *floppy = drive->driver_data;
Borislav Petkov802e6632009-05-02 10:45:17 +0200121 u8 buf[20];
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200122
123 drive->atapi_flags &= ~IDE_AFLAG_SRFP;
124
Linus Torvalds07bd3f42009-01-02 16:12:51 +0100125 ide_floppy_create_mode_sense_cmd(pc, IDEFLOPPY_CAPABILITIES_PAGE);
126 pc->flags |= PC_FLAG_SUPPRESS_ERROR;
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200127
Borislav Petkov802e6632009-05-02 10:45:17 +0200128 if (ide_queue_pc_tail(drive, floppy->disk, pc, buf, pc->req_xfer))
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200129 return 1;
130
Borislav Petkov802e6632009-05-02 10:45:17 +0200131 if (buf[8 + 2] & 0x40)
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200132 drive->atapi_flags |= IDE_AFLAG_SRFP;
133
134 return 0;
135}
136
Linus Torvalds07bd3f42009-01-02 16:12:51 +0100137static int ide_floppy_format_unit(ide_drive_t *drive, struct ide_atapi_pc *pc,
138 int __user *arg)
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200139{
Bartlomiej Zolnierkiewicz0df96272008-10-17 18:09:16 +0200140 struct ide_disk_obj *floppy = drive->driver_data;
Borislav Petkov5122e512009-05-02 10:53:10 +0200141 u8 buf[12];
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200142 int blocks, length, flags, err = 0;
143
144 if (floppy->openers > 1) {
145 /* Don't format if someone is using the disk */
Bartlomiej Zolnierkiewicze0128622008-10-17 18:09:11 +0200146 drive->dev_flags &= ~IDE_DFLAG_FORMAT_IN_PROGRESS;
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200147 return -EBUSY;
148 }
149
Bartlomiej Zolnierkiewicze0128622008-10-17 18:09:11 +0200150 drive->dev_flags |= IDE_DFLAG_FORMAT_IN_PROGRESS;
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200151
152 /*
153 * Send ATAPI_FORMAT_UNIT to the drive.
154 *
155 * Userland gives us the following structure:
156 *
157 * struct idefloppy_format_command {
158 * int nblocks;
159 * int blocksize;
160 * int flags;
161 * } ;
162 *
163 * flags is a bitmask, currently, the only defined flag is:
164 *
165 * 0x01 - verify media after format.
166 */
167 if (get_user(blocks, arg) ||
168 get_user(length, arg+1) ||
169 get_user(flags, arg+2)) {
170 err = -EFAULT;
171 goto out;
172 }
173
Linus Torvalds07bd3f42009-01-02 16:12:51 +0100174 ide_floppy_get_sfrp_bit(drive, pc);
Borislav Petkov5122e512009-05-02 10:53:10 +0200175 ide_floppy_create_format_unit_cmd(pc, buf, blocks, length, flags);
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200176
Borislav Petkov5122e512009-05-02 10:53:10 +0200177 if (ide_queue_pc_tail(drive, floppy->disk, pc, buf, pc->req_xfer))
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200178 err = -EIO;
179
180out:
181 if (err)
Bartlomiej Zolnierkiewicze0128622008-10-17 18:09:11 +0200182 drive->dev_flags &= ~IDE_DFLAG_FORMAT_IN_PROGRESS;
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200183 return err;
184}
185
186/*
187 * Get ATAPI_FORMAT_UNIT progress indication.
188 *
189 * Userland gives a pointer to an int. The int is set to a progress
190 * indicator 0-65536, with 65536=100%.
191 *
192 * If the drive does not support format progress indication, we just check
193 * the dsc bit, and return either 0 or 65536.
194 */
195
Linus Torvalds07bd3f42009-01-02 16:12:51 +0100196static int ide_floppy_get_format_progress(ide_drive_t *drive,
197 struct ide_atapi_pc *pc,
198 int __user *arg)
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200199{
Bartlomiej Zolnierkiewicz0df96272008-10-17 18:09:16 +0200200 struct ide_disk_obj *floppy = drive->driver_data;
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200201 int progress_indication = 0x10000;
202
203 if (drive->atapi_flags & IDE_AFLAG_SRFP) {
Linus Torvalds07bd3f42009-01-02 16:12:51 +0100204 ide_create_request_sense_cmd(drive, pc);
Borislav Petkovb13345f2009-05-02 10:26:12 +0200205 if (ide_queue_pc_tail(drive, floppy->disk, pc, pc->buf,
206 pc->req_xfer))
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200207 return -EIO;
208
209 if (floppy->sense_key == 2 &&
210 floppy->asc == 4 &&
211 floppy->ascq == 4)
212 progress_indication = floppy->progress_indication;
213
214 /* Else assume format_unit has finished, and we're at 0x10000 */
215 } else {
216 ide_hwif_t *hwif = drive->hwif;
217 unsigned long flags;
218 u8 stat;
219
220 local_irq_save(flags);
221 stat = hwif->tp_ops->read_status(hwif);
222 local_irq_restore(flags);
223
224 progress_indication = ((stat & ATA_DSC) == 0) ? 0 : 0x10000;
225 }
226
227 if (put_user(progress_indication, arg))
228 return -EFAULT;
229
230 return 0;
231}
232
Bartlomiej Zolnierkiewicz5bb15362008-10-13 21:39:44 +0200233static int ide_floppy_lockdoor(ide_drive_t *drive, struct ide_atapi_pc *pc,
234 unsigned long arg, unsigned int cmd)
235{
Bartlomiej Zolnierkiewicz0df96272008-10-17 18:09:16 +0200236 struct ide_disk_obj *floppy = drive->driver_data;
Bartlomiej Zolnierkiewicz5bb15362008-10-13 21:39:44 +0200237 struct gendisk *disk = floppy->disk;
238 int prevent = (arg && cmd != CDROMEJECT) ? 1 : 0;
239
240 if (floppy->openers > 1)
241 return -EBUSY;
242
243 ide_set_media_lock(drive, disk, prevent);
244
245 if (cmd == CDROMEJECT)
246 ide_do_start_stop(drive, disk, 2);
247
248 return 0;
249}
250
Linus Torvalds07bd3f42009-01-02 16:12:51 +0100251static int ide_floppy_format_ioctl(ide_drive_t *drive, struct ide_atapi_pc *pc,
252 fmode_t mode, unsigned int cmd,
253 void __user *argp)
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200254{
255 switch (cmd) {
256 case IDEFLOPPY_IOCTL_FORMAT_SUPPORTED:
257 return 0;
258 case IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY:
Linus Torvalds07bd3f42009-01-02 16:12:51 +0100259 return ide_floppy_get_format_capacities(drive, pc, argp);
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200260 case IDEFLOPPY_IOCTL_FORMAT_START:
Al Virobadf8082008-10-16 10:23:20 -0400261 if (!(mode & FMODE_WRITE))
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200262 return -EPERM;
Linus Torvalds07bd3f42009-01-02 16:12:51 +0100263 return ide_floppy_format_unit(drive, pc, (int __user *)argp);
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200264 case IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS:
Linus Torvalds07bd3f42009-01-02 16:12:51 +0100265 return ide_floppy_get_format_progress(drive, pc, argp);
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200266 default:
267 return -ENOTTY;
268 }
269}
Bartlomiej Zolnierkiewicz5bb15362008-10-13 21:39:44 +0200270
Al Virobadf8082008-10-16 10:23:20 -0400271int ide_floppy_ioctl(ide_drive_t *drive, struct block_device *bdev,
272 fmode_t mode, unsigned int cmd, unsigned long arg)
Bartlomiej Zolnierkiewicz5bb15362008-10-13 21:39:44 +0200273{
Bartlomiej Zolnierkiewicz5bb15362008-10-13 21:39:44 +0200274 struct ide_atapi_pc pc;
275 void __user *argp = (void __user *)arg;
276 int err;
277
278 if (cmd == CDROMEJECT || cmd == CDROM_LOCKDOOR)
279 return ide_floppy_lockdoor(drive, &pc, arg, cmd);
280
Linus Torvalds07bd3f42009-01-02 16:12:51 +0100281 err = ide_floppy_format_ioctl(drive, &pc, mode, cmd, argp);
Bartlomiej Zolnierkiewicz5bb15362008-10-13 21:39:44 +0200282 if (err != -ENOTTY)
283 return err;
284
285 /*
286 * skip SCSI_IOCTL_SEND_COMMAND (deprecated)
287 * and CDROM_SEND_PACKET (legacy) ioctls
288 */
289 if (cmd != CDROM_SEND_PACKET && cmd != SCSI_IOCTL_SEND_COMMAND)
Al Viro74f3c8a2007-08-27 15:38:10 -0400290 err = scsi_cmd_ioctl(bdev->bd_disk->queue, bdev->bd_disk,
Al Virobadf8082008-10-16 10:23:20 -0400291 mode, cmd, argp);
Bartlomiej Zolnierkiewicz5bb15362008-10-13 21:39:44 +0200292
293 if (err == -ENOTTY)
Al Viro1bddd9e2008-09-02 17:19:43 -0400294 err = generic_ide_ioctl(drive, bdev, cmd, arg);
Bartlomiej Zolnierkiewicz5bb15362008-10-13 21:39:44 +0200295
296 return err;
297}