summaryrefslogtreecommitdiffstats
path: root/cfe/cfe/main/cfe_attach.c
blob: 1235d6eff95baa54db28e579005880beb28c7fe0 (plain)
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
/*  *********************************************************************
    *  Broadcom Common Firmware Environment (CFE)
    *  
    *  Device Attach routines			File: cfe_attach.c
    *  
    *  This module manages the list of device drivers.  When a driver
    *  is probed, it can call cfe_attach to create actual device
    *  instances.  The routines in this module manage the
    *  device list and the assignment of device names.
    *  
    *  Author:  Mitch Lichtenberg (mpl@broadcom.com)
    *  
    *********************************************************************  
    *
    *  Copyright 2000,2001,2002,2003
    *  Broadcom Corporation. All rights reserved.
    *  
    *  This software is furnished under license and may be used and 
    *  copied only in accordance with the following terms and 
    *  conditions.  Subject to these conditions, you may download, 
    *  copy, install, use, modify and distribute modified or unmodified 
    *  copies of this software in source and/or binary form.  No title 
    *  or ownership is transferred hereby.
    *  
    *  1) Any source code used, modified or distributed must reproduce 
    *     and retain this copyright notice and list of conditions 
    *     as they appear in the source file.
    *  
    *  2) No right is granted to use any trade name, trademark, or 
    *     logo of Broadcom Corporation.  The "Broadcom Corporation" 
    *     name may not be used to endorse or promote products derived 
    *     from this software without the prior written permission of 
    *     Broadcom Corporation.
    *  
    *  3) THIS SOFTWARE IS PROVIDED "AS-IS" AND ANY EXPRESS OR
    *     IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, ANY IMPLIED
    *     WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
    *     PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT 
    *     SHALL BROADCOM BE LIABLE FOR ANY DAMAGES WHATSOEVER, AND IN 
    *     PARTICULAR, BROADCOM SHALL NOT BE LIABLE FOR DIRECT, INDIRECT,
    *     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
    *     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
    *     GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
    *     BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 
    *     OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 
    *     TORT (INCLUDING NEGLIGENCE OR OTHERWISE), EVEN IF ADVISED OF 
    *     THE POSSIBILITY OF SUCH DAMAGE.
    ********************************************************************* */


#include "lib_types.h"
#include "lib_malloc.h"
#include "lib_queue.h"
#include "lib_printf.h"
#include "lib_string.h"
#include "cfe_iocb.h"
#include "cfe_device.h"

/*  *********************************************************************
    *  Constants
    ********************************************************************* */

#define CFE_MAX_DEVINST 64		/* max # of instances of devices */

/*  *********************************************************************
    *  Globals
    ********************************************************************* */

/*
 * Our device list.
 */

queue_t cfe_devices = {&cfe_devices, &cfe_devices};

/*  *********************************************************************
    *  cfe_finddev(name)
    *  
    *  Locate a device in the device list by its name and return
    *  a pointer to the device structure.
    *  
    *  Input parameters: 
    *  	   name - name of device, e.g., "uart0"
    *  	   
    *  Return value:
    *  	   cfe_device_t pointer or NULL
    ********************************************************************* */

cfe_device_t *cfe_finddev(char *name)
{
    queue_t *qb;
    cfe_device_t *dev;

    for (qb = cfe_devices.q_next; qb != &cfe_devices; qb = qb->q_next) {
	dev = (cfe_device_t *) qb;
	if (strcmp(dev->dev_fullname,name) == 0) {
	    return dev;
	    }
	}

    return NULL;
}


/*  *********************************************************************
    *  cfe_device_reset()
    *  
    *  Call all the "reset" methods in the devices on the device 
    *  list.  Note that the methods get called even when the
    *  devices are closed!
    *  
    *  Input parameters: 
    *  	   nothing
    *  	   
    *  Return value:
    *  	   nothing
    ********************************************************************* */

void cfe_device_reset(void)
{
    queue_t *qb;
    cfe_device_t *dev;

    for (qb = cfe_devices.q_next; qb != &cfe_devices; qb = qb->q_next) {
	dev = (cfe_device_t *) qb;
	if (dev->dev_dispatch->dev_reset) {
	    (*(dev->dev_dispatch->dev_reset))(dev->dev_softc);
	    }
	}
}

/*  *********************************************************************
    *  cfe_attach_idx(drv,idx,softc,bootinfo,description)
    *  
    *  Add a device to the device list at a specific index.  This
    *  is mainly used for devices like SCSI disks or CD-ROMs so
    *  we can use an index that matches the target ID or LUN.
    *  
    *  Input parameters: 
    *  	   drv - driver structure (from the device driver module)
    *  	   idx - requested index (e.g., uartn where 'n' is the idx)
    *  	   softc - Unique information maintained for this device
    *  	   bootinfo - suffix for long form of the device name.  For
    *  	              example, scsi0.3.1  might mean SCSI controller
    *  	              0, device ID 3, LUN 1.  The bootinfo would be
    *  	              "3.1"
    *  	   description - something nice to say for the devices command
    *  	   
    *  Return value:
    *  	   0 if device has already been added at this index
    *     <0 for other problems
    *  	   1 if we were successful.
    ********************************************************************* */

int cfe_attach_idx(cfe_driver_t *drv,int idx,void *softc,
		   char *bootinfo,char *description)
{
    char name[64];
    cfe_device_t *dev;

    xsprintf(name,"%s%d",drv->drv_bootname,idx);

    if (bootinfo) {
	strcat(name,".");
	strcat(name,bootinfo);
	}

    if (cfe_finddev(name) != NULL) {
	return 0;
	}

    dev = (cfe_device_t *) KMALLOC(sizeof(cfe_device_t),0);
    if (!dev) return -1;

    dev->dev_fullname = strdup(name);
    dev->dev_softc = softc;
    dev->dev_class = drv->drv_class;
    dev->dev_dispatch = drv->drv_dispatch;
    dev->dev_description = description ? strdup(description) : NULL;
    dev->dev_opencount = 0;

    q_enqueue(&cfe_devices,(queue_t *) dev);

    return 1;

}



/*  *********************************************************************
    *  cfe_attach(drv,softc,bootinfo,description
    *  
    *  Add a device to the system.  This is a callback from the
    *  probe routine, and is used to actually add devices to CFE's
    *  device list.
    *  
    *  Input parameters: 
    *  	   drv - driver structure (from the device driver module)
    *  	   softc - Unique information maintained for this device
    *  	   bootinfo - suffix for long form of the device name.  For
    *  	              example, scsi0.3.1  might mean SCSI controller
    *  	              0, device ID 3, LUN 1.  The bootinfo would be
    *  	              "3.1"
    *  	   description - something nice to say for the devices command
    *  	   
    *  Return value:
    *  	   nothing
    ********************************************************************* */

void cfe_attach(cfe_driver_t *drv,void *softc,
		char *bootinfo,
		char *description)
{
    int idx;
    int res;

    /*
     * Try device indicies 0..CFE_MAX_DEVINST to assign a unique
     * device name for this device.  This is a really braindead way to 
     * do this, but how many devices are we expecting anyway?
     */

    for (idx = 0; idx < CFE_MAX_DEVINST; idx++) {

	res = cfe_attach_idx(drv,idx,softc,bootinfo,description);

	if (res < 0) break;	/* out of memory or other badness */
	if (res > 0) break;	/* success! */
	/* otherwise, try again, slot is taken */
	}

}

/*  *********************************************************************
    *  cfe_attach_init()
    *  
    *  Initialize this module.
    *  
    *  Input parameters: 
    *  	   nothing
    *  	  
    *  Return value:
    *  	   nothing
    ********************************************************************* */
void cfe_attach_init(void)
{
    q_init(&(cfe_devices));
}


/*  *********************************************************************
    *  cfe_device_name(ctx)
    *  
    *  Given a device context, return a device name
    *  
    *  Input parameters: 
    *  	   ctx - context
    *  	   
    *  Return value:
    *  	   name
    ********************************************************************* */

char *cfe_device_name(cfe_devctx_t *ctx)
{
    return ctx->dev_dev->dev_fullname;
}