blob: 5f98dc1bf03943abfd3315109acf94431858a9e2 [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
Dave Jones8a70e7f2012-07-12 19:17:34 +020030#include <linux/module.h>
Ilan Elias6a2968a2011-09-18 11:19:35 +030031#include <linux/types.h>
32#include <linux/workqueue.h>
33#include <linux/completion.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040034#include <linux/export.h>
Ilan Elias6a2968a2011-09-18 11:19:35 +030035#include <linux/sched.h>
36#include <linux/bitops.h>
37#include <linux/skbuff.h>
38
39#include "../nfc.h"
40#include <net/nfc/nci.h>
41#include <net/nfc/nci_core.h>
42#include <linux/nfc.h>
43
44static void nci_cmd_work(struct work_struct *work);
45static void nci_rx_work(struct work_struct *work);
46static void nci_tx_work(struct work_struct *work);
47
48/* ---- NCI requests ---- */
49
50void nci_req_complete(struct nci_dev *ndev, int result)
51{
52 if (ndev->req_status == NCI_REQ_PEND) {
53 ndev->req_result = result;
54 ndev->req_status = NCI_REQ_DONE;
55 complete(&ndev->req_completion);
56 }
57}
58
59static void nci_req_cancel(struct nci_dev *ndev, int err)
60{
61 if (ndev->req_status == NCI_REQ_PEND) {
62 ndev->req_result = err;
63 ndev->req_status = NCI_REQ_CANCELED;
64 complete(&ndev->req_completion);
65 }
66}
67
68/* Execute request and wait for completion. */
69static int __nci_request(struct nci_dev *ndev,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +010070 void (*req)(struct nci_dev *ndev, unsigned long opt),
71 unsigned long opt, __u32 timeout)
Ilan Elias6a2968a2011-09-18 11:19:35 +030072{
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);
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +010080 completion_rc =
81 wait_for_completion_interruptible_timeout(&ndev->req_completion,
82 timeout);
Ilan Elias6a2968a2011-09-18 11:19:35 +030083
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,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100113 void (*req)(struct nci_dev *ndev,
114 unsigned long opt),
115 unsigned long opt, __u32 timeout)
Ilan Elias6a2968a2011-09-18 11:19:35 +0300116{
117 int rc;
118
119 if (!test_bit(NCI_UP, &ndev->flags))
120 return -ENETDOWN;
121
122 /* Serialize all requests */
123 mutex_lock(&ndev->req_lock);
124 rc = __nci_request(ndev, req, opt, timeout);
125 mutex_unlock(&ndev->req_lock);
126
127 return rc;
128}
129
130static void nci_reset_req(struct nci_dev *ndev, unsigned long opt)
131{
Ilan Eliase8c0dac2011-11-09 12:09:14 +0200132 struct nci_core_reset_cmd cmd;
133
134 cmd.reset_type = NCI_RESET_TYPE_RESET_CONFIG;
135 nci_send_cmd(ndev, NCI_OP_CORE_RESET_CMD, 1, &cmd);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300136}
137
138static void nci_init_req(struct nci_dev *ndev, unsigned long opt)
139{
140 nci_send_cmd(ndev, NCI_OP_CORE_INIT_CMD, 0, NULL);
141}
142
143static void nci_init_complete_req(struct nci_dev *ndev, unsigned long opt)
144{
Ilan Elias2eb1dc12011-09-22 10:47:52 +0300145 struct nci_rf_disc_map_cmd cmd;
146 struct disc_map_config *cfg = cmd.mapping_configs;
147 __u8 *num = &cmd.num_mapping_configs;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300148 int i;
149
Ilan Elias6a2968a2011-09-18 11:19:35 +0300150 /* set rf mapping configurations */
Ilan Elias2eb1dc12011-09-22 10:47:52 +0300151 *num = 0;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300152
153 /* by default mapping is set to NCI_RF_INTERFACE_FRAME */
154 for (i = 0; i < ndev->num_supported_rf_interfaces; i++) {
155 if (ndev->supported_rf_interfaces[i] ==
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100156 NCI_RF_INTERFACE_ISO_DEP) {
Ilan Elias2eb1dc12011-09-22 10:47:52 +0300157 cfg[*num].rf_protocol = NCI_RF_PROTOCOL_ISO_DEP;
Ilan Elias637d85a2011-12-20 16:57:40 +0200158 cfg[*num].mode = NCI_DISC_MAP_MODE_POLL |
159 NCI_DISC_MAP_MODE_LISTEN;
160 cfg[*num].rf_interface = NCI_RF_INTERFACE_ISO_DEP;
Ilan Elias2eb1dc12011-09-22 10:47:52 +0300161 (*num)++;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300162 } else if (ndev->supported_rf_interfaces[i] ==
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100163 NCI_RF_INTERFACE_NFC_DEP) {
Ilan Elias2eb1dc12011-09-22 10:47:52 +0300164 cfg[*num].rf_protocol = NCI_RF_PROTOCOL_NFC_DEP;
Ilan Elias637d85a2011-12-20 16:57:40 +0200165 cfg[*num].mode = NCI_DISC_MAP_MODE_POLL |
166 NCI_DISC_MAP_MODE_LISTEN;
167 cfg[*num].rf_interface = NCI_RF_INTERFACE_NFC_DEP;
Ilan Elias2eb1dc12011-09-22 10:47:52 +0300168 (*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,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100176 (1 + ((*num) * sizeof(struct disc_map_config))), &cmd);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300177}
178
Ilan Elias7e035232012-08-15 11:46:22 +0300179struct nci_set_config_param {
180 __u8 id;
181 size_t len;
182 __u8 *val;
183};
184
185static void nci_set_config_req(struct nci_dev *ndev, unsigned long opt)
186{
187 struct nci_set_config_param *param = (struct nci_set_config_param *)opt;
188 struct nci_core_set_config_cmd cmd;
189
190 BUG_ON(param->len > NCI_MAX_PARAM_LEN);
191
192 cmd.num_params = 1;
193 cmd.param.id = param->id;
194 cmd.param.len = param->len;
195 memcpy(cmd.param.val, param->val, param->len);
196
197 nci_send_cmd(ndev, NCI_OP_CORE_SET_CONFIG_CMD, (3 + param->len), &cmd);
198}
199
Ilan Elias6a2968a2011-09-18 11:19:35 +0300200static void nci_rf_discover_req(struct nci_dev *ndev, unsigned long opt)
201{
202 struct nci_rf_disc_cmd cmd;
203 __u32 protocols = opt;
204
205 cmd.num_disc_configs = 0;
206
207 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
Szymon Janc874934f2012-10-04 15:15:51 +0200208 (protocols & NFC_PROTO_JEWEL_MASK ||
209 protocols & NFC_PROTO_MIFARE_MASK ||
210 protocols & NFC_PROTO_ISO14443_MASK ||
211 protocols & NFC_PROTO_NFC_DEP_MASK)) {
Ilan Elias637d85a2011-12-20 16:57:40 +0200212 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100213 NCI_NFC_A_PASSIVE_POLL_MODE;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300214 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
215 cmd.num_disc_configs++;
216 }
217
218 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
Samuel Ortiz01d719a2012-07-04 00:14:04 +0200219 (protocols & NFC_PROTO_ISO14443_B_MASK)) {
Ilan Elias637d85a2011-12-20 16:57:40 +0200220 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100221 NCI_NFC_B_PASSIVE_POLL_MODE;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300222 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
223 cmd.num_disc_configs++;
224 }
225
226 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
Szymon Janc874934f2012-10-04 15:15:51 +0200227 (protocols & NFC_PROTO_FELICA_MASK ||
228 protocols & NFC_PROTO_NFC_DEP_MASK)) {
Ilan Elias637d85a2011-12-20 16:57:40 +0200229 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100230 NCI_NFC_F_PASSIVE_POLL_MODE;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300231 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
232 cmd.num_disc_configs++;
233 }
234
235 nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_CMD,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100236 (1 + (cmd.num_disc_configs * sizeof(struct disc_config))),
237 &cmd);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300238}
239
Ilan Elias019c4fb2012-01-18 13:16:14 +0200240struct nci_rf_discover_select_param {
241 __u8 rf_discovery_id;
242 __u8 rf_protocol;
243};
244
245static void nci_rf_discover_select_req(struct nci_dev *ndev, unsigned long opt)
246{
247 struct nci_rf_discover_select_param *param =
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100248 (struct nci_rf_discover_select_param *)opt;
Ilan Elias019c4fb2012-01-18 13:16:14 +0200249 struct nci_rf_discover_select_cmd cmd;
250
251 cmd.rf_discovery_id = param->rf_discovery_id;
252 cmd.rf_protocol = param->rf_protocol;
253
254 switch (cmd.rf_protocol) {
255 case NCI_RF_PROTOCOL_ISO_DEP:
256 cmd.rf_interface = NCI_RF_INTERFACE_ISO_DEP;
257 break;
258
259 case NCI_RF_PROTOCOL_NFC_DEP:
260 cmd.rf_interface = NCI_RF_INTERFACE_NFC_DEP;
261 break;
262
263 default:
264 cmd.rf_interface = NCI_RF_INTERFACE_FRAME;
265 break;
266 }
267
268 nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_SELECT_CMD,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100269 sizeof(struct nci_rf_discover_select_cmd), &cmd);
Ilan Elias019c4fb2012-01-18 13:16:14 +0200270}
271
Ilan Elias6a2968a2011-09-18 11:19:35 +0300272static void nci_rf_deactivate_req(struct nci_dev *ndev, unsigned long opt)
273{
274 struct nci_rf_deactivate_cmd cmd;
275
276 cmd.type = NCI_DEACTIVATE_TYPE_IDLE_MODE;
277
278 nci_send_cmd(ndev, NCI_OP_RF_DEACTIVATE_CMD,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100279 sizeof(struct nci_rf_deactivate_cmd), &cmd);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300280}
281
282static int nci_open_device(struct nci_dev *ndev)
283{
284 int rc = 0;
285
286 mutex_lock(&ndev->req_lock);
287
288 if (test_bit(NCI_UP, &ndev->flags)) {
289 rc = -EALREADY;
290 goto done;
291 }
292
293 if (ndev->ops->open(ndev)) {
294 rc = -EIO;
295 goto done;
296 }
297
298 atomic_set(&ndev->cmd_cnt, 1);
299
300 set_bit(NCI_INIT, &ndev->flags);
301
302 rc = __nci_request(ndev, nci_reset_req, 0,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100303 msecs_to_jiffies(NCI_RESET_TIMEOUT));
Ilan Elias6a2968a2011-09-18 11:19:35 +0300304
305 if (!rc) {
306 rc = __nci_request(ndev, nci_init_req, 0,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100307 msecs_to_jiffies(NCI_INIT_TIMEOUT));
Ilan Elias6a2968a2011-09-18 11:19:35 +0300308 }
309
310 if (!rc) {
311 rc = __nci_request(ndev, nci_init_complete_req, 0,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100312 msecs_to_jiffies(NCI_INIT_TIMEOUT));
Ilan Elias6a2968a2011-09-18 11:19:35 +0300313 }
314
315 clear_bit(NCI_INIT, &ndev->flags);
316
317 if (!rc) {
318 set_bit(NCI_UP, &ndev->flags);
Ilan Elias019c4fb2012-01-18 13:16:14 +0200319 nci_clear_target_list(ndev);
Ilan Elias8939e472012-01-18 13:16:12 +0200320 atomic_set(&ndev->state, NCI_IDLE);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300321 } else {
322 /* Init failed, cleanup */
323 skb_queue_purge(&ndev->cmd_q);
324 skb_queue_purge(&ndev->rx_q);
325 skb_queue_purge(&ndev->tx_q);
326
327 ndev->ops->close(ndev);
328 ndev->flags = 0;
329 }
330
331done:
332 mutex_unlock(&ndev->req_lock);
333 return rc;
334}
335
336static int nci_close_device(struct nci_dev *ndev)
337{
338 nci_req_cancel(ndev, ENODEV);
339 mutex_lock(&ndev->req_lock);
340
341 if (!test_and_clear_bit(NCI_UP, &ndev->flags)) {
342 del_timer_sync(&ndev->cmd_timer);
Ilan Eliasc4bf98b2012-01-17 12:03:50 +0200343 del_timer_sync(&ndev->data_timer);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300344 mutex_unlock(&ndev->req_lock);
345 return 0;
346 }
347
348 /* Drop RX and TX queues */
349 skb_queue_purge(&ndev->rx_q);
350 skb_queue_purge(&ndev->tx_q);
351
352 /* Flush RX and TX wq */
353 flush_workqueue(ndev->rx_wq);
354 flush_workqueue(ndev->tx_wq);
355
356 /* Reset device */
357 skb_queue_purge(&ndev->cmd_q);
358 atomic_set(&ndev->cmd_cnt, 1);
359
360 set_bit(NCI_INIT, &ndev->flags);
361 __nci_request(ndev, nci_reset_req, 0,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100362 msecs_to_jiffies(NCI_RESET_TIMEOUT));
Ilan Elias6a2968a2011-09-18 11:19:35 +0300363 clear_bit(NCI_INIT, &ndev->flags);
364
365 /* Flush cmd wq */
366 flush_workqueue(ndev->cmd_wq);
367
368 /* After this point our queues are empty
369 * and no works are scheduled. */
370 ndev->ops->close(ndev);
371
372 /* Clear flags */
373 ndev->flags = 0;
374
375 mutex_unlock(&ndev->req_lock);
376
377 return 0;
378}
379
380/* NCI command timer function */
381static void nci_cmd_timer(unsigned long arg)
382{
383 struct nci_dev *ndev = (void *) arg;
384
Ilan Elias6a2968a2011-09-18 11:19:35 +0300385 atomic_set(&ndev->cmd_cnt, 1);
386 queue_work(ndev->cmd_wq, &ndev->cmd_work);
387}
388
Ilan Eliasc4bf98b2012-01-17 12:03:50 +0200389/* NCI data exchange timer function */
390static void nci_data_timer(unsigned long arg)
391{
392 struct nci_dev *ndev = (void *) arg;
393
394 set_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags);
395 queue_work(ndev->rx_wq, &ndev->rx_work);
396}
397
Ilan Elias6a2968a2011-09-18 11:19:35 +0300398static int nci_dev_up(struct nfc_dev *nfc_dev)
399{
400 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
401
Ilan Elias6a2968a2011-09-18 11:19:35 +0300402 return nci_open_device(ndev);
403}
404
405static int nci_dev_down(struct nfc_dev *nfc_dev)
406{
407 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
408
Ilan Elias6a2968a2011-09-18 11:19:35 +0300409 return nci_close_device(ndev);
410}
411
Ilan Elias7e035232012-08-15 11:46:22 +0300412static int nci_set_local_general_bytes(struct nfc_dev *nfc_dev)
413{
414 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
415 struct nci_set_config_param param;
416 __u8 local_gb[NFC_MAX_GT_LEN];
Szymon Jancf9fc36f2012-10-04 15:15:46 +0200417 int i;
Ilan Elias7e035232012-08-15 11:46:22 +0300418
419 param.val = nfc_get_local_general_bytes(nfc_dev, &param.len);
420 if ((param.val == NULL) || (param.len == 0))
Szymon Jancf9fc36f2012-10-04 15:15:46 +0200421 return 0;
Ilan Elias7e035232012-08-15 11:46:22 +0300422
Szymon Janc460d8f92012-10-04 15:15:45 +0200423 if (param.len > NFC_MAX_GT_LEN)
Ilan Elias7e035232012-08-15 11:46:22 +0300424 return -EINVAL;
425
426 for (i = 0; i < param.len; i++)
427 local_gb[param.len-1-i] = param.val[i];
428
429 param.id = NCI_PN_ATR_REQ_GEN_BYTES;
430 param.val = local_gb;
431
Szymon Jancf9fc36f2012-10-04 15:15:46 +0200432 return nci_request(ndev, nci_set_config_req, (unsigned long)&param,
433 msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT));
Ilan Elias7e035232012-08-15 11:46:22 +0300434}
435
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200436static int nci_start_poll(struct nfc_dev *nfc_dev,
437 __u32 im_protocols, __u32 tm_protocols)
Ilan Elias6a2968a2011-09-18 11:19:35 +0300438{
439 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
440 int rc;
441
Ilan Elias019c4fb2012-01-18 13:16:14 +0200442 if ((atomic_read(&ndev->state) == NCI_DISCOVERY) ||
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100443 (atomic_read(&ndev->state) == NCI_W4_ALL_DISCOVERIES)) {
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800444 pr_err("unable to start poll, since poll is already active\n");
Ilan Elias6a2968a2011-09-18 11:19:35 +0300445 return -EBUSY;
446 }
447
Ilan Eliasde054792011-09-22 11:13:01 +0300448 if (ndev->target_active_prot) {
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800449 pr_err("there is an active target\n");
Ilan Eliasde054792011-09-22 11:13:01 +0300450 return -EBUSY;
451 }
452
Ilan Elias019c4fb2012-01-18 13:16:14 +0200453 if ((atomic_read(&ndev->state) == NCI_W4_HOST_SELECT) ||
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100454 (atomic_read(&ndev->state) == NCI_POLL_ACTIVE)) {
Ilan Elias019c4fb2012-01-18 13:16:14 +0200455 pr_debug("target active or w4 select, implicitly deactivate\n");
Ilan Elias6a2968a2011-09-18 11:19:35 +0300456
457 rc = nci_request(ndev, nci_rf_deactivate_req, 0,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100458 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
Ilan Elias6a2968a2011-09-18 11:19:35 +0300459 if (rc)
460 return -EBUSY;
461 }
462
Ilan Elias7e035232012-08-15 11:46:22 +0300463 if (im_protocols & NFC_PROTO_NFC_DEP_MASK) {
464 rc = nci_set_local_general_bytes(nfc_dev);
465 if (rc) {
466 pr_err("failed to set local general bytes\n");
467 return rc;
468 }
469 }
470
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200471 rc = nci_request(ndev, nci_rf_discover_req, im_protocols,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100472 msecs_to_jiffies(NCI_RF_DISC_TIMEOUT));
Ilan Elias6a2968a2011-09-18 11:19:35 +0300473
474 if (!rc)
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200475 ndev->poll_prots = im_protocols;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300476
477 return rc;
478}
479
480static void nci_stop_poll(struct nfc_dev *nfc_dev)
481{
482 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
483
Ilan Elias019c4fb2012-01-18 13:16:14 +0200484 if ((atomic_read(&ndev->state) != NCI_DISCOVERY) &&
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100485 (atomic_read(&ndev->state) != NCI_W4_ALL_DISCOVERIES)) {
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800486 pr_err("unable to stop poll, since poll is not active\n");
Ilan Elias6a2968a2011-09-18 11:19:35 +0300487 return;
488 }
489
490 nci_request(ndev, nci_rf_deactivate_req, 0,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100491 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
Ilan Elias6a2968a2011-09-18 11:19:35 +0300492}
493
Eric Lapuyade90099432012-05-07 12:31:13 +0200494static int nci_activate_target(struct nfc_dev *nfc_dev,
495 struct nfc_target *target, __u32 protocol)
Ilan Elias6a2968a2011-09-18 11:19:35 +0300496{
497 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
Ilan Elias019c4fb2012-01-18 13:16:14 +0200498 struct nci_rf_discover_select_param param;
Eric Lapuyade90099432012-05-07 12:31:13 +0200499 struct nfc_target *nci_target = NULL;
Ilan Elias019c4fb2012-01-18 13:16:14 +0200500 int i;
501 int rc = 0;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300502
Eric Lapuyade90099432012-05-07 12:31:13 +0200503 pr_debug("target_idx %d, protocol 0x%x\n", target->idx, protocol);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300504
Ilan Elias019c4fb2012-01-18 13:16:14 +0200505 if ((atomic_read(&ndev->state) != NCI_W4_HOST_SELECT) &&
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100506 (atomic_read(&ndev->state) != NCI_POLL_ACTIVE)) {
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800507 pr_err("there is no available target to activate\n");
Ilan Elias6a2968a2011-09-18 11:19:35 +0300508 return -EINVAL;
509 }
510
511 if (ndev->target_active_prot) {
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800512 pr_err("there is already an active target\n");
Ilan Elias6a2968a2011-09-18 11:19:35 +0300513 return -EBUSY;
514 }
515
Ilan Elias019c4fb2012-01-18 13:16:14 +0200516 for (i = 0; i < ndev->n_targets; i++) {
Eric Lapuyade90099432012-05-07 12:31:13 +0200517 if (ndev->targets[i].idx == target->idx) {
518 nci_target = &ndev->targets[i];
Ilan Elias019c4fb2012-01-18 13:16:14 +0200519 break;
520 }
521 }
522
Eric Lapuyade90099432012-05-07 12:31:13 +0200523 if (!nci_target) {
Ilan Elias019c4fb2012-01-18 13:16:14 +0200524 pr_err("unable to find the selected target\n");
525 return -EINVAL;
526 }
527
Eric Lapuyade90099432012-05-07 12:31:13 +0200528 if (!(nci_target->supported_protocols & (1 << protocol))) {
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800529 pr_err("target does not support the requested protocol 0x%x\n",
530 protocol);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300531 return -EINVAL;
532 }
533
Ilan Elias019c4fb2012-01-18 13:16:14 +0200534 if (atomic_read(&ndev->state) == NCI_W4_HOST_SELECT) {
Eric Lapuyade90099432012-05-07 12:31:13 +0200535 param.rf_discovery_id = nci_target->logical_idx;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300536
Ilan Elias019c4fb2012-01-18 13:16:14 +0200537 if (protocol == NFC_PROTO_JEWEL)
538 param.rf_protocol = NCI_RF_PROTOCOL_T1T;
539 else if (protocol == NFC_PROTO_MIFARE)
540 param.rf_protocol = NCI_RF_PROTOCOL_T2T;
541 else if (protocol == NFC_PROTO_FELICA)
542 param.rf_protocol = NCI_RF_PROTOCOL_T3T;
Samuel Ortiz01d719a2012-07-04 00:14:04 +0200543 else if (protocol == NFC_PROTO_ISO14443 ||
544 protocol == NFC_PROTO_ISO14443_B)
Ilan Elias019c4fb2012-01-18 13:16:14 +0200545 param.rf_protocol = NCI_RF_PROTOCOL_ISO_DEP;
546 else
547 param.rf_protocol = NCI_RF_PROTOCOL_NFC_DEP;
548
549 rc = nci_request(ndev, nci_rf_discover_select_req,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100550 (unsigned long)&param,
551 msecs_to_jiffies(NCI_RF_DISC_SELECT_TIMEOUT));
Ilan Elias019c4fb2012-01-18 13:16:14 +0200552 }
553
554 if (!rc)
555 ndev->target_active_prot = protocol;
556
557 return rc;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300558}
559
Eric Lapuyade90099432012-05-07 12:31:13 +0200560static void nci_deactivate_target(struct nfc_dev *nfc_dev,
561 struct nfc_target *target)
Ilan Elias6a2968a2011-09-18 11:19:35 +0300562{
563 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
564
Ilan Elias767f19a2012-08-15 11:46:24 +0300565 pr_debug("entry\n");
Ilan Elias6a2968a2011-09-18 11:19:35 +0300566
567 if (!ndev->target_active_prot) {
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800568 pr_err("unable to deactivate target, no active target\n");
Ilan Elias6a2968a2011-09-18 11:19:35 +0300569 return;
570 }
571
572 ndev->target_active_prot = 0;
573
Ilan Elias8939e472012-01-18 13:16:12 +0200574 if (atomic_read(&ndev->state) == NCI_POLL_ACTIVE) {
Ilan Elias6a2968a2011-09-18 11:19:35 +0300575 nci_request(ndev, nci_rf_deactivate_req, 0,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100576 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
Ilan Elias6a2968a2011-09-18 11:19:35 +0300577 }
578}
579
Ilan Elias767f19a2012-08-15 11:46:24 +0300580static int nci_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
581 __u8 comm_mode, __u8 *gb, size_t gb_len)
582{
583 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
584 int rc;
585
586 pr_debug("target_idx %d, comm_mode %d\n", target->idx, comm_mode);
587
588 rc = nci_activate_target(nfc_dev, target, NFC_PROTO_NFC_DEP);
589 if (rc)
590 return rc;
591
592 rc = nfc_set_remote_general_bytes(nfc_dev, ndev->remote_gb,
593 ndev->remote_gb_len);
594 if (!rc)
595 rc = nfc_dep_link_is_up(nfc_dev, target->idx, NFC_COMM_PASSIVE,
596 NFC_RF_INITIATOR);
597
598 return rc;
599}
600
601static int nci_dep_link_down(struct nfc_dev *nfc_dev)
602{
603 pr_debug("entry\n");
604
605 nci_deactivate_target(nfc_dev, NULL);
606
607 return 0;
608}
609
610
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +0200611static int nci_transceive(struct nfc_dev *nfc_dev, struct nfc_target *target,
612 struct sk_buff *skb,
613 data_exchange_cb_t cb, void *cb_context)
Ilan Elias6a2968a2011-09-18 11:19:35 +0300614{
615 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
Ilan Elias38f04c62011-09-22 11:36:19 +0300616 int rc;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300617
Eric Lapuyade90099432012-05-07 12:31:13 +0200618 pr_debug("target_idx %d, len %d\n", target->idx, skb->len);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300619
620 if (!ndev->target_active_prot) {
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800621 pr_err("unable to exchange data, no active target\n");
Ilan Elias6a2968a2011-09-18 11:19:35 +0300622 return -EINVAL;
623 }
624
Ilan Elias38f04c62011-09-22 11:36:19 +0300625 if (test_and_set_bit(NCI_DATA_EXCHANGE, &ndev->flags))
626 return -EBUSY;
627
Ilan Elias6a2968a2011-09-18 11:19:35 +0300628 /* store cb and context to be used on receiving data */
629 ndev->data_exchange_cb = cb;
630 ndev->data_exchange_cb_context = cb_context;
631
Ilan Eliase8c0dac2011-11-09 12:09:14 +0200632 rc = nci_send_data(ndev, NCI_STATIC_RF_CONN_ID, skb);
Ilan Elias38f04c62011-09-22 11:36:19 +0300633 if (rc)
634 clear_bit(NCI_DATA_EXCHANGE, &ndev->flags);
635
636 return rc;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300637}
638
639static struct nfc_ops nci_nfc_ops = {
640 .dev_up = nci_dev_up,
641 .dev_down = nci_dev_down,
642 .start_poll = nci_start_poll,
643 .stop_poll = nci_stop_poll,
Ilan Elias767f19a2012-08-15 11:46:24 +0300644 .dep_link_up = nci_dep_link_up,
645 .dep_link_down = nci_dep_link_down,
Ilan Elias6a2968a2011-09-18 11:19:35 +0300646 .activate_target = nci_activate_target,
647 .deactivate_target = nci_deactivate_target,
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +0200648 .im_transceive = nci_transceive,
Ilan Elias6a2968a2011-09-18 11:19:35 +0300649};
650
651/* ---- Interface to NCI drivers ---- */
652
653/**
654 * nci_allocate_device - allocate a new nci device
655 *
656 * @ops: device operations
657 * @supported_protocols: NFC protocols supported by the device
658 */
659struct nci_dev *nci_allocate_device(struct nci_ops *ops,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100660 __u32 supported_protocols,
661 int tx_headroom, int tx_tailroom)
Ilan Elias6a2968a2011-09-18 11:19:35 +0300662{
Dan Carpenter8ebafde2011-09-23 09:14:35 +0300663 struct nci_dev *ndev;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300664
Joe Perches24bf3302011-11-29 11:37:35 -0800665 pr_debug("supported_protocols 0x%x\n", supported_protocols);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300666
667 if (!ops->open || !ops->close || !ops->send)
Dan Carpenter8ebafde2011-09-23 09:14:35 +0300668 return NULL;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300669
670 if (!supported_protocols)
Dan Carpenter8ebafde2011-09-23 09:14:35 +0300671 return NULL;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300672
673 ndev = kzalloc(sizeof(struct nci_dev), GFP_KERNEL);
674 if (!ndev)
Dan Carpenter8ebafde2011-09-23 09:14:35 +0300675 return NULL;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300676
677 ndev->ops = ops;
678 ndev->tx_headroom = tx_headroom;
679 ndev->tx_tailroom = tx_tailroom;
680
681 ndev->nfc_dev = nfc_allocate_device(&nci_nfc_ops,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100682 supported_protocols,
683 tx_headroom + NCI_DATA_HDR_SIZE,
684 tx_tailroom);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300685 if (!ndev->nfc_dev)
686 goto free_exit;
687
688 nfc_set_drvdata(ndev->nfc_dev, ndev);
689
Dan Carpenter8ebafde2011-09-23 09:14:35 +0300690 return ndev;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300691
692free_exit:
693 kfree(ndev);
Dan Carpenter8ebafde2011-09-23 09:14:35 +0300694 return NULL;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300695}
696EXPORT_SYMBOL(nci_allocate_device);
697
698/**
699 * nci_free_device - deallocate nci device
700 *
701 * @ndev: The nci device to deallocate
702 */
703void nci_free_device(struct nci_dev *ndev)
704{
Ilan Elias6a2968a2011-09-18 11:19:35 +0300705 nfc_free_device(ndev->nfc_dev);
706 kfree(ndev);
707}
708EXPORT_SYMBOL(nci_free_device);
709
710/**
711 * nci_register_device - register a nci device in the nfc subsystem
712 *
713 * @dev: The nci device to register
714 */
715int nci_register_device(struct nci_dev *ndev)
716{
717 int rc;
718 struct device *dev = &ndev->nfc_dev->dev;
719 char name[32];
720
Ilan Elias6a2968a2011-09-18 11:19:35 +0300721 rc = nfc_register_device(ndev->nfc_dev);
722 if (rc)
723 goto exit;
724
725 ndev->flags = 0;
726
727 INIT_WORK(&ndev->cmd_work, nci_cmd_work);
728 snprintf(name, sizeof(name), "%s_nci_cmd_wq", dev_name(dev));
729 ndev->cmd_wq = create_singlethread_workqueue(name);
730 if (!ndev->cmd_wq) {
731 rc = -ENOMEM;
732 goto unreg_exit;
733 }
734
735 INIT_WORK(&ndev->rx_work, nci_rx_work);
736 snprintf(name, sizeof(name), "%s_nci_rx_wq", dev_name(dev));
737 ndev->rx_wq = create_singlethread_workqueue(name);
738 if (!ndev->rx_wq) {
739 rc = -ENOMEM;
740 goto destroy_cmd_wq_exit;
741 }
742
743 INIT_WORK(&ndev->tx_work, nci_tx_work);
744 snprintf(name, sizeof(name), "%s_nci_tx_wq", dev_name(dev));
745 ndev->tx_wq = create_singlethread_workqueue(name);
746 if (!ndev->tx_wq) {
747 rc = -ENOMEM;
748 goto destroy_rx_wq_exit;
749 }
750
751 skb_queue_head_init(&ndev->cmd_q);
752 skb_queue_head_init(&ndev->rx_q);
753 skb_queue_head_init(&ndev->tx_q);
754
755 setup_timer(&ndev->cmd_timer, nci_cmd_timer,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100756 (unsigned long) ndev);
Ilan Eliasc4bf98b2012-01-17 12:03:50 +0200757 setup_timer(&ndev->data_timer, nci_data_timer,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100758 (unsigned long) ndev);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300759
760 mutex_init(&ndev->req_lock);
761
762 goto exit;
763
764destroy_rx_wq_exit:
765 destroy_workqueue(ndev->rx_wq);
766
767destroy_cmd_wq_exit:
768 destroy_workqueue(ndev->cmd_wq);
769
770unreg_exit:
771 nfc_unregister_device(ndev->nfc_dev);
772
773exit:
774 return rc;
775}
776EXPORT_SYMBOL(nci_register_device);
777
778/**
779 * nci_unregister_device - unregister a nci device in the nfc subsystem
780 *
781 * @dev: The nci device to unregister
782 */
783void nci_unregister_device(struct nci_dev *ndev)
784{
Ilan Elias6a2968a2011-09-18 11:19:35 +0300785 nci_close_device(ndev);
786
787 destroy_workqueue(ndev->cmd_wq);
788 destroy_workqueue(ndev->rx_wq);
789 destroy_workqueue(ndev->tx_wq);
790
791 nfc_unregister_device(ndev->nfc_dev);
792}
793EXPORT_SYMBOL(nci_unregister_device);
794
795/**
796 * nci_recv_frame - receive frame from NCI drivers
797 *
798 * @skb: The sk_buff to receive
799 */
800int nci_recv_frame(struct sk_buff *skb)
801{
802 struct nci_dev *ndev = (struct nci_dev *) skb->dev;
803
Joe Perches24bf3302011-11-29 11:37:35 -0800804 pr_debug("len %d\n", skb->len);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300805
Szymon Janc874934f2012-10-04 15:15:51 +0200806 if (!ndev || (!test_bit(NCI_UP, &ndev->flags) &&
807 !test_bit(NCI_INIT, &ndev->flags))) {
Ilan Elias6a2968a2011-09-18 11:19:35 +0300808 kfree_skb(skb);
809 return -ENXIO;
810 }
811
812 /* Queue frame for rx worker thread */
813 skb_queue_tail(&ndev->rx_q, skb);
814 queue_work(ndev->rx_wq, &ndev->rx_work);
815
816 return 0;
817}
818EXPORT_SYMBOL(nci_recv_frame);
819
820static int nci_send_frame(struct sk_buff *skb)
821{
822 struct nci_dev *ndev = (struct nci_dev *) skb->dev;
823
Joe Perches24bf3302011-11-29 11:37:35 -0800824 pr_debug("len %d\n", skb->len);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300825
826 if (!ndev) {
827 kfree_skb(skb);
828 return -ENODEV;
829 }
830
831 /* Get rid of skb owner, prior to sending to the driver. */
832 skb_orphan(skb);
833
834 return ndev->ops->send(skb);
835}
836
837/* Send NCI command */
838int nci_send_cmd(struct nci_dev *ndev, __u16 opcode, __u8 plen, void *payload)
839{
840 struct nci_ctrl_hdr *hdr;
841 struct sk_buff *skb;
842
Joe Perches24bf3302011-11-29 11:37:35 -0800843 pr_debug("opcode 0x%x, plen %d\n", opcode, plen);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300844
845 skb = nci_skb_alloc(ndev, (NCI_CTRL_HDR_SIZE + plen), GFP_KERNEL);
846 if (!skb) {
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800847 pr_err("no memory for command\n");
Ilan Elias6a2968a2011-09-18 11:19:35 +0300848 return -ENOMEM;
849 }
850
851 hdr = (struct nci_ctrl_hdr *) skb_put(skb, NCI_CTRL_HDR_SIZE);
852 hdr->gid = nci_opcode_gid(opcode);
853 hdr->oid = nci_opcode_oid(opcode);
854 hdr->plen = plen;
855
856 nci_mt_set((__u8 *)hdr, NCI_MT_CMD_PKT);
857 nci_pbf_set((__u8 *)hdr, NCI_PBF_LAST);
858
859 if (plen)
860 memcpy(skb_put(skb, plen), payload, plen);
861
862 skb->dev = (void *) ndev;
863
864 skb_queue_tail(&ndev->cmd_q, skb);
865 queue_work(ndev->cmd_wq, &ndev->cmd_work);
866
867 return 0;
868}
869
870/* ---- NCI TX Data worker thread ---- */
871
872static void nci_tx_work(struct work_struct *work)
873{
874 struct nci_dev *ndev = container_of(work, struct nci_dev, tx_work);
875 struct sk_buff *skb;
876
Joe Perches24bf3302011-11-29 11:37:35 -0800877 pr_debug("credits_cnt %d\n", atomic_read(&ndev->credits_cnt));
Ilan Elias6a2968a2011-09-18 11:19:35 +0300878
879 /* Send queued tx data */
880 while (atomic_read(&ndev->credits_cnt)) {
881 skb = skb_dequeue(&ndev->tx_q);
882 if (!skb)
883 return;
884
Ilan Eliasdb98c822011-11-09 12:09:16 +0200885 /* Check if data flow control is used */
886 if (atomic_read(&ndev->credits_cnt) !=
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100887 NCI_DATA_FLOW_CONTROL_NOT_USED)
Ilan Eliasdb98c822011-11-09 12:09:16 +0200888 atomic_dec(&ndev->credits_cnt);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300889
Joe Perches20c239c2011-11-29 11:37:33 -0800890 pr_debug("NCI TX: MT=data, PBF=%d, conn_id=%d, plen=%d\n",
891 nci_pbf(skb->data),
892 nci_conn_id(skb->data),
893 nci_plen(skb->data));
Ilan Elias6a2968a2011-09-18 11:19:35 +0300894
895 nci_send_frame(skb);
Ilan Eliasc4bf98b2012-01-17 12:03:50 +0200896
897 mod_timer(&ndev->data_timer,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100898 jiffies + msecs_to_jiffies(NCI_DATA_TIMEOUT));
Ilan Elias6a2968a2011-09-18 11:19:35 +0300899 }
900}
901
902/* ----- NCI RX worker thread (data & control) ----- */
903
904static void nci_rx_work(struct work_struct *work)
905{
906 struct nci_dev *ndev = container_of(work, struct nci_dev, rx_work);
907 struct sk_buff *skb;
908
909 while ((skb = skb_dequeue(&ndev->rx_q))) {
910 /* Process frame */
911 switch (nci_mt(skb->data)) {
912 case NCI_MT_RSP_PKT:
913 nci_rsp_packet(ndev, skb);
914 break;
915
916 case NCI_MT_NTF_PKT:
917 nci_ntf_packet(ndev, skb);
918 break;
919
920 case NCI_MT_DATA_PKT:
921 nci_rx_data_packet(ndev, skb);
922 break;
923
924 default:
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800925 pr_err("unknown MT 0x%x\n", nci_mt(skb->data));
Ilan Elias6a2968a2011-09-18 11:19:35 +0300926 kfree_skb(skb);
927 break;
928 }
929 }
Ilan Eliasc4bf98b2012-01-17 12:03:50 +0200930
931 /* check if a data exchange timout has occurred */
932 if (test_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags)) {
933 /* complete the data exchange transaction, if exists */
934 if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags))
935 nci_data_exchange_complete(ndev, NULL, -ETIMEDOUT);
936
937 clear_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags);
938 }
Ilan Elias6a2968a2011-09-18 11:19:35 +0300939}
940
941/* ----- NCI TX CMD worker thread ----- */
942
943static void nci_cmd_work(struct work_struct *work)
944{
945 struct nci_dev *ndev = container_of(work, struct nci_dev, cmd_work);
946 struct sk_buff *skb;
947
Joe Perches24bf3302011-11-29 11:37:35 -0800948 pr_debug("cmd_cnt %d\n", atomic_read(&ndev->cmd_cnt));
Ilan Elias6a2968a2011-09-18 11:19:35 +0300949
950 /* Send queued command */
951 if (atomic_read(&ndev->cmd_cnt)) {
952 skb = skb_dequeue(&ndev->cmd_q);
953 if (!skb)
954 return;
955
956 atomic_dec(&ndev->cmd_cnt);
957
Joe Perches20c239c2011-11-29 11:37:33 -0800958 pr_debug("NCI TX: MT=cmd, PBF=%d, GID=0x%x, OID=0x%x, plen=%d\n",
959 nci_pbf(skb->data),
960 nci_opcode_gid(nci_opcode(skb->data)),
961 nci_opcode_oid(nci_opcode(skb->data)),
962 nci_plen(skb->data));
Ilan Elias6a2968a2011-09-18 11:19:35 +0300963
964 nci_send_frame(skb);
965
966 mod_timer(&ndev->cmd_timer,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100967 jiffies + msecs_to_jiffies(NCI_CMD_TIMEOUT));
Ilan Elias6a2968a2011-09-18 11:19:35 +0300968 }
969}
Dave Jones8a70e7f2012-07-12 19:17:34 +0200970
971MODULE_LICENSE("GPL");