Merge branch 'for-np' of git://git.wormnet.eu/alex/ts78xx into orion/master
[linux-2.6] / drivers / ide / ide-gd.c
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"
18 #include "ide-floppy.h"
19
20 #define IDE_GD_VERSION  "1.18"
21
22 /* module parameters */
23 static unsigned long debug_mask;
24 module_param(debug_mask, ulong, 0644);
25
26 static DEFINE_MUTEX(ide_disk_ref_mutex);
27
28 static void ide_disk_release(struct kref *);
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
40                         kref_get(&idkp->kref);
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);
51         kref_put(&idkp->kref, ide_disk_release);
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);
69
70         del_gendisk(g);
71
72         drive->disk_ops->flush(drive);
73
74         ide_disk_put(idkp);
75 }
76
77 static void ide_disk_release(struct kref *kref)
78 {
79         struct ide_disk_obj *idkp = to_ide_drv(kref, ide_disk_obj);
80         ide_drive_t *drive = idkp->drive;
81         struct gendisk *g = idkp->disk;
82
83         drive->disk_ops = NULL;
84         drive->driver_data = NULL;
85         g->private_data = NULL;
86         put_disk(g);
87         kfree(idkp);
88 }
89
90 /*
91  * On HPA drives the capacity needs to be
92  * reinitilized on resume otherwise the disk
93  * can not be used and a hard reset is required
94  */
95 static void ide_gd_resume(ide_drive_t *drive)
96 {
97         if (ata_id_hpa_enabled(drive->id))
98                 (void)drive->disk_ops->get_capacity(drive);
99 }
100
101 static void ide_gd_shutdown(ide_drive_t *drive)
102 {
103 #ifdef  CONFIG_ALPHA
104         /* On Alpha, halt(8) doesn't actually turn the machine off,
105            it puts you into the sort of firmware monitor. Typically,
106            it's used to boot another kernel image, so it's not much
107            different from reboot(8). Therefore, we don't need to
108            spin down the disk in this case, especially since Alpha
109            firmware doesn't handle disks in standby mode properly.
110            On the other hand, it's reasonably safe to turn the power
111            off when the shutdown process reaches the firmware prompt,
112            as the firmware initialization takes rather long time -
113            at least 10 seconds, which should be sufficient for
114            the disk to expire its write cache. */
115         if (system_state != SYSTEM_POWER_OFF) {
116 #else
117         if (system_state == SYSTEM_RESTART) {
118 #endif
119                 drive->disk_ops->flush(drive);
120                 return;
121         }
122
123         printk(KERN_INFO "Shutdown: %s\n", drive->name);
124
125         drive->gendev.bus->suspend(&drive->gendev, PMSG_SUSPEND);
126 }
127
128 #ifdef CONFIG_IDE_PROC_FS
129 static ide_proc_entry_t *ide_disk_proc_entries(ide_drive_t *drive)
130 {
131         return (drive->media == ide_disk) ? ide_disk_proc : ide_floppy_proc;
132 }
133
134 static const struct ide_proc_devset *ide_disk_proc_devsets(ide_drive_t *drive)
135 {
136         return (drive->media == ide_disk) ? ide_disk_settings
137                                           : ide_floppy_settings;
138 }
139 #endif
140
141 static ide_startstop_t ide_gd_do_request(ide_drive_t *drive,
142                                          struct request *rq, sector_t sector)
143 {
144         return drive->disk_ops->do_request(drive, rq, sector);
145 }
146
147 static int ide_gd_end_request(ide_drive_t *drive, int uptodate, int nrsecs)
148 {
149         return drive->disk_ops->end_request(drive, uptodate, nrsecs);
150 }
151
152 static struct ide_driver ide_gd_driver = {
153         .gen_driver = {
154                 .owner          = THIS_MODULE,
155                 .name           = "ide-gd",
156                 .bus            = &ide_bus_type,
157         },
158         .probe                  = ide_gd_probe,
159         .remove                 = ide_gd_remove,
160         .resume                 = ide_gd_resume,
161         .shutdown               = ide_gd_shutdown,
162         .version                = IDE_GD_VERSION,
163         .do_request             = ide_gd_do_request,
164         .end_request            = ide_gd_end_request,
165 #ifdef CONFIG_IDE_PROC_FS
166         .proc_entries           = ide_disk_proc_entries,
167         .proc_devsets           = ide_disk_proc_devsets,
168 #endif
169 };
170
171 static int ide_gd_open(struct block_device *bdev, fmode_t mode)
172 {
173         struct gendisk *disk = bdev->bd_disk;
174         struct ide_disk_obj *idkp;
175         ide_drive_t *drive;
176         int ret = 0;
177
178         idkp = ide_disk_get(disk);
179         if (idkp == NULL)
180                 return -ENXIO;
181
182         drive = idkp->drive;
183
184         ide_debug_log(IDE_DBG_FUNC, "Call %s\n", __func__);
185
186         idkp->openers++;
187
188         if ((drive->dev_flags & IDE_DFLAG_REMOVABLE) && idkp->openers == 1) {
189                 drive->dev_flags &= ~IDE_DFLAG_FORMAT_IN_PROGRESS;
190                 /* Just in case */
191
192                 ret = drive->disk_ops->init_media(drive, disk);
193
194                 /*
195                  * Allow O_NDELAY to open a drive without a disk, or with an
196                  * unreadable disk, so that we can get the format capacity
197                  * of the drive or begin the format - Sam
198                  */
199                 if (ret && (mode & FMODE_NDELAY) == 0) {
200                         ret = -EIO;
201                         goto out_put_idkp;
202                 }
203
204                 if ((drive->dev_flags & IDE_DFLAG_WP) && (mode & FMODE_WRITE)) {
205                         ret = -EROFS;
206                         goto out_put_idkp;
207                 }
208
209                 /*
210                  * Ignore the return code from door_lock,
211                  * since the open() has already succeeded,
212                  * and the door_lock is irrelevant at this point.
213                  */
214                 drive->disk_ops->set_doorlock(drive, disk, 1);
215                 drive->dev_flags |= IDE_DFLAG_MEDIA_CHANGED;
216                 check_disk_change(bdev);
217         } else if (drive->dev_flags & IDE_DFLAG_FORMAT_IN_PROGRESS) {
218                 ret = -EBUSY;
219                 goto out_put_idkp;
220         }
221         return 0;
222
223 out_put_idkp:
224         idkp->openers--;
225         ide_disk_put(idkp);
226         return ret;
227 }
228
229 static int ide_gd_release(struct gendisk *disk, fmode_t mode)
230 {
231         struct ide_disk_obj *idkp = ide_drv_g(disk, ide_disk_obj);
232         ide_drive_t *drive = idkp->drive;
233
234         ide_debug_log(IDE_DBG_FUNC, "Call %s\n", __func__);
235
236         if (idkp->openers == 1)
237                 drive->disk_ops->flush(drive);
238
239         if ((drive->dev_flags & IDE_DFLAG_REMOVABLE) && idkp->openers == 1) {
240                 drive->disk_ops->set_doorlock(drive, disk, 0);
241                 drive->dev_flags &= ~IDE_DFLAG_FORMAT_IN_PROGRESS;
242         }
243
244         idkp->openers--;
245
246         ide_disk_put(idkp);
247
248         return 0;
249 }
250
251 static int ide_gd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
252 {
253         struct ide_disk_obj *idkp = ide_drv_g(bdev->bd_disk, ide_disk_obj);
254         ide_drive_t *drive = idkp->drive;
255
256         geo->heads = drive->bios_head;
257         geo->sectors = drive->bios_sect;
258         geo->cylinders = (u16)drive->bios_cyl; /* truncate */
259         return 0;
260 }
261
262 static int ide_gd_media_changed(struct gendisk *disk)
263 {
264         struct ide_disk_obj *idkp = ide_drv_g(disk, ide_disk_obj);
265         ide_drive_t *drive = idkp->drive;
266         int ret;
267
268         /* do not scan partitions twice if this is a removable device */
269         if (drive->dev_flags & IDE_DFLAG_ATTACH) {
270                 drive->dev_flags &= ~IDE_DFLAG_ATTACH;
271                 return 0;
272         }
273
274         ret = !!(drive->dev_flags & IDE_DFLAG_MEDIA_CHANGED);
275         drive->dev_flags &= ~IDE_DFLAG_MEDIA_CHANGED;
276
277         return ret;
278 }
279
280 static int ide_gd_revalidate_disk(struct gendisk *disk)
281 {
282         struct ide_disk_obj *idkp = ide_drv_g(disk, ide_disk_obj);
283         ide_drive_t *drive = idkp->drive;
284
285         if (ide_gd_media_changed(disk))
286                 drive->disk_ops->get_capacity(drive);
287
288         set_capacity(disk, ide_gd_capacity(drive));
289         return 0;
290 }
291
292 static int ide_gd_ioctl(struct block_device *bdev, fmode_t mode,
293                              unsigned int cmd, unsigned long arg)
294 {
295         struct ide_disk_obj *idkp = ide_drv_g(bdev->bd_disk, ide_disk_obj);
296         ide_drive_t *drive = idkp->drive;
297
298         return drive->disk_ops->ioctl(drive, bdev, mode, cmd, arg);
299 }
300
301 static struct block_device_operations ide_gd_ops = {
302         .owner                  = THIS_MODULE,
303         .open                   = ide_gd_open,
304         .release                = ide_gd_release,
305         .locked_ioctl           = ide_gd_ioctl,
306         .getgeo                 = ide_gd_getgeo,
307         .media_changed          = ide_gd_media_changed,
308         .revalidate_disk        = ide_gd_revalidate_disk
309 };
310
311 static int ide_gd_probe(ide_drive_t *drive)
312 {
313         const struct ide_disk_ops *disk_ops = NULL;
314         struct ide_disk_obj *idkp;
315         struct gendisk *g;
316
317         /* strstr("foo", "") is non-NULL */
318         if (!strstr("ide-gd", drive->driver_req))
319                 goto failed;
320
321 #ifdef CONFIG_IDE_GD_ATA
322         if (drive->media == ide_disk)
323                 disk_ops = &ide_ata_disk_ops;
324 #endif
325 #ifdef CONFIG_IDE_GD_ATAPI
326         if (drive->media == ide_floppy)
327                 disk_ops = &ide_atapi_disk_ops;
328 #endif
329         if (disk_ops == NULL)
330                 goto failed;
331
332         if (disk_ops->check(drive, DRV_NAME) == 0) {
333                 printk(KERN_ERR PFX "%s: not supported by this driver\n",
334                         drive->name);
335                 goto failed;
336         }
337
338         idkp = kzalloc(sizeof(*idkp), GFP_KERNEL);
339         if (!idkp) {
340                 printk(KERN_ERR PFX "%s: can't allocate a disk structure\n",
341                         drive->name);
342                 goto failed;
343         }
344
345         g = alloc_disk_node(IDE_DISK_MINORS, hwif_to_node(drive->hwif));
346         if (!g)
347                 goto out_free_idkp;
348
349         ide_init_disk(g, drive);
350
351         kref_init(&idkp->kref);
352
353         idkp->drive = drive;
354         idkp->driver = &ide_gd_driver;
355         idkp->disk = g;
356
357         g->private_data = &idkp->driver;
358
359         drive->driver_data = idkp;
360         drive->debug_mask = debug_mask;
361         drive->disk_ops = disk_ops;
362
363         disk_ops->setup(drive);
364
365         set_capacity(g, ide_gd_capacity(drive));
366
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;
373         add_disk(g);
374         return 0;
375
376 out_free_idkp:
377         kfree(idkp);
378 failed:
379         return -ENODEV;
380 }
381
382 static int __init ide_gd_init(void)
383 {
384         printk(KERN_INFO DRV_NAME " driver " IDE_GD_VERSION "\n");
385         return driver_register(&ide_gd_driver.gen_driver);
386 }
387
388 static void __exit ide_gd_exit(void)
389 {
390         driver_unregister(&ide_gd_driver.gen_driver);
391 }
392
393 MODULE_ALIAS("ide:*m-disk*");
394 MODULE_ALIAS("ide-disk");
395 MODULE_ALIAS("ide:*m-floppy*");
396 MODULE_ALIAS("ide-floppy");
397 module_init(ide_gd_init);
398 module_exit(ide_gd_exit);
399 MODULE_LICENSE("GPL");
400 MODULE_DESCRIPTION("generic ATA/ATAPI disk driver");