blob: 8f8be8546038d5950d5e944e26871fc7f0186a49 [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 u8 header_len, desc_cnt;
40 int i, blocks, length, u_array_size, u_index;
41 int __user *argp;
42
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);
50 if (ide_queue_pc_tail(drive, floppy->disk, pc)) {
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +020051 printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
52 return -EIO;
53 }
54
Linus Torvalds07bd3f42009-01-02 16:12:51 +010055 header_len = pc->buf[3];
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +020056 desc_cnt = header_len / 8; /* capacity descriptor of 8 bytes */
57
58 u_index = 0;
59 argp = arg + 1;
60
61 /*
62 * We always skip the first capacity descriptor. That's the current
63 * capacity. We are interested in the remaining descriptors, the
64 * formattable capacities.
65 */
66 for (i = 1; i < desc_cnt; i++) {
67 unsigned int desc_start = 4 + i*8;
68
69 if (u_index >= u_array_size)
70 break; /* User-supplied buffer too small */
71
Linus Torvalds07bd3f42009-01-02 16:12:51 +010072 blocks = be32_to_cpup((__be32 *)&pc->buf[desc_start]);
73 length = be16_to_cpup((__be16 *)&pc->buf[desc_start + 6]);
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +020074
75 if (put_user(blocks, argp))
76 return -EFAULT;
77
78 ++argp;
79
80 if (put_user(length, argp))
81 return -EFAULT;
82
83 ++argp;
84
85 ++u_index;
86 }
87
88 if (put_user(u_index, arg))
89 return -EFAULT;
90
91 return 0;
92}
93
94static void ide_floppy_create_format_unit_cmd(struct ide_atapi_pc *pc, int b,
95 int l, int flags)
96{
97 ide_init_pc(pc);
98 pc->c[0] = GPCMD_FORMAT_UNIT;
99 pc->c[1] = 0x17;
100
101 memset(pc->buf, 0, 12);
102 pc->buf[1] = 0xA2;
103 /* Default format list header, u8 1: FOV/DCRT/IMM bits set */
104
105 if (flags & 1) /* Verify bit on... */
106 pc->buf[1] ^= 0x20; /* ... turn off DCRT bit */
107 pc->buf[3] = 8;
108
109 put_unaligned(cpu_to_be32(b), (unsigned int *)(&pc->buf[4]));
110 put_unaligned(cpu_to_be32(l), (unsigned int *)(&pc->buf[8]));
111 pc->buf_size = 12;
112 pc->flags |= PC_FLAG_WRITING;
113}
114
Linus Torvalds07bd3f42009-01-02 16:12:51 +0100115static int ide_floppy_get_sfrp_bit(ide_drive_t *drive, struct ide_atapi_pc *pc)
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200116{
Bartlomiej Zolnierkiewicz0df96272008-10-17 18:09:16 +0200117 struct ide_disk_obj *floppy = drive->driver_data;
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200118
119 drive->atapi_flags &= ~IDE_AFLAG_SRFP;
120
Linus Torvalds07bd3f42009-01-02 16:12:51 +0100121 ide_floppy_create_mode_sense_cmd(pc, IDEFLOPPY_CAPABILITIES_PAGE);
122 pc->flags |= PC_FLAG_SUPPRESS_ERROR;
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200123
Linus Torvalds07bd3f42009-01-02 16:12:51 +0100124 if (ide_queue_pc_tail(drive, floppy->disk, pc))
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200125 return 1;
126
Linus Torvalds07bd3f42009-01-02 16:12:51 +0100127 if (pc->buf[8 + 2] & 0x40)
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200128 drive->atapi_flags |= IDE_AFLAG_SRFP;
129
130 return 0;
131}
132
Linus Torvalds07bd3f42009-01-02 16:12:51 +0100133static int ide_floppy_format_unit(ide_drive_t *drive, struct ide_atapi_pc *pc,
134 int __user *arg)
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200135{
Bartlomiej Zolnierkiewicz0df96272008-10-17 18:09:16 +0200136 struct ide_disk_obj *floppy = drive->driver_data;
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200137 int blocks, length, flags, err = 0;
138
139 if (floppy->openers > 1) {
140 /* Don't format if someone is using the disk */
Bartlomiej Zolnierkiewicze0128622008-10-17 18:09:11 +0200141 drive->dev_flags &= ~IDE_DFLAG_FORMAT_IN_PROGRESS;
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200142 return -EBUSY;
143 }
144
Bartlomiej Zolnierkiewicze0128622008-10-17 18:09:11 +0200145 drive->dev_flags |= IDE_DFLAG_FORMAT_IN_PROGRESS;
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200146
147 /*
148 * Send ATAPI_FORMAT_UNIT to the drive.
149 *
150 * Userland gives us the following structure:
151 *
152 * struct idefloppy_format_command {
153 * int nblocks;
154 * int blocksize;
155 * int flags;
156 * } ;
157 *
158 * flags is a bitmask, currently, the only defined flag is:
159 *
160 * 0x01 - verify media after format.
161 */
162 if (get_user(blocks, arg) ||
163 get_user(length, arg+1) ||
164 get_user(flags, arg+2)) {
165 err = -EFAULT;
166 goto out;
167 }
168
Linus Torvalds07bd3f42009-01-02 16:12:51 +0100169 ide_floppy_get_sfrp_bit(drive, pc);
170 ide_floppy_create_format_unit_cmd(pc, blocks, length, flags);
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200171
Linus Torvalds07bd3f42009-01-02 16:12:51 +0100172 if (ide_queue_pc_tail(drive, floppy->disk, pc))
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200173 err = -EIO;
174
175out:
176 if (err)
Bartlomiej Zolnierkiewicze0128622008-10-17 18:09:11 +0200177 drive->dev_flags &= ~IDE_DFLAG_FORMAT_IN_PROGRESS;
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200178 return err;
179}
180
181/*
182 * Get ATAPI_FORMAT_UNIT progress indication.
183 *
184 * Userland gives a pointer to an int. The int is set to a progress
185 * indicator 0-65536, with 65536=100%.
186 *
187 * If the drive does not support format progress indication, we just check
188 * the dsc bit, and return either 0 or 65536.
189 */
190
Linus Torvalds07bd3f42009-01-02 16:12:51 +0100191static int ide_floppy_get_format_progress(ide_drive_t *drive,
192 struct ide_atapi_pc *pc,
193 int __user *arg)
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200194{
Bartlomiej Zolnierkiewicz0df96272008-10-17 18:09:16 +0200195 struct ide_disk_obj *floppy = drive->driver_data;
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200196 int progress_indication = 0x10000;
197
198 if (drive->atapi_flags & IDE_AFLAG_SRFP) {
Linus Torvalds07bd3f42009-01-02 16:12:51 +0100199 ide_create_request_sense_cmd(drive, pc);
200 if (ide_queue_pc_tail(drive, floppy->disk, pc))
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200201 return -EIO;
202
203 if (floppy->sense_key == 2 &&
204 floppy->asc == 4 &&
205 floppy->ascq == 4)
206 progress_indication = floppy->progress_indication;
207
208 /* Else assume format_unit has finished, and we're at 0x10000 */
209 } else {
210 ide_hwif_t *hwif = drive->hwif;
211 unsigned long flags;
212 u8 stat;
213
214 local_irq_save(flags);
215 stat = hwif->tp_ops->read_status(hwif);
216 local_irq_restore(flags);
217
218 progress_indication = ((stat & ATA_DSC) == 0) ? 0 : 0x10000;
219 }
220
221 if (put_user(progress_indication, arg))
222 return -EFAULT;
223
224 return 0;
225}
226
Bartlomiej Zolnierkiewicz5bb15362008-10-13 21:39:44 +0200227static int ide_floppy_lockdoor(ide_drive_t *drive, struct ide_atapi_pc *pc,
228 unsigned long arg, unsigned int cmd)
229{
Bartlomiej Zolnierkiewicz0df96272008-10-17 18:09:16 +0200230 struct ide_disk_obj *floppy = drive->driver_data;
Bartlomiej Zolnierkiewicz5bb15362008-10-13 21:39:44 +0200231 struct gendisk *disk = floppy->disk;
232 int prevent = (arg && cmd != CDROMEJECT) ? 1 : 0;
233
234 if (floppy->openers > 1)
235 return -EBUSY;
236
237 ide_set_media_lock(drive, disk, prevent);
238
239 if (cmd == CDROMEJECT)
240 ide_do_start_stop(drive, disk, 2);
241
242 return 0;
243}
244
Linus Torvalds07bd3f42009-01-02 16:12:51 +0100245static int ide_floppy_format_ioctl(ide_drive_t *drive, struct ide_atapi_pc *pc,
246 fmode_t mode, unsigned int cmd,
247 void __user *argp)
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200248{
249 switch (cmd) {
250 case IDEFLOPPY_IOCTL_FORMAT_SUPPORTED:
251 return 0;
252 case IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY:
Linus Torvalds07bd3f42009-01-02 16:12:51 +0100253 return ide_floppy_get_format_capacities(drive, pc, argp);
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200254 case IDEFLOPPY_IOCTL_FORMAT_START:
Al Virobadf8082008-10-16 10:23:20 -0400255 if (!(mode & FMODE_WRITE))
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200256 return -EPERM;
Linus Torvalds07bd3f42009-01-02 16:12:51 +0100257 return ide_floppy_format_unit(drive, pc, (int __user *)argp);
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200258 case IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS:
Linus Torvalds07bd3f42009-01-02 16:12:51 +0100259 return ide_floppy_get_format_progress(drive, pc, argp);
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200260 default:
261 return -ENOTTY;
262 }
263}
Bartlomiej Zolnierkiewicz5bb15362008-10-13 21:39:44 +0200264
Al Virobadf8082008-10-16 10:23:20 -0400265int ide_floppy_ioctl(ide_drive_t *drive, struct block_device *bdev,
266 fmode_t mode, unsigned int cmd, unsigned long arg)
Bartlomiej Zolnierkiewicz5bb15362008-10-13 21:39:44 +0200267{
Bartlomiej Zolnierkiewicz5bb15362008-10-13 21:39:44 +0200268 struct ide_atapi_pc pc;
269 void __user *argp = (void __user *)arg;
270 int err;
271
272 if (cmd == CDROMEJECT || cmd == CDROM_LOCKDOOR)
273 return ide_floppy_lockdoor(drive, &pc, arg, cmd);
274
Linus Torvalds07bd3f42009-01-02 16:12:51 +0100275 err = ide_floppy_format_ioctl(drive, &pc, mode, cmd, argp);
Bartlomiej Zolnierkiewicz5bb15362008-10-13 21:39:44 +0200276 if (err != -ENOTTY)
277 return err;
278
279 /*
280 * skip SCSI_IOCTL_SEND_COMMAND (deprecated)
281 * and CDROM_SEND_PACKET (legacy) ioctls
282 */
283 if (cmd != CDROM_SEND_PACKET && cmd != SCSI_IOCTL_SEND_COMMAND)
Al Viro74f3c8a2007-08-27 15:38:10 -0400284 err = scsi_cmd_ioctl(bdev->bd_disk->queue, bdev->bd_disk,
Al Virobadf8082008-10-16 10:23:20 -0400285 mode, cmd, argp);
Bartlomiej Zolnierkiewicz5bb15362008-10-13 21:39:44 +0200286
287 if (err == -ENOTTY)
Al Viro1bddd9e2008-09-02 17:19:43 -0400288 err = generic_ide_ioctl(drive, bdev, cmd, arg);
Bartlomiej Zolnierkiewicz5bb15362008-10-13 21:39:44 +0200289
290 return err;
291}