OMAP2xxx clock: rename clk_init_one() to clk_preinit()
[linux-ginger.git] / drivers / ide / ide-gd.c
blob4b6b71e2cdf5b6cc3f1fe1288e0f99408d5a3742
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>
8 #include <linux/ide.h>
9 #include <linux/hdreg.h>
10 #include <linux/dmi.h>
12 #if !defined(CONFIG_DEBUG_BLOCK_EXT_DEVT)
13 #define IDE_DISK_MINORS (1 << PARTN_BITS)
14 #else
15 #define IDE_DISK_MINORS 0
16 #endif
18 #include "ide-disk.h"
19 #include "ide-floppy.h"
21 #define IDE_GD_VERSION "1.18"
23 /* module parameters */
24 static unsigned long debug_mask;
25 module_param(debug_mask, ulong, 0644);
27 static DEFINE_MUTEX(ide_disk_ref_mutex);
29 static void ide_disk_release(struct device *);
31 static struct ide_disk_obj *ide_disk_get(struct gendisk *disk)
33 struct ide_disk_obj *idkp = NULL;
35 mutex_lock(&ide_disk_ref_mutex);
36 idkp = ide_drv_g(disk, ide_disk_obj);
37 if (idkp) {
38 if (ide_device_get(idkp->drive))
39 idkp = NULL;
40 else
41 get_device(&idkp->dev);
43 mutex_unlock(&ide_disk_ref_mutex);
44 return idkp;
47 static void ide_disk_put(struct ide_disk_obj *idkp)
49 ide_drive_t *drive = idkp->drive;
51 mutex_lock(&ide_disk_ref_mutex);
52 put_device(&idkp->dev);
53 ide_device_put(drive);
54 mutex_unlock(&ide_disk_ref_mutex);
57 sector_t ide_gd_capacity(ide_drive_t *drive)
59 return drive->capacity64;
62 static int ide_gd_probe(ide_drive_t *);
64 static void ide_gd_remove(ide_drive_t *drive)
66 struct ide_disk_obj *idkp = drive->driver_data;
67 struct gendisk *g = idkp->disk;
69 ide_proc_unregister_driver(drive, idkp->driver);
70 device_del(&idkp->dev);
71 del_gendisk(g);
72 drive->disk_ops->flush(drive);
74 mutex_lock(&ide_disk_ref_mutex);
75 put_device(&idkp->dev);
76 mutex_unlock(&ide_disk_ref_mutex);
79 static void ide_disk_release(struct device *dev)
81 struct ide_disk_obj *idkp = to_ide_drv(dev, ide_disk_obj);
82 ide_drive_t *drive = idkp->drive;
83 struct gendisk *g = idkp->disk;
85 drive->disk_ops = NULL;
86 drive->driver_data = NULL;
87 g->private_data = NULL;
88 put_disk(g);
89 kfree(idkp);
93 * On HPA drives the capacity needs to be
94 * reinitilized on resume otherwise the disk
95 * can not be used and a hard reset is required
97 static void ide_gd_resume(ide_drive_t *drive)
99 if (ata_id_hpa_enabled(drive->id))
100 (void)drive->disk_ops->get_capacity(drive);
103 static const struct dmi_system_id ide_coldreboot_table[] = {
105 /* Acer TravelMate 66x cuts power during reboot */
106 .ident = "Acer TravelMate 660",
107 .matches = {
108 DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
109 DMI_MATCH(DMI_PRODUCT_NAME, "TravelMate 660"),
113 { } /* terminate list */
116 static void ide_gd_shutdown(ide_drive_t *drive)
118 #ifdef CONFIG_ALPHA
119 /* On Alpha, halt(8) doesn't actually turn the machine off,
120 it puts you into the sort of firmware monitor. Typically,
121 it's used to boot another kernel image, so it's not much
122 different from reboot(8). Therefore, we don't need to
123 spin down the disk in this case, especially since Alpha
124 firmware doesn't handle disks in standby mode properly.
125 On the other hand, it's reasonably safe to turn the power
126 off when the shutdown process reaches the firmware prompt,
127 as the firmware initialization takes rather long time -
128 at least 10 seconds, which should be sufficient for
129 the disk to expire its write cache. */
130 if (system_state != SYSTEM_POWER_OFF) {
131 #else
132 if (system_state == SYSTEM_RESTART &&
133 !dmi_check_system(ide_coldreboot_table)) {
134 #endif
135 drive->disk_ops->flush(drive);
136 return;
139 printk(KERN_INFO "Shutdown: %s\n", drive->name);
141 drive->gendev.bus->suspend(&drive->gendev, PMSG_SUSPEND);
144 #ifdef CONFIG_IDE_PROC_FS
145 static ide_proc_entry_t *ide_disk_proc_entries(ide_drive_t *drive)
147 return (drive->media == ide_disk) ? ide_disk_proc : ide_floppy_proc;
150 static const struct ide_proc_devset *ide_disk_proc_devsets(ide_drive_t *drive)
152 return (drive->media == ide_disk) ? ide_disk_settings
153 : ide_floppy_settings;
155 #endif
157 static ide_startstop_t ide_gd_do_request(ide_drive_t *drive,
158 struct request *rq, sector_t sector)
160 return drive->disk_ops->do_request(drive, rq, sector);
163 static struct ide_driver ide_gd_driver = {
164 .gen_driver = {
165 .owner = THIS_MODULE,
166 .name = "ide-gd",
167 .bus = &ide_bus_type,
169 .probe = ide_gd_probe,
170 .remove = ide_gd_remove,
171 .resume = ide_gd_resume,
172 .shutdown = ide_gd_shutdown,
173 .version = IDE_GD_VERSION,
174 .do_request = ide_gd_do_request,
175 #ifdef CONFIG_IDE_PROC_FS
176 .proc_entries = ide_disk_proc_entries,
177 .proc_devsets = ide_disk_proc_devsets,
178 #endif
181 static int ide_gd_open(struct block_device *bdev, fmode_t mode)
183 struct gendisk *disk = bdev->bd_disk;
184 struct ide_disk_obj *idkp;
185 ide_drive_t *drive;
186 int ret = 0;
188 idkp = ide_disk_get(disk);
189 if (idkp == NULL)
190 return -ENXIO;
192 drive = idkp->drive;
194 ide_debug_log(IDE_DBG_FUNC, "enter");
196 idkp->openers++;
198 if ((drive->dev_flags & IDE_DFLAG_REMOVABLE) && idkp->openers == 1) {
199 drive->dev_flags &= ~IDE_DFLAG_FORMAT_IN_PROGRESS;
200 /* Just in case */
202 ret = drive->disk_ops->init_media(drive, disk);
205 * Allow O_NDELAY to open a drive without a disk, or with an
206 * unreadable disk, so that we can get the format capacity
207 * of the drive or begin the format - Sam
209 if (ret && (mode & FMODE_NDELAY) == 0) {
210 ret = -EIO;
211 goto out_put_idkp;
214 if ((drive->dev_flags & IDE_DFLAG_WP) && (mode & FMODE_WRITE)) {
215 ret = -EROFS;
216 goto out_put_idkp;
220 * Ignore the return code from door_lock,
221 * since the open() has already succeeded,
222 * and the door_lock is irrelevant at this point.
224 drive->disk_ops->set_doorlock(drive, disk, 1);
225 drive->dev_flags |= IDE_DFLAG_MEDIA_CHANGED;
226 check_disk_change(bdev);
227 } else if (drive->dev_flags & IDE_DFLAG_FORMAT_IN_PROGRESS) {
228 ret = -EBUSY;
229 goto out_put_idkp;
231 return 0;
233 out_put_idkp:
234 idkp->openers--;
235 ide_disk_put(idkp);
236 return ret;
239 static int ide_gd_release(struct gendisk *disk, fmode_t mode)
241 struct ide_disk_obj *idkp = ide_drv_g(disk, ide_disk_obj);
242 ide_drive_t *drive = idkp->drive;
244 ide_debug_log(IDE_DBG_FUNC, "enter");
246 if (idkp->openers == 1)
247 drive->disk_ops->flush(drive);
249 if ((drive->dev_flags & IDE_DFLAG_REMOVABLE) && idkp->openers == 1) {
250 drive->disk_ops->set_doorlock(drive, disk, 0);
251 drive->dev_flags &= ~IDE_DFLAG_FORMAT_IN_PROGRESS;
254 idkp->openers--;
256 ide_disk_put(idkp);
258 return 0;
261 static int ide_gd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
263 struct ide_disk_obj *idkp = ide_drv_g(bdev->bd_disk, ide_disk_obj);
264 ide_drive_t *drive = idkp->drive;
266 geo->heads = drive->bios_head;
267 geo->sectors = drive->bios_sect;
268 geo->cylinders = (u16)drive->bios_cyl; /* truncate */
269 return 0;
272 static int ide_gd_media_changed(struct gendisk *disk)
274 struct ide_disk_obj *idkp = ide_drv_g(disk, ide_disk_obj);
275 ide_drive_t *drive = idkp->drive;
276 int ret;
278 /* do not scan partitions twice if this is a removable device */
279 if (drive->dev_flags & IDE_DFLAG_ATTACH) {
280 drive->dev_flags &= ~IDE_DFLAG_ATTACH;
281 return 0;
284 ret = !!(drive->dev_flags & IDE_DFLAG_MEDIA_CHANGED);
285 drive->dev_flags &= ~IDE_DFLAG_MEDIA_CHANGED;
287 return ret;
290 static int ide_gd_revalidate_disk(struct gendisk *disk)
292 struct ide_disk_obj *idkp = ide_drv_g(disk, ide_disk_obj);
293 ide_drive_t *drive = idkp->drive;
295 if (ide_gd_media_changed(disk))
296 drive->disk_ops->get_capacity(drive);
298 set_capacity(disk, ide_gd_capacity(drive));
299 return 0;
302 static int ide_gd_ioctl(struct block_device *bdev, fmode_t mode,
303 unsigned int cmd, unsigned long arg)
305 struct ide_disk_obj *idkp = ide_drv_g(bdev->bd_disk, ide_disk_obj);
306 ide_drive_t *drive = idkp->drive;
308 return drive->disk_ops->ioctl(drive, bdev, mode, cmd, arg);
311 static struct block_device_operations ide_gd_ops = {
312 .owner = THIS_MODULE,
313 .open = ide_gd_open,
314 .release = ide_gd_release,
315 .locked_ioctl = ide_gd_ioctl,
316 .getgeo = ide_gd_getgeo,
317 .media_changed = ide_gd_media_changed,
318 .revalidate_disk = ide_gd_revalidate_disk
321 static int ide_gd_probe(ide_drive_t *drive)
323 const struct ide_disk_ops *disk_ops = NULL;
324 struct ide_disk_obj *idkp;
325 struct gendisk *g;
327 /* strstr("foo", "") is non-NULL */
328 if (!strstr("ide-gd", drive->driver_req))
329 goto failed;
331 #ifdef CONFIG_IDE_GD_ATA
332 if (drive->media == ide_disk)
333 disk_ops = &ide_ata_disk_ops;
334 #endif
335 #ifdef CONFIG_IDE_GD_ATAPI
336 if (drive->media == ide_floppy)
337 disk_ops = &ide_atapi_disk_ops;
338 #endif
339 if (disk_ops == NULL)
340 goto failed;
342 if (disk_ops->check(drive, DRV_NAME) == 0) {
343 printk(KERN_ERR PFX "%s: not supported by this driver\n",
344 drive->name);
345 goto failed;
348 idkp = kzalloc(sizeof(*idkp), GFP_KERNEL);
349 if (!idkp) {
350 printk(KERN_ERR PFX "%s: can't allocate a disk structure\n",
351 drive->name);
352 goto failed;
355 g = alloc_disk_node(IDE_DISK_MINORS, hwif_to_node(drive->hwif));
356 if (!g)
357 goto out_free_idkp;
359 ide_init_disk(g, drive);
361 idkp->dev.parent = &drive->gendev;
362 idkp->dev.release = ide_disk_release;
363 dev_set_name(&idkp->dev, dev_name(&drive->gendev));
365 if (device_register(&idkp->dev))
366 goto out_free_disk;
368 idkp->drive = drive;
369 idkp->driver = &ide_gd_driver;
370 idkp->disk = g;
372 g->private_data = &idkp->driver;
374 drive->driver_data = idkp;
375 drive->debug_mask = debug_mask;
376 drive->disk_ops = disk_ops;
378 disk_ops->setup(drive);
380 set_capacity(g, ide_gd_capacity(drive));
382 g->minors = IDE_DISK_MINORS;
383 g->driverfs_dev = &drive->gendev;
384 g->flags |= GENHD_FL_EXT_DEVT;
385 if (drive->dev_flags & IDE_DFLAG_REMOVABLE)
386 g->flags = GENHD_FL_REMOVABLE;
387 g->fops = &ide_gd_ops;
388 add_disk(g);
389 return 0;
391 out_free_disk:
392 put_disk(g);
393 out_free_idkp:
394 kfree(idkp);
395 failed:
396 return -ENODEV;
399 static int __init ide_gd_init(void)
401 printk(KERN_INFO DRV_NAME " driver " IDE_GD_VERSION "\n");
402 return driver_register(&ide_gd_driver.gen_driver);
405 static void __exit ide_gd_exit(void)
407 driver_unregister(&ide_gd_driver.gen_driver);
410 MODULE_ALIAS("ide:*m-disk*");
411 MODULE_ALIAS("ide-disk");
412 MODULE_ALIAS("ide:*m-floppy*");
413 MODULE_ALIAS("ide-floppy");
414 module_init(ide_gd_init);
415 module_exit(ide_gd_exit);
416 MODULE_LICENSE("GPL");
417 MODULE_DESCRIPTION("generic ATA/ATAPI disk driver");