blob: 269ffc5288d02fcf894e0cac249ec5073bf2f785 [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
19 * along with this program; if not, write to the
20 * Free Software Foundation, Inc.,
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 */
23
Samuel Ortiz52858b52011-12-14 16:43:05 +010024#define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
Joe Perchesed1e0ad2011-11-29 11:37:32 -080025
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -030026#include <linux/init.h>
27#include <linux/kernel.h>
28#include <linux/module.h>
29#include <linux/slab.h>
Samuel Ortizbe055b22013-04-11 11:52:20 +020030#include <linux/rfkill.h>
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +010031#include <linux/nfc.h>
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -030032
Samuel Ortiz5df16ca2012-06-12 16:54:16 +020033#include <net/genetlink.h>
34
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -030035#include "nfc.h"
36
37#define VERSION "0.1"
38
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +020039#define NFC_CHECK_PRES_FREQ_MS 2000
40
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -030041int nfc_devlist_generation;
42DEFINE_MUTEX(nfc_devlist_mutex);
43
Samuel Ortiz7eda8b8e92012-10-22 15:57:58 +020044/* NFC device ID bitmap */
45static DEFINE_IDA(nfc_index_ida);
46
Samuel Ortiz9ea71872013-07-31 01:19:43 +020047int nfc_fw_download(struct nfc_dev *dev, const char *firmware_name)
Eric Lapuyade9674da82013-04-29 17:13:27 +020048{
49 int rc = 0;
50
51 pr_debug("%s do firmware %s\n", dev_name(&dev->dev), firmware_name);
52
53 device_lock(&dev->dev);
54
55 if (!device_is_registered(&dev->dev)) {
56 rc = -ENODEV;
57 goto error;
58 }
59
60 if (dev->dev_up) {
61 rc = -EBUSY;
62 goto error;
63 }
64
Samuel Ortiz9ea71872013-07-31 01:19:43 +020065 if (!dev->ops->fw_download) {
Eric Lapuyade9674da82013-04-29 17:13:27 +020066 rc = -EOPNOTSUPP;
67 goto error;
68 }
69
Samuel Ortiz9ea71872013-07-31 01:19:43 +020070 dev->fw_download_in_progress = true;
71 rc = dev->ops->fw_download(dev, firmware_name);
Eric Lapuyade9674da82013-04-29 17:13:27 +020072 if (rc)
Samuel Ortiz9ea71872013-07-31 01:19:43 +020073 dev->fw_download_in_progress = false;
Eric Lapuyade9674da82013-04-29 17:13:27 +020074
75error:
76 device_unlock(&dev->dev);
77 return rc;
78}
79
Eric Lapuyade352a5f52013-07-19 14:57:55 +020080/**
81 * nfc_fw_download_done - inform that a firmware download was completed
82 *
83 * @dev: The nfc device to which firmware was downloaded
84 * @firmware_name: The firmware filename
85 * @result: The positive value of a standard errno value
86 */
87int nfc_fw_download_done(struct nfc_dev *dev, const char *firmware_name,
88 u32 result)
Eric Lapuyade9674da82013-04-29 17:13:27 +020089{
Samuel Ortiz9ea71872013-07-31 01:19:43 +020090 dev->fw_download_in_progress = false;
Eric Lapuyade9674da82013-04-29 17:13:27 +020091
Eric Lapuyade352a5f52013-07-19 14:57:55 +020092 return nfc_genl_fw_download_done(dev, firmware_name, result);
Eric Lapuyade9674da82013-04-29 17:13:27 +020093}
Samuel Ortiz9ea71872013-07-31 01:19:43 +020094EXPORT_SYMBOL(nfc_fw_download_done);
Eric Lapuyade9674da82013-04-29 17:13:27 +020095
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -030096/**
Ilan Elias8b3fe7b2011-09-18 11:19:33 +030097 * nfc_dev_up - turn on the NFC device
98 *
99 * @dev: The nfc device to be turned on
100 *
101 * The device remains up until the nfc_dev_down function is called.
102 */
103int nfc_dev_up(struct nfc_dev *dev)
104{
105 int rc = 0;
106
Joe Perches20c239c2011-11-29 11:37:33 -0800107 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300108
109 device_lock(&dev->dev);
110
Samuel Ortizbe055b22013-04-11 11:52:20 +0200111 if (dev->rfkill && rfkill_blocked(dev->rfkill)) {
112 rc = -ERFKILL;
113 goto error;
114 }
115
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300116 if (!device_is_registered(&dev->dev)) {
117 rc = -ENODEV;
118 goto error;
119 }
120
Samuel Ortiz9ea71872013-07-31 01:19:43 +0200121 if (dev->fw_download_in_progress) {
Eric Lapuyade9674da82013-04-29 17:13:27 +0200122 rc = -EBUSY;
123 goto error;
124 }
125
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300126 if (dev->dev_up) {
127 rc = -EALREADY;
128 goto error;
129 }
130
131 if (dev->ops->dev_up)
132 rc = dev->ops->dev_up(dev);
133
134 if (!rc)
135 dev->dev_up = true;
136
Samuel Ortiz0a946302013-05-10 11:57:06 +0200137 /* We have to enable the device before discovering SEs */
138 if (dev->ops->discover_se) {
139 rc = dev->ops->discover_se(dev);
Samuel Ortiz369f4d52013-07-24 14:49:22 +0200140 if (rc)
Samuel Ortiz0a946302013-05-10 11:57:06 +0200141 pr_warn("SE discovery failed\n");
142 }
143
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300144error:
145 device_unlock(&dev->dev);
146 return rc;
147}
148
149/**
150 * nfc_dev_down - turn off the NFC device
151 *
152 * @dev: The nfc device to be turned off
153 */
154int nfc_dev_down(struct nfc_dev *dev)
155{
156 int rc = 0;
157
Joe Perches20c239c2011-11-29 11:37:33 -0800158 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300159
160 device_lock(&dev->dev);
161
162 if (!device_is_registered(&dev->dev)) {
163 rc = -ENODEV;
164 goto error;
165 }
166
167 if (!dev->dev_up) {
168 rc = -EALREADY;
169 goto error;
170 }
171
Eric Lapuyade90099432012-05-07 12:31:13 +0200172 if (dev->polling || dev->active_target) {
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300173 rc = -EBUSY;
174 goto error;
175 }
176
177 if (dev->ops->dev_down)
178 dev->ops->dev_down(dev);
179
180 dev->dev_up = false;
181
182error:
183 device_unlock(&dev->dev);
184 return rc;
185}
186
Samuel Ortizbe055b22013-04-11 11:52:20 +0200187static int nfc_rfkill_set_block(void *data, bool blocked)
188{
189 struct nfc_dev *dev = data;
190
191 pr_debug("%s blocked %d", dev_name(&dev->dev), blocked);
192
193 if (!blocked)
194 return 0;
195
196 nfc_dev_down(dev);
197
198 return 0;
199}
200
201static const struct rfkill_ops nfc_rfkill_ops = {
202 .set_block = nfc_rfkill_set_block,
203};
204
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300205/**
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300206 * nfc_start_poll - start polling for nfc targets
207 *
208 * @dev: The nfc device that must start polling
209 * @protocols: bitset of nfc protocols that must be used for polling
210 *
211 * The device remains polling for targets until a target is found or
212 * the nfc_stop_poll function is called.
213 */
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200214int nfc_start_poll(struct nfc_dev *dev, u32 im_protocols, u32 tm_protocols)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300215{
216 int rc;
217
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200218 pr_debug("dev_name %s initiator protocols 0x%x target protocols 0x%x\n",
219 dev_name(&dev->dev), im_protocols, tm_protocols);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300220
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200221 if (!im_protocols && !tm_protocols)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300222 return -EINVAL;
223
224 device_lock(&dev->dev);
225
226 if (!device_is_registered(&dev->dev)) {
227 rc = -ENODEV;
228 goto error;
229 }
230
Samuel Ortiz7757dc82013-04-10 12:25:30 +0200231 if (!dev->dev_up) {
232 rc = -ENODEV;
233 goto error;
234 }
235
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300236 if (dev->polling) {
237 rc = -EBUSY;
238 goto error;
239 }
240
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200241 rc = dev->ops->start_poll(dev, im_protocols, tm_protocols);
Samuel Ortizf212ad52012-05-31 00:02:26 +0200242 if (!rc) {
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300243 dev->polling = true;
Samuel Ortizf212ad52012-05-31 00:02:26 +0200244 dev->rf_mode = NFC_RF_NONE;
245 }
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300246
247error:
248 device_unlock(&dev->dev);
249 return rc;
250}
251
252/**
253 * nfc_stop_poll - stop polling for nfc targets
254 *
255 * @dev: The nfc device that must stop polling
256 */
257int nfc_stop_poll(struct nfc_dev *dev)
258{
259 int rc = 0;
260
Joe Perches20c239c2011-11-29 11:37:33 -0800261 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300262
263 device_lock(&dev->dev);
264
265 if (!device_is_registered(&dev->dev)) {
266 rc = -ENODEV;
267 goto error;
268 }
269
270 if (!dev->polling) {
271 rc = -EINVAL;
272 goto error;
273 }
274
275 dev->ops->stop_poll(dev);
276 dev->polling = false;
Thierry Escande5bcf0992012-10-05 11:05:45 +0200277 dev->rf_mode = NFC_RF_NONE;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300278
279error:
280 device_unlock(&dev->dev);
281 return rc;
282}
283
Eric Lapuyade90099432012-05-07 12:31:13 +0200284static struct nfc_target *nfc_find_target(struct nfc_dev *dev, u32 target_idx)
285{
286 int i;
287
288 if (dev->n_targets == 0)
289 return NULL;
290
Szymon Janc0f450772012-10-17 15:23:39 +0200291 for (i = 0; i < dev->n_targets; i++) {
Eric Lapuyade90099432012-05-07 12:31:13 +0200292 if (dev->targets[i].idx == target_idx)
293 return &dev->targets[i];
294 }
295
296 return NULL;
297}
298
Samuel Ortiz47807d32012-03-05 01:03:50 +0100299int nfc_dep_link_up(struct nfc_dev *dev, int target_index, u8 comm_mode)
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100300{
301 int rc = 0;
Samuel Ortiz47807d32012-03-05 01:03:50 +0100302 u8 *gb;
303 size_t gb_len;
Eric Lapuyade90099432012-05-07 12:31:13 +0200304 struct nfc_target *target;
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100305
Samuel Ortiz47807d32012-03-05 01:03:50 +0100306 pr_debug("dev_name=%s comm %d\n", dev_name(&dev->dev), comm_mode);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100307
308 if (!dev->ops->dep_link_up)
309 return -EOPNOTSUPP;
310
311 device_lock(&dev->dev);
312
313 if (!device_is_registered(&dev->dev)) {
314 rc = -ENODEV;
315 goto error;
316 }
317
318 if (dev->dep_link_up == true) {
319 rc = -EALREADY;
320 goto error;
321 }
322
Samuel Ortiz47807d32012-03-05 01:03:50 +0100323 gb = nfc_llcp_general_bytes(dev, &gb_len);
324 if (gb_len > NFC_MAX_GT_LEN) {
325 rc = -EINVAL;
326 goto error;
327 }
328
Eric Lapuyade90099432012-05-07 12:31:13 +0200329 target = nfc_find_target(dev, target_index);
330 if (target == NULL) {
331 rc = -ENOTCONN;
332 goto error;
333 }
334
335 rc = dev->ops->dep_link_up(dev, target, comm_mode, gb, gb_len);
Samuel Ortizf212ad52012-05-31 00:02:26 +0200336 if (!rc) {
Eric Lapuyade90099432012-05-07 12:31:13 +0200337 dev->active_target = target;
Samuel Ortizf212ad52012-05-31 00:02:26 +0200338 dev->rf_mode = NFC_RF_INITIATOR;
339 }
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100340
341error:
342 device_unlock(&dev->dev);
343 return rc;
344}
345
346int nfc_dep_link_down(struct nfc_dev *dev)
347{
348 int rc = 0;
349
350 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
351
352 if (!dev->ops->dep_link_down)
353 return -EOPNOTSUPP;
354
355 device_lock(&dev->dev);
356
357 if (!device_is_registered(&dev->dev)) {
358 rc = -ENODEV;
359 goto error;
360 }
361
362 if (dev->dep_link_up == false) {
363 rc = -EALREADY;
364 goto error;
365 }
366
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100367 rc = dev->ops->dep_link_down(dev);
368 if (!rc) {
369 dev->dep_link_up = false;
Eric Lapuyade90099432012-05-07 12:31:13 +0200370 dev->active_target = NULL;
Thierry Escande5bcf0992012-10-05 11:05:45 +0200371 dev->rf_mode = NFC_RF_NONE;
Samuel Ortizd6469602011-12-14 16:43:12 +0100372 nfc_llcp_mac_is_down(dev);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100373 nfc_genl_dep_link_down_event(dev);
374 }
375
376error:
377 device_unlock(&dev->dev);
Thierry Escande5bcf0992012-10-05 11:05:45 +0200378
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100379 return rc;
380}
381
382int nfc_dep_link_is_up(struct nfc_dev *dev, u32 target_idx,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100383 u8 comm_mode, u8 rf_mode)
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100384{
385 dev->dep_link_up = true;
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100386
Samuel Ortizd6469602011-12-14 16:43:12 +0100387 nfc_llcp_mac_is_up(dev, target_idx, comm_mode, rf_mode);
388
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100389 return nfc_genl_dep_link_up_event(dev, target_idx, comm_mode, rf_mode);
390}
391EXPORT_SYMBOL(nfc_dep_link_is_up);
392
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300393/**
394 * nfc_activate_target - prepare the target for data exchange
395 *
396 * @dev: The nfc device that found the target
397 * @target_idx: index of the target that must be activated
398 * @protocol: nfc protocol that will be used for data exchange
399 */
400int nfc_activate_target(struct nfc_dev *dev, u32 target_idx, u32 protocol)
401{
402 int rc;
Eric Lapuyade90099432012-05-07 12:31:13 +0200403 struct nfc_target *target;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300404
Joe Perches20c239c2011-11-29 11:37:33 -0800405 pr_debug("dev_name=%s target_idx=%u protocol=%u\n",
406 dev_name(&dev->dev), target_idx, protocol);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300407
408 device_lock(&dev->dev);
409
410 if (!device_is_registered(&dev->dev)) {
411 rc = -ENODEV;
412 goto error;
413 }
414
Eric Lapuyade90099432012-05-07 12:31:13 +0200415 if (dev->active_target) {
416 rc = -EBUSY;
417 goto error;
418 }
419
420 target = nfc_find_target(dev, target_idx);
421 if (target == NULL) {
422 rc = -ENOTCONN;
423 goto error;
424 }
425
426 rc = dev->ops->activate_target(dev, target, protocol);
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200427 if (!rc) {
Eric Lapuyade90099432012-05-07 12:31:13 +0200428 dev->active_target = target;
Samuel Ortizf212ad52012-05-31 00:02:26 +0200429 dev->rf_mode = NFC_RF_INITIATOR;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300430
Eric Lapuyadef0c91032012-11-26 18:06:27 +0100431 if (dev->ops->check_presence && !dev->shutting_down)
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200432 mod_timer(&dev->check_pres_timer, jiffies +
433 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
434 }
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300435
436error:
437 device_unlock(&dev->dev);
438 return rc;
439}
440
441/**
442 * nfc_deactivate_target - deactivate a nfc target
443 *
444 * @dev: The nfc device that found the target
445 * @target_idx: index of the target that must be deactivated
446 */
447int nfc_deactivate_target(struct nfc_dev *dev, u32 target_idx)
448{
449 int rc = 0;
450
Joe Perches20c239c2011-11-29 11:37:33 -0800451 pr_debug("dev_name=%s target_idx=%u\n",
452 dev_name(&dev->dev), target_idx);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300453
454 device_lock(&dev->dev);
455
456 if (!device_is_registered(&dev->dev)) {
457 rc = -ENODEV;
458 goto error;
459 }
460
Eric Lapuyade90099432012-05-07 12:31:13 +0200461 if (dev->active_target == NULL) {
462 rc = -ENOTCONN;
463 goto error;
464 }
465
466 if (dev->active_target->idx != target_idx) {
467 rc = -ENOTCONN;
468 goto error;
469 }
470
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200471 if (dev->ops->check_presence)
472 del_timer_sync(&dev->check_pres_timer);
473
Eric Lapuyade90099432012-05-07 12:31:13 +0200474 dev->ops->deactivate_target(dev, dev->active_target);
475 dev->active_target = NULL;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300476
477error:
478 device_unlock(&dev->dev);
479 return rc;
480}
481
482/**
483 * nfc_data_exchange - transceive data
484 *
485 * @dev: The nfc device that found the target
486 * @target_idx: index of the target
487 * @skb: data to be sent
488 * @cb: callback called when the response is received
489 * @cb_context: parameter for the callback function
490 *
491 * The user must wait for the callback before calling this function again.
492 */
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100493int nfc_data_exchange(struct nfc_dev *dev, u32 target_idx, struct sk_buff *skb,
494 data_exchange_cb_t cb, void *cb_context)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300495{
496 int rc;
497
Joe Perches20c239c2011-11-29 11:37:33 -0800498 pr_debug("dev_name=%s target_idx=%u skb->len=%u\n",
499 dev_name(&dev->dev), target_idx, skb->len);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300500
501 device_lock(&dev->dev);
502
503 if (!device_is_registered(&dev->dev)) {
504 rc = -ENODEV;
505 kfree_skb(skb);
506 goto error;
507 }
508
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +0200509 if (dev->rf_mode == NFC_RF_INITIATOR && dev->active_target != NULL) {
510 if (dev->active_target->idx != target_idx) {
511 rc = -EADDRNOTAVAIL;
512 kfree_skb(skb);
513 goto error;
514 }
515
516 if (dev->ops->check_presence)
517 del_timer_sync(&dev->check_pres_timer);
518
519 rc = dev->ops->im_transceive(dev, dev->active_target, skb, cb,
520 cb_context);
521
Eric Lapuyadef0c91032012-11-26 18:06:27 +0100522 if (!rc && dev->ops->check_presence && !dev->shutting_down)
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +0200523 mod_timer(&dev->check_pres_timer, jiffies +
524 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
525 } else if (dev->rf_mode == NFC_RF_TARGET && dev->ops->tm_send != NULL) {
526 rc = dev->ops->tm_send(dev, skb);
527 } else {
Eric Lapuyade144612c2012-04-10 19:43:11 +0200528 rc = -ENOTCONN;
529 kfree_skb(skb);
530 goto error;
531 }
532
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200533
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300534error:
535 device_unlock(&dev->dev);
536 return rc;
537}
538
Arron Wangd8eb18e2013-08-23 16:02:08 +0800539struct nfc_se *nfc_find_se(struct nfc_dev *dev, u32 se_idx)
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200540{
541 struct nfc_se *se, *n;
542
543 list_for_each_entry_safe(se, n, &dev->secure_elements, list)
544 if (se->idx == se_idx)
545 return se;
546
547 return NULL;
548}
Arron Wangd8eb18e2013-08-23 16:02:08 +0800549EXPORT_SYMBOL(nfc_find_se);
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200550
551int nfc_enable_se(struct nfc_dev *dev, u32 se_idx)
552{
553
554 struct nfc_se *se;
555 int rc;
556
557 pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
558
559 device_lock(&dev->dev);
560
561 if (!device_is_registered(&dev->dev)) {
562 rc = -ENODEV;
563 goto error;
564 }
565
566 if (!dev->dev_up) {
567 rc = -ENODEV;
568 goto error;
569 }
570
571 if (dev->polling) {
572 rc = -EBUSY;
573 goto error;
574 }
575
576 if (!dev->ops->enable_se || !dev->ops->disable_se) {
577 rc = -EOPNOTSUPP;
578 goto error;
579 }
580
Arron Wangd8eb18e2013-08-23 16:02:08 +0800581 se = nfc_find_se(dev, se_idx);
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200582 if (!se) {
583 rc = -EINVAL;
584 goto error;
585 }
586
Arron Wang2c383282013-07-30 14:35:35 +0200587 if (se->state == NFC_SE_ENABLED) {
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200588 rc = -EALREADY;
589 goto error;
590 }
591
592 rc = dev->ops->enable_se(dev, se_idx);
Arron Wang39525ee12013-07-30 14:40:05 +0200593 if (rc >= 0)
594 se->state = NFC_SE_ENABLED;
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200595
596error:
597 device_unlock(&dev->dev);
598 return rc;
599}
600
601int nfc_disable_se(struct nfc_dev *dev, u32 se_idx)
602{
603
604 struct nfc_se *se;
605 int rc;
606
607 pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
608
609 device_lock(&dev->dev);
610
611 if (!device_is_registered(&dev->dev)) {
612 rc = -ENODEV;
613 goto error;
614 }
615
616 if (!dev->dev_up) {
617 rc = -ENODEV;
618 goto error;
619 }
620
621 if (!dev->ops->enable_se || !dev->ops->disable_se) {
622 rc = -EOPNOTSUPP;
623 goto error;
624 }
625
Arron Wangd8eb18e2013-08-23 16:02:08 +0800626 se = nfc_find_se(dev, se_idx);
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200627 if (!se) {
628 rc = -EINVAL;
629 goto error;
630 }
631
Arron Wang2c383282013-07-30 14:35:35 +0200632 if (se->state == NFC_SE_DISABLED) {
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200633 rc = -EALREADY;
634 goto error;
635 }
636
637 rc = dev->ops->disable_se(dev, se_idx);
Arron Wang39525ee12013-07-30 14:40:05 +0200638 if (rc >= 0)
639 se->state = NFC_SE_DISABLED;
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200640
641error:
642 device_unlock(&dev->dev);
643 return rc;
644}
645
Samuel Ortiz541d9202011-12-14 16:43:10 +0100646int nfc_set_remote_general_bytes(struct nfc_dev *dev, u8 *gb, u8 gb_len)
647{
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100648 pr_debug("dev_name=%s gb_len=%d\n", dev_name(&dev->dev), gb_len);
Samuel Ortiz541d9202011-12-14 16:43:10 +0100649
650 if (gb_len > NFC_MAX_GT_LEN)
651 return -EINVAL;
652
Samuel Ortizd6469602011-12-14 16:43:12 +0100653 return nfc_llcp_set_remote_gb(dev, gb, gb_len);
Samuel Ortiz541d9202011-12-14 16:43:10 +0100654}
655EXPORT_SYMBOL(nfc_set_remote_general_bytes);
656
Samuel Ortizab73b752012-04-10 12:51:52 +0200657u8 *nfc_get_local_general_bytes(struct nfc_dev *dev, size_t *gb_len)
658{
659 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
660
661 return nfc_llcp_general_bytes(dev, gb_len);
662}
663EXPORT_SYMBOL(nfc_get_local_general_bytes);
664
Samuel Ortiz73167ce2012-05-31 00:05:50 +0200665int nfc_tm_data_received(struct nfc_dev *dev, struct sk_buff *skb)
666{
667 /* Only LLCP target mode for now */
668 if (dev->dep_link_up == false) {
669 kfree_skb(skb);
670 return -ENOLINK;
671 }
672
673 return nfc_llcp_data_received(dev, skb);
674}
675EXPORT_SYMBOL(nfc_tm_data_received);
676
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200677int nfc_tm_activated(struct nfc_dev *dev, u32 protocol, u8 comm_mode,
678 u8 *gb, size_t gb_len)
679{
680 int rc;
681
682 device_lock(&dev->dev);
683
684 dev->polling = false;
685
686 if (gb != NULL) {
687 rc = nfc_set_remote_general_bytes(dev, gb, gb_len);
688 if (rc < 0)
689 goto out;
690 }
691
Samuel Ortizf212ad52012-05-31 00:02:26 +0200692 dev->rf_mode = NFC_RF_TARGET;
693
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200694 if (protocol == NFC_PROTO_NFC_DEP_MASK)
695 nfc_dep_link_is_up(dev, 0, comm_mode, NFC_RF_TARGET);
696
697 rc = nfc_genl_tm_activated(dev, protocol);
698
699out:
700 device_unlock(&dev->dev);
701
702 return rc;
703}
704EXPORT_SYMBOL(nfc_tm_activated);
705
706int nfc_tm_deactivated(struct nfc_dev *dev)
707{
708 dev->dep_link_up = false;
Thierry Escande5bcf0992012-10-05 11:05:45 +0200709 dev->rf_mode = NFC_RF_NONE;
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200710
711 return nfc_genl_tm_deactivated(dev);
712}
713EXPORT_SYMBOL(nfc_tm_deactivated);
714
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300715/**
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +0100716 * nfc_alloc_send_skb - allocate a skb for data exchange responses
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300717 *
718 * @size: size to allocate
719 * @gfp: gfp flags
720 */
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +0100721struct sk_buff *nfc_alloc_send_skb(struct nfc_dev *dev, struct sock *sk,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100722 unsigned int flags, unsigned int size,
723 unsigned int *err)
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +0100724{
725 struct sk_buff *skb;
726 unsigned int total_size;
727
728 total_size = size +
729 dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE;
730
731 skb = sock_alloc_send_skb(sk, total_size, flags & MSG_DONTWAIT, err);
732 if (skb)
733 skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE);
734
735 return skb;
736}
737
738/**
739 * nfc_alloc_recv_skb - allocate a skb for data exchange responses
740 *
741 * @size: size to allocate
742 * @gfp: gfp flags
743 */
744struct sk_buff *nfc_alloc_recv_skb(unsigned int size, gfp_t gfp)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300745{
746 struct sk_buff *skb;
747 unsigned int total_size;
748
749 total_size = size + 1;
750 skb = alloc_skb(total_size, gfp);
751
752 if (skb)
753 skb_reserve(skb, 1);
754
755 return skb;
756}
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +0100757EXPORT_SYMBOL(nfc_alloc_recv_skb);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300758
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300759/**
760 * nfc_targets_found - inform that targets were found
761 *
762 * @dev: The nfc device that found the targets
763 * @targets: array of nfc targets found
764 * @ntargets: targets array size
765 *
766 * The device driver must call this function when one or many nfc targets
767 * are found. After calling this function, the device driver must stop
768 * polling for targets.
Eric Lapuyaded94f9c52012-05-03 16:33:32 +0200769 * NOTE: This function can be called with targets=NULL and n_targets=0 to
770 * notify a driver error, meaning that the polling operation cannot complete.
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200771 * IMPORTANT: this function must not be called from an atomic context.
772 * In addition, it must also not be called from a context that would prevent
773 * the NFC Core to call other nfc ops entry point concurrently.
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300774 */
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100775int nfc_targets_found(struct nfc_dev *dev,
776 struct nfc_target *targets, int n_targets)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300777{
Samuel Ortizc4fbb652012-04-10 19:43:09 +0200778 int i;
779
Joe Perches20c239c2011-11-29 11:37:33 -0800780 pr_debug("dev_name=%s n_targets=%d\n", dev_name(&dev->dev), n_targets);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300781
Samuel Ortizc4fbb652012-04-10 19:43:09 +0200782 for (i = 0; i < n_targets; i++)
Eric Lapuyade01ae0ee2012-04-10 19:43:10 +0200783 targets[i].idx = dev->target_next_idx++;
Samuel Ortizc4fbb652012-04-10 19:43:09 +0200784
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200785 device_lock(&dev->dev);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300786
Eric Lapuyade8668fdd2012-05-03 16:21:58 +0200787 if (dev->polling == false) {
788 device_unlock(&dev->dev);
789 return 0;
790 }
791
792 dev->polling = false;
793
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300794 dev->targets_generation++;
795
796 kfree(dev->targets);
Eric Lapuyaded94f9c52012-05-03 16:33:32 +0200797 dev->targets = NULL;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300798
Eric Lapuyaded94f9c52012-05-03 16:33:32 +0200799 if (targets) {
800 dev->targets = kmemdup(targets,
801 n_targets * sizeof(struct nfc_target),
802 GFP_ATOMIC);
803
804 if (!dev->targets) {
805 dev->n_targets = 0;
806 device_unlock(&dev->dev);
807 return -ENOMEM;
808 }
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300809 }
810
811 dev->n_targets = n_targets;
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200812 device_unlock(&dev->dev);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300813
814 nfc_genl_targets_found(dev);
815
816 return 0;
817}
818EXPORT_SYMBOL(nfc_targets_found);
819
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200820/**
821 * nfc_target_lost - inform that an activated target went out of field
822 *
823 * @dev: The nfc device that had the activated target in field
824 * @target_idx: the nfc index of the target
825 *
826 * The device driver must call this function when the activated target
827 * goes out of the field.
828 * IMPORTANT: this function must not be called from an atomic context.
829 * In addition, it must also not be called from a context that would prevent
830 * the NFC Core to call other nfc ops entry point concurrently.
831 */
Eric Lapuyadee1da0ef2012-04-10 19:43:05 +0200832int nfc_target_lost(struct nfc_dev *dev, u32 target_idx)
833{
834 struct nfc_target *tg;
835 int i;
836
837 pr_debug("dev_name %s n_target %d\n", dev_name(&dev->dev), target_idx);
838
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200839 device_lock(&dev->dev);
Eric Lapuyadee1da0ef2012-04-10 19:43:05 +0200840
841 for (i = 0; i < dev->n_targets; i++) {
842 tg = &dev->targets[i];
843 if (tg->idx == target_idx)
844 break;
845 }
846
847 if (i == dev->n_targets) {
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200848 device_unlock(&dev->dev);
Eric Lapuyadee1da0ef2012-04-10 19:43:05 +0200849 return -EINVAL;
850 }
851
852 dev->targets_generation++;
853 dev->n_targets--;
Eric Lapuyade90099432012-05-07 12:31:13 +0200854 dev->active_target = NULL;
Eric Lapuyadee1da0ef2012-04-10 19:43:05 +0200855
856 if (dev->n_targets) {
857 memcpy(&dev->targets[i], &dev->targets[i + 1],
858 (dev->n_targets - i) * sizeof(struct nfc_target));
859 } else {
860 kfree(dev->targets);
861 dev->targets = NULL;
862 }
863
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200864 device_unlock(&dev->dev);
Eric Lapuyadee1da0ef2012-04-10 19:43:05 +0200865
866 nfc_genl_target_lost(dev, target_idx);
867
868 return 0;
869}
870EXPORT_SYMBOL(nfc_target_lost);
871
Eric Lapuyade9eb334a2012-06-11 15:52:38 +0200872inline void nfc_driver_failure(struct nfc_dev *dev, int err)
Eric Lapuyade456411c2012-06-11 13:49:51 +0200873{
Eric Lapuyade9eb334a2012-06-11 15:52:38 +0200874 nfc_targets_found(dev, NULL, 0);
Eric Lapuyade456411c2012-06-11 13:49:51 +0200875}
876EXPORT_SYMBOL(nfc_driver_failure);
877
Samuel Ortizfed7c252013-05-10 15:28:38 +0200878int nfc_add_se(struct nfc_dev *dev, u32 se_idx, u16 type)
879{
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200880 struct nfc_se *se;
Samuel Ortiz2757c3722013-05-10 15:47:37 +0200881 int rc;
Samuel Ortizfed7c252013-05-10 15:28:38 +0200882
883 pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
884
Arron Wangd8eb18e2013-08-23 16:02:08 +0800885 se = nfc_find_se(dev, se_idx);
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200886 if (se)
887 return -EALREADY;
Samuel Ortizfed7c252013-05-10 15:28:38 +0200888
889 se = kzalloc(sizeof(struct nfc_se), GFP_KERNEL);
890 if (!se)
891 return -ENOMEM;
892
893 se->idx = se_idx;
894 se->type = type;
895 se->state = NFC_SE_DISABLED;
896 INIT_LIST_HEAD(&se->list);
897
898 list_add(&se->list, &dev->secure_elements);
899
Samuel Ortiz2757c3722013-05-10 15:47:37 +0200900 rc = nfc_genl_se_added(dev, se_idx, type);
901 if (rc < 0) {
902 list_del(&se->list);
903 kfree(se);
904
905 return rc;
906 }
907
Samuel Ortizfed7c252013-05-10 15:28:38 +0200908 return 0;
909}
910EXPORT_SYMBOL(nfc_add_se);
911
912int nfc_remove_se(struct nfc_dev *dev, u32 se_idx)
913{
914 struct nfc_se *se, *n;
Samuel Ortiz2757c3722013-05-10 15:47:37 +0200915 int rc;
Samuel Ortizfed7c252013-05-10 15:28:38 +0200916
917 pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
918
919 list_for_each_entry_safe(se, n, &dev->secure_elements, list)
920 if (se->idx == se_idx) {
Samuel Ortiz2757c3722013-05-10 15:47:37 +0200921 rc = nfc_genl_se_removed(dev, se_idx);
922 if (rc < 0)
923 return rc;
924
Samuel Ortizfed7c252013-05-10 15:28:38 +0200925 list_del(&se->list);
926 kfree(se);
927
928 return 0;
929 }
930
931 return -EINVAL;
932}
933EXPORT_SYMBOL(nfc_remove_se);
934
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300935static void nfc_release(struct device *d)
936{
937 struct nfc_dev *dev = to_nfc_dev(d);
Samuel Ortizee656e92013-05-10 15:53:29 +0200938 struct nfc_se *se, *n;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300939
Joe Perches20c239c2011-11-29 11:37:33 -0800940 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300941
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300942 nfc_genl_data_exit(&dev->genl_data);
943 kfree(dev->targets);
Samuel Ortizee656e92013-05-10 15:53:29 +0200944
945 list_for_each_entry_safe(se, n, &dev->secure_elements, list) {
946 nfc_genl_se_removed(dev, se->idx);
947 list_del(&se->list);
948 kfree(se);
949 }
950
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300951 kfree(dev);
952}
953
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200954static void nfc_check_pres_work(struct work_struct *work)
955{
956 struct nfc_dev *dev = container_of(work, struct nfc_dev,
957 check_pres_work);
958 int rc;
959
960 device_lock(&dev->dev);
961
Eric Lapuyade90099432012-05-07 12:31:13 +0200962 if (dev->active_target && timer_pending(&dev->check_pres_timer) == 0) {
963 rc = dev->ops->check_presence(dev, dev->active_target);
Eric Lapuyade632c0162012-10-02 17:27:36 +0200964 if (rc == -EOPNOTSUPP)
965 goto exit;
Eric Lapuyadef0c91032012-11-26 18:06:27 +0100966 if (rc) {
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200967 u32 active_target_idx = dev->active_target->idx;
968 device_unlock(&dev->dev);
969 nfc_target_lost(dev, active_target_idx);
970 return;
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200971 }
Eric Lapuyadef0c91032012-11-26 18:06:27 +0100972
973 if (!dev->shutting_down)
974 mod_timer(&dev->check_pres_timer, jiffies +
975 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200976 }
977
Eric Lapuyade632c0162012-10-02 17:27:36 +0200978exit:
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200979 device_unlock(&dev->dev);
980}
981
982static void nfc_check_pres_timeout(unsigned long data)
983{
984 struct nfc_dev *dev = (struct nfc_dev *)data;
985
Linus Torvalds916082b2012-10-02 16:01:31 -0700986 schedule_work(&dev->check_pres_work);
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200987}
988
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300989struct class nfc_class = {
990 .name = "nfc",
991 .dev_release = nfc_release,
992};
993EXPORT_SYMBOL(nfc_class);
994
Michał Mirosław9f3b7952013-02-01 20:40:17 +0100995static int match_idx(struct device *d, const void *data)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300996{
997 struct nfc_dev *dev = to_nfc_dev(d);
Michał Mirosław9f3b7952013-02-01 20:40:17 +0100998 const unsigned int *idx = data;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300999
1000 return dev->idx == *idx;
1001}
1002
Eric Dumazet95c96172012-04-15 05:58:06 +00001003struct nfc_dev *nfc_get_device(unsigned int idx)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001004{
1005 struct device *d;
1006
1007 d = class_find_device(&nfc_class, NULL, &idx, match_idx);
1008 if (!d)
1009 return NULL;
1010
1011 return to_nfc_dev(d);
1012}
1013
1014/**
1015 * nfc_allocate_device - allocate a new nfc device
1016 *
1017 * @ops: device operations
1018 * @supported_protocols: NFC protocols supported by the device
1019 */
1020struct nfc_dev *nfc_allocate_device(struct nfc_ops *ops,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +01001021 u32 supported_protocols,
1022 int tx_headroom, int tx_tailroom)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001023{
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001024 struct nfc_dev *dev;
1025
1026 if (!ops->start_poll || !ops->stop_poll || !ops->activate_target ||
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02001027 !ops->deactivate_target || !ops->im_transceive)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001028 return NULL;
1029
1030 if (!supported_protocols)
1031 return NULL;
1032
1033 dev = kzalloc(sizeof(struct nfc_dev), GFP_KERNEL);
1034 if (!dev)
1035 return NULL;
1036
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001037 dev->ops = ops;
1038 dev->supported_protocols = supported_protocols;
Samuel Ortize8753042011-08-19 15:47:11 +02001039 dev->tx_headroom = tx_headroom;
1040 dev->tx_tailroom = tx_tailroom;
Samuel Ortizfed7c252013-05-10 15:28:38 +02001041 INIT_LIST_HEAD(&dev->secure_elements);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001042
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001043 nfc_genl_data_init(&dev->genl_data);
1044
Thierry Escande5bcf0992012-10-05 11:05:45 +02001045 dev->rf_mode = NFC_RF_NONE;
Eric Lapuyaded4ccb132012-05-07 12:31:15 +02001046
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001047 /* first generation must not be 0 */
1048 dev->targets_generation = 1;
1049
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +02001050 if (ops->check_presence) {
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +02001051 init_timer(&dev->check_pres_timer);
1052 dev->check_pres_timer.data = (unsigned long)dev;
1053 dev->check_pres_timer.function = nfc_check_pres_timeout;
1054
1055 INIT_WORK(&dev->check_pres_work, nfc_check_pres_work);
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +02001056 }
1057
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001058 return dev;
1059}
1060EXPORT_SYMBOL(nfc_allocate_device);
1061
1062/**
1063 * nfc_register_device - register a nfc device in the nfc subsystem
1064 *
1065 * @dev: The nfc device to register
1066 */
1067int nfc_register_device(struct nfc_dev *dev)
1068{
1069 int rc;
1070
Joe Perches20c239c2011-11-29 11:37:33 -08001071 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001072
Samuel Ortiz7eda8b8e92012-10-22 15:57:58 +02001073 dev->idx = ida_simple_get(&nfc_index_ida, 0, 0, GFP_KERNEL);
1074 if (dev->idx < 0)
1075 return dev->idx;
1076
1077 dev->dev.class = &nfc_class;
1078 dev_set_name(&dev->dev, "nfc%d", dev->idx);
1079 device_initialize(&dev->dev);
1080
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001081 mutex_lock(&nfc_devlist_mutex);
1082 nfc_devlist_generation++;
1083 rc = device_add(&dev->dev);
1084 mutex_unlock(&nfc_devlist_mutex);
1085
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001086 if (rc < 0)
1087 return rc;
1088
Samuel Ortizd6469602011-12-14 16:43:12 +01001089 rc = nfc_llcp_register_device(dev);
1090 if (rc)
1091 pr_err("Could not register llcp device\n");
1092
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001093 rc = nfc_genl_device_added(dev);
1094 if (rc)
Joe Perches20c239c2011-11-29 11:37:33 -08001095 pr_debug("The userspace won't be notified that the device %s was added\n",
1096 dev_name(&dev->dev));
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001097
Samuel Ortizbe055b22013-04-11 11:52:20 +02001098 dev->rfkill = rfkill_alloc(dev_name(&dev->dev), &dev->dev,
1099 RFKILL_TYPE_NFC, &nfc_rfkill_ops, dev);
1100 if (dev->rfkill) {
1101 if (rfkill_register(dev->rfkill) < 0) {
1102 rfkill_destroy(dev->rfkill);
1103 dev->rfkill = NULL;
1104 }
1105 }
1106
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001107 return 0;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001108}
1109EXPORT_SYMBOL(nfc_register_device);
1110
1111/**
1112 * nfc_unregister_device - unregister a nfc device in the nfc subsystem
1113 *
1114 * @dev: The nfc device to unregister
1115 */
1116void nfc_unregister_device(struct nfc_dev *dev)
1117{
Samuel Ortiz7eda8b8e92012-10-22 15:57:58 +02001118 int rc, id;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001119
Joe Perches20c239c2011-11-29 11:37:33 -08001120 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001121
Samuel Ortiz7eda8b8e92012-10-22 15:57:58 +02001122 id = dev->idx;
1123
Samuel Ortizbe055b22013-04-11 11:52:20 +02001124 if (dev->rfkill) {
1125 rfkill_unregister(dev->rfkill);
1126 rfkill_destroy(dev->rfkill);
1127 }
1128
Eric Lapuyadef0c91032012-11-26 18:06:27 +01001129 if (dev->ops->check_presence) {
1130 device_lock(&dev->dev);
1131 dev->shutting_down = true;
1132 device_unlock(&dev->dev);
1133 del_timer_sync(&dev->check_pres_timer);
1134 cancel_work_sync(&dev->check_pres_work);
1135 }
Samuel Ortizd6469602011-12-14 16:43:12 +01001136
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001137 rc = nfc_genl_device_removed(dev);
1138 if (rc)
Eric Lapuyadef0c91032012-11-26 18:06:27 +01001139 pr_debug("The userspace won't be notified that the device %s "
1140 "was removed\n", dev_name(&dev->dev));
1141
1142 nfc_llcp_unregister_device(dev);
1143
1144 mutex_lock(&nfc_devlist_mutex);
1145 nfc_devlist_generation++;
1146 device_del(&dev->dev);
1147 mutex_unlock(&nfc_devlist_mutex);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001148
Samuel Ortiz7eda8b8e92012-10-22 15:57:58 +02001149 ida_simple_remove(&nfc_index_ida, id);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001150}
1151EXPORT_SYMBOL(nfc_unregister_device);
1152
1153static int __init nfc_init(void)
1154{
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001155 int rc;
1156
Joe Perchesed1e0ad2011-11-29 11:37:32 -08001157 pr_info("NFC Core ver %s\n", VERSION);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001158
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001159 rc = class_register(&nfc_class);
1160 if (rc)
1161 return rc;
1162
1163 rc = nfc_genl_init();
1164 if (rc)
1165 goto err_genl;
1166
1167 /* the first generation must not be 0 */
1168 nfc_devlist_generation = 1;
1169
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -03001170 rc = rawsock_init();
1171 if (rc)
1172 goto err_rawsock;
1173
Samuel Ortizd6469602011-12-14 16:43:12 +01001174 rc = nfc_llcp_init();
1175 if (rc)
1176 goto err_llcp_sock;
1177
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -03001178 rc = af_nfc_init();
1179 if (rc)
1180 goto err_af_nfc;
1181
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001182 return 0;
1183
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -03001184err_af_nfc:
Samuel Ortizd6469602011-12-14 16:43:12 +01001185 nfc_llcp_exit();
1186err_llcp_sock:
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -03001187 rawsock_exit();
1188err_rawsock:
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -03001189 nfc_genl_exit();
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001190err_genl:
1191 class_unregister(&nfc_class);
1192 return rc;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001193}
1194
1195static void __exit nfc_exit(void)
1196{
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -03001197 af_nfc_exit();
Samuel Ortizd6469602011-12-14 16:43:12 +01001198 nfc_llcp_exit();
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -03001199 rawsock_exit();
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001200 nfc_genl_exit();
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001201 class_unregister(&nfc_class);
1202}
1203
1204subsys_initcall(nfc_init);
1205module_exit(nfc_exit);
1206
1207MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
1208MODULE_DESCRIPTION("NFC Core ver " VERSION);
1209MODULE_VERSION(VERSION);
1210MODULE_LICENSE("GPL");
Samuel Ortiz1155bb62012-06-12 00:35:50 +02001211MODULE_ALIAS_NETPROTO(PF_NFC);
Samuel Ortiz5df16ca2012-06-12 16:54:16 +02001212MODULE_ALIAS_GENL_FAMILY(NFC_GENL_NAME);