dev builds
themes manual
device status forums
mailing lists
IRC bugs
dev guide

Search | Go
Wiki > Main > RockboxUsbHandling

USB handling in Rockbox

How Rockbox handles USB slave mode


Rockbox is designed for devices with a Universal Mass Storage USB connection. When the user inserts the USB cable, Rockbox should stop accessing the hard drive and enter USB Mode. When the user extracts the cable, Rockbox should continue as normal.

The USB thread

There is a dedicated thread that monitors the USB connector and waits for a connection. When it happens, it tells the other threads to stop the disk activity and report back when they are ready. When all threads have responded, the USB thread grants the USB chip control of the hard drive, and waits until the USB connector is extracted. It then tells all threads that it is permitted to access the HD again.

The signalling mechanism

The USB thread uses the queue_broadcast() function to signal the other threads. This means that threads that don't have an event queue will not be notified.

The USB thread will not enter USB mode until all threads are ready.

DirectedGraphPlugin_1.png diagram

Step 1: The USB thread sends SYS_USB_CONNECTED to all threads

DirectedGraphPlugin_1.png diagram

Step 2: All threads reply with SYS_USB_CONNECTED_ACK

The SYS_USB events

Event Description
SYS_USB_CONNECTED This is sent to all threads when the USB cable is connected
SYS_USB_CONNECTED_ACK This is sent by a thread as a reply to SYS_USB_CONNECTED when the thread is ready to enter USB mode
SYS_USB_DISCONNECTED This is sent to all threads when the USB cable is disconnected
SYS_USB_DISCONNECTED_ACK This is sent by a thread as a reply to SYS_USB_DISCONNECTED when the thread is ready to leave USB mode

Helper functions

There are two functions that can handle this for you. Use them.

void usb_acknowledge(long id)

Call this to send an acknowledge to a USB event. Example:

void usb_wait_for_disconnect(struct event_queue *q)

Call this to wait until the USB thread sends the SYS_USB_DISCONNECTED event. This function also sends the SYS_USB_DISCONNECTED_ACK event for you. Pass a pointer to your event queue.

int usb_wait_for_disconnect_w_tmo(struct event_queue *q, int ticks)

Same as above, but you can specify a timeout where the function returns even if the USB connection is still active. The return value is 1 if there was a timeout, and 0 if the USB was disconnected.


If you decide that your thread should respond to USB events, do like this:

static struct event_queue my_queue;

void my_thread(void)
    struct event ev;

   while(1) {
      queue_wait_w_tmo(&my_queue, &ev, 0);

      switch( {
         /* Do cleanup, like closing open files */

         /* Wait until the USB cable is extracted again */

Special case: the main thread

The main thread is responsible for displaying the "USB Connected" screen while in USB mode. Therefore, the main thread should do it differently. The SYS_USB_CONNECTED event is received in the button queue (which is a regular event queue), so you have to handle it in your button event handler. For your convenience, there is a function that handles all this for you, the default event handler.

Just call the default event handler in the default case of your switch. It returns the event that was handled if you need to know, for example if you want to quit the function if it has been in USB mode.


while (!exit) {
        key = button_get();
        switch( key ) {
        case BUTTON_STOP:
            exit = true;

            if(default_event_handler(key) == SYS_USB_CONNECTED) {
                return MYFUNC_ATTACHED_USB;

r21 - 08 Oct 2023 - 18:38:48 - AdminUser

Copyright © by the contributing authors.