aboutsummaryrefslogtreecommitdiffstats
path: root/net/nfc
diff options
context:
space:
mode:
authorTrond Myklebust <Trond.Myklebust@netapp.com>2011-11-02 23:56:40 -0400
committerTrond Myklebust <Trond.Myklebust@netapp.com>2011-11-02 23:56:40 -0400
commit31cbecb4ab538f433145bc5a46f3bea9b9627031 (patch)
treed6206d42dea7298f7ef05fd1f7bf474245f0d43a /net/nfc
parent2b72c9ccd22c4a3299e5a358dcd639fb253730f4 (diff)
parent278c023a99b0d6b471d0f4a79835c703482e29ac (diff)
Merge branch 'osd-devel' into nfs-for-next
Diffstat (limited to 'net/nfc')
-rw-r--r--net/nfc/Kconfig2
-rw-r--r--net/nfc/Makefile1
-rw-r--r--net/nfc/core.c83
-rw-r--r--net/nfc/nci/Kconfig10
-rw-r--r--net/nfc/nci/Makefile7
-rw-r--r--net/nfc/nci/core.c797
-rw-r--r--net/nfc/nci/data.c247
-rw-r--r--net/nfc/nci/lib.c94
-rw-r--r--net/nfc/nci/ntf.c258
-rw-r--r--net/nfc/nci/rsp.c226
-rw-r--r--net/nfc/netlink.c56
-rw-r--r--net/nfc/nfc.h8
-rw-r--r--net/nfc/rawsock.c13
13 files changed, 1792 insertions, 10 deletions
diff --git a/net/nfc/Kconfig b/net/nfc/Kconfig
index 33e095b124b3..58cddadf8e8e 100644
--- a/net/nfc/Kconfig
+++ b/net/nfc/Kconfig
@@ -13,4 +13,6 @@ menuconfig NFC
13 To compile this support as a module, choose M here: the module will 13 To compile this support as a module, choose M here: the module will
14 be called nfc. 14 be called nfc.
15 15
16source "net/nfc/nci/Kconfig"
17
16source "drivers/nfc/Kconfig" 18source "drivers/nfc/Kconfig"
diff --git a/net/nfc/Makefile b/net/nfc/Makefile
index 16250c353851..fbb550f2377b 100644
--- a/net/nfc/Makefile
+++ b/net/nfc/Makefile
@@ -3,5 +3,6 @@
3# 3#
4 4
5obj-$(CONFIG_NFC) += nfc.o 5obj-$(CONFIG_NFC) += nfc.o
6obj-$(CONFIG_NFC_NCI) += nci/
6 7
7nfc-objs := core.o netlink.o af_nfc.o rawsock.o 8nfc-objs := core.o netlink.o af_nfc.o rawsock.o
diff --git a/net/nfc/core.c b/net/nfc/core.c
index b6fd4e1f2057..47e02c1b8c02 100644
--- a/net/nfc/core.c
+++ b/net/nfc/core.c
@@ -53,6 +53,80 @@ int nfc_printk(const char *level, const char *format, ...)
53EXPORT_SYMBOL(nfc_printk); 53EXPORT_SYMBOL(nfc_printk);
54 54
55/** 55/**
56 * nfc_dev_up - turn on the NFC device
57 *
58 * @dev: The nfc device to be turned on
59 *
60 * The device remains up until the nfc_dev_down function is called.
61 */
62int nfc_dev_up(struct nfc_dev *dev)
63{
64 int rc = 0;
65
66 nfc_dbg("dev_name=%s", dev_name(&dev->dev));
67
68 device_lock(&dev->dev);
69
70 if (!device_is_registered(&dev->dev)) {
71 rc = -ENODEV;
72 goto error;
73 }
74
75 if (dev->dev_up) {
76 rc = -EALREADY;
77 goto error;
78 }
79
80 if (dev->ops->dev_up)
81 rc = dev->ops->dev_up(dev);
82
83 if (!rc)
84 dev->dev_up = true;
85
86error:
87 device_unlock(&dev->dev);
88 return rc;
89}
90
91/**
92 * nfc_dev_down - turn off the NFC device
93 *
94 * @dev: The nfc device to be turned off
95 */
96int nfc_dev_down(struct nfc_dev *dev)
97{
98 int rc = 0;
99
100 nfc_dbg("dev_name=%s", dev_name(&dev->dev));
101
102 device_lock(&dev->dev);
103
104 if (!device_is_registered(&dev->dev)) {
105 rc = -ENODEV;
106 goto error;
107 }
108
109 if (!dev->dev_up) {
110 rc = -EALREADY;
111 goto error;
112 }
113
114 if (dev->polling || dev->remote_activated) {
115 rc = -EBUSY;
116 goto error;
117 }
118
119 if (dev->ops->dev_down)
120 dev->ops->dev_down(dev);
121
122 dev->dev_up = false;
123
124error:
125 device_unlock(&dev->dev);
126 return rc;
127}
128
129/**
56 * nfc_start_poll - start polling for nfc targets 130 * nfc_start_poll - start polling for nfc targets
57 * 131 *
58 * @dev: The nfc device that must start polling 132 * @dev: The nfc device that must start polling
@@ -144,6 +218,8 @@ int nfc_activate_target(struct nfc_dev *dev, u32 target_idx, u32 protocol)
144 } 218 }
145 219
146 rc = dev->ops->activate_target(dev, target_idx, protocol); 220 rc = dev->ops->activate_target(dev, target_idx, protocol);
221 if (!rc)
222 dev->remote_activated = true;
147 223
148error: 224error:
149 device_unlock(&dev->dev); 225 device_unlock(&dev->dev);
@@ -170,6 +246,7 @@ int nfc_deactivate_target(struct nfc_dev *dev, u32 target_idx)
170 } 246 }
171 247
172 dev->ops->deactivate_target(dev, target_idx); 248 dev->ops->deactivate_target(dev, target_idx);
249 dev->remote_activated = false;
173 250
174error: 251error:
175 device_unlock(&dev->dev); 252 device_unlock(&dev->dev);
@@ -322,7 +399,9 @@ struct nfc_dev *nfc_get_device(unsigned idx)
322 * @supported_protocols: NFC protocols supported by the device 399 * @supported_protocols: NFC protocols supported by the device
323 */ 400 */
324struct nfc_dev *nfc_allocate_device(struct nfc_ops *ops, 401struct nfc_dev *nfc_allocate_device(struct nfc_ops *ops,
325 u32 supported_protocols) 402 u32 supported_protocols,
403 int tx_headroom,
404 int tx_tailroom)
326{ 405{
327 static atomic_t dev_no = ATOMIC_INIT(0); 406 static atomic_t dev_no = ATOMIC_INIT(0);
328 struct nfc_dev *dev; 407 struct nfc_dev *dev;
@@ -345,6 +424,8 @@ struct nfc_dev *nfc_allocate_device(struct nfc_ops *ops,
345 424
346 dev->ops = ops; 425 dev->ops = ops;
347 dev->supported_protocols = supported_protocols; 426 dev->supported_protocols = supported_protocols;
427 dev->tx_headroom = tx_headroom;
428 dev->tx_tailroom = tx_tailroom;
348 429
349 spin_lock_init(&dev->targets_lock); 430 spin_lock_init(&dev->targets_lock);
350 nfc_genl_data_init(&dev->genl_data); 431 nfc_genl_data_init(&dev->genl_data);
diff --git a/net/nfc/nci/Kconfig b/net/nfc/nci/Kconfig
new file mode 100644
index 000000000000..decdc49b26d8
--- /dev/null
+++ b/net/nfc/nci/Kconfig
@@ -0,0 +1,10 @@
1config NFC_NCI
2 depends on NFC && EXPERIMENTAL
3 tristate "NCI protocol support (EXPERIMENTAL)"
4 default n
5 help
6 NCI (NFC Controller Interface) is a communication protocol between
7 an NFC Controller (NFCC) and a Device Host (DH).
8
9 Say Y here to compile NCI support into the kernel or say M to
10 compile it as module (nci).
diff --git a/net/nfc/nci/Makefile b/net/nfc/nci/Makefile
new file mode 100644
index 000000000000..cdb3a2e44471
--- /dev/null
+++ b/net/nfc/nci/Makefile
@@ -0,0 +1,7 @@
1#
2# Makefile for the Linux NFC NCI layer.
3#
4
5obj-$(CONFIG_NFC_NCI) += nci.o
6
7nci-objs := core.o data.o lib.o ntf.o rsp.o \ No newline at end of file
diff --git a/net/nfc/nci/core.c b/net/nfc/nci/core.c
new file mode 100644
index 000000000000..4047e29acb3b
--- /dev/null
+++ b/net/nfc/nci/core.c
@@ -0,0 +1,797 @@
1/*
2 * The NFC Controller Interface is the communication protocol between an
3 * NFC Controller (NFCC) and a Device Host (DH).
4 *
5 * Copyright (C) 2011 Texas Instruments, Inc.
6 *
7 * Written by Ilan Elias <ilane@ti.com>
8 *
9 * Acknowledgements:
10 * This file is based on hci_core.c, which was written
11 * by Maxim Krasnyansky.
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2
15 * as published by the Free Software Foundation
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 */
27
28#include <linux/types.h>
29#include <linux/workqueue.h>
30#include <linux/completion.h>
31#include <linux/sched.h>
32#include <linux/bitops.h>
33#include <linux/skbuff.h>
34
35#include "../nfc.h"
36#include <net/nfc/nci.h>
37#include <net/nfc/nci_core.h>
38#include <linux/nfc.h>
39
40static void nci_cmd_work(struct work_struct *work);
41static void nci_rx_work(struct work_struct *work);
42static void nci_tx_work(struct work_struct *work);
43
44/* ---- NCI requests ---- */
45
46void nci_req_complete(struct nci_dev *ndev, int result)
47{
48 if (ndev->req_status == NCI_REQ_PEND) {
49 ndev->req_result = result;
50 ndev->req_status = NCI_REQ_DONE;
51 complete(&ndev->req_completion);
52 }
53}
54
55static void nci_req_cancel(struct nci_dev *ndev, int err)
56{
57 if (ndev->req_status == NCI_REQ_PEND) {
58 ndev->req_result = err;
59 ndev->req_status = NCI_REQ_CANCELED;
60 complete(&ndev->req_completion);
61 }
62}
63
64/* Execute request and wait for completion. */
65static int __nci_request(struct nci_dev *ndev,
66 void (*req)(struct nci_dev *ndev, unsigned long opt),
67 unsigned long opt,
68 __u32 timeout)
69{
70 int rc = 0;
71 unsigned long completion_rc;
72
73 ndev->req_status = NCI_REQ_PEND;
74
75 init_completion(&ndev->req_completion);
76 req(ndev, opt);
77 completion_rc = wait_for_completion_interruptible_timeout(
78 &ndev->req_completion,
79 timeout);
80
81 nfc_dbg("wait_for_completion return %ld", completion_rc);
82
83 if (completion_rc > 0) {
84 switch (ndev->req_status) {
85 case NCI_REQ_DONE:
86 rc = nci_to_errno(ndev->req_result);
87 break;
88
89 case NCI_REQ_CANCELED:
90 rc = -ndev->req_result;
91 break;
92
93 default:
94 rc = -ETIMEDOUT;
95 break;
96 }
97 } else {
98 nfc_err("wait_for_completion_interruptible_timeout failed %ld",
99 completion_rc);
100
101 rc = ((completion_rc == 0) ? (-ETIMEDOUT) : (completion_rc));
102 }
103
104 ndev->req_status = ndev->req_result = 0;
105
106 return rc;
107}
108
109static inline int nci_request(struct nci_dev *ndev,
110 void (*req)(struct nci_dev *ndev, unsigned long opt),
111 unsigned long opt, __u32 timeout)
112{
113 int rc;
114
115 if (!test_bit(NCI_UP, &ndev->flags))
116 return -ENETDOWN;
117
118 /* Serialize all requests */
119 mutex_lock(&ndev->req_lock);
120 rc = __nci_request(ndev, req, opt, timeout);
121 mutex_unlock(&ndev->req_lock);
122
123 return rc;
124}
125
126static void nci_reset_req(struct nci_dev *ndev, unsigned long opt)
127{
128 nci_send_cmd(ndev, NCI_OP_CORE_RESET_CMD, 0, NULL);
129}
130
131static void nci_init_req(struct nci_dev *ndev, unsigned long opt)
132{
133 nci_send_cmd(ndev, NCI_OP_CORE_INIT_CMD, 0, NULL);
134}
135
136static void nci_init_complete_req(struct nci_dev *ndev, unsigned long opt)
137{
138 struct nci_core_conn_create_cmd conn_cmd;
139 struct nci_rf_disc_map_cmd cmd;
140 struct disc_map_config *cfg = cmd.mapping_configs;
141 __u8 *num = &cmd.num_mapping_configs;
142 int i;
143
144 /* create static rf connection */
145 conn_cmd.target_handle = 0;
146 conn_cmd.num_target_specific_params = 0;
147 nci_send_cmd(ndev, NCI_OP_CORE_CONN_CREATE_CMD, 2, &conn_cmd);
148
149 /* set rf mapping configurations */
150 *num = 0;
151
152 /* by default mapping is set to NCI_RF_INTERFACE_FRAME */
153 for (i = 0; i < ndev->num_supported_rf_interfaces; i++) {
154 if (ndev->supported_rf_interfaces[i] ==
155 NCI_RF_INTERFACE_ISO_DEP) {
156 cfg[*num].rf_protocol = NCI_RF_PROTOCOL_ISO_DEP;
157 cfg[*num].mode = NCI_DISC_MAP_MODE_BOTH;
158 cfg[*num].rf_interface_type = NCI_RF_INTERFACE_ISO_DEP;
159 (*num)++;
160 } else if (ndev->supported_rf_interfaces[i] ==
161 NCI_RF_INTERFACE_NFC_DEP) {
162 cfg[*num].rf_protocol = NCI_RF_PROTOCOL_NFC_DEP;
163 cfg[*num].mode = NCI_DISC_MAP_MODE_BOTH;
164 cfg[*num].rf_interface_type = NCI_RF_INTERFACE_NFC_DEP;
165 (*num)++;
166 }
167
168 if (*num == NCI_MAX_NUM_MAPPING_CONFIGS)
169 break;
170 }
171
172 nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_MAP_CMD,
173 (1 + ((*num)*sizeof(struct disc_map_config))),
174 &cmd);
175}
176
177static void nci_rf_discover_req(struct nci_dev *ndev, unsigned long opt)
178{
179 struct nci_rf_disc_cmd cmd;
180 __u32 protocols = opt;
181
182 cmd.num_disc_configs = 0;
183
184 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
185 (protocols & NFC_PROTO_JEWEL_MASK
186 || protocols & NFC_PROTO_MIFARE_MASK
187 || protocols & NFC_PROTO_ISO14443_MASK
188 || protocols & NFC_PROTO_NFC_DEP_MASK)) {
189 cmd.disc_configs[cmd.num_disc_configs].type =
190 NCI_DISCOVERY_TYPE_POLL_A_PASSIVE;
191 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
192 cmd.num_disc_configs++;
193 }
194
195 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
196 (protocols & NFC_PROTO_ISO14443_MASK)) {
197 cmd.disc_configs[cmd.num_disc_configs].type =
198 NCI_DISCOVERY_TYPE_POLL_B_PASSIVE;
199 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
200 cmd.num_disc_configs++;
201 }
202
203 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
204 (protocols & NFC_PROTO_FELICA_MASK
205 || protocols & NFC_PROTO_NFC_DEP_MASK)) {
206 cmd.disc_configs[cmd.num_disc_configs].type =
207 NCI_DISCOVERY_TYPE_POLL_F_PASSIVE;
208 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
209 cmd.num_disc_configs++;
210 }
211
212 nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_CMD,
213 (1 + (cmd.num_disc_configs*sizeof(struct disc_config))),
214 &cmd);
215}
216
217static void nci_rf_deactivate_req(struct nci_dev *ndev, unsigned long opt)
218{
219 struct nci_rf_deactivate_cmd cmd;
220
221 cmd.type = NCI_DEACTIVATE_TYPE_IDLE_MODE;
222
223 nci_send_cmd(ndev, NCI_OP_RF_DEACTIVATE_CMD,
224 sizeof(struct nci_rf_deactivate_cmd),
225 &cmd);
226}
227
228static int nci_open_device(struct nci_dev *ndev)
229{
230 int rc = 0;
231
232 mutex_lock(&ndev->req_lock);
233
234 if (test_bit(NCI_UP, &ndev->flags)) {
235 rc = -EALREADY;
236 goto done;
237 }
238
239 if (ndev->ops->open(ndev)) {
240 rc = -EIO;
241 goto done;
242 }
243
244 atomic_set(&ndev->cmd_cnt, 1);
245
246 set_bit(NCI_INIT, &ndev->flags);
247
248 rc = __nci_request(ndev, nci_reset_req, 0,
249 msecs_to_jiffies(NCI_RESET_TIMEOUT));
250
251 if (!rc) {
252 rc = __nci_request(ndev, nci_init_req, 0,
253 msecs_to_jiffies(NCI_INIT_TIMEOUT));
254 }
255
256 if (!rc) {
257 rc = __nci_request(ndev, nci_init_complete_req, 0,
258 msecs_to_jiffies(NCI_INIT_TIMEOUT));
259 }
260
261 clear_bit(NCI_INIT, &ndev->flags);
262
263 if (!rc) {
264 set_bit(NCI_UP, &ndev->flags);
265 } else {
266 /* Init failed, cleanup */
267 skb_queue_purge(&ndev->cmd_q);
268 skb_queue_purge(&ndev->rx_q);
269 skb_queue_purge(&ndev->tx_q);
270
271 ndev->ops->close(ndev);
272 ndev->flags = 0;
273 }
274
275done:
276 mutex_unlock(&ndev->req_lock);
277 return rc;
278}
279
280static int nci_close_device(struct nci_dev *ndev)
281{
282 nci_req_cancel(ndev, ENODEV);
283 mutex_lock(&ndev->req_lock);
284
285 if (!test_and_clear_bit(NCI_UP, &ndev->flags)) {
286 del_timer_sync(&ndev->cmd_timer);
287 mutex_unlock(&ndev->req_lock);
288 return 0;
289 }
290
291 /* Drop RX and TX queues */
292 skb_queue_purge(&ndev->rx_q);
293 skb_queue_purge(&ndev->tx_q);
294
295 /* Flush RX and TX wq */
296 flush_workqueue(ndev->rx_wq);
297 flush_workqueue(ndev->tx_wq);
298
299 /* Reset device */
300 skb_queue_purge(&ndev->cmd_q);
301 atomic_set(&ndev->cmd_cnt, 1);
302
303 set_bit(NCI_INIT, &ndev->flags);
304 __nci_request(ndev, nci_reset_req, 0,
305 msecs_to_jiffies(NCI_RESET_TIMEOUT));
306 clear_bit(NCI_INIT, &ndev->flags);
307
308 /* Flush cmd wq */
309 flush_workqueue(ndev->cmd_wq);
310
311 /* After this point our queues are empty
312 * and no works are scheduled. */
313 ndev->ops->close(ndev);
314
315 /* Clear flags */
316 ndev->flags = 0;
317
318 mutex_unlock(&ndev->req_lock);
319
320 return 0;
321}
322
323/* NCI command timer function */
324static void nci_cmd_timer(unsigned long arg)
325{
326 struct nci_dev *ndev = (void *) arg;
327
328 nfc_dbg("entry");
329
330 atomic_set(&ndev->cmd_cnt, 1);
331 queue_work(ndev->cmd_wq, &ndev->cmd_work);
332}
333
334static int nci_dev_up(struct nfc_dev *nfc_dev)
335{
336 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
337
338 nfc_dbg("entry");
339
340 return nci_open_device(ndev);
341}
342
343static int nci_dev_down(struct nfc_dev *nfc_dev)
344{
345 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
346
347 nfc_dbg("entry");
348
349 return nci_close_device(ndev);
350}
351
352static int nci_start_poll(struct nfc_dev *nfc_dev, __u32 protocols)
353{
354 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
355 int rc;
356
357 nfc_dbg("entry");
358
359 if (test_bit(NCI_DISCOVERY, &ndev->flags)) {
360 nfc_err("unable to start poll, since poll is already active");
361 return -EBUSY;
362 }
363
364 if (ndev->target_active_prot) {
365 nfc_err("there is an active target");
366 return -EBUSY;
367 }
368
369 if (test_bit(NCI_POLL_ACTIVE, &ndev->flags)) {
370 nfc_dbg("target is active, implicitly deactivate...");
371
372 rc = nci_request(ndev, nci_rf_deactivate_req, 0,
373 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
374 if (rc)
375 return -EBUSY;
376 }
377
378 rc = nci_request(ndev, nci_rf_discover_req, protocols,
379 msecs_to_jiffies(NCI_RF_DISC_TIMEOUT));
380
381 if (!rc)
382 ndev->poll_prots = protocols;
383
384 return rc;
385}
386
387static void nci_stop_poll(struct nfc_dev *nfc_dev)
388{
389 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
390
391 nfc_dbg("entry");
392
393 if (!test_bit(NCI_DISCOVERY, &ndev->flags)) {
394 nfc_err("unable to stop poll, since poll is not active");
395 return;
396 }
397
398 nci_request(ndev, nci_rf_deactivate_req, 0,
399 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
400}
401
402static int nci_activate_target(struct nfc_dev *nfc_dev, __u32 target_idx,
403 __u32 protocol)
404{
405 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
406
407 nfc_dbg("entry, target_idx %d, protocol 0x%x", target_idx, protocol);
408
409 if (!test_bit(NCI_POLL_ACTIVE, &ndev->flags)) {
410 nfc_err("there is no available target to activate");
411 return -EINVAL;
412 }
413
414 if (ndev->target_active_prot) {
415 nfc_err("there is already an active target");
416 return -EBUSY;
417 }
418
419 if (!(ndev->target_available_prots & (1 << protocol))) {
420 nfc_err("target does not support the requested protocol 0x%x",
421 protocol);
422 return -EINVAL;
423 }
424
425 ndev->target_active_prot = protocol;
426 ndev->target_available_prots = 0;
427
428 return 0;
429}
430
431static void nci_deactivate_target(struct nfc_dev *nfc_dev, __u32 target_idx)
432{
433 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
434
435 nfc_dbg("entry, target_idx %d", target_idx);
436
437 if (!ndev->target_active_prot) {
438 nfc_err("unable to deactivate target, no active target");
439 return;
440 }
441
442 ndev->target_active_prot = 0;
443
444 if (test_bit(NCI_POLL_ACTIVE, &ndev->flags)) {
445 nci_request(ndev, nci_rf_deactivate_req, 0,
446 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
447 }
448}
449
450static int nci_data_exchange(struct nfc_dev *nfc_dev, __u32 target_idx,
451 struct sk_buff *skb,
452 data_exchange_cb_t cb,
453 void *cb_context)
454{
455 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
456 int rc;
457
458 nfc_dbg("entry, target_idx %d, len %d", target_idx, skb->len);
459
460 if (!ndev->target_active_prot) {
461 nfc_err("unable to exchange data, no active target");
462 return -EINVAL;
463 }
464
465 if (test_and_set_bit(NCI_DATA_EXCHANGE, &ndev->flags))
466 return -EBUSY;
467
468 /* store cb and context to be used on receiving data */
469 ndev->data_exchange_cb = cb;
470 ndev->data_exchange_cb_context = cb_context;
471
472 rc = nci_send_data(ndev, ndev->conn_id, skb);
473 if (rc)
474 clear_bit(NCI_DATA_EXCHANGE, &ndev->flags);
475
476 return rc;
477}
478
479static struct nfc_ops nci_nfc_ops = {
480 .dev_up = nci_dev_up,
481 .dev_down = nci_dev_down,
482 .start_poll = nci_start_poll,
483 .stop_poll = nci_stop_poll,
484 .activate_target = nci_activate_target,
485 .deactivate_target = nci_deactivate_target,
486 .data_exchange = nci_data_exchange,
487};
488
489/* ---- Interface to NCI drivers ---- */
490
491/**
492 * nci_allocate_device - allocate a new nci device
493 *
494 * @ops: device operations
495 * @supported_protocols: NFC protocols supported by the device
496 */
497struct nci_dev *nci_allocate_device(struct nci_ops *ops,
498 __u32 supported_protocols,
499 int tx_headroom,
500 int tx_tailroom)
501{
502 struct nci_dev *ndev;
503
504 nfc_dbg("entry, supported_protocols 0x%x", supported_protocols);
505
506 if (!ops->open || !ops->close || !ops->send)
507 return NULL;
508
509 if (!supported_protocols)
510 return NULL;
511
512 ndev = kzalloc(sizeof(struct nci_dev), GFP_KERNEL);
513 if (!ndev)
514 return NULL;
515
516 ndev->ops = ops;
517 ndev->tx_headroom = tx_headroom;
518 ndev->tx_tailroom = tx_tailroom;
519
520 ndev->nfc_dev = nfc_allocate_device(&nci_nfc_ops,
521 supported_protocols,
522 tx_headroom + NCI_DATA_HDR_SIZE,
523 tx_tailroom);
524 if (!ndev->nfc_dev)
525 goto free_exit;
526
527 nfc_set_drvdata(ndev->nfc_dev, ndev);
528
529 return ndev;
530
531free_exit:
532 kfree(ndev);
533 return NULL;
534}
535EXPORT_SYMBOL(nci_allocate_device);
536
537/**
538 * nci_free_device - deallocate nci device
539 *
540 * @ndev: The nci device to deallocate
541 */
542void nci_free_device(struct nci_dev *ndev)
543{
544 nfc_dbg("entry");
545
546 nfc_free_device(ndev->nfc_dev);
547 kfree(ndev);
548}
549EXPORT_SYMBOL(nci_free_device);
550
551/**
552 * nci_register_device - register a nci device in the nfc subsystem
553 *
554 * @dev: The nci device to register
555 */
556int nci_register_device(struct nci_dev *ndev)
557{
558 int rc;
559 struct device *dev = &ndev->nfc_dev->dev;
560 char name[32];
561
562 nfc_dbg("entry");
563
564 rc = nfc_register_device(ndev->nfc_dev);
565 if (rc)
566 goto exit;
567
568 ndev->flags = 0;
569
570 INIT_WORK(&ndev->cmd_work, nci_cmd_work);
571 snprintf(name, sizeof(name), "%s_nci_cmd_wq", dev_name(dev));
572 ndev->cmd_wq = create_singlethread_workqueue(name);
573 if (!ndev->cmd_wq) {
574 rc = -ENOMEM;
575 goto unreg_exit;
576 }
577
578 INIT_WORK(&ndev->rx_work, nci_rx_work);
579 snprintf(name, sizeof(name), "%s_nci_rx_wq", dev_name(dev));
580 ndev->rx_wq = create_singlethread_workqueue(name);
581 if (!ndev->rx_wq) {
582 rc = -ENOMEM;
583 goto destroy_cmd_wq_exit;
584 }
585
586 INIT_WORK(&ndev->tx_work, nci_tx_work);
587 snprintf(name, sizeof(name), "%s_nci_tx_wq", dev_name(dev));
588 ndev->tx_wq = create_singlethread_workqueue(name);
589 if (!ndev->tx_wq) {
590 rc = -ENOMEM;
591 goto destroy_rx_wq_exit;
592 }
593
594 skb_queue_head_init(&ndev->cmd_q);
595 skb_queue_head_init(&ndev->rx_q);
596 skb_queue_head_init(&ndev->tx_q);
597
598 setup_timer(&ndev->cmd_timer, nci_cmd_timer,
599 (unsigned long) ndev);
600
601 mutex_init(&ndev->req_lock);
602
603 goto exit;
604
605destroy_rx_wq_exit:
606 destroy_workqueue(ndev->rx_wq);
607
608destroy_cmd_wq_exit:
609 destroy_workqueue(ndev->cmd_wq);
610
611unreg_exit:
612 nfc_unregister_device(ndev->nfc_dev);
613
614exit:
615 return rc;
616}
617EXPORT_SYMBOL(nci_register_device);
618
619/**
620 * nci_unregister_device - unregister a nci device in the nfc subsystem
621 *
622 * @dev: The nci device to unregister
623 */
624void nci_unregister_device(struct nci_dev *ndev)
625{
626 nfc_dbg("entry");
627
628 nci_close_device(ndev);
629
630 destroy_workqueue(ndev->cmd_wq);
631 destroy_workqueue(ndev->rx_wq);
632 destroy_workqueue(ndev->tx_wq);
633
634 nfc_unregister_device(ndev->nfc_dev);
635}
636EXPORT_SYMBOL(nci_unregister_device);
637
638/**
639 * nci_recv_frame - receive frame from NCI drivers
640 *
641 * @skb: The sk_buff to receive
642 */
643int nci_recv_frame(struct sk_buff *skb)
644{
645 struct nci_dev *ndev = (struct nci_dev *) skb->dev;
646
647 nfc_dbg("entry, len %d", skb->len);
648
649 if (!ndev || (!test_bit(NCI_UP, &ndev->flags)
650 && !test_bit(NCI_INIT, &ndev->flags))) {
651 kfree_skb(skb);
652 return -ENXIO;
653 }
654
655 /* Queue frame for rx worker thread */
656 skb_queue_tail(&ndev->rx_q, skb);
657 queue_work(ndev->rx_wq, &ndev->rx_work);
658
659 return 0;
660}
661EXPORT_SYMBOL(nci_recv_frame);
662
663static int nci_send_frame(struct sk_buff *skb)
664{
665 struct nci_dev *ndev = (struct nci_dev *) skb->dev;
666
667 nfc_dbg("entry, len %d", skb->len);
668
669 if (!ndev) {
670 kfree_skb(skb);
671 return -ENODEV;
672 }
673
674 /* Get rid of skb owner, prior to sending to the driver. */
675 skb_orphan(skb);
676
677 return ndev->ops->send(skb);
678}
679
680/* Send NCI command */
681int nci_send_cmd(struct nci_dev *ndev, __u16 opcode, __u8 plen, void *payload)
682{
683 struct nci_ctrl_hdr *hdr;
684 struct sk_buff *skb;
685
686 nfc_dbg("entry, opcode 0x%x, plen %d", opcode, plen);
687
688 skb = nci_skb_alloc(ndev, (NCI_CTRL_HDR_SIZE + plen), GFP_KERNEL);
689 if (!skb) {
690 nfc_err("no memory for command");
691 return -ENOMEM;
692 }
693
694 hdr = (struct nci_ctrl_hdr *) skb_put(skb, NCI_CTRL_HDR_SIZE);
695 hdr->gid = nci_opcode_gid(opcode);
696 hdr->oid = nci_opcode_oid(opcode);
697 hdr->plen = plen;
698
699 nci_mt_set((__u8 *)hdr, NCI_MT_CMD_PKT);
700 nci_pbf_set((__u8 *)hdr, NCI_PBF_LAST);
701
702 if (plen)
703 memcpy(skb_put(skb, plen), payload, plen);
704
705 skb->dev = (void *) ndev;
706
707 skb_queue_tail(&ndev->cmd_q, skb);
708 queue_work(ndev->cmd_wq, &ndev->cmd_work);
709
710 return 0;
711}
712
713/* ---- NCI TX Data worker thread ---- */
714
715static void nci_tx_work(struct work_struct *work)
716{
717 struct nci_dev *ndev = container_of(work, struct nci_dev, tx_work);
718 struct sk_buff *skb;
719
720 nfc_dbg("entry, credits_cnt %d", atomic_read(&ndev->credits_cnt));
721
722 /* Send queued tx data */
723 while (atomic_read(&ndev->credits_cnt)) {
724 skb = skb_dequeue(&ndev->tx_q);
725 if (!skb)
726 return;
727
728 atomic_dec(&ndev->credits_cnt);
729
730 nfc_dbg("NCI TX: MT=data, PBF=%d, conn_id=%d, plen=%d",
731 nci_pbf(skb->data),
732 nci_conn_id(skb->data),
733 nci_plen(skb->data));
734
735 nci_send_frame(skb);
736 }
737}
738
739/* ----- NCI RX worker thread (data & control) ----- */
740
741static void nci_rx_work(struct work_struct *work)
742{
743 struct nci_dev *ndev = container_of(work, struct nci_dev, rx_work);
744 struct sk_buff *skb;
745
746 while ((skb = skb_dequeue(&ndev->rx_q))) {
747 /* Process frame */
748 switch (nci_mt(skb->data)) {
749 case NCI_MT_RSP_PKT:
750 nci_rsp_packet(ndev, skb);
751 break;
752
753 case NCI_MT_NTF_PKT:
754 nci_ntf_packet(ndev, skb);
755 break;
756
757 case NCI_MT_DATA_PKT:
758 nci_rx_data_packet(ndev, skb);
759 break;
760
761 default:
762 nfc_err("unknown MT 0x%x", nci_mt(skb->data));
763 kfree_skb(skb);
764 break;
765 }
766 }
767}
768
769/* ----- NCI TX CMD worker thread ----- */
770
771static void nci_cmd_work(struct work_struct *work)
772{
773 struct nci_dev *ndev = container_of(work, struct nci_dev, cmd_work);
774 struct sk_buff *skb;
775
776 nfc_dbg("entry, cmd_cnt %d", atomic_read(&ndev->cmd_cnt));
777
778 /* Send queued command */
779 if (atomic_read(&ndev->cmd_cnt)) {
780 skb = skb_dequeue(&ndev->cmd_q);
781 if (!skb)
782 return;
783
784 atomic_dec(&ndev->cmd_cnt);
785
786 nfc_dbg("NCI TX: MT=cmd, PBF=%d, GID=0x%x, OID=0x%x, plen=%d",
787 nci_pbf(skb->data),
788 nci_opcode_gid(nci_opcode(skb->data)),
789 nci_opcode_oid(nci_opcode(skb->data)),
790 nci_plen(skb->data));
791
792 nci_send_frame(skb);
793
794 mod_timer(&ndev->cmd_timer,
795 jiffies + msecs_to_jiffies(NCI_CMD_TIMEOUT));
796 }
797}
diff --git a/net/nfc/nci/data.c b/net/nfc/nci/data.c
new file mode 100644
index 000000000000..e5ed90fc1a9c
--- /dev/null
+++ b/net/nfc/nci/data.c
@@ -0,0 +1,247 @@
1/*
2 * The NFC Controller Interface is the communication protocol between an
3 * NFC Controller (NFCC) and a Device Host (DH).
4 *
5 * Copyright (C) 2011 Texas Instruments, Inc.
6 *
7 * Written by Ilan Elias <ilane@ti.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2
11 * as published by the Free Software Foundation
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 Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 */
23
24#include <linux/types.h>
25#include <linux/interrupt.h>
26#include <linux/wait.h>
27#include <linux/bitops.h>
28#include <linux/skbuff.h>
29
30#include "../nfc.h"
31#include <net/nfc/nci.h>
32#include <net/nfc/nci_core.h>
33#include <linux/nfc.h>
34
35/* Complete data exchange transaction and forward skb to nfc core */
36void nci_data_exchange_complete(struct nci_dev *ndev,
37 struct sk_buff *skb,
38 int err)
39{
40 data_exchange_cb_t cb = ndev->data_exchange_cb;
41 void *cb_context = ndev->data_exchange_cb_context;
42
43 nfc_dbg("entry, len %d, err %d", ((skb) ? (skb->len) : (0)), err);
44
45 if (cb) {
46 ndev->data_exchange_cb = NULL;
47 ndev->data_exchange_cb_context = 0;
48
49 /* forward skb to nfc core */
50 cb(cb_context, skb, err);
51 } else if (skb) {
52 nfc_err("no rx callback, dropping rx data...");
53
54 /* no waiting callback, free skb */
55 kfree_skb(skb);
56 }
57
58 clear_bit(NCI_DATA_EXCHANGE, &ndev->flags);
59}
60
61/* ----------------- NCI TX Data ----------------- */
62
63static inline void nci_push_data_hdr(struct nci_dev *ndev,
64 __u8 conn_id,
65 struct sk_buff *skb,
66 __u8 pbf)
67{
68 struct nci_data_hdr *hdr;
69 int plen = skb->len;
70
71 hdr = (struct nci_data_hdr *) skb_push(skb, NCI_DATA_HDR_SIZE);
72 hdr->conn_id = conn_id;
73 hdr->rfu = 0;
74 hdr->plen = plen;
75
76 nci_mt_set((__u8 *)hdr, NCI_MT_DATA_PKT);
77 nci_pbf_set((__u8 *)hdr, pbf);
78
79 skb->dev = (void *) ndev;
80}
81
82static int nci_queue_tx_data_frags(struct nci_dev *ndev,
83 __u8 conn_id,
84 struct sk_buff *skb) {
85 int total_len = skb->len;
86 unsigned char *data = skb->data;
87 unsigned long flags;
88 struct sk_buff_head frags_q;
89 struct sk_buff *skb_frag;
90 int frag_len;
91 int rc = 0;
92
93 nfc_dbg("entry, conn_id 0x%x, total_len %d", conn_id, total_len);
94
95 __skb_queue_head_init(&frags_q);
96
97 while (total_len) {
98 frag_len = min_t(int, total_len, ndev->max_pkt_payload_size);
99
100 skb_frag = nci_skb_alloc(ndev,
101 (NCI_DATA_HDR_SIZE + frag_len),
102 GFP_KERNEL);
103 if (skb_frag == NULL) {
104 rc = -ENOMEM;
105 goto free_exit;
106 }
107 skb_reserve(skb_frag, NCI_DATA_HDR_SIZE);
108
109 /* first, copy the data */
110 memcpy(skb_put(skb_frag, frag_len), data, frag_len);
111
112 /* second, set the header */
113 nci_push_data_hdr(ndev, conn_id, skb_frag,
114 ((total_len == frag_len) ? (NCI_PBF_LAST) : (NCI_PBF_CONT)));
115
116 __skb_queue_tail(&frags_q, skb_frag);
117
118 data += frag_len;
119 total_len -= frag_len;
120
121 nfc_dbg("frag_len %d, remaining total_len %d",
122 frag_len, total_len);
123 }
124
125 /* queue all fragments atomically */
126 spin_lock_irqsave(&ndev->tx_q.lock, flags);
127
128 while ((skb_frag = __skb_dequeue(&frags_q)) != NULL)
129 __skb_queue_tail(&ndev->tx_q, skb_frag);
130
131 spin_unlock_irqrestore(&ndev->tx_q.lock, flags);
132
133 /* free the original skb */
134 kfree_skb(skb);
135
136 goto exit;
137
138free_exit:
139 while ((skb_frag = __skb_dequeue(&frags_q)) != NULL)
140 kfree_skb(skb_frag);
141
142exit:
143 return rc;
144}
145
146/* Send NCI data */
147int nci_send_data(struct nci_dev *ndev, __u8 conn_id, struct sk_buff *skb)
148{
149 int rc = 0;
150
151 nfc_dbg("entry, conn_id 0x%x, plen %d", conn_id, skb->len);
152
153 /* check if the packet need to be fragmented */
154 if (skb->len <= ndev->max_pkt_payload_size) {
155 /* no need to fragment packet */
156 nci_push_data_hdr(ndev, conn_id, skb, NCI_PBF_LAST);
157
158 skb_queue_tail(&ndev->tx_q, skb);
159 } else {
160 /* fragment packet and queue the fragments */
161 rc = nci_queue_tx_data_frags(ndev, conn_id, skb);
162 if (rc) {
163 nfc_err("failed to fragment tx data packet");
164 goto free_exit;
165 }
166 }
167
168 queue_work(ndev->tx_wq, &ndev->tx_work);
169
170 goto exit;
171
172free_exit:
173 kfree_skb(skb);
174
175exit:
176 return rc;
177}
178
179/* ----------------- NCI RX Data ----------------- */
180
181static void nci_add_rx_data_frag(struct nci_dev *ndev,
182 struct sk_buff *skb,
183 __u8 pbf)
184{
185 int reassembly_len;
186 int err = 0;
187
188 if (ndev->rx_data_reassembly) {
189 reassembly_len = ndev->rx_data_reassembly->len;
190
191 /* first, make enough room for the already accumulated data */
192 if (skb_cow_head(skb, reassembly_len)) {
193 nfc_err("error adding room for accumulated rx data");
194
195 kfree_skb(skb);
196 skb = 0;
197
198 kfree_skb(ndev->rx_data_reassembly);
199 ndev->rx_data_reassembly = 0;
200
201 err = -ENOMEM;
202 goto exit;
203 }
204
205 /* second, combine the two fragments */
206 memcpy(skb_push(skb, reassembly_len),
207 ndev->rx_data_reassembly->data,
208 reassembly_len);
209
210 /* third, free old reassembly */
211 kfree_skb(ndev->rx_data_reassembly);
212 ndev->rx_data_reassembly = 0;
213 }
214
215 if (pbf == NCI_PBF_CONT) {
216 /* need to wait for next fragment, store skb and exit */
217 ndev->rx_data_reassembly = skb;
218 return;
219 }
220
221exit:
222 nci_data_exchange_complete(ndev, skb, err);
223}
224
225/* Rx Data packet */
226void nci_rx_data_packet(struct nci_dev *ndev, struct sk_buff *skb)
227{
228 __u8 pbf = nci_pbf(skb->data);
229
230 nfc_dbg("entry, len %d", skb->len);
231
232 nfc_dbg("NCI RX: MT=data, PBF=%d, conn_id=%d, plen=%d",
233 nci_pbf(skb->data),
234 nci_conn_id(skb->data),
235 nci_plen(skb->data));
236
237 /* strip the nci data header */
238 skb_pull(skb, NCI_DATA_HDR_SIZE);
239
240 if (ndev->target_active_prot == NFC_PROTO_MIFARE) {
241 /* frame I/F => remove the status byte */
242 nfc_dbg("NFC_PROTO_MIFARE => remove the status byte");
243 skb_trim(skb, (skb->len - 1));
244 }
245
246 nci_add_rx_data_frag(ndev, skb, pbf);
247}
diff --git a/net/nfc/nci/lib.c b/net/nfc/nci/lib.c
new file mode 100644
index 000000000000..b19dc2fa90e1
--- /dev/null
+++ b/net/nfc/nci/lib.c
@@ -0,0 +1,94 @@
1/*
2 * The NFC Controller Interface is the communication protocol between an
3 * NFC Controller (NFCC) and a Device Host (DH).
4 *
5 * Copyright (C) 2011 Texas Instruments, Inc.
6 *
7 * Written by Ilan Elias <ilane@ti.com>
8 *
9 * Acknowledgements:
10 * This file is based on lib.c, which was written
11 * by Maxim Krasnyansky.
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2
15 * as published by the Free Software Foundation
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 */
27
28#include <linux/module.h>
29#include <linux/kernel.h>
30#include <linux/types.h>
31#include <linux/errno.h>
32
33#include <net/nfc/nci.h>
34
35/* NCI status codes to Unix errno mapping */
36int nci_to_errno(__u8 code)
37{
38 switch (code) {
39 case NCI_STATUS_OK:
40 return 0;
41
42 case NCI_STATUS_REJECTED:
43 return -EBUSY;
44
45 case NCI_STATUS_MESSAGE_CORRUPTED:
46 return -EBADMSG;
47
48 case NCI_STATUS_BUFFER_FULL:
49 return -ENOBUFS;
50
51 case NCI_STATUS_NOT_INITIALIZED:
52 return -EHOSTDOWN;
53
54 case NCI_STATUS_SYNTAX_ERROR:
55 case NCI_STATUS_SEMANTIC_ERROR:
56 case NCI_STATUS_INVALID_PARAM:
57 case NCI_STATUS_RF_PROTOCOL_ERROR:
58 case NCI_STATUS_NFCEE_PROTOCOL_ERROR:
59 return -EPROTO;
60
61 case NCI_STATUS_UNKNOWN_GID:
62 case NCI_STATUS_UNKNOWN_OID:
63 return -EBADRQC;
64
65 case NCI_STATUS_MESSAGE_SIZE_EXCEEDED:
66 return -EMSGSIZE;
67
68 case NCI_STATUS_DISCOVERY_ALREADY_STARTED:
69 return -EALREADY;
70
71 case NCI_STATUS_DISCOVERY_TARGET_ACTIVATION_FAILED:
72 case NCI_STATUS_NFCEE_INTERFACE_ACTIVATION_FAILED:
73 return -ECONNREFUSED;
74
75 case NCI_STATUS_RF_TRANSMISSION_ERROR:
76 case NCI_STATUS_NFCEE_TRANSMISSION_ERROR:
77 return -ECOMM;
78
79 case NCI_STATUS_RF_TIMEOUT_ERROR:
80 case NCI_STATUS_NFCEE_TIMEOUT_ERROR:
81 return -ETIMEDOUT;
82
83 case NCI_STATUS_RF_LINK_LOSS_ERROR:
84 return -ENOLINK;
85
86 case NCI_STATUS_MAX_ACTIVE_NFCEE_INTERFACES_REACHED:
87 return -EDQUOT;
88
89 case NCI_STATUS_FAILED:
90 default:
91 return -ENOSYS;
92 }
93}
94EXPORT_SYMBOL(nci_to_errno);
diff --git a/net/nfc/nci/ntf.c b/net/nfc/nci/ntf.c
new file mode 100644
index 000000000000..96633f5cda4f
--- /dev/null
+++ b/net/nfc/nci/ntf.c
@@ -0,0 +1,258 @@
1/*
2 * The NFC Controller Interface is the communication protocol between an
3 * NFC Controller (NFCC) and a Device Host (DH).
4 *
5 * Copyright (C) 2011 Texas Instruments, Inc.
6 *
7 * Written by Ilan Elias <ilane@ti.com>
8 *
9 * Acknowledgements:
10 * This file is based on hci_event.c, which was written
11 * by Maxim Krasnyansky.
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2
15 * as published by the Free Software Foundation
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 */
27
28#include <linux/types.h>
29#include <linux/interrupt.h>
30#include <linux/bitops.h>
31#include <linux/skbuff.h>
32
33#include "../nfc.h"
34#include <net/nfc/nci.h>
35#include <net/nfc/nci_core.h>
36#include <linux/nfc.h>
37
38/* Handle NCI Notification packets */
39
40static void nci_core_conn_credits_ntf_packet(struct nci_dev *ndev,
41 struct sk_buff *skb)
42{
43 struct nci_core_conn_credit_ntf *ntf = (void *) skb->data;
44 int i;
45
46 nfc_dbg("entry, num_entries %d", ntf->num_entries);
47
48 if (ntf->num_entries > NCI_MAX_NUM_CONN)
49 ntf->num_entries = NCI_MAX_NUM_CONN;
50
51 /* update the credits */
52 for (i = 0; i < ntf->num_entries; i++) {
53 nfc_dbg("entry[%d]: conn_id %d, credits %d", i,
54 ntf->conn_entries[i].conn_id,
55 ntf->conn_entries[i].credits);
56
57 if (ntf->conn_entries[i].conn_id == ndev->conn_id) {
58 /* found static rf connection */
59 atomic_add(ntf->conn_entries[i].credits,
60 &ndev->credits_cnt);
61 }
62 }
63
64 /* trigger the next tx */
65 if (!skb_queue_empty(&ndev->tx_q))
66 queue_work(ndev->tx_wq, &ndev->tx_work);
67}
68
69static void nci_rf_field_info_ntf_packet(struct nci_dev *ndev,
70 struct sk_buff *skb)
71{
72 struct nci_rf_field_info_ntf *ntf = (void *) skb->data;
73
74 nfc_dbg("entry, rf_field_status %d", ntf->rf_field_status);
75}
76
77static int nci_rf_activate_nfca_passive_poll(struct nci_dev *ndev,
78 struct nci_rf_activate_ntf *ntf, __u8 *data)
79{
80 struct rf_tech_specific_params_nfca_poll *nfca_poll;
81 struct activation_params_nfca_poll_iso_dep *nfca_poll_iso_dep;
82
83 nfca_poll = &ntf->rf_tech_specific_params.nfca_poll;
84 nfca_poll_iso_dep = &ntf->activation_params.nfca_poll_iso_dep;
85
86 nfca_poll->sens_res = __le16_to_cpu(*((__u16 *)data));
87 data += 2;
88
89 nfca_poll->nfcid1_len = *data++;
90
91 nfc_dbg("sens_res 0x%x, nfcid1_len %d",
92 nfca_poll->sens_res,
93 nfca_poll->nfcid1_len);
94
95 memcpy(nfca_poll->nfcid1, data, nfca_poll->nfcid1_len);
96 data += nfca_poll->nfcid1_len;
97
98 nfca_poll->sel_res_len = *data++;
99
100 if (nfca_poll->sel_res_len != 0)
101 nfca_poll->sel_res = *data++;
102
103 ntf->rf_interface_type = *data++;
104 ntf->activation_params_len = *data++;
105
106 nfc_dbg("sel_res_len %d, sel_res 0x%x, rf_interface_type %d, activation_params_len %d",
107 nfca_poll->sel_res_len,
108 nfca_poll->sel_res,
109 ntf->rf_interface_type,
110 ntf->activation_params_len);
111
112 switch (ntf->rf_interface_type) {
113 case NCI_RF_INTERFACE_ISO_DEP:
114 nfca_poll_iso_dep->rats_res_len = *data++;
115 if (nfca_poll_iso_dep->rats_res_len > 0) {
116 memcpy(nfca_poll_iso_dep->rats_res,
117 data,
118 nfca_poll_iso_dep->rats_res_len);
119 }
120 break;
121
122 case NCI_RF_INTERFACE_FRAME:
123 /* no activation params */
124 break;
125
126 default:
127 nfc_err("unsupported rf_interface_type 0x%x",
128 ntf->rf_interface_type);
129 return -EPROTO;
130 }
131
132 return 0;
133}
134
135static void nci_target_found(struct nci_dev *ndev,
136 struct nci_rf_activate_ntf *ntf)
137{
138 struct nfc_target nfc_tgt;
139
140 if (ntf->rf_protocol == NCI_RF_PROTOCOL_T2T) /* T2T MifareUL */
141 nfc_tgt.supported_protocols = NFC_PROTO_MIFARE_MASK;
142 else if (ntf->rf_protocol == NCI_RF_PROTOCOL_ISO_DEP) /* 4A */
143 nfc_tgt.supported_protocols = NFC_PROTO_ISO14443_MASK;
144
145 nfc_tgt.sens_res = ntf->rf_tech_specific_params.nfca_poll.sens_res;
146 nfc_tgt.sel_res = ntf->rf_tech_specific_params.nfca_poll.sel_res;
147
148 if (!(nfc_tgt.supported_protocols & ndev->poll_prots)) {
149 nfc_dbg("the target found does not have the desired protocol");
150 return;
151 }
152
153 nfc_dbg("new target found, supported_protocols 0x%x",
154 nfc_tgt.supported_protocols);
155
156 ndev->target_available_prots = nfc_tgt.supported_protocols;
157
158 nfc_targets_found(ndev->nfc_dev, &nfc_tgt, 1);
159}
160
161static void nci_rf_activate_ntf_packet(struct nci_dev *ndev,
162 struct sk_buff *skb)
163{
164 struct nci_rf_activate_ntf ntf;
165 __u8 *data = skb->data;
166 int rc = -1;
167
168 clear_bit(NCI_DISCOVERY, &ndev->flags);
169 set_bit(NCI_POLL_ACTIVE, &ndev->flags);
170
171 ntf.target_handle = *data++;
172 ntf.rf_protocol = *data++;
173 ntf.rf_tech_and_mode = *data++;
174 ntf.rf_tech_specific_params_len = *data++;
175
176 nfc_dbg("target_handle %d, rf_protocol 0x%x, rf_tech_and_mode 0x%x, rf_tech_specific_params_len %d",
177 ntf.target_handle,
178 ntf.rf_protocol,
179 ntf.rf_tech_and_mode,
180 ntf.rf_tech_specific_params_len);
181
182 switch (ntf.rf_tech_and_mode) {
183 case NCI_NFC_A_PASSIVE_POLL_MODE:
184 rc = nci_rf_activate_nfca_passive_poll(ndev, &ntf,
185 data);
186 break;
187
188 default:
189 nfc_err("unsupported rf_tech_and_mode 0x%x",
190 ntf.rf_tech_and_mode);
191 return;
192 }
193
194 if (!rc)
195 nci_target_found(ndev, &ntf);
196}
197
198static void nci_rf_deactivate_ntf_packet(struct nci_dev *ndev,
199 struct sk_buff *skb)
200{
201 __u8 type = skb->data[0];
202
203 nfc_dbg("entry, type 0x%x", type);
204
205 clear_bit(NCI_POLL_ACTIVE, &ndev->flags);
206 ndev->target_active_prot = 0;
207
208 /* drop tx data queue */
209 skb_queue_purge(&ndev->tx_q);
210
211 /* drop partial rx data packet */
212 if (ndev->rx_data_reassembly) {
213 kfree_skb(ndev->rx_data_reassembly);
214 ndev->rx_data_reassembly = 0;
215 }
216
217 /* complete the data exchange transaction, if exists */
218 if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags))
219 nci_data_exchange_complete(ndev, NULL, -EIO);
220}
221
222void nci_ntf_packet(struct nci_dev *ndev, struct sk_buff *skb)
223{
224 __u16 ntf_opcode = nci_opcode(skb->data);
225
226 nfc_dbg("NCI RX: MT=ntf, PBF=%d, GID=0x%x, OID=0x%x, plen=%d",
227 nci_pbf(skb->data),
228 nci_opcode_gid(ntf_opcode),
229 nci_opcode_oid(ntf_opcode),
230 nci_plen(skb->data));
231
232 /* strip the nci control header */
233 skb_pull(skb, NCI_CTRL_HDR_SIZE);
234
235 switch (ntf_opcode) {
236 case NCI_OP_CORE_CONN_CREDITS_NTF:
237 nci_core_conn_credits_ntf_packet(ndev, skb);
238 break;
239
240 case NCI_OP_RF_FIELD_INFO_NTF:
241 nci_rf_field_info_ntf_packet(ndev, skb);
242 break;
243
244 case NCI_OP_RF_ACTIVATE_NTF:
245 nci_rf_activate_ntf_packet(ndev, skb);
246 break;
247
248 case NCI_OP_RF_DEACTIVATE_NTF:
249 nci_rf_deactivate_ntf_packet(ndev, skb);
250 break;
251
252 default:
253 nfc_err("unknown ntf opcode 0x%x", ntf_opcode);
254 break;
255 }
256
257 kfree_skb(skb);
258}
diff --git a/net/nfc/nci/rsp.c b/net/nfc/nci/rsp.c
new file mode 100644
index 000000000000..0403d4cd0917
--- /dev/null
+++ b/net/nfc/nci/rsp.c
@@ -0,0 +1,226 @@
1/*
2 * The NFC Controller Interface is the communication protocol between an
3 * NFC Controller (NFCC) and a Device Host (DH).
4 *
5 * Copyright (C) 2011 Texas Instruments, Inc.
6 *
7 * Written by Ilan Elias <ilane@ti.com>
8 *
9 * Acknowledgements:
10 * This file is based on hci_event.c, which was written
11 * by Maxim Krasnyansky.
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2
15 * as published by the Free Software Foundation
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 */
27
28#include <linux/types.h>
29#include <linux/interrupt.h>
30#include <linux/bitops.h>
31#include <linux/skbuff.h>
32
33#include "../nfc.h"
34#include <net/nfc/nci.h>
35#include <net/nfc/nci_core.h>
36
37/* Handle NCI Response packets */
38
39static void nci_core_reset_rsp_packet(struct nci_dev *ndev, struct sk_buff *skb)
40{
41 struct nci_core_reset_rsp *rsp = (void *) skb->data;
42
43 nfc_dbg("entry, status 0x%x", rsp->status);
44
45 if (rsp->status == NCI_STATUS_OK)
46 ndev->nci_ver = rsp->nci_ver;
47
48 nfc_dbg("nci_ver 0x%x", ndev->nci_ver);
49
50 nci_req_complete(ndev, rsp->status);
51}
52
53static void nci_core_init_rsp_packet(struct nci_dev *ndev, struct sk_buff *skb)
54{
55 struct nci_core_init_rsp_1 *rsp_1 = (void *) skb->data;
56 struct nci_core_init_rsp_2 *rsp_2;
57
58 nfc_dbg("entry, status 0x%x", rsp_1->status);
59
60 if (rsp_1->status != NCI_STATUS_OK)
61 return;
62
63 ndev->nfcc_features = __le32_to_cpu(rsp_1->nfcc_features);
64 ndev->num_supported_rf_interfaces = rsp_1->num_supported_rf_interfaces;
65
66 if (ndev->num_supported_rf_interfaces >
67 NCI_MAX_SUPPORTED_RF_INTERFACES) {
68 ndev->num_supported_rf_interfaces =
69 NCI_MAX_SUPPORTED_RF_INTERFACES;
70 }
71
72 memcpy(ndev->supported_rf_interfaces,
73 rsp_1->supported_rf_interfaces,
74 ndev->num_supported_rf_interfaces);
75
76 rsp_2 = (void *) (skb->data + 6 + ndev->num_supported_rf_interfaces);
77
78 ndev->max_logical_connections =
79 rsp_2->max_logical_connections;
80 ndev->max_routing_table_size =
81 __le16_to_cpu(rsp_2->max_routing_table_size);
82 ndev->max_control_packet_payload_length =
83 rsp_2->max_control_packet_payload_length;
84 ndev->rf_sending_buffer_size =
85 __le16_to_cpu(rsp_2->rf_sending_buffer_size);
86 ndev->rf_receiving_buffer_size =
87 __le16_to_cpu(rsp_2->rf_receiving_buffer_size);
88 ndev->manufacturer_id =
89 __le16_to_cpu(rsp_2->manufacturer_id);
90
91 nfc_dbg("nfcc_features 0x%x",
92 ndev->nfcc_features);
93 nfc_dbg("num_supported_rf_interfaces %d",
94 ndev->num_supported_rf_interfaces);
95 nfc_dbg("supported_rf_interfaces[0] 0x%x",
96 ndev->supported_rf_interfaces[0]);
97 nfc_dbg("supported_rf_interfaces[1] 0x%x",
98 ndev->supported_rf_interfaces[1]);
99 nfc_dbg("supported_rf_interfaces[2] 0x%x",
100 ndev->supported_rf_interfaces[2]);
101 nfc_dbg("supported_rf_interfaces[3] 0x%x",
102 ndev->supported_rf_interfaces[3]);
103 nfc_dbg("max_logical_connections %d",
104 ndev->max_logical_connections);
105 nfc_dbg("max_routing_table_size %d",
106 ndev->max_routing_table_size);
107 nfc_dbg("max_control_packet_payload_length %d",
108 ndev->max_control_packet_payload_length);
109 nfc_dbg("rf_sending_buffer_size %d",
110 ndev->rf_sending_buffer_size);
111 nfc_dbg("rf_receiving_buffer_size %d",
112 ndev->rf_receiving_buffer_size);
113 nfc_dbg("manufacturer_id 0x%x",
114 ndev->manufacturer_id);
115
116 nci_req_complete(ndev, rsp_1->status);
117}
118
119static void nci_core_conn_create_rsp_packet(struct nci_dev *ndev,
120 struct sk_buff *skb)
121{
122 struct nci_core_conn_create_rsp *rsp = (void *) skb->data;
123
124 nfc_dbg("entry, status 0x%x", rsp->status);
125
126 if (rsp->status != NCI_STATUS_OK)
127 return;
128
129 ndev->max_pkt_payload_size = rsp->max_pkt_payload_size;
130 ndev->initial_num_credits = rsp->initial_num_credits;
131 ndev->conn_id = rsp->conn_id;
132
133 atomic_set(&ndev->credits_cnt, ndev->initial_num_credits);
134
135 nfc_dbg("max_pkt_payload_size %d", ndev->max_pkt_payload_size);
136 nfc_dbg("initial_num_credits %d", ndev->initial_num_credits);
137 nfc_dbg("conn_id %d", ndev->conn_id);
138}
139
140static void nci_rf_disc_map_rsp_packet(struct nci_dev *ndev,
141 struct sk_buff *skb)
142{
143 __u8 status = skb->data[0];
144
145 nfc_dbg("entry, status 0x%x", status);
146
147 nci_req_complete(ndev, status);
148}
149
150static void nci_rf_disc_rsp_packet(struct nci_dev *ndev, struct sk_buff *skb)
151{
152 __u8 status = skb->data[0];
153
154 nfc_dbg("entry, status 0x%x", status);
155
156 if (status == NCI_STATUS_OK)
157 set_bit(NCI_DISCOVERY, &ndev->flags);
158
159 nci_req_complete(ndev, status);
160}
161
162static void nci_rf_deactivate_rsp_packet(struct nci_dev *ndev,
163 struct sk_buff *skb)
164{
165 __u8 status = skb->data[0];
166
167 nfc_dbg("entry, status 0x%x", status);
168
169 clear_bit(NCI_DISCOVERY, &ndev->flags);
170
171 nci_req_complete(ndev, status);
172}
173
174void nci_rsp_packet(struct nci_dev *ndev, struct sk_buff *skb)
175{
176 __u16 rsp_opcode = nci_opcode(skb->data);
177
178 /* we got a rsp, stop the cmd timer */
179 del_timer(&ndev->cmd_timer);
180
181 nfc_dbg("NCI RX: MT=rsp, PBF=%d, GID=0x%x, OID=0x%x, plen=%d",
182 nci_pbf(skb->data),
183 nci_opcode_gid(rsp_opcode),
184 nci_opcode_oid(rsp_opcode),
185 nci_plen(skb->data));
186
187 /* strip the nci control header */
188 skb_pull(skb, NCI_CTRL_HDR_SIZE);
189
190 switch (rsp_opcode) {
191 case NCI_OP_CORE_RESET_RSP:
192 nci_core_reset_rsp_packet(ndev, skb);
193 break;
194
195 case NCI_OP_CORE_INIT_RSP:
196 nci_core_init_rsp_packet(ndev, skb);
197 break;
198
199 case NCI_OP_CORE_CONN_CREATE_RSP:
200 nci_core_conn_create_rsp_packet(ndev, skb);
201 break;
202
203 case NCI_OP_RF_DISCOVER_MAP_RSP:
204 nci_rf_disc_map_rsp_packet(ndev, skb);
205 break;
206
207 case NCI_OP_RF_DISCOVER_RSP:
208 nci_rf_disc_rsp_packet(ndev, skb);
209 break;
210
211 case NCI_OP_RF_DEACTIVATE_RSP:
212 nci_rf_deactivate_rsp_packet(ndev, skb);
213 break;
214
215 default:
216 nfc_err("unknown rsp opcode 0x%x", rsp_opcode);
217 break;
218 }
219
220 kfree_skb(skb);
221
222 /* trigger the next cmd */
223 atomic_set(&ndev->cmd_cnt, 1);
224 if (!skb_queue_empty(&ndev->cmd_q))
225 queue_work(ndev->cmd_wq, &ndev->cmd_work);
226}
diff --git a/net/nfc/netlink.c b/net/nfc/netlink.c
index ccdff7953f7d..03f8818e1f16 100644
--- a/net/nfc/netlink.c
+++ b/net/nfc/netlink.c
@@ -367,6 +367,52 @@ out_putdev:
367 return rc; 367 return rc;
368} 368}
369 369
370static int nfc_genl_dev_up(struct sk_buff *skb, struct genl_info *info)
371{
372 struct nfc_dev *dev;
373 int rc;
374 u32 idx;
375
376 nfc_dbg("entry");
377
378 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
379 return -EINVAL;
380
381 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
382
383 dev = nfc_get_device(idx);
384 if (!dev)
385 return -ENODEV;
386
387 rc = nfc_dev_up(dev);
388
389 nfc_put_device(dev);
390 return rc;
391}
392
393static int nfc_genl_dev_down(struct sk_buff *skb, struct genl_info *info)
394{
395 struct nfc_dev *dev;
396 int rc;
397 u32 idx;
398
399 nfc_dbg("entry");
400
401 if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
402 return -EINVAL;
403
404 idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
405
406 dev = nfc_get_device(idx);
407 if (!dev)
408 return -ENODEV;
409
410 rc = nfc_dev_down(dev);
411
412 nfc_put_device(dev);
413 return rc;
414}
415
370static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info) 416static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info)
371{ 417{
372 struct nfc_dev *dev; 418 struct nfc_dev *dev;
@@ -441,6 +487,16 @@ static struct genl_ops nfc_genl_ops[] = {
441 .policy = nfc_genl_policy, 487 .policy = nfc_genl_policy,
442 }, 488 },
443 { 489 {
490 .cmd = NFC_CMD_DEV_UP,
491 .doit = nfc_genl_dev_up,
492 .policy = nfc_genl_policy,
493 },
494 {
495 .cmd = NFC_CMD_DEV_DOWN,
496 .doit = nfc_genl_dev_down,
497 .policy = nfc_genl_policy,
498 },
499 {
444 .cmd = NFC_CMD_START_POLL, 500 .cmd = NFC_CMD_START_POLL,
445 .doit = nfc_genl_start_poll, 501 .doit = nfc_genl_start_poll,
446 .policy = nfc_genl_policy, 502 .policy = nfc_genl_policy,
diff --git a/net/nfc/nfc.h b/net/nfc/nfc.h
index aaf9832298f3..d86583f4831d 100644
--- a/net/nfc/nfc.h
+++ b/net/nfc/nfc.h
@@ -24,10 +24,10 @@
24#ifndef __LOCAL_NFC_H 24#ifndef __LOCAL_NFC_H
25#define __LOCAL_NFC_H 25#define __LOCAL_NFC_H
26 26
27#include <net/nfc.h> 27#include <net/nfc/nfc.h>
28#include <net/sock.h> 28#include <net/sock.h>
29 29
30__attribute__((format (printf, 2, 3))) 30__printf(2, 3)
31int nfc_printk(const char *level, const char *fmt, ...); 31int nfc_printk(const char *level, const char *fmt, ...);
32 32
33#define nfc_info(fmt, arg...) nfc_printk(KERN_INFO, fmt, ##arg) 33#define nfc_info(fmt, arg...) nfc_printk(KERN_INFO, fmt, ##arg)
@@ -101,6 +101,10 @@ static inline void nfc_device_iter_exit(struct class_dev_iter *iter)
101 class_dev_iter_exit(iter); 101 class_dev_iter_exit(iter);
102} 102}
103 103
104int nfc_dev_up(struct nfc_dev *dev);
105
106int nfc_dev_down(struct nfc_dev *dev);
107
104int nfc_start_poll(struct nfc_dev *dev, u32 protocols); 108int nfc_start_poll(struct nfc_dev *dev, u32 protocols);
105 109
106int nfc_stop_poll(struct nfc_dev *dev); 110int nfc_stop_poll(struct nfc_dev *dev);
diff --git a/net/nfc/rawsock.c b/net/nfc/rawsock.c
index 52de84a55115..9fd652a51424 100644
--- a/net/nfc/rawsock.c
+++ b/net/nfc/rawsock.c
@@ -123,11 +123,7 @@ error:
123 123
124static int rawsock_add_header(struct sk_buff *skb) 124static int rawsock_add_header(struct sk_buff *skb)
125{ 125{
126 126 *skb_push(skb, NFC_HEADER_SIZE) = 0;
127 if (skb_cow_head(skb, 1))
128 return -ENOMEM;
129
130 *skb_push(skb, 1) = 0;
131 127
132 return 0; 128 return 0;
133} 129}
@@ -197,6 +193,7 @@ static int rawsock_sendmsg(struct kiocb *iocb, struct socket *sock,
197 struct msghdr *msg, size_t len) 193 struct msghdr *msg, size_t len)
198{ 194{
199 struct sock *sk = sock->sk; 195 struct sock *sk = sock->sk;
196 struct nfc_dev *dev = nfc_rawsock(sk)->dev;
200 struct sk_buff *skb; 197 struct sk_buff *skb;
201 int rc; 198 int rc;
202 199
@@ -208,11 +205,13 @@ static int rawsock_sendmsg(struct kiocb *iocb, struct socket *sock,
208 if (sock->state != SS_CONNECTED) 205 if (sock->state != SS_CONNECTED)
209 return -ENOTCONN; 206 return -ENOTCONN;
210 207
211 skb = sock_alloc_send_skb(sk, len, msg->msg_flags & MSG_DONTWAIT, 208 skb = sock_alloc_send_skb(sk, len + dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE,
212 &rc); 209 msg->msg_flags & MSG_DONTWAIT, &rc);
213 if (!skb) 210 if (!skb)
214 return rc; 211 return rc;
215 212
213 skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE);
214
216 rc = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len); 215 rc = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
217 if (rc < 0) { 216 if (rc < 0) {
218 kfree_skb(skb); 217 kfree_skb(skb);