4 * Copyright (c) 2003 Evgeniy Polyakov <johnpol@2ka.mipt.ru>
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/skbuff.h>
23 #include <linux/netlink.h>
24 #include <linux/connector.h>
28 #include "w1_netlink.h"
30 #if defined(CONFIG_W1_CON) && (defined(CONFIG_CONNECTOR) || (defined(CONFIG_CONNECTOR_MODULE) && defined(CONFIG_W1_MODULE)))
31 void w1_netlink_send(struct w1_master *dev, struct w1_netlink_msg *msg)
33 char buf[sizeof(struct cn_msg) + sizeof(struct w1_netlink_msg)];
34 struct cn_msg *m = (struct cn_msg *)buf;
35 struct w1_netlink_msg *w = (struct w1_netlink_msg *)(m+1);
37 memset(buf, 0, sizeof(buf));
39 m->id.idx = CN_W1_IDX;
40 m->id.val = CN_W1_VAL;
43 m->len = sizeof(struct w1_netlink_msg);
45 memcpy(w, msg, sizeof(struct w1_netlink_msg));
47 cn_netlink_send(m, 0, GFP_KERNEL);
50 static void w1_send_slave(struct w1_master *dev, u64 rn)
52 struct cn_msg *msg = dev->priv;
53 struct w1_netlink_msg *hdr = (struct w1_netlink_msg *)(msg + 1);
54 struct w1_netlink_cmd *cmd = (struct w1_netlink_cmd *)(hdr + 1);
57 avail = dev->priv_size - cmd->len;
60 u64 *data = (void *)(cmd + 1) + cmd->len;
70 cn_netlink_send(msg, 0, GFP_KERNEL);
72 msg->len = sizeof(struct w1_netlink_msg) + sizeof(struct w1_netlink_cmd);
73 hdr->len = sizeof(struct w1_netlink_cmd);
77 static int w1_process_search_command(struct w1_master *dev, struct cn_msg *msg,
80 struct w1_netlink_msg *hdr = (struct w1_netlink_msg *)(msg + 1);
81 struct w1_netlink_cmd *cmd = (struct w1_netlink_cmd *)(hdr + 1);
82 int search_type = (cmd->cmd == W1_CMD_ALARM_SEARCH)?W1_ALARM_SEARCH:W1_SEARCH;
85 dev->priv_size = avail;
87 w1_search_devices(dev, search_type, w1_send_slave);
90 cn_netlink_send(msg, 0, GFP_KERNEL);
98 static int w1_process_command_master(struct w1_master *dev, struct cn_msg *req_msg,
99 struct w1_netlink_msg *req_hdr, struct w1_netlink_cmd *req_cmd)
103 struct w1_netlink_msg *hdr;
104 struct w1_netlink_cmd *cmd;
106 msg = kzalloc(PAGE_SIZE, GFP_KERNEL);
110 msg->id = req_msg->id;
111 msg->seq = req_msg->seq;
113 msg->len = sizeof(struct w1_netlink_msg) + sizeof(struct w1_netlink_cmd);
115 hdr = (struct w1_netlink_msg *)(msg + 1);
116 cmd = (struct w1_netlink_cmd *)(hdr + 1);
118 hdr->type = W1_MASTER_CMD;
119 hdr->id = req_hdr->id;
120 hdr->len = sizeof(struct w1_netlink_cmd);
122 cmd->cmd = req_cmd->cmd;
127 case W1_CMD_ALARM_SEARCH:
128 err = w1_process_search_command(dev, msg,
129 PAGE_SIZE - msg->len - sizeof(struct cn_msg));
133 cn_netlink_send(msg, 0, GFP_KERNEL);
141 static int w1_send_read_reply(struct w1_slave *sl, struct cn_msg *msg,
142 struct w1_netlink_msg *hdr, struct w1_netlink_cmd *cmd)
145 struct w1_netlink_msg *h;
146 struct w1_netlink_cmd *c;
150 data = kzalloc(sizeof(struct cn_msg) +
151 sizeof(struct w1_netlink_msg) +
152 sizeof(struct w1_netlink_cmd) +
153 cmd->len, GFP_KERNEL);
157 cm = (struct cn_msg *)(data);
158 h = (struct w1_netlink_msg *)(cm + 1);
159 c = (struct w1_netlink_cmd *)(h + 1);
161 memcpy(cm, msg, sizeof(struct cn_msg));
162 memcpy(h, hdr, sizeof(struct w1_netlink_msg));
163 memcpy(c, cmd, sizeof(struct w1_netlink_cmd));
165 cm->ack = msg->seq+1;
166 cm->len = sizeof(struct w1_netlink_msg) + sizeof(struct w1_netlink_cmd) + cmd->len;
168 h->len = sizeof(struct w1_netlink_cmd) + cmd->len;
170 memcpy(c->data, cmd->data, c->len);
172 err = cn_netlink_send(cm, 0, GFP_KERNEL);
179 static int w1_process_command_slave(struct w1_slave *sl, struct cn_msg *msg,
180 struct w1_netlink_msg *hdr, struct w1_netlink_cmd *cmd)
184 dev_dbg(&sl->master->dev, "%s: %02x.%012llx.%02x: cmd=%02x, len=%u.\n",
185 __func__, sl->reg_num.family, (unsigned long long)sl->reg_num.id, sl->reg_num.crc,
190 w1_touch_block(sl->master, cmd->data, cmd->len);
191 w1_send_read_reply(sl, msg, hdr, cmd);
194 w1_read_block(sl->master, cmd->data, cmd->len);
195 w1_send_read_reply(sl, msg, hdr, cmd);
198 w1_write_block(sl->master, cmd->data, cmd->len);
208 static int w1_process_command_root(struct cn_msg *msg, struct w1_netlink_msg *mcmd)
212 struct w1_netlink_msg *w;
215 if (mcmd->type != W1_LIST_MASTERS) {
216 printk(KERN_NOTICE "%s: msg: %x.%x, wrong type: %u, len: %u.\n",
217 __func__, msg->id.idx, msg->id.val, mcmd->type, mcmd->len);
221 cn = kmalloc(PAGE_SIZE, GFP_KERNEL);
225 cn->id.idx = CN_W1_IDX;
226 cn->id.val = CN_W1_VAL;
230 cn->len = sizeof(struct w1_netlink_msg);
231 w = (struct w1_netlink_msg *)(cn + 1);
233 w->type = W1_LIST_MASTERS;
238 mutex_lock(&w1_mlock);
239 list_for_each_entry(m, &w1_masters, w1_master_entry) {
240 if (cn->len + sizeof(*id) > PAGE_SIZE - sizeof(struct cn_msg)) {
241 cn_netlink_send(cn, 0, GFP_KERNEL);
243 cn->len = sizeof(struct w1_netlink_msg);
249 w->len += sizeof(*id);
250 cn->len += sizeof(*id);
254 cn_netlink_send(cn, 0, GFP_KERNEL);
255 mutex_unlock(&w1_mlock);
261 static void w1_cn_callback(void *data)
263 struct cn_msg *msg = data;
264 struct w1_netlink_msg *m = (struct w1_netlink_msg *)(msg + 1);
265 struct w1_netlink_cmd *cmd;
267 struct w1_master *dev;
270 while (msg->len && !err) {
271 struct w1_reg_num id;
273 u8 *cmd_data = m->data;
278 memcpy(&id, m->id.id, sizeof(id));
280 printk("%s: %02x.%012llx.%02x: type=%02x, len=%u.\n",
281 __func__, id.family, (unsigned long long)id.id, id.crc, m->type, m->len);
283 if (m->len + sizeof(struct w1_netlink_msg) > msg->len) {
288 if (m->type == W1_MASTER_CMD) {
289 dev = w1_search_master_id(m->id.mst.id);
290 } else if (m->type == W1_SLAVE_CMD) {
291 sl = w1_search_slave(&id);
295 err = w1_process_command_root(msg, m);
308 mutex_lock(&dev->mutex);
310 if (sl && w1_reset_select_slave(sl)) {
316 cmd = (struct w1_netlink_cmd *)cmd_data;
318 if (cmd->len + sizeof(struct w1_netlink_cmd) > mlen) {
324 w1_process_command_slave(sl, msg, m, cmd);
326 w1_process_command_master(dev, msg, m, cmd);
328 cmd_data += cmd->len + sizeof(struct w1_netlink_cmd);
329 mlen -= cmd->len + sizeof(struct w1_netlink_cmd);
332 atomic_dec(&dev->refcnt);
334 atomic_dec(&sl->refcnt);
335 mutex_unlock(&dev->mutex);
337 msg->len -= sizeof(struct w1_netlink_msg) + m->len;
338 m = (struct w1_netlink_msg *)(((u8 *)m) + sizeof(struct w1_netlink_msg) + m->len);
341 * Let's allow requests for nonexisting devices.
348 int w1_init_netlink(void)
350 struct cb_id w1_id = {.idx = CN_W1_IDX, .val = CN_W1_VAL};
352 return cn_add_callback(&w1_id, "w1", &w1_cn_callback);
355 void w1_fini_netlink(void)
357 struct cb_id w1_id = {.idx = CN_W1_IDX, .val = CN_W1_VAL};
359 cn_del_callback(&w1_id);
362 void w1_netlink_send(struct w1_master *dev, struct w1_netlink_msg *msg)
366 int w1_init_netlink(void)
371 void w1_fini_netlink(void)