Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *  OLPC XO-1.5 ebook switch driver
0004  *  (based on generic ACPI button driver)
0005  *
0006  *  Copyright (C) 2009 Paul Fox <pgf@laptop.org>
0007  *  Copyright (C) 2010 One Laptop per Child
0008  */
0009 
0010 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0011 
0012 #include <linux/kernel.h>
0013 #include <linux/module.h>
0014 #include <linux/init.h>
0015 #include <linux/types.h>
0016 #include <linux/input.h>
0017 #include <linux/acpi.h>
0018 
0019 #define MODULE_NAME "xo15-ebook"
0020 
0021 #define XO15_EBOOK_CLASS        MODULE_NAME
0022 #define XO15_EBOOK_TYPE_UNKNOWN 0x00
0023 #define XO15_EBOOK_NOTIFY_STATUS    0x80
0024 
0025 #define XO15_EBOOK_SUBCLASS     "ebook"
0026 #define XO15_EBOOK_HID          "XO15EBK"
0027 #define XO15_EBOOK_DEVICE_NAME      "EBook Switch"
0028 
0029 MODULE_DESCRIPTION("OLPC XO-1.5 ebook switch driver");
0030 MODULE_LICENSE("GPL");
0031 
0032 static const struct acpi_device_id ebook_device_ids[] = {
0033     { XO15_EBOOK_HID, 0 },
0034     { "", 0 },
0035 };
0036 MODULE_DEVICE_TABLE(acpi, ebook_device_ids);
0037 
0038 struct ebook_switch {
0039     struct input_dev *input;
0040     char phys[32];          /* for input device */
0041 };
0042 
0043 static int ebook_send_state(struct acpi_device *device)
0044 {
0045     struct ebook_switch *button = acpi_driver_data(device);
0046     unsigned long long state;
0047     acpi_status status;
0048 
0049     status = acpi_evaluate_integer(device->handle, "EBK", NULL, &state);
0050     if (ACPI_FAILURE(status))
0051         return -EIO;
0052 
0053     /* input layer checks if event is redundant */
0054     input_report_switch(button->input, SW_TABLET_MODE, !state);
0055     input_sync(button->input);
0056     return 0;
0057 }
0058 
0059 static void ebook_switch_notify(struct acpi_device *device, u32 event)
0060 {
0061     switch (event) {
0062     case ACPI_FIXED_HARDWARE_EVENT:
0063     case XO15_EBOOK_NOTIFY_STATUS:
0064         ebook_send_state(device);
0065         break;
0066     default:
0067         acpi_handle_debug(device->handle,
0068                   "Unsupported event [0x%x]\n", event);
0069         break;
0070     }
0071 }
0072 
0073 #ifdef CONFIG_PM_SLEEP
0074 static int ebook_switch_resume(struct device *dev)
0075 {
0076     return ebook_send_state(to_acpi_device(dev));
0077 }
0078 #endif
0079 
0080 static SIMPLE_DEV_PM_OPS(ebook_switch_pm, NULL, ebook_switch_resume);
0081 
0082 static int ebook_switch_add(struct acpi_device *device)
0083 {
0084     struct ebook_switch *button;
0085     struct input_dev *input;
0086     const char *hid = acpi_device_hid(device);
0087     char *name, *class;
0088     int error;
0089 
0090     button = kzalloc(sizeof(struct ebook_switch), GFP_KERNEL);
0091     if (!button)
0092         return -ENOMEM;
0093 
0094     device->driver_data = button;
0095 
0096     button->input = input = input_allocate_device();
0097     if (!input) {
0098         error = -ENOMEM;
0099         goto err_free_button;
0100     }
0101 
0102     name = acpi_device_name(device);
0103     class = acpi_device_class(device);
0104 
0105     if (strcmp(hid, XO15_EBOOK_HID)) {
0106         pr_err("Unsupported hid [%s]\n", hid);
0107         error = -ENODEV;
0108         goto err_free_input;
0109     }
0110 
0111     strcpy(name, XO15_EBOOK_DEVICE_NAME);
0112     sprintf(class, "%s/%s", XO15_EBOOK_CLASS, XO15_EBOOK_SUBCLASS);
0113 
0114     snprintf(button->phys, sizeof(button->phys), "%s/button/input0", hid);
0115 
0116     input->name = name;
0117     input->phys = button->phys;
0118     input->id.bustype = BUS_HOST;
0119     input->dev.parent = &device->dev;
0120 
0121     input->evbit[0] = BIT_MASK(EV_SW);
0122     set_bit(SW_TABLET_MODE, input->swbit);
0123 
0124     error = input_register_device(input);
0125     if (error)
0126         goto err_free_input;
0127 
0128     ebook_send_state(device);
0129 
0130     if (device->wakeup.flags.valid) {
0131         /* Button's GPE is run-wake GPE */
0132         acpi_enable_gpe(device->wakeup.gpe_device,
0133                 device->wakeup.gpe_number);
0134         device_set_wakeup_enable(&device->dev, true);
0135     }
0136 
0137     return 0;
0138 
0139  err_free_input:
0140     input_free_device(input);
0141  err_free_button:
0142     kfree(button);
0143     return error;
0144 }
0145 
0146 static int ebook_switch_remove(struct acpi_device *device)
0147 {
0148     struct ebook_switch *button = acpi_driver_data(device);
0149 
0150     input_unregister_device(button->input);
0151     kfree(button);
0152     return 0;
0153 }
0154 
0155 static struct acpi_driver xo15_ebook_driver = {
0156     .name = MODULE_NAME,
0157     .class = XO15_EBOOK_CLASS,
0158     .ids = ebook_device_ids,
0159     .ops = {
0160         .add = ebook_switch_add,
0161         .remove = ebook_switch_remove,
0162         .notify = ebook_switch_notify,
0163     },
0164     .drv.pm = &ebook_switch_pm,
0165 };
0166 module_acpi_driver(xo15_ebook_driver);