blob: 0f43bb3895669061a3007a2f27709f4441ab44d1 [file] [log] [blame]
Tomas Winkler191b0702019-03-12 00:10:42 +02001// SPDX-License-Identifier: GPL-2.0
Eric Lapuyade4912e2f2013-04-15 11:19:20 +02002/*
Tomas Winkler191b0702019-03-12 00:10:42 +02003 * Copyright (c) 2013, Intel Corporation.
4 *
Eric Lapuyade4912e2f2013-04-15 11:19:20 +02005 * MEI Library for mei bus nfc device access
Eric Lapuyade4912e2f2013-04-15 11:19:20 +02006 */
Joe Perches17936b42013-04-05 12:27:39 -07007#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8
Eric Lapuyade4912e2f2013-04-15 11:19:20 +02009#include <linux/module.h>
10#include <linux/slab.h>
11#include <linux/nfc.h>
12
13#include "mei_phy.h"
14
Tomas Winkler1d3ff76722015-05-07 15:54:06 +030015struct mei_nfc_hdr {
16 u8 cmd;
Tomas Winklerbe9b7202015-05-07 15:54:04 +030017 u8 status;
18 u16 req_id;
19 u32 reserved;
20 u16 data_size;
Tomas Winkler1d3ff76722015-05-07 15:54:06 +030021} __packed;
22
23struct mei_nfc_cmd {
24 struct mei_nfc_hdr hdr;
Tomas Winklerbe9b7202015-05-07 15:54:04 +030025 u8 sub_command;
26 u8 data[];
27} __packed;
28
29struct mei_nfc_reply {
Tomas Winkler1d3ff76722015-05-07 15:54:06 +030030 struct mei_nfc_hdr hdr;
Tomas Winklerbe9b7202015-05-07 15:54:04 +030031 u8 sub_command;
32 u8 reply_status;
33 u8 data[];
34} __packed;
35
36struct mei_nfc_if_version {
37 u8 radio_version_sw[3];
38 u8 reserved[3];
39 u8 radio_version_hw[3];
40 u8 i2c_addr;
41 u8 fw_ivn;
42 u8 vendor_id;
43 u8 radio_type;
44} __packed;
45
46struct mei_nfc_connect {
47 u8 fw_ivn;
48 u8 vendor_id;
49} __packed;
50
51struct mei_nfc_connect_resp {
52 u8 fw_ivn;
53 u8 vendor_id;
54 u16 me_major;
55 u16 me_minor;
56 u16 me_hotfix;
57 u16 me_build;
58} __packed;
59
Eric Lapuyade4912e2f2013-04-15 11:19:20 +020060
Tomas Winklerbe9b7202015-05-07 15:54:04 +030061#define MEI_NFC_CMD_MAINTENANCE 0x00
62#define MEI_NFC_CMD_HCI_SEND 0x01
63#define MEI_NFC_CMD_HCI_RECV 0x02
64
65#define MEI_NFC_SUBCMD_CONNECT 0x00
66#define MEI_NFC_SUBCMD_IF_VERSION 0x01
67
Eric Lapuyade4912e2f2013-04-15 11:19:20 +020068#define MEI_NFC_MAX_READ (MEI_NFC_HEADER_SIZE + MEI_NFC_MAX_HCI_PAYLOAD)
69
70#define MEI_DUMP_SKB_IN(info, skb) \
71do { \
72 pr_debug("%s:\n", info); \
73 print_hex_dump_debug("mei in : ", DUMP_PREFIX_OFFSET, \
74 16, 1, (skb)->data, (skb)->len, false); \
75} while (0)
76
77#define MEI_DUMP_SKB_OUT(info, skb) \
78do { \
79 pr_debug("%s:\n", info); \
80 print_hex_dump_debug("mei out: ", DUMP_PREFIX_OFFSET, \
Tomas Winklerbe9b7202015-05-07 15:54:04 +030081 16, 1, (skb)->data, (skb)->len, false); \
Eric Lapuyade4912e2f2013-04-15 11:19:20 +020082} while (0)
83
Tomas Winkler1d3ff76722015-05-07 15:54:06 +030084#define MEI_DUMP_NFC_HDR(info, _hdr) \
85do { \
86 pr_debug("%s:\n", info); \
87 pr_debug("cmd=%02d status=%d req_id=%d rsvd=%d size=%d\n", \
88 (_hdr)->cmd, (_hdr)->status, (_hdr)->req_id, \
89 (_hdr)->reserved, (_hdr)->data_size); \
90} while (0)
Tomas Winklerbe9b7202015-05-07 15:54:04 +030091
92static int mei_nfc_if_version(struct nfc_mei_phy *phy)
Eric Lapuyade4912e2f2013-04-15 11:19:20 +020093{
Tomas Winklerbe9b7202015-05-07 15:54:04 +030094
95 struct mei_nfc_cmd cmd;
96 struct mei_nfc_reply *reply = NULL;
97 struct mei_nfc_if_version *version;
98 size_t if_version_length;
99 int bytes_recv, r;
Eric Lapuyade4912e2f2013-04-15 11:19:20 +0200100
101 pr_info("%s\n", __func__);
102
Tomas Winklerbe9b7202015-05-07 15:54:04 +0300103 memset(&cmd, 0, sizeof(struct mei_nfc_cmd));
Tomas Winkler1d3ff76722015-05-07 15:54:06 +0300104 cmd.hdr.cmd = MEI_NFC_CMD_MAINTENANCE;
105 cmd.hdr.data_size = 1;
Tomas Winklerbe9b7202015-05-07 15:54:04 +0300106 cmd.sub_command = MEI_NFC_SUBCMD_IF_VERSION;
Eric Lapuyade4912e2f2013-04-15 11:19:20 +0200107
Tomas Winkler1d3ff76722015-05-07 15:54:06 +0300108 MEI_DUMP_NFC_HDR("version", &cmd.hdr);
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300109 r = mei_cldev_send(phy->cldev, (u8 *)&cmd, sizeof(struct mei_nfc_cmd));
Eric Lapuyade4912e2f2013-04-15 11:19:20 +0200110 if (r < 0) {
Tomas Winklerbe9b7202015-05-07 15:54:04 +0300111 pr_err("Could not send IF version cmd\n");
Valentin Iliebda7eb22013-06-07 22:01:55 +0300112 return r;
Eric Lapuyade4912e2f2013-04-15 11:19:20 +0200113 }
114
Tomas Winklerbe9b7202015-05-07 15:54:04 +0300115 /* to be sure on the stack we alloc memory */
116 if_version_length = sizeof(struct mei_nfc_reply) +
117 sizeof(struct mei_nfc_if_version);
Samuel Ortiz73f3adb2013-04-30 23:48:50 +0200118
Tomas Winklerbe9b7202015-05-07 15:54:04 +0300119 reply = kzalloc(if_version_length, GFP_KERNEL);
120 if (!reply)
121 return -ENOMEM;
122
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300123 bytes_recv = mei_cldev_recv(phy->cldev, (u8 *)reply, if_version_length);
Alexander Usyskin582ab272016-10-31 19:02:39 +0200124 if (bytes_recv < 0 || bytes_recv < if_version_length) {
Tomas Winklerbe9b7202015-05-07 15:54:04 +0300125 pr_err("Could not read IF version\n");
126 r = -EIO;
127 goto err;
Samuel Ortiz73f3adb2013-04-30 23:48:50 +0200128 }
129
Tomas Winklerbe9b7202015-05-07 15:54:04 +0300130 version = (struct mei_nfc_if_version *)reply->data;
Eric Lapuyade4912e2f2013-04-15 11:19:20 +0200131
Tomas Winklerbe9b7202015-05-07 15:54:04 +0300132 phy->fw_ivn = version->fw_ivn;
133 phy->vendor_id = version->vendor_id;
134 phy->radio_type = version->radio_type;
135
136err:
137 kfree(reply);
138 return r;
Eric Lapuyade4912e2f2013-04-15 11:19:20 +0200139}
Eric Lapuyade4912e2f2013-04-15 11:19:20 +0200140
Tomas Winklerbe9b7202015-05-07 15:54:04 +0300141static int mei_nfc_connect(struct nfc_mei_phy *phy)
Eric Lapuyade4912e2f2013-04-15 11:19:20 +0200142{
Tomas Winklerbe9b7202015-05-07 15:54:04 +0300143 struct mei_nfc_cmd *cmd, *reply;
144 struct mei_nfc_connect *connect;
145 struct mei_nfc_connect_resp *connect_resp;
146 size_t connect_length, connect_resp_length;
147 int bytes_recv, r;
Eric Lapuyade4912e2f2013-04-15 11:19:20 +0200148
149 pr_info("%s\n", __func__);
150
Tomas Winklerbe9b7202015-05-07 15:54:04 +0300151 connect_length = sizeof(struct mei_nfc_cmd) +
152 sizeof(struct mei_nfc_connect);
Eric Lapuyade4912e2f2013-04-15 11:19:20 +0200153
Tomas Winklerbe9b7202015-05-07 15:54:04 +0300154 connect_resp_length = sizeof(struct mei_nfc_cmd) +
155 sizeof(struct mei_nfc_connect_resp);
156
157 cmd = kzalloc(connect_length, GFP_KERNEL);
158 if (!cmd)
159 return -ENOMEM;
160 connect = (struct mei_nfc_connect *)cmd->data;
161
162 reply = kzalloc(connect_resp_length, GFP_KERNEL);
163 if (!reply) {
164 kfree(cmd);
165 return -ENOMEM;
166 }
167
168 connect_resp = (struct mei_nfc_connect_resp *)reply->data;
169
Tomas Winkler1d3ff76722015-05-07 15:54:06 +0300170 cmd->hdr.cmd = MEI_NFC_CMD_MAINTENANCE;
171 cmd->hdr.data_size = 3;
Tomas Winklerbe9b7202015-05-07 15:54:04 +0300172 cmd->sub_command = MEI_NFC_SUBCMD_CONNECT;
173 connect->fw_ivn = phy->fw_ivn;
174 connect->vendor_id = phy->vendor_id;
175
Tomas Winkler1d3ff76722015-05-07 15:54:06 +0300176 MEI_DUMP_NFC_HDR("connect request", &cmd->hdr);
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300177 r = mei_cldev_send(phy->cldev, (u8 *)cmd, connect_length);
Tomas Winklerbe9b7202015-05-07 15:54:04 +0300178 if (r < 0) {
179 pr_err("Could not send connect cmd %d\n", r);
180 goto err;
181 }
182
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300183 bytes_recv = mei_cldev_recv(phy->cldev, (u8 *)reply,
184 connect_resp_length);
Tomas Winklerbe9b7202015-05-07 15:54:04 +0300185 if (bytes_recv < 0) {
186 r = bytes_recv;
187 pr_err("Could not read connect response %d\n", r);
188 goto err;
189 }
190
Tomas Winkler1d3ff76722015-05-07 15:54:06 +0300191 MEI_DUMP_NFC_HDR("connect reply", &reply->hdr);
192
Tomas Winklerbe9b7202015-05-07 15:54:04 +0300193 pr_info("IVN 0x%x Vendor ID 0x%x\n",
194 connect_resp->fw_ivn, connect_resp->vendor_id);
195
196 pr_info("ME FW %d.%d.%d.%d\n",
197 connect_resp->me_major, connect_resp->me_minor,
198 connect_resp->me_hotfix, connect_resp->me_build);
199
200 r = 0;
201
202err:
203 kfree(reply);
204 kfree(cmd);
205
206 return r;
Eric Lapuyade4912e2f2013-04-15 11:19:20 +0200207}
Tomas Winklerbe9b7202015-05-07 15:54:04 +0300208
209static int mei_nfc_send(struct nfc_mei_phy *phy, u8 *buf, size_t length)
210{
Tomas Winkler1d3ff76722015-05-07 15:54:06 +0300211 struct mei_nfc_hdr *hdr;
Tomas Winklerbe9b7202015-05-07 15:54:04 +0300212 u8 *mei_buf;
213 int err;
214
215 err = -ENOMEM;
216 mei_buf = kzalloc(length + MEI_NFC_HEADER_SIZE, GFP_KERNEL);
217 if (!mei_buf)
218 goto out;
219
Tomas Winkler1d3ff76722015-05-07 15:54:06 +0300220 hdr = (struct mei_nfc_hdr *)mei_buf;
Tomas Winklerbe9b7202015-05-07 15:54:04 +0300221 hdr->cmd = MEI_NFC_CMD_HCI_SEND;
222 hdr->status = 0;
223 hdr->req_id = phy->req_id;
224 hdr->reserved = 0;
225 hdr->data_size = length;
226
Tomas Winkler1d3ff76722015-05-07 15:54:06 +0300227 MEI_DUMP_NFC_HDR("send", hdr);
228
Tomas Winklerbe9b7202015-05-07 15:54:04 +0300229 memcpy(mei_buf + MEI_NFC_HEADER_SIZE, buf, length);
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300230 err = mei_cldev_send(phy->cldev, mei_buf, length + MEI_NFC_HEADER_SIZE);
Tomas Winklerbe9b7202015-05-07 15:54:04 +0300231 if (err < 0)
232 goto out;
233
234 if (!wait_event_interruptible_timeout(phy->send_wq,
235 phy->recv_req_id == phy->req_id, HZ)) {
236 pr_err("NFC MEI command timeout\n");
237 err = -ETIME;
238 } else {
239 phy->req_id++;
240 }
241out:
242 kfree(mei_buf);
243 return err;
244}
Eric Lapuyade4912e2f2013-04-15 11:19:20 +0200245
246/*
247 * Writing a frame must not return the number of written bytes.
248 * It must return either zero for success, or <0 for error.
249 * In addition, it must not alter the skb
250 */
251static int nfc_mei_phy_write(void *phy_id, struct sk_buff *skb)
252{
253 struct nfc_mei_phy *phy = phy_id;
254 int r;
255
256 MEI_DUMP_SKB_OUT("mei frame sent", skb);
257
Tomas Winklerbe9b7202015-05-07 15:54:04 +0300258 r = mei_nfc_send(phy, skb->data, skb->len);
Eric Lapuyade4912e2f2013-04-15 11:19:20 +0200259 if (r > 0)
260 r = 0;
261
262 return r;
263}
264
Tomas Winklerbe9b7202015-05-07 15:54:04 +0300265static int mei_nfc_recv(struct nfc_mei_phy *phy, u8 *buf, size_t length)
266{
Tomas Winkler1d3ff76722015-05-07 15:54:06 +0300267 struct mei_nfc_hdr *hdr;
Tomas Winklerbe9b7202015-05-07 15:54:04 +0300268 int received_length;
269
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300270 received_length = mei_cldev_recv(phy->cldev, buf, length);
Tomas Winklerbe9b7202015-05-07 15:54:04 +0300271 if (received_length < 0)
272 return received_length;
273
Tomas Winkler1d3ff76722015-05-07 15:54:06 +0300274 hdr = (struct mei_nfc_hdr *) buf;
Tomas Winklerbe9b7202015-05-07 15:54:04 +0300275
Tomas Winkler1d3ff76722015-05-07 15:54:06 +0300276 MEI_DUMP_NFC_HDR("receive", hdr);
277 if (hdr->cmd == MEI_NFC_CMD_HCI_SEND) {
278 phy->recv_req_id = hdr->req_id;
Tomas Winklerbe9b7202015-05-07 15:54:04 +0300279 wake_up(&phy->send_wq);
280
281 return 0;
282 }
283
284 return received_length;
285}
286
287
Alexander Usyskin7c7a6072016-11-16 22:51:29 +0200288static void nfc_mei_rx_cb(struct mei_cl_device *cldev)
Eric Lapuyade4912e2f2013-04-15 11:19:20 +0200289{
Tomas Winkler972cedf2016-10-19 16:33:30 +0300290 struct nfc_mei_phy *phy = mei_cldev_get_drvdata(cldev);
Alexander Usyskin7c7a6072016-11-16 22:51:29 +0200291 struct sk_buff *skb;
292 int reply_size;
Tomas Winkler972cedf2016-10-19 16:33:30 +0300293
294 if (!phy)
295 return;
Eric Lapuyade4912e2f2013-04-15 11:19:20 +0200296
297 if (phy->hard_fault != 0)
298 return;
299
Alexander Usyskin7c7a6072016-11-16 22:51:29 +0200300 skb = alloc_skb(MEI_NFC_MAX_READ, GFP_KERNEL);
301 if (!skb)
302 return;
Eric Lapuyade4912e2f2013-04-15 11:19:20 +0200303
Alexander Usyskin7c7a6072016-11-16 22:51:29 +0200304 reply_size = mei_nfc_recv(phy, skb->data, MEI_NFC_MAX_READ);
305 if (reply_size < MEI_NFC_HEADER_SIZE) {
306 kfree_skb(skb);
307 return;
Eric Lapuyade4912e2f2013-04-15 11:19:20 +0200308 }
Alexander Usyskin7c7a6072016-11-16 22:51:29 +0200309
310 skb_put(skb, reply_size);
311 skb_pull(skb, MEI_NFC_HEADER_SIZE);
312
313 MEI_DUMP_SKB_IN("mei frame read", skb);
314
315 nfc_hci_recv_frame(phy->hdev, skb);
Eric Lapuyade4912e2f2013-04-15 11:19:20 +0200316}
Tomas Winklerbe9b7202015-05-07 15:54:04 +0300317
318static int nfc_mei_phy_enable(void *phy_id)
319{
320 int r;
321 struct nfc_mei_phy *phy = phy_id;
322
323 pr_info("%s\n", __func__);
324
325 if (phy->powered == 1)
326 return 0;
327
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300328 r = mei_cldev_enable(phy->cldev);
Tomas Winklerbe9b7202015-05-07 15:54:04 +0300329 if (r < 0) {
330 pr_err("Could not enable device %d\n", r);
331 return r;
332 }
333
334 r = mei_nfc_if_version(phy);
335 if (r < 0) {
336 pr_err("Could not enable device %d\n", r);
337 goto err;
338 }
339
340 r = mei_nfc_connect(phy);
341 if (r < 0) {
342 pr_err("Could not connect to device %d\n", r);
343 goto err;
344 }
345
Alexander Usyskin7c7a6072016-11-16 22:51:29 +0200346 r = mei_cldev_register_rx_cb(phy->cldev, nfc_mei_rx_cb);
Tomas Winklerbe9b7202015-05-07 15:54:04 +0300347 if (r) {
348 pr_err("Event cb registration failed %d\n", r);
349 goto err;
350 }
351
352 phy->powered = 1;
353
354 return 0;
355
356err:
357 phy->powered = 0;
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300358 mei_cldev_disable(phy->cldev);
Tomas Winklerbe9b7202015-05-07 15:54:04 +0300359 return r;
360}
361
362static void nfc_mei_phy_disable(void *phy_id)
363{
364 struct nfc_mei_phy *phy = phy_id;
365
366 pr_info("%s\n", __func__);
367
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300368 mei_cldev_disable(phy->cldev);
Tomas Winklerbe9b7202015-05-07 15:54:04 +0300369
370 phy->powered = 0;
371}
Eric Lapuyade4912e2f2013-04-15 11:19:20 +0200372
373struct nfc_phy_ops mei_phy_ops = {
374 .write = nfc_mei_phy_write,
375 .enable = nfc_mei_phy_enable,
376 .disable = nfc_mei_phy_disable,
377};
378EXPORT_SYMBOL_GPL(mei_phy_ops);
379
Tomas Winkler89391382015-09-10 10:18:04 +0300380struct nfc_mei_phy *nfc_mei_phy_alloc(struct mei_cl_device *cldev)
Eric Lapuyade4912e2f2013-04-15 11:19:20 +0200381{
382 struct nfc_mei_phy *phy;
383
384 phy = kzalloc(sizeof(struct nfc_mei_phy), GFP_KERNEL);
385 if (!phy)
386 return NULL;
387
Tomas Winkler89391382015-09-10 10:18:04 +0300388 phy->cldev = cldev;
Tomas Winklerbe9b7202015-05-07 15:54:04 +0300389 init_waitqueue_head(&phy->send_wq);
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300390 mei_cldev_set_drvdata(cldev, phy);
Eric Lapuyade4912e2f2013-04-15 11:19:20 +0200391
392 return phy;
393}
394EXPORT_SYMBOL_GPL(nfc_mei_phy_alloc);
395
396void nfc_mei_phy_free(struct nfc_mei_phy *phy)
397{
Tomas Winklerd49dc5e2015-09-10 10:18:05 +0300398 mei_cldev_disable(phy->cldev);
Eric Lapuyade4912e2f2013-04-15 11:19:20 +0200399 kfree(phy);
400}
401EXPORT_SYMBOL_GPL(nfc_mei_phy_free);
402
403MODULE_LICENSE("GPL");
404MODULE_DESCRIPTION("mei bus NFC device interface");