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 | { |
| 162 | int a; |
| 163 | int retval; |
| 164 | int act_len; |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 165 | struct asus_oled_packet *packet; |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 166 | |
| 167 | packet = kzalloc(sizeof(struct asus_oled_packet), GFP_KERNEL); |
| 168 | |
| 169 | if (!packet) { |
| 170 | dev_err(&odev->udev->dev, "out of memory\n"); |
| 171 | return; |
| 172 | } |
| 173 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 174 | setup_packet_header(packet, 0x20, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00); |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 175 | |
Andre Haupt | 526fd20 | 2009-01-19 12:00:18 +0100 | [diff] [blame] | 176 | if (enabl) |
| 177 | packet->bitmap[0] = 0xaf; |
| 178 | else |
| 179 | packet->bitmap[0] = 0xae; |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 180 | |
Andre Haupt | 118015c | 2009-01-19 12:00:19 +0100 | [diff] [blame] | 181 | for (a = 0; a < 1; a++) { |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 182 | retval = usb_bulk_msg(odev->udev, |
| 183 | usb_sndbulkpipe(odev->udev, 2), |
| 184 | packet, |
| 185 | sizeof(struct asus_oled_header) + 1, |
| 186 | &act_len, |
| 187 | -1); |
| 188 | |
| 189 | if (retval) |
| 190 | dev_dbg(&odev->udev->dev, "retval = %d\n", retval); |
| 191 | } |
| 192 | |
| 193 | odev->enabled = enabl; |
| 194 | |
| 195 | kfree(packet); |
| 196 | } |
| 197 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 198 | static ssize_t set_enabled(struct device *dev, struct device_attribute *attr, |
| 199 | const char *buf, size_t count) |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 200 | { |
| 201 | struct usb_interface *intf = to_usb_interface(dev); |
| 202 | struct asus_oled_dev *odev = usb_get_intfdata(intf); |
Eugeni Dodonov | 20633bf | 2009-12-23 10:27:22 -0200 | [diff] [blame] | 203 | unsigned long value; |
Ken O'Brien | 67d6baa | 2011-11-19 01:18:12 +0000 | [diff] [blame] | 204 | if (kstrtoul(buf, 10, &value)) |
Eugeni Dodonov | 20633bf | 2009-12-23 10:27:22 -0200 | [diff] [blame] | 205 | return -EINVAL; |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 206 | |
Eugeni Dodonov | 20633bf | 2009-12-23 10:27:22 -0200 | [diff] [blame] | 207 | enable_oled(odev, value); |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 208 | |
| 209 | return count; |
| 210 | } |
| 211 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 212 | static ssize_t class_set_enabled(struct device *device, |
| 213 | struct device_attribute *attr, |
| 214 | const char *buf, size_t count) |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 215 | { |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 216 | struct asus_oled_dev *odev = |
| 217 | (struct asus_oled_dev *) dev_get_drvdata(device); |
Eugeni Dodonov | 20633bf | 2009-12-23 10:27:22 -0200 | [diff] [blame] | 218 | unsigned long value; |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 219 | |
Ken O'Brien | 67d6baa | 2011-11-19 01:18:12 +0000 | [diff] [blame] | 220 | if (kstrtoul(buf, 10, &value)) |
Eugeni Dodonov | 20633bf | 2009-12-23 10:27:22 -0200 | [diff] [blame] | 221 | return -EINVAL; |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 222 | |
Eugeni Dodonov | 20633bf | 2009-12-23 10:27:22 -0200 | [diff] [blame] | 223 | enable_oled(odev, value); |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 224 | |
| 225 | return count; |
| 226 | } |
| 227 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 228 | static ssize_t get_enabled(struct device *dev, struct device_attribute *attr, |
| 229 | char *buf) |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 230 | { |
| 231 | struct usb_interface *intf = to_usb_interface(dev); |
| 232 | struct asus_oled_dev *odev = usb_get_intfdata(intf); |
| 233 | |
| 234 | return sprintf(buf, "%d\n", odev->enabled); |
| 235 | } |
| 236 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 237 | static ssize_t class_get_enabled(struct device *device, |
| 238 | struct device_attribute *attr, char *buf) |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 239 | { |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 240 | struct asus_oled_dev *odev = |
| 241 | (struct asus_oled_dev *) dev_get_drvdata(device); |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 242 | |
| 243 | return sprintf(buf, "%d\n", odev->enabled); |
| 244 | } |
| 245 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 246 | static void send_packets(struct usb_device *udev, |
| 247 | struct asus_oled_packet *packet, |
| 248 | char *buf, uint8_t p_type, size_t p_num) |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 249 | { |
| 250 | size_t i; |
| 251 | int act_len; |
| 252 | |
| 253 | for (i = 0; i < p_num; i++) { |
| 254 | int retval; |
| 255 | |
| 256 | switch (p_type) { |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 257 | case ASUS_OLED_ROLL: |
| 258 | setup_packet_header(packet, 0x40, 0x80, p_num, |
| 259 | i + 1, 0x00, 0x01, 0xff); |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 260 | break; |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 261 | case ASUS_OLED_STATIC: |
| 262 | setup_packet_header(packet, 0x10 + i, 0x80, 0x01, |
| 263 | 0x01, 0x00, 0x01, 0x00); |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 264 | break; |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 265 | case ASUS_OLED_FLASH: |
| 266 | setup_packet_header(packet, 0x10 + i, 0x80, 0x01, |
| 267 | 0x01, 0x00, 0x00, 0xff); |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 268 | break; |
| 269 | } |
| 270 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 271 | memcpy(packet->bitmap, buf + (ASUS_OLED_PACKET_BUF_SIZE*i), |
| 272 | ASUS_OLED_PACKET_BUF_SIZE); |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 273 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 274 | retval = usb_bulk_msg(udev, usb_sndctrlpipe(udev, 2), |
| 275 | packet, sizeof(struct asus_oled_packet), |
| 276 | &act_len, -1); |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 277 | |
| 278 | if (retval) |
| 279 | dev_dbg(&udev->dev, "retval = %d\n", retval); |
| 280 | } |
| 281 | } |
| 282 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 283 | static void send_packet(struct usb_device *udev, |
| 284 | struct asus_oled_packet *packet, |
| 285 | size_t offset, size_t len, char *buf, uint8_t b1, |
| 286 | uint8_t b2, uint8_t b3, uint8_t b4, uint8_t b5, |
| 287 | uint8_t b6) { |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 288 | int retval; |
| 289 | int act_len; |
| 290 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 291 | setup_packet_header(packet, b1, b2, b3, b4, b5, b6, 0x00); |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 292 | memcpy(packet->bitmap, buf + offset, len); |
| 293 | |
| 294 | retval = usb_bulk_msg(udev, |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 295 | usb_sndctrlpipe(udev, 2), |
| 296 | packet, |
| 297 | sizeof(struct asus_oled_packet), |
| 298 | &act_len, |
| 299 | -1); |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 300 | |
| 301 | if (retval) |
| 302 | dev_dbg(&udev->dev, "retval = %d\n", retval); |
| 303 | } |
| 304 | |
| 305 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 306 | static void send_packets_g50(struct usb_device *udev, |
| 307 | struct asus_oled_packet *packet, char *buf) |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 308 | { |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 309 | send_packet(udev, packet, 0, 0x100, buf, |
| 310 | 0x10, 0x00, 0x02, 0x01, 0x00, 0x01); |
| 311 | send_packet(udev, packet, 0x100, 0x080, buf, |
| 312 | 0x10, 0x00, 0x02, 0x02, 0x80, 0x00); |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 313 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 314 | send_packet(udev, packet, 0x180, 0x100, buf, |
| 315 | 0x11, 0x00, 0x03, 0x01, 0x00, 0x01); |
| 316 | send_packet(udev, packet, 0x280, 0x100, buf, |
| 317 | 0x11, 0x00, 0x03, 0x02, 0x00, 0x01); |
| 318 | send_packet(udev, packet, 0x380, 0x080, buf, |
| 319 | 0x11, 0x00, 0x03, 0x03, 0x80, 0x00); |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | |
| 323 | static void send_data(struct asus_oled_dev *odev) |
| 324 | { |
| 325 | 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] | 326 | struct asus_oled_packet *packet; |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 327 | |
| 328 | packet = kzalloc(sizeof(struct asus_oled_packet), GFP_KERNEL); |
| 329 | |
| 330 | if (!packet) { |
| 331 | dev_err(&odev->udev->dev, "out of memory\n"); |
| 332 | return; |
| 333 | } |
| 334 | |
Andre Haupt | 118015c | 2009-01-19 12:00:19 +0100 | [diff] [blame] | 335 | if (odev->pack_mode == PACK_MODE_G1) { |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 336 | /* When sending roll-mode data the display updated only |
| 337 | first packet. I have no idea why, but when static picture |
| 338 | is sent just before rolling picture everything works fine. */ |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 339 | if (odev->pic_mode == ASUS_OLED_ROLL) |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 340 | send_packets(odev->udev, packet, odev->buf, |
| 341 | ASUS_OLED_STATIC, 2); |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 342 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 343 | /* Only ROLL mode can use more than 2 packets.*/ |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 344 | if (odev->pic_mode != ASUS_OLED_ROLL && packet_num > 2) |
| 345 | packet_num = 2; |
| 346 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 347 | send_packets(odev->udev, packet, odev->buf, |
| 348 | odev->pic_mode, packet_num); |
| 349 | } else if (odev->pack_mode == PACK_MODE_G50) { |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 350 | send_packets_g50(odev->udev, packet, odev->buf); |
| 351 | } |
| 352 | |
| 353 | kfree(packet); |
| 354 | } |
| 355 | |
| 356 | static int append_values(struct asus_oled_dev *odev, uint8_t val, size_t count) |
| 357 | { |
Pekka Paalanen | 635032c | 2012-01-22 16:33:46 +0200 | [diff] [blame^] | 358 | odev->last_val = val; |
| 359 | |
| 360 | if (val == 0) { |
| 361 | odev->buf_offs += count; |
| 362 | return 0; |
| 363 | } |
| 364 | |
| 365 | while (count-- > 0) { |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 366 | size_t x = odev->buf_offs % odev->width; |
| 367 | size_t y = odev->buf_offs / odev->width; |
| 368 | size_t i; |
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 | x += odev->x_shift; |
| 371 | y += odev->y_shift; |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 372 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 373 | switch (odev->pack_mode) { |
| 374 | case PACK_MODE_G1: |
| 375 | /* i = (x/128)*640 + 127 - x + (y/8)*128; |
| 376 | This one for 128 is the same, but might be better |
| 377 | for different widths? */ |
| 378 | i = (x/odev->dev_width)*640 + |
| 379 | odev->dev_width - 1 - x + |
| 380 | (y/8)*odev->dev_width; |
| 381 | break; |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 382 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 383 | case PACK_MODE_G50: |
| 384 | i = (odev->dev_width - 1 - x)/8 + y*odev->dev_width/8; |
| 385 | break; |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 386 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 387 | default: |
| 388 | i = 0; |
| 389 | printk(ASUS_OLED_ERROR "Unknown OLED Pack Mode: %d!\n", |
| 390 | odev->pack_mode); |
| 391 | break; |
| 392 | } |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 393 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 394 | if (i >= odev->buf_size) { |
| 395 | printk(ASUS_OLED_ERROR "Buffer overflow! Report a bug:" |
| 396 | "offs: %d >= %d i: %d (x: %d y: %d)\n", |
| 397 | (int) odev->buf_offs, (int) odev->buf_size, |
| 398 | (int) i, (int) x, (int) y); |
| 399 | return -EIO; |
| 400 | } |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 401 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 402 | switch (odev->pack_mode) { |
| 403 | case PACK_MODE_G1: |
| 404 | odev->buf[i] &= ~(1<<(y%8)); |
| 405 | break; |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 406 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 407 | case PACK_MODE_G50: |
| 408 | odev->buf[i] &= ~(1<<(x%8)); |
| 409 | break; |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 410 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 411 | default: |
| 412 | /* cannot get here; stops gcc complaining*/ |
| 413 | ; |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 414 | } |
| 415 | |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 416 | odev->buf_offs++; |
| 417 | } |
| 418 | |
| 419 | return 0; |
| 420 | } |
| 421 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 422 | static ssize_t odev_set_picture(struct asus_oled_dev *odev, |
| 423 | const char *buf, size_t count) |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 424 | { |
| 425 | size_t offs = 0, max_offs; |
| 426 | |
Andre Haupt | 526fd20 | 2009-01-19 12:00:18 +0100 | [diff] [blame] | 427 | if (count < 1) |
| 428 | return 0; |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 429 | |
Andre Haupt | 118015c | 2009-01-19 12:00:19 +0100 | [diff] [blame] | 430 | if (tolower(buf[0]) == 'b') { |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 431 | /* binary mode, set the entire memory*/ |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 432 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 433 | size_t i; |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 434 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 435 | odev->buf_size = (odev->dev_width * ASUS_OLED_DISP_HEIGHT) / 8; |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 436 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 437 | kfree(odev->buf); |
| 438 | odev->buf = kmalloc(odev->buf_size, GFP_KERNEL); |
Peter Huewe | 0df28be | 2010-01-07 19:57:36 +0100 | [diff] [blame] | 439 | if (odev->buf == NULL) { |
| 440 | odev->buf_size = 0; |
| 441 | printk(ASUS_OLED_ERROR "Out of memory!\n"); |
| 442 | return -ENOMEM; |
| 443 | } |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 444 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 445 | memset(odev->buf, 0xff, odev->buf_size); |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 446 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 447 | for (i = 1; i < count && i <= 32 * 32; i++) { |
| 448 | odev->buf[i-1] = buf[i]; |
| 449 | odev->buf_offs = i-1; |
| 450 | } |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 451 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 452 | odev->width = odev->dev_width / 8; |
| 453 | odev->height = ASUS_OLED_DISP_HEIGHT; |
| 454 | odev->x_shift = 0; |
| 455 | odev->y_shift = 0; |
| 456 | odev->last_val = 0; |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 457 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 458 | send_data(odev); |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 459 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 460 | return count; |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | if (buf[0] == '<') { |
| 464 | size_t i; |
| 465 | size_t w = 0, h = 0; |
| 466 | size_t w_mem, h_mem; |
| 467 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 468 | if (count < 10 || buf[2] != ':') |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 469 | goto error_header; |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 470 | |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 471 | |
Andre Haupt | 118015c | 2009-01-19 12:00:19 +0100 | [diff] [blame] | 472 | switch (tolower(buf[1])) { |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 473 | case ASUS_OLED_STATIC: |
| 474 | case ASUS_OLED_ROLL: |
| 475 | case ASUS_OLED_FLASH: |
| 476 | odev->pic_mode = buf[1]; |
| 477 | break; |
| 478 | default: |
| 479 | printk(ASUS_OLED_ERROR "Wrong picture mode: '%c'.\n", |
| 480 | buf[1]); |
| 481 | return -EIO; |
| 482 | break; |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | for (i = 3; i < count; ++i) { |
| 486 | if (buf[i] >= '0' && buf[i] <= '9') { |
| 487 | w = 10*w + (buf[i] - '0'); |
| 488 | |
Andre Haupt | 526fd20 | 2009-01-19 12:00:18 +0100 | [diff] [blame] | 489 | if (w > ASUS_OLED_MAX_WIDTH) |
| 490 | goto error_width; |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 491 | } else if (tolower(buf[i]) == 'x') { |
Andre Haupt | 526fd20 | 2009-01-19 12:00:18 +0100 | [diff] [blame] | 492 | break; |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 493 | } else { |
Andre Haupt | 526fd20 | 2009-01-19 12:00:18 +0100 | [diff] [blame] | 494 | goto error_width; |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 495 | } |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | for (++i; i < count; ++i) { |
| 499 | if (buf[i] >= '0' && buf[i] <= '9') { |
| 500 | h = 10*h + (buf[i] - '0'); |
| 501 | |
Andre Haupt | 526fd20 | 2009-01-19 12:00:18 +0100 | [diff] [blame] | 502 | if (h > ASUS_OLED_DISP_HEIGHT) |
| 503 | goto error_height; |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 504 | } else if (tolower(buf[i]) == '>') { |
Andre Haupt | 526fd20 | 2009-01-19 12:00:18 +0100 | [diff] [blame] | 505 | break; |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 506 | } else { |
Andre Haupt | 526fd20 | 2009-01-19 12:00:18 +0100 | [diff] [blame] | 507 | goto error_height; |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 508 | } |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 509 | } |
| 510 | |
Andre Haupt | 526fd20 | 2009-01-19 12:00:18 +0100 | [diff] [blame] | 511 | if (w < 1 || w > ASUS_OLED_MAX_WIDTH) |
| 512 | goto error_width; |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 513 | |
Andre Haupt | 526fd20 | 2009-01-19 12:00:18 +0100 | [diff] [blame] | 514 | if (h < 1 || h > ASUS_OLED_DISP_HEIGHT) |
| 515 | goto error_height; |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 516 | |
Andre Haupt | 526fd20 | 2009-01-19 12:00:18 +0100 | [diff] [blame] | 517 | if (i >= count || buf[i] != '>') |
| 518 | goto error_header; |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 519 | |
| 520 | offs = i+1; |
| 521 | |
| 522 | if (w % (odev->dev_width) != 0) |
| 523 | w_mem = (w/(odev->dev_width) + 1)*(odev->dev_width); |
| 524 | else |
| 525 | w_mem = w; |
| 526 | |
| 527 | if (h < ASUS_OLED_DISP_HEIGHT) |
| 528 | h_mem = ASUS_OLED_DISP_HEIGHT; |
| 529 | else |
| 530 | h_mem = h; |
| 531 | |
| 532 | odev->buf_size = w_mem * h_mem / 8; |
| 533 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 534 | kfree(odev->buf); |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 535 | odev->buf = kmalloc(odev->buf_size, GFP_KERNEL); |
| 536 | |
| 537 | if (odev->buf == NULL) { |
| 538 | odev->buf_size = 0; |
| 539 | printk(ASUS_OLED_ERROR "Out of memory!\n"); |
| 540 | return -ENOMEM; |
| 541 | } |
| 542 | |
| 543 | memset(odev->buf, 0xff, odev->buf_size); |
| 544 | |
| 545 | odev->buf_offs = 0; |
| 546 | odev->width = w; |
| 547 | odev->height = h; |
| 548 | odev->x_shift = 0; |
| 549 | odev->y_shift = 0; |
| 550 | odev->last_val = 0; |
| 551 | |
| 552 | if (odev->pic_mode == ASUS_OLED_FLASH) { |
| 553 | if (h < ASUS_OLED_DISP_HEIGHT/2) |
| 554 | odev->y_shift = (ASUS_OLED_DISP_HEIGHT/2 - h)/2; |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 555 | } else { |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 556 | if (h < ASUS_OLED_DISP_HEIGHT) |
| 557 | odev->y_shift = (ASUS_OLED_DISP_HEIGHT - h)/2; |
| 558 | } |
| 559 | |
| 560 | if (w < (odev->dev_width)) |
| 561 | odev->x_shift = ((odev->dev_width) - w)/2; |
| 562 | } |
| 563 | |
| 564 | max_offs = odev->width * odev->height; |
| 565 | |
| 566 | while (offs < count && odev->buf_offs < max_offs) { |
Greg Kroah-Hartman | 0d99b6e | 2009-06-04 11:29:54 -0700 | [diff] [blame] | 567 | int ret = 0; |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 568 | |
| 569 | if (buf[offs] == '1' || buf[offs] == '#') { |
Andre Haupt | 94aa5b4 | 2009-01-19 12:00:20 +0100 | [diff] [blame] | 570 | ret = append_values(odev, 1, 1); |
| 571 | if (ret < 0) |
Andre Haupt | 526fd20 | 2009-01-19 12:00:18 +0100 | [diff] [blame] | 572 | return ret; |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 573 | } else if (buf[offs] == '0' || buf[offs] == ' ') { |
Andre Haupt | 94aa5b4 | 2009-01-19 12:00:20 +0100 | [diff] [blame] | 574 | ret = append_values(odev, 0, 1); |
| 575 | if (ret < 0) |
Andre Haupt | 526fd20 | 2009-01-19 12:00:18 +0100 | [diff] [blame] | 576 | return ret; |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 577 | } else if (buf[offs] == '\n') { |
| 578 | /* New line detected. Lets assume, that all characters |
| 579 | till the end of the line were equal to the last |
| 580 | character in this line.*/ |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 581 | if (odev->buf_offs % odev->width != 0) |
Andre Haupt | 94aa5b4 | 2009-01-19 12:00:20 +0100 | [diff] [blame] | 582 | ret = append_values(odev, odev->last_val, |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 583 | odev->width - |
| 584 | (odev->buf_offs % |
| 585 | odev->width)); |
| 586 | if (ret < 0) |
| 587 | return ret; |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | offs++; |
| 591 | } |
| 592 | |
Andre Haupt | 526fd20 | 2009-01-19 12:00:18 +0100 | [diff] [blame] | 593 | if (odev->buf_offs >= max_offs) |
| 594 | send_data(odev); |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 595 | |
| 596 | return count; |
| 597 | |
| 598 | error_width: |
| 599 | printk(ASUS_OLED_ERROR "Wrong picture width specified.\n"); |
| 600 | return -EIO; |
| 601 | |
| 602 | error_height: |
| 603 | printk(ASUS_OLED_ERROR "Wrong picture height specified.\n"); |
| 604 | return -EIO; |
| 605 | |
| 606 | error_header: |
| 607 | printk(ASUS_OLED_ERROR "Wrong picture header.\n"); |
| 608 | return -EIO; |
| 609 | } |
| 610 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 611 | static ssize_t set_picture(struct device *dev, struct device_attribute *attr, |
| 612 | const char *buf, size_t count) |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 613 | { |
| 614 | struct usb_interface *intf = to_usb_interface(dev); |
| 615 | |
| 616 | return odev_set_picture(usb_get_intfdata(intf), buf, count); |
| 617 | } |
| 618 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 619 | static ssize_t class_set_picture(struct device *device, |
| 620 | struct device_attribute *attr, |
| 621 | const char *buf, size_t count) |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 622 | { |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 623 | return odev_set_picture((struct asus_oled_dev *) |
| 624 | dev_get_drvdata(device), buf, count); |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | #define ASUS_OLED_DEVICE_ATTR(_file) dev_attr_asus_oled_##_file |
| 628 | |
Greg Kroah-Hartman | 515b498 | 2010-11-18 11:21:04 -0800 | [diff] [blame] | 629 | static DEVICE_ATTR(asus_oled_enabled, S_IWUSR | S_IRUGO, |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 630 | get_enabled, set_enabled); |
Greg Kroah-Hartman | 515b498 | 2010-11-18 11:21:04 -0800 | [diff] [blame] | 631 | static DEVICE_ATTR(asus_oled_picture, S_IWUSR , NULL, set_picture); |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 632 | |
Greg Kroah-Hartman | 515b498 | 2010-11-18 11:21:04 -0800 | [diff] [blame] | 633 | static DEVICE_ATTR(enabled, S_IWUSR | S_IRUGO, |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 634 | class_get_enabled, class_set_enabled); |
Greg Kroah-Hartman | 515b498 | 2010-11-18 11:21:04 -0800 | [diff] [blame] | 635 | static DEVICE_ATTR(picture, S_IWUSR, NULL, class_set_picture); |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 636 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 637 | static int asus_oled_probe(struct usb_interface *interface, |
| 638 | const struct usb_device_id *id) |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 639 | { |
| 640 | struct usb_device *udev = interface_to_usbdev(interface); |
| 641 | struct asus_oled_dev *odev = NULL; |
| 642 | int retval = -ENOMEM; |
| 643 | uint16_t dev_width = 0; |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 644 | enum oled_pack_mode pack_mode = PACK_MODE_LAST; |
| 645 | const struct oled_dev_desc_str *dev_desc = oled_dev_desc_table; |
Andre Haupt | ccdb245 | 2009-01-19 12:00:16 +0100 | [diff] [blame] | 646 | const char *desc = NULL; |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 647 | |
Andre Haupt | ccdb245 | 2009-01-19 12:00:16 +0100 | [diff] [blame] | 648 | if (!id) { |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 649 | /* Even possible? Just to make sure...*/ |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 650 | dev_err(&interface->dev, "No usb_device_id provided!\n"); |
| 651 | return -ENODEV; |
| 652 | } |
| 653 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 654 | for (; dev_desc->idVendor; dev_desc++) { |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 655 | if (dev_desc->idVendor == id->idVendor |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 656 | && dev_desc->idProduct == id->idProduct) { |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 657 | dev_width = dev_desc->devWidth; |
| 658 | desc = dev_desc->devDesc; |
| 659 | pack_mode = dev_desc->packMode; |
| 660 | break; |
| 661 | } |
| 662 | } |
| 663 | |
Andre Haupt | 118015c | 2009-01-19 12:00:19 +0100 | [diff] [blame] | 664 | if (!desc || dev_width < 1 || pack_mode == PACK_MODE_LAST) { |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 665 | dev_err(&interface->dev, |
| 666 | "Missing or incomplete device description!\n"); |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 667 | return -ENODEV; |
| 668 | } |
| 669 | |
| 670 | odev = kzalloc(sizeof(struct asus_oled_dev), GFP_KERNEL); |
| 671 | |
| 672 | if (odev == NULL) { |
| 673 | dev_err(&interface->dev, "Out of memory\n"); |
| 674 | return -ENOMEM; |
| 675 | } |
| 676 | |
| 677 | odev->udev = usb_get_dev(udev); |
| 678 | odev->pic_mode = ASUS_OLED_STATIC; |
| 679 | odev->dev_width = dev_width; |
| 680 | odev->pack_mode = pack_mode; |
| 681 | odev->height = 0; |
| 682 | odev->width = 0; |
| 683 | odev->x_shift = 0; |
| 684 | odev->y_shift = 0; |
| 685 | odev->buf_offs = 0; |
| 686 | odev->buf_size = 0; |
| 687 | odev->last_val = 0; |
| 688 | odev->buf = NULL; |
| 689 | odev->enabled = 1; |
Andre Haupt | ccdb245 | 2009-01-19 12:00:16 +0100 | [diff] [blame] | 690 | odev->dev = NULL; |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 691 | |
Andre Haupt | 118015c | 2009-01-19 12:00:19 +0100 | [diff] [blame] | 692 | usb_set_intfdata(interface, odev); |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 693 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 694 | retval = device_create_file(&interface->dev, |
| 695 | &ASUS_OLED_DEVICE_ATTR(enabled)); |
Andre Haupt | 94aa5b4 | 2009-01-19 12:00:20 +0100 | [diff] [blame] | 696 | if (retval) |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 697 | goto err_files; |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 698 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 699 | retval = device_create_file(&interface->dev, |
| 700 | &ASUS_OLED_DEVICE_ATTR(picture)); |
Andre Haupt | 94aa5b4 | 2009-01-19 12:00:20 +0100 | [diff] [blame] | 701 | if (retval) |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 702 | goto err_files; |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 703 | |
Andre Haupt | 118015c | 2009-01-19 12:00:19 +0100 | [diff] [blame] | 704 | odev->dev = device_create(oled_class, &interface->dev, MKDEV(0, 0), |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 705 | NULL, "oled_%d", ++oled_num); |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 706 | |
| 707 | if (IS_ERR(odev->dev)) { |
| 708 | retval = PTR_ERR(odev->dev); |
| 709 | goto err_files; |
| 710 | } |
| 711 | |
| 712 | dev_set_drvdata(odev->dev, odev); |
| 713 | |
Andre Haupt | 94aa5b4 | 2009-01-19 12:00:20 +0100 | [diff] [blame] | 714 | retval = device_create_file(odev->dev, &dev_attr_enabled); |
| 715 | if (retval) |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 716 | goto err_class_enabled; |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 717 | |
Andre Haupt | 94aa5b4 | 2009-01-19 12:00:20 +0100 | [diff] [blame] | 718 | retval = device_create_file(odev->dev, &dev_attr_picture); |
| 719 | if (retval) |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 720 | goto err_class_picture; |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 721 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 722 | dev_info(&interface->dev, |
| 723 | "Attached Asus OLED device: %s [width %u, pack_mode %d]\n", |
| 724 | desc, odev->dev_width, odev->pack_mode); |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 725 | |
| 726 | if (start_off) |
| 727 | enable_oled(odev, 0); |
| 728 | |
| 729 | return 0; |
| 730 | |
| 731 | err_class_picture: |
| 732 | device_remove_file(odev->dev, &dev_attr_picture); |
| 733 | |
| 734 | err_class_enabled: |
| 735 | device_remove_file(odev->dev, &dev_attr_enabled); |
| 736 | device_unregister(odev->dev); |
| 737 | |
| 738 | err_files: |
| 739 | device_remove_file(&interface->dev, &ASUS_OLED_DEVICE_ATTR(enabled)); |
| 740 | device_remove_file(&interface->dev, &ASUS_OLED_DEVICE_ATTR(picture)); |
| 741 | |
Andre Haupt | 118015c | 2009-01-19 12:00:19 +0100 | [diff] [blame] | 742 | usb_set_intfdata(interface, NULL); |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 743 | usb_put_dev(odev->udev); |
| 744 | kfree(odev); |
| 745 | |
| 746 | return retval; |
| 747 | } |
| 748 | |
| 749 | static void asus_oled_disconnect(struct usb_interface *interface) |
| 750 | { |
| 751 | struct asus_oled_dev *odev; |
| 752 | |
Andre Haupt | 118015c | 2009-01-19 12:00:19 +0100 | [diff] [blame] | 753 | odev = usb_get_intfdata(interface); |
| 754 | usb_set_intfdata(interface, NULL); |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 755 | |
| 756 | device_remove_file(odev->dev, &dev_attr_picture); |
| 757 | device_remove_file(odev->dev, &dev_attr_enabled); |
| 758 | device_unregister(odev->dev); |
| 759 | |
Andre Haupt | 118015c | 2009-01-19 12:00:19 +0100 | [diff] [blame] | 760 | device_remove_file(&interface->dev, &ASUS_OLED_DEVICE_ATTR(picture)); |
| 761 | device_remove_file(&interface->dev, &ASUS_OLED_DEVICE_ATTR(enabled)); |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 762 | |
| 763 | usb_put_dev(odev->udev); |
| 764 | |
Kevin A. Granade | 1ff12a4 | 2009-09-05 01:03:39 -0500 | [diff] [blame] | 765 | kfree(odev->buf); |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 766 | |
| 767 | kfree(odev); |
| 768 | |
| 769 | dev_info(&interface->dev, "Disconnected Asus OLED device\n"); |
| 770 | } |
| 771 | |
| 772 | static struct usb_driver oled_driver = { |
| 773 | .name = ASUS_OLED_NAME, |
| 774 | .probe = asus_oled_probe, |
| 775 | .disconnect = asus_oled_disconnect, |
| 776 | .id_table = id_table, |
| 777 | }; |
| 778 | |
Andi Kleen | 0933e2d | 2010-01-05 12:48:09 +0100 | [diff] [blame] | 779 | static CLASS_ATTR_STRING(version, S_IRUGO, |
Andrea Gelmini | e67fdbc | 2010-03-01 00:28:47 +0100 | [diff] [blame] | 780 | ASUS_OLED_UNDERSCORE_NAME " " ASUS_OLED_VERSION); |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 781 | |
| 782 | static int __init asus_oled_init(void) |
| 783 | { |
| 784 | int retval = 0; |
| 785 | oled_class = class_create(THIS_MODULE, ASUS_OLED_UNDERSCORE_NAME); |
| 786 | |
| 787 | if (IS_ERR(oled_class)) { |
| 788 | err("Error creating " ASUS_OLED_UNDERSCORE_NAME " class"); |
| 789 | return PTR_ERR(oled_class); |
| 790 | } |
| 791 | |
Andi Kleen | 0933e2d | 2010-01-05 12:48:09 +0100 | [diff] [blame] | 792 | retval = class_create_file(oled_class, &class_attr_version.attr); |
Andre Haupt | 94aa5b4 | 2009-01-19 12:00:20 +0100 | [diff] [blame] | 793 | if (retval) { |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 794 | err("Error creating class version file"); |
| 795 | goto error; |
| 796 | } |
| 797 | |
| 798 | retval = usb_register(&oled_driver); |
| 799 | |
| 800 | if (retval) { |
| 801 | err("usb_register failed. Error number %d", retval); |
| 802 | goto error; |
| 803 | } |
| 804 | |
| 805 | return retval; |
| 806 | |
| 807 | error: |
| 808 | class_destroy(oled_class); |
| 809 | return retval; |
| 810 | } |
| 811 | |
| 812 | static void __exit asus_oled_exit(void) |
| 813 | { |
Andi Kleen | 0933e2d | 2010-01-05 12:48:09 +0100 | [diff] [blame] | 814 | class_remove_file(oled_class, &class_attr_version.attr); |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 815 | class_destroy(oled_class); |
| 816 | |
| 817 | usb_deregister(&oled_driver); |
| 818 | } |
| 819 | |
Andre Haupt | 118015c | 2009-01-19 12:00:19 +0100 | [diff] [blame] | 820 | module_init(asus_oled_init); |
| 821 | module_exit(asus_oled_exit); |
Jakub Schmidtke | fb53440 | 2008-11-04 23:46:58 -0800 | [diff] [blame] | 822 | |