Commit | Line | Data |
---|---|---|
0127854d BZ |
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 | ||
07bd3f47 LT |
34 | static int ide_floppy_get_format_capacities(ide_drive_t *drive, |
35 | struct ide_atapi_pc *pc, | |
36 | int __user *arg) | |
0127854d | 37 | { |
806f80a6 | 38 | struct ide_disk_obj *floppy = drive->driver_data; |
0127854d BZ |
39 | int i, blocks, length, u_array_size, u_index; |
40 | int __user *argp; | |
41fa9f86 | 41 | u8 pc_buf[256], header_len, desc_cnt; |
0127854d BZ |
42 | |
43 | if (get_user(u_array_size, arg)) | |
44 | return -EFAULT; | |
45 | ||
46 | if (u_array_size <= 0) | |
47 | return -EINVAL; | |
48 | ||
07bd3f47 | 49 | ide_floppy_create_read_capacity_cmd(pc); |
41fa9f86 | 50 | |
b13345f3 | 51 | if (ide_queue_pc_tail(drive, floppy->disk, pc, pc_buf, pc->req_xfer)) { |
0127854d BZ |
52 | printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n"); |
53 | return -EIO; | |
54 | } | |
55 | ||
b13345f3 | 56 | header_len = pc_buf[3]; |
0127854d BZ |
57 | desc_cnt = header_len / 8; /* capacity descriptor of 8 bytes */ |
58 | ||
59 | u_index = 0; | |
60 | argp = arg + 1; | |
61 | ||
62 | /* | |
63 | * We always skip the first capacity descriptor. That's the current | |
64 | * capacity. We are interested in the remaining descriptors, the | |
65 | * formattable capacities. | |
66 | */ | |
67 | for (i = 1; i < desc_cnt; i++) { | |
68 | unsigned int desc_start = 4 + i*8; | |
69 | ||
70 | if (u_index >= u_array_size) | |
71 | break; /* User-supplied buffer too small */ | |
72 | ||
b13345f3 BP |
73 | blocks = be32_to_cpup((__be32 *)&pc_buf[desc_start]); |
74 | length = be16_to_cpup((__be16 *)&pc_buf[desc_start + 6]); | |
0127854d BZ |
75 | |
76 | if (put_user(blocks, argp)) | |
77 | return -EFAULT; | |
78 | ||
79 | ++argp; | |
80 | ||
81 | if (put_user(length, argp)) | |
82 | return -EFAULT; | |
83 | ||
84 | ++argp; | |
85 | ||
86 | ++u_index; | |
87 | } | |
88 | ||
89 | if (put_user(u_index, arg)) | |
90 | return -EFAULT; | |
91 | ||
92 | return 0; | |
93 | } | |
94 | ||
5122e517 BP |
95 | static void ide_floppy_create_format_unit_cmd(struct ide_atapi_pc *pc, |
96 | u8 *buf, int b, int l, | |
97 | int flags) | |
0127854d BZ |
98 | { |
99 | ide_init_pc(pc); | |
100 | pc->c[0] = GPCMD_FORMAT_UNIT; | |
101 | pc->c[1] = 0x17; | |
102 | ||
5122e517 BP |
103 | memset(buf, 0, 12); |
104 | buf[1] = 0xA2; | |
0127854d BZ |
105 | /* Default format list header, u8 1: FOV/DCRT/IMM bits set */ |
106 | ||
107 | if (flags & 1) /* Verify bit on... */ | |
5122e517 BP |
108 | buf[1] ^= 0x20; /* ... turn off DCRT bit */ |
109 | buf[3] = 8; | |
0127854d | 110 | |
5122e517 BP |
111 | put_unaligned(cpu_to_be32(b), (unsigned int *)(&buf[4])); |
112 | put_unaligned(cpu_to_be32(l), (unsigned int *)(&buf[8])); | |
113 | pc->req_xfer = 12; | |
0127854d BZ |
114 | pc->flags |= PC_FLAG_WRITING; |
115 | } | |
116 | ||
07bd3f47 | 117 | static int ide_floppy_get_sfrp_bit(ide_drive_t *drive, struct ide_atapi_pc *pc) |
0127854d | 118 | { |
0df96277 | 119 | struct ide_disk_obj *floppy = drive->driver_data; |
802e6634 | 120 | u8 buf[20]; |
0127854d BZ |
121 | |
122 | drive->atapi_flags &= ~IDE_AFLAG_SRFP; | |
123 | ||
07bd3f47 LT |
124 | ide_floppy_create_mode_sense_cmd(pc, IDEFLOPPY_CAPABILITIES_PAGE); |
125 | pc->flags |= PC_FLAG_SUPPRESS_ERROR; | |
0127854d | 126 | |
802e6634 | 127 | if (ide_queue_pc_tail(drive, floppy->disk, pc, buf, pc->req_xfer)) |
0127854d BZ |
128 | return 1; |
129 | ||
802e6634 | 130 | if (buf[8 + 2] & 0x40) |
0127854d BZ |
131 | drive->atapi_flags |= IDE_AFLAG_SRFP; |
132 | ||
133 | return 0; | |
134 | } | |
135 | ||
07bd3f47 LT |
136 | static int ide_floppy_format_unit(ide_drive_t *drive, struct ide_atapi_pc *pc, |
137 | int __user *arg) | |
0127854d | 138 | { |
0df96277 | 139 | struct ide_disk_obj *floppy = drive->driver_data; |
5122e517 | 140 | u8 buf[12]; |
0127854d BZ |
141 | int blocks, length, flags, err = 0; |
142 | ||
143 | if (floppy->openers > 1) { | |
144 | /* Don't format if someone is using the disk */ | |
e0128628 | 145 | drive->dev_flags &= ~IDE_DFLAG_FORMAT_IN_PROGRESS; |
0127854d BZ |
146 | return -EBUSY; |
147 | } | |
148 | ||
e0128628 | 149 | drive->dev_flags |= IDE_DFLAG_FORMAT_IN_PROGRESS; |
0127854d BZ |
150 | |
151 | /* | |
152 | * Send ATAPI_FORMAT_UNIT to the drive. | |
153 | * | |
154 | * Userland gives us the following structure: | |
155 | * | |
156 | * struct idefloppy_format_command { | |
157 | * int nblocks; | |
158 | * int blocksize; | |
159 | * int flags; | |
160 | * } ; | |
161 | * | |
162 | * flags is a bitmask, currently, the only defined flag is: | |
163 | * | |
164 | * 0x01 - verify media after format. | |
165 | */ | |
166 | if (get_user(blocks, arg) || | |
167 | get_user(length, arg+1) || | |
168 | get_user(flags, arg+2)) { | |
169 | err = -EFAULT; | |
170 | goto out; | |
171 | } | |
172 | ||
07bd3f47 | 173 | ide_floppy_get_sfrp_bit(drive, pc); |
5122e517 | 174 | ide_floppy_create_format_unit_cmd(pc, buf, blocks, length, flags); |
0127854d | 175 | |
5122e517 | 176 | if (ide_queue_pc_tail(drive, floppy->disk, pc, buf, pc->req_xfer)) |
0127854d BZ |
177 | err = -EIO; |
178 | ||
179 | out: | |
180 | if (err) | |
e0128628 | 181 | drive->dev_flags &= ~IDE_DFLAG_FORMAT_IN_PROGRESS; |
0127854d BZ |
182 | return err; |
183 | } | |
184 | ||
185 | /* | |
186 | * Get ATAPI_FORMAT_UNIT progress indication. | |
187 | * | |
188 | * Userland gives a pointer to an int. The int is set to a progress | |
189 | * indicator 0-65536, with 65536=100%. | |
190 | * | |
191 | * If the drive does not support format progress indication, we just check | |
192 | * the dsc bit, and return either 0 or 65536. | |
193 | */ | |
194 | ||
07bd3f47 LT |
195 | static int ide_floppy_get_format_progress(ide_drive_t *drive, |
196 | struct ide_atapi_pc *pc, | |
197 | int __user *arg) | |
0127854d | 198 | { |
0df96277 | 199 | struct ide_disk_obj *floppy = drive->driver_data; |
60cfab85 | 200 | u8 sense_buf[18]; |
0127854d BZ |
201 | int progress_indication = 0x10000; |
202 | ||
203 | if (drive->atapi_flags & IDE_AFLAG_SRFP) { | |
07bd3f47 | 204 | ide_create_request_sense_cmd(drive, pc); |
60cfab85 | 205 | if (ide_queue_pc_tail(drive, floppy->disk, pc, sense_buf, |
b13345f3 | 206 | pc->req_xfer)) |
0127854d BZ |
207 | 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 | ||
5bb1536a BZ |
233 | static int ide_floppy_lockdoor(ide_drive_t *drive, struct ide_atapi_pc *pc, |
234 | unsigned long arg, unsigned int cmd) | |
235 | { | |
0df96277 | 236 | struct ide_disk_obj *floppy = drive->driver_data; |
5bb1536a BZ |
237 | 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 | ||
07bd3f47 LT |
251 | static 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) | |
0127854d BZ |
254 | { |
255 | switch (cmd) { | |
256 | case IDEFLOPPY_IOCTL_FORMAT_SUPPORTED: | |
257 | return 0; | |
258 | case IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY: | |
07bd3f47 | 259 | return ide_floppy_get_format_capacities(drive, pc, argp); |
0127854d | 260 | case IDEFLOPPY_IOCTL_FORMAT_START: |
badf8082 | 261 | if (!(mode & FMODE_WRITE)) |
0127854d | 262 | return -EPERM; |
07bd3f47 | 263 | return ide_floppy_format_unit(drive, pc, (int __user *)argp); |
0127854d | 264 | case IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS: |
07bd3f47 | 265 | return ide_floppy_get_format_progress(drive, pc, argp); |
0127854d BZ |
266 | default: |
267 | return -ENOTTY; | |
268 | } | |
269 | } | |
5bb1536a | 270 | |
badf8082 AV |
271 | int ide_floppy_ioctl(ide_drive_t *drive, struct block_device *bdev, |
272 | fmode_t mode, unsigned int cmd, unsigned long arg) | |
5bb1536a | 273 | { |
5bb1536a BZ |
274 | 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 | ||
07bd3f47 | 281 | err = ide_floppy_format_ioctl(drive, &pc, mode, cmd, argp); |
5bb1536a BZ |
282 | 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) | |
74f3c8af | 290 | err = scsi_cmd_ioctl(bdev->bd_disk->queue, bdev->bd_disk, |
badf8082 | 291 | mode, cmd, argp); |
5bb1536a BZ |
292 | |
293 | if (err == -ENOTTY) | |
1bddd9e6 | 294 | err = generic_ide_ioctl(drive, bdev, cmd, arg); |
5bb1536a BZ |
295 | |
296 | return err; | |
297 | } |