]>
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 | ||
32 | static struct genl_multicast_group nfc_genl_event_mcgrp = { | |
33 | .name = NFC_GENL_MCAST_EVENT_NAME, | |
34 | }; | |
35 | ||
36 | struct genl_family nfc_genl_family = { | |
37 | .id = GENL_ID_GENERATE, | |
38 | .hdrsize = 0, | |
39 | .name = NFC_GENL_NAME, | |
40 | .version = NFC_GENL_VERSION, | |
41 | .maxattr = NFC_ATTR_MAX, | |
42 | }; | |
43 | ||
44 | static const struct nla_policy nfc_genl_policy[NFC_ATTR_MAX + 1] = { | |
45 | [NFC_ATTR_DEVICE_INDEX] = { .type = NLA_U32 }, | |
46 | [NFC_ATTR_DEVICE_NAME] = { .type = NLA_STRING, | |
47 | .len = NFC_DEVICE_NAME_MAXSIZE }, | |
48 | [NFC_ATTR_PROTOCOLS] = { .type = NLA_U32 }, | |
49 | }; | |
50 | ||
51 | static int nfc_genl_send_target(struct sk_buff *msg, struct nfc_target *target, | |
52 | struct netlink_callback *cb, int flags) | |
53 | { | |
54 | void *hdr; | |
55 | ||
4d12b8b1 LRV |
56 | hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq, |
57 | &nfc_genl_family, flags, NFC_CMD_GET_TARGET); | |
58 | if (!hdr) | |
59 | return -EMSGSIZE; | |
60 | ||
61 | genl_dump_check_consistent(cb, hdr, &nfc_genl_family); | |
62 | ||
63 | NLA_PUT_U32(msg, NFC_ATTR_TARGET_INDEX, target->idx); | |
64 | NLA_PUT_U32(msg, NFC_ATTR_PROTOCOLS, | |
65 | target->supported_protocols); | |
66 | NLA_PUT_U16(msg, NFC_ATTR_TARGET_SENS_RES, target->sens_res); | |
67 | NLA_PUT_U8(msg, NFC_ATTR_TARGET_SEL_RES, target->sel_res); | |
68 | ||
69 | return genlmsg_end(msg, hdr); | |
70 | ||
71 | nla_put_failure: | |
72 | genlmsg_cancel(msg, hdr); | |
73 | return -EMSGSIZE; | |
74 | } | |
75 | ||
76 | static struct nfc_dev *__get_device_from_cb(struct netlink_callback *cb) | |
77 | { | |
78 | struct nfc_dev *dev; | |
79 | int rc; | |
80 | u32 idx; | |
81 | ||
82 | rc = nlmsg_parse(cb->nlh, GENL_HDRLEN + nfc_genl_family.hdrsize, | |
83 | nfc_genl_family.attrbuf, | |
84 | nfc_genl_family.maxattr, | |
85 | nfc_genl_policy); | |
86 | if (rc < 0) | |
87 | return ERR_PTR(rc); | |
88 | ||
89 | if (!nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX]) | |
90 | return ERR_PTR(-EINVAL); | |
91 | ||
92 | idx = nla_get_u32(nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX]); | |
93 | ||
94 | dev = nfc_get_device(idx); | |
95 | if (!dev) | |
96 | return ERR_PTR(-ENODEV); | |
97 | ||
98 | return dev; | |
99 | } | |
100 | ||
101 | static int nfc_genl_dump_targets(struct sk_buff *skb, | |
102 | struct netlink_callback *cb) | |
103 | { | |
104 | int i = cb->args[0]; | |
105 | struct nfc_dev *dev = (struct nfc_dev *) cb->args[1]; | |
106 | int rc; | |
107 | ||
4d12b8b1 LRV |
108 | if (!dev) { |
109 | dev = __get_device_from_cb(cb); | |
110 | if (IS_ERR(dev)) | |
111 | return PTR_ERR(dev); | |
112 | ||
113 | cb->args[1] = (long) dev; | |
114 | } | |
115 | ||
116 | spin_lock_bh(&dev->targets_lock); | |
117 | ||
118 | cb->seq = dev->targets_generation; | |
119 | ||
120 | while (i < dev->n_targets) { | |
121 | rc = nfc_genl_send_target(skb, &dev->targets[i], cb, | |
122 | NLM_F_MULTI); | |
123 | if (rc < 0) | |
124 | break; | |
125 | ||
126 | i++; | |
127 | } | |
128 | ||
129 | spin_unlock_bh(&dev->targets_lock); | |
130 | ||
131 | cb->args[0] = i; | |
132 | ||
133 | return skb->len; | |
134 | } | |
135 | ||
136 | static int nfc_genl_dump_targets_done(struct netlink_callback *cb) | |
137 | { | |
138 | struct nfc_dev *dev = (struct nfc_dev *) cb->args[1]; | |
139 | ||
4d12b8b1 LRV |
140 | if (dev) |
141 | nfc_put_device(dev); | |
142 | ||
143 | return 0; | |
144 | } | |
145 | ||
146 | int nfc_genl_targets_found(struct nfc_dev *dev) | |
147 | { | |
148 | struct sk_buff *msg; | |
149 | void *hdr; | |
150 | ||
4d12b8b1 LRV |
151 | dev->genl_data.poll_req_pid = 0; |
152 | ||
153 | msg = nlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC); | |
154 | if (!msg) | |
155 | return -ENOMEM; | |
156 | ||
157 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, | |
158 | NFC_EVENT_TARGETS_FOUND); | |
159 | if (!hdr) | |
160 | goto free_msg; | |
161 | ||
162 | NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx); | |
163 | ||
164 | genlmsg_end(msg, hdr); | |
165 | ||
166 | return genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC); | |
167 | ||
168 | nla_put_failure: | |
169 | genlmsg_cancel(msg, hdr); | |
170 | free_msg: | |
171 | nlmsg_free(msg); | |
172 | return -EMSGSIZE; | |
173 | } | |
174 | ||
175 | int nfc_genl_device_added(struct nfc_dev *dev) | |
176 | { | |
177 | struct sk_buff *msg; | |
178 | void *hdr; | |
179 | ||
4d12b8b1 LRV |
180 | msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
181 | if (!msg) | |
182 | return -ENOMEM; | |
183 | ||
184 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, | |
185 | NFC_EVENT_DEVICE_ADDED); | |
186 | if (!hdr) | |
187 | goto free_msg; | |
188 | ||
189 | NLA_PUT_STRING(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)); | |
190 | NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx); | |
191 | NLA_PUT_U32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols); | |
192 | ||
193 | genlmsg_end(msg, hdr); | |
194 | ||
195 | genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL); | |
196 | ||
197 | return 0; | |
198 | ||
199 | nla_put_failure: | |
200 | genlmsg_cancel(msg, hdr); | |
201 | free_msg: | |
202 | nlmsg_free(msg); | |
203 | return -EMSGSIZE; | |
204 | } | |
205 | ||
206 | int nfc_genl_device_removed(struct nfc_dev *dev) | |
207 | { | |
208 | struct sk_buff *msg; | |
209 | void *hdr; | |
210 | ||
4d12b8b1 LRV |
211 | msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
212 | if (!msg) | |
213 | return -ENOMEM; | |
214 | ||
215 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, | |
216 | NFC_EVENT_DEVICE_REMOVED); | |
217 | if (!hdr) | |
218 | goto free_msg; | |
219 | ||
220 | NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx); | |
221 | ||
222 | genlmsg_end(msg, hdr); | |
223 | ||
224 | genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL); | |
225 | ||
226 | return 0; | |
227 | ||
228 | nla_put_failure: | |
229 | genlmsg_cancel(msg, hdr); | |
230 | free_msg: | |
231 | nlmsg_free(msg); | |
232 | return -EMSGSIZE; | |
233 | } | |
234 | ||
235 | static int nfc_genl_send_device(struct sk_buff *msg, struct nfc_dev *dev, | |
236 | u32 pid, u32 seq, | |
237 | struct netlink_callback *cb, | |
238 | int flags) | |
239 | { | |
240 | void *hdr; | |
241 | ||
4d12b8b1 LRV |
242 | hdr = genlmsg_put(msg, pid, seq, &nfc_genl_family, flags, |
243 | NFC_CMD_GET_DEVICE); | |
244 | if (!hdr) | |
245 | return -EMSGSIZE; | |
246 | ||
247 | if (cb) | |
248 | genl_dump_check_consistent(cb, hdr, &nfc_genl_family); | |
249 | ||
250 | NLA_PUT_STRING(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)); | |
251 | NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx); | |
252 | NLA_PUT_U32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols); | |
253 | ||
254 | return genlmsg_end(msg, hdr); | |
255 | ||
256 | nla_put_failure: | |
257 | genlmsg_cancel(msg, hdr); | |
258 | return -EMSGSIZE; | |
259 | } | |
260 | ||
261 | static int nfc_genl_dump_devices(struct sk_buff *skb, | |
262 | struct netlink_callback *cb) | |
263 | { | |
264 | struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0]; | |
265 | struct nfc_dev *dev = (struct nfc_dev *) cb->args[1]; | |
266 | bool first_call = false; | |
267 | ||
4d12b8b1 LRV |
268 | if (!iter) { |
269 | first_call = true; | |
270 | iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL); | |
271 | if (!iter) | |
272 | return -ENOMEM; | |
273 | cb->args[0] = (long) iter; | |
274 | } | |
275 | ||
276 | mutex_lock(&nfc_devlist_mutex); | |
277 | ||
278 | cb->seq = nfc_devlist_generation; | |
279 | ||
280 | if (first_call) { | |
281 | nfc_device_iter_init(iter); | |
282 | dev = nfc_device_iter_next(iter); | |
283 | } | |
284 | ||
285 | while (dev) { | |
286 | int rc; | |
287 | ||
288 | rc = nfc_genl_send_device(skb, dev, NETLINK_CB(cb->skb).pid, | |
289 | cb->nlh->nlmsg_seq, | |
290 | cb, NLM_F_MULTI); | |
291 | if (rc < 0) | |
292 | break; | |
293 | ||
294 | dev = nfc_device_iter_next(iter); | |
295 | } | |
296 | ||
297 | mutex_unlock(&nfc_devlist_mutex); | |
298 | ||
299 | cb->args[1] = (long) dev; | |
300 | ||
301 | return skb->len; | |
302 | } | |
303 | ||
304 | static int nfc_genl_dump_devices_done(struct netlink_callback *cb) | |
305 | { | |
306 | struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0]; | |
307 | ||
4d12b8b1 LRV |
308 | nfc_device_iter_exit(iter); |
309 | kfree(iter); | |
310 | ||
311 | return 0; | |
312 | } | |
313 | ||
314 | static int nfc_genl_get_device(struct sk_buff *skb, struct genl_info *info) | |
315 | { | |
316 | struct sk_buff *msg; | |
317 | struct nfc_dev *dev; | |
318 | u32 idx; | |
319 | int rc = -ENOBUFS; | |
320 | ||
4d12b8b1 LRV |
321 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) |
322 | return -EINVAL; | |
323 | ||
324 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); | |
325 | ||
326 | dev = nfc_get_device(idx); | |
327 | if (!dev) | |
328 | return -ENODEV; | |
329 | ||
330 | msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); | |
331 | if (!msg) { | |
332 | rc = -ENOMEM; | |
333 | goto out_putdev; | |
334 | } | |
335 | ||
336 | rc = nfc_genl_send_device(msg, dev, info->snd_pid, info->snd_seq, | |
337 | NULL, 0); | |
338 | if (rc < 0) | |
339 | goto out_free; | |
340 | ||
341 | nfc_put_device(dev); | |
342 | ||
343 | return genlmsg_reply(msg, info); | |
344 | ||
345 | out_free: | |
346 | nlmsg_free(msg); | |
347 | out_putdev: | |
348 | nfc_put_device(dev); | |
349 | return rc; | |
350 | } | |
351 | ||
8b3fe7b5 IE |
352 | static int nfc_genl_dev_up(struct sk_buff *skb, struct genl_info *info) |
353 | { | |
354 | struct nfc_dev *dev; | |
355 | int rc; | |
356 | u32 idx; | |
357 | ||
8b3fe7b5 IE |
358 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) |
359 | return -EINVAL; | |
360 | ||
361 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); | |
362 | ||
363 | dev = nfc_get_device(idx); | |
364 | if (!dev) | |
365 | return -ENODEV; | |
366 | ||
367 | rc = nfc_dev_up(dev); | |
368 | ||
369 | nfc_put_device(dev); | |
370 | return rc; | |
371 | } | |
372 | ||
373 | static int nfc_genl_dev_down(struct sk_buff *skb, struct genl_info *info) | |
374 | { | |
375 | struct nfc_dev *dev; | |
376 | int rc; | |
377 | u32 idx; | |
378 | ||
8b3fe7b5 IE |
379 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) |
380 | return -EINVAL; | |
381 | ||
382 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); | |
383 | ||
384 | dev = nfc_get_device(idx); | |
385 | if (!dev) | |
386 | return -ENODEV; | |
387 | ||
388 | rc = nfc_dev_down(dev); | |
389 | ||
390 | nfc_put_device(dev); | |
391 | return rc; | |
392 | } | |
393 | ||
4d12b8b1 LRV |
394 | static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info) |
395 | { | |
396 | struct nfc_dev *dev; | |
397 | int rc; | |
398 | u32 idx; | |
399 | u32 protocols; | |
400 | ||
4d12b8b1 LRV |
401 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX] || |
402 | !info->attrs[NFC_ATTR_PROTOCOLS]) | |
403 | return -EINVAL; | |
404 | ||
405 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); | |
406 | protocols = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]); | |
407 | ||
408 | dev = nfc_get_device(idx); | |
409 | if (!dev) | |
410 | return -ENODEV; | |
411 | ||
412 | mutex_lock(&dev->genl_data.genl_data_mutex); | |
413 | ||
414 | rc = nfc_start_poll(dev, protocols); | |
415 | if (!rc) | |
416 | dev->genl_data.poll_req_pid = info->snd_pid; | |
417 | ||
418 | mutex_unlock(&dev->genl_data.genl_data_mutex); | |
419 | ||
420 | nfc_put_device(dev); | |
421 | return rc; | |
422 | } | |
423 | ||
424 | static int nfc_genl_stop_poll(struct sk_buff *skb, struct genl_info *info) | |
425 | { | |
426 | struct nfc_dev *dev; | |
427 | int rc; | |
428 | u32 idx; | |
429 | ||
4d12b8b1 LRV |
430 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) |
431 | return -EINVAL; | |
432 | ||
433 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); | |
434 | ||
435 | dev = nfc_get_device(idx); | |
436 | if (!dev) | |
437 | return -ENODEV; | |
438 | ||
439 | mutex_lock(&dev->genl_data.genl_data_mutex); | |
440 | ||
441 | if (dev->genl_data.poll_req_pid != info->snd_pid) { | |
442 | rc = -EBUSY; | |
443 | goto out; | |
444 | } | |
445 | ||
446 | rc = nfc_stop_poll(dev); | |
447 | dev->genl_data.poll_req_pid = 0; | |
448 | ||
449 | out: | |
450 | mutex_unlock(&dev->genl_data.genl_data_mutex); | |
451 | nfc_put_device(dev); | |
452 | return rc; | |
453 | } | |
454 | ||
455 | static struct genl_ops nfc_genl_ops[] = { | |
456 | { | |
457 | .cmd = NFC_CMD_GET_DEVICE, | |
458 | .doit = nfc_genl_get_device, | |
459 | .dumpit = nfc_genl_dump_devices, | |
460 | .done = nfc_genl_dump_devices_done, | |
461 | .policy = nfc_genl_policy, | |
8b3fe7b5 IE |
462 | }, |
463 | { | |
464 | .cmd = NFC_CMD_DEV_UP, | |
465 | .doit = nfc_genl_dev_up, | |
466 | .policy = nfc_genl_policy, | |
467 | }, | |
468 | { | |
469 | .cmd = NFC_CMD_DEV_DOWN, | |
470 | .doit = nfc_genl_dev_down, | |
471 | .policy = nfc_genl_policy, | |
4d12b8b1 LRV |
472 | }, |
473 | { | |
474 | .cmd = NFC_CMD_START_POLL, | |
475 | .doit = nfc_genl_start_poll, | |
476 | .policy = nfc_genl_policy, | |
477 | }, | |
478 | { | |
479 | .cmd = NFC_CMD_STOP_POLL, | |
480 | .doit = nfc_genl_stop_poll, | |
481 | .policy = nfc_genl_policy, | |
482 | }, | |
483 | { | |
484 | .cmd = NFC_CMD_GET_TARGET, | |
485 | .dumpit = nfc_genl_dump_targets, | |
486 | .done = nfc_genl_dump_targets_done, | |
487 | .policy = nfc_genl_policy, | |
488 | }, | |
489 | }; | |
490 | ||
491 | static int nfc_genl_rcv_nl_event(struct notifier_block *this, | |
492 | unsigned long event, void *ptr) | |
493 | { | |
494 | struct netlink_notify *n = ptr; | |
495 | struct class_dev_iter iter; | |
496 | struct nfc_dev *dev; | |
497 | ||
498 | if (event != NETLINK_URELEASE || n->protocol != NETLINK_GENERIC) | |
499 | goto out; | |
500 | ||
20c239c1 | 501 | pr_debug("NETLINK_URELEASE event from id %d\n", n->pid); |
4d12b8b1 LRV |
502 | |
503 | nfc_device_iter_init(&iter); | |
504 | dev = nfc_device_iter_next(&iter); | |
505 | ||
506 | while (dev) { | |
4d12b8b1 LRV |
507 | if (dev->genl_data.poll_req_pid == n->pid) { |
508 | nfc_stop_poll(dev); | |
509 | dev->genl_data.poll_req_pid = 0; | |
510 | } | |
4d12b8b1 LRV |
511 | dev = nfc_device_iter_next(&iter); |
512 | } | |
513 | ||
514 | nfc_device_iter_exit(&iter); | |
515 | ||
516 | out: | |
517 | return NOTIFY_DONE; | |
518 | } | |
519 | ||
520 | void nfc_genl_data_init(struct nfc_genl_data *genl_data) | |
521 | { | |
522 | genl_data->poll_req_pid = 0; | |
523 | mutex_init(&genl_data->genl_data_mutex); | |
524 | } | |
525 | ||
526 | void nfc_genl_data_exit(struct nfc_genl_data *genl_data) | |
527 | { | |
528 | mutex_destroy(&genl_data->genl_data_mutex); | |
529 | } | |
530 | ||
531 | static struct notifier_block nl_notifier = { | |
532 | .notifier_call = nfc_genl_rcv_nl_event, | |
533 | }; | |
534 | ||
535 | /** | |
536 | * nfc_genl_init() - Initialize netlink interface | |
537 | * | |
538 | * This initialization function registers the nfc netlink family. | |
539 | */ | |
540 | int __init nfc_genl_init(void) | |
541 | { | |
542 | int rc; | |
543 | ||
544 | rc = genl_register_family_with_ops(&nfc_genl_family, nfc_genl_ops, | |
545 | ARRAY_SIZE(nfc_genl_ops)); | |
546 | if (rc) | |
547 | return rc; | |
548 | ||
549 | rc = genl_register_mc_group(&nfc_genl_family, &nfc_genl_event_mcgrp); | |
550 | ||
551 | netlink_register_notifier(&nl_notifier); | |
552 | ||
553 | return rc; | |
554 | } | |
555 | ||
556 | /** | |
557 | * nfc_genl_exit() - Deinitialize netlink interface | |
558 | * | |
559 | * This exit function unregisters the nfc netlink family. | |
560 | */ | |
561 | void nfc_genl_exit(void) | |
562 | { | |
563 | netlink_unregister_notifier(&nl_notifier); | |
564 | genl_unregister_family(&nfc_genl_family); | |
565 | } |