diff options
Diffstat (limited to 'drivers/w1/w1_netlink.c')
| -rw-r--r-- | drivers/w1/w1_netlink.c | 219 |
1 files changed, 186 insertions, 33 deletions
diff --git a/drivers/w1/w1_netlink.c b/drivers/w1/w1_netlink.c index 328645da7972..65c5ebd0787e 100644 --- a/drivers/w1/w1_netlink.c +++ b/drivers/w1/w1_netlink.c | |||
| @@ -21,72 +21,225 @@ | |||
| 21 | 21 | ||
| 22 | #include <linux/skbuff.h> | 22 | #include <linux/skbuff.h> |
| 23 | #include <linux/netlink.h> | 23 | #include <linux/netlink.h> |
| 24 | #include <linux/connector.h> | ||
| 24 | 25 | ||
| 25 | #include "w1.h" | 26 | #include "w1.h" |
| 26 | #include "w1_log.h" | 27 | #include "w1_log.h" |
| 27 | #include "w1_netlink.h" | 28 | #include "w1_netlink.h" |
| 28 | 29 | ||
| 29 | #ifndef NETLINK_DISABLED | 30 | #if defined(CONFIG_W1_CON) && (defined(CONFIG_CONNECTOR) || (defined(CONFIG_CONNECTOR_MODULE) && defined(CONFIG_W1_MODULE))) |
| 30 | void w1_netlink_send(struct w1_master *dev, struct w1_netlink_msg *msg) | 31 | void w1_netlink_send(struct w1_master *dev, struct w1_netlink_msg *msg) |
| 31 | { | 32 | { |
| 32 | unsigned int size; | 33 | char buf[sizeof(struct cn_msg) + sizeof(struct w1_netlink_msg)]; |
| 33 | struct sk_buff *skb; | 34 | struct cn_msg *m = (struct cn_msg *)buf; |
| 34 | struct w1_netlink_msg *data; | 35 | struct w1_netlink_msg *w = (struct w1_netlink_msg *)(m+1); |
| 35 | struct nlmsghdr *nlh; | ||
| 36 | 36 | ||
| 37 | if (!dev->nls) | 37 | memset(buf, 0, sizeof(buf)); |
| 38 | return; | ||
| 39 | 38 | ||
| 40 | size = NLMSG_SPACE(sizeof(struct w1_netlink_msg)); | 39 | m->id.idx = CN_W1_IDX; |
| 40 | m->id.val = CN_W1_VAL; | ||
| 41 | 41 | ||
| 42 | skb = alloc_skb(size, GFP_ATOMIC); | 42 | m->seq = dev->seq++; |
| 43 | if (!skb) { | 43 | m->len = sizeof(struct w1_netlink_msg); |
| 44 | dev_err(&dev->dev, "skb_alloc() failed.\n"); | 44 | |
| 45 | return; | 45 | memcpy(w, msg, sizeof(struct w1_netlink_msg)); |
| 46 | } | 46 | |
| 47 | cn_netlink_send(m, 0, GFP_KERNEL); | ||
| 48 | } | ||
| 49 | |||
| 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) | ||
| 52 | { | ||
| 53 | dev_dbg(&dev->dev, "%s: %s: cmd=%02x, len=%u.\n", | ||
| 54 | __func__, dev->name, cmd->cmd, cmd->len); | ||
| 55 | |||
| 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); | ||
| 60 | return 0; | ||
| 61 | } | ||
| 62 | |||
| 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) | ||
| 65 | { | ||
| 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)); | ||
| 47 | 86 | ||
| 48 | nlh = NLMSG_PUT(skb, 0, dev->seq++, NLMSG_DONE, size - sizeof(*nlh)); | 87 | cm->ack = msg->seq+1; |
| 88 | cm->len = sizeof(struct w1_netlink_msg) + sizeof(struct w1_netlink_cmd) + cmd->len; | ||
| 49 | 89 | ||
| 50 | data = (struct w1_netlink_msg *)NLMSG_DATA(nlh); | 90 | h->len = sizeof(struct w1_netlink_cmd) + cmd->len; |
| 51 | 91 | ||
| 52 | memcpy(data, msg, sizeof(struct w1_netlink_msg)); | 92 | memcpy(c->data, cmd->data, c->len); |
| 53 | 93 | ||
| 54 | NETLINK_CB(skb).dst_group = dev->groups; | 94 | err = cn_netlink_send(cm, 0, GFP_KERNEL); |
| 55 | netlink_broadcast(dev->nls, skb, 0, dev->groups, GFP_ATOMIC); | ||
| 56 | 95 | ||
| 57 | nlmsg_failure: | 96 | kfree(data); |
| 58 | return; | 97 | |
| 98 | return err; | ||
| 59 | } | 99 | } |
| 60 | 100 | ||
| 61 | int dev_init_netlink(struct w1_master *dev) | 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) | ||
| 62 | { | 103 | { |
| 63 | dev->nls = netlink_kernel_create(NETLINK_W1, 1, NULL, THIS_MODULE); | 104 | int err = 0; |
| 64 | if (!dev->nls) { | 105 | |
| 65 | printk(KERN_ERR "Failed to create new netlink socket(%u) for w1 master %s.\n", | 106 | dev_dbg(&sl->master->dev, "%s: %02x.%012llx.%02x: cmd=%02x, len=%u.\n", |
| 66 | NETLINK_W1, dev->dev.bus_id); | 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; | ||
| 67 | } | 126 | } |
| 68 | 127 | ||
| 69 | return 0; | 128 | return err; |
| 70 | } | 129 | } |
| 71 | 130 | ||
| 72 | void dev_fini_netlink(struct w1_master *dev) | 131 | static void w1_cn_callback(void *data) |
| 73 | { | 132 | { |
| 74 | if (dev->nls && dev->nls->sk_socket) | 133 | struct cn_msg *msg = data; |
| 75 | sock_release(dev->nls->sk_socket); | 134 | struct w1_netlink_msg *m = (struct w1_netlink_msg *)(msg + 1); |
| 135 | struct w1_netlink_cmd *cmd; | ||
| 136 | struct w1_slave *sl; | ||
| 137 | struct w1_master *dev; | ||
| 138 | int err = 0; | ||
| 139 | |||
| 140 | while (msg->len && !err) { | ||
| 141 | struct w1_reg_num id; | ||
| 142 | u16 mlen = m->len; | ||
| 143 | u8 *cmd_data = m->data; | ||
| 144 | |||
| 145 | dev = NULL; | ||
| 146 | sl = NULL; | ||
| 147 | |||
| 148 | memcpy(&id, m->id.id, sizeof(id)); | ||
| 149 | #if 0 | ||
| 150 | printk("%s: %02x.%012llx.%02x: type=%02x, len=%u.\n", | ||
| 151 | __func__, id.family, (unsigned long long)id.id, id.crc, m->type, m->len); | ||
| 152 | #endif | ||
| 153 | if (m->len + sizeof(struct w1_netlink_msg) > msg->len) { | ||
| 154 | err = -E2BIG; | ||
| 155 | break; | ||
| 156 | } | ||
| 157 | |||
| 158 | if (!mlen) | ||
| 159 | goto out_cont; | ||
| 160 | |||
| 161 | if (m->type == W1_MASTER_CMD) { | ||
| 162 | dev = w1_search_master_id(m->id.mst.id); | ||
| 163 | } else if (m->type == W1_SLAVE_CMD) { | ||
| 164 | sl = w1_search_slave(&id); | ||
| 165 | if (sl) | ||
| 166 | dev = sl->master; | ||
| 167 | } | ||
| 168 | |||
| 169 | if (!dev) { | ||
| 170 | err = -ENODEV; | ||
| 171 | goto out_cont; | ||
| 172 | } | ||
| 173 | |||
| 174 | mutex_lock(&dev->mutex); | ||
| 175 | |||
| 176 | if (sl && w1_reset_select_slave(sl)) { | ||
| 177 | err = -ENODEV; | ||
| 178 | goto out_up; | ||
| 179 | } | ||
| 180 | |||
| 181 | while (mlen) { | ||
| 182 | cmd = (struct w1_netlink_cmd *)cmd_data; | ||
| 183 | |||
| 184 | if (cmd->len + sizeof(struct w1_netlink_cmd) > mlen) { | ||
| 185 | err = -E2BIG; | ||
| 186 | break; | ||
| 187 | } | ||
| 188 | |||
| 189 | if (sl) | ||
| 190 | w1_process_command_slave(sl, msg, m, cmd); | ||
| 191 | else | ||
| 192 | w1_process_command_master(dev, msg, m, cmd); | ||
| 193 | |||
| 194 | cmd_data += cmd->len + sizeof(struct w1_netlink_cmd); | ||
| 195 | mlen -= cmd->len + sizeof(struct w1_netlink_cmd); | ||
| 196 | } | ||
| 197 | out_up: | ||
| 198 | atomic_dec(&dev->refcnt); | ||
| 199 | if (sl) | ||
| 200 | atomic_dec(&sl->refcnt); | ||
| 201 | mutex_unlock(&dev->mutex); | ||
| 202 | out_cont: | ||
| 203 | msg->len -= sizeof(struct w1_netlink_msg) + m->len; | ||
| 204 | m = (struct w1_netlink_msg *)(((u8 *)m) + sizeof(struct w1_netlink_msg) + m->len); | ||
| 205 | |||
| 206 | /* | ||
| 207 | * Let's allow requests for nonexisting devices. | ||
| 208 | */ | ||
| 209 | if (err == -ENODEV) | ||
| 210 | err = 0; | ||
| 211 | } | ||
| 212 | #if 0 | ||
| 213 | if (err) { | ||
| 214 | printk("%s: malformed message. Dropping.\n", __func__); | ||
| 215 | } | ||
| 216 | #endif | ||
| 76 | } | 217 | } |
| 77 | #else | ||
| 78 | #warning Netlink support is disabled. Please compile with NET support enabled. | ||
| 79 | 218 | ||
| 219 | int w1_init_netlink(void) | ||
| 220 | { | ||
| 221 | struct cb_id w1_id = {.idx = CN_W1_IDX, .val = CN_W1_VAL}; | ||
| 222 | |||
| 223 | return cn_add_callback(&w1_id, "w1", &w1_cn_callback); | ||
| 224 | } | ||
| 225 | |||
| 226 | void w1_fini_netlink(void) | ||
| 227 | { | ||
| 228 | struct cb_id w1_id = {.idx = CN_W1_IDX, .val = CN_W1_VAL}; | ||
| 229 | |||
| 230 | cn_del_callback(&w1_id); | ||
| 231 | } | ||
| 232 | #else | ||
| 80 | void w1_netlink_send(struct w1_master *dev, struct w1_netlink_msg *msg) | 233 | void w1_netlink_send(struct w1_master *dev, struct w1_netlink_msg *msg) |
| 81 | { | 234 | { |
| 82 | } | 235 | } |
| 83 | 236 | ||
| 84 | int dev_init_netlink(struct w1_master *dev) | 237 | int w1_init_netlink(void) |
| 85 | { | 238 | { |
| 86 | return 0; | 239 | return 0; |
| 87 | } | 240 | } |
| 88 | 241 | ||
| 89 | void dev_fini_netlink(struct w1_master *dev) | 242 | void w1_fini_netlink(void) |
| 90 | { | 243 | { |
| 91 | } | 244 | } |
| 92 | #endif | 245 | #endif |
