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