Linux Audio

Check our new training course

Loading...
v5.9
  1// SPDX-License-Identifier: GPL-2.0-only
  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>
 11#include <linux/dmi.h>
 12#include <linux/slab.h>
 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"
 21#include "ide-floppy.h"
 22
 23#define IDE_GD_VERSION	"1.18"
 24
 25/* module parameters */
 26static DEFINE_MUTEX(ide_gd_mutex);
 27static unsigned long debug_mask;
 28module_param(debug_mask, ulong, 0644);
 29
 30static DEFINE_MUTEX(ide_disk_ref_mutex);
 31
 32static void ide_disk_release(struct device *);
 33
 34static struct ide_disk_obj *ide_disk_get(struct gendisk *disk)
 35{
 36	struct ide_disk_obj *idkp = NULL;
 37
 38	mutex_lock(&ide_disk_ref_mutex);
 39	idkp = ide_drv_g(disk, ide_disk_obj);
 40	if (idkp) {
 41		if (ide_device_get(idkp->drive))
 42			idkp = NULL;
 43		else
 44			get_device(&idkp->dev);
 45	}
 46	mutex_unlock(&ide_disk_ref_mutex);
 47	return idkp;
 48}
 49
 50static void ide_disk_put(struct ide_disk_obj *idkp)
 51{
 52	ide_drive_t *drive = idkp->drive;
 53
 54	mutex_lock(&ide_disk_ref_mutex);
 55	put_device(&idkp->dev);
 56	ide_device_put(drive);
 57	mutex_unlock(&ide_disk_ref_mutex);
 58}
 59
 60sector_t ide_gd_capacity(ide_drive_t *drive)
 61{
 62	return drive->capacity64;
 63}
 64
 65static int ide_gd_probe(ide_drive_t *);
 66
 67static void ide_gd_remove(ide_drive_t *drive)
 68{
 69	struct ide_disk_obj *idkp = drive->driver_data;
 70	struct gendisk *g = idkp->disk;
 71
 72	ide_proc_unregister_driver(drive, idkp->driver);
 73	device_del(&idkp->dev);
 74	del_gendisk(g);
 75	drive->disk_ops->flush(drive);
 76
 77	mutex_lock(&ide_disk_ref_mutex);
 78	put_device(&idkp->dev);
 79	mutex_unlock(&ide_disk_ref_mutex);
 80}
 81
 82static void ide_disk_release(struct device *dev)
 83{
 84	struct ide_disk_obj *idkp = to_ide_drv(dev, ide_disk_obj);
 85	ide_drive_t *drive = idkp->drive;
 86	struct gendisk *g = idkp->disk;
 87
 88	drive->disk_ops = NULL;
 89	drive->driver_data = NULL;
 90	g->private_data = NULL;
 91	put_disk(g);
 92	kfree(idkp);
 93}
 94
 95/*
 96 * On HPA drives the capacity needs to be
 97 * reinitialized on resume otherwise the disk
 98 * can not be used and a hard reset is required
 99 */
100static void ide_gd_resume(ide_drive_t *drive)
101{
102	if (ata_id_hpa_enabled(drive->id))
103		(void)drive->disk_ops->get_capacity(drive);
104}
105
106static const struct dmi_system_id ide_coldreboot_table[] = {
107	{
108		/* Acer TravelMate 66x cuts power during reboot */
109		.ident   = "Acer TravelMate 660",
110		.matches = {
111			DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
112			DMI_MATCH(DMI_PRODUCT_NAME, "TravelMate 660"),
113		},
114	},
115
116	{ }	/* terminate list */
117};
118
119static void ide_gd_shutdown(ide_drive_t *drive)
120{
121#ifdef	CONFIG_ALPHA
122	/* On Alpha, halt(8) doesn't actually turn the machine off,
123	   it puts you into the sort of firmware monitor. Typically,
124	   it's used to boot another kernel image, so it's not much
125	   different from reboot(8). Therefore, we don't need to
126	   spin down the disk in this case, especially since Alpha
127	   firmware doesn't handle disks in standby mode properly.
128	   On the other hand, it's reasonably safe to turn the power
129	   off when the shutdown process reaches the firmware prompt,
130	   as the firmware initialization takes rather long time -
131	   at least 10 seconds, which should be sufficient for
132	   the disk to expire its write cache. */
133	if (system_state != SYSTEM_POWER_OFF) {
134#else
135	if (system_state == SYSTEM_RESTART &&
136		!dmi_check_system(ide_coldreboot_table)) {
137#endif
138		drive->disk_ops->flush(drive);
139		return;
140	}
141
142	printk(KERN_INFO "Shutdown: %s\n", drive->name);
143
144	drive->gendev.bus->suspend(&drive->gendev, PMSG_SUSPEND);
145}
146
147#ifdef CONFIG_IDE_PROC_FS
148static ide_proc_entry_t *ide_disk_proc_entries(ide_drive_t *drive)
149{
150	return (drive->media == ide_disk) ? ide_disk_proc : ide_floppy_proc;
151}
152
153static const struct ide_proc_devset *ide_disk_proc_devsets(ide_drive_t *drive)
154{
155	return (drive->media == ide_disk) ? ide_disk_settings
156					  : ide_floppy_settings;
157}
158#endif
159
160static ide_startstop_t ide_gd_do_request(ide_drive_t *drive,
161					 struct request *rq, sector_t sector)
162{
163	return drive->disk_ops->do_request(drive, rq, sector);
164}
165
166static struct ide_driver ide_gd_driver = {
167	.gen_driver = {
168		.owner		= THIS_MODULE,
169		.name		= "ide-gd",
170		.bus		= &ide_bus_type,
171	},
172	.probe			= ide_gd_probe,
173	.remove			= ide_gd_remove,
174	.resume			= ide_gd_resume,
175	.shutdown		= ide_gd_shutdown,
176	.version		= IDE_GD_VERSION,
177	.do_request		= ide_gd_do_request,
178#ifdef CONFIG_IDE_PROC_FS
179	.proc_entries		= ide_disk_proc_entries,
180	.proc_devsets		= ide_disk_proc_devsets,
181#endif
182};
183
184static int ide_gd_open(struct block_device *bdev, fmode_t mode)
185{
186	struct gendisk *disk = bdev->bd_disk;
187	struct ide_disk_obj *idkp;
188	ide_drive_t *drive;
189	int ret = 0;
190
191	idkp = ide_disk_get(disk);
192	if (idkp == NULL)
193		return -ENXIO;
194
195	drive = idkp->drive;
196
197	ide_debug_log(IDE_DBG_FUNC, "enter");
198
199	idkp->openers++;
200
201	if ((drive->dev_flags & IDE_DFLAG_REMOVABLE) && idkp->openers == 1) {
202		drive->dev_flags &= ~IDE_DFLAG_FORMAT_IN_PROGRESS;
203		/* Just in case */
204
205		ret = drive->disk_ops->init_media(drive, disk);
206
207		/*
208		 * Allow O_NDELAY to open a drive without a disk, or with an
209		 * unreadable disk, so that we can get the format capacity
210		 * of the drive or begin the format - Sam
211		 */
212		if (ret && (mode & FMODE_NDELAY) == 0) {
213			ret = -EIO;
214			goto out_put_idkp;
215		}
216
217		if ((drive->dev_flags & IDE_DFLAG_WP) && (mode & FMODE_WRITE)) {
218			ret = -EROFS;
219			goto out_put_idkp;
220		}
221
222		/*
223		 * Ignore the return code from door_lock,
224		 * since the open() has already succeeded,
225		 * and the door_lock is irrelevant at this point.
226		 */
227		drive->disk_ops->set_doorlock(drive, disk, 1);
228		drive->dev_flags |= IDE_DFLAG_MEDIA_CHANGED;
229		check_disk_change(bdev);
230	} else if (drive->dev_flags & IDE_DFLAG_FORMAT_IN_PROGRESS) {
231		ret = -EBUSY;
232		goto out_put_idkp;
233	}
234	return 0;
235
236out_put_idkp:
237	idkp->openers--;
238	ide_disk_put(idkp);
239	return ret;
240}
241
242static int ide_gd_unlocked_open(struct block_device *bdev, fmode_t mode)
243{
244	int ret;
245
246	mutex_lock(&ide_gd_mutex);
247	ret = ide_gd_open(bdev, mode);
248	mutex_unlock(&ide_gd_mutex);
249
250	return ret;
251}
252
253
254static void ide_gd_release(struct gendisk *disk, fmode_t mode)
255{
256	struct ide_disk_obj *idkp = ide_drv_g(disk, ide_disk_obj);
257	ide_drive_t *drive = idkp->drive;
258
259	ide_debug_log(IDE_DBG_FUNC, "enter");
260
261	mutex_lock(&ide_gd_mutex);
262	if (idkp->openers == 1)
263		drive->disk_ops->flush(drive);
264
265	if ((drive->dev_flags & IDE_DFLAG_REMOVABLE) && idkp->openers == 1) {
266		drive->disk_ops->set_doorlock(drive, disk, 0);
267		drive->dev_flags &= ~IDE_DFLAG_FORMAT_IN_PROGRESS;
268	}
269
270	idkp->openers--;
271
272	ide_disk_put(idkp);
273	mutex_unlock(&ide_gd_mutex);
274}
275
276static int ide_gd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
277{
278	struct ide_disk_obj *idkp = ide_drv_g(bdev->bd_disk, ide_disk_obj);
279	ide_drive_t *drive = idkp->drive;
280
281	geo->heads = drive->bios_head;
282	geo->sectors = drive->bios_sect;
283	geo->cylinders = (u16)drive->bios_cyl; /* truncate */
284	return 0;
285}
286
287static unsigned int ide_gd_check_events(struct gendisk *disk,
288					unsigned int clearing)
289{
290	struct ide_disk_obj *idkp = ide_drv_g(disk, ide_disk_obj);
291	ide_drive_t *drive = idkp->drive;
292	bool ret;
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
300	/*
301	 * The following is used to force revalidation on the first open on
302	 * removeable devices, and never gets reported to userland as
303	 * DISK_EVENT_FLAG_UEVENT isn't set in genhd->event_flags.
304	 * This is intended as removable ide disk can't really detect
305	 * MEDIA_CHANGE events.
306	 */
307	ret = drive->dev_flags & IDE_DFLAG_MEDIA_CHANGED;
308	drive->dev_flags &= ~IDE_DFLAG_MEDIA_CHANGED;
309
310	return ret ? DISK_EVENT_MEDIA_CHANGE : 0;
311}
312
313static void ide_gd_unlock_native_capacity(struct gendisk *disk)
314{
315	struct ide_disk_obj *idkp = ide_drv_g(disk, ide_disk_obj);
316	ide_drive_t *drive = idkp->drive;
317	const struct ide_disk_ops *disk_ops = drive->disk_ops;
318
319	if (disk_ops->unlock_native_capacity)
320		disk_ops->unlock_native_capacity(drive);
321}
322
323static int ide_gd_revalidate_disk(struct gendisk *disk)
324{
325	struct ide_disk_obj *idkp = ide_drv_g(disk, ide_disk_obj);
326	ide_drive_t *drive = idkp->drive;
327
328	if (ide_gd_check_events(disk, 0))
329		drive->disk_ops->get_capacity(drive);
330
331	set_capacity(disk, ide_gd_capacity(drive));
332	return 0;
333}
334
335static int ide_gd_ioctl(struct block_device *bdev, fmode_t mode,
336			     unsigned int cmd, unsigned long arg)
337{
338	struct ide_disk_obj *idkp = ide_drv_g(bdev->bd_disk, ide_disk_obj);
339	ide_drive_t *drive = idkp->drive;
340
341	return drive->disk_ops->ioctl(drive, bdev, mode, cmd, arg);
342}
343
344#ifdef CONFIG_COMPAT
345static int ide_gd_compat_ioctl(struct block_device *bdev, fmode_t mode,
346			       unsigned int cmd, unsigned long arg)
347{
348	struct ide_disk_obj *idkp = ide_drv_g(bdev->bd_disk, ide_disk_obj);
349	ide_drive_t *drive = idkp->drive;
350
351	if (!drive->disk_ops->compat_ioctl)
352		return -ENOIOCTLCMD;
353
354	return drive->disk_ops->compat_ioctl(drive, bdev, mode, cmd, arg);
355}
356#endif
357
358static const struct block_device_operations ide_gd_ops = {
359	.owner			= THIS_MODULE,
360	.open			= ide_gd_unlocked_open,
361	.release		= ide_gd_release,
362	.ioctl			= ide_gd_ioctl,
363#ifdef CONFIG_COMPAT
364	.compat_ioctl		= ide_gd_compat_ioctl,
365#endif
366	.getgeo			= ide_gd_getgeo,
367	.check_events		= ide_gd_check_events,
368	.unlock_native_capacity	= ide_gd_unlock_native_capacity,
369	.revalidate_disk	= ide_gd_revalidate_disk
370};
371
372static int ide_gd_probe(ide_drive_t *drive)
373{
374	const struct ide_disk_ops *disk_ops = NULL;
375	struct ide_disk_obj *idkp;
376	struct gendisk *g;
377
378	/* strstr("foo", "") is non-NULL */
379	if (!strstr("ide-gd", drive->driver_req))
380		goto failed;
381
382#ifdef CONFIG_IDE_GD_ATA
383	if (drive->media == ide_disk)
384		disk_ops = &ide_ata_disk_ops;
385#endif
386#ifdef CONFIG_IDE_GD_ATAPI
387	if (drive->media == ide_floppy)
388		disk_ops = &ide_atapi_disk_ops;
389#endif
390	if (disk_ops == NULL)
391		goto failed;
392
393	if (disk_ops->check(drive, DRV_NAME) == 0) {
394		printk(KERN_ERR PFX "%s: not supported by this driver\n",
395			drive->name);
396		goto failed;
397	}
398
399	idkp = kzalloc(sizeof(*idkp), GFP_KERNEL);
400	if (!idkp) {
401		printk(KERN_ERR PFX "%s: can't allocate a disk structure\n",
402			drive->name);
403		goto failed;
404	}
405
406	g = alloc_disk_node(IDE_DISK_MINORS, hwif_to_node(drive->hwif));
407	if (!g)
408		goto out_free_idkp;
409
410	ide_init_disk(g, drive);
411
412	idkp->dev.parent = &drive->gendev;
413	idkp->dev.release = ide_disk_release;
414	dev_set_name(&idkp->dev, "%s", dev_name(&drive->gendev));
415
416	if (device_register(&idkp->dev))
417		goto out_free_disk;
418
419	idkp->drive = drive;
420	idkp->driver = &ide_gd_driver;
421	idkp->disk = g;
422
423	g->private_data = &idkp->driver;
424
425	drive->driver_data = idkp;
426	drive->debug_mask = debug_mask;
427	drive->disk_ops = disk_ops;
428
429	disk_ops->setup(drive);
430
431	set_capacity(g, ide_gd_capacity(drive));
432
433	g->minors = IDE_DISK_MINORS;
434	g->flags |= GENHD_FL_EXT_DEVT;
435	if (drive->dev_flags & IDE_DFLAG_REMOVABLE)
436		g->flags = GENHD_FL_REMOVABLE;
437	g->fops = &ide_gd_ops;
438	g->events = DISK_EVENT_MEDIA_CHANGE;
439	device_add_disk(&drive->gendev, g, NULL);
440	return 0;
441
442out_free_disk:
443	put_disk(g);
444out_free_idkp:
445	kfree(idkp);
446failed:
447	return -ENODEV;
448}
449
450static int __init ide_gd_init(void)
451{
452	printk(KERN_INFO DRV_NAME " driver " IDE_GD_VERSION "\n");
453	return driver_register(&ide_gd_driver.gen_driver);
454}
455
456static void __exit ide_gd_exit(void)
457{
458	driver_unregister(&ide_gd_driver.gen_driver);
459}
460
461MODULE_ALIAS("ide:*m-disk*");
462MODULE_ALIAS("ide-disk");
463MODULE_ALIAS("ide:*m-floppy*");
464MODULE_ALIAS("ide-floppy");
465module_init(ide_gd_init);
466module_exit(ide_gd_exit);
467MODULE_LICENSE("GPL");
468MODULE_DESCRIPTION("generic ATA/ATAPI disk driver");
v4.10.11
 
  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>
 11#include <linux/slab.h>
 12
 13#if !defined(CONFIG_DEBUG_BLOCK_EXT_DEVT)
 14#define IDE_DISK_MINORS		(1 << PARTN_BITS)
 15#else
 16#define IDE_DISK_MINORS		0
 17#endif
 18
 19#include "ide-disk.h"
 20#include "ide-floppy.h"
 21
 22#define IDE_GD_VERSION	"1.18"
 23
 24/* module parameters */
 25static DEFINE_MUTEX(ide_gd_mutex);
 26static unsigned long debug_mask;
 27module_param(debug_mask, ulong, 0644);
 28
 29static DEFINE_MUTEX(ide_disk_ref_mutex);
 30
 31static void ide_disk_release(struct device *);
 32
 33static 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
 43			get_device(&idkp->dev);
 44	}
 45	mutex_unlock(&ide_disk_ref_mutex);
 46	return idkp;
 47}
 48
 49static 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);
 54	put_device(&idkp->dev);
 55	ide_device_put(drive);
 56	mutex_unlock(&ide_disk_ref_mutex);
 57}
 58
 59sector_t ide_gd_capacity(ide_drive_t *drive)
 60{
 61	return drive->capacity64;
 62}
 63
 64static int ide_gd_probe(ide_drive_t *);
 65
 66static 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);
 72	device_del(&idkp->dev);
 73	del_gendisk(g);
 74	drive->disk_ops->flush(drive);
 75
 76	mutex_lock(&ide_disk_ref_mutex);
 77	put_device(&idkp->dev);
 78	mutex_unlock(&ide_disk_ref_mutex);
 79}
 80
 81static void ide_disk_release(struct device *dev)
 82{
 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;
 86
 87	drive->disk_ops = NULL;
 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
 96 * reinitialized on resume otherwise the disk
 97 * can not be used and a hard reset is required
 98 */
 99static void ide_gd_resume(ide_drive_t *drive)
100{
101	if (ata_id_hpa_enabled(drive->id))
102		(void)drive->disk_ops->get_capacity(drive);
103}
104
105static 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
118static 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
134	if (system_state == SYSTEM_RESTART &&
135		!dmi_check_system(ide_coldreboot_table)) {
136#endif
137		drive->disk_ops->flush(drive);
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
146#ifdef CONFIG_IDE_PROC_FS
147static ide_proc_entry_t *ide_disk_proc_entries(ide_drive_t *drive)
148{
149	return (drive->media == ide_disk) ? ide_disk_proc : ide_floppy_proc;
150}
151
152static const struct ide_proc_devset *ide_disk_proc_devsets(ide_drive_t *drive)
153{
154	return (drive->media == ide_disk) ? ide_disk_settings
155					  : ide_floppy_settings;
156}
157#endif
158
159static 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
165static struct ide_driver ide_gd_driver = {
166	.gen_driver = {
167		.owner		= THIS_MODULE,
168		.name		= "ide-gd",
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,
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,
180#endif
181};
182
183static int ide_gd_open(struct block_device *bdev, fmode_t mode)
184{
185	struct gendisk *disk = bdev->bd_disk;
186	struct ide_disk_obj *idkp;
187	ide_drive_t *drive;
188	int ret = 0;
189
190	idkp = ide_disk_get(disk);
191	if (idkp == NULL)
192		return -ENXIO;
193
194	drive = idkp->drive;
195
196	ide_debug_log(IDE_DBG_FUNC, "enter");
197
198	idkp->openers++;
199
200	if ((drive->dev_flags & IDE_DFLAG_REMOVABLE) && idkp->openers == 1) {
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		 */
211		if (ret && (mode & FMODE_NDELAY) == 0) {
212			ret = -EIO;
213			goto out_put_idkp;
214		}
215
216		if ((drive->dev_flags & IDE_DFLAG_WP) && (mode & FMODE_WRITE)) {
217			ret = -EROFS;
218			goto out_put_idkp;
219		}
220
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		 */
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) {
230		ret = -EBUSY;
231		goto out_put_idkp;
232	}
233	return 0;
234
235out_put_idkp:
236	idkp->openers--;
237	ide_disk_put(idkp);
238	return ret;
239}
240
241static int ide_gd_unlocked_open(struct block_device *bdev, fmode_t mode)
242{
243	int ret;
244
245	mutex_lock(&ide_gd_mutex);
246	ret = ide_gd_open(bdev, mode);
247	mutex_unlock(&ide_gd_mutex);
248
249	return ret;
250}
251
252
253static void ide_gd_release(struct gendisk *disk, fmode_t mode)
254{
255	struct ide_disk_obj *idkp = ide_drv_g(disk, ide_disk_obj);
256	ide_drive_t *drive = idkp->drive;
257
258	ide_debug_log(IDE_DBG_FUNC, "enter");
259
260	mutex_lock(&ide_gd_mutex);
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	}
268
269	idkp->openers--;
270
271	ide_disk_put(idkp);
272	mutex_unlock(&ide_gd_mutex);
273}
274
275static int ide_gd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
276{
277	struct ide_disk_obj *idkp = ide_drv_g(bdev->bd_disk, ide_disk_obj);
278	ide_drive_t *drive = idkp->drive;
279
280	geo->heads = drive->bios_head;
281	geo->sectors = drive->bios_sect;
282	geo->cylinders = (u16)drive->bios_cyl; /* truncate */
283	return 0;
284}
285
286static unsigned int ide_gd_check_events(struct gendisk *disk,
287					unsigned int clearing)
288{
289	struct ide_disk_obj *idkp = ide_drv_g(disk, ide_disk_obj);
290	ide_drive_t *drive = idkp->drive;
291	bool ret;
292
293	/* do not scan partitions twice if this is a removable device */
294	if (drive->dev_flags & IDE_DFLAG_ATTACH) {
295		drive->dev_flags &= ~IDE_DFLAG_ATTACH;
296		return 0;
297	}
298
299	/*
300	 * The following is used to force revalidation on the first open on
301	 * removeable devices, and never gets reported to userland as
302	 * genhd->events is 0.  This is intended as removeable ide disk
303	 * can't really detect MEDIA_CHANGE events.
 
304	 */
305	ret = drive->dev_flags & IDE_DFLAG_MEDIA_CHANGED;
306	drive->dev_flags &= ~IDE_DFLAG_MEDIA_CHANGED;
307
308	return ret ? DISK_EVENT_MEDIA_CHANGE : 0;
309}
310
311static void ide_gd_unlock_native_capacity(struct gendisk *disk)
312{
313	struct ide_disk_obj *idkp = ide_drv_g(disk, ide_disk_obj);
314	ide_drive_t *drive = idkp->drive;
315	const struct ide_disk_ops *disk_ops = drive->disk_ops;
316
317	if (disk_ops->unlock_native_capacity)
318		disk_ops->unlock_native_capacity(drive);
319}
320
321static int ide_gd_revalidate_disk(struct gendisk *disk)
322{
323	struct ide_disk_obj *idkp = ide_drv_g(disk, ide_disk_obj);
324	ide_drive_t *drive = idkp->drive;
325
326	if (ide_gd_check_events(disk, 0))
327		drive->disk_ops->get_capacity(drive);
328
329	set_capacity(disk, ide_gd_capacity(drive));
330	return 0;
331}
332
333static int ide_gd_ioctl(struct block_device *bdev, fmode_t mode,
334			     unsigned int cmd, unsigned long arg)
335{
336	struct ide_disk_obj *idkp = ide_drv_g(bdev->bd_disk, ide_disk_obj);
337	ide_drive_t *drive = idkp->drive;
338
339	return drive->disk_ops->ioctl(drive, bdev, mode, cmd, arg);
340}
341
 
 
 
 
 
 
 
 
 
 
 
 
 
 
342static const struct block_device_operations ide_gd_ops = {
343	.owner			= THIS_MODULE,
344	.open			= ide_gd_unlocked_open,
345	.release		= ide_gd_release,
346	.ioctl			= ide_gd_ioctl,
 
 
 
347	.getgeo			= ide_gd_getgeo,
348	.check_events		= ide_gd_check_events,
349	.unlock_native_capacity	= ide_gd_unlock_native_capacity,
350	.revalidate_disk	= ide_gd_revalidate_disk
351};
352
353static int ide_gd_probe(ide_drive_t *drive)
354{
355	const struct ide_disk_ops *disk_ops = NULL;
356	struct ide_disk_obj *idkp;
357	struct gendisk *g;
358
359	/* strstr("foo", "") is non-NULL */
360	if (!strstr("ide-gd", drive->driver_req))
361		goto failed;
362
363#ifdef CONFIG_IDE_GD_ATA
364	if (drive->media == ide_disk)
365		disk_ops = &ide_ata_disk_ops;
366#endif
367#ifdef CONFIG_IDE_GD_ATAPI
368	if (drive->media == ide_floppy)
369		disk_ops = &ide_atapi_disk_ops;
370#endif
371	if (disk_ops == NULL)
372		goto failed;
373
374	if (disk_ops->check(drive, DRV_NAME) == 0) {
375		printk(KERN_ERR PFX "%s: not supported by this driver\n",
376			drive->name);
377		goto failed;
378	}
379
380	idkp = kzalloc(sizeof(*idkp), GFP_KERNEL);
381	if (!idkp) {
382		printk(KERN_ERR PFX "%s: can't allocate a disk structure\n",
383			drive->name);
384		goto failed;
385	}
386
387	g = alloc_disk_node(IDE_DISK_MINORS, hwif_to_node(drive->hwif));
388	if (!g)
389		goto out_free_idkp;
390
391	ide_init_disk(g, drive);
392
393	idkp->dev.parent = &drive->gendev;
394	idkp->dev.release = ide_disk_release;
395	dev_set_name(&idkp->dev, "%s", dev_name(&drive->gendev));
396
397	if (device_register(&idkp->dev))
398		goto out_free_disk;
399
400	idkp->drive = drive;
401	idkp->driver = &ide_gd_driver;
402	idkp->disk = g;
403
404	g->private_data = &idkp->driver;
405
406	drive->driver_data = idkp;
407	drive->debug_mask = debug_mask;
408	drive->disk_ops = disk_ops;
409
410	disk_ops->setup(drive);
411
412	set_capacity(g, ide_gd_capacity(drive));
413
414	g->minors = IDE_DISK_MINORS;
415	g->flags |= GENHD_FL_EXT_DEVT;
416	if (drive->dev_flags & IDE_DFLAG_REMOVABLE)
417		g->flags = GENHD_FL_REMOVABLE;
418	g->fops = &ide_gd_ops;
419	device_add_disk(&drive->gendev, g);
 
420	return 0;
421
422out_free_disk:
423	put_disk(g);
424out_free_idkp:
425	kfree(idkp);
426failed:
427	return -ENODEV;
428}
429
430static int __init ide_gd_init(void)
431{
432	printk(KERN_INFO DRV_NAME " driver " IDE_GD_VERSION "\n");
433	return driver_register(&ide_gd_driver.gen_driver);
434}
435
436static void __exit ide_gd_exit(void)
437{
438	driver_unregister(&ide_gd_driver.gen_driver);
439}
440
441MODULE_ALIAS("ide:*m-disk*");
442MODULE_ALIAS("ide-disk");
443MODULE_ALIAS("ide:*m-floppy*");
444MODULE_ALIAS("ide-floppy");
445module_init(ide_gd_init);
446module_exit(ide_gd_exit);
447MODULE_LICENSE("GPL");
448MODULE_DESCRIPTION("generic ATA/ATAPI disk driver");