Re: [PATCH] Add support for dumping descriptors to usbdevs(8)

2015-04-16 Thread Martin Pieuchot
On 16/04/15(Thu) 10:02, Dimitris Papastamos wrote:
 ping

A bit too soon to ping ;)  You could wait at least a week!



Re: [PATCH] Add support for dumping descriptors to usbdevs(8)

2015-04-16 Thread Dimitris Papastamos
On Thu, Apr 16, 2015 at 12:06:49PM +0200, Martin Pieuchot wrote:
 On 13/04/15(Mon) 17:49, Dimitris Papastamos wrote:
  Hi,
  
  Attached below an updated patch that makes the layout a bit more sensible.
 
 It's nice it works. 
 
 Now like various students that applied for the GSoC you've done the
 first step.  The interesting part of that GSoC is also the boring part:
 the parser.  How are you gonna deal with AUDIO, VIDEO and HID descriptors?
 
 I'm asking the question but I'm not really interested in the answer.  I
 don't see the point of having multiple people working on the same task.
 But if a student is selected for this task, you might want to discuss
 that with him.

Sure, that makes sense.

 Honestly if you are looking for an easy task to contribute to the USB
 land in OpenBSD you could have asked before, there's a lot to do :)

I should have I guess.  The gsoc page was tempting even if I am not a
student.

 So here's a small task, just for you:  can you add an option to
 usbdev(8) to enable/disable USB bus probing?   By default it would be
 on, just like know: when you plug a device is it detected/attached.
 
 If you turn it off to new device will be attached and port events
 will simply be acknowledged?

Will have a look at that.



Re: [PATCH] Add support for dumping descriptors to usbdevs(8)

2015-04-16 Thread Dimitris Papastamos
ping



Re: [PATCH] Add support for dumping descriptors to usbdevs(8)

2015-04-16 Thread Martin Pieuchot
On 13/04/15(Mon) 17:49, Dimitris Papastamos wrote:
 Hi,
 
 Attached below an updated patch that makes the layout a bit more sensible.

It's nice it works. 

Now like various students that applied for the GSoC you've done the
first step.  The interesting part of that GSoC is also the boring part:
the parser.  How are you gonna deal with AUDIO, VIDEO and HID descriptors?

I'm asking the question but I'm not really interested in the answer.  I
don't see the point of having multiple people working on the same task.
But if a student is selected for this task, you might want to discuss
that with him.

Honestly if you are looking for an easy task to contribute to the USB
land in OpenBSD you could have asked before, there's a lot to do :)

So here's a small task, just for you:  can you add an option to
usbdev(8) to enable/disable USB bus probing?   By default it would be
on, just like know: when you plug a device is it detected/attached.

If you turn it off to new device will be attached and port events
will simply be acknowledged?



[PATCH] Add support for dumping descriptors to usbdevs(8)

2015-04-13 Thread Dimitris Papastamos
Hi everyone,

This is a preliminary patch that adds descriptor dumping support
to usbdevs(8).  I have attempted to keep any unnecessary changes to
a minimum.  I will do some cleanup work incrementally on top of
this patch.

I've tested this on a single machine running -current so more testing
would be nice.

Let me know what you think and I will rework my patch.

Cheers,
Dimitris

Index: usr.sbin/usbdevs/usbdevs.8
===
RCS file: /cvs/src/usr.sbin/usbdevs/usbdevs.8,v
retrieving revision 1.9
diff -u -p -r1.9 usbdevs.8
--- usr.sbin/usbdevs/usbdevs.8  26 Jun 2008 05:42:21 -  1.9
+++ usr.sbin/usbdevs/usbdevs.8  13 Apr 2015 13:57:27 -
@@ -54,7 +54,13 @@ Show the device drivers associated with 
 .It Fl f Ar dev
 Only print information for the given USB controller.
 .It Fl v
-Be verbose.
+Verbose mode.  Causes
+.Nm
+to display detailed information about the devices present.
+Multiple
+.Fl v
+options increase verbosity.  At level 1 or above the device
+and configuration descriptors will be shown.
 .El
 .Sh FILES
 .Bl -tag -width Pa
Index: usr.sbin/usbdevs/usbdevs.c
===
RCS file: /cvs/src/usr.sbin/usbdevs/usbdevs.c,v
retrieving revision 1.24
diff -u -p -r1.24 usbdevs.c
--- usr.sbin/usbdevs/usbdevs.c  31 Mar 2015 13:38:27 -  1.24
+++ usr.sbin/usbdevs/usbdevs.c  13 Apr 2015 13:57:27 -
@@ -50,6 +50,14 @@ int verbose = 0;
 int showdevs = 0;
 
 void usage(void);
+int getddesc(int, int, int, struct usb_device_ddesc *);
+int getcdesc(int, int, int, struct usb_device_cdesc *);
+int getfdesc(int, int, int, int, struct usb_device_fdesc *);
+void dumpedesc(usb_endpoint_descriptor_t *);
+void dumpidesc(usb_interface_descriptor_t *);
+void dumpcdesc(usb_config_descriptor_t *);
+void dumpddesc(usb_device_descriptor_t *);
+void dumpdesc(int, int, int);
 void usbdev(int f, int a, int rec);
 void usbdump(int f);
 void dumpone(char *name, int f, int addr);
@@ -67,6 +75,236 @@ usage(void)
 char done[USB_MAX_DEVICES];
 int indent;
 
+int
+getddesc(int f, int b, int a, struct usb_device_ddesc *ddesc)
+{
+   int r;
+
+   ddesc-udd_bus = b;
+   ddesc-udd_addr = a;
+   r = ioctl(f, USB_DEVICE_GET_DDESC, ddesc);
+   if (r) {
+   if (errno != ENXIO)
+   warn(USB_DEVICE_GET_DDESC);
+   return -1;
+   }
+   return 0;
+}
+
+int
+getcdesc(int f, int b, int a, struct usb_device_cdesc *cdesc)
+{
+   int r;
+
+   cdesc-udc_bus = b;
+   cdesc-udc_addr = a;
+   cdesc-udc_config_index = USB_CURRENT_CONFIG_INDEX;
+   r = ioctl(f, USB_DEVICE_GET_CDESC, cdesc);
+   if (r) {
+   if (errno != ENXIO)
+   warn(USB_DEVICE_GET_CDESC);
+   return -1;
+   }
+   return 0;
+}
+
+int
+getfdesc(int f, int b, int a, int l, struct usb_device_fdesc *fdesc)
+{
+   int r;
+
+   fdesc-udf_bus = b;
+   fdesc-udf_addr = a;
+   fdesc-udf_config_index = USB_CURRENT_CONFIG_INDEX;
+   fdesc-udf_size = l;
+   fdesc-udf_data = malloc(l);
+   if (!fdesc-udf_data)
+   err(1, malloc);
+   r = ioctl(f, USB_DEVICE_GET_FDESC, fdesc);
+   if (r) {
+   if (errno != ENXIO)
+   warn(USB_DEVICE_GET_FDESC);
+   free(fdesc-udf_data);
+   return -1;
+   }
+   return 0;
+}
+
+void
+dumpedesc(usb_endpoint_descriptor_t *ed)
+{
+   char *xfertype, *synchtype;
+
+   printf(%*sEndpoint Descriptor:\n, indent, );
+   indent++;
+   printf(%*sbLength: %d\n, indent, , ed-bLength);
+   printf(%*sbDescriptorType: %d\n, indent, , ed-bDescriptorType);
+   printf(%*sbEndpointAddress: %#x EP %d %s\n, indent, , 
ed-bEndpointAddress,
+  UE_GET_ADDR(ed-bEndpointAddress),
+  UE_GET_DIR(ed-bEndpointAddress) ? in : out);
+   printf(%*sbmAttributes: %#x\n, indent, , ed-bmAttributes);
+
+   switch (UE_GET_XFERTYPE(ed-bmAttributes)) {
+   case UE_CONTROL:
+   xfertype = Control;
+   break;
+   case UE_ISOCHRONOUS:
+   xfertype = Isochronous;
+   break;
+   case UE_BULK:
+   xfertype = Bulk;
+   break;
+   case UE_INTERRUPT:
+   xfertype = Interrupt;
+   break;
+   default:
+   xfertype = Unknown;
+   break;
+   }
+
+   switch (UE_GET_ISO_TYPE(ed-bmAttributes)) {
+   case UE_ISO_ASYNC:
+   synchtype = Asynchronous;
+   break;
+   case UE_ISO_ADAPT:
+   synchtype = Adaptive;
+   break;
+   case UE_ISO_SYNC:
+   synchtype = Synchronous;
+   break;
+   default:
+   synchtype = No Synchronization;
+   break;
+   }
+
+   indent++;
+   printf(%*sTransfer type: %s\n, indent, , xfertype);
+   

Re: [PATCH] Add support for dumping descriptors to usbdevs(8)

2015-04-13 Thread Dimitris Papastamos
Hi,

Attached below an updated patch that makes the layout a bit more sensible.

Index: usr.sbin/usbdevs/usbdevs.8
===
RCS file: /cvs/src/usr.sbin/usbdevs/usbdevs.8,v
retrieving revision 1.9
diff -u -p -r1.9 usbdevs.8
--- usr.sbin/usbdevs/usbdevs.8  26 Jun 2008 05:42:21 -  1.9
+++ usr.sbin/usbdevs/usbdevs.8  13 Apr 2015 16:46:59 -
@@ -54,7 +54,13 @@ Show the device drivers associated with 
 .It Fl f Ar dev
 Only print information for the given USB controller.
 .It Fl v
-Be verbose.
+Verbose mode.  Causes
+.Nm
+to display detailed information about the devices present.
+Multiple
+.Fl v
+options increase verbosity.  At level 1 or above the device
+and configuration descriptors will be shown.
 .El
 .Sh FILES
 .Bl -tag -width Pa
Index: usr.sbin/usbdevs/usbdevs.c
===
RCS file: /cvs/src/usr.sbin/usbdevs/usbdevs.c,v
retrieving revision 1.24
diff -u -p -r1.24 usbdevs.c
--- usr.sbin/usbdevs/usbdevs.c  31 Mar 2015 13:38:27 -  1.24
+++ usr.sbin/usbdevs/usbdevs.c  13 Apr 2015 16:46:59 -
@@ -30,14 +30,17 @@
  * POSSIBILITY OF SUCH DAMAGE.
  */
 
+#include sys/types.h
+
+#include err.h
+#include errno.h
+#include stdarg.h
 #include stdio.h
 #include stdlib.h
 #include string.h
-#include sys/types.h
 #include fcntl.h
 #include unistd.h
-#include err.h
-#include errno.h
+
 #include dev/usb/usb.h
 
 #ifndef nitems
@@ -45,15 +48,29 @@
 #endif
 
 #define USBDEV /dev/usb
+#define INDENT_LEVEL 3
 
-int verbose = 0;
-int showdevs = 0;
+int verbose;
+int showdevs;
+
+int indent;
+char done[USB_MAX_DEVICES];
 
 void usage(void);
+void pushlevel(void);
+void poplevel(void);
+void printent(const char *, ...);
+int getddesc(int, int, int, struct usb_device_ddesc *);
+int getcdesc(int, int, int, struct usb_device_cdesc *);
+int getfdesc(int, int, int, int, struct usb_device_fdesc *);
+void dumpedesc(usb_endpoint_descriptor_t *);
+void dumpidesc(usb_interface_descriptor_t *);
+void dumpcdesc(usb_config_descriptor_t *);
+void dumpddesc(usb_device_descriptor_t *);
+void dumpdesc(int, int, int);
 void usbdev(int f, int a, int rec);
 void usbdump(int f);
 void dumpone(char *name, int f, int addr);
-int main(int, char **);
 
 extern char *__progname;
 
@@ -64,8 +81,256 @@ usage(void)
exit(1);
 }
 
-char done[USB_MAX_DEVICES];
-int indent;
+void
+pushlevel(void)
+{
+   indent += INDENT_LEVEL;
+}
+
+void
+poplevel(void)
+{
+   indent -= INDENT_LEVEL;
+}
+
+void
+printent(const char *fmt, ...)
+{
+   va_list ap;
+
+   va_start(ap, fmt);
+   printf(%*s, indent, );
+   vprintf(fmt, ap);
+   va_end(ap);
+}
+
+int
+getddesc(int f, int b, int a, struct usb_device_ddesc *ddesc)
+{
+   int r;
+
+   ddesc-udd_bus = b;
+   ddesc-udd_addr = a;
+   r = ioctl(f, USB_DEVICE_GET_DDESC, ddesc);
+   if (r) {
+   if (errno != ENXIO)
+   warn(USB_DEVICE_GET_DDESC);
+   return -1;
+   }
+   return 0;
+}
+
+int
+getcdesc(int f, int b, int a, struct usb_device_cdesc *cdesc)
+{
+   int r;
+
+   cdesc-udc_bus = b;
+   cdesc-udc_addr = a;
+   cdesc-udc_config_index = USB_CURRENT_CONFIG_INDEX;
+   r = ioctl(f, USB_DEVICE_GET_CDESC, cdesc);
+   if (r) {
+   warn(USB_DEVICE_GET_CDESC);
+   return -1;
+   }
+   return 0;
+}
+
+int
+getfdesc(int f, int b, int a, int l, struct usb_device_fdesc *fdesc)
+{
+   int r;
+
+   fdesc-udf_bus = b;
+   fdesc-udf_addr = a;
+   fdesc-udf_config_index = USB_CURRENT_CONFIG_INDEX;
+   fdesc-udf_size = l;
+   fdesc-udf_data = malloc(l);
+   if (!fdesc-udf_data)
+   err(1, malloc);
+   r = ioctl(f, USB_DEVICE_GET_FDESC, fdesc);
+   if (r) {
+   warn(USB_DEVICE_GET_FDESC);
+   free(fdesc-udf_data);
+   return -1;
+   }
+   return 0;
+}
+
+void
+dumpedesc(usb_endpoint_descriptor_t *ed)
+{
+   char *xfertype, *synchtype;
+
+   printent(Endpoint Descriptor:\n);
+   pushlevel();
+   printent(bLength:  %d\n, ed-bLength);
+   printent(bDescriptorType:  %d\n, ed-bDescriptorType);
+   printent(bEndpointAddress: %#x EP %d %s\n, ed-bEndpointAddress,
+  UE_GET_ADDR(ed-bEndpointAddress),
+  UE_GET_DIR(ed-bEndpointAddress) ? in : out);
+   printent(bmAttributes: %#x\n, ed-bmAttributes);
+
+   switch (UE_GET_XFERTYPE(ed-bmAttributes)) {
+   case UE_CONTROL:
+   xfertype = Control;
+   break;
+   case UE_ISOCHRONOUS:
+   xfertype = Isochronous;
+   break;
+   case UE_BULK:
+   xfertype = Bulk;
+   break;
+   case UE_INTERRUPT:
+   xfertype = Interrupt;
+   break;
+   default:
+   xfertype = Unknown;
+   break;
+   }
+
+   switch 

Re: [PATCH] Add support for dumping descriptors to usbdevs(8)

2015-04-13 Thread Dimitris Papastamos
One more thing I forgot to mention, the current printing layout
is not ideal.  I'll look into making it a bit more readable.