get rid of static variables: use the Driver structure's PrivateData

This commit is contained in:
marschap
2007-11-01 16:32:01 +00:00
parent 98ab97ecca
commit 01dcfd6f95
3 changed files with 55 additions and 52 deletions
+26 -30
View File
@@ -33,11 +33,6 @@
#endif #endif
/* USB device handle & interface index we write to */
static usb_dev_handle *bwct_usb;
static int bwct_usb_i;
/** /**
* Initialize the driver. * Initialize the driver.
* \param drvthis Pointer to driver structure. * \param drvthis Pointer to driver structure.
@@ -78,7 +73,8 @@ hd_init_bwct_usb(Driver *drvthis)
usb_find_busses(); usb_find_busses();
usb_find_devices(); usb_find_devices();
bwct_usb = NULL; p->usbHandle = NULL;
p->usbIndex = 0;
for (bus = usb_get_busses(); bus != NULL; bus = bus->next) { for (bus = usb_get_busses(); bus != NULL; bus = bus->next) {
struct usb_device *dev; struct usb_device *dev;
@@ -92,41 +88,41 @@ hd_init_bwct_usb(Driver *drvthis)
/* Loop through all of the configurations */ /* Loop through all of the configurations */
for (c = 0; c < dev->descriptor.bNumConfigurations; c++) { for (c = 0; c < dev->descriptor.bNumConfigurations; c++) {
/* Loop through all of the interfaces */ /* 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; int a;
/* Loop through all of the alternate settings */ /* 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 */ /* Check if this interface is a BWCT lcd */
if (((dev->config[c].interface[bwct_usb_i].altsetting[a].bInterfaceClass == 0xFF) && if (((dev->config[c].interface[p->usbIndex].altsetting[a].bInterfaceClass == 0xFF) &&
(dev->config[c].interface[bwct_usb_i].altsetting[a].bInterfaceSubClass == 0x01)) || (dev->config[c].interface[p->usbIndex].altsetting[a].bInterfaceSubClass == 0x01)) ||
(dev->descriptor.idProduct == BWCT_USB_PRODUCTID)) { (dev->descriptor.idProduct == BWCT_USB_PRODUCTID)) {
/* BWCT device found; try to find its description and serial number */ /* BWCT device found; try to find its description and serial number */
bwct_usb = usb_open(dev); p->usbHandle = usb_open(dev);
if (bwct_usb == NULL) { if (p->usbHandle == NULL) {
report(RPT_WARNING, "hd_init_bwct_usb: unable to open device"); report(RPT_WARNING, "hd_init_bwct_usb: unable to open device");
// return -1; /* it's better to continue */ // return -1; /* it's better to continue */
} }
else { else {
/* get device information & check for serial number */ /* 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, LCD_MAX_WIDTH) <= 0)
// *manufacturer = '\0'; // *manufacturer = '\0';
//manufacturer[sizeof(manufacturer)-1] = '\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, LCD_MAX_WIDTH) <= 0)
// *product = '\0'; // *product = '\0';
//product[sizeof(product)-1] = '\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, LCD_MAX_WIDTH) <= 0)
*device_serial = '\0'; *device_serial = '\0';
device_serial[sizeof(device_serial)-1] = '\0'; device_serial[sizeof(device_serial)-1] = '\0';
if ((*serial != '\0') && (*device_serial == '\0')) { if ((*serial != '\0') && (*device_serial == '\0')) {
report(RPT_ERR, "hd_init_bwct_usb: unable to get device's serial number"); report(RPT_ERR, "hd_init_bwct_usb: unable to get device's serial number");
usb_close(bwct_usb); usb_close(p->usbHandle);
return -1; return -1;
} }
@@ -134,8 +130,8 @@ hd_init_bwct_usb(Driver *drvthis)
if ((*serial == '\0') || (strcmp(serial, device_serial) == 0)) if ((*serial == '\0') || (strcmp(serial, device_serial) == 0))
goto done; goto done;
usb_close(bwct_usb); usb_close(p->usbHandle);
bwct_usb = NULL; p->usbHandle = NULL;
} }
} }
} }
@@ -145,19 +141,19 @@ hd_init_bwct_usb(Driver *drvthis)
} }
done: done:
if (bwct_usb != NULL) { if (p->usbHandle != NULL) {
debug(RPT_DEBUG, "hd_init_bwct_usb: opening device succeeded"); 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 defined(LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP)
if ((usb_detach_kernel_driver_np(bwct_usb, bwct_usb_i) < 0) || if ((usb_detach_kernel_driver_np(p->usbHandle, p->usbIndex) < 0) ||
(usb_claim_interface(bwct_usb, bwct_usb_i) < 0)) { (usb_claim_interface(p->usbHandle, p->usbIndex) < 0)) {
usb_close(bwct_usb); usb_close(p->usbHandle);
report(RPT_ERR, "hd_init_bwct_usb: unable to re-claim interface"); report(RPT_ERR, "hd_init_bwct_usb: unable to re-claim interface");
return -1; return -1;
} }
#else #else
usb_close(bwct_usb); usb_close(p->usbHandle);
report(RPT_ERR, "hd_init_bwct_usb: unable to claim interface"); report(RPT_ERR, "hd_init_bwct_usb: unable to claim interface");
return -1; return -1;
#endif #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; 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 void
bwct_usb_HD44780_close(PrivateData *p) bwct_usb_HD44780_close(PrivateData *p)
{ {
if (bwct_usb != NULL) { if (p->usbHandle != NULL) {
usb_close(bwct_usb); usb_close(p->usbHandle);
bwct_usb = NULL; p->usbHandle = NULL;
} }
} }
@@ -232,8 +228,8 @@ bwct_usb_set_contrast(Driver *drvthis, int promille)
// And set it (converted from [0,1000] -> [0,255]). // And set it (converted from [0,1000] -> [0,255]).
// If successful, update the local value. // If successful, update the local value.
if (usb_control_msg(bwct_usb, USB_TYPE_VENDOR, BWCT_LCD_SET_CONTRAST, if (usb_control_msg(p->usbHandle, USB_TYPE_VENDOR, BWCT_LCD_SET_CONTRAST,
(promille * 255) / 1000, bwct_usb_i, NULL, 0, 1000) < 0) (promille * 255) / 1000, p->usbIndex, NULL, 0, 1000) < 0)
report(RPT_WARNING, "hd_init_lcd2usb: setting contrast failed"); report(RPT_WARNING, "hd_init_lcd2usb: setting contrast failed");
else else
p->contrast = promille; p->contrast = promille;
+13 -17
View File
@@ -25,10 +25,6 @@
#endif #endif
/* USB device handle & interface index we write to */
static usb_dev_handle *lcd2usb;
/** /**
* Initialize the driver. * Initialize the driver.
* \param drvthis Pointer to driver structure. * \param drvthis Pointer to driver structure.
@@ -58,7 +54,7 @@ hd_init_lcd2usb(Driver *drvthis)
usb_find_busses(); usb_find_busses();
usb_find_devices(); usb_find_devices();
lcd2usb = NULL; p->usbHandle = NULL;
for (bus = usb_get_busses(); bus != NULL; bus = bus->next) { for (bus = usb_get_busses(); bus != NULL; bus = bus->next) {
struct usb_device *dev; struct usb_device *dev;
@@ -69,23 +65,23 @@ hd_init_lcd2usb(Driver *drvthis)
(dev->descriptor.idProduct == LCD2USB_PRODUCTID)) { (dev->descriptor.idProduct == LCD2USB_PRODUCTID)) {
/* LCD2USB device found; try to find its description */ /* LCD2USB device found; try to find its description */
lcd2usb = usb_open(dev); p->usbHandle = usb_open(dev);
if (lcd2usb == NULL) { if (p->usbHandle == NULL) {
report(RPT_WARNING, "hd_init_lcd2usb: unable to open device"); report(RPT_WARNING, "hd_init_lcd2usb: unable to open device");
} }
else { else {
/* read firmware version */ /* read firmware version */
unsigned char buffer[2]; unsigned char buffer[2];
if (usb_control_msg(lcd2usb, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, 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) 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]); 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"); debug(RPT_DEBUG, "hd_init_lcd2usb: opening device succeeded");
} }
else { else {
@@ -116,7 +112,7 @@ lcd2usb_HD44780_senddata(PrivateData *p, unsigned char displayID, unsigned char
int id = (displayID == 0) ? LCD2USB_CTRL_BOTH int id = (displayID == 0) ? LCD2USB_CTRL_BOTH
: ((displayID == 1) ? LCD2USB_CTRL_0 : LCD2USB_CTRL_1); : ((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; int promille = (state == BACKLIGHT_ON) ? p->brightness : p->offbrightness;
// And set it (converted from [0,1000] -> [0,255]). // 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); (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]). // And set it (converted from [0,1000] -> [0,255]).
// If successful, update the local value. // 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) (promille * 255) / 1000, 0, NULL, 0, 1000) < 0)
report(RPT_WARNING, "hd_init_lcd2usb: setting contrast failed"); report(RPT_WARNING, "hd_init_lcd2usb: setting contrast failed");
else else
@@ -198,7 +194,7 @@ lcd2usb_HD44780_scankeypad(PrivateData *p)
int nBytes; int nBytes;
/* send control request and accept return value */ /* 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, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
LCD2USB_GET_KEYS, 0, 0, (char *) buffer, sizeof(buffer), 1000); LCD2USB_GET_KEYS, 0, 0, (char *) buffer, sizeof(buffer), 1000);
@@ -217,9 +213,9 @@ lcd2usb_HD44780_scankeypad(PrivateData *p)
void void
lcd2usb_HD44780_close(PrivateData *p) lcd2usb_HD44780_close(PrivateData *p)
{ {
if (lcd2usb != NULL) { if (p->usbHandle != NULL) {
usb_close(lcd2usb); usb_close(p->usbHandle);
lcd2usb = NULL; p->usbHandle = NULL;
} }
} }
+16 -5
View File
@@ -10,6 +10,10 @@
# include "config.h" # include "config.h"
#endif #endif
#if defined(HAVE_LIBUSB)
# include <usb.h>
#endif
# if TIME_WITH_SYS_TIME # if TIME_WITH_SYS_TIME
# include <sys/time.h> # include <sys/time.h>
# include <time.h> # include <time.h>
@@ -86,11 +90,18 @@ typedef struct ConnectionMapping {
typedef struct driver_private_data { typedef struct driver_private_data {
unsigned int port; // parallel connection typeS
unsigned int port; /* parallel port */
/* for serial connection type */ // serial connection types
int fd; int fd; /* file handle to serial device */
int serial_type; 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; int charmap;
@@ -170,7 +181,7 @@ typedef struct driver_private_data {
typedef struct hwDependentFns { typedef struct hwDependentFns {
// microsec pauses // microsec pauses
void (*uPause)(PrivateData *p, int usecs); 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_report)(const int level, const char *format, .../*args*/);
void (*drv_debug)(const int level, const char *format, .../*args*/); void (*drv_debug)(const int level, const char *format, .../*args*/);