m68k: Fix asm register constraints for atomic ops
[sfrench/cifs-2.6.git] / drivers / platform / x86 / think-lmi.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Think LMI BIOS configuration driver
4  *
5  * Copyright(C) 2019-2021 Lenovo
6  *
7  * Original code from Thinkpad-wmi project https://github.com/iksaif/thinkpad-wmi
8  * Copyright(C) 2017 Corentin Chary <corentin.chary@gmail.com>
9  * Distributed under the GPL-2.0 license
10  */
11
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
14 #include <linux/acpi.h>
15 #include <linux/errno.h>
16 #include <linux/fs.h>
17 #include <linux/string.h>
18 #include <linux/types.h>
19 #include <linux/wmi.h>
20 #include "firmware_attributes_class.h"
21 #include "think-lmi.h"
22
23 /*
24  * Name:
25  *  Lenovo_BiosSetting
26  * Description:
27  *  Get item name and settings for current LMI instance.
28  * Type:
29  *  Query
30  * Returns:
31  *  "Item,Value"
32  * Example:
33  *  "WakeOnLAN,Enable"
34  */
35 #define LENOVO_BIOS_SETTING_GUID "51F5230E-9677-46CD-A1CF-C0B23EE34DB7"
36
37 /*
38  * Name:
39  *  Lenovo_SetBiosSetting
40  * Description:
41  *  Change the BIOS setting to the desired value using the Lenovo_SetBiosSetting
42  *  class. To save the settings, use the Lenovo_SaveBiosSetting class.
43  *  BIOS settings and values are case sensitive.
44  *  After making changes to the BIOS settings, you must reboot the computer
45  *  before the changes will take effect.
46  * Type:
47  *  Method
48  * Arguments:
49  *  "Item,Value,Password,Encoding,KbdLang;"
50  * Example:
51  *  "WakeOnLAN,Disable,pa55w0rd,ascii,us;"
52  */
53 #define LENOVO_SET_BIOS_SETTINGS_GUID "98479A64-33F5-4E33-A707-8E251EBBC3A1"
54
55 /*
56  * Name:
57  *  Lenovo_SaveBiosSettings
58  * Description:
59  *  Save any pending changes in settings.
60  * Type:
61  *  Method
62  * Arguments:
63  *  "Password,Encoding,KbdLang;"
64  * Example:
65  * "pa55w0rd,ascii,us;"
66  */
67 #define LENOVO_SAVE_BIOS_SETTINGS_GUID "6A4B54EF-A5ED-4D33-9455-B0D9B48DF4B3"
68
69 /*
70  * Name:
71  *  Lenovo_BiosPasswordSettings
72  * Description:
73  *  Return BIOS Password settings
74  * Type:
75  *  Query
76  * Returns:
77  *  PasswordMode, PasswordState, MinLength, MaxLength,
78  *  SupportedEncoding, SupportedKeyboard
79  */
80 #define LENOVO_BIOS_PASSWORD_SETTINGS_GUID "8ADB159E-1E32-455C-BC93-308A7ED98246"
81
82 /*
83  * Name:
84  *  Lenovo_SetBiosPassword
85  * Description:
86  *  Change a specific password.
87  *  - BIOS settings cannot be changed at the same boot as power-on
88  *    passwords (POP) and hard disk passwords (HDP). If you want to change
89  *    BIOS settings and POP or HDP, you must reboot the system after changing
90  *    one of them.
91  *  - A password cannot be set using this method when one does not already
92  *    exist. Passwords can only be updated or cleared.
93  * Type:
94  *  Method
95  * Arguments:
96  *  "PasswordType,CurrentPassword,NewPassword,Encoding,KbdLang;"
97  * Example:
98  *  "pop,pa55w0rd,newpa55w0rd,ascii,us;”
99  */
100 #define LENOVO_SET_BIOS_PASSWORD_GUID "2651D9FD-911C-4B69-B94E-D0DED5963BD7"
101
102 /*
103  * Name:
104  *  Lenovo_GetBiosSelections
105  * Description:
106  *  Return a list of valid settings for a given item.
107  * Type:
108  *  Method
109  * Arguments:
110  *  "Item"
111  * Returns:
112  *  "Value1,Value2,Value3,..."
113  * Example:
114  *  -> "FlashOverLAN"
115  *  <- "Enabled,Disabled"
116  */
117 #define LENOVO_GET_BIOS_SELECTIONS_GUID "7364651A-132F-4FE7-ADAA-40C6C7EE2E3B"
118
119 #define TLMI_POP_PWD (1 << 0)
120 #define TLMI_PAP_PWD (1 << 1)
121 #define to_tlmi_pwd_setting(kobj)  container_of(kobj, struct tlmi_pwd_setting, kobj)
122 #define to_tlmi_attr_setting(kobj)  container_of(kobj, struct tlmi_attr_setting, kobj)
123
124 static const struct tlmi_err_codes tlmi_errs[] = {
125         {"Success", 0},
126         {"Not Supported", -EOPNOTSUPP},
127         {"Invalid Parameter", -EINVAL},
128         {"Access Denied", -EACCES},
129         {"System Busy", -EBUSY},
130 };
131
132 static const char * const encoding_options[] = {
133         [TLMI_ENCODING_ASCII] = "ascii",
134         [TLMI_ENCODING_SCANCODE] = "scancode",
135 };
136 static struct think_lmi tlmi_priv;
137 static struct class *fw_attr_class;
138
139 /* ------ Utility functions ------------*/
140 /* Convert BIOS WMI error string to suitable error code */
141 static int tlmi_errstr_to_err(const char *errstr)
142 {
143         int i;
144
145         for (i = 0; i < sizeof(tlmi_errs)/sizeof(struct tlmi_err_codes); i++) {
146                 if (!strcmp(tlmi_errs[i].err_str, errstr))
147                         return tlmi_errs[i].err_code;
148         }
149         return -EPERM;
150 }
151
152 /* Extract error string from WMI return buffer */
153 static int tlmi_extract_error(const struct acpi_buffer *output)
154 {
155         const union acpi_object *obj;
156
157         obj = output->pointer;
158         if (!obj)
159                 return -ENOMEM;
160         if (obj->type != ACPI_TYPE_STRING || !obj->string.pointer)
161                 return -EIO;
162
163         return tlmi_errstr_to_err(obj->string.pointer);
164 }
165
166 /* Utility function to execute WMI call to BIOS */
167 static int tlmi_simple_call(const char *guid, const char *arg)
168 {
169         const struct acpi_buffer input = { strlen(arg), (char *)arg };
170         struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
171         acpi_status status;
172         int i, err;
173
174         /*
175          * Duplicated call required to match BIOS workaround for behavior
176          * seen when WMI accessed via scripting on other OS.
177          */
178         for (i = 0; i < 2; i++) {
179                 /* (re)initialize output buffer to default state */
180                 output.length = ACPI_ALLOCATE_BUFFER;
181                 output.pointer = NULL;
182
183                 status = wmi_evaluate_method(guid, 0, 0, &input, &output);
184                 if (ACPI_FAILURE(status)) {
185                         kfree(output.pointer);
186                         return -EIO;
187                 }
188                 err = tlmi_extract_error(&output);
189                 kfree(output.pointer);
190                 if (err)
191                         return err;
192         }
193         return 0;
194 }
195
196 /* Extract output string from WMI return buffer */
197 static int tlmi_extract_output_string(const struct acpi_buffer *output,
198                                       char **string)
199 {
200         const union acpi_object *obj;
201         char *s;
202
203         obj = output->pointer;
204         if (!obj)
205                 return -ENOMEM;
206         if (obj->type != ACPI_TYPE_STRING || !obj->string.pointer)
207                 return -EIO;
208
209         s = kstrdup(obj->string.pointer, GFP_KERNEL);
210         if (!s)
211                 return -ENOMEM;
212         *string = s;
213         return 0;
214 }
215
216 /* ------ Core interface functions ------------*/
217
218 /* Get password settings from BIOS */
219 static int tlmi_get_pwd_settings(struct tlmi_pwdcfg *pwdcfg)
220 {
221         struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
222         const union acpi_object *obj;
223         acpi_status status;
224
225         if (!tlmi_priv.can_get_password_settings)
226                 return -EOPNOTSUPP;
227
228         status = wmi_query_block(LENOVO_BIOS_PASSWORD_SETTINGS_GUID, 0,
229                                  &output);
230         if (ACPI_FAILURE(status))
231                 return -EIO;
232
233         obj = output.pointer;
234         if (!obj)
235                 return -ENOMEM;
236         if (obj->type != ACPI_TYPE_BUFFER || !obj->buffer.pointer) {
237                 kfree(obj);
238                 return -EIO;
239         }
240         /*
241          * The size of thinkpad_wmi_pcfg on ThinkStation is larger than ThinkPad.
242          * To make the driver compatible on different brands, we permit it to get
243          * the data in below case.
244          */
245         if (obj->buffer.length < sizeof(struct tlmi_pwdcfg)) {
246                 pr_warn("Unknown pwdcfg buffer length %d\n", obj->buffer.length);
247                 kfree(obj);
248                 return -EIO;
249         }
250         memcpy(pwdcfg, obj->buffer.pointer, sizeof(struct tlmi_pwdcfg));
251         kfree(obj);
252         return 0;
253 }
254
255 static int tlmi_save_bios_settings(const char *password)
256 {
257         return tlmi_simple_call(LENOVO_SAVE_BIOS_SETTINGS_GUID,
258                                 password);
259 }
260
261 static int tlmi_setting(int item, char **value, const char *guid_string)
262 {
263         struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
264         acpi_status status;
265         int ret;
266
267         status = wmi_query_block(guid_string, item, &output);
268         if (ACPI_FAILURE(status)) {
269                 kfree(output.pointer);
270                 return -EIO;
271         }
272
273         ret = tlmi_extract_output_string(&output, value);
274         kfree(output.pointer);
275         return ret;
276 }
277
278 static int tlmi_get_bios_selections(const char *item, char **value)
279 {
280         const struct acpi_buffer input = { strlen(item), (char *)item };
281         struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
282         acpi_status status;
283         int ret;
284
285         status = wmi_evaluate_method(LENOVO_GET_BIOS_SELECTIONS_GUID,
286                                      0, 0, &input, &output);
287
288         if (ACPI_FAILURE(status)) {
289                 kfree(output.pointer);
290                 return -EIO;
291         }
292
293         ret = tlmi_extract_output_string(&output, value);
294         kfree(output.pointer);
295         return ret;
296 }
297
298 /* ---- Authentication sysfs --------------------------------------------------------- */
299 static ssize_t is_enabled_show(struct kobject *kobj, struct kobj_attribute *attr,
300                                           char *buf)
301 {
302         struct tlmi_pwd_setting *setting = to_tlmi_pwd_setting(kobj);
303
304         return sysfs_emit(buf, "%d\n", setting->valid);
305 }
306
307 static struct kobj_attribute auth_is_pass_set = __ATTR_RO(is_enabled);
308
309 static ssize_t current_password_store(struct kobject *kobj,
310                                       struct kobj_attribute *attr,
311                                       const char *buf, size_t count)
312 {
313         struct tlmi_pwd_setting *setting = to_tlmi_pwd_setting(kobj);
314         size_t pwdlen;
315         char *p;
316
317         pwdlen = strlen(buf);
318         /* pwdlen == 0 is allowed to clear the password */
319         if (pwdlen && ((pwdlen < setting->minlen) || (pwdlen > setting->maxlen)))
320                 return -EINVAL;
321
322         strscpy(setting->password, buf, setting->maxlen);
323         /* Strip out CR if one is present, setting password won't work if it is present */
324         p = strchrnul(setting->password, '\n');
325         *p = '\0';
326         return count;
327 }
328
329 static struct kobj_attribute auth_current_password = __ATTR_WO(current_password);
330
331 static ssize_t new_password_store(struct kobject *kobj,
332                                   struct kobj_attribute *attr,
333                                   const char *buf, size_t count)
334 {
335         struct tlmi_pwd_setting *setting = to_tlmi_pwd_setting(kobj);
336         char *auth_str, *new_pwd, *p;
337         size_t pwdlen;
338         int ret;
339
340         if (!capable(CAP_SYS_ADMIN))
341                 return -EPERM;
342
343         if (!tlmi_priv.can_set_bios_password)
344                 return -EOPNOTSUPP;
345
346         new_pwd = kstrdup(buf, GFP_KERNEL);
347         if (!new_pwd)
348                 return -ENOMEM;
349
350         /* Strip out CR if one is present, setting password won't work if it is present */
351         p = strchrnul(new_pwd, '\n');
352         *p = '\0';
353
354         pwdlen = strlen(new_pwd);
355         /* pwdlen == 0 is allowed to clear the password */
356         if (pwdlen && ((pwdlen < setting->minlen) || (pwdlen > setting->maxlen))) {
357                 ret = -EINVAL;
358                 goto out;
359         }
360
361         /* Format: 'PasswordType,CurrentPw,NewPw,Encoding,KbdLang;' */
362         auth_str = kasprintf(GFP_KERNEL, "%s,%s,%s,%s,%s;",
363                  setting->pwd_type, setting->password, new_pwd,
364                  encoding_options[setting->encoding], setting->kbdlang);
365         if (!auth_str) {
366                 ret = -ENOMEM;
367                 goto out;
368         }
369         ret = tlmi_simple_call(LENOVO_SET_BIOS_PASSWORD_GUID, auth_str);
370         kfree(auth_str);
371 out:
372         kfree(new_pwd);
373         return ret ?: count;
374 }
375
376 static struct kobj_attribute auth_new_password = __ATTR_WO(new_password);
377
378 static ssize_t min_password_length_show(struct kobject *kobj, struct kobj_attribute *attr,
379                          char *buf)
380 {
381         struct tlmi_pwd_setting *setting = to_tlmi_pwd_setting(kobj);
382
383         return sysfs_emit(buf, "%d\n", setting->minlen);
384 }
385
386 static struct kobj_attribute auth_min_pass_length = __ATTR_RO(min_password_length);
387
388 static ssize_t max_password_length_show(struct kobject *kobj, struct kobj_attribute *attr,
389                          char *buf)
390 {
391         struct tlmi_pwd_setting *setting = to_tlmi_pwd_setting(kobj);
392
393         return sysfs_emit(buf, "%d\n", setting->maxlen);
394 }
395 static struct kobj_attribute auth_max_pass_length = __ATTR_RO(max_password_length);
396
397 static ssize_t mechanism_show(struct kobject *kobj, struct kobj_attribute *attr,
398                          char *buf)
399 {
400         return sysfs_emit(buf, "password\n");
401 }
402 static struct kobj_attribute auth_mechanism = __ATTR_RO(mechanism);
403
404 static ssize_t encoding_show(struct kobject *kobj, struct kobj_attribute *attr,
405                          char *buf)
406 {
407         struct tlmi_pwd_setting *setting = to_tlmi_pwd_setting(kobj);
408
409         return sysfs_emit(buf, "%s\n", encoding_options[setting->encoding]);
410 }
411
412 static ssize_t encoding_store(struct kobject *kobj,
413                                   struct kobj_attribute *attr,
414                                   const char *buf, size_t count)
415 {
416         struct tlmi_pwd_setting *setting = to_tlmi_pwd_setting(kobj);
417         int i;
418
419         /* Scan for a matching profile */
420         i = sysfs_match_string(encoding_options, buf);
421         if (i < 0)
422                 return -EINVAL;
423
424         setting->encoding = i;
425         return count;
426 }
427
428 static struct kobj_attribute auth_encoding = __ATTR_RW(encoding);
429
430 static ssize_t kbdlang_show(struct kobject *kobj, struct kobj_attribute *attr,
431                          char *buf)
432 {
433         struct tlmi_pwd_setting *setting = to_tlmi_pwd_setting(kobj);
434
435         return sysfs_emit(buf, "%s\n", setting->kbdlang);
436 }
437
438 static ssize_t kbdlang_store(struct kobject *kobj,
439                                   struct kobj_attribute *attr,
440                                   const char *buf, size_t count)
441 {
442         struct tlmi_pwd_setting *setting = to_tlmi_pwd_setting(kobj);
443         int length;
444
445         /* Calculate length till '\n' or terminating 0 */
446         length = strchrnul(buf, '\n') - buf;
447         if (!length || length >= TLMI_LANG_MAXLEN)
448                 return -EINVAL;
449
450         memcpy(setting->kbdlang, buf, length);
451         setting->kbdlang[length] = '\0';
452         return count;
453 }
454
455 static struct kobj_attribute auth_kbdlang = __ATTR_RW(kbdlang);
456
457 static ssize_t role_show(struct kobject *kobj, struct kobj_attribute *attr,
458                          char *buf)
459 {
460         struct tlmi_pwd_setting *setting = to_tlmi_pwd_setting(kobj);
461
462         return sysfs_emit(buf, "%s\n", setting->role);
463 }
464 static struct kobj_attribute auth_role = __ATTR_RO(role);
465
466 static struct attribute *auth_attrs[] = {
467         &auth_is_pass_set.attr,
468         &auth_min_pass_length.attr,
469         &auth_max_pass_length.attr,
470         &auth_current_password.attr,
471         &auth_new_password.attr,
472         &auth_role.attr,
473         &auth_mechanism.attr,
474         &auth_encoding.attr,
475         &auth_kbdlang.attr,
476         NULL
477 };
478
479 static const struct attribute_group auth_attr_group = {
480         .attrs = auth_attrs,
481 };
482
483 /* ---- Attributes sysfs --------------------------------------------------------- */
484 static ssize_t display_name_show(struct kobject *kobj, struct kobj_attribute *attr,
485                 char *buf)
486 {
487         struct tlmi_attr_setting *setting = to_tlmi_attr_setting(kobj);
488
489         return sysfs_emit(buf, "%s\n", setting->display_name);
490 }
491
492 static ssize_t current_value_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
493 {
494         struct tlmi_attr_setting *setting = to_tlmi_attr_setting(kobj);
495         char *item, *value;
496         int ret;
497
498         ret = tlmi_setting(setting->index, &item, LENOVO_BIOS_SETTING_GUID);
499         if (ret)
500                 return ret;
501
502         /* validate and split from `item,value` -> `value` */
503         value = strpbrk(item, ",");
504         if (!value || value == item || !strlen(value + 1))
505                 return -EINVAL;
506
507         ret = sysfs_emit(buf, "%s\n", value + 1);
508         kfree(item);
509         return ret;
510 }
511
512 static ssize_t possible_values_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
513 {
514         struct tlmi_attr_setting *setting = to_tlmi_attr_setting(kobj);
515
516         if (!tlmi_priv.can_get_bios_selections)
517                 return -EOPNOTSUPP;
518
519         return sysfs_emit(buf, "%s\n", setting->possible_values);
520 }
521
522 static ssize_t current_value_store(struct kobject *kobj,
523                 struct kobj_attribute *attr,
524                 const char *buf, size_t count)
525 {
526         struct tlmi_attr_setting *setting = to_tlmi_attr_setting(kobj);
527         char *set_str = NULL, *new_setting = NULL;
528         char *auth_str = NULL;
529         char *p;
530         int ret;
531
532         if (!tlmi_priv.can_set_bios_settings)
533                 return -EOPNOTSUPP;
534
535         new_setting = kstrdup(buf, GFP_KERNEL);
536         if (!new_setting)
537                 return -ENOMEM;
538
539         /* Strip out CR if one is present */
540         p = strchrnul(new_setting, '\n');
541         *p = '\0';
542
543         if (tlmi_priv.pwd_admin->valid && tlmi_priv.pwd_admin->password[0]) {
544                 auth_str = kasprintf(GFP_KERNEL, "%s,%s,%s;",
545                                 tlmi_priv.pwd_admin->password,
546                                 encoding_options[tlmi_priv.pwd_admin->encoding],
547                                 tlmi_priv.pwd_admin->kbdlang);
548                 if (!auth_str) {
549                         ret = -ENOMEM;
550                         goto out;
551                 }
552         }
553
554         if (auth_str)
555                 set_str = kasprintf(GFP_KERNEL, "%s,%s,%s", setting->display_name,
556                                 new_setting, auth_str);
557         else
558                 set_str = kasprintf(GFP_KERNEL, "%s,%s;", setting->display_name,
559                                 new_setting);
560         if (!set_str) {
561                 ret = -ENOMEM;
562                 goto out;
563         }
564
565         ret = tlmi_simple_call(LENOVO_SET_BIOS_SETTINGS_GUID, set_str);
566         if (ret)
567                 goto out;
568
569         if (auth_str)
570                 ret = tlmi_save_bios_settings(auth_str);
571         else
572                 ret = tlmi_save_bios_settings("");
573
574 out:
575         kfree(auth_str);
576         kfree(set_str);
577         kfree(new_setting);
578         return ret ?: count;
579 }
580
581 static struct kobj_attribute attr_displ_name = __ATTR_RO(display_name);
582
583 static struct kobj_attribute attr_possible_values = __ATTR_RO(possible_values);
584
585 static struct kobj_attribute attr_current_val = __ATTR_RW_MODE(current_value, 0600);
586
587 static struct attribute *tlmi_attrs[] = {
588         &attr_displ_name.attr,
589         &attr_current_val.attr,
590         &attr_possible_values.attr,
591         NULL
592 };
593
594 static const struct attribute_group tlmi_attr_group = {
595         .attrs = tlmi_attrs,
596 };
597
598 static ssize_t tlmi_attr_show(struct kobject *kobj, struct attribute *attr,
599                                     char *buf)
600 {
601         struct kobj_attribute *kattr;
602
603         kattr = container_of(attr, struct kobj_attribute, attr);
604         if (kattr->show)
605                 return kattr->show(kobj, kattr, buf);
606         return -EIO;
607 }
608
609 static ssize_t tlmi_attr_store(struct kobject *kobj, struct attribute *attr,
610                                      const char *buf, size_t count)
611 {
612         struct kobj_attribute *kattr;
613
614         kattr = container_of(attr, struct kobj_attribute, attr);
615         if (kattr->store)
616                 return kattr->store(kobj, kattr, buf, count);
617         return -EIO;
618 }
619
620 static const struct sysfs_ops tlmi_kobj_sysfs_ops = {
621         .show   = tlmi_attr_show,
622         .store  = tlmi_attr_store,
623 };
624
625 static void tlmi_attr_setting_release(struct kobject *kobj)
626 {
627         struct tlmi_attr_setting *setting = to_tlmi_attr_setting(kobj);
628
629         kfree(setting->possible_values);
630         kfree(setting);
631 }
632
633 static void tlmi_pwd_setting_release(struct kobject *kobj)
634 {
635         struct tlmi_pwd_setting *setting = to_tlmi_pwd_setting(kobj);
636
637         kfree(setting);
638 }
639
640 static struct kobj_type tlmi_attr_setting_ktype = {
641         .release        = &tlmi_attr_setting_release,
642         .sysfs_ops      = &tlmi_kobj_sysfs_ops,
643 };
644
645 static struct kobj_type tlmi_pwd_setting_ktype = {
646         .release        = &tlmi_pwd_setting_release,
647         .sysfs_ops      = &tlmi_kobj_sysfs_ops,
648 };
649
650 /* ---- Initialisation --------------------------------------------------------- */
651 static void tlmi_release_attr(void)
652 {
653         int i;
654
655         /* Attribute structures */
656         for (i = 0; i < TLMI_SETTINGS_COUNT; i++) {
657                 if (tlmi_priv.setting[i]) {
658                         sysfs_remove_group(&tlmi_priv.setting[i]->kobj, &tlmi_attr_group);
659                         kobject_put(&tlmi_priv.setting[i]->kobj);
660                 }
661         }
662         kset_unregister(tlmi_priv.attribute_kset);
663
664         /* Authentication structures */
665         sysfs_remove_group(&tlmi_priv.pwd_admin->kobj, &auth_attr_group);
666         kobject_put(&tlmi_priv.pwd_admin->kobj);
667         sysfs_remove_group(&tlmi_priv.pwd_power->kobj, &auth_attr_group);
668         kobject_put(&tlmi_priv.pwd_power->kobj);
669         kset_unregister(tlmi_priv.authentication_kset);
670 }
671
672 static int tlmi_sysfs_init(void)
673 {
674         int i, ret;
675
676         ret = fw_attributes_class_get(&fw_attr_class);
677         if (ret)
678                 return ret;
679
680         tlmi_priv.class_dev = device_create(fw_attr_class, NULL, MKDEV(0, 0),
681                         NULL, "%s", "thinklmi");
682         if (IS_ERR(tlmi_priv.class_dev)) {
683                 ret = PTR_ERR(tlmi_priv.class_dev);
684                 goto fail_class_created;
685         }
686
687         tlmi_priv.attribute_kset = kset_create_and_add("attributes", NULL,
688                         &tlmi_priv.class_dev->kobj);
689         if (!tlmi_priv.attribute_kset) {
690                 ret = -ENOMEM;
691                 goto fail_device_created;
692         }
693
694         for (i = 0; i < TLMI_SETTINGS_COUNT; i++) {
695                 /* Check if index is a valid setting - skip if it isn't */
696                 if (!tlmi_priv.setting[i])
697                         continue;
698
699                 /* check for duplicate or reserved values */
700                 if (kset_find_obj(tlmi_priv.attribute_kset, tlmi_priv.setting[i]->display_name) ||
701                     !strcmp(tlmi_priv.setting[i]->display_name, "Reserved")) {
702                         pr_debug("duplicate or reserved attribute name found - %s\n",
703                                 tlmi_priv.setting[i]->display_name);
704                         kfree(tlmi_priv.setting[i]->possible_values);
705                         kfree(tlmi_priv.setting[i]);
706                         tlmi_priv.setting[i] = NULL;
707                         continue;
708                 }
709
710                 /* Build attribute */
711                 tlmi_priv.setting[i]->kobj.kset = tlmi_priv.attribute_kset;
712                 ret = kobject_init_and_add(&tlmi_priv.setting[i]->kobj, &tlmi_attr_setting_ktype,
713                                 NULL, "%s", tlmi_priv.setting[i]->display_name);
714                 if (ret)
715                         goto fail_create_attr;
716
717                 ret = sysfs_create_group(&tlmi_priv.setting[i]->kobj, &tlmi_attr_group);
718                 if (ret)
719                         goto fail_create_attr;
720         }
721
722         /* Create authentication entries */
723         tlmi_priv.authentication_kset = kset_create_and_add("authentication", NULL,
724                                                                 &tlmi_priv.class_dev->kobj);
725         if (!tlmi_priv.authentication_kset) {
726                 ret = -ENOMEM;
727                 goto fail_create_attr;
728         }
729         tlmi_priv.pwd_admin->kobj.kset = tlmi_priv.authentication_kset;
730         ret = kobject_init_and_add(&tlmi_priv.pwd_admin->kobj, &tlmi_pwd_setting_ktype,
731                         NULL, "%s", "Admin");
732         if (ret)
733                 goto fail_create_attr;
734
735         ret = sysfs_create_group(&tlmi_priv.pwd_admin->kobj, &auth_attr_group);
736         if (ret)
737                 goto fail_create_attr;
738
739         tlmi_priv.pwd_power->kobj.kset = tlmi_priv.authentication_kset;
740         ret = kobject_init_and_add(&tlmi_priv.pwd_power->kobj, &tlmi_pwd_setting_ktype,
741                         NULL, "%s", "System");
742         if (ret)
743                 goto fail_create_attr;
744
745         ret = sysfs_create_group(&tlmi_priv.pwd_power->kobj, &auth_attr_group);
746         if (ret)
747                 goto fail_create_attr;
748
749         return ret;
750
751 fail_create_attr:
752         tlmi_release_attr();
753 fail_device_created:
754         device_destroy(fw_attr_class, MKDEV(0, 0));
755 fail_class_created:
756         fw_attributes_class_put();
757         return ret;
758 }
759
760 /* ---- Base Driver -------------------------------------------------------- */
761 static int tlmi_analyze(void)
762 {
763         struct tlmi_pwdcfg pwdcfg;
764         acpi_status status;
765         int i, ret;
766
767         if (wmi_has_guid(LENOVO_SET_BIOS_SETTINGS_GUID) &&
768             wmi_has_guid(LENOVO_SAVE_BIOS_SETTINGS_GUID))
769                 tlmi_priv.can_set_bios_settings = true;
770
771         if (wmi_has_guid(LENOVO_GET_BIOS_SELECTIONS_GUID))
772                 tlmi_priv.can_get_bios_selections = true;
773
774         if (wmi_has_guid(LENOVO_SET_BIOS_PASSWORD_GUID))
775                 tlmi_priv.can_set_bios_password = true;
776
777         if (wmi_has_guid(LENOVO_BIOS_PASSWORD_SETTINGS_GUID))
778                 tlmi_priv.can_get_password_settings = true;
779
780         /*
781          * Try to find the number of valid settings of this machine
782          * and use it to create sysfs attributes.
783          */
784         for (i = 0; i < TLMI_SETTINGS_COUNT; ++i) {
785                 struct tlmi_attr_setting *setting;
786                 char *item = NULL;
787                 char *p;
788
789                 tlmi_priv.setting[i] = NULL;
790                 status = tlmi_setting(i, &item, LENOVO_BIOS_SETTING_GUID);
791                 if (ACPI_FAILURE(status))
792                         break;
793                 if (!item)
794                         break;
795                 if (!*item)
796                         continue;
797
798                 /* It is not allowed to have '/' for file name. Convert it into '\'. */
799                 strreplace(item, '/', '\\');
800
801                 /* Remove the value part */
802                 p = strchrnul(item, ',');
803                 *p = '\0';
804
805                 /* Create a setting entry */
806                 setting = kzalloc(sizeof(*setting), GFP_KERNEL);
807                 if (!setting) {
808                         ret = -ENOMEM;
809                         goto fail_clear_attr;
810                 }
811                 setting->index = i;
812                 strscpy(setting->display_name, item, TLMI_SETTINGS_MAXLEN);
813                 /* If BIOS selections supported, load those */
814                 if (tlmi_priv.can_get_bios_selections) {
815                         ret = tlmi_get_bios_selections(setting->display_name,
816                                         &setting->possible_values);
817                         if (ret || !setting->possible_values)
818                                 pr_info("Error retrieving possible values for %d : %s\n",
819                                                 i, setting->display_name);
820                 }
821                 tlmi_priv.setting[i] = setting;
822                 tlmi_priv.settings_count++;
823                 kfree(item);
824         }
825
826         /* Create password setting structure */
827         ret = tlmi_get_pwd_settings(&pwdcfg);
828         if (ret)
829                 goto fail_clear_attr;
830
831         tlmi_priv.pwd_admin = kzalloc(sizeof(struct tlmi_pwd_setting), GFP_KERNEL);
832         if (!tlmi_priv.pwd_admin) {
833                 ret = -ENOMEM;
834                 goto fail_clear_attr;
835         }
836         strscpy(tlmi_priv.pwd_admin->kbdlang, "us", TLMI_LANG_MAXLEN);
837         tlmi_priv.pwd_admin->encoding = TLMI_ENCODING_ASCII;
838         tlmi_priv.pwd_admin->pwd_type = "pap";
839         tlmi_priv.pwd_admin->role = "bios-admin";
840         tlmi_priv.pwd_admin->minlen = pwdcfg.min_length;
841         if (WARN_ON(pwdcfg.max_length >= TLMI_PWD_BUFSIZE))
842                 pwdcfg.max_length = TLMI_PWD_BUFSIZE - 1;
843         tlmi_priv.pwd_admin->maxlen = pwdcfg.max_length;
844         if (pwdcfg.password_state & TLMI_PAP_PWD)
845                 tlmi_priv.pwd_admin->valid = true;
846
847         tlmi_priv.pwd_power = kzalloc(sizeof(struct tlmi_pwd_setting), GFP_KERNEL);
848         if (!tlmi_priv.pwd_power) {
849                 ret = -ENOMEM;
850                 goto fail_clear_attr;
851         }
852         strscpy(tlmi_priv.pwd_power->kbdlang, "us", TLMI_LANG_MAXLEN);
853         tlmi_priv.pwd_power->encoding = TLMI_ENCODING_ASCII;
854         tlmi_priv.pwd_power->pwd_type = "pop";
855         tlmi_priv.pwd_power->role = "power-on";
856         tlmi_priv.pwd_power->minlen = pwdcfg.min_length;
857         tlmi_priv.pwd_power->maxlen = pwdcfg.max_length;
858
859         if (pwdcfg.password_state & TLMI_POP_PWD)
860                 tlmi_priv.pwd_power->valid = true;
861
862         return 0;
863
864 fail_clear_attr:
865         for (i = 0; i < TLMI_SETTINGS_COUNT; ++i)
866                 kfree(tlmi_priv.setting[i]);
867         return ret;
868 }
869
870 static void tlmi_remove(struct wmi_device *wdev)
871 {
872         tlmi_release_attr();
873         device_destroy(fw_attr_class, MKDEV(0, 0));
874         fw_attributes_class_put();
875 }
876
877 static int tlmi_probe(struct wmi_device *wdev, const void *context)
878 {
879         tlmi_analyze();
880         return tlmi_sysfs_init();
881 }
882
883 static const struct wmi_device_id tlmi_id_table[] = {
884         { .guid_string = LENOVO_BIOS_SETTING_GUID },
885         { }
886 };
887 MODULE_DEVICE_TABLE(wmi, tlmi_id_table);
888
889 static struct wmi_driver tlmi_driver = {
890         .driver = {
891                 .name = "think-lmi",
892         },
893         .id_table = tlmi_id_table,
894         .probe = tlmi_probe,
895         .remove = tlmi_remove,
896 };
897
898 MODULE_AUTHOR("Sugumaran L <slacshiminar@lenovo.com>");
899 MODULE_AUTHOR("Mark Pearson <markpearson@lenovo.com>");
900 MODULE_AUTHOR("Corentin Chary <corentin.chary@gmail.com>");
901 MODULE_DESCRIPTION("ThinkLMI Driver");
902 MODULE_LICENSE("GPL");
903
904 module_wmi_driver(tlmi_driver);