]>
Commit | Line | Data |
---|---|---|
4d12b8b1 LRV |
1 | /* |
2 | * Copyright (C) 2011 Instituto Nokia de Tecnologia | |
3 | * | |
4 | * Authors: | |
5 | * Lauro Ramos Venancio <lauro.venancio@openbossa.org> | |
6 | * Aloisio Almeida Jr <aloisio.almeida@openbossa.org> | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; either version 2 of the License, or | |
11 | * (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program; if not, write to the | |
20 | * Free Software Foundation, Inc., | |
21 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
22 | */ | |
23 | ||
52858b51 | 24 | #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__ |
20c239c1 | 25 | |
4d12b8b1 LRV |
26 | #include <net/genetlink.h> |
27 | #include <linux/nfc.h> | |
28 | #include <linux/slab.h> | |
29 | ||
30 | #include "nfc.h" | |
31 | ||
52feb444 TE |
32 | #include "llcp/llcp.h" |
33 | ||
4d12b8b1 LRV |
34 | static struct genl_multicast_group nfc_genl_event_mcgrp = { |
35 | .name = NFC_GENL_MCAST_EVENT_NAME, | |
36 | }; | |
37 | ||
e5fe4cf8 | 38 | static struct genl_family nfc_genl_family = { |
4d12b8b1 LRV |
39 | .id = GENL_ID_GENERATE, |
40 | .hdrsize = 0, | |
41 | .name = NFC_GENL_NAME, | |
42 | .version = NFC_GENL_VERSION, | |
43 | .maxattr = NFC_ATTR_MAX, | |
44 | }; | |
45 | ||
46 | static const struct nla_policy nfc_genl_policy[NFC_ATTR_MAX + 1] = { | |
47 | [NFC_ATTR_DEVICE_INDEX] = { .type = NLA_U32 }, | |
48 | [NFC_ATTR_DEVICE_NAME] = { .type = NLA_STRING, | |
49 | .len = NFC_DEVICE_NAME_MAXSIZE }, | |
50 | [NFC_ATTR_PROTOCOLS] = { .type = NLA_U32 }, | |
1ed28f61 SO |
51 | [NFC_ATTR_COMM_MODE] = { .type = NLA_U8 }, |
52 | [NFC_ATTR_RF_MODE] = { .type = NLA_U8 }, | |
c970a1ac | 53 | [NFC_ATTR_DEVICE_POWERED] = { .type = NLA_U8 }, |
fe7c5800 SO |
54 | [NFC_ATTR_IM_PROTOCOLS] = { .type = NLA_U32 }, |
55 | [NFC_ATTR_TM_PROTOCOLS] = { .type = NLA_U32 }, | |
8af362d1 TE |
56 | [NFC_ATTR_LLC_PARAM_LTO] = { .type = NLA_U8 }, |
57 | [NFC_ATTR_LLC_PARAM_RW] = { .type = NLA_U8 }, | |
58 | [NFC_ATTR_LLC_PARAM_MIUX] = { .type = NLA_U16 }, | |
4d12b8b1 LRV |
59 | }; |
60 | ||
61 | static int nfc_genl_send_target(struct sk_buff *msg, struct nfc_target *target, | |
0a40acb2 | 62 | struct netlink_callback *cb, int flags) |
4d12b8b1 LRV |
63 | { |
64 | void *hdr; | |
65 | ||
15e47304 | 66 | hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, |
0a40acb2 | 67 | &nfc_genl_family, flags, NFC_CMD_GET_TARGET); |
4d12b8b1 LRV |
68 | if (!hdr) |
69 | return -EMSGSIZE; | |
70 | ||
71 | genl_dump_check_consistent(cb, hdr, &nfc_genl_family); | |
72 | ||
1e6428d8 DM |
73 | if (nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target->idx) || |
74 | nla_put_u32(msg, NFC_ATTR_PROTOCOLS, target->supported_protocols) || | |
75 | nla_put_u16(msg, NFC_ATTR_TARGET_SENS_RES, target->sens_res) || | |
76 | nla_put_u8(msg, NFC_ATTR_TARGET_SEL_RES, target->sel_res)) | |
77 | goto nla_put_failure; | |
78 | if (target->nfcid1_len > 0 && | |
79 | nla_put(msg, NFC_ATTR_TARGET_NFCID1, target->nfcid1_len, | |
80 | target->nfcid1)) | |
81 | goto nla_put_failure; | |
82 | if (target->sensb_res_len > 0 && | |
83 | nla_put(msg, NFC_ATTR_TARGET_SENSB_RES, target->sensb_res_len, | |
84 | target->sensb_res)) | |
85 | goto nla_put_failure; | |
86 | if (target->sensf_res_len > 0 && | |
87 | nla_put(msg, NFC_ATTR_TARGET_SENSF_RES, target->sensf_res_len, | |
88 | target->sensf_res)) | |
89 | goto nla_put_failure; | |
4d12b8b1 LRV |
90 | |
91 | return genlmsg_end(msg, hdr); | |
92 | ||
93 | nla_put_failure: | |
94 | genlmsg_cancel(msg, hdr); | |
95 | return -EMSGSIZE; | |
96 | } | |
97 | ||
98 | static struct nfc_dev *__get_device_from_cb(struct netlink_callback *cb) | |
99 | { | |
100 | struct nfc_dev *dev; | |
101 | int rc; | |
102 | u32 idx; | |
103 | ||
104 | rc = nlmsg_parse(cb->nlh, GENL_HDRLEN + nfc_genl_family.hdrsize, | |
0a40acb2 SO |
105 | nfc_genl_family.attrbuf, |
106 | nfc_genl_family.maxattr, | |
107 | nfc_genl_policy); | |
4d12b8b1 LRV |
108 | if (rc < 0) |
109 | return ERR_PTR(rc); | |
110 | ||
111 | if (!nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX]) | |
112 | return ERR_PTR(-EINVAL); | |
113 | ||
114 | idx = nla_get_u32(nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX]); | |
115 | ||
116 | dev = nfc_get_device(idx); | |
117 | if (!dev) | |
118 | return ERR_PTR(-ENODEV); | |
119 | ||
120 | return dev; | |
121 | } | |
122 | ||
123 | static int nfc_genl_dump_targets(struct sk_buff *skb, | |
0a40acb2 | 124 | struct netlink_callback *cb) |
4d12b8b1 LRV |
125 | { |
126 | int i = cb->args[0]; | |
127 | struct nfc_dev *dev = (struct nfc_dev *) cb->args[1]; | |
128 | int rc; | |
129 | ||
4d12b8b1 LRV |
130 | if (!dev) { |
131 | dev = __get_device_from_cb(cb); | |
132 | if (IS_ERR(dev)) | |
133 | return PTR_ERR(dev); | |
134 | ||
135 | cb->args[1] = (long) dev; | |
136 | } | |
137 | ||
d4ccb132 | 138 | device_lock(&dev->dev); |
4d12b8b1 LRV |
139 | |
140 | cb->seq = dev->targets_generation; | |
141 | ||
142 | while (i < dev->n_targets) { | |
143 | rc = nfc_genl_send_target(skb, &dev->targets[i], cb, | |
0a40acb2 | 144 | NLM_F_MULTI); |
4d12b8b1 LRV |
145 | if (rc < 0) |
146 | break; | |
147 | ||
148 | i++; | |
149 | } | |
150 | ||
d4ccb132 | 151 | device_unlock(&dev->dev); |
4d12b8b1 LRV |
152 | |
153 | cb->args[0] = i; | |
154 | ||
155 | return skb->len; | |
156 | } | |
157 | ||
158 | static int nfc_genl_dump_targets_done(struct netlink_callback *cb) | |
159 | { | |
160 | struct nfc_dev *dev = (struct nfc_dev *) cb->args[1]; | |
161 | ||
4d12b8b1 LRV |
162 | if (dev) |
163 | nfc_put_device(dev); | |
164 | ||
165 | return 0; | |
166 | } | |
167 | ||
168 | int nfc_genl_targets_found(struct nfc_dev *dev) | |
169 | { | |
170 | struct sk_buff *msg; | |
171 | void *hdr; | |
172 | ||
15e47304 | 173 | dev->genl_data.poll_req_portid = 0; |
4d12b8b1 | 174 | |
58050fce | 175 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); |
4d12b8b1 LRV |
176 | if (!msg) |
177 | return -ENOMEM; | |
178 | ||
179 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, | |
0a40acb2 | 180 | NFC_EVENT_TARGETS_FOUND); |
4d12b8b1 LRV |
181 | if (!hdr) |
182 | goto free_msg; | |
183 | ||
1e6428d8 DM |
184 | if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx)) |
185 | goto nla_put_failure; | |
4d12b8b1 LRV |
186 | |
187 | genlmsg_end(msg, hdr); | |
188 | ||
189 | return genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC); | |
190 | ||
191 | nla_put_failure: | |
192 | genlmsg_cancel(msg, hdr); | |
193 | free_msg: | |
194 | nlmsg_free(msg); | |
195 | return -EMSGSIZE; | |
196 | } | |
197 | ||
8112a5c9 SO |
198 | int nfc_genl_target_lost(struct nfc_dev *dev, u32 target_idx) |
199 | { | |
200 | struct sk_buff *msg; | |
201 | void *hdr; | |
202 | ||
58050fce | 203 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
8112a5c9 SO |
204 | if (!msg) |
205 | return -ENOMEM; | |
206 | ||
207 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, | |
208 | NFC_EVENT_TARGET_LOST); | |
209 | if (!hdr) | |
210 | goto free_msg; | |
211 | ||
59ef43e6 JL |
212 | if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) || |
213 | nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx)) | |
214 | goto nla_put_failure; | |
8112a5c9 SO |
215 | |
216 | genlmsg_end(msg, hdr); | |
217 | ||
218 | genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL); | |
219 | ||
220 | return 0; | |
221 | ||
fc40a8c1 SO |
222 | nla_put_failure: |
223 | genlmsg_cancel(msg, hdr); | |
224 | free_msg: | |
225 | nlmsg_free(msg); | |
226 | return -EMSGSIZE; | |
227 | } | |
228 | ||
229 | int nfc_genl_tm_activated(struct nfc_dev *dev, u32 protocol) | |
230 | { | |
231 | struct sk_buff *msg; | |
232 | void *hdr; | |
233 | ||
58050fce | 234 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
fc40a8c1 SO |
235 | if (!msg) |
236 | return -ENOMEM; | |
237 | ||
238 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, | |
239 | NFC_EVENT_TM_ACTIVATED); | |
240 | if (!hdr) | |
241 | goto free_msg; | |
242 | ||
243 | if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx)) | |
244 | goto nla_put_failure; | |
245 | if (nla_put_u32(msg, NFC_ATTR_TM_PROTOCOLS, protocol)) | |
246 | goto nla_put_failure; | |
247 | ||
248 | genlmsg_end(msg, hdr); | |
249 | ||
250 | genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL); | |
251 | ||
252 | return 0; | |
253 | ||
254 | nla_put_failure: | |
255 | genlmsg_cancel(msg, hdr); | |
256 | free_msg: | |
257 | nlmsg_free(msg); | |
258 | return -EMSGSIZE; | |
259 | } | |
260 | ||
261 | int nfc_genl_tm_deactivated(struct nfc_dev *dev) | |
262 | { | |
263 | struct sk_buff *msg; | |
264 | void *hdr; | |
265 | ||
58050fce | 266 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
fc40a8c1 SO |
267 | if (!msg) |
268 | return -ENOMEM; | |
269 | ||
270 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, | |
271 | NFC_EVENT_TM_DEACTIVATED); | |
272 | if (!hdr) | |
273 | goto free_msg; | |
274 | ||
275 | if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx)) | |
276 | goto nla_put_failure; | |
277 | ||
278 | genlmsg_end(msg, hdr); | |
279 | ||
280 | genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL); | |
281 | ||
282 | return 0; | |
283 | ||
8112a5c9 SO |
284 | nla_put_failure: |
285 | genlmsg_cancel(msg, hdr); | |
286 | free_msg: | |
287 | nlmsg_free(msg); | |
288 | return -EMSGSIZE; | |
289 | } | |
290 | ||
4d12b8b1 LRV |
291 | int nfc_genl_device_added(struct nfc_dev *dev) |
292 | { | |
293 | struct sk_buff *msg; | |
294 | void *hdr; | |
295 | ||
58050fce | 296 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
4d12b8b1 LRV |
297 | if (!msg) |
298 | return -ENOMEM; | |
299 | ||
300 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, | |
0a40acb2 | 301 | NFC_EVENT_DEVICE_ADDED); |
4d12b8b1 LRV |
302 | if (!hdr) |
303 | goto free_msg; | |
304 | ||
1e6428d8 DM |
305 | if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) || |
306 | nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) || | |
307 | nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) || | |
308 | nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up)) | |
309 | goto nla_put_failure; | |
4d12b8b1 LRV |
310 | |
311 | genlmsg_end(msg, hdr); | |
312 | ||
313 | genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL); | |
314 | ||
315 | return 0; | |
316 | ||
317 | nla_put_failure: | |
318 | genlmsg_cancel(msg, hdr); | |
319 | free_msg: | |
320 | nlmsg_free(msg); | |
321 | return -EMSGSIZE; | |
322 | } | |
323 | ||
324 | int nfc_genl_device_removed(struct nfc_dev *dev) | |
325 | { | |
326 | struct sk_buff *msg; | |
327 | void *hdr; | |
328 | ||
58050fce | 329 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
4d12b8b1 LRV |
330 | if (!msg) |
331 | return -ENOMEM; | |
332 | ||
333 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, | |
0a40acb2 | 334 | NFC_EVENT_DEVICE_REMOVED); |
4d12b8b1 LRV |
335 | if (!hdr) |
336 | goto free_msg; | |
337 | ||
1e6428d8 DM |
338 | if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx)) |
339 | goto nla_put_failure; | |
4d12b8b1 LRV |
340 | |
341 | genlmsg_end(msg, hdr); | |
342 | ||
343 | genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL); | |
344 | ||
345 | return 0; | |
346 | ||
347 | nla_put_failure: | |
348 | genlmsg_cancel(msg, hdr); | |
349 | free_msg: | |
350 | nlmsg_free(msg); | |
351 | return -EMSGSIZE; | |
352 | } | |
353 | ||
354 | static int nfc_genl_send_device(struct sk_buff *msg, struct nfc_dev *dev, | |
15e47304 | 355 | u32 portid, u32 seq, |
0a40acb2 SO |
356 | struct netlink_callback *cb, |
357 | int flags) | |
4d12b8b1 LRV |
358 | { |
359 | void *hdr; | |
360 | ||
15e47304 | 361 | hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags, |
0a40acb2 | 362 | NFC_CMD_GET_DEVICE); |
4d12b8b1 LRV |
363 | if (!hdr) |
364 | return -EMSGSIZE; | |
365 | ||
366 | if (cb) | |
367 | genl_dump_check_consistent(cb, hdr, &nfc_genl_family); | |
368 | ||
1e6428d8 DM |
369 | if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) || |
370 | nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) || | |
371 | nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) || | |
390a1bd8 | 372 | nla_put_u32(msg, NFC_ATTR_SE, dev->supported_se) || |
7ad39395 TE |
373 | nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up) || |
374 | nla_put_u8(msg, NFC_ATTR_RF_MODE, dev->rf_mode)) | |
1e6428d8 | 375 | goto nla_put_failure; |
4d12b8b1 LRV |
376 | |
377 | return genlmsg_end(msg, hdr); | |
378 | ||
379 | nla_put_failure: | |
380 | genlmsg_cancel(msg, hdr); | |
381 | return -EMSGSIZE; | |
382 | } | |
383 | ||
384 | static int nfc_genl_dump_devices(struct sk_buff *skb, | |
0a40acb2 | 385 | struct netlink_callback *cb) |
4d12b8b1 LRV |
386 | { |
387 | struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0]; | |
388 | struct nfc_dev *dev = (struct nfc_dev *) cb->args[1]; | |
389 | bool first_call = false; | |
390 | ||
4d12b8b1 LRV |
391 | if (!iter) { |
392 | first_call = true; | |
393 | iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL); | |
394 | if (!iter) | |
395 | return -ENOMEM; | |
396 | cb->args[0] = (long) iter; | |
397 | } | |
398 | ||
399 | mutex_lock(&nfc_devlist_mutex); | |
400 | ||
401 | cb->seq = nfc_devlist_generation; | |
402 | ||
403 | if (first_call) { | |
404 | nfc_device_iter_init(iter); | |
405 | dev = nfc_device_iter_next(iter); | |
406 | } | |
407 | ||
408 | while (dev) { | |
409 | int rc; | |
410 | ||
15e47304 | 411 | rc = nfc_genl_send_device(skb, dev, NETLINK_CB(cb->skb).portid, |
0a40acb2 | 412 | cb->nlh->nlmsg_seq, cb, NLM_F_MULTI); |
4d12b8b1 LRV |
413 | if (rc < 0) |
414 | break; | |
415 | ||
416 | dev = nfc_device_iter_next(iter); | |
417 | } | |
418 | ||
419 | mutex_unlock(&nfc_devlist_mutex); | |
420 | ||
421 | cb->args[1] = (long) dev; | |
422 | ||
423 | return skb->len; | |
424 | } | |
425 | ||
426 | static int nfc_genl_dump_devices_done(struct netlink_callback *cb) | |
427 | { | |
428 | struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0]; | |
429 | ||
4d12b8b1 LRV |
430 | nfc_device_iter_exit(iter); |
431 | kfree(iter); | |
432 | ||
433 | return 0; | |
434 | } | |
435 | ||
1ed28f61 | 436 | int nfc_genl_dep_link_up_event(struct nfc_dev *dev, u32 target_idx, |
0a40acb2 | 437 | u8 comm_mode, u8 rf_mode) |
1ed28f61 SO |
438 | { |
439 | struct sk_buff *msg; | |
440 | void *hdr; | |
441 | ||
442 | pr_debug("DEP link is up\n"); | |
443 | ||
58050fce | 444 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); |
1ed28f61 SO |
445 | if (!msg) |
446 | return -ENOMEM; | |
447 | ||
0a40acb2 | 448 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, NFC_CMD_DEP_LINK_UP); |
1ed28f61 SO |
449 | if (!hdr) |
450 | goto free_msg; | |
451 | ||
1e6428d8 DM |
452 | if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx)) |
453 | goto nla_put_failure; | |
454 | if (rf_mode == NFC_RF_INITIATOR && | |
455 | nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx)) | |
456 | goto nla_put_failure; | |
457 | if (nla_put_u8(msg, NFC_ATTR_COMM_MODE, comm_mode) || | |
458 | nla_put_u8(msg, NFC_ATTR_RF_MODE, rf_mode)) | |
459 | goto nla_put_failure; | |
1ed28f61 SO |
460 | |
461 | genlmsg_end(msg, hdr); | |
462 | ||
463 | dev->dep_link_up = true; | |
464 | ||
465 | genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC); | |
466 | ||
467 | return 0; | |
468 | ||
469 | nla_put_failure: | |
470 | genlmsg_cancel(msg, hdr); | |
471 | free_msg: | |
472 | nlmsg_free(msg); | |
473 | return -EMSGSIZE; | |
474 | } | |
475 | ||
476 | int nfc_genl_dep_link_down_event(struct nfc_dev *dev) | |
477 | { | |
478 | struct sk_buff *msg; | |
479 | void *hdr; | |
480 | ||
481 | pr_debug("DEP link is down\n"); | |
482 | ||
58050fce | 483 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); |
1ed28f61 SO |
484 | if (!msg) |
485 | return -ENOMEM; | |
486 | ||
487 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, | |
0a40acb2 | 488 | NFC_CMD_DEP_LINK_DOWN); |
1ed28f61 SO |
489 | if (!hdr) |
490 | goto free_msg; | |
491 | ||
1e6428d8 DM |
492 | if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx)) |
493 | goto nla_put_failure; | |
1ed28f61 SO |
494 | |
495 | genlmsg_end(msg, hdr); | |
496 | ||
497 | genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC); | |
498 | ||
499 | return 0; | |
500 | ||
501 | nla_put_failure: | |
502 | genlmsg_cancel(msg, hdr); | |
503 | free_msg: | |
504 | nlmsg_free(msg); | |
505 | return -EMSGSIZE; | |
506 | } | |
507 | ||
4d12b8b1 LRV |
508 | static int nfc_genl_get_device(struct sk_buff *skb, struct genl_info *info) |
509 | { | |
510 | struct sk_buff *msg; | |
511 | struct nfc_dev *dev; | |
512 | u32 idx; | |
513 | int rc = -ENOBUFS; | |
514 | ||
4d12b8b1 LRV |
515 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) |
516 | return -EINVAL; | |
517 | ||
518 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); | |
519 | ||
520 | dev = nfc_get_device(idx); | |
521 | if (!dev) | |
522 | return -ENODEV; | |
523 | ||
58050fce | 524 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
4d12b8b1 LRV |
525 | if (!msg) { |
526 | rc = -ENOMEM; | |
527 | goto out_putdev; | |
528 | } | |
529 | ||
15e47304 | 530 | rc = nfc_genl_send_device(msg, dev, info->snd_portid, info->snd_seq, |
0a40acb2 | 531 | NULL, 0); |
4d12b8b1 LRV |
532 | if (rc < 0) |
533 | goto out_free; | |
534 | ||
535 | nfc_put_device(dev); | |
536 | ||
537 | return genlmsg_reply(msg, info); | |
538 | ||
539 | out_free: | |
540 | nlmsg_free(msg); | |
541 | out_putdev: | |
542 | nfc_put_device(dev); | |
543 | return rc; | |
544 | } | |
545 | ||
8b3fe7b5 IE |
546 | static int nfc_genl_dev_up(struct sk_buff *skb, struct genl_info *info) |
547 | { | |
548 | struct nfc_dev *dev; | |
549 | int rc; | |
550 | u32 idx; | |
551 | ||
8b3fe7b5 IE |
552 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) |
553 | return -EINVAL; | |
554 | ||
555 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); | |
556 | ||
557 | dev = nfc_get_device(idx); | |
558 | if (!dev) | |
559 | return -ENODEV; | |
560 | ||
561 | rc = nfc_dev_up(dev); | |
562 | ||
563 | nfc_put_device(dev); | |
564 | return rc; | |
565 | } | |
566 | ||
567 | static int nfc_genl_dev_down(struct sk_buff *skb, struct genl_info *info) | |
568 | { | |
569 | struct nfc_dev *dev; | |
570 | int rc; | |
571 | u32 idx; | |
572 | ||
8b3fe7b5 IE |
573 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) |
574 | return -EINVAL; | |
575 | ||
576 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); | |
577 | ||
578 | dev = nfc_get_device(idx); | |
579 | if (!dev) | |
580 | return -ENODEV; | |
581 | ||
582 | rc = nfc_dev_down(dev); | |
583 | ||
584 | nfc_put_device(dev); | |
585 | return rc; | |
586 | } | |
587 | ||
4d12b8b1 LRV |
588 | static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info) |
589 | { | |
590 | struct nfc_dev *dev; | |
591 | int rc; | |
592 | u32 idx; | |
fe7c5800 | 593 | u32 im_protocols = 0, tm_protocols = 0; |
4d12b8b1 | 594 | |
1ed28f61 SO |
595 | pr_debug("Poll start\n"); |
596 | ||
4d12b8b1 | 597 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX] || |
fe7c5800 SO |
598 | ((!info->attrs[NFC_ATTR_IM_PROTOCOLS] && |
599 | !info->attrs[NFC_ATTR_PROTOCOLS]) && | |
0f450772 | 600 | !info->attrs[NFC_ATTR_TM_PROTOCOLS])) |
4d12b8b1 LRV |
601 | return -EINVAL; |
602 | ||
603 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); | |
fe7c5800 SO |
604 | |
605 | if (info->attrs[NFC_ATTR_TM_PROTOCOLS]) | |
606 | tm_protocols = nla_get_u32(info->attrs[NFC_ATTR_TM_PROTOCOLS]); | |
fe7c5800 SO |
607 | |
608 | if (info->attrs[NFC_ATTR_IM_PROTOCOLS]) | |
609 | im_protocols = nla_get_u32(info->attrs[NFC_ATTR_IM_PROTOCOLS]); | |
5e50ee3a SO |
610 | else if (info->attrs[NFC_ATTR_PROTOCOLS]) |
611 | im_protocols = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]); | |
4d12b8b1 LRV |
612 | |
613 | dev = nfc_get_device(idx); | |
614 | if (!dev) | |
615 | return -ENODEV; | |
616 | ||
617 | mutex_lock(&dev->genl_data.genl_data_mutex); | |
618 | ||
fe7c5800 | 619 | rc = nfc_start_poll(dev, im_protocols, tm_protocols); |
4d12b8b1 | 620 | if (!rc) |
15e47304 | 621 | dev->genl_data.poll_req_portid = info->snd_portid; |
4d12b8b1 LRV |
622 | |
623 | mutex_unlock(&dev->genl_data.genl_data_mutex); | |
624 | ||
625 | nfc_put_device(dev); | |
626 | return rc; | |
627 | } | |
628 | ||
629 | static int nfc_genl_stop_poll(struct sk_buff *skb, struct genl_info *info) | |
630 | { | |
631 | struct nfc_dev *dev; | |
632 | int rc; | |
633 | u32 idx; | |
634 | ||
4d12b8b1 LRV |
635 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) |
636 | return -EINVAL; | |
637 | ||
638 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); | |
639 | ||
640 | dev = nfc_get_device(idx); | |
641 | if (!dev) | |
642 | return -ENODEV; | |
643 | ||
a831b913 SO |
644 | device_lock(&dev->dev); |
645 | ||
646 | if (!dev->polling) { | |
647 | device_unlock(&dev->dev); | |
648 | return -EINVAL; | |
649 | } | |
650 | ||
651 | device_unlock(&dev->dev); | |
652 | ||
4d12b8b1 LRV |
653 | mutex_lock(&dev->genl_data.genl_data_mutex); |
654 | ||
15e47304 | 655 | if (dev->genl_data.poll_req_portid != info->snd_portid) { |
4d12b8b1 LRV |
656 | rc = -EBUSY; |
657 | goto out; | |
658 | } | |
659 | ||
660 | rc = nfc_stop_poll(dev); | |
15e47304 | 661 | dev->genl_data.poll_req_portid = 0; |
4d12b8b1 LRV |
662 | |
663 | out: | |
664 | mutex_unlock(&dev->genl_data.genl_data_mutex); | |
665 | nfc_put_device(dev); | |
666 | return rc; | |
667 | } | |
668 | ||
1ed28f61 SO |
669 | static int nfc_genl_dep_link_up(struct sk_buff *skb, struct genl_info *info) |
670 | { | |
671 | struct nfc_dev *dev; | |
672 | int rc, tgt_idx; | |
673 | u32 idx; | |
47807d3d | 674 | u8 comm; |
1ed28f61 SO |
675 | |
676 | pr_debug("DEP link up\n"); | |
677 | ||
678 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX] || | |
47807d3d | 679 | !info->attrs[NFC_ATTR_COMM_MODE]) |
1ed28f61 SO |
680 | return -EINVAL; |
681 | ||
682 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); | |
683 | if (!info->attrs[NFC_ATTR_TARGET_INDEX]) | |
684 | tgt_idx = NFC_TARGET_IDX_ANY; | |
685 | else | |
686 | tgt_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]); | |
687 | ||
688 | comm = nla_get_u8(info->attrs[NFC_ATTR_COMM_MODE]); | |
1ed28f61 SO |
689 | |
690 | if (comm != NFC_COMM_ACTIVE && comm != NFC_COMM_PASSIVE) | |
691 | return -EINVAL; | |
692 | ||
1ed28f61 SO |
693 | dev = nfc_get_device(idx); |
694 | if (!dev) | |
695 | return -ENODEV; | |
696 | ||
47807d3d | 697 | rc = nfc_dep_link_up(dev, tgt_idx, comm); |
1ed28f61 SO |
698 | |
699 | nfc_put_device(dev); | |
700 | ||
701 | return rc; | |
702 | } | |
703 | ||
704 | static int nfc_genl_dep_link_down(struct sk_buff *skb, struct genl_info *info) | |
705 | { | |
706 | struct nfc_dev *dev; | |
707 | int rc; | |
708 | u32 idx; | |
709 | ||
710 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) | |
711 | return -EINVAL; | |
712 | ||
713 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); | |
714 | ||
715 | dev = nfc_get_device(idx); | |
716 | if (!dev) | |
717 | return -ENODEV; | |
718 | ||
719 | rc = nfc_dep_link_down(dev); | |
720 | ||
721 | nfc_put_device(dev); | |
722 | return rc; | |
723 | } | |
724 | ||
52feb444 TE |
725 | static int nfc_genl_send_params(struct sk_buff *msg, |
726 | struct nfc_llcp_local *local, | |
727 | u32 portid, u32 seq) | |
728 | { | |
729 | void *hdr; | |
730 | ||
731 | hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, 0, | |
732 | NFC_CMD_LLC_GET_PARAMS); | |
733 | if (!hdr) | |
734 | return -EMSGSIZE; | |
735 | ||
736 | if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, local->dev->idx) || | |
737 | nla_put_u8(msg, NFC_ATTR_LLC_PARAM_LTO, local->lto) || | |
738 | nla_put_u8(msg, NFC_ATTR_LLC_PARAM_RW, local->rw) || | |
739 | nla_put_u16(msg, NFC_ATTR_LLC_PARAM_MIUX, be16_to_cpu(local->miux))) | |
740 | goto nla_put_failure; | |
741 | ||
742 | return genlmsg_end(msg, hdr); | |
743 | ||
744 | nla_put_failure: | |
745 | ||
746 | genlmsg_cancel(msg, hdr); | |
747 | return -EMSGSIZE; | |
748 | } | |
749 | ||
750 | static int nfc_genl_llc_get_params(struct sk_buff *skb, struct genl_info *info) | |
751 | { | |
752 | struct nfc_dev *dev; | |
753 | struct nfc_llcp_local *local; | |
754 | int rc = 0; | |
755 | struct sk_buff *msg = NULL; | |
756 | u32 idx; | |
757 | ||
758 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) | |
759 | return -EINVAL; | |
760 | ||
761 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); | |
762 | ||
763 | dev = nfc_get_device(idx); | |
764 | if (!dev) | |
765 | return -ENODEV; | |
766 | ||
767 | device_lock(&dev->dev); | |
768 | ||
769 | local = nfc_llcp_find_local(dev); | |
770 | if (!local) { | |
771 | rc = -ENODEV; | |
772 | goto exit; | |
773 | } | |
774 | ||
775 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); | |
776 | if (!msg) { | |
777 | rc = -ENOMEM; | |
778 | goto exit; | |
779 | } | |
780 | ||
781 | rc = nfc_genl_send_params(msg, local, info->snd_portid, info->snd_seq); | |
782 | ||
783 | exit: | |
784 | device_unlock(&dev->dev); | |
785 | ||
786 | nfc_put_device(dev); | |
787 | ||
788 | if (rc < 0) { | |
789 | if (msg) | |
790 | nlmsg_free(msg); | |
791 | ||
792 | return rc; | |
793 | } | |
794 | ||
795 | return genlmsg_reply(msg, info); | |
796 | } | |
797 | ||
798 | static int nfc_genl_llc_set_params(struct sk_buff *skb, struct genl_info *info) | |
799 | { | |
800 | struct nfc_dev *dev; | |
801 | struct nfc_llcp_local *local; | |
802 | u8 rw = 0; | |
803 | u16 miux = 0; | |
804 | u32 idx; | |
805 | int rc = 0; | |
806 | ||
807 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX] || | |
808 | (!info->attrs[NFC_ATTR_LLC_PARAM_LTO] && | |
809 | !info->attrs[NFC_ATTR_LLC_PARAM_RW] && | |
810 | !info->attrs[NFC_ATTR_LLC_PARAM_MIUX])) | |
811 | return -EINVAL; | |
812 | ||
813 | if (info->attrs[NFC_ATTR_LLC_PARAM_RW]) { | |
814 | rw = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_RW]); | |
815 | ||
816 | if (rw > LLCP_MAX_RW) | |
817 | return -EINVAL; | |
818 | } | |
819 | ||
820 | if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX]) { | |
821 | miux = nla_get_u16(info->attrs[NFC_ATTR_LLC_PARAM_MIUX]); | |
822 | ||
823 | if (miux > LLCP_MAX_MIUX) | |
824 | return -EINVAL; | |
825 | } | |
826 | ||
827 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); | |
828 | ||
829 | dev = nfc_get_device(idx); | |
830 | if (!dev) | |
831 | return -ENODEV; | |
832 | ||
833 | device_lock(&dev->dev); | |
834 | ||
835 | local = nfc_llcp_find_local(dev); | |
836 | if (!local) { | |
837 | nfc_put_device(dev); | |
838 | rc = -ENODEV; | |
839 | goto exit; | |
840 | } | |
841 | ||
842 | if (info->attrs[NFC_ATTR_LLC_PARAM_LTO]) { | |
843 | if (dev->dep_link_up) { | |
844 | rc = -EINPROGRESS; | |
845 | goto exit; | |
846 | } | |
847 | ||
848 | local->lto = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_LTO]); | |
849 | } | |
850 | ||
851 | if (info->attrs[NFC_ATTR_LLC_PARAM_RW]) | |
852 | local->rw = rw; | |
853 | ||
854 | if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX]) | |
855 | local->miux = cpu_to_be16(miux); | |
856 | ||
857 | exit: | |
858 | device_unlock(&dev->dev); | |
859 | ||
860 | nfc_put_device(dev); | |
861 | ||
862 | return rc; | |
863 | } | |
864 | ||
4d12b8b1 LRV |
865 | static struct genl_ops nfc_genl_ops[] = { |
866 | { | |
867 | .cmd = NFC_CMD_GET_DEVICE, | |
868 | .doit = nfc_genl_get_device, | |
869 | .dumpit = nfc_genl_dump_devices, | |
870 | .done = nfc_genl_dump_devices_done, | |
871 | .policy = nfc_genl_policy, | |
8b3fe7b5 IE |
872 | }, |
873 | { | |
874 | .cmd = NFC_CMD_DEV_UP, | |
875 | .doit = nfc_genl_dev_up, | |
876 | .policy = nfc_genl_policy, | |
877 | }, | |
878 | { | |
879 | .cmd = NFC_CMD_DEV_DOWN, | |
880 | .doit = nfc_genl_dev_down, | |
881 | .policy = nfc_genl_policy, | |
4d12b8b1 LRV |
882 | }, |
883 | { | |
884 | .cmd = NFC_CMD_START_POLL, | |
885 | .doit = nfc_genl_start_poll, | |
886 | .policy = nfc_genl_policy, | |
887 | }, | |
888 | { | |
889 | .cmd = NFC_CMD_STOP_POLL, | |
890 | .doit = nfc_genl_stop_poll, | |
891 | .policy = nfc_genl_policy, | |
892 | }, | |
1ed28f61 SO |
893 | { |
894 | .cmd = NFC_CMD_DEP_LINK_UP, | |
895 | .doit = nfc_genl_dep_link_up, | |
896 | .policy = nfc_genl_policy, | |
897 | }, | |
898 | { | |
899 | .cmd = NFC_CMD_DEP_LINK_DOWN, | |
900 | .doit = nfc_genl_dep_link_down, | |
901 | .policy = nfc_genl_policy, | |
902 | }, | |
4d12b8b1 LRV |
903 | { |
904 | .cmd = NFC_CMD_GET_TARGET, | |
905 | .dumpit = nfc_genl_dump_targets, | |
906 | .done = nfc_genl_dump_targets_done, | |
907 | .policy = nfc_genl_policy, | |
908 | }, | |
52feb444 TE |
909 | { |
910 | .cmd = NFC_CMD_LLC_GET_PARAMS, | |
911 | .doit = nfc_genl_llc_get_params, | |
912 | .policy = nfc_genl_policy, | |
913 | }, | |
914 | { | |
915 | .cmd = NFC_CMD_LLC_SET_PARAMS, | |
916 | .doit = nfc_genl_llc_set_params, | |
917 | .policy = nfc_genl_policy, | |
918 | }, | |
4d12b8b1 LRV |
919 | }; |
920 | ||
3c0cc8aa SJ |
921 | |
922 | struct urelease_work { | |
923 | struct work_struct w; | |
c487606f | 924 | int portid; |
3c0cc8aa SJ |
925 | }; |
926 | ||
927 | static void nfc_urelease_event_work(struct work_struct *work) | |
4d12b8b1 | 928 | { |
3c0cc8aa | 929 | struct urelease_work *w = container_of(work, struct urelease_work, w); |
4d12b8b1 LRV |
930 | struct class_dev_iter iter; |
931 | struct nfc_dev *dev; | |
932 | ||
c487606f | 933 | pr_debug("portid %d\n", w->portid); |
4d12b8b1 | 934 | |
3c0cc8aa | 935 | mutex_lock(&nfc_devlist_mutex); |
4d12b8b1 LRV |
936 | |
937 | nfc_device_iter_init(&iter); | |
938 | dev = nfc_device_iter_next(&iter); | |
939 | ||
940 | while (dev) { | |
3c0cc8aa SJ |
941 | mutex_lock(&dev->genl_data.genl_data_mutex); |
942 | ||
c487606f | 943 | if (dev->genl_data.poll_req_portid == w->portid) { |
4d12b8b1 | 944 | nfc_stop_poll(dev); |
15e47304 | 945 | dev->genl_data.poll_req_portid = 0; |
4d12b8b1 | 946 | } |
3c0cc8aa SJ |
947 | |
948 | mutex_unlock(&dev->genl_data.genl_data_mutex); | |
949 | ||
4d12b8b1 LRV |
950 | dev = nfc_device_iter_next(&iter); |
951 | } | |
952 | ||
953 | nfc_device_iter_exit(&iter); | |
954 | ||
3c0cc8aa SJ |
955 | mutex_unlock(&nfc_devlist_mutex); |
956 | ||
957 | kfree(w); | |
958 | } | |
959 | ||
960 | static int nfc_genl_rcv_nl_event(struct notifier_block *this, | |
961 | unsigned long event, void *ptr) | |
962 | { | |
963 | struct netlink_notify *n = ptr; | |
964 | struct urelease_work *w; | |
965 | ||
966 | if (event != NETLINK_URELEASE || n->protocol != NETLINK_GENERIC) | |
967 | goto out; | |
968 | ||
c487606f | 969 | pr_debug("NETLINK_URELEASE event from id %d\n", n->portid); |
3c0cc8aa SJ |
970 | |
971 | w = kmalloc(sizeof(*w), GFP_ATOMIC); | |
972 | if (w) { | |
973 | INIT_WORK((struct work_struct *) w, nfc_urelease_event_work); | |
c487606f | 974 | w->portid = n->portid; |
3c0cc8aa SJ |
975 | schedule_work((struct work_struct *) w); |
976 | } | |
977 | ||
4d12b8b1 LRV |
978 | out: |
979 | return NOTIFY_DONE; | |
980 | } | |
981 | ||
982 | void nfc_genl_data_init(struct nfc_genl_data *genl_data) | |
983 | { | |
15e47304 | 984 | genl_data->poll_req_portid = 0; |
4d12b8b1 LRV |
985 | mutex_init(&genl_data->genl_data_mutex); |
986 | } | |
987 | ||
988 | void nfc_genl_data_exit(struct nfc_genl_data *genl_data) | |
989 | { | |
990 | mutex_destroy(&genl_data->genl_data_mutex); | |
991 | } | |
992 | ||
993 | static struct notifier_block nl_notifier = { | |
994 | .notifier_call = nfc_genl_rcv_nl_event, | |
995 | }; | |
996 | ||
997 | /** | |
998 | * nfc_genl_init() - Initialize netlink interface | |
999 | * | |
1000 | * This initialization function registers the nfc netlink family. | |
1001 | */ | |
1002 | int __init nfc_genl_init(void) | |
1003 | { | |
1004 | int rc; | |
1005 | ||
1006 | rc = genl_register_family_with_ops(&nfc_genl_family, nfc_genl_ops, | |
0a40acb2 | 1007 | ARRAY_SIZE(nfc_genl_ops)); |
4d12b8b1 LRV |
1008 | if (rc) |
1009 | return rc; | |
1010 | ||
1011 | rc = genl_register_mc_group(&nfc_genl_family, &nfc_genl_event_mcgrp); | |
1012 | ||
1013 | netlink_register_notifier(&nl_notifier); | |
1014 | ||
1015 | return rc; | |
1016 | } | |
1017 | ||
1018 | /** | |
1019 | * nfc_genl_exit() - Deinitialize netlink interface | |
1020 | * | |
1021 | * This exit function unregisters the nfc netlink family. | |
1022 | */ | |
1023 | void nfc_genl_exit(void) | |
1024 | { | |
1025 | netlink_unregister_notifier(&nl_notifier); | |
1026 | genl_unregister_family(&nfc_genl_family); | |
1027 | } |