]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/platform/x86/dell-laptop.c
b4c2190e89f97f8e2466f45e60b3cbe01965c2bd
[mirror_ubuntu-bionic-kernel.git] / drivers / platform / x86 / dell-laptop.c
1 /*
2 * Driver for Dell laptop extras
3 *
4 * Copyright (c) Red Hat <mjg@redhat.com>
5 *
6 * Based on documentation in the libsmbios package, Copyright (C) 2005 Dell
7 * Inc.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/init.h>
17 #include <linux/platform_device.h>
18 #include <linux/backlight.h>
19 #include <linux/err.h>
20 #include <linux/dmi.h>
21 #include <linux/io.h>
22 #include <linux/rfkill.h>
23 #include <linux/power_supply.h>
24 #include <linux/acpi.h>
25 #include <linux/i8042.h>
26 #include "../../firmware/dcdbas.h"
27
28 #define BRIGHTNESS_TOKEN 0x7d
29
30 /* This structure will be modified by the firmware when we enter
31 * system management mode, hence the volatiles */
32
33 struct calling_interface_buffer {
34 u16 class;
35 u16 select;
36 volatile u32 input[4];
37 volatile u32 output[4];
38 } __packed;
39
40 struct calling_interface_token {
41 u16 tokenID;
42 u16 location;
43 union {
44 u16 value;
45 u16 stringlength;
46 };
47 };
48
49 struct calling_interface_structure {
50 struct dmi_header header;
51 u16 cmdIOAddress;
52 u8 cmdIOCode;
53 u32 supportedCmds;
54 struct calling_interface_token tokens[];
55 } __packed;
56
57 static int da_command_address;
58 static int da_command_code;
59 static int da_num_tokens;
60 static struct calling_interface_token *da_tokens;
61
62 static struct platform_driver platform_driver = {
63 .driver = {
64 .name = "dell-laptop",
65 .owner = THIS_MODULE,
66 }
67 };
68
69 static struct platform_device *platform_device;
70 static struct backlight_device *dell_backlight_device;
71 static struct rfkill *wifi_rfkill;
72 static struct rfkill *bluetooth_rfkill;
73 static struct rfkill *wwan_rfkill;
74
75 static const struct dmi_system_id __initdata dell_device_table[] = {
76 {
77 .ident = "Dell laptop",
78 .matches = {
79 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
80 DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
81 },
82 },
83 { }
84 };
85
86 static void __init parse_da_table(const struct dmi_header *dm)
87 {
88 /* Final token is a terminator, so we don't want to copy it */
89 int tokens = (dm->length-11)/sizeof(struct calling_interface_token)-1;
90 struct calling_interface_structure *table =
91 container_of(dm, struct calling_interface_structure, header);
92
93 /* 4 bytes of table header, plus 7 bytes of Dell header, plus at least
94 6 bytes of entry */
95
96 if (dm->length < 17)
97 return;
98
99 da_command_address = table->cmdIOAddress;
100 da_command_code = table->cmdIOCode;
101
102 da_tokens = krealloc(da_tokens, (da_num_tokens + tokens) *
103 sizeof(struct calling_interface_token),
104 GFP_KERNEL);
105
106 if (!da_tokens)
107 return;
108
109 memcpy(da_tokens+da_num_tokens, table->tokens,
110 sizeof(struct calling_interface_token) * tokens);
111
112 da_num_tokens += tokens;
113 }
114
115 static void __init find_tokens(const struct dmi_header *dm, void *dummy)
116 {
117 switch (dm->type) {
118 case 0xd4: /* Indexed IO */
119 break;
120 case 0xd5: /* Protected Area Type 1 */
121 break;
122 case 0xd6: /* Protected Area Type 2 */
123 break;
124 case 0xda: /* Calling interface */
125 parse_da_table(dm);
126 break;
127 }
128 }
129
130 static int find_token_location(int tokenid)
131 {
132 int i;
133 for (i = 0; i < da_num_tokens; i++) {
134 if (da_tokens[i].tokenID == tokenid)
135 return da_tokens[i].location;
136 }
137
138 return -1;
139 }
140
141 static struct calling_interface_buffer *
142 dell_send_request(struct calling_interface_buffer *buffer, int class,
143 int select)
144 {
145 struct smi_cmd command;
146
147 command.magic = SMI_CMD_MAGIC;
148 command.command_address = da_command_address;
149 command.command_code = da_command_code;
150 command.ebx = virt_to_phys(buffer);
151 command.ecx = 0x42534931;
152
153 buffer->class = class;
154 buffer->select = select;
155
156 dcdbas_smi_request(&command);
157
158 return buffer;
159 }
160
161 /* Derived from information in DellWirelessCtl.cpp:
162 Class 17, select 11 is radio control. It returns an array of 32-bit values.
163
164 result[0]: return code
165 result[1]:
166 Bit 0: Hardware switch supported
167 Bit 1: Wifi locator supported
168 Bit 2: Wifi is supported
169 Bit 3: Bluetooth is supported
170 Bit 4: WWAN is supported
171 Bit 5: Wireless keyboard supported
172 Bits 6-7: Reserved
173 Bit 8: Wifi is installed
174 Bit 9: Bluetooth is installed
175 Bit 10: WWAN is installed
176 Bits 11-15: Reserved
177 Bit 16: Hardware switch is on
178 Bit 17: Wifi is blocked
179 Bit 18: Bluetooth is blocked
180 Bit 19: WWAN is blocked
181 Bits 20-31: Reserved
182 result[2]: NVRAM size in bytes
183 result[3]: NVRAM format version number
184 */
185
186 static int dell_rfkill_set(void *data, bool blocked)
187 {
188 struct calling_interface_buffer buffer;
189 int disable = blocked ? 1 : 0;
190 unsigned long radio = (unsigned long)data;
191
192 memset(&buffer, 0, sizeof(struct calling_interface_buffer));
193 buffer.input[0] = (1 | (radio<<8) | (disable << 16));
194 dell_send_request(&buffer, 17, 11);
195
196 return 0;
197 }
198
199 static void dell_rfkill_query(struct rfkill *rfkill, void *data)
200 {
201 struct calling_interface_buffer buffer;
202 int status;
203 int bit = (unsigned long)data + 16;
204
205 memset(&buffer, 0, sizeof(struct calling_interface_buffer));
206 dell_send_request(&buffer, 17, 11);
207 status = buffer.output[1];
208
209 rfkill_set_sw_state(rfkill, !!(status & BIT(bit)));
210 rfkill_set_hw_state(rfkill, !(status & BIT(16)));
211 }
212
213 static const struct rfkill_ops dell_rfkill_ops = {
214 .set_block = dell_rfkill_set,
215 .query = dell_rfkill_query,
216 };
217
218 static void dell_update_rfkill(struct work_struct *ignored)
219 {
220 if (wifi_rfkill)
221 dell_rfkill_query(wifi_rfkill, (void *)1);
222 if (bluetooth_rfkill)
223 dell_rfkill_query(bluetooth_rfkill, (void *)2);
224 if (wwan_rfkill)
225 dell_rfkill_query(wwan_rfkill, (void *)3);
226 }
227 static DECLARE_DELAYED_WORK(dell_rfkill_work, dell_update_rfkill);
228
229
230 static int __init dell_setup_rfkill(void)
231 {
232 struct calling_interface_buffer buffer;
233 int status;
234 int ret;
235
236 memset(&buffer, 0, sizeof(struct calling_interface_buffer));
237 dell_send_request(&buffer, 17, 11);
238 status = buffer.output[1];
239
240 if ((status & (1<<2|1<<8)) == (1<<2|1<<8)) {
241 wifi_rfkill = rfkill_alloc("dell-wifi", &platform_device->dev,
242 RFKILL_TYPE_WLAN,
243 &dell_rfkill_ops, (void *) 1);
244 if (!wifi_rfkill) {
245 ret = -ENOMEM;
246 goto err_wifi;
247 }
248 ret = rfkill_register(wifi_rfkill);
249 if (ret)
250 goto err_wifi;
251 }
252
253 if ((status & (1<<3|1<<9)) == (1<<3|1<<9)) {
254 bluetooth_rfkill = rfkill_alloc("dell-bluetooth",
255 &platform_device->dev,
256 RFKILL_TYPE_BLUETOOTH,
257 &dell_rfkill_ops, (void *) 2);
258 if (!bluetooth_rfkill) {
259 ret = -ENOMEM;
260 goto err_bluetooth;
261 }
262 ret = rfkill_register(bluetooth_rfkill);
263 if (ret)
264 goto err_bluetooth;
265 }
266
267 if ((status & (1<<4|1<<10)) == (1<<4|1<<10)) {
268 wwan_rfkill = rfkill_alloc("dell-wwan",
269 &platform_device->dev,
270 RFKILL_TYPE_WWAN,
271 &dell_rfkill_ops, (void *) 3);
272 if (!wwan_rfkill) {
273 ret = -ENOMEM;
274 goto err_wwan;
275 }
276 ret = rfkill_register(wwan_rfkill);
277 if (ret)
278 goto err_wwan;
279 }
280
281 return 0;
282 err_wwan:
283 rfkill_destroy(wwan_rfkill);
284 if (bluetooth_rfkill)
285 rfkill_unregister(bluetooth_rfkill);
286 err_bluetooth:
287 rfkill_destroy(bluetooth_rfkill);
288 if (wifi_rfkill)
289 rfkill_unregister(wifi_rfkill);
290 err_wifi:
291 rfkill_destroy(wifi_rfkill);
292
293 return ret;
294 }
295
296 static void dell_cleanup_rfkill(void)
297 {
298 if (wifi_rfkill) {
299 rfkill_unregister(wifi_rfkill);
300 rfkill_destroy(wifi_rfkill);
301 }
302 if (bluetooth_rfkill) {
303 rfkill_unregister(bluetooth_rfkill);
304 rfkill_destroy(bluetooth_rfkill);
305 }
306 if (wwan_rfkill) {
307 rfkill_unregister(wwan_rfkill);
308 rfkill_destroy(wwan_rfkill);
309 }
310 }
311
312 static int dell_send_intensity(struct backlight_device *bd)
313 {
314 struct calling_interface_buffer buffer;
315
316 memset(&buffer, 0, sizeof(struct calling_interface_buffer));
317 buffer.input[0] = find_token_location(BRIGHTNESS_TOKEN);
318 buffer.input[1] = bd->props.brightness;
319
320 if (buffer.input[0] == -1)
321 return -ENODEV;
322
323 if (power_supply_is_system_supplied() > 0)
324 dell_send_request(&buffer, 1, 2);
325 else
326 dell_send_request(&buffer, 1, 1);
327
328 return 0;
329 }
330
331 static int dell_get_intensity(struct backlight_device *bd)
332 {
333 struct calling_interface_buffer buffer;
334
335 memset(&buffer, 0, sizeof(struct calling_interface_buffer));
336 buffer.input[0] = find_token_location(BRIGHTNESS_TOKEN);
337
338 if (buffer.input[0] == -1)
339 return -ENODEV;
340
341 if (power_supply_is_system_supplied() > 0)
342 dell_send_request(&buffer, 0, 2);
343 else
344 dell_send_request(&buffer, 0, 1);
345
346 return buffer.output[1];
347 }
348
349 static struct backlight_ops dell_ops = {
350 .get_brightness = dell_get_intensity,
351 .update_status = dell_send_intensity,
352 };
353
354 bool dell_laptop_i8042_filter(unsigned char data, unsigned char str,
355 struct serio *port)
356 {
357 static bool extended;
358
359 if (str & 0x20)
360 return false;
361
362 if (unlikely(data == 0xe0)) {
363 extended = true;
364 return false;
365 } else if (unlikely(extended)) {
366 switch (data) {
367 case 0x8:
368 schedule_delayed_work(&dell_rfkill_work,
369 round_jiffies_relative(HZ));
370 break;
371 }
372 extended = false;
373 }
374
375 return false;
376 }
377
378 static int __init dell_init(void)
379 {
380 struct calling_interface_buffer buffer;
381 int max_intensity = 0;
382 int ret;
383
384 if (!dmi_check_system(dell_device_table))
385 return -ENODEV;
386
387 dmi_walk(find_tokens, NULL);
388
389 if (!da_tokens) {
390 printk(KERN_INFO "dell-laptop: Unable to find dmi tokens\n");
391 return -ENODEV;
392 }
393
394 ret = platform_driver_register(&platform_driver);
395 if (ret)
396 goto fail_platform_driver;
397 platform_device = platform_device_alloc("dell-laptop", -1);
398 if (!platform_device) {
399 ret = -ENOMEM;
400 goto fail_platform_device1;
401 }
402 ret = platform_device_add(platform_device);
403 if (ret)
404 goto fail_platform_device2;
405
406 ret = dell_setup_rfkill();
407
408 if (ret) {
409 printk(KERN_WARNING "dell-laptop: Unable to setup rfkill\n");
410 goto fail_rfkill;
411 }
412
413 ret = i8042_install_filter(dell_laptop_i8042_filter);
414 if (ret) {
415 printk(KERN_WARNING
416 "dell-laptop: Unable to install key filter\n");
417 goto fail_filter;
418 }
419
420 #ifdef CONFIG_ACPI
421 /* In the event of an ACPI backlight being available, don't
422 * register the platform controller.
423 */
424 if (acpi_video_backlight_support())
425 return 0;
426 #endif
427
428 memset(&buffer, 0, sizeof(struct calling_interface_buffer));
429 buffer.input[0] = find_token_location(BRIGHTNESS_TOKEN);
430
431 if (buffer.input[0] != -1) {
432 dell_send_request(&buffer, 0, 2);
433 max_intensity = buffer.output[3];
434 }
435
436 if (max_intensity) {
437 dell_backlight_device = backlight_device_register(
438 "dell_backlight",
439 &platform_device->dev, NULL,
440 &dell_ops);
441
442 if (IS_ERR(dell_backlight_device)) {
443 ret = PTR_ERR(dell_backlight_device);
444 dell_backlight_device = NULL;
445 goto fail_backlight;
446 }
447
448 dell_backlight_device->props.max_brightness = max_intensity;
449 dell_backlight_device->props.brightness =
450 dell_get_intensity(dell_backlight_device);
451 backlight_update_status(dell_backlight_device);
452 }
453
454 return 0;
455
456 fail_backlight:
457 i8042_remove_filter(dell_laptop_i8042_filter);
458 fail_filter:
459 dell_cleanup_rfkill();
460 fail_rfkill:
461 platform_device_del(platform_device);
462 fail_platform_device2:
463 platform_device_put(platform_device);
464 fail_platform_device1:
465 platform_driver_unregister(&platform_driver);
466 fail_platform_driver:
467 kfree(da_tokens);
468 return ret;
469 }
470
471 static void __exit dell_exit(void)
472 {
473 cancel_delayed_work_sync(&dell_rfkill_work);
474 i8042_remove_filter(dell_laptop_i8042_filter);
475 backlight_device_unregister(dell_backlight_device);
476 dell_cleanup_rfkill();
477 if (platform_device) {
478 platform_device_del(platform_device);
479 platform_driver_unregister(&platform_driver);
480 }
481 }
482
483 module_init(dell_init);
484 module_exit(dell_exit);
485
486 MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
487 MODULE_DESCRIPTION("Dell laptop driver");
488 MODULE_LICENSE("GPL");
489 MODULE_ALIAS("dmi:*svnDellInc.:*:ct8:*");