diff -ru avrdude-5.11.1/avrftdi.c avrdude-5.11.1.priv/avrftdi.c
--- avrdude-5.11.1/avrftdi.c	2011-09-15 08:37:04.000000000 -0600
+++ avrdude-5.11.1.priv/avrftdi.c	2011-10-15 22:03:49.000000000 -0600
@@ -1,6 +1,6 @@
 /*
  * avrftdi - extension for avrdude, Wolfgang Moser, Ville Voipio
- * Copyright (C) 2011 Hannes Weisbach, Doug Springer
+ * Copyright (C) 2011 Hannes Weisbach, Doug Springer gpib at rickyrockrat net
  *
  * 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
@@ -38,6 +38,32 @@
 #include "pgm.h"
 #include "avrftdi.h"
 
+#define SCK 0x01
+#define SDO 0x02
+#define SDI 0x04
+
+#define RX 0x20
+#define TX 0x11
+
+#define TRX (RX | TX)
+
+#define TYPE_C_D 0x500
+#define TYPE_H   0x700
+#define TYPE_4H  0x800
+
+#if ! defined(_PDATA)
+#define _PDATA  ((struct avrftdi_data *)(pgm->cookie))
+#else
+#error "_PDATA already defined! Rename _PDATA to something else in this file"
+#endif
+
+
+#define E(x) if ((x)) { fprintf(stdout, "%s:%d %s() %s: %s (%d)\n\t%s\n", __FILE__, __LINE__, __FUNCTION__, \
+	#x, strerror(errno), errno, ftdi_get_error_string(_PDATA->ftdic)); return -1; }
+
+#define E_VOID(x) if ((x)) { fprintf(stdout, "%s:%d %s() %s: %s (%d)\n\t%s\n", __FILE__, __LINE__, __FUNCTION__, \
+	#x, strerror(errno), errno, ftdi_get_error_string(_PDATA->ftdic)); }
+
 #ifdef HAVE_LIBUSB
 #ifdef HAVE_LIBFTDI
 
@@ -52,10 +78,15 @@
  */
 //#define DRYRUN
 
-static struct ftdi_context ftdic;
-static uint16_t pin_value, pin_direction, pin_inversion, led_mask;
-static int type; /**type is  bcdDevice. C/D is 0x500 H is 0x700 4H is 0x800*/
-static int ftype; /** is from FTDI. Use TYPE_2232C, TYPE_2232H, or TYPE_4232H*/
+struct avrftdi_data{
+  struct ftdi_context *ftdic;
+  uint16_t pin_value;
+	uint16_t pin_direction;
+	uint16_t pin_inversion;
+	uint16_t led_mask;
+  int type; /**type is  bcdDevice. C/D is 0x500 H is 0x700 4H is 0x800*/
+  int ftype; /** is from FTDI. Use TYPE_2232C, TYPE_2232H, or TYPE_4232H*/	
+};
 
 static void buf_dump(unsigned char *buf, int len, char *desc,
 		     int offset, int width)
@@ -73,7 +104,7 @@
 	fprintf(stderr, "%s end\n", desc);
 }
 
-static int set_frequency(uint32_t freq)
+static int set_frequency(PROGRAMMER *pgm, uint32_t freq)
 {
 	uint32_t divisor;
 	uint8_t buf[3];
@@ -108,7 +139,7 @@
 	buf[2] = (uint8_t)((divisor >> 8) & 0xff);
 
 #ifndef DRYRUN
-	E(ftdi_write_data(&ftdic, buf, 3) < 0);
+	E(ftdi_write_data( _PDATA->ftdic, buf, 3) < 0);
 #endif
 
 	return 0;
@@ -138,14 +169,14 @@
 	} else {
 		inversion_mask = 0;
 	}
-	if(TYPE_4232H == ftype)
+	if(TYPE_4232H == _PDATA->ftype)
 		mlim=7;
-	else if(TYPE_2232C==ftype)
+	else if(TYPE_2232C==_PDATA->ftype)
 		mlim=11;
-	else if(TYPE_2232H == ftype)
+	else if(TYPE_2232H == _PDATA->ftype)
 		mlim=15;
 	else{
-		printf("Unknown type %d (0x%x)\n",ftype,ftype);
+		printf("Unknown type %d (0x%x)\n",_PDATA->ftype,_PDATA->ftype);
 		mlim=15;
 	}
 	/* check that the pin number is in range */
@@ -160,20 +191,20 @@
 	}
 
 	/* create the mask and check that the pin is available */
-	if (pin_direction & (1 << (pin -1)) ) {
+	if (_PDATA->pin_direction & (1 << (pin -1)) ) {
 		fprintf(stderr,
 			"%s failure: pin %d has two definitions in config file\n",
 			progname, pin);
 		return -1;
 	} else {
-		pin_direction |= (1 << (pin - 1));
-		pin_inversion |= inversion_mask;
+		_PDATA->pin_direction |= (1 << (pin - 1));
+		_PDATA->pin_inversion |= inversion_mask;
 	}
 	if(PIN_LED_ERR == pinfunc ||
 	   PIN_LED_VFY == pinfunc ||
 	   PIN_LED_RDY == pinfunc ||
 	   PIN_LED_PGM == pinfunc) {
-		led_mask|=(1 << (pin - 1));
+		_PDATA->led_mask|=(1 << (pin - 1));
 	}
 
 	return 0;
@@ -198,15 +229,15 @@
 			"add_pins: %d: 0x%04x, inv=0x%04x\n",
 			pinfunc, pin, inversion_mask);
 	mask = pin;
-	if (TYPE_4232H == ftype)
+	if (TYPE_4232H == _PDATA->ftype)
 		mlim = 8;
-	else if (TYPE_2232C == ftype)
+	else if (TYPE_2232C == _PDATA->ftype)
 		mlim = 12;
-	else if (TYPE_2232H == ftype)
+	else if (TYPE_2232H == _PDATA->ftype)
 		mlim = 16;
 	else{
 		printf("Unknown type %d (0x%x)\n",
-		       ftype, ftype);
+		       _PDATA->ftype, _PDATA->ftype);
 		mlim = 16;
 	}
 	if (mask >= 1 << mlim) {
@@ -215,15 +246,15 @@
 			progname, mask, 1 << mlim);
 		mask &= ~(1 << mlim) - 1;
 	}
-	else if (mask & pin_direction) {
+	else if (mask & _PDATA->pin_direction) {
 		fprintf(stderr,
 			"%s failure: conflicting pins in pin list: ",
 			progname);
-		mask &= pin_direction;
+		mask &= _PDATA->pin_direction;
 	}
 	else {
-		pin_direction |= (uint16_t)mask;
-		pin_inversion |= inversion_mask;
+		_PDATA->pin_direction |= (uint16_t)mask;
+		_PDATA->pin_inversion |= inversion_mask;
 		return 0;
 	}
 
@@ -241,28 +272,30 @@
 }
 
 
-static int write_flush(void)
+static int write_flush(PROGRAMMER *pgm)
 {
 	unsigned char buf[6];
 	if(verbose > 2)
 		fprintf(stderr,
 			"%s info: direction: 0x%04x, value: 0x%04x, inversion: 0x%04x\n",
-			progname, pin_direction, pin_value, pin_inversion);
+			progname, _PDATA->pin_direction, _PDATA->pin_value, _PDATA->pin_inversion);
 
 	buf[0] = 0x80;
-	buf[1] = pin_value & 0xff;
-	buf[2] = pin_direction & 0xff;
+	buf[1] = _PDATA->pin_value & 0xff;
+	buf[2] = _PDATA->pin_direction & 0xff;
 	buf[3] = 0x82;
-	buf[4] = (pin_value >> 8) & 0xff;
-	buf[5] = (pin_direction >> 8) & 0xff;
-
+	buf[4] = (_PDATA->pin_value >> 8) & 0xff;
+	buf[5] = (_PDATA->pin_direction >> 8) & 0xff;
+  if (verbose > 3)
+  	fprintf(stderr,"FTDI LOG:wflush 6");
+  	
 #ifndef DRYRUN
-	E(ftdi_write_data(&ftdic, buf, 6) != 6);
+	E(ftdi_write_data(_PDATA->ftdic, buf, 6) != 6);
 
 #endif
 
 	if (verbose > 3)
-		printf("FTDI LOG: %02x %02x %02x %02x %02x %02x\n",
+		fprintf(stderr," %02x %02x %02x %02x %02x %02x\n",
 		       buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
 
 	/* we need to flush here, because set_pin is used as reset.
@@ -270,14 +303,16 @@
 	 * avr has got the reset signal when we start sleeping.
 	 * (it may be stuck in the USB stack or some USB hub)
 	 */
-	E(ftdi_usb_purge_buffers(&ftdic));
+	/* This hangs periodically, and erratically.
+	E(ftdi_usb_purge_buffers(_PDATA->ftdic)); 
+	*/
 
 	return 0;
 
 }
 
 /* this function sets or clears a GPIO pin */
-static int set_pin(int pin, int value)
+static int set_pin(PROGRAMMER *pgm, int pin, int value)
 {
 	int bit;
 	uint16_t tval;
@@ -290,7 +325,7 @@
 	}
 	--pin;
 	bit= 1 << (pin);
-	if (pin_inversion & bit) {
+	if (_PDATA->pin_inversion & bit) {
 		value = !value;
 	}
 	if (value)
@@ -301,11 +336,11 @@
 			"%s info: pin %04x bit %04x value 0x%04x\n",
 			progname, pin + 1, bit, value);
 	/* set bits depending on value */
-	/*pin_value ^= (-value ^ pin_value) & (1 << (pin - 1));  */
-	tval = (pin_value & (~bit)) | value;
-	if (tval != pin_value) {
-		pin_value = tval;
-		return write_flush();
+	/*_PDATA->pin_value ^= (-value ^ _PDATA->pin_value) & (1 << (pin - 1));  */
+	tval = (_PDATA->pin_value & (~bit)) | value;
+	if (tval != _PDATA->pin_value) {
+		_PDATA->pin_value = tval;
+		return write_flush(pgm);
 	} else if (verbose > 1)
 	    fprintf(stderr, "SameVal\n");
 	return 0;
@@ -313,7 +348,7 @@
 }
 
 /* this function sets or clears one or more GPIO pin these are bit-mapped */
-static int set_pins(int pin, int value)
+static int set_pins(PROGRAMMER *pgm, int pin, int value)
 {
 	if (0 == pin) {
 		if(verbose > 2)
@@ -321,7 +356,7 @@
 		return 1;
 	}
 	pin >>=1;
-	if (pin_inversion & pin) {
+	if (_PDATA->pin_inversion & pin) {
 		value = !value;
 	}
 	if (value)
@@ -332,9 +367,9 @@
 			"%s info: pin %04x value %d\n",
 			progname, pin, value);
 	/* set bits depending on value */
-	/*pin_value ^= (-value ^ pin_value) & (1 << (pin - 1));  */
-	pin_value = (pin_value & (~pin)) | value;
-	return write_flush();
+	/*_PDATA->pin_value ^= (-value ^ _PDATA->pin_value) & (1 << (pin - 1));  */
+	_PDATA->pin_value = (_PDATA->pin_value & (~pin)) | value;
+	return write_flush(pgm);
 }
 
 /* these functions are callbacks, which go into the
@@ -342,25 +377,25 @@
  */
 static int set_led_pgm(struct programmer_t * pgm, int value)
 {
-	return set_pin(pgm->pinno[PIN_LED_PGM], value);
+	return set_pin(pgm,pgm->pinno[PIN_LED_PGM], value);
 }
 
 static int set_led_rdy(struct programmer_t * pgm, int value)
 {
-	return set_pin(pgm->pinno[PIN_LED_RDY], value);
+	return set_pin(pgm, pgm->pinno[PIN_LED_RDY], value);
 }
 
 static int set_led_err(struct programmer_t * pgm, int value)
 {
-	return set_pin(pgm->pinno[PIN_LED_ERR], value);
+	return set_pin(pgm, pgm->pinno[PIN_LED_ERR], value);
 }
 
 static int set_led_vfy(struct programmer_t * pgm, int value)
 {
-	return set_pin(pgm->pinno[PIN_LED_VFY], value);
+	return set_pin(pgm, pgm->pinno[PIN_LED_VFY], value);
 }
 
-static int avrftdi_transmit(unsigned char mode, unsigned char *cmd,
+static int avrftdi_transmit(PROGRAMMER *pgm, unsigned char mode, unsigned char *cmd,
 			    unsigned char *data, int buf_size)
 {
 	int k = 0;
@@ -374,9 +409,11 @@
 
 		memcpy(buf + 3, cmd, buf_size);
 		buf[buf_size + 3] = 0x87;
-
+    if (verbose > 4)
+		fprintf(stderr,"FTDI LOG:transmit %02x %02x %02x sz %d \n",
+		       buf[0], buf[1], buf[2], buf_size+4);
 #ifndef DRYRUN
-		E(ftdi_write_data(&ftdic, buf, buf_size + 4) != buf_size + 4);
+		E(ftdi_write_data(_PDATA->ftdic, buf, buf_size + 4) != buf_size + 4);
 #endif
 	}
 
@@ -384,7 +421,7 @@
 		memset(buf, 0, sizeof(buf));
 		do {
 #ifndef DRYRUN
-			n = ftdi_read_data(&ftdic, buf + k, buf_size - k);
+			n = ftdi_read_data(_PDATA->ftdic, buf + k, buf_size - k);
 			E(n < 0);
 #else
 			n = buf_size - k;
@@ -407,7 +444,7 @@
 	struct usb_device *found_dev;
 	/* use vid/pid in following priority: config,
 	 * defaults. cmd-line is currently not supported */
-	type = 0;
+	_PDATA->type = 0;
 	snfound = 0;
 	foundsn = NULL;
 
@@ -435,12 +472,12 @@
 		interface = INTERFACE_A;
 	}
 #ifndef DRYRUN
-	E(ftdi_init(&ftdic) < 0);
+	E(ftdi_init(_PDATA->ftdic) < 0);
 	found_dev = NULL;
-	if (ftdi_usb_find_all(&ftdic, &devlist, vid, pid)) {
+	if (ftdi_usb_find_all(_PDATA->ftdic, &devlist, vid, pid)) {
 		devlist_ptr = devlist;
 		do {
-			ftdi_usb_get_strings(&ftdic, devlist_ptr->dev,
+			ftdi_usb_get_strings(_PDATA->ftdic, devlist_ptr->dev,
 					     NULL, 0, NULL, 0, serial, 255);
 
 			if (verbose)
@@ -454,11 +491,11 @@
 					foundsn = strdup(serial);
 					snfound = 1;
 					found_dev = devlist_ptr->dev;
-					type = devlist_ptr->dev->descriptor.bcdDevice;
+					_PDATA->type = devlist_ptr->dev->descriptor.bcdDevice;
 				}
 			}else {
-				if (0 == type)	/**we assume it will attach to first found.  */
-					type = devlist_ptr->dev->descriptor.bcdDevice;
+				if (0 == _PDATA->type)	/**we assume it will attach to first found.  */
+					_PDATA->type = devlist_ptr->dev->descriptor.bcdDevice;
 				if (NULL == found_dev)
 					found_dev = devlist_ptr->dev;
 				if (NULL == foundsn)
@@ -484,8 +521,8 @@
 	if (verbose) {
 		fprintf(stderr,
 			"%s: Using device VID:PID %04x:%04x type 0x%04x(",
-			progname, vid, pid, type);
-		switch (type) {
+			progname, vid, pid, _PDATA->type);
+		switch (_PDATA->type) {
 			case TYPE_C_D:
 			    fprintf(stderr,"C/D"); break;
 			case TYPE_H:
@@ -493,12 +530,12 @@
 			case TYPE_4H:
 			    fprintf(stderr,"4H"); break;
 			default:
-			    fprintf(stderr,"unknown %04x",type); break;
+			    fprintf(stderr,"unknown %04x",_PDATA->type); break;
 		}
 
 		fprintf(stderr,") and SN '%s'.\n", foundsn);
 	}
-	if (type == TYPE_C_D && INTERFACE_B == interface){
+	if (_PDATA->type == TYPE_C_D && INTERFACE_B == interface){
 		fprintf(stderr,
 			"%s: Type C/D found. Setting interface to A\n",
 			progname);
@@ -510,10 +547,10 @@
 			"%s: Using USB Interface %c\n",
 			progname, INTERFACE_A == interface? 'A': 'B');
 	free(foundsn);
-	E(ftdi_set_interface(&ftdic, interface) < 0);
-	E(ftdi_usb_open_dev(&ftdic,found_dev) <0);
-/*	E(ftdi_usb_open_desc(&ftdic, vid,pid,NULL,0==pgm->usbsn[0]?NULL:pgm->usbsn) < 0);  */
-	ftype=ftdic.type;
+	E(ftdi_set_interface(_PDATA->ftdic, interface) < 0);
+	E(ftdi_usb_open_dev(_PDATA->ftdic,found_dev) <0);
+/*	E(ftdi_usb_open_desc(_PDATA->ftdic, vid,pid,NULL,0==pgm->usbsn[0]?NULL:pgm->usbsn) < 0);  */
+	_PDATA->ftype=_PDATA->ftdic->type;
 #endif
 
 
@@ -545,10 +582,10 @@
 		pgm->pinno[PIN_AVR_RESET] = 4;
 	}
 	/**sync our internal state with the chip  */
-	pin_direction = 0;
-	pin_value = 0;
-	write_flush();
-	pin_direction = (0x3 | (1 << (pgm->pinno[PIN_AVR_RESET] - 1)));
+	_PDATA->pin_direction = 0;
+	_PDATA->pin_value = 0;
+	write_flush(pgm);
+	_PDATA->pin_direction = (0x3 | (1 << (pgm->pinno[PIN_AVR_RESET] - 1)));
 
 	/* gather the rest of the pins */
 	if (add_pins(pgm, PPI_AVR_VCC)) return -1;
@@ -558,19 +595,19 @@
 	if (add_pin(pgm, PIN_LED_PGM)) return -1;
 	if (add_pin(pgm, PIN_LED_VFY)) return -1;
 #ifndef DRYRUN
-	E(ftdi_set_bitmode(&ftdic, pin_direction & 0xff, BITMODE_MPSSE) < 0);	/*set SPI */
+	E(ftdi_set_bitmode(_PDATA->ftdic, _PDATA->pin_direction & 0xff, BITMODE_MPSSE) < 0);	/*set SPI */
 #endif
 	if (verbose > 1) {
-		fprintf(stderr, "pin direction mask: %04x\n", pin_direction);
-		fprintf(stderr, "pin value mask: %04x\n", pin_value);
+		fprintf(stderr, "pin direction mask: %04x\n", _PDATA->pin_direction);
+		fprintf(stderr, "pin value mask: %04x\n", _PDATA->pin_value);
 	}
 
 	if (pgm->baudrate) {
-		set_frequency(pgm->baudrate);
+		set_frequency(pgm,pgm->baudrate);
 	} else if(pgm->bitclock) {
-		set_frequency((uint32_t)(1.0f/pgm->bitclock));
+		set_frequency(pgm,(uint32_t)(1.0f/pgm->bitclock));
 	} else {
-		set_frequency(pgm->baudrate ? pgm->baudrate : 150000);
+		set_frequency(pgm,pgm->baudrate ? pgm->baudrate : 150000);
 	}
 	/**set the ready LED, if we have one .. and set our direction up */
 	set_led_rdy(pgm,0);
@@ -580,24 +617,24 @@
 
 static void avrftdi_close(PROGRAMMER * pgm)
 {
-	if(ftdic.usb_dev) {
-		set_pins(pgm->pinno[PPI_AVR_BUFF], ON);
-		set_pin(pgm->pinno[PIN_AVR_RESET], ON);
+	if(_PDATA->ftdic->usb_dev) {
+		set_pins(pgm,pgm->pinno[PPI_AVR_BUFF], ON);
+		set_pin(pgm,pgm->pinno[PIN_AVR_RESET], ON);
 		/**Stop driving the pins - except for the LEDs */
 		if (verbose > 1)
 			fprintf(stderr,
 				"LED Mask=0x%04x value =0x%04x &=0x%04x\n",
-				led_mask, pin_value, led_mask & pin_value);
-		pin_direction = led_mask;
-		pin_value &= led_mask;
-		write_flush();
+				_PDATA->led_mask, _PDATA->pin_value, _PDATA->led_mask & _PDATA->pin_value);
+		_PDATA->pin_direction = _PDATA->led_mask;
+		_PDATA->pin_value &= _PDATA->led_mask;
+		write_flush(pgm);
 #ifndef DRYRUN
-		E_VOID(ftdi_usb_close(&ftdic));
+		E_VOID(ftdi_usb_close(_PDATA->ftdic));
 #endif
 	}
 
 #ifndef DRYRUN
-	ftdi_deinit(&ftdic);
+	ftdi_deinit(_PDATA->ftdic);
 #endif
 
 	return;
@@ -606,19 +643,19 @@
 
 static int avrftdi_initialize(PROGRAMMER * pgm, AVRPART * p)
 {
-	set_pin(pgm->pinno[PIN_AVR_RESET], OFF);
-	set_pins(pgm->pinno[PPI_AVR_BUFF], OFF);
-	set_pin(pgm->pinno[PIN_AVR_SCK], OFF);
+	set_pin(pgm, pgm->pinno[PIN_AVR_RESET], OFF);
+	set_pins(pgm, pgm->pinno[PPI_AVR_BUFF], OFF);
+	set_pin(pgm, pgm->pinno[PIN_AVR_SCK], OFF);
 	/*use speed optimization with CAUTION*/
 	usleep(20 * 1000);
 
 	/* giving rst-pulse of at least 2 avr-clock-cycles, for
 	 * security (2us @ 1MHz) */
-	set_pin(pgm->pinno[PIN_AVR_RESET], ON);
+	set_pin(pgm, pgm->pinno[PIN_AVR_RESET], ON);
 	usleep(20 * 1000);
 
 	/*setting rst back to 0 */
-	set_pin(pgm->pinno[PIN_AVR_RESET], OFF);
+	set_pin(pgm, pgm->pinno[PIN_AVR_RESET], OFF);
 	/*wait at least 20ms bevor issuing spi commands to avr */
 	usleep(20 * 1000);
 
@@ -643,7 +680,7 @@
 
 static int avrftdi_cmd(PROGRAMMER * pgm, unsigned char cmd[4], unsigned char res[4])
 {
-	return avrftdi_transmit(TRX, cmd, res, sizeof(cmd));
+	return avrftdi_transmit(pgm, TRX, cmd, res, sizeof(cmd));
 }
 
 
@@ -667,9 +704,9 @@
 		pgm->cmd(pgm, buf, buf);
 		if (buf[p->pollindex-1] != p->pollvalue) {
 			//try resetting
-			set_pin(pgm->pinno[PIN_AVR_RESET], ON);
+			set_pin(pgm, pgm->pinno[PIN_AVR_RESET], ON);
 			usleep(20);
-			set_pin(pgm->pinno[PIN_AVR_RESET], OFF);
+			set_pin(pgm, pgm->pinno[PIN_AVR_RESET], OFF);
 			avr_set_bits(p->op[AVR_OP_PGM_ENABLE], buf);
 		} else
 			return 0;
@@ -718,7 +755,7 @@
 			 "load extended address command", 0, 16 * 3);
 
 #ifndef DRYRUN
-	E(ftdi_write_data(&ftdic, buf, sizeof(buf)) != sizeof(buf));
+	E(ftdi_write_data(_PDATA->ftdic, buf, sizeof(buf)) != sizeof(buf));
 #endif
 	return 0;
 }
@@ -737,7 +774,7 @@
 		avr_set_addr(m->op[AVR_OP_WRITE], cmd, add);
 		avr_set_input(m->op[AVR_OP_WRITE], cmd, *data++);
 
-		E(avrftdi_transmit(TX, cmd, cmd, 4) < 0);
+		E(avrftdi_transmit(pgm, TX, cmd, cmd, 4) < 0);
 
 		usleep((m->max_write_delay));
 		if(verbose < 3)
@@ -761,7 +798,7 @@
 		avr_set_bits(m->op[AVR_OP_READ], cmd);
 		avr_set_addr(m->op[AVR_OP_READ], cmd, add);
 
-		E(avrftdi_transmit(TRX, cmd, cmd, 4) < 0);
+		E(avrftdi_transmit(pgm, TRX, cmd, cmd, 4) < 0);
 
 		avr_get_output(m->op[AVR_OP_READ], cmd, bufptr++);
 		if(verbose < 3)
@@ -879,7 +916,7 @@
 				"%s info: buffer size: %d\n",
 				progname, buf_size);
 
-		E(avrftdi_transmit(TX, buf, buf, buf_size) < 0);
+		E(avrftdi_transmit(pgm,TX, buf, buf, buf_size) < 0);
 
 		bufptr = buf;
 		if (((address * 2) % m->page_size) == 0 || bytes == 0) {
@@ -960,7 +997,7 @@
 			buf_size = o_ptr - o_buf;
 
 			if((buf_size >= (page_size - 8)) || ( i == blocksize-1)) {
-				E(avrftdi_transmit(TRX, o_buf, i_buf, buf_size) < 0);
+				E(avrftdi_transmit(pgm,TRX, o_buf, i_buf, buf_size) < 0);
 
 				for(buf_index = 0; buf_index < buf_size; buf_index+=8) {
 					avr_get_output(m->op[AVR_OP_READ_LO], i_buf+buf_index, bufptr++);
@@ -1003,11 +1040,33 @@
 		return -2;
 }
 
+void avrftdi_setup(PROGRAMMER * pgm)
+{
+	struct avrftdi_data *p;
+  if ((p = calloc(1,sizeof(struct avrftdi_data))) == 0) {
+    fprintf(stderr,
+	    "%s: avrftdi_setup(): Out of memory allocating private data\n",
+	    progname);
+    exit(1);
+  }
+	if(NULL ==(p->ftdic=calloc(1,sizeof(struct ftdi_context)) ) ){
+		fprintf(stderr,
+	    "%s: avrftdi_setup(): Out of memory allocating private data context\n",
+	    progname);
+    exit(1);
+	}
+	pgm->cookie=p;
+}
+void avrftdi_teardown(PROGRAMMER * pgm)
+{
+	free(_PDATA->ftdic);
+	free(pgm->cookie);
+	pgm->cookie=NULL;
+}
 void avrftdi_initpgm(PROGRAMMER * pgm)
 {
 	strcpy(pgm->type, "avrftdi");
 
-	pin_value=pin_direction=pin_inversion=led_mask=0;
 	/*
 	 * mandatory functions
 	 */
@@ -1035,6 +1094,8 @@
 	pgm->err_led = set_led_err;
 	pgm->pgm_led = set_led_pgm;
 	pgm->vfy_led = set_led_vfy;
+	pgm->setup          = avrftdi_setup;
+  pgm->teardown       = avrftdi_teardown;
 
 
 }
diff -ru avrdude-5.11.1/avrftdi.h avrdude-5.11.1.priv/avrftdi.h
--- avrdude-5.11.1/avrftdi.h	2011-10-15 21:10:16.000000000 -0600
+++ avrdude-5.11.1.priv/avrftdi.h	2011-10-15 21:12:10.000000000 -0600
@@ -26,24 +26,7 @@
 extern "C" {
 #endif
 
-#define SCK 0x01
-#define SDO 0x02
-#define SDI 0x04
 
-#define RX 0x20
-#define TX 0x11
-
-#define TRX (RX | TX)
-
-#define TYPE_C_D 0x500
-#define TYPE_H   0x700
-#define TYPE_4H  0x800
-
-#define E(x) if ((x)) { fprintf(stdout, "%s:%d %s() %s: %s (%d)\n\t%s\n", __FILE__, __LINE__, __FUNCTION__, \
-	#x, strerror(errno), errno, ftdi_get_error_string(&ftdic)); return -1; }
-
-#define E_VOID(x) if ((x)) { fprintf(stdout, "%s:%d %s() %s: %s (%d)\n\t%s\n", __FILE__, __LINE__, __FUNCTION__, \
-	#x, strerror(errno), errno, ftdi_get_error_string(&ftdic)); }
 
 void avrftdi_initpgm        (PROGRAMMER * pgm);
 
