• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* -------------------------------------------------------------------------
2  * Copyright (C) 2014-2016, Intel Corporation
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  */
15 
16 #include <linux/module.h>
17 #include <linux/nfc.h>
18 #include <linux/i2c.h>
19 #include <linux/delay.h>
20 #include <linux/firmware.h>
21 #include <net/nfc/nci_core.h>
22 
23 #include "fdp.h"
24 
25 #define FDP_OTP_PATCH_NAME			"otp.bin"
26 #define FDP_RAM_PATCH_NAME			"ram.bin"
27 #define FDP_FW_HEADER_SIZE			576
28 #define FDP_FW_UPDATE_SLEEP			1000
29 
30 #define NCI_GET_VERSION_TIMEOUT			8000
31 #define NCI_PATCH_REQUEST_TIMEOUT		8000
32 #define FDP_PATCH_CONN_DEST			0xC2
33 #define FDP_PATCH_CONN_PARAM_TYPE		0xA0
34 
35 #define NCI_PATCH_TYPE_RAM			0x00
36 #define NCI_PATCH_TYPE_OTP			0x01
37 #define NCI_PATCH_TYPE_EOT			0xFF
38 
39 #define NCI_PARAM_ID_FW_RAM_VERSION		0xA0
40 #define NCI_PARAM_ID_FW_OTP_VERSION		0xA1
41 #define NCI_PARAM_ID_OTP_LIMITED_VERSION	0xC5
42 #define NCI_PARAM_ID_KEY_INDEX_ID		0xC6
43 
44 #define NCI_GID_PROP				0x0F
45 #define NCI_OP_PROP_PATCH_OID			0x08
46 #define NCI_OP_PROP_SET_PDATA_OID		0x23
47 
48 struct fdp_nci_info {
49 	struct nfc_phy_ops *phy_ops;
50 	struct fdp_i2c_phy *phy;
51 	struct nci_dev *ndev;
52 
53 	const struct firmware *otp_patch;
54 	const struct firmware *ram_patch;
55 	u32 otp_patch_version;
56 	u32 ram_patch_version;
57 
58 	u32 otp_version;
59 	u32 ram_version;
60 	u32 limited_otp_version;
61 	u8 key_index;
62 
63 	u8 *fw_vsc_cfg;
64 	u8 clock_type;
65 	u32 clock_freq;
66 
67 	atomic_t data_pkt_counter;
68 	void (*data_pkt_counter_cb)(struct nci_dev *ndev);
69 	u8 setup_patch_sent;
70 	u8 setup_patch_ntf;
71 	u8 setup_patch_status;
72 	u8 setup_reset_ntf;
73 	wait_queue_head_t setup_wq;
74 };
75 
76 static u8 nci_core_get_config_otp_ram_version[5] = {
77 	0x04,
78 	NCI_PARAM_ID_FW_RAM_VERSION,
79 	NCI_PARAM_ID_FW_OTP_VERSION,
80 	NCI_PARAM_ID_OTP_LIMITED_VERSION,
81 	NCI_PARAM_ID_KEY_INDEX_ID
82 };
83 
84 struct nci_core_get_config_rsp {
85 	u8 status;
86 	u8 count;
87 	u8 data[0];
88 };
89 
fdp_nci_create_conn(struct nci_dev * ndev)90 static int fdp_nci_create_conn(struct nci_dev *ndev)
91 {
92 	struct fdp_nci_info *info = nci_get_drvdata(ndev);
93 	struct core_conn_create_dest_spec_params param;
94 	int r;
95 
96 	/* proprietary destination specific paramerer without value */
97 	param.type = FDP_PATCH_CONN_PARAM_TYPE;
98 	param.length = 0x00;
99 
100 	r = nci_core_conn_create(info->ndev, FDP_PATCH_CONN_DEST, 1,
101 				 sizeof(param), &param);
102 	if (r)
103 		return r;
104 
105 	return nci_get_conn_info_by_dest_type_params(ndev,
106 						     FDP_PATCH_CONN_DEST, NULL);
107 }
108 
fdp_nci_get_versions(struct nci_dev * ndev)109 static inline int fdp_nci_get_versions(struct nci_dev *ndev)
110 {
111 	return nci_core_cmd(ndev, NCI_OP_CORE_GET_CONFIG_CMD,
112 			    sizeof(nci_core_get_config_otp_ram_version),
113 			    (__u8 *) &nci_core_get_config_otp_ram_version);
114 }
115 
fdp_nci_patch_cmd(struct nci_dev * ndev,u8 type)116 static inline int fdp_nci_patch_cmd(struct nci_dev *ndev, u8 type)
117 {
118 	return nci_prop_cmd(ndev, NCI_OP_PROP_PATCH_OID, sizeof(type), &type);
119 }
120 
fdp_nci_set_production_data(struct nci_dev * ndev,u8 len,char * data)121 static inline int fdp_nci_set_production_data(struct nci_dev *ndev, u8 len,
122 					      char *data)
123 {
124 	return nci_prop_cmd(ndev, NCI_OP_PROP_SET_PDATA_OID, len, data);
125 }
126 
fdp_nci_set_clock(struct nci_dev * ndev,u8 clock_type,u32 clock_freq)127 static int fdp_nci_set_clock(struct nci_dev *ndev, u8 clock_type,
128 			     u32 clock_freq)
129 {
130 	u32 fc = 13560;
131 	u32 nd, num, delta;
132 	char data[9];
133 
134 	nd = (24 * fc) / clock_freq;
135 	delta = 24 * fc - nd * clock_freq;
136 	num = (32768 * delta) / clock_freq;
137 
138 	data[0] = 0x00;
139 	data[1] = 0x00;
140 	data[2] = 0x00;
141 
142 	data[3] = 0x10;
143 	data[4] = 0x04;
144 	data[5] = num & 0xFF;
145 	data[6] = (num >> 8) & 0xff;
146 	data[7] = nd;
147 	data[8] = clock_type;
148 
149 	return fdp_nci_set_production_data(ndev, 9, data);
150 }
151 
fdp_nci_send_patch_cb(struct nci_dev * ndev)152 static void fdp_nci_send_patch_cb(struct nci_dev *ndev)
153 {
154 	struct fdp_nci_info *info = nci_get_drvdata(ndev);
155 
156 	info->setup_patch_sent = 1;
157 	wake_up(&info->setup_wq);
158 }
159 
160 /**
161  * Register a packet sent counter and a callback
162  *
163  * We have no other way of knowing when all firmware packets were sent out
164  * on the i2c bus. We need to know that in order to close the connection and
165  * send the patch end message.
166  */
fdp_nci_set_data_pkt_counter(struct nci_dev * ndev,void (* cb)(struct nci_dev * ndev),int count)167 static void fdp_nci_set_data_pkt_counter(struct nci_dev *ndev,
168 				  void (*cb)(struct nci_dev *ndev), int count)
169 {
170 	struct fdp_nci_info *info = nci_get_drvdata(ndev);
171 	struct device *dev = &info->phy->i2c_dev->dev;
172 
173 	dev_dbg(dev, "NCI data pkt counter %d\n", count);
174 	atomic_set(&info->data_pkt_counter, count);
175 	info->data_pkt_counter_cb = cb;
176 }
177 
178 /**
179  * The device is expecting a stream of packets. All packets need to
180  * have the PBF flag set to 0x0 (last packet) even if the firmware
181  * file is segmented and there are multiple packets. If we give the
182  * whole firmware to nci_send_data it will segment it and it will set
183  * the PBF flag to 0x01 so we need to do the segmentation here.
184  *
185  * The firmware will be analyzed and applied when we send NCI_OP_PROP_PATCH_CMD
186  * command with NCI_PATCH_TYPE_EOT parameter. The device will send a
187  * NFCC_PATCH_NTF packaet and a NCI_OP_CORE_RESET_NTF packet.
188  */
fdp_nci_send_patch(struct nci_dev * ndev,u8 conn_id,u8 type)189 static int fdp_nci_send_patch(struct nci_dev *ndev, u8 conn_id, u8 type)
190 {
191 	struct fdp_nci_info *info = nci_get_drvdata(ndev);
192 	const struct firmware *fw;
193 	struct sk_buff *skb;
194 	unsigned long len;
195 	int max_size, payload_size;
196 	int rc = 0;
197 
198 	if ((type == NCI_PATCH_TYPE_OTP && !info->otp_patch) ||
199 	    (type == NCI_PATCH_TYPE_RAM && !info->ram_patch))
200 		return -EINVAL;
201 
202 	if (type == NCI_PATCH_TYPE_OTP)
203 		fw = info->otp_patch;
204 	else
205 		fw = info->ram_patch;
206 
207 	max_size = nci_conn_max_data_pkt_payload_size(ndev, conn_id);
208 	if (max_size <= 0)
209 		return -EINVAL;
210 
211 	len = fw->size;
212 
213 	fdp_nci_set_data_pkt_counter(ndev, fdp_nci_send_patch_cb,
214 				     DIV_ROUND_UP(fw->size, max_size));
215 
216 	while (len) {
217 
218 		payload_size = min_t(unsigned long, max_size, len);
219 
220 		skb = nci_skb_alloc(ndev, (NCI_CTRL_HDR_SIZE + payload_size),
221 				    GFP_KERNEL);
222 		if (!skb) {
223 			fdp_nci_set_data_pkt_counter(ndev, NULL, 0);
224 			return -ENOMEM;
225 		}
226 
227 
228 		skb_reserve(skb, NCI_CTRL_HDR_SIZE);
229 
230 		skb_put_data(skb, fw->data + (fw->size - len), payload_size);
231 
232 		rc = nci_send_data(ndev, conn_id, skb);
233 
234 		if (rc) {
235 			fdp_nci_set_data_pkt_counter(ndev, NULL, 0);
236 			return rc;
237 		}
238 
239 		len -= payload_size;
240 	}
241 
242 	return rc;
243 }
244 
fdp_nci_open(struct nci_dev * ndev)245 static int fdp_nci_open(struct nci_dev *ndev)
246 {
247 	int r;
248 	struct fdp_nci_info *info = nci_get_drvdata(ndev);
249 	struct device *dev = &info->phy->i2c_dev->dev;
250 
251 	dev_dbg(dev, "%s\n", __func__);
252 
253 	r = info->phy_ops->enable(info->phy);
254 
255 	return r;
256 }
257 
fdp_nci_close(struct nci_dev * ndev)258 static int fdp_nci_close(struct nci_dev *ndev)
259 {
260 	struct fdp_nci_info *info = nci_get_drvdata(ndev);
261 	struct device *dev = &info->phy->i2c_dev->dev;
262 
263 	dev_dbg(dev, "%s\n", __func__);
264 	return 0;
265 }
266 
fdp_nci_send(struct nci_dev * ndev,struct sk_buff * skb)267 static int fdp_nci_send(struct nci_dev *ndev, struct sk_buff *skb)
268 {
269 	struct fdp_nci_info *info = nci_get_drvdata(ndev);
270 	struct device *dev = &info->phy->i2c_dev->dev;
271 
272 	dev_dbg(dev, "%s\n", __func__);
273 
274 	if (atomic_dec_and_test(&info->data_pkt_counter))
275 		info->data_pkt_counter_cb(ndev);
276 
277 	return info->phy_ops->write(info->phy, skb);
278 }
279 
fdp_nci_recv_frame(struct nci_dev * ndev,struct sk_buff * skb)280 int fdp_nci_recv_frame(struct nci_dev *ndev, struct sk_buff *skb)
281 {
282 	struct fdp_nci_info *info = nci_get_drvdata(ndev);
283 	struct device *dev = &info->phy->i2c_dev->dev;
284 
285 	dev_dbg(dev, "%s\n", __func__);
286 	return nci_recv_frame(ndev, skb);
287 }
288 EXPORT_SYMBOL(fdp_nci_recv_frame);
289 
fdp_nci_request_firmware(struct nci_dev * ndev)290 static int fdp_nci_request_firmware(struct nci_dev *ndev)
291 {
292 	struct fdp_nci_info *info = nci_get_drvdata(ndev);
293 	struct device *dev = &info->phy->i2c_dev->dev;
294 	u8 *data;
295 	int r;
296 
297 	r = request_firmware(&info->ram_patch, FDP_RAM_PATCH_NAME, dev);
298 	if (r < 0) {
299 		nfc_err(dev, "RAM patch request error\n");
300 		goto error;
301 	}
302 
303 	data = (u8 *) info->ram_patch->data;
304 	info->ram_patch_version =
305 		data[FDP_FW_HEADER_SIZE] |
306 		(data[FDP_FW_HEADER_SIZE + 1] << 8) |
307 		(data[FDP_FW_HEADER_SIZE + 2] << 16) |
308 		(data[FDP_FW_HEADER_SIZE + 3] << 24);
309 
310 	dev_dbg(dev, "RAM patch version: %d, size: %d\n",
311 		  info->ram_patch_version, (int) info->ram_patch->size);
312 
313 
314 	r = request_firmware(&info->otp_patch, FDP_OTP_PATCH_NAME, dev);
315 	if (r < 0) {
316 		nfc_err(dev, "OTP patch request error\n");
317 		goto out;
318 	}
319 
320 	data = (u8 *) info->otp_patch->data;
321 	info->otp_patch_version =
322 		data[FDP_FW_HEADER_SIZE] |
323 		(data[FDP_FW_HEADER_SIZE + 1] << 8) |
324 		(data[FDP_FW_HEADER_SIZE+2] << 16) |
325 		(data[FDP_FW_HEADER_SIZE+3] << 24);
326 
327 	dev_dbg(dev, "OTP patch version: %d, size: %d\n",
328 		 info->otp_patch_version, (int) info->otp_patch->size);
329 out:
330 	return 0;
331 error:
332 	return r;
333 }
334 
fdp_nci_release_firmware(struct nci_dev * ndev)335 static void fdp_nci_release_firmware(struct nci_dev *ndev)
336 {
337 	struct fdp_nci_info *info = nci_get_drvdata(ndev);
338 
339 	if (info->otp_patch) {
340 		release_firmware(info->otp_patch);
341 		info->otp_patch = NULL;
342 	}
343 
344 	if (info->ram_patch) {
345 		release_firmware(info->ram_patch);
346 		info->ram_patch = NULL;
347 	}
348 }
349 
fdp_nci_patch_otp(struct nci_dev * ndev)350 static int fdp_nci_patch_otp(struct nci_dev *ndev)
351 {
352 	struct fdp_nci_info *info = nci_get_drvdata(ndev);
353 	struct device *dev = &info->phy->i2c_dev->dev;
354 	int conn_id;
355 	int r = 0;
356 
357 	if (info->otp_version >= info->otp_patch_version)
358 		goto out;
359 
360 	info->setup_patch_sent = 0;
361 	info->setup_reset_ntf = 0;
362 	info->setup_patch_ntf = 0;
363 
364 	/* Patch init request */
365 	r = fdp_nci_patch_cmd(ndev, NCI_PATCH_TYPE_OTP);
366 	if (r)
367 		goto out;
368 
369 	/* Patch data connection creation */
370 	conn_id = fdp_nci_create_conn(ndev);
371 	if (conn_id < 0) {
372 		r = conn_id;
373 		goto out;
374 	}
375 
376 	/* Send the patch over the data connection */
377 	r = fdp_nci_send_patch(ndev, conn_id, NCI_PATCH_TYPE_OTP);
378 	if (r)
379 		goto out;
380 
381 	/* Wait for all the packets to be send over i2c */
382 	wait_event_interruptible(info->setup_wq,
383 				 info->setup_patch_sent == 1);
384 
385 	/* make sure that the NFCC processed the last data packet */
386 	msleep(FDP_FW_UPDATE_SLEEP);
387 
388 	/* Close the data connection */
389 	r = nci_core_conn_close(info->ndev, conn_id);
390 	if (r)
391 		goto out;
392 
393 	/* Patch finish message */
394 	if (fdp_nci_patch_cmd(ndev, NCI_PATCH_TYPE_EOT)) {
395 		nfc_err(dev, "OTP patch error 0x%x\n", r);
396 		r = -EINVAL;
397 		goto out;
398 	}
399 
400 	/* If the patch notification didn't arrive yet, wait for it */
401 	wait_event_interruptible(info->setup_wq, info->setup_patch_ntf);
402 
403 	/* Check if the patching was successful */
404 	r = info->setup_patch_status;
405 	if (r) {
406 		nfc_err(dev, "OTP patch error 0x%x\n", r);
407 		r = -EINVAL;
408 		goto out;
409 	}
410 
411 	/*
412 	 * We need to wait for the reset notification before we
413 	 * can continue
414 	 */
415 	wait_event_interruptible(info->setup_wq, info->setup_reset_ntf);
416 
417 out:
418 	return r;
419 }
420 
fdp_nci_patch_ram(struct nci_dev * ndev)421 static int fdp_nci_patch_ram(struct nci_dev *ndev)
422 {
423 	struct fdp_nci_info *info = nci_get_drvdata(ndev);
424 	struct device *dev = &info->phy->i2c_dev->dev;
425 	int conn_id;
426 	int r = 0;
427 
428 	if (info->ram_version >= info->ram_patch_version)
429 		goto out;
430 
431 	info->setup_patch_sent = 0;
432 	info->setup_reset_ntf = 0;
433 	info->setup_patch_ntf = 0;
434 
435 	/* Patch init request */
436 	r = fdp_nci_patch_cmd(ndev, NCI_PATCH_TYPE_RAM);
437 	if (r)
438 		goto out;
439 
440 	/* Patch data connection creation */
441 	conn_id = fdp_nci_create_conn(ndev);
442 	if (conn_id < 0) {
443 		r = conn_id;
444 		goto out;
445 	}
446 
447 	/* Send the patch over the data connection */
448 	r = fdp_nci_send_patch(ndev, conn_id, NCI_PATCH_TYPE_RAM);
449 	if (r)
450 		goto out;
451 
452 	/* Wait for all the packets to be send over i2c */
453 	wait_event_interruptible(info->setup_wq,
454 				 info->setup_patch_sent == 1);
455 
456 	/* make sure that the NFCC processed the last data packet */
457 	msleep(FDP_FW_UPDATE_SLEEP);
458 
459 	/* Close the data connection */
460 	r = nci_core_conn_close(info->ndev, conn_id);
461 	if (r)
462 		goto out;
463 
464 	/* Patch finish message */
465 	if (fdp_nci_patch_cmd(ndev, NCI_PATCH_TYPE_EOT)) {
466 		nfc_err(dev, "RAM patch error 0x%x\n", r);
467 		r = -EINVAL;
468 		goto out;
469 	}
470 
471 	/* If the patch notification didn't arrive yet, wait for it */
472 	wait_event_interruptible(info->setup_wq, info->setup_patch_ntf);
473 
474 	/* Check if the patching was successful */
475 	r = info->setup_patch_status;
476 	if (r) {
477 		nfc_err(dev, "RAM patch error 0x%x\n", r);
478 		r = -EINVAL;
479 		goto out;
480 	}
481 
482 	/*
483 	 * We need to wait for the reset notification before we
484 	 * can continue
485 	 */
486 	wait_event_interruptible(info->setup_wq, info->setup_reset_ntf);
487 
488 out:
489 	return r;
490 }
491 
fdp_nci_setup(struct nci_dev * ndev)492 static int fdp_nci_setup(struct nci_dev *ndev)
493 {
494 	/* Format: total length followed by an NCI packet */
495 	struct fdp_nci_info *info = nci_get_drvdata(ndev);
496 	struct device *dev = &info->phy->i2c_dev->dev;
497 	int r;
498 	u8 patched = 0;
499 
500 	dev_dbg(dev, "%s\n", __func__);
501 
502 	r = nci_core_init(ndev);
503 	if (r)
504 		goto error;
505 
506 	/* Get RAM and OTP version */
507 	r = fdp_nci_get_versions(ndev);
508 	if (r)
509 		goto error;
510 
511 	/* Load firmware from disk */
512 	r = fdp_nci_request_firmware(ndev);
513 	if (r)
514 		goto error;
515 
516 	/* Update OTP */
517 	if (info->otp_version < info->otp_patch_version) {
518 		r = fdp_nci_patch_otp(ndev);
519 		if (r)
520 			goto error;
521 		patched = 1;
522 	}
523 
524 	/* Update RAM */
525 	if (info->ram_version < info->ram_patch_version) {
526 		r = fdp_nci_patch_ram(ndev);
527 		if (r)
528 			goto error;
529 		patched = 1;
530 	}
531 
532 	/* Release the firmware buffers */
533 	fdp_nci_release_firmware(ndev);
534 
535 	/* If a patch was applied the new version is checked */
536 	if (patched) {
537 		r = nci_core_init(ndev);
538 		if (r)
539 			goto error;
540 
541 		r = fdp_nci_get_versions(ndev);
542 		if (r)
543 			goto error;
544 
545 		if (info->otp_version != info->otp_patch_version ||
546 		    info->ram_version != info->ram_patch_version) {
547 			nfc_err(dev, "Firmware update failed");
548 			r = -EINVAL;
549 			goto error;
550 		}
551 	}
552 
553 	/*
554 	 * We initialized the devices but the NFC subsystem expects
555 	 * it to not be initialized.
556 	 */
557 	return nci_core_reset(ndev);
558 
559 error:
560 	fdp_nci_release_firmware(ndev);
561 	nfc_err(dev, "Setup error %d\n", r);
562 	return r;
563 }
564 
fdp_nci_post_setup(struct nci_dev * ndev)565 static int fdp_nci_post_setup(struct nci_dev *ndev)
566 {
567 	struct fdp_nci_info *info = nci_get_drvdata(ndev);
568 	struct device *dev = &info->phy->i2c_dev->dev;
569 	int r;
570 
571 	/* Check if the device has VSC */
572 	if (info->fw_vsc_cfg && info->fw_vsc_cfg[0]) {
573 
574 		/* Set the vendor specific configuration */
575 		r = fdp_nci_set_production_data(ndev, info->fw_vsc_cfg[3],
576 						&info->fw_vsc_cfg[4]);
577 		if (r) {
578 			nfc_err(dev, "Vendor specific config set error %d\n",
579 				r);
580 			return r;
581 		}
582 	}
583 
584 	/* Set clock type and frequency */
585 	r = fdp_nci_set_clock(ndev, info->clock_type, info->clock_freq);
586 	if (r) {
587 		nfc_err(dev, "Clock set error %d\n", r);
588 		return r;
589 	}
590 
591 	/*
592 	 * In order to apply the VSC FDP needs a reset
593 	 */
594 	r = nci_core_reset(ndev);
595 	if (r)
596 		return r;
597 
598 	/**
599 	 * The nci core was initialized when post setup was called
600 	 * so we leave it like that
601 	 */
602 	return nci_core_init(ndev);
603 }
604 
fdp_nci_core_reset_ntf_packet(struct nci_dev * ndev,struct sk_buff * skb)605 static int fdp_nci_core_reset_ntf_packet(struct nci_dev *ndev,
606 					  struct sk_buff *skb)
607 {
608 	struct fdp_nci_info *info = nci_get_drvdata(ndev);
609 	struct device *dev = &info->phy->i2c_dev->dev;
610 
611 	dev_dbg(dev, "%s\n", __func__);
612 	info->setup_reset_ntf = 1;
613 	wake_up(&info->setup_wq);
614 
615 	return 0;
616 }
617 
fdp_nci_prop_patch_ntf_packet(struct nci_dev * ndev,struct sk_buff * skb)618 static int fdp_nci_prop_patch_ntf_packet(struct nci_dev *ndev,
619 					  struct sk_buff *skb)
620 {
621 	struct fdp_nci_info *info = nci_get_drvdata(ndev);
622 	struct device *dev = &info->phy->i2c_dev->dev;
623 
624 	dev_dbg(dev, "%s\n", __func__);
625 	info->setup_patch_ntf = 1;
626 	info->setup_patch_status = skb->data[0];
627 	wake_up(&info->setup_wq);
628 
629 	return 0;
630 }
631 
fdp_nci_prop_patch_rsp_packet(struct nci_dev * ndev,struct sk_buff * skb)632 static int fdp_nci_prop_patch_rsp_packet(struct nci_dev *ndev,
633 					  struct sk_buff *skb)
634 {
635 	struct fdp_nci_info *info = nci_get_drvdata(ndev);
636 	struct device *dev = &info->phy->i2c_dev->dev;
637 	u8 status = skb->data[0];
638 
639 	dev_dbg(dev, "%s: status 0x%x\n", __func__, status);
640 	nci_req_complete(ndev, status);
641 
642 	return 0;
643 }
644 
fdp_nci_prop_set_production_data_rsp_packet(struct nci_dev * ndev,struct sk_buff * skb)645 static int fdp_nci_prop_set_production_data_rsp_packet(struct nci_dev *ndev,
646 							struct sk_buff *skb)
647 {
648 	struct fdp_nci_info *info = nci_get_drvdata(ndev);
649 	struct device *dev = &info->phy->i2c_dev->dev;
650 	u8 status = skb->data[0];
651 
652 	dev_dbg(dev, "%s: status 0x%x\n", __func__, status);
653 	nci_req_complete(ndev, status);
654 
655 	return 0;
656 }
657 
fdp_nci_core_get_config_rsp_packet(struct nci_dev * ndev,struct sk_buff * skb)658 static int fdp_nci_core_get_config_rsp_packet(struct nci_dev *ndev,
659 						struct sk_buff *skb)
660 {
661 	struct fdp_nci_info *info = nci_get_drvdata(ndev);
662 	struct device *dev = &info->phy->i2c_dev->dev;
663 	struct nci_core_get_config_rsp *rsp = (void *) skb->data;
664 	u8 i, *p;
665 
666 	if (rsp->status == NCI_STATUS_OK) {
667 
668 		p = rsp->data;
669 		for (i = 0; i < 4; i++) {
670 
671 			switch (*p++) {
672 			case NCI_PARAM_ID_FW_RAM_VERSION:
673 				p++;
674 				info->ram_version = le32_to_cpup((__le32 *) p);
675 				p += 4;
676 				break;
677 			case NCI_PARAM_ID_FW_OTP_VERSION:
678 				p++;
679 				info->otp_version = le32_to_cpup((__le32 *) p);
680 				p += 4;
681 				break;
682 			case NCI_PARAM_ID_OTP_LIMITED_VERSION:
683 				p++;
684 				info->otp_version = le32_to_cpup((__le32 *) p);
685 				p += 4;
686 				break;
687 			case NCI_PARAM_ID_KEY_INDEX_ID:
688 				p++;
689 				info->key_index = *p++;
690 			}
691 		}
692 	}
693 
694 	dev_dbg(dev, "OTP version %d\n", info->otp_version);
695 	dev_dbg(dev, "RAM version %d\n", info->ram_version);
696 	dev_dbg(dev, "key index %d\n", info->key_index);
697 	dev_dbg(dev, "%s: status 0x%x\n", __func__, rsp->status);
698 
699 	nci_req_complete(ndev, rsp->status);
700 
701 	return 0;
702 }
703 
704 static struct nci_driver_ops fdp_core_ops[] = {
705 	{
706 		.opcode = NCI_OP_CORE_GET_CONFIG_RSP,
707 		.rsp = fdp_nci_core_get_config_rsp_packet,
708 	},
709 	{
710 		.opcode = NCI_OP_CORE_RESET_NTF,
711 		.ntf = fdp_nci_core_reset_ntf_packet,
712 	},
713 };
714 
715 static struct nci_driver_ops fdp_prop_ops[] = {
716 	{
717 		.opcode = nci_opcode_pack(NCI_GID_PROP, NCI_OP_PROP_PATCH_OID),
718 		.rsp = fdp_nci_prop_patch_rsp_packet,
719 		.ntf = fdp_nci_prop_patch_ntf_packet,
720 	},
721 	{
722 		.opcode = nci_opcode_pack(NCI_GID_PROP,
723 					  NCI_OP_PROP_SET_PDATA_OID),
724 		.rsp = fdp_nci_prop_set_production_data_rsp_packet,
725 	},
726 };
727 
728 static struct nci_ops nci_ops = {
729 	.open = fdp_nci_open,
730 	.close = fdp_nci_close,
731 	.send = fdp_nci_send,
732 	.setup = fdp_nci_setup,
733 	.post_setup = fdp_nci_post_setup,
734 	.prop_ops = fdp_prop_ops,
735 	.n_prop_ops = ARRAY_SIZE(fdp_prop_ops),
736 	.core_ops = fdp_core_ops,
737 	.n_core_ops = ARRAY_SIZE(fdp_core_ops),
738 };
739 
fdp_nci_probe(struct fdp_i2c_phy * phy,struct nfc_phy_ops * phy_ops,struct nci_dev ** ndevp,int tx_headroom,int tx_tailroom,u8 clock_type,u32 clock_freq,u8 * fw_vsc_cfg)740 int fdp_nci_probe(struct fdp_i2c_phy *phy, struct nfc_phy_ops *phy_ops,
741 			struct nci_dev **ndevp, int tx_headroom,
742 			int tx_tailroom, u8 clock_type, u32 clock_freq,
743 			u8 *fw_vsc_cfg)
744 {
745 	struct device *dev = &phy->i2c_dev->dev;
746 	struct fdp_nci_info *info;
747 	struct nci_dev *ndev;
748 	u32 protocols;
749 	int r;
750 
751 	info = devm_kzalloc(dev, sizeof(struct fdp_nci_info), GFP_KERNEL);
752 	if (!info)
753 		return -ENOMEM;
754 
755 	info->phy = phy;
756 	info->phy_ops = phy_ops;
757 	info->clock_type = clock_type;
758 	info->clock_freq = clock_freq;
759 	info->fw_vsc_cfg = fw_vsc_cfg;
760 
761 	init_waitqueue_head(&info->setup_wq);
762 
763 	protocols = NFC_PROTO_JEWEL_MASK |
764 		    NFC_PROTO_MIFARE_MASK |
765 		    NFC_PROTO_FELICA_MASK |
766 		    NFC_PROTO_ISO14443_MASK |
767 		    NFC_PROTO_ISO14443_B_MASK |
768 		    NFC_PROTO_NFC_DEP_MASK |
769 		    NFC_PROTO_ISO15693_MASK;
770 
771 	ndev = nci_allocate_device(&nci_ops, protocols, tx_headroom,
772 				   tx_tailroom);
773 	if (!ndev) {
774 		nfc_err(dev, "Cannot allocate nfc ndev\n");
775 		return -ENOMEM;
776 	}
777 
778 	r = nci_register_device(ndev);
779 	if (r)
780 		goto err_regdev;
781 
782 	*ndevp = ndev;
783 	info->ndev = ndev;
784 
785 	nci_set_drvdata(ndev, info);
786 
787 	return 0;
788 
789 err_regdev:
790 	nci_free_device(ndev);
791 	return r;
792 }
793 EXPORT_SYMBOL(fdp_nci_probe);
794 
fdp_nci_remove(struct nci_dev * ndev)795 void fdp_nci_remove(struct nci_dev *ndev)
796 {
797 	struct fdp_nci_info *info = nci_get_drvdata(ndev);
798 	struct device *dev = &info->phy->i2c_dev->dev;
799 
800 	dev_dbg(dev, "%s\n", __func__);
801 
802 	nci_unregister_device(ndev);
803 	nci_free_device(ndev);
804 }
805 EXPORT_SYMBOL(fdp_nci_remove);
806 
807 MODULE_LICENSE("GPL");
808 MODULE_DESCRIPTION("NFC NCI driver for Intel Fields Peak NFC controller");
809 MODULE_AUTHOR("Robert Dolca <robert.dolca@intel.com>");
810