drm/tests: hdmi: Fix memory leaks in drm_display_mode_from_cea_vic()
[drm/drm-misc.git] / net / nfc / digital_core.c
blobdae378f1d52b607abf1cd0931473b28a390f1949
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * NFC Digital Protocol stack
4 * Copyright (c) 2013, Intel Corporation.
5 */
7 #define pr_fmt(fmt) "digital: %s: " fmt, __func__
9 #include <linux/module.h>
11 #include "digital.h"
13 #define DIGITAL_PROTO_NFCA_RF_TECH \
14 (NFC_PROTO_JEWEL_MASK | NFC_PROTO_MIFARE_MASK | \
15 NFC_PROTO_NFC_DEP_MASK | NFC_PROTO_ISO14443_MASK)
17 #define DIGITAL_PROTO_NFCB_RF_TECH NFC_PROTO_ISO14443_B_MASK
19 #define DIGITAL_PROTO_NFCF_RF_TECH \
20 (NFC_PROTO_FELICA_MASK | NFC_PROTO_NFC_DEP_MASK)
22 #define DIGITAL_PROTO_ISO15693_RF_TECH NFC_PROTO_ISO15693_MASK
24 /* Delay between each poll frame (ms) */
25 #define DIGITAL_POLL_INTERVAL 10
27 struct digital_cmd {
28 struct list_head queue;
30 u8 type;
31 u8 pending;
33 u16 timeout;
34 struct sk_buff *req;
35 struct sk_buff *resp;
36 struct digital_tg_mdaa_params *mdaa_params;
38 nfc_digital_cmd_complete_t cmd_cb;
39 void *cb_context;
42 struct sk_buff *digital_skb_alloc(struct nfc_digital_dev *ddev,
43 unsigned int len)
45 struct sk_buff *skb;
47 skb = alloc_skb(len + ddev->tx_headroom + ddev->tx_tailroom,
48 GFP_KERNEL);
49 if (skb)
50 skb_reserve(skb, ddev->tx_headroom);
52 return skb;
55 void digital_skb_add_crc(struct sk_buff *skb, crc_func_t crc_func, u16 init,
56 u8 bitwise_inv, u8 msb_first)
58 u16 crc;
60 crc = crc_func(init, skb->data, skb->len);
62 if (bitwise_inv)
63 crc = ~crc;
65 if (msb_first)
66 crc = __fswab16(crc);
68 skb_put_u8(skb, crc & 0xFF);
69 skb_put_u8(skb, (crc >> 8) & 0xFF);
72 int digital_skb_check_crc(struct sk_buff *skb, crc_func_t crc_func,
73 u16 crc_init, u8 bitwise_inv, u8 msb_first)
75 int rc;
76 u16 crc;
78 if (skb->len <= 2)
79 return -EIO;
81 crc = crc_func(crc_init, skb->data, skb->len - 2);
83 if (bitwise_inv)
84 crc = ~crc;
86 if (msb_first)
87 crc = __swab16(crc);
89 rc = (skb->data[skb->len - 2] - (crc & 0xFF)) +
90 (skb->data[skb->len - 1] - ((crc >> 8) & 0xFF));
92 if (rc)
93 return -EIO;
95 skb_trim(skb, skb->len - 2);
97 return 0;
100 static inline void digital_switch_rf(struct nfc_digital_dev *ddev, bool on)
102 ddev->ops->switch_rf(ddev, on);
105 static inline void digital_abort_cmd(struct nfc_digital_dev *ddev)
107 ddev->ops->abort_cmd(ddev);
110 static void digital_wq_cmd_complete(struct work_struct *work)
112 struct digital_cmd *cmd;
113 struct nfc_digital_dev *ddev = container_of(work,
114 struct nfc_digital_dev,
115 cmd_complete_work);
117 mutex_lock(&ddev->cmd_lock);
119 cmd = list_first_entry_or_null(&ddev->cmd_queue, struct digital_cmd,
120 queue);
121 if (!cmd) {
122 mutex_unlock(&ddev->cmd_lock);
123 return;
126 list_del(&cmd->queue);
128 mutex_unlock(&ddev->cmd_lock);
130 if (!IS_ERR(cmd->resp))
131 print_hex_dump_debug("DIGITAL RX: ", DUMP_PREFIX_NONE, 16, 1,
132 cmd->resp->data, cmd->resp->len, false);
134 cmd->cmd_cb(ddev, cmd->cb_context, cmd->resp);
136 kfree(cmd->mdaa_params);
137 kfree(cmd);
139 schedule_work(&ddev->cmd_work);
142 static void digital_send_cmd_complete(struct nfc_digital_dev *ddev,
143 void *arg, struct sk_buff *resp)
145 struct digital_cmd *cmd = arg;
147 cmd->resp = resp;
149 schedule_work(&ddev->cmd_complete_work);
152 static void digital_wq_cmd(struct work_struct *work)
154 int rc;
155 struct digital_cmd *cmd;
156 struct digital_tg_mdaa_params *params;
157 struct nfc_digital_dev *ddev = container_of(work,
158 struct nfc_digital_dev,
159 cmd_work);
161 mutex_lock(&ddev->cmd_lock);
163 cmd = list_first_entry_or_null(&ddev->cmd_queue, struct digital_cmd,
164 queue);
165 if (!cmd || cmd->pending) {
166 mutex_unlock(&ddev->cmd_lock);
167 return;
170 cmd->pending = 1;
172 mutex_unlock(&ddev->cmd_lock);
174 if (cmd->req)
175 print_hex_dump_debug("DIGITAL TX: ", DUMP_PREFIX_NONE, 16, 1,
176 cmd->req->data, cmd->req->len, false);
178 switch (cmd->type) {
179 case DIGITAL_CMD_IN_SEND:
180 rc = ddev->ops->in_send_cmd(ddev, cmd->req, cmd->timeout,
181 digital_send_cmd_complete, cmd);
182 break;
184 case DIGITAL_CMD_TG_SEND:
185 rc = ddev->ops->tg_send_cmd(ddev, cmd->req, cmd->timeout,
186 digital_send_cmd_complete, cmd);
187 break;
189 case DIGITAL_CMD_TG_LISTEN:
190 rc = ddev->ops->tg_listen(ddev, cmd->timeout,
191 digital_send_cmd_complete, cmd);
192 break;
194 case DIGITAL_CMD_TG_LISTEN_MDAA:
195 params = cmd->mdaa_params;
197 rc = ddev->ops->tg_listen_mdaa(ddev, params, cmd->timeout,
198 digital_send_cmd_complete, cmd);
199 break;
201 case DIGITAL_CMD_TG_LISTEN_MD:
202 rc = ddev->ops->tg_listen_md(ddev, cmd->timeout,
203 digital_send_cmd_complete, cmd);
204 break;
206 default:
207 pr_err("Unknown cmd type %d\n", cmd->type);
208 return;
211 if (!rc)
212 return;
214 pr_err("in_send_command returned err %d\n", rc);
216 mutex_lock(&ddev->cmd_lock);
217 list_del(&cmd->queue);
218 mutex_unlock(&ddev->cmd_lock);
220 kfree_skb(cmd->req);
221 kfree(cmd->mdaa_params);
222 kfree(cmd);
224 schedule_work(&ddev->cmd_work);
227 int digital_send_cmd(struct nfc_digital_dev *ddev, u8 cmd_type,
228 struct sk_buff *skb, struct digital_tg_mdaa_params *params,
229 u16 timeout, nfc_digital_cmd_complete_t cmd_cb,
230 void *cb_context)
232 struct digital_cmd *cmd;
234 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
235 if (!cmd)
236 return -ENOMEM;
238 cmd->type = cmd_type;
239 cmd->timeout = timeout;
240 cmd->req = skb;
241 cmd->mdaa_params = params;
242 cmd->cmd_cb = cmd_cb;
243 cmd->cb_context = cb_context;
244 INIT_LIST_HEAD(&cmd->queue);
246 mutex_lock(&ddev->cmd_lock);
247 list_add_tail(&cmd->queue, &ddev->cmd_queue);
248 mutex_unlock(&ddev->cmd_lock);
250 schedule_work(&ddev->cmd_work);
252 return 0;
255 int digital_in_configure_hw(struct nfc_digital_dev *ddev, int type, int param)
257 int rc;
259 rc = ddev->ops->in_configure_hw(ddev, type, param);
260 if (rc)
261 pr_err("in_configure_hw failed: %d\n", rc);
263 return rc;
266 int digital_tg_configure_hw(struct nfc_digital_dev *ddev, int type, int param)
268 int rc;
270 rc = ddev->ops->tg_configure_hw(ddev, type, param);
271 if (rc)
272 pr_err("tg_configure_hw failed: %d\n", rc);
274 return rc;
277 static int digital_tg_listen_mdaa(struct nfc_digital_dev *ddev, u8 rf_tech)
279 struct digital_tg_mdaa_params *params;
280 int rc;
282 params = kzalloc(sizeof(*params), GFP_KERNEL);
283 if (!params)
284 return -ENOMEM;
286 params->sens_res = DIGITAL_SENS_RES_NFC_DEP;
287 get_random_bytes(params->nfcid1, sizeof(params->nfcid1));
288 params->sel_res = DIGITAL_SEL_RES_NFC_DEP;
290 params->nfcid2[0] = DIGITAL_SENSF_NFCID2_NFC_DEP_B1;
291 params->nfcid2[1] = DIGITAL_SENSF_NFCID2_NFC_DEP_B2;
292 get_random_bytes(params->nfcid2 + 2, NFC_NFCID2_MAXSIZE - 2);
293 params->sc = DIGITAL_SENSF_FELICA_SC;
295 rc = digital_send_cmd(ddev, DIGITAL_CMD_TG_LISTEN_MDAA, NULL, params,
296 500, digital_tg_recv_atr_req, NULL);
297 if (rc)
298 kfree(params);
300 return rc;
303 static int digital_tg_listen_md(struct nfc_digital_dev *ddev, u8 rf_tech)
305 return digital_send_cmd(ddev, DIGITAL_CMD_TG_LISTEN_MD, NULL, NULL, 500,
306 digital_tg_recv_md_req, NULL);
309 int digital_target_found(struct nfc_digital_dev *ddev,
310 struct nfc_target *target, u8 protocol)
312 int rc;
313 u8 framing;
314 u8 rf_tech;
315 u8 poll_tech_count;
316 int (*check_crc)(struct sk_buff *skb);
317 void (*add_crc)(struct sk_buff *skb);
319 rf_tech = ddev->poll_techs[ddev->poll_tech_index].rf_tech;
321 switch (protocol) {
322 case NFC_PROTO_JEWEL:
323 framing = NFC_DIGITAL_FRAMING_NFCA_T1T;
324 check_crc = digital_skb_check_crc_b;
325 add_crc = digital_skb_add_crc_b;
326 break;
328 case NFC_PROTO_MIFARE:
329 framing = NFC_DIGITAL_FRAMING_NFCA_T2T;
330 check_crc = digital_skb_check_crc_a;
331 add_crc = digital_skb_add_crc_a;
332 break;
334 case NFC_PROTO_FELICA:
335 framing = NFC_DIGITAL_FRAMING_NFCF_T3T;
336 check_crc = digital_skb_check_crc_f;
337 add_crc = digital_skb_add_crc_f;
338 break;
340 case NFC_PROTO_NFC_DEP:
341 if (rf_tech == NFC_DIGITAL_RF_TECH_106A) {
342 framing = NFC_DIGITAL_FRAMING_NFCA_NFC_DEP;
343 check_crc = digital_skb_check_crc_a;
344 add_crc = digital_skb_add_crc_a;
345 } else {
346 framing = NFC_DIGITAL_FRAMING_NFCF_NFC_DEP;
347 check_crc = digital_skb_check_crc_f;
348 add_crc = digital_skb_add_crc_f;
350 break;
352 case NFC_PROTO_ISO15693:
353 framing = NFC_DIGITAL_FRAMING_ISO15693_T5T;
354 check_crc = digital_skb_check_crc_b;
355 add_crc = digital_skb_add_crc_b;
356 break;
358 case NFC_PROTO_ISO14443:
359 framing = NFC_DIGITAL_FRAMING_NFCA_T4T;
360 check_crc = digital_skb_check_crc_a;
361 add_crc = digital_skb_add_crc_a;
362 break;
364 case NFC_PROTO_ISO14443_B:
365 framing = NFC_DIGITAL_FRAMING_NFCB_T4T;
366 check_crc = digital_skb_check_crc_b;
367 add_crc = digital_skb_add_crc_b;
368 break;
370 default:
371 pr_err("Invalid protocol %d\n", protocol);
372 return -EINVAL;
375 pr_debug("rf_tech=%d, protocol=%d\n", rf_tech, protocol);
377 ddev->curr_rf_tech = rf_tech;
379 if (DIGITAL_DRV_CAPS_IN_CRC(ddev)) {
380 ddev->skb_add_crc = digital_skb_add_crc_none;
381 ddev->skb_check_crc = digital_skb_check_crc_none;
382 } else {
383 ddev->skb_add_crc = add_crc;
384 ddev->skb_check_crc = check_crc;
387 rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING, framing);
388 if (rc)
389 return rc;
391 target->supported_protocols = (1 << protocol);
393 poll_tech_count = ddev->poll_tech_count;
394 ddev->poll_tech_count = 0;
396 rc = nfc_targets_found(ddev->nfc_dev, target, 1);
397 if (rc) {
398 ddev->poll_tech_count = poll_tech_count;
399 return rc;
402 return 0;
405 void digital_poll_next_tech(struct nfc_digital_dev *ddev)
407 u8 rand_mod;
409 digital_switch_rf(ddev, 0);
411 mutex_lock(&ddev->poll_lock);
413 if (!ddev->poll_tech_count) {
414 mutex_unlock(&ddev->poll_lock);
415 return;
418 get_random_bytes(&rand_mod, sizeof(rand_mod));
419 ddev->poll_tech_index = rand_mod % ddev->poll_tech_count;
421 mutex_unlock(&ddev->poll_lock);
423 schedule_delayed_work(&ddev->poll_work,
424 msecs_to_jiffies(DIGITAL_POLL_INTERVAL));
427 static void digital_wq_poll(struct work_struct *work)
429 int rc;
430 struct digital_poll_tech *poll_tech;
431 struct nfc_digital_dev *ddev = container_of(work,
432 struct nfc_digital_dev,
433 poll_work.work);
434 mutex_lock(&ddev->poll_lock);
436 if (!ddev->poll_tech_count) {
437 mutex_unlock(&ddev->poll_lock);
438 return;
441 poll_tech = &ddev->poll_techs[ddev->poll_tech_index];
443 mutex_unlock(&ddev->poll_lock);
445 rc = poll_tech->poll_func(ddev, poll_tech->rf_tech);
446 if (rc)
447 digital_poll_next_tech(ddev);
450 static void digital_add_poll_tech(struct nfc_digital_dev *ddev, u8 rf_tech,
451 digital_poll_t poll_func)
453 struct digital_poll_tech *poll_tech;
455 if (ddev->poll_tech_count >= NFC_DIGITAL_POLL_MODE_COUNT_MAX)
456 return;
458 poll_tech = &ddev->poll_techs[ddev->poll_tech_count++];
460 poll_tech->rf_tech = rf_tech;
461 poll_tech->poll_func = poll_func;
465 * digital_start_poll - start_poll operation
466 * @nfc_dev: device to be polled
467 * @im_protocols: bitset of nfc initiator protocols to be used for polling
468 * @tm_protocols: bitset of nfc transport protocols to be used for polling
470 * For every supported protocol, the corresponding polling function is added
471 * to the table of polling technologies (ddev->poll_techs[]) using
472 * digital_add_poll_tech().
473 * When a polling function fails (by timeout or protocol error) the next one is
474 * schedule by digital_poll_next_tech() on the poll workqueue (ddev->poll_work).
476 static int digital_start_poll(struct nfc_dev *nfc_dev, __u32 im_protocols,
477 __u32 tm_protocols)
479 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
480 u32 matching_im_protocols, matching_tm_protocols;
482 pr_debug("protocols: im 0x%x, tm 0x%x, supported 0x%x\n", im_protocols,
483 tm_protocols, ddev->protocols);
485 matching_im_protocols = ddev->protocols & im_protocols;
486 matching_tm_protocols = ddev->protocols & tm_protocols;
488 if (!matching_im_protocols && !matching_tm_protocols) {
489 pr_err("Unknown protocol\n");
490 return -EINVAL;
493 if (ddev->poll_tech_count) {
494 pr_err("Already polling\n");
495 return -EBUSY;
498 if (ddev->curr_protocol) {
499 pr_err("A target is already active\n");
500 return -EBUSY;
503 ddev->poll_tech_count = 0;
504 ddev->poll_tech_index = 0;
506 if (matching_im_protocols & DIGITAL_PROTO_NFCA_RF_TECH)
507 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_106A,
508 digital_in_send_sens_req);
510 if (matching_im_protocols & DIGITAL_PROTO_NFCB_RF_TECH)
511 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_106B,
512 digital_in_send_sensb_req);
514 if (matching_im_protocols & DIGITAL_PROTO_NFCF_RF_TECH) {
515 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_212F,
516 digital_in_send_sensf_req);
518 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_424F,
519 digital_in_send_sensf_req);
522 if (matching_im_protocols & DIGITAL_PROTO_ISO15693_RF_TECH)
523 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_ISO15693,
524 digital_in_send_iso15693_inv_req);
526 if (matching_tm_protocols & NFC_PROTO_NFC_DEP_MASK) {
527 if (ddev->ops->tg_listen_mdaa) {
528 digital_add_poll_tech(ddev, 0,
529 digital_tg_listen_mdaa);
530 } else if (ddev->ops->tg_listen_md) {
531 digital_add_poll_tech(ddev, 0,
532 digital_tg_listen_md);
533 } else {
534 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_106A,
535 digital_tg_listen_nfca);
537 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_212F,
538 digital_tg_listen_nfcf);
540 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_424F,
541 digital_tg_listen_nfcf);
545 if (!ddev->poll_tech_count) {
546 pr_err("Unsupported protocols: im=0x%x, tm=0x%x\n",
547 matching_im_protocols, matching_tm_protocols);
548 return -EINVAL;
551 schedule_delayed_work(&ddev->poll_work, 0);
553 return 0;
556 static void digital_stop_poll(struct nfc_dev *nfc_dev)
558 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
560 mutex_lock(&ddev->poll_lock);
562 if (!ddev->poll_tech_count) {
563 pr_err("Polling operation was not running\n");
564 mutex_unlock(&ddev->poll_lock);
565 return;
568 ddev->poll_tech_count = 0;
570 mutex_unlock(&ddev->poll_lock);
572 cancel_delayed_work_sync(&ddev->poll_work);
574 digital_abort_cmd(ddev);
577 static int digital_dev_up(struct nfc_dev *nfc_dev)
579 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
581 digital_switch_rf(ddev, 1);
583 return 0;
586 static int digital_dev_down(struct nfc_dev *nfc_dev)
588 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
590 digital_switch_rf(ddev, 0);
592 return 0;
595 static int digital_dep_link_up(struct nfc_dev *nfc_dev,
596 struct nfc_target *target,
597 __u8 comm_mode, __u8 *gb, size_t gb_len)
599 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
600 int rc;
602 rc = digital_in_send_atr_req(ddev, target, comm_mode, gb, gb_len);
604 if (!rc)
605 ddev->curr_protocol = NFC_PROTO_NFC_DEP;
607 return rc;
610 static int digital_dep_link_down(struct nfc_dev *nfc_dev)
612 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
614 digital_abort_cmd(ddev);
616 ddev->curr_protocol = 0;
618 return 0;
621 static int digital_activate_target(struct nfc_dev *nfc_dev,
622 struct nfc_target *target, __u32 protocol)
624 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
626 if (ddev->poll_tech_count) {
627 pr_err("Can't activate a target while polling\n");
628 return -EBUSY;
631 if (ddev->curr_protocol) {
632 pr_err("A target is already active\n");
633 return -EBUSY;
636 ddev->curr_protocol = protocol;
638 return 0;
641 static void digital_deactivate_target(struct nfc_dev *nfc_dev,
642 struct nfc_target *target,
643 u8 mode)
645 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
647 if (!ddev->curr_protocol) {
648 pr_err("No active target\n");
649 return;
652 digital_abort_cmd(ddev);
653 ddev->curr_protocol = 0;
656 static int digital_tg_send(struct nfc_dev *dev, struct sk_buff *skb)
658 struct nfc_digital_dev *ddev = nfc_get_drvdata(dev);
660 return digital_tg_send_dep_res(ddev, skb);
663 static void digital_in_send_complete(struct nfc_digital_dev *ddev, void *arg,
664 struct sk_buff *resp)
666 struct digital_data_exch *data_exch = arg;
667 int rc;
669 if (IS_ERR(resp)) {
670 rc = PTR_ERR(resp);
671 resp = NULL;
672 goto done;
675 if (ddev->curr_protocol == NFC_PROTO_MIFARE) {
676 rc = digital_in_recv_mifare_res(resp);
677 /* crc check is done in digital_in_recv_mifare_res() */
678 goto done;
681 if ((ddev->curr_protocol == NFC_PROTO_ISO14443) ||
682 (ddev->curr_protocol == NFC_PROTO_ISO14443_B)) {
683 rc = digital_in_iso_dep_pull_sod(ddev, resp);
684 if (rc)
685 goto done;
688 rc = ddev->skb_check_crc(resp);
690 done:
691 if (rc) {
692 kfree_skb(resp);
693 resp = NULL;
696 data_exch->cb(data_exch->cb_context, resp, rc);
698 kfree(data_exch);
701 static int digital_in_send(struct nfc_dev *nfc_dev, struct nfc_target *target,
702 struct sk_buff *skb, data_exchange_cb_t cb,
703 void *cb_context)
705 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
706 struct digital_data_exch *data_exch;
707 int rc;
709 data_exch = kzalloc(sizeof(*data_exch), GFP_KERNEL);
710 if (!data_exch)
711 return -ENOMEM;
713 data_exch->cb = cb;
714 data_exch->cb_context = cb_context;
716 if (ddev->curr_protocol == NFC_PROTO_NFC_DEP) {
717 rc = digital_in_send_dep_req(ddev, target, skb, data_exch);
718 goto exit;
721 if ((ddev->curr_protocol == NFC_PROTO_ISO14443) ||
722 (ddev->curr_protocol == NFC_PROTO_ISO14443_B)) {
723 rc = digital_in_iso_dep_push_sod(ddev, skb);
724 if (rc)
725 goto exit;
728 ddev->skb_add_crc(skb);
730 rc = digital_in_send_cmd(ddev, skb, 500, digital_in_send_complete,
731 data_exch);
733 exit:
734 if (rc)
735 kfree(data_exch);
737 return rc;
740 static const struct nfc_ops digital_nfc_ops = {
741 .dev_up = digital_dev_up,
742 .dev_down = digital_dev_down,
743 .start_poll = digital_start_poll,
744 .stop_poll = digital_stop_poll,
745 .dep_link_up = digital_dep_link_up,
746 .dep_link_down = digital_dep_link_down,
747 .activate_target = digital_activate_target,
748 .deactivate_target = digital_deactivate_target,
749 .tm_send = digital_tg_send,
750 .im_transceive = digital_in_send,
753 struct nfc_digital_dev *nfc_digital_allocate_device(const struct nfc_digital_ops *ops,
754 __u32 supported_protocols,
755 __u32 driver_capabilities,
756 int tx_headroom, int tx_tailroom)
758 struct nfc_digital_dev *ddev;
760 if (!ops->in_configure_hw || !ops->in_send_cmd || !ops->tg_listen ||
761 !ops->tg_configure_hw || !ops->tg_send_cmd || !ops->abort_cmd ||
762 !ops->switch_rf || (ops->tg_listen_md && !ops->tg_get_rf_tech))
763 return NULL;
765 ddev = kzalloc(sizeof(*ddev), GFP_KERNEL);
766 if (!ddev)
767 return NULL;
769 ddev->driver_capabilities = driver_capabilities;
770 ddev->ops = ops;
772 mutex_init(&ddev->cmd_lock);
773 INIT_LIST_HEAD(&ddev->cmd_queue);
775 INIT_WORK(&ddev->cmd_work, digital_wq_cmd);
776 INIT_WORK(&ddev->cmd_complete_work, digital_wq_cmd_complete);
778 mutex_init(&ddev->poll_lock);
779 INIT_DELAYED_WORK(&ddev->poll_work, digital_wq_poll);
781 if (supported_protocols & NFC_PROTO_JEWEL_MASK)
782 ddev->protocols |= NFC_PROTO_JEWEL_MASK;
783 if (supported_protocols & NFC_PROTO_MIFARE_MASK)
784 ddev->protocols |= NFC_PROTO_MIFARE_MASK;
785 if (supported_protocols & NFC_PROTO_FELICA_MASK)
786 ddev->protocols |= NFC_PROTO_FELICA_MASK;
787 if (supported_protocols & NFC_PROTO_NFC_DEP_MASK)
788 ddev->protocols |= NFC_PROTO_NFC_DEP_MASK;
789 if (supported_protocols & NFC_PROTO_ISO15693_MASK)
790 ddev->protocols |= NFC_PROTO_ISO15693_MASK;
791 if (supported_protocols & NFC_PROTO_ISO14443_MASK)
792 ddev->protocols |= NFC_PROTO_ISO14443_MASK;
793 if (supported_protocols & NFC_PROTO_ISO14443_B_MASK)
794 ddev->protocols |= NFC_PROTO_ISO14443_B_MASK;
796 ddev->tx_headroom = tx_headroom + DIGITAL_MAX_HEADER_LEN;
797 ddev->tx_tailroom = tx_tailroom + DIGITAL_CRC_LEN;
799 ddev->nfc_dev = nfc_allocate_device(&digital_nfc_ops, ddev->protocols,
800 ddev->tx_headroom,
801 ddev->tx_tailroom);
802 if (!ddev->nfc_dev) {
803 pr_err("nfc_allocate_device failed\n");
804 goto free_dev;
807 nfc_set_drvdata(ddev->nfc_dev, ddev);
809 return ddev;
811 free_dev:
812 kfree(ddev);
814 return NULL;
816 EXPORT_SYMBOL(nfc_digital_allocate_device);
818 void nfc_digital_free_device(struct nfc_digital_dev *ddev)
820 nfc_free_device(ddev->nfc_dev);
821 kfree(ddev);
823 EXPORT_SYMBOL(nfc_digital_free_device);
825 int nfc_digital_register_device(struct nfc_digital_dev *ddev)
827 return nfc_register_device(ddev->nfc_dev);
829 EXPORT_SYMBOL(nfc_digital_register_device);
831 void nfc_digital_unregister_device(struct nfc_digital_dev *ddev)
833 struct digital_cmd *cmd, *n;
835 nfc_unregister_device(ddev->nfc_dev);
837 mutex_lock(&ddev->poll_lock);
838 ddev->poll_tech_count = 0;
839 mutex_unlock(&ddev->poll_lock);
841 cancel_delayed_work_sync(&ddev->poll_work);
842 cancel_work_sync(&ddev->cmd_work);
843 cancel_work_sync(&ddev->cmd_complete_work);
845 list_for_each_entry_safe(cmd, n, &ddev->cmd_queue, queue) {
846 list_del(&cmd->queue);
848 /* Call the command callback if any and pass it a ENODEV error.
849 * This gives a chance to the command issuer to free any
850 * allocated buffer.
852 if (cmd->cmd_cb)
853 cmd->cmd_cb(ddev, cmd->cb_context, ERR_PTR(-ENODEV));
855 kfree(cmd->mdaa_params);
856 kfree(cmd);
859 EXPORT_SYMBOL(nfc_digital_unregister_device);
861 MODULE_DESCRIPTION("NFC Digital protocol stack");
862 MODULE_LICENSE("GPL");