blob: 573c80c6ff7a161ceafdf70b8f14463b6fb5adff [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 Ortiz5df16cad2012-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
Andrew Lunn7cdda1c2020-10-28 01:56:53 +0100192 * @im_protocols: bitset of nfc initiator protocols to be used for polling
193 * @tm_protocols: bitset of nfc transport protocols to be used for polling
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300194 *
195 * The device remains polling for targets until a target is found or
196 * the nfc_stop_poll function is called.
197 */
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200198int nfc_start_poll(struct nfc_dev *dev, u32 im_protocols, u32 tm_protocols)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300199{
200 int rc;
201
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200202 pr_debug("dev_name %s initiator protocols 0x%x target protocols 0x%x\n",
203 dev_name(&dev->dev), im_protocols, tm_protocols);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300204
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200205 if (!im_protocols && !tm_protocols)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300206 return -EINVAL;
207
208 device_lock(&dev->dev);
209
210 if (!device_is_registered(&dev->dev)) {
211 rc = -ENODEV;
212 goto error;
213 }
214
Samuel Ortiz7757dc82013-04-10 12:25:30 +0200215 if (!dev->dev_up) {
216 rc = -ENODEV;
217 goto error;
218 }
219
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300220 if (dev->polling) {
221 rc = -EBUSY;
222 goto error;
223 }
224
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200225 rc = dev->ops->start_poll(dev, im_protocols, tm_protocols);
Samuel Ortizf212ad52012-05-31 00:02:26 +0200226 if (!rc) {
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300227 dev->polling = true;
Samuel Ortizf212ad52012-05-31 00:02:26 +0200228 dev->rf_mode = NFC_RF_NONE;
229 }
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300230
231error:
232 device_unlock(&dev->dev);
233 return rc;
234}
235
236/**
237 * nfc_stop_poll - stop polling for nfc targets
238 *
239 * @dev: The nfc device that must stop polling
240 */
241int nfc_stop_poll(struct nfc_dev *dev)
242{
243 int rc = 0;
244
Joe Perches20c239c2011-11-29 11:37:33 -0800245 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300246
247 device_lock(&dev->dev);
248
249 if (!device_is_registered(&dev->dev)) {
250 rc = -ENODEV;
251 goto error;
252 }
253
254 if (!dev->polling) {
255 rc = -EINVAL;
256 goto error;
257 }
258
259 dev->ops->stop_poll(dev);
260 dev->polling = false;
Thierry Escande5bcf0992012-10-05 11:05:45 +0200261 dev->rf_mode = NFC_RF_NONE;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300262
263error:
264 device_unlock(&dev->dev);
265 return rc;
266}
267
Eric Lapuyade90099432012-05-07 12:31:13 +0200268static struct nfc_target *nfc_find_target(struct nfc_dev *dev, u32 target_idx)
269{
270 int i;
271
Szymon Janc0f450772012-10-17 15:23:39 +0200272 for (i = 0; i < dev->n_targets; i++) {
Eric Lapuyade90099432012-05-07 12:31:13 +0200273 if (dev->targets[i].idx == target_idx)
274 return &dev->targets[i];
275 }
276
277 return NULL;
278}
279
Samuel Ortiz47807d32012-03-05 01:03:50 +0100280int nfc_dep_link_up(struct nfc_dev *dev, int target_index, u8 comm_mode)
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100281{
282 int rc = 0;
Samuel Ortiz47807d32012-03-05 01:03:50 +0100283 u8 *gb;
284 size_t gb_len;
Eric Lapuyade90099432012-05-07 12:31:13 +0200285 struct nfc_target *target;
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100286
Samuel Ortiz47807d32012-03-05 01:03:50 +0100287 pr_debug("dev_name=%s comm %d\n", dev_name(&dev->dev), comm_mode);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100288
289 if (!dev->ops->dep_link_up)
290 return -EOPNOTSUPP;
291
292 device_lock(&dev->dev);
293
294 if (!device_is_registered(&dev->dev)) {
295 rc = -ENODEV;
296 goto error;
297 }
298
299 if (dev->dep_link_up == true) {
300 rc = -EALREADY;
301 goto error;
302 }
303
Samuel Ortiz47807d32012-03-05 01:03:50 +0100304 gb = nfc_llcp_general_bytes(dev, &gb_len);
305 if (gb_len > NFC_MAX_GT_LEN) {
306 rc = -EINVAL;
307 goto error;
308 }
309
Eric Lapuyade90099432012-05-07 12:31:13 +0200310 target = nfc_find_target(dev, target_index);
311 if (target == NULL) {
312 rc = -ENOTCONN;
313 goto error;
314 }
315
316 rc = dev->ops->dep_link_up(dev, target, comm_mode, gb, gb_len);
Samuel Ortizf212ad52012-05-31 00:02:26 +0200317 if (!rc) {
Eric Lapuyade90099432012-05-07 12:31:13 +0200318 dev->active_target = target;
Samuel Ortizf212ad52012-05-31 00:02:26 +0200319 dev->rf_mode = NFC_RF_INITIATOR;
320 }
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100321
322error:
323 device_unlock(&dev->dev);
324 return rc;
325}
326
327int nfc_dep_link_down(struct nfc_dev *dev)
328{
329 int rc = 0;
330
331 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
332
333 if (!dev->ops->dep_link_down)
334 return -EOPNOTSUPP;
335
336 device_lock(&dev->dev);
337
338 if (!device_is_registered(&dev->dev)) {
339 rc = -ENODEV;
340 goto error;
341 }
342
343 if (dev->dep_link_up == false) {
344 rc = -EALREADY;
345 goto error;
346 }
347
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100348 rc = dev->ops->dep_link_down(dev);
349 if (!rc) {
350 dev->dep_link_up = false;
Eric Lapuyade90099432012-05-07 12:31:13 +0200351 dev->active_target = NULL;
Thierry Escande5bcf0992012-10-05 11:05:45 +0200352 dev->rf_mode = NFC_RF_NONE;
Samuel Ortizd6469602011-12-14 16:43:12 +0100353 nfc_llcp_mac_is_down(dev);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100354 nfc_genl_dep_link_down_event(dev);
355 }
356
357error:
358 device_unlock(&dev->dev);
Thierry Escande5bcf0992012-10-05 11:05:45 +0200359
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100360 return rc;
361}
362
363int nfc_dep_link_is_up(struct nfc_dev *dev, u32 target_idx,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100364 u8 comm_mode, u8 rf_mode)
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100365{
366 dev->dep_link_up = true;
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100367
Arron Wangd31652a2013-11-14 17:03:41 +0800368 if (!dev->active_target && rf_mode == NFC_RF_INITIATOR) {
Samuel Ortize29a9e22013-08-21 14:46:20 +0200369 struct nfc_target *target;
370
371 target = nfc_find_target(dev, target_idx);
372 if (target == NULL)
373 return -ENOTCONN;
374
375 dev->active_target = target;
376 }
377
378 dev->polling = false;
379 dev->rf_mode = rf_mode;
380
Samuel Ortizd6469602011-12-14 16:43:12 +0100381 nfc_llcp_mac_is_up(dev, target_idx, comm_mode, rf_mode);
382
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100383 return nfc_genl_dep_link_up_event(dev, target_idx, comm_mode, rf_mode);
384}
385EXPORT_SYMBOL(nfc_dep_link_is_up);
386
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300387/**
388 * nfc_activate_target - prepare the target for data exchange
389 *
390 * @dev: The nfc device that found the target
391 * @target_idx: index of the target that must be activated
392 * @protocol: nfc protocol that will be used for data exchange
393 */
394int nfc_activate_target(struct nfc_dev *dev, u32 target_idx, u32 protocol)
395{
396 int rc;
Eric Lapuyade90099432012-05-07 12:31:13 +0200397 struct nfc_target *target;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300398
Joe Perches20c239c2011-11-29 11:37:33 -0800399 pr_debug("dev_name=%s target_idx=%u protocol=%u\n",
400 dev_name(&dev->dev), target_idx, protocol);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300401
402 device_lock(&dev->dev);
403
404 if (!device_is_registered(&dev->dev)) {
405 rc = -ENODEV;
406 goto error;
407 }
408
Eric Lapuyade90099432012-05-07 12:31:13 +0200409 if (dev->active_target) {
410 rc = -EBUSY;
411 goto error;
412 }
413
414 target = nfc_find_target(dev, target_idx);
415 if (target == NULL) {
416 rc = -ENOTCONN;
417 goto error;
418 }
419
420 rc = dev->ops->activate_target(dev, target, protocol);
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200421 if (!rc) {
Eric Lapuyade90099432012-05-07 12:31:13 +0200422 dev->active_target = target;
Samuel Ortizf212ad52012-05-31 00:02:26 +0200423 dev->rf_mode = NFC_RF_INITIATOR;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300424
Eric Lapuyadef0c91032012-11-26 18:06:27 +0100425 if (dev->ops->check_presence && !dev->shutting_down)
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200426 mod_timer(&dev->check_pres_timer, jiffies +
427 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
428 }
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300429
430error:
431 device_unlock(&dev->dev);
432 return rc;
433}
434
435/**
436 * nfc_deactivate_target - deactivate a nfc target
437 *
438 * @dev: The nfc device that found the target
439 * @target_idx: index of the target that must be deactivated
Andrew Lunn7cdda1c2020-10-28 01:56:53 +0100440 * @mode: idle or sleep?
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300441 */
Christophe Ricard96d45812015-10-25 22:54:43 +0100442int nfc_deactivate_target(struct nfc_dev *dev, u32 target_idx, u8 mode)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300443{
444 int rc = 0;
445
Joe Perches20c239c2011-11-29 11:37:33 -0800446 pr_debug("dev_name=%s target_idx=%u\n",
447 dev_name(&dev->dev), target_idx);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300448
449 device_lock(&dev->dev);
450
451 if (!device_is_registered(&dev->dev)) {
452 rc = -ENODEV;
453 goto error;
454 }
455
Eric Lapuyade90099432012-05-07 12:31:13 +0200456 if (dev->active_target == NULL) {
457 rc = -ENOTCONN;
458 goto error;
459 }
460
461 if (dev->active_target->idx != target_idx) {
462 rc = -ENOTCONN;
463 goto error;
464 }
465
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200466 if (dev->ops->check_presence)
467 del_timer_sync(&dev->check_pres_timer);
468
Christophe Ricard96d45812015-10-25 22:54:43 +0100469 dev->ops->deactivate_target(dev, dev->active_target, mode);
Eric Lapuyade90099432012-05-07 12:31:13 +0200470 dev->active_target = NULL;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300471
472error:
473 device_unlock(&dev->dev);
474 return rc;
475}
476
477/**
478 * nfc_data_exchange - transceive data
479 *
480 * @dev: The nfc device that found the target
481 * @target_idx: index of the target
482 * @skb: data to be sent
483 * @cb: callback called when the response is received
484 * @cb_context: parameter for the callback function
485 *
486 * The user must wait for the callback before calling this function again.
487 */
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100488int nfc_data_exchange(struct nfc_dev *dev, u32 target_idx, struct sk_buff *skb,
489 data_exchange_cb_t cb, void *cb_context)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300490{
491 int rc;
492
Joe Perches20c239c2011-11-29 11:37:33 -0800493 pr_debug("dev_name=%s target_idx=%u skb->len=%u\n",
494 dev_name(&dev->dev), target_idx, skb->len);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300495
496 device_lock(&dev->dev);
497
498 if (!device_is_registered(&dev->dev)) {
499 rc = -ENODEV;
500 kfree_skb(skb);
501 goto error;
502 }
503
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +0200504 if (dev->rf_mode == NFC_RF_INITIATOR && dev->active_target != NULL) {
505 if (dev->active_target->idx != target_idx) {
506 rc = -EADDRNOTAVAIL;
507 kfree_skb(skb);
508 goto error;
509 }
510
511 if (dev->ops->check_presence)
512 del_timer_sync(&dev->check_pres_timer);
513
514 rc = dev->ops->im_transceive(dev, dev->active_target, skb, cb,
515 cb_context);
516
Eric Lapuyadef0c91032012-11-26 18:06:27 +0100517 if (!rc && dev->ops->check_presence && !dev->shutting_down)
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +0200518 mod_timer(&dev->check_pres_timer, jiffies +
519 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
520 } else if (dev->rf_mode == NFC_RF_TARGET && dev->ops->tm_send != NULL) {
521 rc = dev->ops->tm_send(dev, skb);
522 } else {
Eric Lapuyade144612c2012-04-10 19:43:11 +0200523 rc = -ENOTCONN;
524 kfree_skb(skb);
525 goto error;
526 }
527
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200528
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300529error:
530 device_unlock(&dev->dev);
531 return rc;
532}
533
Arron Wangd8eb18e2013-08-23 16:02:08 +0800534struct nfc_se *nfc_find_se(struct nfc_dev *dev, u32 se_idx)
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200535{
Axel Lin156cef82014-02-14 19:29:19 +0800536 struct nfc_se *se;
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200537
Axel Lin156cef82014-02-14 19:29:19 +0800538 list_for_each_entry(se, &dev->secure_elements, list)
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200539 if (se->idx == se_idx)
540 return se;
541
542 return NULL;
543}
Arron Wangd8eb18e2013-08-23 16:02:08 +0800544EXPORT_SYMBOL(nfc_find_se);
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200545
546int nfc_enable_se(struct nfc_dev *dev, u32 se_idx)
547{
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200548 struct nfc_se *se;
549 int rc;
550
551 pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
552
553 device_lock(&dev->dev);
554
555 if (!device_is_registered(&dev->dev)) {
556 rc = -ENODEV;
557 goto error;
558 }
559
560 if (!dev->dev_up) {
561 rc = -ENODEV;
562 goto error;
563 }
564
565 if (dev->polling) {
566 rc = -EBUSY;
567 goto error;
568 }
569
570 if (!dev->ops->enable_se || !dev->ops->disable_se) {
571 rc = -EOPNOTSUPP;
572 goto error;
573 }
574
Arron Wangd8eb18e2013-08-23 16:02:08 +0800575 se = nfc_find_se(dev, se_idx);
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200576 if (!se) {
577 rc = -EINVAL;
578 goto error;
579 }
580
Arron Wang2c383282013-07-30 14:35:35 +0200581 if (se->state == NFC_SE_ENABLED) {
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200582 rc = -EALREADY;
583 goto error;
584 }
585
586 rc = dev->ops->enable_se(dev, se_idx);
Arron Wang39525ee12013-07-30 14:40:05 +0200587 if (rc >= 0)
588 se->state = NFC_SE_ENABLED;
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200589
590error:
591 device_unlock(&dev->dev);
592 return rc;
593}
594
595int nfc_disable_se(struct nfc_dev *dev, u32 se_idx)
596{
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200597 struct nfc_se *se;
598 int rc;
599
600 pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
601
602 device_lock(&dev->dev);
603
604 if (!device_is_registered(&dev->dev)) {
605 rc = -ENODEV;
606 goto error;
607 }
608
609 if (!dev->dev_up) {
610 rc = -ENODEV;
611 goto error;
612 }
613
614 if (!dev->ops->enable_se || !dev->ops->disable_se) {
615 rc = -EOPNOTSUPP;
616 goto error;
617 }
618
Arron Wangd8eb18e2013-08-23 16:02:08 +0800619 se = nfc_find_se(dev, se_idx);
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200620 if (!se) {
621 rc = -EINVAL;
622 goto error;
623 }
624
Arron Wang2c383282013-07-30 14:35:35 +0200625 if (se->state == NFC_SE_DISABLED) {
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200626 rc = -EALREADY;
627 goto error;
628 }
629
630 rc = dev->ops->disable_se(dev, se_idx);
Arron Wang39525ee12013-07-30 14:40:05 +0200631 if (rc >= 0)
632 se->state = NFC_SE_DISABLED;
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200633
634error:
635 device_unlock(&dev->dev);
636 return rc;
637}
638
Samuel Ortiz541d9202011-12-14 16:43:10 +0100639int nfc_set_remote_general_bytes(struct nfc_dev *dev, u8 *gb, u8 gb_len)
640{
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100641 pr_debug("dev_name=%s gb_len=%d\n", dev_name(&dev->dev), gb_len);
Samuel Ortiz541d9202011-12-14 16:43:10 +0100642
Samuel Ortizd6469602011-12-14 16:43:12 +0100643 return nfc_llcp_set_remote_gb(dev, gb, gb_len);
Samuel Ortiz541d9202011-12-14 16:43:10 +0100644}
645EXPORT_SYMBOL(nfc_set_remote_general_bytes);
646
Samuel Ortizab73b752012-04-10 12:51:52 +0200647u8 *nfc_get_local_general_bytes(struct nfc_dev *dev, size_t *gb_len)
648{
649 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
650
651 return nfc_llcp_general_bytes(dev, gb_len);
652}
653EXPORT_SYMBOL(nfc_get_local_general_bytes);
654
Samuel Ortiz73167ce2012-05-31 00:05:50 +0200655int nfc_tm_data_received(struct nfc_dev *dev, struct sk_buff *skb)
656{
657 /* Only LLCP target mode for now */
658 if (dev->dep_link_up == false) {
659 kfree_skb(skb);
660 return -ENOLINK;
661 }
662
663 return nfc_llcp_data_received(dev, skb);
664}
665EXPORT_SYMBOL(nfc_tm_data_received);
666
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200667int nfc_tm_activated(struct nfc_dev *dev, u32 protocol, u8 comm_mode,
668 u8 *gb, size_t gb_len)
669{
670 int rc;
671
672 device_lock(&dev->dev);
673
674 dev->polling = false;
675
676 if (gb != NULL) {
677 rc = nfc_set_remote_general_bytes(dev, gb, gb_len);
678 if (rc < 0)
679 goto out;
680 }
681
Samuel Ortizf212ad52012-05-31 00:02:26 +0200682 dev->rf_mode = NFC_RF_TARGET;
683
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200684 if (protocol == NFC_PROTO_NFC_DEP_MASK)
685 nfc_dep_link_is_up(dev, 0, comm_mode, NFC_RF_TARGET);
686
687 rc = nfc_genl_tm_activated(dev, protocol);
688
689out:
690 device_unlock(&dev->dev);
691
692 return rc;
693}
694EXPORT_SYMBOL(nfc_tm_activated);
695
696int nfc_tm_deactivated(struct nfc_dev *dev)
697{
698 dev->dep_link_up = false;
Thierry Escande5bcf0992012-10-05 11:05:45 +0200699 dev->rf_mode = NFC_RF_NONE;
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200700
701 return nfc_genl_tm_deactivated(dev);
702}
703EXPORT_SYMBOL(nfc_tm_deactivated);
704
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300705/**
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +0100706 * nfc_alloc_send_skb - allocate a skb for data exchange responses
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300707 *
Andrew Lunn7cdda1c2020-10-28 01:56:53 +0100708 * @dev: device sending the response
709 * @sk: socket sending the response
710 * @flags: MSG_DONTWAIT flag
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300711 * @size: size to allocate
Andrew Lunn7cdda1c2020-10-28 01:56:53 +0100712 * @err: pointer to memory to store the error code
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300713 */
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +0100714struct sk_buff *nfc_alloc_send_skb(struct nfc_dev *dev, struct sock *sk,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100715 unsigned int flags, unsigned int size,
716 unsigned int *err)
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +0100717{
718 struct sk_buff *skb;
719 unsigned int total_size;
720
721 total_size = size +
722 dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE;
723
724 skb = sock_alloc_send_skb(sk, total_size, flags & MSG_DONTWAIT, err);
725 if (skb)
726 skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE);
727
728 return skb;
729}
730
731/**
732 * nfc_alloc_recv_skb - allocate a skb for data exchange responses
733 *
734 * @size: size to allocate
735 * @gfp: gfp flags
736 */
737struct sk_buff *nfc_alloc_recv_skb(unsigned int size, gfp_t gfp)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300738{
739 struct sk_buff *skb;
740 unsigned int total_size;
741
742 total_size = size + 1;
743 skb = alloc_skb(total_size, gfp);
744
745 if (skb)
746 skb_reserve(skb, 1);
747
748 return skb;
749}
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +0100750EXPORT_SYMBOL(nfc_alloc_recv_skb);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300751
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300752/**
753 * nfc_targets_found - inform that targets were found
754 *
755 * @dev: The nfc device that found the targets
756 * @targets: array of nfc targets found
Andrew Lunnffbab1c2020-07-13 01:15:08 +0200757 * @n_targets: targets array size
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300758 *
759 * The device driver must call this function when one or many nfc targets
760 * are found. After calling this function, the device driver must stop
761 * polling for targets.
Eric Lapuyaded94f9c52012-05-03 16:33:32 +0200762 * NOTE: This function can be called with targets=NULL and n_targets=0 to
763 * notify a driver error, meaning that the polling operation cannot complete.
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200764 * IMPORTANT: this function must not be called from an atomic context.
765 * In addition, it must also not be called from a context that would prevent
766 * the NFC Core to call other nfc ops entry point concurrently.
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300767 */
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100768int nfc_targets_found(struct nfc_dev *dev,
769 struct nfc_target *targets, int n_targets)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300770{
Samuel Ortizc4fbb652012-04-10 19:43:09 +0200771 int i;
772
Joe Perches20c239c2011-11-29 11:37:33 -0800773 pr_debug("dev_name=%s n_targets=%d\n", dev_name(&dev->dev), n_targets);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300774
Samuel Ortizc4fbb652012-04-10 19:43:09 +0200775 for (i = 0; i < n_targets; i++)
Eric Lapuyade01ae0ee2012-04-10 19:43:10 +0200776 targets[i].idx = dev->target_next_idx++;
Samuel Ortizc4fbb652012-04-10 19:43:09 +0200777
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200778 device_lock(&dev->dev);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300779
Eric Lapuyade8668fdd2012-05-03 16:21:58 +0200780 if (dev->polling == false) {
781 device_unlock(&dev->dev);
782 return 0;
783 }
784
785 dev->polling = false;
786
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300787 dev->targets_generation++;
788
789 kfree(dev->targets);
Eric Lapuyaded94f9c52012-05-03 16:33:32 +0200790 dev->targets = NULL;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300791
Eric Lapuyaded94f9c52012-05-03 16:33:32 +0200792 if (targets) {
793 dev->targets = kmemdup(targets,
794 n_targets * sizeof(struct nfc_target),
795 GFP_ATOMIC);
796
797 if (!dev->targets) {
798 dev->n_targets = 0;
799 device_unlock(&dev->dev);
800 return -ENOMEM;
801 }
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300802 }
803
804 dev->n_targets = n_targets;
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200805 device_unlock(&dev->dev);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300806
807 nfc_genl_targets_found(dev);
808
809 return 0;
810}
811EXPORT_SYMBOL(nfc_targets_found);
812
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200813/**
814 * nfc_target_lost - inform that an activated target went out of field
815 *
816 * @dev: The nfc device that had the activated target in field
817 * @target_idx: the nfc index of the target
818 *
819 * The device driver must call this function when the activated target
820 * goes out of the field.
821 * IMPORTANT: this function must not be called from an atomic context.
822 * In addition, it must also not be called from a context that would prevent
823 * the NFC Core to call other nfc ops entry point concurrently.
824 */
Eric Lapuyadee1da0ef2012-04-10 19:43:05 +0200825int nfc_target_lost(struct nfc_dev *dev, u32 target_idx)
826{
827 struct nfc_target *tg;
828 int i;
829
830 pr_debug("dev_name %s n_target %d\n", dev_name(&dev->dev), target_idx);
831
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200832 device_lock(&dev->dev);
Eric Lapuyadee1da0ef2012-04-10 19:43:05 +0200833
834 for (i = 0; i < dev->n_targets; i++) {
835 tg = &dev->targets[i];
836 if (tg->idx == target_idx)
837 break;
838 }
839
840 if (i == dev->n_targets) {
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200841 device_unlock(&dev->dev);
Eric Lapuyadee1da0ef2012-04-10 19:43:05 +0200842 return -EINVAL;
843 }
844
845 dev->targets_generation++;
846 dev->n_targets--;
Eric Lapuyade90099432012-05-07 12:31:13 +0200847 dev->active_target = NULL;
Eric Lapuyadee1da0ef2012-04-10 19:43:05 +0200848
849 if (dev->n_targets) {
850 memcpy(&dev->targets[i], &dev->targets[i + 1],
851 (dev->n_targets - i) * sizeof(struct nfc_target));
852 } else {
853 kfree(dev->targets);
854 dev->targets = NULL;
855 }
856
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200857 device_unlock(&dev->dev);
Eric Lapuyadee1da0ef2012-04-10 19:43:05 +0200858
859 nfc_genl_target_lost(dev, target_idx);
860
861 return 0;
862}
863EXPORT_SYMBOL(nfc_target_lost);
864
Eric Lapuyade9eb334a2012-06-11 15:52:38 +0200865inline void nfc_driver_failure(struct nfc_dev *dev, int err)
Eric Lapuyade456411c2012-06-11 13:49:51 +0200866{
Eric Lapuyade9eb334a2012-06-11 15:52:38 +0200867 nfc_targets_found(dev, NULL, 0);
Eric Lapuyade456411c2012-06-11 13:49:51 +0200868}
869EXPORT_SYMBOL(nfc_driver_failure);
870
Samuel Ortizfed7c252013-05-10 15:28:38 +0200871int nfc_add_se(struct nfc_dev *dev, u32 se_idx, u16 type)
872{
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200873 struct nfc_se *se;
Samuel Ortiz2757c3722013-05-10 15:47:37 +0200874 int rc;
Samuel Ortizfed7c252013-05-10 15:28:38 +0200875
876 pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
877
Arron Wangd8eb18e2013-08-23 16:02:08 +0800878 se = nfc_find_se(dev, se_idx);
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200879 if (se)
880 return -EALREADY;
Samuel Ortizfed7c252013-05-10 15:28:38 +0200881
882 se = kzalloc(sizeof(struct nfc_se), GFP_KERNEL);
883 if (!se)
884 return -ENOMEM;
885
886 se->idx = se_idx;
887 se->type = type;
888 se->state = NFC_SE_DISABLED;
889 INIT_LIST_HEAD(&se->list);
890
891 list_add(&se->list, &dev->secure_elements);
892
Samuel Ortiz2757c3722013-05-10 15:47:37 +0200893 rc = nfc_genl_se_added(dev, se_idx, type);
894 if (rc < 0) {
895 list_del(&se->list);
896 kfree(se);
897
898 return rc;
899 }
900
Samuel Ortizfed7c252013-05-10 15:28:38 +0200901 return 0;
902}
903EXPORT_SYMBOL(nfc_add_se);
904
905int nfc_remove_se(struct nfc_dev *dev, u32 se_idx)
906{
907 struct nfc_se *se, *n;
Samuel Ortiz2757c3722013-05-10 15:47:37 +0200908 int rc;
Samuel Ortizfed7c252013-05-10 15:28:38 +0200909
910 pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
911
912 list_for_each_entry_safe(se, n, &dev->secure_elements, list)
913 if (se->idx == se_idx) {
Samuel Ortiz2757c3722013-05-10 15:47:37 +0200914 rc = nfc_genl_se_removed(dev, se_idx);
915 if (rc < 0)
916 return rc;
917
Samuel Ortizfed7c252013-05-10 15:28:38 +0200918 list_del(&se->list);
919 kfree(se);
920
921 return 0;
922 }
923
924 return -EINVAL;
925}
926EXPORT_SYMBOL(nfc_remove_se);
927
Christophe Ricard447b27c2015-02-01 22:26:16 +0100928int nfc_se_transaction(struct nfc_dev *dev, u8 se_idx,
929 struct nfc_evt_transaction *evt_transaction)
930{
931 int rc;
932
933 pr_debug("transaction: %x\n", se_idx);
934
935 device_lock(&dev->dev);
936
937 if (!evt_transaction) {
938 rc = -EPROTO;
939 goto out;
940 }
941
942 rc = nfc_genl_se_transaction(dev, se_idx, evt_transaction);
943out:
944 device_unlock(&dev->dev);
945 return rc;
946}
947EXPORT_SYMBOL(nfc_se_transaction);
948
Christophe Ricard9afec6d2015-12-23 23:45:18 +0100949int nfc_se_connectivity(struct nfc_dev *dev, u8 se_idx)
950{
951 int rc;
952
953 pr_debug("connectivity: %x\n", se_idx);
954
955 device_lock(&dev->dev);
956 rc = nfc_genl_se_connectivity(dev, se_idx);
957 device_unlock(&dev->dev);
958 return rc;
959}
960EXPORT_SYMBOL(nfc_se_connectivity);
961
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300962static void nfc_release(struct device *d)
963{
964 struct nfc_dev *dev = to_nfc_dev(d);
Samuel Ortizee656e92013-05-10 15:53:29 +0200965 struct nfc_se *se, *n;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300966
Joe Perches20c239c2011-11-29 11:37:33 -0800967 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300968
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300969 nfc_genl_data_exit(&dev->genl_data);
970 kfree(dev->targets);
Samuel Ortizee656e92013-05-10 15:53:29 +0200971
972 list_for_each_entry_safe(se, n, &dev->secure_elements, list) {
973 nfc_genl_se_removed(dev, se->idx);
974 list_del(&se->list);
975 kfree(se);
976 }
977
Johan Hovold20777bc2017-03-30 12:15:35 +0200978 ida_simple_remove(&nfc_index_ida, dev->idx);
979
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300980 kfree(dev);
981}
982
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200983static void nfc_check_pres_work(struct work_struct *work)
984{
985 struct nfc_dev *dev = container_of(work, struct nfc_dev,
986 check_pres_work);
987 int rc;
988
989 device_lock(&dev->dev);
990
Eric Lapuyade90099432012-05-07 12:31:13 +0200991 if (dev->active_target && timer_pending(&dev->check_pres_timer) == 0) {
992 rc = dev->ops->check_presence(dev, dev->active_target);
Eric Lapuyade632c0162012-10-02 17:27:36 +0200993 if (rc == -EOPNOTSUPP)
994 goto exit;
Eric Lapuyadef0c91032012-11-26 18:06:27 +0100995 if (rc) {
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200996 u32 active_target_idx = dev->active_target->idx;
997 device_unlock(&dev->dev);
998 nfc_target_lost(dev, active_target_idx);
999 return;
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +02001000 }
Eric Lapuyadef0c91032012-11-26 18:06:27 +01001001
1002 if (!dev->shutting_down)
1003 mod_timer(&dev->check_pres_timer, jiffies +
1004 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +02001005 }
1006
Eric Lapuyade632c0162012-10-02 17:27:36 +02001007exit:
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +02001008 device_unlock(&dev->dev);
1009}
1010
Allen Pais4b519bb2017-10-11 16:03:44 +05301011static void nfc_check_pres_timeout(struct timer_list *t)
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +02001012{
Allen Pais4b519bb2017-10-11 16:03:44 +05301013 struct nfc_dev *dev = from_timer(dev, t, check_pres_timer);
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +02001014
Linus Torvalds916082b2012-10-02 16:01:31 -07001015 schedule_work(&dev->check_pres_work);
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +02001016}
1017
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001018struct class nfc_class = {
1019 .name = "nfc",
1020 .dev_release = nfc_release,
1021};
1022EXPORT_SYMBOL(nfc_class);
1023
Michał Mirosław9f3b7952013-02-01 20:40:17 +01001024static int match_idx(struct device *d, const void *data)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001025{
1026 struct nfc_dev *dev = to_nfc_dev(d);
Michał Mirosław9f3b7952013-02-01 20:40:17 +01001027 const unsigned int *idx = data;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001028
1029 return dev->idx == *idx;
1030}
1031
Eric Dumazet95c96172012-04-15 05:58:06 +00001032struct nfc_dev *nfc_get_device(unsigned int idx)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001033{
1034 struct device *d;
1035
1036 d = class_find_device(&nfc_class, NULL, &idx, match_idx);
1037 if (!d)
1038 return NULL;
1039
1040 return to_nfc_dev(d);
1041}
1042
1043/**
1044 * nfc_allocate_device - allocate a new nfc device
1045 *
1046 * @ops: device operations
1047 * @supported_protocols: NFC protocols supported by the device
Andrew Lunn7cdda1c2020-10-28 01:56:53 +01001048 * @tx_headroom: reserved space at beginning of skb
1049 * @tx_tailroom: reserved space at end of skb
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001050 */
1051struct nfc_dev *nfc_allocate_device(struct nfc_ops *ops,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +01001052 u32 supported_protocols,
1053 int tx_headroom, int tx_tailroom)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001054{
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001055 struct nfc_dev *dev;
Johan Hovold20777bc2017-03-30 12:15:35 +02001056 int rc;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001057
1058 if (!ops->start_poll || !ops->stop_poll || !ops->activate_target ||
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02001059 !ops->deactivate_target || !ops->im_transceive)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001060 return NULL;
1061
1062 if (!supported_protocols)
1063 return NULL;
1064
1065 dev = kzalloc(sizeof(struct nfc_dev), GFP_KERNEL);
1066 if (!dev)
1067 return NULL;
1068
Johan Hovold20777bc2017-03-30 12:15:35 +02001069 rc = ida_simple_get(&nfc_index_ida, 0, 0, GFP_KERNEL);
1070 if (rc < 0)
1071 goto err_free_dev;
1072 dev->idx = rc;
1073
1074 dev->dev.class = &nfc_class;
1075 dev_set_name(&dev->dev, "nfc%d", dev->idx);
1076 device_initialize(&dev->dev);
1077
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001078 dev->ops = ops;
1079 dev->supported_protocols = supported_protocols;
Samuel Ortize8753042011-08-19 15:47:11 +02001080 dev->tx_headroom = tx_headroom;
1081 dev->tx_tailroom = tx_tailroom;
Samuel Ortizfed7c252013-05-10 15:28:38 +02001082 INIT_LIST_HEAD(&dev->secure_elements);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001083
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001084 nfc_genl_data_init(&dev->genl_data);
1085
Thierry Escande5bcf0992012-10-05 11:05:45 +02001086 dev->rf_mode = NFC_RF_NONE;
Eric Lapuyaded4ccb132012-05-07 12:31:15 +02001087
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001088 /* first generation must not be 0 */
1089 dev->targets_generation = 1;
1090
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +02001091 if (ops->check_presence) {
Allen Pais4b519bb2017-10-11 16:03:44 +05301092 timer_setup(&dev->check_pres_timer, nfc_check_pres_timeout, 0);
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +02001093 INIT_WORK(&dev->check_pres_work, nfc_check_pres_work);
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +02001094 }
1095
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001096 return dev;
Johan Hovold20777bc2017-03-30 12:15:35 +02001097
1098err_free_dev:
1099 kfree(dev);
1100
Johan Hovoldc45e3e42017-07-09 13:08:58 +02001101 return NULL;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001102}
1103EXPORT_SYMBOL(nfc_allocate_device);
1104
1105/**
1106 * nfc_register_device - register a nfc device in the nfc subsystem
1107 *
1108 * @dev: The nfc device to register
1109 */
1110int nfc_register_device(struct nfc_dev *dev)
1111{
1112 int rc;
1113
Joe Perches20c239c2011-11-29 11:37:33 -08001114 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001115
1116 mutex_lock(&nfc_devlist_mutex);
1117 nfc_devlist_generation++;
1118 rc = device_add(&dev->dev);
1119 mutex_unlock(&nfc_devlist_mutex);
1120
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001121 if (rc < 0)
1122 return rc;
1123
Samuel Ortizd6469602011-12-14 16:43:12 +01001124 rc = nfc_llcp_register_device(dev);
1125 if (rc)
1126 pr_err("Could not register llcp device\n");
1127
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001128 rc = nfc_genl_device_added(dev);
1129 if (rc)
Joe Perches20c239c2011-11-29 11:37:33 -08001130 pr_debug("The userspace won't be notified that the device %s was added\n",
1131 dev_name(&dev->dev));
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001132
Samuel Ortizbe055b22013-04-11 11:52:20 +02001133 dev->rfkill = rfkill_alloc(dev_name(&dev->dev), &dev->dev,
1134 RFKILL_TYPE_NFC, &nfc_rfkill_ops, dev);
1135 if (dev->rfkill) {
1136 if (rfkill_register(dev->rfkill) < 0) {
1137 rfkill_destroy(dev->rfkill);
1138 dev->rfkill = NULL;
1139 }
1140 }
1141
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001142 return 0;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001143}
1144EXPORT_SYMBOL(nfc_register_device);
1145
1146/**
1147 * nfc_unregister_device - unregister a nfc device in the nfc subsystem
1148 *
1149 * @dev: The nfc device to unregister
1150 */
1151void nfc_unregister_device(struct nfc_dev *dev)
1152{
Johan Hovold20777bc2017-03-30 12:15:35 +02001153 int rc;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001154
Joe Perches20c239c2011-11-29 11:37:33 -08001155 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001156
Samuel Ortizbe055b22013-04-11 11:52:20 +02001157 if (dev->rfkill) {
1158 rfkill_unregister(dev->rfkill);
1159 rfkill_destroy(dev->rfkill);
1160 }
1161
Eric Lapuyadef0c91032012-11-26 18:06:27 +01001162 if (dev->ops->check_presence) {
1163 device_lock(&dev->dev);
1164 dev->shutting_down = true;
1165 device_unlock(&dev->dev);
1166 del_timer_sync(&dev->check_pres_timer);
1167 cancel_work_sync(&dev->check_pres_work);
1168 }
Samuel Ortizd6469602011-12-14 16:43:12 +01001169
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001170 rc = nfc_genl_device_removed(dev);
1171 if (rc)
Eric Lapuyadef0c91032012-11-26 18:06:27 +01001172 pr_debug("The userspace won't be notified that the device %s "
1173 "was removed\n", dev_name(&dev->dev));
1174
1175 nfc_llcp_unregister_device(dev);
1176
1177 mutex_lock(&nfc_devlist_mutex);
1178 nfc_devlist_generation++;
1179 device_del(&dev->dev);
1180 mutex_unlock(&nfc_devlist_mutex);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001181}
1182EXPORT_SYMBOL(nfc_unregister_device);
1183
1184static int __init nfc_init(void)
1185{
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001186 int rc;
1187
Joe Perchesed1e0ad2011-11-29 11:37:32 -08001188 pr_info("NFC Core ver %s\n", VERSION);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001189
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001190 rc = class_register(&nfc_class);
1191 if (rc)
1192 return rc;
1193
1194 rc = nfc_genl_init();
1195 if (rc)
1196 goto err_genl;
1197
1198 /* the first generation must not be 0 */
1199 nfc_devlist_generation = 1;
1200
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -03001201 rc = rawsock_init();
1202 if (rc)
1203 goto err_rawsock;
1204
Samuel Ortizd6469602011-12-14 16:43:12 +01001205 rc = nfc_llcp_init();
1206 if (rc)
1207 goto err_llcp_sock;
1208
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -03001209 rc = af_nfc_init();
1210 if (rc)
1211 goto err_af_nfc;
1212
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001213 return 0;
1214
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -03001215err_af_nfc:
Samuel Ortizd6469602011-12-14 16:43:12 +01001216 nfc_llcp_exit();
1217err_llcp_sock:
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -03001218 rawsock_exit();
1219err_rawsock:
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -03001220 nfc_genl_exit();
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001221err_genl:
1222 class_unregister(&nfc_class);
1223 return rc;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001224}
1225
1226static void __exit nfc_exit(void)
1227{
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -03001228 af_nfc_exit();
Samuel Ortizd6469602011-12-14 16:43:12 +01001229 nfc_llcp_exit();
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -03001230 rawsock_exit();
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001231 nfc_genl_exit();
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001232 class_unregister(&nfc_class);
1233}
1234
1235subsys_initcall(nfc_init);
1236module_exit(nfc_exit);
1237
1238MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
1239MODULE_DESCRIPTION("NFC Core ver " VERSION);
1240MODULE_VERSION(VERSION);
1241MODULE_LICENSE("GPL");
Samuel Ortiz1155bb62012-06-12 00:35:50 +02001242MODULE_ALIAS_NETPROTO(PF_NFC);
Samuel Ortiz5df16cad2012-06-12 16:54:16 +02001243MODULE_ALIAS_GENL_FAMILY(NFC_GENL_NAME);