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
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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21 #include <linux/module.h>
22 #include <linux/kernel.h>
23 #include <linux/init.h>
24 #include <linux/usb.h>
25 #include <linux/uaccess.h>
28 #include <linux/vmalloc.h>
29 #include <linux/slab.h>
30 #include <linux/prefetch.h>
31 #include <linux/delay.h>
32 #include <video/udlfb.h>
35 static struct fb_fix_screeninfo dlfb_fix
= {
37 .type
= FB_TYPE_PACKED_PIXELS
,
38 .visual
= FB_VISUAL_TRUECOLOR
,
42 .accel
= FB_ACCEL_NONE
,
45 static const u32 udlfb_info_flags
= FBINFO_DEFAULT
| FBINFO_READS_FAST
|
47 FBINFO_HWACCEL_IMAGEBLIT
| FBINFO_HWACCEL_FILLRECT
|
48 FBINFO_HWACCEL_COPYAREA
| FBINFO_MISC_ALWAYS_SETPAR
;
51 * There are many DisplayLink-based graphics products, all with unique PIDs.
52 * So we match on DisplayLink's VID + Vendor-Defined Interface Class (0xff)
53 * We also require a match on SubClass (0x00) and Protocol (0x00),
54 * which is compatible with all known USB 2.0 era graphics chips and firmware,
55 * but allows DisplayLink to increment those for any future incompatible chips
57 static struct usb_device_id id_table
[] = {
59 .bInterfaceClass
= 0xff,
60 .bInterfaceSubClass
= 0x00,
61 .bInterfaceProtocol
= 0x00,
62 .match_flags
= USB_DEVICE_ID_MATCH_VENDOR
|
63 USB_DEVICE_ID_MATCH_INT_CLASS
|
64 USB_DEVICE_ID_MATCH_INT_SUBCLASS
|
65 USB_DEVICE_ID_MATCH_INT_PROTOCOL
,
69 MODULE_DEVICE_TABLE(usb
, id_table
);
72 static bool console
= 1; /* Allow fbcon to open framebuffer */
73 static bool fb_defio
= 1; /* Detect mmap writes using page faults */
74 static bool shadow
= 1; /* Optionally disable shadow framebuffer */
75 static int pixel_limit
; /* Optionally force a pixel resolution limit */
77 /* dlfb keeps a list of urbs for efficient bulk transfers */
78 static void dlfb_urb_completion(struct urb
*urb
);
79 static struct urb
*dlfb_get_urb(struct dlfb_data
*dev
);
80 static int dlfb_submit_urb(struct dlfb_data
*dev
, struct urb
* urb
, size_t len
);
81 static int dlfb_alloc_urb_list(struct dlfb_data
*dev
, int count
, size_t size
);
82 static void dlfb_free_urb_list(struct dlfb_data
*dev
);
85 * All DisplayLink bulk operations start with 0xAF, followed by specific code
86 * All operations are written to buffers which then later get sent to device
88 static char *dlfb_set_register(char *buf
, u8 reg
, u8 val
)
97 static char *dlfb_vidreg_lock(char *buf
)
99 return dlfb_set_register(buf
, 0xFF, 0x00);
102 static char *dlfb_vidreg_unlock(char *buf
)
104 return dlfb_set_register(buf
, 0xFF, 0xFF);
108 * Map FB_BLANK_* to DisplayLink register
110 * ----- -----------------------------
111 * 0x00 FB_BLANK_UNBLANK (0)
113 * 0x03 FB_BLANK_VSYNC_SUSPEND (2)
114 * 0x05 FB_BLANK_HSYNC_SUSPEND (3)
115 * 0x07 FB_BLANK_POWERDOWN (4) Note: requires modeset to come back
117 static char *dlfb_blanking(char *buf
, int fb_blank
)
122 case FB_BLANK_POWERDOWN
:
125 case FB_BLANK_HSYNC_SUSPEND
:
128 case FB_BLANK_VSYNC_SUSPEND
:
131 case FB_BLANK_NORMAL
:
138 buf
= dlfb_set_register(buf
, 0x1F, reg
);
143 static char *dlfb_set_color_depth(char *buf
, u8 selection
)
145 return dlfb_set_register(buf
, 0x00, selection
);
148 static char *dlfb_set_base16bpp(char *wrptr
, u32 base
)
150 /* the base pointer is 16 bits wide, 0x20 is hi byte. */
151 wrptr
= dlfb_set_register(wrptr
, 0x20, base
>> 16);
152 wrptr
= dlfb_set_register(wrptr
, 0x21, base
>> 8);
153 return dlfb_set_register(wrptr
, 0x22, base
);
157 * DisplayLink HW has separate 16bpp and 8bpp framebuffers.
158 * In 24bpp modes, the low 323 RGB bits go in the 8bpp framebuffer
160 static char *dlfb_set_base8bpp(char *wrptr
, u32 base
)
162 wrptr
= dlfb_set_register(wrptr
, 0x26, base
>> 16);
163 wrptr
= dlfb_set_register(wrptr
, 0x27, base
>> 8);
164 return dlfb_set_register(wrptr
, 0x28, base
);
167 static char *dlfb_set_register_16(char *wrptr
, u8 reg
, u16 value
)
169 wrptr
= dlfb_set_register(wrptr
, reg
, value
>> 8);
170 return dlfb_set_register(wrptr
, reg
+1, value
);
174 * This is kind of weird because the controller takes some
175 * register values in a different byte order than other registers.
177 static char *dlfb_set_register_16be(char *wrptr
, u8 reg
, u16 value
)
179 wrptr
= dlfb_set_register(wrptr
, reg
, value
);
180 return dlfb_set_register(wrptr
, reg
+1, value
>> 8);
184 * LFSR is linear feedback shift register. The reason we have this is
185 * because the display controller needs to minimize the clock depth of
186 * various counters used in the display path. So this code reverses the
187 * provided value into the lfsr16 value by counting backwards to get
188 * the value that needs to be set in the hardware comparator to get the
189 * same actual count. This makes sense once you read above a couple of
190 * times and think about it from a hardware perspective.
192 static u16
dlfb_lfsr16(u16 actual_count
)
194 u32 lv
= 0xFFFF; /* This is the lfsr value that the hw starts with */
196 while (actual_count
--) {
198 (((lv
>> 15) ^ (lv
>> 4) ^ (lv
>> 2) ^ (lv
>> 1)) & 1))
206 * This does LFSR conversion on the value that is to be written.
207 * See LFSR explanation above for more detail.
209 static char *dlfb_set_register_lfsr16(char *wrptr
, u8 reg
, u16 value
)
211 return dlfb_set_register_16(wrptr
, reg
, dlfb_lfsr16(value
));
215 * This takes a standard fbdev screeninfo struct and all of its monitor mode
216 * details and converts them into the DisplayLink equivalent register commands.
218 static char *dlfb_set_vid_cmds(char *wrptr
, struct fb_var_screeninfo
*var
)
224 /* x display start */
225 xds
= var
->left_margin
+ var
->hsync_len
;
226 wrptr
= dlfb_set_register_lfsr16(wrptr
, 0x01, xds
);
228 xde
= xds
+ var
->xres
;
229 wrptr
= dlfb_set_register_lfsr16(wrptr
, 0x03, xde
);
231 /* y display start */
232 yds
= var
->upper_margin
+ var
->vsync_len
;
233 wrptr
= dlfb_set_register_lfsr16(wrptr
, 0x05, yds
);
235 yde
= yds
+ var
->yres
;
236 wrptr
= dlfb_set_register_lfsr16(wrptr
, 0x07, yde
);
238 /* x end count is active + blanking - 1 */
239 wrptr
= dlfb_set_register_lfsr16(wrptr
, 0x09,
240 xde
+ var
->right_margin
- 1);
242 /* libdlo hardcodes hsync start to 1 */
243 wrptr
= dlfb_set_register_lfsr16(wrptr
, 0x0B, 1);
245 /* hsync end is width of sync pulse + 1 */
246 wrptr
= dlfb_set_register_lfsr16(wrptr
, 0x0D, var
->hsync_len
+ 1);
248 /* hpixels is active pixels */
249 wrptr
= dlfb_set_register_16(wrptr
, 0x0F, var
->xres
);
251 /* yendcount is vertical active + vertical blanking */
252 yec
= var
->yres
+ var
->upper_margin
+ var
->lower_margin
+
254 wrptr
= dlfb_set_register_lfsr16(wrptr
, 0x11, yec
);
256 /* libdlo hardcodes vsync start to 0 */
257 wrptr
= dlfb_set_register_lfsr16(wrptr
, 0x13, 0);
259 /* vsync end is width of vsync pulse */
260 wrptr
= dlfb_set_register_lfsr16(wrptr
, 0x15, var
->vsync_len
);
262 /* vpixels is active pixels */
263 wrptr
= dlfb_set_register_16(wrptr
, 0x17, var
->yres
);
265 /* convert picoseconds to 5kHz multiple for pclk5k = x * 1E12/5k */
266 wrptr
= dlfb_set_register_16be(wrptr
, 0x1B,
267 200*1000*1000/var
->pixclock
);
273 * This takes a standard fbdev screeninfo struct that was fetched or prepared
274 * and then generates the appropriate command sequence that then drives the
275 * display controller.
277 static int dlfb_set_video_mode(struct dlfb_data
*dev
,
278 struct fb_var_screeninfo
*var
)
286 if (!atomic_read(&dev
->usb_active
))
289 urb
= dlfb_get_urb(dev
);
293 buf
= (char *) urb
->transfer_buffer
;
296 * This first section has to do with setting the base address on the
297 * controller * associated with the display. There are 2 base
298 * pointers, currently, we only * use the 16 bpp segment.
300 wrptr
= dlfb_vidreg_lock(buf
);
301 wrptr
= dlfb_set_color_depth(wrptr
, 0x00);
302 /* set base for 16bpp segment to 0 */
303 wrptr
= dlfb_set_base16bpp(wrptr
, 0);
304 /* set base for 8bpp segment to end of fb */
305 wrptr
= dlfb_set_base8bpp(wrptr
, dev
->info
->fix
.smem_len
);
307 wrptr
= dlfb_set_vid_cmds(wrptr
, var
);
308 wrptr
= dlfb_blanking(wrptr
, FB_BLANK_UNBLANK
);
309 wrptr
= dlfb_vidreg_unlock(wrptr
);
311 writesize
= wrptr
- buf
;
313 retval
= dlfb_submit_urb(dev
, urb
, writesize
);
315 dev
->blank_mode
= FB_BLANK_UNBLANK
;
320 static int dlfb_ops_mmap(struct fb_info
*info
, struct vm_area_struct
*vma
)
322 unsigned long start
= vma
->vm_start
;
323 unsigned long size
= vma
->vm_end
- vma
->vm_start
;
324 unsigned long offset
= vma
->vm_pgoff
<< PAGE_SHIFT
;
325 unsigned long page
, pos
;
327 if (vma
->vm_pgoff
> (~0UL >> PAGE_SHIFT
))
329 if (size
> info
->fix
.smem_len
)
331 if (offset
> info
->fix
.smem_len
- size
)
334 pos
= (unsigned long)info
->fix
.smem_start
+ offset
;
336 pr_notice("mmap() framebuffer addr:%lu size:%lu\n",
340 page
= vmalloc_to_pfn((void *)pos
);
341 if (remap_pfn_range(vma
, start
, page
, PAGE_SIZE
, PAGE_SHARED
))
346 if (size
> PAGE_SIZE
)
356 * Trims identical data from front and back of line
357 * Sets new front buffer address and width
358 * And returns byte count of identical pixels
359 * Assumes CPU natural alignment (unsigned long)
360 * for back and front buffer ptrs and width
362 static int dlfb_trim_hline(const u8
*bback
, const u8
**bfront
, int *width_bytes
)
365 const unsigned long *back
= (const unsigned long *) bback
;
366 const unsigned long *front
= (const unsigned long *) *bfront
;
367 const int width
= *width_bytes
/ sizeof(unsigned long);
368 int identical
= width
;
372 prefetch((void *) front
);
373 prefetch((void *) back
);
375 for (j
= 0; j
< width
; j
++) {
376 if (back
[j
] != front
[j
]) {
382 for (k
= width
- 1; k
> j
; k
--) {
383 if (back
[k
] != front
[k
]) {
389 identical
= start
+ (width
- end
);
390 *bfront
= (u8
*) &front
[start
];
391 *width_bytes
= (end
- start
) * sizeof(unsigned long);
393 return identical
* sizeof(unsigned long);
397 * Render a command stream for an encoded horizontal line segment of pixels.
399 * A command buffer holds several commands.
400 * It always begins with a fresh command header
401 * (the protocol doesn't require this, but we enforce it to allow
402 * multiple buffers to be potentially encoded and sent in parallel).
403 * A single command encodes one contiguous horizontal line of pixels
405 * The function relies on the client to do all allocation, so that
406 * rendering can be done directly to output buffers (e.g. USB URBs).
407 * The function fills the supplied command buffer, providing information
408 * on where it left off, so the client may call in again with additional
409 * buffers if the line will take several buffers to complete.
411 * A single command can transmit a maximum of 256 pixels,
412 * regardless of the compression ratio (protocol design limit).
413 * To the hardware, 0 for a size byte means 256
415 * Rather than 256 pixel commands which are either rl or raw encoded,
416 * the rlx command simply assumes alternating raw and rl spans within one cmd.
417 * This has a slightly larger header overhead, but produces more even results.
418 * It also processes all data (read and write) in a single pass.
419 * Performance benchmarks of common cases show it having just slightly better
420 * compression than 256 pixel raw or rle commands, with similar CPU consumpion.
421 * But for very rl friendly data, will compress not quite as well.
423 static void dlfb_compress_hline(
424 const uint16_t **pixel_start_ptr
,
425 const uint16_t *const pixel_end
,
426 uint32_t *device_address_ptr
,
427 uint8_t **command_buffer_ptr
,
428 const uint8_t *const cmd_buffer_end
)
430 const uint16_t *pixel
= *pixel_start_ptr
;
431 uint32_t dev_addr
= *device_address_ptr
;
432 uint8_t *cmd
= *command_buffer_ptr
;
435 while ((pixel_end
> pixel
) &&
436 (cmd_buffer_end
- MIN_RLX_CMD_BYTES
> cmd
)) {
437 uint8_t *raw_pixels_count_byte
= NULL
;
438 uint8_t *cmd_pixels_count_byte
= NULL
;
439 const uint16_t *raw_pixel_start
= NULL
;
440 const uint16_t *cmd_pixel_start
, *cmd_pixel_end
= NULL
;
442 prefetchw((void *) cmd
); /* pull in one cache line at least */
446 *cmd
++ = (uint8_t) ((dev_addr
>> 16) & 0xFF);
447 *cmd
++ = (uint8_t) ((dev_addr
>> 8) & 0xFF);
448 *cmd
++ = (uint8_t) ((dev_addr
) & 0xFF);
450 cmd_pixels_count_byte
= cmd
++; /* we'll know this later */
451 cmd_pixel_start
= pixel
;
453 raw_pixels_count_byte
= cmd
++; /* we'll know this later */
454 raw_pixel_start
= pixel
;
456 cmd_pixel_end
= pixel
+ min(MAX_CMD_PIXELS
+ 1,
457 min((int)(pixel_end
- pixel
),
458 (int)(cmd_buffer_end
- cmd
) / bpp
));
460 prefetch_range((void *) pixel
, (cmd_pixel_end
- pixel
) * bpp
);
462 while (pixel
< cmd_pixel_end
) {
463 const uint16_t * const repeating_pixel
= pixel
;
465 *(uint16_t *)cmd
= cpu_to_be16p(pixel
);
469 if (unlikely((pixel
< cmd_pixel_end
) &&
470 (*pixel
== *repeating_pixel
))) {
471 /* go back and fill in raw pixel count */
472 *raw_pixels_count_byte
= ((repeating_pixel
-
473 raw_pixel_start
) + 1) & 0xFF;
475 while ((pixel
< cmd_pixel_end
)
476 && (*pixel
== *repeating_pixel
)) {
480 /* immediately after raw data is repeat byte */
481 *cmd
++ = ((pixel
- repeating_pixel
) - 1) & 0xFF;
483 /* Then start another raw pixel span */
484 raw_pixel_start
= pixel
;
485 raw_pixels_count_byte
= cmd
++;
489 if (pixel
> raw_pixel_start
) {
490 /* finalize last RAW span */
491 *raw_pixels_count_byte
= (pixel
-raw_pixel_start
) & 0xFF;
494 *cmd_pixels_count_byte
= (pixel
- cmd_pixel_start
) & 0xFF;
495 dev_addr
+= (pixel
- cmd_pixel_start
) * bpp
;
498 if (cmd_buffer_end
<= MIN_RLX_CMD_BYTES
+ cmd
) {
499 /* Fill leftover bytes with no-ops */
500 if (cmd_buffer_end
> cmd
)
501 memset(cmd
, 0xAF, cmd_buffer_end
- cmd
);
502 cmd
= (uint8_t *) cmd_buffer_end
;
505 *command_buffer_ptr
= cmd
;
506 *pixel_start_ptr
= pixel
;
507 *device_address_ptr
= dev_addr
;
513 * There are 3 copies of every pixel: The front buffer that the fbdev
514 * client renders to, the actual framebuffer across the USB bus in hardware
515 * (that we can only write to, slowly, and can never read), and (optionally)
516 * our shadow copy that tracks what's been sent to that hardware buffer.
518 static int dlfb_render_hline(struct dlfb_data
*dev
, struct urb
**urb_ptr
,
519 const char *front
, char **urb_buf_ptr
,
520 u32 byte_offset
, u32 byte_width
,
521 int *ident_ptr
, int *sent_ptr
)
523 const u8
*line_start
, *line_end
, *next_pixel
;
524 u32 dev_addr
= dev
->base16
+ byte_offset
;
525 struct urb
*urb
= *urb_ptr
;
526 u8
*cmd
= *urb_buf_ptr
;
527 u8
*cmd_end
= (u8
*) urb
->transfer_buffer
+ urb
->transfer_buffer_length
;
529 line_start
= (u8
*) (front
+ byte_offset
);
530 next_pixel
= line_start
;
531 line_end
= next_pixel
+ byte_width
;
533 if (dev
->backing_buffer
) {
535 const u8
*back_start
= (u8
*) (dev
->backing_buffer
538 *ident_ptr
+= dlfb_trim_hline(back_start
, &next_pixel
,
541 offset
= next_pixel
- line_start
;
542 line_end
= next_pixel
+ byte_width
;
544 back_start
+= offset
;
545 line_start
+= offset
;
547 memcpy((char *)back_start
, (char *) line_start
,
551 while (next_pixel
< line_end
) {
553 dlfb_compress_hline((const uint16_t **) &next_pixel
,
554 (const uint16_t *) line_end
, &dev_addr
,
555 (u8
**) &cmd
, (u8
*) cmd_end
);
557 if (cmd
>= cmd_end
) {
558 int len
= cmd
- (u8
*) urb
->transfer_buffer
;
559 if (dlfb_submit_urb(dev
, urb
, len
))
560 return 1; /* lost pixels is set */
562 urb
= dlfb_get_urb(dev
);
564 return 1; /* lost_pixels is set */
566 cmd
= urb
->transfer_buffer
;
567 cmd_end
= &cmd
[urb
->transfer_buffer_length
];
576 static int dlfb_handle_damage(struct dlfb_data
*dev
, int x
, int y
,
577 int width
, int height
, char *data
)
581 cycles_t start_cycles
, end_cycles
;
583 int bytes_identical
= 0;
587 start_cycles
= get_cycles();
589 aligned_x
= DL_ALIGN_DOWN(x
, sizeof(unsigned long));
590 width
= DL_ALIGN_UP(width
+ (x
-aligned_x
), sizeof(unsigned long));
594 (x
+ width
> dev
->info
->var
.xres
) ||
595 (y
+ height
> dev
->info
->var
.yres
))
598 if (!atomic_read(&dev
->usb_active
))
601 urb
= dlfb_get_urb(dev
);
604 cmd
= urb
->transfer_buffer
;
606 for (i
= y
; i
< y
+ height
; i
++) {
607 const int line_offset
= dev
->info
->fix
.line_length
* i
;
608 const int byte_offset
= line_offset
+ (x
* BPP
);
610 if (dlfb_render_hline(dev
, &urb
,
611 (char *) dev
->info
->fix
.smem_start
,
612 &cmd
, byte_offset
, width
* BPP
,
613 &bytes_identical
, &bytes_sent
))
617 if (cmd
> (char *) urb
->transfer_buffer
) {
618 /* Send partial buffer remaining before exiting */
619 int len
= cmd
- (char *) urb
->transfer_buffer
;
620 ret
= dlfb_submit_urb(dev
, urb
, len
);
623 dlfb_urb_completion(urb
);
626 atomic_add(bytes_sent
, &dev
->bytes_sent
);
627 atomic_add(bytes_identical
, &dev
->bytes_identical
);
628 atomic_add(width
*height
*2, &dev
->bytes_rendered
);
629 end_cycles
= get_cycles();
630 atomic_add(((unsigned int) ((end_cycles
- start_cycles
)
631 >> 10)), /* Kcycles */
632 &dev
->cpu_kcycles_used
);
638 * Path triggered by usermode clients who write to filesystem
639 * e.g. cat filename > /dev/fb1
640 * Not used by X Windows or text-mode console. But useful for testing.
641 * Slow because of extra copy and we must assume all pixels dirty.
643 static ssize_t
dlfb_ops_write(struct fb_info
*info
, const char __user
*buf
,
644 size_t count
, loff_t
*ppos
)
647 struct dlfb_data
*dev
= info
->par
;
648 u32 offset
= (u32
) *ppos
;
650 result
= fb_sys_write(info
, buf
, count
, ppos
);
653 int start
= max((int)(offset
/ info
->fix
.line_length
), 0);
654 int lines
= min((u32
)((result
/ info
->fix
.line_length
) + 1),
655 (u32
)info
->var
.yres
);
657 dlfb_handle_damage(dev
, 0, start
, info
->var
.xres
,
658 lines
, info
->screen_base
);
664 /* hardware has native COPY command (see libdlo), but not worth it for fbcon */
665 static void dlfb_ops_copyarea(struct fb_info
*info
,
666 const struct fb_copyarea
*area
)
669 struct dlfb_data
*dev
= info
->par
;
671 sys_copyarea(info
, area
);
673 dlfb_handle_damage(dev
, area
->dx
, area
->dy
,
674 area
->width
, area
->height
, info
->screen_base
);
677 static void dlfb_ops_imageblit(struct fb_info
*info
,
678 const struct fb_image
*image
)
680 struct dlfb_data
*dev
= info
->par
;
682 sys_imageblit(info
, image
);
684 dlfb_handle_damage(dev
, image
->dx
, image
->dy
,
685 image
->width
, image
->height
, info
->screen_base
);
688 static void dlfb_ops_fillrect(struct fb_info
*info
,
689 const struct fb_fillrect
*rect
)
691 struct dlfb_data
*dev
= info
->par
;
693 sys_fillrect(info
, rect
);
695 dlfb_handle_damage(dev
, rect
->dx
, rect
->dy
, rect
->width
,
696 rect
->height
, info
->screen_base
);
700 * NOTE: fb_defio.c is holding info->fbdefio.mutex
701 * Touching ANY framebuffer memory that triggers a page fault
702 * in fb_defio will cause a deadlock, when it also tries to
703 * grab the same mutex.
705 static void dlfb_dpy_deferred_io(struct fb_info
*info
,
706 struct list_head
*pagelist
)
709 struct fb_deferred_io
*fbdefio
= info
->fbdefio
;
710 struct dlfb_data
*dev
= info
->par
;
713 cycles_t start_cycles
, end_cycles
;
715 int bytes_identical
= 0;
716 int bytes_rendered
= 0;
721 if (!atomic_read(&dev
->usb_active
))
724 start_cycles
= get_cycles();
726 urb
= dlfb_get_urb(dev
);
730 cmd
= urb
->transfer_buffer
;
732 /* walk the written page list and render each to device */
733 list_for_each_entry(cur
, &fbdefio
->pagelist
, lru
) {
735 if (dlfb_render_hline(dev
, &urb
, (char *) info
->fix
.smem_start
,
736 &cmd
, cur
->index
<< PAGE_SHIFT
,
737 PAGE_SIZE
, &bytes_identical
, &bytes_sent
))
739 bytes_rendered
+= PAGE_SIZE
;
742 if (cmd
> (char *) urb
->transfer_buffer
) {
743 /* Send partial buffer remaining before exiting */
744 int len
= cmd
- (char *) urb
->transfer_buffer
;
745 dlfb_submit_urb(dev
, urb
, len
);
748 dlfb_urb_completion(urb
);
751 atomic_add(bytes_sent
, &dev
->bytes_sent
);
752 atomic_add(bytes_identical
, &dev
->bytes_identical
);
753 atomic_add(bytes_rendered
, &dev
->bytes_rendered
);
754 end_cycles
= get_cycles();
755 atomic_add(((unsigned int) ((end_cycles
- start_cycles
)
756 >> 10)), /* Kcycles */
757 &dev
->cpu_kcycles_used
);
760 static int dlfb_get_edid(struct dlfb_data
*dev
, char *edid
, int len
)
766 rbuf
= kmalloc(2, GFP_KERNEL
);
770 for (i
= 0; i
< len
; i
++) {
771 ret
= usb_control_msg(dev
->udev
,
772 usb_rcvctrlpipe(dev
->udev
, 0), (0x02),
773 (0x80 | (0x02 << 5)), i
<< 8, 0xA1, rbuf
, 2,
776 pr_err("Read EDID byte %d failed err %x\n", i
, ret
);
788 static int dlfb_ops_ioctl(struct fb_info
*info
, unsigned int cmd
,
792 struct dlfb_data
*dev
= info
->par
;
794 if (!atomic_read(&dev
->usb_active
))
797 /* TODO: Update X server to get this from sysfs instead */
798 if (cmd
== DLFB_IOCTL_RETURN_EDID
) {
799 void __user
*edid
= (void __user
*)arg
;
800 if (copy_to_user(edid
, dev
->edid
, dev
->edid_size
))
805 /* TODO: Help propose a standard fb.h ioctl to report mmap damage */
806 if (cmd
== DLFB_IOCTL_REPORT_DAMAGE
) {
809 if (copy_from_user(&area
, (void __user
*)arg
,
810 sizeof(struct dloarea
)))
814 * If we have a damage-aware client, turn fb_defio "off"
815 * To avoid perf imact of unnecessary page fault handling.
816 * Done by resetting the delay for this fb_info to a very
817 * long period. Pages will become writable and stay that way.
818 * Reset to normal value when all clients have closed this fb.
821 info
->fbdefio
->delay
= DL_DEFIO_WRITE_DISABLE
;
826 if (area
.x
> info
->var
.xres
)
827 area
.x
= info
->var
.xres
;
832 if (area
.y
> info
->var
.yres
)
833 area
.y
= info
->var
.yres
;
835 dlfb_handle_damage(dev
, area
.x
, area
.y
, area
.w
, area
.h
,
842 /* taken from vesafb */
844 dlfb_ops_setcolreg(unsigned regno
, unsigned red
, unsigned green
,
845 unsigned blue
, unsigned transp
, struct fb_info
*info
)
849 if (regno
>= info
->cmap
.len
)
853 if (info
->var
.red
.offset
== 10) {
855 ((u32
*) (info
->pseudo_palette
))[regno
] =
856 ((red
& 0xf800) >> 1) |
857 ((green
& 0xf800) >> 6) | ((blue
& 0xf800) >> 11);
860 ((u32
*) (info
->pseudo_palette
))[regno
] =
862 ((green
& 0xfc00) >> 5) | ((blue
& 0xf800) >> 11);
870 * It's common for several clients to have framebuffer open simultaneously.
871 * e.g. both fbcon and X. Makes things interesting.
872 * Assumes caller is holding info->lock (for open and release at least)
874 static int dlfb_ops_open(struct fb_info
*info
, int user
)
876 struct dlfb_data
*dev
= info
->par
;
879 * fbcon aggressively connects to first framebuffer it finds,
880 * preventing other clients (X) from working properly. Usually
881 * not what the user wants. Fail by default with option to enable.
883 if ((user
== 0) && (!console
))
886 /* If the USB device is gone, we don't accept new opens */
887 if (dev
->virtualized
)
892 kref_get(&dev
->kref
);
894 if (fb_defio
&& (info
->fbdefio
== NULL
)) {
895 /* enable defio at last moment if not disabled by client */
897 struct fb_deferred_io
*fbdefio
;
899 fbdefio
= kzalloc(sizeof(struct fb_deferred_io
), GFP_KERNEL
);
902 fbdefio
->delay
= DL_DEFIO_WRITE_DELAY
;
903 fbdefio
->deferred_io
= dlfb_dpy_deferred_io
;
906 info
->fbdefio
= fbdefio
;
907 fb_deferred_io_init(info
);
910 pr_notice("open /dev/fb%d user=%d fb_info=%p count=%d\n",
911 info
->node
, user
, info
, dev
->fb_count
);
917 * Called when all client interfaces to start transactions have been disabled,
918 * and all references to our device instance (dlfb_data) are released.
919 * Every transaction must have a reference, so we know are fully spun down
921 static void dlfb_free(struct kref
*kref
)
923 struct dlfb_data
*dev
= container_of(kref
, struct dlfb_data
, kref
);
925 vfree(dev
->backing_buffer
);
929 pr_warn("freeing dlfb_data %p\n", dev
);
934 static void dlfb_release_urb_work(struct work_struct
*work
)
936 struct urb_node
*unode
= container_of(work
, struct urb_node
,
937 release_urb_work
.work
);
939 up(&unode
->dev
->urbs
.limit_sem
);
942 static void dlfb_free_framebuffer(struct dlfb_data
*dev
)
944 struct fb_info
*info
= dev
->info
;
947 int node
= info
->node
;
949 unregister_framebuffer(info
);
951 if (info
->cmap
.len
!= 0)
952 fb_dealloc_cmap(&info
->cmap
);
953 if (info
->monspecs
.modedb
)
954 fb_destroy_modedb(info
->monspecs
.modedb
);
955 vfree(info
->screen_base
);
957 fb_destroy_modelist(&info
->modelist
);
961 /* Assume info structure is freed after this point */
962 framebuffer_release(info
);
964 pr_warn("fb_info for /dev/fb%d has been freed\n", node
);
967 /* ref taken in probe() as part of registering framebfufer */
968 kref_put(&dev
->kref
, dlfb_free
);
971 static void dlfb_free_framebuffer_work(struct work_struct
*work
)
973 struct dlfb_data
*dev
= container_of(work
, struct dlfb_data
,
974 free_framebuffer_work
.work
);
975 dlfb_free_framebuffer(dev
);
978 * Assumes caller is holding info->lock mutex (for open and release at least)
980 static int dlfb_ops_release(struct fb_info
*info
, int user
)
982 struct dlfb_data
*dev
= info
->par
;
986 /* We can't free fb_info here - fbmem will touch it when we return */
987 if (dev
->virtualized
&& (dev
->fb_count
== 0))
988 schedule_delayed_work(&dev
->free_framebuffer_work
, HZ
);
990 if ((dev
->fb_count
== 0) && (info
->fbdefio
)) {
991 fb_deferred_io_cleanup(info
);
992 kfree(info
->fbdefio
);
993 info
->fbdefio
= NULL
;
994 info
->fbops
->fb_mmap
= dlfb_ops_mmap
;
997 pr_warn("released /dev/fb%d user=%d count=%d\n",
998 info
->node
, user
, dev
->fb_count
);
1000 kref_put(&dev
->kref
, dlfb_free
);
1006 * Check whether a video mode is supported by the DisplayLink chip
1007 * We start from monitor's modes, so don't need to filter that here
1009 static int dlfb_is_valid_mode(struct fb_videomode
*mode
,
1010 struct fb_info
*info
)
1012 struct dlfb_data
*dev
= info
->par
;
1014 if (mode
->xres
* mode
->yres
> dev
->sku_pixel_limit
) {
1015 pr_warn("%dx%d beyond chip capabilities\n",
1016 mode
->xres
, mode
->yres
);
1020 pr_info("%dx%d @ %d Hz valid mode\n", mode
->xres
, mode
->yres
,
1026 static void dlfb_var_color_format(struct fb_var_screeninfo
*var
)
1028 const struct fb_bitfield red
= { 11, 5, 0 };
1029 const struct fb_bitfield green
= { 5, 6, 0 };
1030 const struct fb_bitfield blue
= { 0, 5, 0 };
1032 var
->bits_per_pixel
= 16;
1038 static int dlfb_ops_check_var(struct fb_var_screeninfo
*var
,
1039 struct fb_info
*info
)
1041 struct fb_videomode mode
;
1043 /* TODO: support dynamically changing framebuffer size */
1044 if ((var
->xres
* var
->yres
* 2) > info
->fix
.smem_len
)
1047 /* set device-specific elements of var unrelated to mode */
1048 dlfb_var_color_format(var
);
1050 fb_var_to_videomode(&mode
, var
);
1052 if (!dlfb_is_valid_mode(&mode
, info
))
1058 static int dlfb_ops_set_par(struct fb_info
*info
)
1060 struct dlfb_data
*dev
= info
->par
;
1062 u16
*pix_framebuffer
;
1065 pr_notice("set_par mode %dx%d\n", info
->var
.xres
, info
->var
.yres
);
1067 result
= dlfb_set_video_mode(dev
, &info
->var
);
1069 if ((result
== 0) && (dev
->fb_count
== 0)) {
1071 /* paint greenscreen */
1073 pix_framebuffer
= (u16
*) info
->screen_base
;
1074 for (i
= 0; i
< info
->fix
.smem_len
/ 2; i
++)
1075 pix_framebuffer
[i
] = 0x37e6;
1077 dlfb_handle_damage(dev
, 0, 0, info
->var
.xres
, info
->var
.yres
,
1084 /* To fonzi the jukebox (e.g. make blanking changes take effect) */
1085 static char *dlfb_dummy_render(char *buf
)
1088 *buf
++ = 0x6A; /* copy */
1089 *buf
++ = 0x00; /* from address*/
1092 *buf
++ = 0x01; /* one pixel */
1093 *buf
++ = 0x00; /* to address */
1100 * In order to come back from full DPMS off, we need to set the mode again
1102 static int dlfb_ops_blank(int blank_mode
, struct fb_info
*info
)
1104 struct dlfb_data
*dev
= info
->par
;
1108 pr_info("/dev/fb%d FB_BLANK mode %d --> %d\n",
1109 info
->node
, dev
->blank_mode
, blank_mode
);
1111 if ((dev
->blank_mode
== FB_BLANK_POWERDOWN
) &&
1112 (blank_mode
!= FB_BLANK_POWERDOWN
)) {
1114 /* returning from powerdown requires a fresh modeset */
1115 dlfb_set_video_mode(dev
, &info
->var
);
1118 urb
= dlfb_get_urb(dev
);
1122 bufptr
= (char *) urb
->transfer_buffer
;
1123 bufptr
= dlfb_vidreg_lock(bufptr
);
1124 bufptr
= dlfb_blanking(bufptr
, blank_mode
);
1125 bufptr
= dlfb_vidreg_unlock(bufptr
);
1127 /* seems like a render op is needed to have blank change take effect */
1128 bufptr
= dlfb_dummy_render(bufptr
);
1130 dlfb_submit_urb(dev
, urb
, bufptr
-
1131 (char *) urb
->transfer_buffer
);
1133 dev
->blank_mode
= blank_mode
;
1138 static struct fb_ops dlfb_ops
= {
1139 .owner
= THIS_MODULE
,
1140 .fb_read
= fb_sys_read
,
1141 .fb_write
= dlfb_ops_write
,
1142 .fb_setcolreg
= dlfb_ops_setcolreg
,
1143 .fb_fillrect
= dlfb_ops_fillrect
,
1144 .fb_copyarea
= dlfb_ops_copyarea
,
1145 .fb_imageblit
= dlfb_ops_imageblit
,
1146 .fb_mmap
= dlfb_ops_mmap
,
1147 .fb_ioctl
= dlfb_ops_ioctl
,
1148 .fb_open
= dlfb_ops_open
,
1149 .fb_release
= dlfb_ops_release
,
1150 .fb_blank
= dlfb_ops_blank
,
1151 .fb_check_var
= dlfb_ops_check_var
,
1152 .fb_set_par
= dlfb_ops_set_par
,
1157 * Assumes &info->lock held by caller
1158 * Assumes no active clients have framebuffer open
1160 static int dlfb_realloc_framebuffer(struct dlfb_data
*dev
, struct fb_info
*info
)
1162 int retval
= -ENOMEM
;
1163 int old_len
= info
->fix
.smem_len
;
1165 unsigned char *old_fb
= info
->screen_base
;
1166 unsigned char *new_fb
;
1167 unsigned char *new_back
= NULL
;
1169 pr_warn("Reallocating framebuffer. Addresses will change!\n");
1171 new_len
= info
->fix
.line_length
* info
->var
.yres
;
1173 if (PAGE_ALIGN(new_len
) > old_len
) {
1175 * Alloc system memory for virtual framebuffer
1177 new_fb
= vmalloc(new_len
);
1179 pr_err("Virtual framebuffer alloc failed\n");
1183 if (info
->screen_base
) {
1184 memcpy(new_fb
, old_fb
, old_len
);
1185 vfree(info
->screen_base
);
1188 info
->screen_base
= new_fb
;
1189 info
->fix
.smem_len
= PAGE_ALIGN(new_len
);
1190 info
->fix
.smem_start
= (unsigned long) new_fb
;
1191 info
->flags
= udlfb_info_flags
;
1194 * Second framebuffer copy to mirror the framebuffer state
1195 * on the physical USB device. We can function without this.
1196 * But with imperfect damage info we may send pixels over USB
1197 * that were, in fact, unchanged - wasting limited USB bandwidth
1200 new_back
= vzalloc(new_len
);
1202 pr_info("No shadow/backing buffer allocated\n");
1204 vfree(dev
->backing_buffer
);
1205 dev
->backing_buffer
= new_back
;
1216 * 1) Get EDID from hw, or use sw default
1217 * 2) Parse into various fb_info structs
1218 * 3) Allocate virtual framebuffer memory to back highest res mode
1220 * Parses EDID into three places used by various parts of fbdev:
1221 * fb_var_screeninfo contains the timing of the monitor's preferred mode
1222 * fb_info.monspecs is full parsed EDID info, including monspecs.modedb
1223 * fb_info.modelist is a linked list of all monitor & VESA modes which work
1225 * If EDID is not readable/valid, then modelist is all VESA modes,
1226 * monspecs is NULL, and fb_var_screeninfo is set to safe VESA mode
1227 * Returns 0 if successful
1229 static int dlfb_setup_modes(struct dlfb_data
*dev
,
1230 struct fb_info
*info
,
1231 char *default_edid
, size_t default_edid_size
)
1234 const struct fb_videomode
*default_vmode
= NULL
;
1239 if (info
->dev
) /* only use mutex if info has been registered */
1240 mutex_lock(&info
->lock
);
1242 edid
= kmalloc(EDID_LENGTH
, GFP_KERNEL
);
1248 fb_destroy_modelist(&info
->modelist
);
1249 memset(&info
->monspecs
, 0, sizeof(info
->monspecs
));
1252 * Try to (re)read EDID from hardware first
1253 * EDID data may return, but not parse as valid
1254 * Try again a few times, in case of e.g. analog cable noise
1258 i
= dlfb_get_edid(dev
, edid
, EDID_LENGTH
);
1260 if (i
>= EDID_LENGTH
)
1261 fb_edid_to_monspecs(edid
, &info
->monspecs
);
1263 if (info
->monspecs
.modedb_len
> 0) {
1270 /* If that fails, use a previously returned EDID if available */
1271 if (info
->monspecs
.modedb_len
== 0) {
1273 pr_err("Unable to get valid EDID from device/display\n");
1276 fb_edid_to_monspecs(dev
->edid
, &info
->monspecs
);
1277 if (info
->monspecs
.modedb_len
> 0)
1278 pr_err("Using previously queried EDID\n");
1282 /* If that fails, use the default EDID we were handed */
1283 if (info
->monspecs
.modedb_len
== 0) {
1284 if (default_edid_size
>= EDID_LENGTH
) {
1285 fb_edid_to_monspecs(default_edid
, &info
->monspecs
);
1286 if (info
->monspecs
.modedb_len
> 0) {
1287 memcpy(edid
, default_edid
, default_edid_size
);
1289 dev
->edid_size
= default_edid_size
;
1290 pr_err("Using default/backup EDID\n");
1295 /* If we've got modes, let's pick a best default mode */
1296 if (info
->monspecs
.modedb_len
> 0) {
1298 for (i
= 0; i
< info
->monspecs
.modedb_len
; i
++) {
1299 if (dlfb_is_valid_mode(&info
->monspecs
.modedb
[i
], info
))
1300 fb_add_videomode(&info
->monspecs
.modedb
[i
],
1304 /* if we've removed top/best mode */
1306 &= ~FB_MISC_1ST_DETAIL
;
1310 default_vmode
= fb_find_best_display(&info
->monspecs
,
1314 /* If everything else has failed, fall back to safe default mode */
1315 if (default_vmode
== NULL
) {
1317 struct fb_videomode fb_vmode
= {0};
1320 * Add the standard VESA modes to our modelist
1321 * Since we don't have EDID, there may be modes that
1322 * overspec monitor and/or are incorrect aspect ratio, etc.
1323 * But at least the user has a chance to choose
1325 for (i
= 0; i
< VESA_MODEDB_SIZE
; i
++) {
1326 if (dlfb_is_valid_mode((struct fb_videomode
*)
1327 &vesa_modes
[i
], info
))
1328 fb_add_videomode(&vesa_modes
[i
],
1333 * default to resolution safe for projectors
1334 * (since they are most common case without EDID)
1336 fb_vmode
.xres
= 800;
1337 fb_vmode
.yres
= 600;
1338 fb_vmode
.refresh
= 60;
1339 default_vmode
= fb_find_nearest_mode(&fb_vmode
,
1343 /* If we have good mode and no active clients*/
1344 if ((default_vmode
!= NULL
) && (dev
->fb_count
== 0)) {
1346 fb_videomode_to_var(&info
->var
, default_vmode
);
1347 dlfb_var_color_format(&info
->var
);
1350 * with mode size info, we can now alloc our framebuffer.
1352 memcpy(&info
->fix
, &dlfb_fix
, sizeof(dlfb_fix
));
1353 info
->fix
.line_length
= info
->var
.xres
*
1354 (info
->var
.bits_per_pixel
/ 8);
1356 result
= dlfb_realloc_framebuffer(dev
, info
);
1362 if (edid
&& (dev
->edid
!= edid
))
1366 mutex_unlock(&info
->lock
);
1371 static ssize_t
metrics_bytes_rendered_show(struct device
*fbdev
,
1372 struct device_attribute
*a
, char *buf
) {
1373 struct fb_info
*fb_info
= dev_get_drvdata(fbdev
);
1374 struct dlfb_data
*dev
= fb_info
->par
;
1375 return snprintf(buf
, PAGE_SIZE
, "%u\n",
1376 atomic_read(&dev
->bytes_rendered
));
1379 static ssize_t
metrics_bytes_identical_show(struct device
*fbdev
,
1380 struct device_attribute
*a
, char *buf
) {
1381 struct fb_info
*fb_info
= dev_get_drvdata(fbdev
);
1382 struct dlfb_data
*dev
= fb_info
->par
;
1383 return snprintf(buf
, PAGE_SIZE
, "%u\n",
1384 atomic_read(&dev
->bytes_identical
));
1387 static ssize_t
metrics_bytes_sent_show(struct device
*fbdev
,
1388 struct device_attribute
*a
, char *buf
) {
1389 struct fb_info
*fb_info
= dev_get_drvdata(fbdev
);
1390 struct dlfb_data
*dev
= fb_info
->par
;
1391 return snprintf(buf
, PAGE_SIZE
, "%u\n",
1392 atomic_read(&dev
->bytes_sent
));
1395 static ssize_t
metrics_cpu_kcycles_used_show(struct device
*fbdev
,
1396 struct device_attribute
*a
, char *buf
) {
1397 struct fb_info
*fb_info
= dev_get_drvdata(fbdev
);
1398 struct dlfb_data
*dev
= fb_info
->par
;
1399 return snprintf(buf
, PAGE_SIZE
, "%u\n",
1400 atomic_read(&dev
->cpu_kcycles_used
));
1403 static ssize_t
edid_show(
1405 struct kobject
*kobj
, struct bin_attribute
*a
,
1406 char *buf
, loff_t off
, size_t count
) {
1407 struct device
*fbdev
= container_of(kobj
, struct device
, kobj
);
1408 struct fb_info
*fb_info
= dev_get_drvdata(fbdev
);
1409 struct dlfb_data
*dev
= fb_info
->par
;
1411 if (dev
->edid
== NULL
)
1414 if ((off
>= dev
->edid_size
) || (count
> dev
->edid_size
))
1417 if (off
+ count
> dev
->edid_size
)
1418 count
= dev
->edid_size
- off
;
1420 pr_info("sysfs edid copy %p to %p, %d bytes\n",
1421 dev
->edid
, buf
, (int) count
);
1423 memcpy(buf
, dev
->edid
, count
);
1428 static ssize_t
edid_store(
1430 struct kobject
*kobj
, struct bin_attribute
*a
,
1431 char *src
, loff_t src_off
, size_t src_size
) {
1432 struct device
*fbdev
= container_of(kobj
, struct device
, kobj
);
1433 struct fb_info
*fb_info
= dev_get_drvdata(fbdev
);
1434 struct dlfb_data
*dev
= fb_info
->par
;
1437 /* We only support write of entire EDID at once, no offset*/
1438 if ((src_size
!= EDID_LENGTH
) || (src_off
!= 0))
1441 ret
= dlfb_setup_modes(dev
, fb_info
, src
, src_size
);
1445 if (!dev
->edid
|| memcmp(src
, dev
->edid
, src_size
))
1448 pr_info("sysfs written EDID is new default\n");
1449 dlfb_ops_set_par(fb_info
);
1453 static ssize_t
metrics_reset_store(struct device
*fbdev
,
1454 struct device_attribute
*attr
,
1455 const char *buf
, size_t count
)
1457 struct fb_info
*fb_info
= dev_get_drvdata(fbdev
);
1458 struct dlfb_data
*dev
= fb_info
->par
;
1460 atomic_set(&dev
->bytes_rendered
, 0);
1461 atomic_set(&dev
->bytes_identical
, 0);
1462 atomic_set(&dev
->bytes_sent
, 0);
1463 atomic_set(&dev
->cpu_kcycles_used
, 0);
1468 static struct bin_attribute edid_attr
= {
1469 .attr
.name
= "edid",
1471 .size
= EDID_LENGTH
,
1476 static struct device_attribute fb_device_attrs
[] = {
1477 __ATTR_RO(metrics_bytes_rendered
),
1478 __ATTR_RO(metrics_bytes_identical
),
1479 __ATTR_RO(metrics_bytes_sent
),
1480 __ATTR_RO(metrics_cpu_kcycles_used
),
1481 __ATTR(metrics_reset
, S_IWUSR
, NULL
, metrics_reset_store
),
1485 * This is necessary before we can communicate with the display controller.
1487 static int dlfb_select_std_channel(struct dlfb_data
*dev
)
1490 u8 set_def_chn
[] = { 0x57, 0xCD, 0xDC, 0xA7,
1491 0x1C, 0x88, 0x5E, 0x15,
1492 0x60, 0xFE, 0xC6, 0x97,
1493 0x16, 0x3D, 0x47, 0xF2 };
1495 ret
= usb_control_msg(dev
->udev
, usb_sndctrlpipe(dev
->udev
, 0),
1496 NR_USB_REQUEST_CHANNEL
,
1497 (USB_DIR_OUT
| USB_TYPE_VENDOR
), 0, 0,
1498 set_def_chn
, sizeof(set_def_chn
), USB_CTRL_SET_TIMEOUT
);
1502 static int dlfb_parse_vendor_descriptor(struct dlfb_data
*dev
,
1503 struct usb_interface
*interface
)
1511 buf
= kzalloc(MAX_VENDOR_DESCRIPTOR_SIZE
, GFP_KERNEL
);
1516 total_len
= usb_get_descriptor(interface_to_usbdev(interface
),
1517 0x5f, /* vendor specific */
1518 0, desc
, MAX_VENDOR_DESCRIPTOR_SIZE
);
1520 /* if not found, look in configuration descriptor */
1521 if (total_len
< 0) {
1522 if (0 == usb_get_extra_descriptor(interface
->cur_altsetting
,
1524 total_len
= (int) desc
[0];
1527 if (total_len
> 5) {
1528 pr_info("vendor descriptor length:%x data:%11ph\n", total_len
,
1531 if ((desc
[0] != total_len
) || /* descriptor length */
1532 (desc
[1] != 0x5f) || /* vendor descriptor type */
1533 (desc
[2] != 0x01) || /* version (2 bytes) */
1534 (desc
[3] != 0x00) ||
1535 (desc
[4] != total_len
- 2)) /* length after type */
1538 desc_end
= desc
+ total_len
;
1539 desc
+= 5; /* the fixed header we've already parsed */
1541 while (desc
< desc_end
) {
1545 key
= le16_to_cpu(*((u16
*) desc
));
1546 desc
+= sizeof(u16
);
1551 case 0x0200: { /* max_area */
1553 max_area
= le32_to_cpu(*((u32
*)desc
));
1554 pr_warn("DL chip limited to %d pixel modes\n",
1556 dev
->sku_pixel_limit
= max_area
;
1565 pr_info("vendor descriptor not available (%d)\n", total_len
);
1571 /* allow udlfb to load for now even if firmware unrecognized */
1572 pr_err("Unrecognized vendor firmware descriptor\n");
1579 static void dlfb_init_framebuffer_work(struct work_struct
*work
);
1581 static int dlfb_usb_probe(struct usb_interface
*interface
,
1582 const struct usb_device_id
*id
)
1584 struct usb_device
*usbdev
;
1585 struct dlfb_data
*dev
= NULL
;
1586 int retval
= -ENOMEM
;
1588 /* usb initialization */
1590 usbdev
= interface_to_usbdev(interface
);
1592 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
1594 dev_err(&interface
->dev
, "dlfb_usb_probe: failed alloc of dev struct\n");
1598 kref_init(&dev
->kref
); /* matching kref_put in usb .disconnect fn */
1601 dev
->gdev
= &usbdev
->dev
; /* our generic struct device * */
1602 usb_set_intfdata(interface
, dev
);
1604 pr_info("%s %s - serial #%s\n",
1605 usbdev
->manufacturer
, usbdev
->product
, usbdev
->serial
);
1606 pr_info("vid_%04x&pid_%04x&rev_%04x driver's dlfb_data struct at %p\n",
1607 usbdev
->descriptor
.idVendor
, usbdev
->descriptor
.idProduct
,
1608 usbdev
->descriptor
.bcdDevice
, dev
);
1609 pr_info("console enable=%d\n", console
);
1610 pr_info("fb_defio enable=%d\n", fb_defio
);
1611 pr_info("shadow enable=%d\n", shadow
);
1613 dev
->sku_pixel_limit
= 2048 * 1152; /* default to maximum */
1615 if (!dlfb_parse_vendor_descriptor(dev
, interface
)) {
1616 pr_err("firmware not recognized. Assume incompatible device\n");
1621 pr_warn("DL chip limit of %d overridden"
1622 " by module param to %d\n",
1623 dev
->sku_pixel_limit
, pixel_limit
);
1624 dev
->sku_pixel_limit
= pixel_limit
;
1628 if (!dlfb_alloc_urb_list(dev
, WRITES_IN_FLIGHT
, MAX_TRANSFER
)) {
1630 pr_err("dlfb_alloc_urb_list failed\n");
1634 kref_get(&dev
->kref
); /* matching kref_put in free_framebuffer_work */
1636 /* We don't register a new USB class. Our client interface is fbdev */
1638 /* Workitem keep things fast & simple during USB enumeration */
1639 INIT_DELAYED_WORK(&dev
->init_framebuffer_work
,
1640 dlfb_init_framebuffer_work
);
1641 schedule_delayed_work(&dev
->init_framebuffer_work
, 0);
1648 kref_put(&dev
->kref
, dlfb_free
); /* ref for framebuffer */
1649 kref_put(&dev
->kref
, dlfb_free
); /* last ref from kref_init */
1651 /* dev has been deallocated. Do not dereference */
1657 static void dlfb_init_framebuffer_work(struct work_struct
*work
)
1659 struct dlfb_data
*dev
= container_of(work
, struct dlfb_data
,
1660 init_framebuffer_work
.work
);
1661 struct fb_info
*info
;
1665 /* allocates framebuffer driver structure, not framebuffer memory */
1666 info
= framebuffer_alloc(0, dev
->gdev
);
1669 pr_err("framebuffer_alloc failed\n");
1675 info
->pseudo_palette
= dev
->pseudo_palette
;
1676 info
->fbops
= &dlfb_ops
;
1678 retval
= fb_alloc_cmap(&info
->cmap
, 256, 0);
1680 pr_err("fb_alloc_cmap failed %x\n", retval
);
1684 INIT_DELAYED_WORK(&dev
->free_framebuffer_work
,
1685 dlfb_free_framebuffer_work
);
1687 INIT_LIST_HEAD(&info
->modelist
);
1689 retval
= dlfb_setup_modes(dev
, info
, NULL
, 0);
1691 pr_err("unable to find common mode for display and adapter\n");
1695 /* ready to begin using device */
1697 atomic_set(&dev
->usb_active
, 1);
1698 dlfb_select_std_channel(dev
);
1700 dlfb_ops_check_var(&info
->var
, info
);
1701 dlfb_ops_set_par(info
);
1703 retval
= register_framebuffer(info
);
1705 pr_err("register_framebuffer failed %d\n", retval
);
1709 for (i
= 0; i
< ARRAY_SIZE(fb_device_attrs
); i
++) {
1710 retval
= device_create_file(info
->dev
, &fb_device_attrs
[i
]);
1712 pr_warn("device_create_file failed %d\n", retval
);
1716 retval
= device_create_bin_file(info
->dev
, &edid_attr
);
1718 pr_warn("device_create_bin_file failed %d\n", retval
);
1721 pr_info("DisplayLink USB device /dev/fb%d attached. %dx%d resolution."
1722 " Using %dK framebuffer memory\n", info
->node
,
1723 info
->var
.xres
, info
->var
.yres
,
1724 ((dev
->backing_buffer
) ?
1725 info
->fix
.smem_len
* 2 : info
->fix
.smem_len
) >> 10);
1729 dlfb_free_framebuffer(dev
);
1732 static void dlfb_usb_disconnect(struct usb_interface
*interface
)
1734 struct dlfb_data
*dev
;
1735 struct fb_info
*info
;
1738 dev
= usb_get_intfdata(interface
);
1741 pr_info("USB disconnect starting\n");
1743 /* we virtualize until all fb clients release. Then we free */
1744 dev
->virtualized
= true;
1746 /* When non-active we'll update virtual framebuffer, but no new urbs */
1747 atomic_set(&dev
->usb_active
, 0);
1749 /* this function will wait for all in-flight urbs to complete */
1750 dlfb_free_urb_list(dev
);
1753 /* remove udlfb's sysfs interfaces */
1754 for (i
= 0; i
< ARRAY_SIZE(fb_device_attrs
); i
++)
1755 device_remove_file(info
->dev
, &fb_device_attrs
[i
]);
1756 device_remove_bin_file(info
->dev
, &edid_attr
);
1757 unlink_framebuffer(info
);
1760 usb_set_intfdata(interface
, NULL
);
1764 /* if clients still have us open, will be freed on last close */
1765 if (dev
->fb_count
== 0)
1766 schedule_delayed_work(&dev
->free_framebuffer_work
, 0);
1768 /* release reference taken by kref_init in probe() */
1769 kref_put(&dev
->kref
, dlfb_free
);
1771 /* consider dlfb_data freed */
1776 static struct usb_driver dlfb_driver
= {
1778 .probe
= dlfb_usb_probe
,
1779 .disconnect
= dlfb_usb_disconnect
,
1780 .id_table
= id_table
,
1783 module_usb_driver(dlfb_driver
);
1785 static void dlfb_urb_completion(struct urb
*urb
)
1787 struct urb_node
*unode
= urb
->context
;
1788 struct dlfb_data
*dev
= unode
->dev
;
1789 unsigned long flags
;
1791 /* sync/async unlink faults aren't errors */
1793 if (!(urb
->status
== -ENOENT
||
1794 urb
->status
== -ECONNRESET
||
1795 urb
->status
== -ESHUTDOWN
)) {
1796 pr_err("%s - nonzero write bulk status received: %d\n",
1797 __func__
, urb
->status
);
1798 atomic_set(&dev
->lost_pixels
, 1);
1802 urb
->transfer_buffer_length
= dev
->urbs
.size
; /* reset to actual */
1804 spin_lock_irqsave(&dev
->urbs
.lock
, flags
);
1805 list_add_tail(&unode
->entry
, &dev
->urbs
.list
);
1806 dev
->urbs
.available
++;
1807 spin_unlock_irqrestore(&dev
->urbs
.lock
, flags
);
1810 * When using fb_defio, we deadlock if up() is called
1811 * while another is waiting. So queue to another process.
1814 schedule_delayed_work(&unode
->release_urb_work
, 0);
1816 up(&dev
->urbs
.limit_sem
);
1819 static void dlfb_free_urb_list(struct dlfb_data
*dev
)
1821 int count
= dev
->urbs
.count
;
1822 struct list_head
*node
;
1823 struct urb_node
*unode
;
1826 unsigned long flags
;
1828 pr_notice("Freeing all render urbs\n");
1830 /* keep waiting and freeing, until we've got 'em all */
1833 /* Getting interrupted means a leak, but ok at disconnect */
1834 ret
= down_interruptible(&dev
->urbs
.limit_sem
);
1838 spin_lock_irqsave(&dev
->urbs
.lock
, flags
);
1840 node
= dev
->urbs
.list
.next
; /* have reserved one with sem */
1841 list_del_init(node
);
1843 spin_unlock_irqrestore(&dev
->urbs
.lock
, flags
);
1845 unode
= list_entry(node
, struct urb_node
, entry
);
1848 /* Free each separately allocated piece */
1849 usb_free_coherent(urb
->dev
, dev
->urbs
.size
,
1850 urb
->transfer_buffer
, urb
->transfer_dma
);
1855 dev
->urbs
.count
= 0;
1858 static int dlfb_alloc_urb_list(struct dlfb_data
*dev
, int count
, size_t size
)
1862 struct urb_node
*unode
;
1865 spin_lock_init(&dev
->urbs
.lock
);
1867 dev
->urbs
.size
= size
;
1868 INIT_LIST_HEAD(&dev
->urbs
.list
);
1871 unode
= kzalloc(sizeof(struct urb_node
), GFP_KERNEL
);
1876 INIT_DELAYED_WORK(&unode
->release_urb_work
,
1877 dlfb_release_urb_work
);
1879 urb
= usb_alloc_urb(0, GFP_KERNEL
);
1886 buf
= usb_alloc_coherent(dev
->udev
, MAX_TRANSFER
, GFP_KERNEL
,
1887 &urb
->transfer_dma
);
1894 /* urb->transfer_buffer_length set to actual before submit */
1895 usb_fill_bulk_urb(urb
, dev
->udev
, usb_sndbulkpipe(dev
->udev
, 1),
1896 buf
, size
, dlfb_urb_completion
, unode
);
1897 urb
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
1899 list_add_tail(&unode
->entry
, &dev
->urbs
.list
);
1904 sema_init(&dev
->urbs
.limit_sem
, i
);
1905 dev
->urbs
.count
= i
;
1906 dev
->urbs
.available
= i
;
1908 pr_notice("allocated %d %d byte urbs\n", i
, (int) size
);
1913 static struct urb
*dlfb_get_urb(struct dlfb_data
*dev
)
1916 struct list_head
*entry
;
1917 struct urb_node
*unode
;
1918 struct urb
*urb
= NULL
;
1919 unsigned long flags
;
1921 /* Wait for an in-flight buffer to complete and get re-queued */
1922 ret
= down_timeout(&dev
->urbs
.limit_sem
, GET_URB_TIMEOUT
);
1924 atomic_set(&dev
->lost_pixels
, 1);
1925 pr_warn("wait for urb interrupted: %x available: %d\n",
1926 ret
, dev
->urbs
.available
);
1930 spin_lock_irqsave(&dev
->urbs
.lock
, flags
);
1932 BUG_ON(list_empty(&dev
->urbs
.list
)); /* reserved one with limit_sem */
1933 entry
= dev
->urbs
.list
.next
;
1934 list_del_init(entry
);
1935 dev
->urbs
.available
--;
1937 spin_unlock_irqrestore(&dev
->urbs
.lock
, flags
);
1939 unode
= list_entry(entry
, struct urb_node
, entry
);
1946 static int dlfb_submit_urb(struct dlfb_data
*dev
, struct urb
*urb
, size_t len
)
1950 BUG_ON(len
> dev
->urbs
.size
);
1952 urb
->transfer_buffer_length
= len
; /* set to actual payload len */
1953 ret
= usb_submit_urb(urb
, GFP_KERNEL
);
1955 dlfb_urb_completion(urb
); /* because no one else will */
1956 atomic_set(&dev
->lost_pixels
, 1);
1957 pr_err("usb_submit_urb error %x\n", ret
);
1962 module_param(console
, bool, S_IWUSR
| S_IRUSR
| S_IWGRP
| S_IRGRP
);
1963 MODULE_PARM_DESC(console
, "Allow fbcon to open framebuffer");
1965 module_param(fb_defio
, bool, S_IWUSR
| S_IRUSR
| S_IWGRP
| S_IRGRP
);
1966 MODULE_PARM_DESC(fb_defio
, "Page fault detection of mmap writes");
1968 module_param(shadow
, bool, S_IWUSR
| S_IRUSR
| S_IWGRP
| S_IRGRP
);
1969 MODULE_PARM_DESC(shadow
, "Shadow vid mem. Disable to save mem but lose perf");
1971 module_param(pixel_limit
, int, S_IWUSR
| S_IRUSR
| S_IWGRP
| S_IRGRP
);
1972 MODULE_PARM_DESC(pixel_limit
, "Force limit on max mode (in x*y pixels)");
1974 MODULE_AUTHOR("Roberto De Ioris <roberto@unbit.it>, "
1975 "Jaya Kumar <jayakumar.lkml@gmail.com>, "
1976 "Bernie Thompson <bernie@plugable.com>");
1977 MODULE_DESCRIPTION("DisplayLink kernel framebuffer driver");
1978 MODULE_LICENSE("GPL");