/[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 10 by dpavlin, Mon Oct 8 16:18:27 2007 UTC revision 24 by dpavlin, Mon Oct 8 16:19:56 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.14 2005/06/22 08:13:38 debug Exp $   *  $Id: device.c,v 1.26 2006/04/06 19:17:37 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 181  struct device_entry *device_lookup(char Line 218  struct device_entry *device_lookup(char
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) {
# Line 220  int device_unregister(char *name) Line 257  int device_unregister(char *name)
257   *  device called "kn210" at a specific address.   *  device called "kn210" at a specific address.
258   *   *
259   *  TODO: This function is quite ugly, and should be cleaned up.   *  TODO: This function is quite ugly, and should be cleaned up.
260     *  name_and_params should be const.
261   */   */
262  void *device_add(struct machine *machine, char *name_and_params)  void *device_add(struct machine *machine, char *name_and_params)
263  {  {
# Line 227  void *device_add(struct machine *machine Line 265  void *device_add(struct machine *machine
265          struct devinit devinit;          struct devinit devinit;
266          char *s2, *s3;          char *s2, *s3;
267          size_t len;          size_t len;
268            int quoted;
269    
270          memset(&devinit, 0, sizeof(struct devinit));          memset(&devinit, 0, sizeof(struct devinit));
271          devinit.machine = machine;          devinit.machine = machine;
272    
273          /*  Default values:  */          /*  Default values:  */
274          devinit.addr_mult = 1;          devinit.addr_mult = 1;
275            devinit.in_use = 1;
276    
277          /*  Get the device name first:  */          /*  Get the device name first:  */
278          s2 = name_and_params;          s2 = name_and_params;
# Line 247  void *device_add(struct machine *machine Line 287  void *device_add(struct machine *machine
287          }          }
288          memcpy(devinit.name, name_and_params, len);          memcpy(devinit.name, name_and_params, len);
289          devinit.name[len] = '\0';          devinit.name[len] = '\0';
290            devinit.dma_irq_nr = -1;
291    
292          p = device_lookup(devinit.name);          p = device_lookup(devinit.name);
293          if (p == NULL) {          if (p == NULL) {
# Line 284  void *device_add(struct machine *machine Line 325  void *device_add(struct machine *machine
325    
326                  if (strncmp(s2, "addr=", 5) == 0) {                  if (strncmp(s2, "addr=", 5) == 0) {
327                          devinit.addr = mystrtoull(s3, NULL, 0);                          devinit.addr = mystrtoull(s3, NULL, 0);
328                    } else if (strncmp(s2, "addr2=", 6) == 0) {
329                            devinit.addr2 = mystrtoull(s3, NULL, 0);
330                  } else if (strncmp(s2, "len=", 4) == 0) {                  } else if (strncmp(s2, "len=", 4) == 0) {
331                          devinit.len = mystrtoull(s3, NULL, 0);                          devinit.len = mystrtoull(s3, NULL, 0);
332                  } else if (strncmp(s2, "addr_mult=", 10) == 0) {                  } else if (strncmp(s2, "addr_mult=", 10) == 0) {
333                          devinit.addr_mult = mystrtoull(s3, NULL, 0);                          devinit.addr_mult = mystrtoull(s3, NULL, 0);
334                    } else if (strncmp(s2, "pci_little_endian=", 18) == 0) {
335                            devinit.pci_little_endian = mystrtoull(s3, NULL, 0);
336                            switch (devinit.pci_little_endian) {
337                            case 0: break;
338                            case 1: devinit.pci_little_endian =
339                                        MEM_PCI_LITTLE_ENDIAN;
340                                    break;
341                            default:fatal("Bad pci_little_endian value.\n");
342                                    exit(1);
343                            }
344                  } else if (strncmp(s2, "irq=", 4) == 0) {                  } else if (strncmp(s2, "irq=", 4) == 0) {
345                          devinit.irq_nr = mystrtoull(s3, NULL, 0);                          devinit.irq_nr = mystrtoull(s3, NULL, 0);
346                    } else if (strncmp(s2, "dma_irq=", 8) == 0) {
347                            devinit.dma_irq_nr = mystrtoull(s3, NULL, 0);
348                    } else if (strncmp(s2, "in_use=", 7) == 0) {
349                            devinit.in_use = mystrtoull(s3, NULL, 0);
350                    } else if (strncmp(s2, "name2=", 6) == 0) {
351                            char *h = s2 + 6;
352                            size_t len = 0;
353                            quoted = 0;
354                            while (*h) {
355                                    if (*h == '\'')
356                                            quoted = !quoted;
357                                    h++, len++;
358                                    if (!quoted && *h == ' ')
359                                            break;
360                            }
361                            devinit.name2 = malloc(len + 1);
362                            if (devinit.name2 == NULL) {
363                                    fprintf(stderr, "out of memory\n");
364                                    exit(1);
365                            }
366                            h = s2 + 6;
367                            if (*h == '\'')
368                                    len -= 2, h++;
369                            snprintf(devinit.name2, len + 1, h);
370                  } else {                  } else {
371                          fatal("unknown param: %s\n", s2);                          fatal("unknown param: %s\n", s2);
372                          if (device_exit_on_error)                          if (device_exit_on_error)
# Line 300  void *device_add(struct machine *machine Line 377  void *device_add(struct machine *machine
377    
378                  /*  skip to the next param:  */                  /*  skip to the next param:  */
379                  s2 = s3;                  s2 = s3;
380                  while (*s2 != '\0' && *s2 != ' ' && *s2 != ',' && *s2 != ';')                  quoted = 0;
381                    while (*s2 != '\0' && (*s2 != ' ' || quoted) &&
382                        *s2 != ',' && *s2 != ';') {
383                            if (*s2 == '\'')
384                                    quoted = !quoted;
385                          s2 ++;                          s2 ++;
386                    }
387          }          }
388    
389    

Legend:
Removed from v.10  
changed lines
  Added in v.24

  ViewVC Help
Powered by ViewVC 1.1.26