blob: 557fe92d29c7ee55619de8ddbf14da18f270de78 [file] [log] [blame]
Ilan Elias6a2968a2011-09-18 11:19:35 +03001/*
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{
Ilan Eliase8c0dac2011-11-09 12:09:14 +0200128 struct nci_core_reset_cmd cmd;
129
130 cmd.reset_type = NCI_RESET_TYPE_RESET_CONFIG;
131 nci_send_cmd(ndev, NCI_OP_CORE_RESET_CMD, 1, &cmd);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300132}
133
134static void nci_init_req(struct nci_dev *ndev, unsigned long opt)
135{
136 nci_send_cmd(ndev, NCI_OP_CORE_INIT_CMD, 0, NULL);
137}
138
139static void nci_init_complete_req(struct nci_dev *ndev, unsigned long opt)
140{
Ilan Elias6a2968a2011-09-18 11:19:35 +0300141 struct nci_core_conn_create_cmd conn_cmd;
Ilan Elias2eb1dc12011-09-22 10:47:52 +0300142 struct nci_rf_disc_map_cmd cmd;
143 struct disc_map_config *cfg = cmd.mapping_configs;
144 __u8 *num = &cmd.num_mapping_configs;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300145 int i;
146
147 /* create static rf connection */
148 conn_cmd.target_handle = 0;
149 conn_cmd.num_target_specific_params = 0;
150 nci_send_cmd(ndev, NCI_OP_CORE_CONN_CREATE_CMD, 2, &conn_cmd);
151
152 /* set rf mapping configurations */
Ilan Elias2eb1dc12011-09-22 10:47:52 +0300153 *num = 0;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300154
155 /* by default mapping is set to NCI_RF_INTERFACE_FRAME */
156 for (i = 0; i < ndev->num_supported_rf_interfaces; i++) {
157 if (ndev->supported_rf_interfaces[i] ==
158 NCI_RF_INTERFACE_ISO_DEP) {
Ilan Elias2eb1dc12011-09-22 10:47:52 +0300159 cfg[*num].rf_protocol = NCI_RF_PROTOCOL_ISO_DEP;
160 cfg[*num].mode = NCI_DISC_MAP_MODE_BOTH;
161 cfg[*num].rf_interface_type = NCI_RF_INTERFACE_ISO_DEP;
162 (*num)++;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300163 } else if (ndev->supported_rf_interfaces[i] ==
164 NCI_RF_INTERFACE_NFC_DEP) {
Ilan Elias2eb1dc12011-09-22 10:47:52 +0300165 cfg[*num].rf_protocol = NCI_RF_PROTOCOL_NFC_DEP;
166 cfg[*num].mode = NCI_DISC_MAP_MODE_BOTH;
167 cfg[*num].rf_interface_type = NCI_RF_INTERFACE_NFC_DEP;
168 (*num)++;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300169 }
170
Ilan Elias2eb1dc12011-09-22 10:47:52 +0300171 if (*num == NCI_MAX_NUM_MAPPING_CONFIGS)
Ilan Elias6a2968a2011-09-18 11:19:35 +0300172 break;
173 }
174
175 nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_MAP_CMD,
Ilan Elias2eb1dc12011-09-22 10:47:52 +0300176 (1 + ((*num)*sizeof(struct disc_map_config))),
Ilan Elias6a2968a2011-09-18 11:19:35 +0300177 &cmd);
178}
179
180static void nci_rf_discover_req(struct nci_dev *ndev, unsigned long opt)
181{
182 struct nci_rf_disc_cmd cmd;
183 __u32 protocols = opt;
184
185 cmd.num_disc_configs = 0;
186
187 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
188 (protocols & NFC_PROTO_JEWEL_MASK
189 || protocols & NFC_PROTO_MIFARE_MASK
190 || protocols & NFC_PROTO_ISO14443_MASK
191 || protocols & NFC_PROTO_NFC_DEP_MASK)) {
192 cmd.disc_configs[cmd.num_disc_configs].type =
193 NCI_DISCOVERY_TYPE_POLL_A_PASSIVE;
194 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
195 cmd.num_disc_configs++;
196 }
197
198 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
199 (protocols & NFC_PROTO_ISO14443_MASK)) {
200 cmd.disc_configs[cmd.num_disc_configs].type =
201 NCI_DISCOVERY_TYPE_POLL_B_PASSIVE;
202 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
203 cmd.num_disc_configs++;
204 }
205
206 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
207 (protocols & NFC_PROTO_FELICA_MASK
208 || protocols & NFC_PROTO_NFC_DEP_MASK)) {
209 cmd.disc_configs[cmd.num_disc_configs].type =
210 NCI_DISCOVERY_TYPE_POLL_F_PASSIVE;
211 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
212 cmd.num_disc_configs++;
213 }
214
215 nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_CMD,
216 (1 + (cmd.num_disc_configs*sizeof(struct disc_config))),
217 &cmd);
218}
219
220static void nci_rf_deactivate_req(struct nci_dev *ndev, unsigned long opt)
221{
222 struct nci_rf_deactivate_cmd cmd;
223
224 cmd.type = NCI_DEACTIVATE_TYPE_IDLE_MODE;
225
226 nci_send_cmd(ndev, NCI_OP_RF_DEACTIVATE_CMD,
227 sizeof(struct nci_rf_deactivate_cmd),
228 &cmd);
229}
230
231static int nci_open_device(struct nci_dev *ndev)
232{
233 int rc = 0;
234
235 mutex_lock(&ndev->req_lock);
236
237 if (test_bit(NCI_UP, &ndev->flags)) {
238 rc = -EALREADY;
239 goto done;
240 }
241
242 if (ndev->ops->open(ndev)) {
243 rc = -EIO;
244 goto done;
245 }
246
247 atomic_set(&ndev->cmd_cnt, 1);
248
249 set_bit(NCI_INIT, &ndev->flags);
250
251 rc = __nci_request(ndev, nci_reset_req, 0,
252 msecs_to_jiffies(NCI_RESET_TIMEOUT));
253
254 if (!rc) {
255 rc = __nci_request(ndev, nci_init_req, 0,
256 msecs_to_jiffies(NCI_INIT_TIMEOUT));
257 }
258
259 if (!rc) {
260 rc = __nci_request(ndev, nci_init_complete_req, 0,
261 msecs_to_jiffies(NCI_INIT_TIMEOUT));
262 }
263
264 clear_bit(NCI_INIT, &ndev->flags);
265
266 if (!rc) {
267 set_bit(NCI_UP, &ndev->flags);
268 } else {
269 /* Init failed, cleanup */
270 skb_queue_purge(&ndev->cmd_q);
271 skb_queue_purge(&ndev->rx_q);
272 skb_queue_purge(&ndev->tx_q);
273
274 ndev->ops->close(ndev);
275 ndev->flags = 0;
276 }
277
278done:
279 mutex_unlock(&ndev->req_lock);
280 return rc;
281}
282
283static int nci_close_device(struct nci_dev *ndev)
284{
285 nci_req_cancel(ndev, ENODEV);
286 mutex_lock(&ndev->req_lock);
287
288 if (!test_and_clear_bit(NCI_UP, &ndev->flags)) {
289 del_timer_sync(&ndev->cmd_timer);
290 mutex_unlock(&ndev->req_lock);
291 return 0;
292 }
293
294 /* Drop RX and TX queues */
295 skb_queue_purge(&ndev->rx_q);
296 skb_queue_purge(&ndev->tx_q);
297
298 /* Flush RX and TX wq */
299 flush_workqueue(ndev->rx_wq);
300 flush_workqueue(ndev->tx_wq);
301
302 /* Reset device */
303 skb_queue_purge(&ndev->cmd_q);
304 atomic_set(&ndev->cmd_cnt, 1);
305
306 set_bit(NCI_INIT, &ndev->flags);
307 __nci_request(ndev, nci_reset_req, 0,
308 msecs_to_jiffies(NCI_RESET_TIMEOUT));
309 clear_bit(NCI_INIT, &ndev->flags);
310
311 /* Flush cmd wq */
312 flush_workqueue(ndev->cmd_wq);
313
314 /* After this point our queues are empty
315 * and no works are scheduled. */
316 ndev->ops->close(ndev);
317
318 /* Clear flags */
319 ndev->flags = 0;
320
321 mutex_unlock(&ndev->req_lock);
322
323 return 0;
324}
325
326/* NCI command timer function */
327static void nci_cmd_timer(unsigned long arg)
328{
329 struct nci_dev *ndev = (void *) arg;
330
331 nfc_dbg("entry");
332
333 atomic_set(&ndev->cmd_cnt, 1);
334 queue_work(ndev->cmd_wq, &ndev->cmd_work);
335}
336
337static int nci_dev_up(struct nfc_dev *nfc_dev)
338{
339 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
340
341 nfc_dbg("entry");
342
343 return nci_open_device(ndev);
344}
345
346static int nci_dev_down(struct nfc_dev *nfc_dev)
347{
348 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
349
350 nfc_dbg("entry");
351
352 return nci_close_device(ndev);
353}
354
355static int nci_start_poll(struct nfc_dev *nfc_dev, __u32 protocols)
356{
357 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
358 int rc;
359
360 nfc_dbg("entry");
361
362 if (test_bit(NCI_DISCOVERY, &ndev->flags)) {
363 nfc_err("unable to start poll, since poll is already active");
364 return -EBUSY;
365 }
366
Ilan Eliasde054792011-09-22 11:13:01 +0300367 if (ndev->target_active_prot) {
368 nfc_err("there is an active target");
369 return -EBUSY;
370 }
371
Ilan Elias6a2968a2011-09-18 11:19:35 +0300372 if (test_bit(NCI_POLL_ACTIVE, &ndev->flags)) {
Ilan Eliasde054792011-09-22 11:13:01 +0300373 nfc_dbg("target is active, implicitly deactivate...");
Ilan Elias6a2968a2011-09-18 11:19:35 +0300374
375 rc = nci_request(ndev, nci_rf_deactivate_req, 0,
376 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
377 if (rc)
378 return -EBUSY;
379 }
380
381 rc = nci_request(ndev, nci_rf_discover_req, protocols,
382 msecs_to_jiffies(NCI_RF_DISC_TIMEOUT));
383
384 if (!rc)
385 ndev->poll_prots = protocols;
386
387 return rc;
388}
389
390static void nci_stop_poll(struct nfc_dev *nfc_dev)
391{
392 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
393
394 nfc_dbg("entry");
395
396 if (!test_bit(NCI_DISCOVERY, &ndev->flags)) {
397 nfc_err("unable to stop poll, since poll is not active");
398 return;
399 }
400
401 nci_request(ndev, nci_rf_deactivate_req, 0,
402 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
403}
404
405static int nci_activate_target(struct nfc_dev *nfc_dev, __u32 target_idx,
406 __u32 protocol)
407{
408 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
409
410 nfc_dbg("entry, target_idx %d, protocol 0x%x", target_idx, protocol);
411
412 if (!test_bit(NCI_POLL_ACTIVE, &ndev->flags)) {
413 nfc_err("there is no available target to activate");
414 return -EINVAL;
415 }
416
417 if (ndev->target_active_prot) {
418 nfc_err("there is already an active target");
419 return -EBUSY;
420 }
421
422 if (!(ndev->target_available_prots & (1 << protocol))) {
423 nfc_err("target does not support the requested protocol 0x%x",
424 protocol);
425 return -EINVAL;
426 }
427
428 ndev->target_active_prot = protocol;
429 ndev->target_available_prots = 0;
430
431 return 0;
432}
433
434static void nci_deactivate_target(struct nfc_dev *nfc_dev, __u32 target_idx)
435{
436 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
437
438 nfc_dbg("entry, target_idx %d", target_idx);
439
440 if (!ndev->target_active_prot) {
441 nfc_err("unable to deactivate target, no active target");
442 return;
443 }
444
445 ndev->target_active_prot = 0;
446
447 if (test_bit(NCI_POLL_ACTIVE, &ndev->flags)) {
448 nci_request(ndev, nci_rf_deactivate_req, 0,
449 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
450 }
451}
452
453static int nci_data_exchange(struct nfc_dev *nfc_dev, __u32 target_idx,
454 struct sk_buff *skb,
455 data_exchange_cb_t cb,
456 void *cb_context)
457{
458 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
Ilan Elias38f04c62011-09-22 11:36:19 +0300459 int rc;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300460
461 nfc_dbg("entry, target_idx %d, len %d", target_idx, skb->len);
462
463 if (!ndev->target_active_prot) {
464 nfc_err("unable to exchange data, no active target");
465 return -EINVAL;
466 }
467
Ilan Elias38f04c62011-09-22 11:36:19 +0300468 if (test_and_set_bit(NCI_DATA_EXCHANGE, &ndev->flags))
469 return -EBUSY;
470
Ilan Elias6a2968a2011-09-18 11:19:35 +0300471 /* store cb and context to be used on receiving data */
472 ndev->data_exchange_cb = cb;
473 ndev->data_exchange_cb_context = cb_context;
474
Ilan Eliase8c0dac2011-11-09 12:09:14 +0200475 rc = nci_send_data(ndev, NCI_STATIC_RF_CONN_ID, skb);
Ilan Elias38f04c62011-09-22 11:36:19 +0300476 if (rc)
477 clear_bit(NCI_DATA_EXCHANGE, &ndev->flags);
478
479 return rc;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300480}
481
482static struct nfc_ops nci_nfc_ops = {
483 .dev_up = nci_dev_up,
484 .dev_down = nci_dev_down,
485 .start_poll = nci_start_poll,
486 .stop_poll = nci_stop_poll,
487 .activate_target = nci_activate_target,
488 .deactivate_target = nci_deactivate_target,
489 .data_exchange = nci_data_exchange,
490};
491
492/* ---- Interface to NCI drivers ---- */
493
494/**
495 * nci_allocate_device - allocate a new nci device
496 *
497 * @ops: device operations
498 * @supported_protocols: NFC protocols supported by the device
499 */
500struct nci_dev *nci_allocate_device(struct nci_ops *ops,
501 __u32 supported_protocols,
502 int tx_headroom,
503 int tx_tailroom)
504{
Dan Carpenter8ebafde2011-09-23 09:14:35 +0300505 struct nci_dev *ndev;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300506
507 nfc_dbg("entry, supported_protocols 0x%x", supported_protocols);
508
509 if (!ops->open || !ops->close || !ops->send)
Dan Carpenter8ebafde2011-09-23 09:14:35 +0300510 return NULL;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300511
512 if (!supported_protocols)
Dan Carpenter8ebafde2011-09-23 09:14:35 +0300513 return NULL;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300514
515 ndev = kzalloc(sizeof(struct nci_dev), GFP_KERNEL);
516 if (!ndev)
Dan Carpenter8ebafde2011-09-23 09:14:35 +0300517 return NULL;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300518
519 ndev->ops = ops;
520 ndev->tx_headroom = tx_headroom;
521 ndev->tx_tailroom = tx_tailroom;
522
523 ndev->nfc_dev = nfc_allocate_device(&nci_nfc_ops,
524 supported_protocols,
525 tx_headroom + NCI_DATA_HDR_SIZE,
526 tx_tailroom);
527 if (!ndev->nfc_dev)
528 goto free_exit;
529
530 nfc_set_drvdata(ndev->nfc_dev, ndev);
531
Dan Carpenter8ebafde2011-09-23 09:14:35 +0300532 return ndev;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300533
534free_exit:
535 kfree(ndev);
Dan Carpenter8ebafde2011-09-23 09:14:35 +0300536 return NULL;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300537}
538EXPORT_SYMBOL(nci_allocate_device);
539
540/**
541 * nci_free_device - deallocate nci device
542 *
543 * @ndev: The nci device to deallocate
544 */
545void nci_free_device(struct nci_dev *ndev)
546{
547 nfc_dbg("entry");
548
549 nfc_free_device(ndev->nfc_dev);
550 kfree(ndev);
551}
552EXPORT_SYMBOL(nci_free_device);
553
554/**
555 * nci_register_device - register a nci device in the nfc subsystem
556 *
557 * @dev: The nci device to register
558 */
559int nci_register_device(struct nci_dev *ndev)
560{
561 int rc;
562 struct device *dev = &ndev->nfc_dev->dev;
563 char name[32];
564
565 nfc_dbg("entry");
566
567 rc = nfc_register_device(ndev->nfc_dev);
568 if (rc)
569 goto exit;
570
571 ndev->flags = 0;
572
573 INIT_WORK(&ndev->cmd_work, nci_cmd_work);
574 snprintf(name, sizeof(name), "%s_nci_cmd_wq", dev_name(dev));
575 ndev->cmd_wq = create_singlethread_workqueue(name);
576 if (!ndev->cmd_wq) {
577 rc = -ENOMEM;
578 goto unreg_exit;
579 }
580
581 INIT_WORK(&ndev->rx_work, nci_rx_work);
582 snprintf(name, sizeof(name), "%s_nci_rx_wq", dev_name(dev));
583 ndev->rx_wq = create_singlethread_workqueue(name);
584 if (!ndev->rx_wq) {
585 rc = -ENOMEM;
586 goto destroy_cmd_wq_exit;
587 }
588
589 INIT_WORK(&ndev->tx_work, nci_tx_work);
590 snprintf(name, sizeof(name), "%s_nci_tx_wq", dev_name(dev));
591 ndev->tx_wq = create_singlethread_workqueue(name);
592 if (!ndev->tx_wq) {
593 rc = -ENOMEM;
594 goto destroy_rx_wq_exit;
595 }
596
597 skb_queue_head_init(&ndev->cmd_q);
598 skb_queue_head_init(&ndev->rx_q);
599 skb_queue_head_init(&ndev->tx_q);
600
601 setup_timer(&ndev->cmd_timer, nci_cmd_timer,
602 (unsigned long) ndev);
603
604 mutex_init(&ndev->req_lock);
605
606 goto exit;
607
608destroy_rx_wq_exit:
609 destroy_workqueue(ndev->rx_wq);
610
611destroy_cmd_wq_exit:
612 destroy_workqueue(ndev->cmd_wq);
613
614unreg_exit:
615 nfc_unregister_device(ndev->nfc_dev);
616
617exit:
618 return rc;
619}
620EXPORT_SYMBOL(nci_register_device);
621
622/**
623 * nci_unregister_device - unregister a nci device in the nfc subsystem
624 *
625 * @dev: The nci device to unregister
626 */
627void nci_unregister_device(struct nci_dev *ndev)
628{
629 nfc_dbg("entry");
630
631 nci_close_device(ndev);
632
633 destroy_workqueue(ndev->cmd_wq);
634 destroy_workqueue(ndev->rx_wq);
635 destroy_workqueue(ndev->tx_wq);
636
637 nfc_unregister_device(ndev->nfc_dev);
638}
639EXPORT_SYMBOL(nci_unregister_device);
640
641/**
642 * nci_recv_frame - receive frame from NCI drivers
643 *
644 * @skb: The sk_buff to receive
645 */
646int nci_recv_frame(struct sk_buff *skb)
647{
648 struct nci_dev *ndev = (struct nci_dev *) skb->dev;
649
650 nfc_dbg("entry, len %d", skb->len);
651
652 if (!ndev || (!test_bit(NCI_UP, &ndev->flags)
653 && !test_bit(NCI_INIT, &ndev->flags))) {
654 kfree_skb(skb);
655 return -ENXIO;
656 }
657
658 /* Queue frame for rx worker thread */
659 skb_queue_tail(&ndev->rx_q, skb);
660 queue_work(ndev->rx_wq, &ndev->rx_work);
661
662 return 0;
663}
664EXPORT_SYMBOL(nci_recv_frame);
665
666static int nci_send_frame(struct sk_buff *skb)
667{
668 struct nci_dev *ndev = (struct nci_dev *) skb->dev;
669
670 nfc_dbg("entry, len %d", skb->len);
671
672 if (!ndev) {
673 kfree_skb(skb);
674 return -ENODEV;
675 }
676
677 /* Get rid of skb owner, prior to sending to the driver. */
678 skb_orphan(skb);
679
680 return ndev->ops->send(skb);
681}
682
683/* Send NCI command */
684int nci_send_cmd(struct nci_dev *ndev, __u16 opcode, __u8 plen, void *payload)
685{
686 struct nci_ctrl_hdr *hdr;
687 struct sk_buff *skb;
688
689 nfc_dbg("entry, opcode 0x%x, plen %d", opcode, plen);
690
691 skb = nci_skb_alloc(ndev, (NCI_CTRL_HDR_SIZE + plen), GFP_KERNEL);
692 if (!skb) {
693 nfc_err("no memory for command");
694 return -ENOMEM;
695 }
696
697 hdr = (struct nci_ctrl_hdr *) skb_put(skb, NCI_CTRL_HDR_SIZE);
698 hdr->gid = nci_opcode_gid(opcode);
699 hdr->oid = nci_opcode_oid(opcode);
700 hdr->plen = plen;
701
702 nci_mt_set((__u8 *)hdr, NCI_MT_CMD_PKT);
703 nci_pbf_set((__u8 *)hdr, NCI_PBF_LAST);
704
705 if (plen)
706 memcpy(skb_put(skb, plen), payload, plen);
707
708 skb->dev = (void *) ndev;
709
710 skb_queue_tail(&ndev->cmd_q, skb);
711 queue_work(ndev->cmd_wq, &ndev->cmd_work);
712
713 return 0;
714}
715
716/* ---- NCI TX Data worker thread ---- */
717
718static void nci_tx_work(struct work_struct *work)
719{
720 struct nci_dev *ndev = container_of(work, struct nci_dev, tx_work);
721 struct sk_buff *skb;
722
723 nfc_dbg("entry, credits_cnt %d", atomic_read(&ndev->credits_cnt));
724
725 /* Send queued tx data */
726 while (atomic_read(&ndev->credits_cnt)) {
727 skb = skb_dequeue(&ndev->tx_q);
728 if (!skb)
729 return;
730
731 atomic_dec(&ndev->credits_cnt);
732
733 nfc_dbg("NCI TX: MT=data, PBF=%d, conn_id=%d, plen=%d",
734 nci_pbf(skb->data),
735 nci_conn_id(skb->data),
736 nci_plen(skb->data));
737
738 nci_send_frame(skb);
739 }
740}
741
742/* ----- NCI RX worker thread (data & control) ----- */
743
744static void nci_rx_work(struct work_struct *work)
745{
746 struct nci_dev *ndev = container_of(work, struct nci_dev, rx_work);
747 struct sk_buff *skb;
748
749 while ((skb = skb_dequeue(&ndev->rx_q))) {
750 /* Process frame */
751 switch (nci_mt(skb->data)) {
752 case NCI_MT_RSP_PKT:
753 nci_rsp_packet(ndev, skb);
754 break;
755
756 case NCI_MT_NTF_PKT:
757 nci_ntf_packet(ndev, skb);
758 break;
759
760 case NCI_MT_DATA_PKT:
761 nci_rx_data_packet(ndev, skb);
762 break;
763
764 default:
765 nfc_err("unknown MT 0x%x", nci_mt(skb->data));
766 kfree_skb(skb);
767 break;
768 }
769 }
770}
771
772/* ----- NCI TX CMD worker thread ----- */
773
774static void nci_cmd_work(struct work_struct *work)
775{
776 struct nci_dev *ndev = container_of(work, struct nci_dev, cmd_work);
777 struct sk_buff *skb;
778
779 nfc_dbg("entry, cmd_cnt %d", atomic_read(&ndev->cmd_cnt));
780
781 /* Send queued command */
782 if (atomic_read(&ndev->cmd_cnt)) {
783 skb = skb_dequeue(&ndev->cmd_q);
784 if (!skb)
785 return;
786
787 atomic_dec(&ndev->cmd_cnt);
788
789 nfc_dbg("NCI TX: MT=cmd, PBF=%d, GID=0x%x, OID=0x%x, plen=%d",
790 nci_pbf(skb->data),
791 nci_opcode_gid(nci_opcode(skb->data)),
792 nci_opcode_oid(nci_opcode(skb->data)),
793 nci_plen(skb->data));
794
795 nci_send_frame(skb);
796
797 mod_timer(&ndev->cmd_timer,
798 jiffies + msecs_to_jiffies(NCI_CMD_TIMEOUT));
799 }
800}