X25: remove duplicated #include
[safe/jmp/linux-2.6] / drivers / hid / hid-picolcd.c
index 0ea7a3f..7aabf65 100644 (file)
 #include <linux/fb.h>
 #include <linux/vmalloc.h>
 #include <linux/backlight.h>
+#include <linux/lcd.h>
+
+#include <linux/leds.h>
 
 #include <linux/seq_file.h>
 #include <linux/debugfs.h>
 
 #include <linux/completion.h>
+#include <linux/uaccess.h>
 
 #define PICOLCD_NAME "PicoLCD (graphic)"
 
@@ -73,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
@@ -124,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
  *
@@ -166,16 +170,21 @@ struct picolcd_pending {
 struct picolcd_data {
        struct hid_device *hdev;
 #ifdef CONFIG_DEBUG_FS
+       struct dentry *debug_reset;
+       struct dentry *debug_eeprom;
+       struct dentry *debug_flash;
+       struct mutex mutex_flash;
        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;
@@ -183,12 +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_BACKLIGHT_CLASS_DEVICE) || defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
+#endif /* CONFIG_HID_PICOLCD_FB */
+#ifdef CONFIG_HID_PICOLCD_LCD
+       struct lcd_device *lcd;
+       u8 lcd_contrast;
+#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 */
+#endif /* CONFIG_HID_PICOLCD_BACKLIGHT */
+#ifdef CONFIG_HID_PICOLCD_LEDS
+       /* LED stuff */
+       u8 led_state;
+       struct led_classdev *led[8];
+#endif /* CONFIG_HID_PICOLCD_LEDS */
 
        /* Housekeeping stuff */
        spinlock_t lock;
@@ -270,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)
 {
@@ -745,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
  */
@@ -834,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)
@@ -847,7 +877,250 @@ 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 */
+
+#ifdef CONFIG_HID_PICOLCD_LCD
+/*
+ * lcd class device
+ */
+static int picolcd_get_contrast(struct lcd_device *ldev)
+{
+       struct picolcd_data *data = lcd_get_data(ldev);
+       return data->lcd_contrast;
+}
+
+static int picolcd_set_contrast(struct lcd_device *ldev, int contrast)
+{
+       struct picolcd_data *data = lcd_get_data(ldev);
+       struct hid_report *report = picolcd_out_report(REPORT_CONTRAST, data->hdev);
+       unsigned long flags;
+
+       if (!report || report->maxfield != 1 || report->field[0]->report_count != 1)
+               return -ENODEV;
+
+       data->lcd_contrast = contrast & 0x0ff;
+       spin_lock_irqsave(&data->lock, flags);
+       hid_set_field(report->field[0], 0, data->lcd_contrast);
+       usbhid_submit_report(data->hdev, report, USB_DIR_OUT);
+       spin_unlock_irqrestore(&data->lock, flags);
+       return 0;
+}
+
+static int picolcd_check_lcd_fb(struct lcd_device *ldev, struct fb_info *fb)
+{
+       return fb && fb == picolcd_fbinfo((struct picolcd_data *)lcd_get_data(ldev));
+}
+
+static struct lcd_ops picolcd_lcdops = {
+       .get_contrast   = picolcd_get_contrast,
+       .set_contrast   = picolcd_set_contrast,
+       .check_fb       = picolcd_check_lcd_fb,
+};
+
+static int picolcd_init_lcd(struct picolcd_data *data, struct hid_report *report)
+{
+       struct device *dev = &data->hdev->dev;
+       struct lcd_device *ldev;
+
+       if (!report)
+               return -ENODEV;
+       if (report->maxfield != 1 || report->field[0]->report_count != 1 ||
+                       report->field[0]->report_size != 8) {
+               dev_err(dev, "unsupported CONTRAST report");
+               return -EINVAL;
+       }
+
+       ldev = lcd_device_register(dev_name(dev), dev, data, &picolcd_lcdops);
+       if (IS_ERR(ldev)) {
+               dev_err(dev, "failed to register LCD\n");
+               return PTR_ERR(ldev);
+       }
+       ldev->props.max_contrast = 0x0ff;
+       data->lcd_contrast = 0xe5;
+       data->lcd = ldev;
+       picolcd_set_contrast(ldev, 0xe5);
+       return 0;
+}
+
+static void picolcd_exit_lcd(struct picolcd_data *data)
+{
+       struct lcd_device *ldev = data->lcd;
+
+       data->lcd = NULL;
+       if (ldev)
+               lcd_device_unregister(ldev);
+}
+
+static inline int picolcd_resume_lcd(struct picolcd_data *data)
+{
+       if (!data->lcd)
+               return 0;
+       return picolcd_set_contrast(data->lcd, data->lcd_contrast);
+}
+#else
+static inline int picolcd_init_lcd(struct picolcd_data *data,
+               struct hid_report *report)
+{
+       return 0;
+}
+static inline void picolcd_exit_lcd(struct picolcd_data *data)
+{
+}
+static inline int picolcd_resume_lcd(struct picolcd_data *data)
+{
+       return 0;
+}
+#endif /* CONFIG_HID_PICOLCD_LCD */
+
+#ifdef CONFIG_HID_PICOLCD_LEDS
+/**
+ * LED class device
+ */
+static void picolcd_leds_set(struct picolcd_data *data)
+{
+       struct hid_report *report;
+       unsigned long flags;
+
+       if (!data->led[0])
+               return;
+       report = picolcd_out_report(REPORT_LED_STATE, data->hdev);
+       if (!report || report->maxfield != 1 || report->field[0]->report_count != 1)
+               return;
+
+       spin_lock_irqsave(&data->lock, flags);
+       hid_set_field(report->field[0], 0, data->led_state);
+       usbhid_submit_report(data->hdev, report, USB_DIR_OUT);
+       spin_unlock_irqrestore(&data->lock, flags);
+}
+
+static void picolcd_led_set_brightness(struct led_classdev *led_cdev,
+                       enum led_brightness value)
+{
+       struct device *dev;
+       struct hid_device *hdev;
+       struct picolcd_data *data;
+       int i, state = 0;
+
+       dev  = led_cdev->dev->parent;
+       hdev = container_of(dev, struct hid_device, dev);
+       data = hid_get_drvdata(hdev);
+       for (i = 0; i < 8; i++) {
+               if (led_cdev != data->led[i])
+                       continue;
+               state = (data->led_state >> i) & 1;
+               if (value == LED_OFF && state) {
+                       data->led_state &= ~(1 << i);
+                       picolcd_leds_set(data);
+               } else if (value != LED_OFF && !state) {
+                       data->led_state |= 1 << i;
+                       picolcd_leds_set(data);
+               }
+               break;
+       }
+}
+
+static enum led_brightness picolcd_led_get_brightness(struct led_classdev *led_cdev)
+{
+       struct device *dev;
+       struct hid_device *hdev;
+       struct picolcd_data *data;
+       int i, value = 0;
+
+       dev  = led_cdev->dev->parent;
+       hdev = container_of(dev, struct hid_device, dev);
+       data = hid_get_drvdata(hdev);
+       for (i = 0; i < 8; i++)
+               if (led_cdev == data->led[i]) {
+                       value = (data->led_state >> i) & 1;
+                       break;
+               }
+       return value ? LED_FULL : LED_OFF;
+}
+
+static int picolcd_init_leds(struct picolcd_data *data, struct hid_report *report)
+{
+       struct device *dev = &data->hdev->dev;
+       struct led_classdev *led;
+       size_t name_sz = strlen(dev_name(dev)) + 8;
+       char *name;
+       int i, ret = 0;
+
+       if (!report)
+               return -ENODEV;
+       if (report->maxfield != 1 || report->field[0]->report_count != 1 ||
+                       report->field[0]->report_size != 8) {
+               dev_err(dev, "unsupported LED_STATE report");
+               return -EINVAL;
+       }
+
+       for (i = 0; i < 8; i++) {
+               led = kzalloc(sizeof(struct led_classdev)+name_sz, GFP_KERNEL);
+               if (!led) {
+                       dev_err(dev, "can't allocate memory for LED %d\n", i);
+                       ret = -ENOMEM;
+                       goto err;
+               }
+               name = (void *)(&led[1]);
+               snprintf(name, name_sz, "%s::GPO%d", dev_name(dev), i);
+               led->name = name;
+               led->brightness = 0;
+               led->max_brightness = 1;
+               led->brightness_get = picolcd_led_get_brightness;
+               led->brightness_set = picolcd_led_set_brightness;
+
+               data->led[i] = led;
+               ret = led_classdev_register(dev, data->led[i]);
+               if (ret) {
+                       data->led[i] = NULL;
+                       kfree(led);
+                       dev_err(dev, "can't register LED %d\n", i);
+                       goto err;
+               }
+       }
+       return 0;
+err:
+       for (i = 0; i < 8; i++)
+               if (data->led[i]) {
+                       led = data->led[i];
+                       data->led[i] = NULL;
+                       led_classdev_unregister(led);
+                       kfree(led);
+               }
+       return ret;
+}
+
+static void picolcd_exit_leds(struct picolcd_data *data)
+{
+       struct led_classdev *led;
+       int i;
+
+       for (i = 0; i < 8; i++) {
+               led = data->led[i];
+               data->led[i] = NULL;
+               if (!led)
+                       continue;
+               led_classdev_unregister(led);
+               kfree(led);
+       }
+}
+
+#else
+static inline int picolcd_init_leds(struct picolcd_data *data,
+               struct hid_report *report)
+{
+       return 0;
+}
+static inline void picolcd_exit_leds(struct picolcd_data *data)
+{
+}
+static inline int picolcd_leds_set(struct picolcd_data *data)
+{
+       return 0;
+}
+#endif /* CONFIG_HID_PICOLCD_LEDS */
 
 /*
  * input class device
@@ -939,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");
@@ -984,12 +1255,14 @@ static int picolcd_reset(struct hid_device *hdev)
        if (error)
                return error;
 
+       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;
 }
 
@@ -1013,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) {
@@ -1031,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);
@@ -1057,9 +1319,388 @@ 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
 /*
+ * The "reset" file
+ */
+static int picolcd_debug_reset_show(struct seq_file *f, void *p)
+{
+       if (picolcd_fbinfo((struct picolcd_data *)f->private))
+               seq_printf(f, "all fb\n");
+       else
+               seq_printf(f, "all\n");
+       return 0;
+}
+
+static int picolcd_debug_reset_open(struct inode *inode, struct file *f)
+{
+       return single_open(f, picolcd_debug_reset_show, inode->i_private);
+}
+
+static ssize_t picolcd_debug_reset_write(struct file *f, const char __user *user_buf,
+               size_t count, loff_t *ppos)
+{
+       struct picolcd_data *data = ((struct seq_file *)f->private_data)->private;
+       char buf[32];
+       size_t cnt = min(count, sizeof(buf)-1);
+       if (copy_from_user(buf, user_buf, cnt))
+               return -EFAULT;
+
+       while (cnt > 0 && (buf[cnt-1] == ' ' || buf[cnt-1] == '\n'))
+               cnt--;
+       buf[cnt] = '\0';
+       if (strcmp(buf, "all") == 0) {
+               picolcd_reset(data->hdev);
+               picolcd_fb_reset(data, 1);
+       } else if (strcmp(buf, "fb") == 0) {
+               picolcd_fb_reset(data, 1);
+       } else {
+               return -EINVAL;
+       }
+       return count;
+}
+
+static const struct file_operations picolcd_debug_reset_fops = {
+       .owner    = THIS_MODULE,
+       .open     = picolcd_debug_reset_open,
+       .read     = seq_read,
+       .llseek   = seq_lseek,
+       .write    = picolcd_debug_reset_write,
+       .release  = single_release,
+};
+
+/*
+ * The "eeprom" file
+ */
+static int picolcd_debug_eeprom_open(struct inode *i, struct file *f)
+{
+       f->private_data = i->i_private;
+       return 0;
+}
+
+static ssize_t picolcd_debug_eeprom_read(struct file *f, char __user *u,
+               size_t s, loff_t *off)
+{
+       struct picolcd_data *data = f->private_data;
+       struct picolcd_pending *resp;
+       u8 raw_data[3];
+       ssize_t ret = -EIO;
+
+       if (s == 0)
+               return -EINVAL;
+       if (*off > 0x0ff)
+               return 0;
+
+       /* prepare buffer with info about what we want to read (addr & len) */
+       raw_data[0] = *off & 0xff;
+       raw_data[1] = (*off >> 8) && 0xff;
+       raw_data[2] = s < 20 ? s : 20;
+       if (*off + raw_data[2] > 0xff)
+               raw_data[2] = 0x100 - *off;
+       resp = picolcd_send_and_wait(data->hdev, REPORT_EE_READ, raw_data,
+                       sizeof(raw_data));
+       if (!resp)
+               return -EIO;
+
+       if (resp->in_report && resp->in_report->id == REPORT_EE_DATA) {
+               /* successful read :) */
+               ret = resp->raw_data[2];
+               if (ret > s)
+                       ret = s;
+               if (copy_to_user(u, resp->raw_data+3, ret))
+                       ret = -EFAULT;
+               else
+                       *off += ret;
+       } /* anything else is some kind of IO error */
+
+       kfree(resp);
+       return ret;
+}
+
+static ssize_t picolcd_debug_eeprom_write(struct file *f, const char __user *u,
+               size_t s, loff_t *off)
+{
+       struct picolcd_data *data = f->private_data;
+       struct picolcd_pending *resp;
+       ssize_t ret = -EIO;
+       u8 raw_data[23];
+
+       if (s == 0)
+               return -EINVAL;
+       if (*off > 0x0ff)
+               return -ENOSPC;
+
+       memset(raw_data, 0, sizeof(raw_data));
+       raw_data[0] = *off & 0xff;
+       raw_data[1] = (*off >> 8) && 0xff;
+       raw_data[2] = s < 20 ? s : 20;
+       if (*off + raw_data[2] > 0xff)
+               raw_data[2] = 0x100 - *off;
+
+       if (copy_from_user(raw_data+3, u, raw_data[2]))
+               return -EFAULT;
+       resp = picolcd_send_and_wait(data->hdev, REPORT_EE_WRITE, raw_data,
+                       sizeof(raw_data));
+
+       if (!resp)
+               return -EIO;
+
+       if (resp->in_report && resp->in_report->id == REPORT_EE_DATA) {
+               /* check if written data matches */
+               if (memcmp(raw_data, resp->raw_data, 3+raw_data[2]) == 0) {
+                       *off += raw_data[2];
+                       ret = raw_data[2];
+               }
+       }
+       kfree(resp);
+       return ret;
+}
+
+/*
+ * Notes:
+ * - read/write happens in chunks of at most 20 bytes, it's up to userspace
+ *   to loop in order to get more data.
+ * - on write errors on otherwise correct write request the bytes
+ *   that should have been written are in undefined state.
+ */
+static const struct file_operations picolcd_debug_eeprom_fops = {
+       .owner    = THIS_MODULE,
+       .open     = picolcd_debug_eeprom_open,
+       .read     = picolcd_debug_eeprom_read,
+       .write    = picolcd_debug_eeprom_write,
+       .llseek   = generic_file_llseek,
+};
+
+/*
+ * The "flash" file
+ */
+static int picolcd_debug_flash_open(struct inode *i, struct file *f)
+{
+       f->private_data = i->i_private;
+       return 0;
+}
+
+/* record a flash address to buf (bounds check to be done by caller) */
+static int _picolcd_flash_setaddr(struct picolcd_data *data, u8 *buf, long off)
+{
+       buf[0] = off & 0xff;
+       buf[1] = (off >> 8) & 0xff;
+       if (data->addr_sz == 3)
+               buf[2] = (off >> 16) & 0xff;
+       return data->addr_sz == 2 ? 2 : 3;
+}
+
+/* read a given size of data (bounds check to be done by caller) */
+static ssize_t _picolcd_flash_read(struct picolcd_data *data, int report_id,
+               char __user *u, size_t s, loff_t *off)
+{
+       struct picolcd_pending *resp;
+       u8 raw_data[4];
+       ssize_t ret = 0;
+       int len_off, err = -EIO;
+
+       while (s > 0) {
+               err = -EIO;
+               len_off = _picolcd_flash_setaddr(data, raw_data, *off);
+               raw_data[len_off] = s > 32 ? 32 : s;
+               resp = picolcd_send_and_wait(data->hdev, report_id, raw_data, len_off+1);
+               if (!resp || !resp->in_report)
+                       goto skip;
+               if (resp->in_report->id == REPORT_MEMORY ||
+                       resp->in_report->id == REPORT_BL_READ_MEMORY) {
+                       if (memcmp(raw_data, resp->raw_data, len_off+1) != 0)
+                               goto skip;
+                       if (copy_to_user(u+ret, resp->raw_data+len_off+1, raw_data[len_off])) {
+                               err = -EFAULT;
+                               goto skip;
+                       }
+                       *off += raw_data[len_off];
+                       s    -= raw_data[len_off];
+                       ret  += raw_data[len_off];
+                       err   = 0;
+               }
+skip:
+               kfree(resp);
+               if (err)
+                       return ret > 0 ? ret : err;
+       }
+       return ret;
+}
+
+static ssize_t picolcd_debug_flash_read(struct file *f, char __user *u,
+               size_t s, loff_t *off)
+{
+       struct picolcd_data *data = f->private_data;
+
+       if (s == 0)
+               return -EINVAL;
+       if (*off > 0x05fff)
+               return 0;
+       if (*off + s > 0x05fff)
+               s = 0x06000 - *off;
+
+       if (data->status & PICOLCD_BOOTLOADER)
+               return _picolcd_flash_read(data, REPORT_BL_READ_MEMORY, u, s, off);
+       else
+               return _picolcd_flash_read(data, REPORT_READ_MEMORY, u, s, off);
+}
+
+/* erase block aligned to 64bytes boundary */
+static ssize_t _picolcd_flash_erase64(struct picolcd_data *data, int report_id,
+               loff_t *off)
+{
+       struct picolcd_pending *resp;
+       u8 raw_data[3];
+       int len_off;
+       ssize_t ret = -EIO;
+
+       if (*off & 0x3f)
+               return -EINVAL;
+
+       len_off = _picolcd_flash_setaddr(data, raw_data, *off);
+       resp = picolcd_send_and_wait(data->hdev, report_id, raw_data, len_off);
+       if (!resp || !resp->in_report)
+               goto skip;
+       if (resp->in_report->id == REPORT_MEMORY ||
+               resp->in_report->id == REPORT_BL_ERASE_MEMORY) {
+               if (memcmp(raw_data, resp->raw_data, len_off) != 0)
+                       goto skip;
+               ret = 0;
+       }
+skip:
+       kfree(resp);
+       return ret;
+}
+
+/* write a given size of data (bounds check to be done by caller) */
+static ssize_t _picolcd_flash_write(struct picolcd_data *data, int report_id,
+               const char __user *u, size_t s, loff_t *off)
+{
+       struct picolcd_pending *resp;
+       u8 raw_data[36];
+       ssize_t ret = 0;
+       int len_off, err = -EIO;
+
+       while (s > 0) {
+               err = -EIO;
+               len_off = _picolcd_flash_setaddr(data, raw_data, *off);
+               raw_data[len_off] = s > 32 ? 32 : s;
+               if (copy_from_user(raw_data+len_off+1, u, raw_data[len_off])) {
+                       err = -EFAULT;
+                       break;
+               }
+               resp = picolcd_send_and_wait(data->hdev, report_id, raw_data,
+                               len_off+1+raw_data[len_off]);
+               if (!resp || !resp->in_report)
+                       goto skip;
+               if (resp->in_report->id == REPORT_MEMORY ||
+                       resp->in_report->id == REPORT_BL_WRITE_MEMORY) {
+                       if (memcmp(raw_data, resp->raw_data, len_off+1+raw_data[len_off]) != 0)
+                               goto skip;
+                       *off += raw_data[len_off];
+                       s    -= raw_data[len_off];
+                       ret  += raw_data[len_off];
+                       err   = 0;
+               }
+skip:
+               kfree(resp);
+               if (err)
+                       break;
+       }
+       return ret > 0 ? ret : err;
+}
+
+static ssize_t picolcd_debug_flash_write(struct file *f, const char __user *u,
+               size_t s, loff_t *off)
+{
+       struct picolcd_data *data = f->private_data;
+       ssize_t err, ret = 0;
+       int report_erase, report_write;
+
+       if (s == 0)
+               return -EINVAL;
+       if (*off > 0x5fff)
+               return -ENOSPC;
+       if (s & 0x3f)
+               return -EINVAL;
+       if (*off & 0x3f)
+               return -EINVAL;
+
+       if (data->status & PICOLCD_BOOTLOADER) {
+               report_erase = REPORT_BL_ERASE_MEMORY;
+               report_write = REPORT_BL_WRITE_MEMORY;
+       } else {
+               report_erase = REPORT_ERASE_MEMORY;
+               report_write = REPORT_WRITE_MEMORY;
+       }
+       mutex_lock(&data->mutex_flash);
+       while (s > 0) {
+               err = _picolcd_flash_erase64(data, report_erase, off);
+               if (err)
+                       break;
+               err = _picolcd_flash_write(data, report_write, u, 64, off);
+               if (err < 0)
+                       break;
+               ret += err;
+               *off += err;
+               s -= err;
+               if (err != 64)
+                       break;
+       }
+       mutex_unlock(&data->mutex_flash);
+       return ret > 0 ? ret : err;
+}
+
+/*
+ * Notes:
+ * - concurrent writing is prevented by mutex and all writes must be
+ *   n*64 bytes and 64-byte aligned, each write being preceeded by an
+ *   ERASE which erases a 64byte block.
+ *   If less than requested was written or an error is returned for an
+ *   otherwise correct write request the next 64-byte block which should
+ *   have been written is in undefined state (mostly: original, erased,
+ *   (half-)written with write error)
+ * - reading can happend without special restriction
+ */
+static const struct file_operations picolcd_debug_flash_fops = {
+       .owner    = THIS_MODULE,
+       .open     = picolcd_debug_flash_open,
+       .read     = picolcd_debug_flash_read,
+       .write    = picolcd_debug_flash_write,
+       .llseek   = generic_file_llseek,
+};
+
+
+/*
  * Helper code for HID report level dumping/debugging
  */
 static const char *error_codes[] = {
@@ -1534,9 +2175,75 @@ static void picolcd_debug_raw_event(struct picolcd_data *data,
        wake_up_interruptible(&hdev->debug_wait);
        kfree(buff);
 }
+
+static 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)
+{
+       struct hid_device *hdev = data->hdev;
+
+       mutex_init(&data->mutex_flash);
+
+       /* reset */
+       if (reset)
+               data->debug_reset = debugfs_create_file("reset", 0600,
+                               hdev->debug_dir, data, &picolcd_debug_reset_fops);
+
+       /* eeprom */
+       if (eeprom_r || eeprom_w)
+               data->debug_eeprom = debugfs_create_file("eeprom",
+                       (eeprom_w ? S_IWUSR : 0) | (eeprom_r ? S_IRUSR : 0),
+                       hdev->debug_dir, data, &picolcd_debug_eeprom_fops);
+
+       /* flash */
+       if (flash_r && flash_r->maxfield == 1 && flash_r->field[0]->report_size == 8)
+               data->addr_sz = flash_r->field[0]->report_count - 1;
+       else
+               data->addr_sz = -1;
+       if (data->addr_sz == 2 || data->addr_sz == 3) {
+               data->debug_flash = debugfs_create_file("flash",
+                       (flash_w ? S_IWUSR : 0) | (flash_r ? S_IRUSR : 0),
+                       hdev->debug_dir, data, &picolcd_debug_flash_fops);
+       } else if (flash_r || flash_w)
+               dev_warn(&hdev->dev, "Unexpected FLASH access reports, "
+                               "please submit rdesc for review\n");
+}
+
+static void picolcd_exit_devfs(struct picolcd_data *data)
+{
+       struct dentry *dent;
+
+       dent = data->debug_reset;
+       data->debug_reset = NULL;
+       if (dent)
+               debugfs_remove(dent);
+       dent = data->debug_eeprom;
+       data->debug_eeprom = NULL;
+       if (dent)
+               debugfs_remove(dent);
+       dent = data->debug_flash;
+       data->debug_flash = NULL;
+       if (dent)
+               debugfs_remove(dent);
+       mutex_destroy(&data->mutex_flash);
+}
 #else
-#define picolcd_debug_raw_event(data, hdev, report, raw_data, size)
-#endif
+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 */
 
 /*
  * Handle raw report as sent by device
@@ -1576,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)
@@ -1646,7 +2393,6 @@ static inline void picolcd_exit_cir(struct picolcd_data *data)
 
 static int picolcd_probe_lcd(struct hid_device *hdev, struct picolcd_data *data)
 {
-       struct hid_report *report;
        int error;
 
        error = picolcd_check_version(hdev);
@@ -1673,21 +2419,31 @@ static int picolcd_probe_lcd(struct hid_device *hdev, struct picolcd_data *data)
        if (error)
                goto err;
 
+       /* Setup lcd class device */
+       error = picolcd_init_lcd(data, picolcd_out_report(REPORT_CONTRAST, hdev));
+       if (error)
+               goto err;
+
        /* Setup backlight class device */
        error = picolcd_init_backlight(data, picolcd_out_report(REPORT_BRIGHTNESS, hdev));
        if (error)
                goto err;
 
-#ifdef CONFIG_DEBUG_FS
-       report = picolcd_out_report(REPORT_READ_MEMORY, hdev);
-       if (report && report->maxfield == 1 && report->field[0]->report_size == 8)
-               data->addr_sz = report->field[0]->report_count - 1;
-       else
-               data->addr_sz = -1;
-#endif
+       /* Setup the LED class devices */
+       error = picolcd_init_leds(data, picolcd_out_report(REPORT_LED_STATE, hdev));
+       if (error)
+               goto err;
+
+       picolcd_init_devfs(data, picolcd_out_report(REPORT_EE_READ, hdev),
+                       picolcd_out_report(REPORT_EE_WRITE, hdev),
+                       picolcd_out_report(REPORT_READ_MEMORY, hdev),
+                       picolcd_out_report(REPORT_WRITE_MEMORY, hdev),
+                       picolcd_out_report(REPORT_RESET, hdev));
        return 0;
 err:
+       picolcd_exit_leds(data);
        picolcd_exit_backlight(data);
+       picolcd_exit_lcd(data);
        picolcd_exit_framebuffer(data);
        picolcd_exit_cir(data);
        picolcd_exit_keys(data);
@@ -1696,7 +2452,6 @@ err:
 
 static int picolcd_probe_bootloader(struct hid_device *hdev, struct picolcd_data *data)
 {
-       struct hid_report *report;
        int error;
 
        error = picolcd_check_version(hdev);
@@ -1708,13 +2463,9 @@ static int picolcd_probe_bootloader(struct hid_device *hdev, struct picolcd_data
                                "please submit /sys/kernel/debug/hid/%s/rdesc for this device.\n",
                                dev_name(&hdev->dev));
 
-#ifdef CONFIG_DEBUG_FS
-       report = picolcd_out_report(REPORT_BL_READ_MEMORY, hdev);
-       if (report && report->maxfield == 1 && report->field[0]->report_size == 8)
-               data->addr_sz = report->field[0]->report_count - 1;
-       else
-               data->addr_sz = -1;
-#endif
+       picolcd_init_devfs(data, NULL, NULL,
+                       picolcd_out_report(REPORT_BL_READ_MEMORY, hdev),
+                       picolcd_out_report(REPORT_BL_WRITE_MEMORY, hdev), NULL);
        return 0;
 }
 
@@ -1740,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);
@@ -1767,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:
@@ -1807,7 +2567,9 @@ static void picolcd_remove(struct hid_device *hdev)
        data->status |= PICOLCD_FAILED;
        spin_unlock_irqrestore(&data->lock, flags);
 
+       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);
@@ -1818,8 +2580,11 @@ static void picolcd_remove(struct hid_device *hdev)
                complete(&data->pending->ready);
        spin_unlock_irqrestore(&data->lock, flags);
 
+       /* Cleanup LED */
+       picolcd_exit_leds(data);
        /* Clean up the framebuffer */
        picolcd_exit_backlight(data);
+       picolcd_exit_lcd(data);
        picolcd_exit_framebuffer(data);
        /* Cleanup input */
        picolcd_exit_cir(data);
@@ -1843,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)