blob: eb377f87bcae8207dcd97d61456c09f1b4b801d6 [file] [log] [blame]
Thomas Gleixner1ccea772019-05-19 15:51:43 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03002/*
3 * Copyright (C) 2011 Instituto Nokia de Tecnologia
4 *
5 * Authors:
6 * Lauro Ramos Venancio <lauro.venancio@openbossa.org>
7 * Aloisio Almeida Jr <aloisio.almeida@openbossa.org>
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03008 */
9
Samuel Ortiz52858b52011-12-14 16:43:05 +010010#define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
Joe Perchesed1e0ad2011-11-29 11:37:32 -080011
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -030012#include <linux/init.h>
13#include <linux/kernel.h>
14#include <linux/module.h>
15#include <linux/slab.h>
Samuel Ortizbe055b22013-04-11 11:52:20 +020016#include <linux/rfkill.h>
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +010017#include <linux/nfc.h>
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -030018
Samuel Ortiz5df16ca2012-06-12 16:54:16 +020019#include <net/genetlink.h>
20
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -030021#include "nfc.h"
22
23#define VERSION "0.1"
24
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +020025#define NFC_CHECK_PRES_FREQ_MS 2000
26
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -030027int nfc_devlist_generation;
28DEFINE_MUTEX(nfc_devlist_mutex);
29
Samuel Ortiz7eda8b8e92012-10-22 15:57:58 +020030/* NFC device ID bitmap */
31static DEFINE_IDA(nfc_index_ida);
32
Samuel Ortiz9ea71872013-07-31 01:19:43 +020033int nfc_fw_download(struct nfc_dev *dev, const char *firmware_name)
Eric Lapuyade9674da82013-04-29 17:13:27 +020034{
35 int rc = 0;
36
37 pr_debug("%s do firmware %s\n", dev_name(&dev->dev), firmware_name);
38
39 device_lock(&dev->dev);
40
41 if (!device_is_registered(&dev->dev)) {
42 rc = -ENODEV;
43 goto error;
44 }
45
46 if (dev->dev_up) {
47 rc = -EBUSY;
48 goto error;
49 }
50
Samuel Ortiz9ea71872013-07-31 01:19:43 +020051 if (!dev->ops->fw_download) {
Eric Lapuyade9674da82013-04-29 17:13:27 +020052 rc = -EOPNOTSUPP;
53 goto error;
54 }
55
Samuel Ortiz9ea71872013-07-31 01:19:43 +020056 dev->fw_download_in_progress = true;
57 rc = dev->ops->fw_download(dev, firmware_name);
Eric Lapuyade9674da82013-04-29 17:13:27 +020058 if (rc)
Samuel Ortiz9ea71872013-07-31 01:19:43 +020059 dev->fw_download_in_progress = false;
Eric Lapuyade9674da82013-04-29 17:13:27 +020060
61error:
62 device_unlock(&dev->dev);
63 return rc;
64}
65
Eric Lapuyade352a5f52013-07-19 14:57:55 +020066/**
67 * nfc_fw_download_done - inform that a firmware download was completed
68 *
69 * @dev: The nfc device to which firmware was downloaded
70 * @firmware_name: The firmware filename
71 * @result: The positive value of a standard errno value
72 */
73int nfc_fw_download_done(struct nfc_dev *dev, const char *firmware_name,
74 u32 result)
Eric Lapuyade9674da82013-04-29 17:13:27 +020075{
Samuel Ortiz9ea71872013-07-31 01:19:43 +020076 dev->fw_download_in_progress = false;
Eric Lapuyade9674da82013-04-29 17:13:27 +020077
Eric Lapuyade352a5f52013-07-19 14:57:55 +020078 return nfc_genl_fw_download_done(dev, firmware_name, result);
Eric Lapuyade9674da82013-04-29 17:13:27 +020079}
Samuel Ortiz9ea71872013-07-31 01:19:43 +020080EXPORT_SYMBOL(nfc_fw_download_done);
Eric Lapuyade9674da82013-04-29 17:13:27 +020081
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -030082/**
Ilan Elias8b3fe7b2011-09-18 11:19:33 +030083 * nfc_dev_up - turn on the NFC device
84 *
85 * @dev: The nfc device to be turned on
86 *
87 * The device remains up until the nfc_dev_down function is called.
88 */
89int nfc_dev_up(struct nfc_dev *dev)
90{
91 int rc = 0;
92
Joe Perches20c239c2011-11-29 11:37:33 -080093 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Ilan Elias8b3fe7b2011-09-18 11:19:33 +030094
95 device_lock(&dev->dev);
96
Samuel Ortizbe055b22013-04-11 11:52:20 +020097 if (dev->rfkill && rfkill_blocked(dev->rfkill)) {
98 rc = -ERFKILL;
99 goto error;
100 }
101
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300102 if (!device_is_registered(&dev->dev)) {
103 rc = -ENODEV;
104 goto error;
105 }
106
Samuel Ortiz9ea71872013-07-31 01:19:43 +0200107 if (dev->fw_download_in_progress) {
Eric Lapuyade9674da82013-04-29 17:13:27 +0200108 rc = -EBUSY;
109 goto error;
110 }
111
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300112 if (dev->dev_up) {
113 rc = -EALREADY;
114 goto error;
115 }
116
117 if (dev->ops->dev_up)
118 rc = dev->ops->dev_up(dev);
119
120 if (!rc)
121 dev->dev_up = true;
122
Samuel Ortiz0a946302013-05-10 11:57:06 +0200123 /* We have to enable the device before discovering SEs */
Samuel Ortiza434c242013-12-22 01:00:20 +0100124 if (dev->ops->discover_se && dev->ops->discover_se(dev))
125 pr_err("SE discovery failed\n");
Samuel Ortiz0a946302013-05-10 11:57:06 +0200126
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300127error:
128 device_unlock(&dev->dev);
129 return rc;
130}
131
132/**
133 * nfc_dev_down - turn off the NFC device
134 *
135 * @dev: The nfc device to be turned off
136 */
137int nfc_dev_down(struct nfc_dev *dev)
138{
139 int rc = 0;
140
Joe Perches20c239c2011-11-29 11:37:33 -0800141 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300142
143 device_lock(&dev->dev);
144
145 if (!device_is_registered(&dev->dev)) {
146 rc = -ENODEV;
147 goto error;
148 }
149
150 if (!dev->dev_up) {
151 rc = -EALREADY;
152 goto error;
153 }
154
Eric Lapuyade90099432012-05-07 12:31:13 +0200155 if (dev->polling || dev->active_target) {
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300156 rc = -EBUSY;
157 goto error;
158 }
159
160 if (dev->ops->dev_down)
161 dev->ops->dev_down(dev);
162
163 dev->dev_up = false;
164
165error:
166 device_unlock(&dev->dev);
167 return rc;
168}
169
Samuel Ortizbe055b22013-04-11 11:52:20 +0200170static int nfc_rfkill_set_block(void *data, bool blocked)
171{
172 struct nfc_dev *dev = data;
173
174 pr_debug("%s blocked %d", dev_name(&dev->dev), blocked);
175
176 if (!blocked)
177 return 0;
178
179 nfc_dev_down(dev);
180
181 return 0;
182}
183
184static const struct rfkill_ops nfc_rfkill_ops = {
185 .set_block = nfc_rfkill_set_block,
186};
187
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300188/**
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300189 * nfc_start_poll - start polling for nfc targets
190 *
191 * @dev: The nfc device that must start polling
192 * @protocols: bitset of nfc protocols that must be used for polling
193 *
194 * The device remains polling for targets until a target is found or
195 * the nfc_stop_poll function is called.
196 */
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200197int nfc_start_poll(struct nfc_dev *dev, u32 im_protocols, u32 tm_protocols)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300198{
199 int rc;
200
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200201 pr_debug("dev_name %s initiator protocols 0x%x target protocols 0x%x\n",
202 dev_name(&dev->dev), im_protocols, tm_protocols);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300203
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200204 if (!im_protocols && !tm_protocols)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300205 return -EINVAL;
206
207 device_lock(&dev->dev);
208
209 if (!device_is_registered(&dev->dev)) {
210 rc = -ENODEV;
211 goto error;
212 }
213
Samuel Ortiz7757dc82013-04-10 12:25:30 +0200214 if (!dev->dev_up) {
215 rc = -ENODEV;
216 goto error;
217 }
218
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300219 if (dev->polling) {
220 rc = -EBUSY;
221 goto error;
222 }
223
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200224 rc = dev->ops->start_poll(dev, im_protocols, tm_protocols);
Samuel Ortizf212ad52012-05-31 00:02:26 +0200225 if (!rc) {
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300226 dev->polling = true;
Samuel Ortizf212ad52012-05-31 00:02:26 +0200227 dev->rf_mode = NFC_RF_NONE;
228 }
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300229
230error:
231 device_unlock(&dev->dev);
232 return rc;
233}
234
235/**
236 * nfc_stop_poll - stop polling for nfc targets
237 *
238 * @dev: The nfc device that must stop polling
239 */
240int nfc_stop_poll(struct nfc_dev *dev)
241{
242 int rc = 0;
243
Joe Perches20c239c2011-11-29 11:37:33 -0800244 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300245
246 device_lock(&dev->dev);
247
248 if (!device_is_registered(&dev->dev)) {
249 rc = -ENODEV;
250 goto error;
251 }
252
253 if (!dev->polling) {
254 rc = -EINVAL;
255 goto error;
256 }
257
258 dev->ops->stop_poll(dev);
259 dev->polling = false;
Thierry Escande5bcf0992012-10-05 11:05:45 +0200260 dev->rf_mode = NFC_RF_NONE;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300261
262error:
263 device_unlock(&dev->dev);
264 return rc;
265}
266
Eric Lapuyade90099432012-05-07 12:31:13 +0200267static struct nfc_target *nfc_find_target(struct nfc_dev *dev, u32 target_idx)
268{
269 int i;
270
Szymon Janc0f450772012-10-17 15:23:39 +0200271 for (i = 0; i < dev->n_targets; i++) {
Eric Lapuyade90099432012-05-07 12:31:13 +0200272 if (dev->targets[i].idx == target_idx)
273 return &dev->targets[i];
274 }
275
276 return NULL;
277}
278
Samuel Ortiz47807d32012-03-05 01:03:50 +0100279int nfc_dep_link_up(struct nfc_dev *dev, int target_index, u8 comm_mode)
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100280{
281 int rc = 0;
Samuel Ortiz47807d32012-03-05 01:03:50 +0100282 u8 *gb;
283 size_t gb_len;
Eric Lapuyade90099432012-05-07 12:31:13 +0200284 struct nfc_target *target;
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100285
Samuel Ortiz47807d32012-03-05 01:03:50 +0100286 pr_debug("dev_name=%s comm %d\n", dev_name(&dev->dev), comm_mode);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100287
288 if (!dev->ops->dep_link_up)
289 return -EOPNOTSUPP;
290
291 device_lock(&dev->dev);
292
293 if (!device_is_registered(&dev->dev)) {
294 rc = -ENODEV;
295 goto error;
296 }
297
298 if (dev->dep_link_up == true) {
299 rc = -EALREADY;
300 goto error;
301 }
302
Samuel Ortiz47807d32012-03-05 01:03:50 +0100303 gb = nfc_llcp_general_bytes(dev, &gb_len);
304 if (gb_len > NFC_MAX_GT_LEN) {
305 rc = -EINVAL;
306 goto error;
307 }
308
Eric Lapuyade90099432012-05-07 12:31:13 +0200309 target = nfc_find_target(dev, target_index);
310 if (target == NULL) {
311 rc = -ENOTCONN;
312 goto error;
313 }
314
315 rc = dev->ops->dep_link_up(dev, target, comm_mode, gb, gb_len);
Samuel Ortizf212ad52012-05-31 00:02:26 +0200316 if (!rc) {
Eric Lapuyade90099432012-05-07 12:31:13 +0200317 dev->active_target = target;
Samuel Ortizf212ad52012-05-31 00:02:26 +0200318 dev->rf_mode = NFC_RF_INITIATOR;
319 }
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100320
321error:
322 device_unlock(&dev->dev);
323 return rc;
324}
325
326int nfc_dep_link_down(struct nfc_dev *dev)
327{
328 int rc = 0;
329
330 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
331
332 if (!dev->ops->dep_link_down)
333 return -EOPNOTSUPP;
334
335 device_lock(&dev->dev);
336
337 if (!device_is_registered(&dev->dev)) {
338 rc = -ENODEV;
339 goto error;
340 }
341
342 if (dev->dep_link_up == false) {
343 rc = -EALREADY;
344 goto error;
345 }
346
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100347 rc = dev->ops->dep_link_down(dev);
348 if (!rc) {
349 dev->dep_link_up = false;
Eric Lapuyade90099432012-05-07 12:31:13 +0200350 dev->active_target = NULL;
Thierry Escande5bcf0992012-10-05 11:05:45 +0200351 dev->rf_mode = NFC_RF_NONE;
Samuel Ortizd6469602011-12-14 16:43:12 +0100352 nfc_llcp_mac_is_down(dev);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100353 nfc_genl_dep_link_down_event(dev);
354 }
355
356error:
357 device_unlock(&dev->dev);
Thierry Escande5bcf0992012-10-05 11:05:45 +0200358
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100359 return rc;
360}
361
362int nfc_dep_link_is_up(struct nfc_dev *dev, u32 target_idx,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100363 u8 comm_mode, u8 rf_mode)
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100364{
365 dev->dep_link_up = true;
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100366
Arron Wangd31652a2013-11-14 17:03:41 +0800367 if (!dev->active_target && rf_mode == NFC_RF_INITIATOR) {
Samuel Ortize29a9e22013-08-21 14:46:20 +0200368 struct nfc_target *target;
369
370 target = nfc_find_target(dev, target_idx);
371 if (target == NULL)
372 return -ENOTCONN;
373
374 dev->active_target = target;
375 }
376
377 dev->polling = false;
378 dev->rf_mode = rf_mode;
379
Samuel Ortizd6469602011-12-14 16:43:12 +0100380 nfc_llcp_mac_is_up(dev, target_idx, comm_mode, rf_mode);
381
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100382 return nfc_genl_dep_link_up_event(dev, target_idx, comm_mode, rf_mode);
383}
384EXPORT_SYMBOL(nfc_dep_link_is_up);
385
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300386/**
387 * nfc_activate_target - prepare the target for data exchange
388 *
389 * @dev: The nfc device that found the target
390 * @target_idx: index of the target that must be activated
391 * @protocol: nfc protocol that will be used for data exchange
392 */
393int nfc_activate_target(struct nfc_dev *dev, u32 target_idx, u32 protocol)
394{
395 int rc;
Eric Lapuyade90099432012-05-07 12:31:13 +0200396 struct nfc_target *target;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300397
Joe Perches20c239c2011-11-29 11:37:33 -0800398 pr_debug("dev_name=%s target_idx=%u protocol=%u\n",
399 dev_name(&dev->dev), target_idx, protocol);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300400
401 device_lock(&dev->dev);
402
403 if (!device_is_registered(&dev->dev)) {
404 rc = -ENODEV;
405 goto error;
406 }
407
Eric Lapuyade90099432012-05-07 12:31:13 +0200408 if (dev->active_target) {
409 rc = -EBUSY;
410 goto error;
411 }
412
413 target = nfc_find_target(dev, target_idx);
414 if (target == NULL) {
415 rc = -ENOTCONN;
416 goto error;
417 }
418
419 rc = dev->ops->activate_target(dev, target, protocol);
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200420 if (!rc) {
Eric Lapuyade90099432012-05-07 12:31:13 +0200421 dev->active_target = target;
Samuel Ortizf212ad52012-05-31 00:02:26 +0200422 dev->rf_mode = NFC_RF_INITIATOR;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300423
Eric Lapuyadef0c91032012-11-26 18:06:27 +0100424 if (dev->ops->check_presence && !dev->shutting_down)
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200425 mod_timer(&dev->check_pres_timer, jiffies +
426 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
427 }
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300428
429error:
430 device_unlock(&dev->dev);
431 return rc;
432}
433
434/**
435 * nfc_deactivate_target - deactivate a nfc target
436 *
437 * @dev: The nfc device that found the target
438 * @target_idx: index of the target that must be deactivated
439 */
Christophe Ricard96d45812015-10-25 22:54:43 +0100440int nfc_deactivate_target(struct nfc_dev *dev, u32 target_idx, u8 mode)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300441{
442 int rc = 0;
443
Joe Perches20c239c2011-11-29 11:37:33 -0800444 pr_debug("dev_name=%s target_idx=%u\n",
445 dev_name(&dev->dev), target_idx);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300446
447 device_lock(&dev->dev);
448
449 if (!device_is_registered(&dev->dev)) {
450 rc = -ENODEV;
451 goto error;
452 }
453
Eric Lapuyade90099432012-05-07 12:31:13 +0200454 if (dev->active_target == NULL) {
455 rc = -ENOTCONN;
456 goto error;
457 }
458
459 if (dev->active_target->idx != target_idx) {
460 rc = -ENOTCONN;
461 goto error;
462 }
463
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200464 if (dev->ops->check_presence)
465 del_timer_sync(&dev->check_pres_timer);
466
Christophe Ricard96d45812015-10-25 22:54:43 +0100467 dev->ops->deactivate_target(dev, dev->active_target, mode);
Eric Lapuyade90099432012-05-07 12:31:13 +0200468 dev->active_target = NULL;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300469
470error:
471 device_unlock(&dev->dev);
472 return rc;
473}
474
475/**
476 * nfc_data_exchange - transceive data
477 *
478 * @dev: The nfc device that found the target
479 * @target_idx: index of the target
480 * @skb: data to be sent
481 * @cb: callback called when the response is received
482 * @cb_context: parameter for the callback function
483 *
484 * The user must wait for the callback before calling this function again.
485 */
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100486int nfc_data_exchange(struct nfc_dev *dev, u32 target_idx, struct sk_buff *skb,
487 data_exchange_cb_t cb, void *cb_context)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300488{
489 int rc;
490
Joe Perches20c239c2011-11-29 11:37:33 -0800491 pr_debug("dev_name=%s target_idx=%u skb->len=%u\n",
492 dev_name(&dev->dev), target_idx, skb->len);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300493
494 device_lock(&dev->dev);
495
496 if (!device_is_registered(&dev->dev)) {
497 rc = -ENODEV;
498 kfree_skb(skb);
499 goto error;
500 }
501
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +0200502 if (dev->rf_mode == NFC_RF_INITIATOR && dev->active_target != NULL) {
503 if (dev->active_target->idx != target_idx) {
504 rc = -EADDRNOTAVAIL;
505 kfree_skb(skb);
506 goto error;
507 }
508
509 if (dev->ops->check_presence)
510 del_timer_sync(&dev->check_pres_timer);
511
512 rc = dev->ops->im_transceive(dev, dev->active_target, skb, cb,
513 cb_context);
514
Eric Lapuyadef0c91032012-11-26 18:06:27 +0100515 if (!rc && dev->ops->check_presence && !dev->shutting_down)
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +0200516 mod_timer(&dev->check_pres_timer, jiffies +
517 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
518 } else if (dev->rf_mode == NFC_RF_TARGET && dev->ops->tm_send != NULL) {
519 rc = dev->ops->tm_send(dev, skb);
520 } else {
Eric Lapuyade144612c2012-04-10 19:43:11 +0200521 rc = -ENOTCONN;
522 kfree_skb(skb);
523 goto error;
524 }
525
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200526
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300527error:
528 device_unlock(&dev->dev);
529 return rc;
530}
531
Arron Wangd8eb18e2013-08-23 16:02:08 +0800532struct nfc_se *nfc_find_se(struct nfc_dev *dev, u32 se_idx)
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200533{
Axel Lin156cef82014-02-14 19:29:19 +0800534 struct nfc_se *se;
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200535
Axel Lin156cef82014-02-14 19:29:19 +0800536 list_for_each_entry(se, &dev->secure_elements, list)
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200537 if (se->idx == se_idx)
538 return se;
539
540 return NULL;
541}
Arron Wangd8eb18e2013-08-23 16:02:08 +0800542EXPORT_SYMBOL(nfc_find_se);
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200543
544int nfc_enable_se(struct nfc_dev *dev, u32 se_idx)
545{
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200546 struct nfc_se *se;
547 int rc;
548
549 pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
550
551 device_lock(&dev->dev);
552
553 if (!device_is_registered(&dev->dev)) {
554 rc = -ENODEV;
555 goto error;
556 }
557
558 if (!dev->dev_up) {
559 rc = -ENODEV;
560 goto error;
561 }
562
563 if (dev->polling) {
564 rc = -EBUSY;
565 goto error;
566 }
567
568 if (!dev->ops->enable_se || !dev->ops->disable_se) {
569 rc = -EOPNOTSUPP;
570 goto error;
571 }
572
Arron Wangd8eb18e2013-08-23 16:02:08 +0800573 se = nfc_find_se(dev, se_idx);
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200574 if (!se) {
575 rc = -EINVAL;
576 goto error;
577 }
578
Arron Wang2c383282013-07-30 14:35:35 +0200579 if (se->state == NFC_SE_ENABLED) {
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200580 rc = -EALREADY;
581 goto error;
582 }
583
584 rc = dev->ops->enable_se(dev, se_idx);
Arron Wang39525ee12013-07-30 14:40:05 +0200585 if (rc >= 0)
586 se->state = NFC_SE_ENABLED;
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200587
588error:
589 device_unlock(&dev->dev);
590 return rc;
591}
592
593int nfc_disable_se(struct nfc_dev *dev, u32 se_idx)
594{
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200595 struct nfc_se *se;
596 int rc;
597
598 pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
599
600 device_lock(&dev->dev);
601
602 if (!device_is_registered(&dev->dev)) {
603 rc = -ENODEV;
604 goto error;
605 }
606
607 if (!dev->dev_up) {
608 rc = -ENODEV;
609 goto error;
610 }
611
612 if (!dev->ops->enable_se || !dev->ops->disable_se) {
613 rc = -EOPNOTSUPP;
614 goto error;
615 }
616
Arron Wangd8eb18e2013-08-23 16:02:08 +0800617 se = nfc_find_se(dev, se_idx);
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200618 if (!se) {
619 rc = -EINVAL;
620 goto error;
621 }
622
Arron Wang2c383282013-07-30 14:35:35 +0200623 if (se->state == NFC_SE_DISABLED) {
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200624 rc = -EALREADY;
625 goto error;
626 }
627
628 rc = dev->ops->disable_se(dev, se_idx);
Arron Wang39525ee12013-07-30 14:40:05 +0200629 if (rc >= 0)
630 se->state = NFC_SE_DISABLED;
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200631
632error:
633 device_unlock(&dev->dev);
634 return rc;
635}
636
Samuel Ortiz541d9202011-12-14 16:43:10 +0100637int nfc_set_remote_general_bytes(struct nfc_dev *dev, u8 *gb, u8 gb_len)
638{
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100639 pr_debug("dev_name=%s gb_len=%d\n", dev_name(&dev->dev), gb_len);
Samuel Ortiz541d9202011-12-14 16:43:10 +0100640
Samuel Ortizd6469602011-12-14 16:43:12 +0100641 return nfc_llcp_set_remote_gb(dev, gb, gb_len);
Samuel Ortiz541d9202011-12-14 16:43:10 +0100642}
643EXPORT_SYMBOL(nfc_set_remote_general_bytes);
644
Samuel Ortizab73b752012-04-10 12:51:52 +0200645u8 *nfc_get_local_general_bytes(struct nfc_dev *dev, size_t *gb_len)
646{
647 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
648
649 return nfc_llcp_general_bytes(dev, gb_len);
650}
651EXPORT_SYMBOL(nfc_get_local_general_bytes);
652
Samuel Ortiz73167ce2012-05-31 00:05:50 +0200653int nfc_tm_data_received(struct nfc_dev *dev, struct sk_buff *skb)
654{
655 /* Only LLCP target mode for now */
656 if (dev->dep_link_up == false) {
657 kfree_skb(skb);
658 return -ENOLINK;
659 }
660
661 return nfc_llcp_data_received(dev, skb);
662}
663EXPORT_SYMBOL(nfc_tm_data_received);
664
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200665int nfc_tm_activated(struct nfc_dev *dev, u32 protocol, u8 comm_mode,
666 u8 *gb, size_t gb_len)
667{
668 int rc;
669
670 device_lock(&dev->dev);
671
672 dev->polling = false;
673
674 if (gb != NULL) {
675 rc = nfc_set_remote_general_bytes(dev, gb, gb_len);
676 if (rc < 0)
677 goto out;
678 }
679
Samuel Ortizf212ad52012-05-31 00:02:26 +0200680 dev->rf_mode = NFC_RF_TARGET;
681
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200682 if (protocol == NFC_PROTO_NFC_DEP_MASK)
683 nfc_dep_link_is_up(dev, 0, comm_mode, NFC_RF_TARGET);
684
685 rc = nfc_genl_tm_activated(dev, protocol);
686
687out:
688 device_unlock(&dev->dev);
689
690 return rc;
691}
692EXPORT_SYMBOL(nfc_tm_activated);
693
694int nfc_tm_deactivated(struct nfc_dev *dev)
695{
696 dev->dep_link_up = false;
Thierry Escande5bcf0992012-10-05 11:05:45 +0200697 dev->rf_mode = NFC_RF_NONE;
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200698
699 return nfc_genl_tm_deactivated(dev);
700}
701EXPORT_SYMBOL(nfc_tm_deactivated);
702
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300703/**
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +0100704 * nfc_alloc_send_skb - allocate a skb for data exchange responses
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300705 *
706 * @size: size to allocate
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300707 */
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +0100708struct sk_buff *nfc_alloc_send_skb(struct nfc_dev *dev, struct sock *sk,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100709 unsigned int flags, unsigned int size,
710 unsigned int *err)
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +0100711{
712 struct sk_buff *skb;
713 unsigned int total_size;
714
715 total_size = size +
716 dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE;
717
718 skb = sock_alloc_send_skb(sk, total_size, flags & MSG_DONTWAIT, err);
719 if (skb)
720 skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE);
721
722 return skb;
723}
724
725/**
726 * nfc_alloc_recv_skb - allocate a skb for data exchange responses
727 *
728 * @size: size to allocate
729 * @gfp: gfp flags
730 */
731struct sk_buff *nfc_alloc_recv_skb(unsigned int size, gfp_t gfp)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300732{
733 struct sk_buff *skb;
734 unsigned int total_size;
735
736 total_size = size + 1;
737 skb = alloc_skb(total_size, gfp);
738
739 if (skb)
740 skb_reserve(skb, 1);
741
742 return skb;
743}
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +0100744EXPORT_SYMBOL(nfc_alloc_recv_skb);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300745
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300746/**
747 * nfc_targets_found - inform that targets were found
748 *
749 * @dev: The nfc device that found the targets
750 * @targets: array of nfc targets found
Andrew Lunnffbab1c2020-07-13 01:15:08 +0200751 * @n_targets: targets array size
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300752 *
753 * The device driver must call this function when one or many nfc targets
754 * are found. After calling this function, the device driver must stop
755 * polling for targets.
Eric Lapuyaded94f9c52012-05-03 16:33:32 +0200756 * NOTE: This function can be called with targets=NULL and n_targets=0 to
757 * notify a driver error, meaning that the polling operation cannot complete.
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200758 * IMPORTANT: this function must not be called from an atomic context.
759 * In addition, it must also not be called from a context that would prevent
760 * the NFC Core to call other nfc ops entry point concurrently.
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300761 */
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100762int nfc_targets_found(struct nfc_dev *dev,
763 struct nfc_target *targets, int n_targets)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300764{
Samuel Ortizc4fbb652012-04-10 19:43:09 +0200765 int i;
766
Joe Perches20c239c2011-11-29 11:37:33 -0800767 pr_debug("dev_name=%s n_targets=%d\n", dev_name(&dev->dev), n_targets);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300768
Samuel Ortizc4fbb652012-04-10 19:43:09 +0200769 for (i = 0; i < n_targets; i++)
Eric Lapuyade01ae0ee2012-04-10 19:43:10 +0200770 targets[i].idx = dev->target_next_idx++;
Samuel Ortizc4fbb652012-04-10 19:43:09 +0200771
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200772 device_lock(&dev->dev);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300773
Eric Lapuyade8668fdd2012-05-03 16:21:58 +0200774 if (dev->polling == false) {
775 device_unlock(&dev->dev);
776 return 0;
777 }
778
779 dev->polling = false;
780
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300781 dev->targets_generation++;
782
783 kfree(dev->targets);
Eric Lapuyaded94f9c52012-05-03 16:33:32 +0200784 dev->targets = NULL;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300785
Eric Lapuyaded94f9c52012-05-03 16:33:32 +0200786 if (targets) {
787 dev->targets = kmemdup(targets,
788 n_targets * sizeof(struct nfc_target),
789 GFP_ATOMIC);
790
791 if (!dev->targets) {
792 dev->n_targets = 0;
793 device_unlock(&dev->dev);
794 return -ENOMEM;
795 }
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300796 }
797
798 dev->n_targets = n_targets;
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200799 device_unlock(&dev->dev);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300800
801 nfc_genl_targets_found(dev);
802
803 return 0;
804}
805EXPORT_SYMBOL(nfc_targets_found);
806
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200807/**
808 * nfc_target_lost - inform that an activated target went out of field
809 *
810 * @dev: The nfc device that had the activated target in field
811 * @target_idx: the nfc index of the target
812 *
813 * The device driver must call this function when the activated target
814 * goes out of the field.
815 * IMPORTANT: this function must not be called from an atomic context.
816 * In addition, it must also not be called from a context that would prevent
817 * the NFC Core to call other nfc ops entry point concurrently.
818 */
Eric Lapuyadee1da0ef2012-04-10 19:43:05 +0200819int nfc_target_lost(struct nfc_dev *dev, u32 target_idx)
820{
821 struct nfc_target *tg;
822 int i;
823
824 pr_debug("dev_name %s n_target %d\n", dev_name(&dev->dev), target_idx);
825
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200826 device_lock(&dev->dev);
Eric Lapuyadee1da0ef2012-04-10 19:43:05 +0200827
828 for (i = 0; i < dev->n_targets; i++) {
829 tg = &dev->targets[i];
830 if (tg->idx == target_idx)
831 break;
832 }
833
834 if (i == dev->n_targets) {
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200835 device_unlock(&dev->dev);
Eric Lapuyadee1da0ef2012-04-10 19:43:05 +0200836 return -EINVAL;
837 }
838
839 dev->targets_generation++;
840 dev->n_targets--;
Eric Lapuyade90099432012-05-07 12:31:13 +0200841 dev->active_target = NULL;
Eric Lapuyadee1da0ef2012-04-10 19:43:05 +0200842
843 if (dev->n_targets) {
844 memcpy(&dev->targets[i], &dev->targets[i + 1],
845 (dev->n_targets - i) * sizeof(struct nfc_target));
846 } else {
847 kfree(dev->targets);
848 dev->targets = NULL;
849 }
850
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200851 device_unlock(&dev->dev);
Eric Lapuyadee1da0ef2012-04-10 19:43:05 +0200852
853 nfc_genl_target_lost(dev, target_idx);
854
855 return 0;
856}
857EXPORT_SYMBOL(nfc_target_lost);
858
Eric Lapuyade9eb334a2012-06-11 15:52:38 +0200859inline void nfc_driver_failure(struct nfc_dev *dev, int err)
Eric Lapuyade456411c2012-06-11 13:49:51 +0200860{
Eric Lapuyade9eb334a2012-06-11 15:52:38 +0200861 nfc_targets_found(dev, NULL, 0);
Eric Lapuyade456411c2012-06-11 13:49:51 +0200862}
863EXPORT_SYMBOL(nfc_driver_failure);
864
Samuel Ortizfed7c252013-05-10 15:28:38 +0200865int nfc_add_se(struct nfc_dev *dev, u32 se_idx, u16 type)
866{
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200867 struct nfc_se *se;
Samuel Ortiz2757c3722013-05-10 15:47:37 +0200868 int rc;
Samuel Ortizfed7c252013-05-10 15:28:38 +0200869
870 pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
871
Arron Wangd8eb18e2013-08-23 16:02:08 +0800872 se = nfc_find_se(dev, se_idx);
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200873 if (se)
874 return -EALREADY;
Samuel Ortizfed7c252013-05-10 15:28:38 +0200875
876 se = kzalloc(sizeof(struct nfc_se), GFP_KERNEL);
877 if (!se)
878 return -ENOMEM;
879
880 se->idx = se_idx;
881 se->type = type;
882 se->state = NFC_SE_DISABLED;
883 INIT_LIST_HEAD(&se->list);
884
885 list_add(&se->list, &dev->secure_elements);
886
Samuel Ortiz2757c3722013-05-10 15:47:37 +0200887 rc = nfc_genl_se_added(dev, se_idx, type);
888 if (rc < 0) {
889 list_del(&se->list);
890 kfree(se);
891
892 return rc;
893 }
894
Samuel Ortizfed7c252013-05-10 15:28:38 +0200895 return 0;
896}
897EXPORT_SYMBOL(nfc_add_se);
898
899int nfc_remove_se(struct nfc_dev *dev, u32 se_idx)
900{
901 struct nfc_se *se, *n;
Samuel Ortiz2757c3722013-05-10 15:47:37 +0200902 int rc;
Samuel Ortizfed7c252013-05-10 15:28:38 +0200903
904 pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
905
906 list_for_each_entry_safe(se, n, &dev->secure_elements, list)
907 if (se->idx == se_idx) {
Samuel Ortiz2757c3722013-05-10 15:47:37 +0200908 rc = nfc_genl_se_removed(dev, se_idx);
909 if (rc < 0)
910 return rc;
911
Samuel Ortizfed7c252013-05-10 15:28:38 +0200912 list_del(&se->list);
913 kfree(se);
914
915 return 0;
916 }
917
918 return -EINVAL;
919}
920EXPORT_SYMBOL(nfc_remove_se);
921
Christophe Ricard447b27c2015-02-01 22:26:16 +0100922int nfc_se_transaction(struct nfc_dev *dev, u8 se_idx,
923 struct nfc_evt_transaction *evt_transaction)
924{
925 int rc;
926
927 pr_debug("transaction: %x\n", se_idx);
928
929 device_lock(&dev->dev);
930
931 if (!evt_transaction) {
932 rc = -EPROTO;
933 goto out;
934 }
935
936 rc = nfc_genl_se_transaction(dev, se_idx, evt_transaction);
937out:
938 device_unlock(&dev->dev);
939 return rc;
940}
941EXPORT_SYMBOL(nfc_se_transaction);
942
Christophe Ricard9afec6d2015-12-23 23:45:18 +0100943int nfc_se_connectivity(struct nfc_dev *dev, u8 se_idx)
944{
945 int rc;
946
947 pr_debug("connectivity: %x\n", se_idx);
948
949 device_lock(&dev->dev);
950 rc = nfc_genl_se_connectivity(dev, se_idx);
951 device_unlock(&dev->dev);
952 return rc;
953}
954EXPORT_SYMBOL(nfc_se_connectivity);
955
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300956static void nfc_release(struct device *d)
957{
958 struct nfc_dev *dev = to_nfc_dev(d);
Samuel Ortizee656e92013-05-10 15:53:29 +0200959 struct nfc_se *se, *n;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300960
Joe Perches20c239c2011-11-29 11:37:33 -0800961 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300962
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300963 nfc_genl_data_exit(&dev->genl_data);
964 kfree(dev->targets);
Samuel Ortizee656e92013-05-10 15:53:29 +0200965
966 list_for_each_entry_safe(se, n, &dev->secure_elements, list) {
967 nfc_genl_se_removed(dev, se->idx);
968 list_del(&se->list);
969 kfree(se);
970 }
971
Johan Hovold20777bc2017-03-30 12:15:35 +0200972 ida_simple_remove(&nfc_index_ida, dev->idx);
973
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300974 kfree(dev);
975}
976
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200977static void nfc_check_pres_work(struct work_struct *work)
978{
979 struct nfc_dev *dev = container_of(work, struct nfc_dev,
980 check_pres_work);
981 int rc;
982
983 device_lock(&dev->dev);
984
Eric Lapuyade90099432012-05-07 12:31:13 +0200985 if (dev->active_target && timer_pending(&dev->check_pres_timer) == 0) {
986 rc = dev->ops->check_presence(dev, dev->active_target);
Eric Lapuyade632c0162012-10-02 17:27:36 +0200987 if (rc == -EOPNOTSUPP)
988 goto exit;
Eric Lapuyadef0c91032012-11-26 18:06:27 +0100989 if (rc) {
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200990 u32 active_target_idx = dev->active_target->idx;
991 device_unlock(&dev->dev);
992 nfc_target_lost(dev, active_target_idx);
993 return;
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200994 }
Eric Lapuyadef0c91032012-11-26 18:06:27 +0100995
996 if (!dev->shutting_down)
997 mod_timer(&dev->check_pres_timer, jiffies +
998 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200999 }
1000
Eric Lapuyade632c0162012-10-02 17:27:36 +02001001exit:
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +02001002 device_unlock(&dev->dev);
1003}
1004
Allen Pais4b519bb2017-10-11 16:03:44 +05301005static void nfc_check_pres_timeout(struct timer_list *t)
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +02001006{
Allen Pais4b519bb2017-10-11 16:03:44 +05301007 struct nfc_dev *dev = from_timer(dev, t, check_pres_timer);
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +02001008
Linus Torvalds916082b2012-10-02 16:01:31 -07001009 schedule_work(&dev->check_pres_work);
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +02001010}
1011
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001012struct class nfc_class = {
1013 .name = "nfc",
1014 .dev_release = nfc_release,
1015};
1016EXPORT_SYMBOL(nfc_class);
1017
Michał Mirosław9f3b7952013-02-01 20:40:17 +01001018static int match_idx(struct device *d, const void *data)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001019{
1020 struct nfc_dev *dev = to_nfc_dev(d);
Michał Mirosław9f3b7952013-02-01 20:40:17 +01001021 const unsigned int *idx = data;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001022
1023 return dev->idx == *idx;
1024}
1025
Eric Dumazet95c96172012-04-15 05:58:06 +00001026struct nfc_dev *nfc_get_device(unsigned int idx)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001027{
1028 struct device *d;
1029
1030 d = class_find_device(&nfc_class, NULL, &idx, match_idx);
1031 if (!d)
1032 return NULL;
1033
1034 return to_nfc_dev(d);
1035}
1036
1037/**
1038 * nfc_allocate_device - allocate a new nfc device
1039 *
1040 * @ops: device operations
1041 * @supported_protocols: NFC protocols supported by the device
1042 */
1043struct nfc_dev *nfc_allocate_device(struct nfc_ops *ops,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +01001044 u32 supported_protocols,
1045 int tx_headroom, int tx_tailroom)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001046{
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001047 struct nfc_dev *dev;
Johan Hovold20777bc2017-03-30 12:15:35 +02001048 int rc;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001049
1050 if (!ops->start_poll || !ops->stop_poll || !ops->activate_target ||
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02001051 !ops->deactivate_target || !ops->im_transceive)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001052 return NULL;
1053
1054 if (!supported_protocols)
1055 return NULL;
1056
1057 dev = kzalloc(sizeof(struct nfc_dev), GFP_KERNEL);
1058 if (!dev)
1059 return NULL;
1060
Johan Hovold20777bc2017-03-30 12:15:35 +02001061 rc = ida_simple_get(&nfc_index_ida, 0, 0, GFP_KERNEL);
1062 if (rc < 0)
1063 goto err_free_dev;
1064 dev->idx = rc;
1065
1066 dev->dev.class = &nfc_class;
1067 dev_set_name(&dev->dev, "nfc%d", dev->idx);
1068 device_initialize(&dev->dev);
1069
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001070 dev->ops = ops;
1071 dev->supported_protocols = supported_protocols;
Samuel Ortize8753042011-08-19 15:47:11 +02001072 dev->tx_headroom = tx_headroom;
1073 dev->tx_tailroom = tx_tailroom;
Samuel Ortizfed7c252013-05-10 15:28:38 +02001074 INIT_LIST_HEAD(&dev->secure_elements);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001075
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001076 nfc_genl_data_init(&dev->genl_data);
1077
Thierry Escande5bcf0992012-10-05 11:05:45 +02001078 dev->rf_mode = NFC_RF_NONE;
Eric Lapuyaded4ccb132012-05-07 12:31:15 +02001079
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001080 /* first generation must not be 0 */
1081 dev->targets_generation = 1;
1082
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +02001083 if (ops->check_presence) {
Allen Pais4b519bb2017-10-11 16:03:44 +05301084 timer_setup(&dev->check_pres_timer, nfc_check_pres_timeout, 0);
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +02001085 INIT_WORK(&dev->check_pres_work, nfc_check_pres_work);
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +02001086 }
1087
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001088 return dev;
Johan Hovold20777bc2017-03-30 12:15:35 +02001089
1090err_free_dev:
1091 kfree(dev);
1092
Johan Hovoldc45e3e42017-07-09 13:08:58 +02001093 return NULL;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001094}
1095EXPORT_SYMBOL(nfc_allocate_device);
1096
1097/**
1098 * nfc_register_device - register a nfc device in the nfc subsystem
1099 *
1100 * @dev: The nfc device to register
1101 */
1102int nfc_register_device(struct nfc_dev *dev)
1103{
1104 int rc;
1105
Joe Perches20c239c2011-11-29 11:37:33 -08001106 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001107
1108 mutex_lock(&nfc_devlist_mutex);
1109 nfc_devlist_generation++;
1110 rc = device_add(&dev->dev);
1111 mutex_unlock(&nfc_devlist_mutex);
1112
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001113 if (rc < 0)
1114 return rc;
1115
Samuel Ortizd6469602011-12-14 16:43:12 +01001116 rc = nfc_llcp_register_device(dev);
1117 if (rc)
1118 pr_err("Could not register llcp device\n");
1119
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001120 rc = nfc_genl_device_added(dev);
1121 if (rc)
Joe Perches20c239c2011-11-29 11:37:33 -08001122 pr_debug("The userspace won't be notified that the device %s was added\n",
1123 dev_name(&dev->dev));
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001124
Samuel Ortizbe055b22013-04-11 11:52:20 +02001125 dev->rfkill = rfkill_alloc(dev_name(&dev->dev), &dev->dev,
1126 RFKILL_TYPE_NFC, &nfc_rfkill_ops, dev);
1127 if (dev->rfkill) {
1128 if (rfkill_register(dev->rfkill) < 0) {
1129 rfkill_destroy(dev->rfkill);
1130 dev->rfkill = NULL;
1131 }
1132 }
1133
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001134 return 0;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001135}
1136EXPORT_SYMBOL(nfc_register_device);
1137
1138/**
1139 * nfc_unregister_device - unregister a nfc device in the nfc subsystem
1140 *
1141 * @dev: The nfc device to unregister
1142 */
1143void nfc_unregister_device(struct nfc_dev *dev)
1144{
Johan Hovold20777bc2017-03-30 12:15:35 +02001145 int rc;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001146
Joe Perches20c239c2011-11-29 11:37:33 -08001147 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001148
Samuel Ortizbe055b22013-04-11 11:52:20 +02001149 if (dev->rfkill) {
1150 rfkill_unregister(dev->rfkill);
1151 rfkill_destroy(dev->rfkill);
1152 }
1153
Eric Lapuyadef0c91032012-11-26 18:06:27 +01001154 if (dev->ops->check_presence) {
1155 device_lock(&dev->dev);
1156 dev->shutting_down = true;
1157 device_unlock(&dev->dev);
1158 del_timer_sync(&dev->check_pres_timer);
1159 cancel_work_sync(&dev->check_pres_work);
1160 }
Samuel Ortizd6469602011-12-14 16:43:12 +01001161
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001162 rc = nfc_genl_device_removed(dev);
1163 if (rc)
Eric Lapuyadef0c91032012-11-26 18:06:27 +01001164 pr_debug("The userspace won't be notified that the device %s "
1165 "was removed\n", dev_name(&dev->dev));
1166
1167 nfc_llcp_unregister_device(dev);
1168
1169 mutex_lock(&nfc_devlist_mutex);
1170 nfc_devlist_generation++;
1171 device_del(&dev->dev);
1172 mutex_unlock(&nfc_devlist_mutex);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001173}
1174EXPORT_SYMBOL(nfc_unregister_device);
1175
1176static int __init nfc_init(void)
1177{
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001178 int rc;
1179
Joe Perchesed1e0ad2011-11-29 11:37:32 -08001180 pr_info("NFC Core ver %s\n", VERSION);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001181
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001182 rc = class_register(&nfc_class);
1183 if (rc)
1184 return rc;
1185
1186 rc = nfc_genl_init();
1187 if (rc)
1188 goto err_genl;
1189
1190 /* the first generation must not be 0 */
1191 nfc_devlist_generation = 1;
1192
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -03001193 rc = rawsock_init();
1194 if (rc)
1195 goto err_rawsock;
1196
Samuel Ortizd6469602011-12-14 16:43:12 +01001197 rc = nfc_llcp_init();
1198 if (rc)
1199 goto err_llcp_sock;
1200
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -03001201 rc = af_nfc_init();
1202 if (rc)
1203 goto err_af_nfc;
1204
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001205 return 0;
1206
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -03001207err_af_nfc:
Samuel Ortizd6469602011-12-14 16:43:12 +01001208 nfc_llcp_exit();
1209err_llcp_sock:
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -03001210 rawsock_exit();
1211err_rawsock:
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -03001212 nfc_genl_exit();
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001213err_genl:
1214 class_unregister(&nfc_class);
1215 return rc;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001216}
1217
1218static void __exit nfc_exit(void)
1219{
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -03001220 af_nfc_exit();
Samuel Ortizd6469602011-12-14 16:43:12 +01001221 nfc_llcp_exit();
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -03001222 rawsock_exit();
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001223 nfc_genl_exit();
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001224 class_unregister(&nfc_class);
1225}
1226
1227subsys_initcall(nfc_init);
1228module_exit(nfc_exit);
1229
1230MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
1231MODULE_DESCRIPTION("NFC Core ver " VERSION);
1232MODULE_VERSION(VERSION);
1233MODULE_LICENSE("GPL");
Samuel Ortiz1155bb62012-06-12 00:35:50 +02001234MODULE_ALIAS_NETPROTO(PF_NFC);
Samuel Ortiz5df16ca2012-06-12 16:54:16 +02001235MODULE_ALIAS_GENL_FAMILY(NFC_GENL_NAME);