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