2005-04-17 02:20:36 +04:00
/*
* w1_netlink . c
*
2011-08-26 02:59:06 +04:00
* Copyright ( c ) 2003 Evgeniy Polyakov < zbr @ ioremap . net >
2005-04-17 02:20:36 +04:00
*
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation ; either version 2 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program ; if not , write to the Free Software
* Foundation , Inc . , 59 Temple Place , Suite 330 , Boston , MA 02111 - 1307 USA
*/
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 11:04:11 +03:00
# include <linux/slab.h>
2005-04-17 02:20:36 +04:00
# include <linux/skbuff.h>
# include <linux/netlink.h>
2006-03-23 19:11:58 +03:00
# include <linux/connector.h>
2005-04-17 02:20:36 +04:00
# include "w1.h"
# include "w1_log.h"
# include "w1_netlink.h"
2006-04-03 12:01:12 +04:00
# if defined(CONFIG_W1_CON) && (defined(CONFIG_CONNECTOR) || (defined(CONFIG_CONNECTOR_MODULE) && defined(CONFIG_W1_MODULE)))
2014-04-09 07:37:09 +04:00
# define MIN(a, b) (((a) < (b)) ? (a) : (b))
/* Bundle together everything required to process a request in one memory
* allocation .
*/
struct w1_cb_block {
atomic_t refcnt ;
u32 portid ; /* Sending process port ID */
/* maximum value for first_cn->len */
u16 maxlen ;
/* pointers to building up the reply message */
struct cn_msg * first_cn ; /* fixed once the structure is populated */
struct cn_msg * cn ; /* advances as cn_msg is appeneded */
struct w1_netlink_msg * msg ; /* advances as w1_netlink_msg is appened */
struct w1_netlink_cmd * cmd ; /* advances as cmds are appened */
struct w1_netlink_msg * cur_msg ; /* currently message being processed */
/* copy of the original request follows */
struct cn_msg request_cn ;
/* followed by variable length:
* cn_msg , data ( w1_netlink_msg and w1_netlink_cmd )
* one or more struct w1_cb_node
* reply first_cn , data ( w1_netlink_msg and w1_netlink_cmd )
*/
} ;
struct w1_cb_node {
struct w1_async_cmd async ;
/* pointers within w1_cb_block and cn data */
struct w1_cb_block * block ;
struct w1_netlink_msg * msg ;
struct w1_slave * sl ;
struct w1_master * dev ;
} ;
/**
* w1_reply_len ( ) - calculate current reply length , compare to maxlen
* @ block : block to calculate
*
* Calculates the current message length including possible multiple
* cn_msg and data , excludes the first sizeof ( struct cn_msg ) . Direclty
* compariable to maxlen and usable to send the message .
*/
static u16 w1_reply_len ( struct w1_cb_block * block )
{
if ( ! block - > cn )
return 0 ;
return ( u8 * ) block - > cn - ( u8 * ) block - > first_cn + block - > cn - > len ;
}
static void w1_unref_block ( struct w1_cb_block * block )
{
if ( atomic_sub_return ( 1 , & block - > refcnt ) = = 0 ) {
u16 len = w1_reply_len ( block ) ;
if ( len ) {
cn_netlink_send_mult ( block - > first_cn , len ,
block - > portid , 0 , GFP_KERNEL ) ;
}
kfree ( block ) ;
}
}
/**
* w1_reply_make_space ( ) - send message if needed to make space
* @ block : block to make space on
* @ space : how many bytes requested
*
* Verify there is enough room left for the caller to add " space " bytes to the
* message , if there isn ' t send the message and reset .
*/
static void w1_reply_make_space ( struct w1_cb_block * block , u16 space )
{
u16 len = w1_reply_len ( block ) ;
if ( len + space > = block - > maxlen ) {
cn_netlink_send_mult ( block - > first_cn , len , block - > portid , 0 , GFP_KERNEL ) ;
block - > first_cn - > len = 0 ;
block - > cn = NULL ;
block - > msg = NULL ;
block - > cmd = NULL ;
}
}
/* Early send when replies aren't bundled. */
static void w1_netlink_check_send ( struct w1_cb_block * block )
{
if ( ! ( block - > request_cn . flags & W1_CN_BUNDLE ) & & block - > cn )
w1_reply_make_space ( block , block - > maxlen ) ;
}
/**
* w1_netlink_setup_msg ( ) - prepare to write block - > msg
* @ block : block to operate on
* @ ack : determines if cn can be reused
*
* block - > cn will be setup with the correct ack , advancing if needed
* block - > cn - > len does not include space for block - > msg
* block - > msg advances but remains uninitialized
*/
static void w1_netlink_setup_msg ( struct w1_cb_block * block , u32 ack )
{
if ( block - > cn & & block - > cn - > ack = = ack ) {
block - > msg = ( struct w1_netlink_msg * ) ( block - > cn - > data + block - > cn - > len ) ;
} else {
/* advance or set to data */
if ( block - > cn )
block - > cn = ( struct cn_msg * ) ( block - > cn - > data +
block - > cn - > len ) ;
else
block - > cn = block - > first_cn ;
memcpy ( block - > cn , & block - > request_cn , sizeof ( * block - > cn ) ) ;
block - > cn - > len = 0 ;
block - > cn - > ack = ack ;
block - > msg = ( struct w1_netlink_msg * ) block - > cn - > data ;
}
}
/* Append cmd to msg, include cmd->data as well. This is because
* any following data goes with the command and in the case of a read is
* the results .
*/
static void w1_netlink_queue_cmd ( struct w1_cb_block * block ,
struct w1_netlink_cmd * cmd )
{
u32 space ;
w1_reply_make_space ( block , sizeof ( struct cn_msg ) +
sizeof ( struct w1_netlink_msg ) + sizeof ( * cmd ) + cmd - > len ) ;
/* There's a status message sent after each command, so no point
* in trying to bundle this cmd after an existing one , because
* there won ' t be one . Allocate and copy over a new cn_msg .
*/
w1_netlink_setup_msg ( block , block - > request_cn . seq + 1 ) ;
memcpy ( block - > msg , block - > cur_msg , sizeof ( * block - > msg ) ) ;
block - > cn - > len + = sizeof ( * block - > msg ) ;
block - > msg - > len = 0 ;
block - > cmd = ( struct w1_netlink_cmd * ) ( block - > msg - > data ) ;
space = sizeof ( * cmd ) + cmd - > len ;
if ( block - > cmd ! = cmd )
memcpy ( block - > cmd , cmd , space ) ;
block - > cn - > len + = space ;
block - > msg - > len + = space ;
}
/* Append req_msg and req_cmd, no other commands and no data from req_cmd are
* copied .
*/
static void w1_netlink_queue_status ( struct w1_cb_block * block ,
struct w1_netlink_msg * req_msg , struct w1_netlink_cmd * req_cmd ,
int error )
2005-04-17 02:20:36 +04:00
{
2014-04-09 07:37:09 +04:00
u16 space = sizeof ( struct cn_msg ) + sizeof ( * req_msg ) + sizeof ( * req_cmd ) ;
w1_reply_make_space ( block , space ) ;
w1_netlink_setup_msg ( block , block - > request_cn . ack ) ;
memcpy ( block - > msg , req_msg , sizeof ( * req_msg ) ) ;
block - > cn - > len + = sizeof ( * req_msg ) ;
block - > msg - > len = 0 ;
block - > msg - > status = ( u8 ) - error ;
if ( req_cmd ) {
struct w1_netlink_cmd * cmd = ( struct w1_netlink_cmd * ) block - > msg - > data ;
memcpy ( cmd , req_cmd , sizeof ( * cmd ) ) ;
block - > cn - > len + = sizeof ( * cmd ) ;
block - > msg - > len + = sizeof ( * cmd ) ;
cmd - > len = 0 ;
}
w1_netlink_check_send ( block ) ;
}
2005-04-17 02:20:36 +04:00
2014-04-09 07:37:09 +04:00
/**
* w1_netlink_send_error ( ) - sends the error message now
* @ cn : original cn_msg
* @ msg : original w1_netlink_msg
* @ portid : where to send it
* @ error : error status
*
* Use when a block isn ' t available to queue the message to and cn , msg
* might not be contiguous .
*/
static void w1_netlink_send_error ( struct cn_msg * cn , struct w1_netlink_msg * msg ,
int portid , int error )
{
struct {
struct cn_msg cn ;
struct w1_netlink_msg msg ;
} packet ;
memcpy ( & packet . cn , cn , sizeof ( packet . cn ) ) ;
memcpy ( & packet . msg , msg , sizeof ( packet . msg ) ) ;
packet . cn . len = sizeof ( packet . msg ) ;
packet . msg . len = 0 ;
packet . msg . status = ( u8 ) - error ;
cn_netlink_send ( & packet . cn , portid , 0 , GFP_KERNEL ) ;
}
/**
* w1_netlink_send ( ) - sends w1 netlink notifications
* @ dev : w1_master the even is associated with or for
* @ msg : w1_netlink_msg message to be sent
*
* This are notifications generated from the kernel .
*/
void w1_netlink_send ( struct w1_master * dev , struct w1_netlink_msg * msg )
{
struct {
struct cn_msg cn ;
struct w1_netlink_msg msg ;
} packet ;
memset ( & packet , 0 , sizeof ( packet ) ) ;
2005-04-17 02:20:36 +04:00
2014-04-09 07:37:09 +04:00
packet . cn . id . idx = CN_W1_IDX ;
packet . cn . id . val = CN_W1_VAL ;
2005-04-17 02:20:36 +04:00
2014-04-09 07:37:09 +04:00
packet . cn . seq = dev - > seq + + ;
packet . cn . len = sizeof ( * msg ) ;
2005-04-17 02:20:36 +04:00
2014-04-09 07:37:09 +04:00
memcpy ( & packet . msg , msg , sizeof ( * msg ) ) ;
packet . msg . len = 0 ;
2005-04-17 02:20:36 +04:00
2014-04-09 07:37:09 +04:00
cn_netlink_send ( & packet . cn , 0 , 0 , GFP_KERNEL ) ;
2006-03-23 19:11:58 +03:00
}
2005-04-17 02:20:36 +04:00
2009-01-08 05:09:01 +03:00
static void w1_send_slave ( struct w1_master * dev , u64 rn )
{
2014-04-09 07:37:09 +04:00
struct w1_cb_block * block = dev - > priv ;
struct w1_netlink_cmd * cache_cmd = block - > cmd ;
2014-01-16 08:29:12 +04:00
u64 * data ;
2009-01-08 05:09:01 +03:00
2014-04-09 07:37:09 +04:00
w1_reply_make_space ( block , sizeof ( * data ) ) ;
2009-01-08 05:09:01 +03:00
2014-04-09 07:37:09 +04:00
/* Add cmd back if the packet was sent */
if ( ! block - > cmd ) {
cache_cmd - > len = 0 ;
w1_netlink_queue_cmd ( block , cache_cmd ) ;
2009-01-08 05:09:01 +03:00
}
2014-04-09 07:37:09 +04:00
data = ( u64 * ) ( block - > cmd - > data + block - > cmd - > len ) ;
2009-01-08 05:09:01 +03:00
2014-01-16 08:29:12 +04:00
* data = rn ;
2014-04-09 07:37:09 +04:00
block - > cn - > len + = sizeof ( * data ) ;
block - > msg - > len + = sizeof ( * data ) ;
block - > cmd - > len + = sizeof ( * data ) ;
2009-01-08 05:09:01 +03:00
}
2014-01-16 08:29:17 +04:00
static void w1_found_send_slave ( struct w1_master * dev , u64 rn )
2006-03-23 19:11:58 +03:00
{
2014-01-16 08:29:17 +04:00
/* update kernel slave list */
w1_slave_found ( dev , rn ) ;
2005-04-17 02:20:36 +04:00
2014-01-16 08:29:17 +04:00
w1_send_slave ( dev , rn ) ;
}
/* Get the current slave list, or search (with or without alarm) */
2014-04-09 07:37:09 +04:00
static int w1_get_slaves ( struct w1_master * dev , struct w1_netlink_cmd * req_cmd )
2014-01-16 08:29:17 +04:00
{
struct w1_slave * sl ;
2014-04-09 07:37:09 +04:00
req_cmd - > len = 0 ;
w1_netlink_queue_cmd ( dev - > priv , req_cmd ) ;
2014-01-16 08:29:17 +04:00
if ( req_cmd - > cmd = = W1_CMD_LIST_SLAVES ) {
2014-04-09 07:37:09 +04:00
u64 rn ;
2014-01-16 08:29:18 +04:00
mutex_lock ( & dev - > list_mutex ) ;
2014-01-16 08:29:17 +04:00
list_for_each_entry ( sl , & dev - > slist , w1_slave_entry ) {
memcpy ( & rn , & sl - > reg_num , sizeof ( rn ) ) ;
w1_send_slave ( dev , rn ) ;
}
2014-01-16 08:29:18 +04:00
mutex_unlock ( & dev - > list_mutex ) ;
2014-01-16 08:29:17 +04:00
} else {
2014-04-09 07:37:09 +04:00
w1_search_process_cb ( dev , req_cmd - > cmd = = W1_CMD_ALARM_SEARCH ?
2014-01-16 08:29:17 +04:00
W1_ALARM_SEARCH : W1_SEARCH , w1_found_send_slave ) ;
}
2009-01-08 05:09:01 +03:00
2006-03-23 19:11:58 +03:00
return 0 ;
2005-04-17 02:20:36 +04:00
}
2005-07-27 13:10:11 +04:00
2014-04-09 07:37:09 +04:00
static int w1_process_command_io ( struct w1_master * dev ,
struct w1_netlink_cmd * cmd )
2006-03-23 19:11:58 +03:00
{
int err = 0 ;
2009-01-08 05:09:03 +03:00
switch ( cmd - > cmd ) {
case W1_CMD_TOUCH :
w1_touch_block ( dev , cmd - > data , cmd - > len ) ;
2014-04-09 07:37:09 +04:00
w1_netlink_queue_cmd ( dev - > priv , cmd ) ;
2009-01-08 05:09:03 +03:00
break ;
case W1_CMD_READ :
w1_read_block ( dev , cmd - > data , cmd - > len ) ;
2014-04-09 07:37:09 +04:00
w1_netlink_queue_cmd ( dev - > priv , cmd ) ;
2009-01-08 05:09:03 +03:00
break ;
case W1_CMD_WRITE :
w1_write_block ( dev , cmd - > data , cmd - > len ) ;
break ;
default :
2009-01-08 05:09:05 +03:00
err = - EINVAL ;
2009-01-08 05:09:03 +03:00
break ;
}
return err ;
}
2014-01-16 08:29:17 +04:00
static int w1_process_command_addremove ( struct w1_master * dev ,
struct w1_netlink_cmd * cmd )
2009-01-08 05:09:03 +03:00
{
2014-01-16 08:29:17 +04:00
struct w1_slave * sl ;
int err = 0 ;
struct w1_reg_num * id ;
2009-01-08 05:09:03 +03:00
2014-04-09 07:37:09 +04:00
if ( cmd - > len ! = sizeof ( * id ) )
2014-01-16 08:29:17 +04:00
return - EINVAL ;
2009-01-08 05:09:03 +03:00
2014-01-16 08:29:17 +04:00
id = ( struct w1_reg_num * ) cmd - > data ;
2009-01-08 05:09:03 +03:00
2014-01-16 08:29:17 +04:00
sl = w1_slave_search_device ( dev , id ) ;
switch ( cmd - > cmd ) {
case W1_CMD_SLAVE_ADD :
if ( sl )
err = - EINVAL ;
else
err = w1_attach_slave_device ( dev , id ) ;
break ;
case W1_CMD_SLAVE_REMOVE :
if ( sl )
w1_slave_detach ( sl ) ;
else
err = - EINVAL ;
break ;
default :
err = - EINVAL ;
break ;
}
2009-01-08 05:09:03 +03:00
2014-01-16 08:29:17 +04:00
return err ;
}
2009-01-08 05:09:03 +03:00
2014-01-16 08:29:17 +04:00
static int w1_process_command_master ( struct w1_master * dev ,
struct w1_netlink_cmd * req_cmd )
{
int err = - EINVAL ;
2006-03-23 19:11:58 +03:00
2014-01-16 08:29:26 +04:00
/* drop bus_mutex for search (does it's own locking), and add/remove
* which doesn ' t use the bus
*/
2014-01-16 08:29:17 +04:00
switch ( req_cmd - > cmd ) {
2009-01-08 05:09:03 +03:00
case W1_CMD_SEARCH :
case W1_CMD_ALARM_SEARCH :
2014-01-16 08:29:17 +04:00
case W1_CMD_LIST_SLAVES :
2014-01-16 08:29:26 +04:00
mutex_unlock ( & dev - > bus_mutex ) ;
2014-04-09 07:37:09 +04:00
err = w1_get_slaves ( dev , req_cmd ) ;
2014-01-16 08:29:26 +04:00
mutex_lock ( & dev - > bus_mutex ) ;
2009-01-08 05:09:03 +03:00
break ;
case W1_CMD_READ :
case W1_CMD_WRITE :
case W1_CMD_TOUCH :
2014-04-09 07:37:09 +04:00
err = w1_process_command_io ( dev , req_cmd ) ;
2009-01-08 05:09:03 +03:00
break ;
2009-01-08 05:09:04 +03:00
case W1_CMD_RESET :
err = w1_reset_bus ( dev ) ;
break ;
2014-01-16 08:29:17 +04:00
case W1_CMD_SLAVE_ADD :
case W1_CMD_SLAVE_REMOVE :
2014-01-16 08:29:26 +04:00
mutex_unlock ( & dev - > bus_mutex ) ;
mutex_lock ( & dev - > mutex ) ;
2014-04-09 07:37:09 +04:00
err = w1_process_command_addremove ( dev , req_cmd ) ;
2014-01-16 08:29:26 +04:00
mutex_unlock ( & dev - > mutex ) ;
mutex_lock ( & dev - > bus_mutex ) ;
2014-01-16 08:29:17 +04:00
break ;
2009-01-08 05:09:03 +03:00
default :
2009-01-08 05:09:05 +03:00
err = - EINVAL ;
2009-01-08 05:09:03 +03:00
break ;
2005-07-27 13:10:11 +04:00
}
2006-03-23 19:11:58 +03:00
return err ;
}
2014-04-09 07:37:09 +04:00
static int w1_process_command_slave ( struct w1_slave * sl ,
struct w1_netlink_cmd * cmd )
2009-01-08 05:09:03 +03:00
{
dev_dbg ( & sl - > master - > dev , " %s: %02x.%012llx.%02x: cmd=%02x, len=%u. \n " ,
__func__ , sl - > reg_num . family , ( unsigned long long ) sl - > reg_num . id ,
sl - > reg_num . crc , cmd - > cmd , cmd - > len ) ;
2014-04-09 07:37:09 +04:00
return w1_process_command_io ( sl - > master , cmd ) ;
2009-01-08 05:09:03 +03:00
}
2014-04-09 07:37:09 +04:00
static int w1_process_command_root ( struct cn_msg * req_cn , u32 portid )
2009-01-08 05:08:59 +03:00
{
2014-04-09 07:37:09 +04:00
struct w1_master * dev ;
2009-01-08 05:08:59 +03:00
struct cn_msg * cn ;
2014-04-09 07:37:09 +04:00
struct w1_netlink_msg * msg ;
2009-01-08 05:08:59 +03:00
u32 * id ;
cn = kmalloc ( PAGE_SIZE , GFP_KERNEL ) ;
if ( ! cn )
return - ENOMEM ;
cn - > id . idx = CN_W1_IDX ;
cn - > id . val = CN_W1_VAL ;
2014-04-09 07:37:09 +04:00
cn - > seq = req_cn - > seq ;
cn - > ack = req_cn - > seq + 1 ;
2009-01-08 05:08:59 +03:00
cn - > len = sizeof ( struct w1_netlink_msg ) ;
2014-04-09 07:37:09 +04:00
msg = ( struct w1_netlink_msg * ) cn - > data ;
2009-01-08 05:08:59 +03:00
2014-04-09 07:37:09 +04:00
msg - > type = W1_LIST_MASTERS ;
msg - > status = 0 ;
msg - > len = 0 ;
id = ( u32 * ) msg - > data ;
2009-01-08 05:08:59 +03:00
mutex_lock ( & w1_mlock ) ;
2014-04-09 07:37:09 +04:00
list_for_each_entry ( dev , & w1_masters , w1_master_entry ) {
2009-01-08 05:08:59 +03:00
if ( cn - > len + sizeof ( * id ) > PAGE_SIZE - sizeof ( struct cn_msg ) ) {
2014-01-16 08:29:20 +04:00
cn_netlink_send ( cn , portid , 0 , GFP_KERNEL ) ;
2009-01-08 05:08:59 +03:00
cn - > len = sizeof ( struct w1_netlink_msg ) ;
2014-04-09 07:37:09 +04:00
msg - > len = 0 ;
id = ( u32 * ) msg - > data ;
2009-01-08 05:08:59 +03:00
}
2014-04-09 07:37:09 +04:00
* id = dev - > id ;
msg - > len + = sizeof ( * id ) ;
2009-01-08 05:08:59 +03:00
cn - > len + = sizeof ( * id ) ;
id + + ;
}
2014-01-16 08:29:20 +04:00
cn_netlink_send ( cn , portid , 0 , GFP_KERNEL ) ;
2009-01-08 05:08:59 +03:00
mutex_unlock ( & w1_mlock ) ;
kfree ( cn ) ;
return 0 ;
}
2014-01-16 08:29:18 +04:00
static void w1_process_cb ( struct w1_master * dev , struct w1_async_cmd * async_cmd )
{
struct w1_cb_node * node = container_of ( async_cmd , struct w1_cb_node ,
async ) ;
2014-04-09 07:37:09 +04:00
u16 mlen = node - > msg - > len ;
u16 len ;
2014-01-16 08:29:18 +04:00
int err = 0 ;
struct w1_slave * sl = node - > sl ;
2014-04-09 07:37:09 +04:00
struct w1_netlink_cmd * cmd = ( struct w1_netlink_cmd * ) node - > msg - > data ;
2014-01-16 08:29:18 +04:00
2014-01-16 08:29:26 +04:00
mutex_lock ( & dev - > bus_mutex ) ;
2014-04-09 07:37:09 +04:00
dev - > priv = node - > block ;
2014-01-16 08:29:18 +04:00
if ( sl & & w1_reset_select_slave ( sl ) )
err = - ENODEV ;
2014-04-09 07:37:09 +04:00
node - > block - > cur_msg = node - > msg ;
2014-01-16 08:29:18 +04:00
while ( mlen & & ! err ) {
if ( cmd - > len + sizeof ( struct w1_netlink_cmd ) > mlen ) {
err = - E2BIG ;
break ;
}
if ( sl )
2014-04-09 07:37:09 +04:00
err = w1_process_command_slave ( sl , cmd ) ;
2014-01-16 08:29:18 +04:00
else
2014-04-09 07:37:09 +04:00
err = w1_process_command_master ( dev , cmd ) ;
w1_netlink_check_send ( node - > block ) ;
2014-01-16 08:29:18 +04:00
2014-04-09 07:37:09 +04:00
w1_netlink_queue_status ( node - > block , node - > msg , cmd , err ) ;
2014-01-16 08:29:18 +04:00
err = 0 ;
2014-04-09 07:37:09 +04:00
len = sizeof ( * cmd ) + cmd - > len ;
cmd = ( struct w1_netlink_cmd * ) ( ( u8 * ) cmd + len ) ;
mlen - = len ;
2014-01-16 08:29:18 +04:00
}
if ( ! cmd | | err )
2014-04-09 07:37:09 +04:00
w1_netlink_queue_status ( node - > block , node - > msg , cmd , err ) ;
2014-01-16 08:29:18 +04:00
2014-04-09 07:37:07 +04:00
/* ref taken in w1_search_slave or w1_search_master_id when building
* the block
*/
2014-01-16 08:29:18 +04:00
if ( sl )
w1_unref_slave ( sl ) ;
else
atomic_dec ( & dev - > refcnt ) ;
2014-04-09 07:37:09 +04:00
dev - > priv = NULL ;
2014-01-16 08:29:26 +04:00
mutex_unlock ( & dev - > bus_mutex ) ;
2014-01-16 08:29:18 +04:00
mutex_lock ( & dev - > list_mutex ) ;
list_del ( & async_cmd - > async_entry ) ;
mutex_unlock ( & dev - > list_mutex ) ;
2014-04-09 07:37:09 +04:00
w1_unref_block ( node - > block ) ;
}
static void w1_list_count_cmds ( struct w1_netlink_msg * msg , int * cmd_count ,
u16 * slave_len )
{
struct w1_netlink_cmd * cmd = ( struct w1_netlink_cmd * ) msg - > data ;
u16 mlen = msg - > len ;
u16 len ;
int slave_list = 0 ;
while ( mlen ) {
if ( cmd - > len + sizeof ( struct w1_netlink_cmd ) > mlen )
break ;
switch ( cmd - > cmd ) {
case W1_CMD_SEARCH :
case W1_CMD_ALARM_SEARCH :
case W1_CMD_LIST_SLAVES :
+ + slave_list ;
}
+ + * cmd_count ;
len = sizeof ( * cmd ) + cmd - > len ;
cmd = ( struct w1_netlink_cmd * ) ( ( u8 * ) cmd + len ) ;
mlen - = len ;
}
if ( slave_list ) {
struct w1_master * dev = w1_search_master_id ( msg - > id . mst . id ) ;
if ( dev ) {
/* Bytes, and likely an overstimate, and if it isn't
* the results can still be split between packets .
*/
* slave_len + = sizeof ( struct w1_reg_num ) * slave_list *
( dev - > slave_count + dev - > max_slave_count ) ;
/* search incremented it */
atomic_dec ( & dev - > refcnt ) ;
}
}
2014-01-16 08:29:18 +04:00
}
2014-04-09 07:37:09 +04:00
static void w1_cn_callback ( struct cn_msg * cn , struct netlink_skb_parms * nsp )
2006-03-23 19:11:58 +03:00
{
2014-04-09 07:37:09 +04:00
struct w1_netlink_msg * msg = ( struct w1_netlink_msg * ) ( cn + 1 ) ;
2006-03-23 19:11:58 +03:00
struct w1_slave * sl ;
struct w1_master * dev ;
2014-01-16 08:29:18 +04:00
u16 msg_len ;
2014-04-09 07:37:09 +04:00
u16 slave_len = 0 ;
2006-03-23 19:11:58 +03:00
int err = 0 ;
2014-01-16 08:29:18 +04:00
struct w1_cb_block * block = NULL ;
struct w1_cb_node * node = NULL ;
int node_count = 0 ;
2014-04-09 07:37:09 +04:00
int cmd_count = 0 ;
/* If any unknown flag is set let the application know, that way
* applications can detect the absence of features in kernels that
* don ' t know about them . http : //lwn.net/Articles/587527/
*/
if ( cn - > flags & ~ ( W1_CN_BUNDLE ) ) {
w1_netlink_send_error ( cn , msg , nsp - > portid , - EINVAL ) ;
return ;
}
2014-01-16 08:29:18 +04:00
/* Count the number of master or slave commands there are to allocate
* space for one cb_node each .
*/
2014-04-09 07:37:09 +04:00
msg_len = cn - > len ;
2014-01-16 08:29:18 +04:00
while ( msg_len & & ! err ) {
2014-04-09 07:37:09 +04:00
if ( msg - > len + sizeof ( struct w1_netlink_msg ) > msg_len ) {
2014-01-16 08:29:18 +04:00
err = - E2BIG ;
break ;
}
2014-04-09 07:37:09 +04:00
/* count messages for nodes and allocate any additional space
* required for slave lists
*/
if ( msg - > type = = W1_MASTER_CMD | | msg - > type = = W1_SLAVE_CMD ) {
2014-01-16 08:29:18 +04:00
+ + node_count ;
2014-04-09 07:37:09 +04:00
w1_list_count_cmds ( msg , & cmd_count , & slave_len ) ;
}
2014-01-16 08:29:18 +04:00
2014-04-09 07:37:09 +04:00
msg_len - = sizeof ( struct w1_netlink_msg ) + msg - > len ;
msg = ( struct w1_netlink_msg * ) ( ( ( u8 * ) msg ) +
sizeof ( struct w1_netlink_msg ) + msg - > len ) ;
2014-01-16 08:29:18 +04:00
}
2014-04-09 07:37:09 +04:00
msg = ( struct w1_netlink_msg * ) ( cn + 1 ) ;
2014-01-16 08:29:18 +04:00
if ( node_count ) {
2014-04-09 07:37:09 +04:00
int size ;
2014-11-11 05:19:37 +03:00
int reply_size = sizeof ( * cn ) + cn - > len + slave_len ;
2014-04-09 07:37:09 +04:00
if ( cn - > flags & W1_CN_BUNDLE ) {
/* bundling duplicats some of the messages */
reply_size + = 2 * cmd_count * ( sizeof ( struct cn_msg ) +
sizeof ( struct w1_netlink_msg ) +
sizeof ( struct w1_netlink_cmd ) ) ;
}
reply_size = MIN ( CONNECTOR_MAX_MSG_SIZE , reply_size ) ;
/* allocate space for the block, a copy of the original message,
* one node per cmd to point into the original message ,
* space for replies which is the original message size plus
* space for any list slave data and status messages
* cn - > len doesn ' t include itself which is part of the block
* */
size = /* block + original message */
sizeof ( struct w1_cb_block ) + sizeof ( * cn ) + cn - > len +
/* space for nodes */
node_count * sizeof ( struct w1_cb_node ) +
/* replies */
sizeof ( struct cn_msg ) + reply_size ;
block = kzalloc ( size , GFP_KERNEL ) ;
2014-01-16 08:29:18 +04:00
if ( ! block ) {
2014-04-09 07:37:09 +04:00
/* if the system is already out of memory,
* ( A ) will this work , and ( B ) would it be better
* to not try ?
*/
w1_netlink_send_error ( cn , msg , nsp - > portid , - ENOMEM ) ;
2014-01-16 08:29:18 +04:00
return ;
}
atomic_set ( & block - > refcnt , 1 ) ;
2014-01-16 08:29:20 +04:00
block - > portid = nsp - > portid ;
2014-04-09 07:37:09 +04:00
memcpy ( & block - > request_cn , cn , sizeof ( * cn ) + cn - > len ) ;
node = ( struct w1_cb_node * ) ( block - > request_cn . data + cn - > len ) ;
/* Sneeky, when not bundling, reply_size is the allocated space
* required for the reply , cn_msg isn ' t part of maxlen so
* it should be reply_size - sizeof ( struct cn_msg ) , however
* when checking if there is enough space , w1_reply_make_space
* is called with the full message size including cn_msg ,
* because it isn ' t known at that time if an additional cn_msg
* will need to be allocated . So an extra cn_msg is added
* above in " size " .
*/
block - > maxlen = reply_size ;
block - > first_cn = ( struct cn_msg * ) ( node + node_count ) ;
memset ( block - > first_cn , 0 , sizeof ( * block - > first_cn ) ) ;
2014-01-16 08:29:18 +04:00
}
2006-03-23 19:11:58 +03:00
2014-04-09 07:37:09 +04:00
msg_len = cn - > len ;
2014-01-16 08:29:18 +04:00
while ( msg_len & & ! err ) {
2006-03-23 19:11:58 +03:00
dev = NULL ;
sl = NULL ;
2014-04-09 07:37:09 +04:00
if ( msg - > len + sizeof ( struct w1_netlink_msg ) > msg_len ) {
2006-03-23 19:11:58 +03:00
err = - E2BIG ;
break ;
}
2014-04-09 07:37:07 +04:00
/* execute on this thread, no need to process later */
2014-04-09 07:37:09 +04:00
if ( msg - > type = = W1_LIST_MASTERS ) {
err = w1_process_command_root ( cn , nsp - > portid ) ;
2014-04-09 07:37:07 +04:00
goto out_cont ;
}
/* All following message types require additional data,
* check here before references are taken .
*/
2014-04-09 07:37:09 +04:00
if ( ! msg - > len ) {
2014-04-09 07:37:07 +04:00
err = - EPROTO ;
goto out_cont ;
}
2014-04-09 07:37:09 +04:00
/* both search calls take references */
if ( msg - > type = = W1_MASTER_CMD ) {
dev = w1_search_master_id ( msg - > id . mst . id ) ;
} else if ( msg - > type = = W1_SLAVE_CMD ) {
sl = w1_search_slave ( ( struct w1_reg_num * ) msg - > id . id ) ;
2006-03-23 19:11:58 +03:00
if ( sl )
dev = sl - > master ;
2009-01-08 05:08:59 +03:00
} else {
2014-06-19 04:52:00 +04:00
pr_notice ( " %s: cn: %x.%x, wrong type: %u, len: %u. \n " ,
2014-04-09 07:37:09 +04:00
__func__ , cn - > id . idx , cn - > id . val ,
msg - > type , msg - > len ) ;
2014-04-09 07:37:07 +04:00
err = - EPROTO ;
2009-01-08 05:08:59 +03:00
goto out_cont ;
2006-03-23 19:11:58 +03:00
}
if ( ! dev ) {
err = - ENODEV ;
goto out_cont ;
}
2009-01-08 05:09:01 +03:00
err = 0 ;
2014-01-16 08:29:18 +04:00
atomic_inc ( & block - > refcnt ) ;
node - > async . cb = w1_process_cb ;
node - > block = block ;
2014-04-09 07:37:09 +04:00
node - > msg = ( struct w1_netlink_msg * ) ( ( u8 * ) & block - > request_cn +
( size_t ) ( ( u8 * ) msg - ( u8 * ) cn ) ) ;
2014-01-16 08:29:18 +04:00
node - > sl = sl ;
node - > dev = dev ;
2006-03-23 19:11:58 +03:00
2014-01-16 08:29:18 +04:00
mutex_lock ( & dev - > list_mutex ) ;
list_add_tail ( & node - > async . async_entry , & dev - > async_list ) ;
wake_up_process ( dev - > thread ) ;
mutex_unlock ( & dev - > list_mutex ) ;
+ + node ;
2009-01-08 05:09:05 +03:00
2006-03-23 19:11:58 +03:00
out_cont :
2014-04-09 07:37:09 +04:00
/* Can't queue because that modifies block and another
* thread could be processing the messages by now and
* there isn ' t a lock , send directly .
*/
2014-01-16 08:29:18 +04:00
if ( err )
2014-04-09 07:37:09 +04:00
w1_netlink_send_error ( cn , msg , nsp - > portid , err ) ;
msg_len - = sizeof ( struct w1_netlink_msg ) + msg - > len ;
msg = ( struct w1_netlink_msg * ) ( ( ( u8 * ) msg ) +
sizeof ( struct w1_netlink_msg ) + msg - > len ) ;
2006-03-23 19:11:58 +03:00
/*
* Let ' s allow requests for nonexisting devices .
*/
if ( err = = - ENODEV )
err = 0 ;
}
2014-04-09 07:37:09 +04:00
if ( block )
w1_unref_block ( block ) ;
2005-07-27 13:10:11 +04:00
}
2006-03-23 19:11:58 +03:00
int w1_init_netlink ( void )
2005-07-27 13:10:11 +04:00
{
2006-03-23 19:11:58 +03:00
struct cb_id w1_id = { . idx = CN_W1_IDX , . val = CN_W1_VAL } ;
return cn_add_callback ( & w1_id , " w1 " , & w1_cn_callback ) ;
}
void w1_fini_netlink ( void )
{
struct cb_id w1_id = { . idx = CN_W1_IDX , . val = CN_W1_VAL } ;
cn_del_callback ( & w1_id ) ;
2005-07-27 13:10:11 +04:00
}
2005-04-17 02:20:36 +04:00
# else
2014-04-09 07:37:09 +04:00
void w1_netlink_send ( struct w1_master * dev , struct w1_netlink_msg * cn )
2005-04-17 02:20:36 +04:00
{
}
2005-07-27 13:10:11 +04:00
2006-03-23 19:11:58 +03:00
int w1_init_netlink ( void )
2005-07-27 13:10:11 +04:00
{
return 0 ;
}
2006-03-23 19:11:58 +03:00
void w1_fini_netlink ( void )
2005-07-27 13:10:11 +04:00
{
}
2005-04-17 02:20:36 +04:00
# endif