changeset 8:1969a435a7a2

Change to using 16c0:05e1 shared CDC USB ID Enumerate devices using "udevadm info" (lsusb does not show strings) Randomize serial numbers Add domain name to manufacturer string
author Frank Kingswood <frank@kingswood-consulting.co.uk>
date Mon, 21 Apr 2014 11:03:21 +0100
parents ef38703286dd
children 16a338171c99
files Makefile program usb_serial.c
diffstat 3 files changed, 53 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
     1.1 --- a/Makefile	Fri Apr 11 17:21:26 2014 +0100
     1.2 +++ b/Makefile	Mon Apr 21 11:03:21 2014 +0100
     1.3 @@ -59,14 +59,21 @@
     1.4  # need something to pick up latest tag
     1.5  # echo '"'$$(hg tags | sort | awk '/^v[0-9\.]+/||1 { V=$$1 } END { if(V!="") print " " V}')'"'
     1.6  
     1.7 -version.h:      Makefile $(filter-out version.h,$(wildcard *.cc *.h))
     1.8 -	ID=$$(hg id -i) ; \
     1.9 -	TAG=$$(hg tags | sort | awk '/^v[0-9\.]+/ { V=$$1 } END { if(V!="") print " " V}') ; \
    1.10 -	REV=$$(hg id -r $${TAG:-0}) ; \
    1.11 -	if [ "$$ID" != "$$REV" ] ; then \
    1.12 -	   [ -n "$$TAG" ] && echo '"'$$TAG' "' ; \
    1.13 -	   echo '"'$$ID'"' ; \
    1.14 -	fi  >$@
    1.15 +version.h:	$(wildcard *.c *.cc)
    1.16 +		ID=$$(hg id -i) ; \
    1.17 +		TAG=$$(hg tags | sort | awk '/^v[0-9\.]+/ { V=$$1 } END { if(V!="") print " " V}') ; \
    1.18 +		REV=$$(hg id -r $${TAG:-0}) ; \
    1.19 +		if [ "$$ID" != "$$REV" ] ; then \
    1.20 +		   [ -n "$$TAG" ] && echo '"'$$TAG' "' ; \
    1.21 +		   echo '"'$$ID'"' ; \
    1.22 +		fi  >$@
    1.23 +
    1.24 +random.h:	$(wildcard *.c *.cc)
    1.25 +		echo "#define STR_SERIAL_NUMBER L\"$$(od -A n -t u -N 4 /dev/urandom | tr -d ' ')\"" >$@
    1.26 +
    1.27 +o/usb_serial.o: usb_serial.c random.h | o
    1.28 +		@echo gcc $^
    1.29 +		avr-gcc -c $(CFLAGS) -o $@ $<
    1.30  
    1.31  o/commands.o:   commands.cc version.h | o
    1.32  		@echo g++ $^
    1.33 @@ -77,7 +84,8 @@
    1.34  		avr-g++ -L/usr/local/avr/lib $(LDFLAGS) -o $@ $^ $(DASHV)
    1.35  		chmod -x $@
    1.36  		avr-size $@
    1.37 -CLEAN+=main.avr version.h
    1.38 +
    1.39 +CLEAN+=main.avr version.h random.h
    1.40  
    1.41  # --------------------------------------------------------------------------
    1.42  
     2.1 --- a/program	Fri Apr 11 17:21:26 2014 +0100
     2.2 +++ b/program	Mon Apr 21 11:03:21 2014 +0100
     2.3 @@ -14,21 +14,42 @@
     2.4     DASHQ=
     2.5  fi
     2.6  
     2.7 -for DEV in /dev/ttyUSB0* ; do
     2.8 -   true
     2.9 -done
    2.10 +find_device() {
    2.11 +   for F in /dev/ttyACM* ; do udevadm info --name $F ;done | awk '
    2.12 +      BEGIN { name="";state=0 }
    2.13 +      /^P:/ { if(state==111) state+=1000 }
    2.14 +      /^E: ID_VENDOR_ID='$1'$/ { state+=1 }
    2.15 +      /^E: ID_MODEL_ID='$2'$/ { state+=10 }
    2.16 +      /^E: DEVNAME=/ { name=substr($2,9); print "   found " name >"/dev/stderr"; state+=100; }
    2.17 +      END { if(state==111) { print name } else { exit 2 }}
    2.18 +   '
    2.19 +}
    2.20  
    2.21  MODE=
    2.22 -if lsusb -d 2341:8036 >/dev/null ; then
    2.23 +if [ "$(lsusb -d 2341:8036 | wc -l)" = "1" ] ; then
    2.24 +   DEV=$(find_device 2341 8036)
    2.25     MODE=AVR109
    2.26  
    2.27 -elif lsusb -d 16c0:0478 >/dev/null ; then
    2.28 +elif [ "$(lsusb -d 16c0:0478 | wc -l)" = "1" ] ; then
    2.29 +   DEV=$(find_device 16c0 0478)
    2.30     MODE=Teensy
    2.31  
    2.32 -elif lsusb -d 16c0:047a >/dev/null ; then
    2.33 -   echo "Autodetecting USB-GPIB"
    2.34 +elif lsusb -d 16c0:05e1 >/dev/null ; then
    2.35 +   echo "Detecting USB-GPIB on shared CDC USB ID"
    2.36 +
    2.37 +   DEV=$(
    2.38 +      for F in /dev/ttyACM* ; do udevadm info --name $F ;done | awk '
    2.39 +         BEGIN { name="";state=0 }
    2.40 +         /^P:/ { if(state==111) state+=10000 }
    2.41 +         /^N:/ { name=$2; state+=1; }
    2.42 +         /^E: ID_MODEL=USB-GPIB-32U4/ { state+=10; }
    2.43 +         /^E: ID_VENDOR=Frank_Kingswood__kingswood-consulting.co.uk$/ { state+=100; }
    2.44 +         /^E: DEVNAME=/ { name=substr($2,9); print "   found " name >"/dev/stderr"; state+=1000; }
    2.45 +         END { if(state==1111) { print name  }}
    2.46 +      '
    2.47 +   )
    2.48 +
    2.49     trap "true" ALRM
    2.50 -
    2.51     exec 3<>$DEV
    2.52  
    2.53     set +e
    2.54 @@ -80,7 +101,9 @@
    2.55     trap - ALRM
    2.56  
    2.57  else
    2.58 -   echo "No device found"
    2.59 +   echo "No device or multiple devices found:"
    2.60 +   echo
    2.61 +   lsusb | grep -Pv "Linux Foundation .+ root hub"
    2.62     exit 1
    2.63  fi
    2.64  
    2.65 @@ -92,7 +115,6 @@
    2.66  
    2.67  sleep 0.5
    2.68  set -x
    2.69 -
    2.70  case "$MODE" in
    2.71     Teensy)
    2.72        teensy_loader_cli -mmcu="$2" -w -v $DASHV "$3"
     3.1 --- a/usb_serial.c	Fri Apr 11 17:21:26 2014 +0100
     3.2 +++ b/usb_serial.c	Mon Apr 21 11:03:21 2014 +0100
     3.3 @@ -42,7 +42,7 @@
     3.4  
     3.5  // You can change these to give your code its own name.  On Windows,
     3.6  // these are only used before an INF file (driver install) is loaded.
     3.7 -#define STR_MANUFACTURER   L"Frank Kingswood"
     3.8 +#define STR_MANUFACTURER   L"Frank Kingswood, kingswood-consulting.co.uk"
     3.9  #define STR_PRODUCT        L"USB-GPIB-32U4"
    3.10  
    3.11  // All USB serial devices are supposed to have a serial number
    3.12 @@ -60,14 +60,15 @@
    3.13  // Udev rules (in /etc/udev/rules.d) can define persistent device
    3.14  // names linked to this serial number, as well as permissions, owner
    3.15  // and group settings.
    3.16 -#define STR_SERIAL_NUMBER  L"324"
    3.17 +// #define STR_SERIAL_NUMBER  L"0"
    3.18 +#include "random.h"
    3.19  
    3.20  // Mac OS-X and Linux automatically load the correct drivers.  On
    3.21  // Windows, even though the driver is supplied by Microsoft, an
    3.22  // INF file is needed to load the driver.  These numbers need to
    3.23  // match the INF file.
    3.24  #define VENDOR_ID          0x16C0
    3.25 -#define PRODUCT_ID         0x047A
    3.26 +#define PRODUCT_ID         0x05e1  // shared CDC class ID
    3.27  
    3.28  // When you write data, it goes into a USB endpoint buffer, which
    3.29  // is transmitted to the PC when it becomes full, or after a timeout