Arnd Bergmann | 6e9624b | 2010-08-07 18:25:34 +0200 | [diff] [blame] | 1 | #include <linux/smp_lock.h> |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 2 | #include <linux/module.h> |
| 3 | #include <linux/types.h> |
| 4 | #include <linux/string.h> |
| 5 | #include <linux/kernel.h> |
| 6 | #include <linux/errno.h> |
| 7 | #include <linux/genhd.h> |
| 8 | #include <linux/mutex.h> |
| 9 | #include <linux/ide.h> |
| 10 | #include <linux/hdreg.h> |
Bruno Prémont | b0aedb0 | 2009-04-22 20:33:41 +0200 | [diff] [blame] | 11 | #include <linux/dmi.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 12 | #include <linux/slab.h> |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 13 | |
| 14 | #if !defined(CONFIG_DEBUG_BLOCK_EXT_DEVT) |
| 15 | #define IDE_DISK_MINORS (1 << PARTN_BITS) |
| 16 | #else |
| 17 | #define IDE_DISK_MINORS 0 |
| 18 | #endif |
| 19 | |
| 20 | #include "ide-disk.h" |
Bartlomiej Zolnierkiewicz | 806f80a | 2008-10-17 18:09:14 +0200 | [diff] [blame] | 21 | #include "ide-floppy.h" |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 22 | |
| 23 | #define IDE_GD_VERSION "1.18" |
| 24 | |
Bartlomiej Zolnierkiewicz | 806f80a | 2008-10-17 18:09:14 +0200 | [diff] [blame] | 25 | /* module parameters */ |
| 26 | static unsigned long debug_mask; |
| 27 | module_param(debug_mask, ulong, 0644); |
| 28 | |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 29 | static DEFINE_MUTEX(ide_disk_ref_mutex); |
| 30 | |
Bartlomiej Zolnierkiewicz | 8fed436 | 2009-02-25 20:28:24 +0100 | [diff] [blame] | 31 | static void ide_disk_release(struct device *); |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 32 | |
| 33 | static struct ide_disk_obj *ide_disk_get(struct gendisk *disk) |
| 34 | { |
| 35 | struct ide_disk_obj *idkp = NULL; |
| 36 | |
| 37 | mutex_lock(&ide_disk_ref_mutex); |
| 38 | idkp = ide_drv_g(disk, ide_disk_obj); |
| 39 | if (idkp) { |
| 40 | if (ide_device_get(idkp->drive)) |
| 41 | idkp = NULL; |
| 42 | else |
Bartlomiej Zolnierkiewicz | 8fed436 | 2009-02-25 20:28:24 +0100 | [diff] [blame] | 43 | get_device(&idkp->dev); |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 44 | } |
| 45 | mutex_unlock(&ide_disk_ref_mutex); |
| 46 | return idkp; |
| 47 | } |
| 48 | |
| 49 | static void ide_disk_put(struct ide_disk_obj *idkp) |
| 50 | { |
| 51 | ide_drive_t *drive = idkp->drive; |
| 52 | |
| 53 | mutex_lock(&ide_disk_ref_mutex); |
Bartlomiej Zolnierkiewicz | 8fed436 | 2009-02-25 20:28:24 +0100 | [diff] [blame] | 54 | put_device(&idkp->dev); |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 55 | ide_device_put(drive); |
| 56 | mutex_unlock(&ide_disk_ref_mutex); |
| 57 | } |
| 58 | |
| 59 | sector_t ide_gd_capacity(ide_drive_t *drive) |
| 60 | { |
| 61 | return drive->capacity64; |
| 62 | } |
| 63 | |
| 64 | static int ide_gd_probe(ide_drive_t *); |
| 65 | |
| 66 | static void ide_gd_remove(ide_drive_t *drive) |
| 67 | { |
| 68 | struct ide_disk_obj *idkp = drive->driver_data; |
| 69 | struct gendisk *g = idkp->disk; |
| 70 | |
| 71 | ide_proc_unregister_driver(drive, idkp->driver); |
Bartlomiej Zolnierkiewicz | 8fed436 | 2009-02-25 20:28:24 +0100 | [diff] [blame] | 72 | device_del(&idkp->dev); |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 73 | del_gendisk(g); |
Bartlomiej Zolnierkiewicz | 806f80a | 2008-10-17 18:09:14 +0200 | [diff] [blame] | 74 | drive->disk_ops->flush(drive); |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 75 | |
Bartlomiej Zolnierkiewicz | 8fed436 | 2009-02-25 20:28:24 +0100 | [diff] [blame] | 76 | mutex_lock(&ide_disk_ref_mutex); |
| 77 | put_device(&idkp->dev); |
| 78 | mutex_unlock(&ide_disk_ref_mutex); |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 79 | } |
| 80 | |
Bartlomiej Zolnierkiewicz | 8fed436 | 2009-02-25 20:28:24 +0100 | [diff] [blame] | 81 | static void ide_disk_release(struct device *dev) |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 82 | { |
Bartlomiej Zolnierkiewicz | 8fed436 | 2009-02-25 20:28:24 +0100 | [diff] [blame] | 83 | struct ide_disk_obj *idkp = to_ide_drv(dev, ide_disk_obj); |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 84 | ide_drive_t *drive = idkp->drive; |
| 85 | struct gendisk *g = idkp->disk; |
| 86 | |
Bartlomiej Zolnierkiewicz | 806f80a | 2008-10-17 18:09:14 +0200 | [diff] [blame] | 87 | drive->disk_ops = NULL; |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 88 | drive->driver_data = NULL; |
| 89 | g->private_data = NULL; |
| 90 | put_disk(g); |
| 91 | kfree(idkp); |
| 92 | } |
| 93 | |
| 94 | /* |
| 95 | * On HPA drives the capacity needs to be |
Uwe Kleine-König | 421f91d | 2010-06-11 12:17:00 +0200 | [diff] [blame] | 96 | * reinitialized on resume otherwise the disk |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 97 | * can not be used and a hard reset is required |
| 98 | */ |
| 99 | static void ide_gd_resume(ide_drive_t *drive) |
| 100 | { |
| 101 | if (ata_id_hpa_enabled(drive->id)) |
Bartlomiej Zolnierkiewicz | 806f80a | 2008-10-17 18:09:14 +0200 | [diff] [blame] | 102 | (void)drive->disk_ops->get_capacity(drive); |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 103 | } |
| 104 | |
Bruno Prémont | b0aedb0 | 2009-04-22 20:33:41 +0200 | [diff] [blame] | 105 | static const struct dmi_system_id ide_coldreboot_table[] = { |
| 106 | { |
| 107 | /* Acer TravelMate 66x cuts power during reboot */ |
| 108 | .ident = "Acer TravelMate 660", |
| 109 | .matches = { |
| 110 | DMI_MATCH(DMI_SYS_VENDOR, "Acer"), |
| 111 | DMI_MATCH(DMI_PRODUCT_NAME, "TravelMate 660"), |
| 112 | }, |
| 113 | }, |
| 114 | |
| 115 | { } /* terminate list */ |
| 116 | }; |
| 117 | |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 118 | static void ide_gd_shutdown(ide_drive_t *drive) |
| 119 | { |
| 120 | #ifdef CONFIG_ALPHA |
| 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) { |
| 133 | #else |
Bruno Prémont | b0aedb0 | 2009-04-22 20:33:41 +0200 | [diff] [blame] | 134 | if (system_state == SYSTEM_RESTART && |
| 135 | !dmi_check_system(ide_coldreboot_table)) { |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 136 | #endif |
Bartlomiej Zolnierkiewicz | 806f80a | 2008-10-17 18:09:14 +0200 | [diff] [blame] | 137 | drive->disk_ops->flush(drive); |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 138 | return; |
| 139 | } |
| 140 | |
| 141 | printk(KERN_INFO "Shutdown: %s\n", drive->name); |
| 142 | |
| 143 | drive->gendev.bus->suspend(&drive->gendev, PMSG_SUSPEND); |
| 144 | } |
| 145 | |
Bartlomiej Zolnierkiewicz | 79cb380 | 2008-10-17 18:09:13 +0200 | [diff] [blame] | 146 | #ifdef CONFIG_IDE_PROC_FS |
| 147 | static ide_proc_entry_t *ide_disk_proc_entries(ide_drive_t *drive) |
| 148 | { |
Bartlomiej Zolnierkiewicz | 806f80a | 2008-10-17 18:09:14 +0200 | [diff] [blame] | 149 | return (drive->media == ide_disk) ? ide_disk_proc : ide_floppy_proc; |
Bartlomiej Zolnierkiewicz | 79cb380 | 2008-10-17 18:09:13 +0200 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | static const struct ide_proc_devset *ide_disk_proc_devsets(ide_drive_t *drive) |
| 153 | { |
Bartlomiej Zolnierkiewicz | 806f80a | 2008-10-17 18:09:14 +0200 | [diff] [blame] | 154 | return (drive->media == ide_disk) ? ide_disk_settings |
| 155 | : ide_floppy_settings; |
Bartlomiej Zolnierkiewicz | 79cb380 | 2008-10-17 18:09:13 +0200 | [diff] [blame] | 156 | } |
| 157 | #endif |
| 158 | |
Bartlomiej Zolnierkiewicz | 806f80a | 2008-10-17 18:09:14 +0200 | [diff] [blame] | 159 | static ide_startstop_t ide_gd_do_request(ide_drive_t *drive, |
| 160 | struct request *rq, sector_t sector) |
| 161 | { |
| 162 | return drive->disk_ops->do_request(drive, rq, sector); |
| 163 | } |
| 164 | |
Bartlomiej Zolnierkiewicz | 7f3c868 | 2009-01-06 17:20:53 +0100 | [diff] [blame] | 165 | static struct ide_driver ide_gd_driver = { |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 166 | .gen_driver = { |
| 167 | .owner = THIS_MODULE, |
Bartlomiej Zolnierkiewicz | 806f80a | 2008-10-17 18:09:14 +0200 | [diff] [blame] | 168 | .name = "ide-gd", |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 169 | .bus = &ide_bus_type, |
| 170 | }, |
| 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, |
Bartlomiej Zolnierkiewicz | 806f80a | 2008-10-17 18:09:14 +0200 | [diff] [blame] | 176 | .do_request = ide_gd_do_request, |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 177 | #ifdef CONFIG_IDE_PROC_FS |
Bartlomiej Zolnierkiewicz | 79cb380 | 2008-10-17 18:09:13 +0200 | [diff] [blame] | 178 | .proc_entries = ide_disk_proc_entries, |
| 179 | .proc_devsets = ide_disk_proc_devsets, |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 180 | #endif |
| 181 | }; |
| 182 | |
Al Viro | b2f21e0 | 2008-10-16 10:34:00 -0400 | [diff] [blame] | 183 | static int ide_gd_open(struct block_device *bdev, fmode_t mode) |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 184 | { |
Al Viro | b2f21e0 | 2008-10-16 10:34:00 -0400 | [diff] [blame] | 185 | struct gendisk *disk = bdev->bd_disk; |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 186 | struct ide_disk_obj *idkp; |
| 187 | ide_drive_t *drive; |
Bartlomiej Zolnierkiewicz | 806f80a | 2008-10-17 18:09:14 +0200 | [diff] [blame] | 188 | int ret = 0; |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 189 | |
| 190 | idkp = ide_disk_get(disk); |
| 191 | if (idkp == NULL) |
| 192 | return -ENXIO; |
| 193 | |
| 194 | drive = idkp->drive; |
| 195 | |
Borislav Petkov | 088b1b8 | 2009-01-02 13:34:47 +0100 | [diff] [blame] | 196 | ide_debug_log(IDE_DBG_FUNC, "enter"); |
Bartlomiej Zolnierkiewicz | 806f80a | 2008-10-17 18:09:14 +0200 | [diff] [blame] | 197 | |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 198 | idkp->openers++; |
| 199 | |
| 200 | if ((drive->dev_flags & IDE_DFLAG_REMOVABLE) && idkp->openers == 1) { |
Bartlomiej Zolnierkiewicz | 806f80a | 2008-10-17 18:09:14 +0200 | [diff] [blame] | 201 | drive->dev_flags &= ~IDE_DFLAG_FORMAT_IN_PROGRESS; |
| 202 | /* Just in case */ |
| 203 | |
| 204 | ret = drive->disk_ops->init_media(drive, disk); |
| 205 | |
| 206 | /* |
| 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 |
| 210 | */ |
Al Viro | b2f21e0 | 2008-10-16 10:34:00 -0400 | [diff] [blame] | 211 | if (ret && (mode & FMODE_NDELAY) == 0) { |
Bartlomiej Zolnierkiewicz | 806f80a | 2008-10-17 18:09:14 +0200 | [diff] [blame] | 212 | ret = -EIO; |
| 213 | goto out_put_idkp; |
| 214 | } |
| 215 | |
Al Viro | b2f21e0 | 2008-10-16 10:34:00 -0400 | [diff] [blame] | 216 | if ((drive->dev_flags & IDE_DFLAG_WP) && (mode & FMODE_WRITE)) { |
Bartlomiej Zolnierkiewicz | 806f80a | 2008-10-17 18:09:14 +0200 | [diff] [blame] | 217 | ret = -EROFS; |
| 218 | goto out_put_idkp; |
| 219 | } |
| 220 | |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 221 | /* |
| 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. |
| 225 | */ |
Bartlomiej Zolnierkiewicz | 806f80a | 2008-10-17 18:09:14 +0200 | [diff] [blame] | 226 | drive->disk_ops->set_doorlock(drive, disk, 1); |
Bartlomiej Zolnierkiewicz | cedd120 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 227 | drive->dev_flags |= IDE_DFLAG_MEDIA_CHANGED; |
Al Viro | b2f21e0 | 2008-10-16 10:34:00 -0400 | [diff] [blame] | 228 | check_disk_change(bdev); |
Bartlomiej Zolnierkiewicz | 806f80a | 2008-10-17 18:09:14 +0200 | [diff] [blame] | 229 | } else if (drive->dev_flags & IDE_DFLAG_FORMAT_IN_PROGRESS) { |
| 230 | ret = -EBUSY; |
| 231 | goto out_put_idkp; |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 232 | } |
| 233 | return 0; |
Bartlomiej Zolnierkiewicz | 806f80a | 2008-10-17 18:09:14 +0200 | [diff] [blame] | 234 | |
| 235 | out_put_idkp: |
| 236 | idkp->openers--; |
| 237 | ide_disk_put(idkp); |
| 238 | return ret; |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 239 | } |
| 240 | |
Arnd Bergmann | 6e9624b | 2010-08-07 18:25:34 +0200 | [diff] [blame] | 241 | static int ide_gd_unlocked_open(struct block_device *bdev, fmode_t mode) |
| 242 | { |
| 243 | int ret; |
| 244 | |
| 245 | lock_kernel(); |
| 246 | ret = ide_gd_open(bdev, mode); |
| 247 | unlock_kernel(); |
| 248 | |
| 249 | return ret; |
| 250 | } |
| 251 | |
| 252 | |
Al Viro | b2f21e0 | 2008-10-16 10:34:00 -0400 | [diff] [blame] | 253 | static int ide_gd_release(struct gendisk *disk, fmode_t mode) |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 254 | { |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 255 | struct ide_disk_obj *idkp = ide_drv_g(disk, ide_disk_obj); |
| 256 | ide_drive_t *drive = idkp->drive; |
| 257 | |
Borislav Petkov | 088b1b8 | 2009-01-02 13:34:47 +0100 | [diff] [blame] | 258 | ide_debug_log(IDE_DBG_FUNC, "enter"); |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 259 | |
Arnd Bergmann | 6e9624b | 2010-08-07 18:25:34 +0200 | [diff] [blame] | 260 | lock_kernel(); |
Bartlomiej Zolnierkiewicz | 806f80a | 2008-10-17 18:09:14 +0200 | [diff] [blame] | 261 | if (idkp->openers == 1) |
| 262 | drive->disk_ops->flush(drive); |
| 263 | |
| 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; |
| 267 | } |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 268 | |
| 269 | idkp->openers--; |
| 270 | |
| 271 | ide_disk_put(idkp); |
Arnd Bergmann | 6e9624b | 2010-08-07 18:25:34 +0200 | [diff] [blame] | 272 | unlock_kernel(); |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 273 | |
| 274 | return 0; |
| 275 | } |
| 276 | |
| 277 | static int ide_gd_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 278 | { |
| 279 | struct ide_disk_obj *idkp = ide_drv_g(bdev->bd_disk, ide_disk_obj); |
| 280 | ide_drive_t *drive = idkp->drive; |
| 281 | |
| 282 | geo->heads = drive->bios_head; |
| 283 | geo->sectors = drive->bios_sect; |
| 284 | geo->cylinders = (u16)drive->bios_cyl; /* truncate */ |
| 285 | return 0; |
| 286 | } |
| 287 | |
| 288 | static int ide_gd_media_changed(struct gendisk *disk) |
| 289 | { |
| 290 | struct ide_disk_obj *idkp = ide_drv_g(disk, ide_disk_obj); |
| 291 | ide_drive_t *drive = idkp->drive; |
Bartlomiej Zolnierkiewicz | cedd120 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 292 | int ret; |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 293 | |
| 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; |
| 297 | return 0; |
| 298 | } |
| 299 | |
Bartlomiej Zolnierkiewicz | cedd120 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 300 | ret = !!(drive->dev_flags & IDE_DFLAG_MEDIA_CHANGED); |
| 301 | drive->dev_flags &= ~IDE_DFLAG_MEDIA_CHANGED; |
| 302 | |
| 303 | return ret; |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 304 | } |
| 305 | |
Tejun Heo | c3e33e0 | 2010-05-15 20:09:29 +0200 | [diff] [blame] | 306 | static void ide_gd_unlock_native_capacity(struct gendisk *disk) |
Bartlomiej Zolnierkiewicz | e957b60 | 2009-06-07 13:52:52 +0200 | [diff] [blame] | 307 | { |
| 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; |
| 311 | |
Tejun Heo | c3e33e0 | 2010-05-15 20:09:29 +0200 | [diff] [blame] | 312 | if (disk_ops->unlock_native_capacity) |
| 313 | disk_ops->unlock_native_capacity(drive); |
Bartlomiej Zolnierkiewicz | e957b60 | 2009-06-07 13:52:52 +0200 | [diff] [blame] | 314 | } |
| 315 | |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 316 | static int ide_gd_revalidate_disk(struct gendisk *disk) |
| 317 | { |
| 318 | struct ide_disk_obj *idkp = ide_drv_g(disk, ide_disk_obj); |
Borislav Petkov | 52ebb43 | 2008-11-02 21:40:10 +0100 | [diff] [blame] | 319 | ide_drive_t *drive = idkp->drive; |
| 320 | |
| 321 | if (ide_gd_media_changed(disk)) |
| 322 | drive->disk_ops->get_capacity(drive); |
| 323 | |
| 324 | set_capacity(disk, ide_gd_capacity(drive)); |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 325 | return 0; |
| 326 | } |
| 327 | |
Al Viro | b2f21e0 | 2008-10-16 10:34:00 -0400 | [diff] [blame] | 328 | static int ide_gd_ioctl(struct block_device *bdev, fmode_t mode, |
Bartlomiej Zolnierkiewicz | 806f80a | 2008-10-17 18:09:14 +0200 | [diff] [blame] | 329 | unsigned int cmd, unsigned long arg) |
| 330 | { |
Bartlomiej Zolnierkiewicz | 806f80a | 2008-10-17 18:09:14 +0200 | [diff] [blame] | 331 | struct ide_disk_obj *idkp = ide_drv_g(bdev->bd_disk, ide_disk_obj); |
| 332 | ide_drive_t *drive = idkp->drive; |
| 333 | |
Al Viro | b2f21e0 | 2008-10-16 10:34:00 -0400 | [diff] [blame] | 334 | return drive->disk_ops->ioctl(drive, bdev, mode, cmd, arg); |
Bartlomiej Zolnierkiewicz | 806f80a | 2008-10-17 18:09:14 +0200 | [diff] [blame] | 335 | } |
| 336 | |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 337 | static const struct block_device_operations ide_gd_ops = { |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 338 | .owner = THIS_MODULE, |
Arnd Bergmann | 6e9624b | 2010-08-07 18:25:34 +0200 | [diff] [blame] | 339 | .open = ide_gd_unlocked_open, |
Al Viro | b2f21e0 | 2008-10-16 10:34:00 -0400 | [diff] [blame] | 340 | .release = ide_gd_release, |
Arnd Bergmann | 8a6cfeb | 2010-07-08 10:18:46 +0200 | [diff] [blame] | 341 | .ioctl = ide_gd_ioctl, |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 342 | .getgeo = ide_gd_getgeo, |
| 343 | .media_changed = ide_gd_media_changed, |
Tejun Heo | c3e33e0 | 2010-05-15 20:09:29 +0200 | [diff] [blame] | 344 | .unlock_native_capacity = ide_gd_unlock_native_capacity, |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 345 | .revalidate_disk = ide_gd_revalidate_disk |
| 346 | }; |
| 347 | |
| 348 | static int ide_gd_probe(ide_drive_t *drive) |
| 349 | { |
Bartlomiej Zolnierkiewicz | 806f80a | 2008-10-17 18:09:14 +0200 | [diff] [blame] | 350 | const struct ide_disk_ops *disk_ops = NULL; |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 351 | struct ide_disk_obj *idkp; |
| 352 | struct gendisk *g; |
| 353 | |
| 354 | /* strstr("foo", "") is non-NULL */ |
Bartlomiej Zolnierkiewicz | 806f80a | 2008-10-17 18:09:14 +0200 | [diff] [blame] | 355 | if (!strstr("ide-gd", drive->driver_req)) |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 356 | goto failed; |
| 357 | |
Bartlomiej Zolnierkiewicz | 806f80a | 2008-10-17 18:09:14 +0200 | [diff] [blame] | 358 | #ifdef CONFIG_IDE_GD_ATA |
| 359 | if (drive->media == ide_disk) |
| 360 | disk_ops = &ide_ata_disk_ops; |
| 361 | #endif |
| 362 | #ifdef CONFIG_IDE_GD_ATAPI |
| 363 | if (drive->media == ide_floppy) |
| 364 | disk_ops = &ide_atapi_disk_ops; |
| 365 | #endif |
| 366 | if (disk_ops == NULL) |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 367 | goto failed; |
| 368 | |
Bartlomiej Zolnierkiewicz | 806f80a | 2008-10-17 18:09:14 +0200 | [diff] [blame] | 369 | if (disk_ops->check(drive, DRV_NAME) == 0) { |
| 370 | printk(KERN_ERR PFX "%s: not supported by this driver\n", |
| 371 | drive->name); |
| 372 | goto failed; |
| 373 | } |
| 374 | |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 375 | idkp = kzalloc(sizeof(*idkp), GFP_KERNEL); |
Bartlomiej Zolnierkiewicz | 806f80a | 2008-10-17 18:09:14 +0200 | [diff] [blame] | 376 | if (!idkp) { |
| 377 | printk(KERN_ERR PFX "%s: can't allocate a disk structure\n", |
| 378 | drive->name); |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 379 | goto failed; |
Bartlomiej Zolnierkiewicz | 806f80a | 2008-10-17 18:09:14 +0200 | [diff] [blame] | 380 | } |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 381 | |
| 382 | g = alloc_disk_node(IDE_DISK_MINORS, hwif_to_node(drive->hwif)); |
| 383 | if (!g) |
| 384 | goto out_free_idkp; |
| 385 | |
| 386 | ide_init_disk(g, drive); |
| 387 | |
Bartlomiej Zolnierkiewicz | 8fed436 | 2009-02-25 20:28:24 +0100 | [diff] [blame] | 388 | idkp->dev.parent = &drive->gendev; |
| 389 | idkp->dev.release = ide_disk_release; |
| 390 | dev_set_name(&idkp->dev, dev_name(&drive->gendev)); |
| 391 | |
| 392 | if (device_register(&idkp->dev)) |
| 393 | goto out_free_disk; |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 394 | |
| 395 | idkp->drive = drive; |
| 396 | idkp->driver = &ide_gd_driver; |
| 397 | idkp->disk = g; |
| 398 | |
| 399 | g->private_data = &idkp->driver; |
| 400 | |
| 401 | drive->driver_data = idkp; |
Bartlomiej Zolnierkiewicz | 806f80a | 2008-10-17 18:09:14 +0200 | [diff] [blame] | 402 | drive->debug_mask = debug_mask; |
| 403 | drive->disk_ops = disk_ops; |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 404 | |
Bartlomiej Zolnierkiewicz | 806f80a | 2008-10-17 18:09:14 +0200 | [diff] [blame] | 405 | disk_ops->setup(drive); |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 406 | |
| 407 | set_capacity(g, ide_gd_capacity(drive)); |
| 408 | |
| 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; |
| 415 | add_disk(g); |
| 416 | return 0; |
| 417 | |
Bartlomiej Zolnierkiewicz | 8fed436 | 2009-02-25 20:28:24 +0100 | [diff] [blame] | 418 | out_free_disk: |
| 419 | put_disk(g); |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 420 | out_free_idkp: |
| 421 | kfree(idkp); |
| 422 | failed: |
| 423 | return -ENODEV; |
| 424 | } |
| 425 | |
| 426 | static int __init ide_gd_init(void) |
| 427 | { |
Bartlomiej Zolnierkiewicz | 806f80a | 2008-10-17 18:09:14 +0200 | [diff] [blame] | 428 | printk(KERN_INFO DRV_NAME " driver " IDE_GD_VERSION "\n"); |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 429 | return driver_register(&ide_gd_driver.gen_driver); |
| 430 | } |
| 431 | |
| 432 | static void __exit ide_gd_exit(void) |
| 433 | { |
| 434 | driver_unregister(&ide_gd_driver.gen_driver); |
| 435 | } |
| 436 | |
| 437 | MODULE_ALIAS("ide:*m-disk*"); |
| 438 | MODULE_ALIAS("ide-disk"); |
Bartlomiej Zolnierkiewicz | 806f80a | 2008-10-17 18:09:14 +0200 | [diff] [blame] | 439 | MODULE_ALIAS("ide:*m-floppy*"); |
| 440 | MODULE_ALIAS("ide-floppy"); |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 441 | module_init(ide_gd_init); |
| 442 | module_exit(ide_gd_exit); |
| 443 | MODULE_LICENSE("GPL"); |
Bartlomiej Zolnierkiewicz | 806f80a | 2008-10-17 18:09:14 +0200 | [diff] [blame] | 444 | MODULE_DESCRIPTION("generic ATA/ATAPI disk driver"); |