dell-smbios-base.c 17 KB

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