Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * w1_netlink.c |
| 3 | * |
Evgeniy Polyakov | a801876 | 2011-08-25 15:59:06 -0700 | [diff] [blame] | 4 | * Copyright (c) 2003 Evgeniy Polyakov <zbr@ioremap.net> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 | */ |
| 21 | |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 22 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/skbuff.h> |
| 24 | #include <linux/netlink.h> |
Evgeniy Polyakov | 1200337 | 2006-03-23 19:11:58 +0300 | [diff] [blame] | 25 | #include <linux/connector.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
| 27 | #include "w1.h" |
| 28 | #include "w1_log.h" |
| 29 | #include "w1_netlink.h" |
| 30 | |
Evgeniy Polyakov | 46e07f6 | 2006-04-03 12:01:12 +0400 | [diff] [blame] | 31 | #if defined(CONFIG_W1_CON) && (defined(CONFIG_CONNECTOR) || (defined(CONFIG_CONNECTOR_MODULE) && defined(CONFIG_W1_MODULE))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | void w1_netlink_send(struct w1_master *dev, struct w1_netlink_msg *msg) |
| 33 | { |
Evgeniy Polyakov | 1200337 | 2006-03-23 19:11:58 +0300 | [diff] [blame] | 34 | char buf[sizeof(struct cn_msg) + sizeof(struct w1_netlink_msg)]; |
| 35 | struct cn_msg *m = (struct cn_msg *)buf; |
| 36 | struct w1_netlink_msg *w = (struct w1_netlink_msg *)(m+1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
Evgeniy Polyakov | 1200337 | 2006-03-23 19:11:58 +0300 | [diff] [blame] | 38 | memset(buf, 0, sizeof(buf)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
Evgeniy Polyakov | 1200337 | 2006-03-23 19:11:58 +0300 | [diff] [blame] | 40 | m->id.idx = CN_W1_IDX; |
| 41 | m->id.val = CN_W1_VAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
Evgeniy Polyakov | 1200337 | 2006-03-23 19:11:58 +0300 | [diff] [blame] | 43 | m->seq = dev->seq++; |
| 44 | m->len = sizeof(struct w1_netlink_msg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
Evgeniy Polyakov | 1200337 | 2006-03-23 19:11:58 +0300 | [diff] [blame] | 46 | memcpy(w, msg, sizeof(struct w1_netlink_msg)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | |
David Fries | 5dbf567 | 2014-01-15 22:29:20 -0600 | [diff] [blame^] | 48 | cn_netlink_send(m, dev->portid, 0, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | } |
Evgeniy Polyakov | 2d83317 | 2005-07-27 13:10:11 +0400 | [diff] [blame] | 50 | |
Evgeniy Polyakov | 3b83840 | 2009-01-07 18:09:01 -0800 | [diff] [blame] | 51 | static void w1_send_slave(struct w1_master *dev, u64 rn) |
Evgeniy Polyakov | 2d83317 | 2005-07-27 13:10:11 +0400 | [diff] [blame] | 52 | { |
Evgeniy Polyakov | 3b83840 | 2009-01-07 18:09:01 -0800 | [diff] [blame] | 53 | struct cn_msg *msg = dev->priv; |
| 54 | struct w1_netlink_msg *hdr = (struct w1_netlink_msg *)(msg + 1); |
| 55 | struct w1_netlink_cmd *cmd = (struct w1_netlink_cmd *)(hdr + 1); |
| 56 | int avail; |
David Fries | 6b355b3 | 2014-01-15 22:29:12 -0600 | [diff] [blame] | 57 | u64 *data; |
Evgeniy Polyakov | 2d83317 | 2005-07-27 13:10:11 +0400 | [diff] [blame] | 58 | |
Evgeniy Polyakov | 3b83840 | 2009-01-07 18:09:01 -0800 | [diff] [blame] | 59 | avail = dev->priv_size - cmd->len; |
Evgeniy Polyakov | 1200337 | 2006-03-23 19:11:58 +0300 | [diff] [blame] | 60 | |
David Fries | 6b355b3 | 2014-01-15 22:29:12 -0600 | [diff] [blame] | 61 | if (avail < 8) { |
| 62 | msg->ack++; |
David Fries | 5dbf567 | 2014-01-15 22:29:20 -0600 | [diff] [blame^] | 63 | cn_netlink_send(msg, dev->portid, 0, GFP_KERNEL); |
Evgeniy Polyakov | 3b83840 | 2009-01-07 18:09:01 -0800 | [diff] [blame] | 64 | |
David Fries | 6b355b3 | 2014-01-15 22:29:12 -0600 | [diff] [blame] | 65 | msg->len = sizeof(struct w1_netlink_msg) + |
| 66 | sizeof(struct w1_netlink_cmd); |
| 67 | hdr->len = sizeof(struct w1_netlink_cmd); |
| 68 | cmd->len = 0; |
Evgeniy Polyakov | 3b83840 | 2009-01-07 18:09:01 -0800 | [diff] [blame] | 69 | } |
| 70 | |
David Fries | 6b355b3 | 2014-01-15 22:29:12 -0600 | [diff] [blame] | 71 | data = (void *)(cmd + 1) + cmd->len; |
Evgeniy Polyakov | 3b83840 | 2009-01-07 18:09:01 -0800 | [diff] [blame] | 72 | |
David Fries | 6b355b3 | 2014-01-15 22:29:12 -0600 | [diff] [blame] | 73 | *data = rn; |
| 74 | cmd->len += 8; |
| 75 | hdr->len += 8; |
| 76 | msg->len += 8; |
Evgeniy Polyakov | 3b83840 | 2009-01-07 18:09:01 -0800 | [diff] [blame] | 77 | } |
| 78 | |
David Fries | 70b34d2 | 2014-01-15 22:29:17 -0600 | [diff] [blame] | 79 | static void w1_found_send_slave(struct w1_master *dev, u64 rn) |
Evgeniy Polyakov | 3b83840 | 2009-01-07 18:09:01 -0800 | [diff] [blame] | 80 | { |
David Fries | 70b34d2 | 2014-01-15 22:29:17 -0600 | [diff] [blame] | 81 | /* update kernel slave list */ |
| 82 | w1_slave_found(dev, rn); |
| 83 | |
| 84 | w1_send_slave(dev, rn); |
| 85 | } |
| 86 | |
| 87 | /* Get the current slave list, or search (with or without alarm) */ |
| 88 | static int w1_get_slaves(struct w1_master *dev, |
| 89 | struct cn_msg *req_msg, struct w1_netlink_msg *req_hdr, |
| 90 | struct w1_netlink_cmd *req_cmd) |
| 91 | { |
| 92 | struct cn_msg *msg; |
| 93 | struct w1_netlink_msg *hdr; |
| 94 | struct w1_netlink_cmd *cmd; |
| 95 | struct w1_slave *sl; |
| 96 | |
| 97 | msg = kzalloc(PAGE_SIZE, GFP_KERNEL); |
| 98 | if (!msg) |
| 99 | return -ENOMEM; |
| 100 | |
| 101 | msg->id = req_msg->id; |
| 102 | msg->seq = req_msg->seq; |
| 103 | msg->ack = 0; |
| 104 | msg->len = sizeof(struct w1_netlink_msg) + |
| 105 | sizeof(struct w1_netlink_cmd); |
| 106 | |
| 107 | hdr = (struct w1_netlink_msg *)(msg + 1); |
| 108 | cmd = (struct w1_netlink_cmd *)(hdr + 1); |
| 109 | |
| 110 | hdr->type = W1_MASTER_CMD; |
| 111 | hdr->id = req_hdr->id; |
| 112 | hdr->len = sizeof(struct w1_netlink_cmd); |
| 113 | |
| 114 | cmd->cmd = req_cmd->cmd; |
| 115 | cmd->len = 0; |
Evgeniy Polyakov | 3b83840 | 2009-01-07 18:09:01 -0800 | [diff] [blame] | 116 | |
| 117 | dev->priv = msg; |
David Fries | 70b34d2 | 2014-01-15 22:29:17 -0600 | [diff] [blame] | 118 | dev->priv_size = PAGE_SIZE - msg->len - sizeof(struct cn_msg); |
Evgeniy Polyakov | 3b83840 | 2009-01-07 18:09:01 -0800 | [diff] [blame] | 119 | |
David Fries | 70b34d2 | 2014-01-15 22:29:17 -0600 | [diff] [blame] | 120 | if (req_cmd->cmd == W1_CMD_LIST_SLAVES) { |
| 121 | __u64 rn; |
David Fries | 9fcbbac | 2014-01-15 22:29:18 -0600 | [diff] [blame] | 122 | mutex_lock(&dev->list_mutex); |
David Fries | 70b34d2 | 2014-01-15 22:29:17 -0600 | [diff] [blame] | 123 | list_for_each_entry(sl, &dev->slist, w1_slave_entry) { |
| 124 | memcpy(&rn, &sl->reg_num, sizeof(rn)); |
| 125 | w1_send_slave(dev, rn); |
| 126 | } |
David Fries | 9fcbbac | 2014-01-15 22:29:18 -0600 | [diff] [blame] | 127 | mutex_unlock(&dev->list_mutex); |
David Fries | 70b34d2 | 2014-01-15 22:29:17 -0600 | [diff] [blame] | 128 | } else { |
| 129 | w1_search_process_cb(dev, cmd->cmd == W1_CMD_ALARM_SEARCH ? |
| 130 | W1_ALARM_SEARCH : W1_SEARCH, w1_found_send_slave); |
| 131 | } |
Evgeniy Polyakov | 3b83840 | 2009-01-07 18:09:01 -0800 | [diff] [blame] | 132 | |
| 133 | msg->ack = 0; |
David Fries | 5dbf567 | 2014-01-15 22:29:20 -0600 | [diff] [blame^] | 134 | cn_netlink_send(msg, dev->portid, 0, GFP_KERNEL); |
Evgeniy Polyakov | 3b83840 | 2009-01-07 18:09:01 -0800 | [diff] [blame] | 135 | |
| 136 | dev->priv = NULL; |
| 137 | dev->priv_size = 0; |
| 138 | |
David Fries | 70b34d2 | 2014-01-15 22:29:17 -0600 | [diff] [blame] | 139 | kfree(msg); |
| 140 | |
Evgeniy Polyakov | 2d83317 | 2005-07-27 13:10:11 +0400 | [diff] [blame] | 141 | return 0; |
| 142 | } |
| 143 | |
Evgeniy Polyakov | c7e2663 | 2009-01-07 18:09:03 -0800 | [diff] [blame] | 144 | static int w1_send_read_reply(struct cn_msg *msg, struct w1_netlink_msg *hdr, |
David Fries | 5dbf567 | 2014-01-15 22:29:20 -0600 | [diff] [blame^] | 145 | struct w1_netlink_cmd *cmd, u32 portid) |
Evgeniy Polyakov | c7e2663 | 2009-01-07 18:09:03 -0800 | [diff] [blame] | 146 | { |
| 147 | void *data; |
| 148 | struct w1_netlink_msg *h; |
| 149 | struct w1_netlink_cmd *c; |
| 150 | struct cn_msg *cm; |
| 151 | int err; |
| 152 | |
| 153 | data = kzalloc(sizeof(struct cn_msg) + |
| 154 | sizeof(struct w1_netlink_msg) + |
| 155 | sizeof(struct w1_netlink_cmd) + |
| 156 | cmd->len, GFP_KERNEL); |
| 157 | if (!data) |
| 158 | return -ENOMEM; |
| 159 | |
| 160 | cm = (struct cn_msg *)(data); |
| 161 | h = (struct w1_netlink_msg *)(cm + 1); |
| 162 | c = (struct w1_netlink_cmd *)(h + 1); |
| 163 | |
| 164 | memcpy(cm, msg, sizeof(struct cn_msg)); |
| 165 | memcpy(h, hdr, sizeof(struct w1_netlink_msg)); |
| 166 | memcpy(c, cmd, sizeof(struct w1_netlink_cmd)); |
| 167 | |
| 168 | cm->ack = msg->seq+1; |
| 169 | cm->len = sizeof(struct w1_netlink_msg) + |
| 170 | sizeof(struct w1_netlink_cmd) + cmd->len; |
| 171 | |
| 172 | h->len = sizeof(struct w1_netlink_cmd) + cmd->len; |
| 173 | |
| 174 | memcpy(c->data, cmd->data, c->len); |
| 175 | |
David Fries | 5dbf567 | 2014-01-15 22:29:20 -0600 | [diff] [blame^] | 176 | err = cn_netlink_send(cm, portid, 0, GFP_KERNEL); |
Evgeniy Polyakov | c7e2663 | 2009-01-07 18:09:03 -0800 | [diff] [blame] | 177 | |
| 178 | kfree(data); |
| 179 | |
| 180 | return err; |
| 181 | } |
| 182 | |
| 183 | static int w1_process_command_io(struct w1_master *dev, struct cn_msg *msg, |
| 184 | struct w1_netlink_msg *hdr, struct w1_netlink_cmd *cmd) |
| 185 | { |
| 186 | int err = 0; |
| 187 | |
| 188 | switch (cmd->cmd) { |
| 189 | case W1_CMD_TOUCH: |
| 190 | w1_touch_block(dev, cmd->data, cmd->len); |
David Fries | 5dbf567 | 2014-01-15 22:29:20 -0600 | [diff] [blame^] | 191 | w1_send_read_reply(msg, hdr, cmd, dev->portid); |
Evgeniy Polyakov | c7e2663 | 2009-01-07 18:09:03 -0800 | [diff] [blame] | 192 | break; |
| 193 | case W1_CMD_READ: |
| 194 | w1_read_block(dev, cmd->data, cmd->len); |
David Fries | 5dbf567 | 2014-01-15 22:29:20 -0600 | [diff] [blame^] | 195 | w1_send_read_reply(msg, hdr, cmd, dev->portid); |
Evgeniy Polyakov | c7e2663 | 2009-01-07 18:09:03 -0800 | [diff] [blame] | 196 | break; |
| 197 | case W1_CMD_WRITE: |
| 198 | w1_write_block(dev, cmd->data, cmd->len); |
| 199 | break; |
| 200 | default: |
Evgeniy Polyakov | 4037014 | 2009-01-07 18:09:05 -0800 | [diff] [blame] | 201 | err = -EINVAL; |
Evgeniy Polyakov | c7e2663 | 2009-01-07 18:09:03 -0800 | [diff] [blame] | 202 | break; |
| 203 | } |
| 204 | |
| 205 | return err; |
| 206 | } |
| 207 | |
David Fries | 70b34d2 | 2014-01-15 22:29:17 -0600 | [diff] [blame] | 208 | static int w1_process_command_addremove(struct w1_master *dev, |
| 209 | struct cn_msg *msg, struct w1_netlink_msg *hdr, |
| 210 | struct w1_netlink_cmd *cmd) |
| 211 | { |
| 212 | struct w1_slave *sl; |
| 213 | int err = 0; |
| 214 | struct w1_reg_num *id; |
| 215 | |
| 216 | if (cmd->len != 8) |
| 217 | return -EINVAL; |
| 218 | |
| 219 | id = (struct w1_reg_num *)cmd->data; |
| 220 | |
| 221 | sl = w1_slave_search_device(dev, id); |
| 222 | switch (cmd->cmd) { |
| 223 | case W1_CMD_SLAVE_ADD: |
| 224 | if (sl) |
| 225 | err = -EINVAL; |
| 226 | else |
| 227 | err = w1_attach_slave_device(dev, id); |
| 228 | break; |
| 229 | case W1_CMD_SLAVE_REMOVE: |
| 230 | if (sl) |
| 231 | w1_slave_detach(sl); |
| 232 | else |
| 233 | err = -EINVAL; |
| 234 | break; |
| 235 | default: |
| 236 | err = -EINVAL; |
| 237 | break; |
| 238 | } |
| 239 | |
| 240 | return err; |
| 241 | } |
| 242 | |
| 243 | static int w1_process_command_master(struct w1_master *dev, |
| 244 | struct cn_msg *req_msg, struct w1_netlink_msg *req_hdr, |
| 245 | struct w1_netlink_cmd *req_cmd) |
Evgeniy Polyakov | 3b83840 | 2009-01-07 18:09:01 -0800 | [diff] [blame] | 246 | { |
| 247 | int err = -EINVAL; |
Evgeniy Polyakov | 3b83840 | 2009-01-07 18:09:01 -0800 | [diff] [blame] | 248 | |
David Fries | 70b34d2 | 2014-01-15 22:29:17 -0600 | [diff] [blame] | 249 | switch (req_cmd->cmd) { |
Evgeniy Polyakov | c7e2663 | 2009-01-07 18:09:03 -0800 | [diff] [blame] | 250 | case W1_CMD_SEARCH: |
| 251 | case W1_CMD_ALARM_SEARCH: |
David Fries | 70b34d2 | 2014-01-15 22:29:17 -0600 | [diff] [blame] | 252 | case W1_CMD_LIST_SLAVES: |
| 253 | err = w1_get_slaves(dev, req_msg, req_hdr, req_cmd); |
Evgeniy Polyakov | c7e2663 | 2009-01-07 18:09:03 -0800 | [diff] [blame] | 254 | break; |
| 255 | case W1_CMD_READ: |
| 256 | case W1_CMD_WRITE: |
| 257 | case W1_CMD_TOUCH: |
Evgeniy Polyakov | 4037014 | 2009-01-07 18:09:05 -0800 | [diff] [blame] | 258 | err = w1_process_command_io(dev, req_msg, req_hdr, req_cmd); |
Evgeniy Polyakov | c7e2663 | 2009-01-07 18:09:03 -0800 | [diff] [blame] | 259 | break; |
Evgeniy Polyakov | f89735c | 2009-01-07 18:09:04 -0800 | [diff] [blame] | 260 | case W1_CMD_RESET: |
| 261 | err = w1_reset_bus(dev); |
| 262 | break; |
David Fries | 70b34d2 | 2014-01-15 22:29:17 -0600 | [diff] [blame] | 263 | case W1_CMD_SLAVE_ADD: |
| 264 | case W1_CMD_SLAVE_REMOVE: |
| 265 | err = w1_process_command_addremove(dev, req_msg, req_hdr, |
| 266 | req_cmd); |
| 267 | break; |
Evgeniy Polyakov | c7e2663 | 2009-01-07 18:09:03 -0800 | [diff] [blame] | 268 | default: |
Evgeniy Polyakov | 4037014 | 2009-01-07 18:09:05 -0800 | [diff] [blame] | 269 | err = -EINVAL; |
Evgeniy Polyakov | c7e2663 | 2009-01-07 18:09:03 -0800 | [diff] [blame] | 270 | break; |
Evgeniy Polyakov | 3b83840 | 2009-01-07 18:09:01 -0800 | [diff] [blame] | 271 | } |
| 272 | |
Evgeniy Polyakov | 3b83840 | 2009-01-07 18:09:01 -0800 | [diff] [blame] | 273 | return err; |
| 274 | } |
| 275 | |
Evgeniy Polyakov | 1200337 | 2006-03-23 19:11:58 +0300 | [diff] [blame] | 276 | static int w1_process_command_slave(struct w1_slave *sl, struct cn_msg *msg, |
| 277 | struct w1_netlink_msg *hdr, struct w1_netlink_cmd *cmd) |
| 278 | { |
Evgeniy Polyakov | 1200337 | 2006-03-23 19:11:58 +0300 | [diff] [blame] | 279 | 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] | 280 | __func__, sl->reg_num.family, (unsigned long long)sl->reg_num.id, |
| 281 | sl->reg_num.crc, cmd->cmd, cmd->len); |
Evgeniy Polyakov | 1200337 | 2006-03-23 19:11:58 +0300 | [diff] [blame] | 282 | |
Evgeniy Polyakov | c7e2663 | 2009-01-07 18:09:03 -0800 | [diff] [blame] | 283 | return w1_process_command_io(sl->master, msg, hdr, cmd); |
Evgeniy Polyakov | 1200337 | 2006-03-23 19:11:58 +0300 | [diff] [blame] | 284 | } |
| 285 | |
David Fries | 5dbf567 | 2014-01-15 22:29:20 -0600 | [diff] [blame^] | 286 | static int w1_process_command_root(struct cn_msg *msg, |
| 287 | struct w1_netlink_msg *mcmd, u32 portid) |
Evgeniy Polyakov | 610705e | 2009-01-07 18:08:59 -0800 | [diff] [blame] | 288 | { |
| 289 | struct w1_master *m; |
| 290 | struct cn_msg *cn; |
| 291 | struct w1_netlink_msg *w; |
| 292 | u32 *id; |
| 293 | |
| 294 | if (mcmd->type != W1_LIST_MASTERS) { |
| 295 | printk(KERN_NOTICE "%s: msg: %x.%x, wrong type: %u, len: %u.\n", |
Evgeniy Polyakov | c7e2663 | 2009-01-07 18:09:03 -0800 | [diff] [blame] | 296 | __func__, msg->id.idx, msg->id.val, mcmd->type, mcmd->len); |
Evgeniy Polyakov | 610705e | 2009-01-07 18:08:59 -0800 | [diff] [blame] | 297 | return -EPROTO; |
| 298 | } |
| 299 | |
| 300 | cn = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 301 | if (!cn) |
| 302 | return -ENOMEM; |
| 303 | |
| 304 | cn->id.idx = CN_W1_IDX; |
| 305 | cn->id.val = CN_W1_VAL; |
| 306 | |
| 307 | cn->seq = msg->seq; |
| 308 | cn->ack = 1; |
| 309 | cn->len = sizeof(struct w1_netlink_msg); |
| 310 | w = (struct w1_netlink_msg *)(cn + 1); |
| 311 | |
| 312 | w->type = W1_LIST_MASTERS; |
Evgeniy Polyakov | 4037014 | 2009-01-07 18:09:05 -0800 | [diff] [blame] | 313 | w->status = 0; |
Evgeniy Polyakov | 610705e | 2009-01-07 18:08:59 -0800 | [diff] [blame] | 314 | w->len = 0; |
| 315 | id = (u32 *)(w + 1); |
| 316 | |
| 317 | mutex_lock(&w1_mlock); |
| 318 | list_for_each_entry(m, &w1_masters, w1_master_entry) { |
| 319 | if (cn->len + sizeof(*id) > PAGE_SIZE - sizeof(struct cn_msg)) { |
David Fries | 5dbf567 | 2014-01-15 22:29:20 -0600 | [diff] [blame^] | 320 | cn_netlink_send(cn, portid, 0, GFP_KERNEL); |
Evgeniy Polyakov | 610705e | 2009-01-07 18:08:59 -0800 | [diff] [blame] | 321 | cn->ack++; |
| 322 | cn->len = sizeof(struct w1_netlink_msg); |
| 323 | w->len = 0; |
| 324 | id = (u32 *)(w + 1); |
| 325 | } |
| 326 | |
| 327 | *id = m->id; |
| 328 | w->len += sizeof(*id); |
| 329 | cn->len += sizeof(*id); |
| 330 | id++; |
| 331 | } |
| 332 | cn->ack = 0; |
David Fries | 5dbf567 | 2014-01-15 22:29:20 -0600 | [diff] [blame^] | 333 | cn_netlink_send(cn, portid, 0, GFP_KERNEL); |
Evgeniy Polyakov | 610705e | 2009-01-07 18:08:59 -0800 | [diff] [blame] | 334 | mutex_unlock(&w1_mlock); |
| 335 | |
| 336 | kfree(cn); |
| 337 | return 0; |
| 338 | } |
| 339 | |
Evgeniy Polyakov | 4037014 | 2009-01-07 18:09:05 -0800 | [diff] [blame] | 340 | static int w1_netlink_send_error(struct cn_msg *rcmsg, struct w1_netlink_msg *rmsg, |
David Fries | 5dbf567 | 2014-01-15 22:29:20 -0600 | [diff] [blame^] | 341 | struct w1_netlink_cmd *rcmd, int portid, int error) |
Evgeniy Polyakov | 4037014 | 2009-01-07 18:09:05 -0800 | [diff] [blame] | 342 | { |
| 343 | struct cn_msg *cmsg; |
| 344 | struct w1_netlink_msg *msg; |
| 345 | struct w1_netlink_cmd *cmd; |
| 346 | |
| 347 | cmsg = kzalloc(sizeof(*msg) + sizeof(*cmd) + sizeof(*cmsg), GFP_KERNEL); |
| 348 | if (!cmsg) |
| 349 | return -ENOMEM; |
| 350 | |
| 351 | msg = (struct w1_netlink_msg *)(cmsg + 1); |
| 352 | cmd = (struct w1_netlink_cmd *)(msg + 1); |
| 353 | |
| 354 | memcpy(cmsg, rcmsg, sizeof(*cmsg)); |
| 355 | cmsg->len = sizeof(*msg); |
| 356 | |
| 357 | memcpy(msg, rmsg, sizeof(*msg)); |
| 358 | msg->len = 0; |
| 359 | msg->status = (short)-error; |
| 360 | |
| 361 | if (rcmd) { |
| 362 | memcpy(cmd, rcmd, sizeof(*cmd)); |
| 363 | cmd->len = 0; |
| 364 | msg->len += sizeof(*cmd); |
| 365 | cmsg->len += sizeof(*cmd); |
| 366 | } |
| 367 | |
David Fries | 5dbf567 | 2014-01-15 22:29:20 -0600 | [diff] [blame^] | 368 | error = cn_netlink_send(cmsg, portid, 0, GFP_KERNEL); |
Evgeniy Polyakov | 4037014 | 2009-01-07 18:09:05 -0800 | [diff] [blame] | 369 | kfree(cmsg); |
| 370 | |
| 371 | return error; |
| 372 | } |
| 373 | |
David Fries | 9fcbbac | 2014-01-15 22:29:18 -0600 | [diff] [blame] | 374 | /* Bundle together a reference count, the full message, and broken out |
| 375 | * commands to be executed on each w1 master kthread in one memory allocation. |
| 376 | */ |
| 377 | struct w1_cb_block { |
| 378 | atomic_t refcnt; |
David Fries | 5dbf567 | 2014-01-15 22:29:20 -0600 | [diff] [blame^] | 379 | u32 portid; /* Sending process port ID */ |
David Fries | 9fcbbac | 2014-01-15 22:29:18 -0600 | [diff] [blame] | 380 | struct cn_msg msg; |
| 381 | /* cn_msg data */ |
| 382 | /* one or more variable length struct w1_cb_node */ |
| 383 | }; |
| 384 | struct w1_cb_node { |
| 385 | struct w1_async_cmd async; |
| 386 | /* pointers within w1_cb_block and msg data */ |
| 387 | struct w1_cb_block *block; |
| 388 | struct w1_netlink_msg *m; |
| 389 | struct w1_slave *sl; |
| 390 | struct w1_master *dev; |
| 391 | }; |
| 392 | |
| 393 | static void w1_process_cb(struct w1_master *dev, struct w1_async_cmd *async_cmd) |
| 394 | { |
| 395 | struct w1_cb_node *node = container_of(async_cmd, struct w1_cb_node, |
| 396 | async); |
| 397 | u16 mlen = node->m->len; |
| 398 | u8 *cmd_data = node->m->data; |
| 399 | int err = 0; |
| 400 | struct w1_slave *sl = node->sl; |
| 401 | struct w1_netlink_cmd *cmd = NULL; |
| 402 | |
| 403 | mutex_lock(&dev->mutex); |
David Fries | 5dbf567 | 2014-01-15 22:29:20 -0600 | [diff] [blame^] | 404 | dev->portid = node->block->portid; |
David Fries | 9fcbbac | 2014-01-15 22:29:18 -0600 | [diff] [blame] | 405 | if (sl && w1_reset_select_slave(sl)) |
| 406 | err = -ENODEV; |
| 407 | |
| 408 | while (mlen && !err) { |
| 409 | cmd = (struct w1_netlink_cmd *)cmd_data; |
| 410 | |
| 411 | if (cmd->len + sizeof(struct w1_netlink_cmd) > mlen) { |
| 412 | err = -E2BIG; |
| 413 | break; |
| 414 | } |
| 415 | |
| 416 | if (sl) |
| 417 | err = w1_process_command_slave(sl, &node->block->msg, |
| 418 | node->m, cmd); |
| 419 | else |
| 420 | err = w1_process_command_master(dev, &node->block->msg, |
| 421 | node->m, cmd); |
| 422 | |
David Fries | 5dbf567 | 2014-01-15 22:29:20 -0600 | [diff] [blame^] | 423 | w1_netlink_send_error(&node->block->msg, node->m, cmd, |
| 424 | node->block->portid, err); |
David Fries | 9fcbbac | 2014-01-15 22:29:18 -0600 | [diff] [blame] | 425 | err = 0; |
| 426 | |
| 427 | cmd_data += cmd->len + sizeof(struct w1_netlink_cmd); |
| 428 | mlen -= cmd->len + sizeof(struct w1_netlink_cmd); |
| 429 | } |
| 430 | |
| 431 | if (!cmd || err) |
David Fries | 5dbf567 | 2014-01-15 22:29:20 -0600 | [diff] [blame^] | 432 | w1_netlink_send_error(&node->block->msg, node->m, cmd, |
| 433 | node->block->portid, err); |
David Fries | 9fcbbac | 2014-01-15 22:29:18 -0600 | [diff] [blame] | 434 | |
| 435 | if (sl) |
| 436 | w1_unref_slave(sl); |
| 437 | else |
| 438 | atomic_dec(&dev->refcnt); |
David Fries | 5dbf567 | 2014-01-15 22:29:20 -0600 | [diff] [blame^] | 439 | dev->portid = 0; |
David Fries | 9fcbbac | 2014-01-15 22:29:18 -0600 | [diff] [blame] | 440 | mutex_unlock(&dev->mutex); |
| 441 | |
| 442 | mutex_lock(&dev->list_mutex); |
| 443 | list_del(&async_cmd->async_entry); |
| 444 | mutex_unlock(&dev->list_mutex); |
| 445 | |
| 446 | if (atomic_sub_return(1, &node->block->refcnt) == 0) |
| 447 | kfree(node->block); |
| 448 | } |
| 449 | |
Philipp Reisner | 7069331 | 2009-10-02 02:40:05 +0000 | [diff] [blame] | 450 | static void w1_cn_callback(struct cn_msg *msg, struct netlink_skb_parms *nsp) |
Evgeniy Polyakov | 1200337 | 2006-03-23 19:11:58 +0300 | [diff] [blame] | 451 | { |
Evgeniy Polyakov | 1200337 | 2006-03-23 19:11:58 +0300 | [diff] [blame] | 452 | struct w1_netlink_msg *m = (struct w1_netlink_msg *)(msg + 1); |
Evgeniy Polyakov | 1200337 | 2006-03-23 19:11:58 +0300 | [diff] [blame] | 453 | struct w1_slave *sl; |
| 454 | struct w1_master *dev; |
David Fries | 9fcbbac | 2014-01-15 22:29:18 -0600 | [diff] [blame] | 455 | u16 msg_len; |
Evgeniy Polyakov | 1200337 | 2006-03-23 19:11:58 +0300 | [diff] [blame] | 456 | int err = 0; |
David Fries | 9fcbbac | 2014-01-15 22:29:18 -0600 | [diff] [blame] | 457 | struct w1_cb_block *block = NULL; |
| 458 | struct w1_cb_node *node = NULL; |
| 459 | int node_count = 0; |
Evgeniy Polyakov | 1200337 | 2006-03-23 19:11:58 +0300 | [diff] [blame] | 460 | |
David Fries | 9fcbbac | 2014-01-15 22:29:18 -0600 | [diff] [blame] | 461 | /* Count the number of master or slave commands there are to allocate |
| 462 | * space for one cb_node each. |
| 463 | */ |
| 464 | msg_len = msg->len; |
| 465 | while (msg_len && !err) { |
| 466 | if (m->len + sizeof(struct w1_netlink_msg) > msg_len) { |
| 467 | err = -E2BIG; |
| 468 | break; |
| 469 | } |
| 470 | |
| 471 | if (m->type == W1_MASTER_CMD || m->type == W1_SLAVE_CMD) |
| 472 | ++node_count; |
| 473 | |
| 474 | msg_len -= sizeof(struct w1_netlink_msg) + m->len; |
| 475 | m = (struct w1_netlink_msg *)(((u8 *)m) + |
| 476 | sizeof(struct w1_netlink_msg) + m->len); |
| 477 | } |
| 478 | m = (struct w1_netlink_msg *)(msg + 1); |
| 479 | if (node_count) { |
| 480 | /* msg->len doesn't include itself */ |
| 481 | long size = sizeof(struct w1_cb_block) + msg->len + |
| 482 | node_count*sizeof(struct w1_cb_node); |
| 483 | block = kmalloc(size, GFP_KERNEL); |
| 484 | if (!block) { |
David Fries | 5dbf567 | 2014-01-15 22:29:20 -0600 | [diff] [blame^] | 485 | w1_netlink_send_error(msg, m, NULL, nsp->portid, |
| 486 | -ENOMEM); |
David Fries | 9fcbbac | 2014-01-15 22:29:18 -0600 | [diff] [blame] | 487 | return; |
| 488 | } |
| 489 | atomic_set(&block->refcnt, 1); |
David Fries | 5dbf567 | 2014-01-15 22:29:20 -0600 | [diff] [blame^] | 490 | block->portid = nsp->portid; |
David Fries | 9fcbbac | 2014-01-15 22:29:18 -0600 | [diff] [blame] | 491 | memcpy(&block->msg, msg, sizeof(*msg) + msg->len); |
| 492 | node = (struct w1_cb_node *)((u8 *)block->msg.data + msg->len); |
| 493 | } |
| 494 | |
| 495 | msg_len = msg->len; |
| 496 | while (msg_len && !err) { |
Evgeniy Polyakov | 1200337 | 2006-03-23 19:11:58 +0300 | [diff] [blame] | 497 | struct w1_reg_num id; |
| 498 | u16 mlen = m->len; |
Evgeniy Polyakov | 1200337 | 2006-03-23 19:11:58 +0300 | [diff] [blame] | 499 | |
| 500 | dev = NULL; |
| 501 | sl = NULL; |
| 502 | |
| 503 | memcpy(&id, m->id.id, sizeof(id)); |
| 504 | #if 0 |
| 505 | printk("%s: %02x.%012llx.%02x: type=%02x, len=%u.\n", |
| 506 | __func__, id.family, (unsigned long long)id.id, id.crc, m->type, m->len); |
| 507 | #endif |
David Fries | 9fcbbac | 2014-01-15 22:29:18 -0600 | [diff] [blame] | 508 | if (m->len + sizeof(struct w1_netlink_msg) > msg_len) { |
Evgeniy Polyakov | 1200337 | 2006-03-23 19:11:58 +0300 | [diff] [blame] | 509 | err = -E2BIG; |
| 510 | break; |
| 511 | } |
| 512 | |
Evgeniy Polyakov | 1200337 | 2006-03-23 19:11:58 +0300 | [diff] [blame] | 513 | if (m->type == W1_MASTER_CMD) { |
| 514 | dev = w1_search_master_id(m->id.mst.id); |
| 515 | } else if (m->type == W1_SLAVE_CMD) { |
| 516 | sl = w1_search_slave(&id); |
| 517 | if (sl) |
| 518 | dev = sl->master; |
Evgeniy Polyakov | 610705e | 2009-01-07 18:08:59 -0800 | [diff] [blame] | 519 | } else { |
David Fries | 5dbf567 | 2014-01-15 22:29:20 -0600 | [diff] [blame^] | 520 | err = w1_process_command_root(msg, m, nsp->portid); |
Evgeniy Polyakov | 610705e | 2009-01-07 18:08:59 -0800 | [diff] [blame] | 521 | goto out_cont; |
Evgeniy Polyakov | 1200337 | 2006-03-23 19:11:58 +0300 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | if (!dev) { |
| 525 | err = -ENODEV; |
| 526 | goto out_cont; |
| 527 | } |
| 528 | |
Evgeniy Polyakov | 9be62e0 | 2009-01-07 18:09:01 -0800 | [diff] [blame] | 529 | err = 0; |
| 530 | if (!mlen) |
| 531 | goto out_cont; |
| 532 | |
David Fries | 9fcbbac | 2014-01-15 22:29:18 -0600 | [diff] [blame] | 533 | atomic_inc(&block->refcnt); |
| 534 | node->async.cb = w1_process_cb; |
| 535 | node->block = block; |
| 536 | node->m = (struct w1_netlink_msg *)((u8 *)&block->msg + |
| 537 | (size_t)((u8 *)m - (u8 *)msg)); |
| 538 | node->sl = sl; |
| 539 | node->dev = dev; |
Evgeniy Polyakov | 1200337 | 2006-03-23 19:11:58 +0300 | [diff] [blame] | 540 | |
David Fries | 9fcbbac | 2014-01-15 22:29:18 -0600 | [diff] [blame] | 541 | mutex_lock(&dev->list_mutex); |
| 542 | list_add_tail(&node->async.async_entry, &dev->async_list); |
| 543 | wake_up_process(dev->thread); |
| 544 | mutex_unlock(&dev->list_mutex); |
| 545 | ++node; |
Evgeniy Polyakov | 1200337 | 2006-03-23 19:11:58 +0300 | [diff] [blame] | 546 | |
Evgeniy Polyakov | 1200337 | 2006-03-23 19:11:58 +0300 | [diff] [blame] | 547 | out_cont: |
David Fries | 9fcbbac | 2014-01-15 22:29:18 -0600 | [diff] [blame] | 548 | if (err) |
David Fries | 5dbf567 | 2014-01-15 22:29:20 -0600 | [diff] [blame^] | 549 | w1_netlink_send_error(msg, m, NULL, nsp->portid, err); |
David Fries | 9fcbbac | 2014-01-15 22:29:18 -0600 | [diff] [blame] | 550 | msg_len -= sizeof(struct w1_netlink_msg) + m->len; |
Evgeniy Polyakov | 1200337 | 2006-03-23 19:11:58 +0300 | [diff] [blame] | 551 | m = (struct w1_netlink_msg *)(((u8 *)m) + sizeof(struct w1_netlink_msg) + m->len); |
| 552 | |
| 553 | /* |
| 554 | * Let's allow requests for nonexisting devices. |
| 555 | */ |
| 556 | if (err == -ENODEV) |
| 557 | err = 0; |
| 558 | } |
David Fries | 9fcbbac | 2014-01-15 22:29:18 -0600 | [diff] [blame] | 559 | if (block && atomic_sub_return(1, &block->refcnt) == 0) |
| 560 | kfree(block); |
Evgeniy Polyakov | 1200337 | 2006-03-23 19:11:58 +0300 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | int w1_init_netlink(void) |
| 564 | { |
| 565 | struct cb_id w1_id = {.idx = CN_W1_IDX, .val = CN_W1_VAL}; |
| 566 | |
| 567 | return cn_add_callback(&w1_id, "w1", &w1_cn_callback); |
| 568 | } |
| 569 | |
| 570 | void w1_fini_netlink(void) |
| 571 | { |
| 572 | struct cb_id w1_id = {.idx = CN_W1_IDX, .val = CN_W1_VAL}; |
| 573 | |
| 574 | cn_del_callback(&w1_id); |
Evgeniy Polyakov | 2d83317 | 2005-07-27 13:10:11 +0400 | [diff] [blame] | 575 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | void w1_netlink_send(struct w1_master *dev, struct w1_netlink_msg *msg) |
| 578 | { |
| 579 | } |
Evgeniy Polyakov | 2d83317 | 2005-07-27 13:10:11 +0400 | [diff] [blame] | 580 | |
Evgeniy Polyakov | 1200337 | 2006-03-23 19:11:58 +0300 | [diff] [blame] | 581 | int w1_init_netlink(void) |
Evgeniy Polyakov | 2d83317 | 2005-07-27 13:10:11 +0400 | [diff] [blame] | 582 | { |
| 583 | return 0; |
| 584 | } |
| 585 | |
Evgeniy Polyakov | 1200337 | 2006-03-23 19:11:58 +0300 | [diff] [blame] | 586 | void w1_fini_netlink(void) |
Evgeniy Polyakov | 2d83317 | 2005-07-27 13:10:11 +0400 | [diff] [blame] | 587 | { |
| 588 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | #endif |