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