blob: 6a60db515bdabcb490410b35ea85bb7a3fd621f1 [file] [log] [blame]
Michał Kępień2f9f26b2016-01-22 15:27:13 +01001/*
2 * Common functions for kernel modules using Dell SMBIOS
3 *
4 * Copyright (c) Red Hat <mjg@redhat.com>
5 * Copyright (c) 2014 Gabriele Mazzotta <gabriele.mzt@gmail.com>
6 * Copyright (c) 2014 Pali Rohár <pali.rohar@gmail.com>
7 *
8 * Based on documentation in the libsmbios package:
9 * Copyright (C) 2005-2014 Dell Inc.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
14 */
Mario Limonciello7b4dd2162017-09-26 13:50:02 -050015#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Michał Kępień2f9f26b2016-01-22 15:27:13 +010016
17#include <linux/kernel.h>
18#include <linux/module.h>
Mario Limonciello33b9ca12017-11-01 14:25:30 -050019#include <linux/capability.h>
Michał Kępień2f9f26b2016-01-22 15:27:13 +010020#include <linux/dmi.h>
Michał Kępieńe8edf532016-03-04 14:09:06 +010021#include <linux/err.h>
Michał Kępień2f9f26b2016-01-22 15:27:13 +010022#include <linux/mutex.h>
Mario Limonciello33b9ca12017-11-01 14:25:30 -050023#include <linux/platform_device.h>
Michał Kępień2f9f26b2016-01-22 15:27:13 +010024#include <linux/slab.h>
Michał Kępień2f9f26b2016-01-22 15:27:13 +010025#include "dell-smbios.h"
26
Mario Limonciello1f8543a2017-11-01 14:25:34 -050027static u32 da_supported_commands;
Michał Kępień2f9f26b2016-01-22 15:27:13 +010028static int da_num_tokens;
Mario Limonciello33b9ca12017-11-01 14:25:30 -050029static struct platform_device *platform_device;
Michał Kępieńb7bca2d2016-01-22 15:27:26 +010030static struct calling_interface_token *da_tokens;
Mario Limonciello33b9ca12017-11-01 14:25:30 -050031static struct device_attribute *token_location_attrs;
32static struct device_attribute *token_value_attrs;
33static struct attribute **token_attrs;
Mario Limonciello549b4932017-11-01 14:25:31 -050034static DEFINE_MUTEX(smbios_mutex);
35
36struct smbios_device {
37 struct list_head list;
38 struct device *device;
39 int (*call_fn)(struct calling_interface_buffer *);
40};
41
Mario Limonciello1f8543a2017-11-01 14:25:34 -050042struct smbios_call {
43 u32 need_capability;
44 int cmd_class;
45 int cmd_select;
46};
47
48/* calls that are whitelisted for given capabilities */
49static struct smbios_call call_whitelist[] = {
50 /* generally tokens are allowed, but may be further filtered or
51 * restricted by token blacklist or whitelist
52 */
53 {CAP_SYS_ADMIN, CLASS_TOKEN_READ, SELECT_TOKEN_STD},
54 {CAP_SYS_ADMIN, CLASS_TOKEN_READ, SELECT_TOKEN_AC},
55 {CAP_SYS_ADMIN, CLASS_TOKEN_READ, SELECT_TOKEN_BAT},
56 {CAP_SYS_ADMIN, CLASS_TOKEN_WRITE, SELECT_TOKEN_STD},
57 {CAP_SYS_ADMIN, CLASS_TOKEN_WRITE, SELECT_TOKEN_AC},
58 {CAP_SYS_ADMIN, CLASS_TOKEN_WRITE, SELECT_TOKEN_BAT},
59 /* used by userspace: fwupdate */
60 {CAP_SYS_ADMIN, CLASS_ADMIN_PROP, SELECT_ADMIN_PROP},
61 /* used by userspace: fwupd */
62 {CAP_SYS_ADMIN, CLASS_INFO, SELECT_DOCK},
63 {CAP_SYS_ADMIN, CLASS_FLASH_INTERFACE, SELECT_FLASH_INTERFACE},
64};
65
66/* calls that are explicitly blacklisted */
67static struct smbios_call call_blacklist[] = {
68 {0x0000, 01, 07}, /* manufacturing use */
69 {0x0000, 06, 05}, /* manufacturing use */
70 {0x0000, 11, 03}, /* write once */
71 {0x0000, 11, 07}, /* write once */
72 {0x0000, 11, 11}, /* write once */
73 {0x0000, 19, -1}, /* diagnostics */
74 /* handled by kernel: dell-laptop */
75 {0x0000, CLASS_INFO, SELECT_RFKILL},
76 {0x0000, CLASS_KBD_BACKLIGHT, SELECT_KBD_BACKLIGHT},
77};
78
79struct token_range {
80 u32 need_capability;
81 u16 min;
82 u16 max;
83};
84
85/* tokens that are whitelisted for given capabilities */
86static struct token_range token_whitelist[] = {
87 /* used by userspace: fwupdate */
88 {CAP_SYS_ADMIN, CAPSULE_EN_TOKEN, CAPSULE_DIS_TOKEN},
89 /* can indicate to userspace that WMI is needed */
90 {0x0000, WSMT_EN_TOKEN, WSMT_DIS_TOKEN}
91};
92
93/* tokens that are explicitly blacklisted */
94static struct token_range token_blacklist[] = {
95 {0x0000, 0x0058, 0x0059}, /* ME use */
96 {0x0000, 0x00CD, 0x00D0}, /* raid shadow copy */
97 {0x0000, 0x013A, 0x01FF}, /* sata shadow copy */
98 {0x0000, 0x0175, 0x0176}, /* write once */
99 {0x0000, 0x0195, 0x0197}, /* diagnostics */
100 {0x0000, 0x01DC, 0x01DD}, /* manufacturing use */
101 {0x0000, 0x027D, 0x0284}, /* diagnostics */
102 {0x0000, 0x02E3, 0x02E3}, /* manufacturing use */
103 {0x0000, 0x02FF, 0x02FF}, /* manufacturing use */
104 {0x0000, 0x0300, 0x0302}, /* manufacturing use */
105 {0x0000, 0x0325, 0x0326}, /* manufacturing use */
106 {0x0000, 0x0332, 0x0335}, /* fan control */
107 {0x0000, 0x0350, 0x0350}, /* manufacturing use */
108 {0x0000, 0x0363, 0x0363}, /* manufacturing use */
109 {0x0000, 0x0368, 0x0368}, /* manufacturing use */
110 {0x0000, 0x03F6, 0x03F7}, /* manufacturing use */
111 {0x0000, 0x049E, 0x049F}, /* manufacturing use */
112 {0x0000, 0x04A0, 0x04A3}, /* disagnostics */
113 {0x0000, 0x04E6, 0x04E7}, /* manufacturing use */
114 {0x0000, 0x4000, 0x7FFF}, /* internal BIOS use */
115 {0x0000, 0x9000, 0x9001}, /* internal BIOS use */
116 {0x0000, 0xA000, 0xBFFF}, /* write only */
117 {0x0000, 0xEFF0, 0xEFFF}, /* internal BIOS use */
118 /* handled by kernel: dell-laptop */
119 {0x0000, BRIGHTNESS_TOKEN, BRIGHTNESS_TOKEN},
120 {0x0000, KBD_LED_OFF_TOKEN, KBD_LED_AUTO_TOKEN},
121 {0x0000, KBD_LED_AC_TOKEN, KBD_LED_AC_TOKEN},
122 {0x0000, KBD_LED_AUTO_25_TOKEN, KBD_LED_AUTO_75_TOKEN},
123 {0x0000, KBD_LED_AUTO_100_TOKEN, KBD_LED_AUTO_100_TOKEN},
124 {0x0000, GLOBAL_MIC_MUTE_ENABLE, GLOBAL_MIC_MUTE_DISABLE},
125};
126
Mario Limonciello549b4932017-11-01 14:25:31 -0500127static LIST_HEAD(smbios_device_list);
Michał Kępień2f9f26b2016-01-22 15:27:13 +0100128
Michał Kępień0db21802016-03-04 14:09:07 +0100129int dell_smbios_error(int value)
Michał Kępieńe8edf532016-03-04 14:09:06 +0100130{
131 switch (value) {
132 case 0: /* Completed successfully */
133 return 0;
134 case -1: /* Completed with error */
135 return -EIO;
136 case -2: /* Function not supported */
137 return -ENXIO;
138 default: /* Unknown error */
139 return -EINVAL;
140 }
141}
Michał Kępień0db21802016-03-04 14:09:07 +0100142EXPORT_SYMBOL_GPL(dell_smbios_error);
Michał Kępieńe8edf532016-03-04 14:09:06 +0100143
Mario Limonciello549b4932017-11-01 14:25:31 -0500144int dell_smbios_register_device(struct device *d, void *call_fn)
Michał Kępień2f9f26b2016-01-22 15:27:13 +0100145{
Mario Limonciello549b4932017-11-01 14:25:31 -0500146 struct smbios_device *priv;
Michał Kępień2f9f26b2016-01-22 15:27:13 +0100147
Mario Limonciello549b4932017-11-01 14:25:31 -0500148 priv = devm_kzalloc(d, sizeof(struct smbios_device), GFP_KERNEL);
149 if (!priv)
150 return -ENOMEM;
151 get_device(d);
152 priv->device = d;
153 priv->call_fn = call_fn;
154 mutex_lock(&smbios_mutex);
155 list_add_tail(&priv->list, &smbios_device_list);
156 mutex_unlock(&smbios_mutex);
157 dev_dbg(d, "Added device: %s\n", d->driver->name);
158 return 0;
159}
160EXPORT_SYMBOL_GPL(dell_smbios_register_device);
161
162void dell_smbios_unregister_device(struct device *d)
Michał Kępień2f9f26b2016-01-22 15:27:13 +0100163{
Mario Limonciello549b4932017-11-01 14:25:31 -0500164 struct smbios_device *priv;
Michał Kępień2f9f26b2016-01-22 15:27:13 +0100165
Mario Limonciello549b4932017-11-01 14:25:31 -0500166 mutex_lock(&smbios_mutex);
167 list_for_each_entry(priv, &smbios_device_list, list) {
168 if (priv->device == d) {
169 list_del(&priv->list);
170 put_device(d);
171 break;
172 }
173 }
174 mutex_unlock(&smbios_mutex);
175 dev_dbg(d, "Remove device: %s\n", d->driver->name);
176}
177EXPORT_SYMBOL_GPL(dell_smbios_unregister_device);
178
Mario Limonciello1f8543a2017-11-01 14:25:34 -0500179int dell_smbios_call_filter(struct device *d,
180 struct calling_interface_buffer *buffer)
181{
182 u16 t = 0;
183 int i;
184
185 /* can't make calls over 30 */
186 if (buffer->cmd_class > 30) {
187 dev_dbg(d, "class too big: %u\n", buffer->cmd_class);
188 return -EINVAL;
189 }
190
191 /* supported calls on the particular system */
192 if (!(da_supported_commands & (1 << buffer->cmd_class))) {
193 dev_dbg(d, "invalid command, supported commands: 0x%8x\n",
194 da_supported_commands);
195 return -EINVAL;
196 }
197
198 /* match against call blacklist */
199 for (i = 0; i < ARRAY_SIZE(call_blacklist); i++) {
200 if (buffer->cmd_class != call_blacklist[i].cmd_class)
201 continue;
202 if (buffer->cmd_select != call_blacklist[i].cmd_select &&
203 call_blacklist[i].cmd_select != -1)
204 continue;
205 dev_dbg(d, "blacklisted command: %u/%u\n",
206 buffer->cmd_class, buffer->cmd_select);
207 return -EINVAL;
208 }
209
210 /* if a token call, find token ID */
211
212 if ((buffer->cmd_class == CLASS_TOKEN_READ ||
213 buffer->cmd_class == CLASS_TOKEN_WRITE) &&
214 buffer->cmd_select < 3) {
215 /* find the matching token ID */
216 for (i = 0; i < da_num_tokens; i++) {
217 if (da_tokens[i].location != buffer->input[0])
218 continue;
219 t = da_tokens[i].tokenID;
220 break;
221 }
222
223 /* token call; but token didn't exist */
224 if (!t) {
225 dev_dbg(d, "token at location %04x doesn't exist\n",
226 buffer->input[0]);
227 return -EINVAL;
228 }
229
230 /* match against token blacklist */
231 for (i = 0; i < ARRAY_SIZE(token_blacklist); i++) {
232 if (!token_blacklist[i].min || !token_blacklist[i].max)
233 continue;
234 if (t >= token_blacklist[i].min &&
235 t <= token_blacklist[i].max)
236 return -EINVAL;
237 }
238
239 /* match against token whitelist */
240 for (i = 0; i < ARRAY_SIZE(token_whitelist); i++) {
241 if (!token_whitelist[i].min || !token_whitelist[i].max)
242 continue;
243 if (t < token_whitelist[i].min ||
244 t > token_whitelist[i].max)
245 continue;
246 if (!token_whitelist[i].need_capability ||
247 capable(token_whitelist[i].need_capability)) {
248 dev_dbg(d, "whitelisted token: %x\n", t);
249 return 0;
250 }
251
252 }
253 }
254 /* match against call whitelist */
255 for (i = 0; i < ARRAY_SIZE(call_whitelist); i++) {
256 if (buffer->cmd_class != call_whitelist[i].cmd_class)
257 continue;
258 if (buffer->cmd_select != call_whitelist[i].cmd_select)
259 continue;
260 if (!call_whitelist[i].need_capability ||
261 capable(call_whitelist[i].need_capability)) {
262 dev_dbg(d, "whitelisted capable command: %u/%u\n",
263 buffer->cmd_class, buffer->cmd_select);
264 return 0;
265 }
266 dev_dbg(d, "missing capability %d for %u/%u\n",
267 call_whitelist[i].need_capability,
268 buffer->cmd_class, buffer->cmd_select);
269
270 }
271
272 /* not in a whitelist, only allow processes with capabilities */
273 if (capable(CAP_SYS_RAWIO)) {
274 dev_dbg(d, "Allowing %u/%u due to CAP_SYS_RAWIO\n",
275 buffer->cmd_class, buffer->cmd_select);
276 return 0;
277 }
278
279 return -EACCES;
280}
281EXPORT_SYMBOL_GPL(dell_smbios_call_filter);
282
Mario Limonciello549b4932017-11-01 14:25:31 -0500283int dell_smbios_call(struct calling_interface_buffer *buffer)
Michał Kępień2f9f26b2016-01-22 15:27:13 +0100284{
Mario Limonciello549b4932017-11-01 14:25:31 -0500285 int (*call_fn)(struct calling_interface_buffer *) = NULL;
286 struct device *selected_dev = NULL;
287 struct smbios_device *priv;
288 int ret;
289
290 mutex_lock(&smbios_mutex);
291 list_for_each_entry(priv, &smbios_device_list, list) {
292 if (!selected_dev || priv->device->id >= selected_dev->id) {
293 dev_dbg(priv->device, "Trying device ID: %d\n",
294 priv->device->id);
295 call_fn = priv->call_fn;
296 selected_dev = priv->device;
297 }
298 }
299
300 if (!selected_dev) {
301 ret = -ENODEV;
302 pr_err("No dell-smbios drivers are loaded\n");
303 goto out_smbios_call;
304 }
305
306 ret = call_fn(buffer);
307
308out_smbios_call:
309 mutex_unlock(&smbios_mutex);
310 return ret;
Michał Kępień2f9f26b2016-01-22 15:27:13 +0100311}
Mario Limonciello549b4932017-11-01 14:25:31 -0500312EXPORT_SYMBOL_GPL(dell_smbios_call);
Michał Kępień2f9f26b2016-01-22 15:27:13 +0100313
Michał Kępień96f7ef92016-01-22 15:27:22 +0100314struct calling_interface_token *dell_smbios_find_token(int tokenid)
315{
316 int i;
317
318 for (i = 0; i < da_num_tokens; i++) {
319 if (da_tokens[i].tokenID == tokenid)
320 return &da_tokens[i];
321 }
322
323 return NULL;
324}
325EXPORT_SYMBOL_GPL(dell_smbios_find_token);
326
Hans de Goede504b0252017-03-16 11:55:32 +0100327static BLOCKING_NOTIFIER_HEAD(dell_laptop_chain_head);
328
329int dell_laptop_register_notifier(struct notifier_block *nb)
330{
331 return blocking_notifier_chain_register(&dell_laptop_chain_head, nb);
332}
333EXPORT_SYMBOL_GPL(dell_laptop_register_notifier);
334
335int dell_laptop_unregister_notifier(struct notifier_block *nb)
336{
337 return blocking_notifier_chain_unregister(&dell_laptop_chain_head, nb);
338}
339EXPORT_SYMBOL_GPL(dell_laptop_unregister_notifier);
340
341void dell_laptop_call_notifier(unsigned long action, void *data)
342{
343 blocking_notifier_call_chain(&dell_laptop_chain_head, action, data);
344}
345EXPORT_SYMBOL_GPL(dell_laptop_call_notifier);
346
Michał Kępień2f9f26b2016-01-22 15:27:13 +0100347static void __init parse_da_table(const struct dmi_header *dm)
348{
349 /* Final token is a terminator, so we don't want to copy it */
350 int tokens = (dm->length-11)/sizeof(struct calling_interface_token)-1;
351 struct calling_interface_token *new_da_tokens;
352 struct calling_interface_structure *table =
353 container_of(dm, struct calling_interface_structure, header);
354
355 /* 4 bytes of table header, plus 7 bytes of Dell header, plus at least
356 6 bytes of entry */
357
358 if (dm->length < 17)
359 return;
360
Mario Limonciello1f8543a2017-11-01 14:25:34 -0500361 da_supported_commands = table->supportedCmds;
362
Michał Kępień2f9f26b2016-01-22 15:27:13 +0100363 new_da_tokens = krealloc(da_tokens, (da_num_tokens + tokens) *
364 sizeof(struct calling_interface_token),
365 GFP_KERNEL);
366
367 if (!new_da_tokens)
368 return;
369 da_tokens = new_da_tokens;
370
371 memcpy(da_tokens+da_num_tokens, table->tokens,
372 sizeof(struct calling_interface_token) * tokens);
373
374 da_num_tokens += tokens;
375}
376
Mario Limonciello33b9ca12017-11-01 14:25:30 -0500377static void zero_duplicates(struct device *dev)
378{
379 int i, j;
380
381 for (i = 0; i < da_num_tokens; i++) {
382 if (da_tokens[i].tokenID == 0)
383 continue;
384 for (j = i+1; j < da_num_tokens; j++) {
385 if (da_tokens[j].tokenID == 0)
386 continue;
387 if (da_tokens[i].tokenID == da_tokens[j].tokenID) {
388 dev_dbg(dev, "Zeroing dup token ID %x(%x/%x)\n",
389 da_tokens[j].tokenID,
390 da_tokens[j].location,
391 da_tokens[j].value);
392 da_tokens[j].tokenID = 0;
393 }
394 }
395 }
396}
397
Michał Kępień2f9f26b2016-01-22 15:27:13 +0100398static void __init find_tokens(const struct dmi_header *dm, void *dummy)
399{
400 switch (dm->type) {
401 case 0xd4: /* Indexed IO */
402 case 0xd5: /* Protected Area Type 1 */
403 case 0xd6: /* Protected Area Type 2 */
404 break;
405 case 0xda: /* Calling interface */
406 parse_da_table(dm);
407 break;
408 }
409}
410
Mario Limonciello33b9ca12017-11-01 14:25:30 -0500411static int match_attribute(struct device *dev,
412 struct device_attribute *attr)
413{
414 int i;
415
416 for (i = 0; i < da_num_tokens * 2; i++) {
417 if (!token_attrs[i])
418 continue;
419 if (strcmp(token_attrs[i]->name, attr->attr.name) == 0)
420 return i/2;
421 }
422 dev_dbg(dev, "couldn't match: %s\n", attr->attr.name);
423 return -EINVAL;
424}
425
426static ssize_t location_show(struct device *dev,
427 struct device_attribute *attr, char *buf)
428{
429 int i;
430
431 if (!capable(CAP_SYS_ADMIN))
432 return -EPERM;
433
434 i = match_attribute(dev, attr);
435 if (i > 0)
436 return scnprintf(buf, PAGE_SIZE, "%08x", da_tokens[i].location);
437 return 0;
438}
439
440static ssize_t value_show(struct device *dev,
441 struct device_attribute *attr, char *buf)
442{
443 int i;
444
445 if (!capable(CAP_SYS_ADMIN))
446 return -EPERM;
447
448 i = match_attribute(dev, attr);
449 if (i > 0)
450 return scnprintf(buf, PAGE_SIZE, "%08x", da_tokens[i].value);
451 return 0;
452}
453
454static struct attribute_group smbios_attribute_group = {
455 .name = "tokens"
456};
457
458static struct platform_driver platform_driver = {
459 .driver = {
460 .name = "dell-smbios",
461 },
462};
463
464static int build_tokens_sysfs(struct platform_device *dev)
465{
Mario Limonciello33b9ca12017-11-01 14:25:30 -0500466 char *location_name;
467 char *value_name;
468 size_t size;
469 int ret;
470 int i, j;
471
472 /* (number of tokens + 1 for null terminated */
473 size = sizeof(struct device_attribute) * (da_num_tokens + 1);
474 token_location_attrs = kzalloc(size, GFP_KERNEL);
475 if (!token_location_attrs)
476 return -ENOMEM;
477 token_value_attrs = kzalloc(size, GFP_KERNEL);
478 if (!token_value_attrs)
479 goto out_allocate_value;
480
481 /* need to store both location and value + terminator*/
482 size = sizeof(struct attribute *) * ((2 * da_num_tokens) + 1);
483 token_attrs = kzalloc(size, GFP_KERNEL);
484 if (!token_attrs)
485 goto out_allocate_attrs;
486
487 for (i = 0, j = 0; i < da_num_tokens; i++) {
488 /* skip empty */
489 if (da_tokens[i].tokenID == 0)
490 continue;
491 /* add location */
Arnd Bergmann54d11732017-11-08 13:08:39 +0100492 location_name = kasprintf(GFP_KERNEL, "%04x_location",
493 da_tokens[i].tokenID);
Mario Limonciello33b9ca12017-11-01 14:25:30 -0500494 if (location_name == NULL)
495 goto out_unwind_strings;
496 sysfs_attr_init(&token_location_attrs[i].attr);
497 token_location_attrs[i].attr.name = location_name;
498 token_location_attrs[i].attr.mode = 0444;
499 token_location_attrs[i].show = location_show;
500 token_attrs[j++] = &token_location_attrs[i].attr;
501
502 /* add value */
Arnd Bergmann54d11732017-11-08 13:08:39 +0100503 value_name = kasprintf(GFP_KERNEL, "%04x_value",
504 da_tokens[i].tokenID);
Mario Limonciello33b9ca12017-11-01 14:25:30 -0500505 if (value_name == NULL)
506 goto loop_fail_create_value;
507 sysfs_attr_init(&token_value_attrs[i].attr);
508 token_value_attrs[i].attr.name = value_name;
509 token_value_attrs[i].attr.mode = 0444;
510 token_value_attrs[i].show = value_show;
511 token_attrs[j++] = &token_value_attrs[i].attr;
512 continue;
513
514loop_fail_create_value:
515 kfree(value_name);
516 goto out_unwind_strings;
517 }
518 smbios_attribute_group.attrs = token_attrs;
519
520 ret = sysfs_create_group(&dev->dev.kobj, &smbios_attribute_group);
521 if (ret)
522 goto out_unwind_strings;
523 return 0;
524
525out_unwind_strings:
526 for (i = i-1; i > 0; i--) {
527 kfree(token_location_attrs[i].attr.name);
528 kfree(token_value_attrs[i].attr.name);
529 }
530 kfree(token_attrs);
531out_allocate_attrs:
532 kfree(token_value_attrs);
533out_allocate_value:
534 kfree(token_location_attrs);
535
536 return -ENOMEM;
537}
538
539static void free_group(struct platform_device *pdev)
540{
541 int i;
542
543 sysfs_remove_group(&pdev->dev.kobj,
544 &smbios_attribute_group);
545 for (i = 0; i < da_num_tokens; i++) {
546 kfree(token_location_attrs[i].attr.name);
547 kfree(token_value_attrs[i].attr.name);
548 }
549 kfree(token_attrs);
550 kfree(token_value_attrs);
551 kfree(token_location_attrs);
552}
553
Michał Kępień2f9f26b2016-01-22 15:27:13 +0100554static int __init dell_smbios_init(void)
555{
Mario Limonciello980f4812017-11-01 14:25:29 -0500556 const struct dmi_device *valid;
Michał Kępień2f9f26b2016-01-22 15:27:13 +0100557 int ret;
558
Mario Limonciello980f4812017-11-01 14:25:29 -0500559 valid = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, "Dell System", NULL);
560 if (!valid) {
561 pr_err("Unable to run on non-Dell system\n");
562 return -ENODEV;
563 }
564
Michał Kępień2f9f26b2016-01-22 15:27:13 +0100565 dmi_walk(find_tokens, NULL);
566
567 if (!da_tokens) {
568 pr_info("Unable to find dmi tokens\n");
569 return -ENODEV;
570 }
571
Mario Limonciello33b9ca12017-11-01 14:25:30 -0500572 ret = platform_driver_register(&platform_driver);
573 if (ret)
574 goto fail_platform_driver;
575
576 platform_device = platform_device_alloc("dell-smbios", 0);
577 if (!platform_device) {
578 ret = -ENOMEM;
579 goto fail_platform_device_alloc;
580 }
581 ret = platform_device_add(platform_device);
582 if (ret)
583 goto fail_platform_device_add;
584
585 /* duplicate tokens will cause problems building sysfs files */
586 zero_duplicates(&platform_device->dev);
587
588 ret = build_tokens_sysfs(platform_device);
589 if (ret)
590 goto fail_create_group;
Michał Kępień2f9f26b2016-01-22 15:27:13 +0100591
592 return 0;
593
Mario Limonciello33b9ca12017-11-01 14:25:30 -0500594fail_create_group:
595 platform_device_del(platform_device);
596
597fail_platform_device_add:
598 platform_device_put(platform_device);
599
600fail_platform_device_alloc:
601 platform_driver_unregister(&platform_driver);
602
603fail_platform_driver:
Michał Kępień2f9f26b2016-01-22 15:27:13 +0100604 kfree(da_tokens);
605 return ret;
606}
607
608static void __exit dell_smbios_exit(void)
609{
Mario Limonciello549b4932017-11-01 14:25:31 -0500610 mutex_lock(&smbios_mutex);
Mario Limonciello33b9ca12017-11-01 14:25:30 -0500611 if (platform_device) {
612 free_group(platform_device);
613 platform_device_unregister(platform_device);
614 platform_driver_unregister(&platform_driver);
615 }
Mario Limonciello33b9ca12017-11-01 14:25:30 -0500616 kfree(da_tokens);
Mario Limonciello549b4932017-11-01 14:25:31 -0500617 mutex_unlock(&smbios_mutex);
Michał Kępień2f9f26b2016-01-22 15:27:13 +0100618}
619
620subsys_initcall(dell_smbios_init);
621module_exit(dell_smbios_exit);
622
623MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
624MODULE_AUTHOR("Gabriele Mazzotta <gabriele.mzt@gmail.com>");
625MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
626MODULE_DESCRIPTION("Common functions for kernel modules using Dell SMBIOS");
627MODULE_LICENSE("GPL");