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>
10 #include <linux/dmi.h>
11 #include <linux/slab.h>
13 #if !defined(CONFIG_DEBUG_BLOCK_EXT_DEVT)
14 #define IDE_DISK_MINORS (1 << PARTN_BITS)
16 #define IDE_DISK_MINORS 0
20 #include "ide-floppy.h"
22 #define IDE_GD_VERSION "1.18"
24 /* module parameters */
25 static DEFINE_MUTEX(ide_gd_mutex
);
26 static unsigned long debug_mask
;
27 module_param(debug_mask
, ulong
, 0644);
29 static DEFINE_MUTEX(ide_disk_ref_mutex
);
31 static void ide_disk_release(struct device
*);
33 static struct ide_disk_obj
*ide_disk_get(struct gendisk
*disk
)
35 struct ide_disk_obj
*idkp
= NULL
;
37 mutex_lock(&ide_disk_ref_mutex
);
38 idkp
= ide_drv_g(disk
, ide_disk_obj
);
40 if (ide_device_get(idkp
->drive
))
43 get_device(&idkp
->dev
);
45 mutex_unlock(&ide_disk_ref_mutex
);
49 static void ide_disk_put(struct ide_disk_obj
*idkp
)
51 ide_drive_t
*drive
= idkp
->drive
;
53 mutex_lock(&ide_disk_ref_mutex
);
54 put_device(&idkp
->dev
);
55 ide_device_put(drive
);
56 mutex_unlock(&ide_disk_ref_mutex
);
59 sector_t
ide_gd_capacity(ide_drive_t
*drive
)
61 return drive
->capacity64
;
64 static int ide_gd_probe(ide_drive_t
*);
66 static void ide_gd_remove(ide_drive_t
*drive
)
68 struct ide_disk_obj
*idkp
= drive
->driver_data
;
69 struct gendisk
*g
= idkp
->disk
;
71 ide_proc_unregister_driver(drive
, idkp
->driver
);
72 device_del(&idkp
->dev
);
74 drive
->disk_ops
->flush(drive
);
76 mutex_lock(&ide_disk_ref_mutex
);
77 put_device(&idkp
->dev
);
78 mutex_unlock(&ide_disk_ref_mutex
);
81 static void ide_disk_release(struct device
*dev
)
83 struct ide_disk_obj
*idkp
= to_ide_drv(dev
, ide_disk_obj
);
84 ide_drive_t
*drive
= idkp
->drive
;
85 struct gendisk
*g
= idkp
->disk
;
87 drive
->disk_ops
= NULL
;
88 drive
->driver_data
= NULL
;
89 g
->private_data
= NULL
;
95 * On HPA drives the capacity needs to be
96 * reinitialized on resume otherwise the disk
97 * can not be used and a hard reset is required
99 static void ide_gd_resume(ide_drive_t
*drive
)
101 if (ata_id_hpa_enabled(drive
->id
))
102 (void)drive
->disk_ops
->get_capacity(drive
);
105 static const struct dmi_system_id ide_coldreboot_table
[] = {
107 /* Acer TravelMate 66x cuts power during reboot */
108 .ident
= "Acer TravelMate 660",
110 DMI_MATCH(DMI_SYS_VENDOR
, "Acer"),
111 DMI_MATCH(DMI_PRODUCT_NAME
, "TravelMate 660"),
115 { } /* terminate list */
118 static void ide_gd_shutdown(ide_drive_t
*drive
)
121 /* On Alpha, halt(8) doesn't actually turn the machine off,
122 it puts you into the sort of firmware monitor. Typically,
123 it's used to boot another kernel image, so it's not much
124 different from reboot(8). Therefore, we don't need to
125 spin down the disk in this case, especially since Alpha
126 firmware doesn't handle disks in standby mode properly.
127 On the other hand, it's reasonably safe to turn the power
128 off when the shutdown process reaches the firmware prompt,
129 as the firmware initialization takes rather long time -
130 at least 10 seconds, which should be sufficient for
131 the disk to expire its write cache. */
132 if (system_state
!= SYSTEM_POWER_OFF
) {
134 if (system_state
== SYSTEM_RESTART
&&
135 !dmi_check_system(ide_coldreboot_table
)) {
137 drive
->disk_ops
->flush(drive
);
141 printk(KERN_INFO
"Shutdown: %s\n", drive
->name
);
143 drive
->gendev
.bus
->suspend(&drive
->gendev
, PMSG_SUSPEND
);
146 #ifdef CONFIG_IDE_PROC_FS
147 static ide_proc_entry_t
*ide_disk_proc_entries(ide_drive_t
*drive
)
149 return (drive
->media
== ide_disk
) ? ide_disk_proc
: ide_floppy_proc
;
152 static const struct ide_proc_devset
*ide_disk_proc_devsets(ide_drive_t
*drive
)
154 return (drive
->media
== ide_disk
) ? ide_disk_settings
155 : ide_floppy_settings
;
159 static ide_startstop_t
ide_gd_do_request(ide_drive_t
*drive
,
160 struct request
*rq
, sector_t sector
)
162 return drive
->disk_ops
->do_request(drive
, rq
, sector
);
165 static struct ide_driver ide_gd_driver
= {
167 .owner
= THIS_MODULE
,
169 .bus
= &ide_bus_type
,
171 .probe
= ide_gd_probe
,
172 .remove
= ide_gd_remove
,
173 .resume
= ide_gd_resume
,
174 .shutdown
= ide_gd_shutdown
,
175 .version
= IDE_GD_VERSION
,
176 .do_request
= ide_gd_do_request
,
177 #ifdef CONFIG_IDE_PROC_FS
178 .proc_entries
= ide_disk_proc_entries
,
179 .proc_devsets
= ide_disk_proc_devsets
,
183 static int ide_gd_open(struct block_device
*bdev
, fmode_t mode
)
185 struct gendisk
*disk
= bdev
->bd_disk
;
186 struct ide_disk_obj
*idkp
;
190 idkp
= ide_disk_get(disk
);
196 ide_debug_log(IDE_DBG_FUNC
, "enter");
200 if ((drive
->dev_flags
& IDE_DFLAG_REMOVABLE
) && idkp
->openers
== 1) {
201 drive
->dev_flags
&= ~IDE_DFLAG_FORMAT_IN_PROGRESS
;
204 ret
= drive
->disk_ops
->init_media(drive
, disk
);
207 * Allow O_NDELAY to open a drive without a disk, or with an
208 * unreadable disk, so that we can get the format capacity
209 * of the drive or begin the format - Sam
211 if (ret
&& (mode
& FMODE_NDELAY
) == 0) {
216 if ((drive
->dev_flags
& IDE_DFLAG_WP
) && (mode
& FMODE_WRITE
)) {
222 * Ignore the return code from door_lock,
223 * since the open() has already succeeded,
224 * and the door_lock is irrelevant at this point.
226 drive
->disk_ops
->set_doorlock(drive
, disk
, 1);
227 drive
->dev_flags
|= IDE_DFLAG_MEDIA_CHANGED
;
228 check_disk_change(bdev
);
229 } else if (drive
->dev_flags
& IDE_DFLAG_FORMAT_IN_PROGRESS
) {
241 static int ide_gd_unlocked_open(struct block_device
*bdev
, fmode_t mode
)
245 mutex_lock(&ide_gd_mutex
);
246 ret
= ide_gd_open(bdev
, mode
);
247 mutex_unlock(&ide_gd_mutex
);
253 static int ide_gd_release(struct gendisk
*disk
, fmode_t mode
)
255 struct ide_disk_obj
*idkp
= ide_drv_g(disk
, ide_disk_obj
);
256 ide_drive_t
*drive
= idkp
->drive
;
258 ide_debug_log(IDE_DBG_FUNC
, "enter");
260 mutex_lock(&ide_gd_mutex
);
261 if (idkp
->openers
== 1)
262 drive
->disk_ops
->flush(drive
);
264 if ((drive
->dev_flags
& IDE_DFLAG_REMOVABLE
) && idkp
->openers
== 1) {
265 drive
->disk_ops
->set_doorlock(drive
, disk
, 0);
266 drive
->dev_flags
&= ~IDE_DFLAG_FORMAT_IN_PROGRESS
;
272 mutex_unlock(&ide_gd_mutex
);
277 static int ide_gd_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
279 struct ide_disk_obj
*idkp
= ide_drv_g(bdev
->bd_disk
, ide_disk_obj
);
280 ide_drive_t
*drive
= idkp
->drive
;
282 geo
->heads
= drive
->bios_head
;
283 geo
->sectors
= drive
->bios_sect
;
284 geo
->cylinders
= (u16
)drive
->bios_cyl
; /* truncate */
288 static int ide_gd_media_changed(struct gendisk
*disk
)
290 struct ide_disk_obj
*idkp
= ide_drv_g(disk
, ide_disk_obj
);
291 ide_drive_t
*drive
= idkp
->drive
;
294 /* do not scan partitions twice if this is a removable device */
295 if (drive
->dev_flags
& IDE_DFLAG_ATTACH
) {
296 drive
->dev_flags
&= ~IDE_DFLAG_ATTACH
;
300 ret
= !!(drive
->dev_flags
& IDE_DFLAG_MEDIA_CHANGED
);
301 drive
->dev_flags
&= ~IDE_DFLAG_MEDIA_CHANGED
;
306 static void ide_gd_unlock_native_capacity(struct gendisk
*disk
)
308 struct ide_disk_obj
*idkp
= ide_drv_g(disk
, ide_disk_obj
);
309 ide_drive_t
*drive
= idkp
->drive
;
310 const struct ide_disk_ops
*disk_ops
= drive
->disk_ops
;
312 if (disk_ops
->unlock_native_capacity
)
313 disk_ops
->unlock_native_capacity(drive
);
316 static int ide_gd_revalidate_disk(struct gendisk
*disk
)
318 struct ide_disk_obj
*idkp
= ide_drv_g(disk
, ide_disk_obj
);
319 ide_drive_t
*drive
= idkp
->drive
;
321 if (ide_gd_media_changed(disk
))
322 drive
->disk_ops
->get_capacity(drive
);
324 set_capacity(disk
, ide_gd_capacity(drive
));
328 static int ide_gd_ioctl(struct block_device
*bdev
, fmode_t mode
,
329 unsigned int cmd
, unsigned long arg
)
331 struct ide_disk_obj
*idkp
= ide_drv_g(bdev
->bd_disk
, ide_disk_obj
);
332 ide_drive_t
*drive
= idkp
->drive
;
334 return drive
->disk_ops
->ioctl(drive
, bdev
, mode
, cmd
, arg
);
337 static const struct block_device_operations ide_gd_ops
= {
338 .owner
= THIS_MODULE
,
339 .open
= ide_gd_unlocked_open
,
340 .release
= ide_gd_release
,
341 .ioctl
= ide_gd_ioctl
,
342 .getgeo
= ide_gd_getgeo
,
343 .media_changed
= ide_gd_media_changed
,
344 .unlock_native_capacity
= ide_gd_unlock_native_capacity
,
345 .revalidate_disk
= ide_gd_revalidate_disk
348 static int ide_gd_probe(ide_drive_t
*drive
)
350 const struct ide_disk_ops
*disk_ops
= NULL
;
351 struct ide_disk_obj
*idkp
;
354 /* strstr("foo", "") is non-NULL */
355 if (!strstr("ide-gd", drive
->driver_req
))
358 #ifdef CONFIG_IDE_GD_ATA
359 if (drive
->media
== ide_disk
)
360 disk_ops
= &ide_ata_disk_ops
;
362 #ifdef CONFIG_IDE_GD_ATAPI
363 if (drive
->media
== ide_floppy
)
364 disk_ops
= &ide_atapi_disk_ops
;
366 if (disk_ops
== NULL
)
369 if (disk_ops
->check(drive
, DRV_NAME
) == 0) {
370 printk(KERN_ERR PFX
"%s: not supported by this driver\n",
375 idkp
= kzalloc(sizeof(*idkp
), GFP_KERNEL
);
377 printk(KERN_ERR PFX
"%s: can't allocate a disk structure\n",
382 g
= alloc_disk_node(IDE_DISK_MINORS
, hwif_to_node(drive
->hwif
));
386 ide_init_disk(g
, drive
);
388 idkp
->dev
.parent
= &drive
->gendev
;
389 idkp
->dev
.release
= ide_disk_release
;
390 dev_set_name(&idkp
->dev
, dev_name(&drive
->gendev
));
392 if (device_register(&idkp
->dev
))
396 idkp
->driver
= &ide_gd_driver
;
399 g
->private_data
= &idkp
->driver
;
401 drive
->driver_data
= idkp
;
402 drive
->debug_mask
= debug_mask
;
403 drive
->disk_ops
= disk_ops
;
405 disk_ops
->setup(drive
);
407 set_capacity(g
, ide_gd_capacity(drive
));
409 g
->minors
= IDE_DISK_MINORS
;
410 g
->driverfs_dev
= &drive
->gendev
;
411 g
->flags
|= GENHD_FL_EXT_DEVT
;
412 if (drive
->dev_flags
& IDE_DFLAG_REMOVABLE
)
413 g
->flags
= GENHD_FL_REMOVABLE
;
414 g
->fops
= &ide_gd_ops
;
426 static int __init
ide_gd_init(void)
428 printk(KERN_INFO DRV_NAME
" driver " IDE_GD_VERSION
"\n");
429 return driver_register(&ide_gd_driver
.gen_driver
);
432 static void __exit
ide_gd_exit(void)
434 driver_unregister(&ide_gd_driver
.gen_driver
);
437 MODULE_ALIAS("ide:*m-disk*");
438 MODULE_ALIAS("ide-disk");
439 MODULE_ALIAS("ide:*m-floppy*");
440 MODULE_ALIAS("ide-floppy");
441 module_init(ide_gd_init
);
442 module_exit(ide_gd_exit
);
443 MODULE_LICENSE("GPL");
444 MODULE_DESCRIPTION("generic ATA/ATAPI disk driver");