Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * w1_netlink.c |
| 3 | * |
| 4 | * Copyright (c) 2003 Evgeniy Polyakov <johnpol@2ka.mipt.ru> |
| 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 | |
| 22 | #include <linux/skbuff.h> |
| 23 | #include <linux/netlink.h> |
Evgeniy Polyakov | 1200337 | 2006-03-23 19:11:58 +0300 | [diff] [blame] | 24 | #include <linux/connector.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
| 26 | #include "w1.h" |
| 27 | #include "w1_log.h" |
| 28 | #include "w1_netlink.h" |
| 29 | |
Evgeniy Polyakov | 46e07f6 | 2006-04-03 12:01:12 +0400 | [diff] [blame] | 30 | #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] | 31 | void w1_netlink_send(struct w1_master *dev, struct w1_netlink_msg *msg) |
| 32 | { |
Evgeniy Polyakov | 1200337 | 2006-03-23 19:11:58 +0300 | [diff] [blame] | 33 | char buf[sizeof(struct cn_msg) + sizeof(struct w1_netlink_msg)]; |
| 34 | struct cn_msg *m = (struct cn_msg *)buf; |
| 35 | struct w1_netlink_msg *w = (struct w1_netlink_msg *)(m+1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
Evgeniy Polyakov | 1200337 | 2006-03-23 19:11:58 +0300 | [diff] [blame] | 37 | memset(buf, 0, sizeof(buf)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
Evgeniy Polyakov | 1200337 | 2006-03-23 19:11:58 +0300 | [diff] [blame] | 39 | m->id.idx = CN_W1_IDX; |
| 40 | m->id.val = CN_W1_VAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
Evgeniy Polyakov | 1200337 | 2006-03-23 19:11:58 +0300 | [diff] [blame] | 42 | m->seq = dev->seq++; |
| 43 | m->len = sizeof(struct w1_netlink_msg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
Evgeniy Polyakov | 1200337 | 2006-03-23 19:11:58 +0300 | [diff] [blame] | 45 | memcpy(w, msg, sizeof(struct w1_netlink_msg)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
Evgeniy Polyakov | 1200337 | 2006-03-23 19:11:58 +0300 | [diff] [blame] | 47 | cn_netlink_send(m, 0, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | } |
Evgeniy Polyakov | 2d83317 | 2005-07-27 13:10:11 +0400 | [diff] [blame] | 49 | |
Evgeniy Polyakov | 1200337 | 2006-03-23 19:11:58 +0300 | [diff] [blame] | 50 | static int w1_process_command_master(struct w1_master *dev, struct cn_msg *msg, |
| 51 | struct w1_netlink_msg *hdr, struct w1_netlink_cmd *cmd) |
Evgeniy Polyakov | 2d83317 | 2005-07-27 13:10:11 +0400 | [diff] [blame] | 52 | { |
Evgeniy Polyakov | 1200337 | 2006-03-23 19:11:58 +0300 | [diff] [blame] | 53 | dev_dbg(&dev->dev, "%s: %s: cmd=%02x, len=%u.\n", |
| 54 | __func__, dev->name, cmd->cmd, cmd->len); |
Evgeniy Polyakov | 2d83317 | 2005-07-27 13:10:11 +0400 | [diff] [blame] | 55 | |
Evgeniy Polyakov | 1200337 | 2006-03-23 19:11:58 +0300 | [diff] [blame] | 56 | if (cmd->cmd != W1_CMD_SEARCH && cmd->cmd != W1_CMD_ALARM_SEARCH) |
| 57 | return -EINVAL; |
| 58 | |
| 59 | w1_search_process(dev, (cmd->cmd == W1_CMD_ALARM_SEARCH)?W1_ALARM_SEARCH:W1_SEARCH); |
Evgeniy Polyakov | 2d83317 | 2005-07-27 13:10:11 +0400 | [diff] [blame] | 60 | return 0; |
| 61 | } |
| 62 | |
Evgeniy Polyakov | 1200337 | 2006-03-23 19:11:58 +0300 | [diff] [blame] | 63 | static int w1_send_read_reply(struct w1_slave *sl, struct cn_msg *msg, |
| 64 | struct w1_netlink_msg *hdr, struct w1_netlink_cmd *cmd) |
Evgeniy Polyakov | 2d83317 | 2005-07-27 13:10:11 +0400 | [diff] [blame] | 65 | { |
Evgeniy Polyakov | 1200337 | 2006-03-23 19:11:58 +0300 | [diff] [blame] | 66 | void *data; |
| 67 | struct w1_netlink_msg *h; |
| 68 | struct w1_netlink_cmd *c; |
| 69 | struct cn_msg *cm; |
| 70 | int err; |
| 71 | |
| 72 | data = kzalloc(sizeof(struct cn_msg) + |
| 73 | sizeof(struct w1_netlink_msg) + |
| 74 | sizeof(struct w1_netlink_cmd) + |
| 75 | cmd->len, GFP_KERNEL); |
| 76 | if (!data) |
| 77 | return -ENOMEM; |
| 78 | |
| 79 | cm = (struct cn_msg *)(data); |
| 80 | h = (struct w1_netlink_msg *)(cm + 1); |
| 81 | c = (struct w1_netlink_cmd *)(h + 1); |
| 82 | |
| 83 | memcpy(cm, msg, sizeof(struct cn_msg)); |
| 84 | memcpy(h, hdr, sizeof(struct w1_netlink_msg)); |
| 85 | memcpy(c, cmd, sizeof(struct w1_netlink_cmd)); |
| 86 | |
| 87 | cm->ack = msg->seq+1; |
| 88 | cm->len = sizeof(struct w1_netlink_msg) + sizeof(struct w1_netlink_cmd) + cmd->len; |
| 89 | |
| 90 | h->len = sizeof(struct w1_netlink_cmd) + cmd->len; |
| 91 | |
| 92 | memcpy(c->data, cmd->data, c->len); |
| 93 | |
| 94 | err = cn_netlink_send(cm, 0, GFP_KERNEL); |
| 95 | |
| 96 | kfree(data); |
| 97 | |
| 98 | return err; |
| 99 | } |
| 100 | |
| 101 | static int w1_process_command_slave(struct w1_slave *sl, struct cn_msg *msg, |
| 102 | struct w1_netlink_msg *hdr, struct w1_netlink_cmd *cmd) |
| 103 | { |
| 104 | int err = 0; |
| 105 | |
| 106 | dev_dbg(&sl->master->dev, "%s: %02x.%012llx.%02x: cmd=%02x, len=%u.\n", |
| 107 | __func__, sl->reg_num.family, (unsigned long long)sl->reg_num.id, sl->reg_num.crc, |
| 108 | cmd->cmd, cmd->len); |
| 109 | |
| 110 | switch (cmd->cmd) { |
| 111 | case W1_CMD_READ: |
| 112 | w1_read_block(sl->master, cmd->data, cmd->len); |
| 113 | w1_send_read_reply(sl, msg, hdr, cmd); |
| 114 | break; |
| 115 | case W1_CMD_WRITE: |
| 116 | w1_write_block(sl->master, cmd->data, cmd->len); |
| 117 | break; |
| 118 | case W1_CMD_SEARCH: |
| 119 | case W1_CMD_ALARM_SEARCH: |
| 120 | w1_search_process(sl->master, |
| 121 | (cmd->cmd == W1_CMD_ALARM_SEARCH)?W1_ALARM_SEARCH:W1_SEARCH); |
| 122 | break; |
| 123 | default: |
| 124 | err = -1; |
| 125 | break; |
| 126 | } |
| 127 | |
| 128 | return err; |
| 129 | } |
| 130 | |
Evgeniy Polyakov | 610705e | 2009-01-07 18:08:59 -0800 | [diff] [blame^] | 131 | static int w1_process_command_root(struct cn_msg *msg, struct w1_netlink_msg *mcmd) |
| 132 | { |
| 133 | struct w1_master *m; |
| 134 | struct cn_msg *cn; |
| 135 | struct w1_netlink_msg *w; |
| 136 | u32 *id; |
| 137 | |
| 138 | if (mcmd->type != W1_LIST_MASTERS) { |
| 139 | printk(KERN_NOTICE "%s: msg: %x.%x, wrong type: %u, len: %u.\n", |
| 140 | __func__, msg->id.idx, msg->id.val, mcmd->type, mcmd->len); |
| 141 | return -EPROTO; |
| 142 | } |
| 143 | |
| 144 | cn = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 145 | if (!cn) |
| 146 | return -ENOMEM; |
| 147 | |
| 148 | cn->id.idx = CN_W1_IDX; |
| 149 | cn->id.val = CN_W1_VAL; |
| 150 | |
| 151 | cn->seq = msg->seq; |
| 152 | cn->ack = 1; |
| 153 | cn->len = sizeof(struct w1_netlink_msg); |
| 154 | w = (struct w1_netlink_msg *)(cn + 1); |
| 155 | |
| 156 | w->type = W1_LIST_MASTERS; |
| 157 | w->reserved = 0; |
| 158 | w->len = 0; |
| 159 | id = (u32 *)(w + 1); |
| 160 | |
| 161 | mutex_lock(&w1_mlock); |
| 162 | list_for_each_entry(m, &w1_masters, w1_master_entry) { |
| 163 | if (cn->len + sizeof(*id) > PAGE_SIZE - sizeof(struct cn_msg)) { |
| 164 | cn_netlink_send(cn, 0, GFP_KERNEL); |
| 165 | cn->ack++; |
| 166 | cn->len = sizeof(struct w1_netlink_msg); |
| 167 | w->len = 0; |
| 168 | id = (u32 *)(w + 1); |
| 169 | } |
| 170 | |
| 171 | *id = m->id; |
| 172 | w->len += sizeof(*id); |
| 173 | cn->len += sizeof(*id); |
| 174 | id++; |
| 175 | } |
| 176 | cn->ack = 0; |
| 177 | cn_netlink_send(cn, 0, GFP_KERNEL); |
| 178 | mutex_unlock(&w1_mlock); |
| 179 | |
| 180 | kfree(cn); |
| 181 | return 0; |
| 182 | } |
| 183 | |
Evgeniy Polyakov | 1200337 | 2006-03-23 19:11:58 +0300 | [diff] [blame] | 184 | static void w1_cn_callback(void *data) |
| 185 | { |
| 186 | struct cn_msg *msg = data; |
| 187 | struct w1_netlink_msg *m = (struct w1_netlink_msg *)(msg + 1); |
| 188 | struct w1_netlink_cmd *cmd; |
| 189 | struct w1_slave *sl; |
| 190 | struct w1_master *dev; |
| 191 | int err = 0; |
| 192 | |
| 193 | while (msg->len && !err) { |
| 194 | struct w1_reg_num id; |
| 195 | u16 mlen = m->len; |
| 196 | u8 *cmd_data = m->data; |
| 197 | |
| 198 | dev = NULL; |
| 199 | sl = NULL; |
| 200 | |
| 201 | memcpy(&id, m->id.id, sizeof(id)); |
| 202 | #if 0 |
| 203 | printk("%s: %02x.%012llx.%02x: type=%02x, len=%u.\n", |
| 204 | __func__, id.family, (unsigned long long)id.id, id.crc, m->type, m->len); |
| 205 | #endif |
| 206 | if (m->len + sizeof(struct w1_netlink_msg) > msg->len) { |
| 207 | err = -E2BIG; |
| 208 | break; |
| 209 | } |
| 210 | |
| 211 | if (!mlen) |
| 212 | goto out_cont; |
| 213 | |
| 214 | if (m->type == W1_MASTER_CMD) { |
| 215 | dev = w1_search_master_id(m->id.mst.id); |
| 216 | } else if (m->type == W1_SLAVE_CMD) { |
| 217 | sl = w1_search_slave(&id); |
| 218 | if (sl) |
| 219 | dev = sl->master; |
Evgeniy Polyakov | 610705e | 2009-01-07 18:08:59 -0800 | [diff] [blame^] | 220 | } else { |
| 221 | err = w1_process_command_root(msg, m); |
| 222 | goto out_cont; |
Evgeniy Polyakov | 1200337 | 2006-03-23 19:11:58 +0300 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | if (!dev) { |
| 226 | err = -ENODEV; |
| 227 | goto out_cont; |
| 228 | } |
| 229 | |
Evgeniy Polyakov | abd52a1 | 2006-04-03 12:04:27 +0400 | [diff] [blame] | 230 | mutex_lock(&dev->mutex); |
Evgeniy Polyakov | 1200337 | 2006-03-23 19:11:58 +0300 | [diff] [blame] | 231 | |
| 232 | if (sl && w1_reset_select_slave(sl)) { |
| 233 | err = -ENODEV; |
| 234 | goto out_up; |
| 235 | } |
| 236 | |
| 237 | while (mlen) { |
| 238 | cmd = (struct w1_netlink_cmd *)cmd_data; |
| 239 | |
| 240 | if (cmd->len + sizeof(struct w1_netlink_cmd) > mlen) { |
| 241 | err = -E2BIG; |
| 242 | break; |
| 243 | } |
| 244 | |
| 245 | if (sl) |
| 246 | w1_process_command_slave(sl, msg, m, cmd); |
| 247 | else |
| 248 | w1_process_command_master(dev, msg, m, cmd); |
| 249 | |
| 250 | cmd_data += cmd->len + sizeof(struct w1_netlink_cmd); |
| 251 | mlen -= cmd->len + sizeof(struct w1_netlink_cmd); |
| 252 | } |
| 253 | out_up: |
| 254 | atomic_dec(&dev->refcnt); |
| 255 | if (sl) |
| 256 | atomic_dec(&sl->refcnt); |
Evgeniy Polyakov | abd52a1 | 2006-04-03 12:04:27 +0400 | [diff] [blame] | 257 | mutex_unlock(&dev->mutex); |
Evgeniy Polyakov | 1200337 | 2006-03-23 19:11:58 +0300 | [diff] [blame] | 258 | out_cont: |
| 259 | msg->len -= sizeof(struct w1_netlink_msg) + m->len; |
| 260 | m = (struct w1_netlink_msg *)(((u8 *)m) + sizeof(struct w1_netlink_msg) + m->len); |
| 261 | |
| 262 | /* |
| 263 | * Let's allow requests for nonexisting devices. |
| 264 | */ |
| 265 | if (err == -ENODEV) |
| 266 | err = 0; |
| 267 | } |
| 268 | #if 0 |
| 269 | if (err) { |
| 270 | printk("%s: malformed message. Dropping.\n", __func__); |
| 271 | } |
| 272 | #endif |
| 273 | } |
| 274 | |
| 275 | int w1_init_netlink(void) |
| 276 | { |
| 277 | struct cb_id w1_id = {.idx = CN_W1_IDX, .val = CN_W1_VAL}; |
| 278 | |
| 279 | return cn_add_callback(&w1_id, "w1", &w1_cn_callback); |
| 280 | } |
| 281 | |
| 282 | void w1_fini_netlink(void) |
| 283 | { |
| 284 | struct cb_id w1_id = {.idx = CN_W1_IDX, .val = CN_W1_VAL}; |
| 285 | |
| 286 | cn_del_callback(&w1_id); |
Evgeniy Polyakov | 2d83317 | 2005-07-27 13:10:11 +0400 | [diff] [blame] | 287 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | void w1_netlink_send(struct w1_master *dev, struct w1_netlink_msg *msg) |
| 290 | { |
| 291 | } |
Evgeniy Polyakov | 2d83317 | 2005-07-27 13:10:11 +0400 | [diff] [blame] | 292 | |
Evgeniy Polyakov | 1200337 | 2006-03-23 19:11:58 +0300 | [diff] [blame] | 293 | int w1_init_netlink(void) |
Evgeniy Polyakov | 2d83317 | 2005-07-27 13:10:11 +0400 | [diff] [blame] | 294 | { |
| 295 | return 0; |
| 296 | } |
| 297 | |
Evgeniy Polyakov | 1200337 | 2006-03-23 19:11:58 +0300 | [diff] [blame] | 298 | void w1_fini_netlink(void) |
Evgeniy Polyakov | 2d83317 | 2005-07-27 13:10:11 +0400 | [diff] [blame] | 299 | { |
| 300 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | #endif |