1/*
2 * Copyright 2002/03, Thomas Kurschel. All rights reserved.
3 * Distributed under the terms of the MIT License.
4 */
5
6/*
7	Part of Open IDE bus manager
8
9	Manager of IDE controllers
10
11	Whenever a new IDE channel is reported, a new SIM is
12	registered at the SCSI bus manager.
13*/
14
15#include "ide_internal.h"
16#include "ide_sim.h"
17
18#include <string.h>
19#include <malloc.h>
20
21
22/** called when an IDE channel was registered by a controller driver */
23
24static status_t
25ide_channel_added(device_node *parent)
26{
27	const char *controller_name = NULL;
28	uint32 channel_id;
29
30	SHOW_FLOW0(2, "");
31
32	if (pnp->get_attr_string(parent, IDE_CONTROLLER_CONTROLLER_NAME_ITEM,
33			&controller_name, true) != B_OK) {
34		dprintf("ide: ignored controller - controller name missing\n");
35		goto err;
36	}
37
38	channel_id = pnp->create_id(IDE_CHANNEL_ID_GENERATOR);
39
40	if (channel_id < 0) {
41		SHOW_ERROR(0, "Cannot register IDE controller %s - out of IDs", controller_name);
42		goto err;
43	}
44
45	{
46		device_attr attrs[] =
47		{
48			{ B_DEVICE_FIXED_CHILD, B_STRING_TYPE, { string: SCSI_FOR_SIM_MODULE_NAME }},
49
50			{ SCSI_DESCRIPTION_CONTROLLER_NAME, B_STRING_TYPE,
51				{ string: controller_name }},
52			// maximum number of blocks per transmission:
53			// - ATAPI uses packets, i.e. normal SCSI limits apply
54			//   but I'm not sure about controller restrictions
55			// - ATA allows up to 256 blocks
56			// - some broken disk's firmware (read: IBM DTTA drives)
57			//   don't like 256 blocks in command queuing mode
58			// -> use 255 blocks as a least common nominator
59			//    (this is still 127.5K for HDs and 510K for CDs,
60			//     which should be sufficient)
61			// Note: to fix specific drive bugs, use ide_sim_get_restrictions()
62			// in ide_sim.c!
63			{ B_DMA_MAX_TRANSFER_BLOCKS, B_UINT32_TYPE, { ui32: 255 }},
64			{ IDE_CHANNEL_ID_ITEM, B_UINT32_TYPE, { ui32: channel_id }},
65//			{ PNP_MANAGER_ID_GENERATOR, B_STRING_TYPE, { string: IDE_CHANNEL_ID_GENERATOR }},
66//			{ PNP_MANAGER_AUTO_ID, B_UINT32_TYPE, { ui32: channel_id }},
67
68			{ NULL }
69		};
70
71		return pnp->register_node(parent, IDE_SIM_MODULE_NAME, attrs, NULL,
72			NULL);
73	}
74
75err:
76	return B_NO_MEMORY;
77}
78
79
80static status_t
81std_ops(int32 op, ...)
82{
83	switch (op) {
84		case B_MODULE_INIT:
85		case B_MODULE_UNINIT:
86			return B_OK;
87
88		default:
89			return B_ERROR;
90	}
91}
92
93
94ide_for_controller_interface ide_for_controller_module = {
95	{
96		{
97			IDE_FOR_CONTROLLER_MODULE_NAME,
98			0,
99			&std_ops
100		},
101
102		NULL,	// supported devices
103		ide_channel_added,
104		NULL,
105		NULL,
106		NULL
107	},
108
109	ide_irq_handler
110};
111