diff options
author | Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com> | 2008-08-31 04:24:55 +0200 |
---|---|---|
committer | Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com> | 2008-08-31 04:24:55 +0200 |
commit | c1de7a6daf9c657484e1c6d433f01fccd49a7f48 (patch) | |
tree | 7d73ef79e8208b3ac21de13a4a4e968dbf0e6e19 | |
parent | ef0255fc75f28655f9681422079287d68a14dbaa (diff) | |
download | u-boot-c1de7a6daf9c657484e1c6d433f01fccd49a7f48.tar.gz |
devices: merge to list_head
Signed-off-by: Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
-rw-r--r-- | common/Makefile | 1 | ||||
-rw-r--r-- | common/cmd_console.c | 9 | ||||
-rw-r--r-- | common/cmd_terminal.c | 10 | ||||
-rw-r--r-- | common/console.c | 62 | ||||
-rw-r--r-- | common/devices.c | 79 | ||||
-rw-r--r-- | common/lists.c | 734 | ||||
-rw-r--r-- | common/usb_kbd.c | 14 | ||||
-rw-r--r-- | include/devices.h | 8 | ||||
-rw-r--r-- | include/lists.h | 77 |
9 files changed, 87 insertions, 907 deletions
diff --git a/common/Makefile b/common/Makefile index 6365faeefe..0fe9c8b42f 100644 --- a/common/Makefile +++ b/common/Makefile @@ -37,7 +37,6 @@ COBJS-y += dlmalloc.o COBJS-y += exports.o COBJS-y += hush.o COBJS-y += image.o -COBJS-y += lists.o COBJS-y += memsize.o COBJS-y += s_record.o COBJS-y += serial.o diff --git a/common/cmd_console.c b/common/cmd_console.c index 50ddb011cd..e2bc2a30d9 100644 --- a/common/cmd_console.c +++ b/common/cmd_console.c @@ -31,14 +31,17 @@ extern void _do_coninfo (void); int do_coninfo (cmd_tbl_t * cmd, int flag, int argc, char *argv[]) { - int i, l; + int l; + struct list_head *list = device_get_list(); + struct list_head *pos; + device_t *dev; /* Scan for valid output and input devices */ puts ("List of available devices:\n"); - for (i = 1; i <= ListNumItems (devlist); i++) { - device_t *dev = ListGetPtrToItem (devlist, i); + list_for_each(pos, list) { + dev = list_entry(pos, device_t, list); printf ("%-8s %08x %c%c%c ", dev->name, diff --git a/common/cmd_terminal.c b/common/cmd_terminal.c index 57943c10b9..67a2546731 100644 --- a/common/cmd_terminal.c +++ b/common/cmd_terminal.c @@ -27,10 +27,10 @@ #include <common.h> #include <command.h> #include <devices.h> +#include <serial.h> int do_terminal(cmd_tbl_t * cmd, int flag, int argc, char *argv[]) { - int i, l; int last_tilde = 0; device_t *dev = NULL; @@ -38,13 +38,7 @@ int do_terminal(cmd_tbl_t * cmd, int flag, int argc, char *argv[]) return -1; /* Scan for selected output/input device */ - for (i = 1; i <= ListNumItems (devlist); i++) { - device_t *tmp = ListGetPtrToItem (devlist, i); - if (!strcmp(tmp->name, argv[1])) { - dev = tmp; - break; - } - } + dev = device_get_by_name(argv[1]); if (!dev) return -1; diff --git a/common/console.c b/common/console.c index 1b095b1ca5..cfcaeb86ca 100644 --- a/common/console.c +++ b/common/console.c @@ -325,9 +325,22 @@ inline void dbg(const char *fmt, ...) /** U-Boot INIT FUNCTIONS *************************************************/ +device_t *search_device (int flags, char *name) +{ + device_t *dev; + + dev = device_get_by_name(name); + + if(dev && (dev->flags & flags)) + return dev; + + return NULL; +} + int console_assign (int file, char *devname) { - int flag, i; + int flag; + device_t *dev; /* Check for valid file */ switch (file) { @@ -344,16 +357,10 @@ int console_assign (int file, char *devname) /* Check for valid device name */ - for (i = 1; i <= ListNumItems (devlist); i++) { - device_t *dev = ListGetPtrToItem (devlist, i); - - if (strcmp (devname, dev->name) == 0) { - if (dev->flags & flag) - return console_setfile (file, dev); + dev = search_device(flag, devname); - return -1; - } - } + if(dev) + return console_setfile (file, dev); return -1; } @@ -371,27 +378,6 @@ int console_init_f (void) return (0); } -#if defined(CFG_CONSOLE_IS_IN_ENV) || defined(CONFIG_SPLASH_SCREEN) || defined(CONFIG_SILENT_CONSOLE) -/* search a device */ -device_t *search_device (int flags, char *name) -{ - int i, items; - device_t *dev = NULL; - - items = ListNumItems (devlist); - if (name == NULL) - return dev; - - for (i = 1; i <= items; i++) { - dev = ListGetPtrToItem (devlist, i); - if ((dev->flags & flags) && (strcmp (name, dev->name) == 0)) { - break; - } - } - return dev; -} -#endif /* CFG_CONSOLE_IS_IN_ENV || CONFIG_SPLASH_SCREEN */ - #ifdef CFG_CONSOLE_IS_IN_ENV /* Called after the relocation - use desired console functions */ int console_init_r (void) @@ -488,7 +474,10 @@ int console_init_r (void) int console_init_r (void) { device_t *inputdev = NULL, *outputdev = NULL; - int i, items = ListNumItems (devlist); + int i; + struct list_head *list = device_get_list(); + struct list_head *pos; + device_t *dev; #ifdef CONFIG_SPLASH_SCREEN /* suppress all output if splash screen is enabled and we have @@ -498,11 +487,8 @@ int console_init_r (void) #endif /* Scan devices looking for input and output devices */ - for (i = 1; - (i <= items) && ((inputdev == NULL) || (outputdev == NULL)); - i++ - ) { - device_t *dev = ListGetPtrToItem (devlist, i); + list_for_each(pos, list) { + dev = list_entry(pos, device_t, list); if ((dev->flags & DEV_FLAGS_INPUT) && (inputdev == NULL)) { inputdev = dev; @@ -510,6 +496,8 @@ int console_init_r (void) if ((dev->flags & DEV_FLAGS_OUTPUT) && (outputdev == NULL)) { outputdev = dev; } + if(inputdev && outputdev) + break; } /* Initializes output console first */ diff --git a/common/devices.c b/common/devices.c index 9cc963ac20..2977436420 100644 --- a/common/devices.c +++ b/common/devices.c @@ -36,7 +36,7 @@ DECLARE_GLOBAL_DATA_PTR; -list_t devlist = 0; +static device_t devs; device_t *stdio_devices[] = { NULL, NULL, NULL }; char *stdio_names[MAX_FILES] = { "stdin", "stdout", "stderr" }; @@ -48,18 +48,18 @@ char *stdio_names[MAX_FILES] = { "stdin", "stdout", "stderr" }; #ifdef CFG_DEVICE_NULLDEV void nulldev_putc(const char c) { - /* nulldev is empty! */ + /* nulldev is empty! */ } void nulldev_puts(const char *s) { - /* nulldev is empty! */ + /* nulldev is empty! */ } int nulldev_input(void) { - /* nulldev is empty! */ - return 0; + /* nulldev is empty! */ + return 0; } #endif @@ -108,10 +108,32 @@ static void drv_system_init (void) * DEVICES ************************************************************************** */ +struct list_head* device_get_list(void) +{ + return &(devs.list); +} + +device_t* device_get_by_name(char* name) +{ + struct list_head *pos; + device_t *dev; + + if(!name) + return NULL; + + list_for_each(pos, &(devs.list)) { + dev = list_entry(pos, device_t, list); + if(strcmp(dev->name, name) == 0) + return dev; + } + + return NULL; +} + int device_register (device_t * dev) { - ListInsertItem (devlist, dev, LIST_END); + list_add(&(dev->list), &(devs.list)); return 0; } @@ -121,20 +143,15 @@ int device_register (device_t * dev) #ifdef CFG_DEVICE_DEREGISTER int device_deregister(char *devname) { - int i,l,dev_index; - device_t *dev = NULL; + int l; + struct list_head *pos; + device_t *dev; char temp_names[3][8]; - dev_index = -1; - for (i=1; i<=ListNumItems(devlist); i++) { - dev = ListGetPtrToItem (devlist, i); - if(strcmp(dev->name,devname)==0) { - dev_index=i; - break; - } - } - if(dev_index<0) /* device not found */ - return 0; + dev = device_get_by_name(devname); + + if(!dev) /* device not found */ + return -1; /* get stdio devices (ListRemoveItem changes the dev list) */ for (l=0 ; l< MAX_FILES; l++) { if (stdio_devices[l] == dev) { @@ -145,14 +162,15 @@ int device_deregister(char *devname) stdio_devices[l]->name, sizeof(stdio_devices[l]->name)); } - ListRemoveItem(devlist,NULL,dev_index); + + list_del(&(dev->list)); + /* reassign Device list */ - for (i=1; i<=ListNumItems(devlist); i++) { - dev = ListGetPtrToItem (devlist, i); + list_for_each(pos, &(devs.list)) { + dev = list_entry(pos, device_t, list); for (l=0 ; l< MAX_FILES; l++) { - if(strcmp(dev->name,temp_names[l])==0) { + if(strcmp(dev->name, temp_names[l]) == 0) stdio_devices[l] = dev; - } } } return 0; @@ -161,7 +179,7 @@ int device_deregister(char *devname) int devices_init (void) { -#ifndef CONFIG_ARM /* already relocated for current ARM implementation */ +#ifndef CONFIG_ARM /* already relocated for current ARM implementation */ ulong relocation_offset = gd->reloc_off; int i; @@ -173,12 +191,8 @@ int devices_init (void) #endif /* Initialize the list */ - devlist = ListCreate (sizeof (device_t)); + INIT_LIST_HEAD(&(devs.list)); - if (devlist == NULL) { - eputs ("Cannot initialize the list of devices!\n"); - return -1; - } #if defined(CONFIG_HARD_I2C) || defined(CONFIG_SOFT_I2C) i2c_init (CFG_I2C_SPEED, CFG_I2C_SLAVE); #endif @@ -207,10 +221,3 @@ int devices_init (void) return (0); } - -int devices_done (void) -{ - ListDispose (devlist); - - return 0; -} diff --git a/common/lists.c b/common/lists.c deleted file mode 100644 index 0dc090ab86..0000000000 --- a/common/lists.c +++ /dev/null @@ -1,734 +0,0 @@ -#include <common.h> -#include <malloc.h> -#include <lists.h> - -#define MAX(a,b) (((a)>(b)) ? (a) : (b)) -#define MIN(a,b) (((a)<(b)) ? (a) : (b)) -#define CAT4CHARS(a,b,c,d) ((a<<24) | (b<<16) | (c<<8) | d) - -/* increase list size by 10% every time it is full */ -#define kDefaultAllocationPercentIncrease 10 - -/* always increase list size by 4 items when it is full */ -#define kDefaultAllocationminNumItemsIncrease 4 - -/* - * how many items to expand the list by when it becomes full - * = current listSize (in items) + (hiword percent of list size) + loword - */ -#define NUMITEMSPERALLOC(list) MAX(((*list)->listSize * \ - ((*list)->percentIncrease + 100)) / 100, \ - (*list)->minNumItemsIncrease ) - -#define ITEMPTR(list,item) &(((char *)&(*list)->itemList)[(*(list))->itemSize * (item)]) - -#define LIST_SIGNATURE CAT4CHARS('L', 'I', 'S', 'T'); - -#define calloc(size,num) malloc(size*num) - -/********************************************************************/ - -Handle NewHandle (unsigned int numBytes) -{ - void *memPtr; - HandleRecord *hanPtr; - - memPtr = calloc (numBytes, 1); - hanPtr = (HandleRecord *) calloc (sizeof (HandleRecord), 1); - if (hanPtr && (memPtr || numBytes == 0)) { - hanPtr->ptr = memPtr; - hanPtr->size = numBytes; - return (Handle) hanPtr; - } else { - free (memPtr); - free (hanPtr); - return NULL; - } -} -/********************************************************************/ - -void DisposeHandle (Handle handle) -{ - if (handle) { - free (*handle); - free ((void *) handle); - } -} -/********************************************************************/ - -unsigned int GetHandleSize (Handle handle) -{ - return ((HandleRecord *) handle)->size; -} -/********************************************************************/ - -int SetHandleSize (Handle handle, unsigned int newSize) -{ - HandleRecord *hanRecPtr = (HandleRecord *) handle; - void *newPtr, *oldPtr; - unsigned int oldSize; - - - oldPtr = hanRecPtr->ptr; - oldSize = hanRecPtr->size; - - if (oldSize == newSize) - return 1; - - if (oldPtr == NULL) { - newPtr = malloc (newSize); - } else { - newPtr = realloc (oldPtr, newSize); - } - if (newPtr || (newSize == 0)) { - hanRecPtr->ptr = newPtr; - hanRecPtr->size = newSize; - if (newSize > oldSize) - memset ((char *) newPtr + oldSize, 0, newSize - oldSize); - return 1; - } else - return 0; -} - -#ifdef CFG_ALL_LIST_FUNCTIONS - -/* Used to compare list elements by their raw data contents */ -static int ListMemBlockCmp (void *a, void *b, int size) -{ - return memcmp (a, b, size); -} - -/***************************************************************************/ - -/* - * Binary search numElements of size elementSize in array for a match - * to the. item. Return the index of the element that matches - * (0 - numElements - 1). If no match is found return the -i-1 where - * i is the index (0 - numElements) where the item should be placed. - * (*theCmp)(a,b) should return <0 if a<b, 0 if a==b, >0 if a>b. - * - * This function is like the C-Library function bsearch() except that - * this function returns the index where the item should be placed if - * it is not found. - */ -int BinSearch ( void *array, int numElements, int elementSize, - void *itemPtr, CompareFunction compareFunction) -{ - int low, high, mid, cmp; - void *arrayItemPtr; - - for (low = 0, high = numElements - 1, mid = 0, cmp = -1; low <= high;) { - mid = (low + high) >> 1; - - arrayItemPtr = (void *) (((char *) array) + (mid * elementSize)); - cmp = compareFunction - ? compareFunction (itemPtr, arrayItemPtr) - : ListMemBlockCmp (itemPtr, arrayItemPtr, elementSize); - if (cmp == 0) { - return mid; - } else if (cmp < 0) { - high = mid - 1; - } else { - low = mid + 1; - } - } - if (cmp > 0) - mid++; - - return -mid - 1; -} - -#endif /* CFG_ALL_LIST_FUNCTIONS */ - -/*******************************************************************************/ - -/* - * If numNewItems == 0 then expand the list by the number of items - * indicated by its allocation policy. - * If numNewItems > 0 then expand the list by exactly the number of - * items indicated. - * If numNewItems < 0 then expand the list by the absolute value of - * numNewItems plus the number of items indicated by its allocation - * policy. - * Returns 1 for success, 0 if out of memory -*/ -static int ExpandListSpace (list_t list, int numNewItems) -{ - if (numNewItems == 0) { - numNewItems = NUMITEMSPERALLOC (list); - } else if (numNewItems < 0) { - numNewItems = (-numNewItems) + NUMITEMSPERALLOC (list); - } - - if (SetHandleSize ((Handle) list, - sizeof (ListStruct) + - ((*list)->listSize + - numNewItems) * (*list)->itemSize)) { - (*list)->listSize += numNewItems; - return 1; - } else { - return 0; - } -} - -/*******************************/ - -#ifdef CFG_ALL_LIST_FUNCTIONS - -/* - * This function reallocate the list, minus any currently unused - * portion of its allotted memory. - */ -void ListCompact (list_t list) -{ - - if (!SetHandleSize ((Handle) list, - sizeof (ListStruct) + - (*list)->numItems * (*list)->itemSize)) { - return; - } - - (*list)->listSize = (*list)->numItems; -} - -#endif /* CFG_ALL_LIST_FUNCTIONS */ - -/*******************************/ - -list_t ListCreate (int elementSize) -{ - list_t list; - - list = (list_t) (NewHandle (sizeof (ListStruct))); /* create empty list */ - if (list) { - (*list)->signature = LIST_SIGNATURE; - (*list)->numItems = 0; - (*list)->listSize = 0; - (*list)->itemSize = elementSize; - (*list)->percentIncrease = kDefaultAllocationPercentIncrease; - (*list)->minNumItemsIncrease = - kDefaultAllocationminNumItemsIncrease; - } - - return list; -} - -/*******************************/ - -void ListSetAllocationPolicy (list_t list, int minItemsPerAlloc, - int percentIncreasePerAlloc) -{ - (*list)->percentIncrease = percentIncreasePerAlloc; - (*list)->minNumItemsIncrease = minItemsPerAlloc; -} - -/*******************************/ - -void ListDispose (list_t list) -{ - DisposeHandle ((Handle) list); -} -/*******************************/ - -#ifdef CFG_ALL_LIST_FUNCTIONS - -void ListDisposePtrList (list_t list) -{ - int index; - int numItems; - - if (list) { - numItems = ListNumItems (list); - - for (index = 1; index <= numItems; index++) - free (*(void **) ListGetPtrToItem (list, index)); - - ListDispose (list); - } -} - -/*******************************/ - -/* - * keeps memory, resets the number of items to 0 - */ -void ListClear (list_t list) -{ - if (!list) - return; - (*list)->numItems = 0; -} - -/*******************************/ - -/* - * copy is only as large as necessary - */ -list_t ListCopy (list_t originalList) -{ - list_t tempList = NULL; - int numItems; - - if (!originalList) - return NULL; - - tempList = ListCreate ((*originalList)->itemSize); - if (tempList) { - numItems = ListNumItems (originalList); - - if (!SetHandleSize ((Handle) tempList, - sizeof (ListStruct) + - numItems * (*tempList)->itemSize)) { - ListDispose (tempList); - return NULL; - } - - (*tempList)->numItems = (*originalList)->numItems; - (*tempList)->listSize = (*originalList)->numItems; - (*tempList)->itemSize = (*originalList)->itemSize; - (*tempList)->percentIncrease = (*originalList)->percentIncrease; - (*tempList)->minNumItemsIncrease = - (*originalList)->minNumItemsIncrease; - - memcpy (ITEMPTR (tempList, 0), ITEMPTR (originalList, 0), - numItems * (*tempList)->itemSize); - } - - return tempList; -} - -/********************************/ - -/* - * list1 = list1 + list2 - */ -int ListAppend (list_t list1, list_t list2) -{ - int numItemsL1, numItemsL2; - - if (!list2) - return 1; - - if (!list1) - return 0; - if ((*list1)->itemSize != (*list2)->itemSize) - return 0; - - numItemsL1 = ListNumItems (list1); - numItemsL2 = ListNumItems (list2); - - if (numItemsL2 == 0) - return 1; - - if (!SetHandleSize ((Handle) list1, - sizeof (ListStruct) + (numItemsL1 + numItemsL2) * - (*list1)->itemSize)) { - return 0; - } - - (*list1)->numItems = numItemsL1 + numItemsL2; - (*list1)->listSize = numItemsL1 + numItemsL2; - - memmove (ITEMPTR (list1, numItemsL1), - ITEMPTR (list2, 0), - numItemsL2 * (*list2)->itemSize); - - return 1; -} - -#endif /* CFG_ALL_LIST_FUNCTIONS */ - -/*******************************/ - -/* - * returns 1 if the item is inserted, returns 0 if out of memory or - * bad arguments were passed. - */ -int ListInsertItem (list_t list, void *ptrToItem, int itemPosition) -{ - return ListInsertItems (list, ptrToItem, itemPosition, 1); -} - -/*******************************/ - -int ListInsertItems (list_t list, void *ptrToItems, int firstItemPosition, - int numItemsToInsert) -{ - int numItems = (*list)->numItems; - - if (firstItemPosition == numItems + 1) - firstItemPosition = LIST_END; - else if (firstItemPosition > numItems) - return 0; - - if ((*list)->numItems >= (*list)->listSize) { - if (!ExpandListSpace (list, -numItemsToInsert)) - return 0; - } - - if (firstItemPosition == LIST_START) { - if (numItems == 0) { - /* special case for empty list */ - firstItemPosition = LIST_END; - } else { - firstItemPosition = 1; - } - } - - if (firstItemPosition == LIST_END) { /* add at the end of the list */ - if (ptrToItems) - memcpy (ITEMPTR (list, numItems), ptrToItems, - (*list)->itemSize * numItemsToInsert); - else - memset (ITEMPTR (list, numItems), 0, - (*list)->itemSize * numItemsToInsert); - - (*list)->numItems += numItemsToInsert; - } else { /* move part of list up to make room for new item */ - memmove (ITEMPTR (list, firstItemPosition - 1 + numItemsToInsert), - ITEMPTR (list, firstItemPosition - 1), - (numItems + 1 - firstItemPosition) * (*list)->itemSize); - - if (ptrToItems) - memmove (ITEMPTR (list, firstItemPosition - 1), ptrToItems, - (*list)->itemSize * numItemsToInsert); - else - memset (ITEMPTR (list, firstItemPosition - 1), 0, - (*list)->itemSize * numItemsToInsert); - - (*list)->numItems += numItemsToInsert; - } - - return 1; -} - -#ifdef CFG_ALL_LIST_FUNCTIONS - -/*******************************/ - -int ListEqual (list_t list1, list_t list2) -{ - if (list1 == list2) - return 1; - - if (list1 == NULL || list2 == NULL) - return 0; - - if ((*list1)->itemSize == (*list1)->itemSize) { - if ((*list1)->numItems == (*list2)->numItems) { - return (memcmp (ITEMPTR (list1, 0), ITEMPTR (list2, 0), - (*list1)->itemSize * (*list1)->numItems) == 0); - } - } - - return 0; -} - -/*******************************/ - -/* - * The item pointed to by ptrToItem is copied over the current item - * at itemPosition - */ -void ListReplaceItem (list_t list, void *ptrToItem, int itemPosition) -{ - ListReplaceItems (list, ptrToItem, itemPosition, 1); -} - -/*******************************/ - -/* - * The item pointed to by ptrToItems is copied over the current item - * at itemPosition - */ -void ListReplaceItems ( list_t list, void *ptrToItems, - int firstItemPosition, int numItemsToReplace) -{ - - if (firstItemPosition == LIST_END) - firstItemPosition = (*list)->numItems; - else if (firstItemPosition == LIST_START) - firstItemPosition = 1; - - memmove (ITEMPTR (list, firstItemPosition - 1), ptrToItems, - (*list)->itemSize * numItemsToReplace); -} - -/*******************************/ - -void ListGetItem (list_t list, void *itemDestination, int itemPosition) -{ - ListGetItems (list, itemDestination, itemPosition, 1); -} - -#endif /* CFG_ALL_LIST_FUNCTIONS */ - -/*******************************/ - -#if defined(CFG_ALL_LIST_FUNCTIONS) || defined(CFG_DEVICE_DEREGISTER) - -void ListRemoveItem (list_t list, void *itemDestination, int itemPosition) -{ - ListRemoveItems (list, itemDestination, itemPosition, 1); -} - -/*******************************/ - -void ListRemoveItems (list_t list, void *itemsDestination, - int firstItemPosition, int numItemsToRemove) -{ - int firstItemAfterChunk, numToMove; - - if (firstItemPosition == LIST_START) - firstItemPosition = 1; - else if (firstItemPosition == LIST_END) - firstItemPosition = (*list)->numItems; - - if (itemsDestination != NULL) - memcpy (itemsDestination, ITEMPTR (list, firstItemPosition - 1), - (*list)->itemSize * numItemsToRemove); - - firstItemAfterChunk = firstItemPosition + numItemsToRemove; - numToMove = (*list)->numItems - (firstItemAfterChunk - 1); - - if (numToMove > 0) { - /* - * move part of list down to cover hole left by removed item - */ - memmove (ITEMPTR (list, firstItemPosition - 1), - ITEMPTR (list, firstItemAfterChunk - 1), - (*list)->itemSize * numToMove); - } - - (*list)->numItems -= numItemsToRemove; -} -#endif /* CFG_ALL_LIST_FUNCTIONS || CFG_DEVICE_DEREGISTER */ - -/*******************************/ - -void ListGetItems (list_t list, void *itemsDestination, - int firstItemPosition, int numItemsToGet) -{ - - if (firstItemPosition == LIST_START) - firstItemPosition = 1; - else if (firstItemPosition == LIST_END) - firstItemPosition = (*list)->numItems; - - memcpy (itemsDestination, - ITEMPTR (list, firstItemPosition - 1), - (*list)->itemSize * numItemsToGet); -} - -/*******************************/ - -/* - * Returns a pointer to the item at itemPosition. returns null if an - * errors occurred. - */ -void *ListGetPtrToItem (list_t list, int itemPosition) -{ - if (itemPosition == LIST_START) - itemPosition = 1; - else if (itemPosition == LIST_END) - itemPosition = (*list)->numItems; - - return ITEMPTR (list, itemPosition - 1); -} - -/*******************************/ - -/* - * returns a pointer the lists data (abstraction violation for - * optimization) - */ -void *ListGetDataPtr (list_t list) -{ - return &((*list)->itemList[0]); -} - -/********************************/ - -#ifdef CFG_ALL_LIST_FUNCTIONS - -int ListApplyToEach (list_t list, int ascending, - ListApplicationFunc funcToApply, - void *callbackData) -{ - int result = 0, index; - - if (!list || !funcToApply) - goto Error; - - if (ascending) { - for (index = 1; index <= ListNumItems (list); index++) { - result = funcToApply (index, - ListGetPtrToItem (list, index), - callbackData); - if (result < 0) - goto Error; - } - } else { - for (index = ListNumItems (list); - index > 0 && index <= ListNumItems (list); - index--) { - result = funcToApply (index, - ListGetPtrToItem (list, index), - callbackData); - if (result < 0) - goto Error; - } - } - -Error: - return result; -} - -#endif /* CFG_ALL_LIST_FUNCTIONS */ - -/********************************/ - -int ListGetItemSize (list_t list) -{ - return (*list)->itemSize; -} - -/********************************/ - -int ListNumItems (list_t list) -{ - return (*list)->numItems; -} - -/*******************************/ - -#ifdef CFG_ALL_LIST_FUNCTIONS - -void ListRemoveDuplicates (list_t list, CompareFunction compareFunction) -{ - int numItems, index, startIndexForFind, duplicatesIndex; - - numItems = ListNumItems (list); - - for (index = 1; index < numItems; index++) { - startIndexForFind = index + 1; - while (startIndexForFind <= numItems) { - duplicatesIndex = - ListFindItem (list, - ListGetPtrToItem (list, index), - startIndexForFind, - compareFunction); - if (duplicatesIndex > 0) { - ListRemoveItem (list, NULL, duplicatesIndex); - numItems--; - startIndexForFind = duplicatesIndex; - } else { - break; - } - } - } -} - -/*******************************/ - - -/*******************************/ - -int ListFindItem (list_t list, void *ptrToItem, int startingPosition, - CompareFunction compareFunction) -{ - int numItems, size, index, cmp; - void *listItemPtr; - - if ((numItems = (*list)->numItems) == 0) - return 0; - - size = (*list)->itemSize; - - if (startingPosition == LIST_START) - startingPosition = 1; - else if (startingPosition == LIST_END) - startingPosition = numItems; - - for (index = startingPosition; index <= numItems; index++) { - listItemPtr = ITEMPTR (list, index - 1); - cmp = compareFunction - ? compareFunction (ptrToItem, listItemPtr) - : ListMemBlockCmp (ptrToItem, listItemPtr, size); - if (cmp == 0) - return index; - } - - return 0; -} - -/*******************************/ - -int ShortCompare (void *a, void *b) -{ - if (*(short *) a < *(short *) b) - return -1; - if (*(short *) a > *(short *) b) - return 1; - return 0; -} - -/*******************************/ - -int IntCompare (void *a, void *b) -{ - if (*(int *) a < *(int *) b) - return -1; - if (*(int *) a > *(int *) b) - return 1; - return 0; -} - -/*******************************/ - -int CStringCompare (void *a, void *b) -{ - return strcmp (*(char **) a, *(char **) b); -} - -/*******************************/ - - -int ListBinSearch (list_t list, void *ptrToItem, - CompareFunction compareFunction) -{ - int index; - - index = BinSearch (ITEMPTR (list, 0), - (int) (*list)->numItems, - (int) (*list)->itemSize, ptrToItem, - compareFunction); - - if (index >= 0) - index++; /* lists start from 1 */ - else - index = 0; /* item not found */ - - return index; -} - -/**************************************************************************/ - -/* - * Reserves memory for numItems in the list. If it succeeds then - * numItems items can be inserted without possibility of an out of - * memory error (useful to simplify error recovery in complex - * functions). Returns 1 if success, 0 if out of memory. - */ -int ListPreAllocate (list_t list, int numItems) -{ - if ((*list)->listSize - (*list)->numItems < numItems) { - return ExpandListSpace (list, - numItems - ((*list)->listSize - - (*list)->numItems)); - } else { - return 1; /* enough items are already pre-allocated */ - } -} - -#endif /* CFG_ALL_LIST_FUNCTIONS */ diff --git a/common/usb_kbd.c b/common/usb_kbd.c index 04d9730e6f..108bd60f95 100644 --- a/common/usb_kbd.c +++ b/common/usb_kbd.c @@ -152,7 +152,7 @@ static int usb_kbd_probe(struct usb_device *dev, unsigned int ifnum); /* search for keyboard and register it if found */ int drv_usb_kbd_init(void) { - int error,i,index; + int error,i; device_t usb_kbd_dev,*old_dev; struct usb_device *dev; char *stdinname = getenv ("stdin"); @@ -166,13 +166,11 @@ int drv_usb_kbd_init(void) if(usb_kbd_probe(dev,0)==1) { /* Ok, we found a keyboard */ /* check, if it is already registered */ USB_KBD_PRINTF("USB KBD found set up device.\n"); - for (index=1; index<=ListNumItems(devlist); index++) { - old_dev = ListGetPtrToItem(devlist, index); - if(strcmp(old_dev->name,DEVNAME)==0) { - /* ok, already registered, just return ok */ - USB_KBD_PRINTF("USB KBD is already registered.\n"); - return 1; - } + old_dev = device_get_by_name(DEVNAME); + if(old_dev) { + /* ok, already registered, just return ok */ + USB_KBD_PRINTF("USB KBD is already registered.\n"); + return 1; } /* register the keyboard */ USB_KBD_PRINTF("USB KBD register.\n"); diff --git a/include/devices.h b/include/devices.h index 2d9e282e38..490016b694 100644 --- a/include/devices.h +++ b/include/devices.h @@ -21,7 +21,7 @@ * MA 02111-1307 USA */ -#include <lists.h> +#include <linux/list.h> #ifndef _DEVICES_H_ #define _DEVICES_H_ @@ -59,6 +59,7 @@ typedef struct { /* Other functions */ void *priv; /* Private extensions */ + struct list_head list; } device_t; /* @@ -82,7 +83,6 @@ typedef struct { /* * VARIABLES */ -extern list_t devlist; extern device_t *stdio_devices[]; extern char *stdio_names[MAX_FILES]; @@ -91,8 +91,10 @@ extern char *stdio_names[MAX_FILES]; */ int device_register (device_t * dev); int devices_init (void); -int devices_done (void); int device_deregister(char *devname); +struct list_head* device_get_list(void); +device_t* device_get_by_name(char* name); + #ifdef CONFIG_LCD int drv_lcd_init (void); #endif diff --git a/include/lists.h b/include/lists.h deleted file mode 100644 index 10a2a19273..0000000000 --- a/include/lists.h +++ /dev/null @@ -1,77 +0,0 @@ -#ifndef _LISTS_H_ -#define _LISTS_H_ - -#define LIST_START -1 /* Handy Constants that substitute for item positions */ -#define LIST_END 0 /* END_OF_LIST means one past current length of list when */ - /* inserting. Otherwise it refers the last item in the list. */ - -typedef struct - { - void *ptr; - unsigned int size; - } HandleRecord; - -typedef void **Handle; - -typedef int (*CompareFunction)(void *data1, void *data2) ; - -typedef struct ListStructTag - { - int signature; /* debugging aid */ - int percentIncrease; /* %of current size to increase by when list is out of space */ - int minNumItemsIncrease; /* fixed number of items to increase by when list is out of space */ - int listSize; /* number of items than can fit in the currently allocated memory */ - int itemSize; /* the size of each item in the list (same for every item) */ - int numItems; /* number of items currently in the list */ - unsigned char itemList[1]; /* resizable array of list elements */ - } ListStruct; - -typedef struct ListStructTag **list_t; /* The list abstract data type */ -typedef int ( * ListApplicationFunc)(int index, void *ptrToItem, void *callbackData); - -/* Basic List Operations */ -list_t ListCreate(int elementSize); -int ListNumItems(list_t list); -int ListInsertItem(list_t list, void *ptrToItem, int itemPosition); -int ListInsertItems(list_t list, void *ptrToItems, int firstItemPosition, int numItemsToInsert); -void ListDispose(list_t list); -void *ListGetPtrToItem(list_t list, int itemPosition); -void ListRemoveItem(list_t list, void *itemDestination, int itemPosition); -void ListRemoveItems(list_t list, void *itemsDestination, int firstItemPosition, int numItemsToRemove); - -#if 0 /* rarely ever used; kept here for reference just in case ... */ -void ListDisposePtrList(list_t list); -void ListGetItem(list_t list, void *itemDestination, int itemPosition); -void ListReplaceItem(list_t list, void *ptrToItem, int itemPosition); -void ListRemoveItem(list_t list, void *itemDestination, int itemPosition); -void ListGetItems(list_t list, void *itemsDestination, int firstItemPosition, int numItemsToGet); -void ListReplaceItems(list_t list, void *ptrToItems, int firstItemPosition, int numItemsToReplace); -void ListRemoveItems(list_t list, void *itemsDestination, int firstItemPosition, int numItemsToRemove); -list_t ListCopy(list_t originalList); -int ListAppend(list_t list1, list_t list2); -void ListClear(list_t list); -int ListEqual(list_t list1, list_t list2); -int ListInsertInOrder(list_t list, void *ptrToItem, CompareFunction compareFunction); -void *ListGetDataPtr(list_t list); -int ListApplyToEach(list_t list, int ascending, ListApplicationFunc funcToApply, void *callbackData); - -/* List Searching and Sorting */ -int ListFindItem(list_t list, void *ptrToItem, int startingPosition, CompareFunction compareFunction); -void ListRemoveDuplicates(list_t list, CompareFunction compareFunction); -int ListBinSearch(list_t list, void *itemPtr, CompareFunction compareFunction); -void ListQuickSort(list_t list, CompareFunction compareFunction); -void ListHeapSort(list_t list, CompareFunction compareFunction); -void ListInsertionSort(list_t list, CompareFunction compareFunction); -int ListIsSorted(list_t list, CompareFunction compareFunction); - -/* Advanced List Functions */ -void ListSetAllocationPolicy(list_t list, int minItemsPerAlloc, int percentIncreasePerAlloc); -void ListCompact(list_t list); -int ListPreAllocate(list_t list, int numItems); -int ListGetItemSize(list_t list); -int GetIntListFromParmInfo(va_list parmInfo, int numIntegers, list_t *integerList); -int ListInsertAfterItem(list_t list, void *ptrToItem, void *ptrToItemToInsertAfter, CompareFunction compareFunction); -int ListInsertBeforeItem(list_t list, void *ptrToItem, void *ptrToItemToInsertBefore, CompareFunction compareFunction); -#endif /* 0 */ - -#endif /* _LISTS_H_ */ |