blob: 865e33bcd2261ce033d23ae4d22fbf0bb1097462 [file] [log] [blame]
Samuel Ortiz59fcd7c2013-04-11 03:03:29 +02001/*
2 *
3 * Intel Management Engine Interface (Intel MEI) Linux driver
4 * Copyright (c) 2003-2013, Intel Corporation.
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 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
13 * more details.
14 *
15 */
16
17#include <linux/kernel.h>
Samuel Ortiz36eda942013-04-11 03:03:31 +020018#include <linux/sched.h>
Samuel Ortiz59fcd7c2013-04-11 03:03:29 +020019#include <linux/module.h>
20#include <linux/moduleparam.h>
21#include <linux/device.h>
Tomas Winkler1f180352014-09-29 16:31:46 +030022#include <linux/slab.h>
Tomas Winkler71ce7892015-07-23 15:08:43 +030023#include <linux/uuid.h>
Tomas Winkler1f180352014-09-29 16:31:46 +030024
Samuel Ortiz59fcd7c2013-04-11 03:03:29 +020025#include <linux/mei_cl_bus.h>
26
27#include "mei_dev.h"
28#include "client.h"
29
Tomas Winkler71ce7892015-07-23 15:08:43 +030030#define MEI_UUID_ANY NULL_UUID_LE
31
Samuel Ortiz59fcd7c2013-04-11 03:03:29 +020032struct mei_nfc_cmd {
33 u8 command;
34 u8 status;
35 u16 req_id;
36 u32 reserved;
37 u16 data_size;
38 u8 sub_command;
39 u8 data[];
40} __packed;
41
42struct mei_nfc_reply {
43 u8 command;
44 u8 status;
45 u16 req_id;
46 u32 reserved;
47 u16 data_size;
48 u8 sub_command;
49 u8 reply_status;
50 u8 data[];
51} __packed;
52
53struct mei_nfc_if_version {
54 u8 radio_version_sw[3];
55 u8 reserved[3];
56 u8 radio_version_hw[3];
57 u8 i2c_addr;
58 u8 fw_ivn;
59 u8 vendor_id;
60 u8 radio_type;
61} __packed;
62
63struct mei_nfc_connect {
64 u8 fw_ivn;
65 u8 vendor_id;
66} __packed;
67
68struct mei_nfc_connect_resp {
69 u8 fw_ivn;
70 u8 vendor_id;
71 u16 me_major;
72 u16 me_minor;
73 u16 me_hotfix;
74 u16 me_build;
75} __packed;
76
77struct mei_nfc_hci_hdr {
78 u8 cmd;
79 u8 status;
80 u16 req_id;
81 u32 reserved;
82 u16 data_size;
83} __packed;
84
85#define MEI_NFC_CMD_MAINTENANCE 0x00
86#define MEI_NFC_CMD_HCI_SEND 0x01
87#define MEI_NFC_CMD_HCI_RECV 0x02
88
89#define MEI_NFC_SUBCMD_CONNECT 0x00
90#define MEI_NFC_SUBCMD_IF_VERSION 0x01
91
92#define MEI_NFC_HEADER_SIZE 10
93
Alexander Usyskina8605ea2014-09-29 16:31:49 +030094/**
95 * struct mei_nfc_dev - NFC mei device
Samuel Ortiz59fcd7c2013-04-11 03:03:29 +020096 *
Alexander Usyskind49ed642015-05-04 09:43:54 +030097 * @me_cl: NFC me client
Samuel Ortiz59fcd7c2013-04-11 03:03:29 +020098 * @cl: NFC host client
99 * @cl_info: NFC info host client
100 * @init_work: perform connection to the info client
Alexander Usyskin83ce0742014-01-08 22:31:46 +0200101 * @fw_ivn: NFC Interface Version Number
Samuel Ortiz59fcd7c2013-04-11 03:03:29 +0200102 * @vendor_id: NFC manufacturer ID
103 * @radio_type: NFC radio type
Alexander Usyskince231392014-09-29 16:31:50 +0300104 * @bus_name: bus name
105 *
Samuel Ortiz59fcd7c2013-04-11 03:03:29 +0200106 */
107struct mei_nfc_dev {
Alexander Usyskind49ed642015-05-04 09:43:54 +0300108 struct mei_me_client *me_cl;
Samuel Ortiz59fcd7c2013-04-11 03:03:29 +0200109 struct mei_cl *cl;
110 struct mei_cl *cl_info;
111 struct work_struct init_work;
112 u8 fw_ivn;
113 u8 vendor_id;
114 u8 radio_type;
Samuel Ortiz91a6b952013-04-11 03:03:30 +0200115 char *bus_name;
Samuel Ortiz59fcd7c2013-04-11 03:03:29 +0200116};
117
Samuel Ortiz59fcd7c2013-04-11 03:03:29 +0200118/* UUIDs for NFC F/W clients */
119const uuid_le mei_nfc_guid = UUID_LE(0x0bb17a78, 0x2a8e, 0x4c50,
120 0x94, 0xd4, 0x50, 0x26,
121 0x67, 0x23, 0x77, 0x5c);
122
123static const uuid_le mei_nfc_info_guid = UUID_LE(0xd2de1625, 0x382d, 0x417d,
124 0x48, 0xa4, 0xef, 0xab,
125 0xba, 0x8a, 0x12, 0x06);
126
Samuel Ortiz91a6b952013-04-11 03:03:30 +0200127/* Vendors */
128#define MEI_NFC_VENDOR_INSIDE 0x00
129#define MEI_NFC_VENDOR_NXP 0x01
130
131/* Radio types */
132#define MEI_NFC_VENDOR_INSIDE_UREAD 0x00
133#define MEI_NFC_VENDOR_NXP_PN544 0x01
134
Samuel Ortiz59fcd7c2013-04-11 03:03:29 +0200135static void mei_nfc_free(struct mei_nfc_dev *ndev)
136{
Tomas Winklera176c242014-11-05 18:18:52 +0200137 if (!ndev)
138 return;
139
Samuel Ortiz59fcd7c2013-04-11 03:03:29 +0200140 if (ndev->cl) {
141 list_del(&ndev->cl->device_link);
142 mei_cl_unlink(ndev->cl);
143 kfree(ndev->cl);
144 }
145
146 if (ndev->cl_info) {
147 list_del(&ndev->cl_info->device_link);
148 mei_cl_unlink(ndev->cl_info);
149 kfree(ndev->cl_info);
150 }
Tomas Winkler2753ff52013-06-10 10:10:26 +0300151
Alexander Usyskind49ed642015-05-04 09:43:54 +0300152 mei_me_cl_put(ndev->me_cl);
Tomas Winklera176c242014-11-05 18:18:52 +0200153 kfree(ndev);
Samuel Ortiz59fcd7c2013-04-11 03:03:29 +0200154}
155
Samuel Ortiz91a6b952013-04-11 03:03:30 +0200156static int mei_nfc_build_bus_name(struct mei_nfc_dev *ndev)
157{
Tomas Winklerb37719c32015-07-23 15:08:33 +0300158 struct mei_device *bus;
Samuel Ortiz91a6b952013-04-11 03:03:30 +0200159
160 if (!ndev->cl)
161 return -ENODEV;
162
Tomas Winklerb37719c32015-07-23 15:08:33 +0300163 bus = ndev->cl->dev;
Samuel Ortiz91a6b952013-04-11 03:03:30 +0200164
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";
170 return 0;
171
172 default:
Tomas Winklerb37719c32015-07-23 15:08:33 +0300173 dev_err(bus->dev, "Unknown radio type 0x%x\n",
Samuel Ortiz91a6b952013-04-11 03:03:30 +0200174 ndev->radio_type);
175
176 return -EINVAL;
177 }
178
179 case MEI_NFC_VENDOR_NXP:
180 switch (ndev->radio_type) {
181 case MEI_NFC_VENDOR_NXP_PN544:
182 ndev->bus_name = "pn544";
183 return 0;
184 default:
Tomas Winklerb37719c32015-07-23 15:08:33 +0300185 dev_err(bus->dev, "Unknown radio type 0x%x\n",
Samuel Ortiz91a6b952013-04-11 03:03:30 +0200186 ndev->radio_type);
187
188 return -EINVAL;
189 }
190
191 default:
Tomas Winklerb37719c32015-07-23 15:08:33 +0300192 dev_err(bus->dev, "Unknown vendor ID 0x%x\n",
Samuel Ortiz91a6b952013-04-11 03:03:30 +0200193 ndev->vendor_id);
194
195 return -EINVAL;
196 }
197
198 return 0;
199}
200
Samuel Ortiz59fcd7c2013-04-11 03:03:29 +0200201static int mei_nfc_if_version(struct mei_nfc_dev *ndev)
202{
Tomas Winklerb37719c32015-07-23 15:08:33 +0300203 struct mei_device *bus;
Samuel Ortiz59fcd7c2013-04-11 03:03:29 +0200204 struct mei_cl *cl;
205
206 struct mei_nfc_cmd cmd;
207 struct mei_nfc_reply *reply = NULL;
208 struct mei_nfc_if_version *version;
209 size_t if_version_length;
210 int bytes_recv, ret;
211
212 cl = ndev->cl_info;
Tomas Winklerb37719c32015-07-23 15:08:33 +0300213 bus = cl->dev;
Samuel Ortiz59fcd7c2013-04-11 03:03:29 +0200214
215 memset(&cmd, 0, sizeof(struct mei_nfc_cmd));
216 cmd.command = MEI_NFC_CMD_MAINTENANCE;
217 cmd.data_size = 1;
218 cmd.sub_command = MEI_NFC_SUBCMD_IF_VERSION;
219
Tomas Winklerbe9b7202015-05-07 15:54:04 +0300220 ret = __mei_cl_send(cl, (u8 *)&cmd, sizeof(struct mei_nfc_cmd), 1);
Samuel Ortiz59fcd7c2013-04-11 03:03:29 +0200221 if (ret < 0) {
Tomas Winklerb37719c32015-07-23 15:08:33 +0300222 dev_err(bus->dev, "Could not send IF version cmd\n");
Samuel Ortiz59fcd7c2013-04-11 03:03:29 +0200223 return ret;
224 }
225
226 /* to be sure on the stack we alloc memory */
227 if_version_length = sizeof(struct mei_nfc_reply) +
228 sizeof(struct mei_nfc_if_version);
229
230 reply = kzalloc(if_version_length, GFP_KERNEL);
231 if (!reply)
232 return -ENOMEM;
233
234 bytes_recv = __mei_cl_recv(cl, (u8 *)reply, if_version_length);
235 if (bytes_recv < 0 || bytes_recv < sizeof(struct mei_nfc_reply)) {
Tomas Winklerb37719c32015-07-23 15:08:33 +0300236 dev_err(bus->dev, "Could not read IF version\n");
Samuel Ortiz59fcd7c2013-04-11 03:03:29 +0200237 ret = -EIO;
238 goto err;
239 }
240
241 version = (struct mei_nfc_if_version *)reply->data;
242
243 ndev->fw_ivn = version->fw_ivn;
244 ndev->vendor_id = version->vendor_id;
245 ndev->radio_type = version->radio_type;
246
247err:
248 kfree(reply);
249 return ret;
250}
251
252static void mei_nfc_init(struct work_struct *work)
253{
Tomas Winklerb37719c32015-07-23 15:08:33 +0300254 struct mei_device *bus;
Samuel Ortiz91a6b952013-04-11 03:03:30 +0200255 struct mei_cl_device *cldev;
Samuel Ortiz59fcd7c2013-04-11 03:03:29 +0200256 struct mei_nfc_dev *ndev;
257 struct mei_cl *cl_info;
Alexander Usyskind49ed642015-05-04 09:43:54 +0300258 struct mei_me_client *me_cl_info;
Samuel Ortiz59fcd7c2013-04-11 03:03:29 +0200259
260 ndev = container_of(work, struct mei_nfc_dev, init_work);
261
262 cl_info = ndev->cl_info;
Tomas Winklerb37719c32015-07-23 15:08:33 +0300263 bus = cl_info->dev;
Samuel Ortiz59fcd7c2013-04-11 03:03:29 +0200264
Tomas Winklerb37719c32015-07-23 15:08:33 +0300265 mutex_lock(&bus->device_lock);
Samuel Ortiz59fcd7c2013-04-11 03:03:29 +0200266
Alexander Usyskind49ed642015-05-04 09:43:54 +0300267 /* check for valid client id */
Tomas Winklerb37719c32015-07-23 15:08:33 +0300268 me_cl_info = mei_me_cl_by_uuid(bus, &mei_nfc_info_guid);
Alexander Usyskind49ed642015-05-04 09:43:54 +0300269 if (!me_cl_info) {
Tomas Winklerb37719c32015-07-23 15:08:33 +0300270 mutex_unlock(&bus->device_lock);
271 dev_info(bus->dev, "nfc: failed to find the info client\n");
Alexander Usyskind49ed642015-05-04 09:43:54 +0300272 goto err;
273 }
274
275 if (mei_cl_connect(cl_info, me_cl_info, NULL) < 0) {
276 mei_me_cl_put(me_cl_info);
Tomas Winklerb37719c32015-07-23 15:08:33 +0300277 mutex_unlock(&bus->device_lock);
278 dev_err(bus->dev, "Could not connect to the NFC INFO ME client");
Samuel Ortiz59fcd7c2013-04-11 03:03:29 +0200279
280 goto err;
281 }
Alexander Usyskind49ed642015-05-04 09:43:54 +0300282 mei_me_cl_put(me_cl_info);
Tomas Winklerb37719c32015-07-23 15:08:33 +0300283 mutex_unlock(&bus->device_lock);
Samuel Ortiz59fcd7c2013-04-11 03:03:29 +0200284
285 if (mei_nfc_if_version(ndev) < 0) {
Tomas Winklerb37719c32015-07-23 15:08:33 +0300286 dev_err(bus->dev, "Could not get the NFC interface version");
Samuel Ortiz59fcd7c2013-04-11 03:03:29 +0200287
288 goto err;
289 }
290
Tomas Winklerb37719c32015-07-23 15:08:33 +0300291 dev_info(bus->dev, "NFC MEI VERSION: IVN 0x%x Vendor ID 0x%x Type 0x%x\n",
Samuel Ortiz59fcd7c2013-04-11 03:03:29 +0200292 ndev->fw_ivn, ndev->vendor_id, ndev->radio_type);
293
Tomas Winklerb37719c32015-07-23 15:08:33 +0300294 mutex_lock(&bus->device_lock);
Samuel Ortiz59fcd7c2013-04-11 03:03:29 +0200295
296 if (mei_cl_disconnect(cl_info) < 0) {
Tomas Winklerb37719c32015-07-23 15:08:33 +0300297 mutex_unlock(&bus->device_lock);
298 dev_err(bus->dev, "Could not disconnect the NFC INFO ME client");
Samuel Ortiz59fcd7c2013-04-11 03:03:29 +0200299
300 goto err;
301 }
302
Tomas Winklerb37719c32015-07-23 15:08:33 +0300303 mutex_unlock(&bus->device_lock);
Samuel Ortiz59fcd7c2013-04-11 03:03:29 +0200304
Samuel Ortiz91a6b952013-04-11 03:03:30 +0200305 if (mei_nfc_build_bus_name(ndev) < 0) {
Tomas Winklerb37719c32015-07-23 15:08:33 +0300306 dev_err(bus->dev, "Could not build the bus ID name\n");
Samuel Ortiz91a6b952013-04-11 03:03:30 +0200307 return;
308 }
309
Tomas Winklerb37719c32015-07-23 15:08:33 +0300310 cldev = mei_cl_add_device(bus, ndev->me_cl, ndev->cl,
Tomas Winklerbe9b7202015-05-07 15:54:04 +0300311 ndev->bus_name);
Samuel Ortiz91a6b952013-04-11 03:03:30 +0200312 if (!cldev) {
Tomas Winklerb37719c32015-07-23 15:08:33 +0300313 dev_err(bus->dev, "Could not add the NFC device to the MEI bus\n");
Samuel Ortiz91a6b952013-04-11 03:03:30 +0200314
315 goto err;
316 }
317
318 cldev->priv_data = ndev;
319
320
Samuel Ortiz59fcd7c2013-04-11 03:03:29 +0200321 return;
322
323err:
Tomas Winklerb37719c32015-07-23 15:08:33 +0300324 mutex_lock(&bus->device_lock);
Samuel Ortiz59fcd7c2013-04-11 03:03:29 +0200325 mei_nfc_free(ndev);
Tomas Winklerb37719c32015-07-23 15:08:33 +0300326 mutex_unlock(&bus->device_lock);
Samuel Ortiz59fcd7c2013-04-11 03:03:29 +0200327
Samuel Ortiz59fcd7c2013-04-11 03:03:29 +0200328}
329
330
Tomas Winklerb37719c32015-07-23 15:08:33 +0300331int mei_nfc_host_init(struct mei_device *bus, struct mei_me_client *me_cl)
Samuel Ortiz59fcd7c2013-04-11 03:03:29 +0200332{
Tomas Winklera176c242014-11-05 18:18:52 +0200333 struct mei_nfc_dev *ndev;
Tomas Winkler03b8d342015-02-10 10:39:44 +0200334 struct mei_cl *cl_info, *cl;
Tomas Winklerd3208322014-08-24 12:08:55 +0300335 int ret;
Samuel Ortiz59fcd7c2013-04-11 03:03:29 +0200336
Tomas Winklera176c242014-11-05 18:18:52 +0200337
338 /* in case of internal reset bail out
339 * as the device is already setup
340 */
Tomas Winklerb37719c32015-07-23 15:08:33 +0300341 cl = mei_cl_bus_find_cl_by_uuid(bus, mei_nfc_guid);
Tomas Winklera176c242014-11-05 18:18:52 +0200342 if (cl)
Samuel Ortiz59fcd7c2013-04-11 03:03:29 +0200343 return 0;
344
Tomas Winklera176c242014-11-05 18:18:52 +0200345 ndev = kzalloc(sizeof(struct mei_nfc_dev), GFP_KERNEL);
346 if (!ndev) {
347 ret = -ENOMEM;
348 goto err;
349 }
350
Alexander Usyskind49ed642015-05-04 09:43:54 +0300351 ndev->me_cl = mei_me_cl_get(me_cl);
352 if (!ndev->me_cl) {
353 ret = -ENODEV;
Samuel Ortiz59fcd7c2013-04-11 03:03:29 +0200354 goto err;
355 }
356
Tomas Winklerb37719c32015-07-23 15:08:33 +0300357 cl_info = mei_cl_alloc_linked(bus, MEI_HOST_CLIENT_ID_ANY);
Tomas Winkler03b8d342015-02-10 10:39:44 +0200358 if (IS_ERR(cl_info)) {
359 ret = PTR_ERR(cl_info);
360 goto err;
361 }
362
Tomas Winklerb37719c32015-07-23 15:08:33 +0300363 list_add_tail(&cl_info->device_link, &bus->device_list);
Samuel Ortiz59fcd7c2013-04-11 03:03:29 +0200364
Tomas Winkler03b8d342015-02-10 10:39:44 +0200365 ndev->cl_info = cl_info;
366
Tomas Winklerb37719c32015-07-23 15:08:33 +0300367 cl = mei_cl_alloc_linked(bus, MEI_HOST_CLIENT_ID_ANY);
Tomas Winkler03b8d342015-02-10 10:39:44 +0200368 if (IS_ERR(cl)) {
369 ret = PTR_ERR(cl);
370 goto err;
371 }
372
Tomas Winklerb37719c32015-07-23 15:08:33 +0300373 list_add_tail(&cl->device_link, &bus->device_list);
Samuel Ortiz59fcd7c2013-04-11 03:03:29 +0200374
Tomas Winkler03b8d342015-02-10 10:39:44 +0200375 ndev->cl = cl;
376
Samuel Ortiz59fcd7c2013-04-11 03:03:29 +0200377 INIT_WORK(&ndev->init_work, mei_nfc_init);
378 schedule_work(&ndev->init_work);
379
380 return 0;
381
382err:
383 mei_nfc_free(ndev);
384
385 return ret;
386}
387
Tomas Winklerb37719c32015-07-23 15:08:33 +0300388void mei_nfc_host_exit(struct mei_device *bus)
Samuel Ortiz59fcd7c2013-04-11 03:03:29 +0200389{
Tomas Winklera176c242014-11-05 18:18:52 +0200390 struct mei_nfc_dev *ndev;
391 struct mei_cl *cl;
392 struct mei_cl_device *cldev;
Tomas Winkler92db1552014-09-29 16:31:37 +0300393
Tomas Winklerb37719c32015-07-23 15:08:33 +0300394 cl = mei_cl_bus_find_cl_by_uuid(bus, mei_nfc_guid);
Tomas Winklera176c242014-11-05 18:18:52 +0200395 if (!cl)
396 return;
397
Tomas Winklerb37719c32015-07-23 15:08:33 +0300398 cldev = cl->cldev;
Tomas Winklera176c242014-11-05 18:18:52 +0200399 if (!cldev)
400 return;
401
402 ndev = (struct mei_nfc_dev *)cldev->priv_data;
403 if (ndev)
404 cancel_work_sync(&ndev->init_work);
405
406 cldev->priv_data = NULL;
407
Tomas Winklera176c242014-11-05 18:18:52 +0200408 /* Need to remove the device here
409 * since mei_nfc_free will unlink the clients
410 */
411 mei_cl_remove_device(cldev);
Tomas Winkler4f273952015-07-08 00:22:03 +0300412
Tomas Winklerb37719c32015-07-23 15:08:33 +0300413 mutex_lock(&bus->device_lock);
Tomas Winklera176c242014-11-05 18:18:52 +0200414 mei_nfc_free(ndev);
Tomas Winklerb37719c32015-07-23 15:08:33 +0300415 mutex_unlock(&bus->device_lock);
Samuel Ortiz59fcd7c2013-04-11 03:03:29 +0200416}
Tomas Winkler48705692014-02-17 15:13:19 +0200417
Tomas Winkler71ce7892015-07-23 15:08:43 +0300418#define MEI_FIXUP(_uuid, _hook) { _uuid, _hook }
419
420static struct mei_fixup {
421
422 const uuid_le uuid;
423 void (*hook)(struct mei_cl_device *cldev);
424} mei_fixups[] = {};
425
426/**
427 * mei_cl_dev_fixup - run fixup handlers
428 *
429 * @cldev: me client device
430 */
431void mei_cl_dev_fixup(struct mei_cl_device *cldev)
432{
433 struct mei_fixup *f;
434 const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
435 int i;
436
437 for (i = 0; i < ARRAY_SIZE(mei_fixups); i++) {
438
439 f = &mei_fixups[i];
440 if (uuid_le_cmp(f->uuid, MEI_UUID_ANY) == 0 ||
441 uuid_le_cmp(f->uuid, *uuid) == 0)
442 f->hook(cldev);
443 }
444}
Tomas Winkler48705692014-02-17 15:13:19 +0200445