Merge git://git.kernel.org/pub/scm/linux/kernel/git/mason/btrfs-unstable
[linux-2.6/mini2440.git] / drivers / ide / ide-floppy_ioctl.c
blobcd8a42027ede72f10c0acd0a96e7c0fb6ad2e4f6
1 /*
2 * ide-floppy IOCTLs handling.
3 */
5 #include <linux/kernel.h>
6 #include <linux/ide.h>
7 #include <linux/cdrom.h>
9 #include <asm/unaligned.h>
11 #include <scsi/scsi_ioctl.h>
13 #include "ide-floppy.h"
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.
20 * Userland gives us the following structure:
22 * struct idefloppy_format_capacities {
23 * int nformats;
24 * struct {
25 * int nblocks;
26 * int blocksize;
27 * } formats[];
28 * };
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.
34 static int ide_floppy_get_format_capacities(ide_drive_t *drive,
35 struct ide_atapi_pc *pc,
36 int __user *arg)
38 struct ide_disk_obj *floppy = drive->driver_data;
39 int i, blocks, length, u_array_size, u_index;
40 int __user *argp;
41 u8 pc_buf[256], header_len, desc_cnt;
43 if (get_user(u_array_size, arg))
44 return -EFAULT;
46 if (u_array_size <= 0)
47 return -EINVAL;
49 ide_floppy_create_read_capacity_cmd(pc);
50 pc->buf = &pc_buf[0];
51 pc->buf_size = sizeof(pc_buf);
53 if (ide_queue_pc_tail(drive, floppy->disk, pc)) {
54 printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
55 return -EIO;
58 header_len = pc->buf[3];
59 desc_cnt = header_len / 8; /* capacity descriptor of 8 bytes */
61 u_index = 0;
62 argp = arg + 1;
65 * We always skip the first capacity descriptor. That's the current
66 * capacity. We are interested in the remaining descriptors, the
67 * formattable capacities.
69 for (i = 1; i < desc_cnt; i++) {
70 unsigned int desc_start = 4 + i*8;
72 if (u_index >= u_array_size)
73 break; /* User-supplied buffer too small */
75 blocks = be32_to_cpup((__be32 *)&pc->buf[desc_start]);
76 length = be16_to_cpup((__be16 *)&pc->buf[desc_start + 6]);
78 if (put_user(blocks, argp))
79 return -EFAULT;
81 ++argp;
83 if (put_user(length, argp))
84 return -EFAULT;
86 ++argp;
88 ++u_index;
91 if (put_user(u_index, arg))
92 return -EFAULT;
94 return 0;
97 static void ide_floppy_create_format_unit_cmd(struct ide_atapi_pc *pc, int b,
98 int l, int flags)
100 ide_init_pc(pc);
101 pc->c[0] = GPCMD_FORMAT_UNIT;
102 pc->c[1] = 0x17;
104 memset(pc->buf, 0, 12);
105 pc->buf[1] = 0xA2;
106 /* Default format list header, u8 1: FOV/DCRT/IMM bits set */
108 if (flags & 1) /* Verify bit on... */
109 pc->buf[1] ^= 0x20; /* ... turn off DCRT bit */
110 pc->buf[3] = 8;
112 put_unaligned(cpu_to_be32(b), (unsigned int *)(&pc->buf[4]));
113 put_unaligned(cpu_to_be32(l), (unsigned int *)(&pc->buf[8]));
114 pc->buf_size = 12;
115 pc->flags |= PC_FLAG_WRITING;
118 static int ide_floppy_get_sfrp_bit(ide_drive_t *drive, struct ide_atapi_pc *pc)
120 struct ide_disk_obj *floppy = drive->driver_data;
122 drive->atapi_flags &= ~IDE_AFLAG_SRFP;
124 ide_floppy_create_mode_sense_cmd(pc, IDEFLOPPY_CAPABILITIES_PAGE);
125 pc->flags |= PC_FLAG_SUPPRESS_ERROR;
127 if (ide_queue_pc_tail(drive, floppy->disk, pc))
128 return 1;
130 if (pc->buf[8 + 2] & 0x40)
131 drive->atapi_flags |= IDE_AFLAG_SRFP;
133 return 0;
136 static int ide_floppy_format_unit(ide_drive_t *drive, struct ide_atapi_pc *pc,
137 int __user *arg)
139 struct ide_disk_obj *floppy = drive->driver_data;
140 int blocks, length, flags, err = 0;
142 if (floppy->openers > 1) {
143 /* Don't format if someone is using the disk */
144 drive->dev_flags &= ~IDE_DFLAG_FORMAT_IN_PROGRESS;
145 return -EBUSY;
148 drive->dev_flags |= IDE_DFLAG_FORMAT_IN_PROGRESS;
151 * Send ATAPI_FORMAT_UNIT to the drive.
153 * Userland gives us the following structure:
155 * struct idefloppy_format_command {
156 * int nblocks;
157 * int blocksize;
158 * int flags;
159 * } ;
161 * flags is a bitmask, currently, the only defined flag is:
163 * 0x01 - verify media after format.
165 if (get_user(blocks, arg) ||
166 get_user(length, arg+1) ||
167 get_user(flags, arg+2)) {
168 err = -EFAULT;
169 goto out;
172 ide_floppy_get_sfrp_bit(drive, pc);
173 ide_floppy_create_format_unit_cmd(pc, blocks, length, flags);
175 if (ide_queue_pc_tail(drive, floppy->disk, pc))
176 err = -EIO;
178 out:
179 if (err)
180 drive->dev_flags &= ~IDE_DFLAG_FORMAT_IN_PROGRESS;
181 return err;
185 * Get ATAPI_FORMAT_UNIT progress indication.
187 * Userland gives a pointer to an int. The int is set to a progress
188 * indicator 0-65536, with 65536=100%.
190 * If the drive does not support format progress indication, we just check
191 * the dsc bit, and return either 0 or 65536.
194 static int ide_floppy_get_format_progress(ide_drive_t *drive,
195 struct ide_atapi_pc *pc,
196 int __user *arg)
198 struct ide_disk_obj *floppy = drive->driver_data;
199 int progress_indication = 0x10000;
201 if (drive->atapi_flags & IDE_AFLAG_SRFP) {
202 ide_create_request_sense_cmd(drive, pc);
203 if (ide_queue_pc_tail(drive, floppy->disk, pc))
204 return -EIO;
206 if (floppy->sense_key == 2 &&
207 floppy->asc == 4 &&
208 floppy->ascq == 4)
209 progress_indication = floppy->progress_indication;
211 /* Else assume format_unit has finished, and we're at 0x10000 */
212 } else {
213 ide_hwif_t *hwif = drive->hwif;
214 unsigned long flags;
215 u8 stat;
217 local_irq_save(flags);
218 stat = hwif->tp_ops->read_status(hwif);
219 local_irq_restore(flags);
221 progress_indication = ((stat & ATA_DSC) == 0) ? 0 : 0x10000;
224 if (put_user(progress_indication, arg))
225 return -EFAULT;
227 return 0;
230 static int ide_floppy_lockdoor(ide_drive_t *drive, struct ide_atapi_pc *pc,
231 unsigned long arg, unsigned int cmd)
233 struct ide_disk_obj *floppy = drive->driver_data;
234 struct gendisk *disk = floppy->disk;
235 int prevent = (arg && cmd != CDROMEJECT) ? 1 : 0;
237 if (floppy->openers > 1)
238 return -EBUSY;
240 ide_set_media_lock(drive, disk, prevent);
242 if (cmd == CDROMEJECT)
243 ide_do_start_stop(drive, disk, 2);
245 return 0;
248 static int ide_floppy_format_ioctl(ide_drive_t *drive, struct ide_atapi_pc *pc,
249 fmode_t mode, unsigned int cmd,
250 void __user *argp)
252 switch (cmd) {
253 case IDEFLOPPY_IOCTL_FORMAT_SUPPORTED:
254 return 0;
255 case IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY:
256 return ide_floppy_get_format_capacities(drive, pc, argp);
257 case IDEFLOPPY_IOCTL_FORMAT_START:
258 if (!(mode & FMODE_WRITE))
259 return -EPERM;
260 return ide_floppy_format_unit(drive, pc, (int __user *)argp);
261 case IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS:
262 return ide_floppy_get_format_progress(drive, pc, argp);
263 default:
264 return -ENOTTY;
268 int ide_floppy_ioctl(ide_drive_t *drive, struct block_device *bdev,
269 fmode_t mode, unsigned int cmd, unsigned long arg)
271 struct ide_atapi_pc pc;
272 void __user *argp = (void __user *)arg;
273 int err;
275 if (cmd == CDROMEJECT || cmd == CDROM_LOCKDOOR)
276 return ide_floppy_lockdoor(drive, &pc, arg, cmd);
278 err = ide_floppy_format_ioctl(drive, &pc, mode, cmd, argp);
279 if (err != -ENOTTY)
280 return err;
283 * skip SCSI_IOCTL_SEND_COMMAND (deprecated)
284 * and CDROM_SEND_PACKET (legacy) ioctls
286 if (cmd != CDROM_SEND_PACKET && cmd != SCSI_IOCTL_SEND_COMMAND)
287 err = scsi_cmd_ioctl(bdev->bd_disk->queue, bdev->bd_disk,
288 mode, cmd, argp);
290 if (err == -ENOTTY)
291 err = generic_ide_ioctl(drive, bdev, cmd, arg);
293 return err;