blob: b63109ada5a7bbaf4898017ed419a7e2eef3c299 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * w1_netlink.c
3 *
Evgeniy Polyakova8018762011-08-25 15:59:06 -07004 * Copyright (c) 2003 Evgeniy Polyakov <zbr@ioremap.net>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
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 Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/skbuff.h>
24#include <linux/netlink.h>
Evgeniy Polyakov12003372006-03-23 19:11:58 +030025#include <linux/connector.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27#include "w1.h"
28#include "w1_log.h"
29#include "w1_netlink.h"
30
Evgeniy Polyakov46e07f62006-04-03 12:01:12 +040031#if defined(CONFIG_W1_CON) && (defined(CONFIG_CONNECTOR) || (defined(CONFIG_CONNECTOR_MODULE) && defined(CONFIG_W1_MODULE)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070032void w1_netlink_send(struct w1_master *dev, struct w1_netlink_msg *msg)
33{
Evgeniy Polyakov12003372006-03-23 19:11:58 +030034 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 Torvalds1da177e2005-04-16 15:20:36 -070037
Evgeniy Polyakov12003372006-03-23 19:11:58 +030038 memset(buf, 0, sizeof(buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Evgeniy Polyakov12003372006-03-23 19:11:58 +030040 m->id.idx = CN_W1_IDX;
41 m->id.val = CN_W1_VAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Evgeniy Polyakov12003372006-03-23 19:11:58 +030043 m->seq = dev->seq++;
44 m->len = sizeof(struct w1_netlink_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Evgeniy Polyakov12003372006-03-23 19:11:58 +030046 memcpy(w, msg, sizeof(struct w1_netlink_msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
David Friesac8f7332014-01-15 22:29:19 -060048 cn_netlink_send(m, 0, 0, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049}
Evgeniy Polyakov2d833172005-07-27 13:10:11 +040050
Evgeniy Polyakov3b838402009-01-07 18:09:01 -080051static void w1_send_slave(struct w1_master *dev, u64 rn)
Evgeniy Polyakov2d833172005-07-27 13:10:11 +040052{
Evgeniy Polyakov3b838402009-01-07 18:09:01 -080053 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 Fries6b355b32014-01-15 22:29:12 -060057 u64 *data;
Evgeniy Polyakov2d833172005-07-27 13:10:11 +040058
Evgeniy Polyakov3b838402009-01-07 18:09:01 -080059 avail = dev->priv_size - cmd->len;
Evgeniy Polyakov12003372006-03-23 19:11:58 +030060
David Fries6b355b32014-01-15 22:29:12 -060061 if (avail < 8) {
62 msg->ack++;
David Friesac8f7332014-01-15 22:29:19 -060063 cn_netlink_send(msg, 0, 0, GFP_KERNEL);
Evgeniy Polyakov3b838402009-01-07 18:09:01 -080064
David Fries6b355b32014-01-15 22:29:12 -060065 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 Polyakov3b838402009-01-07 18:09:01 -080069 }
70
David Fries6b355b32014-01-15 22:29:12 -060071 data = (void *)(cmd + 1) + cmd->len;
Evgeniy Polyakov3b838402009-01-07 18:09:01 -080072
David Fries6b355b32014-01-15 22:29:12 -060073 *data = rn;
74 cmd->len += 8;
75 hdr->len += 8;
76 msg->len += 8;
Evgeniy Polyakov3b838402009-01-07 18:09:01 -080077}
78
David Fries70b34d22014-01-15 22:29:17 -060079static void w1_found_send_slave(struct w1_master *dev, u64 rn)
Evgeniy Polyakov3b838402009-01-07 18:09:01 -080080{
David Fries70b34d22014-01-15 22:29:17 -060081 /* 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) */
88static 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 Polyakov3b838402009-01-07 18:09:01 -0800116
117 dev->priv = msg;
David Fries70b34d22014-01-15 22:29:17 -0600118 dev->priv_size = PAGE_SIZE - msg->len - sizeof(struct cn_msg);
Evgeniy Polyakov3b838402009-01-07 18:09:01 -0800119
David Fries70b34d22014-01-15 22:29:17 -0600120 if (req_cmd->cmd == W1_CMD_LIST_SLAVES) {
121 __u64 rn;
David Fries9fcbbac2014-01-15 22:29:18 -0600122 mutex_lock(&dev->list_mutex);
David Fries70b34d22014-01-15 22:29:17 -0600123 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 Fries9fcbbac2014-01-15 22:29:18 -0600127 mutex_unlock(&dev->list_mutex);
David Fries70b34d22014-01-15 22:29:17 -0600128 } 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 Polyakov3b838402009-01-07 18:09:01 -0800132
133 msg->ack = 0;
David Friesac8f7332014-01-15 22:29:19 -0600134 cn_netlink_send(msg, 0, 0, GFP_KERNEL);
Evgeniy Polyakov3b838402009-01-07 18:09:01 -0800135
136 dev->priv = NULL;
137 dev->priv_size = 0;
138
David Fries70b34d22014-01-15 22:29:17 -0600139 kfree(msg);
140
Evgeniy Polyakov2d833172005-07-27 13:10:11 +0400141 return 0;
142}
143
Evgeniy Polyakovc7e26632009-01-07 18:09:03 -0800144static int w1_send_read_reply(struct cn_msg *msg, struct w1_netlink_msg *hdr,
145 struct w1_netlink_cmd *cmd)
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 Friesac8f7332014-01-15 22:29:19 -0600176 err = cn_netlink_send(cm, 0, 0, GFP_KERNEL);
Evgeniy Polyakovc7e26632009-01-07 18:09:03 -0800177
178 kfree(data);
179
180 return err;
181}
182
183static 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);
191 w1_send_read_reply(msg, hdr, cmd);
192 break;
193 case W1_CMD_READ:
194 w1_read_block(dev, cmd->data, cmd->len);
195 w1_send_read_reply(msg, hdr, cmd);
196 break;
197 case W1_CMD_WRITE:
198 w1_write_block(dev, cmd->data, cmd->len);
199 break;
200 default:
Evgeniy Polyakov40370142009-01-07 18:09:05 -0800201 err = -EINVAL;
Evgeniy Polyakovc7e26632009-01-07 18:09:03 -0800202 break;
203 }
204
205 return err;
206}
207
David Fries70b34d22014-01-15 22:29:17 -0600208static 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
243static 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 Polyakov3b838402009-01-07 18:09:01 -0800246{
247 int err = -EINVAL;
Evgeniy Polyakov3b838402009-01-07 18:09:01 -0800248
David Fries70b34d22014-01-15 22:29:17 -0600249 switch (req_cmd->cmd) {
Evgeniy Polyakovc7e26632009-01-07 18:09:03 -0800250 case W1_CMD_SEARCH:
251 case W1_CMD_ALARM_SEARCH:
David Fries70b34d22014-01-15 22:29:17 -0600252 case W1_CMD_LIST_SLAVES:
253 err = w1_get_slaves(dev, req_msg, req_hdr, req_cmd);
Evgeniy Polyakovc7e26632009-01-07 18:09:03 -0800254 break;
255 case W1_CMD_READ:
256 case W1_CMD_WRITE:
257 case W1_CMD_TOUCH:
Evgeniy Polyakov40370142009-01-07 18:09:05 -0800258 err = w1_process_command_io(dev, req_msg, req_hdr, req_cmd);
Evgeniy Polyakovc7e26632009-01-07 18:09:03 -0800259 break;
Evgeniy Polyakovf89735c2009-01-07 18:09:04 -0800260 case W1_CMD_RESET:
261 err = w1_reset_bus(dev);
262 break;
David Fries70b34d22014-01-15 22:29:17 -0600263 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 Polyakovc7e26632009-01-07 18:09:03 -0800268 default:
Evgeniy Polyakov40370142009-01-07 18:09:05 -0800269 err = -EINVAL;
Evgeniy Polyakovc7e26632009-01-07 18:09:03 -0800270 break;
Evgeniy Polyakov3b838402009-01-07 18:09:01 -0800271 }
272
Evgeniy Polyakov3b838402009-01-07 18:09:01 -0800273 return err;
274}
275
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300276static 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 Polyakov12003372006-03-23 19:11:58 +0300279 dev_dbg(&sl->master->dev, "%s: %02x.%012llx.%02x: cmd=%02x, len=%u.\n",
Evgeniy Polyakovc7e26632009-01-07 18:09:03 -0800280 __func__, sl->reg_num.family, (unsigned long long)sl->reg_num.id,
281 sl->reg_num.crc, cmd->cmd, cmd->len);
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300282
Evgeniy Polyakovc7e26632009-01-07 18:09:03 -0800283 return w1_process_command_io(sl->master, msg, hdr, cmd);
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300284}
285
Evgeniy Polyakov610705e2009-01-07 18:08:59 -0800286static int w1_process_command_root(struct cn_msg *msg, struct w1_netlink_msg *mcmd)
287{
288 struct w1_master *m;
289 struct cn_msg *cn;
290 struct w1_netlink_msg *w;
291 u32 *id;
292
293 if (mcmd->type != W1_LIST_MASTERS) {
294 printk(KERN_NOTICE "%s: msg: %x.%x, wrong type: %u, len: %u.\n",
Evgeniy Polyakovc7e26632009-01-07 18:09:03 -0800295 __func__, msg->id.idx, msg->id.val, mcmd->type, mcmd->len);
Evgeniy Polyakov610705e2009-01-07 18:08:59 -0800296 return -EPROTO;
297 }
298
299 cn = kmalloc(PAGE_SIZE, GFP_KERNEL);
300 if (!cn)
301 return -ENOMEM;
302
303 cn->id.idx = CN_W1_IDX;
304 cn->id.val = CN_W1_VAL;
305
306 cn->seq = msg->seq;
307 cn->ack = 1;
308 cn->len = sizeof(struct w1_netlink_msg);
309 w = (struct w1_netlink_msg *)(cn + 1);
310
311 w->type = W1_LIST_MASTERS;
Evgeniy Polyakov40370142009-01-07 18:09:05 -0800312 w->status = 0;
Evgeniy Polyakov610705e2009-01-07 18:08:59 -0800313 w->len = 0;
314 id = (u32 *)(w + 1);
315
316 mutex_lock(&w1_mlock);
317 list_for_each_entry(m, &w1_masters, w1_master_entry) {
318 if (cn->len + sizeof(*id) > PAGE_SIZE - sizeof(struct cn_msg)) {
David Friesac8f7332014-01-15 22:29:19 -0600319 cn_netlink_send(cn, 0, 0, GFP_KERNEL);
Evgeniy Polyakov610705e2009-01-07 18:08:59 -0800320 cn->ack++;
321 cn->len = sizeof(struct w1_netlink_msg);
322 w->len = 0;
323 id = (u32 *)(w + 1);
324 }
325
326 *id = m->id;
327 w->len += sizeof(*id);
328 cn->len += sizeof(*id);
329 id++;
330 }
331 cn->ack = 0;
David Friesac8f7332014-01-15 22:29:19 -0600332 cn_netlink_send(cn, 0, 0, GFP_KERNEL);
Evgeniy Polyakov610705e2009-01-07 18:08:59 -0800333 mutex_unlock(&w1_mlock);
334
335 kfree(cn);
336 return 0;
337}
338
Evgeniy Polyakov40370142009-01-07 18:09:05 -0800339static int w1_netlink_send_error(struct cn_msg *rcmsg, struct w1_netlink_msg *rmsg,
340 struct w1_netlink_cmd *rcmd, int error)
341{
342 struct cn_msg *cmsg;
343 struct w1_netlink_msg *msg;
344 struct w1_netlink_cmd *cmd;
345
346 cmsg = kzalloc(sizeof(*msg) + sizeof(*cmd) + sizeof(*cmsg), GFP_KERNEL);
347 if (!cmsg)
348 return -ENOMEM;
349
350 msg = (struct w1_netlink_msg *)(cmsg + 1);
351 cmd = (struct w1_netlink_cmd *)(msg + 1);
352
353 memcpy(cmsg, rcmsg, sizeof(*cmsg));
354 cmsg->len = sizeof(*msg);
355
356 memcpy(msg, rmsg, sizeof(*msg));
357 msg->len = 0;
358 msg->status = (short)-error;
359
360 if (rcmd) {
361 memcpy(cmd, rcmd, sizeof(*cmd));
362 cmd->len = 0;
363 msg->len += sizeof(*cmd);
364 cmsg->len += sizeof(*cmd);
365 }
366
David Friesac8f7332014-01-15 22:29:19 -0600367 error = cn_netlink_send(cmsg, 0, 0, GFP_KERNEL);
Evgeniy Polyakov40370142009-01-07 18:09:05 -0800368 kfree(cmsg);
369
370 return error;
371}
372
David Fries9fcbbac2014-01-15 22:29:18 -0600373/* Bundle together a reference count, the full message, and broken out
374 * commands to be executed on each w1 master kthread in one memory allocation.
375 */
376struct w1_cb_block {
377 atomic_t refcnt;
378 struct cn_msg msg;
379 /* cn_msg data */
380 /* one or more variable length struct w1_cb_node */
381};
382struct w1_cb_node {
383 struct w1_async_cmd async;
384 /* pointers within w1_cb_block and msg data */
385 struct w1_cb_block *block;
386 struct w1_netlink_msg *m;
387 struct w1_slave *sl;
388 struct w1_master *dev;
389};
390
391static void w1_process_cb(struct w1_master *dev, struct w1_async_cmd *async_cmd)
392{
393 struct w1_cb_node *node = container_of(async_cmd, struct w1_cb_node,
394 async);
395 u16 mlen = node->m->len;
396 u8 *cmd_data = node->m->data;
397 int err = 0;
398 struct w1_slave *sl = node->sl;
399 struct w1_netlink_cmd *cmd = NULL;
400
401 mutex_lock(&dev->mutex);
402 if (sl && w1_reset_select_slave(sl))
403 err = -ENODEV;
404
405 while (mlen && !err) {
406 cmd = (struct w1_netlink_cmd *)cmd_data;
407
408 if (cmd->len + sizeof(struct w1_netlink_cmd) > mlen) {
409 err = -E2BIG;
410 break;
411 }
412
413 if (sl)
414 err = w1_process_command_slave(sl, &node->block->msg,
415 node->m, cmd);
416 else
417 err = w1_process_command_master(dev, &node->block->msg,
418 node->m, cmd);
419
420 w1_netlink_send_error(&node->block->msg, node->m, cmd, err);
421 err = 0;
422
423 cmd_data += cmd->len + sizeof(struct w1_netlink_cmd);
424 mlen -= cmd->len + sizeof(struct w1_netlink_cmd);
425 }
426
427 if (!cmd || err)
428 w1_netlink_send_error(&node->block->msg, node->m, cmd, err);
429
430 if (sl)
431 w1_unref_slave(sl);
432 else
433 atomic_dec(&dev->refcnt);
434 mutex_unlock(&dev->mutex);
435
436 mutex_lock(&dev->list_mutex);
437 list_del(&async_cmd->async_entry);
438 mutex_unlock(&dev->list_mutex);
439
440 if (atomic_sub_return(1, &node->block->refcnt) == 0)
441 kfree(node->block);
442}
443
Philipp Reisner70693312009-10-02 02:40:05 +0000444static void w1_cn_callback(struct cn_msg *msg, struct netlink_skb_parms *nsp)
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300445{
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300446 struct w1_netlink_msg *m = (struct w1_netlink_msg *)(msg + 1);
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300447 struct w1_slave *sl;
448 struct w1_master *dev;
David Fries9fcbbac2014-01-15 22:29:18 -0600449 u16 msg_len;
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300450 int err = 0;
David Fries9fcbbac2014-01-15 22:29:18 -0600451 struct w1_cb_block *block = NULL;
452 struct w1_cb_node *node = NULL;
453 int node_count = 0;
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300454
David Fries9fcbbac2014-01-15 22:29:18 -0600455 /* Count the number of master or slave commands there are to allocate
456 * space for one cb_node each.
457 */
458 msg_len = msg->len;
459 while (msg_len && !err) {
460 if (m->len + sizeof(struct w1_netlink_msg) > msg_len) {
461 err = -E2BIG;
462 break;
463 }
464
465 if (m->type == W1_MASTER_CMD || m->type == W1_SLAVE_CMD)
466 ++node_count;
467
468 msg_len -= sizeof(struct w1_netlink_msg) + m->len;
469 m = (struct w1_netlink_msg *)(((u8 *)m) +
470 sizeof(struct w1_netlink_msg) + m->len);
471 }
472 m = (struct w1_netlink_msg *)(msg + 1);
473 if (node_count) {
474 /* msg->len doesn't include itself */
475 long size = sizeof(struct w1_cb_block) + msg->len +
476 node_count*sizeof(struct w1_cb_node);
477 block = kmalloc(size, GFP_KERNEL);
478 if (!block) {
479 w1_netlink_send_error(msg, m, NULL, -ENOMEM);
480 return;
481 }
482 atomic_set(&block->refcnt, 1);
483 memcpy(&block->msg, msg, sizeof(*msg) + msg->len);
484 node = (struct w1_cb_node *)((u8 *)block->msg.data + msg->len);
485 }
486
487 msg_len = msg->len;
488 while (msg_len && !err) {
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300489 struct w1_reg_num id;
490 u16 mlen = m->len;
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300491
492 dev = NULL;
493 sl = NULL;
494
495 memcpy(&id, m->id.id, sizeof(id));
496#if 0
497 printk("%s: %02x.%012llx.%02x: type=%02x, len=%u.\n",
498 __func__, id.family, (unsigned long long)id.id, id.crc, m->type, m->len);
499#endif
David Fries9fcbbac2014-01-15 22:29:18 -0600500 if (m->len + sizeof(struct w1_netlink_msg) > msg_len) {
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300501 err = -E2BIG;
502 break;
503 }
504
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300505 if (m->type == W1_MASTER_CMD) {
506 dev = w1_search_master_id(m->id.mst.id);
507 } else if (m->type == W1_SLAVE_CMD) {
508 sl = w1_search_slave(&id);
509 if (sl)
510 dev = sl->master;
Evgeniy Polyakov610705e2009-01-07 18:08:59 -0800511 } else {
512 err = w1_process_command_root(msg, m);
513 goto out_cont;
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300514 }
515
516 if (!dev) {
517 err = -ENODEV;
518 goto out_cont;
519 }
520
Evgeniy Polyakov9be62e02009-01-07 18:09:01 -0800521 err = 0;
522 if (!mlen)
523 goto out_cont;
524
David Fries9fcbbac2014-01-15 22:29:18 -0600525 atomic_inc(&block->refcnt);
526 node->async.cb = w1_process_cb;
527 node->block = block;
528 node->m = (struct w1_netlink_msg *)((u8 *)&block->msg +
529 (size_t)((u8 *)m - (u8 *)msg));
530 node->sl = sl;
531 node->dev = dev;
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300532
David Fries9fcbbac2014-01-15 22:29:18 -0600533 mutex_lock(&dev->list_mutex);
534 list_add_tail(&node->async.async_entry, &dev->async_list);
535 wake_up_process(dev->thread);
536 mutex_unlock(&dev->list_mutex);
537 ++node;
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300538
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300539out_cont:
David Fries9fcbbac2014-01-15 22:29:18 -0600540 if (err)
541 w1_netlink_send_error(msg, m, NULL, err);
542 msg_len -= sizeof(struct w1_netlink_msg) + m->len;
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300543 m = (struct w1_netlink_msg *)(((u8 *)m) + sizeof(struct w1_netlink_msg) + m->len);
544
545 /*
546 * Let's allow requests for nonexisting devices.
547 */
548 if (err == -ENODEV)
549 err = 0;
550 }
David Fries9fcbbac2014-01-15 22:29:18 -0600551 if (block && atomic_sub_return(1, &block->refcnt) == 0)
552 kfree(block);
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300553}
554
555int w1_init_netlink(void)
556{
557 struct cb_id w1_id = {.idx = CN_W1_IDX, .val = CN_W1_VAL};
558
559 return cn_add_callback(&w1_id, "w1", &w1_cn_callback);
560}
561
562void w1_fini_netlink(void)
563{
564 struct cb_id w1_id = {.idx = CN_W1_IDX, .val = CN_W1_VAL};
565
566 cn_del_callback(&w1_id);
Evgeniy Polyakov2d833172005-07-27 13:10:11 +0400567}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569void w1_netlink_send(struct w1_master *dev, struct w1_netlink_msg *msg)
570{
571}
Evgeniy Polyakov2d833172005-07-27 13:10:11 +0400572
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300573int w1_init_netlink(void)
Evgeniy Polyakov2d833172005-07-27 13:10:11 +0400574{
575 return 0;
576}
577
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300578void w1_fini_netlink(void)
Evgeniy Polyakov2d833172005-07-27 13:10:11 +0400579{
580}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581#endif