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
);
198 if (strict_strtoul(buf
, 10, &value
))
201 enable_oled(odev
, value
);
206 static ssize_t
class_set_enabled(struct device
*device
,
207 struct device_attribute
*attr
,
208 const char *buf
, size_t count
)
210 struct asus_oled_dev
*odev
=
211 (struct asus_oled_dev
*) dev_get_drvdata(device
);
214 if (strict_strtoul(buf
, 10, &value
))
217 enable_oled(odev
, value
);
222 static ssize_t
get_enabled(struct device
*dev
, struct device_attribute
*attr
,
225 struct usb_interface
*intf
= to_usb_interface(dev
);
226 struct asus_oled_dev
*odev
= usb_get_intfdata(intf
);
228 return sprintf(buf
, "%d\n", odev
->enabled
);
231 static ssize_t
class_get_enabled(struct device
*device
,
232 struct device_attribute
*attr
, char *buf
)
234 struct asus_oled_dev
*odev
=
235 (struct asus_oled_dev
*) dev_get_drvdata(device
);
237 return sprintf(buf
, "%d\n", odev
->enabled
);
240 static void send_packets(struct usb_device
*udev
,
241 struct asus_oled_packet
*packet
,
242 char *buf
, uint8_t p_type
, size_t p_num
)
247 for (i
= 0; i
< p_num
; i
++) {
252 setup_packet_header(packet
, 0x40, 0x80, p_num
,
253 i
+ 1, 0x00, 0x01, 0xff);
255 case ASUS_OLED_STATIC
:
256 setup_packet_header(packet
, 0x10 + i
, 0x80, 0x01,
257 0x01, 0x00, 0x01, 0x00);
259 case ASUS_OLED_FLASH
:
260 setup_packet_header(packet
, 0x10 + i
, 0x80, 0x01,
261 0x01, 0x00, 0x00, 0xff);
265 memcpy(packet
->bitmap
, buf
+ (ASUS_OLED_PACKET_BUF_SIZE
*i
),
266 ASUS_OLED_PACKET_BUF_SIZE
);
268 retval
= usb_bulk_msg(udev
, usb_sndctrlpipe(udev
, 2),
269 packet
, sizeof(struct asus_oled_packet
),
273 dev_dbg(&udev
->dev
, "retval = %d\n", retval
);
277 static void send_packet(struct usb_device
*udev
,
278 struct asus_oled_packet
*packet
,
279 size_t offset
, size_t len
, char *buf
, uint8_t b1
,
280 uint8_t b2
, uint8_t b3
, uint8_t b4
, uint8_t b5
,
285 setup_packet_header(packet
, b1
, b2
, b3
, b4
, b5
, b6
, 0x00);
286 memcpy(packet
->bitmap
, buf
+ offset
, len
);
288 retval
= usb_bulk_msg(udev
,
289 usb_sndctrlpipe(udev
, 2),
291 sizeof(struct asus_oled_packet
),
296 dev_dbg(&udev
->dev
, "retval = %d\n", retval
);
300 static void send_packets_g50(struct usb_device
*udev
,
301 struct asus_oled_packet
*packet
, char *buf
)
303 send_packet(udev
, packet
, 0, 0x100, buf
,
304 0x10, 0x00, 0x02, 0x01, 0x00, 0x01);
305 send_packet(udev
, packet
, 0x100, 0x080, buf
,
306 0x10, 0x00, 0x02, 0x02, 0x80, 0x00);
308 send_packet(udev
, packet
, 0x180, 0x100, buf
,
309 0x11, 0x00, 0x03, 0x01, 0x00, 0x01);
310 send_packet(udev
, packet
, 0x280, 0x100, buf
,
311 0x11, 0x00, 0x03, 0x02, 0x00, 0x01);
312 send_packet(udev
, packet
, 0x380, 0x080, buf
,
313 0x11, 0x00, 0x03, 0x03, 0x80, 0x00);
317 static void send_data(struct asus_oled_dev
*odev
)
319 size_t packet_num
= odev
->buf_size
/ ASUS_OLED_PACKET_BUF_SIZE
;
320 struct asus_oled_packet
*packet
;
322 packet
= kzalloc(sizeof(struct asus_oled_packet
), GFP_KERNEL
);
325 dev_err(&odev
->udev
->dev
, "out of memory\n");
329 if (odev
->pack_mode
== PACK_MODE_G1
) {
330 /* When sending roll-mode data the display updated only
331 first packet. I have no idea why, but when static picture
332 is sent just before rolling picture everything works fine. */
333 if (odev
->pic_mode
== ASUS_OLED_ROLL
)
334 send_packets(odev
->udev
, packet
, odev
->buf
,
335 ASUS_OLED_STATIC
, 2);
337 /* Only ROLL mode can use more than 2 packets.*/
338 if (odev
->pic_mode
!= ASUS_OLED_ROLL
&& packet_num
> 2)
341 send_packets(odev
->udev
, packet
, odev
->buf
,
342 odev
->pic_mode
, packet_num
);
343 } else if (odev
->pack_mode
== PACK_MODE_G50
) {
344 send_packets_g50(odev
->udev
, packet
, odev
->buf
);
350 static int append_values(struct asus_oled_dev
*odev
, uint8_t val
, size_t count
)
352 while (count
-- > 0 && val
) {
353 size_t x
= odev
->buf_offs
% odev
->width
;
354 size_t y
= odev
->buf_offs
/ odev
->width
;
360 switch (odev
->pack_mode
) {
362 /* i = (x/128)*640 + 127 - x + (y/8)*128;
363 This one for 128 is the same, but might be better
364 for different widths? */
365 i
= (x
/odev
->dev_width
)*640 +
366 odev
->dev_width
- 1 - x
+
367 (y
/8)*odev
->dev_width
;
371 i
= (odev
->dev_width
- 1 - x
)/8 + y
*odev
->dev_width
/8;
376 printk(ASUS_OLED_ERROR
"Unknown OLED Pack Mode: %d!\n",
381 if (i
>= odev
->buf_size
) {
382 printk(ASUS_OLED_ERROR
"Buffer overflow! Report a bug:"
383 "offs: %d >= %d i: %d (x: %d y: %d)\n",
384 (int) odev
->buf_offs
, (int) odev
->buf_size
,
385 (int) i
, (int) x
, (int) y
);
389 switch (odev
->pack_mode
) {
391 odev
->buf
[i
] &= ~(1<<(y
%8));
395 odev
->buf
[i
] &= ~(1<<(x
%8));
399 /* cannot get here; stops gcc complaining*/
403 odev
->last_val
= val
;
410 static ssize_t
odev_set_picture(struct asus_oled_dev
*odev
,
411 const char *buf
, size_t count
)
413 size_t offs
= 0, max_offs
;
418 if (tolower(buf
[0]) == 'b') {
419 /* binary mode, set the entire memory*/
423 odev
->buf_size
= (odev
->dev_width
* ASUS_OLED_DISP_HEIGHT
) / 8;
426 odev
->buf
= kmalloc(odev
->buf_size
, GFP_KERNEL
);
428 memset(odev
->buf
, 0xff, odev
->buf_size
);
430 for (i
= 1; i
< count
&& i
<= 32 * 32; i
++) {
431 odev
->buf
[i
-1] = buf
[i
];
432 odev
->buf_offs
= i
-1;
435 odev
->width
= odev
->dev_width
/ 8;
436 odev
->height
= ASUS_OLED_DISP_HEIGHT
;
451 if (count
< 10 || buf
[2] != ':')
455 switch (tolower(buf
[1])) {
456 case ASUS_OLED_STATIC
:
458 case ASUS_OLED_FLASH
:
459 odev
->pic_mode
= buf
[1];
462 printk(ASUS_OLED_ERROR
"Wrong picture mode: '%c'.\n",
468 for (i
= 3; i
< count
; ++i
) {
469 if (buf
[i
] >= '0' && buf
[i
] <= '9') {
470 w
= 10*w
+ (buf
[i
] - '0');
472 if (w
> ASUS_OLED_MAX_WIDTH
)
474 } else if (tolower(buf
[i
]) == 'x') {
481 for (++i
; i
< count
; ++i
) {
482 if (buf
[i
] >= '0' && buf
[i
] <= '9') {
483 h
= 10*h
+ (buf
[i
] - '0');
485 if (h
> ASUS_OLED_DISP_HEIGHT
)
487 } else if (tolower(buf
[i
]) == '>') {
494 if (w
< 1 || w
> ASUS_OLED_MAX_WIDTH
)
497 if (h
< 1 || h
> ASUS_OLED_DISP_HEIGHT
)
500 if (i
>= count
|| buf
[i
] != '>')
505 if (w
% (odev
->dev_width
) != 0)
506 w_mem
= (w
/(odev
->dev_width
) + 1)*(odev
->dev_width
);
510 if (h
< ASUS_OLED_DISP_HEIGHT
)
511 h_mem
= ASUS_OLED_DISP_HEIGHT
;
515 odev
->buf_size
= w_mem
* h_mem
/ 8;
518 odev
->buf
= kmalloc(odev
->buf_size
, GFP_KERNEL
);
520 if (odev
->buf
== NULL
) {
522 printk(ASUS_OLED_ERROR
"Out of memory!\n");
526 memset(odev
->buf
, 0xff, odev
->buf_size
);
535 if (odev
->pic_mode
== ASUS_OLED_FLASH
) {
536 if (h
< ASUS_OLED_DISP_HEIGHT
/2)
537 odev
->y_shift
= (ASUS_OLED_DISP_HEIGHT
/2 - h
)/2;
539 if (h
< ASUS_OLED_DISP_HEIGHT
)
540 odev
->y_shift
= (ASUS_OLED_DISP_HEIGHT
- h
)/2;
543 if (w
< (odev
->dev_width
))
544 odev
->x_shift
= ((odev
->dev_width
) - w
)/2;
547 max_offs
= odev
->width
* odev
->height
;
549 while (offs
< count
&& odev
->buf_offs
< max_offs
) {
552 if (buf
[offs
] == '1' || buf
[offs
] == '#') {
553 ret
= append_values(odev
, 1, 1);
556 } else if (buf
[offs
] == '0' || buf
[offs
] == ' ') {
557 ret
= append_values(odev
, 0, 1);
560 } else if (buf
[offs
] == '\n') {
561 /* New line detected. Lets assume, that all characters
562 till the end of the line were equal to the last
563 character in this line.*/
564 if (odev
->buf_offs
% odev
->width
!= 0)
565 ret
= append_values(odev
, odev
->last_val
,
576 if (odev
->buf_offs
>= max_offs
)
582 printk(ASUS_OLED_ERROR
"Wrong picture width specified.\n");
586 printk(ASUS_OLED_ERROR
"Wrong picture height specified.\n");
590 printk(ASUS_OLED_ERROR
"Wrong picture header.\n");
594 static ssize_t
set_picture(struct device
*dev
, struct device_attribute
*attr
,
595 const char *buf
, size_t count
)
597 struct usb_interface
*intf
= to_usb_interface(dev
);
599 return odev_set_picture(usb_get_intfdata(intf
), buf
, count
);
602 static ssize_t
class_set_picture(struct device
*device
,
603 struct device_attribute
*attr
,
604 const char *buf
, size_t count
)
606 return odev_set_picture((struct asus_oled_dev
*)
607 dev_get_drvdata(device
), buf
, count
);
610 #define ASUS_OLED_DEVICE_ATTR(_file) dev_attr_asus_oled_##_file
612 static DEVICE_ATTR(asus_oled_enabled
, S_IWUSR
| S_IRUGO
,
613 get_enabled
, set_enabled
);
614 static DEVICE_ATTR(asus_oled_picture
, S_IWUSR
, NULL
, set_picture
);
616 static DEVICE_ATTR(enabled
, S_IWUSR
| S_IRUGO
,
617 class_get_enabled
, class_set_enabled
);
618 static DEVICE_ATTR(picture
, S_IWUSR
, NULL
, class_set_picture
);
620 static int asus_oled_probe(struct usb_interface
*interface
,
621 const struct usb_device_id
*id
)
623 struct usb_device
*udev
= interface_to_usbdev(interface
);
624 struct asus_oled_dev
*odev
= NULL
;
625 int retval
= -ENOMEM
;
626 uint16_t dev_width
= 0;
627 enum oled_pack_mode pack_mode
= PACK_MODE_LAST
;
628 const struct oled_dev_desc_str
*dev_desc
= oled_dev_desc_table
;
629 const char *desc
= NULL
;
632 /* Even possible? Just to make sure...*/
633 dev_err(&interface
->dev
, "No usb_device_id provided!\n");
637 for (; dev_desc
->idVendor
; dev_desc
++) {
638 if (dev_desc
->idVendor
== id
->idVendor
639 && dev_desc
->idProduct
== id
->idProduct
) {
640 dev_width
= dev_desc
->devWidth
;
641 desc
= dev_desc
->devDesc
;
642 pack_mode
= dev_desc
->packMode
;
647 if (!desc
|| dev_width
< 1 || pack_mode
== PACK_MODE_LAST
) {
648 dev_err(&interface
->dev
,
649 "Missing or incomplete device description!\n");
653 odev
= kzalloc(sizeof(struct asus_oled_dev
), GFP_KERNEL
);
656 dev_err(&interface
->dev
, "Out of memory\n");
660 odev
->udev
= usb_get_dev(udev
);
661 odev
->pic_mode
= ASUS_OLED_STATIC
;
662 odev
->dev_width
= dev_width
;
663 odev
->pack_mode
= pack_mode
;
675 usb_set_intfdata(interface
, odev
);
677 retval
= device_create_file(&interface
->dev
,
678 &ASUS_OLED_DEVICE_ATTR(enabled
));
682 retval
= device_create_file(&interface
->dev
,
683 &ASUS_OLED_DEVICE_ATTR(picture
));
687 odev
->dev
= device_create(oled_class
, &interface
->dev
, MKDEV(0, 0),
688 NULL
, "oled_%d", ++oled_num
);
690 if (IS_ERR(odev
->dev
)) {
691 retval
= PTR_ERR(odev
->dev
);
695 dev_set_drvdata(odev
->dev
, odev
);
697 retval
= device_create_file(odev
->dev
, &dev_attr_enabled
);
699 goto err_class_enabled
;
701 retval
= device_create_file(odev
->dev
, &dev_attr_picture
);
703 goto err_class_picture
;
705 dev_info(&interface
->dev
,
706 "Attached Asus OLED device: %s [width %u, pack_mode %d]\n",
707 desc
, odev
->dev_width
, odev
->pack_mode
);
710 enable_oled(odev
, 0);
715 device_remove_file(odev
->dev
, &dev_attr_picture
);
718 device_remove_file(odev
->dev
, &dev_attr_enabled
);
719 device_unregister(odev
->dev
);
722 device_remove_file(&interface
->dev
, &ASUS_OLED_DEVICE_ATTR(enabled
));
723 device_remove_file(&interface
->dev
, &ASUS_OLED_DEVICE_ATTR(picture
));
725 usb_set_intfdata(interface
, NULL
);
726 usb_put_dev(odev
->udev
);
732 static void asus_oled_disconnect(struct usb_interface
*interface
)
734 struct asus_oled_dev
*odev
;
736 odev
= usb_get_intfdata(interface
);
737 usb_set_intfdata(interface
, NULL
);
739 device_remove_file(odev
->dev
, &dev_attr_picture
);
740 device_remove_file(odev
->dev
, &dev_attr_enabled
);
741 device_unregister(odev
->dev
);
743 device_remove_file(&interface
->dev
, &ASUS_OLED_DEVICE_ATTR(picture
));
744 device_remove_file(&interface
->dev
, &ASUS_OLED_DEVICE_ATTR(enabled
));
746 usb_put_dev(odev
->udev
);
752 dev_info(&interface
->dev
, "Disconnected Asus OLED device\n");
755 static struct usb_driver oled_driver
= {
756 .name
= ASUS_OLED_NAME
,
757 .probe
= asus_oled_probe
,
758 .disconnect
= asus_oled_disconnect
,
759 .id_table
= id_table
,
762 static ssize_t
version_show(struct class *dev
, char *buf
)
764 return sprintf(buf
, ASUS_OLED_UNDERSCORE_NAME
" %s\n",
768 static CLASS_ATTR(version
, S_IRUGO
, version_show
, NULL
);
770 static int __init
asus_oled_init(void)
773 oled_class
= class_create(THIS_MODULE
, ASUS_OLED_UNDERSCORE_NAME
);
775 if (IS_ERR(oled_class
)) {
776 err("Error creating " ASUS_OLED_UNDERSCORE_NAME
" class");
777 return PTR_ERR(oled_class
);
780 retval
= class_create_file(oled_class
, &class_attr_version
);
782 err("Error creating class version file");
786 retval
= usb_register(&oled_driver
);
789 err("usb_register failed. Error number %d", retval
);
796 class_destroy(oled_class
);
800 static void __exit
asus_oled_exit(void)
802 class_remove_file(oled_class
, &class_attr_version
);
803 class_destroy(oled_class
);
805 usb_deregister(&oled_driver
);
808 module_init(asus_oled_init
);
809 module_exit(asus_oled_exit
);