]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/misc/mei/bus-fixup.c
Merge remote-tracking branches 'asoc/topic/rockchip', 'asoc/topic/rt5514', 'asoc...
[mirror_ubuntu-bionic-kernel.git] / drivers / misc / mei / bus-fixup.c
CommitLineData
59fcd7c6
SO
1/*
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>
36eda94f 18#include <linux/sched.h>
59fcd7c6
SO
19#include <linux/module.h>
20#include <linux/moduleparam.h>
21#include <linux/device.h>
1f180359 22#include <linux/slab.h>
71ce7891 23#include <linux/uuid.h>
1f180359 24
59fcd7c6
SO
25#include <linux/mei_cl_bus.h>
26
27#include "mei_dev.h"
28#include "client.h"
29
5b6dca29
TW
30#define MEI_UUID_NFC_INFO UUID_LE(0xd2de1625, 0x382d, 0x417d, \
31 0x48, 0xa4, 0xef, 0xab, 0xba, 0x8a, 0x12, 0x06)
32
6009595a
TW
33static const uuid_le mei_nfc_info_guid = MEI_UUID_NFC_INFO;
34
35#define MEI_UUID_NFC_HCI UUID_LE(0x0bb17a78, 0x2a8e, 0x4c50, \
36 0x94, 0xd4, 0x50, 0x26, 0x67, 0x23, 0x77, 0x5c)
37
e97cdb30
TW
38#define MEI_UUID_WD UUID_LE(0x05B79A6F, 0x4628, 0x4D7F, \
39 0x89, 0x9D, 0xA9, 0x15, 0x14, 0xCB, 0x32, 0xAB)
40
7279b238
AU
41#define MEI_UUID_MKHIF_FIX UUID_LE(0x55213584, 0x9a29, 0x4916, \
42 0xba, 0xdf, 0xf, 0xb7, 0xed, 0x68, 0x2a, 0xeb)
43
71ce7891
TW
44#define MEI_UUID_ANY NULL_UUID_LE
45
dd070a16
TW
46/**
47 * number_of_connections - determine whether an client be on the bus
48 * according number of connections
49 * We support only clients:
50 * 1. with single connection
51 * 2. and fixed clients (max_number_of_connections == 0)
52 *
53 * @cldev: me clients device
54 */
55static void number_of_connections(struct mei_cl_device *cldev)
56{
2bcfdc23 57 dev_dbg(&cldev->dev, "running hook %s\n", __func__);
dd070a16
TW
58
59 if (cldev->me_cl->props.max_number_of_connections > 1)
60 cldev->do_match = 0;
61}
62
5b6dca29
TW
63/**
64 * blacklist - blacklist a client from the bus
65 *
66 * @cldev: me clients device
67 */
68static void blacklist(struct mei_cl_device *cldev)
69{
2bcfdc23
TW
70 dev_dbg(&cldev->dev, "running hook %s\n", __func__);
71
5b6dca29
TW
72 cldev->do_match = 0;
73}
74
7279b238
AU
75#define OSTYPE_LINUX 2
76struct mei_os_ver {
77 __le16 build;
78 __le16 reserved1;
79 u8 os_type;
80 u8 major;
81 u8 minor;
82 u8 reserved2;
83} __packed;
84
85#define MKHI_FEATURE_PTT 0x10
86
87struct mkhi_rule_id {
88 __le16 rule_type;
89 u8 feature_id;
90 u8 reserved;
91} __packed;
92
93struct mkhi_fwcaps {
94 struct mkhi_rule_id id;
95 u8 len;
96 u8 data[0];
97} __packed;
98
99#define MKHI_FWCAPS_GROUP_ID 0x3
100#define MKHI_FWCAPS_SET_OS_VER_APP_RULE_CMD 6
101struct mkhi_msg_hdr {
102 u8 group_id;
103 u8 command;
104 u8 reserved;
105 u8 result;
106} __packed;
107
108struct mkhi_msg {
109 struct mkhi_msg_hdr hdr;
110 u8 data[0];
111} __packed;
112
113static int mei_osver(struct mei_cl_device *cldev)
114{
7279b238
AU
115 const size_t size = sizeof(struct mkhi_msg_hdr) +
116 sizeof(struct mkhi_fwcaps) +
117 sizeof(struct mei_os_ver);
7279b238
AU
118 char buf[size];
119 struct mkhi_msg *req;
120 struct mkhi_fwcaps *fwcaps;
121 struct mei_os_ver *os_ver;
122 unsigned int mode = MEI_CL_IO_TX_BLOCKING | MEI_CL_IO_TX_INTERNAL;
123
124 memset(buf, 0, size);
125
126 req = (struct mkhi_msg *)buf;
127 req->hdr.group_id = MKHI_FWCAPS_GROUP_ID;
128 req->hdr.command = MKHI_FWCAPS_SET_OS_VER_APP_RULE_CMD;
129
130 fwcaps = (struct mkhi_fwcaps *)req->data;
131
132 fwcaps->id.rule_type = 0x0;
133 fwcaps->id.feature_id = MKHI_FEATURE_PTT;
134 fwcaps->len = sizeof(*os_ver);
135 os_ver = (struct mei_os_ver *)fwcaps->data;
136 os_ver->os_type = OSTYPE_LINUX;
137
c6240cac 138 return __mei_cl_send(cldev->cl, buf, size, mode);
7279b238
AU
139}
140
141static void mei_mkhi_fix(struct mei_cl_device *cldev)
142{
143 int ret;
144
7ee7f45a
AU
145 if (!cldev->bus->hbm_f_os_supported)
146 return;
147
7279b238
AU
148 ret = mei_cldev_enable(cldev);
149 if (ret)
150 return;
151
152 ret = mei_osver(cldev);
c6240cac 153 if (ret < 0)
7279b238
AU
154 dev_err(&cldev->dev, "OS version command failed %d\n", ret);
155
156 mei_cldev_disable(cldev);
157}
158
e97cdb30
TW
159/**
160 * mei_wd - wd client on the bus, change protocol version
161 * as the API has changed.
162 *
163 * @cldev: me clients device
164 */
165#if IS_ENABLED(CONFIG_INTEL_MEI_ME)
166#include <linux/pci.h>
167#include "hw-me-regs.h"
168static void mei_wd(struct mei_cl_device *cldev)
169{
170 struct pci_dev *pdev = to_pci_dev(cldev->dev.parent);
171
172 dev_dbg(&cldev->dev, "running hook %s\n", __func__);
173 if (pdev->device == MEI_DEV_ID_WPT_LP ||
174 pdev->device == MEI_DEV_ID_SPT ||
175 pdev->device == MEI_DEV_ID_SPT_H)
176 cldev->me_cl->props.protocol_version = 0x2;
177
178 cldev->do_match = 1;
179}
180#else
181static inline void mei_wd(struct mei_cl_device *cldev) {}
182#endif /* CONFIG_INTEL_MEI_ME */
183
59fcd7c6
SO
184struct mei_nfc_cmd {
185 u8 command;
186 u8 status;
187 u16 req_id;
188 u32 reserved;
189 u16 data_size;
190 u8 sub_command;
191 u8 data[];
192} __packed;
193
194struct mei_nfc_reply {
195 u8 command;
196 u8 status;
197 u16 req_id;
198 u32 reserved;
199 u16 data_size;
200 u8 sub_command;
201 u8 reply_status;
202 u8 data[];
203} __packed;
204
205struct mei_nfc_if_version {
206 u8 radio_version_sw[3];
207 u8 reserved[3];
208 u8 radio_version_hw[3];
209 u8 i2c_addr;
210 u8 fw_ivn;
211 u8 vendor_id;
212 u8 radio_type;
213} __packed;
214
59fcd7c6
SO
215
216#define MEI_NFC_CMD_MAINTENANCE 0x00
59fcd7c6
SO
217#define MEI_NFC_SUBCMD_IF_VERSION 0x01
218
91a6b95f
SO
219/* Vendors */
220#define MEI_NFC_VENDOR_INSIDE 0x00
221#define MEI_NFC_VENDOR_NXP 0x01
222
223/* Radio types */
224#define MEI_NFC_VENDOR_INSIDE_UREAD 0x00
225#define MEI_NFC_VENDOR_NXP_PN544 0x01
226
b39910c2
TW
227/**
228 * mei_nfc_if_version - get NFC interface version
229 *
230 * @cl: host client (nfc info)
231 * @ver: NFC interface version to be filled in
232 *
233 * Return: 0 on success; < 0 otherwise
234 */
235static int mei_nfc_if_version(struct mei_cl *cl,
236 struct mei_nfc_if_version *ver)
59fcd7c6 237{
b37719c3 238 struct mei_device *bus;
b39910c2
TW
239 struct mei_nfc_cmd cmd = {
240 .command = MEI_NFC_CMD_MAINTENANCE,
241 .data_size = 1,
242 .sub_command = MEI_NFC_SUBCMD_IF_VERSION,
243 };
59fcd7c6 244 struct mei_nfc_reply *reply = NULL;
59fcd7c6
SO
245 size_t if_version_length;
246 int bytes_recv, ret;
247
b37719c3 248 bus = cl->dev;
59fcd7c6 249
b39910c2 250 WARN_ON(mutex_is_locked(&bus->device_lock));
59fcd7c6 251
e0cb6b2f
AU
252 ret = __mei_cl_send(cl, (u8 *)&cmd, sizeof(struct mei_nfc_cmd),
253 MEI_CL_IO_TX_BLOCKING);
59fcd7c6 254 if (ret < 0) {
b37719c3 255 dev_err(bus->dev, "Could not send IF version cmd\n");
59fcd7c6
SO
256 return ret;
257 }
258
259 /* to be sure on the stack we alloc memory */
260 if_version_length = sizeof(struct mei_nfc_reply) +
261 sizeof(struct mei_nfc_if_version);
262
263 reply = kzalloc(if_version_length, GFP_KERNEL);
264 if (!reply)
265 return -ENOMEM;
266
b39910c2 267 ret = 0;
076802d0 268 bytes_recv = __mei_cl_recv(cl, (u8 *)reply, if_version_length, 0);
582ab27a 269 if (bytes_recv < if_version_length) {
b37719c3 270 dev_err(bus->dev, "Could not read IF version\n");
59fcd7c6
SO
271 ret = -EIO;
272 goto err;
273 }
274
b39910c2 275 memcpy(ver, reply->data, sizeof(struct mei_nfc_if_version));
59fcd7c6 276
b39910c2
TW
277 dev_info(bus->dev, "NFC MEI VERSION: IVN 0x%x Vendor ID 0x%x Type 0x%x\n",
278 ver->fw_ivn, ver->vendor_id, ver->radio_type);
59fcd7c6
SO
279
280err:
281 kfree(reply);
282 return ret;
283}
284
b39910c2
TW
285/**
286 * mei_nfc_radio_name - derive nfc radio name from the interface version
287 *
288 * @ver: NFC radio version
289 *
290 * Return: radio name string
291 */
292static const char *mei_nfc_radio_name(struct mei_nfc_if_version *ver)
293{
294
295 if (ver->vendor_id == MEI_NFC_VENDOR_INSIDE) {
296 if (ver->radio_type == MEI_NFC_VENDOR_INSIDE_UREAD)
297 return "microread";
298 }
299
300 if (ver->vendor_id == MEI_NFC_VENDOR_NXP) {
301 if (ver->radio_type == MEI_NFC_VENDOR_NXP_PN544)
302 return "pn544";
303 }
304
305 return NULL;
306}
307
6009595a
TW
308/**
309 * mei_nfc - The nfc fixup function. The function retrieves nfc radio
310 * name and set is as device attribute so we can load
311 * the proper device driver for it
312 *
313 * @cldev: me client device (nfc)
314 */
315static void mei_nfc(struct mei_cl_device *cldev)
59fcd7c6 316{
b37719c3 317 struct mei_device *bus;
6009595a
TW
318 struct mei_cl *cl;
319 struct mei_me_client *me_cl = NULL;
320 struct mei_nfc_if_version ver;
321 const char *radio_name = NULL;
322 int ret;
59fcd7c6 323
6009595a 324 bus = cldev->bus;
59fcd7c6 325
2bcfdc23 326 dev_dbg(&cldev->dev, "running hook %s\n", __func__);
59fcd7c6 327
b37719c3 328 mutex_lock(&bus->device_lock);
6009595a 329 /* we need to connect to INFO GUID */
7851e008 330 cl = mei_cl_alloc_linked(bus);
6009595a
TW
331 if (IS_ERR(cl)) {
332 ret = PTR_ERR(cl);
333 cl = NULL;
334 dev_err(bus->dev, "nfc hook alloc failed %d\n", ret);
335 goto out;
59fcd7c6 336 }
59fcd7c6 337
6009595a
TW
338 me_cl = mei_me_cl_by_uuid(bus, &mei_nfc_info_guid);
339 if (!me_cl) {
340 ret = -ENOTTY;
341 dev_err(bus->dev, "Cannot find nfc info %d\n", ret);
342 goto out;
59fcd7c6
SO
343 }
344
6009595a
TW
345 ret = mei_cl_connect(cl, me_cl, NULL);
346 if (ret < 0) {
347 dev_err(&cldev->dev, "Can't connect to the NFC INFO ME ret = %d\n",
348 ret);
349 goto out;
59fcd7c6
SO
350 }
351
b37719c3 352 mutex_unlock(&bus->device_lock);
59fcd7c6 353
6009595a
TW
354 ret = mei_nfc_if_version(cl, &ver);
355 if (ret)
356 goto disconnect;
b39910c2 357
6009595a 358 radio_name = mei_nfc_radio_name(&ver);
91a6b95f 359
6009595a
TW
360 if (!radio_name) {
361 ret = -ENOENT;
362 dev_err(&cldev->dev, "Can't get the NFC interface version ret = %d\n",
363 ret);
364 goto disconnect;
91a6b95f
SO
365 }
366
6009595a
TW
367 dev_dbg(bus->dev, "nfc radio %s\n", radio_name);
368 strlcpy(cldev->name, radio_name, sizeof(cldev->name));
59fcd7c6 369
6009595a 370disconnect:
b37719c3 371 mutex_lock(&bus->device_lock);
6009595a
TW
372 if (mei_cl_disconnect(cl) < 0)
373 dev_err(bus->dev, "Can't disconnect the NFC INFO ME\n");
59fcd7c6 374
6009595a 375 mei_cl_flush_queues(cl, NULL);
59fcd7c6 376
6009595a
TW
377out:
378 mei_cl_unlink(cl);
379 mutex_unlock(&bus->device_lock);
380 mei_me_cl_put(me_cl);
381 kfree(cl);
59fcd7c6 382
6009595a
TW
383 if (ret)
384 cldev->do_match = 0;
4f273959 385
6009595a 386 dev_dbg(bus->dev, "end of fixup match = %d\n", cldev->do_match);
48705693 387}
dc844b0d 388
71ce7891
TW
389#define MEI_FIXUP(_uuid, _hook) { _uuid, _hook }
390
391static struct mei_fixup {
392
393 const uuid_le uuid;
394 void (*hook)(struct mei_cl_device *cldev);
5b6dca29 395} mei_fixups[] = {
dd070a16 396 MEI_FIXUP(MEI_UUID_ANY, number_of_connections),
5b6dca29 397 MEI_FIXUP(MEI_UUID_NFC_INFO, blacklist),
6009595a 398 MEI_FIXUP(MEI_UUID_NFC_HCI, mei_nfc),
e97cdb30 399 MEI_FIXUP(MEI_UUID_WD, mei_wd),
7279b238 400 MEI_FIXUP(MEI_UUID_MKHIF_FIX, mei_mkhi_fix),
5b6dca29 401};
71ce7891
TW
402
403/**
ae48d74d 404 * mei_cldev_fixup - run fixup handlers
71ce7891
TW
405 *
406 * @cldev: me client device
407 */
ae48d74d 408void mei_cl_bus_dev_fixup(struct mei_cl_device *cldev)
71ce7891
TW
409{
410 struct mei_fixup *f;
411 const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
412 int i;
413
414 for (i = 0; i < ARRAY_SIZE(mei_fixups); i++) {
415
416 f = &mei_fixups[i];
417 if (uuid_le_cmp(f->uuid, MEI_UUID_ANY) == 0 ||
418 uuid_le_cmp(f->uuid, *uuid) == 0)
419 f->hook(cldev);
420 }
421}
91a6b95f 422