X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=drivers%2Fw1%2Fw1_netlink.c;h=328645da79727f8eee095bce9c7d4d7047841d55;hb=9464c7cf61b9433057924c36e6e02f303a00e768;hp=65c5ebd0787e0ea4840ab73db3af612443d3d7b2;hpb=41689045f6a3cbe0550e1d34e9cc20d2e8c432ba;p=linux-2.6.git diff --git a/drivers/w1/w1_netlink.c b/drivers/w1/w1_netlink.c index 65c5ebd07..328645da7 100644 --- a/drivers/w1/w1_netlink.c +++ b/drivers/w1/w1_netlink.c @@ -21,225 +21,72 @@ #include #include -#include #include "w1.h" #include "w1_log.h" #include "w1_netlink.h" -#if defined(CONFIG_W1_CON) && (defined(CONFIG_CONNECTOR) || (defined(CONFIG_CONNECTOR_MODULE) && defined(CONFIG_W1_MODULE))) +#ifndef NETLINK_DISABLED void w1_netlink_send(struct w1_master *dev, struct w1_netlink_msg *msg) { - char buf[sizeof(struct cn_msg) + sizeof(struct w1_netlink_msg)]; - struct cn_msg *m = (struct cn_msg *)buf; - struct w1_netlink_msg *w = (struct w1_netlink_msg *)(m+1); + unsigned int size; + struct sk_buff *skb; + struct w1_netlink_msg *data; + struct nlmsghdr *nlh; - memset(buf, 0, sizeof(buf)); + if (!dev->nls) + return; - m->id.idx = CN_W1_IDX; - m->id.val = CN_W1_VAL; + size = NLMSG_SPACE(sizeof(struct w1_netlink_msg)); - m->seq = dev->seq++; - m->len = sizeof(struct w1_netlink_msg); - - memcpy(w, msg, sizeof(struct w1_netlink_msg)); - - cn_netlink_send(m, 0, GFP_KERNEL); -} - -static int w1_process_command_master(struct w1_master *dev, struct cn_msg *msg, - struct w1_netlink_msg *hdr, struct w1_netlink_cmd *cmd) -{ - dev_dbg(&dev->dev, "%s: %s: cmd=%02x, len=%u.\n", - __func__, dev->name, cmd->cmd, cmd->len); - - if (cmd->cmd != W1_CMD_SEARCH && cmd->cmd != W1_CMD_ALARM_SEARCH) - return -EINVAL; - - w1_search_process(dev, (cmd->cmd == W1_CMD_ALARM_SEARCH)?W1_ALARM_SEARCH:W1_SEARCH); - return 0; -} - -static int w1_send_read_reply(struct w1_slave *sl, struct cn_msg *msg, - struct w1_netlink_msg *hdr, struct w1_netlink_cmd *cmd) -{ - void *data; - struct w1_netlink_msg *h; - struct w1_netlink_cmd *c; - struct cn_msg *cm; - int err; - - data = kzalloc(sizeof(struct cn_msg) + - sizeof(struct w1_netlink_msg) + - sizeof(struct w1_netlink_cmd) + - cmd->len, GFP_KERNEL); - if (!data) - return -ENOMEM; - - cm = (struct cn_msg *)(data); - h = (struct w1_netlink_msg *)(cm + 1); - c = (struct w1_netlink_cmd *)(h + 1); - - memcpy(cm, msg, sizeof(struct cn_msg)); - memcpy(h, hdr, sizeof(struct w1_netlink_msg)); - memcpy(c, cmd, sizeof(struct w1_netlink_cmd)); - - cm->ack = msg->seq+1; - cm->len = sizeof(struct w1_netlink_msg) + sizeof(struct w1_netlink_cmd) + cmd->len; + skb = alloc_skb(size, GFP_ATOMIC); + if (!skb) { + dev_err(&dev->dev, "skb_alloc() failed.\n"); + return; + } - h->len = sizeof(struct w1_netlink_cmd) + cmd->len; + nlh = NLMSG_PUT(skb, 0, dev->seq++, NLMSG_DONE, size - sizeof(*nlh)); - memcpy(c->data, cmd->data, c->len); + data = (struct w1_netlink_msg *)NLMSG_DATA(nlh); - err = cn_netlink_send(cm, 0, GFP_KERNEL); + memcpy(data, msg, sizeof(struct w1_netlink_msg)); - kfree(data); + NETLINK_CB(skb).dst_group = dev->groups; + netlink_broadcast(dev->nls, skb, 0, dev->groups, GFP_ATOMIC); - return err; +nlmsg_failure: + return; } -static int w1_process_command_slave(struct w1_slave *sl, struct cn_msg *msg, - struct w1_netlink_msg *hdr, struct w1_netlink_cmd *cmd) +int dev_init_netlink(struct w1_master *dev) { - int err = 0; - - 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); - - switch (cmd->cmd) { - case W1_CMD_READ: - w1_read_block(sl->master, cmd->data, cmd->len); - w1_send_read_reply(sl, msg, hdr, cmd); - break; - case W1_CMD_WRITE: - w1_write_block(sl->master, cmd->data, cmd->len); - break; - case W1_CMD_SEARCH: - case W1_CMD_ALARM_SEARCH: - w1_search_process(sl->master, - (cmd->cmd == W1_CMD_ALARM_SEARCH)?W1_ALARM_SEARCH:W1_SEARCH); - break; - default: - err = -1; - break; + dev->nls = netlink_kernel_create(NETLINK_W1, 1, NULL, THIS_MODULE); + if (!dev->nls) { + printk(KERN_ERR "Failed to create new netlink socket(%u) for w1 master %s.\n", + NETLINK_W1, dev->dev.bus_id); } - return err; -} - -static void w1_cn_callback(void *data) -{ - struct cn_msg *msg = data; - struct w1_netlink_msg *m = (struct w1_netlink_msg *)(msg + 1); - struct w1_netlink_cmd *cmd; - struct w1_slave *sl; - struct w1_master *dev; - int err = 0; - - while (msg->len && !err) { - struct w1_reg_num id; - u16 mlen = m->len; - u8 *cmd_data = m->data; - - dev = NULL; - sl = NULL; - - memcpy(&id, m->id.id, sizeof(id)); -#if 0 - printk("%s: %02x.%012llx.%02x: type=%02x, len=%u.\n", - __func__, id.family, (unsigned long long)id.id, id.crc, m->type, m->len); -#endif - if (m->len + sizeof(struct w1_netlink_msg) > msg->len) { - err = -E2BIG; - break; - } - - if (!mlen) - goto out_cont; - - if (m->type == W1_MASTER_CMD) { - dev = w1_search_master_id(m->id.mst.id); - } else if (m->type == W1_SLAVE_CMD) { - sl = w1_search_slave(&id); - if (sl) - dev = sl->master; - } - - if (!dev) { - err = -ENODEV; - goto out_cont; - } - - mutex_lock(&dev->mutex); - - if (sl && w1_reset_select_slave(sl)) { - err = -ENODEV; - goto out_up; - } - - while (mlen) { - cmd = (struct w1_netlink_cmd *)cmd_data; - - if (cmd->len + sizeof(struct w1_netlink_cmd) > mlen) { - err = -E2BIG; - break; - } - - if (sl) - w1_process_command_slave(sl, msg, m, cmd); - else - w1_process_command_master(dev, msg, m, cmd); - - cmd_data += cmd->len + sizeof(struct w1_netlink_cmd); - mlen -= cmd->len + sizeof(struct w1_netlink_cmd); - } -out_up: - atomic_dec(&dev->refcnt); - if (sl) - atomic_dec(&sl->refcnt); - mutex_unlock(&dev->mutex); -out_cont: - msg->len -= sizeof(struct w1_netlink_msg) + m->len; - m = (struct w1_netlink_msg *)(((u8 *)m) + sizeof(struct w1_netlink_msg) + m->len); - - /* - * Let's allow requests for nonexisting devices. - */ - if (err == -ENODEV) - err = 0; - } -#if 0 - if (err) { - printk("%s: malformed message. Dropping.\n", __func__); - } -#endif -} - -int w1_init_netlink(void) -{ - struct cb_id w1_id = {.idx = CN_W1_IDX, .val = CN_W1_VAL}; - - return cn_add_callback(&w1_id, "w1", &w1_cn_callback); + return 0; } -void w1_fini_netlink(void) +void dev_fini_netlink(struct w1_master *dev) { - struct cb_id w1_id = {.idx = CN_W1_IDX, .val = CN_W1_VAL}; - - cn_del_callback(&w1_id); + if (dev->nls && dev->nls->sk_socket) + sock_release(dev->nls->sk_socket); } #else +#warning Netlink support is disabled. Please compile with NET support enabled. + void w1_netlink_send(struct w1_master *dev, struct w1_netlink_msg *msg) { } -int w1_init_netlink(void) +int dev_init_netlink(struct w1_master *dev) { return 0; } -void w1_fini_netlink(void) +void dev_fini_netlink(struct w1_master *dev) { } #endif