]> Pileus Git - ~andy/linux/blob - drivers/ide/ide-floppy_ioctl.c
ide-atapi: add a buffer-arg to ide_queue_pc_tail
[~andy/linux] / drivers / ide / ide-floppy_ioctl.c
1 /*
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
34 static int ide_floppy_get_format_capacities(ide_drive_t *drive,
35                                             struct ide_atapi_pc *pc,
36                                             int __user *arg)
37 {
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;
42
43         if (get_user(u_array_size, arg))
44                 return -EFAULT;
45
46         if (u_array_size <= 0)
47                 return -EINVAL;
48
49         ide_floppy_create_read_capacity_cmd(pc);
50         pc->buf_size = sizeof(pc_buf);
51
52         if (ide_queue_pc_tail(drive, floppy->disk, pc, pc_buf, pc->req_xfer)) {
53                 printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
54                 return -EIO;
55         }
56
57         header_len = pc_buf[3];
58         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
74                 blocks = be32_to_cpup((__be32 *)&pc_buf[desc_start]);
75                 length = be16_to_cpup((__be16 *)&pc_buf[desc_start + 6]);
76
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
96 static 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
117 static int ide_floppy_get_sfrp_bit(ide_drive_t *drive, struct ide_atapi_pc *pc)
118 {
119         struct ide_disk_obj *floppy = drive->driver_data;
120
121         drive->atapi_flags &= ~IDE_AFLAG_SRFP;
122
123         ide_floppy_create_mode_sense_cmd(pc, IDEFLOPPY_CAPABILITIES_PAGE);
124         pc->flags |= PC_FLAG_SUPPRESS_ERROR;
125
126         if (ide_queue_pc_tail(drive, floppy->disk, pc, pc->buf, pc->req_xfer))
127                 return 1;
128
129         if (pc->buf[8 + 2] & 0x40)
130                 drive->atapi_flags |= IDE_AFLAG_SRFP;
131
132         return 0;
133 }
134
135 static int ide_floppy_format_unit(ide_drive_t *drive, struct ide_atapi_pc *pc,
136                                   int __user *arg)
137 {
138         struct ide_disk_obj *floppy = drive->driver_data;
139         int blocks, length, flags, err = 0;
140
141         if (floppy->openers > 1) {
142                 /* Don't format if someone is using the disk */
143                 drive->dev_flags &= ~IDE_DFLAG_FORMAT_IN_PROGRESS;
144                 return -EBUSY;
145         }
146
147         drive->dev_flags |= IDE_DFLAG_FORMAT_IN_PROGRESS;
148
149         /*
150          * Send ATAPI_FORMAT_UNIT to the drive.
151          *
152          * Userland gives us the following structure:
153          *
154          * struct idefloppy_format_command {
155          *        int nblocks;
156          *        int blocksize;
157          *        int flags;
158          *        } ;
159          *
160          * flags is a bitmask, currently, the only defined flag is:
161          *
162          *        0x01 - verify media after format.
163          */
164         if (get_user(blocks, arg) ||
165                         get_user(length, arg+1) ||
166                         get_user(flags, arg+2)) {
167                 err = -EFAULT;
168                 goto out;
169         }
170
171         ide_floppy_get_sfrp_bit(drive, pc);
172         ide_floppy_create_format_unit_cmd(pc, blocks, length, flags);
173
174         if (ide_queue_pc_tail(drive, floppy->disk, pc, pc->buf, pc->req_xfer))
175                 err = -EIO;
176
177 out:
178         if (err)
179                 drive->dev_flags &= ~IDE_DFLAG_FORMAT_IN_PROGRESS;
180         return err;
181 }
182
183 /*
184  * Get ATAPI_FORMAT_UNIT progress indication.
185  *
186  * Userland gives a pointer to an int.  The int is set to a progress
187  * indicator 0-65536, with 65536=100%.
188  *
189  * If the drive does not support format progress indication, we just check
190  * the dsc bit, and return either 0 or 65536.
191  */
192
193 static int ide_floppy_get_format_progress(ide_drive_t *drive,
194                                           struct ide_atapi_pc *pc,
195                                           int __user *arg)
196 {
197         struct ide_disk_obj *floppy = drive->driver_data;
198         int progress_indication = 0x10000;
199
200         if (drive->atapi_flags & IDE_AFLAG_SRFP) {
201                 ide_create_request_sense_cmd(drive, pc);
202                 if (ide_queue_pc_tail(drive, floppy->disk, pc, pc->buf,
203                                       pc->req_xfer))
204                         return -EIO;
205
206                 if (floppy->sense_key == 2 &&
207                     floppy->asc == 4 &&
208                     floppy->ascq == 4)
209                         progress_indication = floppy->progress_indication;
210
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;
216
217                 local_irq_save(flags);
218                 stat = hwif->tp_ops->read_status(hwif);
219                 local_irq_restore(flags);
220
221                 progress_indication = ((stat & ATA_DSC) == 0) ? 0 : 0x10000;
222         }
223
224         if (put_user(progress_indication, arg))
225                 return -EFAULT;
226
227         return 0;
228 }
229
230 static int ide_floppy_lockdoor(ide_drive_t *drive, struct ide_atapi_pc *pc,
231                                unsigned long arg, unsigned int cmd)
232 {
233         struct ide_disk_obj *floppy = drive->driver_data;
234         struct gendisk *disk = floppy->disk;
235         int prevent = (arg && cmd != CDROMEJECT) ? 1 : 0;
236
237         if (floppy->openers > 1)
238                 return -EBUSY;
239
240         ide_set_media_lock(drive, disk, prevent);
241
242         if (cmd == CDROMEJECT)
243                 ide_do_start_stop(drive, disk, 2);
244
245         return 0;
246 }
247
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)
251 {
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;
265         }
266 }
267
268 int ide_floppy_ioctl(ide_drive_t *drive, struct block_device *bdev,
269                      fmode_t mode, unsigned int cmd, unsigned long arg)
270 {
271         struct ide_atapi_pc pc;
272         void __user *argp = (void __user *)arg;
273         int err;
274
275         if (cmd == CDROMEJECT || cmd == CDROM_LOCKDOOR)
276                 return ide_floppy_lockdoor(drive, &pc, arg, cmd);
277
278         err = ide_floppy_format_ioctl(drive, &pc, mode, cmd, argp);
279         if (err != -ENOTTY)
280                 return err;
281
282         /*
283          * skip SCSI_IOCTL_SEND_COMMAND (deprecated)
284          * and CDROM_SEND_PACKET (legacy) ioctls
285          */
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);
289
290         if (err == -ENOTTY)
291                 err = generic_ide_ioctl(drive, bdev, cmd, arg);
292
293         return err;
294 }