blob: e77e4e0396cf9f4debc146e7a4816e01c4928271 [file] [log] [blame]
Jakub Schmidtkefb534402008-11-04 23:46:58 -08001/*
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. Mattock631dd1a2010-10-18 11:03:14 +020027 * <http://lapsus.berlios.de/asus_oled.html>.
Jakub Schmidtkefb534402008-11-04 23:46:58 -080028 *
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 Huewee89230e2009-12-15 05:04:22 +010055#define USB_VENDOR_ID_ASUS 0x0b05
56#define USB_DEVICE_ID_ASUS_LCM 0x1726
57#define USB_DEVICE_ID_ASUS_LCM2 0x175b
58
Jakub Schmidtkefb534402008-11-04 23:46:58 -080059MODULE_AUTHOR("Jakub Schmidtke, sjakub@gmail.com");
60MODULE_DESCRIPTION("Asus OLED Driver v" ASUS_OLED_VERSION);
61MODULE_LICENSE("GPL");
62
Andre Hauptb0e5ca82009-01-19 12:00:17 +010063static struct class *oled_class;
64static int oled_num;
Jakub Schmidtkefb534402008-11-04 23:46:58 -080065
Andre Hauptb0e5ca82009-01-19 12:00:17 +010066static uint start_off;
Jakub Schmidtkefb534402008-11-04 23:46:58 -080067
68module_param(start_off, uint, 0644);
69
Kevin A. Granade1ff12a42009-09-05 01:03:39 -050070MODULE_PARM_DESC(start_off,
71 "Set to 1 to switch off OLED display after it is attached");
Jakub Schmidtkefb534402008-11-04 23:46:58 -080072
Johan Meiring9f027eb2010-11-06 18:24:46 +020073enum oled_pack_mode {
Jakub Schmidtkefb534402008-11-04 23:46:58 -080074 PACK_MODE_G1,
75 PACK_MODE_G50,
76 PACK_MODE_LAST
Kevin A. Granade1ff12a42009-09-05 01:03:39 -050077};
Jakub Schmidtkefb534402008-11-04 23:46:58 -080078
79struct oled_dev_desc_str {
80 uint16_t idVendor;
81 uint16_t idProduct;
Kevin A. Granade1ff12a42009-09-05 01:03:39 -050082 /* width of display */
83 uint16_t devWidth;
84 /* formula to be used while packing the picture */
85 enum oled_pack_mode packMode;
Jakub Schmidtkefb534402008-11-04 23:46:58 -080086 const char *devDesc;
87};
88
89/* table of devices that work with this driver */
Németh Mártona4577322010-01-10 00:18:34 +010090static const struct usb_device_id id_table[] = {
Kevin A. Granade1ff12a42009-09-05 01:03:39 -050091 /* Asus G1/G2 (and variants)*/
Peter Huewee89230e2009-12-15 05:04:22 +010092 { USB_DEVICE(USB_VENDOR_ID_ASUS, USB_DEVICE_ID_ASUS_LCM) },
Kevin A. Granade1ff12a42009-09-05 01:03:39 -050093 /* Asus G50V (and possibly others - G70? G71?)*/
Peter Huewee89230e2009-12-15 05:04:22 +010094 { USB_DEVICE(USB_VENDOR_ID_ASUS, USB_DEVICE_ID_ASUS_LCM2) },
Jakub Schmidtkefb534402008-11-04 23:46:58 -080095 { },
96};
97
98/* parameters of specific devices */
Andre Haupt118015c2009-01-19 12:00:19 +010099static struct oled_dev_desc_str oled_dev_desc_table[] = {
Peter Huewee89230e2009-12-15 05:04:22 +0100100 { 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 Schmidtkefb534402008-11-04 23:46:58 -0800104 { },
105};
106
Andre Haupt118015c2009-01-19 12:00:19 +0100107MODULE_DEVICE_TABLE(usb, id_table);
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800108
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800109struct 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
122struct asus_oled_packet {
123 struct asus_oled_header header;
124 uint8_t bitmap[ASUS_OLED_PACKET_BUF_SIZE];
125} __attribute((packed));
126
127struct asus_oled_dev {
Kevin A. Granade1ff12a42009-09-05 01:03:39 -0500128 struct usb_device *udev;
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800129 uint8_t pic_mode;
130 uint16_t dev_width;
Kevin A. Granade1ff12a42009-09-05 01:03:39 -0500131 enum oled_pack_mode pack_mode;
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800132 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. Granade1ff12a42009-09-05 01:03:39 -0500144static 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 Schmidtkefb534402008-11-04 23:46:58 -0800160static void enable_oled(struct asus_oled_dev *odev, uint8_t enabl)
161{
162 int a;
163 int retval;
164 int act_len;
Kevin A. Granade1ff12a42009-09-05 01:03:39 -0500165 struct asus_oled_packet *packet;
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800166
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. Granade1ff12a42009-09-05 01:03:39 -0500174 setup_packet_header(packet, 0x20, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00);
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800175
Andre Haupt526fd202009-01-19 12:00:18 +0100176 if (enabl)
177 packet->bitmap[0] = 0xaf;
178 else
179 packet->bitmap[0] = 0xae;
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800180
Andre Haupt118015c2009-01-19 12:00:19 +0100181 for (a = 0; a < 1; a++) {
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800182 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. Granade1ff12a42009-09-05 01:03:39 -0500198static ssize_t set_enabled(struct device *dev, struct device_attribute *attr,
199 const char *buf, size_t count)
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800200{
201 struct usb_interface *intf = to_usb_interface(dev);
202 struct asus_oled_dev *odev = usb_get_intfdata(intf);
Eugeni Dodonov20633bf2009-12-23 10:27:22 -0200203 unsigned long value;
Ken O'Brien67d6baa2011-11-19 01:18:12 +0000204 if (kstrtoul(buf, 10, &value))
Eugeni Dodonov20633bf2009-12-23 10:27:22 -0200205 return -EINVAL;
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800206
Eugeni Dodonov20633bf2009-12-23 10:27:22 -0200207 enable_oled(odev, value);
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800208
209 return count;
210}
211
Kevin A. Granade1ff12a42009-09-05 01:03:39 -0500212static ssize_t class_set_enabled(struct device *device,
213 struct device_attribute *attr,
214 const char *buf, size_t count)
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800215{
Kevin A. Granade1ff12a42009-09-05 01:03:39 -0500216 struct asus_oled_dev *odev =
217 (struct asus_oled_dev *) dev_get_drvdata(device);
Eugeni Dodonov20633bf2009-12-23 10:27:22 -0200218 unsigned long value;
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800219
Ken O'Brien67d6baa2011-11-19 01:18:12 +0000220 if (kstrtoul(buf, 10, &value))
Eugeni Dodonov20633bf2009-12-23 10:27:22 -0200221 return -EINVAL;
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800222
Eugeni Dodonov20633bf2009-12-23 10:27:22 -0200223 enable_oled(odev, value);
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800224
225 return count;
226}
227
Kevin A. Granade1ff12a42009-09-05 01:03:39 -0500228static ssize_t get_enabled(struct device *dev, struct device_attribute *attr,
229 char *buf)
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800230{
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. Granade1ff12a42009-09-05 01:03:39 -0500237static ssize_t class_get_enabled(struct device *device,
238 struct device_attribute *attr, char *buf)
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800239{
Kevin A. Granade1ff12a42009-09-05 01:03:39 -0500240 struct asus_oled_dev *odev =
241 (struct asus_oled_dev *) dev_get_drvdata(device);
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800242
243 return sprintf(buf, "%d\n", odev->enabled);
244}
245
Kevin A. Granade1ff12a42009-09-05 01:03:39 -0500246static 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 Schmidtkefb534402008-11-04 23:46:58 -0800249{
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. Granade1ff12a42009-09-05 01:03:39 -0500257 case ASUS_OLED_ROLL:
258 setup_packet_header(packet, 0x40, 0x80, p_num,
259 i + 1, 0x00, 0x01, 0xff);
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800260 break;
Kevin A. Granade1ff12a42009-09-05 01:03:39 -0500261 case ASUS_OLED_STATIC:
262 setup_packet_header(packet, 0x10 + i, 0x80, 0x01,
263 0x01, 0x00, 0x01, 0x00);
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800264 break;
Kevin A. Granade1ff12a42009-09-05 01:03:39 -0500265 case ASUS_OLED_FLASH:
266 setup_packet_header(packet, 0x10 + i, 0x80, 0x01,
267 0x01, 0x00, 0x00, 0xff);
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800268 break;
269 }
270
Kevin A. Granade1ff12a42009-09-05 01:03:39 -0500271 memcpy(packet->bitmap, buf + (ASUS_OLED_PACKET_BUF_SIZE*i),
272 ASUS_OLED_PACKET_BUF_SIZE);
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800273
Kevin A. Granade1ff12a42009-09-05 01:03:39 -0500274 retval = usb_bulk_msg(udev, usb_sndctrlpipe(udev, 2),
275 packet, sizeof(struct asus_oled_packet),
276 &act_len, -1);
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800277
278 if (retval)
279 dev_dbg(&udev->dev, "retval = %d\n", retval);
280 }
281}
282
Kevin A. Granade1ff12a42009-09-05 01:03:39 -0500283static 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 Schmidtkefb534402008-11-04 23:46:58 -0800288 int retval;
289 int act_len;
290
Kevin A. Granade1ff12a42009-09-05 01:03:39 -0500291 setup_packet_header(packet, b1, b2, b3, b4, b5, b6, 0x00);
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800292 memcpy(packet->bitmap, buf + offset, len);
293
294 retval = usb_bulk_msg(udev,
Kevin A. Granade1ff12a42009-09-05 01:03:39 -0500295 usb_sndctrlpipe(udev, 2),
296 packet,
297 sizeof(struct asus_oled_packet),
298 &act_len,
299 -1);
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800300
301 if (retval)
302 dev_dbg(&udev->dev, "retval = %d\n", retval);
303}
304
305
Kevin A. Granade1ff12a42009-09-05 01:03:39 -0500306static void send_packets_g50(struct usb_device *udev,
307 struct asus_oled_packet *packet, char *buf)
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800308{
Kevin A. Granade1ff12a42009-09-05 01:03:39 -0500309 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 Schmidtkefb534402008-11-04 23:46:58 -0800313
Kevin A. Granade1ff12a42009-09-05 01:03:39 -0500314 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 Schmidtkefb534402008-11-04 23:46:58 -0800320}
321
322
323static void send_data(struct asus_oled_dev *odev)
324{
325 size_t packet_num = odev->buf_size / ASUS_OLED_PACKET_BUF_SIZE;
Kevin A. Granade1ff12a42009-09-05 01:03:39 -0500326 struct asus_oled_packet *packet;
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800327
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 Haupt118015c2009-01-19 12:00:19 +0100335 if (odev->pack_mode == PACK_MODE_G1) {
Kevin A. Granade1ff12a42009-09-05 01:03:39 -0500336 /* 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 Schmidtkefb534402008-11-04 23:46:58 -0800339 if (odev->pic_mode == ASUS_OLED_ROLL)
Kevin A. Granade1ff12a42009-09-05 01:03:39 -0500340 send_packets(odev->udev, packet, odev->buf,
341 ASUS_OLED_STATIC, 2);
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800342
Kevin A. Granade1ff12a42009-09-05 01:03:39 -0500343 /* Only ROLL mode can use more than 2 packets.*/
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800344 if (odev->pic_mode != ASUS_OLED_ROLL && packet_num > 2)
345 packet_num = 2;
346
Kevin A. Granade1ff12a42009-09-05 01:03:39 -0500347 send_packets(odev->udev, packet, odev->buf,
348 odev->pic_mode, packet_num);
349 } else if (odev->pack_mode == PACK_MODE_G50) {
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800350 send_packets_g50(odev->udev, packet, odev->buf);
351 }
352
353 kfree(packet);
354}
355
356static int append_values(struct asus_oled_dev *odev, uint8_t val, size_t count)
357{
Kevin A. Granade1ff12a42009-09-05 01:03:39 -0500358 while (count-- > 0 && val) {
359 size_t x = odev->buf_offs % odev->width;
360 size_t y = odev->buf_offs / odev->width;
361 size_t i;
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800362
Kevin A. Granade1ff12a42009-09-05 01:03:39 -0500363 x += odev->x_shift;
364 y += odev->y_shift;
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800365
Kevin A. Granade1ff12a42009-09-05 01:03:39 -0500366 switch (odev->pack_mode) {
367 case PACK_MODE_G1:
368 /* i = (x/128)*640 + 127 - x + (y/8)*128;
369 This one for 128 is the same, but might be better
370 for different widths? */
371 i = (x/odev->dev_width)*640 +
372 odev->dev_width - 1 - x +
373 (y/8)*odev->dev_width;
374 break;
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800375
Kevin A. Granade1ff12a42009-09-05 01:03:39 -0500376 case PACK_MODE_G50:
377 i = (odev->dev_width - 1 - x)/8 + y*odev->dev_width/8;
378 break;
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800379
Kevin A. Granade1ff12a42009-09-05 01:03:39 -0500380 default:
381 i = 0;
382 printk(ASUS_OLED_ERROR "Unknown OLED Pack Mode: %d!\n",
383 odev->pack_mode);
384 break;
385 }
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800386
Kevin A. Granade1ff12a42009-09-05 01:03:39 -0500387 if (i >= odev->buf_size) {
388 printk(ASUS_OLED_ERROR "Buffer overflow! Report a bug:"
389 "offs: %d >= %d i: %d (x: %d y: %d)\n",
390 (int) odev->buf_offs, (int) odev->buf_size,
391 (int) i, (int) x, (int) y);
392 return -EIO;
393 }
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800394
Kevin A. Granade1ff12a42009-09-05 01:03:39 -0500395 switch (odev->pack_mode) {
396 case PACK_MODE_G1:
397 odev->buf[i] &= ~(1<<(y%8));
398 break;
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800399
Kevin A. Granade1ff12a42009-09-05 01:03:39 -0500400 case PACK_MODE_G50:
401 odev->buf[i] &= ~(1<<(x%8));
402 break;
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800403
Kevin A. Granade1ff12a42009-09-05 01:03:39 -0500404 default:
405 /* cannot get here; stops gcc complaining*/
406 ;
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800407 }
408
409 odev->last_val = val;
410 odev->buf_offs++;
411 }
412
413 return 0;
414}
415
Kevin A. Granade1ff12a42009-09-05 01:03:39 -0500416static ssize_t odev_set_picture(struct asus_oled_dev *odev,
417 const char *buf, size_t count)
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800418{
419 size_t offs = 0, max_offs;
420
Andre Haupt526fd202009-01-19 12:00:18 +0100421 if (count < 1)
422 return 0;
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800423
Andre Haupt118015c2009-01-19 12:00:19 +0100424 if (tolower(buf[0]) == 'b') {
Kevin A. Granade1ff12a42009-09-05 01:03:39 -0500425 /* binary mode, set the entire memory*/
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800426
Kevin A. Granade1ff12a42009-09-05 01:03:39 -0500427 size_t i;
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800428
Kevin A. Granade1ff12a42009-09-05 01:03:39 -0500429 odev->buf_size = (odev->dev_width * ASUS_OLED_DISP_HEIGHT) / 8;
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800430
Kevin A. Granade1ff12a42009-09-05 01:03:39 -0500431 kfree(odev->buf);
432 odev->buf = kmalloc(odev->buf_size, GFP_KERNEL);
Peter Huewe0df28be2010-01-07 19:57:36 +0100433 if (odev->buf == NULL) {
434 odev->buf_size = 0;
435 printk(ASUS_OLED_ERROR "Out of memory!\n");
436 return -ENOMEM;
437 }
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800438
Kevin A. Granade1ff12a42009-09-05 01:03:39 -0500439 memset(odev->buf, 0xff, odev->buf_size);
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800440
Kevin A. Granade1ff12a42009-09-05 01:03:39 -0500441 for (i = 1; i < count && i <= 32 * 32; i++) {
442 odev->buf[i-1] = buf[i];
443 odev->buf_offs = i-1;
444 }
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800445
Kevin A. Granade1ff12a42009-09-05 01:03:39 -0500446 odev->width = odev->dev_width / 8;
447 odev->height = ASUS_OLED_DISP_HEIGHT;
448 odev->x_shift = 0;
449 odev->y_shift = 0;
450 odev->last_val = 0;
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800451
Kevin A. Granade1ff12a42009-09-05 01:03:39 -0500452 send_data(odev);
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800453
Kevin A. Granade1ff12a42009-09-05 01:03:39 -0500454 return count;
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800455 }
456
457 if (buf[0] == '<') {
458 size_t i;
459 size_t w = 0, h = 0;
460 size_t w_mem, h_mem;
461
Kevin A. Granade1ff12a42009-09-05 01:03:39 -0500462 if (count < 10 || buf[2] != ':')
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800463 goto error_header;
Kevin A. Granade1ff12a42009-09-05 01:03:39 -0500464
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800465
Andre Haupt118015c2009-01-19 12:00:19 +0100466 switch (tolower(buf[1])) {
Kevin A. Granade1ff12a42009-09-05 01:03:39 -0500467 case ASUS_OLED_STATIC:
468 case ASUS_OLED_ROLL:
469 case ASUS_OLED_FLASH:
470 odev->pic_mode = buf[1];
471 break;
472 default:
473 printk(ASUS_OLED_ERROR "Wrong picture mode: '%c'.\n",
474 buf[1]);
475 return -EIO;
476 break;
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800477 }
478
479 for (i = 3; i < count; ++i) {
480 if (buf[i] >= '0' && buf[i] <= '9') {
481 w = 10*w + (buf[i] - '0');
482
Andre Haupt526fd202009-01-19 12:00:18 +0100483 if (w > ASUS_OLED_MAX_WIDTH)
484 goto error_width;
Kevin A. Granade1ff12a42009-09-05 01:03:39 -0500485 } else if (tolower(buf[i]) == 'x') {
Andre Haupt526fd202009-01-19 12:00:18 +0100486 break;
Kevin A. Granade1ff12a42009-09-05 01:03:39 -0500487 } else {
Andre Haupt526fd202009-01-19 12:00:18 +0100488 goto error_width;
Kevin A. Granade1ff12a42009-09-05 01:03:39 -0500489 }
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800490 }
491
492 for (++i; i < count; ++i) {
493 if (buf[i] >= '0' && buf[i] <= '9') {
494 h = 10*h + (buf[i] - '0');
495
Andre Haupt526fd202009-01-19 12:00:18 +0100496 if (h > ASUS_OLED_DISP_HEIGHT)
497 goto error_height;
Kevin A. Granade1ff12a42009-09-05 01:03:39 -0500498 } else if (tolower(buf[i]) == '>') {
Andre Haupt526fd202009-01-19 12:00:18 +0100499 break;
Kevin A. Granade1ff12a42009-09-05 01:03:39 -0500500 } else {
Andre Haupt526fd202009-01-19 12:00:18 +0100501 goto error_height;
Kevin A. Granade1ff12a42009-09-05 01:03:39 -0500502 }
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800503 }
504
Andre Haupt526fd202009-01-19 12:00:18 +0100505 if (w < 1 || w > ASUS_OLED_MAX_WIDTH)
506 goto error_width;
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800507
Andre Haupt526fd202009-01-19 12:00:18 +0100508 if (h < 1 || h > ASUS_OLED_DISP_HEIGHT)
509 goto error_height;
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800510
Andre Haupt526fd202009-01-19 12:00:18 +0100511 if (i >= count || buf[i] != '>')
512 goto error_header;
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800513
514 offs = i+1;
515
516 if (w % (odev->dev_width) != 0)
517 w_mem = (w/(odev->dev_width) + 1)*(odev->dev_width);
518 else
519 w_mem = w;
520
521 if (h < ASUS_OLED_DISP_HEIGHT)
522 h_mem = ASUS_OLED_DISP_HEIGHT;
523 else
524 h_mem = h;
525
526 odev->buf_size = w_mem * h_mem / 8;
527
Kevin A. Granade1ff12a42009-09-05 01:03:39 -0500528 kfree(odev->buf);
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800529 odev->buf = kmalloc(odev->buf_size, GFP_KERNEL);
530
531 if (odev->buf == NULL) {
532 odev->buf_size = 0;
533 printk(ASUS_OLED_ERROR "Out of memory!\n");
534 return -ENOMEM;
535 }
536
537 memset(odev->buf, 0xff, odev->buf_size);
538
539 odev->buf_offs = 0;
540 odev->width = w;
541 odev->height = h;
542 odev->x_shift = 0;
543 odev->y_shift = 0;
544 odev->last_val = 0;
545
546 if (odev->pic_mode == ASUS_OLED_FLASH) {
547 if (h < ASUS_OLED_DISP_HEIGHT/2)
548 odev->y_shift = (ASUS_OLED_DISP_HEIGHT/2 - h)/2;
Kevin A. Granade1ff12a42009-09-05 01:03:39 -0500549 } else {
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800550 if (h < ASUS_OLED_DISP_HEIGHT)
551 odev->y_shift = (ASUS_OLED_DISP_HEIGHT - h)/2;
552 }
553
554 if (w < (odev->dev_width))
555 odev->x_shift = ((odev->dev_width) - w)/2;
556 }
557
558 max_offs = odev->width * odev->height;
559
560 while (offs < count && odev->buf_offs < max_offs) {
Greg Kroah-Hartman0d99b6e2009-06-04 11:29:54 -0700561 int ret = 0;
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800562
563 if (buf[offs] == '1' || buf[offs] == '#') {
Andre Haupt94aa5b42009-01-19 12:00:20 +0100564 ret = append_values(odev, 1, 1);
565 if (ret < 0)
Andre Haupt526fd202009-01-19 12:00:18 +0100566 return ret;
Kevin A. Granade1ff12a42009-09-05 01:03:39 -0500567 } else if (buf[offs] == '0' || buf[offs] == ' ') {
Andre Haupt94aa5b42009-01-19 12:00:20 +0100568 ret = append_values(odev, 0, 1);
569 if (ret < 0)
Andre Haupt526fd202009-01-19 12:00:18 +0100570 return ret;
Kevin A. Granade1ff12a42009-09-05 01:03:39 -0500571 } else if (buf[offs] == '\n') {
572 /* New line detected. Lets assume, that all characters
573 till the end of the line were equal to the last
574 character in this line.*/
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800575 if (odev->buf_offs % odev->width != 0)
Andre Haupt94aa5b42009-01-19 12:00:20 +0100576 ret = append_values(odev, odev->last_val,
Kevin A. Granade1ff12a42009-09-05 01:03:39 -0500577 odev->width -
578 (odev->buf_offs %
579 odev->width));
580 if (ret < 0)
581 return ret;
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800582 }
583
584 offs++;
585 }
586
Andre Haupt526fd202009-01-19 12:00:18 +0100587 if (odev->buf_offs >= max_offs)
588 send_data(odev);
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800589
590 return count;
591
592error_width:
593 printk(ASUS_OLED_ERROR "Wrong picture width specified.\n");
594 return -EIO;
595
596error_height:
597 printk(ASUS_OLED_ERROR "Wrong picture height specified.\n");
598 return -EIO;
599
600error_header:
601 printk(ASUS_OLED_ERROR "Wrong picture header.\n");
602 return -EIO;
603}
604
Kevin A. Granade1ff12a42009-09-05 01:03:39 -0500605static ssize_t set_picture(struct device *dev, struct device_attribute *attr,
606 const char *buf, size_t count)
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800607{
608 struct usb_interface *intf = to_usb_interface(dev);
609
610 return odev_set_picture(usb_get_intfdata(intf), buf, count);
611}
612
Kevin A. Granade1ff12a42009-09-05 01:03:39 -0500613static ssize_t class_set_picture(struct device *device,
614 struct device_attribute *attr,
615 const char *buf, size_t count)
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800616{
Kevin A. Granade1ff12a42009-09-05 01:03:39 -0500617 return odev_set_picture((struct asus_oled_dev *)
618 dev_get_drvdata(device), buf, count);
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800619}
620
621#define ASUS_OLED_DEVICE_ATTR(_file) dev_attr_asus_oled_##_file
622
Greg Kroah-Hartman515b4982010-11-18 11:21:04 -0800623static DEVICE_ATTR(asus_oled_enabled, S_IWUSR | S_IRUGO,
Kevin A. Granade1ff12a42009-09-05 01:03:39 -0500624 get_enabled, set_enabled);
Greg Kroah-Hartman515b4982010-11-18 11:21:04 -0800625static DEVICE_ATTR(asus_oled_picture, S_IWUSR , NULL, set_picture);
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800626
Greg Kroah-Hartman515b4982010-11-18 11:21:04 -0800627static DEVICE_ATTR(enabled, S_IWUSR | S_IRUGO,
Kevin A. Granade1ff12a42009-09-05 01:03:39 -0500628 class_get_enabled, class_set_enabled);
Greg Kroah-Hartman515b4982010-11-18 11:21:04 -0800629static DEVICE_ATTR(picture, S_IWUSR, NULL, class_set_picture);
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800630
Kevin A. Granade1ff12a42009-09-05 01:03:39 -0500631static int asus_oled_probe(struct usb_interface *interface,
632 const struct usb_device_id *id)
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800633{
634 struct usb_device *udev = interface_to_usbdev(interface);
635 struct asus_oled_dev *odev = NULL;
636 int retval = -ENOMEM;
637 uint16_t dev_width = 0;
Kevin A. Granade1ff12a42009-09-05 01:03:39 -0500638 enum oled_pack_mode pack_mode = PACK_MODE_LAST;
639 const struct oled_dev_desc_str *dev_desc = oled_dev_desc_table;
Andre Hauptccdb2452009-01-19 12:00:16 +0100640 const char *desc = NULL;
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800641
Andre Hauptccdb2452009-01-19 12:00:16 +0100642 if (!id) {
Kevin A. Granade1ff12a42009-09-05 01:03:39 -0500643 /* Even possible? Just to make sure...*/
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800644 dev_err(&interface->dev, "No usb_device_id provided!\n");
645 return -ENODEV;
646 }
647
Kevin A. Granade1ff12a42009-09-05 01:03:39 -0500648 for (; dev_desc->idVendor; dev_desc++) {
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800649 if (dev_desc->idVendor == id->idVendor
Kevin A. Granade1ff12a42009-09-05 01:03:39 -0500650 && dev_desc->idProduct == id->idProduct) {
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800651 dev_width = dev_desc->devWidth;
652 desc = dev_desc->devDesc;
653 pack_mode = dev_desc->packMode;
654 break;
655 }
656 }
657
Andre Haupt118015c2009-01-19 12:00:19 +0100658 if (!desc || dev_width < 1 || pack_mode == PACK_MODE_LAST) {
Kevin A. Granade1ff12a42009-09-05 01:03:39 -0500659 dev_err(&interface->dev,
660 "Missing or incomplete device description!\n");
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800661 return -ENODEV;
662 }
663
664 odev = kzalloc(sizeof(struct asus_oled_dev), GFP_KERNEL);
665
666 if (odev == NULL) {
667 dev_err(&interface->dev, "Out of memory\n");
668 return -ENOMEM;
669 }
670
671 odev->udev = usb_get_dev(udev);
672 odev->pic_mode = ASUS_OLED_STATIC;
673 odev->dev_width = dev_width;
674 odev->pack_mode = pack_mode;
675 odev->height = 0;
676 odev->width = 0;
677 odev->x_shift = 0;
678 odev->y_shift = 0;
679 odev->buf_offs = 0;
680 odev->buf_size = 0;
681 odev->last_val = 0;
682 odev->buf = NULL;
683 odev->enabled = 1;
Andre Hauptccdb2452009-01-19 12:00:16 +0100684 odev->dev = NULL;
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800685
Andre Haupt118015c2009-01-19 12:00:19 +0100686 usb_set_intfdata(interface, odev);
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800687
Kevin A. Granade1ff12a42009-09-05 01:03:39 -0500688 retval = device_create_file(&interface->dev,
689 &ASUS_OLED_DEVICE_ATTR(enabled));
Andre Haupt94aa5b42009-01-19 12:00:20 +0100690 if (retval)
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800691 goto err_files;
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800692
Kevin A. Granade1ff12a42009-09-05 01:03:39 -0500693 retval = device_create_file(&interface->dev,
694 &ASUS_OLED_DEVICE_ATTR(picture));
Andre Haupt94aa5b42009-01-19 12:00:20 +0100695 if (retval)
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800696 goto err_files;
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800697
Andre Haupt118015c2009-01-19 12:00:19 +0100698 odev->dev = device_create(oled_class, &interface->dev, MKDEV(0, 0),
Kevin A. Granade1ff12a42009-09-05 01:03:39 -0500699 NULL, "oled_%d", ++oled_num);
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800700
701 if (IS_ERR(odev->dev)) {
702 retval = PTR_ERR(odev->dev);
703 goto err_files;
704 }
705
706 dev_set_drvdata(odev->dev, odev);
707
Andre Haupt94aa5b42009-01-19 12:00:20 +0100708 retval = device_create_file(odev->dev, &dev_attr_enabled);
709 if (retval)
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800710 goto err_class_enabled;
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800711
Andre Haupt94aa5b42009-01-19 12:00:20 +0100712 retval = device_create_file(odev->dev, &dev_attr_picture);
713 if (retval)
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800714 goto err_class_picture;
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800715
Kevin A. Granade1ff12a42009-09-05 01:03:39 -0500716 dev_info(&interface->dev,
717 "Attached Asus OLED device: %s [width %u, pack_mode %d]\n",
718 desc, odev->dev_width, odev->pack_mode);
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800719
720 if (start_off)
721 enable_oled(odev, 0);
722
723 return 0;
724
725err_class_picture:
726 device_remove_file(odev->dev, &dev_attr_picture);
727
728err_class_enabled:
729 device_remove_file(odev->dev, &dev_attr_enabled);
730 device_unregister(odev->dev);
731
732err_files:
733 device_remove_file(&interface->dev, &ASUS_OLED_DEVICE_ATTR(enabled));
734 device_remove_file(&interface->dev, &ASUS_OLED_DEVICE_ATTR(picture));
735
Andre Haupt118015c2009-01-19 12:00:19 +0100736 usb_set_intfdata(interface, NULL);
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800737 usb_put_dev(odev->udev);
738 kfree(odev);
739
740 return retval;
741}
742
743static void asus_oled_disconnect(struct usb_interface *interface)
744{
745 struct asus_oled_dev *odev;
746
Andre Haupt118015c2009-01-19 12:00:19 +0100747 odev = usb_get_intfdata(interface);
748 usb_set_intfdata(interface, NULL);
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800749
750 device_remove_file(odev->dev, &dev_attr_picture);
751 device_remove_file(odev->dev, &dev_attr_enabled);
752 device_unregister(odev->dev);
753
Andre Haupt118015c2009-01-19 12:00:19 +0100754 device_remove_file(&interface->dev, &ASUS_OLED_DEVICE_ATTR(picture));
755 device_remove_file(&interface->dev, &ASUS_OLED_DEVICE_ATTR(enabled));
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800756
757 usb_put_dev(odev->udev);
758
Kevin A. Granade1ff12a42009-09-05 01:03:39 -0500759 kfree(odev->buf);
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800760
761 kfree(odev);
762
763 dev_info(&interface->dev, "Disconnected Asus OLED device\n");
764}
765
766static struct usb_driver oled_driver = {
767 .name = ASUS_OLED_NAME,
768 .probe = asus_oled_probe,
769 .disconnect = asus_oled_disconnect,
770 .id_table = id_table,
771};
772
Andi Kleen0933e2d2010-01-05 12:48:09 +0100773static CLASS_ATTR_STRING(version, S_IRUGO,
Andrea Gelminie67fdbc2010-03-01 00:28:47 +0100774 ASUS_OLED_UNDERSCORE_NAME " " ASUS_OLED_VERSION);
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800775
776static int __init asus_oled_init(void)
777{
778 int retval = 0;
779 oled_class = class_create(THIS_MODULE, ASUS_OLED_UNDERSCORE_NAME);
780
781 if (IS_ERR(oled_class)) {
782 err("Error creating " ASUS_OLED_UNDERSCORE_NAME " class");
783 return PTR_ERR(oled_class);
784 }
785
Andi Kleen0933e2d2010-01-05 12:48:09 +0100786 retval = class_create_file(oled_class, &class_attr_version.attr);
Andre Haupt94aa5b42009-01-19 12:00:20 +0100787 if (retval) {
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800788 err("Error creating class version file");
789 goto error;
790 }
791
792 retval = usb_register(&oled_driver);
793
794 if (retval) {
795 err("usb_register failed. Error number %d", retval);
796 goto error;
797 }
798
799 return retval;
800
801error:
802 class_destroy(oled_class);
803 return retval;
804}
805
806static void __exit asus_oled_exit(void)
807{
Andi Kleen0933e2d2010-01-05 12:48:09 +0100808 class_remove_file(oled_class, &class_attr_version.attr);
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800809 class_destroy(oled_class);
810
811 usb_deregister(&oled_driver);
812}
813
Andre Haupt118015c2009-01-19 12:00:19 +0100814module_init(asus_oled_init);
815module_exit(asus_oled_exit);
Jakub Schmidtkefb534402008-11-04 23:46:58 -0800816