]> git.karo-electronics.de Git - karo-tx-linux.git/blobdiff - drivers/hid/hid-picolcd.c
Merge branch 'next' of git://git.kernel.org/pub/scm/linux/kernel/git/benh/powerpc
[karo-tx-linux.git] / drivers / hid / hid-picolcd.c
index 0eacc6b6d5bb0e5b87d46a17940bbdd9a11e1533..7aabf65c48ef0d4aa31b7e8f74a205d447477bbd 100644 (file)
@@ -77,7 +77,7 @@
 #define REPORT_HOOK_VERSION    0xf7 /* LCD: IN[2], OUT[1]   */
 #define REPORT_EXIT_FLASHER    0xff /*                      Bootloader: OUT[2]         */
 
-#if defined(CONFIG_FB) || defined(CONFIG_FB_MODULE)
+#ifdef CONFIG_HID_PICOLCD_FB
 /* Framebuffer
  *
  * The PicoLCD use a Topway LCD module of 256x64 pixel
@@ -128,7 +128,7 @@ static const struct fb_var_screeninfo picolcdfb_var = {
        .bits_per_pixel = 1,
        .grayscale      = 1,
 };
-#endif /* CONFIG_FB */
+#endif /* CONFIG_HID_PICOLCD_FB */
 
 /* Input device
  *
@@ -177,13 +177,14 @@ struct picolcd_data {
        int addr_sz;
 #endif
        u8 version[2];
+       unsigned short opmode_delay;
        /* input stuff */
        u8 pressed_keys[2];
        struct input_dev *input_keys;
        struct input_dev *input_cir;
        unsigned short keycode[PICOLCD_KEYS];
 
-#if defined(CONFIG_FB) || defined(CONFIG_FB_MODULE)
+#ifdef CONFIG_HID_PICOLCD_FB
        /* Framebuffer stuff */
        u8 fb_update_rate;
        u8 fb_bpp;
@@ -191,21 +192,21 @@ struct picolcd_data {
        u8 *fb_bitmap;          /* framebuffer */
        struct fb_info *fb_info;
        struct fb_deferred_io fb_defio;
-#endif /* CONFIG_FB */
-#if defined(CONFIG_LCD_CLASS_DEVICE) || defined(CONFIG_LCD_CLASS_DEVICE_MODULE)
+#endif /* CONFIG_HID_PICOLCD_FB */
+#ifdef CONFIG_HID_PICOLCD_LCD
        struct lcd_device *lcd;
        u8 lcd_contrast;
-#endif
-#if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) || defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
+#endif /* CONFIG_HID_PICOLCD_LCD */
+#ifdef CONFIG_HID_PICOLCD_BACKLIGHT
        struct backlight_device *backlight;
        u8 lcd_brightness;
        u8 lcd_power;
-#endif /* CONFIG_BACKLIGHT_CLASS_DEVICE */
-#if defined(CONFIG_LEDS_CLASS) || defined(CONFIG_LEDS_CLASS_MODULE)
+#endif /* CONFIG_HID_PICOLCD_BACKLIGHT */
+#ifdef CONFIG_HID_PICOLCD_LEDS
        /* LED stuff */
        u8 led_state;
        struct led_classdev *led[8];
-#endif /* CONFIG_LEDS_CLASS */
+#endif /* CONFIG_HID_PICOLCD_LEDS */
 
        /* Housekeeping stuff */
        spinlock_t lock;
@@ -287,7 +288,7 @@ static struct picolcd_pending *picolcd_send_and_wait(struct hid_device *hdev,
        return work;
 }
 
-#if defined(CONFIG_FB) || defined(CONFIG_FB_MODULE)
+#ifdef CONFIG_HID_PICOLCD_FB
 /* Send a given tile to PicoLCD */
 static int picolcd_fb_send_tile(struct hid_device *hdev, int chip, int tile)
 {
@@ -762,13 +763,13 @@ static inline int picolcd_init_framebuffer(struct picolcd_data *data)
 {
        return 0;
 }
-static void picolcd_exit_framebuffer(struct picolcd_data *data)
+static inline void picolcd_exit_framebuffer(struct picolcd_data *data)
 {
 }
 #define picolcd_fbinfo(d) NULL
-#endif /* CONFIG_FB */
+#endif /* CONFIG_HID_PICOLCD_FB */
 
-#if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) || defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
+#ifdef CONFIG_HID_PICOLCD_BACKLIGHT
 /*
  * backlight class device
  */
@@ -851,6 +852,18 @@ static inline int picolcd_resume_backlight(struct picolcd_data *data)
        return picolcd_set_brightness(data->backlight);
 }
 
+#ifdef CONFIG_PM
+static void picolcd_suspend_backlight(struct picolcd_data *data)
+{
+       int bl_power = data->lcd_power;
+       if (!data->backlight)
+               return;
+
+       data->backlight->props.power = FB_BLANK_POWERDOWN;
+       picolcd_set_brightness(data->backlight);
+       data->lcd_power = data->backlight->props.power = bl_power;
+}
+#endif /* CONFIG_PM */
 #else
 static inline int picolcd_init_backlight(struct picolcd_data *data,
                struct hid_report *report)
@@ -864,9 +877,12 @@ static inline int picolcd_resume_backlight(struct picolcd_data *data)
 {
        return 0;
 }
-#endif /* CONFIG_BACKLIGHT_CLASS_DEVICE */
+static inline void picolcd_suspend_backlight(struct picolcd_data *data)
+{
+}
+#endif /* CONFIG_HID_PICOLCD_BACKLIGHT */
 
-#if defined(CONFIG_LCD_CLASS_DEVICE) || defined(CONFIG_LCD_CLASS_DEVICE_MODULE)
+#ifdef CONFIG_HID_PICOLCD_LCD
 /*
  * lcd class device
  */
@@ -957,9 +973,9 @@ static inline int picolcd_resume_lcd(struct picolcd_data *data)
 {
        return 0;
 }
-#endif /* CONFIG_LCD_CLASS_DEVICE */
+#endif /* CONFIG_HID_PICOLCD_LCD */
 
-#if defined(CONFIG_LEDS_CLASS) || defined(CONFIG_LEDS_CLASS_MODULE)
+#ifdef CONFIG_HID_PICOLCD_LEDS
 /**
  * LED class device
  */
@@ -1097,14 +1113,14 @@ static inline int picolcd_init_leds(struct picolcd_data *data,
 {
        return 0;
 }
-static void picolcd_exit_leds(struct picolcd_data *data)
+static inline void picolcd_exit_leds(struct picolcd_data *data)
 {
 }
 static inline int picolcd_leds_set(struct picolcd_data *data)
 {
        return 0;
 }
-#endif /* CONFIG_LEDS_CLASS */
+#endif /* CONFIG_HID_PICOLCD_LEDS */
 
 /*
  * input class device
@@ -1196,16 +1212,14 @@ static int picolcd_check_version(struct hid_device *hdev)
        }
 
        if (verinfo->raw_size == 2) {
+               data->version[0] = verinfo->raw_data[1];
+               data->version[1] = verinfo->raw_data[0];
                if (data->status & PICOLCD_BOOTLOADER) {
                        dev_info(&hdev->dev, "PicoLCD, bootloader version %d.%d\n",
-                                       verinfo->raw_data[0], verinfo->raw_data[1]);
-                       data->version[0] = verinfo->raw_data[0];
-                       data->version[1] = verinfo->raw_data[1];
+                                       verinfo->raw_data[1], verinfo->raw_data[0]);
                } else {
                        dev_info(&hdev->dev, "PicoLCD, firmware version %d.%d\n",
                                        verinfo->raw_data[1], verinfo->raw_data[0]);
-                       data->version[0] = verinfo->raw_data[1];
-                       data->version[1] = verinfo->raw_data[0];
                }
        } else {
                dev_err(&hdev->dev, "confused, got unexpected version response from PicoLCD\n");
@@ -1243,10 +1257,10 @@ static int picolcd_reset(struct hid_device *hdev)
 
        picolcd_resume_lcd(data);
        picolcd_resume_backlight(data);
-#if defined(CONFIG_FB) || defined(CONFIG_FB_MODULE)
+#ifdef CONFIG_HID_PICOLCD_FB
        if (data->fb_info)
                schedule_delayed_work(&data->fb_info->deferred_work, 0);
-#endif /* CONFIG_FB */
+#endif /* CONFIG_HID_PICOLCD_FB */
 
        picolcd_leds_set(data);
        return 0;
@@ -1272,8 +1286,7 @@ static ssize_t picolcd_operation_mode_store(struct device *dev,
        struct picolcd_data *data = dev_get_drvdata(dev);
        struct hid_report *report = NULL;
        size_t cnt = count;
-       int timeout = 5000;
-       unsigned u;
+       int timeout = data->opmode_delay;
        unsigned long flags;
 
        if (cnt >= 3 && strncmp("lcd", buf, 3) == 0) {
@@ -1290,20 +1303,10 @@ static ssize_t picolcd_operation_mode_store(struct device *dev,
        if (!report)
                return -EINVAL;
 
-       while (cnt > 0 && (*buf == ' ' || *buf == '\t')) {
-               buf++;
-               cnt--;
-       }
        while (cnt > 0 && (buf[cnt-1] == '\n' || buf[cnt-1] == '\r'))
                cnt--;
-       if (cnt > 0) {
-               if (sscanf(buf, "%u", &u) != 1)
-                       return -EINVAL;
-               if (u > 30000)
-                       return -EINVAL;
-               else
-                       timeout = u;
-       }
+       if (cnt != 0)
+               return -EINVAL;
 
        spin_lock_irqsave(&data->lock, flags);
        hid_set_field(report->field[0], 0, timeout & 0xff);
@@ -1316,6 +1319,34 @@ static ssize_t picolcd_operation_mode_store(struct device *dev,
 static DEVICE_ATTR(operation_mode, 0644, picolcd_operation_mode_show,
                picolcd_operation_mode_store);
 
+/*
+ * The "operation_mode_delay" sysfs attribute
+ */
+static ssize_t picolcd_operation_mode_delay_show(struct device *dev,
+               struct device_attribute *attr, char *buf)
+{
+       struct picolcd_data *data = dev_get_drvdata(dev);
+
+       return snprintf(buf, PAGE_SIZE, "%hu\n", data->opmode_delay);
+}
+
+static ssize_t picolcd_operation_mode_delay_store(struct device *dev,
+               struct device_attribute *attr, const char *buf, size_t count)
+{
+       struct picolcd_data *data = dev_get_drvdata(dev);
+       unsigned u;
+       if (sscanf(buf, "%u", &u) != 1)
+               return -EINVAL;
+       if (u > 30000)
+               return -EINVAL;
+       else
+               data->opmode_delay = u;
+       return count;
+}
+
+static DEVICE_ATTR(operation_mode_delay, 0644, picolcd_operation_mode_delay_show,
+               picolcd_operation_mode_delay_store);
+
 
 #ifdef CONFIG_DEBUG_FS
 /*
@@ -1585,7 +1616,7 @@ static ssize_t _picolcd_flash_write(struct picolcd_data *data, int report_id,
                raw_data[len_off] = s > 32 ? 32 : s;
                if (copy_from_user(raw_data+len_off+1, u, raw_data[len_off])) {
                        err = -EFAULT;
-                       goto skip;
+                       break;
                }
                resp = picolcd_send_and_wait(data->hdev, report_id, raw_data,
                                len_off+1+raw_data[len_off]);
@@ -2198,9 +2229,18 @@ static void picolcd_exit_devfs(struct picolcd_data *data)
        mutex_destroy(&data->mutex_flash);
 }
 #else
-#define picolcd_debug_raw_event(data, hdev, report, raw_data, size)
-#define picolcd_init_devfs(data, eeprom_r, eeprom_w, flash_r, flash_w, reset)
-static void picolcd_exit_devfs(struct picolcd_data *data)
+static inline void picolcd_debug_raw_event(struct picolcd_data *data,
+               struct hid_device *hdev, struct hid_report *report,
+               u8 *raw_data, int size)
+{
+}
+static inline void picolcd_init_devfs(struct picolcd_data *data,
+               struct hid_report *eeprom_r, struct hid_report *eeprom_w,
+               struct hid_report *flash_r, struct hid_report *flash_w,
+               struct hid_report *reset)
+{
+}
+static inline void picolcd_exit_devfs(struct picolcd_data *data)
 {
 }
 #endif /* CONFIG_DEBUG_FS */
@@ -2243,6 +2283,46 @@ static int picolcd_raw_event(struct hid_device *hdev,
        return 1;
 }
 
+#ifdef CONFIG_PM
+static int picolcd_suspend(struct hid_device *hdev, pm_message_t message)
+{
+       if (message.event & PM_EVENT_AUTO)
+               return 0;
+
+       picolcd_suspend_backlight(hid_get_drvdata(hdev));
+       dbg_hid(PICOLCD_NAME " device ready for suspend\n");
+       return 0;
+}
+
+static int picolcd_resume(struct hid_device *hdev)
+{
+       int ret;
+       ret = picolcd_resume_backlight(hid_get_drvdata(hdev));
+       if (ret)
+               dbg_hid(PICOLCD_NAME " restoring backlight failed: %d\n", ret);
+       return 0;
+}
+
+static int picolcd_reset_resume(struct hid_device *hdev)
+{
+       int ret;
+       ret = picolcd_reset(hdev);
+       if (ret)
+               dbg_hid(PICOLCD_NAME " resetting our device failed: %d\n", ret);
+       ret = picolcd_fb_reset(hid_get_drvdata(hdev), 0);
+       if (ret)
+               dbg_hid(PICOLCD_NAME " restoring framebuffer content failed: %d\n", ret);
+       ret = picolcd_resume_lcd(hid_get_drvdata(hdev));
+       if (ret)
+               dbg_hid(PICOLCD_NAME " restoring lcd failed: %d\n", ret);
+       ret = picolcd_resume_backlight(hid_get_drvdata(hdev));
+       if (ret)
+               dbg_hid(PICOLCD_NAME " restoring backlight failed: %d\n", ret);
+       picolcd_leds_set(hid_get_drvdata(hdev));
+       return 0;
+}
+#endif
+
 /* initialize keypad input device */
 static int picolcd_init_keys(struct picolcd_data *data,
                struct hid_report *report)
@@ -2411,6 +2491,7 @@ static int picolcd_probe(struct hid_device *hdev,
        spin_lock_init(&data->lock);
        mutex_init(&data->mutex);
        data->hdev = hdev;
+       data->opmode_delay = 5000;
        if (hdev->product == USB_DEVICE_ID_PICOLCD_BOOTLOADER)
                data->status |= PICOLCD_BOOTLOADER;
        hid_set_drvdata(hdev, data);
@@ -2438,24 +2519,32 @@ static int picolcd_probe(struct hid_device *hdev,
                goto err_cleanup_hid_hw;
        }
 
-       error = device_create_file(&hdev->dev, &dev_attr_operation_mode);
+       error = device_create_file(&hdev->dev, &dev_attr_operation_mode_delay);
        if (error) {
                dev_err(&hdev->dev, "failed to create sysfs attributes\n");
                goto err_cleanup_hid_ll;
        }
 
+       error = device_create_file(&hdev->dev, &dev_attr_operation_mode);
+       if (error) {
+               dev_err(&hdev->dev, "failed to create sysfs attributes\n");
+               goto err_cleanup_sysfs1;
+       }
+
        if (data->status & PICOLCD_BOOTLOADER)
                error = picolcd_probe_bootloader(hdev, data);
        else
                error = picolcd_probe_lcd(hdev, data);
        if (error)
-               goto err_cleanup_sysfs;
+               goto err_cleanup_sysfs2;
 
        dbg_hid(PICOLCD_NAME " activated and initialized\n");
        return 0;
 
-err_cleanup_sysfs:
+err_cleanup_sysfs2:
        device_remove_file(&hdev->dev, &dev_attr_operation_mode);
+err_cleanup_sysfs1:
+       device_remove_file(&hdev->dev, &dev_attr_operation_mode_delay);
 err_cleanup_hid_ll:
        hdev->ll_driver->close(hdev);
 err_cleanup_hid_hw:
@@ -2480,6 +2569,7 @@ static void picolcd_remove(struct hid_device *hdev)
 
        picolcd_exit_devfs(data);
        device_remove_file(&hdev->dev, &dev_attr_operation_mode);
+       device_remove_file(&hdev->dev, &dev_attr_operation_mode_delay);
        hdev->ll_driver->close(hdev);
        hid_hw_stop(hdev);
        hid_set_drvdata(hdev, NULL);
@@ -2518,6 +2608,11 @@ static struct hid_driver picolcd_driver = {
        .probe =         picolcd_probe,
        .remove =        picolcd_remove,
        .raw_event =     picolcd_raw_event,
+#ifdef CONFIG_PM
+       .suspend =       picolcd_suspend,
+       .resume =        picolcd_resume,
+       .reset_resume =  picolcd_reset_resume,
+#endif
 };
 
 static int __init picolcd_init(void)