*** ardour-2.0beta9.orig/libs/surfaces/tranzport/tranzport_control_protocol.cc	2006-11-02 04:27:52.000000000 -0800
--- ardour-2.0beta9/libs/surfaces/tranzport/tranzport_control_protocol.cc	2006-12-02 17:08:21.000000000 -0800
***************
*** 18,23 ****
--- 18,26 ----
      $Id: tranzport_control_protocol.cc 1059 2006-11-02 12:27:49Z paul $
  */
  
+ #define DEFAULT_USB_TIMEOUT 5
+ #define MAX_RETRY 10
+ 
  #include <iostream>
  #include <algorithm>
  #include <cmath>
*************** int
*** 429,441 ****
  TranzportControlProtocol::write (uint8_t* cmd, uint32_t timeout_override)
  {
  	int val;
  
! 	val = usb_interrupt_write (udev, WRITE_ENDPOINT, (char*) cmd, 8, timeout_override ? timeout_override : timeout);
  
! 	if (val < 0)
  		return val;
! 	if (val != 8)
  		return -1;
  	return 0;
  
  }	
--- 432,454 ----
  TranzportControlProtocol::write (uint8_t* cmd, uint32_t timeout_override)
  {
  	int val;
+ 	int retry = 0;
  
! 	while((val = usb_interrupt_write (udev, WRITE_ENDPOINT, (char*) cmd, 8, timeout_override ? timeout_override : timeout))!=8 && retry++ < MAX_RETRY) {
! 		printf("usb_interrupt_write failed: %d\n", val);
! 	}
  
! 	if (retry == MAX_RETRY) {
! 		printf("Too many retries on a tranzport write, aborting\n");
! 		}
! 	if (val < 0) {
! 		printf("usb_interrupt_write failed: %d\n", val);
  		return val;
! 		}
! 	if (val != 8) {
! 		printf("usb_interrupt_write failed: %d\n", val);
  		return -1;
+ 		}
  	return 0;
  
  }	
*************** TranzportControlProtocol::lcd_clear ()
*** 457,463 ****
  
  	for (uint8_t i = 0; i < 10; ++i) {
  		cmd[2] = i;
! 		usb_interrupt_write (udev, WRITE_ENDPOINT, (char*) cmd, 8, 1000);
  	}
  	
  	memset (current_screen, ' ', sizeof (current_screen));
--- 470,476 ----
  
  	for (uint8_t i = 0; i < 10; ++i) {
  		cmd[2] = i;
! 		usb_interrupt_write (udev, WRITE_ENDPOINT, (char*) cmd, 8, DEFAULT_USB_TIMEOUT);
  	}
  	
  	memset (current_screen, ' ', sizeof (current_screen));
*************** TranzportControlProtocol::lights_off ()
*** 478,508 ****
  	cmd[7] = 0x00;
  
  	cmd[2] = LightRecord;
! 	if (write (cmd, 1000) == 0) {
  		lights[LightRecord] = false;
  	}
  	cmd[2] = LightTrackrec;
! 	if (write (cmd, 1000) == 0) {
  		lights[LightTrackrec] = false;
  	}
  	cmd[2] = LightTrackmute;
! 	if (write (cmd, 1000) == 0) {
  		lights[LightTrackmute] = false;
  	}
  	cmd[2] = LightTracksolo;
! 	if (write (cmd, 1000) == 0) {
  		lights[LightTracksolo] = false;
  	}
  	cmd[2] = LightAnysolo;
! 	if (write (cmd, 1000) == 0) {
  		lights[LightAnysolo] = false;
  	}
  	cmd[2] = LightLoop;
! 	if (write (cmd, 1000) == 0) {
  		lights[LightLoop] = false;
  	}
  	cmd[2] = LightPunch;
! 	if (write (cmd, 1000) == 0) {
  		lights[LightPunch] = false;
  	}
  }
--- 491,521 ----
  	cmd[7] = 0x00;
  
  	cmd[2] = LightRecord;
! 	if (write (cmd, DEFAULT_USB_TIMEOUT) == 0) {
  		lights[LightRecord] = false;
  	}
  	cmd[2] = LightTrackrec;
! 	if (write (cmd, DEFAULT_USB_TIMEOUT) == 0) {
  		lights[LightTrackrec] = false;
  	}
  	cmd[2] = LightTrackmute;
! 	if (write (cmd, DEFAULT_USB_TIMEOUT) == 0) {
  		lights[LightTrackmute] = false;
  	}
  	cmd[2] = LightTracksolo;
! 	if (write (cmd, DEFAULT_USB_TIMEOUT) == 0) {
  		lights[LightTracksolo] = false;
  	}
  	cmd[2] = LightAnysolo;
! 	if (write (cmd, DEFAULT_USB_TIMEOUT) == 0) {
  		lights[LightAnysolo] = false;
  	}
  	cmd[2] = LightLoop;
! 	if (write (cmd, DEFAULT_USB_TIMEOUT) == 0) {
  		lights[LightLoop] = false;
  	}
  	cmd[2] = LightPunch;
! 	if (write (cmd, DEFAULT_USB_TIMEOUT) == 0) {
  		lights[LightPunch] = false;
  	}
  }
*************** TranzportControlProtocol::light_on (Ligh
*** 523,529 ****
  		cmd[6] = 0x00;
  		cmd[7] = 0x00;
  
! 		if (write (cmd, 1000) == 0) {
  			lights[light] = true;
  			return 0;
  		} else {
--- 536,542 ----
  		cmd[6] = 0x00;
  		cmd[7] = 0x00;
  
! 		if (write (cmd, DEFAULT_USB_TIMEOUT) == 0) {
  			lights[light] = true;
  			return 0;
  		} else {
*************** TranzportControlProtocol::light_off (Lig
*** 551,557 ****
  		cmd[6] = 0x00;
  		cmd[7] = 0x00;
  
! 		if (write (cmd, 1000) == 0) {
  			lights[light] = false;
  			return 0;
  		} else {
--- 564,570 ----
  		cmd[6] = 0x00;
  		cmd[7] = 0x00;
  
! 		if (write (cmd, DEFAULT_USB_TIMEOUT) == 0) {
  			lights[light] = false;
  			return 0;
  		} else {
*************** TranzportControlProtocol::monitor_work (
*** 598,610 ****
  			first_time = true;
  		}
  
! 		pthread_testcancel();
  		val = usb_interrupt_read (udev, READ_ENDPOINT, (char*) buf, 8, 10);
  		pthread_testcancel();
  
  		if (val == 8) {
  			process (buf);
! 		}
  
  		if (_device_status != STATUS_OFFLINE) {
  			if (first_time) {
--- 611,624 ----
  			first_time = true;
  		}
  
! 		pthread_testcancel(); // I have no idea what pthread_testcancel really does
! 
  		val = usb_interrupt_read (udev, READ_ENDPOINT, (char*) buf, 8, 10);
  		pthread_testcancel();
  
  		if (val == 8) {
  			process (buf);
! 		} else if (val > 0) { printf("Partial usb read: %d\n", val); } 
  
  		if (_device_status != STATUS_OFFLINE) {
  			if (first_time) {
*************** TranzportControlProtocol::monitor_work (
*** 613,619 ****
--- 627,635 ----
  				first_time = false;
  			}
  			/* update whatever needs updating */
+ 	//		printf("In update_state\n");
  			update_state ();
+ 	//		printf("out of update_state\n");
  		}
  	}
  
*************** TranzportControlProtocol::update_state (
*** 627,632 ****
--- 643,650 ----
  	int col_base;
  	int col;
  	int cell;
+ 	int retry;
+ 	int val;
  
  	/* do the text updates */
  
*************** TranzportControlProtocol::update_state (
*** 664,674 ****
  				cmd[5] = pending_screen[row][col_base+2];
  				cmd[6] = pending_screen[row][col_base+3];
  				cmd[7] = 0x00;
! 
! 				if (usb_interrupt_write (udev, WRITE_ENDPOINT, (char *) cmd, 8, 1000) == 8) {
  					/* successful write: copy to current */
  					memcpy (&current_screen[row][col_base], &pending_screen[row][col_base], 4);
! 				}
  
  				/* skip the rest of the 4 character cell since we wrote+copied it already */
  				
--- 682,699 ----
  				cmd[5] = pending_screen[row][col_base+2];
  				cmd[6] = pending_screen[row][col_base+3];
  				cmd[7] = 0x00;
! 				retry=0;
! 				while (((val = usb_interrupt_write (udev, WRITE_ENDPOINT, (char *) cmd, 8, DEFAULT_USB_TIMEOUT)) != 8) && retry++ < MAX_RETRY)  {
! 					printf("usb screen update failed, val = %d, retrying\n", val); 
! 					printf("cmd and data were %02x %02x %02x %02x %02x %02x %02x %02x\n", cmd[0],cmd[1],cmd[2], cmd[3], cmd[4], cmd[5],cmd[6],cmd[7]); 
! 				} 
! 				if(retry == MAX_RETRY) {
! 					printf("usb screen update failed for some reason... why? cmd and data were %02x %02x %02x %02x %02x %02x %02x %02x\n", cmd[0],cmd[1],cmd[2], cmd[3], cmd[4], cmd[5],cmd[6],cmd[7]); 
! 					}
! // pretend it worked anyway for now
  					/* successful write: copy to current */
   				memcpy (&current_screen[row][col_base], &pending_screen[row][col_base], 4);
! 				retry = 0;
  
  				/* skip the rest of the 4 character cell since we wrote+copied it already */
  				
*************** TranzportControlProtocol::update_state (
*** 814,820 ****
  int
  TranzportControlProtocol::process (uint8_t* buf)
  {
! 	// printf("read: %02x %02x %02x %02x %02x %02x %02x %02x\n", buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7]);
  
  	uint32_t this_button_mask;
  	uint32_t button_changes;
--- 839,845 ----
  int
  TranzportControlProtocol::process (uint8_t* buf)
  {
! 	printf("read: %02x %02x %02x %02x %02x %02x %02x %02x\n", buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7]);
  
  	uint32_t this_button_mask;
  	uint32_t button_changes;
*************** TranzportControlProtocol::process (uint8
*** 831,836 ****
--- 856,862 ----
  	buttonmask = this_button_mask;
  
  	if (_datawheel) {
+ 	printf("datawheel: %02x\n", _datawheel);
  		datawheel ();
  	}
  
*************** TranzportControlProtocol::process (uint8
*** 863,868 ****
--- 889,895 ----
  		}
  	}
  	if (button_changes & ButtonTrackRec) {
+ 		printf("button trackrec: %d button record %d\n", button_changes, ButtonTrackRec);
  		if (buttonmask & ButtonTrackRec) {
  			button_event_trackrec_press (buttonmask&ButtonShift);
  		} else {
*************** TranzportControlProtocol::process (uint8
*** 968,973 ****
--- 995,1001 ----
  		}
  	}
  	if (button_changes & ButtonRecord) {
+ 		printf("button changes: %d button record %d\n", button_changes, ButtonRecord);
  		if (buttonmask & ButtonRecord) {
  			button_event_record_press (buttonmask&ButtonShift);
  		} else {
*************** TranzportControlProtocol::button_event_t
*** 1036,1041 ****
--- 1064,1070 ----
  	if (shifted) {
  		toggle_all_rec_enables ();
  	} else {
+ 	  printf("Rec enable called with: %d\n",shifted);
  		route_set_rec_enable (0, !route_get_rec_enable (0));
  	}
  }
