]>
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 }, | |
d9b8d8e1 TE |
59 | [NFC_ATTR_LLC_SDP] = { .type = NLA_NESTED }, |
60 | }; | |
61 | ||
62 | static const struct nla_policy nfc_sdp_genl_policy[NFC_SDP_ATTR_MAX + 1] = { | |
63 | [NFC_SDP_ATTR_URI] = { .type = NLA_STRING }, | |
64 | [NFC_SDP_ATTR_SAP] = { .type = NLA_U8 }, | |
4d12b8b1 LRV |
65 | }; |
66 | ||
67 | static int nfc_genl_send_target(struct sk_buff *msg, struct nfc_target *target, | |
0a40acb2 | 68 | struct netlink_callback *cb, int flags) |
4d12b8b1 LRV |
69 | { |
70 | void *hdr; | |
71 | ||
15e47304 | 72 | hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, |
0a40acb2 | 73 | &nfc_genl_family, flags, NFC_CMD_GET_TARGET); |
4d12b8b1 LRV |
74 | if (!hdr) |
75 | return -EMSGSIZE; | |
76 | ||
77 | genl_dump_check_consistent(cb, hdr, &nfc_genl_family); | |
78 | ||
1e6428d8 DM |
79 | if (nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target->idx) || |
80 | nla_put_u32(msg, NFC_ATTR_PROTOCOLS, target->supported_protocols) || | |
81 | nla_put_u16(msg, NFC_ATTR_TARGET_SENS_RES, target->sens_res) || | |
82 | nla_put_u8(msg, NFC_ATTR_TARGET_SEL_RES, target->sel_res)) | |
83 | goto nla_put_failure; | |
84 | if (target->nfcid1_len > 0 && | |
85 | nla_put(msg, NFC_ATTR_TARGET_NFCID1, target->nfcid1_len, | |
86 | target->nfcid1)) | |
87 | goto nla_put_failure; | |
88 | if (target->sensb_res_len > 0 && | |
89 | nla_put(msg, NFC_ATTR_TARGET_SENSB_RES, target->sensb_res_len, | |
90 | target->sensb_res)) | |
91 | goto nla_put_failure; | |
92 | if (target->sensf_res_len > 0 && | |
93 | nla_put(msg, NFC_ATTR_TARGET_SENSF_RES, target->sensf_res_len, | |
94 | target->sensf_res)) | |
95 | goto nla_put_failure; | |
4d12b8b1 LRV |
96 | |
97 | return genlmsg_end(msg, hdr); | |
98 | ||
99 | nla_put_failure: | |
100 | genlmsg_cancel(msg, hdr); | |
101 | return -EMSGSIZE; | |
102 | } | |
103 | ||
104 | static struct nfc_dev *__get_device_from_cb(struct netlink_callback *cb) | |
105 | { | |
106 | struct nfc_dev *dev; | |
107 | int rc; | |
108 | u32 idx; | |
109 | ||
110 | rc = nlmsg_parse(cb->nlh, GENL_HDRLEN + nfc_genl_family.hdrsize, | |
0a40acb2 SO |
111 | nfc_genl_family.attrbuf, |
112 | nfc_genl_family.maxattr, | |
113 | nfc_genl_policy); | |
4d12b8b1 LRV |
114 | if (rc < 0) |
115 | return ERR_PTR(rc); | |
116 | ||
117 | if (!nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX]) | |
118 | return ERR_PTR(-EINVAL); | |
119 | ||
120 | idx = nla_get_u32(nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX]); | |
121 | ||
122 | dev = nfc_get_device(idx); | |
123 | if (!dev) | |
124 | return ERR_PTR(-ENODEV); | |
125 | ||
126 | return dev; | |
127 | } | |
128 | ||
129 | static int nfc_genl_dump_targets(struct sk_buff *skb, | |
0a40acb2 | 130 | struct netlink_callback *cb) |
4d12b8b1 LRV |
131 | { |
132 | int i = cb->args[0]; | |
133 | struct nfc_dev *dev = (struct nfc_dev *) cb->args[1]; | |
134 | int rc; | |
135 | ||
4d12b8b1 LRV |
136 | if (!dev) { |
137 | dev = __get_device_from_cb(cb); | |
138 | if (IS_ERR(dev)) | |
139 | return PTR_ERR(dev); | |
140 | ||
141 | cb->args[1] = (long) dev; | |
142 | } | |
143 | ||
d4ccb132 | 144 | device_lock(&dev->dev); |
4d12b8b1 LRV |
145 | |
146 | cb->seq = dev->targets_generation; | |
147 | ||
148 | while (i < dev->n_targets) { | |
149 | rc = nfc_genl_send_target(skb, &dev->targets[i], cb, | |
0a40acb2 | 150 | NLM_F_MULTI); |
4d12b8b1 LRV |
151 | if (rc < 0) |
152 | break; | |
153 | ||
154 | i++; | |
155 | } | |
156 | ||
d4ccb132 | 157 | device_unlock(&dev->dev); |
4d12b8b1 LRV |
158 | |
159 | cb->args[0] = i; | |
160 | ||
161 | return skb->len; | |
162 | } | |
163 | ||
164 | static int nfc_genl_dump_targets_done(struct netlink_callback *cb) | |
165 | { | |
166 | struct nfc_dev *dev = (struct nfc_dev *) cb->args[1]; | |
167 | ||
4d12b8b1 LRV |
168 | if (dev) |
169 | nfc_put_device(dev); | |
170 | ||
171 | return 0; | |
172 | } | |
173 | ||
174 | int nfc_genl_targets_found(struct nfc_dev *dev) | |
175 | { | |
176 | struct sk_buff *msg; | |
177 | void *hdr; | |
178 | ||
15e47304 | 179 | dev->genl_data.poll_req_portid = 0; |
4d12b8b1 | 180 | |
58050fce | 181 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); |
4d12b8b1 LRV |
182 | if (!msg) |
183 | return -ENOMEM; | |
184 | ||
185 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, | |
0a40acb2 | 186 | NFC_EVENT_TARGETS_FOUND); |
4d12b8b1 LRV |
187 | if (!hdr) |
188 | goto free_msg; | |
189 | ||
1e6428d8 DM |
190 | if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx)) |
191 | goto nla_put_failure; | |
4d12b8b1 LRV |
192 | |
193 | genlmsg_end(msg, hdr); | |
194 | ||
195 | return genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC); | |
196 | ||
197 | nla_put_failure: | |
198 | genlmsg_cancel(msg, hdr); | |
199 | free_msg: | |
200 | nlmsg_free(msg); | |
201 | return -EMSGSIZE; | |
202 | } | |
203 | ||
8112a5c9 SO |
204 | int nfc_genl_target_lost(struct nfc_dev *dev, u32 target_idx) |
205 | { | |
206 | struct sk_buff *msg; | |
207 | void *hdr; | |
208 | ||
58050fce | 209 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
8112a5c9 SO |
210 | if (!msg) |
211 | return -ENOMEM; | |
212 | ||
213 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, | |
214 | NFC_EVENT_TARGET_LOST); | |
215 | if (!hdr) | |
216 | goto free_msg; | |
217 | ||
59ef43e6 JL |
218 | if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) || |
219 | nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx)) | |
220 | goto nla_put_failure; | |
8112a5c9 SO |
221 | |
222 | genlmsg_end(msg, hdr); | |
223 | ||
224 | genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL); | |
225 | ||
226 | return 0; | |
227 | ||
fc40a8c1 SO |
228 | nla_put_failure: |
229 | genlmsg_cancel(msg, hdr); | |
230 | free_msg: | |
231 | nlmsg_free(msg); | |
232 | return -EMSGSIZE; | |
233 | } | |
234 | ||
235 | int nfc_genl_tm_activated(struct nfc_dev *dev, u32 protocol) | |
236 | { | |
237 | struct sk_buff *msg; | |
238 | void *hdr; | |
239 | ||
58050fce | 240 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
fc40a8c1 SO |
241 | if (!msg) |
242 | return -ENOMEM; | |
243 | ||
244 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, | |
245 | NFC_EVENT_TM_ACTIVATED); | |
246 | if (!hdr) | |
247 | goto free_msg; | |
248 | ||
249 | if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx)) | |
250 | goto nla_put_failure; | |
251 | if (nla_put_u32(msg, NFC_ATTR_TM_PROTOCOLS, protocol)) | |
252 | goto nla_put_failure; | |
253 | ||
254 | genlmsg_end(msg, hdr); | |
255 | ||
256 | genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL); | |
257 | ||
258 | return 0; | |
259 | ||
260 | nla_put_failure: | |
261 | genlmsg_cancel(msg, hdr); | |
262 | free_msg: | |
263 | nlmsg_free(msg); | |
264 | return -EMSGSIZE; | |
265 | } | |
266 | ||
267 | int nfc_genl_tm_deactivated(struct nfc_dev *dev) | |
268 | { | |
269 | struct sk_buff *msg; | |
270 | void *hdr; | |
271 | ||
58050fce | 272 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
fc40a8c1 SO |
273 | if (!msg) |
274 | return -ENOMEM; | |
275 | ||
276 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, | |
277 | NFC_EVENT_TM_DEACTIVATED); | |
278 | if (!hdr) | |
279 | goto free_msg; | |
280 | ||
281 | if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx)) | |
282 | goto nla_put_failure; | |
283 | ||
284 | genlmsg_end(msg, hdr); | |
285 | ||
286 | genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL); | |
287 | ||
288 | return 0; | |
289 | ||
8112a5c9 SO |
290 | nla_put_failure: |
291 | genlmsg_cancel(msg, hdr); | |
292 | free_msg: | |
293 | nlmsg_free(msg); | |
294 | return -EMSGSIZE; | |
295 | } | |
296 | ||
4d12b8b1 LRV |
297 | int nfc_genl_device_added(struct nfc_dev *dev) |
298 | { | |
299 | struct sk_buff *msg; | |
300 | void *hdr; | |
301 | ||
58050fce | 302 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
4d12b8b1 LRV |
303 | if (!msg) |
304 | return -ENOMEM; | |
305 | ||
306 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, | |
0a40acb2 | 307 | NFC_EVENT_DEVICE_ADDED); |
4d12b8b1 LRV |
308 | if (!hdr) |
309 | goto free_msg; | |
310 | ||
1e6428d8 DM |
311 | if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) || |
312 | nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) || | |
313 | nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) || | |
314 | nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up)) | |
315 | goto nla_put_failure; | |
4d12b8b1 LRV |
316 | |
317 | genlmsg_end(msg, hdr); | |
318 | ||
319 | genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL); | |
320 | ||
321 | return 0; | |
322 | ||
323 | nla_put_failure: | |
324 | genlmsg_cancel(msg, hdr); | |
325 | free_msg: | |
326 | nlmsg_free(msg); | |
327 | return -EMSGSIZE; | |
328 | } | |
329 | ||
330 | int nfc_genl_device_removed(struct nfc_dev *dev) | |
331 | { | |
332 | struct sk_buff *msg; | |
333 | void *hdr; | |
334 | ||
58050fce | 335 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
4d12b8b1 LRV |
336 | if (!msg) |
337 | return -ENOMEM; | |
338 | ||
339 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, | |
0a40acb2 | 340 | NFC_EVENT_DEVICE_REMOVED); |
4d12b8b1 LRV |
341 | if (!hdr) |
342 | goto free_msg; | |
343 | ||
1e6428d8 DM |
344 | if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx)) |
345 | goto nla_put_failure; | |
4d12b8b1 LRV |
346 | |
347 | genlmsg_end(msg, hdr); | |
348 | ||
349 | genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL); | |
350 | ||
351 | return 0; | |
352 | ||
353 | nla_put_failure: | |
354 | genlmsg_cancel(msg, hdr); | |
355 | free_msg: | |
356 | nlmsg_free(msg); | |
357 | return -EMSGSIZE; | |
358 | } | |
359 | ||
d9b8d8e1 TE |
360 | int nfc_genl_llc_send_sdres(struct nfc_dev *dev, struct hlist_head *sdres_list) |
361 | { | |
362 | struct sk_buff *msg; | |
363 | struct nlattr *sdp_attr, *uri_attr; | |
364 | struct nfc_llcp_sdp_tlv *sdres; | |
365 | struct hlist_node *n; | |
366 | void *hdr; | |
367 | int rc = -EMSGSIZE; | |
368 | int i; | |
369 | ||
370 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); | |
371 | if (!msg) | |
372 | return -ENOMEM; | |
373 | ||
374 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, | |
375 | NFC_EVENT_LLC_SDRES); | |
376 | if (!hdr) | |
377 | goto free_msg; | |
378 | ||
379 | if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx)) | |
380 | goto nla_put_failure; | |
381 | ||
382 | sdp_attr = nla_nest_start(msg, NFC_ATTR_LLC_SDP); | |
383 | if (sdp_attr == NULL) { | |
384 | rc = -ENOMEM; | |
385 | goto nla_put_failure; | |
386 | } | |
387 | ||
388 | i = 1; | |
389 | hlist_for_each_entry_safe(sdres, n, sdres_list, node) { | |
390 | pr_debug("uri: %s, sap: %d\n", sdres->uri, sdres->sap); | |
391 | ||
392 | uri_attr = nla_nest_start(msg, i++); | |
393 | if (uri_attr == NULL) { | |
394 | rc = -ENOMEM; | |
395 | goto nla_put_failure; | |
396 | } | |
397 | ||
398 | if (nla_put_u8(msg, NFC_SDP_ATTR_SAP, sdres->sap)) | |
399 | goto nla_put_failure; | |
400 | ||
401 | if (nla_put_string(msg, NFC_SDP_ATTR_URI, sdres->uri)) | |
402 | goto nla_put_failure; | |
403 | ||
404 | nla_nest_end(msg, uri_attr); | |
405 | ||
406 | hlist_del(&sdres->node); | |
407 | ||
408 | nfc_llcp_free_sdp_tlv(sdres); | |
409 | } | |
410 | ||
411 | nla_nest_end(msg, sdp_attr); | |
412 | ||
413 | genlmsg_end(msg, hdr); | |
414 | ||
415 | return genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC); | |
416 | ||
417 | nla_put_failure: | |
418 | genlmsg_cancel(msg, hdr); | |
419 | ||
420 | free_msg: | |
421 | nlmsg_free(msg); | |
422 | ||
423 | nfc_llcp_free_sdp_tlv_list(sdres_list); | |
424 | ||
425 | return rc; | |
426 | } | |
427 | ||
4d12b8b1 | 428 | static int nfc_genl_send_device(struct sk_buff *msg, struct nfc_dev *dev, |
15e47304 | 429 | u32 portid, u32 seq, |
0a40acb2 SO |
430 | struct netlink_callback *cb, |
431 | int flags) | |
4d12b8b1 LRV |
432 | { |
433 | void *hdr; | |
434 | ||
15e47304 | 435 | hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags, |
0a40acb2 | 436 | NFC_CMD_GET_DEVICE); |
4d12b8b1 LRV |
437 | if (!hdr) |
438 | return -EMSGSIZE; | |
439 | ||
440 | if (cb) | |
441 | genl_dump_check_consistent(cb, hdr, &nfc_genl_family); | |
442 | ||
1e6428d8 DM |
443 | if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) || |
444 | nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) || | |
445 | nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) || | |
390a1bd8 | 446 | nla_put_u32(msg, NFC_ATTR_SE, dev->supported_se) || |
7ad39395 TE |
447 | nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up) || |
448 | nla_put_u8(msg, NFC_ATTR_RF_MODE, dev->rf_mode)) | |
1e6428d8 | 449 | goto nla_put_failure; |
4d12b8b1 LRV |
450 | |
451 | return genlmsg_end(msg, hdr); | |
452 | ||
453 | nla_put_failure: | |
454 | genlmsg_cancel(msg, hdr); | |
455 | return -EMSGSIZE; | |
456 | } | |
457 | ||
458 | static int nfc_genl_dump_devices(struct sk_buff *skb, | |
0a40acb2 | 459 | struct netlink_callback *cb) |
4d12b8b1 LRV |
460 | { |
461 | struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0]; | |
462 | struct nfc_dev *dev = (struct nfc_dev *) cb->args[1]; | |
463 | bool first_call = false; | |
464 | ||
4d12b8b1 LRV |
465 | if (!iter) { |
466 | first_call = true; | |
467 | iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL); | |
468 | if (!iter) | |
469 | return -ENOMEM; | |
470 | cb->args[0] = (long) iter; | |
471 | } | |
472 | ||
473 | mutex_lock(&nfc_devlist_mutex); | |
474 | ||
475 | cb->seq = nfc_devlist_generation; | |
476 | ||
477 | if (first_call) { | |
478 | nfc_device_iter_init(iter); | |
479 | dev = nfc_device_iter_next(iter); | |
480 | } | |
481 | ||
482 | while (dev) { | |
483 | int rc; | |
484 | ||
15e47304 | 485 | rc = nfc_genl_send_device(skb, dev, NETLINK_CB(cb->skb).portid, |
0a40acb2 | 486 | cb->nlh->nlmsg_seq, cb, NLM_F_MULTI); |
4d12b8b1 LRV |
487 | if (rc < 0) |
488 | break; | |
489 | ||
490 | dev = nfc_device_iter_next(iter); | |
491 | } | |
492 | ||
493 | mutex_unlock(&nfc_devlist_mutex); | |
494 | ||
495 | cb->args[1] = (long) dev; | |
496 | ||
497 | return skb->len; | |
498 | } | |
499 | ||
500 | static int nfc_genl_dump_devices_done(struct netlink_callback *cb) | |
501 | { | |
502 | struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0]; | |
503 | ||
4d12b8b1 LRV |
504 | nfc_device_iter_exit(iter); |
505 | kfree(iter); | |
506 | ||
507 | return 0; | |
508 | } | |
509 | ||
1ed28f61 | 510 | int nfc_genl_dep_link_up_event(struct nfc_dev *dev, u32 target_idx, |
0a40acb2 | 511 | u8 comm_mode, u8 rf_mode) |
1ed28f61 SO |
512 | { |
513 | struct sk_buff *msg; | |
514 | void *hdr; | |
515 | ||
516 | pr_debug("DEP link is up\n"); | |
517 | ||
58050fce | 518 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); |
1ed28f61 SO |
519 | if (!msg) |
520 | return -ENOMEM; | |
521 | ||
0a40acb2 | 522 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, NFC_CMD_DEP_LINK_UP); |
1ed28f61 SO |
523 | if (!hdr) |
524 | goto free_msg; | |
525 | ||
1e6428d8 DM |
526 | if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx)) |
527 | goto nla_put_failure; | |
528 | if (rf_mode == NFC_RF_INITIATOR && | |
529 | nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx)) | |
530 | goto nla_put_failure; | |
531 | if (nla_put_u8(msg, NFC_ATTR_COMM_MODE, comm_mode) || | |
532 | nla_put_u8(msg, NFC_ATTR_RF_MODE, rf_mode)) | |
533 | goto nla_put_failure; | |
1ed28f61 SO |
534 | |
535 | genlmsg_end(msg, hdr); | |
536 | ||
537 | dev->dep_link_up = true; | |
538 | ||
539 | genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC); | |
540 | ||
541 | return 0; | |
542 | ||
543 | nla_put_failure: | |
544 | genlmsg_cancel(msg, hdr); | |
545 | free_msg: | |
546 | nlmsg_free(msg); | |
547 | return -EMSGSIZE; | |
548 | } | |
549 | ||
550 | int nfc_genl_dep_link_down_event(struct nfc_dev *dev) | |
551 | { | |
552 | struct sk_buff *msg; | |
553 | void *hdr; | |
554 | ||
555 | pr_debug("DEP link is down\n"); | |
556 | ||
58050fce | 557 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); |
1ed28f61 SO |
558 | if (!msg) |
559 | return -ENOMEM; | |
560 | ||
561 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, | |
0a40acb2 | 562 | NFC_CMD_DEP_LINK_DOWN); |
1ed28f61 SO |
563 | if (!hdr) |
564 | goto free_msg; | |
565 | ||
1e6428d8 DM |
566 | if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx)) |
567 | goto nla_put_failure; | |
1ed28f61 SO |
568 | |
569 | genlmsg_end(msg, hdr); | |
570 | ||
571 | genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC); | |
572 | ||
573 | return 0; | |
574 | ||
575 | nla_put_failure: | |
576 | genlmsg_cancel(msg, hdr); | |
577 | free_msg: | |
578 | nlmsg_free(msg); | |
579 | return -EMSGSIZE; | |
580 | } | |
581 | ||
4d12b8b1 LRV |
582 | static int nfc_genl_get_device(struct sk_buff *skb, struct genl_info *info) |
583 | { | |
584 | struct sk_buff *msg; | |
585 | struct nfc_dev *dev; | |
586 | u32 idx; | |
587 | int rc = -ENOBUFS; | |
588 | ||
4d12b8b1 LRV |
589 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) |
590 | return -EINVAL; | |
591 | ||
592 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); | |
593 | ||
594 | dev = nfc_get_device(idx); | |
595 | if (!dev) | |
596 | return -ENODEV; | |
597 | ||
58050fce | 598 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
4d12b8b1 LRV |
599 | if (!msg) { |
600 | rc = -ENOMEM; | |
601 | goto out_putdev; | |
602 | } | |
603 | ||
15e47304 | 604 | rc = nfc_genl_send_device(msg, dev, info->snd_portid, info->snd_seq, |
0a40acb2 | 605 | NULL, 0); |
4d12b8b1 LRV |
606 | if (rc < 0) |
607 | goto out_free; | |
608 | ||
609 | nfc_put_device(dev); | |
610 | ||
611 | return genlmsg_reply(msg, info); | |
612 | ||
613 | out_free: | |
614 | nlmsg_free(msg); | |
615 | out_putdev: | |
616 | nfc_put_device(dev); | |
617 | return rc; | |
618 | } | |
619 | ||
8b3fe7b5 IE |
620 | static int nfc_genl_dev_up(struct sk_buff *skb, struct genl_info *info) |
621 | { | |
622 | struct nfc_dev *dev; | |
623 | int rc; | |
624 | u32 idx; | |
625 | ||
8b3fe7b5 IE |
626 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) |
627 | return -EINVAL; | |
628 | ||
629 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); | |
630 | ||
631 | dev = nfc_get_device(idx); | |
632 | if (!dev) | |
633 | return -ENODEV; | |
634 | ||
635 | rc = nfc_dev_up(dev); | |
636 | ||
637 | nfc_put_device(dev); | |
638 | return rc; | |
639 | } | |
640 | ||
641 | static int nfc_genl_dev_down(struct sk_buff *skb, struct genl_info *info) | |
642 | { | |
643 | struct nfc_dev *dev; | |
644 | int rc; | |
645 | u32 idx; | |
646 | ||
8b3fe7b5 IE |
647 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) |
648 | return -EINVAL; | |
649 | ||
650 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); | |
651 | ||
652 | dev = nfc_get_device(idx); | |
653 | if (!dev) | |
654 | return -ENODEV; | |
655 | ||
656 | rc = nfc_dev_down(dev); | |
657 | ||
658 | nfc_put_device(dev); | |
659 | return rc; | |
660 | } | |
661 | ||
4d12b8b1 LRV |
662 | static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info) |
663 | { | |
664 | struct nfc_dev *dev; | |
665 | int rc; | |
666 | u32 idx; | |
fe7c5800 | 667 | u32 im_protocols = 0, tm_protocols = 0; |
4d12b8b1 | 668 | |
1ed28f61 SO |
669 | pr_debug("Poll start\n"); |
670 | ||
4d12b8b1 | 671 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX] || |
fe7c5800 SO |
672 | ((!info->attrs[NFC_ATTR_IM_PROTOCOLS] && |
673 | !info->attrs[NFC_ATTR_PROTOCOLS]) && | |
0f450772 | 674 | !info->attrs[NFC_ATTR_TM_PROTOCOLS])) |
4d12b8b1 LRV |
675 | return -EINVAL; |
676 | ||
677 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); | |
fe7c5800 SO |
678 | |
679 | if (info->attrs[NFC_ATTR_TM_PROTOCOLS]) | |
680 | tm_protocols = nla_get_u32(info->attrs[NFC_ATTR_TM_PROTOCOLS]); | |
fe7c5800 SO |
681 | |
682 | if (info->attrs[NFC_ATTR_IM_PROTOCOLS]) | |
683 | im_protocols = nla_get_u32(info->attrs[NFC_ATTR_IM_PROTOCOLS]); | |
5e50ee3a SO |
684 | else if (info->attrs[NFC_ATTR_PROTOCOLS]) |
685 | im_protocols = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]); | |
4d12b8b1 LRV |
686 | |
687 | dev = nfc_get_device(idx); | |
688 | if (!dev) | |
689 | return -ENODEV; | |
690 | ||
691 | mutex_lock(&dev->genl_data.genl_data_mutex); | |
692 | ||
fe7c5800 | 693 | rc = nfc_start_poll(dev, im_protocols, tm_protocols); |
4d12b8b1 | 694 | if (!rc) |
15e47304 | 695 | dev->genl_data.poll_req_portid = info->snd_portid; |
4d12b8b1 LRV |
696 | |
697 | mutex_unlock(&dev->genl_data.genl_data_mutex); | |
698 | ||
699 | nfc_put_device(dev); | |
700 | return rc; | |
701 | } | |
702 | ||
703 | static int nfc_genl_stop_poll(struct sk_buff *skb, struct genl_info *info) | |
704 | { | |
705 | struct nfc_dev *dev; | |
706 | int rc; | |
707 | u32 idx; | |
708 | ||
4d12b8b1 LRV |
709 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) |
710 | return -EINVAL; | |
711 | ||
712 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); | |
713 | ||
714 | dev = nfc_get_device(idx); | |
715 | if (!dev) | |
716 | return -ENODEV; | |
717 | ||
a831b913 SO |
718 | device_lock(&dev->dev); |
719 | ||
720 | if (!dev->polling) { | |
721 | device_unlock(&dev->dev); | |
722 | return -EINVAL; | |
723 | } | |
724 | ||
725 | device_unlock(&dev->dev); | |
726 | ||
4d12b8b1 LRV |
727 | mutex_lock(&dev->genl_data.genl_data_mutex); |
728 | ||
15e47304 | 729 | if (dev->genl_data.poll_req_portid != info->snd_portid) { |
4d12b8b1 LRV |
730 | rc = -EBUSY; |
731 | goto out; | |
732 | } | |
733 | ||
734 | rc = nfc_stop_poll(dev); | |
15e47304 | 735 | dev->genl_data.poll_req_portid = 0; |
4d12b8b1 LRV |
736 | |
737 | out: | |
738 | mutex_unlock(&dev->genl_data.genl_data_mutex); | |
739 | nfc_put_device(dev); | |
740 | return rc; | |
741 | } | |
742 | ||
1ed28f61 SO |
743 | static int nfc_genl_dep_link_up(struct sk_buff *skb, struct genl_info *info) |
744 | { | |
745 | struct nfc_dev *dev; | |
746 | int rc, tgt_idx; | |
747 | u32 idx; | |
47807d3d | 748 | u8 comm; |
1ed28f61 SO |
749 | |
750 | pr_debug("DEP link up\n"); | |
751 | ||
752 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX] || | |
47807d3d | 753 | !info->attrs[NFC_ATTR_COMM_MODE]) |
1ed28f61 SO |
754 | return -EINVAL; |
755 | ||
756 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); | |
757 | if (!info->attrs[NFC_ATTR_TARGET_INDEX]) | |
758 | tgt_idx = NFC_TARGET_IDX_ANY; | |
759 | else | |
760 | tgt_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]); | |
761 | ||
762 | comm = nla_get_u8(info->attrs[NFC_ATTR_COMM_MODE]); | |
1ed28f61 SO |
763 | |
764 | if (comm != NFC_COMM_ACTIVE && comm != NFC_COMM_PASSIVE) | |
765 | return -EINVAL; | |
766 | ||
1ed28f61 SO |
767 | dev = nfc_get_device(idx); |
768 | if (!dev) | |
769 | return -ENODEV; | |
770 | ||
47807d3d | 771 | rc = nfc_dep_link_up(dev, tgt_idx, comm); |
1ed28f61 SO |
772 | |
773 | nfc_put_device(dev); | |
774 | ||
775 | return rc; | |
776 | } | |
777 | ||
778 | static int nfc_genl_dep_link_down(struct sk_buff *skb, struct genl_info *info) | |
779 | { | |
780 | struct nfc_dev *dev; | |
781 | int rc; | |
782 | u32 idx; | |
783 | ||
784 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) | |
785 | return -EINVAL; | |
786 | ||
787 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); | |
788 | ||
789 | dev = nfc_get_device(idx); | |
790 | if (!dev) | |
791 | return -ENODEV; | |
792 | ||
793 | rc = nfc_dep_link_down(dev); | |
794 | ||
795 | nfc_put_device(dev); | |
796 | return rc; | |
797 | } | |
798 | ||
52feb444 TE |
799 | static int nfc_genl_send_params(struct sk_buff *msg, |
800 | struct nfc_llcp_local *local, | |
801 | u32 portid, u32 seq) | |
802 | { | |
803 | void *hdr; | |
804 | ||
805 | hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, 0, | |
806 | NFC_CMD_LLC_GET_PARAMS); | |
807 | if (!hdr) | |
808 | return -EMSGSIZE; | |
809 | ||
810 | if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, local->dev->idx) || | |
811 | nla_put_u8(msg, NFC_ATTR_LLC_PARAM_LTO, local->lto) || | |
812 | nla_put_u8(msg, NFC_ATTR_LLC_PARAM_RW, local->rw) || | |
813 | nla_put_u16(msg, NFC_ATTR_LLC_PARAM_MIUX, be16_to_cpu(local->miux))) | |
814 | goto nla_put_failure; | |
815 | ||
816 | return genlmsg_end(msg, hdr); | |
817 | ||
818 | nla_put_failure: | |
819 | ||
820 | genlmsg_cancel(msg, hdr); | |
821 | return -EMSGSIZE; | |
822 | } | |
823 | ||
824 | static int nfc_genl_llc_get_params(struct sk_buff *skb, struct genl_info *info) | |
825 | { | |
826 | struct nfc_dev *dev; | |
827 | struct nfc_llcp_local *local; | |
828 | int rc = 0; | |
829 | struct sk_buff *msg = NULL; | |
830 | u32 idx; | |
831 | ||
832 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) | |
833 | return -EINVAL; | |
834 | ||
835 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); | |
836 | ||
837 | dev = nfc_get_device(idx); | |
838 | if (!dev) | |
839 | return -ENODEV; | |
840 | ||
841 | device_lock(&dev->dev); | |
842 | ||
843 | local = nfc_llcp_find_local(dev); | |
844 | if (!local) { | |
845 | rc = -ENODEV; | |
846 | goto exit; | |
847 | } | |
848 | ||
849 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); | |
850 | if (!msg) { | |
851 | rc = -ENOMEM; | |
852 | goto exit; | |
853 | } | |
854 | ||
855 | rc = nfc_genl_send_params(msg, local, info->snd_portid, info->snd_seq); | |
856 | ||
857 | exit: | |
858 | device_unlock(&dev->dev); | |
859 | ||
860 | nfc_put_device(dev); | |
861 | ||
862 | if (rc < 0) { | |
863 | if (msg) | |
864 | nlmsg_free(msg); | |
865 | ||
866 | return rc; | |
867 | } | |
868 | ||
869 | return genlmsg_reply(msg, info); | |
870 | } | |
871 | ||
872 | static int nfc_genl_llc_set_params(struct sk_buff *skb, struct genl_info *info) | |
873 | { | |
874 | struct nfc_dev *dev; | |
875 | struct nfc_llcp_local *local; | |
876 | u8 rw = 0; | |
877 | u16 miux = 0; | |
878 | u32 idx; | |
879 | int rc = 0; | |
880 | ||
881 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX] || | |
882 | (!info->attrs[NFC_ATTR_LLC_PARAM_LTO] && | |
883 | !info->attrs[NFC_ATTR_LLC_PARAM_RW] && | |
884 | !info->attrs[NFC_ATTR_LLC_PARAM_MIUX])) | |
885 | return -EINVAL; | |
886 | ||
887 | if (info->attrs[NFC_ATTR_LLC_PARAM_RW]) { | |
888 | rw = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_RW]); | |
889 | ||
890 | if (rw > LLCP_MAX_RW) | |
891 | return -EINVAL; | |
892 | } | |
893 | ||
894 | if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX]) { | |
895 | miux = nla_get_u16(info->attrs[NFC_ATTR_LLC_PARAM_MIUX]); | |
896 | ||
897 | if (miux > LLCP_MAX_MIUX) | |
898 | return -EINVAL; | |
899 | } | |
900 | ||
901 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); | |
902 | ||
903 | dev = nfc_get_device(idx); | |
904 | if (!dev) | |
905 | return -ENODEV; | |
906 | ||
907 | device_lock(&dev->dev); | |
908 | ||
909 | local = nfc_llcp_find_local(dev); | |
910 | if (!local) { | |
911 | nfc_put_device(dev); | |
912 | rc = -ENODEV; | |
913 | goto exit; | |
914 | } | |
915 | ||
916 | if (info->attrs[NFC_ATTR_LLC_PARAM_LTO]) { | |
917 | if (dev->dep_link_up) { | |
918 | rc = -EINPROGRESS; | |
919 | goto exit; | |
920 | } | |
921 | ||
922 | local->lto = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_LTO]); | |
923 | } | |
924 | ||
925 | if (info->attrs[NFC_ATTR_LLC_PARAM_RW]) | |
926 | local->rw = rw; | |
927 | ||
928 | if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX]) | |
929 | local->miux = cpu_to_be16(miux); | |
930 | ||
931 | exit: | |
932 | device_unlock(&dev->dev); | |
933 | ||
934 | nfc_put_device(dev); | |
935 | ||
936 | return rc; | |
937 | } | |
938 | ||
d9b8d8e1 TE |
939 | static int nfc_genl_llc_sdreq(struct sk_buff *skb, struct genl_info *info) |
940 | { | |
941 | struct nfc_dev *dev; | |
942 | struct nfc_llcp_local *local; | |
943 | struct nlattr *attr, *sdp_attrs[NFC_SDP_ATTR_MAX+1]; | |
944 | u32 idx; | |
945 | u8 tid; | |
946 | char *uri; | |
947 | int rc = 0, rem; | |
948 | size_t uri_len, tlvs_len; | |
949 | struct hlist_head sdreq_list; | |
950 | struct nfc_llcp_sdp_tlv *sdreq; | |
951 | ||
952 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX] || | |
953 | !info->attrs[NFC_ATTR_LLC_SDP]) | |
954 | return -EINVAL; | |
955 | ||
956 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); | |
957 | ||
958 | dev = nfc_get_device(idx); | |
959 | if (!dev) { | |
960 | rc = -ENODEV; | |
961 | goto exit; | |
962 | } | |
963 | ||
964 | device_lock(&dev->dev); | |
965 | ||
966 | if (dev->dep_link_up == false) { | |
967 | rc = -ENOLINK; | |
968 | goto exit; | |
969 | } | |
970 | ||
971 | local = nfc_llcp_find_local(dev); | |
972 | if (!local) { | |
973 | nfc_put_device(dev); | |
974 | rc = -ENODEV; | |
975 | goto exit; | |
976 | } | |
977 | ||
978 | INIT_HLIST_HEAD(&sdreq_list); | |
979 | ||
980 | tlvs_len = 0; | |
981 | ||
982 | nla_for_each_nested(attr, info->attrs[NFC_ATTR_LLC_SDP], rem) { | |
983 | rc = nla_parse_nested(sdp_attrs, NFC_SDP_ATTR_MAX, attr, | |
984 | nfc_sdp_genl_policy); | |
985 | ||
986 | if (rc != 0) { | |
987 | rc = -EINVAL; | |
988 | goto exit; | |
989 | } | |
990 | ||
991 | if (!sdp_attrs[NFC_SDP_ATTR_URI]) | |
992 | continue; | |
993 | ||
994 | uri_len = nla_len(sdp_attrs[NFC_SDP_ATTR_URI]); | |
995 | if (uri_len == 0) | |
996 | continue; | |
997 | ||
998 | uri = nla_data(sdp_attrs[NFC_SDP_ATTR_URI]); | |
999 | if (uri == NULL || *uri == 0) | |
1000 | continue; | |
1001 | ||
1002 | tid = local->sdreq_next_tid++; | |
1003 | ||
1004 | sdreq = nfc_llcp_build_sdreq_tlv(tid, uri, uri_len); | |
1005 | if (sdreq == NULL) { | |
1006 | rc = -ENOMEM; | |
1007 | goto exit; | |
1008 | } | |
1009 | ||
1010 | tlvs_len += sdreq->tlv_len; | |
1011 | ||
1012 | hlist_add_head(&sdreq->node, &sdreq_list); | |
1013 | } | |
1014 | ||
1015 | if (hlist_empty(&sdreq_list)) { | |
1016 | rc = -EINVAL; | |
1017 | goto exit; | |
1018 | } | |
1019 | ||
1020 | rc = nfc_llcp_send_snl_sdreq(local, &sdreq_list, tlvs_len); | |
1021 | exit: | |
1022 | device_unlock(&dev->dev); | |
1023 | ||
1024 | nfc_put_device(dev); | |
1025 | ||
1026 | return rc; | |
1027 | } | |
1028 | ||
4d12b8b1 LRV |
1029 | static struct genl_ops nfc_genl_ops[] = { |
1030 | { | |
1031 | .cmd = NFC_CMD_GET_DEVICE, | |
1032 | .doit = nfc_genl_get_device, | |
1033 | .dumpit = nfc_genl_dump_devices, | |
1034 | .done = nfc_genl_dump_devices_done, | |
1035 | .policy = nfc_genl_policy, | |
8b3fe7b5 IE |
1036 | }, |
1037 | { | |
1038 | .cmd = NFC_CMD_DEV_UP, | |
1039 | .doit = nfc_genl_dev_up, | |
1040 | .policy = nfc_genl_policy, | |
1041 | }, | |
1042 | { | |
1043 | .cmd = NFC_CMD_DEV_DOWN, | |
1044 | .doit = nfc_genl_dev_down, | |
1045 | .policy = nfc_genl_policy, | |
4d12b8b1 LRV |
1046 | }, |
1047 | { | |
1048 | .cmd = NFC_CMD_START_POLL, | |
1049 | .doit = nfc_genl_start_poll, | |
1050 | .policy = nfc_genl_policy, | |
1051 | }, | |
1052 | { | |
1053 | .cmd = NFC_CMD_STOP_POLL, | |
1054 | .doit = nfc_genl_stop_poll, | |
1055 | .policy = nfc_genl_policy, | |
1056 | }, | |
1ed28f61 SO |
1057 | { |
1058 | .cmd = NFC_CMD_DEP_LINK_UP, | |
1059 | .doit = nfc_genl_dep_link_up, | |
1060 | .policy = nfc_genl_policy, | |
1061 | }, | |
1062 | { | |
1063 | .cmd = NFC_CMD_DEP_LINK_DOWN, | |
1064 | .doit = nfc_genl_dep_link_down, | |
1065 | .policy = nfc_genl_policy, | |
1066 | }, | |
4d12b8b1 LRV |
1067 | { |
1068 | .cmd = NFC_CMD_GET_TARGET, | |
1069 | .dumpit = nfc_genl_dump_targets, | |
1070 | .done = nfc_genl_dump_targets_done, | |
1071 | .policy = nfc_genl_policy, | |
1072 | }, | |
52feb444 TE |
1073 | { |
1074 | .cmd = NFC_CMD_LLC_GET_PARAMS, | |
1075 | .doit = nfc_genl_llc_get_params, | |
1076 | .policy = nfc_genl_policy, | |
1077 | }, | |
1078 | { | |
1079 | .cmd = NFC_CMD_LLC_SET_PARAMS, | |
1080 | .doit = nfc_genl_llc_set_params, | |
1081 | .policy = nfc_genl_policy, | |
1082 | }, | |
d9b8d8e1 TE |
1083 | { |
1084 | .cmd = NFC_CMD_LLC_SDREQ, | |
1085 | .doit = nfc_genl_llc_sdreq, | |
1086 | .policy = nfc_genl_policy, | |
1087 | }, | |
4d12b8b1 LRV |
1088 | }; |
1089 | ||
3c0cc8aa SJ |
1090 | |
1091 | struct urelease_work { | |
1092 | struct work_struct w; | |
c487606f | 1093 | int portid; |
3c0cc8aa SJ |
1094 | }; |
1095 | ||
1096 | static void nfc_urelease_event_work(struct work_struct *work) | |
4d12b8b1 | 1097 | { |
3c0cc8aa | 1098 | struct urelease_work *w = container_of(work, struct urelease_work, w); |
4d12b8b1 LRV |
1099 | struct class_dev_iter iter; |
1100 | struct nfc_dev *dev; | |
1101 | ||
c487606f | 1102 | pr_debug("portid %d\n", w->portid); |
4d12b8b1 | 1103 | |
3c0cc8aa | 1104 | mutex_lock(&nfc_devlist_mutex); |
4d12b8b1 LRV |
1105 | |
1106 | nfc_device_iter_init(&iter); | |
1107 | dev = nfc_device_iter_next(&iter); | |
1108 | ||
1109 | while (dev) { | |
3c0cc8aa SJ |
1110 | mutex_lock(&dev->genl_data.genl_data_mutex); |
1111 | ||
c487606f | 1112 | if (dev->genl_data.poll_req_portid == w->portid) { |
4d12b8b1 | 1113 | nfc_stop_poll(dev); |
15e47304 | 1114 | dev->genl_data.poll_req_portid = 0; |
4d12b8b1 | 1115 | } |
3c0cc8aa SJ |
1116 | |
1117 | mutex_unlock(&dev->genl_data.genl_data_mutex); | |
1118 | ||
4d12b8b1 LRV |
1119 | dev = nfc_device_iter_next(&iter); |
1120 | } | |
1121 | ||
1122 | nfc_device_iter_exit(&iter); | |
1123 | ||
3c0cc8aa SJ |
1124 | mutex_unlock(&nfc_devlist_mutex); |
1125 | ||
1126 | kfree(w); | |
1127 | } | |
1128 | ||
1129 | static int nfc_genl_rcv_nl_event(struct notifier_block *this, | |
1130 | unsigned long event, void *ptr) | |
1131 | { | |
1132 | struct netlink_notify *n = ptr; | |
1133 | struct urelease_work *w; | |
1134 | ||
1135 | if (event != NETLINK_URELEASE || n->protocol != NETLINK_GENERIC) | |
1136 | goto out; | |
1137 | ||
c487606f | 1138 | pr_debug("NETLINK_URELEASE event from id %d\n", n->portid); |
3c0cc8aa SJ |
1139 | |
1140 | w = kmalloc(sizeof(*w), GFP_ATOMIC); | |
1141 | if (w) { | |
1142 | INIT_WORK((struct work_struct *) w, nfc_urelease_event_work); | |
c487606f | 1143 | w->portid = n->portid; |
3c0cc8aa SJ |
1144 | schedule_work((struct work_struct *) w); |
1145 | } | |
1146 | ||
4d12b8b1 LRV |
1147 | out: |
1148 | return NOTIFY_DONE; | |
1149 | } | |
1150 | ||
1151 | void nfc_genl_data_init(struct nfc_genl_data *genl_data) | |
1152 | { | |
15e47304 | 1153 | genl_data->poll_req_portid = 0; |
4d12b8b1 LRV |
1154 | mutex_init(&genl_data->genl_data_mutex); |
1155 | } | |
1156 | ||
1157 | void nfc_genl_data_exit(struct nfc_genl_data *genl_data) | |
1158 | { | |
1159 | mutex_destroy(&genl_data->genl_data_mutex); | |
1160 | } | |
1161 | ||
1162 | static struct notifier_block nl_notifier = { | |
1163 | .notifier_call = nfc_genl_rcv_nl_event, | |
1164 | }; | |
1165 | ||
1166 | /** | |
1167 | * nfc_genl_init() - Initialize netlink interface | |
1168 | * | |
1169 | * This initialization function registers the nfc netlink family. | |
1170 | */ | |
1171 | int __init nfc_genl_init(void) | |
1172 | { | |
1173 | int rc; | |
1174 | ||
1175 | rc = genl_register_family_with_ops(&nfc_genl_family, nfc_genl_ops, | |
0a40acb2 | 1176 | ARRAY_SIZE(nfc_genl_ops)); |
4d12b8b1 LRV |
1177 | if (rc) |
1178 | return rc; | |
1179 | ||
1180 | rc = genl_register_mc_group(&nfc_genl_family, &nfc_genl_event_mcgrp); | |
1181 | ||
1182 | netlink_register_notifier(&nl_notifier); | |
1183 | ||
1184 | return rc; | |
1185 | } | |
1186 | ||
1187 | /** | |
1188 | * nfc_genl_exit() - Deinitialize netlink interface | |
1189 | * | |
1190 | * This exit function unregisters the nfc netlink family. | |
1191 | */ | |
1192 | void nfc_genl_exit(void) | |
1193 | { | |
1194 | netlink_unregister_notifier(&nl_notifier); | |
1195 | genl_unregister_family(&nfc_genl_family); | |
1196 | } |