summaryrefslogtreecommitdiffstats
path: root/kernel/drivers/nfc/st21nfca/vendor_cmds.c
blob: ab765e5478c018884090b2b5bcfdc511c2732436 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
 * Proprietary commands extension for STMicroelectronics NFC Chip
 *
 * Copyright (C) 2014-2015  STMicroelectronics SAS. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

#include <net/genetlink.h>
#include <linux/module.h>
#include <linux/nfc.h>
#include <net/nfc/hci.h>
#include <net/nfc/llc.h>

#include "st21nfca.h"

#define ST21NFCA_HCI_DM_GETDATA			0x10
#define ST21NFCA_HCI_DM_PUTDATA			0x11
#define ST21NFCA_HCI_DM_LOAD			0x12
#define ST21NFCA_HCI_DM_GETINFO			0x13
#define ST21NFCA_HCI_DM_UPDATE_AID		0x20
#define ST21NFCA_HCI_DM_RESET			0x3e

#define ST21NFCA_HCI_DM_FIELD_GENERATOR		0x32

#define ST21NFCA_FACTORY_MODE_ON		1
#define ST21NFCA_FACTORY_MODE_OFF		0

#define ST21NFCA_EVT_POST_DATA			0x02

struct get_param_data {
	u8 gate;
	u8 data;
} __packed;

static int st21nfca_factory_mode(struct nfc_dev *dev, void *data,
			       size_t data_len)
{
	struct nfc_hci_dev *hdev = nfc_get_drvdata(dev);

	if (data_len != 1)
		return -EINVAL;

	pr_debug("factory mode: %x\n", ((u8 *)data)[0]);

	switch (((u8 *)data)[0]) {
	case ST21NFCA_FACTORY_MODE_ON:
		test_and_set_bit(ST21NFCA_FACTORY_MODE, &hdev->quirks);
	break;
	case ST21NFCA_FACTORY_MODE_OFF:
		clear_bit(ST21NFCA_FACTORY_MODE, &hdev->quirks);
	break;
	default:
		return -EINVAL;
	}

	return 0;
}

static int st21nfca_hci_clear_all_pipes(struct nfc_dev *dev, void *data,
				      size_t data_len)
{
	struct nfc_hci_dev *hdev = nfc_get_drvdata(dev);

	return nfc_hci_disconnect_all_gates(hdev);
}

static int st21nfca_hci_dm_put_data(struct nfc_dev *dev, void *data,
				  size_t data_len)
{
	struct nfc_hci_dev *hdev = nfc_get_drvdata(dev);

	return nfc_hci_send_cmd(hdev, ST21NFCA_DEVICE_MGNT_GATE,
				ST21NFCA_HCI_DM_PUTDATA, data,
				data_len, NULL);
}

static int st21nfca_hci_dm_update_aid(struct nfc_dev *dev, void *data,
				    size_t data_len)
{
	struct nfc_hci_dev *hdev = nfc_get_drvdata(dev);

	return nfc_hci_send_cmd(hdev, ST21NFCA_DEVICE_MGNT_GATE,
			ST21NFCA_HCI_DM_UPDATE_AID, data, data_len, NULL);
}

static int st21nfca_hci_dm_get_info(struct nfc_dev *dev, void *data,
				    size_t data_len)
{
	int r;
	struct sk_buff *msg, *skb;
	struct nfc_hci_dev *hdev = nfc_get_drvdata(dev);

	r = nfc_hci_send_cmd(hdev,
			     ST21NFCA_DEVICE_MGNT_GATE,
			     ST21NFCA_HCI_DM_GETINFO,
			     data, data_len, &skb);
	if (r)
		goto exit;

	msg = nfc_vendor_cmd_alloc_reply_skb(dev, ST21NFCA_VENDOR_OUI,
					     HCI_DM_GET_INFO, skb->len);
	if (!msg) {
		r = -ENOMEM;
		goto free_skb;
	}

	if (nla_put(msg, NFC_ATTR_VENDOR_DATA, skb->len, skb->data)) {
		kfree_skb(msg);
		r = -ENOBUFS;
		goto free_skb;
	}

	r = nfc_vendor_cmd_reply(msg);

free_skb:
	kfree_skb(skb);
exit:
	return r;
}

static int st21nfca_hci_dm_get_data(struct nfc_dev *dev, void *data,
				    size_t data_len)
{
	int r;
	struct sk_buff *msg, *skb;
	struct nfc_hci_dev *hdev = nfc_get_drvdata(dev);

	r = nfc_hci_send_cmd(hdev,
			     ST21NFCA_DEVICE_MGNT_GATE,
			     ST21NFCA_HCI_DM_GETDATA,
			     data, data_len, &skb);
	if (r)
		goto exit;

	msg = nfc_vendor_cmd_alloc_reply_skb(dev, ST21NFCA_VENDOR_OUI,
					     HCI_DM_GET_DATA, skb->len);
	if (!msg) {
		r = -ENOMEM;
		goto free_skb;
	}

	if (nla_put(msg, NFC_ATTR_VENDOR_DATA, skb->len, skb->data)) {
		kfree_skb(msg);
		r = -ENOBUFS;
		goto free_skb;
	}

	r = nfc_vendor_cmd_reply(msg);

free_skb:
	kfree_skb(skb);
exit:
	return r;
}

static int st21nfca_hci_dm_load(struct nfc_dev *dev, void *data,
				size_t data_len)
{
	struct nfc_hci_dev *hdev = nfc_get_drvdata(dev);

	return nfc_hci_send_cmd(hdev, ST21NFCA_DEVICE_MGNT_GATE,
				ST21NFCA_HCI_DM_LOAD, data, data_len, NULL);
}

static int st21nfca_hci_dm_reset(struct nfc_dev *dev, void *data,
				 size_t data_len)
{
	int r;
	struct nfc_hci_dev *hdev = nfc_get_drvdata(dev);

	r = nfc_hci_send_cmd_async(hdev, ST21NFCA_DEVICE_MGNT_GATE,
			ST21NFCA_HCI_DM_RESET, data, data_len, NULL, NULL);
	if (r < 0)
		return r;

	r = nfc_llc_stop(hdev->llc);
	if (r < 0)
		return r;

	return nfc_llc_start(hdev->llc);
}

static int st21nfca_hci_get_param(struct nfc_dev *dev, void *data,
				  size_t data_len)
{
	int r;
	struct sk_buff *msg, *skb;
	struct nfc_hci_dev *hdev = nfc_get_drvdata(dev);
	struct get_param_data *param = (struct get_param_data *)data;

	if (data_len < sizeof(struct get_param_data))
		return -EPROTO;

	r = nfc_hci_get_param(hdev, param->gate, param->data, &skb);
	if (r)
		goto exit;

	msg = nfc_vendor_cmd_alloc_reply_skb(dev, ST21NFCA_VENDOR_OUI,
					     HCI_GET_PARAM, skb->len);
	if (!msg) {
		r = -ENOMEM;
		goto free_skb;
	}

	if (nla_put(msg, NFC_ATTR_VENDOR_DATA, skb->len, skb->data)) {
		kfree_skb(msg);
		r = -ENOBUFS;
		goto free_skb;
	}

	r = nfc_vendor_cmd_reply(msg);

free_skb:
	kfree_skb(skb);
exit:
	return r;
}

static int st21nfca_hci_dm_field_generator(struct nfc_dev *dev, void *data,
					   size_t data_len)
{
	struct nfc_hci_dev *hdev = nfc_get_drvdata(dev);

	return nfc_hci_send_cmd(hdev,
				ST21NFCA_DEVICE_MGNT_GATE,
				ST21NFCA_HCI_DM_FIELD_GENERATOR,
				data, data_len, NULL);
}

int st21nfca_hci_loopback_event_received(struct nfc_hci_dev *hdev, u8 event,
					 struct sk_buff *skb)
{
	struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);

	switch (event) {
	case ST21NFCA_EVT_POST_DATA:
		info->vendor_info.rx_skb = skb;
	break;
	default:
		nfc_err(&hdev->ndev->dev, "Unexpected event on loopback gate\n");
	}
	complete(&info->vendor_info.req_completion);
	return 0;
}
EXPORT_SYMBOL(st21nfca_hci_loopback_event_received);

static int st21nfca_hci_loopback(struct nfc_dev *dev, void *data,
				 size_t data_len)
{
	int r;
	struct sk_buff *msg;
	struct nfc_hci_dev *hdev = nfc_get_drvdata(dev);
	struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);

	if (data_len <= 0)
		return -EPROTO;

	reinit_completion(&info->vendor_info.req_completion);
	info->vendor_info.rx_skb = NULL;

	r = nfc_hci_send_event(hdev, NFC_HCI_LOOPBACK_GATE,
			       ST21NFCA_EVT_POST_DATA, data, data_len);
	if (r < 0) {
		r = -EPROTO;
		goto exit;
	}

	wait_for_completion_interruptible(&info->vendor_info.req_completion);
	if (!info->vendor_info.rx_skb ||
	    info->vendor_info.rx_skb->len != data_len) {
		r = -EPROTO;
		goto exit;
	}

	msg = nfc_vendor_cmd_alloc_reply_skb(hdev->ndev,
					ST21NFCA_VENDOR_OUI,
					HCI_LOOPBACK,
					info->vendor_info.rx_skb->len);
	if (!msg) {
		r = -ENOMEM;
		goto free_skb;
	}

	if (nla_put(msg, NFC_ATTR_VENDOR_DATA, info->vendor_info.rx_skb->len,
		    info->vendor_info.rx_skb->data)) {
		kfree_skb(msg);
		r = -ENOBUFS;
		goto free_skb;
	}

	r = nfc_vendor_cmd_reply(msg);
free_skb:
	kfree_skb(info->vendor_info.rx_skb);
exit:
	return r;
}

static struct nfc_vendor_cmd st21nfca_vendor_cmds[] = {
	{
		.vendor_id = ST21NFCA_VENDOR_OUI,
		.subcmd = FACTORY_MODE,
		.doit = st21nfca_factory_mode,
	},
	{
		.vendor_id = ST21NFCA_VENDOR_OUI,
		.subcmd = HCI_CLEAR_ALL_PIPES,
		.doit = st21nfca_hci_clear_all_pipes,
	},
	{
		.vendor_id = ST21NFCA_VENDOR_OUI,
		.subcmd = HCI_DM_PUT_DATA,
		.doit = st21nfca_hci_dm_put_data,
	},
	{
		.vendor_id = ST21NFCA_VENDOR_OUI,
		.subcmd = HCI_DM_UPDATE_AID,
		.doit = st21nfca_hci_dm_update_aid,
	},
	{
		.vendor_id = ST21NFCA_VENDOR_OUI,
		.subcmd = HCI_DM_GET_INFO,
		.doit = st21nfca_hci_dm_get_info,
	},
	{
		.vendor_id = ST21NFCA_VENDOR_OUI,
		.subcmd = HCI_DM_GET_DATA,
		.doit = st21nfca_hci_dm_get_data,
	},
	{
		.vendor_id = ST21NFCA_VENDOR_OUI,
		.subcmd = HCI_DM_LOAD,
		.doit = st21nfca_hci_dm_load,
	},
	{
		.vendor_id = ST21NFCA_VENDOR_OUI,
		.subcmd = HCI_DM_RESET,
		.doit = st21nfca_hci_dm_reset,
	},
	{
		.vendor_id = ST21NFCA_VENDOR_OUI,
		.subcmd = HCI_GET_PARAM,
		.doit = st21nfca_hci_get_param,
	},
	{
		.vendor_id = ST21NFCA_VENDOR_OUI,
		.subcmd = HCI_DM_FIELD_GENERATOR,
		.doit = st21nfca_hci_dm_field_generator,
	},
	{
		.vendor_id = ST21NFCA_VENDOR_OUI,
		.subcmd = HCI_LOOPBACK,
		.doit = st21nfca_hci_loopback,
	},
};

int st21nfca_vendor_cmds_init(struct nfc_hci_dev *hdev)
{
	struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);

	init_completion(&info->vendor_info.req_completion);
	return nfc_set_vendor_cmds(hdev->ndev, st21nfca_vendor_cmds,
				   sizeof(st21nfca_vendor_cmds));
}
EXPORT_SYMBOL(st21nfca_vendor_cmds_init);