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