xref: /linux/drivers/nfc/pn533/pn533.c (revision e58e871becec2d3b04ed91c0c16fe8deac9c9dfa)
1 /*
2  * Driver for NXP PN533 NFC Chip - core functions
3  *
4  * Copyright (C) 2011 Instituto Nokia de Tecnologia
5  * Copyright (C) 2012-2013 Tieto Poland
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #include <linux/device.h>
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/slab.h>
25 #include <linux/nfc.h>
26 #include <linux/netdevice.h>
27 #include <net/nfc/nfc.h>
28 #include "pn533.h"
29 
30 #define VERSION "0.3"
31 
32 /* How much time we spend listening for initiators */
33 #define PN533_LISTEN_TIME 2
34 /* Delay between each poll frame (ms) */
35 #define PN533_POLL_INTERVAL 10
36 
37 /* structs for pn533 commands */
38 
39 /* PN533_CMD_GET_FIRMWARE_VERSION */
40 struct pn533_fw_version {
41 	u8 ic;
42 	u8 ver;
43 	u8 rev;
44 	u8 support;
45 };
46 
47 /* PN533_CMD_RF_CONFIGURATION */
48 #define PN533_CFGITEM_RF_FIELD    0x01
49 #define PN533_CFGITEM_TIMING      0x02
50 #define PN533_CFGITEM_MAX_RETRIES 0x05
51 #define PN533_CFGITEM_PASORI      0x82
52 
53 #define PN533_CFGITEM_RF_FIELD_AUTO_RFCA 0x2
54 #define PN533_CFGITEM_RF_FIELD_ON        0x1
55 #define PN533_CFGITEM_RF_FIELD_OFF       0x0
56 
57 #define PN533_CONFIG_TIMING_102 0xb
58 #define PN533_CONFIG_TIMING_204 0xc
59 #define PN533_CONFIG_TIMING_409 0xd
60 #define PN533_CONFIG_TIMING_819 0xe
61 
62 #define PN533_CONFIG_MAX_RETRIES_NO_RETRY 0x00
63 #define PN533_CONFIG_MAX_RETRIES_ENDLESS 0xFF
64 
65 struct pn533_config_max_retries {
66 	u8 mx_rty_atr;
67 	u8 mx_rty_psl;
68 	u8 mx_rty_passive_act;
69 } __packed;
70 
71 struct pn533_config_timing {
72 	u8 rfu;
73 	u8 atr_res_timeout;
74 	u8 dep_timeout;
75 } __packed;
76 
77 /* PN533_CMD_IN_LIST_PASSIVE_TARGET */
78 
79 /* felica commands opcode */
80 #define PN533_FELICA_OPC_SENSF_REQ 0
81 #define PN533_FELICA_OPC_SENSF_RES 1
82 /* felica SENSF_REQ parameters */
83 #define PN533_FELICA_SENSF_SC_ALL 0xFFFF
84 #define PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE 0
85 #define PN533_FELICA_SENSF_RC_SYSTEM_CODE 1
86 #define PN533_FELICA_SENSF_RC_ADVANCED_PROTOCOL 2
87 
88 /* type B initiator_data values */
89 #define PN533_TYPE_B_AFI_ALL_FAMILIES 0
90 #define PN533_TYPE_B_POLL_METHOD_TIMESLOT 0
91 #define PN533_TYPE_B_POLL_METHOD_PROBABILISTIC 1
92 
93 union pn533_cmd_poll_initdata {
94 	struct {
95 		u8 afi;
96 		u8 polling_method;
97 	} __packed type_b;
98 	struct {
99 		u8 opcode;
100 		__be16 sc;
101 		u8 rc;
102 		u8 tsn;
103 	} __packed felica;
104 };
105 
106 struct pn533_poll_modulations {
107 	struct {
108 		u8 maxtg;
109 		u8 brty;
110 		union pn533_cmd_poll_initdata initiator_data;
111 	} __packed data;
112 	u8 len;
113 };
114 
115 static const struct pn533_poll_modulations poll_mod[] = {
116 	[PN533_POLL_MOD_106KBPS_A] = {
117 		.data = {
118 			.maxtg = 1,
119 			.brty = 0,
120 		},
121 		.len = 2,
122 	},
123 	[PN533_POLL_MOD_212KBPS_FELICA] = {
124 		.data = {
125 			.maxtg = 1,
126 			.brty = 1,
127 			.initiator_data.felica = {
128 				.opcode = PN533_FELICA_OPC_SENSF_REQ,
129 				.sc = PN533_FELICA_SENSF_SC_ALL,
130 				.rc = PN533_FELICA_SENSF_RC_SYSTEM_CODE,
131 				.tsn = 0x03,
132 			},
133 		},
134 		.len = 7,
135 	},
136 	[PN533_POLL_MOD_424KBPS_FELICA] = {
137 		.data = {
138 			.maxtg = 1,
139 			.brty = 2,
140 			.initiator_data.felica = {
141 				.opcode = PN533_FELICA_OPC_SENSF_REQ,
142 				.sc = PN533_FELICA_SENSF_SC_ALL,
143 				.rc = PN533_FELICA_SENSF_RC_SYSTEM_CODE,
144 				.tsn = 0x03,
145 			},
146 		 },
147 		.len = 7,
148 	},
149 	[PN533_POLL_MOD_106KBPS_JEWEL] = {
150 		.data = {
151 			.maxtg = 1,
152 			.brty = 4,
153 		},
154 		.len = 2,
155 	},
156 	[PN533_POLL_MOD_847KBPS_B] = {
157 		.data = {
158 			.maxtg = 1,
159 			.brty = 8,
160 			.initiator_data.type_b = {
161 				.afi = PN533_TYPE_B_AFI_ALL_FAMILIES,
162 				.polling_method =
163 					PN533_TYPE_B_POLL_METHOD_TIMESLOT,
164 			},
165 		},
166 		.len = 3,
167 	},
168 	[PN533_LISTEN_MOD] = {
169 		.len = 0,
170 	},
171 };
172 
173 /* PN533_CMD_IN_ATR */
174 
175 struct pn533_cmd_activate_response {
176 	u8 status;
177 	u8 nfcid3t[10];
178 	u8 didt;
179 	u8 bst;
180 	u8 brt;
181 	u8 to;
182 	u8 ppt;
183 	/* optional */
184 	u8 gt[];
185 } __packed;
186 
187 struct pn533_cmd_jump_dep_response {
188 	u8 status;
189 	u8 tg;
190 	u8 nfcid3t[10];
191 	u8 didt;
192 	u8 bst;
193 	u8 brt;
194 	u8 to;
195 	u8 ppt;
196 	/* optional */
197 	u8 gt[];
198 } __packed;
199 
200 
201 /* PN533_TG_INIT_AS_TARGET */
202 #define PN533_INIT_TARGET_PASSIVE 0x1
203 #define PN533_INIT_TARGET_DEP 0x2
204 
205 #define PN533_INIT_TARGET_RESP_FRAME_MASK 0x3
206 #define PN533_INIT_TARGET_RESP_ACTIVE     0x1
207 #define PN533_INIT_TARGET_RESP_DEP        0x4
208 
209 /* The rule: value(high byte) + value(low byte) + checksum = 0 */
210 static inline u8 pn533_ext_checksum(u16 value)
211 {
212 	return ~(u8)(((value & 0xFF00) >> 8) + (u8)(value & 0xFF)) + 1;
213 }
214 
215 /* The rule: value + checksum = 0 */
216 static inline u8 pn533_std_checksum(u8 value)
217 {
218 	return ~value + 1;
219 }
220 
221 /* The rule: sum(data elements) + checksum = 0 */
222 static u8 pn533_std_data_checksum(u8 *data, int datalen)
223 {
224 	u8 sum = 0;
225 	int i;
226 
227 	for (i = 0; i < datalen; i++)
228 		sum += data[i];
229 
230 	return pn533_std_checksum(sum);
231 }
232 
233 static void pn533_std_tx_frame_init(void *_frame, u8 cmd_code)
234 {
235 	struct pn533_std_frame *frame = _frame;
236 
237 	frame->preamble = 0;
238 	frame->start_frame = cpu_to_be16(PN533_STD_FRAME_SOF);
239 	PN533_STD_FRAME_IDENTIFIER(frame) = PN533_STD_FRAME_DIR_OUT;
240 	PN533_FRAME_CMD(frame) = cmd_code;
241 	frame->datalen = 2;
242 }
243 
244 static void pn533_std_tx_frame_finish(void *_frame)
245 {
246 	struct pn533_std_frame *frame = _frame;
247 
248 	frame->datalen_checksum = pn533_std_checksum(frame->datalen);
249 
250 	PN533_STD_FRAME_CHECKSUM(frame) =
251 		pn533_std_data_checksum(frame->data, frame->datalen);
252 
253 	PN533_STD_FRAME_POSTAMBLE(frame) = 0;
254 }
255 
256 static void pn533_std_tx_update_payload_len(void *_frame, int len)
257 {
258 	struct pn533_std_frame *frame = _frame;
259 
260 	frame->datalen += len;
261 }
262 
263 static bool pn533_std_rx_frame_is_valid(void *_frame, struct pn533 *dev)
264 {
265 	u8 checksum;
266 	struct pn533_std_frame *stdf = _frame;
267 
268 	if (stdf->start_frame != cpu_to_be16(PN533_STD_FRAME_SOF))
269 		return false;
270 
271 	if (likely(!PN533_STD_IS_EXTENDED(stdf))) {
272 		/* Standard frame code */
273 		dev->ops->rx_header_len = PN533_STD_FRAME_HEADER_LEN;
274 
275 		checksum = pn533_std_checksum(stdf->datalen);
276 		if (checksum != stdf->datalen_checksum)
277 			return false;
278 
279 		checksum = pn533_std_data_checksum(stdf->data, stdf->datalen);
280 		if (checksum != PN533_STD_FRAME_CHECKSUM(stdf))
281 			return false;
282 	} else {
283 		/* Extended */
284 		struct pn533_ext_frame *eif = _frame;
285 
286 		dev->ops->rx_header_len = PN533_EXT_FRAME_HEADER_LEN;
287 
288 		checksum = pn533_ext_checksum(be16_to_cpu(eif->datalen));
289 		if (checksum != eif->datalen_checksum)
290 			return false;
291 
292 		/* check data checksum */
293 		checksum = pn533_std_data_checksum(eif->data,
294 						   be16_to_cpu(eif->datalen));
295 		if (checksum != PN533_EXT_FRAME_CHECKSUM(eif))
296 			return false;
297 	}
298 
299 	return true;
300 }
301 
302 bool pn533_rx_frame_is_ack(void *_frame)
303 {
304 	struct pn533_std_frame *frame = _frame;
305 
306 	if (frame->start_frame != cpu_to_be16(PN533_STD_FRAME_SOF))
307 		return false;
308 
309 	if (frame->datalen != 0 || frame->datalen_checksum != 0xFF)
310 		return false;
311 
312 	return true;
313 }
314 EXPORT_SYMBOL_GPL(pn533_rx_frame_is_ack);
315 
316 static inline int pn533_std_rx_frame_size(void *frame)
317 {
318 	struct pn533_std_frame *f = frame;
319 
320 	/* check for Extended Information frame */
321 	if (PN533_STD_IS_EXTENDED(f)) {
322 		struct pn533_ext_frame *eif = frame;
323 
324 		return sizeof(struct pn533_ext_frame)
325 			+ be16_to_cpu(eif->datalen) + PN533_STD_FRAME_TAIL_LEN;
326 	}
327 
328 	return sizeof(struct pn533_std_frame) + f->datalen +
329 	       PN533_STD_FRAME_TAIL_LEN;
330 }
331 
332 static u8 pn533_std_get_cmd_code(void *frame)
333 {
334 	struct pn533_std_frame *f = frame;
335 	struct pn533_ext_frame *eif = frame;
336 
337 	if (PN533_STD_IS_EXTENDED(f))
338 		return PN533_FRAME_CMD(eif);
339 	else
340 		return PN533_FRAME_CMD(f);
341 }
342 
343 bool pn533_rx_frame_is_cmd_response(struct pn533 *dev, void *frame)
344 {
345 	return (dev->ops->get_cmd_code(frame) ==
346 				PN533_CMD_RESPONSE(dev->cmd->code));
347 }
348 EXPORT_SYMBOL_GPL(pn533_rx_frame_is_cmd_response);
349 
350 
351 static struct pn533_frame_ops pn533_std_frame_ops = {
352 	.tx_frame_init = pn533_std_tx_frame_init,
353 	.tx_frame_finish = pn533_std_tx_frame_finish,
354 	.tx_update_payload_len = pn533_std_tx_update_payload_len,
355 	.tx_header_len = PN533_STD_FRAME_HEADER_LEN,
356 	.tx_tail_len = PN533_STD_FRAME_TAIL_LEN,
357 
358 	.rx_is_frame_valid = pn533_std_rx_frame_is_valid,
359 	.rx_frame_size = pn533_std_rx_frame_size,
360 	.rx_header_len = PN533_STD_FRAME_HEADER_LEN,
361 	.rx_tail_len = PN533_STD_FRAME_TAIL_LEN,
362 
363 	.max_payload_len =  PN533_STD_FRAME_MAX_PAYLOAD_LEN,
364 	.get_cmd_code = pn533_std_get_cmd_code,
365 };
366 
367 static void pn533_build_cmd_frame(struct pn533 *dev, u8 cmd_code,
368 				  struct sk_buff *skb)
369 {
370 	/* payload is already there, just update datalen */
371 	int payload_len = skb->len;
372 	struct pn533_frame_ops *ops = dev->ops;
373 
374 
375 	skb_push(skb, ops->tx_header_len);
376 	skb_put(skb, ops->tx_tail_len);
377 
378 	ops->tx_frame_init(skb->data, cmd_code);
379 	ops->tx_update_payload_len(skb->data, payload_len);
380 	ops->tx_frame_finish(skb->data);
381 }
382 
383 static int pn533_send_async_complete(struct pn533 *dev)
384 {
385 	struct pn533_cmd *cmd = dev->cmd;
386 	struct sk_buff *resp;
387 	int status, rc = 0;
388 
389 	if (!cmd) {
390 		dev_dbg(dev->dev, "%s: cmd not set\n", __func__);
391 		goto done;
392 	}
393 
394 	dev_kfree_skb(cmd->req);
395 
396 	status = cmd->status;
397 	resp = cmd->resp;
398 
399 	if (status < 0) {
400 		rc = cmd->complete_cb(dev, cmd->complete_cb_context,
401 				      ERR_PTR(status));
402 		dev_kfree_skb(resp);
403 		goto done;
404 	}
405 
406 	/* when no response is set we got interrupted */
407 	if (!resp)
408 		resp = ERR_PTR(-EINTR);
409 
410 	if (!IS_ERR(resp)) {
411 		skb_pull(resp, dev->ops->rx_header_len);
412 		skb_trim(resp, resp->len - dev->ops->rx_tail_len);
413 	}
414 
415 	rc = cmd->complete_cb(dev, cmd->complete_cb_context, resp);
416 
417 done:
418 	kfree(cmd);
419 	dev->cmd = NULL;
420 	return rc;
421 }
422 
423 static int __pn533_send_async(struct pn533 *dev, u8 cmd_code,
424 			      struct sk_buff *req,
425 			      pn533_send_async_complete_t complete_cb,
426 			      void *complete_cb_context)
427 {
428 	struct pn533_cmd *cmd;
429 	int rc = 0;
430 
431 	dev_dbg(dev->dev, "Sending command 0x%x\n", cmd_code);
432 
433 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
434 	if (!cmd)
435 		return -ENOMEM;
436 
437 	cmd->code = cmd_code;
438 	cmd->req = req;
439 	cmd->complete_cb = complete_cb;
440 	cmd->complete_cb_context = complete_cb_context;
441 
442 	pn533_build_cmd_frame(dev, cmd_code, req);
443 
444 	mutex_lock(&dev->cmd_lock);
445 
446 	if (!dev->cmd_pending) {
447 		dev->cmd = cmd;
448 		rc = dev->phy_ops->send_frame(dev, req);
449 		if (rc) {
450 			dev->cmd = NULL;
451 			goto error;
452 		}
453 
454 		dev->cmd_pending = 1;
455 		goto unlock;
456 	}
457 
458 	dev_dbg(dev->dev, "%s Queueing command 0x%x\n",
459 		__func__, cmd_code);
460 
461 	INIT_LIST_HEAD(&cmd->queue);
462 	list_add_tail(&cmd->queue, &dev->cmd_queue);
463 
464 	goto unlock;
465 
466 error:
467 	kfree(cmd);
468 unlock:
469 	mutex_unlock(&dev->cmd_lock);
470 	return rc;
471 }
472 
473 static int pn533_send_data_async(struct pn533 *dev, u8 cmd_code,
474 				 struct sk_buff *req,
475 				 pn533_send_async_complete_t complete_cb,
476 				 void *complete_cb_context)
477 {
478 	int rc;
479 
480 	rc = __pn533_send_async(dev, cmd_code, req, complete_cb,
481 				complete_cb_context);
482 
483 	return rc;
484 }
485 
486 static int pn533_send_cmd_async(struct pn533 *dev, u8 cmd_code,
487 				struct sk_buff *req,
488 				pn533_send_async_complete_t complete_cb,
489 				void *complete_cb_context)
490 {
491 	int rc;
492 
493 	rc = __pn533_send_async(dev, cmd_code, req, complete_cb,
494 				complete_cb_context);
495 
496 	return rc;
497 }
498 
499 /*
500  * pn533_send_cmd_direct_async
501  *
502  * The function sends a piority cmd directly to the chip omitting the cmd
503  * queue. It's intended to be used by chaining mechanism of received responses
504  * where the host has to request every single chunk of data before scheduling
505  * next cmd from the queue.
506  */
507 static int pn533_send_cmd_direct_async(struct pn533 *dev, u8 cmd_code,
508 				       struct sk_buff *req,
509 				       pn533_send_async_complete_t complete_cb,
510 				       void *complete_cb_context)
511 {
512 	struct pn533_cmd *cmd;
513 	int rc;
514 
515 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
516 	if (!cmd)
517 		return -ENOMEM;
518 
519 	cmd->code = cmd_code;
520 	cmd->req = req;
521 	cmd->complete_cb = complete_cb;
522 	cmd->complete_cb_context = complete_cb_context;
523 
524 	pn533_build_cmd_frame(dev, cmd_code, req);
525 
526 	dev->cmd = cmd;
527 	rc = dev->phy_ops->send_frame(dev, req);
528 	if (rc < 0) {
529 		dev->cmd = NULL;
530 		kfree(cmd);
531 	}
532 
533 	return rc;
534 }
535 
536 static void pn533_wq_cmd_complete(struct work_struct *work)
537 {
538 	struct pn533 *dev = container_of(work, struct pn533, cmd_complete_work);
539 	int rc;
540 
541 	rc = pn533_send_async_complete(dev);
542 	if (rc != -EINPROGRESS)
543 		queue_work(dev->wq, &dev->cmd_work);
544 }
545 
546 static void pn533_wq_cmd(struct work_struct *work)
547 {
548 	struct pn533 *dev = container_of(work, struct pn533, cmd_work);
549 	struct pn533_cmd *cmd;
550 	int rc;
551 
552 	mutex_lock(&dev->cmd_lock);
553 
554 	if (list_empty(&dev->cmd_queue)) {
555 		dev->cmd_pending = 0;
556 		mutex_unlock(&dev->cmd_lock);
557 		return;
558 	}
559 
560 	cmd = list_first_entry(&dev->cmd_queue, struct pn533_cmd, queue);
561 
562 	list_del(&cmd->queue);
563 
564 	mutex_unlock(&dev->cmd_lock);
565 
566 	dev->cmd = cmd;
567 	rc = dev->phy_ops->send_frame(dev, cmd->req);
568 	if (rc < 0) {
569 		dev->cmd = NULL;
570 		dev_kfree_skb(cmd->req);
571 		kfree(cmd);
572 		return;
573 	}
574 
575 }
576 
577 struct pn533_sync_cmd_response {
578 	struct sk_buff *resp;
579 	struct completion done;
580 };
581 
582 static int pn533_send_sync_complete(struct pn533 *dev, void *_arg,
583 				    struct sk_buff *resp)
584 {
585 	struct pn533_sync_cmd_response *arg = _arg;
586 
587 	arg->resp = resp;
588 	complete(&arg->done);
589 
590 	return 0;
591 }
592 
593 /*  pn533_send_cmd_sync
594  *
595  *  Please note the req parameter is freed inside the function to
596  *  limit a number of return value interpretations by the caller.
597  *
598  *  1. negative in case of error during TX path -> req should be freed
599  *
600  *  2. negative in case of error during RX path -> req should not be freed
601  *     as it's been already freed at the beginning of RX path by
602  *     async_complete_cb.
603  *
604  *  3. valid pointer in case of succesfult RX path
605  *
606  *  A caller has to check a return value with IS_ERR macro. If the test pass,
607  *  the returned pointer is valid.
608  *
609  */
610 static struct sk_buff *pn533_send_cmd_sync(struct pn533 *dev, u8 cmd_code,
611 					       struct sk_buff *req)
612 {
613 	int rc;
614 	struct pn533_sync_cmd_response arg;
615 
616 	init_completion(&arg.done);
617 
618 	rc = pn533_send_cmd_async(dev, cmd_code, req,
619 				  pn533_send_sync_complete, &arg);
620 	if (rc) {
621 		dev_kfree_skb(req);
622 		return ERR_PTR(rc);
623 	}
624 
625 	wait_for_completion(&arg.done);
626 
627 	return arg.resp;
628 }
629 
630 static struct sk_buff *pn533_alloc_skb(struct pn533 *dev, unsigned int size)
631 {
632 	struct sk_buff *skb;
633 
634 	skb = alloc_skb(dev->ops->tx_header_len +
635 			size +
636 			dev->ops->tx_tail_len, GFP_KERNEL);
637 
638 	if (skb)
639 		skb_reserve(skb, dev->ops->tx_header_len);
640 
641 	return skb;
642 }
643 
644 struct pn533_target_type_a {
645 	__be16 sens_res;
646 	u8 sel_res;
647 	u8 nfcid_len;
648 	u8 nfcid_data[];
649 } __packed;
650 
651 
652 #define PN533_TYPE_A_SENS_RES_NFCID1(x) ((u8)((be16_to_cpu(x) & 0x00C0) >> 6))
653 #define PN533_TYPE_A_SENS_RES_SSD(x) ((u8)((be16_to_cpu(x) & 0x001F) >> 0))
654 #define PN533_TYPE_A_SENS_RES_PLATCONF(x) ((u8)((be16_to_cpu(x) & 0x0F00) >> 8))
655 
656 #define PN533_TYPE_A_SENS_RES_SSD_JEWEL 0x00
657 #define PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL 0x0C
658 
659 #define PN533_TYPE_A_SEL_PROT(x) (((x) & 0x60) >> 5)
660 #define PN533_TYPE_A_SEL_CASCADE(x) (((x) & 0x04) >> 2)
661 
662 #define PN533_TYPE_A_SEL_PROT_MIFARE 0
663 #define PN533_TYPE_A_SEL_PROT_ISO14443 1
664 #define PN533_TYPE_A_SEL_PROT_DEP 2
665 #define PN533_TYPE_A_SEL_PROT_ISO14443_DEP 3
666 
667 static bool pn533_target_type_a_is_valid(struct pn533_target_type_a *type_a,
668 							int target_data_len)
669 {
670 	u8 ssd;
671 	u8 platconf;
672 
673 	if (target_data_len < sizeof(struct pn533_target_type_a))
674 		return false;
675 
676 	/*
677 	 * The length check of nfcid[] and ats[] are not being performed because
678 	 * the values are not being used
679 	 */
680 
681 	/* Requirement 4.6.3.3 from NFC Forum Digital Spec */
682 	ssd = PN533_TYPE_A_SENS_RES_SSD(type_a->sens_res);
683 	platconf = PN533_TYPE_A_SENS_RES_PLATCONF(type_a->sens_res);
684 
685 	if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
686 	     platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
687 	    (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
688 	     platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
689 		return false;
690 
691 	/* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */
692 	if (PN533_TYPE_A_SEL_CASCADE(type_a->sel_res) != 0)
693 		return false;
694 
695 	return true;
696 }
697 
698 static int pn533_target_found_type_a(struct nfc_target *nfc_tgt, u8 *tgt_data,
699 							int tgt_data_len)
700 {
701 	struct pn533_target_type_a *tgt_type_a;
702 
703 	tgt_type_a = (struct pn533_target_type_a *)tgt_data;
704 
705 	if (!pn533_target_type_a_is_valid(tgt_type_a, tgt_data_len))
706 		return -EPROTO;
707 
708 	switch (PN533_TYPE_A_SEL_PROT(tgt_type_a->sel_res)) {
709 	case PN533_TYPE_A_SEL_PROT_MIFARE:
710 		nfc_tgt->supported_protocols = NFC_PROTO_MIFARE_MASK;
711 		break;
712 	case PN533_TYPE_A_SEL_PROT_ISO14443:
713 		nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK;
714 		break;
715 	case PN533_TYPE_A_SEL_PROT_DEP:
716 		nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
717 		break;
718 	case PN533_TYPE_A_SEL_PROT_ISO14443_DEP:
719 		nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK |
720 							NFC_PROTO_NFC_DEP_MASK;
721 		break;
722 	}
723 
724 	nfc_tgt->sens_res = be16_to_cpu(tgt_type_a->sens_res);
725 	nfc_tgt->sel_res = tgt_type_a->sel_res;
726 	nfc_tgt->nfcid1_len = tgt_type_a->nfcid_len;
727 	memcpy(nfc_tgt->nfcid1, tgt_type_a->nfcid_data, nfc_tgt->nfcid1_len);
728 
729 	return 0;
730 }
731 
732 struct pn533_target_felica {
733 	u8 pol_res;
734 	u8 opcode;
735 	u8 nfcid2[NFC_NFCID2_MAXSIZE];
736 	u8 pad[8];
737 	/* optional */
738 	u8 syst_code[];
739 } __packed;
740 
741 #define PN533_FELICA_SENSF_NFCID2_DEP_B1 0x01
742 #define PN533_FELICA_SENSF_NFCID2_DEP_B2 0xFE
743 
744 static bool pn533_target_felica_is_valid(struct pn533_target_felica *felica,
745 							int target_data_len)
746 {
747 	if (target_data_len < sizeof(struct pn533_target_felica))
748 		return false;
749 
750 	if (felica->opcode != PN533_FELICA_OPC_SENSF_RES)
751 		return false;
752 
753 	return true;
754 }
755 
756 static int pn533_target_found_felica(struct nfc_target *nfc_tgt, u8 *tgt_data,
757 							int tgt_data_len)
758 {
759 	struct pn533_target_felica *tgt_felica;
760 
761 	tgt_felica = (struct pn533_target_felica *)tgt_data;
762 
763 	if (!pn533_target_felica_is_valid(tgt_felica, tgt_data_len))
764 		return -EPROTO;
765 
766 	if ((tgt_felica->nfcid2[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1) &&
767 	    (tgt_felica->nfcid2[1] == PN533_FELICA_SENSF_NFCID2_DEP_B2))
768 		nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
769 	else
770 		nfc_tgt->supported_protocols = NFC_PROTO_FELICA_MASK;
771 
772 	memcpy(nfc_tgt->sensf_res, &tgt_felica->opcode, 9);
773 	nfc_tgt->sensf_res_len = 9;
774 
775 	memcpy(nfc_tgt->nfcid2, tgt_felica->nfcid2, NFC_NFCID2_MAXSIZE);
776 	nfc_tgt->nfcid2_len = NFC_NFCID2_MAXSIZE;
777 
778 	return 0;
779 }
780 
781 struct pn533_target_jewel {
782 	__be16 sens_res;
783 	u8 jewelid[4];
784 } __packed;
785 
786 static bool pn533_target_jewel_is_valid(struct pn533_target_jewel *jewel,
787 							int target_data_len)
788 {
789 	u8 ssd;
790 	u8 platconf;
791 
792 	if (target_data_len < sizeof(struct pn533_target_jewel))
793 		return false;
794 
795 	/* Requirement 4.6.3.3 from NFC Forum Digital Spec */
796 	ssd = PN533_TYPE_A_SENS_RES_SSD(jewel->sens_res);
797 	platconf = PN533_TYPE_A_SENS_RES_PLATCONF(jewel->sens_res);
798 
799 	if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
800 	     platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
801 	    (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
802 	     platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
803 		return false;
804 
805 	return true;
806 }
807 
808 static int pn533_target_found_jewel(struct nfc_target *nfc_tgt, u8 *tgt_data,
809 							int tgt_data_len)
810 {
811 	struct pn533_target_jewel *tgt_jewel;
812 
813 	tgt_jewel = (struct pn533_target_jewel *)tgt_data;
814 
815 	if (!pn533_target_jewel_is_valid(tgt_jewel, tgt_data_len))
816 		return -EPROTO;
817 
818 	nfc_tgt->supported_protocols = NFC_PROTO_JEWEL_MASK;
819 	nfc_tgt->sens_res = be16_to_cpu(tgt_jewel->sens_res);
820 	nfc_tgt->nfcid1_len = 4;
821 	memcpy(nfc_tgt->nfcid1, tgt_jewel->jewelid, nfc_tgt->nfcid1_len);
822 
823 	return 0;
824 }
825 
826 struct pn533_type_b_prot_info {
827 	u8 bitrate;
828 	u8 fsci_type;
829 	u8 fwi_adc_fo;
830 } __packed;
831 
832 #define PN533_TYPE_B_PROT_FCSI(x) (((x) & 0xF0) >> 4)
833 #define PN533_TYPE_B_PROT_TYPE(x) (((x) & 0x0F) >> 0)
834 #define PN533_TYPE_B_PROT_TYPE_RFU_MASK 0x8
835 
836 struct pn533_type_b_sens_res {
837 	u8 opcode;
838 	u8 nfcid[4];
839 	u8 appdata[4];
840 	struct pn533_type_b_prot_info prot_info;
841 } __packed;
842 
843 #define PN533_TYPE_B_OPC_SENSB_RES 0x50
844 
845 struct pn533_target_type_b {
846 	struct pn533_type_b_sens_res sensb_res;
847 	u8 attrib_res_len;
848 	u8 attrib_res[];
849 } __packed;
850 
851 static bool pn533_target_type_b_is_valid(struct pn533_target_type_b *type_b,
852 							int target_data_len)
853 {
854 	if (target_data_len < sizeof(struct pn533_target_type_b))
855 		return false;
856 
857 	if (type_b->sensb_res.opcode != PN533_TYPE_B_OPC_SENSB_RES)
858 		return false;
859 
860 	if (PN533_TYPE_B_PROT_TYPE(type_b->sensb_res.prot_info.fsci_type) &
861 						PN533_TYPE_B_PROT_TYPE_RFU_MASK)
862 		return false;
863 
864 	return true;
865 }
866 
867 static int pn533_target_found_type_b(struct nfc_target *nfc_tgt, u8 *tgt_data,
868 							int tgt_data_len)
869 {
870 	struct pn533_target_type_b *tgt_type_b;
871 
872 	tgt_type_b = (struct pn533_target_type_b *)tgt_data;
873 
874 	if (!pn533_target_type_b_is_valid(tgt_type_b, tgt_data_len))
875 		return -EPROTO;
876 
877 	nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_B_MASK;
878 
879 	return 0;
880 }
881 
882 static void pn533_poll_reset_mod_list(struct pn533 *dev);
883 static int pn533_target_found(struct pn533 *dev, u8 tg, u8 *tgdata,
884 			      int tgdata_len)
885 {
886 	struct nfc_target nfc_tgt;
887 	int rc;
888 
889 	dev_dbg(dev->dev, "%s: modulation=%d\n",
890 		__func__, dev->poll_mod_curr);
891 
892 	if (tg != 1)
893 		return -EPROTO;
894 
895 	memset(&nfc_tgt, 0, sizeof(struct nfc_target));
896 
897 	switch (dev->poll_mod_curr) {
898 	case PN533_POLL_MOD_106KBPS_A:
899 		rc = pn533_target_found_type_a(&nfc_tgt, tgdata, tgdata_len);
900 		break;
901 	case PN533_POLL_MOD_212KBPS_FELICA:
902 	case PN533_POLL_MOD_424KBPS_FELICA:
903 		rc = pn533_target_found_felica(&nfc_tgt, tgdata, tgdata_len);
904 		break;
905 	case PN533_POLL_MOD_106KBPS_JEWEL:
906 		rc = pn533_target_found_jewel(&nfc_tgt, tgdata, tgdata_len);
907 		break;
908 	case PN533_POLL_MOD_847KBPS_B:
909 		rc = pn533_target_found_type_b(&nfc_tgt, tgdata, tgdata_len);
910 		break;
911 	default:
912 		nfc_err(dev->dev,
913 			"Unknown current poll modulation\n");
914 		return -EPROTO;
915 	}
916 
917 	if (rc)
918 		return rc;
919 
920 	if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) {
921 		dev_dbg(dev->dev,
922 			"The Tg found doesn't have the desired protocol\n");
923 		return -EAGAIN;
924 	}
925 
926 	dev_dbg(dev->dev,
927 		"Target found - supported protocols: 0x%x\n",
928 		nfc_tgt.supported_protocols);
929 
930 	dev->tgt_available_prots = nfc_tgt.supported_protocols;
931 
932 	pn533_poll_reset_mod_list(dev);
933 	nfc_targets_found(dev->nfc_dev, &nfc_tgt, 1);
934 
935 	return 0;
936 }
937 
938 static inline void pn533_poll_next_mod(struct pn533 *dev)
939 {
940 	dev->poll_mod_curr = (dev->poll_mod_curr + 1) % dev->poll_mod_count;
941 }
942 
943 static void pn533_poll_reset_mod_list(struct pn533 *dev)
944 {
945 	dev->poll_mod_count = 0;
946 }
947 
948 static void pn533_poll_add_mod(struct pn533 *dev, u8 mod_index)
949 {
950 	dev->poll_mod_active[dev->poll_mod_count] =
951 		(struct pn533_poll_modulations *)&poll_mod[mod_index];
952 	dev->poll_mod_count++;
953 }
954 
955 static void pn533_poll_create_mod_list(struct pn533 *dev,
956 				       u32 im_protocols, u32 tm_protocols)
957 {
958 	pn533_poll_reset_mod_list(dev);
959 
960 	if ((im_protocols & NFC_PROTO_MIFARE_MASK) ||
961 	    (im_protocols & NFC_PROTO_ISO14443_MASK) ||
962 	    (im_protocols & NFC_PROTO_NFC_DEP_MASK))
963 		pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_A);
964 
965 	if (im_protocols & NFC_PROTO_FELICA_MASK ||
966 	    im_protocols & NFC_PROTO_NFC_DEP_MASK) {
967 		pn533_poll_add_mod(dev, PN533_POLL_MOD_212KBPS_FELICA);
968 		pn533_poll_add_mod(dev, PN533_POLL_MOD_424KBPS_FELICA);
969 	}
970 
971 	if (im_protocols & NFC_PROTO_JEWEL_MASK)
972 		pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_JEWEL);
973 
974 	if (im_protocols & NFC_PROTO_ISO14443_B_MASK)
975 		pn533_poll_add_mod(dev, PN533_POLL_MOD_847KBPS_B);
976 
977 	if (tm_protocols)
978 		pn533_poll_add_mod(dev, PN533_LISTEN_MOD);
979 }
980 
981 static int pn533_start_poll_complete(struct pn533 *dev, struct sk_buff *resp)
982 {
983 	u8 nbtg, tg, *tgdata;
984 	int rc, tgdata_len;
985 
986 	/* Toggle the DEP polling */
987 	if (dev->poll_protocols & NFC_PROTO_NFC_DEP_MASK)
988 		dev->poll_dep = 1;
989 
990 	nbtg = resp->data[0];
991 	tg = resp->data[1];
992 	tgdata = &resp->data[2];
993 	tgdata_len = resp->len - 2;  /* nbtg + tg */
994 
995 	if (nbtg) {
996 		rc = pn533_target_found(dev, tg, tgdata, tgdata_len);
997 
998 		/* We must stop the poll after a valid target found */
999 		if (rc == 0)
1000 			return 0;
1001 	}
1002 
1003 	return -EAGAIN;
1004 }
1005 
1006 static struct sk_buff *pn533_alloc_poll_tg_frame(struct pn533 *dev)
1007 {
1008 	struct sk_buff *skb;
1009 	u8 *felica, *nfcid3, *gb;
1010 
1011 	u8 *gbytes = dev->gb;
1012 	size_t gbytes_len = dev->gb_len;
1013 
1014 	u8 felica_params[18] = {0x1, 0xfe, /* DEP */
1015 				0x0, 0x0, 0x0, 0x0, 0x0, 0x0, /* random */
1016 				0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
1017 				0xff, 0xff}; /* System code */
1018 
1019 	u8 mifare_params[6] = {0x1, 0x1, /* SENS_RES */
1020 			       0x0, 0x0, 0x0,
1021 			       0x40}; /* SEL_RES for DEP */
1022 
1023 	unsigned int skb_len = 36 + /*
1024 				     * mode (1), mifare (6),
1025 				     * felica (18), nfcid3 (10), gb_len (1)
1026 				     */
1027 			       gbytes_len +
1028 			       1;  /* len Tk*/
1029 
1030 	skb = pn533_alloc_skb(dev, skb_len);
1031 	if (!skb)
1032 		return NULL;
1033 
1034 	/* DEP support only */
1035 	*skb_put(skb, 1) = PN533_INIT_TARGET_DEP;
1036 
1037 	/* MIFARE params */
1038 	memcpy(skb_put(skb, 6), mifare_params, 6);
1039 
1040 	/* Felica params */
1041 	felica = skb_put(skb, 18);
1042 	memcpy(felica, felica_params, 18);
1043 	get_random_bytes(felica + 2, 6);
1044 
1045 	/* NFCID3 */
1046 	nfcid3 = skb_put(skb, 10);
1047 	memset(nfcid3, 0, 10);
1048 	memcpy(nfcid3, felica, 8);
1049 
1050 	/* General bytes */
1051 	*skb_put(skb, 1) = gbytes_len;
1052 
1053 	gb = skb_put(skb, gbytes_len);
1054 	memcpy(gb, gbytes, gbytes_len);
1055 
1056 	/* Len Tk */
1057 	*skb_put(skb, 1) = 0;
1058 
1059 	return skb;
1060 }
1061 
1062 static void pn533_wq_tm_mi_recv(struct work_struct *work);
1063 static struct sk_buff *pn533_build_response(struct pn533 *dev);
1064 
1065 static int pn533_tm_get_data_complete(struct pn533 *dev, void *arg,
1066 				      struct sk_buff *resp)
1067 {
1068 	struct sk_buff *skb;
1069 	u8 status, ret, mi;
1070 	int rc;
1071 
1072 	dev_dbg(dev->dev, "%s\n", __func__);
1073 
1074 	if (IS_ERR(resp)) {
1075 		skb_queue_purge(&dev->resp_q);
1076 		return PTR_ERR(resp);
1077 	}
1078 
1079 	status = resp->data[0];
1080 
1081 	ret = status & PN533_CMD_RET_MASK;
1082 	mi = status & PN533_CMD_MI_MASK;
1083 
1084 	skb_pull(resp, sizeof(status));
1085 
1086 	if (ret != PN533_CMD_RET_SUCCESS) {
1087 		rc = -EIO;
1088 		goto error;
1089 	}
1090 
1091 	skb_queue_tail(&dev->resp_q, resp);
1092 
1093 	if (mi) {
1094 		queue_work(dev->wq, &dev->mi_tm_rx_work);
1095 		return -EINPROGRESS;
1096 	}
1097 
1098 	skb = pn533_build_response(dev);
1099 	if (!skb) {
1100 		rc = -EIO;
1101 		goto error;
1102 	}
1103 
1104 	return nfc_tm_data_received(dev->nfc_dev, skb);
1105 
1106 error:
1107 	nfc_tm_deactivated(dev->nfc_dev);
1108 	dev->tgt_mode = 0;
1109 	skb_queue_purge(&dev->resp_q);
1110 	dev_kfree_skb(resp);
1111 
1112 	return rc;
1113 }
1114 
1115 static void pn533_wq_tm_mi_recv(struct work_struct *work)
1116 {
1117 	struct pn533 *dev = container_of(work, struct pn533, mi_tm_rx_work);
1118 	struct sk_buff *skb;
1119 	int rc;
1120 
1121 	dev_dbg(dev->dev, "%s\n", __func__);
1122 
1123 	skb = pn533_alloc_skb(dev, 0);
1124 	if (!skb)
1125 		return;
1126 
1127 	rc = pn533_send_cmd_direct_async(dev,
1128 					PN533_CMD_TG_GET_DATA,
1129 					skb,
1130 					pn533_tm_get_data_complete,
1131 					NULL);
1132 
1133 	if (rc < 0)
1134 		dev_kfree_skb(skb);
1135 }
1136 
1137 static int pn533_tm_send_complete(struct pn533 *dev, void *arg,
1138 				  struct sk_buff *resp);
1139 static void pn533_wq_tm_mi_send(struct work_struct *work)
1140 {
1141 	struct pn533 *dev = container_of(work, struct pn533, mi_tm_tx_work);
1142 	struct sk_buff *skb;
1143 	int rc;
1144 
1145 	dev_dbg(dev->dev, "%s\n", __func__);
1146 
1147 	/* Grab the first skb in the queue */
1148 	skb = skb_dequeue(&dev->fragment_skb);
1149 	if (skb == NULL) {	/* No more data */
1150 		/* Reset the queue for future use */
1151 		skb_queue_head_init(&dev->fragment_skb);
1152 		goto error;
1153 	}
1154 
1155 	/* last entry - remove MI bit */
1156 	if (skb_queue_len(&dev->fragment_skb) == 0) {
1157 		rc = pn533_send_cmd_direct_async(dev, PN533_CMD_TG_SET_DATA,
1158 					skb, pn533_tm_send_complete, NULL);
1159 	} else
1160 		rc = pn533_send_cmd_direct_async(dev,
1161 					PN533_CMD_TG_SET_META_DATA,
1162 					skb, pn533_tm_send_complete, NULL);
1163 
1164 	if (rc == 0) /* success */
1165 		return;
1166 
1167 	dev_err(dev->dev,
1168 		"Error %d when trying to perform set meta data_exchange", rc);
1169 
1170 	dev_kfree_skb(skb);
1171 
1172 error:
1173 	dev->phy_ops->send_ack(dev, GFP_KERNEL);
1174 	queue_work(dev->wq, &dev->cmd_work);
1175 }
1176 
1177 static void pn533_wq_tg_get_data(struct work_struct *work)
1178 {
1179 	struct pn533 *dev = container_of(work, struct pn533, tg_work);
1180 	struct sk_buff *skb;
1181 	int rc;
1182 
1183 	dev_dbg(dev->dev, "%s\n", __func__);
1184 
1185 	skb = pn533_alloc_skb(dev, 0);
1186 	if (!skb)
1187 		return;
1188 
1189 	rc = pn533_send_data_async(dev, PN533_CMD_TG_GET_DATA, skb,
1190 				   pn533_tm_get_data_complete, NULL);
1191 
1192 	if (rc < 0)
1193 		dev_kfree_skb(skb);
1194 }
1195 
1196 #define ATR_REQ_GB_OFFSET 17
1197 static int pn533_init_target_complete(struct pn533 *dev, struct sk_buff *resp)
1198 {
1199 	u8 mode, *cmd, comm_mode = NFC_COMM_PASSIVE, *gb;
1200 	size_t gb_len;
1201 	int rc;
1202 
1203 	dev_dbg(dev->dev, "%s\n", __func__);
1204 
1205 	if (resp->len < ATR_REQ_GB_OFFSET + 1)
1206 		return -EINVAL;
1207 
1208 	mode = resp->data[0];
1209 	cmd = &resp->data[1];
1210 
1211 	dev_dbg(dev->dev, "Target mode 0x%x len %d\n",
1212 		mode, resp->len);
1213 
1214 	if ((mode & PN533_INIT_TARGET_RESP_FRAME_MASK) ==
1215 	    PN533_INIT_TARGET_RESP_ACTIVE)
1216 		comm_mode = NFC_COMM_ACTIVE;
1217 
1218 	if ((mode & PN533_INIT_TARGET_RESP_DEP) == 0)  /* Only DEP supported */
1219 		return -EOPNOTSUPP;
1220 
1221 	gb = cmd + ATR_REQ_GB_OFFSET;
1222 	gb_len = resp->len - (ATR_REQ_GB_OFFSET + 1);
1223 
1224 	rc = nfc_tm_activated(dev->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
1225 			      comm_mode, gb, gb_len);
1226 	if (rc < 0) {
1227 		nfc_err(dev->dev,
1228 			"Error when signaling target activation\n");
1229 		return rc;
1230 	}
1231 
1232 	dev->tgt_mode = 1;
1233 	queue_work(dev->wq, &dev->tg_work);
1234 
1235 	return 0;
1236 }
1237 
1238 static void pn533_listen_mode_timer(unsigned long data)
1239 {
1240 	struct pn533 *dev = (struct pn533 *)data;
1241 
1242 	dev_dbg(dev->dev, "Listen mode timeout\n");
1243 
1244 	dev->cancel_listen = 1;
1245 
1246 	pn533_poll_next_mod(dev);
1247 
1248 	queue_delayed_work(dev->wq, &dev->poll_work,
1249 			   msecs_to_jiffies(PN533_POLL_INTERVAL));
1250 }
1251 
1252 static int pn533_rf_complete(struct pn533 *dev, void *arg,
1253 			     struct sk_buff *resp)
1254 {
1255 	int rc = 0;
1256 
1257 	dev_dbg(dev->dev, "%s\n", __func__);
1258 
1259 	if (IS_ERR(resp)) {
1260 		rc = PTR_ERR(resp);
1261 
1262 		nfc_err(dev->dev, "RF setting error %d\n", rc);
1263 
1264 		return rc;
1265 	}
1266 
1267 	queue_delayed_work(dev->wq, &dev->poll_work,
1268 			   msecs_to_jiffies(PN533_POLL_INTERVAL));
1269 
1270 	dev_kfree_skb(resp);
1271 	return rc;
1272 }
1273 
1274 static void pn533_wq_rf(struct work_struct *work)
1275 {
1276 	struct pn533 *dev = container_of(work, struct pn533, rf_work);
1277 	struct sk_buff *skb;
1278 	int rc;
1279 
1280 	dev_dbg(dev->dev, "%s\n", __func__);
1281 
1282 	skb = pn533_alloc_skb(dev, 2);
1283 	if (!skb)
1284 		return;
1285 
1286 	*skb_put(skb, 1) = PN533_CFGITEM_RF_FIELD;
1287 	*skb_put(skb, 1) = PN533_CFGITEM_RF_FIELD_AUTO_RFCA;
1288 
1289 	rc = pn533_send_cmd_async(dev, PN533_CMD_RF_CONFIGURATION, skb,
1290 				  pn533_rf_complete, NULL);
1291 	if (rc < 0) {
1292 		dev_kfree_skb(skb);
1293 		nfc_err(dev->dev, "RF setting error %d\n", rc);
1294 	}
1295 }
1296 
1297 static int pn533_poll_dep_complete(struct pn533 *dev, void *arg,
1298 				   struct sk_buff *resp)
1299 {
1300 	struct pn533_cmd_jump_dep_response *rsp;
1301 	struct nfc_target nfc_target;
1302 	u8 target_gt_len;
1303 	int rc;
1304 
1305 	if (IS_ERR(resp))
1306 		return PTR_ERR(resp);
1307 
1308 	rsp = (struct pn533_cmd_jump_dep_response *)resp->data;
1309 
1310 	rc = rsp->status & PN533_CMD_RET_MASK;
1311 	if (rc != PN533_CMD_RET_SUCCESS) {
1312 		/* Not target found, turn radio off */
1313 		queue_work(dev->wq, &dev->rf_work);
1314 
1315 		dev_kfree_skb(resp);
1316 		return 0;
1317 	}
1318 
1319 	dev_dbg(dev->dev, "Creating new target");
1320 
1321 	nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1322 	nfc_target.nfcid1_len = 10;
1323 	memcpy(nfc_target.nfcid1, rsp->nfcid3t, nfc_target.nfcid1_len);
1324 	rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
1325 	if (rc)
1326 		goto error;
1327 
1328 	dev->tgt_available_prots = 0;
1329 	dev->tgt_active_prot = NFC_PROTO_NFC_DEP;
1330 
1331 	/* ATR_RES general bytes are located at offset 17 */
1332 	target_gt_len = resp->len - 17;
1333 	rc = nfc_set_remote_general_bytes(dev->nfc_dev,
1334 					  rsp->gt, target_gt_len);
1335 	if (!rc) {
1336 		rc = nfc_dep_link_is_up(dev->nfc_dev,
1337 					dev->nfc_dev->targets[0].idx,
1338 					0, NFC_RF_INITIATOR);
1339 
1340 		if (!rc)
1341 			pn533_poll_reset_mod_list(dev);
1342 	}
1343 error:
1344 	dev_kfree_skb(resp);
1345 	return rc;
1346 }
1347 
1348 #define PASSIVE_DATA_LEN 5
1349 static int pn533_poll_dep(struct nfc_dev *nfc_dev)
1350 {
1351 	struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1352 	struct sk_buff *skb;
1353 	int rc, skb_len;
1354 	u8 *next, nfcid3[NFC_NFCID3_MAXSIZE];
1355 	u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3};
1356 
1357 	dev_dbg(dev->dev, "%s", __func__);
1358 
1359 	if (!dev->gb) {
1360 		dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len);
1361 
1362 		if (!dev->gb || !dev->gb_len) {
1363 			dev->poll_dep = 0;
1364 			queue_work(dev->wq, &dev->rf_work);
1365 		}
1366 	}
1367 
1368 	skb_len = 3 + dev->gb_len; /* ActPass + BR + Next */
1369 	skb_len += PASSIVE_DATA_LEN;
1370 
1371 	/* NFCID3 */
1372 	skb_len += NFC_NFCID3_MAXSIZE;
1373 	nfcid3[0] = 0x1;
1374 	nfcid3[1] = 0xfe;
1375 	get_random_bytes(nfcid3 + 2, 6);
1376 
1377 	skb = pn533_alloc_skb(dev, skb_len);
1378 	if (!skb)
1379 		return -ENOMEM;
1380 
1381 	*skb_put(skb, 1) = 0x01;  /* Active */
1382 	*skb_put(skb, 1) = 0x02;  /* 424 kbps */
1383 
1384 	next = skb_put(skb, 1);  /* Next */
1385 	*next = 0;
1386 
1387 	/* Copy passive data */
1388 	memcpy(skb_put(skb, PASSIVE_DATA_LEN), passive_data, PASSIVE_DATA_LEN);
1389 	*next |= 1;
1390 
1391 	/* Copy NFCID3 (which is NFCID2 from SENSF_RES) */
1392 	memcpy(skb_put(skb, NFC_NFCID3_MAXSIZE), nfcid3,
1393 	       NFC_NFCID3_MAXSIZE);
1394 	*next |= 2;
1395 
1396 	memcpy(skb_put(skb, dev->gb_len), dev->gb, dev->gb_len);
1397 	*next |= 4; /* We have some Gi */
1398 
1399 	rc = pn533_send_cmd_async(dev, PN533_CMD_IN_JUMP_FOR_DEP, skb,
1400 				  pn533_poll_dep_complete, NULL);
1401 
1402 	if (rc < 0)
1403 		dev_kfree_skb(skb);
1404 
1405 	return rc;
1406 }
1407 
1408 static int pn533_poll_complete(struct pn533 *dev, void *arg,
1409 			       struct sk_buff *resp)
1410 {
1411 	struct pn533_poll_modulations *cur_mod;
1412 	int rc;
1413 
1414 	dev_dbg(dev->dev, "%s\n", __func__);
1415 
1416 	if (IS_ERR(resp)) {
1417 		rc = PTR_ERR(resp);
1418 
1419 		nfc_err(dev->dev, "%s  Poll complete error %d\n",
1420 			__func__, rc);
1421 
1422 		if (rc == -ENOENT) {
1423 			if (dev->poll_mod_count != 0)
1424 				return rc;
1425 			goto stop_poll;
1426 		} else if (rc < 0) {
1427 			nfc_err(dev->dev,
1428 				"Error %d when running poll\n", rc);
1429 			goto stop_poll;
1430 		}
1431 	}
1432 
1433 	cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1434 
1435 	if (cur_mod->len == 0) { /* Target mode */
1436 		del_timer(&dev->listen_timer);
1437 		rc = pn533_init_target_complete(dev, resp);
1438 		goto done;
1439 	}
1440 
1441 	/* Initiator mode */
1442 	rc = pn533_start_poll_complete(dev, resp);
1443 	if (!rc)
1444 		goto done;
1445 
1446 	if (!dev->poll_mod_count) {
1447 		dev_dbg(dev->dev, "Polling has been stopped\n");
1448 		goto done;
1449 	}
1450 
1451 	pn533_poll_next_mod(dev);
1452 	/* Not target found, turn radio off */
1453 	queue_work(dev->wq, &dev->rf_work);
1454 
1455 done:
1456 	dev_kfree_skb(resp);
1457 	return rc;
1458 
1459 stop_poll:
1460 	nfc_err(dev->dev, "Polling operation has been stopped\n");
1461 
1462 	pn533_poll_reset_mod_list(dev);
1463 	dev->poll_protocols = 0;
1464 	return rc;
1465 }
1466 
1467 static struct sk_buff *pn533_alloc_poll_in_frame(struct pn533 *dev,
1468 					struct pn533_poll_modulations *mod)
1469 {
1470 	struct sk_buff *skb;
1471 
1472 	skb = pn533_alloc_skb(dev, mod->len);
1473 	if (!skb)
1474 		return NULL;
1475 
1476 	memcpy(skb_put(skb, mod->len), &mod->data, mod->len);
1477 
1478 	return skb;
1479 }
1480 
1481 static int pn533_send_poll_frame(struct pn533 *dev)
1482 {
1483 	struct pn533_poll_modulations *mod;
1484 	struct sk_buff *skb;
1485 	int rc;
1486 	u8 cmd_code;
1487 
1488 	mod = dev->poll_mod_active[dev->poll_mod_curr];
1489 
1490 	dev_dbg(dev->dev, "%s mod len %d\n",
1491 		__func__, mod->len);
1492 
1493 	if ((dev->poll_protocols & NFC_PROTO_NFC_DEP_MASK) && dev->poll_dep)  {
1494 		dev->poll_dep = 0;
1495 		return pn533_poll_dep(dev->nfc_dev);
1496 	}
1497 
1498 	if (mod->len == 0) {  /* Listen mode */
1499 		cmd_code = PN533_CMD_TG_INIT_AS_TARGET;
1500 		skb = pn533_alloc_poll_tg_frame(dev);
1501 	} else {  /* Polling mode */
1502 		cmd_code =  PN533_CMD_IN_LIST_PASSIVE_TARGET;
1503 		skb = pn533_alloc_poll_in_frame(dev, mod);
1504 	}
1505 
1506 	if (!skb) {
1507 		nfc_err(dev->dev, "Failed to allocate skb\n");
1508 		return -ENOMEM;
1509 	}
1510 
1511 	rc = pn533_send_cmd_async(dev, cmd_code, skb, pn533_poll_complete,
1512 				  NULL);
1513 	if (rc < 0) {
1514 		dev_kfree_skb(skb);
1515 		nfc_err(dev->dev, "Polling loop error %d\n", rc);
1516 	}
1517 
1518 	return rc;
1519 }
1520 
1521 static void pn533_wq_poll(struct work_struct *work)
1522 {
1523 	struct pn533 *dev = container_of(work, struct pn533, poll_work.work);
1524 	struct pn533_poll_modulations *cur_mod;
1525 	int rc;
1526 
1527 	cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1528 
1529 	dev_dbg(dev->dev,
1530 		"%s cancel_listen %d modulation len %d\n",
1531 		__func__, dev->cancel_listen, cur_mod->len);
1532 
1533 	if (dev->cancel_listen == 1) {
1534 		dev->cancel_listen = 0;
1535 		dev->phy_ops->abort_cmd(dev, GFP_ATOMIC);
1536 	}
1537 
1538 	rc = pn533_send_poll_frame(dev);
1539 	if (rc)
1540 		return;
1541 
1542 	if (cur_mod->len == 0 && dev->poll_mod_count > 1)
1543 		mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ);
1544 }
1545 
1546 static int pn533_start_poll(struct nfc_dev *nfc_dev,
1547 			    u32 im_protocols, u32 tm_protocols)
1548 {
1549 	struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1550 	struct pn533_poll_modulations *cur_mod;
1551 	u8 rand_mod;
1552 	int rc;
1553 
1554 	dev_dbg(dev->dev,
1555 		"%s: im protocols 0x%x tm protocols 0x%x\n",
1556 		__func__, im_protocols, tm_protocols);
1557 
1558 	if (dev->tgt_active_prot) {
1559 		nfc_err(dev->dev,
1560 			"Cannot poll with a target already activated\n");
1561 		return -EBUSY;
1562 	}
1563 
1564 	if (dev->tgt_mode) {
1565 		nfc_err(dev->dev,
1566 			"Cannot poll while already being activated\n");
1567 		return -EBUSY;
1568 	}
1569 
1570 	if (tm_protocols) {
1571 		dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len);
1572 		if (dev->gb == NULL)
1573 			tm_protocols = 0;
1574 	}
1575 
1576 	pn533_poll_create_mod_list(dev, im_protocols, tm_protocols);
1577 	dev->poll_protocols = im_protocols;
1578 	dev->listen_protocols = tm_protocols;
1579 
1580 	/* Do not always start polling from the same modulation */
1581 	get_random_bytes(&rand_mod, sizeof(rand_mod));
1582 	rand_mod %= dev->poll_mod_count;
1583 	dev->poll_mod_curr = rand_mod;
1584 
1585 	cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1586 
1587 	rc = pn533_send_poll_frame(dev);
1588 
1589 	/* Start listen timer */
1590 	if (!rc && cur_mod->len == 0 && dev->poll_mod_count > 1)
1591 		mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ);
1592 
1593 	return rc;
1594 }
1595 
1596 static void pn533_stop_poll(struct nfc_dev *nfc_dev)
1597 {
1598 	struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1599 
1600 	del_timer(&dev->listen_timer);
1601 
1602 	if (!dev->poll_mod_count) {
1603 		dev_dbg(dev->dev,
1604 			"Polling operation was not running\n");
1605 		return;
1606 	}
1607 
1608 	dev->phy_ops->abort_cmd(dev, GFP_KERNEL);
1609 	flush_delayed_work(&dev->poll_work);
1610 	pn533_poll_reset_mod_list(dev);
1611 }
1612 
1613 static int pn533_activate_target_nfcdep(struct pn533 *dev)
1614 {
1615 	struct pn533_cmd_activate_response *rsp;
1616 	u16 gt_len;
1617 	int rc;
1618 	struct sk_buff *skb;
1619 	struct sk_buff *resp;
1620 
1621 	dev_dbg(dev->dev, "%s\n", __func__);
1622 
1623 	skb = pn533_alloc_skb(dev, sizeof(u8) * 2); /*TG + Next*/
1624 	if (!skb)
1625 		return -ENOMEM;
1626 
1627 	*skb_put(skb, sizeof(u8)) = 1; /* TG */
1628 	*skb_put(skb, sizeof(u8)) = 0; /* Next */
1629 
1630 	resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_ATR, skb);
1631 	if (IS_ERR(resp))
1632 		return PTR_ERR(resp);
1633 
1634 	rsp = (struct pn533_cmd_activate_response *)resp->data;
1635 	rc = rsp->status & PN533_CMD_RET_MASK;
1636 	if (rc != PN533_CMD_RET_SUCCESS) {
1637 		nfc_err(dev->dev,
1638 			"Target activation failed (error 0x%x)\n", rc);
1639 		dev_kfree_skb(resp);
1640 		return -EIO;
1641 	}
1642 
1643 	/* ATR_RES general bytes are located at offset 16 */
1644 	gt_len = resp->len - 16;
1645 	rc = nfc_set_remote_general_bytes(dev->nfc_dev, rsp->gt, gt_len);
1646 
1647 	dev_kfree_skb(resp);
1648 	return rc;
1649 }
1650 
1651 static int pn533_activate_target(struct nfc_dev *nfc_dev,
1652 				 struct nfc_target *target, u32 protocol)
1653 {
1654 	struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1655 	int rc;
1656 
1657 	dev_dbg(dev->dev, "%s: protocol=%u\n", __func__, protocol);
1658 
1659 	if (dev->poll_mod_count) {
1660 		nfc_err(dev->dev,
1661 			"Cannot activate while polling\n");
1662 		return -EBUSY;
1663 	}
1664 
1665 	if (dev->tgt_active_prot) {
1666 		nfc_err(dev->dev,
1667 			"There is already an active target\n");
1668 		return -EBUSY;
1669 	}
1670 
1671 	if (!dev->tgt_available_prots) {
1672 		nfc_err(dev->dev,
1673 			"There is no available target to activate\n");
1674 		return -EINVAL;
1675 	}
1676 
1677 	if (!(dev->tgt_available_prots & (1 << protocol))) {
1678 		nfc_err(dev->dev,
1679 			"Target doesn't support requested proto %u\n",
1680 			protocol);
1681 		return -EINVAL;
1682 	}
1683 
1684 	if (protocol == NFC_PROTO_NFC_DEP) {
1685 		rc = pn533_activate_target_nfcdep(dev);
1686 		if (rc) {
1687 			nfc_err(dev->dev,
1688 				"Activating target with DEP failed %d\n", rc);
1689 			return rc;
1690 		}
1691 	}
1692 
1693 	dev->tgt_active_prot = protocol;
1694 	dev->tgt_available_prots = 0;
1695 
1696 	return 0;
1697 }
1698 
1699 static int pn533_deactivate_target_complete(struct pn533 *dev, void *arg,
1700 			     struct sk_buff *resp)
1701 {
1702 	int rc = 0;
1703 
1704 	dev_dbg(dev->dev, "%s\n", __func__);
1705 
1706 	if (IS_ERR(resp)) {
1707 		rc = PTR_ERR(resp);
1708 
1709 		nfc_err(dev->dev, "Target release error %d\n", rc);
1710 
1711 		return rc;
1712 	}
1713 
1714 	rc = resp->data[0] & PN533_CMD_RET_MASK;
1715 	if (rc != PN533_CMD_RET_SUCCESS)
1716 		nfc_err(dev->dev,
1717 			"Error 0x%x when releasing the target\n", rc);
1718 
1719 	dev_kfree_skb(resp);
1720 	return rc;
1721 }
1722 
1723 static void pn533_deactivate_target(struct nfc_dev *nfc_dev,
1724 				    struct nfc_target *target, u8 mode)
1725 {
1726 	struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1727 	struct sk_buff *skb;
1728 	int rc;
1729 
1730 	dev_dbg(dev->dev, "%s\n", __func__);
1731 
1732 	if (!dev->tgt_active_prot) {
1733 		nfc_err(dev->dev, "There is no active target\n");
1734 		return;
1735 	}
1736 
1737 	dev->tgt_active_prot = 0;
1738 	skb_queue_purge(&dev->resp_q);
1739 
1740 	skb = pn533_alloc_skb(dev, sizeof(u8));
1741 	if (!skb)
1742 		return;
1743 
1744 	*skb_put(skb, 1) = 1; /* TG*/
1745 
1746 	rc = pn533_send_cmd_async(dev, PN533_CMD_IN_RELEASE, skb,
1747 				  pn533_deactivate_target_complete, NULL);
1748 	if (rc < 0) {
1749 		dev_kfree_skb(skb);
1750 		nfc_err(dev->dev, "Target release error %d\n", rc);
1751 	}
1752 }
1753 
1754 
1755 static int pn533_in_dep_link_up_complete(struct pn533 *dev, void *arg,
1756 					 struct sk_buff *resp)
1757 {
1758 	struct pn533_cmd_jump_dep_response *rsp;
1759 	u8 target_gt_len;
1760 	int rc;
1761 	u8 active = *(u8 *)arg;
1762 
1763 	kfree(arg);
1764 
1765 	if (IS_ERR(resp))
1766 		return PTR_ERR(resp);
1767 
1768 	if (dev->tgt_available_prots &&
1769 	    !(dev->tgt_available_prots & (1 << NFC_PROTO_NFC_DEP))) {
1770 		nfc_err(dev->dev,
1771 			"The target does not support DEP\n");
1772 		rc =  -EINVAL;
1773 		goto error;
1774 	}
1775 
1776 	rsp = (struct pn533_cmd_jump_dep_response *)resp->data;
1777 
1778 	rc = rsp->status & PN533_CMD_RET_MASK;
1779 	if (rc != PN533_CMD_RET_SUCCESS) {
1780 		nfc_err(dev->dev,
1781 			"Bringing DEP link up failed (error 0x%x)\n", rc);
1782 		goto error;
1783 	}
1784 
1785 	if (!dev->tgt_available_prots) {
1786 		struct nfc_target nfc_target;
1787 
1788 		dev_dbg(dev->dev, "Creating new target\n");
1789 
1790 		nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1791 		nfc_target.nfcid1_len = 10;
1792 		memcpy(nfc_target.nfcid1, rsp->nfcid3t, nfc_target.nfcid1_len);
1793 		rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
1794 		if (rc)
1795 			goto error;
1796 
1797 		dev->tgt_available_prots = 0;
1798 	}
1799 
1800 	dev->tgt_active_prot = NFC_PROTO_NFC_DEP;
1801 
1802 	/* ATR_RES general bytes are located at offset 17 */
1803 	target_gt_len = resp->len - 17;
1804 	rc = nfc_set_remote_general_bytes(dev->nfc_dev,
1805 					  rsp->gt, target_gt_len);
1806 	if (rc == 0)
1807 		rc = nfc_dep_link_is_up(dev->nfc_dev,
1808 					dev->nfc_dev->targets[0].idx,
1809 					!active, NFC_RF_INITIATOR);
1810 
1811 error:
1812 	dev_kfree_skb(resp);
1813 	return rc;
1814 }
1815 
1816 static int pn533_rf_field(struct nfc_dev *nfc_dev, u8 rf);
1817 static int pn533_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
1818 			     u8 comm_mode, u8 *gb, size_t gb_len)
1819 {
1820 	struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1821 	struct sk_buff *skb;
1822 	int rc, skb_len;
1823 	u8 *next, *arg, nfcid3[NFC_NFCID3_MAXSIZE];
1824 	u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3};
1825 
1826 	dev_dbg(dev->dev, "%s\n", __func__);
1827 
1828 	if (dev->poll_mod_count) {
1829 		nfc_err(dev->dev,
1830 			"Cannot bring the DEP link up while polling\n");
1831 		return -EBUSY;
1832 	}
1833 
1834 	if (dev->tgt_active_prot) {
1835 		nfc_err(dev->dev,
1836 			"There is already an active target\n");
1837 		return -EBUSY;
1838 	}
1839 
1840 	skb_len = 3 + gb_len; /* ActPass + BR + Next */
1841 	skb_len += PASSIVE_DATA_LEN;
1842 
1843 	/* NFCID3 */
1844 	skb_len += NFC_NFCID3_MAXSIZE;
1845 	if (target && !target->nfcid2_len) {
1846 		nfcid3[0] = 0x1;
1847 		nfcid3[1] = 0xfe;
1848 		get_random_bytes(nfcid3 + 2, 6);
1849 	}
1850 
1851 	skb = pn533_alloc_skb(dev, skb_len);
1852 	if (!skb)
1853 		return -ENOMEM;
1854 
1855 	*skb_put(skb, 1) = !comm_mode;  /* ActPass */
1856 	*skb_put(skb, 1) = 0x02;  /* 424 kbps */
1857 
1858 	next = skb_put(skb, 1);  /* Next */
1859 	*next = 0;
1860 
1861 	/* Copy passive data */
1862 	memcpy(skb_put(skb, PASSIVE_DATA_LEN), passive_data, PASSIVE_DATA_LEN);
1863 	*next |= 1;
1864 
1865 	/* Copy NFCID3 (which is NFCID2 from SENSF_RES) */
1866 	if (target && target->nfcid2_len)
1867 		memcpy(skb_put(skb, NFC_NFCID3_MAXSIZE), target->nfcid2,
1868 		       target->nfcid2_len);
1869 	else
1870 		memcpy(skb_put(skb, NFC_NFCID3_MAXSIZE), nfcid3,
1871 		       NFC_NFCID3_MAXSIZE);
1872 	*next |= 2;
1873 
1874 	if (gb != NULL && gb_len > 0) {
1875 		memcpy(skb_put(skb, gb_len), gb, gb_len);
1876 		*next |= 4; /* We have some Gi */
1877 	} else {
1878 		*next = 0;
1879 	}
1880 
1881 	arg = kmalloc(sizeof(*arg), GFP_KERNEL);
1882 	if (!arg) {
1883 		dev_kfree_skb(skb);
1884 		return -ENOMEM;
1885 	}
1886 
1887 	*arg = !comm_mode;
1888 
1889 	pn533_rf_field(dev->nfc_dev, 0);
1890 
1891 	rc = pn533_send_cmd_async(dev, PN533_CMD_IN_JUMP_FOR_DEP, skb,
1892 				  pn533_in_dep_link_up_complete, arg);
1893 
1894 	if (rc < 0) {
1895 		dev_kfree_skb(skb);
1896 		kfree(arg);
1897 	}
1898 
1899 	return rc;
1900 }
1901 
1902 static int pn533_dep_link_down(struct nfc_dev *nfc_dev)
1903 {
1904 	struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1905 
1906 	dev_dbg(dev->dev, "%s\n", __func__);
1907 
1908 	pn533_poll_reset_mod_list(dev);
1909 
1910 	if (dev->tgt_mode || dev->tgt_active_prot)
1911 		dev->phy_ops->abort_cmd(dev, GFP_KERNEL);
1912 
1913 	dev->tgt_active_prot = 0;
1914 	dev->tgt_mode = 0;
1915 
1916 	skb_queue_purge(&dev->resp_q);
1917 
1918 	return 0;
1919 }
1920 
1921 struct pn533_data_exchange_arg {
1922 	data_exchange_cb_t cb;
1923 	void *cb_context;
1924 };
1925 
1926 static struct sk_buff *pn533_build_response(struct pn533 *dev)
1927 {
1928 	struct sk_buff *skb, *tmp, *t;
1929 	unsigned int skb_len = 0, tmp_len = 0;
1930 
1931 	dev_dbg(dev->dev, "%s\n", __func__);
1932 
1933 	if (skb_queue_empty(&dev->resp_q))
1934 		return NULL;
1935 
1936 	if (skb_queue_len(&dev->resp_q) == 1) {
1937 		skb = skb_dequeue(&dev->resp_q);
1938 		goto out;
1939 	}
1940 
1941 	skb_queue_walk_safe(&dev->resp_q, tmp, t)
1942 		skb_len += tmp->len;
1943 
1944 	dev_dbg(dev->dev, "%s total length %d\n",
1945 		__func__, skb_len);
1946 
1947 	skb = alloc_skb(skb_len, GFP_KERNEL);
1948 	if (skb == NULL)
1949 		goto out;
1950 
1951 	skb_put(skb, skb_len);
1952 
1953 	skb_queue_walk_safe(&dev->resp_q, tmp, t) {
1954 		memcpy(skb->data + tmp_len, tmp->data, tmp->len);
1955 		tmp_len += tmp->len;
1956 	}
1957 
1958 out:
1959 	skb_queue_purge(&dev->resp_q);
1960 
1961 	return skb;
1962 }
1963 
1964 static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg,
1965 					struct sk_buff *resp)
1966 {
1967 	struct pn533_data_exchange_arg *arg = _arg;
1968 	struct sk_buff *skb;
1969 	int rc = 0;
1970 	u8 status, ret, mi;
1971 
1972 	dev_dbg(dev->dev, "%s\n", __func__);
1973 
1974 	if (IS_ERR(resp)) {
1975 		rc = PTR_ERR(resp);
1976 		goto _error;
1977 	}
1978 
1979 	status = resp->data[0];
1980 	ret = status & PN533_CMD_RET_MASK;
1981 	mi = status & PN533_CMD_MI_MASK;
1982 
1983 	skb_pull(resp, sizeof(status));
1984 
1985 	if (ret != PN533_CMD_RET_SUCCESS) {
1986 		nfc_err(dev->dev,
1987 			"Exchanging data failed (error 0x%x)\n", ret);
1988 		rc = -EIO;
1989 		goto error;
1990 	}
1991 
1992 	skb_queue_tail(&dev->resp_q, resp);
1993 
1994 	if (mi) {
1995 		dev->cmd_complete_mi_arg = arg;
1996 		queue_work(dev->wq, &dev->mi_rx_work);
1997 		return -EINPROGRESS;
1998 	}
1999 
2000 	/* Prepare for the next round */
2001 	if (skb_queue_len(&dev->fragment_skb) > 0) {
2002 		dev->cmd_complete_dep_arg = arg;
2003 		queue_work(dev->wq, &dev->mi_tx_work);
2004 
2005 		return -EINPROGRESS;
2006 	}
2007 
2008 	skb = pn533_build_response(dev);
2009 	if (!skb) {
2010 		rc = -ENOMEM;
2011 		goto error;
2012 	}
2013 
2014 	arg->cb(arg->cb_context, skb, 0);
2015 	kfree(arg);
2016 	return 0;
2017 
2018 error:
2019 	dev_kfree_skb(resp);
2020 _error:
2021 	skb_queue_purge(&dev->resp_q);
2022 	arg->cb(arg->cb_context, NULL, rc);
2023 	kfree(arg);
2024 	return rc;
2025 }
2026 
2027 /*
2028  * Receive an incoming pn533 frame. skb contains only header and payload.
2029  * If skb == NULL, it is a notification that the link below is dead.
2030  */
2031 void pn533_recv_frame(struct pn533 *dev, struct sk_buff *skb, int status)
2032 {
2033 	if (!dev->cmd)
2034 		goto sched_wq;
2035 
2036 	dev->cmd->status = status;
2037 
2038 	if (status != 0) {
2039 		dev_dbg(dev->dev, "%s: Error received: %d\n", __func__, status);
2040 		goto sched_wq;
2041 	}
2042 
2043 	if (skb == NULL) {
2044 		pr_err("NULL Frame -> link is dead\n");
2045 		goto sched_wq;
2046 	}
2047 
2048 	if (pn533_rx_frame_is_ack(skb->data)) {
2049 		dev_dbg(dev->dev, "%s: Received ACK frame\n", __func__);
2050 		dev_kfree_skb(skb);
2051 		return;
2052 	}
2053 
2054 	print_hex_dump_debug("PN533 RX: ", DUMP_PREFIX_NONE, 16, 1, skb->data,
2055 			     dev->ops->rx_frame_size(skb->data), false);
2056 
2057 	if (!dev->ops->rx_is_frame_valid(skb->data, dev)) {
2058 		nfc_err(dev->dev, "Received an invalid frame\n");
2059 		dev->cmd->status = -EIO;
2060 	} else if (!pn533_rx_frame_is_cmd_response(dev, skb->data)) {
2061 		nfc_err(dev->dev, "It it not the response to the last command\n");
2062 		dev->cmd->status = -EIO;
2063 	}
2064 
2065 	dev->cmd->resp = skb;
2066 
2067 sched_wq:
2068 	queue_work(dev->wq, &dev->cmd_complete_work);
2069 }
2070 EXPORT_SYMBOL(pn533_recv_frame);
2071 
2072 /* Split the Tx skb into small chunks */
2073 static int pn533_fill_fragment_skbs(struct pn533 *dev, struct sk_buff *skb)
2074 {
2075 	struct sk_buff *frag;
2076 	int  frag_size;
2077 
2078 	do {
2079 		/* Remaining size */
2080 		if (skb->len > PN533_CMD_DATAFRAME_MAXLEN)
2081 			frag_size = PN533_CMD_DATAFRAME_MAXLEN;
2082 		else
2083 			frag_size = skb->len;
2084 
2085 		/* Allocate and reserve */
2086 		frag = pn533_alloc_skb(dev, frag_size);
2087 		if (!frag) {
2088 			skb_queue_purge(&dev->fragment_skb);
2089 			break;
2090 		}
2091 
2092 		if (!dev->tgt_mode) {
2093 			/* Reserve the TG/MI byte */
2094 			skb_reserve(frag, 1);
2095 
2096 			/* MI + TG */
2097 			if (frag_size  == PN533_CMD_DATAFRAME_MAXLEN)
2098 				*skb_push(frag, sizeof(u8)) =
2099 							(PN533_CMD_MI_MASK | 1);
2100 			else
2101 				*skb_push(frag, sizeof(u8)) =  1; /* TG */
2102 		}
2103 
2104 		memcpy(skb_put(frag, frag_size), skb->data, frag_size);
2105 
2106 		/* Reduce the size of incoming buffer */
2107 		skb_pull(skb, frag_size);
2108 
2109 		/* Add this to skb_queue */
2110 		skb_queue_tail(&dev->fragment_skb, frag);
2111 
2112 	} while (skb->len > 0);
2113 
2114 	dev_kfree_skb(skb);
2115 
2116 	return skb_queue_len(&dev->fragment_skb);
2117 }
2118 
2119 static int pn533_transceive(struct nfc_dev *nfc_dev,
2120 			    struct nfc_target *target, struct sk_buff *skb,
2121 			    data_exchange_cb_t cb, void *cb_context)
2122 {
2123 	struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2124 	struct pn533_data_exchange_arg *arg = NULL;
2125 	int rc;
2126 
2127 	dev_dbg(dev->dev, "%s\n", __func__);
2128 
2129 	if (!dev->tgt_active_prot) {
2130 		nfc_err(dev->dev,
2131 			"Can't exchange data if there is no active target\n");
2132 		rc = -EINVAL;
2133 		goto error;
2134 	}
2135 
2136 	arg = kmalloc(sizeof(*arg), GFP_KERNEL);
2137 	if (!arg) {
2138 		rc = -ENOMEM;
2139 		goto error;
2140 	}
2141 
2142 	arg->cb = cb;
2143 	arg->cb_context = cb_context;
2144 
2145 	switch (dev->device_type) {
2146 	case PN533_DEVICE_PASORI:
2147 		if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
2148 			rc = pn533_send_data_async(dev, PN533_CMD_IN_COMM_THRU,
2149 						   skb,
2150 						   pn533_data_exchange_complete,
2151 						   arg);
2152 
2153 			break;
2154 		}
2155 	default:
2156 		/* jumbo frame ? */
2157 		if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
2158 			rc = pn533_fill_fragment_skbs(dev, skb);
2159 			if (rc <= 0)
2160 				goto error;
2161 
2162 			skb = skb_dequeue(&dev->fragment_skb);
2163 			if (!skb) {
2164 				rc = -EIO;
2165 				goto error;
2166 			}
2167 		} else {
2168 			*skb_push(skb, sizeof(u8)) =  1; /* TG */
2169 		}
2170 
2171 		rc = pn533_send_data_async(dev, PN533_CMD_IN_DATA_EXCHANGE,
2172 					   skb, pn533_data_exchange_complete,
2173 					   arg);
2174 
2175 		break;
2176 	}
2177 
2178 	if (rc < 0) /* rc from send_async */
2179 		goto error;
2180 
2181 	return 0;
2182 
2183 error:
2184 	kfree(arg);
2185 	dev_kfree_skb(skb);
2186 	return rc;
2187 }
2188 
2189 static int pn533_tm_send_complete(struct pn533 *dev, void *arg,
2190 				  struct sk_buff *resp)
2191 {
2192 	u8 status;
2193 
2194 	dev_dbg(dev->dev, "%s\n", __func__);
2195 
2196 	if (IS_ERR(resp))
2197 		return PTR_ERR(resp);
2198 
2199 	status = resp->data[0];
2200 
2201 	/* Prepare for the next round */
2202 	if (skb_queue_len(&dev->fragment_skb) > 0) {
2203 		queue_work(dev->wq, &dev->mi_tm_tx_work);
2204 		return -EINPROGRESS;
2205 	}
2206 	dev_kfree_skb(resp);
2207 
2208 	if (status != 0) {
2209 		nfc_tm_deactivated(dev->nfc_dev);
2210 
2211 		dev->tgt_mode = 0;
2212 
2213 		return 0;
2214 	}
2215 
2216 	queue_work(dev->wq, &dev->tg_work);
2217 
2218 	return 0;
2219 }
2220 
2221 static int pn533_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
2222 {
2223 	struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2224 	int rc;
2225 
2226 	dev_dbg(dev->dev, "%s\n", __func__);
2227 
2228 	/* let's split in multiple chunks if size's too big */
2229 	if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
2230 		rc = pn533_fill_fragment_skbs(dev, skb);
2231 		if (rc <= 0)
2232 			goto error;
2233 
2234 		/* get the first skb */
2235 		skb = skb_dequeue(&dev->fragment_skb);
2236 		if (!skb) {
2237 			rc = -EIO;
2238 			goto error;
2239 		}
2240 
2241 		rc = pn533_send_data_async(dev, PN533_CMD_TG_SET_META_DATA, skb,
2242 						pn533_tm_send_complete, NULL);
2243 	} else {
2244 		/* Send th skb */
2245 		rc = pn533_send_data_async(dev, PN533_CMD_TG_SET_DATA, skb,
2246 						pn533_tm_send_complete, NULL);
2247 	}
2248 
2249 error:
2250 	if (rc < 0) {
2251 		dev_kfree_skb(skb);
2252 		skb_queue_purge(&dev->fragment_skb);
2253 	}
2254 
2255 	return rc;
2256 }
2257 
2258 static void pn533_wq_mi_recv(struct work_struct *work)
2259 {
2260 	struct pn533 *dev = container_of(work, struct pn533, mi_rx_work);
2261 	struct sk_buff *skb;
2262 	int rc;
2263 
2264 	dev_dbg(dev->dev, "%s\n", __func__);
2265 
2266 	skb = pn533_alloc_skb(dev, PN533_CMD_DATAEXCH_HEAD_LEN);
2267 	if (!skb)
2268 		goto error;
2269 
2270 	switch (dev->device_type) {
2271 	case PN533_DEVICE_PASORI:
2272 		if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
2273 			rc = pn533_send_cmd_direct_async(dev,
2274 						PN533_CMD_IN_COMM_THRU,
2275 						skb,
2276 						pn533_data_exchange_complete,
2277 						 dev->cmd_complete_mi_arg);
2278 
2279 			break;
2280 		}
2281 	default:
2282 		*skb_put(skb, sizeof(u8)) =  1; /*TG*/
2283 
2284 		rc = pn533_send_cmd_direct_async(dev,
2285 						 PN533_CMD_IN_DATA_EXCHANGE,
2286 						 skb,
2287 						 pn533_data_exchange_complete,
2288 						 dev->cmd_complete_mi_arg);
2289 
2290 		break;
2291 	}
2292 
2293 	if (rc == 0) /* success */
2294 		return;
2295 
2296 	nfc_err(dev->dev,
2297 		"Error %d when trying to perform data_exchange\n", rc);
2298 
2299 	dev_kfree_skb(skb);
2300 	kfree(dev->cmd_complete_mi_arg);
2301 
2302 error:
2303 	dev->phy_ops->send_ack(dev, GFP_KERNEL);
2304 	queue_work(dev->wq, &dev->cmd_work);
2305 }
2306 
2307 static void pn533_wq_mi_send(struct work_struct *work)
2308 {
2309 	struct pn533 *dev = container_of(work, struct pn533, mi_tx_work);
2310 	struct sk_buff *skb;
2311 	int rc;
2312 
2313 	dev_dbg(dev->dev, "%s\n", __func__);
2314 
2315 	/* Grab the first skb in the queue */
2316 	skb = skb_dequeue(&dev->fragment_skb);
2317 
2318 	if (skb == NULL) {	/* No more data */
2319 		/* Reset the queue for future use */
2320 		skb_queue_head_init(&dev->fragment_skb);
2321 		goto error;
2322 	}
2323 
2324 	switch (dev->device_type) {
2325 	case PN533_DEVICE_PASORI:
2326 		if (dev->tgt_active_prot != NFC_PROTO_FELICA) {
2327 			rc = -EIO;
2328 			break;
2329 		}
2330 
2331 		rc = pn533_send_cmd_direct_async(dev, PN533_CMD_IN_COMM_THRU,
2332 						 skb,
2333 						 pn533_data_exchange_complete,
2334 						 dev->cmd_complete_dep_arg);
2335 
2336 		break;
2337 
2338 	default:
2339 		/* Still some fragments? */
2340 		rc = pn533_send_cmd_direct_async(dev,
2341 						 PN533_CMD_IN_DATA_EXCHANGE,
2342 						 skb,
2343 						 pn533_data_exchange_complete,
2344 						 dev->cmd_complete_dep_arg);
2345 
2346 		break;
2347 	}
2348 
2349 	if (rc == 0) /* success */
2350 		return;
2351 
2352 	nfc_err(dev->dev,
2353 		"Error %d when trying to perform data_exchange\n", rc);
2354 
2355 	dev_kfree_skb(skb);
2356 	kfree(dev->cmd_complete_dep_arg);
2357 
2358 error:
2359 	dev->phy_ops->send_ack(dev, GFP_KERNEL);
2360 	queue_work(dev->wq, &dev->cmd_work);
2361 }
2362 
2363 static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata,
2364 								u8 cfgdata_len)
2365 {
2366 	struct sk_buff *skb;
2367 	struct sk_buff *resp;
2368 	int skb_len;
2369 
2370 	dev_dbg(dev->dev, "%s\n", __func__);
2371 
2372 	skb_len = sizeof(cfgitem) + cfgdata_len; /* cfgitem + cfgdata */
2373 
2374 	skb = pn533_alloc_skb(dev, skb_len);
2375 	if (!skb)
2376 		return -ENOMEM;
2377 
2378 	*skb_put(skb, sizeof(cfgitem)) = cfgitem;
2379 	memcpy(skb_put(skb, cfgdata_len), cfgdata, cfgdata_len);
2380 
2381 	resp = pn533_send_cmd_sync(dev, PN533_CMD_RF_CONFIGURATION, skb);
2382 	if (IS_ERR(resp))
2383 		return PTR_ERR(resp);
2384 
2385 	dev_kfree_skb(resp);
2386 	return 0;
2387 }
2388 
2389 static int pn533_get_firmware_version(struct pn533 *dev,
2390 				      struct pn533_fw_version *fv)
2391 {
2392 	struct sk_buff *skb;
2393 	struct sk_buff *resp;
2394 
2395 	skb = pn533_alloc_skb(dev, 0);
2396 	if (!skb)
2397 		return -ENOMEM;
2398 
2399 	resp = pn533_send_cmd_sync(dev, PN533_CMD_GET_FIRMWARE_VERSION, skb);
2400 	if (IS_ERR(resp))
2401 		return PTR_ERR(resp);
2402 
2403 	fv->ic = resp->data[0];
2404 	fv->ver = resp->data[1];
2405 	fv->rev = resp->data[2];
2406 	fv->support = resp->data[3];
2407 
2408 	dev_kfree_skb(resp);
2409 	return 0;
2410 }
2411 
2412 static int pn533_pasori_fw_reset(struct pn533 *dev)
2413 {
2414 	struct sk_buff *skb;
2415 	struct sk_buff *resp;
2416 
2417 	dev_dbg(dev->dev, "%s\n", __func__);
2418 
2419 	skb = pn533_alloc_skb(dev, sizeof(u8));
2420 	if (!skb)
2421 		return -ENOMEM;
2422 
2423 	*skb_put(skb, sizeof(u8)) = 0x1;
2424 
2425 	resp = pn533_send_cmd_sync(dev, 0x18, skb);
2426 	if (IS_ERR(resp))
2427 		return PTR_ERR(resp);
2428 
2429 	dev_kfree_skb(resp);
2430 
2431 	return 0;
2432 }
2433 
2434 static int pn533_rf_field(struct nfc_dev *nfc_dev, u8 rf)
2435 {
2436 	struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2437 	u8 rf_field = !!rf;
2438 	int rc;
2439 
2440 	rf_field |= PN533_CFGITEM_RF_FIELD_AUTO_RFCA;
2441 
2442 	rc = pn533_set_configuration(dev, PN533_CFGITEM_RF_FIELD,
2443 				     (u8 *)&rf_field, 1);
2444 	if (rc) {
2445 		nfc_err(dev->dev, "Error on setting RF field\n");
2446 		return rc;
2447 	}
2448 
2449 	return rc;
2450 }
2451 
2452 static int pn532_sam_configuration(struct nfc_dev *nfc_dev)
2453 {
2454 	struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2455 	struct sk_buff *skb;
2456 	struct sk_buff *resp;
2457 
2458 	skb = pn533_alloc_skb(dev, 1);
2459 	if (!skb)
2460 		return -ENOMEM;
2461 
2462 	*skb_put(skb, 1) = 0x01;
2463 
2464 	resp = pn533_send_cmd_sync(dev, PN533_CMD_SAM_CONFIGURATION, skb);
2465 	if (IS_ERR(resp))
2466 		return PTR_ERR(resp);
2467 
2468 	dev_kfree_skb(resp);
2469 	return 0;
2470 }
2471 
2472 static int pn533_dev_up(struct nfc_dev *nfc_dev)
2473 {
2474 	struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2475 
2476 	if (dev->device_type == PN533_DEVICE_PN532) {
2477 		int rc = pn532_sam_configuration(nfc_dev);
2478 
2479 		if (rc)
2480 			return rc;
2481 	}
2482 
2483 	return pn533_rf_field(nfc_dev, 1);
2484 }
2485 
2486 static int pn533_dev_down(struct nfc_dev *nfc_dev)
2487 {
2488 	return pn533_rf_field(nfc_dev, 0);
2489 }
2490 
2491 static struct nfc_ops pn533_nfc_ops = {
2492 	.dev_up = pn533_dev_up,
2493 	.dev_down = pn533_dev_down,
2494 	.dep_link_up = pn533_dep_link_up,
2495 	.dep_link_down = pn533_dep_link_down,
2496 	.start_poll = pn533_start_poll,
2497 	.stop_poll = pn533_stop_poll,
2498 	.activate_target = pn533_activate_target,
2499 	.deactivate_target = pn533_deactivate_target,
2500 	.im_transceive = pn533_transceive,
2501 	.tm_send = pn533_tm_send,
2502 };
2503 
2504 static int pn533_setup(struct pn533 *dev)
2505 {
2506 	struct pn533_config_max_retries max_retries;
2507 	struct pn533_config_timing timing;
2508 	u8 pasori_cfg[3] = {0x08, 0x01, 0x08};
2509 	int rc;
2510 
2511 	switch (dev->device_type) {
2512 	case PN533_DEVICE_STD:
2513 	case PN533_DEVICE_PASORI:
2514 	case PN533_DEVICE_ACR122U:
2515 	case PN533_DEVICE_PN532:
2516 		max_retries.mx_rty_atr = 0x2;
2517 		max_retries.mx_rty_psl = 0x1;
2518 		max_retries.mx_rty_passive_act =
2519 			PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2520 
2521 		timing.rfu = PN533_CONFIG_TIMING_102;
2522 		timing.atr_res_timeout = PN533_CONFIG_TIMING_102;
2523 		timing.dep_timeout = PN533_CONFIG_TIMING_204;
2524 
2525 		break;
2526 
2527 	default:
2528 		nfc_err(dev->dev, "Unknown device type %d\n",
2529 			dev->device_type);
2530 		return -EINVAL;
2531 	}
2532 
2533 	rc = pn533_set_configuration(dev, PN533_CFGITEM_MAX_RETRIES,
2534 				     (u8 *)&max_retries, sizeof(max_retries));
2535 	if (rc) {
2536 		nfc_err(dev->dev,
2537 			"Error on setting MAX_RETRIES config\n");
2538 		return rc;
2539 	}
2540 
2541 
2542 	rc = pn533_set_configuration(dev, PN533_CFGITEM_TIMING,
2543 				     (u8 *)&timing, sizeof(timing));
2544 	if (rc) {
2545 		nfc_err(dev->dev, "Error on setting RF timings\n");
2546 		return rc;
2547 	}
2548 
2549 	switch (dev->device_type) {
2550 	case PN533_DEVICE_STD:
2551 	case PN533_DEVICE_PN532:
2552 		break;
2553 
2554 	case PN533_DEVICE_PASORI:
2555 		pn533_pasori_fw_reset(dev);
2556 
2557 		rc = pn533_set_configuration(dev, PN533_CFGITEM_PASORI,
2558 					     pasori_cfg, 3);
2559 		if (rc) {
2560 			nfc_err(dev->dev,
2561 				"Error while settings PASORI config\n");
2562 			return rc;
2563 		}
2564 
2565 		pn533_pasori_fw_reset(dev);
2566 
2567 		break;
2568 	}
2569 
2570 	return 0;
2571 }
2572 
2573 int pn533_finalize_setup(struct pn533 *dev)
2574 {
2575 
2576 	struct pn533_fw_version fw_ver;
2577 	int rc;
2578 
2579 	memset(&fw_ver, 0, sizeof(fw_ver));
2580 
2581 	rc = pn533_get_firmware_version(dev, &fw_ver);
2582 	if (rc) {
2583 		nfc_err(dev->dev, "Unable to get FW version\n");
2584 		return rc;
2585 	}
2586 
2587 	nfc_info(dev->dev, "NXP PN5%02X firmware ver %d.%d now attached\n",
2588 		fw_ver.ic, fw_ver.ver, fw_ver.rev);
2589 
2590 	rc = pn533_setup(dev);
2591 	if (rc)
2592 		return rc;
2593 
2594 	return 0;
2595 }
2596 EXPORT_SYMBOL_GPL(pn533_finalize_setup);
2597 
2598 struct pn533 *pn533_register_device(u32 device_type,
2599 				u32 protocols,
2600 				enum pn533_protocol_type protocol_type,
2601 				void *phy,
2602 				struct pn533_phy_ops *phy_ops,
2603 				struct pn533_frame_ops *fops,
2604 				struct device *dev,
2605 				struct device *parent)
2606 {
2607 	struct pn533 *priv;
2608 	int rc = -ENOMEM;
2609 
2610 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
2611 	if (!priv)
2612 		return ERR_PTR(-ENOMEM);
2613 
2614 	priv->phy = phy;
2615 	priv->phy_ops = phy_ops;
2616 	priv->dev = dev;
2617 	if (fops != NULL)
2618 		priv->ops = fops;
2619 	else
2620 		priv->ops = &pn533_std_frame_ops;
2621 
2622 	priv->protocol_type = protocol_type;
2623 	priv->device_type = device_type;
2624 
2625 	mutex_init(&priv->cmd_lock);
2626 
2627 	INIT_WORK(&priv->cmd_work, pn533_wq_cmd);
2628 	INIT_WORK(&priv->cmd_complete_work, pn533_wq_cmd_complete);
2629 	INIT_WORK(&priv->mi_rx_work, pn533_wq_mi_recv);
2630 	INIT_WORK(&priv->mi_tx_work, pn533_wq_mi_send);
2631 	INIT_WORK(&priv->tg_work, pn533_wq_tg_get_data);
2632 	INIT_WORK(&priv->mi_tm_rx_work, pn533_wq_tm_mi_recv);
2633 	INIT_WORK(&priv->mi_tm_tx_work, pn533_wq_tm_mi_send);
2634 	INIT_DELAYED_WORK(&priv->poll_work, pn533_wq_poll);
2635 	INIT_WORK(&priv->rf_work, pn533_wq_rf);
2636 	priv->wq = alloc_ordered_workqueue("pn533", 0);
2637 	if (priv->wq == NULL)
2638 		goto error;
2639 
2640 	init_timer(&priv->listen_timer);
2641 	priv->listen_timer.data = (unsigned long) priv;
2642 	priv->listen_timer.function = pn533_listen_mode_timer;
2643 
2644 	skb_queue_head_init(&priv->resp_q);
2645 	skb_queue_head_init(&priv->fragment_skb);
2646 
2647 	INIT_LIST_HEAD(&priv->cmd_queue);
2648 
2649 	priv->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols,
2650 					   priv->ops->tx_header_len +
2651 					   PN533_CMD_DATAEXCH_HEAD_LEN,
2652 					   priv->ops->tx_tail_len);
2653 	if (!priv->nfc_dev) {
2654 		rc = -ENOMEM;
2655 		goto destroy_wq;
2656 	}
2657 
2658 	nfc_set_parent_dev(priv->nfc_dev, parent);
2659 	nfc_set_drvdata(priv->nfc_dev, priv);
2660 
2661 	rc = nfc_register_device(priv->nfc_dev);
2662 	if (rc)
2663 		goto free_nfc_dev;
2664 
2665 	return priv;
2666 
2667 free_nfc_dev:
2668 	nfc_free_device(priv->nfc_dev);
2669 
2670 destroy_wq:
2671 	destroy_workqueue(priv->wq);
2672 error:
2673 	kfree(priv);
2674 	return ERR_PTR(rc);
2675 }
2676 EXPORT_SYMBOL_GPL(pn533_register_device);
2677 
2678 void pn533_unregister_device(struct pn533 *priv)
2679 {
2680 	struct pn533_cmd *cmd, *n;
2681 
2682 	nfc_unregister_device(priv->nfc_dev);
2683 	nfc_free_device(priv->nfc_dev);
2684 
2685 	flush_delayed_work(&priv->poll_work);
2686 	destroy_workqueue(priv->wq);
2687 
2688 	skb_queue_purge(&priv->resp_q);
2689 
2690 	del_timer(&priv->listen_timer);
2691 
2692 	list_for_each_entry_safe(cmd, n, &priv->cmd_queue, queue) {
2693 		list_del(&cmd->queue);
2694 		kfree(cmd);
2695 	}
2696 
2697 	kfree(priv);
2698 }
2699 EXPORT_SYMBOL_GPL(pn533_unregister_device);
2700 
2701 
2702 MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
2703 MODULE_AUTHOR("Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
2704 MODULE_AUTHOR("Waldemar Rymarkiewicz <waldemar.rymarkiewicz@tieto.com>");
2705 MODULE_DESCRIPTION("PN533 driver ver " VERSION);
2706 MODULE_VERSION(VERSION);
2707 MODULE_LICENSE("GPL");
2708