blob: 747174be7b502f164ac331786d57defc8257056c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * w1_netlink.c
3 *
Evgeniy Polyakova8018762011-08-25 15:59:06 -07004 * Copyright (c) 2003 Evgeniy Polyakov <zbr@ioremap.net>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 *
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.
11 *
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.
16 *
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
20 */
21
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/skbuff.h>
24#include <linux/netlink.h>
Evgeniy Polyakov12003372006-03-23 19:11:58 +030025#include <linux/connector.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27#include "w1.h"
28#include "w1_log.h"
29#include "w1_netlink.h"
30
Evgeniy Polyakov46e07f62006-04-03 12:01:12 +040031#if defined(CONFIG_W1_CON) && (defined(CONFIG_CONNECTOR) || (defined(CONFIG_CONNECTOR_MODULE) && defined(CONFIG_W1_MODULE)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070032void w1_netlink_send(struct w1_master *dev, struct w1_netlink_msg *msg)
33{
Evgeniy Polyakov12003372006-03-23 19:11:58 +030034 char buf[sizeof(struct cn_msg) + sizeof(struct w1_netlink_msg)];
35 struct cn_msg *m = (struct cn_msg *)buf;
36 struct w1_netlink_msg *w = (struct w1_netlink_msg *)(m+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Evgeniy Polyakov12003372006-03-23 19:11:58 +030038 memset(buf, 0, sizeof(buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Evgeniy Polyakov12003372006-03-23 19:11:58 +030040 m->id.idx = CN_W1_IDX;
41 m->id.val = CN_W1_VAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Evgeniy Polyakov12003372006-03-23 19:11:58 +030043 m->seq = dev->seq++;
44 m->len = sizeof(struct w1_netlink_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Evgeniy Polyakov12003372006-03-23 19:11:58 +030046 memcpy(w, msg, sizeof(struct w1_netlink_msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Evgeniy Polyakov12003372006-03-23 19:11:58 +030048 cn_netlink_send(m, 0, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049}
Evgeniy Polyakov2d833172005-07-27 13:10:11 +040050
Evgeniy Polyakov3b838402009-01-07 18:09:01 -080051static void w1_send_slave(struct w1_master *dev, u64 rn)
Evgeniy Polyakov2d833172005-07-27 13:10:11 +040052{
Evgeniy Polyakov3b838402009-01-07 18:09:01 -080053 struct cn_msg *msg = dev->priv;
54 struct w1_netlink_msg *hdr = (struct w1_netlink_msg *)(msg + 1);
55 struct w1_netlink_cmd *cmd = (struct w1_netlink_cmd *)(hdr + 1);
56 int avail;
David Fries6b355b32014-01-15 22:29:12 -060057 u64 *data;
Evgeniy Polyakov2d833172005-07-27 13:10:11 +040058
Evgeniy Polyakov3b838402009-01-07 18:09:01 -080059 avail = dev->priv_size - cmd->len;
Evgeniy Polyakov12003372006-03-23 19:11:58 +030060
David Fries6b355b32014-01-15 22:29:12 -060061 if (avail < 8) {
62 msg->ack++;
63 cn_netlink_send(msg, 0, GFP_KERNEL);
Evgeniy Polyakov3b838402009-01-07 18:09:01 -080064
David Fries6b355b32014-01-15 22:29:12 -060065 msg->len = sizeof(struct w1_netlink_msg) +
66 sizeof(struct w1_netlink_cmd);
67 hdr->len = sizeof(struct w1_netlink_cmd);
68 cmd->len = 0;
Evgeniy Polyakov3b838402009-01-07 18:09:01 -080069 }
70
David Fries6b355b32014-01-15 22:29:12 -060071 data = (void *)(cmd + 1) + cmd->len;
Evgeniy Polyakov3b838402009-01-07 18:09:01 -080072
David Fries6b355b32014-01-15 22:29:12 -060073 *data = rn;
74 cmd->len += 8;
75 hdr->len += 8;
76 msg->len += 8;
Evgeniy Polyakov3b838402009-01-07 18:09:01 -080077}
78
David Fries70b34d22014-01-15 22:29:17 -060079static void w1_found_send_slave(struct w1_master *dev, u64 rn)
Evgeniy Polyakov3b838402009-01-07 18:09:01 -080080{
David Fries70b34d22014-01-15 22:29:17 -060081 /* update kernel slave list */
82 w1_slave_found(dev, rn);
83
84 w1_send_slave(dev, rn);
85}
86
87/* Get the current slave list, or search (with or without alarm) */
88static int w1_get_slaves(struct w1_master *dev,
89 struct cn_msg *req_msg, struct w1_netlink_msg *req_hdr,
90 struct w1_netlink_cmd *req_cmd)
91{
92 struct cn_msg *msg;
93 struct w1_netlink_msg *hdr;
94 struct w1_netlink_cmd *cmd;
95 struct w1_slave *sl;
96
97 msg = kzalloc(PAGE_SIZE, GFP_KERNEL);
98 if (!msg)
99 return -ENOMEM;
100
101 msg->id = req_msg->id;
102 msg->seq = req_msg->seq;
103 msg->ack = 0;
104 msg->len = sizeof(struct w1_netlink_msg) +
105 sizeof(struct w1_netlink_cmd);
106
107 hdr = (struct w1_netlink_msg *)(msg + 1);
108 cmd = (struct w1_netlink_cmd *)(hdr + 1);
109
110 hdr->type = W1_MASTER_CMD;
111 hdr->id = req_hdr->id;
112 hdr->len = sizeof(struct w1_netlink_cmd);
113
114 cmd->cmd = req_cmd->cmd;
115 cmd->len = 0;
Evgeniy Polyakov3b838402009-01-07 18:09:01 -0800116
117 dev->priv = msg;
David Fries70b34d22014-01-15 22:29:17 -0600118 dev->priv_size = PAGE_SIZE - msg->len - sizeof(struct cn_msg);
Evgeniy Polyakov3b838402009-01-07 18:09:01 -0800119
David Fries70b34d22014-01-15 22:29:17 -0600120 if (req_cmd->cmd == W1_CMD_LIST_SLAVES) {
121 __u64 rn;
122 list_for_each_entry(sl, &dev->slist, w1_slave_entry) {
123 memcpy(&rn, &sl->reg_num, sizeof(rn));
124 w1_send_slave(dev, rn);
125 }
126 } else {
127 w1_search_process_cb(dev, cmd->cmd == W1_CMD_ALARM_SEARCH ?
128 W1_ALARM_SEARCH : W1_SEARCH, w1_found_send_slave);
129 }
Evgeniy Polyakov3b838402009-01-07 18:09:01 -0800130
131 msg->ack = 0;
132 cn_netlink_send(msg, 0, GFP_KERNEL);
133
134 dev->priv = NULL;
135 dev->priv_size = 0;
136
David Fries70b34d22014-01-15 22:29:17 -0600137 kfree(msg);
138
Evgeniy Polyakov2d833172005-07-27 13:10:11 +0400139 return 0;
140}
141
Evgeniy Polyakovc7e26632009-01-07 18:09:03 -0800142static int w1_send_read_reply(struct cn_msg *msg, struct w1_netlink_msg *hdr,
143 struct w1_netlink_cmd *cmd)
144{
145 void *data;
146 struct w1_netlink_msg *h;
147 struct w1_netlink_cmd *c;
148 struct cn_msg *cm;
149 int err;
150
151 data = kzalloc(sizeof(struct cn_msg) +
152 sizeof(struct w1_netlink_msg) +
153 sizeof(struct w1_netlink_cmd) +
154 cmd->len, GFP_KERNEL);
155 if (!data)
156 return -ENOMEM;
157
158 cm = (struct cn_msg *)(data);
159 h = (struct w1_netlink_msg *)(cm + 1);
160 c = (struct w1_netlink_cmd *)(h + 1);
161
162 memcpy(cm, msg, sizeof(struct cn_msg));
163 memcpy(h, hdr, sizeof(struct w1_netlink_msg));
164 memcpy(c, cmd, sizeof(struct w1_netlink_cmd));
165
166 cm->ack = msg->seq+1;
167 cm->len = sizeof(struct w1_netlink_msg) +
168 sizeof(struct w1_netlink_cmd) + cmd->len;
169
170 h->len = sizeof(struct w1_netlink_cmd) + cmd->len;
171
172 memcpy(c->data, cmd->data, c->len);
173
174 err = cn_netlink_send(cm, 0, GFP_KERNEL);
175
176 kfree(data);
177
178 return err;
179}
180
181static int w1_process_command_io(struct w1_master *dev, struct cn_msg *msg,
182 struct w1_netlink_msg *hdr, struct w1_netlink_cmd *cmd)
183{
184 int err = 0;
185
186 switch (cmd->cmd) {
187 case W1_CMD_TOUCH:
188 w1_touch_block(dev, cmd->data, cmd->len);
189 w1_send_read_reply(msg, hdr, cmd);
190 break;
191 case W1_CMD_READ:
192 w1_read_block(dev, cmd->data, cmd->len);
193 w1_send_read_reply(msg, hdr, cmd);
194 break;
195 case W1_CMD_WRITE:
196 w1_write_block(dev, cmd->data, cmd->len);
197 break;
198 default:
Evgeniy Polyakov40370142009-01-07 18:09:05 -0800199 err = -EINVAL;
Evgeniy Polyakovc7e26632009-01-07 18:09:03 -0800200 break;
201 }
202
203 return err;
204}
205
David Fries70b34d22014-01-15 22:29:17 -0600206static int w1_process_command_addremove(struct w1_master *dev,
207 struct cn_msg *msg, struct w1_netlink_msg *hdr,
208 struct w1_netlink_cmd *cmd)
209{
210 struct w1_slave *sl;
211 int err = 0;
212 struct w1_reg_num *id;
213
214 if (cmd->len != 8)
215 return -EINVAL;
216
217 id = (struct w1_reg_num *)cmd->data;
218
219 sl = w1_slave_search_device(dev, id);
220 switch (cmd->cmd) {
221 case W1_CMD_SLAVE_ADD:
222 if (sl)
223 err = -EINVAL;
224 else
225 err = w1_attach_slave_device(dev, id);
226 break;
227 case W1_CMD_SLAVE_REMOVE:
228 if (sl)
229 w1_slave_detach(sl);
230 else
231 err = -EINVAL;
232 break;
233 default:
234 err = -EINVAL;
235 break;
236 }
237
238 return err;
239}
240
241static int w1_process_command_master(struct w1_master *dev,
242 struct cn_msg *req_msg, struct w1_netlink_msg *req_hdr,
243 struct w1_netlink_cmd *req_cmd)
Evgeniy Polyakov3b838402009-01-07 18:09:01 -0800244{
245 int err = -EINVAL;
Evgeniy Polyakov3b838402009-01-07 18:09:01 -0800246
David Fries70b34d22014-01-15 22:29:17 -0600247 switch (req_cmd->cmd) {
Evgeniy Polyakovc7e26632009-01-07 18:09:03 -0800248 case W1_CMD_SEARCH:
249 case W1_CMD_ALARM_SEARCH:
David Fries70b34d22014-01-15 22:29:17 -0600250 case W1_CMD_LIST_SLAVES:
251 err = w1_get_slaves(dev, req_msg, req_hdr, req_cmd);
Evgeniy Polyakovc7e26632009-01-07 18:09:03 -0800252 break;
253 case W1_CMD_READ:
254 case W1_CMD_WRITE:
255 case W1_CMD_TOUCH:
Evgeniy Polyakov40370142009-01-07 18:09:05 -0800256 err = w1_process_command_io(dev, req_msg, req_hdr, req_cmd);
Evgeniy Polyakovc7e26632009-01-07 18:09:03 -0800257 break;
Evgeniy Polyakovf89735c2009-01-07 18:09:04 -0800258 case W1_CMD_RESET:
259 err = w1_reset_bus(dev);
260 break;
David Fries70b34d22014-01-15 22:29:17 -0600261 case W1_CMD_SLAVE_ADD:
262 case W1_CMD_SLAVE_REMOVE:
263 err = w1_process_command_addremove(dev, req_msg, req_hdr,
264 req_cmd);
265 break;
Evgeniy Polyakovc7e26632009-01-07 18:09:03 -0800266 default:
Evgeniy Polyakov40370142009-01-07 18:09:05 -0800267 err = -EINVAL;
Evgeniy Polyakovc7e26632009-01-07 18:09:03 -0800268 break;
Evgeniy Polyakov3b838402009-01-07 18:09:01 -0800269 }
270
Evgeniy Polyakov3b838402009-01-07 18:09:01 -0800271 return err;
272}
273
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300274static int w1_process_command_slave(struct w1_slave *sl, struct cn_msg *msg,
275 struct w1_netlink_msg *hdr, struct w1_netlink_cmd *cmd)
276{
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300277 dev_dbg(&sl->master->dev, "%s: %02x.%012llx.%02x: cmd=%02x, len=%u.\n",
Evgeniy Polyakovc7e26632009-01-07 18:09:03 -0800278 __func__, sl->reg_num.family, (unsigned long long)sl->reg_num.id,
279 sl->reg_num.crc, cmd->cmd, cmd->len);
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300280
Evgeniy Polyakovc7e26632009-01-07 18:09:03 -0800281 return w1_process_command_io(sl->master, msg, hdr, cmd);
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300282}
283
Evgeniy Polyakov610705e2009-01-07 18:08:59 -0800284static int w1_process_command_root(struct cn_msg *msg, struct w1_netlink_msg *mcmd)
285{
286 struct w1_master *m;
287 struct cn_msg *cn;
288 struct w1_netlink_msg *w;
289 u32 *id;
290
291 if (mcmd->type != W1_LIST_MASTERS) {
292 printk(KERN_NOTICE "%s: msg: %x.%x, wrong type: %u, len: %u.\n",
Evgeniy Polyakovc7e26632009-01-07 18:09:03 -0800293 __func__, msg->id.idx, msg->id.val, mcmd->type, mcmd->len);
Evgeniy Polyakov610705e2009-01-07 18:08:59 -0800294 return -EPROTO;
295 }
296
297 cn = kmalloc(PAGE_SIZE, GFP_KERNEL);
298 if (!cn)
299 return -ENOMEM;
300
301 cn->id.idx = CN_W1_IDX;
302 cn->id.val = CN_W1_VAL;
303
304 cn->seq = msg->seq;
305 cn->ack = 1;
306 cn->len = sizeof(struct w1_netlink_msg);
307 w = (struct w1_netlink_msg *)(cn + 1);
308
309 w->type = W1_LIST_MASTERS;
Evgeniy Polyakov40370142009-01-07 18:09:05 -0800310 w->status = 0;
Evgeniy Polyakov610705e2009-01-07 18:08:59 -0800311 w->len = 0;
312 id = (u32 *)(w + 1);
313
314 mutex_lock(&w1_mlock);
315 list_for_each_entry(m, &w1_masters, w1_master_entry) {
316 if (cn->len + sizeof(*id) > PAGE_SIZE - sizeof(struct cn_msg)) {
317 cn_netlink_send(cn, 0, GFP_KERNEL);
318 cn->ack++;
319 cn->len = sizeof(struct w1_netlink_msg);
320 w->len = 0;
321 id = (u32 *)(w + 1);
322 }
323
324 *id = m->id;
325 w->len += sizeof(*id);
326 cn->len += sizeof(*id);
327 id++;
328 }
329 cn->ack = 0;
330 cn_netlink_send(cn, 0, GFP_KERNEL);
331 mutex_unlock(&w1_mlock);
332
333 kfree(cn);
334 return 0;
335}
336
Evgeniy Polyakov40370142009-01-07 18:09:05 -0800337static int w1_netlink_send_error(struct cn_msg *rcmsg, struct w1_netlink_msg *rmsg,
338 struct w1_netlink_cmd *rcmd, int error)
339{
340 struct cn_msg *cmsg;
341 struct w1_netlink_msg *msg;
342 struct w1_netlink_cmd *cmd;
343
344 cmsg = kzalloc(sizeof(*msg) + sizeof(*cmd) + sizeof(*cmsg), GFP_KERNEL);
345 if (!cmsg)
346 return -ENOMEM;
347
348 msg = (struct w1_netlink_msg *)(cmsg + 1);
349 cmd = (struct w1_netlink_cmd *)(msg + 1);
350
351 memcpy(cmsg, rcmsg, sizeof(*cmsg));
352 cmsg->len = sizeof(*msg);
353
354 memcpy(msg, rmsg, sizeof(*msg));
355 msg->len = 0;
356 msg->status = (short)-error;
357
358 if (rcmd) {
359 memcpy(cmd, rcmd, sizeof(*cmd));
360 cmd->len = 0;
361 msg->len += sizeof(*cmd);
362 cmsg->len += sizeof(*cmd);
363 }
364
365 error = cn_netlink_send(cmsg, 0, GFP_KERNEL);
366 kfree(cmsg);
367
368 return error;
369}
370
Philipp Reisner70693312009-10-02 02:40:05 +0000371static void w1_cn_callback(struct cn_msg *msg, struct netlink_skb_parms *nsp)
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300372{
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300373 struct w1_netlink_msg *m = (struct w1_netlink_msg *)(msg + 1);
374 struct w1_netlink_cmd *cmd;
375 struct w1_slave *sl;
376 struct w1_master *dev;
377 int err = 0;
378
379 while (msg->len && !err) {
380 struct w1_reg_num id;
381 u16 mlen = m->len;
382 u8 *cmd_data = m->data;
383
384 dev = NULL;
385 sl = NULL;
Evgeniy Polyakov40370142009-01-07 18:09:05 -0800386 cmd = NULL;
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300387
388 memcpy(&id, m->id.id, sizeof(id));
389#if 0
390 printk("%s: %02x.%012llx.%02x: type=%02x, len=%u.\n",
391 __func__, id.family, (unsigned long long)id.id, id.crc, m->type, m->len);
392#endif
393 if (m->len + sizeof(struct w1_netlink_msg) > msg->len) {
394 err = -E2BIG;
395 break;
396 }
397
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300398 if (m->type == W1_MASTER_CMD) {
399 dev = w1_search_master_id(m->id.mst.id);
400 } else if (m->type == W1_SLAVE_CMD) {
401 sl = w1_search_slave(&id);
402 if (sl)
403 dev = sl->master;
Evgeniy Polyakov610705e2009-01-07 18:08:59 -0800404 } else {
405 err = w1_process_command_root(msg, m);
406 goto out_cont;
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300407 }
408
409 if (!dev) {
410 err = -ENODEV;
411 goto out_cont;
412 }
413
Evgeniy Polyakov9be62e02009-01-07 18:09:01 -0800414 err = 0;
415 if (!mlen)
416 goto out_cont;
417
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400418 mutex_lock(&dev->mutex);
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300419
420 if (sl && w1_reset_select_slave(sl)) {
421 err = -ENODEV;
422 goto out_up;
423 }
424
425 while (mlen) {
426 cmd = (struct w1_netlink_cmd *)cmd_data;
427
428 if (cmd->len + sizeof(struct w1_netlink_cmd) > mlen) {
429 err = -E2BIG;
430 break;
431 }
432
433 if (sl)
Evgeniy Polyakov40370142009-01-07 18:09:05 -0800434 err = w1_process_command_slave(sl, msg, m, cmd);
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300435 else
Evgeniy Polyakov40370142009-01-07 18:09:05 -0800436 err = w1_process_command_master(dev, msg, m, cmd);
437
438 w1_netlink_send_error(msg, m, cmd, err);
439 err = 0;
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300440
441 cmd_data += cmd->len + sizeof(struct w1_netlink_cmd);
442 mlen -= cmd->len + sizeof(struct w1_netlink_cmd);
443 }
444out_up:
445 atomic_dec(&dev->refcnt);
446 if (sl)
447 atomic_dec(&sl->refcnt);
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400448 mutex_unlock(&dev->mutex);
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300449out_cont:
Evgeniy Polyakov40370142009-01-07 18:09:05 -0800450 if (!cmd || err)
451 w1_netlink_send_error(msg, m, cmd, err);
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300452 msg->len -= sizeof(struct w1_netlink_msg) + m->len;
453 m = (struct w1_netlink_msg *)(((u8 *)m) + sizeof(struct w1_netlink_msg) + m->len);
454
455 /*
456 * Let's allow requests for nonexisting devices.
457 */
458 if (err == -ENODEV)
459 err = 0;
460 }
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300461}
462
463int w1_init_netlink(void)
464{
465 struct cb_id w1_id = {.idx = CN_W1_IDX, .val = CN_W1_VAL};
466
467 return cn_add_callback(&w1_id, "w1", &w1_cn_callback);
468}
469
470void w1_fini_netlink(void)
471{
472 struct cb_id w1_id = {.idx = CN_W1_IDX, .val = CN_W1_VAL};
473
474 cn_del_callback(&w1_id);
Evgeniy Polyakov2d833172005-07-27 13:10:11 +0400475}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477void w1_netlink_send(struct w1_master *dev, struct w1_netlink_msg *msg)
478{
479}
Evgeniy Polyakov2d833172005-07-27 13:10:11 +0400480
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300481int w1_init_netlink(void)
Evgeniy Polyakov2d833172005-07-27 13:10:11 +0400482{
483 return 0;
484}
485
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300486void w1_fini_netlink(void)
Evgeniy Polyakov2d833172005-07-27 13:10:11 +0400487{
488}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489#endif