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 | ||
3e256b8f LRV |
654 | static void nfc_release(struct device *d) |
655 | { | |
656 | struct nfc_dev *dev = to_nfc_dev(d); | |
657 | ||
20c239c1 | 658 | pr_debug("dev_name=%s\n", dev_name(&dev->dev)); |
3e256b8f | 659 | |
c8d56ae7 EL |
660 | if (dev->ops->check_presence) { |
661 | del_timer_sync(&dev->check_pres_timer); | |
662 | destroy_workqueue(dev->check_pres_wq); | |
663 | } | |
664 | ||
4d12b8b1 LRV |
665 | nfc_genl_data_exit(&dev->genl_data); |
666 | kfree(dev->targets); | |
3e256b8f LRV |
667 | kfree(dev); |
668 | } | |
669 | ||
c8d56ae7 EL |
670 | static void nfc_check_pres_work(struct work_struct *work) |
671 | { | |
672 | struct nfc_dev *dev = container_of(work, struct nfc_dev, | |
673 | check_pres_work); | |
674 | int rc; | |
675 | ||
676 | device_lock(&dev->dev); | |
677 | ||
90099433 EL |
678 | if (dev->active_target && timer_pending(&dev->check_pres_timer) == 0) { |
679 | rc = dev->ops->check_presence(dev, dev->active_target); | |
c8d56ae7 EL |
680 | if (!rc) { |
681 | mod_timer(&dev->check_pres_timer, jiffies + | |
682 | msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS)); | |
683 | } else { | |
d4ccb132 EL |
684 | u32 active_target_idx = dev->active_target->idx; |
685 | device_unlock(&dev->dev); | |
686 | nfc_target_lost(dev, active_target_idx); | |
687 | return; | |
c8d56ae7 EL |
688 | } |
689 | } | |
690 | ||
691 | device_unlock(&dev->dev); | |
692 | } | |
693 | ||
694 | static void nfc_check_pres_timeout(unsigned long data) | |
695 | { | |
696 | struct nfc_dev *dev = (struct nfc_dev *)data; | |
697 | ||
698 | queue_work(dev->check_pres_wq, &dev->check_pres_work); | |
699 | } | |
700 | ||
3e256b8f LRV |
701 | struct class nfc_class = { |
702 | .name = "nfc", | |
703 | .dev_release = nfc_release, | |
704 | }; | |
705 | EXPORT_SYMBOL(nfc_class); | |
706 | ||
707 | static int match_idx(struct device *d, void *data) | |
708 | { | |
709 | struct nfc_dev *dev = to_nfc_dev(d); | |
95c96174 | 710 | unsigned int *idx = data; |
3e256b8f LRV |
711 | |
712 | return dev->idx == *idx; | |
713 | } | |
714 | ||
95c96174 | 715 | struct nfc_dev *nfc_get_device(unsigned int idx) |
3e256b8f LRV |
716 | { |
717 | struct device *d; | |
718 | ||
719 | d = class_find_device(&nfc_class, NULL, &idx, match_idx); | |
720 | if (!d) | |
721 | return NULL; | |
722 | ||
723 | return to_nfc_dev(d); | |
724 | } | |
725 | ||
726 | /** | |
727 | * nfc_allocate_device - allocate a new nfc device | |
728 | * | |
729 | * @ops: device operations | |
730 | * @supported_protocols: NFC protocols supported by the device | |
731 | */ | |
732 | struct nfc_dev *nfc_allocate_device(struct nfc_ops *ops, | |
0a40acb2 SO |
733 | u32 supported_protocols, |
734 | int tx_headroom, int tx_tailroom) | |
3e256b8f LRV |
735 | { |
736 | static atomic_t dev_no = ATOMIC_INIT(0); | |
737 | struct nfc_dev *dev; | |
738 | ||
739 | if (!ops->start_poll || !ops->stop_poll || !ops->activate_target || | |
be9ae4ce | 740 | !ops->deactivate_target || !ops->im_transceive) |
3e256b8f LRV |
741 | return NULL; |
742 | ||
743 | if (!supported_protocols) | |
744 | return NULL; | |
745 | ||
746 | dev = kzalloc(sizeof(struct nfc_dev), GFP_KERNEL); | |
747 | if (!dev) | |
748 | return NULL; | |
749 | ||
750 | dev->dev.class = &nfc_class; | |
751 | dev->idx = atomic_inc_return(&dev_no) - 1; | |
752 | dev_set_name(&dev->dev, "nfc%d", dev->idx); | |
753 | device_initialize(&dev->dev); | |
754 | ||
755 | dev->ops = ops; | |
756 | dev->supported_protocols = supported_protocols; | |
e8753043 SO |
757 | dev->tx_headroom = tx_headroom; |
758 | dev->tx_tailroom = tx_tailroom; | |
3e256b8f | 759 | |
4d12b8b1 LRV |
760 | nfc_genl_data_init(&dev->genl_data); |
761 | ||
d4ccb132 | 762 | |
4d12b8b1 LRV |
763 | /* first generation must not be 0 */ |
764 | dev->targets_generation = 1; | |
765 | ||
c8d56ae7 EL |
766 | if (ops->check_presence) { |
767 | char name[32]; | |
768 | init_timer(&dev->check_pres_timer); | |
769 | dev->check_pres_timer.data = (unsigned long)dev; | |
770 | dev->check_pres_timer.function = nfc_check_pres_timeout; | |
771 | ||
772 | INIT_WORK(&dev->check_pres_work, nfc_check_pres_work); | |
773 | snprintf(name, sizeof(name), "nfc%d_check_pres_wq", dev->idx); | |
774 | dev->check_pres_wq = alloc_workqueue(name, WQ_NON_REENTRANT | | |
775 | WQ_UNBOUND | | |
776 | WQ_MEM_RECLAIM, 1); | |
777 | if (dev->check_pres_wq == NULL) { | |
778 | kfree(dev); | |
779 | return NULL; | |
780 | } | |
781 | } | |
782 | ||
3e256b8f LRV |
783 | return dev; |
784 | } | |
785 | EXPORT_SYMBOL(nfc_allocate_device); | |
786 | ||
787 | /** | |
788 | * nfc_register_device - register a nfc device in the nfc subsystem | |
789 | * | |
790 | * @dev: The nfc device to register | |
791 | */ | |
792 | int nfc_register_device(struct nfc_dev *dev) | |
793 | { | |
794 | int rc; | |
795 | ||
20c239c1 | 796 | pr_debug("dev_name=%s\n", dev_name(&dev->dev)); |
3e256b8f LRV |
797 | |
798 | mutex_lock(&nfc_devlist_mutex); | |
799 | nfc_devlist_generation++; | |
800 | rc = device_add(&dev->dev); | |
801 | mutex_unlock(&nfc_devlist_mutex); | |
802 | ||
4d12b8b1 LRV |
803 | if (rc < 0) |
804 | return rc; | |
805 | ||
d646960f SO |
806 | rc = nfc_llcp_register_device(dev); |
807 | if (rc) | |
808 | pr_err("Could not register llcp device\n"); | |
809 | ||
4d12b8b1 LRV |
810 | rc = nfc_genl_device_added(dev); |
811 | if (rc) | |
20c239c1 JP |
812 | pr_debug("The userspace won't be notified that the device %s was added\n", |
813 | dev_name(&dev->dev)); | |
4d12b8b1 LRV |
814 | |
815 | return 0; | |
3e256b8f LRV |
816 | } |
817 | EXPORT_SYMBOL(nfc_register_device); | |
818 | ||
819 | /** | |
820 | * nfc_unregister_device - unregister a nfc device in the nfc subsystem | |
821 | * | |
822 | * @dev: The nfc device to unregister | |
823 | */ | |
824 | void nfc_unregister_device(struct nfc_dev *dev) | |
825 | { | |
4d12b8b1 LRV |
826 | int rc; |
827 | ||
20c239c1 | 828 | pr_debug("dev_name=%s\n", dev_name(&dev->dev)); |
3e256b8f LRV |
829 | |
830 | mutex_lock(&nfc_devlist_mutex); | |
831 | nfc_devlist_generation++; | |
832 | ||
833 | /* lock to avoid unregistering a device while an operation | |
834 | is in progress */ | |
835 | device_lock(&dev->dev); | |
836 | device_del(&dev->dev); | |
837 | device_unlock(&dev->dev); | |
838 | ||
839 | mutex_unlock(&nfc_devlist_mutex); | |
4d12b8b1 | 840 | |
d646960f SO |
841 | nfc_llcp_unregister_device(dev); |
842 | ||
4d12b8b1 LRV |
843 | rc = nfc_genl_device_removed(dev); |
844 | if (rc) | |
20c239c1 JP |
845 | pr_debug("The userspace won't be notified that the device %s was removed\n", |
846 | dev_name(&dev->dev)); | |
4d12b8b1 | 847 | |
3e256b8f LRV |
848 | } |
849 | EXPORT_SYMBOL(nfc_unregister_device); | |
850 | ||
851 | static int __init nfc_init(void) | |
852 | { | |
4d12b8b1 LRV |
853 | int rc; |
854 | ||
ed1e0ad8 | 855 | pr_info("NFC Core ver %s\n", VERSION); |
3e256b8f | 856 | |
4d12b8b1 LRV |
857 | rc = class_register(&nfc_class); |
858 | if (rc) | |
859 | return rc; | |
860 | ||
861 | rc = nfc_genl_init(); | |
862 | if (rc) | |
863 | goto err_genl; | |
864 | ||
865 | /* the first generation must not be 0 */ | |
866 | nfc_devlist_generation = 1; | |
867 | ||
23b7869c LRV |
868 | rc = rawsock_init(); |
869 | if (rc) | |
870 | goto err_rawsock; | |
871 | ||
d646960f SO |
872 | rc = nfc_llcp_init(); |
873 | if (rc) | |
874 | goto err_llcp_sock; | |
875 | ||
c7fe3b52 AAJ |
876 | rc = af_nfc_init(); |
877 | if (rc) | |
878 | goto err_af_nfc; | |
879 | ||
4d12b8b1 LRV |
880 | return 0; |
881 | ||
c7fe3b52 | 882 | err_af_nfc: |
d646960f SO |
883 | nfc_llcp_exit(); |
884 | err_llcp_sock: | |
23b7869c LRV |
885 | rawsock_exit(); |
886 | err_rawsock: | |
c7fe3b52 | 887 | nfc_genl_exit(); |
4d12b8b1 LRV |
888 | err_genl: |
889 | class_unregister(&nfc_class); | |
890 | return rc; | |
3e256b8f LRV |
891 | } |
892 | ||
893 | static void __exit nfc_exit(void) | |
894 | { | |
c7fe3b52 | 895 | af_nfc_exit(); |
d646960f | 896 | nfc_llcp_exit(); |
23b7869c | 897 | rawsock_exit(); |
4d12b8b1 | 898 | nfc_genl_exit(); |
3e256b8f LRV |
899 | class_unregister(&nfc_class); |
900 | } | |
901 | ||
902 | subsys_initcall(nfc_init); | |
903 | module_exit(nfc_exit); | |
904 | ||
905 | MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>"); | |
906 | MODULE_DESCRIPTION("NFC Core ver " VERSION); | |
907 | MODULE_VERSION(VERSION); | |
908 | MODULE_LICENSE("GPL"); |