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