blob: 7e667bc77ef2d02649fbe32cd69b4ab94e06e9f6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * w1_netlink.c
3 *
4 * Copyright (c) 2003 Evgeniy Polyakov <johnpol@2ka.mipt.ru>
5 *
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;
Evgeniy Polyakov2d833172005-07-27 13:10:11 +040057
Evgeniy Polyakov3b838402009-01-07 18:09:01 -080058 avail = dev->priv_size - cmd->len;
Evgeniy Polyakov12003372006-03-23 19:11:58 +030059
Evgeniy Polyakov3b838402009-01-07 18:09:01 -080060 if (avail > 8) {
61 u64 *data = (void *)(cmd + 1) + cmd->len;
62
63 *data = rn;
64 cmd->len += 8;
65 hdr->len += 8;
66 msg->len += 8;
67 return;
68 }
69
70 msg->ack++;
71 cn_netlink_send(msg, 0, GFP_KERNEL);
72
73 msg->len = sizeof(struct w1_netlink_msg) + sizeof(struct w1_netlink_cmd);
74 hdr->len = sizeof(struct w1_netlink_cmd);
75 cmd->len = 0;
76}
77
78static int w1_process_search_command(struct w1_master *dev, struct cn_msg *msg,
79 unsigned int avail)
80{
81 struct w1_netlink_msg *hdr = (struct w1_netlink_msg *)(msg + 1);
82 struct w1_netlink_cmd *cmd = (struct w1_netlink_cmd *)(hdr + 1);
83 int search_type = (cmd->cmd == W1_CMD_ALARM_SEARCH)?W1_ALARM_SEARCH:W1_SEARCH;
84
85 dev->priv = msg;
86 dev->priv_size = avail;
87
88 w1_search_devices(dev, search_type, w1_send_slave);
89
90 msg->ack = 0;
91 cn_netlink_send(msg, 0, GFP_KERNEL);
92
93 dev->priv = NULL;
94 dev->priv_size = 0;
95
Evgeniy Polyakov2d833172005-07-27 13:10:11 +040096 return 0;
97}
98
Evgeniy Polyakovc7e26632009-01-07 18:09:03 -080099static int w1_send_read_reply(struct cn_msg *msg, struct w1_netlink_msg *hdr,
100 struct w1_netlink_cmd *cmd)
101{
102 void *data;
103 struct w1_netlink_msg *h;
104 struct w1_netlink_cmd *c;
105 struct cn_msg *cm;
106 int err;
107
108 data = kzalloc(sizeof(struct cn_msg) +
109 sizeof(struct w1_netlink_msg) +
110 sizeof(struct w1_netlink_cmd) +
111 cmd->len, GFP_KERNEL);
112 if (!data)
113 return -ENOMEM;
114
115 cm = (struct cn_msg *)(data);
116 h = (struct w1_netlink_msg *)(cm + 1);
117 c = (struct w1_netlink_cmd *)(h + 1);
118
119 memcpy(cm, msg, sizeof(struct cn_msg));
120 memcpy(h, hdr, sizeof(struct w1_netlink_msg));
121 memcpy(c, cmd, sizeof(struct w1_netlink_cmd));
122
123 cm->ack = msg->seq+1;
124 cm->len = sizeof(struct w1_netlink_msg) +
125 sizeof(struct w1_netlink_cmd) + cmd->len;
126
127 h->len = sizeof(struct w1_netlink_cmd) + cmd->len;
128
129 memcpy(c->data, cmd->data, c->len);
130
131 err = cn_netlink_send(cm, 0, GFP_KERNEL);
132
133 kfree(data);
134
135 return err;
136}
137
138static int w1_process_command_io(struct w1_master *dev, struct cn_msg *msg,
139 struct w1_netlink_msg *hdr, struct w1_netlink_cmd *cmd)
140{
141 int err = 0;
142
143 switch (cmd->cmd) {
144 case W1_CMD_TOUCH:
145 w1_touch_block(dev, cmd->data, cmd->len);
146 w1_send_read_reply(msg, hdr, cmd);
147 break;
148 case W1_CMD_READ:
149 w1_read_block(dev, cmd->data, cmd->len);
150 w1_send_read_reply(msg, hdr, cmd);
151 break;
152 case W1_CMD_WRITE:
153 w1_write_block(dev, cmd->data, cmd->len);
154 break;
155 default:
Evgeniy Polyakov40370142009-01-07 18:09:05 -0800156 err = -EINVAL;
Evgeniy Polyakovc7e26632009-01-07 18:09:03 -0800157 break;
158 }
159
160 return err;
161}
162
Evgeniy Polyakov3b838402009-01-07 18:09:01 -0800163static int w1_process_command_master(struct w1_master *dev, struct cn_msg *req_msg,
164 struct w1_netlink_msg *req_hdr, struct w1_netlink_cmd *req_cmd)
165{
166 int err = -EINVAL;
167 struct cn_msg *msg;
168 struct w1_netlink_msg *hdr;
169 struct w1_netlink_cmd *cmd;
170
171 msg = kzalloc(PAGE_SIZE, GFP_KERNEL);
172 if (!msg)
173 return -ENOMEM;
174
175 msg->id = req_msg->id;
176 msg->seq = req_msg->seq;
177 msg->ack = 0;
178 msg->len = sizeof(struct w1_netlink_msg) + sizeof(struct w1_netlink_cmd);
179
180 hdr = (struct w1_netlink_msg *)(msg + 1);
181 cmd = (struct w1_netlink_cmd *)(hdr + 1);
182
183 hdr->type = W1_MASTER_CMD;
184 hdr->id = req_hdr->id;
185 hdr->len = sizeof(struct w1_netlink_cmd);
186
187 cmd->cmd = req_cmd->cmd;
188 cmd->len = 0;
189
190 switch (cmd->cmd) {
Evgeniy Polyakovc7e26632009-01-07 18:09:03 -0800191 case W1_CMD_SEARCH:
192 case W1_CMD_ALARM_SEARCH:
193 err = w1_process_search_command(dev, msg,
Evgeniy Polyakov3b838402009-01-07 18:09:01 -0800194 PAGE_SIZE - msg->len - sizeof(struct cn_msg));
Evgeniy Polyakovc7e26632009-01-07 18:09:03 -0800195 break;
196 case W1_CMD_READ:
197 case W1_CMD_WRITE:
198 case W1_CMD_TOUCH:
Evgeniy Polyakov40370142009-01-07 18:09:05 -0800199 err = w1_process_command_io(dev, req_msg, req_hdr, req_cmd);
Evgeniy Polyakovc7e26632009-01-07 18:09:03 -0800200 break;
Evgeniy Polyakovf89735c2009-01-07 18:09:04 -0800201 case W1_CMD_RESET:
202 err = w1_reset_bus(dev);
203 break;
Evgeniy Polyakovc7e26632009-01-07 18:09:03 -0800204 default:
Evgeniy Polyakov40370142009-01-07 18:09:05 -0800205 err = -EINVAL;
Evgeniy Polyakovc7e26632009-01-07 18:09:03 -0800206 break;
Evgeniy Polyakov3b838402009-01-07 18:09:01 -0800207 }
208
209 kfree(msg);
210 return err;
211}
212
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300213static int w1_process_command_slave(struct w1_slave *sl, struct cn_msg *msg,
214 struct w1_netlink_msg *hdr, struct w1_netlink_cmd *cmd)
215{
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300216 dev_dbg(&sl->master->dev, "%s: %02x.%012llx.%02x: cmd=%02x, len=%u.\n",
Evgeniy Polyakovc7e26632009-01-07 18:09:03 -0800217 __func__, sl->reg_num.family, (unsigned long long)sl->reg_num.id,
218 sl->reg_num.crc, cmd->cmd, cmd->len);
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300219
Evgeniy Polyakovc7e26632009-01-07 18:09:03 -0800220 return w1_process_command_io(sl->master, msg, hdr, cmd);
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300221}
222
Evgeniy Polyakov610705e2009-01-07 18:08:59 -0800223static int w1_process_command_root(struct cn_msg *msg, struct w1_netlink_msg *mcmd)
224{
225 struct w1_master *m;
226 struct cn_msg *cn;
227 struct w1_netlink_msg *w;
228 u32 *id;
229
230 if (mcmd->type != W1_LIST_MASTERS) {
231 printk(KERN_NOTICE "%s: msg: %x.%x, wrong type: %u, len: %u.\n",
Evgeniy Polyakovc7e26632009-01-07 18:09:03 -0800232 __func__, msg->id.idx, msg->id.val, mcmd->type, mcmd->len);
Evgeniy Polyakov610705e2009-01-07 18:08:59 -0800233 return -EPROTO;
234 }
235
236 cn = kmalloc(PAGE_SIZE, GFP_KERNEL);
237 if (!cn)
238 return -ENOMEM;
239
240 cn->id.idx = CN_W1_IDX;
241 cn->id.val = CN_W1_VAL;
242
243 cn->seq = msg->seq;
244 cn->ack = 1;
245 cn->len = sizeof(struct w1_netlink_msg);
246 w = (struct w1_netlink_msg *)(cn + 1);
247
248 w->type = W1_LIST_MASTERS;
Evgeniy Polyakov40370142009-01-07 18:09:05 -0800249 w->status = 0;
Evgeniy Polyakov610705e2009-01-07 18:08:59 -0800250 w->len = 0;
251 id = (u32 *)(w + 1);
252
253 mutex_lock(&w1_mlock);
254 list_for_each_entry(m, &w1_masters, w1_master_entry) {
255 if (cn->len + sizeof(*id) > PAGE_SIZE - sizeof(struct cn_msg)) {
256 cn_netlink_send(cn, 0, GFP_KERNEL);
257 cn->ack++;
258 cn->len = sizeof(struct w1_netlink_msg);
259 w->len = 0;
260 id = (u32 *)(w + 1);
261 }
262
263 *id = m->id;
264 w->len += sizeof(*id);
265 cn->len += sizeof(*id);
266 id++;
267 }
268 cn->ack = 0;
269 cn_netlink_send(cn, 0, GFP_KERNEL);
270 mutex_unlock(&w1_mlock);
271
272 kfree(cn);
273 return 0;
274}
275
Evgeniy Polyakov40370142009-01-07 18:09:05 -0800276static int w1_netlink_send_error(struct cn_msg *rcmsg, struct w1_netlink_msg *rmsg,
277 struct w1_netlink_cmd *rcmd, int error)
278{
279 struct cn_msg *cmsg;
280 struct w1_netlink_msg *msg;
281 struct w1_netlink_cmd *cmd;
282
283 cmsg = kzalloc(sizeof(*msg) + sizeof(*cmd) + sizeof(*cmsg), GFP_KERNEL);
284 if (!cmsg)
285 return -ENOMEM;
286
287 msg = (struct w1_netlink_msg *)(cmsg + 1);
288 cmd = (struct w1_netlink_cmd *)(msg + 1);
289
290 memcpy(cmsg, rcmsg, sizeof(*cmsg));
291 cmsg->len = sizeof(*msg);
292
293 memcpy(msg, rmsg, sizeof(*msg));
294 msg->len = 0;
295 msg->status = (short)-error;
296
297 if (rcmd) {
298 memcpy(cmd, rcmd, sizeof(*cmd));
299 cmd->len = 0;
300 msg->len += sizeof(*cmd);
301 cmsg->len += sizeof(*cmd);
302 }
303
304 error = cn_netlink_send(cmsg, 0, GFP_KERNEL);
305 kfree(cmsg);
306
307 return error;
308}
309
Philipp Reisner70693312009-10-02 02:40:05 +0000310static void w1_cn_callback(struct cn_msg *msg, struct netlink_skb_parms *nsp)
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300311{
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300312 struct w1_netlink_msg *m = (struct w1_netlink_msg *)(msg + 1);
313 struct w1_netlink_cmd *cmd;
314 struct w1_slave *sl;
315 struct w1_master *dev;
316 int err = 0;
317
318 while (msg->len && !err) {
319 struct w1_reg_num id;
320 u16 mlen = m->len;
321 u8 *cmd_data = m->data;
322
323 dev = NULL;
324 sl = NULL;
Evgeniy Polyakov40370142009-01-07 18:09:05 -0800325 cmd = NULL;
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300326
327 memcpy(&id, m->id.id, sizeof(id));
328#if 0
329 printk("%s: %02x.%012llx.%02x: type=%02x, len=%u.\n",
330 __func__, id.family, (unsigned long long)id.id, id.crc, m->type, m->len);
331#endif
332 if (m->len + sizeof(struct w1_netlink_msg) > msg->len) {
333 err = -E2BIG;
334 break;
335 }
336
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300337 if (m->type == W1_MASTER_CMD) {
338 dev = w1_search_master_id(m->id.mst.id);
339 } else if (m->type == W1_SLAVE_CMD) {
340 sl = w1_search_slave(&id);
341 if (sl)
342 dev = sl->master;
Evgeniy Polyakov610705e2009-01-07 18:08:59 -0800343 } else {
344 err = w1_process_command_root(msg, m);
345 goto out_cont;
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300346 }
347
348 if (!dev) {
349 err = -ENODEV;
350 goto out_cont;
351 }
352
Evgeniy Polyakov9be62e02009-01-07 18:09:01 -0800353 err = 0;
354 if (!mlen)
355 goto out_cont;
356
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400357 mutex_lock(&dev->mutex);
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300358
359 if (sl && w1_reset_select_slave(sl)) {
360 err = -ENODEV;
361 goto out_up;
362 }
363
364 while (mlen) {
365 cmd = (struct w1_netlink_cmd *)cmd_data;
366
367 if (cmd->len + sizeof(struct w1_netlink_cmd) > mlen) {
368 err = -E2BIG;
369 break;
370 }
371
372 if (sl)
Evgeniy Polyakov40370142009-01-07 18:09:05 -0800373 err = w1_process_command_slave(sl, msg, m, cmd);
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300374 else
Evgeniy Polyakov40370142009-01-07 18:09:05 -0800375 err = w1_process_command_master(dev, msg, m, cmd);
376
377 w1_netlink_send_error(msg, m, cmd, err);
378 err = 0;
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300379
380 cmd_data += cmd->len + sizeof(struct w1_netlink_cmd);
381 mlen -= cmd->len + sizeof(struct w1_netlink_cmd);
382 }
383out_up:
384 atomic_dec(&dev->refcnt);
385 if (sl)
386 atomic_dec(&sl->refcnt);
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400387 mutex_unlock(&dev->mutex);
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300388out_cont:
Evgeniy Polyakov40370142009-01-07 18:09:05 -0800389 if (!cmd || err)
390 w1_netlink_send_error(msg, m, cmd, err);
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300391 msg->len -= sizeof(struct w1_netlink_msg) + m->len;
392 m = (struct w1_netlink_msg *)(((u8 *)m) + sizeof(struct w1_netlink_msg) + m->len);
393
394 /*
395 * Let's allow requests for nonexisting devices.
396 */
397 if (err == -ENODEV)
398 err = 0;
399 }
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300400}
401
402int w1_init_netlink(void)
403{
404 struct cb_id w1_id = {.idx = CN_W1_IDX, .val = CN_W1_VAL};
405
406 return cn_add_callback(&w1_id, "w1", &w1_cn_callback);
407}
408
409void w1_fini_netlink(void)
410{
411 struct cb_id w1_id = {.idx = CN_W1_IDX, .val = CN_W1_VAL};
412
413 cn_del_callback(&w1_id);
Evgeniy Polyakov2d833172005-07-27 13:10:11 +0400414}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416void w1_netlink_send(struct w1_master *dev, struct w1_netlink_msg *msg)
417{
418}
Evgeniy Polyakov2d833172005-07-27 13:10:11 +0400419
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300420int w1_init_netlink(void)
Evgeniy Polyakov2d833172005-07-27 13:10:11 +0400421{
422 return 0;
423}
424
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300425void w1_fini_netlink(void)
Evgeniy Polyakov2d833172005-07-27 13:10:11 +0400426{
427}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428#endif