4 * Copyright (C) 2007,2008 Jakub Schmidtke (sjakub@gmail.com)
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.
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.
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
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 * This module is based on usbled and asus-laptop modules.
26 * Asus OLED support is based on asusoled program taken from
27 * https://launchpad.net/asusoled/.
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>
41 #define ASUS_OLED_VERSION "0.04-dev"
42 #define ASUS_OLED_NAME "asus-oled"
43 #define ASUS_OLED_UNDERSCORE_NAME "asus_oled"
45 #define ASUS_OLED_ERROR "Asus OLED Display Error: "
47 #define ASUS_OLED_STATIC 's'
48 #define ASUS_OLED_ROLL 'r'
49 #define ASUS_OLED_FLASH 'f'
51 #define ASUS_OLED_MAX_WIDTH 1792
52 #define ASUS_OLED_DISP_HEIGHT 32
53 #define ASUS_OLED_PACKET_BUF_SIZE 256
55 MODULE_AUTHOR("Jakub Schmidtke, sjakub@gmail.com");
56 MODULE_DESCRIPTION("Asus OLED Driver v" ASUS_OLED_VERSION);
57 MODULE_LICENSE("GPL");
59 static struct class *oled_class;
62 static uint start_off;
64 module_param(start_off, uint, 0644);
66 MODULE_PARM_DESC(start_off,
67 "Set to 1 to switch off OLED display after it is attached");
75 struct oled_dev_desc_str {
78 /* width of display */
80 /* formula to be used while packing the picture */
81 enum oled_pack_mode packMode;
85 /* table of devices that work with this driver */
86 static struct usb_device_id id_table[] = {
87 /* Asus G1/G2 (and variants)*/
88 { USB_DEVICE(0x0b05, 0x1726) },
89 /* Asus G50V (and possibly others - G70? G71?)*/
90 { USB_DEVICE(0x0b05, 0x175b) },
94 /* parameters of specific devices */
95 static struct oled_dev_desc_str oled_dev_desc_table[] = {
96 { 0x0b05, 0x1726, 128, PACK_MODE_G1, "G1/G2" },
97 { 0x0b05, 0x175b, 256, PACK_MODE_G50, "G50" },
101 MODULE_DEVICE_TABLE(usb, id_table);
103 struct asus_oled_header {
114 } __attribute((packed));
116 struct asus_oled_packet {
117 struct asus_oled_header header;
118 uint8_t bitmap[ASUS_OLED_PACKET_BUF_SIZE];
119 } __attribute((packed));
121 struct asus_oled_dev {
122 struct usb_device *udev;
125 enum oled_pack_mode pack_mode;
138 static void setup_packet_header(struct asus_oled_packet *packet, char flags,
139 char value3, char buffer1, char buffer2, char value6,
140 char value7, char value8)
142 memset(packet, 0, sizeof(struct asus_oled_header));
143 packet->header.magic1 = 0x55;
144 packet->header.magic2 = 0xaa;
145 packet->header.flags = flags;
146 packet->header.value3 = value3;
147 packet->header.buffer1 = buffer1;
148 packet->header.buffer2 = buffer2;
149 packet->header.value6 = value6;
150 packet->header.value7 = value7;
151 packet->header.value8 = value8;
154 static void enable_oled(struct asus_oled_dev *odev, uint8_t enabl)
159 struct asus_oled_packet *packet;
161 packet = kzalloc(sizeof(struct asus_oled_packet), GFP_KERNEL);
164 dev_err(&odev->udev->dev, "out of memory\n");
168 setup_packet_header(packet, 0x20, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00);
171 packet->bitmap[0] = 0xaf;
173 packet->bitmap[0] = 0xae;
175 for (a = 0; a < 1; a++) {
176 retval = usb_bulk_msg(odev->udev,
177 usb_sndbulkpipe(odev->udev, 2),
179 sizeof(struct asus_oled_header) + 1,
184 dev_dbg(&odev->udev->dev, "retval = %d\n", retval);
187 odev->enabled = enabl;
192 static ssize_t set_enabled(struct device *dev, struct device_attribute *attr,
193 const char *buf, size_t count)
195 struct usb_interface *intf = to_usb_interface(dev);
196 struct asus_oled_dev *odev = usb_get_intfdata(intf);
197 int temp = strict_strtoul(buf, 10, NULL);
199 enable_oled(odev, temp);
204 static ssize_t class_set_enabled(struct device *device,
205 struct device_attribute *attr,
206 const char *buf, size_t count)
208 struct asus_oled_dev *odev =
209 (struct asus_oled_dev *) dev_get_drvdata(device);
211 int temp = strict_strtoul(buf, 10, NULL);
213 enable_oled(odev, temp);
218 static ssize_t get_enabled(struct device *dev, struct device_attribute *attr,
221 struct usb_interface *intf = to_usb_interface(dev);
222 struct asus_oled_dev *odev = usb_get_intfdata(intf);
224 return sprintf(buf, "%d\n", odev->enabled);
227 static ssize_t class_get_enabled(struct device *device,
228 struct device_attribute *attr, char *buf)
230 struct asus_oled_dev *odev =
231 (struct asus_oled_dev *) dev_get_drvdata(device);
233 return sprintf(buf, "%d\n", odev->enabled);
236 static void send_packets(struct usb_device *udev,
237 struct asus_oled_packet *packet,
238 char *buf, uint8_t p_type, size_t p_num)
243 for (i = 0; i < p_num; i++) {
248 setup_packet_header(packet, 0x40, 0x80, p_num,
249 i + 1, 0x00, 0x01, 0xff);
251 case ASUS_OLED_STATIC:
252 setup_packet_header(packet, 0x10 + i, 0x80, 0x01,
253 0x01, 0x00, 0x01, 0x00);
255 case ASUS_OLED_FLASH:
256 setup_packet_header(packet, 0x10 + i, 0x80, 0x01,
257 0x01, 0x00, 0x00, 0xff);
261 memcpy(packet->bitmap, buf + (ASUS_OLED_PACKET_BUF_SIZE*i),
262 ASUS_OLED_PACKET_BUF_SIZE);
264 retval = usb_bulk_msg(udev, usb_sndctrlpipe(udev, 2),
265 packet, sizeof(struct asus_oled_packet),
269 dev_dbg(&udev->dev, "retval = %d\n", retval);
273 static void send_packet(struct usb_device *udev,
274 struct asus_oled_packet *packet,
275 size_t offset, size_t len, char *buf, uint8_t b1,
276 uint8_t b2, uint8_t b3, uint8_t b4, uint8_t b5,
281 setup_packet_header(packet, b1, b2, b3, b4, b5, b6, 0x00);
282 memcpy(packet->bitmap, buf + offset, len);
284 retval = usb_bulk_msg(udev,
285 usb_sndctrlpipe(udev, 2),
287 sizeof(struct asus_oled_packet),
292 dev_dbg(&udev->dev, "retval = %d\n", retval);
296 static void send_packets_g50(struct usb_device *udev,
297 struct asus_oled_packet *packet, char *buf)
299 send_packet(udev, packet, 0, 0x100, buf,
300 0x10, 0x00, 0x02, 0x01, 0x00, 0x01);
301 send_packet(udev, packet, 0x100, 0x080, buf,
302 0x10, 0x00, 0x02, 0x02, 0x80, 0x00);
304 send_packet(udev, packet, 0x180, 0x100, buf,
305 0x11, 0x00, 0x03, 0x01, 0x00, 0x01);
306 send_packet(udev, packet, 0x280, 0x100, buf,
307 0x11, 0x00, 0x03, 0x02, 0x00, 0x01);
308 send_packet(udev, packet, 0x380, 0x080, buf,
309 0x11, 0x00, 0x03, 0x03, 0x80, 0x00);
313 static void send_data(struct asus_oled_dev *odev)
315 size_t packet_num = odev->buf_size / ASUS_OLED_PACKET_BUF_SIZE;
316 struct asus_oled_packet *packet;
318 packet = kzalloc(sizeof(struct asus_oled_packet), GFP_KERNEL);
321 dev_err(&odev->udev->dev, "out of memory\n");
325 if (odev->pack_mode == PACK_MODE_G1) {
326 /* When sending roll-mode data the display updated only
327 first packet. I have no idea why, but when static picture
328 is sent just before rolling picture everything works fine. */
329 if (odev->pic_mode == ASUS_OLED_ROLL)
330 send_packets(odev->udev, packet, odev->buf,
331 ASUS_OLED_STATIC, 2);
333 /* Only ROLL mode can use more than 2 packets.*/
334 if (odev->pic_mode != ASUS_OLED_ROLL && packet_num > 2)
337 send_packets(odev->udev, packet, odev->buf,
338 odev->pic_mode, packet_num);
339 } else if (odev->pack_mode == PACK_MODE_G50) {
340 send_packets_g50(odev->udev, packet, odev->buf);
346 static int append_values(struct asus_oled_dev *odev, uint8_t val, size_t count)
348 while (count-- > 0 && val) {
349 size_t x = odev->buf_offs % odev->width;
350 size_t y = odev->buf_offs / odev->width;
356 switch (odev->pack_mode) {
358 /* i = (x/128)*640 + 127 - x + (y/8)*128;
359 This one for 128 is the same, but might be better
360 for different widths? */
361 i = (x/odev->dev_width)*640 +
362 odev->dev_width - 1 - x +
363 (y/8)*odev->dev_width;
367 i = (odev->dev_width - 1 - x)/8 + y*odev->dev_width/8;
372 printk(ASUS_OLED_ERROR "Unknown OLED Pack Mode: %d!\n",
377 if (i >= odev->buf_size) {
378 printk(ASUS_OLED_ERROR "Buffer overflow! Report a bug:"
379 "offs: %d >= %d i: %d (x: %d y: %d)\n",
380 (int) odev->buf_offs, (int) odev->buf_size,
381 (int) i, (int) x, (int) y);
385 switch (odev->pack_mode) {
387 odev->buf[i] &= ~(1<<(y%8));
391 odev->buf[i] &= ~(1<<(x%8));
395 /* cannot get here; stops gcc complaining*/
399 odev->last_val = val;
406 static ssize_t odev_set_picture(struct asus_oled_dev *odev,
407 const char *buf, size_t count)
409 size_t offs = 0, max_offs;
414 if (tolower(buf[0]) == 'b') {
415 /* binary mode, set the entire memory*/
419 odev->buf_size = (odev->dev_width * ASUS_OLED_DISP_HEIGHT) / 8;
422 odev->buf = kmalloc(odev->buf_size, GFP_KERNEL);
424 memset(odev->buf, 0xff, odev->buf_size);
426 for (i = 1; i < count && i <= 32 * 32; i++) {
427 odev->buf[i-1] = buf[i];
428 odev->buf_offs = i-1;
431 odev->width = odev->dev_width / 8;
432 odev->height = ASUS_OLED_DISP_HEIGHT;
447 if (count < 10 || buf[2] != ':')
451 switch (tolower(buf[1])) {
452 case ASUS_OLED_STATIC:
454 case ASUS_OLED_FLASH:
455 odev->pic_mode = buf[1];
458 printk(ASUS_OLED_ERROR "Wrong picture mode: '%c'.\n",
464 for (i = 3; i < count; ++i) {
465 if (buf[i] >= '0' && buf[i] <= '9') {
466 w = 10*w + (buf[i] - '0');
468 if (w > ASUS_OLED_MAX_WIDTH)
470 } else if (tolower(buf[i]) == 'x') {
477 for (++i; i < count; ++i) {
478 if (buf[i] >= '0' && buf[i] <= '9') {
479 h = 10*h + (buf[i] - '0');
481 if (h > ASUS_OLED_DISP_HEIGHT)
483 } else if (tolower(buf[i]) == '>') {
490 if (w < 1 || w > ASUS_OLED_MAX_WIDTH)
493 if (h < 1 || h > ASUS_OLED_DISP_HEIGHT)
496 if (i >= count || buf[i] != '>')
501 if (w % (odev->dev_width) != 0)
502 w_mem = (w/(odev->dev_width) + 1)*(odev->dev_width);
506 if (h < ASUS_OLED_DISP_HEIGHT)
507 h_mem = ASUS_OLED_DISP_HEIGHT;
511 odev->buf_size = w_mem * h_mem / 8;
514 odev->buf = kmalloc(odev->buf_size, GFP_KERNEL);
516 if (odev->buf == NULL) {
518 printk(ASUS_OLED_ERROR "Out of memory!\n");
522 memset(odev->buf, 0xff, odev->buf_size);
531 if (odev->pic_mode == ASUS_OLED_FLASH) {
532 if (h < ASUS_OLED_DISP_HEIGHT/2)
533 odev->y_shift = (ASUS_OLED_DISP_HEIGHT/2 - h)/2;
535 if (h < ASUS_OLED_DISP_HEIGHT)
536 odev->y_shift = (ASUS_OLED_DISP_HEIGHT - h)/2;
539 if (w < (odev->dev_width))
540 odev->x_shift = ((odev->dev_width) - w)/2;
543 max_offs = odev->width * odev->height;
545 while (offs < count && odev->buf_offs < max_offs) {
548 if (buf[offs] == '1' || buf[offs] == '#') {
549 ret = append_values(odev, 1, 1);
552 } else if (buf[offs] == '0' || buf[offs] == ' ') {
553 ret = append_values(odev, 0, 1);
556 } else if (buf[offs] == '\n') {
557 /* New line detected. Lets assume, that all characters
558 till the end of the line were equal to the last
559 character in this line.*/
560 if (odev->buf_offs % odev->width != 0)
561 ret = append_values(odev, odev->last_val,
572 if (odev->buf_offs >= max_offs)
578 printk(ASUS_OLED_ERROR "Wrong picture width specified.\n");
582 printk(ASUS_OLED_ERROR "Wrong picture height specified.\n");
586 printk(ASUS_OLED_ERROR "Wrong picture header.\n");
590 static ssize_t set_picture(struct device *dev, struct device_attribute *attr,
591 const char *buf, size_t count)
593 struct usb_interface *intf = to_usb_interface(dev);
595 return odev_set_picture(usb_get_intfdata(intf), buf, count);
598 static ssize_t class_set_picture(struct device *device,
599 struct device_attribute *attr,
600 const char *buf, size_t count)
602 return odev_set_picture((struct asus_oled_dev *)
603 dev_get_drvdata(device), buf, count);
606 #define ASUS_OLED_DEVICE_ATTR(_file) dev_attr_asus_oled_##_file
608 static DEVICE_ATTR(asus_oled_enabled, S_IWUGO | S_IRUGO,
609 get_enabled, set_enabled);
610 static DEVICE_ATTR(asus_oled_picture, S_IWUGO , NULL, set_picture);
612 static DEVICE_ATTR(enabled, S_IWUGO | S_IRUGO,
613 class_get_enabled, class_set_enabled);
614 static DEVICE_ATTR(picture, S_IWUGO, NULL, class_set_picture);
616 static int asus_oled_probe(struct usb_interface *interface,
617 const struct usb_device_id *id)
619 struct usb_device *udev = interface_to_usbdev(interface);
620 struct asus_oled_dev *odev = NULL;
621 int retval = -ENOMEM;
622 uint16_t dev_width = 0;
623 enum oled_pack_mode pack_mode = PACK_MODE_LAST;
624 const struct oled_dev_desc_str *dev_desc = oled_dev_desc_table;
625 const char *desc = NULL;
628 /* Even possible? Just to make sure...*/
629 dev_err(&interface->dev, "No usb_device_id provided!\n");
633 for (; dev_desc->idVendor; dev_desc++) {
634 if (dev_desc->idVendor == id->idVendor
635 && dev_desc->idProduct == id->idProduct) {
636 dev_width = dev_desc->devWidth;
637 desc = dev_desc->devDesc;
638 pack_mode = dev_desc->packMode;
643 if (!desc || dev_width < 1 || pack_mode == PACK_MODE_LAST) {
644 dev_err(&interface->dev,
645 "Missing or incomplete device description!\n");
649 odev = kzalloc(sizeof(struct asus_oled_dev), GFP_KERNEL);
652 dev_err(&interface->dev, "Out of memory\n");
656 odev->udev = usb_get_dev(udev);
657 odev->pic_mode = ASUS_OLED_STATIC;
658 odev->dev_width = dev_width;
659 odev->pack_mode = pack_mode;
671 usb_set_intfdata(interface, odev);
673 retval = device_create_file(&interface->dev,
674 &ASUS_OLED_DEVICE_ATTR(enabled));
678 retval = device_create_file(&interface->dev,
679 &ASUS_OLED_DEVICE_ATTR(picture));
683 odev->dev = device_create(oled_class, &interface->dev, MKDEV(0, 0),
684 NULL, "oled_%d", ++oled_num);
686 if (IS_ERR(odev->dev)) {
687 retval = PTR_ERR(odev->dev);
691 dev_set_drvdata(odev->dev, odev);
693 retval = device_create_file(odev->dev, &dev_attr_enabled);
695 goto err_class_enabled;
697 retval = device_create_file(odev->dev, &dev_attr_picture);
699 goto err_class_picture;
701 dev_info(&interface->dev,
702 "Attached Asus OLED device: %s [width %u, pack_mode %d]\n",
703 desc, odev->dev_width, odev->pack_mode);
706 enable_oled(odev, 0);
711 device_remove_file(odev->dev, &dev_attr_picture);
714 device_remove_file(odev->dev, &dev_attr_enabled);
715 device_unregister(odev->dev);
718 device_remove_file(&interface->dev, &ASUS_OLED_DEVICE_ATTR(enabled));
719 device_remove_file(&interface->dev, &ASUS_OLED_DEVICE_ATTR(picture));
721 usb_set_intfdata(interface, NULL);
722 usb_put_dev(odev->udev);
728 static void asus_oled_disconnect(struct usb_interface *interface)
730 struct asus_oled_dev *odev;
732 odev = usb_get_intfdata(interface);
733 usb_set_intfdata(interface, NULL);
735 device_remove_file(odev->dev, &dev_attr_picture);
736 device_remove_file(odev->dev, &dev_attr_enabled);
737 device_unregister(odev->dev);
739 device_remove_file(&interface->dev, &ASUS_OLED_DEVICE_ATTR(picture));
740 device_remove_file(&interface->dev, &ASUS_OLED_DEVICE_ATTR(enabled));
742 usb_put_dev(odev->udev);
748 dev_info(&interface->dev, "Disconnected Asus OLED device\n");
751 static struct usb_driver oled_driver = {
752 .name = ASUS_OLED_NAME,
753 .probe = asus_oled_probe,
754 .disconnect = asus_oled_disconnect,
755 .id_table = id_table,
758 static ssize_t version_show(struct class *dev, char *buf)
760 return sprintf(buf, ASUS_OLED_UNDERSCORE_NAME " %s\n",
764 static CLASS_ATTR(version, S_IRUGO, version_show, NULL);
766 static int __init asus_oled_init(void)
769 oled_class = class_create(THIS_MODULE, ASUS_OLED_UNDERSCORE_NAME);
771 if (IS_ERR(oled_class)) {
772 err("Error creating " ASUS_OLED_UNDERSCORE_NAME " class");
773 return PTR_ERR(oled_class);
776 retval = class_create_file(oled_class, &class_attr_version);
778 err("Error creating class version file");
782 retval = usb_register(&oled_driver);
785 err("usb_register failed. Error number %d", retval);
792 class_destroy(oled_class);
796 static void __exit asus_oled_exit(void)
798 class_remove_file(oled_class, &class_attr_version);
799 class_destroy(oled_class);
801 usb_deregister(&oled_driver);
804 module_init(asus_oled_init);
805 module_exit(asus_oled_exit);