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