1*4882a593Smuzhiyun=============================================== 2*4882a593SmuzhiyunUserspace communication protocol over connector 3*4882a593Smuzhiyun=============================================== 4*4882a593Smuzhiyun 5*4882a593SmuzhiyunMessage types 6*4882a593Smuzhiyun============= 7*4882a593Smuzhiyun 8*4882a593SmuzhiyunThere are three types of messages between w1 core and userspace: 9*4882a593Smuzhiyun 10*4882a593Smuzhiyun1. Events. They are generated each time a new master or slave device 11*4882a593Smuzhiyun is found either due to automatic or requested search. 12*4882a593Smuzhiyun2. Userspace commands. 13*4882a593Smuzhiyun3. Replies to userspace commands. 14*4882a593Smuzhiyun 15*4882a593Smuzhiyun 16*4882a593SmuzhiyunProtocol 17*4882a593Smuzhiyun======== 18*4882a593Smuzhiyun 19*4882a593Smuzhiyun:: 20*4882a593Smuzhiyun 21*4882a593Smuzhiyun [struct cn_msg] - connector header. 22*4882a593Smuzhiyun Its length field is equal to size of the attached data 23*4882a593Smuzhiyun [struct w1_netlink_msg] - w1 netlink header. 24*4882a593Smuzhiyun __u8 type - message type. 25*4882a593Smuzhiyun W1_LIST_MASTERS 26*4882a593Smuzhiyun list current bus masters 27*4882a593Smuzhiyun W1_SLAVE_ADD/W1_SLAVE_REMOVE 28*4882a593Smuzhiyun slave add/remove events 29*4882a593Smuzhiyun W1_MASTER_ADD/W1_MASTER_REMOVE 30*4882a593Smuzhiyun master add/remove events 31*4882a593Smuzhiyun W1_MASTER_CMD 32*4882a593Smuzhiyun userspace command for bus master 33*4882a593Smuzhiyun device (search/alarm search) 34*4882a593Smuzhiyun W1_SLAVE_CMD 35*4882a593Smuzhiyun userspace command for slave device 36*4882a593Smuzhiyun (read/write/touch) 37*4882a593Smuzhiyun __u8 status - error indication from kernel 38*4882a593Smuzhiyun __u16 len - size of data attached to this header data 39*4882a593Smuzhiyun union { 40*4882a593Smuzhiyun __u8 id[8]; - slave unique device id 41*4882a593Smuzhiyun struct w1_mst { 42*4882a593Smuzhiyun __u32 id; - master's id 43*4882a593Smuzhiyun __u32 res; - reserved 44*4882a593Smuzhiyun } mst; 45*4882a593Smuzhiyun } id; 46*4882a593Smuzhiyun 47*4882a593Smuzhiyun [struct w1_netlink_cmd] - command for given master or slave device. 48*4882a593Smuzhiyun __u8 cmd - command opcode. 49*4882a593Smuzhiyun W1_CMD_READ - read command 50*4882a593Smuzhiyun W1_CMD_WRITE - write command 51*4882a593Smuzhiyun W1_CMD_SEARCH - search command 52*4882a593Smuzhiyun W1_CMD_ALARM_SEARCH - alarm search command 53*4882a593Smuzhiyun W1_CMD_TOUCH - touch command 54*4882a593Smuzhiyun (write and sample data back to userspace) 55*4882a593Smuzhiyun W1_CMD_RESET - send bus reset 56*4882a593Smuzhiyun W1_CMD_SLAVE_ADD - add slave to kernel list 57*4882a593Smuzhiyun W1_CMD_SLAVE_REMOVE - remove slave from kernel list 58*4882a593Smuzhiyun W1_CMD_LIST_SLAVES - get slaves list from kernel 59*4882a593Smuzhiyun __u8 res - reserved 60*4882a593Smuzhiyun __u16 len - length of data for this command 61*4882a593Smuzhiyun For read command data must be allocated like for write command 62*4882a593Smuzhiyun __u8 data[0] - data for this command 63*4882a593Smuzhiyun 64*4882a593Smuzhiyun 65*4882a593SmuzhiyunEach connector message can include one or more w1_netlink_msg with 66*4882a593Smuzhiyunzero or more attached w1_netlink_cmd messages. 67*4882a593Smuzhiyun 68*4882a593SmuzhiyunFor event messages there are no w1_netlink_cmd embedded structures, 69*4882a593Smuzhiyunonly connector header and w1_netlink_msg strucutre with "len" field 70*4882a593Smuzhiyunbeing zero and filled type (one of event types) and id: 71*4882a593Smuzhiyuneither 8 bytes of slave unique id in host order, 72*4882a593Smuzhiyunor master's id, which is assigned to bus master device 73*4882a593Smuzhiyunwhen it is added to w1 core. 74*4882a593Smuzhiyun 75*4882a593SmuzhiyunCurrently replies to userspace commands are only generated for read 76*4882a593Smuzhiyuncommand request. One reply is generated exactly for one w1_netlink_cmd 77*4882a593Smuzhiyunread request. Replies are not combined when sent - i.e. typical reply 78*4882a593Smuzhiyunmessages looks like the following:: 79*4882a593Smuzhiyun 80*4882a593Smuzhiyun [cn_msg][w1_netlink_msg][w1_netlink_cmd] 81*4882a593Smuzhiyun cn_msg.len = sizeof(struct w1_netlink_msg) + 82*4882a593Smuzhiyun sizeof(struct w1_netlink_cmd) + 83*4882a593Smuzhiyun cmd->len; 84*4882a593Smuzhiyun w1_netlink_msg.len = sizeof(struct w1_netlink_cmd) + cmd->len; 85*4882a593Smuzhiyun w1_netlink_cmd.len = cmd->len; 86*4882a593Smuzhiyun 87*4882a593SmuzhiyunReplies to W1_LIST_MASTERS should send a message back to the userspace 88*4882a593Smuzhiyunwhich will contain list of all registered master ids in the following 89*4882a593Smuzhiyunformat:: 90*4882a593Smuzhiyun 91*4882a593Smuzhiyun cn_msg (CN_W1_IDX.CN_W1_VAL as id, len is equal to sizeof(struct 92*4882a593Smuzhiyun w1_netlink_msg) plus number of masters multiplied by 4) 93*4882a593Smuzhiyun w1_netlink_msg (type: W1_LIST_MASTERS, len is equal to 94*4882a593Smuzhiyun number of masters multiplied by 4 (u32 size)) 95*4882a593Smuzhiyun id0 ... idN 96*4882a593Smuzhiyun 97*4882a593SmuzhiyunEach message is at most 4k in size, so if number of master devices 98*4882a593Smuzhiyunexceeds this, it will be split into several messages. 99*4882a593Smuzhiyun 100*4882a593SmuzhiyunW1 search and alarm search commands. 101*4882a593Smuzhiyun 102*4882a593Smuzhiyunrequest:: 103*4882a593Smuzhiyun 104*4882a593Smuzhiyun [cn_msg] 105*4882a593Smuzhiyun [w1_netlink_msg type = W1_MASTER_CMD 106*4882a593Smuzhiyun id is equal to the bus master id to use for searching] 107*4882a593Smuzhiyun [w1_netlink_cmd cmd = W1_CMD_SEARCH or W1_CMD_ALARM_SEARCH] 108*4882a593Smuzhiyun 109*4882a593Smuzhiyunreply:: 110*4882a593Smuzhiyun 111*4882a593Smuzhiyun [cn_msg, ack = 1 and increasing, 0 means the last message, 112*4882a593Smuzhiyun seq is equal to the request seq] 113*4882a593Smuzhiyun [w1_netlink_msg type = W1_MASTER_CMD] 114*4882a593Smuzhiyun [w1_netlink_cmd cmd = W1_CMD_SEARCH or W1_CMD_ALARM_SEARCH 115*4882a593Smuzhiyun len is equal to number of IDs multiplied by 8] 116*4882a593Smuzhiyun [64bit-id0 ... 64bit-idN] 117*4882a593Smuzhiyun 118*4882a593SmuzhiyunLength in each header corresponds to the size of the data behind it, so 119*4882a593Smuzhiyunw1_netlink_cmd->len = N * 8; where N is number of IDs in this message. 120*4882a593SmuzhiyunCan be zero. 121*4882a593Smuzhiyun 122*4882a593Smuzhiyun:: 123*4882a593Smuzhiyun 124*4882a593Smuzhiyun w1_netlink_msg->len = sizeof(struct w1_netlink_cmd) + N * 8; 125*4882a593Smuzhiyun cn_msg->len = sizeof(struct w1_netlink_msg) + 126*4882a593Smuzhiyun sizeof(struct w1_netlink_cmd) + 127*4882a593Smuzhiyun N*8; 128*4882a593Smuzhiyun 129*4882a593SmuzhiyunW1 reset command:: 130*4882a593Smuzhiyun 131*4882a593Smuzhiyun [cn_msg] 132*4882a593Smuzhiyun [w1_netlink_msg type = W1_MASTER_CMD 133*4882a593Smuzhiyun id is equal to the bus master id to use for searching] 134*4882a593Smuzhiyun [w1_netlink_cmd cmd = W1_CMD_RESET] 135*4882a593Smuzhiyun 136*4882a593Smuzhiyun 137*4882a593SmuzhiyunCommand status replies 138*4882a593Smuzhiyun====================== 139*4882a593Smuzhiyun 140*4882a593SmuzhiyunEach command (either root, master or slave with or without w1_netlink_cmd 141*4882a593Smuzhiyunstructure) will be 'acked' by the w1 core. Format of the reply is the same 142*4882a593Smuzhiyunas request message except that length parameters do not account for data 143*4882a593Smuzhiyunrequested by the user, i.e. read/write/touch IO requests will not contain 144*4882a593Smuzhiyundata, so w1_netlink_cmd.len will be 0, w1_netlink_msg.len will be size 145*4882a593Smuzhiyunof the w1_netlink_cmd structure and cn_msg.len will be equal to the sum 146*4882a593Smuzhiyunof the sizeof(struct w1_netlink_msg) and sizeof(struct w1_netlink_cmd). 147*4882a593SmuzhiyunIf reply is generated for master or root command (which do not have 148*4882a593Smuzhiyunw1_netlink_cmd attached), reply will contain only cn_msg and w1_netlink_msg 149*4882a593Smuzhiyunstructures. 150*4882a593Smuzhiyun 151*4882a593Smuzhiyunw1_netlink_msg.status field will carry positive error value 152*4882a593Smuzhiyun(EINVAL for example) or zero in case of success. 153*4882a593Smuzhiyun 154*4882a593SmuzhiyunAll other fields in every structure will mirror the same parameters in the 155*4882a593Smuzhiyunrequest message (except lengths as described above). 156*4882a593Smuzhiyun 157*4882a593SmuzhiyunStatus reply is generated for every w1_netlink_cmd embedded in the 158*4882a593Smuzhiyunw1_netlink_msg, if there are no w1_netlink_cmd structures, 159*4882a593Smuzhiyunreply will be generated for the w1_netlink_msg. 160*4882a593Smuzhiyun 161*4882a593SmuzhiyunAll w1_netlink_cmd command structures are handled in every w1_netlink_msg, 162*4882a593Smuzhiyuneven if there were errors, only length mismatch interrupts message processing. 163*4882a593Smuzhiyun 164*4882a593Smuzhiyun 165*4882a593SmuzhiyunOperation steps in w1 core when new command is received 166*4882a593Smuzhiyun======================================================= 167*4882a593Smuzhiyun 168*4882a593SmuzhiyunWhen new message (w1_netlink_msg) is received w1 core detects if it is 169*4882a593Smuzhiyunmaster or slave request, according to w1_netlink_msg.type field. 170*4882a593SmuzhiyunThen master or slave device is searched for. 171*4882a593SmuzhiyunWhen found, master device (requested or those one on where slave device 172*4882a593Smuzhiyunis found) is locked. If slave command is requested, then reset/select 173*4882a593Smuzhiyunprocedure is started to select given device. 174*4882a593Smuzhiyun 175*4882a593SmuzhiyunThen all requested in w1_netlink_msg operations are performed one by one. 176*4882a593SmuzhiyunIf command requires reply (like read command) it is sent on command completion. 177*4882a593Smuzhiyun 178*4882a593SmuzhiyunWhen all commands (w1_netlink_cmd) are processed master device is unlocked 179*4882a593Smuzhiyunand next w1_netlink_msg header processing started. 180*4882a593Smuzhiyun 181*4882a593Smuzhiyun 182*4882a593SmuzhiyunConnector [1] specific documentation 183*4882a593Smuzhiyun==================================== 184*4882a593Smuzhiyun 185*4882a593SmuzhiyunEach connector message includes two u32 fields as "address". 186*4882a593Smuzhiyunw1 uses CN_W1_IDX and CN_W1_VAL defined in include/linux/connector.h header. 187*4882a593SmuzhiyunEach message also includes sequence and acknowledge numbers. 188*4882a593SmuzhiyunSequence number for event messages is appropriate bus master sequence number 189*4882a593Smuzhiyunincreased with each event message sent "through" this master. 190*4882a593SmuzhiyunSequence number for userspace requests is set by userspace application. 191*4882a593SmuzhiyunSequence number for reply is the same as was in request, and 192*4882a593Smuzhiyunacknowledge number is set to seq+1. 193*4882a593Smuzhiyun 194*4882a593Smuzhiyun 195*4882a593SmuzhiyunAdditional documentation, source code examples 196*4882a593Smuzhiyun============================================== 197*4882a593Smuzhiyun 198*4882a593Smuzhiyun1. Documentation/driver-api/connector.rst 199*4882a593Smuzhiyun2. http://www.ioremap.net/archive/w1 200*4882a593Smuzhiyun 201*4882a593Smuzhiyun This archive includes userspace application w1d.c which uses 202*4882a593Smuzhiyun read/write/search commands for all master/slave devices found on the bus. 203