4 * Copyright (c) 2003 Evgeniy Polyakov <zbr@ioremap.net>
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.
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.
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
22 #include <linux/slab.h>
23 #include <linux/skbuff.h>
24 #include <linux/netlink.h>
25 #include <linux/connector.h>
29 #include "w1_netlink.h"
31 #if defined(CONFIG_W1_CON) && (defined(CONFIG_CONNECTOR) || (defined(CONFIG_CONNECTOR_MODULE) && defined(CONFIG_W1_MODULE)))
33 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
35 /* Bundle together everything required to process a request in one memory
40 u32 portid
; /* Sending process port ID */
41 /* maximum value for first_cn->len */
43 /* pointers to building up the reply message */
44 struct cn_msg
*first_cn
; /* fixed once the structure is populated */
45 struct cn_msg
*cn
; /* advances as cn_msg is appeneded */
46 struct w1_netlink_msg
*msg
; /* advances as w1_netlink_msg is appened */
47 struct w1_netlink_cmd
*cmd
; /* advances as cmds are appened */
48 struct w1_netlink_msg
*cur_msg
; /* currently message being processed */
49 /* copy of the original request follows */
50 struct cn_msg request_cn
;
51 /* followed by variable length:
52 * cn_msg, data (w1_netlink_msg and w1_netlink_cmd)
53 * one or more struct w1_cb_node
54 * reply first_cn, data (w1_netlink_msg and w1_netlink_cmd)
58 struct w1_async_cmd async
;
59 /* pointers within w1_cb_block and cn data */
60 struct w1_cb_block
*block
;
61 struct w1_netlink_msg
*msg
;
63 struct w1_master
*dev
;
67 * w1_reply_len() - calculate current reply length, compare to maxlen
68 * @block: block to calculate
70 * Calculates the current message length including possible multiple
71 * cn_msg and data, excludes the first sizeof(struct cn_msg). Direclty
72 * compariable to maxlen and usable to send the message.
74 static u16
w1_reply_len(struct w1_cb_block
*block
)
78 return (u8
*)block
->cn
- (u8
*)block
->first_cn
+ block
->cn
->len
;
81 static void w1_unref_block(struct w1_cb_block
*block
)
83 if (atomic_sub_return(1, &block
->refcnt
) == 0) {
84 u16 len
= w1_reply_len(block
);
86 cn_netlink_send_mult(block
->first_cn
, len
,
87 block
->portid
, 0, GFP_KERNEL
);
94 * w1_reply_make_space() - send message if needed to make space
95 * @block: block to make space on
96 * @space: how many bytes requested
98 * Verify there is enough room left for the caller to add "space" bytes to the
99 * message, if there isn't send the message and reset.
101 static void w1_reply_make_space(struct w1_cb_block
*block
, u16 space
)
103 u16 len
= w1_reply_len(block
);
104 if (len
+ space
>= block
->maxlen
) {
105 cn_netlink_send_mult(block
->first_cn
, len
, block
->portid
, 0, GFP_KERNEL
);
106 block
->first_cn
->len
= 0;
113 /* Early send when replies aren't bundled. */
114 static void w1_netlink_check_send(struct w1_cb_block
*block
)
116 if (!(block
->request_cn
.flags
& W1_CN_BUNDLE
) && block
->cn
)
117 w1_reply_make_space(block
, block
->maxlen
);
121 * w1_netlink_setup_msg() - prepare to write block->msg
122 * @block: block to operate on
123 * @ack: determines if cn can be reused
125 * block->cn will be setup with the correct ack, advancing if needed
126 * block->cn->len does not include space for block->msg
127 * block->msg advances but remains uninitialized
129 static void w1_netlink_setup_msg(struct w1_cb_block
*block
, u32 ack
)
131 if (block
->cn
&& block
->cn
->ack
== ack
) {
132 block
->msg
= (struct w1_netlink_msg
*)(block
->cn
->data
+ block
->cn
->len
);
134 /* advance or set to data */
136 block
->cn
= (struct cn_msg
*)(block
->cn
->data
+
139 block
->cn
= block
->first_cn
;
141 memcpy(block
->cn
, &block
->request_cn
, sizeof(*block
->cn
));
143 block
->cn
->ack
= ack
;
144 block
->msg
= (struct w1_netlink_msg
*)block
->cn
->data
;
148 /* Append cmd to msg, include cmd->data as well. This is because
149 * any following data goes with the command and in the case of a read is
152 static void w1_netlink_queue_cmd(struct w1_cb_block
*block
,
153 struct w1_netlink_cmd
*cmd
)
156 w1_reply_make_space(block
, sizeof(struct cn_msg
) +
157 sizeof(struct w1_netlink_msg
) + sizeof(*cmd
) + cmd
->len
);
159 /* There's a status message sent after each command, so no point
160 * in trying to bundle this cmd after an existing one, because
161 * there won't be one. Allocate and copy over a new cn_msg.
163 w1_netlink_setup_msg(block
, block
->request_cn
.seq
+ 1);
164 memcpy(block
->msg
, block
->cur_msg
, sizeof(*block
->msg
));
165 block
->cn
->len
+= sizeof(*block
->msg
);
167 block
->cmd
= (struct w1_netlink_cmd
*)(block
->msg
->data
);
169 space
= sizeof(*cmd
) + cmd
->len
;
170 if (block
->cmd
!= cmd
)
171 memcpy(block
->cmd
, cmd
, space
);
172 block
->cn
->len
+= space
;
173 block
->msg
->len
+= space
;
176 /* Append req_msg and req_cmd, no other commands and no data from req_cmd are
179 static void w1_netlink_queue_status(struct w1_cb_block
*block
,
180 struct w1_netlink_msg
*req_msg
, struct w1_netlink_cmd
*req_cmd
,
183 u16 space
= sizeof(struct cn_msg
) + sizeof(*req_msg
) + sizeof(*req_cmd
);
184 w1_reply_make_space(block
, space
);
185 w1_netlink_setup_msg(block
, block
->request_cn
.ack
);
187 memcpy(block
->msg
, req_msg
, sizeof(*req_msg
));
188 block
->cn
->len
+= sizeof(*req_msg
);
190 block
->msg
->status
= (u8
)-error
;
192 struct w1_netlink_cmd
*cmd
= (struct w1_netlink_cmd
*)block
->msg
->data
;
193 memcpy(cmd
, req_cmd
, sizeof(*cmd
));
194 block
->cn
->len
+= sizeof(*cmd
);
195 block
->msg
->len
+= sizeof(*cmd
);
198 w1_netlink_check_send(block
);
202 * w1_netlink_send_error() - sends the error message now
203 * @cn: original cn_msg
204 * @msg: original w1_netlink_msg
205 * @portid: where to send it
206 * @error: error status
208 * Use when a block isn't available to queue the message to and cn, msg
209 * might not be contiguous.
211 static void w1_netlink_send_error(struct cn_msg
*cn
, struct w1_netlink_msg
*msg
,
212 int portid
, int error
)
216 struct w1_netlink_msg msg
;
218 memcpy(&packet
.cn
, cn
, sizeof(packet
.cn
));
219 memcpy(&packet
.msg
, msg
, sizeof(packet
.msg
));
220 packet
.cn
.len
= sizeof(packet
.msg
);
222 packet
.msg
.status
= (u8
)-error
;
223 cn_netlink_send(&packet
.cn
, portid
, 0, GFP_KERNEL
);
227 * w1_netlink_send() - sends w1 netlink notifications
228 * @dev: w1_master the even is associated with or for
229 * @msg: w1_netlink_msg message to be sent
231 * This are notifications generated from the kernel.
233 void w1_netlink_send(struct w1_master
*dev
, struct w1_netlink_msg
*msg
)
237 struct w1_netlink_msg msg
;
239 memset(&packet
, 0, sizeof(packet
));
241 packet
.cn
.id
.idx
= CN_W1_IDX
;
242 packet
.cn
.id
.val
= CN_W1_VAL
;
244 packet
.cn
.seq
= dev
->seq
++;
245 packet
.cn
.len
= sizeof(*msg
);
247 memcpy(&packet
.msg
, msg
, sizeof(*msg
));
250 cn_netlink_send(&packet
.cn
, 0, 0, GFP_KERNEL
);
253 static void w1_send_slave(struct w1_master
*dev
, u64 rn
)
255 struct w1_cb_block
*block
= dev
->priv
;
256 struct w1_netlink_cmd
*cache_cmd
= block
->cmd
;
259 w1_reply_make_space(block
, sizeof(*data
));
261 /* Add cmd back if the packet was sent */
264 w1_netlink_queue_cmd(block
, cache_cmd
);
267 data
= (u64
*)(block
->cmd
->data
+ block
->cmd
->len
);
270 block
->cn
->len
+= sizeof(*data
);
271 block
->msg
->len
+= sizeof(*data
);
272 block
->cmd
->len
+= sizeof(*data
);
275 static void w1_found_send_slave(struct w1_master
*dev
, u64 rn
)
277 /* update kernel slave list */
278 w1_slave_found(dev
, rn
);
280 w1_send_slave(dev
, rn
);
283 /* Get the current slave list, or search (with or without alarm) */
284 static int w1_get_slaves(struct w1_master
*dev
, struct w1_netlink_cmd
*req_cmd
)
289 w1_netlink_queue_cmd(dev
->priv
, req_cmd
);
291 if (req_cmd
->cmd
== W1_CMD_LIST_SLAVES
) {
293 mutex_lock(&dev
->list_mutex
);
294 list_for_each_entry(sl
, &dev
->slist
, w1_slave_entry
) {
295 memcpy(&rn
, &sl
->reg_num
, sizeof(rn
));
296 w1_send_slave(dev
, rn
);
298 mutex_unlock(&dev
->list_mutex
);
300 w1_search_process_cb(dev
, req_cmd
->cmd
== W1_CMD_ALARM_SEARCH
?
301 W1_ALARM_SEARCH
: W1_SEARCH
, w1_found_send_slave
);
307 static int w1_process_command_io(struct w1_master
*dev
,
308 struct w1_netlink_cmd
*cmd
)
314 w1_touch_block(dev
, cmd
->data
, cmd
->len
);
315 w1_netlink_queue_cmd(dev
->priv
, cmd
);
318 w1_read_block(dev
, cmd
->data
, cmd
->len
);
319 w1_netlink_queue_cmd(dev
->priv
, cmd
);
322 w1_write_block(dev
, cmd
->data
, cmd
->len
);
332 static int w1_process_command_addremove(struct w1_master
*dev
,
333 struct w1_netlink_cmd
*cmd
)
337 struct w1_reg_num
*id
;
339 if (cmd
->len
!= sizeof(*id
))
342 id
= (struct w1_reg_num
*)cmd
->data
;
344 sl
= w1_slave_search_device(dev
, id
);
346 case W1_CMD_SLAVE_ADD
:
350 err
= w1_attach_slave_device(dev
, id
);
352 case W1_CMD_SLAVE_REMOVE
:
366 static int w1_process_command_master(struct w1_master
*dev
,
367 struct w1_netlink_cmd
*req_cmd
)
371 /* drop bus_mutex for search (does it's own locking), and add/remove
372 * which doesn't use the bus
374 switch (req_cmd
->cmd
) {
376 case W1_CMD_ALARM_SEARCH
:
377 case W1_CMD_LIST_SLAVES
:
378 mutex_unlock(&dev
->bus_mutex
);
379 err
= w1_get_slaves(dev
, req_cmd
);
380 mutex_lock(&dev
->bus_mutex
);
385 err
= w1_process_command_io(dev
, req_cmd
);
388 err
= w1_reset_bus(dev
);
390 case W1_CMD_SLAVE_ADD
:
391 case W1_CMD_SLAVE_REMOVE
:
392 mutex_unlock(&dev
->bus_mutex
);
393 mutex_lock(&dev
->mutex
);
394 err
= w1_process_command_addremove(dev
, req_cmd
);
395 mutex_unlock(&dev
->mutex
);
396 mutex_lock(&dev
->bus_mutex
);
406 static int w1_process_command_slave(struct w1_slave
*sl
,
407 struct w1_netlink_cmd
*cmd
)
409 dev_dbg(&sl
->master
->dev
, "%s: %02x.%012llx.%02x: cmd=%02x, len=%u.\n",
410 __func__
, sl
->reg_num
.family
, (unsigned long long)sl
->reg_num
.id
,
411 sl
->reg_num
.crc
, cmd
->cmd
, cmd
->len
);
413 return w1_process_command_io(sl
->master
, cmd
);
416 static int w1_process_command_root(struct cn_msg
*req_cn
, u32 portid
)
418 struct w1_master
*dev
;
420 struct w1_netlink_msg
*msg
;
423 cn
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
427 cn
->id
.idx
= CN_W1_IDX
;
428 cn
->id
.val
= CN_W1_VAL
;
430 cn
->seq
= req_cn
->seq
;
431 cn
->ack
= req_cn
->seq
+ 1;
432 cn
->len
= sizeof(struct w1_netlink_msg
);
433 msg
= (struct w1_netlink_msg
*)cn
->data
;
435 msg
->type
= W1_LIST_MASTERS
;
438 id
= (u32
*)msg
->data
;
440 mutex_lock(&w1_mlock
);
441 list_for_each_entry(dev
, &w1_masters
, w1_master_entry
) {
442 if (cn
->len
+ sizeof(*id
) > PAGE_SIZE
- sizeof(struct cn_msg
)) {
443 cn_netlink_send(cn
, portid
, 0, GFP_KERNEL
);
444 cn
->len
= sizeof(struct w1_netlink_msg
);
446 id
= (u32
*)msg
->data
;
450 msg
->len
+= sizeof(*id
);
451 cn
->len
+= sizeof(*id
);
454 cn_netlink_send(cn
, portid
, 0, GFP_KERNEL
);
455 mutex_unlock(&w1_mlock
);
461 static void w1_process_cb(struct w1_master
*dev
, struct w1_async_cmd
*async_cmd
)
463 struct w1_cb_node
*node
= container_of(async_cmd
, struct w1_cb_node
,
465 u16 mlen
= node
->msg
->len
;
468 struct w1_slave
*sl
= node
->sl
;
469 struct w1_netlink_cmd
*cmd
= (struct w1_netlink_cmd
*)node
->msg
->data
;
471 mutex_lock(&dev
->bus_mutex
);
472 dev
->priv
= node
->block
;
473 if (sl
&& w1_reset_select_slave(sl
))
475 node
->block
->cur_msg
= node
->msg
;
477 while (mlen
&& !err
) {
478 if (cmd
->len
+ sizeof(struct w1_netlink_cmd
) > mlen
) {
484 err
= w1_process_command_slave(sl
, cmd
);
486 err
= w1_process_command_master(dev
, cmd
);
487 w1_netlink_check_send(node
->block
);
489 w1_netlink_queue_status(node
->block
, node
->msg
, cmd
, err
);
492 len
= sizeof(*cmd
) + cmd
->len
;
493 cmd
= (struct w1_netlink_cmd
*)((u8
*)cmd
+ len
);
498 w1_netlink_queue_status(node
->block
, node
->msg
, cmd
, err
);
500 /* ref taken in w1_search_slave or w1_search_master_id when building
506 atomic_dec(&dev
->refcnt
);
508 mutex_unlock(&dev
->bus_mutex
);
510 mutex_lock(&dev
->list_mutex
);
511 list_del(&async_cmd
->async_entry
);
512 mutex_unlock(&dev
->list_mutex
);
514 w1_unref_block(node
->block
);
517 static void w1_list_count_cmds(struct w1_netlink_msg
*msg
, int *cmd_count
,
520 struct w1_netlink_cmd
*cmd
= (struct w1_netlink_cmd
*)msg
->data
;
525 if (cmd
->len
+ sizeof(struct w1_netlink_cmd
) > mlen
)
530 case W1_CMD_ALARM_SEARCH
:
531 case W1_CMD_LIST_SLAVES
:
535 len
= sizeof(*cmd
) + cmd
->len
;
536 cmd
= (struct w1_netlink_cmd
*)((u8
*)cmd
+ len
);
541 struct w1_master
*dev
= w1_search_master_id(msg
->id
.mst
.id
);
543 /* Bytes, and likely an overstimate, and if it isn't
544 * the results can still be split between packets.
546 *slave_len
+= sizeof(struct w1_reg_num
) * slave_list
*
547 (dev
->slave_count
+ dev
->max_slave_count
);
548 /* search incremented it */
549 atomic_dec(&dev
->refcnt
);
554 static void w1_cn_callback(struct cn_msg
*cn
, struct netlink_skb_parms
*nsp
)
556 struct w1_netlink_msg
*msg
= (struct w1_netlink_msg
*)(cn
+ 1);
558 struct w1_master
*dev
;
562 struct w1_cb_block
*block
= NULL
;
563 struct w1_cb_node
*node
= NULL
;
567 /* If any unknown flag is set let the application know, that way
568 * applications can detect the absence of features in kernels that
569 * don't know about them. http://lwn.net/Articles/587527/
571 if (cn
->flags
& ~(W1_CN_BUNDLE
)) {
572 w1_netlink_send_error(cn
, msg
, nsp
->portid
, -EINVAL
);
576 /* Count the number of master or slave commands there are to allocate
577 * space for one cb_node each.
580 while (msg_len
&& !err
) {
581 if (msg
->len
+ sizeof(struct w1_netlink_msg
) > msg_len
) {
586 /* count messages for nodes and allocate any additional space
587 * required for slave lists
589 if (msg
->type
== W1_MASTER_CMD
|| msg
->type
== W1_SLAVE_CMD
) {
591 w1_list_count_cmds(msg
, &cmd_count
, &slave_len
);
594 msg_len
-= sizeof(struct w1_netlink_msg
) + msg
->len
;
595 msg
= (struct w1_netlink_msg
*)(((u8
*)msg
) +
596 sizeof(struct w1_netlink_msg
) + msg
->len
);
598 msg
= (struct w1_netlink_msg
*)(cn
+ 1);
601 int reply_size
= sizeof(*cn
) + cn
->len
+ slave_len
;
602 if (cn
->flags
& W1_CN_BUNDLE
) {
603 /* bundling duplicats some of the messages */
604 reply_size
+= 2 * cmd_count
* (sizeof(struct cn_msg
) +
605 sizeof(struct w1_netlink_msg
) +
606 sizeof(struct w1_netlink_cmd
));
608 reply_size
= MIN(CONNECTOR_MAX_MSG_SIZE
, reply_size
);
610 /* allocate space for the block, a copy of the original message,
611 * one node per cmd to point into the original message,
612 * space for replies which is the original message size plus
613 * space for any list slave data and status messages
614 * cn->len doesn't include itself which is part of the block
616 size
= /* block + original message */
617 sizeof(struct w1_cb_block
) + sizeof(*cn
) + cn
->len
+
618 /* space for nodes */
619 node_count
* sizeof(struct w1_cb_node
) +
621 sizeof(struct cn_msg
) + reply_size
;
622 block
= kzalloc(size
, GFP_KERNEL
);
624 /* if the system is already out of memory,
625 * (A) will this work, and (B) would it be better
628 w1_netlink_send_error(cn
, msg
, nsp
->portid
, -ENOMEM
);
631 atomic_set(&block
->refcnt
, 1);
632 block
->portid
= nsp
->portid
;
633 memcpy(&block
->request_cn
, cn
, sizeof(*cn
) + cn
->len
);
634 node
= (struct w1_cb_node
*)(block
->request_cn
.data
+ cn
->len
);
636 /* Sneeky, when not bundling, reply_size is the allocated space
637 * required for the reply, cn_msg isn't part of maxlen so
638 * it should be reply_size - sizeof(struct cn_msg), however
639 * when checking if there is enough space, w1_reply_make_space
640 * is called with the full message size including cn_msg,
641 * because it isn't known at that time if an additional cn_msg
642 * will need to be allocated. So an extra cn_msg is added
645 block
->maxlen
= reply_size
;
646 block
->first_cn
= (struct cn_msg
*)(node
+ node_count
);
647 memset(block
->first_cn
, 0, sizeof(*block
->first_cn
));
651 while (msg_len
&& !err
) {
656 if (msg
->len
+ sizeof(struct w1_netlink_msg
) > msg_len
) {
661 /* execute on this thread, no need to process later */
662 if (msg
->type
== W1_LIST_MASTERS
) {
663 err
= w1_process_command_root(cn
, nsp
->portid
);
667 /* All following message types require additional data,
668 * check here before references are taken.
675 /* both search calls take references */
676 if (msg
->type
== W1_MASTER_CMD
) {
677 dev
= w1_search_master_id(msg
->id
.mst
.id
);
678 } else if (msg
->type
== W1_SLAVE_CMD
) {
679 sl
= w1_search_slave((struct w1_reg_num
*)msg
->id
.id
);
683 pr_notice("%s: cn: %x.%x, wrong type: %u, len: %u.\n",
684 __func__
, cn
->id
.idx
, cn
->id
.val
,
685 msg
->type
, msg
->len
);
697 atomic_inc(&block
->refcnt
);
698 node
->async
.cb
= w1_process_cb
;
700 node
->msg
= (struct w1_netlink_msg
*)((u8
*)&block
->request_cn
+
701 (size_t)((u8
*)msg
- (u8
*)cn
));
705 mutex_lock(&dev
->list_mutex
);
706 list_add_tail(&node
->async
.async_entry
, &dev
->async_list
);
707 wake_up_process(dev
->thread
);
708 mutex_unlock(&dev
->list_mutex
);
712 /* Can't queue because that modifies block and another
713 * thread could be processing the messages by now and
714 * there isn't a lock, send directly.
717 w1_netlink_send_error(cn
, msg
, nsp
->portid
, err
);
718 msg_len
-= sizeof(struct w1_netlink_msg
) + msg
->len
;
719 msg
= (struct w1_netlink_msg
*)(((u8
*)msg
) +
720 sizeof(struct w1_netlink_msg
) + msg
->len
);
723 * Let's allow requests for nonexisting devices.
729 w1_unref_block(block
);
732 int w1_init_netlink(void)
734 struct cb_id w1_id
= {.idx
= CN_W1_IDX
, .val
= CN_W1_VAL
};
736 return cn_add_callback(&w1_id
, "w1", &w1_cn_callback
);
739 void w1_fini_netlink(void)
741 struct cb_id w1_id
= {.idx
= CN_W1_IDX
, .val
= CN_W1_VAL
};
743 cn_del_callback(&w1_id
);
746 void w1_netlink_send(struct w1_master
*dev
, struct w1_netlink_msg
*cn
)
750 int w1_init_netlink(void)
755 void w1_fini_netlink(void)