diff options
Diffstat (limited to 'drivers/staging/udlfb')
-rw-r--r-- | drivers/staging/udlfb/Kconfig | 14 | ||||
-rw-r--r-- | drivers/staging/udlfb/udlfb.c | 1993 | ||||
-rw-r--r-- | drivers/staging/udlfb/udlfb.h | 281 |
3 files changed, 1432 insertions, 856 deletions
diff --git a/drivers/staging/udlfb/Kconfig b/drivers/staging/udlfb/Kconfig index 641692d..65bd5db 100644 --- a/drivers/staging/udlfb/Kconfig +++ b/drivers/staging/udlfb/Kconfig @@ -1,8 +1,14 @@ config FB_UDL tristate "Displaylink USB Framebuffer support" depends on FB && USB + select FB_MODE_HELPERS + select FB_SYS_FILLRECT + select FB_SYS_COPYAREA + select FB_SYS_IMAGEBLIT + select FB_SYS_FOPS + select FB_DEFERRED_IO ---help--- - This is an experimental driver for DisplayLink USB devices - that provides a framebuffer device. A normal framebuffer can - be used with this driver, or xorg can be run on the device - using it. + This is a kernel framebuffer driver for DisplayLink USB devices. + Supports fbdev clients like xf86-video-fbdev, kdrive, fbi, and + mplayer -vo fbdev. Supports all USB 2.0 era DisplayLink devices. + To compile as a module, choose M here: the module name is udlfb. diff --git a/drivers/staging/udlfb/udlfb.c b/drivers/staging/udlfb/udlfb.c index f5416af..8f6223c 100644 --- a/drivers/staging/udlfb/udlfb.c +++ b/drivers/staging/udlfb/udlfb.c @@ -1,17 +1,20 @@ -/***************************************************************************** - * DLFB Kernel Driver * - * Version 0.2 (udlfb) * - * (C) 2009 Roberto De Ioris <roberto@unbit.it> * - * * - * This file is licensed under the GPLv2. See COPYING in the package. * - * Based on the amazing work of Florian Echtler and libdlo 0.1 * - * * - * * - * 10.06.09 release 0.2.3 (edid ioctl, fallback for unsupported modes) * - * 05.06.09 release 0.2.2 (real screen blanking, rle compression, double buffer) * - * 31.05.09 release 0.2 * - * 22.05.09 First public (ugly) release * - *****************************************************************************/ +/* + * udlfb.c -- Framebuffer driver for DisplayLink USB controller + * + * Copyright (C) 2009 Roberto De Ioris <roberto@unbit.it> + * Copyright (C) 2009 Jaya Kumar <jayakumar.lkml@gmail.com> + * Copyright (C) 2009 Bernie Thompson <bernie@plugable.com> + * + * This file is subject to the terms and conditions of the GNU General Public + * License v2. See the file COPYING in the main directory of this archive for + * more details. + * + * Layout is based on skeletonfb by James Simmons and Geert Uytterhoeven, + * usb-skeleton by GregKH. + * + * Device-specific portions based on information from Displaylink, with work + * from Florian Echtler, Henrik Bjerregaard Pedersen, and others. + */ #include <linux/module.h> #include <linux/kernel.h> @@ -20,606 +23,672 @@ #include <linux/uaccess.h> #include <linux/mm.h> #include <linux/fb.h> -#include <linux/mutex.h> #include <linux/vmalloc.h> #include "udlfb.h" -#define DRIVER_VERSION "DLFB 0.2" +static struct fb_fix_screeninfo dlfb_fix = { + .id = "udlfb", + .type = FB_TYPE_PACKED_PIXELS, + .visual = FB_VISUAL_TRUECOLOR, + .xpanstep = 0, + .ypanstep = 0, + .ywrapstep = 0, + .accel = FB_ACCEL_NONE, +}; -/* memory functions taken from vfb */ +static const u32 udlfb_info_flags = FBINFO_DEFAULT | FBINFO_READS_FAST | +#ifdef FBINFO_VIRTFB + FBINFO_VIRTFB | +#endif + FBINFO_HWACCEL_IMAGEBLIT | FBINFO_HWACCEL_FILLRECT | + FBINFO_HWACCEL_COPYAREA | FBINFO_MISC_ALWAYS_SETPAR; -static void *rvmalloc(unsigned long size) -{ - void *mem; - unsigned long adr; +/* + * There are many DisplayLink-based products, all with unique PIDs. We are able + * to support all volume ones (circa 2009) with a single driver, so we match + * globally on VID. TODO: Probe() needs to detect when we might be running + * "future" chips, and bail on those, so a compatible driver can match. + */ +static struct usb_device_id id_table[] = { + {.idVendor = 0x17e9, .match_flags = USB_DEVICE_ID_MATCH_VENDOR,}, + {}, +}; +MODULE_DEVICE_TABLE(usb, id_table); - size = PAGE_ALIGN(size); - mem = vmalloc_32(size); - if (!mem) - return NULL; +#ifndef CONFIG_FB_DEFERRED_IO +#warning message "kernel FB_DEFFERRED_IO option to support generic fbdev apps" +#endif - memset(mem, 0, size); /* Clear the ram out, no junk to the user */ - adr = (unsigned long)mem; - while (size > 0) { - SetPageReserved(vmalloc_to_page((void *)adr)); - adr += PAGE_SIZE; - size -= PAGE_SIZE; - } +#ifndef CONFIG_FB_SYS_IMAGEBLIT +#ifndef CONFIG_FB_SYS_IMAGEBLIT_MODULE +#warning message "FB_SYS_* in kernel or module option to support fb console" +#endif +#endif - return mem; -} +#ifndef CONFIG_FB_MODE_HELPERS +#warning message "kernel FB_MODE_HELPERS required. Expect build break" +#endif -static void rvfree(void *mem, unsigned long size) -{ - unsigned long adr; +/* dlfb keeps a list of urbs for efficient bulk transfers */ +static void dlfb_urb_completion(struct urb *urb); +static struct urb *dlfb_get_urb(struct dlfb_data *dev); +static int dlfb_submit_urb(struct dlfb_data *dev, struct urb * urb, size_t len); +static int dlfb_alloc_urb_list(struct dlfb_data *dev, int count, size_t size); +static void dlfb_free_urb_list(struct dlfb_data *dev); - if (!mem) - return; +/* other symbols with dependents */ +#ifdef CONFIG_FB_DEFERRED_IO +static struct fb_deferred_io dlfb_defio; +#endif - adr = (unsigned long)mem; - while ((long)size > 0) { - ClearPageReserved(vmalloc_to_page((void *)adr)); - adr += PAGE_SIZE; - size -= PAGE_SIZE; - } - vfree(mem); +/* + * All DisplayLink bulk operations start with 0xAF, followed by specific code + * All operations are written to buffers which then later get sent to device + */ +static char *dlfb_set_register(char *buf, u8 reg, u8 val) +{ + *buf++ = 0xAF; + *buf++ = 0x20; + *buf++ = reg; + *buf++ = val; + return buf; } -static int dlfb_mmap(struct fb_info *info, struct vm_area_struct *vma) +static char *dlfb_vidreg_lock(char *buf) { - unsigned long start = vma->vm_start; - unsigned long size = vma->vm_end - vma->vm_start; - unsigned long offset = vma->vm_pgoff << PAGE_SHIFT; - unsigned long page, pos; - - printk("MMAP: %lu %u\n", offset + size, info->fix.smem_len); - - if (offset + size > info->fix.smem_len) - return -EINVAL; - - pos = (unsigned long)info->fix.smem_start + offset; - - while (size > 0) { - page = vmalloc_to_pfn((void *)pos); - if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED)) - return -EAGAIN; - - start += PAGE_SIZE; - pos += PAGE_SIZE; - if (size > PAGE_SIZE) - size -= PAGE_SIZE; - else - size = 0; - } - - vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */ - return 0; - + return dlfb_set_register(buf, 0xFF, 0x00); } -/* ioctl structure */ -struct dloarea { - int x, y; - int w, h; - int x2, y2; -}; +static char *dlfb_vidreg_unlock(char *buf) +{ + return dlfb_set_register(buf, 0xFF, 0xFF); +} /* -static struct usb_device_id id_table [] = { - { USB_DEVICE(0x17e9, 0x023d) }, - { } -}; -*/ + * On/Off for driving the DisplayLink framebuffer to the display + */ +static char *dlfb_enable_hvsync(char *buf, bool enable) +{ + if (enable) + return dlfb_set_register(buf, 0x1F, 0x00); + else + return dlfb_set_register(buf, 0x1F, 0x01); +} -static struct usb_device_id id_table[] = { - {.idVendor = 0x17e9, .match_flags = USB_DEVICE_ID_MATCH_VENDOR,}, - {}, -}; -MODULE_DEVICE_TABLE(usb, id_table); +static char *dlfb_set_color_depth(char *buf, u8 selection) +{ + return dlfb_set_register(buf, 0x00, selection); +} -static struct usb_driver dlfb_driver; +static char *dlfb_set_base16bpp(char *wrptr, u32 base) +{ + /* the base pointer is 16 bits wide, 0x20 is hi byte. */ + wrptr = dlfb_set_register(wrptr, 0x20, base >> 16); + wrptr = dlfb_set_register(wrptr, 0x21, base >> 8); + return dlfb_set_register(wrptr, 0x22, base); +} -// thanks to Henrik Bjerregaard Pedersen for this function -static char *rle_compress16(uint16_t * src, char *dst, int rem) +/* + * DisplayLink HW has separate 16bpp and 8bpp framebuffers. + * In 24bpp modes, the low 323 RGB bits go in the 8bpp framebuffer + */ +static char *dlfb_set_base8bpp(char *wrptr, u32 base) { + wrptr = dlfb_set_register(wrptr, 0x26, base >> 16); + wrptr = dlfb_set_register(wrptr, 0x27, base >> 8); + return dlfb_set_register(wrptr, 0x28, base); +} - int rl; - uint16_t pix0; - char *end_if_raw = dst + 6 + 2 * rem; +static char *dlfb_set_register_16(char *wrptr, u8 reg, u16 value) +{ + wrptr = dlfb_set_register(wrptr, reg, value >> 8); + return dlfb_set_register(wrptr, reg+1, value); +} - dst += 6; // header will be filled in if RLE is worth it +/* + * This is kind of weird because the controller takes some + * register values in a different byte order than other registers. + */ +static char *dlfb_set_register_16be(char *wrptr, u8 reg, u16 value) +{ + wrptr = dlfb_set_register(wrptr, reg, value); + return dlfb_set_register(wrptr, reg+1, value >> 8); +} - while (rem && dst < end_if_raw) { - char *start = (char *)src; +/* + * LFSR is linear feedback shift register. The reason we have this is + * because the display controller needs to minimize the clock depth of + * various counters used in the display path. So this code reverses the + * provided value into the lfsr16 value by counting backwards to get + * the value that needs to be set in the hardware comparator to get the + * same actual count. This makes sense once you read above a couple of + * times and think about it from a hardware perspective. + */ +static u16 dlfb_lfsr16(u16 actual_count) +{ + u32 lv = 0xFFFF; /* This is the lfsr value that the hw starts with */ - pix0 = *src++; - rl = 1; - rem--; - while (rem && *src == pix0) - rem--, rl++, src++; - *dst++ = rl; - *dst++ = start[1]; - *dst++ = start[0]; + while (actual_count--) { + lv = ((lv << 1) | + (((lv >> 15) ^ (lv >> 4) ^ (lv >> 2) ^ (lv >> 1)) & 1)) + & 0xFFFF; } - return dst; + return (u16) lv; } /* -Thanks to Henrik Bjerregaard Pedersen for rle implementation and code refactoring. -Next step is huffman compression. -*/ - -static int -image_blit(struct dlfb_data *dev_info, int x, int y, int width, int height, - char *data) + * This does LFSR conversion on the value that is to be written. + * See LFSR explanation above for more detail. + */ +static char *dlfb_set_register_lfsr16(char *wrptr, u8 reg, u16 value) { + return dlfb_set_register_16(wrptr, reg, dlfb_lfsr16(value)); +} - int i, j, base; - int rem = width; - int ret; - - int firstdiff, thistime; - - char *bufptr; - - if (x + width > dev_info->info->var.xres) - return -EINVAL; - - if (y + height > dev_info->info->var.yres) - return -EINVAL; +/* + * This takes a standard fbdev screeninfo struct and all of its monitor mode + * details and converts them into the DisplayLink equivalent register commands. + */ +static char *dlfb_set_vid_cmds(char *wrptr, struct fb_var_screeninfo *var) +{ + u16 xds, yds; + u16 xde, yde; + u16 yec; - mutex_lock(&dev_info->bulk_mutex); + /* x display start */ + xds = var->left_margin + var->hsync_len; + wrptr = dlfb_set_register_lfsr16(wrptr, 0x01, xds); + /* x display end */ + xde = xds + var->xres; + wrptr = dlfb_set_register_lfsr16(wrptr, 0x03, xde); - base = - dev_info->base16 + ((dev_info->info->var.xres * 2 * y) + (x * 2)); + /* y display start */ + yds = var->upper_margin + var->vsync_len; + wrptr = dlfb_set_register_lfsr16(wrptr, 0x05, yds); + /* y display end */ + yde = yds + var->yres; + wrptr = dlfb_set_register_lfsr16(wrptr, 0x07, yde); - data += (dev_info->info->var.xres * 2 * y) + (x * 2); + /* x end count is active + blanking - 1 */ + wrptr = dlfb_set_register_lfsr16(wrptr, 0x09, + xde + var->right_margin - 1); - /* printk("IMAGE_BLIT\n"); */ + /* libdlo hardcodes hsync start to 1 */ + wrptr = dlfb_set_register_lfsr16(wrptr, 0x0B, 1); - bufptr = dev_info->buf; + /* hsync end is width of sync pulse + 1 */ + wrptr = dlfb_set_register_lfsr16(wrptr, 0x0D, var->hsync_len + 1); - for (i = y; i < y + height; i++) { + /* hpixels is active pixels */ + wrptr = dlfb_set_register_16(wrptr, 0x0F, var->xres); - if (dev_info->bufend - bufptr < BUF_HIGH_WATER_MARK) { - ret = dlfb_bulk_msg(dev_info, bufptr - dev_info->buf); - bufptr = dev_info->buf; - } + /* yendcount is vertical active + vertical blanking */ + yec = var->yres + var->upper_margin + var->lower_margin + + var->vsync_len; + wrptr = dlfb_set_register_lfsr16(wrptr, 0x11, yec); - rem = width; + /* libdlo hardcodes vsync start to 0 */ + wrptr = dlfb_set_register_lfsr16(wrptr, 0x13, 0); - /* printk("WRITING LINE %d\n", i); */ + /* vsync end is width of vsync pulse */ + wrptr = dlfb_set_register_lfsr16(wrptr, 0x15, var->vsync_len); - while (rem) { + /* vpixels is active pixels */ + wrptr = dlfb_set_register_16(wrptr, 0x17, var->yres); - if (dev_info->bufend - bufptr < BUF_HIGH_WATER_MARK) { - ret = - dlfb_bulk_msg(dev_info, - bufptr - dev_info->buf); - bufptr = dev_info->buf; - } - // number of pixels to consider this time - thistime = rem; - if (thistime > 255) - thistime = 255; - - // find position of first pixel that has changed - firstdiff = -1; - for (j = 0; j < thistime * 2; j++) { - if (dev_info->backing_buffer - [base - dev_info->base16 + j] != data[j]) { - firstdiff = j / 2; - break; - } - } + /* convert picoseconds to 5kHz multiple for pclk5k = x * 1E12/5k */ + wrptr = dlfb_set_register_16be(wrptr, 0x1B, + 200*1000*1000/var->pixclock); - if (firstdiff >= 0) { - char *end_of_rle; - - end_of_rle = - rle_compress16((uint16_t *) (data + - firstdiff * 2), - bufptr, - thistime - firstdiff); - - if (end_of_rle < - bufptr + 6 + 2 * (thistime - firstdiff)) { - bufptr[0] = 0xAF; - bufptr[1] = 0x69; - - bufptr[2] = - (char)((base + - firstdiff * 2) >> 16); - bufptr[3] = - (char)((base + firstdiff * 2) >> 8); - bufptr[4] = - (char)(base + firstdiff * 2); - bufptr[5] = thistime - firstdiff; - - bufptr = end_of_rle; - - } else { - // fallback to raw (or some other encoding?) - *bufptr++ = 0xAF; - *bufptr++ = 0x68; - - *bufptr++ = - (char)((base + - firstdiff * 2) >> 16); - *bufptr++ = - (char)((base + firstdiff * 2) >> 8); - *bufptr++ = - (char)(base + firstdiff * 2); - *bufptr++ = thistime - firstdiff; - // PUT COMPRESSION HERE - for (j = firstdiff * 2; - j < thistime * 2; j += 2) { - *bufptr++ = data[j + 1]; - *bufptr++ = data[j]; - } - } - } + return wrptr; +} - base += thistime * 2; - data += thistime * 2; - rem -= thistime; - } +/* + * This takes a standard fbdev screeninfo struct that was fetched or prepared + * and then generates the appropriate command sequence that then drives the + * display controller. + */ +static int dlfb_set_video_mode(struct dlfb_data *dev, + struct fb_var_screeninfo *var) +{ + char *buf; + char *wrptr; + int retval = 0; + int writesize; + struct urb *urb; - memcpy(dev_info->backing_buffer + (base - dev_info->base16) - - (width * 2), data - (width * 2), width * 2); + if (!atomic_read(&dev->usb_active)) + return -EPERM; - base += (dev_info->info->var.xres * 2) - (width * 2); - data += (dev_info->info->var.xres * 2) - (width * 2); + urb = dlfb_get_urb(dev); + if (!urb) + return -ENOMEM; + buf = (char *) urb->transfer_buffer; - } + /* + * This first section has to do with setting the base address on the + * controller * associated with the display. There are 2 base + * pointers, currently, we only * use the 16 bpp segment. + */ + wrptr = dlfb_vidreg_lock(buf); + wrptr = dlfb_set_color_depth(wrptr, 0x00); + /* set base for 16bpp segment to 0 */ + wrptr = dlfb_set_base16bpp(wrptr, 0); + /* set base for 8bpp segment to end of fb */ + wrptr = dlfb_set_base8bpp(wrptr, dev->info->fix.smem_len); - if (bufptr > dev_info->buf) { - ret = dlfb_bulk_msg(dev_info, bufptr - dev_info->buf); - } + wrptr = dlfb_set_vid_cmds(wrptr, var); + wrptr = dlfb_enable_hvsync(wrptr, true); + wrptr = dlfb_vidreg_unlock(wrptr); - mutex_unlock(&dev_info->bulk_mutex); + writesize = wrptr - buf; - return base; + retval = dlfb_submit_urb(dev, urb, writesize); + return retval; } -static int -draw_rect(struct dlfb_data *dev_info, int x, int y, int width, int height, - unsigned char red, unsigned char green, unsigned char blue) +static int dlfb_ops_mmap(struct fb_info *info, struct vm_area_struct *vma) { + unsigned long start = vma->vm_start; + unsigned long size = vma->vm_end - vma->vm_start; + unsigned long offset = vma->vm_pgoff << PAGE_SHIFT; + unsigned long page, pos; + struct dlfb_data *dev = info->par; - int i, j, base; - int ret; - unsigned short col = - (((((red) & 0xF8) | ((green) >> 5)) & 0xFF) << 8) + - (((((green) & 0x1C) << 3) | ((blue) >> 3)) & 0xFF); - int rem = width; - - char *bufptr; + dl_notice("MMAP: %lu %u\n", offset + size, info->fix.smem_len); - if (x + width > dev_info->info->var.xres) + if (offset + size > info->fix.smem_len) return -EINVAL; - if (y + height > dev_info->info->var.yres) - return -EINVAL; + pos = (unsigned long)info->fix.smem_start + offset; - mutex_lock(&dev_info->bulk_mutex); + while (size > 0) { + page = vmalloc_to_pfn((void *)pos); + if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED)) + return -EAGAIN; - base = dev_info->base16 + (dev_info->info->var.xres * 2 * y) + (x * 2); + start += PAGE_SIZE; + pos += PAGE_SIZE; + if (size > PAGE_SIZE) + size -= PAGE_SIZE; + else + size = 0; + } - bufptr = dev_info->buf; + vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */ + return 0; - for (i = y; i < y + height; i++) { +} - for (j = 0; j < width * 2; j += 2) { - dev_info->backing_buffer[base - dev_info->base16 + j] = - (char)(col >> 8); - dev_info->backing_buffer[base - dev_info->base16 + j + - 1] = (char)(col); - } - if (dev_info->bufend - bufptr < BUF_HIGH_WATER_MARK) { - ret = dlfb_bulk_msg(dev_info, bufptr - dev_info->buf); - bufptr = dev_info->buf; +/* + * Trims identical data from front and back of line + * Sets new front buffer address and width + * And returns byte count of identical pixels + * Assumes CPU natural alignment (unsigned long) + * for back and front buffer ptrs and width + */ +static int dlfb_trim_hline(const u8 *bback, const u8 **bfront, int *width_bytes) +{ + int j, k; + const unsigned long *back = (const unsigned long *) bback; + const unsigned long *front = (const unsigned long *) *bfront; + const int width = *width_bytes / sizeof(unsigned long); + int identical = width; + int start = width; + int end = width; + + prefetch((void *) front); + prefetch((void *) back); + + for (j = 0; j < width; j++) { + if (back[j] != front[j]) { + start = j; + break; } + } - rem = width; - - while (rem) { - - if (dev_info->bufend - bufptr < BUF_HIGH_WATER_MARK) { - ret = - dlfb_bulk_msg(dev_info, - bufptr - dev_info->buf); - bufptr = dev_info->buf; - } - - *bufptr++ = 0xAF; - *bufptr++ = 0x69; - - *bufptr++ = (char)(base >> 16); - *bufptr++ = (char)(base >> 8); - *bufptr++ = (char)(base); - - if (rem > 255) { - *bufptr++ = 255; - *bufptr++ = 255; - rem -= 255; - base += 255 * 2; - } else { - *bufptr++ = rem; - *bufptr++ = rem; - base += rem * 2; - rem = 0; - } - - *bufptr++ = (char)(col >> 8); - *bufptr++ = (char)(col); - + for (k = width - 1; k > j; k--) { + if (back[k] != front[k]) { + end = k+1; + break; } - - base += (dev_info->info->var.xres * 2) - (width * 2); - } - if (bufptr > dev_info->buf) - ret = dlfb_bulk_msg(dev_info, bufptr - dev_info->buf); + identical = start + (width - end); + *bfront = (u8 *) &front[start]; + *width_bytes = (end - start) * sizeof(unsigned long); - mutex_unlock(&dev_info->bulk_mutex); - - return 1; + return identical * sizeof(unsigned long); } -static void swapfb(struct dlfb_data *dev_info) +/* +Render a command stream for an encoded horizontal line segment of pixels. + +A command buffer holds several commands. +It always begins with a fresh command header +(the protocol doesn't require this, but we enforce it to allow +multiple buffers to be potentially encoded and sent in parallel). +A single command encodes one contiguous horizontal line of pixels + +The function relies on the client to do all allocation, so that +rendering can be done directly to output buffers (e.g. USB URBs). +The function fills the supplied command buffer, providing information +on where it left off, so the client may call in again with additional +buffers if the line will take several buffers to complete. + +A single command can transmit a maximum of 256 pixels, +regardless of the compression ratio (protocol design limit). +To the hardware, 0 for a size byte means 256 + +Rather than 256 pixel commands which are either rl or raw encoded, +the rlx command simply assumes alternating raw and rl spans within one cmd. +This has a slightly larger header overhead, but produces more even results. +It also processes all data (read and write) in a single pass. +Performance benchmarks of common cases show it having just slightly better +compression than 256 pixel raw -or- rle commands, with similar CPU consumpion. +But for very rl friendly data, will compress not quite as well. +*/ +static void dlfb_compress_hline( + const uint16_t **pixel_start_ptr, + const uint16_t *const pixel_end, + uint32_t *device_address_ptr, + uint8_t **command_buffer_ptr, + const uint8_t *const cmd_buffer_end) { + const uint16_t *pixel = *pixel_start_ptr; + uint32_t dev_addr = *device_address_ptr; + uint8_t *cmd = *command_buffer_ptr; + const int bpp = 2; + + while ((pixel_end > pixel) && + (cmd_buffer_end - MIN_RLX_CMD_BYTES > cmd)) { + uint8_t *raw_pixels_count_byte = 0; + uint8_t *cmd_pixels_count_byte = 0; + const uint16_t *raw_pixel_start = 0; + const uint16_t *cmd_pixel_start, *cmd_pixel_end = 0; + const uint32_t be_dev_addr = cpu_to_be32(dev_addr); + + prefetchw((void *) cmd); /* pull in one cache line at least */ + + *cmd++ = 0xAF; + *cmd++ = 0x6B; + *cmd++ = (uint8_t) ((be_dev_addr >> 8) & 0xFF); + *cmd++ = (uint8_t) ((be_dev_addr >> 16) & 0xFF); + *cmd++ = (uint8_t) ((be_dev_addr >> 24) & 0xFF); + + cmd_pixels_count_byte = cmd++; /* we'll know this later */ + cmd_pixel_start = pixel; + + raw_pixels_count_byte = cmd++; /* we'll know this later */ + raw_pixel_start = pixel; + + cmd_pixel_end = pixel + min(MAX_CMD_PIXELS + 1, + min((int)(pixel_end - pixel), + (int)(cmd_buffer_end - cmd) / bpp)); + + prefetch_range((void *) pixel, (cmd_pixel_end - pixel) * bpp); + + while (pixel < cmd_pixel_end) { + const uint16_t * const repeating_pixel = pixel; + + *(uint16_t *)cmd = cpu_to_be16p(pixel); + cmd += 2; + pixel++; + + if (unlikely((pixel < cmd_pixel_end) && + (*pixel == *repeating_pixel))) { + /* go back and fill in raw pixel count */ + *raw_pixels_count_byte = ((repeating_pixel - + raw_pixel_start) + 1) & 0xFF; + + while ((pixel < cmd_pixel_end) + && (*pixel == *repeating_pixel)) { + pixel++; + } - int tmpbase; - char *bufptr; - - mutex_lock(&dev_info->bulk_mutex); - - tmpbase = dev_info->base16; - - dev_info->base16 = dev_info->base16d; - dev_info->base16d = tmpbase; + /* immediately after raw data is repeat byte */ + *cmd++ = ((pixel - repeating_pixel) - 1) & 0xFF; - bufptr = dev_info->buf; + /* Then start another raw pixel span */ + raw_pixel_start = pixel; + raw_pixels_count_byte = cmd++; + } + } - bufptr = dlfb_set_register(bufptr, 0xFF, 0x00); + if (pixel > raw_pixel_start) { + /* finalize last RAW span */ + *raw_pixels_count_byte = (pixel-raw_pixel_start) & 0xFF; + } - // set addresses - bufptr = - dlfb_set_register(bufptr, 0x20, (char)(dev_info->base16 >> 16)); - bufptr = dlfb_set_register(bufptr, 0x21, (char)(dev_info->base16 >> 8)); - bufptr = dlfb_set_register(bufptr, 0x22, (char)(dev_info->base16)); + *cmd_pixels_count_byte = (pixel - cmd_pixel_start) & 0xFF; + dev_addr += (pixel - cmd_pixel_start) * bpp; + } - bufptr = dlfb_set_register(bufptr, 0xFF, 0x00); + if (cmd_buffer_end <= MIN_RLX_CMD_BYTES + cmd) { + /* Fill leftover bytes with no-ops */ + if (cmd_buffer_end > cmd) + memset(cmd, 0xAF, cmd_buffer_end - cmd); + cmd = (uint8_t *) cmd_buffer_end; + } - dlfb_bulk_msg(dev_info, bufptr - dev_info->buf); + *command_buffer_ptr = cmd; + *pixel_start_ptr = pixel; + *device_address_ptr = dev_addr; - mutex_unlock(&dev_info->bulk_mutex); + return; } -static int copyfb(struct dlfb_data *dev_info) +/* + * There are 3 copies of every pixel: The front buffer that the fbdev + * client renders to, the actual framebuffer across the USB bus in hardware + * (that we can only write to, slowly, and can never read), and (optionally) + * our shadow copy that tracks what's been sent to that hardware buffer. + */ +static void dlfb_render_hline(struct dlfb_data *dev, struct urb **urb_ptr, + const char *front, char **urb_buf_ptr, + u32 byte_offset, u32 byte_width, + int *ident_ptr, int *sent_ptr) { - int base; - int source; - int rem; - int i, ret; - - char *bufptr; - - base = dev_info->base16d; - - mutex_lock(&dev_info->bulk_mutex); - - source = dev_info->base16; - - bufptr = dev_info->buf; - - for (i = 0; i < dev_info->info->var.yres; i++) { - - if (dev_info->bufend - bufptr < BUF_HIGH_WATER_MARK) { - ret = dlfb_bulk_msg(dev_info, bufptr - dev_info->buf); - bufptr = dev_info->buf; - } - - rem = dev_info->info->var.xres; - - while (rem) { - - if (dev_info->bufend - bufptr < BUF_HIGH_WATER_MARK) { - ret = - dlfb_bulk_msg(dev_info, - bufptr - dev_info->buf); - bufptr = dev_info->buf; - - } - - *bufptr++ = 0xAF; - *bufptr++ = 0x6A; - - *bufptr++ = (char)(base >> 16); - *bufptr++ = (char)(base >> 8); - *bufptr++ = (char)(base); - - if (rem > 255) { - *bufptr++ = 255; - *bufptr++ = (char)(source >> 16); - *bufptr++ = (char)(source >> 8); - *bufptr++ = (char)(source); - - rem -= 255; - base += 255 * 2; - source += 255 * 2; - - } else { - *bufptr++ = rem; - *bufptr++ = (char)(source >> 16); - *bufptr++ = (char)(source >> 8); - *bufptr++ = (char)(source); + const u8 *line_start, *line_end, *next_pixel; + u32 dev_addr = dev->base16 + byte_offset; + struct urb *urb = *urb_ptr; + u8 *cmd = *urb_buf_ptr; + u8 *cmd_end = (u8 *) urb->transfer_buffer + urb->transfer_buffer_length; + + line_start = (u8 *) (front + byte_offset); + next_pixel = line_start; + line_end = next_pixel + byte_width; + + if (dev->backing_buffer) { + int offset; + const u8 *back_start = (u8 *) (dev->backing_buffer + + byte_offset); + + *ident_ptr += dlfb_trim_hline(back_start, &next_pixel, + &byte_width); + + offset = next_pixel - line_start; + line_end = next_pixel + byte_width; + dev_addr += offset; + back_start += offset; + line_start += offset; + + memcpy((char *)back_start, (char *) line_start, + byte_width); + } - base += rem * 2; - source += rem * 2; - rem = 0; - } + while (next_pixel < line_end) { + + dlfb_compress_hline((const uint16_t **) &next_pixel, + (const uint16_t *) line_end, &dev_addr, + (u8 **) &cmd, (u8 *) cmd_end); + + if (cmd >= cmd_end) { + int len = cmd - (u8 *) urb->transfer_buffer; + if (dlfb_submit_urb(dev, urb, len)) + return; /* lost pixels is set */ + *sent_ptr += len; + urb = dlfb_get_urb(dev); + if (!urb) + return; /* lost_pixels is set */ + *urb_ptr = urb; + cmd = urb->transfer_buffer; + cmd_end = &cmd[urb->transfer_buffer_length]; } } - if (bufptr > dev_info->buf) - ret = dlfb_bulk_msg(dev_info, bufptr - dev_info->buf); - - mutex_unlock(&dev_info->bulk_mutex); - - return 1; - + *urb_buf_ptr = cmd; } -static int -copyarea(struct dlfb_data *dev_info, int dx, int dy, int sx, int sy, - int width, int height) +int dlfb_handle_damage(struct dlfb_data *dev, int x, int y, + int width, int height, char *data) { - int base; - int source; - int rem; int i, ret; - - char *bufptr; - - if (dx + width > dev_info->info->var.xres) + char *cmd; + cycles_t start_cycles, end_cycles; + int bytes_sent = 0; + int bytes_identical = 0; + struct urb *urb; + int aligned_x; + + start_cycles = get_cycles(); + + aligned_x = DL_ALIGN_DOWN(x, sizeof(unsigned long)); + width = DL_ALIGN_UP(width + (x-aligned_x), sizeof(unsigned long)); + x = aligned_x; + + if ((width <= 0) || + (x + width > dev->info->var.xres) || + (y + height > dev->info->var.yres)) return -EINVAL; - if (dy + height > dev_info->info->var.yres) - return -EINVAL; - - mutex_lock(&dev_info->bulk_mutex); + if (!atomic_read(&dev->usb_active)) + return 0; - base = - dev_info->base16 + (dev_info->info->var.xres * 2 * dy) + (dx * 2); - source = (dev_info->info->var.xres * 2 * sy) + (sx * 2); + urb = dlfb_get_urb(dev); + if (!urb) + return 0; + cmd = urb->transfer_buffer; - bufptr = dev_info->buf; + for (i = y; i < y + height ; i++) { + const int line_offset = dev->info->fix.line_length * i; + const int byte_offset = line_offset + (x * BPP); - for (i = sy; i < sy + height; i++) { + dlfb_render_hline(dev, &urb, (char *) dev->info->fix.smem_start, + &cmd, byte_offset, width * BPP, + &bytes_identical, &bytes_sent); + } - memcpy(dev_info->backing_buffer + base - dev_info->base16, - dev_info->backing_buffer + source, width * 2); + if (cmd > (char *) urb->transfer_buffer) { + /* Send partial buffer remaining before exiting */ + int len = cmd - (char *) urb->transfer_buffer; + ret = dlfb_submit_urb(dev, urb, len); + bytes_sent += len; + } else + dlfb_urb_completion(urb); + + atomic_add(bytes_sent, &dev->bytes_sent); + atomic_add(bytes_identical, &dev->bytes_identical); + atomic_add(width*height*2, &dev->bytes_rendered); + end_cycles = get_cycles(); + atomic_add(((unsigned int) ((end_cycles - start_cycles) + >> 10)), /* Kcycles */ + &dev->cpu_kcycles_used); - if (dev_info->bufend - bufptr < BUF_HIGH_WATER_MARK) { - ret = dlfb_bulk_msg(dev_info, bufptr - dev_info->buf); - bufptr = dev_info->buf; - } - - rem = width; + return 0; +} - while (rem) { +/* hardware has native COPY command (see libdlo), but not worth it for fbcon */ +static void dlfb_ops_copyarea(struct fb_info *info, + const struct fb_copyarea *area) +{ - if (dev_info->bufend - bufptr < BUF_HIGH_WATER_MARK) { - ret = - dlfb_bulk_msg(dev_info, - bufptr - dev_info->buf); - bufptr = dev_info->buf; - } + struct dlfb_data *dev = info->par; - *bufptr++ = 0xAF; - *bufptr++ = 0x6A; +#if defined CONFIG_FB_SYS_COPYAREA || defined CONFIG_FB_SYS_COPYAREA_MODULE - *bufptr++ = (char)(base >> 16); - *bufptr++ = (char)(base >> 8); - *bufptr++ = (char)(base); + sys_copyarea(info, area); - if (rem > 255) { - *bufptr++ = 255; - *bufptr++ = (char)(source >> 16); - *bufptr++ = (char)(source >> 8); - *bufptr++ = (char)(source); + dlfb_handle_damage(dev, area->dx, area->dy, + area->width, area->height, info->screen_base); +#endif + atomic_inc(&dev->copy_count); - rem -= 255; - base += 255 * 2; - source += 255 * 2; +} - } else { - *bufptr++ = rem; - *bufptr++ = (char)(source >> 16); - *bufptr++ = (char)(source >> 8); - *bufptr++ = (char)(source); +static void dlfb_ops_imageblit(struct fb_info *info, + const struct fb_image *image) +{ + struct dlfb_data *dev = info->par; - base += rem * 2; - source += rem * 2; - rem = 0; - } - } +#if defined CONFIG_FB_SYS_IMAGEBLIT || defined CONFIG_FB_SYS_IMAGEBLIT_MODULE - base += (dev_info->info->var.xres * 2) - (width * 2); - source += (dev_info->info->var.xres * 2) - (width * 2); - } + sys_imageblit(info, image); - if (bufptr > dev_info->buf) - ret = dlfb_bulk_msg(dev_info, bufptr - dev_info->buf); + dlfb_handle_damage(dev, image->dx, image->dy, + image->width, image->height, info->screen_base); - mutex_unlock(&dev_info->bulk_mutex); +#endif - return 1; + atomic_inc(&dev->blit_count); } -static void dlfb_copyarea(struct fb_info *info, const struct fb_copyarea *area) +static void dlfb_ops_fillrect(struct fb_info *info, + const struct fb_fillrect *rect) { - struct dlfb_data *dev = info->par; - copyarea(dev, area->dx, area->dy, area->sx, area->sy, area->width, - area->height); +#if defined CONFIG_FB_SYS_FILLRECT || defined CONFIG_FB_SYS_FILLRECT_MODULE - /* printk("COPY AREA %d %d %d %d %d %d !!!\n", area->dx, area->dy, area->sx, area->sy, area->width, area->height); */ + sys_fillrect(info, rect); -} + dlfb_handle_damage(dev, rect->dx, rect->dy, rect->width, + rect->height, info->screen_base); +#endif -static void dlfb_imageblit(struct fb_info *info, const struct fb_image *image) -{ + atomic_inc(&dev->fill_count); - int ret; - struct dlfb_data *dev = info->par; - /* printk("IMAGE BLIT (1) %d %d %d %d DEPTH %d {%p}!!!\n", image->dx, image->dy, image->width, image->height, image->depth, dev->udev); */ - cfb_imageblit(info, image); - ret = - image_blit(dev, image->dx, image->dy, image->width, image->height, - info->screen_base); - /* 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); */ } -static void dlfb_fillrect(struct fb_info *info, - const struct fb_fillrect *region) +static void dlfb_get_edid(struct dlfb_data *dev) { - - unsigned char red, green, blue; - struct dlfb_data *dev = info->par; - - memcpy(&red, ®ion->color, 1); - memcpy(&green, ®ion->color + 1, 1); - memcpy(&blue, ®ion->color + 2, 1); - draw_rect(dev, region->dx, region->dy, region->width, region->height, - red, green, blue); - /* printk("FILL RECT %d %d !!!\n", region->dx, region->dy); */ - + int i; + int ret; + char rbuf[2]; + + for (i = 0; i < sizeof(dev->edid); i++) { + ret = usb_control_msg(dev->udev, + usb_rcvctrlpipe(dev->udev, 0), (0x02), + (0x80 | (0x02 << 5)), i << 8, 0xA1, rbuf, 2, + 0); + dev->edid[i] = rbuf[1]; + } } -static int dlfb_ioctl(struct fb_info *info, unsigned int cmd, unsigned long arg) +static int dlfb_ops_ioctl(struct fb_info *info, unsigned int cmd, + unsigned long arg) { - struct dlfb_data *dev_info = info->par; + struct dlfb_data *dev = info->par; struct dloarea *area = NULL; - if (cmd == 0xAD) { + if (!atomic_read(&dev->usb_active)) + return 0; + + /* TODO: Update X server to get this from sysfs instead */ + if (cmd == DLFB_IOCTL_RETURN_EDID) { char *edid = (char *)arg; - dlfb_edid(dev_info); - if (copy_to_user(edid, dev_info->edid, 128)) { + dlfb_get_edid(dev); + if (copy_to_user(edid, dev->edid, sizeof(dev->edid))) return -EFAULT; - } return 0; } - if (cmd == 0xAA || cmd == 0xAB || cmd == 0xAC) { + /* TODO: Help propose a standard fb.h ioctl to report mmap damage */ + if (cmd == DLFB_IOCTL_REPORT_DAMAGE) { area = (struct dloarea *)arg; @@ -634,36 +703,20 @@ static int dlfb_ioctl(struct fb_info *info, unsigned int cmd, unsigned long arg) if (area->y > info->var.yres) area->y = info->var.yres; - } - if (cmd == 0xAA) { - image_blit(dev_info, area->x, area->y, area->w, area->h, + atomic_set(&dev->use_defio, 0); + + dlfb_handle_damage(dev, area->x, area->y, area->w, area->h, info->screen_base); + atomic_inc(&dev->damage_count); } - if (cmd == 0xAC) { - copyfb(dev_info); - image_blit(dev_info, area->x, area->y, area->w, area->h, - info->screen_base); - swapfb(dev_info); - } else if (cmd == 0xAB) { - - if (area->x2 < 0) - area->x2 = 0; - - if (area->y2 < 0) - area->y2 = 0; - copyarea(dev_info, - area->x2, area->y2, area->x, area->y, area->w, - area->h); - } return 0; } /* taken from vesafb */ - static int -dlfb_setcolreg(unsigned regno, unsigned red, unsigned green, +dlfb_ops_setcolreg(unsigned regno, unsigned red, unsigned green, unsigned blue, unsigned transp, struct fb_info *info) { int err = 0; @@ -688,234 +741,698 @@ dlfb_setcolreg(unsigned regno, unsigned red, unsigned green, return err; } -static int dlfb_release(struct fb_info *info, int user) +/* + * It's common for several clients to have framebuffer open simultaneously. + * e.g. both fbcon and X. Makes things interesting. + */ +static int dlfb_ops_open(struct fb_info *info, int user) +{ + struct dlfb_data *dev = info->par; + +/* if (user == 0) + * We could special case kernel mode clients (fbcon) here + */ + + mutex_lock(&dev->fb_open_lock); + + dev->fb_count++; + +#ifdef CONFIG_FB_DEFERRED_IO + if ((atomic_read(&dev->use_defio)) && (info->fbdefio == NULL)) { + /* enable defio */ + info->fbdefio = &dlfb_defio; + fb_deferred_io_init(info); + } +#endif + + dl_notice("open /dev/fb%d user=%d fb_info=%p count=%d\n", + info->node, user, info, dev->fb_count); + + mutex_unlock(&dev->fb_open_lock); + + return 0; +} + +static int dlfb_ops_release(struct fb_info *info, int user) +{ + struct dlfb_data *dev = info->par; + + mutex_lock(&dev->fb_open_lock); + + dev->fb_count--; + +#ifdef CONFIG_FB_DEFERRED_IO + if ((dev->fb_count == 0) && (info->fbdefio)) { + fb_deferred_io_cleanup(info); + info->fbdefio = NULL; + info->fbops->fb_mmap = dlfb_ops_mmap; + } +#endif + + dl_notice("release /dev/fb%d user=%d count=%d\n", + info->node, user, dev->fb_count); + + mutex_unlock(&dev->fb_open_lock); + + return 0; +} + +/* + * Called when all client interfaces to start transactions have been disabled, + * and all references to our device instance (dlfb_data) are released. + * Every transaction must have a reference, so we know are fully spun down + */ +static void dlfb_delete(struct kref *kref) +{ + struct dlfb_data *dev = container_of(kref, struct dlfb_data, kref); + + if (dev->backing_buffer) + vfree(dev->backing_buffer); + + mutex_destroy(&dev->fb_open_lock); + + kfree(dev); +} + +/* + * Called by fbdev as last part of unregister_framebuffer() process + * No new clients can open connections. Deallocate everything fb_info. + */ +static void dlfb_ops_destroy(struct fb_info *info) +{ + struct dlfb_data *dev = info->par; + + if (info->cmap.len != 0) + fb_dealloc_cmap(&info->cmap); + if (info->monspecs.modedb) + fb_destroy_modedb(info->monspecs.modedb); + if (info->screen_base) + vfree(info->screen_base); + + fb_destroy_modelist(&info->modelist); + + framebuffer_release(info); + + /* ref taken before register_framebuffer() for dlfb_data clients */ + kref_put(&dev->kref, dlfb_delete); +} + +/* + * Check whether a video mode is supported by the DisplayLink chip + * We start from monitor's modes, so don't need to filter that here + */ +static int dlfb_is_valid_mode(struct fb_videomode *mode, + struct fb_info *info) +{ + struct dlfb_data *dev = info->par; + + if (mode->xres * mode->yres > dev->sku_pixel_limit) + return 0; + + return 1; +} + +static void dlfb_var_color_format(struct fb_var_screeninfo *var) +{ + const struct fb_bitfield red = { 11, 5, 0 }; + const struct fb_bitfield green = { 5, 6, 0 }; + const struct fb_bitfield blue = { 0, 5, 0 }; + + var->bits_per_pixel = 16; + var->red = red; + var->green = green; + var->blue = blue; +} + +static int dlfb_ops_check_var(struct fb_var_screeninfo *var, + struct fb_info *info) { - struct dlfb_data *dev_info = info->par; - image_blit(dev_info, 0, 0, info->var.xres, info->var.yres, - info->screen_base); + struct fb_videomode mode; + + /* TODO: support dynamically changing framebuffer size */ + if ((var->xres * var->yres * 2) > info->fix.smem_len) + return -EINVAL; + + /* set device-specific elements of var unrelated to mode */ + dlfb_var_color_format(var); + + fb_var_to_videomode(&mode, var); + + if (!dlfb_is_valid_mode(&mode, info)) + return -EINVAL; + return 0; } -static int dlfb_blank(int blank_mode, struct fb_info *info) +static int dlfb_ops_set_par(struct fb_info *info) { - struct dlfb_data *dev_info = info->par; - char *bufptr = dev_info->buf; + struct dlfb_data *dev = info->par; + + dl_notice("set_par mode %dx%d\n", info->var.xres, info->var.yres); - bufptr = dlfb_set_register(bufptr, 0xFF, 0x00); + return dlfb_set_video_mode(dev, &info->var); +} + +static int dlfb_ops_blank(int blank_mode, struct fb_info *info) +{ + struct dlfb_data *dev = info->par; + char *bufptr; + struct urb *urb; + + urb = dlfb_get_urb(dev); + if (!urb) + return 0; + bufptr = (char *) urb->transfer_buffer; + + /* overloading usb_active. UNBLANK can conflict with teardown */ + + bufptr = dlfb_vidreg_lock(bufptr); if (blank_mode != FB_BLANK_UNBLANK) { - bufptr = dlfb_set_register(bufptr, 0x1F, 0x01); + atomic_set(&dev->usb_active, 0); + bufptr = dlfb_enable_hvsync(bufptr, false); } else { - bufptr = dlfb_set_register(bufptr, 0x1F, 0x00); + atomic_set(&dev->usb_active, 1); + bufptr = dlfb_enable_hvsync(bufptr, true); } - bufptr = dlfb_set_register(bufptr, 0xFF, 0xFF); + bufptr = dlfb_vidreg_unlock(bufptr); - dlfb_bulk_msg(dev_info, bufptr - dev_info->buf); + dlfb_submit_urb(dev, urb, bufptr - (char *) urb->transfer_buffer); return 0; } static struct fb_ops dlfb_ops = { - .fb_setcolreg = dlfb_setcolreg, - .fb_fillrect = dlfb_fillrect, - .fb_copyarea = dlfb_copyarea, - .fb_imageblit = dlfb_imageblit, - .fb_mmap = dlfb_mmap, - .fb_ioctl = dlfb_ioctl, - .fb_release = dlfb_release, - .fb_blank = dlfb_blank, + .owner = THIS_MODULE, + .fb_setcolreg = dlfb_ops_setcolreg, + .fb_fillrect = dlfb_ops_fillrect, + .fb_copyarea = dlfb_ops_copyarea, + .fb_imageblit = dlfb_ops_imageblit, + .fb_mmap = dlfb_ops_mmap, + .fb_ioctl = dlfb_ops_ioctl, + .fb_open = dlfb_ops_open, + .fb_release = dlfb_ops_release, + .fb_blank = dlfb_ops_blank, + .fb_check_var = dlfb_ops_check_var, + .fb_set_par = dlfb_ops_set_par, }; -static int -dlfb_probe(struct usb_interface *interface, const struct usb_device_id *id) +/* + * Calls dlfb_get_edid() to query the EDID of attached monitor via usb cmds + * Then parses EDID into three places used by various parts of fbdev: + * fb_var_screeninfo contains the timing of the monitor's preferred mode + * fb_info.monspecs is full parsed EDID info, including monspecs.modedb + * fb_info.modelist is a linked list of all monitor & VESA modes which work + * + * If EDID is not readable/valid, then modelist is all VESA modes, + * monspecs is NULL, and fb_var_screeninfo is set to safe VESA mode + * Returns 0 if EDID parses successfully + */ +static int dlfb_parse_edid(struct dlfb_data *dev, + struct fb_var_screeninfo *var, + struct fb_info *info) { - struct dlfb_data *dev_info; - struct fb_info *info; + int i; + const struct fb_videomode *default_vmode = NULL; + int result = 0; - int ret; - char rbuf[4]; + fb_destroy_modelist(&info->modelist); + memset(&info->monspecs, 0, sizeof(info->monspecs)); - dev_info = kzalloc(sizeof(*dev_info), GFP_KERNEL); - if (dev_info == NULL) { - printk("cannot allocate dev_info structure.\n"); - return -ENOMEM; + dlfb_get_edid(dev); + fb_edid_to_monspecs(dev->edid, &info->monspecs); + + if (info->monspecs.modedb_len > 0) { + + for (i = 0; i < info->monspecs.modedb_len; i++) { + if (dlfb_is_valid_mode(&info->monspecs.modedb[i], info)) + fb_add_videomode(&info->monspecs.modedb[i], + &info->modelist); + } + + default_vmode = fb_find_best_display(&info->monspecs, + &info->modelist); + } else { + struct fb_videomode fb_vmode = {0}; + + dl_err("Unable to get valid EDID from device/display\n"); + result = 1; + + /* + * Add the standard VESA modes to our modelist + * Since we don't have EDID, there may be modes that + * overspec monitor and/or are incorrect aspect ratio, etc. + * But at least the user has a chance to choose + */ + for (i = 0; i < VESA_MODEDB_SIZE; i++) { + if (dlfb_is_valid_mode((struct fb_videomode *) + &vesa_modes[i], info)) + fb_add_videomode(&vesa_modes[i], + &info->modelist); + } + + /* + * default to resolution safe for projectors + * (since they are most common case without EDID) + */ + fb_vmode.xres = 800; + fb_vmode.yres = 600; + fb_vmode.refresh = 60; + default_vmode = fb_find_nearest_mode(&fb_vmode, + &info->modelist); } - mutex_init(&dev_info->bulk_mutex); + fb_videomode_to_var(var, default_vmode); + dlfb_var_color_format(var); - dev_info->udev = usb_get_dev(interface_to_usbdev(interface)); - dev_info->interface = interface; + return result; +} - printk("DisplayLink device attached\n"); +static ssize_t metrics_bytes_rendered_show(struct device *fbdev, + struct device_attribute *a, char *buf) { + struct fb_info *fb_info = dev_get_drvdata(fbdev); + struct dlfb_data *dev = fb_info->par; + return snprintf(buf, PAGE_SIZE, "%u\n", + atomic_read(&dev->bytes_rendered)); +} - /* add framebuffer info to usb interface */ - usb_set_intfdata(interface, dev_info); +static ssize_t metrics_bytes_identical_show(struct device *fbdev, + struct device_attribute *a, char *buf) { + struct fb_info *fb_info = dev_get_drvdata(fbdev); + struct dlfb_data *dev = fb_info->par; + return snprintf(buf, PAGE_SIZE, "%u\n", + atomic_read(&dev->bytes_identical)); +} - dev_info->buf = kmalloc(BUF_SIZE, GFP_KERNEL); - /* usb_buffer_alloc(dev_info->udev, BUF_SIZE , GFP_KERNEL, &dev_info->tx_urb->transfer_dma); */ +static ssize_t metrics_bytes_sent_show(struct device *fbdev, + struct device_attribute *a, char *buf) { + struct fb_info *fb_info = dev_get_drvdata(fbdev); + struct dlfb_data *dev = fb_info->par; + return snprintf(buf, PAGE_SIZE, "%u\n", + atomic_read(&dev->bytes_sent)); +} - if (dev_info->buf == NULL) { - printk("unable to allocate memory for dlfb commands\n"); - goto out; - } - dev_info->bufend = dev_info->buf + BUF_SIZE; +static ssize_t metrics_cpu_kcycles_used_show(struct device *fbdev, + struct device_attribute *a, char *buf) { + struct fb_info *fb_info = dev_get_drvdata(fbdev); + struct dlfb_data *dev = fb_info->par; + return snprintf(buf, PAGE_SIZE, "%u\n", + atomic_read(&dev->cpu_kcycles_used)); +} - dev_info->tx_urb = usb_alloc_urb(0, GFP_KERNEL); - usb_fill_bulk_urb(dev_info->tx_urb, dev_info->udev, - usb_sndbulkpipe(dev_info->udev, 1), dev_info->buf, 0, - dlfb_bulk_callback, dev_info); +static ssize_t metrics_misc_show(struct device *fbdev, + struct device_attribute *a, char *buf) { + struct fb_info *fb_info = dev_get_drvdata(fbdev); + struct dlfb_data *dev = fb_info->par; + return snprintf(buf, PAGE_SIZE, + "Calls to\ndamage: %u\nblit: %u\n" + "defio faults: %u\ncopy: %u\n" + "fill: %u\n\n" + "active framebuffer clients: %d\n" + "urbs available %d(%d)\n" + "Shadow framebuffer in use? %s\n" + "Any lost pixels? %s\n", + atomic_read(&dev->damage_count), + atomic_read(&dev->blit_count), + atomic_read(&dev->defio_fault_count), + atomic_read(&dev->copy_count), + atomic_read(&dev->fill_count), + dev->fb_count, + dev->urbs.available, dev->urbs.limit_sem.count, + (dev->backing_buffer) ? "yes" : "no", + atomic_read(&dev->lost_pixels) ? "yes" : "no"); +} - ret = - usb_control_msg(dev_info->udev, usb_rcvctrlpipe(dev_info->udev, 0), - (0x06), (0x80 | (0x02 << 5)), 0, 0, rbuf, 4, 0); - printk("ret control msg 0: %d %x%x%x%x\n", ret, rbuf[0], rbuf[1], - rbuf[2], rbuf[3]); +static ssize_t edid_show(struct kobject *kobj, struct bin_attribute *a, + char *buf, loff_t off, size_t count) { + struct device *fbdev = container_of(kobj, struct device, kobj); + struct fb_info *fb_info = dev_get_drvdata(fbdev); + struct dlfb_data *dev = fb_info->par; + char *edid = &dev->edid[0]; + const size_t size = sizeof(dev->edid); - dlfb_edid(dev_info); + if (dlfb_parse_edid(dev, &fb_info->var, fb_info)) + return 0; - info = framebuffer_alloc(sizeof(u32) * 256, &dev_info->udev->dev); + if (off >= size) + return 0; - if (!info) { - printk("non posso allocare il framebuffer displaylink"); - goto out; - } + if (off + count > size) + count = size - off; + memcpy(buf, edid + off, count); - fb_parse_edid(dev_info->edid, &info->var); + return count; +} - printk("EDID XRES %d YRES %d\n", info->var.xres, info->var.yres); - if (dlfb_set_video_mode(dev_info, info->var.xres, info->var.yres) != 0) { - info->var.xres = 1280; - info->var.yres = 1024; - if (dlfb_set_video_mode - (dev_info, info->var.xres, info->var.yres) != 0) { - goto out; - } +static ssize_t metrics_reset_store(struct device *fbdev, + struct device_attribute *attr, + const char *buf, size_t count) +{ + struct fb_info *fb_info = dev_get_drvdata(fbdev); + struct dlfb_data *dev = fb_info->par; + + atomic_set(&dev->bytes_rendered, 0); + atomic_set(&dev->bytes_identical, 0); + atomic_set(&dev->bytes_sent, 0); + atomic_set(&dev->cpu_kcycles_used, 0); + atomic_set(&dev->blit_count, 0); + atomic_set(&dev->copy_count, 0); + atomic_set(&dev->fill_count, 0); + atomic_set(&dev->defio_fault_count, 0); + atomic_set(&dev->damage_count, 0); + + return count; +} + +static ssize_t use_defio_show(struct device *fbdev, + struct device_attribute *a, char *buf) { + struct fb_info *fb_info = dev_get_drvdata(fbdev); + struct dlfb_data *dev = fb_info->par; + return snprintf(buf, PAGE_SIZE, "%d\n", + atomic_read(&dev->use_defio)); +} + +static ssize_t use_defio_store(struct device *fbdev, + struct device_attribute *attr, + const char *buf, size_t count) +{ + struct fb_info *fb_info = dev_get_drvdata(fbdev); + struct dlfb_data *dev = fb_info->par; + + if (count > 0) { + if (buf[0] == '0') + atomic_set(&dev->use_defio, 0); + if (buf[0] == '1') + atomic_set(&dev->use_defio, 1); } + return count; +} - printk("found valid mode...%d\n", info->var.pixclock); +static struct bin_attribute edid_attr = { + .attr.name = "edid", + .attr.mode = 0444, + .size = 128, + .read = edid_show, +}; - info->pseudo_palette = info->par; - info->par = dev_info; +static struct device_attribute fb_device_attrs[] = { + __ATTR_RO(metrics_bytes_rendered), + __ATTR_RO(metrics_bytes_identical), + __ATTR_RO(metrics_bytes_sent), + __ATTR_RO(metrics_cpu_kcycles_used), + __ATTR_RO(metrics_misc), + __ATTR(metrics_reset, S_IWUGO, NULL, metrics_reset_store), + __ATTR_RW(use_defio), +}; - dev_info->info = info; +#ifdef CONFIG_FB_DEFERRED_IO +static void dlfb_dpy_deferred_io(struct fb_info *info, + struct list_head *pagelist) +{ + struct page *cur; + struct fb_deferred_io *fbdefio = info->fbdefio; + struct dlfb_data *dev = info->par; + struct urb *urb; + char *cmd; + cycles_t start_cycles, end_cycles; + int bytes_sent = 0; + int bytes_identical = 0; + int bytes_rendered = 0; + int fault_count = 0; + + if (!atomic_read(&dev->use_defio)) + return; - info->flags = - FBINFO_DEFAULT | FBINFO_READS_FAST | FBINFO_HWACCEL_IMAGEBLIT | - FBINFO_HWACCEL_COPYAREA | FBINFO_HWACCEL_FILLRECT; - info->fbops = &dlfb_ops; - info->screen_base = rvmalloc(dev_info->screen_size); + if (!atomic_read(&dev->usb_active)) + return; + + start_cycles = get_cycles(); - if (info->screen_base == NULL) { - printk - ("cannot allocate framebuffer virtual memory of %d bytes\n", - dev_info->screen_size); - goto out0; + urb = dlfb_get_urb(dev); + if (!urb) + return; + cmd = urb->transfer_buffer; + + /* walk the written page list and render each to device */ + list_for_each_entry(cur, &fbdefio->pagelist, lru) { + dlfb_render_hline(dev, &urb, (char *) info->fix.smem_start, + &cmd, cur->index << PAGE_SHIFT, + PAGE_SIZE, &bytes_identical, &bytes_sent); + bytes_rendered += PAGE_SIZE; + fault_count++; } - printk("screen base allocated !!!\n"); + if (cmd > (char *) urb->transfer_buffer) { + /* Send partial buffer remaining before exiting */ + int len = cmd - (char *) urb->transfer_buffer; + dlfb_submit_urb(dev, urb, len); + bytes_sent += len; + } else + dlfb_urb_completion(urb); + + atomic_add(fault_count, &dev->defio_fault_count); + atomic_add(bytes_sent, &dev->bytes_sent); + atomic_add(bytes_identical, &dev->bytes_identical); + atomic_add(bytes_rendered, &dev->bytes_rendered); + end_cycles = get_cycles(); + atomic_add(((unsigned int) ((end_cycles - start_cycles) + >> 10)), /* Kcycles */ + &dev->cpu_kcycles_used); +} - dev_info->backing_buffer = kzalloc(dev_info->screen_size, GFP_KERNEL); +static struct fb_deferred_io dlfb_defio = { + .delay = 5, + .deferred_io = dlfb_dpy_deferred_io, +}; - if (!dev_info->backing_buffer) - printk("non posso allocare il backing buffer\n"); +#endif - /* info->var = dev_info->si; */ +/* + * This is necessary before we can communicate with the display controller. + */ +static int dlfb_select_std_channel(struct dlfb_data *dev) +{ + int ret; + u8 set_def_chn[] = { 0x57, 0xCD, 0xDC, 0xA7, + 0x1C, 0x88, 0x5E, 0x15, + 0x60, 0xFE, 0xC6, 0x97, + 0x16, 0x3D, 0x47, 0xF2 }; + + ret = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0), + NR_USB_REQUEST_CHANNEL, + (USB_DIR_OUT | USB_TYPE_VENDOR), 0, 0, + set_def_chn, sizeof(set_def_chn), USB_CTRL_SET_TIMEOUT); + return ret; +} - info->var.bits_per_pixel = 16; - info->var.activate = FB_ACTIVATE_TEST; - info->var.vmode = FB_VMODE_NONINTERLACED; - info->var.red.offset = 11; - info->var.red.length = 5; - info->var.red.msb_right = 0; +static int dlfb_usb_probe(struct usb_interface *interface, + const struct usb_device_id *id) +{ + struct usb_device *usbdev; + struct dlfb_data *dev; + struct fb_info *info; + int videomemorysize; + int i; + unsigned char *videomemory; + int retval = -ENOMEM; + struct fb_var_screeninfo *var; + int registered = 0; + u16 *pix_framebuffer; + + /* usb initialization */ + + usbdev = interface_to_usbdev(interface); + + dev = kzalloc(sizeof(*dev), GFP_KERNEL); + if (dev == NULL) { + err("dlfb_usb_probe: failed alloc of dev struct\n"); + goto error; + } - info->var.green.offset = 5; - info->var.green.length = 6; - info->var.green.msb_right = 0; + /* we need to wait for both usb and fbdev to spin down on disconnect */ + kref_init(&dev->kref); /* matching kref_put in usb .disconnect fn */ + kref_get(&dev->kref); /* matching kref_put in .fb_destroy function*/ - info->var.blue.offset = 0; - info->var.blue.length = 5; - info->var.blue.msb_right = 0; + dev->udev = usbdev; + dev->gdev = &usbdev->dev; /* our generic struct device * */ + usb_set_intfdata(interface, dev); - /* info->var.pixclock = (10000000 / FB_W * 1000 / FB_H)/2 ; */ + if (!dlfb_alloc_urb_list(dev, WRITES_IN_FLIGHT, MAX_TRANSFER)) { + retval = -ENOMEM; + dl_err("dlfb_alloc_urb_list failed\n"); + goto error; + } - info->fix.smem_start = (unsigned long)info->screen_base; - info->fix.smem_len = PAGE_ALIGN(dev_info->screen_size); - if (strlen(dev_info->udev->product) > 15) { - memcpy(info->fix.id, dev_info->udev->product, 15); - } else { - memcpy(info->fix.id, dev_info->udev->product, - strlen(dev_info->udev->product)); + mutex_init(&dev->fb_open_lock); + + /* We don't register a new USB class. Our client interface is fbdev */ + + /* allocates framebuffer driver structure, not framebuffer memory */ + info = framebuffer_alloc(0, &usbdev->dev); + if (!info) { + retval = -ENOMEM; + dl_err("framebuffer_alloc failed\n"); + goto error; + } + dev->info = info; + info->par = dev; + info->pseudo_palette = dev->pseudo_palette; + info->fbops = &dlfb_ops; + + var = &info->var; + + /* TODO set limit based on actual SKU detection */ + dev->sku_pixel_limit = 2048 * 1152; + + INIT_LIST_HEAD(&info->modelist); + dlfb_parse_edid(dev, var, info); + + /* + * ok, now that we've got the size info, we can alloc our framebuffer. + */ + info->fix = dlfb_fix; + info->fix.line_length = var->xres * (var->bits_per_pixel / 8); + videomemorysize = info->fix.line_length * var->yres; + + /* + * The big chunk of system memory we use as a virtual framebuffer. + * TODO: Handle fbcon cursor code calling blit in interrupt context + */ + videomemory = vmalloc(videomemorysize); + if (!videomemory) { + retval = -ENOMEM; + dl_err("Virtual framebuffer alloc failed\n"); + goto error; } - info->fix.type = FB_TYPE_PACKED_PIXELS; - info->fix.visual = FB_VISUAL_TRUECOLOR; - info->fix.accel = info->flags; - info->fix.line_length = dev_info->line_length; - if (fb_alloc_cmap(&info->cmap, 256, 0) < 0) - goto out1; + info->screen_base = videomemory; + info->fix.smem_len = PAGE_ALIGN(videomemorysize); + info->fix.smem_start = (unsigned long) videomemory; + info->flags = udlfb_info_flags; + + + /* + * Second framebuffer copy, mirroring the state of the framebuffer + * on the physical USB device. We can function without this. + * But with imperfect damage info we may end up sending pixels over USB + * that were, in fact, unchanged -- wasting limited USB bandwidth + */ + dev->backing_buffer = vmalloc(videomemorysize); + if (!dev->backing_buffer) + dl_warn("No shadow/backing buffer allcoated\n"); + else + memset(dev->backing_buffer, 0, videomemorysize); + + retval = fb_alloc_cmap(&info->cmap, 256, 0); + if (retval < 0) { + dl_err("fb_alloc_cmap failed %x\n", retval); + goto error; + } + + /* ready to begin using device */ + +#ifdef CONFIG_FB_DEFERRED_IO + atomic_set(&dev->use_defio, 1); +#endif + atomic_set(&dev->usb_active, 1); + dlfb_select_std_channel(dev); + + dlfb_ops_check_var(var, info); + dlfb_ops_set_par(info); + + /* paint greenscreen */ + pix_framebuffer = (u16 *) videomemory; + for (i = 0; i < videomemorysize / 2; i++) + pix_framebuffer[i] = 0x37e6; + + dlfb_handle_damage(dev, 0, 0, info->var.xres, info->var.yres, + videomemory); - printk("colormap allocated\n"); - if (register_framebuffer(info) < 0) - goto out2; + retval = register_framebuffer(info); + if (retval < 0) { + dl_err("register_framebuffer failed %d\n", retval); + goto error; + } + registered = 1; + + for (i = 0; i < ARRAY_SIZE(fb_device_attrs); i++) + device_create_file(info->dev, &fb_device_attrs[i]); - draw_rect(dev_info, 0, 0, dev_info->info->var.xres, - dev_info->info->var.yres, 0x30, 0xff, 0x30); + device_create_bin_file(info->dev, &edid_attr); + dl_err("DisplayLink USB device /dev/fb%d attached. %dx%d resolution." + " Using %dK framebuffer memory\n", info->node, + var->xres, var->yres, + ((dev->backing_buffer) ? + videomemorysize * 2 : videomemorysize) >> 10); return 0; -out2: - fb_dealloc_cmap(&info->cmap); -out1: - rvfree(info->screen_base, dev_info->screen_size); -out0: - framebuffer_release(info); -out: - usb_set_intfdata(interface, NULL); - usb_put_dev(dev_info->udev); - kfree(dev_info); - return -ENOMEM; +error: + if (dev) { + if (registered) { + unregister_framebuffer(info); + dlfb_ops_destroy(info); + } else + kref_put(&dev->kref, dlfb_delete); + + if (dev->urbs.count > 0) + dlfb_free_urb_list(dev); + kref_put(&dev->kref, dlfb_delete); /* last ref from kref_init */ + + /* dev has been deallocated. Do not dereference */ + } + return retval; } -static void dlfb_disconnect(struct usb_interface *interface) +static void dlfb_usb_disconnect(struct usb_interface *interface) { - struct dlfb_data *dev_info = usb_get_intfdata(interface); + struct dlfb_data *dev; + struct fb_info *info; + int i; + + dev = usb_get_intfdata(interface); + info = dev->info; - mutex_unlock(&dev_info->bulk_mutex); + /* when non-active we'll update virtual framebuffer, but no new urbs */ + atomic_set(&dev->usb_active, 0); - usb_kill_urb(dev_info->tx_urb); - usb_free_urb(dev_info->tx_urb); usb_set_intfdata(interface, NULL); - usb_put_dev(dev_info->udev); - if (dev_info->info) { - unregister_framebuffer(dev_info->info); - fb_dealloc_cmap(&dev_info->info->cmap); - rvfree(dev_info->info->screen_base, dev_info->screen_size); - kfree(dev_info->backing_buffer); - framebuffer_release(dev_info->info); + for (i = 0; i < ARRAY_SIZE(fb_device_attrs); i++) + device_remove_file(info->dev, &fb_device_attrs[i]); + + device_remove_bin_file(info->dev, &edid_attr); + /* this function will wait for all in-flight urbs to complete */ + dlfb_free_urb_list(dev); + + if (info) { + dl_notice("Detaching /dev/fb%d\n", info->node); + unregister_framebuffer(info); + dlfb_ops_destroy(info); } - kfree(dev_info); + /* release reference taken by kref_init in probe() */ + kref_put(&dev->kref, dlfb_delete); + + /* consider dlfb_data freed */ - printk("DisplayLink device disconnected\n"); + return; } static struct usb_driver dlfb_driver = { .name = "udlfb", - .probe = dlfb_probe, - .disconnect = dlfb_disconnect, + .probe = dlfb_usb_probe, + .disconnect = dlfb_usb_disconnect, .id_table = id_table, }; -static int __init dlfb_init(void) +static int __init dlfb_module_init(void) { int res; - dlfb_init_modes(); - res = usb_register(&dlfb_driver); if (res) err("usb_register failed. Error number %d", res); @@ -925,14 +1442,186 @@ static int __init dlfb_init(void) return res; } -static void __exit dlfb_exit(void) +static void __exit dlfb_module_exit(void) { usb_deregister(&dlfb_driver); } -module_init(dlfb_init); -module_exit(dlfb_exit); +module_init(dlfb_module_init); +module_exit(dlfb_module_exit); -MODULE_AUTHOR("Roberto De Ioris <roberto@unbit.it>"); -MODULE_DESCRIPTION(DRIVER_VERSION); +static void dlfb_urb_completion(struct urb *urb) +{ + struct urb_node *unode = urb->context; + struct dlfb_data *dev = unode->dev; + unsigned long flags; + + /* sync/async unlink faults aren't errors */ + if (urb->status) { + if (!(urb->status == -ENOENT || + urb->status == -ECONNRESET || + urb->status == -ESHUTDOWN)) { + dl_err("%s - nonzero write bulk status received: %d\n", + __func__, urb->status); + atomic_set(&dev->lost_pixels, 1); + } + } + + urb->transfer_buffer_length = dev->urbs.size; /* reset to actual */ + + spin_lock_irqsave(&dev->urbs.lock, flags); + list_add_tail(&unode->entry, &dev->urbs.list); + dev->urbs.available++; + spin_unlock_irqrestore(&dev->urbs.lock, flags); + + up(&dev->urbs.limit_sem); +} + +static void dlfb_free_urb_list(struct dlfb_data *dev) +{ + int count = dev->urbs.count; + struct list_head *node; + struct urb_node *unode; + struct urb *urb; + int ret; + unsigned long flags; + + dl_notice("Waiting for completes and freeing all render urbs\n"); + + /* keep waiting and freeing, until we've got 'em all */ + while (count--) { + /* Timeout means a memory leak and/or fault */ + ret = down_timeout(&dev->urbs.limit_sem, FREE_URB_TIMEOUT); + if (ret) { + BUG_ON(ret); + break; + } + spin_lock_irqsave(&dev->urbs.lock, flags); + + node = dev->urbs.list.next; /* have reserved one with sem */ + list_del_init(node); + + spin_unlock_irqrestore(&dev->urbs.lock, flags); + + unode = list_entry(node, struct urb_node, entry); + urb = unode->urb; + + /* Free each separately allocated piece */ + usb_buffer_free(urb->dev, dev->urbs.size, + urb->transfer_buffer, urb->transfer_dma); + usb_free_urb(urb); + kfree(node); + } + + kref_put(&dev->kref, dlfb_delete); + +} + +static int dlfb_alloc_urb_list(struct dlfb_data *dev, int count, size_t size) +{ + int i = 0; + struct urb *urb; + struct urb_node *unode; + char *buf; + + spin_lock_init(&dev->urbs.lock); + + dev->urbs.size = size; + INIT_LIST_HEAD(&dev->urbs.list); + + while (i < count) { + unode = kzalloc(sizeof(struct urb_node), GFP_KERNEL); + if (!unode) + break; + unode->dev = dev; + + urb = usb_alloc_urb(0, GFP_KERNEL); + if (!urb) { + kfree(unode); + break; + } + unode->urb = urb; + + buf = usb_buffer_alloc(dev->udev, MAX_TRANSFER, GFP_KERNEL, + &urb->transfer_dma); + if (!buf) { + kfree(unode); + usb_free_urb(urb); + break; + } + + /* urb->transfer_buffer_length set to actual before submit */ + usb_fill_bulk_urb(urb, dev->udev, usb_sndbulkpipe(dev->udev, 1), + buf, size, dlfb_urb_completion, unode); + urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; + + list_add_tail(&unode->entry, &dev->urbs.list); + + i++; + } + + sema_init(&dev->urbs.limit_sem, i); + dev->urbs.count = i; + dev->urbs.available = i; + + kref_get(&dev->kref); /* released in free_render_urbs() */ + + dl_notice("allocated %d %d byte urbs \n", i, (int) size); + + return i; +} + +static struct urb *dlfb_get_urb(struct dlfb_data *dev) +{ + int ret = 0; + struct list_head *entry; + struct urb_node *unode; + struct urb *urb = NULL; + unsigned long flags; + + /* Wait for an in-flight buffer to complete and get re-queued */ + ret = down_timeout(&dev->urbs.limit_sem, GET_URB_TIMEOUT); + if (ret) { + atomic_set(&dev->lost_pixels, 1); + dl_err("wait for urb interrupted: %x\n", ret); + goto error; + } + + spin_lock_irqsave(&dev->urbs.lock, flags); + + BUG_ON(list_empty(&dev->urbs.list)); /* reserved one with limit_sem */ + entry = dev->urbs.list.next; + list_del_init(entry); + dev->urbs.available--; + + spin_unlock_irqrestore(&dev->urbs.lock, flags); + + unode = list_entry(entry, struct urb_node, entry); + urb = unode->urb; + +error: + return urb; +} + +static int dlfb_submit_urb(struct dlfb_data *dev, struct urb *urb, size_t len) +{ + int ret; + + BUG_ON(len > dev->urbs.size); + + urb->transfer_buffer_length = len; /* set to actual payload len */ + ret = usb_submit_urb(urb, GFP_KERNEL); + if (ret) { + dlfb_urb_completion(urb); /* because no one else will */ + atomic_set(&dev->lost_pixels, 1); + dl_err("usb_submit_urb error %x\n", ret); + } + return ret; +} + +MODULE_AUTHOR("Roberto De Ioris <roberto@unbit.it>, " + "Jaya Kumar <jayakumar.lkml@gmail.com>, " + "Bernie Thompson <bernie@plugable.com>"); +MODULE_DESCRIPTION("DisplayLink kernel framebuffer driver"); MODULE_LICENSE("GPL"); + diff --git a/drivers/staging/udlfb/udlfb.h b/drivers/staging/udlfb/udlfb.h index 40ad85e..b07a693 100644 --- a/drivers/staging/udlfb/udlfb.h +++ b/drivers/staging/udlfb/udlfb.h @@ -1,225 +1,106 @@ #ifndef UDLFB_H #define UDLFB_H -#define MAX_VMODES 4 -#define FB_BPP 16 +/* + * TODO: Propose standard fb.h ioctl for reporting damage, + * using _IOWR() and one of the existing area structs from fb.h + * Consider these ioctls deprecated, but they're still used by the + * DisplayLink X server as yet - need both to be modified in tandem + * when new ioctl(s) are ready. + */ +#define DLFB_IOCTL_RETURN_EDID 0xAD +#define DLFB_IOCTL_REPORT_DAMAGE 0xAA +struct dloarea { + int x, y; + int w, h; + int x2, y2; +}; -#define STD_CHANNEL "\x57\xCD\xDC\xA7\x1C\x88\x5E\x15" \ - "\x60\xFE\xC6\x97\x16\x3D\x47\xF2" +struct urb_node { + struct list_head entry; + struct dlfb_data *dev; + struct urb *urb; +}; -/* as libdlo */ -#define BUF_HIGH_WATER_MARK 1024 -#define BUF_SIZE (64*1024) +struct urb_list { + struct list_head list; + spinlock_t lock; + struct semaphore limit_sem; + int available; + int count; + size_t size; +}; struct dlfb_data { struct usb_device *udev; - struct usb_interface *interface; - struct urb *tx_urb, *ctrl_urb; - struct usb_ctrlrequest dr; + struct device *gdev; /* &udev->dev */ struct fb_info *info; - char *buf; - char *bufend; + struct urb_list urbs; + struct kref kref; char *backing_buffer; - struct mutex bulk_mutex; + struct delayed_work deferred_work; + struct mutex fb_open_lock; + int fb_count; + atomic_t usb_active; /* 0 = update virtual buffer, but no usb traffic */ + atomic_t lost_pixels; /* 1 = a render op failed. Need screen refresh */ + atomic_t use_defio; /* 0 = rely on ioctls and blit/copy/fill rects */ char edid[128]; - int screen_size; - int line_length; - struct completion done; + int sku_pixel_limit; int base16; - int base16d; int base8; - int base8d; + u32 pseudo_palette[256]; + /* blit-only rendering path metrics, exposed through sysfs */ + atomic_t bytes_rendered; /* raw pixel-bytes driver asked to render */ + atomic_t bytes_identical; /* saved effort with backbuffer comparison */ + atomic_t bytes_sent; /* to usb, after compression including overhead */ + atomic_t cpu_kcycles_used; /* transpired during pixel processing */ + /* interface usage metrics. Clients can call driver via several */ + atomic_t blit_count; + atomic_t copy_count; + atomic_t fill_count; + atomic_t damage_count; + atomic_t defio_fault_count; }; -struct dlfb_video_mode { - uint8_t col; - uint32_t hclock; - uint32_t vclock; - uint8_t unknown1[6]; - uint16_t xres; - uint8_t unknown2[6]; - uint16_t yres; - uint8_t unknown3[4]; -} __attribute__ ((__packed__)); - -static struct dlfb_video_mode dlfb_video_modes[MAX_VMODES]; - -static void dlfb_bulk_callback(struct urb *urb) -{ - struct dlfb_data *dev_info = urb->context; - complete(&dev_info->done); -} - -static void dlfb_edid(struct dlfb_data *dev_info) -{ - int i; - int ret; - char rbuf[2]; - - for (i = 0; i < 128; i++) { - ret = - usb_control_msg(dev_info->udev, - usb_rcvctrlpipe(dev_info->udev, 0), (0x02), - (0x80 | (0x02 << 5)), i << 8, 0xA1, rbuf, 2, - 0); - /*printk("ret control msg edid %d: %d [%d]\n",i, ret, rbuf[1]); */ - dev_info->edid[i] = rbuf[1]; - } - -} - -static int dlfb_bulk_msg(struct dlfb_data *dev_info, int len) -{ - int ret; - - init_completion(&dev_info->done); - - dev_info->tx_urb->actual_length = 0; - dev_info->tx_urb->transfer_buffer_length = len; - - ret = usb_submit_urb(dev_info->tx_urb, GFP_KERNEL); - if (!wait_for_completion_timeout(&dev_info->done, 1000)) { - usb_kill_urb(dev_info->tx_urb); - printk("usb timeout !!!\n"); - } - - return dev_info->tx_urb->actual_length; -} - -static void dlfb_init_modes(void) -{ - dlfb_video_modes[0].col = 0; - memcpy(&dlfb_video_modes[0].hclock, "\x20\x3C\x7A\xC9", 4); - memcpy(&dlfb_video_modes[0].vclock, "\xF2\x6C\x48\xF9", 4); - memcpy(&dlfb_video_modes[0].unknown1, "\x70\x53\xFF\xFF\x21\x27", 6); - dlfb_video_modes[0].xres = 800; - memcpy(&dlfb_video_modes[0].unknown2, "\x91\xF3\xFF\xFF\xFF\xF9", 6); - dlfb_video_modes[0].yres = 480; - memcpy(&dlfb_video_modes[0].unknown3, "\x01\x02\xC8\x19", 4); - - dlfb_video_modes[1].col = 0; - memcpy(&dlfb_video_modes[1].hclock, "\x36\x18\xD5\x10", 4); - memcpy(&dlfb_video_modes[1].vclock, "\x60\xA9\x7B\x33", 4); - memcpy(&dlfb_video_modes[1].unknown1, "\xA1\x2B\x27\x32\xFF\xFF", 6); - dlfb_video_modes[1].xres = 1024; - memcpy(&dlfb_video_modes[1].unknown2, "\xD9\x9A\xFF\xCA\xFF\xFF", 6); - dlfb_video_modes[1].yres = 768; - memcpy(&dlfb_video_modes[1].unknown3, "\x04\x03\xC8\x32", 4); - - dlfb_video_modes[2].col = 0; - memcpy(&dlfb_video_modes[2].hclock, "\x98\xF8\x0D\x57", 4); - memcpy(&dlfb_video_modes[2].vclock, "\x2A\x55\x4D\x54", 4); - memcpy(&dlfb_video_modes[2].unknown1, "\xCA\x0D\xFF\xFF\x94\x43", 6); - dlfb_video_modes[2].xres = 1280; - memcpy(&dlfb_video_modes[2].unknown2, "\x9A\xA8\xFF\xFF\xFF\xF9", 6); - dlfb_video_modes[2].yres = 1024; - memcpy(&dlfb_video_modes[2].unknown3, "\x04\x02\x60\x54", 4); - - dlfb_video_modes[3].col = 0; - memcpy(&dlfb_video_modes[3].hclock, "\x42\x24\x38\x36", 4); - memcpy(&dlfb_video_modes[3].vclock, "\xC1\x52\xD9\x29", 4); - memcpy(&dlfb_video_modes[3].unknown1, "\xEA\xB8\x32\x60\xFF\xFF", 6); - dlfb_video_modes[3].xres = 1400; - memcpy(&dlfb_video_modes[3].unknown2, "\xC9\x4E\xFF\xFF\xFF\xF2", 6); - dlfb_video_modes[3].yres = 1050; - memcpy(&dlfb_video_modes[3].unknown3, "\x04\x02\x1E\x5F", 4); -} - -static char *dlfb_set_register(char *bufptr, uint8_t reg, uint8_t val) -{ - *bufptr++ = 0xAF; - *bufptr++ = 0x20; - *bufptr++ = reg; - *bufptr++ = val; - - return bufptr; -} - -static int dlfb_set_video_mode(struct dlfb_data *dev_info, int width, int height) -{ - int i, ret; - unsigned char j; - char *bufptr = dev_info->buf; - uint8_t *vdata; - - for (i = 0; i < MAX_VMODES; i++) { - printk("INIT VIDEO %d %d %d\n", i, dlfb_video_modes[i].xres, - dlfb_video_modes[i].yres); - if (dlfb_video_modes[i].xres == width - && dlfb_video_modes[i].yres == height) { - - dev_info->base16 = 0; - dev_info->base16d = width * height * (FB_BPP / 8); - - //dev_info->base8 = width * height * (FB_BPP / 8); - - dev_info->base8 = dev_info->base16; - dev_info->base8d = dev_info->base16d; - - /* set encryption key (null) */ - memcpy(dev_info->buf, STD_CHANNEL, 16); - ret = - usb_control_msg(dev_info->udev, - usb_sndctrlpipe(dev_info->udev, 0), - 0x12, (0x02 << 5), 0, 0, - dev_info->buf, 16, 0); - printk("ret control msg 1 (STD_CHANNEL): %d\n", ret); - - /* set registers */ - bufptr = dlfb_set_register(bufptr, 0xFF, 0x00); - - /* set color depth */ - bufptr = dlfb_set_register(bufptr, 0x00, 0x00); - - /* set addresses */ - bufptr = - dlfb_set_register(bufptr, 0x20, - (char)(dev_info->base16 >> 16)); - bufptr = - dlfb_set_register(bufptr, 0x21, - (char)(dev_info->base16 >> 8)); - bufptr = - dlfb_set_register(bufptr, 0x22, - (char)(dev_info->base16)); - - bufptr = - dlfb_set_register(bufptr, 0x26, - (char)(dev_info->base8 >> 16)); - bufptr = - dlfb_set_register(bufptr, 0x27, - (char)(dev_info->base8 >> 8)); - bufptr = - dlfb_set_register(bufptr, 0x28, - (char)(dev_info->base8)); +#define NR_USB_REQUEST_I2C_SUB_IO 0x02 +#define NR_USB_REQUEST_CHANNEL 0x12 - /* set video mode */ - vdata = (uint8_t *)&dlfb_video_modes[i]; - for (j = 0; j < 29; j++) - bufptr = dlfb_set_register(bufptr, j, vdata[j]); +/* -BULK_SIZE as per usb-skeleton. Can we get full page and avoid overhead? */ +#define BULK_SIZE 512 +#define MAX_TRANSFER (PAGE_SIZE*16 - BULK_SIZE) +#define WRITES_IN_FLIGHT (4) - /* blank */ - bufptr = dlfb_set_register(bufptr, 0x1F, 0x00); +#define GET_URB_TIMEOUT HZ +#define FREE_URB_TIMEOUT (HZ*2) - /* end registers */ - bufptr = dlfb_set_register(bufptr, 0xFF, 0xFF); +#define BPP 2 +#define MAX_CMD_PIXELS 255 - /* send */ - ret = dlfb_bulk_msg(dev_info, bufptr - dev_info->buf); - printk("ret bulk 2: %d %td\n", ret, - bufptr - dev_info->buf); +#define RLX_HEADER_BYTES 7 +#define MIN_RLX_PIX_BYTES 4 +#define MIN_RLX_CMD_BYTES (RLX_HEADER_BYTES + MIN_RLX_PIX_BYTES) - /* flush */ - ret = dlfb_bulk_msg(dev_info, 0); - printk("ret bulk 3: %d\n", ret); +#define RLE_HEADER_BYTES 6 +#define MIN_RLE_PIX_BYTES 3 +#define MIN_RLE_CMD_BYTES (RLE_HEADER_BYTES + MIN_RLE_PIX_BYTES) - dev_info->screen_size = width * height * (FB_BPP / 8); - dev_info->line_length = width * (FB_BPP / 8); +#define RAW_HEADER_BYTES 6 +#define MIN_RAW_PIX_BYTES 2 +#define MIN_RAW_CMD_BYTES (RAW_HEADER_BYTES + MIN_RAW_PIX_BYTES) - return 0; - } - } +/* remove these once align.h patch is taken into kernel */ +#define DL_ALIGN_UP(x, a) ALIGN(x, a) +#define DL_ALIGN_DOWN(x, a) ALIGN(x-(a-1), a) - return -1; -} +/* remove once this gets added to sysfs.h */ +#define __ATTR_RW(attr) __ATTR(attr, 0644, attr##_show, attr##_store) +#define dl_err(format, arg...) \ + dev_err(dev->gdev, "dlfb: " format, ## arg) +#define dl_warn(format, arg...) \ + dev_warn(dev->gdev, "dlfb: " format, ## arg) +#define dl_notice(format, arg...) \ + dev_notice(dev->gdev, "dlfb: " format, ## arg) +#define dl_info(format, arg...) \ + dev_info(dev->gdev, "dlfb: " format, ## arg) #endif |