blob: ca1e65f4b1331bfd164245996ed462ab61f46a4e [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
283 if (dev->n_targets == 0)
284 return NULL;
285
Szymon Janc0f450772012-10-17 15:23:39 +0200286 for (i = 0; i < dev->n_targets; i++) {
Eric Lapuyade90099432012-05-07 12:31:13 +0200287 if (dev->targets[i].idx == target_idx)
288 return &dev->targets[i];
289 }
290
291 return NULL;
292}
293
Samuel Ortiz47807d32012-03-05 01:03:50 +0100294int nfc_dep_link_up(struct nfc_dev *dev, int target_index, u8 comm_mode)
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100295{
296 int rc = 0;
Samuel Ortiz47807d32012-03-05 01:03:50 +0100297 u8 *gb;
298 size_t gb_len;
Eric Lapuyade90099432012-05-07 12:31:13 +0200299 struct nfc_target *target;
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100300
Samuel Ortiz47807d32012-03-05 01:03:50 +0100301 pr_debug("dev_name=%s comm %d\n", dev_name(&dev->dev), comm_mode);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100302
303 if (!dev->ops->dep_link_up)
304 return -EOPNOTSUPP;
305
306 device_lock(&dev->dev);
307
308 if (!device_is_registered(&dev->dev)) {
309 rc = -ENODEV;
310 goto error;
311 }
312
313 if (dev->dep_link_up == true) {
314 rc = -EALREADY;
315 goto error;
316 }
317
Samuel Ortiz47807d32012-03-05 01:03:50 +0100318 gb = nfc_llcp_general_bytes(dev, &gb_len);
319 if (gb_len > NFC_MAX_GT_LEN) {
320 rc = -EINVAL;
321 goto error;
322 }
323
Eric Lapuyade90099432012-05-07 12:31:13 +0200324 target = nfc_find_target(dev, target_index);
325 if (target == NULL) {
326 rc = -ENOTCONN;
327 goto error;
328 }
329
330 rc = dev->ops->dep_link_up(dev, target, comm_mode, gb, gb_len);
Samuel Ortizf212ad52012-05-31 00:02:26 +0200331 if (!rc) {
Eric Lapuyade90099432012-05-07 12:31:13 +0200332 dev->active_target = target;
Samuel Ortizf212ad52012-05-31 00:02:26 +0200333 dev->rf_mode = NFC_RF_INITIATOR;
334 }
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100335
336error:
337 device_unlock(&dev->dev);
338 return rc;
339}
340
341int nfc_dep_link_down(struct nfc_dev *dev)
342{
343 int rc = 0;
344
345 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
346
347 if (!dev->ops->dep_link_down)
348 return -EOPNOTSUPP;
349
350 device_lock(&dev->dev);
351
352 if (!device_is_registered(&dev->dev)) {
353 rc = -ENODEV;
354 goto error;
355 }
356
357 if (dev->dep_link_up == false) {
358 rc = -EALREADY;
359 goto error;
360 }
361
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100362 rc = dev->ops->dep_link_down(dev);
363 if (!rc) {
364 dev->dep_link_up = false;
Eric Lapuyade90099432012-05-07 12:31:13 +0200365 dev->active_target = NULL;
Thierry Escande5bcf0992012-10-05 11:05:45 +0200366 dev->rf_mode = NFC_RF_NONE;
Samuel Ortizd6469602011-12-14 16:43:12 +0100367 nfc_llcp_mac_is_down(dev);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100368 nfc_genl_dep_link_down_event(dev);
369 }
370
371error:
372 device_unlock(&dev->dev);
Thierry Escande5bcf0992012-10-05 11:05:45 +0200373
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100374 return rc;
375}
376
377int nfc_dep_link_is_up(struct nfc_dev *dev, u32 target_idx,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100378 u8 comm_mode, u8 rf_mode)
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100379{
380 dev->dep_link_up = true;
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100381
Arron Wangd31652a2013-11-14 17:03:41 +0800382 if (!dev->active_target && rf_mode == NFC_RF_INITIATOR) {
Samuel Ortize29a9e22013-08-21 14:46:20 +0200383 struct nfc_target *target;
384
385 target = nfc_find_target(dev, target_idx);
386 if (target == NULL)
387 return -ENOTCONN;
388
389 dev->active_target = target;
390 }
391
392 dev->polling = false;
393 dev->rf_mode = rf_mode;
394
Samuel Ortizd6469602011-12-14 16:43:12 +0100395 nfc_llcp_mac_is_up(dev, target_idx, comm_mode, rf_mode);
396
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100397 return nfc_genl_dep_link_up_event(dev, target_idx, comm_mode, rf_mode);
398}
399EXPORT_SYMBOL(nfc_dep_link_is_up);
400
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300401/**
402 * nfc_activate_target - prepare the target for data exchange
403 *
404 * @dev: The nfc device that found the target
405 * @target_idx: index of the target that must be activated
406 * @protocol: nfc protocol that will be used for data exchange
407 */
408int nfc_activate_target(struct nfc_dev *dev, u32 target_idx, u32 protocol)
409{
410 int rc;
Eric Lapuyade90099432012-05-07 12:31:13 +0200411 struct nfc_target *target;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300412
Joe Perches20c239c2011-11-29 11:37:33 -0800413 pr_debug("dev_name=%s target_idx=%u protocol=%u\n",
414 dev_name(&dev->dev), target_idx, protocol);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300415
416 device_lock(&dev->dev);
417
418 if (!device_is_registered(&dev->dev)) {
419 rc = -ENODEV;
420 goto error;
421 }
422
Eric Lapuyade90099432012-05-07 12:31:13 +0200423 if (dev->active_target) {
424 rc = -EBUSY;
425 goto error;
426 }
427
428 target = nfc_find_target(dev, target_idx);
429 if (target == NULL) {
430 rc = -ENOTCONN;
431 goto error;
432 }
433
434 rc = dev->ops->activate_target(dev, target, protocol);
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200435 if (!rc) {
Eric Lapuyade90099432012-05-07 12:31:13 +0200436 dev->active_target = target;
Samuel Ortizf212ad52012-05-31 00:02:26 +0200437 dev->rf_mode = NFC_RF_INITIATOR;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300438
Eric Lapuyadef0c91032012-11-26 18:06:27 +0100439 if (dev->ops->check_presence && !dev->shutting_down)
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200440 mod_timer(&dev->check_pres_timer, jiffies +
441 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
442 }
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300443
444error:
445 device_unlock(&dev->dev);
446 return rc;
447}
448
449/**
450 * nfc_deactivate_target - deactivate a nfc target
451 *
452 * @dev: The nfc device that found the target
453 * @target_idx: index of the target that must be deactivated
454 */
455int nfc_deactivate_target(struct nfc_dev *dev, u32 target_idx)
456{
457 int rc = 0;
458
Joe Perches20c239c2011-11-29 11:37:33 -0800459 pr_debug("dev_name=%s target_idx=%u\n",
460 dev_name(&dev->dev), target_idx);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300461
462 device_lock(&dev->dev);
463
464 if (!device_is_registered(&dev->dev)) {
465 rc = -ENODEV;
466 goto error;
467 }
468
Eric Lapuyade90099432012-05-07 12:31:13 +0200469 if (dev->active_target == NULL) {
470 rc = -ENOTCONN;
471 goto error;
472 }
473
474 if (dev->active_target->idx != target_idx) {
475 rc = -ENOTCONN;
476 goto error;
477 }
478
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200479 if (dev->ops->check_presence)
480 del_timer_sync(&dev->check_pres_timer);
481
Eric Lapuyade90099432012-05-07 12:31:13 +0200482 dev->ops->deactivate_target(dev, dev->active_target);
483 dev->active_target = NULL;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300484
485error:
486 device_unlock(&dev->dev);
487 return rc;
488}
489
490/**
491 * nfc_data_exchange - transceive data
492 *
493 * @dev: The nfc device that found the target
494 * @target_idx: index of the target
495 * @skb: data to be sent
496 * @cb: callback called when the response is received
497 * @cb_context: parameter for the callback function
498 *
499 * The user must wait for the callback before calling this function again.
500 */
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100501int nfc_data_exchange(struct nfc_dev *dev, u32 target_idx, struct sk_buff *skb,
502 data_exchange_cb_t cb, void *cb_context)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300503{
504 int rc;
505
Joe Perches20c239c2011-11-29 11:37:33 -0800506 pr_debug("dev_name=%s target_idx=%u skb->len=%u\n",
507 dev_name(&dev->dev), target_idx, skb->len);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300508
509 device_lock(&dev->dev);
510
511 if (!device_is_registered(&dev->dev)) {
512 rc = -ENODEV;
513 kfree_skb(skb);
514 goto error;
515 }
516
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +0200517 if (dev->rf_mode == NFC_RF_INITIATOR && dev->active_target != NULL) {
518 if (dev->active_target->idx != target_idx) {
519 rc = -EADDRNOTAVAIL;
520 kfree_skb(skb);
521 goto error;
522 }
523
524 if (dev->ops->check_presence)
525 del_timer_sync(&dev->check_pres_timer);
526
527 rc = dev->ops->im_transceive(dev, dev->active_target, skb, cb,
528 cb_context);
529
Eric Lapuyadef0c91032012-11-26 18:06:27 +0100530 if (!rc && dev->ops->check_presence && !dev->shutting_down)
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +0200531 mod_timer(&dev->check_pres_timer, jiffies +
532 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
533 } else if (dev->rf_mode == NFC_RF_TARGET && dev->ops->tm_send != NULL) {
534 rc = dev->ops->tm_send(dev, skb);
535 } else {
Eric Lapuyade144612c2012-04-10 19:43:11 +0200536 rc = -ENOTCONN;
537 kfree_skb(skb);
538 goto error;
539 }
540
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200541
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300542error:
543 device_unlock(&dev->dev);
544 return rc;
545}
546
Arron Wangd8eb18e2013-08-23 16:02:08 +0800547struct nfc_se *nfc_find_se(struct nfc_dev *dev, u32 se_idx)
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200548{
549 struct nfc_se *se, *n;
550
551 list_for_each_entry_safe(se, n, &dev->secure_elements, list)
552 if (se->idx == se_idx)
553 return se;
554
555 return NULL;
556}
Arron Wangd8eb18e2013-08-23 16:02:08 +0800557EXPORT_SYMBOL(nfc_find_se);
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200558
559int nfc_enable_se(struct nfc_dev *dev, u32 se_idx)
560{
561
562 struct nfc_se *se;
563 int rc;
564
565 pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
566
567 device_lock(&dev->dev);
568
569 if (!device_is_registered(&dev->dev)) {
570 rc = -ENODEV;
571 goto error;
572 }
573
574 if (!dev->dev_up) {
575 rc = -ENODEV;
576 goto error;
577 }
578
579 if (dev->polling) {
580 rc = -EBUSY;
581 goto error;
582 }
583
584 if (!dev->ops->enable_se || !dev->ops->disable_se) {
585 rc = -EOPNOTSUPP;
586 goto error;
587 }
588
Arron Wangd8eb18e2013-08-23 16:02:08 +0800589 se = nfc_find_se(dev, se_idx);
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200590 if (!se) {
591 rc = -EINVAL;
592 goto error;
593 }
594
Arron Wang2c383282013-07-30 14:35:35 +0200595 if (se->state == NFC_SE_ENABLED) {
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200596 rc = -EALREADY;
597 goto error;
598 }
599
600 rc = dev->ops->enable_se(dev, se_idx);
Arron Wang39525ee12013-07-30 14:40:05 +0200601 if (rc >= 0)
602 se->state = NFC_SE_ENABLED;
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200603
604error:
605 device_unlock(&dev->dev);
606 return rc;
607}
608
609int nfc_disable_se(struct nfc_dev *dev, u32 se_idx)
610{
611
612 struct nfc_se *se;
613 int rc;
614
615 pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
616
617 device_lock(&dev->dev);
618
619 if (!device_is_registered(&dev->dev)) {
620 rc = -ENODEV;
621 goto error;
622 }
623
624 if (!dev->dev_up) {
625 rc = -ENODEV;
626 goto error;
627 }
628
629 if (!dev->ops->enable_se || !dev->ops->disable_se) {
630 rc = -EOPNOTSUPP;
631 goto error;
632 }
633
Arron Wangd8eb18e2013-08-23 16:02:08 +0800634 se = nfc_find_se(dev, se_idx);
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200635 if (!se) {
636 rc = -EINVAL;
637 goto error;
638 }
639
Arron Wang2c383282013-07-30 14:35:35 +0200640 if (se->state == NFC_SE_DISABLED) {
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200641 rc = -EALREADY;
642 goto error;
643 }
644
645 rc = dev->ops->disable_se(dev, se_idx);
Arron Wang39525ee12013-07-30 14:40:05 +0200646 if (rc >= 0)
647 se->state = NFC_SE_DISABLED;
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200648
649error:
650 device_unlock(&dev->dev);
651 return rc;
652}
653
Samuel Ortiz541d9202011-12-14 16:43:10 +0100654int nfc_set_remote_general_bytes(struct nfc_dev *dev, u8 *gb, u8 gb_len)
655{
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100656 pr_debug("dev_name=%s gb_len=%d\n", dev_name(&dev->dev), gb_len);
Samuel Ortiz541d9202011-12-14 16:43:10 +0100657
658 if (gb_len > NFC_MAX_GT_LEN)
659 return -EINVAL;
660
Samuel Ortizd6469602011-12-14 16:43:12 +0100661 return nfc_llcp_set_remote_gb(dev, gb, gb_len);
Samuel Ortiz541d9202011-12-14 16:43:10 +0100662}
663EXPORT_SYMBOL(nfc_set_remote_general_bytes);
664
Samuel Ortizab73b752012-04-10 12:51:52 +0200665u8 *nfc_get_local_general_bytes(struct nfc_dev *dev, size_t *gb_len)
666{
667 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
668
669 return nfc_llcp_general_bytes(dev, gb_len);
670}
671EXPORT_SYMBOL(nfc_get_local_general_bytes);
672
Samuel Ortiz73167ce2012-05-31 00:05:50 +0200673int nfc_tm_data_received(struct nfc_dev *dev, struct sk_buff *skb)
674{
675 /* Only LLCP target mode for now */
676 if (dev->dep_link_up == false) {
677 kfree_skb(skb);
678 return -ENOLINK;
679 }
680
681 return nfc_llcp_data_received(dev, skb);
682}
683EXPORT_SYMBOL(nfc_tm_data_received);
684
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200685int nfc_tm_activated(struct nfc_dev *dev, u32 protocol, u8 comm_mode,
686 u8 *gb, size_t gb_len)
687{
688 int rc;
689
690 device_lock(&dev->dev);
691
692 dev->polling = false;
693
694 if (gb != NULL) {
695 rc = nfc_set_remote_general_bytes(dev, gb, gb_len);
696 if (rc < 0)
697 goto out;
698 }
699
Samuel Ortizf212ad52012-05-31 00:02:26 +0200700 dev->rf_mode = NFC_RF_TARGET;
701
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200702 if (protocol == NFC_PROTO_NFC_DEP_MASK)
703 nfc_dep_link_is_up(dev, 0, comm_mode, NFC_RF_TARGET);
704
705 rc = nfc_genl_tm_activated(dev, protocol);
706
707out:
708 device_unlock(&dev->dev);
709
710 return rc;
711}
712EXPORT_SYMBOL(nfc_tm_activated);
713
714int nfc_tm_deactivated(struct nfc_dev *dev)
715{
716 dev->dep_link_up = false;
Thierry Escande5bcf0992012-10-05 11:05:45 +0200717 dev->rf_mode = NFC_RF_NONE;
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200718
719 return nfc_genl_tm_deactivated(dev);
720}
721EXPORT_SYMBOL(nfc_tm_deactivated);
722
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300723/**
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +0100724 * nfc_alloc_send_skb - allocate a skb for data exchange responses
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300725 *
726 * @size: size to allocate
727 * @gfp: gfp flags
728 */
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +0100729struct sk_buff *nfc_alloc_send_skb(struct nfc_dev *dev, struct sock *sk,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100730 unsigned int flags, unsigned int size,
731 unsigned int *err)
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +0100732{
733 struct sk_buff *skb;
734 unsigned int total_size;
735
736 total_size = size +
737 dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE;
738
739 skb = sock_alloc_send_skb(sk, total_size, flags & MSG_DONTWAIT, err);
740 if (skb)
741 skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE);
742
743 return skb;
744}
745
746/**
747 * nfc_alloc_recv_skb - allocate a skb for data exchange responses
748 *
749 * @size: size to allocate
750 * @gfp: gfp flags
751 */
752struct sk_buff *nfc_alloc_recv_skb(unsigned int size, gfp_t gfp)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300753{
754 struct sk_buff *skb;
755 unsigned int total_size;
756
757 total_size = size + 1;
758 skb = alloc_skb(total_size, gfp);
759
760 if (skb)
761 skb_reserve(skb, 1);
762
763 return skb;
764}
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +0100765EXPORT_SYMBOL(nfc_alloc_recv_skb);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300766
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300767/**
768 * nfc_targets_found - inform that targets were found
769 *
770 * @dev: The nfc device that found the targets
771 * @targets: array of nfc targets found
772 * @ntargets: targets array size
773 *
774 * The device driver must call this function when one or many nfc targets
775 * are found. After calling this function, the device driver must stop
776 * polling for targets.
Eric Lapuyaded94f9c52012-05-03 16:33:32 +0200777 * NOTE: This function can be called with targets=NULL and n_targets=0 to
778 * notify a driver error, meaning that the polling operation cannot complete.
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200779 * IMPORTANT: this function must not be called from an atomic context.
780 * In addition, it must also not be called from a context that would prevent
781 * the NFC Core to call other nfc ops entry point concurrently.
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300782 */
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100783int nfc_targets_found(struct nfc_dev *dev,
784 struct nfc_target *targets, int n_targets)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300785{
Samuel Ortizc4fbb652012-04-10 19:43:09 +0200786 int i;
787
Joe Perches20c239c2011-11-29 11:37:33 -0800788 pr_debug("dev_name=%s n_targets=%d\n", dev_name(&dev->dev), n_targets);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300789
Samuel Ortizc4fbb652012-04-10 19:43:09 +0200790 for (i = 0; i < n_targets; i++)
Eric Lapuyade01ae0ee2012-04-10 19:43:10 +0200791 targets[i].idx = dev->target_next_idx++;
Samuel Ortizc4fbb652012-04-10 19:43:09 +0200792
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200793 device_lock(&dev->dev);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300794
Eric Lapuyade8668fdd2012-05-03 16:21:58 +0200795 if (dev->polling == false) {
796 device_unlock(&dev->dev);
797 return 0;
798 }
799
800 dev->polling = false;
801
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300802 dev->targets_generation++;
803
804 kfree(dev->targets);
Eric Lapuyaded94f9c52012-05-03 16:33:32 +0200805 dev->targets = NULL;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300806
Eric Lapuyaded94f9c52012-05-03 16:33:32 +0200807 if (targets) {
808 dev->targets = kmemdup(targets,
809 n_targets * sizeof(struct nfc_target),
810 GFP_ATOMIC);
811
812 if (!dev->targets) {
813 dev->n_targets = 0;
814 device_unlock(&dev->dev);
815 return -ENOMEM;
816 }
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300817 }
818
819 dev->n_targets = n_targets;
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200820 device_unlock(&dev->dev);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300821
822 nfc_genl_targets_found(dev);
823
824 return 0;
825}
826EXPORT_SYMBOL(nfc_targets_found);
827
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200828/**
829 * nfc_target_lost - inform that an activated target went out of field
830 *
831 * @dev: The nfc device that had the activated target in field
832 * @target_idx: the nfc index of the target
833 *
834 * The device driver must call this function when the activated target
835 * goes out of the field.
836 * IMPORTANT: this function must not be called from an atomic context.
837 * In addition, it must also not be called from a context that would prevent
838 * the NFC Core to call other nfc ops entry point concurrently.
839 */
Eric Lapuyadee1da0ef2012-04-10 19:43:05 +0200840int nfc_target_lost(struct nfc_dev *dev, u32 target_idx)
841{
842 struct nfc_target *tg;
843 int i;
844
845 pr_debug("dev_name %s n_target %d\n", dev_name(&dev->dev), target_idx);
846
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200847 device_lock(&dev->dev);
Eric Lapuyadee1da0ef2012-04-10 19:43:05 +0200848
849 for (i = 0; i < dev->n_targets; i++) {
850 tg = &dev->targets[i];
851 if (tg->idx == target_idx)
852 break;
853 }
854
855 if (i == dev->n_targets) {
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200856 device_unlock(&dev->dev);
Eric Lapuyadee1da0ef2012-04-10 19:43:05 +0200857 return -EINVAL;
858 }
859
860 dev->targets_generation++;
861 dev->n_targets--;
Eric Lapuyade90099432012-05-07 12:31:13 +0200862 dev->active_target = NULL;
Eric Lapuyadee1da0ef2012-04-10 19:43:05 +0200863
864 if (dev->n_targets) {
865 memcpy(&dev->targets[i], &dev->targets[i + 1],
866 (dev->n_targets - i) * sizeof(struct nfc_target));
867 } else {
868 kfree(dev->targets);
869 dev->targets = NULL;
870 }
871
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200872 device_unlock(&dev->dev);
Eric Lapuyadee1da0ef2012-04-10 19:43:05 +0200873
874 nfc_genl_target_lost(dev, target_idx);
875
876 return 0;
877}
878EXPORT_SYMBOL(nfc_target_lost);
879
Eric Lapuyade9eb334a2012-06-11 15:52:38 +0200880inline void nfc_driver_failure(struct nfc_dev *dev, int err)
Eric Lapuyade456411c2012-06-11 13:49:51 +0200881{
Eric Lapuyade9eb334a2012-06-11 15:52:38 +0200882 nfc_targets_found(dev, NULL, 0);
Eric Lapuyade456411c2012-06-11 13:49:51 +0200883}
884EXPORT_SYMBOL(nfc_driver_failure);
885
Samuel Ortizfed7c252013-05-10 15:28:38 +0200886int nfc_add_se(struct nfc_dev *dev, u32 se_idx, u16 type)
887{
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200888 struct nfc_se *se;
Samuel Ortiz2757c3722013-05-10 15:47:37 +0200889 int rc;
Samuel Ortizfed7c252013-05-10 15:28:38 +0200890
891 pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
892
Arron Wangd8eb18e2013-08-23 16:02:08 +0800893 se = nfc_find_se(dev, se_idx);
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200894 if (se)
895 return -EALREADY;
Samuel Ortizfed7c252013-05-10 15:28:38 +0200896
897 se = kzalloc(sizeof(struct nfc_se), GFP_KERNEL);
898 if (!se)
899 return -ENOMEM;
900
901 se->idx = se_idx;
902 se->type = type;
903 se->state = NFC_SE_DISABLED;
904 INIT_LIST_HEAD(&se->list);
905
906 list_add(&se->list, &dev->secure_elements);
907
Samuel Ortiz2757c3722013-05-10 15:47:37 +0200908 rc = nfc_genl_se_added(dev, se_idx, type);
909 if (rc < 0) {
910 list_del(&se->list);
911 kfree(se);
912
913 return rc;
914 }
915
Samuel Ortizfed7c252013-05-10 15:28:38 +0200916 return 0;
917}
918EXPORT_SYMBOL(nfc_add_se);
919
920int nfc_remove_se(struct nfc_dev *dev, u32 se_idx)
921{
922 struct nfc_se *se, *n;
Samuel Ortiz2757c3722013-05-10 15:47:37 +0200923 int rc;
Samuel Ortizfed7c252013-05-10 15:28:38 +0200924
925 pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
926
927 list_for_each_entry_safe(se, n, &dev->secure_elements, list)
928 if (se->idx == se_idx) {
Samuel Ortiz2757c3722013-05-10 15:47:37 +0200929 rc = nfc_genl_se_removed(dev, se_idx);
930 if (rc < 0)
931 return rc;
932
Samuel Ortizfed7c252013-05-10 15:28:38 +0200933 list_del(&se->list);
934 kfree(se);
935
936 return 0;
937 }
938
939 return -EINVAL;
940}
941EXPORT_SYMBOL(nfc_remove_se);
942
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300943static void nfc_release(struct device *d)
944{
945 struct nfc_dev *dev = to_nfc_dev(d);
Samuel Ortizee656e92013-05-10 15:53:29 +0200946 struct nfc_se *se, *n;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300947
Joe Perches20c239c2011-11-29 11:37:33 -0800948 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300949
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300950 nfc_genl_data_exit(&dev->genl_data);
951 kfree(dev->targets);
Samuel Ortizee656e92013-05-10 15:53:29 +0200952
953 list_for_each_entry_safe(se, n, &dev->secure_elements, list) {
954 nfc_genl_se_removed(dev, se->idx);
955 list_del(&se->list);
956 kfree(se);
957 }
958
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300959 kfree(dev);
960}
961
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200962static void nfc_check_pres_work(struct work_struct *work)
963{
964 struct nfc_dev *dev = container_of(work, struct nfc_dev,
965 check_pres_work);
966 int rc;
967
968 device_lock(&dev->dev);
969
Eric Lapuyade90099432012-05-07 12:31:13 +0200970 if (dev->active_target && timer_pending(&dev->check_pres_timer) == 0) {
971 rc = dev->ops->check_presence(dev, dev->active_target);
Eric Lapuyade632c0162012-10-02 17:27:36 +0200972 if (rc == -EOPNOTSUPP)
973 goto exit;
Eric Lapuyadef0c91032012-11-26 18:06:27 +0100974 if (rc) {
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200975 u32 active_target_idx = dev->active_target->idx;
976 device_unlock(&dev->dev);
977 nfc_target_lost(dev, active_target_idx);
978 return;
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200979 }
Eric Lapuyadef0c91032012-11-26 18:06:27 +0100980
981 if (!dev->shutting_down)
982 mod_timer(&dev->check_pres_timer, jiffies +
983 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200984 }
985
Eric Lapuyade632c0162012-10-02 17:27:36 +0200986exit:
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200987 device_unlock(&dev->dev);
988}
989
990static void nfc_check_pres_timeout(unsigned long data)
991{
992 struct nfc_dev *dev = (struct nfc_dev *)data;
993
Linus Torvalds916082b2012-10-02 16:01:31 -0700994 schedule_work(&dev->check_pres_work);
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200995}
996
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300997struct class nfc_class = {
998 .name = "nfc",
999 .dev_release = nfc_release,
1000};
1001EXPORT_SYMBOL(nfc_class);
1002
Michał Mirosław9f3b7952013-02-01 20:40:17 +01001003static int match_idx(struct device *d, const void *data)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001004{
1005 struct nfc_dev *dev = to_nfc_dev(d);
Michał Mirosław9f3b7952013-02-01 20:40:17 +01001006 const unsigned int *idx = data;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001007
1008 return dev->idx == *idx;
1009}
1010
Eric Dumazet95c96172012-04-15 05:58:06 +00001011struct nfc_dev *nfc_get_device(unsigned int idx)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001012{
1013 struct device *d;
1014
1015 d = class_find_device(&nfc_class, NULL, &idx, match_idx);
1016 if (!d)
1017 return NULL;
1018
1019 return to_nfc_dev(d);
1020}
1021
1022/**
1023 * nfc_allocate_device - allocate a new nfc device
1024 *
1025 * @ops: device operations
1026 * @supported_protocols: NFC protocols supported by the device
1027 */
1028struct nfc_dev *nfc_allocate_device(struct nfc_ops *ops,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +01001029 u32 supported_protocols,
1030 int tx_headroom, int tx_tailroom)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001031{
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001032 struct nfc_dev *dev;
1033
1034 if (!ops->start_poll || !ops->stop_poll || !ops->activate_target ||
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02001035 !ops->deactivate_target || !ops->im_transceive)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001036 return NULL;
1037
1038 if (!supported_protocols)
1039 return NULL;
1040
1041 dev = kzalloc(sizeof(struct nfc_dev), GFP_KERNEL);
1042 if (!dev)
1043 return NULL;
1044
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001045 dev->ops = ops;
1046 dev->supported_protocols = supported_protocols;
Samuel Ortize8753042011-08-19 15:47:11 +02001047 dev->tx_headroom = tx_headroom;
1048 dev->tx_tailroom = tx_tailroom;
Samuel Ortizfed7c252013-05-10 15:28:38 +02001049 INIT_LIST_HEAD(&dev->secure_elements);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001050
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001051 nfc_genl_data_init(&dev->genl_data);
1052
Thierry Escande5bcf0992012-10-05 11:05:45 +02001053 dev->rf_mode = NFC_RF_NONE;
Eric Lapuyaded4ccb132012-05-07 12:31:15 +02001054
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001055 /* first generation must not be 0 */
1056 dev->targets_generation = 1;
1057
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +02001058 if (ops->check_presence) {
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +02001059 init_timer(&dev->check_pres_timer);
1060 dev->check_pres_timer.data = (unsigned long)dev;
1061 dev->check_pres_timer.function = nfc_check_pres_timeout;
1062
1063 INIT_WORK(&dev->check_pres_work, nfc_check_pres_work);
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +02001064 }
1065
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001066 return dev;
1067}
1068EXPORT_SYMBOL(nfc_allocate_device);
1069
1070/**
1071 * nfc_register_device - register a nfc device in the nfc subsystem
1072 *
1073 * @dev: The nfc device to register
1074 */
1075int nfc_register_device(struct nfc_dev *dev)
1076{
1077 int rc;
1078
Joe Perches20c239c2011-11-29 11:37:33 -08001079 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001080
Samuel Ortiz7eda8b8e92012-10-22 15:57:58 +02001081 dev->idx = ida_simple_get(&nfc_index_ida, 0, 0, GFP_KERNEL);
1082 if (dev->idx < 0)
1083 return dev->idx;
1084
1085 dev->dev.class = &nfc_class;
1086 dev_set_name(&dev->dev, "nfc%d", dev->idx);
1087 device_initialize(&dev->dev);
1088
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001089 mutex_lock(&nfc_devlist_mutex);
1090 nfc_devlist_generation++;
1091 rc = device_add(&dev->dev);
1092 mutex_unlock(&nfc_devlist_mutex);
1093
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001094 if (rc < 0)
1095 return rc;
1096
Samuel Ortizd6469602011-12-14 16:43:12 +01001097 rc = nfc_llcp_register_device(dev);
1098 if (rc)
1099 pr_err("Could not register llcp device\n");
1100
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001101 rc = nfc_genl_device_added(dev);
1102 if (rc)
Joe Perches20c239c2011-11-29 11:37:33 -08001103 pr_debug("The userspace won't be notified that the device %s was added\n",
1104 dev_name(&dev->dev));
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001105
Samuel Ortizbe055b22013-04-11 11:52:20 +02001106 dev->rfkill = rfkill_alloc(dev_name(&dev->dev), &dev->dev,
1107 RFKILL_TYPE_NFC, &nfc_rfkill_ops, dev);
1108 if (dev->rfkill) {
1109 if (rfkill_register(dev->rfkill) < 0) {
1110 rfkill_destroy(dev->rfkill);
1111 dev->rfkill = NULL;
1112 }
1113 }
1114
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001115 return 0;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001116}
1117EXPORT_SYMBOL(nfc_register_device);
1118
1119/**
1120 * nfc_unregister_device - unregister a nfc device in the nfc subsystem
1121 *
1122 * @dev: The nfc device to unregister
1123 */
1124void nfc_unregister_device(struct nfc_dev *dev)
1125{
Samuel Ortiz7eda8b8e92012-10-22 15:57:58 +02001126 int rc, id;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001127
Joe Perches20c239c2011-11-29 11:37:33 -08001128 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001129
Samuel Ortiz7eda8b8e92012-10-22 15:57:58 +02001130 id = dev->idx;
1131
Samuel Ortizbe055b22013-04-11 11:52:20 +02001132 if (dev->rfkill) {
1133 rfkill_unregister(dev->rfkill);
1134 rfkill_destroy(dev->rfkill);
1135 }
1136
Eric Lapuyadef0c91032012-11-26 18:06:27 +01001137 if (dev->ops->check_presence) {
1138 device_lock(&dev->dev);
1139 dev->shutting_down = true;
1140 device_unlock(&dev->dev);
1141 del_timer_sync(&dev->check_pres_timer);
1142 cancel_work_sync(&dev->check_pres_work);
1143 }
Samuel Ortizd6469602011-12-14 16:43:12 +01001144
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001145 rc = nfc_genl_device_removed(dev);
1146 if (rc)
Eric Lapuyadef0c91032012-11-26 18:06:27 +01001147 pr_debug("The userspace won't be notified that the device %s "
1148 "was removed\n", dev_name(&dev->dev));
1149
1150 nfc_llcp_unregister_device(dev);
1151
1152 mutex_lock(&nfc_devlist_mutex);
1153 nfc_devlist_generation++;
1154 device_del(&dev->dev);
1155 mutex_unlock(&nfc_devlist_mutex);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001156
Samuel Ortiz7eda8b8e92012-10-22 15:57:58 +02001157 ida_simple_remove(&nfc_index_ida, id);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001158}
1159EXPORT_SYMBOL(nfc_unregister_device);
1160
1161static int __init nfc_init(void)
1162{
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001163 int rc;
1164
Joe Perchesed1e0ad2011-11-29 11:37:32 -08001165 pr_info("NFC Core ver %s\n", VERSION);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001166
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001167 rc = class_register(&nfc_class);
1168 if (rc)
1169 return rc;
1170
1171 rc = nfc_genl_init();
1172 if (rc)
1173 goto err_genl;
1174
1175 /* the first generation must not be 0 */
1176 nfc_devlist_generation = 1;
1177
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -03001178 rc = rawsock_init();
1179 if (rc)
1180 goto err_rawsock;
1181
Samuel Ortizd6469602011-12-14 16:43:12 +01001182 rc = nfc_llcp_init();
1183 if (rc)
1184 goto err_llcp_sock;
1185
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -03001186 rc = af_nfc_init();
1187 if (rc)
1188 goto err_af_nfc;
1189
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001190 return 0;
1191
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -03001192err_af_nfc:
Samuel Ortizd6469602011-12-14 16:43:12 +01001193 nfc_llcp_exit();
1194err_llcp_sock:
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -03001195 rawsock_exit();
1196err_rawsock:
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -03001197 nfc_genl_exit();
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001198err_genl:
1199 class_unregister(&nfc_class);
1200 return rc;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001201}
1202
1203static void __exit nfc_exit(void)
1204{
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -03001205 af_nfc_exit();
Samuel Ortizd6469602011-12-14 16:43:12 +01001206 nfc_llcp_exit();
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -03001207 rawsock_exit();
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001208 nfc_genl_exit();
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001209 class_unregister(&nfc_class);
1210}
1211
1212subsys_initcall(nfc_init);
1213module_exit(nfc_exit);
1214
1215MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
1216MODULE_DESCRIPTION("NFC Core ver " VERSION);
1217MODULE_VERSION(VERSION);
1218MODULE_LICENSE("GPL");
Samuel Ortiz1155bb62012-06-12 00:35:50 +02001219MODULE_ALIAS_NETPROTO(PF_NFC);
Samuel Ortiz5df16ca2012-06-12 16:54:16 +02001220MODULE_ALIAS_GENL_FAMILY(NFC_GENL_NAME);