blob: 08b29b55ea63acdff30d2ba29c1cda51a52f7cb8 [file] [log] [blame]
Thierry Escande4b10884eb2013-09-19 17:55:25 +02001/*
2 * NFC Digital Protocol stack
3 * Copyright (c) 2013, Intel Corporation.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 */
15
16#ifndef __DIGITAL_H
17#define __DIGITAL_H
18
19#include <net/nfc/nfc.h>
20#include <net/nfc/digital.h>
21
Thierry Escande2c66dae2013-09-19 17:55:27 +020022#include <linux/crc-ccitt.h>
Thierry Escande8c0695e42013-09-19 17:55:28 +020023#include <linux/crc-itu-t.h>
Thierry Escande2c66dae2013-09-19 17:55:27 +020024
Samuel Ortizc5da0e42013-09-20 09:05:48 +020025#define PROTOCOL_ERR(req) pr_err("%d: NFC Digital Protocol error: %s\n", \
26 __LINE__, req)
Thierry Escande4b10884eb2013-09-19 17:55:25 +020027
Thierry Escande59ee2362013-09-19 17:55:26 +020028#define DIGITAL_CMD_IN_SEND 0
29#define DIGITAL_CMD_TG_SEND 1
30#define DIGITAL_CMD_TG_LISTEN 2
31#define DIGITAL_CMD_TG_LISTEN_MDAA 3
32
33#define DIGITAL_MAX_HEADER_LEN 7
34#define DIGITAL_CRC_LEN 2
35
Thierry Escande7d0911c2013-09-19 17:55:29 +020036#define DIGITAL_SENSF_NFCID2_NFC_DEP_B1 0x01
37#define DIGITAL_SENSF_NFCID2_NFC_DEP_B2 0xFE
38
39#define DIGITAL_SENS_RES_NFC_DEP 0x0100
40#define DIGITAL_SEL_RES_NFC_DEP 0x40
41#define DIGITAL_SENSF_FELICA_SC 0xFFFF
42
Thierry Escande2c66dae2013-09-19 17:55:27 +020043#define DIGITAL_DRV_CAPS_IN_CRC(ddev) \
44 ((ddev)->driver_capabilities & NFC_DIGITAL_DRV_CAPS_IN_CRC)
45#define DIGITAL_DRV_CAPS_TG_CRC(ddev) \
46 ((ddev)->driver_capabilities & NFC_DIGITAL_DRV_CAPS_TG_CRC)
47
48struct digital_data_exch {
49 data_exchange_cb_t cb;
50 void *cb_context;
51};
52
Thierry Escande59ee2362013-09-19 17:55:26 +020053struct sk_buff *digital_skb_alloc(struct nfc_digital_dev *ddev,
54 unsigned int len);
55
56int digital_send_cmd(struct nfc_digital_dev *ddev, u8 cmd_type,
Thierry Escande1c7a4c22013-09-19 17:55:30 +020057 struct sk_buff *skb, struct digital_tg_mdaa_params *params,
58 u16 timeout, nfc_digital_cmd_complete_t cmd_cb,
59 void *cb_context);
Thierry Escande59ee2362013-09-19 17:55:26 +020060
61int digital_in_configure_hw(struct nfc_digital_dev *ddev, int type, int param);
62static inline int digital_in_send_cmd(struct nfc_digital_dev *ddev,
63 struct sk_buff *skb, u16 timeout,
64 nfc_digital_cmd_complete_t cmd_cb,
65 void *cb_context)
66{
Thierry Escande1c7a4c22013-09-19 17:55:30 +020067 return digital_send_cmd(ddev, DIGITAL_CMD_IN_SEND, skb, NULL, timeout,
68 cmd_cb, cb_context);
Thierry Escande59ee2362013-09-19 17:55:26 +020069}
70
71void digital_poll_next_tech(struct nfc_digital_dev *ddev);
72
73int digital_in_send_sens_req(struct nfc_digital_dev *ddev, u8 rf_tech);
Thierry Escande8c0695e42013-09-19 17:55:28 +020074int digital_in_send_sensf_req(struct nfc_digital_dev *ddev, u8 rf_tech);
Thierry Escande59ee2362013-09-19 17:55:26 +020075
Thierry Escande2c66dae2013-09-19 17:55:27 +020076int digital_target_found(struct nfc_digital_dev *ddev,
77 struct nfc_target *target, u8 protocol);
78
79int digital_in_recv_mifare_res(struct sk_buff *resp);
80
Thierry Escande7d0911c2013-09-19 17:55:29 +020081int digital_in_send_atr_req(struct nfc_digital_dev *ddev,
82 struct nfc_target *target, __u8 comm_mode, __u8 *gb,
83 size_t gb_len);
84int digital_in_send_dep_req(struct nfc_digital_dev *ddev,
85 struct nfc_target *target, struct sk_buff *skb,
86 struct digital_data_exch *data_exch);
87
Thierry Escande1c7a4c22013-09-19 17:55:30 +020088int digital_tg_configure_hw(struct nfc_digital_dev *ddev, int type, int param);
89static inline int digital_tg_send_cmd(struct nfc_digital_dev *ddev,
90 struct sk_buff *skb, u16 timeout,
91 nfc_digital_cmd_complete_t cmd_cb, void *cb_context)
92{
93 return digital_send_cmd(ddev, DIGITAL_CMD_TG_SEND, skb, NULL, timeout,
94 cmd_cb, cb_context);
95}
96
97void digital_tg_recv_sens_req(struct nfc_digital_dev *ddev, void *arg,
98 struct sk_buff *resp);
99
100void digital_tg_recv_sensf_req(struct nfc_digital_dev *ddev, void *arg,
101 struct sk_buff *resp);
102
103static inline int digital_tg_listen(struct nfc_digital_dev *ddev, u16 timeout,
104 nfc_digital_cmd_complete_t cb, void *arg)
105{
106 return digital_send_cmd(ddev, DIGITAL_CMD_TG_LISTEN, NULL, NULL,
107 timeout, cb, arg);
108}
109
110void digital_tg_recv_atr_req(struct nfc_digital_dev *ddev, void *arg,
111 struct sk_buff *resp);
112
113int digital_tg_send_dep_res(struct nfc_digital_dev *ddev, struct sk_buff *skb);
114
115int digital_tg_listen_nfca(struct nfc_digital_dev *ddev, u8 rf_tech);
116int digital_tg_listen_nfcf(struct nfc_digital_dev *ddev, u8 rf_tech);
117
Thierry Escande2c66dae2013-09-19 17:55:27 +0200118typedef u16 (*crc_func_t)(u16, const u8 *, size_t);
119
120#define CRC_A_INIT 0x6363
121#define CRC_B_INIT 0xFFFF
Thierry Escande8c0695e42013-09-19 17:55:28 +0200122#define CRC_F_INIT 0x0000
Thierry Escande2c66dae2013-09-19 17:55:27 +0200123
124void digital_skb_add_crc(struct sk_buff *skb, crc_func_t crc_func, u16 init,
125 u8 bitwise_inv, u8 msb_first);
126
127static inline void digital_skb_add_crc_a(struct sk_buff *skb)
128{
129 digital_skb_add_crc(skb, crc_ccitt, CRC_A_INIT, 0, 0);
130}
131
132static inline void digital_skb_add_crc_b(struct sk_buff *skb)
133{
134 digital_skb_add_crc(skb, crc_ccitt, CRC_B_INIT, 1, 0);
135}
136
Thierry Escande8c0695e42013-09-19 17:55:28 +0200137static inline void digital_skb_add_crc_f(struct sk_buff *skb)
138{
139 digital_skb_add_crc(skb, crc_itu_t, CRC_F_INIT, 0, 1);
140}
141
Thierry Escande2c66dae2013-09-19 17:55:27 +0200142static inline void digital_skb_add_crc_none(struct sk_buff *skb)
143{
144 return;
145}
146
147int digital_skb_check_crc(struct sk_buff *skb, crc_func_t crc_func,
148 u16 crc_init, u8 bitwise_inv, u8 msb_first);
149
150static inline int digital_skb_check_crc_a(struct sk_buff *skb)
151{
152 return digital_skb_check_crc(skb, crc_ccitt, CRC_A_INIT, 0, 0);
153}
154
155static inline int digital_skb_check_crc_b(struct sk_buff *skb)
156{
157 return digital_skb_check_crc(skb, crc_ccitt, CRC_B_INIT, 1, 0);
158}
159
Thierry Escande8c0695e42013-09-19 17:55:28 +0200160static inline int digital_skb_check_crc_f(struct sk_buff *skb)
161{
162 return digital_skb_check_crc(skb, crc_itu_t, CRC_F_INIT, 0, 1);
163}
164
Thierry Escande2c66dae2013-09-19 17:55:27 +0200165static inline int digital_skb_check_crc_none(struct sk_buff *skb)
166{
167 return 0;
168}
169
Thierry Escande4b10884eb2013-09-19 17:55:25 +0200170#endif /* __DIGITAL_H */