blob: 02ab34132157066d9a0def95276b7abe3f8d7f71 [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 */
136 if (dev->ops->discover_se) {
137 rc = dev->ops->discover_se(dev);
Samuel Ortiz369f4d52013-07-24 14:49:22 +0200138 if (rc)
Samuel Ortiz0a946302013-05-10 11:57:06 +0200139 pr_warn("SE discovery failed\n");
140 }
141
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300142error:
143 device_unlock(&dev->dev);
144 return rc;
145}
146
147/**
148 * nfc_dev_down - turn off the NFC device
149 *
150 * @dev: The nfc device to be turned off
151 */
152int nfc_dev_down(struct nfc_dev *dev)
153{
154 int rc = 0;
155
Joe Perches20c239c2011-11-29 11:37:33 -0800156 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300157
158 device_lock(&dev->dev);
159
160 if (!device_is_registered(&dev->dev)) {
161 rc = -ENODEV;
162 goto error;
163 }
164
165 if (!dev->dev_up) {
166 rc = -EALREADY;
167 goto error;
168 }
169
Eric Lapuyade90099432012-05-07 12:31:13 +0200170 if (dev->polling || dev->active_target) {
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300171 rc = -EBUSY;
172 goto error;
173 }
174
175 if (dev->ops->dev_down)
176 dev->ops->dev_down(dev);
177
178 dev->dev_up = false;
179
180error:
181 device_unlock(&dev->dev);
182 return rc;
183}
184
Samuel Ortizbe055b22013-04-11 11:52:20 +0200185static int nfc_rfkill_set_block(void *data, bool blocked)
186{
187 struct nfc_dev *dev = data;
188
189 pr_debug("%s blocked %d", dev_name(&dev->dev), blocked);
190
191 if (!blocked)
192 return 0;
193
194 nfc_dev_down(dev);
195
196 return 0;
197}
198
199static const struct rfkill_ops nfc_rfkill_ops = {
200 .set_block = nfc_rfkill_set_block,
201};
202
Ilan Elias8b3fe7b2011-09-18 11:19:33 +0300203/**
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300204 * nfc_start_poll - start polling for nfc targets
205 *
206 * @dev: The nfc device that must start polling
207 * @protocols: bitset of nfc protocols that must be used for polling
208 *
209 * The device remains polling for targets until a target is found or
210 * the nfc_stop_poll function is called.
211 */
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200212int nfc_start_poll(struct nfc_dev *dev, u32 im_protocols, u32 tm_protocols)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300213{
214 int rc;
215
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200216 pr_debug("dev_name %s initiator protocols 0x%x target protocols 0x%x\n",
217 dev_name(&dev->dev), im_protocols, tm_protocols);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300218
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200219 if (!im_protocols && !tm_protocols)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300220 return -EINVAL;
221
222 device_lock(&dev->dev);
223
224 if (!device_is_registered(&dev->dev)) {
225 rc = -ENODEV;
226 goto error;
227 }
228
Samuel Ortiz7757dc82013-04-10 12:25:30 +0200229 if (!dev->dev_up) {
230 rc = -ENODEV;
231 goto error;
232 }
233
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300234 if (dev->polling) {
235 rc = -EBUSY;
236 goto error;
237 }
238
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200239 rc = dev->ops->start_poll(dev, im_protocols, tm_protocols);
Samuel Ortizf212ad52012-05-31 00:02:26 +0200240 if (!rc) {
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300241 dev->polling = true;
Samuel Ortizf212ad52012-05-31 00:02:26 +0200242 dev->rf_mode = NFC_RF_NONE;
243 }
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300244
245error:
246 device_unlock(&dev->dev);
247 return rc;
248}
249
250/**
251 * nfc_stop_poll - stop polling for nfc targets
252 *
253 * @dev: The nfc device that must stop polling
254 */
255int nfc_stop_poll(struct nfc_dev *dev)
256{
257 int rc = 0;
258
Joe Perches20c239c2011-11-29 11:37:33 -0800259 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300260
261 device_lock(&dev->dev);
262
263 if (!device_is_registered(&dev->dev)) {
264 rc = -ENODEV;
265 goto error;
266 }
267
268 if (!dev->polling) {
269 rc = -EINVAL;
270 goto error;
271 }
272
273 dev->ops->stop_poll(dev);
274 dev->polling = false;
Thierry Escande5bcf0992012-10-05 11:05:45 +0200275 dev->rf_mode = NFC_RF_NONE;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300276
277error:
278 device_unlock(&dev->dev);
279 return rc;
280}
281
Eric Lapuyade90099432012-05-07 12:31:13 +0200282static struct nfc_target *nfc_find_target(struct nfc_dev *dev, u32 target_idx)
283{
284 int i;
285
286 if (dev->n_targets == 0)
287 return NULL;
288
Szymon Janc0f450772012-10-17 15:23:39 +0200289 for (i = 0; i < dev->n_targets; i++) {
Eric Lapuyade90099432012-05-07 12:31:13 +0200290 if (dev->targets[i].idx == target_idx)
291 return &dev->targets[i];
292 }
293
294 return NULL;
295}
296
Samuel Ortiz47807d32012-03-05 01:03:50 +0100297int nfc_dep_link_up(struct nfc_dev *dev, int target_index, u8 comm_mode)
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100298{
299 int rc = 0;
Samuel Ortiz47807d32012-03-05 01:03:50 +0100300 u8 *gb;
301 size_t gb_len;
Eric Lapuyade90099432012-05-07 12:31:13 +0200302 struct nfc_target *target;
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100303
Samuel Ortiz47807d32012-03-05 01:03:50 +0100304 pr_debug("dev_name=%s comm %d\n", dev_name(&dev->dev), comm_mode);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100305
306 if (!dev->ops->dep_link_up)
307 return -EOPNOTSUPP;
308
309 device_lock(&dev->dev);
310
311 if (!device_is_registered(&dev->dev)) {
312 rc = -ENODEV;
313 goto error;
314 }
315
316 if (dev->dep_link_up == true) {
317 rc = -EALREADY;
318 goto error;
319 }
320
Samuel Ortiz47807d32012-03-05 01:03:50 +0100321 gb = nfc_llcp_general_bytes(dev, &gb_len);
322 if (gb_len > NFC_MAX_GT_LEN) {
323 rc = -EINVAL;
324 goto error;
325 }
326
Eric Lapuyade90099432012-05-07 12:31:13 +0200327 target = nfc_find_target(dev, target_index);
328 if (target == NULL) {
329 rc = -ENOTCONN;
330 goto error;
331 }
332
333 rc = dev->ops->dep_link_up(dev, target, comm_mode, gb, gb_len);
Samuel Ortizf212ad52012-05-31 00:02:26 +0200334 if (!rc) {
Eric Lapuyade90099432012-05-07 12:31:13 +0200335 dev->active_target = target;
Samuel Ortizf212ad52012-05-31 00:02:26 +0200336 dev->rf_mode = NFC_RF_INITIATOR;
337 }
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100338
339error:
340 device_unlock(&dev->dev);
341 return rc;
342}
343
344int nfc_dep_link_down(struct nfc_dev *dev)
345{
346 int rc = 0;
347
348 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
349
350 if (!dev->ops->dep_link_down)
351 return -EOPNOTSUPP;
352
353 device_lock(&dev->dev);
354
355 if (!device_is_registered(&dev->dev)) {
356 rc = -ENODEV;
357 goto error;
358 }
359
360 if (dev->dep_link_up == false) {
361 rc = -EALREADY;
362 goto error;
363 }
364
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100365 rc = dev->ops->dep_link_down(dev);
366 if (!rc) {
367 dev->dep_link_up = false;
Eric Lapuyade90099432012-05-07 12:31:13 +0200368 dev->active_target = NULL;
Thierry Escande5bcf0992012-10-05 11:05:45 +0200369 dev->rf_mode = NFC_RF_NONE;
Samuel Ortizd6469602011-12-14 16:43:12 +0100370 nfc_llcp_mac_is_down(dev);
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100371 nfc_genl_dep_link_down_event(dev);
372 }
373
374error:
375 device_unlock(&dev->dev);
Thierry Escande5bcf0992012-10-05 11:05:45 +0200376
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100377 return rc;
378}
379
380int nfc_dep_link_is_up(struct nfc_dev *dev, u32 target_idx,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100381 u8 comm_mode, u8 rf_mode)
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100382{
383 dev->dep_link_up = true;
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100384
Samuel Ortize29a9e22013-08-21 14:46:20 +0200385 if (!dev->active_target) {
386 struct nfc_target *target;
387
388 target = nfc_find_target(dev, target_idx);
389 if (target == NULL)
390 return -ENOTCONN;
391
392 dev->active_target = target;
393 }
394
395 dev->polling = false;
396 dev->rf_mode = rf_mode;
397
Samuel Ortizd6469602011-12-14 16:43:12 +0100398 nfc_llcp_mac_is_up(dev, target_idx, comm_mode, rf_mode);
399
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100400 return nfc_genl_dep_link_up_event(dev, target_idx, comm_mode, rf_mode);
401}
402EXPORT_SYMBOL(nfc_dep_link_is_up);
403
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300404/**
405 * nfc_activate_target - prepare the target for data exchange
406 *
407 * @dev: The nfc device that found the target
408 * @target_idx: index of the target that must be activated
409 * @protocol: nfc protocol that will be used for data exchange
410 */
411int nfc_activate_target(struct nfc_dev *dev, u32 target_idx, u32 protocol)
412{
413 int rc;
Eric Lapuyade90099432012-05-07 12:31:13 +0200414 struct nfc_target *target;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300415
Joe Perches20c239c2011-11-29 11:37:33 -0800416 pr_debug("dev_name=%s target_idx=%u protocol=%u\n",
417 dev_name(&dev->dev), target_idx, protocol);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300418
419 device_lock(&dev->dev);
420
421 if (!device_is_registered(&dev->dev)) {
422 rc = -ENODEV;
423 goto error;
424 }
425
Eric Lapuyade90099432012-05-07 12:31:13 +0200426 if (dev->active_target) {
427 rc = -EBUSY;
428 goto error;
429 }
430
431 target = nfc_find_target(dev, target_idx);
432 if (target == NULL) {
433 rc = -ENOTCONN;
434 goto error;
435 }
436
437 rc = dev->ops->activate_target(dev, target, protocol);
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200438 if (!rc) {
Eric Lapuyade90099432012-05-07 12:31:13 +0200439 dev->active_target = target;
Samuel Ortizf212ad52012-05-31 00:02:26 +0200440 dev->rf_mode = NFC_RF_INITIATOR;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300441
Eric Lapuyadef0c91032012-11-26 18:06:27 +0100442 if (dev->ops->check_presence && !dev->shutting_down)
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200443 mod_timer(&dev->check_pres_timer, jiffies +
444 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
445 }
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300446
447error:
448 device_unlock(&dev->dev);
449 return rc;
450}
451
452/**
453 * nfc_deactivate_target - deactivate a nfc target
454 *
455 * @dev: The nfc device that found the target
456 * @target_idx: index of the target that must be deactivated
457 */
458int nfc_deactivate_target(struct nfc_dev *dev, u32 target_idx)
459{
460 int rc = 0;
461
Joe Perches20c239c2011-11-29 11:37:33 -0800462 pr_debug("dev_name=%s target_idx=%u\n",
463 dev_name(&dev->dev), target_idx);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300464
465 device_lock(&dev->dev);
466
467 if (!device_is_registered(&dev->dev)) {
468 rc = -ENODEV;
469 goto error;
470 }
471
Eric Lapuyade90099432012-05-07 12:31:13 +0200472 if (dev->active_target == NULL) {
473 rc = -ENOTCONN;
474 goto error;
475 }
476
477 if (dev->active_target->idx != target_idx) {
478 rc = -ENOTCONN;
479 goto error;
480 }
481
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200482 if (dev->ops->check_presence)
483 del_timer_sync(&dev->check_pres_timer);
484
Eric Lapuyade90099432012-05-07 12:31:13 +0200485 dev->ops->deactivate_target(dev, dev->active_target);
486 dev->active_target = NULL;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300487
488error:
489 device_unlock(&dev->dev);
490 return rc;
491}
492
493/**
494 * nfc_data_exchange - transceive data
495 *
496 * @dev: The nfc device that found the target
497 * @target_idx: index of the target
498 * @skb: data to be sent
499 * @cb: callback called when the response is received
500 * @cb_context: parameter for the callback function
501 *
502 * The user must wait for the callback before calling this function again.
503 */
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100504int nfc_data_exchange(struct nfc_dev *dev, u32 target_idx, struct sk_buff *skb,
505 data_exchange_cb_t cb, void *cb_context)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300506{
507 int rc;
508
Joe Perches20c239c2011-11-29 11:37:33 -0800509 pr_debug("dev_name=%s target_idx=%u skb->len=%u\n",
510 dev_name(&dev->dev), target_idx, skb->len);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300511
512 device_lock(&dev->dev);
513
514 if (!device_is_registered(&dev->dev)) {
515 rc = -ENODEV;
516 kfree_skb(skb);
517 goto error;
518 }
519
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +0200520 if (dev->rf_mode == NFC_RF_INITIATOR && dev->active_target != NULL) {
521 if (dev->active_target->idx != target_idx) {
522 rc = -EADDRNOTAVAIL;
523 kfree_skb(skb);
524 goto error;
525 }
526
527 if (dev->ops->check_presence)
528 del_timer_sync(&dev->check_pres_timer);
529
530 rc = dev->ops->im_transceive(dev, dev->active_target, skb, cb,
531 cb_context);
532
Eric Lapuyadef0c91032012-11-26 18:06:27 +0100533 if (!rc && dev->ops->check_presence && !dev->shutting_down)
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +0200534 mod_timer(&dev->check_pres_timer, jiffies +
535 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
536 } else if (dev->rf_mode == NFC_RF_TARGET && dev->ops->tm_send != NULL) {
537 rc = dev->ops->tm_send(dev, skb);
538 } else {
Eric Lapuyade144612c2012-04-10 19:43:11 +0200539 rc = -ENOTCONN;
540 kfree_skb(skb);
541 goto error;
542 }
543
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200544
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300545error:
546 device_unlock(&dev->dev);
547 return rc;
548}
549
Arron Wangd8eb18e2013-08-23 16:02:08 +0800550struct nfc_se *nfc_find_se(struct nfc_dev *dev, u32 se_idx)
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200551{
552 struct nfc_se *se, *n;
553
554 list_for_each_entry_safe(se, n, &dev->secure_elements, list)
555 if (se->idx == se_idx)
556 return se;
557
558 return NULL;
559}
Arron Wangd8eb18e2013-08-23 16:02:08 +0800560EXPORT_SYMBOL(nfc_find_se);
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200561
562int nfc_enable_se(struct nfc_dev *dev, u32 se_idx)
563{
564
565 struct nfc_se *se;
566 int rc;
567
568 pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
569
570 device_lock(&dev->dev);
571
572 if (!device_is_registered(&dev->dev)) {
573 rc = -ENODEV;
574 goto error;
575 }
576
577 if (!dev->dev_up) {
578 rc = -ENODEV;
579 goto error;
580 }
581
582 if (dev->polling) {
583 rc = -EBUSY;
584 goto error;
585 }
586
587 if (!dev->ops->enable_se || !dev->ops->disable_se) {
588 rc = -EOPNOTSUPP;
589 goto error;
590 }
591
Arron Wangd8eb18e2013-08-23 16:02:08 +0800592 se = nfc_find_se(dev, se_idx);
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200593 if (!se) {
594 rc = -EINVAL;
595 goto error;
596 }
597
Arron Wang2c383282013-07-30 14:35:35 +0200598 if (se->state == NFC_SE_ENABLED) {
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200599 rc = -EALREADY;
600 goto error;
601 }
602
603 rc = dev->ops->enable_se(dev, se_idx);
Arron Wang39525ee12013-07-30 14:40:05 +0200604 if (rc >= 0)
605 se->state = NFC_SE_ENABLED;
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200606
607error:
608 device_unlock(&dev->dev);
609 return rc;
610}
611
612int nfc_disable_se(struct nfc_dev *dev, u32 se_idx)
613{
614
615 struct nfc_se *se;
616 int rc;
617
618 pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
619
620 device_lock(&dev->dev);
621
622 if (!device_is_registered(&dev->dev)) {
623 rc = -ENODEV;
624 goto error;
625 }
626
627 if (!dev->dev_up) {
628 rc = -ENODEV;
629 goto error;
630 }
631
632 if (!dev->ops->enable_se || !dev->ops->disable_se) {
633 rc = -EOPNOTSUPP;
634 goto error;
635 }
636
Arron Wangd8eb18e2013-08-23 16:02:08 +0800637 se = nfc_find_se(dev, se_idx);
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200638 if (!se) {
639 rc = -EINVAL;
640 goto error;
641 }
642
Arron Wang2c383282013-07-30 14:35:35 +0200643 if (se->state == NFC_SE_DISABLED) {
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200644 rc = -EALREADY;
645 goto error;
646 }
647
648 rc = dev->ops->disable_se(dev, se_idx);
Arron Wang39525ee12013-07-30 14:40:05 +0200649 if (rc >= 0)
650 se->state = NFC_SE_DISABLED;
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200651
652error:
653 device_unlock(&dev->dev);
654 return rc;
655}
656
Samuel Ortiz541d9202011-12-14 16:43:10 +0100657int nfc_set_remote_general_bytes(struct nfc_dev *dev, u8 *gb, u8 gb_len)
658{
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100659 pr_debug("dev_name=%s gb_len=%d\n", dev_name(&dev->dev), gb_len);
Samuel Ortiz541d9202011-12-14 16:43:10 +0100660
661 if (gb_len > NFC_MAX_GT_LEN)
662 return -EINVAL;
663
Samuel Ortizd6469602011-12-14 16:43:12 +0100664 return nfc_llcp_set_remote_gb(dev, gb, gb_len);
Samuel Ortiz541d9202011-12-14 16:43:10 +0100665}
666EXPORT_SYMBOL(nfc_set_remote_general_bytes);
667
Samuel Ortizab73b752012-04-10 12:51:52 +0200668u8 *nfc_get_local_general_bytes(struct nfc_dev *dev, size_t *gb_len)
669{
670 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
671
672 return nfc_llcp_general_bytes(dev, gb_len);
673}
674EXPORT_SYMBOL(nfc_get_local_general_bytes);
675
Samuel Ortiz73167ce2012-05-31 00:05:50 +0200676int nfc_tm_data_received(struct nfc_dev *dev, struct sk_buff *skb)
677{
678 /* Only LLCP target mode for now */
679 if (dev->dep_link_up == false) {
680 kfree_skb(skb);
681 return -ENOLINK;
682 }
683
684 return nfc_llcp_data_received(dev, skb);
685}
686EXPORT_SYMBOL(nfc_tm_data_received);
687
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200688int nfc_tm_activated(struct nfc_dev *dev, u32 protocol, u8 comm_mode,
689 u8 *gb, size_t gb_len)
690{
691 int rc;
692
693 device_lock(&dev->dev);
694
695 dev->polling = false;
696
697 if (gb != NULL) {
698 rc = nfc_set_remote_general_bytes(dev, gb, gb_len);
699 if (rc < 0)
700 goto out;
701 }
702
Samuel Ortizf212ad52012-05-31 00:02:26 +0200703 dev->rf_mode = NFC_RF_TARGET;
704
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200705 if (protocol == NFC_PROTO_NFC_DEP_MASK)
706 nfc_dep_link_is_up(dev, 0, comm_mode, NFC_RF_TARGET);
707
708 rc = nfc_genl_tm_activated(dev, protocol);
709
710out:
711 device_unlock(&dev->dev);
712
713 return rc;
714}
715EXPORT_SYMBOL(nfc_tm_activated);
716
717int nfc_tm_deactivated(struct nfc_dev *dev)
718{
719 dev->dep_link_up = false;
Thierry Escande5bcf0992012-10-05 11:05:45 +0200720 dev->rf_mode = NFC_RF_NONE;
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200721
722 return nfc_genl_tm_deactivated(dev);
723}
724EXPORT_SYMBOL(nfc_tm_deactivated);
725
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300726/**
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +0100727 * nfc_alloc_send_skb - allocate a skb for data exchange responses
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300728 *
729 * @size: size to allocate
730 * @gfp: gfp flags
731 */
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +0100732struct sk_buff *nfc_alloc_send_skb(struct nfc_dev *dev, struct sock *sk,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100733 unsigned int flags, unsigned int size,
734 unsigned int *err)
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +0100735{
736 struct sk_buff *skb;
737 unsigned int total_size;
738
739 total_size = size +
740 dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE;
741
742 skb = sock_alloc_send_skb(sk, total_size, flags & MSG_DONTWAIT, err);
743 if (skb)
744 skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE);
745
746 return skb;
747}
748
749/**
750 * nfc_alloc_recv_skb - allocate a skb for data exchange responses
751 *
752 * @size: size to allocate
753 * @gfp: gfp flags
754 */
755struct sk_buff *nfc_alloc_recv_skb(unsigned int size, gfp_t gfp)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300756{
757 struct sk_buff *skb;
758 unsigned int total_size;
759
760 total_size = size + 1;
761 skb = alloc_skb(total_size, gfp);
762
763 if (skb)
764 skb_reserve(skb, 1);
765
766 return skb;
767}
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +0100768EXPORT_SYMBOL(nfc_alloc_recv_skb);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300769
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300770/**
771 * nfc_targets_found - inform that targets were found
772 *
773 * @dev: The nfc device that found the targets
774 * @targets: array of nfc targets found
775 * @ntargets: targets array size
776 *
777 * The device driver must call this function when one or many nfc targets
778 * are found. After calling this function, the device driver must stop
779 * polling for targets.
Eric Lapuyaded94f9c52012-05-03 16:33:32 +0200780 * NOTE: This function can be called with targets=NULL and n_targets=0 to
781 * notify a driver error, meaning that the polling operation cannot complete.
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200782 * IMPORTANT: this function must not be called from an atomic context.
783 * In addition, it must also not be called from a context that would prevent
784 * the NFC Core to call other nfc ops entry point concurrently.
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300785 */
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100786int nfc_targets_found(struct nfc_dev *dev,
787 struct nfc_target *targets, int n_targets)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300788{
Samuel Ortizc4fbb652012-04-10 19:43:09 +0200789 int i;
790
Joe Perches20c239c2011-11-29 11:37:33 -0800791 pr_debug("dev_name=%s n_targets=%d\n", dev_name(&dev->dev), n_targets);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300792
Samuel Ortizc4fbb652012-04-10 19:43:09 +0200793 for (i = 0; i < n_targets; i++)
Eric Lapuyade01ae0ee2012-04-10 19:43:10 +0200794 targets[i].idx = dev->target_next_idx++;
Samuel Ortizc4fbb652012-04-10 19:43:09 +0200795
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200796 device_lock(&dev->dev);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300797
Eric Lapuyade8668fdd2012-05-03 16:21:58 +0200798 if (dev->polling == false) {
799 device_unlock(&dev->dev);
800 return 0;
801 }
802
803 dev->polling = false;
804
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300805 dev->targets_generation++;
806
807 kfree(dev->targets);
Eric Lapuyaded94f9c52012-05-03 16:33:32 +0200808 dev->targets = NULL;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300809
Eric Lapuyaded94f9c52012-05-03 16:33:32 +0200810 if (targets) {
811 dev->targets = kmemdup(targets,
812 n_targets * sizeof(struct nfc_target),
813 GFP_ATOMIC);
814
815 if (!dev->targets) {
816 dev->n_targets = 0;
817 device_unlock(&dev->dev);
818 return -ENOMEM;
819 }
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300820 }
821
822 dev->n_targets = n_targets;
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200823 device_unlock(&dev->dev);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300824
825 nfc_genl_targets_found(dev);
826
827 return 0;
828}
829EXPORT_SYMBOL(nfc_targets_found);
830
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200831/**
832 * nfc_target_lost - inform that an activated target went out of field
833 *
834 * @dev: The nfc device that had the activated target in field
835 * @target_idx: the nfc index of the target
836 *
837 * The device driver must call this function when the activated target
838 * goes out of the field.
839 * IMPORTANT: this function must not be called from an atomic context.
840 * In addition, it must also not be called from a context that would prevent
841 * the NFC Core to call other nfc ops entry point concurrently.
842 */
Eric Lapuyadee1da0ef2012-04-10 19:43:05 +0200843int nfc_target_lost(struct nfc_dev *dev, u32 target_idx)
844{
845 struct nfc_target *tg;
846 int i;
847
848 pr_debug("dev_name %s n_target %d\n", dev_name(&dev->dev), target_idx);
849
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200850 device_lock(&dev->dev);
Eric Lapuyadee1da0ef2012-04-10 19:43:05 +0200851
852 for (i = 0; i < dev->n_targets; i++) {
853 tg = &dev->targets[i];
854 if (tg->idx == target_idx)
855 break;
856 }
857
858 if (i == dev->n_targets) {
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200859 device_unlock(&dev->dev);
Eric Lapuyadee1da0ef2012-04-10 19:43:05 +0200860 return -EINVAL;
861 }
862
863 dev->targets_generation++;
864 dev->n_targets--;
Eric Lapuyade90099432012-05-07 12:31:13 +0200865 dev->active_target = NULL;
Eric Lapuyadee1da0ef2012-04-10 19:43:05 +0200866
867 if (dev->n_targets) {
868 memcpy(&dev->targets[i], &dev->targets[i + 1],
869 (dev->n_targets - i) * sizeof(struct nfc_target));
870 } else {
871 kfree(dev->targets);
872 dev->targets = NULL;
873 }
874
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200875 device_unlock(&dev->dev);
Eric Lapuyadee1da0ef2012-04-10 19:43:05 +0200876
877 nfc_genl_target_lost(dev, target_idx);
878
879 return 0;
880}
881EXPORT_SYMBOL(nfc_target_lost);
882
Eric Lapuyade9eb334a2012-06-11 15:52:38 +0200883inline void nfc_driver_failure(struct nfc_dev *dev, int err)
Eric Lapuyade456411c2012-06-11 13:49:51 +0200884{
Eric Lapuyade9eb334a2012-06-11 15:52:38 +0200885 nfc_targets_found(dev, NULL, 0);
Eric Lapuyade456411c2012-06-11 13:49:51 +0200886}
887EXPORT_SYMBOL(nfc_driver_failure);
888
Samuel Ortizfed7c252013-05-10 15:28:38 +0200889int nfc_add_se(struct nfc_dev *dev, u32 se_idx, u16 type)
890{
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200891 struct nfc_se *se;
Samuel Ortiz2757c3722013-05-10 15:47:37 +0200892 int rc;
Samuel Ortizfed7c252013-05-10 15:28:38 +0200893
894 pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
895
Arron Wangd8eb18e2013-08-23 16:02:08 +0800896 se = nfc_find_se(dev, se_idx);
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200897 if (se)
898 return -EALREADY;
Samuel Ortizfed7c252013-05-10 15:28:38 +0200899
900 se = kzalloc(sizeof(struct nfc_se), GFP_KERNEL);
901 if (!se)
902 return -ENOMEM;
903
904 se->idx = se_idx;
905 se->type = type;
906 se->state = NFC_SE_DISABLED;
907 INIT_LIST_HEAD(&se->list);
908
909 list_add(&se->list, &dev->secure_elements);
910
Samuel Ortiz2757c3722013-05-10 15:47:37 +0200911 rc = nfc_genl_se_added(dev, se_idx, type);
912 if (rc < 0) {
913 list_del(&se->list);
914 kfree(se);
915
916 return rc;
917 }
918
Samuel Ortizfed7c252013-05-10 15:28:38 +0200919 return 0;
920}
921EXPORT_SYMBOL(nfc_add_se);
922
923int nfc_remove_se(struct nfc_dev *dev, u32 se_idx)
924{
925 struct nfc_se *se, *n;
Samuel Ortiz2757c3722013-05-10 15:47:37 +0200926 int rc;
Samuel Ortizfed7c252013-05-10 15:28:38 +0200927
928 pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
929
930 list_for_each_entry_safe(se, n, &dev->secure_elements, list)
931 if (se->idx == se_idx) {
Samuel Ortiz2757c3722013-05-10 15:47:37 +0200932 rc = nfc_genl_se_removed(dev, se_idx);
933 if (rc < 0)
934 return rc;
935
Samuel Ortizfed7c252013-05-10 15:28:38 +0200936 list_del(&se->list);
937 kfree(se);
938
939 return 0;
940 }
941
942 return -EINVAL;
943}
944EXPORT_SYMBOL(nfc_remove_se);
945
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300946static void nfc_release(struct device *d)
947{
948 struct nfc_dev *dev = to_nfc_dev(d);
Samuel Ortizee656e92013-05-10 15:53:29 +0200949 struct nfc_se *se, *n;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300950
Joe Perches20c239c2011-11-29 11:37:33 -0800951 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300952
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300953 nfc_genl_data_exit(&dev->genl_data);
954 kfree(dev->targets);
Samuel Ortizee656e92013-05-10 15:53:29 +0200955
956 list_for_each_entry_safe(se, n, &dev->secure_elements, list) {
957 nfc_genl_se_removed(dev, se->idx);
958 list_del(&se->list);
959 kfree(se);
960 }
961
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300962 kfree(dev);
963}
964
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200965static void nfc_check_pres_work(struct work_struct *work)
966{
967 struct nfc_dev *dev = container_of(work, struct nfc_dev,
968 check_pres_work);
969 int rc;
970
971 device_lock(&dev->dev);
972
Eric Lapuyade90099432012-05-07 12:31:13 +0200973 if (dev->active_target && timer_pending(&dev->check_pres_timer) == 0) {
974 rc = dev->ops->check_presence(dev, dev->active_target);
Eric Lapuyade632c0162012-10-02 17:27:36 +0200975 if (rc == -EOPNOTSUPP)
976 goto exit;
Eric Lapuyadef0c91032012-11-26 18:06:27 +0100977 if (rc) {
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200978 u32 active_target_idx = dev->active_target->idx;
979 device_unlock(&dev->dev);
980 nfc_target_lost(dev, active_target_idx);
981 return;
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200982 }
Eric Lapuyadef0c91032012-11-26 18:06:27 +0100983
984 if (!dev->shutting_down)
985 mod_timer(&dev->check_pres_timer, jiffies +
986 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200987 }
988
Eric Lapuyade632c0162012-10-02 17:27:36 +0200989exit:
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200990 device_unlock(&dev->dev);
991}
992
993static void nfc_check_pres_timeout(unsigned long data)
994{
995 struct nfc_dev *dev = (struct nfc_dev *)data;
996
Linus Torvalds916082b2012-10-02 16:01:31 -0700997 schedule_work(&dev->check_pres_work);
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200998}
999
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001000struct class nfc_class = {
1001 .name = "nfc",
1002 .dev_release = nfc_release,
1003};
1004EXPORT_SYMBOL(nfc_class);
1005
Michał Mirosław9f3b7952013-02-01 20:40:17 +01001006static int match_idx(struct device *d, const void *data)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001007{
1008 struct nfc_dev *dev = to_nfc_dev(d);
Michał Mirosław9f3b7952013-02-01 20:40:17 +01001009 const unsigned int *idx = data;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001010
1011 return dev->idx == *idx;
1012}
1013
Eric Dumazet95c96172012-04-15 05:58:06 +00001014struct nfc_dev *nfc_get_device(unsigned int idx)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001015{
1016 struct device *d;
1017
1018 d = class_find_device(&nfc_class, NULL, &idx, match_idx);
1019 if (!d)
1020 return NULL;
1021
1022 return to_nfc_dev(d);
1023}
1024
1025/**
1026 * nfc_allocate_device - allocate a new nfc device
1027 *
1028 * @ops: device operations
1029 * @supported_protocols: NFC protocols supported by the device
1030 */
1031struct nfc_dev *nfc_allocate_device(struct nfc_ops *ops,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +01001032 u32 supported_protocols,
1033 int tx_headroom, int tx_tailroom)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001034{
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001035 struct nfc_dev *dev;
1036
1037 if (!ops->start_poll || !ops->stop_poll || !ops->activate_target ||
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02001038 !ops->deactivate_target || !ops->im_transceive)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001039 return NULL;
1040
1041 if (!supported_protocols)
1042 return NULL;
1043
1044 dev = kzalloc(sizeof(struct nfc_dev), GFP_KERNEL);
1045 if (!dev)
1046 return NULL;
1047
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001048 dev->ops = ops;
1049 dev->supported_protocols = supported_protocols;
Samuel Ortize8753042011-08-19 15:47:11 +02001050 dev->tx_headroom = tx_headroom;
1051 dev->tx_tailroom = tx_tailroom;
Samuel Ortizfed7c252013-05-10 15:28:38 +02001052 INIT_LIST_HEAD(&dev->secure_elements);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001053
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001054 nfc_genl_data_init(&dev->genl_data);
1055
Thierry Escande5bcf0992012-10-05 11:05:45 +02001056 dev->rf_mode = NFC_RF_NONE;
Eric Lapuyaded4ccb132012-05-07 12:31:15 +02001057
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001058 /* first generation must not be 0 */
1059 dev->targets_generation = 1;
1060
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +02001061 if (ops->check_presence) {
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +02001062 init_timer(&dev->check_pres_timer);
1063 dev->check_pres_timer.data = (unsigned long)dev;
1064 dev->check_pres_timer.function = nfc_check_pres_timeout;
1065
1066 INIT_WORK(&dev->check_pres_work, nfc_check_pres_work);
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +02001067 }
1068
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001069 return dev;
1070}
1071EXPORT_SYMBOL(nfc_allocate_device);
1072
1073/**
1074 * nfc_register_device - register a nfc device in the nfc subsystem
1075 *
1076 * @dev: The nfc device to register
1077 */
1078int nfc_register_device(struct nfc_dev *dev)
1079{
1080 int rc;
1081
Joe Perches20c239c2011-11-29 11:37:33 -08001082 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001083
Samuel Ortiz7eda8b8e92012-10-22 15:57:58 +02001084 dev->idx = ida_simple_get(&nfc_index_ida, 0, 0, GFP_KERNEL);
1085 if (dev->idx < 0)
1086 return dev->idx;
1087
1088 dev->dev.class = &nfc_class;
1089 dev_set_name(&dev->dev, "nfc%d", dev->idx);
1090 device_initialize(&dev->dev);
1091
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001092 mutex_lock(&nfc_devlist_mutex);
1093 nfc_devlist_generation++;
1094 rc = device_add(&dev->dev);
1095 mutex_unlock(&nfc_devlist_mutex);
1096
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001097 if (rc < 0)
1098 return rc;
1099
Samuel Ortizd6469602011-12-14 16:43:12 +01001100 rc = nfc_llcp_register_device(dev);
1101 if (rc)
1102 pr_err("Could not register llcp device\n");
1103
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001104 rc = nfc_genl_device_added(dev);
1105 if (rc)
Joe Perches20c239c2011-11-29 11:37:33 -08001106 pr_debug("The userspace won't be notified that the device %s was added\n",
1107 dev_name(&dev->dev));
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001108
Samuel Ortizbe055b22013-04-11 11:52:20 +02001109 dev->rfkill = rfkill_alloc(dev_name(&dev->dev), &dev->dev,
1110 RFKILL_TYPE_NFC, &nfc_rfkill_ops, dev);
1111 if (dev->rfkill) {
1112 if (rfkill_register(dev->rfkill) < 0) {
1113 rfkill_destroy(dev->rfkill);
1114 dev->rfkill = NULL;
1115 }
1116 }
1117
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001118 return 0;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001119}
1120EXPORT_SYMBOL(nfc_register_device);
1121
1122/**
1123 * nfc_unregister_device - unregister a nfc device in the nfc subsystem
1124 *
1125 * @dev: The nfc device to unregister
1126 */
1127void nfc_unregister_device(struct nfc_dev *dev)
1128{
Samuel Ortiz7eda8b8e92012-10-22 15:57:58 +02001129 int rc, id;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001130
Joe Perches20c239c2011-11-29 11:37:33 -08001131 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001132
Samuel Ortiz7eda8b8e92012-10-22 15:57:58 +02001133 id = dev->idx;
1134
Samuel Ortizbe055b22013-04-11 11:52:20 +02001135 if (dev->rfkill) {
1136 rfkill_unregister(dev->rfkill);
1137 rfkill_destroy(dev->rfkill);
1138 }
1139
Eric Lapuyadef0c91032012-11-26 18:06:27 +01001140 if (dev->ops->check_presence) {
1141 device_lock(&dev->dev);
1142 dev->shutting_down = true;
1143 device_unlock(&dev->dev);
1144 del_timer_sync(&dev->check_pres_timer);
1145 cancel_work_sync(&dev->check_pres_work);
1146 }
Samuel Ortizd6469602011-12-14 16:43:12 +01001147
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001148 rc = nfc_genl_device_removed(dev);
1149 if (rc)
Eric Lapuyadef0c91032012-11-26 18:06:27 +01001150 pr_debug("The userspace won't be notified that the device %s "
1151 "was removed\n", dev_name(&dev->dev));
1152
1153 nfc_llcp_unregister_device(dev);
1154
1155 mutex_lock(&nfc_devlist_mutex);
1156 nfc_devlist_generation++;
1157 device_del(&dev->dev);
1158 mutex_unlock(&nfc_devlist_mutex);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001159
Samuel Ortiz7eda8b8e92012-10-22 15:57:58 +02001160 ida_simple_remove(&nfc_index_ida, id);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001161}
1162EXPORT_SYMBOL(nfc_unregister_device);
1163
1164static int __init nfc_init(void)
1165{
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001166 int rc;
1167
Joe Perchesed1e0ad2011-11-29 11:37:32 -08001168 pr_info("NFC Core ver %s\n", VERSION);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001169
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001170 rc = class_register(&nfc_class);
1171 if (rc)
1172 return rc;
1173
1174 rc = nfc_genl_init();
1175 if (rc)
1176 goto err_genl;
1177
1178 /* the first generation must not be 0 */
1179 nfc_devlist_generation = 1;
1180
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -03001181 rc = rawsock_init();
1182 if (rc)
1183 goto err_rawsock;
1184
Samuel Ortizd6469602011-12-14 16:43:12 +01001185 rc = nfc_llcp_init();
1186 if (rc)
1187 goto err_llcp_sock;
1188
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -03001189 rc = af_nfc_init();
1190 if (rc)
1191 goto err_af_nfc;
1192
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001193 return 0;
1194
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -03001195err_af_nfc:
Samuel Ortizd6469602011-12-14 16:43:12 +01001196 nfc_llcp_exit();
1197err_llcp_sock:
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -03001198 rawsock_exit();
1199err_rawsock:
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -03001200 nfc_genl_exit();
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001201err_genl:
1202 class_unregister(&nfc_class);
1203 return rc;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001204}
1205
1206static void __exit nfc_exit(void)
1207{
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -03001208 af_nfc_exit();
Samuel Ortizd6469602011-12-14 16:43:12 +01001209 nfc_llcp_exit();
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -03001210 rawsock_exit();
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001211 nfc_genl_exit();
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001212 class_unregister(&nfc_class);
1213}
1214
1215subsys_initcall(nfc_init);
1216module_exit(nfc_exit);
1217
1218MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
1219MODULE_DESCRIPTION("NFC Core ver " VERSION);
1220MODULE_VERSION(VERSION);
1221MODULE_LICENSE("GPL");
Samuel Ortiz1155bb62012-06-12 00:35:50 +02001222MODULE_ALIAS_NETPROTO(PF_NFC);
Samuel Ortiz5df16ca2012-06-12 16:54:16 +02001223MODULE_ALIAS_GENL_FAMILY(NFC_GENL_NAME);