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