Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * w1_netlink.c | |
3 | * | |
a8018766 | 4 | * Copyright (c) 2003 Evgeniy Polyakov <zbr@ioremap.net> |
1da177e4 LT |
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 | ||
5a0e3ad6 | 22 | #include <linux/slab.h> |
1da177e4 LT |
23 | #include <linux/skbuff.h> |
24 | #include <linux/netlink.h> | |
12003375 | 25 | #include <linux/connector.h> |
1da177e4 LT |
26 | |
27 | #include "w1.h" | |
28 | #include "w1_log.h" | |
29 | #include "w1_netlink.h" | |
30 | ||
46e07f6e | 31 | #if defined(CONFIG_W1_CON) && (defined(CONFIG_CONNECTOR) || (defined(CONFIG_CONNECTOR_MODULE) && defined(CONFIG_W1_MODULE))) |
1da177e4 LT |
32 | void w1_netlink_send(struct w1_master *dev, struct w1_netlink_msg *msg) |
33 | { | |
12003375 EP |
34 | 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); | |
1da177e4 | 37 | |
12003375 | 38 | memset(buf, 0, sizeof(buf)); |
1da177e4 | 39 | |
12003375 EP |
40 | m->id.idx = CN_W1_IDX; |
41 | m->id.val = CN_W1_VAL; | |
1da177e4 | 42 | |
12003375 EP |
43 | m->seq = dev->seq++; |
44 | m->len = sizeof(struct w1_netlink_msg); | |
1da177e4 | 45 | |
12003375 | 46 | memcpy(w, msg, sizeof(struct w1_netlink_msg)); |
1da177e4 | 47 | |
12003375 EP |
48 | cn_netlink_send(m, 0, GFP_KERNEL); |
49 | } | |
1da177e4 | 50 | |
3b838407 EP |
51 | static void w1_send_slave(struct w1_master *dev, u64 rn) |
52 | { | |
53 | 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; | |
1a8be0fe | 57 | u64 *data; |
3b838407 | 58 | |
963bb101 DF |
59 | /* update kernel slave list */ |
60 | w1_slave_found(dev, rn); | |
61 | ||
3b838407 EP |
62 | avail = dev->priv_size - cmd->len; |
63 | ||
1a8be0fe DF |
64 | if (avail < 8) { |
65 | msg->ack++; | |
66 | cn_netlink_send(msg, 0, GFP_KERNEL); | |
3b838407 | 67 | |
1a8be0fe DF |
68 | msg->len = sizeof(struct w1_netlink_msg) + |
69 | sizeof(struct w1_netlink_cmd); | |
70 | hdr->len = sizeof(struct w1_netlink_cmd); | |
71 | cmd->len = 0; | |
3b838407 EP |
72 | } |
73 | ||
1a8be0fe | 74 | data = (void *)(cmd + 1) + cmd->len; |
3b838407 | 75 | |
1a8be0fe DF |
76 | *data = rn; |
77 | cmd->len += 8; | |
78 | hdr->len += 8; | |
79 | msg->len += 8; | |
3b838407 EP |
80 | } |
81 | ||
82 | static int w1_process_search_command(struct w1_master *dev, struct cn_msg *msg, | |
83 | unsigned int avail) | |
12003375 | 84 | { |
3b838407 EP |
85 | struct w1_netlink_msg *hdr = (struct w1_netlink_msg *)(msg + 1); |
86 | struct w1_netlink_cmd *cmd = (struct w1_netlink_cmd *)(hdr + 1); | |
87 | int search_type = (cmd->cmd == W1_CMD_ALARM_SEARCH)?W1_ALARM_SEARCH:W1_SEARCH; | |
1da177e4 | 88 | |
3b838407 EP |
89 | dev->priv = msg; |
90 | dev->priv_size = avail; | |
91 | ||
963bb101 | 92 | w1_search_process_cb(dev, search_type, w1_send_slave); |
3b838407 EP |
93 | |
94 | msg->ack = 0; | |
95 | cn_netlink_send(msg, 0, GFP_KERNEL); | |
96 | ||
97 | dev->priv = NULL; | |
98 | dev->priv_size = 0; | |
1da177e4 | 99 | |
12003375 | 100 | return 0; |
1da177e4 | 101 | } |
2d833179 | 102 | |
c7e26631 EP |
103 | static int w1_send_read_reply(struct cn_msg *msg, struct w1_netlink_msg *hdr, |
104 | struct w1_netlink_cmd *cmd) | |
2d833179 | 105 | { |
12003375 EP |
106 | void *data; |
107 | struct w1_netlink_msg *h; | |
108 | struct w1_netlink_cmd *c; | |
109 | struct cn_msg *cm; | |
110 | int err; | |
111 | ||
112 | data = kzalloc(sizeof(struct cn_msg) + | |
113 | sizeof(struct w1_netlink_msg) + | |
114 | sizeof(struct w1_netlink_cmd) + | |
115 | cmd->len, GFP_KERNEL); | |
116 | if (!data) | |
117 | return -ENOMEM; | |
118 | ||
119 | cm = (struct cn_msg *)(data); | |
120 | h = (struct w1_netlink_msg *)(cm + 1); | |
121 | c = (struct w1_netlink_cmd *)(h + 1); | |
122 | ||
123 | memcpy(cm, msg, sizeof(struct cn_msg)); | |
124 | memcpy(h, hdr, sizeof(struct w1_netlink_msg)); | |
125 | memcpy(c, cmd, sizeof(struct w1_netlink_cmd)); | |
126 | ||
127 | cm->ack = msg->seq+1; | |
c7e26631 EP |
128 | cm->len = sizeof(struct w1_netlink_msg) + |
129 | sizeof(struct w1_netlink_cmd) + cmd->len; | |
12003375 EP |
130 | |
131 | h->len = sizeof(struct w1_netlink_cmd) + cmd->len; | |
132 | ||
133 | memcpy(c->data, cmd->data, c->len); | |
134 | ||
135 | err = cn_netlink_send(cm, 0, GFP_KERNEL); | |
136 | ||
137 | kfree(data); | |
138 | ||
139 | return err; | |
140 | } | |
141 | ||
c7e26631 | 142 | static int w1_process_command_io(struct w1_master *dev, struct cn_msg *msg, |
12003375 EP |
143 | struct w1_netlink_msg *hdr, struct w1_netlink_cmd *cmd) |
144 | { | |
145 | int err = 0; | |
146 | ||
c7e26631 EP |
147 | switch (cmd->cmd) { |
148 | case W1_CMD_TOUCH: | |
149 | w1_touch_block(dev, cmd->data, cmd->len); | |
150 | w1_send_read_reply(msg, hdr, cmd); | |
151 | break; | |
152 | case W1_CMD_READ: | |
153 | w1_read_block(dev, cmd->data, cmd->len); | |
154 | w1_send_read_reply(msg, hdr, cmd); | |
155 | break; | |
156 | case W1_CMD_WRITE: | |
157 | w1_write_block(dev, cmd->data, cmd->len); | |
158 | break; | |
159 | default: | |
4037014e | 160 | err = -EINVAL; |
c7e26631 EP |
161 | break; |
162 | } | |
163 | ||
164 | return err; | |
165 | } | |
166 | ||
167 | static int w1_process_command_master(struct w1_master *dev, struct cn_msg *req_msg, | |
168 | struct w1_netlink_msg *req_hdr, struct w1_netlink_cmd *req_cmd) | |
169 | { | |
170 | int err = -EINVAL; | |
171 | struct cn_msg *msg; | |
172 | struct w1_netlink_msg *hdr; | |
173 | struct w1_netlink_cmd *cmd; | |
174 | ||
175 | msg = kzalloc(PAGE_SIZE, GFP_KERNEL); | |
176 | if (!msg) | |
177 | return -ENOMEM; | |
178 | ||
179 | msg->id = req_msg->id; | |
180 | msg->seq = req_msg->seq; | |
181 | msg->ack = 0; | |
182 | msg->len = sizeof(struct w1_netlink_msg) + sizeof(struct w1_netlink_cmd); | |
183 | ||
184 | hdr = (struct w1_netlink_msg *)(msg + 1); | |
185 | cmd = (struct w1_netlink_cmd *)(hdr + 1); | |
186 | ||
187 | hdr->type = W1_MASTER_CMD; | |
188 | hdr->id = req_hdr->id; | |
189 | hdr->len = sizeof(struct w1_netlink_cmd); | |
190 | ||
191 | cmd->cmd = req_cmd->cmd; | |
192 | cmd->len = 0; | |
12003375 EP |
193 | |
194 | switch (cmd->cmd) { | |
c7e26631 EP |
195 | case W1_CMD_SEARCH: |
196 | case W1_CMD_ALARM_SEARCH: | |
197 | err = w1_process_search_command(dev, msg, | |
198 | PAGE_SIZE - msg->len - sizeof(struct cn_msg)); | |
199 | break; | |
200 | case W1_CMD_READ: | |
201 | case W1_CMD_WRITE: | |
202 | case W1_CMD_TOUCH: | |
4037014e | 203 | err = w1_process_command_io(dev, req_msg, req_hdr, req_cmd); |
c7e26631 | 204 | break; |
f89735c4 EP |
205 | case W1_CMD_RESET: |
206 | err = w1_reset_bus(dev); | |
207 | break; | |
c7e26631 | 208 | default: |
4037014e | 209 | err = -EINVAL; |
c7e26631 | 210 | break; |
2d833179 EP |
211 | } |
212 | ||
c7e26631 | 213 | kfree(msg); |
12003375 EP |
214 | return err; |
215 | } | |
216 | ||
c7e26631 EP |
217 | static int w1_process_command_slave(struct w1_slave *sl, struct cn_msg *msg, |
218 | struct w1_netlink_msg *hdr, struct w1_netlink_cmd *cmd) | |
219 | { | |
220 | dev_dbg(&sl->master->dev, "%s: %02x.%012llx.%02x: cmd=%02x, len=%u.\n", | |
221 | __func__, sl->reg_num.family, (unsigned long long)sl->reg_num.id, | |
222 | sl->reg_num.crc, cmd->cmd, cmd->len); | |
223 | ||
224 | return w1_process_command_io(sl->master, msg, hdr, cmd); | |
225 | } | |
226 | ||
610705e7 EP |
227 | static int w1_process_command_root(struct cn_msg *msg, struct w1_netlink_msg *mcmd) |
228 | { | |
229 | struct w1_master *m; | |
230 | struct cn_msg *cn; | |
231 | struct w1_netlink_msg *w; | |
232 | u32 *id; | |
233 | ||
234 | if (mcmd->type != W1_LIST_MASTERS) { | |
235 | printk(KERN_NOTICE "%s: msg: %x.%x, wrong type: %u, len: %u.\n", | |
c7e26631 | 236 | __func__, msg->id.idx, msg->id.val, mcmd->type, mcmd->len); |
610705e7 EP |
237 | return -EPROTO; |
238 | } | |
239 | ||
240 | cn = kmalloc(PAGE_SIZE, GFP_KERNEL); | |
241 | if (!cn) | |
242 | return -ENOMEM; | |
243 | ||
244 | cn->id.idx = CN_W1_IDX; | |
245 | cn->id.val = CN_W1_VAL; | |
246 | ||
247 | cn->seq = msg->seq; | |
248 | cn->ack = 1; | |
249 | cn->len = sizeof(struct w1_netlink_msg); | |
250 | w = (struct w1_netlink_msg *)(cn + 1); | |
251 | ||
252 | w->type = W1_LIST_MASTERS; | |
4037014e | 253 | w->status = 0; |
610705e7 EP |
254 | w->len = 0; |
255 | id = (u32 *)(w + 1); | |
256 | ||
257 | mutex_lock(&w1_mlock); | |
258 | list_for_each_entry(m, &w1_masters, w1_master_entry) { | |
259 | if (cn->len + sizeof(*id) > PAGE_SIZE - sizeof(struct cn_msg)) { | |
260 | cn_netlink_send(cn, 0, GFP_KERNEL); | |
261 | cn->ack++; | |
262 | cn->len = sizeof(struct w1_netlink_msg); | |
263 | w->len = 0; | |
264 | id = (u32 *)(w + 1); | |
265 | } | |
266 | ||
267 | *id = m->id; | |
268 | w->len += sizeof(*id); | |
269 | cn->len += sizeof(*id); | |
270 | id++; | |
271 | } | |
272 | cn->ack = 0; | |
273 | cn_netlink_send(cn, 0, GFP_KERNEL); | |
274 | mutex_unlock(&w1_mlock); | |
275 | ||
276 | kfree(cn); | |
277 | return 0; | |
278 | } | |
279 | ||
4037014e EP |
280 | static int w1_netlink_send_error(struct cn_msg *rcmsg, struct w1_netlink_msg *rmsg, |
281 | struct w1_netlink_cmd *rcmd, int error) | |
282 | { | |
283 | struct cn_msg *cmsg; | |
284 | struct w1_netlink_msg *msg; | |
285 | struct w1_netlink_cmd *cmd; | |
286 | ||
287 | cmsg = kzalloc(sizeof(*msg) + sizeof(*cmd) + sizeof(*cmsg), GFP_KERNEL); | |
288 | if (!cmsg) | |
289 | return -ENOMEM; | |
290 | ||
291 | msg = (struct w1_netlink_msg *)(cmsg + 1); | |
292 | cmd = (struct w1_netlink_cmd *)(msg + 1); | |
293 | ||
294 | memcpy(cmsg, rcmsg, sizeof(*cmsg)); | |
295 | cmsg->len = sizeof(*msg); | |
296 | ||
297 | memcpy(msg, rmsg, sizeof(*msg)); | |
298 | msg->len = 0; | |
299 | msg->status = (short)-error; | |
300 | ||
301 | if (rcmd) { | |
302 | memcpy(cmd, rcmd, sizeof(*cmd)); | |
303 | cmd->len = 0; | |
304 | msg->len += sizeof(*cmd); | |
305 | cmsg->len += sizeof(*cmd); | |
306 | } | |
307 | ||
308 | error = cn_netlink_send(cmsg, 0, GFP_KERNEL); | |
309 | kfree(cmsg); | |
310 | ||
311 | return error; | |
312 | } | |
313 | ||
7069331d | 314 | static void w1_cn_callback(struct cn_msg *msg, struct netlink_skb_parms *nsp) |
12003375 | 315 | { |
12003375 EP |
316 | struct w1_netlink_msg *m = (struct w1_netlink_msg *)(msg + 1); |
317 | struct w1_netlink_cmd *cmd; | |
318 | struct w1_slave *sl; | |
319 | struct w1_master *dev; | |
320 | int err = 0; | |
321 | ||
322 | while (msg->len && !err) { | |
323 | struct w1_reg_num id; | |
324 | u16 mlen = m->len; | |
325 | u8 *cmd_data = m->data; | |
326 | ||
327 | dev = NULL; | |
328 | sl = NULL; | |
4037014e | 329 | cmd = NULL; |
12003375 EP |
330 | |
331 | memcpy(&id, m->id.id, sizeof(id)); | |
332 | #if 0 | |
333 | printk("%s: %02x.%012llx.%02x: type=%02x, len=%u.\n", | |
334 | __func__, id.family, (unsigned long long)id.id, id.crc, m->type, m->len); | |
335 | #endif | |
336 | if (m->len + sizeof(struct w1_netlink_msg) > msg->len) { | |
337 | err = -E2BIG; | |
338 | break; | |
339 | } | |
340 | ||
12003375 EP |
341 | if (m->type == W1_MASTER_CMD) { |
342 | dev = w1_search_master_id(m->id.mst.id); | |
343 | } else if (m->type == W1_SLAVE_CMD) { | |
344 | sl = w1_search_slave(&id); | |
345 | if (sl) | |
346 | dev = sl->master; | |
610705e7 EP |
347 | } else { |
348 | err = w1_process_command_root(msg, m); | |
349 | goto out_cont; | |
12003375 EP |
350 | } |
351 | ||
352 | if (!dev) { | |
353 | err = -ENODEV; | |
354 | goto out_cont; | |
355 | } | |
356 | ||
9be62e0b EP |
357 | err = 0; |
358 | if (!mlen) | |
359 | goto out_cont; | |
360 | ||
abd52a13 | 361 | mutex_lock(&dev->mutex); |
12003375 EP |
362 | |
363 | if (sl && w1_reset_select_slave(sl)) { | |
364 | err = -ENODEV; | |
365 | goto out_up; | |
366 | } | |
367 | ||
368 | while (mlen) { | |
369 | cmd = (struct w1_netlink_cmd *)cmd_data; | |
370 | ||
371 | if (cmd->len + sizeof(struct w1_netlink_cmd) > mlen) { | |
372 | err = -E2BIG; | |
373 | break; | |
374 | } | |
375 | ||
376 | if (sl) | |
4037014e | 377 | err = w1_process_command_slave(sl, msg, m, cmd); |
12003375 | 378 | else |
4037014e EP |
379 | err = w1_process_command_master(dev, msg, m, cmd); |
380 | ||
381 | w1_netlink_send_error(msg, m, cmd, err); | |
382 | err = 0; | |
12003375 EP |
383 | |
384 | cmd_data += cmd->len + sizeof(struct w1_netlink_cmd); | |
385 | mlen -= cmd->len + sizeof(struct w1_netlink_cmd); | |
386 | } | |
387 | out_up: | |
388 | atomic_dec(&dev->refcnt); | |
389 | if (sl) | |
390 | atomic_dec(&sl->refcnt); | |
abd52a13 | 391 | mutex_unlock(&dev->mutex); |
12003375 | 392 | out_cont: |
4037014e EP |
393 | if (!cmd || err) |
394 | w1_netlink_send_error(msg, m, cmd, err); | |
12003375 EP |
395 | msg->len -= sizeof(struct w1_netlink_msg) + m->len; |
396 | m = (struct w1_netlink_msg *)(((u8 *)m) + sizeof(struct w1_netlink_msg) + m->len); | |
397 | ||
398 | /* | |
399 | * Let's allow requests for nonexisting devices. | |
400 | */ | |
401 | if (err == -ENODEV) | |
402 | err = 0; | |
403 | } | |
2d833179 EP |
404 | } |
405 | ||
12003375 | 406 | int w1_init_netlink(void) |
2d833179 | 407 | { |
12003375 EP |
408 | struct cb_id w1_id = {.idx = CN_W1_IDX, .val = CN_W1_VAL}; |
409 | ||
410 | return cn_add_callback(&w1_id, "w1", &w1_cn_callback); | |
411 | } | |
412 | ||
413 | void w1_fini_netlink(void) | |
414 | { | |
415 | struct cb_id w1_id = {.idx = CN_W1_IDX, .val = CN_W1_VAL}; | |
416 | ||
417 | cn_del_callback(&w1_id); | |
2d833179 | 418 | } |
1da177e4 | 419 | #else |
1da177e4 LT |
420 | void w1_netlink_send(struct w1_master *dev, struct w1_netlink_msg *msg) |
421 | { | |
422 | } | |
2d833179 | 423 | |
12003375 | 424 | int w1_init_netlink(void) |
2d833179 EP |
425 | { |
426 | return 0; | |
427 | } | |
428 | ||
12003375 | 429 | void w1_fini_netlink(void) |
2d833179 EP |
430 | { |
431 | } | |
1da177e4 | 432 | #endif |