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