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