blob: 0d2c9f0368dacba57ea3e691aaf1950d29f199b3 [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
96static void ide_floppy_create_format_unit_cmd(struct ide_atapi_pc *pc, int b,
97 int l, int flags)
98{
99 ide_init_pc(pc);
100 pc->c[0] = GPCMD_FORMAT_UNIT;
101 pc->c[1] = 0x17;
102
103 memset(pc->buf, 0, 12);
104 pc->buf[1] = 0xA2;
105 /* Default format list header, u8 1: FOV/DCRT/IMM bits set */
106
107 if (flags & 1) /* Verify bit on... */
108 pc->buf[1] ^= 0x20; /* ... turn off DCRT bit */
109 pc->buf[3] = 8;
110
111 put_unaligned(cpu_to_be32(b), (unsigned int *)(&pc->buf[4]));
112 put_unaligned(cpu_to_be32(l), (unsigned int *)(&pc->buf[8]));
113 pc->buf_size = 12;
114 pc->flags |= PC_FLAG_WRITING;
115}
116
Linus Torvalds07bd3f42009-01-02 16:12:51 +0100117static int ide_floppy_get_sfrp_bit(ide_drive_t *drive, struct ide_atapi_pc *pc)
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200118{
Bartlomiej Zolnierkiewicz0df96272008-10-17 18:09:16 +0200119 struct ide_disk_obj *floppy = drive->driver_data;
Borislav Petkov802e6632009-05-02 10:45:17 +0200120 u8 buf[20];
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200121
122 drive->atapi_flags &= ~IDE_AFLAG_SRFP;
123
Linus Torvalds07bd3f42009-01-02 16:12:51 +0100124 ide_floppy_create_mode_sense_cmd(pc, IDEFLOPPY_CAPABILITIES_PAGE);
125 pc->flags |= PC_FLAG_SUPPRESS_ERROR;
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200126
Borislav Petkov802e6632009-05-02 10:45:17 +0200127 if (ide_queue_pc_tail(drive, floppy->disk, pc, buf, pc->req_xfer))
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200128 return 1;
129
Borislav Petkov802e6632009-05-02 10:45:17 +0200130 if (buf[8 + 2] & 0x40)
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200131 drive->atapi_flags |= IDE_AFLAG_SRFP;
132
133 return 0;
134}
135
Linus Torvalds07bd3f42009-01-02 16:12:51 +0100136static int ide_floppy_format_unit(ide_drive_t *drive, struct ide_atapi_pc *pc,
137 int __user *arg)
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200138{
Bartlomiej Zolnierkiewicz0df96272008-10-17 18:09:16 +0200139 struct ide_disk_obj *floppy = drive->driver_data;
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200140 int blocks, length, flags, err = 0;
141
142 if (floppy->openers > 1) {
143 /* Don't format if someone is using the disk */
Bartlomiej Zolnierkiewicze0128622008-10-17 18:09:11 +0200144 drive->dev_flags &= ~IDE_DFLAG_FORMAT_IN_PROGRESS;
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200145 return -EBUSY;
146 }
147
Bartlomiej Zolnierkiewicze0128622008-10-17 18:09:11 +0200148 drive->dev_flags |= IDE_DFLAG_FORMAT_IN_PROGRESS;
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200149
150 /*
151 * Send ATAPI_FORMAT_UNIT to the drive.
152 *
153 * Userland gives us the following structure:
154 *
155 * struct idefloppy_format_command {
156 * int nblocks;
157 * int blocksize;
158 * int flags;
159 * } ;
160 *
161 * flags is a bitmask, currently, the only defined flag is:
162 *
163 * 0x01 - verify media after format.
164 */
165 if (get_user(blocks, arg) ||
166 get_user(length, arg+1) ||
167 get_user(flags, arg+2)) {
168 err = -EFAULT;
169 goto out;
170 }
171
Linus Torvalds07bd3f42009-01-02 16:12:51 +0100172 ide_floppy_get_sfrp_bit(drive, pc);
173 ide_floppy_create_format_unit_cmd(pc, blocks, length, flags);
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200174
Borislav Petkovb13345f2009-05-02 10:26:12 +0200175 if (ide_queue_pc_tail(drive, floppy->disk, pc, pc->buf, pc->req_xfer))
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200176 err = -EIO;
177
178out:
179 if (err)
Bartlomiej Zolnierkiewicze0128622008-10-17 18:09:11 +0200180 drive->dev_flags &= ~IDE_DFLAG_FORMAT_IN_PROGRESS;
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200181 return err;
182}
183
184/*
185 * Get ATAPI_FORMAT_UNIT progress indication.
186 *
187 * Userland gives a pointer to an int. The int is set to a progress
188 * indicator 0-65536, with 65536=100%.
189 *
190 * If the drive does not support format progress indication, we just check
191 * the dsc bit, and return either 0 or 65536.
192 */
193
Linus Torvalds07bd3f42009-01-02 16:12:51 +0100194static int ide_floppy_get_format_progress(ide_drive_t *drive,
195 struct ide_atapi_pc *pc,
196 int __user *arg)
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200197{
Bartlomiej Zolnierkiewicz0df96272008-10-17 18:09:16 +0200198 struct ide_disk_obj *floppy = drive->driver_data;
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200199 int progress_indication = 0x10000;
200
201 if (drive->atapi_flags & IDE_AFLAG_SRFP) {
Linus Torvalds07bd3f42009-01-02 16:12:51 +0100202 ide_create_request_sense_cmd(drive, pc);
Borislav Petkovb13345f2009-05-02 10:26:12 +0200203 if (ide_queue_pc_tail(drive, floppy->disk, pc, pc->buf,
204 pc->req_xfer))
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200205 return -EIO;
206
207 if (floppy->sense_key == 2 &&
208 floppy->asc == 4 &&
209 floppy->ascq == 4)
210 progress_indication = floppy->progress_indication;
211
212 /* Else assume format_unit has finished, and we're at 0x10000 */
213 } else {
214 ide_hwif_t *hwif = drive->hwif;
215 unsigned long flags;
216 u8 stat;
217
218 local_irq_save(flags);
219 stat = hwif->tp_ops->read_status(hwif);
220 local_irq_restore(flags);
221
222 progress_indication = ((stat & ATA_DSC) == 0) ? 0 : 0x10000;
223 }
224
225 if (put_user(progress_indication, arg))
226 return -EFAULT;
227
228 return 0;
229}
230
Bartlomiej Zolnierkiewicz5bb15362008-10-13 21:39:44 +0200231static int ide_floppy_lockdoor(ide_drive_t *drive, struct ide_atapi_pc *pc,
232 unsigned long arg, unsigned int cmd)
233{
Bartlomiej Zolnierkiewicz0df96272008-10-17 18:09:16 +0200234 struct ide_disk_obj *floppy = drive->driver_data;
Bartlomiej Zolnierkiewicz5bb15362008-10-13 21:39:44 +0200235 struct gendisk *disk = floppy->disk;
236 int prevent = (arg && cmd != CDROMEJECT) ? 1 : 0;
237
238 if (floppy->openers > 1)
239 return -EBUSY;
240
241 ide_set_media_lock(drive, disk, prevent);
242
243 if (cmd == CDROMEJECT)
244 ide_do_start_stop(drive, disk, 2);
245
246 return 0;
247}
248
Linus Torvalds07bd3f42009-01-02 16:12:51 +0100249static int ide_floppy_format_ioctl(ide_drive_t *drive, struct ide_atapi_pc *pc,
250 fmode_t mode, unsigned int cmd,
251 void __user *argp)
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200252{
253 switch (cmd) {
254 case IDEFLOPPY_IOCTL_FORMAT_SUPPORTED:
255 return 0;
256 case IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY:
Linus Torvalds07bd3f42009-01-02 16:12:51 +0100257 return ide_floppy_get_format_capacities(drive, pc, argp);
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200258 case IDEFLOPPY_IOCTL_FORMAT_START:
Al Virobadf8082008-10-16 10:23:20 -0400259 if (!(mode & FMODE_WRITE))
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200260 return -EPERM;
Linus Torvalds07bd3f42009-01-02 16:12:51 +0100261 return ide_floppy_format_unit(drive, pc, (int __user *)argp);
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200262 case IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS:
Linus Torvalds07bd3f42009-01-02 16:12:51 +0100263 return ide_floppy_get_format_progress(drive, pc, argp);
Bartlomiej Zolnierkiewicz01278542008-10-10 22:39:38 +0200264 default:
265 return -ENOTTY;
266 }
267}
Bartlomiej Zolnierkiewicz5bb15362008-10-13 21:39:44 +0200268
Al Virobadf8082008-10-16 10:23:20 -0400269int ide_floppy_ioctl(ide_drive_t *drive, struct block_device *bdev,
270 fmode_t mode, unsigned int cmd, unsigned long arg)
Bartlomiej Zolnierkiewicz5bb15362008-10-13 21:39:44 +0200271{
Bartlomiej Zolnierkiewicz5bb15362008-10-13 21:39:44 +0200272 struct ide_atapi_pc pc;
273 void __user *argp = (void __user *)arg;
274 int err;
275
276 if (cmd == CDROMEJECT || cmd == CDROM_LOCKDOOR)
277 return ide_floppy_lockdoor(drive, &pc, arg, cmd);
278
Linus Torvalds07bd3f42009-01-02 16:12:51 +0100279 err = ide_floppy_format_ioctl(drive, &pc, mode, cmd, argp);
Bartlomiej Zolnierkiewicz5bb15362008-10-13 21:39:44 +0200280 if (err != -ENOTTY)
281 return err;
282
283 /*
284 * skip SCSI_IOCTL_SEND_COMMAND (deprecated)
285 * and CDROM_SEND_PACKET (legacy) ioctls
286 */
287 if (cmd != CDROM_SEND_PACKET && cmd != SCSI_IOCTL_SEND_COMMAND)
Al Viro74f3c8a2007-08-27 15:38:10 -0400288 err = scsi_cmd_ioctl(bdev->bd_disk->queue, bdev->bd_disk,
Al Virobadf8082008-10-16 10:23:20 -0400289 mode, cmd, argp);
Bartlomiej Zolnierkiewicz5bb15362008-10-13 21:39:44 +0200290
291 if (err == -ENOTTY)
Al Viro1bddd9e2008-09-02 17:19:43 -0400292 err = generic_ide_ioctl(drive, bdev, cmd, arg);
Bartlomiej Zolnierkiewicz5bb15362008-10-13 21:39:44 +0200293
294 return err;
295}