w1: add touch block command
[GitHub/LineageOS/android_kernel_samsung_universal7580.git] / drivers / w1 / w1_netlink.c
CommitLineData
1da177e4
LT
1/*
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>
12003375 24#include <linux/connector.h>
1da177e4
LT
25
26#include "w1.h"
27#include "w1_log.h"
28#include "w1_netlink.h"
29
46e07f6e 30#if defined(CONFIG_W1_CON) && (defined(CONFIG_CONNECTOR) || (defined(CONFIG_CONNECTOR_MODULE) && defined(CONFIG_W1_MODULE)))
1da177e4
LT
31void w1_netlink_send(struct w1_master *dev, struct w1_netlink_msg *msg)
32{
12003375
EP
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);
1da177e4 36
12003375 37 memset(buf, 0, sizeof(buf));
1da177e4 38
12003375
EP
39 m->id.idx = CN_W1_IDX;
40 m->id.val = CN_W1_VAL;
1da177e4 41
12003375
EP
42 m->seq = dev->seq++;
43 m->len = sizeof(struct w1_netlink_msg);
1da177e4 44
12003375 45 memcpy(w, msg, sizeof(struct w1_netlink_msg));
1da177e4 46
12003375
EP
47 cn_netlink_send(m, 0, GFP_KERNEL);
48}
1da177e4 49
12003375
EP
50static int w1_process_command_master(struct w1_master *dev, struct cn_msg *msg,
51 struct w1_netlink_msg *hdr, struct w1_netlink_cmd *cmd)
52{
53 dev_dbg(&dev->dev, "%s: %s: cmd=%02x, len=%u.\n",
54 __func__, dev->name, cmd->cmd, cmd->len);
1da177e4 55
12003375
EP
56 if (cmd->cmd != W1_CMD_SEARCH && cmd->cmd != W1_CMD_ALARM_SEARCH)
57 return -EINVAL;
1da177e4 58
12003375
EP
59 w1_search_process(dev, (cmd->cmd == W1_CMD_ALARM_SEARCH)?W1_ALARM_SEARCH:W1_SEARCH);
60 return 0;
1da177e4 61}
2d833179 62
12003375
EP
63static int w1_send_read_reply(struct w1_slave *sl, struct cn_msg *msg,
64 struct w1_netlink_msg *hdr, struct w1_netlink_cmd *cmd)
2d833179 65{
12003375
EP
66 void *data;
67 struct w1_netlink_msg *h;
68 struct w1_netlink_cmd *c;
69 struct cn_msg *cm;
70 int err;
71
72 data = kzalloc(sizeof(struct cn_msg) +
73 sizeof(struct w1_netlink_msg) +
74 sizeof(struct w1_netlink_cmd) +
75 cmd->len, GFP_KERNEL);
76 if (!data)
77 return -ENOMEM;
78
79 cm = (struct cn_msg *)(data);
80 h = (struct w1_netlink_msg *)(cm + 1);
81 c = (struct w1_netlink_cmd *)(h + 1);
82
83 memcpy(cm, msg, sizeof(struct cn_msg));
84 memcpy(h, hdr, sizeof(struct w1_netlink_msg));
85 memcpy(c, cmd, sizeof(struct w1_netlink_cmd));
86
87 cm->ack = msg->seq+1;
88 cm->len = sizeof(struct w1_netlink_msg) + sizeof(struct w1_netlink_cmd) + cmd->len;
89
90 h->len = sizeof(struct w1_netlink_cmd) + cmd->len;
91
92 memcpy(c->data, cmd->data, c->len);
93
94 err = cn_netlink_send(cm, 0, GFP_KERNEL);
95
96 kfree(data);
97
98 return err;
99}
100
101static int w1_process_command_slave(struct w1_slave *sl, struct cn_msg *msg,
102 struct w1_netlink_msg *hdr, struct w1_netlink_cmd *cmd)
103{
104 int err = 0;
105
106 dev_dbg(&sl->master->dev, "%s: %02x.%012llx.%02x: cmd=%02x, len=%u.\n",
107 __func__, sl->reg_num.family, (unsigned long long)sl->reg_num.id, sl->reg_num.crc,
108 cmd->cmd, cmd->len);
109
110 switch (cmd->cmd) {
9be62e0b
EP
111 case W1_CMD_TOUCH:
112 w1_touch_block(sl->master, cmd->data, cmd->len);
113 w1_send_read_reply(sl, msg, hdr, cmd);
114 break;
12003375
EP
115 case W1_CMD_READ:
116 w1_read_block(sl->master, cmd->data, cmd->len);
117 w1_send_read_reply(sl, msg, hdr, cmd);
118 break;
119 case W1_CMD_WRITE:
120 w1_write_block(sl->master, cmd->data, cmd->len);
121 break;
122 case W1_CMD_SEARCH:
123 case W1_CMD_ALARM_SEARCH:
124 w1_search_process(sl->master,
125 (cmd->cmd == W1_CMD_ALARM_SEARCH)?W1_ALARM_SEARCH:W1_SEARCH);
126 break;
127 default:
128 err = -1;
129 break;
2d833179
EP
130 }
131
12003375
EP
132 return err;
133}
134
610705e7
EP
135static int w1_process_command_root(struct cn_msg *msg, struct w1_netlink_msg *mcmd)
136{
137 struct w1_master *m;
138 struct cn_msg *cn;
139 struct w1_netlink_msg *w;
140 u32 *id;
141
142 if (mcmd->type != W1_LIST_MASTERS) {
143 printk(KERN_NOTICE "%s: msg: %x.%x, wrong type: %u, len: %u.\n",
144 __func__, msg->id.idx, msg->id.val, mcmd->type, mcmd->len);
145 return -EPROTO;
146 }
147
148 cn = kmalloc(PAGE_SIZE, GFP_KERNEL);
149 if (!cn)
150 return -ENOMEM;
151
152 cn->id.idx = CN_W1_IDX;
153 cn->id.val = CN_W1_VAL;
154
155 cn->seq = msg->seq;
156 cn->ack = 1;
157 cn->len = sizeof(struct w1_netlink_msg);
158 w = (struct w1_netlink_msg *)(cn + 1);
159
160 w->type = W1_LIST_MASTERS;
161 w->reserved = 0;
162 w->len = 0;
163 id = (u32 *)(w + 1);
164
165 mutex_lock(&w1_mlock);
166 list_for_each_entry(m, &w1_masters, w1_master_entry) {
167 if (cn->len + sizeof(*id) > PAGE_SIZE - sizeof(struct cn_msg)) {
168 cn_netlink_send(cn, 0, GFP_KERNEL);
169 cn->ack++;
170 cn->len = sizeof(struct w1_netlink_msg);
171 w->len = 0;
172 id = (u32 *)(w + 1);
173 }
174
175 *id = m->id;
176 w->len += sizeof(*id);
177 cn->len += sizeof(*id);
178 id++;
179 }
180 cn->ack = 0;
181 cn_netlink_send(cn, 0, GFP_KERNEL);
182 mutex_unlock(&w1_mlock);
183
184 kfree(cn);
185 return 0;
186}
187
12003375
EP
188static void w1_cn_callback(void *data)
189{
190 struct cn_msg *msg = data;
191 struct w1_netlink_msg *m = (struct w1_netlink_msg *)(msg + 1);
192 struct w1_netlink_cmd *cmd;
193 struct w1_slave *sl;
194 struct w1_master *dev;
195 int err = 0;
196
197 while (msg->len && !err) {
198 struct w1_reg_num id;
199 u16 mlen = m->len;
200 u8 *cmd_data = m->data;
201
202 dev = NULL;
203 sl = NULL;
204
205 memcpy(&id, m->id.id, sizeof(id));
206#if 0
207 printk("%s: %02x.%012llx.%02x: type=%02x, len=%u.\n",
208 __func__, id.family, (unsigned long long)id.id, id.crc, m->type, m->len);
209#endif
210 if (m->len + sizeof(struct w1_netlink_msg) > msg->len) {
211 err = -E2BIG;
212 break;
213 }
214
12003375
EP
215 if (m->type == W1_MASTER_CMD) {
216 dev = w1_search_master_id(m->id.mst.id);
217 } else if (m->type == W1_SLAVE_CMD) {
218 sl = w1_search_slave(&id);
219 if (sl)
220 dev = sl->master;
610705e7
EP
221 } else {
222 err = w1_process_command_root(msg, m);
223 goto out_cont;
12003375
EP
224 }
225
226 if (!dev) {
227 err = -ENODEV;
228 goto out_cont;
229 }
230
9be62e0b
EP
231 err = 0;
232 if (!mlen)
233 goto out_cont;
234
abd52a13 235 mutex_lock(&dev->mutex);
12003375
EP
236
237 if (sl && w1_reset_select_slave(sl)) {
238 err = -ENODEV;
239 goto out_up;
240 }
241
242 while (mlen) {
243 cmd = (struct w1_netlink_cmd *)cmd_data;
244
245 if (cmd->len + sizeof(struct w1_netlink_cmd) > mlen) {
246 err = -E2BIG;
247 break;
248 }
249
250 if (sl)
251 w1_process_command_slave(sl, msg, m, cmd);
252 else
253 w1_process_command_master(dev, msg, m, cmd);
254
255 cmd_data += cmd->len + sizeof(struct w1_netlink_cmd);
256 mlen -= cmd->len + sizeof(struct w1_netlink_cmd);
257 }
258out_up:
259 atomic_dec(&dev->refcnt);
260 if (sl)
261 atomic_dec(&sl->refcnt);
abd52a13 262 mutex_unlock(&dev->mutex);
12003375
EP
263out_cont:
264 msg->len -= sizeof(struct w1_netlink_msg) + m->len;
265 m = (struct w1_netlink_msg *)(((u8 *)m) + sizeof(struct w1_netlink_msg) + m->len);
266
267 /*
268 * Let's allow requests for nonexisting devices.
269 */
270 if (err == -ENODEV)
271 err = 0;
272 }
273#if 0
274 if (err) {
275 printk("%s: malformed message. Dropping.\n", __func__);
276 }
277#endif
2d833179
EP
278}
279
12003375 280int w1_init_netlink(void)
2d833179 281{
12003375
EP
282 struct cb_id w1_id = {.idx = CN_W1_IDX, .val = CN_W1_VAL};
283
284 return cn_add_callback(&w1_id, "w1", &w1_cn_callback);
285}
286
287void w1_fini_netlink(void)
288{
289 struct cb_id w1_id = {.idx = CN_W1_IDX, .val = CN_W1_VAL};
290
291 cn_del_callback(&w1_id);
2d833179 292}
1da177e4 293#else
1da177e4
LT
294void w1_netlink_send(struct w1_master *dev, struct w1_netlink_msg *msg)
295{
296}
2d833179 297
12003375 298int w1_init_netlink(void)
2d833179
EP
299{
300 return 0;
301}
302
12003375 303void w1_fini_netlink(void)
2d833179
EP
304{
305}
1da177e4 306#endif