2 * MEI Library for mei bus nfc device access
4 * Copyright (C) 2013 Intel Corporation. All rights reserved.
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.
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.
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/>.
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21 #include <linux/module.h>
22 #include <linux/slab.h>
23 #include <linux/nfc.h>
37 struct mei_nfc_reply {
48 struct mei_nfc_if_version {
49 u8 radio_version_sw[3];
51 u8 radio_version_hw[3];
58 struct mei_nfc_connect {
63 struct mei_nfc_connect_resp {
72 struct mei_nfc_hci_hdr {
80 #define MEI_NFC_CMD_MAINTENANCE 0x00
81 #define MEI_NFC_CMD_HCI_SEND 0x01
82 #define MEI_NFC_CMD_HCI_RECV 0x02
84 #define MEI_NFC_SUBCMD_CONNECT 0x00
85 #define MEI_NFC_SUBCMD_IF_VERSION 0x01
87 #define MEI_NFC_HEADER_SIZE 10
90 #define MEI_NFC_MAX_READ (MEI_NFC_HEADER_SIZE + MEI_NFC_MAX_HCI_PAYLOAD)
92 #define MEI_DUMP_SKB_IN(info, skb) \
94 pr_debug("%s:\n", info); \
95 print_hex_dump_debug("mei in : ", DUMP_PREFIX_OFFSET, \
96 16, 1, (skb)->data, (skb)->len, false); \
99 #define MEI_DUMP_SKB_OUT(info, skb) \
101 pr_debug("%s:\n", info); \
102 print_hex_dump_debug("mei out: ", DUMP_PREFIX_OFFSET, \
103 16, 1, (skb)->data, (skb)->len, false); \
107 static int mei_nfc_if_version(struct nfc_mei_phy *phy)
110 struct mei_nfc_cmd cmd;
111 struct mei_nfc_reply *reply = NULL;
112 struct mei_nfc_if_version *version;
113 size_t if_version_length;
116 pr_info("%s\n", __func__);
118 memset(&cmd, 0, sizeof(struct mei_nfc_cmd));
119 cmd.command = MEI_NFC_CMD_MAINTENANCE;
121 cmd.sub_command = MEI_NFC_SUBCMD_IF_VERSION;
123 r = mei_cl_send(phy->device, (u8 *)&cmd, sizeof(struct mei_nfc_cmd));
125 pr_err("Could not send IF version cmd\n");
129 /* to be sure on the stack we alloc memory */
130 if_version_length = sizeof(struct mei_nfc_reply) +
131 sizeof(struct mei_nfc_if_version);
133 reply = kzalloc(if_version_length, GFP_KERNEL);
137 bytes_recv = mei_cl_recv(phy->device, (u8 *)reply, if_version_length);
138 if (bytes_recv < 0 || bytes_recv < sizeof(struct mei_nfc_reply)) {
139 pr_err("Could not read IF version\n");
144 version = (struct mei_nfc_if_version *)reply->data;
146 phy->fw_ivn = version->fw_ivn;
147 phy->vendor_id = version->vendor_id;
148 phy->radio_type = version->radio_type;
155 static int mei_nfc_connect(struct nfc_mei_phy *phy)
157 struct mei_nfc_cmd *cmd, *reply;
158 struct mei_nfc_connect *connect;
159 struct mei_nfc_connect_resp *connect_resp;
160 size_t connect_length, connect_resp_length;
163 pr_info("%s\n", __func__);
165 connect_length = sizeof(struct mei_nfc_cmd) +
166 sizeof(struct mei_nfc_connect);
168 connect_resp_length = sizeof(struct mei_nfc_cmd) +
169 sizeof(struct mei_nfc_connect_resp);
171 cmd = kzalloc(connect_length, GFP_KERNEL);
174 connect = (struct mei_nfc_connect *)cmd->data;
176 reply = kzalloc(connect_resp_length, GFP_KERNEL);
182 connect_resp = (struct mei_nfc_connect_resp *)reply->data;
184 cmd->command = MEI_NFC_CMD_MAINTENANCE;
186 cmd->sub_command = MEI_NFC_SUBCMD_CONNECT;
187 connect->fw_ivn = phy->fw_ivn;
188 connect->vendor_id = phy->vendor_id;
190 r = mei_cl_send(phy->device, (u8 *)cmd, connect_length);
192 pr_err("Could not send connect cmd %d\n", r);
196 bytes_recv = mei_cl_recv(phy->device, (u8 *)reply, connect_resp_length);
197 if (bytes_recv < 0) {
199 pr_err("Could not read connect response %d\n", r);
203 pr_info("IVN 0x%x Vendor ID 0x%x\n",
204 connect_resp->fw_ivn, connect_resp->vendor_id);
206 pr_info("ME FW %d.%d.%d.%d\n",
207 connect_resp->me_major, connect_resp->me_minor,
208 connect_resp->me_hotfix, connect_resp->me_build);
219 static int mei_nfc_send(struct nfc_mei_phy *phy, u8 *buf, size_t length)
221 struct mei_nfc_hci_hdr *hdr;
226 mei_buf = kzalloc(length + MEI_NFC_HEADER_SIZE, GFP_KERNEL);
230 hdr = (struct mei_nfc_hci_hdr *) mei_buf;
231 hdr->cmd = MEI_NFC_CMD_HCI_SEND;
233 hdr->req_id = phy->req_id;
235 hdr->data_size = length;
237 memcpy(mei_buf + MEI_NFC_HEADER_SIZE, buf, length);
238 err = mei_cl_send(phy->device, mei_buf, length + MEI_NFC_HEADER_SIZE);
242 if (!wait_event_interruptible_timeout(phy->send_wq,
243 phy->recv_req_id == phy->req_id, HZ)) {
244 pr_err("NFC MEI command timeout\n");
255 * Writing a frame must not return the number of written bytes.
256 * It must return either zero for success, or <0 for error.
257 * In addition, it must not alter the skb
259 static int nfc_mei_phy_write(void *phy_id, struct sk_buff *skb)
261 struct nfc_mei_phy *phy = phy_id;
264 MEI_DUMP_SKB_OUT("mei frame sent", skb);
266 r = mei_nfc_send(phy, skb->data, skb->len);
273 static int mei_nfc_recv(struct nfc_mei_phy *phy, u8 *buf, size_t length)
275 struct mei_nfc_hci_hdr *hci_hdr;
278 received_length = mei_cl_recv(phy->device, buf, length);
279 if (received_length < 0)
280 return received_length;
282 hci_hdr = (struct mei_nfc_hci_hdr *) buf;
284 if (hci_hdr->cmd == MEI_NFC_CMD_HCI_SEND) {
285 phy->recv_req_id = hci_hdr->req_id;
286 wake_up(&phy->send_wq);
291 return received_length;
295 static void nfc_mei_event_cb(struct mei_cl_device *device, u32 events,
298 struct nfc_mei_phy *phy = context;
300 if (phy->hard_fault != 0)
303 if (events & BIT(MEI_CL_EVENT_RX)) {
307 skb = alloc_skb(MEI_NFC_MAX_READ, GFP_KERNEL);
311 reply_size = mei_nfc_recv(phy, skb->data, MEI_NFC_MAX_READ);
312 if (reply_size < MEI_NFC_HEADER_SIZE) {
317 skb_put(skb, reply_size);
318 skb_pull(skb, MEI_NFC_HEADER_SIZE);
320 MEI_DUMP_SKB_IN("mei frame read", skb);
322 nfc_hci_recv_frame(phy->hdev, skb);
326 static int nfc_mei_phy_enable(void *phy_id)
329 struct nfc_mei_phy *phy = phy_id;
331 pr_info("%s\n", __func__);
333 if (phy->powered == 1)
336 r = mei_cl_enable_device(phy->device);
338 pr_err("Could not enable device %d\n", r);
342 r = mei_nfc_if_version(phy);
344 pr_err("Could not enable device %d\n", r);
348 r = mei_nfc_connect(phy);
350 pr_err("Could not connect to device %d\n", r);
354 r = mei_cl_register_event_cb(phy->device, nfc_mei_event_cb, phy);
356 pr_err("Event cb registration failed %d\n", r);
366 mei_cl_disable_device(phy->device);
370 static void nfc_mei_phy_disable(void *phy_id)
372 struct nfc_mei_phy *phy = phy_id;
374 pr_info("%s\n", __func__);
376 mei_cl_disable_device(phy->device);
381 struct nfc_phy_ops mei_phy_ops = {
382 .write = nfc_mei_phy_write,
383 .enable = nfc_mei_phy_enable,
384 .disable = nfc_mei_phy_disable,
386 EXPORT_SYMBOL_GPL(mei_phy_ops);
388 struct nfc_mei_phy *nfc_mei_phy_alloc(struct mei_cl_device *device)
390 struct nfc_mei_phy *phy;
392 phy = kzalloc(sizeof(struct nfc_mei_phy), GFP_KERNEL);
396 phy->device = device;
397 init_waitqueue_head(&phy->send_wq);
398 mei_cl_set_drvdata(device, phy);
402 EXPORT_SYMBOL_GPL(nfc_mei_phy_alloc);
404 void nfc_mei_phy_free(struct nfc_mei_phy *phy)
406 mei_cl_disable_device(phy->device);
409 EXPORT_SYMBOL_GPL(nfc_mei_phy_free);
411 MODULE_LICENSE("GPL");
412 MODULE_DESCRIPTION("mei bus NFC device interface");