Linux Audio

Check our new training course

Loading...
v3.1
  1/*
  2 *  ideapad-laptop.c - Lenovo IdeaPad ACPI Extras
  3 *
  4 *  Copyright © 2010 Intel Corporation
  5 *  Copyright © 2010 David Woodhouse <dwmw2@infradead.org>
  6 *
  7 *  This program is free software; you can redistribute it and/or modify
  8 *  it under the terms of the GNU General Public License as published by
  9 *  the Free Software Foundation; either version 2 of the License, or
 10 *  (at your option) any later version.
 11 *
 12 *  This program is distributed in the hope that it will be useful,
 13 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 14 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15 *  GNU General Public License for more details.
 16 *
 17 *  You should have received a copy of the GNU General Public License
 18 *  along with this program; if not, write to the Free Software
 19 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 20 *  02110-1301, USA.
 21 */
 22
 23#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 24
 25#include <linux/kernel.h>
 26#include <linux/module.h>
 27#include <linux/init.h>
 28#include <linux/types.h>
 29#include <acpi/acpi_bus.h>
 30#include <acpi/acpi_drivers.h>
 31#include <linux/rfkill.h>
 32#include <linux/platform_device.h>
 33#include <linux/input.h>
 34#include <linux/input/sparse-keymap.h>
 35#include <linux/backlight.h>
 36#include <linux/fb.h>
 
 
 
 37
 38#define IDEAPAD_RFKILL_DEV_NUM	(3)
 39
 40#define CFG_BT_BIT	(16)
 41#define CFG_3G_BIT	(17)
 42#define CFG_WIFI_BIT	(18)
 43#define CFG_CAMERA_BIT	(19)
 44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 45struct ideapad_private {
 
 46	struct rfkill *rfk[IDEAPAD_RFKILL_DEV_NUM];
 
 47	struct platform_device *platform_device;
 48	struct input_dev *inputdev;
 49	struct backlight_device *blightdev;
 
 50	unsigned long cfg;
 51};
 52
 53static acpi_handle ideapad_handle;
 54static bool no_bt_rfkill;
 55module_param(no_bt_rfkill, bool, 0444);
 56MODULE_PARM_DESC(no_bt_rfkill, "No rfkill for bluetooth.");
 57
 58/*
 59 * ACPI Helpers
 60 */
 61#define IDEAPAD_EC_TIMEOUT (100) /* in ms */
 62
 63static int read_method_int(acpi_handle handle, const char *method, int *val)
 64{
 65	acpi_status status;
 66	unsigned long long result;
 67
 68	status = acpi_evaluate_integer(handle, (char *)method, NULL, &result);
 69	if (ACPI_FAILURE(status)) {
 70		*val = -1;
 71		return -1;
 72	} else {
 73		*val = result;
 74		return 0;
 75	}
 76}
 77
 78static int method_vpcr(acpi_handle handle, int cmd, int *ret)
 79{
 80	acpi_status status;
 81	unsigned long long result;
 82	struct acpi_object_list params;
 83	union acpi_object in_obj;
 84
 85	params.count = 1;
 86	params.pointer = &in_obj;
 87	in_obj.type = ACPI_TYPE_INTEGER;
 88	in_obj.integer.value = cmd;
 89
 90	status = acpi_evaluate_integer(handle, "VPCR", &params, &result);
 91
 92	if (ACPI_FAILURE(status)) {
 93		*ret = -1;
 94		return -1;
 95	} else {
 96		*ret = result;
 97		return 0;
 98	}
 99}
100
101static int method_vpcw(acpi_handle handle, int cmd, int data)
102{
103	struct acpi_object_list params;
104	union acpi_object in_obj[2];
105	acpi_status status;
106
107	params.count = 2;
108	params.pointer = in_obj;
109	in_obj[0].type = ACPI_TYPE_INTEGER;
110	in_obj[0].integer.value = cmd;
111	in_obj[1].type = ACPI_TYPE_INTEGER;
112	in_obj[1].integer.value = data;
113
114	status = acpi_evaluate_object(handle, "VPCW", &params, NULL);
115	if (status != AE_OK)
116		return -1;
117	return 0;
118}
119
120static int read_ec_data(acpi_handle handle, int cmd, unsigned long *data)
121{
122	int val;
123	unsigned long int end_jiffies;
124
125	if (method_vpcw(handle, 1, cmd))
126		return -1;
127
128	for (end_jiffies = jiffies+(HZ)*IDEAPAD_EC_TIMEOUT/1000+1;
129	     time_before(jiffies, end_jiffies);) {
130		schedule();
131		if (method_vpcr(handle, 1, &val))
132			return -1;
133		if (val == 0) {
134			if (method_vpcr(handle, 0, &val))
135				return -1;
136			*data = val;
137			return 0;
138		}
139	}
140	pr_err("timeout in read_ec_cmd\n");
141	return -1;
142}
143
144static int write_ec_cmd(acpi_handle handle, int cmd, unsigned long data)
145{
146	int val;
147	unsigned long int end_jiffies;
148
149	if (method_vpcw(handle, 0, data))
150		return -1;
151	if (method_vpcw(handle, 1, cmd))
152		return -1;
153
154	for (end_jiffies = jiffies+(HZ)*IDEAPAD_EC_TIMEOUT/1000+1;
155	     time_before(jiffies, end_jiffies);) {
156		schedule();
157		if (method_vpcr(handle, 1, &val))
158			return -1;
159		if (val == 0)
160			return 0;
161	}
162	pr_err("timeout in write_ec_cmd\n");
163	return -1;
164}
165
166/*
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
167 * sysfs
168 */
169static ssize_t show_ideapad_cam(struct device *dev,
170				struct device_attribute *attr,
171				char *buf)
172{
173	unsigned long result;
 
174
175	if (read_ec_data(ideapad_handle, 0x1D, &result))
176		return sprintf(buf, "-1\n");
177	return sprintf(buf, "%lu\n", result);
178}
179
180static ssize_t store_ideapad_cam(struct device *dev,
181				 struct device_attribute *attr,
182				 const char *buf, size_t count)
183{
184	int ret, state;
 
185
186	if (!count)
187		return 0;
188	if (sscanf(buf, "%i", &state) != 1)
189		return -EINVAL;
190	ret = write_ec_cmd(ideapad_handle, 0x1E, state);
191	if (ret < 0)
192		return ret;
193	return count;
194}
195
196static DEVICE_ATTR(camera_power, 0644, show_ideapad_cam, store_ideapad_cam);
197
198static ssize_t show_ideapad_cfg(struct device *dev,
199				struct device_attribute *attr,
200				char *buf)
201{
 
 
 
 
 
 
 
 
 
 
 
 
 
202	struct ideapad_private *priv = dev_get_drvdata(dev);
203
204	return sprintf(buf, "0x%.8lX\n", priv->cfg);
 
 
 
 
 
 
 
 
 
205}
206
207static DEVICE_ATTR(cfg, 0444, show_ideapad_cfg, NULL);
208
209static struct attribute *ideapad_attributes[] = {
210	&dev_attr_camera_power.attr,
211	&dev_attr_cfg.attr,
212	NULL
213};
214
215static mode_t ideapad_is_visible(struct kobject *kobj,
216				 struct attribute *attr,
217				 int idx)
218{
219	struct device *dev = container_of(kobj, struct device, kobj);
220	struct ideapad_private *priv = dev_get_drvdata(dev);
221	bool supported;
222
223	if (attr == &dev_attr_camera_power.attr)
224		supported = test_bit(CFG_CAMERA_BIT, &(priv->cfg));
225	else
 
 
 
 
226		supported = true;
227
228	return supported ? attr->mode : 0;
229}
230
231static struct attribute_group ideapad_attribute_group = {
232	.is_visible = ideapad_is_visible,
233	.attrs = ideapad_attributes
234};
235
236/*
237 * Rfkill
238 */
239struct ideapad_rfk_data {
240	char *name;
241	int cfgbit;
242	int opcode;
243	int type;
244};
245
246const struct ideapad_rfk_data ideapad_rfk_data[] = {
247	{ "ideapad_wlan",      CFG_WIFI_BIT, 0x15, RFKILL_TYPE_WLAN },
248	{ "ideapad_bluetooth", CFG_BT_BIT,   0x17, RFKILL_TYPE_BLUETOOTH },
249	{ "ideapad_3g",        CFG_3G_BIT,   0x20, RFKILL_TYPE_WWAN },
250};
251
252static int ideapad_rfk_set(void *data, bool blocked)
253{
254	unsigned long opcode = (unsigned long)data;
255
256	return write_ec_cmd(ideapad_handle, opcode, !blocked);
257}
258
259static struct rfkill_ops ideapad_rfk_ops = {
260	.set_block = ideapad_rfk_set,
261};
262
263static void ideapad_sync_rfk_state(struct acpi_device *adevice)
264{
265	struct ideapad_private *priv = dev_get_drvdata(&adevice->dev);
266	unsigned long hw_blocked;
267	int i;
268
269	if (read_ec_data(ideapad_handle, 0x23, &hw_blocked))
270		return;
271	hw_blocked = !hw_blocked;
272
273	for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
274		if (priv->rfk[i])
275			rfkill_set_hw_state(priv->rfk[i], hw_blocked);
276}
277
278static int __devinit ideapad_register_rfkill(struct acpi_device *adevice,
279					     int dev)
280{
281	struct ideapad_private *priv = dev_get_drvdata(&adevice->dev);
282	int ret;
283	unsigned long sw_blocked;
284
285	if (no_bt_rfkill &&
286	    (ideapad_rfk_data[dev].type == RFKILL_TYPE_BLUETOOTH)) {
287		/* Force to enable bluetooth when no_bt_rfkill=1 */
288		write_ec_cmd(ideapad_handle,
289			     ideapad_rfk_data[dev].opcode, 1);
290		return 0;
291	}
 
 
292
293	priv->rfk[dev] = rfkill_alloc(ideapad_rfk_data[dev].name, &adevice->dev,
294				      ideapad_rfk_data[dev].type, &ideapad_rfk_ops,
295				      (void *)(long)dev);
 
 
296	if (!priv->rfk[dev])
297		return -ENOMEM;
298
299	if (read_ec_data(ideapad_handle, ideapad_rfk_data[dev].opcode-1,
300			 &sw_blocked)) {
301		rfkill_init_sw_state(priv->rfk[dev], 0);
302	} else {
303		sw_blocked = !sw_blocked;
304		rfkill_init_sw_state(priv->rfk[dev], sw_blocked);
305	}
306
307	ret = rfkill_register(priv->rfk[dev]);
308	if (ret) {
309		rfkill_destroy(priv->rfk[dev]);
310		return ret;
311	}
312	return 0;
313}
314
315static void ideapad_unregister_rfkill(struct acpi_device *adevice, int dev)
316{
317	struct ideapad_private *priv = dev_get_drvdata(&adevice->dev);
318
319	if (!priv->rfk[dev])
320		return;
321
322	rfkill_unregister(priv->rfk[dev]);
323	rfkill_destroy(priv->rfk[dev]);
324}
325
326/*
327 * Platform device
328 */
329static int __devinit ideapad_platform_init(struct ideapad_private *priv)
330{
331	int result;
332
333	priv->platform_device = platform_device_alloc("ideapad", -1);
334	if (!priv->platform_device)
335		return -ENOMEM;
336	platform_set_drvdata(priv->platform_device, priv);
337
338	result = platform_device_add(priv->platform_device);
339	if (result)
340		goto fail_platform_device;
341
342	result = sysfs_create_group(&priv->platform_device->dev.kobj,
343				    &ideapad_attribute_group);
344	if (result)
345		goto fail_sysfs;
346	return 0;
347
348fail_sysfs:
349	platform_device_del(priv->platform_device);
350fail_platform_device:
351	platform_device_put(priv->platform_device);
352	return result;
353}
354
355static void ideapad_platform_exit(struct ideapad_private *priv)
356{
357	sysfs_remove_group(&priv->platform_device->dev.kobj,
358			   &ideapad_attribute_group);
359	platform_device_unregister(priv->platform_device);
360}
361
362/*
363 * input device
364 */
365static const struct key_entry ideapad_keymap[] = {
366	{ KE_KEY, 0x06, { KEY_SWITCHVIDEOMODE } },
367	{ KE_KEY, 0x0D, { KEY_WLAN } },
 
 
 
 
 
 
 
 
368	{ KE_END, 0 },
369};
370
371static int __devinit ideapad_input_init(struct ideapad_private *priv)
372{
373	struct input_dev *inputdev;
374	int error;
375
376	inputdev = input_allocate_device();
377	if (!inputdev) {
378		pr_info("Unable to allocate input device\n");
379		return -ENOMEM;
380	}
381
382	inputdev->name = "Ideapad extra buttons";
383	inputdev->phys = "ideapad/input0";
384	inputdev->id.bustype = BUS_HOST;
385	inputdev->dev.parent = &priv->platform_device->dev;
386
387	error = sparse_keymap_setup(inputdev, ideapad_keymap, NULL);
388	if (error) {
389		pr_err("Unable to setup input device keymap\n");
390		goto err_free_dev;
391	}
392
393	error = input_register_device(inputdev);
394	if (error) {
395		pr_err("Unable to register input device\n");
396		goto err_free_keymap;
397	}
398
399	priv->inputdev = inputdev;
400	return 0;
401
402err_free_keymap:
403	sparse_keymap_free(inputdev);
404err_free_dev:
405	input_free_device(inputdev);
406	return error;
407}
408
409static void ideapad_input_exit(struct ideapad_private *priv)
410{
411	sparse_keymap_free(priv->inputdev);
412	input_unregister_device(priv->inputdev);
413	priv->inputdev = NULL;
414}
415
416static void ideapad_input_report(struct ideapad_private *priv,
417				 unsigned long scancode)
418{
419	sparse_keymap_report_event(priv->inputdev, scancode, 1, true);
420}
421
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
422/*
423 * backlight
424 */
425static int ideapad_backlight_get_brightness(struct backlight_device *blightdev)
426{
 
427	unsigned long now;
428
429	if (read_ec_data(ideapad_handle, 0x12, &now))
 
 
 
430		return -EIO;
431	return now;
432}
433
434static int ideapad_backlight_update_status(struct backlight_device *blightdev)
435{
436	if (write_ec_cmd(ideapad_handle, 0x13, blightdev->props.brightness))
 
 
 
 
 
 
437		return -EIO;
438	if (write_ec_cmd(ideapad_handle, 0x33,
439			 blightdev->props.power == FB_BLANK_POWERDOWN ? 0 : 1))
440		return -EIO;
441
442	return 0;
443}
444
445static const struct backlight_ops ideapad_backlight_ops = {
446	.get_brightness = ideapad_backlight_get_brightness,
447	.update_status = ideapad_backlight_update_status,
448};
449
450static int ideapad_backlight_init(struct ideapad_private *priv)
451{
452	struct backlight_device *blightdev;
453	struct backlight_properties props;
454	unsigned long max, now, power;
455
456	if (read_ec_data(ideapad_handle, 0x11, &max))
457		return -EIO;
458	if (read_ec_data(ideapad_handle, 0x12, &now))
459		return -EIO;
460	if (read_ec_data(ideapad_handle, 0x18, &power))
461		return -EIO;
462
463	memset(&props, 0, sizeof(struct backlight_properties));
464	props.max_brightness = max;
465	props.type = BACKLIGHT_PLATFORM;
466	blightdev = backlight_device_register("ideapad",
467					      &priv->platform_device->dev,
468					      priv,
469					      &ideapad_backlight_ops,
470					      &props);
471	if (IS_ERR(blightdev)) {
472		pr_err("Could not register backlight device\n");
473		return PTR_ERR(blightdev);
474	}
475
476	priv->blightdev = blightdev;
477	blightdev->props.brightness = now;
478	blightdev->props.power = power ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
479	backlight_update_status(blightdev);
480
481	return 0;
482}
483
484static void ideapad_backlight_exit(struct ideapad_private *priv)
485{
486	if (priv->blightdev)
487		backlight_device_unregister(priv->blightdev);
488	priv->blightdev = NULL;
489}
490
491static void ideapad_backlight_notify_power(struct ideapad_private *priv)
492{
493	unsigned long power;
494	struct backlight_device *blightdev = priv->blightdev;
495
496	if (read_ec_data(ideapad_handle, 0x18, &power))
 
 
497		return;
498	blightdev->props.power = power ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
499}
500
501static void ideapad_backlight_notify_brightness(struct ideapad_private *priv)
502{
503	unsigned long now;
504
505	/* if we control brightness via acpi video driver */
506	if (priv->blightdev == NULL) {
507		read_ec_data(ideapad_handle, 0x12, &now);
508		return;
509	}
510
511	backlight_force_update(priv->blightdev, BACKLIGHT_UPDATE_HOTKEY);
512}
513
514/*
515 * module init/exit
516 */
517static const struct acpi_device_id ideapad_device_ids[] = {
518	{ "VPC2004", 0},
519	{ "", 0},
520};
521MODULE_DEVICE_TABLE(acpi, ideapad_device_ids);
 
 
 
 
 
 
 
 
 
 
 
522
523static int __devinit ideapad_acpi_add(struct acpi_device *adevice)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
524{
525	int ret, i;
526	unsigned long cfg;
527	struct ideapad_private *priv;
 
 
 
 
 
528
529	if (read_method_int(adevice->handle, "_CFG", (int *)&cfg))
530		return -ENODEV;
531
532	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
533	if (!priv)
534		return -ENOMEM;
535	dev_set_drvdata(&adevice->dev, priv);
536	ideapad_handle = adevice->handle;
537	priv->cfg = cfg;
 
 
 
 
 
 
538
539	ret = ideapad_platform_init(priv);
540	if (ret)
541		goto platform_failed;
542
543	ret = ideapad_input_init(priv);
544	if (ret)
545		goto input_failed;
546
547	for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++) {
548		if (test_bit(ideapad_rfk_data[i].cfgbit, &cfg))
549			ideapad_register_rfkill(adevice, i);
550		else
551			priv->rfk[i] = NULL;
552	}
553	ideapad_sync_rfk_state(adevice);
 
554
555	if (!acpi_video_backlight_support()) {
556		ret = ideapad_backlight_init(priv);
557		if (ret && ret != -ENODEV)
558			goto backlight_failed;
559	}
 
 
 
 
560
561	return 0;
562
 
563backlight_failed:
564	for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
565		ideapad_unregister_rfkill(adevice, i);
566	ideapad_input_exit(priv);
567input_failed:
568	ideapad_platform_exit(priv);
569platform_failed:
 
 
570	kfree(priv);
571	return ret;
572}
573
574static int __devexit ideapad_acpi_remove(struct acpi_device *adevice, int type)
575{
576	struct ideapad_private *priv = dev_get_drvdata(&adevice->dev);
577	int i;
578
 
 
579	ideapad_backlight_exit(priv);
580	for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
581		ideapad_unregister_rfkill(adevice, i);
582	ideapad_input_exit(priv);
583	ideapad_platform_exit(priv);
584	dev_set_drvdata(&adevice->dev, NULL);
 
585	kfree(priv);
586
587	return 0;
588}
589
590static void ideapad_acpi_notify(struct acpi_device *adevice, u32 event)
 
591{
592	struct ideapad_private *priv = dev_get_drvdata(&adevice->dev);
593	acpi_handle handle = adevice->handle;
594	unsigned long vpc1, vpc2, vpc_bit;
595
596	if (read_ec_data(handle, 0x10, &vpc1))
597		return;
598	if (read_ec_data(handle, 0x1A, &vpc2))
599		return;
600
601	vpc1 = (vpc2 << 8) | vpc1;
602	for (vpc_bit = 0; vpc_bit < 16; vpc_bit++) {
603		if (test_bit(vpc_bit, &vpc1)) {
604			switch (vpc_bit) {
605			case 9:
606				ideapad_sync_rfk_state(adevice);
607				break;
608			case 4:
609				ideapad_backlight_notify_brightness(priv);
610				break;
611			case 2:
612				ideapad_backlight_notify_power(priv);
613				break;
614			default:
615				ideapad_input_report(priv, vpc_bit);
616			}
617		}
618	}
619}
 
 
620
621static struct acpi_driver ideapad_acpi_driver = {
622	.name = "ideapad_acpi",
623	.class = "IdeaPad",
624	.ids = ideapad_device_ids,
625	.ops.add = ideapad_acpi_add,
626	.ops.remove = ideapad_acpi_remove,
627	.ops.notify = ideapad_acpi_notify,
628	.owner = THIS_MODULE,
629};
 
630
631static int __init ideapad_acpi_module_init(void)
632{
633	return acpi_bus_register_driver(&ideapad_acpi_driver);
634}
 
 
 
 
 
 
635
636static void __exit ideapad_acpi_module_exit(void)
637{
638	acpi_bus_unregister_driver(&ideapad_acpi_driver);
639}
640
641MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
642MODULE_DESCRIPTION("IdeaPad ACPI Extras");
643MODULE_LICENSE("GPL");
644
645module_init(ideapad_acpi_module_init);
646module_exit(ideapad_acpi_module_exit);
v3.15
  1/*
  2 *  ideapad-laptop.c - Lenovo IdeaPad ACPI Extras
  3 *
  4 *  Copyright © 2010 Intel Corporation
  5 *  Copyright © 2010 David Woodhouse <dwmw2@infradead.org>
  6 *
  7 *  This program is free software; you can redistribute it and/or modify
  8 *  it under the terms of the GNU General Public License as published by
  9 *  the Free Software Foundation; either version 2 of the License, or
 10 *  (at your option) any later version.
 11 *
 12 *  This program is distributed in the hope that it will be useful,
 13 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 14 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15 *  GNU General Public License for more details.
 16 *
 17 *  You should have received a copy of the GNU General Public License
 18 *  along with this program; if not, write to the Free Software
 19 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 20 *  02110-1301, USA.
 21 */
 22
 23#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 24
 25#include <linux/kernel.h>
 26#include <linux/module.h>
 27#include <linux/init.h>
 28#include <linux/types.h>
 29#include <linux/acpi.h>
 
 30#include <linux/rfkill.h>
 31#include <linux/platform_device.h>
 32#include <linux/input.h>
 33#include <linux/input/sparse-keymap.h>
 34#include <linux/backlight.h>
 35#include <linux/fb.h>
 36#include <linux/debugfs.h>
 37#include <linux/seq_file.h>
 38#include <linux/i8042.h>
 39
 40#define IDEAPAD_RFKILL_DEV_NUM	(3)
 41
 42#define CFG_BT_BIT	(16)
 43#define CFG_3G_BIT	(17)
 44#define CFG_WIFI_BIT	(18)
 45#define CFG_CAMERA_BIT	(19)
 46
 47enum {
 48	VPCCMD_R_VPC1 = 0x10,
 49	VPCCMD_R_BL_MAX,
 50	VPCCMD_R_BL,
 51	VPCCMD_W_BL,
 52	VPCCMD_R_WIFI,
 53	VPCCMD_W_WIFI,
 54	VPCCMD_R_BT,
 55	VPCCMD_W_BT,
 56	VPCCMD_R_BL_POWER,
 57	VPCCMD_R_NOVO,
 58	VPCCMD_R_VPC2,
 59	VPCCMD_R_TOUCHPAD,
 60	VPCCMD_W_TOUCHPAD,
 61	VPCCMD_R_CAMERA,
 62	VPCCMD_W_CAMERA,
 63	VPCCMD_R_3G,
 64	VPCCMD_W_3G,
 65	VPCCMD_R_ODD, /* 0x21 */
 66	VPCCMD_W_FAN,
 67	VPCCMD_R_RF,
 68	VPCCMD_W_RF,
 69	VPCCMD_R_FAN = 0x2B,
 70	VPCCMD_R_SPECIAL_BUTTONS = 0x31,
 71	VPCCMD_W_BL_POWER = 0x33,
 72};
 73
 74struct ideapad_rfk_priv {
 75	int dev;
 76	struct ideapad_private *priv;
 77};
 78
 79struct ideapad_private {
 80	struct acpi_device *adev;
 81	struct rfkill *rfk[IDEAPAD_RFKILL_DEV_NUM];
 82	struct ideapad_rfk_priv rfk_priv[IDEAPAD_RFKILL_DEV_NUM];
 83	struct platform_device *platform_device;
 84	struct input_dev *inputdev;
 85	struct backlight_device *blightdev;
 86	struct dentry *debug;
 87	unsigned long cfg;
 88};
 89
 
 90static bool no_bt_rfkill;
 91module_param(no_bt_rfkill, bool, 0444);
 92MODULE_PARM_DESC(no_bt_rfkill, "No rfkill for bluetooth.");
 93
 94/*
 95 * ACPI Helpers
 96 */
 97#define IDEAPAD_EC_TIMEOUT (100) /* in ms */
 98
 99static int read_method_int(acpi_handle handle, const char *method, int *val)
100{
101	acpi_status status;
102	unsigned long long result;
103
104	status = acpi_evaluate_integer(handle, (char *)method, NULL, &result);
105	if (ACPI_FAILURE(status)) {
106		*val = -1;
107		return -1;
108	} else {
109		*val = result;
110		return 0;
111	}
112}
113
114static int method_vpcr(acpi_handle handle, int cmd, int *ret)
115{
116	acpi_status status;
117	unsigned long long result;
118	struct acpi_object_list params;
119	union acpi_object in_obj;
120
121	params.count = 1;
122	params.pointer = &in_obj;
123	in_obj.type = ACPI_TYPE_INTEGER;
124	in_obj.integer.value = cmd;
125
126	status = acpi_evaluate_integer(handle, "VPCR", &params, &result);
127
128	if (ACPI_FAILURE(status)) {
129		*ret = -1;
130		return -1;
131	} else {
132		*ret = result;
133		return 0;
134	}
135}
136
137static int method_vpcw(acpi_handle handle, int cmd, int data)
138{
139	struct acpi_object_list params;
140	union acpi_object in_obj[2];
141	acpi_status status;
142
143	params.count = 2;
144	params.pointer = in_obj;
145	in_obj[0].type = ACPI_TYPE_INTEGER;
146	in_obj[0].integer.value = cmd;
147	in_obj[1].type = ACPI_TYPE_INTEGER;
148	in_obj[1].integer.value = data;
149
150	status = acpi_evaluate_object(handle, "VPCW", &params, NULL);
151	if (status != AE_OK)
152		return -1;
153	return 0;
154}
155
156static int read_ec_data(acpi_handle handle, int cmd, unsigned long *data)
157{
158	int val;
159	unsigned long int end_jiffies;
160
161	if (method_vpcw(handle, 1, cmd))
162		return -1;
163
164	for (end_jiffies = jiffies+(HZ)*IDEAPAD_EC_TIMEOUT/1000+1;
165	     time_before(jiffies, end_jiffies);) {
166		schedule();
167		if (method_vpcr(handle, 1, &val))
168			return -1;
169		if (val == 0) {
170			if (method_vpcr(handle, 0, &val))
171				return -1;
172			*data = val;
173			return 0;
174		}
175	}
176	pr_err("timeout in read_ec_cmd\n");
177	return -1;
178}
179
180static int write_ec_cmd(acpi_handle handle, int cmd, unsigned long data)
181{
182	int val;
183	unsigned long int end_jiffies;
184
185	if (method_vpcw(handle, 0, data))
186		return -1;
187	if (method_vpcw(handle, 1, cmd))
188		return -1;
189
190	for (end_jiffies = jiffies+(HZ)*IDEAPAD_EC_TIMEOUT/1000+1;
191	     time_before(jiffies, end_jiffies);) {
192		schedule();
193		if (method_vpcr(handle, 1, &val))
194			return -1;
195		if (val == 0)
196			return 0;
197	}
198	pr_err("timeout in write_ec_cmd\n");
199	return -1;
200}
201
202/*
203 * debugfs
204 */
205static int debugfs_status_show(struct seq_file *s, void *data)
206{
207	struct ideapad_private *priv = s->private;
208	unsigned long value;
209
210	if (!priv)
211		return -EINVAL;
212
213	if (!read_ec_data(priv->adev->handle, VPCCMD_R_BL_MAX, &value))
214		seq_printf(s, "Backlight max:\t%lu\n", value);
215	if (!read_ec_data(priv->adev->handle, VPCCMD_R_BL, &value))
216		seq_printf(s, "Backlight now:\t%lu\n", value);
217	if (!read_ec_data(priv->adev->handle, VPCCMD_R_BL_POWER, &value))
218		seq_printf(s, "BL power value:\t%s\n", value ? "On" : "Off");
219	seq_printf(s, "=====================\n");
220
221	if (!read_ec_data(priv->adev->handle, VPCCMD_R_RF, &value))
222		seq_printf(s, "Radio status:\t%s(%lu)\n",
223			   value ? "On" : "Off", value);
224	if (!read_ec_data(priv->adev->handle, VPCCMD_R_WIFI, &value))
225		seq_printf(s, "Wifi status:\t%s(%lu)\n",
226			   value ? "On" : "Off", value);
227	if (!read_ec_data(priv->adev->handle, VPCCMD_R_BT, &value))
228		seq_printf(s, "BT status:\t%s(%lu)\n",
229			   value ? "On" : "Off", value);
230	if (!read_ec_data(priv->adev->handle, VPCCMD_R_3G, &value))
231		seq_printf(s, "3G status:\t%s(%lu)\n",
232			   value ? "On" : "Off", value);
233	seq_printf(s, "=====================\n");
234
235	if (!read_ec_data(priv->adev->handle, VPCCMD_R_TOUCHPAD, &value))
236		seq_printf(s, "Touchpad status:%s(%lu)\n",
237			   value ? "On" : "Off", value);
238	if (!read_ec_data(priv->adev->handle, VPCCMD_R_CAMERA, &value))
239		seq_printf(s, "Camera status:\t%s(%lu)\n",
240			   value ? "On" : "Off", value);
241
242	return 0;
243}
244
245static int debugfs_status_open(struct inode *inode, struct file *file)
246{
247	return single_open(file, debugfs_status_show, inode->i_private);
248}
249
250static const struct file_operations debugfs_status_fops = {
251	.owner = THIS_MODULE,
252	.open = debugfs_status_open,
253	.read = seq_read,
254	.llseek = seq_lseek,
255	.release = single_release,
256};
257
258static int debugfs_cfg_show(struct seq_file *s, void *data)
259{
260	struct ideapad_private *priv = s->private;
261
262	if (!priv) {
263		seq_printf(s, "cfg: N/A\n");
264	} else {
265		seq_printf(s, "cfg: 0x%.8lX\n\nCapability: ",
266			   priv->cfg);
267		if (test_bit(CFG_BT_BIT, &priv->cfg))
268			seq_printf(s, "Bluetooth ");
269		if (test_bit(CFG_3G_BIT, &priv->cfg))
270			seq_printf(s, "3G ");
271		if (test_bit(CFG_WIFI_BIT, &priv->cfg))
272			seq_printf(s, "Wireless ");
273		if (test_bit(CFG_CAMERA_BIT, &priv->cfg))
274			seq_printf(s, "Camera ");
275		seq_printf(s, "\nGraphic: ");
276		switch ((priv->cfg)&0x700) {
277		case 0x100:
278			seq_printf(s, "Intel");
279			break;
280		case 0x200:
281			seq_printf(s, "ATI");
282			break;
283		case 0x300:
284			seq_printf(s, "Nvidia");
285			break;
286		case 0x400:
287			seq_printf(s, "Intel and ATI");
288			break;
289		case 0x500:
290			seq_printf(s, "Intel and Nvidia");
291			break;
292		}
293		seq_printf(s, "\n");
294	}
295	return 0;
296}
297
298static int debugfs_cfg_open(struct inode *inode, struct file *file)
299{
300	return single_open(file, debugfs_cfg_show, inode->i_private);
301}
302
303static const struct file_operations debugfs_cfg_fops = {
304	.owner = THIS_MODULE,
305	.open = debugfs_cfg_open,
306	.read = seq_read,
307	.llseek = seq_lseek,
308	.release = single_release,
309};
310
311static int ideapad_debugfs_init(struct ideapad_private *priv)
312{
313	struct dentry *node;
314
315	priv->debug = debugfs_create_dir("ideapad", NULL);
316	if (priv->debug == NULL) {
317		pr_err("failed to create debugfs directory");
318		goto errout;
319	}
320
321	node = debugfs_create_file("cfg", S_IRUGO, priv->debug, priv,
322				   &debugfs_cfg_fops);
323	if (!node) {
324		pr_err("failed to create cfg in debugfs");
325		goto errout;
326	}
327
328	node = debugfs_create_file("status", S_IRUGO, priv->debug, priv,
329				   &debugfs_status_fops);
330	if (!node) {
331		pr_err("failed to create status in debugfs");
332		goto errout;
333	}
334
335	return 0;
336
337errout:
338	return -ENOMEM;
339}
340
341static void ideapad_debugfs_exit(struct ideapad_private *priv)
342{
343	debugfs_remove_recursive(priv->debug);
344	priv->debug = NULL;
345}
346
347/*
348 * sysfs
349 */
350static ssize_t show_ideapad_cam(struct device *dev,
351				struct device_attribute *attr,
352				char *buf)
353{
354	unsigned long result;
355	struct ideapad_private *priv = dev_get_drvdata(dev);
356
357	if (read_ec_data(priv->adev->handle, VPCCMD_R_CAMERA, &result))
358		return sprintf(buf, "-1\n");
359	return sprintf(buf, "%lu\n", result);
360}
361
362static ssize_t store_ideapad_cam(struct device *dev,
363				 struct device_attribute *attr,
364				 const char *buf, size_t count)
365{
366	int ret, state;
367	struct ideapad_private *priv = dev_get_drvdata(dev);
368
369	if (!count)
370		return 0;
371	if (sscanf(buf, "%i", &state) != 1)
372		return -EINVAL;
373	ret = write_ec_cmd(priv->adev->handle, VPCCMD_W_CAMERA, state);
374	if (ret < 0)
375		return -EIO;
376	return count;
377}
378
379static DEVICE_ATTR(camera_power, 0644, show_ideapad_cam, store_ideapad_cam);
380
381static ssize_t show_ideapad_fan(struct device *dev,
382				struct device_attribute *attr,
383				char *buf)
384{
385	unsigned long result;
386	struct ideapad_private *priv = dev_get_drvdata(dev);
387
388	if (read_ec_data(priv->adev->handle, VPCCMD_R_FAN, &result))
389		return sprintf(buf, "-1\n");
390	return sprintf(buf, "%lu\n", result);
391}
392
393static ssize_t store_ideapad_fan(struct device *dev,
394				 struct device_attribute *attr,
395				 const char *buf, size_t count)
396{
397	int ret, state;
398	struct ideapad_private *priv = dev_get_drvdata(dev);
399
400	if (!count)
401		return 0;
402	if (sscanf(buf, "%i", &state) != 1)
403		return -EINVAL;
404	if (state < 0 || state > 4 || state == 3)
405		return -EINVAL;
406	ret = write_ec_cmd(priv->adev->handle, VPCCMD_W_FAN, state);
407	if (ret < 0)
408		return -EIO;
409	return count;
410}
411
412static DEVICE_ATTR(fan_mode, 0644, show_ideapad_fan, store_ideapad_fan);
413
414static struct attribute *ideapad_attributes[] = {
415	&dev_attr_camera_power.attr,
416	&dev_attr_fan_mode.attr,
417	NULL
418};
419
420static umode_t ideapad_is_visible(struct kobject *kobj,
421				 struct attribute *attr,
422				 int idx)
423{
424	struct device *dev = container_of(kobj, struct device, kobj);
425	struct ideapad_private *priv = dev_get_drvdata(dev);
426	bool supported;
427
428	if (attr == &dev_attr_camera_power.attr)
429		supported = test_bit(CFG_CAMERA_BIT, &(priv->cfg));
430	else if (attr == &dev_attr_fan_mode.attr) {
431		unsigned long value;
432		supported = !read_ec_data(priv->adev->handle, VPCCMD_R_FAN,
433					  &value);
434	} else
435		supported = true;
436
437	return supported ? attr->mode : 0;
438}
439
440static struct attribute_group ideapad_attribute_group = {
441	.is_visible = ideapad_is_visible,
442	.attrs = ideapad_attributes
443};
444
445/*
446 * Rfkill
447 */
448struct ideapad_rfk_data {
449	char *name;
450	int cfgbit;
451	int opcode;
452	int type;
453};
454
455const struct ideapad_rfk_data ideapad_rfk_data[] = {
456	{ "ideapad_wlan",    CFG_WIFI_BIT, VPCCMD_W_WIFI, RFKILL_TYPE_WLAN },
457	{ "ideapad_bluetooth", CFG_BT_BIT, VPCCMD_W_BT, RFKILL_TYPE_BLUETOOTH },
458	{ "ideapad_3g",        CFG_3G_BIT, VPCCMD_W_3G, RFKILL_TYPE_WWAN },
459};
460
461static int ideapad_rfk_set(void *data, bool blocked)
462{
463	struct ideapad_rfk_priv *priv = data;
464
465	return write_ec_cmd(priv->priv->adev->handle, priv->dev, !blocked);
466}
467
468static struct rfkill_ops ideapad_rfk_ops = {
469	.set_block = ideapad_rfk_set,
470};
471
472static void ideapad_sync_rfk_state(struct ideapad_private *priv)
473{
 
474	unsigned long hw_blocked;
475	int i;
476
477	if (read_ec_data(priv->adev->handle, VPCCMD_R_RF, &hw_blocked))
478		return;
479	hw_blocked = !hw_blocked;
480
481	for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
482		if (priv->rfk[i])
483			rfkill_set_hw_state(priv->rfk[i], hw_blocked);
484}
485
486static int ideapad_register_rfkill(struct ideapad_private *priv, int dev)
 
487{
 
488	int ret;
489	unsigned long sw_blocked;
490
491	if (no_bt_rfkill &&
492	    (ideapad_rfk_data[dev].type == RFKILL_TYPE_BLUETOOTH)) {
493		/* Force to enable bluetooth when no_bt_rfkill=1 */
494		write_ec_cmd(priv->adev->handle,
495			     ideapad_rfk_data[dev].opcode, 1);
496		return 0;
497	}
498	priv->rfk_priv[dev].dev = dev;
499	priv->rfk_priv[dev].priv = priv;
500
501	priv->rfk[dev] = rfkill_alloc(ideapad_rfk_data[dev].name,
502				      &priv->platform_device->dev,
503				      ideapad_rfk_data[dev].type,
504				      &ideapad_rfk_ops,
505				      &priv->rfk_priv[dev]);
506	if (!priv->rfk[dev])
507		return -ENOMEM;
508
509	if (read_ec_data(priv->adev->handle, ideapad_rfk_data[dev].opcode-1,
510			 &sw_blocked)) {
511		rfkill_init_sw_state(priv->rfk[dev], 0);
512	} else {
513		sw_blocked = !sw_blocked;
514		rfkill_init_sw_state(priv->rfk[dev], sw_blocked);
515	}
516
517	ret = rfkill_register(priv->rfk[dev]);
518	if (ret) {
519		rfkill_destroy(priv->rfk[dev]);
520		return ret;
521	}
522	return 0;
523}
524
525static void ideapad_unregister_rfkill(struct ideapad_private *priv, int dev)
526{
 
 
527	if (!priv->rfk[dev])
528		return;
529
530	rfkill_unregister(priv->rfk[dev]);
531	rfkill_destroy(priv->rfk[dev]);
532}
533
534/*
535 * Platform device
536 */
537static int ideapad_sysfs_init(struct ideapad_private *priv)
538{
539	return sysfs_create_group(&priv->platform_device->dev.kobj,
 
 
 
 
 
 
 
 
 
 
 
540				    &ideapad_attribute_group);
 
 
 
 
 
 
 
 
 
541}
542
543static void ideapad_sysfs_exit(struct ideapad_private *priv)
544{
545	sysfs_remove_group(&priv->platform_device->dev.kobj,
546			   &ideapad_attribute_group);
 
547}
548
549/*
550 * input device
551 */
552static const struct key_entry ideapad_keymap[] = {
553	{ KE_KEY, 6,  { KEY_SWITCHVIDEOMODE } },
554	{ KE_KEY, 7,  { KEY_CAMERA } },
555	{ KE_KEY, 11, { KEY_F16 } },
556	{ KE_KEY, 13, { KEY_WLAN } },
557	{ KE_KEY, 16, { KEY_PROG1 } },
558	{ KE_KEY, 17, { KEY_PROG2 } },
559	{ KE_KEY, 64, { KEY_PROG3 } },
560	{ KE_KEY, 65, { KEY_PROG4 } },
561	{ KE_KEY, 66, { KEY_TOUCHPAD_OFF } },
562	{ KE_KEY, 67, { KEY_TOUCHPAD_ON } },
563	{ KE_END, 0 },
564};
565
566static int ideapad_input_init(struct ideapad_private *priv)
567{
568	struct input_dev *inputdev;
569	int error;
570
571	inputdev = input_allocate_device();
572	if (!inputdev)
 
573		return -ENOMEM;
 
574
575	inputdev->name = "Ideapad extra buttons";
576	inputdev->phys = "ideapad/input0";
577	inputdev->id.bustype = BUS_HOST;
578	inputdev->dev.parent = &priv->platform_device->dev;
579
580	error = sparse_keymap_setup(inputdev, ideapad_keymap, NULL);
581	if (error) {
582		pr_err("Unable to setup input device keymap\n");
583		goto err_free_dev;
584	}
585
586	error = input_register_device(inputdev);
587	if (error) {
588		pr_err("Unable to register input device\n");
589		goto err_free_keymap;
590	}
591
592	priv->inputdev = inputdev;
593	return 0;
594
595err_free_keymap:
596	sparse_keymap_free(inputdev);
597err_free_dev:
598	input_free_device(inputdev);
599	return error;
600}
601
602static void ideapad_input_exit(struct ideapad_private *priv)
603{
604	sparse_keymap_free(priv->inputdev);
605	input_unregister_device(priv->inputdev);
606	priv->inputdev = NULL;
607}
608
609static void ideapad_input_report(struct ideapad_private *priv,
610				 unsigned long scancode)
611{
612	sparse_keymap_report_event(priv->inputdev, scancode, 1, true);
613}
614
615static void ideapad_input_novokey(struct ideapad_private *priv)
616{
617	unsigned long long_pressed;
618
619	if (read_ec_data(priv->adev->handle, VPCCMD_R_NOVO, &long_pressed))
620		return;
621	if (long_pressed)
622		ideapad_input_report(priv, 17);
623	else
624		ideapad_input_report(priv, 16);
625}
626
627static void ideapad_check_special_buttons(struct ideapad_private *priv)
628{
629	unsigned long bit, value;
630
631	read_ec_data(priv->adev->handle, VPCCMD_R_SPECIAL_BUTTONS, &value);
632
633	for (bit = 0; bit < 16; bit++) {
634		if (test_bit(bit, &value)) {
635			switch (bit) {
636			case 0:	/* Z580 */
637			case 6:	/* Z570 */
638				/* Thermal Management button */
639				ideapad_input_report(priv, 65);
640				break;
641			case 1:
642				/* OneKey Theater button */
643				ideapad_input_report(priv, 64);
644				break;
645			default:
646				pr_info("Unknown special button: %lu\n", bit);
647				break;
648			}
649		}
650	}
651}
652
653/*
654 * backlight
655 */
656static int ideapad_backlight_get_brightness(struct backlight_device *blightdev)
657{
658	struct ideapad_private *priv = bl_get_data(blightdev);
659	unsigned long now;
660
661	if (!priv)
662		return -EINVAL;
663
664	if (read_ec_data(priv->adev->handle, VPCCMD_R_BL, &now))
665		return -EIO;
666	return now;
667}
668
669static int ideapad_backlight_update_status(struct backlight_device *blightdev)
670{
671	struct ideapad_private *priv = bl_get_data(blightdev);
672
673	if (!priv)
674		return -EINVAL;
675
676	if (write_ec_cmd(priv->adev->handle, VPCCMD_W_BL,
677			 blightdev->props.brightness))
678		return -EIO;
679	if (write_ec_cmd(priv->adev->handle, VPCCMD_W_BL_POWER,
680			 blightdev->props.power == FB_BLANK_POWERDOWN ? 0 : 1))
681		return -EIO;
682
683	return 0;
684}
685
686static const struct backlight_ops ideapad_backlight_ops = {
687	.get_brightness = ideapad_backlight_get_brightness,
688	.update_status = ideapad_backlight_update_status,
689};
690
691static int ideapad_backlight_init(struct ideapad_private *priv)
692{
693	struct backlight_device *blightdev;
694	struct backlight_properties props;
695	unsigned long max, now, power;
696
697	if (read_ec_data(priv->adev->handle, VPCCMD_R_BL_MAX, &max))
698		return -EIO;
699	if (read_ec_data(priv->adev->handle, VPCCMD_R_BL, &now))
700		return -EIO;
701	if (read_ec_data(priv->adev->handle, VPCCMD_R_BL_POWER, &power))
702		return -EIO;
703
704	memset(&props, 0, sizeof(struct backlight_properties));
705	props.max_brightness = max;
706	props.type = BACKLIGHT_PLATFORM;
707	blightdev = backlight_device_register("ideapad",
708					      &priv->platform_device->dev,
709					      priv,
710					      &ideapad_backlight_ops,
711					      &props);
712	if (IS_ERR(blightdev)) {
713		pr_err("Could not register backlight device\n");
714		return PTR_ERR(blightdev);
715	}
716
717	priv->blightdev = blightdev;
718	blightdev->props.brightness = now;
719	blightdev->props.power = power ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
720	backlight_update_status(blightdev);
721
722	return 0;
723}
724
725static void ideapad_backlight_exit(struct ideapad_private *priv)
726{
727	if (priv->blightdev)
728		backlight_device_unregister(priv->blightdev);
729	priv->blightdev = NULL;
730}
731
732static void ideapad_backlight_notify_power(struct ideapad_private *priv)
733{
734	unsigned long power;
735	struct backlight_device *blightdev = priv->blightdev;
736
737	if (!blightdev)
738		return;
739	if (read_ec_data(priv->adev->handle, VPCCMD_R_BL_POWER, &power))
740		return;
741	blightdev->props.power = power ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
742}
743
744static void ideapad_backlight_notify_brightness(struct ideapad_private *priv)
745{
746	unsigned long now;
747
748	/* if we control brightness via acpi video driver */
749	if (priv->blightdev == NULL) {
750		read_ec_data(priv->adev->handle, VPCCMD_R_BL, &now);
751		return;
752	}
753
754	backlight_force_update(priv->blightdev, BACKLIGHT_UPDATE_HOTKEY);
755}
756
757/*
758 * module init/exit
759 */
760static void ideapad_sync_touchpad_state(struct ideapad_private *priv)
761{
762	unsigned long value;
763
764	/* Without reading from EC touchpad LED doesn't switch state */
765	if (!read_ec_data(priv->adev->handle, VPCCMD_R_TOUCHPAD, &value)) {
766		/* Some IdeaPads don't really turn off touchpad - they only
767		 * switch the LED state. We (de)activate KBC AUX port to turn
768		 * touchpad off and on. We send KEY_TOUCHPAD_OFF and
769		 * KEY_TOUCHPAD_ON to not to get out of sync with LED */
770		unsigned char param;
771		i8042_command(&param, value ? I8042_CMD_AUX_ENABLE :
772			      I8042_CMD_AUX_DISABLE);
773		ideapad_input_report(priv, value ? 67 : 66);
774	}
775}
776
777static void ideapad_acpi_notify(acpi_handle handle, u32 event, void *data)
778{
779	struct ideapad_private *priv = data;
780	unsigned long vpc1, vpc2, vpc_bit;
781
782	if (read_ec_data(handle, VPCCMD_R_VPC1, &vpc1))
783		return;
784	if (read_ec_data(handle, VPCCMD_R_VPC2, &vpc2))
785		return;
786
787	vpc1 = (vpc2 << 8) | vpc1;
788	for (vpc_bit = 0; vpc_bit < 16; vpc_bit++) {
789		if (test_bit(vpc_bit, &vpc1)) {
790			switch (vpc_bit) {
791			case 9:
792				ideapad_sync_rfk_state(priv);
793				break;
794			case 13:
795			case 11:
796			case 7:
797			case 6:
798				ideapad_input_report(priv, vpc_bit);
799				break;
800			case 5:
801				ideapad_sync_touchpad_state(priv);
802				break;
803			case 4:
804				ideapad_backlight_notify_brightness(priv);
805				break;
806			case 3:
807				ideapad_input_novokey(priv);
808				break;
809			case 2:
810				ideapad_backlight_notify_power(priv);
811				break;
812			case 0:
813				ideapad_check_special_buttons(priv);
814				break;
815			default:
816				pr_info("Unknown event: %lu\n", vpc_bit);
817			}
818		}
819	}
820}
821
822static int ideapad_acpi_add(struct platform_device *pdev)
823{
824	int ret, i;
825	int cfg;
826	struct ideapad_private *priv;
827	struct acpi_device *adev;
828
829	ret = acpi_bus_get_device(ACPI_HANDLE(&pdev->dev), &adev);
830	if (ret)
831		return -ENODEV;
832
833	if (read_method_int(adev->handle, "_CFG", &cfg))
834		return -ENODEV;
835
836	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
837	if (!priv)
838		return -ENOMEM;
839
840	dev_set_drvdata(&pdev->dev, priv);
841	priv->cfg = cfg;
842	priv->adev = adev;
843	priv->platform_device = pdev;
844
845	ret = ideapad_sysfs_init(priv);
846	if (ret)
847		goto sysfs_failed;
848
849	ret = ideapad_debugfs_init(priv);
850	if (ret)
851		goto debugfs_failed;
852
853	ret = ideapad_input_init(priv);
854	if (ret)
855		goto input_failed;
856
857	for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++) {
858		if (test_bit(ideapad_rfk_data[i].cfgbit, &priv->cfg))
859			ideapad_register_rfkill(priv, i);
860		else
861			priv->rfk[i] = NULL;
862	}
863	ideapad_sync_rfk_state(priv);
864	ideapad_sync_touchpad_state(priv);
865
866	if (!acpi_video_backlight_support()) {
867		ret = ideapad_backlight_init(priv);
868		if (ret && ret != -ENODEV)
869			goto backlight_failed;
870	}
871	ret = acpi_install_notify_handler(adev->handle,
872		ACPI_DEVICE_NOTIFY, ideapad_acpi_notify, priv);
873	if (ret)
874		goto notification_failed;
875
876	return 0;
877notification_failed:
878	ideapad_backlight_exit(priv);
879backlight_failed:
880	for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
881		ideapad_unregister_rfkill(priv, i);
882	ideapad_input_exit(priv);
883input_failed:
884	ideapad_debugfs_exit(priv);
885debugfs_failed:
886	ideapad_sysfs_exit(priv);
887sysfs_failed:
888	kfree(priv);
889	return ret;
890}
891
892static int ideapad_acpi_remove(struct platform_device *pdev)
893{
894	struct ideapad_private *priv = dev_get_drvdata(&pdev->dev);
895	int i;
896
897	acpi_remove_notify_handler(priv->adev->handle,
898		ACPI_DEVICE_NOTIFY, ideapad_acpi_notify);
899	ideapad_backlight_exit(priv);
900	for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
901		ideapad_unregister_rfkill(priv, i);
902	ideapad_input_exit(priv);
903	ideapad_debugfs_exit(priv);
904	ideapad_sysfs_exit(priv);
905	dev_set_drvdata(&pdev->dev, NULL);
906	kfree(priv);
907
908	return 0;
909}
910
911#ifdef CONFIG_PM_SLEEP
912static int ideapad_acpi_resume(struct device *device)
913{
914	struct ideapad_private *priv;
 
 
915
916	if (!device)
917		return -EINVAL;
918	priv = dev_get_drvdata(device);
 
919
920	ideapad_sync_rfk_state(priv);
921	ideapad_sync_touchpad_state(priv);
922	return 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
923}
924#endif
925static SIMPLE_DEV_PM_OPS(ideapad_pm, NULL, ideapad_acpi_resume);
926
927static const struct acpi_device_id ideapad_device_ids[] = {
928	{ "VPC2004", 0},
929	{ "", 0},
 
 
 
 
 
930};
931MODULE_DEVICE_TABLE(acpi, ideapad_device_ids);
932
933static struct platform_driver ideapad_acpi_driver = {
934	.probe = ideapad_acpi_add,
935	.remove = ideapad_acpi_remove,
936	.driver = {
937		.name   = "ideapad_acpi",
938		.owner  = THIS_MODULE,
939		.pm     = &ideapad_pm,
940		.acpi_match_table = ACPI_PTR(ideapad_device_ids),
941	},
942};
943
944module_platform_driver(ideapad_acpi_driver);
 
 
 
945
946MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
947MODULE_DESCRIPTION("IdeaPad ACPI Extras");
948MODULE_LICENSE("GPL");