1 // SPDX-License-Identifier: GPL-2.0-only
3 * udlfb.c -- Framebuffer driver for DisplayLink USB controller
5 * Copyright (C) 2009 Roberto De Ioris <roberto@unbit.it>
6 * Copyright (C) 2009 Jaya Kumar <jayakumar.lkml@gmail.com>
7 * Copyright (C) 2009 Bernie Thompson <bernie@plugable.com>
9 * Layout is based on skeletonfb by James Simmons and Geert Uytterhoeven,
10 * usb-skeleton by GregKH.
12 * Device-specific portions based on information from Displaylink, with work
13 * from Florian Echtler, Henrik Bjerregaard Pedersen, and others.
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>
23 #include <linux/vmalloc.h>
24 #include <linux/slab.h>
25 #include <linux/delay.h>
26 #include <asm/unaligned.h>
27 #include <video/udlfb.h>
30 #define OUT_EP_NUM 1 /* The endpoint number we will use */
32 static const struct fb_fix_screeninfo dlfb_fix = {
34 .type = FB_TYPE_PACKED_PIXELS,
35 .visual = FB_VISUAL_TRUECOLOR,
39 .accel = FB_ACCEL_NONE,
42 static const u32 udlfb_info_flags = FBINFO_DEFAULT | FBINFO_READS_FAST |
44 FBINFO_HWACCEL_IMAGEBLIT | FBINFO_HWACCEL_FILLRECT |
45 FBINFO_HWACCEL_COPYAREA | FBINFO_MISC_ALWAYS_SETPAR;
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
54 static const struct usb_device_id id_table[] = {
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,
66 MODULE_DEVICE_TABLE(usb, id_table);
69 static bool console = true; /* Allow fbcon to open framebuffer */
70 static bool fb_defio = true; /* Detect mmap writes using page faults */
71 static bool shadow = true; /* Optionally disable shadow framebuffer */
72 static int pixel_limit; /* Optionally force a pixel resolution limit */
74 struct dlfb_deferred_free {
75 struct list_head list;
79 static int dlfb_realloc_framebuffer(struct dlfb_data *dlfb, struct fb_info *info, u32 new_len);
81 /* dlfb keeps a list of urbs for efficient bulk transfers */
82 static void dlfb_urb_completion(struct urb *urb);
83 static struct urb *dlfb_get_urb(struct dlfb_data *dlfb);
84 static int dlfb_submit_urb(struct dlfb_data *dlfb, struct urb * urb, size_t len);
85 static int dlfb_alloc_urb_list(struct dlfb_data *dlfb, int count, size_t size);
86 static void dlfb_free_urb_list(struct dlfb_data *dlfb);
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
92 static char *dlfb_set_register(char *buf, u8 reg, u8 val)
101 static char *dlfb_vidreg_lock(char *buf)
103 return dlfb_set_register(buf, 0xFF, 0x00);
106 static char *dlfb_vidreg_unlock(char *buf)
108 return dlfb_set_register(buf, 0xFF, 0xFF);
112 * Map FB_BLANK_* to DisplayLink register
114 * ----- -----------------------------
115 * 0x00 FB_BLANK_UNBLANK (0)
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
121 static char *dlfb_blanking(char *buf, int fb_blank)
126 case FB_BLANK_POWERDOWN:
129 case FB_BLANK_HSYNC_SUSPEND:
132 case FB_BLANK_VSYNC_SUSPEND:
135 case FB_BLANK_NORMAL:
142 buf = dlfb_set_register(buf, 0x1F, reg);
147 static char *dlfb_set_color_depth(char *buf, u8 selection)
149 return dlfb_set_register(buf, 0x00, selection);
152 static char *dlfb_set_base16bpp(char *wrptr, u32 base)
154 /* the base pointer is 16 bits wide, 0x20 is hi byte. */
155 wrptr = dlfb_set_register(wrptr, 0x20, base >> 16);
156 wrptr = dlfb_set_register(wrptr, 0x21, base >> 8);
157 return dlfb_set_register(wrptr, 0x22, base);
161 * DisplayLink HW has separate 16bpp and 8bpp framebuffers.
162 * In 24bpp modes, the low 323 RGB bits go in the 8bpp framebuffer
164 static char *dlfb_set_base8bpp(char *wrptr, u32 base)
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);
171 static char *dlfb_set_register_16(char *wrptr, u8 reg, u16 value)
173 wrptr = dlfb_set_register(wrptr, reg, value >> 8);
174 return dlfb_set_register(wrptr, reg+1, value);
178 * This is kind of weird because the controller takes some
179 * register values in a different byte order than other registers.
181 static char *dlfb_set_register_16be(char *wrptr, u8 reg, u16 value)
183 wrptr = dlfb_set_register(wrptr, reg, value);
184 return dlfb_set_register(wrptr, reg+1, value >> 8);
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.
196 static u16 dlfb_lfsr16(u16 actual_count)
198 u32 lv = 0xFFFF; /* This is the lfsr value that the hw starts with */
200 while (actual_count--) {
202 (((lv >> 15) ^ (lv >> 4) ^ (lv >> 2) ^ (lv >> 1)) & 1))
210 * This does LFSR conversion on the value that is to be written.
211 * See LFSR explanation above for more detail.
213 static char *dlfb_set_register_lfsr16(char *wrptr, u8 reg, u16 value)
215 return dlfb_set_register_16(wrptr, reg, dlfb_lfsr16(value));
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.
222 static char *dlfb_set_vid_cmds(char *wrptr, struct fb_var_screeninfo *var)
228 /* x display start */
229 xds = var->left_margin + var->hsync_len;
230 wrptr = dlfb_set_register_lfsr16(wrptr, 0x01, xds);
232 xde = xds + var->xres;
233 wrptr = dlfb_set_register_lfsr16(wrptr, 0x03, xde);
235 /* y display start */
236 yds = var->upper_margin + var->vsync_len;
237 wrptr = dlfb_set_register_lfsr16(wrptr, 0x05, yds);
239 yde = yds + var->yres;
240 wrptr = dlfb_set_register_lfsr16(wrptr, 0x07, yde);
242 /* x end count is active + blanking - 1 */
243 wrptr = dlfb_set_register_lfsr16(wrptr, 0x09,
244 xde + var->right_margin - 1);
246 /* libdlo hardcodes hsync start to 1 */
247 wrptr = dlfb_set_register_lfsr16(wrptr, 0x0B, 1);
249 /* hsync end is width of sync pulse + 1 */
250 wrptr = dlfb_set_register_lfsr16(wrptr, 0x0D, var->hsync_len + 1);
252 /* hpixels is active pixels */
253 wrptr = dlfb_set_register_16(wrptr, 0x0F, var->xres);
255 /* yendcount is vertical active + vertical blanking */
256 yec = var->yres + var->upper_margin + var->lower_margin +
258 wrptr = dlfb_set_register_lfsr16(wrptr, 0x11, yec);
260 /* libdlo hardcodes vsync start to 0 */
261 wrptr = dlfb_set_register_lfsr16(wrptr, 0x13, 0);
263 /* vsync end is width of vsync pulse */
264 wrptr = dlfb_set_register_lfsr16(wrptr, 0x15, var->vsync_len);
266 /* vpixels is active pixels */
267 wrptr = dlfb_set_register_16(wrptr, 0x17, var->yres);
269 /* convert picoseconds to 5kHz multiple for pclk5k = x * 1E12/5k */
270 wrptr = dlfb_set_register_16be(wrptr, 0x1B,
271 200*1000*1000/var->pixclock);
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.
281 static int dlfb_set_video_mode(struct dlfb_data *dlfb,
282 struct fb_var_screeninfo *var)
290 if (!atomic_read(&dlfb->usb_active))
293 urb = dlfb_get_urb(dlfb);
297 buf = (char *) urb->transfer_buffer;
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.
304 wrptr = dlfb_vidreg_lock(buf);
305 wrptr = dlfb_set_color_depth(wrptr, 0x00);
306 /* set base for 16bpp segment to 0 */
307 wrptr = dlfb_set_base16bpp(wrptr, 0);
308 /* set base for 8bpp segment to end of fb */
309 wrptr = dlfb_set_base8bpp(wrptr, dlfb->info->fix.smem_len);
311 wrptr = dlfb_set_vid_cmds(wrptr, var);
312 wrptr = dlfb_blanking(wrptr, FB_BLANK_UNBLANK);
313 wrptr = dlfb_vidreg_unlock(wrptr);
315 writesize = wrptr - buf;
317 retval = dlfb_submit_urb(dlfb, urb, writesize);
319 dlfb->blank_mode = FB_BLANK_UNBLANK;
324 static int dlfb_ops_mmap(struct fb_info *info, struct vm_area_struct *vma)
326 unsigned long start = vma->vm_start;
327 unsigned long size = vma->vm_end - vma->vm_start;
328 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
329 unsigned long page, pos;
332 return fb_deferred_io_mmap(info, vma);
334 if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT))
336 if (size > info->fix.smem_len)
338 if (offset > info->fix.smem_len - size)
341 pos = (unsigned long)info->fix.smem_start + offset;
343 dev_dbg(info->dev, "mmap() framebuffer addr:%lu size:%lu\n",
347 page = vmalloc_to_pfn((void *)pos);
348 if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED))
353 if (size > PAGE_SIZE)
363 * Trims identical data from front and back of line
364 * Sets new front buffer address and width
365 * And returns byte count of identical pixels
366 * Assumes CPU natural alignment (unsigned long)
367 * for back and front buffer ptrs and width
369 static int dlfb_trim_hline(const u8 *bback, const u8 **bfront, int *width_bytes)
372 const unsigned long *back = (const unsigned long *) bback;
373 const unsigned long *front = (const unsigned long *) *bfront;
374 const int width = *width_bytes / sizeof(unsigned long);
379 for (j = 0; j < width; j++) {
380 if (back[j] != front[j]) {
386 for (k = width - 1; k > j; k--) {
387 if (back[k] != front[k]) {
393 identical = start + (width - end);
394 *bfront = (u8 *) &front[start];
395 *width_bytes = (end - start) * sizeof(unsigned long);
397 return identical * sizeof(unsigned long);
401 * Render a command stream for an encoded horizontal line segment of pixels.
403 * A command buffer holds several commands.
404 * It always begins with a fresh command header
405 * (the protocol doesn't require this, but we enforce it to allow
406 * multiple buffers to be potentially encoded and sent in parallel).
407 * A single command encodes one contiguous horizontal line of pixels
409 * The function relies on the client to do all allocation, so that
410 * rendering can be done directly to output buffers (e.g. USB URBs).
411 * The function fills the supplied command buffer, providing information
412 * on where it left off, so the client may call in again with additional
413 * buffers if the line will take several buffers to complete.
415 * A single command can transmit a maximum of 256 pixels,
416 * regardless of the compression ratio (protocol design limit).
417 * To the hardware, 0 for a size byte means 256
419 * Rather than 256 pixel commands which are either rl or raw encoded,
420 * the rlx command simply assumes alternating raw and rl spans within one cmd.
421 * This has a slightly larger header overhead, but produces more even results.
422 * It also processes all data (read and write) in a single pass.
423 * Performance benchmarks of common cases show it having just slightly better
424 * compression than 256 pixel raw or rle commands, with similar CPU consumpion.
425 * But for very rl friendly data, will compress not quite as well.
427 static void dlfb_compress_hline(
428 const uint16_t **pixel_start_ptr,
429 const uint16_t *const pixel_end,
430 uint32_t *device_address_ptr,
431 uint8_t **command_buffer_ptr,
432 const uint8_t *const cmd_buffer_end,
433 unsigned long back_buffer_offset,
436 const uint16_t *pixel = *pixel_start_ptr;
437 uint32_t dev_addr = *device_address_ptr;
438 uint8_t *cmd = *command_buffer_ptr;
440 while ((pixel_end > pixel) &&
441 (cmd_buffer_end - MIN_RLX_CMD_BYTES > cmd)) {
442 uint8_t *raw_pixels_count_byte = NULL;
443 uint8_t *cmd_pixels_count_byte = NULL;
444 const uint16_t *raw_pixel_start = NULL;
445 const uint16_t *cmd_pixel_start, *cmd_pixel_end = NULL;
447 if (back_buffer_offset &&
448 *pixel == *(u16 *)((u8 *)pixel + back_buffer_offset)) {
457 *cmd++ = dev_addr >> 16;
458 *cmd++ = dev_addr >> 8;
461 cmd_pixels_count_byte = cmd++; /* we'll know this later */
462 cmd_pixel_start = pixel;
464 raw_pixels_count_byte = cmd++; /* we'll know this later */
465 raw_pixel_start = pixel;
467 cmd_pixel_end = pixel + min3(MAX_CMD_PIXELS + 1UL,
468 (unsigned long)(pixel_end - pixel),
469 (unsigned long)(cmd_buffer_end - 1 - cmd) / BPP);
471 if (back_buffer_offset) {
472 /* note: the framebuffer may change under us, so we must test for underflow */
473 while (cmd_pixel_end - 1 > pixel &&
474 *(cmd_pixel_end - 1) == *(u16 *)((u8 *)(cmd_pixel_end - 1) + back_buffer_offset))
478 while (pixel < cmd_pixel_end) {
479 const uint16_t * const repeating_pixel = pixel;
480 u16 pixel_value = *pixel;
482 put_unaligned_be16(pixel_value, cmd);
483 if (back_buffer_offset)
484 *(u16 *)((u8 *)pixel + back_buffer_offset) = pixel_value;
488 if (unlikely((pixel < cmd_pixel_end) &&
489 (*pixel == pixel_value))) {
490 /* go back and fill in raw pixel count */
491 *raw_pixels_count_byte = ((repeating_pixel -
492 raw_pixel_start) + 1) & 0xFF;
495 if (back_buffer_offset)
496 *(u16 *)((u8 *)pixel + back_buffer_offset) = pixel_value;
498 } while ((pixel < cmd_pixel_end) &&
499 (*pixel == pixel_value));
501 /* immediately after raw data is repeat byte */
502 *cmd++ = ((pixel - repeating_pixel) - 1) & 0xFF;
504 /* Then start another raw pixel span */
505 raw_pixel_start = pixel;
506 raw_pixels_count_byte = cmd++;
510 if (pixel > raw_pixel_start) {
511 /* finalize last RAW span */
512 *raw_pixels_count_byte = (pixel-raw_pixel_start) & 0xFF;
514 /* undo unused byte */
518 *cmd_pixels_count_byte = (pixel - cmd_pixel_start) & 0xFF;
519 dev_addr += (u8 *)pixel - (u8 *)cmd_pixel_start;
522 if (cmd_buffer_end - MIN_RLX_CMD_BYTES <= cmd) {
523 /* Fill leftover bytes with no-ops */
524 if (cmd_buffer_end > cmd)
525 memset(cmd, 0xAF, cmd_buffer_end - cmd);
526 cmd = (uint8_t *) cmd_buffer_end;
529 *command_buffer_ptr = cmd;
530 *pixel_start_ptr = pixel;
531 *device_address_ptr = dev_addr;
535 * There are 3 copies of every pixel: The front buffer that the fbdev
536 * client renders to, the actual framebuffer across the USB bus in hardware
537 * (that we can only write to, slowly, and can never read), and (optionally)
538 * our shadow copy that tracks what's been sent to that hardware buffer.
540 static int dlfb_render_hline(struct dlfb_data *dlfb, struct urb **urb_ptr,
541 const char *front, char **urb_buf_ptr,
542 u32 byte_offset, u32 byte_width,
543 int *ident_ptr, int *sent_ptr)
545 const u8 *line_start, *line_end, *next_pixel;
546 u32 dev_addr = dlfb->base16 + byte_offset;
547 struct urb *urb = *urb_ptr;
548 u8 *cmd = *urb_buf_ptr;
549 u8 *cmd_end = (u8 *) urb->transfer_buffer + urb->transfer_buffer_length;
550 unsigned long back_buffer_offset = 0;
552 line_start = (u8 *) (front + byte_offset);
553 next_pixel = line_start;
554 line_end = next_pixel + byte_width;
556 if (dlfb->backing_buffer) {
558 const u8 *back_start = (u8 *) (dlfb->backing_buffer
561 back_buffer_offset = (unsigned long)back_start - (unsigned long)line_start;
563 *ident_ptr += dlfb_trim_hline(back_start, &next_pixel,
566 offset = next_pixel - line_start;
567 line_end = next_pixel + byte_width;
569 back_start += offset;
570 line_start += offset;
573 while (next_pixel < line_end) {
575 dlfb_compress_hline((const uint16_t **) &next_pixel,
576 (const uint16_t *) line_end, &dev_addr,
577 (u8 **) &cmd, (u8 *) cmd_end, back_buffer_offset,
580 if (cmd >= cmd_end) {
581 int len = cmd - (u8 *) urb->transfer_buffer;
582 if (dlfb_submit_urb(dlfb, urb, len))
583 return 1; /* lost pixels is set */
585 urb = dlfb_get_urb(dlfb);
587 return 1; /* lost_pixels is set */
589 cmd = urb->transfer_buffer;
590 cmd_end = &cmd[urb->transfer_buffer_length];
599 static int dlfb_handle_damage(struct dlfb_data *dlfb, int x, int y, int width, int height)
603 cycles_t start_cycles, end_cycles;
605 int bytes_identical = 0;
609 start_cycles = get_cycles();
611 mutex_lock(&dlfb->render_mutex);
613 aligned_x = DL_ALIGN_DOWN(x, sizeof(unsigned long));
614 width = DL_ALIGN_UP(width + (x-aligned_x), sizeof(unsigned long));
618 (x + width > dlfb->info->var.xres) ||
619 (y + height > dlfb->info->var.yres)) {
624 if (!atomic_read(&dlfb->usb_active)) {
629 urb = dlfb_get_urb(dlfb);
634 cmd = urb->transfer_buffer;
636 for (i = y; i < y + height ; i++) {
637 const int line_offset = dlfb->info->fix.line_length * i;
638 const int byte_offset = line_offset + (x * BPP);
640 if (dlfb_render_hline(dlfb, &urb,
641 (char *) dlfb->info->fix.smem_start,
642 &cmd, byte_offset, width * BPP,
643 &bytes_identical, &bytes_sent))
647 if (cmd > (char *) urb->transfer_buffer) {
649 if (cmd < (char *) urb->transfer_buffer + urb->transfer_buffer_length)
651 /* Send partial buffer remaining before exiting */
652 len = cmd - (char *) urb->transfer_buffer;
653 dlfb_submit_urb(dlfb, urb, len);
656 dlfb_urb_completion(urb);
659 atomic_add(bytes_sent, &dlfb->bytes_sent);
660 atomic_add(bytes_identical, &dlfb->bytes_identical);
661 atomic_add(width*height*2, &dlfb->bytes_rendered);
662 end_cycles = get_cycles();
663 atomic_add(((unsigned int) ((end_cycles - start_cycles)
664 >> 10)), /* Kcycles */
665 &dlfb->cpu_kcycles_used);
670 mutex_unlock(&dlfb->render_mutex);
674 static void dlfb_init_damage(struct dlfb_data *dlfb)
676 dlfb->damage_x = INT_MAX;
678 dlfb->damage_y = INT_MAX;
682 static void dlfb_damage_work(struct work_struct *w)
684 struct dlfb_data *dlfb = container_of(w, struct dlfb_data, damage_work);
687 spin_lock_irq(&dlfb->damage_lock);
689 x2 = dlfb->damage_x2;
691 y2 = dlfb->damage_y2;
692 dlfb_init_damage(dlfb);
693 spin_unlock_irq(&dlfb->damage_lock);
695 if (x < x2 && y < y2)
696 dlfb_handle_damage(dlfb, x, y, x2 - x, y2 - y);
699 static void dlfb_offload_damage(struct dlfb_data *dlfb, int x, int y, int width, int height)
705 if (x >= x2 || y >= y2)
708 spin_lock_irqsave(&dlfb->damage_lock, flags);
709 dlfb->damage_x = min(x, dlfb->damage_x);
710 dlfb->damage_x2 = max(x2, dlfb->damage_x2);
711 dlfb->damage_y = min(y, dlfb->damage_y);
712 dlfb->damage_y2 = max(y2, dlfb->damage_y2);
713 spin_unlock_irqrestore(&dlfb->damage_lock, flags);
715 schedule_work(&dlfb->damage_work);
719 * Path triggered by usermode clients who write to filesystem
720 * e.g. cat filename > /dev/fb1
721 * Not used by X Windows or text-mode console. But useful for testing.
722 * Slow because of extra copy and we must assume all pixels dirty.
724 static ssize_t dlfb_ops_write(struct fb_info *info, const char __user *buf,
725 size_t count, loff_t *ppos)
728 struct dlfb_data *dlfb = info->par;
729 u32 offset = (u32) *ppos;
731 result = fb_sys_write(info, buf, count, ppos);
734 int start = max((int)(offset / info->fix.line_length), 0);
735 int lines = min((u32)((result / info->fix.line_length) + 1),
736 (u32)info->var.yres);
738 dlfb_handle_damage(dlfb, 0, start, info->var.xres,
745 /* hardware has native COPY command (see libdlo), but not worth it for fbcon */
746 static void dlfb_ops_copyarea(struct fb_info *info,
747 const struct fb_copyarea *area)
750 struct dlfb_data *dlfb = info->par;
752 sys_copyarea(info, area);
754 dlfb_offload_damage(dlfb, area->dx, area->dy,
755 area->width, area->height);
758 static void dlfb_ops_imageblit(struct fb_info *info,
759 const struct fb_image *image)
761 struct dlfb_data *dlfb = info->par;
763 sys_imageblit(info, image);
765 dlfb_offload_damage(dlfb, image->dx, image->dy,
766 image->width, image->height);
769 static void dlfb_ops_fillrect(struct fb_info *info,
770 const struct fb_fillrect *rect)
772 struct dlfb_data *dlfb = info->par;
774 sys_fillrect(info, rect);
776 dlfb_offload_damage(dlfb, rect->dx, rect->dy, rect->width,
781 * NOTE: fb_defio.c is holding info->fbdefio.mutex
782 * Touching ANY framebuffer memory that triggers a page fault
783 * in fb_defio will cause a deadlock, when it also tries to
784 * grab the same mutex.
786 static void dlfb_dpy_deferred_io(struct fb_info *info, struct list_head *pagereflist)
788 struct fb_deferred_io_pageref *pageref;
789 struct dlfb_data *dlfb = info->par;
792 cycles_t start_cycles, end_cycles;
794 int bytes_identical = 0;
795 int bytes_rendered = 0;
797 mutex_lock(&dlfb->render_mutex);
802 if (!atomic_read(&dlfb->usb_active))
805 start_cycles = get_cycles();
807 urb = dlfb_get_urb(dlfb);
811 cmd = urb->transfer_buffer;
813 /* walk the written page list and render each to device */
814 list_for_each_entry(pageref, pagereflist, list) {
815 if (dlfb_render_hline(dlfb, &urb, (char *) info->fix.smem_start,
816 &cmd, pageref->offset, PAGE_SIZE,
817 &bytes_identical, &bytes_sent))
819 bytes_rendered += PAGE_SIZE;
822 if (cmd > (char *) urb->transfer_buffer) {
824 if (cmd < (char *) urb->transfer_buffer + urb->transfer_buffer_length)
826 /* Send partial buffer remaining before exiting */
827 len = cmd - (char *) urb->transfer_buffer;
828 dlfb_submit_urb(dlfb, urb, len);
831 dlfb_urb_completion(urb);
834 atomic_add(bytes_sent, &dlfb->bytes_sent);
835 atomic_add(bytes_identical, &dlfb->bytes_identical);
836 atomic_add(bytes_rendered, &dlfb->bytes_rendered);
837 end_cycles = get_cycles();
838 atomic_add(((unsigned int) ((end_cycles - start_cycles)
839 >> 10)), /* Kcycles */
840 &dlfb->cpu_kcycles_used);
842 mutex_unlock(&dlfb->render_mutex);
845 static int dlfb_get_edid(struct dlfb_data *dlfb, char *edid, int len)
850 rbuf = kmalloc(2, GFP_KERNEL);
854 for (i = 0; i < len; i++) {
855 ret = usb_control_msg(dlfb->udev,
856 usb_rcvctrlpipe(dlfb->udev, 0), 0x02,
857 (0x80 | (0x02 << 5)), i << 8, 0xA1,
858 rbuf, 2, USB_CTRL_GET_TIMEOUT);
860 dev_err(&dlfb->udev->dev,
861 "Read EDID byte %d failed: %d\n", i, ret);
873 static int dlfb_ops_ioctl(struct fb_info *info, unsigned int cmd,
877 struct dlfb_data *dlfb = info->par;
879 if (!atomic_read(&dlfb->usb_active))
882 /* TODO: Update X server to get this from sysfs instead */
883 if (cmd == DLFB_IOCTL_RETURN_EDID) {
884 void __user *edid = (void __user *)arg;
885 if (copy_to_user(edid, dlfb->edid, dlfb->edid_size))
890 /* TODO: Help propose a standard fb.h ioctl to report mmap damage */
891 if (cmd == DLFB_IOCTL_REPORT_DAMAGE) {
894 if (copy_from_user(&area, (void __user *)arg,
895 sizeof(struct dloarea)))
899 * If we have a damage-aware client, turn fb_defio "off"
900 * To avoid perf imact of unnecessary page fault handling.
901 * Done by resetting the delay for this fb_info to a very
902 * long period. Pages will become writable and stay that way.
903 * Reset to normal value when all clients have closed this fb.
906 info->fbdefio->delay = DL_DEFIO_WRITE_DISABLE;
911 if (area.x > info->var.xres)
912 area.x = info->var.xres;
917 if (area.y > info->var.yres)
918 area.y = info->var.yres;
920 dlfb_handle_damage(dlfb, area.x, area.y, area.w, area.h);
926 /* taken from vesafb */
928 dlfb_ops_setcolreg(unsigned regno, unsigned red, unsigned green,
929 unsigned blue, unsigned transp, struct fb_info *info)
933 if (regno >= info->cmap.len)
937 if (info->var.red.offset == 10) {
939 ((u32 *) (info->pseudo_palette))[regno] =
940 ((red & 0xf800) >> 1) |
941 ((green & 0xf800) >> 6) | ((blue & 0xf800) >> 11);
944 ((u32 *) (info->pseudo_palette))[regno] =
946 ((green & 0xfc00) >> 5) | ((blue & 0xf800) >> 11);
954 * It's common for several clients to have framebuffer open simultaneously.
955 * e.g. both fbcon and X. Makes things interesting.
956 * Assumes caller is holding info->lock (for open and release at least)
958 static int dlfb_ops_open(struct fb_info *info, int user)
960 struct dlfb_data *dlfb = info->par;
963 * fbcon aggressively connects to first framebuffer it finds,
964 * preventing other clients (X) from working properly. Usually
965 * not what the user wants. Fail by default with option to enable.
967 if ((user == 0) && (!console))
970 /* If the USB device is gone, we don't accept new opens */
971 if (dlfb->virtualized)
976 if (fb_defio && (info->fbdefio == NULL)) {
977 /* enable defio at last moment if not disabled by client */
979 struct fb_deferred_io *fbdefio;
981 fbdefio = kzalloc(sizeof(struct fb_deferred_io), GFP_KERNEL);
984 fbdefio->delay = DL_DEFIO_WRITE_DELAY;
985 fbdefio->sort_pagereflist = true;
986 fbdefio->deferred_io = dlfb_dpy_deferred_io;
989 info->fbdefio = fbdefio;
990 fb_deferred_io_init(info);
993 dev_dbg(info->dev, "open, user=%d fb_info=%p count=%d\n",
994 user, info, dlfb->fb_count);
999 static void dlfb_ops_destroy(struct fb_info *info)
1001 struct dlfb_data *dlfb = info->par;
1003 cancel_work_sync(&dlfb->damage_work);
1005 mutex_destroy(&dlfb->render_mutex);
1007 if (info->cmap.len != 0)
1008 fb_dealloc_cmap(&info->cmap);
1009 if (info->monspecs.modedb)
1010 fb_destroy_modedb(info->monspecs.modedb);
1011 vfree(info->screen_base);
1013 fb_destroy_modelist(&info->modelist);
1015 while (!list_empty(&dlfb->deferred_free)) {
1016 struct dlfb_deferred_free *d = list_entry(dlfb->deferred_free.next, struct dlfb_deferred_free, list);
1021 vfree(dlfb->backing_buffer);
1023 dlfb_free_urb_list(dlfb);
1024 usb_put_dev(dlfb->udev);
1027 /* Assume info structure is freed after this point */
1028 framebuffer_release(info);
1032 * Assumes caller is holding info->lock mutex (for open and release at least)
1034 static int dlfb_ops_release(struct fb_info *info, int user)
1036 struct dlfb_data *dlfb = info->par;
1040 if ((dlfb->fb_count == 0) && (info->fbdefio)) {
1041 fb_deferred_io_cleanup(info);
1042 kfree(info->fbdefio);
1043 info->fbdefio = NULL;
1046 dev_dbg(info->dev, "release, user=%d count=%d\n", user, dlfb->fb_count);
1052 * Check whether a video mode is supported by the DisplayLink chip
1053 * We start from monitor's modes, so don't need to filter that here
1055 static int dlfb_is_valid_mode(struct fb_videomode *mode, struct dlfb_data *dlfb)
1057 if (mode->xres * mode->yres > dlfb->sku_pixel_limit)
1063 static void dlfb_var_color_format(struct fb_var_screeninfo *var)
1065 const struct fb_bitfield red = { 11, 5, 0 };
1066 const struct fb_bitfield green = { 5, 6, 0 };
1067 const struct fb_bitfield blue = { 0, 5, 0 };
1069 var->bits_per_pixel = 16;
1075 static int dlfb_ops_check_var(struct fb_var_screeninfo *var,
1076 struct fb_info *info)
1078 struct fb_videomode mode;
1079 struct dlfb_data *dlfb = info->par;
1081 /* set device-specific elements of var unrelated to mode */
1082 dlfb_var_color_format(var);
1084 fb_var_to_videomode(&mode, var);
1086 if (!dlfb_is_valid_mode(&mode, dlfb))
1092 static int dlfb_ops_set_par(struct fb_info *info)
1094 struct dlfb_data *dlfb = info->par;
1096 u16 *pix_framebuffer;
1098 struct fb_var_screeninfo fvs;
1099 u32 line_length = info->var.xres * (info->var.bits_per_pixel / 8);
1101 /* clear the activate field because it causes spurious miscompares */
1104 fvs.vmode &= ~FB_VMODE_SMOOTH_XPAN;
1106 if (!memcmp(&dlfb->current_mode, &fvs, sizeof(struct fb_var_screeninfo)))
1109 result = dlfb_realloc_framebuffer(dlfb, info, info->var.yres * line_length);
1113 result = dlfb_set_video_mode(dlfb, &info->var);
1118 dlfb->current_mode = fvs;
1119 info->fix.line_length = line_length;
1121 if (dlfb->fb_count == 0) {
1123 /* paint greenscreen */
1125 pix_framebuffer = (u16 *) info->screen_base;
1126 for (i = 0; i < info->fix.smem_len / 2; i++)
1127 pix_framebuffer[i] = 0x37e6;
1130 dlfb_handle_damage(dlfb, 0, 0, info->var.xres, info->var.yres);
1135 /* To fonzi the jukebox (e.g. make blanking changes take effect) */
1136 static char *dlfb_dummy_render(char *buf)
1139 *buf++ = 0x6A; /* copy */
1140 *buf++ = 0x00; /* from address*/
1143 *buf++ = 0x01; /* one pixel */
1144 *buf++ = 0x00; /* to address */
1151 * In order to come back from full DPMS off, we need to set the mode again
1153 static int dlfb_ops_blank(int blank_mode, struct fb_info *info)
1155 struct dlfb_data *dlfb = info->par;
1159 dev_dbg(info->dev, "blank, mode %d --> %d\n",
1160 dlfb->blank_mode, blank_mode);
1162 if ((dlfb->blank_mode == FB_BLANK_POWERDOWN) &&
1163 (blank_mode != FB_BLANK_POWERDOWN)) {
1165 /* returning from powerdown requires a fresh modeset */
1166 dlfb_set_video_mode(dlfb, &info->var);
1169 urb = dlfb_get_urb(dlfb);
1173 bufptr = (char *) urb->transfer_buffer;
1174 bufptr = dlfb_vidreg_lock(bufptr);
1175 bufptr = dlfb_blanking(bufptr, blank_mode);
1176 bufptr = dlfb_vidreg_unlock(bufptr);
1178 /* seems like a render op is needed to have blank change take effect */
1179 bufptr = dlfb_dummy_render(bufptr);
1181 dlfb_submit_urb(dlfb, urb, bufptr -
1182 (char *) urb->transfer_buffer);
1184 dlfb->blank_mode = blank_mode;
1189 static const struct fb_ops dlfb_ops = {
1190 .owner = THIS_MODULE,
1191 .fb_read = fb_sys_read,
1192 .fb_write = dlfb_ops_write,
1193 .fb_setcolreg = dlfb_ops_setcolreg,
1194 .fb_fillrect = dlfb_ops_fillrect,
1195 .fb_copyarea = dlfb_ops_copyarea,
1196 .fb_imageblit = dlfb_ops_imageblit,
1197 .fb_mmap = dlfb_ops_mmap,
1198 .fb_ioctl = dlfb_ops_ioctl,
1199 .fb_open = dlfb_ops_open,
1200 .fb_release = dlfb_ops_release,
1201 .fb_blank = dlfb_ops_blank,
1202 .fb_check_var = dlfb_ops_check_var,
1203 .fb_set_par = dlfb_ops_set_par,
1204 .fb_destroy = dlfb_ops_destroy,
1208 static void dlfb_deferred_vfree(struct dlfb_data *dlfb, void *mem)
1210 struct dlfb_deferred_free *d = kmalloc(sizeof(struct dlfb_deferred_free), GFP_KERNEL);
1214 list_add(&d->list, &dlfb->deferred_free);
1218 * Assumes &info->lock held by caller
1219 * Assumes no active clients have framebuffer open
1221 static int dlfb_realloc_framebuffer(struct dlfb_data *dlfb, struct fb_info *info, u32 new_len)
1223 u32 old_len = info->fix.smem_len;
1224 const void *old_fb = (const void __force *)info->screen_base;
1225 unsigned char *new_fb;
1226 unsigned char *new_back = NULL;
1228 new_len = PAGE_ALIGN(new_len);
1230 if (new_len > old_len) {
1232 * Alloc system memory for virtual framebuffer
1234 new_fb = vmalloc(new_len);
1236 dev_err(info->dev, "Virtual framebuffer alloc failed\n");
1239 memset(new_fb, 0xff, new_len);
1241 if (info->screen_base) {
1242 memcpy(new_fb, old_fb, old_len);
1243 dlfb_deferred_vfree(dlfb, (void __force *)info->screen_base);
1246 info->screen_base = (char __iomem *)new_fb;
1247 info->fix.smem_len = new_len;
1248 info->fix.smem_start = (unsigned long) new_fb;
1249 info->flags = udlfb_info_flags;
1252 * Second framebuffer copy to mirror the framebuffer state
1253 * on the physical USB device. We can function without this.
1254 * But with imperfect damage info we may send pixels over USB
1255 * that were, in fact, unchanged - wasting limited USB bandwidth
1258 new_back = vzalloc(new_len);
1261 "No shadow/backing buffer allocated\n");
1263 dlfb_deferred_vfree(dlfb, dlfb->backing_buffer);
1264 dlfb->backing_buffer = new_back;
1271 * 1) Get EDID from hw, or use sw default
1272 * 2) Parse into various fb_info structs
1273 * 3) Allocate virtual framebuffer memory to back highest res mode
1275 * Parses EDID into three places used by various parts of fbdev:
1276 * fb_var_screeninfo contains the timing of the monitor's preferred mode
1277 * fb_info.monspecs is full parsed EDID info, including monspecs.modedb
1278 * fb_info.modelist is a linked list of all monitor & VESA modes which work
1280 * If EDID is not readable/valid, then modelist is all VESA modes,
1281 * monspecs is NULL, and fb_var_screeninfo is set to safe VESA mode
1282 * Returns 0 if successful
1284 static int dlfb_setup_modes(struct dlfb_data *dlfb,
1285 struct fb_info *info,
1286 char *default_edid, size_t default_edid_size)
1289 int i, result = 0, tries = 3;
1290 struct device *dev = info->device;
1291 struct fb_videomode *mode;
1292 const struct fb_videomode *default_vmode = NULL;
1295 /* only use mutex if info has been registered */
1296 mutex_lock(&info->lock);
1297 /* parent device is used otherwise */
1301 edid = kmalloc(EDID_LENGTH, GFP_KERNEL);
1307 fb_destroy_modelist(&info->modelist);
1308 memset(&info->monspecs, 0, sizeof(info->monspecs));
1311 * Try to (re)read EDID from hardware first
1312 * EDID data may return, but not parse as valid
1313 * Try again a few times, in case of e.g. analog cable noise
1317 i = dlfb_get_edid(dlfb, edid, EDID_LENGTH);
1319 if (i >= EDID_LENGTH)
1320 fb_edid_to_monspecs(edid, &info->monspecs);
1322 if (info->monspecs.modedb_len > 0) {
1324 dlfb->edid_size = i;
1329 /* If that fails, use a previously returned EDID if available */
1330 if (info->monspecs.modedb_len == 0) {
1331 dev_err(dev, "Unable to get valid EDID from device/display\n");
1334 fb_edid_to_monspecs(dlfb->edid, &info->monspecs);
1335 if (info->monspecs.modedb_len > 0)
1336 dev_err(dev, "Using previously queried EDID\n");
1340 /* If that fails, use the default EDID we were handed */
1341 if (info->monspecs.modedb_len == 0) {
1342 if (default_edid_size >= EDID_LENGTH) {
1343 fb_edid_to_monspecs(default_edid, &info->monspecs);
1344 if (info->monspecs.modedb_len > 0) {
1345 memcpy(edid, default_edid, default_edid_size);
1347 dlfb->edid_size = default_edid_size;
1348 dev_err(dev, "Using default/backup EDID\n");
1353 /* If we've got modes, let's pick a best default mode */
1354 if (info->monspecs.modedb_len > 0) {
1356 for (i = 0; i < info->monspecs.modedb_len; i++) {
1357 mode = &info->monspecs.modedb[i];
1358 if (dlfb_is_valid_mode(mode, dlfb)) {
1359 fb_add_videomode(mode, &info->modelist);
1361 dev_dbg(dev, "Specified mode %dx%d too big\n",
1362 mode->xres, mode->yres);
1364 /* if we've removed top/best mode */
1366 &= ~FB_MISC_1ST_DETAIL;
1370 default_vmode = fb_find_best_display(&info->monspecs,
1374 /* If everything else has failed, fall back to safe default mode */
1375 if (default_vmode == NULL) {
1377 struct fb_videomode fb_vmode = {0};
1380 * Add the standard VESA modes to our modelist
1381 * Since we don't have EDID, there may be modes that
1382 * overspec monitor and/or are incorrect aspect ratio, etc.
1383 * But at least the user has a chance to choose
1385 for (i = 0; i < VESA_MODEDB_SIZE; i++) {
1386 mode = (struct fb_videomode *)&vesa_modes[i];
1387 if (dlfb_is_valid_mode(mode, dlfb))
1388 fb_add_videomode(mode, &info->modelist);
1390 dev_dbg(dev, "VESA mode %dx%d too big\n",
1391 mode->xres, mode->yres);
1395 * default to resolution safe for projectors
1396 * (since they are most common case without EDID)
1398 fb_vmode.xres = 800;
1399 fb_vmode.yres = 600;
1400 fb_vmode.refresh = 60;
1401 default_vmode = fb_find_nearest_mode(&fb_vmode,
1405 /* If we have good mode and no active clients*/
1406 if ((default_vmode != NULL) && (dlfb->fb_count == 0)) {
1408 fb_videomode_to_var(&info->var, default_vmode);
1409 dlfb_var_color_format(&info->var);
1412 * with mode size info, we can now alloc our framebuffer.
1414 memcpy(&info->fix, &dlfb_fix, sizeof(dlfb_fix));
1419 if (edid && (dlfb->edid != edid))
1423 mutex_unlock(&info->lock);
1428 static ssize_t metrics_bytes_rendered_show(struct device *fbdev,
1429 struct device_attribute *a, char *buf) {
1430 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1431 struct dlfb_data *dlfb = fb_info->par;
1432 return sysfs_emit(buf, "%u\n",
1433 atomic_read(&dlfb->bytes_rendered));
1436 static ssize_t metrics_bytes_identical_show(struct device *fbdev,
1437 struct device_attribute *a, char *buf) {
1438 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1439 struct dlfb_data *dlfb = fb_info->par;
1440 return sysfs_emit(buf, "%u\n",
1441 atomic_read(&dlfb->bytes_identical));
1444 static ssize_t metrics_bytes_sent_show(struct device *fbdev,
1445 struct device_attribute *a, char *buf) {
1446 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1447 struct dlfb_data *dlfb = fb_info->par;
1448 return sysfs_emit(buf, "%u\n",
1449 atomic_read(&dlfb->bytes_sent));
1452 static ssize_t metrics_cpu_kcycles_used_show(struct device *fbdev,
1453 struct device_attribute *a, char *buf) {
1454 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1455 struct dlfb_data *dlfb = fb_info->par;
1456 return sysfs_emit(buf, "%u\n",
1457 atomic_read(&dlfb->cpu_kcycles_used));
1460 static ssize_t edid_show(
1462 struct kobject *kobj, struct bin_attribute *a,
1463 char *buf, loff_t off, size_t count) {
1464 struct device *fbdev = kobj_to_dev(kobj);
1465 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1466 struct dlfb_data *dlfb = fb_info->par;
1468 if (dlfb->edid == NULL)
1471 if ((off >= dlfb->edid_size) || (count > dlfb->edid_size))
1474 if (off + count > dlfb->edid_size)
1475 count = dlfb->edid_size - off;
1477 memcpy(buf, dlfb->edid, count);
1482 static ssize_t edid_store(
1484 struct kobject *kobj, struct bin_attribute *a,
1485 char *src, loff_t src_off, size_t src_size) {
1486 struct device *fbdev = kobj_to_dev(kobj);
1487 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1488 struct dlfb_data *dlfb = fb_info->par;
1491 /* We only support write of entire EDID at once, no offset*/
1492 if ((src_size != EDID_LENGTH) || (src_off != 0))
1495 ret = dlfb_setup_modes(dlfb, fb_info, src, src_size);
1499 if (!dlfb->edid || memcmp(src, dlfb->edid, src_size))
1502 ret = dlfb_ops_set_par(fb_info);
1509 static ssize_t metrics_reset_store(struct device *fbdev,
1510 struct device_attribute *attr,
1511 const char *buf, size_t count)
1513 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1514 struct dlfb_data *dlfb = fb_info->par;
1516 atomic_set(&dlfb->bytes_rendered, 0);
1517 atomic_set(&dlfb->bytes_identical, 0);
1518 atomic_set(&dlfb->bytes_sent, 0);
1519 atomic_set(&dlfb->cpu_kcycles_used, 0);
1524 static const struct bin_attribute edid_attr = {
1525 .attr.name = "edid",
1527 .size = EDID_LENGTH,
1532 static const struct device_attribute fb_device_attrs[] = {
1533 __ATTR_RO(metrics_bytes_rendered),
1534 __ATTR_RO(metrics_bytes_identical),
1535 __ATTR_RO(metrics_bytes_sent),
1536 __ATTR_RO(metrics_cpu_kcycles_used),
1537 __ATTR(metrics_reset, S_IWUSR, NULL, metrics_reset_store),
1541 * This is necessary before we can communicate with the display controller.
1543 static int dlfb_select_std_channel(struct dlfb_data *dlfb)
1546 static const u8 set_def_chn[] = {
1547 0x57, 0xCD, 0xDC, 0xA7,
1548 0x1C, 0x88, 0x5E, 0x15,
1549 0x60, 0xFE, 0xC6, 0x97,
1550 0x16, 0x3D, 0x47, 0xF2 };
1552 ret = usb_control_msg_send(dlfb->udev, 0, NR_USB_REQUEST_CHANNEL,
1553 (USB_DIR_OUT | USB_TYPE_VENDOR), 0, 0,
1554 &set_def_chn, sizeof(set_def_chn), USB_CTRL_SET_TIMEOUT,
1560 static int dlfb_parse_vendor_descriptor(struct dlfb_data *dlfb,
1561 struct usb_interface *intf)
1568 buf = kzalloc(MAX_VENDOR_DESCRIPTOR_SIZE, GFP_KERNEL);
1573 total_len = usb_get_descriptor(interface_to_usbdev(intf),
1574 0x5f, /* vendor specific */
1575 0, desc, MAX_VENDOR_DESCRIPTOR_SIZE);
1577 /* if not found, look in configuration descriptor */
1578 if (total_len < 0) {
1579 if (0 == usb_get_extra_descriptor(intf->cur_altsetting,
1581 total_len = (int) desc[0];
1584 if (total_len > 5) {
1585 dev_info(&intf->dev,
1586 "vendor descriptor length: %d data: %11ph\n",
1589 if ((desc[0] != total_len) || /* descriptor length */
1590 (desc[1] != 0x5f) || /* vendor descriptor type */
1591 (desc[2] != 0x01) || /* version (2 bytes) */
1592 (desc[3] != 0x00) ||
1593 (desc[4] != total_len - 2)) /* length after type */
1596 desc_end = desc + total_len;
1597 desc += 5; /* the fixed header we've already parsed */
1599 while (desc < desc_end) {
1604 key |= (u16)*desc++ << 8;
1608 case 0x0200: { /* max_area */
1609 u32 max_area = *desc++;
1610 max_area |= (u32)*desc++ << 8;
1611 max_area |= (u32)*desc++ << 16;
1612 max_area |= (u32)*desc++ << 24;
1613 dev_warn(&intf->dev,
1614 "DL chip limited to %d pixel modes\n",
1616 dlfb->sku_pixel_limit = max_area;
1625 dev_info(&intf->dev, "vendor descriptor not available (%d)\n",
1632 /* allow udlfb to load for now even if firmware unrecognized */
1633 dev_err(&intf->dev, "Unrecognized vendor firmware descriptor\n");
1640 static int dlfb_usb_probe(struct usb_interface *intf,
1641 const struct usb_device_id *id)
1644 const struct device_attribute *attr;
1645 struct dlfb_data *dlfb;
1646 struct fb_info *info;
1648 struct usb_device *usbdev = interface_to_usbdev(intf);
1649 static u8 out_ep[] = {OUT_EP_NUM + USB_DIR_OUT, 0};
1651 /* usb initialization */
1652 dlfb = kzalloc(sizeof(*dlfb), GFP_KERNEL);
1654 dev_err(&intf->dev, "%s: failed to allocate dlfb\n", __func__);
1658 INIT_LIST_HEAD(&dlfb->deferred_free);
1660 dlfb->udev = usb_get_dev(usbdev);
1661 usb_set_intfdata(intf, dlfb);
1663 if (!usb_check_bulk_endpoints(intf, out_ep)) {
1664 dev_err(&intf->dev, "Invalid DisplayLink device!\n");
1669 dev_dbg(&intf->dev, "console enable=%d\n", console);
1670 dev_dbg(&intf->dev, "fb_defio enable=%d\n", fb_defio);
1671 dev_dbg(&intf->dev, "shadow enable=%d\n", shadow);
1673 dlfb->sku_pixel_limit = 2048 * 1152; /* default to maximum */
1675 if (!dlfb_parse_vendor_descriptor(dlfb, intf)) {
1677 "firmware not recognized, incompatible device?\n");
1683 dev_warn(&intf->dev,
1684 "DL chip limit of %d overridden to %d\n",
1685 dlfb->sku_pixel_limit, pixel_limit);
1686 dlfb->sku_pixel_limit = pixel_limit;
1690 /* allocates framebuffer driver structure, not framebuffer memory */
1691 info = framebuffer_alloc(0, &dlfb->udev->dev);
1699 info->pseudo_palette = dlfb->pseudo_palette;
1700 dlfb->ops = dlfb_ops;
1701 info->fbops = &dlfb->ops;
1703 mutex_init(&dlfb->render_mutex);
1704 dlfb_init_damage(dlfb);
1705 spin_lock_init(&dlfb->damage_lock);
1706 INIT_WORK(&dlfb->damage_work, dlfb_damage_work);
1708 INIT_LIST_HEAD(&info->modelist);
1710 if (!dlfb_alloc_urb_list(dlfb, WRITES_IN_FLIGHT, MAX_TRANSFER)) {
1712 dev_err(&intf->dev, "unable to allocate urb list\n");
1716 /* We don't register a new USB class. Our client interface is dlfbev */
1718 retval = fb_alloc_cmap(&info->cmap, 256, 0);
1720 dev_err(info->device, "cmap allocation failed: %d\n", retval);
1724 retval = dlfb_setup_modes(dlfb, info, NULL, 0);
1726 dev_err(info->device,
1727 "unable to find common mode for display and adapter\n");
1731 /* ready to begin using device */
1733 atomic_set(&dlfb->usb_active, 1);
1734 dlfb_select_std_channel(dlfb);
1736 dlfb_ops_check_var(&info->var, info);
1737 retval = dlfb_ops_set_par(info);
1741 retval = register_framebuffer(info);
1743 dev_err(info->device, "unable to register framebuffer: %d\n",
1748 for (i = 0; i < ARRAY_SIZE(fb_device_attrs); i++) {
1749 attr = &fb_device_attrs[i];
1750 retval = device_create_file(info->dev, attr);
1752 dev_warn(info->device,
1753 "failed to create '%s' attribute: %d\n",
1754 attr->attr.name, retval);
1757 retval = device_create_bin_file(info->dev, &edid_attr);
1759 dev_warn(info->device, "failed to create '%s' attribute: %d\n",
1760 edid_attr.attr.name, retval);
1762 dev_info(info->device,
1763 "%s is DisplayLink USB device (%dx%d, %dK framebuffer memory)\n",
1764 dev_name(info->dev), info->var.xres, info->var.yres,
1765 ((dlfb->backing_buffer) ?
1766 info->fix.smem_len * 2 : info->fix.smem_len) >> 10);
1771 dlfb_ops_destroy(dlfb->info);
1773 usb_put_dev(dlfb->udev);
1779 static void dlfb_usb_disconnect(struct usb_interface *intf)
1781 struct dlfb_data *dlfb;
1782 struct fb_info *info;
1785 dlfb = usb_get_intfdata(intf);
1788 dev_dbg(&intf->dev, "USB disconnect starting\n");
1790 /* we virtualize until all fb clients release. Then we free */
1791 dlfb->virtualized = true;
1793 /* When non-active we'll update virtual framebuffer, but no new urbs */
1794 atomic_set(&dlfb->usb_active, 0);
1796 /* this function will wait for all in-flight urbs to complete */
1797 dlfb_free_urb_list(dlfb);
1799 /* remove udlfb's sysfs interfaces */
1800 for (i = 0; i < ARRAY_SIZE(fb_device_attrs); i++)
1801 device_remove_file(info->dev, &fb_device_attrs[i]);
1802 device_remove_bin_file(info->dev, &edid_attr);
1804 unregister_framebuffer(info);
1807 static struct usb_driver dlfb_driver = {
1809 .probe = dlfb_usb_probe,
1810 .disconnect = dlfb_usb_disconnect,
1811 .id_table = id_table,
1814 module_usb_driver(dlfb_driver);
1816 static void dlfb_urb_completion(struct urb *urb)
1818 struct urb_node *unode = urb->context;
1819 struct dlfb_data *dlfb = unode->dlfb;
1820 unsigned long flags;
1822 switch (urb->status) {
1829 /* sync/async unlink faults aren't errors */
1832 dev_err(&dlfb->udev->dev,
1833 "%s - nonzero write bulk status received: %d\n",
1834 __func__, urb->status);
1835 atomic_set(&dlfb->lost_pixels, 1);
1839 urb->transfer_buffer_length = dlfb->urbs.size; /* reset to actual */
1841 spin_lock_irqsave(&dlfb->urbs.lock, flags);
1842 list_add_tail(&unode->entry, &dlfb->urbs.list);
1843 dlfb->urbs.available++;
1844 spin_unlock_irqrestore(&dlfb->urbs.lock, flags);
1846 up(&dlfb->urbs.limit_sem);
1849 static void dlfb_free_urb_list(struct dlfb_data *dlfb)
1851 int count = dlfb->urbs.count;
1852 struct list_head *node;
1853 struct urb_node *unode;
1856 /* keep waiting and freeing, until we've got 'em all */
1858 down(&dlfb->urbs.limit_sem);
1860 spin_lock_irq(&dlfb->urbs.lock);
1862 node = dlfb->urbs.list.next; /* have reserved one with sem */
1863 list_del_init(node);
1865 spin_unlock_irq(&dlfb->urbs.lock);
1867 unode = list_entry(node, struct urb_node, entry);
1870 /* Free each separately allocated piece */
1871 usb_free_coherent(urb->dev, dlfb->urbs.size,
1872 urb->transfer_buffer, urb->transfer_dma);
1877 dlfb->urbs.count = 0;
1880 static int dlfb_alloc_urb_list(struct dlfb_data *dlfb, int count, size_t size)
1883 struct urb_node *unode;
1885 size_t wanted_size = count * size;
1887 spin_lock_init(&dlfb->urbs.lock);
1890 dlfb->urbs.size = size;
1891 INIT_LIST_HEAD(&dlfb->urbs.list);
1893 sema_init(&dlfb->urbs.limit_sem, 0);
1894 dlfb->urbs.count = 0;
1895 dlfb->urbs.available = 0;
1897 while (dlfb->urbs.count * size < wanted_size) {
1898 unode = kzalloc(sizeof(*unode), GFP_KERNEL);
1903 urb = usb_alloc_urb(0, GFP_KERNEL);
1910 buf = usb_alloc_coherent(dlfb->udev, size, GFP_KERNEL,
1911 &urb->transfer_dma);
1915 if (size > PAGE_SIZE) {
1917 dlfb_free_urb_list(dlfb);
1923 /* urb->transfer_buffer_length set to actual before submit */
1924 usb_fill_bulk_urb(urb, dlfb->udev,
1925 usb_sndbulkpipe(dlfb->udev, OUT_EP_NUM),
1926 buf, size, dlfb_urb_completion, unode);
1927 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1929 list_add_tail(&unode->entry, &dlfb->urbs.list);
1931 up(&dlfb->urbs.limit_sem);
1933 dlfb->urbs.available++;
1936 return dlfb->urbs.count;
1939 static struct urb *dlfb_get_urb(struct dlfb_data *dlfb)
1942 struct list_head *entry;
1943 struct urb_node *unode;
1945 /* Wait for an in-flight buffer to complete and get re-queued */
1946 ret = down_timeout(&dlfb->urbs.limit_sem, GET_URB_TIMEOUT);
1948 atomic_set(&dlfb->lost_pixels, 1);
1949 dev_warn(&dlfb->udev->dev,
1950 "wait for urb interrupted: %d available: %d\n",
1951 ret, dlfb->urbs.available);
1955 spin_lock_irq(&dlfb->urbs.lock);
1957 BUG_ON(list_empty(&dlfb->urbs.list)); /* reserved one with limit_sem */
1958 entry = dlfb->urbs.list.next;
1959 list_del_init(entry);
1960 dlfb->urbs.available--;
1962 spin_unlock_irq(&dlfb->urbs.lock);
1964 unode = list_entry(entry, struct urb_node, entry);
1968 static int dlfb_submit_urb(struct dlfb_data *dlfb, struct urb *urb, size_t len)
1972 BUG_ON(len > dlfb->urbs.size);
1974 urb->transfer_buffer_length = len; /* set to actual payload len */
1975 ret = usb_submit_urb(urb, GFP_KERNEL);
1977 dlfb_urb_completion(urb); /* because no one else will */
1978 atomic_set(&dlfb->lost_pixels, 1);
1979 dev_err(&dlfb->udev->dev, "submit urb error: %d\n", ret);
1984 module_param(console, bool, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
1985 MODULE_PARM_DESC(console, "Allow fbcon to open framebuffer");
1987 module_param(fb_defio, bool, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
1988 MODULE_PARM_DESC(fb_defio, "Page fault detection of mmap writes");
1990 module_param(shadow, bool, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
1991 MODULE_PARM_DESC(shadow, "Shadow vid mem. Disable to save mem but lose perf");
1993 module_param(pixel_limit, int, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
1994 MODULE_PARM_DESC(pixel_limit, "Force limit on max mode (in x*y pixels)");
1996 MODULE_AUTHOR("Roberto De Ioris <roberto@unbit.it>, "
1997 "Jaya Kumar <jayakumar.lkml@gmail.com>, "
1998 "Bernie Thompson <bernie@plugable.com>");
1999 MODULE_DESCRIPTION("DisplayLink kernel framebuffer driver");
2000 MODULE_LICENSE("GPL");