GNU Linux-libre 4.19.304-gnu1
[releases.git] / drivers / video / fbdev / udlfb.c
1 /*
2  * udlfb.c -- Framebuffer driver for DisplayLink USB controller
3  *
4  * Copyright (C) 2009 Roberto De Ioris <roberto@unbit.it>
5  * Copyright (C) 2009 Jaya Kumar <jayakumar.lkml@gmail.com>
6  * Copyright (C) 2009 Bernie Thompson <bernie@plugable.com>
7  *
8  * This file is subject to the terms and conditions of the GNU General Public
9  * License v2. See the file COPYING in the main directory of this archive for
10  * more details.
11  *
12  * Layout is based on skeletonfb by James Simmons and Geert Uytterhoeven,
13  * usb-skeleton by GregKH.
14  *
15  * Device-specific portions based on information from Displaylink, with work
16  * from Florian Echtler, Henrik Bjerregaard Pedersen, and others.
17  */
18
19 #include <linux/module.h>
20 #include <linux/kernel.h>
21 #include <linux/init.h>
22 #include <linux/usb.h>
23 #include <linux/uaccess.h>
24 #include <linux/mm.h>
25 #include <linux/fb.h>
26 #include <linux/vmalloc.h>
27 #include <linux/slab.h>
28 #include <linux/delay.h>
29 #include <asm/unaligned.h>
30 #include <video/udlfb.h>
31 #include "edid.h"
32
33 static const struct fb_fix_screeninfo dlfb_fix = {
34         .id =           "udlfb",
35         .type =         FB_TYPE_PACKED_PIXELS,
36         .visual =       FB_VISUAL_TRUECOLOR,
37         .xpanstep =     0,
38         .ypanstep =     0,
39         .ywrapstep =    0,
40         .accel =        FB_ACCEL_NONE,
41 };
42
43 static const u32 udlfb_info_flags = FBINFO_DEFAULT | FBINFO_READS_FAST |
44                 FBINFO_VIRTFB |
45                 FBINFO_HWACCEL_IMAGEBLIT | FBINFO_HWACCEL_FILLRECT |
46                 FBINFO_HWACCEL_COPYAREA | FBINFO_MISC_ALWAYS_SETPAR;
47
48 /*
49  * There are many DisplayLink-based graphics products, all with unique PIDs.
50  * So we match on DisplayLink's VID + Vendor-Defined Interface Class (0xff)
51  * We also require a match on SubClass (0x00) and Protocol (0x00),
52  * which is compatible with all known USB 2.0 era graphics chips and firmware,
53  * but allows DisplayLink to increment those for any future incompatible chips
54  */
55 static const struct usb_device_id id_table[] = {
56         {.idVendor = 0x17e9,
57          .bInterfaceClass = 0xff,
58          .bInterfaceSubClass = 0x00,
59          .bInterfaceProtocol = 0x00,
60          .match_flags = USB_DEVICE_ID_MATCH_VENDOR |
61                 USB_DEVICE_ID_MATCH_INT_CLASS |
62                 USB_DEVICE_ID_MATCH_INT_SUBCLASS |
63                 USB_DEVICE_ID_MATCH_INT_PROTOCOL,
64         },
65         {},
66 };
67 MODULE_DEVICE_TABLE(usb, id_table);
68
69 /* module options */
70 static bool console = 1; /* Allow fbcon to open framebuffer */
71 static bool fb_defio = 1;  /* Detect mmap writes using page faults */
72 static bool shadow = 1; /* Optionally disable shadow framebuffer */
73 static int pixel_limit; /* Optionally force a pixel resolution limit */
74
75 struct dlfb_deferred_free {
76         struct list_head list;
77         void *mem;
78 };
79
80 static int dlfb_realloc_framebuffer(struct dlfb_data *dlfb, struct fb_info *info, u32 new_len);
81
82 /* dlfb keeps a list of urbs for efficient bulk transfers */
83 static void dlfb_urb_completion(struct urb *urb);
84 static struct urb *dlfb_get_urb(struct dlfb_data *dlfb);
85 static int dlfb_submit_urb(struct dlfb_data *dlfb, struct urb * urb, size_t len);
86 static int dlfb_alloc_urb_list(struct dlfb_data *dlfb, int count, size_t size);
87 static void dlfb_free_urb_list(struct dlfb_data *dlfb);
88
89 /*
90  * All DisplayLink bulk operations start with 0xAF, followed by specific code
91  * All operations are written to buffers which then later get sent to device
92  */
93 static char *dlfb_set_register(char *buf, u8 reg, u8 val)
94 {
95         *buf++ = 0xAF;
96         *buf++ = 0x20;
97         *buf++ = reg;
98         *buf++ = val;
99         return buf;
100 }
101
102 static char *dlfb_vidreg_lock(char *buf)
103 {
104         return dlfb_set_register(buf, 0xFF, 0x00);
105 }
106
107 static char *dlfb_vidreg_unlock(char *buf)
108 {
109         return dlfb_set_register(buf, 0xFF, 0xFF);
110 }
111
112 /*
113  * Map FB_BLANK_* to DisplayLink register
114  * DLReg FB_BLANK_*
115  * ----- -----------------------------
116  *  0x00 FB_BLANK_UNBLANK (0)
117  *  0x01 FB_BLANK (1)
118  *  0x03 FB_BLANK_VSYNC_SUSPEND (2)
119  *  0x05 FB_BLANK_HSYNC_SUSPEND (3)
120  *  0x07 FB_BLANK_POWERDOWN (4) Note: requires modeset to come back
121  */
122 static char *dlfb_blanking(char *buf, int fb_blank)
123 {
124         u8 reg;
125
126         switch (fb_blank) {
127         case FB_BLANK_POWERDOWN:
128                 reg = 0x07;
129                 break;
130         case FB_BLANK_HSYNC_SUSPEND:
131                 reg = 0x05;
132                 break;
133         case FB_BLANK_VSYNC_SUSPEND:
134                 reg = 0x03;
135                 break;
136         case FB_BLANK_NORMAL:
137                 reg = 0x01;
138                 break;
139         default:
140                 reg = 0x00;
141         }
142
143         buf = dlfb_set_register(buf, 0x1F, reg);
144
145         return buf;
146 }
147
148 static char *dlfb_set_color_depth(char *buf, u8 selection)
149 {
150         return dlfb_set_register(buf, 0x00, selection);
151 }
152
153 static char *dlfb_set_base16bpp(char *wrptr, u32 base)
154 {
155         /* the base pointer is 16 bits wide, 0x20 is hi byte. */
156         wrptr = dlfb_set_register(wrptr, 0x20, base >> 16);
157         wrptr = dlfb_set_register(wrptr, 0x21, base >> 8);
158         return dlfb_set_register(wrptr, 0x22, base);
159 }
160
161 /*
162  * DisplayLink HW has separate 16bpp and 8bpp framebuffers.
163  * In 24bpp modes, the low 323 RGB bits go in the 8bpp framebuffer
164  */
165 static char *dlfb_set_base8bpp(char *wrptr, u32 base)
166 {
167         wrptr = dlfb_set_register(wrptr, 0x26, base >> 16);
168         wrptr = dlfb_set_register(wrptr, 0x27, base >> 8);
169         return dlfb_set_register(wrptr, 0x28, base);
170 }
171
172 static char *dlfb_set_register_16(char *wrptr, u8 reg, u16 value)
173 {
174         wrptr = dlfb_set_register(wrptr, reg, value >> 8);
175         return dlfb_set_register(wrptr, reg+1, value);
176 }
177
178 /*
179  * This is kind of weird because the controller takes some
180  * register values in a different byte order than other registers.
181  */
182 static char *dlfb_set_register_16be(char *wrptr, u8 reg, u16 value)
183 {
184         wrptr = dlfb_set_register(wrptr, reg, value);
185         return dlfb_set_register(wrptr, reg+1, value >> 8);
186 }
187
188 /*
189  * LFSR is linear feedback shift register. The reason we have this is
190  * because the display controller needs to minimize the clock depth of
191  * various counters used in the display path. So this code reverses the
192  * provided value into the lfsr16 value by counting backwards to get
193  * the value that needs to be set in the hardware comparator to get the
194  * same actual count. This makes sense once you read above a couple of
195  * times and think about it from a hardware perspective.
196  */
197 static u16 dlfb_lfsr16(u16 actual_count)
198 {
199         u32 lv = 0xFFFF; /* This is the lfsr value that the hw starts with */
200
201         while (actual_count--) {
202                 lv =     ((lv << 1) |
203                         (((lv >> 15) ^ (lv >> 4) ^ (lv >> 2) ^ (lv >> 1)) & 1))
204                         & 0xFFFF;
205         }
206
207         return (u16) lv;
208 }
209
210 /*
211  * This does LFSR conversion on the value that is to be written.
212  * See LFSR explanation above for more detail.
213  */
214 static char *dlfb_set_register_lfsr16(char *wrptr, u8 reg, u16 value)
215 {
216         return dlfb_set_register_16(wrptr, reg, dlfb_lfsr16(value));
217 }
218
219 /*
220  * This takes a standard fbdev screeninfo struct and all of its monitor mode
221  * details and converts them into the DisplayLink equivalent register commands.
222  */
223 static char *dlfb_set_vid_cmds(char *wrptr, struct fb_var_screeninfo *var)
224 {
225         u16 xds, yds;
226         u16 xde, yde;
227         u16 yec;
228
229         /* x display start */
230         xds = var->left_margin + var->hsync_len;
231         wrptr = dlfb_set_register_lfsr16(wrptr, 0x01, xds);
232         /* x display end */
233         xde = xds + var->xres;
234         wrptr = dlfb_set_register_lfsr16(wrptr, 0x03, xde);
235
236         /* y display start */
237         yds = var->upper_margin + var->vsync_len;
238         wrptr = dlfb_set_register_lfsr16(wrptr, 0x05, yds);
239         /* y display end */
240         yde = yds + var->yres;
241         wrptr = dlfb_set_register_lfsr16(wrptr, 0x07, yde);
242
243         /* x end count is active + blanking - 1 */
244         wrptr = dlfb_set_register_lfsr16(wrptr, 0x09,
245                         xde + var->right_margin - 1);
246
247         /* libdlo hardcodes hsync start to 1 */
248         wrptr = dlfb_set_register_lfsr16(wrptr, 0x0B, 1);
249
250         /* hsync end is width of sync pulse + 1 */
251         wrptr = dlfb_set_register_lfsr16(wrptr, 0x0D, var->hsync_len + 1);
252
253         /* hpixels is active pixels */
254         wrptr = dlfb_set_register_16(wrptr, 0x0F, var->xres);
255
256         /* yendcount is vertical active + vertical blanking */
257         yec = var->yres + var->upper_margin + var->lower_margin +
258                         var->vsync_len;
259         wrptr = dlfb_set_register_lfsr16(wrptr, 0x11, yec);
260
261         /* libdlo hardcodes vsync start to 0 */
262         wrptr = dlfb_set_register_lfsr16(wrptr, 0x13, 0);
263
264         /* vsync end is width of vsync pulse */
265         wrptr = dlfb_set_register_lfsr16(wrptr, 0x15, var->vsync_len);
266
267         /* vpixels is active pixels */
268         wrptr = dlfb_set_register_16(wrptr, 0x17, var->yres);
269
270         /* convert picoseconds to 5kHz multiple for pclk5k = x * 1E12/5k */
271         wrptr = dlfb_set_register_16be(wrptr, 0x1B,
272                         200*1000*1000/var->pixclock);
273
274         return wrptr;
275 }
276
277 /*
278  * This takes a standard fbdev screeninfo struct that was fetched or prepared
279  * and then generates the appropriate command sequence that then drives the
280  * display controller.
281  */
282 static int dlfb_set_video_mode(struct dlfb_data *dlfb,
283                                 struct fb_var_screeninfo *var)
284 {
285         char *buf;
286         char *wrptr;
287         int retval;
288         int writesize;
289         struct urb *urb;
290
291         if (!atomic_read(&dlfb->usb_active))
292                 return -EPERM;
293
294         urb = dlfb_get_urb(dlfb);
295         if (!urb)
296                 return -ENOMEM;
297
298         buf = (char *) urb->transfer_buffer;
299
300         /*
301         * This first section has to do with setting the base address on the
302         * controller * associated with the display. There are 2 base
303         * pointers, currently, we only * use the 16 bpp segment.
304         */
305         wrptr = dlfb_vidreg_lock(buf);
306         wrptr = dlfb_set_color_depth(wrptr, 0x00);
307         /* set base for 16bpp segment to 0 */
308         wrptr = dlfb_set_base16bpp(wrptr, 0);
309         /* set base for 8bpp segment to end of fb */
310         wrptr = dlfb_set_base8bpp(wrptr, dlfb->info->fix.smem_len);
311
312         wrptr = dlfb_set_vid_cmds(wrptr, var);
313         wrptr = dlfb_blanking(wrptr, FB_BLANK_UNBLANK);
314         wrptr = dlfb_vidreg_unlock(wrptr);
315
316         writesize = wrptr - buf;
317
318         retval = dlfb_submit_urb(dlfb, urb, writesize);
319
320         dlfb->blank_mode = FB_BLANK_UNBLANK;
321
322         return retval;
323 }
324
325 static int dlfb_ops_mmap(struct fb_info *info, struct vm_area_struct *vma)
326 {
327         unsigned long start = vma->vm_start;
328         unsigned long size = vma->vm_end - vma->vm_start;
329         unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
330         unsigned long page, pos;
331
332         if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT))
333                 return -EINVAL;
334         if (size > info->fix.smem_len)
335                 return -EINVAL;
336         if (offset > info->fix.smem_len - size)
337                 return -EINVAL;
338
339         pos = (unsigned long)info->fix.smem_start + offset;
340
341         dev_dbg(info->dev, "mmap() framebuffer addr:%lu size:%lu\n",
342                 pos, size);
343
344         while (size > 0) {
345                 page = vmalloc_to_pfn((void *)pos);
346                 if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED))
347                         return -EAGAIN;
348
349                 start += PAGE_SIZE;
350                 pos += PAGE_SIZE;
351                 if (size > PAGE_SIZE)
352                         size -= PAGE_SIZE;
353                 else
354                         size = 0;
355         }
356
357         return 0;
358 }
359
360 /*
361  * Trims identical data from front and back of line
362  * Sets new front buffer address and width
363  * And returns byte count of identical pixels
364  * Assumes CPU natural alignment (unsigned long)
365  * for back and front buffer ptrs and width
366  */
367 static int dlfb_trim_hline(const u8 *bback, const u8 **bfront, int *width_bytes)
368 {
369         int j, k;
370         const unsigned long *back = (const unsigned long *) bback;
371         const unsigned long *front = (const unsigned long *) *bfront;
372         const int width = *width_bytes / sizeof(unsigned long);
373         int identical = width;
374         int start = width;
375         int end = width;
376
377         for (j = 0; j < width; j++) {
378                 if (back[j] != front[j]) {
379                         start = j;
380                         break;
381                 }
382         }
383
384         for (k = width - 1; k > j; k--) {
385                 if (back[k] != front[k]) {
386                         end = k+1;
387                         break;
388                 }
389         }
390
391         identical = start + (width - end);
392         *bfront = (u8 *) &front[start];
393         *width_bytes = (end - start) * sizeof(unsigned long);
394
395         return identical * sizeof(unsigned long);
396 }
397
398 /*
399  * Render a command stream for an encoded horizontal line segment of pixels.
400  *
401  * A command buffer holds several commands.
402  * It always begins with a fresh command header
403  * (the protocol doesn't require this, but we enforce it to allow
404  * multiple buffers to be potentially encoded and sent in parallel).
405  * A single command encodes one contiguous horizontal line of pixels
406  *
407  * The function relies on the client to do all allocation, so that
408  * rendering can be done directly to output buffers (e.g. USB URBs).
409  * The function fills the supplied command buffer, providing information
410  * on where it left off, so the client may call in again with additional
411  * buffers if the line will take several buffers to complete.
412  *
413  * A single command can transmit a maximum of 256 pixels,
414  * regardless of the compression ratio (protocol design limit).
415  * To the hardware, 0 for a size byte means 256
416  *
417  * Rather than 256 pixel commands which are either rl or raw encoded,
418  * the rlx command simply assumes alternating raw and rl spans within one cmd.
419  * This has a slightly larger header overhead, but produces more even results.
420  * It also processes all data (read and write) in a single pass.
421  * Performance benchmarks of common cases show it having just slightly better
422  * compression than 256 pixel raw or rle commands, with similar CPU consumpion.
423  * But for very rl friendly data, will compress not quite as well.
424  */
425 static void dlfb_compress_hline(
426         const uint16_t **pixel_start_ptr,
427         const uint16_t *const pixel_end,
428         uint32_t *device_address_ptr,
429         uint8_t **command_buffer_ptr,
430         const uint8_t *const cmd_buffer_end,
431         unsigned long back_buffer_offset,
432         int *ident_ptr)
433 {
434         const uint16_t *pixel = *pixel_start_ptr;
435         uint32_t dev_addr  = *device_address_ptr;
436         uint8_t *cmd = *command_buffer_ptr;
437
438         while ((pixel_end > pixel) &&
439                (cmd_buffer_end - MIN_RLX_CMD_BYTES > cmd)) {
440                 uint8_t *raw_pixels_count_byte = NULL;
441                 uint8_t *cmd_pixels_count_byte = NULL;
442                 const uint16_t *raw_pixel_start = NULL;
443                 const uint16_t *cmd_pixel_start, *cmd_pixel_end = NULL;
444
445                 if (back_buffer_offset &&
446                     *pixel == *(u16 *)((u8 *)pixel + back_buffer_offset)) {
447                         pixel++;
448                         dev_addr += BPP;
449                         (*ident_ptr)++;
450                         continue;
451                 }
452
453                 *cmd++ = 0xAF;
454                 *cmd++ = 0x6B;
455                 *cmd++ = dev_addr >> 16;
456                 *cmd++ = dev_addr >> 8;
457                 *cmd++ = dev_addr;
458
459                 cmd_pixels_count_byte = cmd++; /*  we'll know this later */
460                 cmd_pixel_start = pixel;
461
462                 raw_pixels_count_byte = cmd++; /*  we'll know this later */
463                 raw_pixel_start = pixel;
464
465                 cmd_pixel_end = pixel + min3(MAX_CMD_PIXELS + 1UL,
466                                         (unsigned long)(pixel_end - pixel),
467                                         (unsigned long)(cmd_buffer_end - 1 - cmd) / BPP);
468
469                 if (back_buffer_offset) {
470                         /* note: the framebuffer may change under us, so we must test for underflow */
471                         while (cmd_pixel_end - 1 > pixel &&
472                                *(cmd_pixel_end - 1) == *(u16 *)((u8 *)(cmd_pixel_end - 1) + back_buffer_offset))
473                                 cmd_pixel_end--;
474                 }
475
476                 while (pixel < cmd_pixel_end) {
477                         const uint16_t * const repeating_pixel = pixel;
478                         u16 pixel_value = *pixel;
479
480                         put_unaligned_be16(pixel_value, cmd);
481                         if (back_buffer_offset)
482                                 *(u16 *)((u8 *)pixel + back_buffer_offset) = pixel_value;
483                         cmd += 2;
484                         pixel++;
485
486                         if (unlikely((pixel < cmd_pixel_end) &&
487                                      (*pixel == pixel_value))) {
488                                 /* go back and fill in raw pixel count */
489                                 *raw_pixels_count_byte = ((repeating_pixel -
490                                                 raw_pixel_start) + 1) & 0xFF;
491
492                                 do {
493                                         if (back_buffer_offset)
494                                                 *(u16 *)((u8 *)pixel + back_buffer_offset) = pixel_value;
495                                         pixel++;
496                                 } while ((pixel < cmd_pixel_end) &&
497                                          (*pixel == pixel_value));
498
499                                 /* immediately after raw data is repeat byte */
500                                 *cmd++ = ((pixel - repeating_pixel) - 1) & 0xFF;
501
502                                 /* Then start another raw pixel span */
503                                 raw_pixel_start = pixel;
504                                 raw_pixels_count_byte = cmd++;
505                         }
506                 }
507
508                 if (pixel > raw_pixel_start) {
509                         /* finalize last RAW span */
510                         *raw_pixels_count_byte = (pixel-raw_pixel_start) & 0xFF;
511                 } else {
512                         /* undo unused byte */
513                         cmd--;
514                 }
515
516                 *cmd_pixels_count_byte = (pixel - cmd_pixel_start) & 0xFF;
517                 dev_addr += (u8 *)pixel - (u8 *)cmd_pixel_start;
518         }
519
520         if (cmd_buffer_end - MIN_RLX_CMD_BYTES <= cmd) {
521                 /* Fill leftover bytes with no-ops */
522                 if (cmd_buffer_end > cmd)
523                         memset(cmd, 0xAF, cmd_buffer_end - cmd);
524                 cmd = (uint8_t *) cmd_buffer_end;
525         }
526
527         *command_buffer_ptr = cmd;
528         *pixel_start_ptr = pixel;
529         *device_address_ptr = dev_addr;
530 }
531
532 /*
533  * There are 3 copies of every pixel: The front buffer that the fbdev
534  * client renders to, the actual framebuffer across the USB bus in hardware
535  * (that we can only write to, slowly, and can never read), and (optionally)
536  * our shadow copy that tracks what's been sent to that hardware buffer.
537  */
538 static int dlfb_render_hline(struct dlfb_data *dlfb, struct urb **urb_ptr,
539                               const char *front, char **urb_buf_ptr,
540                               u32 byte_offset, u32 byte_width,
541                               int *ident_ptr, int *sent_ptr)
542 {
543         const u8 *line_start, *line_end, *next_pixel;
544         u32 dev_addr = dlfb->base16 + byte_offset;
545         struct urb *urb = *urb_ptr;
546         u8 *cmd = *urb_buf_ptr;
547         u8 *cmd_end = (u8 *) urb->transfer_buffer + urb->transfer_buffer_length;
548         unsigned long back_buffer_offset = 0;
549
550         line_start = (u8 *) (front + byte_offset);
551         next_pixel = line_start;
552         line_end = next_pixel + byte_width;
553
554         if (dlfb->backing_buffer) {
555                 int offset;
556                 const u8 *back_start = (u8 *) (dlfb->backing_buffer
557                                                 + byte_offset);
558
559                 back_buffer_offset = (unsigned long)back_start - (unsigned long)line_start;
560
561                 *ident_ptr += dlfb_trim_hline(back_start, &next_pixel,
562                         &byte_width);
563
564                 offset = next_pixel - line_start;
565                 line_end = next_pixel + byte_width;
566                 dev_addr += offset;
567                 back_start += offset;
568                 line_start += offset;
569         }
570
571         while (next_pixel < line_end) {
572
573                 dlfb_compress_hline((const uint16_t **) &next_pixel,
574                              (const uint16_t *) line_end, &dev_addr,
575                         (u8 **) &cmd, (u8 *) cmd_end, back_buffer_offset,
576                         ident_ptr);
577
578                 if (cmd >= cmd_end) {
579                         int len = cmd - (u8 *) urb->transfer_buffer;
580                         if (dlfb_submit_urb(dlfb, urb, len))
581                                 return 1; /* lost pixels is set */
582                         *sent_ptr += len;
583                         urb = dlfb_get_urb(dlfb);
584                         if (!urb)
585                                 return 1; /* lost_pixels is set */
586                         *urb_ptr = urb;
587                         cmd = urb->transfer_buffer;
588                         cmd_end = &cmd[urb->transfer_buffer_length];
589                 }
590         }
591
592         *urb_buf_ptr = cmd;
593
594         return 0;
595 }
596
597 static int dlfb_handle_damage(struct dlfb_data *dlfb, int x, int y, int width, int height)
598 {
599         int i, ret;
600         char *cmd;
601         cycles_t start_cycles, end_cycles;
602         int bytes_sent = 0;
603         int bytes_identical = 0;
604         struct urb *urb;
605         int aligned_x;
606
607         start_cycles = get_cycles();
608
609         mutex_lock(&dlfb->render_mutex);
610
611         aligned_x = DL_ALIGN_DOWN(x, sizeof(unsigned long));
612         width = DL_ALIGN_UP(width + (x-aligned_x), sizeof(unsigned long));
613         x = aligned_x;
614
615         if ((width <= 0) ||
616             (x + width > dlfb->info->var.xres) ||
617             (y + height > dlfb->info->var.yres)) {
618                 ret = -EINVAL;
619                 goto unlock_ret;
620         }
621
622         if (!atomic_read(&dlfb->usb_active)) {
623                 ret = 0;
624                 goto unlock_ret;
625         }
626
627         urb = dlfb_get_urb(dlfb);
628         if (!urb) {
629                 ret = 0;
630                 goto unlock_ret;
631         }
632         cmd = urb->transfer_buffer;
633
634         for (i = y; i < y + height ; i++) {
635                 const int line_offset = dlfb->info->fix.line_length * i;
636                 const int byte_offset = line_offset + (x * BPP);
637
638                 if (dlfb_render_hline(dlfb, &urb,
639                                       (char *) dlfb->info->fix.smem_start,
640                                       &cmd, byte_offset, width * BPP,
641                                       &bytes_identical, &bytes_sent))
642                         goto error;
643         }
644
645         if (cmd > (char *) urb->transfer_buffer) {
646                 int len;
647                 if (cmd < (char *) urb->transfer_buffer + urb->transfer_buffer_length)
648                         *cmd++ = 0xAF;
649                 /* Send partial buffer remaining before exiting */
650                 len = cmd - (char *) urb->transfer_buffer;
651                 dlfb_submit_urb(dlfb, urb, len);
652                 bytes_sent += len;
653         } else
654                 dlfb_urb_completion(urb);
655
656 error:
657         atomic_add(bytes_sent, &dlfb->bytes_sent);
658         atomic_add(bytes_identical, &dlfb->bytes_identical);
659         atomic_add(width*height*2, &dlfb->bytes_rendered);
660         end_cycles = get_cycles();
661         atomic_add(((unsigned int) ((end_cycles - start_cycles)
662                     >> 10)), /* Kcycles */
663                    &dlfb->cpu_kcycles_used);
664
665         ret = 0;
666
667 unlock_ret:
668         mutex_unlock(&dlfb->render_mutex);
669         return ret;
670 }
671
672 static void dlfb_init_damage(struct dlfb_data *dlfb)
673 {
674         dlfb->damage_x = INT_MAX;
675         dlfb->damage_x2 = 0;
676         dlfb->damage_y = INT_MAX;
677         dlfb->damage_y2 = 0;
678 }
679
680 static void dlfb_damage_work(struct work_struct *w)
681 {
682         struct dlfb_data *dlfb = container_of(w, struct dlfb_data, damage_work);
683         int x, x2, y, y2;
684
685         spin_lock_irq(&dlfb->damage_lock);
686         x = dlfb->damage_x;
687         x2 = dlfb->damage_x2;
688         y = dlfb->damage_y;
689         y2 = dlfb->damage_y2;
690         dlfb_init_damage(dlfb);
691         spin_unlock_irq(&dlfb->damage_lock);
692
693         if (x < x2 && y < y2)
694                 dlfb_handle_damage(dlfb, x, y, x2 - x, y2 - y);
695 }
696
697 static void dlfb_offload_damage(struct dlfb_data *dlfb, int x, int y, int width, int height)
698 {
699         unsigned long flags;
700         int x2 = x + width;
701         int y2 = y + height;
702
703         if (x >= x2 || y >= y2)
704                 return;
705
706         spin_lock_irqsave(&dlfb->damage_lock, flags);
707         dlfb->damage_x = min(x, dlfb->damage_x);
708         dlfb->damage_x2 = max(x2, dlfb->damage_x2);
709         dlfb->damage_y = min(y, dlfb->damage_y);
710         dlfb->damage_y2 = max(y2, dlfb->damage_y2);
711         spin_unlock_irqrestore(&dlfb->damage_lock, flags);
712
713         schedule_work(&dlfb->damage_work);
714 }
715
716 /*
717  * Path triggered by usermode clients who write to filesystem
718  * e.g. cat filename > /dev/fb1
719  * Not used by X Windows or text-mode console. But useful for testing.
720  * Slow because of extra copy and we must assume all pixels dirty.
721  */
722 static ssize_t dlfb_ops_write(struct fb_info *info, const char __user *buf,
723                           size_t count, loff_t *ppos)
724 {
725         ssize_t result;
726         struct dlfb_data *dlfb = info->par;
727         u32 offset = (u32) *ppos;
728
729         result = fb_sys_write(info, buf, count, ppos);
730
731         if (result > 0) {
732                 int start = max((int)(offset / info->fix.line_length), 0);
733                 int lines = min((u32)((result / info->fix.line_length) + 1),
734                                 (u32)info->var.yres);
735
736                 dlfb_handle_damage(dlfb, 0, start, info->var.xres,
737                         lines);
738         }
739
740         return result;
741 }
742
743 /* hardware has native COPY command (see libdlo), but not worth it for fbcon */
744 static void dlfb_ops_copyarea(struct fb_info *info,
745                                 const struct fb_copyarea *area)
746 {
747
748         struct dlfb_data *dlfb = info->par;
749
750         sys_copyarea(info, area);
751
752         dlfb_offload_damage(dlfb, area->dx, area->dy,
753                         area->width, area->height);
754 }
755
756 static void dlfb_ops_imageblit(struct fb_info *info,
757                                 const struct fb_image *image)
758 {
759         struct dlfb_data *dlfb = info->par;
760
761         sys_imageblit(info, image);
762
763         dlfb_offload_damage(dlfb, image->dx, image->dy,
764                         image->width, image->height);
765 }
766
767 static void dlfb_ops_fillrect(struct fb_info *info,
768                           const struct fb_fillrect *rect)
769 {
770         struct dlfb_data *dlfb = info->par;
771
772         sys_fillrect(info, rect);
773
774         dlfb_offload_damage(dlfb, rect->dx, rect->dy, rect->width,
775                               rect->height);
776 }
777
778 /*
779  * NOTE: fb_defio.c is holding info->fbdefio.mutex
780  *   Touching ANY framebuffer memory that triggers a page fault
781  *   in fb_defio will cause a deadlock, when it also tries to
782  *   grab the same mutex.
783  */
784 static void dlfb_dpy_deferred_io(struct fb_info *info,
785                                 struct list_head *pagelist)
786 {
787         struct page *cur;
788         struct fb_deferred_io *fbdefio = info->fbdefio;
789         struct dlfb_data *dlfb = info->par;
790         struct urb *urb;
791         char *cmd;
792         cycles_t start_cycles, end_cycles;
793         int bytes_sent = 0;
794         int bytes_identical = 0;
795         int bytes_rendered = 0;
796
797         mutex_lock(&dlfb->render_mutex);
798
799         if (!fb_defio)
800                 goto unlock_ret;
801
802         if (!atomic_read(&dlfb->usb_active))
803                 goto unlock_ret;
804
805         start_cycles = get_cycles();
806
807         urb = dlfb_get_urb(dlfb);
808         if (!urb)
809                 goto unlock_ret;
810
811         cmd = urb->transfer_buffer;
812
813         /* walk the written page list and render each to device */
814         list_for_each_entry(cur, &fbdefio->pagelist, lru) {
815
816                 if (dlfb_render_hline(dlfb, &urb, (char *) info->fix.smem_start,
817                                   &cmd, cur->index << PAGE_SHIFT,
818                                   PAGE_SIZE, &bytes_identical, &bytes_sent))
819                         goto error;
820                 bytes_rendered += PAGE_SIZE;
821         }
822
823         if (cmd > (char *) urb->transfer_buffer) {
824                 int len;
825                 if (cmd < (char *) urb->transfer_buffer + urb->transfer_buffer_length)
826                         *cmd++ = 0xAF;
827                 /* Send partial buffer remaining before exiting */
828                 len = cmd - (char *) urb->transfer_buffer;
829                 dlfb_submit_urb(dlfb, urb, len);
830                 bytes_sent += len;
831         } else
832                 dlfb_urb_completion(urb);
833
834 error:
835         atomic_add(bytes_sent, &dlfb->bytes_sent);
836         atomic_add(bytes_identical, &dlfb->bytes_identical);
837         atomic_add(bytes_rendered, &dlfb->bytes_rendered);
838         end_cycles = get_cycles();
839         atomic_add(((unsigned int) ((end_cycles - start_cycles)
840                     >> 10)), /* Kcycles */
841                    &dlfb->cpu_kcycles_used);
842 unlock_ret:
843         mutex_unlock(&dlfb->render_mutex);
844 }
845
846 static int dlfb_get_edid(struct dlfb_data *dlfb, char *edid, int len)
847 {
848         int i, ret;
849         char *rbuf;
850
851         rbuf = kmalloc(2, GFP_KERNEL);
852         if (!rbuf)
853                 return 0;
854
855         for (i = 0; i < len; i++) {
856                 ret = usb_control_msg(dlfb->udev,
857                                       usb_rcvctrlpipe(dlfb->udev, 0), 0x02,
858                                       (0x80 | (0x02 << 5)), i << 8, 0xA1,
859                                       rbuf, 2, USB_CTRL_GET_TIMEOUT);
860                 if (ret < 2) {
861                         dev_err(&dlfb->udev->dev,
862                                 "Read EDID byte %d failed: %d\n", i, ret);
863                         i--;
864                         break;
865                 }
866                 edid[i] = rbuf[1];
867         }
868
869         kfree(rbuf);
870
871         return i;
872 }
873
874 static int dlfb_ops_ioctl(struct fb_info *info, unsigned int cmd,
875                                 unsigned long arg)
876 {
877
878         struct dlfb_data *dlfb = info->par;
879
880         if (!atomic_read(&dlfb->usb_active))
881                 return 0;
882
883         /* TODO: Update X server to get this from sysfs instead */
884         if (cmd == DLFB_IOCTL_RETURN_EDID) {
885                 void __user *edid = (void __user *)arg;
886                 if (copy_to_user(edid, dlfb->edid, dlfb->edid_size))
887                         return -EFAULT;
888                 return 0;
889         }
890
891         /* TODO: Help propose a standard fb.h ioctl to report mmap damage */
892         if (cmd == DLFB_IOCTL_REPORT_DAMAGE) {
893                 struct dloarea area;
894
895                 if (copy_from_user(&area, (void __user *)arg,
896                                   sizeof(struct dloarea)))
897                         return -EFAULT;
898
899                 /*
900                  * If we have a damage-aware client, turn fb_defio "off"
901                  * To avoid perf imact of unnecessary page fault handling.
902                  * Done by resetting the delay for this fb_info to a very
903                  * long period. Pages will become writable and stay that way.
904                  * Reset to normal value when all clients have closed this fb.
905                  */
906                 if (info->fbdefio)
907                         info->fbdefio->delay = DL_DEFIO_WRITE_DISABLE;
908
909                 if (area.x < 0)
910                         area.x = 0;
911
912                 if (area.x > info->var.xres)
913                         area.x = info->var.xres;
914
915                 if (area.y < 0)
916                         area.y = 0;
917
918                 if (area.y > info->var.yres)
919                         area.y = info->var.yres;
920
921                 dlfb_handle_damage(dlfb, area.x, area.y, area.w, area.h);
922         }
923
924         return 0;
925 }
926
927 /* taken from vesafb */
928 static int
929 dlfb_ops_setcolreg(unsigned regno, unsigned red, unsigned green,
930                unsigned blue, unsigned transp, struct fb_info *info)
931 {
932         int err = 0;
933
934         if (regno >= info->cmap.len)
935                 return 1;
936
937         if (regno < 16) {
938                 if (info->var.red.offset == 10) {
939                         /* 1:5:5:5 */
940                         ((u32 *) (info->pseudo_palette))[regno] =
941                             ((red & 0xf800) >> 1) |
942                             ((green & 0xf800) >> 6) | ((blue & 0xf800) >> 11);
943                 } else {
944                         /* 0:5:6:5 */
945                         ((u32 *) (info->pseudo_palette))[regno] =
946                             ((red & 0xf800)) |
947                             ((green & 0xfc00) >> 5) | ((blue & 0xf800) >> 11);
948                 }
949         }
950
951         return err;
952 }
953
954 /*
955  * It's common for several clients to have framebuffer open simultaneously.
956  * e.g. both fbcon and X. Makes things interesting.
957  * Assumes caller is holding info->lock (for open and release at least)
958  */
959 static int dlfb_ops_open(struct fb_info *info, int user)
960 {
961         struct dlfb_data *dlfb = info->par;
962
963         /*
964          * fbcon aggressively connects to first framebuffer it finds,
965          * preventing other clients (X) from working properly. Usually
966          * not what the user wants. Fail by default with option to enable.
967          */
968         if ((user == 0) && (!console))
969                 return -EBUSY;
970
971         /* If the USB device is gone, we don't accept new opens */
972         if (dlfb->virtualized)
973                 return -ENODEV;
974
975         dlfb->fb_count++;
976
977         if (fb_defio && (info->fbdefio == NULL)) {
978                 /* enable defio at last moment if not disabled by client */
979
980                 struct fb_deferred_io *fbdefio;
981
982                 fbdefio = kzalloc(sizeof(struct fb_deferred_io), GFP_KERNEL);
983
984                 if (fbdefio) {
985                         fbdefio->delay = DL_DEFIO_WRITE_DELAY;
986                         fbdefio->deferred_io = dlfb_dpy_deferred_io;
987                 }
988
989                 info->fbdefio = fbdefio;
990                 fb_deferred_io_init(info);
991         }
992
993         dev_dbg(info->dev, "open, user=%d fb_info=%p count=%d\n",
994                 user, info, dlfb->fb_count);
995
996         return 0;
997 }
998
999 static void dlfb_ops_destroy(struct fb_info *info)
1000 {
1001         struct dlfb_data *dlfb = info->par;
1002
1003         cancel_work_sync(&dlfb->damage_work);
1004
1005         mutex_destroy(&dlfb->render_mutex);
1006
1007         if (info->cmap.len != 0)
1008                 fb_dealloc_cmap(&info->cmap);
1009         if (info->monspecs.modedb)
1010                 fb_destroy_modedb(info->monspecs.modedb);
1011         vfree(info->screen_base);
1012
1013         fb_destroy_modelist(&info->modelist);
1014
1015         while (!list_empty(&dlfb->deferred_free)) {
1016                 struct dlfb_deferred_free *d = list_entry(dlfb->deferred_free.next, struct dlfb_deferred_free, list);
1017                 list_del(&d->list);
1018                 vfree(d->mem);
1019                 kfree(d);
1020         }
1021         vfree(dlfb->backing_buffer);
1022         kfree(dlfb->edid);
1023         dlfb_free_urb_list(dlfb);
1024         usb_put_dev(dlfb->udev);
1025         kfree(dlfb);
1026
1027         /* Assume info structure is freed after this point */
1028         framebuffer_release(info);
1029 }
1030
1031 /*
1032  * Assumes caller is holding info->lock mutex (for open and release at least)
1033  */
1034 static int dlfb_ops_release(struct fb_info *info, int user)
1035 {
1036         struct dlfb_data *dlfb = info->par;
1037
1038         dlfb->fb_count--;
1039
1040         if ((dlfb->fb_count == 0) && (info->fbdefio)) {
1041                 fb_deferred_io_cleanup(info);
1042                 kfree(info->fbdefio);
1043                 info->fbdefio = NULL;
1044                 info->fbops->fb_mmap = dlfb_ops_mmap;
1045         }
1046
1047         dev_dbg(info->dev, "release, user=%d count=%d\n", user, dlfb->fb_count);
1048
1049         return 0;
1050 }
1051
1052 /*
1053  * Check whether a video mode is supported by the DisplayLink chip
1054  * We start from monitor's modes, so don't need to filter that here
1055  */
1056 static int dlfb_is_valid_mode(struct fb_videomode *mode, struct dlfb_data *dlfb)
1057 {
1058         if (mode->xres * mode->yres > dlfb->sku_pixel_limit)
1059                 return 0;
1060
1061         return 1;
1062 }
1063
1064 static void dlfb_var_color_format(struct fb_var_screeninfo *var)
1065 {
1066         const struct fb_bitfield red = { 11, 5, 0 };
1067         const struct fb_bitfield green = { 5, 6, 0 };
1068         const struct fb_bitfield blue = { 0, 5, 0 };
1069
1070         var->bits_per_pixel = 16;
1071         var->red = red;
1072         var->green = green;
1073         var->blue = blue;
1074 }
1075
1076 static int dlfb_ops_check_var(struct fb_var_screeninfo *var,
1077                                 struct fb_info *info)
1078 {
1079         struct fb_videomode mode;
1080         struct dlfb_data *dlfb = info->par;
1081
1082         /* set device-specific elements of var unrelated to mode */
1083         dlfb_var_color_format(var);
1084
1085         fb_var_to_videomode(&mode, var);
1086
1087         if (!dlfb_is_valid_mode(&mode, dlfb))
1088                 return -EINVAL;
1089
1090         return 0;
1091 }
1092
1093 static int dlfb_ops_set_par(struct fb_info *info)
1094 {
1095         struct dlfb_data *dlfb = info->par;
1096         int result;
1097         u16 *pix_framebuffer;
1098         int i;
1099         struct fb_var_screeninfo fvs;
1100         u32 line_length = info->var.xres * (info->var.bits_per_pixel / 8);
1101
1102         /* clear the activate field because it causes spurious miscompares */
1103         fvs = info->var;
1104         fvs.activate = 0;
1105         fvs.vmode &= ~FB_VMODE_SMOOTH_XPAN;
1106
1107         if (!memcmp(&dlfb->current_mode, &fvs, sizeof(struct fb_var_screeninfo)))
1108                 return 0;
1109
1110         result = dlfb_realloc_framebuffer(dlfb, info, info->var.yres * line_length);
1111         if (result)
1112                 return result;
1113
1114         result = dlfb_set_video_mode(dlfb, &info->var);
1115
1116         if (result)
1117                 return result;
1118
1119         dlfb->current_mode = fvs;
1120         info->fix.line_length = line_length;
1121
1122         if (dlfb->fb_count == 0) {
1123
1124                 /* paint greenscreen */
1125
1126                 pix_framebuffer = (u16 *) info->screen_base;
1127                 for (i = 0; i < info->fix.smem_len / 2; i++)
1128                         pix_framebuffer[i] = 0x37e6;
1129         }
1130
1131         dlfb_handle_damage(dlfb, 0, 0, info->var.xres, info->var.yres);
1132
1133         return 0;
1134 }
1135
1136 /* To fonzi the jukebox (e.g. make blanking changes take effect) */
1137 static char *dlfb_dummy_render(char *buf)
1138 {
1139         *buf++ = 0xAF;
1140         *buf++ = 0x6A; /* copy */
1141         *buf++ = 0x00; /* from address*/
1142         *buf++ = 0x00;
1143         *buf++ = 0x00;
1144         *buf++ = 0x01; /* one pixel */
1145         *buf++ = 0x00; /* to address */
1146         *buf++ = 0x00;
1147         *buf++ = 0x00;
1148         return buf;
1149 }
1150
1151 /*
1152  * In order to come back from full DPMS off, we need to set the mode again
1153  */
1154 static int dlfb_ops_blank(int blank_mode, struct fb_info *info)
1155 {
1156         struct dlfb_data *dlfb = info->par;
1157         char *bufptr;
1158         struct urb *urb;
1159
1160         dev_dbg(info->dev, "blank, mode %d --> %d\n",
1161                 dlfb->blank_mode, blank_mode);
1162
1163         if ((dlfb->blank_mode == FB_BLANK_POWERDOWN) &&
1164             (blank_mode != FB_BLANK_POWERDOWN)) {
1165
1166                 /* returning from powerdown requires a fresh modeset */
1167                 dlfb_set_video_mode(dlfb, &info->var);
1168         }
1169
1170         urb = dlfb_get_urb(dlfb);
1171         if (!urb)
1172                 return 0;
1173
1174         bufptr = (char *) urb->transfer_buffer;
1175         bufptr = dlfb_vidreg_lock(bufptr);
1176         bufptr = dlfb_blanking(bufptr, blank_mode);
1177         bufptr = dlfb_vidreg_unlock(bufptr);
1178
1179         /* seems like a render op is needed to have blank change take effect */
1180         bufptr = dlfb_dummy_render(bufptr);
1181
1182         dlfb_submit_urb(dlfb, urb, bufptr -
1183                         (char *) urb->transfer_buffer);
1184
1185         dlfb->blank_mode = blank_mode;
1186
1187         return 0;
1188 }
1189
1190 static struct fb_ops dlfb_ops = {
1191         .owner = THIS_MODULE,
1192         .fb_read = fb_sys_read,
1193         .fb_write = dlfb_ops_write,
1194         .fb_setcolreg = dlfb_ops_setcolreg,
1195         .fb_fillrect = dlfb_ops_fillrect,
1196         .fb_copyarea = dlfb_ops_copyarea,
1197         .fb_imageblit = dlfb_ops_imageblit,
1198         .fb_mmap = dlfb_ops_mmap,
1199         .fb_ioctl = dlfb_ops_ioctl,
1200         .fb_open = dlfb_ops_open,
1201         .fb_release = dlfb_ops_release,
1202         .fb_blank = dlfb_ops_blank,
1203         .fb_check_var = dlfb_ops_check_var,
1204         .fb_set_par = dlfb_ops_set_par,
1205         .fb_destroy = dlfb_ops_destroy,
1206 };
1207
1208
1209 static void dlfb_deferred_vfree(struct dlfb_data *dlfb, void *mem)
1210 {
1211         struct dlfb_deferred_free *d = kmalloc(sizeof(struct dlfb_deferred_free), GFP_KERNEL);
1212         if (!d)
1213                 return;
1214         d->mem = mem;
1215         list_add(&d->list, &dlfb->deferred_free);
1216 }
1217
1218 /*
1219  * Assumes &info->lock held by caller
1220  * Assumes no active clients have framebuffer open
1221  */
1222 static int dlfb_realloc_framebuffer(struct dlfb_data *dlfb, struct fb_info *info, u32 new_len)
1223 {
1224         u32 old_len = info->fix.smem_len;
1225         const void *old_fb = (const void __force *)info->screen_base;
1226         unsigned char *new_fb;
1227         unsigned char *new_back = NULL;
1228
1229         new_len = PAGE_ALIGN(new_len);
1230
1231         if (new_len > old_len) {
1232                 /*
1233                  * Alloc system memory for virtual framebuffer
1234                  */
1235                 new_fb = vmalloc(new_len);
1236                 if (!new_fb) {
1237                         dev_err(info->dev, "Virtual framebuffer alloc failed\n");
1238                         return -ENOMEM;
1239                 }
1240                 memset(new_fb, 0xff, new_len);
1241
1242                 if (info->screen_base) {
1243                         memcpy(new_fb, old_fb, old_len);
1244                         dlfb_deferred_vfree(dlfb, (void __force *)info->screen_base);
1245                 }
1246
1247                 info->screen_base = (char __iomem *)new_fb;
1248                 info->fix.smem_len = new_len;
1249                 info->fix.smem_start = (unsigned long) new_fb;
1250                 info->flags = udlfb_info_flags;
1251
1252                 /*
1253                  * Second framebuffer copy to mirror the framebuffer state
1254                  * on the physical USB device. We can function without this.
1255                  * But with imperfect damage info we may send pixels over USB
1256                  * that were, in fact, unchanged - wasting limited USB bandwidth
1257                  */
1258                 if (shadow)
1259                         new_back = vzalloc(new_len);
1260                 if (!new_back)
1261                         dev_info(info->dev,
1262                                  "No shadow/backing buffer allocated\n");
1263                 else {
1264                         dlfb_deferred_vfree(dlfb, dlfb->backing_buffer);
1265                         dlfb->backing_buffer = new_back;
1266                 }
1267         }
1268         return 0;
1269 }
1270
1271 /*
1272  * 1) Get EDID from hw, or use sw default
1273  * 2) Parse into various fb_info structs
1274  * 3) Allocate virtual framebuffer memory to back highest res mode
1275  *
1276  * Parses EDID into three places used by various parts of fbdev:
1277  * fb_var_screeninfo contains the timing of the monitor's preferred mode
1278  * fb_info.monspecs is full parsed EDID info, including monspecs.modedb
1279  * fb_info.modelist is a linked list of all monitor & VESA modes which work
1280  *
1281  * If EDID is not readable/valid, then modelist is all VESA modes,
1282  * monspecs is NULL, and fb_var_screeninfo is set to safe VESA mode
1283  * Returns 0 if successful
1284  */
1285 static int dlfb_setup_modes(struct dlfb_data *dlfb,
1286                            struct fb_info *info,
1287                            char *default_edid, size_t default_edid_size)
1288 {
1289         char *edid;
1290         int i, result = 0, tries = 3;
1291         struct device *dev = info->device;
1292         struct fb_videomode *mode;
1293         const struct fb_videomode *default_vmode = NULL;
1294
1295         if (info->dev) {
1296                 /* only use mutex if info has been registered */
1297                 mutex_lock(&info->lock);
1298                 /* parent device is used otherwise */
1299                 dev = info->dev;
1300         }
1301
1302         edid = kmalloc(EDID_LENGTH, GFP_KERNEL);
1303         if (!edid) {
1304                 result = -ENOMEM;
1305                 goto error;
1306         }
1307
1308         fb_destroy_modelist(&info->modelist);
1309         memset(&info->monspecs, 0, sizeof(info->monspecs));
1310
1311         /*
1312          * Try to (re)read EDID from hardware first
1313          * EDID data may return, but not parse as valid
1314          * Try again a few times, in case of e.g. analog cable noise
1315          */
1316         while (tries--) {
1317
1318                 i = dlfb_get_edid(dlfb, edid, EDID_LENGTH);
1319
1320                 if (i >= EDID_LENGTH)
1321                         fb_edid_to_monspecs(edid, &info->monspecs);
1322
1323                 if (info->monspecs.modedb_len > 0) {
1324                         dlfb->edid = edid;
1325                         dlfb->edid_size = i;
1326                         break;
1327                 }
1328         }
1329
1330         /* If that fails, use a previously returned EDID if available */
1331         if (info->monspecs.modedb_len == 0) {
1332                 dev_err(dev, "Unable to get valid EDID from device/display\n");
1333
1334                 if (dlfb->edid) {
1335                         fb_edid_to_monspecs(dlfb->edid, &info->monspecs);
1336                         if (info->monspecs.modedb_len > 0)
1337                                 dev_err(dev, "Using previously queried EDID\n");
1338                 }
1339         }
1340
1341         /* If that fails, use the default EDID we were handed */
1342         if (info->monspecs.modedb_len == 0) {
1343                 if (default_edid_size >= EDID_LENGTH) {
1344                         fb_edid_to_monspecs(default_edid, &info->monspecs);
1345                         if (info->monspecs.modedb_len > 0) {
1346                                 memcpy(edid, default_edid, default_edid_size);
1347                                 dlfb->edid = edid;
1348                                 dlfb->edid_size = default_edid_size;
1349                                 dev_err(dev, "Using default/backup EDID\n");
1350                         }
1351                 }
1352         }
1353
1354         /* If we've got modes, let's pick a best default mode */
1355         if (info->monspecs.modedb_len > 0) {
1356
1357                 for (i = 0; i < info->monspecs.modedb_len; i++) {
1358                         mode = &info->monspecs.modedb[i];
1359                         if (dlfb_is_valid_mode(mode, dlfb)) {
1360                                 fb_add_videomode(mode, &info->modelist);
1361                         } else {
1362                                 dev_dbg(dev, "Specified mode %dx%d too big\n",
1363                                         mode->xres, mode->yres);
1364                                 if (i == 0)
1365                                         /* if we've removed top/best mode */
1366                                         info->monspecs.misc
1367                                                 &= ~FB_MISC_1ST_DETAIL;
1368                         }
1369                 }
1370
1371                 default_vmode = fb_find_best_display(&info->monspecs,
1372                                                      &info->modelist);
1373         }
1374
1375         /* If everything else has failed, fall back to safe default mode */
1376         if (default_vmode == NULL) {
1377
1378                 struct fb_videomode fb_vmode = {0};
1379
1380                 /*
1381                  * Add the standard VESA modes to our modelist
1382                  * Since we don't have EDID, there may be modes that
1383                  * overspec monitor and/or are incorrect aspect ratio, etc.
1384                  * But at least the user has a chance to choose
1385                  */
1386                 for (i = 0; i < VESA_MODEDB_SIZE; i++) {
1387                         mode = (struct fb_videomode *)&vesa_modes[i];
1388                         if (dlfb_is_valid_mode(mode, dlfb))
1389                                 fb_add_videomode(mode, &info->modelist);
1390                         else
1391                                 dev_dbg(dev, "VESA mode %dx%d too big\n",
1392                                         mode->xres, mode->yres);
1393                 }
1394
1395                 /*
1396                  * default to resolution safe for projectors
1397                  * (since they are most common case without EDID)
1398                  */
1399                 fb_vmode.xres = 800;
1400                 fb_vmode.yres = 600;
1401                 fb_vmode.refresh = 60;
1402                 default_vmode = fb_find_nearest_mode(&fb_vmode,
1403                                                      &info->modelist);
1404         }
1405
1406         /* If we have good mode and no active clients*/
1407         if ((default_vmode != NULL) && (dlfb->fb_count == 0)) {
1408
1409                 fb_videomode_to_var(&info->var, default_vmode);
1410                 dlfb_var_color_format(&info->var);
1411
1412                 /*
1413                  * with mode size info, we can now alloc our framebuffer.
1414                  */
1415                 memcpy(&info->fix, &dlfb_fix, sizeof(dlfb_fix));
1416         } else
1417                 result = -EINVAL;
1418
1419 error:
1420         if (edid && (dlfb->edid != edid))
1421                 kfree(edid);
1422
1423         if (info->dev)
1424                 mutex_unlock(&info->lock);
1425
1426         return result;
1427 }
1428
1429 static ssize_t metrics_bytes_rendered_show(struct device *fbdev,
1430                                    struct device_attribute *a, char *buf) {
1431         struct fb_info *fb_info = dev_get_drvdata(fbdev);
1432         struct dlfb_data *dlfb = fb_info->par;
1433         return sysfs_emit(buf, "%u\n",
1434                         atomic_read(&dlfb->bytes_rendered));
1435 }
1436
1437 static ssize_t metrics_bytes_identical_show(struct device *fbdev,
1438                                    struct device_attribute *a, char *buf) {
1439         struct fb_info *fb_info = dev_get_drvdata(fbdev);
1440         struct dlfb_data *dlfb = fb_info->par;
1441         return sysfs_emit(buf, "%u\n",
1442                         atomic_read(&dlfb->bytes_identical));
1443 }
1444
1445 static ssize_t metrics_bytes_sent_show(struct device *fbdev,
1446                                    struct device_attribute *a, char *buf) {
1447         struct fb_info *fb_info = dev_get_drvdata(fbdev);
1448         struct dlfb_data *dlfb = fb_info->par;
1449         return sysfs_emit(buf, "%u\n",
1450                         atomic_read(&dlfb->bytes_sent));
1451 }
1452
1453 static ssize_t metrics_cpu_kcycles_used_show(struct device *fbdev,
1454                                    struct device_attribute *a, char *buf) {
1455         struct fb_info *fb_info = dev_get_drvdata(fbdev);
1456         struct dlfb_data *dlfb = fb_info->par;
1457         return sysfs_emit(buf, "%u\n",
1458                         atomic_read(&dlfb->cpu_kcycles_used));
1459 }
1460
1461 static ssize_t edid_show(
1462                         struct file *filp,
1463                         struct kobject *kobj, struct bin_attribute *a,
1464                          char *buf, loff_t off, size_t count) {
1465         struct device *fbdev = container_of(kobj, struct device, kobj);
1466         struct fb_info *fb_info = dev_get_drvdata(fbdev);
1467         struct dlfb_data *dlfb = fb_info->par;
1468
1469         if (dlfb->edid == NULL)
1470                 return 0;
1471
1472         if ((off >= dlfb->edid_size) || (count > dlfb->edid_size))
1473                 return 0;
1474
1475         if (off + count > dlfb->edid_size)
1476                 count = dlfb->edid_size - off;
1477
1478         memcpy(buf, dlfb->edid, count);
1479
1480         return count;
1481 }
1482
1483 static ssize_t edid_store(
1484                         struct file *filp,
1485                         struct kobject *kobj, struct bin_attribute *a,
1486                         char *src, loff_t src_off, size_t src_size) {
1487         struct device *fbdev = container_of(kobj, struct device, kobj);
1488         struct fb_info *fb_info = dev_get_drvdata(fbdev);
1489         struct dlfb_data *dlfb = fb_info->par;
1490         int ret;
1491
1492         /* We only support write of entire EDID at once, no offset*/
1493         if ((src_size != EDID_LENGTH) || (src_off != 0))
1494                 return -EINVAL;
1495
1496         ret = dlfb_setup_modes(dlfb, fb_info, src, src_size);
1497         if (ret)
1498                 return ret;
1499
1500         if (!dlfb->edid || memcmp(src, dlfb->edid, src_size))
1501                 return -EINVAL;
1502
1503         ret = dlfb_ops_set_par(fb_info);
1504         if (ret)
1505                 return ret;
1506
1507         return src_size;
1508 }
1509
1510 static ssize_t metrics_reset_store(struct device *fbdev,
1511                            struct device_attribute *attr,
1512                            const char *buf, size_t count)
1513 {
1514         struct fb_info *fb_info = dev_get_drvdata(fbdev);
1515         struct dlfb_data *dlfb = fb_info->par;
1516
1517         atomic_set(&dlfb->bytes_rendered, 0);
1518         atomic_set(&dlfb->bytes_identical, 0);
1519         atomic_set(&dlfb->bytes_sent, 0);
1520         atomic_set(&dlfb->cpu_kcycles_used, 0);
1521
1522         return count;
1523 }
1524
1525 static const struct bin_attribute edid_attr = {
1526         .attr.name = "edid",
1527         .attr.mode = 0666,
1528         .size = EDID_LENGTH,
1529         .read = edid_show,
1530         .write = edid_store
1531 };
1532
1533 static const struct device_attribute fb_device_attrs[] = {
1534         __ATTR_RO(metrics_bytes_rendered),
1535         __ATTR_RO(metrics_bytes_identical),
1536         __ATTR_RO(metrics_bytes_sent),
1537         __ATTR_RO(metrics_cpu_kcycles_used),
1538         __ATTR(metrics_reset, S_IWUSR, NULL, metrics_reset_store),
1539 };
1540
1541 /*
1542  * This is necessary before we can communicate with the display controller.
1543  */
1544 static int dlfb_select_std_channel(struct dlfb_data *dlfb)
1545 {
1546         int ret;
1547         void *buf;
1548         static const u8 set_def_chn[] = {
1549                                 0x57, 0xCD, 0xDC, 0xA7,
1550                                 0x1C, 0x88, 0x5E, 0x15,
1551                                 0x60, 0xFE, 0xC6, 0x97,
1552                                 0x16, 0x3D, 0x47, 0xF2  };
1553
1554         buf = kmemdup(set_def_chn, sizeof(set_def_chn), GFP_KERNEL);
1555
1556         if (!buf)
1557                 return -ENOMEM;
1558
1559         ret = usb_control_msg(dlfb->udev, usb_sndctrlpipe(dlfb->udev, 0),
1560                         NR_USB_REQUEST_CHANNEL,
1561                         (USB_DIR_OUT | USB_TYPE_VENDOR), 0, 0,
1562                         buf, sizeof(set_def_chn), USB_CTRL_SET_TIMEOUT);
1563
1564         kfree(buf);
1565
1566         return ret;
1567 }
1568
1569 static int dlfb_parse_vendor_descriptor(struct dlfb_data *dlfb,
1570                                         struct usb_interface *intf)
1571 {
1572         char *desc;
1573         char *buf;
1574         char *desc_end;
1575         int total_len;
1576
1577         buf = kzalloc(MAX_VENDOR_DESCRIPTOR_SIZE, GFP_KERNEL);
1578         if (!buf)
1579                 return false;
1580         desc = buf;
1581
1582         total_len = usb_get_descriptor(interface_to_usbdev(intf),
1583                                         0x5f, /* vendor specific */
1584                                         0, desc, MAX_VENDOR_DESCRIPTOR_SIZE);
1585
1586         /* if not found, look in configuration descriptor */
1587         if (total_len < 0) {
1588                 if (0 == usb_get_extra_descriptor(intf->cur_altsetting,
1589                         0x5f, &desc))
1590                         total_len = (int) desc[0];
1591         }
1592
1593         if (total_len > 5) {
1594                 dev_info(&intf->dev,
1595                          "vendor descriptor length: %d data: %11ph\n",
1596                          total_len, desc);
1597
1598                 if ((desc[0] != total_len) || /* descriptor length */
1599                     (desc[1] != 0x5f) ||   /* vendor descriptor type */
1600                     (desc[2] != 0x01) ||   /* version (2 bytes) */
1601                     (desc[3] != 0x00) ||
1602                     (desc[4] != total_len - 2)) /* length after type */
1603                         goto unrecognized;
1604
1605                 desc_end = desc + total_len;
1606                 desc += 5; /* the fixed header we've already parsed */
1607
1608                 while (desc < desc_end) {
1609                         u8 length;
1610                         u16 key;
1611
1612                         key = *desc++;
1613                         key |= (u16)*desc++ << 8;
1614                         length = *desc++;
1615
1616                         switch (key) {
1617                         case 0x0200: { /* max_area */
1618                                 u32 max_area = *desc++;
1619                                 max_area |= (u32)*desc++ << 8;
1620                                 max_area |= (u32)*desc++ << 16;
1621                                 max_area |= (u32)*desc++ << 24;
1622                                 dev_warn(&intf->dev,
1623                                          "DL chip limited to %d pixel modes\n",
1624                                          max_area);
1625                                 dlfb->sku_pixel_limit = max_area;
1626                                 break;
1627                         }
1628                         default:
1629                                 break;
1630                         }
1631                         desc += length;
1632                 }
1633         } else {
1634                 dev_info(&intf->dev, "vendor descriptor not available (%d)\n",
1635                          total_len);
1636         }
1637
1638         goto success;
1639
1640 unrecognized:
1641         /* allow udlfb to load for now even if firmware unrecognized */
1642         dev_err(&intf->dev, "Unrecognized vendor firmware descriptor\n");
1643
1644 success:
1645         kfree(buf);
1646         return true;
1647 }
1648
1649 static int dlfb_usb_probe(struct usb_interface *intf,
1650                           const struct usb_device_id *id)
1651 {
1652         int i;
1653         const struct device_attribute *attr;
1654         struct dlfb_data *dlfb;
1655         struct fb_info *info;
1656         int retval = -ENOMEM;
1657         struct usb_device *usbdev = interface_to_usbdev(intf);
1658
1659         /* usb initialization */
1660         dlfb = kzalloc(sizeof(*dlfb), GFP_KERNEL);
1661         if (!dlfb) {
1662                 dev_err(&intf->dev, "%s: failed to allocate dlfb\n", __func__);
1663                 return -ENOMEM;
1664         }
1665
1666         INIT_LIST_HEAD(&dlfb->deferred_free);
1667
1668         dlfb->udev = usb_get_dev(usbdev);
1669         usb_set_intfdata(intf, dlfb);
1670
1671         dev_dbg(&intf->dev, "console enable=%d\n", console);
1672         dev_dbg(&intf->dev, "fb_defio enable=%d\n", fb_defio);
1673         dev_dbg(&intf->dev, "shadow enable=%d\n", shadow);
1674
1675         dlfb->sku_pixel_limit = 2048 * 1152; /* default to maximum */
1676
1677         if (!dlfb_parse_vendor_descriptor(dlfb, intf)) {
1678                 dev_err(&intf->dev,
1679                         "firmware not recognized, incompatible device?\n");
1680                 goto error;
1681         }
1682
1683         if (pixel_limit) {
1684                 dev_warn(&intf->dev,
1685                          "DL chip limit of %d overridden to %d\n",
1686                          dlfb->sku_pixel_limit, pixel_limit);
1687                 dlfb->sku_pixel_limit = pixel_limit;
1688         }
1689
1690
1691         /* allocates framebuffer driver structure, not framebuffer memory */
1692         info = framebuffer_alloc(0, &dlfb->udev->dev);
1693         if (!info) {
1694                 dev_err(&dlfb->udev->dev, "framebuffer_alloc failed\n");
1695                 goto error;
1696         }
1697
1698         dlfb->info = info;
1699         info->par = dlfb;
1700         info->pseudo_palette = dlfb->pseudo_palette;
1701         dlfb->ops = dlfb_ops;
1702         info->fbops = &dlfb->ops;
1703
1704         mutex_init(&dlfb->render_mutex);
1705         dlfb_init_damage(dlfb);
1706         spin_lock_init(&dlfb->damage_lock);
1707         INIT_WORK(&dlfb->damage_work, dlfb_damage_work);
1708
1709         INIT_LIST_HEAD(&info->modelist);
1710
1711         if (!dlfb_alloc_urb_list(dlfb, WRITES_IN_FLIGHT, MAX_TRANSFER)) {
1712                 retval = -ENOMEM;
1713                 dev_err(&intf->dev, "unable to allocate urb list\n");
1714                 goto error;
1715         }
1716
1717         /* We don't register a new USB class. Our client interface is dlfbev */
1718
1719         retval = fb_alloc_cmap(&info->cmap, 256, 0);
1720         if (retval < 0) {
1721                 dev_err(info->device, "cmap allocation failed: %d\n", retval);
1722                 goto error;
1723         }
1724
1725         retval = dlfb_setup_modes(dlfb, info, NULL, 0);
1726         if (retval != 0) {
1727                 dev_err(info->device,
1728                         "unable to find common mode for display and adapter\n");
1729                 goto error;
1730         }
1731
1732         /* ready to begin using device */
1733
1734         atomic_set(&dlfb->usb_active, 1);
1735         dlfb_select_std_channel(dlfb);
1736
1737         dlfb_ops_check_var(&info->var, info);
1738         retval = dlfb_ops_set_par(info);
1739         if (retval)
1740                 goto error;
1741
1742         retval = register_framebuffer(info);
1743         if (retval < 0) {
1744                 dev_err(info->device, "unable to register framebuffer: %d\n",
1745                         retval);
1746                 goto error;
1747         }
1748
1749         for (i = 0; i < ARRAY_SIZE(fb_device_attrs); i++) {
1750                 attr = &fb_device_attrs[i];
1751                 retval = device_create_file(info->dev, attr);
1752                 if (retval)
1753                         dev_warn(info->device,
1754                                  "failed to create '%s' attribute: %d\n",
1755                                  attr->attr.name, retval);
1756         }
1757
1758         retval = device_create_bin_file(info->dev, &edid_attr);
1759         if (retval)
1760                 dev_warn(info->device, "failed to create '%s' attribute: %d\n",
1761                          edid_attr.attr.name, retval);
1762
1763         dev_info(info->device,
1764                  "%s is DisplayLink USB device (%dx%d, %dK framebuffer memory)\n",
1765                  dev_name(info->dev), info->var.xres, info->var.yres,
1766                  ((dlfb->backing_buffer) ?
1767                  info->fix.smem_len * 2 : info->fix.smem_len) >> 10);
1768         return 0;
1769
1770 error:
1771         if (dlfb->info) {
1772                 dlfb_ops_destroy(dlfb->info);
1773         } else {
1774                 usb_put_dev(dlfb->udev);
1775                 kfree(dlfb);
1776         }
1777         return retval;
1778 }
1779
1780 static void dlfb_usb_disconnect(struct usb_interface *intf)
1781 {
1782         struct dlfb_data *dlfb;
1783         struct fb_info *info;
1784         int i;
1785
1786         dlfb = usb_get_intfdata(intf);
1787         info = dlfb->info;
1788
1789         dev_dbg(&intf->dev, "USB disconnect starting\n");
1790
1791         /* we virtualize until all fb clients release. Then we free */
1792         dlfb->virtualized = true;
1793
1794         /* When non-active we'll update virtual framebuffer, but no new urbs */
1795         atomic_set(&dlfb->usb_active, 0);
1796
1797         /* this function will wait for all in-flight urbs to complete */
1798         dlfb_free_urb_list(dlfb);
1799
1800         /* remove udlfb's sysfs interfaces */
1801         for (i = 0; i < ARRAY_SIZE(fb_device_attrs); i++)
1802                 device_remove_file(info->dev, &fb_device_attrs[i]);
1803         device_remove_bin_file(info->dev, &edid_attr);
1804
1805         unregister_framebuffer(info);
1806 }
1807
1808 static struct usb_driver dlfb_driver = {
1809         .name = "udlfb",
1810         .probe = dlfb_usb_probe,
1811         .disconnect = dlfb_usb_disconnect,
1812         .id_table = id_table,
1813 };
1814
1815 module_usb_driver(dlfb_driver);
1816
1817 static void dlfb_urb_completion(struct urb *urb)
1818 {
1819         struct urb_node *unode = urb->context;
1820         struct dlfb_data *dlfb = unode->dlfb;
1821         unsigned long flags;
1822
1823         switch (urb->status) {
1824         case 0:
1825                 /* success */
1826                 break;
1827         case -ECONNRESET:
1828         case -ENOENT:
1829         case -ESHUTDOWN:
1830                 /* sync/async unlink faults aren't errors */
1831                 break;
1832         default:
1833                 dev_err(&dlfb->udev->dev,
1834                         "%s - nonzero write bulk status received: %d\n",
1835                         __func__, urb->status);
1836                 atomic_set(&dlfb->lost_pixels, 1);
1837                 break;
1838         }
1839
1840         urb->transfer_buffer_length = dlfb->urbs.size; /* reset to actual */
1841
1842         spin_lock_irqsave(&dlfb->urbs.lock, flags);
1843         list_add_tail(&unode->entry, &dlfb->urbs.list);
1844         dlfb->urbs.available++;
1845         spin_unlock_irqrestore(&dlfb->urbs.lock, flags);
1846
1847         up(&dlfb->urbs.limit_sem);
1848 }
1849
1850 static void dlfb_free_urb_list(struct dlfb_data *dlfb)
1851 {
1852         int count = dlfb->urbs.count;
1853         struct list_head *node;
1854         struct urb_node *unode;
1855         struct urb *urb;
1856
1857         /* keep waiting and freeing, until we've got 'em all */
1858         while (count--) {
1859                 down(&dlfb->urbs.limit_sem);
1860
1861                 spin_lock_irq(&dlfb->urbs.lock);
1862
1863                 node = dlfb->urbs.list.next; /* have reserved one with sem */
1864                 list_del_init(node);
1865
1866                 spin_unlock_irq(&dlfb->urbs.lock);
1867
1868                 unode = list_entry(node, struct urb_node, entry);
1869                 urb = unode->urb;
1870
1871                 /* Free each separately allocated piece */
1872                 usb_free_coherent(urb->dev, dlfb->urbs.size,
1873                                   urb->transfer_buffer, urb->transfer_dma);
1874                 usb_free_urb(urb);
1875                 kfree(node);
1876         }
1877
1878         dlfb->urbs.count = 0;
1879 }
1880
1881 static int dlfb_alloc_urb_list(struct dlfb_data *dlfb, int count, size_t size)
1882 {
1883         struct urb *urb;
1884         struct urb_node *unode;
1885         char *buf;
1886         size_t wanted_size = count * size;
1887
1888         spin_lock_init(&dlfb->urbs.lock);
1889
1890 retry:
1891         dlfb->urbs.size = size;
1892         INIT_LIST_HEAD(&dlfb->urbs.list);
1893
1894         sema_init(&dlfb->urbs.limit_sem, 0);
1895         dlfb->urbs.count = 0;
1896         dlfb->urbs.available = 0;
1897
1898         while (dlfb->urbs.count * size < wanted_size) {
1899                 unode = kzalloc(sizeof(*unode), GFP_KERNEL);
1900                 if (!unode)
1901                         break;
1902                 unode->dlfb = dlfb;
1903
1904                 urb = usb_alloc_urb(0, GFP_KERNEL);
1905                 if (!urb) {
1906                         kfree(unode);
1907                         break;
1908                 }
1909                 unode->urb = urb;
1910
1911                 buf = usb_alloc_coherent(dlfb->udev, size, GFP_KERNEL,
1912                                          &urb->transfer_dma);
1913                 if (!buf) {
1914                         kfree(unode);
1915                         usb_free_urb(urb);
1916                         if (size > PAGE_SIZE) {
1917                                 size /= 2;
1918                                 dlfb_free_urb_list(dlfb);
1919                                 goto retry;
1920                         }
1921                         break;
1922                 }
1923
1924                 /* urb->transfer_buffer_length set to actual before submit */
1925                 usb_fill_bulk_urb(urb, dlfb->udev, usb_sndbulkpipe(dlfb->udev, 1),
1926                         buf, size, dlfb_urb_completion, unode);
1927                 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1928
1929                 list_add_tail(&unode->entry, &dlfb->urbs.list);
1930
1931                 up(&dlfb->urbs.limit_sem);
1932                 dlfb->urbs.count++;
1933                 dlfb->urbs.available++;
1934         }
1935
1936         return dlfb->urbs.count;
1937 }
1938
1939 static struct urb *dlfb_get_urb(struct dlfb_data *dlfb)
1940 {
1941         int ret;
1942         struct list_head *entry;
1943         struct urb_node *unode;
1944
1945         /* Wait for an in-flight buffer to complete and get re-queued */
1946         ret = down_timeout(&dlfb->urbs.limit_sem, GET_URB_TIMEOUT);
1947         if (ret) {
1948                 atomic_set(&dlfb->lost_pixels, 1);
1949                 dev_warn(&dlfb->udev->dev,
1950                          "wait for urb interrupted: %d available: %d\n",
1951                          ret, dlfb->urbs.available);
1952                 return NULL;
1953         }
1954
1955         spin_lock_irq(&dlfb->urbs.lock);
1956
1957         BUG_ON(list_empty(&dlfb->urbs.list)); /* reserved one with limit_sem */
1958         entry = dlfb->urbs.list.next;
1959         list_del_init(entry);
1960         dlfb->urbs.available--;
1961
1962         spin_unlock_irq(&dlfb->urbs.lock);
1963
1964         unode = list_entry(entry, struct urb_node, entry);
1965         return unode->urb;
1966 }
1967
1968 static int dlfb_submit_urb(struct dlfb_data *dlfb, struct urb *urb, size_t len)
1969 {
1970         int ret;
1971
1972         BUG_ON(len > dlfb->urbs.size);
1973
1974         urb->transfer_buffer_length = len; /* set to actual payload len */
1975         ret = usb_submit_urb(urb, GFP_KERNEL);
1976         if (ret) {
1977                 dlfb_urb_completion(urb); /* because no one else will */
1978                 atomic_set(&dlfb->lost_pixels, 1);
1979                 dev_err(&dlfb->udev->dev, "submit urb error: %d\n", ret);
1980         }
1981         return ret;
1982 }
1983
1984 module_param(console, bool, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
1985 MODULE_PARM_DESC(console, "Allow fbcon to open framebuffer");
1986
1987 module_param(fb_defio, bool, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
1988 MODULE_PARM_DESC(fb_defio, "Page fault detection of mmap writes");
1989
1990 module_param(shadow, bool, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
1991 MODULE_PARM_DESC(shadow, "Shadow vid mem. Disable to save mem but lose perf");
1992
1993 module_param(pixel_limit, int, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
1994 MODULE_PARM_DESC(pixel_limit, "Force limit on max mode (in x*y pixels)");
1995
1996 MODULE_AUTHOR("Roberto De Ioris <roberto@unbit.it>, "
1997               "Jaya Kumar <jayakumar.lkml@gmail.com>, "
1998               "Bernie Thompson <bernie@plugable.com>");
1999 MODULE_DESCRIPTION("DisplayLink kernel framebuffer driver");
2000 MODULE_LICENSE("GPL");
2001