From dc1bacb7cbf8bd8b6b7375fb37c2ebe9ed99ebc2 Mon Sep 17 00:00:00 2001
From: Joerg Wunsch <j@uriah.heep.sax.de>
Date: Fri, 13 Sep 2013 19:56:07 +0000
Subject: [PATCH] Submitted by Stephen Roe: patch #7710: usb_libusb: Check
 VID/PID before opening device * usb_libusb.c (usbdev_open): Swap the sequence
 of verifying the VID:PID, and opening the device.

git-svn-id: svn://svn.savannah.nongnu.org/avrdude/trunk/avrdude@1228 81a1dc3b-b13d-400b-aceb-764788c761c2
---
 ChangeLog    |  7 +++++++
 NEWS         |  9 +++++----
 usb_libusb.c | 18 +++++++++++-------
 3 files changed, 23 insertions(+), 11 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 1bdf5da0..ac7a93a1 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2013-09-13  Joerg Wunsch <j.gnu@uriah.heep.sax.de>
+
+	Submitted by Stephen Roe:
+	patch #7710: usb_libusb: Check VID/PID before opening device
+	* usb_libusb.c (usbdev_open): Swap the sequence of verifying the
+	VID:PID, and opening the device.
+
 2013-09-13  Joerg Wunsch <j.gnu@uriah.heep.sax.de>
 
 	patch #8176: butterfly.c (AVR109 protocol implementation) clean-up and bug-fixing
diff --git a/NEWS b/NEWS
index 9cb179b3..c2808a1f 100644
--- a/NEWS
+++ b/NEWS
@@ -107,10 +107,11 @@ Current:
       - bug #28344: chip_erase_delay too short for ATmega324P, 644, 644P, and 1284P
       - bug #34277: avrdude reads wrong byte order if using avr911 (aka butterfly)
       - bug #35456: The progress bar for STK500V2 programmer is "wrong".
-      - patch #5708 avrdude should make 10 synchronization attempts instead of just one
-      - patch #7606 ATtiny43u support
-      - patch #7657 Add ATmega406 support for avrdude using DRAGON + JTAG
-      - bug #35474 Feature request: print fuse values in safemode output.
+      - patch #5708: avrdude should make 10 synchronization attempts instead of just one
+      - patch #7606: ATtiny43u support
+      - patch #7657: Add ATmega406 support for avrdude using DRAGON + JTAG
+      - bug #35474: Feature request: print fuse values in safemode output.
+      - patch #7710: usb_libusb: Check VID/PID before opening device
 
   * Keep track of input file contents
 
diff --git a/usb_libusb.c b/usb_libusb.c
index 22570fb2..8f3b0498 100644
--- a/usb_libusb.c
+++ b/usb_libusb.c
@@ -115,11 +115,11 @@ static int usbdev_open(char * port, long baud, union filedescriptor *fd)
     {
       for (dev = bus->devices; dev; dev = dev->next)
 	{
-	  udev = usb_open(dev);
-	  if (udev)
+	  if (dev->descriptor.idVendor == USB_VENDOR_ATMEL &&
+	      dev->descriptor.idProduct == (unsigned short)baud)
 	    {
-	      if (dev->descriptor.idVendor == USB_VENDOR_ATMEL &&
-		  dev->descriptor.idProduct == (unsigned short)baud)
+	      udev = usb_open(dev);
+	      if (udev)
 		{
 		  /* yeah, we found something */
 		  if (usb_get_string_simple(udev,
@@ -246,10 +246,14 @@ static int usbdev_open(char * port, long baud, union filedescriptor *fd)
 			  fd->usb.max_xfer = dev->config[0].interface[0].altsetting[0].endpoint[i].wMaxPacketSize;
 			}
 		    }
-                  return 0;
+		  return 0;
+		  trynext:
+		  usb_close(udev);
 		}
-	      trynext:
-	      usb_close(udev);
+	      else
+		fprintf(stderr,
+			"%s: usbdev_open(): cannot open device: %s\n",
+			progname, usb_strerror());
 	    }
 	}
     }