blob: 629b768459736f15f814d7fce81c30efa88fb149 [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
Samuel Ortiz52858b52011-12-14 16:43:05 +010028#define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
Joe Perchesed1e0ad2011-11-29 11:37:32 -080029
Ilan Elias6a2968a2011-09-18 11:19:35 +030030#include <linux/types.h>
31#include <linux/workqueue.h>
32#include <linux/completion.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040033#include <linux/export.h>
Ilan Elias6a2968a2011-09-18 11:19:35 +030034#include <linux/sched.h>
35#include <linux/bitops.h>
36#include <linux/skbuff.h>
37
38#include "../nfc.h"
39#include <net/nfc/nci.h>
40#include <net/nfc/nci_core.h>
41#include <linux/nfc.h>
42
43static void nci_cmd_work(struct work_struct *work);
44static void nci_rx_work(struct work_struct *work);
45static void nci_tx_work(struct work_struct *work);
46
47/* ---- NCI requests ---- */
48
49void nci_req_complete(struct nci_dev *ndev, int result)
50{
51 if (ndev->req_status == NCI_REQ_PEND) {
52 ndev->req_result = result;
53 ndev->req_status = NCI_REQ_DONE;
54 complete(&ndev->req_completion);
55 }
56}
57
58static void nci_req_cancel(struct nci_dev *ndev, int err)
59{
60 if (ndev->req_status == NCI_REQ_PEND) {
61 ndev->req_result = err;
62 ndev->req_status = NCI_REQ_CANCELED;
63 complete(&ndev->req_completion);
64 }
65}
66
67/* Execute request and wait for completion. */
68static int __nci_request(struct nci_dev *ndev,
69 void (*req)(struct nci_dev *ndev, unsigned long opt),
70 unsigned long opt,
71 __u32 timeout)
72{
73 int rc = 0;
Dan Carpenterf8c141c2011-12-09 09:35:39 +030074 long completion_rc;
Ilan Elias6a2968a2011-09-18 11:19:35 +030075
76 ndev->req_status = NCI_REQ_PEND;
77
78 init_completion(&ndev->req_completion);
79 req(ndev, opt);
80 completion_rc = wait_for_completion_interruptible_timeout(
81 &ndev->req_completion,
82 timeout);
83
Joe Perches20c239c2011-11-29 11:37:33 -080084 pr_debug("wait_for_completion return %ld\n", completion_rc);
Ilan Elias6a2968a2011-09-18 11:19:35 +030085
86 if (completion_rc > 0) {
87 switch (ndev->req_status) {
88 case NCI_REQ_DONE:
89 rc = nci_to_errno(ndev->req_result);
90 break;
91
92 case NCI_REQ_CANCELED:
93 rc = -ndev->req_result;
94 break;
95
96 default:
97 rc = -ETIMEDOUT;
98 break;
99 }
100 } else {
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800101 pr_err("wait_for_completion_interruptible_timeout failed %ld\n",
102 completion_rc);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300103
104 rc = ((completion_rc == 0) ? (-ETIMEDOUT) : (completion_rc));
105 }
106
107 ndev->req_status = ndev->req_result = 0;
108
109 return rc;
110}
111
112static inline int nci_request(struct nci_dev *ndev,
113 void (*req)(struct nci_dev *ndev, unsigned long opt),
114 unsigned long opt, __u32 timeout)
115{
116 int rc;
117
118 if (!test_bit(NCI_UP, &ndev->flags))
119 return -ENETDOWN;
120
121 /* Serialize all requests */
122 mutex_lock(&ndev->req_lock);
123 rc = __nci_request(ndev, req, opt, timeout);
124 mutex_unlock(&ndev->req_lock);
125
126 return rc;
127}
128
129static void nci_reset_req(struct nci_dev *ndev, unsigned long opt)
130{
Ilan Eliase8c0dac2011-11-09 12:09:14 +0200131 struct nci_core_reset_cmd cmd;
132
133 cmd.reset_type = NCI_RESET_TYPE_RESET_CONFIG;
134 nci_send_cmd(ndev, NCI_OP_CORE_RESET_CMD, 1, &cmd);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300135}
136
137static void nci_init_req(struct nci_dev *ndev, unsigned long opt)
138{
139 nci_send_cmd(ndev, NCI_OP_CORE_INIT_CMD, 0, NULL);
140}
141
142static void nci_init_complete_req(struct nci_dev *ndev, unsigned long opt)
143{
Ilan Elias2eb1dc12011-09-22 10:47:52 +0300144 struct nci_rf_disc_map_cmd cmd;
145 struct disc_map_config *cfg = cmd.mapping_configs;
146 __u8 *num = &cmd.num_mapping_configs;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300147 int i;
148
Ilan Elias6a2968a2011-09-18 11:19:35 +0300149 /* set rf mapping configurations */
Ilan Elias2eb1dc12011-09-22 10:47:52 +0300150 *num = 0;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300151
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) {
Ilan Elias2eb1dc12011-09-22 10:47:52 +0300156 cfg[*num].rf_protocol = NCI_RF_PROTOCOL_ISO_DEP;
Ilan Elias637d85a2011-12-20 16:57:40 +0200157 cfg[*num].mode = NCI_DISC_MAP_MODE_POLL |
158 NCI_DISC_MAP_MODE_LISTEN;
159 cfg[*num].rf_interface = NCI_RF_INTERFACE_ISO_DEP;
Ilan Elias2eb1dc12011-09-22 10:47:52 +0300160 (*num)++;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300161 } else if (ndev->supported_rf_interfaces[i] ==
162 NCI_RF_INTERFACE_NFC_DEP) {
Ilan Elias2eb1dc12011-09-22 10:47:52 +0300163 cfg[*num].rf_protocol = NCI_RF_PROTOCOL_NFC_DEP;
Ilan Elias637d85a2011-12-20 16:57:40 +0200164 cfg[*num].mode = NCI_DISC_MAP_MODE_POLL |
165 NCI_DISC_MAP_MODE_LISTEN;
166 cfg[*num].rf_interface = NCI_RF_INTERFACE_NFC_DEP;
Ilan Elias2eb1dc12011-09-22 10:47:52 +0300167 (*num)++;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300168 }
169
Ilan Elias2eb1dc12011-09-22 10:47:52 +0300170 if (*num == NCI_MAX_NUM_MAPPING_CONFIGS)
Ilan Elias6a2968a2011-09-18 11:19:35 +0300171 break;
172 }
173
174 nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_MAP_CMD,
Ilan Elias2eb1dc12011-09-22 10:47:52 +0300175 (1 + ((*num)*sizeof(struct disc_map_config))),
Ilan Elias6a2968a2011-09-18 11:19:35 +0300176 &cmd);
177}
178
179static void nci_rf_discover_req(struct nci_dev *ndev, unsigned long opt)
180{
181 struct nci_rf_disc_cmd cmd;
182 __u32 protocols = opt;
183
184 cmd.num_disc_configs = 0;
185
186 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
187 (protocols & NFC_PROTO_JEWEL_MASK
188 || protocols & NFC_PROTO_MIFARE_MASK
189 || protocols & NFC_PROTO_ISO14443_MASK
190 || protocols & NFC_PROTO_NFC_DEP_MASK)) {
Ilan Elias637d85a2011-12-20 16:57:40 +0200191 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
192 NCI_NFC_A_PASSIVE_POLL_MODE;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300193 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
194 cmd.num_disc_configs++;
195 }
196
197 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
198 (protocols & NFC_PROTO_ISO14443_MASK)) {
Ilan Elias637d85a2011-12-20 16:57:40 +0200199 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
200 NCI_NFC_B_PASSIVE_POLL_MODE;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300201 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
202 cmd.num_disc_configs++;
203 }
204
205 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
206 (protocols & NFC_PROTO_FELICA_MASK
207 || protocols & NFC_PROTO_NFC_DEP_MASK)) {
Ilan Elias637d85a2011-12-20 16:57:40 +0200208 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
209 NCI_NFC_F_PASSIVE_POLL_MODE;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300210 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
211 cmd.num_disc_configs++;
212 }
213
214 nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_CMD,
215 (1 + (cmd.num_disc_configs*sizeof(struct disc_config))),
216 &cmd);
217}
218
219static void nci_rf_deactivate_req(struct nci_dev *ndev, unsigned long opt)
220{
221 struct nci_rf_deactivate_cmd cmd;
222
223 cmd.type = NCI_DEACTIVATE_TYPE_IDLE_MODE;
224
225 nci_send_cmd(ndev, NCI_OP_RF_DEACTIVATE_CMD,
226 sizeof(struct nci_rf_deactivate_cmd),
227 &cmd);
228}
229
230static int nci_open_device(struct nci_dev *ndev)
231{
232 int rc = 0;
233
234 mutex_lock(&ndev->req_lock);
235
236 if (test_bit(NCI_UP, &ndev->flags)) {
237 rc = -EALREADY;
238 goto done;
239 }
240
241 if (ndev->ops->open(ndev)) {
242 rc = -EIO;
243 goto done;
244 }
245
246 atomic_set(&ndev->cmd_cnt, 1);
247
248 set_bit(NCI_INIT, &ndev->flags);
249
250 rc = __nci_request(ndev, nci_reset_req, 0,
251 msecs_to_jiffies(NCI_RESET_TIMEOUT));
252
253 if (!rc) {
254 rc = __nci_request(ndev, nci_init_req, 0,
255 msecs_to_jiffies(NCI_INIT_TIMEOUT));
256 }
257
258 if (!rc) {
259 rc = __nci_request(ndev, nci_init_complete_req, 0,
260 msecs_to_jiffies(NCI_INIT_TIMEOUT));
261 }
262
263 clear_bit(NCI_INIT, &ndev->flags);
264
265 if (!rc) {
266 set_bit(NCI_UP, &ndev->flags);
Ilan Elias8939e472012-01-18 13:16:12 +0200267 atomic_set(&ndev->state, NCI_IDLE);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300268 } 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);
Ilan Eliasc4bf98b2012-01-17 12:03:50 +0200290 del_timer_sync(&ndev->data_timer);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300291 mutex_unlock(&ndev->req_lock);
292 return 0;
293 }
294
295 /* Drop RX and TX queues */
296 skb_queue_purge(&ndev->rx_q);
297 skb_queue_purge(&ndev->tx_q);
298
299 /* Flush RX and TX wq */
300 flush_workqueue(ndev->rx_wq);
301 flush_workqueue(ndev->tx_wq);
302
303 /* Reset device */
304 skb_queue_purge(&ndev->cmd_q);
305 atomic_set(&ndev->cmd_cnt, 1);
306
307 set_bit(NCI_INIT, &ndev->flags);
308 __nci_request(ndev, nci_reset_req, 0,
309 msecs_to_jiffies(NCI_RESET_TIMEOUT));
310 clear_bit(NCI_INIT, &ndev->flags);
311
312 /* Flush cmd wq */
313 flush_workqueue(ndev->cmd_wq);
314
315 /* After this point our queues are empty
316 * and no works are scheduled. */
317 ndev->ops->close(ndev);
318
319 /* Clear flags */
320 ndev->flags = 0;
321
322 mutex_unlock(&ndev->req_lock);
323
324 return 0;
325}
326
327/* NCI command timer function */
328static void nci_cmd_timer(unsigned long arg)
329{
330 struct nci_dev *ndev = (void *) arg;
331
Ilan Elias6a2968a2011-09-18 11:19:35 +0300332 atomic_set(&ndev->cmd_cnt, 1);
333 queue_work(ndev->cmd_wq, &ndev->cmd_work);
334}
335
Ilan Eliasc4bf98b2012-01-17 12:03:50 +0200336/* NCI data exchange timer function */
337static void nci_data_timer(unsigned long arg)
338{
339 struct nci_dev *ndev = (void *) arg;
340
341 set_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags);
342 queue_work(ndev->rx_wq, &ndev->rx_work);
343}
344
Ilan Elias6a2968a2011-09-18 11:19:35 +0300345static int nci_dev_up(struct nfc_dev *nfc_dev)
346{
347 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
348
Ilan Elias6a2968a2011-09-18 11:19:35 +0300349 return nci_open_device(ndev);
350}
351
352static int nci_dev_down(struct nfc_dev *nfc_dev)
353{
354 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
355
Ilan Elias6a2968a2011-09-18 11:19:35 +0300356 return nci_close_device(ndev);
357}
358
359static int nci_start_poll(struct nfc_dev *nfc_dev, __u32 protocols)
360{
361 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
362 int rc;
363
Ilan Elias8939e472012-01-18 13:16:12 +0200364 if (atomic_read(&ndev->state) == NCI_DISCOVERY) {
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800365 pr_err("unable to start poll, since poll is already active\n");
Ilan Elias6a2968a2011-09-18 11:19:35 +0300366 return -EBUSY;
367 }
368
Ilan Eliasde054792011-09-22 11:13:01 +0300369 if (ndev->target_active_prot) {
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800370 pr_err("there is an active target\n");
Ilan Eliasde054792011-09-22 11:13:01 +0300371 return -EBUSY;
372 }
373
Ilan Elias8939e472012-01-18 13:16:12 +0200374 if (atomic_read(&ndev->state) == NCI_POLL_ACTIVE) {
Joe Perches20c239c2011-11-29 11:37:33 -0800375 pr_debug("target is active, implicitly deactivate...\n");
Ilan Elias6a2968a2011-09-18 11:19:35 +0300376
377 rc = nci_request(ndev, nci_rf_deactivate_req, 0,
378 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
379 if (rc)
380 return -EBUSY;
381 }
382
383 rc = nci_request(ndev, nci_rf_discover_req, protocols,
384 msecs_to_jiffies(NCI_RF_DISC_TIMEOUT));
385
386 if (!rc)
387 ndev->poll_prots = protocols;
388
389 return rc;
390}
391
392static void nci_stop_poll(struct nfc_dev *nfc_dev)
393{
394 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
395
Ilan Elias8939e472012-01-18 13:16:12 +0200396 if (atomic_read(&ndev->state) != NCI_DISCOVERY) {
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800397 pr_err("unable to stop poll, since poll is not active\n");
Ilan Elias6a2968a2011-09-18 11:19:35 +0300398 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
Joe Perches24bf3302011-11-29 11:37:35 -0800410 pr_debug("target_idx %d, protocol 0x%x\n", target_idx, protocol);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300411
Ilan Elias8939e472012-01-18 13:16:12 +0200412 if (atomic_read(&ndev->state) != NCI_POLL_ACTIVE) {
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800413 pr_err("there is no available target to activate\n");
Ilan Elias6a2968a2011-09-18 11:19:35 +0300414 return -EINVAL;
415 }
416
417 if (ndev->target_active_prot) {
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800418 pr_err("there is already an active target\n");
Ilan Elias6a2968a2011-09-18 11:19:35 +0300419 return -EBUSY;
420 }
421
422 if (!(ndev->target_available_prots & (1 << protocol))) {
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800423 pr_err("target does not support the requested protocol 0x%x\n",
424 protocol);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300425 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
Joe Perches24bf3302011-11-29 11:37:35 -0800438 pr_debug("target_idx %d\n", target_idx);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300439
440 if (!ndev->target_active_prot) {
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800441 pr_err("unable to deactivate target, no active target\n");
Ilan Elias6a2968a2011-09-18 11:19:35 +0300442 return;
443 }
444
445 ndev->target_active_prot = 0;
446
Ilan Elias8939e472012-01-18 13:16:12 +0200447 if (atomic_read(&ndev->state) == NCI_POLL_ACTIVE) {
Ilan Elias6a2968a2011-09-18 11:19:35 +0300448 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
Joe Perches24bf3302011-11-29 11:37:35 -0800461 pr_debug("target_idx %d, len %d\n", target_idx, skb->len);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300462
463 if (!ndev->target_active_prot) {
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800464 pr_err("unable to exchange data, no active target\n");
Ilan Elias6a2968a2011-09-18 11:19:35 +0300465 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
Joe Perches24bf3302011-11-29 11:37:35 -0800507 pr_debug("supported_protocols 0x%x\n", supported_protocols);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300508
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{
Ilan Elias6a2968a2011-09-18 11:19:35 +0300547 nfc_free_device(ndev->nfc_dev);
548 kfree(ndev);
549}
550EXPORT_SYMBOL(nci_free_device);
551
552/**
553 * nci_register_device - register a nci device in the nfc subsystem
554 *
555 * @dev: The nci device to register
556 */
557int nci_register_device(struct nci_dev *ndev)
558{
559 int rc;
560 struct device *dev = &ndev->nfc_dev->dev;
561 char name[32];
562
Ilan Elias6a2968a2011-09-18 11:19:35 +0300563 rc = nfc_register_device(ndev->nfc_dev);
564 if (rc)
565 goto exit;
566
567 ndev->flags = 0;
568
569 INIT_WORK(&ndev->cmd_work, nci_cmd_work);
570 snprintf(name, sizeof(name), "%s_nci_cmd_wq", dev_name(dev));
571 ndev->cmd_wq = create_singlethread_workqueue(name);
572 if (!ndev->cmd_wq) {
573 rc = -ENOMEM;
574 goto unreg_exit;
575 }
576
577 INIT_WORK(&ndev->rx_work, nci_rx_work);
578 snprintf(name, sizeof(name), "%s_nci_rx_wq", dev_name(dev));
579 ndev->rx_wq = create_singlethread_workqueue(name);
580 if (!ndev->rx_wq) {
581 rc = -ENOMEM;
582 goto destroy_cmd_wq_exit;
583 }
584
585 INIT_WORK(&ndev->tx_work, nci_tx_work);
586 snprintf(name, sizeof(name), "%s_nci_tx_wq", dev_name(dev));
587 ndev->tx_wq = create_singlethread_workqueue(name);
588 if (!ndev->tx_wq) {
589 rc = -ENOMEM;
590 goto destroy_rx_wq_exit;
591 }
592
593 skb_queue_head_init(&ndev->cmd_q);
594 skb_queue_head_init(&ndev->rx_q);
595 skb_queue_head_init(&ndev->tx_q);
596
597 setup_timer(&ndev->cmd_timer, nci_cmd_timer,
598 (unsigned long) ndev);
Ilan Eliasc4bf98b2012-01-17 12:03:50 +0200599 setup_timer(&ndev->data_timer, nci_data_timer,
600 (unsigned long) ndev);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300601
602 mutex_init(&ndev->req_lock);
603
604 goto exit;
605
606destroy_rx_wq_exit:
607 destroy_workqueue(ndev->rx_wq);
608
609destroy_cmd_wq_exit:
610 destroy_workqueue(ndev->cmd_wq);
611
612unreg_exit:
613 nfc_unregister_device(ndev->nfc_dev);
614
615exit:
616 return rc;
617}
618EXPORT_SYMBOL(nci_register_device);
619
620/**
621 * nci_unregister_device - unregister a nci device in the nfc subsystem
622 *
623 * @dev: The nci device to unregister
624 */
625void nci_unregister_device(struct nci_dev *ndev)
626{
Ilan Elias6a2968a2011-09-18 11:19:35 +0300627 nci_close_device(ndev);
628
629 destroy_workqueue(ndev->cmd_wq);
630 destroy_workqueue(ndev->rx_wq);
631 destroy_workqueue(ndev->tx_wq);
632
633 nfc_unregister_device(ndev->nfc_dev);
634}
635EXPORT_SYMBOL(nci_unregister_device);
636
637/**
638 * nci_recv_frame - receive frame from NCI drivers
639 *
640 * @skb: The sk_buff to receive
641 */
642int nci_recv_frame(struct sk_buff *skb)
643{
644 struct nci_dev *ndev = (struct nci_dev *) skb->dev;
645
Joe Perches24bf3302011-11-29 11:37:35 -0800646 pr_debug("len %d\n", skb->len);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300647
648 if (!ndev || (!test_bit(NCI_UP, &ndev->flags)
649 && !test_bit(NCI_INIT, &ndev->flags))) {
650 kfree_skb(skb);
651 return -ENXIO;
652 }
653
654 /* Queue frame for rx worker thread */
655 skb_queue_tail(&ndev->rx_q, skb);
656 queue_work(ndev->rx_wq, &ndev->rx_work);
657
658 return 0;
659}
660EXPORT_SYMBOL(nci_recv_frame);
661
662static int nci_send_frame(struct sk_buff *skb)
663{
664 struct nci_dev *ndev = (struct nci_dev *) skb->dev;
665
Joe Perches24bf3302011-11-29 11:37:35 -0800666 pr_debug("len %d\n", skb->len);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300667
668 if (!ndev) {
669 kfree_skb(skb);
670 return -ENODEV;
671 }
672
673 /* Get rid of skb owner, prior to sending to the driver. */
674 skb_orphan(skb);
675
676 return ndev->ops->send(skb);
677}
678
679/* Send NCI command */
680int nci_send_cmd(struct nci_dev *ndev, __u16 opcode, __u8 plen, void *payload)
681{
682 struct nci_ctrl_hdr *hdr;
683 struct sk_buff *skb;
684
Joe Perches24bf3302011-11-29 11:37:35 -0800685 pr_debug("opcode 0x%x, plen %d\n", opcode, plen);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300686
687 skb = nci_skb_alloc(ndev, (NCI_CTRL_HDR_SIZE + plen), GFP_KERNEL);
688 if (!skb) {
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800689 pr_err("no memory for command\n");
Ilan Elias6a2968a2011-09-18 11:19:35 +0300690 return -ENOMEM;
691 }
692
693 hdr = (struct nci_ctrl_hdr *) skb_put(skb, NCI_CTRL_HDR_SIZE);
694 hdr->gid = nci_opcode_gid(opcode);
695 hdr->oid = nci_opcode_oid(opcode);
696 hdr->plen = plen;
697
698 nci_mt_set((__u8 *)hdr, NCI_MT_CMD_PKT);
699 nci_pbf_set((__u8 *)hdr, NCI_PBF_LAST);
700
701 if (plen)
702 memcpy(skb_put(skb, plen), payload, plen);
703
704 skb->dev = (void *) ndev;
705
706 skb_queue_tail(&ndev->cmd_q, skb);
707 queue_work(ndev->cmd_wq, &ndev->cmd_work);
708
709 return 0;
710}
711
712/* ---- NCI TX Data worker thread ---- */
713
714static void nci_tx_work(struct work_struct *work)
715{
716 struct nci_dev *ndev = container_of(work, struct nci_dev, tx_work);
717 struct sk_buff *skb;
718
Joe Perches24bf3302011-11-29 11:37:35 -0800719 pr_debug("credits_cnt %d\n", atomic_read(&ndev->credits_cnt));
Ilan Elias6a2968a2011-09-18 11:19:35 +0300720
721 /* Send queued tx data */
722 while (atomic_read(&ndev->credits_cnt)) {
723 skb = skb_dequeue(&ndev->tx_q);
724 if (!skb)
725 return;
726
Ilan Eliasdb98c822011-11-09 12:09:16 +0200727 /* Check if data flow control is used */
728 if (atomic_read(&ndev->credits_cnt) !=
729 NCI_DATA_FLOW_CONTROL_NOT_USED)
730 atomic_dec(&ndev->credits_cnt);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300731
Joe Perches20c239c2011-11-29 11:37:33 -0800732 pr_debug("NCI TX: MT=data, PBF=%d, conn_id=%d, plen=%d\n",
733 nci_pbf(skb->data),
734 nci_conn_id(skb->data),
735 nci_plen(skb->data));
Ilan Elias6a2968a2011-09-18 11:19:35 +0300736
737 nci_send_frame(skb);
Ilan Eliasc4bf98b2012-01-17 12:03:50 +0200738
739 mod_timer(&ndev->data_timer,
740 jiffies + msecs_to_jiffies(NCI_DATA_TIMEOUT));
Ilan Elias6a2968a2011-09-18 11:19:35 +0300741 }
742}
743
744/* ----- NCI RX worker thread (data & control) ----- */
745
746static void nci_rx_work(struct work_struct *work)
747{
748 struct nci_dev *ndev = container_of(work, struct nci_dev, rx_work);
749 struct sk_buff *skb;
750
751 while ((skb = skb_dequeue(&ndev->rx_q))) {
752 /* Process frame */
753 switch (nci_mt(skb->data)) {
754 case NCI_MT_RSP_PKT:
755 nci_rsp_packet(ndev, skb);
756 break;
757
758 case NCI_MT_NTF_PKT:
759 nci_ntf_packet(ndev, skb);
760 break;
761
762 case NCI_MT_DATA_PKT:
763 nci_rx_data_packet(ndev, skb);
764 break;
765
766 default:
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800767 pr_err("unknown MT 0x%x\n", nci_mt(skb->data));
Ilan Elias6a2968a2011-09-18 11:19:35 +0300768 kfree_skb(skb);
769 break;
770 }
771 }
Ilan Eliasc4bf98b2012-01-17 12:03:50 +0200772
773 /* check if a data exchange timout has occurred */
774 if (test_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags)) {
775 /* complete the data exchange transaction, if exists */
776 if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags))
777 nci_data_exchange_complete(ndev, NULL, -ETIMEDOUT);
778
779 clear_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags);
780 }
Ilan Elias6a2968a2011-09-18 11:19:35 +0300781}
782
783/* ----- NCI TX CMD worker thread ----- */
784
785static void nci_cmd_work(struct work_struct *work)
786{
787 struct nci_dev *ndev = container_of(work, struct nci_dev, cmd_work);
788 struct sk_buff *skb;
789
Joe Perches24bf3302011-11-29 11:37:35 -0800790 pr_debug("cmd_cnt %d\n", atomic_read(&ndev->cmd_cnt));
Ilan Elias6a2968a2011-09-18 11:19:35 +0300791
792 /* Send queued command */
793 if (atomic_read(&ndev->cmd_cnt)) {
794 skb = skb_dequeue(&ndev->cmd_q);
795 if (!skb)
796 return;
797
798 atomic_dec(&ndev->cmd_cnt);
799
Joe Perches20c239c2011-11-29 11:37:33 -0800800 pr_debug("NCI TX: MT=cmd, PBF=%d, GID=0x%x, OID=0x%x, plen=%d\n",
801 nci_pbf(skb->data),
802 nci_opcode_gid(nci_opcode(skb->data)),
803 nci_opcode_oid(nci_opcode(skb->data)),
804 nci_plen(skb->data));
Ilan Elias6a2968a2011-09-18 11:19:35 +0300805
806 nci_send_frame(skb);
807
808 mod_timer(&ndev->cmd_timer,
809 jiffies + msecs_to_jiffies(NCI_CMD_TIMEOUT));
810 }
811}