Staging: asus_oled: Add defines for asus vendor_id and device_id to be consistent...
[cascardo/linux.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 #define USB_VENDOR_ID_ASUS      0x0b05
56 #define USB_DEVICE_ID_ASUS_LCM      0x1726
57 #define USB_DEVICE_ID_ASUS_LCM2     0x175b
58
59 MODULE_AUTHOR("Jakub Schmidtke, sjakub@gmail.com");
60 MODULE_DESCRIPTION("Asus OLED Driver v" ASUS_OLED_VERSION);
61 MODULE_LICENSE("GPL");
62
63 static struct class *oled_class;
64 static int oled_num;
65
66 static uint start_off;
67
68 module_param(start_off, uint, 0644);
69
70 MODULE_PARM_DESC(start_off,
71                  "Set to 1 to switch off OLED display after it is attached");
72
73 enum oled_pack_mode{
74         PACK_MODE_G1,
75         PACK_MODE_G50,
76         PACK_MODE_LAST
77 };
78
79 struct oled_dev_desc_str {
80         uint16_t                idVendor;
81         uint16_t                idProduct;
82         /* width of display */
83         uint16_t                devWidth;
84         /* formula to be used while packing the picture */
85         enum oled_pack_mode     packMode;
86         const char              *devDesc;
87 };
88
89 /* table of devices that work with this driver */
90 static struct usb_device_id id_table[] = {
91         /* Asus G1/G2 (and variants)*/
92         { USB_DEVICE(USB_VENDOR_ID_ASUS, USB_DEVICE_ID_ASUS_LCM) },
93         /* Asus G50V (and possibly others - G70? G71?)*/
94         { USB_DEVICE(USB_VENDOR_ID_ASUS, USB_DEVICE_ID_ASUS_LCM2) },
95         { },
96 };
97
98 /* parameters of specific devices */
99 static struct oled_dev_desc_str oled_dev_desc_table[] = {
100         { 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" },
104         { },
105 };
106
107 MODULE_DEVICE_TABLE(usb, id_table);
108
109 struct 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
122 struct asus_oled_packet {
123         struct asus_oled_header         header;
124         uint8_t                         bitmap[ASUS_OLED_PACKET_BUF_SIZE];
125 } __attribute((packed));
126
127 struct asus_oled_dev {
128         struct usb_device       *udev;
129         uint8_t                 pic_mode;
130         uint16_t                dev_width;
131         enum oled_pack_mode     pack_mode;
132         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
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)
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
160 static void enable_oled(struct asus_oled_dev *odev, uint8_t enabl)
161 {
162         int a;
163         int retval;
164         int act_len;
165         struct asus_oled_packet *packet;
166
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
174         setup_packet_header(packet, 0x20, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00);
175
176         if (enabl)
177                 packet->bitmap[0] = 0xaf;
178         else
179                 packet->bitmap[0] = 0xae;
180
181         for (a = 0; a < 1; a++) {
182                 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
198 static ssize_t set_enabled(struct device *dev, struct device_attribute *attr,
199                            const char *buf, size_t count)
200 {
201         struct usb_interface *intf = to_usb_interface(dev);
202         struct asus_oled_dev *odev = usb_get_intfdata(intf);
203         unsigned long value;
204         if (strict_strtoul(buf, 10, &value))
205                 return -EINVAL;
206
207         enable_oled(odev, value);
208
209         return count;
210 }
211
212 static ssize_t class_set_enabled(struct device *device,
213                                  struct device_attribute *attr,
214                                  const char *buf, size_t count)
215 {
216         struct asus_oled_dev *odev =
217                 (struct asus_oled_dev *) dev_get_drvdata(device);
218         unsigned long value;
219
220         if (strict_strtoul(buf, 10, &value))
221                 return -EINVAL;
222
223         enable_oled(odev, value);
224
225         return count;
226 }
227
228 static ssize_t get_enabled(struct device *dev, struct device_attribute *attr,
229                            char *buf)
230 {
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
237 static ssize_t class_get_enabled(struct device *device,
238                                  struct device_attribute *attr, char *buf)
239 {
240         struct asus_oled_dev *odev =
241                 (struct asus_oled_dev *) dev_get_drvdata(device);
242
243         return sprintf(buf, "%d\n", odev->enabled);
244 }
245
246 static void send_packets(struct usb_device *udev,
247                          struct asus_oled_packet *packet,
248                          char *buf, uint8_t p_type, size_t p_num)
249 {
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) {
257                 case ASUS_OLED_ROLL:
258                         setup_packet_header(packet, 0x40, 0x80, p_num,
259                                             i + 1, 0x00, 0x01, 0xff);
260                         break;
261                 case ASUS_OLED_STATIC:
262                         setup_packet_header(packet, 0x10 + i, 0x80, 0x01,
263                                             0x01, 0x00, 0x01, 0x00);
264                         break;
265                 case ASUS_OLED_FLASH:
266                         setup_packet_header(packet, 0x10 + i, 0x80, 0x01,
267                                             0x01, 0x00, 0x00, 0xff);
268                         break;
269                 }
270
271                 memcpy(packet->bitmap, buf + (ASUS_OLED_PACKET_BUF_SIZE*i),
272                        ASUS_OLED_PACKET_BUF_SIZE);
273
274                 retval = usb_bulk_msg(udev, usb_sndctrlpipe(udev, 2),
275                                       packet, sizeof(struct asus_oled_packet),
276                                       &act_len, -1);
277
278                 if (retval)
279                         dev_dbg(&udev->dev, "retval = %d\n", retval);
280         }
281 }
282
283 static 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) {
288         int retval;
289         int act_len;
290
291         setup_packet_header(packet, b1, b2, b3, b4, b5, b6, 0x00);
292         memcpy(packet->bitmap, buf + offset, len);
293
294         retval = usb_bulk_msg(udev,
295                               usb_sndctrlpipe(udev, 2),
296                               packet,
297                               sizeof(struct asus_oled_packet),
298                               &act_len,
299                               -1);
300
301         if (retval)
302                 dev_dbg(&udev->dev, "retval = %d\n", retval);
303 }
304
305
306 static void send_packets_g50(struct usb_device *udev,
307                              struct asus_oled_packet *packet, char *buf)
308 {
309         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);
313
314         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);
320 }
321
322
323 static void send_data(struct asus_oled_dev *odev)
324 {
325         size_t packet_num = odev->buf_size / ASUS_OLED_PACKET_BUF_SIZE;
326         struct asus_oled_packet *packet;
327
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
335         if (odev->pack_mode == PACK_MODE_G1) {
336                 /* 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. */
339                 if (odev->pic_mode == ASUS_OLED_ROLL)
340                         send_packets(odev->udev, packet, odev->buf,
341                                      ASUS_OLED_STATIC, 2);
342
343                 /* Only ROLL mode can use more than 2 packets.*/
344                 if (odev->pic_mode != ASUS_OLED_ROLL && packet_num > 2)
345                         packet_num = 2;
346
347                 send_packets(odev->udev, packet, odev->buf,
348                              odev->pic_mode, packet_num);
349         } else if (odev->pack_mode == PACK_MODE_G50) {
350                 send_packets_g50(odev->udev, packet, odev->buf);
351         }
352
353         kfree(packet);
354 }
355
356 static int append_values(struct asus_oled_dev *odev, uint8_t val, size_t count)
357 {
358         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;
362
363                 x += odev->x_shift;
364                 y += odev->y_shift;
365
366                 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;
375
376                 case PACK_MODE_G50:
377                         i =  (odev->dev_width - 1 - x)/8 + y*odev->dev_width/8;
378                         break;
379
380                 default:
381                         i = 0;
382                         printk(ASUS_OLED_ERROR "Unknown OLED Pack Mode: %d!\n",
383                                odev->pack_mode);
384                         break;
385                 }
386
387                 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                 }
394
395                 switch (odev->pack_mode) {
396                 case PACK_MODE_G1:
397                         odev->buf[i] &= ~(1<<(y%8));
398                         break;
399
400                 case PACK_MODE_G50:
401                         odev->buf[i] &= ~(1<<(x%8));
402                         break;
403
404                 default:
405                         /* cannot get here; stops gcc complaining*/
406                         ;
407                 }
408
409                 odev->last_val = val;
410                 odev->buf_offs++;
411         }
412
413         return 0;
414 }
415
416 static ssize_t odev_set_picture(struct asus_oled_dev *odev,
417                                 const char *buf, size_t count)
418 {
419         size_t offs = 0, max_offs;
420
421         if (count < 1)
422                 return 0;
423
424         if (tolower(buf[0]) == 'b') {
425                 /* binary mode, set the entire memory*/
426
427                 size_t i;
428
429                 odev->buf_size = (odev->dev_width * ASUS_OLED_DISP_HEIGHT) / 8;
430
431                 kfree(odev->buf);
432                 odev->buf = kmalloc(odev->buf_size, GFP_KERNEL);
433
434                 memset(odev->buf, 0xff, odev->buf_size);
435
436                 for (i = 1; i < count && i <= 32 * 32; i++) {
437                         odev->buf[i-1] = buf[i];
438                         odev->buf_offs = i-1;
439                 }
440
441                 odev->width = odev->dev_width / 8;
442                 odev->height = ASUS_OLED_DISP_HEIGHT;
443                 odev->x_shift = 0;
444                 odev->y_shift = 0;
445                 odev->last_val =  0;
446
447                 send_data(odev);
448
449                 return count;
450         }
451
452         if (buf[0] == '<') {
453                 size_t i;
454                 size_t w = 0, h = 0;
455                 size_t w_mem, h_mem;
456
457                 if (count < 10 || buf[2] != ':')
458                         goto error_header;
459
460
461                 switch (tolower(buf[1])) {
462                 case ASUS_OLED_STATIC:
463                 case ASUS_OLED_ROLL:
464                 case ASUS_OLED_FLASH:
465                         odev->pic_mode = buf[1];
466                         break;
467                 default:
468                         printk(ASUS_OLED_ERROR "Wrong picture mode: '%c'.\n",
469                                buf[1]);
470                         return -EIO;
471                         break;
472                 }
473
474                 for (i = 3; i < count; ++i) {
475                         if (buf[i] >= '0' && buf[i] <= '9') {
476                                 w = 10*w + (buf[i] - '0');
477
478                                 if (w > ASUS_OLED_MAX_WIDTH)
479                                         goto error_width;
480                         } else if (tolower(buf[i]) == 'x') {
481                                 break;
482                         } else {
483                                 goto error_width;
484                         }
485                 }
486
487                 for (++i; i < count; ++i) {
488                         if (buf[i] >= '0' && buf[i] <= '9') {
489                                 h = 10*h + (buf[i] - '0');
490
491                                 if (h > ASUS_OLED_DISP_HEIGHT)
492                                         goto error_height;
493                         } else if (tolower(buf[i]) == '>') {
494                                 break;
495                         } else {
496                                 goto error_height;
497                         }
498                 }
499
500                 if (w < 1 || w > ASUS_OLED_MAX_WIDTH)
501                         goto error_width;
502
503                 if (h < 1 || h > ASUS_OLED_DISP_HEIGHT)
504                         goto error_height;
505
506                 if (i >= count || buf[i] != '>')
507                         goto error_header;
508
509                 offs = i+1;
510
511                 if (w % (odev->dev_width) != 0)
512                         w_mem = (w/(odev->dev_width) + 1)*(odev->dev_width);
513                 else
514                         w_mem = w;
515
516                 if (h < ASUS_OLED_DISP_HEIGHT)
517                         h_mem = ASUS_OLED_DISP_HEIGHT;
518                 else
519                         h_mem = h;
520
521                 odev->buf_size = w_mem * h_mem / 8;
522
523                 kfree(odev->buf);
524                 odev->buf = kmalloc(odev->buf_size, GFP_KERNEL);
525
526                 if (odev->buf == NULL) {
527                         odev->buf_size = 0;
528                         printk(ASUS_OLED_ERROR "Out of memory!\n");
529                         return -ENOMEM;
530                 }
531
532                 memset(odev->buf, 0xff, odev->buf_size);
533
534                 odev->buf_offs = 0;
535                 odev->width = w;
536                 odev->height = h;
537                 odev->x_shift = 0;
538                 odev->y_shift = 0;
539                 odev->last_val = 0;
540
541                 if (odev->pic_mode == ASUS_OLED_FLASH) {
542                         if (h < ASUS_OLED_DISP_HEIGHT/2)
543                                 odev->y_shift = (ASUS_OLED_DISP_HEIGHT/2 - h)/2;
544                 } else {
545                         if (h < ASUS_OLED_DISP_HEIGHT)
546                                 odev->y_shift = (ASUS_OLED_DISP_HEIGHT - h)/2;
547                 }
548
549                 if (w < (odev->dev_width))
550                         odev->x_shift = ((odev->dev_width) - w)/2;
551         }
552
553         max_offs = odev->width * odev->height;
554
555         while (offs < count && odev->buf_offs < max_offs) {
556                 int ret = 0;
557
558                 if (buf[offs] == '1' || buf[offs] == '#') {
559                         ret = append_values(odev, 1, 1);
560                         if (ret < 0)
561                                 return ret;
562                 } else if (buf[offs] == '0' || buf[offs] == ' ') {
563                         ret = append_values(odev, 0, 1);
564                         if (ret < 0)
565                                 return ret;
566                 } else if (buf[offs] == '\n') {
567                         /* New line detected. Lets assume, that all characters
568                            till the end of the line were equal to the last
569                            character in this line.*/
570                         if (odev->buf_offs % odev->width != 0)
571                                 ret = append_values(odev, odev->last_val,
572                                                     odev->width -
573                                                     (odev->buf_offs %
574                                                      odev->width));
575                         if (ret < 0)
576                                 return ret;
577                 }
578
579                 offs++;
580         }
581
582         if (odev->buf_offs >= max_offs)
583                 send_data(odev);
584
585         return count;
586
587 error_width:
588         printk(ASUS_OLED_ERROR "Wrong picture width specified.\n");
589         return -EIO;
590
591 error_height:
592         printk(ASUS_OLED_ERROR "Wrong picture height specified.\n");
593         return -EIO;
594
595 error_header:
596         printk(ASUS_OLED_ERROR "Wrong picture header.\n");
597         return -EIO;
598 }
599
600 static ssize_t set_picture(struct device *dev, struct device_attribute *attr,
601                            const char *buf, size_t count)
602 {
603         struct usb_interface *intf = to_usb_interface(dev);
604
605         return odev_set_picture(usb_get_intfdata(intf), buf, count);
606 }
607
608 static ssize_t class_set_picture(struct device *device,
609                                  struct device_attribute *attr,
610                                  const char *buf, size_t count)
611 {
612         return odev_set_picture((struct asus_oled_dev *)
613                                 dev_get_drvdata(device), buf, count);
614 }
615
616 #define ASUS_OLED_DEVICE_ATTR(_file)            dev_attr_asus_oled_##_file
617
618 static DEVICE_ATTR(asus_oled_enabled, S_IWUGO | S_IRUGO,
619                    get_enabled, set_enabled);
620 static DEVICE_ATTR(asus_oled_picture, S_IWUGO , NULL, set_picture);
621
622 static DEVICE_ATTR(enabled, S_IWUGO | S_IRUGO,
623                    class_get_enabled, class_set_enabled);
624 static DEVICE_ATTR(picture, S_IWUGO, NULL, class_set_picture);
625
626 static int asus_oled_probe(struct usb_interface *interface,
627                            const struct usb_device_id *id)
628 {
629         struct usb_device *udev = interface_to_usbdev(interface);
630         struct asus_oled_dev *odev = NULL;
631         int retval = -ENOMEM;
632         uint16_t dev_width = 0;
633         enum oled_pack_mode pack_mode = PACK_MODE_LAST;
634         const struct oled_dev_desc_str *dev_desc = oled_dev_desc_table;
635         const char *desc = NULL;
636
637         if (!id) {
638                 /* Even possible? Just to make sure...*/
639                 dev_err(&interface->dev, "No usb_device_id provided!\n");
640                 return -ENODEV;
641         }
642
643         for (; dev_desc->idVendor; dev_desc++) {
644                 if (dev_desc->idVendor == id->idVendor
645                     && dev_desc->idProduct == id->idProduct) {
646                         dev_width = dev_desc->devWidth;
647                         desc = dev_desc->devDesc;
648                         pack_mode = dev_desc->packMode;
649                         break;
650                 }
651         }
652
653         if (!desc || dev_width < 1 || pack_mode == PACK_MODE_LAST) {
654                 dev_err(&interface->dev,
655                         "Missing or incomplete device description!\n");
656                 return -ENODEV;
657         }
658
659         odev = kzalloc(sizeof(struct asus_oled_dev), GFP_KERNEL);
660
661         if (odev == NULL) {
662                 dev_err(&interface->dev, "Out of memory\n");
663                 return -ENOMEM;
664         }
665
666         odev->udev = usb_get_dev(udev);
667         odev->pic_mode = ASUS_OLED_STATIC;
668         odev->dev_width = dev_width;
669         odev->pack_mode = pack_mode;
670         odev->height = 0;
671         odev->width = 0;
672         odev->x_shift = 0;
673         odev->y_shift = 0;
674         odev->buf_offs = 0;
675         odev->buf_size = 0;
676         odev->last_val = 0;
677         odev->buf = NULL;
678         odev->enabled = 1;
679         odev->dev = NULL;
680
681         usb_set_intfdata(interface, odev);
682
683         retval = device_create_file(&interface->dev,
684                                     &ASUS_OLED_DEVICE_ATTR(enabled));
685         if (retval)
686                 goto err_files;
687
688         retval = device_create_file(&interface->dev,
689                                     &ASUS_OLED_DEVICE_ATTR(picture));
690         if (retval)
691                 goto err_files;
692
693         odev->dev = device_create(oled_class, &interface->dev, MKDEV(0, 0),
694                                   NULL, "oled_%d", ++oled_num);
695
696         if (IS_ERR(odev->dev)) {
697                 retval = PTR_ERR(odev->dev);
698                 goto err_files;
699         }
700
701         dev_set_drvdata(odev->dev, odev);
702
703         retval = device_create_file(odev->dev, &dev_attr_enabled);
704         if (retval)
705                 goto err_class_enabled;
706
707         retval = device_create_file(odev->dev, &dev_attr_picture);
708         if (retval)
709                 goto err_class_picture;
710
711         dev_info(&interface->dev,
712                  "Attached Asus OLED device: %s [width %u, pack_mode %d]\n",
713                  desc, odev->dev_width, odev->pack_mode);
714
715         if (start_off)
716                 enable_oled(odev, 0);
717
718         return 0;
719
720 err_class_picture:
721         device_remove_file(odev->dev, &dev_attr_picture);
722
723 err_class_enabled:
724         device_remove_file(odev->dev, &dev_attr_enabled);
725         device_unregister(odev->dev);
726
727 err_files:
728         device_remove_file(&interface->dev, &ASUS_OLED_DEVICE_ATTR(enabled));
729         device_remove_file(&interface->dev, &ASUS_OLED_DEVICE_ATTR(picture));
730
731         usb_set_intfdata(interface, NULL);
732         usb_put_dev(odev->udev);
733         kfree(odev);
734
735         return retval;
736 }
737
738 static void asus_oled_disconnect(struct usb_interface *interface)
739 {
740         struct asus_oled_dev *odev;
741
742         odev = usb_get_intfdata(interface);
743         usb_set_intfdata(interface, NULL);
744
745         device_remove_file(odev->dev, &dev_attr_picture);
746         device_remove_file(odev->dev, &dev_attr_enabled);
747         device_unregister(odev->dev);
748
749         device_remove_file(&interface->dev, &ASUS_OLED_DEVICE_ATTR(picture));
750         device_remove_file(&interface->dev, &ASUS_OLED_DEVICE_ATTR(enabled));
751
752         usb_put_dev(odev->udev);
753
754         kfree(odev->buf);
755
756         kfree(odev);
757
758         dev_info(&interface->dev, "Disconnected Asus OLED device\n");
759 }
760
761 static struct usb_driver oled_driver = {
762         .name =         ASUS_OLED_NAME,
763         .probe =        asus_oled_probe,
764         .disconnect =   asus_oled_disconnect,
765         .id_table =     id_table,
766 };
767
768 static ssize_t version_show(struct class *dev, char *buf)
769 {
770         return sprintf(buf, ASUS_OLED_UNDERSCORE_NAME " %s\n",
771                        ASUS_OLED_VERSION);
772 }
773
774 static CLASS_ATTR(version, S_IRUGO, version_show, NULL);
775
776 static 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
786         retval = class_create_file(oled_class, &class_attr_version);
787         if (retval) {
788                 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
801 error:
802         class_destroy(oled_class);
803         return retval;
804 }
805
806 static void __exit asus_oled_exit(void)
807 {
808         class_remove_file(oled_class, &class_attr_version);
809         class_destroy(oled_class);
810
811         usb_deregister(&oled_driver);
812 }
813
814 module_init(asus_oled_init);
815 module_exit(asus_oled_exit);
816