w1.netlink 5.5 KB

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