Split headers/packet size fix
diff --git a/ChangeLog b/ChangeLog
index 30f4cbe..c9af4c9 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2007-01-01  Richard Low <richard@wentnet.com>
+
+	* src/libmtp.c: split headers/packet size fix
+	* src/ptp.c: ditto
+	* src/ptp.h: ditto
+	* src/libusb-glue.c: ditto
+
 2007-01-01  Orson Teodoro <orsonteodoro@yahoo.com>
 
 	* src/ptp.h: changed some WMDRMND def codes to AAVT def codes. 
diff --git a/src/libmtp.c b/src/libmtp.c
index 0eab8d4..00eae88 100644
--- a/src/libmtp.c
+++ b/src/libmtp.c
@@ -618,6 +618,7 @@
     printf("LIBMTP panic: could not open iconv() converters to/from UCS-2!\n");
     return NULL;
   }
+  params->split_header_data = 0;
 
   ptp_usb = (PTP_USB *) malloc(sizeof(PTP_USB));
   // Callbacks and stuff
@@ -2701,9 +2702,16 @@
     new_track.ObjectFormat = map_libmtp_type_to_ptp_type(metadata->filetype);
 		new_track.StorageID = store;
 		new_track.ParentObject = parenthandle;
-
+    
+    // get split headers correct
+    if (ptp_operation_issupported(params,PTP_OC_MTP_SendObjectPropList))
+      params->split_header_data = 1;
+    
     // Create the object
     ret = ptp_sendobjectinfo(params, &store, &localph, &metadata->item_id, &new_track);
+    
+    params->split_header_data = 0;
+    
     if (ret != PTP_RC_OK) {
       ptp_perror(params, ret);
       printf("LIBMTP_Send_Track_From_File_Descriptor: Could not send object info.\n");
@@ -2724,8 +2732,14 @@
   ptp_usb->current_transfer_complete = 0;
   ptp_usb->current_transfer_callback = callback;
   ptp_usb->current_transfer_callback_data = data;
+  
+  // get split headers correct
+  if (ptp_operation_issupported(params,PTP_OC_MTP_SendObjectPropList))
+    params->split_header_data = 1;
 
   ret = ptp_sendobject_fromfd(params, fd, metadata->filesize);
+  
+  params->split_header_data = 0;
 
   ptp_usb->callback_active = 0;
   ptp_usb->current_transfer_callback = NULL;
@@ -3086,6 +3100,10 @@
     }
   }
 
+  // get split headers correct
+  if (ptp_operation_issupported(params,PTP_OC_MTP_SendObjectPropList))
+    params->split_header_data = 1;
+    
   if (filedata->filesize != (uint64_t) -1) {
     // Callbacks
     ptp_usb->callback_active = 1;
@@ -3112,6 +3130,8 @@
       printf("Return code: 0x%04x (look this up in ptp.h for an explanation).\n",  ret);
     }
   }
+  
+  params->split_header_data = 0;
 
   if (ret != PTP_RC_OK) {
     ptp_perror(params, ret);
diff --git a/src/libusb-glue.c b/src/libusb-glue.c
index b77cddf..86debd6 100644
--- a/src/libusb-glue.c
+++ b/src/libusb-glue.c
@@ -399,7 +399,7 @@
 
 
 // Based on same function on library.c in libgphoto2
-#define CONTEXT_BLOCK_SIZE	0x100000
+#define CONTEXT_BLOCK_SIZE	0x10000
 static short
 ptp_read_func (
 	unsigned long size, PTPDataHandler *handler,void *data,
@@ -484,6 +484,9 @@
     towrite = size-curwrite;
     if (towrite > CONTEXT_BLOCK_SIZE)
       towrite = CONTEXT_BLOCK_SIZE;
+    else
+      if (towrite > ptp_usb->outep_maxpacket && towrite % ptp_usb->outep_maxpacket != 0)
+        towrite -= towrite % ptp_usb->outep_maxpacket;
     handler->getfunc (NULL, handler->private,towrite,bytes,&towrite);
     result = USB_BULK_WRITE(ptp_usb->handle,ptp_usb->outep,(char*)bytes,towrite,ptpcam_usb_timeout);
 #ifdef ENABLE_USB_BULK_DEBUG
@@ -521,6 +524,10 @@
   if (!ptp_usb->callback_active) {
     // Then terminate an even packet boundary write with a zero length packet
     if ((size % ptp_usb->outep_maxpacket) == 0) {
+#ifdef ENABLE_USB_BULK_DEBUG
+      printf("USB OUT==>\n");
+      printf("Zero Write\n");
+#endif
       result=USB_BULK_WRITE(ptp_usb->handle,ptp_usb->outep,(char *)"x",0,ptpcam_usb_timeout);
     }
   } else if (ptp_usb->current_transfer_complete == ptp_usb->current_transfer_total) {
@@ -528,6 +535,10 @@
     uint64_t actual_xfer_size = ptp_usb->current_transfer_total - 2*PTP_USB_BULK_HDR_LEN;
     
     if ((actual_xfer_size % ptp_usb->outep_maxpacket) == 0) {
+#ifdef ENABLE_USB_BULK_DEBUG
+      printf("USB OUT==>\n");
+      printf("Zero Write\n");
+#endif
       result=USB_BULK_WRITE(ptp_usb->handle,ptp_usb->outep,(char *)"x",0,ptpcam_usb_timeout);
     }
     // Set as complete and disable callback, just as good. 
diff --git a/src/ptp.c b/src/ptp.c
index 8684a67..a2ba6ee 100644
--- a/src/ptp.c
+++ b/src/ptp.c
@@ -143,9 +143,6 @@
 	return ret;
 }
 
-/* Used for file transactions */
-#define FILE_BUFFER_SIZE 0x10000
-
 uint16_t
 ptp_usb_senddata (PTPParams* params, PTPContainer* ptp,
 		  unsigned long size, PTPDataHandler *handler
@@ -313,10 +310,6 @@
 		/* Evaluate full data length. */
 		len=dtoh32(usbdata.length)-PTP_USB_BULK_HDR_LEN;
 
-		/* autodetect split header/data MTP devices */
-		if (dtoh32(usbdata.length) > 12 && (rlen==12))
-			params->split_header_data = 1;
-
 		data = malloc(PTP_USB_BULK_HS_MAX_PACKET_LEN);
 		/* Copy first part of data to 'data' */
 		handler->putfunc(
@@ -1297,6 +1290,7 @@
 	PTP_CNT_INIT(ptp);
 	ptp.Code=PTP_OC_SendObject;
 	ptp.Nparam=0;
+	
 	ret = ptp_transaction_new(params, &ptp, PTP_DP_SENDDATA, size, &handler);
 	ptp_exit_fd_handler (&handler);
 	return ret;
@@ -2851,6 +2845,9 @@
 	ptp.Param4 = (uint32_t) (objectsize >> 32);
 	ptp.Param5 = (uint32_t) (objectsize & 0xffffffffU);
 	ptp.Nparam = 5;
+  
+	// we always use a split header here
+	params->split_header_data = 1;
 
 	/* Set object handle to 0 for a new object */
 	oplsize = ptp_pack_OPL(params,proplist,&opldata);
@@ -2860,6 +2857,8 @@
 	*parenthandle = ptp.Param2;
 	*handle = ptp.Param3; 
 
+	params->split_header_data = 0;
+
 	return ret;
 }
 
diff --git a/src/ptp.h b/src/ptp.h
index 9253958..bc1f7cf 100644
--- a/src/ptp.h
+++ b/src/ptp.h
@@ -59,7 +59,8 @@
  */
 #define PTP_USB_BULK_HS_MAX_PACKET_LEN	4096
 #define PTP_USB_BULK_HDR_LEN		(2*sizeof(uint32_t)+2*sizeof(uint16_t))
-#define PTP_USB_BULK_PAYLOAD_LEN	(PTP_USB_BULK_HS_MAX_PACKET_LEN-PTP_USB_BULK_HDR_LEN)
+//#define PTP_USB_BULK_PAYLOAD_LEN	(PTP_USB_BULK_HS_MAX_PACKET_LEN-PTP_USB_BULK_HDR_LEN)
+#define PTP_USB_BULK_PAYLOAD_LEN 512-PTP_USB_BULK_HDR_LEN
 #define PTP_USB_BULK_REQ_LEN	(PTP_USB_BULK_HDR_LEN+5*sizeof(uint32_t))
 
 struct _PTPUSBBulkContainer {