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