dell-smbios-base.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Common functions for kernel modules using Dell SMBIOS
  4. *
  5. * Copyright (c) Red Hat <mjg@redhat.com>
  6. * Copyright (c) 2014 Gabriele Mazzotta <gabriele.mzt@gmail.com>
  7. * Copyright (c) 2014 Pali Rohár <pali@kernel.org>
  8. *
  9. * Based on documentation in the libsmbios package:
  10. * Copyright (C) 2005-2014 Dell Inc.
  11. */
  12. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  13. #include <linux/container_of.h>
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/capability.h>
  17. #include <linux/dmi.h>
  18. #include <linux/err.h>
  19. #include <linux/mutex.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/slab.h>
  22. #include "dell-smbios.h"
  23. static u32 da_supported_commands;
  24. static int da_num_tokens;
  25. static struct platform_device *platform_device;
  26. static struct calling_interface_token *da_tokens;
  27. static struct token_sysfs_data *token_entries;
  28. static struct attribute **token_attrs;
  29. static DEFINE_MUTEX(smbios_mutex);
  30. struct token_sysfs_data {
  31. struct device_attribute location_attr;
  32. struct device_attribute value_attr;
  33. struct calling_interface_token *token;
  34. };
  35. struct smbios_device {
  36. struct list_head list;
  37. struct device *device;
  38. int priority;
  39. int (*call_fn)(struct calling_interface_buffer *arg);
  40. };
  41. struct smbios_call {
  42. u32 need_capability;
  43. int cmd_class;
  44. int cmd_select;
  45. };
  46. /* calls that are whitelisted for given capabilities */
  47. static struct smbios_call call_whitelist[] = {
  48. /* generally tokens are allowed, but may be further filtered or
  49. * restricted by token blacklist or whitelist
  50. */
  51. {CAP_SYS_ADMIN, CLASS_TOKEN_READ, SELECT_TOKEN_STD},
  52. {CAP_SYS_ADMIN, CLASS_TOKEN_READ, SELECT_TOKEN_AC},
  53. {CAP_SYS_ADMIN, CLASS_TOKEN_READ, SELECT_TOKEN_BAT},
  54. {CAP_SYS_ADMIN, CLASS_TOKEN_WRITE, SELECT_TOKEN_STD},
  55. {CAP_SYS_ADMIN, CLASS_TOKEN_WRITE, SELECT_TOKEN_AC},
  56. {CAP_SYS_ADMIN, CLASS_TOKEN_WRITE, SELECT_TOKEN_BAT},
  57. /* used by userspace: fwupdate */
  58. {CAP_SYS_ADMIN, CLASS_ADMIN_PROP, SELECT_ADMIN_PROP},
  59. /* used by userspace: fwupd */
  60. {CAP_SYS_ADMIN, CLASS_INFO, SELECT_DOCK},
  61. {CAP_SYS_ADMIN, CLASS_FLASH_INTERFACE, SELECT_FLASH_INTERFACE},
  62. };
  63. /* calls that are explicitly blacklisted */
  64. static struct smbios_call call_blacklist[] = {
  65. {0x0000, 1, 7}, /* manufacturing use */
  66. {0x0000, 6, 5}, /* manufacturing use */
  67. {0x0000, 11, 3}, /* write once */
  68. {0x0000, 11, 7}, /* write once */
  69. {0x0000, 11, 11}, /* write once */
  70. {0x0000, 19, -1}, /* diagnostics */
  71. /* handled by kernel: dell-laptop */
  72. {0x0000, CLASS_INFO, SELECT_RFKILL},
  73. {0x0000, CLASS_KBD_BACKLIGHT, SELECT_KBD_BACKLIGHT},
  74. {0x0000, CLASS_INFO, SELECT_THERMAL_MANAGEMENT},
  75. };
  76. struct token_range {
  77. u32 need_capability;
  78. u16 min;
  79. u16 max;
  80. };
  81. /* tokens that are whitelisted for given capabilities */
  82. static struct token_range token_whitelist[] = {
  83. /* used by userspace: fwupdate */
  84. {CAP_SYS_ADMIN, CAPSULE_EN_TOKEN, CAPSULE_DIS_TOKEN},
  85. /* can indicate to userspace that WMI is needed */
  86. {0x0000, WSMT_EN_TOKEN, WSMT_DIS_TOKEN}
  87. };
  88. /* tokens that are explicitly blacklisted */
  89. static struct token_range token_blacklist[] = {
  90. {0x0000, 0x0058, 0x0059}, /* ME use */
  91. {0x0000, 0x00CD, 0x00D0}, /* raid shadow copy */
  92. {0x0000, 0x013A, 0x01FF}, /* sata shadow copy */
  93. {0x0000, 0x0175, 0x0176}, /* write once */
  94. {0x0000, 0x0195, 0x0197}, /* diagnostics */
  95. {0x0000, 0x01DC, 0x01DD}, /* manufacturing use */
  96. {0x0000, 0x027D, 0x0284}, /* diagnostics */
  97. {0x0000, 0x02E3, 0x02E3}, /* manufacturing use */
  98. {0x0000, 0x02FF, 0x02FF}, /* manufacturing use */
  99. {0x0000, 0x0300, 0x0302}, /* manufacturing use */
  100. {0x0000, 0x0325, 0x0326}, /* manufacturing use */
  101. {0x0000, 0x0332, 0x0335}, /* fan control */
  102. {0x0000, 0x0350, 0x0350}, /* manufacturing use */
  103. {0x0000, 0x0363, 0x0363}, /* manufacturing use */
  104. {0x0000, 0x0368, 0x0368}, /* manufacturing use */
  105. {0x0000, 0x03F6, 0x03F7}, /* manufacturing use */
  106. {0x0000, 0x049E, 0x049F}, /* manufacturing use */
  107. {0x0000, 0x04A0, 0x04A3}, /* disagnostics */
  108. {0x0000, 0x04E6, 0x04E7}, /* manufacturing use */
  109. {0x0000, 0x4000, 0x7FFF}, /* internal BIOS use */
  110. {0x0000, 0x9000, 0x9001}, /* internal BIOS use */
  111. {0x0000, 0xA000, 0xBFFF}, /* write only */
  112. {0x0000, 0xEFF0, 0xEFFF}, /* internal BIOS use */
  113. /* handled by kernel: dell-laptop */
  114. {0x0000, BRIGHTNESS_TOKEN, BRIGHTNESS_TOKEN},
  115. {0x0000, KBD_LED_OFF_TOKEN, KBD_LED_AUTO_TOKEN},
  116. {0x0000, KBD_LED_AC_TOKEN, KBD_LED_AC_TOKEN},
  117. {0x0000, KBD_LED_AUTO_25_TOKEN, KBD_LED_AUTO_75_TOKEN},
  118. {0x0000, KBD_LED_AUTO_100_TOKEN, KBD_LED_AUTO_100_TOKEN},
  119. {0x0000, GLOBAL_MIC_MUTE_ENABLE, GLOBAL_MIC_MUTE_DISABLE},
  120. };
  121. static LIST_HEAD(smbios_device_list);
  122. int dell_smbios_error(int value)
  123. {
  124. switch (value) {
  125. case 0: /* Completed successfully */
  126. return 0;
  127. case -1: /* Completed with error */
  128. return -EIO;
  129. case -2: /* Function not supported */
  130. return -ENXIO;
  131. default: /* Unknown error */
  132. return -EINVAL;
  133. }
  134. }
  135. EXPORT_SYMBOL_GPL(dell_smbios_error);
  136. int dell_smbios_register_device(struct device *d, int priority, void *call_fn)
  137. {
  138. struct smbios_device *priv;
  139. priv = devm_kzalloc(d, sizeof(struct smbios_device), GFP_KERNEL);
  140. if (!priv)
  141. return -ENOMEM;
  142. get_device(d);
  143. priv->device = d;
  144. priv->priority = priority;
  145. priv->call_fn = call_fn;
  146. mutex_lock(&smbios_mutex);
  147. list_add_tail(&priv->list, &smbios_device_list);
  148. mutex_unlock(&smbios_mutex);
  149. dev_dbg(d, "Added device: %s\n", d->driver->name);
  150. return 0;
  151. }
  152. EXPORT_SYMBOL_GPL(dell_smbios_register_device);
  153. void dell_smbios_unregister_device(struct device *d)
  154. {
  155. struct smbios_device *priv;
  156. mutex_lock(&smbios_mutex);
  157. list_for_each_entry(priv, &smbios_device_list, list) {
  158. if (priv->device == d) {
  159. list_del(&priv->list);
  160. put_device(d);
  161. break;
  162. }
  163. }
  164. mutex_unlock(&smbios_mutex);
  165. dev_dbg(d, "Remove device: %s\n", d->driver->name);
  166. }
  167. EXPORT_SYMBOL_GPL(dell_smbios_unregister_device);
  168. int dell_smbios_call_filter(struct device *d,
  169. struct calling_interface_buffer *buffer)
  170. {
  171. u16 t = 0;
  172. int i;
  173. /* can't make calls over 30 */
  174. if (buffer->cmd_class > 30) {
  175. dev_dbg(d, "class too big: %u\n", buffer->cmd_class);
  176. return -EINVAL;
  177. }
  178. /* supported calls on the particular system */
  179. if (!(da_supported_commands & (1 << buffer->cmd_class))) {
  180. dev_dbg(d, "invalid command, supported commands: 0x%8x\n",
  181. da_supported_commands);
  182. return -EINVAL;
  183. }
  184. /* match against call blacklist */
  185. for (i = 0; i < ARRAY_SIZE(call_blacklist); i++) {
  186. if (buffer->cmd_class != call_blacklist[i].cmd_class)
  187. continue;
  188. if (buffer->cmd_select != call_blacklist[i].cmd_select &&
  189. call_blacklist[i].cmd_select != -1)
  190. continue;
  191. dev_dbg(d, "blacklisted command: %u/%u\n",
  192. buffer->cmd_class, buffer->cmd_select);
  193. return -EINVAL;
  194. }
  195. /* if a token call, find token ID */
  196. if ((buffer->cmd_class == CLASS_TOKEN_READ ||
  197. buffer->cmd_class == CLASS_TOKEN_WRITE) &&
  198. buffer->cmd_select < 3) {
  199. /* tokens enabled ? */
  200. if (!da_tokens) {
  201. dev_dbg(d, "no token support on this system\n");
  202. return -EINVAL;
  203. }
  204. /* find the matching token ID */
  205. for (i = 0; i < da_num_tokens; i++) {
  206. if (da_tokens[i].location != buffer->input[0])
  207. continue;
  208. t = da_tokens[i].tokenID;
  209. break;
  210. }
  211. /* token call; but token didn't exist */
  212. if (!t) {
  213. dev_dbg(d, "token at location %04x doesn't exist\n",
  214. buffer->input[0]);
  215. return -EINVAL;
  216. }
  217. /* match against token blacklist */
  218. for (i = 0; i < ARRAY_SIZE(token_blacklist); i++) {
  219. if (!token_blacklist[i].min || !token_blacklist[i].max)
  220. continue;
  221. if (t >= token_blacklist[i].min &&
  222. t <= token_blacklist[i].max)
  223. return -EINVAL;
  224. }
  225. /* match against token whitelist */
  226. for (i = 0; i < ARRAY_SIZE(token_whitelist); i++) {
  227. if (!token_whitelist[i].min || !token_whitelist[i].max)
  228. continue;
  229. if (t < token_whitelist[i].min ||
  230. t > token_whitelist[i].max)
  231. continue;
  232. if (!token_whitelist[i].need_capability ||
  233. capable(token_whitelist[i].need_capability)) {
  234. dev_dbg(d, "whitelisted token: %x\n", t);
  235. return 0;
  236. }
  237. }
  238. }
  239. /* match against call whitelist */
  240. for (i = 0; i < ARRAY_SIZE(call_whitelist); i++) {
  241. if (buffer->cmd_class != call_whitelist[i].cmd_class)
  242. continue;
  243. if (buffer->cmd_select != call_whitelist[i].cmd_select)
  244. continue;
  245. if (!call_whitelist[i].need_capability ||
  246. capable(call_whitelist[i].need_capability)) {
  247. dev_dbg(d, "whitelisted capable command: %u/%u\n",
  248. buffer->cmd_class, buffer->cmd_select);
  249. return 0;
  250. }
  251. dev_dbg(d, "missing capability %d for %u/%u\n",
  252. call_whitelist[i].need_capability,
  253. buffer->cmd_class, buffer->cmd_select);
  254. }
  255. /* not in a whitelist, only allow processes with capabilities */
  256. if (capable(CAP_SYS_RAWIO)) {
  257. dev_dbg(d, "Allowing %u/%u due to CAP_SYS_RAWIO\n",
  258. buffer->cmd_class, buffer->cmd_select);
  259. return 0;
  260. }
  261. return -EACCES;
  262. }
  263. EXPORT_SYMBOL_GPL(dell_smbios_call_filter);
  264. int dell_smbios_call(struct calling_interface_buffer *buffer)
  265. {
  266. struct smbios_device *selected = NULL;
  267. struct smbios_device *priv;
  268. int ret;
  269. mutex_lock(&smbios_mutex);
  270. list_for_each_entry(priv, &smbios_device_list, list) {
  271. if (!selected || priv->priority >= selected->priority) {
  272. dev_dbg(priv->device, "Trying device ID: %d\n", priv->priority);
  273. selected = priv;
  274. }
  275. }
  276. if (!selected) {
  277. ret = -ENODEV;
  278. pr_err("No dell-smbios drivers are loaded\n");
  279. goto out_smbios_call;
  280. }
  281. ret = selected->call_fn(buffer);
  282. out_smbios_call:
  283. mutex_unlock(&smbios_mutex);
  284. return ret;
  285. }
  286. EXPORT_SYMBOL_GPL(dell_smbios_call);
  287. void dell_fill_request(struct calling_interface_buffer *buffer,
  288. u32 arg0, u32 arg1, u32 arg2, u32 arg3)
  289. {
  290. memset(buffer, 0, sizeof(struct calling_interface_buffer));
  291. buffer->input[0] = arg0;
  292. buffer->input[1] = arg1;
  293. buffer->input[2] = arg2;
  294. buffer->input[3] = arg3;
  295. }
  296. EXPORT_SYMBOL_GPL(dell_fill_request);
  297. int dell_send_request(struct calling_interface_buffer *buffer,
  298. u16 class, u16 select)
  299. {
  300. int ret;
  301. buffer->cmd_class = class;
  302. buffer->cmd_select = select;
  303. ret = dell_smbios_call(buffer);
  304. if (ret != 0)
  305. return ret;
  306. return dell_smbios_error(buffer->output[0]);
  307. }
  308. EXPORT_SYMBOL_GPL(dell_send_request);
  309. struct calling_interface_token *dell_smbios_find_token(int tokenid)
  310. {
  311. int i;
  312. if (!da_tokens)
  313. return NULL;
  314. for (i = 0; i < da_num_tokens; i++) {
  315. if (da_tokens[i].tokenID == tokenid)
  316. return &da_tokens[i];
  317. }
  318. return NULL;
  319. }
  320. EXPORT_SYMBOL_GPL(dell_smbios_find_token);
  321. static BLOCKING_NOTIFIER_HEAD(dell_laptop_chain_head);
  322. int dell_laptop_register_notifier(struct notifier_block *nb)
  323. {
  324. return blocking_notifier_chain_register(&dell_laptop_chain_head, nb);
  325. }
  326. EXPORT_SYMBOL_GPL(dell_laptop_register_notifier);
  327. int dell_laptop_unregister_notifier(struct notifier_block *nb)
  328. {
  329. return blocking_notifier_chain_unregister(&dell_laptop_chain_head, nb);
  330. }
  331. EXPORT_SYMBOL_GPL(dell_laptop_unregister_notifier);
  332. void dell_laptop_call_notifier(unsigned long action, void *data)
  333. {
  334. blocking_notifier_call_chain(&dell_laptop_chain_head, action, data);
  335. }
  336. EXPORT_SYMBOL_GPL(dell_laptop_call_notifier);
  337. bool dell_smbios_class_is_supported(u16 class)
  338. {
  339. /* Classes over 30 always unsupported */
  340. if (class > 30)
  341. return false;
  342. return da_supported_commands & (1 << class);
  343. }
  344. EXPORT_SYMBOL_GPL(dell_smbios_class_is_supported);
  345. static void __init parse_da_table(const struct dmi_header *dm)
  346. {
  347. /* Final token is a terminator, so we don't want to copy it */
  348. int tokens = (dm->length-11)/sizeof(struct calling_interface_token)-1;
  349. struct calling_interface_token *new_da_tokens;
  350. struct calling_interface_structure *table =
  351. container_of(dm, struct calling_interface_structure, header);
  352. /*
  353. * 4 bytes of table header, plus 7 bytes of Dell header
  354. * plus at least 6 bytes of entry
  355. */
  356. if (dm->length < 17)
  357. return;
  358. da_supported_commands = table->supportedCmds;
  359. new_da_tokens = krealloc(da_tokens, (da_num_tokens + tokens) *
  360. sizeof(struct calling_interface_token),
  361. GFP_KERNEL);
  362. if (!new_da_tokens)
  363. return;
  364. da_tokens = new_da_tokens;
  365. memcpy(da_tokens+da_num_tokens, table->tokens,
  366. sizeof(struct calling_interface_token) * tokens);
  367. da_num_tokens += tokens;
  368. }
  369. static void zero_duplicates(struct device *dev)
  370. {
  371. int i, j;
  372. for (i = 0; i < da_num_tokens; i++) {
  373. if (da_tokens[i].tokenID == 0)
  374. continue;
  375. for (j = i+1; j < da_num_tokens; j++) {
  376. if (da_tokens[j].tokenID == 0)
  377. continue;
  378. if (da_tokens[i].tokenID == da_tokens[j].tokenID) {
  379. dev_dbg(dev, "Zeroing dup token ID %x(%x/%x)\n",
  380. da_tokens[j].tokenID,
  381. da_tokens[j].location,
  382. da_tokens[j].value);
  383. da_tokens[j].tokenID = 0;
  384. }
  385. }
  386. }
  387. }
  388. static void __init find_tokens(const struct dmi_header *dm, void *dummy)
  389. {
  390. switch (dm->type) {
  391. case 0xd4: /* Indexed IO */
  392. case 0xd5: /* Protected Area Type 1 */
  393. case 0xd6: /* Protected Area Type 2 */
  394. break;
  395. case 0xda: /* Calling interface */
  396. parse_da_table(dm);
  397. break;
  398. }
  399. }
  400. static ssize_t location_show(struct device *dev,
  401. struct device_attribute *attr, char *buf)
  402. {
  403. struct token_sysfs_data *data = container_of(attr, struct token_sysfs_data, location_attr);
  404. if (!capable(CAP_SYS_ADMIN))
  405. return -EPERM;
  406. return sysfs_emit(buf, "%08x", data->token->location);
  407. }
  408. static ssize_t value_show(struct device *dev,
  409. struct device_attribute *attr, char *buf)
  410. {
  411. struct token_sysfs_data *data = container_of(attr, struct token_sysfs_data, value_attr);
  412. if (!capable(CAP_SYS_ADMIN))
  413. return -EPERM;
  414. return sysfs_emit(buf, "%08x", data->token->value);
  415. }
  416. static struct attribute_group smbios_attribute_group = {
  417. .name = "tokens"
  418. };
  419. static struct platform_driver platform_driver = {
  420. .driver = {
  421. .name = "dell-smbios",
  422. },
  423. };
  424. static int build_tokens_sysfs(struct platform_device *dev)
  425. {
  426. char *location_name;
  427. char *value_name;
  428. int ret;
  429. int i, j;
  430. token_entries = kzalloc_objs(*token_entries, da_num_tokens);
  431. if (!token_entries)
  432. return -ENOMEM;
  433. /* need to store both location and value + terminator*/
  434. token_attrs = kzalloc_objs(*token_attrs, (2 * da_num_tokens) + 1);
  435. if (!token_attrs)
  436. goto out_allocate_attrs;
  437. for (i = 0, j = 0; i < da_num_tokens; i++) {
  438. /* skip empty */
  439. if (da_tokens[i].tokenID == 0)
  440. continue;
  441. token_entries[i].token = &da_tokens[i];
  442. /* add location */
  443. location_name = kasprintf(GFP_KERNEL, "%04x_location",
  444. da_tokens[i].tokenID);
  445. if (location_name == NULL)
  446. goto out_unwind_strings;
  447. sysfs_attr_init(&token_entries[i].location_attr.attr);
  448. token_entries[i].location_attr.attr.name = location_name;
  449. token_entries[i].location_attr.attr.mode = 0444;
  450. token_entries[i].location_attr.show = location_show;
  451. token_attrs[j++] = &token_entries[i].location_attr.attr;
  452. /* add value */
  453. value_name = kasprintf(GFP_KERNEL, "%04x_value",
  454. da_tokens[i].tokenID);
  455. if (!value_name) {
  456. kfree(location_name);
  457. goto out_unwind_strings;
  458. }
  459. sysfs_attr_init(&token_entries[i].value_attr.attr);
  460. token_entries[i].value_attr.attr.name = value_name;
  461. token_entries[i].value_attr.attr.mode = 0444;
  462. token_entries[i].value_attr.show = value_show;
  463. token_attrs[j++] = &token_entries[i].value_attr.attr;
  464. }
  465. smbios_attribute_group.attrs = token_attrs;
  466. ret = sysfs_create_group(&dev->dev.kobj, &smbios_attribute_group);
  467. if (ret)
  468. goto out_unwind_strings;
  469. return 0;
  470. out_unwind_strings:
  471. while (i--) {
  472. kfree(token_entries[i].location_attr.attr.name);
  473. kfree(token_entries[i].value_attr.attr.name);
  474. }
  475. kfree(token_attrs);
  476. out_allocate_attrs:
  477. kfree(token_entries);
  478. return -ENOMEM;
  479. }
  480. static void free_group(struct platform_device *pdev)
  481. {
  482. int i;
  483. sysfs_remove_group(&pdev->dev.kobj,
  484. &smbios_attribute_group);
  485. for (i = 0; i < da_num_tokens; i++) {
  486. kfree(token_entries[i].location_attr.attr.name);
  487. kfree(token_entries[i].value_attr.attr.name);
  488. }
  489. kfree(token_attrs);
  490. kfree(token_entries);
  491. }
  492. static int __init dell_smbios_init(void)
  493. {
  494. int ret, wmi, smm;
  495. if (!dmi_find_device(DMI_DEV_TYPE_OEM_STRING, "Dell System", NULL) &&
  496. !dmi_find_device(DMI_DEV_TYPE_OEM_STRING, "Alienware", NULL) &&
  497. !dmi_find_device(DMI_DEV_TYPE_OEM_STRING, "www.dell.com", NULL)) {
  498. pr_err("Unable to run on non-Dell system\n");
  499. return -ENODEV;
  500. }
  501. dmi_walk(find_tokens, NULL);
  502. ret = platform_driver_register(&platform_driver);
  503. if (ret)
  504. goto fail_platform_driver;
  505. platform_device = platform_device_alloc("dell-smbios", 0);
  506. if (!platform_device) {
  507. ret = -ENOMEM;
  508. goto fail_platform_device_alloc;
  509. }
  510. ret = platform_device_add(platform_device);
  511. if (ret)
  512. goto fail_platform_device_add;
  513. /* register backends */
  514. wmi = init_dell_smbios_wmi();
  515. if (wmi)
  516. pr_debug("Failed to initialize WMI backend: %d\n", wmi);
  517. smm = init_dell_smbios_smm();
  518. if (smm)
  519. pr_debug("Failed to initialize SMM backend: %d\n", smm);
  520. if (wmi && smm) {
  521. pr_err("No SMBIOS backends available (wmi: %d, smm: %d)\n",
  522. wmi, smm);
  523. ret = -ENODEV;
  524. goto fail_create_group;
  525. }
  526. if (da_tokens) {
  527. /* duplicate tokens will cause problems building sysfs files */
  528. zero_duplicates(&platform_device->dev);
  529. ret = build_tokens_sysfs(platform_device);
  530. if (ret)
  531. goto fail_sysfs;
  532. }
  533. return 0;
  534. fail_sysfs:
  535. if (!wmi)
  536. exit_dell_smbios_wmi();
  537. if (!smm)
  538. exit_dell_smbios_smm();
  539. fail_create_group:
  540. platform_device_del(platform_device);
  541. fail_platform_device_add:
  542. platform_device_put(platform_device);
  543. fail_platform_device_alloc:
  544. platform_driver_unregister(&platform_driver);
  545. fail_platform_driver:
  546. kfree(da_tokens);
  547. return ret;
  548. }
  549. static void __exit dell_smbios_exit(void)
  550. {
  551. exit_dell_smbios_wmi();
  552. exit_dell_smbios_smm();
  553. mutex_lock(&smbios_mutex);
  554. if (platform_device) {
  555. if (da_tokens)
  556. free_group(platform_device);
  557. platform_device_unregister(platform_device);
  558. platform_driver_unregister(&platform_driver);
  559. }
  560. kfree(da_tokens);
  561. mutex_unlock(&smbios_mutex);
  562. }
  563. module_init(dell_smbios_init);
  564. module_exit(dell_smbios_exit);
  565. MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
  566. MODULE_AUTHOR("Gabriele Mazzotta <gabriele.mzt@gmail.com>");
  567. MODULE_AUTHOR("Pali Rohár <pali@kernel.org>");
  568. MODULE_AUTHOR("Mario Limonciello <mario.limonciello@outlook.com>");
  569. MODULE_DESCRIPTION("Common functions for kernel modules using Dell SMBIOS");
  570. MODULE_LICENSE("GPL");