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