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