1 |
/* |
/* |
2 |
* Copyright (C) 2005 Anders Gavare. All rights reserved. |
* Copyright (C) 2005-2006 Anders Gavare. All rights reserved. |
3 |
* |
* |
4 |
* Redistribution and use in source and binary forms, with or without |
* Redistribution and use in source and binary forms, with or without |
5 |
* modification, are permitted provided that the following conditions are met: |
* modification, are permitted provided that the following conditions are met: |
25 |
* SUCH DAMAGE. |
* SUCH DAMAGE. |
26 |
* |
* |
27 |
* |
* |
28 |
* $Id: device.c,v 1.13 2005/02/26 17:37:25 debug Exp $ |
* $Id: device.c,v 1.25 2006/02/18 21:03:11 debug Exp $ |
29 |
* |
* |
30 |
* Device registry framework. |
* Device registry framework. |
31 |
*/ |
*/ |
35 |
#include <string.h> |
#include <string.h> |
36 |
|
|
37 |
#include "device.h" |
#include "device.h" |
38 |
|
#include "memory.h" |
39 |
#include "misc.h" |
#include "misc.h" |
40 |
|
|
41 |
|
|
44 |
static int n_device_entries = 0; |
static int n_device_entries = 0; |
45 |
static int device_exit_on_error = 1; |
static int device_exit_on_error = 1; |
46 |
|
|
47 |
|
static struct pci_entry *pci_entries = NULL; |
48 |
|
static int n_pci_entries = 0; |
49 |
|
|
50 |
|
|
51 |
/* |
/* |
52 |
* device_entry_compar(): |
* device_entry_compar(): |
109 |
|
|
110 |
|
|
111 |
/* |
/* |
112 |
|
* pci_register(): |
113 |
|
* |
114 |
|
* Registers a pci device. The pci device is added to the pci_entries array. |
115 |
|
* |
116 |
|
* Return value is 1 if the pci device was registered. If it was not |
117 |
|
* added, this function does not return. |
118 |
|
*/ |
119 |
|
int pci_register(char *name, void (*initf)(struct machine *, struct memory *, |
120 |
|
struct pci_device *)) |
121 |
|
{ |
122 |
|
pci_entries = realloc(pci_entries, sizeof(struct pci_entry) |
123 |
|
* (n_pci_entries + 1)); |
124 |
|
if (pci_entries == NULL) { |
125 |
|
fprintf(stderr, "pci_register(): out of memory\n"); |
126 |
|
exit(1); |
127 |
|
} |
128 |
|
|
129 |
|
memset(&pci_entries[n_pci_entries], 0, sizeof(struct pci_entry)); |
130 |
|
|
131 |
|
pci_entries[n_pci_entries].name = strdup(name); |
132 |
|
pci_entries[n_pci_entries].initf = initf; |
133 |
|
n_pci_entries ++; |
134 |
|
return 1; |
135 |
|
} |
136 |
|
|
137 |
|
|
138 |
|
/* |
139 |
|
* pci_lookup_initf(): |
140 |
|
* |
141 |
|
* Find a pci device init function by scanning the pci_entries array. |
142 |
|
* |
143 |
|
* Return value is a function pointer, or NULL if the name was not found. |
144 |
|
*/ |
145 |
|
void (*pci_lookup_initf(const char *name))(struct machine *machine, |
146 |
|
struct memory *mem, struct pci_device *pd) |
147 |
|
{ |
148 |
|
int i; |
149 |
|
|
150 |
|
if (name == NULL) { |
151 |
|
fprintf(stderr, "pci_lookup_initf(): name = NULL\n"); |
152 |
|
exit(1); |
153 |
|
} |
154 |
|
|
155 |
|
for (i=0; i<n_pci_entries; i++) |
156 |
|
if (strcmp(name, pci_entries[i].name) == 0) |
157 |
|
return pci_entries[i].initf; |
158 |
|
return NULL; |
159 |
|
} |
160 |
|
|
161 |
|
|
162 |
|
/* |
163 |
* device_lookup(): |
* device_lookup(): |
164 |
* |
* |
165 |
* Lookup a device name by scanning the device_entries array (as a binary |
* Lookup a device name by scanning the device_entries array (as a binary |
170 |
*/ |
*/ |
171 |
struct device_entry *device_lookup(char *name) |
struct device_entry *device_lookup(char *name) |
172 |
{ |
{ |
173 |
int i, step, r, do_return = 0; |
int hi, lo; |
174 |
|
|
175 |
if (name == NULL) { |
if (name == NULL) { |
176 |
fprintf(stderr, "device_lookup(): NULL ptr\n"); |
fprintf(stderr, "device_lookup(): NULL ptr\n"); |
183 |
if (n_device_entries == 0) |
if (n_device_entries == 0) |
184 |
return NULL; |
return NULL; |
185 |
|
|
186 |
i = n_device_entries / 2; |
lo = 0; hi = n_device_entries - 1; |
|
step = i/2 + 1; |
|
187 |
|
|
188 |
for (;;) { |
while (lo <= hi) { |
189 |
if (i < 0) |
int r, i = (lo + hi) / 2; |
|
i = 0; |
|
|
if (i >= n_device_entries) |
|
|
i = n_device_entries - 1; |
|
190 |
|
|
191 |
/* printf("device_lookup(): i=%i step=%i\n", i, step); |
/* printf("device_lookup(): i=%i (lo=%i hi=%i)\n", i, lo, hi); |
192 |
printf(" name='%s', '%s'\n", name, |
printf(" name='%s', '%s'\n", name, |
193 |
device_entries[i].name); */ |
device_entries[i].name); */ |
194 |
|
|
195 |
r = strcmp(name, device_entries[i].name); |
r = strcmp(name, device_entries[i].name); |
196 |
|
if (r == 0) { |
|
if (r < 0) { |
|
|
/* Go left: */ |
|
|
i -= step; |
|
|
if (step == 0) |
|
|
i --; |
|
|
} else if (r > 0) { |
|
|
/* Go right: */ |
|
|
i += step; |
|
|
if (step == 0) |
|
|
i ++; |
|
|
} else { |
|
197 |
/* Found it! */ |
/* Found it! */ |
198 |
return &device_entries[i]; |
return &device_entries[i]; |
199 |
} |
} |
200 |
|
|
201 |
if (do_return) |
/* Try left or right half: */ |
202 |
return NULL; |
if (r < 0) |
203 |
|
hi = i - 1; |
204 |
if (step == 0) |
if (r > 0) |
205 |
do_return = 1; |
lo = i + 1; |
|
|
|
|
step /= 2; |
|
206 |
} |
} |
207 |
|
|
208 |
|
return NULL; |
209 |
} |
} |
210 |
|
|
211 |
|
|
218 |
*/ |
*/ |
219 |
int device_unregister(char *name) |
int device_unregister(char *name) |
220 |
{ |
{ |
221 |
size_t i; |
ssize_t i; |
222 |
struct device_entry *p = device_lookup(name); |
struct device_entry *p = device_lookup(name); |
223 |
|
|
224 |
if (p == NULL) { |
if (p == NULL) { |
253 |
/* |
/* |
254 |
* device_add(): |
* device_add(): |
255 |
* |
* |
256 |
* Add a device to a machine. |
* Add a device to a machine. For example: "kn210 addr=0x12340000" adds a |
257 |
|
* device called "kn210" at a specific address. |
258 |
* |
* |
259 |
* "kn210 addr=0x12340000" adds a kn210 device at a specific address. |
* TODO: This function is quite ugly, and should be cleaned up. |
260 |
*/ |
*/ |
261 |
void *device_add(struct machine *machine, char *name_and_params) |
void *device_add(struct machine *machine, char *name_and_params) |
262 |
{ |
{ |
264 |
struct devinit devinit; |
struct devinit devinit; |
265 |
char *s2, *s3; |
char *s2, *s3; |
266 |
size_t len; |
size_t len; |
267 |
|
int quoted; |
268 |
|
|
269 |
memset(&devinit, 0, sizeof(struct devinit)); |
memset(&devinit, 0, sizeof(struct devinit)); |
270 |
devinit.machine = machine; |
devinit.machine = machine; |
271 |
|
|
272 |
/* Default values: */ |
/* Default values: */ |
273 |
devinit.addr_mult = 1; |
devinit.addr_mult = 1; |
274 |
|
devinit.in_use = 1; |
275 |
|
|
276 |
/* Get the device name first: */ |
/* Get the device name first: */ |
277 |
s2 = name_and_params; |
s2 = name_and_params; |
286 |
} |
} |
287 |
memcpy(devinit.name, name_and_params, len); |
memcpy(devinit.name, name_and_params, len); |
288 |
devinit.name[len] = '\0'; |
devinit.name[len] = '\0'; |
289 |
|
devinit.dma_irq_nr = -1; |
290 |
|
|
291 |
p = device_lookup(devinit.name); |
p = device_lookup(devinit.name); |
292 |
if (p == NULL) { |
if (p == NULL) { |
324 |
|
|
325 |
if (strncmp(s2, "addr=", 5) == 0) { |
if (strncmp(s2, "addr=", 5) == 0) { |
326 |
devinit.addr = mystrtoull(s3, NULL, 0); |
devinit.addr = mystrtoull(s3, NULL, 0); |
327 |
|
} else if (strncmp(s2, "addr2=", 6) == 0) { |
328 |
|
devinit.addr2 = mystrtoull(s3, NULL, 0); |
329 |
} else if (strncmp(s2, "len=", 4) == 0) { |
} else if (strncmp(s2, "len=", 4) == 0) { |
330 |
devinit.len = mystrtoull(s3, NULL, 0); |
devinit.len = mystrtoull(s3, NULL, 0); |
331 |
} else if (strncmp(s2, "addr_mult=", 10) == 0) { |
} else if (strncmp(s2, "addr_mult=", 10) == 0) { |
332 |
devinit.addr_mult = mystrtoull(s3, NULL, 0); |
devinit.addr_mult = mystrtoull(s3, NULL, 0); |
333 |
|
} else if (strncmp(s2, "pci_little_endian=", 18) == 0) { |
334 |
|
devinit.pci_little_endian = mystrtoull(s3, NULL, 0); |
335 |
|
switch (devinit.pci_little_endian) { |
336 |
|
case 0: break; |
337 |
|
case 1: devinit.pci_little_endian = |
338 |
|
MEM_PCI_LITTLE_ENDIAN; |
339 |
|
break; |
340 |
|
default:fatal("Bad pci_little_endian value.\n"); |
341 |
|
exit(1); |
342 |
|
} |
343 |
} else if (strncmp(s2, "irq=", 4) == 0) { |
} else if (strncmp(s2, "irq=", 4) == 0) { |
344 |
devinit.irq_nr = mystrtoull(s3, NULL, 0); |
devinit.irq_nr = mystrtoull(s3, NULL, 0); |
345 |
|
} else if (strncmp(s2, "dma_irq=", 8) == 0) { |
346 |
|
devinit.dma_irq_nr = mystrtoull(s3, NULL, 0); |
347 |
|
} else if (strncmp(s2, "in_use=", 7) == 0) { |
348 |
|
devinit.in_use = mystrtoull(s3, NULL, 0); |
349 |
|
} else if (strncmp(s2, "name2=", 6) == 0) { |
350 |
|
char *h = s2 + 6; |
351 |
|
size_t len = 0; |
352 |
|
quoted = 0; |
353 |
|
while (*h) { |
354 |
|
if (*h == '\'') |
355 |
|
quoted = !quoted; |
356 |
|
h++, len++; |
357 |
|
if (!quoted && *h == ' ') |
358 |
|
break; |
359 |
|
} |
360 |
|
devinit.name2 = malloc(len + 1); |
361 |
|
if (devinit.name2 == NULL) { |
362 |
|
fprintf(stderr, "out of memory\n"); |
363 |
|
exit(1); |
364 |
|
} |
365 |
|
h = s2 + 6; |
366 |
|
if (*h == '\'') |
367 |
|
len -= 2, h++; |
368 |
|
snprintf(devinit.name2, len + 1, h); |
369 |
} else { |
} else { |
370 |
fatal("unknown param: %s\n", s2); |
fatal("unknown param: %s\n", s2); |
371 |
if (device_exit_on_error) |
if (device_exit_on_error) |
376 |
|
|
377 |
/* skip to the next param: */ |
/* skip to the next param: */ |
378 |
s2 = s3; |
s2 = s3; |
379 |
while (*s2 != '\0' && *s2 != ' ' && *s2 != ',' && *s2 != ';') |
quoted = 0; |
380 |
|
while (*s2 != '\0' && (*s2 != ' ' || quoted) && |
381 |
|
*s2 != ',' && *s2 != ';') { |
382 |
|
if (*s2 == '\'') |
383 |
|
quoted = !quoted; |
384 |
s2 ++; |
s2 ++; |
385 |
|
} |
386 |
} |
} |
387 |
|
|
388 |
|
|
434 |
|
|
435 |
/* |
/* |
436 |
* device_set_exit_on_error(): |
* device_set_exit_on_error(): |
437 |
|
* |
438 |
|
* This function selects the behaviour of the emulator when a device is not |
439 |
|
* found. During startup, it is nicest to abort the whole emulator session, |
440 |
|
* but if a device addition is attempted from within the debugger, then it is |
441 |
|
* nicer to just print a warning and continue. |
442 |
*/ |
*/ |
443 |
void device_set_exit_on_error(int exit_on_error) |
void device_set_exit_on_error(int exit_on_error) |
444 |
{ |
{ |
450 |
* device_init(): |
* device_init(): |
451 |
* |
* |
452 |
* Initialize the device registry, and call autodev_init() to automatically |
* Initialize the device registry, and call autodev_init() to automatically |
453 |
* add all normal devices (from the devices/ directory). |
* add all normal devices (from the src/devices/ directory). |
454 |
* |
* |
455 |
* This function should be called before any other device_*() function is used. |
* This function should be called before any other device_*() function is used. |
456 |
*/ |
*/ |