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 #define pr_fmt(fmt) "digital: %s: " fmt, __func__
18 #include <linux/module.h>
22 #define DIGITAL_PROTO_NFCA_RF_TECH \
23 (NFC_PROTO_JEWEL_MASK | NFC_PROTO_MIFARE_MASK | NFC_PROTO_NFC_DEP_MASK)
25 #define DIGITAL_PROTO_NFCF_RF_TECH \
26 (NFC_PROTO_FELICA_MASK | NFC_PROTO_NFC_DEP_MASK)
28 #define DIGITAL_PROTO_ISO15693_RF_TECH NFC_PROTO_ISO15693_MASK
31 struct list_head queue;
39 struct digital_tg_mdaa_params *mdaa_params;
41 nfc_digital_cmd_complete_t cmd_cb;
45 struct sk_buff *digital_skb_alloc(struct nfc_digital_dev *ddev,
50 skb = alloc_skb(len + ddev->tx_headroom + ddev->tx_tailroom,
53 skb_reserve(skb, ddev->tx_headroom);
58 void digital_skb_add_crc(struct sk_buff *skb, crc_func_t crc_func, u16 init,
59 u8 bitwise_inv, u8 msb_first)
63 crc = crc_func(init, skb->data, skb->len);
71 *skb_put(skb, 1) = crc & 0xFF;
72 *skb_put(skb, 1) = (crc >> 8) & 0xFF;
75 int digital_skb_check_crc(struct sk_buff *skb, crc_func_t crc_func,
76 u16 crc_init, u8 bitwise_inv, u8 msb_first)
84 crc = crc_func(crc_init, skb->data, skb->len - 2);
92 rc = (skb->data[skb->len - 2] - (crc & 0xFF)) +
93 (skb->data[skb->len - 1] - ((crc >> 8) & 0xFF));
98 skb_trim(skb, skb->len - 2);
103 static inline void digital_switch_rf(struct nfc_digital_dev *ddev, bool on)
105 ddev->ops->switch_rf(ddev, on);
108 static inline void digital_abort_cmd(struct nfc_digital_dev *ddev)
110 ddev->ops->abort_cmd(ddev);
113 static void digital_wq_cmd_complete(struct work_struct *work)
115 struct digital_cmd *cmd;
116 struct nfc_digital_dev *ddev = container_of(work,
117 struct nfc_digital_dev,
120 mutex_lock(&ddev->cmd_lock);
122 cmd = list_first_entry_or_null(&ddev->cmd_queue, struct digital_cmd,
125 mutex_unlock(&ddev->cmd_lock);
129 list_del(&cmd->queue);
131 mutex_unlock(&ddev->cmd_lock);
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);
137 cmd->cmd_cb(ddev, cmd->cb_context, cmd->resp);
139 kfree(cmd->mdaa_params);
142 schedule_work(&ddev->cmd_work);
145 static void digital_send_cmd_complete(struct nfc_digital_dev *ddev,
146 void *arg, struct sk_buff *resp)
148 struct digital_cmd *cmd = arg;
152 schedule_work(&ddev->cmd_complete_work);
155 static void digital_wq_cmd(struct work_struct *work)
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,
164 mutex_lock(&ddev->cmd_lock);
166 cmd = list_first_entry_or_null(&ddev->cmd_queue, struct digital_cmd,
168 if (!cmd || cmd->pending) {
169 mutex_unlock(&ddev->cmd_lock);
173 mutex_unlock(&ddev->cmd_lock);
176 print_hex_dump_debug("DIGITAL TX: ", DUMP_PREFIX_NONE, 16, 1,
177 cmd->req->data, cmd->req->len, false);
180 case DIGITAL_CMD_IN_SEND:
181 rc = ddev->ops->in_send_cmd(ddev, cmd->req, cmd->timeout,
182 digital_send_cmd_complete, cmd);
185 case DIGITAL_CMD_TG_SEND:
186 rc = ddev->ops->tg_send_cmd(ddev, cmd->req, cmd->timeout,
187 digital_send_cmd_complete, cmd);
190 case DIGITAL_CMD_TG_LISTEN:
191 rc = ddev->ops->tg_listen(ddev, cmd->timeout,
192 digital_send_cmd_complete, cmd);
195 case DIGITAL_CMD_TG_LISTEN_MDAA:
196 params = cmd->mdaa_params;
198 rc = ddev->ops->tg_listen_mdaa(ddev, params, cmd->timeout,
199 digital_send_cmd_complete, cmd);
203 pr_err("Unknown cmd type %d\n", cmd->type);
210 pr_err("in_send_command returned err %d\n", rc);
212 mutex_lock(&ddev->cmd_lock);
213 list_del(&cmd->queue);
214 mutex_unlock(&ddev->cmd_lock);
217 kfree(cmd->mdaa_params);
220 schedule_work(&ddev->cmd_work);
223 int 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,
228 struct digital_cmd *cmd;
230 cmd = kzalloc(sizeof(struct digital_cmd), GFP_KERNEL);
234 cmd->type = cmd_type;
235 cmd->timeout = timeout;
237 cmd->mdaa_params = params;
238 cmd->cmd_cb = cmd_cb;
239 cmd->cb_context = cb_context;
240 INIT_LIST_HEAD(&cmd->queue);
242 mutex_lock(&ddev->cmd_lock);
243 list_add_tail(&cmd->queue, &ddev->cmd_queue);
244 mutex_unlock(&ddev->cmd_lock);
246 schedule_work(&ddev->cmd_work);
251 int digital_in_configure_hw(struct nfc_digital_dev *ddev, int type, int param)
255 rc = ddev->ops->in_configure_hw(ddev, type, param);
257 pr_err("in_configure_hw failed: %d\n", rc);
262 int digital_tg_configure_hw(struct nfc_digital_dev *ddev, int type, int param)
266 rc = ddev->ops->tg_configure_hw(ddev, type, param);
268 pr_err("tg_configure_hw failed: %d\n", rc);
273 static int digital_tg_listen_mdaa(struct nfc_digital_dev *ddev, u8 rf_tech)
275 struct digital_tg_mdaa_params *params;
277 params = kzalloc(sizeof(struct digital_tg_mdaa_params), GFP_KERNEL);
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;
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;
290 return digital_send_cmd(ddev, DIGITAL_CMD_TG_LISTEN_MDAA, NULL, params,
291 500, digital_tg_recv_atr_req, NULL);
294 int digital_target_found(struct nfc_digital_dev *ddev,
295 struct nfc_target *target, u8 protocol)
300 int (*check_crc)(struct sk_buff *skb);
301 void (*add_crc)(struct sk_buff *skb);
303 rf_tech = ddev->poll_techs[ddev->poll_tech_index].rf_tech;
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;
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;
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;
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;
330 framing = NFC_DIGITAL_FRAMING_NFCF_NFC_DEP;
331 check_crc = digital_skb_check_crc_f;
332 add_crc = digital_skb_add_crc_f;
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;
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;
349 pr_err("Invalid protocol %d\n", protocol);
353 pr_debug("rf_tech=%d, protocol=%d\n", rf_tech, protocol);
355 ddev->curr_rf_tech = rf_tech;
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;
361 ddev->skb_add_crc = add_crc;
362 ddev->skb_check_crc = check_crc;
365 rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING, framing);
369 target->supported_protocols = (1 << protocol);
370 rc = nfc_targets_found(ddev->nfc_dev, target, 1);
374 ddev->poll_tech_count = 0;
379 void digital_poll_next_tech(struct nfc_digital_dev *ddev)
381 digital_switch_rf(ddev, 0);
383 mutex_lock(&ddev->poll_lock);
385 if (!ddev->poll_tech_count) {
386 mutex_unlock(&ddev->poll_lock);
390 ddev->poll_tech_index = (ddev->poll_tech_index + 1) %
391 ddev->poll_tech_count;
393 mutex_unlock(&ddev->poll_lock);
395 schedule_work(&ddev->poll_work);
398 static void digital_wq_poll(struct work_struct *work)
401 struct digital_poll_tech *poll_tech;
402 struct nfc_digital_dev *ddev = container_of(work,
403 struct nfc_digital_dev,
405 mutex_lock(&ddev->poll_lock);
407 if (!ddev->poll_tech_count) {
408 mutex_unlock(&ddev->poll_lock);
412 poll_tech = &ddev->poll_techs[ddev->poll_tech_index];
414 mutex_unlock(&ddev->poll_lock);
416 rc = poll_tech->poll_func(ddev, poll_tech->rf_tech);
418 digital_poll_next_tech(ddev);
421 static void digital_add_poll_tech(struct nfc_digital_dev *ddev, u8 rf_tech,
422 digital_poll_t poll_func)
424 struct digital_poll_tech *poll_tech;
426 if (ddev->poll_tech_count >= NFC_DIGITAL_POLL_MODE_COUNT_MAX)
429 poll_tech = &ddev->poll_techs[ddev->poll_tech_count++];
431 poll_tech->rf_tech = rf_tech;
432 poll_tech->poll_func = poll_func;
436 * start_poll operation
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).
444 static int digital_start_poll(struct nfc_dev *nfc_dev, __u32 im_protocols,
447 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
448 u32 matching_im_protocols, matching_tm_protocols;
450 pr_debug("protocols: im 0x%x, tm 0x%x, supported 0x%x\n", im_protocols,
451 tm_protocols, ddev->protocols);
453 matching_im_protocols = ddev->protocols & im_protocols;
454 matching_tm_protocols = ddev->protocols & tm_protocols;
456 if (!matching_im_protocols && !matching_tm_protocols) {
457 pr_err("Unknown protocol\n");
461 if (ddev->poll_tech_count) {
462 pr_err("Already polling\n");
466 if (ddev->curr_protocol) {
467 pr_err("A target is already active\n");
471 ddev->poll_tech_count = 0;
472 ddev->poll_tech_index = 0;
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);
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);
482 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_424F,
483 digital_in_send_sensf_req);
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);
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);
495 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_106A,
496 digital_tg_listen_nfca);
498 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_212F,
499 digital_tg_listen_nfcf);
501 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_424F,
502 digital_tg_listen_nfcf);
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);
512 schedule_work(&ddev->poll_work);
517 static void digital_stop_poll(struct nfc_dev *nfc_dev)
519 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
521 mutex_lock(&ddev->poll_lock);
523 if (!ddev->poll_tech_count) {
524 pr_err("Polling operation was not running\n");
525 mutex_unlock(&ddev->poll_lock);
529 ddev->poll_tech_count = 0;
531 mutex_unlock(&ddev->poll_lock);
533 cancel_work_sync(&ddev->poll_work);
535 digital_abort_cmd(ddev);
538 static int digital_dev_up(struct nfc_dev *nfc_dev)
540 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
542 digital_switch_rf(ddev, 1);
547 static int digital_dev_down(struct nfc_dev *nfc_dev)
549 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
551 digital_switch_rf(ddev, 0);
556 static 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)
560 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
563 rc = digital_in_send_atr_req(ddev, target, comm_mode, gb, gb_len);
566 ddev->curr_protocol = NFC_PROTO_NFC_DEP;
571 static int digital_dep_link_down(struct nfc_dev *nfc_dev)
573 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
575 ddev->curr_protocol = 0;
580 static int digital_activate_target(struct nfc_dev *nfc_dev,
581 struct nfc_target *target, __u32 protocol)
583 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
585 if (ddev->poll_tech_count) {
586 pr_err("Can't activate a target while polling\n");
590 if (ddev->curr_protocol) {
591 pr_err("A target is already active\n");
595 ddev->curr_protocol = protocol;
600 static void digital_deactivate_target(struct nfc_dev *nfc_dev,
601 struct nfc_target *target)
603 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
605 if (!ddev->curr_protocol) {
606 pr_err("No active target\n");
610 ddev->curr_protocol = 0;
613 static int digital_tg_send(struct nfc_dev *dev, struct sk_buff *skb)
615 struct nfc_digital_dev *ddev = nfc_get_drvdata(dev);
617 return digital_tg_send_dep_res(ddev, skb);
620 static void digital_in_send_complete(struct nfc_digital_dev *ddev, void *arg,
621 struct sk_buff *resp)
623 struct digital_data_exch *data_exch = arg;
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() */
638 if (ddev->curr_protocol == NFC_PROTO_ISO14443) {
639 rc = digital_in_iso_dep_pull_sod(ddev, resp);
644 rc = ddev->skb_check_crc(resp);
652 data_exch->cb(data_exch->cb_context, resp, rc);
657 static int digital_in_send(struct nfc_dev *nfc_dev, struct nfc_target *target,
658 struct sk_buff *skb, data_exchange_cb_t cb,
661 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
662 struct digital_data_exch *data_exch;
665 data_exch = kzalloc(sizeof(struct digital_data_exch), GFP_KERNEL);
667 pr_err("Failed to allocate data_exch struct\n");
672 data_exch->cb_context = cb_context;
674 if (ddev->curr_protocol == NFC_PROTO_NFC_DEP) {
675 rc = digital_in_send_dep_req(ddev, target, skb, data_exch);
679 if (ddev->curr_protocol == NFC_PROTO_ISO14443) {
680 rc = digital_in_iso_dep_push_sod(ddev, skb);
685 ddev->skb_add_crc(skb);
687 rc = digital_in_send_cmd(ddev, skb, 500, digital_in_send_complete,
697 static 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,
710 struct 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)
715 struct nfc_digital_dev *ddev;
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 ||
722 ddev = kzalloc(sizeof(struct nfc_digital_dev), GFP_KERNEL);
726 ddev->driver_capabilities = driver_capabilities;
729 mutex_init(&ddev->cmd_lock);
730 INIT_LIST_HEAD(&ddev->cmd_queue);
732 INIT_WORK(&ddev->cmd_work, digital_wq_cmd);
733 INIT_WORK(&ddev->cmd_complete_work, digital_wq_cmd_complete);
735 mutex_init(&ddev->poll_lock);
736 INIT_WORK(&ddev->poll_work, digital_wq_poll);
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;
751 ddev->tx_headroom = tx_headroom + DIGITAL_MAX_HEADER_LEN;
752 ddev->tx_tailroom = tx_tailroom + DIGITAL_CRC_LEN;
754 ddev->nfc_dev = nfc_allocate_device(&digital_nfc_ops, ddev->protocols,
757 if (!ddev->nfc_dev) {
758 pr_err("nfc_allocate_device failed\n");
762 nfc_set_drvdata(ddev->nfc_dev, ddev);
771 EXPORT_SYMBOL(nfc_digital_allocate_device);
773 void nfc_digital_free_device(struct nfc_digital_dev *ddev)
775 nfc_free_device(ddev->nfc_dev);
778 EXPORT_SYMBOL(nfc_digital_free_device);
780 int nfc_digital_register_device(struct nfc_digital_dev *ddev)
782 return nfc_register_device(ddev->nfc_dev);
784 EXPORT_SYMBOL(nfc_digital_register_device);
786 void nfc_digital_unregister_device(struct nfc_digital_dev *ddev)
788 struct digital_cmd *cmd, *n;
790 nfc_unregister_device(ddev->nfc_dev);
792 mutex_lock(&ddev->poll_lock);
793 ddev->poll_tech_count = 0;
794 mutex_unlock(&ddev->poll_lock);
796 cancel_work_sync(&ddev->poll_work);
797 cancel_work_sync(&ddev->cmd_work);
798 cancel_work_sync(&ddev->cmd_complete_work);
800 list_for_each_entry_safe(cmd, n, &ddev->cmd_queue, queue) {
801 list_del(&cmd->queue);
802 kfree(cmd->mdaa_params);
806 EXPORT_SYMBOL(nfc_digital_unregister_device);
808 MODULE_LICENSE("GPL");