Linux Audio

Check our new training course

Loading...
v3.15
 
  1/*
  2 * NFC Digital Protocol stack
  3 * Copyright (c) 2013, Intel Corporation.
  4 *
  5 * This program is free software; you can redistribute it and/or modify it
  6 * under the terms and conditions of the GNU General Public License,
  7 * version 2, as published by the Free Software Foundation.
  8 *
  9 * This program is distributed in the hope it will be useful, but WITHOUT
 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 12 * more details.
 13 *
 14 */
 15
 16#define pr_fmt(fmt) "digital: %s: " fmt, __func__
 17
 18#include <linux/module.h>
 19
 20#include "digital.h"
 21
 22#define DIGITAL_PROTO_NFCA_RF_TECH \
 23	(NFC_PROTO_JEWEL_MASK | NFC_PROTO_MIFARE_MASK | NFC_PROTO_NFC_DEP_MASK)
 
 
 
 24
 25#define DIGITAL_PROTO_NFCF_RF_TECH \
 26	(NFC_PROTO_FELICA_MASK | NFC_PROTO_NFC_DEP_MASK)
 27
 28#define DIGITAL_PROTO_ISO15693_RF_TECH	NFC_PROTO_ISO15693_MASK
 29
 
 
 
 30struct digital_cmd {
 31	struct list_head queue;
 32
 33	u8 type;
 34	u8 pending;
 35
 36	u16 timeout;
 37	struct sk_buff *req;
 38	struct sk_buff *resp;
 39	struct digital_tg_mdaa_params *mdaa_params;
 40
 41	nfc_digital_cmd_complete_t cmd_cb;
 42	void *cb_context;
 43};
 44
 45struct sk_buff *digital_skb_alloc(struct nfc_digital_dev *ddev,
 46				  unsigned int len)
 47{
 48	struct sk_buff *skb;
 49
 50	skb = alloc_skb(len + ddev->tx_headroom + ddev->tx_tailroom,
 51			GFP_KERNEL);
 52	if (skb)
 53		skb_reserve(skb, ddev->tx_headroom);
 54
 55	return skb;
 56}
 57
 58void digital_skb_add_crc(struct sk_buff *skb, crc_func_t crc_func, u16 init,
 59			 u8 bitwise_inv, u8 msb_first)
 60{
 61	u16 crc;
 62
 63	crc = crc_func(init, skb->data, skb->len);
 64
 65	if (bitwise_inv)
 66		crc = ~crc;
 67
 68	if (msb_first)
 69		crc = __fswab16(crc);
 70
 71	*skb_put(skb, 1) = crc & 0xFF;
 72	*skb_put(skb, 1) = (crc >> 8) & 0xFF;
 73}
 74
 75int digital_skb_check_crc(struct sk_buff *skb, crc_func_t crc_func,
 76			  u16 crc_init, u8 bitwise_inv, u8 msb_first)
 77{
 78	int rc;
 79	u16 crc;
 80
 81	if (skb->len <= 2)
 82		return -EIO;
 83
 84	crc = crc_func(crc_init, skb->data, skb->len - 2);
 85
 86	if (bitwise_inv)
 87		crc = ~crc;
 88
 89	if (msb_first)
 90		crc = __swab16(crc);
 91
 92	rc = (skb->data[skb->len - 2] - (crc & 0xFF)) +
 93	     (skb->data[skb->len - 1] - ((crc >> 8) & 0xFF));
 94
 95	if (rc)
 96		return -EIO;
 97
 98	skb_trim(skb, skb->len - 2);
 99
100	return 0;
101}
102
103static inline void digital_switch_rf(struct nfc_digital_dev *ddev, bool on)
104{
105	ddev->ops->switch_rf(ddev, on);
106}
107
108static inline void digital_abort_cmd(struct nfc_digital_dev *ddev)
109{
110	ddev->ops->abort_cmd(ddev);
111}
112
113static void digital_wq_cmd_complete(struct work_struct *work)
114{
115	struct digital_cmd *cmd;
116	struct nfc_digital_dev *ddev = container_of(work,
117						    struct nfc_digital_dev,
118						    cmd_complete_work);
119
120	mutex_lock(&ddev->cmd_lock);
121
122	cmd = list_first_entry_or_null(&ddev->cmd_queue, struct digital_cmd,
123				       queue);
124	if (!cmd) {
125		mutex_unlock(&ddev->cmd_lock);
126		return;
127	}
128
129	list_del(&cmd->queue);
130
131	mutex_unlock(&ddev->cmd_lock);
132
133	if (!IS_ERR(cmd->resp))
134		print_hex_dump_debug("DIGITAL RX: ", DUMP_PREFIX_NONE, 16, 1,
135				     cmd->resp->data, cmd->resp->len, false);
136
137	cmd->cmd_cb(ddev, cmd->cb_context, cmd->resp);
138
139	kfree(cmd->mdaa_params);
140	kfree(cmd);
141
142	schedule_work(&ddev->cmd_work);
143}
144
145static void digital_send_cmd_complete(struct nfc_digital_dev *ddev,
146				      void *arg, struct sk_buff *resp)
147{
148	struct digital_cmd *cmd = arg;
149
150	cmd->resp = resp;
151
152	schedule_work(&ddev->cmd_complete_work);
153}
154
155static void digital_wq_cmd(struct work_struct *work)
156{
157	int rc;
158	struct digital_cmd *cmd;
159	struct digital_tg_mdaa_params *params;
160	struct nfc_digital_dev *ddev = container_of(work,
161						    struct nfc_digital_dev,
162						    cmd_work);
163
164	mutex_lock(&ddev->cmd_lock);
165
166	cmd = list_first_entry_or_null(&ddev->cmd_queue, struct digital_cmd,
167				       queue);
168	if (!cmd || cmd->pending) {
169		mutex_unlock(&ddev->cmd_lock);
170		return;
171	}
172
 
 
173	mutex_unlock(&ddev->cmd_lock);
174
175	if (cmd->req)
176		print_hex_dump_debug("DIGITAL TX: ", DUMP_PREFIX_NONE, 16, 1,
177				     cmd->req->data, cmd->req->len, false);
178
179	switch (cmd->type) {
180	case DIGITAL_CMD_IN_SEND:
181		rc = ddev->ops->in_send_cmd(ddev, cmd->req, cmd->timeout,
182					    digital_send_cmd_complete, cmd);
183		break;
184
185	case DIGITAL_CMD_TG_SEND:
186		rc = ddev->ops->tg_send_cmd(ddev, cmd->req, cmd->timeout,
187					    digital_send_cmd_complete, cmd);
188		break;
189
190	case DIGITAL_CMD_TG_LISTEN:
191		rc = ddev->ops->tg_listen(ddev, cmd->timeout,
192					  digital_send_cmd_complete, cmd);
193		break;
194
195	case DIGITAL_CMD_TG_LISTEN_MDAA:
196		params = cmd->mdaa_params;
197
198		rc = ddev->ops->tg_listen_mdaa(ddev, params, cmd->timeout,
199					       digital_send_cmd_complete, cmd);
200		break;
201
 
 
 
 
 
202	default:
203		pr_err("Unknown cmd type %d\n", cmd->type);
204		return;
205	}
206
207	if (!rc)
208		return;
209
210	pr_err("in_send_command returned err %d\n", rc);
211
212	mutex_lock(&ddev->cmd_lock);
213	list_del(&cmd->queue);
214	mutex_unlock(&ddev->cmd_lock);
215
216	kfree_skb(cmd->req);
217	kfree(cmd->mdaa_params);
218	kfree(cmd);
219
220	schedule_work(&ddev->cmd_work);
221}
222
223int digital_send_cmd(struct nfc_digital_dev *ddev, u8 cmd_type,
224		     struct sk_buff *skb, struct digital_tg_mdaa_params *params,
225		     u16 timeout, nfc_digital_cmd_complete_t cmd_cb,
226		     void *cb_context)
227{
228	struct digital_cmd *cmd;
229
230	cmd = kzalloc(sizeof(struct digital_cmd), GFP_KERNEL);
231	if (!cmd)
232		return -ENOMEM;
233
234	cmd->type = cmd_type;
235	cmd->timeout = timeout;
236	cmd->req = skb;
237	cmd->mdaa_params = params;
238	cmd->cmd_cb = cmd_cb;
239	cmd->cb_context = cb_context;
240	INIT_LIST_HEAD(&cmd->queue);
241
242	mutex_lock(&ddev->cmd_lock);
243	list_add_tail(&cmd->queue, &ddev->cmd_queue);
244	mutex_unlock(&ddev->cmd_lock);
245
246	schedule_work(&ddev->cmd_work);
247
248	return 0;
249}
250
251int digital_in_configure_hw(struct nfc_digital_dev *ddev, int type, int param)
252{
253	int rc;
254
255	rc = ddev->ops->in_configure_hw(ddev, type, param);
256	if (rc)
257		pr_err("in_configure_hw failed: %d\n", rc);
258
259	return rc;
260}
261
262int digital_tg_configure_hw(struct nfc_digital_dev *ddev, int type, int param)
263{
264	int rc;
265
266	rc = ddev->ops->tg_configure_hw(ddev, type, param);
267	if (rc)
268		pr_err("tg_configure_hw failed: %d\n", rc);
269
270	return rc;
271}
272
273static int digital_tg_listen_mdaa(struct nfc_digital_dev *ddev, u8 rf_tech)
274{
275	struct digital_tg_mdaa_params *params;
 
276
277	params = kzalloc(sizeof(struct digital_tg_mdaa_params), GFP_KERNEL);
278	if (!params)
279		return -ENOMEM;
280
281	params->sens_res = DIGITAL_SENS_RES_NFC_DEP;
282	get_random_bytes(params->nfcid1, sizeof(params->nfcid1));
283	params->sel_res = DIGITAL_SEL_RES_NFC_DEP;
284
285	params->nfcid2[0] = DIGITAL_SENSF_NFCID2_NFC_DEP_B1;
286	params->nfcid2[1] = DIGITAL_SENSF_NFCID2_NFC_DEP_B2;
287	get_random_bytes(params->nfcid2 + 2, NFC_NFCID2_MAXSIZE - 2);
288	params->sc = DIGITAL_SENSF_FELICA_SC;
289
290	return digital_send_cmd(ddev, DIGITAL_CMD_TG_LISTEN_MDAA, NULL, params,
291				500, digital_tg_recv_atr_req, NULL);
 
 
 
 
 
 
 
 
 
 
292}
293
294int digital_target_found(struct nfc_digital_dev *ddev,
295			 struct nfc_target *target, u8 protocol)
296{
297	int rc;
298	u8 framing;
299	u8 rf_tech;
 
300	int (*check_crc)(struct sk_buff *skb);
301	void (*add_crc)(struct sk_buff *skb);
302
303	rf_tech = ddev->poll_techs[ddev->poll_tech_index].rf_tech;
304
305	switch (protocol) {
306	case NFC_PROTO_JEWEL:
307		framing = NFC_DIGITAL_FRAMING_NFCA_T1T;
308		check_crc = digital_skb_check_crc_b;
309		add_crc = digital_skb_add_crc_b;
310		break;
311
312	case NFC_PROTO_MIFARE:
313		framing = NFC_DIGITAL_FRAMING_NFCA_T2T;
314		check_crc = digital_skb_check_crc_a;
315		add_crc = digital_skb_add_crc_a;
316		break;
317
318	case NFC_PROTO_FELICA:
319		framing = NFC_DIGITAL_FRAMING_NFCF_T3T;
320		check_crc = digital_skb_check_crc_f;
321		add_crc = digital_skb_add_crc_f;
322		break;
323
324	case NFC_PROTO_NFC_DEP:
325		if (rf_tech == NFC_DIGITAL_RF_TECH_106A) {
326			framing = NFC_DIGITAL_FRAMING_NFCA_NFC_DEP;
327			check_crc = digital_skb_check_crc_a;
328			add_crc = digital_skb_add_crc_a;
329		} else {
330			framing = NFC_DIGITAL_FRAMING_NFCF_NFC_DEP;
331			check_crc = digital_skb_check_crc_f;
332			add_crc = digital_skb_add_crc_f;
333		}
334		break;
335
336	case NFC_PROTO_ISO15693:
337		framing = NFC_DIGITAL_FRAMING_ISO15693_T5T;
338		check_crc = digital_skb_check_crc_b;
339		add_crc = digital_skb_add_crc_b;
340		break;
341
342	case NFC_PROTO_ISO14443:
343		framing = NFC_DIGITAL_FRAMING_NFCA_T4T;
344		check_crc = digital_skb_check_crc_a;
345		add_crc = digital_skb_add_crc_a;
346		break;
347
 
 
 
 
 
 
348	default:
349		pr_err("Invalid protocol %d\n", protocol);
350		return -EINVAL;
351	}
352
353	pr_debug("rf_tech=%d, protocol=%d\n", rf_tech, protocol);
354
355	ddev->curr_rf_tech = rf_tech;
356
357	if (DIGITAL_DRV_CAPS_IN_CRC(ddev)) {
358		ddev->skb_add_crc = digital_skb_add_crc_none;
359		ddev->skb_check_crc = digital_skb_check_crc_none;
360	} else {
361		ddev->skb_add_crc = add_crc;
362		ddev->skb_check_crc = check_crc;
363	}
364
365	rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING, framing);
366	if (rc)
367		return rc;
368
369	target->supported_protocols = (1 << protocol);
370	rc = nfc_targets_found(ddev->nfc_dev, target, 1);
371	if (rc)
372		return rc;
373
 
374	ddev->poll_tech_count = 0;
375
 
 
 
 
 
 
376	return 0;
377}
378
379void digital_poll_next_tech(struct nfc_digital_dev *ddev)
380{
 
 
381	digital_switch_rf(ddev, 0);
382
383	mutex_lock(&ddev->poll_lock);
384
385	if (!ddev->poll_tech_count) {
386		mutex_unlock(&ddev->poll_lock);
387		return;
388	}
389
390	ddev->poll_tech_index = (ddev->poll_tech_index + 1) %
391				ddev->poll_tech_count;
392
393	mutex_unlock(&ddev->poll_lock);
394
395	schedule_work(&ddev->poll_work);
 
396}
397
398static void digital_wq_poll(struct work_struct *work)
399{
400	int rc;
401	struct digital_poll_tech *poll_tech;
402	struct nfc_digital_dev *ddev = container_of(work,
403						    struct nfc_digital_dev,
404						    poll_work);
405	mutex_lock(&ddev->poll_lock);
406
407	if (!ddev->poll_tech_count) {
408		mutex_unlock(&ddev->poll_lock);
409		return;
410	}
411
412	poll_tech = &ddev->poll_techs[ddev->poll_tech_index];
413
414	mutex_unlock(&ddev->poll_lock);
415
416	rc = poll_tech->poll_func(ddev, poll_tech->rf_tech);
417	if (rc)
418		digital_poll_next_tech(ddev);
419}
420
421static void digital_add_poll_tech(struct nfc_digital_dev *ddev, u8 rf_tech,
422				  digital_poll_t poll_func)
423{
424	struct digital_poll_tech *poll_tech;
425
426	if (ddev->poll_tech_count >= NFC_DIGITAL_POLL_MODE_COUNT_MAX)
427		return;
428
429	poll_tech = &ddev->poll_techs[ddev->poll_tech_count++];
430
431	poll_tech->rf_tech = rf_tech;
432	poll_tech->poll_func = poll_func;
433}
434
435/**
436 * start_poll operation
 
 
 
437 *
438 * For every supported protocol, the corresponding polling function is added
439 * to the table of polling technologies (ddev->poll_techs[]) using
440 * digital_add_poll_tech().
441 * When a polling function fails (by timeout or protocol error) the next one is
442 * schedule by digital_poll_next_tech() on the poll workqueue (ddev->poll_work).
443 */
444static int digital_start_poll(struct nfc_dev *nfc_dev, __u32 im_protocols,
445			      __u32 tm_protocols)
446{
447	struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
448	u32 matching_im_protocols, matching_tm_protocols;
449
450	pr_debug("protocols: im 0x%x, tm 0x%x, supported 0x%x\n", im_protocols,
451		 tm_protocols, ddev->protocols);
452
453	matching_im_protocols = ddev->protocols & im_protocols;
454	matching_tm_protocols = ddev->protocols & tm_protocols;
455
456	if (!matching_im_protocols && !matching_tm_protocols) {
457		pr_err("Unknown protocol\n");
458		return -EINVAL;
459	}
460
461	if (ddev->poll_tech_count) {
462		pr_err("Already polling\n");
463		return -EBUSY;
464	}
465
466	if (ddev->curr_protocol) {
467		pr_err("A target is already active\n");
468		return -EBUSY;
469	}
470
471	ddev->poll_tech_count = 0;
472	ddev->poll_tech_index = 0;
473
474	if (matching_im_protocols & DIGITAL_PROTO_NFCA_RF_TECH)
475		digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_106A,
476				      digital_in_send_sens_req);
477
 
 
 
 
478	if (matching_im_protocols & DIGITAL_PROTO_NFCF_RF_TECH) {
479		digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_212F,
480				      digital_in_send_sensf_req);
481
482		digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_424F,
483				      digital_in_send_sensf_req);
484	}
485
486	if (matching_im_protocols & DIGITAL_PROTO_ISO15693_RF_TECH)
487		digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_ISO15693,
488				      digital_in_send_iso15693_inv_req);
489
490	if (matching_tm_protocols & NFC_PROTO_NFC_DEP_MASK) {
491		if (ddev->ops->tg_listen_mdaa) {
492			digital_add_poll_tech(ddev, 0,
493					      digital_tg_listen_mdaa);
 
 
 
494		} else {
495			digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_106A,
496					      digital_tg_listen_nfca);
497
498			digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_212F,
499					      digital_tg_listen_nfcf);
500
501			digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_424F,
502					      digital_tg_listen_nfcf);
503		}
504	}
505
506	if (!ddev->poll_tech_count) {
507		pr_err("Unsupported protocols: im=0x%x, tm=0x%x\n",
508		       matching_im_protocols, matching_tm_protocols);
509		return -EINVAL;
510	}
511
512	schedule_work(&ddev->poll_work);
513
514	return 0;
515}
516
517static void digital_stop_poll(struct nfc_dev *nfc_dev)
518{
519	struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
520
521	mutex_lock(&ddev->poll_lock);
522
523	if (!ddev->poll_tech_count) {
524		pr_err("Polling operation was not running\n");
525		mutex_unlock(&ddev->poll_lock);
526		return;
527	}
528
529	ddev->poll_tech_count = 0;
530
531	mutex_unlock(&ddev->poll_lock);
532
533	cancel_work_sync(&ddev->poll_work);
534
535	digital_abort_cmd(ddev);
536}
537
538static int digital_dev_up(struct nfc_dev *nfc_dev)
539{
540	struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
541
542	digital_switch_rf(ddev, 1);
543
544	return 0;
545}
546
547static int digital_dev_down(struct nfc_dev *nfc_dev)
548{
549	struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
550
551	digital_switch_rf(ddev, 0);
552
553	return 0;
554}
555
556static int digital_dep_link_up(struct nfc_dev *nfc_dev,
557			       struct nfc_target *target,
558			       __u8 comm_mode, __u8 *gb, size_t gb_len)
559{
560	struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
561	int rc;
562
563	rc = digital_in_send_atr_req(ddev, target, comm_mode, gb, gb_len);
564
565	if (!rc)
566		ddev->curr_protocol = NFC_PROTO_NFC_DEP;
567
568	return rc;
569}
570
571static int digital_dep_link_down(struct nfc_dev *nfc_dev)
572{
573	struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
574
 
 
575	ddev->curr_protocol = 0;
576
577	return 0;
578}
579
580static int digital_activate_target(struct nfc_dev *nfc_dev,
581				   struct nfc_target *target, __u32 protocol)
582{
583	struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
584
585	if (ddev->poll_tech_count) {
586		pr_err("Can't activate a target while polling\n");
587		return -EBUSY;
588	}
589
590	if (ddev->curr_protocol) {
591		pr_err("A target is already active\n");
592		return -EBUSY;
593	}
594
595	ddev->curr_protocol = protocol;
596
597	return 0;
598}
599
600static void digital_deactivate_target(struct nfc_dev *nfc_dev,
601				      struct nfc_target *target)
 
602{
603	struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
604
605	if (!ddev->curr_protocol) {
606		pr_err("No active target\n");
607		return;
608	}
609
 
610	ddev->curr_protocol = 0;
611}
612
613static int digital_tg_send(struct nfc_dev *dev, struct sk_buff *skb)
614{
615	struct nfc_digital_dev *ddev = nfc_get_drvdata(dev);
616
617	return digital_tg_send_dep_res(ddev, skb);
618}
619
620static void digital_in_send_complete(struct nfc_digital_dev *ddev, void *arg,
621				     struct sk_buff *resp)
622{
623	struct digital_data_exch *data_exch = arg;
624	int rc;
625
626	if (IS_ERR(resp)) {
627		rc = PTR_ERR(resp);
628		resp = NULL;
629		goto done;
630	}
631
632	if (ddev->curr_protocol == NFC_PROTO_MIFARE) {
633		rc = digital_in_recv_mifare_res(resp);
634		/* crc check is done in digital_in_recv_mifare_res() */
635		goto done;
636	}
637
638	if (ddev->curr_protocol == NFC_PROTO_ISO14443) {
 
639		rc = digital_in_iso_dep_pull_sod(ddev, resp);
640		if (rc)
641			goto done;
642	}
643
644	rc = ddev->skb_check_crc(resp);
645
646done:
647	if (rc) {
648		kfree_skb(resp);
649		resp = NULL;
650	}
651
652	data_exch->cb(data_exch->cb_context, resp, rc);
653
654	kfree(data_exch);
655}
656
657static int digital_in_send(struct nfc_dev *nfc_dev, struct nfc_target *target,
658			   struct sk_buff *skb, data_exchange_cb_t cb,
659			   void *cb_context)
660{
661	struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
662	struct digital_data_exch *data_exch;
663	int rc;
664
665	data_exch = kzalloc(sizeof(struct digital_data_exch), GFP_KERNEL);
666	if (!data_exch) {
667		pr_err("Failed to allocate data_exch struct\n");
668		return -ENOMEM;
669	}
670
671	data_exch->cb = cb;
672	data_exch->cb_context = cb_context;
673
674	if (ddev->curr_protocol == NFC_PROTO_NFC_DEP) {
675		rc = digital_in_send_dep_req(ddev, target, skb, data_exch);
676		goto exit;
677	}
678
679	if (ddev->curr_protocol == NFC_PROTO_ISO14443) {
 
680		rc = digital_in_iso_dep_push_sod(ddev, skb);
681		if (rc)
682			goto exit;
683	}
684
685	ddev->skb_add_crc(skb);
686
687	rc = digital_in_send_cmd(ddev, skb, 500, digital_in_send_complete,
688				 data_exch);
689
690exit:
691	if (rc)
692		kfree(data_exch);
693
694	return rc;
695}
696
697static struct nfc_ops digital_nfc_ops = {
698	.dev_up = digital_dev_up,
699	.dev_down = digital_dev_down,
700	.start_poll = digital_start_poll,
701	.stop_poll = digital_stop_poll,
702	.dep_link_up = digital_dep_link_up,
703	.dep_link_down = digital_dep_link_down,
704	.activate_target = digital_activate_target,
705	.deactivate_target = digital_deactivate_target,
706	.tm_send = digital_tg_send,
707	.im_transceive = digital_in_send,
708};
709
710struct nfc_digital_dev *nfc_digital_allocate_device(struct nfc_digital_ops *ops,
711					    __u32 supported_protocols,
712					    __u32 driver_capabilities,
713					    int tx_headroom, int tx_tailroom)
714{
715	struct nfc_digital_dev *ddev;
716
717	if (!ops->in_configure_hw || !ops->in_send_cmd || !ops->tg_listen ||
718	    !ops->tg_configure_hw || !ops->tg_send_cmd || !ops->abort_cmd ||
719	    !ops->switch_rf)
720		return NULL;
721
722	ddev = kzalloc(sizeof(struct nfc_digital_dev), GFP_KERNEL);
723	if (!ddev)
724		return NULL;
725
726	ddev->driver_capabilities = driver_capabilities;
727	ddev->ops = ops;
728
729	mutex_init(&ddev->cmd_lock);
730	INIT_LIST_HEAD(&ddev->cmd_queue);
731
732	INIT_WORK(&ddev->cmd_work, digital_wq_cmd);
733	INIT_WORK(&ddev->cmd_complete_work, digital_wq_cmd_complete);
734
735	mutex_init(&ddev->poll_lock);
736	INIT_WORK(&ddev->poll_work, digital_wq_poll);
737
738	if (supported_protocols & NFC_PROTO_JEWEL_MASK)
739		ddev->protocols |= NFC_PROTO_JEWEL_MASK;
740	if (supported_protocols & NFC_PROTO_MIFARE_MASK)
741		ddev->protocols |= NFC_PROTO_MIFARE_MASK;
742	if (supported_protocols & NFC_PROTO_FELICA_MASK)
743		ddev->protocols |= NFC_PROTO_FELICA_MASK;
744	if (supported_protocols & NFC_PROTO_NFC_DEP_MASK)
745		ddev->protocols |= NFC_PROTO_NFC_DEP_MASK;
746	if (supported_protocols & NFC_PROTO_ISO15693_MASK)
747		ddev->protocols |= NFC_PROTO_ISO15693_MASK;
748	if (supported_protocols & NFC_PROTO_ISO14443_MASK)
749		ddev->protocols |= NFC_PROTO_ISO14443_MASK;
 
 
750
751	ddev->tx_headroom = tx_headroom + DIGITAL_MAX_HEADER_LEN;
752	ddev->tx_tailroom = tx_tailroom + DIGITAL_CRC_LEN;
753
754	ddev->nfc_dev = nfc_allocate_device(&digital_nfc_ops, ddev->protocols,
755					    ddev->tx_headroom,
756					    ddev->tx_tailroom);
757	if (!ddev->nfc_dev) {
758		pr_err("nfc_allocate_device failed\n");
759		goto free_dev;
760	}
761
762	nfc_set_drvdata(ddev->nfc_dev, ddev);
763
764	return ddev;
765
766free_dev:
767	kfree(ddev);
768
769	return NULL;
770}
771EXPORT_SYMBOL(nfc_digital_allocate_device);
772
773void nfc_digital_free_device(struct nfc_digital_dev *ddev)
774{
775	nfc_free_device(ddev->nfc_dev);
776	kfree(ddev);
777}
778EXPORT_SYMBOL(nfc_digital_free_device);
779
780int nfc_digital_register_device(struct nfc_digital_dev *ddev)
781{
782	return nfc_register_device(ddev->nfc_dev);
783}
784EXPORT_SYMBOL(nfc_digital_register_device);
785
786void nfc_digital_unregister_device(struct nfc_digital_dev *ddev)
787{
788	struct digital_cmd *cmd, *n;
789
790	nfc_unregister_device(ddev->nfc_dev);
791
792	mutex_lock(&ddev->poll_lock);
793	ddev->poll_tech_count = 0;
794	mutex_unlock(&ddev->poll_lock);
795
796	cancel_work_sync(&ddev->poll_work);
797	cancel_work_sync(&ddev->cmd_work);
798	cancel_work_sync(&ddev->cmd_complete_work);
799
800	list_for_each_entry_safe(cmd, n, &ddev->cmd_queue, queue) {
801		list_del(&cmd->queue);
 
 
 
 
 
 
 
 
802		kfree(cmd->mdaa_params);
803		kfree(cmd);
804	}
805}
806EXPORT_SYMBOL(nfc_digital_unregister_device);
807
 
808MODULE_LICENSE("GPL");
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * NFC Digital Protocol stack
  4 * Copyright (c) 2013, Intel Corporation.
 
 
 
 
 
 
 
 
 
 
  5 */
  6
  7#define pr_fmt(fmt) "digital: %s: " fmt, __func__
  8
  9#include <linux/module.h>
 10
 11#include "digital.h"
 12
 13#define DIGITAL_PROTO_NFCA_RF_TECH \
 14	(NFC_PROTO_JEWEL_MASK | NFC_PROTO_MIFARE_MASK | \
 15	NFC_PROTO_NFC_DEP_MASK | NFC_PROTO_ISO14443_MASK)
 16
 17#define DIGITAL_PROTO_NFCB_RF_TECH	NFC_PROTO_ISO14443_B_MASK
 18
 19#define DIGITAL_PROTO_NFCF_RF_TECH \
 20	(NFC_PROTO_FELICA_MASK | NFC_PROTO_NFC_DEP_MASK)
 21
 22#define DIGITAL_PROTO_ISO15693_RF_TECH	NFC_PROTO_ISO15693_MASK
 23
 24/* Delay between each poll frame (ms) */
 25#define DIGITAL_POLL_INTERVAL 10
 26
 27struct digital_cmd {
 28	struct list_head queue;
 29
 30	u8 type;
 31	u8 pending;
 32
 33	u16 timeout;
 34	struct sk_buff *req;
 35	struct sk_buff *resp;
 36	struct digital_tg_mdaa_params *mdaa_params;
 37
 38	nfc_digital_cmd_complete_t cmd_cb;
 39	void *cb_context;
 40};
 41
 42struct sk_buff *digital_skb_alloc(struct nfc_digital_dev *ddev,
 43				  unsigned int len)
 44{
 45	struct sk_buff *skb;
 46
 47	skb = alloc_skb(len + ddev->tx_headroom + ddev->tx_tailroom,
 48			GFP_KERNEL);
 49	if (skb)
 50		skb_reserve(skb, ddev->tx_headroom);
 51
 52	return skb;
 53}
 54
 55void digital_skb_add_crc(struct sk_buff *skb, crc_func_t crc_func, u16 init,
 56			 u8 bitwise_inv, u8 msb_first)
 57{
 58	u16 crc;
 59
 60	crc = crc_func(init, skb->data, skb->len);
 61
 62	if (bitwise_inv)
 63		crc = ~crc;
 64
 65	if (msb_first)
 66		crc = __fswab16(crc);
 67
 68	skb_put_u8(skb, crc & 0xFF);
 69	skb_put_u8(skb, (crc >> 8) & 0xFF);
 70}
 71
 72int digital_skb_check_crc(struct sk_buff *skb, crc_func_t crc_func,
 73			  u16 crc_init, u8 bitwise_inv, u8 msb_first)
 74{
 75	int rc;
 76	u16 crc;
 77
 78	if (skb->len <= 2)
 79		return -EIO;
 80
 81	crc = crc_func(crc_init, skb->data, skb->len - 2);
 82
 83	if (bitwise_inv)
 84		crc = ~crc;
 85
 86	if (msb_first)
 87		crc = __swab16(crc);
 88
 89	rc = (skb->data[skb->len - 2] - (crc & 0xFF)) +
 90	     (skb->data[skb->len - 1] - ((crc >> 8) & 0xFF));
 91
 92	if (rc)
 93		return -EIO;
 94
 95	skb_trim(skb, skb->len - 2);
 96
 97	return 0;
 98}
 99
100static inline void digital_switch_rf(struct nfc_digital_dev *ddev, bool on)
101{
102	ddev->ops->switch_rf(ddev, on);
103}
104
105static inline void digital_abort_cmd(struct nfc_digital_dev *ddev)
106{
107	ddev->ops->abort_cmd(ddev);
108}
109
110static void digital_wq_cmd_complete(struct work_struct *work)
111{
112	struct digital_cmd *cmd;
113	struct nfc_digital_dev *ddev = container_of(work,
114						    struct nfc_digital_dev,
115						    cmd_complete_work);
116
117	mutex_lock(&ddev->cmd_lock);
118
119	cmd = list_first_entry_or_null(&ddev->cmd_queue, struct digital_cmd,
120				       queue);
121	if (!cmd) {
122		mutex_unlock(&ddev->cmd_lock);
123		return;
124	}
125
126	list_del(&cmd->queue);
127
128	mutex_unlock(&ddev->cmd_lock);
129
130	if (!IS_ERR(cmd->resp))
131		print_hex_dump_debug("DIGITAL RX: ", DUMP_PREFIX_NONE, 16, 1,
132				     cmd->resp->data, cmd->resp->len, false);
133
134	cmd->cmd_cb(ddev, cmd->cb_context, cmd->resp);
135
136	kfree(cmd->mdaa_params);
137	kfree(cmd);
138
139	schedule_work(&ddev->cmd_work);
140}
141
142static void digital_send_cmd_complete(struct nfc_digital_dev *ddev,
143				      void *arg, struct sk_buff *resp)
144{
145	struct digital_cmd *cmd = arg;
146
147	cmd->resp = resp;
148
149	schedule_work(&ddev->cmd_complete_work);
150}
151
152static void digital_wq_cmd(struct work_struct *work)
153{
154	int rc;
155	struct digital_cmd *cmd;
156	struct digital_tg_mdaa_params *params;
157	struct nfc_digital_dev *ddev = container_of(work,
158						    struct nfc_digital_dev,
159						    cmd_work);
160
161	mutex_lock(&ddev->cmd_lock);
162
163	cmd = list_first_entry_or_null(&ddev->cmd_queue, struct digital_cmd,
164				       queue);
165	if (!cmd || cmd->pending) {
166		mutex_unlock(&ddev->cmd_lock);
167		return;
168	}
169
170	cmd->pending = 1;
171
172	mutex_unlock(&ddev->cmd_lock);
173
174	if (cmd->req)
175		print_hex_dump_debug("DIGITAL TX: ", DUMP_PREFIX_NONE, 16, 1,
176				     cmd->req->data, cmd->req->len, false);
177
178	switch (cmd->type) {
179	case DIGITAL_CMD_IN_SEND:
180		rc = ddev->ops->in_send_cmd(ddev, cmd->req, cmd->timeout,
181					    digital_send_cmd_complete, cmd);
182		break;
183
184	case DIGITAL_CMD_TG_SEND:
185		rc = ddev->ops->tg_send_cmd(ddev, cmd->req, cmd->timeout,
186					    digital_send_cmd_complete, cmd);
187		break;
188
189	case DIGITAL_CMD_TG_LISTEN:
190		rc = ddev->ops->tg_listen(ddev, cmd->timeout,
191					  digital_send_cmd_complete, cmd);
192		break;
193
194	case DIGITAL_CMD_TG_LISTEN_MDAA:
195		params = cmd->mdaa_params;
196
197		rc = ddev->ops->tg_listen_mdaa(ddev, params, cmd->timeout,
198					       digital_send_cmd_complete, cmd);
199		break;
200
201	case DIGITAL_CMD_TG_LISTEN_MD:
202		rc = ddev->ops->tg_listen_md(ddev, cmd->timeout,
203					       digital_send_cmd_complete, cmd);
204		break;
205
206	default:
207		pr_err("Unknown cmd type %d\n", cmd->type);
208		return;
209	}
210
211	if (!rc)
212		return;
213
214	pr_err("in_send_command returned err %d\n", rc);
215
216	mutex_lock(&ddev->cmd_lock);
217	list_del(&cmd->queue);
218	mutex_unlock(&ddev->cmd_lock);
219
220	kfree_skb(cmd->req);
221	kfree(cmd->mdaa_params);
222	kfree(cmd);
223
224	schedule_work(&ddev->cmd_work);
225}
226
227int digital_send_cmd(struct nfc_digital_dev *ddev, u8 cmd_type,
228		     struct sk_buff *skb, struct digital_tg_mdaa_params *params,
229		     u16 timeout, nfc_digital_cmd_complete_t cmd_cb,
230		     void *cb_context)
231{
232	struct digital_cmd *cmd;
233
234	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
235	if (!cmd)
236		return -ENOMEM;
237
238	cmd->type = cmd_type;
239	cmd->timeout = timeout;
240	cmd->req = skb;
241	cmd->mdaa_params = params;
242	cmd->cmd_cb = cmd_cb;
243	cmd->cb_context = cb_context;
244	INIT_LIST_HEAD(&cmd->queue);
245
246	mutex_lock(&ddev->cmd_lock);
247	list_add_tail(&cmd->queue, &ddev->cmd_queue);
248	mutex_unlock(&ddev->cmd_lock);
249
250	schedule_work(&ddev->cmd_work);
251
252	return 0;
253}
254
255int digital_in_configure_hw(struct nfc_digital_dev *ddev, int type, int param)
256{
257	int rc;
258
259	rc = ddev->ops->in_configure_hw(ddev, type, param);
260	if (rc)
261		pr_err("in_configure_hw failed: %d\n", rc);
262
263	return rc;
264}
265
266int digital_tg_configure_hw(struct nfc_digital_dev *ddev, int type, int param)
267{
268	int rc;
269
270	rc = ddev->ops->tg_configure_hw(ddev, type, param);
271	if (rc)
272		pr_err("tg_configure_hw failed: %d\n", rc);
273
274	return rc;
275}
276
277static int digital_tg_listen_mdaa(struct nfc_digital_dev *ddev, u8 rf_tech)
278{
279	struct digital_tg_mdaa_params *params;
280	int rc;
281
282	params = kzalloc(sizeof(*params), GFP_KERNEL);
283	if (!params)
284		return -ENOMEM;
285
286	params->sens_res = DIGITAL_SENS_RES_NFC_DEP;
287	get_random_bytes(params->nfcid1, sizeof(params->nfcid1));
288	params->sel_res = DIGITAL_SEL_RES_NFC_DEP;
289
290	params->nfcid2[0] = DIGITAL_SENSF_NFCID2_NFC_DEP_B1;
291	params->nfcid2[1] = DIGITAL_SENSF_NFCID2_NFC_DEP_B2;
292	get_random_bytes(params->nfcid2 + 2, NFC_NFCID2_MAXSIZE - 2);
293	params->sc = DIGITAL_SENSF_FELICA_SC;
294
295	rc = digital_send_cmd(ddev, DIGITAL_CMD_TG_LISTEN_MDAA, NULL, params,
296			      500, digital_tg_recv_atr_req, NULL);
297	if (rc)
298		kfree(params);
299
300	return rc;
301}
302
303static int digital_tg_listen_md(struct nfc_digital_dev *ddev, u8 rf_tech)
304{
305	return digital_send_cmd(ddev, DIGITAL_CMD_TG_LISTEN_MD, NULL, NULL, 500,
306				digital_tg_recv_md_req, NULL);
307}
308
309int digital_target_found(struct nfc_digital_dev *ddev,
310			 struct nfc_target *target, u8 protocol)
311{
312	int rc;
313	u8 framing;
314	u8 rf_tech;
315	u8 poll_tech_count;
316	int (*check_crc)(struct sk_buff *skb);
317	void (*add_crc)(struct sk_buff *skb);
318
319	rf_tech = ddev->poll_techs[ddev->poll_tech_index].rf_tech;
320
321	switch (protocol) {
322	case NFC_PROTO_JEWEL:
323		framing = NFC_DIGITAL_FRAMING_NFCA_T1T;
324		check_crc = digital_skb_check_crc_b;
325		add_crc = digital_skb_add_crc_b;
326		break;
327
328	case NFC_PROTO_MIFARE:
329		framing = NFC_DIGITAL_FRAMING_NFCA_T2T;
330		check_crc = digital_skb_check_crc_a;
331		add_crc = digital_skb_add_crc_a;
332		break;
333
334	case NFC_PROTO_FELICA:
335		framing = NFC_DIGITAL_FRAMING_NFCF_T3T;
336		check_crc = digital_skb_check_crc_f;
337		add_crc = digital_skb_add_crc_f;
338		break;
339
340	case NFC_PROTO_NFC_DEP:
341		if (rf_tech == NFC_DIGITAL_RF_TECH_106A) {
342			framing = NFC_DIGITAL_FRAMING_NFCA_NFC_DEP;
343			check_crc = digital_skb_check_crc_a;
344			add_crc = digital_skb_add_crc_a;
345		} else {
346			framing = NFC_DIGITAL_FRAMING_NFCF_NFC_DEP;
347			check_crc = digital_skb_check_crc_f;
348			add_crc = digital_skb_add_crc_f;
349		}
350		break;
351
352	case NFC_PROTO_ISO15693:
353		framing = NFC_DIGITAL_FRAMING_ISO15693_T5T;
354		check_crc = digital_skb_check_crc_b;
355		add_crc = digital_skb_add_crc_b;
356		break;
357
358	case NFC_PROTO_ISO14443:
359		framing = NFC_DIGITAL_FRAMING_NFCA_T4T;
360		check_crc = digital_skb_check_crc_a;
361		add_crc = digital_skb_add_crc_a;
362		break;
363
364	case NFC_PROTO_ISO14443_B:
365		framing = NFC_DIGITAL_FRAMING_NFCB_T4T;
366		check_crc = digital_skb_check_crc_b;
367		add_crc = digital_skb_add_crc_b;
368		break;
369
370	default:
371		pr_err("Invalid protocol %d\n", protocol);
372		return -EINVAL;
373	}
374
375	pr_debug("rf_tech=%d, protocol=%d\n", rf_tech, protocol);
376
377	ddev->curr_rf_tech = rf_tech;
378
379	if (DIGITAL_DRV_CAPS_IN_CRC(ddev)) {
380		ddev->skb_add_crc = digital_skb_add_crc_none;
381		ddev->skb_check_crc = digital_skb_check_crc_none;
382	} else {
383		ddev->skb_add_crc = add_crc;
384		ddev->skb_check_crc = check_crc;
385	}
386
387	rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING, framing);
388	if (rc)
389		return rc;
390
391	target->supported_protocols = (1 << protocol);
 
 
 
392
393	poll_tech_count = ddev->poll_tech_count;
394	ddev->poll_tech_count = 0;
395
396	rc = nfc_targets_found(ddev->nfc_dev, target, 1);
397	if (rc) {
398		ddev->poll_tech_count = poll_tech_count;
399		return rc;
400	}
401
402	return 0;
403}
404
405void digital_poll_next_tech(struct nfc_digital_dev *ddev)
406{
407	u8 rand_mod;
408
409	digital_switch_rf(ddev, 0);
410
411	mutex_lock(&ddev->poll_lock);
412
413	if (!ddev->poll_tech_count) {
414		mutex_unlock(&ddev->poll_lock);
415		return;
416	}
417
418	get_random_bytes(&rand_mod, sizeof(rand_mod));
419	ddev->poll_tech_index = rand_mod % ddev->poll_tech_count;
420
421	mutex_unlock(&ddev->poll_lock);
422
423	schedule_delayed_work(&ddev->poll_work,
424			      msecs_to_jiffies(DIGITAL_POLL_INTERVAL));
425}
426
427static void digital_wq_poll(struct work_struct *work)
428{
429	int rc;
430	struct digital_poll_tech *poll_tech;
431	struct nfc_digital_dev *ddev = container_of(work,
432						    struct nfc_digital_dev,
433						    poll_work.work);
434	mutex_lock(&ddev->poll_lock);
435
436	if (!ddev->poll_tech_count) {
437		mutex_unlock(&ddev->poll_lock);
438		return;
439	}
440
441	poll_tech = &ddev->poll_techs[ddev->poll_tech_index];
442
443	mutex_unlock(&ddev->poll_lock);
444
445	rc = poll_tech->poll_func(ddev, poll_tech->rf_tech);
446	if (rc)
447		digital_poll_next_tech(ddev);
448}
449
450static void digital_add_poll_tech(struct nfc_digital_dev *ddev, u8 rf_tech,
451				  digital_poll_t poll_func)
452{
453	struct digital_poll_tech *poll_tech;
454
455	if (ddev->poll_tech_count >= NFC_DIGITAL_POLL_MODE_COUNT_MAX)
456		return;
457
458	poll_tech = &ddev->poll_techs[ddev->poll_tech_count++];
459
460	poll_tech->rf_tech = rf_tech;
461	poll_tech->poll_func = poll_func;
462}
463
464/**
465 * digital_start_poll - start_poll operation
466 * @nfc_dev: device to be polled
467 * @im_protocols: bitset of nfc initiator protocols to be used for polling
468 * @tm_protocols: bitset of nfc transport protocols to be used for polling
469 *
470 * For every supported protocol, the corresponding polling function is added
471 * to the table of polling technologies (ddev->poll_techs[]) using
472 * digital_add_poll_tech().
473 * When a polling function fails (by timeout or protocol error) the next one is
474 * schedule by digital_poll_next_tech() on the poll workqueue (ddev->poll_work).
475 */
476static int digital_start_poll(struct nfc_dev *nfc_dev, __u32 im_protocols,
477			      __u32 tm_protocols)
478{
479	struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
480	u32 matching_im_protocols, matching_tm_protocols;
481
482	pr_debug("protocols: im 0x%x, tm 0x%x, supported 0x%x\n", im_protocols,
483		 tm_protocols, ddev->protocols);
484
485	matching_im_protocols = ddev->protocols & im_protocols;
486	matching_tm_protocols = ddev->protocols & tm_protocols;
487
488	if (!matching_im_protocols && !matching_tm_protocols) {
489		pr_err("Unknown protocol\n");
490		return -EINVAL;
491	}
492
493	if (ddev->poll_tech_count) {
494		pr_err("Already polling\n");
495		return -EBUSY;
496	}
497
498	if (ddev->curr_protocol) {
499		pr_err("A target is already active\n");
500		return -EBUSY;
501	}
502
503	ddev->poll_tech_count = 0;
504	ddev->poll_tech_index = 0;
505
506	if (matching_im_protocols & DIGITAL_PROTO_NFCA_RF_TECH)
507		digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_106A,
508				      digital_in_send_sens_req);
509
510	if (matching_im_protocols & DIGITAL_PROTO_NFCB_RF_TECH)
511		digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_106B,
512				      digital_in_send_sensb_req);
513
514	if (matching_im_protocols & DIGITAL_PROTO_NFCF_RF_TECH) {
515		digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_212F,
516				      digital_in_send_sensf_req);
517
518		digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_424F,
519				      digital_in_send_sensf_req);
520	}
521
522	if (matching_im_protocols & DIGITAL_PROTO_ISO15693_RF_TECH)
523		digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_ISO15693,
524				      digital_in_send_iso15693_inv_req);
525
526	if (matching_tm_protocols & NFC_PROTO_NFC_DEP_MASK) {
527		if (ddev->ops->tg_listen_mdaa) {
528			digital_add_poll_tech(ddev, 0,
529					      digital_tg_listen_mdaa);
530		} else if (ddev->ops->tg_listen_md) {
531			digital_add_poll_tech(ddev, 0,
532					      digital_tg_listen_md);
533		} else {
534			digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_106A,
535					      digital_tg_listen_nfca);
536
537			digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_212F,
538					      digital_tg_listen_nfcf);
539
540			digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_424F,
541					      digital_tg_listen_nfcf);
542		}
543	}
544
545	if (!ddev->poll_tech_count) {
546		pr_err("Unsupported protocols: im=0x%x, tm=0x%x\n",
547		       matching_im_protocols, matching_tm_protocols);
548		return -EINVAL;
549	}
550
551	schedule_delayed_work(&ddev->poll_work, 0);
552
553	return 0;
554}
555
556static void digital_stop_poll(struct nfc_dev *nfc_dev)
557{
558	struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
559
560	mutex_lock(&ddev->poll_lock);
561
562	if (!ddev->poll_tech_count) {
563		pr_err("Polling operation was not running\n");
564		mutex_unlock(&ddev->poll_lock);
565		return;
566	}
567
568	ddev->poll_tech_count = 0;
569
570	mutex_unlock(&ddev->poll_lock);
571
572	cancel_delayed_work_sync(&ddev->poll_work);
573
574	digital_abort_cmd(ddev);
575}
576
577static int digital_dev_up(struct nfc_dev *nfc_dev)
578{
579	struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
580
581	digital_switch_rf(ddev, 1);
582
583	return 0;
584}
585
586static int digital_dev_down(struct nfc_dev *nfc_dev)
587{
588	struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
589
590	digital_switch_rf(ddev, 0);
591
592	return 0;
593}
594
595static int digital_dep_link_up(struct nfc_dev *nfc_dev,
596			       struct nfc_target *target,
597			       __u8 comm_mode, __u8 *gb, size_t gb_len)
598{
599	struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
600	int rc;
601
602	rc = digital_in_send_atr_req(ddev, target, comm_mode, gb, gb_len);
603
604	if (!rc)
605		ddev->curr_protocol = NFC_PROTO_NFC_DEP;
606
607	return rc;
608}
609
610static int digital_dep_link_down(struct nfc_dev *nfc_dev)
611{
612	struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
613
614	digital_abort_cmd(ddev);
615
616	ddev->curr_protocol = 0;
617
618	return 0;
619}
620
621static int digital_activate_target(struct nfc_dev *nfc_dev,
622				   struct nfc_target *target, __u32 protocol)
623{
624	struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
625
626	if (ddev->poll_tech_count) {
627		pr_err("Can't activate a target while polling\n");
628		return -EBUSY;
629	}
630
631	if (ddev->curr_protocol) {
632		pr_err("A target is already active\n");
633		return -EBUSY;
634	}
635
636	ddev->curr_protocol = protocol;
637
638	return 0;
639}
640
641static void digital_deactivate_target(struct nfc_dev *nfc_dev,
642				      struct nfc_target *target,
643				      u8 mode)
644{
645	struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
646
647	if (!ddev->curr_protocol) {
648		pr_err("No active target\n");
649		return;
650	}
651
652	digital_abort_cmd(ddev);
653	ddev->curr_protocol = 0;
654}
655
656static int digital_tg_send(struct nfc_dev *dev, struct sk_buff *skb)
657{
658	struct nfc_digital_dev *ddev = nfc_get_drvdata(dev);
659
660	return digital_tg_send_dep_res(ddev, skb);
661}
662
663static void digital_in_send_complete(struct nfc_digital_dev *ddev, void *arg,
664				     struct sk_buff *resp)
665{
666	struct digital_data_exch *data_exch = arg;
667	int rc;
668
669	if (IS_ERR(resp)) {
670		rc = PTR_ERR(resp);
671		resp = NULL;
672		goto done;
673	}
674
675	if (ddev->curr_protocol == NFC_PROTO_MIFARE) {
676		rc = digital_in_recv_mifare_res(resp);
677		/* crc check is done in digital_in_recv_mifare_res() */
678		goto done;
679	}
680
681	if ((ddev->curr_protocol == NFC_PROTO_ISO14443) ||
682	    (ddev->curr_protocol == NFC_PROTO_ISO14443_B)) {
683		rc = digital_in_iso_dep_pull_sod(ddev, resp);
684		if (rc)
685			goto done;
686	}
687
688	rc = ddev->skb_check_crc(resp);
689
690done:
691	if (rc) {
692		kfree_skb(resp);
693		resp = NULL;
694	}
695
696	data_exch->cb(data_exch->cb_context, resp, rc);
697
698	kfree(data_exch);
699}
700
701static int digital_in_send(struct nfc_dev *nfc_dev, struct nfc_target *target,
702			   struct sk_buff *skb, data_exchange_cb_t cb,
703			   void *cb_context)
704{
705	struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
706	struct digital_data_exch *data_exch;
707	int rc;
708
709	data_exch = kzalloc(sizeof(*data_exch), GFP_KERNEL);
710	if (!data_exch)
 
711		return -ENOMEM;
 
712
713	data_exch->cb = cb;
714	data_exch->cb_context = cb_context;
715
716	if (ddev->curr_protocol == NFC_PROTO_NFC_DEP) {
717		rc = digital_in_send_dep_req(ddev, target, skb, data_exch);
718		goto exit;
719	}
720
721	if ((ddev->curr_protocol == NFC_PROTO_ISO14443) ||
722	    (ddev->curr_protocol == NFC_PROTO_ISO14443_B)) {
723		rc = digital_in_iso_dep_push_sod(ddev, skb);
724		if (rc)
725			goto exit;
726	}
727
728	ddev->skb_add_crc(skb);
729
730	rc = digital_in_send_cmd(ddev, skb, 500, digital_in_send_complete,
731				 data_exch);
732
733exit:
734	if (rc)
735		kfree(data_exch);
736
737	return rc;
738}
739
740static const struct nfc_ops digital_nfc_ops = {
741	.dev_up = digital_dev_up,
742	.dev_down = digital_dev_down,
743	.start_poll = digital_start_poll,
744	.stop_poll = digital_stop_poll,
745	.dep_link_up = digital_dep_link_up,
746	.dep_link_down = digital_dep_link_down,
747	.activate_target = digital_activate_target,
748	.deactivate_target = digital_deactivate_target,
749	.tm_send = digital_tg_send,
750	.im_transceive = digital_in_send,
751};
752
753struct nfc_digital_dev *nfc_digital_allocate_device(const struct nfc_digital_ops *ops,
754					    __u32 supported_protocols,
755					    __u32 driver_capabilities,
756					    int tx_headroom, int tx_tailroom)
757{
758	struct nfc_digital_dev *ddev;
759
760	if (!ops->in_configure_hw || !ops->in_send_cmd || !ops->tg_listen ||
761	    !ops->tg_configure_hw || !ops->tg_send_cmd || !ops->abort_cmd ||
762	    !ops->switch_rf || (ops->tg_listen_md && !ops->tg_get_rf_tech))
763		return NULL;
764
765	ddev = kzalloc(sizeof(*ddev), GFP_KERNEL);
766	if (!ddev)
767		return NULL;
768
769	ddev->driver_capabilities = driver_capabilities;
770	ddev->ops = ops;
771
772	mutex_init(&ddev->cmd_lock);
773	INIT_LIST_HEAD(&ddev->cmd_queue);
774
775	INIT_WORK(&ddev->cmd_work, digital_wq_cmd);
776	INIT_WORK(&ddev->cmd_complete_work, digital_wq_cmd_complete);
777
778	mutex_init(&ddev->poll_lock);
779	INIT_DELAYED_WORK(&ddev->poll_work, digital_wq_poll);
780
781	if (supported_protocols & NFC_PROTO_JEWEL_MASK)
782		ddev->protocols |= NFC_PROTO_JEWEL_MASK;
783	if (supported_protocols & NFC_PROTO_MIFARE_MASK)
784		ddev->protocols |= NFC_PROTO_MIFARE_MASK;
785	if (supported_protocols & NFC_PROTO_FELICA_MASK)
786		ddev->protocols |= NFC_PROTO_FELICA_MASK;
787	if (supported_protocols & NFC_PROTO_NFC_DEP_MASK)
788		ddev->protocols |= NFC_PROTO_NFC_DEP_MASK;
789	if (supported_protocols & NFC_PROTO_ISO15693_MASK)
790		ddev->protocols |= NFC_PROTO_ISO15693_MASK;
791	if (supported_protocols & NFC_PROTO_ISO14443_MASK)
792		ddev->protocols |= NFC_PROTO_ISO14443_MASK;
793	if (supported_protocols & NFC_PROTO_ISO14443_B_MASK)
794		ddev->protocols |= NFC_PROTO_ISO14443_B_MASK;
795
796	ddev->tx_headroom = tx_headroom + DIGITAL_MAX_HEADER_LEN;
797	ddev->tx_tailroom = tx_tailroom + DIGITAL_CRC_LEN;
798
799	ddev->nfc_dev = nfc_allocate_device(&digital_nfc_ops, ddev->protocols,
800					    ddev->tx_headroom,
801					    ddev->tx_tailroom);
802	if (!ddev->nfc_dev) {
803		pr_err("nfc_allocate_device failed\n");
804		goto free_dev;
805	}
806
807	nfc_set_drvdata(ddev->nfc_dev, ddev);
808
809	return ddev;
810
811free_dev:
812	kfree(ddev);
813
814	return NULL;
815}
816EXPORT_SYMBOL(nfc_digital_allocate_device);
817
818void nfc_digital_free_device(struct nfc_digital_dev *ddev)
819{
820	nfc_free_device(ddev->nfc_dev);
821	kfree(ddev);
822}
823EXPORT_SYMBOL(nfc_digital_free_device);
824
825int nfc_digital_register_device(struct nfc_digital_dev *ddev)
826{
827	return nfc_register_device(ddev->nfc_dev);
828}
829EXPORT_SYMBOL(nfc_digital_register_device);
830
831void nfc_digital_unregister_device(struct nfc_digital_dev *ddev)
832{
833	struct digital_cmd *cmd, *n;
834
835	nfc_unregister_device(ddev->nfc_dev);
836
837	mutex_lock(&ddev->poll_lock);
838	ddev->poll_tech_count = 0;
839	mutex_unlock(&ddev->poll_lock);
840
841	cancel_delayed_work_sync(&ddev->poll_work);
842	cancel_work_sync(&ddev->cmd_work);
843	cancel_work_sync(&ddev->cmd_complete_work);
844
845	list_for_each_entry_safe(cmd, n, &ddev->cmd_queue, queue) {
846		list_del(&cmd->queue);
847
848		/* Call the command callback if any and pass it a ENODEV error.
849		 * This gives a chance to the command issuer to free any
850		 * allocated buffer.
851		 */
852		if (cmd->cmd_cb)
853			cmd->cmd_cb(ddev, cmd->cb_context, ERR_PTR(-ENODEV));
854
855		kfree(cmd->mdaa_params);
856		kfree(cmd);
857	}
858}
859EXPORT_SYMBOL(nfc_digital_unregister_device);
860
861MODULE_DESCRIPTION("NFC Digital protocol stack");
862MODULE_LICENSE("GPL");