GNU Linux-libre 4.14.265-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                 del_timer_sync(&ndev->cmd_timer);
564                 del_timer_sync(&ndev->data_timer);
565                 mutex_unlock(&ndev->req_lock);
566                 return 0;
567         }
568
569         /* Drop RX and TX queues */
570         skb_queue_purge(&ndev->rx_q);
571         skb_queue_purge(&ndev->tx_q);
572
573         /* Flush RX and TX wq */
574         flush_workqueue(ndev->rx_wq);
575         flush_workqueue(ndev->tx_wq);
576
577         /* Reset device */
578         skb_queue_purge(&ndev->cmd_q);
579         atomic_set(&ndev->cmd_cnt, 1);
580
581         set_bit(NCI_INIT, &ndev->flags);
582         __nci_request(ndev, nci_reset_req, 0,
583                       msecs_to_jiffies(NCI_RESET_TIMEOUT));
584
585         /* After this point our queues are empty
586          * and no works are scheduled.
587          */
588         ndev->ops->close(ndev);
589
590         clear_bit(NCI_INIT, &ndev->flags);
591
592         del_timer_sync(&ndev->cmd_timer);
593
594         /* Flush cmd wq */
595         flush_workqueue(ndev->cmd_wq);
596
597         /* Clear flags except NCI_UNREG */
598         ndev->flags &= BIT(NCI_UNREG);
599
600         mutex_unlock(&ndev->req_lock);
601
602         return 0;
603 }
604
605 /* NCI command timer function */
606 static void nci_cmd_timer(unsigned long arg)
607 {
608         struct nci_dev *ndev = (void *) arg;
609
610         atomic_set(&ndev->cmd_cnt, 1);
611         queue_work(ndev->cmd_wq, &ndev->cmd_work);
612 }
613
614 /* NCI data exchange timer function */
615 static void nci_data_timer(unsigned long arg)
616 {
617         struct nci_dev *ndev = (void *) arg;
618
619         set_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags);
620         queue_work(ndev->rx_wq, &ndev->rx_work);
621 }
622
623 static int nci_dev_up(struct nfc_dev *nfc_dev)
624 {
625         struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
626
627         return nci_open_device(ndev);
628 }
629
630 static int nci_dev_down(struct nfc_dev *nfc_dev)
631 {
632         struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
633
634         return nci_close_device(ndev);
635 }
636
637 int nci_set_config(struct nci_dev *ndev, __u8 id, size_t len, __u8 *val)
638 {
639         struct nci_set_config_param param;
640
641         if (!val || !len)
642                 return 0;
643
644         param.id = id;
645         param.len = len;
646         param.val = val;
647
648         return __nci_request(ndev, nci_set_config_req, (unsigned long)&param,
649                              msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT));
650 }
651 EXPORT_SYMBOL(nci_set_config);
652
653 static void nci_nfcee_discover_req(struct nci_dev *ndev, unsigned long opt)
654 {
655         struct nci_nfcee_discover_cmd cmd;
656         __u8 action = opt;
657
658         cmd.discovery_action = action;
659
660         nci_send_cmd(ndev, NCI_OP_NFCEE_DISCOVER_CMD, 1, &cmd);
661 }
662
663 int nci_nfcee_discover(struct nci_dev *ndev, u8 action)
664 {
665         return __nci_request(ndev, nci_nfcee_discover_req, action,
666                                 msecs_to_jiffies(NCI_CMD_TIMEOUT));
667 }
668 EXPORT_SYMBOL(nci_nfcee_discover);
669
670 static void nci_nfcee_mode_set_req(struct nci_dev *ndev, unsigned long opt)
671 {
672         struct nci_nfcee_mode_set_cmd *cmd =
673                                         (struct nci_nfcee_mode_set_cmd *)opt;
674
675         nci_send_cmd(ndev, NCI_OP_NFCEE_MODE_SET_CMD,
676                      sizeof(struct nci_nfcee_mode_set_cmd), cmd);
677 }
678
679 int nci_nfcee_mode_set(struct nci_dev *ndev, u8 nfcee_id, u8 nfcee_mode)
680 {
681         struct nci_nfcee_mode_set_cmd cmd;
682
683         cmd.nfcee_id = nfcee_id;
684         cmd.nfcee_mode = nfcee_mode;
685
686         return __nci_request(ndev, nci_nfcee_mode_set_req,
687                              (unsigned long)&cmd,
688                              msecs_to_jiffies(NCI_CMD_TIMEOUT));
689 }
690 EXPORT_SYMBOL(nci_nfcee_mode_set);
691
692 static void nci_core_conn_create_req(struct nci_dev *ndev, unsigned long opt)
693 {
694         struct core_conn_create_data *data =
695                                         (struct core_conn_create_data *)opt;
696
697         nci_send_cmd(ndev, NCI_OP_CORE_CONN_CREATE_CMD, data->length, data->cmd);
698 }
699
700 int nci_core_conn_create(struct nci_dev *ndev, u8 destination_type,
701                          u8 number_destination_params,
702                          size_t params_len,
703                          struct core_conn_create_dest_spec_params *params)
704 {
705         int r;
706         struct nci_core_conn_create_cmd *cmd;
707         struct core_conn_create_data data;
708
709         data.length = params_len + sizeof(struct nci_core_conn_create_cmd);
710         cmd = kzalloc(data.length, GFP_KERNEL);
711         if (!cmd)
712                 return -ENOMEM;
713
714         cmd->destination_type = destination_type;
715         cmd->number_destination_params = number_destination_params;
716
717         data.cmd = cmd;
718
719         if (params) {
720                 memcpy(cmd->params, params, params_len);
721                 if (params->length > 0)
722                         memcpy(&ndev->cur_params,
723                                &params->value[DEST_SPEC_PARAMS_ID_INDEX],
724                                sizeof(struct dest_spec_params));
725                 else
726                         ndev->cur_params.id = 0;
727         } else {
728                 ndev->cur_params.id = 0;
729         }
730         ndev->cur_dest_type = destination_type;
731
732         r = __nci_request(ndev, nci_core_conn_create_req, (unsigned long)&data,
733                           msecs_to_jiffies(NCI_CMD_TIMEOUT));
734         kfree(cmd);
735         return r;
736 }
737 EXPORT_SYMBOL(nci_core_conn_create);
738
739 static void nci_core_conn_close_req(struct nci_dev *ndev, unsigned long opt)
740 {
741         __u8 conn_id = opt;
742
743         nci_send_cmd(ndev, NCI_OP_CORE_CONN_CLOSE_CMD, 1, &conn_id);
744 }
745
746 int nci_core_conn_close(struct nci_dev *ndev, u8 conn_id)
747 {
748         ndev->cur_conn_id = conn_id;
749         return __nci_request(ndev, nci_core_conn_close_req, conn_id,
750                              msecs_to_jiffies(NCI_CMD_TIMEOUT));
751 }
752 EXPORT_SYMBOL(nci_core_conn_close);
753
754 static int nci_set_local_general_bytes(struct nfc_dev *nfc_dev)
755 {
756         struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
757         struct nci_set_config_param param;
758         int rc;
759
760         param.val = nfc_get_local_general_bytes(nfc_dev, &param.len);
761         if ((param.val == NULL) || (param.len == 0))
762                 return 0;
763
764         if (param.len > NFC_MAX_GT_LEN)
765                 return -EINVAL;
766
767         param.id = NCI_PN_ATR_REQ_GEN_BYTES;
768
769         rc = nci_request(ndev, nci_set_config_req, (unsigned long)&param,
770                          msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT));
771         if (rc)
772                 return rc;
773
774         param.id = NCI_LN_ATR_RES_GEN_BYTES;
775
776         return nci_request(ndev, nci_set_config_req, (unsigned long)&param,
777                            msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT));
778 }
779
780 static int nci_set_listen_parameters(struct nfc_dev *nfc_dev)
781 {
782         struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
783         int rc;
784         __u8 val;
785
786         val = NCI_LA_SEL_INFO_NFC_DEP_MASK;
787
788         rc = nci_set_config(ndev, NCI_LA_SEL_INFO, 1, &val);
789         if (rc)
790                 return rc;
791
792         val = NCI_LF_PROTOCOL_TYPE_NFC_DEP_MASK;
793
794         rc = nci_set_config(ndev, NCI_LF_PROTOCOL_TYPE, 1, &val);
795         if (rc)
796                 return rc;
797
798         val = NCI_LF_CON_BITR_F_212 | NCI_LF_CON_BITR_F_424;
799
800         return nci_set_config(ndev, NCI_LF_CON_BITR_F, 1, &val);
801 }
802
803 static int nci_start_poll(struct nfc_dev *nfc_dev,
804                           __u32 im_protocols, __u32 tm_protocols)
805 {
806         struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
807         struct nci_rf_discover_param param;
808         int rc;
809
810         if ((atomic_read(&ndev->state) == NCI_DISCOVERY) ||
811             (atomic_read(&ndev->state) == NCI_W4_ALL_DISCOVERIES)) {
812                 pr_err("unable to start poll, since poll is already active\n");
813                 return -EBUSY;
814         }
815
816         if (ndev->target_active_prot) {
817                 pr_err("there is an active target\n");
818                 return -EBUSY;
819         }
820
821         if ((atomic_read(&ndev->state) == NCI_W4_HOST_SELECT) ||
822             (atomic_read(&ndev->state) == NCI_POLL_ACTIVE)) {
823                 pr_debug("target active or w4 select, implicitly deactivate\n");
824
825                 rc = nci_request(ndev, nci_rf_deactivate_req,
826                                  NCI_DEACTIVATE_TYPE_IDLE_MODE,
827                                  msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
828                 if (rc)
829                         return -EBUSY;
830         }
831
832         if ((im_protocols | tm_protocols) & NFC_PROTO_NFC_DEP_MASK) {
833                 rc = nci_set_local_general_bytes(nfc_dev);
834                 if (rc) {
835                         pr_err("failed to set local general bytes\n");
836                         return rc;
837                 }
838         }
839
840         if (tm_protocols & NFC_PROTO_NFC_DEP_MASK) {
841                 rc = nci_set_listen_parameters(nfc_dev);
842                 if (rc)
843                         pr_err("failed to set listen parameters\n");
844         }
845
846         param.im_protocols = im_protocols;
847         param.tm_protocols = tm_protocols;
848         rc = nci_request(ndev, nci_rf_discover_req, (unsigned long)&param,
849                          msecs_to_jiffies(NCI_RF_DISC_TIMEOUT));
850
851         if (!rc)
852                 ndev->poll_prots = im_protocols;
853
854         return rc;
855 }
856
857 static void nci_stop_poll(struct nfc_dev *nfc_dev)
858 {
859         struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
860
861         if ((atomic_read(&ndev->state) != NCI_DISCOVERY) &&
862             (atomic_read(&ndev->state) != NCI_W4_ALL_DISCOVERIES)) {
863                 pr_err("unable to stop poll, since poll is not active\n");
864                 return;
865         }
866
867         nci_request(ndev, nci_rf_deactivate_req, NCI_DEACTIVATE_TYPE_IDLE_MODE,
868                     msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
869 }
870
871 static int nci_activate_target(struct nfc_dev *nfc_dev,
872                                struct nfc_target *target, __u32 protocol)
873 {
874         struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
875         struct nci_rf_discover_select_param param;
876         struct nfc_target *nci_target = NULL;
877         int i;
878         int rc = 0;
879
880         pr_debug("target_idx %d, protocol 0x%x\n", target->idx, protocol);
881
882         if ((atomic_read(&ndev->state) != NCI_W4_HOST_SELECT) &&
883             (atomic_read(&ndev->state) != NCI_POLL_ACTIVE)) {
884                 pr_err("there is no available target to activate\n");
885                 return -EINVAL;
886         }
887
888         if (ndev->target_active_prot) {
889                 pr_err("there is already an active target\n");
890                 return -EBUSY;
891         }
892
893         for (i = 0; i < ndev->n_targets; i++) {
894                 if (ndev->targets[i].idx == target->idx) {
895                         nci_target = &ndev->targets[i];
896                         break;
897                 }
898         }
899
900         if (!nci_target) {
901                 pr_err("unable to find the selected target\n");
902                 return -EINVAL;
903         }
904
905         if (!(nci_target->supported_protocols & (1 << protocol))) {
906                 pr_err("target does not support the requested protocol 0x%x\n",
907                        protocol);
908                 return -EINVAL;
909         }
910
911         if (atomic_read(&ndev->state) == NCI_W4_HOST_SELECT) {
912                 param.rf_discovery_id = nci_target->logical_idx;
913
914                 if (protocol == NFC_PROTO_JEWEL)
915                         param.rf_protocol = NCI_RF_PROTOCOL_T1T;
916                 else if (protocol == NFC_PROTO_MIFARE)
917                         param.rf_protocol = NCI_RF_PROTOCOL_T2T;
918                 else if (protocol == NFC_PROTO_FELICA)
919                         param.rf_protocol = NCI_RF_PROTOCOL_T3T;
920                 else if (protocol == NFC_PROTO_ISO14443 ||
921                          protocol == NFC_PROTO_ISO14443_B)
922                         param.rf_protocol = NCI_RF_PROTOCOL_ISO_DEP;
923                 else
924                         param.rf_protocol = NCI_RF_PROTOCOL_NFC_DEP;
925
926                 rc = nci_request(ndev, nci_rf_discover_select_req,
927                                  (unsigned long)&param,
928                                  msecs_to_jiffies(NCI_RF_DISC_SELECT_TIMEOUT));
929         }
930
931         if (!rc)
932                 ndev->target_active_prot = protocol;
933
934         return rc;
935 }
936
937 static void nci_deactivate_target(struct nfc_dev *nfc_dev,
938                                   struct nfc_target *target,
939                                   __u8 mode)
940 {
941         struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
942         u8 nci_mode = NCI_DEACTIVATE_TYPE_IDLE_MODE;
943
944         pr_debug("entry\n");
945
946         if (!ndev->target_active_prot) {
947                 pr_err("unable to deactivate target, no active target\n");
948                 return;
949         }
950
951         ndev->target_active_prot = 0;
952
953         switch (mode) {
954         case NFC_TARGET_MODE_SLEEP:
955                 nci_mode = NCI_DEACTIVATE_TYPE_SLEEP_MODE;
956                 break;
957         }
958
959         if (atomic_read(&ndev->state) == NCI_POLL_ACTIVE) {
960                 nci_request(ndev, nci_rf_deactivate_req, nci_mode,
961                             msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
962         }
963 }
964
965 static int nci_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
966                            __u8 comm_mode, __u8 *gb, size_t gb_len)
967 {
968         struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
969         int rc;
970
971         pr_debug("target_idx %d, comm_mode %d\n", target->idx, comm_mode);
972
973         rc = nci_activate_target(nfc_dev, target, NFC_PROTO_NFC_DEP);
974         if (rc)
975                 return rc;
976
977         rc = nfc_set_remote_general_bytes(nfc_dev, ndev->remote_gb,
978                                           ndev->remote_gb_len);
979         if (!rc)
980                 rc = nfc_dep_link_is_up(nfc_dev, target->idx, NFC_COMM_PASSIVE,
981                                         NFC_RF_INITIATOR);
982
983         return rc;
984 }
985
986 static int nci_dep_link_down(struct nfc_dev *nfc_dev)
987 {
988         struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
989         int rc;
990
991         pr_debug("entry\n");
992
993         if (nfc_dev->rf_mode == NFC_RF_INITIATOR) {
994                 nci_deactivate_target(nfc_dev, NULL, NCI_DEACTIVATE_TYPE_IDLE_MODE);
995         } else {
996                 if (atomic_read(&ndev->state) == NCI_LISTEN_ACTIVE ||
997                     atomic_read(&ndev->state) == NCI_DISCOVERY) {
998                         nci_request(ndev, nci_rf_deactivate_req, 0,
999                                 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
1000                 }
1001
1002                 rc = nfc_tm_deactivated(nfc_dev);
1003                 if (rc)
1004                         pr_err("error when signaling tm deactivation\n");
1005         }
1006
1007         return 0;
1008 }
1009
1010
1011 static int nci_transceive(struct nfc_dev *nfc_dev, struct nfc_target *target,
1012                           struct sk_buff *skb,
1013                           data_exchange_cb_t cb, void *cb_context)
1014 {
1015         struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1016         int rc;
1017         struct nci_conn_info    *conn_info;
1018
1019         conn_info = ndev->rf_conn_info;
1020         if (!conn_info)
1021                 return -EPROTO;
1022
1023         pr_debug("target_idx %d, len %d\n", target->idx, skb->len);
1024
1025         if (!ndev->target_active_prot) {
1026                 pr_err("unable to exchange data, no active target\n");
1027                 return -EINVAL;
1028         }
1029
1030         if (test_and_set_bit(NCI_DATA_EXCHANGE, &ndev->flags))
1031                 return -EBUSY;
1032
1033         /* store cb and context to be used on receiving data */
1034         conn_info->data_exchange_cb = cb;
1035         conn_info->data_exchange_cb_context = cb_context;
1036
1037         rc = nci_send_data(ndev, NCI_STATIC_RF_CONN_ID, skb);
1038         if (rc)
1039                 clear_bit(NCI_DATA_EXCHANGE, &ndev->flags);
1040
1041         return rc;
1042 }
1043
1044 static int nci_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
1045 {
1046         struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1047         int rc;
1048
1049         rc = nci_send_data(ndev, NCI_STATIC_RF_CONN_ID, skb);
1050         if (rc)
1051                 pr_err("unable to send data\n");
1052
1053         return rc;
1054 }
1055
1056 static int nci_enable_se(struct nfc_dev *nfc_dev, u32 se_idx)
1057 {
1058         struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1059
1060         if (ndev->ops->enable_se)
1061                 return ndev->ops->enable_se(ndev, se_idx);
1062
1063         return 0;
1064 }
1065
1066 static int nci_disable_se(struct nfc_dev *nfc_dev, u32 se_idx)
1067 {
1068         struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1069
1070         if (ndev->ops->disable_se)
1071                 return ndev->ops->disable_se(ndev, se_idx);
1072
1073         return 0;
1074 }
1075
1076 static int nci_discover_se(struct nfc_dev *nfc_dev)
1077 {
1078         int r;
1079         struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1080
1081         if (ndev->ops->discover_se) {
1082                 r = nci_nfcee_discover(ndev, NCI_NFCEE_DISCOVERY_ACTION_ENABLE);
1083                 if (r != NCI_STATUS_OK)
1084                         return -EPROTO;
1085
1086                 return ndev->ops->discover_se(ndev);
1087         }
1088
1089         return 0;
1090 }
1091
1092 static int nci_se_io(struct nfc_dev *nfc_dev, u32 se_idx,
1093                      u8 *apdu, size_t apdu_length,
1094                      se_io_cb_t cb, void *cb_context)
1095 {
1096         struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1097
1098         if (ndev->ops->se_io)
1099                 return ndev->ops->se_io(ndev, se_idx, apdu,
1100                                 apdu_length, cb, cb_context);
1101
1102         return 0;
1103 }
1104
1105 static int nci_fw_download(struct nfc_dev *nfc_dev, const char *firmware_name)
1106 {
1107         struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1108
1109         if (!ndev->ops->fw_download)
1110                 return -ENOTSUPP;
1111
1112         return ndev->ops->fw_download(ndev, firmware_name);
1113 }
1114
1115 static struct nfc_ops nci_nfc_ops = {
1116         .dev_up = nci_dev_up,
1117         .dev_down = nci_dev_down,
1118         .start_poll = nci_start_poll,
1119         .stop_poll = nci_stop_poll,
1120         .dep_link_up = nci_dep_link_up,
1121         .dep_link_down = nci_dep_link_down,
1122         .activate_target = nci_activate_target,
1123         .deactivate_target = nci_deactivate_target,
1124         .im_transceive = nci_transceive,
1125         .tm_send = nci_tm_send,
1126         .enable_se = nci_enable_se,
1127         .disable_se = nci_disable_se,
1128         .discover_se = nci_discover_se,
1129         .se_io = nci_se_io,
1130         .fw_download = nci_fw_download,
1131 };
1132
1133 /* ---- Interface to NCI drivers ---- */
1134 /**
1135  * nci_allocate_device - allocate a new nci device
1136  *
1137  * @ops: device operations
1138  * @supported_protocols: NFC protocols supported by the device
1139  */
1140 struct nci_dev *nci_allocate_device(struct nci_ops *ops,
1141                                     __u32 supported_protocols,
1142                                     int tx_headroom, int tx_tailroom)
1143 {
1144         struct nci_dev *ndev;
1145
1146         pr_debug("supported_protocols 0x%x\n", supported_protocols);
1147
1148         if (!ops->open || !ops->close || !ops->send)
1149                 return NULL;
1150
1151         if (!supported_protocols)
1152                 return NULL;
1153
1154         ndev = kzalloc(sizeof(struct nci_dev), GFP_KERNEL);
1155         if (!ndev)
1156                 return NULL;
1157
1158         ndev->ops = ops;
1159
1160         if (ops->n_prop_ops > NCI_MAX_PROPRIETARY_CMD) {
1161                 pr_err("Too many proprietary commands: %zd\n",
1162                        ops->n_prop_ops);
1163                 ops->prop_ops = NULL;
1164                 ops->n_prop_ops = 0;
1165         }
1166
1167         ndev->tx_headroom = tx_headroom;
1168         ndev->tx_tailroom = tx_tailroom;
1169         init_completion(&ndev->req_completion);
1170
1171         ndev->nfc_dev = nfc_allocate_device(&nci_nfc_ops,
1172                                             supported_protocols,
1173                                             tx_headroom + NCI_DATA_HDR_SIZE,
1174                                             tx_tailroom);
1175         if (!ndev->nfc_dev)
1176                 goto free_nci;
1177
1178         ndev->hci_dev = nci_hci_allocate(ndev);
1179         if (!ndev->hci_dev)
1180                 goto free_nfc;
1181
1182         nfc_set_drvdata(ndev->nfc_dev, ndev);
1183
1184         return ndev;
1185
1186 free_nfc:
1187         nfc_free_device(ndev->nfc_dev);
1188 free_nci:
1189         kfree(ndev);
1190         return NULL;
1191 }
1192 EXPORT_SYMBOL(nci_allocate_device);
1193
1194 /**
1195  * nci_free_device - deallocate nci device
1196  *
1197  * @ndev: The nci device to deallocate
1198  */
1199 void nci_free_device(struct nci_dev *ndev)
1200 {
1201         nfc_free_device(ndev->nfc_dev);
1202         nci_hci_deallocate(ndev);
1203         kfree(ndev);
1204 }
1205 EXPORT_SYMBOL(nci_free_device);
1206
1207 /**
1208  * nci_register_device - register a nci device in the nfc subsystem
1209  *
1210  * @dev: The nci device to register
1211  */
1212 int nci_register_device(struct nci_dev *ndev)
1213 {
1214         int rc;
1215         struct device *dev = &ndev->nfc_dev->dev;
1216         char name[32];
1217
1218         ndev->flags = 0;
1219
1220         INIT_WORK(&ndev->cmd_work, nci_cmd_work);
1221         snprintf(name, sizeof(name), "%s_nci_cmd_wq", dev_name(dev));
1222         ndev->cmd_wq = create_singlethread_workqueue(name);
1223         if (!ndev->cmd_wq) {
1224                 rc = -ENOMEM;
1225                 goto exit;
1226         }
1227
1228         INIT_WORK(&ndev->rx_work, nci_rx_work);
1229         snprintf(name, sizeof(name), "%s_nci_rx_wq", dev_name(dev));
1230         ndev->rx_wq = create_singlethread_workqueue(name);
1231         if (!ndev->rx_wq) {
1232                 rc = -ENOMEM;
1233                 goto destroy_cmd_wq_exit;
1234         }
1235
1236         INIT_WORK(&ndev->tx_work, nci_tx_work);
1237         snprintf(name, sizeof(name), "%s_nci_tx_wq", dev_name(dev));
1238         ndev->tx_wq = create_singlethread_workqueue(name);
1239         if (!ndev->tx_wq) {
1240                 rc = -ENOMEM;
1241                 goto destroy_rx_wq_exit;
1242         }
1243
1244         skb_queue_head_init(&ndev->cmd_q);
1245         skb_queue_head_init(&ndev->rx_q);
1246         skb_queue_head_init(&ndev->tx_q);
1247
1248         setup_timer(&ndev->cmd_timer, nci_cmd_timer,
1249                     (unsigned long) ndev);
1250         setup_timer(&ndev->data_timer, nci_data_timer,
1251                     (unsigned long) ndev);
1252
1253         mutex_init(&ndev->req_lock);
1254         INIT_LIST_HEAD(&ndev->conn_info_list);
1255
1256         rc = nfc_register_device(ndev->nfc_dev);
1257         if (rc)
1258                 goto destroy_rx_wq_exit;
1259
1260         goto exit;
1261
1262 destroy_rx_wq_exit:
1263         destroy_workqueue(ndev->rx_wq);
1264
1265 destroy_cmd_wq_exit:
1266         destroy_workqueue(ndev->cmd_wq);
1267
1268 exit:
1269         return rc;
1270 }
1271 EXPORT_SYMBOL(nci_register_device);
1272
1273 /**
1274  * nci_unregister_device - unregister a nci device in the nfc subsystem
1275  *
1276  * @dev: The nci device to unregister
1277  */
1278 void nci_unregister_device(struct nci_dev *ndev)
1279 {
1280         struct nci_conn_info    *conn_info, *n;
1281
1282         /* This set_bit is not protected with specialized barrier,
1283          * However, it is fine because the mutex_lock(&ndev->req_lock);
1284          * in nci_close_device() will help to emit one.
1285          */
1286         set_bit(NCI_UNREG, &ndev->flags);
1287
1288         nci_close_device(ndev);
1289
1290         destroy_workqueue(ndev->cmd_wq);
1291         destroy_workqueue(ndev->rx_wq);
1292         destroy_workqueue(ndev->tx_wq);
1293
1294         list_for_each_entry_safe(conn_info, n, &ndev->conn_info_list, list) {
1295                 list_del(&conn_info->list);
1296                 /* conn_info is allocated with devm_kzalloc */
1297         }
1298
1299         nfc_unregister_device(ndev->nfc_dev);
1300 }
1301 EXPORT_SYMBOL(nci_unregister_device);
1302
1303 /**
1304  * nci_recv_frame - receive frame from NCI drivers
1305  *
1306  * @ndev: The nci device
1307  * @skb: The sk_buff to receive
1308  */
1309 int nci_recv_frame(struct nci_dev *ndev, struct sk_buff *skb)
1310 {
1311         pr_debug("len %d\n", skb->len);
1312
1313         if (!ndev || (!test_bit(NCI_UP, &ndev->flags) &&
1314             !test_bit(NCI_INIT, &ndev->flags))) {
1315                 kfree_skb(skb);
1316                 return -ENXIO;
1317         }
1318
1319         /* Queue frame for rx worker thread */
1320         skb_queue_tail(&ndev->rx_q, skb);
1321         queue_work(ndev->rx_wq, &ndev->rx_work);
1322
1323         return 0;
1324 }
1325 EXPORT_SYMBOL(nci_recv_frame);
1326
1327 int nci_send_frame(struct nci_dev *ndev, struct sk_buff *skb)
1328 {
1329         pr_debug("len %d\n", skb->len);
1330
1331         if (!ndev) {
1332                 kfree_skb(skb);
1333                 return -ENODEV;
1334         }
1335
1336         /* Get rid of skb owner, prior to sending to the driver. */
1337         skb_orphan(skb);
1338
1339         /* Send copy to sniffer */
1340         nfc_send_to_raw_sock(ndev->nfc_dev, skb,
1341                              RAW_PAYLOAD_NCI, NFC_DIRECTION_TX);
1342
1343         return ndev->ops->send(ndev, skb);
1344 }
1345 EXPORT_SYMBOL(nci_send_frame);
1346
1347 /* Send NCI command */
1348 int nci_send_cmd(struct nci_dev *ndev, __u16 opcode, __u8 plen, void *payload)
1349 {
1350         struct nci_ctrl_hdr *hdr;
1351         struct sk_buff *skb;
1352
1353         pr_debug("opcode 0x%x, plen %d\n", opcode, plen);
1354
1355         skb = nci_skb_alloc(ndev, (NCI_CTRL_HDR_SIZE + plen), GFP_KERNEL);
1356         if (!skb) {
1357                 pr_err("no memory for command\n");
1358                 return -ENOMEM;
1359         }
1360
1361         hdr = skb_put(skb, NCI_CTRL_HDR_SIZE);
1362         hdr->gid = nci_opcode_gid(opcode);
1363         hdr->oid = nci_opcode_oid(opcode);
1364         hdr->plen = plen;
1365
1366         nci_mt_set((__u8 *)hdr, NCI_MT_CMD_PKT);
1367         nci_pbf_set((__u8 *)hdr, NCI_PBF_LAST);
1368
1369         if (plen)
1370                 skb_put_data(skb, payload, plen);
1371
1372         skb_queue_tail(&ndev->cmd_q, skb);
1373         queue_work(ndev->cmd_wq, &ndev->cmd_work);
1374
1375         return 0;
1376 }
1377 EXPORT_SYMBOL(nci_send_cmd);
1378
1379 /* Proprietary commands API */
1380 static struct nci_driver_ops *ops_cmd_lookup(struct nci_driver_ops *ops,
1381                                              size_t n_ops,
1382                                              __u16 opcode)
1383 {
1384         size_t i;
1385         struct nci_driver_ops *op;
1386
1387         if (!ops || !n_ops)
1388                 return NULL;
1389
1390         for (i = 0; i < n_ops; i++) {
1391                 op = &ops[i];
1392                 if (op->opcode == opcode)
1393                         return op;
1394         }
1395
1396         return NULL;
1397 }
1398
1399 static int nci_op_rsp_packet(struct nci_dev *ndev, __u16 rsp_opcode,
1400                              struct sk_buff *skb, struct nci_driver_ops *ops,
1401                              size_t n_ops)
1402 {
1403         struct nci_driver_ops *op;
1404
1405         op = ops_cmd_lookup(ops, n_ops, rsp_opcode);
1406         if (!op || !op->rsp)
1407                 return -ENOTSUPP;
1408
1409         return op->rsp(ndev, skb);
1410 }
1411
1412 static int nci_op_ntf_packet(struct nci_dev *ndev, __u16 ntf_opcode,
1413                              struct sk_buff *skb, struct nci_driver_ops *ops,
1414                              size_t n_ops)
1415 {
1416         struct nci_driver_ops *op;
1417
1418         op = ops_cmd_lookup(ops, n_ops, ntf_opcode);
1419         if (!op || !op->ntf)
1420                 return -ENOTSUPP;
1421
1422         return op->ntf(ndev, skb);
1423 }
1424
1425 int nci_prop_rsp_packet(struct nci_dev *ndev, __u16 opcode,
1426                         struct sk_buff *skb)
1427 {
1428         return nci_op_rsp_packet(ndev, opcode, skb, ndev->ops->prop_ops,
1429                                  ndev->ops->n_prop_ops);
1430 }
1431
1432 int nci_prop_ntf_packet(struct nci_dev *ndev, __u16 opcode,
1433                         struct sk_buff *skb)
1434 {
1435         return nci_op_ntf_packet(ndev, opcode, skb, ndev->ops->prop_ops,
1436                                  ndev->ops->n_prop_ops);
1437 }
1438
1439 int nci_core_rsp_packet(struct nci_dev *ndev, __u16 opcode,
1440                         struct sk_buff *skb)
1441 {
1442         return nci_op_rsp_packet(ndev, opcode, skb, ndev->ops->core_ops,
1443                                   ndev->ops->n_core_ops);
1444 }
1445
1446 int nci_core_ntf_packet(struct nci_dev *ndev, __u16 opcode,
1447                         struct sk_buff *skb)
1448 {
1449         return nci_op_ntf_packet(ndev, opcode, skb, ndev->ops->core_ops,
1450                                  ndev->ops->n_core_ops);
1451 }
1452
1453 /* ---- NCI TX Data worker thread ---- */
1454
1455 static void nci_tx_work(struct work_struct *work)
1456 {
1457         struct nci_dev *ndev = container_of(work, struct nci_dev, tx_work);
1458         struct nci_conn_info    *conn_info;
1459         struct sk_buff *skb;
1460
1461         conn_info = nci_get_conn_info_by_conn_id(ndev, ndev->cur_conn_id);
1462         if (!conn_info)
1463                 return;
1464
1465         pr_debug("credits_cnt %d\n", atomic_read(&conn_info->credits_cnt));
1466
1467         /* Send queued tx data */
1468         while (atomic_read(&conn_info->credits_cnt)) {
1469                 skb = skb_dequeue(&ndev->tx_q);
1470                 if (!skb)
1471                         return;
1472
1473                 /* Check if data flow control is used */
1474                 if (atomic_read(&conn_info->credits_cnt) !=
1475                     NCI_DATA_FLOW_CONTROL_NOT_USED)
1476                         atomic_dec(&conn_info->credits_cnt);
1477
1478                 pr_debug("NCI TX: MT=data, PBF=%d, conn_id=%d, plen=%d\n",
1479                          nci_pbf(skb->data),
1480                          nci_conn_id(skb->data),
1481                          nci_plen(skb->data));
1482
1483                 nci_send_frame(ndev, skb);
1484
1485                 mod_timer(&ndev->data_timer,
1486                           jiffies + msecs_to_jiffies(NCI_DATA_TIMEOUT));
1487         }
1488 }
1489
1490 /* ----- NCI RX worker thread (data & control) ----- */
1491
1492 static void nci_rx_work(struct work_struct *work)
1493 {
1494         struct nci_dev *ndev = container_of(work, struct nci_dev, rx_work);
1495         struct sk_buff *skb;
1496
1497         while ((skb = skb_dequeue(&ndev->rx_q))) {
1498
1499                 /* Send copy to sniffer */
1500                 nfc_send_to_raw_sock(ndev->nfc_dev, skb,
1501                                      RAW_PAYLOAD_NCI, NFC_DIRECTION_RX);
1502
1503                 /* Process frame */
1504                 switch (nci_mt(skb->data)) {
1505                 case NCI_MT_RSP_PKT:
1506                         nci_rsp_packet(ndev, skb);
1507                         break;
1508
1509                 case NCI_MT_NTF_PKT:
1510                         nci_ntf_packet(ndev, skb);
1511                         break;
1512
1513                 case NCI_MT_DATA_PKT:
1514                         nci_rx_data_packet(ndev, skb);
1515                         break;
1516
1517                 default:
1518                         pr_err("unknown MT 0x%x\n", nci_mt(skb->data));
1519                         kfree_skb(skb);
1520                         break;
1521                 }
1522         }
1523
1524         /* check if a data exchange timout has occurred */
1525         if (test_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags)) {
1526                 /* complete the data exchange transaction, if exists */
1527                 if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags))
1528                         nci_data_exchange_complete(ndev, NULL,
1529                                                    ndev->cur_conn_id,
1530                                                    -ETIMEDOUT);
1531
1532                 clear_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags);
1533         }
1534 }
1535
1536 /* ----- NCI TX CMD worker thread ----- */
1537
1538 static void nci_cmd_work(struct work_struct *work)
1539 {
1540         struct nci_dev *ndev = container_of(work, struct nci_dev, cmd_work);
1541         struct sk_buff *skb;
1542
1543         pr_debug("cmd_cnt %d\n", atomic_read(&ndev->cmd_cnt));
1544
1545         /* Send queued command */
1546         if (atomic_read(&ndev->cmd_cnt)) {
1547                 skb = skb_dequeue(&ndev->cmd_q);
1548                 if (!skb)
1549                         return;
1550
1551                 atomic_dec(&ndev->cmd_cnt);
1552
1553                 pr_debug("NCI TX: MT=cmd, PBF=%d, GID=0x%x, OID=0x%x, plen=%d\n",
1554                          nci_pbf(skb->data),
1555                          nci_opcode_gid(nci_opcode(skb->data)),
1556                          nci_opcode_oid(nci_opcode(skb->data)),
1557                          nci_plen(skb->data));
1558
1559                 nci_send_frame(ndev, skb);
1560
1561                 mod_timer(&ndev->cmd_timer,
1562                           jiffies + msecs_to_jiffies(NCI_CMD_TIMEOUT));
1563         }
1564 }
1565
1566 MODULE_LICENSE("GPL");