···22222323 If you choose to build a module, it'll be called microread_i2c.2424 Say N if unsure.2525+2626+config NFC_MICROREAD_MEI2727+ tristate "NFC Microread MEI support"2828+ depends on NFC_MICROREAD && INTEL_MEI_BUS_NFC2929+ ---help---3030+ This module adds support for the mei interface of adapters using3131+ Inside microread chipsets. Select this if your microread chipset3232+ is handled by Intel's Management Engine Interface on your platform.3333+3434+ If you choose to build a module, it'll be called microread_mei.3535+ Say N if unsure.
···11+/*22+ * HCI based Driver for Inside Secure microread NFC Chip33+ *44+ * Copyright (C) 2013 Intel Corporation. All rights reserved.55+ *66+ * This program is free software; you can redistribute it and/or modify it77+ * under the terms and conditions of the GNU General Public License,88+ * version 2, as published by the Free Software Foundation.99+ *1010+ * This program is distributed in the hope that it will be useful,1111+ * but WITHOUT ANY WARRANTY; without even the implied warranty of1212+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the1313+ * GNU General Public License for more details.1414+ *1515+ * You should have received a copy of the GNU General Public License1616+ * along with this program; if not, write to the1717+ * Free Software Foundation, Inc.,1818+ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.1919+ */2020+2121+#include <linux/module.h>2222+#include <linux/slab.h>2323+#include <linux/interrupt.h>2424+#include <linux/gpio.h>2525+#include <linux/mei_bus.h>2626+2727+#include <linux/nfc.h>2828+#include <net/nfc/hci.h>2929+#include <net/nfc/llc.h>3030+3131+#include "microread.h"3232+3333+#define MICROREAD_DRIVER_NAME "microread"3434+3535+#define MICROREAD_UUID UUID_LE(0x0bb17a78, 0x2a8e, 0x4c50, 0x94, \3636+ 0xd4, 0x50, 0x26, 0x67, 0x23, 0x77, 0x5c)3737+3838+struct mei_nfc_hdr {3939+ u8 cmd;4040+ u8 status;4141+ u16 req_id;4242+ u32 reserved;4343+ u16 data_size;4444+} __attribute__((packed));4545+4646+#define MEI_NFC_HEADER_SIZE 104747+#define MEI_NFC_MAX_HCI_PAYLOAD 3004848+#define MEI_NFC_MAX_READ (MEI_NFC_HEADER_SIZE + MEI_NFC_MAX_HCI_PAYLOAD)4949+5050+struct microread_mei_phy {5151+ struct mei_bus_client *client;5252+ struct nfc_hci_dev *hdev;5353+5454+ int powered;5555+5656+ int hard_fault; /*5757+ * < 0 if hardware error occured (e.g. i2c err)5858+ * and prevents normal operation.5959+ */6060+};6161+6262+#define MEI_DUMP_SKB_IN(info, skb) \6363+do { \6464+ pr_debug("%s:\n", info); \6565+ print_hex_dump(KERN_DEBUG, "mei in : ", DUMP_PREFIX_OFFSET, \6666+ 16, 1, (skb)->data, (skb)->len, 0); \6767+} while (0)6868+6969+#define MEI_DUMP_SKB_OUT(info, skb) \7070+do { \7171+ pr_debug("%s:\n", info); \7272+ print_hex_dump(KERN_DEBUG, "mei out: ", DUMP_PREFIX_OFFSET, \7373+ 16, 1, (skb)->data, (skb)->len, 0); \7474+} while (0)7575+7676+static int microread_mei_enable(void *phy_id)7777+{7878+ struct microread_mei_phy *phy = phy_id;7979+8080+ pr_info(DRIVER_DESC ": %s\n", __func__);8181+8282+ phy->powered = 1;8383+8484+ return 0;8585+}8686+8787+static void microread_mei_disable(void *phy_id)8888+{8989+ struct microread_mei_phy *phy = phy_id;9090+9191+ pr_info(DRIVER_DESC ": %s\n", __func__);9292+9393+ phy->powered = 0;9494+}9595+9696+/*9797+ * Writing a frame must not return the number of written bytes.9898+ * It must return either zero for success, or <0 for error.9999+ * In addition, it must not alter the skb100100+ */101101+static int microread_mei_write(void *phy_id, struct sk_buff *skb)102102+{103103+ struct microread_mei_phy *phy = phy_id;104104+ int r;105105+106106+ MEI_DUMP_SKB_OUT("mei frame sent", skb);107107+108108+ r = mei_bus_send(phy->client, skb->data, skb->len);109109+ if (r > 0)110110+ r = 0;111111+112112+ return r;113113+}114114+115115+static void microread_event_cb(struct mei_bus_client *client, u32 events,116116+ void *context)117117+{118118+ struct microread_mei_phy *phy = context;119119+120120+ if (phy->hard_fault != 0)121121+ return;122122+123123+ if (events & BIT(MEI_BUS_EVENT_RX)) {124124+ struct sk_buff *skb;125125+ int reply_size;126126+127127+ skb = alloc_skb(MEI_NFC_MAX_READ, GFP_KERNEL);128128+ if (!skb)129129+ return;130130+131131+ reply_size = mei_bus_recv(client, skb->data, MEI_NFC_MAX_READ);132132+ if (reply_size < MEI_NFC_HEADER_SIZE) {133133+ kfree(skb);134134+ return;135135+ }136136+137137+ skb_put(skb, reply_size);138138+ skb_pull(skb, MEI_NFC_HEADER_SIZE);139139+140140+ MEI_DUMP_SKB_IN("mei frame read", skb);141141+142142+ nfc_hci_recv_frame(phy->hdev, skb);143143+ }144144+}145145+146146+static struct nfc_phy_ops mei_phy_ops = {147147+ .write = microread_mei_write,148148+ .enable = microread_mei_enable,149149+ .disable = microread_mei_disable,150150+};151151+152152+static int microread_mei_probe(struct mei_bus_client *client)153153+{154154+ struct microread_mei_phy *phy;155155+ int r;156156+157157+ pr_info("Probing NFC microread\n");158158+159159+ phy = kzalloc(sizeof(struct microread_mei_phy), GFP_KERNEL);160160+ if (!phy) {161161+ pr_err("Cannot allocate memory for microread mei phy.\n");162162+ return -ENOMEM;163163+ }164164+165165+ phy->client = client;166166+ mei_bus_set_clientdata(client, phy);167167+168168+ r = mei_bus_register_event_cb(client, microread_event_cb, phy);169169+ if (r) {170170+ pr_err(MICROREAD_DRIVER_NAME ": event cb registration failed\n");171171+ goto err_out;172172+ }173173+174174+ r = microread_probe(phy, &mei_phy_ops, LLC_NOP_NAME,175175+ MEI_NFC_HEADER_SIZE, 0, MEI_NFC_MAX_HCI_PAYLOAD,176176+ &phy->hdev);177177+ if (r < 0)178178+ goto err_out;179179+180180+ return 0;181181+182182+err_out:183183+ kfree(phy);184184+185185+ return r;186186+}187187+188188+static int microread_mei_remove(struct mei_bus_client *client)189189+{190190+ struct microread_mei_phy *phy = mei_bus_get_clientdata(client);191191+192192+ pr_info("Removing microread\n");193193+194194+ microread_remove(phy->hdev);195195+196196+ if (phy->powered)197197+ microread_mei_disable(phy);198198+199199+ kfree(phy);200200+201201+ return 0;202202+}203203+204204+static struct mei_bus_driver microread_driver = {205205+ .driver = {206206+ .name = MICROREAD_DRIVER_NAME,207207+ },208208+ .id = {209209+ .name = MICROREAD_DRIVER_NAME,210210+ .uuid = MICROREAD_UUID,211211+ },212212+213213+ .probe = microread_mei_probe,214214+ .remove = microread_mei_remove,215215+};216216+217217+static int microread_mei_init(void)218218+{219219+ int r;220220+221221+ pr_debug(DRIVER_DESC ": %s\n", __func__);222222+223223+ r = mei_driver_register(µread_driver);224224+ if (r) {225225+ pr_err(MICROREAD_DRIVER_NAME ": driver registration failed\n");226226+ return r;227227+ }228228+229229+ return 0;230230+}231231+232232+static void microread_mei_exit(void)233233+{234234+ mei_driver_unregister(µread_driver);235235+}236236+237237+module_init(microread_mei_init);238238+module_exit(microread_mei_exit);239239+240240+MODULE_LICENSE("GPL");241241+MODULE_DESCRIPTION(DRIVER_DESC);