aboutsummaryrefslogtreecommitdiffstats
path: root/net
diff options
context:
space:
mode:
authorThierry Escande <thierry.escande@linux.intel.com>2013-09-19 11:55:26 -0400
committerSamuel Ortiz <sameo@linux.intel.com>2013-09-24 20:02:07 -0400
commit59ee2361c9248f07846f7a6e585768dcce18fb16 (patch)
tree05cb3823442a74481dbec0a7e34e002c12dff53e /net
parent4b10884eb428c243ae2070a539612e645f3d9b93 (diff)
NFC Digital: Implement driver commands mechanism
This implements the mechanism used to send commands to the driver in initiator mode through in_send_cmd(). Commands are serialized and sent to the driver by using a work item on the system workqueue. Responses are handled asynchronously by another work item. Once the digital stack receives the response through the command_complete callback, the next command is sent to the driver. This also implements the polling mechanism. It's handled by a work item cycling on all supported protocols. The start poll command for a given protocol is sent to the driver using the mechanism described above. The process continues until a peer is discovered or stop_poll is called. This patch implements the poll function for NFC-A that sends a SENS_REQ command and waits for the SENS_RES response. Signed-off-by: Thierry Escande <thierry.escande@linux.intel.com> Signed-off-by: Samuel Ortiz <sameo@linux.intel.com>
Diffstat (limited to 'net')
-rw-r--r--net/nfc/Makefile2
-rw-r--r--net/nfc/digital.h29
-rw-r--r--net/nfc/digital_core.c340
-rw-r--r--net/nfc/digital_technology.c64
4 files changed, 427 insertions, 8 deletions
diff --git a/net/nfc/Makefile b/net/nfc/Makefile
index 8e0cabd85e99..2db39713a907 100644
--- a/net/nfc/Makefile
+++ b/net/nfc/Makefile
@@ -10,4 +10,4 @@ obj-$(CONFIG_NFC_DIGITAL) += nfc_digital.o
10nfc-objs := core.o netlink.o af_nfc.o rawsock.o llcp_core.o llcp_commands.o \ 10nfc-objs := core.o netlink.o af_nfc.o rawsock.o llcp_core.o llcp_commands.o \
11 llcp_sock.o 11 llcp_sock.o
12 12
13nfc_digital-objs := digital_core.o 13nfc_digital-objs := digital_core.o digital_technology.o
diff --git a/net/nfc/digital.h b/net/nfc/digital.h
index 8d91ed820912..0a2767098daa 100644
--- a/net/nfc/digital.h
+++ b/net/nfc/digital.h
@@ -24,4 +24,33 @@
24#define PROTOCOL_ERR(req) pr_err("%s:%d: NFC Digital Protocol error: %s\n", \ 24#define PROTOCOL_ERR(req) pr_err("%s:%d: NFC Digital Protocol error: %s\n", \
25 __func__, __LINE__, req) 25 __func__, __LINE__, req)
26 26
27#define DIGITAL_CMD_IN_SEND 0
28#define DIGITAL_CMD_TG_SEND 1
29#define DIGITAL_CMD_TG_LISTEN 2
30#define DIGITAL_CMD_TG_LISTEN_MDAA 3
31
32#define DIGITAL_MAX_HEADER_LEN 7
33#define DIGITAL_CRC_LEN 2
34
35struct sk_buff *digital_skb_alloc(struct nfc_digital_dev *ddev,
36 unsigned int len);
37
38int digital_send_cmd(struct nfc_digital_dev *ddev, u8 cmd_type,
39 struct sk_buff *skb, u16 timeout,
40 nfc_digital_cmd_complete_t cmd_cb, void *cb_context);
41
42int digital_in_configure_hw(struct nfc_digital_dev *ddev, int type, int param);
43static inline int digital_in_send_cmd(struct nfc_digital_dev *ddev,
44 struct sk_buff *skb, u16 timeout,
45 nfc_digital_cmd_complete_t cmd_cb,
46 void *cb_context)
47{
48 return digital_send_cmd(ddev, DIGITAL_CMD_IN_SEND, skb, timeout, cmd_cb,
49 cb_context);
50}
51
52void digital_poll_next_tech(struct nfc_digital_dev *ddev);
53
54int digital_in_send_sens_req(struct nfc_digital_dev *ddev, u8 rf_tech);
55
27#endif /* __DIGITAL_H */ 56#endif /* __DIGITAL_H */
diff --git a/net/nfc/digital_core.c b/net/nfc/digital_core.c
index 471188a0d2e0..13abd293ca37 100644
--- a/net/nfc/digital_core.c
+++ b/net/nfc/digital_core.c
@@ -17,24 +17,319 @@
17 17
18#include "digital.h" 18#include "digital.h"
19 19
20#define DIGITAL_PROTO_NFCA_RF_TECH \
21 (NFC_PROTO_JEWEL_MASK | NFC_PROTO_MIFARE_MASK)
22
23struct digital_cmd {
24 struct list_head queue;
25
26 u8 type;
27 u8 pending;
28
29 u16 timeout;
30 struct sk_buff *req;
31 struct sk_buff *resp;
32
33 nfc_digital_cmd_complete_t cmd_cb;
34 void *cb_context;
35};
36
37struct sk_buff *digital_skb_alloc(struct nfc_digital_dev *ddev,
38 unsigned int len)
39{
40 struct sk_buff *skb;
41
42 skb = alloc_skb(len + ddev->tx_headroom + ddev->tx_tailroom,
43 GFP_KERNEL);
44 if (skb)
45 skb_reserve(skb, ddev->tx_headroom);
46
47 return skb;
48}
49
50static inline void digital_switch_rf(struct nfc_digital_dev *ddev, bool on)
51{
52 ddev->ops->switch_rf(ddev, on);
53}
54
55static inline void digital_abort_cmd(struct nfc_digital_dev *ddev)
56{
57 ddev->ops->abort_cmd(ddev);
58}
59
60static void digital_wq_cmd_complete(struct work_struct *work)
61{
62 struct digital_cmd *cmd;
63 struct nfc_digital_dev *ddev = container_of(work,
64 struct nfc_digital_dev,
65 cmd_complete_work);
66
67 mutex_lock(&ddev->cmd_lock);
68
69 cmd = list_first_entry_or_null(&ddev->cmd_queue, struct digital_cmd,
70 queue);
71 if (!cmd) {
72 mutex_unlock(&ddev->cmd_lock);
73 return;
74 }
75
76 list_del(&cmd->queue);
77
78 mutex_unlock(&ddev->cmd_lock);
79
80 if (!IS_ERR(cmd->resp))
81 print_hex_dump_debug("DIGITAL RX: ", DUMP_PREFIX_NONE, 16, 1,
82 cmd->resp->data, cmd->resp->len, false);
83
84 cmd->cmd_cb(ddev, cmd->cb_context, cmd->resp);
85
86 kfree(cmd);
87
88 schedule_work(&ddev->cmd_work);
89}
90
91static void digital_send_cmd_complete(struct nfc_digital_dev *ddev,
92 void *arg, struct sk_buff *resp)
93{
94 struct digital_cmd *cmd = arg;
95
96 cmd->resp = resp;
97
98 schedule_work(&ddev->cmd_complete_work);
99}
100
101static void digital_wq_cmd(struct work_struct *work)
102{
103 int rc;
104 struct digital_cmd *cmd;
105 struct nfc_digital_dev *ddev = container_of(work,
106 struct nfc_digital_dev,
107 cmd_work);
108
109 mutex_lock(&ddev->cmd_lock);
110
111 cmd = list_first_entry_or_null(&ddev->cmd_queue, struct digital_cmd,
112 queue);
113 if (!cmd || cmd->pending) {
114 mutex_unlock(&ddev->cmd_lock);
115 return;
116 }
117
118 mutex_unlock(&ddev->cmd_lock);
119
120 if (cmd->req)
121 print_hex_dump_debug("DIGITAL TX: ", DUMP_PREFIX_NONE, 16, 1,
122 cmd->req->data, cmd->req->len, false);
123
124 switch (cmd->type) {
125 case DIGITAL_CMD_IN_SEND:
126 rc = ddev->ops->in_send_cmd(ddev, cmd->req, cmd->timeout,
127 digital_send_cmd_complete, cmd);
128 break;
129 default:
130 PR_ERR("Unknown cmd type %d", cmd->type);
131 return;
132 }
133
134 if (!rc)
135 return;
136
137 PR_ERR("in_send_command returned err %d", rc);
138
139 mutex_lock(&ddev->cmd_lock);
140 list_del(&cmd->queue);
141 mutex_unlock(&ddev->cmd_lock);
142
143 kfree_skb(cmd->req);
144 kfree(cmd);
145
146 schedule_work(&ddev->cmd_work);
147}
148
149int digital_send_cmd(struct nfc_digital_dev *ddev, u8 cmd_type,
150 struct sk_buff *skb, u16 timeout,
151 nfc_digital_cmd_complete_t cmd_cb, void *cb_context)
152{
153 struct digital_cmd *cmd;
154
155 cmd = kzalloc(sizeof(struct digital_cmd), GFP_KERNEL);
156 if (!cmd)
157 return -ENOMEM;
158
159 cmd->type = cmd_type;
160 cmd->timeout = timeout;
161 cmd->req = skb;
162 cmd->cmd_cb = cmd_cb;
163 cmd->cb_context = cb_context;
164 INIT_LIST_HEAD(&cmd->queue);
165
166 mutex_lock(&ddev->cmd_lock);
167 list_add_tail(&cmd->queue, &ddev->cmd_queue);
168 mutex_unlock(&ddev->cmd_lock);
169
170 schedule_work(&ddev->cmd_work);
171
172 return 0;
173}
174
175int digital_in_configure_hw(struct nfc_digital_dev *ddev, int type, int param)
176{
177 int rc;
178
179 rc = ddev->ops->in_configure_hw(ddev, type, param);
180 if (rc)
181 PR_ERR("in_configure_hw failed: %d", rc);
182
183 return rc;
184}
185
186void digital_poll_next_tech(struct nfc_digital_dev *ddev)
187{
188 digital_switch_rf(ddev, 0);
189
190 mutex_lock(&ddev->poll_lock);
191
192 if (!ddev->poll_tech_count) {
193 mutex_unlock(&ddev->poll_lock);
194 return;
195 }
196
197 ddev->poll_tech_index = (ddev->poll_tech_index + 1) %
198 ddev->poll_tech_count;
199
200 mutex_unlock(&ddev->poll_lock);
201
202 schedule_work(&ddev->poll_work);
203}
204
205static void digital_wq_poll(struct work_struct *work)
206{
207 int rc;
208 struct digital_poll_tech *poll_tech;
209 struct nfc_digital_dev *ddev = container_of(work,
210 struct nfc_digital_dev,
211 poll_work);
212 mutex_lock(&ddev->poll_lock);
213
214 if (!ddev->poll_tech_count) {
215 mutex_unlock(&ddev->poll_lock);
216 return;
217 }
218
219 poll_tech = &ddev->poll_techs[ddev->poll_tech_index];
220
221 mutex_unlock(&ddev->poll_lock);
222
223 rc = poll_tech->poll_func(ddev, poll_tech->rf_tech);
224 if (rc)
225 digital_poll_next_tech(ddev);
226}
227
228static void digital_add_poll_tech(struct nfc_digital_dev *ddev, u8 rf_tech,
229 digital_poll_t poll_func)
230{
231 struct digital_poll_tech *poll_tech;
232
233 if (ddev->poll_tech_count >= NFC_DIGITAL_POLL_MODE_COUNT_MAX)
234 return;
235
236 poll_tech = &ddev->poll_techs[ddev->poll_tech_count++];
237
238 poll_tech->rf_tech = rf_tech;
239 poll_tech->poll_func = poll_func;
240}
241
242/**
243 * start_poll operation
244 *
245 * For every supported protocol, the corresponding polling function is added
246 * to the table of polling technologies (ddev->poll_techs[]) using
247 * digital_add_poll_tech().
248 * When a polling function fails (by timeout or protocol error) the next one is
249 * schedule by digital_poll_next_tech() on the poll workqueue (ddev->poll_work).
250 */
20static int digital_start_poll(struct nfc_dev *nfc_dev, __u32 im_protocols, 251static int digital_start_poll(struct nfc_dev *nfc_dev, __u32 im_protocols,
21 __u32 tm_protocols) 252 __u32 tm_protocols)
22{ 253{
23 return -EOPNOTSUPP; 254 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
255 u32 matching_im_protocols, matching_tm_protocols;
256
257 PR_DBG("protocols: im 0x%x, tm 0x%x, supported 0x%x", im_protocols,
258 tm_protocols, ddev->protocols);
259
260 matching_im_protocols = ddev->protocols & im_protocols;
261 matching_tm_protocols = ddev->protocols & tm_protocols;
262
263 if (!matching_im_protocols && !matching_tm_protocols) {
264 PR_ERR("No known protocol");
265 return -EINVAL;
266 }
267
268 if (ddev->poll_tech_count) {
269 PR_ERR("Already polling");
270 return -EBUSY;
271 }
272
273 if (ddev->curr_protocol) {
274 PR_ERR("A target is already active");
275 return -EBUSY;
276 }
277
278 ddev->poll_tech_count = 0;
279 ddev->poll_tech_index = 0;
280
281 if (matching_im_protocols & DIGITAL_PROTO_NFCA_RF_TECH)
282 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_106A,
283 digital_in_send_sens_req);
284
285 if (!ddev->poll_tech_count) {
286 PR_ERR("Unsupported protocols: im=0x%x, tm=0x%x",
287 matching_im_protocols, matching_tm_protocols);
288 return -EINVAL;
289 }
290
291 schedule_work(&ddev->poll_work);
292
293 return 0;
24} 294}
25 295
26static void digital_stop_poll(struct nfc_dev *nfc_dev) 296static void digital_stop_poll(struct nfc_dev *nfc_dev)
27{ 297{
298 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
299
300 mutex_lock(&ddev->poll_lock);
301
302 if (!ddev->poll_tech_count) {
303 PR_ERR("Polling operation was not running");
304 mutex_unlock(&ddev->poll_lock);
305 return;
306 }
307
308 ddev->poll_tech_count = 0;
309
310 mutex_unlock(&ddev->poll_lock);
311
312 cancel_work_sync(&ddev->poll_work);
313
314 digital_abort_cmd(ddev);
28} 315}
29 316
30static int digital_dev_up(struct nfc_dev *nfc_dev) 317static int digital_dev_up(struct nfc_dev *nfc_dev)
31{ 318{
32 return -EOPNOTSUPP; 319 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
320
321 digital_switch_rf(ddev, 1);
322
323 return 0;
33} 324}
34 325
35static int digital_dev_down(struct nfc_dev *nfc_dev) 326static int digital_dev_down(struct nfc_dev *nfc_dev)
36{ 327{
37 return -EOPNOTSUPP; 328 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
329
330 digital_switch_rf(ddev, 0);
331
332 return 0;
38} 333}
39 334
40static int digital_dep_link_up(struct nfc_dev *nfc_dev, 335static int digital_dep_link_up(struct nfc_dev *nfc_dev,
@@ -52,12 +347,15 @@ static int digital_dep_link_down(struct nfc_dev *nfc_dev)
52static int digital_activate_target(struct nfc_dev *nfc_dev, 347static int digital_activate_target(struct nfc_dev *nfc_dev,
53 struct nfc_target *target, __u32 protocol) 348 struct nfc_target *target, __u32 protocol)
54{ 349{
55 return -EOPNOTSUPP; 350 return 0;
56} 351}
57 352
58static void digital_deactivate_target(struct nfc_dev *nfc_dev, 353static void digital_deactivate_target(struct nfc_dev *nfc_dev,
59 struct nfc_target *target) 354 struct nfc_target *target)
60{ 355{
356 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
357
358 ddev->curr_protocol = 0;
61} 359}
62 360
63static int digital_tg_send(struct nfc_dev *dev, struct sk_buff *skb) 361static int digital_tg_send(struct nfc_dev *dev, struct sk_buff *skb)
@@ -106,8 +404,22 @@ struct nfc_digital_dev *nfc_digital_allocate_device(struct nfc_digital_ops *ops,
106 ddev->driver_capabilities = driver_capabilities; 404 ddev->driver_capabilities = driver_capabilities;
107 ddev->ops = ops; 405 ddev->ops = ops;
108 406
109 ddev->tx_headroom = tx_headroom; 407 mutex_init(&ddev->cmd_lock);
110 ddev->tx_tailroom = tx_tailroom; 408 INIT_LIST_HEAD(&ddev->cmd_queue);
409
410 INIT_WORK(&ddev->cmd_work, digital_wq_cmd);
411 INIT_WORK(&ddev->cmd_complete_work, digital_wq_cmd_complete);
412
413 mutex_init(&ddev->poll_lock);
414 INIT_WORK(&ddev->poll_work, digital_wq_poll);
415
416 if (supported_protocols & NFC_PROTO_JEWEL_MASK)
417 ddev->protocols |= NFC_PROTO_JEWEL_MASK;
418 if (supported_protocols & NFC_PROTO_MIFARE_MASK)
419 ddev->protocols |= NFC_PROTO_MIFARE_MASK;
420
421 ddev->tx_headroom = tx_headroom + DIGITAL_MAX_HEADER_LEN;
422 ddev->tx_tailroom = tx_tailroom + DIGITAL_CRC_LEN;
111 423
112 ddev->nfc_dev = nfc_allocate_device(&digital_nfc_ops, ddev->protocols, 424 ddev->nfc_dev = nfc_allocate_device(&digital_nfc_ops, ddev->protocols,
113 ddev->tx_headroom, 425 ddev->tx_headroom,
@@ -131,7 +443,6 @@ EXPORT_SYMBOL(nfc_digital_allocate_device);
131void nfc_digital_free_device(struct nfc_digital_dev *ddev) 443void nfc_digital_free_device(struct nfc_digital_dev *ddev)
132{ 444{
133 nfc_free_device(ddev->nfc_dev); 445 nfc_free_device(ddev->nfc_dev);
134
135 kfree(ddev); 446 kfree(ddev);
136} 447}
137EXPORT_SYMBOL(nfc_digital_free_device); 448EXPORT_SYMBOL(nfc_digital_free_device);
@@ -144,7 +455,22 @@ EXPORT_SYMBOL(nfc_digital_register_device);
144 455
145void nfc_digital_unregister_device(struct nfc_digital_dev *ddev) 456void nfc_digital_unregister_device(struct nfc_digital_dev *ddev)
146{ 457{
458 struct digital_cmd *cmd, *n;
459
147 nfc_unregister_device(ddev->nfc_dev); 460 nfc_unregister_device(ddev->nfc_dev);
461
462 mutex_lock(&ddev->poll_lock);
463 ddev->poll_tech_count = 0;
464 mutex_unlock(&ddev->poll_lock);
465
466 cancel_work_sync(&ddev->poll_work);
467 cancel_work_sync(&ddev->cmd_work);
468 cancel_work_sync(&ddev->cmd_complete_work);
469
470 list_for_each_entry_safe(cmd, n, &ddev->cmd_queue, queue) {
471 list_del(&cmd->queue);
472 kfree(cmd);
473 }
148} 474}
149EXPORT_SYMBOL(nfc_digital_unregister_device); 475EXPORT_SYMBOL(nfc_digital_unregister_device);
150 476
diff --git a/net/nfc/digital_technology.c b/net/nfc/digital_technology.c
new file mode 100644
index 000000000000..084b0fba5f4d
--- /dev/null
+++ b/net/nfc/digital_technology.c
@@ -0,0 +1,64 @@
1/*
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#include "digital.h"
17
18#define DIGITAL_CMD_SENS_REQ 0x26
19#define DIGITAL_CMD_ALL_REQ 0x52
20#define DIGITAL_CMD_SEL_REQ_CL1 0x93
21#define DIGITAL_CMD_SEL_REQ_CL2 0x95
22#define DIGITAL_CMD_SEL_REQ_CL3 0x97
23
24#define DIGITAL_SDD_REQ_SEL_PAR 0x20
25
26#define DIGITAL_SDD_RES_CT 0x88
27#define DIGITAL_SDD_RES_LEN 5
28
29static void digital_in_recv_sens_res(struct nfc_digital_dev *ddev, void *arg,
30 struct sk_buff *resp)
31{
32 if (!IS_ERR(resp))
33 dev_kfree_skb(resp);
34
35 digital_poll_next_tech(ddev);
36}
37
38int digital_in_send_sens_req(struct nfc_digital_dev *ddev, u8 rf_tech)
39{
40 struct sk_buff *skb;
41 int rc;
42
43 rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH,
44 NFC_DIGITAL_RF_TECH_106A);
45 if (rc)
46 return rc;
47
48 rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
49 NFC_DIGITAL_FRAMING_NFCA_SHORT);
50 if (rc)
51 return rc;
52
53 skb = digital_skb_alloc(ddev, 1);
54 if (!skb)
55 return -ENOMEM;
56
57 *skb_put(skb, sizeof(u8)) = DIGITAL_CMD_SENS_REQ;
58
59 rc = digital_in_send_cmd(ddev, skb, 30, digital_in_recv_sens_res, NULL);
60 if (rc)
61 kfree_skb(skb);
62
63 return rc;
64}