blob: 8a7021577f32d2c65755cd7521df8638d7fcfab8 [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
22#include <linux/skbuff.h>
23#include <linux/netlink.h>
Evgeniy Polyakov12003372006-03-23 19:11:58 +030024#include <linux/connector.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26#include "w1.h"
27#include "w1_log.h"
28#include "w1_netlink.h"
29
Evgeniy Polyakov46e07f62006-04-03 12:01:12 +040030#if defined(CONFIG_W1_CON) && (defined(CONFIG_CONNECTOR) || (defined(CONFIG_CONNECTOR_MODULE) && defined(CONFIG_W1_MODULE)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070031void w1_netlink_send(struct w1_master *dev, struct w1_netlink_msg *msg)
32{
Evgeniy Polyakov12003372006-03-23 19:11:58 +030033 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Evgeniy Polyakov12003372006-03-23 19:11:58 +030037 memset(buf, 0, sizeof(buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Evgeniy Polyakov12003372006-03-23 19:11:58 +030039 m->id.idx = CN_W1_IDX;
40 m->id.val = CN_W1_VAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Evgeniy Polyakov12003372006-03-23 19:11:58 +030042 m->seq = dev->seq++;
43 m->len = sizeof(struct w1_netlink_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Evgeniy Polyakov12003372006-03-23 19:11:58 +030045 memcpy(w, msg, sizeof(struct w1_netlink_msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Evgeniy Polyakov12003372006-03-23 19:11:58 +030047 cn_netlink_send(m, 0, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048}
Evgeniy Polyakov2d833172005-07-27 13:10:11 +040049
Evgeniy Polyakov3b838402009-01-07 18:09:01 -080050static void w1_send_slave(struct w1_master *dev, u64 rn)
Evgeniy Polyakov2d833172005-07-27 13:10:11 +040051{
Evgeniy Polyakov3b838402009-01-07 18:09:01 -080052 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);
55 int avail;
Evgeniy Polyakov2d833172005-07-27 13:10:11 +040056
Evgeniy Polyakov3b838402009-01-07 18:09:01 -080057 avail = dev->priv_size - cmd->len;
Evgeniy Polyakov12003372006-03-23 19:11:58 +030058
Evgeniy Polyakov3b838402009-01-07 18:09:01 -080059 if (avail > 8) {
60 u64 *data = (void *)(cmd + 1) + cmd->len;
61
62 *data = rn;
63 cmd->len += 8;
64 hdr->len += 8;
65 msg->len += 8;
66 return;
67 }
68
69 msg->ack++;
70 cn_netlink_send(msg, 0, GFP_KERNEL);
71
72 msg->len = sizeof(struct w1_netlink_msg) + sizeof(struct w1_netlink_cmd);
73 hdr->len = sizeof(struct w1_netlink_cmd);
74 cmd->len = 0;
75}
76
77static int w1_process_search_command(struct w1_master *dev, struct cn_msg *msg,
78 unsigned int avail)
79{
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;
83
84 dev->priv = msg;
85 dev->priv_size = avail;
86
87 w1_search_devices(dev, search_type, w1_send_slave);
88
89 msg->ack = 0;
90 cn_netlink_send(msg, 0, GFP_KERNEL);
91
92 dev->priv = NULL;
93 dev->priv_size = 0;
94
Evgeniy Polyakov2d833172005-07-27 13:10:11 +040095 return 0;
96}
97
Evgeniy Polyakov3b838402009-01-07 18:09:01 -080098static 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)
100{
101 int err = -EINVAL;
102 struct cn_msg *msg;
103 struct w1_netlink_msg *hdr;
104 struct w1_netlink_cmd *cmd;
105
106 msg = kzalloc(PAGE_SIZE, GFP_KERNEL);
107 if (!msg)
108 return -ENOMEM;
109
110 msg->id = req_msg->id;
111 msg->seq = req_msg->seq;
112 msg->ack = 0;
113 msg->len = sizeof(struct w1_netlink_msg) + sizeof(struct w1_netlink_cmd);
114
115 hdr = (struct w1_netlink_msg *)(msg + 1);
116 cmd = (struct w1_netlink_cmd *)(hdr + 1);
117
118 hdr->type = W1_MASTER_CMD;
119 hdr->id = req_hdr->id;
120 hdr->len = sizeof(struct w1_netlink_cmd);
121
122 cmd->cmd = req_cmd->cmd;
123 cmd->len = 0;
124
125 switch (cmd->cmd) {
126 case W1_CMD_SEARCH:
127 case W1_CMD_ALARM_SEARCH:
128 err = w1_process_search_command(dev, msg,
129 PAGE_SIZE - msg->len - sizeof(struct cn_msg));
130 break;
131 default:
132 cmd->res = EINVAL;
133 cn_netlink_send(msg, 0, GFP_KERNEL);
134 break;
135 }
136
137 kfree(msg);
138 return err;
139}
140
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300141static int w1_send_read_reply(struct w1_slave *sl, struct cn_msg *msg,
142 struct w1_netlink_msg *hdr, struct w1_netlink_cmd *cmd)
Evgeniy Polyakov2d833172005-07-27 13:10:11 +0400143{
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300144 void *data;
145 struct w1_netlink_msg *h;
146 struct w1_netlink_cmd *c;
147 struct cn_msg *cm;
148 int err;
149
150 data = kzalloc(sizeof(struct cn_msg) +
151 sizeof(struct w1_netlink_msg) +
152 sizeof(struct w1_netlink_cmd) +
153 cmd->len, GFP_KERNEL);
154 if (!data)
155 return -ENOMEM;
156
157 cm = (struct cn_msg *)(data);
158 h = (struct w1_netlink_msg *)(cm + 1);
159 c = (struct w1_netlink_cmd *)(h + 1);
160
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));
164
165 cm->ack = msg->seq+1;
166 cm->len = sizeof(struct w1_netlink_msg) + sizeof(struct w1_netlink_cmd) + cmd->len;
167
168 h->len = sizeof(struct w1_netlink_cmd) + cmd->len;
169
170 memcpy(c->data, cmd->data, c->len);
171
172 err = cn_netlink_send(cm, 0, GFP_KERNEL);
173
174 kfree(data);
175
176 return err;
177}
178
179static int w1_process_command_slave(struct w1_slave *sl, struct cn_msg *msg,
180 struct w1_netlink_msg *hdr, struct w1_netlink_cmd *cmd)
181{
182 int err = 0;
183
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,
186 cmd->cmd, cmd->len);
187
188 switch (cmd->cmd) {
Evgeniy Polyakov9be62e02009-01-07 18:09:01 -0800189 case W1_CMD_TOUCH:
190 w1_touch_block(sl->master, cmd->data, cmd->len);
191 w1_send_read_reply(sl, msg, hdr, cmd);
192 break;
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300193 case W1_CMD_READ:
194 w1_read_block(sl->master, cmd->data, cmd->len);
195 w1_send_read_reply(sl, msg, hdr, cmd);
196 break;
197 case W1_CMD_WRITE:
198 w1_write_block(sl->master, cmd->data, cmd->len);
199 break;
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300200 default:
201 err = -1;
202 break;
203 }
204
205 return err;
206}
207
Evgeniy Polyakov610705e2009-01-07 18:08:59 -0800208static int w1_process_command_root(struct cn_msg *msg, struct w1_netlink_msg *mcmd)
209{
210 struct w1_master *m;
211 struct cn_msg *cn;
212 struct w1_netlink_msg *w;
213 u32 *id;
214
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);
218 return -EPROTO;
219 }
220
221 cn = kmalloc(PAGE_SIZE, GFP_KERNEL);
222 if (!cn)
223 return -ENOMEM;
224
225 cn->id.idx = CN_W1_IDX;
226 cn->id.val = CN_W1_VAL;
227
228 cn->seq = msg->seq;
229 cn->ack = 1;
230 cn->len = sizeof(struct w1_netlink_msg);
231 w = (struct w1_netlink_msg *)(cn + 1);
232
233 w->type = W1_LIST_MASTERS;
234 w->reserved = 0;
235 w->len = 0;
236 id = (u32 *)(w + 1);
237
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);
242 cn->ack++;
243 cn->len = sizeof(struct w1_netlink_msg);
244 w->len = 0;
245 id = (u32 *)(w + 1);
246 }
247
248 *id = m->id;
249 w->len += sizeof(*id);
250 cn->len += sizeof(*id);
251 id++;
252 }
253 cn->ack = 0;
254 cn_netlink_send(cn, 0, GFP_KERNEL);
255 mutex_unlock(&w1_mlock);
256
257 kfree(cn);
258 return 0;
259}
260
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300261static void w1_cn_callback(void *data)
262{
263 struct cn_msg *msg = data;
264 struct w1_netlink_msg *m = (struct w1_netlink_msg *)(msg + 1);
265 struct w1_netlink_cmd *cmd;
266 struct w1_slave *sl;
267 struct w1_master *dev;
268 int err = 0;
269
270 while (msg->len && !err) {
271 struct w1_reg_num id;
272 u16 mlen = m->len;
273 u8 *cmd_data = m->data;
274
275 dev = NULL;
276 sl = NULL;
277
278 memcpy(&id, m->id.id, sizeof(id));
279#if 0
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);
282#endif
283 if (m->len + sizeof(struct w1_netlink_msg) > msg->len) {
284 err = -E2BIG;
285 break;
286 }
287
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300288 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);
292 if (sl)
293 dev = sl->master;
Evgeniy Polyakov610705e2009-01-07 18:08:59 -0800294 } else {
295 err = w1_process_command_root(msg, m);
296 goto out_cont;
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300297 }
298
299 if (!dev) {
300 err = -ENODEV;
301 goto out_cont;
302 }
303
Evgeniy Polyakov9be62e02009-01-07 18:09:01 -0800304 err = 0;
305 if (!mlen)
306 goto out_cont;
307
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400308 mutex_lock(&dev->mutex);
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300309
310 if (sl && w1_reset_select_slave(sl)) {
311 err = -ENODEV;
312 goto out_up;
313 }
314
315 while (mlen) {
316 cmd = (struct w1_netlink_cmd *)cmd_data;
317
318 if (cmd->len + sizeof(struct w1_netlink_cmd) > mlen) {
319 err = -E2BIG;
320 break;
321 }
322
323 if (sl)
324 w1_process_command_slave(sl, msg, m, cmd);
325 else
326 w1_process_command_master(dev, msg, m, cmd);
327
328 cmd_data += cmd->len + sizeof(struct w1_netlink_cmd);
329 mlen -= cmd->len + sizeof(struct w1_netlink_cmd);
330 }
331out_up:
332 atomic_dec(&dev->refcnt);
333 if (sl)
334 atomic_dec(&sl->refcnt);
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400335 mutex_unlock(&dev->mutex);
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300336out_cont:
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);
339
340 /*
341 * Let's allow requests for nonexisting devices.
342 */
343 if (err == -ENODEV)
344 err = 0;
345 }
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300346}
347
348int w1_init_netlink(void)
349{
350 struct cb_id w1_id = {.idx = CN_W1_IDX, .val = CN_W1_VAL};
351
352 return cn_add_callback(&w1_id, "w1", &w1_cn_callback);
353}
354
355void w1_fini_netlink(void)
356{
357 struct cb_id w1_id = {.idx = CN_W1_IDX, .val = CN_W1_VAL};
358
359 cn_del_callback(&w1_id);
Evgeniy Polyakov2d833172005-07-27 13:10:11 +0400360}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362void w1_netlink_send(struct w1_master *dev, struct w1_netlink_msg *msg)
363{
364}
Evgeniy Polyakov2d833172005-07-27 13:10:11 +0400365
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300366int w1_init_netlink(void)
Evgeniy Polyakov2d833172005-07-27 13:10:11 +0400367{
368 return 0;
369}
370
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300371void w1_fini_netlink(void)
Evgeniy Polyakov2d833172005-07-27 13:10:11 +0400372{
373}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374#endif