1 #include <linux/module.h>
2 #include <linux/types.h>
3 #include <linux/string.h>
4 #include <linux/kernel.h>
5 #include <linux/errno.h>
6 #include <linux/genhd.h>
7 #include <linux/mutex.h>
9 #include <linux/hdreg.h>
11 #if !defined(CONFIG_DEBUG_BLOCK_EXT_DEVT)
12 #define IDE_DISK_MINORS (1 << PARTN_BITS)
14 #define IDE_DISK_MINORS 0
18 #include "ide-floppy.h"
20 #define IDE_GD_VERSION "1.18"
22 /* module parameters */
23 static unsigned long debug_mask;
24 module_param(debug_mask, ulong, 0644);
26 static DEFINE_MUTEX(ide_disk_ref_mutex);
28 static void ide_disk_release(struct device *);
30 static struct ide_disk_obj *ide_disk_get(struct gendisk *disk)
32 struct ide_disk_obj *idkp = NULL;
34 mutex_lock(&ide_disk_ref_mutex);
35 idkp = ide_drv_g(disk, ide_disk_obj);
37 if (ide_device_get(idkp->drive))
40 get_device(&idkp->dev);
42 mutex_unlock(&ide_disk_ref_mutex);
46 static void ide_disk_put(struct ide_disk_obj *idkp)
48 ide_drive_t *drive = idkp->drive;
50 mutex_lock(&ide_disk_ref_mutex);
51 put_device(&idkp->dev);
52 ide_device_put(drive);
53 mutex_unlock(&ide_disk_ref_mutex);
56 sector_t ide_gd_capacity(ide_drive_t *drive)
58 return drive->capacity64;
61 static int ide_gd_probe(ide_drive_t *);
63 static void ide_gd_remove(ide_drive_t *drive)
65 struct ide_disk_obj *idkp = drive->driver_data;
66 struct gendisk *g = idkp->disk;
68 ide_proc_unregister_driver(drive, idkp->driver);
69 device_del(&idkp->dev);
71 drive->disk_ops->flush(drive);
73 mutex_lock(&ide_disk_ref_mutex);
74 put_device(&idkp->dev);
75 mutex_unlock(&ide_disk_ref_mutex);
78 static void ide_disk_release(struct device *dev)
80 struct ide_disk_obj *idkp = to_ide_drv(dev, ide_disk_obj);
81 ide_drive_t *drive = idkp->drive;
82 struct gendisk *g = idkp->disk;
84 drive->disk_ops = NULL;
85 drive->driver_data = NULL;
86 g->private_data = NULL;
92 * On HPA drives the capacity needs to be
93 * reinitilized on resume otherwise the disk
94 * can not be used and a hard reset is required
96 static void ide_gd_resume(ide_drive_t *drive)
98 if (ata_id_hpa_enabled(drive->id))
99 (void)drive->disk_ops->get_capacity(drive);
102 static void ide_gd_shutdown(ide_drive_t *drive)
105 /* On Alpha, halt(8) doesn't actually turn the machine off,
106 it puts you into the sort of firmware monitor. Typically,
107 it's used to boot another kernel image, so it's not much
108 different from reboot(8). Therefore, we don't need to
109 spin down the disk in this case, especially since Alpha
110 firmware doesn't handle disks in standby mode properly.
111 On the other hand, it's reasonably safe to turn the power
112 off when the shutdown process reaches the firmware prompt,
113 as the firmware initialization takes rather long time -
114 at least 10 seconds, which should be sufficient for
115 the disk to expire its write cache. */
116 if (system_state != SYSTEM_POWER_OFF) {
118 if (system_state == SYSTEM_RESTART) {
120 drive->disk_ops->flush(drive);
124 printk(KERN_INFO "Shutdown: %s\n", drive->name);
126 drive->gendev.bus->suspend(&drive->gendev, PMSG_SUSPEND);
129 #ifdef CONFIG_IDE_PROC_FS
130 static ide_proc_entry_t *ide_disk_proc_entries(ide_drive_t *drive)
132 return (drive->media == ide_disk) ? ide_disk_proc : ide_floppy_proc;
135 static const struct ide_proc_devset *ide_disk_proc_devsets(ide_drive_t *drive)
137 return (drive->media == ide_disk) ? ide_disk_settings
138 : ide_floppy_settings;
142 static ide_startstop_t ide_gd_do_request(ide_drive_t *drive,
143 struct request *rq, sector_t sector)
145 return drive->disk_ops->do_request(drive, rq, sector);
148 static struct ide_driver ide_gd_driver = {
150 .owner = THIS_MODULE,
152 .bus = &ide_bus_type,
154 .probe = ide_gd_probe,
155 .remove = ide_gd_remove,
156 .resume = ide_gd_resume,
157 .shutdown = ide_gd_shutdown,
158 .version = IDE_GD_VERSION,
159 .do_request = ide_gd_do_request,
160 #ifdef CONFIG_IDE_PROC_FS
161 .proc_entries = ide_disk_proc_entries,
162 .proc_devsets = ide_disk_proc_devsets,
166 static int ide_gd_open(struct block_device *bdev, fmode_t mode)
168 struct gendisk *disk = bdev->bd_disk;
169 struct ide_disk_obj *idkp;
173 idkp = ide_disk_get(disk);
179 ide_debug_log(IDE_DBG_FUNC, "enter");
183 if ((drive->dev_flags & IDE_DFLAG_REMOVABLE) && idkp->openers == 1) {
184 drive->dev_flags &= ~IDE_DFLAG_FORMAT_IN_PROGRESS;
187 ret = drive->disk_ops->init_media(drive, disk);
190 * Allow O_NDELAY to open a drive without a disk, or with an
191 * unreadable disk, so that we can get the format capacity
192 * of the drive or begin the format - Sam
194 if (ret && (mode & FMODE_NDELAY) == 0) {
199 if ((drive->dev_flags & IDE_DFLAG_WP) && (mode & FMODE_WRITE)) {
205 * Ignore the return code from door_lock,
206 * since the open() has already succeeded,
207 * and the door_lock is irrelevant at this point.
209 drive->disk_ops->set_doorlock(drive, disk, 1);
210 drive->dev_flags |= IDE_DFLAG_MEDIA_CHANGED;
211 check_disk_change(bdev);
212 } else if (drive->dev_flags & IDE_DFLAG_FORMAT_IN_PROGRESS) {
224 static int ide_gd_release(struct gendisk *disk, fmode_t mode)
226 struct ide_disk_obj *idkp = ide_drv_g(disk, ide_disk_obj);
227 ide_drive_t *drive = idkp->drive;
229 ide_debug_log(IDE_DBG_FUNC, "enter");
231 if (idkp->openers == 1)
232 drive->disk_ops->flush(drive);
234 if ((drive->dev_flags & IDE_DFLAG_REMOVABLE) && idkp->openers == 1) {
235 drive->disk_ops->set_doorlock(drive, disk, 0);
236 drive->dev_flags &= ~IDE_DFLAG_FORMAT_IN_PROGRESS;
246 static int ide_gd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
248 struct ide_disk_obj *idkp = ide_drv_g(bdev->bd_disk, ide_disk_obj);
249 ide_drive_t *drive = idkp->drive;
251 geo->heads = drive->bios_head;
252 geo->sectors = drive->bios_sect;
253 geo->cylinders = (u16)drive->bios_cyl; /* truncate */
257 static int ide_gd_media_changed(struct gendisk *disk)
259 struct ide_disk_obj *idkp = ide_drv_g(disk, ide_disk_obj);
260 ide_drive_t *drive = idkp->drive;
263 /* do not scan partitions twice if this is a removable device */
264 if (drive->dev_flags & IDE_DFLAG_ATTACH) {
265 drive->dev_flags &= ~IDE_DFLAG_ATTACH;
269 ret = !!(drive->dev_flags & IDE_DFLAG_MEDIA_CHANGED);
270 drive->dev_flags &= ~IDE_DFLAG_MEDIA_CHANGED;
275 static int ide_gd_revalidate_disk(struct gendisk *disk)
277 struct ide_disk_obj *idkp = ide_drv_g(disk, ide_disk_obj);
278 ide_drive_t *drive = idkp->drive;
280 if (ide_gd_media_changed(disk))
281 drive->disk_ops->get_capacity(drive);
283 set_capacity(disk, ide_gd_capacity(drive));
287 static int ide_gd_ioctl(struct block_device *bdev, fmode_t mode,
288 unsigned int cmd, unsigned long arg)
290 struct ide_disk_obj *idkp = ide_drv_g(bdev->bd_disk, ide_disk_obj);
291 ide_drive_t *drive = idkp->drive;
293 return drive->disk_ops->ioctl(drive, bdev, mode, cmd, arg);
296 static struct block_device_operations ide_gd_ops = {
297 .owner = THIS_MODULE,
299 .release = ide_gd_release,
300 .locked_ioctl = ide_gd_ioctl,
301 .getgeo = ide_gd_getgeo,
302 .media_changed = ide_gd_media_changed,
303 .revalidate_disk = ide_gd_revalidate_disk
306 static int ide_gd_probe(ide_drive_t *drive)
308 const struct ide_disk_ops *disk_ops = NULL;
309 struct ide_disk_obj *idkp;
312 /* strstr("foo", "") is non-NULL */
313 if (!strstr("ide-gd", drive->driver_req))
316 #ifdef CONFIG_IDE_GD_ATA
317 if (drive->media == ide_disk)
318 disk_ops = &ide_ata_disk_ops;
320 #ifdef CONFIG_IDE_GD_ATAPI
321 if (drive->media == ide_floppy)
322 disk_ops = &ide_atapi_disk_ops;
324 if (disk_ops == NULL)
327 if (disk_ops->check(drive, DRV_NAME) == 0) {
328 printk(KERN_ERR PFX "%s: not supported by this driver\n",
333 idkp = kzalloc(sizeof(*idkp), GFP_KERNEL);
335 printk(KERN_ERR PFX "%s: can't allocate a disk structure\n",
340 g = alloc_disk_node(IDE_DISK_MINORS, hwif_to_node(drive->hwif));
344 ide_init_disk(g, drive);
346 idkp->dev.parent = &drive->gendev;
347 idkp->dev.release = ide_disk_release;
348 dev_set_name(&idkp->dev, dev_name(&drive->gendev));
350 if (device_register(&idkp->dev))
354 idkp->driver = &ide_gd_driver;
357 g->private_data = &idkp->driver;
359 drive->driver_data = idkp;
360 drive->debug_mask = debug_mask;
361 drive->disk_ops = disk_ops;
363 disk_ops->setup(drive);
365 set_capacity(g, ide_gd_capacity(drive));
367 g->minors = IDE_DISK_MINORS;
368 g->driverfs_dev = &drive->gendev;
369 g->flags |= GENHD_FL_EXT_DEVT;
370 if (drive->dev_flags & IDE_DFLAG_REMOVABLE)
371 g->flags = GENHD_FL_REMOVABLE;
372 g->fops = &ide_gd_ops;
384 static int __init ide_gd_init(void)
386 printk(KERN_INFO DRV_NAME " driver " IDE_GD_VERSION "\n");
387 return driver_register(&ide_gd_driver.gen_driver);
390 static void __exit ide_gd_exit(void)
392 driver_unregister(&ide_gd_driver.gen_driver);
395 MODULE_ALIAS("ide:*m-disk*");
396 MODULE_ALIAS("ide-disk");
397 MODULE_ALIAS("ide:*m-floppy*");
398 MODULE_ALIAS("ide-floppy");
399 module_init(ide_gd_init);
400 module_exit(ide_gd_exit);
401 MODULE_LICENSE("GPL");
402 MODULE_DESCRIPTION("generic ATA/ATAPI disk driver");