/[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 12 by dpavlin, Mon Oct 8 16:18:38 2007 UTC revision 22 by dpavlin, Mon Oct 8 16:19:37 2007 UTC
# Line 1  Line 1 
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:
# 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.25 2006/02/18 21:03:11 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(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
# 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 227  void *device_add(struct machine *machine Line 264  void *device_add(struct machine *machine
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;
# Line 248  void *device_add(struct machine *machine Line 286  void *device_add(struct machine *machine
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) {
# Line 285  void *device_add(struct machine *machine Line 324  void *device_add(struct machine *machine
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) {                  } else if (strncmp(s2, "in_use=", 7) == 0) {
348                          devinit.in_use = mystrtoull(s3, NULL, 0);                          devinit.in_use = mystrtoull(s3, NULL, 0);
349                  } else if (strncmp(s2, "name2=", 6) == 0) {                  } else if (strncmp(s2, "name2=", 6) == 0) {
350                          char *h = s2 + 6;                          char *h = s2 + 6;
351                          size_t len = 0;                          size_t len = 0;
352                          while (*h && *h != ' ')                          quoted = 0;
353                            while (*h) {
354                                    if (*h == '\'')
355                                            quoted = !quoted;
356                                  h++, len++;                                  h++, len++;
357                                    if (!quoted && *h == ' ')
358                                            break;
359                            }
360                          devinit.name2 = malloc(len + 1);                          devinit.name2 = malloc(len + 1);
361                          if (devinit.name2 == NULL) {                          if (devinit.name2 == NULL) {
362                                  fprintf(stderr, "out of memory\n");                                  fprintf(stderr, "out of memory\n");
363                                  exit(1);                                  exit(1);
364                          }                          }
365                          snprintf(devinit.name2, len + 1, s2 + 6);                          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)
# Line 314  void *device_add(struct machine *machine Line 376  void *device_add(struct machine *machine
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    

Legend:
Removed from v.12  
changed lines
  Added in v.22

  ViewVC Help
Powered by ViewVC 1.1.26