--- io/hpiod/device.cpp.orig	2006-05-09 19:17:28.000000000 -0400
+++ io/hpiod/device.cpp	2006-05-08 02:50:09.000000000 -0400
@@ -99,16 +99,88 @@
 
 int Device::Write(int fd, const void *buf, int size)
 {
-   syslog(LOG_ERR, "error Write: unimplemented (osx) %s %s %d\n", URI, __FILE__, __LINE__);
+   int r, ep;
+  
+   if (FD[fd].pHD == NULL)
+   {
+      syslog(LOG_ERR, "invalid Device::Write state (osx): %s %s %d\n", URI,__FILE__, __LINE__);
    return -1;
 }
 
+   ep = GetOutEP(dev, FD[fd].Config, FD[fd].Interface, FD[fd].AltSetting, USB_ENDPOINT_TYPE_BULK);
+   
+   if (ep < 0)
+   {
+      syslog(LOG_ERR, "invalid bulk out endpoint (osx) %s %s %d\n", URI, __FILE__, __LINE__);
+      return -1;
+   }
+   
+   r = usb_bulk_write(FD[fd].pHD, ep, (char *)buf, size, LIBUSB_TIMEOUT);
+
+   if (r < 0)
+   {
+      syslog(LOG_ERR, "error Write (osx) %s: %s %d\n", URI, __FILE__, __LINE__);
+      return r;
+   }
+   
+   return r;   
+}
+
 int Device::Read(int fd, void *buf, int size, int usec)
 {
-   syslog(LOG_ERR, "error Read: unimplemented (osx) %s %s %d\n", URI, __FILE__, __LINE__);
+   int r, ep;
+   struct timeval t1, t2;
+   int total_usec, tmo_usec=usec;   
+
+   if (FD[fd].pHD == NULL)
+   {
+       syslog(LOG_ERR, "invalid Device::Read state (osx): %s %s %d\n", URI,__FILE__, __LINE__);
+       return -2;
+   }
+   
+   if (FD[fd].ucnt)
+      return CutBuf(fd, buf, size);   
+   
+   gettimeofday (&t1, NULL);     /* get start time */   
+   
+   ep = GetInEP(dev, FD[fd].Config, FD[fd].Interface, FD[fd].AltSetting, USB_ENDPOINT_TYPE_BULK);
+   
+   if (ep < 0)
+   {
+       syslog(LOG_ERR, "invalid bulk in endpoint (osx) %s %s %d\n", URI, __FILE__, __LINE__);
    return -2;
 }
 
+   while (1)
+   {
+       r = usb_bulk_read(FD[fd].pHD, ep, (char *)&FD[fd].ubuf, sizeof(FD[fd].ubuf), tmo_usec/1000);
+    
+       if (r < 0)
+       {
+          syslog(LOG_ERR, "error Read bulk (osx) %s: %s %d\n", URI, __FILE__, __LINE__);
+          return r;
+       }
+       
+       if ((FD[fd].ucnt = r) == 0)
+       {
+             gettimeofday(&t2, NULL);   /* get current time */
+    
+             total_usec = (t2.tv_sec - t1.tv_sec)*1000000;
+             total_usec += (t2.tv_usec > t1.tv_usec) ? t2.tv_usec - t1.tv_usec : t1.tv_usec - t2.tv_usec;
+             if (total_usec > usec)
+             {
+                syslog(LOG_ERR, "error Read bulk timedout (osx) %s: %s %d\n", URI, __FILE__, __LINE__);
+                return -ETIMEDOUT;   /* timeout */
+             }
+             
+             tmo_usec = usec - total_usec;    /* decrease timeout */
+             continue;
+       }
+      
+       return (CutBuf(fd, buf, size));
+   }
+}
+
 #else
 
 /*

