File: ide.c

package info (click to toggle)
oskit 0.97.20000202-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 58,008 kB
  • ctags: 172,612
  • sloc: ansic: 832,827; asm: 7,640; sh: 3,920; yacc: 3,664; perl: 1,457; lex: 427; makefile: 337; csh: 141; awk: 78
file content (340 lines) | stat: -rw-r--r-- 8,693 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*
 * Copyright (c) 1997, 1998, 1999 The University of Utah and the Flux Group.
 * 
 * This file is part of the OSKit Linux Glue Libraries, which are free
 * software, also known as "open source;" you can redistribute them and/or
 * modify them under the terms of the GNU General Public License (GPL),
 * version 2, as published by the Free Software Foundation (FSF).
 * 
 * The OSKit is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GPL for more details.  You should have
 * received a copy of the GPL along with the OSKit; see the file COPYING.  If
 * not, write to the FSF, 59 Temple Place #330, Boston, MA 02111-1307, USA.
 */
/*
 * This source file overrides the Linux block/ide.c,
 * but #include's the original ide.c as the first thing -
 * in other words, we basically extend Linux's ide.c at the bottom.
 */
#include "../drivers/block/ide.c"

#include <linux/version.h>

#include <oskit/dev/blk.h>
#include <oskit/dev/linux.h>
#include <oskit/dev/bus.h>
#include <oskit/dev/isa.h>
#include <oskit/dev/ide.h>

#include "glue.h"
#include "block.h"
#include "osenv.h"

static oskit_error_t probe(struct driver_struct *ds);

/* Device driver structure representing the Linux IDE device driver */
static struct driver_struct drv = {
	{ &oskit_linux_driver_ops },
	probe,
	{ "ide", "Linux IDE device driver", NULL,
	  "Linus Torvalds & others", "Linux "UTS_RELEASE }
};

/* Descriptive information on an IDE hardware interface */
static oskit_devinfo_t bus_info = {
	"ide", "IDE bus interface", NULL,
	"Linus Torvalds & others", "Linux "UTS_RELEASE
};

/* Descriptive information for specific IDE devices */
static oskit_devinfo_t disk_info = {
	"disk", "IDE hard disk drive", NULL,
	"Linus Torvalds & others", "Linux "UTS_RELEASE
};
#if 0 /* XXX check the author names before using */
static oskit_devinfo_t tape_info = {
	"tape", "IDE tape drive", NULL,
	"Linus Torvalds & others", "Linux "UTS_RELEASE
};
static oskit_devinfo_t cdrom_info = {
	"cdrom", "IDE CD-ROM drive", NULL,
	"Linus Torvalds & others", "Linux "UTS_RELEASE
};
#endif

static oskit_blkdev_t devi[MAX_HWIFS][MAX_DRIVES];
static oskit_idebus_t busi[MAX_HWIFS];


/* Find the Linux ide_drive structure for a given device node */
static inline ide_drive_t *
devnode_to_drive(oskit_blkdev_t *dev)
{
	int idx;

	/* Find the index number in the devi array */
	idx = dev - &devi[0][0];
	if (idx < 0 || idx >= MAX_HWIFS*MAX_DRIVES)
		panic("%s:%d: bad blkdev pointer", __FILE__, __LINE__);

	return &ide_hwifs[idx / MAX_DRIVES].drives[idx % MAX_DRIVES];
}

/* Find the Linux device number for a given oskit_blkdev_t device node pointer */
static inline kdev_t
devnode_to_devnum(oskit_blkdev_t *dev)
{
	int idx, major, minor;

	/* Find the index number in the devi array */
	idx = dev - &devi[0][0];
	if (idx < 0 || idx >= MAX_HWIFS*MAX_DRIVES)
		panic("%s:%d: bad blkdev pointer", __FILE__, __LINE__);

	/* Derive the Linux major and minor numbers */
	major = ide_hwif_to_major[idx / MAX_DRIVES];
	minor = (idx % MAX_DRIVES) << PARTN_BITS;

	return MKDEV(major, minor);
}


/*** IDE disk device node interface ***/

static OSKIT_COMDECL
disk_query(oskit_blkdev_t *dev, const struct oskit_guid *iid, void **out_ihandle)
{
	if (memcmp(iid, &oskit_iunknown_iid, sizeof(*iid)) == 0 ||
	    memcmp(iid, &oskit_device_iid, sizeof(*iid)) == 0 ||
	    memcmp(iid, &oskit_blkdev_iid, sizeof(*iid)) == 0) {
		*out_ihandle = dev;
		return 0;
	}

	*out_ihandle = NULL;
	return OSKIT_E_NOINTERFACE;
}

static OSKIT_COMDECL_U disk_addref(oskit_blkdev_t *dev)
{
	/* No reference counting for block device nodes */
	return 1;
}

static OSKIT_COMDECL_U disk_release(oskit_blkdev_t *dev)
{
	/* No reference counting for block device nodes */
	return 1;
}

static OSKIT_COMDECL disk_getinfo(oskit_blkdev_t *dev, oskit_devinfo_t *out_info)
{
	ide_drive_t *drive = devnode_to_drive(dev);

	*out_info = disk_info;

	/* If more detailed information is available, use it */
	if (drive->id && drive->id->model[0])
		out_info->description = drive->id->model;

	return 0;
}

static OSKIT_COMDECL disk_getdriver(oskit_blkdev_t *fdev,
				    oskit_driver_t **out_driver)
{
	*out_driver = (oskit_driver_t*)&drv.drvi;
	drv.drvi.ops->addref(&drv.drvi);
	return 0;
}

static OSKIT_COMDECL disk_open(oskit_blkdev_t *dev, unsigned mode,
				oskit_blkio_t **out_blkio)
{
	kdev_t kdev = devnode_to_devnum(dev);

	return oskit_linux_block_open_kdev(kdev, NULL, mode, out_blkio);
}


static struct oskit_blkdev_ops disk_ops = {
	disk_query, disk_addref, disk_release,
	disk_getinfo, disk_getdriver,
	disk_open
};


/*** IDE hardware interface device node ("IDE bus") interface ***/

static OSKIT_COMDECL
bus_query(oskit_idebus_t *bus, const struct oskit_guid *iid, void **out_ihandle)
{
	if (memcmp(iid, &oskit_iunknown_iid, sizeof(*iid)) == 0 ||
	    memcmp(iid, &oskit_device_iid, sizeof(*iid)) == 0 ||
	    memcmp(iid, &oskit_bus_iid, sizeof(*iid)) == 0 ||
	    memcmp(iid, &oskit_idebus_iid, sizeof(*iid)) == 0) {
		*out_ihandle = bus;
		return 0;
	}

	*out_ihandle = NULL;
	return OSKIT_E_NOINTERFACE;
}

static OSKIT_COMDECL_U
bus_addref(oskit_idebus_t *bus)
{
	/* No reference counting for block device nodes */
	return 1;
}

static OSKIT_COMDECL_U
bus_release(oskit_idebus_t *bus)
{
	/* No reference counting for block device nodes */
	return 1;
}

static OSKIT_COMDECL
bus_getinfo(oskit_idebus_t *bus, oskit_devinfo_t *out_info)
{
	*out_info = bus_info;
	return 0;
}

static OSKIT_COMDECL
bus_getdriver(oskit_idebus_t *bus, oskit_driver_t **out_driver)
{
	*out_driver = (oskit_driver_t*)&drv.drvi;
	drv.drvi.ops->addref(&drv.drvi);
	return 0;
}

static OSKIT_COMDECL
bus_getchild(oskit_idebus_t *bus, oskit_u32_t idx,
	      struct oskit_device **out_fdev, char *out_pos)
{
	int hwif = bus - busi;
	int drive;

	if (hwif < 0 || hwif >= MAX_HWIFS)
		panic("%s:%d: bad bus pointer", __FILE__, __LINE__);

	/*
	 * Find the IDE device corresponding to this index.
	 * It may be somewhat unnecessary to go to this trouble
	 * since IDE may never support more than two drives per hwif
	 * and we're unlikely to find a slave without a master;
	 * but just for good practice...
	 */
	for (drive = 0; ; drive++) {
		if (drive >= MAX_DRIVES)
			return OSKIT_E_DEV_NOMORE_CHILDREN;
		if (!ide_hwifs[hwif].drives[drive].present)
			continue;
		if (idx-- == 0)
			break;
	}

	/* Return the device node and position string for this drive */
	*out_fdev = (oskit_device_t*)&devi[hwif][drive];
	strcpy(out_pos, drive ? "slave" : "master");
	return 0;
}

static OSKIT_COMDECL
bus_probe(oskit_idebus_t *bus)
{
	/*
	 * The IDE bus is probed when the IDE bus itself is found,
	 * and dynamic insertion/removal isn't supported,
	 * so we don't have to do anything here.
	 */
	return 0;
}

static struct oskit_idebus_ops bus_ops = {
	bus_query, 
	bus_addref, 
	bus_release,
	bus_getinfo, 
	bus_getdriver,
	bus_getchild, 
	bus_probe
};


/*** Driver initialization/probe code ***/

static oskit_error_t probe(struct driver_struct *ds)
{
	static int initialized = 0;
	oskit_error_t rc;
	int i, j, found;

	if (initialized)
		return 0;
	initialized = 1;

	/* Call the Linux IDE probe code */
	ide_init();

	/*
	 * Register each IDE device found in the hardware tree.
	 * Each hardware interface becomes an IDE bus,
	 * and each device on an interface becomes a child of that bus.
	 */
	found = 0;
	for (i = 0; i < MAX_HWIFS; i++) {

		/* Only interested in detected hardware interfaces */
		if (blkdevs[ide_hwif_to_major[i]].fops == NULL)
			continue;
		found++;

		/* Initialize the device interfaces */
		for (j = 0; j < MAX_DRIVES; j++) {

			/* Only interested in detected devices */
			if (!ide_hwifs[i].drives[j].present)
				continue;

			if (ide_hwifs[i].drives[j].media == ide_tape) {
				osenv_log(OSENV_LOG_WARNING, 
					 "%s: IDE tape device %d not supported\n",
					 j, __FUNCTION__);

				ide_hwifs[i].drives[j].present = 0;
				continue;
			}

			found++;

			/* Use the appropriate operations vector table */
			switch (ide_hwifs[i].drives[j].media) {
			case ide_disk: devi[i][j].ops = &disk_ops; break;
			case ide_floppy: devi[i][j].ops = &disk_ops; break;
			case ide_cdrom: devi[i][j].ops = &disk_ops; break;
			}

			/* Perform linux per-drive boot time init */
			ide_revalidate_disk(MKDEV(ide_hwifs[i].major,
						  j<<PARTN_BITS));
		}

		/* Initialize and register the IDE bus */
		busi[i].ops = &bus_ops;
		rc = osenv_isabus_addchild(
			ide_hwifs[i].io_ports[IDE_DATA_OFFSET],
			(oskit_device_t*)&busi[i]);
	}

	return found;
}

oskit_error_t oskit_linux_init_ide(void)
{
	return oskit_linux_driver_register(&drv);
}