blob: 586075a3feedfe49e7a5c5d42d257c066239aa78 [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
Thierry Escande4b10884eb2013-09-19 17:55:25 +020025#define PR_DBG(fmt, ...) pr_debug("%s: " fmt "\n", __func__, ##__VA_ARGS__)
26#define PR_ERR(fmt, ...) pr_err("%s: " fmt "\n", __func__, ##__VA_ARGS__)
27#define PROTOCOL_ERR(req) pr_err("%s:%d: NFC Digital Protocol error: %s\n", \
28 __func__, __LINE__, req)
29
Thierry Escande59ee2362013-09-19 17:55:26 +020030#define DIGITAL_CMD_IN_SEND 0
31#define DIGITAL_CMD_TG_SEND 1
32#define DIGITAL_CMD_TG_LISTEN 2
33#define DIGITAL_CMD_TG_LISTEN_MDAA 3
34
35#define DIGITAL_MAX_HEADER_LEN 7
36#define DIGITAL_CRC_LEN 2
37
Thierry Escande7d0911c2013-09-19 17:55:29 +020038#define DIGITAL_SENSF_NFCID2_NFC_DEP_B1 0x01
39#define DIGITAL_SENSF_NFCID2_NFC_DEP_B2 0xFE
40
41#define DIGITAL_SENS_RES_NFC_DEP 0x0100
42#define DIGITAL_SEL_RES_NFC_DEP 0x40
43#define DIGITAL_SENSF_FELICA_SC 0xFFFF
44
Thierry Escande2c66dae2013-09-19 17:55:27 +020045#define DIGITAL_DRV_CAPS_IN_CRC(ddev) \
46 ((ddev)->driver_capabilities & NFC_DIGITAL_DRV_CAPS_IN_CRC)
47#define DIGITAL_DRV_CAPS_TG_CRC(ddev) \
48 ((ddev)->driver_capabilities & NFC_DIGITAL_DRV_CAPS_TG_CRC)
49
50struct digital_data_exch {
51 data_exchange_cb_t cb;
52 void *cb_context;
53};
54
Thierry Escande59ee2362013-09-19 17:55:26 +020055struct sk_buff *digital_skb_alloc(struct nfc_digital_dev *ddev,
56 unsigned int len);
57
58int digital_send_cmd(struct nfc_digital_dev *ddev, u8 cmd_type,
Thierry Escande1c7a4c22013-09-19 17:55:30 +020059 struct sk_buff *skb, struct digital_tg_mdaa_params *params,
60 u16 timeout, nfc_digital_cmd_complete_t cmd_cb,
61 void *cb_context);
Thierry Escande59ee2362013-09-19 17:55:26 +020062
63int digital_in_configure_hw(struct nfc_digital_dev *ddev, int type, int param);
64static inline int digital_in_send_cmd(struct nfc_digital_dev *ddev,
65 struct sk_buff *skb, u16 timeout,
66 nfc_digital_cmd_complete_t cmd_cb,
67 void *cb_context)
68{
Thierry Escande1c7a4c22013-09-19 17:55:30 +020069 return digital_send_cmd(ddev, DIGITAL_CMD_IN_SEND, skb, NULL, timeout,
70 cmd_cb, cb_context);
Thierry Escande59ee2362013-09-19 17:55:26 +020071}
72
73void digital_poll_next_tech(struct nfc_digital_dev *ddev);
74
75int digital_in_send_sens_req(struct nfc_digital_dev *ddev, u8 rf_tech);
Thierry Escande8c0695e42013-09-19 17:55:28 +020076int digital_in_send_sensf_req(struct nfc_digital_dev *ddev, u8 rf_tech);
Thierry Escande59ee2362013-09-19 17:55:26 +020077
Thierry Escande2c66dae2013-09-19 17:55:27 +020078int digital_target_found(struct nfc_digital_dev *ddev,
79 struct nfc_target *target, u8 protocol);
80
81int digital_in_recv_mifare_res(struct sk_buff *resp);
82
Thierry Escande7d0911c2013-09-19 17:55:29 +020083int digital_in_send_atr_req(struct nfc_digital_dev *ddev,
84 struct nfc_target *target, __u8 comm_mode, __u8 *gb,
85 size_t gb_len);
86int digital_in_send_dep_req(struct nfc_digital_dev *ddev,
87 struct nfc_target *target, struct sk_buff *skb,
88 struct digital_data_exch *data_exch);
89
Thierry Escande1c7a4c22013-09-19 17:55:30 +020090int digital_tg_configure_hw(struct nfc_digital_dev *ddev, int type, int param);
91static inline int digital_tg_send_cmd(struct nfc_digital_dev *ddev,
92 struct sk_buff *skb, u16 timeout,
93 nfc_digital_cmd_complete_t cmd_cb, void *cb_context)
94{
95 return digital_send_cmd(ddev, DIGITAL_CMD_TG_SEND, skb, NULL, timeout,
96 cmd_cb, cb_context);
97}
98
99void digital_tg_recv_sens_req(struct nfc_digital_dev *ddev, void *arg,
100 struct sk_buff *resp);
101
102void digital_tg_recv_sensf_req(struct nfc_digital_dev *ddev, void *arg,
103 struct sk_buff *resp);
104
105static inline int digital_tg_listen(struct nfc_digital_dev *ddev, u16 timeout,
106 nfc_digital_cmd_complete_t cb, void *arg)
107{
108 return digital_send_cmd(ddev, DIGITAL_CMD_TG_LISTEN, NULL, NULL,
109 timeout, cb, arg);
110}
111
112void digital_tg_recv_atr_req(struct nfc_digital_dev *ddev, void *arg,
113 struct sk_buff *resp);
114
115int digital_tg_send_dep_res(struct nfc_digital_dev *ddev, struct sk_buff *skb);
116
117int digital_tg_listen_nfca(struct nfc_digital_dev *ddev, u8 rf_tech);
118int digital_tg_listen_nfcf(struct nfc_digital_dev *ddev, u8 rf_tech);
119
Thierry Escande2c66dae2013-09-19 17:55:27 +0200120typedef u16 (*crc_func_t)(u16, const u8 *, size_t);
121
122#define CRC_A_INIT 0x6363
123#define CRC_B_INIT 0xFFFF
Thierry Escande8c0695e42013-09-19 17:55:28 +0200124#define CRC_F_INIT 0x0000
Thierry Escande2c66dae2013-09-19 17:55:27 +0200125
126void digital_skb_add_crc(struct sk_buff *skb, crc_func_t crc_func, u16 init,
127 u8 bitwise_inv, u8 msb_first);
128
129static inline void digital_skb_add_crc_a(struct sk_buff *skb)
130{
131 digital_skb_add_crc(skb, crc_ccitt, CRC_A_INIT, 0, 0);
132}
133
134static inline void digital_skb_add_crc_b(struct sk_buff *skb)
135{
136 digital_skb_add_crc(skb, crc_ccitt, CRC_B_INIT, 1, 0);
137}
138
Thierry Escande8c0695e42013-09-19 17:55:28 +0200139static inline void digital_skb_add_crc_f(struct sk_buff *skb)
140{
141 digital_skb_add_crc(skb, crc_itu_t, CRC_F_INIT, 0, 1);
142}
143
Thierry Escande2c66dae2013-09-19 17:55:27 +0200144static inline void digital_skb_add_crc_none(struct sk_buff *skb)
145{
146 return;
147}
148
149int digital_skb_check_crc(struct sk_buff *skb, crc_func_t crc_func,
150 u16 crc_init, u8 bitwise_inv, u8 msb_first);
151
152static inline int digital_skb_check_crc_a(struct sk_buff *skb)
153{
154 return digital_skb_check_crc(skb, crc_ccitt, CRC_A_INIT, 0, 0);
155}
156
157static inline int digital_skb_check_crc_b(struct sk_buff *skb)
158{
159 return digital_skb_check_crc(skb, crc_ccitt, CRC_B_INIT, 1, 0);
160}
161
Thierry Escande8c0695e42013-09-19 17:55:28 +0200162static inline int digital_skb_check_crc_f(struct sk_buff *skb)
163{
164 return digital_skb_check_crc(skb, crc_itu_t, CRC_F_INIT, 0, 1);
165}
166
Thierry Escande2c66dae2013-09-19 17:55:27 +0200167static inline int digital_skb_check_crc_none(struct sk_buff *skb)
168{
169 return 0;
170}
171
Thierry Escande4b10884eb2013-09-19 17:55:25 +0200172#endif /* __DIGITAL_H */