Page 1 of 1

USB2.0 mass storage device

Posted: Sat Apr 24, 2010 6:23 am
by ehenkes
EHCI and USB 2.0 works. Based on control transfers, the OS can fetch descriptors and parse them. As next step I want to read sectors out of an attached USB stick. As far as I have seen, the usb stick belongs to the interface subclass "SCSI". Can you give me some advice how to implement the necessary SCSI support. How is this done as good practice?

Re: USB2.0 mass storage device

Posted: Mon Apr 26, 2010 11:44 am
by TomT
Here is my CBW CommandBlockWrapper for the SCSI Inquiry request.
take a look at tatOS/usb/inquiry.s and also other files in that directory.

See the /docs directory for a list of resources. I found these documents useful:
[6] "Working Draft American National Standard SCSI Block Commands (SBC-2)"
Nov 2004 and the "SCSI Primary Commands (SPC-2)"

Good luck,
TomT
http://code.google.com/p/tatos/


;Command Block Wrapper for SCSI Inquiry (31 bytes)
InquiryRequest:
dd 0x43425355 ;dCBWSignature
dd 0xaabbccdd ;dCBWTag (device will copy this into CSW)
dd 0x24 ;dCBWDataTransferLength (for tdData)
db 0x80 ;bmCBWFlags 0x80=Device2Host, 00=Host2Device
db 0 ;bCBWLun
db 6 ;bCBWCBLength (of CBWCB)
;CBWCB (16 bytes) see SCSI Inquiry Command
db 0x12 ;SCSI operation code
db 0 ;SCSI reserved
db 0 ;SCSI page or operation code
db 0 ;SCSI reserved
db 0x24 ;SCSI allocation length
db 0 ;SCSI control
times 15 db 0 ;USBmass CBWCB must be 16 bytes long (we add alittle extra 0)

Re: USB2.0 mass storage device

Posted: Sun May 02, 2010 1:39 pm
by ehenkes
@TomT:
thank you very much for this link to tatOS, great work! what ist the reason for this order of the commands in your OS? Where can I find the original spec for this procedure?

Code: Select all

	STDCALL mpdstr16,putmessage
	call SetAddress

	STDCALL mpdstr17,putmessage
	call SetConfiguration



	;now we start on the SCSI commands
	;the order of the commands and redundancy is important
	;we init the flash toggles here and then let prepareTDchain touch them only
	mov dword  [bulktogglein] ,0
	mov dword  [bulktoggleout],0

	STDCALL mpdstr18,putmessage
	call Inquiry

	STDCALL mpdstr19,putmessage
	call TestUnitReady

	STDCALL mpdstr20,putmessage
	call RequestSense

	STDCALL mpdstr19,putmessage
	call TestUnitReady

	STDCALL mpdstr20,putmessage
	call RequestSense

	STDCALL mpdstr21,putmessage
	call ReadCapacity

	;done-ready for read10/write10 
	STDCALL mpdstr22,putmessage
	mov dword [FLASHDRIVEREADY],1
@all:
Is there no Hobby-OS with USB2/SCSI for Mass Storage Devices (MSD) written in C? We have some code working, but with some errors, which we do not understand regarding the root causes. If somebody is interested in this area I invite him/her to help us. Control Transfers work great, it is the area of bulk transfers causing some trouble and instability.

Source Code: http://prettyos.svn.sourceforge.net/vie ... z?view=tar (comments English) please look at ehci.c and usb2.c
chat: irc.euirc.net #PrettyOS (we speak English)
forum: http://www.c-plusplus.de/forum/viewforu ... is-62.html (German)

Re: USB2.0 mass storage device

Posted: Fri May 07, 2010 4:21 pm
by ehenkes
Currently, the biggest problem is that we can carry out one SCSI command perfectly at a real PC (VMWare already works great) , but as soon as a second SCSI command follows, the asynchronous scheduler does not carry out the qH and qTD connected to the IN endpoint. As said, control transfers and the first SCSI command work great.

http://prettyos.svn.sourceforge.net/vie ... z?view=tar

Does anybody have a clear sequence how to manage the async scheduler?

Problem solved. :D

But I would appreciate to communicate to other people working on this complex area.

EDIT: currently, our EHCI / USB 2.0 / MSD/SCSI driver works well. Mainly handshake and toggle problems during bulk transfer.

Re: USB2.0 mass storage device

Posted: Fri May 14, 2010 6:39 am
by rdos
I have plans to implement this in the "near future". I have working USB-stacks for UHCI and OHCI, and experimental for EHCI, but I found the mass-storage class to lack in documentation (the SCSI part). I also have the FAT-file systems implemented and relatively bug-free under ATA/IDE.

Links to "easy-to-follow" code for how the SCSI part works would be appreciated. The rest seems to be well described in mass storage class in the USB documentation.

Re: USB2.0 mass storage device

Posted: Fri May 14, 2010 8:07 am
by eddyb
rdos wrote:I have plans to implement this in the "near future". I have working USB-stacks for UHCI and OHCI, and experimental for EHCI, but I found the mass-storage class to lack in documentation (the SCSI part). I also have the FAT-file systems implemented and relatively bug-free under ATA/IDE.

Links to "easy-to-follow" code for how the SCSI part works would be appreciated. The rest seems to be well described in mass storage class in the USB documentation.
Wikipedia is your friend: http://en.wikipedia.org/wiki/SCSI_command . Have fun :D .

Re: USB2.0 mass storage device

Posted: Fri May 14, 2010 12:00 pm
by ehenkes
The most important thing is that inside the SCSI command MSB comes first! :)
Try the sequence: inquiry - test unit ready - request sense - test unit ready - request sense - read capacity - read ...
No handshakes (HC does the ACK alone), switch data toggle per endpoint after each packet transfer or use setconfig(1) before each transfer, if you want to start with toggle 0. :wink:

There is a spec for usb MSD: "Universal Serial Bus Mass Storage Class Bulk-Only Transport" Rev. 1.0, September 31, 1999
http://www.usb.org/developers/devclass_ ... ulk_10.pdf

Have fun! :D


Links to "easy-to-follow" code for how the SCSI part works would be appreciated <--- http://prettyos.svn.sourceforge.net/vie ... usb2_msd.c :)

I would appreciate a co-worker or at least an experience exchange on this complex topic.

Re: USB2.0 mass storage device

Posted: Fri May 14, 2010 2:07 pm
by rdos
OK, thanks for the info. I will get back to this at some point, but it might take a few months. I'll code it in assembly, so it will probably not be easy-to-read code. Currently, I have USB-code for various serial to USB adapters, as this is what I currently need for our professional payment terminal.

Re: USB2.0 mass storage device

Posted: Sat May 15, 2010 1:21 am
by ehenkes
Some people think that "test unit ready" is not necessary. The device has to be "ready". :)

What does that mean, if "Request Sense" shows "Sense data are not SCSI compliant" in the "Valid" information?

Re: USB2.0 mass storage device

Posted: Sun May 16, 2010 2:11 pm
by ehenkes

Re: USB2.0 mass storage device

Posted: Tue Jun 01, 2010 4:22 pm
by ehenkes
I can recommend Jan Axelson's book "usb mass storage". Above all, here you can meet the author and other developers in this new forum:
http://www.lvr.com/forum/index.php?topic=14.0 :D