1*4882a593Smuzhiyun================= 2*4882a593SmuzhiyunThe UCAN Protocol 3*4882a593Smuzhiyun================= 4*4882a593Smuzhiyun 5*4882a593SmuzhiyunUCAN is the protocol used by the microcontroller-based USB-CAN 6*4882a593Smuzhiyunadapter that is integrated on System-on-Modules from Theobroma Systems 7*4882a593Smuzhiyunand that is also available as a standalone USB stick. 8*4882a593Smuzhiyun 9*4882a593SmuzhiyunThe UCAN protocol has been designed to be hardware-independent. 10*4882a593SmuzhiyunIt is modeled closely after how Linux represents CAN devices 11*4882a593Smuzhiyuninternally. All multi-byte integers are encoded as Little Endian. 12*4882a593Smuzhiyun 13*4882a593SmuzhiyunAll structures mentioned in this document are defined in 14*4882a593Smuzhiyun``drivers/net/can/usb/ucan.c``. 15*4882a593Smuzhiyun 16*4882a593SmuzhiyunUSB Endpoints 17*4882a593Smuzhiyun============= 18*4882a593Smuzhiyun 19*4882a593SmuzhiyunUCAN devices use three USB endpoints: 20*4882a593Smuzhiyun 21*4882a593SmuzhiyunCONTROL endpoint 22*4882a593Smuzhiyun The driver sends device management commands on this endpoint 23*4882a593Smuzhiyun 24*4882a593SmuzhiyunIN endpoint 25*4882a593Smuzhiyun The device sends CAN data frames and CAN error frames 26*4882a593Smuzhiyun 27*4882a593SmuzhiyunOUT endpoint 28*4882a593Smuzhiyun The driver sends CAN data frames on the out endpoint 29*4882a593Smuzhiyun 30*4882a593Smuzhiyun 31*4882a593SmuzhiyunCONTROL Messages 32*4882a593Smuzhiyun================ 33*4882a593Smuzhiyun 34*4882a593SmuzhiyunUCAN devices are configured using vendor requests on the control pipe. 35*4882a593Smuzhiyun 36*4882a593SmuzhiyunTo support multiple CAN interfaces in a single USB device all 37*4882a593Smuzhiyunconfiguration commands target the corresponding interface in the USB 38*4882a593Smuzhiyundescriptor. 39*4882a593Smuzhiyun 40*4882a593SmuzhiyunThe driver uses ``ucan_ctrl_command_in/out`` and 41*4882a593Smuzhiyun``ucan_device_request_in`` to deliver commands to the device. 42*4882a593Smuzhiyun 43*4882a593SmuzhiyunSetup Packet 44*4882a593Smuzhiyun------------ 45*4882a593Smuzhiyun 46*4882a593Smuzhiyun================= ===================================================== 47*4882a593Smuzhiyun``bmRequestType`` Direction | Vendor | (Interface or Device) 48*4882a593Smuzhiyun``bRequest`` Command Number 49*4882a593Smuzhiyun``wValue`` Subcommand Number (16 Bit) or 0 if not used 50*4882a593Smuzhiyun``wIndex`` USB Interface Index (0 for device commands) 51*4882a593Smuzhiyun``wLength`` * Host to Device - Number of bytes to transmit 52*4882a593Smuzhiyun * Device to Host - Maximum Number of bytes to 53*4882a593Smuzhiyun receive. If the device send less. Commom ZLP 54*4882a593Smuzhiyun semantics are used. 55*4882a593Smuzhiyun================= ===================================================== 56*4882a593Smuzhiyun 57*4882a593SmuzhiyunError Handling 58*4882a593Smuzhiyun-------------- 59*4882a593Smuzhiyun 60*4882a593SmuzhiyunThe device indicates failed control commands by stalling the 61*4882a593Smuzhiyunpipe. 62*4882a593Smuzhiyun 63*4882a593SmuzhiyunDevice Commands 64*4882a593Smuzhiyun--------------- 65*4882a593Smuzhiyun 66*4882a593SmuzhiyunUCAN_DEVICE_GET_FW_STRING 67*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~ 68*4882a593Smuzhiyun 69*4882a593Smuzhiyun*Dev2Host; optional* 70*4882a593Smuzhiyun 71*4882a593SmuzhiyunRequest the device firmware string. 72*4882a593Smuzhiyun 73*4882a593Smuzhiyun 74*4882a593SmuzhiyunInterface Commands 75*4882a593Smuzhiyun------------------ 76*4882a593Smuzhiyun 77*4882a593SmuzhiyunUCAN_COMMAND_START 78*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~ 79*4882a593Smuzhiyun 80*4882a593Smuzhiyun*Host2Dev; mandatory* 81*4882a593Smuzhiyun 82*4882a593SmuzhiyunBring the CAN interface up. 83*4882a593Smuzhiyun 84*4882a593SmuzhiyunPayload Format 85*4882a593Smuzhiyun ``ucan_ctl_payload_t.cmd_start`` 86*4882a593Smuzhiyun 87*4882a593Smuzhiyun==== ============================ 88*4882a593Smuzhiyunmode or mask of ``UCAN_MODE_*`` 89*4882a593Smuzhiyun==== ============================ 90*4882a593Smuzhiyun 91*4882a593SmuzhiyunUCAN_COMMAND_STOP 92*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~ 93*4882a593Smuzhiyun 94*4882a593Smuzhiyun*Host2Dev; mandatory* 95*4882a593Smuzhiyun 96*4882a593SmuzhiyunStop the CAN interface 97*4882a593Smuzhiyun 98*4882a593SmuzhiyunPayload Format 99*4882a593Smuzhiyun *empty* 100*4882a593Smuzhiyun 101*4882a593SmuzhiyunUCAN_COMMAND_RESET 102*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~ 103*4882a593Smuzhiyun 104*4882a593Smuzhiyun*Host2Dev; mandatory* 105*4882a593Smuzhiyun 106*4882a593SmuzhiyunReset the CAN controller (including error counters) 107*4882a593Smuzhiyun 108*4882a593SmuzhiyunPayload Format 109*4882a593Smuzhiyun *empty* 110*4882a593Smuzhiyun 111*4882a593SmuzhiyunUCAN_COMMAND_GET 112*4882a593Smuzhiyun~~~~~~~~~~~~~~~~ 113*4882a593Smuzhiyun 114*4882a593Smuzhiyun*Host2Dev; mandatory* 115*4882a593Smuzhiyun 116*4882a593SmuzhiyunGet Information from the Device 117*4882a593Smuzhiyun 118*4882a593SmuzhiyunSubcommands 119*4882a593Smuzhiyun^^^^^^^^^^^ 120*4882a593Smuzhiyun 121*4882a593SmuzhiyunUCAN_COMMAND_GET_INFO 122*4882a593Smuzhiyun Request the device information structure ``ucan_ctl_payload_t.device_info``. 123*4882a593Smuzhiyun 124*4882a593Smuzhiyun See the ``device_info`` field for details, and 125*4882a593Smuzhiyun ``uapi/linux/can/netlink.h`` for an explanation of the 126*4882a593Smuzhiyun ``can_bittiming fields``. 127*4882a593Smuzhiyun 128*4882a593Smuzhiyun Payload Format 129*4882a593Smuzhiyun ``ucan_ctl_payload_t.device_info`` 130*4882a593Smuzhiyun 131*4882a593SmuzhiyunUCAN_COMMAND_GET_PROTOCOL_VERSION 132*4882a593Smuzhiyun 133*4882a593Smuzhiyun Request the device protocol version 134*4882a593Smuzhiyun ``ucan_ctl_payload_t.protocol_version``. The current protocol version is 3. 135*4882a593Smuzhiyun 136*4882a593Smuzhiyun Payload Format 137*4882a593Smuzhiyun ``ucan_ctl_payload_t.protocol_version`` 138*4882a593Smuzhiyun 139*4882a593Smuzhiyun.. note:: Devices that do not implement this command use the old 140*4882a593Smuzhiyun protocol version 1 141*4882a593Smuzhiyun 142*4882a593SmuzhiyunUCAN_COMMAND_SET_BITTIMING 143*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~ 144*4882a593Smuzhiyun 145*4882a593Smuzhiyun*Host2Dev; mandatory* 146*4882a593Smuzhiyun 147*4882a593SmuzhiyunSetup bittiming by sending the structure 148*4882a593Smuzhiyun``ucan_ctl_payload_t.cmd_set_bittiming`` (see ``struct bittiming`` for 149*4882a593Smuzhiyundetails) 150*4882a593Smuzhiyun 151*4882a593SmuzhiyunPayload Format 152*4882a593Smuzhiyun ``ucan_ctl_payload_t.cmd_set_bittiming``. 153*4882a593Smuzhiyun 154*4882a593SmuzhiyunUCAN_SLEEP/WAKE 155*4882a593Smuzhiyun~~~~~~~~~~~~~~~ 156*4882a593Smuzhiyun 157*4882a593Smuzhiyun*Host2Dev; optional* 158*4882a593Smuzhiyun 159*4882a593SmuzhiyunConfigure sleep and wake modes. Not yet supported by the driver. 160*4882a593Smuzhiyun 161*4882a593SmuzhiyunUCAN_FILTER 162*4882a593Smuzhiyun~~~~~~~~~~~ 163*4882a593Smuzhiyun 164*4882a593Smuzhiyun*Host2Dev; optional* 165*4882a593Smuzhiyun 166*4882a593SmuzhiyunSetup hardware CAN filters. Not yet supported by the driver. 167*4882a593Smuzhiyun 168*4882a593SmuzhiyunAllowed interface commands 169*4882a593Smuzhiyun-------------------------- 170*4882a593Smuzhiyun 171*4882a593Smuzhiyun================== =================== ================== 172*4882a593SmuzhiyunLegal Device State Command New Device State 173*4882a593Smuzhiyun================== =================== ================== 174*4882a593Smuzhiyunstopped SET_BITTIMING stopped 175*4882a593Smuzhiyunstopped START started 176*4882a593Smuzhiyunstarted STOP or RESET stopped 177*4882a593Smuzhiyunstopped STOP or RESET stopped 178*4882a593Smuzhiyunstarted RESTART started 179*4882a593Smuzhiyunany GET *no change* 180*4882a593Smuzhiyun================== =================== ================== 181*4882a593Smuzhiyun 182*4882a593SmuzhiyunIN Message Format 183*4882a593Smuzhiyun================= 184*4882a593Smuzhiyun 185*4882a593SmuzhiyunA data packet on the USB IN endpoint contains one or more 186*4882a593Smuzhiyun``ucan_message_in`` values. If multiple messages are batched in a USB 187*4882a593Smuzhiyundata packet, the ``len`` field can be used to jump to the next 188*4882a593Smuzhiyun``ucan_message_in`` value (take care to sanity-check the ``len`` value 189*4882a593Smuzhiyunagainst the actual data size). 190*4882a593Smuzhiyun 191*4882a593Smuzhiyun.. _can_ucan_in_message_len: 192*4882a593Smuzhiyun 193*4882a593Smuzhiyun``len`` field 194*4882a593Smuzhiyun------------- 195*4882a593Smuzhiyun 196*4882a593SmuzhiyunEach ``ucan_message_in`` must be aligned to a 4-byte boundary (relative 197*4882a593Smuzhiyunto the start of the start of the data buffer). That means that there 198*4882a593Smuzhiyunmay be padding bytes between multiple ``ucan_message_in`` values: 199*4882a593Smuzhiyun 200*4882a593Smuzhiyun.. code:: 201*4882a593Smuzhiyun 202*4882a593Smuzhiyun +----------------------------+ < 0 203*4882a593Smuzhiyun | | 204*4882a593Smuzhiyun | struct ucan_message_in | 205*4882a593Smuzhiyun | | 206*4882a593Smuzhiyun +----------------------------+ < len 207*4882a593Smuzhiyun [padding] 208*4882a593Smuzhiyun +----------------------------+ < round_up(len, 4) 209*4882a593Smuzhiyun | | 210*4882a593Smuzhiyun | struct ucan_message_in | 211*4882a593Smuzhiyun | | 212*4882a593Smuzhiyun +----------------------------+ 213*4882a593Smuzhiyun [...] 214*4882a593Smuzhiyun 215*4882a593Smuzhiyun``type`` field 216*4882a593Smuzhiyun-------------- 217*4882a593Smuzhiyun 218*4882a593SmuzhiyunThe ``type`` field specifies the type of the message. 219*4882a593Smuzhiyun 220*4882a593SmuzhiyunUCAN_IN_RX 221*4882a593Smuzhiyun~~~~~~~~~~ 222*4882a593Smuzhiyun 223*4882a593Smuzhiyun``subtype`` 224*4882a593Smuzhiyun zero 225*4882a593Smuzhiyun 226*4882a593SmuzhiyunData received from the CAN bus (ID + payload). 227*4882a593Smuzhiyun 228*4882a593SmuzhiyunUCAN_IN_TX_COMPLETE 229*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~ 230*4882a593Smuzhiyun 231*4882a593Smuzhiyun``subtype`` 232*4882a593Smuzhiyun zero 233*4882a593Smuzhiyun 234*4882a593SmuzhiyunThe CAN device has sent a message to the CAN bus. It answers with a 235*4882a593Smuzhiyunlist of tuples <echo-ids, flags>. 236*4882a593Smuzhiyun 237*4882a593SmuzhiyunThe echo-id identifies the frame from (echos the id from a previous 238*4882a593SmuzhiyunUCAN_OUT_TX message). The flag indicates the result of the 239*4882a593Smuzhiyuntransmission. Whereas a set Bit 0 indicates success. All other bits 240*4882a593Smuzhiyunare reserved and set to zero. 241*4882a593Smuzhiyun 242*4882a593SmuzhiyunFlow Control 243*4882a593Smuzhiyun------------ 244*4882a593Smuzhiyun 245*4882a593SmuzhiyunWhen receiving CAN messages there is no flow control on the USB 246*4882a593Smuzhiyunbuffer. The driver has to handle inbound message quickly enough to 247*4882a593Smuzhiyunavoid drops. I case the device buffer overflow the condition is 248*4882a593Smuzhiyunreported by sending corresponding error frames (see 249*4882a593Smuzhiyun:ref:`can_ucan_error_handling`) 250*4882a593Smuzhiyun 251*4882a593Smuzhiyun 252*4882a593SmuzhiyunOUT Message Format 253*4882a593Smuzhiyun================== 254*4882a593Smuzhiyun 255*4882a593SmuzhiyunA data packet on the USB OUT endpoint contains one or more ``struct 256*4882a593Smuzhiyunucan_message_out`` values. If multiple messages are batched into one 257*4882a593Smuzhiyundata packet, the device uses the ``len`` field to jump to the next 258*4882a593Smuzhiyunucan_message_out value. Each ucan_message_out must be aligned to 4 259*4882a593Smuzhiyunbytes (relative to the start of the data buffer). The mechanism is 260*4882a593Smuzhiyunsame as described in :ref:`can_ucan_in_message_len`. 261*4882a593Smuzhiyun 262*4882a593Smuzhiyun.. code:: 263*4882a593Smuzhiyun 264*4882a593Smuzhiyun +----------------------------+ < 0 265*4882a593Smuzhiyun | | 266*4882a593Smuzhiyun | struct ucan_message_out | 267*4882a593Smuzhiyun | | 268*4882a593Smuzhiyun +----------------------------+ < len 269*4882a593Smuzhiyun [padding] 270*4882a593Smuzhiyun +----------------------------+ < round_up(len, 4) 271*4882a593Smuzhiyun | | 272*4882a593Smuzhiyun | struct ucan_message_out | 273*4882a593Smuzhiyun | | 274*4882a593Smuzhiyun +----------------------------+ 275*4882a593Smuzhiyun [...] 276*4882a593Smuzhiyun 277*4882a593Smuzhiyun``type`` field 278*4882a593Smuzhiyun-------------- 279*4882a593Smuzhiyun 280*4882a593SmuzhiyunIn protocol version 3 only ``UCAN_OUT_TX`` is defined, others are used 281*4882a593Smuzhiyunonly by legacy devices (protocol version 1). 282*4882a593Smuzhiyun 283*4882a593SmuzhiyunUCAN_OUT_TX 284*4882a593Smuzhiyun~~~~~~~~~~~ 285*4882a593Smuzhiyun``subtype`` 286*4882a593Smuzhiyun echo id to be replied within a CAN_IN_TX_COMPLETE message 287*4882a593Smuzhiyun 288*4882a593SmuzhiyunTransmit a CAN frame. (parameters: ``id``, ``data``) 289*4882a593Smuzhiyun 290*4882a593SmuzhiyunFlow Control 291*4882a593Smuzhiyun------------ 292*4882a593Smuzhiyun 293*4882a593SmuzhiyunWhen the device outbound buffers are full it starts sending *NAKs* on 294*4882a593Smuzhiyunthe *OUT* pipe until more buffers are available. The driver stops the 295*4882a593Smuzhiyunqueue when a certain threshold of out packets are incomplete. 296*4882a593Smuzhiyun 297*4882a593Smuzhiyun.. _can_ucan_error_handling: 298*4882a593Smuzhiyun 299*4882a593SmuzhiyunCAN Error Handling 300*4882a593Smuzhiyun================== 301*4882a593Smuzhiyun 302*4882a593SmuzhiyunIf error reporting is turned on the device encodes errors into CAN 303*4882a593Smuzhiyunerror frames (see ``uapi/linux/can/error.h``) and sends it using the 304*4882a593SmuzhiyunIN endpoint. The driver updates its error statistics and forwards 305*4882a593Smuzhiyunit. 306*4882a593Smuzhiyun 307*4882a593SmuzhiyunAlthough UCAN devices can suppress error frames completely, in Linux 308*4882a593Smuzhiyunthe driver is always interested. Hence, the device is always started with 309*4882a593Smuzhiyunthe ``UCAN_MODE_BERR_REPORT`` set. Filtering those messages for the 310*4882a593Smuzhiyunuser space is done by the driver. 311*4882a593Smuzhiyun 312*4882a593SmuzhiyunBus OFF 313*4882a593Smuzhiyun------- 314*4882a593Smuzhiyun 315*4882a593Smuzhiyun- The device does not recover from bus of automatically. 316*4882a593Smuzhiyun- Bus OFF is indicated by an error frame (see ``uapi/linux/can/error.h``) 317*4882a593Smuzhiyun- Bus OFF recovery is started by ``UCAN_COMMAND_RESTART`` 318*4882a593Smuzhiyun- Once Bus OFF recover is completed the device sends an error frame 319*4882a593Smuzhiyun indicating that it is on ERROR-ACTIVE state. 320*4882a593Smuzhiyun- During Bus OFF no frames are sent by the device. 321*4882a593Smuzhiyun- During Bus OFF transmission requests from the host are completed 322*4882a593Smuzhiyun immediately with the success bit left unset. 323*4882a593Smuzhiyun 324*4882a593SmuzhiyunExample Conversation 325*4882a593Smuzhiyun==================== 326*4882a593Smuzhiyun 327*4882a593Smuzhiyun#) Device is connected to USB 328*4882a593Smuzhiyun#) Host sends command ``UCAN_COMMAND_RESET``, subcmd 0 329*4882a593Smuzhiyun#) Host sends command ``UCAN_COMMAND_GET``, subcmd ``UCAN_COMMAND_GET_INFO`` 330*4882a593Smuzhiyun#) Device sends ``UCAN_IN_DEVICE_INFO`` 331*4882a593Smuzhiyun#) Host sends command ``UCAN_OUT_SET_BITTIMING`` 332*4882a593Smuzhiyun#) Host sends command ``UCAN_COMMAND_START``, subcmd 0, mode ``UCAN_MODE_BERR_REPORT`` 333