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