How can I use target mode in Linux with USB?

Kernel 3.5 introduces:

This release includes a driver for using an IEEE-1394 connection as a SCSI transport. This enables to expose SCSI devices to other nodes on the Firewire bus, for example hard disk drives. It's a similar functionality to Firewire Target Disk Mode on many Apple computers.

This release also adds a usb-gadget driver that does the same with USB. The driver supports two USB protocols are supported that is BBB or BOT (Bulk Only Transport) and UAS (USB Attached SCSI). BOT is advertised on alternative interface 0 (primary) and UAS is on alternative interface 1. Both protocols can work on USB 2.0 and USB 3.0. UAS utilizes the USB 3.0 feature called streams support.

http://kernelnewbies.org/Linux_3.5

I have an Arch Linux with kernel 3.5.3-1 and wanna try out this feature.


Solution 1:

FireWire is a peer-to-peer architecture, so all that is required for a PC to pretend to be a hard disk is some software.

USB, on the other hand, has a strict distinction between the host (the PC) and devices. Whether your hardware can run as a host or as a device (or both, in the case of USB-On-The-Go) depends on what your USB interface chip was designed to do.

Linux runs on both PCs and on devices, so it has drivers for both kinds of USB interfaces. However, all the "USB Gadget" drivers require that the interface chip supports device mode, and none of the controllers built into PCs do.

Your only chance to run this code would be to use some embedded device, like a router, a NAS box, or a phone.

Solution 2:

Frome the code under that message, you can see the patches. The one relevant for you is:

--- a/drivers/usb/gadget/Kconfig
+++ b/drivers/usb/gadget/Kconfig
@@ -798,6 +798,16 @@ config USB_MASS_STORAGE
          Say "y" to link the driver statically, or "m" to build
          a dynamically linked module called "g_mass_storage".

+config USB_GADGET_TARGET
+       tristate "USB Gadget Target Fabric Module"
+       depends on TARGET_CORE
+       help
+         This fabric is an USB gadget. Two USB protocols are supported that is
+         BBB or BOT (Bulk Only Transport) and UAS (USB Attached SCSI). BOT is
+         advertised on alternative interface 0 (primary) and UAS is on
+         alternative interface 1. Both protocols can work on USB2.0 and USB3.0.
+         UAS utilizes the USB 3.0 feature called streams support.
+
 config USB_G_SERIAL
        tristate "Serial Gadget (with CDC ACM and CDC OBEX support)"
        help

This illustrates that USB_GADGET_TARGET has been added, if you enable this option in your kernel and recompile / reinstall the kernel then you will be able to use target mode in linux with USB. Note that you have to enable TARGET_CORE as well.

More information can be found at:

  • http://en.wikipedia.org/wiki/USB_Attached_SCSI
  • http://www.usb.org/developers/devclass_docs/usbmassbulk_10.pdf