NFC: nci: Add nci_prop_cmd allowing to send proprietary nci cmd
[deliverable/linux.git] / net / nfc / nci / core.c
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 * Copyright (C) 2014 Marvell International Ltd.
7 *
8 * Written by Ilan Elias <ilane@ti.com>
9 *
10 * Acknowledgements:
11 * This file is based on hci_core.c, which was written
12 * by Maxim Krasnyansky.
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License version 2
16 * as published by the Free Software Foundation
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, see <http://www.gnu.org/licenses/>.
25 *
26 */
27
28 #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
29
30 #include <linux/module.h>
31 #include <linux/kernel.h>
32 #include <linux/types.h>
33 #include <linux/workqueue.h>
34 #include <linux/completion.h>
35 #include <linux/export.h>
36 #include <linux/sched.h>
37 #include <linux/bitops.h>
38 #include <linux/skbuff.h>
39
40 #include "../nfc.h"
41 #include <net/nfc/nci.h>
42 #include <net/nfc/nci_core.h>
43 #include <linux/nfc.h>
44
45 struct core_conn_create_data {
46 int length;
47 struct nci_core_conn_create_cmd *cmd;
48 };
49
50 static void nci_cmd_work(struct work_struct *work);
51 static void nci_rx_work(struct work_struct *work);
52 static void nci_tx_work(struct work_struct *work);
53
54 struct nci_conn_info *nci_get_conn_info_by_conn_id(struct nci_dev *ndev,
55 int conn_id)
56 {
57 struct nci_conn_info *conn_info;
58
59 list_for_each_entry(conn_info, &ndev->conn_info_list, list) {
60 if (conn_info->conn_id == conn_id)
61 return conn_info;
62 }
63
64 return NULL;
65 }
66
67 /* ---- NCI requests ---- */
68
69 void nci_req_complete(struct nci_dev *ndev, int result)
70 {
71 if (ndev->req_status == NCI_REQ_PEND) {
72 ndev->req_result = result;
73 ndev->req_status = NCI_REQ_DONE;
74 complete(&ndev->req_completion);
75 }
76 }
77
78 static void nci_req_cancel(struct nci_dev *ndev, int err)
79 {
80 if (ndev->req_status == NCI_REQ_PEND) {
81 ndev->req_result = err;
82 ndev->req_status = NCI_REQ_CANCELED;
83 complete(&ndev->req_completion);
84 }
85 }
86
87 /* Execute request and wait for completion. */
88 static int __nci_request(struct nci_dev *ndev,
89 void (*req)(struct nci_dev *ndev, unsigned long opt),
90 unsigned long opt, __u32 timeout)
91 {
92 int rc = 0;
93 long completion_rc;
94
95 ndev->req_status = NCI_REQ_PEND;
96
97 reinit_completion(&ndev->req_completion);
98 req(ndev, opt);
99 completion_rc =
100 wait_for_completion_interruptible_timeout(&ndev->req_completion,
101 timeout);
102
103 pr_debug("wait_for_completion return %ld\n", completion_rc);
104
105 if (completion_rc > 0) {
106 switch (ndev->req_status) {
107 case NCI_REQ_DONE:
108 rc = nci_to_errno(ndev->req_result);
109 break;
110
111 case NCI_REQ_CANCELED:
112 rc = -ndev->req_result;
113 break;
114
115 default:
116 rc = -ETIMEDOUT;
117 break;
118 }
119 } else {
120 pr_err("wait_for_completion_interruptible_timeout failed %ld\n",
121 completion_rc);
122
123 rc = ((completion_rc == 0) ? (-ETIMEDOUT) : (completion_rc));
124 }
125
126 ndev->req_status = ndev->req_result = 0;
127
128 return rc;
129 }
130
131 inline int nci_request(struct nci_dev *ndev,
132 void (*req)(struct nci_dev *ndev,
133 unsigned long opt),
134 unsigned long opt, __u32 timeout)
135 {
136 int rc;
137
138 if (!test_bit(NCI_UP, &ndev->flags))
139 return -ENETDOWN;
140
141 /* Serialize all requests */
142 mutex_lock(&ndev->req_lock);
143 rc = __nci_request(ndev, req, opt, timeout);
144 mutex_unlock(&ndev->req_lock);
145
146 return rc;
147 }
148
149 static void nci_reset_req(struct nci_dev *ndev, unsigned long opt)
150 {
151 struct nci_core_reset_cmd cmd;
152
153 cmd.reset_type = NCI_RESET_TYPE_RESET_CONFIG;
154 nci_send_cmd(ndev, NCI_OP_CORE_RESET_CMD, 1, &cmd);
155 }
156
157 static void nci_init_req(struct nci_dev *ndev, unsigned long opt)
158 {
159 nci_send_cmd(ndev, NCI_OP_CORE_INIT_CMD, 0, NULL);
160 }
161
162 static void nci_init_complete_req(struct nci_dev *ndev, unsigned long opt)
163 {
164 struct nci_rf_disc_map_cmd cmd;
165 struct disc_map_config *cfg = cmd.mapping_configs;
166 __u8 *num = &cmd.num_mapping_configs;
167 int i;
168
169 /* set rf mapping configurations */
170 *num = 0;
171
172 /* by default mapping is set to NCI_RF_INTERFACE_FRAME */
173 for (i = 0; i < ndev->num_supported_rf_interfaces; i++) {
174 if (ndev->supported_rf_interfaces[i] ==
175 NCI_RF_INTERFACE_ISO_DEP) {
176 cfg[*num].rf_protocol = NCI_RF_PROTOCOL_ISO_DEP;
177 cfg[*num].mode = NCI_DISC_MAP_MODE_POLL |
178 NCI_DISC_MAP_MODE_LISTEN;
179 cfg[*num].rf_interface = NCI_RF_INTERFACE_ISO_DEP;
180 (*num)++;
181 } else if (ndev->supported_rf_interfaces[i] ==
182 NCI_RF_INTERFACE_NFC_DEP) {
183 cfg[*num].rf_protocol = NCI_RF_PROTOCOL_NFC_DEP;
184 cfg[*num].mode = NCI_DISC_MAP_MODE_POLL |
185 NCI_DISC_MAP_MODE_LISTEN;
186 cfg[*num].rf_interface = NCI_RF_INTERFACE_NFC_DEP;
187 (*num)++;
188 }
189
190 if (*num == NCI_MAX_NUM_MAPPING_CONFIGS)
191 break;
192 }
193
194 nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_MAP_CMD,
195 (1 + ((*num) * sizeof(struct disc_map_config))), &cmd);
196 }
197
198 struct nci_set_config_param {
199 __u8 id;
200 size_t len;
201 __u8 *val;
202 };
203
204 static void nci_set_config_req(struct nci_dev *ndev, unsigned long opt)
205 {
206 struct nci_set_config_param *param = (struct nci_set_config_param *)opt;
207 struct nci_core_set_config_cmd cmd;
208
209 BUG_ON(param->len > NCI_MAX_PARAM_LEN);
210
211 cmd.num_params = 1;
212 cmd.param.id = param->id;
213 cmd.param.len = param->len;
214 memcpy(cmd.param.val, param->val, param->len);
215
216 nci_send_cmd(ndev, NCI_OP_CORE_SET_CONFIG_CMD, (3 + param->len), &cmd);
217 }
218
219 struct nci_rf_discover_param {
220 __u32 im_protocols;
221 __u32 tm_protocols;
222 };
223
224 static void nci_rf_discover_req(struct nci_dev *ndev, unsigned long opt)
225 {
226 struct nci_rf_discover_param *param =
227 (struct nci_rf_discover_param *)opt;
228 struct nci_rf_disc_cmd cmd;
229
230 cmd.num_disc_configs = 0;
231
232 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
233 (param->im_protocols & NFC_PROTO_JEWEL_MASK ||
234 param->im_protocols & NFC_PROTO_MIFARE_MASK ||
235 param->im_protocols & NFC_PROTO_ISO14443_MASK ||
236 param->im_protocols & NFC_PROTO_NFC_DEP_MASK)) {
237 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
238 NCI_NFC_A_PASSIVE_POLL_MODE;
239 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
240 cmd.num_disc_configs++;
241 }
242
243 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
244 (param->im_protocols & NFC_PROTO_ISO14443_B_MASK)) {
245 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
246 NCI_NFC_B_PASSIVE_POLL_MODE;
247 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
248 cmd.num_disc_configs++;
249 }
250
251 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
252 (param->im_protocols & NFC_PROTO_FELICA_MASK ||
253 param->im_protocols & NFC_PROTO_NFC_DEP_MASK)) {
254 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
255 NCI_NFC_F_PASSIVE_POLL_MODE;
256 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
257 cmd.num_disc_configs++;
258 }
259
260 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
261 (param->im_protocols & NFC_PROTO_ISO15693_MASK)) {
262 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
263 NCI_NFC_V_PASSIVE_POLL_MODE;
264 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
265 cmd.num_disc_configs++;
266 }
267
268 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS - 1) &&
269 (param->tm_protocols & NFC_PROTO_NFC_DEP_MASK)) {
270 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
271 NCI_NFC_A_PASSIVE_LISTEN_MODE;
272 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
273 cmd.num_disc_configs++;
274 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
275 NCI_NFC_F_PASSIVE_LISTEN_MODE;
276 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
277 cmd.num_disc_configs++;
278 }
279
280 nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_CMD,
281 (1 + (cmd.num_disc_configs * sizeof(struct disc_config))),
282 &cmd);
283 }
284
285 struct nci_rf_discover_select_param {
286 __u8 rf_discovery_id;
287 __u8 rf_protocol;
288 };
289
290 static void nci_rf_discover_select_req(struct nci_dev *ndev, unsigned long opt)
291 {
292 struct nci_rf_discover_select_param *param =
293 (struct nci_rf_discover_select_param *)opt;
294 struct nci_rf_discover_select_cmd cmd;
295
296 cmd.rf_discovery_id = param->rf_discovery_id;
297 cmd.rf_protocol = param->rf_protocol;
298
299 switch (cmd.rf_protocol) {
300 case NCI_RF_PROTOCOL_ISO_DEP:
301 cmd.rf_interface = NCI_RF_INTERFACE_ISO_DEP;
302 break;
303
304 case NCI_RF_PROTOCOL_NFC_DEP:
305 cmd.rf_interface = NCI_RF_INTERFACE_NFC_DEP;
306 break;
307
308 default:
309 cmd.rf_interface = NCI_RF_INTERFACE_FRAME;
310 break;
311 }
312
313 nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_SELECT_CMD,
314 sizeof(struct nci_rf_discover_select_cmd), &cmd);
315 }
316
317 static void nci_rf_deactivate_req(struct nci_dev *ndev, unsigned long opt)
318 {
319 struct nci_rf_deactivate_cmd cmd;
320
321 cmd.type = opt;
322
323 nci_send_cmd(ndev, NCI_OP_RF_DEACTIVATE_CMD,
324 sizeof(struct nci_rf_deactivate_cmd), &cmd);
325 }
326
327 struct nci_prop_cmd_param {
328 __u16 opcode;
329 size_t len;
330 __u8 *payload;
331 };
332
333 static void nci_prop_cmd_req(struct nci_dev *ndev, unsigned long opt)
334 {
335 struct nci_prop_cmd_param *param = (struct nci_prop_cmd_param *)opt;
336
337 nci_send_cmd(ndev, param->opcode, param->len, param->payload);
338 }
339
340 int nci_prop_cmd(struct nci_dev *ndev, __u8 oid, size_t len, __u8 *payload)
341 {
342 struct nci_prop_cmd_param param;
343
344 param.opcode = nci_opcode_pack(NCI_GID_PROPRIETARY, oid);
345 param.len = len;
346 param.payload = payload;
347
348 return __nci_request(ndev, nci_prop_cmd_req, (unsigned long)&param,
349 msecs_to_jiffies(NCI_CMD_TIMEOUT));
350 }
351 EXPORT_SYMBOL(nci_prop_cmd);
352
353 static int nci_open_device(struct nci_dev *ndev)
354 {
355 int rc = 0;
356
357 mutex_lock(&ndev->req_lock);
358
359 if (test_bit(NCI_UP, &ndev->flags)) {
360 rc = -EALREADY;
361 goto done;
362 }
363
364 if (ndev->ops->open(ndev)) {
365 rc = -EIO;
366 goto done;
367 }
368
369 atomic_set(&ndev->cmd_cnt, 1);
370
371 set_bit(NCI_INIT, &ndev->flags);
372
373 if (ndev->ops->init)
374 rc = ndev->ops->init(ndev);
375
376 if (!rc) {
377 rc = __nci_request(ndev, nci_reset_req, 0,
378 msecs_to_jiffies(NCI_RESET_TIMEOUT));
379 }
380
381 if (!rc && ndev->ops->setup) {
382 rc = ndev->ops->setup(ndev);
383 }
384
385 if (!rc) {
386 rc = __nci_request(ndev, nci_init_req, 0,
387 msecs_to_jiffies(NCI_INIT_TIMEOUT));
388 }
389
390 if (!rc) {
391 rc = __nci_request(ndev, nci_init_complete_req, 0,
392 msecs_to_jiffies(NCI_INIT_TIMEOUT));
393 }
394
395 clear_bit(NCI_INIT, &ndev->flags);
396
397 if (!rc) {
398 set_bit(NCI_UP, &ndev->flags);
399 nci_clear_target_list(ndev);
400 atomic_set(&ndev->state, NCI_IDLE);
401 } else {
402 /* Init failed, cleanup */
403 skb_queue_purge(&ndev->cmd_q);
404 skb_queue_purge(&ndev->rx_q);
405 skb_queue_purge(&ndev->tx_q);
406
407 ndev->ops->close(ndev);
408 ndev->flags = 0;
409 }
410
411 done:
412 mutex_unlock(&ndev->req_lock);
413 return rc;
414 }
415
416 static int nci_close_device(struct nci_dev *ndev)
417 {
418 nci_req_cancel(ndev, ENODEV);
419 mutex_lock(&ndev->req_lock);
420
421 if (!test_and_clear_bit(NCI_UP, &ndev->flags)) {
422 del_timer_sync(&ndev->cmd_timer);
423 del_timer_sync(&ndev->data_timer);
424 mutex_unlock(&ndev->req_lock);
425 return 0;
426 }
427
428 /* Drop RX and TX queues */
429 skb_queue_purge(&ndev->rx_q);
430 skb_queue_purge(&ndev->tx_q);
431
432 /* Flush RX and TX wq */
433 flush_workqueue(ndev->rx_wq);
434 flush_workqueue(ndev->tx_wq);
435
436 /* Reset device */
437 skb_queue_purge(&ndev->cmd_q);
438 atomic_set(&ndev->cmd_cnt, 1);
439
440 set_bit(NCI_INIT, &ndev->flags);
441 __nci_request(ndev, nci_reset_req, 0,
442 msecs_to_jiffies(NCI_RESET_TIMEOUT));
443 clear_bit(NCI_INIT, &ndev->flags);
444
445 del_timer_sync(&ndev->cmd_timer);
446
447 /* Flush cmd wq */
448 flush_workqueue(ndev->cmd_wq);
449
450 /* After this point our queues are empty
451 * and no works are scheduled. */
452 ndev->ops->close(ndev);
453
454 /* Clear flags */
455 ndev->flags = 0;
456
457 mutex_unlock(&ndev->req_lock);
458
459 return 0;
460 }
461
462 /* NCI command timer function */
463 static void nci_cmd_timer(unsigned long arg)
464 {
465 struct nci_dev *ndev = (void *) arg;
466
467 atomic_set(&ndev->cmd_cnt, 1);
468 queue_work(ndev->cmd_wq, &ndev->cmd_work);
469 }
470
471 /* NCI data exchange timer function */
472 static void nci_data_timer(unsigned long arg)
473 {
474 struct nci_dev *ndev = (void *) arg;
475
476 set_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags);
477 queue_work(ndev->rx_wq, &ndev->rx_work);
478 }
479
480 static int nci_dev_up(struct nfc_dev *nfc_dev)
481 {
482 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
483
484 return nci_open_device(ndev);
485 }
486
487 static int nci_dev_down(struct nfc_dev *nfc_dev)
488 {
489 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
490
491 return nci_close_device(ndev);
492 }
493
494 int nci_set_config(struct nci_dev *ndev, __u8 id, size_t len, __u8 *val)
495 {
496 struct nci_set_config_param param;
497
498 if (!val || !len)
499 return 0;
500
501 param.id = id;
502 param.len = len;
503 param.val = val;
504
505 return __nci_request(ndev, nci_set_config_req, (unsigned long)&param,
506 msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT));
507 }
508 EXPORT_SYMBOL(nci_set_config);
509
510 static void nci_nfcee_discover_req(struct nci_dev *ndev, unsigned long opt)
511 {
512 struct nci_nfcee_discover_cmd cmd;
513 __u8 action = opt;
514
515 cmd.discovery_action = action;
516
517 nci_send_cmd(ndev, NCI_OP_NFCEE_DISCOVER_CMD, 1, &cmd);
518 }
519
520 int nci_nfcee_discover(struct nci_dev *ndev, u8 action)
521 {
522 return nci_request(ndev, nci_nfcee_discover_req, action,
523 msecs_to_jiffies(NCI_CMD_TIMEOUT));
524 }
525 EXPORT_SYMBOL(nci_nfcee_discover);
526
527 static void nci_nfcee_mode_set_req(struct nci_dev *ndev, unsigned long opt)
528 {
529 struct nci_nfcee_mode_set_cmd *cmd =
530 (struct nci_nfcee_mode_set_cmd *)opt;
531
532 nci_send_cmd(ndev, NCI_OP_NFCEE_MODE_SET_CMD,
533 sizeof(struct nci_nfcee_mode_set_cmd), cmd);
534 }
535
536 int nci_nfcee_mode_set(struct nci_dev *ndev, u8 nfcee_id, u8 nfcee_mode)
537 {
538 struct nci_nfcee_mode_set_cmd cmd;
539
540 cmd.nfcee_id = nfcee_id;
541 cmd.nfcee_mode = nfcee_mode;
542
543 return nci_request(ndev, nci_nfcee_mode_set_req, (unsigned long)&cmd,
544 msecs_to_jiffies(NCI_CMD_TIMEOUT));
545 }
546 EXPORT_SYMBOL(nci_nfcee_mode_set);
547
548 static void nci_core_conn_create_req(struct nci_dev *ndev, unsigned long opt)
549 {
550 struct core_conn_create_data *data =
551 (struct core_conn_create_data *)opt;
552
553 nci_send_cmd(ndev, NCI_OP_CORE_CONN_CREATE_CMD, data->length, data->cmd);
554 }
555
556 int nci_core_conn_create(struct nci_dev *ndev, u8 destination_type,
557 u8 number_destination_params,
558 size_t params_len,
559 struct core_conn_create_dest_spec_params *params)
560 {
561 int r;
562 struct nci_core_conn_create_cmd *cmd;
563 struct core_conn_create_data data;
564
565 data.length = params_len + sizeof(struct nci_core_conn_create_cmd);
566 cmd = kzalloc(data.length, GFP_KERNEL);
567 if (!cmd)
568 return -ENOMEM;
569
570 cmd->destination_type = destination_type;
571 cmd->number_destination_params = number_destination_params;
572 memcpy(cmd->params, params, params_len);
573
574 data.cmd = cmd;
575 ndev->cur_id = params->value[DEST_SPEC_PARAMS_ID_INDEX];
576
577 r = __nci_request(ndev, nci_core_conn_create_req,
578 (unsigned long)&data,
579 msecs_to_jiffies(NCI_CMD_TIMEOUT));
580 kfree(cmd);
581 return r;
582 }
583 EXPORT_SYMBOL(nci_core_conn_create);
584
585 static void nci_core_conn_close_req(struct nci_dev *ndev, unsigned long opt)
586 {
587 __u8 conn_id = opt;
588
589 nci_send_cmd(ndev, NCI_OP_CORE_CONN_CLOSE_CMD, 1, &conn_id);
590 }
591
592 int nci_core_conn_close(struct nci_dev *ndev, u8 conn_id)
593 {
594 return nci_request(ndev, nci_core_conn_close_req, conn_id,
595 msecs_to_jiffies(NCI_CMD_TIMEOUT));
596 }
597 EXPORT_SYMBOL(nci_core_conn_close);
598
599 static int nci_set_local_general_bytes(struct nfc_dev *nfc_dev)
600 {
601 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
602 struct nci_set_config_param param;
603 int rc;
604
605 param.val = nfc_get_local_general_bytes(nfc_dev, &param.len);
606 if ((param.val == NULL) || (param.len == 0))
607 return 0;
608
609 if (param.len > NFC_MAX_GT_LEN)
610 return -EINVAL;
611
612 param.id = NCI_PN_ATR_REQ_GEN_BYTES;
613
614 rc = nci_request(ndev, nci_set_config_req, (unsigned long)&param,
615 msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT));
616 if (rc)
617 return rc;
618
619 param.id = NCI_LN_ATR_RES_GEN_BYTES;
620
621 return nci_request(ndev, nci_set_config_req, (unsigned long)&param,
622 msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT));
623 }
624
625 static int nci_set_listen_parameters(struct nfc_dev *nfc_dev)
626 {
627 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
628 int rc;
629 __u8 val;
630
631 val = NCI_LA_SEL_INFO_NFC_DEP_MASK;
632
633 rc = nci_set_config(ndev, NCI_LA_SEL_INFO, 1, &val);
634 if (rc)
635 return rc;
636
637 val = NCI_LF_PROTOCOL_TYPE_NFC_DEP_MASK;
638
639 rc = nci_set_config(ndev, NCI_LF_PROTOCOL_TYPE, 1, &val);
640 if (rc)
641 return rc;
642
643 val = NCI_LF_CON_BITR_F_212 | NCI_LF_CON_BITR_F_424;
644
645 return nci_set_config(ndev, NCI_LF_CON_BITR_F, 1, &val);
646 }
647
648 static int nci_start_poll(struct nfc_dev *nfc_dev,
649 __u32 im_protocols, __u32 tm_protocols)
650 {
651 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
652 struct nci_rf_discover_param param;
653 int rc;
654
655 if ((atomic_read(&ndev->state) == NCI_DISCOVERY) ||
656 (atomic_read(&ndev->state) == NCI_W4_ALL_DISCOVERIES)) {
657 pr_err("unable to start poll, since poll is already active\n");
658 return -EBUSY;
659 }
660
661 if (ndev->target_active_prot) {
662 pr_err("there is an active target\n");
663 return -EBUSY;
664 }
665
666 if ((atomic_read(&ndev->state) == NCI_W4_HOST_SELECT) ||
667 (atomic_read(&ndev->state) == NCI_POLL_ACTIVE)) {
668 pr_debug("target active or w4 select, implicitly deactivate\n");
669
670 rc = nci_request(ndev, nci_rf_deactivate_req,
671 NCI_DEACTIVATE_TYPE_IDLE_MODE,
672 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
673 if (rc)
674 return -EBUSY;
675 }
676
677 if ((im_protocols | tm_protocols) & NFC_PROTO_NFC_DEP_MASK) {
678 rc = nci_set_local_general_bytes(nfc_dev);
679 if (rc) {
680 pr_err("failed to set local general bytes\n");
681 return rc;
682 }
683 }
684
685 if (tm_protocols & NFC_PROTO_NFC_DEP_MASK) {
686 rc = nci_set_listen_parameters(nfc_dev);
687 if (rc)
688 pr_err("failed to set listen parameters\n");
689 }
690
691 param.im_protocols = im_protocols;
692 param.tm_protocols = tm_protocols;
693 rc = nci_request(ndev, nci_rf_discover_req, (unsigned long)&param,
694 msecs_to_jiffies(NCI_RF_DISC_TIMEOUT));
695
696 if (!rc)
697 ndev->poll_prots = im_protocols;
698
699 return rc;
700 }
701
702 static void nci_stop_poll(struct nfc_dev *nfc_dev)
703 {
704 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
705
706 if ((atomic_read(&ndev->state) != NCI_DISCOVERY) &&
707 (atomic_read(&ndev->state) != NCI_W4_ALL_DISCOVERIES)) {
708 pr_err("unable to stop poll, since poll is not active\n");
709 return;
710 }
711
712 nci_request(ndev, nci_rf_deactivate_req, NCI_DEACTIVATE_TYPE_IDLE_MODE,
713 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
714 }
715
716 static int nci_activate_target(struct nfc_dev *nfc_dev,
717 struct nfc_target *target, __u32 protocol)
718 {
719 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
720 struct nci_rf_discover_select_param param;
721 struct nfc_target *nci_target = NULL;
722 int i;
723 int rc = 0;
724
725 pr_debug("target_idx %d, protocol 0x%x\n", target->idx, protocol);
726
727 if ((atomic_read(&ndev->state) != NCI_W4_HOST_SELECT) &&
728 (atomic_read(&ndev->state) != NCI_POLL_ACTIVE)) {
729 pr_err("there is no available target to activate\n");
730 return -EINVAL;
731 }
732
733 if (ndev->target_active_prot) {
734 pr_err("there is already an active target\n");
735 return -EBUSY;
736 }
737
738 for (i = 0; i < ndev->n_targets; i++) {
739 if (ndev->targets[i].idx == target->idx) {
740 nci_target = &ndev->targets[i];
741 break;
742 }
743 }
744
745 if (!nci_target) {
746 pr_err("unable to find the selected target\n");
747 return -EINVAL;
748 }
749
750 if (!(nci_target->supported_protocols & (1 << protocol))) {
751 pr_err("target does not support the requested protocol 0x%x\n",
752 protocol);
753 return -EINVAL;
754 }
755
756 if (atomic_read(&ndev->state) == NCI_W4_HOST_SELECT) {
757 param.rf_discovery_id = nci_target->logical_idx;
758
759 if (protocol == NFC_PROTO_JEWEL)
760 param.rf_protocol = NCI_RF_PROTOCOL_T1T;
761 else if (protocol == NFC_PROTO_MIFARE)
762 param.rf_protocol = NCI_RF_PROTOCOL_T2T;
763 else if (protocol == NFC_PROTO_FELICA)
764 param.rf_protocol = NCI_RF_PROTOCOL_T3T;
765 else if (protocol == NFC_PROTO_ISO14443 ||
766 protocol == NFC_PROTO_ISO14443_B)
767 param.rf_protocol = NCI_RF_PROTOCOL_ISO_DEP;
768 else
769 param.rf_protocol = NCI_RF_PROTOCOL_NFC_DEP;
770
771 rc = nci_request(ndev, nci_rf_discover_select_req,
772 (unsigned long)&param,
773 msecs_to_jiffies(NCI_RF_DISC_SELECT_TIMEOUT));
774 }
775
776 if (!rc)
777 ndev->target_active_prot = protocol;
778
779 return rc;
780 }
781
782 static void nci_deactivate_target(struct nfc_dev *nfc_dev,
783 struct nfc_target *target)
784 {
785 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
786
787 pr_debug("entry\n");
788
789 if (!ndev->target_active_prot) {
790 pr_err("unable to deactivate target, no active target\n");
791 return;
792 }
793
794 ndev->target_active_prot = 0;
795
796 if (atomic_read(&ndev->state) == NCI_POLL_ACTIVE) {
797 nci_request(ndev, nci_rf_deactivate_req,
798 NCI_DEACTIVATE_TYPE_SLEEP_MODE,
799 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
800 }
801 }
802
803 static int nci_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
804 __u8 comm_mode, __u8 *gb, size_t gb_len)
805 {
806 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
807 int rc;
808
809 pr_debug("target_idx %d, comm_mode %d\n", target->idx, comm_mode);
810
811 rc = nci_activate_target(nfc_dev, target, NFC_PROTO_NFC_DEP);
812 if (rc)
813 return rc;
814
815 rc = nfc_set_remote_general_bytes(nfc_dev, ndev->remote_gb,
816 ndev->remote_gb_len);
817 if (!rc)
818 rc = nfc_dep_link_is_up(nfc_dev, target->idx, NFC_COMM_PASSIVE,
819 NFC_RF_INITIATOR);
820
821 return rc;
822 }
823
824 static int nci_dep_link_down(struct nfc_dev *nfc_dev)
825 {
826 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
827 int rc;
828
829 pr_debug("entry\n");
830
831 if (nfc_dev->rf_mode == NFC_RF_INITIATOR) {
832 nci_deactivate_target(nfc_dev, NULL);
833 } else {
834 if (atomic_read(&ndev->state) == NCI_LISTEN_ACTIVE ||
835 atomic_read(&ndev->state) == NCI_DISCOVERY) {
836 nci_request(ndev, nci_rf_deactivate_req, 0,
837 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
838 }
839
840 rc = nfc_tm_deactivated(nfc_dev);
841 if (rc)
842 pr_err("error when signaling tm deactivation\n");
843 }
844
845 return 0;
846 }
847
848
849 static int nci_transceive(struct nfc_dev *nfc_dev, struct nfc_target *target,
850 struct sk_buff *skb,
851 data_exchange_cb_t cb, void *cb_context)
852 {
853 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
854 int rc;
855 struct nci_conn_info *conn_info;
856
857 conn_info = ndev->rf_conn_info;
858 if (!conn_info)
859 return -EPROTO;
860
861 pr_debug("target_idx %d, len %d\n", target->idx, skb->len);
862
863 if (!ndev->target_active_prot) {
864 pr_err("unable to exchange data, no active target\n");
865 return -EINVAL;
866 }
867
868 if (test_and_set_bit(NCI_DATA_EXCHANGE, &ndev->flags))
869 return -EBUSY;
870
871 /* store cb and context to be used on receiving data */
872 conn_info->data_exchange_cb = cb;
873 conn_info->data_exchange_cb_context = cb_context;
874
875 rc = nci_send_data(ndev, NCI_STATIC_RF_CONN_ID, skb);
876 if (rc)
877 clear_bit(NCI_DATA_EXCHANGE, &ndev->flags);
878
879 return rc;
880 }
881
882 static int nci_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
883 {
884 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
885 int rc;
886
887 rc = nci_send_data(ndev, NCI_STATIC_RF_CONN_ID, skb);
888 if (rc)
889 pr_err("unable to send data\n");
890
891 return rc;
892 }
893
894 static int nci_enable_se(struct nfc_dev *nfc_dev, u32 se_idx)
895 {
896 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
897
898 if (ndev->ops->enable_se)
899 return ndev->ops->enable_se(ndev, se_idx);
900
901 return 0;
902 }
903
904 static int nci_disable_se(struct nfc_dev *nfc_dev, u32 se_idx)
905 {
906 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
907
908 if (ndev->ops->disable_se)
909 return ndev->ops->disable_se(ndev, se_idx);
910
911 return 0;
912 }
913
914 static int nci_discover_se(struct nfc_dev *nfc_dev)
915 {
916 int r;
917 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
918
919 if (ndev->ops->discover_se) {
920 r = nci_nfcee_discover(ndev, NCI_NFCEE_DISCOVERY_ACTION_ENABLE);
921 if (r != NCI_STATUS_OK)
922 return -EPROTO;
923
924 return ndev->ops->discover_se(ndev);
925 }
926
927 return 0;
928 }
929
930 static int nci_se_io(struct nfc_dev *nfc_dev, u32 se_idx,
931 u8 *apdu, size_t apdu_length,
932 se_io_cb_t cb, void *cb_context)
933 {
934 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
935
936 if (ndev->ops->se_io)
937 return ndev->ops->se_io(ndev, se_idx, apdu,
938 apdu_length, cb, cb_context);
939
940 return 0;
941 }
942
943 static int nci_fw_download(struct nfc_dev *nfc_dev, const char *firmware_name)
944 {
945 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
946
947 if (!ndev->ops->fw_download)
948 return -ENOTSUPP;
949
950 return ndev->ops->fw_download(ndev, firmware_name);
951 }
952
953 static struct nfc_ops nci_nfc_ops = {
954 .dev_up = nci_dev_up,
955 .dev_down = nci_dev_down,
956 .start_poll = nci_start_poll,
957 .stop_poll = nci_stop_poll,
958 .dep_link_up = nci_dep_link_up,
959 .dep_link_down = nci_dep_link_down,
960 .activate_target = nci_activate_target,
961 .deactivate_target = nci_deactivate_target,
962 .im_transceive = nci_transceive,
963 .tm_send = nci_tm_send,
964 .enable_se = nci_enable_se,
965 .disable_se = nci_disable_se,
966 .discover_se = nci_discover_se,
967 .se_io = nci_se_io,
968 .fw_download = nci_fw_download,
969 };
970
971 /* ---- Interface to NCI drivers ---- */
972 /**
973 * nci_allocate_device - allocate a new nci device
974 *
975 * @ops: device operations
976 * @supported_protocols: NFC protocols supported by the device
977 */
978 struct nci_dev *nci_allocate_device(struct nci_ops *ops,
979 __u32 supported_protocols,
980 int tx_headroom, int tx_tailroom)
981 {
982 struct nci_dev *ndev;
983
984 pr_debug("supported_protocols 0x%x\n", supported_protocols);
985
986 if (!ops->open || !ops->close || !ops->send)
987 return NULL;
988
989 if (!supported_protocols)
990 return NULL;
991
992 ndev = kzalloc(sizeof(struct nci_dev), GFP_KERNEL);
993 if (!ndev)
994 return NULL;
995
996 ndev->ops = ops;
997
998 if (ops->n_prop_ops > NCI_MAX_PROPRIETARY_CMD) {
999 pr_err("Too many proprietary commands: %zd\n",
1000 ops->n_prop_ops);
1001 ops->prop_ops = NULL;
1002 ops->n_prop_ops = 0;
1003 }
1004
1005 ndev->tx_headroom = tx_headroom;
1006 ndev->tx_tailroom = tx_tailroom;
1007 init_completion(&ndev->req_completion);
1008
1009 ndev->nfc_dev = nfc_allocate_device(&nci_nfc_ops,
1010 supported_protocols,
1011 tx_headroom + NCI_DATA_HDR_SIZE,
1012 tx_tailroom);
1013 if (!ndev->nfc_dev)
1014 goto free_nci;
1015
1016 ndev->hci_dev = nci_hci_allocate(ndev);
1017 if (!ndev->hci_dev)
1018 goto free_nfc;
1019
1020 nfc_set_drvdata(ndev->nfc_dev, ndev);
1021
1022 return ndev;
1023
1024 free_nfc:
1025 kfree(ndev->nfc_dev);
1026
1027 free_nci:
1028 kfree(ndev);
1029 return NULL;
1030 }
1031 EXPORT_SYMBOL(nci_allocate_device);
1032
1033 /**
1034 * nci_free_device - deallocate nci device
1035 *
1036 * @ndev: The nci device to deallocate
1037 */
1038 void nci_free_device(struct nci_dev *ndev)
1039 {
1040 nfc_free_device(ndev->nfc_dev);
1041 kfree(ndev);
1042 }
1043 EXPORT_SYMBOL(nci_free_device);
1044
1045 /**
1046 * nci_register_device - register a nci device in the nfc subsystem
1047 *
1048 * @dev: The nci device to register
1049 */
1050 int nci_register_device(struct nci_dev *ndev)
1051 {
1052 int rc;
1053 struct device *dev = &ndev->nfc_dev->dev;
1054 char name[32];
1055
1056 ndev->flags = 0;
1057
1058 INIT_WORK(&ndev->cmd_work, nci_cmd_work);
1059 snprintf(name, sizeof(name), "%s_nci_cmd_wq", dev_name(dev));
1060 ndev->cmd_wq = create_singlethread_workqueue(name);
1061 if (!ndev->cmd_wq) {
1062 rc = -ENOMEM;
1063 goto exit;
1064 }
1065
1066 INIT_WORK(&ndev->rx_work, nci_rx_work);
1067 snprintf(name, sizeof(name), "%s_nci_rx_wq", dev_name(dev));
1068 ndev->rx_wq = create_singlethread_workqueue(name);
1069 if (!ndev->rx_wq) {
1070 rc = -ENOMEM;
1071 goto destroy_cmd_wq_exit;
1072 }
1073
1074 INIT_WORK(&ndev->tx_work, nci_tx_work);
1075 snprintf(name, sizeof(name), "%s_nci_tx_wq", dev_name(dev));
1076 ndev->tx_wq = create_singlethread_workqueue(name);
1077 if (!ndev->tx_wq) {
1078 rc = -ENOMEM;
1079 goto destroy_rx_wq_exit;
1080 }
1081
1082 skb_queue_head_init(&ndev->cmd_q);
1083 skb_queue_head_init(&ndev->rx_q);
1084 skb_queue_head_init(&ndev->tx_q);
1085
1086 setup_timer(&ndev->cmd_timer, nci_cmd_timer,
1087 (unsigned long) ndev);
1088 setup_timer(&ndev->data_timer, nci_data_timer,
1089 (unsigned long) ndev);
1090
1091 mutex_init(&ndev->req_lock);
1092 INIT_LIST_HEAD(&ndev->conn_info_list);
1093
1094 rc = nfc_register_device(ndev->nfc_dev);
1095 if (rc)
1096 goto destroy_rx_wq_exit;
1097
1098 goto exit;
1099
1100 destroy_rx_wq_exit:
1101 destroy_workqueue(ndev->rx_wq);
1102
1103 destroy_cmd_wq_exit:
1104 destroy_workqueue(ndev->cmd_wq);
1105
1106 exit:
1107 return rc;
1108 }
1109 EXPORT_SYMBOL(nci_register_device);
1110
1111 /**
1112 * nci_unregister_device - unregister a nci device in the nfc subsystem
1113 *
1114 * @dev: The nci device to unregister
1115 */
1116 void nci_unregister_device(struct nci_dev *ndev)
1117 {
1118 struct nci_conn_info *conn_info, *n;
1119
1120 nci_close_device(ndev);
1121
1122 destroy_workqueue(ndev->cmd_wq);
1123 destroy_workqueue(ndev->rx_wq);
1124 destroy_workqueue(ndev->tx_wq);
1125
1126 list_for_each_entry_safe(conn_info, n, &ndev->conn_info_list, list) {
1127 list_del(&conn_info->list);
1128 /* conn_info is allocated with devm_kzalloc */
1129 }
1130
1131 nfc_unregister_device(ndev->nfc_dev);
1132 }
1133 EXPORT_SYMBOL(nci_unregister_device);
1134
1135 /**
1136 * nci_recv_frame - receive frame from NCI drivers
1137 *
1138 * @ndev: The nci device
1139 * @skb: The sk_buff to receive
1140 */
1141 int nci_recv_frame(struct nci_dev *ndev, struct sk_buff *skb)
1142 {
1143 pr_debug("len %d\n", skb->len);
1144
1145 if (!ndev || (!test_bit(NCI_UP, &ndev->flags) &&
1146 !test_bit(NCI_INIT, &ndev->flags))) {
1147 kfree_skb(skb);
1148 return -ENXIO;
1149 }
1150
1151 /* Queue frame for rx worker thread */
1152 skb_queue_tail(&ndev->rx_q, skb);
1153 queue_work(ndev->rx_wq, &ndev->rx_work);
1154
1155 return 0;
1156 }
1157 EXPORT_SYMBOL(nci_recv_frame);
1158
1159 static int nci_send_frame(struct nci_dev *ndev, struct sk_buff *skb)
1160 {
1161 pr_debug("len %d\n", skb->len);
1162
1163 if (!ndev) {
1164 kfree_skb(skb);
1165 return -ENODEV;
1166 }
1167
1168 /* Get rid of skb owner, prior to sending to the driver. */
1169 skb_orphan(skb);
1170
1171 /* Send copy to sniffer */
1172 nfc_send_to_raw_sock(ndev->nfc_dev, skb,
1173 RAW_PAYLOAD_NCI, NFC_DIRECTION_TX);
1174
1175 return ndev->ops->send(ndev, skb);
1176 }
1177
1178 /* Send NCI command */
1179 int nci_send_cmd(struct nci_dev *ndev, __u16 opcode, __u8 plen, void *payload)
1180 {
1181 struct nci_ctrl_hdr *hdr;
1182 struct sk_buff *skb;
1183
1184 pr_debug("opcode 0x%x, plen %d\n", opcode, plen);
1185
1186 skb = nci_skb_alloc(ndev, (NCI_CTRL_HDR_SIZE + plen), GFP_KERNEL);
1187 if (!skb) {
1188 pr_err("no memory for command\n");
1189 return -ENOMEM;
1190 }
1191
1192 hdr = (struct nci_ctrl_hdr *) skb_put(skb, NCI_CTRL_HDR_SIZE);
1193 hdr->gid = nci_opcode_gid(opcode);
1194 hdr->oid = nci_opcode_oid(opcode);
1195 hdr->plen = plen;
1196
1197 nci_mt_set((__u8 *)hdr, NCI_MT_CMD_PKT);
1198 nci_pbf_set((__u8 *)hdr, NCI_PBF_LAST);
1199
1200 if (plen)
1201 memcpy(skb_put(skb, plen), payload, plen);
1202
1203 skb_queue_tail(&ndev->cmd_q, skb);
1204 queue_work(ndev->cmd_wq, &ndev->cmd_work);
1205
1206 return 0;
1207 }
1208
1209 /* Proprietary commands API */
1210 static struct nci_prop_ops *prop_cmd_lookup(struct nci_dev *ndev,
1211 __u16 opcode)
1212 {
1213 size_t i;
1214 struct nci_prop_ops *prop_op;
1215
1216 if (!ndev->ops->prop_ops || !ndev->ops->n_prop_ops)
1217 return NULL;
1218
1219 for (i = 0; i < ndev->ops->n_prop_ops; i++) {
1220 prop_op = &ndev->ops->prop_ops[i];
1221 if (prop_op->opcode == opcode)
1222 return prop_op;
1223 }
1224
1225 return NULL;
1226 }
1227
1228 int nci_prop_rsp_packet(struct nci_dev *ndev, __u16 rsp_opcode,
1229 struct sk_buff *skb)
1230 {
1231 struct nci_prop_ops *prop_op;
1232
1233 prop_op = prop_cmd_lookup(ndev, rsp_opcode);
1234 if (!prop_op || !prop_op->rsp)
1235 return -ENOTSUPP;
1236
1237 return prop_op->rsp(ndev, skb);
1238 }
1239
1240 int nci_prop_ntf_packet(struct nci_dev *ndev, __u16 ntf_opcode,
1241 struct sk_buff *skb)
1242 {
1243 struct nci_prop_ops *prop_op;
1244
1245 prop_op = prop_cmd_lookup(ndev, ntf_opcode);
1246 if (!prop_op || !prop_op->ntf)
1247 return -ENOTSUPP;
1248
1249 return prop_op->ntf(ndev, skb);
1250 }
1251
1252 /* ---- NCI TX Data worker thread ---- */
1253
1254 static void nci_tx_work(struct work_struct *work)
1255 {
1256 struct nci_dev *ndev = container_of(work, struct nci_dev, tx_work);
1257 struct nci_conn_info *conn_info;
1258 struct sk_buff *skb;
1259
1260 conn_info = nci_get_conn_info_by_conn_id(ndev, ndev->cur_conn_id);
1261 if (!conn_info)
1262 return;
1263
1264 pr_debug("credits_cnt %d\n", atomic_read(&conn_info->credits_cnt));
1265
1266 /* Send queued tx data */
1267 while (atomic_read(&conn_info->credits_cnt)) {
1268 skb = skb_dequeue(&ndev->tx_q);
1269 if (!skb)
1270 return;
1271
1272 /* Check if data flow control is used */
1273 if (atomic_read(&conn_info->credits_cnt) !=
1274 NCI_DATA_FLOW_CONTROL_NOT_USED)
1275 atomic_dec(&conn_info->credits_cnt);
1276
1277 pr_debug("NCI TX: MT=data, PBF=%d, conn_id=%d, plen=%d\n",
1278 nci_pbf(skb->data),
1279 nci_conn_id(skb->data),
1280 nci_plen(skb->data));
1281
1282 nci_send_frame(ndev, skb);
1283
1284 mod_timer(&ndev->data_timer,
1285 jiffies + msecs_to_jiffies(NCI_DATA_TIMEOUT));
1286 }
1287 }
1288
1289 /* ----- NCI RX worker thread (data & control) ----- */
1290
1291 static void nci_rx_work(struct work_struct *work)
1292 {
1293 struct nci_dev *ndev = container_of(work, struct nci_dev, rx_work);
1294 struct sk_buff *skb;
1295
1296 while ((skb = skb_dequeue(&ndev->rx_q))) {
1297
1298 /* Send copy to sniffer */
1299 nfc_send_to_raw_sock(ndev->nfc_dev, skb,
1300 RAW_PAYLOAD_NCI, NFC_DIRECTION_RX);
1301
1302 /* Process frame */
1303 switch (nci_mt(skb->data)) {
1304 case NCI_MT_RSP_PKT:
1305 nci_rsp_packet(ndev, skb);
1306 break;
1307
1308 case NCI_MT_NTF_PKT:
1309 nci_ntf_packet(ndev, skb);
1310 break;
1311
1312 case NCI_MT_DATA_PKT:
1313 nci_rx_data_packet(ndev, skb);
1314 break;
1315
1316 default:
1317 pr_err("unknown MT 0x%x\n", nci_mt(skb->data));
1318 kfree_skb(skb);
1319 break;
1320 }
1321 }
1322
1323 /* check if a data exchange timout has occurred */
1324 if (test_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags)) {
1325 /* complete the data exchange transaction, if exists */
1326 if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags))
1327 nci_data_exchange_complete(ndev, NULL,
1328 ndev->cur_conn_id,
1329 -ETIMEDOUT);
1330
1331 clear_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags);
1332 }
1333 }
1334
1335 /* ----- NCI TX CMD worker thread ----- */
1336
1337 static void nci_cmd_work(struct work_struct *work)
1338 {
1339 struct nci_dev *ndev = container_of(work, struct nci_dev, cmd_work);
1340 struct sk_buff *skb;
1341
1342 pr_debug("cmd_cnt %d\n", atomic_read(&ndev->cmd_cnt));
1343
1344 /* Send queued command */
1345 if (atomic_read(&ndev->cmd_cnt)) {
1346 skb = skb_dequeue(&ndev->cmd_q);
1347 if (!skb)
1348 return;
1349
1350 atomic_dec(&ndev->cmd_cnt);
1351
1352 pr_debug("NCI TX: MT=cmd, PBF=%d, GID=0x%x, OID=0x%x, plen=%d\n",
1353 nci_pbf(skb->data),
1354 nci_opcode_gid(nci_opcode(skb->data)),
1355 nci_opcode_oid(nci_opcode(skb->data)),
1356 nci_plen(skb->data));
1357
1358 nci_send_frame(ndev, skb);
1359
1360 mod_timer(&ndev->cmd_timer,
1361 jiffies + msecs_to_jiffies(NCI_CMD_TIMEOUT));
1362 }
1363 }
1364
1365 MODULE_LICENSE("GPL");
This page took 0.065484 seconds and 5 git commands to generate.