ddusb README

"ddusb" was written in order to learn how to communicate with a pen drive. (aka flash drive, memory stick...). The Universal Host Controller "UHCI" is the actual hdwre device which controls the transactions.  
If your pc has an "Open" host controller "OHCI" this software will not work. If you have a modern computer with an "EHCI" controller, you may have 8 usb ports with each pair controlled by a "UHCI" controller. You may be able to configure this software to work.

All transactions are conducted via a ps/2 keyboard. Video display is 800x600x8bpp. 

The device endpoint in/out numbers are hard coded for my pen drive so you will have to change these and recompile for yours. See file usbtransaction.s

To build the bootable image ddusb.img you will need NASM and gnu make. Just type "make".  See the makefile for details.

The file usbtransactions.s is purposely bloated and repetative. It uses global data structures and repetative coding to make it easier to understand. 

You should practice messing up a transaction and then recovering. For example after running the Read10CBW transaction which informs the device you are going to read 1 block=512 bytes (which requires you to push the ReadData key 8 times for 8*64 byte packets) instead of pushing 8 times and then running the Read10CSW, only push ReadData 6 times and then Read10CSW. This will cause a babble/stall error of the in endpoint. To fix this you may have success with ClearBulkIn or BulkResetRecovery but I have not. Instead I have to resetport0 then SetAddress then SetConfiguration and then try TestUnitReady. If TestUnitReady fails then go back and resetport0 again and start all over. For me it usually takes (2) port resets. 

On one of my computers the TestUnitReady CSW almost always fails the first time but if you push it again it will pass.

Minimal use of the program is as follows:
	*Insert pen drive into PORT0 (not PORT1)
	*copy "ddusb.img" bootable image to floppy or CD 
	*boot computer
	*first screen gives pci config reg info
	*hit "c" to read the pci configuration registers
	*page down to next screen which is for usb control xfers
	*reset/enable controller
	*reset/enable port0
	*SetAddress
	*optional:get device/config/interface descriptors 
	NOTE: the endpoint IN/OUT numbers are hard coded for my pen drive
	so you may need to fix for yours
	*SetConfiguration
	*page down to next screen
	*now you can Inquire, TestUnitReady, ReadCapacity
	*To use Read10, press Read10CBW then press ReadData 8 times (each time to read 64 bytes for a total of 512 bytes) then finally press the ReadCSW
	*use Write10 in a similar manner
	*as a double check on the write, from linux you can do:
	"dd if=/dev/sda of=temp bs=512 count=4" and this will copy the first 4 blocks off your pen drive to a file named temp then use "xxd temp > temp.hex" to convert the binary file to hex then view in a text editor.
	
	

This program will write to the 4th block of your pen drive so be ware !!!.

Once you get to writting your own driver automating the command->data->status transactions, you will have to pay attention to the device timings. See the 1.1 spec table 7-10 which tells you to wait 50ms for this and 10ms for that...


The program will not make much sense unless you download and study the following Reference Material. The first 5 references are mandatory reading.

[1] "Universal Serial Bus Mass Storage Class Bulk Only Transport", rev 1.0 Sept 1999 Usb Implementors Forum

[2] "Universal Serial Bus Specification", rev 1.1 1998 and rev 2.0, ch9 is most important

[3] Intel "82371AB PCI-ISA Xcelerator (PIIX4)" Controller Manual and "Universal Host Controller Interface (UHCI) Design Guide" rev 1.1, these are your hardware manuals

[4] "Working Draft American National Standard SCSI Block Commands (SBC-2)" Nov 2004 and the "SCSI Primary Commands (SPC-2)"

[5] "PCI Bios Spec" rev2.1

"Usb Simply Buffered Device Enumeration" and "Usb Simply Buffered Mass Storage Class - Bulk Only Transport" 2007 Shakthi Kannan, actual bytes from usb transactions

"USB Made Simple", web site with 7 part series.

"SnoopyPro" software for windows to view some usb transaction data.

LinuxBios usb_scsi_low.h

Beyond Logic "Usb in a Nutshell" excellent website

waste.org "PCI bus info and code from a programmers perspective"

Jan Axelson has some books out on usb mass storage. I bought one and promptly left it on a plane. The book does not cover at all the hardware details of the UHCI, only covers the USB and Mass Storage Class specs.


I did not use an emulator to develope this software. Many trips down to the old computer in my basement shop, so I cant say how well it will work with Bochs or Qemu.


disclaimer:
I am only an amature programmer who undertook the usb journey for the fun and challenge. If this software does not work for you, then consider it a stepping stone on your way to successful usb mass storage programming.



March 2008
Tom Timmermann
www.ticon.net/~tatimmer




