blob: 872529105abc7c3a2e41b9d579e934e019e8e5ff [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 Ortize29a9e22013-08-21 14:46:20 +0200387 if (!dev->active_target) {
388 struct nfc_target *target;
389
390 target = nfc_find_target(dev, target_idx);
391 if (target == NULL)
392 return -ENOTCONN;
393
394 dev->active_target = target;
395 }
396
397 dev->polling = false;
398 dev->rf_mode = rf_mode;
399
Samuel Ortizd6469602011-12-14 16:43:12 +0100400 nfc_llcp_mac_is_up(dev, target_idx, comm_mode, rf_mode);
401
Samuel Ortiz1ed28f62011-12-14 16:43:09 +0100402 return nfc_genl_dep_link_up_event(dev, target_idx, comm_mode, rf_mode);
403}
404EXPORT_SYMBOL(nfc_dep_link_is_up);
405
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300406/**
407 * nfc_activate_target - prepare the target for data exchange
408 *
409 * @dev: The nfc device that found the target
410 * @target_idx: index of the target that must be activated
411 * @protocol: nfc protocol that will be used for data exchange
412 */
413int nfc_activate_target(struct nfc_dev *dev, u32 target_idx, u32 protocol)
414{
415 int rc;
Eric Lapuyade90099432012-05-07 12:31:13 +0200416 struct nfc_target *target;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300417
Joe Perches20c239c2011-11-29 11:37:33 -0800418 pr_debug("dev_name=%s target_idx=%u protocol=%u\n",
419 dev_name(&dev->dev), target_idx, protocol);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300420
421 device_lock(&dev->dev);
422
423 if (!device_is_registered(&dev->dev)) {
424 rc = -ENODEV;
425 goto error;
426 }
427
Eric Lapuyade90099432012-05-07 12:31:13 +0200428 if (dev->active_target) {
429 rc = -EBUSY;
430 goto error;
431 }
432
433 target = nfc_find_target(dev, target_idx);
434 if (target == NULL) {
435 rc = -ENOTCONN;
436 goto error;
437 }
438
439 rc = dev->ops->activate_target(dev, target, protocol);
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200440 if (!rc) {
Eric Lapuyade90099432012-05-07 12:31:13 +0200441 dev->active_target = target;
Samuel Ortizf212ad52012-05-31 00:02:26 +0200442 dev->rf_mode = NFC_RF_INITIATOR;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300443
Eric Lapuyadef0c91032012-11-26 18:06:27 +0100444 if (dev->ops->check_presence && !dev->shutting_down)
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200445 mod_timer(&dev->check_pres_timer, jiffies +
446 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
447 }
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300448
449error:
450 device_unlock(&dev->dev);
451 return rc;
452}
453
454/**
455 * nfc_deactivate_target - deactivate a nfc target
456 *
457 * @dev: The nfc device that found the target
458 * @target_idx: index of the target that must be deactivated
459 */
460int nfc_deactivate_target(struct nfc_dev *dev, u32 target_idx)
461{
462 int rc = 0;
463
Joe Perches20c239c2011-11-29 11:37:33 -0800464 pr_debug("dev_name=%s target_idx=%u\n",
465 dev_name(&dev->dev), target_idx);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300466
467 device_lock(&dev->dev);
468
469 if (!device_is_registered(&dev->dev)) {
470 rc = -ENODEV;
471 goto error;
472 }
473
Eric Lapuyade90099432012-05-07 12:31:13 +0200474 if (dev->active_target == NULL) {
475 rc = -ENOTCONN;
476 goto error;
477 }
478
479 if (dev->active_target->idx != target_idx) {
480 rc = -ENOTCONN;
481 goto error;
482 }
483
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200484 if (dev->ops->check_presence)
485 del_timer_sync(&dev->check_pres_timer);
486
Eric Lapuyade90099432012-05-07 12:31:13 +0200487 dev->ops->deactivate_target(dev, dev->active_target);
488 dev->active_target = NULL;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300489
490error:
491 device_unlock(&dev->dev);
492 return rc;
493}
494
495/**
496 * nfc_data_exchange - transceive data
497 *
498 * @dev: The nfc device that found the target
499 * @target_idx: index of the target
500 * @skb: data to be sent
501 * @cb: callback called when the response is received
502 * @cb_context: parameter for the callback function
503 *
504 * The user must wait for the callback before calling this function again.
505 */
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100506int nfc_data_exchange(struct nfc_dev *dev, u32 target_idx, struct sk_buff *skb,
507 data_exchange_cb_t cb, void *cb_context)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300508{
509 int rc;
510
Joe Perches20c239c2011-11-29 11:37:33 -0800511 pr_debug("dev_name=%s target_idx=%u skb->len=%u\n",
512 dev_name(&dev->dev), target_idx, skb->len);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300513
514 device_lock(&dev->dev);
515
516 if (!device_is_registered(&dev->dev)) {
517 rc = -ENODEV;
518 kfree_skb(skb);
519 goto error;
520 }
521
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +0200522 if (dev->rf_mode == NFC_RF_INITIATOR && dev->active_target != NULL) {
523 if (dev->active_target->idx != target_idx) {
524 rc = -EADDRNOTAVAIL;
525 kfree_skb(skb);
526 goto error;
527 }
528
529 if (dev->ops->check_presence)
530 del_timer_sync(&dev->check_pres_timer);
531
532 rc = dev->ops->im_transceive(dev, dev->active_target, skb, cb,
533 cb_context);
534
Eric Lapuyadef0c91032012-11-26 18:06:27 +0100535 if (!rc && dev->ops->check_presence && !dev->shutting_down)
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +0200536 mod_timer(&dev->check_pres_timer, jiffies +
537 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
538 } else if (dev->rf_mode == NFC_RF_TARGET && dev->ops->tm_send != NULL) {
539 rc = dev->ops->tm_send(dev, skb);
540 } else {
Eric Lapuyade144612c2012-04-10 19:43:11 +0200541 rc = -ENOTCONN;
542 kfree_skb(skb);
543 goto error;
544 }
545
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200546
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300547error:
548 device_unlock(&dev->dev);
549 return rc;
550}
551
Arron Wangd8eb18e2013-08-23 16:02:08 +0800552struct nfc_se *nfc_find_se(struct nfc_dev *dev, u32 se_idx)
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200553{
554 struct nfc_se *se, *n;
555
556 list_for_each_entry_safe(se, n, &dev->secure_elements, list)
557 if (se->idx == se_idx)
558 return se;
559
560 return NULL;
561}
Arron Wangd8eb18e2013-08-23 16:02:08 +0800562EXPORT_SYMBOL(nfc_find_se);
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200563
564int nfc_enable_se(struct nfc_dev *dev, u32 se_idx)
565{
566
567 struct nfc_se *se;
568 int rc;
569
570 pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
571
572 device_lock(&dev->dev);
573
574 if (!device_is_registered(&dev->dev)) {
575 rc = -ENODEV;
576 goto error;
577 }
578
579 if (!dev->dev_up) {
580 rc = -ENODEV;
581 goto error;
582 }
583
584 if (dev->polling) {
585 rc = -EBUSY;
586 goto error;
587 }
588
589 if (!dev->ops->enable_se || !dev->ops->disable_se) {
590 rc = -EOPNOTSUPP;
591 goto error;
592 }
593
Arron Wangd8eb18e2013-08-23 16:02:08 +0800594 se = nfc_find_se(dev, se_idx);
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200595 if (!se) {
596 rc = -EINVAL;
597 goto error;
598 }
599
Arron Wang2c383282013-07-30 14:35:35 +0200600 if (se->state == NFC_SE_ENABLED) {
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200601 rc = -EALREADY;
602 goto error;
603 }
604
605 rc = dev->ops->enable_se(dev, se_idx);
Arron Wang39525ee12013-07-30 14:40:05 +0200606 if (rc >= 0)
607 se->state = NFC_SE_ENABLED;
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200608
609error:
610 device_unlock(&dev->dev);
611 return rc;
612}
613
614int nfc_disable_se(struct nfc_dev *dev, u32 se_idx)
615{
616
617 struct nfc_se *se;
618 int rc;
619
620 pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
621
622 device_lock(&dev->dev);
623
624 if (!device_is_registered(&dev->dev)) {
625 rc = -ENODEV;
626 goto error;
627 }
628
629 if (!dev->dev_up) {
630 rc = -ENODEV;
631 goto error;
632 }
633
634 if (!dev->ops->enable_se || !dev->ops->disable_se) {
635 rc = -EOPNOTSUPP;
636 goto error;
637 }
638
Arron Wangd8eb18e2013-08-23 16:02:08 +0800639 se = nfc_find_se(dev, se_idx);
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200640 if (!se) {
641 rc = -EINVAL;
642 goto error;
643 }
644
Arron Wang2c383282013-07-30 14:35:35 +0200645 if (se->state == NFC_SE_DISABLED) {
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200646 rc = -EALREADY;
647 goto error;
648 }
649
650 rc = dev->ops->disable_se(dev, se_idx);
Arron Wang39525ee12013-07-30 14:40:05 +0200651 if (rc >= 0)
652 se->state = NFC_SE_DISABLED;
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200653
654error:
655 device_unlock(&dev->dev);
656 return rc;
657}
658
Samuel Ortiz541d9202011-12-14 16:43:10 +0100659int nfc_set_remote_general_bytes(struct nfc_dev *dev, u8 *gb, u8 gb_len)
660{
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100661 pr_debug("dev_name=%s gb_len=%d\n", dev_name(&dev->dev), gb_len);
Samuel Ortiz541d9202011-12-14 16:43:10 +0100662
663 if (gb_len > NFC_MAX_GT_LEN)
664 return -EINVAL;
665
Samuel Ortizd6469602011-12-14 16:43:12 +0100666 return nfc_llcp_set_remote_gb(dev, gb, gb_len);
Samuel Ortiz541d9202011-12-14 16:43:10 +0100667}
668EXPORT_SYMBOL(nfc_set_remote_general_bytes);
669
Samuel Ortizab73b752012-04-10 12:51:52 +0200670u8 *nfc_get_local_general_bytes(struct nfc_dev *dev, size_t *gb_len)
671{
672 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
673
674 return nfc_llcp_general_bytes(dev, gb_len);
675}
676EXPORT_SYMBOL(nfc_get_local_general_bytes);
677
Samuel Ortiz73167ce2012-05-31 00:05:50 +0200678int nfc_tm_data_received(struct nfc_dev *dev, struct sk_buff *skb)
679{
680 /* Only LLCP target mode for now */
681 if (dev->dep_link_up == false) {
682 kfree_skb(skb);
683 return -ENOLINK;
684 }
685
686 return nfc_llcp_data_received(dev, skb);
687}
688EXPORT_SYMBOL(nfc_tm_data_received);
689
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200690int nfc_tm_activated(struct nfc_dev *dev, u32 protocol, u8 comm_mode,
691 u8 *gb, size_t gb_len)
692{
693 int rc;
694
695 device_lock(&dev->dev);
696
697 dev->polling = false;
698
699 if (gb != NULL) {
700 rc = nfc_set_remote_general_bytes(dev, gb, gb_len);
701 if (rc < 0)
702 goto out;
703 }
704
Samuel Ortizf212ad52012-05-31 00:02:26 +0200705 dev->rf_mode = NFC_RF_TARGET;
706
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200707 if (protocol == NFC_PROTO_NFC_DEP_MASK)
708 nfc_dep_link_is_up(dev, 0, comm_mode, NFC_RF_TARGET);
709
710 rc = nfc_genl_tm_activated(dev, protocol);
711
712out:
713 device_unlock(&dev->dev);
714
715 return rc;
716}
717EXPORT_SYMBOL(nfc_tm_activated);
718
719int nfc_tm_deactivated(struct nfc_dev *dev)
720{
721 dev->dep_link_up = false;
Thierry Escande5bcf0992012-10-05 11:05:45 +0200722 dev->rf_mode = NFC_RF_NONE;
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200723
724 return nfc_genl_tm_deactivated(dev);
725}
726EXPORT_SYMBOL(nfc_tm_deactivated);
727
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300728/**
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +0100729 * nfc_alloc_send_skb - allocate a skb for data exchange responses
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300730 *
731 * @size: size to allocate
732 * @gfp: gfp flags
733 */
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +0100734struct sk_buff *nfc_alloc_send_skb(struct nfc_dev *dev, struct sock *sk,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100735 unsigned int flags, unsigned int size,
736 unsigned int *err)
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +0100737{
738 struct sk_buff *skb;
739 unsigned int total_size;
740
741 total_size = size +
742 dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE;
743
744 skb = sock_alloc_send_skb(sk, total_size, flags & MSG_DONTWAIT, err);
745 if (skb)
746 skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE);
747
748 return skb;
749}
750
751/**
752 * nfc_alloc_recv_skb - allocate a skb for data exchange responses
753 *
754 * @size: size to allocate
755 * @gfp: gfp flags
756 */
757struct sk_buff *nfc_alloc_recv_skb(unsigned int size, gfp_t gfp)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300758{
759 struct sk_buff *skb;
760 unsigned int total_size;
761
762 total_size = size + 1;
763 skb = alloc_skb(total_size, gfp);
764
765 if (skb)
766 skb_reserve(skb, 1);
767
768 return skb;
769}
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +0100770EXPORT_SYMBOL(nfc_alloc_recv_skb);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300771
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300772/**
773 * nfc_targets_found - inform that targets were found
774 *
775 * @dev: The nfc device that found the targets
776 * @targets: array of nfc targets found
777 * @ntargets: targets array size
778 *
779 * The device driver must call this function when one or many nfc targets
780 * are found. After calling this function, the device driver must stop
781 * polling for targets.
Eric Lapuyaded94f9c52012-05-03 16:33:32 +0200782 * NOTE: This function can be called with targets=NULL and n_targets=0 to
783 * notify a driver error, meaning that the polling operation cannot complete.
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200784 * IMPORTANT: this function must not be called from an atomic context.
785 * In addition, it must also not be called from a context that would prevent
786 * the NFC Core to call other nfc ops entry point concurrently.
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300787 */
Samuel Ortiz0a40acb2012-03-05 01:03:53 +0100788int nfc_targets_found(struct nfc_dev *dev,
789 struct nfc_target *targets, int n_targets)
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300790{
Samuel Ortizc4fbb652012-04-10 19:43:09 +0200791 int i;
792
Joe Perches20c239c2011-11-29 11:37:33 -0800793 pr_debug("dev_name=%s n_targets=%d\n", dev_name(&dev->dev), n_targets);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300794
Samuel Ortizc4fbb652012-04-10 19:43:09 +0200795 for (i = 0; i < n_targets; i++)
Eric Lapuyade01ae0ee2012-04-10 19:43:10 +0200796 targets[i].idx = dev->target_next_idx++;
Samuel Ortizc4fbb652012-04-10 19:43:09 +0200797
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200798 device_lock(&dev->dev);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300799
Eric Lapuyade8668fdd2012-05-03 16:21:58 +0200800 if (dev->polling == false) {
801 device_unlock(&dev->dev);
802 return 0;
803 }
804
805 dev->polling = false;
806
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300807 dev->targets_generation++;
808
809 kfree(dev->targets);
Eric Lapuyaded94f9c52012-05-03 16:33:32 +0200810 dev->targets = NULL;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300811
Eric Lapuyaded94f9c52012-05-03 16:33:32 +0200812 if (targets) {
813 dev->targets = kmemdup(targets,
814 n_targets * sizeof(struct nfc_target),
815 GFP_ATOMIC);
816
817 if (!dev->targets) {
818 dev->n_targets = 0;
819 device_unlock(&dev->dev);
820 return -ENOMEM;
821 }
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300822 }
823
824 dev->n_targets = n_targets;
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200825 device_unlock(&dev->dev);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300826
827 nfc_genl_targets_found(dev);
828
829 return 0;
830}
831EXPORT_SYMBOL(nfc_targets_found);
832
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200833/**
834 * nfc_target_lost - inform that an activated target went out of field
835 *
836 * @dev: The nfc device that had the activated target in field
837 * @target_idx: the nfc index of the target
838 *
839 * The device driver must call this function when the activated target
840 * goes out of the field.
841 * IMPORTANT: this function must not be called from an atomic context.
842 * In addition, it must also not be called from a context that would prevent
843 * the NFC Core to call other nfc ops entry point concurrently.
844 */
Eric Lapuyadee1da0ef2012-04-10 19:43:05 +0200845int nfc_target_lost(struct nfc_dev *dev, u32 target_idx)
846{
847 struct nfc_target *tg;
848 int i;
849
850 pr_debug("dev_name %s n_target %d\n", dev_name(&dev->dev), target_idx);
851
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200852 device_lock(&dev->dev);
Eric Lapuyadee1da0ef2012-04-10 19:43:05 +0200853
854 for (i = 0; i < dev->n_targets; i++) {
855 tg = &dev->targets[i];
856 if (tg->idx == target_idx)
857 break;
858 }
859
860 if (i == dev->n_targets) {
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200861 device_unlock(&dev->dev);
Eric Lapuyadee1da0ef2012-04-10 19:43:05 +0200862 return -EINVAL;
863 }
864
865 dev->targets_generation++;
866 dev->n_targets--;
Eric Lapuyade90099432012-05-07 12:31:13 +0200867 dev->active_target = NULL;
Eric Lapuyadee1da0ef2012-04-10 19:43:05 +0200868
869 if (dev->n_targets) {
870 memcpy(&dev->targets[i], &dev->targets[i + 1],
871 (dev->n_targets - i) * sizeof(struct nfc_target));
872 } else {
873 kfree(dev->targets);
874 dev->targets = NULL;
875 }
876
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200877 device_unlock(&dev->dev);
Eric Lapuyadee1da0ef2012-04-10 19:43:05 +0200878
879 nfc_genl_target_lost(dev, target_idx);
880
881 return 0;
882}
883EXPORT_SYMBOL(nfc_target_lost);
884
Eric Lapuyade9eb334a2012-06-11 15:52:38 +0200885inline void nfc_driver_failure(struct nfc_dev *dev, int err)
Eric Lapuyade456411c2012-06-11 13:49:51 +0200886{
Eric Lapuyade9eb334a2012-06-11 15:52:38 +0200887 nfc_targets_found(dev, NULL, 0);
Eric Lapuyade456411c2012-06-11 13:49:51 +0200888}
889EXPORT_SYMBOL(nfc_driver_failure);
890
Samuel Ortizfed7c252013-05-10 15:28:38 +0200891int nfc_add_se(struct nfc_dev *dev, u32 se_idx, u16 type)
892{
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200893 struct nfc_se *se;
Samuel Ortiz2757c3722013-05-10 15:47:37 +0200894 int rc;
Samuel Ortizfed7c252013-05-10 15:28:38 +0200895
896 pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
897
Arron Wangd8eb18e2013-08-23 16:02:08 +0800898 se = nfc_find_se(dev, se_idx);
Samuel Ortizc531c9e2013-05-10 16:15:32 +0200899 if (se)
900 return -EALREADY;
Samuel Ortizfed7c252013-05-10 15:28:38 +0200901
902 se = kzalloc(sizeof(struct nfc_se), GFP_KERNEL);
903 if (!se)
904 return -ENOMEM;
905
906 se->idx = se_idx;
907 se->type = type;
908 se->state = NFC_SE_DISABLED;
909 INIT_LIST_HEAD(&se->list);
910
911 list_add(&se->list, &dev->secure_elements);
912
Samuel Ortiz2757c3722013-05-10 15:47:37 +0200913 rc = nfc_genl_se_added(dev, se_idx, type);
914 if (rc < 0) {
915 list_del(&se->list);
916 kfree(se);
917
918 return rc;
919 }
920
Samuel Ortizfed7c252013-05-10 15:28:38 +0200921 return 0;
922}
923EXPORT_SYMBOL(nfc_add_se);
924
925int nfc_remove_se(struct nfc_dev *dev, u32 se_idx)
926{
927 struct nfc_se *se, *n;
Samuel Ortiz2757c3722013-05-10 15:47:37 +0200928 int rc;
Samuel Ortizfed7c252013-05-10 15:28:38 +0200929
930 pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
931
932 list_for_each_entry_safe(se, n, &dev->secure_elements, list)
933 if (se->idx == se_idx) {
Samuel Ortiz2757c3722013-05-10 15:47:37 +0200934 rc = nfc_genl_se_removed(dev, se_idx);
935 if (rc < 0)
936 return rc;
937
Samuel Ortizfed7c252013-05-10 15:28:38 +0200938 list_del(&se->list);
939 kfree(se);
940
941 return 0;
942 }
943
944 return -EINVAL;
945}
946EXPORT_SYMBOL(nfc_remove_se);
947
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300948static void nfc_release(struct device *d)
949{
950 struct nfc_dev *dev = to_nfc_dev(d);
Samuel Ortizee656e92013-05-10 15:53:29 +0200951 struct nfc_se *se, *n;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300952
Joe Perches20c239c2011-11-29 11:37:33 -0800953 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300954
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -0300955 nfc_genl_data_exit(&dev->genl_data);
956 kfree(dev->targets);
Samuel Ortizee656e92013-05-10 15:53:29 +0200957
958 list_for_each_entry_safe(se, n, &dev->secure_elements, list) {
959 nfc_genl_se_removed(dev, se->idx);
960 list_del(&se->list);
961 kfree(se);
962 }
963
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -0300964 kfree(dev);
965}
966
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200967static void nfc_check_pres_work(struct work_struct *work)
968{
969 struct nfc_dev *dev = container_of(work, struct nfc_dev,
970 check_pres_work);
971 int rc;
972
973 device_lock(&dev->dev);
974
Eric Lapuyade90099432012-05-07 12:31:13 +0200975 if (dev->active_target && timer_pending(&dev->check_pres_timer) == 0) {
976 rc = dev->ops->check_presence(dev, dev->active_target);
Eric Lapuyade632c0162012-10-02 17:27:36 +0200977 if (rc == -EOPNOTSUPP)
978 goto exit;
Eric Lapuyadef0c91032012-11-26 18:06:27 +0100979 if (rc) {
Eric Lapuyaded4ccb132012-05-07 12:31:15 +0200980 u32 active_target_idx = dev->active_target->idx;
981 device_unlock(&dev->dev);
982 nfc_target_lost(dev, active_target_idx);
983 return;
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200984 }
Eric Lapuyadef0c91032012-11-26 18:06:27 +0100985
986 if (!dev->shutting_down)
987 mod_timer(&dev->check_pres_timer, jiffies +
988 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200989 }
990
Eric Lapuyade632c0162012-10-02 17:27:36 +0200991exit:
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +0200992 device_unlock(&dev->dev);
993}
994
995static void nfc_check_pres_timeout(unsigned long data)
996{
997 struct nfc_dev *dev = (struct nfc_dev *)data;
998
Linus Torvalds916082b2012-10-02 16:01:31 -0700999 schedule_work(&dev->check_pres_work);
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +02001000}
1001
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001002struct class nfc_class = {
1003 .name = "nfc",
1004 .dev_release = nfc_release,
1005};
1006EXPORT_SYMBOL(nfc_class);
1007
Michał Mirosław9f3b7952013-02-01 20:40:17 +01001008static int match_idx(struct device *d, const void *data)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001009{
1010 struct nfc_dev *dev = to_nfc_dev(d);
Michał Mirosław9f3b7952013-02-01 20:40:17 +01001011 const unsigned int *idx = data;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001012
1013 return dev->idx == *idx;
1014}
1015
Eric Dumazet95c96172012-04-15 05:58:06 +00001016struct nfc_dev *nfc_get_device(unsigned int idx)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001017{
1018 struct device *d;
1019
1020 d = class_find_device(&nfc_class, NULL, &idx, match_idx);
1021 if (!d)
1022 return NULL;
1023
1024 return to_nfc_dev(d);
1025}
1026
1027/**
1028 * nfc_allocate_device - allocate a new nfc device
1029 *
1030 * @ops: device operations
1031 * @supported_protocols: NFC protocols supported by the device
1032 */
1033struct nfc_dev *nfc_allocate_device(struct nfc_ops *ops,
Samuel Ortiz0a40acb2012-03-05 01:03:53 +01001034 u32 supported_protocols,
1035 int tx_headroom, int tx_tailroom)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001036{
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001037 struct nfc_dev *dev;
1038
1039 if (!ops->start_poll || !ops->stop_poll || !ops->activate_target ||
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02001040 !ops->deactivate_target || !ops->im_transceive)
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001041 return NULL;
1042
1043 if (!supported_protocols)
1044 return NULL;
1045
1046 dev = kzalloc(sizeof(struct nfc_dev), GFP_KERNEL);
1047 if (!dev)
1048 return NULL;
1049
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001050 dev->ops = ops;
1051 dev->supported_protocols = supported_protocols;
Samuel Ortize8753042011-08-19 15:47:11 +02001052 dev->tx_headroom = tx_headroom;
1053 dev->tx_tailroom = tx_tailroom;
Samuel Ortizfed7c252013-05-10 15:28:38 +02001054 INIT_LIST_HEAD(&dev->secure_elements);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001055
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001056 nfc_genl_data_init(&dev->genl_data);
1057
Thierry Escande5bcf0992012-10-05 11:05:45 +02001058 dev->rf_mode = NFC_RF_NONE;
Eric Lapuyaded4ccb132012-05-07 12:31:15 +02001059
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001060 /* first generation must not be 0 */
1061 dev->targets_generation = 1;
1062
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +02001063 if (ops->check_presence) {
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +02001064 init_timer(&dev->check_pres_timer);
1065 dev->check_pres_timer.data = (unsigned long)dev;
1066 dev->check_pres_timer.function = nfc_check_pres_timeout;
1067
1068 INIT_WORK(&dev->check_pres_work, nfc_check_pres_work);
Eric Lapuyadec8d56ae2012-04-10 19:43:12 +02001069 }
1070
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001071 return dev;
1072}
1073EXPORT_SYMBOL(nfc_allocate_device);
1074
1075/**
1076 * nfc_register_device - register a nfc device in the nfc subsystem
1077 *
1078 * @dev: The nfc device to register
1079 */
1080int nfc_register_device(struct nfc_dev *dev)
1081{
1082 int rc;
1083
Joe Perches20c239c2011-11-29 11:37:33 -08001084 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001085
Samuel Ortiz7eda8b8e92012-10-22 15:57:58 +02001086 dev->idx = ida_simple_get(&nfc_index_ida, 0, 0, GFP_KERNEL);
1087 if (dev->idx < 0)
1088 return dev->idx;
1089
1090 dev->dev.class = &nfc_class;
1091 dev_set_name(&dev->dev, "nfc%d", dev->idx);
1092 device_initialize(&dev->dev);
1093
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001094 mutex_lock(&nfc_devlist_mutex);
1095 nfc_devlist_generation++;
1096 rc = device_add(&dev->dev);
1097 mutex_unlock(&nfc_devlist_mutex);
1098
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001099 if (rc < 0)
1100 return rc;
1101
Samuel Ortizd6469602011-12-14 16:43:12 +01001102 rc = nfc_llcp_register_device(dev);
1103 if (rc)
1104 pr_err("Could not register llcp device\n");
1105
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001106 rc = nfc_genl_device_added(dev);
1107 if (rc)
Joe Perches20c239c2011-11-29 11:37:33 -08001108 pr_debug("The userspace won't be notified that the device %s was added\n",
1109 dev_name(&dev->dev));
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001110
Samuel Ortizbe055b22013-04-11 11:52:20 +02001111 dev->rfkill = rfkill_alloc(dev_name(&dev->dev), &dev->dev,
1112 RFKILL_TYPE_NFC, &nfc_rfkill_ops, dev);
1113 if (dev->rfkill) {
1114 if (rfkill_register(dev->rfkill) < 0) {
1115 rfkill_destroy(dev->rfkill);
1116 dev->rfkill = NULL;
1117 }
1118 }
1119
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001120 return 0;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001121}
1122EXPORT_SYMBOL(nfc_register_device);
1123
1124/**
1125 * nfc_unregister_device - unregister a nfc device in the nfc subsystem
1126 *
1127 * @dev: The nfc device to unregister
1128 */
1129void nfc_unregister_device(struct nfc_dev *dev)
1130{
Samuel Ortiz7eda8b8e92012-10-22 15:57:58 +02001131 int rc, id;
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001132
Joe Perches20c239c2011-11-29 11:37:33 -08001133 pr_debug("dev_name=%s\n", dev_name(&dev->dev));
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001134
Samuel Ortiz7eda8b8e92012-10-22 15:57:58 +02001135 id = dev->idx;
1136
Samuel Ortizbe055b22013-04-11 11:52:20 +02001137 if (dev->rfkill) {
1138 rfkill_unregister(dev->rfkill);
1139 rfkill_destroy(dev->rfkill);
1140 }
1141
Eric Lapuyadef0c91032012-11-26 18:06:27 +01001142 if (dev->ops->check_presence) {
1143 device_lock(&dev->dev);
1144 dev->shutting_down = true;
1145 device_unlock(&dev->dev);
1146 del_timer_sync(&dev->check_pres_timer);
1147 cancel_work_sync(&dev->check_pres_work);
1148 }
Samuel Ortizd6469602011-12-14 16:43:12 +01001149
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001150 rc = nfc_genl_device_removed(dev);
1151 if (rc)
Eric Lapuyadef0c91032012-11-26 18:06:27 +01001152 pr_debug("The userspace won't be notified that the device %s "
1153 "was removed\n", dev_name(&dev->dev));
1154
1155 nfc_llcp_unregister_device(dev);
1156
1157 mutex_lock(&nfc_devlist_mutex);
1158 nfc_devlist_generation++;
1159 device_del(&dev->dev);
1160 mutex_unlock(&nfc_devlist_mutex);
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001161
Samuel Ortiz7eda8b8e92012-10-22 15:57:58 +02001162 ida_simple_remove(&nfc_index_ida, id);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001163}
1164EXPORT_SYMBOL(nfc_unregister_device);
1165
1166static int __init nfc_init(void)
1167{
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001168 int rc;
1169
Joe Perchesed1e0ad2011-11-29 11:37:32 -08001170 pr_info("NFC Core ver %s\n", VERSION);
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001171
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001172 rc = class_register(&nfc_class);
1173 if (rc)
1174 return rc;
1175
1176 rc = nfc_genl_init();
1177 if (rc)
1178 goto err_genl;
1179
1180 /* the first generation must not be 0 */
1181 nfc_devlist_generation = 1;
1182
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -03001183 rc = rawsock_init();
1184 if (rc)
1185 goto err_rawsock;
1186
Samuel Ortizd6469602011-12-14 16:43:12 +01001187 rc = nfc_llcp_init();
1188 if (rc)
1189 goto err_llcp_sock;
1190
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -03001191 rc = af_nfc_init();
1192 if (rc)
1193 goto err_af_nfc;
1194
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001195 return 0;
1196
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -03001197err_af_nfc:
Samuel Ortizd6469602011-12-14 16:43:12 +01001198 nfc_llcp_exit();
1199err_llcp_sock:
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -03001200 rawsock_exit();
1201err_rawsock:
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -03001202 nfc_genl_exit();
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001203err_genl:
1204 class_unregister(&nfc_class);
1205 return rc;
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001206}
1207
1208static void __exit nfc_exit(void)
1209{
Aloisio Almeida Jrc7fe3b52011-07-01 19:31:35 -03001210 af_nfc_exit();
Samuel Ortizd6469602011-12-14 16:43:12 +01001211 nfc_llcp_exit();
Lauro Ramos Venancio23b78692011-07-01 19:31:36 -03001212 rawsock_exit();
Lauro Ramos Venancio4d12b8b2011-07-01 19:31:34 -03001213 nfc_genl_exit();
Lauro Ramos Venancio3e256b82011-07-01 19:31:33 -03001214 class_unregister(&nfc_class);
1215}
1216
1217subsys_initcall(nfc_init);
1218module_exit(nfc_exit);
1219
1220MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
1221MODULE_DESCRIPTION("NFC Core ver " VERSION);
1222MODULE_VERSION(VERSION);
1223MODULE_LICENSE("GPL");
Samuel Ortiz1155bb62012-06-12 00:35:50 +02001224MODULE_ALIAS_NETPROTO(PF_NFC);
Samuel Ortiz5df16ca2012-06-12 16:54:16 +02001225MODULE_ALIAS_GENL_FAMILY(NFC_GENL_NAME);