ide-floppy: remove IDEFLOPPY_DEBUG_INFO
[deliverable/linux.git] / drivers / ide / ide-floppy.c
1 /*
2 * IDE ATAPI floppy driver.
3 *
4 * Copyright (C) 1996-1999 Gadi Oxman <gadio@netvision.net.il>
5 * Copyright (C) 2000-2002 Paul Bristow <paul@paulbristow.net>
6 * Copyright (C) 2005 Bartlomiej Zolnierkiewicz
7 */
8
9 /*
10 * The driver currently doesn't have any fancy features, just the bare
11 * minimum read/write support.
12 *
13 * This driver supports the following IDE floppy drives:
14 *
15 * LS-120/240 SuperDisk
16 * Iomega Zip 100/250
17 * Iomega PC Card Clik!/PocketZip
18 *
19 * For a historical changelog see
20 * Documentation/ide/ChangeLog.ide-floppy.1996-2002
21 */
22
23 #define IDEFLOPPY_VERSION "0.99.newide"
24
25 #include <linux/module.h>
26 #include <linux/types.h>
27 #include <linux/string.h>
28 #include <linux/kernel.h>
29 #include <linux/delay.h>
30 #include <linux/timer.h>
31 #include <linux/mm.h>
32 #include <linux/interrupt.h>
33 #include <linux/major.h>
34 #include <linux/errno.h>
35 #include <linux/genhd.h>
36 #include <linux/slab.h>
37 #include <linux/cdrom.h>
38 #include <linux/ide.h>
39 #include <linux/bitops.h>
40 #include <linux/mutex.h>
41
42 #include <scsi/scsi_ioctl.h>
43
44 #include <asm/byteorder.h>
45 #include <linux/irq.h>
46 #include <linux/uaccess.h>
47 #include <linux/io.h>
48 #include <asm/unaligned.h>
49
50 /* define to see debug info */
51 #define IDEFLOPPY_DEBUG_LOG 0
52
53 /* #define IDEFLOPPY_DEBUG(fmt, args...) printk(KERN_INFO fmt, ## args) */
54 #define IDEFLOPPY_DEBUG( fmt, args... )
55
56 #if IDEFLOPPY_DEBUG_LOG
57 #define debug_log(fmt, args...) \
58 printk(KERN_INFO "ide-floppy: " fmt, ## args)
59 #else
60 #define debug_log(fmt, args... ) do {} while(0)
61 #endif
62
63
64 /*
65 * Some drives require a longer irq timeout.
66 */
67 #define IDEFLOPPY_WAIT_CMD (5 * WAIT_CMD)
68
69 /*
70 * After each failed packet command we issue a request sense command
71 * and retry the packet command IDEFLOPPY_MAX_PC_RETRIES times.
72 */
73 #define IDEFLOPPY_MAX_PC_RETRIES 3
74
75 /*
76 * With each packet command, we allocate a buffer of
77 * IDEFLOPPY_PC_BUFFER_SIZE bytes.
78 */
79 #define IDEFLOPPY_PC_BUFFER_SIZE 256
80
81 /*
82 * In various places in the driver, we need to allocate storage
83 * for packet commands and requests, which will remain valid while
84 * we leave the driver to wait for an interrupt or a timeout event.
85 */
86 #define IDEFLOPPY_PC_STACK (10 + IDEFLOPPY_MAX_PC_RETRIES)
87
88 /*
89 * Our view of a packet command.
90 */
91 typedef struct idefloppy_packet_command_s {
92 u8 c[12]; /* Actual packet bytes */
93 int retries; /* On each retry, we increment retries */
94 int error; /* Error code */
95 int request_transfer; /* Bytes to transfer */
96 int actually_transferred; /* Bytes actually transferred */
97 int buffer_size; /* Size of our data buffer */
98 int b_count; /* Missing/Available data on the current buffer */
99 struct request *rq; /* The corresponding request */
100 u8 *buffer; /* Data buffer */
101 u8 *current_position; /* Pointer into the above buffer */
102 void (*callback) (ide_drive_t *); /* Called when this packet command is completed */
103 u8 pc_buffer[IDEFLOPPY_PC_BUFFER_SIZE]; /* Temporary buffer */
104 unsigned long flags; /* Status/Action bit flags: long for set_bit */
105 } idefloppy_pc_t;
106
107 /*
108 * Packet command flag bits.
109 */
110 #define PC_ABORT 0 /* Set when an error is considered normal - We won't retry */
111 #define PC_DMA_RECOMMENDED 2 /* 1 when we prefer to use DMA if possible */
112 #define PC_DMA_IN_PROGRESS 3 /* 1 while DMA in progress */
113 #define PC_DMA_ERROR 4 /* 1 when encountered problem during DMA */
114 #define PC_WRITING 5 /* Data direction */
115
116 #define PC_SUPPRESS_ERROR 6 /* Suppress error reporting */
117
118 /* format capacities descriptor codes */
119 #define CAPACITY_INVALID 0x00
120 #define CAPACITY_UNFORMATTED 0x01
121 #define CAPACITY_CURRENT 0x02
122 #define CAPACITY_NO_CARTRIDGE 0x03
123
124 /*
125 * Most of our global data which we need to save even as we leave the
126 * driver due to an interrupt or a timer event is stored in a variable
127 * of type idefloppy_floppy_t, defined below.
128 */
129 typedef struct ide_floppy_obj {
130 ide_drive_t *drive;
131 ide_driver_t *driver;
132 struct gendisk *disk;
133 struct kref kref;
134 unsigned int openers; /* protected by BKL for now */
135
136 /* Current packet command */
137 idefloppy_pc_t *pc;
138 /* Last failed packet command */
139 idefloppy_pc_t *failed_pc;
140 /* Packet command stack */
141 idefloppy_pc_t pc_stack[IDEFLOPPY_PC_STACK];
142 /* Next free packet command storage space */
143 int pc_stack_index;
144 struct request rq_stack[IDEFLOPPY_PC_STACK];
145 /* We implement a circular array */
146 int rq_stack_index;
147
148 /*
149 * Last error information
150 */
151 u8 sense_key, asc, ascq;
152 /* delay this long before sending packet command */
153 u8 ticks;
154 int progress_indication;
155
156 /*
157 * Device information
158 */
159 /* Current format */
160 int blocks, block_size, bs_factor;
161 /* Last format capacity descriptor */
162 u8 cap_desc[8];
163 /* Copy of the flexible disk page */
164 u8 flexible_disk_page[32];
165 /* Write protect */
166 int wp;
167 /* Supports format progress report */
168 int srfp;
169 /* Status/Action flags */
170 unsigned long flags;
171 } idefloppy_floppy_t;
172
173 #define IDEFLOPPY_TICKS_DELAY HZ/20 /* default delay for ZIP 100 (50ms) */
174
175 /*
176 * Floppy flag bits values.
177 */
178 #define IDEFLOPPY_DRQ_INTERRUPT 0 /* DRQ interrupt device */
179 #define IDEFLOPPY_MEDIA_CHANGED 1 /* Media may have changed */
180 #define IDEFLOPPY_FORMAT_IN_PROGRESS 3 /* Format in progress */
181 #define IDEFLOPPY_CLIK_DRIVE 4 /* Avoid commands not supported in Clik drive */
182 #define IDEFLOPPY_ZIP_DRIVE 5 /* Requires BH algorithm for packets */
183
184 /*
185 * Defines for the mode sense command
186 */
187 #define MODE_SENSE_CURRENT 0x00
188 #define MODE_SENSE_CHANGEABLE 0x01
189 #define MODE_SENSE_DEFAULT 0x02
190 #define MODE_SENSE_SAVED 0x03
191
192 /*
193 * IOCTLs used in low-level formatting.
194 */
195
196 #define IDEFLOPPY_IOCTL_FORMAT_SUPPORTED 0x4600
197 #define IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY 0x4601
198 #define IDEFLOPPY_IOCTL_FORMAT_START 0x4602
199 #define IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS 0x4603
200
201 /*
202 * Error codes which are returned in rq->errors to the higher part
203 * of the driver.
204 */
205 #define IDEFLOPPY_ERROR_GENERAL 101
206
207 /*
208 * The following is used to format the general configuration word of
209 * the ATAPI IDENTIFY DEVICE command.
210 */
211 struct idefloppy_id_gcw {
212 #if defined(__LITTLE_ENDIAN_BITFIELD)
213 unsigned packet_size :2; /* Packet Size */
214 unsigned reserved234 :3; /* Reserved */
215 unsigned drq_type :2; /* Command packet DRQ type */
216 unsigned removable :1; /* Removable media */
217 unsigned device_type :5; /* Device type */
218 unsigned reserved13 :1; /* Reserved */
219 unsigned protocol :2; /* Protocol type */
220 #elif defined(__BIG_ENDIAN_BITFIELD)
221 unsigned protocol :2; /* Protocol type */
222 unsigned reserved13 :1; /* Reserved */
223 unsigned device_type :5; /* Device type */
224 unsigned removable :1; /* Removable media */
225 unsigned drq_type :2; /* Command packet DRQ type */
226 unsigned reserved234 :3; /* Reserved */
227 unsigned packet_size :2; /* Packet Size */
228 #else
229 #error "Bitfield endianness not defined! Check your byteorder.h"
230 #endif
231 };
232
233 /*
234 * Pages of the SELECT SENSE / MODE SENSE packet commands.
235 * See SFF-8070i spec.
236 */
237 #define IDEFLOPPY_CAPABILITIES_PAGE 0x1b
238 #define IDEFLOPPY_FLEXIBLE_DISK_PAGE 0x05
239
240 static DEFINE_MUTEX(idefloppy_ref_mutex);
241
242 #define to_ide_floppy(obj) container_of(obj, struct ide_floppy_obj, kref)
243
244 #define ide_floppy_g(disk) \
245 container_of((disk)->private_data, struct ide_floppy_obj, driver)
246
247 static struct ide_floppy_obj *ide_floppy_get(struct gendisk *disk)
248 {
249 struct ide_floppy_obj *floppy = NULL;
250
251 mutex_lock(&idefloppy_ref_mutex);
252 floppy = ide_floppy_g(disk);
253 if (floppy)
254 kref_get(&floppy->kref);
255 mutex_unlock(&idefloppy_ref_mutex);
256 return floppy;
257 }
258
259 static void idefloppy_cleanup_obj(struct kref *);
260
261 static void ide_floppy_put(struct ide_floppy_obj *floppy)
262 {
263 mutex_lock(&idefloppy_ref_mutex);
264 kref_put(&floppy->kref, idefloppy_cleanup_obj);
265 mutex_unlock(&idefloppy_ref_mutex);
266 }
267
268 /*
269 * Too bad. The drive wants to send us data which we are not ready to accept.
270 * Just throw it away.
271 */
272 static void idefloppy_discard_data (ide_drive_t *drive, unsigned int bcount)
273 {
274 while (bcount--)
275 (void) HWIF(drive)->INB(IDE_DATA_REG);
276 }
277
278 static void idefloppy_write_zeros(ide_drive_t *drive, unsigned int bcount)
279 {
280 while (bcount--)
281 HWIF(drive)->OUTB(0, IDE_DATA_REG);
282 }
283
284
285 /*
286 * idefloppy_do_end_request is used to finish servicing a request.
287 *
288 * For read/write requests, we will call ide_end_request to pass to the
289 * next buffer.
290 */
291 static int idefloppy_do_end_request(ide_drive_t *drive, int uptodate, int nsecs)
292 {
293 idefloppy_floppy_t *floppy = drive->driver_data;
294 struct request *rq = HWGROUP(drive)->rq;
295 int error;
296
297 debug_log("Reached %s\n", __func__);
298
299 switch (uptodate) {
300 case 0: error = IDEFLOPPY_ERROR_GENERAL; break;
301 case 1: error = 0; break;
302 default: error = uptodate;
303 }
304 if (error)
305 floppy->failed_pc = NULL;
306 /* Why does this happen? */
307 if (!rq)
308 return 0;
309 if (!blk_special_request(rq)) {
310 /* our real local end request function */
311 ide_end_request(drive, uptodate, nsecs);
312 return 0;
313 }
314 rq->errors = error;
315 /* fixme: need to move this local also */
316 ide_end_drive_cmd(drive, 0, 0);
317 return 0;
318 }
319
320 static void ide_floppy_io_buffers(ide_drive_t *drive, idefloppy_pc_t *pc,
321 unsigned int bcount, int direction)
322 {
323 struct request *rq = pc->rq;
324 struct req_iterator iter;
325 struct bio_vec *bvec;
326 unsigned long flags;
327 int count, done = 0;
328 char *data;
329
330 rq_for_each_segment(bvec, rq, iter) {
331 if (!bcount)
332 break;
333
334 count = min(bvec->bv_len, bcount);
335
336 data = bvec_kmap_irq(bvec, &flags);
337 if (direction)
338 drive->hwif->atapi_output_bytes(drive, data, count);
339 else
340 drive->hwif->atapi_input_bytes(drive, data, count);
341 bvec_kunmap_irq(data, &flags);
342
343 bcount -= count;
344 pc->b_count += count;
345 done += count;
346 }
347
348 idefloppy_do_end_request(drive, 1, done >> 9);
349
350 if (bcount) {
351 printk(KERN_ERR "%s: leftover data in %s, bcount == %d\n",
352 drive->name, __func__, bcount);
353 if (direction)
354 idefloppy_write_zeros(drive, bcount);
355 else
356 idefloppy_discard_data(drive, bcount);
357
358 }
359 }
360
361 static void idefloppy_update_buffers (ide_drive_t *drive, idefloppy_pc_t *pc)
362 {
363 struct request *rq = pc->rq;
364 struct bio *bio = rq->bio;
365
366 while ((bio = rq->bio) != NULL)
367 idefloppy_do_end_request(drive, 1, 0);
368 }
369
370 /*
371 * idefloppy_queue_pc_head generates a new packet command request in front
372 * of the request queue, before the current request, so that it will be
373 * processed immediately, on the next pass through the driver.
374 */
375 static void idefloppy_queue_pc_head (ide_drive_t *drive,idefloppy_pc_t *pc,struct request *rq)
376 {
377 struct ide_floppy_obj *floppy = drive->driver_data;
378
379 ide_init_drive_cmd(rq);
380 rq->buffer = (char *) pc;
381 rq->cmd_type = REQ_TYPE_SPECIAL;
382 rq->rq_disk = floppy->disk;
383 (void) ide_do_drive_cmd(drive, rq, ide_preempt);
384 }
385
386 static idefloppy_pc_t *idefloppy_next_pc_storage (ide_drive_t *drive)
387 {
388 idefloppy_floppy_t *floppy = drive->driver_data;
389
390 if (floppy->pc_stack_index == IDEFLOPPY_PC_STACK)
391 floppy->pc_stack_index=0;
392 return (&floppy->pc_stack[floppy->pc_stack_index++]);
393 }
394
395 static struct request *idefloppy_next_rq_storage (ide_drive_t *drive)
396 {
397 idefloppy_floppy_t *floppy = drive->driver_data;
398
399 if (floppy->rq_stack_index == IDEFLOPPY_PC_STACK)
400 floppy->rq_stack_index = 0;
401 return (&floppy->rq_stack[floppy->rq_stack_index++]);
402 }
403
404 static void idefloppy_request_sense_callback(ide_drive_t *drive)
405 {
406 idefloppy_floppy_t *floppy = drive->driver_data;
407 u8 *buf = floppy->pc->buffer;
408
409 debug_log("Reached %s\n", __func__);
410
411 if (!floppy->pc->error) {
412 floppy->sense_key = buf[2] & 0x0F;
413 floppy->asc = buf[12];
414 floppy->ascq = buf[13];
415 floppy->progress_indication = buf[15] & 0x80 ?
416 (u16)get_unaligned((u16 *)&buf[16]) : 0x10000;
417
418 if (floppy->failed_pc)
419 debug_log("pc = %x, sense key = %x, asc = %x,"
420 " ascq = %x\n",
421 floppy->failed_pc->c[0],
422 floppy->sense_key,
423 floppy->asc,
424 floppy->ascq);
425 else
426 debug_log("sense key = %x, asc = %x, ascq = %x\n",
427 floppy->sense_key,
428 floppy->asc,
429 floppy->ascq);
430
431
432 idefloppy_do_end_request(drive, 1, 0);
433 } else {
434 printk(KERN_ERR "Error in REQUEST SENSE itself - Aborting"
435 " request!\n");
436 idefloppy_do_end_request(drive, 0, 0);
437 }
438 }
439
440 /*
441 * General packet command callback function.
442 */
443 static void idefloppy_pc_callback (ide_drive_t *drive)
444 {
445 idefloppy_floppy_t *floppy = drive->driver_data;
446
447 debug_log("Reached %s\n", __func__);
448
449 idefloppy_do_end_request(drive, floppy->pc->error ? 0 : 1, 0);
450 }
451
452 /*
453 * idefloppy_init_pc initializes a packet command.
454 */
455 static void idefloppy_init_pc (idefloppy_pc_t *pc)
456 {
457 memset(pc->c, 0, 12);
458 pc->retries = 0;
459 pc->flags = 0;
460 pc->request_transfer = 0;
461 pc->buffer = pc->pc_buffer;
462 pc->buffer_size = IDEFLOPPY_PC_BUFFER_SIZE;
463 pc->callback = &idefloppy_pc_callback;
464 }
465
466 static void idefloppy_create_request_sense_cmd (idefloppy_pc_t *pc)
467 {
468 idefloppy_init_pc(pc);
469 pc->c[0] = GPCMD_REQUEST_SENSE;
470 pc->c[4] = 255;
471 pc->request_transfer = 18;
472 pc->callback = &idefloppy_request_sense_callback;
473 }
474
475 /*
476 * idefloppy_retry_pc is called when an error was detected during the
477 * last packet command. We queue a request sense packet command in
478 * the head of the request list.
479 */
480 static void idefloppy_retry_pc (ide_drive_t *drive)
481 {
482 idefloppy_pc_t *pc;
483 struct request *rq;
484
485 (void)drive->hwif->INB(IDE_ERROR_REG);
486 pc = idefloppy_next_pc_storage(drive);
487 rq = idefloppy_next_rq_storage(drive);
488 idefloppy_create_request_sense_cmd(pc);
489 idefloppy_queue_pc_head(drive, pc, rq);
490 }
491
492 /* The usual interrupt handler called during a packet command. */
493 static ide_startstop_t idefloppy_pc_intr (ide_drive_t *drive)
494 {
495 idefloppy_floppy_t *floppy = drive->driver_data;
496 ide_hwif_t *hwif = drive->hwif;
497 idefloppy_pc_t *pc = floppy->pc;
498 struct request *rq = pc->rq;
499 xfer_func_t *xferfunc;
500 unsigned int temp;
501 int dma_error = 0;
502 u16 bcount;
503 u8 stat, ireason;
504
505 debug_log("Reached %s interrupt handler\n", __func__);
506
507 if (test_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
508 dma_error = hwif->ide_dma_end(drive);
509 if (dma_error) {
510 printk(KERN_ERR "%s: DMA %s error\n", drive->name,
511 rq_data_dir(rq) ? "write" : "read");
512 set_bit(PC_DMA_ERROR, &pc->flags);
513 } else {
514 pc->actually_transferred = pc->request_transfer;
515 idefloppy_update_buffers(drive, pc);
516 }
517 debug_log("DMA finished\n");
518 }
519
520 /* Clear the interrupt */
521 stat = drive->hwif->INB(IDE_STATUS_REG);
522
523 if ((stat & DRQ_STAT) == 0) { /* No more interrupts */
524 debug_log("Packet command completed, %d bytes transferred\n",
525 pc->actually_transferred);
526 clear_bit(PC_DMA_IN_PROGRESS, &pc->flags);
527
528 local_irq_enable_in_hardirq();
529
530 if ((stat & ERR_STAT) || test_bit(PC_DMA_ERROR, &pc->flags)) {
531 /* Error detected */
532 debug_log("%s: I/O error\n", drive->name);
533 rq->errors++;
534 if (pc->c[0] == GPCMD_REQUEST_SENSE) {
535 printk(KERN_ERR "ide-floppy: I/O error in "
536 "request sense command\n");
537 return ide_do_reset(drive);
538 }
539 /* Retry operation */
540 idefloppy_retry_pc(drive);
541 /* queued, but not started */
542 return ide_stopped;
543 }
544 pc->error = 0;
545 if (floppy->failed_pc == pc)
546 floppy->failed_pc = NULL;
547 /* Command finished - Call the callback function */
548 pc->callback(drive);
549 return ide_stopped;
550 }
551
552 if (test_and_clear_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
553 printk(KERN_ERR "ide-floppy: The floppy wants to issue "
554 "more interrupts in DMA mode\n");
555 ide_dma_off(drive);
556 return ide_do_reset(drive);
557 }
558
559 /* Get the number of bytes to transfer */
560 bcount = (hwif->INB(IDE_BCOUNTH_REG) << 8) |
561 hwif->INB(IDE_BCOUNTL_REG);
562 /* on this interrupt */
563 ireason = hwif->INB(IDE_IREASON_REG);
564
565 if (ireason & CD) {
566 printk(KERN_ERR "ide-floppy: CoD != 0 in %s\n", __func__);
567 return ide_do_reset(drive);
568 }
569 if (((ireason & IO) == IO) == test_bit(PC_WRITING, &pc->flags)) {
570 /* Hopefully, we will never get here */
571 printk(KERN_ERR "ide-floppy: We wanted to %s, ",
572 (ireason & IO) ? "Write" : "Read");
573 printk(KERN_ERR "but the floppy wants us to %s !\n",
574 (ireason & IO) ? "Read" : "Write");
575 return ide_do_reset(drive);
576 }
577 if (!test_bit(PC_WRITING, &pc->flags)) {
578 /* Reading - Check that we have enough space */
579 temp = pc->actually_transferred + bcount;
580 if (temp > pc->request_transfer) {
581 if (temp > pc->buffer_size) {
582 printk(KERN_ERR "ide-floppy: The floppy wants "
583 "to send us more data than expected "
584 "- discarding data\n");
585 idefloppy_discard_data(drive, bcount);
586
587 ide_set_handler(drive,
588 &idefloppy_pc_intr,
589 IDEFLOPPY_WAIT_CMD,
590 NULL);
591 return ide_started;
592 }
593 debug_log("The floppy wants to send us more data than"
594 " expected - allowing transfer\n");
595 }
596 }
597 if (test_bit(PC_WRITING, &pc->flags))
598 xferfunc = hwif->atapi_output_bytes;
599 else
600 xferfunc = hwif->atapi_input_bytes;
601
602 if (pc->buffer)
603 xferfunc(drive, pc->current_position, bcount);
604 else
605 ide_floppy_io_buffers(drive, pc, bcount,
606 test_bit(PC_WRITING, &pc->flags));
607
608 /* Update the current position */
609 pc->actually_transferred += bcount;
610 pc->current_position += bcount;
611
612 /* And set the interrupt handler again */
613 ide_set_handler(drive, &idefloppy_pc_intr, IDEFLOPPY_WAIT_CMD, NULL);
614 return ide_started;
615 }
616
617 /*
618 * This is the original routine that did the packet transfer.
619 * It fails at high speeds on the Iomega ZIP drive, so there's a slower version
620 * for that drive below. The algorithm is chosen based on drive type
621 */
622 static ide_startstop_t idefloppy_transfer_pc (ide_drive_t *drive)
623 {
624 ide_startstop_t startstop;
625 idefloppy_floppy_t *floppy = drive->driver_data;
626 u8 ireason;
627
628 if (ide_wait_stat(&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) {
629 printk(KERN_ERR "ide-floppy: Strange, packet command "
630 "initiated yet DRQ isn't asserted\n");
631 return startstop;
632 }
633 ireason = drive->hwif->INB(IDE_IREASON_REG);
634 if ((ireason & CD) == 0 || (ireason & IO)) {
635 printk(KERN_ERR "ide-floppy: (IO,CoD) != (0,1) while "
636 "issuing a packet command\n");
637 return ide_do_reset(drive);
638 }
639
640 /* Set the interrupt routine */
641 ide_set_handler(drive, &idefloppy_pc_intr, IDEFLOPPY_WAIT_CMD, NULL);
642 /* Send the actual packet */
643 HWIF(drive)->atapi_output_bytes(drive, floppy->pc->c, 12);
644 return ide_started;
645 }
646
647
648 /*
649 * What we have here is a classic case of a top half / bottom half
650 * interrupt service routine. In interrupt mode, the device sends
651 * an interrupt to signal it's ready to receive a packet. However,
652 * we need to delay about 2-3 ticks before issuing the packet or we
653 * gets in trouble.
654 *
655 * So, follow carefully. transfer_pc1 is called as an interrupt (or
656 * directly). In either case, when the device says it's ready for a
657 * packet, we schedule the packet transfer to occur about 2-3 ticks
658 * later in transfer_pc2.
659 */
660 static int idefloppy_transfer_pc2 (ide_drive_t *drive)
661 {
662 idefloppy_floppy_t *floppy = drive->driver_data;
663
664 /* Send the actual packet */
665 HWIF(drive)->atapi_output_bytes(drive, floppy->pc->c, 12);
666 /* Timeout for the packet command */
667 return IDEFLOPPY_WAIT_CMD;
668 }
669
670 static ide_startstop_t idefloppy_transfer_pc1 (ide_drive_t *drive)
671 {
672 idefloppy_floppy_t *floppy = drive->driver_data;
673 ide_startstop_t startstop;
674 u8 ireason;
675
676 if (ide_wait_stat(&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) {
677 printk(KERN_ERR "ide-floppy: Strange, packet command "
678 "initiated yet DRQ isn't asserted\n");
679 return startstop;
680 }
681 ireason = drive->hwif->INB(IDE_IREASON_REG);
682 if ((ireason & CD) == 0 || (ireason & IO)) {
683 printk(KERN_ERR "ide-floppy: (IO,CoD) != (0,1) "
684 "while issuing a packet command\n");
685 return ide_do_reset(drive);
686 }
687 /*
688 * The following delay solves a problem with ATAPI Zip 100 drives
689 * where the Busy flag was apparently being deasserted before the
690 * unit was ready to receive data. This was happening on a
691 * 1200 MHz Athlon system. 10/26/01 25msec is too short,
692 * 40 and 50msec work well. idefloppy_pc_intr will not be actually
693 * used until after the packet is moved in about 50 msec.
694 */
695
696 ide_set_handler(drive,
697 &idefloppy_pc_intr, /* service routine for packet command */
698 floppy->ticks, /* wait this long before "failing" */
699 &idefloppy_transfer_pc2); /* fail == transfer_pc2 */
700 return ide_started;
701 }
702
703 static void ide_floppy_report_error(idefloppy_floppy_t *floppy,
704 idefloppy_pc_t *pc)
705 {
706 /* supress error messages resulting from Medium not present */
707 if (floppy->sense_key == 0x02 &&
708 floppy->asc == 0x3a &&
709 floppy->ascq == 0x00)
710 return;
711
712 printk(KERN_ERR "ide-floppy: %s: I/O error, pc = %2x, key = %2x, "
713 "asc = %2x, ascq = %2x\n",
714 floppy->drive->name, pc->c[0], floppy->sense_key,
715 floppy->asc, floppy->ascq);
716
717 }
718
719 /*
720 * Issue a packet command
721 */
722 static ide_startstop_t idefloppy_issue_pc (ide_drive_t *drive, idefloppy_pc_t *pc)
723 {
724 idefloppy_floppy_t *floppy = drive->driver_data;
725 ide_hwif_t *hwif = drive->hwif;
726 ide_handler_t *pkt_xfer_routine;
727 u16 bcount;
728 u8 dma;
729
730 if (floppy->failed_pc == NULL &&
731 pc->c[0] != GPCMD_REQUEST_SENSE)
732 floppy->failed_pc = pc;
733 /* Set the current packet command */
734 floppy->pc = pc;
735
736 if (pc->retries > IDEFLOPPY_MAX_PC_RETRIES ||
737 test_bit(PC_ABORT, &pc->flags)) {
738 /*
739 * We will "abort" retrying a packet command in case
740 * a legitimate error code was received.
741 */
742 if (!test_bit(PC_ABORT, &pc->flags)) {
743 if (!test_bit(PC_SUPPRESS_ERROR, &pc->flags))
744 ide_floppy_report_error(floppy, pc);
745 /* Giving up */
746 pc->error = IDEFLOPPY_ERROR_GENERAL;
747 }
748 floppy->failed_pc = NULL;
749 pc->callback(drive);
750 return ide_stopped;
751 }
752
753 debug_log("Retry number - %d\n", pc->retries);
754
755 pc->retries++;
756 /* We haven't transferred any data yet */
757 pc->actually_transferred = 0;
758 pc->current_position = pc->buffer;
759 bcount = min(pc->request_transfer, 63 * 1024);
760
761 if (test_and_clear_bit(PC_DMA_ERROR, &pc->flags))
762 ide_dma_off(drive);
763
764 dma = 0;
765
766 if (test_bit(PC_DMA_RECOMMENDED, &pc->flags) && drive->using_dma)
767 dma = !hwif->dma_setup(drive);
768
769 ide_pktcmd_tf_load(drive, IDE_TFLAG_NO_SELECT_MASK |
770 IDE_TFLAG_OUT_DEVICE, bcount, dma);
771
772 if (dma) { /* Begin DMA, if necessary */
773 set_bit(PC_DMA_IN_PROGRESS, &pc->flags);
774 hwif->dma_start(drive);
775 }
776
777 /* Can we transfer the packet when we get the interrupt or wait? */
778 if (test_bit(IDEFLOPPY_ZIP_DRIVE, &floppy->flags)) {
779 /* wait */
780 pkt_xfer_routine = &idefloppy_transfer_pc1;
781 } else {
782 /* immediate */
783 pkt_xfer_routine = &idefloppy_transfer_pc;
784 }
785
786 if (test_bit (IDEFLOPPY_DRQ_INTERRUPT, &floppy->flags)) {
787 /* Issue the packet command */
788 ide_execute_command(drive, WIN_PACKETCMD,
789 pkt_xfer_routine,
790 IDEFLOPPY_WAIT_CMD,
791 NULL);
792 return ide_started;
793 } else {
794 /* Issue the packet command */
795 HWIF(drive)->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
796 return (*pkt_xfer_routine) (drive);
797 }
798 }
799
800 static void idefloppy_rw_callback (ide_drive_t *drive)
801 {
802 debug_log("Reached %s\n", __func__);
803
804 idefloppy_do_end_request(drive, 1, 0);
805 return;
806 }
807
808 static void idefloppy_create_prevent_cmd (idefloppy_pc_t *pc, int prevent)
809 {
810 debug_log("creating prevent removal command, prevent = %d\n", prevent);
811
812 idefloppy_init_pc(pc);
813 pc->c[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL;
814 pc->c[4] = prevent;
815 }
816
817 static void idefloppy_create_read_capacity_cmd (idefloppy_pc_t *pc)
818 {
819 idefloppy_init_pc(pc);
820 pc->c[0] = GPCMD_READ_FORMAT_CAPACITIES;
821 pc->c[7] = 255;
822 pc->c[8] = 255;
823 pc->request_transfer = 255;
824 }
825
826 static void idefloppy_create_format_unit_cmd (idefloppy_pc_t *pc, int b, int l,
827 int flags)
828 {
829 idefloppy_init_pc(pc);
830 pc->c[0] = GPCMD_FORMAT_UNIT;
831 pc->c[1] = 0x17;
832
833 memset(pc->buffer, 0, 12);
834 pc->buffer[1] = 0xA2;
835 /* Default format list header, u8 1: FOV/DCRT/IMM bits set */
836
837 if (flags & 1) /* Verify bit on... */
838 pc->buffer[1] ^= 0x20; /* ... turn off DCRT bit */
839 pc->buffer[3] = 8;
840
841 put_unaligned(cpu_to_be32(b), (unsigned int *)(&pc->buffer[4]));
842 put_unaligned(cpu_to_be32(l), (unsigned int *)(&pc->buffer[8]));
843 pc->buffer_size=12;
844 set_bit(PC_WRITING, &pc->flags);
845 }
846
847 /*
848 * A mode sense command is used to "sense" floppy parameters.
849 */
850 static void idefloppy_create_mode_sense_cmd (idefloppy_pc_t *pc, u8 page_code, u8 type)
851 {
852 u16 length = 8; /* sizeof(Mode Parameter Header) = 8 Bytes */
853
854 idefloppy_init_pc(pc);
855 pc->c[0] = GPCMD_MODE_SENSE_10;
856 pc->c[1] = 0;
857 pc->c[2] = page_code + (type << 6);
858
859 switch (page_code) {
860 case IDEFLOPPY_CAPABILITIES_PAGE:
861 length += 12;
862 break;
863 case IDEFLOPPY_FLEXIBLE_DISK_PAGE:
864 length += 32;
865 break;
866 default:
867 printk(KERN_ERR "ide-floppy: unsupported page code "
868 "in create_mode_sense_cmd\n");
869 }
870 put_unaligned(cpu_to_be16(length), (u16 *) &pc->c[7]);
871 pc->request_transfer = length;
872 }
873
874 static void idefloppy_create_start_stop_cmd (idefloppy_pc_t *pc, int start)
875 {
876 idefloppy_init_pc(pc);
877 pc->c[0] = GPCMD_START_STOP_UNIT;
878 pc->c[4] = start;
879 }
880
881 static void idefloppy_create_test_unit_ready_cmd(idefloppy_pc_t *pc)
882 {
883 idefloppy_init_pc(pc);
884 pc->c[0] = GPCMD_TEST_UNIT_READY;
885 }
886
887 static void idefloppy_create_rw_cmd(idefloppy_floppy_t *floppy,
888 idefloppy_pc_t *pc, struct request *rq,
889 unsigned long sector)
890 {
891 int block = sector / floppy->bs_factor;
892 int blocks = rq->nr_sectors / floppy->bs_factor;
893 int cmd = rq_data_dir(rq);
894
895 debug_log("create_rw10_cmd: block == %d, blocks == %d\n",
896 block, blocks);
897
898 idefloppy_init_pc(pc);
899 pc->c[0] = cmd == READ ? GPCMD_READ_10 : GPCMD_WRITE_10;
900 put_unaligned(cpu_to_be16(blocks), (unsigned short *)&pc->c[7]);
901 put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[2]);
902
903 pc->callback = &idefloppy_rw_callback;
904 pc->rq = rq;
905 pc->b_count = cmd == READ ? 0 : rq->bio->bi_size;
906 if (rq->cmd_flags & REQ_RW)
907 set_bit(PC_WRITING, &pc->flags);
908 pc->buffer = NULL;
909 pc->request_transfer = pc->buffer_size = blocks * floppy->block_size;
910 set_bit(PC_DMA_RECOMMENDED, &pc->flags);
911 }
912
913 static void
914 idefloppy_blockpc_cmd(idefloppy_floppy_t *floppy, idefloppy_pc_t *pc, struct request *rq)
915 {
916 idefloppy_init_pc(pc);
917 pc->callback = &idefloppy_rw_callback;
918 memcpy(pc->c, rq->cmd, sizeof(pc->c));
919 pc->rq = rq;
920 pc->b_count = rq->data_len;
921 if (rq->data_len && rq_data_dir(rq) == WRITE)
922 set_bit(PC_WRITING, &pc->flags);
923 pc->buffer = rq->data;
924 if (rq->bio)
925 set_bit(PC_DMA_RECOMMENDED, &pc->flags);
926
927 /*
928 * possibly problematic, doesn't look like ide-floppy correctly
929 * handled scattered requests if dma fails...
930 */
931 pc->request_transfer = pc->buffer_size = rq->data_len;
932 }
933
934 /*
935 * idefloppy_do_request is our request handling function.
936 */
937 static ide_startstop_t idefloppy_do_request (ide_drive_t *drive, struct request *rq, sector_t block_s)
938 {
939 idefloppy_floppy_t *floppy = drive->driver_data;
940 idefloppy_pc_t *pc;
941 unsigned long block = (unsigned long)block_s;
942
943 debug_log("dev: %s, cmd_type: %x, errors: %d\n",
944 rq->rq_disk ? rq->rq_disk->disk_name : "?",
945 rq->cmd_type, rq->errors);
946 debug_log("sector: %ld, nr_sectors: %ld, "
947 "current_nr_sectors: %d\n", (long)rq->sector,
948 rq->nr_sectors, rq->current_nr_sectors);
949
950 if (rq->errors >= ERROR_MAX) {
951 if (floppy->failed_pc)
952 ide_floppy_report_error(floppy, floppy->failed_pc);
953 else
954 printk(KERN_ERR "ide-floppy: %s: I/O error\n",
955 drive->name);
956 idefloppy_do_end_request(drive, 0, 0);
957 return ide_stopped;
958 }
959 if (blk_fs_request(rq)) {
960 if (((long)rq->sector % floppy->bs_factor) ||
961 (rq->nr_sectors % floppy->bs_factor)) {
962 printk("%s: unsupported r/w request size\n",
963 drive->name);
964 idefloppy_do_end_request(drive, 0, 0);
965 return ide_stopped;
966 }
967 pc = idefloppy_next_pc_storage(drive);
968 idefloppy_create_rw_cmd(floppy, pc, rq, block);
969 } else if (blk_special_request(rq)) {
970 pc = (idefloppy_pc_t *) rq->buffer;
971 } else if (blk_pc_request(rq)) {
972 pc = idefloppy_next_pc_storage(drive);
973 idefloppy_blockpc_cmd(floppy, pc, rq);
974 } else {
975 blk_dump_rq_flags(rq,
976 "ide-floppy: unsupported command in queue");
977 idefloppy_do_end_request(drive, 0, 0);
978 return ide_stopped;
979 }
980
981 pc->rq = rq;
982 return idefloppy_issue_pc(drive, pc);
983 }
984
985 /*
986 * idefloppy_queue_pc_tail adds a special packet command request to the
987 * tail of the request queue, and waits for it to be serviced.
988 */
989 static int idefloppy_queue_pc_tail (ide_drive_t *drive,idefloppy_pc_t *pc)
990 {
991 struct ide_floppy_obj *floppy = drive->driver_data;
992 struct request rq;
993
994 ide_init_drive_cmd (&rq);
995 rq.buffer = (char *) pc;
996 rq.cmd_type = REQ_TYPE_SPECIAL;
997 rq.rq_disk = floppy->disk;
998
999 return ide_do_drive_cmd(drive, &rq, ide_wait);
1000 }
1001
1002 /*
1003 * Look at the flexible disk page parameters. We ignore the CHS capacity
1004 * parameters and use the LBA parameters instead.
1005 */
1006 static int ide_floppy_get_flexible_disk_page(ide_drive_t *drive)
1007 {
1008 idefloppy_floppy_t *floppy = drive->driver_data;
1009 idefloppy_pc_t pc;
1010 u8 *page;
1011 int capacity, lba_capacity;
1012 u16 transfer_rate, sector_size, cyls, rpm;
1013 u8 heads, sectors;
1014
1015 idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_FLEXIBLE_DISK_PAGE,
1016 MODE_SENSE_CURRENT);
1017
1018 if (idefloppy_queue_pc_tail(drive, &pc)) {
1019 printk(KERN_ERR "ide-floppy: Can't get flexible disk page"
1020 " parameters\n");
1021 return 1;
1022 }
1023 floppy->wp = !!(pc.buffer[3] & 0x80);
1024 set_disk_ro(floppy->disk, floppy->wp);
1025 page = &pc.buffer[8];
1026
1027 transfer_rate = be16_to_cpu(*(u16 *)&pc.buffer[8 + 2]);
1028 sector_size = be16_to_cpu(*(u16 *)&pc.buffer[8 + 6]);
1029 cyls = be16_to_cpu(*(u16 *)&pc.buffer[8 + 8]);
1030 rpm = be16_to_cpu(*(u16 *)&pc.buffer[8 + 28]);
1031 heads = pc.buffer[8 + 4];
1032 sectors = pc.buffer[8 + 5];
1033
1034 capacity = cyls * heads * sectors * sector_size;
1035
1036 if (memcmp(page, &floppy->flexible_disk_page, 32))
1037 printk(KERN_INFO "%s: %dkB, %d/%d/%d CHS, %d kBps, "
1038 "%d sector size, %d rpm\n",
1039 drive->name, capacity / 1024, cyls, heads,
1040 sectors, transfer_rate / 8, sector_size, rpm);
1041
1042 memcpy(&floppy->flexible_disk_page, page, 32);
1043 drive->bios_cyl = cyls;
1044 drive->bios_head = heads;
1045 drive->bios_sect = sectors;
1046 lba_capacity = floppy->blocks * floppy->block_size;
1047
1048 if (capacity < lba_capacity) {
1049 printk(KERN_NOTICE "%s: The disk reports a capacity of %d "
1050 "bytes, but the drive only handles %d\n",
1051 drive->name, lba_capacity, capacity);
1052 floppy->blocks = floppy->block_size ?
1053 capacity / floppy->block_size : 0;
1054 }
1055 return 0;
1056 }
1057
1058 static int idefloppy_get_sfrp_bit(ide_drive_t *drive)
1059 {
1060 idefloppy_floppy_t *floppy = drive->driver_data;
1061 idefloppy_pc_t pc;
1062
1063 floppy->srfp = 0;
1064 idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_CAPABILITIES_PAGE,
1065 MODE_SENSE_CURRENT);
1066
1067 set_bit(PC_SUPPRESS_ERROR, &pc.flags);
1068 if (idefloppy_queue_pc_tail(drive, &pc))
1069 return 1;
1070
1071 floppy->srfp = pc.buffer[8 + 2] & 0x40;
1072 return (0);
1073 }
1074
1075 /*
1076 * Determine if a media is present in the floppy drive, and if so, its LBA
1077 * capacity.
1078 */
1079 static int ide_floppy_get_capacity(ide_drive_t *drive)
1080 {
1081 idefloppy_floppy_t *floppy = drive->driver_data;
1082 idefloppy_pc_t pc;
1083 u8 *cap_desc;
1084 u8 header_len, desc_cnt;
1085 int i, rc = 1, blocks, length;
1086
1087 drive->bios_cyl = 0;
1088 drive->bios_head = drive->bios_sect = 0;
1089 floppy->blocks = 0;
1090 floppy->bs_factor = 1;
1091 set_capacity(floppy->disk, 0);
1092
1093 idefloppy_create_read_capacity_cmd(&pc);
1094 if (idefloppy_queue_pc_tail(drive, &pc)) {
1095 printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
1096 return 1;
1097 }
1098 header_len = pc.buffer[3];
1099 cap_desc = &pc.buffer[4];
1100 desc_cnt = header_len / 8; /* capacity descriptor of 8 bytes */
1101
1102 for (i = 0; i < desc_cnt; i++) {
1103 unsigned int desc_start = 4 + i*8;
1104
1105 blocks = be32_to_cpu(*(u32 *)&pc.buffer[desc_start]);
1106 length = be16_to_cpu(*(u16 *)&pc.buffer[desc_start + 6]);
1107
1108 debug_log("Descriptor %d: %dkB, %d blocks, %d sector size\n",
1109 i, blocks * length / 1024, blocks, length);
1110
1111 if (i)
1112 continue;
1113 /*
1114 * the code below is valid only for the 1st descriptor, ie i=0
1115 */
1116
1117 switch (pc.buffer[desc_start + 4] & 0x03) {
1118 /* Clik! drive returns this instead of CAPACITY_CURRENT */
1119 case CAPACITY_UNFORMATTED:
1120 if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags))
1121 /*
1122 * If it is not a clik drive, break out
1123 * (maintains previous driver behaviour)
1124 */
1125 break;
1126 case CAPACITY_CURRENT:
1127 /* Normal Zip/LS-120 disks */
1128 if (memcmp(cap_desc, &floppy->cap_desc, 8))
1129 printk(KERN_INFO "%s: %dkB, %d blocks, %d "
1130 "sector size\n", drive->name,
1131 blocks * length / 1024, blocks, length);
1132 memcpy(&floppy->cap_desc, cap_desc, 8);
1133
1134 if (!length || length % 512) {
1135 printk(KERN_NOTICE "%s: %d bytes block size "
1136 "not supported\n", drive->name, length);
1137 } else {
1138 floppy->blocks = blocks;
1139 floppy->block_size = length;
1140 floppy->bs_factor = length / 512;
1141 if (floppy->bs_factor != 1)
1142 printk(KERN_NOTICE "%s: warning: non "
1143 "512 bytes block size not "
1144 "fully supported\n",
1145 drive->name);
1146 rc = 0;
1147 }
1148 break;
1149 case CAPACITY_NO_CARTRIDGE:
1150 /*
1151 * This is a KERN_ERR so it appears on screen
1152 * for the user to see
1153 */
1154 printk(KERN_ERR "%s: No disk in drive\n", drive->name);
1155 break;
1156 case CAPACITY_INVALID:
1157 printk(KERN_ERR "%s: Invalid capacity for disk "
1158 "in drive\n", drive->name);
1159 break;
1160 }
1161 debug_log("Descriptor 0 Code: %d\n",
1162 pc.buffer[desc_start + 4] & 0x03);
1163 }
1164
1165 /* Clik! disk does not support get_flexible_disk_page */
1166 if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags))
1167 (void) ide_floppy_get_flexible_disk_page(drive);
1168
1169 set_capacity(floppy->disk, floppy->blocks * floppy->bs_factor);
1170 return rc;
1171 }
1172
1173 /*
1174 * Obtain the list of formattable capacities.
1175 * Very similar to ide_floppy_get_capacity, except that we push the capacity
1176 * descriptors to userland, instead of our own structures.
1177 *
1178 * Userland gives us the following structure:
1179 *
1180 * struct idefloppy_format_capacities {
1181 * int nformats;
1182 * struct {
1183 * int nblocks;
1184 * int blocksize;
1185 * } formats[];
1186 * };
1187 *
1188 * userland initializes nformats to the number of allocated formats[] records.
1189 * On exit we set nformats to the number of records we've actually initialized.
1190 */
1191
1192 static int ide_floppy_get_format_capacities(ide_drive_t *drive, int __user *arg)
1193 {
1194 idefloppy_pc_t pc;
1195 u8 header_len, desc_cnt;
1196 int i, blocks, length, u_array_size, u_index;
1197 int __user *argp;
1198
1199 if (get_user(u_array_size, arg))
1200 return (-EFAULT);
1201
1202 if (u_array_size <= 0)
1203 return (-EINVAL);
1204
1205 idefloppy_create_read_capacity_cmd(&pc);
1206 if (idefloppy_queue_pc_tail(drive, &pc)) {
1207 printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
1208 return (-EIO);
1209 }
1210 header_len = pc.buffer[3];
1211 desc_cnt = header_len / 8; /* capacity descriptor of 8 bytes */
1212
1213 u_index = 0;
1214 argp = arg + 1;
1215
1216 /*
1217 * We always skip the first capacity descriptor. That's the current
1218 * capacity. We are interested in the remaining descriptors, the
1219 * formattable capacities.
1220 */
1221 for (i = 1; i < desc_cnt; i++) {
1222 unsigned int desc_start = 4 + i*8;
1223
1224 if (u_index >= u_array_size)
1225 break; /* User-supplied buffer too small */
1226
1227 blocks = be32_to_cpu(*(u32 *)&pc.buffer[desc_start]);
1228 length = be16_to_cpu(*(u16 *)&pc.buffer[desc_start + 6]);
1229
1230 if (put_user(blocks, argp))
1231 return(-EFAULT);
1232 ++argp;
1233
1234 if (put_user(length, argp))
1235 return (-EFAULT);
1236 ++argp;
1237
1238 ++u_index;
1239 }
1240
1241 if (put_user(u_index, arg))
1242 return (-EFAULT);
1243 return (0);
1244 }
1245
1246 /*
1247 ** Get ATAPI_FORMAT_UNIT progress indication.
1248 **
1249 ** Userland gives a pointer to an int. The int is set to a progress
1250 ** indicator 0-65536, with 65536=100%.
1251 **
1252 ** If the drive does not support format progress indication, we just check
1253 ** the dsc bit, and return either 0 or 65536.
1254 */
1255
1256 static int idefloppy_get_format_progress(ide_drive_t *drive, int __user *arg)
1257 {
1258 idefloppy_floppy_t *floppy = drive->driver_data;
1259 idefloppy_pc_t pc;
1260 int progress_indication = 0x10000;
1261
1262 if (floppy->srfp) {
1263 idefloppy_create_request_sense_cmd(&pc);
1264 if (idefloppy_queue_pc_tail(drive, &pc)) {
1265 return (-EIO);
1266 }
1267
1268 if (floppy->sense_key == 2 &&
1269 floppy->asc == 4 &&
1270 floppy->ascq == 4) {
1271 progress_indication = floppy->progress_indication;
1272 }
1273 /* Else assume format_unit has finished, and we're
1274 ** at 0x10000 */
1275 } else {
1276 unsigned long flags;
1277 u8 stat;
1278
1279 local_irq_save(flags);
1280 stat = drive->hwif->INB(IDE_STATUS_REG);
1281 local_irq_restore(flags);
1282
1283 progress_indication = ((stat & SEEK_STAT) == 0) ? 0 : 0x10000;
1284 }
1285 if (put_user(progress_indication, arg))
1286 return (-EFAULT);
1287
1288 return (0);
1289 }
1290
1291 /*
1292 * Return the current floppy capacity.
1293 */
1294 static sector_t idefloppy_capacity (ide_drive_t *drive)
1295 {
1296 idefloppy_floppy_t *floppy = drive->driver_data;
1297 unsigned long capacity = floppy->blocks * floppy->bs_factor;
1298
1299 return capacity;
1300 }
1301
1302 /*
1303 * Check whether we can support a drive, based on the ATAPI IDENTIFY command
1304 * results.
1305 */
1306 static int idefloppy_identify_device(ide_drive_t *drive, struct hd_driveid *id)
1307 {
1308 struct idefloppy_id_gcw gcw;
1309
1310 *((u16 *) &gcw) = id->config;
1311
1312 #ifdef CONFIG_PPC
1313 /* kludge for Apple PowerBook internal zip */
1314 if ((gcw.device_type == 5) &&
1315 !strstr(id->model, "CD-ROM") &&
1316 strstr(id->model, "ZIP"))
1317 gcw.device_type = 0;
1318 #endif
1319
1320 if (gcw.protocol != 2)
1321 printk(KERN_ERR "ide-floppy: Protocol (0x%02x) is not ATAPI\n",
1322 gcw.protocol);
1323 else if (gcw.device_type != 0)
1324 printk(KERN_ERR "ide-floppy: Device type (0x%02x) is not set "
1325 "to floppy\n", gcw.device_type);
1326 else if (!gcw.removable)
1327 printk(KERN_ERR "ide-floppy: The removable flag is not set\n");
1328 else if (gcw.drq_type == 3) {
1329 printk(KERN_ERR "ide-floppy: Sorry, DRQ type (0x%02x) not "
1330 "supported\n", gcw.drq_type);
1331 } else if (gcw.packet_size != 0) {
1332 printk(KERN_ERR "ide-floppy: Packet size (0x%02x) is not 12 "
1333 "bytes long\n", gcw.packet_size);
1334 } else
1335 return 1;
1336 return 0;
1337 }
1338
1339 #ifdef CONFIG_IDE_PROC_FS
1340 static void idefloppy_add_settings(ide_drive_t *drive)
1341 {
1342 idefloppy_floppy_t *floppy = drive->driver_data;
1343
1344 /*
1345 * drive setting name read/write data type min max mul_factor div_factor data pointer set function
1346 */
1347 ide_add_setting(drive, "bios_cyl", SETTING_RW, TYPE_INT, 0, 1023, 1, 1, &drive->bios_cyl, NULL);
1348 ide_add_setting(drive, "bios_head", SETTING_RW, TYPE_BYTE, 0, 255, 1, 1, &drive->bios_head, NULL);
1349 ide_add_setting(drive, "bios_sect", SETTING_RW, TYPE_BYTE, 0, 63, 1, 1, &drive->bios_sect, NULL);
1350 ide_add_setting(drive, "ticks", SETTING_RW, TYPE_BYTE, 0, 255, 1, 1, &floppy->ticks, NULL);
1351 }
1352 #else
1353 static inline void idefloppy_add_settings(ide_drive_t *drive) { ; }
1354 #endif
1355
1356 /*
1357 * Driver initialization.
1358 */
1359 static void idefloppy_setup (ide_drive_t *drive, idefloppy_floppy_t *floppy)
1360 {
1361 struct idefloppy_id_gcw gcw;
1362
1363 *((u16 *) &gcw) = drive->id->config;
1364 floppy->pc = floppy->pc_stack;
1365 if (gcw.drq_type == 1)
1366 set_bit(IDEFLOPPY_DRQ_INTERRUPT, &floppy->flags);
1367 /*
1368 * We used to check revisions here. At this point however
1369 * I'm giving up. Just assume they are all broken, its easier.
1370 *
1371 * The actual reason for the workarounds was likely
1372 * a driver bug after all rather than a firmware bug,
1373 * and the workaround below used to hide it. It should
1374 * be fixed as of version 1.9, but to be on the safe side
1375 * we'll leave the limitation below for the 2.2.x tree.
1376 */
1377
1378 if (!strncmp(drive->id->model, "IOMEGA ZIP 100 ATAPI", 20)) {
1379 set_bit(IDEFLOPPY_ZIP_DRIVE, &floppy->flags);
1380 /* This value will be visible in the /proc/ide/hdx/settings */
1381 floppy->ticks = IDEFLOPPY_TICKS_DELAY;
1382 blk_queue_max_sectors(drive->queue, 64);
1383 }
1384
1385 /*
1386 * Guess what? The IOMEGA Clik! drive also needs the
1387 * above fix. It makes nasty clicking noises without
1388 * it, so please don't remove this.
1389 */
1390 if (strncmp(drive->id->model, "IOMEGA Clik!", 11) == 0) {
1391 blk_queue_max_sectors(drive->queue, 64);
1392 set_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags);
1393 }
1394
1395
1396 (void) ide_floppy_get_capacity(drive);
1397 idefloppy_add_settings(drive);
1398 }
1399
1400 static void ide_floppy_remove(ide_drive_t *drive)
1401 {
1402 idefloppy_floppy_t *floppy = drive->driver_data;
1403 struct gendisk *g = floppy->disk;
1404
1405 ide_proc_unregister_driver(drive, floppy->driver);
1406
1407 del_gendisk(g);
1408
1409 ide_floppy_put(floppy);
1410 }
1411
1412 static void idefloppy_cleanup_obj(struct kref *kref)
1413 {
1414 struct ide_floppy_obj *floppy = to_ide_floppy(kref);
1415 ide_drive_t *drive = floppy->drive;
1416 struct gendisk *g = floppy->disk;
1417
1418 drive->driver_data = NULL;
1419 g->private_data = NULL;
1420 put_disk(g);
1421 kfree(floppy);
1422 }
1423
1424 #ifdef CONFIG_IDE_PROC_FS
1425 static int proc_idefloppy_read_capacity
1426 (char *page, char **start, off_t off, int count, int *eof, void *data)
1427 {
1428 ide_drive_t*drive = (ide_drive_t *)data;
1429 int len;
1430
1431 len = sprintf(page,"%llu\n", (long long)idefloppy_capacity(drive));
1432 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
1433 }
1434
1435 static ide_proc_entry_t idefloppy_proc[] = {
1436 { "capacity", S_IFREG|S_IRUGO, proc_idefloppy_read_capacity, NULL },
1437 { "geometry", S_IFREG|S_IRUGO, proc_ide_read_geometry, NULL },
1438 { NULL, 0, NULL, NULL }
1439 };
1440 #endif /* CONFIG_IDE_PROC_FS */
1441
1442 static int ide_floppy_probe(ide_drive_t *);
1443
1444 static ide_driver_t idefloppy_driver = {
1445 .gen_driver = {
1446 .owner = THIS_MODULE,
1447 .name = "ide-floppy",
1448 .bus = &ide_bus_type,
1449 },
1450 .probe = ide_floppy_probe,
1451 .remove = ide_floppy_remove,
1452 .version = IDEFLOPPY_VERSION,
1453 .media = ide_floppy,
1454 .supports_dsc_overlap = 0,
1455 .do_request = idefloppy_do_request,
1456 .end_request = idefloppy_do_end_request,
1457 .error = __ide_error,
1458 .abort = __ide_abort,
1459 #ifdef CONFIG_IDE_PROC_FS
1460 .proc = idefloppy_proc,
1461 #endif
1462 };
1463
1464 static int idefloppy_open(struct inode *inode, struct file *filp)
1465 {
1466 struct gendisk *disk = inode->i_bdev->bd_disk;
1467 struct ide_floppy_obj *floppy;
1468 ide_drive_t *drive;
1469 idefloppy_pc_t pc;
1470 int ret = 0;
1471
1472 debug_log("Reached %s\n", __func__);
1473
1474 if (!(floppy = ide_floppy_get(disk)))
1475 return -ENXIO;
1476
1477 drive = floppy->drive;
1478
1479 floppy->openers++;
1480
1481 if (floppy->openers == 1) {
1482 clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags);
1483 /* Just in case */
1484
1485 idefloppy_create_test_unit_ready_cmd(&pc);
1486 if (idefloppy_queue_pc_tail(drive, &pc)) {
1487 idefloppy_create_start_stop_cmd(&pc, 1);
1488 (void) idefloppy_queue_pc_tail(drive, &pc);
1489 }
1490
1491 if (ide_floppy_get_capacity(drive)
1492 && (filp->f_flags & O_NDELAY) == 0
1493 /*
1494 ** Allow O_NDELAY to open a drive without a disk, or with
1495 ** an unreadable disk, so that we can get the format
1496 ** capacity of the drive or begin the format - Sam
1497 */
1498 ) {
1499 ret = -EIO;
1500 goto out_put_floppy;
1501 }
1502
1503 if (floppy->wp && (filp->f_mode & 2)) {
1504 ret = -EROFS;
1505 goto out_put_floppy;
1506 }
1507 set_bit(IDEFLOPPY_MEDIA_CHANGED, &floppy->flags);
1508 /* IOMEGA Clik! drives do not support lock/unlock commands */
1509 if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags)) {
1510 idefloppy_create_prevent_cmd(&pc, 1);
1511 (void) idefloppy_queue_pc_tail(drive, &pc);
1512 }
1513 check_disk_change(inode->i_bdev);
1514 } else if (test_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags)) {
1515 ret = -EBUSY;
1516 goto out_put_floppy;
1517 }
1518 return 0;
1519
1520 out_put_floppy:
1521 floppy->openers--;
1522 ide_floppy_put(floppy);
1523 return ret;
1524 }
1525
1526 static int idefloppy_release(struct inode *inode, struct file *filp)
1527 {
1528 struct gendisk *disk = inode->i_bdev->bd_disk;
1529 struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1530 ide_drive_t *drive = floppy->drive;
1531 idefloppy_pc_t pc;
1532
1533 debug_log("Reached %s\n", __func__);
1534
1535 if (floppy->openers == 1) {
1536 /* IOMEGA Clik! drives do not support lock/unlock commands */
1537 if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags)) {
1538 idefloppy_create_prevent_cmd(&pc, 0);
1539 (void) idefloppy_queue_pc_tail(drive, &pc);
1540 }
1541
1542 clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags);
1543 }
1544
1545 floppy->openers--;
1546
1547 ide_floppy_put(floppy);
1548
1549 return 0;
1550 }
1551
1552 static int idefloppy_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1553 {
1554 struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk);
1555 ide_drive_t *drive = floppy->drive;
1556
1557 geo->heads = drive->bios_head;
1558 geo->sectors = drive->bios_sect;
1559 geo->cylinders = (u16)drive->bios_cyl; /* truncate */
1560 return 0;
1561 }
1562
1563 static int ide_floppy_lockdoor(idefloppy_floppy_t *floppy, idefloppy_pc_t *pc,
1564 unsigned long arg, unsigned int cmd)
1565 {
1566 if (floppy->openers > 1)
1567 return -EBUSY;
1568
1569 /* The IOMEGA Clik! Drive doesn't support this command -
1570 * no room for an eject mechanism */
1571 if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags)) {
1572 int prevent = arg ? 1 : 0;
1573
1574 if (cmd == CDROMEJECT)
1575 prevent = 0;
1576
1577 idefloppy_create_prevent_cmd(pc, prevent);
1578 (void) idefloppy_queue_pc_tail(floppy->drive, pc);
1579 }
1580
1581 if (cmd == CDROMEJECT) {
1582 idefloppy_create_start_stop_cmd(pc, 2);
1583 (void) idefloppy_queue_pc_tail(floppy->drive, pc);
1584 }
1585
1586 return 0;
1587 }
1588
1589 static int ide_floppy_format_unit(idefloppy_floppy_t *floppy,
1590 int __user *arg)
1591 {
1592 int blocks, length, flags, err = 0;
1593 idefloppy_pc_t pc;
1594
1595 if (floppy->openers > 1) {
1596 /* Don't format if someone is using the disk */
1597 clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags);
1598 return -EBUSY;
1599 }
1600
1601 set_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags);
1602
1603 /*
1604 * Send ATAPI_FORMAT_UNIT to the drive.
1605 *
1606 * Userland gives us the following structure:
1607 *
1608 * struct idefloppy_format_command {
1609 * int nblocks;
1610 * int blocksize;
1611 * int flags;
1612 * } ;
1613 *
1614 * flags is a bitmask, currently, the only defined flag is:
1615 *
1616 * 0x01 - verify media after format.
1617 */
1618 if (get_user(blocks, arg) ||
1619 get_user(length, arg+1) ||
1620 get_user(flags, arg+2)) {
1621 err = -EFAULT;
1622 goto out;
1623 }
1624
1625 (void) idefloppy_get_sfrp_bit(floppy->drive);
1626 idefloppy_create_format_unit_cmd(&pc, blocks, length, flags);
1627
1628 if (idefloppy_queue_pc_tail(floppy->drive, &pc))
1629 err = -EIO;
1630
1631 out:
1632 if (err)
1633 clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags);
1634 return err;
1635 }
1636
1637
1638 static int idefloppy_ioctl(struct inode *inode, struct file *file,
1639 unsigned int cmd, unsigned long arg)
1640 {
1641 struct block_device *bdev = inode->i_bdev;
1642 struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk);
1643 ide_drive_t *drive = floppy->drive;
1644 idefloppy_pc_t pc;
1645 void __user *argp = (void __user *)arg;
1646 int err;
1647
1648 switch (cmd) {
1649 case CDROMEJECT:
1650 /* fall through */
1651 case CDROM_LOCKDOOR:
1652 return ide_floppy_lockdoor(floppy, &pc, arg, cmd);
1653 case IDEFLOPPY_IOCTL_FORMAT_SUPPORTED:
1654 return 0;
1655 case IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY:
1656 return ide_floppy_get_format_capacities(drive, argp);
1657 case IDEFLOPPY_IOCTL_FORMAT_START:
1658 if (!(file->f_mode & 2))
1659 return -EPERM;
1660
1661 return ide_floppy_format_unit(floppy, (int __user *)arg);
1662 case IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS:
1663 return idefloppy_get_format_progress(drive, argp);
1664 }
1665
1666 /*
1667 * skip SCSI_IOCTL_SEND_COMMAND (deprecated)
1668 * and CDROM_SEND_PACKET (legacy) ioctls
1669 */
1670 if (cmd != CDROM_SEND_PACKET && cmd != SCSI_IOCTL_SEND_COMMAND)
1671 err = scsi_cmd_ioctl(file, bdev->bd_disk->queue,
1672 bdev->bd_disk, cmd, argp);
1673 else
1674 err = -ENOTTY;
1675
1676 if (err == -ENOTTY)
1677 err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
1678
1679 return err;
1680 }
1681
1682 static int idefloppy_media_changed(struct gendisk *disk)
1683 {
1684 struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1685 ide_drive_t *drive = floppy->drive;
1686
1687 /* do not scan partitions twice if this is a removable device */
1688 if (drive->attach) {
1689 drive->attach = 0;
1690 return 0;
1691 }
1692 return test_and_clear_bit(IDEFLOPPY_MEDIA_CHANGED, &floppy->flags);
1693 }
1694
1695 static int idefloppy_revalidate_disk(struct gendisk *disk)
1696 {
1697 struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1698 set_capacity(disk, idefloppy_capacity(floppy->drive));
1699 return 0;
1700 }
1701
1702 static struct block_device_operations idefloppy_ops = {
1703 .owner = THIS_MODULE,
1704 .open = idefloppy_open,
1705 .release = idefloppy_release,
1706 .ioctl = idefloppy_ioctl,
1707 .getgeo = idefloppy_getgeo,
1708 .media_changed = idefloppy_media_changed,
1709 .revalidate_disk= idefloppy_revalidate_disk
1710 };
1711
1712 static int ide_floppy_probe(ide_drive_t *drive)
1713 {
1714 idefloppy_floppy_t *floppy;
1715 struct gendisk *g;
1716
1717 if (!strstr("ide-floppy", drive->driver_req))
1718 goto failed;
1719 if (!drive->present)
1720 goto failed;
1721 if (drive->media != ide_floppy)
1722 goto failed;
1723 if (!idefloppy_identify_device (drive, drive->id)) {
1724 printk (KERN_ERR "ide-floppy: %s: not supported by this version of ide-floppy\n", drive->name);
1725 goto failed;
1726 }
1727 if (drive->scsi) {
1728 printk("ide-floppy: passing drive %s to ide-scsi emulation.\n", drive->name);
1729 goto failed;
1730 }
1731 if ((floppy = kzalloc(sizeof (idefloppy_floppy_t), GFP_KERNEL)) == NULL) {
1732 printk (KERN_ERR "ide-floppy: %s: Can't allocate a floppy structure\n", drive->name);
1733 goto failed;
1734 }
1735
1736 g = alloc_disk(1 << PARTN_BITS);
1737 if (!g)
1738 goto out_free_floppy;
1739
1740 ide_init_disk(g, drive);
1741
1742 ide_proc_register_driver(drive, &idefloppy_driver);
1743
1744 kref_init(&floppy->kref);
1745
1746 floppy->drive = drive;
1747 floppy->driver = &idefloppy_driver;
1748 floppy->disk = g;
1749
1750 g->private_data = &floppy->driver;
1751
1752 drive->driver_data = floppy;
1753
1754 idefloppy_setup (drive, floppy);
1755
1756 g->minors = 1 << PARTN_BITS;
1757 g->driverfs_dev = &drive->gendev;
1758 g->flags = drive->removable ? GENHD_FL_REMOVABLE : 0;
1759 g->fops = &idefloppy_ops;
1760 drive->attach = 1;
1761 add_disk(g);
1762 return 0;
1763
1764 out_free_floppy:
1765 kfree(floppy);
1766 failed:
1767 return -ENODEV;
1768 }
1769
1770 MODULE_DESCRIPTION("ATAPI FLOPPY Driver");
1771
1772 static void __exit idefloppy_exit (void)
1773 {
1774 driver_unregister(&idefloppy_driver.gen_driver);
1775 }
1776
1777 static int __init idefloppy_init(void)
1778 {
1779 printk("ide-floppy driver " IDEFLOPPY_VERSION "\n");
1780 return driver_register(&idefloppy_driver.gen_driver);
1781 }
1782
1783 MODULE_ALIAS("ide:*m-floppy*");
1784 module_init(idefloppy_init);
1785 module_exit(idefloppy_exit);
1786 MODULE_LICENSE("GPL");
This page took 0.12438 seconds and 5 git commands to generate.