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;
36 nfc_digital_cmd_complete_t cmd_cb;
40 struct sk_buff *digital_skb_alloc(struct nfc_digital_dev *ddev,
45 skb = alloc_skb(len + ddev->tx_headroom + ddev->tx_tailroom,
48 skb_reserve(skb, ddev->tx_headroom);
53 void digital_skb_add_crc(struct sk_buff *skb, crc_func_t crc_func, u16 init,
54 u8 bitwise_inv, u8 msb_first)
58 crc = crc_func(init, skb->data, skb->len);
66 *skb_put(skb, 1) = crc & 0xFF;
67 *skb_put(skb, 1) = (crc >> 8) & 0xFF;
70 int digital_skb_check_crc(struct sk_buff *skb, crc_func_t crc_func,
71 u16 crc_init, u8 bitwise_inv, u8 msb_first)
79 crc = crc_func(crc_init, skb->data, skb->len - 2);
87 rc = (skb->data[skb->len - 2] - (crc & 0xFF)) +
88 (skb->data[skb->len - 1] - ((crc >> 8) & 0xFF));
93 skb_trim(skb, skb->len - 2);
98 static inline void digital_switch_rf(struct nfc_digital_dev *ddev, bool on)
100 ddev->ops->switch_rf(ddev, on);
103 static inline void digital_abort_cmd(struct nfc_digital_dev *ddev)
105 ddev->ops->abort_cmd(ddev);
108 static void digital_wq_cmd_complete(struct work_struct *work)
110 struct digital_cmd *cmd;
111 struct nfc_digital_dev *ddev = container_of(work,
112 struct nfc_digital_dev,
115 mutex_lock(&ddev->cmd_lock);
117 cmd = list_first_entry_or_null(&ddev->cmd_queue, struct digital_cmd,
120 mutex_unlock(&ddev->cmd_lock);
124 list_del(&cmd->queue);
126 mutex_unlock(&ddev->cmd_lock);
128 if (!IS_ERR(cmd->resp))
129 print_hex_dump_debug("DIGITAL RX: ", DUMP_PREFIX_NONE, 16, 1,
130 cmd->resp->data, cmd->resp->len, false);
132 cmd->cmd_cb(ddev, cmd->cb_context, cmd->resp);
136 schedule_work(&ddev->cmd_work);
139 static void digital_send_cmd_complete(struct nfc_digital_dev *ddev,
140 void *arg, struct sk_buff *resp)
142 struct digital_cmd *cmd = arg;
146 schedule_work(&ddev->cmd_complete_work);
149 static void digital_wq_cmd(struct work_struct *work)
152 struct digital_cmd *cmd;
153 struct nfc_digital_dev *ddev = container_of(work,
154 struct nfc_digital_dev,
157 mutex_lock(&ddev->cmd_lock);
159 cmd = list_first_entry_or_null(&ddev->cmd_queue, struct digital_cmd,
161 if (!cmd || cmd->pending) {
162 mutex_unlock(&ddev->cmd_lock);
166 mutex_unlock(&ddev->cmd_lock);
169 print_hex_dump_debug("DIGITAL TX: ", DUMP_PREFIX_NONE, 16, 1,
170 cmd->req->data, cmd->req->len, false);
173 case DIGITAL_CMD_IN_SEND:
174 rc = ddev->ops->in_send_cmd(ddev, cmd->req, cmd->timeout,
175 digital_send_cmd_complete, cmd);
178 PR_ERR("Unknown cmd type %d", cmd->type);
185 PR_ERR("in_send_command returned err %d", rc);
187 mutex_lock(&ddev->cmd_lock);
188 list_del(&cmd->queue);
189 mutex_unlock(&ddev->cmd_lock);
194 schedule_work(&ddev->cmd_work);
197 int digital_send_cmd(struct nfc_digital_dev *ddev, u8 cmd_type,
198 struct sk_buff *skb, u16 timeout,
199 nfc_digital_cmd_complete_t cmd_cb, void *cb_context)
201 struct digital_cmd *cmd;
203 cmd = kzalloc(sizeof(struct digital_cmd), GFP_KERNEL);
207 cmd->type = cmd_type;
208 cmd->timeout = timeout;
210 cmd->cmd_cb = cmd_cb;
211 cmd->cb_context = cb_context;
212 INIT_LIST_HEAD(&cmd->queue);
214 mutex_lock(&ddev->cmd_lock);
215 list_add_tail(&cmd->queue, &ddev->cmd_queue);
216 mutex_unlock(&ddev->cmd_lock);
218 schedule_work(&ddev->cmd_work);
223 int digital_in_configure_hw(struct nfc_digital_dev *ddev, int type, int param)
227 rc = ddev->ops->in_configure_hw(ddev, type, param);
229 PR_ERR("in_configure_hw failed: %d", rc);
234 int digital_target_found(struct nfc_digital_dev *ddev,
235 struct nfc_target *target, u8 protocol)
240 int (*check_crc)(struct sk_buff *skb);
241 void (*add_crc)(struct sk_buff *skb);
243 rf_tech = ddev->poll_techs[ddev->poll_tech_index].rf_tech;
246 case NFC_PROTO_JEWEL:
247 framing = NFC_DIGITAL_FRAMING_NFCA_T1T;
248 check_crc = digital_skb_check_crc_b;
249 add_crc = digital_skb_add_crc_b;
252 case NFC_PROTO_MIFARE:
253 framing = NFC_DIGITAL_FRAMING_NFCA_T2T;
254 check_crc = digital_skb_check_crc_a;
255 add_crc = digital_skb_add_crc_a;
258 case NFC_PROTO_FELICA:
259 framing = NFC_DIGITAL_FRAMING_NFCF_T3T;
260 check_crc = digital_skb_check_crc_f;
261 add_crc = digital_skb_add_crc_f;
264 case NFC_PROTO_NFC_DEP:
265 if (rf_tech == NFC_DIGITAL_RF_TECH_106A) {
266 framing = NFC_DIGITAL_FRAMING_NFCA_NFC_DEP;
267 check_crc = digital_skb_check_crc_a;
268 add_crc = digital_skb_add_crc_a;
270 framing = NFC_DIGITAL_FRAMING_NFCF_NFC_DEP;
271 check_crc = digital_skb_check_crc_f;
272 add_crc = digital_skb_add_crc_f;
277 PR_ERR("Invalid protocol %d", protocol);
281 PR_DBG("rf_tech=%d, protocol=%d", rf_tech, protocol);
283 ddev->curr_rf_tech = rf_tech;
284 ddev->curr_protocol = protocol;
286 if (DIGITAL_DRV_CAPS_IN_CRC(ddev)) {
287 ddev->skb_add_crc = digital_skb_add_crc_none;
288 ddev->skb_check_crc = digital_skb_check_crc_none;
290 ddev->skb_add_crc = add_crc;
291 ddev->skb_check_crc = check_crc;
294 rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING, framing);
298 target->supported_protocols = (1 << protocol);
299 rc = nfc_targets_found(ddev->nfc_dev, target, 1);
303 ddev->poll_tech_count = 0;
308 void digital_poll_next_tech(struct nfc_digital_dev *ddev)
310 digital_switch_rf(ddev, 0);
312 mutex_lock(&ddev->poll_lock);
314 if (!ddev->poll_tech_count) {
315 mutex_unlock(&ddev->poll_lock);
319 ddev->poll_tech_index = (ddev->poll_tech_index + 1) %
320 ddev->poll_tech_count;
322 mutex_unlock(&ddev->poll_lock);
324 schedule_work(&ddev->poll_work);
327 static void digital_wq_poll(struct work_struct *work)
330 struct digital_poll_tech *poll_tech;
331 struct nfc_digital_dev *ddev = container_of(work,
332 struct nfc_digital_dev,
334 mutex_lock(&ddev->poll_lock);
336 if (!ddev->poll_tech_count) {
337 mutex_unlock(&ddev->poll_lock);
341 poll_tech = &ddev->poll_techs[ddev->poll_tech_index];
343 mutex_unlock(&ddev->poll_lock);
345 rc = poll_tech->poll_func(ddev, poll_tech->rf_tech);
347 digital_poll_next_tech(ddev);
350 static void digital_add_poll_tech(struct nfc_digital_dev *ddev, u8 rf_tech,
351 digital_poll_t poll_func)
353 struct digital_poll_tech *poll_tech;
355 if (ddev->poll_tech_count >= NFC_DIGITAL_POLL_MODE_COUNT_MAX)
358 poll_tech = &ddev->poll_techs[ddev->poll_tech_count++];
360 poll_tech->rf_tech = rf_tech;
361 poll_tech->poll_func = poll_func;
365 * start_poll operation
367 * For every supported protocol, the corresponding polling function is added
368 * to the table of polling technologies (ddev->poll_techs[]) using
369 * digital_add_poll_tech().
370 * When a polling function fails (by timeout or protocol error) the next one is
371 * schedule by digital_poll_next_tech() on the poll workqueue (ddev->poll_work).
373 static int digital_start_poll(struct nfc_dev *nfc_dev, __u32 im_protocols,
376 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
377 u32 matching_im_protocols, matching_tm_protocols;
379 PR_DBG("protocols: im 0x%x, tm 0x%x, supported 0x%x", im_protocols,
380 tm_protocols, ddev->protocols);
382 matching_im_protocols = ddev->protocols & im_protocols;
383 matching_tm_protocols = ddev->protocols & tm_protocols;
385 if (!matching_im_protocols && !matching_tm_protocols) {
386 PR_ERR("No known protocol");
390 if (ddev->poll_tech_count) {
391 PR_ERR("Already polling");
395 if (ddev->curr_protocol) {
396 PR_ERR("A target is already active");
400 ddev->poll_tech_count = 0;
401 ddev->poll_tech_index = 0;
403 if (matching_im_protocols & DIGITAL_PROTO_NFCA_RF_TECH)
404 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_106A,
405 digital_in_send_sens_req);
407 if (im_protocols & DIGITAL_PROTO_NFCF_RF_TECH) {
408 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_212F,
409 digital_in_send_sensf_req);
411 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_424F,
412 digital_in_send_sensf_req);
415 if (!ddev->poll_tech_count) {
416 PR_ERR("Unsupported protocols: im=0x%x, tm=0x%x",
417 matching_im_protocols, matching_tm_protocols);
421 schedule_work(&ddev->poll_work);
426 static void digital_stop_poll(struct nfc_dev *nfc_dev)
428 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
430 mutex_lock(&ddev->poll_lock);
432 if (!ddev->poll_tech_count) {
433 PR_ERR("Polling operation was not running");
434 mutex_unlock(&ddev->poll_lock);
438 ddev->poll_tech_count = 0;
440 mutex_unlock(&ddev->poll_lock);
442 cancel_work_sync(&ddev->poll_work);
444 digital_abort_cmd(ddev);
447 static int digital_dev_up(struct nfc_dev *nfc_dev)
449 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
451 digital_switch_rf(ddev, 1);
456 static int digital_dev_down(struct nfc_dev *nfc_dev)
458 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
460 digital_switch_rf(ddev, 0);
465 static int digital_dep_link_up(struct nfc_dev *nfc_dev,
466 struct nfc_target *target,
467 __u8 comm_mode, __u8 *gb, size_t gb_len)
469 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
471 return digital_in_send_atr_req(ddev, target, comm_mode, gb, gb_len);
474 static int digital_dep_link_down(struct nfc_dev *nfc_dev)
476 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
478 ddev->curr_protocol = 0;
483 static int digital_activate_target(struct nfc_dev *nfc_dev,
484 struct nfc_target *target, __u32 protocol)
489 static void digital_deactivate_target(struct nfc_dev *nfc_dev,
490 struct nfc_target *target)
492 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
494 ddev->curr_protocol = 0;
497 static int digital_tg_send(struct nfc_dev *dev, struct sk_buff *skb)
502 static void digital_in_send_complete(struct nfc_digital_dev *ddev, void *arg,
503 struct sk_buff *resp)
505 struct digital_data_exch *data_exch = arg;
513 if (ddev->curr_protocol == NFC_PROTO_MIFARE)
514 rc = digital_in_recv_mifare_res(resp);
516 rc = ddev->skb_check_crc(resp);
524 data_exch->cb(data_exch->cb_context, resp, rc);
529 static int digital_in_send(struct nfc_dev *nfc_dev, struct nfc_target *target,
530 struct sk_buff *skb, data_exchange_cb_t cb,
533 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
534 struct digital_data_exch *data_exch;
536 data_exch = kzalloc(sizeof(struct digital_data_exch), GFP_KERNEL);
538 PR_ERR("Failed to allocate data_exch struct");
543 data_exch->cb_context = cb_context;
545 if (ddev->curr_protocol == NFC_PROTO_NFC_DEP)
546 return digital_in_send_dep_req(ddev, target, skb, data_exch);
548 ddev->skb_add_crc(skb);
550 return digital_in_send_cmd(ddev, skb, 500, digital_in_send_complete,
554 static struct nfc_ops digital_nfc_ops = {
555 .dev_up = digital_dev_up,
556 .dev_down = digital_dev_down,
557 .start_poll = digital_start_poll,
558 .stop_poll = digital_stop_poll,
559 .dep_link_up = digital_dep_link_up,
560 .dep_link_down = digital_dep_link_down,
561 .activate_target = digital_activate_target,
562 .deactivate_target = digital_deactivate_target,
563 .tm_send = digital_tg_send,
564 .im_transceive = digital_in_send,
567 struct nfc_digital_dev *nfc_digital_allocate_device(struct nfc_digital_ops *ops,
568 __u32 supported_protocols,
569 __u32 driver_capabilities,
570 int tx_headroom, int tx_tailroom)
572 struct nfc_digital_dev *ddev;
574 if (!ops->in_configure_hw || !ops->in_send_cmd || !ops->tg_listen ||
575 !ops->tg_configure_hw || !ops->tg_send_cmd || !ops->abort_cmd ||
579 ddev = kzalloc(sizeof(struct nfc_digital_dev), GFP_KERNEL);
581 PR_ERR("kzalloc failed");
585 ddev->driver_capabilities = driver_capabilities;
588 mutex_init(&ddev->cmd_lock);
589 INIT_LIST_HEAD(&ddev->cmd_queue);
591 INIT_WORK(&ddev->cmd_work, digital_wq_cmd);
592 INIT_WORK(&ddev->cmd_complete_work, digital_wq_cmd_complete);
594 mutex_init(&ddev->poll_lock);
595 INIT_WORK(&ddev->poll_work, digital_wq_poll);
597 if (supported_protocols & NFC_PROTO_JEWEL_MASK)
598 ddev->protocols |= NFC_PROTO_JEWEL_MASK;
599 if (supported_protocols & NFC_PROTO_MIFARE_MASK)
600 ddev->protocols |= NFC_PROTO_MIFARE_MASK;
601 if (supported_protocols & NFC_PROTO_FELICA_MASK)
602 ddev->protocols |= NFC_PROTO_FELICA_MASK;
603 if (supported_protocols & NFC_PROTO_NFC_DEP_MASK)
604 ddev->protocols |= NFC_PROTO_NFC_DEP_MASK;
606 ddev->tx_headroom = tx_headroom + DIGITAL_MAX_HEADER_LEN;
607 ddev->tx_tailroom = tx_tailroom + DIGITAL_CRC_LEN;
609 ddev->nfc_dev = nfc_allocate_device(&digital_nfc_ops, ddev->protocols,
612 if (!ddev->nfc_dev) {
613 PR_ERR("nfc_allocate_device failed");
617 nfc_set_drvdata(ddev->nfc_dev, ddev);
626 EXPORT_SYMBOL(nfc_digital_allocate_device);
628 void nfc_digital_free_device(struct nfc_digital_dev *ddev)
630 nfc_free_device(ddev->nfc_dev);
633 EXPORT_SYMBOL(nfc_digital_free_device);
635 int nfc_digital_register_device(struct nfc_digital_dev *ddev)
637 return nfc_register_device(ddev->nfc_dev);
639 EXPORT_SYMBOL(nfc_digital_register_device);
641 void nfc_digital_unregister_device(struct nfc_digital_dev *ddev)
643 struct digital_cmd *cmd, *n;
645 nfc_unregister_device(ddev->nfc_dev);
647 mutex_lock(&ddev->poll_lock);
648 ddev->poll_tech_count = 0;
649 mutex_unlock(&ddev->poll_lock);
651 cancel_work_sync(&ddev->poll_work);
652 cancel_work_sync(&ddev->cmd_work);
653 cancel_work_sync(&ddev->cmd_complete_work);
655 list_for_each_entry_safe(cmd, n, &ddev->cmd_queue, queue) {
656 list_del(&cmd->queue);
660 EXPORT_SYMBOL(nfc_digital_unregister_device);
662 MODULE_LICENSE("GPL");