GNU Linux-libre 4.14.332-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 &= BIT(NCI_UNREG);
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 (protocol >= NFC_PROTO_MAX) {
910                 pr_err("the requested nfc protocol is invalid\n");
911                 return -EINVAL;
912         }
913
914         if (!(nci_target->supported_protocols & (1 << protocol))) {
915                 pr_err("target does not support the requested protocol 0x%x\n",
916                        protocol);
917                 return -EINVAL;
918         }
919
920         if (atomic_read(&ndev->state) == NCI_W4_HOST_SELECT) {
921                 param.rf_discovery_id = nci_target->logical_idx;
922
923                 if (protocol == NFC_PROTO_JEWEL)
924                         param.rf_protocol = NCI_RF_PROTOCOL_T1T;
925                 else if (protocol == NFC_PROTO_MIFARE)
926                         param.rf_protocol = NCI_RF_PROTOCOL_T2T;
927                 else if (protocol == NFC_PROTO_FELICA)
928                         param.rf_protocol = NCI_RF_PROTOCOL_T3T;
929                 else if (protocol == NFC_PROTO_ISO14443 ||
930                          protocol == NFC_PROTO_ISO14443_B)
931                         param.rf_protocol = NCI_RF_PROTOCOL_ISO_DEP;
932                 else
933                         param.rf_protocol = NCI_RF_PROTOCOL_NFC_DEP;
934
935                 rc = nci_request(ndev, nci_rf_discover_select_req,
936                                  (unsigned long)&param,
937                                  msecs_to_jiffies(NCI_RF_DISC_SELECT_TIMEOUT));
938         }
939
940         if (!rc)
941                 ndev->target_active_prot = protocol;
942
943         return rc;
944 }
945
946 static void nci_deactivate_target(struct nfc_dev *nfc_dev,
947                                   struct nfc_target *target,
948                                   __u8 mode)
949 {
950         struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
951         u8 nci_mode = NCI_DEACTIVATE_TYPE_IDLE_MODE;
952
953         pr_debug("entry\n");
954
955         if (!ndev->target_active_prot) {
956                 pr_err("unable to deactivate target, no active target\n");
957                 return;
958         }
959
960         ndev->target_active_prot = 0;
961
962         switch (mode) {
963         case NFC_TARGET_MODE_SLEEP:
964                 nci_mode = NCI_DEACTIVATE_TYPE_SLEEP_MODE;
965                 break;
966         }
967
968         if (atomic_read(&ndev->state) == NCI_POLL_ACTIVE) {
969                 nci_request(ndev, nci_rf_deactivate_req, nci_mode,
970                             msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
971         }
972 }
973
974 static int nci_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
975                            __u8 comm_mode, __u8 *gb, size_t gb_len)
976 {
977         struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
978         int rc;
979
980         pr_debug("target_idx %d, comm_mode %d\n", target->idx, comm_mode);
981
982         rc = nci_activate_target(nfc_dev, target, NFC_PROTO_NFC_DEP);
983         if (rc)
984                 return rc;
985
986         rc = nfc_set_remote_general_bytes(nfc_dev, ndev->remote_gb,
987                                           ndev->remote_gb_len);
988         if (!rc)
989                 rc = nfc_dep_link_is_up(nfc_dev, target->idx, NFC_COMM_PASSIVE,
990                                         NFC_RF_INITIATOR);
991
992         return rc;
993 }
994
995 static int nci_dep_link_down(struct nfc_dev *nfc_dev)
996 {
997         struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
998         int rc;
999
1000         pr_debug("entry\n");
1001
1002         if (nfc_dev->rf_mode == NFC_RF_INITIATOR) {
1003                 nci_deactivate_target(nfc_dev, NULL, NCI_DEACTIVATE_TYPE_IDLE_MODE);
1004         } else {
1005                 if (atomic_read(&ndev->state) == NCI_LISTEN_ACTIVE ||
1006                     atomic_read(&ndev->state) == NCI_DISCOVERY) {
1007                         nci_request(ndev, nci_rf_deactivate_req, 0,
1008                                 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
1009                 }
1010
1011                 rc = nfc_tm_deactivated(nfc_dev);
1012                 if (rc)
1013                         pr_err("error when signaling tm deactivation\n");
1014         }
1015
1016         return 0;
1017 }
1018
1019
1020 static int nci_transceive(struct nfc_dev *nfc_dev, struct nfc_target *target,
1021                           struct sk_buff *skb,
1022                           data_exchange_cb_t cb, void *cb_context)
1023 {
1024         struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1025         int rc;
1026         struct nci_conn_info    *conn_info;
1027
1028         conn_info = ndev->rf_conn_info;
1029         if (!conn_info)
1030                 return -EPROTO;
1031
1032         pr_debug("target_idx %d, len %d\n", target->idx, skb->len);
1033
1034         if (!ndev->target_active_prot) {
1035                 pr_err("unable to exchange data, no active target\n");
1036                 return -EINVAL;
1037         }
1038
1039         if (test_and_set_bit(NCI_DATA_EXCHANGE, &ndev->flags))
1040                 return -EBUSY;
1041
1042         /* store cb and context to be used on receiving data */
1043         conn_info->data_exchange_cb = cb;
1044         conn_info->data_exchange_cb_context = cb_context;
1045
1046         rc = nci_send_data(ndev, NCI_STATIC_RF_CONN_ID, skb);
1047         if (rc)
1048                 clear_bit(NCI_DATA_EXCHANGE, &ndev->flags);
1049
1050         return rc;
1051 }
1052
1053 static int nci_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
1054 {
1055         struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1056         int rc;
1057
1058         rc = nci_send_data(ndev, NCI_STATIC_RF_CONN_ID, skb);
1059         if (rc)
1060                 pr_err("unable to send data\n");
1061
1062         return rc;
1063 }
1064
1065 static int nci_enable_se(struct nfc_dev *nfc_dev, u32 se_idx)
1066 {
1067         struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1068
1069         if (ndev->ops->enable_se)
1070                 return ndev->ops->enable_se(ndev, se_idx);
1071
1072         return 0;
1073 }
1074
1075 static int nci_disable_se(struct nfc_dev *nfc_dev, u32 se_idx)
1076 {
1077         struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1078
1079         if (ndev->ops->disable_se)
1080                 return ndev->ops->disable_se(ndev, se_idx);
1081
1082         return 0;
1083 }
1084
1085 static int nci_discover_se(struct nfc_dev *nfc_dev)
1086 {
1087         int r;
1088         struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1089
1090         if (ndev->ops->discover_se) {
1091                 r = nci_nfcee_discover(ndev, NCI_NFCEE_DISCOVERY_ACTION_ENABLE);
1092                 if (r != NCI_STATUS_OK)
1093                         return -EPROTO;
1094
1095                 return ndev->ops->discover_se(ndev);
1096         }
1097
1098         return 0;
1099 }
1100
1101 static int nci_se_io(struct nfc_dev *nfc_dev, u32 se_idx,
1102                      u8 *apdu, size_t apdu_length,
1103                      se_io_cb_t cb, void *cb_context)
1104 {
1105         struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1106
1107         if (ndev->ops->se_io)
1108                 return ndev->ops->se_io(ndev, se_idx, apdu,
1109                                 apdu_length, cb, cb_context);
1110
1111         return 0;
1112 }
1113
1114 static int nci_fw_download(struct nfc_dev *nfc_dev, const char *firmware_name)
1115 {
1116         struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1117
1118         if (!ndev->ops->fw_download)
1119                 return -ENOTSUPP;
1120
1121         return ndev->ops->fw_download(ndev, firmware_name);
1122 }
1123
1124 static struct nfc_ops nci_nfc_ops = {
1125         .dev_up = nci_dev_up,
1126         .dev_down = nci_dev_down,
1127         .start_poll = nci_start_poll,
1128         .stop_poll = nci_stop_poll,
1129         .dep_link_up = nci_dep_link_up,
1130         .dep_link_down = nci_dep_link_down,
1131         .activate_target = nci_activate_target,
1132         .deactivate_target = nci_deactivate_target,
1133         .im_transceive = nci_transceive,
1134         .tm_send = nci_tm_send,
1135         .enable_se = nci_enable_se,
1136         .disable_se = nci_disable_se,
1137         .discover_se = nci_discover_se,
1138         .se_io = nci_se_io,
1139         .fw_download = nci_fw_download,
1140 };
1141
1142 /* ---- Interface to NCI drivers ---- */
1143 /**
1144  * nci_allocate_device - allocate a new nci device
1145  *
1146  * @ops: device operations
1147  * @supported_protocols: NFC protocols supported by the device
1148  */
1149 struct nci_dev *nci_allocate_device(struct nci_ops *ops,
1150                                     __u32 supported_protocols,
1151                                     int tx_headroom, int tx_tailroom)
1152 {
1153         struct nci_dev *ndev;
1154
1155         pr_debug("supported_protocols 0x%x\n", supported_protocols);
1156
1157         if (!ops->open || !ops->close || !ops->send)
1158                 return NULL;
1159
1160         if (!supported_protocols)
1161                 return NULL;
1162
1163         ndev = kzalloc(sizeof(struct nci_dev), GFP_KERNEL);
1164         if (!ndev)
1165                 return NULL;
1166
1167         ndev->ops = ops;
1168
1169         if (ops->n_prop_ops > NCI_MAX_PROPRIETARY_CMD) {
1170                 pr_err("Too many proprietary commands: %zd\n",
1171                        ops->n_prop_ops);
1172                 ops->prop_ops = NULL;
1173                 ops->n_prop_ops = 0;
1174         }
1175
1176         ndev->tx_headroom = tx_headroom;
1177         ndev->tx_tailroom = tx_tailroom;
1178         init_completion(&ndev->req_completion);
1179
1180         ndev->nfc_dev = nfc_allocate_device(&nci_nfc_ops,
1181                                             supported_protocols,
1182                                             tx_headroom + NCI_DATA_HDR_SIZE,
1183                                             tx_tailroom);
1184         if (!ndev->nfc_dev)
1185                 goto free_nci;
1186
1187         ndev->hci_dev = nci_hci_allocate(ndev);
1188         if (!ndev->hci_dev)
1189                 goto free_nfc;
1190
1191         nfc_set_drvdata(ndev->nfc_dev, ndev);
1192
1193         return ndev;
1194
1195 free_nfc:
1196         nfc_free_device(ndev->nfc_dev);
1197 free_nci:
1198         kfree(ndev);
1199         return NULL;
1200 }
1201 EXPORT_SYMBOL(nci_allocate_device);
1202
1203 /**
1204  * nci_free_device - deallocate nci device
1205  *
1206  * @ndev: The nci device to deallocate
1207  */
1208 void nci_free_device(struct nci_dev *ndev)
1209 {
1210         nfc_free_device(ndev->nfc_dev);
1211         nci_hci_deallocate(ndev);
1212         kfree(ndev);
1213 }
1214 EXPORT_SYMBOL(nci_free_device);
1215
1216 /**
1217  * nci_register_device - register a nci device in the nfc subsystem
1218  *
1219  * @dev: The nci device to register
1220  */
1221 int nci_register_device(struct nci_dev *ndev)
1222 {
1223         int rc;
1224         struct device *dev = &ndev->nfc_dev->dev;
1225         char name[32];
1226
1227         ndev->flags = 0;
1228
1229         INIT_WORK(&ndev->cmd_work, nci_cmd_work);
1230         snprintf(name, sizeof(name), "%s_nci_cmd_wq", dev_name(dev));
1231         ndev->cmd_wq = create_singlethread_workqueue(name);
1232         if (!ndev->cmd_wq) {
1233                 rc = -ENOMEM;
1234                 goto exit;
1235         }
1236
1237         INIT_WORK(&ndev->rx_work, nci_rx_work);
1238         snprintf(name, sizeof(name), "%s_nci_rx_wq", dev_name(dev));
1239         ndev->rx_wq = create_singlethread_workqueue(name);
1240         if (!ndev->rx_wq) {
1241                 rc = -ENOMEM;
1242                 goto destroy_cmd_wq_exit;
1243         }
1244
1245         INIT_WORK(&ndev->tx_work, nci_tx_work);
1246         snprintf(name, sizeof(name), "%s_nci_tx_wq", dev_name(dev));
1247         ndev->tx_wq = create_singlethread_workqueue(name);
1248         if (!ndev->tx_wq) {
1249                 rc = -ENOMEM;
1250                 goto destroy_rx_wq_exit;
1251         }
1252
1253         skb_queue_head_init(&ndev->cmd_q);
1254         skb_queue_head_init(&ndev->rx_q);
1255         skb_queue_head_init(&ndev->tx_q);
1256
1257         setup_timer(&ndev->cmd_timer, nci_cmd_timer,
1258                     (unsigned long) ndev);
1259         setup_timer(&ndev->data_timer, nci_data_timer,
1260                     (unsigned long) ndev);
1261
1262         mutex_init(&ndev->req_lock);
1263         INIT_LIST_HEAD(&ndev->conn_info_list);
1264
1265         rc = nfc_register_device(ndev->nfc_dev);
1266         if (rc)
1267                 goto destroy_rx_wq_exit;
1268
1269         goto exit;
1270
1271 destroy_rx_wq_exit:
1272         destroy_workqueue(ndev->rx_wq);
1273
1274 destroy_cmd_wq_exit:
1275         destroy_workqueue(ndev->cmd_wq);
1276
1277 exit:
1278         return rc;
1279 }
1280 EXPORT_SYMBOL(nci_register_device);
1281
1282 /**
1283  * nci_unregister_device - unregister a nci device in the nfc subsystem
1284  *
1285  * @dev: The nci device to unregister
1286  */
1287 void nci_unregister_device(struct nci_dev *ndev)
1288 {
1289         struct nci_conn_info    *conn_info, *n;
1290
1291         /* This set_bit is not protected with specialized barrier,
1292          * However, it is fine because the mutex_lock(&ndev->req_lock);
1293          * in nci_close_device() will help to emit one.
1294          */
1295         set_bit(NCI_UNREG, &ndev->flags);
1296
1297         nci_close_device(ndev);
1298
1299         destroy_workqueue(ndev->cmd_wq);
1300         destroy_workqueue(ndev->rx_wq);
1301         destroy_workqueue(ndev->tx_wq);
1302
1303         list_for_each_entry_safe(conn_info, n, &ndev->conn_info_list, list) {
1304                 list_del(&conn_info->list);
1305                 /* conn_info is allocated with devm_kzalloc */
1306         }
1307
1308         nfc_unregister_device(ndev->nfc_dev);
1309 }
1310 EXPORT_SYMBOL(nci_unregister_device);
1311
1312 /**
1313  * nci_recv_frame - receive frame from NCI drivers
1314  *
1315  * @ndev: The nci device
1316  * @skb: The sk_buff to receive
1317  */
1318 int nci_recv_frame(struct nci_dev *ndev, struct sk_buff *skb)
1319 {
1320         pr_debug("len %d\n", skb->len);
1321
1322         if (!ndev || (!test_bit(NCI_UP, &ndev->flags) &&
1323             !test_bit(NCI_INIT, &ndev->flags))) {
1324                 kfree_skb(skb);
1325                 return -ENXIO;
1326         }
1327
1328         /* Queue frame for rx worker thread */
1329         skb_queue_tail(&ndev->rx_q, skb);
1330         queue_work(ndev->rx_wq, &ndev->rx_work);
1331
1332         return 0;
1333 }
1334 EXPORT_SYMBOL(nci_recv_frame);
1335
1336 int nci_send_frame(struct nci_dev *ndev, struct sk_buff *skb)
1337 {
1338         pr_debug("len %d\n", skb->len);
1339
1340         if (!ndev) {
1341                 kfree_skb(skb);
1342                 return -ENODEV;
1343         }
1344
1345         /* Get rid of skb owner, prior to sending to the driver. */
1346         skb_orphan(skb);
1347
1348         /* Send copy to sniffer */
1349         nfc_send_to_raw_sock(ndev->nfc_dev, skb,
1350                              RAW_PAYLOAD_NCI, NFC_DIRECTION_TX);
1351
1352         return ndev->ops->send(ndev, skb);
1353 }
1354 EXPORT_SYMBOL(nci_send_frame);
1355
1356 /* Send NCI command */
1357 int nci_send_cmd(struct nci_dev *ndev, __u16 opcode, __u8 plen, void *payload)
1358 {
1359         struct nci_ctrl_hdr *hdr;
1360         struct sk_buff *skb;
1361
1362         pr_debug("opcode 0x%x, plen %d\n", opcode, plen);
1363
1364         skb = nci_skb_alloc(ndev, (NCI_CTRL_HDR_SIZE + plen), GFP_KERNEL);
1365         if (!skb) {
1366                 pr_err("no memory for command\n");
1367                 return -ENOMEM;
1368         }
1369
1370         hdr = skb_put(skb, NCI_CTRL_HDR_SIZE);
1371         hdr->gid = nci_opcode_gid(opcode);
1372         hdr->oid = nci_opcode_oid(opcode);
1373         hdr->plen = plen;
1374
1375         nci_mt_set((__u8 *)hdr, NCI_MT_CMD_PKT);
1376         nci_pbf_set((__u8 *)hdr, NCI_PBF_LAST);
1377
1378         if (plen)
1379                 skb_put_data(skb, payload, plen);
1380
1381         skb_queue_tail(&ndev->cmd_q, skb);
1382         queue_work(ndev->cmd_wq, &ndev->cmd_work);
1383
1384         return 0;
1385 }
1386 EXPORT_SYMBOL(nci_send_cmd);
1387
1388 /* Proprietary commands API */
1389 static struct nci_driver_ops *ops_cmd_lookup(struct nci_driver_ops *ops,
1390                                              size_t n_ops,
1391                                              __u16 opcode)
1392 {
1393         size_t i;
1394         struct nci_driver_ops *op;
1395
1396         if (!ops || !n_ops)
1397                 return NULL;
1398
1399         for (i = 0; i < n_ops; i++) {
1400                 op = &ops[i];
1401                 if (op->opcode == opcode)
1402                         return op;
1403         }
1404
1405         return NULL;
1406 }
1407
1408 static int nci_op_rsp_packet(struct nci_dev *ndev, __u16 rsp_opcode,
1409                              struct sk_buff *skb, struct nci_driver_ops *ops,
1410                              size_t n_ops)
1411 {
1412         struct nci_driver_ops *op;
1413
1414         op = ops_cmd_lookup(ops, n_ops, rsp_opcode);
1415         if (!op || !op->rsp)
1416                 return -ENOTSUPP;
1417
1418         return op->rsp(ndev, skb);
1419 }
1420
1421 static int nci_op_ntf_packet(struct nci_dev *ndev, __u16 ntf_opcode,
1422                              struct sk_buff *skb, struct nci_driver_ops *ops,
1423                              size_t n_ops)
1424 {
1425         struct nci_driver_ops *op;
1426
1427         op = ops_cmd_lookup(ops, n_ops, ntf_opcode);
1428         if (!op || !op->ntf)
1429                 return -ENOTSUPP;
1430
1431         return op->ntf(ndev, skb);
1432 }
1433
1434 int nci_prop_rsp_packet(struct nci_dev *ndev, __u16 opcode,
1435                         struct sk_buff *skb)
1436 {
1437         return nci_op_rsp_packet(ndev, opcode, skb, ndev->ops->prop_ops,
1438                                  ndev->ops->n_prop_ops);
1439 }
1440
1441 int nci_prop_ntf_packet(struct nci_dev *ndev, __u16 opcode,
1442                         struct sk_buff *skb)
1443 {
1444         return nci_op_ntf_packet(ndev, opcode, skb, ndev->ops->prop_ops,
1445                                  ndev->ops->n_prop_ops);
1446 }
1447
1448 int nci_core_rsp_packet(struct nci_dev *ndev, __u16 opcode,
1449                         struct sk_buff *skb)
1450 {
1451         return nci_op_rsp_packet(ndev, opcode, skb, ndev->ops->core_ops,
1452                                   ndev->ops->n_core_ops);
1453 }
1454
1455 int nci_core_ntf_packet(struct nci_dev *ndev, __u16 opcode,
1456                         struct sk_buff *skb)
1457 {
1458         return nci_op_ntf_packet(ndev, opcode, skb, ndev->ops->core_ops,
1459                                  ndev->ops->n_core_ops);
1460 }
1461
1462 /* ---- NCI TX Data worker thread ---- */
1463
1464 static void nci_tx_work(struct work_struct *work)
1465 {
1466         struct nci_dev *ndev = container_of(work, struct nci_dev, tx_work);
1467         struct nci_conn_info    *conn_info;
1468         struct sk_buff *skb;
1469
1470         conn_info = nci_get_conn_info_by_conn_id(ndev, ndev->cur_conn_id);
1471         if (!conn_info)
1472                 return;
1473
1474         pr_debug("credits_cnt %d\n", atomic_read(&conn_info->credits_cnt));
1475
1476         /* Send queued tx data */
1477         while (atomic_read(&conn_info->credits_cnt)) {
1478                 skb = skb_dequeue(&ndev->tx_q);
1479                 if (!skb)
1480                         return;
1481
1482                 /* Check if data flow control is used */
1483                 if (atomic_read(&conn_info->credits_cnt) !=
1484                     NCI_DATA_FLOW_CONTROL_NOT_USED)
1485                         atomic_dec(&conn_info->credits_cnt);
1486
1487                 pr_debug("NCI TX: MT=data, PBF=%d, conn_id=%d, plen=%d\n",
1488                          nci_pbf(skb->data),
1489                          nci_conn_id(skb->data),
1490                          nci_plen(skb->data));
1491
1492                 nci_send_frame(ndev, skb);
1493
1494                 mod_timer(&ndev->data_timer,
1495                           jiffies + msecs_to_jiffies(NCI_DATA_TIMEOUT));
1496         }
1497 }
1498
1499 /* ----- NCI RX worker thread (data & control) ----- */
1500
1501 static void nci_rx_work(struct work_struct *work)
1502 {
1503         struct nci_dev *ndev = container_of(work, struct nci_dev, rx_work);
1504         struct sk_buff *skb;
1505
1506         while ((skb = skb_dequeue(&ndev->rx_q))) {
1507
1508                 /* Send copy to sniffer */
1509                 nfc_send_to_raw_sock(ndev->nfc_dev, skb,
1510                                      RAW_PAYLOAD_NCI, NFC_DIRECTION_RX);
1511
1512                 /* Process frame */
1513                 switch (nci_mt(skb->data)) {
1514                 case NCI_MT_RSP_PKT:
1515                         nci_rsp_packet(ndev, skb);
1516                         break;
1517
1518                 case NCI_MT_NTF_PKT:
1519                         nci_ntf_packet(ndev, skb);
1520                         break;
1521
1522                 case NCI_MT_DATA_PKT:
1523                         nci_rx_data_packet(ndev, skb);
1524                         break;
1525
1526                 default:
1527                         pr_err("unknown MT 0x%x\n", nci_mt(skb->data));
1528                         kfree_skb(skb);
1529                         break;
1530                 }
1531         }
1532
1533         /* check if a data exchange timout has occurred */
1534         if (test_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags)) {
1535                 /* complete the data exchange transaction, if exists */
1536                 if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags))
1537                         nci_data_exchange_complete(ndev, NULL,
1538                                                    ndev->cur_conn_id,
1539                                                    -ETIMEDOUT);
1540
1541                 clear_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags);
1542         }
1543 }
1544
1545 /* ----- NCI TX CMD worker thread ----- */
1546
1547 static void nci_cmd_work(struct work_struct *work)
1548 {
1549         struct nci_dev *ndev = container_of(work, struct nci_dev, cmd_work);
1550         struct sk_buff *skb;
1551
1552         pr_debug("cmd_cnt %d\n", atomic_read(&ndev->cmd_cnt));
1553
1554         /* Send queued command */
1555         if (atomic_read(&ndev->cmd_cnt)) {
1556                 skb = skb_dequeue(&ndev->cmd_q);
1557                 if (!skb)
1558                         return;
1559
1560                 atomic_dec(&ndev->cmd_cnt);
1561
1562                 pr_debug("NCI TX: MT=cmd, PBF=%d, GID=0x%x, OID=0x%x, plen=%d\n",
1563                          nci_pbf(skb->data),
1564                          nci_opcode_gid(nci_opcode(skb->data)),
1565                          nci_opcode_oid(nci_opcode(skb->data)),
1566                          nci_plen(skb->data));
1567
1568                 nci_send_frame(ndev, skb);
1569
1570                 mod_timer(&ndev->cmd_timer,
1571                           jiffies + msecs_to_jiffies(NCI_CMD_TIMEOUT));
1572         }
1573 }
1574
1575 MODULE_LICENSE("GPL");