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 products, all with unique PIDs. We are able
52 * to support all volume ones (circa 2009) with a single driver, so we match
53 * globally on VID. TODO: Probe() needs to detect when we might be running
54 * "future" chips, and bail on those, so a compatible driver can match.
56 static struct usb_device_id id_table
[] = {
57 {.idVendor
= 0x17e9, .match_flags
= USB_DEVICE_ID_MATCH_VENDOR
,},
60 MODULE_DEVICE_TABLE(usb
, id_table
);
63 static int console
; /* Optionally allow fbcon to consume first framebuffer */
64 static int fb_defio
= 1; /* Detect mmap writes using page faults */
65 static int shadow
= 1; /* Optionally disable shadow framebuffer */
67 /* dlfb keeps a list of urbs for efficient bulk transfers */
68 static void dlfb_urb_completion(struct urb
*urb
);
69 static struct urb
*dlfb_get_urb(struct dlfb_data
*dev
);
70 static int dlfb_submit_urb(struct dlfb_data
*dev
, struct urb
* urb
, size_t len
);
71 static int dlfb_alloc_urb_list(struct dlfb_data
*dev
, int count
, size_t size
);
72 static void dlfb_free_urb_list(struct dlfb_data
*dev
);
75 * All DisplayLink bulk operations start with 0xAF, followed by specific code
76 * All operations are written to buffers which then later get sent to device
78 static char *dlfb_set_register(char *buf
, u8 reg
, u8 val
)
87 static char *dlfb_vidreg_lock(char *buf
)
89 return dlfb_set_register(buf
, 0xFF, 0x00);
92 static char *dlfb_vidreg_unlock(char *buf
)
94 return dlfb_set_register(buf
, 0xFF, 0xFF);
98 * Map FB_BLANK_* to DisplayLink register
100 * ----- -----------------------------
101 * 0x00 FB_BLANK_UNBLANK (0)
103 * 0x03 FB_BLANK_VSYNC_SUSPEND (2)
104 * 0x05 FB_BLANK_HSYNC_SUSPEND (3)
105 * 0x07 FB_BLANK_POWERDOWN (4) Note: requires modeset to come back
107 static char *dlfb_blanking(char *buf
, int fb_blank
)
112 case FB_BLANK_POWERDOWN
:
115 case FB_BLANK_HSYNC_SUSPEND
:
118 case FB_BLANK_VSYNC_SUSPEND
:
121 case FB_BLANK_NORMAL
:
128 buf
= dlfb_set_register(buf
, 0x1F, reg
);
133 static char *dlfb_set_color_depth(char *buf
, u8 selection
)
135 return dlfb_set_register(buf
, 0x00, selection
);
138 static char *dlfb_set_base16bpp(char *wrptr
, u32 base
)
140 /* the base pointer is 16 bits wide, 0x20 is hi byte. */
141 wrptr
= dlfb_set_register(wrptr
, 0x20, base
>> 16);
142 wrptr
= dlfb_set_register(wrptr
, 0x21, base
>> 8);
143 return dlfb_set_register(wrptr
, 0x22, base
);
147 * DisplayLink HW has separate 16bpp and 8bpp framebuffers.
148 * In 24bpp modes, the low 323 RGB bits go in the 8bpp framebuffer
150 static char *dlfb_set_base8bpp(char *wrptr
, u32 base
)
152 wrptr
= dlfb_set_register(wrptr
, 0x26, base
>> 16);
153 wrptr
= dlfb_set_register(wrptr
, 0x27, base
>> 8);
154 return dlfb_set_register(wrptr
, 0x28, base
);
157 static char *dlfb_set_register_16(char *wrptr
, u8 reg
, u16 value
)
159 wrptr
= dlfb_set_register(wrptr
, reg
, value
>> 8);
160 return dlfb_set_register(wrptr
, reg
+1, value
);
164 * This is kind of weird because the controller takes some
165 * register values in a different byte order than other registers.
167 static char *dlfb_set_register_16be(char *wrptr
, u8 reg
, u16 value
)
169 wrptr
= dlfb_set_register(wrptr
, reg
, value
);
170 return dlfb_set_register(wrptr
, reg
+1, value
>> 8);
174 * LFSR is linear feedback shift register. The reason we have this is
175 * because the display controller needs to minimize the clock depth of
176 * various counters used in the display path. So this code reverses the
177 * provided value into the lfsr16 value by counting backwards to get
178 * the value that needs to be set in the hardware comparator to get the
179 * same actual count. This makes sense once you read above a couple of
180 * times and think about it from a hardware perspective.
182 static u16
dlfb_lfsr16(u16 actual_count
)
184 u32 lv
= 0xFFFF; /* This is the lfsr value that the hw starts with */
186 while (actual_count
--) {
188 (((lv
>> 15) ^ (lv
>> 4) ^ (lv
>> 2) ^ (lv
>> 1)) & 1))
196 * This does LFSR conversion on the value that is to be written.
197 * See LFSR explanation above for more detail.
199 static char *dlfb_set_register_lfsr16(char *wrptr
, u8 reg
, u16 value
)
201 return dlfb_set_register_16(wrptr
, reg
, dlfb_lfsr16(value
));
205 * This takes a standard fbdev screeninfo struct and all of its monitor mode
206 * details and converts them into the DisplayLink equivalent register commands.
208 static char *dlfb_set_vid_cmds(char *wrptr
, struct fb_var_screeninfo
*var
)
214 /* x display start */
215 xds
= var
->left_margin
+ var
->hsync_len
;
216 wrptr
= dlfb_set_register_lfsr16(wrptr
, 0x01, xds
);
218 xde
= xds
+ var
->xres
;
219 wrptr
= dlfb_set_register_lfsr16(wrptr
, 0x03, xde
);
221 /* y display start */
222 yds
= var
->upper_margin
+ var
->vsync_len
;
223 wrptr
= dlfb_set_register_lfsr16(wrptr
, 0x05, yds
);
225 yde
= yds
+ var
->yres
;
226 wrptr
= dlfb_set_register_lfsr16(wrptr
, 0x07, yde
);
228 /* x end count is active + blanking - 1 */
229 wrptr
= dlfb_set_register_lfsr16(wrptr
, 0x09,
230 xde
+ var
->right_margin
- 1);
232 /* libdlo hardcodes hsync start to 1 */
233 wrptr
= dlfb_set_register_lfsr16(wrptr
, 0x0B, 1);
235 /* hsync end is width of sync pulse + 1 */
236 wrptr
= dlfb_set_register_lfsr16(wrptr
, 0x0D, var
->hsync_len
+ 1);
238 /* hpixels is active pixels */
239 wrptr
= dlfb_set_register_16(wrptr
, 0x0F, var
->xres
);
241 /* yendcount is vertical active + vertical blanking */
242 yec
= var
->yres
+ var
->upper_margin
+ var
->lower_margin
+
244 wrptr
= dlfb_set_register_lfsr16(wrptr
, 0x11, yec
);
246 /* libdlo hardcodes vsync start to 0 */
247 wrptr
= dlfb_set_register_lfsr16(wrptr
, 0x13, 0);
249 /* vsync end is width of vsync pulse */
250 wrptr
= dlfb_set_register_lfsr16(wrptr
, 0x15, var
->vsync_len
);
252 /* vpixels is active pixels */
253 wrptr
= dlfb_set_register_16(wrptr
, 0x17, var
->yres
);
255 /* convert picoseconds to 5kHz multiple for pclk5k = x * 1E12/5k */
256 wrptr
= dlfb_set_register_16be(wrptr
, 0x1B,
257 200*1000*1000/var
->pixclock
);
263 * This takes a standard fbdev screeninfo struct that was fetched or prepared
264 * and then generates the appropriate command sequence that then drives the
265 * display controller.
267 static int dlfb_set_video_mode(struct dlfb_data
*dev
,
268 struct fb_var_screeninfo
*var
)
276 if (!atomic_read(&dev
->usb_active
))
279 urb
= dlfb_get_urb(dev
);
283 buf
= (char *) urb
->transfer_buffer
;
286 * This first section has to do with setting the base address on the
287 * controller * associated with the display. There are 2 base
288 * pointers, currently, we only * use the 16 bpp segment.
290 wrptr
= dlfb_vidreg_lock(buf
);
291 wrptr
= dlfb_set_color_depth(wrptr
, 0x00);
292 /* set base for 16bpp segment to 0 */
293 wrptr
= dlfb_set_base16bpp(wrptr
, 0);
294 /* set base for 8bpp segment to end of fb */
295 wrptr
= dlfb_set_base8bpp(wrptr
, dev
->info
->fix
.smem_len
);
297 wrptr
= dlfb_set_vid_cmds(wrptr
, var
);
298 wrptr
= dlfb_blanking(wrptr
, FB_BLANK_UNBLANK
);
299 wrptr
= dlfb_vidreg_unlock(wrptr
);
301 writesize
= wrptr
- buf
;
303 retval
= dlfb_submit_urb(dev
, urb
, writesize
);
305 dev
->blank_mode
= FB_BLANK_UNBLANK
;
310 static int dlfb_ops_mmap(struct fb_info
*info
, struct vm_area_struct
*vma
)
312 unsigned long start
= vma
->vm_start
;
313 unsigned long size
= vma
->vm_end
- vma
->vm_start
;
314 unsigned long offset
= vma
->vm_pgoff
<< PAGE_SHIFT
;
315 unsigned long page
, pos
;
317 if (offset
+ size
> info
->fix
.smem_len
)
320 pos
= (unsigned long)info
->fix
.smem_start
+ offset
;
322 pr_notice("mmap() framebuffer addr:%lu size:%lu\n",
326 page
= vmalloc_to_pfn((void *)pos
);
327 if (remap_pfn_range(vma
, start
, page
, PAGE_SIZE
, PAGE_SHARED
))
332 if (size
> PAGE_SIZE
)
338 vma
->vm_flags
|= VM_RESERVED
; /* avoid to swap out this VMA */
343 * Trims identical data from front and back of line
344 * Sets new front buffer address and width
345 * And returns byte count of identical pixels
346 * Assumes CPU natural alignment (unsigned long)
347 * for back and front buffer ptrs and width
349 static int dlfb_trim_hline(const u8
*bback
, const u8
**bfront
, int *width_bytes
)
352 const unsigned long *back
= (const unsigned long *) bback
;
353 const unsigned long *front
= (const unsigned long *) *bfront
;
354 const int width
= *width_bytes
/ sizeof(unsigned long);
355 int identical
= width
;
359 prefetch((void *) front
);
360 prefetch((void *) back
);
362 for (j
= 0; j
< width
; j
++) {
363 if (back
[j
] != front
[j
]) {
369 for (k
= width
- 1; k
> j
; k
--) {
370 if (back
[k
] != front
[k
]) {
376 identical
= start
+ (width
- end
);
377 *bfront
= (u8
*) &front
[start
];
378 *width_bytes
= (end
- start
) * sizeof(unsigned long);
380 return identical
* sizeof(unsigned long);
384 * Render a command stream for an encoded horizontal line segment of pixels.
386 * A command buffer holds several commands.
387 * It always begins with a fresh command header
388 * (the protocol doesn't require this, but we enforce it to allow
389 * multiple buffers to be potentially encoded and sent in parallel).
390 * A single command encodes one contiguous horizontal line of pixels
392 * The function relies on the client to do all allocation, so that
393 * rendering can be done directly to output buffers (e.g. USB URBs).
394 * The function fills the supplied command buffer, providing information
395 * on where it left off, so the client may call in again with additional
396 * buffers if the line will take several buffers to complete.
398 * A single command can transmit a maximum of 256 pixels,
399 * regardless of the compression ratio (protocol design limit).
400 * To the hardware, 0 for a size byte means 256
402 * Rather than 256 pixel commands which are either rl or raw encoded,
403 * the rlx command simply assumes alternating raw and rl spans within one cmd.
404 * This has a slightly larger header overhead, but produces more even results.
405 * It also processes all data (read and write) in a single pass.
406 * Performance benchmarks of common cases show it having just slightly better
407 * compression than 256 pixel raw or rle commands, with similar CPU consumpion.
408 * But for very rl friendly data, will compress not quite as well.
410 static void dlfb_compress_hline(
411 const uint16_t **pixel_start_ptr
,
412 const uint16_t *const pixel_end
,
413 uint32_t *device_address_ptr
,
414 uint8_t **command_buffer_ptr
,
415 const uint8_t *const cmd_buffer_end
)
417 const uint16_t *pixel
= *pixel_start_ptr
;
418 uint32_t dev_addr
= *device_address_ptr
;
419 uint8_t *cmd
= *command_buffer_ptr
;
422 while ((pixel_end
> pixel
) &&
423 (cmd_buffer_end
- MIN_RLX_CMD_BYTES
> cmd
)) {
424 uint8_t *raw_pixels_count_byte
= 0;
425 uint8_t *cmd_pixels_count_byte
= 0;
426 const uint16_t *raw_pixel_start
= 0;
427 const uint16_t *cmd_pixel_start
, *cmd_pixel_end
= 0;
429 prefetchw((void *) cmd
); /* pull in one cache line at least */
433 *cmd
++ = (uint8_t) ((dev_addr
>> 16) & 0xFF);
434 *cmd
++ = (uint8_t) ((dev_addr
>> 8) & 0xFF);
435 *cmd
++ = (uint8_t) ((dev_addr
) & 0xFF);
437 cmd_pixels_count_byte
= cmd
++; /* we'll know this later */
438 cmd_pixel_start
= pixel
;
440 raw_pixels_count_byte
= cmd
++; /* we'll know this later */
441 raw_pixel_start
= pixel
;
443 cmd_pixel_end
= pixel
+ min(MAX_CMD_PIXELS
+ 1,
444 min((int)(pixel_end
- pixel
),
445 (int)(cmd_buffer_end
- cmd
) / bpp
));
447 prefetch_range((void *) pixel
, (cmd_pixel_end
- pixel
) * bpp
);
449 while (pixel
< cmd_pixel_end
) {
450 const uint16_t * const repeating_pixel
= pixel
;
452 *(uint16_t *)cmd
= cpu_to_be16p(pixel
);
456 if (unlikely((pixel
< cmd_pixel_end
) &&
457 (*pixel
== *repeating_pixel
))) {
458 /* go back and fill in raw pixel count */
459 *raw_pixels_count_byte
= ((repeating_pixel
-
460 raw_pixel_start
) + 1) & 0xFF;
462 while ((pixel
< cmd_pixel_end
)
463 && (*pixel
== *repeating_pixel
)) {
467 /* immediately after raw data is repeat byte */
468 *cmd
++ = ((pixel
- repeating_pixel
) - 1) & 0xFF;
470 /* Then start another raw pixel span */
471 raw_pixel_start
= pixel
;
472 raw_pixels_count_byte
= cmd
++;
476 if (pixel
> raw_pixel_start
) {
477 /* finalize last RAW span */
478 *raw_pixels_count_byte
= (pixel
-raw_pixel_start
) & 0xFF;
481 *cmd_pixels_count_byte
= (pixel
- cmd_pixel_start
) & 0xFF;
482 dev_addr
+= (pixel
- cmd_pixel_start
) * bpp
;
485 if (cmd_buffer_end
<= MIN_RLX_CMD_BYTES
+ cmd
) {
486 /* Fill leftover bytes with no-ops */
487 if (cmd_buffer_end
> cmd
)
488 memset(cmd
, 0xAF, cmd_buffer_end
- cmd
);
489 cmd
= (uint8_t *) cmd_buffer_end
;
492 *command_buffer_ptr
= cmd
;
493 *pixel_start_ptr
= pixel
;
494 *device_address_ptr
= dev_addr
;
500 * There are 3 copies of every pixel: The front buffer that the fbdev
501 * client renders to, the actual framebuffer across the USB bus in hardware
502 * (that we can only write to, slowly, and can never read), and (optionally)
503 * our shadow copy that tracks what's been sent to that hardware buffer.
505 static int dlfb_render_hline(struct dlfb_data
*dev
, struct urb
**urb_ptr
,
506 const char *front
, char **urb_buf_ptr
,
507 u32 byte_offset
, u32 byte_width
,
508 int *ident_ptr
, int *sent_ptr
)
510 const u8
*line_start
, *line_end
, *next_pixel
;
511 u32 dev_addr
= dev
->base16
+ byte_offset
;
512 struct urb
*urb
= *urb_ptr
;
513 u8
*cmd
= *urb_buf_ptr
;
514 u8
*cmd_end
= (u8
*) urb
->transfer_buffer
+ urb
->transfer_buffer_length
;
516 line_start
= (u8
*) (front
+ byte_offset
);
517 next_pixel
= line_start
;
518 line_end
= next_pixel
+ byte_width
;
520 if (dev
->backing_buffer
) {
522 const u8
*back_start
= (u8
*) (dev
->backing_buffer
525 *ident_ptr
+= dlfb_trim_hline(back_start
, &next_pixel
,
528 offset
= next_pixel
- line_start
;
529 line_end
= next_pixel
+ byte_width
;
531 back_start
+= offset
;
532 line_start
+= offset
;
534 memcpy((char *)back_start
, (char *) line_start
,
538 while (next_pixel
< line_end
) {
540 dlfb_compress_hline((const uint16_t **) &next_pixel
,
541 (const uint16_t *) line_end
, &dev_addr
,
542 (u8
**) &cmd
, (u8
*) cmd_end
);
544 if (cmd
>= cmd_end
) {
545 int len
= cmd
- (u8
*) urb
->transfer_buffer
;
546 if (dlfb_submit_urb(dev
, urb
, len
))
547 return 1; /* lost pixels is set */
549 urb
= dlfb_get_urb(dev
);
551 return 1; /* lost_pixels is set */
553 cmd
= urb
->transfer_buffer
;
554 cmd_end
= &cmd
[urb
->transfer_buffer_length
];
563 int dlfb_handle_damage(struct dlfb_data
*dev
, int x
, int y
,
564 int width
, int height
, char *data
)
568 cycles_t start_cycles
, end_cycles
;
570 int bytes_identical
= 0;
574 start_cycles
= get_cycles();
576 aligned_x
= DL_ALIGN_DOWN(x
, sizeof(unsigned long));
577 width
= DL_ALIGN_UP(width
+ (x
-aligned_x
), sizeof(unsigned long));
581 (x
+ width
> dev
->info
->var
.xres
) ||
582 (y
+ height
> dev
->info
->var
.yres
))
585 if (!atomic_read(&dev
->usb_active
))
588 urb
= dlfb_get_urb(dev
);
591 cmd
= urb
->transfer_buffer
;
593 for (i
= y
; i
< y
+ height
; i
++) {
594 const int line_offset
= dev
->info
->fix
.line_length
* i
;
595 const int byte_offset
= line_offset
+ (x
* BPP
);
597 if (dlfb_render_hline(dev
, &urb
,
598 (char *) dev
->info
->fix
.smem_start
,
599 &cmd
, byte_offset
, width
* BPP
,
600 &bytes_identical
, &bytes_sent
))
604 if (cmd
> (char *) urb
->transfer_buffer
) {
605 /* Send partial buffer remaining before exiting */
606 int len
= cmd
- (char *) urb
->transfer_buffer
;
607 ret
= dlfb_submit_urb(dev
, urb
, len
);
610 dlfb_urb_completion(urb
);
613 atomic_add(bytes_sent
, &dev
->bytes_sent
);
614 atomic_add(bytes_identical
, &dev
->bytes_identical
);
615 atomic_add(width
*height
*2, &dev
->bytes_rendered
);
616 end_cycles
= get_cycles();
617 atomic_add(((unsigned int) ((end_cycles
- start_cycles
)
618 >> 10)), /* Kcycles */
619 &dev
->cpu_kcycles_used
);
625 * Path triggered by usermode clients who write to filesystem
626 * e.g. cat filename > /dev/fb1
627 * Not used by X Windows or text-mode console. But useful for testing.
628 * Slow because of extra copy and we must assume all pixels dirty.
630 static ssize_t
dlfb_ops_write(struct fb_info
*info
, const char __user
*buf
,
631 size_t count
, loff_t
*ppos
)
634 struct dlfb_data
*dev
= info
->par
;
635 u32 offset
= (u32
) *ppos
;
637 result
= fb_sys_write(info
, buf
, count
, ppos
);
640 int start
= max((int)(offset
/ info
->fix
.line_length
) - 1, 0);
641 int lines
= min((u32
)((result
/ info
->fix
.line_length
) + 1),
642 (u32
)info
->var
.yres
);
644 dlfb_handle_damage(dev
, 0, start
, info
->var
.xres
,
645 lines
, info
->screen_base
);
651 /* hardware has native COPY command (see libdlo), but not worth it for fbcon */
652 static void dlfb_ops_copyarea(struct fb_info
*info
,
653 const struct fb_copyarea
*area
)
656 struct dlfb_data
*dev
= info
->par
;
658 sys_copyarea(info
, area
);
660 dlfb_handle_damage(dev
, area
->dx
, area
->dy
,
661 area
->width
, area
->height
, info
->screen_base
);
664 static void dlfb_ops_imageblit(struct fb_info
*info
,
665 const struct fb_image
*image
)
667 struct dlfb_data
*dev
= info
->par
;
669 sys_imageblit(info
, image
);
671 dlfb_handle_damage(dev
, image
->dx
, image
->dy
,
672 image
->width
, image
->height
, info
->screen_base
);
675 static void dlfb_ops_fillrect(struct fb_info
*info
,
676 const struct fb_fillrect
*rect
)
678 struct dlfb_data
*dev
= info
->par
;
680 sys_fillrect(info
, rect
);
682 dlfb_handle_damage(dev
, rect
->dx
, rect
->dy
, rect
->width
,
683 rect
->height
, info
->screen_base
);
687 * NOTE: fb_defio.c is holding info->fbdefio.mutex
688 * Touching ANY framebuffer memory that triggers a page fault
689 * in fb_defio will cause a deadlock, when it also tries to
690 * grab the same mutex.
692 static void dlfb_dpy_deferred_io(struct fb_info
*info
,
693 struct list_head
*pagelist
)
696 struct fb_deferred_io
*fbdefio
= info
->fbdefio
;
697 struct dlfb_data
*dev
= info
->par
;
700 cycles_t start_cycles
, end_cycles
;
702 int bytes_identical
= 0;
703 int bytes_rendered
= 0;
708 if (!atomic_read(&dev
->usb_active
))
711 start_cycles
= get_cycles();
713 urb
= dlfb_get_urb(dev
);
717 cmd
= urb
->transfer_buffer
;
719 /* walk the written page list and render each to device */
720 list_for_each_entry(cur
, &fbdefio
->pagelist
, lru
) {
722 if (dlfb_render_hline(dev
, &urb
, (char *) info
->fix
.smem_start
,
723 &cmd
, cur
->index
<< PAGE_SHIFT
,
724 PAGE_SIZE
, &bytes_identical
, &bytes_sent
))
726 bytes_rendered
+= PAGE_SIZE
;
729 if (cmd
> (char *) urb
->transfer_buffer
) {
730 /* Send partial buffer remaining before exiting */
731 int len
= cmd
- (char *) urb
->transfer_buffer
;
732 dlfb_submit_urb(dev
, urb
, len
);
735 dlfb_urb_completion(urb
);
738 atomic_add(bytes_sent
, &dev
->bytes_sent
);
739 atomic_add(bytes_identical
, &dev
->bytes_identical
);
740 atomic_add(bytes_rendered
, &dev
->bytes_rendered
);
741 end_cycles
= get_cycles();
742 atomic_add(((unsigned int) ((end_cycles
- start_cycles
)
743 >> 10)), /* Kcycles */
744 &dev
->cpu_kcycles_used
);
747 static int dlfb_get_edid(struct dlfb_data
*dev
, char *edid
, int len
)
753 rbuf
= kmalloc(2, GFP_KERNEL
);
757 for (i
= 0; i
< len
; i
++) {
758 ret
= usb_control_msg(dev
->udev
,
759 usb_rcvctrlpipe(dev
->udev
, 0), (0x02),
760 (0x80 | (0x02 << 5)), i
<< 8, 0xA1, rbuf
, 2,
763 pr_err("Read EDID byte %d failed err %x\n", i
, ret
);
775 static int dlfb_ops_ioctl(struct fb_info
*info
, unsigned int cmd
,
779 struct dlfb_data
*dev
= info
->par
;
781 if (!atomic_read(&dev
->usb_active
))
784 /* TODO: Update X server to get this from sysfs instead */
785 if (cmd
== DLFB_IOCTL_RETURN_EDID
) {
786 void __user
*edid
= (void __user
*)arg
;
787 if (copy_to_user(edid
, dev
->edid
, dev
->edid_size
))
792 /* TODO: Help propose a standard fb.h ioctl to report mmap damage */
793 if (cmd
== DLFB_IOCTL_REPORT_DAMAGE
) {
796 if (copy_from_user(&area
, (void __user
*)arg
,
797 sizeof(struct dloarea
)))
801 * If we have a damage-aware client, turn fb_defio "off"
802 * To avoid perf imact of unnecessary page fault handling.
803 * Done by resetting the delay for this fb_info to a very
804 * long period. Pages will become writable and stay that way.
805 * Reset to normal value when all clients have closed this fb.
808 info
->fbdefio
->delay
= DL_DEFIO_WRITE_DISABLE
;
813 if (area
.x
> info
->var
.xres
)
814 area
.x
= info
->var
.xres
;
819 if (area
.y
> info
->var
.yres
)
820 area
.y
= info
->var
.yres
;
822 dlfb_handle_damage(dev
, area
.x
, area
.y
, area
.w
, area
.h
,
829 /* taken from vesafb */
831 dlfb_ops_setcolreg(unsigned regno
, unsigned red
, unsigned green
,
832 unsigned blue
, unsigned transp
, struct fb_info
*info
)
836 if (regno
>= info
->cmap
.len
)
840 if (info
->var
.red
.offset
== 10) {
842 ((u32
*) (info
->pseudo_palette
))[regno
] =
843 ((red
& 0xf800) >> 1) |
844 ((green
& 0xf800) >> 6) | ((blue
& 0xf800) >> 11);
847 ((u32
*) (info
->pseudo_palette
))[regno
] =
849 ((green
& 0xfc00) >> 5) | ((blue
& 0xf800) >> 11);
857 * It's common for several clients to have framebuffer open simultaneously.
858 * e.g. both fbcon and X. Makes things interesting.
859 * Assumes caller is holding info->lock (for open and release at least)
861 static int dlfb_ops_open(struct fb_info
*info
, int user
)
863 struct dlfb_data
*dev
= info
->par
;
866 * fbcon aggressively connects to first framebuffer it finds,
867 * preventing other clients (X) from working properly. Usually
868 * not what the user wants. Fail by default with option to enable.
870 if ((user
== 0) && (!console
))
873 /* If the USB device is gone, we don't accept new opens */
874 if (dev
->virtualized
)
879 kref_get(&dev
->kref
);
881 if (fb_defio
&& (info
->fbdefio
== NULL
)) {
882 /* enable defio at last moment if not disabled by client */
884 struct fb_deferred_io
*fbdefio
;
886 fbdefio
= kmalloc(sizeof(struct fb_deferred_io
), GFP_KERNEL
);
889 fbdefio
->delay
= DL_DEFIO_WRITE_DELAY
;
890 fbdefio
->deferred_io
= dlfb_dpy_deferred_io
;
893 info
->fbdefio
= fbdefio
;
894 fb_deferred_io_init(info
);
897 pr_notice("open /dev/fb%d user=%d fb_info=%p count=%d\n",
898 info
->node
, user
, info
, dev
->fb_count
);
904 * Called when all client interfaces to start transactions have been disabled,
905 * and all references to our device instance (dlfb_data) are released.
906 * Every transaction must have a reference, so we know are fully spun down
908 static void dlfb_free(struct kref
*kref
)
910 struct dlfb_data
*dev
= container_of(kref
, struct dlfb_data
, kref
);
912 /* this function will wait for all in-flight urbs to complete */
913 if (dev
->urbs
.count
> 0)
914 dlfb_free_urb_list(dev
);
916 if (dev
->backing_buffer
)
917 vfree(dev
->backing_buffer
);
921 pr_warn("freeing dlfb_data %p\n", dev
);
926 static void dlfb_release_urb_work(struct work_struct
*work
)
928 struct urb_node
*unode
= container_of(work
, struct urb_node
,
929 release_urb_work
.work
);
931 up(&unode
->dev
->urbs
.limit_sem
);
934 static void dlfb_free_framebuffer_work(struct work_struct
*work
)
936 struct dlfb_data
*dev
= container_of(work
, struct dlfb_data
,
937 free_framebuffer_work
.work
);
938 struct fb_info
*info
= dev
->info
;
939 int node
= info
->node
;
941 unregister_framebuffer(info
);
943 if (info
->cmap
.len
!= 0)
944 fb_dealloc_cmap(&info
->cmap
);
945 if (info
->monspecs
.modedb
)
946 fb_destroy_modedb(info
->monspecs
.modedb
);
947 if (info
->screen_base
)
948 vfree(info
->screen_base
);
950 fb_destroy_modelist(&info
->modelist
);
954 /* Assume info structure is freed after this point */
955 framebuffer_release(info
);
957 pr_warn("fb_info for /dev/fb%d has been freed\n", node
);
959 /* ref taken in probe() as part of registering framebfufer */
960 kref_put(&dev
->kref
, dlfb_free
);
964 * Assumes caller is holding info->lock mutex (for open and release at least)
966 static int dlfb_ops_release(struct fb_info
*info
, int user
)
968 struct dlfb_data
*dev
= info
->par
;
972 /* We can't free fb_info here - fbmem will touch it when we return */
973 if (dev
->virtualized
&& (dev
->fb_count
== 0))
974 schedule_delayed_work(&dev
->free_framebuffer_work
, HZ
);
976 if ((dev
->fb_count
== 0) && (info
->fbdefio
)) {
977 fb_deferred_io_cleanup(info
);
978 kfree(info
->fbdefio
);
979 info
->fbdefio
= NULL
;
980 info
->fbops
->fb_mmap
= dlfb_ops_mmap
;
983 pr_warn("released /dev/fb%d user=%d count=%d\n",
984 info
->node
, user
, dev
->fb_count
);
986 kref_put(&dev
->kref
, dlfb_free
);
992 * Check whether a video mode is supported by the DisplayLink chip
993 * We start from monitor's modes, so don't need to filter that here
995 static int dlfb_is_valid_mode(struct fb_videomode
*mode
,
996 struct fb_info
*info
)
998 struct dlfb_data
*dev
= info
->par
;
1000 if (mode
->xres
* mode
->yres
> dev
->sku_pixel_limit
) {
1001 pr_warn("%dx%d beyond chip capabilities\n",
1002 mode
->xres
, mode
->yres
);
1006 pr_info("%dx%d valid mode\n", mode
->xres
, mode
->yres
);
1011 static void dlfb_var_color_format(struct fb_var_screeninfo
*var
)
1013 const struct fb_bitfield red
= { 11, 5, 0 };
1014 const struct fb_bitfield green
= { 5, 6, 0 };
1015 const struct fb_bitfield blue
= { 0, 5, 0 };
1017 var
->bits_per_pixel
= 16;
1023 static int dlfb_ops_check_var(struct fb_var_screeninfo
*var
,
1024 struct fb_info
*info
)
1026 struct fb_videomode mode
;
1028 /* TODO: support dynamically changing framebuffer size */
1029 if ((var
->xres
* var
->yres
* 2) > info
->fix
.smem_len
)
1032 /* set device-specific elements of var unrelated to mode */
1033 dlfb_var_color_format(var
);
1035 fb_var_to_videomode(&mode
, var
);
1037 if (!dlfb_is_valid_mode(&mode
, info
))
1043 static int dlfb_ops_set_par(struct fb_info
*info
)
1045 struct dlfb_data
*dev
= info
->par
;
1047 u16
*pix_framebuffer
;
1050 pr_notice("set_par mode %dx%d\n", info
->var
.xres
, info
->var
.yres
);
1052 result
= dlfb_set_video_mode(dev
, &info
->var
);
1054 if ((result
== 0) && (dev
->fb_count
== 0)) {
1056 /* paint greenscreen */
1058 pix_framebuffer
= (u16
*) info
->screen_base
;
1059 for (i
= 0; i
< info
->fix
.smem_len
/ 2; i
++)
1060 pix_framebuffer
[i
] = 0x37e6;
1062 dlfb_handle_damage(dev
, 0, 0, info
->var
.xres
, info
->var
.yres
,
1069 /* To fonzi the jukebox (e.g. make blanking changes take effect) */
1070 static char *dlfb_dummy_render(char *buf
)
1073 *buf
++ = 0x6A; /* copy */
1074 *buf
++ = 0x00; /* from address*/
1077 *buf
++ = 0x01; /* one pixel */
1078 *buf
++ = 0x00; /* to address */
1085 * In order to come back from full DPMS off, we need to set the mode again
1087 static int dlfb_ops_blank(int blank_mode
, struct fb_info
*info
)
1089 struct dlfb_data
*dev
= info
->par
;
1093 pr_info("/dev/fb%d FB_BLANK mode %d --> %d\n",
1094 info
->node
, dev
->blank_mode
, blank_mode
);
1096 if ((dev
->blank_mode
== FB_BLANK_POWERDOWN
) &&
1097 (blank_mode
!= FB_BLANK_POWERDOWN
)) {
1099 /* returning from powerdown requires a fresh modeset */
1100 dlfb_set_video_mode(dev
, &info
->var
);
1103 urb
= dlfb_get_urb(dev
);
1107 bufptr
= (char *) urb
->transfer_buffer
;
1108 bufptr
= dlfb_vidreg_lock(bufptr
);
1109 bufptr
= dlfb_blanking(bufptr
, blank_mode
);
1110 bufptr
= dlfb_vidreg_unlock(bufptr
);
1112 /* seems like a render op is needed to have blank change take effect */
1113 bufptr
= dlfb_dummy_render(bufptr
);
1115 dlfb_submit_urb(dev
, urb
, bufptr
-
1116 (char *) urb
->transfer_buffer
);
1118 dev
->blank_mode
= blank_mode
;
1123 static struct fb_ops dlfb_ops
= {
1124 .owner
= THIS_MODULE
,
1125 .fb_read
= fb_sys_read
,
1126 .fb_write
= dlfb_ops_write
,
1127 .fb_setcolreg
= dlfb_ops_setcolreg
,
1128 .fb_fillrect
= dlfb_ops_fillrect
,
1129 .fb_copyarea
= dlfb_ops_copyarea
,
1130 .fb_imageblit
= dlfb_ops_imageblit
,
1131 .fb_mmap
= dlfb_ops_mmap
,
1132 .fb_ioctl
= dlfb_ops_ioctl
,
1133 .fb_open
= dlfb_ops_open
,
1134 .fb_release
= dlfb_ops_release
,
1135 .fb_blank
= dlfb_ops_blank
,
1136 .fb_check_var
= dlfb_ops_check_var
,
1137 .fb_set_par
= dlfb_ops_set_par
,
1142 * Assumes &info->lock held by caller
1143 * Assumes no active clients have framebuffer open
1145 static int dlfb_realloc_framebuffer(struct dlfb_data
*dev
, struct fb_info
*info
)
1147 int retval
= -ENOMEM
;
1148 int old_len
= info
->fix
.smem_len
;
1150 unsigned char *old_fb
= info
->screen_base
;
1151 unsigned char *new_fb
;
1152 unsigned char *new_back
= 0;
1154 pr_warn("Reallocating framebuffer. Addresses will change!\n");
1156 new_len
= info
->fix
.line_length
* info
->var
.yres
;
1158 if (PAGE_ALIGN(new_len
) > old_len
) {
1160 * Alloc system memory for virtual framebuffer
1162 new_fb
= vmalloc(new_len
);
1164 pr_err("Virtual framebuffer alloc failed\n");
1168 if (info
->screen_base
) {
1169 memcpy(new_fb
, old_fb
, old_len
);
1170 vfree(info
->screen_base
);
1173 info
->screen_base
= new_fb
;
1174 info
->fix
.smem_len
= PAGE_ALIGN(new_len
);
1175 info
->fix
.smem_start
= (unsigned long) new_fb
;
1176 info
->flags
= udlfb_info_flags
;
1179 * Second framebuffer copy to mirror the framebuffer state
1180 * on the physical USB device. We can function without this.
1181 * But with imperfect damage info we may send pixels over USB
1182 * that were, in fact, unchanged - wasting limited USB bandwidth
1185 new_back
= vzalloc(new_len
);
1187 pr_info("No shadow/backing buffer allocated\n");
1189 if (dev
->backing_buffer
)
1190 vfree(dev
->backing_buffer
);
1191 dev
->backing_buffer
= new_back
;
1202 * 1) Get EDID from hw, or use sw default
1203 * 2) Parse into various fb_info structs
1204 * 3) Allocate virtual framebuffer memory to back highest res mode
1206 * Parses EDID into three places used by various parts of fbdev:
1207 * fb_var_screeninfo contains the timing of the monitor's preferred mode
1208 * fb_info.monspecs is full parsed EDID info, including monspecs.modedb
1209 * fb_info.modelist is a linked list of all monitor & VESA modes which work
1211 * If EDID is not readable/valid, then modelist is all VESA modes,
1212 * monspecs is NULL, and fb_var_screeninfo is set to safe VESA mode
1213 * Returns 0 if successful
1215 static int dlfb_setup_modes(struct dlfb_data
*dev
,
1216 struct fb_info
*info
,
1217 char *default_edid
, size_t default_edid_size
)
1220 const struct fb_videomode
*default_vmode
= NULL
;
1225 if (info
->dev
) /* only use mutex if info has been registered */
1226 mutex_lock(&info
->lock
);
1228 edid
= kmalloc(EDID_LENGTH
, GFP_KERNEL
);
1234 fb_destroy_modelist(&info
->modelist
);
1235 memset(&info
->monspecs
, 0, sizeof(info
->monspecs
));
1238 * Try to (re)read EDID from hardware first
1239 * EDID data may return, but not parse as valid
1240 * Try again a few times, in case of e.g. analog cable noise
1244 i
= dlfb_get_edid(dev
, edid
, EDID_LENGTH
);
1246 if (i
>= EDID_LENGTH
)
1247 fb_edid_to_monspecs(edid
, &info
->monspecs
);
1249 if (info
->monspecs
.modedb_len
> 0) {
1256 /* If that fails, use a previously returned EDID if available */
1257 if (info
->monspecs
.modedb_len
== 0) {
1259 pr_err("Unable to get valid EDID from device/display\n");
1262 fb_edid_to_monspecs(dev
->edid
, &info
->monspecs
);
1263 if (info
->monspecs
.modedb_len
> 0)
1264 pr_err("Using previously queried EDID\n");
1268 /* If that fails, use the default EDID we were handed */
1269 if (info
->monspecs
.modedb_len
== 0) {
1270 if (default_edid_size
>= EDID_LENGTH
) {
1271 fb_edid_to_monspecs(default_edid
, &info
->monspecs
);
1272 if (info
->monspecs
.modedb_len
> 0) {
1273 memcpy(edid
, default_edid
, default_edid_size
);
1275 dev
->edid_size
= default_edid_size
;
1276 pr_err("Using default/backup EDID\n");
1281 /* If we've got modes, let's pick a best default mode */
1282 if (info
->monspecs
.modedb_len
> 0) {
1284 for (i
= 0; i
< info
->monspecs
.modedb_len
; i
++) {
1285 if (dlfb_is_valid_mode(&info
->monspecs
.modedb
[i
], info
))
1286 fb_add_videomode(&info
->monspecs
.modedb
[i
],
1290 /* if we've removed top/best mode */
1292 &= ~FB_MISC_1ST_DETAIL
;
1296 default_vmode
= fb_find_best_display(&info
->monspecs
,
1300 /* If everything else has failed, fall back to safe default mode */
1301 if (default_vmode
== NULL
) {
1303 struct fb_videomode fb_vmode
= {0};
1306 * Add the standard VESA modes to our modelist
1307 * Since we don't have EDID, there may be modes that
1308 * overspec monitor and/or are incorrect aspect ratio, etc.
1309 * But at least the user has a chance to choose
1311 for (i
= 0; i
< VESA_MODEDB_SIZE
; i
++) {
1312 if (dlfb_is_valid_mode((struct fb_videomode
*)
1313 &vesa_modes
[i
], info
))
1314 fb_add_videomode(&vesa_modes
[i
],
1319 * default to resolution safe for projectors
1320 * (since they are most common case without EDID)
1322 fb_vmode
.xres
= 800;
1323 fb_vmode
.yres
= 600;
1324 fb_vmode
.refresh
= 60;
1325 default_vmode
= fb_find_nearest_mode(&fb_vmode
,
1329 /* If we have good mode and no active clients*/
1330 if ((default_vmode
!= NULL
) && (dev
->fb_count
== 0)) {
1332 fb_videomode_to_var(&info
->var
, default_vmode
);
1333 dlfb_var_color_format(&info
->var
);
1336 * with mode size info, we can now alloc our framebuffer.
1338 memcpy(&info
->fix
, &dlfb_fix
, sizeof(dlfb_fix
));
1339 info
->fix
.line_length
= info
->var
.xres
*
1340 (info
->var
.bits_per_pixel
/ 8);
1342 result
= dlfb_realloc_framebuffer(dev
, info
);
1348 if (edid
&& (dev
->edid
!= edid
))
1352 mutex_unlock(&info
->lock
);
1357 static ssize_t
metrics_bytes_rendered_show(struct device
*fbdev
,
1358 struct device_attribute
*a
, char *buf
) {
1359 struct fb_info
*fb_info
= dev_get_drvdata(fbdev
);
1360 struct dlfb_data
*dev
= fb_info
->par
;
1361 return snprintf(buf
, PAGE_SIZE
, "%u\n",
1362 atomic_read(&dev
->bytes_rendered
));
1365 static ssize_t
metrics_bytes_identical_show(struct device
*fbdev
,
1366 struct device_attribute
*a
, char *buf
) {
1367 struct fb_info
*fb_info
= dev_get_drvdata(fbdev
);
1368 struct dlfb_data
*dev
= fb_info
->par
;
1369 return snprintf(buf
, PAGE_SIZE
, "%u\n",
1370 atomic_read(&dev
->bytes_identical
));
1373 static ssize_t
metrics_bytes_sent_show(struct device
*fbdev
,
1374 struct device_attribute
*a
, char *buf
) {
1375 struct fb_info
*fb_info
= dev_get_drvdata(fbdev
);
1376 struct dlfb_data
*dev
= fb_info
->par
;
1377 return snprintf(buf
, PAGE_SIZE
, "%u\n",
1378 atomic_read(&dev
->bytes_sent
));
1381 static ssize_t
metrics_cpu_kcycles_used_show(struct device
*fbdev
,
1382 struct device_attribute
*a
, char *buf
) {
1383 struct fb_info
*fb_info
= dev_get_drvdata(fbdev
);
1384 struct dlfb_data
*dev
= fb_info
->par
;
1385 return snprintf(buf
, PAGE_SIZE
, "%u\n",
1386 atomic_read(&dev
->cpu_kcycles_used
));
1389 static ssize_t
edid_show(
1391 struct kobject
*kobj
, struct bin_attribute
*a
,
1392 char *buf
, loff_t off
, size_t count
) {
1393 struct device
*fbdev
= container_of(kobj
, struct device
, kobj
);
1394 struct fb_info
*fb_info
= dev_get_drvdata(fbdev
);
1395 struct dlfb_data
*dev
= fb_info
->par
;
1397 if (dev
->edid
== NULL
)
1400 if ((off
>= dev
->edid_size
) || (count
> dev
->edid_size
))
1403 if (off
+ count
> dev
->edid_size
)
1404 count
= dev
->edid_size
- off
;
1406 pr_info("sysfs edid copy %p to %p, %d bytes\n",
1407 dev
->edid
, buf
, (int) count
);
1409 memcpy(buf
, dev
->edid
, count
);
1414 static ssize_t
edid_store(
1416 struct kobject
*kobj
, struct bin_attribute
*a
,
1417 char *src
, loff_t src_off
, size_t src_size
) {
1418 struct device
*fbdev
= container_of(kobj
, struct device
, kobj
);
1419 struct fb_info
*fb_info
= dev_get_drvdata(fbdev
);
1420 struct dlfb_data
*dev
= fb_info
->par
;
1422 /* We only support write of entire EDID at once, no offset*/
1423 if ((src_size
!= EDID_LENGTH
) || (src_off
!= 0))
1426 dlfb_setup_modes(dev
, fb_info
, src
, src_size
);
1428 if (dev
->edid
&& (memcmp(src
, dev
->edid
, src_size
) == 0)) {
1429 pr_info("sysfs written EDID is new default\n");
1430 dlfb_ops_set_par(fb_info
);
1436 static ssize_t
metrics_reset_store(struct device
*fbdev
,
1437 struct device_attribute
*attr
,
1438 const char *buf
, size_t count
)
1440 struct fb_info
*fb_info
= dev_get_drvdata(fbdev
);
1441 struct dlfb_data
*dev
= fb_info
->par
;
1443 atomic_set(&dev
->bytes_rendered
, 0);
1444 atomic_set(&dev
->bytes_identical
, 0);
1445 atomic_set(&dev
->bytes_sent
, 0);
1446 atomic_set(&dev
->cpu_kcycles_used
, 0);
1451 static struct bin_attribute edid_attr
= {
1452 .attr
.name
= "edid",
1454 .size
= EDID_LENGTH
,
1459 static struct device_attribute fb_device_attrs
[] = {
1460 __ATTR_RO(metrics_bytes_rendered
),
1461 __ATTR_RO(metrics_bytes_identical
),
1462 __ATTR_RO(metrics_bytes_sent
),
1463 __ATTR_RO(metrics_cpu_kcycles_used
),
1464 __ATTR(metrics_reset
, S_IWUSR
, NULL
, metrics_reset_store
),
1468 * This is necessary before we can communicate with the display controller.
1470 static int dlfb_select_std_channel(struct dlfb_data
*dev
)
1473 u8 set_def_chn
[] = { 0x57, 0xCD, 0xDC, 0xA7,
1474 0x1C, 0x88, 0x5E, 0x15,
1475 0x60, 0xFE, 0xC6, 0x97,
1476 0x16, 0x3D, 0x47, 0xF2 };
1478 ret
= usb_control_msg(dev
->udev
, usb_sndctrlpipe(dev
->udev
, 0),
1479 NR_USB_REQUEST_CHANNEL
,
1480 (USB_DIR_OUT
| USB_TYPE_VENDOR
), 0, 0,
1481 set_def_chn
, sizeof(set_def_chn
), USB_CTRL_SET_TIMEOUT
);
1485 static int dlfb_parse_vendor_descriptor(struct dlfb_data
*dev
,
1486 struct usb_interface
*interface
)
1494 buf
= kzalloc(MAX_VENDOR_DESCRIPTOR_SIZE
, GFP_KERNEL
);
1499 total_len
= usb_get_descriptor(interface_to_usbdev(interface
),
1500 0x5f, /* vendor specific */
1501 0, desc
, MAX_VENDOR_DESCRIPTOR_SIZE
);
1503 /* if not found, look in configuration descriptor */
1504 if (total_len
< 0) {
1505 if (0 == usb_get_extra_descriptor(interface
->cur_altsetting
,
1507 total_len
= (int) desc
[0];
1510 if (total_len
> 5) {
1511 pr_info("vendor descriptor length:%x data:%02x %02x %02x %02x" \
1512 "%02x %02x %02x %02x %02x %02x %02x\n",
1514 desc
[1], desc
[2], desc
[3], desc
[4], desc
[5], desc
[6],
1515 desc
[7], desc
[8], desc
[9], desc
[10]);
1517 if ((desc
[0] != total_len
) || /* descriptor length */
1518 (desc
[1] != 0x5f) || /* vendor descriptor type */
1519 (desc
[2] != 0x01) || /* version (2 bytes) */
1520 (desc
[3] != 0x00) ||
1521 (desc
[4] != total_len
- 2)) /* length after type */
1524 desc_end
= desc
+ total_len
;
1525 desc
+= 5; /* the fixed header we've already parsed */
1527 while (desc
< desc_end
) {
1531 key
= *((u16
*) desc
);
1532 desc
+= sizeof(u16
);
1537 case 0x0200: { /* max_area */
1539 max_area
= le32_to_cpu(*((u32
*)desc
));
1540 pr_warn("DL chip limited to %d pixel modes\n",
1542 dev
->sku_pixel_limit
= max_area
;
1551 pr_info("vendor descriptor not available (%d)\n", total_len
);
1557 /* allow udlfb to load for now even if firmware unrecognized */
1558 pr_err("Unrecognized vendor firmware descriptor\n");
1564 static int dlfb_usb_probe(struct usb_interface
*interface
,
1565 const struct usb_device_id
*id
)
1567 struct usb_device
*usbdev
;
1568 struct dlfb_data
*dev
= 0;
1569 struct fb_info
*info
= 0;
1570 int retval
= -ENOMEM
;
1573 /* usb initialization */
1575 usbdev
= interface_to_usbdev(interface
);
1577 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
1579 err("dlfb_usb_probe: failed alloc of dev struct\n");
1583 /* we need to wait for both usb and fbdev to spin down on disconnect */
1584 kref_init(&dev
->kref
); /* matching kref_put in usb .disconnect fn */
1585 kref_get(&dev
->kref
); /* matching kref_put in free_framebuffer_work */
1588 dev
->gdev
= &usbdev
->dev
; /* our generic struct device * */
1589 usb_set_intfdata(interface
, dev
);
1591 pr_info("%s %s - serial #%s\n",
1592 usbdev
->manufacturer
, usbdev
->product
, usbdev
->serial
);
1593 pr_info("vid_%04x&pid_%04x&rev_%04x driver's dlfb_data struct at %p\n",
1594 usbdev
->descriptor
.idVendor
, usbdev
->descriptor
.idProduct
,
1595 usbdev
->descriptor
.bcdDevice
, dev
);
1596 pr_info("console enable=%d\n", console
);
1597 pr_info("fb_defio enable=%d\n", fb_defio
);
1598 pr_info("shadow enable=%d\n", shadow
);
1600 dev
->sku_pixel_limit
= 2048 * 1152; /* default to maximum */
1602 if (!dlfb_parse_vendor_descriptor(dev
, interface
)) {
1603 pr_err("firmware not recognized. Assume incompatible device\n");
1607 if (!dlfb_alloc_urb_list(dev
, WRITES_IN_FLIGHT
, MAX_TRANSFER
)) {
1609 pr_err("dlfb_alloc_urb_list failed\n");
1613 /* We don't register a new USB class. Our client interface is fbdev */
1615 /* allocates framebuffer driver structure, not framebuffer memory */
1616 info
= framebuffer_alloc(0, &usbdev
->dev
);
1619 pr_err("framebuffer_alloc failed\n");
1625 info
->pseudo_palette
= dev
->pseudo_palette
;
1626 info
->fbops
= &dlfb_ops
;
1628 retval
= fb_alloc_cmap(&info
->cmap
, 256, 0);
1630 pr_err("fb_alloc_cmap failed %x\n", retval
);
1634 INIT_DELAYED_WORK(&dev
->free_framebuffer_work
,
1635 dlfb_free_framebuffer_work
);
1637 INIT_LIST_HEAD(&info
->modelist
);
1639 retval
= dlfb_setup_modes(dev
, info
, NULL
, 0);
1641 pr_err("unable to find common mode for display and adapter\n");
1645 /* ready to begin using device */
1647 atomic_set(&dev
->usb_active
, 1);
1648 dlfb_select_std_channel(dev
);
1650 dlfb_ops_check_var(&info
->var
, info
);
1651 dlfb_ops_set_par(info
);
1653 retval
= register_framebuffer(info
);
1655 pr_err("register_framebuffer failed %d\n", retval
);
1659 for (i
= 0; i
< ARRAY_SIZE(fb_device_attrs
); i
++) {
1660 retval
= device_create_file(info
->dev
, &fb_device_attrs
[i
]);
1662 pr_err("device_create_file failed %d\n", retval
);
1667 retval
= device_create_bin_file(info
->dev
, &edid_attr
);
1669 pr_err("device_create_bin_file failed %d\n", retval
);
1673 pr_info("DisplayLink USB device /dev/fb%d attached. %dx%d resolution."
1674 " Using %dK framebuffer memory\n", info
->node
,
1675 info
->var
.xres
, info
->var
.yres
,
1676 ((dev
->backing_buffer
) ?
1677 info
->fix
.smem_len
* 2 : info
->fix
.smem_len
) >> 10);
1681 for (i
-= 1; i
>= 0; i
--)
1682 device_remove_file(info
->dev
, &fb_device_attrs
[i
]);
1688 if (info
->cmap
.len
!= 0)
1689 fb_dealloc_cmap(&info
->cmap
);
1690 if (info
->monspecs
.modedb
)
1691 fb_destroy_modedb(info
->monspecs
.modedb
);
1692 if (info
->screen_base
)
1693 vfree(info
->screen_base
);
1695 fb_destroy_modelist(&info
->modelist
);
1697 framebuffer_release(info
);
1700 if (dev
->backing_buffer
)
1701 vfree(dev
->backing_buffer
);
1703 kref_put(&dev
->kref
, dlfb_free
); /* ref for framebuffer */
1704 kref_put(&dev
->kref
, dlfb_free
); /* last ref from kref_init */
1706 /* dev has been deallocated. Do not dereference */
1712 static void dlfb_usb_disconnect(struct usb_interface
*interface
)
1714 struct dlfb_data
*dev
;
1715 struct fb_info
*info
;
1718 dev
= usb_get_intfdata(interface
);
1721 pr_info("USB disconnect starting\n");
1723 /* we virtualize until all fb clients release. Then we free */
1724 dev
->virtualized
= true;
1726 /* When non-active we'll update virtual framebuffer, but no new urbs */
1727 atomic_set(&dev
->usb_active
, 0);
1729 /* remove udlfb's sysfs interfaces */
1730 for (i
= 0; i
< ARRAY_SIZE(fb_device_attrs
); i
++)
1731 device_remove_file(info
->dev
, &fb_device_attrs
[i
]);
1732 device_remove_bin_file(info
->dev
, &edid_attr
);
1734 usb_set_intfdata(interface
, NULL
);
1736 /* if clients still have us open, will be freed on last close */
1737 if (dev
->fb_count
== 0)
1738 schedule_delayed_work(&dev
->free_framebuffer_work
, 0);
1740 /* release reference taken by kref_init in probe() */
1741 kref_put(&dev
->kref
, dlfb_free
);
1743 /* consider dlfb_data freed */
1748 static struct usb_driver dlfb_driver
= {
1750 .probe
= dlfb_usb_probe
,
1751 .disconnect
= dlfb_usb_disconnect
,
1752 .id_table
= id_table
,
1755 static int __init
dlfb_module_init(void)
1759 res
= usb_register(&dlfb_driver
);
1761 err("usb_register failed. Error number %d", res
);
1766 static void __exit
dlfb_module_exit(void)
1768 usb_deregister(&dlfb_driver
);
1771 module_init(dlfb_module_init
);
1772 module_exit(dlfb_module_exit
);
1774 static void dlfb_urb_completion(struct urb
*urb
)
1776 struct urb_node
*unode
= urb
->context
;
1777 struct dlfb_data
*dev
= unode
->dev
;
1778 unsigned long flags
;
1780 /* sync/async unlink faults aren't errors */
1782 if (!(urb
->status
== -ENOENT
||
1783 urb
->status
== -ECONNRESET
||
1784 urb
->status
== -ESHUTDOWN
)) {
1785 pr_err("%s - nonzero write bulk status received: %d\n",
1786 __func__
, urb
->status
);
1787 atomic_set(&dev
->lost_pixels
, 1);
1791 urb
->transfer_buffer_length
= dev
->urbs
.size
; /* reset to actual */
1793 spin_lock_irqsave(&dev
->urbs
.lock
, flags
);
1794 list_add_tail(&unode
->entry
, &dev
->urbs
.list
);
1795 dev
->urbs
.available
++;
1796 spin_unlock_irqrestore(&dev
->urbs
.lock
, flags
);
1799 * When using fb_defio, we deadlock if up() is called
1800 * while another is waiting. So queue to another process.
1803 schedule_delayed_work(&unode
->release_urb_work
, 0);
1805 up(&dev
->urbs
.limit_sem
);
1808 static void dlfb_free_urb_list(struct dlfb_data
*dev
)
1810 int count
= dev
->urbs
.count
;
1811 struct list_head
*node
;
1812 struct urb_node
*unode
;
1815 unsigned long flags
;
1817 pr_notice("Waiting for completes and freeing all render urbs\n");
1819 /* keep waiting and freeing, until we've got 'em all */
1822 /* Getting interrupted means a leak, but ok at shutdown*/
1823 ret
= down_interruptible(&dev
->urbs
.limit_sem
);
1827 spin_lock_irqsave(&dev
->urbs
.lock
, flags
);
1829 node
= dev
->urbs
.list
.next
; /* have reserved one with sem */
1830 list_del_init(node
);
1832 spin_unlock_irqrestore(&dev
->urbs
.lock
, flags
);
1834 unode
= list_entry(node
, struct urb_node
, entry
);
1837 /* Free each separately allocated piece */
1838 usb_free_coherent(urb
->dev
, dev
->urbs
.size
,
1839 urb
->transfer_buffer
, urb
->transfer_dma
);
1846 static int dlfb_alloc_urb_list(struct dlfb_data
*dev
, int count
, size_t size
)
1850 struct urb_node
*unode
;
1853 spin_lock_init(&dev
->urbs
.lock
);
1855 dev
->urbs
.size
= size
;
1856 INIT_LIST_HEAD(&dev
->urbs
.list
);
1859 unode
= kzalloc(sizeof(struct urb_node
), GFP_KERNEL
);
1864 INIT_DELAYED_WORK(&unode
->release_urb_work
,
1865 dlfb_release_urb_work
);
1867 urb
= usb_alloc_urb(0, GFP_KERNEL
);
1874 buf
= usb_alloc_coherent(dev
->udev
, MAX_TRANSFER
, GFP_KERNEL
,
1875 &urb
->transfer_dma
);
1882 /* urb->transfer_buffer_length set to actual before submit */
1883 usb_fill_bulk_urb(urb
, dev
->udev
, usb_sndbulkpipe(dev
->udev
, 1),
1884 buf
, size
, dlfb_urb_completion
, unode
);
1885 urb
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
1887 list_add_tail(&unode
->entry
, &dev
->urbs
.list
);
1892 sema_init(&dev
->urbs
.limit_sem
, i
);
1893 dev
->urbs
.count
= i
;
1894 dev
->urbs
.available
= i
;
1896 pr_notice("allocated %d %d byte urbs\n", i
, (int) size
);
1901 static struct urb
*dlfb_get_urb(struct dlfb_data
*dev
)
1904 struct list_head
*entry
;
1905 struct urb_node
*unode
;
1906 struct urb
*urb
= NULL
;
1907 unsigned long flags
;
1909 /* Wait for an in-flight buffer to complete and get re-queued */
1910 ret
= down_timeout(&dev
->urbs
.limit_sem
, GET_URB_TIMEOUT
);
1912 atomic_set(&dev
->lost_pixels
, 1);
1913 pr_warn("wait for urb interrupted: %x available: %d\n",
1914 ret
, dev
->urbs
.available
);
1918 spin_lock_irqsave(&dev
->urbs
.lock
, flags
);
1920 BUG_ON(list_empty(&dev
->urbs
.list
)); /* reserved one with limit_sem */
1921 entry
= dev
->urbs
.list
.next
;
1922 list_del_init(entry
);
1923 dev
->urbs
.available
--;
1925 spin_unlock_irqrestore(&dev
->urbs
.lock
, flags
);
1927 unode
= list_entry(entry
, struct urb_node
, entry
);
1934 static int dlfb_submit_urb(struct dlfb_data
*dev
, struct urb
*urb
, size_t len
)
1938 BUG_ON(len
> dev
->urbs
.size
);
1940 urb
->transfer_buffer_length
= len
; /* set to actual payload len */
1941 ret
= usb_submit_urb(urb
, GFP_KERNEL
);
1943 dlfb_urb_completion(urb
); /* because no one else will */
1944 atomic_set(&dev
->lost_pixels
, 1);
1945 pr_err("usb_submit_urb error %x\n", ret
);
1950 module_param(console
, bool, S_IWUSR
| S_IRUSR
| S_IWGRP
| S_IRGRP
);
1951 MODULE_PARM_DESC(console
, "Allow fbcon to consume first framebuffer found");
1953 module_param(fb_defio
, bool, S_IWUSR
| S_IRUSR
| S_IWGRP
| S_IRGRP
);
1954 MODULE_PARM_DESC(fb_defio
, "Page fault detection of mmap writes");
1956 module_param(shadow
, bool, S_IWUSR
| S_IRUSR
| S_IWGRP
| S_IRGRP
);
1957 MODULE_PARM_DESC(shadow
, "Shadow vid mem. Disable to save mem but lose perf");
1959 MODULE_AUTHOR("Roberto De Ioris <roberto@unbit.it>, "
1960 "Jaya Kumar <jayakumar.lkml@gmail.com>, "
1961 "Bernie Thompson <bernie@plugable.com>");
1962 MODULE_DESCRIPTION("DisplayLink kernel framebuffer driver");
1963 MODULE_LICENSE("GPL");