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 * <http://lapsus.berlios.de/asus_oled.html>.
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_STATIC 's'
46 #define ASUS_OLED_ROLL 'r'
47 #define ASUS_OLED_FLASH 'f'
49 #define ASUS_OLED_MAX_WIDTH 1792
50 #define ASUS_OLED_DISP_HEIGHT 32
51 #define ASUS_OLED_PACKET_BUF_SIZE 256
53 #define USB_VENDOR_ID_ASUS 0x0b05
54 #define USB_DEVICE_ID_ASUS_LCM 0x1726
55 #define USB_DEVICE_ID_ASUS_LCM2 0x175b
57 MODULE_AUTHOR("Jakub Schmidtke, sjakub@gmail.com");
58 MODULE_DESCRIPTION("Asus OLED Driver");
59 MODULE_LICENSE("GPL");
60 MODULE_VERSION(ASUS_OLED_VERSION);
62 static struct class *oled_class;
65 static uint start_off;
67 module_param(start_off, uint, 0644);
69 MODULE_PARM_DESC(start_off,
70 "Set to 1 to switch off OLED display after it is attached");
78 struct oled_dev_desc_str {
81 /* width of display */
83 /* formula to be used while packing the picture */
84 enum oled_pack_mode packMode;
88 /* table of devices that work with this driver */
89 static const struct usb_device_id id_table[] = {
90 /* Asus G1/G2 (and variants)*/
91 { USB_DEVICE(USB_VENDOR_ID_ASUS, USB_DEVICE_ID_ASUS_LCM) },
92 /* Asus G50V (and possibly others - G70? G71?)*/
93 { USB_DEVICE(USB_VENDOR_ID_ASUS, USB_DEVICE_ID_ASUS_LCM2) },
97 /* parameters of specific devices */
98 static struct oled_dev_desc_str oled_dev_desc_table[] = {
99 { USB_VENDOR_ID_ASUS, USB_DEVICE_ID_ASUS_LCM, 128, PACK_MODE_G1,
101 { USB_VENDOR_ID_ASUS, USB_DEVICE_ID_ASUS_LCM2, 256, PACK_MODE_G50,
106 MODULE_DEVICE_TABLE(usb, id_table);
108 struct asus_oled_header {
119 } __attribute((packed));
121 struct asus_oled_packet {
122 struct asus_oled_header header;
123 uint8_t bitmap[ASUS_OLED_PACKET_BUF_SIZE];
124 } __attribute((packed));
126 struct asus_oled_dev {
127 struct usb_device *udev;
130 enum oled_pack_mode pack_mode;
140 uint8_t enabled_post_resume;
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)
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;
160 static void enable_oled(struct asus_oled_dev *odev, uint8_t enabl)
164 struct asus_oled_packet *packet;
166 packet = kzalloc(sizeof(struct asus_oled_packet), GFP_KERNEL);
169 dev_err(&odev->udev->dev, "out of memory\n");
173 setup_packet_header(packet, 0x20, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00);
176 packet->bitmap[0] = 0xaf;
178 packet->bitmap[0] = 0xae;
180 retval = usb_bulk_msg(odev->udev,
181 usb_sndbulkpipe(odev->udev, 2),
183 sizeof(struct asus_oled_header) + 1,
188 dev_dbg(&odev->udev->dev, "retval = %d\n", retval);
190 odev->enabled = enabl;
195 static ssize_t set_enabled(struct device *dev, struct device_attribute *attr,
196 const char *buf, size_t count)
198 struct usb_interface *intf = to_usb_interface(dev);
199 struct asus_oled_dev *odev = usb_get_intfdata(intf);
201 if (kstrtoul(buf, 10, &value))
204 enable_oled(odev, value);
209 static ssize_t class_set_enabled(struct device *device,
210 struct device_attribute *attr,
211 const char *buf, size_t count)
213 struct asus_oled_dev *odev =
214 (struct asus_oled_dev *) dev_get_drvdata(device);
217 if (kstrtoul(buf, 10, &value))
220 enable_oled(odev, value);
225 static ssize_t get_enabled(struct device *dev, struct device_attribute *attr,
228 struct usb_interface *intf = to_usb_interface(dev);
229 struct asus_oled_dev *odev = usb_get_intfdata(intf);
231 return sprintf(buf, "%d\n", odev->enabled);
234 static ssize_t class_get_enabled(struct device *device,
235 struct device_attribute *attr, char *buf)
237 struct asus_oled_dev *odev =
238 (struct asus_oled_dev *) dev_get_drvdata(device);
240 return sprintf(buf, "%d\n", odev->enabled);
243 static void send_packets(struct usb_device *udev,
244 struct asus_oled_packet *packet,
245 char *buf, uint8_t p_type, size_t p_num)
250 for (i = 0; i < p_num; i++) {
255 setup_packet_header(packet, 0x40, 0x80, p_num,
256 i + 1, 0x00, 0x01, 0xff);
258 case ASUS_OLED_STATIC:
259 setup_packet_header(packet, 0x10 + i, 0x80, 0x01,
260 0x01, 0x00, 0x01, 0x00);
262 case ASUS_OLED_FLASH:
263 setup_packet_header(packet, 0x10 + i, 0x80, 0x01,
264 0x01, 0x00, 0x00, 0xff);
268 memcpy(packet->bitmap, buf + (ASUS_OLED_PACKET_BUF_SIZE*i),
269 ASUS_OLED_PACKET_BUF_SIZE);
271 retval = usb_bulk_msg(udev, usb_sndctrlpipe(udev, 2),
272 packet, sizeof(struct asus_oled_packet),
276 dev_dbg(&udev->dev, "retval = %d\n", retval);
280 static void send_packet(struct usb_device *udev,
281 struct asus_oled_packet *packet,
282 size_t offset, size_t len, char *buf, uint8_t b1,
283 uint8_t b2, uint8_t b3, uint8_t b4, uint8_t b5,
288 setup_packet_header(packet, b1, b2, b3, b4, b5, b6, 0x00);
289 memcpy(packet->bitmap, buf + offset, len);
291 retval = usb_bulk_msg(udev,
292 usb_sndctrlpipe(udev, 2),
294 sizeof(struct asus_oled_packet),
299 dev_dbg(&udev->dev, "retval = %d\n", retval);
303 static void send_packets_g50(struct usb_device *udev,
304 struct asus_oled_packet *packet, char *buf)
306 send_packet(udev, packet, 0, 0x100, buf,
307 0x10, 0x00, 0x02, 0x01, 0x00, 0x01);
308 send_packet(udev, packet, 0x100, 0x080, buf,
309 0x10, 0x00, 0x02, 0x02, 0x80, 0x00);
311 send_packet(udev, packet, 0x180, 0x100, buf,
312 0x11, 0x00, 0x03, 0x01, 0x00, 0x01);
313 send_packet(udev, packet, 0x280, 0x100, buf,
314 0x11, 0x00, 0x03, 0x02, 0x00, 0x01);
315 send_packet(udev, packet, 0x380, 0x080, buf,
316 0x11, 0x00, 0x03, 0x03, 0x80, 0x00);
320 static void send_data(struct asus_oled_dev *odev)
322 size_t packet_num = odev->buf_size / ASUS_OLED_PACKET_BUF_SIZE;
323 struct asus_oled_packet *packet;
325 packet = kzalloc(sizeof(struct asus_oled_packet), GFP_KERNEL);
328 dev_err(&odev->udev->dev, "out of memory\n");
332 if (odev->pack_mode == PACK_MODE_G1) {
333 /* When sending roll-mode data the display updated only
334 first packet. I have no idea why, but when static picture
335 is sent just before rolling picture everything works fine. */
336 if (odev->pic_mode == ASUS_OLED_ROLL)
337 send_packets(odev->udev, packet, odev->buf,
338 ASUS_OLED_STATIC, 2);
340 /* Only ROLL mode can use more than 2 packets.*/
341 if (odev->pic_mode != ASUS_OLED_ROLL && packet_num > 2)
344 send_packets(odev->udev, packet, odev->buf,
345 odev->pic_mode, packet_num);
346 } else if (odev->pack_mode == PACK_MODE_G50) {
347 send_packets_g50(odev->udev, packet, odev->buf);
353 static int append_values(struct asus_oled_dev *odev, uint8_t val, size_t count)
355 odev->last_val = val;
358 odev->buf_offs += count;
362 while (count-- > 0) {
363 size_t x = odev->buf_offs % odev->width;
364 size_t y = odev->buf_offs / odev->width;
370 switch (odev->pack_mode) {
372 /* i = (x/128)*640 + 127 - x + (y/8)*128;
373 This one for 128 is the same, but might be better
374 for different widths? */
375 i = (x/odev->dev_width)*640 +
376 odev->dev_width - 1 - x +
377 (y/8)*odev->dev_width;
381 i = (odev->dev_width - 1 - x)/8 + y*odev->dev_width/8;
386 dev_err(odev->dev, "Unknown OLED Pack Mode: %d!\n",
391 if (i >= odev->buf_size) {
392 dev_err(odev->dev, "Buffer overflow! Report a bug:"
393 "offs: %d >= %d i: %d (x: %d y: %d)\n",
394 (int) odev->buf_offs, (int) odev->buf_size,
395 (int) i, (int) x, (int) y);
399 switch (odev->pack_mode) {
401 odev->buf[i] &= ~(1<<(y%8));
405 odev->buf[i] &= ~(1<<(x%8));
409 /* cannot get here; stops gcc complaining*/
419 static ssize_t odev_set_picture(struct asus_oled_dev *odev,
420 const char *buf, size_t count)
422 size_t offs = 0, max_offs;
427 if (tolower(buf[0]) == 'b') {
428 /* binary mode, set the entire memory*/
432 odev->buf_size = (odev->dev_width * ASUS_OLED_DISP_HEIGHT) / 8;
435 odev->buf = kmalloc(odev->buf_size, GFP_KERNEL);
436 if (odev->buf == NULL) {
438 dev_err(odev->dev, "Out of memory!\n");
442 memset(odev->buf, 0xff, odev->buf_size);
444 for (i = 1; i < count && i <= 32 * 32; i++) {
445 odev->buf[i-1] = buf[i];
446 odev->buf_offs = i-1;
449 odev->width = odev->dev_width / 8;
450 odev->height = ASUS_OLED_DISP_HEIGHT;
465 if (count < 10 || buf[2] != ':')
469 switch (tolower(buf[1])) {
470 case ASUS_OLED_STATIC:
472 case ASUS_OLED_FLASH:
473 odev->pic_mode = buf[1];
476 dev_err(odev->dev, "Wrong picture mode: '%c'.\n",
482 for (i = 3; i < count; ++i) {
483 if (buf[i] >= '0' && buf[i] <= '9') {
484 w = 10*w + (buf[i] - '0');
486 if (w > ASUS_OLED_MAX_WIDTH)
488 } else if (tolower(buf[i]) == 'x') {
495 for (++i; i < count; ++i) {
496 if (buf[i] >= '0' && buf[i] <= '9') {
497 h = 10*h + (buf[i] - '0');
499 if (h > ASUS_OLED_DISP_HEIGHT)
501 } else if (tolower(buf[i]) == '>') {
508 if (w < 1 || w > ASUS_OLED_MAX_WIDTH)
511 if (h < 1 || h > ASUS_OLED_DISP_HEIGHT)
514 if (i >= count || buf[i] != '>')
519 if (w % (odev->dev_width) != 0)
520 w_mem = (w/(odev->dev_width) + 1)*(odev->dev_width);
524 if (h < ASUS_OLED_DISP_HEIGHT)
525 h_mem = ASUS_OLED_DISP_HEIGHT;
529 odev->buf_size = w_mem * h_mem / 8;
532 odev->buf = kmalloc(odev->buf_size, GFP_KERNEL);
534 if (odev->buf == NULL) {
536 dev_err(odev->dev, "Out of memory!\n");
540 memset(odev->buf, 0xff, odev->buf_size);
549 if (odev->pic_mode == ASUS_OLED_FLASH) {
550 if (h < ASUS_OLED_DISP_HEIGHT/2)
551 odev->y_shift = (ASUS_OLED_DISP_HEIGHT/2 - h)/2;
553 if (h < ASUS_OLED_DISP_HEIGHT)
554 odev->y_shift = (ASUS_OLED_DISP_HEIGHT - h)/2;
557 if (w < (odev->dev_width))
558 odev->x_shift = ((odev->dev_width) - w)/2;
561 max_offs = odev->width * odev->height;
563 while (offs < count && odev->buf_offs < max_offs) {
566 if (buf[offs] == '1' || buf[offs] == '#') {
567 ret = append_values(odev, 1, 1);
570 } else if (buf[offs] == '0' || buf[offs] == ' ') {
571 ret = append_values(odev, 0, 1);
574 } else if (buf[offs] == '\n') {
575 /* New line detected. Lets assume, that all characters
576 till the end of the line were equal to the last
577 character in this line.*/
578 if (odev->buf_offs % odev->width != 0)
579 ret = append_values(odev, odev->last_val,
590 if (odev->buf_offs >= max_offs)
596 dev_err(odev->dev, "Wrong picture width specified.\n");
600 dev_err(odev->dev, "Wrong picture height specified.\n");
604 dev_err(odev->dev, "Wrong picture header.\n");
608 static ssize_t set_picture(struct device *dev, struct device_attribute *attr,
609 const char *buf, size_t count)
611 struct usb_interface *intf = to_usb_interface(dev);
613 return odev_set_picture(usb_get_intfdata(intf), buf, count);
616 static ssize_t class_set_picture(struct device *device,
617 struct device_attribute *attr,
618 const char *buf, size_t count)
620 return odev_set_picture((struct asus_oled_dev *)
621 dev_get_drvdata(device), buf, count);
624 #define ASUS_OLED_DEVICE_ATTR(_file) dev_attr_asus_oled_##_file
626 static DEVICE_ATTR(asus_oled_enabled, S_IWUSR | S_IRUGO,
627 get_enabled, set_enabled);
628 static DEVICE_ATTR(asus_oled_picture, S_IWUSR , NULL, set_picture);
630 static DEVICE_ATTR(enabled, S_IWUSR | S_IRUGO,
631 class_get_enabled, class_set_enabled);
632 static DEVICE_ATTR(picture, S_IWUSR, NULL, class_set_picture);
634 static int asus_oled_probe(struct usb_interface *interface,
635 const struct usb_device_id *id)
637 struct usb_device *udev = interface_to_usbdev(interface);
638 struct asus_oled_dev *odev = NULL;
639 int retval = -ENOMEM;
640 uint16_t dev_width = 0;
641 enum oled_pack_mode pack_mode = PACK_MODE_LAST;
642 const struct oled_dev_desc_str *dev_desc = oled_dev_desc_table;
643 const char *desc = NULL;
646 /* Even possible? Just to make sure...*/
647 dev_err(&interface->dev, "No usb_device_id provided!\n");
651 for (; dev_desc->idVendor; dev_desc++) {
652 if (dev_desc->idVendor == id->idVendor
653 && dev_desc->idProduct == id->idProduct) {
654 dev_width = dev_desc->devWidth;
655 desc = dev_desc->devDesc;
656 pack_mode = dev_desc->packMode;
661 if (!desc || dev_width < 1 || pack_mode == PACK_MODE_LAST) {
662 dev_err(&interface->dev,
663 "Missing or incomplete device description!\n");
667 odev = kzalloc(sizeof(struct asus_oled_dev), GFP_KERNEL);
670 dev_err(&interface->dev, "Out of memory\n");
674 odev->udev = usb_get_dev(udev);
675 odev->pic_mode = ASUS_OLED_STATIC;
676 odev->dev_width = dev_width;
677 odev->pack_mode = pack_mode;
689 usb_set_intfdata(interface, odev);
691 retval = device_create_file(&interface->dev,
692 &ASUS_OLED_DEVICE_ATTR(enabled));
696 retval = device_create_file(&interface->dev,
697 &ASUS_OLED_DEVICE_ATTR(picture));
701 odev->dev = device_create(oled_class, &interface->dev, MKDEV(0, 0),
702 NULL, "oled_%d", ++oled_num);
704 if (IS_ERR(odev->dev)) {
705 retval = PTR_ERR(odev->dev);
709 dev_set_drvdata(odev->dev, odev);
711 retval = device_create_file(odev->dev, &dev_attr_enabled);
713 goto err_class_enabled;
715 retval = device_create_file(odev->dev, &dev_attr_picture);
717 goto err_class_picture;
719 dev_info(&interface->dev,
720 "Attached Asus OLED device: %s [width %u, pack_mode %d]\n",
721 desc, odev->dev_width, odev->pack_mode);
724 enable_oled(odev, 0);
729 device_remove_file(odev->dev, &dev_attr_picture);
732 device_remove_file(odev->dev, &dev_attr_enabled);
733 device_unregister(odev->dev);
736 device_remove_file(&interface->dev, &ASUS_OLED_DEVICE_ATTR(enabled));
737 device_remove_file(&interface->dev, &ASUS_OLED_DEVICE_ATTR(picture));
739 usb_set_intfdata(interface, NULL);
740 usb_put_dev(odev->udev);
746 static void asus_oled_disconnect(struct usb_interface *interface)
748 struct asus_oled_dev *odev;
750 odev = usb_get_intfdata(interface);
751 usb_set_intfdata(interface, NULL);
753 device_remove_file(odev->dev, &dev_attr_picture);
754 device_remove_file(odev->dev, &dev_attr_enabled);
755 device_unregister(odev->dev);
757 device_remove_file(&interface->dev, &ASUS_OLED_DEVICE_ATTR(picture));
758 device_remove_file(&interface->dev, &ASUS_OLED_DEVICE_ATTR(enabled));
760 usb_put_dev(odev->udev);
766 dev_info(&interface->dev, "Disconnected Asus OLED device\n");
770 static int asus_oled_suspend(struct usb_interface *intf, pm_message_t message)
772 struct asus_oled_dev *odev;
774 odev = usb_get_intfdata(intf);
778 odev->enabled_post_resume = odev->enabled;
779 enable_oled(odev, 0);
784 static int asus_oled_resume(struct usb_interface *intf)
786 struct asus_oled_dev *odev;
788 odev = usb_get_intfdata(intf);
792 enable_oled(odev, odev->enabled_post_resume);
797 #define asus_oled_suspend NULL
798 #define asus_oled_resume NULL
801 static struct usb_driver oled_driver = {
802 .name = ASUS_OLED_NAME,
803 .probe = asus_oled_probe,
804 .disconnect = asus_oled_disconnect,
805 .id_table = id_table,
806 .suspend = asus_oled_suspend,
807 .resume = asus_oled_resume,
810 static CLASS_ATTR_STRING(version, S_IRUGO,
811 ASUS_OLED_UNDERSCORE_NAME " " ASUS_OLED_VERSION);
813 static int __init asus_oled_init(void)
816 oled_class = class_create(THIS_MODULE, ASUS_OLED_UNDERSCORE_NAME);
818 if (IS_ERR(oled_class)) {
819 pr_err("Error creating " ASUS_OLED_UNDERSCORE_NAME " class\n");
820 return PTR_ERR(oled_class);
823 retval = class_create_file(oled_class, &class_attr_version.attr);
825 pr_err("Error creating class version file\n");
829 retval = usb_register(&oled_driver);
832 pr_err("usb_register failed. Error number %d\n", retval);
839 class_destroy(oled_class);
843 static void __exit asus_oled_exit(void)
845 usb_deregister(&oled_driver);
846 class_remove_file(oled_class, &class_attr_version.attr);
847 class_destroy(oled_class);
850 module_init(asus_oled_init);
851 module_exit(asus_oled_exit);