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 #include <linux/module.h>
20 #include <linux/kernel.h>
21 #include <linux/init.h>
22 #include <linux/usb.h>
23 #include <linux/uaccess.h>
26 #include <linux/vmalloc.h>
27 #include <linux/slab.h>
28 #include <linux/prefetch.h>
29 #include <linux/delay.h>
30 #include <asm/unaligned.h>
31 #include <video/udlfb.h>
34 static const struct fb_fix_screeninfo dlfb_fix
= {
36 .type
= FB_TYPE_PACKED_PIXELS
,
37 .visual
= FB_VISUAL_TRUECOLOR
,
41 .accel
= FB_ACCEL_NONE
,
44 static const u32 udlfb_info_flags
= FBINFO_DEFAULT
| FBINFO_READS_FAST
|
46 FBINFO_HWACCEL_IMAGEBLIT
| FBINFO_HWACCEL_FILLRECT
|
47 FBINFO_HWACCEL_COPYAREA
| FBINFO_MISC_ALWAYS_SETPAR
;
50 * There are many DisplayLink-based graphics products, all with unique PIDs.
51 * So we match on DisplayLink's VID + Vendor-Defined Interface Class (0xff)
52 * We also require a match on SubClass (0x00) and Protocol (0x00),
53 * which is compatible with all known USB 2.0 era graphics chips and firmware,
54 * but allows DisplayLink to increment those for any future incompatible chips
56 static const struct usb_device_id id_table
[] = {
58 .bInterfaceClass
= 0xff,
59 .bInterfaceSubClass
= 0x00,
60 .bInterfaceProtocol
= 0x00,
61 .match_flags
= USB_DEVICE_ID_MATCH_VENDOR
|
62 USB_DEVICE_ID_MATCH_INT_CLASS
|
63 USB_DEVICE_ID_MATCH_INT_SUBCLASS
|
64 USB_DEVICE_ID_MATCH_INT_PROTOCOL
,
68 MODULE_DEVICE_TABLE(usb
, id_table
);
71 static bool console
= 1; /* Allow fbcon to open framebuffer */
72 static bool fb_defio
= 1; /* Detect mmap writes using page faults */
73 static bool shadow
= 1; /* Optionally disable shadow framebuffer */
74 static int pixel_limit
; /* Optionally force a pixel resolution limit */
76 struct dlfb_deferred_free
{
77 struct list_head list
;
81 static int dlfb_realloc_framebuffer(struct dlfb_data
*dlfb
, struct fb_info
*info
, u32 new_len
);
83 /* dlfb keeps a list of urbs for efficient bulk transfers */
84 static void dlfb_urb_completion(struct urb
*urb
);
85 static struct urb
*dlfb_get_urb(struct dlfb_data
*dlfb
);
86 static int dlfb_submit_urb(struct dlfb_data
*dlfb
, struct urb
* urb
, size_t len
);
87 static int dlfb_alloc_urb_list(struct dlfb_data
*dlfb
, int count
, size_t size
);
88 static void dlfb_free_urb_list(struct dlfb_data
*dlfb
);
91 * All DisplayLink bulk operations start with 0xAF, followed by specific code
92 * All operations are written to buffers which then later get sent to device
94 static char *dlfb_set_register(char *buf
, u8 reg
, u8 val
)
103 static char *dlfb_vidreg_lock(char *buf
)
105 return dlfb_set_register(buf
, 0xFF, 0x00);
108 static char *dlfb_vidreg_unlock(char *buf
)
110 return dlfb_set_register(buf
, 0xFF, 0xFF);
114 * Map FB_BLANK_* to DisplayLink register
116 * ----- -----------------------------
117 * 0x00 FB_BLANK_UNBLANK (0)
119 * 0x03 FB_BLANK_VSYNC_SUSPEND (2)
120 * 0x05 FB_BLANK_HSYNC_SUSPEND (3)
121 * 0x07 FB_BLANK_POWERDOWN (4) Note: requires modeset to come back
123 static char *dlfb_blanking(char *buf
, int fb_blank
)
128 case FB_BLANK_POWERDOWN
:
131 case FB_BLANK_HSYNC_SUSPEND
:
134 case FB_BLANK_VSYNC_SUSPEND
:
137 case FB_BLANK_NORMAL
:
144 buf
= dlfb_set_register(buf
, 0x1F, reg
);
149 static char *dlfb_set_color_depth(char *buf
, u8 selection
)
151 return dlfb_set_register(buf
, 0x00, selection
);
154 static char *dlfb_set_base16bpp(char *wrptr
, u32 base
)
156 /* the base pointer is 16 bits wide, 0x20 is hi byte. */
157 wrptr
= dlfb_set_register(wrptr
, 0x20, base
>> 16);
158 wrptr
= dlfb_set_register(wrptr
, 0x21, base
>> 8);
159 return dlfb_set_register(wrptr
, 0x22, base
);
163 * DisplayLink HW has separate 16bpp and 8bpp framebuffers.
164 * In 24bpp modes, the low 323 RGB bits go in the 8bpp framebuffer
166 static char *dlfb_set_base8bpp(char *wrptr
, u32 base
)
168 wrptr
= dlfb_set_register(wrptr
, 0x26, base
>> 16);
169 wrptr
= dlfb_set_register(wrptr
, 0x27, base
>> 8);
170 return dlfb_set_register(wrptr
, 0x28, base
);
173 static char *dlfb_set_register_16(char *wrptr
, u8 reg
, u16 value
)
175 wrptr
= dlfb_set_register(wrptr
, reg
, value
>> 8);
176 return dlfb_set_register(wrptr
, reg
+1, value
);
180 * This is kind of weird because the controller takes some
181 * register values in a different byte order than other registers.
183 static char *dlfb_set_register_16be(char *wrptr
, u8 reg
, u16 value
)
185 wrptr
= dlfb_set_register(wrptr
, reg
, value
);
186 return dlfb_set_register(wrptr
, reg
+1, value
>> 8);
190 * LFSR is linear feedback shift register. The reason we have this is
191 * because the display controller needs to minimize the clock depth of
192 * various counters used in the display path. So this code reverses the
193 * provided value into the lfsr16 value by counting backwards to get
194 * the value that needs to be set in the hardware comparator to get the
195 * same actual count. This makes sense once you read above a couple of
196 * times and think about it from a hardware perspective.
198 static u16
dlfb_lfsr16(u16 actual_count
)
200 u32 lv
= 0xFFFF; /* This is the lfsr value that the hw starts with */
202 while (actual_count
--) {
204 (((lv
>> 15) ^ (lv
>> 4) ^ (lv
>> 2) ^ (lv
>> 1)) & 1))
212 * This does LFSR conversion on the value that is to be written.
213 * See LFSR explanation above for more detail.
215 static char *dlfb_set_register_lfsr16(char *wrptr
, u8 reg
, u16 value
)
217 return dlfb_set_register_16(wrptr
, reg
, dlfb_lfsr16(value
));
221 * This takes a standard fbdev screeninfo struct and all of its monitor mode
222 * details and converts them into the DisplayLink equivalent register commands.
224 static char *dlfb_set_vid_cmds(char *wrptr
, struct fb_var_screeninfo
*var
)
230 /* x display start */
231 xds
= var
->left_margin
+ var
->hsync_len
;
232 wrptr
= dlfb_set_register_lfsr16(wrptr
, 0x01, xds
);
234 xde
= xds
+ var
->xres
;
235 wrptr
= dlfb_set_register_lfsr16(wrptr
, 0x03, xde
);
237 /* y display start */
238 yds
= var
->upper_margin
+ var
->vsync_len
;
239 wrptr
= dlfb_set_register_lfsr16(wrptr
, 0x05, yds
);
241 yde
= yds
+ var
->yres
;
242 wrptr
= dlfb_set_register_lfsr16(wrptr
, 0x07, yde
);
244 /* x end count is active + blanking - 1 */
245 wrptr
= dlfb_set_register_lfsr16(wrptr
, 0x09,
246 xde
+ var
->right_margin
- 1);
248 /* libdlo hardcodes hsync start to 1 */
249 wrptr
= dlfb_set_register_lfsr16(wrptr
, 0x0B, 1);
251 /* hsync end is width of sync pulse + 1 */
252 wrptr
= dlfb_set_register_lfsr16(wrptr
, 0x0D, var
->hsync_len
+ 1);
254 /* hpixels is active pixels */
255 wrptr
= dlfb_set_register_16(wrptr
, 0x0F, var
->xres
);
257 /* yendcount is vertical active + vertical blanking */
258 yec
= var
->yres
+ var
->upper_margin
+ var
->lower_margin
+
260 wrptr
= dlfb_set_register_lfsr16(wrptr
, 0x11, yec
);
262 /* libdlo hardcodes vsync start to 0 */
263 wrptr
= dlfb_set_register_lfsr16(wrptr
, 0x13, 0);
265 /* vsync end is width of vsync pulse */
266 wrptr
= dlfb_set_register_lfsr16(wrptr
, 0x15, var
->vsync_len
);
268 /* vpixels is active pixels */
269 wrptr
= dlfb_set_register_16(wrptr
, 0x17, var
->yres
);
271 /* convert picoseconds to 5kHz multiple for pclk5k = x * 1E12/5k */
272 wrptr
= dlfb_set_register_16be(wrptr
, 0x1B,
273 200*1000*1000/var
->pixclock
);
279 * This takes a standard fbdev screeninfo struct that was fetched or prepared
280 * and then generates the appropriate command sequence that then drives the
281 * display controller.
283 static int dlfb_set_video_mode(struct dlfb_data
*dlfb
,
284 struct fb_var_screeninfo
*var
)
292 if (!atomic_read(&dlfb
->usb_active
))
295 urb
= dlfb_get_urb(dlfb
);
299 buf
= (char *) urb
->transfer_buffer
;
302 * This first section has to do with setting the base address on the
303 * controller * associated with the display. There are 2 base
304 * pointers, currently, we only * use the 16 bpp segment.
306 wrptr
= dlfb_vidreg_lock(buf
);
307 wrptr
= dlfb_set_color_depth(wrptr
, 0x00);
308 /* set base for 16bpp segment to 0 */
309 wrptr
= dlfb_set_base16bpp(wrptr
, 0);
310 /* set base for 8bpp segment to end of fb */
311 wrptr
= dlfb_set_base8bpp(wrptr
, dlfb
->info
->fix
.smem_len
);
313 wrptr
= dlfb_set_vid_cmds(wrptr
, var
);
314 wrptr
= dlfb_blanking(wrptr
, FB_BLANK_UNBLANK
);
315 wrptr
= dlfb_vidreg_unlock(wrptr
);
317 writesize
= wrptr
- buf
;
319 retval
= dlfb_submit_urb(dlfb
, urb
, writesize
);
321 dlfb
->blank_mode
= FB_BLANK_UNBLANK
;
326 static int dlfb_ops_mmap(struct fb_info
*info
, struct vm_area_struct
*vma
)
328 unsigned long start
= vma
->vm_start
;
329 unsigned long size
= vma
->vm_end
- vma
->vm_start
;
330 unsigned long offset
= vma
->vm_pgoff
<< PAGE_SHIFT
;
331 unsigned long page
, pos
;
333 if (vma
->vm_pgoff
> (~0UL >> PAGE_SHIFT
))
335 if (size
> info
->fix
.smem_len
)
337 if (offset
> info
->fix
.smem_len
- size
)
340 pos
= (unsigned long)info
->fix
.smem_start
+ offset
;
342 dev_dbg(info
->dev
, "mmap() framebuffer addr:%lu size:%lu\n",
346 page
= vmalloc_to_pfn((void *)pos
);
347 if (remap_pfn_range(vma
, start
, page
, PAGE_SIZE
, PAGE_SHARED
))
352 if (size
> PAGE_SIZE
)
362 * Trims identical data from front and back of line
363 * Sets new front buffer address and width
364 * And returns byte count of identical pixels
365 * Assumes CPU natural alignment (unsigned long)
366 * for back and front buffer ptrs and width
368 static int dlfb_trim_hline(const u8
*bback
, const u8
**bfront
, int *width_bytes
)
371 const unsigned long *back
= (const unsigned long *) bback
;
372 const unsigned long *front
= (const unsigned long *) *bfront
;
373 const int width
= *width_bytes
/ sizeof(unsigned long);
374 int identical
= width
;
378 prefetch((void *) front
);
379 prefetch((void *) back
);
381 for (j
= 0; j
< width
; j
++) {
382 if (back
[j
] != front
[j
]) {
388 for (k
= width
- 1; k
> j
; k
--) {
389 if (back
[k
] != front
[k
]) {
395 identical
= start
+ (width
- end
);
396 *bfront
= (u8
*) &front
[start
];
397 *width_bytes
= (end
- start
) * sizeof(unsigned long);
399 return identical
* sizeof(unsigned long);
403 * Render a command stream for an encoded horizontal line segment of pixels.
405 * A command buffer holds several commands.
406 * It always begins with a fresh command header
407 * (the protocol doesn't require this, but we enforce it to allow
408 * multiple buffers to be potentially encoded and sent in parallel).
409 * A single command encodes one contiguous horizontal line of pixels
411 * The function relies on the client to do all allocation, so that
412 * rendering can be done directly to output buffers (e.g. USB URBs).
413 * The function fills the supplied command buffer, providing information
414 * on where it left off, so the client may call in again with additional
415 * buffers if the line will take several buffers to complete.
417 * A single command can transmit a maximum of 256 pixels,
418 * regardless of the compression ratio (protocol design limit).
419 * To the hardware, 0 for a size byte means 256
421 * Rather than 256 pixel commands which are either rl or raw encoded,
422 * the rlx command simply assumes alternating raw and rl spans within one cmd.
423 * This has a slightly larger header overhead, but produces more even results.
424 * It also processes all data (read and write) in a single pass.
425 * Performance benchmarks of common cases show it having just slightly better
426 * compression than 256 pixel raw or rle commands, with similar CPU consumpion.
427 * But for very rl friendly data, will compress not quite as well.
429 static void dlfb_compress_hline(
430 const uint16_t **pixel_start_ptr
,
431 const uint16_t *const pixel_end
,
432 uint32_t *device_address_ptr
,
433 uint8_t **command_buffer_ptr
,
434 const uint8_t *const cmd_buffer_end
)
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 prefetchw((void *) cmd
); /* pull in one cache line at least */
451 *cmd
++ = dev_addr
>> 16;
452 *cmd
++ = dev_addr
>> 8;
455 cmd_pixels_count_byte
= cmd
++; /* we'll know this later */
456 cmd_pixel_start
= pixel
;
458 raw_pixels_count_byte
= cmd
++; /* we'll know this later */
459 raw_pixel_start
= pixel
;
461 cmd_pixel_end
= pixel
+ min3(MAX_CMD_PIXELS
+ 1UL,
462 (unsigned long)(pixel_end
- pixel
),
463 (unsigned long)(cmd_buffer_end
- 1 - cmd
) / BPP
);
465 prefetch_range((void *) pixel
, (u8
*)cmd_pixel_end
- (u8
*)pixel
);
467 while (pixel
< cmd_pixel_end
) {
468 const uint16_t * const repeating_pixel
= pixel
;
470 put_unaligned_be16(*pixel
, cmd
);
474 if (unlikely((pixel
< cmd_pixel_end
) &&
475 (*pixel
== *repeating_pixel
))) {
476 /* go back and fill in raw pixel count */
477 *raw_pixels_count_byte
= ((repeating_pixel
-
478 raw_pixel_start
) + 1) & 0xFF;
480 while ((pixel
< cmd_pixel_end
)
481 && (*pixel
== *repeating_pixel
)) {
485 /* immediately after raw data is repeat byte */
486 *cmd
++ = ((pixel
- repeating_pixel
) - 1) & 0xFF;
488 /* Then start another raw pixel span */
489 raw_pixel_start
= pixel
;
490 raw_pixels_count_byte
= cmd
++;
494 if (pixel
> raw_pixel_start
) {
495 /* finalize last RAW span */
496 *raw_pixels_count_byte
= (pixel
-raw_pixel_start
) & 0xFF;
498 /* undo unused byte */
502 *cmd_pixels_count_byte
= (pixel
- cmd_pixel_start
) & 0xFF;
503 dev_addr
+= (u8
*)pixel
- (u8
*)cmd_pixel_start
;
506 if (cmd_buffer_end
- MIN_RLX_CMD_BYTES
<= cmd
) {
507 /* Fill leftover bytes with no-ops */
508 if (cmd_buffer_end
> cmd
)
509 memset(cmd
, 0xAF, cmd_buffer_end
- cmd
);
510 cmd
= (uint8_t *) cmd_buffer_end
;
513 *command_buffer_ptr
= cmd
;
514 *pixel_start_ptr
= pixel
;
515 *device_address_ptr
= dev_addr
;
519 * There are 3 copies of every pixel: The front buffer that the fbdev
520 * client renders to, the actual framebuffer across the USB bus in hardware
521 * (that we can only write to, slowly, and can never read), and (optionally)
522 * our shadow copy that tracks what's been sent to that hardware buffer.
524 static int dlfb_render_hline(struct dlfb_data
*dlfb
, struct urb
**urb_ptr
,
525 const char *front
, char **urb_buf_ptr
,
526 u32 byte_offset
, u32 byte_width
,
527 int *ident_ptr
, int *sent_ptr
)
529 const u8
*line_start
, *line_end
, *next_pixel
;
530 u32 dev_addr
= dlfb
->base16
+ byte_offset
;
531 struct urb
*urb
= *urb_ptr
;
532 u8
*cmd
= *urb_buf_ptr
;
533 u8
*cmd_end
= (u8
*) urb
->transfer_buffer
+ urb
->transfer_buffer_length
;
535 line_start
= (u8
*) (front
+ byte_offset
);
536 next_pixel
= line_start
;
537 line_end
= next_pixel
+ byte_width
;
539 if (dlfb
->backing_buffer
) {
541 const u8
*back_start
= (u8
*) (dlfb
->backing_buffer
544 *ident_ptr
+= dlfb_trim_hline(back_start
, &next_pixel
,
547 offset
= next_pixel
- line_start
;
548 line_end
= next_pixel
+ byte_width
;
550 back_start
+= offset
;
551 line_start
+= offset
;
553 memcpy((char *)back_start
, (char *) line_start
,
557 while (next_pixel
< line_end
) {
559 dlfb_compress_hline((const uint16_t **) &next_pixel
,
560 (const uint16_t *) line_end
, &dev_addr
,
561 (u8
**) &cmd
, (u8
*) cmd_end
);
563 if (cmd
>= cmd_end
) {
564 int len
= cmd
- (u8
*) urb
->transfer_buffer
;
565 if (dlfb_submit_urb(dlfb
, urb
, len
))
566 return 1; /* lost pixels is set */
568 urb
= dlfb_get_urb(dlfb
);
570 return 1; /* lost_pixels is set */
572 cmd
= urb
->transfer_buffer
;
573 cmd_end
= &cmd
[urb
->transfer_buffer_length
];
582 static int dlfb_handle_damage(struct dlfb_data
*dlfb
, int x
, int y
,
583 int width
, int height
, char *data
)
587 cycles_t start_cycles
, end_cycles
;
589 int bytes_identical
= 0;
593 start_cycles
= get_cycles();
595 aligned_x
= DL_ALIGN_DOWN(x
, sizeof(unsigned long));
596 width
= DL_ALIGN_UP(width
+ (x
-aligned_x
), sizeof(unsigned long));
600 (x
+ width
> dlfb
->info
->var
.xres
) ||
601 (y
+ height
> dlfb
->info
->var
.yres
))
604 if (!atomic_read(&dlfb
->usb_active
))
607 urb
= dlfb_get_urb(dlfb
);
610 cmd
= urb
->transfer_buffer
;
612 for (i
= y
; i
< y
+ height
; i
++) {
613 const int line_offset
= dlfb
->info
->fix
.line_length
* i
;
614 const int byte_offset
= line_offset
+ (x
* BPP
);
616 if (dlfb_render_hline(dlfb
, &urb
,
617 (char *) dlfb
->info
->fix
.smem_start
,
618 &cmd
, byte_offset
, width
* BPP
,
619 &bytes_identical
, &bytes_sent
))
623 if (cmd
> (char *) urb
->transfer_buffer
) {
625 if (cmd
< (char *) urb
->transfer_buffer
+ urb
->transfer_buffer_length
)
627 /* Send partial buffer remaining before exiting */
628 len
= cmd
- (char *) urb
->transfer_buffer
;
629 ret
= dlfb_submit_urb(dlfb
, urb
, len
);
632 dlfb_urb_completion(urb
);
635 atomic_add(bytes_sent
, &dlfb
->bytes_sent
);
636 atomic_add(bytes_identical
, &dlfb
->bytes_identical
);
637 atomic_add(width
*height
*2, &dlfb
->bytes_rendered
);
638 end_cycles
= get_cycles();
639 atomic_add(((unsigned int) ((end_cycles
- start_cycles
)
640 >> 10)), /* Kcycles */
641 &dlfb
->cpu_kcycles_used
);
647 * Path triggered by usermode clients who write to filesystem
648 * e.g. cat filename > /dev/fb1
649 * Not used by X Windows or text-mode console. But useful for testing.
650 * Slow because of extra copy and we must assume all pixels dirty.
652 static ssize_t
dlfb_ops_write(struct fb_info
*info
, const char __user
*buf
,
653 size_t count
, loff_t
*ppos
)
656 struct dlfb_data
*dlfb
= info
->par
;
657 u32 offset
= (u32
) *ppos
;
659 result
= fb_sys_write(info
, buf
, count
, ppos
);
662 int start
= max((int)(offset
/ info
->fix
.line_length
), 0);
663 int lines
= min((u32
)((result
/ info
->fix
.line_length
) + 1),
664 (u32
)info
->var
.yres
);
666 dlfb_handle_damage(dlfb
, 0, start
, info
->var
.xres
,
667 lines
, info
->screen_base
);
673 /* hardware has native COPY command (see libdlo), but not worth it for fbcon */
674 static void dlfb_ops_copyarea(struct fb_info
*info
,
675 const struct fb_copyarea
*area
)
678 struct dlfb_data
*dlfb
= info
->par
;
680 sys_copyarea(info
, area
);
682 dlfb_handle_damage(dlfb
, area
->dx
, area
->dy
,
683 area
->width
, area
->height
, info
->screen_base
);
686 static void dlfb_ops_imageblit(struct fb_info
*info
,
687 const struct fb_image
*image
)
689 struct dlfb_data
*dlfb
= info
->par
;
691 sys_imageblit(info
, image
);
693 dlfb_handle_damage(dlfb
, image
->dx
, image
->dy
,
694 image
->width
, image
->height
, info
->screen_base
);
697 static void dlfb_ops_fillrect(struct fb_info
*info
,
698 const struct fb_fillrect
*rect
)
700 struct dlfb_data
*dlfb
= info
->par
;
702 sys_fillrect(info
, rect
);
704 dlfb_handle_damage(dlfb
, rect
->dx
, rect
->dy
, rect
->width
,
705 rect
->height
, info
->screen_base
);
709 * NOTE: fb_defio.c is holding info->fbdefio.mutex
710 * Touching ANY framebuffer memory that triggers a page fault
711 * in fb_defio will cause a deadlock, when it also tries to
712 * grab the same mutex.
714 static void dlfb_dpy_deferred_io(struct fb_info
*info
,
715 struct list_head
*pagelist
)
718 struct fb_deferred_io
*fbdefio
= info
->fbdefio
;
719 struct dlfb_data
*dlfb
= info
->par
;
722 cycles_t start_cycles
, end_cycles
;
724 int bytes_identical
= 0;
725 int bytes_rendered
= 0;
730 if (!atomic_read(&dlfb
->usb_active
))
733 start_cycles
= get_cycles();
735 urb
= dlfb_get_urb(dlfb
);
739 cmd
= urb
->transfer_buffer
;
741 /* walk the written page list and render each to device */
742 list_for_each_entry(cur
, &fbdefio
->pagelist
, lru
) {
744 if (dlfb_render_hline(dlfb
, &urb
, (char *) info
->fix
.smem_start
,
745 &cmd
, cur
->index
<< PAGE_SHIFT
,
746 PAGE_SIZE
, &bytes_identical
, &bytes_sent
))
748 bytes_rendered
+= PAGE_SIZE
;
751 if (cmd
> (char *) urb
->transfer_buffer
) {
753 if (cmd
< (char *) urb
->transfer_buffer
+ urb
->transfer_buffer_length
)
755 /* Send partial buffer remaining before exiting */
756 len
= cmd
- (char *) urb
->transfer_buffer
;
757 dlfb_submit_urb(dlfb
, urb
, len
);
760 dlfb_urb_completion(urb
);
763 atomic_add(bytes_sent
, &dlfb
->bytes_sent
);
764 atomic_add(bytes_identical
, &dlfb
->bytes_identical
);
765 atomic_add(bytes_rendered
, &dlfb
->bytes_rendered
);
766 end_cycles
= get_cycles();
767 atomic_add(((unsigned int) ((end_cycles
- start_cycles
)
768 >> 10)), /* Kcycles */
769 &dlfb
->cpu_kcycles_used
);
772 static int dlfb_get_edid(struct dlfb_data
*dlfb
, char *edid
, int len
)
777 rbuf
= kmalloc(2, GFP_KERNEL
);
781 for (i
= 0; i
< len
; i
++) {
782 ret
= usb_control_msg(dlfb
->udev
,
783 usb_rcvctrlpipe(dlfb
->udev
, 0), 0x02,
784 (0x80 | (0x02 << 5)), i
<< 8, 0xA1,
785 rbuf
, 2, USB_CTRL_GET_TIMEOUT
);
787 dev_err(&dlfb
->udev
->dev
,
788 "Read EDID byte %d failed: %d\n", i
, ret
);
800 static int dlfb_ops_ioctl(struct fb_info
*info
, unsigned int cmd
,
804 struct dlfb_data
*dlfb
= info
->par
;
806 if (!atomic_read(&dlfb
->usb_active
))
809 /* TODO: Update X server to get this from sysfs instead */
810 if (cmd
== DLFB_IOCTL_RETURN_EDID
) {
811 void __user
*edid
= (void __user
*)arg
;
812 if (copy_to_user(edid
, dlfb
->edid
, dlfb
->edid_size
))
817 /* TODO: Help propose a standard fb.h ioctl to report mmap damage */
818 if (cmd
== DLFB_IOCTL_REPORT_DAMAGE
) {
821 if (copy_from_user(&area
, (void __user
*)arg
,
822 sizeof(struct dloarea
)))
826 * If we have a damage-aware client, turn fb_defio "off"
827 * To avoid perf imact of unnecessary page fault handling.
828 * Done by resetting the delay for this fb_info to a very
829 * long period. Pages will become writable and stay that way.
830 * Reset to normal value when all clients have closed this fb.
833 info
->fbdefio
->delay
= DL_DEFIO_WRITE_DISABLE
;
838 if (area
.x
> info
->var
.xres
)
839 area
.x
= info
->var
.xres
;
844 if (area
.y
> info
->var
.yres
)
845 area
.y
= info
->var
.yres
;
847 dlfb_handle_damage(dlfb
, area
.x
, area
.y
, area
.w
, area
.h
,
854 /* taken from vesafb */
856 dlfb_ops_setcolreg(unsigned regno
, unsigned red
, unsigned green
,
857 unsigned blue
, unsigned transp
, struct fb_info
*info
)
861 if (regno
>= info
->cmap
.len
)
865 if (info
->var
.red
.offset
== 10) {
867 ((u32
*) (info
->pseudo_palette
))[regno
] =
868 ((red
& 0xf800) >> 1) |
869 ((green
& 0xf800) >> 6) | ((blue
& 0xf800) >> 11);
872 ((u32
*) (info
->pseudo_palette
))[regno
] =
874 ((green
& 0xfc00) >> 5) | ((blue
& 0xf800) >> 11);
882 * It's common for several clients to have framebuffer open simultaneously.
883 * e.g. both fbcon and X. Makes things interesting.
884 * Assumes caller is holding info->lock (for open and release at least)
886 static int dlfb_ops_open(struct fb_info
*info
, int user
)
888 struct dlfb_data
*dlfb
= info
->par
;
891 * fbcon aggressively connects to first framebuffer it finds,
892 * preventing other clients (X) from working properly. Usually
893 * not what the user wants. Fail by default with option to enable.
895 if ((user
== 0) && (!console
))
898 /* If the USB device is gone, we don't accept new opens */
899 if (dlfb
->virtualized
)
904 kref_get(&dlfb
->kref
);
906 if (fb_defio
&& (info
->fbdefio
== NULL
)) {
907 /* enable defio at last moment if not disabled by client */
909 struct fb_deferred_io
*fbdefio
;
911 fbdefio
= kzalloc(sizeof(struct fb_deferred_io
), GFP_KERNEL
);
914 fbdefio
->delay
= DL_DEFIO_WRITE_DELAY
;
915 fbdefio
->deferred_io
= dlfb_dpy_deferred_io
;
918 info
->fbdefio
= fbdefio
;
919 fb_deferred_io_init(info
);
922 dev_dbg(info
->dev
, "open, user=%d fb_info=%p count=%d\n",
923 user
, info
, dlfb
->fb_count
);
929 * Called when all client interfaces to start transactions have been disabled,
930 * and all references to our device instance (dlfb_data) are released.
931 * Every transaction must have a reference, so we know are fully spun down
933 static void dlfb_free(struct kref
*kref
)
935 struct dlfb_data
*dlfb
= container_of(kref
, struct dlfb_data
, kref
);
937 while (!list_empty(&dlfb
->deferred_free
)) {
938 struct dlfb_deferred_free
*d
= list_entry(dlfb
->deferred_free
.next
, struct dlfb_deferred_free
, list
);
943 vfree(dlfb
->backing_buffer
);
948 static void dlfb_free_framebuffer(struct dlfb_data
*dlfb
)
950 struct fb_info
*info
= dlfb
->info
;
953 unregister_framebuffer(info
);
955 if (info
->cmap
.len
!= 0)
956 fb_dealloc_cmap(&info
->cmap
);
957 if (info
->monspecs
.modedb
)
958 fb_destroy_modedb(info
->monspecs
.modedb
);
959 vfree(info
->screen_base
);
961 fb_destroy_modelist(&info
->modelist
);
965 /* Assume info structure is freed after this point */
966 framebuffer_release(info
);
969 /* ref taken in probe() as part of registering framebfufer */
970 kref_put(&dlfb
->kref
, dlfb_free
);
973 static void dlfb_free_framebuffer_work(struct work_struct
*work
)
975 struct dlfb_data
*dlfb
= container_of(work
, struct dlfb_data
,
976 free_framebuffer_work
.work
);
977 dlfb_free_framebuffer(dlfb
);
980 * Assumes caller is holding info->lock mutex (for open and release at least)
982 static int dlfb_ops_release(struct fb_info
*info
, int user
)
984 struct dlfb_data
*dlfb
= info
->par
;
988 /* We can't free fb_info here - fbmem will touch it when we return */
989 if (dlfb
->virtualized
&& (dlfb
->fb_count
== 0))
990 schedule_delayed_work(&dlfb
->free_framebuffer_work
, HZ
);
992 if ((dlfb
->fb_count
== 0) && (info
->fbdefio
)) {
993 fb_deferred_io_cleanup(info
);
994 kfree(info
->fbdefio
);
995 info
->fbdefio
= NULL
;
996 info
->fbops
->fb_mmap
= dlfb_ops_mmap
;
999 dev_dbg(info
->dev
, "release, user=%d count=%d\n", user
, dlfb
->fb_count
);
1001 kref_put(&dlfb
->kref
, dlfb_free
);
1007 * Check whether a video mode is supported by the DisplayLink chip
1008 * We start from monitor's modes, so don't need to filter that here
1010 static int dlfb_is_valid_mode(struct fb_videomode
*mode
, struct dlfb_data
*dlfb
)
1012 if (mode
->xres
* mode
->yres
> dlfb
->sku_pixel_limit
)
1018 static void dlfb_var_color_format(struct fb_var_screeninfo
*var
)
1020 const struct fb_bitfield red
= { 11, 5, 0 };
1021 const struct fb_bitfield green
= { 5, 6, 0 };
1022 const struct fb_bitfield blue
= { 0, 5, 0 };
1024 var
->bits_per_pixel
= 16;
1030 static int dlfb_ops_check_var(struct fb_var_screeninfo
*var
,
1031 struct fb_info
*info
)
1033 struct fb_videomode mode
;
1034 struct dlfb_data
*dlfb
= info
->par
;
1036 /* set device-specific elements of var unrelated to mode */
1037 dlfb_var_color_format(var
);
1039 fb_var_to_videomode(&mode
, var
);
1041 if (!dlfb_is_valid_mode(&mode
, dlfb
))
1047 static int dlfb_ops_set_par(struct fb_info
*info
)
1049 struct dlfb_data
*dlfb
= info
->par
;
1051 u16
*pix_framebuffer
;
1053 struct fb_var_screeninfo fvs
;
1054 u32 line_length
= info
->var
.xres
* (info
->var
.bits_per_pixel
/ 8);
1056 /* clear the activate field because it causes spurious miscompares */
1059 fvs
.vmode
&= ~FB_VMODE_SMOOTH_XPAN
;
1061 if (!memcmp(&dlfb
->current_mode
, &fvs
, sizeof(struct fb_var_screeninfo
)))
1064 result
= dlfb_realloc_framebuffer(dlfb
, info
, info
->var
.yres
* line_length
);
1068 result
= dlfb_set_video_mode(dlfb
, &info
->var
);
1073 dlfb
->current_mode
= fvs
;
1074 info
->fix
.line_length
= line_length
;
1076 if (dlfb
->fb_count
== 0) {
1078 /* paint greenscreen */
1080 pix_framebuffer
= (u16
*) info
->screen_base
;
1081 for (i
= 0; i
< info
->fix
.smem_len
/ 2; i
++)
1082 pix_framebuffer
[i
] = 0x37e6;
1085 dlfb_handle_damage(dlfb
, 0, 0, info
->var
.xres
, info
->var
.yres
,
1091 /* To fonzi the jukebox (e.g. make blanking changes take effect) */
1092 static char *dlfb_dummy_render(char *buf
)
1095 *buf
++ = 0x6A; /* copy */
1096 *buf
++ = 0x00; /* from address*/
1099 *buf
++ = 0x01; /* one pixel */
1100 *buf
++ = 0x00; /* to address */
1107 * In order to come back from full DPMS off, we need to set the mode again
1109 static int dlfb_ops_blank(int blank_mode
, struct fb_info
*info
)
1111 struct dlfb_data
*dlfb
= info
->par
;
1115 dev_dbg(info
->dev
, "blank, mode %d --> %d\n",
1116 dlfb
->blank_mode
, blank_mode
);
1118 if ((dlfb
->blank_mode
== FB_BLANK_POWERDOWN
) &&
1119 (blank_mode
!= FB_BLANK_POWERDOWN
)) {
1121 /* returning from powerdown requires a fresh modeset */
1122 dlfb_set_video_mode(dlfb
, &info
->var
);
1125 urb
= dlfb_get_urb(dlfb
);
1129 bufptr
= (char *) urb
->transfer_buffer
;
1130 bufptr
= dlfb_vidreg_lock(bufptr
);
1131 bufptr
= dlfb_blanking(bufptr
, blank_mode
);
1132 bufptr
= dlfb_vidreg_unlock(bufptr
);
1134 /* seems like a render op is needed to have blank change take effect */
1135 bufptr
= dlfb_dummy_render(bufptr
);
1137 dlfb_submit_urb(dlfb
, urb
, bufptr
-
1138 (char *) urb
->transfer_buffer
);
1140 dlfb
->blank_mode
= blank_mode
;
1145 static struct fb_ops dlfb_ops
= {
1146 .owner
= THIS_MODULE
,
1147 .fb_read
= fb_sys_read
,
1148 .fb_write
= dlfb_ops_write
,
1149 .fb_setcolreg
= dlfb_ops_setcolreg
,
1150 .fb_fillrect
= dlfb_ops_fillrect
,
1151 .fb_copyarea
= dlfb_ops_copyarea
,
1152 .fb_imageblit
= dlfb_ops_imageblit
,
1153 .fb_mmap
= dlfb_ops_mmap
,
1154 .fb_ioctl
= dlfb_ops_ioctl
,
1155 .fb_open
= dlfb_ops_open
,
1156 .fb_release
= dlfb_ops_release
,
1157 .fb_blank
= dlfb_ops_blank
,
1158 .fb_check_var
= dlfb_ops_check_var
,
1159 .fb_set_par
= dlfb_ops_set_par
,
1163 static void dlfb_deferred_vfree(struct dlfb_data
*dlfb
, void *mem
)
1165 struct dlfb_deferred_free
*d
= kmalloc(sizeof(struct dlfb_deferred_free
), GFP_KERNEL
);
1169 list_add(&d
->list
, &dlfb
->deferred_free
);
1173 * Assumes &info->lock held by caller
1174 * Assumes no active clients have framebuffer open
1176 static int dlfb_realloc_framebuffer(struct dlfb_data
*dlfb
, struct fb_info
*info
, u32 new_len
)
1178 u32 old_len
= info
->fix
.smem_len
;
1179 const void *old_fb
= (const void __force
*)info
->screen_base
;
1180 unsigned char *new_fb
;
1181 unsigned char *new_back
= NULL
;
1183 new_len
= PAGE_ALIGN(new_len
);
1185 if (new_len
> old_len
) {
1187 * Alloc system memory for virtual framebuffer
1189 new_fb
= vmalloc(new_len
);
1191 dev_err(info
->dev
, "Virtual framebuffer alloc failed\n");
1194 memset(new_fb
, 0xff, new_len
);
1196 if (info
->screen_base
) {
1197 memcpy(new_fb
, old_fb
, old_len
);
1198 dlfb_deferred_vfree(dlfb
, (void __force
*)info
->screen_base
);
1201 info
->screen_base
= (char __iomem
*)new_fb
;
1202 info
->fix
.smem_len
= new_len
;
1203 info
->fix
.smem_start
= (unsigned long) new_fb
;
1204 info
->flags
= udlfb_info_flags
;
1207 * Second framebuffer copy to mirror the framebuffer state
1208 * on the physical USB device. We can function without this.
1209 * But with imperfect damage info we may send pixels over USB
1210 * that were, in fact, unchanged - wasting limited USB bandwidth
1213 new_back
= vzalloc(new_len
);
1216 "No shadow/backing buffer allocated\n");
1218 dlfb_deferred_vfree(dlfb
, dlfb
->backing_buffer
);
1219 dlfb
->backing_buffer
= new_back
;
1226 * 1) Get EDID from hw, or use sw default
1227 * 2) Parse into various fb_info structs
1228 * 3) Allocate virtual framebuffer memory to back highest res mode
1230 * Parses EDID into three places used by various parts of fbdev:
1231 * fb_var_screeninfo contains the timing of the monitor's preferred mode
1232 * fb_info.monspecs is full parsed EDID info, including monspecs.modedb
1233 * fb_info.modelist is a linked list of all monitor & VESA modes which work
1235 * If EDID is not readable/valid, then modelist is all VESA modes,
1236 * monspecs is NULL, and fb_var_screeninfo is set to safe VESA mode
1237 * Returns 0 if successful
1239 static int dlfb_setup_modes(struct dlfb_data
*dlfb
,
1240 struct fb_info
*info
,
1241 char *default_edid
, size_t default_edid_size
)
1244 int i
, result
= 0, tries
= 3;
1245 struct device
*dev
= info
->device
;
1246 struct fb_videomode
*mode
;
1247 const struct fb_videomode
*default_vmode
= NULL
;
1250 /* only use mutex if info has been registered */
1251 mutex_lock(&info
->lock
);
1252 /* parent device is used otherwise */
1256 edid
= kmalloc(EDID_LENGTH
, GFP_KERNEL
);
1262 fb_destroy_modelist(&info
->modelist
);
1263 memset(&info
->monspecs
, 0, sizeof(info
->monspecs
));
1266 * Try to (re)read EDID from hardware first
1267 * EDID data may return, but not parse as valid
1268 * Try again a few times, in case of e.g. analog cable noise
1272 i
= dlfb_get_edid(dlfb
, edid
, EDID_LENGTH
);
1274 if (i
>= EDID_LENGTH
)
1275 fb_edid_to_monspecs(edid
, &info
->monspecs
);
1277 if (info
->monspecs
.modedb_len
> 0) {
1279 dlfb
->edid_size
= i
;
1284 /* If that fails, use a previously returned EDID if available */
1285 if (info
->monspecs
.modedb_len
== 0) {
1286 dev_err(dev
, "Unable to get valid EDID from device/display\n");
1289 fb_edid_to_monspecs(dlfb
->edid
, &info
->monspecs
);
1290 if (info
->monspecs
.modedb_len
> 0)
1291 dev_err(dev
, "Using previously queried EDID\n");
1295 /* If that fails, use the default EDID we were handed */
1296 if (info
->monspecs
.modedb_len
== 0) {
1297 if (default_edid_size
>= EDID_LENGTH
) {
1298 fb_edid_to_monspecs(default_edid
, &info
->monspecs
);
1299 if (info
->monspecs
.modedb_len
> 0) {
1300 memcpy(edid
, default_edid
, default_edid_size
);
1302 dlfb
->edid_size
= default_edid_size
;
1303 dev_err(dev
, "Using default/backup EDID\n");
1308 /* If we've got modes, let's pick a best default mode */
1309 if (info
->monspecs
.modedb_len
> 0) {
1311 for (i
= 0; i
< info
->monspecs
.modedb_len
; i
++) {
1312 mode
= &info
->monspecs
.modedb
[i
];
1313 if (dlfb_is_valid_mode(mode
, dlfb
)) {
1314 fb_add_videomode(mode
, &info
->modelist
);
1316 dev_dbg(dev
, "Specified mode %dx%d too big\n",
1317 mode
->xres
, mode
->yres
);
1319 /* if we've removed top/best mode */
1321 &= ~FB_MISC_1ST_DETAIL
;
1325 default_vmode
= fb_find_best_display(&info
->monspecs
,
1329 /* If everything else has failed, fall back to safe default mode */
1330 if (default_vmode
== NULL
) {
1332 struct fb_videomode fb_vmode
= {0};
1335 * Add the standard VESA modes to our modelist
1336 * Since we don't have EDID, there may be modes that
1337 * overspec monitor and/or are incorrect aspect ratio, etc.
1338 * But at least the user has a chance to choose
1340 for (i
= 0; i
< VESA_MODEDB_SIZE
; i
++) {
1341 mode
= (struct fb_videomode
*)&vesa_modes
[i
];
1342 if (dlfb_is_valid_mode(mode
, dlfb
))
1343 fb_add_videomode(mode
, &info
->modelist
);
1345 dev_dbg(dev
, "VESA mode %dx%d too big\n",
1346 mode
->xres
, mode
->yres
);
1350 * default to resolution safe for projectors
1351 * (since they are most common case without EDID)
1353 fb_vmode
.xres
= 800;
1354 fb_vmode
.yres
= 600;
1355 fb_vmode
.refresh
= 60;
1356 default_vmode
= fb_find_nearest_mode(&fb_vmode
,
1360 /* If we have good mode and no active clients*/
1361 if ((default_vmode
!= NULL
) && (dlfb
->fb_count
== 0)) {
1363 fb_videomode_to_var(&info
->var
, default_vmode
);
1364 dlfb_var_color_format(&info
->var
);
1367 * with mode size info, we can now alloc our framebuffer.
1369 memcpy(&info
->fix
, &dlfb_fix
, sizeof(dlfb_fix
));
1374 if (edid
&& (dlfb
->edid
!= edid
))
1378 mutex_unlock(&info
->lock
);
1383 static ssize_t
metrics_bytes_rendered_show(struct device
*fbdev
,
1384 struct device_attribute
*a
, char *buf
) {
1385 struct fb_info
*fb_info
= dev_get_drvdata(fbdev
);
1386 struct dlfb_data
*dlfb
= fb_info
->par
;
1387 return snprintf(buf
, PAGE_SIZE
, "%u\n",
1388 atomic_read(&dlfb
->bytes_rendered
));
1391 static ssize_t
metrics_bytes_identical_show(struct device
*fbdev
,
1392 struct device_attribute
*a
, char *buf
) {
1393 struct fb_info
*fb_info
= dev_get_drvdata(fbdev
);
1394 struct dlfb_data
*dlfb
= fb_info
->par
;
1395 return snprintf(buf
, PAGE_SIZE
, "%u\n",
1396 atomic_read(&dlfb
->bytes_identical
));
1399 static ssize_t
metrics_bytes_sent_show(struct device
*fbdev
,
1400 struct device_attribute
*a
, char *buf
) {
1401 struct fb_info
*fb_info
= dev_get_drvdata(fbdev
);
1402 struct dlfb_data
*dlfb
= fb_info
->par
;
1403 return snprintf(buf
, PAGE_SIZE
, "%u\n",
1404 atomic_read(&dlfb
->bytes_sent
));
1407 static ssize_t
metrics_cpu_kcycles_used_show(struct device
*fbdev
,
1408 struct device_attribute
*a
, char *buf
) {
1409 struct fb_info
*fb_info
= dev_get_drvdata(fbdev
);
1410 struct dlfb_data
*dlfb
= fb_info
->par
;
1411 return snprintf(buf
, PAGE_SIZE
, "%u\n",
1412 atomic_read(&dlfb
->cpu_kcycles_used
));
1415 static ssize_t
edid_show(
1417 struct kobject
*kobj
, struct bin_attribute
*a
,
1418 char *buf
, loff_t off
, size_t count
) {
1419 struct device
*fbdev
= container_of(kobj
, struct device
, kobj
);
1420 struct fb_info
*fb_info
= dev_get_drvdata(fbdev
);
1421 struct dlfb_data
*dlfb
= fb_info
->par
;
1423 if (dlfb
->edid
== NULL
)
1426 if ((off
>= dlfb
->edid_size
) || (count
> dlfb
->edid_size
))
1429 if (off
+ count
> dlfb
->edid_size
)
1430 count
= dlfb
->edid_size
- off
;
1432 memcpy(buf
, dlfb
->edid
, count
);
1437 static ssize_t
edid_store(
1439 struct kobject
*kobj
, struct bin_attribute
*a
,
1440 char *src
, loff_t src_off
, size_t src_size
) {
1441 struct device
*fbdev
= container_of(kobj
, struct device
, kobj
);
1442 struct fb_info
*fb_info
= dev_get_drvdata(fbdev
);
1443 struct dlfb_data
*dlfb
= fb_info
->par
;
1446 /* We only support write of entire EDID at once, no offset*/
1447 if ((src_size
!= EDID_LENGTH
) || (src_off
!= 0))
1450 ret
= dlfb_setup_modes(dlfb
, fb_info
, src
, src_size
);
1454 if (!dlfb
->edid
|| memcmp(src
, dlfb
->edid
, src_size
))
1457 ret
= dlfb_ops_set_par(fb_info
);
1464 static ssize_t
metrics_reset_store(struct device
*fbdev
,
1465 struct device_attribute
*attr
,
1466 const char *buf
, size_t count
)
1468 struct fb_info
*fb_info
= dev_get_drvdata(fbdev
);
1469 struct dlfb_data
*dlfb
= fb_info
->par
;
1471 atomic_set(&dlfb
->bytes_rendered
, 0);
1472 atomic_set(&dlfb
->bytes_identical
, 0);
1473 atomic_set(&dlfb
->bytes_sent
, 0);
1474 atomic_set(&dlfb
->cpu_kcycles_used
, 0);
1479 static const struct bin_attribute edid_attr
= {
1480 .attr
.name
= "edid",
1482 .size
= EDID_LENGTH
,
1487 static const struct device_attribute fb_device_attrs
[] = {
1488 __ATTR_RO(metrics_bytes_rendered
),
1489 __ATTR_RO(metrics_bytes_identical
),
1490 __ATTR_RO(metrics_bytes_sent
),
1491 __ATTR_RO(metrics_cpu_kcycles_used
),
1492 __ATTR(metrics_reset
, S_IWUSR
, NULL
, metrics_reset_store
),
1496 * This is necessary before we can communicate with the display controller.
1498 static int dlfb_select_std_channel(struct dlfb_data
*dlfb
)
1502 static const u8 set_def_chn
[] = {
1503 0x57, 0xCD, 0xDC, 0xA7,
1504 0x1C, 0x88, 0x5E, 0x15,
1505 0x60, 0xFE, 0xC6, 0x97,
1506 0x16, 0x3D, 0x47, 0xF2 };
1508 buf
= kmemdup(set_def_chn
, sizeof(set_def_chn
), GFP_KERNEL
);
1513 ret
= usb_control_msg(dlfb
->udev
, usb_sndctrlpipe(dlfb
->udev
, 0),
1514 NR_USB_REQUEST_CHANNEL
,
1515 (USB_DIR_OUT
| USB_TYPE_VENDOR
), 0, 0,
1516 buf
, sizeof(set_def_chn
), USB_CTRL_SET_TIMEOUT
);
1523 static int dlfb_parse_vendor_descriptor(struct dlfb_data
*dlfb
,
1524 struct usb_interface
*intf
)
1531 buf
= kzalloc(MAX_VENDOR_DESCRIPTOR_SIZE
, GFP_KERNEL
);
1536 total_len
= usb_get_descriptor(interface_to_usbdev(intf
),
1537 0x5f, /* vendor specific */
1538 0, desc
, MAX_VENDOR_DESCRIPTOR_SIZE
);
1540 /* if not found, look in configuration descriptor */
1541 if (total_len
< 0) {
1542 if (0 == usb_get_extra_descriptor(intf
->cur_altsetting
,
1544 total_len
= (int) desc
[0];
1547 if (total_len
> 5) {
1548 dev_info(&intf
->dev
,
1549 "vendor descriptor length: %d data: %11ph\n",
1552 if ((desc
[0] != total_len
) || /* descriptor length */
1553 (desc
[1] != 0x5f) || /* vendor descriptor type */
1554 (desc
[2] != 0x01) || /* version (2 bytes) */
1555 (desc
[3] != 0x00) ||
1556 (desc
[4] != total_len
- 2)) /* length after type */
1559 desc_end
= desc
+ total_len
;
1560 desc
+= 5; /* the fixed header we've already parsed */
1562 while (desc
< desc_end
) {
1567 key
|= (u16
)*desc
++ << 8;
1571 case 0x0200: { /* max_area */
1572 u32 max_area
= *desc
++;
1573 max_area
|= (u32
)*desc
++ << 8;
1574 max_area
|= (u32
)*desc
++ << 16;
1575 max_area
|= (u32
)*desc
++ << 24;
1576 dev_warn(&intf
->dev
,
1577 "DL chip limited to %d pixel modes\n",
1579 dlfb
->sku_pixel_limit
= max_area
;
1588 dev_info(&intf
->dev
, "vendor descriptor not available (%d)\n",
1595 /* allow udlfb to load for now even if firmware unrecognized */
1596 dev_err(&intf
->dev
, "Unrecognized vendor firmware descriptor\n");
1603 static void dlfb_init_framebuffer_work(struct work_struct
*work
);
1605 static int dlfb_usb_probe(struct usb_interface
*intf
,
1606 const struct usb_device_id
*id
)
1608 struct dlfb_data
*dlfb
;
1609 int retval
= -ENOMEM
;
1610 struct usb_device
*usbdev
= interface_to_usbdev(intf
);
1612 /* usb initialization */
1613 dlfb
= kzalloc(sizeof(*dlfb
), GFP_KERNEL
);
1615 dev_err(&intf
->dev
, "%s: failed to allocate dlfb\n", __func__
);
1619 kref_init(&dlfb
->kref
); /* matching kref_put in usb .disconnect fn */
1620 INIT_LIST_HEAD(&dlfb
->deferred_free
);
1622 dlfb
->udev
= usbdev
;
1623 usb_set_intfdata(intf
, dlfb
);
1625 dev_dbg(&intf
->dev
, "console enable=%d\n", console
);
1626 dev_dbg(&intf
->dev
, "fb_defio enable=%d\n", fb_defio
);
1627 dev_dbg(&intf
->dev
, "shadow enable=%d\n", shadow
);
1629 dlfb
->sku_pixel_limit
= 2048 * 1152; /* default to maximum */
1631 if (!dlfb_parse_vendor_descriptor(dlfb
, intf
)) {
1633 "firmware not recognized, incompatible device?\n");
1638 dev_warn(&intf
->dev
,
1639 "DL chip limit of %d overridden to %d\n",
1640 dlfb
->sku_pixel_limit
, pixel_limit
);
1641 dlfb
->sku_pixel_limit
= pixel_limit
;
1645 if (!dlfb_alloc_urb_list(dlfb
, WRITES_IN_FLIGHT
, MAX_TRANSFER
)) {
1647 dev_err(&intf
->dev
, "unable to allocate urb list\n");
1651 kref_get(&dlfb
->kref
); /* matching kref_put in free_framebuffer_work */
1653 /* We don't register a new USB class. Our client interface is dlfbev */
1655 /* Workitem keep things fast & simple during USB enumeration */
1656 INIT_DELAYED_WORK(&dlfb
->init_framebuffer_work
,
1657 dlfb_init_framebuffer_work
);
1658 schedule_delayed_work(&dlfb
->init_framebuffer_work
, 0);
1665 kref_put(&dlfb
->kref
, dlfb_free
); /* last ref from kref_init */
1667 /* dev has been deallocated. Do not dereference */
1673 static void dlfb_init_framebuffer_work(struct work_struct
*work
)
1676 struct fb_info
*info
;
1677 const struct device_attribute
*attr
;
1678 struct dlfb_data
*dlfb
= container_of(work
, struct dlfb_data
,
1679 init_framebuffer_work
.work
);
1681 /* allocates framebuffer driver structure, not framebuffer memory */
1682 info
= framebuffer_alloc(0, &dlfb
->udev
->dev
);
1684 dev_err(&dlfb
->udev
->dev
, "framebuffer_alloc failed\n");
1690 info
->pseudo_palette
= dlfb
->pseudo_palette
;
1691 dlfb
->ops
= dlfb_ops
;
1692 info
->fbops
= &dlfb
->ops
;
1694 retval
= fb_alloc_cmap(&info
->cmap
, 256, 0);
1696 dev_err(info
->device
, "cmap allocation failed: %d\n", retval
);
1700 INIT_DELAYED_WORK(&dlfb
->free_framebuffer_work
,
1701 dlfb_free_framebuffer_work
);
1703 INIT_LIST_HEAD(&info
->modelist
);
1705 retval
= dlfb_setup_modes(dlfb
, info
, NULL
, 0);
1707 dev_err(info
->device
,
1708 "unable to find common mode for display and adapter\n");
1712 /* ready to begin using device */
1714 atomic_set(&dlfb
->usb_active
, 1);
1715 dlfb_select_std_channel(dlfb
);
1717 dlfb_ops_check_var(&info
->var
, info
);
1718 retval
= dlfb_ops_set_par(info
);
1722 retval
= register_framebuffer(info
);
1724 dev_err(info
->device
, "unable to register framebuffer: %d\n",
1729 for (i
= 0; i
< ARRAY_SIZE(fb_device_attrs
); i
++) {
1730 attr
= &fb_device_attrs
[i
];
1731 retval
= device_create_file(info
->dev
, attr
);
1733 dev_warn(info
->device
,
1734 "failed to create '%s' attribute: %d\n",
1735 attr
->attr
.name
, retval
);
1738 retval
= device_create_bin_file(info
->dev
, &edid_attr
);
1740 dev_warn(info
->device
, "failed to create '%s' attribute: %d\n",
1741 edid_attr
.attr
.name
, retval
);
1743 dev_info(info
->device
,
1744 "%s is DisplayLink USB device (%dx%d, %dK framebuffer memory)\n",
1745 dev_name(info
->dev
), info
->var
.xres
, info
->var
.yres
,
1746 ((dlfb
->backing_buffer
) ?
1747 info
->fix
.smem_len
* 2 : info
->fix
.smem_len
) >> 10);
1751 dlfb_free_framebuffer(dlfb
);
1754 static void dlfb_usb_disconnect(struct usb_interface
*intf
)
1756 struct dlfb_data
*dlfb
;
1757 struct fb_info
*info
;
1760 dlfb
= usb_get_intfdata(intf
);
1763 dev_dbg(&intf
->dev
, "USB disconnect starting\n");
1765 /* we virtualize until all fb clients release. Then we free */
1766 dlfb
->virtualized
= true;
1768 /* When non-active we'll update virtual framebuffer, but no new urbs */
1769 atomic_set(&dlfb
->usb_active
, 0);
1771 /* this function will wait for all in-flight urbs to complete */
1772 dlfb_free_urb_list(dlfb
);
1775 /* remove udlfb's sysfs interfaces */
1776 for (i
= 0; i
< ARRAY_SIZE(fb_device_attrs
); i
++)
1777 device_remove_file(info
->dev
, &fb_device_attrs
[i
]);
1778 device_remove_bin_file(info
->dev
, &edid_attr
);
1779 unlink_framebuffer(info
);
1782 usb_set_intfdata(intf
, NULL
);
1785 /* if clients still have us open, will be freed on last close */
1786 if (dlfb
->fb_count
== 0)
1787 schedule_delayed_work(&dlfb
->free_framebuffer_work
, 0);
1789 /* release reference taken by kref_init in probe() */
1790 kref_put(&dlfb
->kref
, dlfb_free
);
1792 /* consider dlfb_data freed */
1795 static struct usb_driver dlfb_driver
= {
1797 .probe
= dlfb_usb_probe
,
1798 .disconnect
= dlfb_usb_disconnect
,
1799 .id_table
= id_table
,
1802 module_usb_driver(dlfb_driver
);
1804 static void dlfb_urb_completion(struct urb
*urb
)
1806 struct urb_node
*unode
= urb
->context
;
1807 struct dlfb_data
*dlfb
= unode
->dlfb
;
1808 unsigned long flags
;
1810 switch (urb
->status
) {
1817 /* sync/async unlink faults aren't errors */
1820 dev_err(&dlfb
->udev
->dev
,
1821 "%s - nonzero write bulk status received: %d\n",
1822 __func__
, urb
->status
);
1823 atomic_set(&dlfb
->lost_pixels
, 1);
1827 urb
->transfer_buffer_length
= dlfb
->urbs
.size
; /* reset to actual */
1829 spin_lock_irqsave(&dlfb
->urbs
.lock
, flags
);
1830 list_add_tail(&unode
->entry
, &dlfb
->urbs
.list
);
1831 dlfb
->urbs
.available
++;
1832 spin_unlock_irqrestore(&dlfb
->urbs
.lock
, flags
);
1834 up(&dlfb
->urbs
.limit_sem
);
1837 static void dlfb_free_urb_list(struct dlfb_data
*dlfb
)
1839 int count
= dlfb
->urbs
.count
;
1840 struct list_head
*node
;
1841 struct urb_node
*unode
;
1843 unsigned long flags
;
1845 /* keep waiting and freeing, until we've got 'em all */
1847 down(&dlfb
->urbs
.limit_sem
);
1849 spin_lock_irqsave(&dlfb
->urbs
.lock
, flags
);
1851 node
= dlfb
->urbs
.list
.next
; /* have reserved one with sem */
1852 list_del_init(node
);
1854 spin_unlock_irqrestore(&dlfb
->urbs
.lock
, flags
);
1856 unode
= list_entry(node
, struct urb_node
, entry
);
1859 /* Free each separately allocated piece */
1860 usb_free_coherent(urb
->dev
, dlfb
->urbs
.size
,
1861 urb
->transfer_buffer
, urb
->transfer_dma
);
1866 dlfb
->urbs
.count
= 0;
1869 static int dlfb_alloc_urb_list(struct dlfb_data
*dlfb
, int count
, size_t size
)
1872 struct urb_node
*unode
;
1874 size_t wanted_size
= count
* size
;
1876 spin_lock_init(&dlfb
->urbs
.lock
);
1879 dlfb
->urbs
.size
= size
;
1880 INIT_LIST_HEAD(&dlfb
->urbs
.list
);
1882 sema_init(&dlfb
->urbs
.limit_sem
, 0);
1883 dlfb
->urbs
.count
= 0;
1884 dlfb
->urbs
.available
= 0;
1886 while (dlfb
->urbs
.count
* size
< wanted_size
) {
1887 unode
= kzalloc(sizeof(*unode
), GFP_KERNEL
);
1892 urb
= usb_alloc_urb(0, GFP_KERNEL
);
1899 buf
= usb_alloc_coherent(dlfb
->udev
, size
, GFP_KERNEL
,
1900 &urb
->transfer_dma
);
1904 if (size
> PAGE_SIZE
) {
1906 dlfb_free_urb_list(dlfb
);
1912 /* urb->transfer_buffer_length set to actual before submit */
1913 usb_fill_bulk_urb(urb
, dlfb
->udev
, usb_sndbulkpipe(dlfb
->udev
, 1),
1914 buf
, size
, dlfb_urb_completion
, unode
);
1915 urb
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
1917 list_add_tail(&unode
->entry
, &dlfb
->urbs
.list
);
1919 up(&dlfb
->urbs
.limit_sem
);
1921 dlfb
->urbs
.available
++;
1924 return dlfb
->urbs
.count
;
1927 static struct urb
*dlfb_get_urb(struct dlfb_data
*dlfb
)
1930 struct list_head
*entry
;
1931 struct urb_node
*unode
;
1932 unsigned long flags
;
1934 /* Wait for an in-flight buffer to complete and get re-queued */
1935 ret
= down_timeout(&dlfb
->urbs
.limit_sem
, GET_URB_TIMEOUT
);
1937 atomic_set(&dlfb
->lost_pixels
, 1);
1938 dev_warn(&dlfb
->udev
->dev
,
1939 "wait for urb interrupted: %d available: %d\n",
1940 ret
, dlfb
->urbs
.available
);
1944 spin_lock_irqsave(&dlfb
->urbs
.lock
, flags
);
1946 BUG_ON(list_empty(&dlfb
->urbs
.list
)); /* reserved one with limit_sem */
1947 entry
= dlfb
->urbs
.list
.next
;
1948 list_del_init(entry
);
1949 dlfb
->urbs
.available
--;
1951 spin_unlock_irqrestore(&dlfb
->urbs
.lock
, flags
);
1953 unode
= list_entry(entry
, struct urb_node
, entry
);
1957 static int dlfb_submit_urb(struct dlfb_data
*dlfb
, struct urb
*urb
, size_t len
)
1961 BUG_ON(len
> dlfb
->urbs
.size
);
1963 urb
->transfer_buffer_length
= len
; /* set to actual payload len */
1964 ret
= usb_submit_urb(urb
, GFP_KERNEL
);
1966 dlfb_urb_completion(urb
); /* because no one else will */
1967 atomic_set(&dlfb
->lost_pixels
, 1);
1968 dev_err(&dlfb
->udev
->dev
, "submit urb error: %d\n", ret
);
1973 module_param(console
, bool, S_IWUSR
| S_IRUSR
| S_IWGRP
| S_IRGRP
);
1974 MODULE_PARM_DESC(console
, "Allow fbcon to open framebuffer");
1976 module_param(fb_defio
, bool, S_IWUSR
| S_IRUSR
| S_IWGRP
| S_IRGRP
);
1977 MODULE_PARM_DESC(fb_defio
, "Page fault detection of mmap writes");
1979 module_param(shadow
, bool, S_IWUSR
| S_IRUSR
| S_IWGRP
| S_IRGRP
);
1980 MODULE_PARM_DESC(shadow
, "Shadow vid mem. Disable to save mem but lose perf");
1982 module_param(pixel_limit
, int, S_IWUSR
| S_IRUSR
| S_IWGRP
| S_IRGRP
);
1983 MODULE_PARM_DESC(pixel_limit
, "Force limit on max mode (in x*y pixels)");
1985 MODULE_AUTHOR("Roberto De Ioris <roberto@unbit.it>, "
1986 "Jaya Kumar <jayakumar.lkml@gmail.com>, "
1987 "Bernie Thompson <bernie@plugable.com>");
1988 MODULE_DESCRIPTION("DisplayLink kernel framebuffer driver");
1989 MODULE_LICENSE("GPL");