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