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