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