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", cmd->type);
208 pr_err("in_send_command returned err %d", 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", 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", 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", protocol);
339 pr_debug("rf_tech=%d, protocol=%d", rf_tech, protocol);
341 ddev->curr_rf_tech = rf_tech;
342 ddev->curr_protocol = protocol;
344 if (DIGITAL_DRV_CAPS_IN_CRC(ddev)) {
345 ddev->skb_add_crc = digital_skb_add_crc_none;
346 ddev->skb_check_crc = digital_skb_check_crc_none;
348 ddev->skb_add_crc = add_crc;
349 ddev->skb_check_crc = check_crc;
352 rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING, framing);
356 target->supported_protocols = (1 << protocol);
357 rc = nfc_targets_found(ddev->nfc_dev, target, 1);
361 ddev->poll_tech_count = 0;
366 void digital_poll_next_tech(struct nfc_digital_dev *ddev)
368 digital_switch_rf(ddev, 0);
370 mutex_lock(&ddev->poll_lock);
372 if (!ddev->poll_tech_count) {
373 mutex_unlock(&ddev->poll_lock);
377 ddev->poll_tech_index = (ddev->poll_tech_index + 1) %
378 ddev->poll_tech_count;
380 mutex_unlock(&ddev->poll_lock);
382 schedule_work(&ddev->poll_work);
385 static void digital_wq_poll(struct work_struct *work)
388 struct digital_poll_tech *poll_tech;
389 struct nfc_digital_dev *ddev = container_of(work,
390 struct nfc_digital_dev,
392 mutex_lock(&ddev->poll_lock);
394 if (!ddev->poll_tech_count) {
395 mutex_unlock(&ddev->poll_lock);
399 poll_tech = &ddev->poll_techs[ddev->poll_tech_index];
401 mutex_unlock(&ddev->poll_lock);
403 rc = poll_tech->poll_func(ddev, poll_tech->rf_tech);
405 digital_poll_next_tech(ddev);
408 static void digital_add_poll_tech(struct nfc_digital_dev *ddev, u8 rf_tech,
409 digital_poll_t poll_func)
411 struct digital_poll_tech *poll_tech;
413 if (ddev->poll_tech_count >= NFC_DIGITAL_POLL_MODE_COUNT_MAX)
416 poll_tech = &ddev->poll_techs[ddev->poll_tech_count++];
418 poll_tech->rf_tech = rf_tech;
419 poll_tech->poll_func = poll_func;
423 * start_poll operation
425 * For every supported protocol, the corresponding polling function is added
426 * to the table of polling technologies (ddev->poll_techs[]) using
427 * digital_add_poll_tech().
428 * When a polling function fails (by timeout or protocol error) the next one is
429 * schedule by digital_poll_next_tech() on the poll workqueue (ddev->poll_work).
431 static int digital_start_poll(struct nfc_dev *nfc_dev, __u32 im_protocols,
434 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
435 u32 matching_im_protocols, matching_tm_protocols;
437 pr_debug("protocols: im 0x%x, tm 0x%x, supported 0x%x", im_protocols,
438 tm_protocols, ddev->protocols);
440 matching_im_protocols = ddev->protocols & im_protocols;
441 matching_tm_protocols = ddev->protocols & tm_protocols;
443 if (!matching_im_protocols && !matching_tm_protocols) {
444 pr_err("No known protocol");
448 if (ddev->poll_tech_count) {
449 pr_err("Already polling");
453 if (ddev->curr_protocol) {
454 pr_err("A target is already active");
458 ddev->poll_tech_count = 0;
459 ddev->poll_tech_index = 0;
461 if (matching_im_protocols & DIGITAL_PROTO_NFCA_RF_TECH)
462 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_106A,
463 digital_in_send_sens_req);
465 if (im_protocols & DIGITAL_PROTO_NFCF_RF_TECH) {
466 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_212F,
467 digital_in_send_sensf_req);
469 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_424F,
470 digital_in_send_sensf_req);
473 if (tm_protocols & NFC_PROTO_NFC_DEP_MASK) {
474 if (ddev->ops->tg_listen_mdaa) {
475 digital_add_poll_tech(ddev, 0,
476 digital_tg_listen_mdaa);
478 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_106A,
479 digital_tg_listen_nfca);
481 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_212F,
482 digital_tg_listen_nfcf);
484 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_424F,
485 digital_tg_listen_nfcf);
489 if (!ddev->poll_tech_count) {
490 pr_err("Unsupported protocols: im=0x%x, tm=0x%x",
491 matching_im_protocols, matching_tm_protocols);
495 schedule_work(&ddev->poll_work);
500 static void digital_stop_poll(struct nfc_dev *nfc_dev)
502 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
504 mutex_lock(&ddev->poll_lock);
506 if (!ddev->poll_tech_count) {
507 pr_err("Polling operation was not running");
508 mutex_unlock(&ddev->poll_lock);
512 ddev->poll_tech_count = 0;
514 mutex_unlock(&ddev->poll_lock);
516 cancel_work_sync(&ddev->poll_work);
518 digital_abort_cmd(ddev);
521 static int digital_dev_up(struct nfc_dev *nfc_dev)
523 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
525 digital_switch_rf(ddev, 1);
530 static int digital_dev_down(struct nfc_dev *nfc_dev)
532 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
534 digital_switch_rf(ddev, 0);
539 static int digital_dep_link_up(struct nfc_dev *nfc_dev,
540 struct nfc_target *target,
541 __u8 comm_mode, __u8 *gb, size_t gb_len)
543 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
545 return digital_in_send_atr_req(ddev, target, comm_mode, gb, gb_len);
548 static int digital_dep_link_down(struct nfc_dev *nfc_dev)
550 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
552 ddev->curr_protocol = 0;
557 static int digital_activate_target(struct nfc_dev *nfc_dev,
558 struct nfc_target *target, __u32 protocol)
563 static void digital_deactivate_target(struct nfc_dev *nfc_dev,
564 struct nfc_target *target)
566 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
568 ddev->curr_protocol = 0;
571 static int digital_tg_send(struct nfc_dev *dev, struct sk_buff *skb)
573 struct nfc_digital_dev *ddev = nfc_get_drvdata(dev);
575 return digital_tg_send_dep_res(ddev, skb);
578 static void digital_in_send_complete(struct nfc_digital_dev *ddev, void *arg,
579 struct sk_buff *resp)
581 struct digital_data_exch *data_exch = arg;
589 if (ddev->curr_protocol == NFC_PROTO_MIFARE)
590 rc = digital_in_recv_mifare_res(resp);
592 rc = ddev->skb_check_crc(resp);
600 data_exch->cb(data_exch->cb_context, resp, rc);
605 static int digital_in_send(struct nfc_dev *nfc_dev, struct nfc_target *target,
606 struct sk_buff *skb, data_exchange_cb_t cb,
609 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
610 struct digital_data_exch *data_exch;
612 data_exch = kzalloc(sizeof(struct digital_data_exch), GFP_KERNEL);
614 pr_err("Failed to allocate data_exch struct");
619 data_exch->cb_context = cb_context;
621 if (ddev->curr_protocol == NFC_PROTO_NFC_DEP)
622 return digital_in_send_dep_req(ddev, target, skb, data_exch);
624 ddev->skb_add_crc(skb);
626 return digital_in_send_cmd(ddev, skb, 500, digital_in_send_complete,
630 static struct nfc_ops digital_nfc_ops = {
631 .dev_up = digital_dev_up,
632 .dev_down = digital_dev_down,
633 .start_poll = digital_start_poll,
634 .stop_poll = digital_stop_poll,
635 .dep_link_up = digital_dep_link_up,
636 .dep_link_down = digital_dep_link_down,
637 .activate_target = digital_activate_target,
638 .deactivate_target = digital_deactivate_target,
639 .tm_send = digital_tg_send,
640 .im_transceive = digital_in_send,
643 struct nfc_digital_dev *nfc_digital_allocate_device(struct nfc_digital_ops *ops,
644 __u32 supported_protocols,
645 __u32 driver_capabilities,
646 int tx_headroom, int tx_tailroom)
648 struct nfc_digital_dev *ddev;
650 if (!ops->in_configure_hw || !ops->in_send_cmd || !ops->tg_listen ||
651 !ops->tg_configure_hw || !ops->tg_send_cmd || !ops->abort_cmd ||
655 ddev = kzalloc(sizeof(struct nfc_digital_dev), GFP_KERNEL);
657 pr_err("kzalloc failed");
661 ddev->driver_capabilities = driver_capabilities;
664 mutex_init(&ddev->cmd_lock);
665 INIT_LIST_HEAD(&ddev->cmd_queue);
667 INIT_WORK(&ddev->cmd_work, digital_wq_cmd);
668 INIT_WORK(&ddev->cmd_complete_work, digital_wq_cmd_complete);
670 mutex_init(&ddev->poll_lock);
671 INIT_WORK(&ddev->poll_work, digital_wq_poll);
673 if (supported_protocols & NFC_PROTO_JEWEL_MASK)
674 ddev->protocols |= NFC_PROTO_JEWEL_MASK;
675 if (supported_protocols & NFC_PROTO_MIFARE_MASK)
676 ddev->protocols |= NFC_PROTO_MIFARE_MASK;
677 if (supported_protocols & NFC_PROTO_FELICA_MASK)
678 ddev->protocols |= NFC_PROTO_FELICA_MASK;
679 if (supported_protocols & NFC_PROTO_NFC_DEP_MASK)
680 ddev->protocols |= NFC_PROTO_NFC_DEP_MASK;
682 ddev->tx_headroom = tx_headroom + DIGITAL_MAX_HEADER_LEN;
683 ddev->tx_tailroom = tx_tailroom + DIGITAL_CRC_LEN;
685 ddev->nfc_dev = nfc_allocate_device(&digital_nfc_ops, ddev->protocols,
688 if (!ddev->nfc_dev) {
689 pr_err("nfc_allocate_device failed");
693 nfc_set_drvdata(ddev->nfc_dev, ddev);
702 EXPORT_SYMBOL(nfc_digital_allocate_device);
704 void nfc_digital_free_device(struct nfc_digital_dev *ddev)
706 nfc_free_device(ddev->nfc_dev);
709 EXPORT_SYMBOL(nfc_digital_free_device);
711 int nfc_digital_register_device(struct nfc_digital_dev *ddev)
713 return nfc_register_device(ddev->nfc_dev);
715 EXPORT_SYMBOL(nfc_digital_register_device);
717 void nfc_digital_unregister_device(struct nfc_digital_dev *ddev)
719 struct digital_cmd *cmd, *n;
721 nfc_unregister_device(ddev->nfc_dev);
723 mutex_lock(&ddev->poll_lock);
724 ddev->poll_tech_count = 0;
725 mutex_unlock(&ddev->poll_lock);
727 cancel_work_sync(&ddev->poll_work);
728 cancel_work_sync(&ddev->cmd_work);
729 cancel_work_sync(&ddev->cmd_complete_work);
731 list_for_each_entry_safe(cmd, n, &ddev->cmd_queue, queue) {
732 list_del(&cmd->queue);
733 kfree(cmd->mdaa_params);
737 EXPORT_SYMBOL(nfc_digital_unregister_device);
739 MODULE_LICENSE("GPL");