]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/ide/ide-floppy.c
ide-floppy: convert driver to the new debugging macro
[linux-2.6-omap-h63xx.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  * This driver supports the following IDE floppy drives:
9  *
10  * LS-120/240 SuperDisk
11  * Iomega Zip 100/250
12  * Iomega PC Card Clik!/PocketZip
13  *
14  * For a historical changelog see
15  * Documentation/ide/ChangeLog.ide-floppy.1996-2002
16  */
17
18 #define DRV_NAME "ide-floppy"
19 #define PFX DRV_NAME ": "
20
21 #define IDEFLOPPY_VERSION "1.00"
22
23 #include <linux/module.h>
24 #include <linux/types.h>
25 #include <linux/string.h>
26 #include <linux/kernel.h>
27 #include <linux/delay.h>
28 #include <linux/timer.h>
29 #include <linux/mm.h>
30 #include <linux/interrupt.h>
31 #include <linux/major.h>
32 #include <linux/errno.h>
33 #include <linux/genhd.h>
34 #include <linux/slab.h>
35 #include <linux/cdrom.h>
36 #include <linux/ide.h>
37 #include <linux/hdreg.h>
38 #include <linux/bitops.h>
39 #include <linux/mutex.h>
40 #include <linux/scatterlist.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 #include "ide-floppy.h"
51
52 /* define to see debug info */
53 #define IDEFLOPPY_DEBUG_LOG     0
54
55 #if IDEFLOPPY_DEBUG_LOG
56 #define ide_debug_log(lvl, fmt, args...) __ide_debug_log(lvl, fmt, args)
57 #else
58 #define ide_debug_log(lvl, fmt, args...) do {} while (0)
59 #endif
60
61 /*
62  * After each failed packet command we issue a request sense command and retry
63  * the packet command IDEFLOPPY_MAX_PC_RETRIES times.
64  */
65 #define IDEFLOPPY_MAX_PC_RETRIES        3
66
67 /* format capacities descriptor codes */
68 #define CAPACITY_INVALID        0x00
69 #define CAPACITY_UNFORMATTED    0x01
70 #define CAPACITY_CURRENT        0x02
71 #define CAPACITY_NO_CARTRIDGE   0x03
72
73 /*
74  * The following delay solves a problem with ATAPI Zip 100 drive where BSY bit
75  * was apparently being deasserted before the unit was ready to receive data.
76  */
77 #define IDEFLOPPY_PC_DELAY      (HZ/20) /* default delay for ZIP 100 (50ms) */
78
79 /* Error code returned in rq->errors to the higher part of the driver. */
80 #define IDEFLOPPY_ERROR_GENERAL         101
81
82 static DEFINE_MUTEX(idefloppy_ref_mutex);
83
84 static void idefloppy_cleanup_obj(struct kref *);
85
86 static struct ide_floppy_obj *ide_floppy_get(struct gendisk *disk)
87 {
88         struct ide_floppy_obj *floppy = NULL;
89
90         mutex_lock(&idefloppy_ref_mutex);
91         floppy = ide_drv_g(disk, ide_floppy_obj);
92         if (floppy) {
93                 if (ide_device_get(floppy->drive))
94                         floppy = NULL;
95                 else
96                         kref_get(&floppy->kref);
97         }
98         mutex_unlock(&idefloppy_ref_mutex);
99         return floppy;
100 }
101
102 static void ide_floppy_put(struct ide_floppy_obj *floppy)
103 {
104         ide_drive_t *drive = floppy->drive;
105
106         mutex_lock(&idefloppy_ref_mutex);
107         kref_put(&floppy->kref, idefloppy_cleanup_obj);
108         ide_device_put(drive);
109         mutex_unlock(&idefloppy_ref_mutex);
110 }
111
112 /*
113  * Used to finish servicing a request. For read/write requests, we will call
114  * ide_end_request to pass to the next buffer.
115  */
116 static int idefloppy_end_request(ide_drive_t *drive, int uptodate, int nsecs)
117 {
118         idefloppy_floppy_t *floppy = drive->driver_data;
119         struct request *rq = HWGROUP(drive)->rq;
120         int error;
121
122         ide_debug_log(IDE_DBG_FUNC, "Call %s\n", __func__);
123
124         switch (uptodate) {
125         case 0:
126                 error = IDEFLOPPY_ERROR_GENERAL;
127                 break;
128
129         case 1:
130                 error = 0;
131                 break;
132
133         default:
134                 error = uptodate;
135         }
136
137         if (error)
138                 floppy->failed_pc = NULL;
139         /* Why does this happen? */
140         if (!rq)
141                 return 0;
142         if (!blk_special_request(rq)) {
143                 /* our real local end request function */
144                 ide_end_request(drive, uptodate, nsecs);
145                 return 0;
146         }
147         rq->errors = error;
148         /* fixme: need to move this local also */
149         ide_end_drive_cmd(drive, 0, 0);
150         return 0;
151 }
152
153 static void idefloppy_update_buffers(ide_drive_t *drive,
154                                 struct ide_atapi_pc *pc)
155 {
156         struct request *rq = pc->rq;
157         struct bio *bio = rq->bio;
158
159         while ((bio = rq->bio) != NULL)
160                 idefloppy_end_request(drive, 1, 0);
161 }
162
163 static void ide_floppy_callback(ide_drive_t *drive, int dsc)
164 {
165         idefloppy_floppy_t *floppy = drive->driver_data;
166         struct ide_atapi_pc *pc = drive->pc;
167         int uptodate = pc->error ? 0 : 1;
168
169         ide_debug_log(IDE_DBG_FUNC, "Call %s\n", __func__);
170
171         if (floppy->failed_pc == pc)
172                 floppy->failed_pc = NULL;
173
174         if (pc->c[0] == GPCMD_READ_10 || pc->c[0] == GPCMD_WRITE_10 ||
175             (pc->rq && blk_pc_request(pc->rq)))
176                 uptodate = 1; /* FIXME */
177         else if (pc->c[0] == GPCMD_REQUEST_SENSE) {
178                 u8 *buf = pc->buf;
179
180                 if (!pc->error) {
181                         floppy->sense_key = buf[2] & 0x0F;
182                         floppy->asc = buf[12];
183                         floppy->ascq = buf[13];
184                         floppy->progress_indication = buf[15] & 0x80 ?
185                                 (u16)get_unaligned((u16 *)&buf[16]) : 0x10000;
186
187                         if (floppy->failed_pc)
188                                 ide_debug_log(IDE_DBG_PC, "pc = %x, ",
189                                               floppy->failed_pc->c[0]);
190
191                         ide_debug_log(IDE_DBG_SENSE, "sense key = %x, asc = %x,"
192                                       "ascq = %x\n", floppy->sense_key,
193                                       floppy->asc, floppy->ascq);
194                 } else
195                         printk(KERN_ERR PFX "Error in REQUEST SENSE itself - "
196                                "Aborting request!\n");
197         }
198
199         idefloppy_end_request(drive, uptodate, 0);
200 }
201
202 static void ide_floppy_report_error(idefloppy_floppy_t *floppy,
203                                     struct ide_atapi_pc *pc)
204 {
205         /* supress error messages resulting from Medium not present */
206         if (floppy->sense_key == 0x02 &&
207             floppy->asc       == 0x3a &&
208             floppy->ascq      == 0x00)
209                 return;
210
211         printk(KERN_ERR PFX "%s: I/O error, pc = %2x, key = %2x, "
212                         "asc = %2x, ascq = %2x\n",
213                         floppy->drive->name, pc->c[0], floppy->sense_key,
214                         floppy->asc, floppy->ascq);
215
216 }
217
218 static ide_startstop_t idefloppy_issue_pc(ide_drive_t *drive,
219                 struct ide_atapi_pc *pc)
220 {
221         idefloppy_floppy_t *floppy = drive->driver_data;
222
223         if (floppy->failed_pc == NULL &&
224             pc->c[0] != GPCMD_REQUEST_SENSE)
225                 floppy->failed_pc = pc;
226
227         /* Set the current packet command */
228         drive->pc = pc;
229
230         if (pc->retries > IDEFLOPPY_MAX_PC_RETRIES) {
231                 if (!(pc->flags & PC_FLAG_SUPPRESS_ERROR))
232                         ide_floppy_report_error(floppy, pc);
233                 /* Giving up */
234                 pc->error = IDEFLOPPY_ERROR_GENERAL;
235
236                 floppy->failed_pc = NULL;
237                 drive->pc_callback(drive, 0);
238                 return ide_stopped;
239         }
240
241         ide_debug_log(IDE_DBG_FUNC, "%s: Retry #%d\n", __func__, pc->retries);
242
243         pc->retries++;
244
245         return ide_issue_pc(drive, WAIT_FLOPPY_CMD, NULL);
246 }
247
248 void ide_floppy_create_read_capacity_cmd(struct ide_atapi_pc *pc)
249 {
250         ide_init_pc(pc);
251         pc->c[0] = GPCMD_READ_FORMAT_CAPACITIES;
252         pc->c[7] = 255;
253         pc->c[8] = 255;
254         pc->req_xfer = 255;
255 }
256
257 /* A mode sense command is used to "sense" floppy parameters. */
258 void ide_floppy_create_mode_sense_cmd(struct ide_atapi_pc *pc, u8 page_code)
259 {
260         u16 length = 8; /* sizeof(Mode Parameter Header) = 8 Bytes */
261
262         ide_init_pc(pc);
263         pc->c[0] = GPCMD_MODE_SENSE_10;
264         pc->c[1] = 0;
265         pc->c[2] = page_code;
266
267         switch (page_code) {
268         case IDEFLOPPY_CAPABILITIES_PAGE:
269                 length += 12;
270                 break;
271         case IDEFLOPPY_FLEXIBLE_DISK_PAGE:
272                 length += 32;
273                 break;
274         default:
275                 printk(KERN_ERR PFX "unsupported page code in %s\n", __func__);
276         }
277         put_unaligned(cpu_to_be16(length), (u16 *) &pc->c[7]);
278         pc->req_xfer = length;
279 }
280
281 static void idefloppy_create_rw_cmd(ide_drive_t *drive,
282                                     struct ide_atapi_pc *pc, struct request *rq,
283                                     unsigned long sector)
284 {
285         idefloppy_floppy_t *floppy = drive->driver_data;
286         int block = sector / floppy->bs_factor;
287         int blocks = rq->nr_sectors / floppy->bs_factor;
288         int cmd = rq_data_dir(rq);
289
290         ide_debug_log(IDE_DBG_FUNC, "%s: block: %d, blocks: %d\n", __func__,
291                       block, blocks);
292
293         ide_init_pc(pc);
294         pc->c[0] = cmd == READ ? GPCMD_READ_10 : GPCMD_WRITE_10;
295         put_unaligned(cpu_to_be16(blocks), (unsigned short *)&pc->c[7]);
296         put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[2]);
297
298         memcpy(rq->cmd, pc->c, 12);
299
300         pc->rq = rq;
301         pc->b_count = 0;
302         if (rq->cmd_flags & REQ_RW)
303                 pc->flags |= PC_FLAG_WRITING;
304         pc->buf = NULL;
305         pc->req_xfer = pc->buf_size = blocks * floppy->block_size;
306         pc->flags |= PC_FLAG_DMA_OK;
307 }
308
309 static void idefloppy_blockpc_cmd(idefloppy_floppy_t *floppy,
310                 struct ide_atapi_pc *pc, struct request *rq)
311 {
312         ide_init_pc(pc);
313         memcpy(pc->c, rq->cmd, sizeof(pc->c));
314         pc->rq = rq;
315         pc->b_count = 0;
316         if (rq->data_len && rq_data_dir(rq) == WRITE)
317                 pc->flags |= PC_FLAG_WRITING;
318         pc->buf = rq->data;
319         if (rq->bio)
320                 pc->flags |= PC_FLAG_DMA_OK;
321         /*
322          * possibly problematic, doesn't look like ide-floppy correctly
323          * handled scattered requests if dma fails...
324          */
325         pc->req_xfer = pc->buf_size = rq->data_len;
326 }
327
328 static ide_startstop_t idefloppy_do_request(ide_drive_t *drive,
329                 struct request *rq, sector_t block_s)
330 {
331         idefloppy_floppy_t *floppy = drive->driver_data;
332         ide_hwif_t *hwif = drive->hwif;
333         struct ide_atapi_pc *pc;
334         unsigned long block = (unsigned long)block_s;
335
336         ide_debug_log(IDE_DBG_FUNC, "%s: dev: %s, cmd: 0x%x, cmd_type: %x, "
337                       "errors: %d\n",
338                       __func__, rq->rq_disk ? rq->rq_disk->disk_name : "?",
339                       rq->cmd[0], rq->cmd_type, rq->errors);
340
341         ide_debug_log(IDE_DBG_FUNC, "%s: sector: %ld, nr_sectors: %ld, "
342                       "current_nr_sectors: %d\n",
343                       __func__, (long)rq->sector, rq->nr_sectors,
344                       rq->current_nr_sectors);
345
346         if (rq->errors >= ERROR_MAX) {
347                 if (floppy->failed_pc)
348                         ide_floppy_report_error(floppy, floppy->failed_pc);
349                 else
350                         printk(KERN_ERR PFX "%s: I/O error\n", drive->name);
351
352                 idefloppy_end_request(drive, 0, 0);
353                 return ide_stopped;
354         }
355         if (blk_fs_request(rq)) {
356                 if (((long)rq->sector % floppy->bs_factor) ||
357                     (rq->nr_sectors % floppy->bs_factor)) {
358                         printk(KERN_ERR PFX "%s: unsupported r/w rq size\n",
359                                 drive->name);
360                         idefloppy_end_request(drive, 0, 0);
361                         return ide_stopped;
362                 }
363                 pc = &floppy->queued_pc;
364                 idefloppy_create_rw_cmd(drive, pc, rq, block);
365         } else if (blk_special_request(rq)) {
366                 pc = (struct ide_atapi_pc *) rq->buffer;
367         } else if (blk_pc_request(rq)) {
368                 pc = &floppy->queued_pc;
369                 idefloppy_blockpc_cmd(floppy, pc, rq);
370         } else {
371                 blk_dump_rq_flags(rq, PFX "unsupported command in queue");
372                 idefloppy_end_request(drive, 0, 0);
373                 return ide_stopped;
374         }
375
376         ide_init_sg_cmd(drive, rq);
377         ide_map_sg(drive, rq);
378
379         pc->sg = hwif->sg_table;
380         pc->sg_cnt = hwif->sg_nents;
381
382         pc->rq = rq;
383
384         return idefloppy_issue_pc(drive, pc);
385 }
386
387 /*
388  * Look at the flexible disk page parameters. We ignore the CHS capacity
389  * parameters and use the LBA parameters instead.
390  */
391 static int ide_floppy_get_flexible_disk_page(ide_drive_t *drive)
392 {
393         idefloppy_floppy_t *floppy = drive->driver_data;
394         struct gendisk *disk = floppy->disk;
395         struct ide_atapi_pc pc;
396         u8 *page;
397         int capacity, lba_capacity;
398         u16 transfer_rate, sector_size, cyls, rpm;
399         u8 heads, sectors;
400
401         ide_floppy_create_mode_sense_cmd(&pc, IDEFLOPPY_FLEXIBLE_DISK_PAGE);
402
403         if (ide_queue_pc_tail(drive, disk, &pc)) {
404                 printk(KERN_ERR PFX "Can't get flexible disk page params\n");
405                 return 1;
406         }
407
408         if (pc.buf[3] & 0x80)
409                 drive->atapi_flags |= IDE_AFLAG_WP;
410         else
411                 drive->atapi_flags &= ~IDE_AFLAG_WP;
412
413         set_disk_ro(disk, !!(drive->atapi_flags & IDE_AFLAG_WP));
414
415         page = &pc.buf[8];
416
417         transfer_rate = be16_to_cpup((__be16 *)&pc.buf[8 + 2]);
418         sector_size   = be16_to_cpup((__be16 *)&pc.buf[8 + 6]);
419         cyls          = be16_to_cpup((__be16 *)&pc.buf[8 + 8]);
420         rpm           = be16_to_cpup((__be16 *)&pc.buf[8 + 28]);
421         heads         = pc.buf[8 + 4];
422         sectors       = pc.buf[8 + 5];
423
424         capacity = cyls * heads * sectors * sector_size;
425
426         if (memcmp(page, &floppy->flexible_disk_page, 32))
427                 printk(KERN_INFO PFX "%s: %dkB, %d/%d/%d CHS, %d kBps, "
428                                 "%d sector size, %d rpm\n",
429                                 drive->name, capacity / 1024, cyls, heads,
430                                 sectors, transfer_rate / 8, sector_size, rpm);
431
432         memcpy(&floppy->flexible_disk_page, page, 32);
433         drive->bios_cyl = cyls;
434         drive->bios_head = heads;
435         drive->bios_sect = sectors;
436         lba_capacity = floppy->blocks * floppy->block_size;
437
438         if (capacity < lba_capacity) {
439                 printk(KERN_NOTICE PFX "%s: The disk reports a capacity of %d "
440                         "bytes, but the drive only handles %d\n",
441                         drive->name, lba_capacity, capacity);
442                 floppy->blocks = floppy->block_size ?
443                         capacity / floppy->block_size : 0;
444         }
445         return 0;
446 }
447
448 /*
449  * Determine if a media is present in the floppy drive, and if so, its LBA
450  * capacity.
451  */
452 static int ide_floppy_get_capacity(ide_drive_t *drive)
453 {
454         idefloppy_floppy_t *floppy = drive->driver_data;
455         struct gendisk *disk = floppy->disk;
456         struct ide_atapi_pc pc;
457         u8 *cap_desc;
458         u8 header_len, desc_cnt;
459         int i, rc = 1, blocks, length;
460
461         drive->bios_cyl = 0;
462         drive->bios_head = drive->bios_sect = 0;
463         floppy->blocks = 0;
464         floppy->bs_factor = 1;
465         set_capacity(floppy->disk, 0);
466
467         ide_floppy_create_read_capacity_cmd(&pc);
468         if (ide_queue_pc_tail(drive, disk, &pc)) {
469                 printk(KERN_ERR PFX "Can't get floppy parameters\n");
470                 return 1;
471         }
472         header_len = pc.buf[3];
473         cap_desc = &pc.buf[4];
474         desc_cnt = header_len / 8; /* capacity descriptor of 8 bytes */
475
476         for (i = 0; i < desc_cnt; i++) {
477                 unsigned int desc_start = 4 + i*8;
478
479                 blocks = be32_to_cpup((__be32 *)&pc.buf[desc_start]);
480                 length = be16_to_cpup((__be16 *)&pc.buf[desc_start + 6]);
481
482                 ide_debug_log(IDE_DBG_PROBE, "Descriptor %d: %dkB, %d blocks, "
483                               "%d sector size\n",
484                               i, blocks * length / 1024, blocks, length);
485
486                 if (i)
487                         continue;
488                 /*
489                  * the code below is valid only for the 1st descriptor, ie i=0
490                  */
491
492                 switch (pc.buf[desc_start + 4] & 0x03) {
493                 /* Clik! drive returns this instead of CAPACITY_CURRENT */
494                 case CAPACITY_UNFORMATTED:
495                         if (!(drive->atapi_flags & IDE_AFLAG_CLIK_DRIVE))
496                                 /*
497                                  * If it is not a clik drive, break out
498                                  * (maintains previous driver behaviour)
499                                  */
500                                 break;
501                 case CAPACITY_CURRENT:
502                         /* Normal Zip/LS-120 disks */
503                         if (memcmp(cap_desc, &floppy->cap_desc, 8))
504                                 printk(KERN_INFO PFX "%s: %dkB, %d blocks, %d "
505                                        "sector size\n",
506                                        drive->name, blocks * length / 1024,
507                                        blocks, length);
508                         memcpy(&floppy->cap_desc, cap_desc, 8);
509
510                         if (!length || length % 512) {
511                                 printk(KERN_NOTICE PFX "%s: %d bytes block size"
512                                        " not supported\n", drive->name, length);
513                         } else {
514                                 floppy->blocks = blocks;
515                                 floppy->block_size = length;
516                                 floppy->bs_factor = length / 512;
517                                 if (floppy->bs_factor != 1)
518                                         printk(KERN_NOTICE PFX "%s: Warning: "
519                                                "non 512 bytes block size not "
520                                                "fully supported\n",
521                                                drive->name);
522                                 rc = 0;
523                         }
524                         break;
525                 case CAPACITY_NO_CARTRIDGE:
526                         /*
527                          * This is a KERN_ERR so it appears on screen
528                          * for the user to see
529                          */
530                         printk(KERN_ERR PFX "%s: No disk in drive\n",
531                                drive->name);
532                         break;
533                 case CAPACITY_INVALID:
534                         printk(KERN_ERR PFX "%s: Invalid capacity for disk "
535                                 "in drive\n", drive->name);
536                         break;
537                 }
538                 ide_debug_log(IDE_DBG_PROBE, "Descriptor 0 Code: %d\n",
539                               pc.buf[desc_start + 4] & 0x03);
540         }
541
542         /* Clik! disk does not support get_flexible_disk_page */
543         if (!(drive->atapi_flags & IDE_AFLAG_CLIK_DRIVE))
544                 (void) ide_floppy_get_flexible_disk_page(drive);
545
546         set_capacity(disk, floppy->blocks * floppy->bs_factor);
547
548         return rc;
549 }
550
551 static sector_t idefloppy_capacity(ide_drive_t *drive)
552 {
553         idefloppy_floppy_t *floppy = drive->driver_data;
554         unsigned long capacity = floppy->blocks * floppy->bs_factor;
555
556         return capacity;
557 }
558
559 #ifdef CONFIG_IDE_PROC_FS
560 ide_devset_rw_field(bios_cyl, bios_cyl);
561 ide_devset_rw_field(bios_head, bios_head);
562 ide_devset_rw_field(bios_sect, bios_sect);
563 ide_devset_rw_field(ticks, pc_delay);
564
565 static const struct ide_proc_devset idefloppy_settings[] = {
566         IDE_PROC_DEVSET(bios_cyl,  0, 1023),
567         IDE_PROC_DEVSET(bios_head, 0,  255),
568         IDE_PROC_DEVSET(bios_sect, 0,   63),
569         IDE_PROC_DEVSET(ticks,     0,  255),
570         { 0 },
571 };
572 #endif
573
574 static void idefloppy_setup(ide_drive_t *drive, idefloppy_floppy_t *floppy)
575 {
576         u16 *id = drive->id;
577         u8 gcw[2];
578
579         *((u16 *)&gcw) = id[ATA_ID_CONFIG];
580
581         drive->pc_callback       = ide_floppy_callback;
582         drive->pc_update_buffers = idefloppy_update_buffers;
583         drive->pc_io_buffers     = ide_io_buffers;
584
585         if (((gcw[0] & 0x60) >> 5) == 1)
586                 drive->atapi_flags |= IDE_AFLAG_DRQ_INTERRUPT;
587         /*
588          * We used to check revisions here. At this point however I'm giving up.
589          * Just assume they are all broken, its easier.
590          *
591          * The actual reason for the workarounds was likely a driver bug after
592          * all rather than a firmware bug, and the workaround below used to hide
593          * it. It should be fixed as of version 1.9, but to be on the safe side
594          * we'll leave the limitation below for the 2.2.x tree.
595          */
596         if (!strncmp((char *)&id[ATA_ID_PROD], "IOMEGA ZIP 100 ATAPI", 20)) {
597                 drive->atapi_flags |= IDE_AFLAG_ZIP_DRIVE;
598                 /* This value will be visible in the /proc/ide/hdx/settings */
599                 drive->pc_delay = IDEFLOPPY_PC_DELAY;
600                 blk_queue_max_sectors(drive->queue, 64);
601         }
602
603         /*
604          * Guess what? The IOMEGA Clik! drive also needs the above fix. It makes
605          * nasty clicking noises without it, so please don't remove this.
606          */
607         if (strncmp((char *)&id[ATA_ID_PROD], "IOMEGA Clik!", 11) == 0) {
608                 blk_queue_max_sectors(drive->queue, 64);
609                 drive->atapi_flags |= IDE_AFLAG_CLIK_DRIVE;
610                 /* IOMEGA Clik! drives do not support lock/unlock commands */
611                 drive->atapi_flags |= IDE_AFLAG_NO_DOORLOCK;
612         }
613
614         (void) ide_floppy_get_capacity(drive);
615
616         ide_proc_register_driver(drive, floppy->driver);
617 }
618
619 static void ide_floppy_remove(ide_drive_t *drive)
620 {
621         idefloppy_floppy_t *floppy = drive->driver_data;
622         struct gendisk *g = floppy->disk;
623
624         ide_proc_unregister_driver(drive, floppy->driver);
625
626         del_gendisk(g);
627
628         ide_floppy_put(floppy);
629 }
630
631 static void idefloppy_cleanup_obj(struct kref *kref)
632 {
633         struct ide_floppy_obj *floppy = to_ide_drv(kref, ide_floppy_obj);
634         ide_drive_t *drive = floppy->drive;
635         struct gendisk *g = floppy->disk;
636
637         drive->driver_data = NULL;
638         g->private_data = NULL;
639         put_disk(g);
640         kfree(floppy);
641 }
642
643 #ifdef CONFIG_IDE_PROC_FS
644 static int proc_idefloppy_read_capacity(char *page, char **start, off_t off,
645                 int count, int *eof, void *data)
646 {
647         ide_drive_t*drive = (ide_drive_t *)data;
648         int len;
649
650         len = sprintf(page, "%llu\n", (long long)idefloppy_capacity(drive));
651         PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
652 }
653
654 static ide_proc_entry_t idefloppy_proc[] = {
655         { "capacity",   S_IFREG|S_IRUGO, proc_idefloppy_read_capacity,  NULL },
656         { "geometry",   S_IFREG|S_IRUGO, proc_ide_read_geometry,        NULL },
657         { NULL, 0, NULL, NULL }
658 };
659 #endif  /* CONFIG_IDE_PROC_FS */
660
661 static int ide_floppy_probe(ide_drive_t *);
662
663 static ide_driver_t idefloppy_driver = {
664         .gen_driver = {
665                 .owner          = THIS_MODULE,
666                 .name           = "ide-floppy",
667                 .bus            = &ide_bus_type,
668         },
669         .probe                  = ide_floppy_probe,
670         .remove                 = ide_floppy_remove,
671         .version                = IDEFLOPPY_VERSION,
672         .media                  = ide_floppy,
673         .do_request             = idefloppy_do_request,
674         .end_request            = idefloppy_end_request,
675         .error                  = __ide_error,
676 #ifdef CONFIG_IDE_PROC_FS
677         .proc                   = idefloppy_proc,
678         .settings               = idefloppy_settings,
679 #endif
680 };
681
682 static int idefloppy_open(struct inode *inode, struct file *filp)
683 {
684         struct gendisk *disk = inode->i_bdev->bd_disk;
685         struct ide_floppy_obj *floppy;
686         ide_drive_t *drive;
687         int ret = 0;
688
689         floppy = ide_floppy_get(disk);
690         if (!floppy)
691                 return -ENXIO;
692
693         drive = floppy->drive;
694
695         ide_debug_log(IDE_DBG_FUNC, "Call %s\n", __func__);
696
697         floppy->openers++;
698
699         if (floppy->openers == 1) {
700                 drive->atapi_flags &= ~IDE_AFLAG_FORMAT_IN_PROGRESS;
701                 /* Just in case */
702
703                 if (ide_do_test_unit_ready(drive, disk))
704                         ide_do_start_stop(drive, disk, 1);
705
706                 if (ide_floppy_get_capacity(drive)
707                    && (filp->f_flags & O_NDELAY) == 0
708                     /*
709                      * Allow O_NDELAY to open a drive without a disk, or with an
710                      * unreadable disk, so that we can get the format capacity
711                      * of the drive or begin the format - Sam
712                      */
713                     ) {
714                         ret = -EIO;
715                         goto out_put_floppy;
716                 }
717
718                 if ((drive->atapi_flags & IDE_AFLAG_WP) && (filp->f_mode & 2)) {
719                         ret = -EROFS;
720                         goto out_put_floppy;
721                 }
722
723                 drive->atapi_flags |= IDE_AFLAG_MEDIA_CHANGED;
724                 ide_set_media_lock(drive, disk, 1);
725                 check_disk_change(inode->i_bdev);
726         } else if (drive->atapi_flags & IDE_AFLAG_FORMAT_IN_PROGRESS) {
727                 ret = -EBUSY;
728                 goto out_put_floppy;
729         }
730         return 0;
731
732 out_put_floppy:
733         floppy->openers--;
734         ide_floppy_put(floppy);
735         return ret;
736 }
737
738 static int idefloppy_release(struct inode *inode, struct file *filp)
739 {
740         struct gendisk *disk = inode->i_bdev->bd_disk;
741         struct ide_floppy_obj *floppy = ide_drv_g(disk, ide_floppy_obj);
742         ide_drive_t *drive = floppy->drive;
743
744         ide_debug_log(IDE_DBG_FUNC, "Call %s\n", __func__);
745
746         if (floppy->openers == 1) {
747                 ide_set_media_lock(drive, disk, 0);
748                 drive->atapi_flags &= ~IDE_AFLAG_FORMAT_IN_PROGRESS;
749         }
750
751         floppy->openers--;
752
753         ide_floppy_put(floppy);
754
755         return 0;
756 }
757
758 static int idefloppy_getgeo(struct block_device *bdev, struct hd_geometry *geo)
759 {
760         struct ide_floppy_obj *floppy = ide_drv_g(bdev->bd_disk,
761                                                      ide_floppy_obj);
762         ide_drive_t *drive = floppy->drive;
763
764         geo->heads = drive->bios_head;
765         geo->sectors = drive->bios_sect;
766         geo->cylinders = (u16)drive->bios_cyl; /* truncate */
767         return 0;
768 }
769
770 static int ide_floppy_lockdoor(ide_drive_t *drive, struct ide_atapi_pc *pc,
771                                unsigned long arg, unsigned int cmd)
772 {
773         idefloppy_floppy_t *floppy = drive->driver_data;
774         struct gendisk *disk = floppy->disk;
775         int prevent = (arg && cmd != CDROMEJECT) ? 1 : 0;
776
777         if (floppy->openers > 1)
778                 return -EBUSY;
779
780         ide_set_media_lock(drive, disk, prevent);
781
782         if (cmd == CDROMEJECT)
783                 ide_do_start_stop(drive, disk, 2);
784
785         return 0;
786 }
787
788 static int idefloppy_ioctl(struct inode *inode, struct file *file,
789                         unsigned int cmd, unsigned long arg)
790 {
791         struct block_device *bdev = inode->i_bdev;
792         struct ide_floppy_obj *floppy = ide_drv_g(bdev->bd_disk,
793                                                      ide_floppy_obj);
794         ide_drive_t *drive = floppy->drive;
795         struct ide_atapi_pc pc;
796         void __user *argp = (void __user *)arg;
797         int err;
798
799         if (cmd == CDROMEJECT || cmd == CDROM_LOCKDOOR)
800                 return ide_floppy_lockdoor(drive, &pc, arg, cmd);
801
802         err = ide_floppy_format_ioctl(drive, file, cmd, argp);
803         if (err != -ENOTTY)
804                 return err;
805
806         /*
807          * skip SCSI_IOCTL_SEND_COMMAND (deprecated)
808          * and CDROM_SEND_PACKET (legacy) ioctls
809          */
810         if (cmd != CDROM_SEND_PACKET && cmd != SCSI_IOCTL_SEND_COMMAND)
811                 err = scsi_cmd_ioctl(file, bdev->bd_disk->queue,
812                                         bdev->bd_disk, cmd, argp);
813
814         if (err == -ENOTTY)
815                 err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
816
817         return err;
818 }
819
820 static int idefloppy_media_changed(struct gendisk *disk)
821 {
822         struct ide_floppy_obj *floppy = ide_drv_g(disk, ide_floppy_obj);
823         ide_drive_t *drive = floppy->drive;
824         int ret;
825
826         /* do not scan partitions twice if this is a removable device */
827         if (drive->attach) {
828                 drive->attach = 0;
829                 return 0;
830         }
831         ret = !!(drive->atapi_flags & IDE_AFLAG_MEDIA_CHANGED);
832         drive->atapi_flags &= ~IDE_AFLAG_MEDIA_CHANGED;
833         return ret;
834 }
835
836 static int idefloppy_revalidate_disk(struct gendisk *disk)
837 {
838         struct ide_floppy_obj *floppy = ide_drv_g(disk, ide_floppy_obj);
839         set_capacity(disk, idefloppy_capacity(floppy->drive));
840         return 0;
841 }
842
843 static struct block_device_operations idefloppy_ops = {
844         .owner                  = THIS_MODULE,
845         .open                   = idefloppy_open,
846         .release                = idefloppy_release,
847         .ioctl                  = idefloppy_ioctl,
848         .getgeo                 = idefloppy_getgeo,
849         .media_changed          = idefloppy_media_changed,
850         .revalidate_disk        = idefloppy_revalidate_disk
851 };
852
853 static int ide_floppy_probe(ide_drive_t *drive)
854 {
855         idefloppy_floppy_t *floppy;
856         struct gendisk *g;
857
858         if (!strstr("ide-floppy", drive->driver_req))
859                 goto failed;
860
861         if (drive->media != ide_floppy)
862                 goto failed;
863
864         if (!ide_check_atapi_device(drive, DRV_NAME)) {
865                 printk(KERN_ERR PFX "%s: not supported by this version of "
866                        DRV_NAME "\n", drive->name);
867                 goto failed;
868         }
869         floppy = kzalloc(sizeof(idefloppy_floppy_t), GFP_KERNEL);
870         if (!floppy) {
871                 printk(KERN_ERR PFX "%s: Can't allocate a floppy structure\n",
872                        drive->name);
873                 goto failed;
874         }
875
876         g = alloc_disk(1 << PARTN_BITS);
877         if (!g)
878                 goto out_free_floppy;
879
880         ide_init_disk(g, drive);
881
882         kref_init(&floppy->kref);
883
884         floppy->drive = drive;
885         floppy->driver = &idefloppy_driver;
886         floppy->disk = g;
887
888         g->private_data = &floppy->driver;
889
890         drive->driver_data = floppy;
891
892         idefloppy_setup(drive, floppy);
893
894         g->minors = 1 << PARTN_BITS;
895         g->driverfs_dev = &drive->gendev;
896         g->flags = drive->removable ? GENHD_FL_REMOVABLE : 0;
897         g->fops = &idefloppy_ops;
898         drive->attach = 1;
899         add_disk(g);
900         return 0;
901
902 out_free_floppy:
903         kfree(floppy);
904 failed:
905         return -ENODEV;
906 }
907
908 static void __exit idefloppy_exit(void)
909 {
910         driver_unregister(&idefloppy_driver.gen_driver);
911 }
912
913 static int __init idefloppy_init(void)
914 {
915         printk(KERN_INFO DRV_NAME " driver " IDEFLOPPY_VERSION "\n");
916         return driver_register(&idefloppy_driver.gen_driver);
917 }
918
919 MODULE_ALIAS("ide:*m-floppy*");
920 MODULE_ALIAS("ide-floppy");
921 module_init(idefloppy_init);
922 module_exit(idefloppy_exit);
923 MODULE_LICENSE("GPL");
924 MODULE_DESCRIPTION("ATAPI FLOPPY Driver");
925