2 * NFC Digital Protocol stack
3 * Copyright (c) 2013, Intel Corporation.
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.
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
16 #include <linux/module.h>
20 #define DIGITAL_PROTO_NFCA_RF_TECH \
21 (NFC_PROTO_JEWEL_MASK | NFC_PROTO_MIFARE_MASK | NFC_PROTO_NFC_DEP_MASK)
23 #define DIGITAL_PROTO_NFCF_RF_TECH \
24 (NFC_PROTO_FELICA_MASK | NFC_PROTO_NFC_DEP_MASK)
27 struct list_head queue
;
35 struct digital_tg_mdaa_params
*mdaa_params
;
37 nfc_digital_cmd_complete_t cmd_cb
;
41 struct sk_buff
*digital_skb_alloc(struct nfc_digital_dev
*ddev
,
46 skb
= alloc_skb(len
+ ddev
->tx_headroom
+ ddev
->tx_tailroom
,
49 skb_reserve(skb
, ddev
->tx_headroom
);
54 void digital_skb_add_crc(struct sk_buff
*skb
, crc_func_t crc_func
, u16 init
,
55 u8 bitwise_inv
, u8 msb_first
)
59 crc
= crc_func(init
, skb
->data
, skb
->len
);
67 *skb_put(skb
, 1) = crc
& 0xFF;
68 *skb_put(skb
, 1) = (crc
>> 8) & 0xFF;
71 int digital_skb_check_crc(struct sk_buff
*skb
, crc_func_t crc_func
,
72 u16 crc_init
, u8 bitwise_inv
, u8 msb_first
)
80 crc
= crc_func(crc_init
, skb
->data
, skb
->len
- 2);
88 rc
= (skb
->data
[skb
->len
- 2] - (crc
& 0xFF)) +
89 (skb
->data
[skb
->len
- 1] - ((crc
>> 8) & 0xFF));
94 skb_trim(skb
, skb
->len
- 2);
99 static inline void digital_switch_rf(struct nfc_digital_dev
*ddev
, bool on
)
101 ddev
->ops
->switch_rf(ddev
, on
);
104 static inline void digital_abort_cmd(struct nfc_digital_dev
*ddev
)
106 ddev
->ops
->abort_cmd(ddev
);
109 static void digital_wq_cmd_complete(struct work_struct
*work
)
111 struct digital_cmd
*cmd
;
112 struct nfc_digital_dev
*ddev
= container_of(work
,
113 struct nfc_digital_dev
,
116 mutex_lock(&ddev
->cmd_lock
);
118 cmd
= list_first_entry_or_null(&ddev
->cmd_queue
, struct digital_cmd
,
121 mutex_unlock(&ddev
->cmd_lock
);
125 list_del(&cmd
->queue
);
127 mutex_unlock(&ddev
->cmd_lock
);
129 if (!IS_ERR(cmd
->resp
))
130 print_hex_dump_debug("DIGITAL RX: ", DUMP_PREFIX_NONE
, 16, 1,
131 cmd
->resp
->data
, cmd
->resp
->len
, false);
133 cmd
->cmd_cb(ddev
, cmd
->cb_context
, cmd
->resp
);
135 kfree(cmd
->mdaa_params
);
138 schedule_work(&ddev
->cmd_work
);
141 static void digital_send_cmd_complete(struct nfc_digital_dev
*ddev
,
142 void *arg
, struct sk_buff
*resp
)
144 struct digital_cmd
*cmd
= arg
;
148 schedule_work(&ddev
->cmd_complete_work
);
151 static void digital_wq_cmd(struct work_struct
*work
)
154 struct digital_cmd
*cmd
;
155 struct digital_tg_mdaa_params
*params
;
156 struct nfc_digital_dev
*ddev
= container_of(work
,
157 struct nfc_digital_dev
,
160 mutex_lock(&ddev
->cmd_lock
);
162 cmd
= list_first_entry_or_null(&ddev
->cmd_queue
, struct digital_cmd
,
164 if (!cmd
|| cmd
->pending
) {
165 mutex_unlock(&ddev
->cmd_lock
);
169 mutex_unlock(&ddev
->cmd_lock
);
172 print_hex_dump_debug("DIGITAL TX: ", DUMP_PREFIX_NONE
, 16, 1,
173 cmd
->req
->data
, cmd
->req
->len
, false);
176 case DIGITAL_CMD_IN_SEND
:
177 rc
= ddev
->ops
->in_send_cmd(ddev
, cmd
->req
, cmd
->timeout
,
178 digital_send_cmd_complete
, cmd
);
181 case DIGITAL_CMD_TG_SEND
:
182 rc
= ddev
->ops
->tg_send_cmd(ddev
, cmd
->req
, cmd
->timeout
,
183 digital_send_cmd_complete
, cmd
);
186 case DIGITAL_CMD_TG_LISTEN
:
187 rc
= ddev
->ops
->tg_listen(ddev
, cmd
->timeout
,
188 digital_send_cmd_complete
, cmd
);
191 case DIGITAL_CMD_TG_LISTEN_MDAA
:
192 params
= cmd
->mdaa_params
;
194 rc
= ddev
->ops
->tg_listen_mdaa(ddev
, params
, cmd
->timeout
,
195 digital_send_cmd_complete
, cmd
);
199 PR_ERR("Unknown cmd type %d", cmd
->type
);
206 PR_ERR("in_send_command returned err %d", rc
);
208 mutex_lock(&ddev
->cmd_lock
);
209 list_del(&cmd
->queue
);
210 mutex_unlock(&ddev
->cmd_lock
);
213 kfree(cmd
->mdaa_params
);
216 schedule_work(&ddev
->cmd_work
);
219 int digital_send_cmd(struct nfc_digital_dev
*ddev
, u8 cmd_type
,
220 struct sk_buff
*skb
, struct digital_tg_mdaa_params
*params
,
221 u16 timeout
, nfc_digital_cmd_complete_t cmd_cb
,
224 struct digital_cmd
*cmd
;
226 cmd
= kzalloc(sizeof(struct digital_cmd
), GFP_KERNEL
);
230 cmd
->type
= cmd_type
;
231 cmd
->timeout
= timeout
;
233 cmd
->mdaa_params
= params
;
234 cmd
->cmd_cb
= cmd_cb
;
235 cmd
->cb_context
= cb_context
;
236 INIT_LIST_HEAD(&cmd
->queue
);
238 mutex_lock(&ddev
->cmd_lock
);
239 list_add_tail(&cmd
->queue
, &ddev
->cmd_queue
);
240 mutex_unlock(&ddev
->cmd_lock
);
242 schedule_work(&ddev
->cmd_work
);
247 int digital_in_configure_hw(struct nfc_digital_dev
*ddev
, int type
, int param
)
251 rc
= ddev
->ops
->in_configure_hw(ddev
, type
, param
);
253 PR_ERR("in_configure_hw failed: %d", rc
);
258 int digital_tg_configure_hw(struct nfc_digital_dev
*ddev
, int type
, int param
)
262 rc
= ddev
->ops
->tg_configure_hw(ddev
, type
, param
);
264 PR_ERR("tg_configure_hw failed: %d", rc
);
269 static int digital_tg_listen_mdaa(struct nfc_digital_dev
*ddev
, u8 rf_tech
)
271 struct digital_tg_mdaa_params
*params
;
273 params
= kzalloc(sizeof(struct digital_tg_mdaa_params
), GFP_KERNEL
);
277 params
->sens_res
= DIGITAL_SENS_RES_NFC_DEP
;
278 get_random_bytes(params
->nfcid1
, sizeof(params
->nfcid1
));
279 params
->sel_res
= DIGITAL_SEL_RES_NFC_DEP
;
281 params
->nfcid2
[0] = DIGITAL_SENSF_NFCID2_NFC_DEP_B1
;
282 params
->nfcid2
[1] = DIGITAL_SENSF_NFCID2_NFC_DEP_B2
;
283 get_random_bytes(params
->nfcid2
+ 2, NFC_NFCID2_MAXSIZE
- 2);
284 params
->sc
= DIGITAL_SENSF_FELICA_SC
;
286 return digital_send_cmd(ddev
, DIGITAL_CMD_TG_LISTEN_MDAA
, NULL
, params
,
287 500, digital_tg_recv_atr_req
, NULL
);
290 int digital_target_found(struct nfc_digital_dev
*ddev
,
291 struct nfc_target
*target
, u8 protocol
)
296 int (*check_crc
)(struct sk_buff
*skb
);
297 void (*add_crc
)(struct sk_buff
*skb
);
299 rf_tech
= ddev
->poll_techs
[ddev
->poll_tech_index
].rf_tech
;
302 case NFC_PROTO_JEWEL
:
303 framing
= NFC_DIGITAL_FRAMING_NFCA_T1T
;
304 check_crc
= digital_skb_check_crc_b
;
305 add_crc
= digital_skb_add_crc_b
;
308 case NFC_PROTO_MIFARE
:
309 framing
= NFC_DIGITAL_FRAMING_NFCA_T2T
;
310 check_crc
= digital_skb_check_crc_a
;
311 add_crc
= digital_skb_add_crc_a
;
314 case NFC_PROTO_FELICA
:
315 framing
= NFC_DIGITAL_FRAMING_NFCF_T3T
;
316 check_crc
= digital_skb_check_crc_f
;
317 add_crc
= digital_skb_add_crc_f
;
320 case NFC_PROTO_NFC_DEP
:
321 if (rf_tech
== NFC_DIGITAL_RF_TECH_106A
) {
322 framing
= NFC_DIGITAL_FRAMING_NFCA_NFC_DEP
;
323 check_crc
= digital_skb_check_crc_a
;
324 add_crc
= digital_skb_add_crc_a
;
326 framing
= NFC_DIGITAL_FRAMING_NFCF_NFC_DEP
;
327 check_crc
= digital_skb_check_crc_f
;
328 add_crc
= digital_skb_add_crc_f
;
333 PR_ERR("Invalid protocol %d", protocol
);
337 PR_DBG("rf_tech=%d, protocol=%d", rf_tech
, protocol
);
339 ddev
->curr_rf_tech
= rf_tech
;
340 ddev
->curr_protocol
= protocol
;
342 if (DIGITAL_DRV_CAPS_IN_CRC(ddev
)) {
343 ddev
->skb_add_crc
= digital_skb_add_crc_none
;
344 ddev
->skb_check_crc
= digital_skb_check_crc_none
;
346 ddev
->skb_add_crc
= add_crc
;
347 ddev
->skb_check_crc
= check_crc
;
350 rc
= digital_in_configure_hw(ddev
, NFC_DIGITAL_CONFIG_FRAMING
, framing
);
354 target
->supported_protocols
= (1 << protocol
);
355 rc
= nfc_targets_found(ddev
->nfc_dev
, target
, 1);
359 ddev
->poll_tech_count
= 0;
364 void digital_poll_next_tech(struct nfc_digital_dev
*ddev
)
366 digital_switch_rf(ddev
, 0);
368 mutex_lock(&ddev
->poll_lock
);
370 if (!ddev
->poll_tech_count
) {
371 mutex_unlock(&ddev
->poll_lock
);
375 ddev
->poll_tech_index
= (ddev
->poll_tech_index
+ 1) %
376 ddev
->poll_tech_count
;
378 mutex_unlock(&ddev
->poll_lock
);
380 schedule_work(&ddev
->poll_work
);
383 static void digital_wq_poll(struct work_struct
*work
)
386 struct digital_poll_tech
*poll_tech
;
387 struct nfc_digital_dev
*ddev
= container_of(work
,
388 struct nfc_digital_dev
,
390 mutex_lock(&ddev
->poll_lock
);
392 if (!ddev
->poll_tech_count
) {
393 mutex_unlock(&ddev
->poll_lock
);
397 poll_tech
= &ddev
->poll_techs
[ddev
->poll_tech_index
];
399 mutex_unlock(&ddev
->poll_lock
);
401 rc
= poll_tech
->poll_func(ddev
, poll_tech
->rf_tech
);
403 digital_poll_next_tech(ddev
);
406 static void digital_add_poll_tech(struct nfc_digital_dev
*ddev
, u8 rf_tech
,
407 digital_poll_t poll_func
)
409 struct digital_poll_tech
*poll_tech
;
411 if (ddev
->poll_tech_count
>= NFC_DIGITAL_POLL_MODE_COUNT_MAX
)
414 poll_tech
= &ddev
->poll_techs
[ddev
->poll_tech_count
++];
416 poll_tech
->rf_tech
= rf_tech
;
417 poll_tech
->poll_func
= poll_func
;
421 * start_poll operation
423 * For every supported protocol, the corresponding polling function is added
424 * to the table of polling technologies (ddev->poll_techs[]) using
425 * digital_add_poll_tech().
426 * When a polling function fails (by timeout or protocol error) the next one is
427 * schedule by digital_poll_next_tech() on the poll workqueue (ddev->poll_work).
429 static int digital_start_poll(struct nfc_dev
*nfc_dev
, __u32 im_protocols
,
432 struct nfc_digital_dev
*ddev
= nfc_get_drvdata(nfc_dev
);
433 u32 matching_im_protocols
, matching_tm_protocols
;
435 PR_DBG("protocols: im 0x%x, tm 0x%x, supported 0x%x", im_protocols
,
436 tm_protocols
, ddev
->protocols
);
438 matching_im_protocols
= ddev
->protocols
& im_protocols
;
439 matching_tm_protocols
= ddev
->protocols
& tm_protocols
;
441 if (!matching_im_protocols
&& !matching_tm_protocols
) {
442 PR_ERR("No known protocol");
446 if (ddev
->poll_tech_count
) {
447 PR_ERR("Already polling");
451 if (ddev
->curr_protocol
) {
452 PR_ERR("A target is already active");
456 ddev
->poll_tech_count
= 0;
457 ddev
->poll_tech_index
= 0;
459 if (matching_im_protocols
& DIGITAL_PROTO_NFCA_RF_TECH
)
460 digital_add_poll_tech(ddev
, NFC_DIGITAL_RF_TECH_106A
,
461 digital_in_send_sens_req
);
463 if (im_protocols
& DIGITAL_PROTO_NFCF_RF_TECH
) {
464 digital_add_poll_tech(ddev
, NFC_DIGITAL_RF_TECH_212F
,
465 digital_in_send_sensf_req
);
467 digital_add_poll_tech(ddev
, NFC_DIGITAL_RF_TECH_424F
,
468 digital_in_send_sensf_req
);
471 if (tm_protocols
& NFC_PROTO_NFC_DEP_MASK
) {
472 if (ddev
->ops
->tg_listen_mdaa
) {
473 digital_add_poll_tech(ddev
, 0,
474 digital_tg_listen_mdaa
);
476 digital_add_poll_tech(ddev
, NFC_DIGITAL_RF_TECH_106A
,
477 digital_tg_listen_nfca
);
479 digital_add_poll_tech(ddev
, NFC_DIGITAL_RF_TECH_212F
,
480 digital_tg_listen_nfcf
);
482 digital_add_poll_tech(ddev
, NFC_DIGITAL_RF_TECH_424F
,
483 digital_tg_listen_nfcf
);
487 if (!ddev
->poll_tech_count
) {
488 PR_ERR("Unsupported protocols: im=0x%x, tm=0x%x",
489 matching_im_protocols
, matching_tm_protocols
);
493 schedule_work(&ddev
->poll_work
);
498 static void digital_stop_poll(struct nfc_dev
*nfc_dev
)
500 struct nfc_digital_dev
*ddev
= nfc_get_drvdata(nfc_dev
);
502 mutex_lock(&ddev
->poll_lock
);
504 if (!ddev
->poll_tech_count
) {
505 PR_ERR("Polling operation was not running");
506 mutex_unlock(&ddev
->poll_lock
);
510 ddev
->poll_tech_count
= 0;
512 mutex_unlock(&ddev
->poll_lock
);
514 cancel_work_sync(&ddev
->poll_work
);
516 digital_abort_cmd(ddev
);
519 static int digital_dev_up(struct nfc_dev
*nfc_dev
)
521 struct nfc_digital_dev
*ddev
= nfc_get_drvdata(nfc_dev
);
523 digital_switch_rf(ddev
, 1);
528 static int digital_dev_down(struct nfc_dev
*nfc_dev
)
530 struct nfc_digital_dev
*ddev
= nfc_get_drvdata(nfc_dev
);
532 digital_switch_rf(ddev
, 0);
537 static int digital_dep_link_up(struct nfc_dev
*nfc_dev
,
538 struct nfc_target
*target
,
539 __u8 comm_mode
, __u8
*gb
, size_t gb_len
)
541 struct nfc_digital_dev
*ddev
= nfc_get_drvdata(nfc_dev
);
543 return digital_in_send_atr_req(ddev
, target
, comm_mode
, gb
, gb_len
);
546 static int digital_dep_link_down(struct nfc_dev
*nfc_dev
)
548 struct nfc_digital_dev
*ddev
= nfc_get_drvdata(nfc_dev
);
550 ddev
->curr_protocol
= 0;
555 static int digital_activate_target(struct nfc_dev
*nfc_dev
,
556 struct nfc_target
*target
, __u32 protocol
)
561 static void digital_deactivate_target(struct nfc_dev
*nfc_dev
,
562 struct nfc_target
*target
)
564 struct nfc_digital_dev
*ddev
= nfc_get_drvdata(nfc_dev
);
566 ddev
->curr_protocol
= 0;
569 static int digital_tg_send(struct nfc_dev
*dev
, struct sk_buff
*skb
)
571 struct nfc_digital_dev
*ddev
= nfc_get_drvdata(dev
);
573 return digital_tg_send_dep_res(ddev
, skb
);
576 static void digital_in_send_complete(struct nfc_digital_dev
*ddev
, void *arg
,
577 struct sk_buff
*resp
)
579 struct digital_data_exch
*data_exch
= arg
;
587 if (ddev
->curr_protocol
== NFC_PROTO_MIFARE
)
588 rc
= digital_in_recv_mifare_res(resp
);
590 rc
= ddev
->skb_check_crc(resp
);
598 data_exch
->cb(data_exch
->cb_context
, resp
, rc
);
603 static int digital_in_send(struct nfc_dev
*nfc_dev
, struct nfc_target
*target
,
604 struct sk_buff
*skb
, data_exchange_cb_t cb
,
607 struct nfc_digital_dev
*ddev
= nfc_get_drvdata(nfc_dev
);
608 struct digital_data_exch
*data_exch
;
610 data_exch
= kzalloc(sizeof(struct digital_data_exch
), GFP_KERNEL
);
612 PR_ERR("Failed to allocate data_exch struct");
617 data_exch
->cb_context
= cb_context
;
619 if (ddev
->curr_protocol
== NFC_PROTO_NFC_DEP
)
620 return digital_in_send_dep_req(ddev
, target
, skb
, data_exch
);
622 ddev
->skb_add_crc(skb
);
624 return digital_in_send_cmd(ddev
, skb
, 500, digital_in_send_complete
,
628 static struct nfc_ops digital_nfc_ops
= {
629 .dev_up
= digital_dev_up
,
630 .dev_down
= digital_dev_down
,
631 .start_poll
= digital_start_poll
,
632 .stop_poll
= digital_stop_poll
,
633 .dep_link_up
= digital_dep_link_up
,
634 .dep_link_down
= digital_dep_link_down
,
635 .activate_target
= digital_activate_target
,
636 .deactivate_target
= digital_deactivate_target
,
637 .tm_send
= digital_tg_send
,
638 .im_transceive
= digital_in_send
,
641 struct nfc_digital_dev
*nfc_digital_allocate_device(struct nfc_digital_ops
*ops
,
642 __u32 supported_protocols
,
643 __u32 driver_capabilities
,
644 int tx_headroom
, int tx_tailroom
)
646 struct nfc_digital_dev
*ddev
;
648 if (!ops
->in_configure_hw
|| !ops
->in_send_cmd
|| !ops
->tg_listen
||
649 !ops
->tg_configure_hw
|| !ops
->tg_send_cmd
|| !ops
->abort_cmd
||
653 ddev
= kzalloc(sizeof(struct nfc_digital_dev
), GFP_KERNEL
);
655 PR_ERR("kzalloc failed");
659 ddev
->driver_capabilities
= driver_capabilities
;
662 mutex_init(&ddev
->cmd_lock
);
663 INIT_LIST_HEAD(&ddev
->cmd_queue
);
665 INIT_WORK(&ddev
->cmd_work
, digital_wq_cmd
);
666 INIT_WORK(&ddev
->cmd_complete_work
, digital_wq_cmd_complete
);
668 mutex_init(&ddev
->poll_lock
);
669 INIT_WORK(&ddev
->poll_work
, digital_wq_poll
);
671 if (supported_protocols
& NFC_PROTO_JEWEL_MASK
)
672 ddev
->protocols
|= NFC_PROTO_JEWEL_MASK
;
673 if (supported_protocols
& NFC_PROTO_MIFARE_MASK
)
674 ddev
->protocols
|= NFC_PROTO_MIFARE_MASK
;
675 if (supported_protocols
& NFC_PROTO_FELICA_MASK
)
676 ddev
->protocols
|= NFC_PROTO_FELICA_MASK
;
677 if (supported_protocols
& NFC_PROTO_NFC_DEP_MASK
)
678 ddev
->protocols
|= NFC_PROTO_NFC_DEP_MASK
;
680 ddev
->tx_headroom
= tx_headroom
+ DIGITAL_MAX_HEADER_LEN
;
681 ddev
->tx_tailroom
= tx_tailroom
+ DIGITAL_CRC_LEN
;
683 ddev
->nfc_dev
= nfc_allocate_device(&digital_nfc_ops
, ddev
->protocols
,
686 if (!ddev
->nfc_dev
) {
687 PR_ERR("nfc_allocate_device failed");
691 nfc_set_drvdata(ddev
->nfc_dev
, ddev
);
700 EXPORT_SYMBOL(nfc_digital_allocate_device
);
702 void nfc_digital_free_device(struct nfc_digital_dev
*ddev
)
704 nfc_free_device(ddev
->nfc_dev
);
707 EXPORT_SYMBOL(nfc_digital_free_device
);
709 int nfc_digital_register_device(struct nfc_digital_dev
*ddev
)
711 return nfc_register_device(ddev
->nfc_dev
);
713 EXPORT_SYMBOL(nfc_digital_register_device
);
715 void nfc_digital_unregister_device(struct nfc_digital_dev
*ddev
)
717 struct digital_cmd
*cmd
, *n
;
719 nfc_unregister_device(ddev
->nfc_dev
);
721 mutex_lock(&ddev
->poll_lock
);
722 ddev
->poll_tech_count
= 0;
723 mutex_unlock(&ddev
->poll_lock
);
725 cancel_work_sync(&ddev
->poll_work
);
726 cancel_work_sync(&ddev
->cmd_work
);
727 cancel_work_sync(&ddev
->cmd_complete_work
);
729 list_for_each_entry_safe(cmd
, n
, &ddev
->cmd_queue
, queue
) {
730 list_del(&cmd
->queue
);
731 kfree(cmd
->mdaa_params
);
735 EXPORT_SYMBOL(nfc_digital_unregister_device
);
737 MODULE_LICENSE("GPL");