Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 1 | /* |
| 2 | * NCI based Driver for STMicroelectronics NFC Chip |
| 3 | * |
| 4 | * Copyright (C) 2014 STMicroelectronics SAS. All rights reserved. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms and conditions of the GNU General Public License, |
| 8 | * version 2, as published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/nfc.h> |
| 21 | #include <net/nfc/nci.h> |
| 22 | #include <net/nfc/nci_core.h> |
| 23 | |
| 24 | #include "st21nfcb.h" |
Christophe Ricard | 8ae01f7 | 2015-02-01 22:26:15 +0100 | [diff] [blame] | 25 | #include "st21nfcb_se.h" |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 26 | |
| 27 | #define DRIVER_DESC "NCI NFC driver for ST21NFCB" |
| 28 | |
Christophe Ricard | 9e87f9a | 2014-09-13 10:28:49 +0200 | [diff] [blame] | 29 | #define ST21NFCB_NCI1_X_PROPRIETARY_ISO15693 0x83 |
| 30 | |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 31 | static int st21nfcb_nci_open(struct nci_dev *ndev) |
| 32 | { |
| 33 | struct st21nfcb_nci_info *info = nci_get_drvdata(ndev); |
| 34 | int r; |
| 35 | |
| 36 | if (test_and_set_bit(ST21NFCB_NCI_RUNNING, &info->flags)) |
| 37 | return 0; |
| 38 | |
| 39 | r = ndlc_open(info->ndlc); |
| 40 | if (r) |
| 41 | clear_bit(ST21NFCB_NCI_RUNNING, &info->flags); |
| 42 | |
| 43 | return r; |
| 44 | } |
| 45 | |
| 46 | static int st21nfcb_nci_close(struct nci_dev *ndev) |
| 47 | { |
| 48 | struct st21nfcb_nci_info *info = nci_get_drvdata(ndev); |
| 49 | |
| 50 | if (!test_and_clear_bit(ST21NFCB_NCI_RUNNING, &info->flags)) |
| 51 | return 0; |
| 52 | |
| 53 | ndlc_close(info->ndlc); |
| 54 | |
| 55 | return 0; |
| 56 | } |
| 57 | |
| 58 | static int st21nfcb_nci_send(struct nci_dev *ndev, struct sk_buff *skb) |
| 59 | { |
| 60 | struct st21nfcb_nci_info *info = nci_get_drvdata(ndev); |
| 61 | |
| 62 | skb->dev = (void *)ndev; |
| 63 | |
| 64 | if (!test_bit(ST21NFCB_NCI_RUNNING, &info->flags)) |
| 65 | return -EBUSY; |
| 66 | |
| 67 | return ndlc_send(info->ndlc, skb); |
| 68 | } |
| 69 | |
Christophe Ricard | 9e87f9a | 2014-09-13 10:28:49 +0200 | [diff] [blame] | 70 | static __u32 st21nfcb_nci_get_rfprotocol(struct nci_dev *ndev, |
| 71 | __u8 rf_protocol) |
| 72 | { |
| 73 | return rf_protocol == ST21NFCB_NCI1_X_PROPRIETARY_ISO15693 ? |
| 74 | NFC_PROTO_ISO15693_MASK : 0; |
| 75 | } |
| 76 | |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 77 | static struct nci_ops st21nfcb_nci_ops = { |
| 78 | .open = st21nfcb_nci_open, |
| 79 | .close = st21nfcb_nci_close, |
| 80 | .send = st21nfcb_nci_send, |
Christophe Ricard | 9e87f9a | 2014-09-13 10:28:49 +0200 | [diff] [blame] | 81 | .get_rfprotocol = st21nfcb_nci_get_rfprotocol, |
Christophe Ricard | 8ae01f7 | 2015-02-01 22:26:15 +0100 | [diff] [blame] | 82 | .discover_se = st21nfcb_nci_discover_se, |
| 83 | .enable_se = st21nfcb_nci_enable_se, |
| 84 | .disable_se = st21nfcb_nci_disable_se, |
| 85 | .se_io = st21nfcb_nci_se_io, |
| 86 | .hci_load_session = st21nfcb_hci_load_session, |
| 87 | .hci_event_received = st21nfcb_hci_event_received, |
| 88 | .hci_cmd_received = st21nfcb_hci_cmd_received, |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | int st21nfcb_nci_probe(struct llt_ndlc *ndlc, int phy_headroom, |
| 92 | int phy_tailroom) |
| 93 | { |
| 94 | struct st21nfcb_nci_info *info; |
| 95 | int r; |
| 96 | u32 protocols; |
| 97 | |
| 98 | info = devm_kzalloc(ndlc->dev, |
| 99 | sizeof(struct st21nfcb_nci_info), GFP_KERNEL); |
| 100 | if (!info) |
| 101 | return -ENOMEM; |
| 102 | |
| 103 | protocols = NFC_PROTO_JEWEL_MASK |
| 104 | | NFC_PROTO_MIFARE_MASK |
| 105 | | NFC_PROTO_FELICA_MASK |
| 106 | | NFC_PROTO_ISO14443_MASK |
| 107 | | NFC_PROTO_ISO14443_B_MASK |
Christophe Ricard | 941ec5c | 2014-09-13 10:28:50 +0200 | [diff] [blame] | 108 | | NFC_PROTO_ISO15693_MASK |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 109 | | NFC_PROTO_NFC_DEP_MASK; |
| 110 | |
| 111 | ndlc->ndev = nci_allocate_device(&st21nfcb_nci_ops, protocols, |
| 112 | phy_headroom, phy_tailroom); |
| 113 | if (!ndlc->ndev) { |
| 114 | pr_err("Cannot allocate nfc ndev\n"); |
Christophe Ricard | df2566f | 2014-07-28 18:11:36 +0200 | [diff] [blame] | 115 | return -ENOMEM; |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 116 | } |
| 117 | info->ndlc = ndlc; |
| 118 | |
| 119 | nci_set_drvdata(ndlc->ndev, info); |
| 120 | |
| 121 | r = nci_register_device(ndlc->ndev); |
Christophe Ricard | df2566f | 2014-07-28 18:11:36 +0200 | [diff] [blame] | 122 | if (r) { |
| 123 | pr_err("Cannot register nfc device to nci core\n"); |
| 124 | nci_free_device(ndlc->ndev); |
Christophe Ricard | 8ae01f7 | 2015-02-01 22:26:15 +0100 | [diff] [blame] | 125 | return r; |
Christophe Ricard | df2566f | 2014-07-28 18:11:36 +0200 | [diff] [blame] | 126 | } |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 127 | |
Christophe Ricard | 8ae01f7 | 2015-02-01 22:26:15 +0100 | [diff] [blame] | 128 | return st21nfcb_se_init(ndlc->ndev); |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 129 | } |
| 130 | EXPORT_SYMBOL_GPL(st21nfcb_nci_probe); |
| 131 | |
| 132 | void st21nfcb_nci_remove(struct nci_dev *ndev) |
| 133 | { |
| 134 | struct st21nfcb_nci_info *info = nci_get_drvdata(ndev); |
| 135 | |
| 136 | nci_unregister_device(ndev); |
| 137 | nci_free_device(ndev); |
| 138 | kfree(info); |
| 139 | } |
| 140 | EXPORT_SYMBOL_GPL(st21nfcb_nci_remove); |
| 141 | |
| 142 | MODULE_LICENSE("GPL"); |
| 143 | MODULE_DESCRIPTION(DRIVER_DESC); |