blob: e351e94f8d407957487e19f3724b2ddbf2312ea8 [file] [log] [blame]
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +02001/*
2 * Copyright (C) 2012 Intel Corporation. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
Jeff Kirsher98b32de2013-12-06 08:56:16 -080015 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +020016 */
17
18#define pr_fmt(fmt) "hci: %s: " fmt, __func__
19
20#include <linux/init.h>
21#include <linux/kernel.h>
22#include <linux/module.h>
23#include <linux/nfc.h>
24
25#include <net/nfc/nfc.h>
26#include <net/nfc/hci.h>
Eric Lapuyade67cccfe2012-09-13 17:10:00 +020027#include <net/nfc/llc.h>
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +020028
29#include "hci.h"
30
31/* Largest headroom needed for outgoing HCI commands */
32#define HCI_CMDS_HEADROOM 1
33
Eric Lapuyade84d48192012-10-17 16:50:10 +020034int nfc_hci_result_to_errno(u8 result)
Eric Lapuyade6c1c5b92012-05-03 15:35:25 +020035{
36 switch (result) {
37 case NFC_HCI_ANY_OK:
38 return 0;
Eric Lapuyade23f7e6d2012-10-17 16:48:21 +020039 case NFC_HCI_ANY_E_REG_PAR_UNKNOWN:
40 return -EOPNOTSUPP;
Eric Lapuyade6c1c5b92012-05-03 15:35:25 +020041 case NFC_HCI_ANY_E_TIMEOUT:
42 return -ETIME;
43 default:
44 return -1;
45 }
46}
Eric Lapuyade84d48192012-10-17 16:50:10 +020047EXPORT_SYMBOL(nfc_hci_result_to_errno);
Eric Lapuyade6c1c5b92012-05-03 15:35:25 +020048
Christophe Ricard118278f2015-01-27 01:18:12 +010049void nfc_hci_reset_pipes(struct nfc_hci_dev *hdev)
50{
51 int i = 0;
52
53 for (i = 0; i < NFC_HCI_MAX_PIPES; i++) {
54 hdev->pipes[i].gate = NFC_HCI_INVALID_GATE;
55 hdev->pipes[i].dest_host = NFC_HCI_INVALID_HOST;
56 }
57 memset(hdev->gate2pipe, NFC_HCI_INVALID_PIPE, sizeof(hdev->gate2pipe));
58}
59EXPORT_SYMBOL(nfc_hci_reset_pipes);
60
61void nfc_hci_reset_pipes_per_host(struct nfc_hci_dev *hdev, u8 host)
62{
63 int i = 0;
64
65 for (i = 0; i < NFC_HCI_MAX_PIPES; i++) {
66 if (hdev->pipes[i].dest_host != host)
67 continue;
68
69 hdev->pipes[i].gate = NFC_HCI_INVALID_GATE;
70 hdev->pipes[i].dest_host = NFC_HCI_INVALID_HOST;
71 }
72}
73EXPORT_SYMBOL(nfc_hci_reset_pipes_per_host);
74
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +020075static void nfc_hci_msg_tx_work(struct work_struct *work)
76{
77 struct nfc_hci_dev *hdev = container_of(work, struct nfc_hci_dev,
78 msg_tx_work);
79 struct hci_msg *msg;
80 struct sk_buff *skb;
81 int r = 0;
82
83 mutex_lock(&hdev->msg_tx_mutex);
Eric Lapuyadef0c91032012-11-26 18:06:27 +010084 if (hdev->shutting_down)
85 goto exit;
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +020086
87 if (hdev->cmd_pending_msg) {
88 if (timer_pending(&hdev->cmd_timer) == 0) {
89 if (hdev->cmd_pending_msg->cb)
Eric Lapuyadeb5faa642012-09-11 10:41:41 +020090 hdev->cmd_pending_msg->cb(hdev->
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +020091 cmd_pending_msg->
Eric Lapuyadeb5faa642012-09-11 10:41:41 +020092 cb_context,
93 NULL,
94 -ETIME);
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +020095 kfree(hdev->cmd_pending_msg);
96 hdev->cmd_pending_msg = NULL;
Szymon Janc0f450772012-10-17 15:23:39 +020097 } else {
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +020098 goto exit;
Szymon Janc0f450772012-10-17 15:23:39 +020099 }
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +0200100 }
101
102next_msg:
103 if (list_empty(&hdev->msg_tx_queue))
104 goto exit;
105
106 msg = list_first_entry(&hdev->msg_tx_queue, struct hci_msg, msg_l);
107 list_del(&msg->msg_l);
108
109 pr_debug("msg_tx_queue has a cmd to send\n");
110 while ((skb = skb_dequeue(&msg->msg_frags)) != NULL) {
Eric Lapuyade412fda52012-09-18 19:45:48 +0200111 r = nfc_llc_xmit_from_hci(hdev->llc, skb);
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +0200112 if (r < 0) {
113 kfree_skb(skb);
114 skb_queue_purge(&msg->msg_frags);
115 if (msg->cb)
Eric Lapuyadeb5faa642012-09-11 10:41:41 +0200116 msg->cb(msg->cb_context, NULL, r);
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +0200117 kfree(msg);
118 break;
119 }
120 }
121
122 if (r)
123 goto next_msg;
124
125 if (msg->wait_response == false) {
126 kfree(msg);
127 goto next_msg;
128 }
129
130 hdev->cmd_pending_msg = msg;
131 mod_timer(&hdev->cmd_timer, jiffies +
132 msecs_to_jiffies(hdev->cmd_pending_msg->completion_delay));
133
134exit:
135 mutex_unlock(&hdev->msg_tx_mutex);
136}
137
138static void nfc_hci_msg_rx_work(struct work_struct *work)
139{
140 struct nfc_hci_dev *hdev = container_of(work, struct nfc_hci_dev,
141 msg_rx_work);
142 struct sk_buff *skb;
143 struct hcp_message *message;
144 u8 pipe;
145 u8 type;
146 u8 instruction;
147
148 while ((skb = skb_dequeue(&hdev->msg_rx_queue)) != NULL) {
149 pipe = skb->data[0];
150 skb_pull(skb, NFC_HCI_HCP_PACKET_HEADER_LEN);
151 message = (struct hcp_message *)skb->data;
152 type = HCP_MSG_GET_TYPE(message->header);
153 instruction = HCP_MSG_GET_CMD(message->header);
154 skb_pull(skb, NFC_HCI_HCP_MESSAGE_HEADER_LEN);
155
156 nfc_hci_hcp_message_rx(hdev, pipe, type, instruction, skb);
157 }
158}
159
Eric Lapuyadeccca0d62012-05-03 15:59:37 +0200160static void __nfc_hci_cmd_completion(struct nfc_hci_dev *hdev, int err,
161 struct sk_buff *skb)
162{
163 del_timer_sync(&hdev->cmd_timer);
164
165 if (hdev->cmd_pending_msg->cb)
Eric Lapuyadeb5faa642012-09-11 10:41:41 +0200166 hdev->cmd_pending_msg->cb(hdev->cmd_pending_msg->cb_context,
167 skb, err);
Eric Lapuyadeccca0d62012-05-03 15:59:37 +0200168 else
169 kfree_skb(skb);
170
171 kfree(hdev->cmd_pending_msg);
172 hdev->cmd_pending_msg = NULL;
173
Linus Torvalds916082b2012-10-02 16:01:31 -0700174 schedule_work(&hdev->msg_tx_work);
Eric Lapuyadeccca0d62012-05-03 15:59:37 +0200175}
176
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +0200177void nfc_hci_resp_received(struct nfc_hci_dev *hdev, u8 result,
178 struct sk_buff *skb)
179{
180 mutex_lock(&hdev->msg_tx_mutex);
181
182 if (hdev->cmd_pending_msg == NULL) {
183 kfree_skb(skb);
184 goto exit;
185 }
186
Eric Lapuyadeccca0d62012-05-03 15:59:37 +0200187 __nfc_hci_cmd_completion(hdev, nfc_hci_result_to_errno(result), skb);
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +0200188
189exit:
190 mutex_unlock(&hdev->msg_tx_mutex);
191}
192
193void nfc_hci_cmd_received(struct nfc_hci_dev *hdev, u8 pipe, u8 cmd,
194 struct sk_buff *skb)
195{
Christophe Ricarddeff5aa2014-11-13 00:30:42 +0100196 int r = 0;
Christophe Ricard118278f2015-01-27 01:18:12 +0100197 u8 gate = hdev->pipes[pipe].gate;
Christophe Ricarddeff5aa2014-11-13 00:30:42 +0100198 u8 local_gate, new_pipe;
199 u8 gate_opened = 0x00;
200
201 pr_debug("from gate %x pipe %x cmd %x\n", gate, pipe, cmd);
202
203 switch (cmd) {
204 case NFC_HCI_ADM_NOTIFY_PIPE_CREATED:
205 if (skb->len != 5) {
206 r = -EPROTO;
207 break;
208 }
209
210 local_gate = skb->data[3];
211 new_pipe = skb->data[4];
Christophe Ricardaf775222015-01-27 01:18:13 +0100212 nfc_hci_hcp_message_tx(hdev, pipe, NFC_HCI_HCP_RESPONSE,
213 NFC_HCI_ANY_OK, NULL, 0, NULL, NULL, 0);
Christophe Ricarddeff5aa2014-11-13 00:30:42 +0100214
215 /* save the new created pipe and bind with local gate,
216 * the description for skb->data[3] is destination gate id
217 * but since we received this cmd from host controller, we
218 * are the destination and it is our local gate
219 */
220 hdev->gate2pipe[local_gate] = new_pipe;
221 break;
222 case NFC_HCI_ANY_OPEN_PIPE:
223 /* if the pipe is already created, we allow remote host to
224 * open it
225 */
226 if (gate != 0xff)
Christophe Ricardaf775222015-01-27 01:18:13 +0100227 nfc_hci_hcp_message_tx(hdev, pipe, NFC_HCI_HCP_RESPONSE,
228 NFC_HCI_ANY_OK, &gate_opened, 1,
229 NULL, NULL, 0);
Christophe Ricarddeff5aa2014-11-13 00:30:42 +0100230 break;
Christophe Ricarda2ae2182014-11-13 00:30:43 +0100231 case NFC_HCI_ADM_NOTIFY_ALL_PIPE_CLEARED:
Christophe Ricardaf775222015-01-27 01:18:13 +0100232 nfc_hci_hcp_message_tx(hdev, pipe, NFC_HCI_HCP_RESPONSE,
233 NFC_HCI_ANY_OK, NULL, 0, NULL, NULL, 0);
234
Christophe Ricarda2ae2182014-11-13 00:30:43 +0100235 break;
Christophe Ricarddeff5aa2014-11-13 00:30:42 +0100236 default:
237 pr_info("Discarded unknown cmd %x to gate %x\n", cmd, gate);
238 r = -EINVAL;
239 break;
240 }
241
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +0200242 kfree_skb(skb);
243}
244
Eric Lapuyade9c5121a2012-10-23 11:37:43 +0200245u32 nfc_hci_sak_to_protocol(u8 sak)
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +0200246{
247 switch (NFC_HCI_TYPE_A_SEL_PROT(sak)) {
248 case NFC_HCI_TYPE_A_SEL_PROT_MIFARE:
249 return NFC_PROTO_MIFARE_MASK;
250 case NFC_HCI_TYPE_A_SEL_PROT_ISO14443:
251 return NFC_PROTO_ISO14443_MASK;
252 case NFC_HCI_TYPE_A_SEL_PROT_DEP:
253 return NFC_PROTO_NFC_DEP_MASK;
254 case NFC_HCI_TYPE_A_SEL_PROT_ISO14443_DEP:
255 return NFC_PROTO_ISO14443_MASK | NFC_PROTO_NFC_DEP_MASK;
256 default:
257 return 0xffffffff;
258 }
259}
Eric Lapuyade9c5121a2012-10-23 11:37:43 +0200260EXPORT_SYMBOL(nfc_hci_sak_to_protocol);
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +0200261
Arron Wangf7a5f6c2012-09-27 17:32:55 +0800262int nfc_hci_target_discovered(struct nfc_hci_dev *hdev, u8 gate)
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +0200263{
264 struct nfc_target *targets;
265 struct sk_buff *atqa_skb = NULL;
266 struct sk_buff *sak_skb = NULL;
Eric Lapuyade81b30392012-07-12 20:27:54 +0200267 struct sk_buff *uid_skb = NULL;
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +0200268 int r;
269
270 pr_debug("from gate %d\n", gate);
271
272 targets = kzalloc(sizeof(struct nfc_target), GFP_KERNEL);
273 if (targets == NULL)
274 return -ENOMEM;
275
276 switch (gate) {
277 case NFC_HCI_RF_READER_A_GATE:
278 r = nfc_hci_get_param(hdev, NFC_HCI_RF_READER_A_GATE,
279 NFC_HCI_RF_READER_A_ATQA, &atqa_skb);
280 if (r < 0)
281 goto exit;
282
283 r = nfc_hci_get_param(hdev, NFC_HCI_RF_READER_A_GATE,
284 NFC_HCI_RF_READER_A_SAK, &sak_skb);
285 if (r < 0)
286 goto exit;
287
288 if (atqa_skb->len != 2 || sak_skb->len != 1) {
289 r = -EPROTO;
290 goto exit;
291 }
292
293 targets->supported_protocols =
294 nfc_hci_sak_to_protocol(sak_skb->data[0]);
295 if (targets->supported_protocols == 0xffffffff) {
296 r = -EPROTO;
297 goto exit;
298 }
299
Christophe Ricard74157ef2014-04-01 00:34:01 +0200300 targets->sens_res = be16_to_cpu(*(__be16 *)atqa_skb->data);
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +0200301 targets->sel_res = sak_skb->data[0];
302
Eric Lapuyade81b30392012-07-12 20:27:54 +0200303 r = nfc_hci_get_param(hdev, NFC_HCI_RF_READER_A_GATE,
304 NFC_HCI_RF_READER_A_UID, &uid_skb);
305 if (r < 0)
306 goto exit;
307
308 if (uid_skb->len == 0 || uid_skb->len > NFC_NFCID1_MAXSIZE) {
309 r = -EPROTO;
310 goto exit;
311 }
312
313 memcpy(targets->nfcid1, uid_skb->data, uid_skb->len);
314 targets->nfcid1_len = uid_skb->len;
315
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +0200316 if (hdev->ops->complete_target_discovered) {
317 r = hdev->ops->complete_target_discovered(hdev, gate,
318 targets);
319 if (r < 0)
320 goto exit;
321 }
322 break;
323 case NFC_HCI_RF_READER_B_GATE:
Samuel Ortiz01d719a2012-07-04 00:14:04 +0200324 targets->supported_protocols = NFC_PROTO_ISO14443_B_MASK;
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +0200325 break;
326 default:
327 if (hdev->ops->target_from_gate)
328 r = hdev->ops->target_from_gate(hdev, gate, targets);
329 else
330 r = -EPROTO;
331 if (r < 0)
332 goto exit;
333
334 if (hdev->ops->complete_target_discovered) {
335 r = hdev->ops->complete_target_discovered(hdev, gate,
336 targets);
337 if (r < 0)
338 goto exit;
339 }
340 break;
341 }
342
Arron Wang928326f2012-09-27 17:32:56 +0800343 /* if driver set the new gate, we will skip the old one */
344 if (targets->hci_reader_gate == 0x00)
345 targets->hci_reader_gate = gate;
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +0200346
347 r = nfc_targets_found(hdev->ndev, targets, 1);
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +0200348
349exit:
350 kfree(targets);
351 kfree_skb(atqa_skb);
352 kfree_skb(sak_skb);
Eric Lapuyade81b30392012-07-12 20:27:54 +0200353 kfree_skb(uid_skb);
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +0200354
355 return r;
356}
Arron Wangf7a5f6c2012-09-27 17:32:55 +0800357EXPORT_SYMBOL(nfc_hci_target_discovered);
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +0200358
359void nfc_hci_event_received(struct nfc_hci_dev *hdev, u8 pipe, u8 event,
360 struct sk_buff *skb)
361{
362 int r = 0;
Christophe Ricard118278f2015-01-27 01:18:12 +0100363 u8 gate = hdev->pipes[pipe].gate;
Eric Lapuyade74a5b962012-10-17 16:49:12 +0200364
Christophe Ricard118278f2015-01-27 01:18:12 +0100365 if (gate == NFC_HCI_INVALID_GATE) {
Eric Lapuyade74a5b962012-10-17 16:49:12 +0200366 pr_err("Discarded event %x to unopened pipe %x\n", event, pipe);
367 goto exit;
368 }
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +0200369
Eric Lapuyade40d06d32012-12-04 16:43:24 +0100370 if (hdev->ops->event_received) {
Christophe Ricardfda7a492015-01-27 01:18:11 +0100371 r = hdev->ops->event_received(hdev, pipe, event, skb);
Eric Lapuyade40d06d32012-12-04 16:43:24 +0100372 if (r <= 0)
373 goto exit_noskb;
374 }
375
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +0200376 switch (event) {
377 case NFC_HCI_EVT_TARGET_DISCOVERED:
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +0200378 if (skb->len < 1) { /* no status data? */
379 r = -EPROTO;
380 goto exit;
381 }
382
383 if (skb->data[0] == 3) {
384 /* TODO: Multiple targets in field, none activated
385 * poll is supposedly stopped, but there is no
386 * single target to activate, so nothing to report
387 * up.
388 * if we need to restart poll, we must save the
389 * protocols from the initial poll and reuse here.
390 */
391 }
392
393 if (skb->data[0] != 0) {
394 r = -EPROTO;
395 goto exit;
396 }
397
Eric Lapuyade74a5b962012-10-17 16:49:12 +0200398 r = nfc_hci_target_discovered(hdev, gate);
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +0200399 break;
400 default:
Eric Lapuyade40d06d32012-12-04 16:43:24 +0100401 pr_info("Discarded unknown event %x to gate %x\n", event, gate);
402 r = -EINVAL;
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +0200403 break;
404 }
405
406exit:
407 kfree_skb(skb);
408
Eric Lapuyade27c31192012-11-28 15:48:44 +0100409exit_noskb:
Samuel Ortiz249eb5b2013-11-13 01:00:07 +0100410 if (r)
411 nfc_hci_driver_failure(hdev, r);
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +0200412}
413
414static void nfc_hci_cmd_timeout(unsigned long data)
415{
416 struct nfc_hci_dev *hdev = (struct nfc_hci_dev *)data;
417
Linus Torvalds916082b2012-10-02 16:01:31 -0700418 schedule_work(&hdev->msg_tx_work);
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +0200419}
420
421static int hci_dev_connect_gates(struct nfc_hci_dev *hdev, u8 gate_count,
Eric Lapuyadea10d5952012-06-05 14:42:11 +0200422 struct nfc_hci_gate *gates)
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +0200423{
424 int r;
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +0200425 while (gate_count--) {
Eric Lapuyadea10d5952012-06-05 14:42:11 +0200426 r = nfc_hci_connect_gate(hdev, NFC_HCI_HOST_CONTROLLER_ID,
427 gates->gate, gates->pipe);
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +0200428 if (r < 0)
429 return r;
Eric Lapuyadea10d5952012-06-05 14:42:11 +0200430 gates++;
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +0200431 }
432
433 return 0;
434}
435
436static int hci_dev_session_init(struct nfc_hci_dev *hdev)
437{
438 struct sk_buff *skb = NULL;
439 int r;
Eric Lapuyadea10d5952012-06-05 14:42:11 +0200440
441 if (hdev->init_data.gates[0].gate != NFC_HCI_ADMIN_GATE)
442 return -EPROTO;
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +0200443
444 r = nfc_hci_connect_gate(hdev, NFC_HCI_HOST_CONTROLLER_ID,
Eric Lapuyadea10d5952012-06-05 14:42:11 +0200445 hdev->init_data.gates[0].gate,
446 hdev->init_data.gates[0].pipe);
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +0200447 if (r < 0)
448 goto exit;
449
450 r = nfc_hci_get_param(hdev, NFC_HCI_ADMIN_GATE,
451 NFC_HCI_ADMIN_SESSION_IDENTITY, &skb);
452 if (r < 0)
453 goto disconnect_all;
454
Christophe Ricarde240bc32014-03-25 06:51:49 +0100455 if (skb->len && skb->len == strlen(hdev->init_data.session_id) &&
456 (memcmp(hdev->init_data.session_id, skb->data,
457 skb->len) == 0) && hdev->ops->load_session) {
458 /* Restore gate<->pipe table from some proprietary location. */
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +0200459
Christophe Ricarde240bc32014-03-25 06:51:49 +0100460 r = hdev->ops->load_session(hdev);
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +0200461
Christophe Ricarde240bc32014-03-25 06:51:49 +0100462 if (r < 0)
463 goto disconnect_all;
464 } else {
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +0200465
Christophe Ricarde240bc32014-03-25 06:51:49 +0100466 r = nfc_hci_disconnect_all_gates(hdev);
467 if (r < 0)
468 goto exit;
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +0200469
Christophe Ricarde240bc32014-03-25 06:51:49 +0100470 r = hci_dev_connect_gates(hdev, hdev->init_data.gate_count,
471 hdev->init_data.gates);
472 if (r < 0)
473 goto disconnect_all;
474
475 r = nfc_hci_set_param(hdev, NFC_HCI_ADMIN_GATE,
476 NFC_HCI_ADMIN_SESSION_IDENTITY,
477 hdev->init_data.session_id,
478 strlen(hdev->init_data.session_id));
479 }
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +0200480 if (r == 0)
481 goto exit;
482
483disconnect_all:
484 nfc_hci_disconnect_all_gates(hdev);
485
486exit:
Wei Yongjun33e59712012-08-28 21:02:40 +0800487 kfree_skb(skb);
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +0200488
489 return r;
490}
491
492static int hci_dev_version(struct nfc_hci_dev *hdev)
493{
494 int r;
495 struct sk_buff *skb;
496
497 r = nfc_hci_get_param(hdev, NFC_HCI_ID_MGMT_GATE,
498 NFC_HCI_ID_MGMT_VERSION_SW, &skb);
Eric Lapuyade23f7e6d2012-10-17 16:48:21 +0200499 if (r == -EOPNOTSUPP) {
500 pr_info("Software/Hardware info not available\n");
501 return 0;
502 }
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +0200503 if (r < 0)
504 return r;
505
506 if (skb->len != 3) {
507 kfree_skb(skb);
508 return -EINVAL;
509 }
510
511 hdev->sw_romlib = (skb->data[0] & 0xf0) >> 4;
512 hdev->sw_patch = skb->data[0] & 0x0f;
513 hdev->sw_flashlib_major = skb->data[1];
514 hdev->sw_flashlib_minor = skb->data[2];
515
516 kfree_skb(skb);
517
518 r = nfc_hci_get_param(hdev, NFC_HCI_ID_MGMT_GATE,
519 NFC_HCI_ID_MGMT_VERSION_HW, &skb);
520 if (r < 0)
521 return r;
522
523 if (skb->len != 3) {
524 kfree_skb(skb);
525 return -EINVAL;
526 }
527
528 hdev->hw_derivative = (skb->data[0] & 0xe0) >> 5;
529 hdev->hw_version = skb->data[0] & 0x1f;
530 hdev->hw_mpw = (skb->data[1] & 0xc0) >> 6;
531 hdev->hw_software = skb->data[1] & 0x3f;
532 hdev->hw_bsid = skb->data[2];
533
534 kfree_skb(skb);
535
536 pr_info("SOFTWARE INFO:\n");
537 pr_info("RomLib : %d\n", hdev->sw_romlib);
538 pr_info("Patch : %d\n", hdev->sw_patch);
539 pr_info("FlashLib Major : %d\n", hdev->sw_flashlib_major);
540 pr_info("FlashLib Minor : %d\n", hdev->sw_flashlib_minor);
541 pr_info("HARDWARE INFO:\n");
542 pr_info("Derivative : %d\n", hdev->hw_derivative);
543 pr_info("HW Version : %d\n", hdev->hw_version);
544 pr_info("#MPW : %d\n", hdev->hw_mpw);
545 pr_info("Software : %d\n", hdev->hw_software);
546 pr_info("BSID Version : %d\n", hdev->hw_bsid);
547
548 return 0;
549}
550
551static int hci_dev_up(struct nfc_dev *nfc_dev)
552{
553 struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
554 int r = 0;
555
556 if (hdev->ops->open) {
557 r = hdev->ops->open(hdev);
558 if (r < 0)
559 return r;
560 }
561
Eric Lapuyade412fda52012-09-18 19:45:48 +0200562 r = nfc_llc_start(hdev->llc);
563 if (r < 0)
564 goto exit_close;
565
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +0200566 r = hci_dev_session_init(hdev);
567 if (r < 0)
Eric Lapuyade412fda52012-09-18 19:45:48 +0200568 goto exit_llc;
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +0200569
570 r = nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
571 NFC_HCI_EVT_END_OPERATION, NULL, 0);
572 if (r < 0)
Eric Lapuyade412fda52012-09-18 19:45:48 +0200573 goto exit_llc;
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +0200574
575 if (hdev->ops->hci_ready) {
576 r = hdev->ops->hci_ready(hdev);
577 if (r < 0)
Eric Lapuyade412fda52012-09-18 19:45:48 +0200578 goto exit_llc;
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +0200579 }
580
581 r = hci_dev_version(hdev);
582 if (r < 0)
Eric Lapuyade412fda52012-09-18 19:45:48 +0200583 goto exit_llc;
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +0200584
Eric Lapuyade412fda52012-09-18 19:45:48 +0200585 return 0;
586
587exit_llc:
588 nfc_llc_stop(hdev->llc);
589
590exit_close:
591 if (hdev->ops->close)
592 hdev->ops->close(hdev);
593
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +0200594 return r;
595}
596
597static int hci_dev_down(struct nfc_dev *nfc_dev)
598{
599 struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
600
Eric Lapuyade412fda52012-09-18 19:45:48 +0200601 nfc_llc_stop(hdev->llc);
602
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +0200603 if (hdev->ops->close)
604 hdev->ops->close(hdev);
605
Christophe Ricard118278f2015-01-27 01:18:12 +0100606 nfc_hci_reset_pipes(hdev);
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +0200607
608 return 0;
609}
610
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200611static int hci_start_poll(struct nfc_dev *nfc_dev,
612 u32 im_protocols, u32 tm_protocols)
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +0200613{
614 struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +0200615
616 if (hdev->ops->start_poll)
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200617 return hdev->ops->start_poll(hdev, im_protocols, tm_protocols);
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +0200618 else
Eric Lapuyade03bed292012-05-07 12:31:31 +0200619 return nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
Szymon Janc0f450772012-10-17 15:23:39 +0200620 NFC_HCI_EVT_READER_REQUESTED,
621 NULL, 0);
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +0200622}
623
624static void hci_stop_poll(struct nfc_dev *nfc_dev)
625{
626 struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
627
Christophe Ricard95f76872014-05-20 22:21:57 +0200628 if (hdev->ops->stop_poll)
629 hdev->ops->stop_poll(hdev);
630 else
631 nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
632 NFC_HCI_EVT_END_OPERATION, NULL, 0);
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +0200633}
634
Arron Wangc40d1742012-09-27 17:32:57 +0800635static int hci_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
636 __u8 comm_mode, __u8 *gb, size_t gb_len)
637{
638 struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
639
Samuel Ortiza3952982013-05-25 01:21:21 +0200640 if (!hdev->ops->dep_link_up)
641 return 0;
Arron Wangc40d1742012-09-27 17:32:57 +0800642
Samuel Ortiza3952982013-05-25 01:21:21 +0200643 return hdev->ops->dep_link_up(hdev, target, comm_mode,
644 gb, gb_len);
Arron Wangc40d1742012-09-27 17:32:57 +0800645}
646
647static int hci_dep_link_down(struct nfc_dev *nfc_dev)
648{
649 struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
650
Samuel Ortiza3952982013-05-25 01:21:21 +0200651 if (!hdev->ops->dep_link_down)
652 return 0;
Arron Wangc40d1742012-09-27 17:32:57 +0800653
Samuel Ortiza3952982013-05-25 01:21:21 +0200654 return hdev->ops->dep_link_down(hdev);
Arron Wangc40d1742012-09-27 17:32:57 +0800655}
656
Eric Lapuyade90099432012-05-07 12:31:13 +0200657static int hci_activate_target(struct nfc_dev *nfc_dev,
658 struct nfc_target *target, u32 protocol)
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +0200659{
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +0200660 return 0;
661}
662
Eric Lapuyade90099432012-05-07 12:31:13 +0200663static void hci_deactivate_target(struct nfc_dev *nfc_dev,
664 struct nfc_target *target)
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +0200665{
666}
667
Eric Lapuyadef3e8fb52012-09-11 10:43:50 +0200668#define HCI_CB_TYPE_TRANSCEIVE 1
669
670static void hci_transceive_cb(void *context, struct sk_buff *skb, int err)
671{
672 struct nfc_hci_dev *hdev = context;
673
674 switch (hdev->async_cb_type) {
675 case HCI_CB_TYPE_TRANSCEIVE:
676 /*
677 * TODO: Check RF Error indicator to make sure data is valid.
678 * It seems that HCI cmd can complete without error, but data
679 * can be invalid if an RF error occured? Ignore for now.
680 */
681 if (err == 0)
682 skb_trim(skb, skb->len - 1); /* RF Err ind */
683
684 hdev->async_cb(hdev->async_cb_context, skb, err);
685 break;
686 default:
687 if (err == 0)
688 kfree_skb(skb);
689 break;
690 }
691}
692
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +0200693static int hci_transceive(struct nfc_dev *nfc_dev, struct nfc_target *target,
694 struct sk_buff *skb, data_exchange_cb_t cb,
695 void *cb_context)
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +0200696{
697 struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
698 int r;
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +0200699
Eric Lapuyade90099432012-05-07 12:31:13 +0200700 pr_debug("target_idx=%d\n", target->idx);
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +0200701
702 switch (target->hci_reader_gate) {
703 case NFC_HCI_RF_READER_A_GATE:
704 case NFC_HCI_RF_READER_B_GATE:
Arron Wange8107622012-09-27 17:32:58 +0800705 if (hdev->ops->im_transceive) {
706 r = hdev->ops->im_transceive(hdev, target, skb, cb,
Eric Lapuyadef3e8fb52012-09-11 10:43:50 +0200707 cb_context);
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +0200708 if (r <= 0) /* handled */
709 break;
710 }
711
712 *skb_push(skb, 1) = 0; /* CTR, see spec:10.2.2.1 */
Eric Lapuyadef3e8fb52012-09-11 10:43:50 +0200713
714 hdev->async_cb_type = HCI_CB_TYPE_TRANSCEIVE;
715 hdev->async_cb = cb;
716 hdev->async_cb_context = cb_context;
717
718 r = nfc_hci_send_cmd_async(hdev, target->hci_reader_gate,
719 NFC_HCI_WR_XCHG_DATA, skb->data,
720 skb->len, hci_transceive_cb, hdev);
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +0200721 break;
722 default:
Arron Wange8107622012-09-27 17:32:58 +0800723 if (hdev->ops->im_transceive) {
724 r = hdev->ops->im_transceive(hdev, target, skb, cb,
Eric Lapuyadef3e8fb52012-09-11 10:43:50 +0200725 cb_context);
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +0200726 if (r == 1)
727 r = -ENOTSUPP;
Szymon Janc0f450772012-10-17 15:23:39 +0200728 } else {
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +0200729 r = -ENOTSUPP;
Szymon Janc0f450772012-10-17 15:23:39 +0200730 }
Eric Lapuyadef3e8fb52012-09-11 10:43:50 +0200731 break;
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +0200732 }
733
734 kfree_skb(skb);
735
Eric Lapuyadef3e8fb52012-09-11 10:43:50 +0200736 return r;
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +0200737}
738
Arron Wang984d3342012-10-08 14:54:39 +0800739static int hci_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
Arron Wange8107622012-09-27 17:32:58 +0800740{
741 struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
742
Samuel Ortiza3952982013-05-25 01:21:21 +0200743 if (!hdev->ops->tm_send) {
744 kfree_skb(skb);
745 return -ENOTSUPP;
746 }
Eric Lapuyade924d4a02012-12-04 16:44:25 +0100747
Samuel Ortiza3952982013-05-25 01:21:21 +0200748 return hdev->ops->tm_send(hdev, skb);
Arron Wange8107622012-09-27 17:32:58 +0800749}
750
Eric Lapuyade1676f752012-05-07 12:31:16 +0200751static int hci_check_presence(struct nfc_dev *nfc_dev,
752 struct nfc_target *target)
753{
754 struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
755
Samuel Ortiza3952982013-05-25 01:21:21 +0200756 if (!hdev->ops->check_presence)
757 return 0;
Eric Lapuyade1676f752012-05-07 12:31:16 +0200758
Samuel Ortiza3952982013-05-25 01:21:21 +0200759 return hdev->ops->check_presence(hdev, target);
Eric Lapuyade1676f752012-05-07 12:31:16 +0200760}
761
Samuel Ortiz0a946302013-05-10 11:57:06 +0200762static int hci_discover_se(struct nfc_dev *nfc_dev)
763{
764 struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
765
766 if (hdev->ops->discover_se)
767 return hdev->ops->discover_se(hdev);
768
769 return 0;
770}
771
772static int hci_enable_se(struct nfc_dev *nfc_dev, u32 se_idx)
773{
774 struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
775
776 if (hdev->ops->enable_se)
777 return hdev->ops->enable_se(hdev, se_idx);
778
779 return 0;
780}
781
782static int hci_disable_se(struct nfc_dev *nfc_dev, u32 se_idx)
783{
784 struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
785
786 if (hdev->ops->disable_se)
Dan Carpenter4eba11e2013-06-18 01:48:26 +0300787 return hdev->ops->disable_se(hdev, se_idx);
Samuel Ortiz0a946302013-05-10 11:57:06 +0200788
789 return 0;
790}
791
Christophe Ricard9b8d32b2014-11-13 00:30:34 +0100792static int hci_se_io(struct nfc_dev *nfc_dev, u32 se_idx,
793 u8 *apdu, size_t apdu_length,
794 se_io_cb_t cb, void *cb_context)
795{
796 struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
797
798 if (hdev->ops->se_io)
799 return hdev->ops->se_io(hdev, se_idx, apdu,
800 apdu_length, cb, cb_context);
801
802 return 0;
803}
804
Eric Lapuyade72b06f72012-06-11 13:36:52 +0200805static void nfc_hci_failure(struct nfc_hci_dev *hdev, int err)
Eric Lapuyadea9a741a2012-04-30 18:21:51 +0200806{
Eric Lapuyadea070c852012-06-11 15:06:56 +0200807 mutex_lock(&hdev->msg_tx_mutex);
808
809 if (hdev->cmd_pending_msg == NULL) {
810 nfc_driver_failure(hdev->ndev, err);
811 goto exit;
812 }
813
814 __nfc_hci_cmd_completion(hdev, err, NULL);
815
816exit:
817 mutex_unlock(&hdev->msg_tx_mutex);
Eric Lapuyadea9a741a2012-04-30 18:21:51 +0200818}
Eric Lapuyade72b06f72012-06-11 13:36:52 +0200819
Eric Lapuyade412fda52012-09-18 19:45:48 +0200820static void nfc_hci_llc_failure(struct nfc_hci_dev *hdev, int err)
Eric Lapuyade72b06f72012-06-11 13:36:52 +0200821{
822 nfc_hci_failure(hdev, err);
823}
Eric Lapuyadea9a741a2012-04-30 18:21:51 +0200824
Eric Lapuyade412fda52012-09-18 19:45:48 +0200825static void nfc_hci_recv_from_llc(struct nfc_hci_dev *hdev, struct sk_buff *skb)
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +0200826{
827 struct hcp_packet *packet;
828 u8 type;
829 u8 instruction;
830 struct sk_buff *hcp_skb;
831 u8 pipe;
832 struct sk_buff *frag_skb;
833 int msg_len;
834
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +0200835 packet = (struct hcp_packet *)skb->data;
836 if ((packet->header & ~NFC_HCI_FRAGMENT) == 0) {
837 skb_queue_tail(&hdev->rx_hcp_frags, skb);
838 return;
839 }
840
841 /* it's the last fragment. Does it need re-aggregation? */
842 if (skb_queue_len(&hdev->rx_hcp_frags)) {
843 pipe = packet->header & NFC_HCI_FRAGMENT;
844 skb_queue_tail(&hdev->rx_hcp_frags, skb);
845
846 msg_len = 0;
847 skb_queue_walk(&hdev->rx_hcp_frags, frag_skb) {
848 msg_len += (frag_skb->len -
849 NFC_HCI_HCP_PACKET_HEADER_LEN);
850 }
851
852 hcp_skb = nfc_alloc_recv_skb(NFC_HCI_HCP_PACKET_HEADER_LEN +
853 msg_len, GFP_KERNEL);
854 if (hcp_skb == NULL) {
Eric Lapuyade72b06f72012-06-11 13:36:52 +0200855 nfc_hci_failure(hdev, -ENOMEM);
856 return;
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +0200857 }
858
859 *skb_put(hcp_skb, NFC_HCI_HCP_PACKET_HEADER_LEN) = pipe;
860
861 skb_queue_walk(&hdev->rx_hcp_frags, frag_skb) {
862 msg_len = frag_skb->len - NFC_HCI_HCP_PACKET_HEADER_LEN;
863 memcpy(skb_put(hcp_skb, msg_len),
864 frag_skb->data + NFC_HCI_HCP_PACKET_HEADER_LEN,
865 msg_len);
866 }
867
868 skb_queue_purge(&hdev->rx_hcp_frags);
869 } else {
870 packet->header &= NFC_HCI_FRAGMENT;
871 hcp_skb = skb;
872 }
873
874 /* if this is a response, dispatch immediately to
875 * unblock waiting cmd context. Otherwise, enqueue to dispatch
876 * in separate context where handler can also execute command.
877 */
878 packet = (struct hcp_packet *)hcp_skb->data;
879 type = HCP_MSG_GET_TYPE(packet->message.header);
880 if (type == NFC_HCI_HCP_RESPONSE) {
881 pipe = packet->header;
882 instruction = HCP_MSG_GET_CMD(packet->message.header);
883 skb_pull(hcp_skb, NFC_HCI_HCP_PACKET_HEADER_LEN +
884 NFC_HCI_HCP_MESSAGE_HEADER_LEN);
885 nfc_hci_hcp_message_rx(hdev, pipe, type, instruction, hcp_skb);
886 } else {
887 skb_queue_tail(&hdev->msg_rx_queue, hcp_skb);
Linus Torvalds916082b2012-10-02 16:01:31 -0700888 schedule_work(&hdev->msg_rx_work);
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +0200889 }
890}
Eric Lapuyade412fda52012-09-18 19:45:48 +0200891
Samuel Ortiz9ea71872013-07-31 01:19:43 +0200892static int hci_fw_download(struct nfc_dev *nfc_dev, const char *firmware_name)
Eric Lapuyade9a695d22013-04-29 17:47:42 +0200893{
894 struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
895
Samuel Ortiz9ea71872013-07-31 01:19:43 +0200896 if (!hdev->ops->fw_download)
Samuel Ortiza3952982013-05-25 01:21:21 +0200897 return -ENOTSUPP;
Eric Lapuyade9a695d22013-04-29 17:47:42 +0200898
Samuel Ortiz9ea71872013-07-31 01:19:43 +0200899 return hdev->ops->fw_download(hdev, firmware_name);
Eric Lapuyade9a695d22013-04-29 17:47:42 +0200900}
901
Eric Lapuyade412fda52012-09-18 19:45:48 +0200902static struct nfc_ops hci_nfc_ops = {
903 .dev_up = hci_dev_up,
904 .dev_down = hci_dev_down,
905 .start_poll = hci_start_poll,
906 .stop_poll = hci_stop_poll,
Arron Wangc40d1742012-09-27 17:32:57 +0800907 .dep_link_up = hci_dep_link_up,
908 .dep_link_down = hci_dep_link_down,
Eric Lapuyade412fda52012-09-18 19:45:48 +0200909 .activate_target = hci_activate_target,
910 .deactivate_target = hci_deactivate_target,
911 .im_transceive = hci_transceive,
Arron Wange8107622012-09-27 17:32:58 +0800912 .tm_send = hci_tm_send,
Eric Lapuyade412fda52012-09-18 19:45:48 +0200913 .check_presence = hci_check_presence,
Samuel Ortiz9ea71872013-07-31 01:19:43 +0200914 .fw_download = hci_fw_download,
Samuel Ortiz0a946302013-05-10 11:57:06 +0200915 .discover_se = hci_discover_se,
916 .enable_se = hci_enable_se,
917 .disable_se = hci_disable_se,
Christophe Ricard9b8d32b2014-11-13 00:30:34 +0100918 .se_io = hci_se_io,
Eric Lapuyade412fda52012-09-18 19:45:48 +0200919};
920
921struct nfc_hci_dev *nfc_hci_allocate_device(struct nfc_hci_ops *ops,
922 struct nfc_hci_init_data *init_data,
Eric Lapuyadebf71ab82012-12-18 14:15:49 +0100923 unsigned long quirks,
Eric Lapuyade412fda52012-09-18 19:45:48 +0200924 u32 protocols,
925 const char *llc_name,
926 int tx_headroom,
927 int tx_tailroom,
928 int max_link_payload)
929{
930 struct nfc_hci_dev *hdev;
931
932 if (ops->xmit == NULL)
933 return NULL;
934
935 if (protocols == 0)
936 return NULL;
937
938 hdev = kzalloc(sizeof(struct nfc_hci_dev), GFP_KERNEL);
939 if (hdev == NULL)
940 return NULL;
941
942 hdev->llc = nfc_llc_allocate(llc_name, hdev, ops->xmit,
943 nfc_hci_recv_from_llc, tx_headroom,
944 tx_tailroom, nfc_hci_llc_failure);
945 if (hdev->llc == NULL) {
946 kfree(hdev);
947 return NULL;
948 }
949
Samuel Ortiz0b456c42013-05-07 19:22:11 +0200950 hdev->ndev = nfc_allocate_device(&hci_nfc_ops, protocols,
Eric Lapuyade412fda52012-09-18 19:45:48 +0200951 tx_headroom + HCI_CMDS_HEADROOM,
952 tx_tailroom);
953 if (!hdev->ndev) {
954 nfc_llc_free(hdev->llc);
955 kfree(hdev);
956 return NULL;
957 }
958
959 hdev->ops = ops;
960 hdev->max_data_link_payload = max_link_payload;
961 hdev->init_data = *init_data;
962
963 nfc_set_drvdata(hdev->ndev, hdev);
964
Christophe Ricard118278f2015-01-27 01:18:12 +0100965 nfc_hci_reset_pipes(hdev);
Eric Lapuyade412fda52012-09-18 19:45:48 +0200966
Eric Lapuyadebf71ab82012-12-18 14:15:49 +0100967 hdev->quirks = quirks;
968
Eric Lapuyade412fda52012-09-18 19:45:48 +0200969 return hdev;
970}
971EXPORT_SYMBOL(nfc_hci_allocate_device);
972
973void nfc_hci_free_device(struct nfc_hci_dev *hdev)
974{
975 nfc_free_device(hdev->ndev);
976 nfc_llc_free(hdev->llc);
977 kfree(hdev);
978}
979EXPORT_SYMBOL(nfc_hci_free_device);
980
981int nfc_hci_register_device(struct nfc_hci_dev *hdev)
982{
983 mutex_init(&hdev->msg_tx_mutex);
984
985 INIT_LIST_HEAD(&hdev->msg_tx_queue);
986
987 INIT_WORK(&hdev->msg_tx_work, nfc_hci_msg_tx_work);
988
989 init_timer(&hdev->cmd_timer);
990 hdev->cmd_timer.data = (unsigned long)hdev;
991 hdev->cmd_timer.function = nfc_hci_cmd_timeout;
992
993 skb_queue_head_init(&hdev->rx_hcp_frags);
994
995 INIT_WORK(&hdev->msg_rx_work, nfc_hci_msg_rx_work);
996
997 skb_queue_head_init(&hdev->msg_rx_queue);
998
999 return nfc_register_device(hdev->ndev);
1000}
1001EXPORT_SYMBOL(nfc_hci_register_device);
1002
1003void nfc_hci_unregister_device(struct nfc_hci_dev *hdev)
1004{
1005 struct hci_msg *msg, *n;
1006
Eric Lapuyadef0c91032012-11-26 18:06:27 +01001007 mutex_lock(&hdev->msg_tx_mutex);
1008
1009 if (hdev->cmd_pending_msg) {
1010 if (hdev->cmd_pending_msg->cb)
1011 hdev->cmd_pending_msg->cb(
1012 hdev->cmd_pending_msg->cb_context,
1013 NULL, -ESHUTDOWN);
1014 kfree(hdev->cmd_pending_msg);
1015 hdev->cmd_pending_msg = NULL;
1016 }
1017
1018 hdev->shutting_down = true;
1019
1020 mutex_unlock(&hdev->msg_tx_mutex);
1021
1022 del_timer_sync(&hdev->cmd_timer);
1023 cancel_work_sync(&hdev->msg_tx_work);
1024
1025 cancel_work_sync(&hdev->msg_rx_work);
1026
1027 nfc_unregister_device(hdev->ndev);
1028
Eric Lapuyade412fda52012-09-18 19:45:48 +02001029 skb_queue_purge(&hdev->rx_hcp_frags);
1030 skb_queue_purge(&hdev->msg_rx_queue);
1031
1032 list_for_each_entry_safe(msg, n, &hdev->msg_tx_queue, msg_l) {
1033 list_del(&msg->msg_l);
1034 skb_queue_purge(&msg->msg_frags);
1035 kfree(msg);
1036 }
Eric Lapuyade412fda52012-09-18 19:45:48 +02001037}
1038EXPORT_SYMBOL(nfc_hci_unregister_device);
1039
1040void nfc_hci_set_clientdata(struct nfc_hci_dev *hdev, void *clientdata)
1041{
1042 hdev->clientdata = clientdata;
1043}
1044EXPORT_SYMBOL(nfc_hci_set_clientdata);
1045
1046void *nfc_hci_get_clientdata(struct nfc_hci_dev *hdev)
1047{
1048 return hdev->clientdata;
1049}
1050EXPORT_SYMBOL(nfc_hci_get_clientdata);
1051
1052void nfc_hci_driver_failure(struct nfc_hci_dev *hdev, int err)
1053{
1054 nfc_hci_failure(hdev, err);
1055}
1056EXPORT_SYMBOL(nfc_hci_driver_failure);
1057
Szymon Janc0f450772012-10-17 15:23:39 +02001058void nfc_hci_recv_frame(struct nfc_hci_dev *hdev, struct sk_buff *skb)
Eric Lapuyade412fda52012-09-18 19:45:48 +02001059{
1060 nfc_llc_rcv_from_drv(hdev->llc, skb);
1061}
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +02001062EXPORT_SYMBOL(nfc_hci_recv_frame);
1063
Eric Lapuyade67cccfe2012-09-13 17:10:00 +02001064static int __init nfc_hci_init(void)
1065{
1066 return nfc_llc_init();
1067}
1068
1069static void __exit nfc_hci_exit(void)
1070{
1071 nfc_llc_exit();
1072}
1073
Eric Lapuyade412fda52012-09-18 19:45:48 +02001074subsys_initcall(nfc_hci_init);
Eric Lapuyade67cccfe2012-09-13 17:10:00 +02001075module_exit(nfc_hci_exit);
1076
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +02001077MODULE_LICENSE("GPL");
Eric Lapuyade80faa592012-09-18 19:25:38 +02001078MODULE_DESCRIPTION("NFC HCI Core");