Eric Lapuyade | 4912e2f | 2013-04-15 11:19:20 +0200 | [diff] [blame] | 1 | /* |
| 2 | * MEI Library for mei bus nfc device access |
| 3 | * |
| 4 | * Copyright (C) 2013 Intel Corporation. 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 |
Jeff Kirsher | 98b32de | 2013-12-06 08:56:16 -0800 | [diff] [blame] | 16 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
Eric Lapuyade | 4912e2f | 2013-04-15 11:19:20 +0200 | [diff] [blame] | 17 | */ |
| 18 | |
Joe Perches | 17936b4 | 2013-04-05 12:27:39 -0700 | [diff] [blame] | 19 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 20 | |
Eric Lapuyade | 4912e2f | 2013-04-15 11:19:20 +0200 | [diff] [blame] | 21 | #include <linux/module.h> |
| 22 | #include <linux/slab.h> |
| 23 | #include <linux/nfc.h> |
| 24 | |
| 25 | #include "mei_phy.h" |
| 26 | |
| 27 | struct mei_nfc_hdr { |
| 28 | u8 cmd; |
| 29 | u8 status; |
| 30 | u16 req_id; |
| 31 | u32 reserved; |
| 32 | u16 data_size; |
Valentin Ilie | bda7eb2 | 2013-06-07 22:01:55 +0300 | [diff] [blame] | 33 | } __packed; |
Eric Lapuyade | 4912e2f | 2013-04-15 11:19:20 +0200 | [diff] [blame] | 34 | |
| 35 | #define MEI_NFC_MAX_READ (MEI_NFC_HEADER_SIZE + MEI_NFC_MAX_HCI_PAYLOAD) |
| 36 | |
| 37 | #define MEI_DUMP_SKB_IN(info, skb) \ |
| 38 | do { \ |
| 39 | pr_debug("%s:\n", info); \ |
| 40 | print_hex_dump_debug("mei in : ", DUMP_PREFIX_OFFSET, \ |
| 41 | 16, 1, (skb)->data, (skb)->len, false); \ |
| 42 | } while (0) |
| 43 | |
| 44 | #define MEI_DUMP_SKB_OUT(info, skb) \ |
| 45 | do { \ |
| 46 | pr_debug("%s:\n", info); \ |
| 47 | print_hex_dump_debug("mei out: ", DUMP_PREFIX_OFFSET, \ |
| 48 | 16, 1, (skb)->data, (skb)->len, false); \ |
| 49 | } while (0) |
| 50 | |
| 51 | int nfc_mei_phy_enable(void *phy_id) |
| 52 | { |
| 53 | int r; |
| 54 | struct nfc_mei_phy *phy = phy_id; |
| 55 | |
| 56 | pr_info("%s\n", __func__); |
| 57 | |
| 58 | if (phy->powered == 1) |
| 59 | return 0; |
| 60 | |
| 61 | r = mei_cl_enable_device(phy->device); |
| 62 | if (r < 0) { |
Joe Perches | 17936b4 | 2013-04-05 12:27:39 -0700 | [diff] [blame] | 63 | pr_err("Could not enable device\n"); |
Valentin Ilie | bda7eb2 | 2013-06-07 22:01:55 +0300 | [diff] [blame] | 64 | return r; |
Eric Lapuyade | 4912e2f | 2013-04-15 11:19:20 +0200 | [diff] [blame] | 65 | } |
| 66 | |
Samuel Ortiz | 73f3adb | 2013-04-30 23:48:50 +0200 | [diff] [blame] | 67 | r = mei_cl_register_event_cb(phy->device, nfc_mei_event_cb, phy); |
| 68 | if (r) { |
Joe Perches | 17936b4 | 2013-04-05 12:27:39 -0700 | [diff] [blame] | 69 | pr_err("Event cb registration failed\n"); |
Samuel Ortiz | 73f3adb | 2013-04-30 23:48:50 +0200 | [diff] [blame] | 70 | mei_cl_disable_device(phy->device); |
| 71 | phy->powered = 0; |
| 72 | |
| 73 | return r; |
| 74 | } |
| 75 | |
Eric Lapuyade | 4912e2f | 2013-04-15 11:19:20 +0200 | [diff] [blame] | 76 | phy->powered = 1; |
| 77 | |
| 78 | return 0; |
| 79 | } |
| 80 | EXPORT_SYMBOL_GPL(nfc_mei_phy_enable); |
| 81 | |
| 82 | void nfc_mei_phy_disable(void *phy_id) |
| 83 | { |
| 84 | struct nfc_mei_phy *phy = phy_id; |
| 85 | |
| 86 | pr_info("%s\n", __func__); |
| 87 | |
| 88 | mei_cl_disable_device(phy->device); |
| 89 | |
| 90 | phy->powered = 0; |
| 91 | } |
| 92 | EXPORT_SYMBOL_GPL(nfc_mei_phy_disable); |
| 93 | |
| 94 | /* |
| 95 | * Writing a frame must not return the number of written bytes. |
| 96 | * It must return either zero for success, or <0 for error. |
| 97 | * In addition, it must not alter the skb |
| 98 | */ |
| 99 | static int nfc_mei_phy_write(void *phy_id, struct sk_buff *skb) |
| 100 | { |
| 101 | struct nfc_mei_phy *phy = phy_id; |
| 102 | int r; |
| 103 | |
| 104 | MEI_DUMP_SKB_OUT("mei frame sent", skb); |
| 105 | |
| 106 | r = mei_cl_send(phy->device, skb->data, skb->len); |
| 107 | if (r > 0) |
| 108 | r = 0; |
| 109 | |
| 110 | return r; |
| 111 | } |
| 112 | |
| 113 | void nfc_mei_event_cb(struct mei_cl_device *device, u32 events, void *context) |
| 114 | { |
| 115 | struct nfc_mei_phy *phy = context; |
| 116 | |
| 117 | if (phy->hard_fault != 0) |
| 118 | return; |
| 119 | |
| 120 | if (events & BIT(MEI_CL_EVENT_RX)) { |
| 121 | struct sk_buff *skb; |
| 122 | int reply_size; |
| 123 | |
| 124 | skb = alloc_skb(MEI_NFC_MAX_READ, GFP_KERNEL); |
| 125 | if (!skb) |
| 126 | return; |
| 127 | |
| 128 | reply_size = mei_cl_recv(device, skb->data, MEI_NFC_MAX_READ); |
| 129 | if (reply_size < MEI_NFC_HEADER_SIZE) { |
Salil Kapur | fe0219c | 2013-10-31 23:17:46 +0530 | [diff] [blame^] | 130 | kfree_skb(skb); |
Eric Lapuyade | 4912e2f | 2013-04-15 11:19:20 +0200 | [diff] [blame] | 131 | return; |
| 132 | } |
| 133 | |
| 134 | skb_put(skb, reply_size); |
| 135 | skb_pull(skb, MEI_NFC_HEADER_SIZE); |
| 136 | |
| 137 | MEI_DUMP_SKB_IN("mei frame read", skb); |
| 138 | |
| 139 | nfc_hci_recv_frame(phy->hdev, skb); |
| 140 | } |
| 141 | } |
| 142 | EXPORT_SYMBOL_GPL(nfc_mei_event_cb); |
| 143 | |
| 144 | struct nfc_phy_ops mei_phy_ops = { |
| 145 | .write = nfc_mei_phy_write, |
| 146 | .enable = nfc_mei_phy_enable, |
| 147 | .disable = nfc_mei_phy_disable, |
| 148 | }; |
| 149 | EXPORT_SYMBOL_GPL(mei_phy_ops); |
| 150 | |
| 151 | struct nfc_mei_phy *nfc_mei_phy_alloc(struct mei_cl_device *device) |
| 152 | { |
| 153 | struct nfc_mei_phy *phy; |
| 154 | |
| 155 | phy = kzalloc(sizeof(struct nfc_mei_phy), GFP_KERNEL); |
| 156 | if (!phy) |
| 157 | return NULL; |
| 158 | |
| 159 | phy->device = device; |
| 160 | mei_cl_set_drvdata(device, phy); |
| 161 | |
| 162 | return phy; |
| 163 | } |
| 164 | EXPORT_SYMBOL_GPL(nfc_mei_phy_alloc); |
| 165 | |
| 166 | void nfc_mei_phy_free(struct nfc_mei_phy *phy) |
| 167 | { |
| 168 | kfree(phy); |
| 169 | } |
| 170 | EXPORT_SYMBOL_GPL(nfc_mei_phy_free); |
| 171 | |
| 172 | MODULE_LICENSE("GPL"); |
| 173 | MODULE_DESCRIPTION("mei bus NFC device interface"); |