blob: c699d64a0753aeb42bf938d37bcbd5dad263adc0 [file] [log] [blame]
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001/*
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
Jeff Kirsher98b32de2013-12-06 08:56:16 -080019 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -030020 */
21
Samuel Ortiz52858b52011-12-14 16:43:05 +010022#define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
Joe Perchesed1e0ad2011-11-29 11:37:32 -080023
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -030024#include <linux/init.h>
25#include <linux/kernel.h>
26#include <linux/module.h>
27#include <linux/slab.h>
Samuel Ortizbe055b22013-04-11 11:52:20 +020028#include <linux/rfkill.h>
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +010029#include <linux/nfc.h>
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -030030
Samuel Ortiz5df16ca2012-06-12 16:54:16 +020031#include <net/genetlink.h>
32
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -030033#include "nfc.h"
34
35#define VERSION "0.1"
36
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +020037#define NFC_CHECK_PRES_FREQ_MS 2000
38
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -030039int nfc_devlist_generation;
40DEFINE_MUTEX(nfc_devlist_mutex);
41
Samuel Ortiz7eda8b8e92012-10-22 15:57:58 +020042/* NFC device ID bitmap */
43static DEFINE_IDA(nfc_index_ida);
44
Samuel Ortiz9ea71872013-07-31 01:19:43 +020045int nfc_fw_download(struct nfc_dev *dev, const char *firmware_name)
Eric Lapuyade9674da82013-04-29 17:13:27 +020046{
47 int rc = 0;
48
49 pr_debug("%s do firmware %s\n", dev_name(&dev->dev), firmware_name);
50
51 device_lock(&dev->dev);
52
53 if (!device_is_registered(&dev->dev)) {
54 rc = -ENODEV;
55 goto error;
56 }
57
58 if (dev->dev_up) {
59 rc = -EBUSY;
60 goto error;
61 }
62
Samuel Ortiz9ea71872013-07-31 01:19:43 +020063 if (!dev->ops->fw_download) {
Eric Lapuyade9674da82013-04-29 17:13:27 +020064 rc = -EOPNOTSUPP;
65 goto error;
66 }
67
Samuel Ortiz9ea71872013-07-31 01:19:43 +020068 dev->fw_download_in_progress = true;
69 rc = dev->ops->fw_download(dev, firmware_name);
Eric Lapuyade9674da82013-04-29 17:13:27 +020070 if (rc)
Samuel Ortiz9ea71872013-07-31 01:19:43 +020071 dev->fw_download_in_progress = false;
Eric Lapuyade9674da82013-04-29 17:13:27 +020072
73error:
74 device_unlock(&dev->dev);
75 return rc;
76}
77
Eric Lapuyade352a5f52013-07-19 14:57:55 +020078/**
79 * nfc_fw_download_done - inform that a firmware download was completed
80 *
81 * @dev: The nfc device to which firmware was downloaded
82 * @firmware_name: The firmware filename
83 * @result: The positive value of a standard errno value
84 */
85int nfc_fw_download_done(struct nfc_dev *dev, const char *firmware_name,
86 u32 result)
Eric Lapuyade9674da82013-04-29 17:13:27 +020087{
Samuel Ortiz9ea71872013-07-31 01:19:43 +020088 dev->fw_download_in_progress = false;
Eric Lapuyade9674da82013-04-29 17:13:27 +020089
Eric Lapuyade352a5f52013-07-19 14:57:55 +020090 return nfc_genl_fw_download_done(dev, firmware_name, result);
Eric Lapuyade9674da82013-04-29 17:13:27 +020091}
Samuel Ortiz9ea71872013-07-31 01:19:43 +020092EXPORT_SYMBOL(nfc_fw_download_done);
Eric Lapuyade9674da82013-04-29 17:13:27 +020093
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -030094/**
Ilan Elias8b3fe7b2011-09-18 11:19:33 +030095 * nfc_dev_up - turn on the NFC device
96 *
97 * @dev: The nfc device to be turned on
98 *
99 * The device remains up until the nfc_dev_down function is called.
100 */
101int nfc_dev_up(struct nfc_dev *dev)
102{
103 int rc = 0;
104
Joe Perches20c239c2011-11-29 11:37:33 -0800105 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300106
107 device_lock(&dev->dev);
108
Samuel Ortizbe055b22013-04-11 11:52:20 +0200109 if (dev->rfkill && rfkill_blocked(dev->rfkill)) {
110 rc = -ERFKILL;
111 goto error;
112 }
113
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300114 if (!device_is_registered(&dev->dev)) {
115 rc = -ENODEV;
116 goto error;
117 }
118
Samuel Ortiz9ea71872013-07-31 01:19:43 +0200119 if (dev->fw_download_in_progress) {
Eric Lapuyade9674da82013-04-29 17:13:27 +0200120 rc = -EBUSY;
121 goto error;
122 }
123
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300124 if (dev->dev_up) {
125 rc = -EALREADY;
126 goto error;
127 }
128
129 if (dev->ops->dev_up)
130 rc = dev->ops->dev_up(dev);
131
132 if (!rc)
133 dev->dev_up = true;
134
Samuel Ortiz0a946302013-05-10 11:57:06 +0200135 /* We have to enable the device before discovering SEs */
Samuel Ortiza434c242013-12-22 01:00:20 +0100136 if (dev->ops->discover_se && dev->ops->discover_se(dev))
137 pr_err("SE discovery failed\n");
Samuel Ortiz0a946302013-05-10 11:57:06 +0200138
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300139error:
140 device_unlock(&dev->dev);
141 return rc;
142}
143
144/**
145 * nfc_dev_down - turn off the NFC device
146 *
147 * @dev: The nfc device to be turned off
148 */
149int nfc_dev_down(struct nfc_dev *dev)
150{
151 int rc = 0;
152
Joe Perches20c239c2011-11-29 11:37:33 -0800153 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300154
155 device_lock(&dev->dev);
156
157 if (!device_is_registered(&dev->dev)) {
158 rc = -ENODEV;
159 goto error;
160 }
161
162 if (!dev->dev_up) {
163 rc = -EALREADY;
164 goto error;
165 }
166
Eric Lapuyade90099432012-05-07 12:31:13 +0200167 if (dev->polling || dev->active_target) {
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300168 rc = -EBUSY;
169 goto error;
170 }
171
172 if (dev->ops->dev_down)
173 dev->ops->dev_down(dev);
174
175 dev->dev_up = false;
176
177error:
178 device_unlock(&dev->dev);
179 return rc;
180}
181
Samuel Ortizbe055b22013-04-11 11:52:20 +0200182static int nfc_rfkill_set_block(void *data, bool blocked)
183{
184 struct nfc_dev *dev = data;
185
186 pr_debug("%s blocked %d", dev_name(&dev->dev), blocked);
187
188 if (!blocked)
189 return 0;
190
191 nfc_dev_down(dev);
192
193 return 0;
194}
195
196static const struct rfkill_ops nfc_rfkill_ops = {
197 .set_block = nfc_rfkill_set_block,
198};
199
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300200/**
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300201 * nfc_start_poll - start polling for nfc targets
202 *
203 * @dev: The nfc device that must start polling
204 * @protocols: bitset of nfc protocols that must be used for polling
205 *
206 * The device remains polling for targets until a target is found or
207 * the nfc_stop_poll function is called.
208 */
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200209int nfc_start_poll(struct nfc_dev *dev, u32 im_protocols, u32 tm_protocols)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300210{
211 int rc;
212
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200213 pr_debug("dev_name %s initiator protocols 0x%x target protocols 0x%x\n",
214 dev_name(&dev->dev), im_protocols, tm_protocols);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300215
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200216 if (!im_protocols && !tm_protocols)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300217 return -EINVAL;
218
219 device_lock(&dev->dev);
220
221 if (!device_is_registered(&dev->dev)) {
222 rc = -ENODEV;
223 goto error;
224 }
225
Samuel Ortiz7757dc82013-04-10 12:25:30 +0200226 if (!dev->dev_up) {
227 rc = -ENODEV;
228 goto error;
229 }
230
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300231 if (dev->polling) {
232 rc = -EBUSY;
233 goto error;
234 }
235
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200236 rc = dev->ops->start_poll(dev, im_protocols, tm_protocols);
Samuel Ortizf212ad52012-05-31 00:02:26 +0200237 if (!rc) {
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300238 dev->polling = true;
Samuel Ortizf212ad52012-05-31 00:02:26 +0200239 dev->rf_mode = NFC_RF_NONE;
240 }
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300241
242error:
243 device_unlock(&dev->dev);
244 return rc;
245}
246
247/**
248 * nfc_stop_poll - stop polling for nfc targets
249 *
250 * @dev: The nfc device that must stop polling
251 */
252int nfc_stop_poll(struct nfc_dev *dev)
253{
254 int rc = 0;
255
Joe Perches20c239c2011-11-29 11:37:33 -0800256 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300257
258 device_lock(&dev->dev);
259
260 if (!device_is_registered(&dev->dev)) {
261 rc = -ENODEV;
262 goto error;
263 }
264
265 if (!dev->polling) {
266 rc = -EINVAL;
267 goto error;
268 }
269
270 dev->ops->stop_poll(dev);
271 dev->polling = false;
Thierry Escande5bcf0992012-10-05 11:05:45 +0200272 dev->rf_mode = NFC_RF_NONE;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300273
274error:
275 device_unlock(&dev->dev);
276 return rc;
277}
278
Eric Lapuyade90099432012-05-07 12:31:13 +0200279static struct nfc_target *nfc_find_target(struct nfc_dev *dev, u32 target_idx)
280{
281 int i;
282
Szymon Janc0f450772012-10-17 15:23:39 +0200283 for (i = 0; i < dev->n_targets; i++) {
Eric Lapuyade90099432012-05-07 12:31:13 +0200284 if (dev->targets[i].idx == target_idx)
285 return &dev->targets[i];
286 }
287
288 return NULL;
289}
290
Samuel Ortiz47807d32012-03-05 01:03:50 +0100291int nfc_dep_link_up(struct nfc_dev *dev, int target_index, u8 comm_mode)
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100292{
293 int rc = 0;
Samuel Ortiz47807d32012-03-05 01:03:50 +0100294 u8 *gb;
295 size_t gb_len;
Eric Lapuyade90099432012-05-07 12:31:13 +0200296 struct nfc_target *target;
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100297
Samuel Ortiz47807d32012-03-05 01:03:50 +0100298 pr_debug("dev_name=%s comm %d\n", dev_name(&dev->dev), comm_mode);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100299
300 if (!dev->ops->dep_link_up)
301 return -EOPNOTSUPP;
302
303 device_lock(&dev->dev);
304
305 if (!device_is_registered(&dev->dev)) {
306 rc = -ENODEV;
307 goto error;
308 }
309
310 if (dev->dep_link_up == true) {
311 rc = -EALREADY;
312 goto error;
313 }
314
Samuel Ortiz47807d32012-03-05 01:03:50 +0100315 gb = nfc_llcp_general_bytes(dev, &gb_len);
316 if (gb_len > NFC_MAX_GT_LEN) {
317 rc = -EINVAL;
318 goto error;
319 }
320
Eric Lapuyade90099432012-05-07 12:31:13 +0200321 target = nfc_find_target(dev, target_index);
322 if (target == NULL) {
323 rc = -ENOTCONN;
324 goto error;
325 }
326
327 rc = dev->ops->dep_link_up(dev, target, comm_mode, gb, gb_len);
Samuel Ortizf212ad52012-05-31 00:02:26 +0200328 if (!rc) {
Eric Lapuyade90099432012-05-07 12:31:13 +0200329 dev->active_target = target;
Samuel Ortizf212ad52012-05-31 00:02:26 +0200330 dev->rf_mode = NFC_RF_INITIATOR;
331 }
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100332
333error:
334 device_unlock(&dev->dev);
335 return rc;
336}
337
338int nfc_dep_link_down(struct nfc_dev *dev)
339{
340 int rc = 0;
341
342 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
343
344 if (!dev->ops->dep_link_down)
345 return -EOPNOTSUPP;
346
347 device_lock(&dev->dev);
348
349 if (!device_is_registered(&dev->dev)) {
350 rc = -ENODEV;
351 goto error;
352 }
353
354 if (dev->dep_link_up == false) {
355 rc = -EALREADY;
356 goto error;
357 }
358
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100359 rc = dev->ops->dep_link_down(dev);
360 if (!rc) {
361 dev->dep_link_up = false;
Eric Lapuyade90099432012-05-07 12:31:13 +0200362 dev->active_target = NULL;
Thierry Escande5bcf0992012-10-05 11:05:45 +0200363 dev->rf_mode = NFC_RF_NONE;
Samuel Ortizd6469602011-12-14 16:43:12 +0100364 nfc_llcp_mac_is_down(dev);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100365 nfc_genl_dep_link_down_event(dev);
366 }
367
368error:
369 device_unlock(&dev->dev);
Thierry Escande5bcf0992012-10-05 11:05:45 +0200370
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100371 return rc;
372}
373
374int nfc_dep_link_is_up(struct nfc_dev *dev, u32 target_idx,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100375 u8 comm_mode, u8 rf_mode)
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100376{
377 dev->dep_link_up = true;
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100378
Arron Wangd31652a2013-11-14 17:03:41 +0800379 if (!dev->active_target && rf_mode == NFC_RF_INITIATOR) {
Samuel Ortize29a9e22013-08-21 14:46:20 +0200380 struct nfc_target *target;
381
382 target = nfc_find_target(dev, target_idx);
383 if (target == NULL)
384 return -ENOTCONN;
385
386 dev->active_target = target;
387 }
388
389 dev->polling = false;
390 dev->rf_mode = rf_mode;
391
Samuel Ortizd6469602011-12-14 16:43:12 +0100392 nfc_llcp_mac_is_up(dev, target_idx, comm_mode, rf_mode);
393
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100394 return nfc_genl_dep_link_up_event(dev, target_idx, comm_mode, rf_mode);
395}
396EXPORT_SYMBOL(nfc_dep_link_is_up);
397
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300398/**
399 * nfc_activate_target - prepare the target for data exchange
400 *
401 * @dev: The nfc device that found the target
402 * @target_idx: index of the target that must be activated
403 * @protocol: nfc protocol that will be used for data exchange
404 */
405int nfc_activate_target(struct nfc_dev *dev, u32 target_idx, u32 protocol)
406{
407 int rc;
Eric Lapuyade90099432012-05-07 12:31:13 +0200408 struct nfc_target *target;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300409
Joe Perches20c239c2011-11-29 11:37:33 -0800410 pr_debug("dev_name=%s target_idx=%u protocol=%u\n",
411 dev_name(&dev->dev), target_idx, protocol);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300412
413 device_lock(&dev->dev);
414
415 if (!device_is_registered(&dev->dev)) {
416 rc = -ENODEV;
417 goto error;
418 }
419
Eric Lapuyade90099432012-05-07 12:31:13 +0200420 if (dev->active_target) {
421 rc = -EBUSY;
422 goto error;
423 }
424
425 target = nfc_find_target(dev, target_idx);
426 if (target == NULL) {
427 rc = -ENOTCONN;
428 goto error;
429 }
430
431 rc = dev->ops->activate_target(dev, target, protocol);
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200432 if (!rc) {
Eric Lapuyade90099432012-05-07 12:31:13 +0200433 dev->active_target = target;
Samuel Ortizf212ad52012-05-31 00:02:26 +0200434 dev->rf_mode = NFC_RF_INITIATOR;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300435
Eric Lapuyadef0c91032012-11-26 18:06:27 +0100436 if (dev->ops->check_presence && !dev->shutting_down)
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200437 mod_timer(&dev->check_pres_timer, jiffies +
438 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
439 }
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300440
441error:
442 device_unlock(&dev->dev);
443 return rc;
444}
445
446/**
447 * nfc_deactivate_target - deactivate a nfc target
448 *
449 * @dev: The nfc device that found the target
450 * @target_idx: index of the target that must be deactivated
451 */
Christophe Ricard96d45812015-10-25 22:54:43 +0100452int nfc_deactivate_target(struct nfc_dev *dev, u32 target_idx, u8 mode)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300453{
454 int rc = 0;
455
Joe Perches20c239c2011-11-29 11:37:33 -0800456 pr_debug("dev_name=%s target_idx=%u\n",
457 dev_name(&dev->dev), target_idx);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300458
459 device_lock(&dev->dev);
460
461 if (!device_is_registered(&dev->dev)) {
462 rc = -ENODEV;
463 goto error;
464 }
465
Eric Lapuyade90099432012-05-07 12:31:13 +0200466 if (dev->active_target == NULL) {
467 rc = -ENOTCONN;
468 goto error;
469 }
470
471 if (dev->active_target->idx != target_idx) {
472 rc = -ENOTCONN;
473 goto error;
474 }
475
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200476 if (dev->ops->check_presence)
477 del_timer_sync(&dev->check_pres_timer);
478
Christophe Ricard96d45812015-10-25 22:54:43 +0100479 dev->ops->deactivate_target(dev, dev->active_target, mode);
Eric Lapuyade90099432012-05-07 12:31:13 +0200480 dev->active_target = NULL;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300481
482error:
483 device_unlock(&dev->dev);
484 return rc;
485}
486
487/**
488 * nfc_data_exchange - transceive data
489 *
490 * @dev: The nfc device that found the target
491 * @target_idx: index of the target
492 * @skb: data to be sent
493 * @cb: callback called when the response is received
494 * @cb_context: parameter for the callback function
495 *
496 * The user must wait for the callback before calling this function again.
497 */
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100498int nfc_data_exchange(struct nfc_dev *dev, u32 target_idx, struct sk_buff *skb,
499 data_exchange_cb_t cb, void *cb_context)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300500{
501 int rc;
502
Joe Perches20c239c2011-11-29 11:37:33 -0800503 pr_debug("dev_name=%s target_idx=%u skb->len=%u\n",
504 dev_name(&dev->dev), target_idx, skb->len);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300505
506 device_lock(&dev->dev);
507
508 if (!device_is_registered(&dev->dev)) {
509 rc = -ENODEV;
510 kfree_skb(skb);
511 goto error;
512 }
513
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +0200514 if (dev->rf_mode == NFC_RF_INITIATOR && dev->active_target != NULL) {
515 if (dev->active_target->idx != target_idx) {
516 rc = -EADDRNOTAVAIL;
517 kfree_skb(skb);
518 goto error;
519 }
520
521 if (dev->ops->check_presence)
522 del_timer_sync(&dev->check_pres_timer);
523
524 rc = dev->ops->im_transceive(dev, dev->active_target, skb, cb,
525 cb_context);
526
Eric Lapuyadef0c91032012-11-26 18:06:27 +0100527 if (!rc && dev->ops->check_presence && !dev->shutting_down)
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +0200528 mod_timer(&dev->check_pres_timer, jiffies +
529 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
530 } else if (dev->rf_mode == NFC_RF_TARGET && dev->ops->tm_send != NULL) {
531 rc = dev->ops->tm_send(dev, skb);
532 } else {
Eric Lapuyade144612c2012-04-10 19:43:11 +0200533 rc = -ENOTCONN;
534 kfree_skb(skb);
535 goto error;
536 }
537
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200538
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300539error:
540 device_unlock(&dev->dev);
541 return rc;
542}
543
Arron Wangd8eb18e2013-08-23 16:02:08 +0800544struct nfc_se *nfc_find_se(struct nfc_dev *dev, u32 se_idx)
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200545{
Axel Lin156cef82014-02-14 19:29:19 +0800546 struct nfc_se *se;
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200547
Axel Lin156cef82014-02-14 19:29:19 +0800548 list_for_each_entry(se, &dev->secure_elements, list)
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200549 if (se->idx == se_idx)
550 return se;
551
552 return NULL;
553}
Arron Wangd8eb18e2013-08-23 16:02:08 +0800554EXPORT_SYMBOL(nfc_find_se);
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200555
556int nfc_enable_se(struct nfc_dev *dev, u32 se_idx)
557{
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200558 struct nfc_se *se;
559 int rc;
560
561 pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
562
563 device_lock(&dev->dev);
564
565 if (!device_is_registered(&dev->dev)) {
566 rc = -ENODEV;
567 goto error;
568 }
569
570 if (!dev->dev_up) {
571 rc = -ENODEV;
572 goto error;
573 }
574
575 if (dev->polling) {
576 rc = -EBUSY;
577 goto error;
578 }
579
580 if (!dev->ops->enable_se || !dev->ops->disable_se) {
581 rc = -EOPNOTSUPP;
582 goto error;
583 }
584
Arron Wangd8eb18e2013-08-23 16:02:08 +0800585 se = nfc_find_se(dev, se_idx);
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200586 if (!se) {
587 rc = -EINVAL;
588 goto error;
589 }
590
Arron Wang2c383282013-07-30 14:35:35 +0200591 if (se->state == NFC_SE_ENABLED) {
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200592 rc = -EALREADY;
593 goto error;
594 }
595
596 rc = dev->ops->enable_se(dev, se_idx);
Arron Wang39525ee12013-07-30 14:40:05 +0200597 if (rc >= 0)
598 se->state = NFC_SE_ENABLED;
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200599
600error:
601 device_unlock(&dev->dev);
602 return rc;
603}
604
605int nfc_disable_se(struct nfc_dev *dev, u32 se_idx)
606{
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200607 struct nfc_se *se;
608 int rc;
609
610 pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
611
612 device_lock(&dev->dev);
613
614 if (!device_is_registered(&dev->dev)) {
615 rc = -ENODEV;
616 goto error;
617 }
618
619 if (!dev->dev_up) {
620 rc = -ENODEV;
621 goto error;
622 }
623
624 if (!dev->ops->enable_se || !dev->ops->disable_se) {
625 rc = -EOPNOTSUPP;
626 goto error;
627 }
628
Arron Wangd8eb18e2013-08-23 16:02:08 +0800629 se = nfc_find_se(dev, se_idx);
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200630 if (!se) {
631 rc = -EINVAL;
632 goto error;
633 }
634
Arron Wang2c383282013-07-30 14:35:35 +0200635 if (se->state == NFC_SE_DISABLED) {
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200636 rc = -EALREADY;
637 goto error;
638 }
639
640 rc = dev->ops->disable_se(dev, se_idx);
Arron Wang39525ee12013-07-30 14:40:05 +0200641 if (rc >= 0)
642 se->state = NFC_SE_DISABLED;
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200643
644error:
645 device_unlock(&dev->dev);
646 return rc;
647}
648
Samuel Ortiz541d9202011-12-14 16:43:10 +0100649int nfc_set_remote_general_bytes(struct nfc_dev *dev, u8 *gb, u8 gb_len)
650{
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100651 pr_debug("dev_name=%s gb_len=%d\n", dev_name(&dev->dev), gb_len);
Samuel Ortiz541d9202011-12-14 16:43:10 +0100652
Samuel Ortizd6469602011-12-14 16:43:12 +0100653 return nfc_llcp_set_remote_gb(dev, gb, gb_len);
Samuel Ortiz541d9202011-12-14 16:43:10 +0100654}
655EXPORT_SYMBOL(nfc_set_remote_general_bytes);
656
Samuel Ortizab73b752012-04-10 12:51:52 +0200657u8 *nfc_get_local_general_bytes(struct nfc_dev *dev, size_t *gb_len)
658{
659 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
660
661 return nfc_llcp_general_bytes(dev, gb_len);
662}
663EXPORT_SYMBOL(nfc_get_local_general_bytes);
664
Samuel Ortiz73167ce2012-05-31 00:05:50 +0200665int nfc_tm_data_received(struct nfc_dev *dev, struct sk_buff *skb)
666{
667 /* Only LLCP target mode for now */
668 if (dev->dep_link_up == false) {
669 kfree_skb(skb);
670 return -ENOLINK;
671 }
672
673 return nfc_llcp_data_received(dev, skb);
674}
675EXPORT_SYMBOL(nfc_tm_data_received);
676
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200677int nfc_tm_activated(struct nfc_dev *dev, u32 protocol, u8 comm_mode,
678 u8 *gb, size_t gb_len)
679{
680 int rc;
681
682 device_lock(&dev->dev);
683
684 dev->polling = false;
685
686 if (gb != NULL) {
687 rc = nfc_set_remote_general_bytes(dev, gb, gb_len);
688 if (rc < 0)
689 goto out;
690 }
691
Samuel Ortizf212ad52012-05-31 00:02:26 +0200692 dev->rf_mode = NFC_RF_TARGET;
693
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200694 if (protocol == NFC_PROTO_NFC_DEP_MASK)
695 nfc_dep_link_is_up(dev, 0, comm_mode, NFC_RF_TARGET);
696
697 rc = nfc_genl_tm_activated(dev, protocol);
698
699out:
700 device_unlock(&dev->dev);
701
702 return rc;
703}
704EXPORT_SYMBOL(nfc_tm_activated);
705
706int nfc_tm_deactivated(struct nfc_dev *dev)
707{
708 dev->dep_link_up = false;
Thierry Escande5bcf0992012-10-05 11:05:45 +0200709 dev->rf_mode = NFC_RF_NONE;
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200710
711 return nfc_genl_tm_deactivated(dev);
712}
713EXPORT_SYMBOL(nfc_tm_deactivated);
714
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300715/**
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +0100716 * nfc_alloc_send_skb - allocate a skb for data exchange responses
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300717 *
718 * @size: size to allocate
719 * @gfp: gfp flags
720 */
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +0100721struct sk_buff *nfc_alloc_send_skb(struct nfc_dev *dev, struct sock *sk,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100722 unsigned int flags, unsigned int size,
723 unsigned int *err)
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +0100724{
725 struct sk_buff *skb;
726 unsigned int total_size;
727
728 total_size = size +
729 dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE;
730
731 skb = sock_alloc_send_skb(sk, total_size, flags & MSG_DONTWAIT, err);
732 if (skb)
733 skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE);
734
735 return skb;
736}
737
738/**
739 * nfc_alloc_recv_skb - allocate a skb for data exchange responses
740 *
741 * @size: size to allocate
742 * @gfp: gfp flags
743 */
744struct sk_buff *nfc_alloc_recv_skb(unsigned int size, gfp_t gfp)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300745{
746 struct sk_buff *skb;
747 unsigned int total_size;
748
749 total_size = size + 1;
750 skb = alloc_skb(total_size, gfp);
751
752 if (skb)
753 skb_reserve(skb, 1);
754
755 return skb;
756}
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +0100757EXPORT_SYMBOL(nfc_alloc_recv_skb);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300758
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300759/**
760 * nfc_targets_found - inform that targets were found
761 *
762 * @dev: The nfc device that found the targets
763 * @targets: array of nfc targets found
764 * @ntargets: targets array size
765 *
766 * The device driver must call this function when one or many nfc targets
767 * are found. After calling this function, the device driver must stop
768 * polling for targets.
Eric Lapuyaded94f9c52012-05-03 16:33:32 +0200769 * NOTE: This function can be called with targets=NULL and n_targets=0 to
770 * notify a driver error, meaning that the polling operation cannot complete.
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200771 * IMPORTANT: this function must not be called from an atomic context.
772 * In addition, it must also not be called from a context that would prevent
773 * the NFC Core to call other nfc ops entry point concurrently.
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300774 */
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100775int nfc_targets_found(struct nfc_dev *dev,
776 struct nfc_target *targets, int n_targets)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300777{
Samuel Ortizc4fbb652012-04-10 19:43:09 +0200778 int i;
779
Joe Perches20c239c2011-11-29 11:37:33 -0800780 pr_debug("dev_name=%s n_targets=%d\n", dev_name(&dev->dev), n_targets);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300781
Samuel Ortizc4fbb652012-04-10 19:43:09 +0200782 for (i = 0; i < n_targets; i++)
Eric Lapuyade01ae0ee2012-04-10 19:43:10 +0200783 targets[i].idx = dev->target_next_idx++;
Samuel Ortizc4fbb652012-04-10 19:43:09 +0200784
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200785 device_lock(&dev->dev);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300786
Eric Lapuyade8668fdd2012-05-03 16:21:58 +0200787 if (dev->polling == false) {
788 device_unlock(&dev->dev);
789 return 0;
790 }
791
792 dev->polling = false;
793
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300794 dev->targets_generation++;
795
796 kfree(dev->targets);
Eric Lapuyaded94f9c52012-05-03 16:33:32 +0200797 dev->targets = NULL;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300798
Eric Lapuyaded94f9c52012-05-03 16:33:32 +0200799 if (targets) {
800 dev->targets = kmemdup(targets,
801 n_targets * sizeof(struct nfc_target),
802 GFP_ATOMIC);
803
804 if (!dev->targets) {
805 dev->n_targets = 0;
806 device_unlock(&dev->dev);
807 return -ENOMEM;
808 }
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300809 }
810
811 dev->n_targets = n_targets;
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200812 device_unlock(&dev->dev);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300813
814 nfc_genl_targets_found(dev);
815
816 return 0;
817}
818EXPORT_SYMBOL(nfc_targets_found);
819
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200820/**
821 * nfc_target_lost - inform that an activated target went out of field
822 *
823 * @dev: The nfc device that had the activated target in field
824 * @target_idx: the nfc index of the target
825 *
826 * The device driver must call this function when the activated target
827 * goes out of the field.
828 * IMPORTANT: this function must not be called from an atomic context.
829 * In addition, it must also not be called from a context that would prevent
830 * the NFC Core to call other nfc ops entry point concurrently.
831 */
Eric Lapuyadee1da0ef2012-04-10 19:43:05 +0200832int nfc_target_lost(struct nfc_dev *dev, u32 target_idx)
833{
834 struct nfc_target *tg;
835 int i;
836
837 pr_debug("dev_name %s n_target %d\n", dev_name(&dev->dev), target_idx);
838
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200839 device_lock(&dev->dev);
Eric Lapuyadee1da0ef2012-04-10 19:43:05 +0200840
841 for (i = 0; i < dev->n_targets; i++) {
842 tg = &dev->targets[i];
843 if (tg->idx == target_idx)
844 break;
845 }
846
847 if (i == dev->n_targets) {
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200848 device_unlock(&dev->dev);
Eric Lapuyadee1da0ef2012-04-10 19:43:05 +0200849 return -EINVAL;
850 }
851
852 dev->targets_generation++;
853 dev->n_targets--;
Eric Lapuyade90099432012-05-07 12:31:13 +0200854 dev->active_target = NULL;
Eric Lapuyadee1da0ef2012-04-10 19:43:05 +0200855
856 if (dev->n_targets) {
857 memcpy(&dev->targets[i], &dev->targets[i + 1],
858 (dev->n_targets - i) * sizeof(struct nfc_target));
859 } else {
860 kfree(dev->targets);
861 dev->targets = NULL;
862 }
863
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200864 device_unlock(&dev->dev);
Eric Lapuyadee1da0ef2012-04-10 19:43:05 +0200865
866 nfc_genl_target_lost(dev, target_idx);
867
868 return 0;
869}
870EXPORT_SYMBOL(nfc_target_lost);
871
Eric Lapuyade9eb334a2012-06-11 15:52:38 +0200872inline void nfc_driver_failure(struct nfc_dev *dev, int err)
Eric Lapuyade456411c2012-06-11 13:49:51 +0200873{
Eric Lapuyade9eb334a2012-06-11 15:52:38 +0200874 nfc_targets_found(dev, NULL, 0);
Eric Lapuyade456411c2012-06-11 13:49:51 +0200875}
876EXPORT_SYMBOL(nfc_driver_failure);
877
Samuel Ortizfed7c252013-05-10 15:28:38 +0200878int nfc_add_se(struct nfc_dev *dev, u32 se_idx, u16 type)
879{
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200880 struct nfc_se *se;
Samuel Ortiz2757c3722013-05-10 15:47:37 +0200881 int rc;
Samuel Ortizfed7c252013-05-10 15:28:38 +0200882
883 pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
884
Arron Wangd8eb18e2013-08-23 16:02:08 +0800885 se = nfc_find_se(dev, se_idx);
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200886 if (se)
887 return -EALREADY;
Samuel Ortizfed7c252013-05-10 15:28:38 +0200888
889 se = kzalloc(sizeof(struct nfc_se), GFP_KERNEL);
890 if (!se)
891 return -ENOMEM;
892
893 se->idx = se_idx;
894 se->type = type;
895 se->state = NFC_SE_DISABLED;
896 INIT_LIST_HEAD(&se->list);
897
898 list_add(&se->list, &dev->secure_elements);
899
Samuel Ortiz2757c3722013-05-10 15:47:37 +0200900 rc = nfc_genl_se_added(dev, se_idx, type);
901 if (rc < 0) {
902 list_del(&se->list);
903 kfree(se);
904
905 return rc;
906 }
907
Samuel Ortizfed7c252013-05-10 15:28:38 +0200908 return 0;
909}
910EXPORT_SYMBOL(nfc_add_se);
911
912int nfc_remove_se(struct nfc_dev *dev, u32 se_idx)
913{
914 struct nfc_se *se, *n;
Samuel Ortiz2757c3722013-05-10 15:47:37 +0200915 int rc;
Samuel Ortizfed7c252013-05-10 15:28:38 +0200916
917 pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
918
919 list_for_each_entry_safe(se, n, &dev->secure_elements, list)
920 if (se->idx == se_idx) {
Samuel Ortiz2757c3722013-05-10 15:47:37 +0200921 rc = nfc_genl_se_removed(dev, se_idx);
922 if (rc < 0)
923 return rc;
924
Samuel Ortizfed7c252013-05-10 15:28:38 +0200925 list_del(&se->list);
926 kfree(se);
927
928 return 0;
929 }
930
931 return -EINVAL;
932}
933EXPORT_SYMBOL(nfc_remove_se);
934
Christophe Ricard447b27c2015-02-01 22:26:16 +0100935int nfc_se_transaction(struct nfc_dev *dev, u8 se_idx,
936 struct nfc_evt_transaction *evt_transaction)
937{
938 int rc;
939
940 pr_debug("transaction: %x\n", se_idx);
941
942 device_lock(&dev->dev);
943
944 if (!evt_transaction) {
945 rc = -EPROTO;
946 goto out;
947 }
948
949 rc = nfc_genl_se_transaction(dev, se_idx, evt_transaction);
950out:
951 device_unlock(&dev->dev);
952 return rc;
953}
954EXPORT_SYMBOL(nfc_se_transaction);
955
Christophe Ricard9afec6d2015-12-23 23:45:18 +0100956int nfc_se_connectivity(struct nfc_dev *dev, u8 se_idx)
957{
958 int rc;
959
960 pr_debug("connectivity: %x\n", se_idx);
961
962 device_lock(&dev->dev);
963 rc = nfc_genl_se_connectivity(dev, se_idx);
964 device_unlock(&dev->dev);
965 return rc;
966}
967EXPORT_SYMBOL(nfc_se_connectivity);
968
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300969static void nfc_release(struct device *d)
970{
971 struct nfc_dev *dev = to_nfc_dev(d);
Samuel Ortizee656e92013-05-10 15:53:29 +0200972 struct nfc_se *se, *n;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300973
Joe Perches20c239c2011-11-29 11:37:33 -0800974 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300975
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300976 nfc_genl_data_exit(&dev->genl_data);
977 kfree(dev->targets);
Samuel Ortizee656e92013-05-10 15:53:29 +0200978
979 list_for_each_entry_safe(se, n, &dev->secure_elements, list) {
980 nfc_genl_se_removed(dev, se->idx);
981 list_del(&se->list);
982 kfree(se);
983 }
984
Johan Hovoldf73de3f2017-03-30 12:15:35 +0200985 ida_simple_remove(&nfc_index_ida, dev->idx);
986
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300987 kfree(dev);
988}
989
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200990static void nfc_check_pres_work(struct work_struct *work)
991{
992 struct nfc_dev *dev = container_of(work, struct nfc_dev,
993 check_pres_work);
994 int rc;
995
996 device_lock(&dev->dev);
997
Eric Lapuyade90099432012-05-07 12:31:13 +0200998 if (dev->active_target && timer_pending(&dev->check_pres_timer) == 0) {
999 rc = dev->ops->check_presence(dev, dev->active_target);
Eric Lapuyade632c0162012-10-02 17:27:36 +02001000 if (rc == -EOPNOTSUPP)
1001 goto exit;
Eric Lapuyadef0c91032012-11-26 18:06:27 +01001002 if (rc) {
Eric Lapuyaded4ccb132012-05-07 12:31:15 +02001003 u32 active_target_idx = dev->active_target->idx;
1004 device_unlock(&dev->dev);
1005 nfc_target_lost(dev, active_target_idx);
1006 return;
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +02001007 }
Eric Lapuyadef0c91032012-11-26 18:06:27 +01001008
1009 if (!dev->shutting_down)
1010 mod_timer(&dev->check_pres_timer, jiffies +
1011 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +02001012 }
1013
Eric Lapuyade632c0162012-10-02 17:27:36 +02001014exit:
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +02001015 device_unlock(&dev->dev);
1016}
1017
1018static void nfc_check_pres_timeout(unsigned long data)
1019{
1020 struct nfc_dev *dev = (struct nfc_dev *)data;
1021
Linus Torvalds916082b2012-10-02 16:01:31 -07001022 schedule_work(&dev->check_pres_work);
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +02001023}
1024
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001025struct class nfc_class = {
1026 .name = "nfc",
1027 .dev_release = nfc_release,
1028};
1029EXPORT_SYMBOL(nfc_class);
1030
Michał Mirosław9f3b7952013-02-01 20:40:17 +01001031static int match_idx(struct device *d, const void *data)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001032{
1033 struct nfc_dev *dev = to_nfc_dev(d);
Michał Mirosław9f3b7952013-02-01 20:40:17 +01001034 const unsigned int *idx = data;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001035
1036 return dev->idx == *idx;
1037}
1038
Eric Dumazet95c96172012-04-15 05:58:06 +00001039struct nfc_dev *nfc_get_device(unsigned int idx)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001040{
1041 struct device *d;
1042
1043 d = class_find_device(&nfc_class, NULL, &idx, match_idx);
1044 if (!d)
1045 return NULL;
1046
1047 return to_nfc_dev(d);
1048}
1049
1050/**
1051 * nfc_allocate_device - allocate a new nfc device
1052 *
1053 * @ops: device operations
1054 * @supported_protocols: NFC protocols supported by the device
1055 */
1056struct nfc_dev *nfc_allocate_device(struct nfc_ops *ops,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +01001057 u32 supported_protocols,
1058 int tx_headroom, int tx_tailroom)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001059{
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001060 struct nfc_dev *dev;
Johan Hovoldf73de3f2017-03-30 12:15:35 +02001061 int rc;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001062
1063 if (!ops->start_poll || !ops->stop_poll || !ops->activate_target ||
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02001064 !ops->deactivate_target || !ops->im_transceive)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001065 return NULL;
1066
1067 if (!supported_protocols)
1068 return NULL;
1069
1070 dev = kzalloc(sizeof(struct nfc_dev), GFP_KERNEL);
1071 if (!dev)
1072 return NULL;
1073
Johan Hovoldf73de3f2017-03-30 12:15:35 +02001074 rc = ida_simple_get(&nfc_index_ida, 0, 0, GFP_KERNEL);
1075 if (rc < 0)
1076 goto err_free_dev;
1077 dev->idx = rc;
1078
1079 dev->dev.class = &nfc_class;
1080 dev_set_name(&dev->dev, "nfc%d", dev->idx);
1081 device_initialize(&dev->dev);
1082
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001083 dev->ops = ops;
1084 dev->supported_protocols = supported_protocols;
Samuel Ortize8753042011-08-19 15:47:11 +02001085 dev->tx_headroom = tx_headroom;
1086 dev->tx_tailroom = tx_tailroom;
Samuel Ortizfed7c252013-05-10 15:28:38 +02001087 INIT_LIST_HEAD(&dev->secure_elements);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001088
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001089 nfc_genl_data_init(&dev->genl_data);
1090
Thierry Escande5bcf0992012-10-05 11:05:45 +02001091 dev->rf_mode = NFC_RF_NONE;
Eric Lapuyaded4ccb132012-05-07 12:31:15 +02001092
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001093 /* first generation must not be 0 */
1094 dev->targets_generation = 1;
1095
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +02001096 if (ops->check_presence) {
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +02001097 init_timer(&dev->check_pres_timer);
1098 dev->check_pres_timer.data = (unsigned long)dev;
1099 dev->check_pres_timer.function = nfc_check_pres_timeout;
1100
1101 INIT_WORK(&dev->check_pres_work, nfc_check_pres_work);
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +02001102 }
1103
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001104 return dev;
Johan Hovoldf73de3f2017-03-30 12:15:35 +02001105
1106err_free_dev:
1107 kfree(dev);
1108
Johan Hovold12d7b502017-07-09 13:08:58 +02001109 return NULL;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001110}
1111EXPORT_SYMBOL(nfc_allocate_device);
1112
1113/**
1114 * nfc_register_device - register a nfc device in the nfc subsystem
1115 *
1116 * @dev: The nfc device to register
1117 */
1118int nfc_register_device(struct nfc_dev *dev)
1119{
1120 int rc;
1121
Joe Perches20c239c2011-11-29 11:37:33 -08001122 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001123
1124 mutex_lock(&nfc_devlist_mutex);
1125 nfc_devlist_generation++;
1126 rc = device_add(&dev->dev);
1127 mutex_unlock(&nfc_devlist_mutex);
1128
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001129 if (rc < 0)
1130 return rc;
1131
Samuel Ortizd6469602011-12-14 16:43:12 +01001132 rc = nfc_llcp_register_device(dev);
1133 if (rc)
1134 pr_err("Could not register llcp device\n");
1135
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001136 rc = nfc_genl_device_added(dev);
1137 if (rc)
Joe Perches20c239c2011-11-29 11:37:33 -08001138 pr_debug("The userspace won't be notified that the device %s was added\n",
1139 dev_name(&dev->dev));
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001140
Samuel Ortizbe055b22013-04-11 11:52:20 +02001141 dev->rfkill = rfkill_alloc(dev_name(&dev->dev), &dev->dev,
1142 RFKILL_TYPE_NFC, &nfc_rfkill_ops, dev);
1143 if (dev->rfkill) {
1144 if (rfkill_register(dev->rfkill) < 0) {
1145 rfkill_destroy(dev->rfkill);
1146 dev->rfkill = NULL;
1147 }
1148 }
1149
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001150 return 0;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001151}
1152EXPORT_SYMBOL(nfc_register_device);
1153
1154/**
1155 * nfc_unregister_device - unregister a nfc device in the nfc subsystem
1156 *
1157 * @dev: The nfc device to unregister
1158 */
1159void nfc_unregister_device(struct nfc_dev *dev)
1160{
Johan Hovoldf73de3f2017-03-30 12:15:35 +02001161 int rc;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001162
Joe Perches20c239c2011-11-29 11:37:33 -08001163 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001164
Samuel Ortizbe055b22013-04-11 11:52:20 +02001165 if (dev->rfkill) {
1166 rfkill_unregister(dev->rfkill);
1167 rfkill_destroy(dev->rfkill);
1168 }
1169
Eric Lapuyadef0c91032012-11-26 18:06:27 +01001170 if (dev->ops->check_presence) {
1171 device_lock(&dev->dev);
1172 dev->shutting_down = true;
1173 device_unlock(&dev->dev);
1174 del_timer_sync(&dev->check_pres_timer);
1175 cancel_work_sync(&dev->check_pres_work);
1176 }
Samuel Ortizd6469602011-12-14 16:43:12 +01001177
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001178 rc = nfc_genl_device_removed(dev);
1179 if (rc)
Eric Lapuyadef0c91032012-11-26 18:06:27 +01001180 pr_debug("The userspace won't be notified that the device %s "
1181 "was removed\n", dev_name(&dev->dev));
1182
1183 nfc_llcp_unregister_device(dev);
1184
1185 mutex_lock(&nfc_devlist_mutex);
1186 nfc_devlist_generation++;
1187 device_del(&dev->dev);
1188 mutex_unlock(&nfc_devlist_mutex);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001189}
1190EXPORT_SYMBOL(nfc_unregister_device);
1191
1192static int __init nfc_init(void)
1193{
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001194 int rc;
1195
Joe Perchesed1e0ad2011-11-29 11:37:32 -08001196 pr_info("NFC Core ver %s\n", VERSION);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001197
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001198 rc = class_register(&nfc_class);
1199 if (rc)
1200 return rc;
1201
1202 rc = nfc_genl_init();
1203 if (rc)
1204 goto err_genl;
1205
1206 /* the first generation must not be 0 */
1207 nfc_devlist_generation = 1;
1208
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -03001209 rc = rawsock_init();
1210 if (rc)
1211 goto err_rawsock;
1212
Samuel Ortizd6469602011-12-14 16:43:12 +01001213 rc = nfc_llcp_init();
1214 if (rc)
1215 goto err_llcp_sock;
1216
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -03001217 rc = af_nfc_init();
1218 if (rc)
1219 goto err_af_nfc;
1220
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001221 return 0;
1222
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -03001223err_af_nfc:
Samuel Ortizd6469602011-12-14 16:43:12 +01001224 nfc_llcp_exit();
1225err_llcp_sock:
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -03001226 rawsock_exit();
1227err_rawsock:
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -03001228 nfc_genl_exit();
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001229err_genl:
1230 class_unregister(&nfc_class);
1231 return rc;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001232}
1233
1234static void __exit nfc_exit(void)
1235{
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -03001236 af_nfc_exit();
Samuel Ortizd6469602011-12-14 16:43:12 +01001237 nfc_llcp_exit();
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -03001238 rawsock_exit();
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001239 nfc_genl_exit();
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001240 class_unregister(&nfc_class);
1241}
1242
1243subsys_initcall(nfc_init);
1244module_exit(nfc_exit);
1245
1246MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
1247MODULE_DESCRIPTION("NFC Core ver " VERSION);
1248MODULE_VERSION(VERSION);
1249MODULE_LICENSE("GPL");
Samuel Ortiz1155bb62012-06-12 00:35:50 +02001250MODULE_ALIAS_NETPROTO(PF_NFC);
Samuel Ortiz5df16ca2012-06-12 16:54:16 +02001251MODULE_ALIAS_GENL_FAMILY(NFC_GENL_NAME);