1f7018c21STomi Valkeinen /* 2f7018c21STomi Valkeinen * udlfb.c -- Framebuffer driver for DisplayLink USB controller 3f7018c21STomi Valkeinen * 4f7018c21STomi Valkeinen * Copyright (C) 2009 Roberto De Ioris <roberto@unbit.it> 5f7018c21STomi Valkeinen * Copyright (C) 2009 Jaya Kumar <jayakumar.lkml@gmail.com> 6f7018c21STomi Valkeinen * Copyright (C) 2009 Bernie Thompson <bernie@plugable.com> 7f7018c21STomi Valkeinen * 8f7018c21STomi Valkeinen * This file is subject to the terms and conditions of the GNU General Public 9f7018c21STomi Valkeinen * License v2. See the file COPYING in the main directory of this archive for 10f7018c21STomi Valkeinen * more details. 11f7018c21STomi Valkeinen * 12f7018c21STomi Valkeinen * Layout is based on skeletonfb by James Simmons and Geert Uytterhoeven, 13f7018c21STomi Valkeinen * usb-skeleton by GregKH. 14f7018c21STomi Valkeinen * 15f7018c21STomi Valkeinen * Device-specific portions based on information from Displaylink, with work 16f7018c21STomi Valkeinen * from Florian Echtler, Henrik Bjerregaard Pedersen, and others. 17f7018c21STomi Valkeinen */ 18f7018c21STomi Valkeinen 19f7018c21STomi Valkeinen #include <linux/module.h> 20f7018c21STomi Valkeinen #include <linux/kernel.h> 21f7018c21STomi Valkeinen #include <linux/init.h> 22f7018c21STomi Valkeinen #include <linux/usb.h> 23f7018c21STomi Valkeinen #include <linux/uaccess.h> 24f7018c21STomi Valkeinen #include <linux/mm.h> 25f7018c21STomi Valkeinen #include <linux/fb.h> 26f7018c21STomi Valkeinen #include <linux/vmalloc.h> 27f7018c21STomi Valkeinen #include <linux/slab.h> 28f7018c21STomi Valkeinen #include <linux/prefetch.h> 29f7018c21STomi Valkeinen #include <linux/delay.h> 304e705e17SMikulas Patocka #include <asm/unaligned.h> 31f7018c21STomi Valkeinen #include <video/udlfb.h> 32f7018c21STomi Valkeinen #include "edid.h" 33f7018c21STomi Valkeinen 34fa738a5cSLadislav Michl static const struct fb_fix_screeninfo dlfb_fix = { 35f7018c21STomi Valkeinen .id = "udlfb", 36f7018c21STomi Valkeinen .type = FB_TYPE_PACKED_PIXELS, 37f7018c21STomi Valkeinen .visual = FB_VISUAL_TRUECOLOR, 38f7018c21STomi Valkeinen .xpanstep = 0, 39f7018c21STomi Valkeinen .ypanstep = 0, 40f7018c21STomi Valkeinen .ywrapstep = 0, 41f7018c21STomi Valkeinen .accel = FB_ACCEL_NONE, 42f7018c21STomi Valkeinen }; 43f7018c21STomi Valkeinen 44f7018c21STomi Valkeinen static const u32 udlfb_info_flags = FBINFO_DEFAULT | FBINFO_READS_FAST | 45f7018c21STomi Valkeinen FBINFO_VIRTFB | 46f7018c21STomi Valkeinen FBINFO_HWACCEL_IMAGEBLIT | FBINFO_HWACCEL_FILLRECT | 47f7018c21STomi Valkeinen FBINFO_HWACCEL_COPYAREA | FBINFO_MISC_ALWAYS_SETPAR; 48f7018c21STomi Valkeinen 49f7018c21STomi Valkeinen /* 50f7018c21STomi Valkeinen * There are many DisplayLink-based graphics products, all with unique PIDs. 51f7018c21STomi Valkeinen * So we match on DisplayLink's VID + Vendor-Defined Interface Class (0xff) 52f7018c21STomi Valkeinen * We also require a match on SubClass (0x00) and Protocol (0x00), 53f7018c21STomi Valkeinen * which is compatible with all known USB 2.0 era graphics chips and firmware, 54f7018c21STomi Valkeinen * but allows DisplayLink to increment those for any future incompatible chips 55f7018c21STomi Valkeinen */ 5669de8496SArvind Yadav static const struct usb_device_id id_table[] = { 57f7018c21STomi Valkeinen {.idVendor = 0x17e9, 58f7018c21STomi Valkeinen .bInterfaceClass = 0xff, 59f7018c21STomi Valkeinen .bInterfaceSubClass = 0x00, 60f7018c21STomi Valkeinen .bInterfaceProtocol = 0x00, 61f7018c21STomi Valkeinen .match_flags = USB_DEVICE_ID_MATCH_VENDOR | 62f7018c21STomi Valkeinen USB_DEVICE_ID_MATCH_INT_CLASS | 63f7018c21STomi Valkeinen USB_DEVICE_ID_MATCH_INT_SUBCLASS | 64f7018c21STomi Valkeinen USB_DEVICE_ID_MATCH_INT_PROTOCOL, 65f7018c21STomi Valkeinen }, 66f7018c21STomi Valkeinen {}, 67f7018c21STomi Valkeinen }; 68f7018c21STomi Valkeinen MODULE_DEVICE_TABLE(usb, id_table); 69f7018c21STomi Valkeinen 70f7018c21STomi Valkeinen /* module options */ 71f7018c21STomi Valkeinen static bool console = 1; /* Allow fbcon to open framebuffer */ 72f7018c21STomi Valkeinen static bool fb_defio = 1; /* Detect mmap writes using page faults */ 73f7018c21STomi Valkeinen static bool shadow = 1; /* Optionally disable shadow framebuffer */ 74f7018c21STomi Valkeinen static int pixel_limit; /* Optionally force a pixel resolution limit */ 75f7018c21STomi Valkeinen 76f7018c21STomi Valkeinen /* dlfb keeps a list of urbs for efficient bulk transfers */ 77f7018c21STomi Valkeinen static void dlfb_urb_completion(struct urb *urb); 787ea46206SLadislav Michl static struct urb *dlfb_get_urb(struct dlfb_data *dlfb); 797ea46206SLadislav Michl static int dlfb_submit_urb(struct dlfb_data *dlfb, struct urb * urb, size_t len); 807ea46206SLadislav Michl static int dlfb_alloc_urb_list(struct dlfb_data *dlfb, int count, size_t size); 817ea46206SLadislav Michl static void dlfb_free_urb_list(struct dlfb_data *dlfb); 82f7018c21STomi Valkeinen 83f7018c21STomi Valkeinen /* 84f7018c21STomi Valkeinen * All DisplayLink bulk operations start with 0xAF, followed by specific code 85f7018c21STomi Valkeinen * All operations are written to buffers which then later get sent to device 86f7018c21STomi Valkeinen */ 87f7018c21STomi Valkeinen static char *dlfb_set_register(char *buf, u8 reg, u8 val) 88f7018c21STomi Valkeinen { 89f7018c21STomi Valkeinen *buf++ = 0xAF; 90f7018c21STomi Valkeinen *buf++ = 0x20; 91f7018c21STomi Valkeinen *buf++ = reg; 92f7018c21STomi Valkeinen *buf++ = val; 93f7018c21STomi Valkeinen return buf; 94f7018c21STomi Valkeinen } 95f7018c21STomi Valkeinen 96f7018c21STomi Valkeinen static char *dlfb_vidreg_lock(char *buf) 97f7018c21STomi Valkeinen { 98f7018c21STomi Valkeinen return dlfb_set_register(buf, 0xFF, 0x00); 99f7018c21STomi Valkeinen } 100f7018c21STomi Valkeinen 101f7018c21STomi Valkeinen static char *dlfb_vidreg_unlock(char *buf) 102f7018c21STomi Valkeinen { 103f7018c21STomi Valkeinen return dlfb_set_register(buf, 0xFF, 0xFF); 104f7018c21STomi Valkeinen } 105f7018c21STomi Valkeinen 106f7018c21STomi Valkeinen /* 107f7018c21STomi Valkeinen * Map FB_BLANK_* to DisplayLink register 108f7018c21STomi Valkeinen * DLReg FB_BLANK_* 109f7018c21STomi Valkeinen * ----- ----------------------------- 110f7018c21STomi Valkeinen * 0x00 FB_BLANK_UNBLANK (0) 111f7018c21STomi Valkeinen * 0x01 FB_BLANK (1) 112f7018c21STomi Valkeinen * 0x03 FB_BLANK_VSYNC_SUSPEND (2) 113f7018c21STomi Valkeinen * 0x05 FB_BLANK_HSYNC_SUSPEND (3) 114f7018c21STomi Valkeinen * 0x07 FB_BLANK_POWERDOWN (4) Note: requires modeset to come back 115f7018c21STomi Valkeinen */ 116f7018c21STomi Valkeinen static char *dlfb_blanking(char *buf, int fb_blank) 117f7018c21STomi Valkeinen { 118f7018c21STomi Valkeinen u8 reg; 119f7018c21STomi Valkeinen 120f7018c21STomi Valkeinen switch (fb_blank) { 121f7018c21STomi Valkeinen case FB_BLANK_POWERDOWN: 122f7018c21STomi Valkeinen reg = 0x07; 123f7018c21STomi Valkeinen break; 124f7018c21STomi Valkeinen case FB_BLANK_HSYNC_SUSPEND: 125f7018c21STomi Valkeinen reg = 0x05; 126f7018c21STomi Valkeinen break; 127f7018c21STomi Valkeinen case FB_BLANK_VSYNC_SUSPEND: 128f7018c21STomi Valkeinen reg = 0x03; 129f7018c21STomi Valkeinen break; 130f7018c21STomi Valkeinen case FB_BLANK_NORMAL: 131f7018c21STomi Valkeinen reg = 0x01; 132f7018c21STomi Valkeinen break; 133f7018c21STomi Valkeinen default: 134f7018c21STomi Valkeinen reg = 0x00; 135f7018c21STomi Valkeinen } 136f7018c21STomi Valkeinen 137f7018c21STomi Valkeinen buf = dlfb_set_register(buf, 0x1F, reg); 138f7018c21STomi Valkeinen 139f7018c21STomi Valkeinen return buf; 140f7018c21STomi Valkeinen } 141f7018c21STomi Valkeinen 142f7018c21STomi Valkeinen static char *dlfb_set_color_depth(char *buf, u8 selection) 143f7018c21STomi Valkeinen { 144f7018c21STomi Valkeinen return dlfb_set_register(buf, 0x00, selection); 145f7018c21STomi Valkeinen } 146f7018c21STomi Valkeinen 147f7018c21STomi Valkeinen static char *dlfb_set_base16bpp(char *wrptr, u32 base) 148f7018c21STomi Valkeinen { 149f7018c21STomi Valkeinen /* the base pointer is 16 bits wide, 0x20 is hi byte. */ 150f7018c21STomi Valkeinen wrptr = dlfb_set_register(wrptr, 0x20, base >> 16); 151f7018c21STomi Valkeinen wrptr = dlfb_set_register(wrptr, 0x21, base >> 8); 152f7018c21STomi Valkeinen return dlfb_set_register(wrptr, 0x22, base); 153f7018c21STomi Valkeinen } 154f7018c21STomi Valkeinen 155f7018c21STomi Valkeinen /* 156f7018c21STomi Valkeinen * DisplayLink HW has separate 16bpp and 8bpp framebuffers. 157f7018c21STomi Valkeinen * In 24bpp modes, the low 323 RGB bits go in the 8bpp framebuffer 158f7018c21STomi Valkeinen */ 159f7018c21STomi Valkeinen static char *dlfb_set_base8bpp(char *wrptr, u32 base) 160f7018c21STomi Valkeinen { 161f7018c21STomi Valkeinen wrptr = dlfb_set_register(wrptr, 0x26, base >> 16); 162f7018c21STomi Valkeinen wrptr = dlfb_set_register(wrptr, 0x27, base >> 8); 163f7018c21STomi Valkeinen return dlfb_set_register(wrptr, 0x28, base); 164f7018c21STomi Valkeinen } 165f7018c21STomi Valkeinen 166f7018c21STomi Valkeinen static char *dlfb_set_register_16(char *wrptr, u8 reg, u16 value) 167f7018c21STomi Valkeinen { 168f7018c21STomi Valkeinen wrptr = dlfb_set_register(wrptr, reg, value >> 8); 169f7018c21STomi Valkeinen return dlfb_set_register(wrptr, reg+1, value); 170f7018c21STomi Valkeinen } 171f7018c21STomi Valkeinen 172f7018c21STomi Valkeinen /* 173f7018c21STomi Valkeinen * This is kind of weird because the controller takes some 174f7018c21STomi Valkeinen * register values in a different byte order than other registers. 175f7018c21STomi Valkeinen */ 176f7018c21STomi Valkeinen static char *dlfb_set_register_16be(char *wrptr, u8 reg, u16 value) 177f7018c21STomi Valkeinen { 178f7018c21STomi Valkeinen wrptr = dlfb_set_register(wrptr, reg, value); 179f7018c21STomi Valkeinen return dlfb_set_register(wrptr, reg+1, value >> 8); 180f7018c21STomi Valkeinen } 181f7018c21STomi Valkeinen 182f7018c21STomi Valkeinen /* 183f7018c21STomi Valkeinen * LFSR is linear feedback shift register. The reason we have this is 184f7018c21STomi Valkeinen * because the display controller needs to minimize the clock depth of 185f7018c21STomi Valkeinen * various counters used in the display path. So this code reverses the 186f7018c21STomi Valkeinen * provided value into the lfsr16 value by counting backwards to get 187f7018c21STomi Valkeinen * the value that needs to be set in the hardware comparator to get the 188f7018c21STomi Valkeinen * same actual count. This makes sense once you read above a couple of 189f7018c21STomi Valkeinen * times and think about it from a hardware perspective. 190f7018c21STomi Valkeinen */ 191f7018c21STomi Valkeinen static u16 dlfb_lfsr16(u16 actual_count) 192f7018c21STomi Valkeinen { 193f7018c21STomi Valkeinen u32 lv = 0xFFFF; /* This is the lfsr value that the hw starts with */ 194f7018c21STomi Valkeinen 195f7018c21STomi Valkeinen while (actual_count--) { 196f7018c21STomi Valkeinen lv = ((lv << 1) | 197f7018c21STomi Valkeinen (((lv >> 15) ^ (lv >> 4) ^ (lv >> 2) ^ (lv >> 1)) & 1)) 198f7018c21STomi Valkeinen & 0xFFFF; 199f7018c21STomi Valkeinen } 200f7018c21STomi Valkeinen 201f7018c21STomi Valkeinen return (u16) lv; 202f7018c21STomi Valkeinen } 203f7018c21STomi Valkeinen 204f7018c21STomi Valkeinen /* 205f7018c21STomi Valkeinen * This does LFSR conversion on the value that is to be written. 206f7018c21STomi Valkeinen * See LFSR explanation above for more detail. 207f7018c21STomi Valkeinen */ 208f7018c21STomi Valkeinen static char *dlfb_set_register_lfsr16(char *wrptr, u8 reg, u16 value) 209f7018c21STomi Valkeinen { 210f7018c21STomi Valkeinen return dlfb_set_register_16(wrptr, reg, dlfb_lfsr16(value)); 211f7018c21STomi Valkeinen } 212f7018c21STomi Valkeinen 213f7018c21STomi Valkeinen /* 214f7018c21STomi Valkeinen * This takes a standard fbdev screeninfo struct and all of its monitor mode 215f7018c21STomi Valkeinen * details and converts them into the DisplayLink equivalent register commands. 216f7018c21STomi Valkeinen */ 217f7018c21STomi Valkeinen static char *dlfb_set_vid_cmds(char *wrptr, struct fb_var_screeninfo *var) 218f7018c21STomi Valkeinen { 219f7018c21STomi Valkeinen u16 xds, yds; 220f7018c21STomi Valkeinen u16 xde, yde; 221f7018c21STomi Valkeinen u16 yec; 222f7018c21STomi Valkeinen 223f7018c21STomi Valkeinen /* x display start */ 224f7018c21STomi Valkeinen xds = var->left_margin + var->hsync_len; 225f7018c21STomi Valkeinen wrptr = dlfb_set_register_lfsr16(wrptr, 0x01, xds); 226f7018c21STomi Valkeinen /* x display end */ 227f7018c21STomi Valkeinen xde = xds + var->xres; 228f7018c21STomi Valkeinen wrptr = dlfb_set_register_lfsr16(wrptr, 0x03, xde); 229f7018c21STomi Valkeinen 230f7018c21STomi Valkeinen /* y display start */ 231f7018c21STomi Valkeinen yds = var->upper_margin + var->vsync_len; 232f7018c21STomi Valkeinen wrptr = dlfb_set_register_lfsr16(wrptr, 0x05, yds); 233f7018c21STomi Valkeinen /* y display end */ 234f7018c21STomi Valkeinen yde = yds + var->yres; 235f7018c21STomi Valkeinen wrptr = dlfb_set_register_lfsr16(wrptr, 0x07, yde); 236f7018c21STomi Valkeinen 237f7018c21STomi Valkeinen /* x end count is active + blanking - 1 */ 238f7018c21STomi Valkeinen wrptr = dlfb_set_register_lfsr16(wrptr, 0x09, 239f7018c21STomi Valkeinen xde + var->right_margin - 1); 240f7018c21STomi Valkeinen 241f7018c21STomi Valkeinen /* libdlo hardcodes hsync start to 1 */ 242f7018c21STomi Valkeinen wrptr = dlfb_set_register_lfsr16(wrptr, 0x0B, 1); 243f7018c21STomi Valkeinen 244f7018c21STomi Valkeinen /* hsync end is width of sync pulse + 1 */ 245f7018c21STomi Valkeinen wrptr = dlfb_set_register_lfsr16(wrptr, 0x0D, var->hsync_len + 1); 246f7018c21STomi Valkeinen 247f7018c21STomi Valkeinen /* hpixels is active pixels */ 248f7018c21STomi Valkeinen wrptr = dlfb_set_register_16(wrptr, 0x0F, var->xres); 249f7018c21STomi Valkeinen 250f7018c21STomi Valkeinen /* yendcount is vertical active + vertical blanking */ 251f7018c21STomi Valkeinen yec = var->yres + var->upper_margin + var->lower_margin + 252f7018c21STomi Valkeinen var->vsync_len; 253f7018c21STomi Valkeinen wrptr = dlfb_set_register_lfsr16(wrptr, 0x11, yec); 254f7018c21STomi Valkeinen 255f7018c21STomi Valkeinen /* libdlo hardcodes vsync start to 0 */ 256f7018c21STomi Valkeinen wrptr = dlfb_set_register_lfsr16(wrptr, 0x13, 0); 257f7018c21STomi Valkeinen 258f7018c21STomi Valkeinen /* vsync end is width of vsync pulse */ 259f7018c21STomi Valkeinen wrptr = dlfb_set_register_lfsr16(wrptr, 0x15, var->vsync_len); 260f7018c21STomi Valkeinen 261f7018c21STomi Valkeinen /* vpixels is active pixels */ 262f7018c21STomi Valkeinen wrptr = dlfb_set_register_16(wrptr, 0x17, var->yres); 263f7018c21STomi Valkeinen 264f7018c21STomi Valkeinen /* convert picoseconds to 5kHz multiple for pclk5k = x * 1E12/5k */ 265f7018c21STomi Valkeinen wrptr = dlfb_set_register_16be(wrptr, 0x1B, 266f7018c21STomi Valkeinen 200*1000*1000/var->pixclock); 267f7018c21STomi Valkeinen 268f7018c21STomi Valkeinen return wrptr; 269f7018c21STomi Valkeinen } 270f7018c21STomi Valkeinen 271f7018c21STomi Valkeinen /* 272f7018c21STomi Valkeinen * This takes a standard fbdev screeninfo struct that was fetched or prepared 273f7018c21STomi Valkeinen * and then generates the appropriate command sequence that then drives the 274f7018c21STomi Valkeinen * display controller. 275f7018c21STomi Valkeinen */ 2767ea46206SLadislav Michl static int dlfb_set_video_mode(struct dlfb_data *dlfb, 277f7018c21STomi Valkeinen struct fb_var_screeninfo *var) 278f7018c21STomi Valkeinen { 279f7018c21STomi Valkeinen char *buf; 280f7018c21STomi Valkeinen char *wrptr; 281f63cb8d7SAlexey Klimov int retval; 282f7018c21STomi Valkeinen int writesize; 283f7018c21STomi Valkeinen struct urb *urb; 284f7018c21STomi Valkeinen 2857ea46206SLadislav Michl if (!atomic_read(&dlfb->usb_active)) 286f7018c21STomi Valkeinen return -EPERM; 287f7018c21STomi Valkeinen 2887ea46206SLadislav Michl urb = dlfb_get_urb(dlfb); 289f7018c21STomi Valkeinen if (!urb) 290f7018c21STomi Valkeinen return -ENOMEM; 291f7018c21STomi Valkeinen 292f7018c21STomi Valkeinen buf = (char *) urb->transfer_buffer; 293f7018c21STomi Valkeinen 294f7018c21STomi Valkeinen /* 295f7018c21STomi Valkeinen * This first section has to do with setting the base address on the 296f7018c21STomi Valkeinen * controller * associated with the display. There are 2 base 297f7018c21STomi Valkeinen * pointers, currently, we only * use the 16 bpp segment. 298f7018c21STomi Valkeinen */ 299f7018c21STomi Valkeinen wrptr = dlfb_vidreg_lock(buf); 300f7018c21STomi Valkeinen wrptr = dlfb_set_color_depth(wrptr, 0x00); 301f7018c21STomi Valkeinen /* set base for 16bpp segment to 0 */ 302f7018c21STomi Valkeinen wrptr = dlfb_set_base16bpp(wrptr, 0); 303f7018c21STomi Valkeinen /* set base for 8bpp segment to end of fb */ 3047ea46206SLadislav Michl wrptr = dlfb_set_base8bpp(wrptr, dlfb->info->fix.smem_len); 305f7018c21STomi Valkeinen 306f7018c21STomi Valkeinen wrptr = dlfb_set_vid_cmds(wrptr, var); 307f7018c21STomi Valkeinen wrptr = dlfb_blanking(wrptr, FB_BLANK_UNBLANK); 308f7018c21STomi Valkeinen wrptr = dlfb_vidreg_unlock(wrptr); 309f7018c21STomi Valkeinen 310f7018c21STomi Valkeinen writesize = wrptr - buf; 311f7018c21STomi Valkeinen 3127ea46206SLadislav Michl retval = dlfb_submit_urb(dlfb, urb, writesize); 313f7018c21STomi Valkeinen 3147ea46206SLadislav Michl dlfb->blank_mode = FB_BLANK_UNBLANK; 315f7018c21STomi Valkeinen 316f7018c21STomi Valkeinen return retval; 317f7018c21STomi Valkeinen } 318f7018c21STomi Valkeinen 319f7018c21STomi Valkeinen static int dlfb_ops_mmap(struct fb_info *info, struct vm_area_struct *vma) 320f7018c21STomi Valkeinen { 321f7018c21STomi Valkeinen unsigned long start = vma->vm_start; 322f7018c21STomi Valkeinen unsigned long size = vma->vm_end - vma->vm_start; 323f7018c21STomi Valkeinen unsigned long offset = vma->vm_pgoff << PAGE_SHIFT; 324f7018c21STomi Valkeinen unsigned long page, pos; 325f7018c21STomi Valkeinen 326f7018c21STomi Valkeinen if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT)) 327f7018c21STomi Valkeinen return -EINVAL; 328f7018c21STomi Valkeinen if (size > info->fix.smem_len) 329f7018c21STomi Valkeinen return -EINVAL; 330f7018c21STomi Valkeinen if (offset > info->fix.smem_len - size) 331f7018c21STomi Valkeinen return -EINVAL; 332f7018c21STomi Valkeinen 333f7018c21STomi Valkeinen pos = (unsigned long)info->fix.smem_start + offset; 334f7018c21STomi Valkeinen 3355865889fSLadislav Michl dev_dbg(info->dev, "mmap() framebuffer addr:%lu size:%lu\n", 336f7018c21STomi Valkeinen pos, size); 337f7018c21STomi Valkeinen 338f7018c21STomi Valkeinen while (size > 0) { 339f7018c21STomi Valkeinen page = vmalloc_to_pfn((void *)pos); 340f7018c21STomi Valkeinen if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED)) 341f7018c21STomi Valkeinen return -EAGAIN; 342f7018c21STomi Valkeinen 343f7018c21STomi Valkeinen start += PAGE_SIZE; 344f7018c21STomi Valkeinen pos += PAGE_SIZE; 345f7018c21STomi Valkeinen if (size > PAGE_SIZE) 346f7018c21STomi Valkeinen size -= PAGE_SIZE; 347f7018c21STomi Valkeinen else 348f7018c21STomi Valkeinen size = 0; 349f7018c21STomi Valkeinen } 350f7018c21STomi Valkeinen 351f7018c21STomi Valkeinen return 0; 352f7018c21STomi Valkeinen } 353f7018c21STomi Valkeinen 354f7018c21STomi Valkeinen /* 355f7018c21STomi Valkeinen * Trims identical data from front and back of line 356f7018c21STomi Valkeinen * Sets new front buffer address and width 357f7018c21STomi Valkeinen * And returns byte count of identical pixels 358f7018c21STomi Valkeinen * Assumes CPU natural alignment (unsigned long) 359f7018c21STomi Valkeinen * for back and front buffer ptrs and width 360f7018c21STomi Valkeinen */ 361f7018c21STomi Valkeinen static int dlfb_trim_hline(const u8 *bback, const u8 **bfront, int *width_bytes) 362f7018c21STomi Valkeinen { 363f7018c21STomi Valkeinen int j, k; 364f7018c21STomi Valkeinen const unsigned long *back = (const unsigned long *) bback; 365f7018c21STomi Valkeinen const unsigned long *front = (const unsigned long *) *bfront; 366f7018c21STomi Valkeinen const int width = *width_bytes / sizeof(unsigned long); 367f7018c21STomi Valkeinen int identical = width; 368f7018c21STomi Valkeinen int start = width; 369f7018c21STomi Valkeinen int end = width; 370f7018c21STomi Valkeinen 371f7018c21STomi Valkeinen prefetch((void *) front); 372f7018c21STomi Valkeinen prefetch((void *) back); 373f7018c21STomi Valkeinen 374f7018c21STomi Valkeinen for (j = 0; j < width; j++) { 375f7018c21STomi Valkeinen if (back[j] != front[j]) { 376f7018c21STomi Valkeinen start = j; 377f7018c21STomi Valkeinen break; 378f7018c21STomi Valkeinen } 379f7018c21STomi Valkeinen } 380f7018c21STomi Valkeinen 381f7018c21STomi Valkeinen for (k = width - 1; k > j; k--) { 382f7018c21STomi Valkeinen if (back[k] != front[k]) { 383f7018c21STomi Valkeinen end = k+1; 384f7018c21STomi Valkeinen break; 385f7018c21STomi Valkeinen } 386f7018c21STomi Valkeinen } 387f7018c21STomi Valkeinen 388f7018c21STomi Valkeinen identical = start + (width - end); 389f7018c21STomi Valkeinen *bfront = (u8 *) &front[start]; 390f7018c21STomi Valkeinen *width_bytes = (end - start) * sizeof(unsigned long); 391f7018c21STomi Valkeinen 392f7018c21STomi Valkeinen return identical * sizeof(unsigned long); 393f7018c21STomi Valkeinen } 394f7018c21STomi Valkeinen 395f7018c21STomi Valkeinen /* 396f7018c21STomi Valkeinen * Render a command stream for an encoded horizontal line segment of pixels. 397f7018c21STomi Valkeinen * 398f7018c21STomi Valkeinen * A command buffer holds several commands. 399f7018c21STomi Valkeinen * It always begins with a fresh command header 400f7018c21STomi Valkeinen * (the protocol doesn't require this, but we enforce it to allow 401f7018c21STomi Valkeinen * multiple buffers to be potentially encoded and sent in parallel). 402f7018c21STomi Valkeinen * A single command encodes one contiguous horizontal line of pixels 403f7018c21STomi Valkeinen * 404f7018c21STomi Valkeinen * The function relies on the client to do all allocation, so that 405f7018c21STomi Valkeinen * rendering can be done directly to output buffers (e.g. USB URBs). 406f7018c21STomi Valkeinen * The function fills the supplied command buffer, providing information 407f7018c21STomi Valkeinen * on where it left off, so the client may call in again with additional 408f7018c21STomi Valkeinen * buffers if the line will take several buffers to complete. 409f7018c21STomi Valkeinen * 410f7018c21STomi Valkeinen * A single command can transmit a maximum of 256 pixels, 411f7018c21STomi Valkeinen * regardless of the compression ratio (protocol design limit). 412f7018c21STomi Valkeinen * To the hardware, 0 for a size byte means 256 413f7018c21STomi Valkeinen * 414f7018c21STomi Valkeinen * Rather than 256 pixel commands which are either rl or raw encoded, 415f7018c21STomi Valkeinen * the rlx command simply assumes alternating raw and rl spans within one cmd. 416f7018c21STomi Valkeinen * This has a slightly larger header overhead, but produces more even results. 417f7018c21STomi Valkeinen * It also processes all data (read and write) in a single pass. 418f7018c21STomi Valkeinen * Performance benchmarks of common cases show it having just slightly better 419f7018c21STomi Valkeinen * compression than 256 pixel raw or rle commands, with similar CPU consumpion. 420f7018c21STomi Valkeinen * But for very rl friendly data, will compress not quite as well. 421f7018c21STomi Valkeinen */ 422f7018c21STomi Valkeinen static void dlfb_compress_hline( 423f7018c21STomi Valkeinen const uint16_t **pixel_start_ptr, 424f7018c21STomi Valkeinen const uint16_t *const pixel_end, 425f7018c21STomi Valkeinen uint32_t *device_address_ptr, 426f7018c21STomi Valkeinen uint8_t **command_buffer_ptr, 427f7018c21STomi Valkeinen const uint8_t *const cmd_buffer_end) 428f7018c21STomi Valkeinen { 429f7018c21STomi Valkeinen const uint16_t *pixel = *pixel_start_ptr; 430f7018c21STomi Valkeinen uint32_t dev_addr = *device_address_ptr; 431f7018c21STomi Valkeinen uint8_t *cmd = *command_buffer_ptr; 432f7018c21STomi Valkeinen 433f7018c21STomi Valkeinen while ((pixel_end > pixel) && 434f7018c21STomi Valkeinen (cmd_buffer_end - MIN_RLX_CMD_BYTES > cmd)) { 435f7018c21STomi Valkeinen uint8_t *raw_pixels_count_byte = NULL; 436f7018c21STomi Valkeinen uint8_t *cmd_pixels_count_byte = NULL; 437f7018c21STomi Valkeinen const uint16_t *raw_pixel_start = NULL; 438f7018c21STomi Valkeinen const uint16_t *cmd_pixel_start, *cmd_pixel_end = NULL; 439f7018c21STomi Valkeinen 440f7018c21STomi Valkeinen prefetchw((void *) cmd); /* pull in one cache line at least */ 441f7018c21STomi Valkeinen 442f7018c21STomi Valkeinen *cmd++ = 0xAF; 443f7018c21STomi Valkeinen *cmd++ = 0x6B; 444115e7759SLadislav Michl *cmd++ = dev_addr >> 16; 445115e7759SLadislav Michl *cmd++ = dev_addr >> 8; 446115e7759SLadislav Michl *cmd++ = dev_addr; 447f7018c21STomi Valkeinen 448f7018c21STomi Valkeinen cmd_pixels_count_byte = cmd++; /* we'll know this later */ 449f7018c21STomi Valkeinen cmd_pixel_start = pixel; 450f7018c21STomi Valkeinen 451f7018c21STomi Valkeinen raw_pixels_count_byte = cmd++; /* we'll know this later */ 452f7018c21STomi Valkeinen raw_pixel_start = pixel; 453f7018c21STomi Valkeinen 4544e705e17SMikulas Patocka cmd_pixel_end = pixel + min3(MAX_CMD_PIXELS + 1UL, 4554e705e17SMikulas Patocka (unsigned long)(pixel_end - pixel), 4564e705e17SMikulas Patocka (unsigned long)(cmd_buffer_end - 1 - cmd) / BPP); 457f7018c21STomi Valkeinen 4584e705e17SMikulas Patocka prefetch_range((void *) pixel, (u8 *)cmd_pixel_end - (u8 *)pixel); 459f7018c21STomi Valkeinen 460f7018c21STomi Valkeinen while (pixel < cmd_pixel_end) { 461f7018c21STomi Valkeinen const uint16_t * const repeating_pixel = pixel; 462f7018c21STomi Valkeinen 4634e705e17SMikulas Patocka put_unaligned_be16(*pixel, cmd); 4644e705e17SMikulas Patocka cmd += 2; 465f7018c21STomi Valkeinen pixel++; 466f7018c21STomi Valkeinen 467f7018c21STomi Valkeinen if (unlikely((pixel < cmd_pixel_end) && 468f7018c21STomi Valkeinen (*pixel == *repeating_pixel))) { 469f7018c21STomi Valkeinen /* go back and fill in raw pixel count */ 470f7018c21STomi Valkeinen *raw_pixels_count_byte = ((repeating_pixel - 471f7018c21STomi Valkeinen raw_pixel_start) + 1) & 0xFF; 472f7018c21STomi Valkeinen 473f7018c21STomi Valkeinen while ((pixel < cmd_pixel_end) 474f7018c21STomi Valkeinen && (*pixel == *repeating_pixel)) { 475f7018c21STomi Valkeinen pixel++; 476f7018c21STomi Valkeinen } 477f7018c21STomi Valkeinen 478f7018c21STomi Valkeinen /* immediately after raw data is repeat byte */ 479f7018c21STomi Valkeinen *cmd++ = ((pixel - repeating_pixel) - 1) & 0xFF; 480f7018c21STomi Valkeinen 481f7018c21STomi Valkeinen /* Then start another raw pixel span */ 482f7018c21STomi Valkeinen raw_pixel_start = pixel; 483f7018c21STomi Valkeinen raw_pixels_count_byte = cmd++; 484f7018c21STomi Valkeinen } 485f7018c21STomi Valkeinen } 486f7018c21STomi Valkeinen 487f7018c21STomi Valkeinen if (pixel > raw_pixel_start) { 488f7018c21STomi Valkeinen /* finalize last RAW span */ 489f7018c21STomi Valkeinen *raw_pixels_count_byte = (pixel-raw_pixel_start) & 0xFF; 4904e705e17SMikulas Patocka } else { 4914e705e17SMikulas Patocka /* undo unused byte */ 4924e705e17SMikulas Patocka cmd--; 493f7018c21STomi Valkeinen } 494f7018c21STomi Valkeinen 495f7018c21STomi Valkeinen *cmd_pixels_count_byte = (pixel - cmd_pixel_start) & 0xFF; 4964e705e17SMikulas Patocka dev_addr += (u8 *)pixel - (u8 *)cmd_pixel_start; 497f7018c21STomi Valkeinen } 498f7018c21STomi Valkeinen 4994e705e17SMikulas Patocka if (cmd_buffer_end - MIN_RLX_CMD_BYTES <= cmd) { 500f7018c21STomi Valkeinen /* Fill leftover bytes with no-ops */ 501f7018c21STomi Valkeinen if (cmd_buffer_end > cmd) 502f7018c21STomi Valkeinen memset(cmd, 0xAF, cmd_buffer_end - cmd); 503f7018c21STomi Valkeinen cmd = (uint8_t *) cmd_buffer_end; 504f7018c21STomi Valkeinen } 505f7018c21STomi Valkeinen 506f7018c21STomi Valkeinen *command_buffer_ptr = cmd; 507f7018c21STomi Valkeinen *pixel_start_ptr = pixel; 508f7018c21STomi Valkeinen *device_address_ptr = dev_addr; 509f7018c21STomi Valkeinen } 510f7018c21STomi Valkeinen 511f7018c21STomi Valkeinen /* 512f7018c21STomi Valkeinen * There are 3 copies of every pixel: The front buffer that the fbdev 513f7018c21STomi Valkeinen * client renders to, the actual framebuffer across the USB bus in hardware 514f7018c21STomi Valkeinen * (that we can only write to, slowly, and can never read), and (optionally) 515f7018c21STomi Valkeinen * our shadow copy that tracks what's been sent to that hardware buffer. 516f7018c21STomi Valkeinen */ 5177ea46206SLadislav Michl static int dlfb_render_hline(struct dlfb_data *dlfb, struct urb **urb_ptr, 518f7018c21STomi Valkeinen const char *front, char **urb_buf_ptr, 519f7018c21STomi Valkeinen u32 byte_offset, u32 byte_width, 520f7018c21STomi Valkeinen int *ident_ptr, int *sent_ptr) 521f7018c21STomi Valkeinen { 522f7018c21STomi Valkeinen const u8 *line_start, *line_end, *next_pixel; 5237ea46206SLadislav Michl u32 dev_addr = dlfb->base16 + byte_offset; 524f7018c21STomi Valkeinen struct urb *urb = *urb_ptr; 525f7018c21STomi Valkeinen u8 *cmd = *urb_buf_ptr; 526f7018c21STomi Valkeinen u8 *cmd_end = (u8 *) urb->transfer_buffer + urb->transfer_buffer_length; 527f7018c21STomi Valkeinen 528f7018c21STomi Valkeinen line_start = (u8 *) (front + byte_offset); 529f7018c21STomi Valkeinen next_pixel = line_start; 530f7018c21STomi Valkeinen line_end = next_pixel + byte_width; 531f7018c21STomi Valkeinen 5327ea46206SLadislav Michl if (dlfb->backing_buffer) { 533f7018c21STomi Valkeinen int offset; 5347ea46206SLadislav Michl const u8 *back_start = (u8 *) (dlfb->backing_buffer 535f7018c21STomi Valkeinen + byte_offset); 536f7018c21STomi Valkeinen 537f7018c21STomi Valkeinen *ident_ptr += dlfb_trim_hline(back_start, &next_pixel, 538f7018c21STomi Valkeinen &byte_width); 539f7018c21STomi Valkeinen 540f7018c21STomi Valkeinen offset = next_pixel - line_start; 541f7018c21STomi Valkeinen line_end = next_pixel + byte_width; 542f7018c21STomi Valkeinen dev_addr += offset; 543f7018c21STomi Valkeinen back_start += offset; 544f7018c21STomi Valkeinen line_start += offset; 545f7018c21STomi Valkeinen 546f7018c21STomi Valkeinen memcpy((char *)back_start, (char *) line_start, 547f7018c21STomi Valkeinen byte_width); 548f7018c21STomi Valkeinen } 549f7018c21STomi Valkeinen 550f7018c21STomi Valkeinen while (next_pixel < line_end) { 551f7018c21STomi Valkeinen 552f7018c21STomi Valkeinen dlfb_compress_hline((const uint16_t **) &next_pixel, 553f7018c21STomi Valkeinen (const uint16_t *) line_end, &dev_addr, 554f7018c21STomi Valkeinen (u8 **) &cmd, (u8 *) cmd_end); 555f7018c21STomi Valkeinen 556f7018c21STomi Valkeinen if (cmd >= cmd_end) { 557f7018c21STomi Valkeinen int len = cmd - (u8 *) urb->transfer_buffer; 5587ea46206SLadislav Michl if (dlfb_submit_urb(dlfb, urb, len)) 559f7018c21STomi Valkeinen return 1; /* lost pixels is set */ 560f7018c21STomi Valkeinen *sent_ptr += len; 5617ea46206SLadislav Michl urb = dlfb_get_urb(dlfb); 562f7018c21STomi Valkeinen if (!urb) 563f7018c21STomi Valkeinen return 1; /* lost_pixels is set */ 564f7018c21STomi Valkeinen *urb_ptr = urb; 565f7018c21STomi Valkeinen cmd = urb->transfer_buffer; 566f7018c21STomi Valkeinen cmd_end = &cmd[urb->transfer_buffer_length]; 567f7018c21STomi Valkeinen } 568f7018c21STomi Valkeinen } 569f7018c21STomi Valkeinen 570f7018c21STomi Valkeinen *urb_buf_ptr = cmd; 571f7018c21STomi Valkeinen 572f7018c21STomi Valkeinen return 0; 573f7018c21STomi Valkeinen } 574f7018c21STomi Valkeinen 5757ea46206SLadislav Michl static int dlfb_handle_damage(struct dlfb_data *dlfb, int x, int y, 576f7018c21STomi Valkeinen int width, int height, char *data) 577f7018c21STomi Valkeinen { 578f7018c21STomi Valkeinen int i, ret; 579f7018c21STomi Valkeinen char *cmd; 580f7018c21STomi Valkeinen cycles_t start_cycles, end_cycles; 581f7018c21STomi Valkeinen int bytes_sent = 0; 582f7018c21STomi Valkeinen int bytes_identical = 0; 583f7018c21STomi Valkeinen struct urb *urb; 584f7018c21STomi Valkeinen int aligned_x; 585f7018c21STomi Valkeinen 586f7018c21STomi Valkeinen start_cycles = get_cycles(); 587f7018c21STomi Valkeinen 588f7018c21STomi Valkeinen aligned_x = DL_ALIGN_DOWN(x, sizeof(unsigned long)); 589f7018c21STomi Valkeinen width = DL_ALIGN_UP(width + (x-aligned_x), sizeof(unsigned long)); 590f7018c21STomi Valkeinen x = aligned_x; 591f7018c21STomi Valkeinen 592f7018c21STomi Valkeinen if ((width <= 0) || 5937ea46206SLadislav Michl (x + width > dlfb->info->var.xres) || 5947ea46206SLadislav Michl (y + height > dlfb->info->var.yres)) 595f7018c21STomi Valkeinen return -EINVAL; 596f7018c21STomi Valkeinen 5977ea46206SLadislav Michl if (!atomic_read(&dlfb->usb_active)) 598f7018c21STomi Valkeinen return 0; 599f7018c21STomi Valkeinen 6007ea46206SLadislav Michl urb = dlfb_get_urb(dlfb); 601f7018c21STomi Valkeinen if (!urb) 602f7018c21STomi Valkeinen return 0; 603f7018c21STomi Valkeinen cmd = urb->transfer_buffer; 604f7018c21STomi Valkeinen 605f7018c21STomi Valkeinen for (i = y; i < y + height ; i++) { 6067ea46206SLadislav Michl const int line_offset = dlfb->info->fix.line_length * i; 607f7018c21STomi Valkeinen const int byte_offset = line_offset + (x * BPP); 608f7018c21STomi Valkeinen 6097ea46206SLadislav Michl if (dlfb_render_hline(dlfb, &urb, 6107ea46206SLadislav Michl (char *) dlfb->info->fix.smem_start, 611f7018c21STomi Valkeinen &cmd, byte_offset, width * BPP, 612f7018c21STomi Valkeinen &bytes_identical, &bytes_sent)) 613f7018c21STomi Valkeinen goto error; 614f7018c21STomi Valkeinen } 615f7018c21STomi Valkeinen 616f7018c21STomi Valkeinen if (cmd > (char *) urb->transfer_buffer) { 6174e705e17SMikulas Patocka int len; 6184e705e17SMikulas Patocka if (cmd < (char *) urb->transfer_buffer + urb->transfer_buffer_length) 6194e705e17SMikulas Patocka *cmd++ = 0xAF; 620f7018c21STomi Valkeinen /* Send partial buffer remaining before exiting */ 6214e705e17SMikulas Patocka len = cmd - (char *) urb->transfer_buffer; 6227ea46206SLadislav Michl ret = dlfb_submit_urb(dlfb, urb, len); 623f7018c21STomi Valkeinen bytes_sent += len; 624f7018c21STomi Valkeinen } else 625f7018c21STomi Valkeinen dlfb_urb_completion(urb); 626f7018c21STomi Valkeinen 627f7018c21STomi Valkeinen error: 6287ea46206SLadislav Michl atomic_add(bytes_sent, &dlfb->bytes_sent); 6297ea46206SLadislav Michl atomic_add(bytes_identical, &dlfb->bytes_identical); 6307ea46206SLadislav Michl atomic_add(width*height*2, &dlfb->bytes_rendered); 631f7018c21STomi Valkeinen end_cycles = get_cycles(); 632f7018c21STomi Valkeinen atomic_add(((unsigned int) ((end_cycles - start_cycles) 633f7018c21STomi Valkeinen >> 10)), /* Kcycles */ 6347ea46206SLadislav Michl &dlfb->cpu_kcycles_used); 635f7018c21STomi Valkeinen 636f7018c21STomi Valkeinen return 0; 637f7018c21STomi Valkeinen } 638f7018c21STomi Valkeinen 639f7018c21STomi Valkeinen /* 640f7018c21STomi Valkeinen * Path triggered by usermode clients who write to filesystem 641f7018c21STomi Valkeinen * e.g. cat filename > /dev/fb1 642f7018c21STomi Valkeinen * Not used by X Windows or text-mode console. But useful for testing. 643f7018c21STomi Valkeinen * Slow because of extra copy and we must assume all pixels dirty. 644f7018c21STomi Valkeinen */ 645f7018c21STomi Valkeinen static ssize_t dlfb_ops_write(struct fb_info *info, const char __user *buf, 646f7018c21STomi Valkeinen size_t count, loff_t *ppos) 647f7018c21STomi Valkeinen { 648f7018c21STomi Valkeinen ssize_t result; 6497ea46206SLadislav Michl struct dlfb_data *dlfb = info->par; 650f7018c21STomi Valkeinen u32 offset = (u32) *ppos; 651f7018c21STomi Valkeinen 652f7018c21STomi Valkeinen result = fb_sys_write(info, buf, count, ppos); 653f7018c21STomi Valkeinen 654f7018c21STomi Valkeinen if (result > 0) { 655f7018c21STomi Valkeinen int start = max((int)(offset / info->fix.line_length), 0); 656f7018c21STomi Valkeinen int lines = min((u32)((result / info->fix.line_length) + 1), 657f7018c21STomi Valkeinen (u32)info->var.yres); 658f7018c21STomi Valkeinen 6597ea46206SLadislav Michl dlfb_handle_damage(dlfb, 0, start, info->var.xres, 660f7018c21STomi Valkeinen lines, info->screen_base); 661f7018c21STomi Valkeinen } 662f7018c21STomi Valkeinen 663f7018c21STomi Valkeinen return result; 664f7018c21STomi Valkeinen } 665f7018c21STomi Valkeinen 666f7018c21STomi Valkeinen /* hardware has native COPY command (see libdlo), but not worth it for fbcon */ 667f7018c21STomi Valkeinen static void dlfb_ops_copyarea(struct fb_info *info, 668f7018c21STomi Valkeinen const struct fb_copyarea *area) 669f7018c21STomi Valkeinen { 670f7018c21STomi Valkeinen 6717ea46206SLadislav Michl struct dlfb_data *dlfb = info->par; 672f7018c21STomi Valkeinen 673f7018c21STomi Valkeinen sys_copyarea(info, area); 674f7018c21STomi Valkeinen 6757ea46206SLadislav Michl dlfb_handle_damage(dlfb, area->dx, area->dy, 676f7018c21STomi Valkeinen area->width, area->height, info->screen_base); 677f7018c21STomi Valkeinen } 678f7018c21STomi Valkeinen 679f7018c21STomi Valkeinen static void dlfb_ops_imageblit(struct fb_info *info, 680f7018c21STomi Valkeinen const struct fb_image *image) 681f7018c21STomi Valkeinen { 6827ea46206SLadislav Michl struct dlfb_data *dlfb = info->par; 683f7018c21STomi Valkeinen 684f7018c21STomi Valkeinen sys_imageblit(info, image); 685f7018c21STomi Valkeinen 6867ea46206SLadislav Michl dlfb_handle_damage(dlfb, image->dx, image->dy, 687f7018c21STomi Valkeinen image->width, image->height, info->screen_base); 688f7018c21STomi Valkeinen } 689f7018c21STomi Valkeinen 690f7018c21STomi Valkeinen static void dlfb_ops_fillrect(struct fb_info *info, 691f7018c21STomi Valkeinen const struct fb_fillrect *rect) 692f7018c21STomi Valkeinen { 6937ea46206SLadislav Michl struct dlfb_data *dlfb = info->par; 694f7018c21STomi Valkeinen 695f7018c21STomi Valkeinen sys_fillrect(info, rect); 696f7018c21STomi Valkeinen 6977ea46206SLadislav Michl dlfb_handle_damage(dlfb, rect->dx, rect->dy, rect->width, 698f7018c21STomi Valkeinen rect->height, info->screen_base); 699f7018c21STomi Valkeinen } 700f7018c21STomi Valkeinen 701f7018c21STomi Valkeinen /* 702f7018c21STomi Valkeinen * NOTE: fb_defio.c is holding info->fbdefio.mutex 703f7018c21STomi Valkeinen * Touching ANY framebuffer memory that triggers a page fault 704f7018c21STomi Valkeinen * in fb_defio will cause a deadlock, when it also tries to 705f7018c21STomi Valkeinen * grab the same mutex. 706f7018c21STomi Valkeinen */ 707f7018c21STomi Valkeinen static void dlfb_dpy_deferred_io(struct fb_info *info, 708f7018c21STomi Valkeinen struct list_head *pagelist) 709f7018c21STomi Valkeinen { 710f7018c21STomi Valkeinen struct page *cur; 711f7018c21STomi Valkeinen struct fb_deferred_io *fbdefio = info->fbdefio; 7127ea46206SLadislav Michl struct dlfb_data *dlfb = info->par; 713f7018c21STomi Valkeinen struct urb *urb; 714f7018c21STomi Valkeinen char *cmd; 715f7018c21STomi Valkeinen cycles_t start_cycles, end_cycles; 716f7018c21STomi Valkeinen int bytes_sent = 0; 717f7018c21STomi Valkeinen int bytes_identical = 0; 718f7018c21STomi Valkeinen int bytes_rendered = 0; 719f7018c21STomi Valkeinen 720f7018c21STomi Valkeinen if (!fb_defio) 721f7018c21STomi Valkeinen return; 722f7018c21STomi Valkeinen 7237ea46206SLadislav Michl if (!atomic_read(&dlfb->usb_active)) 724f7018c21STomi Valkeinen return; 725f7018c21STomi Valkeinen 726f7018c21STomi Valkeinen start_cycles = get_cycles(); 727f7018c21STomi Valkeinen 7287ea46206SLadislav Michl urb = dlfb_get_urb(dlfb); 729f7018c21STomi Valkeinen if (!urb) 730f7018c21STomi Valkeinen return; 731f7018c21STomi Valkeinen 732f7018c21STomi Valkeinen cmd = urb->transfer_buffer; 733f7018c21STomi Valkeinen 734f7018c21STomi Valkeinen /* walk the written page list and render each to device */ 735f7018c21STomi Valkeinen list_for_each_entry(cur, &fbdefio->pagelist, lru) { 736f7018c21STomi Valkeinen 7377ea46206SLadislav Michl if (dlfb_render_hline(dlfb, &urb, (char *) info->fix.smem_start, 738f7018c21STomi Valkeinen &cmd, cur->index << PAGE_SHIFT, 739f7018c21STomi Valkeinen PAGE_SIZE, &bytes_identical, &bytes_sent)) 740f7018c21STomi Valkeinen goto error; 741f7018c21STomi Valkeinen bytes_rendered += PAGE_SIZE; 742f7018c21STomi Valkeinen } 743f7018c21STomi Valkeinen 744f7018c21STomi Valkeinen if (cmd > (char *) urb->transfer_buffer) { 7454e705e17SMikulas Patocka int len; 7464e705e17SMikulas Patocka if (cmd < (char *) urb->transfer_buffer + urb->transfer_buffer_length) 7474e705e17SMikulas Patocka *cmd++ = 0xAF; 748f7018c21STomi Valkeinen /* Send partial buffer remaining before exiting */ 7494e705e17SMikulas Patocka len = cmd - (char *) urb->transfer_buffer; 7507ea46206SLadislav Michl dlfb_submit_urb(dlfb, urb, len); 751f7018c21STomi Valkeinen bytes_sent += len; 752f7018c21STomi Valkeinen } else 753f7018c21STomi Valkeinen dlfb_urb_completion(urb); 754f7018c21STomi Valkeinen 755f7018c21STomi Valkeinen error: 7567ea46206SLadislav Michl atomic_add(bytes_sent, &dlfb->bytes_sent); 7577ea46206SLadislav Michl atomic_add(bytes_identical, &dlfb->bytes_identical); 7587ea46206SLadislav Michl atomic_add(bytes_rendered, &dlfb->bytes_rendered); 759f7018c21STomi Valkeinen end_cycles = get_cycles(); 760f7018c21STomi Valkeinen atomic_add(((unsigned int) ((end_cycles - start_cycles) 761f7018c21STomi Valkeinen >> 10)), /* Kcycles */ 7627ea46206SLadislav Michl &dlfb->cpu_kcycles_used); 763f7018c21STomi Valkeinen } 764f7018c21STomi Valkeinen 7657ea46206SLadislav Michl static int dlfb_get_edid(struct dlfb_data *dlfb, char *edid, int len) 766f7018c21STomi Valkeinen { 7675865889fSLadislav Michl int i, ret; 768f7018c21STomi Valkeinen char *rbuf; 769f7018c21STomi Valkeinen 770f7018c21STomi Valkeinen rbuf = kmalloc(2, GFP_KERNEL); 771f7018c21STomi Valkeinen if (!rbuf) 772f7018c21STomi Valkeinen return 0; 773f7018c21STomi Valkeinen 774f7018c21STomi Valkeinen for (i = 0; i < len; i++) { 7757ea46206SLadislav Michl ret = usb_control_msg(dlfb->udev, 7767ea46206SLadislav Michl usb_rcvctrlpipe(dlfb->udev, 0), 0x02, 777c9876947SLadislav Michl (0x80 | (0x02 << 5)), i << 8, 0xA1, 778c9876947SLadislav Michl rbuf, 2, USB_CTRL_GET_TIMEOUT); 779c9876947SLadislav Michl if (ret < 2) { 7805865889fSLadislav Michl dev_err(&dlfb->udev->dev, 7815865889fSLadislav Michl "Read EDID byte %d failed: %d\n", i, ret); 782f7018c21STomi Valkeinen i--; 783f7018c21STomi Valkeinen break; 784f7018c21STomi Valkeinen } 785f7018c21STomi Valkeinen edid[i] = rbuf[1]; 786f7018c21STomi Valkeinen } 787f7018c21STomi Valkeinen 788f7018c21STomi Valkeinen kfree(rbuf); 789f7018c21STomi Valkeinen 790f7018c21STomi Valkeinen return i; 791f7018c21STomi Valkeinen } 792f7018c21STomi Valkeinen 793f7018c21STomi Valkeinen static int dlfb_ops_ioctl(struct fb_info *info, unsigned int cmd, 794f7018c21STomi Valkeinen unsigned long arg) 795f7018c21STomi Valkeinen { 796f7018c21STomi Valkeinen 7977ea46206SLadislav Michl struct dlfb_data *dlfb = info->par; 798f7018c21STomi Valkeinen 7997ea46206SLadislav Michl if (!atomic_read(&dlfb->usb_active)) 800f7018c21STomi Valkeinen return 0; 801f7018c21STomi Valkeinen 802f7018c21STomi Valkeinen /* TODO: Update X server to get this from sysfs instead */ 803f7018c21STomi Valkeinen if (cmd == DLFB_IOCTL_RETURN_EDID) { 804f7018c21STomi Valkeinen void __user *edid = (void __user *)arg; 8057ea46206SLadislav Michl if (copy_to_user(edid, dlfb->edid, dlfb->edid_size)) 806f7018c21STomi Valkeinen return -EFAULT; 807f7018c21STomi Valkeinen return 0; 808f7018c21STomi Valkeinen } 809f7018c21STomi Valkeinen 810f7018c21STomi Valkeinen /* TODO: Help propose a standard fb.h ioctl to report mmap damage */ 811f7018c21STomi Valkeinen if (cmd == DLFB_IOCTL_REPORT_DAMAGE) { 812f7018c21STomi Valkeinen struct dloarea area; 813f7018c21STomi Valkeinen 814f7018c21STomi Valkeinen if (copy_from_user(&area, (void __user *)arg, 815f7018c21STomi Valkeinen sizeof(struct dloarea))) 816f7018c21STomi Valkeinen return -EFAULT; 817f7018c21STomi Valkeinen 818f7018c21STomi Valkeinen /* 819f7018c21STomi Valkeinen * If we have a damage-aware client, turn fb_defio "off" 820f7018c21STomi Valkeinen * To avoid perf imact of unnecessary page fault handling. 821f7018c21STomi Valkeinen * Done by resetting the delay for this fb_info to a very 822f7018c21STomi Valkeinen * long period. Pages will become writable and stay that way. 823f7018c21STomi Valkeinen * Reset to normal value when all clients have closed this fb. 824f7018c21STomi Valkeinen */ 825f7018c21STomi Valkeinen if (info->fbdefio) 826f7018c21STomi Valkeinen info->fbdefio->delay = DL_DEFIO_WRITE_DISABLE; 827f7018c21STomi Valkeinen 828f7018c21STomi Valkeinen if (area.x < 0) 829f7018c21STomi Valkeinen area.x = 0; 830f7018c21STomi Valkeinen 831f7018c21STomi Valkeinen if (area.x > info->var.xres) 832f7018c21STomi Valkeinen area.x = info->var.xres; 833f7018c21STomi Valkeinen 834f7018c21STomi Valkeinen if (area.y < 0) 835f7018c21STomi Valkeinen area.y = 0; 836f7018c21STomi Valkeinen 837f7018c21STomi Valkeinen if (area.y > info->var.yres) 838f7018c21STomi Valkeinen area.y = info->var.yres; 839f7018c21STomi Valkeinen 8407ea46206SLadislav Michl dlfb_handle_damage(dlfb, area.x, area.y, area.w, area.h, 841f7018c21STomi Valkeinen info->screen_base); 842f7018c21STomi Valkeinen } 843f7018c21STomi Valkeinen 844f7018c21STomi Valkeinen return 0; 845f7018c21STomi Valkeinen } 846f7018c21STomi Valkeinen 847f7018c21STomi Valkeinen /* taken from vesafb */ 848f7018c21STomi Valkeinen static int 849f7018c21STomi Valkeinen dlfb_ops_setcolreg(unsigned regno, unsigned red, unsigned green, 850f7018c21STomi Valkeinen unsigned blue, unsigned transp, struct fb_info *info) 851f7018c21STomi Valkeinen { 852f7018c21STomi Valkeinen int err = 0; 853f7018c21STomi Valkeinen 854f7018c21STomi Valkeinen if (regno >= info->cmap.len) 855f7018c21STomi Valkeinen return 1; 856f7018c21STomi Valkeinen 857f7018c21STomi Valkeinen if (regno < 16) { 858f7018c21STomi Valkeinen if (info->var.red.offset == 10) { 859f7018c21STomi Valkeinen /* 1:5:5:5 */ 860f7018c21STomi Valkeinen ((u32 *) (info->pseudo_palette))[regno] = 861f7018c21STomi Valkeinen ((red & 0xf800) >> 1) | 862f7018c21STomi Valkeinen ((green & 0xf800) >> 6) | ((blue & 0xf800) >> 11); 863f7018c21STomi Valkeinen } else { 864f7018c21STomi Valkeinen /* 0:5:6:5 */ 865f7018c21STomi Valkeinen ((u32 *) (info->pseudo_palette))[regno] = 866f7018c21STomi Valkeinen ((red & 0xf800)) | 867f7018c21STomi Valkeinen ((green & 0xfc00) >> 5) | ((blue & 0xf800) >> 11); 868f7018c21STomi Valkeinen } 869f7018c21STomi Valkeinen } 870f7018c21STomi Valkeinen 871f7018c21STomi Valkeinen return err; 872f7018c21STomi Valkeinen } 873f7018c21STomi Valkeinen 874f7018c21STomi Valkeinen /* 875f7018c21STomi Valkeinen * It's common for several clients to have framebuffer open simultaneously. 876f7018c21STomi Valkeinen * e.g. both fbcon and X. Makes things interesting. 877f7018c21STomi Valkeinen * Assumes caller is holding info->lock (for open and release at least) 878f7018c21STomi Valkeinen */ 879f7018c21STomi Valkeinen static int dlfb_ops_open(struct fb_info *info, int user) 880f7018c21STomi Valkeinen { 8817ea46206SLadislav Michl struct dlfb_data *dlfb = info->par; 882f7018c21STomi Valkeinen 883f7018c21STomi Valkeinen /* 884f7018c21STomi Valkeinen * fbcon aggressively connects to first framebuffer it finds, 885f7018c21STomi Valkeinen * preventing other clients (X) from working properly. Usually 886f7018c21STomi Valkeinen * not what the user wants. Fail by default with option to enable. 887f7018c21STomi Valkeinen */ 888f7018c21STomi Valkeinen if ((user == 0) && (!console)) 889f7018c21STomi Valkeinen return -EBUSY; 890f7018c21STomi Valkeinen 891f7018c21STomi Valkeinen /* If the USB device is gone, we don't accept new opens */ 8927ea46206SLadislav Michl if (dlfb->virtualized) 893f7018c21STomi Valkeinen return -ENODEV; 894f7018c21STomi Valkeinen 8957ea46206SLadislav Michl dlfb->fb_count++; 896f7018c21STomi Valkeinen 8977ea46206SLadislav Michl kref_get(&dlfb->kref); 898f7018c21STomi Valkeinen 899f7018c21STomi Valkeinen if (fb_defio && (info->fbdefio == NULL)) { 900f7018c21STomi Valkeinen /* enable defio at last moment if not disabled by client */ 901f7018c21STomi Valkeinen 902f7018c21STomi Valkeinen struct fb_deferred_io *fbdefio; 903f7018c21STomi Valkeinen 904f7018c21STomi Valkeinen fbdefio = kzalloc(sizeof(struct fb_deferred_io), GFP_KERNEL); 905f7018c21STomi Valkeinen 906f7018c21STomi Valkeinen if (fbdefio) { 907f7018c21STomi Valkeinen fbdefio->delay = DL_DEFIO_WRITE_DELAY; 908f7018c21STomi Valkeinen fbdefio->deferred_io = dlfb_dpy_deferred_io; 909f7018c21STomi Valkeinen } 910f7018c21STomi Valkeinen 911f7018c21STomi Valkeinen info->fbdefio = fbdefio; 912f7018c21STomi Valkeinen fb_deferred_io_init(info); 913f7018c21STomi Valkeinen } 914f7018c21STomi Valkeinen 9155865889fSLadislav Michl dev_dbg(info->dev, "open, user=%d fb_info=%p count=%d\n", 9165865889fSLadislav Michl user, info, dlfb->fb_count); 917f7018c21STomi Valkeinen 918f7018c21STomi Valkeinen return 0; 919f7018c21STomi Valkeinen } 920f7018c21STomi Valkeinen 921f7018c21STomi Valkeinen /* 922f7018c21STomi Valkeinen * Called when all client interfaces to start transactions have been disabled, 923f7018c21STomi Valkeinen * and all references to our device instance (dlfb_data) are released. 924f7018c21STomi Valkeinen * Every transaction must have a reference, so we know are fully spun down 925f7018c21STomi Valkeinen */ 926f7018c21STomi Valkeinen static void dlfb_free(struct kref *kref) 927f7018c21STomi Valkeinen { 9287ea46206SLadislav Michl struct dlfb_data *dlfb = container_of(kref, struct dlfb_data, kref); 929f7018c21STomi Valkeinen 9307ea46206SLadislav Michl vfree(dlfb->backing_buffer); 9317ea46206SLadislav Michl kfree(dlfb->edid); 9327ea46206SLadislav Michl kfree(dlfb); 933f7018c21STomi Valkeinen } 934f7018c21STomi Valkeinen 9357ea46206SLadislav Michl static void dlfb_free_framebuffer(struct dlfb_data *dlfb) 936f7018c21STomi Valkeinen { 9377ea46206SLadislav Michl struct fb_info *info = dlfb->info; 938f7018c21STomi Valkeinen 939f7018c21STomi Valkeinen if (info) { 940f7018c21STomi Valkeinen unregister_framebuffer(info); 941f7018c21STomi Valkeinen 942f7018c21STomi Valkeinen if (info->cmap.len != 0) 943f7018c21STomi Valkeinen fb_dealloc_cmap(&info->cmap); 944f7018c21STomi Valkeinen if (info->monspecs.modedb) 945f7018c21STomi Valkeinen fb_destroy_modedb(info->monspecs.modedb); 946f7018c21STomi Valkeinen vfree(info->screen_base); 947f7018c21STomi Valkeinen 948f7018c21STomi Valkeinen fb_destroy_modelist(&info->modelist); 949f7018c21STomi Valkeinen 9507ea46206SLadislav Michl dlfb->info = NULL; 951f7018c21STomi Valkeinen 952f7018c21STomi Valkeinen /* Assume info structure is freed after this point */ 953f7018c21STomi Valkeinen framebuffer_release(info); 954f7018c21STomi Valkeinen } 955f7018c21STomi Valkeinen 956f7018c21STomi Valkeinen /* ref taken in probe() as part of registering framebfufer */ 9577ea46206SLadislav Michl kref_put(&dlfb->kref, dlfb_free); 958f7018c21STomi Valkeinen } 959f7018c21STomi Valkeinen 960f7018c21STomi Valkeinen static void dlfb_free_framebuffer_work(struct work_struct *work) 961f7018c21STomi Valkeinen { 9627ea46206SLadislav Michl struct dlfb_data *dlfb = container_of(work, struct dlfb_data, 963f7018c21STomi Valkeinen free_framebuffer_work.work); 9647ea46206SLadislav Michl dlfb_free_framebuffer(dlfb); 965f7018c21STomi Valkeinen } 966f7018c21STomi Valkeinen /* 967f7018c21STomi Valkeinen * Assumes caller is holding info->lock mutex (for open and release at least) 968f7018c21STomi Valkeinen */ 969f7018c21STomi Valkeinen static int dlfb_ops_release(struct fb_info *info, int user) 970f7018c21STomi Valkeinen { 9717ea46206SLadislav Michl struct dlfb_data *dlfb = info->par; 972f7018c21STomi Valkeinen 9737ea46206SLadislav Michl dlfb->fb_count--; 974f7018c21STomi Valkeinen 975f7018c21STomi Valkeinen /* We can't free fb_info here - fbmem will touch it when we return */ 9767ea46206SLadislav Michl if (dlfb->virtualized && (dlfb->fb_count == 0)) 9777ea46206SLadislav Michl schedule_delayed_work(&dlfb->free_framebuffer_work, HZ); 978f7018c21STomi Valkeinen 9797ea46206SLadislav Michl if ((dlfb->fb_count == 0) && (info->fbdefio)) { 980f7018c21STomi Valkeinen fb_deferred_io_cleanup(info); 981f7018c21STomi Valkeinen kfree(info->fbdefio); 982f7018c21STomi Valkeinen info->fbdefio = NULL; 983f7018c21STomi Valkeinen info->fbops->fb_mmap = dlfb_ops_mmap; 984f7018c21STomi Valkeinen } 985f7018c21STomi Valkeinen 9865865889fSLadislav Michl dev_dbg(info->dev, "release, user=%d count=%d\n", user, dlfb->fb_count); 987f7018c21STomi Valkeinen 9887ea46206SLadislav Michl kref_put(&dlfb->kref, dlfb_free); 989f7018c21STomi Valkeinen 990f7018c21STomi Valkeinen return 0; 991f7018c21STomi Valkeinen } 992f7018c21STomi Valkeinen 993f7018c21STomi Valkeinen /* 994f7018c21STomi Valkeinen * Check whether a video mode is supported by the DisplayLink chip 995f7018c21STomi Valkeinen * We start from monitor's modes, so don't need to filter that here 996f7018c21STomi Valkeinen */ 9975865889fSLadislav Michl static int dlfb_is_valid_mode(struct fb_videomode *mode, struct dlfb_data *dlfb) 998f7018c21STomi Valkeinen { 9995865889fSLadislav Michl if (mode->xres * mode->yres > dlfb->sku_pixel_limit) 1000f7018c21STomi Valkeinen return 0; 1001f7018c21STomi Valkeinen 1002f7018c21STomi Valkeinen return 1; 1003f7018c21STomi Valkeinen } 1004f7018c21STomi Valkeinen 1005f7018c21STomi Valkeinen static void dlfb_var_color_format(struct fb_var_screeninfo *var) 1006f7018c21STomi Valkeinen { 1007f7018c21STomi Valkeinen const struct fb_bitfield red = { 11, 5, 0 }; 1008f7018c21STomi Valkeinen const struct fb_bitfield green = { 5, 6, 0 }; 1009f7018c21STomi Valkeinen const struct fb_bitfield blue = { 0, 5, 0 }; 1010f7018c21STomi Valkeinen 1011f7018c21STomi Valkeinen var->bits_per_pixel = 16; 1012f7018c21STomi Valkeinen var->red = red; 1013f7018c21STomi Valkeinen var->green = green; 1014f7018c21STomi Valkeinen var->blue = blue; 1015f7018c21STomi Valkeinen } 1016f7018c21STomi Valkeinen 1017f7018c21STomi Valkeinen static int dlfb_ops_check_var(struct fb_var_screeninfo *var, 1018f7018c21STomi Valkeinen struct fb_info *info) 1019f7018c21STomi Valkeinen { 1020f7018c21STomi Valkeinen struct fb_videomode mode; 10215865889fSLadislav Michl struct dlfb_data *dlfb = info->par; 1022f7018c21STomi Valkeinen 1023f7018c21STomi Valkeinen /* TODO: support dynamically changing framebuffer size */ 1024f7018c21STomi Valkeinen if ((var->xres * var->yres * 2) > info->fix.smem_len) 1025f7018c21STomi Valkeinen return -EINVAL; 1026f7018c21STomi Valkeinen 1027f7018c21STomi Valkeinen /* set device-specific elements of var unrelated to mode */ 1028f7018c21STomi Valkeinen dlfb_var_color_format(var); 1029f7018c21STomi Valkeinen 1030f7018c21STomi Valkeinen fb_var_to_videomode(&mode, var); 1031f7018c21STomi Valkeinen 10325865889fSLadislav Michl if (!dlfb_is_valid_mode(&mode, dlfb)) 1033f7018c21STomi Valkeinen return -EINVAL; 1034f7018c21STomi Valkeinen 1035f7018c21STomi Valkeinen return 0; 1036f7018c21STomi Valkeinen } 1037f7018c21STomi Valkeinen 1038f7018c21STomi Valkeinen static int dlfb_ops_set_par(struct fb_info *info) 1039f7018c21STomi Valkeinen { 10407ea46206SLadislav Michl struct dlfb_data *dlfb = info->par; 1041f7018c21STomi Valkeinen int result; 1042f7018c21STomi Valkeinen u16 *pix_framebuffer; 1043f7018c21STomi Valkeinen int i; 1044564f1807SMikulas Patocka struct fb_var_screeninfo fvs; 1045564f1807SMikulas Patocka 1046564f1807SMikulas Patocka /* clear the activate field because it causes spurious miscompares */ 1047564f1807SMikulas Patocka fvs = info->var; 1048564f1807SMikulas Patocka fvs.activate = 0; 1049564f1807SMikulas Patocka fvs.vmode &= ~FB_VMODE_SMOOTH_XPAN; 1050564f1807SMikulas Patocka 1051564f1807SMikulas Patocka if (!memcmp(&dlfb->current_mode, &fvs, sizeof(struct fb_var_screeninfo))) 1052564f1807SMikulas Patocka return 0; 1053f7018c21STomi Valkeinen 10547ea46206SLadislav Michl result = dlfb_set_video_mode(dlfb, &info->var); 1055f7018c21STomi Valkeinen 1056564f1807SMikulas Patocka if (result) 1057564f1807SMikulas Patocka return result; 1058564f1807SMikulas Patocka 1059564f1807SMikulas Patocka dlfb->current_mode = fvs; 1060564f1807SMikulas Patocka 1061564f1807SMikulas Patocka if (dlfb->fb_count == 0) { 1062f7018c21STomi Valkeinen 1063f7018c21STomi Valkeinen /* paint greenscreen */ 1064f7018c21STomi Valkeinen 1065f7018c21STomi Valkeinen pix_framebuffer = (u16 *) info->screen_base; 1066f7018c21STomi Valkeinen for (i = 0; i < info->fix.smem_len / 2; i++) 1067f7018c21STomi Valkeinen pix_framebuffer[i] = 0x37e6; 1068f7018c21STomi Valkeinen 10697ea46206SLadislav Michl dlfb_handle_damage(dlfb, 0, 0, info->var.xres, info->var.yres, 1070f7018c21STomi Valkeinen info->screen_base); 1071f7018c21STomi Valkeinen } 1072f7018c21STomi Valkeinen 1073564f1807SMikulas Patocka return 0; 1074f7018c21STomi Valkeinen } 1075f7018c21STomi Valkeinen 1076f7018c21STomi Valkeinen /* To fonzi the jukebox (e.g. make blanking changes take effect) */ 1077f7018c21STomi Valkeinen static char *dlfb_dummy_render(char *buf) 1078f7018c21STomi Valkeinen { 1079f7018c21STomi Valkeinen *buf++ = 0xAF; 1080f7018c21STomi Valkeinen *buf++ = 0x6A; /* copy */ 1081f7018c21STomi Valkeinen *buf++ = 0x00; /* from address*/ 1082f7018c21STomi Valkeinen *buf++ = 0x00; 1083f7018c21STomi Valkeinen *buf++ = 0x00; 1084f7018c21STomi Valkeinen *buf++ = 0x01; /* one pixel */ 1085f7018c21STomi Valkeinen *buf++ = 0x00; /* to address */ 1086f7018c21STomi Valkeinen *buf++ = 0x00; 1087f7018c21STomi Valkeinen *buf++ = 0x00; 1088f7018c21STomi Valkeinen return buf; 1089f7018c21STomi Valkeinen } 1090f7018c21STomi Valkeinen 1091f7018c21STomi Valkeinen /* 1092f7018c21STomi Valkeinen * In order to come back from full DPMS off, we need to set the mode again 1093f7018c21STomi Valkeinen */ 1094f7018c21STomi Valkeinen static int dlfb_ops_blank(int blank_mode, struct fb_info *info) 1095f7018c21STomi Valkeinen { 10967ea46206SLadislav Michl struct dlfb_data *dlfb = info->par; 1097f7018c21STomi Valkeinen char *bufptr; 1098f7018c21STomi Valkeinen struct urb *urb; 1099f7018c21STomi Valkeinen 11005865889fSLadislav Michl dev_dbg(info->dev, "blank, mode %d --> %d\n", 11015865889fSLadislav Michl dlfb->blank_mode, blank_mode); 1102f7018c21STomi Valkeinen 11037ea46206SLadislav Michl if ((dlfb->blank_mode == FB_BLANK_POWERDOWN) && 1104f7018c21STomi Valkeinen (blank_mode != FB_BLANK_POWERDOWN)) { 1105f7018c21STomi Valkeinen 1106f7018c21STomi Valkeinen /* returning from powerdown requires a fresh modeset */ 11077ea46206SLadislav Michl dlfb_set_video_mode(dlfb, &info->var); 1108f7018c21STomi Valkeinen } 1109f7018c21STomi Valkeinen 11107ea46206SLadislav Michl urb = dlfb_get_urb(dlfb); 1111f7018c21STomi Valkeinen if (!urb) 1112f7018c21STomi Valkeinen return 0; 1113f7018c21STomi Valkeinen 1114f7018c21STomi Valkeinen bufptr = (char *) urb->transfer_buffer; 1115f7018c21STomi Valkeinen bufptr = dlfb_vidreg_lock(bufptr); 1116f7018c21STomi Valkeinen bufptr = dlfb_blanking(bufptr, blank_mode); 1117f7018c21STomi Valkeinen bufptr = dlfb_vidreg_unlock(bufptr); 1118f7018c21STomi Valkeinen 1119f7018c21STomi Valkeinen /* seems like a render op is needed to have blank change take effect */ 1120f7018c21STomi Valkeinen bufptr = dlfb_dummy_render(bufptr); 1121f7018c21STomi Valkeinen 11227ea46206SLadislav Michl dlfb_submit_urb(dlfb, urb, bufptr - 1123f7018c21STomi Valkeinen (char *) urb->transfer_buffer); 1124f7018c21STomi Valkeinen 11257ea46206SLadislav Michl dlfb->blank_mode = blank_mode; 1126f7018c21STomi Valkeinen 1127f7018c21STomi Valkeinen return 0; 1128f7018c21STomi Valkeinen } 1129f7018c21STomi Valkeinen 1130f7018c21STomi Valkeinen static struct fb_ops dlfb_ops = { 1131f7018c21STomi Valkeinen .owner = THIS_MODULE, 1132f7018c21STomi Valkeinen .fb_read = fb_sys_read, 1133f7018c21STomi Valkeinen .fb_write = dlfb_ops_write, 1134f7018c21STomi Valkeinen .fb_setcolreg = dlfb_ops_setcolreg, 1135f7018c21STomi Valkeinen .fb_fillrect = dlfb_ops_fillrect, 1136f7018c21STomi Valkeinen .fb_copyarea = dlfb_ops_copyarea, 1137f7018c21STomi Valkeinen .fb_imageblit = dlfb_ops_imageblit, 1138f7018c21STomi Valkeinen .fb_mmap = dlfb_ops_mmap, 1139f7018c21STomi Valkeinen .fb_ioctl = dlfb_ops_ioctl, 1140f7018c21STomi Valkeinen .fb_open = dlfb_ops_open, 1141f7018c21STomi Valkeinen .fb_release = dlfb_ops_release, 1142f7018c21STomi Valkeinen .fb_blank = dlfb_ops_blank, 1143f7018c21STomi Valkeinen .fb_check_var = dlfb_ops_check_var, 1144f7018c21STomi Valkeinen .fb_set_par = dlfb_ops_set_par, 1145f7018c21STomi Valkeinen }; 1146f7018c21STomi Valkeinen 1147f7018c21STomi Valkeinen 1148f7018c21STomi Valkeinen /* 1149f7018c21STomi Valkeinen * Assumes &info->lock held by caller 1150f7018c21STomi Valkeinen * Assumes no active clients have framebuffer open 1151f7018c21STomi Valkeinen */ 11527ea46206SLadislav Michl static int dlfb_realloc_framebuffer(struct dlfb_data *dlfb, struct fb_info *info) 1153f7018c21STomi Valkeinen { 1154f7018c21STomi Valkeinen int old_len = info->fix.smem_len; 1155f7018c21STomi Valkeinen int new_len; 1156f7018c21STomi Valkeinen unsigned char *old_fb = info->screen_base; 1157f7018c21STomi Valkeinen unsigned char *new_fb; 1158f7018c21STomi Valkeinen unsigned char *new_back = NULL; 1159f7018c21STomi Valkeinen 1160f7018c21STomi Valkeinen new_len = info->fix.line_length * info->var.yres; 1161f7018c21STomi Valkeinen 1162f7018c21STomi Valkeinen if (PAGE_ALIGN(new_len) > old_len) { 1163f7018c21STomi Valkeinen /* 1164f7018c21STomi Valkeinen * Alloc system memory for virtual framebuffer 1165f7018c21STomi Valkeinen */ 1166f7018c21STomi Valkeinen new_fb = vmalloc(new_len); 1167f7018c21STomi Valkeinen if (!new_fb) { 11685865889fSLadislav Michl dev_err(info->dev, "Virtual framebuffer alloc failed\n"); 11693c097b06SMarkus Elfring return -ENOMEM; 1170f7018c21STomi Valkeinen } 1171f7018c21STomi Valkeinen 1172f7018c21STomi Valkeinen if (info->screen_base) { 1173f7018c21STomi Valkeinen memcpy(new_fb, old_fb, old_len); 1174f7018c21STomi Valkeinen vfree(info->screen_base); 1175f7018c21STomi Valkeinen } 1176f7018c21STomi Valkeinen 1177f7018c21STomi Valkeinen info->screen_base = new_fb; 1178f7018c21STomi Valkeinen info->fix.smem_len = PAGE_ALIGN(new_len); 1179f7018c21STomi Valkeinen info->fix.smem_start = (unsigned long) new_fb; 1180f7018c21STomi Valkeinen info->flags = udlfb_info_flags; 1181f7018c21STomi Valkeinen 1182f7018c21STomi Valkeinen /* 1183f7018c21STomi Valkeinen * Second framebuffer copy to mirror the framebuffer state 1184f7018c21STomi Valkeinen * on the physical USB device. We can function without this. 1185f7018c21STomi Valkeinen * But with imperfect damage info we may send pixels over USB 1186f7018c21STomi Valkeinen * that were, in fact, unchanged - wasting limited USB bandwidth 1187f7018c21STomi Valkeinen */ 1188f7018c21STomi Valkeinen if (shadow) 1189f7018c21STomi Valkeinen new_back = vzalloc(new_len); 1190f7018c21STomi Valkeinen if (!new_back) 11915865889fSLadislav Michl dev_info(info->dev, 11925865889fSLadislav Michl "No shadow/backing buffer allocated\n"); 1193f7018c21STomi Valkeinen else { 11947ea46206SLadislav Michl vfree(dlfb->backing_buffer); 11957ea46206SLadislav Michl dlfb->backing_buffer = new_back; 1196f7018c21STomi Valkeinen } 1197f7018c21STomi Valkeinen } 11983c097b06SMarkus Elfring return 0; 1199f7018c21STomi Valkeinen } 1200f7018c21STomi Valkeinen 1201f7018c21STomi Valkeinen /* 1202f7018c21STomi Valkeinen * 1) Get EDID from hw, or use sw default 1203f7018c21STomi Valkeinen * 2) Parse into various fb_info structs 1204f7018c21STomi Valkeinen * 3) Allocate virtual framebuffer memory to back highest res mode 1205f7018c21STomi Valkeinen * 1206f7018c21STomi Valkeinen * Parses EDID into three places used by various parts of fbdev: 1207f7018c21STomi Valkeinen * fb_var_screeninfo contains the timing of the monitor's preferred mode 1208f7018c21STomi Valkeinen * fb_info.monspecs is full parsed EDID info, including monspecs.modedb 1209f7018c21STomi Valkeinen * fb_info.modelist is a linked list of all monitor & VESA modes which work 1210f7018c21STomi Valkeinen * 1211f7018c21STomi Valkeinen * If EDID is not readable/valid, then modelist is all VESA modes, 1212f7018c21STomi Valkeinen * monspecs is NULL, and fb_var_screeninfo is set to safe VESA mode 1213f7018c21STomi Valkeinen * Returns 0 if successful 1214f7018c21STomi Valkeinen */ 12157ea46206SLadislav Michl static int dlfb_setup_modes(struct dlfb_data *dlfb, 1216f7018c21STomi Valkeinen struct fb_info *info, 1217f7018c21STomi Valkeinen char *default_edid, size_t default_edid_size) 1218f7018c21STomi Valkeinen { 1219f7018c21STomi Valkeinen char *edid; 12205865889fSLadislav Michl int i, result = 0, tries = 3; 12215865889fSLadislav Michl struct device *dev = info->device; 12225865889fSLadislav Michl struct fb_videomode *mode; 12235865889fSLadislav Michl const struct fb_videomode *default_vmode = NULL; 1224f7018c21STomi Valkeinen 12255865889fSLadislav Michl if (info->dev) { 12265865889fSLadislav Michl /* only use mutex if info has been registered */ 1227f7018c21STomi Valkeinen mutex_lock(&info->lock); 12285865889fSLadislav Michl /* parent device is used otherwise */ 12295865889fSLadislav Michl dev = info->dev; 12305865889fSLadislav Michl } 1231f7018c21STomi Valkeinen 1232f7018c21STomi Valkeinen edid = kmalloc(EDID_LENGTH, GFP_KERNEL); 1233f7018c21STomi Valkeinen if (!edid) { 1234f7018c21STomi Valkeinen result = -ENOMEM; 1235f7018c21STomi Valkeinen goto error; 1236f7018c21STomi Valkeinen } 1237f7018c21STomi Valkeinen 1238f7018c21STomi Valkeinen fb_destroy_modelist(&info->modelist); 1239f7018c21STomi Valkeinen memset(&info->monspecs, 0, sizeof(info->monspecs)); 1240f7018c21STomi Valkeinen 1241f7018c21STomi Valkeinen /* 1242f7018c21STomi Valkeinen * Try to (re)read EDID from hardware first 1243f7018c21STomi Valkeinen * EDID data may return, but not parse as valid 1244f7018c21STomi Valkeinen * Try again a few times, in case of e.g. analog cable noise 1245f7018c21STomi Valkeinen */ 1246f7018c21STomi Valkeinen while (tries--) { 1247f7018c21STomi Valkeinen 12487ea46206SLadislav Michl i = dlfb_get_edid(dlfb, edid, EDID_LENGTH); 1249f7018c21STomi Valkeinen 1250f7018c21STomi Valkeinen if (i >= EDID_LENGTH) 1251f7018c21STomi Valkeinen fb_edid_to_monspecs(edid, &info->monspecs); 1252f7018c21STomi Valkeinen 1253f7018c21STomi Valkeinen if (info->monspecs.modedb_len > 0) { 12547ea46206SLadislav Michl dlfb->edid = edid; 12557ea46206SLadislav Michl dlfb->edid_size = i; 1256f7018c21STomi Valkeinen break; 1257f7018c21STomi Valkeinen } 1258f7018c21STomi Valkeinen } 1259f7018c21STomi Valkeinen 1260f7018c21STomi Valkeinen /* If that fails, use a previously returned EDID if available */ 1261f7018c21STomi Valkeinen if (info->monspecs.modedb_len == 0) { 12625865889fSLadislav Michl dev_err(dev, "Unable to get valid EDID from device/display\n"); 1263f7018c21STomi Valkeinen 12647ea46206SLadislav Michl if (dlfb->edid) { 12657ea46206SLadislav Michl fb_edid_to_monspecs(dlfb->edid, &info->monspecs); 1266f7018c21STomi Valkeinen if (info->monspecs.modedb_len > 0) 12675865889fSLadislav Michl dev_err(dev, "Using previously queried EDID\n"); 1268f7018c21STomi Valkeinen } 1269f7018c21STomi Valkeinen } 1270f7018c21STomi Valkeinen 1271f7018c21STomi Valkeinen /* If that fails, use the default EDID we were handed */ 1272f7018c21STomi Valkeinen if (info->monspecs.modedb_len == 0) { 1273f7018c21STomi Valkeinen if (default_edid_size >= EDID_LENGTH) { 1274f7018c21STomi Valkeinen fb_edid_to_monspecs(default_edid, &info->monspecs); 1275f7018c21STomi Valkeinen if (info->monspecs.modedb_len > 0) { 1276f7018c21STomi Valkeinen memcpy(edid, default_edid, default_edid_size); 12777ea46206SLadislav Michl dlfb->edid = edid; 12787ea46206SLadislav Michl dlfb->edid_size = default_edid_size; 12795865889fSLadislav Michl dev_err(dev, "Using default/backup EDID\n"); 1280f7018c21STomi Valkeinen } 1281f7018c21STomi Valkeinen } 1282f7018c21STomi Valkeinen } 1283f7018c21STomi Valkeinen 1284f7018c21STomi Valkeinen /* If we've got modes, let's pick a best default mode */ 1285f7018c21STomi Valkeinen if (info->monspecs.modedb_len > 0) { 1286f7018c21STomi Valkeinen 1287f7018c21STomi Valkeinen for (i = 0; i < info->monspecs.modedb_len; i++) { 12885865889fSLadislav Michl mode = &info->monspecs.modedb[i]; 12895865889fSLadislav Michl if (dlfb_is_valid_mode(mode, dlfb)) { 12905865889fSLadislav Michl fb_add_videomode(mode, &info->modelist); 12915865889fSLadislav Michl } else { 12925865889fSLadislav Michl dev_dbg(dev, "Specified mode %dx%d too big\n", 12935865889fSLadislav Michl mode->xres, mode->yres); 1294f7018c21STomi Valkeinen if (i == 0) 1295f7018c21STomi Valkeinen /* if we've removed top/best mode */ 1296f7018c21STomi Valkeinen info->monspecs.misc 1297f7018c21STomi Valkeinen &= ~FB_MISC_1ST_DETAIL; 1298f7018c21STomi Valkeinen } 1299f7018c21STomi Valkeinen } 1300f7018c21STomi Valkeinen 1301f7018c21STomi Valkeinen default_vmode = fb_find_best_display(&info->monspecs, 1302f7018c21STomi Valkeinen &info->modelist); 1303f7018c21STomi Valkeinen } 1304f7018c21STomi Valkeinen 1305f7018c21STomi Valkeinen /* If everything else has failed, fall back to safe default mode */ 1306f7018c21STomi Valkeinen if (default_vmode == NULL) { 1307f7018c21STomi Valkeinen 1308f7018c21STomi Valkeinen struct fb_videomode fb_vmode = {0}; 1309f7018c21STomi Valkeinen 1310f7018c21STomi Valkeinen /* 1311f7018c21STomi Valkeinen * Add the standard VESA modes to our modelist 1312f7018c21STomi Valkeinen * Since we don't have EDID, there may be modes that 1313f7018c21STomi Valkeinen * overspec monitor and/or are incorrect aspect ratio, etc. 1314f7018c21STomi Valkeinen * But at least the user has a chance to choose 1315f7018c21STomi Valkeinen */ 1316f7018c21STomi Valkeinen for (i = 0; i < VESA_MODEDB_SIZE; i++) { 13175865889fSLadislav Michl mode = (struct fb_videomode *)&vesa_modes[i]; 13185865889fSLadislav Michl if (dlfb_is_valid_mode(mode, dlfb)) 13195865889fSLadislav Michl fb_add_videomode(mode, &info->modelist); 13205865889fSLadislav Michl else 13215865889fSLadislav Michl dev_dbg(dev, "VESA mode %dx%d too big\n", 13225865889fSLadislav Michl mode->xres, mode->yres); 1323f7018c21STomi Valkeinen } 1324f7018c21STomi Valkeinen 1325f7018c21STomi Valkeinen /* 1326f7018c21STomi Valkeinen * default to resolution safe for projectors 1327f7018c21STomi Valkeinen * (since they are most common case without EDID) 1328f7018c21STomi Valkeinen */ 1329f7018c21STomi Valkeinen fb_vmode.xres = 800; 1330f7018c21STomi Valkeinen fb_vmode.yres = 600; 1331f7018c21STomi Valkeinen fb_vmode.refresh = 60; 1332f7018c21STomi Valkeinen default_vmode = fb_find_nearest_mode(&fb_vmode, 1333f7018c21STomi Valkeinen &info->modelist); 1334f7018c21STomi Valkeinen } 1335f7018c21STomi Valkeinen 1336f7018c21STomi Valkeinen /* If we have good mode and no active clients*/ 13377ea46206SLadislav Michl if ((default_vmode != NULL) && (dlfb->fb_count == 0)) { 1338f7018c21STomi Valkeinen 1339f7018c21STomi Valkeinen fb_videomode_to_var(&info->var, default_vmode); 1340f7018c21STomi Valkeinen dlfb_var_color_format(&info->var); 1341f7018c21STomi Valkeinen 1342f7018c21STomi Valkeinen /* 1343f7018c21STomi Valkeinen * with mode size info, we can now alloc our framebuffer. 1344f7018c21STomi Valkeinen */ 1345f7018c21STomi Valkeinen memcpy(&info->fix, &dlfb_fix, sizeof(dlfb_fix)); 1346f7018c21STomi Valkeinen info->fix.line_length = info->var.xres * 1347f7018c21STomi Valkeinen (info->var.bits_per_pixel / 8); 1348f7018c21STomi Valkeinen 13497ea46206SLadislav Michl result = dlfb_realloc_framebuffer(dlfb, info); 1350f7018c21STomi Valkeinen 1351f7018c21STomi Valkeinen } else 1352f7018c21STomi Valkeinen result = -EINVAL; 1353f7018c21STomi Valkeinen 1354f7018c21STomi Valkeinen error: 13557ea46206SLadislav Michl if (edid && (dlfb->edid != edid)) 1356f7018c21STomi Valkeinen kfree(edid); 1357f7018c21STomi Valkeinen 1358f7018c21STomi Valkeinen if (info->dev) 1359f7018c21STomi Valkeinen mutex_unlock(&info->lock); 1360f7018c21STomi Valkeinen 1361f7018c21STomi Valkeinen return result; 1362f7018c21STomi Valkeinen } 1363f7018c21STomi Valkeinen 1364f7018c21STomi Valkeinen static ssize_t metrics_bytes_rendered_show(struct device *fbdev, 1365f7018c21STomi Valkeinen struct device_attribute *a, char *buf) { 1366f7018c21STomi Valkeinen struct fb_info *fb_info = dev_get_drvdata(fbdev); 13677ea46206SLadislav Michl struct dlfb_data *dlfb = fb_info->par; 1368f7018c21STomi Valkeinen return snprintf(buf, PAGE_SIZE, "%u\n", 13697ea46206SLadislav Michl atomic_read(&dlfb->bytes_rendered)); 1370f7018c21STomi Valkeinen } 1371f7018c21STomi Valkeinen 1372f7018c21STomi Valkeinen static ssize_t metrics_bytes_identical_show(struct device *fbdev, 1373f7018c21STomi Valkeinen struct device_attribute *a, char *buf) { 1374f7018c21STomi Valkeinen struct fb_info *fb_info = dev_get_drvdata(fbdev); 13757ea46206SLadislav Michl struct dlfb_data *dlfb = fb_info->par; 1376f7018c21STomi Valkeinen return snprintf(buf, PAGE_SIZE, "%u\n", 13777ea46206SLadislav Michl atomic_read(&dlfb->bytes_identical)); 1378f7018c21STomi Valkeinen } 1379f7018c21STomi Valkeinen 1380f7018c21STomi Valkeinen static ssize_t metrics_bytes_sent_show(struct device *fbdev, 1381f7018c21STomi Valkeinen struct device_attribute *a, char *buf) { 1382f7018c21STomi Valkeinen struct fb_info *fb_info = dev_get_drvdata(fbdev); 13837ea46206SLadislav Michl struct dlfb_data *dlfb = fb_info->par; 1384f7018c21STomi Valkeinen return snprintf(buf, PAGE_SIZE, "%u\n", 13857ea46206SLadislav Michl atomic_read(&dlfb->bytes_sent)); 1386f7018c21STomi Valkeinen } 1387f7018c21STomi Valkeinen 1388f7018c21STomi Valkeinen static ssize_t metrics_cpu_kcycles_used_show(struct device *fbdev, 1389f7018c21STomi Valkeinen struct device_attribute *a, char *buf) { 1390f7018c21STomi Valkeinen struct fb_info *fb_info = dev_get_drvdata(fbdev); 13917ea46206SLadislav Michl struct dlfb_data *dlfb = fb_info->par; 1392f7018c21STomi Valkeinen return snprintf(buf, PAGE_SIZE, "%u\n", 13937ea46206SLadislav Michl atomic_read(&dlfb->cpu_kcycles_used)); 1394f7018c21STomi Valkeinen } 1395f7018c21STomi Valkeinen 1396f7018c21STomi Valkeinen static ssize_t edid_show( 1397f7018c21STomi Valkeinen struct file *filp, 1398f7018c21STomi Valkeinen struct kobject *kobj, struct bin_attribute *a, 1399f7018c21STomi Valkeinen char *buf, loff_t off, size_t count) { 1400f7018c21STomi Valkeinen struct device *fbdev = container_of(kobj, struct device, kobj); 1401f7018c21STomi Valkeinen struct fb_info *fb_info = dev_get_drvdata(fbdev); 14027ea46206SLadislav Michl struct dlfb_data *dlfb = fb_info->par; 1403f7018c21STomi Valkeinen 14047ea46206SLadislav Michl if (dlfb->edid == NULL) 1405f7018c21STomi Valkeinen return 0; 1406f7018c21STomi Valkeinen 14077ea46206SLadislav Michl if ((off >= dlfb->edid_size) || (count > dlfb->edid_size)) 1408f7018c21STomi Valkeinen return 0; 1409f7018c21STomi Valkeinen 14107ea46206SLadislav Michl if (off + count > dlfb->edid_size) 14117ea46206SLadislav Michl count = dlfb->edid_size - off; 1412f7018c21STomi Valkeinen 14137ea46206SLadislav Michl memcpy(buf, dlfb->edid, count); 1414f7018c21STomi Valkeinen 1415f7018c21STomi Valkeinen return count; 1416f7018c21STomi Valkeinen } 1417f7018c21STomi Valkeinen 1418f7018c21STomi Valkeinen static ssize_t edid_store( 1419f7018c21STomi Valkeinen struct file *filp, 1420f7018c21STomi Valkeinen struct kobject *kobj, struct bin_attribute *a, 1421f7018c21STomi Valkeinen char *src, loff_t src_off, size_t src_size) { 1422f7018c21STomi Valkeinen struct device *fbdev = container_of(kobj, struct device, kobj); 1423f7018c21STomi Valkeinen struct fb_info *fb_info = dev_get_drvdata(fbdev); 14247ea46206SLadislav Michl struct dlfb_data *dlfb = fb_info->par; 1425f7018c21STomi Valkeinen int ret; 1426f7018c21STomi Valkeinen 1427f7018c21STomi Valkeinen /* We only support write of entire EDID at once, no offset*/ 1428f7018c21STomi Valkeinen if ((src_size != EDID_LENGTH) || (src_off != 0)) 1429f7018c21STomi Valkeinen return -EINVAL; 1430f7018c21STomi Valkeinen 14317ea46206SLadislav Michl ret = dlfb_setup_modes(dlfb, fb_info, src, src_size); 1432f7018c21STomi Valkeinen if (ret) 1433f7018c21STomi Valkeinen return ret; 1434f7018c21STomi Valkeinen 14357ea46206SLadislav Michl if (!dlfb->edid || memcmp(src, dlfb->edid, src_size)) 1436f7018c21STomi Valkeinen return -EINVAL; 1437f7018c21STomi Valkeinen 1438f7018c21STomi Valkeinen dlfb_ops_set_par(fb_info); 1439f7018c21STomi Valkeinen return src_size; 1440f7018c21STomi Valkeinen } 1441f7018c21STomi Valkeinen 1442f7018c21STomi Valkeinen static ssize_t metrics_reset_store(struct device *fbdev, 1443f7018c21STomi Valkeinen struct device_attribute *attr, 1444f7018c21STomi Valkeinen const char *buf, size_t count) 1445f7018c21STomi Valkeinen { 1446f7018c21STomi Valkeinen struct fb_info *fb_info = dev_get_drvdata(fbdev); 14477ea46206SLadislav Michl struct dlfb_data *dlfb = fb_info->par; 1448f7018c21STomi Valkeinen 14497ea46206SLadislav Michl atomic_set(&dlfb->bytes_rendered, 0); 14507ea46206SLadislav Michl atomic_set(&dlfb->bytes_identical, 0); 14517ea46206SLadislav Michl atomic_set(&dlfb->bytes_sent, 0); 14527ea46206SLadislav Michl atomic_set(&dlfb->cpu_kcycles_used, 0); 1453f7018c21STomi Valkeinen 1454f7018c21STomi Valkeinen return count; 1455f7018c21STomi Valkeinen } 1456f7018c21STomi Valkeinen 1457598b2eedSBhumika Goyal static const struct bin_attribute edid_attr = { 1458f7018c21STomi Valkeinen .attr.name = "edid", 1459f7018c21STomi Valkeinen .attr.mode = 0666, 1460f7018c21STomi Valkeinen .size = EDID_LENGTH, 1461f7018c21STomi Valkeinen .read = edid_show, 1462f7018c21STomi Valkeinen .write = edid_store 1463f7018c21STomi Valkeinen }; 1464f7018c21STomi Valkeinen 1465fa738a5cSLadislav Michl static const struct device_attribute fb_device_attrs[] = { 1466f7018c21STomi Valkeinen __ATTR_RO(metrics_bytes_rendered), 1467f7018c21STomi Valkeinen __ATTR_RO(metrics_bytes_identical), 1468f7018c21STomi Valkeinen __ATTR_RO(metrics_bytes_sent), 1469f7018c21STomi Valkeinen __ATTR_RO(metrics_cpu_kcycles_used), 1470f7018c21STomi Valkeinen __ATTR(metrics_reset, S_IWUSR, NULL, metrics_reset_store), 1471f7018c21STomi Valkeinen }; 1472f7018c21STomi Valkeinen 1473f7018c21STomi Valkeinen /* 1474f7018c21STomi Valkeinen * This is necessary before we can communicate with the display controller. 1475f7018c21STomi Valkeinen */ 14767ea46206SLadislav Michl static int dlfb_select_std_channel(struct dlfb_data *dlfb) 1477f7018c21STomi Valkeinen { 1478f7018c21STomi Valkeinen int ret; 147945f580c4SMaksim Salau void *buf; 148045f580c4SMaksim Salau static const u8 set_def_chn[] = { 148145f580c4SMaksim Salau 0x57, 0xCD, 0xDC, 0xA7, 1482f7018c21STomi Valkeinen 0x1C, 0x88, 0x5E, 0x15, 1483f7018c21STomi Valkeinen 0x60, 0xFE, 0xC6, 0x97, 1484f7018c21STomi Valkeinen 0x16, 0x3D, 0x47, 0xF2 }; 1485f7018c21STomi Valkeinen 148645f580c4SMaksim Salau buf = kmemdup(set_def_chn, sizeof(set_def_chn), GFP_KERNEL); 148745f580c4SMaksim Salau 148845f580c4SMaksim Salau if (!buf) 148945f580c4SMaksim Salau return -ENOMEM; 149045f580c4SMaksim Salau 14917ea46206SLadislav Michl ret = usb_control_msg(dlfb->udev, usb_sndctrlpipe(dlfb->udev, 0), 1492f7018c21STomi Valkeinen NR_USB_REQUEST_CHANNEL, 1493f7018c21STomi Valkeinen (USB_DIR_OUT | USB_TYPE_VENDOR), 0, 0, 149445f580c4SMaksim Salau buf, sizeof(set_def_chn), USB_CTRL_SET_TIMEOUT); 149545f580c4SMaksim Salau 149645f580c4SMaksim Salau kfree(buf); 149745f580c4SMaksim Salau 1498f7018c21STomi Valkeinen return ret; 1499f7018c21STomi Valkeinen } 1500f7018c21STomi Valkeinen 15017ea46206SLadislav Michl static int dlfb_parse_vendor_descriptor(struct dlfb_data *dlfb, 15025865889fSLadislav Michl struct usb_interface *intf) 1503f7018c21STomi Valkeinen { 1504f7018c21STomi Valkeinen char *desc; 1505f7018c21STomi Valkeinen char *buf; 1506f7018c21STomi Valkeinen char *desc_end; 1507f63cb8d7SAlexey Klimov int total_len; 1508f7018c21STomi Valkeinen 1509f7018c21STomi Valkeinen buf = kzalloc(MAX_VENDOR_DESCRIPTOR_SIZE, GFP_KERNEL); 1510f7018c21STomi Valkeinen if (!buf) 1511f7018c21STomi Valkeinen return false; 1512f7018c21STomi Valkeinen desc = buf; 1513f7018c21STomi Valkeinen 15145865889fSLadislav Michl total_len = usb_get_descriptor(interface_to_usbdev(intf), 1515f7018c21STomi Valkeinen 0x5f, /* vendor specific */ 1516f7018c21STomi Valkeinen 0, desc, MAX_VENDOR_DESCRIPTOR_SIZE); 1517f7018c21STomi Valkeinen 1518f7018c21STomi Valkeinen /* if not found, look in configuration descriptor */ 1519f7018c21STomi Valkeinen if (total_len < 0) { 15205865889fSLadislav Michl if (0 == usb_get_extra_descriptor(intf->cur_altsetting, 1521f7018c21STomi Valkeinen 0x5f, &desc)) 1522f7018c21STomi Valkeinen total_len = (int) desc[0]; 1523f7018c21STomi Valkeinen } 1524f7018c21STomi Valkeinen 1525f7018c21STomi Valkeinen if (total_len > 5) { 15265865889fSLadislav Michl dev_info(&intf->dev, 15275865889fSLadislav Michl "vendor descriptor length: %d data: %11ph\n", 15285865889fSLadislav Michl total_len, desc); 1529f7018c21STomi Valkeinen 1530f7018c21STomi Valkeinen if ((desc[0] != total_len) || /* descriptor length */ 1531f7018c21STomi Valkeinen (desc[1] != 0x5f) || /* vendor descriptor type */ 1532f7018c21STomi Valkeinen (desc[2] != 0x01) || /* version (2 bytes) */ 1533f7018c21STomi Valkeinen (desc[3] != 0x00) || 1534f7018c21STomi Valkeinen (desc[4] != total_len - 2)) /* length after type */ 1535f7018c21STomi Valkeinen goto unrecognized; 1536f7018c21STomi Valkeinen 1537f7018c21STomi Valkeinen desc_end = desc + total_len; 1538f7018c21STomi Valkeinen desc += 5; /* the fixed header we've already parsed */ 1539f7018c21STomi Valkeinen 1540f7018c21STomi Valkeinen while (desc < desc_end) { 1541f7018c21STomi Valkeinen u8 length; 1542f7018c21STomi Valkeinen u16 key; 1543f7018c21STomi Valkeinen 1544115e7759SLadislav Michl key = *desc++; 1545115e7759SLadislav Michl key |= (u16)*desc++ << 8; 1546115e7759SLadislav Michl length = *desc++; 1547f7018c21STomi Valkeinen 1548f7018c21STomi Valkeinen switch (key) { 1549f7018c21STomi Valkeinen case 0x0200: { /* max_area */ 1550115e7759SLadislav Michl u32 max_area = *desc++; 1551115e7759SLadislav Michl max_area |= (u32)*desc++ << 8; 1552115e7759SLadislav Michl max_area |= (u32)*desc++ << 16; 1553115e7759SLadislav Michl max_area |= (u32)*desc++ << 24; 15545865889fSLadislav Michl dev_warn(&intf->dev, 15555865889fSLadislav Michl "DL chip limited to %d pixel modes\n", 1556f7018c21STomi Valkeinen max_area); 15577ea46206SLadislav Michl dlfb->sku_pixel_limit = max_area; 1558f7018c21STomi Valkeinen break; 1559f7018c21STomi Valkeinen } 1560f7018c21STomi Valkeinen default: 1561f7018c21STomi Valkeinen break; 1562f7018c21STomi Valkeinen } 1563f7018c21STomi Valkeinen desc += length; 1564f7018c21STomi Valkeinen } 1565f7018c21STomi Valkeinen } else { 15665865889fSLadislav Michl dev_info(&intf->dev, "vendor descriptor not available (%d)\n", 15675865889fSLadislav Michl total_len); 1568f7018c21STomi Valkeinen } 1569f7018c21STomi Valkeinen 1570f7018c21STomi Valkeinen goto success; 1571f7018c21STomi Valkeinen 1572f7018c21STomi Valkeinen unrecognized: 1573f7018c21STomi Valkeinen /* allow udlfb to load for now even if firmware unrecognized */ 15745865889fSLadislav Michl dev_err(&intf->dev, "Unrecognized vendor firmware descriptor\n"); 1575f7018c21STomi Valkeinen 1576f7018c21STomi Valkeinen success: 1577f7018c21STomi Valkeinen kfree(buf); 1578f7018c21STomi Valkeinen return true; 1579f7018c21STomi Valkeinen } 1580f7018c21STomi Valkeinen 1581f7018c21STomi Valkeinen static void dlfb_init_framebuffer_work(struct work_struct *work); 1582f7018c21STomi Valkeinen 15835865889fSLadislav Michl static int dlfb_usb_probe(struct usb_interface *intf, 1584f7018c21STomi Valkeinen const struct usb_device_id *id) 1585f7018c21STomi Valkeinen { 15867ea46206SLadislav Michl struct dlfb_data *dlfb; 1587f7018c21STomi Valkeinen int retval = -ENOMEM; 15885865889fSLadislav Michl struct usb_device *usbdev = interface_to_usbdev(intf); 1589f7018c21STomi Valkeinen 1590f7018c21STomi Valkeinen /* usb initialization */ 15917ea46206SLadislav Michl dlfb = kzalloc(sizeof(*dlfb), GFP_KERNEL); 15925865889fSLadislav Michl if (!dlfb) { 15935865889fSLadislav Michl dev_err(&intf->dev, "%s: failed to allocate dlfb\n", __func__); 1594f7018c21STomi Valkeinen goto error; 1595f7018c21STomi Valkeinen } 1596f7018c21STomi Valkeinen 15977ea46206SLadislav Michl kref_init(&dlfb->kref); /* matching kref_put in usb .disconnect fn */ 1598f7018c21STomi Valkeinen 15997ea46206SLadislav Michl dlfb->udev = usbdev; 16005865889fSLadislav Michl usb_set_intfdata(intf, dlfb); 1601f7018c21STomi Valkeinen 16025865889fSLadislav Michl dev_dbg(&intf->dev, "console enable=%d\n", console); 16035865889fSLadislav Michl dev_dbg(&intf->dev, "fb_defio enable=%d\n", fb_defio); 16045865889fSLadislav Michl dev_dbg(&intf->dev, "shadow enable=%d\n", shadow); 1605f7018c21STomi Valkeinen 16067ea46206SLadislav Michl dlfb->sku_pixel_limit = 2048 * 1152; /* default to maximum */ 1607f7018c21STomi Valkeinen 16085865889fSLadislav Michl if (!dlfb_parse_vendor_descriptor(dlfb, intf)) { 16095865889fSLadislav Michl dev_err(&intf->dev, 16105865889fSLadislav Michl "firmware not recognized, incompatible device?\n"); 1611f7018c21STomi Valkeinen goto error; 1612f7018c21STomi Valkeinen } 1613f7018c21STomi Valkeinen 1614f7018c21STomi Valkeinen if (pixel_limit) { 16155865889fSLadislav Michl dev_warn(&intf->dev, 16165865889fSLadislav Michl "DL chip limit of %d overridden to %d\n", 16177ea46206SLadislav Michl dlfb->sku_pixel_limit, pixel_limit); 16187ea46206SLadislav Michl dlfb->sku_pixel_limit = pixel_limit; 1619f7018c21STomi Valkeinen } 1620f7018c21STomi Valkeinen 1621f7018c21STomi Valkeinen 16227ea46206SLadislav Michl if (!dlfb_alloc_urb_list(dlfb, WRITES_IN_FLIGHT, MAX_TRANSFER)) { 1623f7018c21STomi Valkeinen retval = -ENOMEM; 16245865889fSLadislav Michl dev_err(&intf->dev, "unable to allocate urb list\n"); 1625f7018c21STomi Valkeinen goto error; 1626f7018c21STomi Valkeinen } 1627f7018c21STomi Valkeinen 16287ea46206SLadislav Michl kref_get(&dlfb->kref); /* matching kref_put in free_framebuffer_work */ 1629f7018c21STomi Valkeinen 16307ea46206SLadislav Michl /* We don't register a new USB class. Our client interface is dlfbev */ 1631f7018c21STomi Valkeinen 1632f7018c21STomi Valkeinen /* Workitem keep things fast & simple during USB enumeration */ 16337ea46206SLadislav Michl INIT_DELAYED_WORK(&dlfb->init_framebuffer_work, 1634f7018c21STomi Valkeinen dlfb_init_framebuffer_work); 16357ea46206SLadislav Michl schedule_delayed_work(&dlfb->init_framebuffer_work, 0); 1636f7018c21STomi Valkeinen 1637f7018c21STomi Valkeinen return 0; 1638f7018c21STomi Valkeinen 1639f7018c21STomi Valkeinen error: 16407ea46206SLadislav Michl if (dlfb) { 1641f7018c21STomi Valkeinen 16427ea46206SLadislav Michl kref_put(&dlfb->kref, dlfb_free); /* last ref from kref_init */ 1643f7018c21STomi Valkeinen 1644f7018c21STomi Valkeinen /* dev has been deallocated. Do not dereference */ 1645f7018c21STomi Valkeinen } 1646f7018c21STomi Valkeinen 1647f7018c21STomi Valkeinen return retval; 1648f7018c21STomi Valkeinen } 1649f7018c21STomi Valkeinen 1650f7018c21STomi Valkeinen static void dlfb_init_framebuffer_work(struct work_struct *work) 1651f7018c21STomi Valkeinen { 16525865889fSLadislav Michl int i, retval; 16535865889fSLadislav Michl struct fb_info *info; 16545865889fSLadislav Michl const struct device_attribute *attr; 16557ea46206SLadislav Michl struct dlfb_data *dlfb = container_of(work, struct dlfb_data, 1656f7018c21STomi Valkeinen init_framebuffer_work.work); 1657f7018c21STomi Valkeinen 1658f7018c21STomi Valkeinen /* allocates framebuffer driver structure, not framebuffer memory */ 16597ea46206SLadislav Michl info = framebuffer_alloc(0, &dlfb->udev->dev); 1660f7018c21STomi Valkeinen if (!info) { 16615865889fSLadislav Michl dev_err(&dlfb->udev->dev, "framebuffer_alloc failed\n"); 1662f7018c21STomi Valkeinen goto error; 1663f7018c21STomi Valkeinen } 1664f7018c21STomi Valkeinen 16657ea46206SLadislav Michl dlfb->info = info; 16667ea46206SLadislav Michl info->par = dlfb; 16677ea46206SLadislav Michl info->pseudo_palette = dlfb->pseudo_palette; 1668*2c29cfc3SMikulas Patocka dlfb->ops = dlfb_ops; 1669*2c29cfc3SMikulas Patocka info->fbops = &dlfb->ops; 1670f7018c21STomi Valkeinen 1671f7018c21STomi Valkeinen retval = fb_alloc_cmap(&info->cmap, 256, 0); 1672f7018c21STomi Valkeinen if (retval < 0) { 16735865889fSLadislav Michl dev_err(info->device, "cmap allocation failed: %d\n", retval); 1674f7018c21STomi Valkeinen goto error; 1675f7018c21STomi Valkeinen } 1676f7018c21STomi Valkeinen 16777ea46206SLadislav Michl INIT_DELAYED_WORK(&dlfb->free_framebuffer_work, 1678f7018c21STomi Valkeinen dlfb_free_framebuffer_work); 1679f7018c21STomi Valkeinen 1680f7018c21STomi Valkeinen INIT_LIST_HEAD(&info->modelist); 1681f7018c21STomi Valkeinen 16827ea46206SLadislav Michl retval = dlfb_setup_modes(dlfb, info, NULL, 0); 1683f7018c21STomi Valkeinen if (retval != 0) { 16845865889fSLadislav Michl dev_err(info->device, 16855865889fSLadislav Michl "unable to find common mode for display and adapter\n"); 1686f7018c21STomi Valkeinen goto error; 1687f7018c21STomi Valkeinen } 1688f7018c21STomi Valkeinen 1689f7018c21STomi Valkeinen /* ready to begin using device */ 1690f7018c21STomi Valkeinen 16917ea46206SLadislav Michl atomic_set(&dlfb->usb_active, 1); 16927ea46206SLadislav Michl dlfb_select_std_channel(dlfb); 1693f7018c21STomi Valkeinen 1694f7018c21STomi Valkeinen dlfb_ops_check_var(&info->var, info); 1695f7018c21STomi Valkeinen dlfb_ops_set_par(info); 1696f7018c21STomi Valkeinen 1697f7018c21STomi Valkeinen retval = register_framebuffer(info); 1698f7018c21STomi Valkeinen if (retval < 0) { 16995865889fSLadislav Michl dev_err(info->device, "unable to register framebuffer: %d\n", 17005865889fSLadislav Michl retval); 1701f7018c21STomi Valkeinen goto error; 1702f7018c21STomi Valkeinen } 1703f7018c21STomi Valkeinen 1704f7018c21STomi Valkeinen for (i = 0; i < ARRAY_SIZE(fb_device_attrs); i++) { 17055865889fSLadislav Michl attr = &fb_device_attrs[i]; 17065865889fSLadislav Michl retval = device_create_file(info->dev, attr); 17075865889fSLadislav Michl if (retval) 17085865889fSLadislav Michl dev_warn(info->device, 17095865889fSLadislav Michl "failed to create '%s' attribute: %d\n", 17105865889fSLadislav Michl attr->attr.name, retval); 1711f7018c21STomi Valkeinen } 1712f7018c21STomi Valkeinen 1713f7018c21STomi Valkeinen retval = device_create_bin_file(info->dev, &edid_attr); 17145865889fSLadislav Michl if (retval) 17155865889fSLadislav Michl dev_warn(info->device, "failed to create '%s' attribute: %d\n", 17165865889fSLadislav Michl edid_attr.attr.name, retval); 1717f7018c21STomi Valkeinen 17185865889fSLadislav Michl dev_info(info->device, 17195865889fSLadislav Michl "%s is DisplayLink USB device (%dx%d, %dK framebuffer memory)\n", 17205865889fSLadislav Michl dev_name(info->dev), info->var.xres, info->var.yres, 17217ea46206SLadislav Michl ((dlfb->backing_buffer) ? 1722f7018c21STomi Valkeinen info->fix.smem_len * 2 : info->fix.smem_len) >> 10); 1723f7018c21STomi Valkeinen return; 1724f7018c21STomi Valkeinen 1725f7018c21STomi Valkeinen error: 17267ea46206SLadislav Michl dlfb_free_framebuffer(dlfb); 1727f7018c21STomi Valkeinen } 1728f7018c21STomi Valkeinen 17295865889fSLadislav Michl static void dlfb_usb_disconnect(struct usb_interface *intf) 1730f7018c21STomi Valkeinen { 17317ea46206SLadislav Michl struct dlfb_data *dlfb; 1732f7018c21STomi Valkeinen struct fb_info *info; 1733f7018c21STomi Valkeinen int i; 1734f7018c21STomi Valkeinen 17355865889fSLadislav Michl dlfb = usb_get_intfdata(intf); 17367ea46206SLadislav Michl info = dlfb->info; 1737f7018c21STomi Valkeinen 17385865889fSLadislav Michl dev_dbg(&intf->dev, "USB disconnect starting\n"); 1739f7018c21STomi Valkeinen 1740f7018c21STomi Valkeinen /* we virtualize until all fb clients release. Then we free */ 17417ea46206SLadislav Michl dlfb->virtualized = true; 1742f7018c21STomi Valkeinen 1743f7018c21STomi Valkeinen /* When non-active we'll update virtual framebuffer, but no new urbs */ 17447ea46206SLadislav Michl atomic_set(&dlfb->usb_active, 0); 1745f7018c21STomi Valkeinen 1746f7018c21STomi Valkeinen /* this function will wait for all in-flight urbs to complete */ 17477ea46206SLadislav Michl dlfb_free_urb_list(dlfb); 1748f7018c21STomi Valkeinen 1749f7018c21STomi Valkeinen if (info) { 1750f7018c21STomi Valkeinen /* remove udlfb's sysfs interfaces */ 1751f7018c21STomi Valkeinen for (i = 0; i < ARRAY_SIZE(fb_device_attrs); i++) 1752f7018c21STomi Valkeinen device_remove_file(info->dev, &fb_device_attrs[i]); 1753f7018c21STomi Valkeinen device_remove_bin_file(info->dev, &edid_attr); 1754f7018c21STomi Valkeinen unlink_framebuffer(info); 1755f7018c21STomi Valkeinen } 1756f7018c21STomi Valkeinen 17575865889fSLadislav Michl usb_set_intfdata(intf, NULL); 17587ea46206SLadislav Michl dlfb->udev = NULL; 1759f7018c21STomi Valkeinen 1760f7018c21STomi Valkeinen /* if clients still have us open, will be freed on last close */ 17617ea46206SLadislav Michl if (dlfb->fb_count == 0) 17627ea46206SLadislav Michl schedule_delayed_work(&dlfb->free_framebuffer_work, 0); 1763f7018c21STomi Valkeinen 1764f7018c21STomi Valkeinen /* release reference taken by kref_init in probe() */ 17657ea46206SLadislav Michl kref_put(&dlfb->kref, dlfb_free); 1766f7018c21STomi Valkeinen 1767f7018c21STomi Valkeinen /* consider dlfb_data freed */ 1768f7018c21STomi Valkeinen } 1769f7018c21STomi Valkeinen 1770f7018c21STomi Valkeinen static struct usb_driver dlfb_driver = { 1771f7018c21STomi Valkeinen .name = "udlfb", 1772f7018c21STomi Valkeinen .probe = dlfb_usb_probe, 1773f7018c21STomi Valkeinen .disconnect = dlfb_usb_disconnect, 1774f7018c21STomi Valkeinen .id_table = id_table, 1775f7018c21STomi Valkeinen }; 1776f7018c21STomi Valkeinen 1777f7018c21STomi Valkeinen module_usb_driver(dlfb_driver); 1778f7018c21STomi Valkeinen 1779f7018c21STomi Valkeinen static void dlfb_urb_completion(struct urb *urb) 1780f7018c21STomi Valkeinen { 1781f7018c21STomi Valkeinen struct urb_node *unode = urb->context; 17827ea46206SLadislav Michl struct dlfb_data *dlfb = unode->dlfb; 1783f7018c21STomi Valkeinen unsigned long flags; 1784f7018c21STomi Valkeinen 17855865889fSLadislav Michl switch (urb->status) { 17865865889fSLadislav Michl case 0: 17875865889fSLadislav Michl /* success */ 17885865889fSLadislav Michl break; 17895865889fSLadislav Michl case -ECONNRESET: 17905865889fSLadislav Michl case -ENOENT: 17915865889fSLadislav Michl case -ESHUTDOWN: 1792f7018c21STomi Valkeinen /* sync/async unlink faults aren't errors */ 17935865889fSLadislav Michl break; 17945865889fSLadislav Michl default: 17955865889fSLadislav Michl dev_err(&dlfb->udev->dev, 17965865889fSLadislav Michl "%s - nonzero write bulk status received: %d\n", 1797f7018c21STomi Valkeinen __func__, urb->status); 17987ea46206SLadislav Michl atomic_set(&dlfb->lost_pixels, 1); 17995865889fSLadislav Michl break; 1800f7018c21STomi Valkeinen } 1801f7018c21STomi Valkeinen 18027ea46206SLadislav Michl urb->transfer_buffer_length = dlfb->urbs.size; /* reset to actual */ 1803f7018c21STomi Valkeinen 18047ea46206SLadislav Michl spin_lock_irqsave(&dlfb->urbs.lock, flags); 18057ea46206SLadislav Michl list_add_tail(&unode->entry, &dlfb->urbs.list); 18067ea46206SLadislav Michl dlfb->urbs.available++; 18077ea46206SLadislav Michl spin_unlock_irqrestore(&dlfb->urbs.lock, flags); 1808f7018c21STomi Valkeinen 18097ea46206SLadislav Michl up(&dlfb->urbs.limit_sem); 1810f7018c21STomi Valkeinen } 1811f7018c21STomi Valkeinen 18127ea46206SLadislav Michl static void dlfb_free_urb_list(struct dlfb_data *dlfb) 1813f7018c21STomi Valkeinen { 18147ea46206SLadislav Michl int count = dlfb->urbs.count; 1815f7018c21STomi Valkeinen struct list_head *node; 1816f7018c21STomi Valkeinen struct urb_node *unode; 1817f7018c21STomi Valkeinen struct urb *urb; 1818f7018c21STomi Valkeinen unsigned long flags; 1819f7018c21STomi Valkeinen 1820f7018c21STomi Valkeinen /* keep waiting and freeing, until we've got 'em all */ 1821f7018c21STomi Valkeinen while (count--) { 18229d0aa601SMikulas Patocka down(&dlfb->urbs.limit_sem); 1823f7018c21STomi Valkeinen 18247ea46206SLadislav Michl spin_lock_irqsave(&dlfb->urbs.lock, flags); 1825f7018c21STomi Valkeinen 18267ea46206SLadislav Michl node = dlfb->urbs.list.next; /* have reserved one with sem */ 1827f7018c21STomi Valkeinen list_del_init(node); 1828f7018c21STomi Valkeinen 18297ea46206SLadislav Michl spin_unlock_irqrestore(&dlfb->urbs.lock, flags); 1830f7018c21STomi Valkeinen 1831f7018c21STomi Valkeinen unode = list_entry(node, struct urb_node, entry); 1832f7018c21STomi Valkeinen urb = unode->urb; 1833f7018c21STomi Valkeinen 1834f7018c21STomi Valkeinen /* Free each separately allocated piece */ 18357ea46206SLadislav Michl usb_free_coherent(urb->dev, dlfb->urbs.size, 1836f7018c21STomi Valkeinen urb->transfer_buffer, urb->transfer_dma); 1837f7018c21STomi Valkeinen usb_free_urb(urb); 1838f7018c21STomi Valkeinen kfree(node); 1839f7018c21STomi Valkeinen } 1840f7018c21STomi Valkeinen 18417ea46206SLadislav Michl dlfb->urbs.count = 0; 1842f7018c21STomi Valkeinen } 1843f7018c21STomi Valkeinen 18447ea46206SLadislav Michl static int dlfb_alloc_urb_list(struct dlfb_data *dlfb, int count, size_t size) 1845f7018c21STomi Valkeinen { 1846f7018c21STomi Valkeinen int i = 0; 1847f7018c21STomi Valkeinen struct urb *urb; 1848f7018c21STomi Valkeinen struct urb_node *unode; 1849f7018c21STomi Valkeinen char *buf; 1850f7018c21STomi Valkeinen 18517ea46206SLadislav Michl spin_lock_init(&dlfb->urbs.lock); 1852f7018c21STomi Valkeinen 18537ea46206SLadislav Michl dlfb->urbs.size = size; 18547ea46206SLadislav Michl INIT_LIST_HEAD(&dlfb->urbs.list); 1855f7018c21STomi Valkeinen 1856f7018c21STomi Valkeinen while (i < count) { 185774fb2519SMarkus Elfring unode = kzalloc(sizeof(*unode), GFP_KERNEL); 1858f7018c21STomi Valkeinen if (!unode) 1859f7018c21STomi Valkeinen break; 18607ea46206SLadislav Michl unode->dlfb = dlfb; 1861f7018c21STomi Valkeinen 1862f7018c21STomi Valkeinen urb = usb_alloc_urb(0, GFP_KERNEL); 1863f7018c21STomi Valkeinen if (!urb) { 1864f7018c21STomi Valkeinen kfree(unode); 1865f7018c21STomi Valkeinen break; 1866f7018c21STomi Valkeinen } 1867f7018c21STomi Valkeinen unode->urb = urb; 1868f7018c21STomi Valkeinen 18697ea46206SLadislav Michl buf = usb_alloc_coherent(dlfb->udev, MAX_TRANSFER, GFP_KERNEL, 1870f7018c21STomi Valkeinen &urb->transfer_dma); 1871f7018c21STomi Valkeinen if (!buf) { 1872f7018c21STomi Valkeinen kfree(unode); 1873f7018c21STomi Valkeinen usb_free_urb(urb); 1874f7018c21STomi Valkeinen break; 1875f7018c21STomi Valkeinen } 1876f7018c21STomi Valkeinen 1877f7018c21STomi Valkeinen /* urb->transfer_buffer_length set to actual before submit */ 18787ea46206SLadislav Michl usb_fill_bulk_urb(urb, dlfb->udev, usb_sndbulkpipe(dlfb->udev, 1), 1879f7018c21STomi Valkeinen buf, size, dlfb_urb_completion, unode); 1880f7018c21STomi Valkeinen urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; 1881f7018c21STomi Valkeinen 18827ea46206SLadislav Michl list_add_tail(&unode->entry, &dlfb->urbs.list); 1883f7018c21STomi Valkeinen 1884f7018c21STomi Valkeinen i++; 1885f7018c21STomi Valkeinen } 1886f7018c21STomi Valkeinen 18877ea46206SLadislav Michl sema_init(&dlfb->urbs.limit_sem, i); 18887ea46206SLadislav Michl dlfb->urbs.count = i; 18897ea46206SLadislav Michl dlfb->urbs.available = i; 1890f7018c21STomi Valkeinen 1891f7018c21STomi Valkeinen return i; 1892f7018c21STomi Valkeinen } 1893f7018c21STomi Valkeinen 18947ea46206SLadislav Michl static struct urb *dlfb_get_urb(struct dlfb_data *dlfb) 1895f7018c21STomi Valkeinen { 1896f63cb8d7SAlexey Klimov int ret; 1897f7018c21STomi Valkeinen struct list_head *entry; 1898f7018c21STomi Valkeinen struct urb_node *unode; 1899f7018c21STomi Valkeinen unsigned long flags; 1900f7018c21STomi Valkeinen 1901f7018c21STomi Valkeinen /* Wait for an in-flight buffer to complete and get re-queued */ 19027ea46206SLadislav Michl ret = down_timeout(&dlfb->urbs.limit_sem, GET_URB_TIMEOUT); 1903f7018c21STomi Valkeinen if (ret) { 19047ea46206SLadislav Michl atomic_set(&dlfb->lost_pixels, 1); 19055865889fSLadislav Michl dev_warn(&dlfb->udev->dev, 19065865889fSLadislav Michl "wait for urb interrupted: %d available: %d\n", 19077ea46206SLadislav Michl ret, dlfb->urbs.available); 1908acea8d5fSLadislav Michl return NULL; 1909f7018c21STomi Valkeinen } 1910f7018c21STomi Valkeinen 19117ea46206SLadislav Michl spin_lock_irqsave(&dlfb->urbs.lock, flags); 1912f7018c21STomi Valkeinen 19137ea46206SLadislav Michl BUG_ON(list_empty(&dlfb->urbs.list)); /* reserved one with limit_sem */ 19147ea46206SLadislav Michl entry = dlfb->urbs.list.next; 1915f7018c21STomi Valkeinen list_del_init(entry); 19167ea46206SLadislav Michl dlfb->urbs.available--; 1917f7018c21STomi Valkeinen 19187ea46206SLadislav Michl spin_unlock_irqrestore(&dlfb->urbs.lock, flags); 1919f7018c21STomi Valkeinen 1920f7018c21STomi Valkeinen unode = list_entry(entry, struct urb_node, entry); 1921acea8d5fSLadislav Michl return unode->urb; 1922f7018c21STomi Valkeinen } 1923f7018c21STomi Valkeinen 19247ea46206SLadislav Michl static int dlfb_submit_urb(struct dlfb_data *dlfb, struct urb *urb, size_t len) 1925f7018c21STomi Valkeinen { 1926f7018c21STomi Valkeinen int ret; 1927f7018c21STomi Valkeinen 19287ea46206SLadislav Michl BUG_ON(len > dlfb->urbs.size); 1929f7018c21STomi Valkeinen 1930f7018c21STomi Valkeinen urb->transfer_buffer_length = len; /* set to actual payload len */ 1931f7018c21STomi Valkeinen ret = usb_submit_urb(urb, GFP_KERNEL); 1932f7018c21STomi Valkeinen if (ret) { 1933f7018c21STomi Valkeinen dlfb_urb_completion(urb); /* because no one else will */ 19347ea46206SLadislav Michl atomic_set(&dlfb->lost_pixels, 1); 19355865889fSLadislav Michl dev_err(&dlfb->udev->dev, "submit urb error: %d\n", ret); 1936f7018c21STomi Valkeinen } 1937f7018c21STomi Valkeinen return ret; 1938f7018c21STomi Valkeinen } 1939f7018c21STomi Valkeinen 1940f7018c21STomi Valkeinen module_param(console, bool, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP); 1941f7018c21STomi Valkeinen MODULE_PARM_DESC(console, "Allow fbcon to open framebuffer"); 1942f7018c21STomi Valkeinen 1943f7018c21STomi Valkeinen module_param(fb_defio, bool, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP); 1944f7018c21STomi Valkeinen MODULE_PARM_DESC(fb_defio, "Page fault detection of mmap writes"); 1945f7018c21STomi Valkeinen 1946f7018c21STomi Valkeinen module_param(shadow, bool, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP); 1947f7018c21STomi Valkeinen MODULE_PARM_DESC(shadow, "Shadow vid mem. Disable to save mem but lose perf"); 1948f7018c21STomi Valkeinen 1949f7018c21STomi Valkeinen module_param(pixel_limit, int, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP); 1950f7018c21STomi Valkeinen MODULE_PARM_DESC(pixel_limit, "Force limit on max mode (in x*y pixels)"); 1951f7018c21STomi Valkeinen 1952f7018c21STomi Valkeinen MODULE_AUTHOR("Roberto De Ioris <roberto@unbit.it>, " 1953f7018c21STomi Valkeinen "Jaya Kumar <jayakumar.lkml@gmail.com>, " 1954f7018c21STomi Valkeinen "Bernie Thompson <bernie@plugable.com>"); 1955f7018c21STomi Valkeinen MODULE_DESCRIPTION("DisplayLink kernel framebuffer driver"); 1956f7018c21STomi Valkeinen MODULE_LICENSE("GPL"); 1957f7018c21STomi Valkeinen 1958