3 * Intel Management Engine Interface (Intel MEI) Linux driver
4 * Copyright (c) 2003-2013, Intel Corporation.
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 it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 #include <linux/kernel.h>
18 #include <linux/sched.h>
19 #include <linux/module.h>
20 #include <linux/moduleparam.h>
21 #include <linux/device.h>
22 #include <linux/slab.h>
24 #include <linux/mei_cl_bus.h>
39 struct mei_nfc_reply
{
50 struct mei_nfc_if_version
{
51 u8 radio_version_sw
[3];
53 u8 radio_version_hw
[3];
60 struct mei_nfc_connect
{
65 struct mei_nfc_connect_resp
{
74 struct mei_nfc_hci_hdr
{
82 #define MEI_NFC_CMD_MAINTENANCE 0x00
83 #define MEI_NFC_CMD_HCI_SEND 0x01
84 #define MEI_NFC_CMD_HCI_RECV 0x02
86 #define MEI_NFC_SUBCMD_CONNECT 0x00
87 #define MEI_NFC_SUBCMD_IF_VERSION 0x01
89 #define MEI_NFC_HEADER_SIZE 10
92 * struct mei_nfc_dev - NFC mei device
94 * @me_cl: NFC me client
95 * @cl: NFC host client
96 * @cl_info: NFC info host client
97 * @init_work: perform connection to the info client
98 * @fw_ivn: NFC Interface Version Number
99 * @vendor_id: NFC manufacturer ID
100 * @radio_type: NFC radio type
101 * @bus_name: bus name
105 struct mei_me_client
*me_cl
;
107 struct mei_cl
*cl_info
;
108 struct work_struct init_work
;
115 /* UUIDs for NFC F/W clients */
116 const uuid_le mei_nfc_guid
= UUID_LE(0x0bb17a78, 0x2a8e, 0x4c50,
117 0x94, 0xd4, 0x50, 0x26,
118 0x67, 0x23, 0x77, 0x5c);
120 static const uuid_le mei_nfc_info_guid
= UUID_LE(0xd2de1625, 0x382d, 0x417d,
121 0x48, 0xa4, 0xef, 0xab,
122 0xba, 0x8a, 0x12, 0x06);
125 #define MEI_NFC_VENDOR_INSIDE 0x00
126 #define MEI_NFC_VENDOR_NXP 0x01
129 #define MEI_NFC_VENDOR_INSIDE_UREAD 0x00
130 #define MEI_NFC_VENDOR_NXP_PN544 0x01
132 static void mei_nfc_free(struct mei_nfc_dev
*ndev
)
138 list_del(&ndev
->cl
->device_link
);
139 mei_cl_unlink(ndev
->cl
);
144 list_del(&ndev
->cl_info
->device_link
);
145 mei_cl_unlink(ndev
->cl_info
);
146 kfree(ndev
->cl_info
);
149 mei_me_cl_put(ndev
->me_cl
);
153 static int mei_nfc_build_bus_name(struct mei_nfc_dev
*ndev
)
155 struct mei_device
*dev
;
162 switch (ndev
->vendor_id
) {
163 case MEI_NFC_VENDOR_INSIDE
:
164 switch (ndev
->radio_type
) {
165 case MEI_NFC_VENDOR_INSIDE_UREAD
:
166 ndev
->bus_name
= "microread";
170 dev_err(dev
->dev
, "Unknown radio type 0x%x\n",
176 case MEI_NFC_VENDOR_NXP
:
177 switch (ndev
->radio_type
) {
178 case MEI_NFC_VENDOR_NXP_PN544
:
179 ndev
->bus_name
= "pn544";
182 dev_err(dev
->dev
, "Unknown radio type 0x%x\n",
189 dev_err(dev
->dev
, "Unknown vendor ID 0x%x\n",
198 static int mei_nfc_if_version(struct mei_nfc_dev
*ndev
)
200 struct mei_device
*dev
;
203 struct mei_nfc_cmd cmd
;
204 struct mei_nfc_reply
*reply
= NULL
;
205 struct mei_nfc_if_version
*version
;
206 size_t if_version_length
;
212 memset(&cmd
, 0, sizeof(struct mei_nfc_cmd
));
213 cmd
.command
= MEI_NFC_CMD_MAINTENANCE
;
215 cmd
.sub_command
= MEI_NFC_SUBCMD_IF_VERSION
;
217 ret
= __mei_cl_send(cl
, (u8
*)&cmd
, sizeof(struct mei_nfc_cmd
), 1);
219 dev_err(dev
->dev
, "Could not send IF version cmd\n");
223 /* to be sure on the stack we alloc memory */
224 if_version_length
= sizeof(struct mei_nfc_reply
) +
225 sizeof(struct mei_nfc_if_version
);
227 reply
= kzalloc(if_version_length
, GFP_KERNEL
);
231 bytes_recv
= __mei_cl_recv(cl
, (u8
*)reply
, if_version_length
);
232 if (bytes_recv
< 0 || bytes_recv
< sizeof(struct mei_nfc_reply
)) {
233 dev_err(dev
->dev
, "Could not read IF version\n");
238 version
= (struct mei_nfc_if_version
*)reply
->data
;
240 ndev
->fw_ivn
= version
->fw_ivn
;
241 ndev
->vendor_id
= version
->vendor_id
;
242 ndev
->radio_type
= version
->radio_type
;
249 static void mei_nfc_init(struct work_struct
*work
)
251 struct mei_device
*dev
;
252 struct mei_cl_device
*cldev
;
253 struct mei_nfc_dev
*ndev
;
254 struct mei_cl
*cl_info
;
255 struct mei_me_client
*me_cl_info
;
257 ndev
= container_of(work
, struct mei_nfc_dev
, init_work
);
259 cl_info
= ndev
->cl_info
;
262 mutex_lock(&dev
->device_lock
);
264 /* check for valid client id */
265 me_cl_info
= mei_me_cl_by_uuid(dev
, &mei_nfc_info_guid
);
267 mutex_unlock(&dev
->device_lock
);
268 dev_info(dev
->dev
, "nfc: failed to find the info client\n");
272 if (mei_cl_connect(cl_info
, me_cl_info
, NULL
) < 0) {
273 mei_me_cl_put(me_cl_info
);
274 mutex_unlock(&dev
->device_lock
);
275 dev_err(dev
->dev
, "Could not connect to the NFC INFO ME client");
279 mei_me_cl_put(me_cl_info
);
280 mutex_unlock(&dev
->device_lock
);
282 if (mei_nfc_if_version(ndev
) < 0) {
283 dev_err(dev
->dev
, "Could not get the NFC interface version");
288 dev_info(dev
->dev
, "NFC MEI VERSION: IVN 0x%x Vendor ID 0x%x Type 0x%x\n",
289 ndev
->fw_ivn
, ndev
->vendor_id
, ndev
->radio_type
);
291 mutex_lock(&dev
->device_lock
);
293 if (mei_cl_disconnect(cl_info
) < 0) {
294 mutex_unlock(&dev
->device_lock
);
295 dev_err(dev
->dev
, "Could not disconnect the NFC INFO ME client");
300 mutex_unlock(&dev
->device_lock
);
302 if (mei_nfc_build_bus_name(ndev
) < 0) {
303 dev_err(dev
->dev
, "Could not build the bus ID name\n");
307 cldev
= mei_cl_add_device(dev
, ndev
->me_cl
, ndev
->cl
,
310 dev_err(dev
->dev
, "Could not add the NFC device to the MEI bus\n");
315 cldev
->priv_data
= ndev
;
321 mutex_lock(&dev
->device_lock
);
323 mutex_unlock(&dev
->device_lock
);
328 int mei_nfc_host_init(struct mei_device
*dev
, struct mei_me_client
*me_cl
)
330 struct mei_nfc_dev
*ndev
;
331 struct mei_cl
*cl_info
, *cl
;
335 /* in case of internal reset bail out
336 * as the device is already setup
338 cl
= mei_cl_bus_find_cl_by_uuid(dev
, mei_nfc_guid
);
342 ndev
= kzalloc(sizeof(struct mei_nfc_dev
), GFP_KERNEL
);
348 ndev
->me_cl
= mei_me_cl_get(me_cl
);
354 cl_info
= mei_cl_alloc_linked(dev
, MEI_HOST_CLIENT_ID_ANY
);
355 if (IS_ERR(cl_info
)) {
356 ret
= PTR_ERR(cl_info
);
360 list_add_tail(&cl_info
->device_link
, &dev
->device_list
);
362 ndev
->cl_info
= cl_info
;
364 cl
= mei_cl_alloc_linked(dev
, MEI_HOST_CLIENT_ID_ANY
);
370 list_add_tail(&cl
->device_link
, &dev
->device_list
);
374 INIT_WORK(&ndev
->init_work
, mei_nfc_init
);
375 schedule_work(&ndev
->init_work
);
385 void mei_nfc_host_exit(struct mei_device
*dev
)
387 struct mei_nfc_dev
*ndev
;
389 struct mei_cl_device
*cldev
;
391 cl
= mei_cl_bus_find_cl_by_uuid(dev
, mei_nfc_guid
);
399 ndev
= (struct mei_nfc_dev
*)cldev
->priv_data
;
401 cancel_work_sync(&ndev
->init_work
);
403 cldev
->priv_data
= NULL
;
405 /* Need to remove the device here
406 * since mei_nfc_free will unlink the clients
408 mei_cl_remove_device(cldev
);
410 mutex_lock(&dev
->device_lock
);
412 mutex_unlock(&dev
->device_lock
);