tcp: minor optimization around tcp_hdr() usage in receive path
[linux-2.6/btrfs-unstable.git] / net / nfc / digital_core.c
blobec0a8998e52dc0f3c6124039aaeee4b0ca5eaeb2
1 /*
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
12 * more details.
16 #define pr_fmt(fmt) "digital: %s: " fmt, __func__
18 #include <linux/module.h>
20 #include "digital.h"
22 #define DIGITAL_PROTO_NFCA_RF_TECH \
23 (NFC_PROTO_JEWEL_MASK | NFC_PROTO_MIFARE_MASK | \
24 NFC_PROTO_NFC_DEP_MASK | NFC_PROTO_ISO14443_MASK)
26 #define DIGITAL_PROTO_NFCB_RF_TECH NFC_PROTO_ISO14443_B_MASK
28 #define DIGITAL_PROTO_NFCF_RF_TECH \
29 (NFC_PROTO_FELICA_MASK | NFC_PROTO_NFC_DEP_MASK)
31 #define DIGITAL_PROTO_ISO15693_RF_TECH NFC_PROTO_ISO15693_MASK
33 /* Delay between each poll frame (ms) */
34 #define DIGITAL_POLL_INTERVAL 10
36 struct digital_cmd {
37 struct list_head queue;
39 u8 type;
40 u8 pending;
42 u16 timeout;
43 struct sk_buff *req;
44 struct sk_buff *resp;
45 struct digital_tg_mdaa_params *mdaa_params;
47 nfc_digital_cmd_complete_t cmd_cb;
48 void *cb_context;
51 struct sk_buff *digital_skb_alloc(struct nfc_digital_dev *ddev,
52 unsigned int len)
54 struct sk_buff *skb;
56 skb = alloc_skb(len + ddev->tx_headroom + ddev->tx_tailroom,
57 GFP_KERNEL);
58 if (skb)
59 skb_reserve(skb, ddev->tx_headroom);
61 return skb;
64 void digital_skb_add_crc(struct sk_buff *skb, crc_func_t crc_func, u16 init,
65 u8 bitwise_inv, u8 msb_first)
67 u16 crc;
69 crc = crc_func(init, skb->data, skb->len);
71 if (bitwise_inv)
72 crc = ~crc;
74 if (msb_first)
75 crc = __fswab16(crc);
77 skb_put_u8(skb, crc & 0xFF);
78 skb_put_u8(skb, (crc >> 8) & 0xFF);
81 int digital_skb_check_crc(struct sk_buff *skb, crc_func_t crc_func,
82 u16 crc_init, u8 bitwise_inv, u8 msb_first)
84 int rc;
85 u16 crc;
87 if (skb->len <= 2)
88 return -EIO;
90 crc = crc_func(crc_init, skb->data, skb->len - 2);
92 if (bitwise_inv)
93 crc = ~crc;
95 if (msb_first)
96 crc = __swab16(crc);
98 rc = (skb->data[skb->len - 2] - (crc & 0xFF)) +
99 (skb->data[skb->len - 1] - ((crc >> 8) & 0xFF));
101 if (rc)
102 return -EIO;
104 skb_trim(skb, skb->len - 2);
106 return 0;
109 static inline void digital_switch_rf(struct nfc_digital_dev *ddev, bool on)
111 ddev->ops->switch_rf(ddev, on);
114 static inline void digital_abort_cmd(struct nfc_digital_dev *ddev)
116 ddev->ops->abort_cmd(ddev);
119 static void digital_wq_cmd_complete(struct work_struct *work)
121 struct digital_cmd *cmd;
122 struct nfc_digital_dev *ddev = container_of(work,
123 struct nfc_digital_dev,
124 cmd_complete_work);
126 mutex_lock(&ddev->cmd_lock);
128 cmd = list_first_entry_or_null(&ddev->cmd_queue, struct digital_cmd,
129 queue);
130 if (!cmd) {
131 mutex_unlock(&ddev->cmd_lock);
132 return;
135 list_del(&cmd->queue);
137 mutex_unlock(&ddev->cmd_lock);
139 if (!IS_ERR(cmd->resp))
140 print_hex_dump_debug("DIGITAL RX: ", DUMP_PREFIX_NONE, 16, 1,
141 cmd->resp->data, cmd->resp->len, false);
143 cmd->cmd_cb(ddev, cmd->cb_context, cmd->resp);
145 kfree(cmd->mdaa_params);
146 kfree(cmd);
148 schedule_work(&ddev->cmd_work);
151 static void digital_send_cmd_complete(struct nfc_digital_dev *ddev,
152 void *arg, struct sk_buff *resp)
154 struct digital_cmd *cmd = arg;
156 cmd->resp = resp;
158 schedule_work(&ddev->cmd_complete_work);
161 static void digital_wq_cmd(struct work_struct *work)
163 int rc;
164 struct digital_cmd *cmd;
165 struct digital_tg_mdaa_params *params;
166 struct nfc_digital_dev *ddev = container_of(work,
167 struct nfc_digital_dev,
168 cmd_work);
170 mutex_lock(&ddev->cmd_lock);
172 cmd = list_first_entry_or_null(&ddev->cmd_queue, struct digital_cmd,
173 queue);
174 if (!cmd || cmd->pending) {
175 mutex_unlock(&ddev->cmd_lock);
176 return;
179 cmd->pending = 1;
181 mutex_unlock(&ddev->cmd_lock);
183 if (cmd->req)
184 print_hex_dump_debug("DIGITAL TX: ", DUMP_PREFIX_NONE, 16, 1,
185 cmd->req->data, cmd->req->len, false);
187 switch (cmd->type) {
188 case DIGITAL_CMD_IN_SEND:
189 rc = ddev->ops->in_send_cmd(ddev, cmd->req, cmd->timeout,
190 digital_send_cmd_complete, cmd);
191 break;
193 case DIGITAL_CMD_TG_SEND:
194 rc = ddev->ops->tg_send_cmd(ddev, cmd->req, cmd->timeout,
195 digital_send_cmd_complete, cmd);
196 break;
198 case DIGITAL_CMD_TG_LISTEN:
199 rc = ddev->ops->tg_listen(ddev, cmd->timeout,
200 digital_send_cmd_complete, cmd);
201 break;
203 case DIGITAL_CMD_TG_LISTEN_MDAA:
204 params = cmd->mdaa_params;
206 rc = ddev->ops->tg_listen_mdaa(ddev, params, cmd->timeout,
207 digital_send_cmd_complete, cmd);
208 break;
210 case DIGITAL_CMD_TG_LISTEN_MD:
211 rc = ddev->ops->tg_listen_md(ddev, cmd->timeout,
212 digital_send_cmd_complete, cmd);
213 break;
215 default:
216 pr_err("Unknown cmd type %d\n", cmd->type);
217 return;
220 if (!rc)
221 return;
223 pr_err("in_send_command returned err %d\n", rc);
225 mutex_lock(&ddev->cmd_lock);
226 list_del(&cmd->queue);
227 mutex_unlock(&ddev->cmd_lock);
229 kfree_skb(cmd->req);
230 kfree(cmd->mdaa_params);
231 kfree(cmd);
233 schedule_work(&ddev->cmd_work);
236 int digital_send_cmd(struct nfc_digital_dev *ddev, u8 cmd_type,
237 struct sk_buff *skb, struct digital_tg_mdaa_params *params,
238 u16 timeout, nfc_digital_cmd_complete_t cmd_cb,
239 void *cb_context)
241 struct digital_cmd *cmd;
243 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
244 if (!cmd)
245 return -ENOMEM;
247 cmd->type = cmd_type;
248 cmd->timeout = timeout;
249 cmd->req = skb;
250 cmd->mdaa_params = params;
251 cmd->cmd_cb = cmd_cb;
252 cmd->cb_context = cb_context;
253 INIT_LIST_HEAD(&cmd->queue);
255 mutex_lock(&ddev->cmd_lock);
256 list_add_tail(&cmd->queue, &ddev->cmd_queue);
257 mutex_unlock(&ddev->cmd_lock);
259 schedule_work(&ddev->cmd_work);
261 return 0;
264 int digital_in_configure_hw(struct nfc_digital_dev *ddev, int type, int param)
266 int rc;
268 rc = ddev->ops->in_configure_hw(ddev, type, param);
269 if (rc)
270 pr_err("in_configure_hw failed: %d\n", rc);
272 return rc;
275 int digital_tg_configure_hw(struct nfc_digital_dev *ddev, int type, int param)
277 int rc;
279 rc = ddev->ops->tg_configure_hw(ddev, type, param);
280 if (rc)
281 pr_err("tg_configure_hw failed: %d\n", rc);
283 return rc;
286 static int digital_tg_listen_mdaa(struct nfc_digital_dev *ddev, u8 rf_tech)
288 struct digital_tg_mdaa_params *params;
290 params = kzalloc(sizeof(*params), GFP_KERNEL);
291 if (!params)
292 return -ENOMEM;
294 params->sens_res = DIGITAL_SENS_RES_NFC_DEP;
295 get_random_bytes(params->nfcid1, sizeof(params->nfcid1));
296 params->sel_res = DIGITAL_SEL_RES_NFC_DEP;
298 params->nfcid2[0] = DIGITAL_SENSF_NFCID2_NFC_DEP_B1;
299 params->nfcid2[1] = DIGITAL_SENSF_NFCID2_NFC_DEP_B2;
300 get_random_bytes(params->nfcid2 + 2, NFC_NFCID2_MAXSIZE - 2);
301 params->sc = DIGITAL_SENSF_FELICA_SC;
303 return digital_send_cmd(ddev, DIGITAL_CMD_TG_LISTEN_MDAA, NULL, params,
304 500, digital_tg_recv_atr_req, NULL);
307 static int digital_tg_listen_md(struct nfc_digital_dev *ddev, u8 rf_tech)
309 return digital_send_cmd(ddev, DIGITAL_CMD_TG_LISTEN_MD, NULL, NULL, 500,
310 digital_tg_recv_md_req, NULL);
313 int digital_target_found(struct nfc_digital_dev *ddev,
314 struct nfc_target *target, u8 protocol)
316 int rc;
317 u8 framing;
318 u8 rf_tech;
319 u8 poll_tech_count;
320 int (*check_crc)(struct sk_buff *skb);
321 void (*add_crc)(struct sk_buff *skb);
323 rf_tech = ddev->poll_techs[ddev->poll_tech_index].rf_tech;
325 switch (protocol) {
326 case NFC_PROTO_JEWEL:
327 framing = NFC_DIGITAL_FRAMING_NFCA_T1T;
328 check_crc = digital_skb_check_crc_b;
329 add_crc = digital_skb_add_crc_b;
330 break;
332 case NFC_PROTO_MIFARE:
333 framing = NFC_DIGITAL_FRAMING_NFCA_T2T;
334 check_crc = digital_skb_check_crc_a;
335 add_crc = digital_skb_add_crc_a;
336 break;
338 case NFC_PROTO_FELICA:
339 framing = NFC_DIGITAL_FRAMING_NFCF_T3T;
340 check_crc = digital_skb_check_crc_f;
341 add_crc = digital_skb_add_crc_f;
342 break;
344 case NFC_PROTO_NFC_DEP:
345 if (rf_tech == NFC_DIGITAL_RF_TECH_106A) {
346 framing = NFC_DIGITAL_FRAMING_NFCA_NFC_DEP;
347 check_crc = digital_skb_check_crc_a;
348 add_crc = digital_skb_add_crc_a;
349 } else {
350 framing = NFC_DIGITAL_FRAMING_NFCF_NFC_DEP;
351 check_crc = digital_skb_check_crc_f;
352 add_crc = digital_skb_add_crc_f;
354 break;
356 case NFC_PROTO_ISO15693:
357 framing = NFC_DIGITAL_FRAMING_ISO15693_T5T;
358 check_crc = digital_skb_check_crc_b;
359 add_crc = digital_skb_add_crc_b;
360 break;
362 case NFC_PROTO_ISO14443:
363 framing = NFC_DIGITAL_FRAMING_NFCA_T4T;
364 check_crc = digital_skb_check_crc_a;
365 add_crc = digital_skb_add_crc_a;
366 break;
368 case NFC_PROTO_ISO14443_B:
369 framing = NFC_DIGITAL_FRAMING_NFCB_T4T;
370 check_crc = digital_skb_check_crc_b;
371 add_crc = digital_skb_add_crc_b;
372 break;
374 default:
375 pr_err("Invalid protocol %d\n", protocol);
376 return -EINVAL;
379 pr_debug("rf_tech=%d, protocol=%d\n", rf_tech, protocol);
381 ddev->curr_rf_tech = rf_tech;
383 if (DIGITAL_DRV_CAPS_IN_CRC(ddev)) {
384 ddev->skb_add_crc = digital_skb_add_crc_none;
385 ddev->skb_check_crc = digital_skb_check_crc_none;
386 } else {
387 ddev->skb_add_crc = add_crc;
388 ddev->skb_check_crc = check_crc;
391 rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING, framing);
392 if (rc)
393 return rc;
395 target->supported_protocols = (1 << protocol);
397 poll_tech_count = ddev->poll_tech_count;
398 ddev->poll_tech_count = 0;
400 rc = nfc_targets_found(ddev->nfc_dev, target, 1);
401 if (rc) {
402 ddev->poll_tech_count = poll_tech_count;
403 return rc;
406 return 0;
409 void digital_poll_next_tech(struct nfc_digital_dev *ddev)
411 u8 rand_mod;
413 digital_switch_rf(ddev, 0);
415 mutex_lock(&ddev->poll_lock);
417 if (!ddev->poll_tech_count) {
418 mutex_unlock(&ddev->poll_lock);
419 return;
422 get_random_bytes(&rand_mod, sizeof(rand_mod));
423 ddev->poll_tech_index = rand_mod % ddev->poll_tech_count;
425 mutex_unlock(&ddev->poll_lock);
427 schedule_delayed_work(&ddev->poll_work,
428 msecs_to_jiffies(DIGITAL_POLL_INTERVAL));
431 static void digital_wq_poll(struct work_struct *work)
433 int rc;
434 struct digital_poll_tech *poll_tech;
435 struct nfc_digital_dev *ddev = container_of(work,
436 struct nfc_digital_dev,
437 poll_work.work);
438 mutex_lock(&ddev->poll_lock);
440 if (!ddev->poll_tech_count) {
441 mutex_unlock(&ddev->poll_lock);
442 return;
445 poll_tech = &ddev->poll_techs[ddev->poll_tech_index];
447 mutex_unlock(&ddev->poll_lock);
449 rc = poll_tech->poll_func(ddev, poll_tech->rf_tech);
450 if (rc)
451 digital_poll_next_tech(ddev);
454 static void digital_add_poll_tech(struct nfc_digital_dev *ddev, u8 rf_tech,
455 digital_poll_t poll_func)
457 struct digital_poll_tech *poll_tech;
459 if (ddev->poll_tech_count >= NFC_DIGITAL_POLL_MODE_COUNT_MAX)
460 return;
462 poll_tech = &ddev->poll_techs[ddev->poll_tech_count++];
464 poll_tech->rf_tech = rf_tech;
465 poll_tech->poll_func = poll_func;
469 * start_poll operation
471 * For every supported protocol, the corresponding polling function is added
472 * to the table of polling technologies (ddev->poll_techs[]) using
473 * digital_add_poll_tech().
474 * When a polling function fails (by timeout or protocol error) the next one is
475 * schedule by digital_poll_next_tech() on the poll workqueue (ddev->poll_work).
477 static int digital_start_poll(struct nfc_dev *nfc_dev, __u32 im_protocols,
478 __u32 tm_protocols)
480 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
481 u32 matching_im_protocols, matching_tm_protocols;
483 pr_debug("protocols: im 0x%x, tm 0x%x, supported 0x%x\n", im_protocols,
484 tm_protocols, ddev->protocols);
486 matching_im_protocols = ddev->protocols & im_protocols;
487 matching_tm_protocols = ddev->protocols & tm_protocols;
489 if (!matching_im_protocols && !matching_tm_protocols) {
490 pr_err("Unknown protocol\n");
491 return -EINVAL;
494 if (ddev->poll_tech_count) {
495 pr_err("Already polling\n");
496 return -EBUSY;
499 if (ddev->curr_protocol) {
500 pr_err("A target is already active\n");
501 return -EBUSY;
504 ddev->poll_tech_count = 0;
505 ddev->poll_tech_index = 0;
507 if (matching_im_protocols & DIGITAL_PROTO_NFCA_RF_TECH)
508 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_106A,
509 digital_in_send_sens_req);
511 if (matching_im_protocols & DIGITAL_PROTO_NFCB_RF_TECH)
512 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_106B,
513 digital_in_send_sensb_req);
515 if (matching_im_protocols & DIGITAL_PROTO_NFCF_RF_TECH) {
516 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_212F,
517 digital_in_send_sensf_req);
519 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_424F,
520 digital_in_send_sensf_req);
523 if (matching_im_protocols & DIGITAL_PROTO_ISO15693_RF_TECH)
524 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_ISO15693,
525 digital_in_send_iso15693_inv_req);
527 if (matching_tm_protocols & NFC_PROTO_NFC_DEP_MASK) {
528 if (ddev->ops->tg_listen_mdaa) {
529 digital_add_poll_tech(ddev, 0,
530 digital_tg_listen_mdaa);
531 } else if (ddev->ops->tg_listen_md) {
532 digital_add_poll_tech(ddev, 0,
533 digital_tg_listen_md);
534 } else {
535 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_106A,
536 digital_tg_listen_nfca);
538 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_212F,
539 digital_tg_listen_nfcf);
541 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_424F,
542 digital_tg_listen_nfcf);
546 if (!ddev->poll_tech_count) {
547 pr_err("Unsupported protocols: im=0x%x, tm=0x%x\n",
548 matching_im_protocols, matching_tm_protocols);
549 return -EINVAL;
552 schedule_delayed_work(&ddev->poll_work, 0);
554 return 0;
557 static void digital_stop_poll(struct nfc_dev *nfc_dev)
559 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
561 mutex_lock(&ddev->poll_lock);
563 if (!ddev->poll_tech_count) {
564 pr_err("Polling operation was not running\n");
565 mutex_unlock(&ddev->poll_lock);
566 return;
569 ddev->poll_tech_count = 0;
571 mutex_unlock(&ddev->poll_lock);
573 cancel_delayed_work_sync(&ddev->poll_work);
575 digital_abort_cmd(ddev);
578 static int digital_dev_up(struct nfc_dev *nfc_dev)
580 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
582 digital_switch_rf(ddev, 1);
584 return 0;
587 static int digital_dev_down(struct nfc_dev *nfc_dev)
589 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
591 digital_switch_rf(ddev, 0);
593 return 0;
596 static int digital_dep_link_up(struct nfc_dev *nfc_dev,
597 struct nfc_target *target,
598 __u8 comm_mode, __u8 *gb, size_t gb_len)
600 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
601 int rc;
603 rc = digital_in_send_atr_req(ddev, target, comm_mode, gb, gb_len);
605 if (!rc)
606 ddev->curr_protocol = NFC_PROTO_NFC_DEP;
608 return rc;
611 static int digital_dep_link_down(struct nfc_dev *nfc_dev)
613 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
615 digital_abort_cmd(ddev);
617 ddev->curr_protocol = 0;
619 return 0;
622 static int digital_activate_target(struct nfc_dev *nfc_dev,
623 struct nfc_target *target, __u32 protocol)
625 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
627 if (ddev->poll_tech_count) {
628 pr_err("Can't activate a target while polling\n");
629 return -EBUSY;
632 if (ddev->curr_protocol) {
633 pr_err("A target is already active\n");
634 return -EBUSY;
637 ddev->curr_protocol = protocol;
639 return 0;
642 static void digital_deactivate_target(struct nfc_dev *nfc_dev,
643 struct nfc_target *target,
644 u8 mode)
646 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
648 if (!ddev->curr_protocol) {
649 pr_err("No active target\n");
650 return;
653 digital_abort_cmd(ddev);
654 ddev->curr_protocol = 0;
657 static int digital_tg_send(struct nfc_dev *dev, struct sk_buff *skb)
659 struct nfc_digital_dev *ddev = nfc_get_drvdata(dev);
661 return digital_tg_send_dep_res(ddev, skb);
664 static void digital_in_send_complete(struct nfc_digital_dev *ddev, void *arg,
665 struct sk_buff *resp)
667 struct digital_data_exch *data_exch = arg;
668 int rc;
670 if (IS_ERR(resp)) {
671 rc = PTR_ERR(resp);
672 resp = NULL;
673 goto done;
676 if (ddev->curr_protocol == NFC_PROTO_MIFARE) {
677 rc = digital_in_recv_mifare_res(resp);
678 /* crc check is done in digital_in_recv_mifare_res() */
679 goto done;
682 if ((ddev->curr_protocol == NFC_PROTO_ISO14443) ||
683 (ddev->curr_protocol == NFC_PROTO_ISO14443_B)) {
684 rc = digital_in_iso_dep_pull_sod(ddev, resp);
685 if (rc)
686 goto done;
689 rc = ddev->skb_check_crc(resp);
691 done:
692 if (rc) {
693 kfree_skb(resp);
694 resp = NULL;
697 data_exch->cb(data_exch->cb_context, resp, rc);
699 kfree(data_exch);
702 static int digital_in_send(struct nfc_dev *nfc_dev, struct nfc_target *target,
703 struct sk_buff *skb, data_exchange_cb_t cb,
704 void *cb_context)
706 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
707 struct digital_data_exch *data_exch;
708 int rc;
710 data_exch = kzalloc(sizeof(*data_exch), GFP_KERNEL);
711 if (!data_exch)
712 return -ENOMEM;
714 data_exch->cb = cb;
715 data_exch->cb_context = cb_context;
717 if (ddev->curr_protocol == NFC_PROTO_NFC_DEP) {
718 rc = digital_in_send_dep_req(ddev, target, skb, data_exch);
719 goto exit;
722 if ((ddev->curr_protocol == NFC_PROTO_ISO14443) ||
723 (ddev->curr_protocol == NFC_PROTO_ISO14443_B)) {
724 rc = digital_in_iso_dep_push_sod(ddev, skb);
725 if (rc)
726 goto exit;
729 ddev->skb_add_crc(skb);
731 rc = digital_in_send_cmd(ddev, skb, 500, digital_in_send_complete,
732 data_exch);
734 exit:
735 if (rc)
736 kfree(data_exch);
738 return rc;
741 static struct nfc_ops digital_nfc_ops = {
742 .dev_up = digital_dev_up,
743 .dev_down = digital_dev_down,
744 .start_poll = digital_start_poll,
745 .stop_poll = digital_stop_poll,
746 .dep_link_up = digital_dep_link_up,
747 .dep_link_down = digital_dep_link_down,
748 .activate_target = digital_activate_target,
749 .deactivate_target = digital_deactivate_target,
750 .tm_send = digital_tg_send,
751 .im_transceive = digital_in_send,
754 struct nfc_digital_dev *nfc_digital_allocate_device(struct nfc_digital_ops *ops,
755 __u32 supported_protocols,
756 __u32 driver_capabilities,
757 int tx_headroom, int tx_tailroom)
759 struct nfc_digital_dev *ddev;
761 if (!ops->in_configure_hw || !ops->in_send_cmd || !ops->tg_listen ||
762 !ops->tg_configure_hw || !ops->tg_send_cmd || !ops->abort_cmd ||
763 !ops->switch_rf || (ops->tg_listen_md && !ops->tg_get_rf_tech))
764 return NULL;
766 ddev = kzalloc(sizeof(*ddev), GFP_KERNEL);
767 if (!ddev)
768 return NULL;
770 ddev->driver_capabilities = driver_capabilities;
771 ddev->ops = ops;
773 mutex_init(&ddev->cmd_lock);
774 INIT_LIST_HEAD(&ddev->cmd_queue);
776 INIT_WORK(&ddev->cmd_work, digital_wq_cmd);
777 INIT_WORK(&ddev->cmd_complete_work, digital_wq_cmd_complete);
779 mutex_init(&ddev->poll_lock);
780 INIT_DELAYED_WORK(&ddev->poll_work, digital_wq_poll);
782 if (supported_protocols & NFC_PROTO_JEWEL_MASK)
783 ddev->protocols |= NFC_PROTO_JEWEL_MASK;
784 if (supported_protocols & NFC_PROTO_MIFARE_MASK)
785 ddev->protocols |= NFC_PROTO_MIFARE_MASK;
786 if (supported_protocols & NFC_PROTO_FELICA_MASK)
787 ddev->protocols |= NFC_PROTO_FELICA_MASK;
788 if (supported_protocols & NFC_PROTO_NFC_DEP_MASK)
789 ddev->protocols |= NFC_PROTO_NFC_DEP_MASK;
790 if (supported_protocols & NFC_PROTO_ISO15693_MASK)
791 ddev->protocols |= NFC_PROTO_ISO15693_MASK;
792 if (supported_protocols & NFC_PROTO_ISO14443_MASK)
793 ddev->protocols |= NFC_PROTO_ISO14443_MASK;
794 if (supported_protocols & NFC_PROTO_ISO14443_B_MASK)
795 ddev->protocols |= NFC_PROTO_ISO14443_B_MASK;
797 ddev->tx_headroom = tx_headroom + DIGITAL_MAX_HEADER_LEN;
798 ddev->tx_tailroom = tx_tailroom + DIGITAL_CRC_LEN;
800 ddev->nfc_dev = nfc_allocate_device(&digital_nfc_ops, ddev->protocols,
801 ddev->tx_headroom,
802 ddev->tx_tailroom);
803 if (!ddev->nfc_dev) {
804 pr_err("nfc_allocate_device failed\n");
805 goto free_dev;
808 nfc_set_drvdata(ddev->nfc_dev, ddev);
810 return ddev;
812 free_dev:
813 kfree(ddev);
815 return NULL;
817 EXPORT_SYMBOL(nfc_digital_allocate_device);
819 void nfc_digital_free_device(struct nfc_digital_dev *ddev)
821 nfc_free_device(ddev->nfc_dev);
822 kfree(ddev);
824 EXPORT_SYMBOL(nfc_digital_free_device);
826 int nfc_digital_register_device(struct nfc_digital_dev *ddev)
828 return nfc_register_device(ddev->nfc_dev);
830 EXPORT_SYMBOL(nfc_digital_register_device);
832 void nfc_digital_unregister_device(struct nfc_digital_dev *ddev)
834 struct digital_cmd *cmd, *n;
836 nfc_unregister_device(ddev->nfc_dev);
838 mutex_lock(&ddev->poll_lock);
839 ddev->poll_tech_count = 0;
840 mutex_unlock(&ddev->poll_lock);
842 cancel_delayed_work_sync(&ddev->poll_work);
843 cancel_work_sync(&ddev->cmd_work);
844 cancel_work_sync(&ddev->cmd_complete_work);
846 list_for_each_entry_safe(cmd, n, &ddev->cmd_queue, queue) {
847 list_del(&cmd->queue);
849 /* Call the command callback if any and pass it a ENODEV error.
850 * This gives a chance to the command issuer to free any
851 * allocated buffer.
853 if (cmd->cmd_cb)
854 cmd->cmd_cb(ddev, cmd->cb_context, ERR_PTR(-ENODEV));
856 kfree(cmd->mdaa_params);
857 kfree(cmd);
860 EXPORT_SYMBOL(nfc_digital_unregister_device);
862 MODULE_LICENSE("GPL");