From 01dcfd6f951577f42fa406f13999dd4886f3ba8a Mon Sep 17 00:00:00 2001 From: marschap Date: Thu, 1 Nov 2007 16:32:01 +0000 Subject: [PATCH] get rid of static variables: use the Driver structure's PrivateData --- server/drivers/hd44780-bwct-usb.c | 56 ++++++++++++++----------------- server/drivers/hd44780-lcd2usb.c | 30 +++++++---------- server/drivers/hd44780-low.h | 21 +++++++++--- 3 files changed, 55 insertions(+), 52 deletions(-) diff --git a/server/drivers/hd44780-bwct-usb.c b/server/drivers/hd44780-bwct-usb.c index 9433004..5c8e76b 100644 --- a/server/drivers/hd44780-bwct-usb.c +++ b/server/drivers/hd44780-bwct-usb.c @@ -33,11 +33,6 @@ #endif -/* USB device handle & interface index we write to */ -static usb_dev_handle *bwct_usb; -static int bwct_usb_i; - - /** * Initialize the driver. * \param drvthis Pointer to driver structure. @@ -78,7 +73,8 @@ hd_init_bwct_usb(Driver *drvthis) usb_find_busses(); usb_find_devices(); - bwct_usb = NULL; + p->usbHandle = NULL; + p->usbIndex = 0; for (bus = usb_get_busses(); bus != NULL; bus = bus->next) { struct usb_device *dev; @@ -92,41 +88,41 @@ hd_init_bwct_usb(Driver *drvthis) /* Loop through all of the configurations */ for (c = 0; c < dev->descriptor.bNumConfigurations; c++) { /* Loop through all of the interfaces */ - for (bwct_usb_i = 0; bwct_usb_i < dev->config[c].bNumInterfaces; bwct_usb_i++) { + for (p->usbIndex = 0; p->usbIndex < dev->config[c].bNumInterfaces; p->usbIndex++) { int a; /* Loop through all of the alternate settings */ - for (a = 0; a < dev->config[c].interface[bwct_usb_i].num_altsetting; a++) { + for (a = 0; a < dev->config[c].interface[p->usbIndex].num_altsetting; a++) { /* Check if this interface is a BWCT lcd */ - if (((dev->config[c].interface[bwct_usb_i].altsetting[a].bInterfaceClass == 0xFF) && - (dev->config[c].interface[bwct_usb_i].altsetting[a].bInterfaceSubClass == 0x01)) || + if (((dev->config[c].interface[p->usbIndex].altsetting[a].bInterfaceClass == 0xFF) && + (dev->config[c].interface[p->usbIndex].altsetting[a].bInterfaceSubClass == 0x01)) || (dev->descriptor.idProduct == BWCT_USB_PRODUCTID)) { /* BWCT device found; try to find its description and serial number */ - bwct_usb = usb_open(dev); - if (bwct_usb == NULL) { + p->usbHandle = usb_open(dev); + if (p->usbHandle == NULL) { report(RPT_WARNING, "hd_init_bwct_usb: unable to open device"); // return -1; /* it's better to continue */ } else { /* get device information & check for serial number */ - //if (usb_get_string_simple(bwct_usb, dev->descriptor.iManufacturer, + //if (usb_get_string_simple(p->usbHandle, dev->descriptor.iManufacturer, // manufacturer, LCD_MAX_WIDTH) <= 0) // *manufacturer = '\0'; //manufacturer[sizeof(manufacturer)-1] = '\0'; - //if (usb_get_string_simple(bwct_usb, dev->descriptor.iProduct, + //if (usb_get_string_simple(p->usbHandle, dev->descriptor.iProduct, // product, LCD_MAX_WIDTH) <= 0) // *product = '\0'; //product[sizeof(product)-1] = '\0'; - if (usb_get_string_simple(bwct_usb, dev->descriptor.iSerialNumber, + if (usb_get_string_simple(p->usbHandle, dev->descriptor.iSerialNumber, device_serial, LCD_MAX_WIDTH) <= 0) *device_serial = '\0'; device_serial[sizeof(device_serial)-1] = '\0'; if ((*serial != '\0') && (*device_serial == '\0')) { report(RPT_ERR, "hd_init_bwct_usb: unable to get device's serial number"); - usb_close(bwct_usb); + usb_close(p->usbHandle); return -1; } @@ -134,8 +130,8 @@ hd_init_bwct_usb(Driver *drvthis) if ((*serial == '\0') || (strcmp(serial, device_serial) == 0)) goto done; - usb_close(bwct_usb); - bwct_usb = NULL; + usb_close(p->usbHandle); + p->usbHandle = NULL; } } } @@ -145,19 +141,19 @@ hd_init_bwct_usb(Driver *drvthis) } done: - if (bwct_usb != NULL) { + if (p->usbHandle != NULL) { debug(RPT_DEBUG, "hd_init_bwct_usb: opening device succeeded"); - if (usb_claim_interface(bwct_usb, bwct_usb_i) < 0) { + if (usb_claim_interface(p->usbHandle, p->usbIndex) < 0) { #if defined(LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP) - if ((usb_detach_kernel_driver_np(bwct_usb, bwct_usb_i) < 0) || - (usb_claim_interface(bwct_usb, bwct_usb_i) < 0)) { - usb_close(bwct_usb); + if ((usb_detach_kernel_driver_np(p->usbHandle, p->usbIndex) < 0) || + (usb_claim_interface(p->usbHandle, p->usbIndex) < 0)) { + usb_close(p->usbHandle); report(RPT_ERR, "hd_init_bwct_usb: unable to re-claim interface"); return -1; } #else - usb_close(bwct_usb); + usb_close(p->usbHandle); report(RPT_ERR, "hd_init_bwct_usb: unable to claim interface"); return -1; #endif @@ -189,7 +185,7 @@ bwct_usb_HD44780_senddata(PrivateData *p, unsigned char displayID, unsigned char { int type = (flags == RS_DATA) ? BWCT_LCD_DATA : BWCT_LCD_CMD; - usb_control_msg(bwct_usb, USB_TYPE_VENDOR, type, ch, bwct_usb_i, NULL, 0, 1000); + usb_control_msg(p->usbHandle, USB_TYPE_VENDOR, type, ch, p->usbIndex, NULL, 0, 1000); } @@ -209,9 +205,9 @@ bwct_usb_HD44780_scankeypad(PrivateData *p) void bwct_usb_HD44780_close(PrivateData *p) { - if (bwct_usb != NULL) { - usb_close(bwct_usb); - bwct_usb = NULL; + if (p->usbHandle != NULL) { + usb_close(p->usbHandle); + p->usbHandle = NULL; } } @@ -232,8 +228,8 @@ bwct_usb_set_contrast(Driver *drvthis, int promille) // And set it (converted from [0,1000] -> [0,255]). // If successful, update the local value. - if (usb_control_msg(bwct_usb, USB_TYPE_VENDOR, BWCT_LCD_SET_CONTRAST, - (promille * 255) / 1000, bwct_usb_i, NULL, 0, 1000) < 0) + if (usb_control_msg(p->usbHandle, USB_TYPE_VENDOR, BWCT_LCD_SET_CONTRAST, + (promille * 255) / 1000, p->usbIndex, NULL, 0, 1000) < 0) report(RPT_WARNING, "hd_init_lcd2usb: setting contrast failed"); else p->contrast = promille; diff --git a/server/drivers/hd44780-lcd2usb.c b/server/drivers/hd44780-lcd2usb.c index 6509859..c15121a 100644 --- a/server/drivers/hd44780-lcd2usb.c +++ b/server/drivers/hd44780-lcd2usb.c @@ -25,10 +25,6 @@ #endif -/* USB device handle & interface index we write to */ -static usb_dev_handle *lcd2usb; - - /** * Initialize the driver. * \param drvthis Pointer to driver structure. @@ -58,7 +54,7 @@ hd_init_lcd2usb(Driver *drvthis) usb_find_busses(); usb_find_devices(); - lcd2usb = NULL; + p->usbHandle = NULL; for (bus = usb_get_busses(); bus != NULL; bus = bus->next) { struct usb_device *dev; @@ -69,23 +65,23 @@ hd_init_lcd2usb(Driver *drvthis) (dev->descriptor.idProduct == LCD2USB_PRODUCTID)) { /* LCD2USB device found; try to find its description */ - lcd2usb = usb_open(dev); - if (lcd2usb == NULL) { + p->usbHandle = usb_open(dev); + if (p->usbHandle == NULL) { report(RPT_WARNING, "hd_init_lcd2usb: unable to open device"); } else { /* read firmware version */ unsigned char buffer[2]; - if (usb_control_msg(lcd2usb, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, - LCD2USB_GET_FWVER, 0, 0, (char *)buffer, sizeof(buffer), 1000) == 2) + if (usb_control_msg(p->usbHandle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, + LCD2USB_GET_FWVER, 0, 0, (char *)buffer, sizeof(buffer), 1000) == 2) report(RPT_INFO, "hd_init_lcd2usb: device with firmware version %d.%02d found", buffer[0], buffer[1]); } } } } - if (lcd2usb != NULL) { + if (p->usbHandle != NULL) { debug(RPT_DEBUG, "hd_init_lcd2usb: opening device succeeded"); } else { @@ -116,7 +112,7 @@ lcd2usb_HD44780_senddata(PrivateData *p, unsigned char displayID, unsigned char int id = (displayID == 0) ? LCD2USB_CTRL_BOTH : ((displayID == 1) ? LCD2USB_CTRL_0 : LCD2USB_CTRL_1); - usb_control_msg(lcd2usb, USB_TYPE_VENDOR, (type | id), ch, 0, NULL, 0, 1000); + usb_control_msg(p->usbHandle, USB_TYPE_VENDOR, (type | id), ch, 0, NULL, 0, 1000); } @@ -132,7 +128,7 @@ lcd2usb_HD44780_backlight(PrivateData *p, unsigned char state) int promille = (state == BACKLIGHT_ON) ? p->brightness : p->offbrightness; // And set it (converted from [0,1000] -> [0,255]). - usb_control_msg(lcd2usb, USB_TYPE_VENDOR, LCD2USB_SET_BRIGHTNESS, + usb_control_msg(p->usbHandle, USB_TYPE_VENDOR, LCD2USB_SET_BRIGHTNESS, (promille * 255) / 1000, 0, NULL, 0, 1000); } @@ -153,7 +149,7 @@ lcd2usb_set_contrast(Driver *drvthis, int promille) // And set it (converted from [0,1000] -> [0,255]). // If successful, update the local value. - if (usb_control_msg(lcd2usb, USB_TYPE_VENDOR, LCD2USB_SET_CONTRAST, + if (usb_control_msg(p->usbHandle, USB_TYPE_VENDOR, LCD2USB_SET_CONTRAST, (promille * 255) / 1000, 0, NULL, 0, 1000) < 0) report(RPT_WARNING, "hd_init_lcd2usb: setting contrast failed"); else @@ -198,7 +194,7 @@ lcd2usb_HD44780_scankeypad(PrivateData *p) int nBytes; /* send control request and accept return value */ - nBytes = usb_control_msg(lcd2usb, + nBytes = usb_control_msg(p->usbHandle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, LCD2USB_GET_KEYS, 0, 0, (char *) buffer, sizeof(buffer), 1000); @@ -217,9 +213,9 @@ lcd2usb_HD44780_scankeypad(PrivateData *p) void lcd2usb_HD44780_close(PrivateData *p) { - if (lcd2usb != NULL) { - usb_close(lcd2usb); - lcd2usb = NULL; + if (p->usbHandle != NULL) { + usb_close(p->usbHandle); + p->usbHandle = NULL; } } diff --git a/server/drivers/hd44780-low.h b/server/drivers/hd44780-low.h index ca6a8f6..aee4284 100644 --- a/server/drivers/hd44780-low.h +++ b/server/drivers/hd44780-low.h @@ -10,6 +10,10 @@ # include "config.h" #endif +#if defined(HAVE_LIBUSB) +# include +#endif + # if TIME_WITH_SYS_TIME # include # include @@ -86,11 +90,18 @@ typedef struct ConnectionMapping { typedef struct driver_private_data { - unsigned int port; + // parallel connection typeS + unsigned int port; /* parallel port */ - /* for serial connection type */ - int fd; - int serial_type; + // serial connection types + int fd; /* file handle to serial device */ + int serial_type; + +#if defined(HAVE_LIBUSB) + // USB connection types + usb_dev_handle *usbHandle; /* USB device handle */ + int usbIndex; /* USB interface index */ +#endif int charmap; @@ -170,7 +181,7 @@ typedef struct driver_private_data { typedef struct hwDependentFns { // microsec pauses void (*uPause)(PrivateData *p, int usecs); - // report and debug helper + // report and debug helper: set by global hd44780 init void (*drv_report)(const int level, const char *format, .../*args*/); void (*drv_debug)(const int level, const char *format, .../*args*/);