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 * @cl: NFC host client
95 * @cl_info: NFC info host client
96 * @init_work: perform connection to the info client
97 * @send_wq: send completion wait queue
98 * @fw_ivn: NFC Interface Version Number
99 * @vendor_id: NFC manufacturer ID
100 * @radio_type: NFC radio type
101 * @bus_name: bus name
103 * @req_id: message counter
104 * @recv_req_id: reception message counter
108 struct mei_cl
*cl_info
;
109 struct work_struct init_work
;
110 wait_queue_head_t send_wq
;
120 static struct mei_nfc_dev nfc_dev
;
122 /* UUIDs for NFC F/W clients */
123 const uuid_le mei_nfc_guid
= UUID_LE(0x0bb17a78, 0x2a8e, 0x4c50,
124 0x94, 0xd4, 0x50, 0x26,
125 0x67, 0x23, 0x77, 0x5c);
127 static const uuid_le mei_nfc_info_guid
= UUID_LE(0xd2de1625, 0x382d, 0x417d,
128 0x48, 0xa4, 0xef, 0xab,
129 0xba, 0x8a, 0x12, 0x06);
132 #define MEI_NFC_VENDOR_INSIDE 0x00
133 #define MEI_NFC_VENDOR_NXP 0x01
136 #define MEI_NFC_VENDOR_INSIDE_UREAD 0x00
137 #define MEI_NFC_VENDOR_NXP_PN544 0x01
139 static void mei_nfc_free(struct mei_nfc_dev
*ndev
)
142 list_del(&ndev
->cl
->device_link
);
143 mei_cl_unlink(ndev
->cl
);
148 list_del(&ndev
->cl_info
->device_link
);
149 mei_cl_unlink(ndev
->cl_info
);
150 kfree(ndev
->cl_info
);
153 memset(ndev
, 0, sizeof(struct mei_nfc_dev
));
156 static int mei_nfc_build_bus_name(struct mei_nfc_dev
*ndev
)
158 struct mei_device
*dev
;
165 switch (ndev
->vendor_id
) {
166 case MEI_NFC_VENDOR_INSIDE
:
167 switch (ndev
->radio_type
) {
168 case MEI_NFC_VENDOR_INSIDE_UREAD
:
169 ndev
->bus_name
= "microread";
173 dev_err(dev
->dev
, "Unknown radio type 0x%x\n",
179 case MEI_NFC_VENDOR_NXP
:
180 switch (ndev
->radio_type
) {
181 case MEI_NFC_VENDOR_NXP_PN544
:
182 ndev
->bus_name
= "pn544";
185 dev_err(dev
->dev
, "Unknown radio type 0x%x\n",
192 dev_err(dev
->dev
, "Unknown vendor ID 0x%x\n",
201 static int mei_nfc_connect(struct mei_nfc_dev
*ndev
)
203 struct mei_device
*dev
;
205 struct mei_nfc_cmd
*cmd
, *reply
;
206 struct mei_nfc_connect
*connect
;
207 struct mei_nfc_connect_resp
*connect_resp
;
208 size_t connect_length
, connect_resp_length
;
214 connect_length
= sizeof(struct mei_nfc_cmd
) +
215 sizeof(struct mei_nfc_connect
);
217 connect_resp_length
= sizeof(struct mei_nfc_cmd
) +
218 sizeof(struct mei_nfc_connect_resp
);
220 cmd
= kzalloc(connect_length
, GFP_KERNEL
);
223 connect
= (struct mei_nfc_connect
*)cmd
->data
;
225 reply
= kzalloc(connect_resp_length
, GFP_KERNEL
);
231 connect_resp
= (struct mei_nfc_connect_resp
*)reply
->data
;
233 cmd
->command
= MEI_NFC_CMD_MAINTENANCE
;
235 cmd
->sub_command
= MEI_NFC_SUBCMD_CONNECT
;
236 connect
->fw_ivn
= ndev
->fw_ivn
;
237 connect
->vendor_id
= ndev
->vendor_id
;
239 ret
= __mei_cl_send(cl
, (u8
*)cmd
, connect_length
);
241 dev_err(dev
->dev
, "Could not send connect cmd\n");
245 bytes_recv
= __mei_cl_recv(cl
, (u8
*)reply
, connect_resp_length
);
246 if (bytes_recv
< 0) {
247 dev_err(dev
->dev
, "Could not read connect response\n");
252 dev_info(dev
->dev
, "IVN 0x%x Vendor ID 0x%x\n",
253 connect_resp
->fw_ivn
, connect_resp
->vendor_id
);
255 dev_info(dev
->dev
, "ME FW %d.%d.%d.%d\n",
256 connect_resp
->me_major
, connect_resp
->me_minor
,
257 connect_resp
->me_hotfix
, connect_resp
->me_build
);
268 static int mei_nfc_if_version(struct mei_nfc_dev
*ndev
)
270 struct mei_device
*dev
;
273 struct mei_nfc_cmd cmd
;
274 struct mei_nfc_reply
*reply
= NULL
;
275 struct mei_nfc_if_version
*version
;
276 size_t if_version_length
;
282 memset(&cmd
, 0, sizeof(struct mei_nfc_cmd
));
283 cmd
.command
= MEI_NFC_CMD_MAINTENANCE
;
285 cmd
.sub_command
= MEI_NFC_SUBCMD_IF_VERSION
;
287 ret
= __mei_cl_send(cl
, (u8
*)&cmd
, sizeof(struct mei_nfc_cmd
));
289 dev_err(dev
->dev
, "Could not send IF version cmd\n");
293 /* to be sure on the stack we alloc memory */
294 if_version_length
= sizeof(struct mei_nfc_reply
) +
295 sizeof(struct mei_nfc_if_version
);
297 reply
= kzalloc(if_version_length
, GFP_KERNEL
);
301 bytes_recv
= __mei_cl_recv(cl
, (u8
*)reply
, if_version_length
);
302 if (bytes_recv
< 0 || bytes_recv
< sizeof(struct mei_nfc_reply
)) {
303 dev_err(dev
->dev
, "Could not read IF version\n");
308 version
= (struct mei_nfc_if_version
*)reply
->data
;
310 ndev
->fw_ivn
= version
->fw_ivn
;
311 ndev
->vendor_id
= version
->vendor_id
;
312 ndev
->radio_type
= version
->radio_type
;
319 static int mei_nfc_enable(struct mei_cl_device
*cldev
)
321 struct mei_device
*dev
;
322 struct mei_nfc_dev
*ndev
= &nfc_dev
;
327 ret
= mei_nfc_connect(ndev
);
329 dev_err(dev
->dev
, "Could not connect to NFC");
336 static int mei_nfc_disable(struct mei_cl_device
*cldev
)
341 static int mei_nfc_send(struct mei_cl_device
*cldev
, u8
*buf
, size_t length
)
343 struct mei_device
*dev
;
344 struct mei_nfc_dev
*ndev
;
345 struct mei_nfc_hci_hdr
*hdr
;
349 ndev
= (struct mei_nfc_dev
*) cldev
->priv_data
;
353 mei_buf
= kzalloc(length
+ MEI_NFC_HEADER_SIZE
, GFP_KERNEL
);
357 hdr
= (struct mei_nfc_hci_hdr
*) mei_buf
;
358 hdr
->cmd
= MEI_NFC_CMD_HCI_SEND
;
360 hdr
->req_id
= ndev
->req_id
;
362 hdr
->data_size
= length
;
364 memcpy(mei_buf
+ MEI_NFC_HEADER_SIZE
, buf
, length
);
365 err
= __mei_cl_send(ndev
->cl
, mei_buf
, length
+ MEI_NFC_HEADER_SIZE
);
369 if (!wait_event_interruptible_timeout(ndev
->send_wq
,
370 ndev
->recv_req_id
== ndev
->req_id
, HZ
)) {
371 dev_err(dev
->dev
, "NFC MEI command timeout\n");
381 static int mei_nfc_recv(struct mei_cl_device
*cldev
, u8
*buf
, size_t length
)
383 struct mei_nfc_dev
*ndev
;
384 struct mei_nfc_hci_hdr
*hci_hdr
;
387 ndev
= (struct mei_nfc_dev
*)cldev
->priv_data
;
389 received_length
= __mei_cl_recv(ndev
->cl
, buf
, length
);
390 if (received_length
< 0)
391 return received_length
;
393 hci_hdr
= (struct mei_nfc_hci_hdr
*) buf
;
395 if (hci_hdr
->cmd
== MEI_NFC_CMD_HCI_SEND
) {
396 ndev
->recv_req_id
= hci_hdr
->req_id
;
397 wake_up(&ndev
->send_wq
);
402 return received_length
;
405 static struct mei_cl_ops nfc_ops
= {
406 .enable
= mei_nfc_enable
,
407 .disable
= mei_nfc_disable
,
408 .send
= mei_nfc_send
,
409 .recv
= mei_nfc_recv
,
412 static void mei_nfc_init(struct work_struct
*work
)
414 struct mei_device
*dev
;
415 struct mei_cl_device
*cldev
;
416 struct mei_nfc_dev
*ndev
;
417 struct mei_cl
*cl_info
;
419 ndev
= container_of(work
, struct mei_nfc_dev
, init_work
);
421 cl_info
= ndev
->cl_info
;
424 mutex_lock(&dev
->device_lock
);
426 if (mei_cl_connect(cl_info
, NULL
) < 0) {
427 mutex_unlock(&dev
->device_lock
);
428 dev_err(dev
->dev
, "Could not connect to the NFC INFO ME client");
433 mutex_unlock(&dev
->device_lock
);
435 if (mei_nfc_if_version(ndev
) < 0) {
436 dev_err(dev
->dev
, "Could not get the NFC interface version");
441 dev_info(dev
->dev
, "NFC MEI VERSION: IVN 0x%x Vendor ID 0x%x Type 0x%x\n",
442 ndev
->fw_ivn
, ndev
->vendor_id
, ndev
->radio_type
);
444 mutex_lock(&dev
->device_lock
);
446 if (mei_cl_disconnect(cl_info
) < 0) {
447 mutex_unlock(&dev
->device_lock
);
448 dev_err(dev
->dev
, "Could not disconnect the NFC INFO ME client");
453 mutex_unlock(&dev
->device_lock
);
455 if (mei_nfc_build_bus_name(ndev
) < 0) {
456 dev_err(dev
->dev
, "Could not build the bus ID name\n");
460 cldev
= mei_cl_add_device(dev
, mei_nfc_guid
, ndev
->bus_name
, &nfc_ops
);
462 dev_err(dev
->dev
, "Could not add the NFC device to the MEI bus\n");
467 cldev
->priv_data
= ndev
;
473 mutex_lock(&dev
->device_lock
);
475 mutex_unlock(&dev
->device_lock
);
480 int mei_nfc_host_init(struct mei_device
*dev
)
482 struct mei_nfc_dev
*ndev
= &nfc_dev
;
483 struct mei_cl
*cl_info
, *cl
= NULL
;
484 struct mei_me_client
*me_cl
;
487 /* already initialized */
491 ndev
->cl_info
= mei_cl_allocate(dev
);
492 ndev
->cl
= mei_cl_allocate(dev
);
495 cl_info
= ndev
->cl_info
;
497 if (!cl
|| !cl_info
) {
502 /* check for valid client id */
503 me_cl
= mei_me_cl_by_uuid(dev
, &mei_nfc_info_guid
);
505 dev_info(dev
->dev
, "nfc: failed to find the client\n");
510 cl_info
->me_client_id
= me_cl
->client_id
;
511 cl_info
->cl_uuid
= me_cl
->props
.protocol_name
;
513 ret
= mei_cl_link(cl_info
, MEI_HOST_CLIENT_ID_ANY
);
518 list_add_tail(&cl_info
->device_link
, &dev
->device_list
);
520 /* check for valid client id */
521 me_cl
= mei_me_cl_by_uuid(dev
, &mei_nfc_guid
);
523 dev_info(dev
->dev
, "nfc: failed to find the client\n");
528 cl
->me_client_id
= me_cl
->client_id
;
529 cl
->cl_uuid
= me_cl
->props
.protocol_name
;
531 ret
= mei_cl_link(cl
, MEI_HOST_CLIENT_ID_ANY
);
535 list_add_tail(&cl
->device_link
, &dev
->device_list
);
539 INIT_WORK(&ndev
->init_work
, mei_nfc_init
);
540 init_waitqueue_head(&ndev
->send_wq
);
541 schedule_work(&ndev
->init_work
);
551 void mei_nfc_host_exit(struct mei_device
*dev
)
553 struct mei_nfc_dev
*ndev
= &nfc_dev
;
555 cancel_work_sync(&ndev
->init_work
);