Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * udlfb.c -- Framebuffer driver for DisplayLink USB controller
0004  *
0005  * Copyright (C) 2009 Roberto De Ioris <roberto@unbit.it>
0006  * Copyright (C) 2009 Jaya Kumar <jayakumar.lkml@gmail.com>
0007  * Copyright (C) 2009 Bernie Thompson <bernie@plugable.com>
0008  *
0009  * Layout is based on skeletonfb by James Simmons and Geert Uytterhoeven,
0010  * usb-skeleton by GregKH.
0011  *
0012  * Device-specific portions based on information from Displaylink, with work
0013  * from Florian Echtler, Henrik Bjerregaard Pedersen, and others.
0014  */
0015 
0016 #include <linux/module.h>
0017 #include <linux/kernel.h>
0018 #include <linux/init.h>
0019 #include <linux/usb.h>
0020 #include <linux/uaccess.h>
0021 #include <linux/mm.h>
0022 #include <linux/fb.h>
0023 #include <linux/vmalloc.h>
0024 #include <linux/slab.h>
0025 #include <linux/delay.h>
0026 #include <asm/unaligned.h>
0027 #include <video/udlfb.h>
0028 #include "edid.h"
0029 
0030 static const struct fb_fix_screeninfo dlfb_fix = {
0031     .id =           "udlfb",
0032     .type =         FB_TYPE_PACKED_PIXELS,
0033     .visual =       FB_VISUAL_TRUECOLOR,
0034     .xpanstep =     0,
0035     .ypanstep =     0,
0036     .ywrapstep =    0,
0037     .accel =        FB_ACCEL_NONE,
0038 };
0039 
0040 static const u32 udlfb_info_flags = FBINFO_DEFAULT | FBINFO_READS_FAST |
0041         FBINFO_VIRTFB |
0042         FBINFO_HWACCEL_IMAGEBLIT | FBINFO_HWACCEL_FILLRECT |
0043         FBINFO_HWACCEL_COPYAREA | FBINFO_MISC_ALWAYS_SETPAR;
0044 
0045 /*
0046  * There are many DisplayLink-based graphics products, all with unique PIDs.
0047  * So we match on DisplayLink's VID + Vendor-Defined Interface Class (0xff)
0048  * We also require a match on SubClass (0x00) and Protocol (0x00),
0049  * which is compatible with all known USB 2.0 era graphics chips and firmware,
0050  * but allows DisplayLink to increment those for any future incompatible chips
0051  */
0052 static const struct usb_device_id id_table[] = {
0053     {.idVendor = 0x17e9,
0054      .bInterfaceClass = 0xff,
0055      .bInterfaceSubClass = 0x00,
0056      .bInterfaceProtocol = 0x00,
0057      .match_flags = USB_DEVICE_ID_MATCH_VENDOR |
0058         USB_DEVICE_ID_MATCH_INT_CLASS |
0059         USB_DEVICE_ID_MATCH_INT_SUBCLASS |
0060         USB_DEVICE_ID_MATCH_INT_PROTOCOL,
0061     },
0062     {},
0063 };
0064 MODULE_DEVICE_TABLE(usb, id_table);
0065 
0066 /* module options */
0067 static bool console = true; /* Allow fbcon to open framebuffer */
0068 static bool fb_defio = true;  /* Detect mmap writes using page faults */
0069 static bool shadow = true; /* Optionally disable shadow framebuffer */
0070 static int pixel_limit; /* Optionally force a pixel resolution limit */
0071 
0072 struct dlfb_deferred_free {
0073     struct list_head list;
0074     void *mem;
0075 };
0076 
0077 static int dlfb_realloc_framebuffer(struct dlfb_data *dlfb, struct fb_info *info, u32 new_len);
0078 
0079 /* dlfb keeps a list of urbs for efficient bulk transfers */
0080 static void dlfb_urb_completion(struct urb *urb);
0081 static struct urb *dlfb_get_urb(struct dlfb_data *dlfb);
0082 static int dlfb_submit_urb(struct dlfb_data *dlfb, struct urb * urb, size_t len);
0083 static int dlfb_alloc_urb_list(struct dlfb_data *dlfb, int count, size_t size);
0084 static void dlfb_free_urb_list(struct dlfb_data *dlfb);
0085 
0086 /*
0087  * All DisplayLink bulk operations start with 0xAF, followed by specific code
0088  * All operations are written to buffers which then later get sent to device
0089  */
0090 static char *dlfb_set_register(char *buf, u8 reg, u8 val)
0091 {
0092     *buf++ = 0xAF;
0093     *buf++ = 0x20;
0094     *buf++ = reg;
0095     *buf++ = val;
0096     return buf;
0097 }
0098 
0099 static char *dlfb_vidreg_lock(char *buf)
0100 {
0101     return dlfb_set_register(buf, 0xFF, 0x00);
0102 }
0103 
0104 static char *dlfb_vidreg_unlock(char *buf)
0105 {
0106     return dlfb_set_register(buf, 0xFF, 0xFF);
0107 }
0108 
0109 /*
0110  * Map FB_BLANK_* to DisplayLink register
0111  * DLReg FB_BLANK_*
0112  * ----- -----------------------------
0113  *  0x00 FB_BLANK_UNBLANK (0)
0114  *  0x01 FB_BLANK (1)
0115  *  0x03 FB_BLANK_VSYNC_SUSPEND (2)
0116  *  0x05 FB_BLANK_HSYNC_SUSPEND (3)
0117  *  0x07 FB_BLANK_POWERDOWN (4) Note: requires modeset to come back
0118  */
0119 static char *dlfb_blanking(char *buf, int fb_blank)
0120 {
0121     u8 reg;
0122 
0123     switch (fb_blank) {
0124     case FB_BLANK_POWERDOWN:
0125         reg = 0x07;
0126         break;
0127     case FB_BLANK_HSYNC_SUSPEND:
0128         reg = 0x05;
0129         break;
0130     case FB_BLANK_VSYNC_SUSPEND:
0131         reg = 0x03;
0132         break;
0133     case FB_BLANK_NORMAL:
0134         reg = 0x01;
0135         break;
0136     default:
0137         reg = 0x00;
0138     }
0139 
0140     buf = dlfb_set_register(buf, 0x1F, reg);
0141 
0142     return buf;
0143 }
0144 
0145 static char *dlfb_set_color_depth(char *buf, u8 selection)
0146 {
0147     return dlfb_set_register(buf, 0x00, selection);
0148 }
0149 
0150 static char *dlfb_set_base16bpp(char *wrptr, u32 base)
0151 {
0152     /* the base pointer is 16 bits wide, 0x20 is hi byte. */
0153     wrptr = dlfb_set_register(wrptr, 0x20, base >> 16);
0154     wrptr = dlfb_set_register(wrptr, 0x21, base >> 8);
0155     return dlfb_set_register(wrptr, 0x22, base);
0156 }
0157 
0158 /*
0159  * DisplayLink HW has separate 16bpp and 8bpp framebuffers.
0160  * In 24bpp modes, the low 323 RGB bits go in the 8bpp framebuffer
0161  */
0162 static char *dlfb_set_base8bpp(char *wrptr, u32 base)
0163 {
0164     wrptr = dlfb_set_register(wrptr, 0x26, base >> 16);
0165     wrptr = dlfb_set_register(wrptr, 0x27, base >> 8);
0166     return dlfb_set_register(wrptr, 0x28, base);
0167 }
0168 
0169 static char *dlfb_set_register_16(char *wrptr, u8 reg, u16 value)
0170 {
0171     wrptr = dlfb_set_register(wrptr, reg, value >> 8);
0172     return dlfb_set_register(wrptr, reg+1, value);
0173 }
0174 
0175 /*
0176  * This is kind of weird because the controller takes some
0177  * register values in a different byte order than other registers.
0178  */
0179 static char *dlfb_set_register_16be(char *wrptr, u8 reg, u16 value)
0180 {
0181     wrptr = dlfb_set_register(wrptr, reg, value);
0182     return dlfb_set_register(wrptr, reg+1, value >> 8);
0183 }
0184 
0185 /*
0186  * LFSR is linear feedback shift register. The reason we have this is
0187  * because the display controller needs to minimize the clock depth of
0188  * various counters used in the display path. So this code reverses the
0189  * provided value into the lfsr16 value by counting backwards to get
0190  * the value that needs to be set in the hardware comparator to get the
0191  * same actual count. This makes sense once you read above a couple of
0192  * times and think about it from a hardware perspective.
0193  */
0194 static u16 dlfb_lfsr16(u16 actual_count)
0195 {
0196     u32 lv = 0xFFFF; /* This is the lfsr value that the hw starts with */
0197 
0198     while (actual_count--) {
0199         lv =     ((lv << 1) |
0200             (((lv >> 15) ^ (lv >> 4) ^ (lv >> 2) ^ (lv >> 1)) & 1))
0201             & 0xFFFF;
0202     }
0203 
0204     return (u16) lv;
0205 }
0206 
0207 /*
0208  * This does LFSR conversion on the value that is to be written.
0209  * See LFSR explanation above for more detail.
0210  */
0211 static char *dlfb_set_register_lfsr16(char *wrptr, u8 reg, u16 value)
0212 {
0213     return dlfb_set_register_16(wrptr, reg, dlfb_lfsr16(value));
0214 }
0215 
0216 /*
0217  * This takes a standard fbdev screeninfo struct and all of its monitor mode
0218  * details and converts them into the DisplayLink equivalent register commands.
0219  */
0220 static char *dlfb_set_vid_cmds(char *wrptr, struct fb_var_screeninfo *var)
0221 {
0222     u16 xds, yds;
0223     u16 xde, yde;
0224     u16 yec;
0225 
0226     /* x display start */
0227     xds = var->left_margin + var->hsync_len;
0228     wrptr = dlfb_set_register_lfsr16(wrptr, 0x01, xds);
0229     /* x display end */
0230     xde = xds + var->xres;
0231     wrptr = dlfb_set_register_lfsr16(wrptr, 0x03, xde);
0232 
0233     /* y display start */
0234     yds = var->upper_margin + var->vsync_len;
0235     wrptr = dlfb_set_register_lfsr16(wrptr, 0x05, yds);
0236     /* y display end */
0237     yde = yds + var->yres;
0238     wrptr = dlfb_set_register_lfsr16(wrptr, 0x07, yde);
0239 
0240     /* x end count is active + blanking - 1 */
0241     wrptr = dlfb_set_register_lfsr16(wrptr, 0x09,
0242             xde + var->right_margin - 1);
0243 
0244     /* libdlo hardcodes hsync start to 1 */
0245     wrptr = dlfb_set_register_lfsr16(wrptr, 0x0B, 1);
0246 
0247     /* hsync end is width of sync pulse + 1 */
0248     wrptr = dlfb_set_register_lfsr16(wrptr, 0x0D, var->hsync_len + 1);
0249 
0250     /* hpixels is active pixels */
0251     wrptr = dlfb_set_register_16(wrptr, 0x0F, var->xres);
0252 
0253     /* yendcount is vertical active + vertical blanking */
0254     yec = var->yres + var->upper_margin + var->lower_margin +
0255             var->vsync_len;
0256     wrptr = dlfb_set_register_lfsr16(wrptr, 0x11, yec);
0257 
0258     /* libdlo hardcodes vsync start to 0 */
0259     wrptr = dlfb_set_register_lfsr16(wrptr, 0x13, 0);
0260 
0261     /* vsync end is width of vsync pulse */
0262     wrptr = dlfb_set_register_lfsr16(wrptr, 0x15, var->vsync_len);
0263 
0264     /* vpixels is active pixels */
0265     wrptr = dlfb_set_register_16(wrptr, 0x17, var->yres);
0266 
0267     /* convert picoseconds to 5kHz multiple for pclk5k = x * 1E12/5k */
0268     wrptr = dlfb_set_register_16be(wrptr, 0x1B,
0269             200*1000*1000/var->pixclock);
0270 
0271     return wrptr;
0272 }
0273 
0274 /*
0275  * This takes a standard fbdev screeninfo struct that was fetched or prepared
0276  * and then generates the appropriate command sequence that then drives the
0277  * display controller.
0278  */
0279 static int dlfb_set_video_mode(struct dlfb_data *dlfb,
0280                 struct fb_var_screeninfo *var)
0281 {
0282     char *buf;
0283     char *wrptr;
0284     int retval;
0285     int writesize;
0286     struct urb *urb;
0287 
0288     if (!atomic_read(&dlfb->usb_active))
0289         return -EPERM;
0290 
0291     urb = dlfb_get_urb(dlfb);
0292     if (!urb)
0293         return -ENOMEM;
0294 
0295     buf = (char *) urb->transfer_buffer;
0296 
0297     /*
0298     * This first section has to do with setting the base address on the
0299     * controller * associated with the display. There are 2 base
0300     * pointers, currently, we only * use the 16 bpp segment.
0301     */
0302     wrptr = dlfb_vidreg_lock(buf);
0303     wrptr = dlfb_set_color_depth(wrptr, 0x00);
0304     /* set base for 16bpp segment to 0 */
0305     wrptr = dlfb_set_base16bpp(wrptr, 0);
0306     /* set base for 8bpp segment to end of fb */
0307     wrptr = dlfb_set_base8bpp(wrptr, dlfb->info->fix.smem_len);
0308 
0309     wrptr = dlfb_set_vid_cmds(wrptr, var);
0310     wrptr = dlfb_blanking(wrptr, FB_BLANK_UNBLANK);
0311     wrptr = dlfb_vidreg_unlock(wrptr);
0312 
0313     writesize = wrptr - buf;
0314 
0315     retval = dlfb_submit_urb(dlfb, urb, writesize);
0316 
0317     dlfb->blank_mode = FB_BLANK_UNBLANK;
0318 
0319     return retval;
0320 }
0321 
0322 static int dlfb_ops_mmap(struct fb_info *info, struct vm_area_struct *vma)
0323 {
0324     unsigned long start = vma->vm_start;
0325     unsigned long size = vma->vm_end - vma->vm_start;
0326     unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
0327     unsigned long page, pos;
0328 
0329     if (info->fbdefio)
0330         return fb_deferred_io_mmap(info, vma);
0331 
0332     if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT))
0333         return -EINVAL;
0334     if (size > info->fix.smem_len)
0335         return -EINVAL;
0336     if (offset > info->fix.smem_len - size)
0337         return -EINVAL;
0338 
0339     pos = (unsigned long)info->fix.smem_start + offset;
0340 
0341     dev_dbg(info->dev, "mmap() framebuffer addr:%lu size:%lu\n",
0342         pos, size);
0343 
0344     while (size > 0) {
0345         page = vmalloc_to_pfn((void *)pos);
0346         if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED))
0347             return -EAGAIN;
0348 
0349         start += PAGE_SIZE;
0350         pos += PAGE_SIZE;
0351         if (size > PAGE_SIZE)
0352             size -= PAGE_SIZE;
0353         else
0354             size = 0;
0355     }
0356 
0357     return 0;
0358 }
0359 
0360 /*
0361  * Trims identical data from front and back of line
0362  * Sets new front buffer address and width
0363  * And returns byte count of identical pixels
0364  * Assumes CPU natural alignment (unsigned long)
0365  * for back and front buffer ptrs and width
0366  */
0367 static int dlfb_trim_hline(const u8 *bback, const u8 **bfront, int *width_bytes)
0368 {
0369     int j, k;
0370     const unsigned long *back = (const unsigned long *) bback;
0371     const unsigned long *front = (const unsigned long *) *bfront;
0372     const int width = *width_bytes / sizeof(unsigned long);
0373     int identical = width;
0374     int start = width;
0375     int end = width;
0376 
0377     for (j = 0; j < width; j++) {
0378         if (back[j] != front[j]) {
0379             start = j;
0380             break;
0381         }
0382     }
0383 
0384     for (k = width - 1; k > j; k--) {
0385         if (back[k] != front[k]) {
0386             end = k+1;
0387             break;
0388         }
0389     }
0390 
0391     identical = start + (width - end);
0392     *bfront = (u8 *) &front[start];
0393     *width_bytes = (end - start) * sizeof(unsigned long);
0394 
0395     return identical * sizeof(unsigned long);
0396 }
0397 
0398 /*
0399  * Render a command stream for an encoded horizontal line segment of pixels.
0400  *
0401  * A command buffer holds several commands.
0402  * It always begins with a fresh command header
0403  * (the protocol doesn't require this, but we enforce it to allow
0404  * multiple buffers to be potentially encoded and sent in parallel).
0405  * A single command encodes one contiguous horizontal line of pixels
0406  *
0407  * The function relies on the client to do all allocation, so that
0408  * rendering can be done directly to output buffers (e.g. USB URBs).
0409  * The function fills the supplied command buffer, providing information
0410  * on where it left off, so the client may call in again with additional
0411  * buffers if the line will take several buffers to complete.
0412  *
0413  * A single command can transmit a maximum of 256 pixels,
0414  * regardless of the compression ratio (protocol design limit).
0415  * To the hardware, 0 for a size byte means 256
0416  *
0417  * Rather than 256 pixel commands which are either rl or raw encoded,
0418  * the rlx command simply assumes alternating raw and rl spans within one cmd.
0419  * This has a slightly larger header overhead, but produces more even results.
0420  * It also processes all data (read and write) in a single pass.
0421  * Performance benchmarks of common cases show it having just slightly better
0422  * compression than 256 pixel raw or rle commands, with similar CPU consumpion.
0423  * But for very rl friendly data, will compress not quite as well.
0424  */
0425 static void dlfb_compress_hline(
0426     const uint16_t **pixel_start_ptr,
0427     const uint16_t *const pixel_end,
0428     uint32_t *device_address_ptr,
0429     uint8_t **command_buffer_ptr,
0430     const uint8_t *const cmd_buffer_end,
0431     unsigned long back_buffer_offset,
0432     int *ident_ptr)
0433 {
0434     const uint16_t *pixel = *pixel_start_ptr;
0435     uint32_t dev_addr  = *device_address_ptr;
0436     uint8_t *cmd = *command_buffer_ptr;
0437 
0438     while ((pixel_end > pixel) &&
0439            (cmd_buffer_end - MIN_RLX_CMD_BYTES > cmd)) {
0440         uint8_t *raw_pixels_count_byte = NULL;
0441         uint8_t *cmd_pixels_count_byte = NULL;
0442         const uint16_t *raw_pixel_start = NULL;
0443         const uint16_t *cmd_pixel_start, *cmd_pixel_end = NULL;
0444 
0445         if (back_buffer_offset &&
0446             *pixel == *(u16 *)((u8 *)pixel + back_buffer_offset)) {
0447             pixel++;
0448             dev_addr += BPP;
0449             (*ident_ptr)++;
0450             continue;
0451         }
0452 
0453         *cmd++ = 0xAF;
0454         *cmd++ = 0x6B;
0455         *cmd++ = dev_addr >> 16;
0456         *cmd++ = dev_addr >> 8;
0457         *cmd++ = dev_addr;
0458 
0459         cmd_pixels_count_byte = cmd++; /*  we'll know this later */
0460         cmd_pixel_start = pixel;
0461 
0462         raw_pixels_count_byte = cmd++; /*  we'll know this later */
0463         raw_pixel_start = pixel;
0464 
0465         cmd_pixel_end = pixel + min3(MAX_CMD_PIXELS + 1UL,
0466                     (unsigned long)(pixel_end - pixel),
0467                     (unsigned long)(cmd_buffer_end - 1 - cmd) / BPP);
0468 
0469         if (back_buffer_offset) {
0470             /* note: the framebuffer may change under us, so we must test for underflow */
0471             while (cmd_pixel_end - 1 > pixel &&
0472                    *(cmd_pixel_end - 1) == *(u16 *)((u8 *)(cmd_pixel_end - 1) + back_buffer_offset))
0473                 cmd_pixel_end--;
0474         }
0475 
0476         while (pixel < cmd_pixel_end) {
0477             const uint16_t * const repeating_pixel = pixel;
0478             u16 pixel_value = *pixel;
0479 
0480             put_unaligned_be16(pixel_value, cmd);
0481             if (back_buffer_offset)
0482                 *(u16 *)((u8 *)pixel + back_buffer_offset) = pixel_value;
0483             cmd += 2;
0484             pixel++;
0485 
0486             if (unlikely((pixel < cmd_pixel_end) &&
0487                      (*pixel == pixel_value))) {
0488                 /* go back and fill in raw pixel count */
0489                 *raw_pixels_count_byte = ((repeating_pixel -
0490                         raw_pixel_start) + 1) & 0xFF;
0491 
0492                 do {
0493                     if (back_buffer_offset)
0494                         *(u16 *)((u8 *)pixel + back_buffer_offset) = pixel_value;
0495                     pixel++;
0496                 } while ((pixel < cmd_pixel_end) &&
0497                      (*pixel == pixel_value));
0498 
0499                 /* immediately after raw data is repeat byte */
0500                 *cmd++ = ((pixel - repeating_pixel) - 1) & 0xFF;
0501 
0502                 /* Then start another raw pixel span */
0503                 raw_pixel_start = pixel;
0504                 raw_pixels_count_byte = cmd++;
0505             }
0506         }
0507 
0508         if (pixel > raw_pixel_start) {
0509             /* finalize last RAW span */
0510             *raw_pixels_count_byte = (pixel-raw_pixel_start) & 0xFF;
0511         } else {
0512             /* undo unused byte */
0513             cmd--;
0514         }
0515 
0516         *cmd_pixels_count_byte = (pixel - cmd_pixel_start) & 0xFF;
0517         dev_addr += (u8 *)pixel - (u8 *)cmd_pixel_start;
0518     }
0519 
0520     if (cmd_buffer_end - MIN_RLX_CMD_BYTES <= cmd) {
0521         /* Fill leftover bytes with no-ops */
0522         if (cmd_buffer_end > cmd)
0523             memset(cmd, 0xAF, cmd_buffer_end - cmd);
0524         cmd = (uint8_t *) cmd_buffer_end;
0525     }
0526 
0527     *command_buffer_ptr = cmd;
0528     *pixel_start_ptr = pixel;
0529     *device_address_ptr = dev_addr;
0530 }
0531 
0532 /*
0533  * There are 3 copies of every pixel: The front buffer that the fbdev
0534  * client renders to, the actual framebuffer across the USB bus in hardware
0535  * (that we can only write to, slowly, and can never read), and (optionally)
0536  * our shadow copy that tracks what's been sent to that hardware buffer.
0537  */
0538 static int dlfb_render_hline(struct dlfb_data *dlfb, struct urb **urb_ptr,
0539                   const char *front, char **urb_buf_ptr,
0540                   u32 byte_offset, u32 byte_width,
0541                   int *ident_ptr, int *sent_ptr)
0542 {
0543     const u8 *line_start, *line_end, *next_pixel;
0544     u32 dev_addr = dlfb->base16 + byte_offset;
0545     struct urb *urb = *urb_ptr;
0546     u8 *cmd = *urb_buf_ptr;
0547     u8 *cmd_end = (u8 *) urb->transfer_buffer + urb->transfer_buffer_length;
0548     unsigned long back_buffer_offset = 0;
0549 
0550     line_start = (u8 *) (front + byte_offset);
0551     next_pixel = line_start;
0552     line_end = next_pixel + byte_width;
0553 
0554     if (dlfb->backing_buffer) {
0555         int offset;
0556         const u8 *back_start = (u8 *) (dlfb->backing_buffer
0557                         + byte_offset);
0558 
0559         back_buffer_offset = (unsigned long)back_start - (unsigned long)line_start;
0560 
0561         *ident_ptr += dlfb_trim_hline(back_start, &next_pixel,
0562             &byte_width);
0563 
0564         offset = next_pixel - line_start;
0565         line_end = next_pixel + byte_width;
0566         dev_addr += offset;
0567         back_start += offset;
0568         line_start += offset;
0569     }
0570 
0571     while (next_pixel < line_end) {
0572 
0573         dlfb_compress_hline((const uint16_t **) &next_pixel,
0574                  (const uint16_t *) line_end, &dev_addr,
0575             (u8 **) &cmd, (u8 *) cmd_end, back_buffer_offset,
0576             ident_ptr);
0577 
0578         if (cmd >= cmd_end) {
0579             int len = cmd - (u8 *) urb->transfer_buffer;
0580             if (dlfb_submit_urb(dlfb, urb, len))
0581                 return 1; /* lost pixels is set */
0582             *sent_ptr += len;
0583             urb = dlfb_get_urb(dlfb);
0584             if (!urb)
0585                 return 1; /* lost_pixels is set */
0586             *urb_ptr = urb;
0587             cmd = urb->transfer_buffer;
0588             cmd_end = &cmd[urb->transfer_buffer_length];
0589         }
0590     }
0591 
0592     *urb_buf_ptr = cmd;
0593 
0594     return 0;
0595 }
0596 
0597 static int dlfb_handle_damage(struct dlfb_data *dlfb, int x, int y, int width, int height)
0598 {
0599     int i, ret;
0600     char *cmd;
0601     cycles_t start_cycles, end_cycles;
0602     int bytes_sent = 0;
0603     int bytes_identical = 0;
0604     struct urb *urb;
0605     int aligned_x;
0606 
0607     start_cycles = get_cycles();
0608 
0609     mutex_lock(&dlfb->render_mutex);
0610 
0611     aligned_x = DL_ALIGN_DOWN(x, sizeof(unsigned long));
0612     width = DL_ALIGN_UP(width + (x-aligned_x), sizeof(unsigned long));
0613     x = aligned_x;
0614 
0615     if ((width <= 0) ||
0616         (x + width > dlfb->info->var.xres) ||
0617         (y + height > dlfb->info->var.yres)) {
0618         ret = -EINVAL;
0619         goto unlock_ret;
0620     }
0621 
0622     if (!atomic_read(&dlfb->usb_active)) {
0623         ret = 0;
0624         goto unlock_ret;
0625     }
0626 
0627     urb = dlfb_get_urb(dlfb);
0628     if (!urb) {
0629         ret = 0;
0630         goto unlock_ret;
0631     }
0632     cmd = urb->transfer_buffer;
0633 
0634     for (i = y; i < y + height ; i++) {
0635         const int line_offset = dlfb->info->fix.line_length * i;
0636         const int byte_offset = line_offset + (x * BPP);
0637 
0638         if (dlfb_render_hline(dlfb, &urb,
0639                       (char *) dlfb->info->fix.smem_start,
0640                       &cmd, byte_offset, width * BPP,
0641                       &bytes_identical, &bytes_sent))
0642             goto error;
0643     }
0644 
0645     if (cmd > (char *) urb->transfer_buffer) {
0646         int len;
0647         if (cmd < (char *) urb->transfer_buffer + urb->transfer_buffer_length)
0648             *cmd++ = 0xAF;
0649         /* Send partial buffer remaining before exiting */
0650         len = cmd - (char *) urb->transfer_buffer;
0651         dlfb_submit_urb(dlfb, urb, len);
0652         bytes_sent += len;
0653     } else
0654         dlfb_urb_completion(urb);
0655 
0656 error:
0657     atomic_add(bytes_sent, &dlfb->bytes_sent);
0658     atomic_add(bytes_identical, &dlfb->bytes_identical);
0659     atomic_add(width*height*2, &dlfb->bytes_rendered);
0660     end_cycles = get_cycles();
0661     atomic_add(((unsigned int) ((end_cycles - start_cycles)
0662             >> 10)), /* Kcycles */
0663            &dlfb->cpu_kcycles_used);
0664 
0665     ret = 0;
0666 
0667 unlock_ret:
0668     mutex_unlock(&dlfb->render_mutex);
0669     return ret;
0670 }
0671 
0672 static void dlfb_init_damage(struct dlfb_data *dlfb)
0673 {
0674     dlfb->damage_x = INT_MAX;
0675     dlfb->damage_x2 = 0;
0676     dlfb->damage_y = INT_MAX;
0677     dlfb->damage_y2 = 0;
0678 }
0679 
0680 static void dlfb_damage_work(struct work_struct *w)
0681 {
0682     struct dlfb_data *dlfb = container_of(w, struct dlfb_data, damage_work);
0683     int x, x2, y, y2;
0684 
0685     spin_lock_irq(&dlfb->damage_lock);
0686     x = dlfb->damage_x;
0687     x2 = dlfb->damage_x2;
0688     y = dlfb->damage_y;
0689     y2 = dlfb->damage_y2;
0690     dlfb_init_damage(dlfb);
0691     spin_unlock_irq(&dlfb->damage_lock);
0692 
0693     if (x < x2 && y < y2)
0694         dlfb_handle_damage(dlfb, x, y, x2 - x, y2 - y);
0695 }
0696 
0697 static void dlfb_offload_damage(struct dlfb_data *dlfb, int x, int y, int width, int height)
0698 {
0699     unsigned long flags;
0700     int x2 = x + width;
0701     int y2 = y + height;
0702 
0703     if (x >= x2 || y >= y2)
0704         return;
0705 
0706     spin_lock_irqsave(&dlfb->damage_lock, flags);
0707     dlfb->damage_x = min(x, dlfb->damage_x);
0708     dlfb->damage_x2 = max(x2, dlfb->damage_x2);
0709     dlfb->damage_y = min(y, dlfb->damage_y);
0710     dlfb->damage_y2 = max(y2, dlfb->damage_y2);
0711     spin_unlock_irqrestore(&dlfb->damage_lock, flags);
0712 
0713     schedule_work(&dlfb->damage_work);
0714 }
0715 
0716 /*
0717  * Path triggered by usermode clients who write to filesystem
0718  * e.g. cat filename > /dev/fb1
0719  * Not used by X Windows or text-mode console. But useful for testing.
0720  * Slow because of extra copy and we must assume all pixels dirty.
0721  */
0722 static ssize_t dlfb_ops_write(struct fb_info *info, const char __user *buf,
0723               size_t count, loff_t *ppos)
0724 {
0725     ssize_t result;
0726     struct dlfb_data *dlfb = info->par;
0727     u32 offset = (u32) *ppos;
0728 
0729     result = fb_sys_write(info, buf, count, ppos);
0730 
0731     if (result > 0) {
0732         int start = max((int)(offset / info->fix.line_length), 0);
0733         int lines = min((u32)((result / info->fix.line_length) + 1),
0734                 (u32)info->var.yres);
0735 
0736         dlfb_handle_damage(dlfb, 0, start, info->var.xres,
0737             lines);
0738     }
0739 
0740     return result;
0741 }
0742 
0743 /* hardware has native COPY command (see libdlo), but not worth it for fbcon */
0744 static void dlfb_ops_copyarea(struct fb_info *info,
0745                 const struct fb_copyarea *area)
0746 {
0747 
0748     struct dlfb_data *dlfb = info->par;
0749 
0750     sys_copyarea(info, area);
0751 
0752     dlfb_offload_damage(dlfb, area->dx, area->dy,
0753             area->width, area->height);
0754 }
0755 
0756 static void dlfb_ops_imageblit(struct fb_info *info,
0757                 const struct fb_image *image)
0758 {
0759     struct dlfb_data *dlfb = info->par;
0760 
0761     sys_imageblit(info, image);
0762 
0763     dlfb_offload_damage(dlfb, image->dx, image->dy,
0764             image->width, image->height);
0765 }
0766 
0767 static void dlfb_ops_fillrect(struct fb_info *info,
0768               const struct fb_fillrect *rect)
0769 {
0770     struct dlfb_data *dlfb = info->par;
0771 
0772     sys_fillrect(info, rect);
0773 
0774     dlfb_offload_damage(dlfb, rect->dx, rect->dy, rect->width,
0775                   rect->height);
0776 }
0777 
0778 /*
0779  * NOTE: fb_defio.c is holding info->fbdefio.mutex
0780  *   Touching ANY framebuffer memory that triggers a page fault
0781  *   in fb_defio will cause a deadlock, when it also tries to
0782  *   grab the same mutex.
0783  */
0784 static void dlfb_dpy_deferred_io(struct fb_info *info, struct list_head *pagereflist)
0785 {
0786     struct fb_deferred_io_pageref *pageref;
0787     struct dlfb_data *dlfb = info->par;
0788     struct urb *urb;
0789     char *cmd;
0790     cycles_t start_cycles, end_cycles;
0791     int bytes_sent = 0;
0792     int bytes_identical = 0;
0793     int bytes_rendered = 0;
0794 
0795     mutex_lock(&dlfb->render_mutex);
0796 
0797     if (!fb_defio)
0798         goto unlock_ret;
0799 
0800     if (!atomic_read(&dlfb->usb_active))
0801         goto unlock_ret;
0802 
0803     start_cycles = get_cycles();
0804 
0805     urb = dlfb_get_urb(dlfb);
0806     if (!urb)
0807         goto unlock_ret;
0808 
0809     cmd = urb->transfer_buffer;
0810 
0811     /* walk the written page list and render each to device */
0812     list_for_each_entry(pageref, pagereflist, list) {
0813         if (dlfb_render_hline(dlfb, &urb, (char *) info->fix.smem_start,
0814                       &cmd, pageref->offset, PAGE_SIZE,
0815                       &bytes_identical, &bytes_sent))
0816             goto error;
0817         bytes_rendered += PAGE_SIZE;
0818     }
0819 
0820     if (cmd > (char *) urb->transfer_buffer) {
0821         int len;
0822         if (cmd < (char *) urb->transfer_buffer + urb->transfer_buffer_length)
0823             *cmd++ = 0xAF;
0824         /* Send partial buffer remaining before exiting */
0825         len = cmd - (char *) urb->transfer_buffer;
0826         dlfb_submit_urb(dlfb, urb, len);
0827         bytes_sent += len;
0828     } else
0829         dlfb_urb_completion(urb);
0830 
0831 error:
0832     atomic_add(bytes_sent, &dlfb->bytes_sent);
0833     atomic_add(bytes_identical, &dlfb->bytes_identical);
0834     atomic_add(bytes_rendered, &dlfb->bytes_rendered);
0835     end_cycles = get_cycles();
0836     atomic_add(((unsigned int) ((end_cycles - start_cycles)
0837             >> 10)), /* Kcycles */
0838            &dlfb->cpu_kcycles_used);
0839 unlock_ret:
0840     mutex_unlock(&dlfb->render_mutex);
0841 }
0842 
0843 static int dlfb_get_edid(struct dlfb_data *dlfb, char *edid, int len)
0844 {
0845     int i, ret;
0846     char *rbuf;
0847 
0848     rbuf = kmalloc(2, GFP_KERNEL);
0849     if (!rbuf)
0850         return 0;
0851 
0852     for (i = 0; i < len; i++) {
0853         ret = usb_control_msg(dlfb->udev,
0854                       usb_rcvctrlpipe(dlfb->udev, 0), 0x02,
0855                       (0x80 | (0x02 << 5)), i << 8, 0xA1,
0856                       rbuf, 2, USB_CTRL_GET_TIMEOUT);
0857         if (ret < 2) {
0858             dev_err(&dlfb->udev->dev,
0859                 "Read EDID byte %d failed: %d\n", i, ret);
0860             i--;
0861             break;
0862         }
0863         edid[i] = rbuf[1];
0864     }
0865 
0866     kfree(rbuf);
0867 
0868     return i;
0869 }
0870 
0871 static int dlfb_ops_ioctl(struct fb_info *info, unsigned int cmd,
0872                 unsigned long arg)
0873 {
0874 
0875     struct dlfb_data *dlfb = info->par;
0876 
0877     if (!atomic_read(&dlfb->usb_active))
0878         return 0;
0879 
0880     /* TODO: Update X server to get this from sysfs instead */
0881     if (cmd == DLFB_IOCTL_RETURN_EDID) {
0882         void __user *edid = (void __user *)arg;
0883         if (copy_to_user(edid, dlfb->edid, dlfb->edid_size))
0884             return -EFAULT;
0885         return 0;
0886     }
0887 
0888     /* TODO: Help propose a standard fb.h ioctl to report mmap damage */
0889     if (cmd == DLFB_IOCTL_REPORT_DAMAGE) {
0890         struct dloarea area;
0891 
0892         if (copy_from_user(&area, (void __user *)arg,
0893                   sizeof(struct dloarea)))
0894             return -EFAULT;
0895 
0896         /*
0897          * If we have a damage-aware client, turn fb_defio "off"
0898          * To avoid perf imact of unnecessary page fault handling.
0899          * Done by resetting the delay for this fb_info to a very
0900          * long period. Pages will become writable and stay that way.
0901          * Reset to normal value when all clients have closed this fb.
0902          */
0903         if (info->fbdefio)
0904             info->fbdefio->delay = DL_DEFIO_WRITE_DISABLE;
0905 
0906         if (area.x < 0)
0907             area.x = 0;
0908 
0909         if (area.x > info->var.xres)
0910             area.x = info->var.xres;
0911 
0912         if (area.y < 0)
0913             area.y = 0;
0914 
0915         if (area.y > info->var.yres)
0916             area.y = info->var.yres;
0917 
0918         dlfb_handle_damage(dlfb, area.x, area.y, area.w, area.h);
0919     }
0920 
0921     return 0;
0922 }
0923 
0924 /* taken from vesafb */
0925 static int
0926 dlfb_ops_setcolreg(unsigned regno, unsigned red, unsigned green,
0927            unsigned blue, unsigned transp, struct fb_info *info)
0928 {
0929     int err = 0;
0930 
0931     if (regno >= info->cmap.len)
0932         return 1;
0933 
0934     if (regno < 16) {
0935         if (info->var.red.offset == 10) {
0936             /* 1:5:5:5 */
0937             ((u32 *) (info->pseudo_palette))[regno] =
0938                 ((red & 0xf800) >> 1) |
0939                 ((green & 0xf800) >> 6) | ((blue & 0xf800) >> 11);
0940         } else {
0941             /* 0:5:6:5 */
0942             ((u32 *) (info->pseudo_palette))[regno] =
0943                 ((red & 0xf800)) |
0944                 ((green & 0xfc00) >> 5) | ((blue & 0xf800) >> 11);
0945         }
0946     }
0947 
0948     return err;
0949 }
0950 
0951 /*
0952  * It's common for several clients to have framebuffer open simultaneously.
0953  * e.g. both fbcon and X. Makes things interesting.
0954  * Assumes caller is holding info->lock (for open and release at least)
0955  */
0956 static int dlfb_ops_open(struct fb_info *info, int user)
0957 {
0958     struct dlfb_data *dlfb = info->par;
0959 
0960     /*
0961      * fbcon aggressively connects to first framebuffer it finds,
0962      * preventing other clients (X) from working properly. Usually
0963      * not what the user wants. Fail by default with option to enable.
0964      */
0965     if ((user == 0) && (!console))
0966         return -EBUSY;
0967 
0968     /* If the USB device is gone, we don't accept new opens */
0969     if (dlfb->virtualized)
0970         return -ENODEV;
0971 
0972     dlfb->fb_count++;
0973 
0974     if (fb_defio && (info->fbdefio == NULL)) {
0975         /* enable defio at last moment if not disabled by client */
0976 
0977         struct fb_deferred_io *fbdefio;
0978 
0979         fbdefio = kzalloc(sizeof(struct fb_deferred_io), GFP_KERNEL);
0980 
0981         if (fbdefio) {
0982             fbdefio->delay = DL_DEFIO_WRITE_DELAY;
0983             fbdefio->sort_pagereflist = true;
0984             fbdefio->deferred_io = dlfb_dpy_deferred_io;
0985         }
0986 
0987         info->fbdefio = fbdefio;
0988         fb_deferred_io_init(info);
0989     }
0990 
0991     dev_dbg(info->dev, "open, user=%d fb_info=%p count=%d\n",
0992         user, info, dlfb->fb_count);
0993 
0994     return 0;
0995 }
0996 
0997 static void dlfb_ops_destroy(struct fb_info *info)
0998 {
0999     struct dlfb_data *dlfb = info->par;
1000 
1001     cancel_work_sync(&dlfb->damage_work);
1002 
1003     mutex_destroy(&dlfb->render_mutex);
1004 
1005     if (info->cmap.len != 0)
1006         fb_dealloc_cmap(&info->cmap);
1007     if (info->monspecs.modedb)
1008         fb_destroy_modedb(info->monspecs.modedb);
1009     vfree(info->screen_base);
1010 
1011     fb_destroy_modelist(&info->modelist);
1012 
1013     while (!list_empty(&dlfb->deferred_free)) {
1014         struct dlfb_deferred_free *d = list_entry(dlfb->deferred_free.next, struct dlfb_deferred_free, list);
1015         list_del(&d->list);
1016         vfree(d->mem);
1017         kfree(d);
1018     }
1019     vfree(dlfb->backing_buffer);
1020     kfree(dlfb->edid);
1021     dlfb_free_urb_list(dlfb);
1022     usb_put_dev(dlfb->udev);
1023     kfree(dlfb);
1024 
1025     /* Assume info structure is freed after this point */
1026     framebuffer_release(info);
1027 }
1028 
1029 /*
1030  * Assumes caller is holding info->lock mutex (for open and release at least)
1031  */
1032 static int dlfb_ops_release(struct fb_info *info, int user)
1033 {
1034     struct dlfb_data *dlfb = info->par;
1035 
1036     dlfb->fb_count--;
1037 
1038     if ((dlfb->fb_count == 0) && (info->fbdefio)) {
1039         fb_deferred_io_cleanup(info);
1040         kfree(info->fbdefio);
1041         info->fbdefio = NULL;
1042     }
1043 
1044     dev_dbg(info->dev, "release, user=%d count=%d\n", user, dlfb->fb_count);
1045 
1046     return 0;
1047 }
1048 
1049 /*
1050  * Check whether a video mode is supported by the DisplayLink chip
1051  * We start from monitor's modes, so don't need to filter that here
1052  */
1053 static int dlfb_is_valid_mode(struct fb_videomode *mode, struct dlfb_data *dlfb)
1054 {
1055     if (mode->xres * mode->yres > dlfb->sku_pixel_limit)
1056         return 0;
1057 
1058     return 1;
1059 }
1060 
1061 static void dlfb_var_color_format(struct fb_var_screeninfo *var)
1062 {
1063     const struct fb_bitfield red = { 11, 5, 0 };
1064     const struct fb_bitfield green = { 5, 6, 0 };
1065     const struct fb_bitfield blue = { 0, 5, 0 };
1066 
1067     var->bits_per_pixel = 16;
1068     var->red = red;
1069     var->green = green;
1070     var->blue = blue;
1071 }
1072 
1073 static int dlfb_ops_check_var(struct fb_var_screeninfo *var,
1074                 struct fb_info *info)
1075 {
1076     struct fb_videomode mode;
1077     struct dlfb_data *dlfb = info->par;
1078 
1079     /* set device-specific elements of var unrelated to mode */
1080     dlfb_var_color_format(var);
1081 
1082     fb_var_to_videomode(&mode, var);
1083 
1084     if (!dlfb_is_valid_mode(&mode, dlfb))
1085         return -EINVAL;
1086 
1087     return 0;
1088 }
1089 
1090 static int dlfb_ops_set_par(struct fb_info *info)
1091 {
1092     struct dlfb_data *dlfb = info->par;
1093     int result;
1094     u16 *pix_framebuffer;
1095     int i;
1096     struct fb_var_screeninfo fvs;
1097     u32 line_length = info->var.xres * (info->var.bits_per_pixel / 8);
1098 
1099     /* clear the activate field because it causes spurious miscompares */
1100     fvs = info->var;
1101     fvs.activate = 0;
1102     fvs.vmode &= ~FB_VMODE_SMOOTH_XPAN;
1103 
1104     if (!memcmp(&dlfb->current_mode, &fvs, sizeof(struct fb_var_screeninfo)))
1105         return 0;
1106 
1107     result = dlfb_realloc_framebuffer(dlfb, info, info->var.yres * line_length);
1108     if (result)
1109         return result;
1110 
1111     result = dlfb_set_video_mode(dlfb, &info->var);
1112 
1113     if (result)
1114         return result;
1115 
1116     dlfb->current_mode = fvs;
1117     info->fix.line_length = line_length;
1118 
1119     if (dlfb->fb_count == 0) {
1120 
1121         /* paint greenscreen */
1122 
1123         pix_framebuffer = (u16 *) info->screen_base;
1124         for (i = 0; i < info->fix.smem_len / 2; i++)
1125             pix_framebuffer[i] = 0x37e6;
1126     }
1127 
1128     dlfb_handle_damage(dlfb, 0, 0, info->var.xres, info->var.yres);
1129 
1130     return 0;
1131 }
1132 
1133 /* To fonzi the jukebox (e.g. make blanking changes take effect) */
1134 static char *dlfb_dummy_render(char *buf)
1135 {
1136     *buf++ = 0xAF;
1137     *buf++ = 0x6A; /* copy */
1138     *buf++ = 0x00; /* from address*/
1139     *buf++ = 0x00;
1140     *buf++ = 0x00;
1141     *buf++ = 0x01; /* one pixel */
1142     *buf++ = 0x00; /* to address */
1143     *buf++ = 0x00;
1144     *buf++ = 0x00;
1145     return buf;
1146 }
1147 
1148 /*
1149  * In order to come back from full DPMS off, we need to set the mode again
1150  */
1151 static int dlfb_ops_blank(int blank_mode, struct fb_info *info)
1152 {
1153     struct dlfb_data *dlfb = info->par;
1154     char *bufptr;
1155     struct urb *urb;
1156 
1157     dev_dbg(info->dev, "blank, mode %d --> %d\n",
1158         dlfb->blank_mode, blank_mode);
1159 
1160     if ((dlfb->blank_mode == FB_BLANK_POWERDOWN) &&
1161         (blank_mode != FB_BLANK_POWERDOWN)) {
1162 
1163         /* returning from powerdown requires a fresh modeset */
1164         dlfb_set_video_mode(dlfb, &info->var);
1165     }
1166 
1167     urb = dlfb_get_urb(dlfb);
1168     if (!urb)
1169         return 0;
1170 
1171     bufptr = (char *) urb->transfer_buffer;
1172     bufptr = dlfb_vidreg_lock(bufptr);
1173     bufptr = dlfb_blanking(bufptr, blank_mode);
1174     bufptr = dlfb_vidreg_unlock(bufptr);
1175 
1176     /* seems like a render op is needed to have blank change take effect */
1177     bufptr = dlfb_dummy_render(bufptr);
1178 
1179     dlfb_submit_urb(dlfb, urb, bufptr -
1180             (char *) urb->transfer_buffer);
1181 
1182     dlfb->blank_mode = blank_mode;
1183 
1184     return 0;
1185 }
1186 
1187 static const struct fb_ops dlfb_ops = {
1188     .owner = THIS_MODULE,
1189     .fb_read = fb_sys_read,
1190     .fb_write = dlfb_ops_write,
1191     .fb_setcolreg = dlfb_ops_setcolreg,
1192     .fb_fillrect = dlfb_ops_fillrect,
1193     .fb_copyarea = dlfb_ops_copyarea,
1194     .fb_imageblit = dlfb_ops_imageblit,
1195     .fb_mmap = dlfb_ops_mmap,
1196     .fb_ioctl = dlfb_ops_ioctl,
1197     .fb_open = dlfb_ops_open,
1198     .fb_release = dlfb_ops_release,
1199     .fb_blank = dlfb_ops_blank,
1200     .fb_check_var = dlfb_ops_check_var,
1201     .fb_set_par = dlfb_ops_set_par,
1202     .fb_destroy = dlfb_ops_destroy,
1203 };
1204 
1205 
1206 static void dlfb_deferred_vfree(struct dlfb_data *dlfb, void *mem)
1207 {
1208     struct dlfb_deferred_free *d = kmalloc(sizeof(struct dlfb_deferred_free), GFP_KERNEL);
1209     if (!d)
1210         return;
1211     d->mem = mem;
1212     list_add(&d->list, &dlfb->deferred_free);
1213 }
1214 
1215 /*
1216  * Assumes &info->lock held by caller
1217  * Assumes no active clients have framebuffer open
1218  */
1219 static int dlfb_realloc_framebuffer(struct dlfb_data *dlfb, struct fb_info *info, u32 new_len)
1220 {
1221     u32 old_len = info->fix.smem_len;
1222     const void *old_fb = (const void __force *)info->screen_base;
1223     unsigned char *new_fb;
1224     unsigned char *new_back = NULL;
1225 
1226     new_len = PAGE_ALIGN(new_len);
1227 
1228     if (new_len > old_len) {
1229         /*
1230          * Alloc system memory for virtual framebuffer
1231          */
1232         new_fb = vmalloc(new_len);
1233         if (!new_fb) {
1234             dev_err(info->dev, "Virtual framebuffer alloc failed\n");
1235             return -ENOMEM;
1236         }
1237         memset(new_fb, 0xff, new_len);
1238 
1239         if (info->screen_base) {
1240             memcpy(new_fb, old_fb, old_len);
1241             dlfb_deferred_vfree(dlfb, (void __force *)info->screen_base);
1242         }
1243 
1244         info->screen_base = (char __iomem *)new_fb;
1245         info->fix.smem_len = new_len;
1246         info->fix.smem_start = (unsigned long) new_fb;
1247         info->flags = udlfb_info_flags;
1248 
1249         /*
1250          * Second framebuffer copy to mirror the framebuffer state
1251          * on the physical USB device. We can function without this.
1252          * But with imperfect damage info we may send pixels over USB
1253          * that were, in fact, unchanged - wasting limited USB bandwidth
1254          */
1255         if (shadow)
1256             new_back = vzalloc(new_len);
1257         if (!new_back)
1258             dev_info(info->dev,
1259                  "No shadow/backing buffer allocated\n");
1260         else {
1261             dlfb_deferred_vfree(dlfb, dlfb->backing_buffer);
1262             dlfb->backing_buffer = new_back;
1263         }
1264     }
1265     return 0;
1266 }
1267 
1268 /*
1269  * 1) Get EDID from hw, or use sw default
1270  * 2) Parse into various fb_info structs
1271  * 3) Allocate virtual framebuffer memory to back highest res mode
1272  *
1273  * Parses EDID into three places used by various parts of fbdev:
1274  * fb_var_screeninfo contains the timing of the monitor's preferred mode
1275  * fb_info.monspecs is full parsed EDID info, including monspecs.modedb
1276  * fb_info.modelist is a linked list of all monitor & VESA modes which work
1277  *
1278  * If EDID is not readable/valid, then modelist is all VESA modes,
1279  * monspecs is NULL, and fb_var_screeninfo is set to safe VESA mode
1280  * Returns 0 if successful
1281  */
1282 static int dlfb_setup_modes(struct dlfb_data *dlfb,
1283                struct fb_info *info,
1284                char *default_edid, size_t default_edid_size)
1285 {
1286     char *edid;
1287     int i, result = 0, tries = 3;
1288     struct device *dev = info->device;
1289     struct fb_videomode *mode;
1290     const struct fb_videomode *default_vmode = NULL;
1291 
1292     if (info->dev) {
1293         /* only use mutex if info has been registered */
1294         mutex_lock(&info->lock);
1295         /* parent device is used otherwise */
1296         dev = info->dev;
1297     }
1298 
1299     edid = kmalloc(EDID_LENGTH, GFP_KERNEL);
1300     if (!edid) {
1301         result = -ENOMEM;
1302         goto error;
1303     }
1304 
1305     fb_destroy_modelist(&info->modelist);
1306     memset(&info->monspecs, 0, sizeof(info->monspecs));
1307 
1308     /*
1309      * Try to (re)read EDID from hardware first
1310      * EDID data may return, but not parse as valid
1311      * Try again a few times, in case of e.g. analog cable noise
1312      */
1313     while (tries--) {
1314 
1315         i = dlfb_get_edid(dlfb, edid, EDID_LENGTH);
1316 
1317         if (i >= EDID_LENGTH)
1318             fb_edid_to_monspecs(edid, &info->monspecs);
1319 
1320         if (info->monspecs.modedb_len > 0) {
1321             dlfb->edid = edid;
1322             dlfb->edid_size = i;
1323             break;
1324         }
1325     }
1326 
1327     /* If that fails, use a previously returned EDID if available */
1328     if (info->monspecs.modedb_len == 0) {
1329         dev_err(dev, "Unable to get valid EDID from device/display\n");
1330 
1331         if (dlfb->edid) {
1332             fb_edid_to_monspecs(dlfb->edid, &info->monspecs);
1333             if (info->monspecs.modedb_len > 0)
1334                 dev_err(dev, "Using previously queried EDID\n");
1335         }
1336     }
1337 
1338     /* If that fails, use the default EDID we were handed */
1339     if (info->monspecs.modedb_len == 0) {
1340         if (default_edid_size >= EDID_LENGTH) {
1341             fb_edid_to_monspecs(default_edid, &info->monspecs);
1342             if (info->monspecs.modedb_len > 0) {
1343                 memcpy(edid, default_edid, default_edid_size);
1344                 dlfb->edid = edid;
1345                 dlfb->edid_size = default_edid_size;
1346                 dev_err(dev, "Using default/backup EDID\n");
1347             }
1348         }
1349     }
1350 
1351     /* If we've got modes, let's pick a best default mode */
1352     if (info->monspecs.modedb_len > 0) {
1353 
1354         for (i = 0; i < info->monspecs.modedb_len; i++) {
1355             mode = &info->monspecs.modedb[i];
1356             if (dlfb_is_valid_mode(mode, dlfb)) {
1357                 fb_add_videomode(mode, &info->modelist);
1358             } else {
1359                 dev_dbg(dev, "Specified mode %dx%d too big\n",
1360                     mode->xres, mode->yres);
1361                 if (i == 0)
1362                     /* if we've removed top/best mode */
1363                     info->monspecs.misc
1364                         &= ~FB_MISC_1ST_DETAIL;
1365             }
1366         }
1367 
1368         default_vmode = fb_find_best_display(&info->monspecs,
1369                              &info->modelist);
1370     }
1371 
1372     /* If everything else has failed, fall back to safe default mode */
1373     if (default_vmode == NULL) {
1374 
1375         struct fb_videomode fb_vmode = {0};
1376 
1377         /*
1378          * Add the standard VESA modes to our modelist
1379          * Since we don't have EDID, there may be modes that
1380          * overspec monitor and/or are incorrect aspect ratio, etc.
1381          * But at least the user has a chance to choose
1382          */
1383         for (i = 0; i < VESA_MODEDB_SIZE; i++) {
1384             mode = (struct fb_videomode *)&vesa_modes[i];
1385             if (dlfb_is_valid_mode(mode, dlfb))
1386                 fb_add_videomode(mode, &info->modelist);
1387             else
1388                 dev_dbg(dev, "VESA mode %dx%d too big\n",
1389                     mode->xres, mode->yres);
1390         }
1391 
1392         /*
1393          * default to resolution safe for projectors
1394          * (since they are most common case without EDID)
1395          */
1396         fb_vmode.xres = 800;
1397         fb_vmode.yres = 600;
1398         fb_vmode.refresh = 60;
1399         default_vmode = fb_find_nearest_mode(&fb_vmode,
1400                              &info->modelist);
1401     }
1402 
1403     /* If we have good mode and no active clients*/
1404     if ((default_vmode != NULL) && (dlfb->fb_count == 0)) {
1405 
1406         fb_videomode_to_var(&info->var, default_vmode);
1407         dlfb_var_color_format(&info->var);
1408 
1409         /*
1410          * with mode size info, we can now alloc our framebuffer.
1411          */
1412         memcpy(&info->fix, &dlfb_fix, sizeof(dlfb_fix));
1413     } else
1414         result = -EINVAL;
1415 
1416 error:
1417     if (edid && (dlfb->edid != edid))
1418         kfree(edid);
1419 
1420     if (info->dev)
1421         mutex_unlock(&info->lock);
1422 
1423     return result;
1424 }
1425 
1426 static ssize_t metrics_bytes_rendered_show(struct device *fbdev,
1427                    struct device_attribute *a, char *buf) {
1428     struct fb_info *fb_info = dev_get_drvdata(fbdev);
1429     struct dlfb_data *dlfb = fb_info->par;
1430     return sysfs_emit(buf, "%u\n",
1431             atomic_read(&dlfb->bytes_rendered));
1432 }
1433 
1434 static ssize_t metrics_bytes_identical_show(struct device *fbdev,
1435                    struct device_attribute *a, char *buf) {
1436     struct fb_info *fb_info = dev_get_drvdata(fbdev);
1437     struct dlfb_data *dlfb = fb_info->par;
1438     return sysfs_emit(buf, "%u\n",
1439             atomic_read(&dlfb->bytes_identical));
1440 }
1441 
1442 static ssize_t metrics_bytes_sent_show(struct device *fbdev,
1443                    struct device_attribute *a, char *buf) {
1444     struct fb_info *fb_info = dev_get_drvdata(fbdev);
1445     struct dlfb_data *dlfb = fb_info->par;
1446     return sysfs_emit(buf, "%u\n",
1447             atomic_read(&dlfb->bytes_sent));
1448 }
1449 
1450 static ssize_t metrics_cpu_kcycles_used_show(struct device *fbdev,
1451                    struct device_attribute *a, char *buf) {
1452     struct fb_info *fb_info = dev_get_drvdata(fbdev);
1453     struct dlfb_data *dlfb = fb_info->par;
1454     return sysfs_emit(buf, "%u\n",
1455             atomic_read(&dlfb->cpu_kcycles_used));
1456 }
1457 
1458 static ssize_t edid_show(
1459             struct file *filp,
1460             struct kobject *kobj, struct bin_attribute *a,
1461              char *buf, loff_t off, size_t count) {
1462     struct device *fbdev = kobj_to_dev(kobj);
1463     struct fb_info *fb_info = dev_get_drvdata(fbdev);
1464     struct dlfb_data *dlfb = fb_info->par;
1465 
1466     if (dlfb->edid == NULL)
1467         return 0;
1468 
1469     if ((off >= dlfb->edid_size) || (count > dlfb->edid_size))
1470         return 0;
1471 
1472     if (off + count > dlfb->edid_size)
1473         count = dlfb->edid_size - off;
1474 
1475     memcpy(buf, dlfb->edid, count);
1476 
1477     return count;
1478 }
1479 
1480 static ssize_t edid_store(
1481             struct file *filp,
1482             struct kobject *kobj, struct bin_attribute *a,
1483             char *src, loff_t src_off, size_t src_size) {
1484     struct device *fbdev = kobj_to_dev(kobj);
1485     struct fb_info *fb_info = dev_get_drvdata(fbdev);
1486     struct dlfb_data *dlfb = fb_info->par;
1487     int ret;
1488 
1489     /* We only support write of entire EDID at once, no offset*/
1490     if ((src_size != EDID_LENGTH) || (src_off != 0))
1491         return -EINVAL;
1492 
1493     ret = dlfb_setup_modes(dlfb, fb_info, src, src_size);
1494     if (ret)
1495         return ret;
1496 
1497     if (!dlfb->edid || memcmp(src, dlfb->edid, src_size))
1498         return -EINVAL;
1499 
1500     ret = dlfb_ops_set_par(fb_info);
1501     if (ret)
1502         return ret;
1503 
1504     return src_size;
1505 }
1506 
1507 static ssize_t metrics_reset_store(struct device *fbdev,
1508                struct device_attribute *attr,
1509                const char *buf, size_t count)
1510 {
1511     struct fb_info *fb_info = dev_get_drvdata(fbdev);
1512     struct dlfb_data *dlfb = fb_info->par;
1513 
1514     atomic_set(&dlfb->bytes_rendered, 0);
1515     atomic_set(&dlfb->bytes_identical, 0);
1516     atomic_set(&dlfb->bytes_sent, 0);
1517     atomic_set(&dlfb->cpu_kcycles_used, 0);
1518 
1519     return count;
1520 }
1521 
1522 static const struct bin_attribute edid_attr = {
1523     .attr.name = "edid",
1524     .attr.mode = 0666,
1525     .size = EDID_LENGTH,
1526     .read = edid_show,
1527     .write = edid_store
1528 };
1529 
1530 static const struct device_attribute fb_device_attrs[] = {
1531     __ATTR_RO(metrics_bytes_rendered),
1532     __ATTR_RO(metrics_bytes_identical),
1533     __ATTR_RO(metrics_bytes_sent),
1534     __ATTR_RO(metrics_cpu_kcycles_used),
1535     __ATTR(metrics_reset, S_IWUSR, NULL, metrics_reset_store),
1536 };
1537 
1538 /*
1539  * This is necessary before we can communicate with the display controller.
1540  */
1541 static int dlfb_select_std_channel(struct dlfb_data *dlfb)
1542 {
1543     int ret;
1544     void *buf;
1545     static const u8 set_def_chn[] = {
1546                 0x57, 0xCD, 0xDC, 0xA7,
1547                 0x1C, 0x88, 0x5E, 0x15,
1548                 0x60, 0xFE, 0xC6, 0x97,
1549                 0x16, 0x3D, 0x47, 0xF2  };
1550 
1551     buf = kmemdup(set_def_chn, sizeof(set_def_chn), GFP_KERNEL);
1552 
1553     if (!buf)
1554         return -ENOMEM;
1555 
1556     ret = usb_control_msg(dlfb->udev, usb_sndctrlpipe(dlfb->udev, 0),
1557             NR_USB_REQUEST_CHANNEL,
1558             (USB_DIR_OUT | USB_TYPE_VENDOR), 0, 0,
1559             buf, sizeof(set_def_chn), USB_CTRL_SET_TIMEOUT);
1560 
1561     kfree(buf);
1562 
1563     return ret;
1564 }
1565 
1566 static int dlfb_parse_vendor_descriptor(struct dlfb_data *dlfb,
1567                     struct usb_interface *intf)
1568 {
1569     char *desc;
1570     char *buf;
1571     char *desc_end;
1572     int total_len;
1573 
1574     buf = kzalloc(MAX_VENDOR_DESCRIPTOR_SIZE, GFP_KERNEL);
1575     if (!buf)
1576         return false;
1577     desc = buf;
1578 
1579     total_len = usb_get_descriptor(interface_to_usbdev(intf),
1580                     0x5f, /* vendor specific */
1581                     0, desc, MAX_VENDOR_DESCRIPTOR_SIZE);
1582 
1583     /* if not found, look in configuration descriptor */
1584     if (total_len < 0) {
1585         if (0 == usb_get_extra_descriptor(intf->cur_altsetting,
1586             0x5f, &desc))
1587             total_len = (int) desc[0];
1588     }
1589 
1590     if (total_len > 5) {
1591         dev_info(&intf->dev,
1592              "vendor descriptor length: %d data: %11ph\n",
1593              total_len, desc);
1594 
1595         if ((desc[0] != total_len) || /* descriptor length */
1596             (desc[1] != 0x5f) ||   /* vendor descriptor type */
1597             (desc[2] != 0x01) ||   /* version (2 bytes) */
1598             (desc[3] != 0x00) ||
1599             (desc[4] != total_len - 2)) /* length after type */
1600             goto unrecognized;
1601 
1602         desc_end = desc + total_len;
1603         desc += 5; /* the fixed header we've already parsed */
1604 
1605         while (desc < desc_end) {
1606             u8 length;
1607             u16 key;
1608 
1609             key = *desc++;
1610             key |= (u16)*desc++ << 8;
1611             length = *desc++;
1612 
1613             switch (key) {
1614             case 0x0200: { /* max_area */
1615                 u32 max_area = *desc++;
1616                 max_area |= (u32)*desc++ << 8;
1617                 max_area |= (u32)*desc++ << 16;
1618                 max_area |= (u32)*desc++ << 24;
1619                 dev_warn(&intf->dev,
1620                      "DL chip limited to %d pixel modes\n",
1621                      max_area);
1622                 dlfb->sku_pixel_limit = max_area;
1623                 break;
1624             }
1625             default:
1626                 break;
1627             }
1628             desc += length;
1629         }
1630     } else {
1631         dev_info(&intf->dev, "vendor descriptor not available (%d)\n",
1632              total_len);
1633     }
1634 
1635     goto success;
1636 
1637 unrecognized:
1638     /* allow udlfb to load for now even if firmware unrecognized */
1639     dev_err(&intf->dev, "Unrecognized vendor firmware descriptor\n");
1640 
1641 success:
1642     kfree(buf);
1643     return true;
1644 }
1645 
1646 static int dlfb_usb_probe(struct usb_interface *intf,
1647               const struct usb_device_id *id)
1648 {
1649     int i;
1650     const struct device_attribute *attr;
1651     struct dlfb_data *dlfb;
1652     struct fb_info *info;
1653     int retval;
1654     struct usb_device *usbdev = interface_to_usbdev(intf);
1655     struct usb_endpoint_descriptor *out;
1656 
1657     /* usb initialization */
1658     dlfb = kzalloc(sizeof(*dlfb), GFP_KERNEL);
1659     if (!dlfb) {
1660         dev_err(&intf->dev, "%s: failed to allocate dlfb\n", __func__);
1661         return -ENOMEM;
1662     }
1663 
1664     INIT_LIST_HEAD(&dlfb->deferred_free);
1665 
1666     dlfb->udev = usb_get_dev(usbdev);
1667     usb_set_intfdata(intf, dlfb);
1668 
1669     retval = usb_find_common_endpoints(intf->cur_altsetting, NULL, &out, NULL, NULL);
1670     if (retval) {
1671         dev_err(&intf->dev, "Device should have at lease 1 bulk endpoint!\n");
1672         goto error;
1673     }
1674 
1675     dev_dbg(&intf->dev, "console enable=%d\n", console);
1676     dev_dbg(&intf->dev, "fb_defio enable=%d\n", fb_defio);
1677     dev_dbg(&intf->dev, "shadow enable=%d\n", shadow);
1678 
1679     dlfb->sku_pixel_limit = 2048 * 1152; /* default to maximum */
1680 
1681     if (!dlfb_parse_vendor_descriptor(dlfb, intf)) {
1682         dev_err(&intf->dev,
1683             "firmware not recognized, incompatible device?\n");
1684         retval = -ENODEV;
1685         goto error;
1686     }
1687 
1688     if (pixel_limit) {
1689         dev_warn(&intf->dev,
1690              "DL chip limit of %d overridden to %d\n",
1691              dlfb->sku_pixel_limit, pixel_limit);
1692         dlfb->sku_pixel_limit = pixel_limit;
1693     }
1694 
1695 
1696     /* allocates framebuffer driver structure, not framebuffer memory */
1697     info = framebuffer_alloc(0, &dlfb->udev->dev);
1698     if (!info) {
1699         retval = -ENOMEM;
1700         goto error;
1701     }
1702 
1703     dlfb->info = info;
1704     info->par = dlfb;
1705     info->pseudo_palette = dlfb->pseudo_palette;
1706     dlfb->ops = dlfb_ops;
1707     info->fbops = &dlfb->ops;
1708 
1709     mutex_init(&dlfb->render_mutex);
1710     dlfb_init_damage(dlfb);
1711     spin_lock_init(&dlfb->damage_lock);
1712     INIT_WORK(&dlfb->damage_work, dlfb_damage_work);
1713 
1714     INIT_LIST_HEAD(&info->modelist);
1715 
1716     if (!dlfb_alloc_urb_list(dlfb, WRITES_IN_FLIGHT, MAX_TRANSFER)) {
1717         retval = -ENOMEM;
1718         dev_err(&intf->dev, "unable to allocate urb list\n");
1719         goto error;
1720     }
1721 
1722     /* We don't register a new USB class. Our client interface is dlfbev */
1723 
1724     retval = fb_alloc_cmap(&info->cmap, 256, 0);
1725     if (retval < 0) {
1726         dev_err(info->device, "cmap allocation failed: %d\n", retval);
1727         goto error;
1728     }
1729 
1730     retval = dlfb_setup_modes(dlfb, info, NULL, 0);
1731     if (retval != 0) {
1732         dev_err(info->device,
1733             "unable to find common mode for display and adapter\n");
1734         goto error;
1735     }
1736 
1737     /* ready to begin using device */
1738 
1739     atomic_set(&dlfb->usb_active, 1);
1740     dlfb_select_std_channel(dlfb);
1741 
1742     dlfb_ops_check_var(&info->var, info);
1743     retval = dlfb_ops_set_par(info);
1744     if (retval)
1745         goto error;
1746 
1747     retval = register_framebuffer(info);
1748     if (retval < 0) {
1749         dev_err(info->device, "unable to register framebuffer: %d\n",
1750             retval);
1751         goto error;
1752     }
1753 
1754     for (i = 0; i < ARRAY_SIZE(fb_device_attrs); i++) {
1755         attr = &fb_device_attrs[i];
1756         retval = device_create_file(info->dev, attr);
1757         if (retval)
1758             dev_warn(info->device,
1759                  "failed to create '%s' attribute: %d\n",
1760                  attr->attr.name, retval);
1761     }
1762 
1763     retval = device_create_bin_file(info->dev, &edid_attr);
1764     if (retval)
1765         dev_warn(info->device, "failed to create '%s' attribute: %d\n",
1766              edid_attr.attr.name, retval);
1767 
1768     dev_info(info->device,
1769          "%s is DisplayLink USB device (%dx%d, %dK framebuffer memory)\n",
1770          dev_name(info->dev), info->var.xres, info->var.yres,
1771          ((dlfb->backing_buffer) ?
1772          info->fix.smem_len * 2 : info->fix.smem_len) >> 10);
1773     return 0;
1774 
1775 error:
1776     if (dlfb->info) {
1777         dlfb_ops_destroy(dlfb->info);
1778     } else {
1779         usb_put_dev(dlfb->udev);
1780         kfree(dlfb);
1781     }
1782     return retval;
1783 }
1784 
1785 static void dlfb_usb_disconnect(struct usb_interface *intf)
1786 {
1787     struct dlfb_data *dlfb;
1788     struct fb_info *info;
1789     int i;
1790 
1791     dlfb = usb_get_intfdata(intf);
1792     info = dlfb->info;
1793 
1794     dev_dbg(&intf->dev, "USB disconnect starting\n");
1795 
1796     /* we virtualize until all fb clients release. Then we free */
1797     dlfb->virtualized = true;
1798 
1799     /* When non-active we'll update virtual framebuffer, but no new urbs */
1800     atomic_set(&dlfb->usb_active, 0);
1801 
1802     /* this function will wait for all in-flight urbs to complete */
1803     dlfb_free_urb_list(dlfb);
1804 
1805     /* remove udlfb's sysfs interfaces */
1806     for (i = 0; i < ARRAY_SIZE(fb_device_attrs); i++)
1807         device_remove_file(info->dev, &fb_device_attrs[i]);
1808     device_remove_bin_file(info->dev, &edid_attr);
1809 
1810     unregister_framebuffer(info);
1811 }
1812 
1813 static struct usb_driver dlfb_driver = {
1814     .name = "udlfb",
1815     .probe = dlfb_usb_probe,
1816     .disconnect = dlfb_usb_disconnect,
1817     .id_table = id_table,
1818 };
1819 
1820 module_usb_driver(dlfb_driver);
1821 
1822 static void dlfb_urb_completion(struct urb *urb)
1823 {
1824     struct urb_node *unode = urb->context;
1825     struct dlfb_data *dlfb = unode->dlfb;
1826     unsigned long flags;
1827 
1828     switch (urb->status) {
1829     case 0:
1830         /* success */
1831         break;
1832     case -ECONNRESET:
1833     case -ENOENT:
1834     case -ESHUTDOWN:
1835         /* sync/async unlink faults aren't errors */
1836         break;
1837     default:
1838         dev_err(&dlfb->udev->dev,
1839             "%s - nonzero write bulk status received: %d\n",
1840             __func__, urb->status);
1841         atomic_set(&dlfb->lost_pixels, 1);
1842         break;
1843     }
1844 
1845     urb->transfer_buffer_length = dlfb->urbs.size; /* reset to actual */
1846 
1847     spin_lock_irqsave(&dlfb->urbs.lock, flags);
1848     list_add_tail(&unode->entry, &dlfb->urbs.list);
1849     dlfb->urbs.available++;
1850     spin_unlock_irqrestore(&dlfb->urbs.lock, flags);
1851 
1852     up(&dlfb->urbs.limit_sem);
1853 }
1854 
1855 static void dlfb_free_urb_list(struct dlfb_data *dlfb)
1856 {
1857     int count = dlfb->urbs.count;
1858     struct list_head *node;
1859     struct urb_node *unode;
1860     struct urb *urb;
1861 
1862     /* keep waiting and freeing, until we've got 'em all */
1863     while (count--) {
1864         down(&dlfb->urbs.limit_sem);
1865 
1866         spin_lock_irq(&dlfb->urbs.lock);
1867 
1868         node = dlfb->urbs.list.next; /* have reserved one with sem */
1869         list_del_init(node);
1870 
1871         spin_unlock_irq(&dlfb->urbs.lock);
1872 
1873         unode = list_entry(node, struct urb_node, entry);
1874         urb = unode->urb;
1875 
1876         /* Free each separately allocated piece */
1877         usb_free_coherent(urb->dev, dlfb->urbs.size,
1878                   urb->transfer_buffer, urb->transfer_dma);
1879         usb_free_urb(urb);
1880         kfree(node);
1881     }
1882 
1883     dlfb->urbs.count = 0;
1884 }
1885 
1886 static int dlfb_alloc_urb_list(struct dlfb_data *dlfb, int count, size_t size)
1887 {
1888     struct urb *urb;
1889     struct urb_node *unode;
1890     char *buf;
1891     size_t wanted_size = count * size;
1892 
1893     spin_lock_init(&dlfb->urbs.lock);
1894 
1895 retry:
1896     dlfb->urbs.size = size;
1897     INIT_LIST_HEAD(&dlfb->urbs.list);
1898 
1899     sema_init(&dlfb->urbs.limit_sem, 0);
1900     dlfb->urbs.count = 0;
1901     dlfb->urbs.available = 0;
1902 
1903     while (dlfb->urbs.count * size < wanted_size) {
1904         unode = kzalloc(sizeof(*unode), GFP_KERNEL);
1905         if (!unode)
1906             break;
1907         unode->dlfb = dlfb;
1908 
1909         urb = usb_alloc_urb(0, GFP_KERNEL);
1910         if (!urb) {
1911             kfree(unode);
1912             break;
1913         }
1914         unode->urb = urb;
1915 
1916         buf = usb_alloc_coherent(dlfb->udev, size, GFP_KERNEL,
1917                      &urb->transfer_dma);
1918         if (!buf) {
1919             kfree(unode);
1920             usb_free_urb(urb);
1921             if (size > PAGE_SIZE) {
1922                 size /= 2;
1923                 dlfb_free_urb_list(dlfb);
1924                 goto retry;
1925             }
1926             break;
1927         }
1928 
1929         /* urb->transfer_buffer_length set to actual before submit */
1930         usb_fill_bulk_urb(urb, dlfb->udev, usb_sndbulkpipe(dlfb->udev, 1),
1931             buf, size, dlfb_urb_completion, unode);
1932         urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1933 
1934         list_add_tail(&unode->entry, &dlfb->urbs.list);
1935 
1936         up(&dlfb->urbs.limit_sem);
1937         dlfb->urbs.count++;
1938         dlfb->urbs.available++;
1939     }
1940 
1941     return dlfb->urbs.count;
1942 }
1943 
1944 static struct urb *dlfb_get_urb(struct dlfb_data *dlfb)
1945 {
1946     int ret;
1947     struct list_head *entry;
1948     struct urb_node *unode;
1949 
1950     /* Wait for an in-flight buffer to complete and get re-queued */
1951     ret = down_timeout(&dlfb->urbs.limit_sem, GET_URB_TIMEOUT);
1952     if (ret) {
1953         atomic_set(&dlfb->lost_pixels, 1);
1954         dev_warn(&dlfb->udev->dev,
1955              "wait for urb interrupted: %d available: %d\n",
1956              ret, dlfb->urbs.available);
1957         return NULL;
1958     }
1959 
1960     spin_lock_irq(&dlfb->urbs.lock);
1961 
1962     BUG_ON(list_empty(&dlfb->urbs.list)); /* reserved one with limit_sem */
1963     entry = dlfb->urbs.list.next;
1964     list_del_init(entry);
1965     dlfb->urbs.available--;
1966 
1967     spin_unlock_irq(&dlfb->urbs.lock);
1968 
1969     unode = list_entry(entry, struct urb_node, entry);
1970     return unode->urb;
1971 }
1972 
1973 static int dlfb_submit_urb(struct dlfb_data *dlfb, struct urb *urb, size_t len)
1974 {
1975     int ret;
1976 
1977     BUG_ON(len > dlfb->urbs.size);
1978 
1979     urb->transfer_buffer_length = len; /* set to actual payload len */
1980     ret = usb_submit_urb(urb, GFP_KERNEL);
1981     if (ret) {
1982         dlfb_urb_completion(urb); /* because no one else will */
1983         atomic_set(&dlfb->lost_pixels, 1);
1984         dev_err(&dlfb->udev->dev, "submit urb error: %d\n", ret);
1985     }
1986     return ret;
1987 }
1988 
1989 module_param(console, bool, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
1990 MODULE_PARM_DESC(console, "Allow fbcon to open framebuffer");
1991 
1992 module_param(fb_defio, bool, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
1993 MODULE_PARM_DESC(fb_defio, "Page fault detection of mmap writes");
1994 
1995 module_param(shadow, bool, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
1996 MODULE_PARM_DESC(shadow, "Shadow vid mem. Disable to save mem but lose perf");
1997 
1998 module_param(pixel_limit, int, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
1999 MODULE_PARM_DESC(pixel_limit, "Force limit on max mode (in x*y pixels)");
2000 
2001 MODULE_AUTHOR("Roberto De Ioris <roberto@unbit.it>, "
2002           "Jaya Kumar <jayakumar.lkml@gmail.com>, "
2003           "Bernie Thompson <bernie@plugable.com>");
2004 MODULE_DESCRIPTION("DisplayLink kernel framebuffer driver");
2005 MODULE_LICENSE("GPL");
2006