From e73ca5fcd5514be78554c563580501798523bd47 Mon Sep 17 00:00:00 2001 From: Werner Almesberger Date: Thu, 10 Feb 2011 21:15:05 -0300 Subject: [PATCH] tools/lib: cache chip ID in the descriptor - atrf.c (struct atrf_dsc): added field "chip_id" to cache chip ID - atrf.c (identify, atrf_identify): moved identification logic to new function - atrf.c (atrf_open, atrf_identify): cache chip ID when opening the device and return ID from cache --- tools/lib/atrf.c | 106 +++++++++++++++++++++++++---------------------- 1 file changed, 57 insertions(+), 49 deletions(-) diff --git a/tools/lib/atrf.c b/tools/lib/atrf.c index 33624aa..fcdd0c5 100644 --- a/tools/lib/atrf.c +++ b/tools/lib/atrf.c @@ -27,6 +27,7 @@ extern struct atrf_driver atben_driver; struct atrf_dsc { struct atrf_driver *driver; void *handle; + enum atrf_chip_id chip_id; }; @@ -53,55 +54,7 @@ int atrf_clear_error(struct atrf_dsc *dsc) } -struct atrf_dsc *atrf_open(void) -{ - struct atrf_dsc *dsc; - struct atrf_driver *driver; - void *handle; - -#ifdef HAVE_USB - driver = &atusb_driver; -#elif HAVE_BEN - driver = &atben_driver; -#else -#error Need either HAVE_USB or HAVE_BEN -#endif - handle = driver->open(); - if (!handle) - return NULL; - dsc = malloc(sizeof(*dsc)); - if (!dsc) { - perror("malloc"); - exit(1); - } - dsc->driver = driver; - dsc->handle = handle; - return dsc; -} - - -void atrf_close(struct atrf_dsc *dsc) -{ - if (dsc->driver->close) - dsc->driver->close(dsc->handle); - free(dsc); -} - - -void atrf_reset(struct atrf_dsc *dsc) -{ - if (dsc->driver->reset) - dsc->driver->reset(dsc->handle); -} - - -void atrf_reset_rf(struct atrf_dsc *dsc) -{ - dsc->driver->reset_rf(dsc->handle); -} - - -enum atrf_chip_id atrf_identify(struct atrf_dsc *dsc) +static enum atrf_chip_id identify(struct atrf_dsc *dsc) { uint8_t part, version; @@ -132,6 +85,61 @@ enum atrf_chip_id atrf_identify(struct atrf_dsc *dsc) } +struct atrf_dsc *atrf_open(void) +{ + struct atrf_dsc *dsc; + struct atrf_driver *driver; + void *handle; + +#ifdef HAVE_USB + driver = &atusb_driver; +#elif HAVE_BEN + driver = &atben_driver; +#else +#error Need either HAVE_USB or HAVE_BEN +#endif + handle = driver->open(); + if (!handle) + return NULL; + dsc = malloc(sizeof(*dsc)); + if (!dsc) { + perror("malloc"); + exit(1); + } + dsc->driver = driver; + dsc->handle = handle; + dsc->chip_id = identify(dsc); + return dsc; +} + + +void atrf_close(struct atrf_dsc *dsc) +{ + if (dsc->driver->close) + dsc->driver->close(dsc->handle); + free(dsc); +} + + +void atrf_reset(struct atrf_dsc *dsc) +{ + if (dsc->driver->reset) + dsc->driver->reset(dsc->handle); +} + + +void atrf_reset_rf(struct atrf_dsc *dsc) +{ + dsc->driver->reset_rf(dsc->handle); +} + + +enum atrf_chip_id atrf_identify(struct atrf_dsc *dsc) +{ + return dsc->chip_id; +} + + int atrf_test_mode(struct atrf_dsc *dsc) { if (!dsc->driver->test_mode)