1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2013, Intel Corporation.
5 * MEI Library for mei bus nfc device access
7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9 #include <linux/module.h>
10 #include <linux/slab.h>
11 #include <linux/nfc.h>
24 struct mei_nfc_hdr hdr
;
29 struct mei_nfc_reply
{
30 struct mei_nfc_hdr hdr
;
36 struct mei_nfc_if_version
{
37 u8 radio_version_sw
[3];
39 u8 radio_version_hw
[3];
46 struct mei_nfc_connect
{
51 struct mei_nfc_connect_resp
{
61 #define MEI_NFC_CMD_MAINTENANCE 0x00
62 #define MEI_NFC_CMD_HCI_SEND 0x01
63 #define MEI_NFC_CMD_HCI_RECV 0x02
65 #define MEI_NFC_SUBCMD_CONNECT 0x00
66 #define MEI_NFC_SUBCMD_IF_VERSION 0x01
68 #define MEI_NFC_MAX_READ (MEI_NFC_HEADER_SIZE + MEI_NFC_MAX_HCI_PAYLOAD)
70 #define MEI_DUMP_SKB_IN(info, skb) \
72 pr_debug("%s:\n", info); \
73 print_hex_dump_debug("mei in : ", DUMP_PREFIX_OFFSET, \
74 16, 1, (skb)->data, (skb)->len, false); \
77 #define MEI_DUMP_SKB_OUT(info, skb) \
79 pr_debug("%s:\n", info); \
80 print_hex_dump_debug("mei out: ", DUMP_PREFIX_OFFSET, \
81 16, 1, (skb)->data, (skb)->len, false); \
84 #define MEI_DUMP_NFC_HDR(info, _hdr) \
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); \
92 static int mei_nfc_if_version(struct nfc_mei_phy
*phy
)
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
;
101 pr_info("%s\n", __func__
);
103 memset(&cmd
, 0, sizeof(struct mei_nfc_cmd
));
104 cmd
.hdr
.cmd
= MEI_NFC_CMD_MAINTENANCE
;
105 cmd
.hdr
.data_size
= 1;
106 cmd
.sub_command
= MEI_NFC_SUBCMD_IF_VERSION
;
108 MEI_DUMP_NFC_HDR("version", &cmd
.hdr
);
109 r
= mei_cldev_send(phy
->cldev
, (u8
*)&cmd
, sizeof(struct mei_nfc_cmd
));
111 pr_err("Could not send IF version cmd\n");
115 /* 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
);
119 reply
= kzalloc(if_version_length
, GFP_KERNEL
);
123 bytes_recv
= mei_cldev_recv(phy
->cldev
, (u8
*)reply
, if_version_length
);
124 if (bytes_recv
< 0 || bytes_recv
< if_version_length
) {
125 pr_err("Could not read IF version\n");
130 version
= (struct mei_nfc_if_version
*)reply
->data
;
132 phy
->fw_ivn
= version
->fw_ivn
;
133 phy
->vendor_id
= version
->vendor_id
;
134 phy
->radio_type
= version
->radio_type
;
141 static int mei_nfc_connect(struct nfc_mei_phy
*phy
)
143 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
;
149 pr_info("%s\n", __func__
);
151 connect_length
= sizeof(struct mei_nfc_cmd
) +
152 sizeof(struct mei_nfc_connect
);
154 connect_resp_length
= sizeof(struct mei_nfc_cmd
) +
155 sizeof(struct mei_nfc_connect_resp
);
157 cmd
= kzalloc(connect_length
, GFP_KERNEL
);
160 connect
= (struct mei_nfc_connect
*)cmd
->data
;
162 reply
= kzalloc(connect_resp_length
, GFP_KERNEL
);
168 connect_resp
= (struct mei_nfc_connect_resp
*)reply
->data
;
170 cmd
->hdr
.cmd
= MEI_NFC_CMD_MAINTENANCE
;
171 cmd
->hdr
.data_size
= 3;
172 cmd
->sub_command
= MEI_NFC_SUBCMD_CONNECT
;
173 connect
->fw_ivn
= phy
->fw_ivn
;
174 connect
->vendor_id
= phy
->vendor_id
;
176 MEI_DUMP_NFC_HDR("connect request", &cmd
->hdr
);
177 r
= mei_cldev_send(phy
->cldev
, (u8
*)cmd
, connect_length
);
179 pr_err("Could not send connect cmd %d\n", r
);
183 bytes_recv
= mei_cldev_recv(phy
->cldev
, (u8
*)reply
,
184 connect_resp_length
);
185 if (bytes_recv
< 0) {
187 pr_err("Could not read connect response %d\n", r
);
191 MEI_DUMP_NFC_HDR("connect reply", &reply
->hdr
);
193 pr_info("IVN 0x%x Vendor ID 0x%x\n",
194 connect_resp
->fw_ivn
, connect_resp
->vendor_id
);
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
);
209 static int mei_nfc_send(struct nfc_mei_phy
*phy
, u8
*buf
, size_t length
)
211 struct mei_nfc_hdr
*hdr
;
216 mei_buf
= kzalloc(length
+ MEI_NFC_HEADER_SIZE
, GFP_KERNEL
);
220 hdr
= (struct mei_nfc_hdr
*)mei_buf
;
221 hdr
->cmd
= MEI_NFC_CMD_HCI_SEND
;
223 hdr
->req_id
= phy
->req_id
;
225 hdr
->data_size
= length
;
227 MEI_DUMP_NFC_HDR("send", hdr
);
229 memcpy(mei_buf
+ MEI_NFC_HEADER_SIZE
, buf
, length
);
230 err
= mei_cldev_send(phy
->cldev
, mei_buf
, length
+ MEI_NFC_HEADER_SIZE
);
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");
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
251 static int nfc_mei_phy_write(void *phy_id
, struct sk_buff
*skb
)
253 struct nfc_mei_phy
*phy
= phy_id
;
256 MEI_DUMP_SKB_OUT("mei frame sent", skb
);
258 r
= mei_nfc_send(phy
, skb
->data
, skb
->len
);
265 static int mei_nfc_recv(struct nfc_mei_phy
*phy
, u8
*buf
, size_t length
)
267 struct mei_nfc_hdr
*hdr
;
270 received_length
= mei_cldev_recv(phy
->cldev
, buf
, length
);
271 if (received_length
< 0)
272 return received_length
;
274 hdr
= (struct mei_nfc_hdr
*) buf
;
276 MEI_DUMP_NFC_HDR("receive", hdr
);
277 if (hdr
->cmd
== MEI_NFC_CMD_HCI_SEND
) {
278 phy
->recv_req_id
= hdr
->req_id
;
279 wake_up(&phy
->send_wq
);
284 return received_length
;
288 static void nfc_mei_rx_cb(struct mei_cl_device
*cldev
)
290 struct nfc_mei_phy
*phy
= mei_cldev_get_drvdata(cldev
);
297 if (phy
->hard_fault
!= 0)
300 skb
= alloc_skb(MEI_NFC_MAX_READ
, GFP_KERNEL
);
304 reply_size
= mei_nfc_recv(phy
, skb
->data
, MEI_NFC_MAX_READ
);
305 if (reply_size
< MEI_NFC_HEADER_SIZE
) {
310 skb_put(skb
, reply_size
);
311 skb_pull(skb
, MEI_NFC_HEADER_SIZE
);
313 MEI_DUMP_SKB_IN("mei frame read", skb
);
315 nfc_hci_recv_frame(phy
->hdev
, skb
);
318 static int nfc_mei_phy_enable(void *phy_id
)
321 struct nfc_mei_phy
*phy
= phy_id
;
323 pr_info("%s\n", __func__
);
325 if (phy
->powered
== 1)
328 r
= mei_cldev_enable(phy
->cldev
);
330 pr_err("Could not enable device %d\n", r
);
334 r
= mei_nfc_if_version(phy
);
336 pr_err("Could not enable device %d\n", r
);
340 r
= mei_nfc_connect(phy
);
342 pr_err("Could not connect to device %d\n", r
);
346 r
= mei_cldev_register_rx_cb(phy
->cldev
, nfc_mei_rx_cb
);
348 pr_err("Event cb registration failed %d\n", r
);
358 mei_cldev_disable(phy
->cldev
);
362 static void nfc_mei_phy_disable(void *phy_id
)
364 struct nfc_mei_phy
*phy
= phy_id
;
366 pr_info("%s\n", __func__
);
368 mei_cldev_disable(phy
->cldev
);
373 struct nfc_phy_ops mei_phy_ops
= {
374 .write
= nfc_mei_phy_write
,
375 .enable
= nfc_mei_phy_enable
,
376 .disable
= nfc_mei_phy_disable
,
378 EXPORT_SYMBOL_GPL(mei_phy_ops
);
380 struct nfc_mei_phy
*nfc_mei_phy_alloc(struct mei_cl_device
*cldev
)
382 struct nfc_mei_phy
*phy
;
384 phy
= kzalloc(sizeof(struct nfc_mei_phy
), GFP_KERNEL
);
389 init_waitqueue_head(&phy
->send_wq
);
390 mei_cldev_set_drvdata(cldev
, phy
);
394 EXPORT_SYMBOL_GPL(nfc_mei_phy_alloc
);
396 void nfc_mei_phy_free(struct nfc_mei_phy
*phy
)
398 mei_cldev_disable(phy
->cldev
);
401 EXPORT_SYMBOL_GPL(nfc_mei_phy_free
);
403 MODULE_LICENSE("GPL");
404 MODULE_DESCRIPTION("mei bus NFC device interface");