/[gxemul]/trunk/src/device.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/src/device.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 19 by dpavlin, Mon Oct 8 16:18:38 2007 UTC revision 20 by dpavlin, Mon Oct 8 16:19:23 2007 UTC
# Line 25  Line 25 
25   *  SUCH DAMAGE.   *  SUCH DAMAGE.
26   *   *
27   *   *
28   *  $Id: device.c,v 1.16 2005/08/10 22:25:50 debug Exp $   *  $Id: device.c,v 1.20 2005/11/22 16:26:35 debug Exp $
29   *   *
30   *  Device registry framework.   *  Device registry framework.
31   */   */
# Line 35  Line 35 
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    
# Line 43  static int device_entries_sorted = 0; Line 44  static int device_entries_sorted = 0;
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():
# Line 105  int device_register(char *name, int (*in Line 109  int device_register(char *name, int (*in
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(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
# Line 115  int device_register(char *name, int (*in Line 170  int device_register(char *name, int (*in
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");
# Line 128  struct device_entry *device_lookup(char Line 183  struct device_entry *device_lookup(char
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;
   
                 if (step & 1)  
                         step = (step/2) + 1;  
                 else  
                         step /= 2;  
206          }          }
207    
208            return NULL;
209  }  }
210    
211    
# Line 285  void *device_add(struct machine *machine Line 322  void *device_add(struct machine *machine
322    
323                  if (strncmp(s2, "addr=", 5) == 0) {                  if (strncmp(s2, "addr=", 5) == 0) {
324                          devinit.addr = mystrtoull(s3, NULL, 0);                          devinit.addr = mystrtoull(s3, NULL, 0);
325                    } else if (strncmp(s2, "addr2=", 6) == 0) {
326                            devinit.addr2 = mystrtoull(s3, NULL, 0);
327                  } else if (strncmp(s2, "len=", 4) == 0) {                  } else if (strncmp(s2, "len=", 4) == 0) {
328                          devinit.len = mystrtoull(s3, NULL, 0);                          devinit.len = mystrtoull(s3, NULL, 0);
329                  } else if (strncmp(s2, "addr_mult=", 10) == 0) {                  } else if (strncmp(s2, "addr_mult=", 10) == 0) {
330                          devinit.addr_mult = mystrtoull(s3, NULL, 0);                          devinit.addr_mult = mystrtoull(s3, NULL, 0);
331                    } else if (strncmp(s2, "pci_little_endian=", 18) == 0) {
332                            devinit.pci_little_endian = mystrtoull(s3, NULL, 0);
333                            switch (devinit.pci_little_endian) {
334                            case 0: break;
335                            case 1: devinit.pci_little_endian =
336                                        MEM_PCI_LITTLE_ENDIAN;
337                                    break;
338                            default:fatal("Bad pci_little_endian value.\n");
339                                    exit(1);
340                            }
341                  } else if (strncmp(s2, "irq=", 4) == 0) {                  } else if (strncmp(s2, "irq=", 4) == 0) {
342                          devinit.irq_nr = mystrtoull(s3, NULL, 0);                          devinit.irq_nr = mystrtoull(s3, NULL, 0);
343                  } else if (strncmp(s2, "in_use=", 7) == 0) {                  } else if (strncmp(s2, "in_use=", 7) == 0) {

Legend:
Removed from v.19  
changed lines
  Added in v.20

  ViewVC Help
Powered by ViewVC 1.1.26