xref: /openbmc/linux/drivers/nfc/mei_phy.c (revision f35e839a)
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
16  * along with this program; if not, write to the
17  * Free Software Foundation, Inc.,
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  */
20 
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;
33 } __attribute__((packed));
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) {
63                 pr_err("MEI_PHY: Could not enable device\n");
64                 return r;
65 	}
66 
67 	phy->powered = 1;
68 
69 	return 0;
70 }
71 EXPORT_SYMBOL_GPL(nfc_mei_phy_enable);
72 
73 void nfc_mei_phy_disable(void *phy_id)
74 {
75 	struct nfc_mei_phy *phy = phy_id;
76 
77 	pr_info("%s\n", __func__);
78 
79 	mei_cl_disable_device(phy->device);
80 
81 	phy->powered = 0;
82 }
83 EXPORT_SYMBOL_GPL(nfc_mei_phy_disable);
84 
85 /*
86  * Writing a frame must not return the number of written bytes.
87  * It must return either zero for success, or <0 for error.
88  * In addition, it must not alter the skb
89  */
90 static int nfc_mei_phy_write(void *phy_id, struct sk_buff *skb)
91 {
92 	struct nfc_mei_phy *phy = phy_id;
93 	int r;
94 
95 	MEI_DUMP_SKB_OUT("mei frame sent", skb);
96 
97 	r = mei_cl_send(phy->device, skb->data, skb->len);
98 	if (r > 0)
99 		r = 0;
100 
101 	return r;
102 }
103 
104 void nfc_mei_event_cb(struct mei_cl_device *device, u32 events, void *context)
105 {
106 	struct nfc_mei_phy *phy = context;
107 
108 	if (phy->hard_fault != 0)
109 		return;
110 
111 	if (events & BIT(MEI_CL_EVENT_RX)) {
112 		struct sk_buff *skb;
113 		int reply_size;
114 
115 		skb = alloc_skb(MEI_NFC_MAX_READ, GFP_KERNEL);
116 		if (!skb)
117 			return;
118 
119 		reply_size = mei_cl_recv(device, skb->data, MEI_NFC_MAX_READ);
120 		if (reply_size < MEI_NFC_HEADER_SIZE) {
121 			kfree(skb);
122 			return;
123 		}
124 
125 		skb_put(skb, reply_size);
126 		skb_pull(skb, MEI_NFC_HEADER_SIZE);
127 
128 		MEI_DUMP_SKB_IN("mei frame read", skb);
129 
130 		nfc_hci_recv_frame(phy->hdev, skb);
131 	}
132 }
133 EXPORT_SYMBOL_GPL(nfc_mei_event_cb);
134 
135 struct nfc_phy_ops mei_phy_ops = {
136 	.write = nfc_mei_phy_write,
137 	.enable = nfc_mei_phy_enable,
138 	.disable = nfc_mei_phy_disable,
139 };
140 EXPORT_SYMBOL_GPL(mei_phy_ops);
141 
142 struct nfc_mei_phy *nfc_mei_phy_alloc(struct mei_cl_device *device)
143 {
144 	struct nfc_mei_phy *phy;
145 
146 	phy = kzalloc(sizeof(struct nfc_mei_phy), GFP_KERNEL);
147 	if (!phy)
148 		return NULL;
149 
150 	phy->device = device;
151 	mei_cl_set_drvdata(device, phy);
152 
153 	return phy;
154 }
155 EXPORT_SYMBOL_GPL(nfc_mei_phy_alloc);
156 
157 void nfc_mei_phy_free(struct nfc_mei_phy *phy)
158 {
159 	kfree(phy);
160 }
161 EXPORT_SYMBOL_GPL(nfc_mei_phy_free);
162 
163 MODULE_LICENSE("GPL");
164 MODULE_DESCRIPTION("mei bus NFC device interface");
165