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