1 /*****************************************************************************
3 * Version 0.2 (udlfb) *
4 * (C) 2009 Roberto De Ioris <roberto@unbit.it> *
6 * This file is licensed under the GPLv2. See COPYING in the package. *
7 * Based on the amazing work of Florian Echtler and libdlo 0.1 *
10 * 10.06.09 release 0.2.3 (edid ioctl, fallback for unsupported modes) *
11 * 05.06.09 release 0.2.2 (real screen blanking, rle compression, double buffer) *
12 * 31.05.09 release 0.2 *
13 * 22.05.09 First public (ugly) release *
14 *****************************************************************************/
16 #include <linux/module.h>
17 #include <linux/kernel.h>
18 #include <linux/init.h>
19 #include <linux/usb.h>
20 #include <linux/uaccess.h>
23 #include <linux/mutex.h>
24 #include <linux/vmalloc.h>
28 #define DRIVER_VERSION "DLFB 0.2"
30 /* memory functions taken from vfb */
32 static void *rvmalloc(unsigned long size)
37 size = PAGE_ALIGN(size);
38 mem = vmalloc_32(size);
42 memset(mem, 0, size); /* Clear the ram out, no junk to the user */
43 adr = (unsigned long)mem;
45 SetPageReserved(vmalloc_to_page((void *)adr));
53 static void rvfree(void *mem, unsigned long size)
60 adr = (unsigned long)mem;
61 while ((long)size > 0) {
62 ClearPageReserved(vmalloc_to_page((void *)adr));
69 static int dlfb_mmap(struct fb_info *info, struct vm_area_struct *vma)
71 unsigned long start = vma->vm_start;
72 unsigned long size = vma->vm_end - vma->vm_start;
73 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
74 unsigned long page, pos;
76 printk("MMAP: %lu %u\n", offset + size, info->fix.smem_len);
78 if (offset + size > info->fix.smem_len)
81 pos = (unsigned long)info->fix.smem_start + offset;
84 page = vmalloc_to_pfn((void *)pos);
85 if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED))
96 vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
101 /* ioctl structure */
109 static struct usb_device_id id_table [] = {
110 { USB_DEVICE(0x17e9, 0x023d) },
115 static struct usb_device_id id_table[] = {
116 {.idVendor = 0x17e9, .match_flags = USB_DEVICE_ID_MATCH_VENDOR,},
119 MODULE_DEVICE_TABLE(usb, id_table);
121 static struct usb_driver dlfb_driver;
123 // thanks to Henrik Bjerregaard Pedersen for this function
124 static char *rle_compress16(uint16_t * src, char *dst, int rem)
129 char *end_if_raw = dst + 6 + 2 * rem;
131 dst += 6; // header will be filled in if RLE is worth it
133 while (rem && dst < end_if_raw) {
134 char *start = (char *)src;
139 while (rem && *src == pix0)
150 Thanks to Henrik Bjerregaard Pedersen for rle implementation and code refactoring.
151 Next step is huffman compression.
155 image_blit(struct dlfb_data *dev_info, int x, int y, int width, int height,
163 int firstdiff, thistime;
167 if (x + width > dev_info->info->var.xres)
170 if (y + height > dev_info->info->var.yres)
173 mutex_lock(&dev_info->bulk_mutex);
176 dev_info->base16 + ((dev_info->info->var.xres * 2 * y) + (x * 2));
178 data += (dev_info->info->var.xres * 2 * y) + (x * 2);
180 /* printk("IMAGE_BLIT\n"); */
182 bufptr = dev_info->buf;
184 for (i = y; i < y + height; i++) {
186 if (dev_info->bufend - bufptr < BUF_HIGH_WATER_MARK) {
187 ret = dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
188 bufptr = dev_info->buf;
193 /* printk("WRITING LINE %d\n", i); */
197 if (dev_info->bufend - bufptr < BUF_HIGH_WATER_MARK) {
199 dlfb_bulk_msg(dev_info,
200 bufptr - dev_info->buf);
201 bufptr = dev_info->buf;
203 // number of pixels to consider this time
208 // find position of first pixel that has changed
210 for (j = 0; j < thistime * 2; j++) {
211 if (dev_info->backing_buffer
212 [base - dev_info->base16 + j] != data[j]) {
218 if (firstdiff >= 0) {
222 rle_compress16((uint16_t *) (data +
225 thistime - firstdiff);
228 bufptr + 6 + 2 * (thistime - firstdiff)) {
234 firstdiff * 2) >> 16);
236 (char)((base + firstdiff * 2) >> 8);
238 (char)(base + firstdiff * 2);
239 bufptr[5] = thistime - firstdiff;
244 // fallback to raw (or some other encoding?)
250 firstdiff * 2) >> 16);
252 (char)((base + firstdiff * 2) >> 8);
254 (char)(base + firstdiff * 2);
255 *bufptr++ = thistime - firstdiff;
256 // PUT COMPRESSION HERE
257 for (j = firstdiff * 2;
258 j < thistime * 2; j += 2) {
259 *bufptr++ = data[j + 1];
265 base += thistime * 2;
266 data += thistime * 2;
270 memcpy(dev_info->backing_buffer + (base - dev_info->base16) -
271 (width * 2), data - (width * 2), width * 2);
273 base += (dev_info->info->var.xres * 2) - (width * 2);
274 data += (dev_info->info->var.xres * 2) - (width * 2);
278 if (bufptr > dev_info->buf) {
279 ret = dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
282 mutex_unlock(&dev_info->bulk_mutex);
289 draw_rect(struct dlfb_data *dev_info, int x, int y, int width, int height,
290 unsigned char red, unsigned char green, unsigned char blue)
296 (((((red) & 0xF8) | ((green) >> 5)) & 0xFF) << 8) +
297 (((((green) & 0x1C) << 3) | ((blue) >> 3)) & 0xFF);
302 if (x + width > dev_info->info->var.xres)
305 if (y + height > dev_info->info->var.yres)
308 mutex_lock(&dev_info->bulk_mutex);
310 base = dev_info->base16 + (dev_info->info->var.xres * 2 * y) + (x * 2);
312 bufptr = dev_info->buf;
314 for (i = y; i < y + height; i++) {
316 for (j = 0; j < width * 2; j += 2) {
317 dev_info->backing_buffer[base - dev_info->base16 + j] =
319 dev_info->backing_buffer[base - dev_info->base16 + j +
322 if (dev_info->bufend - bufptr < BUF_HIGH_WATER_MARK) {
323 ret = dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
324 bufptr = dev_info->buf;
331 if (dev_info->bufend - bufptr < BUF_HIGH_WATER_MARK) {
333 dlfb_bulk_msg(dev_info,
334 bufptr - dev_info->buf);
335 bufptr = dev_info->buf;
341 *bufptr++ = (char)(base >> 16);
342 *bufptr++ = (char)(base >> 8);
343 *bufptr++ = (char)(base);
357 *bufptr++ = (char)(col >> 8);
358 *bufptr++ = (char)(col);
362 base += (dev_info->info->var.xres * 2) - (width * 2);
366 if (bufptr > dev_info->buf)
367 ret = dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
369 mutex_unlock(&dev_info->bulk_mutex);
374 static void swapfb(struct dlfb_data *dev_info)
380 mutex_lock(&dev_info->bulk_mutex);
382 tmpbase = dev_info->base16;
384 dev_info->base16 = dev_info->base16d;
385 dev_info->base16d = tmpbase;
387 bufptr = dev_info->buf;
389 bufptr = dlfb_set_register(bufptr, 0xFF, 0x00);
393 dlfb_set_register(bufptr, 0x20, (char)(dev_info->base16 >> 16));
394 bufptr = dlfb_set_register(bufptr, 0x21, (char)(dev_info->base16 >> 8));
395 bufptr = dlfb_set_register(bufptr, 0x22, (char)(dev_info->base16));
397 bufptr = dlfb_set_register(bufptr, 0xFF, 0x00);
399 dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
401 mutex_unlock(&dev_info->bulk_mutex);
404 static int copyfb(struct dlfb_data *dev_info)
413 base = dev_info->base16d;
415 mutex_lock(&dev_info->bulk_mutex);
417 source = dev_info->base16;
419 bufptr = dev_info->buf;
421 for (i = 0; i < dev_info->info->var.yres; i++) {
423 if (dev_info->bufend - bufptr < BUF_HIGH_WATER_MARK) {
424 ret = dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
425 bufptr = dev_info->buf;
428 rem = dev_info->info->var.xres;
432 if (dev_info->bufend - bufptr < BUF_HIGH_WATER_MARK) {
434 dlfb_bulk_msg(dev_info,
435 bufptr - dev_info->buf);
436 bufptr = dev_info->buf;
443 *bufptr++ = (char)(base >> 16);
444 *bufptr++ = (char)(base >> 8);
445 *bufptr++ = (char)(base);
449 *bufptr++ = (char)(source >> 16);
450 *bufptr++ = (char)(source >> 8);
451 *bufptr++ = (char)(source);
459 *bufptr++ = (char)(source >> 16);
460 *bufptr++ = (char)(source >> 8);
461 *bufptr++ = (char)(source);
470 if (bufptr > dev_info->buf)
471 ret = dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
473 mutex_unlock(&dev_info->bulk_mutex);
480 copyarea(struct dlfb_data *dev_info, int dx, int dy, int sx, int sy,
481 int width, int height)
490 if (dx + width > dev_info->info->var.xres)
493 if (dy + height > dev_info->info->var.yres)
496 mutex_lock(&dev_info->bulk_mutex);
499 dev_info->base16 + (dev_info->info->var.xres * 2 * dy) + (dx * 2);
500 source = (dev_info->info->var.xres * 2 * sy) + (sx * 2);
502 bufptr = dev_info->buf;
504 for (i = sy; i < sy + height; i++) {
506 memcpy(dev_info->backing_buffer + base - dev_info->base16,
507 dev_info->backing_buffer + source, width * 2);
509 if (dev_info->bufend - bufptr < BUF_HIGH_WATER_MARK) {
510 ret = dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
511 bufptr = dev_info->buf;
518 if (dev_info->bufend - bufptr < BUF_HIGH_WATER_MARK) {
520 dlfb_bulk_msg(dev_info,
521 bufptr - dev_info->buf);
522 bufptr = dev_info->buf;
528 *bufptr++ = (char)(base >> 16);
529 *bufptr++ = (char)(base >> 8);
530 *bufptr++ = (char)(base);
534 *bufptr++ = (char)(source >> 16);
535 *bufptr++ = (char)(source >> 8);
536 *bufptr++ = (char)(source);
544 *bufptr++ = (char)(source >> 16);
545 *bufptr++ = (char)(source >> 8);
546 *bufptr++ = (char)(source);
554 base += (dev_info->info->var.xres * 2) - (width * 2);
555 source += (dev_info->info->var.xres * 2) - (width * 2);
558 if (bufptr > dev_info->buf)
559 ret = dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
561 mutex_unlock(&dev_info->bulk_mutex);
566 static void dlfb_copyarea(struct fb_info *info, const struct fb_copyarea *area)
569 struct dlfb_data *dev = info->par;
571 copyarea(dev, area->dx, area->dy, area->sx, area->sy, area->width,
574 /* printk("COPY AREA %d %d %d %d %d %d !!!\n", area->dx, area->dy, area->sx, area->sy, area->width, area->height); */
578 static void dlfb_imageblit(struct fb_info *info, const struct fb_image *image)
582 struct dlfb_data *dev = info->par;
583 /* printk("IMAGE BLIT (1) %d %d %d %d DEPTH %d {%p}!!!\n", image->dx, image->dy, image->width, image->height, image->depth, dev->udev); */
584 cfb_imageblit(info, image);
586 image_blit(dev, image->dx, image->dy, image->width, image->height,
588 /* printk("IMAGE BLIT (2) %d %d %d %d DEPTH %d {%p} %d!!!\n", image->dx, image->dy, image->width, image->height, image->depth, dev->udev, ret); */
591 static void dlfb_fillrect(struct fb_info *info,
592 const struct fb_fillrect *region)
595 unsigned char red, green, blue;
596 struct dlfb_data *dev = info->par;
598 memcpy(&red, ®ion->color, 1);
599 memcpy(&green, ®ion->color + 1, 1);
600 memcpy(&blue, ®ion->color + 2, 1);
601 draw_rect(dev, region->dx, region->dy, region->width, region->height,
603 /* printk("FILL RECT %d %d !!!\n", region->dx, region->dy); */
607 static int dlfb_ioctl(struct fb_info *info, unsigned int cmd, unsigned long arg)
610 struct dlfb_data *dev_info = info->par;
611 struct dloarea *area = NULL;
614 char *edid = (char *)arg;
616 if (copy_to_user(edid, dev_info->edid, 128)) {
622 if (cmd == 0xAA || cmd == 0xAB || cmd == 0xAC) {
624 area = (struct dloarea *)arg;
629 if (area->x > info->var.xres)
630 area->x = info->var.xres;
635 if (area->y > info->var.yres)
636 area->y = info->var.yres;
640 image_blit(dev_info, area->x, area->y, area->w, area->h,
645 image_blit(dev_info, area->x, area->y, area->w, area->h,
648 } else if (cmd == 0xAB) {
657 area->x2, area->y2, area->x, area->y, area->w,
663 /* taken from vesafb */
666 dlfb_setcolreg(unsigned regno, unsigned red, unsigned green,
667 unsigned blue, unsigned transp, struct fb_info *info)
671 if (regno >= info->cmap.len)
675 if (info->var.red.offset == 10) {
677 ((u32 *) (info->pseudo_palette))[regno] =
678 ((red & 0xf800) >> 1) |
679 ((green & 0xf800) >> 6) | ((blue & 0xf800) >> 11);
682 ((u32 *) (info->pseudo_palette))[regno] =
684 ((green & 0xfc00) >> 5) | ((blue & 0xf800) >> 11);
691 static int dlfb_release(struct fb_info *info, int user)
693 struct dlfb_data *dev_info = info->par;
694 image_blit(dev_info, 0, 0, info->var.xres, info->var.yres,
699 static int dlfb_blank(int blank_mode, struct fb_info *info)
701 struct dlfb_data *dev_info = info->par;
702 char *bufptr = dev_info->buf;
704 bufptr = dlfb_set_register(bufptr, 0xFF, 0x00);
705 if (blank_mode != FB_BLANK_UNBLANK) {
706 bufptr = dlfb_set_register(bufptr, 0x1F, 0x01);
708 bufptr = dlfb_set_register(bufptr, 0x1F, 0x00);
710 bufptr = dlfb_set_register(bufptr, 0xFF, 0xFF);
712 dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
717 static struct fb_ops dlfb_ops = {
718 .fb_setcolreg = dlfb_setcolreg,
719 .fb_fillrect = dlfb_fillrect,
720 .fb_copyarea = dlfb_copyarea,
721 .fb_imageblit = dlfb_imageblit,
722 .fb_mmap = dlfb_mmap,
723 .fb_ioctl = dlfb_ioctl,
724 .fb_release = dlfb_release,
725 .fb_blank = dlfb_blank,
729 dlfb_probe(struct usb_interface *interface, const struct usb_device_id *id)
731 struct dlfb_data *dev_info;
732 struct fb_info *info;
737 dev_info = kzalloc(sizeof(*dev_info), GFP_KERNEL);
738 if (dev_info == NULL) {
739 printk("cannot allocate dev_info structure.\n");
743 mutex_init(&dev_info->bulk_mutex);
745 dev_info->udev = usb_get_dev(interface_to_usbdev(interface));
746 dev_info->interface = interface;
748 printk("DisplayLink device attached\n");
750 /* add framebuffer info to usb interface */
751 usb_set_intfdata(interface, dev_info);
753 dev_info->buf = kmalloc(BUF_SIZE, GFP_KERNEL);
754 /* usb_buffer_alloc(dev_info->udev, BUF_SIZE , GFP_KERNEL, &dev_info->tx_urb->transfer_dma); */
756 if (dev_info->buf == NULL) {
757 printk("unable to allocate memory for dlfb commands\n");
760 dev_info->bufend = dev_info->buf + BUF_SIZE;
762 dev_info->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
763 usb_fill_bulk_urb(dev_info->tx_urb, dev_info->udev,
764 usb_sndbulkpipe(dev_info->udev, 1), dev_info->buf, 0,
765 dlfb_bulk_callback, dev_info);
768 usb_control_msg(dev_info->udev, usb_rcvctrlpipe(dev_info->udev, 0),
769 (0x06), (0x80 | (0x02 << 5)), 0, 0, rbuf, 4, 0);
770 printk("ret control msg 0: %d %x%x%x%x\n", ret, rbuf[0], rbuf[1],
775 info = framebuffer_alloc(sizeof(u32) * 256, &dev_info->udev->dev);
778 printk("non posso allocare il framebuffer displaylink");
782 fb_parse_edid(dev_info->edid, &info->var);
784 printk("EDID XRES %d YRES %d\n", info->var.xres, info->var.yres);
786 if (dlfb_set_video_mode(dev_info, info->var.xres, info->var.yres) != 0) {
787 info->var.xres = 1280;
788 info->var.yres = 1024;
789 if (dlfb_set_video_mode
790 (dev_info, info->var.xres, info->var.yres) != 0) {
795 printk("found valid mode...%d\n", info->var.pixclock);
797 info->pseudo_palette = info->par;
798 info->par = dev_info;
800 dev_info->info = info;
803 FBINFO_DEFAULT | FBINFO_READS_FAST | FBINFO_HWACCEL_IMAGEBLIT |
804 FBINFO_HWACCEL_COPYAREA | FBINFO_HWACCEL_FILLRECT;
805 info->fbops = &dlfb_ops;
806 info->screen_base = rvmalloc(dev_info->screen_size);
808 if (info->screen_base == NULL) {
810 ("cannot allocate framebuffer virtual memory of %d bytes\n",
811 dev_info->screen_size);
815 printk("screen base allocated !!!\n");
817 dev_info->backing_buffer = kzalloc(dev_info->screen_size, GFP_KERNEL);
819 if (!dev_info->backing_buffer)
820 printk("non posso allocare il backing buffer\n");
822 /* info->var = dev_info->si; */
824 info->var.bits_per_pixel = 16;
825 info->var.activate = FB_ACTIVATE_TEST;
826 info->var.vmode = FB_VMODE_NONINTERLACED;
828 info->var.red.offset = 11;
829 info->var.red.length = 5;
830 info->var.red.msb_right = 0;
832 info->var.green.offset = 5;
833 info->var.green.length = 6;
834 info->var.green.msb_right = 0;
836 info->var.blue.offset = 0;
837 info->var.blue.length = 5;
838 info->var.blue.msb_right = 0;
840 /* info->var.pixclock = (10000000 / FB_W * 1000 / FB_H)/2 ; */
842 info->fix.smem_start = (unsigned long)info->screen_base;
843 info->fix.smem_len = PAGE_ALIGN(dev_info->screen_size);
844 if (strlen(dev_info->udev->product) > 15) {
845 memcpy(info->fix.id, dev_info->udev->product, 15);
847 memcpy(info->fix.id, dev_info->udev->product,
848 strlen(dev_info->udev->product));
850 info->fix.type = FB_TYPE_PACKED_PIXELS;
851 info->fix.visual = FB_VISUAL_TRUECOLOR;
852 info->fix.accel = info->flags;
853 info->fix.line_length = dev_info->line_length;
855 if (fb_alloc_cmap(&info->cmap, 256, 0) < 0)
858 printk("colormap allocated\n");
859 if (register_framebuffer(info) < 0)
862 draw_rect(dev_info, 0, 0, dev_info->info->var.xres,
863 dev_info->info->var.yres, 0x30, 0xff, 0x30);
868 fb_dealloc_cmap(&info->cmap);
870 rvfree(info->screen_base, dev_info->screen_size);
872 framebuffer_release(info);
874 usb_set_intfdata(interface, NULL);
875 usb_put_dev(dev_info->udev);
881 static void dlfb_disconnect(struct usb_interface *interface)
883 struct dlfb_data *dev_info = usb_get_intfdata(interface);
885 mutex_unlock(&dev_info->bulk_mutex);
887 usb_kill_urb(dev_info->tx_urb);
888 usb_free_urb(dev_info->tx_urb);
889 usb_set_intfdata(interface, NULL);
890 usb_put_dev(dev_info->udev);
892 if (dev_info->info) {
893 unregister_framebuffer(dev_info->info);
894 fb_dealloc_cmap(&dev_info->info->cmap);
895 rvfree(dev_info->info->screen_base, dev_info->screen_size);
896 kfree(dev_info->backing_buffer);
897 framebuffer_release(dev_info->info);
903 printk("DisplayLink device disconnected\n");
906 static struct usb_driver dlfb_driver = {
909 .disconnect = dlfb_disconnect,
910 .id_table = id_table,
913 static int __init dlfb_init(void)
919 res = usb_register(&dlfb_driver);
921 err("usb_register failed. Error number %d", res);
923 printk("VMODES initialized\n");
928 static void __exit dlfb_exit(void)
930 usb_deregister(&dlfb_driver);
933 module_init(dlfb_init);
934 module_exit(dlfb_exit);
936 MODULE_AUTHOR("Roberto De Ioris <roberto@unbit.it>");
937 MODULE_DESCRIPTION(DRIVER_VERSION);
938 MODULE_LICENSE("GPL");