blob: bcb70a6b17cdddcf9d818f25ab26d4ff9db644d4 [file] [log] [blame]
Ilan Elias6a2968a2011-09-18 11:19:35 +03001/*
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.
Julien Lefrique772dccf2014-10-21 16:52:44 +02006 * Copyright (C) 2014 Marvell International Ltd.
Ilan Elias6a2968a2011-09-18 11:19:35 +03007 *
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
Jeff Kirsher98b32de2013-12-06 08:56:16 -080024 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Ilan Elias6a2968a2011-09-18 11:19:35 +030025 *
26 */
27
Samuel Ortiz52858b52011-12-14 16:43:05 +010028#define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
Joe Perchesed1e0ad2011-11-29 11:37:32 -080029
Dave Jones8a70e7f2012-07-12 19:17:34 +020030#include <linux/module.h>
Ilan Elias6a2968a2011-09-18 11:19:35 +030031#include <linux/types.h>
32#include <linux/workqueue.h>
33#include <linux/completion.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040034#include <linux/export.h>
Ilan Elias6a2968a2011-09-18 11:19:35 +030035#include <linux/sched.h>
36#include <linux/bitops.h>
37#include <linux/skbuff.h>
38
39#include "../nfc.h"
40#include <net/nfc/nci.h>
41#include <net/nfc/nci_core.h>
42#include <linux/nfc.h>
43
44static void nci_cmd_work(struct work_struct *work);
45static void nci_rx_work(struct work_struct *work);
46static void nci_tx_work(struct work_struct *work);
47
48/* ---- NCI requests ---- */
49
50void nci_req_complete(struct nci_dev *ndev, int result)
51{
52 if (ndev->req_status == NCI_REQ_PEND) {
53 ndev->req_result = result;
54 ndev->req_status = NCI_REQ_DONE;
55 complete(&ndev->req_completion);
56 }
57}
58
59static void nci_req_cancel(struct nci_dev *ndev, int err)
60{
61 if (ndev->req_status == NCI_REQ_PEND) {
62 ndev->req_result = err;
63 ndev->req_status = NCI_REQ_CANCELED;
64 complete(&ndev->req_completion);
65 }
66}
67
68/* Execute request and wait for completion. */
69static int __nci_request(struct nci_dev *ndev,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +010070 void (*req)(struct nci_dev *ndev, unsigned long opt),
71 unsigned long opt, __u32 timeout)
Ilan Elias6a2968a2011-09-18 11:19:35 +030072{
73 int rc = 0;
Dan Carpenterf8c141c2011-12-09 09:35:39 +030074 long completion_rc;
Ilan Elias6a2968a2011-09-18 11:19:35 +030075
76 ndev->req_status = NCI_REQ_PEND;
77
Axel Lin9bec44b2014-02-13 13:25:48 +080078 reinit_completion(&ndev->req_completion);
Ilan Elias6a2968a2011-09-18 11:19:35 +030079 req(ndev, opt);
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +010080 completion_rc =
81 wait_for_completion_interruptible_timeout(&ndev->req_completion,
82 timeout);
Ilan Elias6a2968a2011-09-18 11:19:35 +030083
Joe Perches20c239c2011-11-29 11:37:33 -080084 pr_debug("wait_for_completion return %ld\n", completion_rc);
Ilan Elias6a2968a2011-09-18 11:19:35 +030085
86 if (completion_rc > 0) {
87 switch (ndev->req_status) {
88 case NCI_REQ_DONE:
89 rc = nci_to_errno(ndev->req_result);
90 break;
91
92 case NCI_REQ_CANCELED:
93 rc = -ndev->req_result;
94 break;
95
96 default:
97 rc = -ETIMEDOUT;
98 break;
99 }
100 } else {
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800101 pr_err("wait_for_completion_interruptible_timeout failed %ld\n",
102 completion_rc);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300103
104 rc = ((completion_rc == 0) ? (-ETIMEDOUT) : (completion_rc));
105 }
106
107 ndev->req_status = ndev->req_result = 0;
108
109 return rc;
110}
111
112static inline int nci_request(struct nci_dev *ndev,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100113 void (*req)(struct nci_dev *ndev,
114 unsigned long opt),
115 unsigned long opt, __u32 timeout)
Ilan Elias6a2968a2011-09-18 11:19:35 +0300116{
117 int rc;
118
119 if (!test_bit(NCI_UP, &ndev->flags))
120 return -ENETDOWN;
121
122 /* Serialize all requests */
123 mutex_lock(&ndev->req_lock);
124 rc = __nci_request(ndev, req, opt, timeout);
125 mutex_unlock(&ndev->req_lock);
126
127 return rc;
128}
129
130static void nci_reset_req(struct nci_dev *ndev, unsigned long opt)
131{
Ilan Eliase8c0dac2011-11-09 12:09:14 +0200132 struct nci_core_reset_cmd cmd;
133
134 cmd.reset_type = NCI_RESET_TYPE_RESET_CONFIG;
135 nci_send_cmd(ndev, NCI_OP_CORE_RESET_CMD, 1, &cmd);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300136}
137
138static void nci_init_req(struct nci_dev *ndev, unsigned long opt)
139{
140 nci_send_cmd(ndev, NCI_OP_CORE_INIT_CMD, 0, NULL);
141}
142
143static void nci_init_complete_req(struct nci_dev *ndev, unsigned long opt)
144{
Ilan Elias2eb1dc12011-09-22 10:47:52 +0300145 struct nci_rf_disc_map_cmd cmd;
146 struct disc_map_config *cfg = cmd.mapping_configs;
147 __u8 *num = &cmd.num_mapping_configs;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300148 int i;
149
Ilan Elias6a2968a2011-09-18 11:19:35 +0300150 /* set rf mapping configurations */
Ilan Elias2eb1dc12011-09-22 10:47:52 +0300151 *num = 0;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300152
153 /* by default mapping is set to NCI_RF_INTERFACE_FRAME */
154 for (i = 0; i < ndev->num_supported_rf_interfaces; i++) {
155 if (ndev->supported_rf_interfaces[i] ==
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100156 NCI_RF_INTERFACE_ISO_DEP) {
Ilan Elias2eb1dc12011-09-22 10:47:52 +0300157 cfg[*num].rf_protocol = NCI_RF_PROTOCOL_ISO_DEP;
Ilan Elias637d85a2011-12-20 16:57:40 +0200158 cfg[*num].mode = NCI_DISC_MAP_MODE_POLL |
159 NCI_DISC_MAP_MODE_LISTEN;
160 cfg[*num].rf_interface = NCI_RF_INTERFACE_ISO_DEP;
Ilan Elias2eb1dc12011-09-22 10:47:52 +0300161 (*num)++;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300162 } else if (ndev->supported_rf_interfaces[i] ==
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100163 NCI_RF_INTERFACE_NFC_DEP) {
Ilan Elias2eb1dc12011-09-22 10:47:52 +0300164 cfg[*num].rf_protocol = NCI_RF_PROTOCOL_NFC_DEP;
Ilan Elias637d85a2011-12-20 16:57:40 +0200165 cfg[*num].mode = NCI_DISC_MAP_MODE_POLL |
166 NCI_DISC_MAP_MODE_LISTEN;
167 cfg[*num].rf_interface = NCI_RF_INTERFACE_NFC_DEP;
Ilan Elias2eb1dc12011-09-22 10:47:52 +0300168 (*num)++;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300169 }
170
Ilan Elias2eb1dc12011-09-22 10:47:52 +0300171 if (*num == NCI_MAX_NUM_MAPPING_CONFIGS)
Ilan Elias6a2968a2011-09-18 11:19:35 +0300172 break;
173 }
174
175 nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_MAP_CMD,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100176 (1 + ((*num) * sizeof(struct disc_map_config))), &cmd);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300177}
178
Ilan Elias7e035232012-08-15 11:46:22 +0300179struct nci_set_config_param {
180 __u8 id;
181 size_t len;
182 __u8 *val;
183};
184
185static void nci_set_config_req(struct nci_dev *ndev, unsigned long opt)
186{
187 struct nci_set_config_param *param = (struct nci_set_config_param *)opt;
188 struct nci_core_set_config_cmd cmd;
189
190 BUG_ON(param->len > NCI_MAX_PARAM_LEN);
191
192 cmd.num_params = 1;
193 cmd.param.id = param->id;
194 cmd.param.len = param->len;
195 memcpy(cmd.param.val, param->val, param->len);
196
197 nci_send_cmd(ndev, NCI_OP_CORE_SET_CONFIG_CMD, (3 + param->len), &cmd);
198}
199
Julien Lefrique772dccf2014-10-21 16:52:44 +0200200struct nci_rf_discover_param {
201 __u32 im_protocols;
202 __u32 tm_protocols;
203};
204
Ilan Elias6a2968a2011-09-18 11:19:35 +0300205static void nci_rf_discover_req(struct nci_dev *ndev, unsigned long opt)
206{
Julien Lefrique772dccf2014-10-21 16:52:44 +0200207 struct nci_rf_discover_param *param =
208 (struct nci_rf_discover_param *)opt;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300209 struct nci_rf_disc_cmd cmd;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300210
211 cmd.num_disc_configs = 0;
212
213 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
Julien Lefrique772dccf2014-10-21 16:52:44 +0200214 (param->im_protocols & NFC_PROTO_JEWEL_MASK ||
215 param->im_protocols & NFC_PROTO_MIFARE_MASK ||
216 param->im_protocols & NFC_PROTO_ISO14443_MASK ||
217 param->im_protocols & NFC_PROTO_NFC_DEP_MASK)) {
Ilan Elias637d85a2011-12-20 16:57:40 +0200218 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100219 NCI_NFC_A_PASSIVE_POLL_MODE;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300220 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
221 cmd.num_disc_configs++;
222 }
223
224 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
Julien Lefrique772dccf2014-10-21 16:52:44 +0200225 (param->im_protocols & NFC_PROTO_ISO14443_B_MASK)) {
Ilan Elias637d85a2011-12-20 16:57:40 +0200226 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100227 NCI_NFC_B_PASSIVE_POLL_MODE;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300228 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
229 cmd.num_disc_configs++;
230 }
231
232 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
Julien Lefrique772dccf2014-10-21 16:52:44 +0200233 (param->im_protocols & NFC_PROTO_FELICA_MASK ||
234 param->im_protocols & NFC_PROTO_NFC_DEP_MASK)) {
Ilan Elias637d85a2011-12-20 16:57:40 +0200235 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100236 NCI_NFC_F_PASSIVE_POLL_MODE;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300237 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
238 cmd.num_disc_configs++;
239 }
240
Vincent Cuissardcfdbeea2014-07-22 19:48:38 +0200241 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
Julien Lefrique772dccf2014-10-21 16:52:44 +0200242 (param->im_protocols & NFC_PROTO_ISO15693_MASK)) {
Vincent Cuissardcfdbeea2014-07-22 19:48:38 +0200243 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
244 NCI_NFC_V_PASSIVE_POLL_MODE;
245 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
246 cmd.num_disc_configs++;
247 }
248
Julien Lefrique772dccf2014-10-21 16:52:44 +0200249 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS - 1) &&
250 (param->tm_protocols & NFC_PROTO_NFC_DEP_MASK)) {
251 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
252 NCI_NFC_A_PASSIVE_LISTEN_MODE;
253 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
254 cmd.num_disc_configs++;
255 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
256 NCI_NFC_F_PASSIVE_LISTEN_MODE;
257 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
258 cmd.num_disc_configs++;
259 }
260
Ilan Elias6a2968a2011-09-18 11:19:35 +0300261 nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_CMD,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100262 (1 + (cmd.num_disc_configs * sizeof(struct disc_config))),
263 &cmd);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300264}
265
Ilan Elias019c4fb2012-01-18 13:16:14 +0200266struct nci_rf_discover_select_param {
267 __u8 rf_discovery_id;
268 __u8 rf_protocol;
269};
270
271static void nci_rf_discover_select_req(struct nci_dev *ndev, unsigned long opt)
272{
273 struct nci_rf_discover_select_param *param =
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100274 (struct nci_rf_discover_select_param *)opt;
Ilan Elias019c4fb2012-01-18 13:16:14 +0200275 struct nci_rf_discover_select_cmd cmd;
276
277 cmd.rf_discovery_id = param->rf_discovery_id;
278 cmd.rf_protocol = param->rf_protocol;
279
280 switch (cmd.rf_protocol) {
281 case NCI_RF_PROTOCOL_ISO_DEP:
282 cmd.rf_interface = NCI_RF_INTERFACE_ISO_DEP;
283 break;
284
285 case NCI_RF_PROTOCOL_NFC_DEP:
286 cmd.rf_interface = NCI_RF_INTERFACE_NFC_DEP;
287 break;
288
289 default:
290 cmd.rf_interface = NCI_RF_INTERFACE_FRAME;
291 break;
292 }
293
294 nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_SELECT_CMD,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100295 sizeof(struct nci_rf_discover_select_cmd), &cmd);
Ilan Elias019c4fb2012-01-18 13:16:14 +0200296}
297
Ilan Elias6a2968a2011-09-18 11:19:35 +0300298static void nci_rf_deactivate_req(struct nci_dev *ndev, unsigned long opt)
299{
300 struct nci_rf_deactivate_cmd cmd;
301
302 cmd.type = NCI_DEACTIVATE_TYPE_IDLE_MODE;
303
304 nci_send_cmd(ndev, NCI_OP_RF_DEACTIVATE_CMD,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100305 sizeof(struct nci_rf_deactivate_cmd), &cmd);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300306}
307
308static int nci_open_device(struct nci_dev *ndev)
309{
310 int rc = 0;
311
312 mutex_lock(&ndev->req_lock);
313
314 if (test_bit(NCI_UP, &ndev->flags)) {
315 rc = -EALREADY;
316 goto done;
317 }
318
319 if (ndev->ops->open(ndev)) {
320 rc = -EIO;
321 goto done;
322 }
323
324 atomic_set(&ndev->cmd_cnt, 1);
325
326 set_bit(NCI_INIT, &ndev->flags);
327
328 rc = __nci_request(ndev, nci_reset_req, 0,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100329 msecs_to_jiffies(NCI_RESET_TIMEOUT));
Ilan Elias6a2968a2011-09-18 11:19:35 +0300330
Amitkumar Karwar44a589c2014-02-06 11:28:31 -0800331 if (ndev->ops->setup)
Amitkumar Karwar86e85862014-01-06 12:58:17 -0800332 ndev->ops->setup(ndev);
333
Ilan Elias6a2968a2011-09-18 11:19:35 +0300334 if (!rc) {
335 rc = __nci_request(ndev, nci_init_req, 0,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100336 msecs_to_jiffies(NCI_INIT_TIMEOUT));
Ilan Elias6a2968a2011-09-18 11:19:35 +0300337 }
338
339 if (!rc) {
340 rc = __nci_request(ndev, nci_init_complete_req, 0,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100341 msecs_to_jiffies(NCI_INIT_TIMEOUT));
Ilan Elias6a2968a2011-09-18 11:19:35 +0300342 }
343
344 clear_bit(NCI_INIT, &ndev->flags);
345
346 if (!rc) {
347 set_bit(NCI_UP, &ndev->flags);
Ilan Elias019c4fb2012-01-18 13:16:14 +0200348 nci_clear_target_list(ndev);
Ilan Elias8939e472012-01-18 13:16:12 +0200349 atomic_set(&ndev->state, NCI_IDLE);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300350 } else {
351 /* Init failed, cleanup */
352 skb_queue_purge(&ndev->cmd_q);
353 skb_queue_purge(&ndev->rx_q);
354 skb_queue_purge(&ndev->tx_q);
355
356 ndev->ops->close(ndev);
357 ndev->flags = 0;
358 }
359
360done:
361 mutex_unlock(&ndev->req_lock);
362 return rc;
363}
364
365static int nci_close_device(struct nci_dev *ndev)
366{
367 nci_req_cancel(ndev, ENODEV);
368 mutex_lock(&ndev->req_lock);
369
370 if (!test_and_clear_bit(NCI_UP, &ndev->flags)) {
371 del_timer_sync(&ndev->cmd_timer);
Ilan Eliasc4bf98b2012-01-17 12:03:50 +0200372 del_timer_sync(&ndev->data_timer);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300373 mutex_unlock(&ndev->req_lock);
374 return 0;
375 }
376
377 /* Drop RX and TX queues */
378 skb_queue_purge(&ndev->rx_q);
379 skb_queue_purge(&ndev->tx_q);
380
381 /* Flush RX and TX wq */
382 flush_workqueue(ndev->rx_wq);
383 flush_workqueue(ndev->tx_wq);
384
385 /* Reset device */
386 skb_queue_purge(&ndev->cmd_q);
387 atomic_set(&ndev->cmd_cnt, 1);
388
389 set_bit(NCI_INIT, &ndev->flags);
390 __nci_request(ndev, nci_reset_req, 0,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100391 msecs_to_jiffies(NCI_RESET_TIMEOUT));
Ilan Elias6a2968a2011-09-18 11:19:35 +0300392 clear_bit(NCI_INIT, &ndev->flags);
393
Amitkumar Karwarfa9be5f2013-12-23 14:15:13 -0800394 del_timer_sync(&ndev->cmd_timer);
395
Ilan Elias6a2968a2011-09-18 11:19:35 +0300396 /* Flush cmd wq */
397 flush_workqueue(ndev->cmd_wq);
398
399 /* After this point our queues are empty
400 * and no works are scheduled. */
401 ndev->ops->close(ndev);
402
403 /* Clear flags */
404 ndev->flags = 0;
405
406 mutex_unlock(&ndev->req_lock);
407
408 return 0;
409}
410
411/* NCI command timer function */
412static void nci_cmd_timer(unsigned long arg)
413{
414 struct nci_dev *ndev = (void *) arg;
415
Ilan Elias6a2968a2011-09-18 11:19:35 +0300416 atomic_set(&ndev->cmd_cnt, 1);
417 queue_work(ndev->cmd_wq, &ndev->cmd_work);
418}
419
Ilan Eliasc4bf98b2012-01-17 12:03:50 +0200420/* NCI data exchange timer function */
421static void nci_data_timer(unsigned long arg)
422{
423 struct nci_dev *ndev = (void *) arg;
424
425 set_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags);
426 queue_work(ndev->rx_wq, &ndev->rx_work);
427}
428
Ilan Elias6a2968a2011-09-18 11:19:35 +0300429static int nci_dev_up(struct nfc_dev *nfc_dev)
430{
431 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
432
Ilan Elias6a2968a2011-09-18 11:19:35 +0300433 return nci_open_device(ndev);
434}
435
436static int nci_dev_down(struct nfc_dev *nfc_dev)
437{
438 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
439
Ilan Elias6a2968a2011-09-18 11:19:35 +0300440 return nci_close_device(ndev);
441}
442
Amitkumar Karwar22c15bf2014-01-06 12:58:18 -0800443int nci_set_config(struct nci_dev *ndev, __u8 id, size_t len, __u8 *val)
444{
445 struct nci_set_config_param param;
446
447 if (!val || !len)
448 return 0;
449
450 param.id = id;
451 param.len = len;
452 param.val = val;
453
454 return __nci_request(ndev, nci_set_config_req, (unsigned long)&param,
455 msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT));
456}
457EXPORT_SYMBOL(nci_set_config);
458
Ilan Elias7e035232012-08-15 11:46:22 +0300459static int nci_set_local_general_bytes(struct nfc_dev *nfc_dev)
460{
461 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
462 struct nci_set_config_param param;
Julien Lefrique529ee062014-10-21 16:52:47 +0200463 int rc;
Ilan Elias7e035232012-08-15 11:46:22 +0300464
465 param.val = nfc_get_local_general_bytes(nfc_dev, &param.len);
466 if ((param.val == NULL) || (param.len == 0))
Szymon Jancf9fc36f2012-10-04 15:15:46 +0200467 return 0;
Ilan Elias7e035232012-08-15 11:46:22 +0300468
Szymon Janc460d8f92012-10-04 15:15:45 +0200469 if (param.len > NFC_MAX_GT_LEN)
Ilan Elias7e035232012-08-15 11:46:22 +0300470 return -EINVAL;
471
Ilan Elias7e035232012-08-15 11:46:22 +0300472 param.id = NCI_PN_ATR_REQ_GEN_BYTES;
Ilan Elias7e035232012-08-15 11:46:22 +0300473
Julien Lefrique529ee062014-10-21 16:52:47 +0200474 rc = nci_request(ndev, nci_set_config_req, (unsigned long)&param,
475 msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT));
476 if (rc)
477 return rc;
478
479 param.id = NCI_LN_ATR_RES_GEN_BYTES;
480
Szymon Jancf9fc36f2012-10-04 15:15:46 +0200481 return nci_request(ndev, nci_set_config_req, (unsigned long)&param,
482 msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT));
Ilan Elias7e035232012-08-15 11:46:22 +0300483}
484
Julien Lefrique90d78c12014-10-21 16:52:45 +0200485static int nci_set_listen_parameters(struct nfc_dev *nfc_dev)
486{
487 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
488 int rc;
489 __u8 val;
490
491 val = NCI_LA_SEL_INFO_NFC_DEP_MASK;
492
493 rc = nci_set_config(ndev, NCI_LA_SEL_INFO, 1, &val);
494 if (rc)
495 return rc;
496
497 val = NCI_LF_PROTOCOL_TYPE_NFC_DEP_MASK;
498
499 rc = nci_set_config(ndev, NCI_LF_PROTOCOL_TYPE, 1, &val);
500 if (rc)
501 return rc;
502
503 val = NCI_LF_CON_BITR_F_212 | NCI_LF_CON_BITR_F_424;
504
505 return nci_set_config(ndev, NCI_LF_CON_BITR_F, 1, &val);
506}
507
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200508static int nci_start_poll(struct nfc_dev *nfc_dev,
509 __u32 im_protocols, __u32 tm_protocols)
Ilan Elias6a2968a2011-09-18 11:19:35 +0300510{
511 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
Julien Lefrique772dccf2014-10-21 16:52:44 +0200512 struct nci_rf_discover_param param;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300513 int rc;
514
Ilan Elias019c4fb2012-01-18 13:16:14 +0200515 if ((atomic_read(&ndev->state) == NCI_DISCOVERY) ||
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100516 (atomic_read(&ndev->state) == NCI_W4_ALL_DISCOVERIES)) {
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800517 pr_err("unable to start poll, since poll is already active\n");
Ilan Elias6a2968a2011-09-18 11:19:35 +0300518 return -EBUSY;
519 }
520
Ilan Eliasde054792011-09-22 11:13:01 +0300521 if (ndev->target_active_prot) {
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800522 pr_err("there is an active target\n");
Ilan Eliasde054792011-09-22 11:13:01 +0300523 return -EBUSY;
524 }
525
Ilan Elias019c4fb2012-01-18 13:16:14 +0200526 if ((atomic_read(&ndev->state) == NCI_W4_HOST_SELECT) ||
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100527 (atomic_read(&ndev->state) == NCI_POLL_ACTIVE)) {
Ilan Elias019c4fb2012-01-18 13:16:14 +0200528 pr_debug("target active or w4 select, implicitly deactivate\n");
Ilan Elias6a2968a2011-09-18 11:19:35 +0300529
530 rc = nci_request(ndev, nci_rf_deactivate_req, 0,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100531 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
Ilan Elias6a2968a2011-09-18 11:19:35 +0300532 if (rc)
533 return -EBUSY;
534 }
535
Julien Lefrique529ee062014-10-21 16:52:47 +0200536 if ((im_protocols | tm_protocols) & NFC_PROTO_NFC_DEP_MASK) {
Ilan Elias7e035232012-08-15 11:46:22 +0300537 rc = nci_set_local_general_bytes(nfc_dev);
538 if (rc) {
539 pr_err("failed to set local general bytes\n");
540 return rc;
541 }
542 }
543
Julien Lefrique90d78c12014-10-21 16:52:45 +0200544 if (tm_protocols & NFC_PROTO_NFC_DEP_MASK) {
545 rc = nci_set_listen_parameters(nfc_dev);
546 if (rc)
547 pr_err("failed to set listen parameters\n");
548 }
549
Julien Lefrique772dccf2014-10-21 16:52:44 +0200550 param.im_protocols = im_protocols;
551 param.tm_protocols = tm_protocols;
552 rc = nci_request(ndev, nci_rf_discover_req, (unsigned long)&param,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100553 msecs_to_jiffies(NCI_RF_DISC_TIMEOUT));
Ilan Elias6a2968a2011-09-18 11:19:35 +0300554
555 if (!rc)
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200556 ndev->poll_prots = im_protocols;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300557
558 return rc;
559}
560
561static void nci_stop_poll(struct nfc_dev *nfc_dev)
562{
563 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
564
Ilan Elias019c4fb2012-01-18 13:16:14 +0200565 if ((atomic_read(&ndev->state) != NCI_DISCOVERY) &&
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100566 (atomic_read(&ndev->state) != NCI_W4_ALL_DISCOVERIES)) {
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800567 pr_err("unable to stop poll, since poll is not active\n");
Ilan Elias6a2968a2011-09-18 11:19:35 +0300568 return;
569 }
570
571 nci_request(ndev, nci_rf_deactivate_req, 0,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100572 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
Ilan Elias6a2968a2011-09-18 11:19:35 +0300573}
574
Eric Lapuyade90099432012-05-07 12:31:13 +0200575static int nci_activate_target(struct nfc_dev *nfc_dev,
576 struct nfc_target *target, __u32 protocol)
Ilan Elias6a2968a2011-09-18 11:19:35 +0300577{
578 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
Ilan Elias019c4fb2012-01-18 13:16:14 +0200579 struct nci_rf_discover_select_param param;
Eric Lapuyade90099432012-05-07 12:31:13 +0200580 struct nfc_target *nci_target = NULL;
Ilan Elias019c4fb2012-01-18 13:16:14 +0200581 int i;
582 int rc = 0;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300583
Eric Lapuyade90099432012-05-07 12:31:13 +0200584 pr_debug("target_idx %d, protocol 0x%x\n", target->idx, protocol);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300585
Ilan Elias019c4fb2012-01-18 13:16:14 +0200586 if ((atomic_read(&ndev->state) != NCI_W4_HOST_SELECT) &&
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100587 (atomic_read(&ndev->state) != NCI_POLL_ACTIVE)) {
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800588 pr_err("there is no available target to activate\n");
Ilan Elias6a2968a2011-09-18 11:19:35 +0300589 return -EINVAL;
590 }
591
592 if (ndev->target_active_prot) {
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800593 pr_err("there is already an active target\n");
Ilan Elias6a2968a2011-09-18 11:19:35 +0300594 return -EBUSY;
595 }
596
Ilan Elias019c4fb2012-01-18 13:16:14 +0200597 for (i = 0; i < ndev->n_targets; i++) {
Eric Lapuyade90099432012-05-07 12:31:13 +0200598 if (ndev->targets[i].idx == target->idx) {
599 nci_target = &ndev->targets[i];
Ilan Elias019c4fb2012-01-18 13:16:14 +0200600 break;
601 }
602 }
603
Eric Lapuyade90099432012-05-07 12:31:13 +0200604 if (!nci_target) {
Ilan Elias019c4fb2012-01-18 13:16:14 +0200605 pr_err("unable to find the selected target\n");
606 return -EINVAL;
607 }
608
Eric Lapuyade90099432012-05-07 12:31:13 +0200609 if (!(nci_target->supported_protocols & (1 << protocol))) {
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800610 pr_err("target does not support the requested protocol 0x%x\n",
611 protocol);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300612 return -EINVAL;
613 }
614
Ilan Elias019c4fb2012-01-18 13:16:14 +0200615 if (atomic_read(&ndev->state) == NCI_W4_HOST_SELECT) {
Eric Lapuyade90099432012-05-07 12:31:13 +0200616 param.rf_discovery_id = nci_target->logical_idx;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300617
Ilan Elias019c4fb2012-01-18 13:16:14 +0200618 if (protocol == NFC_PROTO_JEWEL)
619 param.rf_protocol = NCI_RF_PROTOCOL_T1T;
620 else if (protocol == NFC_PROTO_MIFARE)
621 param.rf_protocol = NCI_RF_PROTOCOL_T2T;
622 else if (protocol == NFC_PROTO_FELICA)
623 param.rf_protocol = NCI_RF_PROTOCOL_T3T;
Samuel Ortiz01d719a2012-07-04 00:14:04 +0200624 else if (protocol == NFC_PROTO_ISO14443 ||
625 protocol == NFC_PROTO_ISO14443_B)
Ilan Elias019c4fb2012-01-18 13:16:14 +0200626 param.rf_protocol = NCI_RF_PROTOCOL_ISO_DEP;
627 else
628 param.rf_protocol = NCI_RF_PROTOCOL_NFC_DEP;
629
630 rc = nci_request(ndev, nci_rf_discover_select_req,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100631 (unsigned long)&param,
632 msecs_to_jiffies(NCI_RF_DISC_SELECT_TIMEOUT));
Ilan Elias019c4fb2012-01-18 13:16:14 +0200633 }
634
635 if (!rc)
636 ndev->target_active_prot = protocol;
637
638 return rc;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300639}
640
Eric Lapuyade90099432012-05-07 12:31:13 +0200641static void nci_deactivate_target(struct nfc_dev *nfc_dev,
642 struct nfc_target *target)
Ilan Elias6a2968a2011-09-18 11:19:35 +0300643{
644 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
645
Ilan Elias767f19a2012-08-15 11:46:24 +0300646 pr_debug("entry\n");
Ilan Elias6a2968a2011-09-18 11:19:35 +0300647
648 if (!ndev->target_active_prot) {
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800649 pr_err("unable to deactivate target, no active target\n");
Ilan Elias6a2968a2011-09-18 11:19:35 +0300650 return;
651 }
652
653 ndev->target_active_prot = 0;
654
Ilan Elias8939e472012-01-18 13:16:12 +0200655 if (atomic_read(&ndev->state) == NCI_POLL_ACTIVE) {
Ilan Elias6a2968a2011-09-18 11:19:35 +0300656 nci_request(ndev, nci_rf_deactivate_req, 0,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100657 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
Ilan Elias6a2968a2011-09-18 11:19:35 +0300658 }
659}
660
Ilan Elias767f19a2012-08-15 11:46:24 +0300661static int nci_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
662 __u8 comm_mode, __u8 *gb, size_t gb_len)
663{
664 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
665 int rc;
666
667 pr_debug("target_idx %d, comm_mode %d\n", target->idx, comm_mode);
668
669 rc = nci_activate_target(nfc_dev, target, NFC_PROTO_NFC_DEP);
670 if (rc)
671 return rc;
672
673 rc = nfc_set_remote_general_bytes(nfc_dev, ndev->remote_gb,
674 ndev->remote_gb_len);
675 if (!rc)
676 rc = nfc_dep_link_is_up(nfc_dev, target->idx, NFC_COMM_PASSIVE,
677 NFC_RF_INITIATOR);
678
679 return rc;
680}
681
682static int nci_dep_link_down(struct nfc_dev *nfc_dev)
683{
Julien Lefriqued7979e12014-10-21 16:52:53 +0200684 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
685 int rc;
686
Ilan Elias767f19a2012-08-15 11:46:24 +0300687 pr_debug("entry\n");
688
Julien Lefriqued7979e12014-10-21 16:52:53 +0200689 if (nfc_dev->rf_mode == NFC_RF_INITIATOR) {
690 nci_deactivate_target(nfc_dev, NULL);
691 } else {
692 if (atomic_read(&ndev->state) == NCI_LISTEN_ACTIVE ||
693 atomic_read(&ndev->state) == NCI_DISCOVERY) {
694 nci_request(ndev, nci_rf_deactivate_req, 0,
695 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
696 }
697
698 rc = nfc_tm_deactivated(nfc_dev);
699 if (rc)
700 pr_err("error when signaling tm deactivation\n");
701 }
Ilan Elias767f19a2012-08-15 11:46:24 +0300702
703 return 0;
704}
705
706
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +0200707static int nci_transceive(struct nfc_dev *nfc_dev, struct nfc_target *target,
708 struct sk_buff *skb,
709 data_exchange_cb_t cb, void *cb_context)
Ilan Elias6a2968a2011-09-18 11:19:35 +0300710{
711 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
Ilan Elias38f04c62011-09-22 11:36:19 +0300712 int rc;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300713
Eric Lapuyade90099432012-05-07 12:31:13 +0200714 pr_debug("target_idx %d, len %d\n", target->idx, skb->len);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300715
716 if (!ndev->target_active_prot) {
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800717 pr_err("unable to exchange data, no active target\n");
Ilan Elias6a2968a2011-09-18 11:19:35 +0300718 return -EINVAL;
719 }
720
Ilan Elias38f04c62011-09-22 11:36:19 +0300721 if (test_and_set_bit(NCI_DATA_EXCHANGE, &ndev->flags))
722 return -EBUSY;
723
Ilan Elias6a2968a2011-09-18 11:19:35 +0300724 /* store cb and context to be used on receiving data */
725 ndev->data_exchange_cb = cb;
726 ndev->data_exchange_cb_context = cb_context;
727
Ilan Eliase8c0dac2011-11-09 12:09:14 +0200728 rc = nci_send_data(ndev, NCI_STATIC_RF_CONN_ID, skb);
Ilan Elias38f04c62011-09-22 11:36:19 +0300729 if (rc)
730 clear_bit(NCI_DATA_EXCHANGE, &ndev->flags);
731
732 return rc;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300733}
734
Julien Lefrique485f4422014-10-21 16:52:48 +0200735static int nci_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
736{
737 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
738 int rc;
739
740 rc = nci_send_data(ndev, NCI_STATIC_RF_CONN_ID, skb);
741 if (rc)
742 pr_err("unable to send data\n");
743
744 return rc;
745}
746
Samuel Ortiz0a946302013-05-10 11:57:06 +0200747static int nci_enable_se(struct nfc_dev *nfc_dev, u32 se_idx)
748{
Christophe Ricard93bca2b2014-11-13 00:30:36 +0100749 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
750
751 if (ndev->ops->enable_se)
752 return ndev->ops->enable_se(ndev, se_idx);
753
Samuel Ortiz0a946302013-05-10 11:57:06 +0200754 return 0;
755}
756
757static int nci_disable_se(struct nfc_dev *nfc_dev, u32 se_idx)
758{
Christophe Ricarde9ef9432014-11-13 00:30:37 +0100759 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
760
761 if (ndev->ops->disable_se)
762 return ndev->ops->disable_se(ndev, se_idx);
763
Samuel Ortiz0a946302013-05-10 11:57:06 +0200764 return 0;
765}
766
767static int nci_discover_se(struct nfc_dev *nfc_dev)
768{
Christophe Ricardba4db552014-11-13 00:30:35 +0100769 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
770
771 if (ndev->ops->discover_se)
772 return ndev->ops->discover_se(ndev);
773
Samuel Ortiz0a946302013-05-10 11:57:06 +0200774 return 0;
775}
776
Ilan Elias6a2968a2011-09-18 11:19:35 +0300777static struct nfc_ops nci_nfc_ops = {
778 .dev_up = nci_dev_up,
779 .dev_down = nci_dev_down,
780 .start_poll = nci_start_poll,
781 .stop_poll = nci_stop_poll,
Ilan Elias767f19a2012-08-15 11:46:24 +0300782 .dep_link_up = nci_dep_link_up,
783 .dep_link_down = nci_dep_link_down,
Ilan Elias6a2968a2011-09-18 11:19:35 +0300784 .activate_target = nci_activate_target,
785 .deactivate_target = nci_deactivate_target,
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +0200786 .im_transceive = nci_transceive,
Julien Lefrique485f4422014-10-21 16:52:48 +0200787 .tm_send = nci_tm_send,
Samuel Ortiz0a946302013-05-10 11:57:06 +0200788 .enable_se = nci_enable_se,
789 .disable_se = nci_disable_se,
790 .discover_se = nci_discover_se,
Ilan Elias6a2968a2011-09-18 11:19:35 +0300791};
792
793/* ---- Interface to NCI drivers ---- */
794
795/**
796 * nci_allocate_device - allocate a new nci device
797 *
798 * @ops: device operations
799 * @supported_protocols: NFC protocols supported by the device
800 */
801struct nci_dev *nci_allocate_device(struct nci_ops *ops,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100802 __u32 supported_protocols,
803 int tx_headroom, int tx_tailroom)
Ilan Elias6a2968a2011-09-18 11:19:35 +0300804{
Dan Carpenter8ebafde2011-09-23 09:14:35 +0300805 struct nci_dev *ndev;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300806
Joe Perches24bf3302011-11-29 11:37:35 -0800807 pr_debug("supported_protocols 0x%x\n", supported_protocols);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300808
809 if (!ops->open || !ops->close || !ops->send)
Dan Carpenter8ebafde2011-09-23 09:14:35 +0300810 return NULL;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300811
812 if (!supported_protocols)
Dan Carpenter8ebafde2011-09-23 09:14:35 +0300813 return NULL;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300814
815 ndev = kzalloc(sizeof(struct nci_dev), GFP_KERNEL);
816 if (!ndev)
Dan Carpenter8ebafde2011-09-23 09:14:35 +0300817 return NULL;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300818
819 ndev->ops = ops;
820 ndev->tx_headroom = tx_headroom;
821 ndev->tx_tailroom = tx_tailroom;
Axel Lin9bec44b2014-02-13 13:25:48 +0800822 init_completion(&ndev->req_completion);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300823
824 ndev->nfc_dev = nfc_allocate_device(&nci_nfc_ops,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100825 supported_protocols,
826 tx_headroom + NCI_DATA_HDR_SIZE,
827 tx_tailroom);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300828 if (!ndev->nfc_dev)
829 goto free_exit;
830
831 nfc_set_drvdata(ndev->nfc_dev, ndev);
832
Dan Carpenter8ebafde2011-09-23 09:14:35 +0300833 return ndev;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300834
835free_exit:
836 kfree(ndev);
Dan Carpenter8ebafde2011-09-23 09:14:35 +0300837 return NULL;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300838}
839EXPORT_SYMBOL(nci_allocate_device);
840
841/**
842 * nci_free_device - deallocate nci device
843 *
844 * @ndev: The nci device to deallocate
845 */
846void nci_free_device(struct nci_dev *ndev)
847{
Ilan Elias6a2968a2011-09-18 11:19:35 +0300848 nfc_free_device(ndev->nfc_dev);
849 kfree(ndev);
850}
851EXPORT_SYMBOL(nci_free_device);
852
853/**
854 * nci_register_device - register a nci device in the nfc subsystem
855 *
856 * @dev: The nci device to register
857 */
858int nci_register_device(struct nci_dev *ndev)
859{
860 int rc;
861 struct device *dev = &ndev->nfc_dev->dev;
862 char name[32];
863
Ilan Elias6a2968a2011-09-18 11:19:35 +0300864 ndev->flags = 0;
865
866 INIT_WORK(&ndev->cmd_work, nci_cmd_work);
867 snprintf(name, sizeof(name), "%s_nci_cmd_wq", dev_name(dev));
868 ndev->cmd_wq = create_singlethread_workqueue(name);
869 if (!ndev->cmd_wq) {
870 rc = -ENOMEM;
Vincent Cuissard3c1c0f52014-07-22 19:48:39 +0200871 goto exit;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300872 }
873
874 INIT_WORK(&ndev->rx_work, nci_rx_work);
875 snprintf(name, sizeof(name), "%s_nci_rx_wq", dev_name(dev));
876 ndev->rx_wq = create_singlethread_workqueue(name);
877 if (!ndev->rx_wq) {
878 rc = -ENOMEM;
879 goto destroy_cmd_wq_exit;
880 }
881
882 INIT_WORK(&ndev->tx_work, nci_tx_work);
883 snprintf(name, sizeof(name), "%s_nci_tx_wq", dev_name(dev));
884 ndev->tx_wq = create_singlethread_workqueue(name);
885 if (!ndev->tx_wq) {
886 rc = -ENOMEM;
887 goto destroy_rx_wq_exit;
888 }
889
890 skb_queue_head_init(&ndev->cmd_q);
891 skb_queue_head_init(&ndev->rx_q);
892 skb_queue_head_init(&ndev->tx_q);
893
894 setup_timer(&ndev->cmd_timer, nci_cmd_timer,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100895 (unsigned long) ndev);
Ilan Eliasc4bf98b2012-01-17 12:03:50 +0200896 setup_timer(&ndev->data_timer, nci_data_timer,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100897 (unsigned long) ndev);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300898
899 mutex_init(&ndev->req_lock);
900
Vincent Cuissard3c1c0f52014-07-22 19:48:39 +0200901 rc = nfc_register_device(ndev->nfc_dev);
902 if (rc)
903 goto destroy_rx_wq_exit;
904
Ilan Elias6a2968a2011-09-18 11:19:35 +0300905 goto exit;
906
907destroy_rx_wq_exit:
908 destroy_workqueue(ndev->rx_wq);
909
910destroy_cmd_wq_exit:
911 destroy_workqueue(ndev->cmd_wq);
912
Ilan Elias6a2968a2011-09-18 11:19:35 +0300913exit:
914 return rc;
915}
916EXPORT_SYMBOL(nci_register_device);
917
918/**
919 * nci_unregister_device - unregister a nci device in the nfc subsystem
920 *
921 * @dev: The nci device to unregister
922 */
923void nci_unregister_device(struct nci_dev *ndev)
924{
Ilan Elias6a2968a2011-09-18 11:19:35 +0300925 nci_close_device(ndev);
926
927 destroy_workqueue(ndev->cmd_wq);
928 destroy_workqueue(ndev->rx_wq);
929 destroy_workqueue(ndev->tx_wq);
930
931 nfc_unregister_device(ndev->nfc_dev);
932}
933EXPORT_SYMBOL(nci_unregister_device);
934
935/**
936 * nci_recv_frame - receive frame from NCI drivers
937 *
Frederic Danis1095e692013-05-22 11:36:17 +0200938 * @ndev: The nci device
Ilan Elias6a2968a2011-09-18 11:19:35 +0300939 * @skb: The sk_buff to receive
940 */
Frederic Danis1095e692013-05-22 11:36:17 +0200941int nci_recv_frame(struct nci_dev *ndev, struct sk_buff *skb)
Ilan Elias6a2968a2011-09-18 11:19:35 +0300942{
Joe Perches24bf3302011-11-29 11:37:35 -0800943 pr_debug("len %d\n", skb->len);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300944
Szymon Janc874934f2012-10-04 15:15:51 +0200945 if (!ndev || (!test_bit(NCI_UP, &ndev->flags) &&
946 !test_bit(NCI_INIT, &ndev->flags))) {
Ilan Elias6a2968a2011-09-18 11:19:35 +0300947 kfree_skb(skb);
948 return -ENXIO;
949 }
950
951 /* Queue frame for rx worker thread */
952 skb_queue_tail(&ndev->rx_q, skb);
953 queue_work(ndev->rx_wq, &ndev->rx_work);
954
955 return 0;
956}
957EXPORT_SYMBOL(nci_recv_frame);
958
Frederic Danis1095e692013-05-22 11:36:17 +0200959static int nci_send_frame(struct nci_dev *ndev, struct sk_buff *skb)
Ilan Elias6a2968a2011-09-18 11:19:35 +0300960{
Joe Perches24bf3302011-11-29 11:37:35 -0800961 pr_debug("len %d\n", skb->len);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300962
963 if (!ndev) {
964 kfree_skb(skb);
965 return -ENODEV;
966 }
967
968 /* Get rid of skb owner, prior to sending to the driver. */
969 skb_orphan(skb);
970
Hiren Tandel05158292014-05-05 19:52:27 +0900971 /* Send copy to sniffer */
972 nfc_send_to_raw_sock(ndev->nfc_dev, skb,
973 RAW_PAYLOAD_NCI, NFC_DIRECTION_TX);
974
Frederic Danis1095e692013-05-22 11:36:17 +0200975 return ndev->ops->send(ndev, skb);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300976}
977
978/* Send NCI command */
979int nci_send_cmd(struct nci_dev *ndev, __u16 opcode, __u8 plen, void *payload)
980{
981 struct nci_ctrl_hdr *hdr;
982 struct sk_buff *skb;
983
Joe Perches24bf3302011-11-29 11:37:35 -0800984 pr_debug("opcode 0x%x, plen %d\n", opcode, plen);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300985
986 skb = nci_skb_alloc(ndev, (NCI_CTRL_HDR_SIZE + plen), GFP_KERNEL);
987 if (!skb) {
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800988 pr_err("no memory for command\n");
Ilan Elias6a2968a2011-09-18 11:19:35 +0300989 return -ENOMEM;
990 }
991
992 hdr = (struct nci_ctrl_hdr *) skb_put(skb, NCI_CTRL_HDR_SIZE);
993 hdr->gid = nci_opcode_gid(opcode);
994 hdr->oid = nci_opcode_oid(opcode);
995 hdr->plen = plen;
996
997 nci_mt_set((__u8 *)hdr, NCI_MT_CMD_PKT);
998 nci_pbf_set((__u8 *)hdr, NCI_PBF_LAST);
999
1000 if (plen)
1001 memcpy(skb_put(skb, plen), payload, plen);
1002
Ilan Elias6a2968a2011-09-18 11:19:35 +03001003 skb_queue_tail(&ndev->cmd_q, skb);
1004 queue_work(ndev->cmd_wq, &ndev->cmd_work);
1005
1006 return 0;
1007}
1008
1009/* ---- NCI TX Data worker thread ---- */
1010
1011static void nci_tx_work(struct work_struct *work)
1012{
1013 struct nci_dev *ndev = container_of(work, struct nci_dev, tx_work);
1014 struct sk_buff *skb;
1015
Joe Perches24bf3302011-11-29 11:37:35 -08001016 pr_debug("credits_cnt %d\n", atomic_read(&ndev->credits_cnt));
Ilan Elias6a2968a2011-09-18 11:19:35 +03001017
1018 /* Send queued tx data */
1019 while (atomic_read(&ndev->credits_cnt)) {
1020 skb = skb_dequeue(&ndev->tx_q);
1021 if (!skb)
1022 return;
1023
Ilan Eliasdb98c822011-11-09 12:09:16 +02001024 /* Check if data flow control is used */
1025 if (atomic_read(&ndev->credits_cnt) !=
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +01001026 NCI_DATA_FLOW_CONTROL_NOT_USED)
Ilan Eliasdb98c822011-11-09 12:09:16 +02001027 atomic_dec(&ndev->credits_cnt);
Ilan Elias6a2968a2011-09-18 11:19:35 +03001028
Joe Perches20c239c2011-11-29 11:37:33 -08001029 pr_debug("NCI TX: MT=data, PBF=%d, conn_id=%d, plen=%d\n",
1030 nci_pbf(skb->data),
1031 nci_conn_id(skb->data),
1032 nci_plen(skb->data));
Ilan Elias6a2968a2011-09-18 11:19:35 +03001033
Frederic Danis1095e692013-05-22 11:36:17 +02001034 nci_send_frame(ndev, skb);
Ilan Eliasc4bf98b2012-01-17 12:03:50 +02001035
1036 mod_timer(&ndev->data_timer,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +01001037 jiffies + msecs_to_jiffies(NCI_DATA_TIMEOUT));
Ilan Elias6a2968a2011-09-18 11:19:35 +03001038 }
1039}
1040
1041/* ----- NCI RX worker thread (data & control) ----- */
1042
1043static void nci_rx_work(struct work_struct *work)
1044{
1045 struct nci_dev *ndev = container_of(work, struct nci_dev, rx_work);
1046 struct sk_buff *skb;
1047
1048 while ((skb = skb_dequeue(&ndev->rx_q))) {
Hiren Tandel05158292014-05-05 19:52:27 +09001049
1050 /* Send copy to sniffer */
1051 nfc_send_to_raw_sock(ndev->nfc_dev, skb,
1052 RAW_PAYLOAD_NCI, NFC_DIRECTION_RX);
1053
Ilan Elias6a2968a2011-09-18 11:19:35 +03001054 /* Process frame */
1055 switch (nci_mt(skb->data)) {
1056 case NCI_MT_RSP_PKT:
1057 nci_rsp_packet(ndev, skb);
1058 break;
1059
1060 case NCI_MT_NTF_PKT:
1061 nci_ntf_packet(ndev, skb);
1062 break;
1063
1064 case NCI_MT_DATA_PKT:
1065 nci_rx_data_packet(ndev, skb);
1066 break;
1067
1068 default:
Joe Perchesed1e0ad2011-11-29 11:37:32 -08001069 pr_err("unknown MT 0x%x\n", nci_mt(skb->data));
Ilan Elias6a2968a2011-09-18 11:19:35 +03001070 kfree_skb(skb);
1071 break;
1072 }
1073 }
Ilan Eliasc4bf98b2012-01-17 12:03:50 +02001074
1075 /* check if a data exchange timout has occurred */
1076 if (test_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags)) {
1077 /* complete the data exchange transaction, if exists */
1078 if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags))
1079 nci_data_exchange_complete(ndev, NULL, -ETIMEDOUT);
1080
1081 clear_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags);
1082 }
Ilan Elias6a2968a2011-09-18 11:19:35 +03001083}
1084
1085/* ----- NCI TX CMD worker thread ----- */
1086
1087static void nci_cmd_work(struct work_struct *work)
1088{
1089 struct nci_dev *ndev = container_of(work, struct nci_dev, cmd_work);
1090 struct sk_buff *skb;
1091
Joe Perches24bf3302011-11-29 11:37:35 -08001092 pr_debug("cmd_cnt %d\n", atomic_read(&ndev->cmd_cnt));
Ilan Elias6a2968a2011-09-18 11:19:35 +03001093
1094 /* Send queued command */
1095 if (atomic_read(&ndev->cmd_cnt)) {
1096 skb = skb_dequeue(&ndev->cmd_q);
1097 if (!skb)
1098 return;
1099
1100 atomic_dec(&ndev->cmd_cnt);
1101
Joe Perches20c239c2011-11-29 11:37:33 -08001102 pr_debug("NCI TX: MT=cmd, PBF=%d, GID=0x%x, OID=0x%x, plen=%d\n",
1103 nci_pbf(skb->data),
1104 nci_opcode_gid(nci_opcode(skb->data)),
1105 nci_opcode_oid(nci_opcode(skb->data)),
1106 nci_plen(skb->data));
Ilan Elias6a2968a2011-09-18 11:19:35 +03001107
Frederic Danis1095e692013-05-22 11:36:17 +02001108 nci_send_frame(ndev, skb);
Ilan Elias6a2968a2011-09-18 11:19:35 +03001109
1110 mod_timer(&ndev->cmd_timer,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +01001111 jiffies + msecs_to_jiffies(NCI_CMD_TIMEOUT));
Ilan Elias6a2968a2011-09-18 11:19:35 +03001112 }
1113}
Dave Jones8a70e7f2012-07-12 19:17:34 +02001114
1115MODULE_LICENSE("GPL");