Thomas Gleixner | c942fdd | 2019-05-27 08:55:06 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Evgeniy Polyakov | a801876 | 2011-08-25 15:59:06 -0700 | [diff] [blame] | 3 | * Copyright (c) 2003 Evgeniy Polyakov <zbr@ioremap.net> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 6 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | #include <linux/skbuff.h> |
| 8 | #include <linux/netlink.h> |
Evgeniy Polyakov | 1200337 | 2006-03-23 19:11:58 +0300 | [diff] [blame] | 9 | #include <linux/connector.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | |
Andrew F. Davis | de0d6db | 2017-06-05 08:52:08 -0500 | [diff] [blame] | 11 | #include "w1_internal.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include "w1_netlink.h" |
| 13 | |
Evgeniy Polyakov | 46e07f6 | 2006-04-03 12:01:12 +0400 | [diff] [blame] | 14 | #if defined(CONFIG_W1_CON) && (defined(CONFIG_CONNECTOR) || (defined(CONFIG_CONNECTOR_MODULE) && defined(CONFIG_W1_MODULE))) |
David Fries | 8a0427d | 2014-04-08 22:37:09 -0500 | [diff] [blame] | 15 | |
David Fries | 8a0427d | 2014-04-08 22:37:09 -0500 | [diff] [blame] | 16 | /* Bundle together everything required to process a request in one memory |
| 17 | * allocation. |
| 18 | */ |
| 19 | struct w1_cb_block { |
| 20 | atomic_t refcnt; |
| 21 | u32 portid; /* Sending process port ID */ |
| 22 | /* maximum value for first_cn->len */ |
| 23 | u16 maxlen; |
| 24 | /* pointers to building up the reply message */ |
| 25 | struct cn_msg *first_cn; /* fixed once the structure is populated */ |
| 26 | struct cn_msg *cn; /* advances as cn_msg is appeneded */ |
| 27 | struct w1_netlink_msg *msg; /* advances as w1_netlink_msg is appened */ |
| 28 | struct w1_netlink_cmd *cmd; /* advances as cmds are appened */ |
| 29 | struct w1_netlink_msg *cur_msg; /* currently message being processed */ |
| 30 | /* copy of the original request follows */ |
| 31 | struct cn_msg request_cn; |
| 32 | /* followed by variable length: |
| 33 | * cn_msg, data (w1_netlink_msg and w1_netlink_cmd) |
| 34 | * one or more struct w1_cb_node |
| 35 | * reply first_cn, data (w1_netlink_msg and w1_netlink_cmd) |
| 36 | */ |
| 37 | }; |
| 38 | struct w1_cb_node { |
| 39 | struct w1_async_cmd async; |
| 40 | /* pointers within w1_cb_block and cn data */ |
| 41 | struct w1_cb_block *block; |
| 42 | struct w1_netlink_msg *msg; |
| 43 | struct w1_slave *sl; |
| 44 | struct w1_master *dev; |
| 45 | }; |
| 46 | |
| 47 | /** |
| 48 | * w1_reply_len() - calculate current reply length, compare to maxlen |
| 49 | * @block: block to calculate |
| 50 | * |
| 51 | * Calculates the current message length including possible multiple |
| 52 | * cn_msg and data, excludes the first sizeof(struct cn_msg). Direclty |
| 53 | * compariable to maxlen and usable to send the message. |
| 54 | */ |
| 55 | static u16 w1_reply_len(struct w1_cb_block *block) |
| 56 | { |
| 57 | if (!block->cn) |
| 58 | return 0; |
| 59 | return (u8 *)block->cn - (u8 *)block->first_cn + block->cn->len; |
| 60 | } |
| 61 | |
| 62 | static void w1_unref_block(struct w1_cb_block *block) |
| 63 | { |
| 64 | if (atomic_sub_return(1, &block->refcnt) == 0) { |
| 65 | u16 len = w1_reply_len(block); |
| 66 | if (len) { |
| 67 | cn_netlink_send_mult(block->first_cn, len, |
| 68 | block->portid, 0, GFP_KERNEL); |
| 69 | } |
| 70 | kfree(block); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * w1_reply_make_space() - send message if needed to make space |
| 76 | * @block: block to make space on |
| 77 | * @space: how many bytes requested |
| 78 | * |
| 79 | * Verify there is enough room left for the caller to add "space" bytes to the |
| 80 | * message, if there isn't send the message and reset. |
| 81 | */ |
| 82 | static void w1_reply_make_space(struct w1_cb_block *block, u16 space) |
| 83 | { |
| 84 | u16 len = w1_reply_len(block); |
| 85 | if (len + space >= block->maxlen) { |
| 86 | cn_netlink_send_mult(block->first_cn, len, block->portid, 0, GFP_KERNEL); |
| 87 | block->first_cn->len = 0; |
| 88 | block->cn = NULL; |
| 89 | block->msg = NULL; |
| 90 | block->cmd = NULL; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | /* Early send when replies aren't bundled. */ |
| 95 | static void w1_netlink_check_send(struct w1_cb_block *block) |
| 96 | { |
| 97 | if (!(block->request_cn.flags & W1_CN_BUNDLE) && block->cn) |
| 98 | w1_reply_make_space(block, block->maxlen); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * w1_netlink_setup_msg() - prepare to write block->msg |
| 103 | * @block: block to operate on |
| 104 | * @ack: determines if cn can be reused |
| 105 | * |
| 106 | * block->cn will be setup with the correct ack, advancing if needed |
| 107 | * block->cn->len does not include space for block->msg |
| 108 | * block->msg advances but remains uninitialized |
| 109 | */ |
| 110 | static void w1_netlink_setup_msg(struct w1_cb_block *block, u32 ack) |
| 111 | { |
| 112 | if (block->cn && block->cn->ack == ack) { |
| 113 | block->msg = (struct w1_netlink_msg *)(block->cn->data + block->cn->len); |
| 114 | } else { |
| 115 | /* advance or set to data */ |
| 116 | if (block->cn) |
| 117 | block->cn = (struct cn_msg *)(block->cn->data + |
| 118 | block->cn->len); |
| 119 | else |
| 120 | block->cn = block->first_cn; |
| 121 | |
| 122 | memcpy(block->cn, &block->request_cn, sizeof(*block->cn)); |
| 123 | block->cn->len = 0; |
| 124 | block->cn->ack = ack; |
| 125 | block->msg = (struct w1_netlink_msg *)block->cn->data; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | /* Append cmd to msg, include cmd->data as well. This is because |
| 130 | * any following data goes with the command and in the case of a read is |
| 131 | * the results. |
| 132 | */ |
| 133 | static void w1_netlink_queue_cmd(struct w1_cb_block *block, |
| 134 | struct w1_netlink_cmd *cmd) |
| 135 | { |
| 136 | u32 space; |
| 137 | w1_reply_make_space(block, sizeof(struct cn_msg) + |
| 138 | sizeof(struct w1_netlink_msg) + sizeof(*cmd) + cmd->len); |
| 139 | |
| 140 | /* There's a status message sent after each command, so no point |
| 141 | * in trying to bundle this cmd after an existing one, because |
| 142 | * there won't be one. Allocate and copy over a new cn_msg. |
| 143 | */ |
| 144 | w1_netlink_setup_msg(block, block->request_cn.seq + 1); |
| 145 | memcpy(block->msg, block->cur_msg, sizeof(*block->msg)); |
| 146 | block->cn->len += sizeof(*block->msg); |
| 147 | block->msg->len = 0; |
| 148 | block->cmd = (struct w1_netlink_cmd *)(block->msg->data); |
| 149 | |
| 150 | space = sizeof(*cmd) + cmd->len; |
| 151 | if (block->cmd != cmd) |
| 152 | memcpy(block->cmd, cmd, space); |
| 153 | block->cn->len += space; |
| 154 | block->msg->len += space; |
| 155 | } |
| 156 | |
| 157 | /* Append req_msg and req_cmd, no other commands and no data from req_cmd are |
| 158 | * copied. |
| 159 | */ |
| 160 | static void w1_netlink_queue_status(struct w1_cb_block *block, |
| 161 | struct w1_netlink_msg *req_msg, struct w1_netlink_cmd *req_cmd, |
| 162 | int error) |
| 163 | { |
| 164 | u16 space = sizeof(struct cn_msg) + sizeof(*req_msg) + sizeof(*req_cmd); |
| 165 | w1_reply_make_space(block, space); |
| 166 | w1_netlink_setup_msg(block, block->request_cn.ack); |
| 167 | |
| 168 | memcpy(block->msg, req_msg, sizeof(*req_msg)); |
| 169 | block->cn->len += sizeof(*req_msg); |
| 170 | block->msg->len = 0; |
| 171 | block->msg->status = (u8)-error; |
| 172 | if (req_cmd) { |
| 173 | struct w1_netlink_cmd *cmd = (struct w1_netlink_cmd *)block->msg->data; |
| 174 | memcpy(cmd, req_cmd, sizeof(*cmd)); |
| 175 | block->cn->len += sizeof(*cmd); |
| 176 | block->msg->len += sizeof(*cmd); |
| 177 | cmd->len = 0; |
| 178 | } |
| 179 | w1_netlink_check_send(block); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * w1_netlink_send_error() - sends the error message now |
| 184 | * @cn: original cn_msg |
| 185 | * @msg: original w1_netlink_msg |
| 186 | * @portid: where to send it |
| 187 | * @error: error status |
| 188 | * |
| 189 | * Use when a block isn't available to queue the message to and cn, msg |
| 190 | * might not be contiguous. |
| 191 | */ |
| 192 | static void w1_netlink_send_error(struct cn_msg *cn, struct w1_netlink_msg *msg, |
| 193 | int portid, int error) |
| 194 | { |
| 195 | struct { |
| 196 | struct cn_msg cn; |
| 197 | struct w1_netlink_msg msg; |
| 198 | } packet; |
| 199 | memcpy(&packet.cn, cn, sizeof(packet.cn)); |
| 200 | memcpy(&packet.msg, msg, sizeof(packet.msg)); |
| 201 | packet.cn.len = sizeof(packet.msg); |
| 202 | packet.msg.len = 0; |
| 203 | packet.msg.status = (u8)-error; |
| 204 | cn_netlink_send(&packet.cn, portid, 0, GFP_KERNEL); |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * w1_netlink_send() - sends w1 netlink notifications |
| 209 | * @dev: w1_master the even is associated with or for |
| 210 | * @msg: w1_netlink_msg message to be sent |
| 211 | * |
| 212 | * This are notifications generated from the kernel. |
| 213 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | void w1_netlink_send(struct w1_master *dev, struct w1_netlink_msg *msg) |
| 215 | { |
David Fries | 8a0427d | 2014-04-08 22:37:09 -0500 | [diff] [blame] | 216 | struct { |
| 217 | struct cn_msg cn; |
| 218 | struct w1_netlink_msg msg; |
| 219 | } packet; |
| 220 | memset(&packet, 0, sizeof(packet)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | |
David Fries | 8a0427d | 2014-04-08 22:37:09 -0500 | [diff] [blame] | 222 | packet.cn.id.idx = CN_W1_IDX; |
| 223 | packet.cn.id.val = CN_W1_VAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | |
David Fries | 8a0427d | 2014-04-08 22:37:09 -0500 | [diff] [blame] | 225 | packet.cn.seq = dev->seq++; |
| 226 | packet.cn.len = sizeof(*msg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | |
David Fries | 8a0427d | 2014-04-08 22:37:09 -0500 | [diff] [blame] | 228 | memcpy(&packet.msg, msg, sizeof(*msg)); |
| 229 | packet.msg.len = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | |
David Fries | 8a0427d | 2014-04-08 22:37:09 -0500 | [diff] [blame] | 231 | cn_netlink_send(&packet.cn, 0, 0, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | } |
Evgeniy Polyakov | 2d83317 | 2005-07-27 13:10:11 +0400 | [diff] [blame] | 233 | |
Evgeniy Polyakov | 3b83840 | 2009-01-07 18:09:01 -0800 | [diff] [blame] | 234 | static void w1_send_slave(struct w1_master *dev, u64 rn) |
Evgeniy Polyakov | 2d83317 | 2005-07-27 13:10:11 +0400 | [diff] [blame] | 235 | { |
David Fries | 8a0427d | 2014-04-08 22:37:09 -0500 | [diff] [blame] | 236 | struct w1_cb_block *block = dev->priv; |
| 237 | struct w1_netlink_cmd *cache_cmd = block->cmd; |
David Fries | 6b355b3 | 2014-01-15 22:29:12 -0600 | [diff] [blame] | 238 | u64 *data; |
Evgeniy Polyakov | 2d83317 | 2005-07-27 13:10:11 +0400 | [diff] [blame] | 239 | |
David Fries | 8a0427d | 2014-04-08 22:37:09 -0500 | [diff] [blame] | 240 | w1_reply_make_space(block, sizeof(*data)); |
Evgeniy Polyakov | 1200337 | 2006-03-23 19:11:58 +0300 | [diff] [blame] | 241 | |
David Fries | 8a0427d | 2014-04-08 22:37:09 -0500 | [diff] [blame] | 242 | /* Add cmd back if the packet was sent */ |
| 243 | if (!block->cmd) { |
| 244 | cache_cmd->len = 0; |
| 245 | w1_netlink_queue_cmd(block, cache_cmd); |
Evgeniy Polyakov | 3b83840 | 2009-01-07 18:09:01 -0800 | [diff] [blame] | 246 | } |
| 247 | |
David Fries | 8a0427d | 2014-04-08 22:37:09 -0500 | [diff] [blame] | 248 | data = (u64 *)(block->cmd->data + block->cmd->len); |
Evgeniy Polyakov | 3b83840 | 2009-01-07 18:09:01 -0800 | [diff] [blame] | 249 | |
David Fries | 6b355b3 | 2014-01-15 22:29:12 -0600 | [diff] [blame] | 250 | *data = rn; |
David Fries | 8a0427d | 2014-04-08 22:37:09 -0500 | [diff] [blame] | 251 | block->cn->len += sizeof(*data); |
| 252 | block->msg->len += sizeof(*data); |
| 253 | block->cmd->len += sizeof(*data); |
Evgeniy Polyakov | 3b83840 | 2009-01-07 18:09:01 -0800 | [diff] [blame] | 254 | } |
| 255 | |
David Fries | 70b34d2 | 2014-01-15 22:29:17 -0600 | [diff] [blame] | 256 | static void w1_found_send_slave(struct w1_master *dev, u64 rn) |
Evgeniy Polyakov | 3b83840 | 2009-01-07 18:09:01 -0800 | [diff] [blame] | 257 | { |
David Fries | 70b34d2 | 2014-01-15 22:29:17 -0600 | [diff] [blame] | 258 | /* update kernel slave list */ |
| 259 | w1_slave_found(dev, rn); |
| 260 | |
| 261 | w1_send_slave(dev, rn); |
| 262 | } |
| 263 | |
| 264 | /* Get the current slave list, or search (with or without alarm) */ |
David Fries | 8a0427d | 2014-04-08 22:37:09 -0500 | [diff] [blame] | 265 | static int w1_get_slaves(struct w1_master *dev, struct w1_netlink_cmd *req_cmd) |
David Fries | 70b34d2 | 2014-01-15 22:29:17 -0600 | [diff] [blame] | 266 | { |
David Fries | 70b34d2 | 2014-01-15 22:29:17 -0600 | [diff] [blame] | 267 | struct w1_slave *sl; |
| 268 | |
David Fries | 8a0427d | 2014-04-08 22:37:09 -0500 | [diff] [blame] | 269 | req_cmd->len = 0; |
| 270 | w1_netlink_queue_cmd(dev->priv, req_cmd); |
Evgeniy Polyakov | 3b83840 | 2009-01-07 18:09:01 -0800 | [diff] [blame] | 271 | |
David Fries | 70b34d2 | 2014-01-15 22:29:17 -0600 | [diff] [blame] | 272 | if (req_cmd->cmd == W1_CMD_LIST_SLAVES) { |
David Fries | 8a0427d | 2014-04-08 22:37:09 -0500 | [diff] [blame] | 273 | u64 rn; |
David Fries | 9fcbbac | 2014-01-15 22:29:18 -0600 | [diff] [blame] | 274 | mutex_lock(&dev->list_mutex); |
David Fries | 70b34d2 | 2014-01-15 22:29:17 -0600 | [diff] [blame] | 275 | list_for_each_entry(sl, &dev->slist, w1_slave_entry) { |
| 276 | memcpy(&rn, &sl->reg_num, sizeof(rn)); |
| 277 | w1_send_slave(dev, rn); |
| 278 | } |
David Fries | 9fcbbac | 2014-01-15 22:29:18 -0600 | [diff] [blame] | 279 | mutex_unlock(&dev->list_mutex); |
David Fries | 70b34d2 | 2014-01-15 22:29:17 -0600 | [diff] [blame] | 280 | } else { |
David Fries | 8a0427d | 2014-04-08 22:37:09 -0500 | [diff] [blame] | 281 | w1_search_process_cb(dev, req_cmd->cmd == W1_CMD_ALARM_SEARCH ? |
David Fries | 70b34d2 | 2014-01-15 22:29:17 -0600 | [diff] [blame] | 282 | W1_ALARM_SEARCH : W1_SEARCH, w1_found_send_slave); |
| 283 | } |
Evgeniy Polyakov | 3b83840 | 2009-01-07 18:09:01 -0800 | [diff] [blame] | 284 | |
Evgeniy Polyakov | 2d83317 | 2005-07-27 13:10:11 +0400 | [diff] [blame] | 285 | return 0; |
| 286 | } |
| 287 | |
David Fries | 8a0427d | 2014-04-08 22:37:09 -0500 | [diff] [blame] | 288 | static int w1_process_command_io(struct w1_master *dev, |
| 289 | struct w1_netlink_cmd *cmd) |
Evgeniy Polyakov | c7e2663 | 2009-01-07 18:09:03 -0800 | [diff] [blame] | 290 | { |
| 291 | int err = 0; |
| 292 | |
| 293 | switch (cmd->cmd) { |
| 294 | case W1_CMD_TOUCH: |
| 295 | w1_touch_block(dev, cmd->data, cmd->len); |
David Fries | 8a0427d | 2014-04-08 22:37:09 -0500 | [diff] [blame] | 296 | w1_netlink_queue_cmd(dev->priv, cmd); |
Evgeniy Polyakov | c7e2663 | 2009-01-07 18:09:03 -0800 | [diff] [blame] | 297 | break; |
| 298 | case W1_CMD_READ: |
| 299 | w1_read_block(dev, cmd->data, cmd->len); |
David Fries | 8a0427d | 2014-04-08 22:37:09 -0500 | [diff] [blame] | 300 | w1_netlink_queue_cmd(dev->priv, cmd); |
Evgeniy Polyakov | c7e2663 | 2009-01-07 18:09:03 -0800 | [diff] [blame] | 301 | break; |
| 302 | case W1_CMD_WRITE: |
| 303 | w1_write_block(dev, cmd->data, cmd->len); |
| 304 | break; |
| 305 | default: |
Evgeniy Polyakov | 4037014 | 2009-01-07 18:09:05 -0800 | [diff] [blame] | 306 | err = -EINVAL; |
Evgeniy Polyakov | c7e2663 | 2009-01-07 18:09:03 -0800 | [diff] [blame] | 307 | break; |
| 308 | } |
| 309 | |
| 310 | return err; |
| 311 | } |
| 312 | |
David Fries | 70b34d2 | 2014-01-15 22:29:17 -0600 | [diff] [blame] | 313 | static int w1_process_command_addremove(struct w1_master *dev, |
David Fries | 70b34d2 | 2014-01-15 22:29:17 -0600 | [diff] [blame] | 314 | struct w1_netlink_cmd *cmd) |
| 315 | { |
| 316 | struct w1_slave *sl; |
| 317 | int err = 0; |
| 318 | struct w1_reg_num *id; |
| 319 | |
David Fries | 8a0427d | 2014-04-08 22:37:09 -0500 | [diff] [blame] | 320 | if (cmd->len != sizeof(*id)) |
David Fries | 70b34d2 | 2014-01-15 22:29:17 -0600 | [diff] [blame] | 321 | return -EINVAL; |
| 322 | |
| 323 | id = (struct w1_reg_num *)cmd->data; |
| 324 | |
| 325 | sl = w1_slave_search_device(dev, id); |
| 326 | switch (cmd->cmd) { |
| 327 | case W1_CMD_SLAVE_ADD: |
| 328 | if (sl) |
| 329 | err = -EINVAL; |
| 330 | else |
| 331 | err = w1_attach_slave_device(dev, id); |
| 332 | break; |
| 333 | case W1_CMD_SLAVE_REMOVE: |
| 334 | if (sl) |
| 335 | w1_slave_detach(sl); |
| 336 | else |
| 337 | err = -EINVAL; |
| 338 | break; |
| 339 | default: |
| 340 | err = -EINVAL; |
| 341 | break; |
| 342 | } |
| 343 | |
| 344 | return err; |
| 345 | } |
| 346 | |
| 347 | static int w1_process_command_master(struct w1_master *dev, |
David Fries | 70b34d2 | 2014-01-15 22:29:17 -0600 | [diff] [blame] | 348 | struct w1_netlink_cmd *req_cmd) |
Evgeniy Polyakov | 3b83840 | 2009-01-07 18:09:01 -0800 | [diff] [blame] | 349 | { |
| 350 | int err = -EINVAL; |
Evgeniy Polyakov | 3b83840 | 2009-01-07 18:09:01 -0800 | [diff] [blame] | 351 | |
David Fries | d3a8a9d | 2014-01-15 22:29:26 -0600 | [diff] [blame] | 352 | /* drop bus_mutex for search (does it's own locking), and add/remove |
| 353 | * which doesn't use the bus |
| 354 | */ |
David Fries | 70b34d2 | 2014-01-15 22:29:17 -0600 | [diff] [blame] | 355 | switch (req_cmd->cmd) { |
Evgeniy Polyakov | c7e2663 | 2009-01-07 18:09:03 -0800 | [diff] [blame] | 356 | case W1_CMD_SEARCH: |
| 357 | case W1_CMD_ALARM_SEARCH: |
David Fries | 70b34d2 | 2014-01-15 22:29:17 -0600 | [diff] [blame] | 358 | case W1_CMD_LIST_SLAVES: |
David Fries | d3a8a9d | 2014-01-15 22:29:26 -0600 | [diff] [blame] | 359 | mutex_unlock(&dev->bus_mutex); |
David Fries | 8a0427d | 2014-04-08 22:37:09 -0500 | [diff] [blame] | 360 | err = w1_get_slaves(dev, req_cmd); |
David Fries | d3a8a9d | 2014-01-15 22:29:26 -0600 | [diff] [blame] | 361 | mutex_lock(&dev->bus_mutex); |
Evgeniy Polyakov | c7e2663 | 2009-01-07 18:09:03 -0800 | [diff] [blame] | 362 | break; |
| 363 | case W1_CMD_READ: |
| 364 | case W1_CMD_WRITE: |
| 365 | case W1_CMD_TOUCH: |
David Fries | 8a0427d | 2014-04-08 22:37:09 -0500 | [diff] [blame] | 366 | err = w1_process_command_io(dev, req_cmd); |
Evgeniy Polyakov | c7e2663 | 2009-01-07 18:09:03 -0800 | [diff] [blame] | 367 | break; |
Evgeniy Polyakov | f89735c | 2009-01-07 18:09:04 -0800 | [diff] [blame] | 368 | case W1_CMD_RESET: |
| 369 | err = w1_reset_bus(dev); |
| 370 | break; |
David Fries | 70b34d2 | 2014-01-15 22:29:17 -0600 | [diff] [blame] | 371 | case W1_CMD_SLAVE_ADD: |
| 372 | case W1_CMD_SLAVE_REMOVE: |
David Fries | d3a8a9d | 2014-01-15 22:29:26 -0600 | [diff] [blame] | 373 | mutex_unlock(&dev->bus_mutex); |
| 374 | mutex_lock(&dev->mutex); |
David Fries | 8a0427d | 2014-04-08 22:37:09 -0500 | [diff] [blame] | 375 | err = w1_process_command_addremove(dev, req_cmd); |
David Fries | d3a8a9d | 2014-01-15 22:29:26 -0600 | [diff] [blame] | 376 | mutex_unlock(&dev->mutex); |
| 377 | mutex_lock(&dev->bus_mutex); |
David Fries | 70b34d2 | 2014-01-15 22:29:17 -0600 | [diff] [blame] | 378 | break; |
Evgeniy Polyakov | c7e2663 | 2009-01-07 18:09:03 -0800 | [diff] [blame] | 379 | default: |
Evgeniy Polyakov | 4037014 | 2009-01-07 18:09:05 -0800 | [diff] [blame] | 380 | err = -EINVAL; |
Evgeniy Polyakov | c7e2663 | 2009-01-07 18:09:03 -0800 | [diff] [blame] | 381 | break; |
Evgeniy Polyakov | 3b83840 | 2009-01-07 18:09:01 -0800 | [diff] [blame] | 382 | } |
| 383 | |
Evgeniy Polyakov | 3b83840 | 2009-01-07 18:09:01 -0800 | [diff] [blame] | 384 | return err; |
| 385 | } |
| 386 | |
David Fries | 8a0427d | 2014-04-08 22:37:09 -0500 | [diff] [blame] | 387 | static int w1_process_command_slave(struct w1_slave *sl, |
| 388 | struct w1_netlink_cmd *cmd) |
Evgeniy Polyakov | 1200337 | 2006-03-23 19:11:58 +0300 | [diff] [blame] | 389 | { |
Evgeniy Polyakov | 1200337 | 2006-03-23 19:11:58 +0300 | [diff] [blame] | 390 | dev_dbg(&sl->master->dev, "%s: %02x.%012llx.%02x: cmd=%02x, len=%u.\n", |
Evgeniy Polyakov | c7e2663 | 2009-01-07 18:09:03 -0800 | [diff] [blame] | 391 | __func__, sl->reg_num.family, (unsigned long long)sl->reg_num.id, |
| 392 | sl->reg_num.crc, cmd->cmd, cmd->len); |
Evgeniy Polyakov | 1200337 | 2006-03-23 19:11:58 +0300 | [diff] [blame] | 393 | |
David Fries | 8a0427d | 2014-04-08 22:37:09 -0500 | [diff] [blame] | 394 | return w1_process_command_io(sl->master, cmd); |
Evgeniy Polyakov | 1200337 | 2006-03-23 19:11:58 +0300 | [diff] [blame] | 395 | } |
| 396 | |
David Fries | 8a0427d | 2014-04-08 22:37:09 -0500 | [diff] [blame] | 397 | static int w1_process_command_root(struct cn_msg *req_cn, u32 portid) |
Evgeniy Polyakov | 610705e | 2009-01-07 18:08:59 -0800 | [diff] [blame] | 398 | { |
David Fries | 8a0427d | 2014-04-08 22:37:09 -0500 | [diff] [blame] | 399 | struct w1_master *dev; |
Evgeniy Polyakov | 610705e | 2009-01-07 18:08:59 -0800 | [diff] [blame] | 400 | struct cn_msg *cn; |
David Fries | 8a0427d | 2014-04-08 22:37:09 -0500 | [diff] [blame] | 401 | struct w1_netlink_msg *msg; |
Evgeniy Polyakov | 610705e | 2009-01-07 18:08:59 -0800 | [diff] [blame] | 402 | u32 *id; |
| 403 | |
Evgeniy Polyakov | 610705e | 2009-01-07 18:08:59 -0800 | [diff] [blame] | 404 | cn = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 405 | if (!cn) |
| 406 | return -ENOMEM; |
| 407 | |
| 408 | cn->id.idx = CN_W1_IDX; |
| 409 | cn->id.val = CN_W1_VAL; |
| 410 | |
David Fries | 8a0427d | 2014-04-08 22:37:09 -0500 | [diff] [blame] | 411 | cn->seq = req_cn->seq; |
| 412 | cn->ack = req_cn->seq + 1; |
Evgeniy Polyakov | 610705e | 2009-01-07 18:08:59 -0800 | [diff] [blame] | 413 | cn->len = sizeof(struct w1_netlink_msg); |
David Fries | 8a0427d | 2014-04-08 22:37:09 -0500 | [diff] [blame] | 414 | msg = (struct w1_netlink_msg *)cn->data; |
Evgeniy Polyakov | 610705e | 2009-01-07 18:08:59 -0800 | [diff] [blame] | 415 | |
David Fries | 8a0427d | 2014-04-08 22:37:09 -0500 | [diff] [blame] | 416 | msg->type = W1_LIST_MASTERS; |
| 417 | msg->status = 0; |
| 418 | msg->len = 0; |
| 419 | id = (u32 *)msg->data; |
Evgeniy Polyakov | 610705e | 2009-01-07 18:08:59 -0800 | [diff] [blame] | 420 | |
| 421 | mutex_lock(&w1_mlock); |
David Fries | 8a0427d | 2014-04-08 22:37:09 -0500 | [diff] [blame] | 422 | list_for_each_entry(dev, &w1_masters, w1_master_entry) { |
Evgeniy Polyakov | 610705e | 2009-01-07 18:08:59 -0800 | [diff] [blame] | 423 | if (cn->len + sizeof(*id) > PAGE_SIZE - sizeof(struct cn_msg)) { |
David Fries | 5dbf567 | 2014-01-15 22:29:20 -0600 | [diff] [blame] | 424 | cn_netlink_send(cn, portid, 0, GFP_KERNEL); |
Evgeniy Polyakov | 610705e | 2009-01-07 18:08:59 -0800 | [diff] [blame] | 425 | cn->len = sizeof(struct w1_netlink_msg); |
David Fries | 8a0427d | 2014-04-08 22:37:09 -0500 | [diff] [blame] | 426 | msg->len = 0; |
| 427 | id = (u32 *)msg->data; |
Evgeniy Polyakov | 610705e | 2009-01-07 18:08:59 -0800 | [diff] [blame] | 428 | } |
| 429 | |
David Fries | 8a0427d | 2014-04-08 22:37:09 -0500 | [diff] [blame] | 430 | *id = dev->id; |
| 431 | msg->len += sizeof(*id); |
Evgeniy Polyakov | 610705e | 2009-01-07 18:08:59 -0800 | [diff] [blame] | 432 | cn->len += sizeof(*id); |
| 433 | id++; |
| 434 | } |
David Fries | 5dbf567 | 2014-01-15 22:29:20 -0600 | [diff] [blame] | 435 | cn_netlink_send(cn, portid, 0, GFP_KERNEL); |
Evgeniy Polyakov | 610705e | 2009-01-07 18:08:59 -0800 | [diff] [blame] | 436 | mutex_unlock(&w1_mlock); |
| 437 | |
| 438 | kfree(cn); |
| 439 | return 0; |
| 440 | } |
| 441 | |
David Fries | 9fcbbac | 2014-01-15 22:29:18 -0600 | [diff] [blame] | 442 | static void w1_process_cb(struct w1_master *dev, struct w1_async_cmd *async_cmd) |
| 443 | { |
| 444 | struct w1_cb_node *node = container_of(async_cmd, struct w1_cb_node, |
| 445 | async); |
David Fries | 8a0427d | 2014-04-08 22:37:09 -0500 | [diff] [blame] | 446 | u16 mlen = node->msg->len; |
| 447 | u16 len; |
David Fries | 9fcbbac | 2014-01-15 22:29:18 -0600 | [diff] [blame] | 448 | int err = 0; |
| 449 | struct w1_slave *sl = node->sl; |
David Fries | 8a0427d | 2014-04-08 22:37:09 -0500 | [diff] [blame] | 450 | struct w1_netlink_cmd *cmd = (struct w1_netlink_cmd *)node->msg->data; |
David Fries | 9fcbbac | 2014-01-15 22:29:18 -0600 | [diff] [blame] | 451 | |
David Fries | d3a8a9d | 2014-01-15 22:29:26 -0600 | [diff] [blame] | 452 | mutex_lock(&dev->bus_mutex); |
David Fries | 8a0427d | 2014-04-08 22:37:09 -0500 | [diff] [blame] | 453 | dev->priv = node->block; |
David Fries | 9fcbbac | 2014-01-15 22:29:18 -0600 | [diff] [blame] | 454 | if (sl && w1_reset_select_slave(sl)) |
| 455 | err = -ENODEV; |
David Fries | 8a0427d | 2014-04-08 22:37:09 -0500 | [diff] [blame] | 456 | node->block->cur_msg = node->msg; |
David Fries | 9fcbbac | 2014-01-15 22:29:18 -0600 | [diff] [blame] | 457 | |
| 458 | while (mlen && !err) { |
David Fries | 9fcbbac | 2014-01-15 22:29:18 -0600 | [diff] [blame] | 459 | if (cmd->len + sizeof(struct w1_netlink_cmd) > mlen) { |
| 460 | err = -E2BIG; |
| 461 | break; |
| 462 | } |
| 463 | |
| 464 | if (sl) |
David Fries | 8a0427d | 2014-04-08 22:37:09 -0500 | [diff] [blame] | 465 | err = w1_process_command_slave(sl, cmd); |
David Fries | 9fcbbac | 2014-01-15 22:29:18 -0600 | [diff] [blame] | 466 | else |
David Fries | 8a0427d | 2014-04-08 22:37:09 -0500 | [diff] [blame] | 467 | err = w1_process_command_master(dev, cmd); |
| 468 | w1_netlink_check_send(node->block); |
David Fries | 9fcbbac | 2014-01-15 22:29:18 -0600 | [diff] [blame] | 469 | |
David Fries | 8a0427d | 2014-04-08 22:37:09 -0500 | [diff] [blame] | 470 | w1_netlink_queue_status(node->block, node->msg, cmd, err); |
David Fries | 9fcbbac | 2014-01-15 22:29:18 -0600 | [diff] [blame] | 471 | err = 0; |
| 472 | |
David Fries | 8a0427d | 2014-04-08 22:37:09 -0500 | [diff] [blame] | 473 | len = sizeof(*cmd) + cmd->len; |
| 474 | cmd = (struct w1_netlink_cmd *)((u8 *)cmd + len); |
| 475 | mlen -= len; |
David Fries | 9fcbbac | 2014-01-15 22:29:18 -0600 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | if (!cmd || err) |
David Fries | 8a0427d | 2014-04-08 22:37:09 -0500 | [diff] [blame] | 479 | w1_netlink_queue_status(node->block, node->msg, cmd, err); |
David Fries | 9fcbbac | 2014-01-15 22:29:18 -0600 | [diff] [blame] | 480 | |
David Fries | 593ceb0 | 2014-04-08 22:37:07 -0500 | [diff] [blame] | 481 | /* ref taken in w1_search_slave or w1_search_master_id when building |
| 482 | * the block |
| 483 | */ |
David Fries | 9fcbbac | 2014-01-15 22:29:18 -0600 | [diff] [blame] | 484 | if (sl) |
| 485 | w1_unref_slave(sl); |
| 486 | else |
| 487 | atomic_dec(&dev->refcnt); |
David Fries | 8a0427d | 2014-04-08 22:37:09 -0500 | [diff] [blame] | 488 | dev->priv = NULL; |
David Fries | d3a8a9d | 2014-01-15 22:29:26 -0600 | [diff] [blame] | 489 | mutex_unlock(&dev->bus_mutex); |
David Fries | 9fcbbac | 2014-01-15 22:29:18 -0600 | [diff] [blame] | 490 | |
| 491 | mutex_lock(&dev->list_mutex); |
| 492 | list_del(&async_cmd->async_entry); |
| 493 | mutex_unlock(&dev->list_mutex); |
| 494 | |
David Fries | 8a0427d | 2014-04-08 22:37:09 -0500 | [diff] [blame] | 495 | w1_unref_block(node->block); |
David Fries | 9fcbbac | 2014-01-15 22:29:18 -0600 | [diff] [blame] | 496 | } |
| 497 | |
David Fries | 8a0427d | 2014-04-08 22:37:09 -0500 | [diff] [blame] | 498 | static void w1_list_count_cmds(struct w1_netlink_msg *msg, int *cmd_count, |
| 499 | u16 *slave_len) |
Evgeniy Polyakov | 1200337 | 2006-03-23 19:11:58 +0300 | [diff] [blame] | 500 | { |
David Fries | 8a0427d | 2014-04-08 22:37:09 -0500 | [diff] [blame] | 501 | struct w1_netlink_cmd *cmd = (struct w1_netlink_cmd *)msg->data; |
| 502 | u16 mlen = msg->len; |
| 503 | u16 len; |
| 504 | int slave_list = 0; |
| 505 | while (mlen) { |
| 506 | if (cmd->len + sizeof(struct w1_netlink_cmd) > mlen) |
| 507 | break; |
| 508 | |
| 509 | switch (cmd->cmd) { |
| 510 | case W1_CMD_SEARCH: |
| 511 | case W1_CMD_ALARM_SEARCH: |
| 512 | case W1_CMD_LIST_SLAVES: |
| 513 | ++slave_list; |
| 514 | } |
| 515 | ++*cmd_count; |
| 516 | len = sizeof(*cmd) + cmd->len; |
| 517 | cmd = (struct w1_netlink_cmd *)((u8 *)cmd + len); |
| 518 | mlen -= len; |
| 519 | } |
| 520 | |
| 521 | if (slave_list) { |
| 522 | struct w1_master *dev = w1_search_master_id(msg->id.mst.id); |
| 523 | if (dev) { |
| 524 | /* Bytes, and likely an overstimate, and if it isn't |
| 525 | * the results can still be split between packets. |
| 526 | */ |
| 527 | *slave_len += sizeof(struct w1_reg_num) * slave_list * |
| 528 | (dev->slave_count + dev->max_slave_count); |
| 529 | /* search incremented it */ |
| 530 | atomic_dec(&dev->refcnt); |
| 531 | } |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | static void w1_cn_callback(struct cn_msg *cn, struct netlink_skb_parms *nsp) |
| 536 | { |
| 537 | struct w1_netlink_msg *msg = (struct w1_netlink_msg *)(cn + 1); |
Evgeniy Polyakov | 1200337 | 2006-03-23 19:11:58 +0300 | [diff] [blame] | 538 | struct w1_slave *sl; |
| 539 | struct w1_master *dev; |
David Fries | 9fcbbac | 2014-01-15 22:29:18 -0600 | [diff] [blame] | 540 | u16 msg_len; |
David Fries | 8a0427d | 2014-04-08 22:37:09 -0500 | [diff] [blame] | 541 | u16 slave_len = 0; |
Evgeniy Polyakov | 1200337 | 2006-03-23 19:11:58 +0300 | [diff] [blame] | 542 | int err = 0; |
David Fries | 9fcbbac | 2014-01-15 22:29:18 -0600 | [diff] [blame] | 543 | struct w1_cb_block *block = NULL; |
| 544 | struct w1_cb_node *node = NULL; |
| 545 | int node_count = 0; |
David Fries | 8a0427d | 2014-04-08 22:37:09 -0500 | [diff] [blame] | 546 | int cmd_count = 0; |
| 547 | |
| 548 | /* If any unknown flag is set let the application know, that way |
| 549 | * applications can detect the absence of features in kernels that |
| 550 | * don't know about them. http://lwn.net/Articles/587527/ |
| 551 | */ |
| 552 | if (cn->flags & ~(W1_CN_BUNDLE)) { |
| 553 | w1_netlink_send_error(cn, msg, nsp->portid, -EINVAL); |
| 554 | return; |
| 555 | } |
Evgeniy Polyakov | 1200337 | 2006-03-23 19:11:58 +0300 | [diff] [blame] | 556 | |
David Fries | 9fcbbac | 2014-01-15 22:29:18 -0600 | [diff] [blame] | 557 | /* Count the number of master or slave commands there are to allocate |
| 558 | * space for one cb_node each. |
| 559 | */ |
David Fries | 8a0427d | 2014-04-08 22:37:09 -0500 | [diff] [blame] | 560 | msg_len = cn->len; |
David Fries | 9fcbbac | 2014-01-15 22:29:18 -0600 | [diff] [blame] | 561 | while (msg_len && !err) { |
David Fries | 8a0427d | 2014-04-08 22:37:09 -0500 | [diff] [blame] | 562 | if (msg->len + sizeof(struct w1_netlink_msg) > msg_len) { |
David Fries | 9fcbbac | 2014-01-15 22:29:18 -0600 | [diff] [blame] | 563 | err = -E2BIG; |
| 564 | break; |
| 565 | } |
| 566 | |
David Fries | 8a0427d | 2014-04-08 22:37:09 -0500 | [diff] [blame] | 567 | /* count messages for nodes and allocate any additional space |
| 568 | * required for slave lists |
| 569 | */ |
| 570 | if (msg->type == W1_MASTER_CMD || msg->type == W1_SLAVE_CMD) { |
David Fries | 9fcbbac | 2014-01-15 22:29:18 -0600 | [diff] [blame] | 571 | ++node_count; |
David Fries | 8a0427d | 2014-04-08 22:37:09 -0500 | [diff] [blame] | 572 | w1_list_count_cmds(msg, &cmd_count, &slave_len); |
| 573 | } |
David Fries | 9fcbbac | 2014-01-15 22:29:18 -0600 | [diff] [blame] | 574 | |
David Fries | 8a0427d | 2014-04-08 22:37:09 -0500 | [diff] [blame] | 575 | msg_len -= sizeof(struct w1_netlink_msg) + msg->len; |
| 576 | msg = (struct w1_netlink_msg *)(((u8 *)msg) + |
| 577 | sizeof(struct w1_netlink_msg) + msg->len); |
David Fries | 9fcbbac | 2014-01-15 22:29:18 -0600 | [diff] [blame] | 578 | } |
David Fries | 8a0427d | 2014-04-08 22:37:09 -0500 | [diff] [blame] | 579 | msg = (struct w1_netlink_msg *)(cn + 1); |
David Fries | 9fcbbac | 2014-01-15 22:29:18 -0600 | [diff] [blame] | 580 | if (node_count) { |
David Fries | 8a0427d | 2014-04-08 22:37:09 -0500 | [diff] [blame] | 581 | int size; |
David Fries | 4b97b27 | 2014-11-10 20:19:37 -0600 | [diff] [blame] | 582 | int reply_size = sizeof(*cn) + cn->len + slave_len; |
David Fries | 8a0427d | 2014-04-08 22:37:09 -0500 | [diff] [blame] | 583 | if (cn->flags & W1_CN_BUNDLE) { |
| 584 | /* bundling duplicats some of the messages */ |
| 585 | reply_size += 2 * cmd_count * (sizeof(struct cn_msg) + |
| 586 | sizeof(struct w1_netlink_msg) + |
| 587 | sizeof(struct w1_netlink_cmd)); |
| 588 | } |
Andrew F. Davis | b04e085 | 2017-03-16 10:19:53 -0500 | [diff] [blame] | 589 | reply_size = min(CONNECTOR_MAX_MSG_SIZE, reply_size); |
David Fries | 8a0427d | 2014-04-08 22:37:09 -0500 | [diff] [blame] | 590 | |
| 591 | /* allocate space for the block, a copy of the original message, |
| 592 | * one node per cmd to point into the original message, |
| 593 | * space for replies which is the original message size plus |
| 594 | * space for any list slave data and status messages |
| 595 | * cn->len doesn't include itself which is part of the block |
| 596 | * */ |
| 597 | size = /* block + original message */ |
| 598 | sizeof(struct w1_cb_block) + sizeof(*cn) + cn->len + |
| 599 | /* space for nodes */ |
| 600 | node_count * sizeof(struct w1_cb_node) + |
| 601 | /* replies */ |
| 602 | sizeof(struct cn_msg) + reply_size; |
| 603 | block = kzalloc(size, GFP_KERNEL); |
David Fries | 9fcbbac | 2014-01-15 22:29:18 -0600 | [diff] [blame] | 604 | if (!block) { |
David Fries | 8a0427d | 2014-04-08 22:37:09 -0500 | [diff] [blame] | 605 | /* if the system is already out of memory, |
| 606 | * (A) will this work, and (B) would it be better |
| 607 | * to not try? |
| 608 | */ |
| 609 | w1_netlink_send_error(cn, msg, nsp->portid, -ENOMEM); |
David Fries | 9fcbbac | 2014-01-15 22:29:18 -0600 | [diff] [blame] | 610 | return; |
| 611 | } |
| 612 | atomic_set(&block->refcnt, 1); |
David Fries | 5dbf567 | 2014-01-15 22:29:20 -0600 | [diff] [blame] | 613 | block->portid = nsp->portid; |
David Fries | 8a0427d | 2014-04-08 22:37:09 -0500 | [diff] [blame] | 614 | memcpy(&block->request_cn, cn, sizeof(*cn) + cn->len); |
| 615 | node = (struct w1_cb_node *)(block->request_cn.data + cn->len); |
| 616 | |
| 617 | /* Sneeky, when not bundling, reply_size is the allocated space |
| 618 | * required for the reply, cn_msg isn't part of maxlen so |
| 619 | * it should be reply_size - sizeof(struct cn_msg), however |
| 620 | * when checking if there is enough space, w1_reply_make_space |
| 621 | * is called with the full message size including cn_msg, |
| 622 | * because it isn't known at that time if an additional cn_msg |
| 623 | * will need to be allocated. So an extra cn_msg is added |
| 624 | * above in "size". |
| 625 | */ |
| 626 | block->maxlen = reply_size; |
| 627 | block->first_cn = (struct cn_msg *)(node + node_count); |
| 628 | memset(block->first_cn, 0, sizeof(*block->first_cn)); |
David Fries | 9fcbbac | 2014-01-15 22:29:18 -0600 | [diff] [blame] | 629 | } |
| 630 | |
David Fries | 8a0427d | 2014-04-08 22:37:09 -0500 | [diff] [blame] | 631 | msg_len = cn->len; |
David Fries | 9fcbbac | 2014-01-15 22:29:18 -0600 | [diff] [blame] | 632 | while (msg_len && !err) { |
Evgeniy Polyakov | 1200337 | 2006-03-23 19:11:58 +0300 | [diff] [blame] | 633 | |
| 634 | dev = NULL; |
| 635 | sl = NULL; |
| 636 | |
David Fries | 8a0427d | 2014-04-08 22:37:09 -0500 | [diff] [blame] | 637 | if (msg->len + sizeof(struct w1_netlink_msg) > msg_len) { |
Evgeniy Polyakov | 1200337 | 2006-03-23 19:11:58 +0300 | [diff] [blame] | 638 | err = -E2BIG; |
| 639 | break; |
| 640 | } |
| 641 | |
David Fries | 593ceb0 | 2014-04-08 22:37:07 -0500 | [diff] [blame] | 642 | /* execute on this thread, no need to process later */ |
David Fries | 8a0427d | 2014-04-08 22:37:09 -0500 | [diff] [blame] | 643 | if (msg->type == W1_LIST_MASTERS) { |
| 644 | err = w1_process_command_root(cn, nsp->portid); |
David Fries | 593ceb0 | 2014-04-08 22:37:07 -0500 | [diff] [blame] | 645 | goto out_cont; |
| 646 | } |
| 647 | |
| 648 | /* All following message types require additional data, |
| 649 | * check here before references are taken. |
| 650 | */ |
David Fries | 8a0427d | 2014-04-08 22:37:09 -0500 | [diff] [blame] | 651 | if (!msg->len) { |
David Fries | 593ceb0 | 2014-04-08 22:37:07 -0500 | [diff] [blame] | 652 | err = -EPROTO; |
| 653 | goto out_cont; |
| 654 | } |
| 655 | |
David Fries | 8a0427d | 2014-04-08 22:37:09 -0500 | [diff] [blame] | 656 | /* both search calls take references */ |
| 657 | if (msg->type == W1_MASTER_CMD) { |
| 658 | dev = w1_search_master_id(msg->id.mst.id); |
| 659 | } else if (msg->type == W1_SLAVE_CMD) { |
| 660 | sl = w1_search_slave((struct w1_reg_num *)msg->id.id); |
Evgeniy Polyakov | 1200337 | 2006-03-23 19:11:58 +0300 | [diff] [blame] | 661 | if (sl) |
| 662 | dev = sl->master; |
Evgeniy Polyakov | 610705e | 2009-01-07 18:08:59 -0800 | [diff] [blame] | 663 | } else { |
Fjodor Schelichow | fdc9167 | 2014-06-19 02:52:00 +0200 | [diff] [blame] | 664 | pr_notice("%s: cn: %x.%x, wrong type: %u, len: %u.\n", |
David Fries | 8a0427d | 2014-04-08 22:37:09 -0500 | [diff] [blame] | 665 | __func__, cn->id.idx, cn->id.val, |
| 666 | msg->type, msg->len); |
David Fries | 593ceb0 | 2014-04-08 22:37:07 -0500 | [diff] [blame] | 667 | err = -EPROTO; |
Evgeniy Polyakov | 610705e | 2009-01-07 18:08:59 -0800 | [diff] [blame] | 668 | goto out_cont; |
Evgeniy Polyakov | 1200337 | 2006-03-23 19:11:58 +0300 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | if (!dev) { |
| 672 | err = -ENODEV; |
| 673 | goto out_cont; |
| 674 | } |
| 675 | |
Evgeniy Polyakov | 9be62e0 | 2009-01-07 18:09:01 -0800 | [diff] [blame] | 676 | err = 0; |
Evgeniy Polyakov | 9be62e0 | 2009-01-07 18:09:01 -0800 | [diff] [blame] | 677 | |
David Fries | 9fcbbac | 2014-01-15 22:29:18 -0600 | [diff] [blame] | 678 | atomic_inc(&block->refcnt); |
| 679 | node->async.cb = w1_process_cb; |
| 680 | node->block = block; |
David Fries | 8a0427d | 2014-04-08 22:37:09 -0500 | [diff] [blame] | 681 | node->msg = (struct w1_netlink_msg *)((u8 *)&block->request_cn + |
| 682 | (size_t)((u8 *)msg - (u8 *)cn)); |
David Fries | 9fcbbac | 2014-01-15 22:29:18 -0600 | [diff] [blame] | 683 | node->sl = sl; |
| 684 | node->dev = dev; |
Evgeniy Polyakov | 1200337 | 2006-03-23 19:11:58 +0300 | [diff] [blame] | 685 | |
David Fries | 9fcbbac | 2014-01-15 22:29:18 -0600 | [diff] [blame] | 686 | mutex_lock(&dev->list_mutex); |
| 687 | list_add_tail(&node->async.async_entry, &dev->async_list); |
| 688 | wake_up_process(dev->thread); |
| 689 | mutex_unlock(&dev->list_mutex); |
| 690 | ++node; |
Evgeniy Polyakov | 1200337 | 2006-03-23 19:11:58 +0300 | [diff] [blame] | 691 | |
Evgeniy Polyakov | 1200337 | 2006-03-23 19:11:58 +0300 | [diff] [blame] | 692 | out_cont: |
David Fries | 8a0427d | 2014-04-08 22:37:09 -0500 | [diff] [blame] | 693 | /* Can't queue because that modifies block and another |
| 694 | * thread could be processing the messages by now and |
| 695 | * there isn't a lock, send directly. |
| 696 | */ |
David Fries | 9fcbbac | 2014-01-15 22:29:18 -0600 | [diff] [blame] | 697 | if (err) |
David Fries | 8a0427d | 2014-04-08 22:37:09 -0500 | [diff] [blame] | 698 | w1_netlink_send_error(cn, msg, nsp->portid, err); |
| 699 | msg_len -= sizeof(struct w1_netlink_msg) + msg->len; |
| 700 | msg = (struct w1_netlink_msg *)(((u8 *)msg) + |
| 701 | sizeof(struct w1_netlink_msg) + msg->len); |
Evgeniy Polyakov | 1200337 | 2006-03-23 19:11:58 +0300 | [diff] [blame] | 702 | |
| 703 | /* |
| 704 | * Let's allow requests for nonexisting devices. |
| 705 | */ |
| 706 | if (err == -ENODEV) |
| 707 | err = 0; |
| 708 | } |
David Fries | 8a0427d | 2014-04-08 22:37:09 -0500 | [diff] [blame] | 709 | if (block) |
| 710 | w1_unref_block(block); |
Evgeniy Polyakov | 1200337 | 2006-03-23 19:11:58 +0300 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | int w1_init_netlink(void) |
| 714 | { |
| 715 | struct cb_id w1_id = {.idx = CN_W1_IDX, .val = CN_W1_VAL}; |
| 716 | |
| 717 | return cn_add_callback(&w1_id, "w1", &w1_cn_callback); |
| 718 | } |
| 719 | |
| 720 | void w1_fini_netlink(void) |
| 721 | { |
| 722 | struct cb_id w1_id = {.idx = CN_W1_IDX, .val = CN_W1_VAL}; |
| 723 | |
| 724 | cn_del_callback(&w1_id); |
Evgeniy Polyakov | 2d83317 | 2005-07-27 13:10:11 +0400 | [diff] [blame] | 725 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | #else |
David Fries | 8a0427d | 2014-04-08 22:37:09 -0500 | [diff] [blame] | 727 | void w1_netlink_send(struct w1_master *dev, struct w1_netlink_msg *cn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | { |
| 729 | } |
Evgeniy Polyakov | 2d83317 | 2005-07-27 13:10:11 +0400 | [diff] [blame] | 730 | |
Evgeniy Polyakov | 1200337 | 2006-03-23 19:11:58 +0300 | [diff] [blame] | 731 | int w1_init_netlink(void) |
Evgeniy Polyakov | 2d83317 | 2005-07-27 13:10:11 +0400 | [diff] [blame] | 732 | { |
| 733 | return 0; |
| 734 | } |
| 735 | |
Evgeniy Polyakov | 1200337 | 2006-03-23 19:11:58 +0300 | [diff] [blame] | 736 | void w1_fini_netlink(void) |
Evgeniy Polyakov | 2d83317 | 2005-07-27 13:10:11 +0400 | [diff] [blame] | 737 | { |
| 738 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | #endif |