Merge branch 'tracing/core' of git://git.kernel.org/pub/scm/linux/kernel/git/frederic...
[linux-2.6-block.git] / drivers / staging / asus_oled / asus_oled.c
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
27  *  https://launchpad.net/asusoled/.
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
55 MODULE_AUTHOR("Jakub Schmidtke, sjakub@gmail.com");
56 MODULE_DESCRIPTION("Asus OLED Driver v" ASUS_OLED_VERSION);
57 MODULE_LICENSE("GPL");
58
59 static struct class *oled_class;
60 static int oled_num;
61
62 static uint start_off;
63
64 module_param(start_off, uint, 0644);
65
66 MODULE_PARM_DESC(start_off,
67                  "Set to 1 to switch off OLED display after it is attached");
68
69 enum oled_pack_mode{
70         PACK_MODE_G1,
71         PACK_MODE_G50,
72         PACK_MODE_LAST
73 };
74
75 struct oled_dev_desc_str {
76         uint16_t                idVendor;
77         uint16_t                idProduct;
78         /* width of display */
79         uint16_t                devWidth;
80         /* formula to be used while packing the picture */
81         enum oled_pack_mode     packMode;
82         const char              *devDesc;
83 };
84
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) },
91         { },
92 };
93
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" },
98         { },
99 };
100
101 MODULE_DEVICE_TABLE(usb, id_table);
102
103 struct asus_oled_header {
104         uint8_t         magic1;
105         uint8_t         magic2;
106         uint8_t         flags;
107         uint8_t         value3;
108         uint8_t         buffer1;
109         uint8_t         buffer2;
110         uint8_t         value6;
111         uint8_t         value7;
112         uint8_t         value8;
113         uint8_t         padding2[7];
114 } __attribute((packed));
115
116 struct asus_oled_packet {
117         struct asus_oled_header         header;
118         uint8_t                         bitmap[ASUS_OLED_PACKET_BUF_SIZE];
119 } __attribute((packed));
120
121 struct asus_oled_dev {
122         struct usb_device       *udev;
123         uint8_t                 pic_mode;
124         uint16_t                dev_width;
125         enum oled_pack_mode     pack_mode;
126         size_t                  height;
127         size_t                  width;
128         size_t                  x_shift;
129         size_t                  y_shift;
130         size_t                  buf_offs;
131         uint8_t                 last_val;
132         size_t                  buf_size;
133         char                    *buf;
134         uint8_t                 enabled;
135         struct device           *dev;
136 };
137
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)
141 {
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;
152 }
153
154 static void enable_oled(struct asus_oled_dev *odev, uint8_t enabl)
155 {
156         int a;
157         int retval;
158         int act_len;
159         struct asus_oled_packet *packet;
160
161         packet = kzalloc(sizeof(struct asus_oled_packet), GFP_KERNEL);
162
163         if (!packet) {
164                 dev_err(&odev->udev->dev, "out of memory\n");
165                 return;
166         }
167
168         setup_packet_header(packet, 0x20, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00);
169
170         if (enabl)
171                 packet->bitmap[0] = 0xaf;
172         else
173                 packet->bitmap[0] = 0xae;
174
175         for (a = 0; a < 1; a++) {
176                 retval = usb_bulk_msg(odev->udev,
177                         usb_sndbulkpipe(odev->udev, 2),
178                         packet,
179                         sizeof(struct asus_oled_header) + 1,
180                         &act_len,
181                         -1);
182
183                 if (retval)
184                         dev_dbg(&odev->udev->dev, "retval = %d\n", retval);
185         }
186
187         odev->enabled = enabl;
188
189         kfree(packet);
190 }
191
192 static ssize_t set_enabled(struct device *dev, struct device_attribute *attr,
193                            const char *buf, size_t count)
194 {
195         struct usb_interface *intf = to_usb_interface(dev);
196         struct asus_oled_dev *odev = usb_get_intfdata(intf);
197         unsigned long value;
198         if (strict_strtoul(buf, 10, &value))
199                 return -EINVAL;
200
201         enable_oled(odev, value);
202
203         return count;
204 }
205
206 static ssize_t class_set_enabled(struct device *device,
207                                  struct device_attribute *attr,
208                                  const char *buf, size_t count)
209 {
210         struct asus_oled_dev *odev =
211                 (struct asus_oled_dev *) dev_get_drvdata(device);
212         unsigned long value;
213
214         if (strict_strtoul(buf, 10, &value))
215                 return -EINVAL;
216
217         enable_oled(odev, value);
218
219         return count;
220 }
221
222 static ssize_t get_enabled(struct device *dev, struct device_attribute *attr,
223                            char *buf)
224 {
225         struct usb_interface *intf = to_usb_interface(dev);
226         struct asus_oled_dev *odev = usb_get_intfdata(intf);
227
228         return sprintf(buf, "%d\n", odev->enabled);
229 }
230
231 static ssize_t class_get_enabled(struct device *device,
232                                  struct device_attribute *attr, char *buf)
233 {
234         struct asus_oled_dev *odev =
235                 (struct asus_oled_dev *) dev_get_drvdata(device);
236
237         return sprintf(buf, "%d\n", odev->enabled);
238 }
239
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)
243 {
244         size_t i;
245         int act_len;
246
247         for (i = 0; i < p_num; i++) {
248                 int retval;
249
250                 switch (p_type) {
251                 case ASUS_OLED_ROLL:
252                         setup_packet_header(packet, 0x40, 0x80, p_num,
253                                             i + 1, 0x00, 0x01, 0xff);
254                         break;
255                 case ASUS_OLED_STATIC:
256                         setup_packet_header(packet, 0x10 + i, 0x80, 0x01,
257                                             0x01, 0x00, 0x01, 0x00);
258                         break;
259                 case ASUS_OLED_FLASH:
260                         setup_packet_header(packet, 0x10 + i, 0x80, 0x01,
261                                             0x01, 0x00, 0x00, 0xff);
262                         break;
263                 }
264
265                 memcpy(packet->bitmap, buf + (ASUS_OLED_PACKET_BUF_SIZE*i),
266                        ASUS_OLED_PACKET_BUF_SIZE);
267
268                 retval = usb_bulk_msg(udev, usb_sndctrlpipe(udev, 2),
269                                       packet, sizeof(struct asus_oled_packet),
270                                       &act_len, -1);
271
272                 if (retval)
273                         dev_dbg(&udev->dev, "retval = %d\n", retval);
274         }
275 }
276
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,
281                         uint8_t b6) {
282         int retval;
283         int act_len;
284
285         setup_packet_header(packet, b1, b2, b3, b4, b5, b6, 0x00);
286         memcpy(packet->bitmap, buf + offset, len);
287
288         retval = usb_bulk_msg(udev,
289                               usb_sndctrlpipe(udev, 2),
290                               packet,
291                               sizeof(struct asus_oled_packet),
292                               &act_len,
293                               -1);
294
295         if (retval)
296                 dev_dbg(&udev->dev, "retval = %d\n", retval);
297 }
298
299
300 static void send_packets_g50(struct usb_device *udev,
301                              struct asus_oled_packet *packet, char *buf)
302 {
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);
307
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);
314 }
315
316
317 static void send_data(struct asus_oled_dev *odev)
318 {
319         size_t packet_num = odev->buf_size / ASUS_OLED_PACKET_BUF_SIZE;
320         struct asus_oled_packet *packet;
321
322         packet = kzalloc(sizeof(struct asus_oled_packet), GFP_KERNEL);
323
324         if (!packet) {
325                 dev_err(&odev->udev->dev, "out of memory\n");
326                 return;
327         }
328
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);
336
337                 /* Only ROLL mode can use more than 2 packets.*/
338                 if (odev->pic_mode != ASUS_OLED_ROLL && packet_num > 2)
339                         packet_num = 2;
340
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);
345         }
346
347         kfree(packet);
348 }
349
350 static int append_values(struct asus_oled_dev *odev, uint8_t val, size_t count)
351 {
352         while (count-- > 0 && val) {
353                 size_t x = odev->buf_offs % odev->width;
354                 size_t y = odev->buf_offs / odev->width;
355                 size_t i;
356
357                 x += odev->x_shift;
358                 y += odev->y_shift;
359
360                 switch (odev->pack_mode) {
361                 case PACK_MODE_G1:
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;
368                         break;
369
370                 case PACK_MODE_G50:
371                         i =  (odev->dev_width - 1 - x)/8 + y*odev->dev_width/8;
372                         break;
373
374                 default:
375                         i = 0;
376                         printk(ASUS_OLED_ERROR "Unknown OLED Pack Mode: %d!\n",
377                                odev->pack_mode);
378                         break;
379                 }
380
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);
386                         return -EIO;
387                 }
388
389                 switch (odev->pack_mode) {
390                 case PACK_MODE_G1:
391                         odev->buf[i] &= ~(1<<(y%8));
392                         break;
393
394                 case PACK_MODE_G50:
395                         odev->buf[i] &= ~(1<<(x%8));
396                         break;
397
398                 default:
399                         /* cannot get here; stops gcc complaining*/
400                         ;
401                 }
402
403                 odev->last_val = val;
404                 odev->buf_offs++;
405         }
406
407         return 0;
408 }
409
410 static ssize_t odev_set_picture(struct asus_oled_dev *odev,
411                                 const char *buf, size_t count)
412 {
413         size_t offs = 0, max_offs;
414
415         if (count < 1)
416                 return 0;
417
418         if (tolower(buf[0]) == 'b') {
419                 /* binary mode, set the entire memory*/
420
421                 size_t i;
422
423                 odev->buf_size = (odev->dev_width * ASUS_OLED_DISP_HEIGHT) / 8;
424
425                 kfree(odev->buf);
426                 odev->buf = kmalloc(odev->buf_size, GFP_KERNEL);
427
428                 memset(odev->buf, 0xff, odev->buf_size);
429
430                 for (i = 1; i < count && i <= 32 * 32; i++) {
431                         odev->buf[i-1] = buf[i];
432                         odev->buf_offs = i-1;
433                 }
434
435                 odev->width = odev->dev_width / 8;
436                 odev->height = ASUS_OLED_DISP_HEIGHT;
437                 odev->x_shift = 0;
438                 odev->y_shift = 0;
439                 odev->last_val =  0;
440
441                 send_data(odev);
442
443                 return count;
444         }
445
446         if (buf[0] == '<') {
447                 size_t i;
448                 size_t w = 0, h = 0;
449                 size_t w_mem, h_mem;
450
451                 if (count < 10 || buf[2] != ':')
452                         goto error_header;
453
454
455                 switch (tolower(buf[1])) {
456                 case ASUS_OLED_STATIC:
457                 case ASUS_OLED_ROLL:
458                 case ASUS_OLED_FLASH:
459                         odev->pic_mode = buf[1];
460                         break;
461                 default:
462                         printk(ASUS_OLED_ERROR "Wrong picture mode: '%c'.\n",
463                                buf[1]);
464                         return -EIO;
465                         break;
466                 }
467
468                 for (i = 3; i < count; ++i) {
469                         if (buf[i] >= '0' && buf[i] <= '9') {
470                                 w = 10*w + (buf[i] - '0');
471
472                                 if (w > ASUS_OLED_MAX_WIDTH)
473                                         goto error_width;
474                         } else if (tolower(buf[i]) == 'x') {
475                                 break;
476                         } else {
477                                 goto error_width;
478                         }
479                 }
480
481                 for (++i; i < count; ++i) {
482                         if (buf[i] >= '0' && buf[i] <= '9') {
483                                 h = 10*h + (buf[i] - '0');
484
485                                 if (h > ASUS_OLED_DISP_HEIGHT)
486                                         goto error_height;
487                         } else if (tolower(buf[i]) == '>') {
488                                 break;
489                         } else {
490                                 goto error_height;
491                         }
492                 }
493
494                 if (w < 1 || w > ASUS_OLED_MAX_WIDTH)
495                         goto error_width;
496
497                 if (h < 1 || h > ASUS_OLED_DISP_HEIGHT)
498                         goto error_height;
499
500                 if (i >= count || buf[i] != '>')
501                         goto error_header;
502
503                 offs = i+1;
504
505                 if (w % (odev->dev_width) != 0)
506                         w_mem = (w/(odev->dev_width) + 1)*(odev->dev_width);
507                 else
508                         w_mem = w;
509
510                 if (h < ASUS_OLED_DISP_HEIGHT)
511                         h_mem = ASUS_OLED_DISP_HEIGHT;
512                 else
513                         h_mem = h;
514
515                 odev->buf_size = w_mem * h_mem / 8;
516
517                 kfree(odev->buf);
518                 odev->buf = kmalloc(odev->buf_size, GFP_KERNEL);
519
520                 if (odev->buf == NULL) {
521                         odev->buf_size = 0;
522                         printk(ASUS_OLED_ERROR "Out of memory!\n");
523                         return -ENOMEM;
524                 }
525
526                 memset(odev->buf, 0xff, odev->buf_size);
527
528                 odev->buf_offs = 0;
529                 odev->width = w;
530                 odev->height = h;
531                 odev->x_shift = 0;
532                 odev->y_shift = 0;
533                 odev->last_val = 0;
534
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;
538                 } else {
539                         if (h < ASUS_OLED_DISP_HEIGHT)
540                                 odev->y_shift = (ASUS_OLED_DISP_HEIGHT - h)/2;
541                 }
542
543                 if (w < (odev->dev_width))
544                         odev->x_shift = ((odev->dev_width) - w)/2;
545         }
546
547         max_offs = odev->width * odev->height;
548
549         while (offs < count && odev->buf_offs < max_offs) {
550                 int ret = 0;
551
552                 if (buf[offs] == '1' || buf[offs] == '#') {
553                         ret = append_values(odev, 1, 1);
554                         if (ret < 0)
555                                 return ret;
556                 } else if (buf[offs] == '0' || buf[offs] == ' ') {
557                         ret = append_values(odev, 0, 1);
558                         if (ret < 0)
559                                 return ret;
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,
566                                                     odev->width -
567                                                     (odev->buf_offs %
568                                                      odev->width));
569                         if (ret < 0)
570                                 return ret;
571                 }
572
573                 offs++;
574         }
575
576         if (odev->buf_offs >= max_offs)
577                 send_data(odev);
578
579         return count;
580
581 error_width:
582         printk(ASUS_OLED_ERROR "Wrong picture width specified.\n");
583         return -EIO;
584
585 error_height:
586         printk(ASUS_OLED_ERROR "Wrong picture height specified.\n");
587         return -EIO;
588
589 error_header:
590         printk(ASUS_OLED_ERROR "Wrong picture header.\n");
591         return -EIO;
592 }
593
594 static ssize_t set_picture(struct device *dev, struct device_attribute *attr,
595                            const char *buf, size_t count)
596 {
597         struct usb_interface *intf = to_usb_interface(dev);
598
599         return odev_set_picture(usb_get_intfdata(intf), buf, count);
600 }
601
602 static ssize_t class_set_picture(struct device *device,
603                                  struct device_attribute *attr,
604                                  const char *buf, size_t count)
605 {
606         return odev_set_picture((struct asus_oled_dev *)
607                                 dev_get_drvdata(device), buf, count);
608 }
609
610 #define ASUS_OLED_DEVICE_ATTR(_file)            dev_attr_asus_oled_##_file
611
612 static DEVICE_ATTR(asus_oled_enabled, S_IWUGO | S_IRUGO,
613                    get_enabled, set_enabled);
614 static DEVICE_ATTR(asus_oled_picture, S_IWUGO , NULL, set_picture);
615
616 static DEVICE_ATTR(enabled, S_IWUGO | S_IRUGO,
617                    class_get_enabled, class_set_enabled);
618 static DEVICE_ATTR(picture, S_IWUGO, NULL, class_set_picture);
619
620 static int asus_oled_probe(struct usb_interface *interface,
621                            const struct usb_device_id *id)
622 {
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;
630
631         if (!id) {
632                 /* Even possible? Just to make sure...*/
633                 dev_err(&interface->dev, "No usb_device_id provided!\n");
634                 return -ENODEV;
635         }
636
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;
643                         break;
644                 }
645         }
646
647         if (!desc || dev_width < 1 || pack_mode == PACK_MODE_LAST) {
648                 dev_err(&interface->dev,
649                         "Missing or incomplete device description!\n");
650                 return -ENODEV;
651         }
652
653         odev = kzalloc(sizeof(struct asus_oled_dev), GFP_KERNEL);
654
655         if (odev == NULL) {
656                 dev_err(&interface->dev, "Out of memory\n");
657                 return -ENOMEM;
658         }
659
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;
664         odev->height = 0;
665         odev->width = 0;
666         odev->x_shift = 0;
667         odev->y_shift = 0;
668         odev->buf_offs = 0;
669         odev->buf_size = 0;
670         odev->last_val = 0;
671         odev->buf = NULL;
672         odev->enabled = 1;
673         odev->dev = NULL;
674
675         usb_set_intfdata(interface, odev);
676
677         retval = device_create_file(&interface->dev,
678                                     &ASUS_OLED_DEVICE_ATTR(enabled));
679         if (retval)
680                 goto err_files;
681
682         retval = device_create_file(&interface->dev,
683                                     &ASUS_OLED_DEVICE_ATTR(picture));
684         if (retval)
685                 goto err_files;
686
687         odev->dev = device_create(oled_class, &interface->dev, MKDEV(0, 0),
688                                   NULL, "oled_%d", ++oled_num);
689
690         if (IS_ERR(odev->dev)) {
691                 retval = PTR_ERR(odev->dev);
692                 goto err_files;
693         }
694
695         dev_set_drvdata(odev->dev, odev);
696
697         retval = device_create_file(odev->dev, &dev_attr_enabled);
698         if (retval)
699                 goto err_class_enabled;
700
701         retval = device_create_file(odev->dev, &dev_attr_picture);
702         if (retval)
703                 goto err_class_picture;
704
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);
708
709         if (start_off)
710                 enable_oled(odev, 0);
711
712         return 0;
713
714 err_class_picture:
715         device_remove_file(odev->dev, &dev_attr_picture);
716
717 err_class_enabled:
718         device_remove_file(odev->dev, &dev_attr_enabled);
719         device_unregister(odev->dev);
720
721 err_files:
722         device_remove_file(&interface->dev, &ASUS_OLED_DEVICE_ATTR(enabled));
723         device_remove_file(&interface->dev, &ASUS_OLED_DEVICE_ATTR(picture));
724
725         usb_set_intfdata(interface, NULL);
726         usb_put_dev(odev->udev);
727         kfree(odev);
728
729         return retval;
730 }
731
732 static void asus_oled_disconnect(struct usb_interface *interface)
733 {
734         struct asus_oled_dev *odev;
735
736         odev = usb_get_intfdata(interface);
737         usb_set_intfdata(interface, NULL);
738
739         device_remove_file(odev->dev, &dev_attr_picture);
740         device_remove_file(odev->dev, &dev_attr_enabled);
741         device_unregister(odev->dev);
742
743         device_remove_file(&interface->dev, &ASUS_OLED_DEVICE_ATTR(picture));
744         device_remove_file(&interface->dev, &ASUS_OLED_DEVICE_ATTR(enabled));
745
746         usb_put_dev(odev->udev);
747
748         kfree(odev->buf);
749
750         kfree(odev);
751
752         dev_info(&interface->dev, "Disconnected Asus OLED device\n");
753 }
754
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,
760 };
761
762 static ssize_t version_show(struct class *dev, char *buf)
763 {
764         return sprintf(buf, ASUS_OLED_UNDERSCORE_NAME " %s\n",
765                        ASUS_OLED_VERSION);
766 }
767
768 static CLASS_ATTR(version, S_IRUGO, version_show, NULL);
769
770 static int __init asus_oled_init(void)
771 {
772         int retval = 0;
773         oled_class = class_create(THIS_MODULE, ASUS_OLED_UNDERSCORE_NAME);
774
775         if (IS_ERR(oled_class)) {
776                 err("Error creating " ASUS_OLED_UNDERSCORE_NAME " class");
777                 return PTR_ERR(oled_class);
778         }
779
780         retval = class_create_file(oled_class, &class_attr_version);
781         if (retval) {
782                 err("Error creating class version file");
783                 goto error;
784         }
785
786         retval = usb_register(&oled_driver);
787
788         if (retval) {
789                 err("usb_register failed. Error number %d", retval);
790                 goto error;
791         }
792
793         return retval;
794
795 error:
796         class_destroy(oled_class);
797         return retval;
798 }
799
800 static void __exit asus_oled_exit(void)
801 {
802         class_remove_file(oled_class, &class_attr_version);
803         class_destroy(oled_class);
804
805         usb_deregister(&oled_driver);
806 }
807
808 module_init(asus_oled_init);
809 module_exit(asus_oled_exit);
810