can_ucan_protocol.rst 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. =================
  2. The UCAN Protocol
  3. =================
  4. UCAN is the protocol used by the microcontroller-based USB-CAN
  5. adapter that is integrated on System-on-Modules from Theobroma Systems
  6. and that is also available as a standalone USB stick.
  7. The UCAN protocol has been designed to be hardware-independent.
  8. It is modeled closely after how Linux represents CAN devices
  9. internally. All multi-byte integers are encoded as Little Endian.
  10. All structures mentioned in this document are defined in
  11. ``drivers/net/can/usb/ucan.c``.
  12. USB Endpoints
  13. =============
  14. UCAN devices use three USB endpoints:
  15. CONTROL endpoint
  16. The driver sends device management commands on this endpoint
  17. IN endpoint
  18. The device sends CAN data frames and CAN error frames
  19. OUT endpoint
  20. The driver sends CAN data frames on the out endpoint
  21. CONTROL Messages
  22. ================
  23. UCAN devices are configured using vendor requests on the control pipe.
  24. To support multiple CAN interfaces in a single USB device all
  25. configuration commands target the corresponding interface in the USB
  26. descriptor.
  27. The driver uses ``ucan_ctrl_command_in/out`` and
  28. ``ucan_device_request_in`` to deliver commands to the device.
  29. Setup Packet
  30. ------------
  31. ================= =====================================================
  32. ``bmRequestType`` Direction | Vendor | (Interface or Device)
  33. ``bRequest`` Command Number
  34. ``wValue`` Subcommand Number (16 Bit) or 0 if not used
  35. ``wIndex`` USB Interface Index (0 for device commands)
  36. ``wLength`` * Host to Device - Number of bytes to transmit
  37. * Device to Host - Maximum Number of bytes to
  38. receive. If the device send less. Commom ZLP
  39. semantics are used.
  40. ================= =====================================================
  41. Error Handling
  42. --------------
  43. The device indicates failed control commands by stalling the
  44. pipe.
  45. Device Commands
  46. ---------------
  47. UCAN_DEVICE_GET_FW_STRING
  48. ~~~~~~~~~~~~~~~~~~~~~~~~~
  49. *Dev2Host; optional*
  50. Request the device firmware string.
  51. Interface Commands
  52. ------------------
  53. UCAN_COMMAND_START
  54. ~~~~~~~~~~~~~~~~~~
  55. *Host2Dev; mandatory*
  56. Bring the CAN interface up.
  57. Payload Format
  58. ``ucan_ctl_payload_t.cmd_start``
  59. ==== ============================
  60. mode or mask of ``UCAN_MODE_*``
  61. ==== ============================
  62. UCAN_COMMAND_STOP
  63. ~~~~~~~~~~~~~~~~~~
  64. *Host2Dev; mandatory*
  65. Stop the CAN interface
  66. Payload Format
  67. *empty*
  68. UCAN_COMMAND_RESET
  69. ~~~~~~~~~~~~~~~~~~
  70. *Host2Dev; mandatory*
  71. Reset the CAN controller (including error counters)
  72. Payload Format
  73. *empty*
  74. UCAN_COMMAND_GET
  75. ~~~~~~~~~~~~~~~~
  76. *Host2Dev; mandatory*
  77. Get Information from the Device
  78. Subcommands
  79. ^^^^^^^^^^^
  80. UCAN_COMMAND_GET_INFO
  81. Request the device information structure ``ucan_ctl_payload_t.device_info``.
  82. See the ``device_info`` field for details, and
  83. ``uapi/linux/can/netlink.h`` for an explanation of the
  84. ``can_bittiming fields``.
  85. Payload Format
  86. ``ucan_ctl_payload_t.device_info``
  87. UCAN_COMMAND_GET_PROTOCOL_VERSION
  88. Request the device protocol version
  89. ``ucan_ctl_payload_t.protocol_version``. The current protocol version is 3.
  90. Payload Format
  91. ``ucan_ctl_payload_t.protocol_version``
  92. .. note:: Devices that do not implement this command use the old
  93. protocol version 1
  94. UCAN_COMMAND_SET_BITTIMING
  95. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  96. *Host2Dev; mandatory*
  97. Setup bittiming by sending the structure
  98. ``ucan_ctl_payload_t.cmd_set_bittiming`` (see ``struct bittiming`` for
  99. details)
  100. Payload Format
  101. ``ucan_ctl_payload_t.cmd_set_bittiming``.
  102. UCAN_SLEEP/WAKE
  103. ~~~~~~~~~~~~~~~
  104. *Host2Dev; optional*
  105. Configure sleep and wake modes. Not yet supported by the driver.
  106. UCAN_FILTER
  107. ~~~~~~~~~~~
  108. *Host2Dev; optional*
  109. Setup hardware CAN filters. Not yet supported by the driver.
  110. Allowed interface commands
  111. --------------------------
  112. ================== =================== ==================
  113. Legal Device State Command New Device State
  114. ================== =================== ==================
  115. stopped SET_BITTIMING stopped
  116. stopped START started
  117. started STOP or RESET stopped
  118. stopped STOP or RESET stopped
  119. started RESTART started
  120. any GET *no change*
  121. ================== =================== ==================
  122. IN Message Format
  123. =================
  124. A data packet on the USB IN endpoint contains one or more
  125. ``ucan_message_in`` values. If multiple messages are batched in a USB
  126. data packet, the ``len`` field can be used to jump to the next
  127. ``ucan_message_in`` value (take care to sanity-check the ``len`` value
  128. against the actual data size).
  129. .. _can_ucan_in_message_len:
  130. ``len`` field
  131. -------------
  132. Each ``ucan_message_in`` must be aligned to a 4-byte boundary (relative
  133. to the start of the start of the data buffer). That means that there
  134. may be padding bytes between multiple ``ucan_message_in`` values:
  135. .. code::
  136. +----------------------------+ < 0
  137. | |
  138. | struct ucan_message_in |
  139. | |
  140. +----------------------------+ < len
  141. [padding]
  142. +----------------------------+ < round_up(len, 4)
  143. | |
  144. | struct ucan_message_in |
  145. | |
  146. +----------------------------+
  147. [...]
  148. ``type`` field
  149. --------------
  150. The ``type`` field specifies the type of the message.
  151. UCAN_IN_RX
  152. ~~~~~~~~~~
  153. ``subtype``
  154. zero
  155. Data received from the CAN bus (ID + payload).
  156. UCAN_IN_TX_COMPLETE
  157. ~~~~~~~~~~~~~~~~~~~
  158. ``subtype``
  159. zero
  160. The CAN device has sent a message to the CAN bus. It answers with a
  161. list of tuples <echo-ids, flags>.
  162. The echo-id identifies the frame from (echos the id from a previous
  163. UCAN_OUT_TX message). The flag indicates the result of the
  164. transmission. Whereas a set Bit 0 indicates success. All other bits
  165. are reserved and set to zero.
  166. Flow Control
  167. ------------
  168. When receiving CAN messages there is no flow control on the USB
  169. buffer. The driver has to handle inbound message quickly enough to
  170. avoid drops. I case the device buffer overflow the condition is
  171. reported by sending corresponding error frames (see
  172. :ref:`can_ucan_error_handling`)
  173. OUT Message Format
  174. ==================
  175. A data packet on the USB OUT endpoint contains one or more ``struct
  176. ucan_message_out`` values. If multiple messages are batched into one
  177. data packet, the device uses the ``len`` field to jump to the next
  178. ucan_message_out value. Each ucan_message_out must be aligned to 4
  179. bytes (relative to the start of the data buffer). The mechanism is
  180. same as described in :ref:`can_ucan_in_message_len`.
  181. .. code::
  182. +----------------------------+ < 0
  183. | |
  184. | struct ucan_message_out |
  185. | |
  186. +----------------------------+ < len
  187. [padding]
  188. +----------------------------+ < round_up(len, 4)
  189. | |
  190. | struct ucan_message_out |
  191. | |
  192. +----------------------------+
  193. [...]
  194. ``type`` field
  195. --------------
  196. In protocol version 3 only ``UCAN_OUT_TX`` is defined, others are used
  197. only by legacy devices (protocol version 1).
  198. UCAN_OUT_TX
  199. ~~~~~~~~~~~
  200. ``subtype``
  201. echo id to be replied within a CAN_IN_TX_COMPLETE message
  202. Transmit a CAN frame. (parameters: ``id``, ``data``)
  203. Flow Control
  204. ------------
  205. When the device outbound buffers are full it starts sending *NAKs* on
  206. the *OUT* pipe until more buffers are available. The driver stops the
  207. queue when a certain threshold of out packets are incomplete.
  208. .. _can_ucan_error_handling:
  209. CAN Error Handling
  210. ==================
  211. If error reporting is turned on the device encodes errors into CAN
  212. error frames (see ``uapi/linux/can/error.h``) and sends it using the
  213. IN endpoint. The driver updates its error statistics and forwards
  214. it.
  215. Although UCAN devices can suppress error frames completely, in Linux
  216. the driver is always interested. Hence, the device is always started with
  217. the ``UCAN_MODE_BERR_REPORT`` set. Filtering those messages for the
  218. user space is done by the driver.
  219. Bus OFF
  220. -------
  221. - The device does not recover from bus of automatically.
  222. - Bus OFF is indicated by an error frame (see ``uapi/linux/can/error.h``)
  223. - Bus OFF recovery is started by ``UCAN_COMMAND_RESTART``
  224. - Once Bus OFF recover is completed the device sends an error frame
  225. indicating that it is on ERROR-ACTIVE state.
  226. - During Bus OFF no frames are sent by the device.
  227. - During Bus OFF transmission requests from the host are completed
  228. immediately with the success bit left unset.
  229. Example Conversation
  230. ====================
  231. #) Device is connected to USB
  232. #) Host sends command ``UCAN_COMMAND_RESET``, subcmd 0
  233. #) Host sends command ``UCAN_COMMAND_GET``, subcmd ``UCAN_COMMAND_GET_INFO``
  234. #) Device sends ``UCAN_IN_DEVICE_INFO``
  235. #) Host sends command ``UCAN_OUT_SET_BITTIMING``
  236. #) Host sends command ``UCAN_COMMAND_START``, subcmd 0, mode ``UCAN_MODE_BERR_REPORT``