Browse Source

fpgautils: Implement bfg_claim_usb for claiming devices by USB bus number and address

Luke Dashjr 12 years ago
parent
commit
2b16acaa9b
2 changed files with 24 additions and 0 deletions
  1. 23 0
      fpgautils.c
  2. 1 0
      fpgautils.h

+ 23 - 0
fpgautils.c

@@ -404,6 +404,7 @@ int _serial_detect(struct device_drv *api, detectone_func_t detectone, autoscan_
 
 enum bfg_device_bus {
 	BDB_SERIAL,
+	BDB_USB,
 };
 
 // TODO: claim USB side of USB-Serial devices
@@ -411,6 +412,10 @@ typedef
 struct my_dev_t {
 	enum bfg_device_bus bus;
 	union {
+		struct {
+			uint8_t usbbus;
+			uint8_t usbaddr;
+		};
 #ifndef WIN32
 		dev_t dev;
 #else
@@ -476,6 +481,24 @@ struct device_drv *bfg_claim_serial(struct device_drv * const api, const bool ve
 	return bfg_claim_any(api, (verbose ? devpath : NULL), &dev);
 }
 
+struct device_drv *bfg_claim_usb(struct device_drv * const api, const bool verbose, const uint8_t usbbus, const uint8_t usbaddr)
+{
+	const my_dev_t dev = {
+		.bus = BDB_USB,
+		.usbbus = usbbus,
+		.usbaddr = usbaddr,
+	};
+	char *desc = NULL;
+	
+	if (verbose)
+	{
+		desc = alloca(3 + 1 + 3 + 1);
+		sprintf(desc, "%03u:%03u", (unsigned)usbbus, (unsigned)usbaddr);
+	}
+	
+	return bfg_claim_any(api, desc, &dev);
+}
+
 // This code is purely for debugging but is very useful for that
 // It also took quite a bit of effort so I left it in
 // #define TERMIOS_DEBUG 1

+ 1 - 0
fpgautils.h

@@ -31,6 +31,7 @@ extern int _serial_autodetect(detectone_func_t, ...);
 extern struct device_drv *bfg_claim_serial(struct device_drv * const, const bool verbose, const char * const devpath);
 #define serial_claim(devpath, drv)    bfg_claim_serial(drv, false, devpath)
 #define serial_claim_v(devpath, drv)  bfg_claim_serial(drv, true , devpath)
+extern struct device_drv *bfg_claim_usb(struct device_drv * const, const bool verbose, const uint8_t usbbus, const uint8_t usbaddr);
 
 extern int serial_open(const char *devpath, unsigned long baud, uint8_t timeout, bool purge);
 extern ssize_t _serial_read(int fd, char *buf, size_t buflen, char *eol);