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