2005-06-19 21:38:03 +00:00
|
|
|
/*
|
|
|
|
* avrdude - A Downloader/Uploader for AVR device programmers
|
2006-01-12 23:13:50 +00:00
|
|
|
* Copyright (C) 2005,2006 Joerg Wunsch
|
2006-01-12 23:24:35 +00:00
|
|
|
* Copyright (C) 2006 David Moore
|
2005-06-19 21:38:03 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2012-11-20 14:03:50 +00:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2005-06-19 21:38:03 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* USB interface via libusb for avrdude.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ac_cfg.h"
|
|
|
|
#if defined(HAVE_LIBUSB)
|
|
|
|
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
2012-01-20 09:39:56 +00:00
|
|
|
#if defined(HAVE_USB_H)
|
|
|
|
# include <usb.h>
|
|
|
|
#elif defined(HAVE_LUSB0_USB_H)
|
|
|
|
# include <lusb0_usb.h>
|
|
|
|
#else
|
|
|
|
# error "libusb needs either <usb.h> or <lusb0_usb.h>"
|
|
|
|
#endif
|
2005-06-19 21:38:03 +00:00
|
|
|
|
2007-01-24 21:07:54 +00:00
|
|
|
#include "avrdude.h"
|
2014-05-19 10:01:59 +00:00
|
|
|
#include "libavrdude.h"
|
|
|
|
|
2006-01-12 23:13:50 +00:00
|
|
|
#include "usbdevs.h"
|
2005-06-19 21:38:03 +00:00
|
|
|
|
2022-01-07 12:15:55 +00:00
|
|
|
#if defined(WIN32)
|
2008-03-29 22:10:11 +00:00
|
|
|
/* someone has defined "interface" to "struct" in Cygwin */
|
|
|
|
# undef interface
|
|
|
|
#endif
|
|
|
|
|
2012-11-28 22:39:01 +00:00
|
|
|
static char usbbuf[USBDEV_MAX_XFER_3];
|
2005-06-19 21:38:03 +00:00
|
|
|
static int buflen = -1, bufptr;
|
|
|
|
|
2005-09-26 12:19:01 +00:00
|
|
|
static int usb_interface;
|
|
|
|
|
2006-01-12 23:13:50 +00:00
|
|
|
/*
|
|
|
|
* The "baud" parameter is meaningless for USB devices, so we reuse it
|
|
|
|
* to pass the desired USB device ID.
|
|
|
|
*/
|
Use const in PROGRAMMER function arguments where appropriate
In order to get meaningful const properties for the PROGRAMMER, AVRPART and
AVRMEM arguments, some code needed to be moved around, otherwise a network of
"tainted" assignments risked rendering nothing const:
- Change void (*enable)(PROGRAMMER *pgm) to void (*enable)(PROGRAMMER *pgm,
const AVRPART *p); this allows changes in the PROGRAMMER structure after
the part is known. For example, use TPI, UPDI, PDI functions in that
programmer appropriate to the part. This used to be done later in the
process, eg, in the initialize() function, which "taints" all other
programmer functions wrt const and sometimes requires other finessing with
flags etc. Much clearer with the modified enable() interface.
- Move TPI initpgm-type code from initialize() to enable() --- note that
initpgm() does not have the info at the time when it is called whether or
not TPI is required
- buspirate.c: move pgm->flag to PDATA(pgm)->flag (so legitimate
modification of the flag does not change PROGRAMMER structure)
- Move AVRPART_INIT_SMC and AVRPART_WRITE bits from the flags field in
AVRPART to jtagmkII.c's private data flags32 fiels as FLAGS32_INIT_SMC and
FLAGS32_WRITE bits
- Move the xbeeResetPin component to private data in stk500.c as this is
needed by xbee when it saddles on the stk500 code (previously, the flags
component of the part was re-dedicated to this)
- Change the way the "chained" private data are used in jtag3.c whilst
keeping the PROGRAMMER structure read-only otherwise
- In stk500v2.c move the STK600 pgm update from stk500v2_initialize() to
stk500v2_enable() so the former keeps the PROGRAMMER structure read-only
(for const assertion).
- In usbasp change the code from changing PROGRAMMER functions late to
dispatching to TPI or regular SPI protocol functions at runtime; reason
being the decision whether to use TPI protocol is done at run-time
depending on the capability of the attached programmer
Also fixes Issue #1071, the treatment of default eecr value.
2022-08-17 15:05:28 +00:00
|
|
|
static int usbdev_open(const char *port, union pinfo pinfo, union filedescriptor *fd) {
|
2005-06-19 21:38:03 +00:00
|
|
|
char string[256];
|
2006-01-12 23:13:50 +00:00
|
|
|
char product[256];
|
2005-06-19 21:38:03 +00:00
|
|
|
struct usb_bus *bus;
|
|
|
|
struct usb_device *dev;
|
|
|
|
usb_dev_handle *udev;
|
|
|
|
char *serno, *cp2;
|
2008-03-14 13:00:08 +00:00
|
|
|
int i;
|
2014-02-26 17:54:32 +00:00
|
|
|
int iface;
|
2005-06-19 21:38:03 +00:00
|
|
|
size_t x;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The syntax for usb devices is defined as:
|
|
|
|
*
|
|
|
|
* -P usb[:serialnumber]
|
|
|
|
*
|
|
|
|
* See if we've got a serial number passed here. The serial number
|
|
|
|
* might contain colons which we remove below, and we compare it
|
|
|
|
* right-to-left, so only the least significant nibbles need to be
|
|
|
|
* specified.
|
|
|
|
*/
|
|
|
|
if ((serno = strchr(port, ':')) != NULL)
|
|
|
|
{
|
|
|
|
/* first, drop all colons there if any */
|
|
|
|
cp2 = ++serno;
|
|
|
|
|
|
|
|
while ((cp2 = strchr(cp2, ':')) != NULL)
|
|
|
|
{
|
|
|
|
x = strlen(cp2) - 1;
|
|
|
|
memmove(cp2, cp2 + 1, x);
|
|
|
|
cp2[x] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strlen(serno) > 12)
|
|
|
|
{
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_error("invalid serial number %s\n", serno);
|
2014-02-27 07:20:32 +00:00
|
|
|
return -1;
|
2005-06-19 21:38:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-26 16:24:56 +00:00
|
|
|
if (fd->usb.max_xfer == 0)
|
|
|
|
fd->usb.max_xfer = USBDEV_MAX_XFER_MKII;
|
|
|
|
|
2005-06-19 21:38:03 +00:00
|
|
|
usb_init();
|
|
|
|
|
|
|
|
usb_find_busses();
|
|
|
|
usb_find_devices();
|
|
|
|
|
2005-09-26 12:19:01 +00:00
|
|
|
for (bus = usb_get_busses(); bus; bus = bus->next)
|
2005-06-19 21:38:03 +00:00
|
|
|
{
|
|
|
|
for (dev = bus->devices; dev; dev = dev->next)
|
|
|
|
{
|
2014-02-21 13:44:11 +00:00
|
|
|
if (dev->descriptor.idVendor == pinfo.usbinfo.vid &&
|
|
|
|
dev->descriptor.idProduct == pinfo.usbinfo.pid)
|
2005-06-19 21:38:03 +00:00
|
|
|
{
|
2013-09-13 19:56:07 +00:00
|
|
|
udev = usb_open(dev);
|
|
|
|
if (udev)
|
2005-06-19 21:38:03 +00:00
|
|
|
{
|
|
|
|
/* yeah, we found something */
|
|
|
|
if (usb_get_string_simple(udev,
|
|
|
|
dev->descriptor.iSerialNumber,
|
|
|
|
string, sizeof(string)) < 0)
|
|
|
|
{
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_error("cannot read serial number: %s\n", usb_strerror());
|
2005-07-25 19:52:20 +00:00
|
|
|
/*
|
|
|
|
* On some systems, libusb appears to have
|
|
|
|
* problems sending control messages. Catch the
|
|
|
|
* benign case where the user did not request a
|
|
|
|
* particular serial number, so we could
|
|
|
|
* continue anyway.
|
|
|
|
*/
|
|
|
|
if (serno != NULL)
|
2014-02-27 07:20:32 +00:00
|
|
|
return -1; /* no chance */
|
2005-07-25 19:52:20 +00:00
|
|
|
else
|
|
|
|
strcpy(string, "[unknown]");
|
2005-06-19 21:38:03 +00:00
|
|
|
}
|
|
|
|
|
2006-01-12 23:13:50 +00:00
|
|
|
if (usb_get_string_simple(udev,
|
|
|
|
dev->descriptor.iProduct,
|
|
|
|
product, sizeof(product)) < 0)
|
|
|
|
{
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_error("cannot read product name: %s\n", usb_strerror());
|
2006-01-12 23:13:50 +00:00
|
|
|
strcpy(product, "[unnamed product]");
|
|
|
|
}
|
2014-02-26 17:54:32 +00:00
|
|
|
/*
|
|
|
|
* The CMSIS-DAP specification mandates the string
|
|
|
|
* "CMSIS-DAP" must be present somewhere in the
|
|
|
|
* product name string for a device compliant to
|
|
|
|
* that protocol. Use this for the decisision
|
|
|
|
* whether we have to search for a HID interface
|
|
|
|
* below.
|
|
|
|
*/
|
|
|
|
if(strstr(product, "CMSIS-DAP") != NULL)
|
|
|
|
{
|
|
|
|
pinfo.usbinfo.flags |= PINFO_FL_USEHID;
|
|
|
|
/* The JTAGICE3 running the CMSIS-DAP firmware doesn't
|
|
|
|
* use a separate endpoint for event reception. */
|
|
|
|
fd->usb.eep = 0;
|
|
|
|
}
|
2006-01-12 23:13:50 +00:00
|
|
|
|
2016-02-15 19:58:45 +00:00
|
|
|
if(strstr(product, "mEDBG") != NULL)
|
|
|
|
{
|
|
|
|
/* The AVR Xplained Mini uses different endpoints. */
|
|
|
|
fd->usb.rep = 0x81;
|
|
|
|
fd->usb.wep = 0x02;
|
|
|
|
}
|
|
|
|
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_notice("usbdev_open(): found %s, serno: %s\n", product, string);
|
2005-06-19 21:38:03 +00:00
|
|
|
if (serno != NULL)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* See if the serial number requested by the
|
|
|
|
* user matches what we found, matching
|
|
|
|
* right-to-left.
|
|
|
|
*/
|
|
|
|
x = strlen(string) - strlen(serno);
|
|
|
|
if (strcasecmp(string + x, serno) != 0)
|
|
|
|
{
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_debug("usbdev_open(): serial number does not match\n");
|
2005-06-19 21:38:03 +00:00
|
|
|
usb_close(udev);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-09-26 12:19:01 +00:00
|
|
|
if (dev->config == NULL)
|
|
|
|
{
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_warning("USB device has no configuration\n");
|
2005-09-26 12:19:01 +00:00
|
|
|
goto trynext;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (usb_set_configuration(udev, dev->config[0].bConfigurationValue))
|
|
|
|
{
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_warning("unable to set configuration %d: %s\n",
|
|
|
|
dev->config[0].bConfigurationValue, usb_strerror());
|
2014-02-26 17:54:32 +00:00
|
|
|
/* let's hope it has already been configured */
|
|
|
|
// goto trynext;
|
2005-09-26 12:19:01 +00:00
|
|
|
}
|
|
|
|
|
2014-02-26 17:54:32 +00:00
|
|
|
for (iface = 0; iface < dev->config[0].bNumInterfaces; iface++)
|
|
|
|
{
|
|
|
|
usb_interface = dev->config[0].interface[iface].altsetting[0].bInterfaceNumber;
|
|
|
|
#ifdef LIBUSB_HAS_GET_DRIVER_NP
|
|
|
|
/*
|
|
|
|
* Many Linux systems attach the usbhid driver
|
|
|
|
* by default to any HID-class device. On
|
|
|
|
* those, the driver needs to be detached before
|
|
|
|
* we can claim the interface.
|
|
|
|
*/
|
|
|
|
(void)usb_detach_kernel_driver_np(udev, usb_interface);
|
|
|
|
#endif
|
|
|
|
if (usb_claim_interface(udev, usb_interface))
|
|
|
|
{
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_error("unable to claim interface %d: %s\n",
|
|
|
|
usb_interface, usb_strerror());
|
2014-02-26 17:54:32 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (pinfo.usbinfo.flags & PINFO_FL_USEHID)
|
|
|
|
{
|
|
|
|
/* only consider an interface that is of class HID */
|
|
|
|
if (dev->config[0].interface[iface].altsetting[0].bInterfaceClass !=
|
|
|
|
USB_CLASS_HID)
|
|
|
|
continue;
|
|
|
|
fd->usb.use_interrupt_xfer = 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (iface == dev->config[0].bNumInterfaces)
|
2005-09-26 12:19:01 +00:00
|
|
|
{
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_warning("no usable interface found\n");
|
2005-09-26 12:19:01 +00:00
|
|
|
goto trynext;
|
|
|
|
}
|
|
|
|
|
2008-03-14 13:00:08 +00:00
|
|
|
fd->usb.handle = udev;
|
2012-11-26 16:24:56 +00:00
|
|
|
if (fd->usb.rep == 0)
|
2008-03-14 13:00:08 +00:00
|
|
|
{
|
2014-02-26 17:54:32 +00:00
|
|
|
/* Try finding out what our read endpoint is. */
|
|
|
|
for (i = 0; i < dev->config[0].interface[iface].altsetting[0].bNumEndpoints; i++)
|
|
|
|
{
|
|
|
|
int possible_ep = dev->config[0].interface[iface].altsetting[0].
|
|
|
|
endpoint[i].bEndpointAddress;
|
|
|
|
|
|
|
|
if ((possible_ep & USB_ENDPOINT_DIR_MASK) != 0)
|
|
|
|
{
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_notice2("usbdev_open(): using read endpoint 0x%02x\n", possible_ep);
|
2014-02-26 17:54:32 +00:00
|
|
|
fd->usb.rep = possible_ep;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (fd->usb.rep == 0)
|
2008-03-14 13:00:08 +00:00
|
|
|
{
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_error("cannot find a read endpoint, using 0x%02x\n",
|
|
|
|
USBDEV_BULK_EP_READ_MKII);
|
2014-02-26 17:54:32 +00:00
|
|
|
fd->usb.rep = USBDEV_BULK_EP_READ_MKII;
|
2008-03-14 13:00:08 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-26 17:54:32 +00:00
|
|
|
for (i = 0; i < dev->config[0].interface[iface].altsetting[0].bNumEndpoints; i++)
|
2008-03-14 13:00:08 +00:00
|
|
|
{
|
2014-02-26 17:54:32 +00:00
|
|
|
if ((dev->config[0].interface[iface].altsetting[0].endpoint[i].bEndpointAddress == fd->usb.rep ||
|
|
|
|
dev->config[0].interface[iface].altsetting[0].endpoint[i].bEndpointAddress == fd->usb.wep) &&
|
|
|
|
dev->config[0].interface[iface].altsetting[0].endpoint[i].wMaxPacketSize < fd->usb.max_xfer)
|
2013-01-02 10:16:37 +00:00
|
|
|
{
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_notice("max packet size expected %d, but found %d due to EP 0x%02x's wMaxPacketSize\n",
|
|
|
|
fd->usb.max_xfer,
|
|
|
|
dev->config[0].interface[iface].altsetting[0].endpoint[i].wMaxPacketSize,
|
|
|
|
dev->config[0].interface[iface].altsetting[0].endpoint[i].bEndpointAddress);
|
|
|
|
fd->usb.max_xfer = dev->config[0].interface[iface].altsetting[0].endpoint[i].wMaxPacketSize;
|
2013-01-02 10:16:37 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-26 17:54:32 +00:00
|
|
|
if (pinfo.usbinfo.flags & PINFO_FL_USEHID)
|
|
|
|
{
|
|
|
|
if (usb_control_msg(udev, 0x21, 0x0a /* SET_IDLE */, 0, 0, NULL, 0, 100) < 0)
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_error("SET_IDLE failed\n");
|
2014-02-26 17:54:32 +00:00
|
|
|
}
|
2013-09-13 19:56:07 +00:00
|
|
|
return 0;
|
|
|
|
trynext:
|
|
|
|
usb_close(udev);
|
2005-06-19 21:38:03 +00:00
|
|
|
}
|
2013-09-13 19:56:07 +00:00
|
|
|
else
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_error("cannot open device: %s\n", usb_strerror());
|
2005-06-19 21:38:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-13 20:07:40 +00:00
|
|
|
if ((pinfo.usbinfo.flags & PINFO_FL_SILENT) == 0)
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_notice("usbdev_open(): did not find any%s USB device \"%s\" (0x%04x:0x%04x)\n",
|
|
|
|
serno? " (matching)": "", port, (unsigned)pinfo.usbinfo.vid, (unsigned)pinfo.usbinfo.pid);
|
2014-02-27 07:20:32 +00:00
|
|
|
return -1;
|
2005-06-19 21:38:03 +00:00
|
|
|
}
|
|
|
|
|
2006-12-11 12:47:35 +00:00
|
|
|
static void usbdev_close(union filedescriptor *fd)
|
2005-06-19 21:38:03 +00:00
|
|
|
{
|
2008-03-14 13:00:08 +00:00
|
|
|
usb_dev_handle *udev = (usb_dev_handle *)fd->usb.handle;
|
2005-06-19 21:38:03 +00:00
|
|
|
|
2014-02-27 07:31:55 +00:00
|
|
|
if (udev == NULL)
|
2014-02-27 07:34:33 +00:00
|
|
|
return;
|
2014-02-27 07:31:55 +00:00
|
|
|
|
2005-09-26 12:19:01 +00:00
|
|
|
(void)usb_release_interface(udev, usb_interface);
|
2006-01-12 23:13:50 +00:00
|
|
|
|
2014-01-27 21:15:28 +00:00
|
|
|
#if defined(__linux__)
|
2006-01-12 23:13:50 +00:00
|
|
|
/*
|
|
|
|
* Without this reset, the AVRISP mkII seems to stall the second
|
2009-02-17 17:09:53 +00:00
|
|
|
* time we try to connect to it. This is not necessary on
|
|
|
|
* FreeBSD.
|
2006-01-12 23:13:50 +00:00
|
|
|
*/
|
|
|
|
usb_reset(udev);
|
2009-02-17 17:09:53 +00:00
|
|
|
#endif
|
2006-01-12 23:13:50 +00:00
|
|
|
|
2005-06-19 21:38:03 +00:00
|
|
|
usb_close(udev);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
Use const in PROGRAMMER function arguments where appropriate
In order to get meaningful const properties for the PROGRAMMER, AVRPART and
AVRMEM arguments, some code needed to be moved around, otherwise a network of
"tainted" assignments risked rendering nothing const:
- Change void (*enable)(PROGRAMMER *pgm) to void (*enable)(PROGRAMMER *pgm,
const AVRPART *p); this allows changes in the PROGRAMMER structure after
the part is known. For example, use TPI, UPDI, PDI functions in that
programmer appropriate to the part. This used to be done later in the
process, eg, in the initialize() function, which "taints" all other
programmer functions wrt const and sometimes requires other finessing with
flags etc. Much clearer with the modified enable() interface.
- Move TPI initpgm-type code from initialize() to enable() --- note that
initpgm() does not have the info at the time when it is called whether or
not TPI is required
- buspirate.c: move pgm->flag to PDATA(pgm)->flag (so legitimate
modification of the flag does not change PROGRAMMER structure)
- Move AVRPART_INIT_SMC and AVRPART_WRITE bits from the flags field in
AVRPART to jtagmkII.c's private data flags32 fiels as FLAGS32_INIT_SMC and
FLAGS32_WRITE bits
- Move the xbeeResetPin component to private data in stk500.c as this is
needed by xbee when it saddles on the stk500 code (previously, the flags
component of the part was re-dedicated to this)
- Change the way the "chained" private data are used in jtag3.c whilst
keeping the PROGRAMMER structure read-only otherwise
- In stk500v2.c move the STK600 pgm update from stk500v2_initialize() to
stk500v2_enable() so the former keeps the PROGRAMMER structure read-only
(for const assertion).
- In usbasp change the code from changing PROGRAMMER functions late to
dispatching to TPI or regular SPI protocol functions at runtime; reason
being the decision whether to use TPI protocol is done at run-time
depending on the capability of the attached programmer
Also fixes Issue #1071, the treatment of default eecr value.
2022-08-17 15:05:28 +00:00
|
|
|
static int usbdev_send(const union filedescriptor *fd, const unsigned char *bp, size_t mlen)
|
2005-06-19 21:38:03 +00:00
|
|
|
{
|
2008-03-14 13:00:08 +00:00
|
|
|
usb_dev_handle *udev = (usb_dev_handle *)fd->usb.handle;
|
2006-12-11 12:47:35 +00:00
|
|
|
int rv;
|
2006-01-12 23:13:50 +00:00
|
|
|
int i = mlen;
|
2014-07-16 20:02:01 +00:00
|
|
|
const unsigned char * p = bp;
|
2006-01-12 23:13:50 +00:00
|
|
|
int tx_size;
|
2005-06-19 21:38:03 +00:00
|
|
|
|
2014-02-27 07:31:55 +00:00
|
|
|
if (udev == NULL)
|
|
|
|
return -1;
|
|
|
|
|
2006-01-12 23:13:50 +00:00
|
|
|
/*
|
|
|
|
* Split the frame into multiple packets. It's important to make
|
|
|
|
* sure we finish with a short packet, or else the device won't know
|
|
|
|
* the frame is finished. For example, if we need to send 64 bytes,
|
|
|
|
* we must send a packet of length 64 followed by a packet of length
|
|
|
|
* 0.
|
|
|
|
*/
|
|
|
|
do {
|
2012-11-26 16:24:56 +00:00
|
|
|
tx_size = (mlen < fd->usb.max_xfer)? mlen: fd->usb.max_xfer;
|
2014-02-26 17:54:32 +00:00
|
|
|
if (fd->usb.use_interrupt_xfer)
|
|
|
|
rv = usb_interrupt_write(udev, fd->usb.wep, (char *)bp, tx_size, 10000);
|
|
|
|
else
|
|
|
|
rv = usb_bulk_write(udev, fd->usb.wep, (char *)bp, tx_size, 10000);
|
2006-01-12 23:13:50 +00:00
|
|
|
if (rv != tx_size)
|
|
|
|
{
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_error("wrote %d out of %d bytes, err = %s\n", rv, tx_size, usb_strerror());
|
2006-01-12 23:13:50 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
bp += tx_size;
|
|
|
|
mlen -= tx_size;
|
2016-02-15 19:58:45 +00:00
|
|
|
} while (mlen > 0);
|
2006-01-12 23:13:50 +00:00
|
|
|
|
|
|
|
if (verbose > 3)
|
2005-09-26 12:19:01 +00:00
|
|
|
{
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_trace("sent: ");
|
2006-01-12 23:13:50 +00:00
|
|
|
|
|
|
|
while (i) {
|
|
|
|
unsigned char c = *p;
|
|
|
|
if (isprint(c)) {
|
2022-10-17 14:44:55 +00:00
|
|
|
msg_trace("%c ", c);
|
2006-01-12 23:13:50 +00:00
|
|
|
}
|
|
|
|
else {
|
2022-10-17 14:44:55 +00:00
|
|
|
msg_trace(". ");
|
2006-01-12 23:13:50 +00:00
|
|
|
}
|
2022-10-17 14:44:55 +00:00
|
|
|
msg_trace("[%02x] ", c);
|
2006-01-12 23:13:50 +00:00
|
|
|
|
|
|
|
p++;
|
|
|
|
i--;
|
|
|
|
}
|
2022-10-17 14:44:55 +00:00
|
|
|
msg_trace("\n");
|
2005-09-26 12:19:01 +00:00
|
|
|
}
|
|
|
|
return 0;
|
2005-06-19 21:38:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* As calls to usb_bulk_read() result in exactly one USB request, we
|
|
|
|
* have to buffer the read results ourselves, so the single-char read
|
|
|
|
* requests performed by the upper layers will be handled. In order
|
|
|
|
* to do this, we maintain a private buffer of what we've got so far,
|
|
|
|
* and transparently issue another USB read request if the buffer is
|
|
|
|
* empty and more data are requested.
|
|
|
|
*/
|
|
|
|
static int
|
2014-02-26 17:54:32 +00:00
|
|
|
usb_fill_buf(usb_dev_handle *udev, int maxsize, int ep, int use_interrupt_xfer)
|
2005-06-19 21:38:03 +00:00
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
|
2014-02-26 17:54:32 +00:00
|
|
|
if (use_interrupt_xfer)
|
|
|
|
rv = usb_interrupt_read(udev, ep, usbbuf, maxsize, 10000);
|
|
|
|
else
|
|
|
|
rv = usb_bulk_read(udev, ep, usbbuf, maxsize, 10000);
|
2005-06-19 21:38:03 +00:00
|
|
|
if (rv < 0)
|
|
|
|
{
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_notice2("usb_fill_buf(): usb_%s_read() error: %s\n",
|
|
|
|
use_interrupt_xfer? "interrupt": "bulk", usb_strerror());
|
2005-06-19 21:38:03 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
buflen = rv;
|
|
|
|
bufptr = 0;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
Use const in PROGRAMMER function arguments where appropriate
In order to get meaningful const properties for the PROGRAMMER, AVRPART and
AVRMEM arguments, some code needed to be moved around, otherwise a network of
"tainted" assignments risked rendering nothing const:
- Change void (*enable)(PROGRAMMER *pgm) to void (*enable)(PROGRAMMER *pgm,
const AVRPART *p); this allows changes in the PROGRAMMER structure after
the part is known. For example, use TPI, UPDI, PDI functions in that
programmer appropriate to the part. This used to be done later in the
process, eg, in the initialize() function, which "taints" all other
programmer functions wrt const and sometimes requires other finessing with
flags etc. Much clearer with the modified enable() interface.
- Move TPI initpgm-type code from initialize() to enable() --- note that
initpgm() does not have the info at the time when it is called whether or
not TPI is required
- buspirate.c: move pgm->flag to PDATA(pgm)->flag (so legitimate
modification of the flag does not change PROGRAMMER structure)
- Move AVRPART_INIT_SMC and AVRPART_WRITE bits from the flags field in
AVRPART to jtagmkII.c's private data flags32 fiels as FLAGS32_INIT_SMC and
FLAGS32_WRITE bits
- Move the xbeeResetPin component to private data in stk500.c as this is
needed by xbee when it saddles on the stk500 code (previously, the flags
component of the part was re-dedicated to this)
- Change the way the "chained" private data are used in jtag3.c whilst
keeping the PROGRAMMER structure read-only otherwise
- In stk500v2.c move the STK600 pgm update from stk500v2_initialize() to
stk500v2_enable() so the former keeps the PROGRAMMER structure read-only
(for const assertion).
- In usbasp change the code from changing PROGRAMMER functions late to
dispatching to TPI or regular SPI protocol functions at runtime; reason
being the decision whether to use TPI protocol is done at run-time
depending on the capability of the attached programmer
Also fixes Issue #1071, the treatment of default eecr value.
2022-08-17 15:05:28 +00:00
|
|
|
static int usbdev_recv(const union filedescriptor *fd, unsigned char *buf, size_t nbytes)
|
2005-06-19 21:38:03 +00:00
|
|
|
{
|
2008-03-14 13:00:08 +00:00
|
|
|
usb_dev_handle *udev = (usb_dev_handle *)fd->usb.handle;
|
2005-06-19 21:38:03 +00:00
|
|
|
int i, amnt;
|
2005-08-30 04:44:29 +00:00
|
|
|
unsigned char * p = buf;
|
2005-06-19 21:38:03 +00:00
|
|
|
|
2014-02-27 07:31:55 +00:00
|
|
|
if (udev == NULL)
|
|
|
|
return -1;
|
|
|
|
|
2005-06-19 21:38:03 +00:00
|
|
|
for (i = 0; nbytes > 0;)
|
|
|
|
{
|
|
|
|
if (buflen <= bufptr)
|
|
|
|
{
|
2014-02-26 17:54:32 +00:00
|
|
|
if (usb_fill_buf(udev, fd->usb.max_xfer, fd->usb.rep, fd->usb.use_interrupt_xfer) < 0)
|
2005-06-19 21:38:03 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
amnt = buflen - bufptr > nbytes? nbytes: buflen - bufptr;
|
|
|
|
memcpy(buf + i, usbbuf + bufptr, amnt);
|
|
|
|
bufptr += amnt;
|
|
|
|
nbytes -= amnt;
|
|
|
|
i += amnt;
|
|
|
|
}
|
|
|
|
|
2009-10-10 20:09:59 +00:00
|
|
|
if (verbose > 4)
|
2005-06-19 21:38:03 +00:00
|
|
|
{
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_trace2("recv: ");
|
2005-06-19 21:38:03 +00:00
|
|
|
|
|
|
|
while (i) {
|
|
|
|
unsigned char c = *p;
|
|
|
|
if (isprint(c)) {
|
2022-10-17 14:44:55 +00:00
|
|
|
msg_trace2("%c ", c);
|
2005-06-19 21:38:03 +00:00
|
|
|
}
|
|
|
|
else {
|
2022-10-17 14:44:55 +00:00
|
|
|
msg_trace2(". ");
|
2005-06-19 21:38:03 +00:00
|
|
|
}
|
2022-10-17 14:44:55 +00:00
|
|
|
msg_trace2("[%02x] ", c);
|
2005-06-19 21:38:03 +00:00
|
|
|
|
|
|
|
p++;
|
|
|
|
i--;
|
|
|
|
}
|
2022-10-17 14:44:55 +00:00
|
|
|
msg_trace2("\n");
|
2005-06-19 21:38:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-01-12 23:13:50 +00:00
|
|
|
/*
|
|
|
|
* This version of recv keeps reading packets until we receive a short
|
|
|
|
* packet. Then, the entire frame is assembled and returned to the
|
|
|
|
* user. The length will be unknown in advance, so we return the
|
|
|
|
* length as the return value of this function, or -1 in case of an
|
|
|
|
* error.
|
|
|
|
*
|
|
|
|
* This is used for the AVRISP mkII device.
|
|
|
|
*/
|
Use const in PROGRAMMER function arguments where appropriate
In order to get meaningful const properties for the PROGRAMMER, AVRPART and
AVRMEM arguments, some code needed to be moved around, otherwise a network of
"tainted" assignments risked rendering nothing const:
- Change void (*enable)(PROGRAMMER *pgm) to void (*enable)(PROGRAMMER *pgm,
const AVRPART *p); this allows changes in the PROGRAMMER structure after
the part is known. For example, use TPI, UPDI, PDI functions in that
programmer appropriate to the part. This used to be done later in the
process, eg, in the initialize() function, which "taints" all other
programmer functions wrt const and sometimes requires other finessing with
flags etc. Much clearer with the modified enable() interface.
- Move TPI initpgm-type code from initialize() to enable() --- note that
initpgm() does not have the info at the time when it is called whether or
not TPI is required
- buspirate.c: move pgm->flag to PDATA(pgm)->flag (so legitimate
modification of the flag does not change PROGRAMMER structure)
- Move AVRPART_INIT_SMC and AVRPART_WRITE bits from the flags field in
AVRPART to jtagmkII.c's private data flags32 fiels as FLAGS32_INIT_SMC and
FLAGS32_WRITE bits
- Move the xbeeResetPin component to private data in stk500.c as this is
needed by xbee when it saddles on the stk500 code (previously, the flags
component of the part was re-dedicated to this)
- Change the way the "chained" private data are used in jtag3.c whilst
keeping the PROGRAMMER structure read-only otherwise
- In stk500v2.c move the STK600 pgm update from stk500v2_initialize() to
stk500v2_enable() so the former keeps the PROGRAMMER structure read-only
(for const assertion).
- In usbasp change the code from changing PROGRAMMER functions late to
dispatching to TPI or regular SPI protocol functions at runtime; reason
being the decision whether to use TPI protocol is done at run-time
depending on the capability of the attached programmer
Also fixes Issue #1071, the treatment of default eecr value.
2022-08-17 15:05:28 +00:00
|
|
|
static int usbdev_recv_frame(const union filedescriptor *fd, unsigned char *buf, size_t nbytes)
|
2006-01-12 23:13:50 +00:00
|
|
|
{
|
2008-03-14 13:00:08 +00:00
|
|
|
usb_dev_handle *udev = (usb_dev_handle *)fd->usb.handle;
|
2006-01-12 23:13:50 +00:00
|
|
|
int rv, n;
|
|
|
|
int i;
|
|
|
|
unsigned char * p = buf;
|
|
|
|
|
2014-02-27 07:31:55 +00:00
|
|
|
if (udev == NULL)
|
|
|
|
return -1;
|
|
|
|
|
2012-11-28 22:39:01 +00:00
|
|
|
/* If there's an event EP, and it has data pending, return it first. */
|
|
|
|
if (fd->usb.eep != 0)
|
|
|
|
{
|
|
|
|
rv = usb_bulk_read(udev, fd->usb.eep, usbbuf,
|
|
|
|
fd->usb.max_xfer, 1);
|
|
|
|
if (rv > 4)
|
|
|
|
{
|
2012-11-29 21:43:54 +00:00
|
|
|
memcpy(buf, usbbuf, rv);
|
|
|
|
n = rv;
|
|
|
|
n |= USB_RECV_FLAG_EVENT;
|
2012-11-28 22:39:01 +00:00
|
|
|
goto printout;
|
|
|
|
}
|
|
|
|
else if (rv > 0)
|
|
|
|
{
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_warning("short event len = %d, ignored\n", rv);
|
2012-11-29 21:43:54 +00:00
|
|
|
/* fallthrough */
|
2012-11-28 22:39:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-01-12 23:13:50 +00:00
|
|
|
n = 0;
|
|
|
|
do
|
|
|
|
{
|
2014-02-26 17:54:32 +00:00
|
|
|
if (fd->usb.use_interrupt_xfer)
|
|
|
|
rv = usb_interrupt_read(udev, fd->usb.rep, usbbuf,
|
|
|
|
fd->usb.max_xfer, 10000);
|
|
|
|
else
|
|
|
|
rv = usb_bulk_read(udev, fd->usb.rep, usbbuf,
|
|
|
|
fd->usb.max_xfer, 10000);
|
2006-01-12 23:13:50 +00:00
|
|
|
if (rv < 0)
|
|
|
|
{
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_notice2("usbdev_recv_frame(): usb_%s_read(): %s\n",
|
|
|
|
fd->usb.use_interrupt_xfer? "interrupt": "bulk", usb_strerror());
|
2006-01-12 23:13:50 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rv <= nbytes)
|
|
|
|
{
|
|
|
|
memcpy (buf, usbbuf, rv);
|
|
|
|
buf += rv;
|
|
|
|
}
|
2014-07-16 20:02:01 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
return -1; // buffer overflow
|
|
|
|
}
|
2006-01-12 23:13:50 +00:00
|
|
|
|
|
|
|
n += rv;
|
|
|
|
nbytes -= rv;
|
|
|
|
}
|
2014-02-22 20:30:16 +00:00
|
|
|
while (nbytes > 0 && rv == fd->usb.max_xfer);
|
2006-01-12 23:13:50 +00:00
|
|
|
|
2014-07-16 20:02:01 +00:00
|
|
|
/*
|
|
|
|
this ends when the buffer is completly filled (nbytes=0) or was too small (nbytes< 0)
|
|
|
|
or a short packet is found.
|
|
|
|
however we cannot say for nbytes=0 that there was really a packet completed,
|
|
|
|
we had to check the last rv value than for a short packet,
|
|
|
|
but what happens if the packet does not end with a short packet?
|
|
|
|
and what if the buffer is filled without the packet was completed?
|
|
|
|
|
|
|
|
preconditions:
|
|
|
|
expected packet is not a multiple of usb.max_xfer. (prevents further waiting)
|
|
|
|
|
|
|
|
expected packet is shorter than the provided buffer (so it cannot filled completely)
|
|
|
|
or buffer size is not a multiple of usb.max_xfer. (so it can clearly detected if the buffer was overflown.)
|
|
|
|
*/
|
2006-01-12 23:13:50 +00:00
|
|
|
|
2012-11-28 22:39:01 +00:00
|
|
|
printout:
|
2006-01-12 23:13:50 +00:00
|
|
|
if (verbose > 3)
|
|
|
|
{
|
2012-11-29 21:43:54 +00:00
|
|
|
i = n & USB_RECV_LENGTH_MASK;
|
2022-10-17 14:44:55 +00:00
|
|
|
pmsg_trace("recv: ");
|
2006-01-12 23:13:50 +00:00
|
|
|
|
|
|
|
while (i) {
|
|
|
|
unsigned char c = *p;
|
|
|
|
if (isprint(c)) {
|
2022-10-17 14:44:55 +00:00
|
|
|
msg_trace("%c ", c);
|
2006-01-12 23:13:50 +00:00
|
|
|
}
|
|
|
|
else {
|
2022-10-17 14:44:55 +00:00
|
|
|
msg_trace(". ");
|
2006-01-12 23:13:50 +00:00
|
|
|
}
|
2022-10-17 14:44:55 +00:00
|
|
|
msg_trace("[%02x] ", c);
|
2006-01-12 23:13:50 +00:00
|
|
|
|
|
|
|
p++;
|
|
|
|
i--;
|
|
|
|
}
|
2022-10-17 14:44:55 +00:00
|
|
|
msg_trace("\n");
|
2006-01-12 23:13:50 +00:00
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
2005-06-19 21:38:03 +00:00
|
|
|
|
Use const in PROGRAMMER function arguments where appropriate
In order to get meaningful const properties for the PROGRAMMER, AVRPART and
AVRMEM arguments, some code needed to be moved around, otherwise a network of
"tainted" assignments risked rendering nothing const:
- Change void (*enable)(PROGRAMMER *pgm) to void (*enable)(PROGRAMMER *pgm,
const AVRPART *p); this allows changes in the PROGRAMMER structure after
the part is known. For example, use TPI, UPDI, PDI functions in that
programmer appropriate to the part. This used to be done later in the
process, eg, in the initialize() function, which "taints" all other
programmer functions wrt const and sometimes requires other finessing with
flags etc. Much clearer with the modified enable() interface.
- Move TPI initpgm-type code from initialize() to enable() --- note that
initpgm() does not have the info at the time when it is called whether or
not TPI is required
- buspirate.c: move pgm->flag to PDATA(pgm)->flag (so legitimate
modification of the flag does not change PROGRAMMER structure)
- Move AVRPART_INIT_SMC and AVRPART_WRITE bits from the flags field in
AVRPART to jtagmkII.c's private data flags32 fiels as FLAGS32_INIT_SMC and
FLAGS32_WRITE bits
- Move the xbeeResetPin component to private data in stk500.c as this is
needed by xbee when it saddles on the stk500 code (previously, the flags
component of the part was re-dedicated to this)
- Change the way the "chained" private data are used in jtag3.c whilst
keeping the PROGRAMMER structure read-only otherwise
- In stk500v2.c move the STK600 pgm update from stk500v2_initialize() to
stk500v2_enable() so the former keeps the PROGRAMMER structure read-only
(for const assertion).
- In usbasp change the code from changing PROGRAMMER functions late to
dispatching to TPI or regular SPI protocol functions at runtime; reason
being the decision whether to use TPI protocol is done at run-time
depending on the capability of the attached programmer
Also fixes Issue #1071, the treatment of default eecr value.
2022-08-17 15:05:28 +00:00
|
|
|
static int usbdev_drain(const union filedescriptor *fd, int display)
|
2005-06-19 21:38:03 +00:00
|
|
|
{
|
2014-09-22 10:01:39 +00:00
|
|
|
/*
|
|
|
|
* There is not much point in trying to flush any data
|
|
|
|
* on an USB endpoint, as the endpoint is supposed to
|
|
|
|
* start afresh after being configured from the host.
|
|
|
|
*
|
|
|
|
* As trying to flush the data here caused strange effects
|
|
|
|
* in some situations (see
|
|
|
|
* https://savannah.nongnu.org/bugs/index.php?43268 )
|
|
|
|
* better avoid it.
|
|
|
|
*/
|
2005-09-16 15:01:52 +00:00
|
|
|
|
2005-06-19 21:38:03 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-01-12 23:13:50 +00:00
|
|
|
/*
|
|
|
|
* Device descriptor for the JTAG ICE mkII.
|
|
|
|
*/
|
2005-06-19 21:38:03 +00:00
|
|
|
struct serial_device usb_serdev =
|
|
|
|
{
|
|
|
|
.open = usbdev_open,
|
|
|
|
.close = usbdev_close,
|
|
|
|
.send = usbdev_send,
|
|
|
|
.recv = usbdev_recv,
|
|
|
|
.drain = usbdev_drain,
|
2006-10-27 08:45:47 +00:00
|
|
|
.flags = SERDEV_FL_NONE,
|
2005-06-19 21:38:03 +00:00
|
|
|
};
|
|
|
|
|
2006-01-12 23:13:50 +00:00
|
|
|
/*
|
|
|
|
* Device descriptor for the AVRISP mkII.
|
|
|
|
*/
|
|
|
|
struct serial_device usb_serdev_frame =
|
|
|
|
{
|
|
|
|
.open = usbdev_open,
|
|
|
|
.close = usbdev_close,
|
|
|
|
.send = usbdev_send,
|
|
|
|
.recv = usbdev_recv_frame,
|
|
|
|
.drain = usbdev_drain,
|
2006-10-27 08:45:47 +00:00
|
|
|
.flags = SERDEV_FL_NONE,
|
2006-01-12 23:13:50 +00:00
|
|
|
};
|
|
|
|
|
2005-06-19 21:38:03 +00:00
|
|
|
#endif /* HAVE_LIBUSB */
|