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)
29 struct list_head queue;
37 struct digital_tg_mdaa_params *mdaa_params;
39 nfc_digital_cmd_complete_t cmd_cb;
43 struct sk_buff *digital_skb_alloc(struct nfc_digital_dev *ddev,
48 skb = alloc_skb(len + ddev->tx_headroom + ddev->tx_tailroom,
51 skb_reserve(skb, ddev->tx_headroom);
56 void digital_skb_add_crc(struct sk_buff *skb, crc_func_t crc_func, u16 init,
57 u8 bitwise_inv, u8 msb_first)
61 crc = crc_func(init, skb->data, skb->len);
69 *skb_put(skb, 1) = crc & 0xFF;
70 *skb_put(skb, 1) = (crc >> 8) & 0xFF;
73 int digital_skb_check_crc(struct sk_buff *skb, crc_func_t crc_func,
74 u16 crc_init, u8 bitwise_inv, u8 msb_first)
82 crc = crc_func(crc_init, skb->data, skb->len - 2);
90 rc = (skb->data[skb->len - 2] - (crc & 0xFF)) +
91 (skb->data[skb->len - 1] - ((crc >> 8) & 0xFF));
96 skb_trim(skb, skb->len - 2);
101 static inline void digital_switch_rf(struct nfc_digital_dev *ddev, bool on)
103 ddev->ops->switch_rf(ddev, on);
106 static inline void digital_abort_cmd(struct nfc_digital_dev *ddev)
108 ddev->ops->abort_cmd(ddev);
111 static void digital_wq_cmd_complete(struct work_struct *work)
113 struct digital_cmd *cmd;
114 struct nfc_digital_dev *ddev = container_of(work,
115 struct nfc_digital_dev,
118 mutex_lock(&ddev->cmd_lock);
120 cmd = list_first_entry_or_null(&ddev->cmd_queue, struct digital_cmd,
123 mutex_unlock(&ddev->cmd_lock);
127 list_del(&cmd->queue);
129 mutex_unlock(&ddev->cmd_lock);
131 if (!IS_ERR(cmd->resp))
132 print_hex_dump_debug("DIGITAL RX: ", DUMP_PREFIX_NONE, 16, 1,
133 cmd->resp->data, cmd->resp->len, false);
135 cmd->cmd_cb(ddev, cmd->cb_context, cmd->resp);
137 kfree(cmd->mdaa_params);
140 schedule_work(&ddev->cmd_work);
143 static void digital_send_cmd_complete(struct nfc_digital_dev *ddev,
144 void *arg, struct sk_buff *resp)
146 struct digital_cmd *cmd = arg;
150 schedule_work(&ddev->cmd_complete_work);
153 static void digital_wq_cmd(struct work_struct *work)
156 struct digital_cmd *cmd;
157 struct digital_tg_mdaa_params *params;
158 struct nfc_digital_dev *ddev = container_of(work,
159 struct nfc_digital_dev,
162 mutex_lock(&ddev->cmd_lock);
164 cmd = list_first_entry_or_null(&ddev->cmd_queue, struct digital_cmd,
166 if (!cmd || cmd->pending) {
167 mutex_unlock(&ddev->cmd_lock);
171 mutex_unlock(&ddev->cmd_lock);
174 print_hex_dump_debug("DIGITAL TX: ", DUMP_PREFIX_NONE, 16, 1,
175 cmd->req->data, cmd->req->len, false);
178 case DIGITAL_CMD_IN_SEND:
179 rc = ddev->ops->in_send_cmd(ddev, cmd->req, cmd->timeout,
180 digital_send_cmd_complete, cmd);
183 case DIGITAL_CMD_TG_SEND:
184 rc = ddev->ops->tg_send_cmd(ddev, cmd->req, cmd->timeout,
185 digital_send_cmd_complete, cmd);
188 case DIGITAL_CMD_TG_LISTEN:
189 rc = ddev->ops->tg_listen(ddev, cmd->timeout,
190 digital_send_cmd_complete, cmd);
193 case DIGITAL_CMD_TG_LISTEN_MDAA:
194 params = cmd->mdaa_params;
196 rc = ddev->ops->tg_listen_mdaa(ddev, params, cmd->timeout,
197 digital_send_cmd_complete, cmd);
201 pr_err("Unknown cmd type %d\n", cmd->type);
208 pr_err("in_send_command returned err %d\n", rc);
210 mutex_lock(&ddev->cmd_lock);
211 list_del(&cmd->queue);
212 mutex_unlock(&ddev->cmd_lock);
215 kfree(cmd->mdaa_params);
218 schedule_work(&ddev->cmd_work);
221 int digital_send_cmd(struct nfc_digital_dev *ddev, u8 cmd_type,
222 struct sk_buff *skb, struct digital_tg_mdaa_params *params,
223 u16 timeout, nfc_digital_cmd_complete_t cmd_cb,
226 struct digital_cmd *cmd;
228 cmd = kzalloc(sizeof(struct digital_cmd), GFP_KERNEL);
232 cmd->type = cmd_type;
233 cmd->timeout = timeout;
235 cmd->mdaa_params = params;
236 cmd->cmd_cb = cmd_cb;
237 cmd->cb_context = cb_context;
238 INIT_LIST_HEAD(&cmd->queue);
240 mutex_lock(&ddev->cmd_lock);
241 list_add_tail(&cmd->queue, &ddev->cmd_queue);
242 mutex_unlock(&ddev->cmd_lock);
244 schedule_work(&ddev->cmd_work);
249 int digital_in_configure_hw(struct nfc_digital_dev *ddev, int type, int param)
253 rc = ddev->ops->in_configure_hw(ddev, type, param);
255 pr_err("in_configure_hw failed: %d\n", rc);
260 int digital_tg_configure_hw(struct nfc_digital_dev *ddev, int type, int param)
264 rc = ddev->ops->tg_configure_hw(ddev, type, param);
266 pr_err("tg_configure_hw failed: %d\n", rc);
271 static int digital_tg_listen_mdaa(struct nfc_digital_dev *ddev, u8 rf_tech)
273 struct digital_tg_mdaa_params *params;
275 params = kzalloc(sizeof(struct digital_tg_mdaa_params), GFP_KERNEL);
279 params->sens_res = DIGITAL_SENS_RES_NFC_DEP;
280 get_random_bytes(params->nfcid1, sizeof(params->nfcid1));
281 params->sel_res = DIGITAL_SEL_RES_NFC_DEP;
283 params->nfcid2[0] = DIGITAL_SENSF_NFCID2_NFC_DEP_B1;
284 params->nfcid2[1] = DIGITAL_SENSF_NFCID2_NFC_DEP_B2;
285 get_random_bytes(params->nfcid2 + 2, NFC_NFCID2_MAXSIZE - 2);
286 params->sc = DIGITAL_SENSF_FELICA_SC;
288 return digital_send_cmd(ddev, DIGITAL_CMD_TG_LISTEN_MDAA, NULL, params,
289 500, digital_tg_recv_atr_req, NULL);
292 int digital_target_found(struct nfc_digital_dev *ddev,
293 struct nfc_target *target, u8 protocol)
298 int (*check_crc)(struct sk_buff *skb);
299 void (*add_crc)(struct sk_buff *skb);
301 rf_tech = ddev->poll_techs[ddev->poll_tech_index].rf_tech;
304 case NFC_PROTO_JEWEL:
305 framing = NFC_DIGITAL_FRAMING_NFCA_T1T;
306 check_crc = digital_skb_check_crc_b;
307 add_crc = digital_skb_add_crc_b;
310 case NFC_PROTO_MIFARE:
311 framing = NFC_DIGITAL_FRAMING_NFCA_T2T;
312 check_crc = digital_skb_check_crc_a;
313 add_crc = digital_skb_add_crc_a;
316 case NFC_PROTO_FELICA:
317 framing = NFC_DIGITAL_FRAMING_NFCF_T3T;
318 check_crc = digital_skb_check_crc_f;
319 add_crc = digital_skb_add_crc_f;
322 case NFC_PROTO_NFC_DEP:
323 if (rf_tech == NFC_DIGITAL_RF_TECH_106A) {
324 framing = NFC_DIGITAL_FRAMING_NFCA_NFC_DEP;
325 check_crc = digital_skb_check_crc_a;
326 add_crc = digital_skb_add_crc_a;
328 framing = NFC_DIGITAL_FRAMING_NFCF_NFC_DEP;
329 check_crc = digital_skb_check_crc_f;
330 add_crc = digital_skb_add_crc_f;
335 pr_err("Invalid protocol %d\n", protocol);
339 pr_debug("rf_tech=%d, protocol=%d\n", rf_tech, protocol);
341 ddev->curr_rf_tech = rf_tech;
343 if (DIGITAL_DRV_CAPS_IN_CRC(ddev)) {
344 ddev->skb_add_crc = digital_skb_add_crc_none;
345 ddev->skb_check_crc = digital_skb_check_crc_none;
347 ddev->skb_add_crc = add_crc;
348 ddev->skb_check_crc = check_crc;
351 rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING, framing);
355 target->supported_protocols = (1 << protocol);
356 rc = nfc_targets_found(ddev->nfc_dev, target, 1);
360 ddev->poll_tech_count = 0;
365 void digital_poll_next_tech(struct nfc_digital_dev *ddev)
367 digital_switch_rf(ddev, 0);
369 mutex_lock(&ddev->poll_lock);
371 if (!ddev->poll_tech_count) {
372 mutex_unlock(&ddev->poll_lock);
376 ddev->poll_tech_index = (ddev->poll_tech_index + 1) %
377 ddev->poll_tech_count;
379 mutex_unlock(&ddev->poll_lock);
381 schedule_work(&ddev->poll_work);
384 static void digital_wq_poll(struct work_struct *work)
387 struct digital_poll_tech *poll_tech;
388 struct nfc_digital_dev *ddev = container_of(work,
389 struct nfc_digital_dev,
391 mutex_lock(&ddev->poll_lock);
393 if (!ddev->poll_tech_count) {
394 mutex_unlock(&ddev->poll_lock);
398 poll_tech = &ddev->poll_techs[ddev->poll_tech_index];
400 mutex_unlock(&ddev->poll_lock);
402 rc = poll_tech->poll_func(ddev, poll_tech->rf_tech);
404 digital_poll_next_tech(ddev);
407 static void digital_add_poll_tech(struct nfc_digital_dev *ddev, u8 rf_tech,
408 digital_poll_t poll_func)
410 struct digital_poll_tech *poll_tech;
412 if (ddev->poll_tech_count >= NFC_DIGITAL_POLL_MODE_COUNT_MAX)
415 poll_tech = &ddev->poll_techs[ddev->poll_tech_count++];
417 poll_tech->rf_tech = rf_tech;
418 poll_tech->poll_func = poll_func;
422 * start_poll operation
424 * For every supported protocol, the corresponding polling function is added
425 * to the table of polling technologies (ddev->poll_techs[]) using
426 * digital_add_poll_tech().
427 * When a polling function fails (by timeout or protocol error) the next one is
428 * schedule by digital_poll_next_tech() on the poll workqueue (ddev->poll_work).
430 static int digital_start_poll(struct nfc_dev *nfc_dev, __u32 im_protocols,
433 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
434 u32 matching_im_protocols, matching_tm_protocols;
436 pr_debug("protocols: im 0x%x, tm 0x%x, supported 0x%x\n", im_protocols,
437 tm_protocols, ddev->protocols);
439 matching_im_protocols = ddev->protocols & im_protocols;
440 matching_tm_protocols = ddev->protocols & tm_protocols;
442 if (!matching_im_protocols && !matching_tm_protocols) {
443 pr_err("Unknown protocol\n");
447 if (ddev->poll_tech_count) {
448 pr_err("Already polling\n");
452 if (ddev->curr_protocol) {
453 pr_err("A target is already active\n");
457 ddev->poll_tech_count = 0;
458 ddev->poll_tech_index = 0;
460 if (matching_im_protocols & DIGITAL_PROTO_NFCA_RF_TECH)
461 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_106A,
462 digital_in_send_sens_req);
464 if (im_protocols & DIGITAL_PROTO_NFCF_RF_TECH) {
465 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_212F,
466 digital_in_send_sensf_req);
468 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_424F,
469 digital_in_send_sensf_req);
472 if (tm_protocols & NFC_PROTO_NFC_DEP_MASK) {
473 if (ddev->ops->tg_listen_mdaa) {
474 digital_add_poll_tech(ddev, 0,
475 digital_tg_listen_mdaa);
477 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_106A,
478 digital_tg_listen_nfca);
480 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_212F,
481 digital_tg_listen_nfcf);
483 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_424F,
484 digital_tg_listen_nfcf);
488 if (!ddev->poll_tech_count) {
489 pr_err("Unsupported protocols: im=0x%x, tm=0x%x\n",
490 matching_im_protocols, matching_tm_protocols);
494 schedule_work(&ddev->poll_work);
499 static void digital_stop_poll(struct nfc_dev *nfc_dev)
501 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
503 mutex_lock(&ddev->poll_lock);
505 if (!ddev->poll_tech_count) {
506 pr_err("Polling operation was not running\n");
507 mutex_unlock(&ddev->poll_lock);
511 ddev->poll_tech_count = 0;
513 mutex_unlock(&ddev->poll_lock);
515 cancel_work_sync(&ddev->poll_work);
517 digital_abort_cmd(ddev);
520 static int digital_dev_up(struct nfc_dev *nfc_dev)
522 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
524 digital_switch_rf(ddev, 1);
529 static int digital_dev_down(struct nfc_dev *nfc_dev)
531 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
533 digital_switch_rf(ddev, 0);
538 static int digital_dep_link_up(struct nfc_dev *nfc_dev,
539 struct nfc_target *target,
540 __u8 comm_mode, __u8 *gb, size_t gb_len)
542 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
545 rc = digital_in_send_atr_req(ddev, target, comm_mode, gb, gb_len);
548 ddev->curr_protocol = NFC_PROTO_NFC_DEP;
553 static int digital_dep_link_down(struct nfc_dev *nfc_dev)
555 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
557 ddev->curr_protocol = 0;
562 static int digital_activate_target(struct nfc_dev *nfc_dev,
563 struct nfc_target *target, __u32 protocol)
565 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
567 if (ddev->poll_tech_count) {
568 pr_err("Can't activate a target while polling\n");
572 if (ddev->curr_protocol) {
573 pr_err("A target is already active\n");
577 ddev->curr_protocol = protocol;
582 static void digital_deactivate_target(struct nfc_dev *nfc_dev,
583 struct nfc_target *target)
585 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
587 if (!ddev->curr_protocol) {
588 pr_err("No active target\n");
592 ddev->curr_protocol = 0;
595 static int digital_tg_send(struct nfc_dev *dev, struct sk_buff *skb)
597 struct nfc_digital_dev *ddev = nfc_get_drvdata(dev);
599 return digital_tg_send_dep_res(ddev, skb);
602 static void digital_in_send_complete(struct nfc_digital_dev *ddev, void *arg,
603 struct sk_buff *resp)
605 struct digital_data_exch *data_exch = arg;
613 if (ddev->curr_protocol == NFC_PROTO_MIFARE)
614 rc = digital_in_recv_mifare_res(resp);
616 rc = ddev->skb_check_crc(resp);
624 data_exch->cb(data_exch->cb_context, resp, rc);
629 static int digital_in_send(struct nfc_dev *nfc_dev, struct nfc_target *target,
630 struct sk_buff *skb, data_exchange_cb_t cb,
633 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
634 struct digital_data_exch *data_exch;
636 data_exch = kzalloc(sizeof(struct digital_data_exch), GFP_KERNEL);
638 pr_err("Failed to allocate data_exch struct\n");
643 data_exch->cb_context = cb_context;
645 if (ddev->curr_protocol == NFC_PROTO_NFC_DEP)
646 return digital_in_send_dep_req(ddev, target, skb, data_exch);
648 ddev->skb_add_crc(skb);
650 return digital_in_send_cmd(ddev, skb, 500, digital_in_send_complete,
654 static struct nfc_ops digital_nfc_ops = {
655 .dev_up = digital_dev_up,
656 .dev_down = digital_dev_down,
657 .start_poll = digital_start_poll,
658 .stop_poll = digital_stop_poll,
659 .dep_link_up = digital_dep_link_up,
660 .dep_link_down = digital_dep_link_down,
661 .activate_target = digital_activate_target,
662 .deactivate_target = digital_deactivate_target,
663 .tm_send = digital_tg_send,
664 .im_transceive = digital_in_send,
667 struct nfc_digital_dev *nfc_digital_allocate_device(struct nfc_digital_ops *ops,
668 __u32 supported_protocols,
669 __u32 driver_capabilities,
670 int tx_headroom, int tx_tailroom)
672 struct nfc_digital_dev *ddev;
674 if (!ops->in_configure_hw || !ops->in_send_cmd || !ops->tg_listen ||
675 !ops->tg_configure_hw || !ops->tg_send_cmd || !ops->abort_cmd ||
679 ddev = kzalloc(sizeof(struct nfc_digital_dev), GFP_KERNEL);
683 ddev->driver_capabilities = driver_capabilities;
686 mutex_init(&ddev->cmd_lock);
687 INIT_LIST_HEAD(&ddev->cmd_queue);
689 INIT_WORK(&ddev->cmd_work, digital_wq_cmd);
690 INIT_WORK(&ddev->cmd_complete_work, digital_wq_cmd_complete);
692 mutex_init(&ddev->poll_lock);
693 INIT_WORK(&ddev->poll_work, digital_wq_poll);
695 if (supported_protocols & NFC_PROTO_JEWEL_MASK)
696 ddev->protocols |= NFC_PROTO_JEWEL_MASK;
697 if (supported_protocols & NFC_PROTO_MIFARE_MASK)
698 ddev->protocols |= NFC_PROTO_MIFARE_MASK;
699 if (supported_protocols & NFC_PROTO_FELICA_MASK)
700 ddev->protocols |= NFC_PROTO_FELICA_MASK;
701 if (supported_protocols & NFC_PROTO_NFC_DEP_MASK)
702 ddev->protocols |= NFC_PROTO_NFC_DEP_MASK;
704 ddev->tx_headroom = tx_headroom + DIGITAL_MAX_HEADER_LEN;
705 ddev->tx_tailroom = tx_tailroom + DIGITAL_CRC_LEN;
707 ddev->nfc_dev = nfc_allocate_device(&digital_nfc_ops, ddev->protocols,
710 if (!ddev->nfc_dev) {
711 pr_err("nfc_allocate_device failed\n");
715 nfc_set_drvdata(ddev->nfc_dev, ddev);
724 EXPORT_SYMBOL(nfc_digital_allocate_device);
726 void nfc_digital_free_device(struct nfc_digital_dev *ddev)
728 nfc_free_device(ddev->nfc_dev);
731 EXPORT_SYMBOL(nfc_digital_free_device);
733 int nfc_digital_register_device(struct nfc_digital_dev *ddev)
735 return nfc_register_device(ddev->nfc_dev);
737 EXPORT_SYMBOL(nfc_digital_register_device);
739 void nfc_digital_unregister_device(struct nfc_digital_dev *ddev)
741 struct digital_cmd *cmd, *n;
743 nfc_unregister_device(ddev->nfc_dev);
745 mutex_lock(&ddev->poll_lock);
746 ddev->poll_tech_count = 0;
747 mutex_unlock(&ddev->poll_lock);
749 cancel_work_sync(&ddev->poll_work);
750 cancel_work_sync(&ddev->cmd_work);
751 cancel_work_sync(&ddev->cmd_complete_work);
753 list_for_each_entry_safe(cmd, n, &ddev->cmd_queue, queue) {
754 list_del(&cmd->queue);
755 kfree(cmd->mdaa_params);
759 EXPORT_SYMBOL(nfc_digital_unregister_device);
761 MODULE_LICENSE("GPL");