NFC: Add firmware upload netlink command
[deliverable/linux.git] / net / nfc / core.c
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
24 #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
25
26 #include <linux/init.h>
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/slab.h>
30 #include <linux/rfkill.h>
31 #include <linux/nfc.h>
32
33 #include <net/genetlink.h>
34
35 #include "nfc.h"
36
37 #define VERSION "0.1"
38
39 #define NFC_CHECK_PRES_FREQ_MS 2000
40
41 int nfc_devlist_generation;
42 DEFINE_MUTEX(nfc_devlist_mutex);
43
44 /* NFC device ID bitmap */
45 static DEFINE_IDA(nfc_index_ida);
46
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
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
99 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
100
101 device_lock(&dev->dev);
102
103 if (dev->rfkill && rfkill_blocked(dev->rfkill)) {
104 rc = -ERFKILL;
105 goto error;
106 }
107
108 if (!device_is_registered(&dev->dev)) {
109 rc = -ENODEV;
110 goto error;
111 }
112
113 if (dev->fw_upload_in_progress) {
114 rc = -EBUSY;
115 goto error;
116 }
117
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
143 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
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
157 if (dev->polling || dev->active_target) {
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
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
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 */
199 int nfc_start_poll(struct nfc_dev *dev, u32 im_protocols, u32 tm_protocols)
200 {
201 int rc;
202
203 pr_debug("dev_name %s initiator protocols 0x%x target protocols 0x%x\n",
204 dev_name(&dev->dev), im_protocols, tm_protocols);
205
206 if (!im_protocols && !tm_protocols)
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
216 if (!dev->dev_up) {
217 rc = -ENODEV;
218 goto error;
219 }
220
221 if (dev->polling) {
222 rc = -EBUSY;
223 goto error;
224 }
225
226 rc = dev->ops->start_poll(dev, im_protocols, tm_protocols);
227 if (!rc) {
228 dev->polling = true;
229 dev->rf_mode = NFC_RF_NONE;
230 }
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
246 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
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;
262 dev->rf_mode = NFC_RF_NONE;
263
264 error:
265 device_unlock(&dev->dev);
266 return rc;
267 }
268
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
276 for (i = 0; i < dev->n_targets; i++) {
277 if (dev->targets[i].idx == target_idx)
278 return &dev->targets[i];
279 }
280
281 return NULL;
282 }
283
284 int nfc_dep_link_up(struct nfc_dev *dev, int target_index, u8 comm_mode)
285 {
286 int rc = 0;
287 u8 *gb;
288 size_t gb_len;
289 struct nfc_target *target;
290
291 pr_debug("dev_name=%s comm %d\n", dev_name(&dev->dev), comm_mode);
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
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
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);
321 if (!rc) {
322 dev->active_target = target;
323 dev->rf_mode = NFC_RF_INITIATOR;
324 }
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
352 rc = dev->ops->dep_link_down(dev);
353 if (!rc) {
354 dev->dep_link_up = false;
355 dev->active_target = NULL;
356 dev->rf_mode = NFC_RF_NONE;
357 nfc_llcp_mac_is_down(dev);
358 nfc_genl_dep_link_down_event(dev);
359 }
360
361 error:
362 device_unlock(&dev->dev);
363
364 return rc;
365 }
366
367 int nfc_dep_link_is_up(struct nfc_dev *dev, u32 target_idx,
368 u8 comm_mode, u8 rf_mode)
369 {
370 dev->dep_link_up = true;
371
372 nfc_llcp_mac_is_up(dev, target_idx, comm_mode, rf_mode);
373
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
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;
388 struct nfc_target *target;
389
390 pr_debug("dev_name=%s target_idx=%u protocol=%u\n",
391 dev_name(&dev->dev), target_idx, protocol);
392
393 device_lock(&dev->dev);
394
395 if (!device_is_registered(&dev->dev)) {
396 rc = -ENODEV;
397 goto error;
398 }
399
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);
412 if (!rc) {
413 dev->active_target = target;
414 dev->rf_mode = NFC_RF_INITIATOR;
415
416 if (dev->ops->check_presence && !dev->shutting_down)
417 mod_timer(&dev->check_pres_timer, jiffies +
418 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
419 }
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
436 pr_debug("dev_name=%s target_idx=%u\n",
437 dev_name(&dev->dev), target_idx);
438
439 device_lock(&dev->dev);
440
441 if (!device_is_registered(&dev->dev)) {
442 rc = -ENODEV;
443 goto error;
444 }
445
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
456 if (dev->ops->check_presence)
457 del_timer_sync(&dev->check_pres_timer);
458
459 dev->ops->deactivate_target(dev, dev->active_target);
460 dev->active_target = NULL;
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 */
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)
480 {
481 int rc;
482
483 pr_debug("dev_name=%s target_idx=%u skb->len=%u\n",
484 dev_name(&dev->dev), target_idx, skb->len);
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
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 }
500
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
507 if (!rc && dev->ops->check_presence && !dev->shutting_down)
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;
514 kfree_skb(skb);
515 goto error;
516 }
517
518
519 error:
520 device_unlock(&dev->dev);
521 return rc;
522 }
523
524 int nfc_set_remote_general_bytes(struct nfc_dev *dev, u8 *gb, u8 gb_len)
525 {
526 pr_debug("dev_name=%s gb_len=%d\n", dev_name(&dev->dev), gb_len);
527
528 if (gb_len > NFC_MAX_GT_LEN)
529 return -EINVAL;
530
531 return nfc_llcp_set_remote_gb(dev, gb, gb_len);
532 }
533 EXPORT_SYMBOL(nfc_set_remote_general_bytes);
534
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
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
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
570 dev->rf_mode = NFC_RF_TARGET;
571
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;
587 dev->rf_mode = NFC_RF_NONE;
588
589 return nfc_genl_tm_deactivated(dev);
590 }
591 EXPORT_SYMBOL(nfc_tm_deactivated);
592
593 /**
594 * nfc_alloc_send_skb - allocate a skb for data exchange responses
595 *
596 * @size: size to allocate
597 * @gfp: gfp flags
598 */
599 struct sk_buff *nfc_alloc_send_skb(struct nfc_dev *dev, struct sock *sk,
600 unsigned int flags, unsigned int size,
601 unsigned int *err)
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)
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 }
635 EXPORT_SYMBOL(nfc_alloc_recv_skb);
636
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.
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.
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.
652 */
653 int nfc_targets_found(struct nfc_dev *dev,
654 struct nfc_target *targets, int n_targets)
655 {
656 int i;
657
658 pr_debug("dev_name=%s n_targets=%d\n", dev_name(&dev->dev), n_targets);
659
660 for (i = 0; i < n_targets; i++)
661 targets[i].idx = dev->target_next_idx++;
662
663 device_lock(&dev->dev);
664
665 if (dev->polling == false) {
666 device_unlock(&dev->dev);
667 return 0;
668 }
669
670 dev->polling = false;
671
672 dev->targets_generation++;
673
674 kfree(dev->targets);
675 dev->targets = NULL;
676
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 }
687 }
688
689 dev->n_targets = n_targets;
690 device_unlock(&dev->dev);
691
692 nfc_genl_targets_found(dev);
693
694 return 0;
695 }
696 EXPORT_SYMBOL(nfc_targets_found);
697
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 */
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
717 device_lock(&dev->dev);
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) {
726 device_unlock(&dev->dev);
727 return -EINVAL;
728 }
729
730 dev->targets_generation++;
731 dev->n_targets--;
732 dev->active_target = NULL;
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
742 device_unlock(&dev->dev);
743
744 nfc_genl_target_lost(dev, target_idx);
745
746 return 0;
747 }
748 EXPORT_SYMBOL(nfc_target_lost);
749
750 inline void nfc_driver_failure(struct nfc_dev *dev, int err)
751 {
752 nfc_targets_found(dev, NULL, 0);
753 }
754 EXPORT_SYMBOL(nfc_driver_failure);
755
756 static void nfc_release(struct device *d)
757 {
758 struct nfc_dev *dev = to_nfc_dev(d);
759
760 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
761
762 nfc_genl_data_exit(&dev->genl_data);
763 kfree(dev->targets);
764 kfree(dev);
765 }
766
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
775 if (dev->active_target && timer_pending(&dev->check_pres_timer) == 0) {
776 rc = dev->ops->check_presence(dev, dev->active_target);
777 if (rc == -EOPNOTSUPP)
778 goto exit;
779 if (rc) {
780 u32 active_target_idx = dev->active_target->idx;
781 device_unlock(&dev->dev);
782 nfc_target_lost(dev, active_target_idx);
783 return;
784 }
785
786 if (!dev->shutting_down)
787 mod_timer(&dev->check_pres_timer, jiffies +
788 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
789 }
790
791 exit:
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
799 schedule_work(&dev->check_pres_work);
800 }
801
802 struct class nfc_class = {
803 .name = "nfc",
804 .dev_release = nfc_release,
805 };
806 EXPORT_SYMBOL(nfc_class);
807
808 static int match_idx(struct device *d, const void *data)
809 {
810 struct nfc_dev *dev = to_nfc_dev(d);
811 const unsigned int *idx = data;
812
813 return dev->idx == *idx;
814 }
815
816 struct nfc_dev *nfc_get_device(unsigned int idx)
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,
834 u32 supported_protocols,
835 u32 supported_se,
836 int tx_headroom, int tx_tailroom)
837 {
838 struct nfc_dev *dev;
839
840 if (!ops->start_poll || !ops->stop_poll || !ops->activate_target ||
841 !ops->deactivate_target || !ops->im_transceive)
842 return NULL;
843
844 if (!supported_protocols)
845 return NULL;
846
847 dev = kzalloc(sizeof(struct nfc_dev), GFP_KERNEL);
848 if (!dev)
849 return NULL;
850
851 dev->ops = ops;
852 dev->supported_protocols = supported_protocols;
853 dev->supported_se = supported_se;
854 dev->active_se = NFC_SE_NONE;
855 dev->tx_headroom = tx_headroom;
856 dev->tx_tailroom = tx_tailroom;
857
858 nfc_genl_data_init(&dev->genl_data);
859
860 dev->rf_mode = NFC_RF_NONE;
861
862 /* first generation must not be 0 */
863 dev->targets_generation = 1;
864
865 if (ops->check_presence) {
866 init_timer(&dev->check_pres_timer);
867 dev->check_pres_timer.data = (unsigned long)dev;
868 dev->check_pres_timer.function = nfc_check_pres_timeout;
869
870 INIT_WORK(&dev->check_pres_work, nfc_check_pres_work);
871 }
872
873 return dev;
874 }
875 EXPORT_SYMBOL(nfc_allocate_device);
876
877 /**
878 * nfc_register_device - register a nfc device in the nfc subsystem
879 *
880 * @dev: The nfc device to register
881 */
882 int nfc_register_device(struct nfc_dev *dev)
883 {
884 int rc;
885
886 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
887
888 dev->idx = ida_simple_get(&nfc_index_ida, 0, 0, GFP_KERNEL);
889 if (dev->idx < 0)
890 return dev->idx;
891
892 dev->dev.class = &nfc_class;
893 dev_set_name(&dev->dev, "nfc%d", dev->idx);
894 device_initialize(&dev->dev);
895
896 mutex_lock(&nfc_devlist_mutex);
897 nfc_devlist_generation++;
898 rc = device_add(&dev->dev);
899 mutex_unlock(&nfc_devlist_mutex);
900
901 if (rc < 0)
902 return rc;
903
904 rc = nfc_llcp_register_device(dev);
905 if (rc)
906 pr_err("Could not register llcp device\n");
907
908 rc = nfc_genl_device_added(dev);
909 if (rc)
910 pr_debug("The userspace won't be notified that the device %s was added\n",
911 dev_name(&dev->dev));
912
913 dev->rfkill = rfkill_alloc(dev_name(&dev->dev), &dev->dev,
914 RFKILL_TYPE_NFC, &nfc_rfkill_ops, dev);
915 if (dev->rfkill) {
916 if (rfkill_register(dev->rfkill) < 0) {
917 rfkill_destroy(dev->rfkill);
918 dev->rfkill = NULL;
919 }
920 }
921
922 return 0;
923 }
924 EXPORT_SYMBOL(nfc_register_device);
925
926 /**
927 * nfc_unregister_device - unregister a nfc device in the nfc subsystem
928 *
929 * @dev: The nfc device to unregister
930 */
931 void nfc_unregister_device(struct nfc_dev *dev)
932 {
933 int rc, id;
934
935 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
936
937 id = dev->idx;
938
939 if (dev->rfkill) {
940 rfkill_unregister(dev->rfkill);
941 rfkill_destroy(dev->rfkill);
942 }
943
944 if (dev->ops->check_presence) {
945 device_lock(&dev->dev);
946 dev->shutting_down = true;
947 device_unlock(&dev->dev);
948 del_timer_sync(&dev->check_pres_timer);
949 cancel_work_sync(&dev->check_pres_work);
950 }
951
952 rc = nfc_genl_device_removed(dev);
953 if (rc)
954 pr_debug("The userspace won't be notified that the device %s "
955 "was removed\n", dev_name(&dev->dev));
956
957 nfc_llcp_unregister_device(dev);
958
959 mutex_lock(&nfc_devlist_mutex);
960 nfc_devlist_generation++;
961 device_del(&dev->dev);
962 mutex_unlock(&nfc_devlist_mutex);
963
964 ida_simple_remove(&nfc_index_ida, id);
965 }
966 EXPORT_SYMBOL(nfc_unregister_device);
967
968 static int __init nfc_init(void)
969 {
970 int rc;
971
972 pr_info("NFC Core ver %s\n", VERSION);
973
974 rc = class_register(&nfc_class);
975 if (rc)
976 return rc;
977
978 rc = nfc_genl_init();
979 if (rc)
980 goto err_genl;
981
982 /* the first generation must not be 0 */
983 nfc_devlist_generation = 1;
984
985 rc = rawsock_init();
986 if (rc)
987 goto err_rawsock;
988
989 rc = nfc_llcp_init();
990 if (rc)
991 goto err_llcp_sock;
992
993 rc = af_nfc_init();
994 if (rc)
995 goto err_af_nfc;
996
997 return 0;
998
999 err_af_nfc:
1000 nfc_llcp_exit();
1001 err_llcp_sock:
1002 rawsock_exit();
1003 err_rawsock:
1004 nfc_genl_exit();
1005 err_genl:
1006 class_unregister(&nfc_class);
1007 return rc;
1008 }
1009
1010 static void __exit nfc_exit(void)
1011 {
1012 af_nfc_exit();
1013 nfc_llcp_exit();
1014 rawsock_exit();
1015 nfc_genl_exit();
1016 class_unregister(&nfc_class);
1017 }
1018
1019 subsys_initcall(nfc_init);
1020 module_exit(nfc_exit);
1021
1022 MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
1023 MODULE_DESCRIPTION("NFC Core ver " VERSION);
1024 MODULE_VERSION(VERSION);
1025 MODULE_LICENSE("GPL");
1026 MODULE_ALIAS_NETPROTO(PF_NFC);
1027 MODULE_ALIAS_GENL_FAMILY(NFC_GENL_NAME);
This page took 0.050784 seconds and 6 git commands to generate.