w1-netlink.rst 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. ===============================================
  2. Userspace communication protocol over connector
  3. ===============================================
  4. Message types
  5. =============
  6. There are three types of messages between w1 core and userspace:
  7. 1. Events. They are generated each time a new master or slave device
  8. is found either due to automatic or requested search.
  9. 2. Userspace commands.
  10. 3. Replies to userspace commands.
  11. Protocol
  12. ========
  13. ::
  14. [struct cn_msg] - connector header.
  15. Its length field is equal to size of the attached data
  16. [struct w1_netlink_msg] - w1 netlink header.
  17. __u8 type - message type.
  18. W1_LIST_MASTERS
  19. list current bus masters
  20. W1_SLAVE_ADD/W1_SLAVE_REMOVE
  21. slave add/remove events
  22. W1_MASTER_ADD/W1_MASTER_REMOVE
  23. master add/remove events
  24. W1_MASTER_CMD
  25. userspace command for bus master
  26. device (search/alarm search)
  27. W1_SLAVE_CMD
  28. userspace command for slave device
  29. (read/write/touch)
  30. __u8 status - error indication from kernel
  31. __u16 len - size of data attached to this header data
  32. union {
  33. __u8 id[8]; - slave unique device id
  34. struct w1_mst {
  35. __u32 id; - master's id
  36. __u32 res; - reserved
  37. } mst;
  38. } id;
  39. [struct w1_netlink_cmd] - command for given master or slave device.
  40. __u8 cmd - command opcode.
  41. W1_CMD_READ - read command
  42. W1_CMD_WRITE - write command
  43. W1_CMD_SEARCH - search command
  44. W1_CMD_ALARM_SEARCH - alarm search command
  45. W1_CMD_TOUCH - touch command
  46. (write and sample data back to userspace)
  47. W1_CMD_RESET - send bus reset
  48. W1_CMD_SLAVE_ADD - add slave to kernel list
  49. W1_CMD_SLAVE_REMOVE - remove slave from kernel list
  50. W1_CMD_LIST_SLAVES - get slaves list from kernel
  51. __u8 res - reserved
  52. __u16 len - length of data for this command
  53. For read command data must be allocated like for write command
  54. __u8 data[0] - data for this command
  55. Each connector message can include one or more w1_netlink_msg with
  56. zero or more attached w1_netlink_cmd messages.
  57. For event messages there are no w1_netlink_cmd embedded structures,
  58. only connector header and w1_netlink_msg strucutre with "len" field
  59. being zero and filled type (one of event types) and id:
  60. either 8 bytes of slave unique id in host order,
  61. or master's id, which is assigned to bus master device
  62. when it is added to w1 core.
  63. Currently replies to userspace commands are only generated for read
  64. command request. One reply is generated exactly for one w1_netlink_cmd
  65. read request. Replies are not combined when sent - i.e. typical reply
  66. messages looks like the following::
  67. [cn_msg][w1_netlink_msg][w1_netlink_cmd]
  68. cn_msg.len = sizeof(struct w1_netlink_msg) +
  69. sizeof(struct w1_netlink_cmd) +
  70. cmd->len;
  71. w1_netlink_msg.len = sizeof(struct w1_netlink_cmd) + cmd->len;
  72. w1_netlink_cmd.len = cmd->len;
  73. Replies to W1_LIST_MASTERS should send a message back to the userspace
  74. which will contain list of all registered master ids in the following
  75. format::
  76. cn_msg (CN_W1_IDX.CN_W1_VAL as id, len is equal to sizeof(struct
  77. w1_netlink_msg) plus number of masters multiplied by 4)
  78. w1_netlink_msg (type: W1_LIST_MASTERS, len is equal to
  79. number of masters multiplied by 4 (u32 size))
  80. id0 ... idN
  81. Each message is at most 4k in size, so if number of master devices
  82. exceeds this, it will be split into several messages.
  83. W1 search and alarm search commands.
  84. request::
  85. [cn_msg]
  86. [w1_netlink_msg type = W1_MASTER_CMD
  87. id is equal to the bus master id to use for searching]
  88. [w1_netlink_cmd cmd = W1_CMD_SEARCH or W1_CMD_ALARM_SEARCH]
  89. reply::
  90. [cn_msg, ack = 1 and increasing, 0 means the last message,
  91. seq is equal to the request seq]
  92. [w1_netlink_msg type = W1_MASTER_CMD]
  93. [w1_netlink_cmd cmd = W1_CMD_SEARCH or W1_CMD_ALARM_SEARCH
  94. len is equal to number of IDs multiplied by 8]
  95. [64bit-id0 ... 64bit-idN]
  96. Length in each header corresponds to the size of the data behind it, so
  97. w1_netlink_cmd->len = N * 8; where N is number of IDs in this message.
  98. Can be zero.
  99. ::
  100. w1_netlink_msg->len = sizeof(struct w1_netlink_cmd) + N * 8;
  101. cn_msg->len = sizeof(struct w1_netlink_msg) +
  102. sizeof(struct w1_netlink_cmd) +
  103. N*8;
  104. W1 reset command::
  105. [cn_msg]
  106. [w1_netlink_msg type = W1_MASTER_CMD
  107. id is equal to the bus master id to use for searching]
  108. [w1_netlink_cmd cmd = W1_CMD_RESET]
  109. Command status replies
  110. ======================
  111. Each command (either root, master or slave with or without w1_netlink_cmd
  112. structure) will be 'acked' by the w1 core. Format of the reply is the same
  113. as request message except that length parameters do not account for data
  114. requested by the user, i.e. read/write/touch IO requests will not contain
  115. data, so w1_netlink_cmd.len will be 0, w1_netlink_msg.len will be size
  116. of the w1_netlink_cmd structure and cn_msg.len will be equal to the sum
  117. of the sizeof(struct w1_netlink_msg) and sizeof(struct w1_netlink_cmd).
  118. If reply is generated for master or root command (which do not have
  119. w1_netlink_cmd attached), reply will contain only cn_msg and w1_netlink_msg
  120. structures.
  121. w1_netlink_msg.status field will carry positive error value
  122. (EINVAL for example) or zero in case of success.
  123. All other fields in every structure will mirror the same parameters in the
  124. request message (except lengths as described above).
  125. Status reply is generated for every w1_netlink_cmd embedded in the
  126. w1_netlink_msg, if there are no w1_netlink_cmd structures,
  127. reply will be generated for the w1_netlink_msg.
  128. All w1_netlink_cmd command structures are handled in every w1_netlink_msg,
  129. even if there were errors, only length mismatch interrupts message processing.
  130. Operation steps in w1 core when new command is received
  131. =======================================================
  132. When new message (w1_netlink_msg) is received w1 core detects if it is
  133. master or slave request, according to w1_netlink_msg.type field.
  134. Then master or slave device is searched for.
  135. When found, master device (requested or those one on where slave device
  136. is found) is locked. If slave command is requested, then reset/select
  137. procedure is started to select given device.
  138. Then all requested in w1_netlink_msg operations are performed one by one.
  139. If command requires reply (like read command) it is sent on command completion.
  140. When all commands (w1_netlink_cmd) are processed master device is unlocked
  141. and next w1_netlink_msg header processing started.
  142. Connector [1] specific documentation
  143. ====================================
  144. Each connector message includes two u32 fields as "address".
  145. w1 uses CN_W1_IDX and CN_W1_VAL defined in include/linux/connector.h header.
  146. Each message also includes sequence and acknowledge numbers.
  147. Sequence number for event messages is appropriate bus master sequence number
  148. increased with each event message sent "through" this master.
  149. Sequence number for userspace requests is set by userspace application.
  150. Sequence number for reply is the same as was in request, and
  151. acknowledge number is set to seq+1.
  152. Additional documentation, source code examples
  153. ==============================================
  154. 1. Documentation/driver-api/connector.rst
  155. 2. http://www.ioremap.net/archive/w1
  156. This archive includes userspace application w1d.c which uses
  157. read/write/search commands for all master/slave devices found on the bus.