regulator: s2mps11: Adjust supported buck voltages to real values
[linux/fpc-iii.git] / drivers / video / fbdev / udlfb.c
blob1d034dddc556949426369d81b0c49b5f3a61176b
1 /*
2 * udlfb.c -- Framebuffer driver for DisplayLink USB controller
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>
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.
12 * Layout is based on skeletonfb by James Simmons and Geert Uytterhoeven,
13 * usb-skeleton by GregKH.
15 * Device-specific portions based on information from Displaylink, with work
16 * from Florian Echtler, Henrik Bjerregaard Pedersen, and others.
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"
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,
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;
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
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,
65 {},
67 MODULE_DEVICE_TABLE(usb, id_table);
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 */
75 struct dlfb_deferred_free {
76 struct list_head list;
77 void *mem;
80 static int dlfb_realloc_framebuffer(struct dlfb_data *dlfb, struct fb_info *info, u32 new_len);
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);
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
93 static char *dlfb_set_register(char *buf, u8 reg, u8 val)
95 *buf++ = 0xAF;
96 *buf++ = 0x20;
97 *buf++ = reg;
98 *buf++ = val;
99 return buf;
102 static char *dlfb_vidreg_lock(char *buf)
104 return dlfb_set_register(buf, 0xFF, 0x00);
107 static char *dlfb_vidreg_unlock(char *buf)
109 return dlfb_set_register(buf, 0xFF, 0xFF);
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
122 static char *dlfb_blanking(char *buf, int fb_blank)
124 u8 reg;
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;
143 buf = dlfb_set_register(buf, 0x1F, reg);
145 return buf;
148 static char *dlfb_set_color_depth(char *buf, u8 selection)
150 return dlfb_set_register(buf, 0x00, selection);
153 static char *dlfb_set_base16bpp(char *wrptr, u32 base)
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);
162 * DisplayLink HW has separate 16bpp and 8bpp framebuffers.
163 * In 24bpp modes, the low 323 RGB bits go in the 8bpp framebuffer
165 static char *dlfb_set_base8bpp(char *wrptr, u32 base)
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);
172 static char *dlfb_set_register_16(char *wrptr, u8 reg, u16 value)
174 wrptr = dlfb_set_register(wrptr, reg, value >> 8);
175 return dlfb_set_register(wrptr, reg+1, value);
179 * This is kind of weird because the controller takes some
180 * register values in a different byte order than other registers.
182 static char *dlfb_set_register_16be(char *wrptr, u8 reg, u16 value)
184 wrptr = dlfb_set_register(wrptr, reg, value);
185 return dlfb_set_register(wrptr, reg+1, value >> 8);
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.
197 static u16 dlfb_lfsr16(u16 actual_count)
199 u32 lv = 0xFFFF; /* This is the lfsr value that the hw starts with */
201 while (actual_count--) {
202 lv = ((lv << 1) |
203 (((lv >> 15) ^ (lv >> 4) ^ (lv >> 2) ^ (lv >> 1)) & 1))
204 & 0xFFFF;
207 return (u16) lv;
211 * This does LFSR conversion on the value that is to be written.
212 * See LFSR explanation above for more detail.
214 static char *dlfb_set_register_lfsr16(char *wrptr, u8 reg, u16 value)
216 return dlfb_set_register_16(wrptr, reg, dlfb_lfsr16(value));
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.
223 static char *dlfb_set_vid_cmds(char *wrptr, struct fb_var_screeninfo *var)
225 u16 xds, yds;
226 u16 xde, yde;
227 u16 yec;
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);
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);
243 /* x end count is active + blanking - 1 */
244 wrptr = dlfb_set_register_lfsr16(wrptr, 0x09,
245 xde + var->right_margin - 1);
247 /* libdlo hardcodes hsync start to 1 */
248 wrptr = dlfb_set_register_lfsr16(wrptr, 0x0B, 1);
250 /* hsync end is width of sync pulse + 1 */
251 wrptr = dlfb_set_register_lfsr16(wrptr, 0x0D, var->hsync_len + 1);
253 /* hpixels is active pixels */
254 wrptr = dlfb_set_register_16(wrptr, 0x0F, var->xres);
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);
261 /* libdlo hardcodes vsync start to 0 */
262 wrptr = dlfb_set_register_lfsr16(wrptr, 0x13, 0);
264 /* vsync end is width of vsync pulse */
265 wrptr = dlfb_set_register_lfsr16(wrptr, 0x15, var->vsync_len);
267 /* vpixels is active pixels */
268 wrptr = dlfb_set_register_16(wrptr, 0x17, var->yres);
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);
274 return wrptr;
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.
282 static int dlfb_set_video_mode(struct dlfb_data *dlfb,
283 struct fb_var_screeninfo *var)
285 char *buf;
286 char *wrptr;
287 int retval;
288 int writesize;
289 struct urb *urb;
291 if (!atomic_read(&dlfb->usb_active))
292 return -EPERM;
294 urb = dlfb_get_urb(dlfb);
295 if (!urb)
296 return -ENOMEM;
298 buf = (char *) urb->transfer_buffer;
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.
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);
312 wrptr = dlfb_set_vid_cmds(wrptr, var);
313 wrptr = dlfb_blanking(wrptr, FB_BLANK_UNBLANK);
314 wrptr = dlfb_vidreg_unlock(wrptr);
316 writesize = wrptr - buf;
318 retval = dlfb_submit_urb(dlfb, urb, writesize);
320 dlfb->blank_mode = FB_BLANK_UNBLANK;
322 return retval;
325 static int dlfb_ops_mmap(struct fb_info *info, struct vm_area_struct *vma)
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;
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;
339 pos = (unsigned long)info->fix.smem_start + offset;
341 dev_dbg(info->dev, "mmap() framebuffer addr:%lu size:%lu\n",
342 pos, size);
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;
349 start += PAGE_SIZE;
350 pos += PAGE_SIZE;
351 if (size > PAGE_SIZE)
352 size -= PAGE_SIZE;
353 else
354 size = 0;
357 return 0;
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
367 static int dlfb_trim_hline(const u8 *bback, const u8 **bfront, int *width_bytes)
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;
377 for (j = 0; j < width; j++) {
378 if (back[j] != front[j]) {
379 start = j;
380 break;
384 for (k = width - 1; k > j; k--) {
385 if (back[k] != front[k]) {
386 end = k+1;
387 break;
391 identical = start + (width - end);
392 *bfront = (u8 *) &front[start];
393 *width_bytes = (end - start) * sizeof(unsigned long);
395 return identical * sizeof(unsigned long);
399 * Render a command stream for an encoded horizontal line segment of pixels.
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
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.
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
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.
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)
434 const uint16_t *pixel = *pixel_start_ptr;
435 uint32_t dev_addr = *device_address_ptr;
436 uint8_t *cmd = *command_buffer_ptr;
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;
445 if (back_buffer_offset &&
446 *pixel == *(u16 *)((u8 *)pixel + back_buffer_offset)) {
447 pixel++;
448 dev_addr += BPP;
449 (*ident_ptr)++;
450 continue;
453 *cmd++ = 0xAF;
454 *cmd++ = 0x6B;
455 *cmd++ = dev_addr >> 16;
456 *cmd++ = dev_addr >> 8;
457 *cmd++ = dev_addr;
459 cmd_pixels_count_byte = cmd++; /* we'll know this later */
460 cmd_pixel_start = pixel;
462 raw_pixels_count_byte = cmd++; /* we'll know this later */
463 raw_pixel_start = pixel;
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);
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--;
476 while (pixel < cmd_pixel_end) {
477 const uint16_t * const repeating_pixel = pixel;
478 u16 pixel_value = *pixel;
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++;
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;
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));
499 /* immediately after raw data is repeat byte */
500 *cmd++ = ((pixel - repeating_pixel) - 1) & 0xFF;
502 /* Then start another raw pixel span */
503 raw_pixel_start = pixel;
504 raw_pixels_count_byte = cmd++;
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--;
516 *cmd_pixels_count_byte = (pixel - cmd_pixel_start) & 0xFF;
517 dev_addr += (u8 *)pixel - (u8 *)cmd_pixel_start;
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;
527 *command_buffer_ptr = cmd;
528 *pixel_start_ptr = pixel;
529 *device_address_ptr = dev_addr;
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.
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)
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;
550 line_start = (u8 *) (front + byte_offset);
551 next_pixel = line_start;
552 line_end = next_pixel + byte_width;
554 if (dlfb->backing_buffer) {
555 int offset;
556 const u8 *back_start = (u8 *) (dlfb->backing_buffer
557 + byte_offset);
559 back_buffer_offset = (unsigned long)back_start - (unsigned long)line_start;
561 *ident_ptr += dlfb_trim_hline(back_start, &next_pixel,
562 &byte_width);
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;
571 while (next_pixel < line_end) {
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);
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];
592 *urb_buf_ptr = cmd;
594 return 0;
597 static int dlfb_handle_damage(struct dlfb_data *dlfb, int x, int y,
598 int width, int height, char *data)
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;
608 start_cycles = get_cycles();
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;
614 if ((width <= 0) ||
615 (x + width > dlfb->info->var.xres) ||
616 (y + height > dlfb->info->var.yres))
617 return -EINVAL;
619 if (!atomic_read(&dlfb->usb_active))
620 return 0;
622 urb = dlfb_get_urb(dlfb);
623 if (!urb)
624 return 0;
625 cmd = urb->transfer_buffer;
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);
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;
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);
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);
658 return 0;
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.
667 static ssize_t dlfb_ops_write(struct fb_info *info, const char __user *buf,
668 size_t count, loff_t *ppos)
670 ssize_t result;
671 struct dlfb_data *dlfb = info->par;
672 u32 offset = (u32) *ppos;
674 result = fb_sys_write(info, buf, count, ppos);
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);
681 dlfb_handle_damage(dlfb, 0, start, info->var.xres,
682 lines, info->screen_base);
685 return result;
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)
693 struct dlfb_data *dlfb = info->par;
695 sys_copyarea(info, area);
697 dlfb_handle_damage(dlfb, area->dx, area->dy,
698 area->width, area->height, info->screen_base);
701 static void dlfb_ops_imageblit(struct fb_info *info,
702 const struct fb_image *image)
704 struct dlfb_data *dlfb = info->par;
706 sys_imageblit(info, image);
708 dlfb_handle_damage(dlfb, image->dx, image->dy,
709 image->width, image->height, info->screen_base);
712 static void dlfb_ops_fillrect(struct fb_info *info,
713 const struct fb_fillrect *rect)
715 struct dlfb_data *dlfb = info->par;
717 sys_fillrect(info, rect);
719 dlfb_handle_damage(dlfb, rect->dx, rect->dy, rect->width,
720 rect->height, info->screen_base);
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.
729 static void dlfb_dpy_deferred_io(struct fb_info *info,
730 struct list_head *pagelist)
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;
742 if (!fb_defio)
743 return;
745 if (!atomic_read(&dlfb->usb_active))
746 return;
748 start_cycles = get_cycles();
750 urb = dlfb_get_urb(dlfb);
751 if (!urb)
752 return;
754 cmd = urb->transfer_buffer;
756 /* walk the written page list and render each to device */
757 list_for_each_entry(cur, &fbdefio->pagelist, lru) {
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;
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);
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);
787 static int dlfb_get_edid(struct dlfb_data *dlfb, char *edid, int len)
789 int i, ret;
790 char *rbuf;
792 rbuf = kmalloc(2, GFP_KERNEL);
793 if (!rbuf)
794 return 0;
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;
807 edid[i] = rbuf[1];
810 kfree(rbuf);
812 return i;
815 static int dlfb_ops_ioctl(struct fb_info *info, unsigned int cmd,
816 unsigned long arg)
819 struct dlfb_data *dlfb = info->par;
821 if (!atomic_read(&dlfb->usb_active))
822 return 0;
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;
832 /* TODO: Help propose a standard fb.h ioctl to report mmap damage */
833 if (cmd == DLFB_IOCTL_REPORT_DAMAGE) {
834 struct dloarea area;
836 if (copy_from_user(&area, (void __user *)arg,
837 sizeof(struct dloarea)))
838 return -EFAULT;
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.
847 if (info->fbdefio)
848 info->fbdefio->delay = DL_DEFIO_WRITE_DISABLE;
850 if (area.x < 0)
851 area.x = 0;
853 if (area.x > info->var.xres)
854 area.x = info->var.xres;
856 if (area.y < 0)
857 area.y = 0;
859 if (area.y > info->var.yres)
860 area.y = info->var.yres;
862 dlfb_handle_damage(dlfb, area.x, area.y, area.w, area.h,
863 info->screen_base);
866 return 0;
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)
874 int err = 0;
876 if (regno >= info->cmap.len)
877 return 1;
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);
893 return err;
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)
901 static int dlfb_ops_open(struct fb_info *info, int user)
903 struct dlfb_data *dlfb = info->par;
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.
910 if ((user == 0) && (!console))
911 return -EBUSY;
913 /* If the USB device is gone, we don't accept new opens */
914 if (dlfb->virtualized)
915 return -ENODEV;
917 dlfb->fb_count++;
919 if (fb_defio && (info->fbdefio == NULL)) {
920 /* enable defio at last moment if not disabled by client */
922 struct fb_deferred_io *fbdefio;
924 fbdefio = kzalloc(sizeof(struct fb_deferred_io), GFP_KERNEL);
926 if (fbdefio) {
927 fbdefio->delay = DL_DEFIO_WRITE_DELAY;
928 fbdefio->deferred_io = dlfb_dpy_deferred_io;
931 info->fbdefio = fbdefio;
932 fb_deferred_io_init(info);
935 dev_dbg(info->dev, "open, user=%d fb_info=%p count=%d\n",
936 user, info, dlfb->fb_count);
938 return 0;
941 static void dlfb_ops_destroy(struct fb_info *info)
943 struct dlfb_data *dlfb = info->par;
945 if (info->cmap.len != 0)
946 fb_dealloc_cmap(&info->cmap);
947 if (info->monspecs.modedb)
948 fb_destroy_modedb(info->monspecs.modedb);
949 vfree(info->screen_base);
951 fb_destroy_modelist(&info->modelist);
953 while (!list_empty(&dlfb->deferred_free)) {
954 struct dlfb_deferred_free *d = list_entry(dlfb->deferred_free.next, struct dlfb_deferred_free, list);
955 list_del(&d->list);
956 vfree(d->mem);
957 kfree(d);
959 vfree(dlfb->backing_buffer);
960 kfree(dlfb->edid);
961 usb_put_dev(dlfb->udev);
962 kfree(dlfb);
964 /* Assume info structure is freed after this point */
965 framebuffer_release(info);
969 * Assumes caller is holding info->lock mutex (for open and release at least)
971 static int dlfb_ops_release(struct fb_info *info, int user)
973 struct dlfb_data *dlfb = info->par;
975 dlfb->fb_count--;
977 if ((dlfb->fb_count == 0) && (info->fbdefio)) {
978 fb_deferred_io_cleanup(info);
979 kfree(info->fbdefio);
980 info->fbdefio = NULL;
981 info->fbops->fb_mmap = dlfb_ops_mmap;
984 dev_dbg(info->dev, "release, user=%d count=%d\n", user, dlfb->fb_count);
986 return 0;
990 * Check whether a video mode is supported by the DisplayLink chip
991 * We start from monitor's modes, so don't need to filter that here
993 static int dlfb_is_valid_mode(struct fb_videomode *mode, struct dlfb_data *dlfb)
995 if (mode->xres * mode->yres > dlfb->sku_pixel_limit)
996 return 0;
998 return 1;
1001 static void dlfb_var_color_format(struct fb_var_screeninfo *var)
1003 const struct fb_bitfield red = { 11, 5, 0 };
1004 const struct fb_bitfield green = { 5, 6, 0 };
1005 const struct fb_bitfield blue = { 0, 5, 0 };
1007 var->bits_per_pixel = 16;
1008 var->red = red;
1009 var->green = green;
1010 var->blue = blue;
1013 static int dlfb_ops_check_var(struct fb_var_screeninfo *var,
1014 struct fb_info *info)
1016 struct fb_videomode mode;
1017 struct dlfb_data *dlfb = info->par;
1019 /* set device-specific elements of var unrelated to mode */
1020 dlfb_var_color_format(var);
1022 fb_var_to_videomode(&mode, var);
1024 if (!dlfb_is_valid_mode(&mode, dlfb))
1025 return -EINVAL;
1027 return 0;
1030 static int dlfb_ops_set_par(struct fb_info *info)
1032 struct dlfb_data *dlfb = info->par;
1033 int result;
1034 u16 *pix_framebuffer;
1035 int i;
1036 struct fb_var_screeninfo fvs;
1037 u32 line_length = info->var.xres * (info->var.bits_per_pixel / 8);
1039 /* clear the activate field because it causes spurious miscompares */
1040 fvs = info->var;
1041 fvs.activate = 0;
1042 fvs.vmode &= ~FB_VMODE_SMOOTH_XPAN;
1044 if (!memcmp(&dlfb->current_mode, &fvs, sizeof(struct fb_var_screeninfo)))
1045 return 0;
1047 result = dlfb_realloc_framebuffer(dlfb, info, info->var.yres * line_length);
1048 if (result)
1049 return result;
1051 result = dlfb_set_video_mode(dlfb, &info->var);
1053 if (result)
1054 return result;
1056 dlfb->current_mode = fvs;
1057 info->fix.line_length = line_length;
1059 if (dlfb->fb_count == 0) {
1061 /* paint greenscreen */
1063 pix_framebuffer = (u16 *) info->screen_base;
1064 for (i = 0; i < info->fix.smem_len / 2; i++)
1065 pix_framebuffer[i] = 0x37e6;
1068 dlfb_handle_damage(dlfb, 0, 0, info->var.xres, info->var.yres,
1069 info->screen_base);
1071 return 0;
1074 /* To fonzi the jukebox (e.g. make blanking changes take effect) */
1075 static char *dlfb_dummy_render(char *buf)
1077 *buf++ = 0xAF;
1078 *buf++ = 0x6A; /* copy */
1079 *buf++ = 0x00; /* from address*/
1080 *buf++ = 0x00;
1081 *buf++ = 0x00;
1082 *buf++ = 0x01; /* one pixel */
1083 *buf++ = 0x00; /* to address */
1084 *buf++ = 0x00;
1085 *buf++ = 0x00;
1086 return buf;
1090 * In order to come back from full DPMS off, we need to set the mode again
1092 static int dlfb_ops_blank(int blank_mode, struct fb_info *info)
1094 struct dlfb_data *dlfb = info->par;
1095 char *bufptr;
1096 struct urb *urb;
1098 dev_dbg(info->dev, "blank, mode %d --> %d\n",
1099 dlfb->blank_mode, blank_mode);
1101 if ((dlfb->blank_mode == FB_BLANK_POWERDOWN) &&
1102 (blank_mode != FB_BLANK_POWERDOWN)) {
1104 /* returning from powerdown requires a fresh modeset */
1105 dlfb_set_video_mode(dlfb, &info->var);
1108 urb = dlfb_get_urb(dlfb);
1109 if (!urb)
1110 return 0;
1112 bufptr = (char *) urb->transfer_buffer;
1113 bufptr = dlfb_vidreg_lock(bufptr);
1114 bufptr = dlfb_blanking(bufptr, blank_mode);
1115 bufptr = dlfb_vidreg_unlock(bufptr);
1117 /* seems like a render op is needed to have blank change take effect */
1118 bufptr = dlfb_dummy_render(bufptr);
1120 dlfb_submit_urb(dlfb, urb, bufptr -
1121 (char *) urb->transfer_buffer);
1123 dlfb->blank_mode = blank_mode;
1125 return 0;
1128 static struct fb_ops dlfb_ops = {
1129 .owner = THIS_MODULE,
1130 .fb_read = fb_sys_read,
1131 .fb_write = dlfb_ops_write,
1132 .fb_setcolreg = dlfb_ops_setcolreg,
1133 .fb_fillrect = dlfb_ops_fillrect,
1134 .fb_copyarea = dlfb_ops_copyarea,
1135 .fb_imageblit = dlfb_ops_imageblit,
1136 .fb_mmap = dlfb_ops_mmap,
1137 .fb_ioctl = dlfb_ops_ioctl,
1138 .fb_open = dlfb_ops_open,
1139 .fb_release = dlfb_ops_release,
1140 .fb_blank = dlfb_ops_blank,
1141 .fb_check_var = dlfb_ops_check_var,
1142 .fb_set_par = dlfb_ops_set_par,
1143 .fb_destroy = dlfb_ops_destroy,
1147 static void dlfb_deferred_vfree(struct dlfb_data *dlfb, void *mem)
1149 struct dlfb_deferred_free *d = kmalloc(sizeof(struct dlfb_deferred_free), GFP_KERNEL);
1150 if (!d)
1151 return;
1152 d->mem = mem;
1153 list_add(&d->list, &dlfb->deferred_free);
1157 * Assumes &info->lock held by caller
1158 * Assumes no active clients have framebuffer open
1160 static int dlfb_realloc_framebuffer(struct dlfb_data *dlfb, struct fb_info *info, u32 new_len)
1162 u32 old_len = info->fix.smem_len;
1163 const void *old_fb = (const void __force *)info->screen_base;
1164 unsigned char *new_fb;
1165 unsigned char *new_back = NULL;
1167 new_len = PAGE_ALIGN(new_len);
1169 if (new_len > old_len) {
1171 * Alloc system memory for virtual framebuffer
1173 new_fb = vmalloc(new_len);
1174 if (!new_fb) {
1175 dev_err(info->dev, "Virtual framebuffer alloc failed\n");
1176 return -ENOMEM;
1178 memset(new_fb, 0xff, new_len);
1180 if (info->screen_base) {
1181 memcpy(new_fb, old_fb, old_len);
1182 dlfb_deferred_vfree(dlfb, (void __force *)info->screen_base);
1185 info->screen_base = (char __iomem *)new_fb;
1186 info->fix.smem_len = new_len;
1187 info->fix.smem_start = (unsigned long) new_fb;
1188 info->flags = udlfb_info_flags;
1191 * Second framebuffer copy to mirror the framebuffer state
1192 * on the physical USB device. We can function without this.
1193 * But with imperfect damage info we may send pixels over USB
1194 * that were, in fact, unchanged - wasting limited USB bandwidth
1196 if (shadow)
1197 new_back = vzalloc(new_len);
1198 if (!new_back)
1199 dev_info(info->dev,
1200 "No shadow/backing buffer allocated\n");
1201 else {
1202 dlfb_deferred_vfree(dlfb, dlfb->backing_buffer);
1203 dlfb->backing_buffer = new_back;
1206 return 0;
1210 * 1) Get EDID from hw, or use sw default
1211 * 2) Parse into various fb_info structs
1212 * 3) Allocate virtual framebuffer memory to back highest res mode
1214 * Parses EDID into three places used by various parts of fbdev:
1215 * fb_var_screeninfo contains the timing of the monitor's preferred mode
1216 * fb_info.monspecs is full parsed EDID info, including monspecs.modedb
1217 * fb_info.modelist is a linked list of all monitor & VESA modes which work
1219 * If EDID is not readable/valid, then modelist is all VESA modes,
1220 * monspecs is NULL, and fb_var_screeninfo is set to safe VESA mode
1221 * Returns 0 if successful
1223 static int dlfb_setup_modes(struct dlfb_data *dlfb,
1224 struct fb_info *info,
1225 char *default_edid, size_t default_edid_size)
1227 char *edid;
1228 int i, result = 0, tries = 3;
1229 struct device *dev = info->device;
1230 struct fb_videomode *mode;
1231 const struct fb_videomode *default_vmode = NULL;
1233 if (info->dev) {
1234 /* only use mutex if info has been registered */
1235 mutex_lock(&info->lock);
1236 /* parent device is used otherwise */
1237 dev = info->dev;
1240 edid = kmalloc(EDID_LENGTH, GFP_KERNEL);
1241 if (!edid) {
1242 result = -ENOMEM;
1243 goto error;
1246 fb_destroy_modelist(&info->modelist);
1247 memset(&info->monspecs, 0, sizeof(info->monspecs));
1250 * Try to (re)read EDID from hardware first
1251 * EDID data may return, but not parse as valid
1252 * Try again a few times, in case of e.g. analog cable noise
1254 while (tries--) {
1256 i = dlfb_get_edid(dlfb, edid, EDID_LENGTH);
1258 if (i >= EDID_LENGTH)
1259 fb_edid_to_monspecs(edid, &info->monspecs);
1261 if (info->monspecs.modedb_len > 0) {
1262 dlfb->edid = edid;
1263 dlfb->edid_size = i;
1264 break;
1268 /* If that fails, use a previously returned EDID if available */
1269 if (info->monspecs.modedb_len == 0) {
1270 dev_err(dev, "Unable to get valid EDID from device/display\n");
1272 if (dlfb->edid) {
1273 fb_edid_to_monspecs(dlfb->edid, &info->monspecs);
1274 if (info->monspecs.modedb_len > 0)
1275 dev_err(dev, "Using previously queried EDID\n");
1279 /* If that fails, use the default EDID we were handed */
1280 if (info->monspecs.modedb_len == 0) {
1281 if (default_edid_size >= EDID_LENGTH) {
1282 fb_edid_to_monspecs(default_edid, &info->monspecs);
1283 if (info->monspecs.modedb_len > 0) {
1284 memcpy(edid, default_edid, default_edid_size);
1285 dlfb->edid = edid;
1286 dlfb->edid_size = default_edid_size;
1287 dev_err(dev, "Using default/backup EDID\n");
1292 /* If we've got modes, let's pick a best default mode */
1293 if (info->monspecs.modedb_len > 0) {
1295 for (i = 0; i < info->monspecs.modedb_len; i++) {
1296 mode = &info->monspecs.modedb[i];
1297 if (dlfb_is_valid_mode(mode, dlfb)) {
1298 fb_add_videomode(mode, &info->modelist);
1299 } else {
1300 dev_dbg(dev, "Specified mode %dx%d too big\n",
1301 mode->xres, mode->yres);
1302 if (i == 0)
1303 /* if we've removed top/best mode */
1304 info->monspecs.misc
1305 &= ~FB_MISC_1ST_DETAIL;
1309 default_vmode = fb_find_best_display(&info->monspecs,
1310 &info->modelist);
1313 /* If everything else has failed, fall back to safe default mode */
1314 if (default_vmode == NULL) {
1316 struct fb_videomode fb_vmode = {0};
1319 * Add the standard VESA modes to our modelist
1320 * Since we don't have EDID, there may be modes that
1321 * overspec monitor and/or are incorrect aspect ratio, etc.
1322 * But at least the user has a chance to choose
1324 for (i = 0; i < VESA_MODEDB_SIZE; i++) {
1325 mode = (struct fb_videomode *)&vesa_modes[i];
1326 if (dlfb_is_valid_mode(mode, dlfb))
1327 fb_add_videomode(mode, &info->modelist);
1328 else
1329 dev_dbg(dev, "VESA mode %dx%d too big\n",
1330 mode->xres, mode->yres);
1334 * default to resolution safe for projectors
1335 * (since they are most common case without EDID)
1337 fb_vmode.xres = 800;
1338 fb_vmode.yres = 600;
1339 fb_vmode.refresh = 60;
1340 default_vmode = fb_find_nearest_mode(&fb_vmode,
1341 &info->modelist);
1344 /* If we have good mode and no active clients*/
1345 if ((default_vmode != NULL) && (dlfb->fb_count == 0)) {
1347 fb_videomode_to_var(&info->var, default_vmode);
1348 dlfb_var_color_format(&info->var);
1351 * with mode size info, we can now alloc our framebuffer.
1353 memcpy(&info->fix, &dlfb_fix, sizeof(dlfb_fix));
1354 } else
1355 result = -EINVAL;
1357 error:
1358 if (edid && (dlfb->edid != edid))
1359 kfree(edid);
1361 if (info->dev)
1362 mutex_unlock(&info->lock);
1364 return result;
1367 static ssize_t metrics_bytes_rendered_show(struct device *fbdev,
1368 struct device_attribute *a, char *buf) {
1369 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1370 struct dlfb_data *dlfb = fb_info->par;
1371 return snprintf(buf, PAGE_SIZE, "%u\n",
1372 atomic_read(&dlfb->bytes_rendered));
1375 static ssize_t metrics_bytes_identical_show(struct device *fbdev,
1376 struct device_attribute *a, char *buf) {
1377 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1378 struct dlfb_data *dlfb = fb_info->par;
1379 return snprintf(buf, PAGE_SIZE, "%u\n",
1380 atomic_read(&dlfb->bytes_identical));
1383 static ssize_t metrics_bytes_sent_show(struct device *fbdev,
1384 struct device_attribute *a, char *buf) {
1385 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1386 struct dlfb_data *dlfb = fb_info->par;
1387 return snprintf(buf, PAGE_SIZE, "%u\n",
1388 atomic_read(&dlfb->bytes_sent));
1391 static ssize_t metrics_cpu_kcycles_used_show(struct device *fbdev,
1392 struct device_attribute *a, char *buf) {
1393 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1394 struct dlfb_data *dlfb = fb_info->par;
1395 return snprintf(buf, PAGE_SIZE, "%u\n",
1396 atomic_read(&dlfb->cpu_kcycles_used));
1399 static ssize_t edid_show(
1400 struct file *filp,
1401 struct kobject *kobj, struct bin_attribute *a,
1402 char *buf, loff_t off, size_t count) {
1403 struct device *fbdev = container_of(kobj, struct device, kobj);
1404 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1405 struct dlfb_data *dlfb = fb_info->par;
1407 if (dlfb->edid == NULL)
1408 return 0;
1410 if ((off >= dlfb->edid_size) || (count > dlfb->edid_size))
1411 return 0;
1413 if (off + count > dlfb->edid_size)
1414 count = dlfb->edid_size - off;
1416 memcpy(buf, dlfb->edid, count);
1418 return count;
1421 static ssize_t edid_store(
1422 struct file *filp,
1423 struct kobject *kobj, struct bin_attribute *a,
1424 char *src, loff_t src_off, size_t src_size) {
1425 struct device *fbdev = container_of(kobj, struct device, kobj);
1426 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1427 struct dlfb_data *dlfb = fb_info->par;
1428 int ret;
1430 /* We only support write of entire EDID at once, no offset*/
1431 if ((src_size != EDID_LENGTH) || (src_off != 0))
1432 return -EINVAL;
1434 ret = dlfb_setup_modes(dlfb, fb_info, src, src_size);
1435 if (ret)
1436 return ret;
1438 if (!dlfb->edid || memcmp(src, dlfb->edid, src_size))
1439 return -EINVAL;
1441 ret = dlfb_ops_set_par(fb_info);
1442 if (ret)
1443 return ret;
1445 return src_size;
1448 static ssize_t metrics_reset_store(struct device *fbdev,
1449 struct device_attribute *attr,
1450 const char *buf, size_t count)
1452 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1453 struct dlfb_data *dlfb = fb_info->par;
1455 atomic_set(&dlfb->bytes_rendered, 0);
1456 atomic_set(&dlfb->bytes_identical, 0);
1457 atomic_set(&dlfb->bytes_sent, 0);
1458 atomic_set(&dlfb->cpu_kcycles_used, 0);
1460 return count;
1463 static const struct bin_attribute edid_attr = {
1464 .attr.name = "edid",
1465 .attr.mode = 0666,
1466 .size = EDID_LENGTH,
1467 .read = edid_show,
1468 .write = edid_store
1471 static const struct device_attribute fb_device_attrs[] = {
1472 __ATTR_RO(metrics_bytes_rendered),
1473 __ATTR_RO(metrics_bytes_identical),
1474 __ATTR_RO(metrics_bytes_sent),
1475 __ATTR_RO(metrics_cpu_kcycles_used),
1476 __ATTR(metrics_reset, S_IWUSR, NULL, metrics_reset_store),
1480 * This is necessary before we can communicate with the display controller.
1482 static int dlfb_select_std_channel(struct dlfb_data *dlfb)
1484 int ret;
1485 void *buf;
1486 static const u8 set_def_chn[] = {
1487 0x57, 0xCD, 0xDC, 0xA7,
1488 0x1C, 0x88, 0x5E, 0x15,
1489 0x60, 0xFE, 0xC6, 0x97,
1490 0x16, 0x3D, 0x47, 0xF2 };
1492 buf = kmemdup(set_def_chn, sizeof(set_def_chn), GFP_KERNEL);
1494 if (!buf)
1495 return -ENOMEM;
1497 ret = usb_control_msg(dlfb->udev, usb_sndctrlpipe(dlfb->udev, 0),
1498 NR_USB_REQUEST_CHANNEL,
1499 (USB_DIR_OUT | USB_TYPE_VENDOR), 0, 0,
1500 buf, sizeof(set_def_chn), USB_CTRL_SET_TIMEOUT);
1502 kfree(buf);
1504 return ret;
1507 static int dlfb_parse_vendor_descriptor(struct dlfb_data *dlfb,
1508 struct usb_interface *intf)
1510 char *desc;
1511 char *buf;
1512 char *desc_end;
1513 int total_len;
1515 buf = kzalloc(MAX_VENDOR_DESCRIPTOR_SIZE, GFP_KERNEL);
1516 if (!buf)
1517 return false;
1518 desc = buf;
1520 total_len = usb_get_descriptor(interface_to_usbdev(intf),
1521 0x5f, /* vendor specific */
1522 0, desc, MAX_VENDOR_DESCRIPTOR_SIZE);
1524 /* if not found, look in configuration descriptor */
1525 if (total_len < 0) {
1526 if (0 == usb_get_extra_descriptor(intf->cur_altsetting,
1527 0x5f, &desc))
1528 total_len = (int) desc[0];
1531 if (total_len > 5) {
1532 dev_info(&intf->dev,
1533 "vendor descriptor length: %d data: %11ph\n",
1534 total_len, desc);
1536 if ((desc[0] != total_len) || /* descriptor length */
1537 (desc[1] != 0x5f) || /* vendor descriptor type */
1538 (desc[2] != 0x01) || /* version (2 bytes) */
1539 (desc[3] != 0x00) ||
1540 (desc[4] != total_len - 2)) /* length after type */
1541 goto unrecognized;
1543 desc_end = desc + total_len;
1544 desc += 5; /* the fixed header we've already parsed */
1546 while (desc < desc_end) {
1547 u8 length;
1548 u16 key;
1550 key = *desc++;
1551 key |= (u16)*desc++ << 8;
1552 length = *desc++;
1554 switch (key) {
1555 case 0x0200: { /* max_area */
1556 u32 max_area = *desc++;
1557 max_area |= (u32)*desc++ << 8;
1558 max_area |= (u32)*desc++ << 16;
1559 max_area |= (u32)*desc++ << 24;
1560 dev_warn(&intf->dev,
1561 "DL chip limited to %d pixel modes\n",
1562 max_area);
1563 dlfb->sku_pixel_limit = max_area;
1564 break;
1566 default:
1567 break;
1569 desc += length;
1571 } else {
1572 dev_info(&intf->dev, "vendor descriptor not available (%d)\n",
1573 total_len);
1576 goto success;
1578 unrecognized:
1579 /* allow udlfb to load for now even if firmware unrecognized */
1580 dev_err(&intf->dev, "Unrecognized vendor firmware descriptor\n");
1582 success:
1583 kfree(buf);
1584 return true;
1587 static int dlfb_usb_probe(struct usb_interface *intf,
1588 const struct usb_device_id *id)
1590 int i;
1591 const struct device_attribute *attr;
1592 struct dlfb_data *dlfb;
1593 struct fb_info *info;
1594 int retval = -ENOMEM;
1595 struct usb_device *usbdev = interface_to_usbdev(intf);
1597 /* usb initialization */
1598 dlfb = kzalloc(sizeof(*dlfb), GFP_KERNEL);
1599 if (!dlfb) {
1600 dev_err(&intf->dev, "%s: failed to allocate dlfb\n", __func__);
1601 return -ENOMEM;
1604 INIT_LIST_HEAD(&dlfb->deferred_free);
1606 dlfb->udev = usb_get_dev(usbdev);
1607 usb_set_intfdata(intf, dlfb);
1609 dev_dbg(&intf->dev, "console enable=%d\n", console);
1610 dev_dbg(&intf->dev, "fb_defio enable=%d\n", fb_defio);
1611 dev_dbg(&intf->dev, "shadow enable=%d\n", shadow);
1613 dlfb->sku_pixel_limit = 2048 * 1152; /* default to maximum */
1615 if (!dlfb_parse_vendor_descriptor(dlfb, intf)) {
1616 dev_err(&intf->dev,
1617 "firmware not recognized, incompatible device?\n");
1618 goto error;
1621 if (pixel_limit) {
1622 dev_warn(&intf->dev,
1623 "DL chip limit of %d overridden to %d\n",
1624 dlfb->sku_pixel_limit, pixel_limit);
1625 dlfb->sku_pixel_limit = pixel_limit;
1629 /* allocates framebuffer driver structure, not framebuffer memory */
1630 info = framebuffer_alloc(0, &dlfb->udev->dev);
1631 if (!info) {
1632 dev_err(&dlfb->udev->dev, "framebuffer_alloc failed\n");
1633 goto error;
1636 dlfb->info = info;
1637 info->par = dlfb;
1638 info->pseudo_palette = dlfb->pseudo_palette;
1639 dlfb->ops = dlfb_ops;
1640 info->fbops = &dlfb->ops;
1642 INIT_LIST_HEAD(&info->modelist);
1644 if (!dlfb_alloc_urb_list(dlfb, WRITES_IN_FLIGHT, MAX_TRANSFER)) {
1645 retval = -ENOMEM;
1646 dev_err(&intf->dev, "unable to allocate urb list\n");
1647 goto error;
1650 /* We don't register a new USB class. Our client interface is dlfbev */
1652 retval = fb_alloc_cmap(&info->cmap, 256, 0);
1653 if (retval < 0) {
1654 dev_err(info->device, "cmap allocation failed: %d\n", retval);
1655 goto error;
1658 retval = dlfb_setup_modes(dlfb, info, NULL, 0);
1659 if (retval != 0) {
1660 dev_err(info->device,
1661 "unable to find common mode for display and adapter\n");
1662 goto error;
1665 /* ready to begin using device */
1667 atomic_set(&dlfb->usb_active, 1);
1668 dlfb_select_std_channel(dlfb);
1670 dlfb_ops_check_var(&info->var, info);
1671 retval = dlfb_ops_set_par(info);
1672 if (retval)
1673 goto error;
1675 retval = register_framebuffer(info);
1676 if (retval < 0) {
1677 dev_err(info->device, "unable to register framebuffer: %d\n",
1678 retval);
1679 goto error;
1682 for (i = 0; i < ARRAY_SIZE(fb_device_attrs); i++) {
1683 attr = &fb_device_attrs[i];
1684 retval = device_create_file(info->dev, attr);
1685 if (retval)
1686 dev_warn(info->device,
1687 "failed to create '%s' attribute: %d\n",
1688 attr->attr.name, retval);
1691 retval = device_create_bin_file(info->dev, &edid_attr);
1692 if (retval)
1693 dev_warn(info->device, "failed to create '%s' attribute: %d\n",
1694 edid_attr.attr.name, retval);
1696 dev_info(info->device,
1697 "%s is DisplayLink USB device (%dx%d, %dK framebuffer memory)\n",
1698 dev_name(info->dev), info->var.xres, info->var.yres,
1699 ((dlfb->backing_buffer) ?
1700 info->fix.smem_len * 2 : info->fix.smem_len) >> 10);
1701 return 0;
1703 error:
1704 if (dlfb->info) {
1705 dlfb_ops_destroy(dlfb->info);
1706 } else {
1707 usb_put_dev(dlfb->udev);
1708 kfree(dlfb);
1710 return retval;
1713 static void dlfb_usb_disconnect(struct usb_interface *intf)
1715 struct dlfb_data *dlfb;
1716 struct fb_info *info;
1717 int i;
1719 dlfb = usb_get_intfdata(intf);
1720 info = dlfb->info;
1722 dev_dbg(&intf->dev, "USB disconnect starting\n");
1724 /* we virtualize until all fb clients release. Then we free */
1725 dlfb->virtualized = true;
1727 /* When non-active we'll update virtual framebuffer, but no new urbs */
1728 atomic_set(&dlfb->usb_active, 0);
1730 /* this function will wait for all in-flight urbs to complete */
1731 dlfb_free_urb_list(dlfb);
1733 /* remove udlfb's sysfs interfaces */
1734 for (i = 0; i < ARRAY_SIZE(fb_device_attrs); i++)
1735 device_remove_file(info->dev, &fb_device_attrs[i]);
1736 device_remove_bin_file(info->dev, &edid_attr);
1738 unregister_framebuffer(info);
1741 static struct usb_driver dlfb_driver = {
1742 .name = "udlfb",
1743 .probe = dlfb_usb_probe,
1744 .disconnect = dlfb_usb_disconnect,
1745 .id_table = id_table,
1748 module_usb_driver(dlfb_driver);
1750 static void dlfb_urb_completion(struct urb *urb)
1752 struct urb_node *unode = urb->context;
1753 struct dlfb_data *dlfb = unode->dlfb;
1754 unsigned long flags;
1756 switch (urb->status) {
1757 case 0:
1758 /* success */
1759 break;
1760 case -ECONNRESET:
1761 case -ENOENT:
1762 case -ESHUTDOWN:
1763 /* sync/async unlink faults aren't errors */
1764 break;
1765 default:
1766 dev_err(&dlfb->udev->dev,
1767 "%s - nonzero write bulk status received: %d\n",
1768 __func__, urb->status);
1769 atomic_set(&dlfb->lost_pixels, 1);
1770 break;
1773 urb->transfer_buffer_length = dlfb->urbs.size; /* reset to actual */
1775 spin_lock_irqsave(&dlfb->urbs.lock, flags);
1776 list_add_tail(&unode->entry, &dlfb->urbs.list);
1777 dlfb->urbs.available++;
1778 spin_unlock_irqrestore(&dlfb->urbs.lock, flags);
1780 up(&dlfb->urbs.limit_sem);
1783 static void dlfb_free_urb_list(struct dlfb_data *dlfb)
1785 int count = dlfb->urbs.count;
1786 struct list_head *node;
1787 struct urb_node *unode;
1788 struct urb *urb;
1790 /* keep waiting and freeing, until we've got 'em all */
1791 while (count--) {
1792 down(&dlfb->urbs.limit_sem);
1794 spin_lock_irq(&dlfb->urbs.lock);
1796 node = dlfb->urbs.list.next; /* have reserved one with sem */
1797 list_del_init(node);
1799 spin_unlock_irq(&dlfb->urbs.lock);
1801 unode = list_entry(node, struct urb_node, entry);
1802 urb = unode->urb;
1804 /* Free each separately allocated piece */
1805 usb_free_coherent(urb->dev, dlfb->urbs.size,
1806 urb->transfer_buffer, urb->transfer_dma);
1807 usb_free_urb(urb);
1808 kfree(node);
1811 dlfb->urbs.count = 0;
1814 static int dlfb_alloc_urb_list(struct dlfb_data *dlfb, int count, size_t size)
1816 struct urb *urb;
1817 struct urb_node *unode;
1818 char *buf;
1819 size_t wanted_size = count * size;
1821 spin_lock_init(&dlfb->urbs.lock);
1823 retry:
1824 dlfb->urbs.size = size;
1825 INIT_LIST_HEAD(&dlfb->urbs.list);
1827 sema_init(&dlfb->urbs.limit_sem, 0);
1828 dlfb->urbs.count = 0;
1829 dlfb->urbs.available = 0;
1831 while (dlfb->urbs.count * size < wanted_size) {
1832 unode = kzalloc(sizeof(*unode), GFP_KERNEL);
1833 if (!unode)
1834 break;
1835 unode->dlfb = dlfb;
1837 urb = usb_alloc_urb(0, GFP_KERNEL);
1838 if (!urb) {
1839 kfree(unode);
1840 break;
1842 unode->urb = urb;
1844 buf = usb_alloc_coherent(dlfb->udev, size, GFP_KERNEL,
1845 &urb->transfer_dma);
1846 if (!buf) {
1847 kfree(unode);
1848 usb_free_urb(urb);
1849 if (size > PAGE_SIZE) {
1850 size /= 2;
1851 dlfb_free_urb_list(dlfb);
1852 goto retry;
1854 break;
1857 /* urb->transfer_buffer_length set to actual before submit */
1858 usb_fill_bulk_urb(urb, dlfb->udev, usb_sndbulkpipe(dlfb->udev, 1),
1859 buf, size, dlfb_urb_completion, unode);
1860 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1862 list_add_tail(&unode->entry, &dlfb->urbs.list);
1864 up(&dlfb->urbs.limit_sem);
1865 dlfb->urbs.count++;
1866 dlfb->urbs.available++;
1869 return dlfb->urbs.count;
1872 static struct urb *dlfb_get_urb(struct dlfb_data *dlfb)
1874 int ret;
1875 struct list_head *entry;
1876 struct urb_node *unode;
1878 /* Wait for an in-flight buffer to complete and get re-queued */
1879 ret = down_timeout(&dlfb->urbs.limit_sem, GET_URB_TIMEOUT);
1880 if (ret) {
1881 atomic_set(&dlfb->lost_pixels, 1);
1882 dev_warn(&dlfb->udev->dev,
1883 "wait for urb interrupted: %d available: %d\n",
1884 ret, dlfb->urbs.available);
1885 return NULL;
1888 spin_lock_irq(&dlfb->urbs.lock);
1890 BUG_ON(list_empty(&dlfb->urbs.list)); /* reserved one with limit_sem */
1891 entry = dlfb->urbs.list.next;
1892 list_del_init(entry);
1893 dlfb->urbs.available--;
1895 spin_unlock_irq(&dlfb->urbs.lock);
1897 unode = list_entry(entry, struct urb_node, entry);
1898 return unode->urb;
1901 static int dlfb_submit_urb(struct dlfb_data *dlfb, struct urb *urb, size_t len)
1903 int ret;
1905 BUG_ON(len > dlfb->urbs.size);
1907 urb->transfer_buffer_length = len; /* set to actual payload len */
1908 ret = usb_submit_urb(urb, GFP_KERNEL);
1909 if (ret) {
1910 dlfb_urb_completion(urb); /* because no one else will */
1911 atomic_set(&dlfb->lost_pixels, 1);
1912 dev_err(&dlfb->udev->dev, "submit urb error: %d\n", ret);
1914 return ret;
1917 module_param(console, bool, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
1918 MODULE_PARM_DESC(console, "Allow fbcon to open framebuffer");
1920 module_param(fb_defio, bool, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
1921 MODULE_PARM_DESC(fb_defio, "Page fault detection of mmap writes");
1923 module_param(shadow, bool, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
1924 MODULE_PARM_DESC(shadow, "Shadow vid mem. Disable to save mem but lose perf");
1926 module_param(pixel_limit, int, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
1927 MODULE_PARM_DESC(pixel_limit, "Force limit on max mode (in x*y pixels)");
1929 MODULE_AUTHOR("Roberto De Ioris <roberto@unbit.it>, "
1930 "Jaya Kumar <jayakumar.lkml@gmail.com>, "
1931 "Bernie Thompson <bernie@plugable.com>");
1932 MODULE_DESCRIPTION("DisplayLink kernel framebuffer driver");
1933 MODULE_LICENSE("GPL");