Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Asus OLED USB driver |
| 3 | * |
| 4 | * Copyright (C) 2007,2008 Jakub Schmidtke (sjakub@gmail.com) |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., |
| 19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * |
| 22 | * |
| 23 | * This module is based on usbled and asus-laptop modules. |
| 24 | * |
| 25 | * |
| 26 | * Asus OLED support is based on asusoled program taken from |
Justin P. Mattock | 631dd1a | 2010-10-18 11:03:14 +0200 | [diff] [blame] | 27 | * <http://lapsus.berlios.de/asus_oled.html>. |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 28 | * |
| 29 | * |
| 30 | */ |
| 31 | |
| 32 | #include <linux/kernel.h> |
| 33 | #include <linux/errno.h> |
| 34 | #include <linux/init.h> |
| 35 | #include <linux/slab.h> |
| 36 | #include <linux/module.h> |
| 37 | #include <linux/usb.h> |
| 38 | #include <linux/platform_device.h> |
| 39 | #include <linux/ctype.h> |
| 40 | |
| 41 | #define ASUS_OLED_VERSION "0.04-dev" |
| 42 | #define ASUS_OLED_NAME "asus-oled" |
| 43 | #define ASUS_OLED_UNDERSCORE_NAME "asus_oled" |
| 44 | |
| 45 | #define ASUS_OLED_ERROR "Asus OLED Display Error: " |
| 46 | |
| 47 | #define ASUS_OLED_STATIC 's' |
| 48 | #define ASUS_OLED_ROLL 'r' |
| 49 | #define ASUS_OLED_FLASH 'f' |
| 50 | |
| 51 | #define ASUS_OLED_MAX_WIDTH 1792 |
| 52 | #define ASUS_OLED_DISP_HEIGHT 32 |
| 53 | #define ASUS_OLED_PACKET_BUF_SIZE 256 |
| 54 | |
Peter Huewe | e89230e | 2009-12-15 05:04:22 +0100 | [diff] [blame] | 55 | #define USB_VENDOR_ID_ASUS 0x0b05 |
| 56 | #define USB_DEVICE_ID_ASUS_LCM 0x1726 |
| 57 | #define USB_DEVICE_ID_ASUS_LCM2 0x175b |
| 58 | |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 59 | MODULE_AUTHOR("Jakub Schmidtke, sjakub@gmail.com"); |
| 60 | MODULE_DESCRIPTION("Asus OLED Driver v" ASUS_OLED_VERSION); |
| 61 | MODULE_LICENSE("GPL"); |
| 62 | |
Andre Haupt | b0e5ca8 | 2009-01-19 12:00:17 +0100 | [diff] [blame] | 63 | static struct class *oled_class; |
| 64 | static int oled_num; |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 65 | |
Andre Haupt | b0e5ca8 | 2009-01-19 12:00:17 +0100 | [diff] [blame] | 66 | static uint start_off; |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 67 | |
| 68 | module_param(start_off, uint, 0644); |
| 69 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 70 | MODULE_PARM_DESC(start_off, |
| 71 | "Set to 1 to switch off OLED display after it is attached"); |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 72 | |
Johan Meiring | 9f027eb | 2010-11-06 18:24:46 +0200 | [diff] [blame] | 73 | enum oled_pack_mode { |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 74 | PACK_MODE_G1, |
| 75 | PACK_MODE_G50, |
| 76 | PACK_MODE_LAST |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 77 | }; |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 78 | |
| 79 | struct oled_dev_desc_str { |
| 80 | uint16_t idVendor; |
| 81 | uint16_t idProduct; |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 82 | /* width of display */ |
| 83 | uint16_t devWidth; |
| 84 | /* formula to be used while packing the picture */ |
| 85 | enum oled_pack_mode packMode; |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 86 | const char *devDesc; |
| 87 | }; |
| 88 | |
| 89 | /* table of devices that work with this driver */ |
Németh Márton | a457732 | 2010-01-10 00:18:34 +0100 | [diff] [blame] | 90 | static const struct usb_device_id id_table[] = { |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 91 | /* Asus G1/G2 (and variants)*/ |
Peter Huewe | e89230e | 2009-12-15 05:04:22 +0100 | [diff] [blame] | 92 | { USB_DEVICE(USB_VENDOR_ID_ASUS, USB_DEVICE_ID_ASUS_LCM) }, |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 93 | /* Asus G50V (and possibly others - G70? G71?)*/ |
Peter Huewe | e89230e | 2009-12-15 05:04:22 +0100 | [diff] [blame] | 94 | { USB_DEVICE(USB_VENDOR_ID_ASUS, USB_DEVICE_ID_ASUS_LCM2) }, |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 95 | { }, |
| 96 | }; |
| 97 | |
| 98 | /* parameters of specific devices */ |
Andre Haupt | 118015c | 2009-01-19 12:00:19 +0100 | [diff] [blame] | 99 | static struct oled_dev_desc_str oled_dev_desc_table[] = { |
Peter Huewe | e89230e | 2009-12-15 05:04:22 +0100 | [diff] [blame] | 100 | { USB_VENDOR_ID_ASUS, USB_DEVICE_ID_ASUS_LCM, 128, PACK_MODE_G1, |
| 101 | "G1/G2" }, |
| 102 | { USB_VENDOR_ID_ASUS, USB_DEVICE_ID_ASUS_LCM2, 256, PACK_MODE_G50, |
| 103 | "G50" }, |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 104 | { }, |
| 105 | }; |
| 106 | |
Andre Haupt | 118015c | 2009-01-19 12:00:19 +0100 | [diff] [blame] | 107 | MODULE_DEVICE_TABLE(usb, id_table); |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 108 | |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 109 | struct asus_oled_header { |
| 110 | uint8_t magic1; |
| 111 | uint8_t magic2; |
| 112 | uint8_t flags; |
| 113 | uint8_t value3; |
| 114 | uint8_t buffer1; |
| 115 | uint8_t buffer2; |
| 116 | uint8_t value6; |
| 117 | uint8_t value7; |
| 118 | uint8_t value8; |
| 119 | uint8_t padding2[7]; |
| 120 | } __attribute((packed)); |
| 121 | |
| 122 | struct asus_oled_packet { |
| 123 | struct asus_oled_header header; |
| 124 | uint8_t bitmap[ASUS_OLED_PACKET_BUF_SIZE]; |
| 125 | } __attribute((packed)); |
| 126 | |
| 127 | struct asus_oled_dev { |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 128 | struct usb_device *udev; |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 129 | uint8_t pic_mode; |
| 130 | uint16_t dev_width; |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 131 | enum oled_pack_mode pack_mode; |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 132 | size_t height; |
| 133 | size_t width; |
| 134 | size_t x_shift; |
| 135 | size_t y_shift; |
| 136 | size_t buf_offs; |
| 137 | uint8_t last_val; |
| 138 | size_t buf_size; |
| 139 | char *buf; |
| 140 | uint8_t enabled; |
| 141 | struct device *dev; |
| 142 | }; |
| 143 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 144 | static void setup_packet_header(struct asus_oled_packet *packet, char flags, |
| 145 | char value3, char buffer1, char buffer2, char value6, |
| 146 | char value7, char value8) |
| 147 | { |
| 148 | memset(packet, 0, sizeof(struct asus_oled_header)); |
| 149 | packet->header.magic1 = 0x55; |
| 150 | packet->header.magic2 = 0xaa; |
| 151 | packet->header.flags = flags; |
| 152 | packet->header.value3 = value3; |
| 153 | packet->header.buffer1 = buffer1; |
| 154 | packet->header.buffer2 = buffer2; |
| 155 | packet->header.value6 = value6; |
| 156 | packet->header.value7 = value7; |
| 157 | packet->header.value8 = value8; |
| 158 | } |
| 159 | |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 160 | static void enable_oled(struct asus_oled_dev *odev, uint8_t enabl) |
| 161 | { |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 162 | int retval; |
| 163 | int act_len; |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 164 | struct asus_oled_packet *packet; |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 165 | |
| 166 | packet = kzalloc(sizeof(struct asus_oled_packet), GFP_KERNEL); |
| 167 | |
| 168 | if (!packet) { |
| 169 | dev_err(&odev->udev->dev, "out of memory\n"); |
| 170 | return; |
| 171 | } |
| 172 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 173 | setup_packet_header(packet, 0x20, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00); |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 174 | |
Andre Haupt | 526fd20a | 2009-01-19 12:00:18 +0100 | [diff] [blame] | 175 | if (enabl) |
| 176 | packet->bitmap[0] = 0xaf; |
| 177 | else |
| 178 | packet->bitmap[0] = 0xae; |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 179 | |
Peter Huewe | 4804918 | 2012-02-25 01:19:41 +0100 | [diff] [blame^] | 180 | retval = usb_bulk_msg(odev->udev, |
| 181 | usb_sndbulkpipe(odev->udev, 2), |
| 182 | packet, |
| 183 | sizeof(struct asus_oled_header) + 1, |
| 184 | &act_len, |
| 185 | -1); |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 186 | |
Peter Huewe | 4804918 | 2012-02-25 01:19:41 +0100 | [diff] [blame^] | 187 | if (retval) |
| 188 | dev_dbg(&odev->udev->dev, "retval = %d\n", retval); |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 189 | |
| 190 | odev->enabled = enabl; |
| 191 | |
| 192 | kfree(packet); |
| 193 | } |
| 194 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 195 | static ssize_t set_enabled(struct device *dev, struct device_attribute *attr, |
| 196 | const char *buf, size_t count) |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 197 | { |
| 198 | struct usb_interface *intf = to_usb_interface(dev); |
| 199 | struct asus_oled_dev *odev = usb_get_intfdata(intf); |
Eugeni Dodonov | 20633bf | 2009-12-23 10:27:22 -0200 | [diff] [blame] | 200 | unsigned long value; |
Ken O'Brien | 67d6baa | 2011-11-19 01:18:12 +0000 | [diff] [blame] | 201 | if (kstrtoul(buf, 10, &value)) |
Eugeni Dodonov | 20633bf | 2009-12-23 10:27:22 -0200 | [diff] [blame] | 202 | return -EINVAL; |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 203 | |
Eugeni Dodonov | 20633bf | 2009-12-23 10:27:22 -0200 | [diff] [blame] | 204 | enable_oled(odev, value); |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 205 | |
| 206 | return count; |
| 207 | } |
| 208 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 209 | static ssize_t class_set_enabled(struct device *device, |
| 210 | struct device_attribute *attr, |
| 211 | const char *buf, size_t count) |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 212 | { |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 213 | struct asus_oled_dev *odev = |
| 214 | (struct asus_oled_dev *) dev_get_drvdata(device); |
Eugeni Dodonov | 20633bf | 2009-12-23 10:27:22 -0200 | [diff] [blame] | 215 | unsigned long value; |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 216 | |
Ken O'Brien | 67d6baa | 2011-11-19 01:18:12 +0000 | [diff] [blame] | 217 | if (kstrtoul(buf, 10, &value)) |
Eugeni Dodonov | 20633bf | 2009-12-23 10:27:22 -0200 | [diff] [blame] | 218 | return -EINVAL; |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 219 | |
Eugeni Dodonov | 20633bf | 2009-12-23 10:27:22 -0200 | [diff] [blame] | 220 | enable_oled(odev, value); |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 221 | |
| 222 | return count; |
| 223 | } |
| 224 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 225 | static ssize_t get_enabled(struct device *dev, struct device_attribute *attr, |
| 226 | char *buf) |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 227 | { |
| 228 | struct usb_interface *intf = to_usb_interface(dev); |
| 229 | struct asus_oled_dev *odev = usb_get_intfdata(intf); |
| 230 | |
| 231 | return sprintf(buf, "%d\n", odev->enabled); |
| 232 | } |
| 233 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 234 | static ssize_t class_get_enabled(struct device *device, |
| 235 | struct device_attribute *attr, char *buf) |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 236 | { |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 237 | struct asus_oled_dev *odev = |
| 238 | (struct asus_oled_dev *) dev_get_drvdata(device); |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 239 | |
| 240 | return sprintf(buf, "%d\n", odev->enabled); |
| 241 | } |
| 242 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 243 | static void send_packets(struct usb_device *udev, |
| 244 | struct asus_oled_packet *packet, |
| 245 | char *buf, uint8_t p_type, size_t p_num) |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 246 | { |
| 247 | size_t i; |
| 248 | int act_len; |
| 249 | |
| 250 | for (i = 0; i < p_num; i++) { |
| 251 | int retval; |
| 252 | |
| 253 | switch (p_type) { |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 254 | case ASUS_OLED_ROLL: |
| 255 | setup_packet_header(packet, 0x40, 0x80, p_num, |
| 256 | i + 1, 0x00, 0x01, 0xff); |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 257 | break; |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 258 | case ASUS_OLED_STATIC: |
| 259 | setup_packet_header(packet, 0x10 + i, 0x80, 0x01, |
| 260 | 0x01, 0x00, 0x01, 0x00); |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 261 | break; |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 262 | case ASUS_OLED_FLASH: |
| 263 | setup_packet_header(packet, 0x10 + i, 0x80, 0x01, |
| 264 | 0x01, 0x00, 0x00, 0xff); |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 265 | break; |
| 266 | } |
| 267 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 268 | memcpy(packet->bitmap, buf + (ASUS_OLED_PACKET_BUF_SIZE*i), |
| 269 | ASUS_OLED_PACKET_BUF_SIZE); |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 270 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 271 | retval = usb_bulk_msg(udev, usb_sndctrlpipe(udev, 2), |
| 272 | packet, sizeof(struct asus_oled_packet), |
| 273 | &act_len, -1); |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 274 | |
| 275 | if (retval) |
| 276 | dev_dbg(&udev->dev, "retval = %d\n", retval); |
| 277 | } |
| 278 | } |
| 279 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 280 | static void send_packet(struct usb_device *udev, |
| 281 | struct asus_oled_packet *packet, |
| 282 | size_t offset, size_t len, char *buf, uint8_t b1, |
| 283 | uint8_t b2, uint8_t b3, uint8_t b4, uint8_t b5, |
| 284 | uint8_t b6) { |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 285 | int retval; |
| 286 | int act_len; |
| 287 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 288 | setup_packet_header(packet, b1, b2, b3, b4, b5, b6, 0x00); |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 289 | memcpy(packet->bitmap, buf + offset, len); |
| 290 | |
| 291 | retval = usb_bulk_msg(udev, |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 292 | usb_sndctrlpipe(udev, 2), |
| 293 | packet, |
| 294 | sizeof(struct asus_oled_packet), |
| 295 | &act_len, |
| 296 | -1); |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 297 | |
| 298 | if (retval) |
| 299 | dev_dbg(&udev->dev, "retval = %d\n", retval); |
| 300 | } |
| 301 | |
| 302 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 303 | static void send_packets_g50(struct usb_device *udev, |
| 304 | struct asus_oled_packet *packet, char *buf) |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 305 | { |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 306 | send_packet(udev, packet, 0, 0x100, buf, |
| 307 | 0x10, 0x00, 0x02, 0x01, 0x00, 0x01); |
| 308 | send_packet(udev, packet, 0x100, 0x080, buf, |
| 309 | 0x10, 0x00, 0x02, 0x02, 0x80, 0x00); |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 310 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 311 | send_packet(udev, packet, 0x180, 0x100, buf, |
| 312 | 0x11, 0x00, 0x03, 0x01, 0x00, 0x01); |
| 313 | send_packet(udev, packet, 0x280, 0x100, buf, |
| 314 | 0x11, 0x00, 0x03, 0x02, 0x00, 0x01); |
| 315 | send_packet(udev, packet, 0x380, 0x080, buf, |
| 316 | 0x11, 0x00, 0x03, 0x03, 0x80, 0x00); |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | |
| 320 | static void send_data(struct asus_oled_dev *odev) |
| 321 | { |
| 322 | size_t packet_num = odev->buf_size / ASUS_OLED_PACKET_BUF_SIZE; |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 323 | struct asus_oled_packet *packet; |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 324 | |
| 325 | packet = kzalloc(sizeof(struct asus_oled_packet), GFP_KERNEL); |
| 326 | |
| 327 | if (!packet) { |
| 328 | dev_err(&odev->udev->dev, "out of memory\n"); |
| 329 | return; |
| 330 | } |
| 331 | |
Andre Haupt | 118015c | 2009-01-19 12:00:19 +0100 | [diff] [blame] | 332 | if (odev->pack_mode == PACK_MODE_G1) { |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 333 | /* When sending roll-mode data the display updated only |
| 334 | first packet. I have no idea why, but when static picture |
| 335 | is sent just before rolling picture everything works fine. */ |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 336 | if (odev->pic_mode == ASUS_OLED_ROLL) |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 337 | send_packets(odev->udev, packet, odev->buf, |
| 338 | ASUS_OLED_STATIC, 2); |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 339 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 340 | /* Only ROLL mode can use more than 2 packets.*/ |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 341 | if (odev->pic_mode != ASUS_OLED_ROLL && packet_num > 2) |
| 342 | packet_num = 2; |
| 343 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 344 | send_packets(odev->udev, packet, odev->buf, |
| 345 | odev->pic_mode, packet_num); |
| 346 | } else if (odev->pack_mode == PACK_MODE_G50) { |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 347 | send_packets_g50(odev->udev, packet, odev->buf); |
| 348 | } |
| 349 | |
| 350 | kfree(packet); |
| 351 | } |
| 352 | |
| 353 | static int append_values(struct asus_oled_dev *odev, uint8_t val, size_t count) |
| 354 | { |
Pekka Paalanen | 635032c | 2012-01-22 16:33:46 +0200 | [diff] [blame] | 355 | odev->last_val = val; |
| 356 | |
| 357 | if (val == 0) { |
| 358 | odev->buf_offs += count; |
| 359 | return 0; |
| 360 | } |
| 361 | |
| 362 | while (count-- > 0) { |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 363 | size_t x = odev->buf_offs % odev->width; |
| 364 | size_t y = odev->buf_offs / odev->width; |
| 365 | size_t i; |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 366 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 367 | x += odev->x_shift; |
| 368 | y += odev->y_shift; |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 369 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 370 | switch (odev->pack_mode) { |
| 371 | case PACK_MODE_G1: |
| 372 | /* i = (x/128)*640 + 127 - x + (y/8)*128; |
| 373 | This one for 128 is the same, but might be better |
| 374 | for different widths? */ |
| 375 | i = (x/odev->dev_width)*640 + |
| 376 | odev->dev_width - 1 - x + |
| 377 | (y/8)*odev->dev_width; |
| 378 | break; |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 379 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 380 | case PACK_MODE_G50: |
| 381 | i = (odev->dev_width - 1 - x)/8 + y*odev->dev_width/8; |
| 382 | break; |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 383 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 384 | default: |
| 385 | i = 0; |
| 386 | printk(ASUS_OLED_ERROR "Unknown OLED Pack Mode: %d!\n", |
| 387 | odev->pack_mode); |
| 388 | break; |
| 389 | } |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 390 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 391 | if (i >= odev->buf_size) { |
| 392 | printk(ASUS_OLED_ERROR "Buffer overflow! Report a bug:" |
| 393 | "offs: %d >= %d i: %d (x: %d y: %d)\n", |
| 394 | (int) odev->buf_offs, (int) odev->buf_size, |
| 395 | (int) i, (int) x, (int) y); |
| 396 | return -EIO; |
| 397 | } |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 398 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 399 | switch (odev->pack_mode) { |
| 400 | case PACK_MODE_G1: |
| 401 | odev->buf[i] &= ~(1<<(y%8)); |
| 402 | break; |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 403 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 404 | case PACK_MODE_G50: |
| 405 | odev->buf[i] &= ~(1<<(x%8)); |
| 406 | break; |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 407 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 408 | default: |
| 409 | /* cannot get here; stops gcc complaining*/ |
| 410 | ; |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 411 | } |
| 412 | |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 413 | odev->buf_offs++; |
| 414 | } |
| 415 | |
| 416 | return 0; |
| 417 | } |
| 418 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 419 | static ssize_t odev_set_picture(struct asus_oled_dev *odev, |
| 420 | const char *buf, size_t count) |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 421 | { |
| 422 | size_t offs = 0, max_offs; |
| 423 | |
Andre Haupt | 526fd20a | 2009-01-19 12:00:18 +0100 | [diff] [blame] | 424 | if (count < 1) |
| 425 | return 0; |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 426 | |
Andre Haupt | 118015c | 2009-01-19 12:00:19 +0100 | [diff] [blame] | 427 | if (tolower(buf[0]) == 'b') { |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 428 | /* binary mode, set the entire memory*/ |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 429 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 430 | size_t i; |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 431 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 432 | odev->buf_size = (odev->dev_width * ASUS_OLED_DISP_HEIGHT) / 8; |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 433 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 434 | kfree(odev->buf); |
| 435 | odev->buf = kmalloc(odev->buf_size, GFP_KERNEL); |
Peter Huewe | 0df28be | 2010-01-07 19:57:36 +0100 | [diff] [blame] | 436 | if (odev->buf == NULL) { |
| 437 | odev->buf_size = 0; |
| 438 | printk(ASUS_OLED_ERROR "Out of memory!\n"); |
| 439 | return -ENOMEM; |
| 440 | } |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 441 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 442 | memset(odev->buf, 0xff, odev->buf_size); |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 443 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 444 | for (i = 1; i < count && i <= 32 * 32; i++) { |
| 445 | odev->buf[i-1] = buf[i]; |
| 446 | odev->buf_offs = i-1; |
| 447 | } |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 448 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 449 | odev->width = odev->dev_width / 8; |
| 450 | odev->height = ASUS_OLED_DISP_HEIGHT; |
| 451 | odev->x_shift = 0; |
| 452 | odev->y_shift = 0; |
| 453 | odev->last_val = 0; |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 454 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 455 | send_data(odev); |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 456 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 457 | return count; |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | if (buf[0] == '<') { |
| 461 | size_t i; |
| 462 | size_t w = 0, h = 0; |
| 463 | size_t w_mem, h_mem; |
| 464 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 465 | if (count < 10 || buf[2] != ':') |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 466 | goto error_header; |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 467 | |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 468 | |
Andre Haupt | 118015c | 2009-01-19 12:00:19 +0100 | [diff] [blame] | 469 | switch (tolower(buf[1])) { |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 470 | case ASUS_OLED_STATIC: |
| 471 | case ASUS_OLED_ROLL: |
| 472 | case ASUS_OLED_FLASH: |
| 473 | odev->pic_mode = buf[1]; |
| 474 | break; |
| 475 | default: |
| 476 | printk(ASUS_OLED_ERROR "Wrong picture mode: '%c'.\n", |
| 477 | buf[1]); |
| 478 | return -EIO; |
| 479 | break; |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | for (i = 3; i < count; ++i) { |
| 483 | if (buf[i] >= '0' && buf[i] <= '9') { |
| 484 | w = 10*w + (buf[i] - '0'); |
| 485 | |
Andre Haupt | 526fd20a | 2009-01-19 12:00:18 +0100 | [diff] [blame] | 486 | if (w > ASUS_OLED_MAX_WIDTH) |
| 487 | goto error_width; |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 488 | } else if (tolower(buf[i]) == 'x') { |
Andre Haupt | 526fd20a | 2009-01-19 12:00:18 +0100 | [diff] [blame] | 489 | break; |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 490 | } else { |
Andre Haupt | 526fd20a | 2009-01-19 12:00:18 +0100 | [diff] [blame] | 491 | goto error_width; |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 492 | } |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | for (++i; i < count; ++i) { |
| 496 | if (buf[i] >= '0' && buf[i] <= '9') { |
| 497 | h = 10*h + (buf[i] - '0'); |
| 498 | |
Andre Haupt | 526fd20a | 2009-01-19 12:00:18 +0100 | [diff] [blame] | 499 | if (h > ASUS_OLED_DISP_HEIGHT) |
| 500 | goto error_height; |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 501 | } else if (tolower(buf[i]) == '>') { |
Andre Haupt | 526fd20a | 2009-01-19 12:00:18 +0100 | [diff] [blame] | 502 | break; |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 503 | } else { |
Andre Haupt | 526fd20a | 2009-01-19 12:00:18 +0100 | [diff] [blame] | 504 | goto error_height; |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 505 | } |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 506 | } |
| 507 | |
Andre Haupt | 526fd20a | 2009-01-19 12:00:18 +0100 | [diff] [blame] | 508 | if (w < 1 || w > ASUS_OLED_MAX_WIDTH) |
| 509 | goto error_width; |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 510 | |
Andre Haupt | 526fd20a | 2009-01-19 12:00:18 +0100 | [diff] [blame] | 511 | if (h < 1 || h > ASUS_OLED_DISP_HEIGHT) |
| 512 | goto error_height; |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 513 | |
Andre Haupt | 526fd20a | 2009-01-19 12:00:18 +0100 | [diff] [blame] | 514 | if (i >= count || buf[i] != '>') |
| 515 | goto error_header; |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 516 | |
| 517 | offs = i+1; |
| 518 | |
| 519 | if (w % (odev->dev_width) != 0) |
| 520 | w_mem = (w/(odev->dev_width) + 1)*(odev->dev_width); |
| 521 | else |
| 522 | w_mem = w; |
| 523 | |
| 524 | if (h < ASUS_OLED_DISP_HEIGHT) |
| 525 | h_mem = ASUS_OLED_DISP_HEIGHT; |
| 526 | else |
| 527 | h_mem = h; |
| 528 | |
| 529 | odev->buf_size = w_mem * h_mem / 8; |
| 530 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 531 | kfree(odev->buf); |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 532 | odev->buf = kmalloc(odev->buf_size, GFP_KERNEL); |
| 533 | |
| 534 | if (odev->buf == NULL) { |
| 535 | odev->buf_size = 0; |
| 536 | printk(ASUS_OLED_ERROR "Out of memory!\n"); |
| 537 | return -ENOMEM; |
| 538 | } |
| 539 | |
| 540 | memset(odev->buf, 0xff, odev->buf_size); |
| 541 | |
| 542 | odev->buf_offs = 0; |
| 543 | odev->width = w; |
| 544 | odev->height = h; |
| 545 | odev->x_shift = 0; |
| 546 | odev->y_shift = 0; |
| 547 | odev->last_val = 0; |
| 548 | |
| 549 | if (odev->pic_mode == ASUS_OLED_FLASH) { |
| 550 | if (h < ASUS_OLED_DISP_HEIGHT/2) |
| 551 | odev->y_shift = (ASUS_OLED_DISP_HEIGHT/2 - h)/2; |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 552 | } else { |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 553 | if (h < ASUS_OLED_DISP_HEIGHT) |
| 554 | odev->y_shift = (ASUS_OLED_DISP_HEIGHT - h)/2; |
| 555 | } |
| 556 | |
| 557 | if (w < (odev->dev_width)) |
| 558 | odev->x_shift = ((odev->dev_width) - w)/2; |
| 559 | } |
| 560 | |
| 561 | max_offs = odev->width * odev->height; |
| 562 | |
| 563 | while (offs < count && odev->buf_offs < max_offs) { |
Greg Kroah-Hartman | 0d99b6e | 2009-06-04 11:29:54 -0700 | [diff] [blame] | 564 | int ret = 0; |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 565 | |
| 566 | if (buf[offs] == '1' || buf[offs] == '#') { |
Andre Haupt | 94aa5b4 | 2009-01-19 12:00:20 +0100 | [diff] [blame] | 567 | ret = append_values(odev, 1, 1); |
| 568 | if (ret < 0) |
Andre Haupt | 526fd20a | 2009-01-19 12:00:18 +0100 | [diff] [blame] | 569 | return ret; |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 570 | } else if (buf[offs] == '0' || buf[offs] == ' ') { |
Andre Haupt | 94aa5b4 | 2009-01-19 12:00:20 +0100 | [diff] [blame] | 571 | ret = append_values(odev, 0, 1); |
| 572 | if (ret < 0) |
Andre Haupt | 526fd20a | 2009-01-19 12:00:18 +0100 | [diff] [blame] | 573 | return ret; |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 574 | } else if (buf[offs] == '\n') { |
| 575 | /* New line detected. Lets assume, that all characters |
| 576 | till the end of the line were equal to the last |
| 577 | character in this line.*/ |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 578 | if (odev->buf_offs % odev->width != 0) |
Andre Haupt | 94aa5b4 | 2009-01-19 12:00:20 +0100 | [diff] [blame] | 579 | ret = append_values(odev, odev->last_val, |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 580 | odev->width - |
| 581 | (odev->buf_offs % |
| 582 | odev->width)); |
| 583 | if (ret < 0) |
| 584 | return ret; |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | offs++; |
| 588 | } |
| 589 | |
Andre Haupt | 526fd20a | 2009-01-19 12:00:18 +0100 | [diff] [blame] | 590 | if (odev->buf_offs >= max_offs) |
| 591 | send_data(odev); |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 592 | |
| 593 | return count; |
| 594 | |
| 595 | error_width: |
| 596 | printk(ASUS_OLED_ERROR "Wrong picture width specified.\n"); |
| 597 | return -EIO; |
| 598 | |
| 599 | error_height: |
| 600 | printk(ASUS_OLED_ERROR "Wrong picture height specified.\n"); |
| 601 | return -EIO; |
| 602 | |
| 603 | error_header: |
| 604 | printk(ASUS_OLED_ERROR "Wrong picture header.\n"); |
| 605 | return -EIO; |
| 606 | } |
| 607 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 608 | static ssize_t set_picture(struct device *dev, struct device_attribute *attr, |
| 609 | const char *buf, size_t count) |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 610 | { |
| 611 | struct usb_interface *intf = to_usb_interface(dev); |
| 612 | |
| 613 | return odev_set_picture(usb_get_intfdata(intf), buf, count); |
| 614 | } |
| 615 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 616 | static ssize_t class_set_picture(struct device *device, |
| 617 | struct device_attribute *attr, |
| 618 | const char *buf, size_t count) |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 619 | { |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 620 | return odev_set_picture((struct asus_oled_dev *) |
| 621 | dev_get_drvdata(device), buf, count); |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | #define ASUS_OLED_DEVICE_ATTR(_file) dev_attr_asus_oled_##_file |
| 625 | |
Greg Kroah-Hartman | 515b498 | 2010-11-18 11:21:04 -0800 | [diff] [blame] | 626 | static DEVICE_ATTR(asus_oled_enabled, S_IWUSR | S_IRUGO, |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 627 | get_enabled, set_enabled); |
Greg Kroah-Hartman | 515b498 | 2010-11-18 11:21:04 -0800 | [diff] [blame] | 628 | static DEVICE_ATTR(asus_oled_picture, S_IWUSR , NULL, set_picture); |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 629 | |
Greg Kroah-Hartman | 515b498 | 2010-11-18 11:21:04 -0800 | [diff] [blame] | 630 | static DEVICE_ATTR(enabled, S_IWUSR | S_IRUGO, |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 631 | class_get_enabled, class_set_enabled); |
Greg Kroah-Hartman | 515b498 | 2010-11-18 11:21:04 -0800 | [diff] [blame] | 632 | static DEVICE_ATTR(picture, S_IWUSR, NULL, class_set_picture); |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 633 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 634 | static int asus_oled_probe(struct usb_interface *interface, |
| 635 | const struct usb_device_id *id) |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 636 | { |
| 637 | struct usb_device *udev = interface_to_usbdev(interface); |
| 638 | struct asus_oled_dev *odev = NULL; |
| 639 | int retval = -ENOMEM; |
| 640 | uint16_t dev_width = 0; |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 641 | enum oled_pack_mode pack_mode = PACK_MODE_LAST; |
| 642 | const struct oled_dev_desc_str *dev_desc = oled_dev_desc_table; |
Andre Haupt | ccdb245 | 2009-01-19 12:00:16 +0100 | [diff] [blame] | 643 | const char *desc = NULL; |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 644 | |
Andre Haupt | ccdb245 | 2009-01-19 12:00:16 +0100 | [diff] [blame] | 645 | if (!id) { |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 646 | /* Even possible? Just to make sure...*/ |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 647 | dev_err(&interface->dev, "No usb_device_id provided!\n"); |
| 648 | return -ENODEV; |
| 649 | } |
| 650 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 651 | for (; dev_desc->idVendor; dev_desc++) { |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 652 | if (dev_desc->idVendor == id->idVendor |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 653 | && dev_desc->idProduct == id->idProduct) { |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 654 | dev_width = dev_desc->devWidth; |
| 655 | desc = dev_desc->devDesc; |
| 656 | pack_mode = dev_desc->packMode; |
| 657 | break; |
| 658 | } |
| 659 | } |
| 660 | |
Andre Haupt | 118015c | 2009-01-19 12:00:19 +0100 | [diff] [blame] | 661 | if (!desc || dev_width < 1 || pack_mode == PACK_MODE_LAST) { |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 662 | dev_err(&interface->dev, |
| 663 | "Missing or incomplete device description!\n"); |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 664 | return -ENODEV; |
| 665 | } |
| 666 | |
| 667 | odev = kzalloc(sizeof(struct asus_oled_dev), GFP_KERNEL); |
| 668 | |
| 669 | if (odev == NULL) { |
| 670 | dev_err(&interface->dev, "Out of memory\n"); |
| 671 | return -ENOMEM; |
| 672 | } |
| 673 | |
| 674 | odev->udev = usb_get_dev(udev); |
| 675 | odev->pic_mode = ASUS_OLED_STATIC; |
| 676 | odev->dev_width = dev_width; |
| 677 | odev->pack_mode = pack_mode; |
| 678 | odev->height = 0; |
| 679 | odev->width = 0; |
| 680 | odev->x_shift = 0; |
| 681 | odev->y_shift = 0; |
| 682 | odev->buf_offs = 0; |
| 683 | odev->buf_size = 0; |
| 684 | odev->last_val = 0; |
| 685 | odev->buf = NULL; |
| 686 | odev->enabled = 1; |
Andre Haupt | ccdb245 | 2009-01-19 12:00:16 +0100 | [diff] [blame] | 687 | odev->dev = NULL; |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 688 | |
Andre Haupt | 118015c | 2009-01-19 12:00:19 +0100 | [diff] [blame] | 689 | usb_set_intfdata(interface, odev); |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 690 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 691 | retval = device_create_file(&interface->dev, |
| 692 | &ASUS_OLED_DEVICE_ATTR(enabled)); |
Andre Haupt | 94aa5b4 | 2009-01-19 12:00:20 +0100 | [diff] [blame] | 693 | if (retval) |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 694 | goto err_files; |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 695 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 696 | retval = device_create_file(&interface->dev, |
| 697 | &ASUS_OLED_DEVICE_ATTR(picture)); |
Andre Haupt | 94aa5b4 | 2009-01-19 12:00:20 +0100 | [diff] [blame] | 698 | if (retval) |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 699 | goto err_files; |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 700 | |
Andre Haupt | 118015c | 2009-01-19 12:00:19 +0100 | [diff] [blame] | 701 | odev->dev = device_create(oled_class, &interface->dev, MKDEV(0, 0), |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 702 | NULL, "oled_%d", ++oled_num); |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 703 | |
| 704 | if (IS_ERR(odev->dev)) { |
| 705 | retval = PTR_ERR(odev->dev); |
| 706 | goto err_files; |
| 707 | } |
| 708 | |
| 709 | dev_set_drvdata(odev->dev, odev); |
| 710 | |
Andre Haupt | 94aa5b4 | 2009-01-19 12:00:20 +0100 | [diff] [blame] | 711 | retval = device_create_file(odev->dev, &dev_attr_enabled); |
| 712 | if (retval) |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 713 | goto err_class_enabled; |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 714 | |
Andre Haupt | 94aa5b4 | 2009-01-19 12:00:20 +0100 | [diff] [blame] | 715 | retval = device_create_file(odev->dev, &dev_attr_picture); |
| 716 | if (retval) |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 717 | goto err_class_picture; |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 718 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 719 | dev_info(&interface->dev, |
| 720 | "Attached Asus OLED device: %s [width %u, pack_mode %d]\n", |
| 721 | desc, odev->dev_width, odev->pack_mode); |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 722 | |
| 723 | if (start_off) |
| 724 | enable_oled(odev, 0); |
| 725 | |
| 726 | return 0; |
| 727 | |
| 728 | err_class_picture: |
| 729 | device_remove_file(odev->dev, &dev_attr_picture); |
| 730 | |
| 731 | err_class_enabled: |
| 732 | device_remove_file(odev->dev, &dev_attr_enabled); |
| 733 | device_unregister(odev->dev); |
| 734 | |
| 735 | err_files: |
| 736 | device_remove_file(&interface->dev, &ASUS_OLED_DEVICE_ATTR(enabled)); |
| 737 | device_remove_file(&interface->dev, &ASUS_OLED_DEVICE_ATTR(picture)); |
| 738 | |
Andre Haupt | 118015c | 2009-01-19 12:00:19 +0100 | [diff] [blame] | 739 | usb_set_intfdata(interface, NULL); |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 740 | usb_put_dev(odev->udev); |
| 741 | kfree(odev); |
| 742 | |
| 743 | return retval; |
| 744 | } |
| 745 | |
| 746 | static void asus_oled_disconnect(struct usb_interface *interface) |
| 747 | { |
| 748 | struct asus_oled_dev *odev; |
| 749 | |
Andre Haupt | 118015c | 2009-01-19 12:00:19 +0100 | [diff] [blame] | 750 | odev = usb_get_intfdata(interface); |
| 751 | usb_set_intfdata(interface, NULL); |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 752 | |
| 753 | device_remove_file(odev->dev, &dev_attr_picture); |
| 754 | device_remove_file(odev->dev, &dev_attr_enabled); |
| 755 | device_unregister(odev->dev); |
| 756 | |
Andre Haupt | 118015c | 2009-01-19 12:00:19 +0100 | [diff] [blame] | 757 | device_remove_file(&interface->dev, &ASUS_OLED_DEVICE_ATTR(picture)); |
| 758 | device_remove_file(&interface->dev, &ASUS_OLED_DEVICE_ATTR(enabled)); |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 759 | |
| 760 | usb_put_dev(odev->udev); |
| 761 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 762 | kfree(odev->buf); |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 763 | |
| 764 | kfree(odev); |
| 765 | |
| 766 | dev_info(&interface->dev, "Disconnected Asus OLED device\n"); |
| 767 | } |
| 768 | |
| 769 | static struct usb_driver oled_driver = { |
| 770 | .name = ASUS_OLED_NAME, |
| 771 | .probe = asus_oled_probe, |
| 772 | .disconnect = asus_oled_disconnect, |
| 773 | .id_table = id_table, |
| 774 | }; |
| 775 | |
Andi Kleen | 0933e2d | 2010-01-05 12:48:09 +0100 | [diff] [blame] | 776 | static CLASS_ATTR_STRING(version, S_IRUGO, |
Andrea Gelmini | e67fdbc | 2010-03-01 00:28:47 +0100 | [diff] [blame] | 777 | ASUS_OLED_UNDERSCORE_NAME " " ASUS_OLED_VERSION); |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 778 | |
| 779 | static int __init asus_oled_init(void) |
| 780 | { |
| 781 | int retval = 0; |
| 782 | oled_class = class_create(THIS_MODULE, ASUS_OLED_UNDERSCORE_NAME); |
| 783 | |
| 784 | if (IS_ERR(oled_class)) { |
| 785 | err("Error creating " ASUS_OLED_UNDERSCORE_NAME " class"); |
| 786 | return PTR_ERR(oled_class); |
| 787 | } |
| 788 | |
Andi Kleen | 0933e2d | 2010-01-05 12:48:09 +0100 | [diff] [blame] | 789 | retval = class_create_file(oled_class, &class_attr_version.attr); |
Andre Haupt | 94aa5b4 | 2009-01-19 12:00:20 +0100 | [diff] [blame] | 790 | if (retval) { |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 791 | err("Error creating class version file"); |
| 792 | goto error; |
| 793 | } |
| 794 | |
| 795 | retval = usb_register(&oled_driver); |
| 796 | |
| 797 | if (retval) { |
| 798 | err("usb_register failed. Error number %d", retval); |
| 799 | goto error; |
| 800 | } |
| 801 | |
| 802 | return retval; |
| 803 | |
| 804 | error: |
| 805 | class_destroy(oled_class); |
| 806 | return retval; |
| 807 | } |
| 808 | |
| 809 | static void __exit asus_oled_exit(void) |
| 810 | { |
Pekka Paalanen | 3589e74 | 2012-01-22 16:33:47 +0200 | [diff] [blame] | 811 | usb_deregister(&oled_driver); |
Andi Kleen | 0933e2d | 2010-01-05 12:48:09 +0100 | [diff] [blame] | 812 | class_remove_file(oled_class, &class_attr_version.attr); |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 813 | class_destroy(oled_class); |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 814 | } |
| 815 | |
Andre Haupt | 118015c | 2009-01-19 12:00:19 +0100 | [diff] [blame] | 816 | module_init(asus_oled_init); |
| 817 | module_exit(asus_oled_exit); |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 818 | |