[sane-devel] patch for sanei_usb.c

gerard klaver gerard@gkall.hobby.nl
Tue, 07 Sep 2004 20:21:19 +0000


--=-/h28pJ5/9YoLVmckZHXc
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

Hello,

This patch for sanei_usb.c provides the following:

1. Detects endpoints for bAlternateSetting > 0 (for example webcams)
2. Some %d debug values for endpoints are changed to 0x%x
3. More debug info for control and isochronous mode


See the attached file for the patch.

Before adding the patch to the sanei_usb.c file in cvs i like to know
if there are any comments about it.

-- 
----------
m.vr.gr.
Gerard Klaver

--=-/h28pJ5/9YoLVmckZHXc
Content-Disposition: attachment; filename=sanei_usb-c.diff
Content-Type: text/x-patch; name=sanei_usb-c.diff; charset=ISO-8859-15
Content-Transfer-Encoding: 7bit

--- sanei_usb-orig.c	2004-09-06 18:35:04.000000000 +0200
+++ sanei_usb-upd.c	2004-09-07 20:11:49.000000000 +0200
@@ -88,6 +88,10 @@
   SANE_Int bulk_out_ep;
   SANE_Int int_in_ep;
   SANE_Int int_out_ep;
+  SANE_Int iso_in_ep;
+  SANE_Int iso_out_ep;
+  SANE_Int control_in_ep;
+  SANE_Int control_out_ep;
   SANE_Int interface_nr;
 #ifdef HAVE_LIBUSB
   usb_dev_handle *libusb_handle;
@@ -518,6 +522,8 @@
 {
   int devcount;
   SANE_Bool found = SANE_FALSE;
+  int alt_setting_nr;
+  int num_altsetting;
 
   DBG (5, "sanei_usb_open: trying to open device `%s'\n", devname);
   if (!dn)
@@ -636,8 +642,22 @@
 	  usb_close (devices[devcount].libusb_handle);
 	  return status;
 	}
-      interface = &dev->config[0].interface->altsetting[0];
+        /* looking for the max. bAlternateSetting value */
+      alt_setting_nr = 0;
+	 while  ( alt_setting_nr == (dev->config[0].interface[0].altsetting[alt_setting_nr].bAlternateSetting )) 
+	 {
+		 alt_setting_nr++;
+	    }
+
+	 num_altsetting = alt_setting_nr;
+	for (alt_setting_nr = 0;
+		   alt_setting_nr < num_altsetting;
+		   alt_setting_nr++)
+	{
 
+	DBG (5, "sanei_usb_open: alt_setting_nr: %d\n", alt_setting_nr);
+        interface = &dev->config[0].interface->altsetting[alt_setting_nr];
+       
       /* Now we look for usable endpoints */
       for (num = 0; num < interface->bNumEndpoints; num++)
 	{
@@ -645,41 +665,51 @@
 	  int address, direction, transfer_type;
 
 	  endpoint = &interface->endpoint[num];
+         DBG (5, "sanei_usb_open:num:: %d endpoint:: 0x%x\n", num, endpoint);
+	  transfer_type = endpoint->bmAttributes & USB_ENDPOINT_TYPE_MASK;
 	  address = endpoint->bEndpointAddress & USB_ENDPOINT_ADDRESS_MASK;
 	  direction = endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK;
+
+         DBG (5, "sanei_usb_open: direction :: %d)\n", direction);
 	  transfer_type = endpoint->bmAttributes & USB_ENDPOINT_TYPE_MASK;
 
+         DBG (5, "sanei_usb_open:  address: %d transfertype: %d\n",
+	         address, transfer_type);
+
+	  
 	  /* save the endpoints we need later */
 	  if (transfer_type == USB_ENDPOINT_TYPE_INTERRUPT)
 	  {
-	    DBG (5, "sanei_usb_open: found interupt-%s endpoint (address %d)\n",
+	    DBG (5, "sanei_usb_open: found interrupt-%s endpoint (address 0x%02x)\n",
 	         direction ? "in" : "out", address);
 	    if (direction)	/* in */
 	    {
 	      if (devices[devcount].int_in_ep)
 		DBG (3, "sanei_usb_open: we already have a int-in endpoint "
-		     "(address: %d), ignoring the new one\n",
+		     "(address: 0x%02x), ignoring the new one\n",
 		     devices[devcount].int_in_ep);
 	      else
 		devices[devcount].int_in_ep = endpoint->bEndpointAddress;
 	    }
 	    else
+	    {
 	      if (devices[devcount].int_out_ep)
 		DBG (3, "sanei_usb_open: we already have a int-out endpoint "
-		     "(address: %d), ignoring the new one\n",
+		     "(address: 0x%02x), ignoring the new one\n",
 		     devices[devcount].int_out_ep);
 	      else
 		devices[devcount].int_out_ep = endpoint->bEndpointAddress;
+	    }
 	  }
 	  else if (transfer_type == USB_ENDPOINT_TYPE_BULK)
 	  {
-	    DBG (5, "sanei_usb_open: found bulk-%s endpoint (address %d)\n",
+	    DBG (5, "sanei_usb_open: found bulk-%s endpoint (address 0x%02x)\n",
 	         direction ? "in" : "out", address);
 	    if (direction)	/* in */
 	      {
 		if (devices[devcount].bulk_in_ep)
 		  DBG (3, "sanei_usb_open: we already have a bulk-in endpoint "
-		       "(address: %d), ignoring the new one\n",
+		       "(address: 0x%02x), ignoring the new one\n",
 		       devices[devcount].bulk_in_ep);
 		else
 		  devices[devcount].bulk_in_ep = endpoint->bEndpointAddress;
@@ -688,23 +718,60 @@
 	      {
 	        if (devices[devcount].bulk_out_ep)
 		  DBG (3, "sanei_usb_open: we already have a bulk-out endpoint "
-		       "(address: %d), ignoring the new one\n",
+		       "(address: 0x%02x), ignoring the new one\n",
 		       devices[devcount].bulk_out_ep);
 	        else
 		  devices[devcount].bulk_out_ep = endpoint->bEndpointAddress;
 	      }
 	    }
-	  /* ignore currently unsupported endpoints */
-	  else {
-	      DBG (5, "sanei_usb_open: ignoring %s-%s endpoint "
-		   "(address: %d)\n",
-		   transfer_type == USB_ENDPOINT_TYPE_CONTROL ? "control" :
-		   transfer_type == USB_ENDPOINT_TYPE_ISOCHRONOUS
-		   ? "isochronous" : "interrupt",
-		   direction ? "in" : "out", address);
-	      continue;
+	  else if (transfer_type == USB_ENDPOINT_TYPE_ISOCHRONOUS)
+	  {
+	    DBG (5, "sanei_usb_open: found isochronous-%s endpoint (address 0x%02x)\n",
+	         direction ? "in" : "out", address);
+	    if (direction)	/* in */
+	      {
+		if (devices[devcount].iso_in_ep)
+		  DBG (3, "sanei_usb_open: we already have a isochronous-in endpoint "
+		       "(address: 0x%02x), ignoring the new one\n",
+		       devices[devcount].iso_in_ep);
+		else
+		  devices[devcount].iso_in_ep = endpoint->bEndpointAddress;
+	      }
+	    else
+	      {
+	        if (devices[devcount].iso_out_ep)
+		  DBG (3, "sanei_usb_open: we already have a isochronous-out endpoint "
+		       "(address: 0x%02x), ignoring the new one\n",
+		       devices[devcount].iso_out_ep);
+	        else
+		  devices[devcount].iso_out_ep = endpoint->bEndpointAddress;
+	      }
 	    }
-	}
+	  else if (transfer_type == USB_ENDPOINT_TYPE_CONTROL)
+	  {
+	    DBG (5, "sanei_usb_open: found control-%s endpoint (address 0x%02x)\n",
+	         direction ? "in" : "out", address);
+	    if (direction)	/* in */
+	      {
+		if (devices[devcount].control_in_ep)
+		  DBG (3, "sanei_usb_open: we already have a control-in endpoint "
+		       "(address: 0x%02x), ignoring the new one\n",
+		       devices[devcount].control_in_ep);
+		else
+		  devices[devcount].control_in_ep = endpoint->bEndpointAddress;
+	      }
+	    else
+	      {
+	        if (devices[devcount].control_out_ep)
+		  DBG (3, "sanei_usb_open: we already have a control-out endpoint "
+		       "(address: 0x%02x), ignoring the new one\n",
+		       devices[devcount].control_out_ep);
+	        else
+		  devices[devcount].control_out_ep = endpoint->bEndpointAddress;
+	      }
+	  }
+        }
+     }   
 #else /* not HAVE_LIBUSB */
       DBG (1, "sanei_usb_open: can't open device `%s': "
 	   "libusb support missing\n", devname);
@@ -782,9 +849,11 @@
       /* Should only be done in case of a stall */
       usb_clear_halt (devices[dn].libusb_handle, devices[dn].bulk_in_ep);
       usb_clear_halt (devices[dn].libusb_handle, devices[dn].bulk_out_ep);
+      usb_clear_halt (devices[dn].libusb_handle, devices[dn].iso_in_ep);  
       /* be careful, we don't know if we are in DATA0 stage now */
       usb_resetep(devices[dn].libusb_handle, devices[dn].bulk_in_ep);
       usb_resetep(devices[dn].libusb_handle, devices[dn].bulk_out_ep);
+      usb_resetep(devices[dn].libusb_handle, devices[dn].iso_in_ep);  
 #endif
       usb_release_interface (devices[dn].libusb_handle, 
 			     devices[dn].interface_nr);
@@ -944,7 +1013,7 @@
 {
   if (dn >= MAX_DEVICES || dn < 0)
     {
-      DBG (1, "sanei_usb_control_msg: dn >= MAX_DEVICES || dn < 0\n");
+      DBG (1, "sanei_usb_control_msg: dn >= MAX_DEVICES || dn < 0, dn=%d\n", dn);
       return SANE_STATUS_INVAL;
     }
 
@@ -1016,6 +1085,7 @@
 {
   ssize_t read_size = 0;
 
+  DBG (1, "sanei_usb_read_int: enter\n");
   if (!size)
     {
       DBG (1, "sanei_usb_read_int: size == NULL\n");
@@ -1080,5 +1150,7 @@
   *size = read_size;
   if (debug_level > 10)
     print_buffer (buffer, read_size);
+
+  DBG (1, "sanei_usb_read_int: exit\n");
   return SANE_STATUS_GOOD;
 }

--=-/h28pJ5/9YoLVmckZHXc--