Commit | Line | Data |
---|---|---|
3e256b8f 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__ |
ed1e0ad8 | 25 | |
3e256b8f LRV |
26 | #include <linux/init.h> |
27 | #include <linux/kernel.h> | |
28 | #include <linux/module.h> | |
29 | #include <linux/slab.h> | |
7c7cd3bf | 30 | #include <linux/nfc.h> |
3e256b8f | 31 | |
5df16cad SO |
32 | #include <net/genetlink.h> |
33 | ||
3e256b8f LRV |
34 | #include "nfc.h" |
35 | ||
36 | #define VERSION "0.1" | |
37 | ||
c8d56ae7 EL |
38 | #define NFC_CHECK_PRES_FREQ_MS 2000 |
39 | ||
3e256b8f LRV |
40 | int nfc_devlist_generation; |
41 | DEFINE_MUTEX(nfc_devlist_mutex); | |
42 | ||
7eda8b8e SO |
43 | /* NFC device ID bitmap */ |
44 | static DEFINE_IDA(nfc_index_ida); | |
45 | ||
8b3fe7b5 IE |
46 | /** |
47 | * nfc_dev_up - turn on the NFC device | |
48 | * | |
49 | * @dev: The nfc device to be turned on | |
50 | * | |
51 | * The device remains up until the nfc_dev_down function is called. | |
52 | */ | |
53 | int nfc_dev_up(struct nfc_dev *dev) | |
54 | { | |
55 | int rc = 0; | |
56 | ||
20c239c1 | 57 | pr_debug("dev_name=%s\n", dev_name(&dev->dev)); |
8b3fe7b5 IE |
58 | |
59 | device_lock(&dev->dev); | |
60 | ||
61 | if (!device_is_registered(&dev->dev)) { | |
62 | rc = -ENODEV; | |
63 | goto error; | |
64 | } | |
65 | ||
66 | if (dev->dev_up) { | |
67 | rc = -EALREADY; | |
68 | goto error; | |
69 | } | |
70 | ||
71 | if (dev->ops->dev_up) | |
72 | rc = dev->ops->dev_up(dev); | |
73 | ||
74 | if (!rc) | |
75 | dev->dev_up = true; | |
76 | ||
77 | error: | |
78 | device_unlock(&dev->dev); | |
79 | return rc; | |
80 | } | |
81 | ||
82 | /** | |
83 | * nfc_dev_down - turn off the NFC device | |
84 | * | |
85 | * @dev: The nfc device to be turned off | |
86 | */ | |
87 | int nfc_dev_down(struct nfc_dev *dev) | |
88 | { | |
89 | int rc = 0; | |
90 | ||
20c239c1 | 91 | pr_debug("dev_name=%s\n", dev_name(&dev->dev)); |
8b3fe7b5 IE |
92 | |
93 | device_lock(&dev->dev); | |
94 | ||
95 | if (!device_is_registered(&dev->dev)) { | |
96 | rc = -ENODEV; | |
97 | goto error; | |
98 | } | |
99 | ||
100 | if (!dev->dev_up) { | |
101 | rc = -EALREADY; | |
102 | goto error; | |
103 | } | |
104 | ||
90099433 | 105 | if (dev->polling || dev->active_target) { |
8b3fe7b5 IE |
106 | rc = -EBUSY; |
107 | goto error; | |
108 | } | |
109 | ||
110 | if (dev->ops->dev_down) | |
111 | dev->ops->dev_down(dev); | |
112 | ||
113 | dev->dev_up = false; | |
114 | ||
115 | error: | |
116 | device_unlock(&dev->dev); | |
117 | return rc; | |
118 | } | |
119 | ||
3e256b8f LRV |
120 | /** |
121 | * nfc_start_poll - start polling for nfc targets | |
122 | * | |
123 | * @dev: The nfc device that must start polling | |
124 | * @protocols: bitset of nfc protocols that must be used for polling | |
125 | * | |
126 | * The device remains polling for targets until a target is found or | |
127 | * the nfc_stop_poll function is called. | |
128 | */ | |
fe7c5800 | 129 | int nfc_start_poll(struct nfc_dev *dev, u32 im_protocols, u32 tm_protocols) |
3e256b8f LRV |
130 | { |
131 | int rc; | |
132 | ||
fe7c5800 SO |
133 | pr_debug("dev_name %s initiator protocols 0x%x target protocols 0x%x\n", |
134 | dev_name(&dev->dev), im_protocols, tm_protocols); | |
3e256b8f | 135 | |
fe7c5800 | 136 | if (!im_protocols && !tm_protocols) |
3e256b8f LRV |
137 | return -EINVAL; |
138 | ||
139 | device_lock(&dev->dev); | |
140 | ||
141 | if (!device_is_registered(&dev->dev)) { | |
142 | rc = -ENODEV; | |
143 | goto error; | |
144 | } | |
145 | ||
146 | if (dev->polling) { | |
147 | rc = -EBUSY; | |
148 | goto error; | |
149 | } | |
150 | ||
fe7c5800 | 151 | rc = dev->ops->start_poll(dev, im_protocols, tm_protocols); |
f212ad5e | 152 | if (!rc) { |
3e256b8f | 153 | dev->polling = true; |
f212ad5e SO |
154 | dev->rf_mode = NFC_RF_NONE; |
155 | } | |
3e256b8f LRV |
156 | |
157 | error: | |
158 | device_unlock(&dev->dev); | |
159 | return rc; | |
160 | } | |
161 | ||
162 | /** | |
163 | * nfc_stop_poll - stop polling for nfc targets | |
164 | * | |
165 | * @dev: The nfc device that must stop polling | |
166 | */ | |
167 | int nfc_stop_poll(struct nfc_dev *dev) | |
168 | { | |
169 | int rc = 0; | |
170 | ||
20c239c1 | 171 | pr_debug("dev_name=%s\n", dev_name(&dev->dev)); |
3e256b8f LRV |
172 | |
173 | device_lock(&dev->dev); | |
174 | ||
175 | if (!device_is_registered(&dev->dev)) { | |
176 | rc = -ENODEV; | |
177 | goto error; | |
178 | } | |
179 | ||
180 | if (!dev->polling) { | |
181 | rc = -EINVAL; | |
182 | goto error; | |
183 | } | |
184 | ||
185 | dev->ops->stop_poll(dev); | |
186 | dev->polling = false; | |
5bcf099c | 187 | dev->rf_mode = NFC_RF_NONE; |
3e256b8f LRV |
188 | |
189 | error: | |
190 | device_unlock(&dev->dev); | |
191 | return rc; | |
192 | } | |
193 | ||
90099433 EL |
194 | static struct nfc_target *nfc_find_target(struct nfc_dev *dev, u32 target_idx) |
195 | { | |
196 | int i; | |
197 | ||
198 | if (dev->n_targets == 0) | |
199 | return NULL; | |
200 | ||
0f450772 | 201 | for (i = 0; i < dev->n_targets; i++) { |
90099433 EL |
202 | if (dev->targets[i].idx == target_idx) |
203 | return &dev->targets[i]; | |
204 | } | |
205 | ||
206 | return NULL; | |
207 | } | |
208 | ||
47807d3d | 209 | int nfc_dep_link_up(struct nfc_dev *dev, int target_index, u8 comm_mode) |
1ed28f61 SO |
210 | { |
211 | int rc = 0; | |
47807d3d SO |
212 | u8 *gb; |
213 | size_t gb_len; | |
90099433 | 214 | struct nfc_target *target; |
1ed28f61 | 215 | |
47807d3d | 216 | pr_debug("dev_name=%s comm %d\n", dev_name(&dev->dev), comm_mode); |
1ed28f61 SO |
217 | |
218 | if (!dev->ops->dep_link_up) | |
219 | return -EOPNOTSUPP; | |
220 | ||
221 | device_lock(&dev->dev); | |
222 | ||
223 | if (!device_is_registered(&dev->dev)) { | |
224 | rc = -ENODEV; | |
225 | goto error; | |
226 | } | |
227 | ||
228 | if (dev->dep_link_up == true) { | |
229 | rc = -EALREADY; | |
230 | goto error; | |
231 | } | |
232 | ||
47807d3d SO |
233 | gb = nfc_llcp_general_bytes(dev, &gb_len); |
234 | if (gb_len > NFC_MAX_GT_LEN) { | |
235 | rc = -EINVAL; | |
236 | goto error; | |
237 | } | |
238 | ||
90099433 EL |
239 | target = nfc_find_target(dev, target_index); |
240 | if (target == NULL) { | |
241 | rc = -ENOTCONN; | |
242 | goto error; | |
243 | } | |
244 | ||
245 | rc = dev->ops->dep_link_up(dev, target, comm_mode, gb, gb_len); | |
f212ad5e | 246 | if (!rc) { |
90099433 | 247 | dev->active_target = target; |
f212ad5e SO |
248 | dev->rf_mode = NFC_RF_INITIATOR; |
249 | } | |
1ed28f61 SO |
250 | |
251 | error: | |
252 | device_unlock(&dev->dev); | |
253 | return rc; | |
254 | } | |
255 | ||
256 | int nfc_dep_link_down(struct nfc_dev *dev) | |
257 | { | |
258 | int rc = 0; | |
259 | ||
260 | pr_debug("dev_name=%s\n", dev_name(&dev->dev)); | |
261 | ||
262 | if (!dev->ops->dep_link_down) | |
263 | return -EOPNOTSUPP; | |
264 | ||
265 | device_lock(&dev->dev); | |
266 | ||
267 | if (!device_is_registered(&dev->dev)) { | |
268 | rc = -ENODEV; | |
269 | goto error; | |
270 | } | |
271 | ||
272 | if (dev->dep_link_up == false) { | |
273 | rc = -EALREADY; | |
274 | goto error; | |
275 | } | |
276 | ||
1ed28f61 SO |
277 | rc = dev->ops->dep_link_down(dev); |
278 | if (!rc) { | |
279 | dev->dep_link_up = false; | |
90099433 | 280 | dev->active_target = NULL; |
5bcf099c | 281 | dev->rf_mode = NFC_RF_NONE; |
d646960f | 282 | nfc_llcp_mac_is_down(dev); |
1ed28f61 SO |
283 | nfc_genl_dep_link_down_event(dev); |
284 | } | |
285 | ||
286 | error: | |
287 | device_unlock(&dev->dev); | |
5bcf099c | 288 | |
1ed28f61 SO |
289 | return rc; |
290 | } | |
291 | ||
292 | int nfc_dep_link_is_up(struct nfc_dev *dev, u32 target_idx, | |
0a40acb2 | 293 | u8 comm_mode, u8 rf_mode) |
1ed28f61 SO |
294 | { |
295 | dev->dep_link_up = true; | |
1ed28f61 | 296 | |
d646960f SO |
297 | nfc_llcp_mac_is_up(dev, target_idx, comm_mode, rf_mode); |
298 | ||
1ed28f61 SO |
299 | return nfc_genl_dep_link_up_event(dev, target_idx, comm_mode, rf_mode); |
300 | } | |
301 | EXPORT_SYMBOL(nfc_dep_link_is_up); | |
302 | ||
3e256b8f LRV |
303 | /** |
304 | * nfc_activate_target - prepare the target for data exchange | |
305 | * | |
306 | * @dev: The nfc device that found the target | |
307 | * @target_idx: index of the target that must be activated | |
308 | * @protocol: nfc protocol that will be used for data exchange | |
309 | */ | |
310 | int nfc_activate_target(struct nfc_dev *dev, u32 target_idx, u32 protocol) | |
311 | { | |
312 | int rc; | |
90099433 | 313 | struct nfc_target *target; |
3e256b8f | 314 | |
20c239c1 JP |
315 | pr_debug("dev_name=%s target_idx=%u protocol=%u\n", |
316 | dev_name(&dev->dev), target_idx, protocol); | |
3e256b8f LRV |
317 | |
318 | device_lock(&dev->dev); | |
319 | ||
320 | if (!device_is_registered(&dev->dev)) { | |
321 | rc = -ENODEV; | |
322 | goto error; | |
323 | } | |
324 | ||
90099433 EL |
325 | if (dev->active_target) { |
326 | rc = -EBUSY; | |
327 | goto error; | |
328 | } | |
329 | ||
330 | target = nfc_find_target(dev, target_idx); | |
331 | if (target == NULL) { | |
332 | rc = -ENOTCONN; | |
333 | goto error; | |
334 | } | |
335 | ||
336 | rc = dev->ops->activate_target(dev, target, protocol); | |
c8d56ae7 | 337 | if (!rc) { |
90099433 | 338 | dev->active_target = target; |
f212ad5e | 339 | dev->rf_mode = NFC_RF_INITIATOR; |
3e256b8f | 340 | |
f0c91038 | 341 | if (dev->ops->check_presence && !dev->shutting_down) |
c8d56ae7 EL |
342 | mod_timer(&dev->check_pres_timer, jiffies + |
343 | msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS)); | |
344 | } | |
3e256b8f LRV |
345 | |
346 | error: | |
347 | device_unlock(&dev->dev); | |
348 | return rc; | |
349 | } | |
350 | ||
351 | /** | |
352 | * nfc_deactivate_target - deactivate a nfc target | |
353 | * | |
354 | * @dev: The nfc device that found the target | |
355 | * @target_idx: index of the target that must be deactivated | |
356 | */ | |
357 | int nfc_deactivate_target(struct nfc_dev *dev, u32 target_idx) | |
358 | { | |
359 | int rc = 0; | |
360 | ||
20c239c1 JP |
361 | pr_debug("dev_name=%s target_idx=%u\n", |
362 | dev_name(&dev->dev), target_idx); | |
3e256b8f LRV |
363 | |
364 | device_lock(&dev->dev); | |
365 | ||
366 | if (!device_is_registered(&dev->dev)) { | |
367 | rc = -ENODEV; | |
368 | goto error; | |
369 | } | |
370 | ||
90099433 EL |
371 | if (dev->active_target == NULL) { |
372 | rc = -ENOTCONN; | |
373 | goto error; | |
374 | } | |
375 | ||
376 | if (dev->active_target->idx != target_idx) { | |
377 | rc = -ENOTCONN; | |
378 | goto error; | |
379 | } | |
380 | ||
c8d56ae7 EL |
381 | if (dev->ops->check_presence) |
382 | del_timer_sync(&dev->check_pres_timer); | |
383 | ||
90099433 EL |
384 | dev->ops->deactivate_target(dev, dev->active_target); |
385 | dev->active_target = NULL; | |
3e256b8f LRV |
386 | |
387 | error: | |
388 | device_unlock(&dev->dev); | |
389 | return rc; | |
390 | } | |
391 | ||
392 | /** | |
393 | * nfc_data_exchange - transceive data | |
394 | * | |
395 | * @dev: The nfc device that found the target | |
396 | * @target_idx: index of the target | |
397 | * @skb: data to be sent | |
398 | * @cb: callback called when the response is received | |
399 | * @cb_context: parameter for the callback function | |
400 | * | |
401 | * The user must wait for the callback before calling this function again. | |
402 | */ | |
0a40acb2 SO |
403 | int nfc_data_exchange(struct nfc_dev *dev, u32 target_idx, struct sk_buff *skb, |
404 | data_exchange_cb_t cb, void *cb_context) | |
3e256b8f LRV |
405 | { |
406 | int rc; | |
407 | ||
20c239c1 JP |
408 | pr_debug("dev_name=%s target_idx=%u skb->len=%u\n", |
409 | dev_name(&dev->dev), target_idx, skb->len); | |
3e256b8f LRV |
410 | |
411 | device_lock(&dev->dev); | |
412 | ||
413 | if (!device_is_registered(&dev->dev)) { | |
414 | rc = -ENODEV; | |
415 | kfree_skb(skb); | |
416 | goto error; | |
417 | } | |
418 | ||
be9ae4ce SO |
419 | if (dev->rf_mode == NFC_RF_INITIATOR && dev->active_target != NULL) { |
420 | if (dev->active_target->idx != target_idx) { | |
421 | rc = -EADDRNOTAVAIL; | |
422 | kfree_skb(skb); | |
423 | goto error; | |
424 | } | |
144612ca | 425 | |
be9ae4ce SO |
426 | if (dev->ops->check_presence) |
427 | del_timer_sync(&dev->check_pres_timer); | |
428 | ||
429 | rc = dev->ops->im_transceive(dev, dev->active_target, skb, cb, | |
430 | cb_context); | |
431 | ||
f0c91038 | 432 | if (!rc && dev->ops->check_presence && !dev->shutting_down) |
be9ae4ce SO |
433 | mod_timer(&dev->check_pres_timer, jiffies + |
434 | msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS)); | |
435 | } else if (dev->rf_mode == NFC_RF_TARGET && dev->ops->tm_send != NULL) { | |
436 | rc = dev->ops->tm_send(dev, skb); | |
437 | } else { | |
438 | rc = -ENOTCONN; | |
144612ca EL |
439 | kfree_skb(skb); |
440 | goto error; | |
441 | } | |
442 | ||
c8d56ae7 | 443 | |
3e256b8f LRV |
444 | error: |
445 | device_unlock(&dev->dev); | |
446 | return rc; | |
447 | } | |
448 | ||
541d920b SO |
449 | int nfc_set_remote_general_bytes(struct nfc_dev *dev, u8 *gb, u8 gb_len) |
450 | { | |
0a40acb2 | 451 | pr_debug("dev_name=%s gb_len=%d\n", dev_name(&dev->dev), gb_len); |
541d920b SO |
452 | |
453 | if (gb_len > NFC_MAX_GT_LEN) | |
454 | return -EINVAL; | |
455 | ||
d646960f | 456 | return nfc_llcp_set_remote_gb(dev, gb, gb_len); |
541d920b SO |
457 | } |
458 | EXPORT_SYMBOL(nfc_set_remote_general_bytes); | |
459 | ||
ab73b751 SO |
460 | u8 *nfc_get_local_general_bytes(struct nfc_dev *dev, size_t *gb_len) |
461 | { | |
462 | pr_debug("dev_name=%s\n", dev_name(&dev->dev)); | |
463 | ||
464 | return nfc_llcp_general_bytes(dev, gb_len); | |
465 | } | |
466 | EXPORT_SYMBOL(nfc_get_local_general_bytes); | |
467 | ||
73167ced SO |
468 | int nfc_tm_data_received(struct nfc_dev *dev, struct sk_buff *skb) |
469 | { | |
470 | /* Only LLCP target mode for now */ | |
471 | if (dev->dep_link_up == false) { | |
472 | kfree_skb(skb); | |
473 | return -ENOLINK; | |
474 | } | |
475 | ||
476 | return nfc_llcp_data_received(dev, skb); | |
477 | } | |
478 | EXPORT_SYMBOL(nfc_tm_data_received); | |
479 | ||
fc40a8c1 SO |
480 | int nfc_tm_activated(struct nfc_dev *dev, u32 protocol, u8 comm_mode, |
481 | u8 *gb, size_t gb_len) | |
482 | { | |
483 | int rc; | |
484 | ||
485 | device_lock(&dev->dev); | |
486 | ||
487 | dev->polling = false; | |
488 | ||
489 | if (gb != NULL) { | |
490 | rc = nfc_set_remote_general_bytes(dev, gb, gb_len); | |
491 | if (rc < 0) | |
492 | goto out; | |
493 | } | |
494 | ||
f212ad5e SO |
495 | dev->rf_mode = NFC_RF_TARGET; |
496 | ||
fc40a8c1 SO |
497 | if (protocol == NFC_PROTO_NFC_DEP_MASK) |
498 | nfc_dep_link_is_up(dev, 0, comm_mode, NFC_RF_TARGET); | |
499 | ||
500 | rc = nfc_genl_tm_activated(dev, protocol); | |
501 | ||
502 | out: | |
503 | device_unlock(&dev->dev); | |
504 | ||
505 | return rc; | |
506 | } | |
507 | EXPORT_SYMBOL(nfc_tm_activated); | |
508 | ||
509 | int nfc_tm_deactivated(struct nfc_dev *dev) | |
510 | { | |
511 | dev->dep_link_up = false; | |
5bcf099c | 512 | dev->rf_mode = NFC_RF_NONE; |
fc40a8c1 SO |
513 | |
514 | return nfc_genl_tm_deactivated(dev); | |
515 | } | |
516 | EXPORT_SYMBOL(nfc_tm_deactivated); | |
517 | ||
3e256b8f | 518 | /** |
7c7cd3bf | 519 | * nfc_alloc_send_skb - allocate a skb for data exchange responses |
3e256b8f LRV |
520 | * |
521 | * @size: size to allocate | |
522 | * @gfp: gfp flags | |
523 | */ | |
7c7cd3bf | 524 | struct sk_buff *nfc_alloc_send_skb(struct nfc_dev *dev, struct sock *sk, |
0a40acb2 SO |
525 | unsigned int flags, unsigned int size, |
526 | unsigned int *err) | |
7c7cd3bf SO |
527 | { |
528 | struct sk_buff *skb; | |
529 | unsigned int total_size; | |
530 | ||
531 | total_size = size + | |
532 | dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE; | |
533 | ||
534 | skb = sock_alloc_send_skb(sk, total_size, flags & MSG_DONTWAIT, err); | |
535 | if (skb) | |
536 | skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE); | |
537 | ||
538 | return skb; | |
539 | } | |
540 | ||
541 | /** | |
542 | * nfc_alloc_recv_skb - allocate a skb for data exchange responses | |
543 | * | |
544 | * @size: size to allocate | |
545 | * @gfp: gfp flags | |
546 | */ | |
547 | struct sk_buff *nfc_alloc_recv_skb(unsigned int size, gfp_t gfp) | |
3e256b8f LRV |
548 | { |
549 | struct sk_buff *skb; | |
550 | unsigned int total_size; | |
551 | ||
552 | total_size = size + 1; | |
553 | skb = alloc_skb(total_size, gfp); | |
554 | ||
555 | if (skb) | |
556 | skb_reserve(skb, 1); | |
557 | ||
558 | return skb; | |
559 | } | |
7c7cd3bf | 560 | EXPORT_SYMBOL(nfc_alloc_recv_skb); |
3e256b8f | 561 | |
4d12b8b1 LRV |
562 | /** |
563 | * nfc_targets_found - inform that targets were found | |
564 | * | |
565 | * @dev: The nfc device that found the targets | |
566 | * @targets: array of nfc targets found | |
567 | * @ntargets: targets array size | |
568 | * | |
569 | * The device driver must call this function when one or many nfc targets | |
570 | * are found. After calling this function, the device driver must stop | |
571 | * polling for targets. | |
d94f9c55 EL |
572 | * NOTE: This function can be called with targets=NULL and n_targets=0 to |
573 | * notify a driver error, meaning that the polling operation cannot complete. | |
d4ccb132 EL |
574 | * IMPORTANT: this function must not be called from an atomic context. |
575 | * In addition, it must also not be called from a context that would prevent | |
576 | * the NFC Core to call other nfc ops entry point concurrently. | |
4d12b8b1 | 577 | */ |
0a40acb2 SO |
578 | int nfc_targets_found(struct nfc_dev *dev, |
579 | struct nfc_target *targets, int n_targets) | |
4d12b8b1 | 580 | { |
c4fbb651 SO |
581 | int i; |
582 | ||
20c239c1 | 583 | pr_debug("dev_name=%s n_targets=%d\n", dev_name(&dev->dev), n_targets); |
4d12b8b1 | 584 | |
c4fbb651 | 585 | for (i = 0; i < n_targets; i++) |
01ae0eea | 586 | targets[i].idx = dev->target_next_idx++; |
c4fbb651 | 587 | |
d4ccb132 | 588 | device_lock(&dev->dev); |
4d12b8b1 | 589 | |
8668fdd6 EL |
590 | if (dev->polling == false) { |
591 | device_unlock(&dev->dev); | |
592 | return 0; | |
593 | } | |
594 | ||
595 | dev->polling = false; | |
596 | ||
4d12b8b1 LRV |
597 | dev->targets_generation++; |
598 | ||
599 | kfree(dev->targets); | |
d94f9c55 | 600 | dev->targets = NULL; |
4d12b8b1 | 601 | |
d94f9c55 EL |
602 | if (targets) { |
603 | dev->targets = kmemdup(targets, | |
604 | n_targets * sizeof(struct nfc_target), | |
605 | GFP_ATOMIC); | |
606 | ||
607 | if (!dev->targets) { | |
608 | dev->n_targets = 0; | |
609 | device_unlock(&dev->dev); | |
610 | return -ENOMEM; | |
611 | } | |
4d12b8b1 LRV |
612 | } |
613 | ||
614 | dev->n_targets = n_targets; | |
d4ccb132 | 615 | device_unlock(&dev->dev); |
4d12b8b1 LRV |
616 | |
617 | nfc_genl_targets_found(dev); | |
618 | ||
619 | return 0; | |
620 | } | |
621 | EXPORT_SYMBOL(nfc_targets_found); | |
622 | ||
d4ccb132 EL |
623 | /** |
624 | * nfc_target_lost - inform that an activated target went out of field | |
625 | * | |
626 | * @dev: The nfc device that had the activated target in field | |
627 | * @target_idx: the nfc index of the target | |
628 | * | |
629 | * The device driver must call this function when the activated target | |
630 | * goes out of the field. | |
631 | * IMPORTANT: this function must not be called from an atomic context. | |
632 | * In addition, it must also not be called from a context that would prevent | |
633 | * the NFC Core to call other nfc ops entry point concurrently. | |
634 | */ | |
e1da0efa EL |
635 | int nfc_target_lost(struct nfc_dev *dev, u32 target_idx) |
636 | { | |
637 | struct nfc_target *tg; | |
638 | int i; | |
639 | ||
640 | pr_debug("dev_name %s n_target %d\n", dev_name(&dev->dev), target_idx); | |
641 | ||
d4ccb132 | 642 | device_lock(&dev->dev); |
e1da0efa EL |
643 | |
644 | for (i = 0; i < dev->n_targets; i++) { | |
645 | tg = &dev->targets[i]; | |
646 | if (tg->idx == target_idx) | |
647 | break; | |
648 | } | |
649 | ||
650 | if (i == dev->n_targets) { | |
d4ccb132 | 651 | device_unlock(&dev->dev); |
e1da0efa EL |
652 | return -EINVAL; |
653 | } | |
654 | ||
655 | dev->targets_generation++; | |
656 | dev->n_targets--; | |
90099433 | 657 | dev->active_target = NULL; |
e1da0efa EL |
658 | |
659 | if (dev->n_targets) { | |
660 | memcpy(&dev->targets[i], &dev->targets[i + 1], | |
661 | (dev->n_targets - i) * sizeof(struct nfc_target)); | |
662 | } else { | |
663 | kfree(dev->targets); | |
664 | dev->targets = NULL; | |
665 | } | |
666 | ||
d4ccb132 | 667 | device_unlock(&dev->dev); |
e1da0efa EL |
668 | |
669 | nfc_genl_target_lost(dev, target_idx); | |
670 | ||
671 | return 0; | |
672 | } | |
673 | EXPORT_SYMBOL(nfc_target_lost); | |
674 | ||
9eb334ac | 675 | inline void nfc_driver_failure(struct nfc_dev *dev, int err) |
456411ca | 676 | { |
9eb334ac | 677 | nfc_targets_found(dev, NULL, 0); |
456411ca EL |
678 | } |
679 | EXPORT_SYMBOL(nfc_driver_failure); | |
680 | ||
3e256b8f LRV |
681 | static void nfc_release(struct device *d) |
682 | { | |
683 | struct nfc_dev *dev = to_nfc_dev(d); | |
684 | ||
20c239c1 | 685 | pr_debug("dev_name=%s\n", dev_name(&dev->dev)); |
3e256b8f | 686 | |
4d12b8b1 LRV |
687 | nfc_genl_data_exit(&dev->genl_data); |
688 | kfree(dev->targets); | |
3e256b8f LRV |
689 | kfree(dev); |
690 | } | |
691 | ||
c8d56ae7 EL |
692 | static void nfc_check_pres_work(struct work_struct *work) |
693 | { | |
694 | struct nfc_dev *dev = container_of(work, struct nfc_dev, | |
695 | check_pres_work); | |
696 | int rc; | |
697 | ||
698 | device_lock(&dev->dev); | |
699 | ||
90099433 EL |
700 | if (dev->active_target && timer_pending(&dev->check_pres_timer) == 0) { |
701 | rc = dev->ops->check_presence(dev, dev->active_target); | |
632c016a EL |
702 | if (rc == -EOPNOTSUPP) |
703 | goto exit; | |
f0c91038 | 704 | if (rc) { |
d4ccb132 EL |
705 | u32 active_target_idx = dev->active_target->idx; |
706 | device_unlock(&dev->dev); | |
707 | nfc_target_lost(dev, active_target_idx); | |
708 | return; | |
c8d56ae7 | 709 | } |
f0c91038 EL |
710 | |
711 | if (!dev->shutting_down) | |
712 | mod_timer(&dev->check_pres_timer, jiffies + | |
713 | msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS)); | |
c8d56ae7 EL |
714 | } |
715 | ||
632c016a | 716 | exit: |
c8d56ae7 EL |
717 | device_unlock(&dev->dev); |
718 | } | |
719 | ||
720 | static void nfc_check_pres_timeout(unsigned long data) | |
721 | { | |
722 | struct nfc_dev *dev = (struct nfc_dev *)data; | |
723 | ||
916082b0 | 724 | schedule_work(&dev->check_pres_work); |
c8d56ae7 EL |
725 | } |
726 | ||
3e256b8f LRV |
727 | struct class nfc_class = { |
728 | .name = "nfc", | |
729 | .dev_release = nfc_release, | |
730 | }; | |
731 | EXPORT_SYMBOL(nfc_class); | |
732 | ||
9f3b795a | 733 | static int match_idx(struct device *d, const void *data) |
3e256b8f LRV |
734 | { |
735 | struct nfc_dev *dev = to_nfc_dev(d); | |
9f3b795a | 736 | const unsigned int *idx = data; |
3e256b8f LRV |
737 | |
738 | return dev->idx == *idx; | |
739 | } | |
740 | ||
95c96174 | 741 | struct nfc_dev *nfc_get_device(unsigned int idx) |
3e256b8f LRV |
742 | { |
743 | struct device *d; | |
744 | ||
745 | d = class_find_device(&nfc_class, NULL, &idx, match_idx); | |
746 | if (!d) | |
747 | return NULL; | |
748 | ||
749 | return to_nfc_dev(d); | |
750 | } | |
751 | ||
752 | /** | |
753 | * nfc_allocate_device - allocate a new nfc device | |
754 | * | |
755 | * @ops: device operations | |
756 | * @supported_protocols: NFC protocols supported by the device | |
757 | */ | |
758 | struct nfc_dev *nfc_allocate_device(struct nfc_ops *ops, | |
0a40acb2 | 759 | u32 supported_protocols, |
390a1bd8 | 760 | u32 supported_se, |
0a40acb2 | 761 | int tx_headroom, int tx_tailroom) |
3e256b8f | 762 | { |
3e256b8f LRV |
763 | struct nfc_dev *dev; |
764 | ||
765 | if (!ops->start_poll || !ops->stop_poll || !ops->activate_target || | |
be9ae4ce | 766 | !ops->deactivate_target || !ops->im_transceive) |
3e256b8f LRV |
767 | return NULL; |
768 | ||
769 | if (!supported_protocols) | |
770 | return NULL; | |
771 | ||
772 | dev = kzalloc(sizeof(struct nfc_dev), GFP_KERNEL); | |
773 | if (!dev) | |
774 | return NULL; | |
775 | ||
3e256b8f LRV |
776 | dev->ops = ops; |
777 | dev->supported_protocols = supported_protocols; | |
390a1bd8 SO |
778 | dev->supported_se = supported_se; |
779 | dev->active_se = NFC_SE_NONE; | |
e8753043 SO |
780 | dev->tx_headroom = tx_headroom; |
781 | dev->tx_tailroom = tx_tailroom; | |
3e256b8f | 782 | |
4d12b8b1 LRV |
783 | nfc_genl_data_init(&dev->genl_data); |
784 | ||
5bcf099c | 785 | dev->rf_mode = NFC_RF_NONE; |
d4ccb132 | 786 | |
4d12b8b1 LRV |
787 | /* first generation must not be 0 */ |
788 | dev->targets_generation = 1; | |
789 | ||
c8d56ae7 | 790 | if (ops->check_presence) { |
c8d56ae7 EL |
791 | init_timer(&dev->check_pres_timer); |
792 | dev->check_pres_timer.data = (unsigned long)dev; | |
793 | dev->check_pres_timer.function = nfc_check_pres_timeout; | |
794 | ||
795 | INIT_WORK(&dev->check_pres_work, nfc_check_pres_work); | |
c8d56ae7 EL |
796 | } |
797 | ||
3e256b8f LRV |
798 | return dev; |
799 | } | |
800 | EXPORT_SYMBOL(nfc_allocate_device); | |
801 | ||
802 | /** | |
803 | * nfc_register_device - register a nfc device in the nfc subsystem | |
804 | * | |
805 | * @dev: The nfc device to register | |
806 | */ | |
807 | int nfc_register_device(struct nfc_dev *dev) | |
808 | { | |
809 | int rc; | |
810 | ||
20c239c1 | 811 | pr_debug("dev_name=%s\n", dev_name(&dev->dev)); |
3e256b8f | 812 | |
7eda8b8e SO |
813 | dev->idx = ida_simple_get(&nfc_index_ida, 0, 0, GFP_KERNEL); |
814 | if (dev->idx < 0) | |
815 | return dev->idx; | |
816 | ||
817 | dev->dev.class = &nfc_class; | |
818 | dev_set_name(&dev->dev, "nfc%d", dev->idx); | |
819 | device_initialize(&dev->dev); | |
820 | ||
3e256b8f LRV |
821 | mutex_lock(&nfc_devlist_mutex); |
822 | nfc_devlist_generation++; | |
823 | rc = device_add(&dev->dev); | |
824 | mutex_unlock(&nfc_devlist_mutex); | |
825 | ||
4d12b8b1 LRV |
826 | if (rc < 0) |
827 | return rc; | |
828 | ||
d646960f SO |
829 | rc = nfc_llcp_register_device(dev); |
830 | if (rc) | |
831 | pr_err("Could not register llcp device\n"); | |
832 | ||
4d12b8b1 LRV |
833 | rc = nfc_genl_device_added(dev); |
834 | if (rc) | |
20c239c1 JP |
835 | pr_debug("The userspace won't be notified that the device %s was added\n", |
836 | dev_name(&dev->dev)); | |
4d12b8b1 LRV |
837 | |
838 | return 0; | |
3e256b8f LRV |
839 | } |
840 | EXPORT_SYMBOL(nfc_register_device); | |
841 | ||
842 | /** | |
843 | * nfc_unregister_device - unregister a nfc device in the nfc subsystem | |
844 | * | |
845 | * @dev: The nfc device to unregister | |
846 | */ | |
847 | void nfc_unregister_device(struct nfc_dev *dev) | |
848 | { | |
7eda8b8e | 849 | int rc, id; |
4d12b8b1 | 850 | |
20c239c1 | 851 | pr_debug("dev_name=%s\n", dev_name(&dev->dev)); |
3e256b8f | 852 | |
7eda8b8e SO |
853 | id = dev->idx; |
854 | ||
f0c91038 EL |
855 | if (dev->ops->check_presence) { |
856 | device_lock(&dev->dev); | |
857 | dev->shutting_down = true; | |
858 | device_unlock(&dev->dev); | |
859 | del_timer_sync(&dev->check_pres_timer); | |
860 | cancel_work_sync(&dev->check_pres_work); | |
861 | } | |
3e256b8f | 862 | |
f0c91038 EL |
863 | rc = nfc_genl_device_removed(dev); |
864 | if (rc) | |
865 | pr_debug("The userspace won't be notified that the device %s " | |
866 | "was removed\n", dev_name(&dev->dev)); | |
4d12b8b1 | 867 | |
d646960f SO |
868 | nfc_llcp_unregister_device(dev); |
869 | ||
f0c91038 EL |
870 | mutex_lock(&nfc_devlist_mutex); |
871 | nfc_devlist_generation++; | |
872 | device_del(&dev->dev); | |
873 | mutex_unlock(&nfc_devlist_mutex); | |
4d12b8b1 | 874 | |
7eda8b8e | 875 | ida_simple_remove(&nfc_index_ida, id); |
3e256b8f LRV |
876 | } |
877 | EXPORT_SYMBOL(nfc_unregister_device); | |
878 | ||
879 | static int __init nfc_init(void) | |
880 | { | |
4d12b8b1 LRV |
881 | int rc; |
882 | ||
ed1e0ad8 | 883 | pr_info("NFC Core ver %s\n", VERSION); |
3e256b8f | 884 | |
4d12b8b1 LRV |
885 | rc = class_register(&nfc_class); |
886 | if (rc) | |
887 | return rc; | |
888 | ||
889 | rc = nfc_genl_init(); | |
890 | if (rc) | |
891 | goto err_genl; | |
892 | ||
893 | /* the first generation must not be 0 */ | |
894 | nfc_devlist_generation = 1; | |
895 | ||
23b7869c LRV |
896 | rc = rawsock_init(); |
897 | if (rc) | |
898 | goto err_rawsock; | |
899 | ||
d646960f SO |
900 | rc = nfc_llcp_init(); |
901 | if (rc) | |
902 | goto err_llcp_sock; | |
903 | ||
c7fe3b52 AAJ |
904 | rc = af_nfc_init(); |
905 | if (rc) | |
906 | goto err_af_nfc; | |
907 | ||
4d12b8b1 LRV |
908 | return 0; |
909 | ||
c7fe3b52 | 910 | err_af_nfc: |
d646960f SO |
911 | nfc_llcp_exit(); |
912 | err_llcp_sock: | |
23b7869c LRV |
913 | rawsock_exit(); |
914 | err_rawsock: | |
c7fe3b52 | 915 | nfc_genl_exit(); |
4d12b8b1 LRV |
916 | err_genl: |
917 | class_unregister(&nfc_class); | |
918 | return rc; | |
3e256b8f LRV |
919 | } |
920 | ||
921 | static void __exit nfc_exit(void) | |
922 | { | |
c7fe3b52 | 923 | af_nfc_exit(); |
d646960f | 924 | nfc_llcp_exit(); |
23b7869c | 925 | rawsock_exit(); |
4d12b8b1 | 926 | nfc_genl_exit(); |
3e256b8f LRV |
927 | class_unregister(&nfc_class); |
928 | } | |
929 | ||
930 | subsys_initcall(nfc_init); | |
931 | module_exit(nfc_exit); | |
932 | ||
933 | MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>"); | |
934 | MODULE_DESCRIPTION("NFC Core ver " VERSION); | |
935 | MODULE_VERSION(VERSION); | |
936 | MODULE_LICENSE("GPL"); | |
1155bb61 | 937 | MODULE_ALIAS_NETPROTO(PF_NFC); |
5df16cad | 938 | MODULE_ALIAS_GENL_FAMILY(NFC_GENL_NAME); |