Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 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 | |
| 11 | #if !defined(CONFIG_DEBUG_BLOCK_EXT_DEVT) |
| 12 | #define IDE_DISK_MINORS (1 << PARTN_BITS) |
| 13 | #else |
| 14 | #define IDE_DISK_MINORS 0 |
| 15 | #endif |
| 16 | |
| 17 | #include "ide-disk.h" |
Bartlomiej Zolnierkiewicz | 806f80a | 2008-10-17 18:09:14 +0200 | [diff] [blame] | 18 | #include "ide-floppy.h" |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 19 | |
| 20 | #define IDE_GD_VERSION "1.18" |
| 21 | |
Bartlomiej Zolnierkiewicz | 806f80a | 2008-10-17 18:09:14 +0200 | [diff] [blame] | 22 | /* module parameters */ |
| 23 | static unsigned long debug_mask; |
| 24 | module_param(debug_mask, ulong, 0644); |
| 25 | |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 26 | static DEFINE_MUTEX(ide_disk_ref_mutex); |
| 27 | |
Bartlomiej Zolnierkiewicz | 8fed436 | 2009-02-25 20:28:24 +0100 | [diff] [blame] | 28 | static void ide_disk_release(struct device *); |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 29 | |
| 30 | static struct ide_disk_obj *ide_disk_get(struct gendisk *disk) |
| 31 | { |
| 32 | struct ide_disk_obj *idkp = NULL; |
| 33 | |
| 34 | mutex_lock(&ide_disk_ref_mutex); |
| 35 | idkp = ide_drv_g(disk, ide_disk_obj); |
| 36 | if (idkp) { |
| 37 | if (ide_device_get(idkp->drive)) |
| 38 | idkp = NULL; |
| 39 | else |
Bartlomiej Zolnierkiewicz | 8fed436 | 2009-02-25 20:28:24 +0100 | [diff] [blame] | 40 | get_device(&idkp->dev); |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 41 | } |
| 42 | mutex_unlock(&ide_disk_ref_mutex); |
| 43 | return idkp; |
| 44 | } |
| 45 | |
| 46 | static void ide_disk_put(struct ide_disk_obj *idkp) |
| 47 | { |
| 48 | ide_drive_t *drive = idkp->drive; |
| 49 | |
| 50 | mutex_lock(&ide_disk_ref_mutex); |
Bartlomiej Zolnierkiewicz | 8fed436 | 2009-02-25 20:28:24 +0100 | [diff] [blame] | 51 | put_device(&idkp->dev); |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 52 | ide_device_put(drive); |
| 53 | mutex_unlock(&ide_disk_ref_mutex); |
| 54 | } |
| 55 | |
| 56 | sector_t ide_gd_capacity(ide_drive_t *drive) |
| 57 | { |
| 58 | return drive->capacity64; |
| 59 | } |
| 60 | |
| 61 | static int ide_gd_probe(ide_drive_t *); |
| 62 | |
| 63 | static void ide_gd_remove(ide_drive_t *drive) |
| 64 | { |
| 65 | struct ide_disk_obj *idkp = drive->driver_data; |
| 66 | struct gendisk *g = idkp->disk; |
| 67 | |
| 68 | ide_proc_unregister_driver(drive, idkp->driver); |
Bartlomiej Zolnierkiewicz | 8fed436 | 2009-02-25 20:28:24 +0100 | [diff] [blame] | 69 | device_del(&idkp->dev); |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 70 | del_gendisk(g); |
Bartlomiej Zolnierkiewicz | 806f80a | 2008-10-17 18:09:14 +0200 | [diff] [blame] | 71 | drive->disk_ops->flush(drive); |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 72 | |
Bartlomiej Zolnierkiewicz | 8fed436 | 2009-02-25 20:28:24 +0100 | [diff] [blame] | 73 | mutex_lock(&ide_disk_ref_mutex); |
| 74 | put_device(&idkp->dev); |
| 75 | mutex_unlock(&ide_disk_ref_mutex); |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 76 | } |
| 77 | |
Bartlomiej Zolnierkiewicz | 8fed436 | 2009-02-25 20:28:24 +0100 | [diff] [blame] | 78 | static void ide_disk_release(struct device *dev) |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 79 | { |
Bartlomiej Zolnierkiewicz | 8fed436 | 2009-02-25 20:28:24 +0100 | [diff] [blame] | 80 | struct ide_disk_obj *idkp = to_ide_drv(dev, ide_disk_obj); |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 81 | ide_drive_t *drive = idkp->drive; |
| 82 | struct gendisk *g = idkp->disk; |
| 83 | |
Bartlomiej Zolnierkiewicz | 806f80a | 2008-10-17 18:09:14 +0200 | [diff] [blame] | 84 | drive->disk_ops = NULL; |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 85 | drive->driver_data = NULL; |
| 86 | g->private_data = NULL; |
| 87 | put_disk(g); |
| 88 | kfree(idkp); |
| 89 | } |
| 90 | |
| 91 | /* |
| 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 |
| 95 | */ |
| 96 | static void ide_gd_resume(ide_drive_t *drive) |
| 97 | { |
| 98 | if (ata_id_hpa_enabled(drive->id)) |
Bartlomiej Zolnierkiewicz | 806f80a | 2008-10-17 18:09:14 +0200 | [diff] [blame] | 99 | (void)drive->disk_ops->get_capacity(drive); |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | static void ide_gd_shutdown(ide_drive_t *drive) |
| 103 | { |
| 104 | #ifdef CONFIG_ALPHA |
| 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) { |
| 117 | #else |
| 118 | if (system_state == SYSTEM_RESTART) { |
| 119 | #endif |
Bartlomiej Zolnierkiewicz | 806f80a | 2008-10-17 18:09:14 +0200 | [diff] [blame] | 120 | drive->disk_ops->flush(drive); |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 121 | return; |
| 122 | } |
| 123 | |
| 124 | printk(KERN_INFO "Shutdown: %s\n", drive->name); |
| 125 | |
| 126 | drive->gendev.bus->suspend(&drive->gendev, PMSG_SUSPEND); |
| 127 | } |
| 128 | |
Bartlomiej Zolnierkiewicz | 79cb380 | 2008-10-17 18:09:13 +0200 | [diff] [blame] | 129 | #ifdef CONFIG_IDE_PROC_FS |
| 130 | static ide_proc_entry_t *ide_disk_proc_entries(ide_drive_t *drive) |
| 131 | { |
Bartlomiej Zolnierkiewicz | 806f80a | 2008-10-17 18:09:14 +0200 | [diff] [blame] | 132 | return (drive->media == ide_disk) ? ide_disk_proc : ide_floppy_proc; |
Bartlomiej Zolnierkiewicz | 79cb380 | 2008-10-17 18:09:13 +0200 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | static const struct ide_proc_devset *ide_disk_proc_devsets(ide_drive_t *drive) |
| 136 | { |
Bartlomiej Zolnierkiewicz | 806f80a | 2008-10-17 18:09:14 +0200 | [diff] [blame] | 137 | return (drive->media == ide_disk) ? ide_disk_settings |
| 138 | : ide_floppy_settings; |
Bartlomiej Zolnierkiewicz | 79cb380 | 2008-10-17 18:09:13 +0200 | [diff] [blame] | 139 | } |
| 140 | #endif |
| 141 | |
Bartlomiej Zolnierkiewicz | 806f80a | 2008-10-17 18:09:14 +0200 | [diff] [blame] | 142 | static ide_startstop_t ide_gd_do_request(ide_drive_t *drive, |
| 143 | struct request *rq, sector_t sector) |
| 144 | { |
| 145 | return drive->disk_ops->do_request(drive, rq, sector); |
| 146 | } |
| 147 | |
| 148 | static int ide_gd_end_request(ide_drive_t *drive, int uptodate, int nrsecs) |
| 149 | { |
| 150 | return drive->disk_ops->end_request(drive, uptodate, nrsecs); |
| 151 | } |
| 152 | |
Bartlomiej Zolnierkiewicz | 7f3c868 | 2009-01-06 17:20:53 +0100 | [diff] [blame] | 153 | static struct ide_driver ide_gd_driver = { |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 154 | .gen_driver = { |
| 155 | .owner = THIS_MODULE, |
Bartlomiej Zolnierkiewicz | 806f80a | 2008-10-17 18:09:14 +0200 | [diff] [blame] | 156 | .name = "ide-gd", |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 157 | .bus = &ide_bus_type, |
| 158 | }, |
| 159 | .probe = ide_gd_probe, |
| 160 | .remove = ide_gd_remove, |
| 161 | .resume = ide_gd_resume, |
| 162 | .shutdown = ide_gd_shutdown, |
| 163 | .version = IDE_GD_VERSION, |
Bartlomiej Zolnierkiewicz | 806f80a | 2008-10-17 18:09:14 +0200 | [diff] [blame] | 164 | .do_request = ide_gd_do_request, |
| 165 | .end_request = ide_gd_end_request, |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 166 | #ifdef CONFIG_IDE_PROC_FS |
Bartlomiej Zolnierkiewicz | 79cb380 | 2008-10-17 18:09:13 +0200 | [diff] [blame] | 167 | .proc_entries = ide_disk_proc_entries, |
| 168 | .proc_devsets = ide_disk_proc_devsets, |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 169 | #endif |
| 170 | }; |
| 171 | |
Al Viro | b2f21e0 | 2008-10-16 10:34:00 -0400 | [diff] [blame] | 172 | static int ide_gd_open(struct block_device *bdev, fmode_t mode) |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 173 | { |
Al Viro | b2f21e0 | 2008-10-16 10:34:00 -0400 | [diff] [blame] | 174 | struct gendisk *disk = bdev->bd_disk; |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 175 | struct ide_disk_obj *idkp; |
| 176 | ide_drive_t *drive; |
Bartlomiej Zolnierkiewicz | 806f80a | 2008-10-17 18:09:14 +0200 | [diff] [blame] | 177 | int ret = 0; |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 178 | |
| 179 | idkp = ide_disk_get(disk); |
| 180 | if (idkp == NULL) |
| 181 | return -ENXIO; |
| 182 | |
| 183 | drive = idkp->drive; |
| 184 | |
Bartlomiej Zolnierkiewicz | 806f80a | 2008-10-17 18:09:14 +0200 | [diff] [blame] | 185 | ide_debug_log(IDE_DBG_FUNC, "Call %s\n", __func__); |
| 186 | |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 187 | idkp->openers++; |
| 188 | |
| 189 | if ((drive->dev_flags & IDE_DFLAG_REMOVABLE) && idkp->openers == 1) { |
Bartlomiej Zolnierkiewicz | 806f80a | 2008-10-17 18:09:14 +0200 | [diff] [blame] | 190 | drive->dev_flags &= ~IDE_DFLAG_FORMAT_IN_PROGRESS; |
| 191 | /* Just in case */ |
| 192 | |
| 193 | ret = drive->disk_ops->init_media(drive, disk); |
| 194 | |
| 195 | /* |
| 196 | * Allow O_NDELAY to open a drive without a disk, or with an |
| 197 | * unreadable disk, so that we can get the format capacity |
| 198 | * of the drive or begin the format - Sam |
| 199 | */ |
Al Viro | b2f21e0 | 2008-10-16 10:34:00 -0400 | [diff] [blame] | 200 | if (ret && (mode & FMODE_NDELAY) == 0) { |
Bartlomiej Zolnierkiewicz | 806f80a | 2008-10-17 18:09:14 +0200 | [diff] [blame] | 201 | ret = -EIO; |
| 202 | goto out_put_idkp; |
| 203 | } |
| 204 | |
Al Viro | b2f21e0 | 2008-10-16 10:34:00 -0400 | [diff] [blame] | 205 | if ((drive->dev_flags & IDE_DFLAG_WP) && (mode & FMODE_WRITE)) { |
Bartlomiej Zolnierkiewicz | 806f80a | 2008-10-17 18:09:14 +0200 | [diff] [blame] | 206 | ret = -EROFS; |
| 207 | goto out_put_idkp; |
| 208 | } |
| 209 | |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 210 | /* |
| 211 | * Ignore the return code from door_lock, |
| 212 | * since the open() has already succeeded, |
| 213 | * and the door_lock is irrelevant at this point. |
| 214 | */ |
Bartlomiej Zolnierkiewicz | 806f80a | 2008-10-17 18:09:14 +0200 | [diff] [blame] | 215 | drive->disk_ops->set_doorlock(drive, disk, 1); |
Bartlomiej Zolnierkiewicz | cedd120 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 216 | drive->dev_flags |= IDE_DFLAG_MEDIA_CHANGED; |
Al Viro | b2f21e0 | 2008-10-16 10:34:00 -0400 | [diff] [blame] | 217 | check_disk_change(bdev); |
Bartlomiej Zolnierkiewicz | 806f80a | 2008-10-17 18:09:14 +0200 | [diff] [blame] | 218 | } else if (drive->dev_flags & IDE_DFLAG_FORMAT_IN_PROGRESS) { |
| 219 | ret = -EBUSY; |
| 220 | goto out_put_idkp; |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 221 | } |
| 222 | return 0; |
Bartlomiej Zolnierkiewicz | 806f80a | 2008-10-17 18:09:14 +0200 | [diff] [blame] | 223 | |
| 224 | out_put_idkp: |
| 225 | idkp->openers--; |
| 226 | ide_disk_put(idkp); |
| 227 | return ret; |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 228 | } |
| 229 | |
Al Viro | b2f21e0 | 2008-10-16 10:34:00 -0400 | [diff] [blame] | 230 | static int ide_gd_release(struct gendisk *disk, fmode_t mode) |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 231 | { |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 232 | struct ide_disk_obj *idkp = ide_drv_g(disk, ide_disk_obj); |
| 233 | ide_drive_t *drive = idkp->drive; |
| 234 | |
Bartlomiej Zolnierkiewicz | 806f80a | 2008-10-17 18:09:14 +0200 | [diff] [blame] | 235 | ide_debug_log(IDE_DBG_FUNC, "Call %s\n", __func__); |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 236 | |
Bartlomiej Zolnierkiewicz | 806f80a | 2008-10-17 18:09:14 +0200 | [diff] [blame] | 237 | if (idkp->openers == 1) |
| 238 | drive->disk_ops->flush(drive); |
| 239 | |
| 240 | if ((drive->dev_flags & IDE_DFLAG_REMOVABLE) && idkp->openers == 1) { |
| 241 | drive->disk_ops->set_doorlock(drive, disk, 0); |
| 242 | drive->dev_flags &= ~IDE_DFLAG_FORMAT_IN_PROGRESS; |
| 243 | } |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 244 | |
| 245 | idkp->openers--; |
| 246 | |
| 247 | ide_disk_put(idkp); |
| 248 | |
| 249 | return 0; |
| 250 | } |
| 251 | |
| 252 | static int ide_gd_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 253 | { |
| 254 | struct ide_disk_obj *idkp = ide_drv_g(bdev->bd_disk, ide_disk_obj); |
| 255 | ide_drive_t *drive = idkp->drive; |
| 256 | |
| 257 | geo->heads = drive->bios_head; |
| 258 | geo->sectors = drive->bios_sect; |
| 259 | geo->cylinders = (u16)drive->bios_cyl; /* truncate */ |
| 260 | return 0; |
| 261 | } |
| 262 | |
| 263 | static int ide_gd_media_changed(struct gendisk *disk) |
| 264 | { |
| 265 | struct ide_disk_obj *idkp = ide_drv_g(disk, ide_disk_obj); |
| 266 | ide_drive_t *drive = idkp->drive; |
Bartlomiej Zolnierkiewicz | cedd120 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 267 | int ret; |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 268 | |
| 269 | /* do not scan partitions twice if this is a removable device */ |
| 270 | if (drive->dev_flags & IDE_DFLAG_ATTACH) { |
| 271 | drive->dev_flags &= ~IDE_DFLAG_ATTACH; |
| 272 | return 0; |
| 273 | } |
| 274 | |
Bartlomiej Zolnierkiewicz | cedd120 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 275 | ret = !!(drive->dev_flags & IDE_DFLAG_MEDIA_CHANGED); |
| 276 | drive->dev_flags &= ~IDE_DFLAG_MEDIA_CHANGED; |
| 277 | |
| 278 | return ret; |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | static int ide_gd_revalidate_disk(struct gendisk *disk) |
| 282 | { |
| 283 | struct ide_disk_obj *idkp = ide_drv_g(disk, ide_disk_obj); |
Borislav Petkov | 52ebb43 | 2008-11-02 21:40:10 +0100 | [diff] [blame] | 284 | ide_drive_t *drive = idkp->drive; |
| 285 | |
| 286 | if (ide_gd_media_changed(disk)) |
| 287 | drive->disk_ops->get_capacity(drive); |
| 288 | |
| 289 | set_capacity(disk, ide_gd_capacity(drive)); |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 290 | return 0; |
| 291 | } |
| 292 | |
Al Viro | b2f21e0 | 2008-10-16 10:34:00 -0400 | [diff] [blame] | 293 | static int ide_gd_ioctl(struct block_device *bdev, fmode_t mode, |
Bartlomiej Zolnierkiewicz | 806f80a | 2008-10-17 18:09:14 +0200 | [diff] [blame] | 294 | unsigned int cmd, unsigned long arg) |
| 295 | { |
Bartlomiej Zolnierkiewicz | 806f80a | 2008-10-17 18:09:14 +0200 | [diff] [blame] | 296 | struct ide_disk_obj *idkp = ide_drv_g(bdev->bd_disk, ide_disk_obj); |
| 297 | ide_drive_t *drive = idkp->drive; |
| 298 | |
Al Viro | b2f21e0 | 2008-10-16 10:34:00 -0400 | [diff] [blame] | 299 | return drive->disk_ops->ioctl(drive, bdev, mode, cmd, arg); |
Bartlomiej Zolnierkiewicz | 806f80a | 2008-10-17 18:09:14 +0200 | [diff] [blame] | 300 | } |
| 301 | |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 302 | static struct block_device_operations ide_gd_ops = { |
| 303 | .owner = THIS_MODULE, |
Al Viro | b2f21e0 | 2008-10-16 10:34:00 -0400 | [diff] [blame] | 304 | .open = ide_gd_open, |
| 305 | .release = ide_gd_release, |
| 306 | .locked_ioctl = ide_gd_ioctl, |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 307 | .getgeo = ide_gd_getgeo, |
| 308 | .media_changed = ide_gd_media_changed, |
| 309 | .revalidate_disk = ide_gd_revalidate_disk |
| 310 | }; |
| 311 | |
| 312 | static int ide_gd_probe(ide_drive_t *drive) |
| 313 | { |
Bartlomiej Zolnierkiewicz | 806f80a | 2008-10-17 18:09:14 +0200 | [diff] [blame] | 314 | const struct ide_disk_ops *disk_ops = NULL; |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 315 | struct ide_disk_obj *idkp; |
| 316 | struct gendisk *g; |
| 317 | |
| 318 | /* strstr("foo", "") is non-NULL */ |
Bartlomiej Zolnierkiewicz | 806f80a | 2008-10-17 18:09:14 +0200 | [diff] [blame] | 319 | if (!strstr("ide-gd", drive->driver_req)) |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 320 | goto failed; |
| 321 | |
Bartlomiej Zolnierkiewicz | 806f80a | 2008-10-17 18:09:14 +0200 | [diff] [blame] | 322 | #ifdef CONFIG_IDE_GD_ATA |
| 323 | if (drive->media == ide_disk) |
| 324 | disk_ops = &ide_ata_disk_ops; |
| 325 | #endif |
| 326 | #ifdef CONFIG_IDE_GD_ATAPI |
| 327 | if (drive->media == ide_floppy) |
| 328 | disk_ops = &ide_atapi_disk_ops; |
| 329 | #endif |
| 330 | if (disk_ops == NULL) |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 331 | goto failed; |
| 332 | |
Bartlomiej Zolnierkiewicz | 806f80a | 2008-10-17 18:09:14 +0200 | [diff] [blame] | 333 | if (disk_ops->check(drive, DRV_NAME) == 0) { |
| 334 | printk(KERN_ERR PFX "%s: not supported by this driver\n", |
| 335 | drive->name); |
| 336 | goto failed; |
| 337 | } |
| 338 | |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 339 | idkp = kzalloc(sizeof(*idkp), GFP_KERNEL); |
Bartlomiej Zolnierkiewicz | 806f80a | 2008-10-17 18:09:14 +0200 | [diff] [blame] | 340 | if (!idkp) { |
| 341 | printk(KERN_ERR PFX "%s: can't allocate a disk structure\n", |
| 342 | drive->name); |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 343 | goto failed; |
Bartlomiej Zolnierkiewicz | 806f80a | 2008-10-17 18:09:14 +0200 | [diff] [blame] | 344 | } |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 345 | |
| 346 | g = alloc_disk_node(IDE_DISK_MINORS, hwif_to_node(drive->hwif)); |
| 347 | if (!g) |
| 348 | goto out_free_idkp; |
| 349 | |
| 350 | ide_init_disk(g, drive); |
| 351 | |
Bartlomiej Zolnierkiewicz | 8fed436 | 2009-02-25 20:28:24 +0100 | [diff] [blame] | 352 | idkp->dev.parent = &drive->gendev; |
| 353 | idkp->dev.release = ide_disk_release; |
| 354 | dev_set_name(&idkp->dev, dev_name(&drive->gendev)); |
| 355 | |
| 356 | if (device_register(&idkp->dev)) |
| 357 | goto out_free_disk; |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 358 | |
| 359 | idkp->drive = drive; |
| 360 | idkp->driver = &ide_gd_driver; |
| 361 | idkp->disk = g; |
| 362 | |
| 363 | g->private_data = &idkp->driver; |
| 364 | |
| 365 | drive->driver_data = idkp; |
Bartlomiej Zolnierkiewicz | 806f80a | 2008-10-17 18:09:14 +0200 | [diff] [blame] | 366 | drive->debug_mask = debug_mask; |
| 367 | drive->disk_ops = disk_ops; |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 368 | |
Bartlomiej Zolnierkiewicz | 806f80a | 2008-10-17 18:09:14 +0200 | [diff] [blame] | 369 | disk_ops->setup(drive); |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 370 | |
| 371 | set_capacity(g, ide_gd_capacity(drive)); |
| 372 | |
| 373 | g->minors = IDE_DISK_MINORS; |
| 374 | g->driverfs_dev = &drive->gendev; |
| 375 | g->flags |= GENHD_FL_EXT_DEVT; |
| 376 | if (drive->dev_flags & IDE_DFLAG_REMOVABLE) |
| 377 | g->flags = GENHD_FL_REMOVABLE; |
| 378 | g->fops = &ide_gd_ops; |
| 379 | add_disk(g); |
| 380 | return 0; |
| 381 | |
Bartlomiej Zolnierkiewicz | 8fed436 | 2009-02-25 20:28:24 +0100 | [diff] [blame] | 382 | out_free_disk: |
| 383 | put_disk(g); |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 384 | out_free_idkp: |
| 385 | kfree(idkp); |
| 386 | failed: |
| 387 | return -ENODEV; |
| 388 | } |
| 389 | |
| 390 | static int __init ide_gd_init(void) |
| 391 | { |
Bartlomiej Zolnierkiewicz | 806f80a | 2008-10-17 18:09:14 +0200 | [diff] [blame] | 392 | printk(KERN_INFO DRV_NAME " driver " IDE_GD_VERSION "\n"); |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 393 | return driver_register(&ide_gd_driver.gen_driver); |
| 394 | } |
| 395 | |
| 396 | static void __exit ide_gd_exit(void) |
| 397 | { |
| 398 | driver_unregister(&ide_gd_driver.gen_driver); |
| 399 | } |
| 400 | |
| 401 | MODULE_ALIAS("ide:*m-disk*"); |
| 402 | MODULE_ALIAS("ide-disk"); |
Bartlomiej Zolnierkiewicz | 806f80a | 2008-10-17 18:09:14 +0200 | [diff] [blame] | 403 | MODULE_ALIAS("ide:*m-floppy*"); |
| 404 | MODULE_ALIAS("ide-floppy"); |
Bartlomiej Zolnierkiewicz | 5fef0e5 | 2008-10-17 18:09:12 +0200 | [diff] [blame] | 405 | module_init(ide_gd_init); |
| 406 | module_exit(ide_gd_exit); |
| 407 | MODULE_LICENSE("GPL"); |
Bartlomiej Zolnierkiewicz | 806f80a | 2008-10-17 18:09:14 +0200 | [diff] [blame] | 408 | MODULE_DESCRIPTION("generic ATA/ATAPI disk driver"); |