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