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
; /* Optionally enable experimental fb_defio mmap support */
66 /* dlfb keeps a list of urbs for efficient bulk transfers */
67 static void dlfb_urb_completion(struct urb
*urb
);
68 static struct urb
*dlfb_get_urb(struct dlfb_data
*dev
);
69 static int dlfb_submit_urb(struct dlfb_data
*dev
, struct urb
* urb
, size_t len
);
70 static int dlfb_alloc_urb_list(struct dlfb_data
*dev
, int count
, size_t size
);
71 static void dlfb_free_urb_list(struct dlfb_data
*dev
);
74 * All DisplayLink bulk operations start with 0xAF, followed by specific code
75 * All operations are written to buffers which then later get sent to device
77 static char *dlfb_set_register(char *buf
, u8 reg
, u8 val
)
86 static char *dlfb_vidreg_lock(char *buf
)
88 return dlfb_set_register(buf
, 0xFF, 0x00);
91 static char *dlfb_vidreg_unlock(char *buf
)
93 return dlfb_set_register(buf
, 0xFF, 0xFF);
97 * Map FB_BLANK_* to DisplayLink register
99 * ----- -----------------------------
100 * 0x00 FB_BLANK_UNBLANK (0)
102 * 0x03 FB_BLANK_VSYNC_SUSPEND (2)
103 * 0x05 FB_BLANK_HSYNC_SUSPEND (3)
104 * 0x07 FB_BLANK_POWERDOWN (4) Note: requires modeset to come back
106 static char *dlfb_blanking(char *buf
, int fb_blank
)
111 case FB_BLANK_POWERDOWN
:
114 case FB_BLANK_HSYNC_SUSPEND
:
117 case FB_BLANK_VSYNC_SUSPEND
:
120 case FB_BLANK_NORMAL
:
127 buf
= dlfb_set_register(buf
, 0x1F, reg
);
132 static char *dlfb_set_color_depth(char *buf
, u8 selection
)
134 return dlfb_set_register(buf
, 0x00, selection
);
137 static char *dlfb_set_base16bpp(char *wrptr
, u32 base
)
139 /* the base pointer is 16 bits wide, 0x20 is hi byte. */
140 wrptr
= dlfb_set_register(wrptr
, 0x20, base
>> 16);
141 wrptr
= dlfb_set_register(wrptr
, 0x21, base
>> 8);
142 return dlfb_set_register(wrptr
, 0x22, base
);
146 * DisplayLink HW has separate 16bpp and 8bpp framebuffers.
147 * In 24bpp modes, the low 323 RGB bits go in the 8bpp framebuffer
149 static char *dlfb_set_base8bpp(char *wrptr
, u32 base
)
151 wrptr
= dlfb_set_register(wrptr
, 0x26, base
>> 16);
152 wrptr
= dlfb_set_register(wrptr
, 0x27, base
>> 8);
153 return dlfb_set_register(wrptr
, 0x28, base
);
156 static char *dlfb_set_register_16(char *wrptr
, u8 reg
, u16 value
)
158 wrptr
= dlfb_set_register(wrptr
, reg
, value
>> 8);
159 return dlfb_set_register(wrptr
, reg
+1, value
);
163 * This is kind of weird because the controller takes some
164 * register values in a different byte order than other registers.
166 static char *dlfb_set_register_16be(char *wrptr
, u8 reg
, u16 value
)
168 wrptr
= dlfb_set_register(wrptr
, reg
, value
);
169 return dlfb_set_register(wrptr
, reg
+1, value
>> 8);
173 * LFSR is linear feedback shift register. The reason we have this is
174 * because the display controller needs to minimize the clock depth of
175 * various counters used in the display path. So this code reverses the
176 * provided value into the lfsr16 value by counting backwards to get
177 * the value that needs to be set in the hardware comparator to get the
178 * same actual count. This makes sense once you read above a couple of
179 * times and think about it from a hardware perspective.
181 static u16
dlfb_lfsr16(u16 actual_count
)
183 u32 lv
= 0xFFFF; /* This is the lfsr value that the hw starts with */
185 while (actual_count
--) {
187 (((lv
>> 15) ^ (lv
>> 4) ^ (lv
>> 2) ^ (lv
>> 1)) & 1))
195 * This does LFSR conversion on the value that is to be written.
196 * See LFSR explanation above for more detail.
198 static char *dlfb_set_register_lfsr16(char *wrptr
, u8 reg
, u16 value
)
200 return dlfb_set_register_16(wrptr
, reg
, dlfb_lfsr16(value
));
204 * This takes a standard fbdev screeninfo struct and all of its monitor mode
205 * details and converts them into the DisplayLink equivalent register commands.
207 static char *dlfb_set_vid_cmds(char *wrptr
, struct fb_var_screeninfo
*var
)
213 /* x display start */
214 xds
= var
->left_margin
+ var
->hsync_len
;
215 wrptr
= dlfb_set_register_lfsr16(wrptr
, 0x01, xds
);
217 xde
= xds
+ var
->xres
;
218 wrptr
= dlfb_set_register_lfsr16(wrptr
, 0x03, xde
);
220 /* y display start */
221 yds
= var
->upper_margin
+ var
->vsync_len
;
222 wrptr
= dlfb_set_register_lfsr16(wrptr
, 0x05, yds
);
224 yde
= yds
+ var
->yres
;
225 wrptr
= dlfb_set_register_lfsr16(wrptr
, 0x07, yde
);
227 /* x end count is active + blanking - 1 */
228 wrptr
= dlfb_set_register_lfsr16(wrptr
, 0x09,
229 xde
+ var
->right_margin
- 1);
231 /* libdlo hardcodes hsync start to 1 */
232 wrptr
= dlfb_set_register_lfsr16(wrptr
, 0x0B, 1);
234 /* hsync end is width of sync pulse + 1 */
235 wrptr
= dlfb_set_register_lfsr16(wrptr
, 0x0D, var
->hsync_len
+ 1);
237 /* hpixels is active pixels */
238 wrptr
= dlfb_set_register_16(wrptr
, 0x0F, var
->xres
);
240 /* yendcount is vertical active + vertical blanking */
241 yec
= var
->yres
+ var
->upper_margin
+ var
->lower_margin
+
243 wrptr
= dlfb_set_register_lfsr16(wrptr
, 0x11, yec
);
245 /* libdlo hardcodes vsync start to 0 */
246 wrptr
= dlfb_set_register_lfsr16(wrptr
, 0x13, 0);
248 /* vsync end is width of vsync pulse */
249 wrptr
= dlfb_set_register_lfsr16(wrptr
, 0x15, var
->vsync_len
);
251 /* vpixels is active pixels */
252 wrptr
= dlfb_set_register_16(wrptr
, 0x17, var
->yres
);
254 /* convert picoseconds to 5kHz multiple for pclk5k = x * 1E12/5k */
255 wrptr
= dlfb_set_register_16be(wrptr
, 0x1B,
256 200*1000*1000/var
->pixclock
);
262 * This takes a standard fbdev screeninfo struct that was fetched or prepared
263 * and then generates the appropriate command sequence that then drives the
264 * display controller.
266 static int dlfb_set_video_mode(struct dlfb_data
*dev
,
267 struct fb_var_screeninfo
*var
)
275 if (!atomic_read(&dev
->usb_active
))
278 urb
= dlfb_get_urb(dev
);
282 buf
= (char *) urb
->transfer_buffer
;
285 * This first section has to do with setting the base address on the
286 * controller * associated with the display. There are 2 base
287 * pointers, currently, we only * use the 16 bpp segment.
289 wrptr
= dlfb_vidreg_lock(buf
);
290 wrptr
= dlfb_set_color_depth(wrptr
, 0x00);
291 /* set base for 16bpp segment to 0 */
292 wrptr
= dlfb_set_base16bpp(wrptr
, 0);
293 /* set base for 8bpp segment to end of fb */
294 wrptr
= dlfb_set_base8bpp(wrptr
, dev
->info
->fix
.smem_len
);
296 wrptr
= dlfb_set_vid_cmds(wrptr
, var
);
297 wrptr
= dlfb_blanking(wrptr
, FB_BLANK_UNBLANK
);
298 wrptr
= dlfb_vidreg_unlock(wrptr
);
300 writesize
= wrptr
- buf
;
302 retval
= dlfb_submit_urb(dev
, urb
, writesize
);
304 dev
->blank_mode
= FB_BLANK_UNBLANK
;
309 static int dlfb_ops_mmap(struct fb_info
*info
, struct vm_area_struct
*vma
)
311 unsigned long start
= vma
->vm_start
;
312 unsigned long size
= vma
->vm_end
- vma
->vm_start
;
313 unsigned long offset
= vma
->vm_pgoff
<< PAGE_SHIFT
;
314 unsigned long page
, pos
;
316 if (offset
+ size
> info
->fix
.smem_len
)
319 pos
= (unsigned long)info
->fix
.smem_start
+ offset
;
321 pr_notice("mmap() framebuffer addr:%lu size:%lu\n",
325 page
= vmalloc_to_pfn((void *)pos
);
326 if (remap_pfn_range(vma
, start
, page
, PAGE_SIZE
, PAGE_SHARED
))
331 if (size
> PAGE_SIZE
)
337 vma
->vm_flags
|= VM_RESERVED
; /* avoid to swap out this VMA */
342 * Trims identical data from front and back of line
343 * Sets new front buffer address and width
344 * And returns byte count of identical pixels
345 * Assumes CPU natural alignment (unsigned long)
346 * for back and front buffer ptrs and width
348 static int dlfb_trim_hline(const u8
*bback
, const u8
**bfront
, int *width_bytes
)
351 const unsigned long *back
= (const unsigned long *) bback
;
352 const unsigned long *front
= (const unsigned long *) *bfront
;
353 const int width
= *width_bytes
/ sizeof(unsigned long);
354 int identical
= width
;
358 prefetch((void *) front
);
359 prefetch((void *) back
);
361 for (j
= 0; j
< width
; j
++) {
362 if (back
[j
] != front
[j
]) {
368 for (k
= width
- 1; k
> j
; k
--) {
369 if (back
[k
] != front
[k
]) {
375 identical
= start
+ (width
- end
);
376 *bfront
= (u8
*) &front
[start
];
377 *width_bytes
= (end
- start
) * sizeof(unsigned long);
379 return identical
* sizeof(unsigned long);
383 * Render a command stream for an encoded horizontal line segment of pixels.
385 * A command buffer holds several commands.
386 * It always begins with a fresh command header
387 * (the protocol doesn't require this, but we enforce it to allow
388 * multiple buffers to be potentially encoded and sent in parallel).
389 * A single command encodes one contiguous horizontal line of pixels
391 * The function relies on the client to do all allocation, so that
392 * rendering can be done directly to output buffers (e.g. USB URBs).
393 * The function fills the supplied command buffer, providing information
394 * on where it left off, so the client may call in again with additional
395 * buffers if the line will take several buffers to complete.
397 * A single command can transmit a maximum of 256 pixels,
398 * regardless of the compression ratio (protocol design limit).
399 * To the hardware, 0 for a size byte means 256
401 * Rather than 256 pixel commands which are either rl or raw encoded,
402 * the rlx command simply assumes alternating raw and rl spans within one cmd.
403 * This has a slightly larger header overhead, but produces more even results.
404 * It also processes all data (read and write) in a single pass.
405 * Performance benchmarks of common cases show it having just slightly better
406 * compression than 256 pixel raw or rle commands, with similar CPU consumpion.
407 * But for very rl friendly data, will compress not quite as well.
409 static void dlfb_compress_hline(
410 const uint16_t **pixel_start_ptr
,
411 const uint16_t *const pixel_end
,
412 uint32_t *device_address_ptr
,
413 uint8_t **command_buffer_ptr
,
414 const uint8_t *const cmd_buffer_end
)
416 const uint16_t *pixel
= *pixel_start_ptr
;
417 uint32_t dev_addr
= *device_address_ptr
;
418 uint8_t *cmd
= *command_buffer_ptr
;
421 while ((pixel_end
> pixel
) &&
422 (cmd_buffer_end
- MIN_RLX_CMD_BYTES
> cmd
)) {
423 uint8_t *raw_pixels_count_byte
= 0;
424 uint8_t *cmd_pixels_count_byte
= 0;
425 const uint16_t *raw_pixel_start
= 0;
426 const uint16_t *cmd_pixel_start
, *cmd_pixel_end
= 0;
428 prefetchw((void *) cmd
); /* pull in one cache line at least */
432 *cmd
++ = (uint8_t) ((dev_addr
>> 16) & 0xFF);
433 *cmd
++ = (uint8_t) ((dev_addr
>> 8) & 0xFF);
434 *cmd
++ = (uint8_t) ((dev_addr
) & 0xFF);
436 cmd_pixels_count_byte
= cmd
++; /* we'll know this later */
437 cmd_pixel_start
= pixel
;
439 raw_pixels_count_byte
= cmd
++; /* we'll know this later */
440 raw_pixel_start
= pixel
;
442 cmd_pixel_end
= pixel
+ min(MAX_CMD_PIXELS
+ 1,
443 min((int)(pixel_end
- pixel
),
444 (int)(cmd_buffer_end
- cmd
) / bpp
));
446 prefetch_range((void *) pixel
, (cmd_pixel_end
- pixel
) * bpp
);
448 while (pixel
< cmd_pixel_end
) {
449 const uint16_t * const repeating_pixel
= pixel
;
451 *(uint16_t *)cmd
= cpu_to_be16p(pixel
);
455 if (unlikely((pixel
< cmd_pixel_end
) &&
456 (*pixel
== *repeating_pixel
))) {
457 /* go back and fill in raw pixel count */
458 *raw_pixels_count_byte
= ((repeating_pixel
-
459 raw_pixel_start
) + 1) & 0xFF;
461 while ((pixel
< cmd_pixel_end
)
462 && (*pixel
== *repeating_pixel
)) {
466 /* immediately after raw data is repeat byte */
467 *cmd
++ = ((pixel
- repeating_pixel
) - 1) & 0xFF;
469 /* Then start another raw pixel span */
470 raw_pixel_start
= pixel
;
471 raw_pixels_count_byte
= cmd
++;
475 if (pixel
> raw_pixel_start
) {
476 /* finalize last RAW span */
477 *raw_pixels_count_byte
= (pixel
-raw_pixel_start
) & 0xFF;
480 *cmd_pixels_count_byte
= (pixel
- cmd_pixel_start
) & 0xFF;
481 dev_addr
+= (pixel
- cmd_pixel_start
) * bpp
;
484 if (cmd_buffer_end
<= MIN_RLX_CMD_BYTES
+ cmd
) {
485 /* Fill leftover bytes with no-ops */
486 if (cmd_buffer_end
> cmd
)
487 memset(cmd
, 0xAF, cmd_buffer_end
- cmd
);
488 cmd
= (uint8_t *) cmd_buffer_end
;
491 *command_buffer_ptr
= cmd
;
492 *pixel_start_ptr
= pixel
;
493 *device_address_ptr
= dev_addr
;
499 * There are 3 copies of every pixel: The front buffer that the fbdev
500 * client renders to, the actual framebuffer across the USB bus in hardware
501 * (that we can only write to, slowly, and can never read), and (optionally)
502 * our shadow copy that tracks what's been sent to that hardware buffer.
504 static int dlfb_render_hline(struct dlfb_data
*dev
, struct urb
**urb_ptr
,
505 const char *front
, char **urb_buf_ptr
,
506 u32 byte_offset
, u32 byte_width
,
507 int *ident_ptr
, int *sent_ptr
)
509 const u8
*line_start
, *line_end
, *next_pixel
;
510 u32 dev_addr
= dev
->base16
+ byte_offset
;
511 struct urb
*urb
= *urb_ptr
;
512 u8
*cmd
= *urb_buf_ptr
;
513 u8
*cmd_end
= (u8
*) urb
->transfer_buffer
+ urb
->transfer_buffer_length
;
515 line_start
= (u8
*) (front
+ byte_offset
);
516 next_pixel
= line_start
;
517 line_end
= next_pixel
+ byte_width
;
519 if (dev
->backing_buffer
) {
521 const u8
*back_start
= (u8
*) (dev
->backing_buffer
524 *ident_ptr
+= dlfb_trim_hline(back_start
, &next_pixel
,
527 offset
= next_pixel
- line_start
;
528 line_end
= next_pixel
+ byte_width
;
530 back_start
+= offset
;
531 line_start
+= offset
;
533 memcpy((char *)back_start
, (char *) line_start
,
537 while (next_pixel
< line_end
) {
539 dlfb_compress_hline((const uint16_t **) &next_pixel
,
540 (const uint16_t *) line_end
, &dev_addr
,
541 (u8
**) &cmd
, (u8
*) cmd_end
);
543 if (cmd
>= cmd_end
) {
544 int len
= cmd
- (u8
*) urb
->transfer_buffer
;
545 if (dlfb_submit_urb(dev
, urb
, len
))
546 return 1; /* lost pixels is set */
548 urb
= dlfb_get_urb(dev
);
550 return 1; /* lost_pixels is set */
552 cmd
= urb
->transfer_buffer
;
553 cmd_end
= &cmd
[urb
->transfer_buffer_length
];
562 int dlfb_handle_damage(struct dlfb_data
*dev
, int x
, int y
,
563 int width
, int height
, char *data
)
567 cycles_t start_cycles
, end_cycles
;
569 int bytes_identical
= 0;
573 start_cycles
= get_cycles();
575 aligned_x
= DL_ALIGN_DOWN(x
, sizeof(unsigned long));
576 width
= DL_ALIGN_UP(width
+ (x
-aligned_x
), sizeof(unsigned long));
580 (x
+ width
> dev
->info
->var
.xres
) ||
581 (y
+ height
> dev
->info
->var
.yres
))
584 if (!atomic_read(&dev
->usb_active
))
587 urb
= dlfb_get_urb(dev
);
590 cmd
= urb
->transfer_buffer
;
592 for (i
= y
; i
< y
+ height
; i
++) {
593 const int line_offset
= dev
->info
->fix
.line_length
* i
;
594 const int byte_offset
= line_offset
+ (x
* BPP
);
596 if (dlfb_render_hline(dev
, &urb
,
597 (char *) dev
->info
->fix
.smem_start
,
598 &cmd
, byte_offset
, width
* BPP
,
599 &bytes_identical
, &bytes_sent
))
603 if (cmd
> (char *) urb
->transfer_buffer
) {
604 /* Send partial buffer remaining before exiting */
605 int len
= cmd
- (char *) urb
->transfer_buffer
;
606 ret
= dlfb_submit_urb(dev
, urb
, len
);
609 dlfb_urb_completion(urb
);
612 atomic_add(bytes_sent
, &dev
->bytes_sent
);
613 atomic_add(bytes_identical
, &dev
->bytes_identical
);
614 atomic_add(width
*height
*2, &dev
->bytes_rendered
);
615 end_cycles
= get_cycles();
616 atomic_add(((unsigned int) ((end_cycles
- start_cycles
)
617 >> 10)), /* Kcycles */
618 &dev
->cpu_kcycles_used
);
624 * Path triggered by usermode clients who write to filesystem
625 * e.g. cat filename > /dev/fb1
626 * Not used by X Windows or text-mode console. But useful for testing.
627 * Slow because of extra copy and we must assume all pixels dirty.
629 static ssize_t
dlfb_ops_write(struct fb_info
*info
, const char __user
*buf
,
630 size_t count
, loff_t
*ppos
)
633 struct dlfb_data
*dev
= info
->par
;
634 u32 offset
= (u32
) *ppos
;
636 result
= fb_sys_write(info
, buf
, count
, ppos
);
639 int start
= max((int)(offset
/ info
->fix
.line_length
) - 1, 0);
640 int lines
= min((u32
)((result
/ info
->fix
.line_length
) + 1),
641 (u32
)info
->var
.yres
);
643 dlfb_handle_damage(dev
, 0, start
, info
->var
.xres
,
644 lines
, info
->screen_base
);
650 /* hardware has native COPY command (see libdlo), but not worth it for fbcon */
651 static void dlfb_ops_copyarea(struct fb_info
*info
,
652 const struct fb_copyarea
*area
)
655 struct dlfb_data
*dev
= info
->par
;
657 sys_copyarea(info
, area
);
659 dlfb_handle_damage(dev
, area
->dx
, area
->dy
,
660 area
->width
, area
->height
, info
->screen_base
);
663 static void dlfb_ops_imageblit(struct fb_info
*info
,
664 const struct fb_image
*image
)
666 struct dlfb_data
*dev
= info
->par
;
668 sys_imageblit(info
, image
);
670 dlfb_handle_damage(dev
, image
->dx
, image
->dy
,
671 image
->width
, image
->height
, info
->screen_base
);
674 static void dlfb_ops_fillrect(struct fb_info
*info
,
675 const struct fb_fillrect
*rect
)
677 struct dlfb_data
*dev
= info
->par
;
679 sys_fillrect(info
, rect
);
681 dlfb_handle_damage(dev
, rect
->dx
, rect
->dy
, rect
->width
,
682 rect
->height
, info
->screen_base
);
686 * NOTE: fb_defio.c is holding info->fbdefio.mutex
687 * Touching ANY framebuffer memory that triggers a page fault
688 * in fb_defio will cause a deadlock, when it also tries to
689 * grab the same mutex.
691 static void dlfb_dpy_deferred_io(struct fb_info
*info
,
692 struct list_head
*pagelist
)
695 struct fb_deferred_io
*fbdefio
= info
->fbdefio
;
696 struct dlfb_data
*dev
= info
->par
;
699 cycles_t start_cycles
, end_cycles
;
701 int bytes_identical
= 0;
702 int bytes_rendered
= 0;
707 if (!atomic_read(&dev
->usb_active
))
710 start_cycles
= get_cycles();
712 urb
= dlfb_get_urb(dev
);
716 cmd
= urb
->transfer_buffer
;
718 /* walk the written page list and render each to device */
719 list_for_each_entry(cur
, &fbdefio
->pagelist
, lru
) {
721 if (dlfb_render_hline(dev
, &urb
, (char *) info
->fix
.smem_start
,
722 &cmd
, cur
->index
<< PAGE_SHIFT
,
723 PAGE_SIZE
, &bytes_identical
, &bytes_sent
))
725 bytes_rendered
+= PAGE_SIZE
;
728 if (cmd
> (char *) urb
->transfer_buffer
) {
729 /* Send partial buffer remaining before exiting */
730 int len
= cmd
- (char *) urb
->transfer_buffer
;
731 dlfb_submit_urb(dev
, urb
, len
);
734 dlfb_urb_completion(urb
);
737 atomic_add(bytes_sent
, &dev
->bytes_sent
);
738 atomic_add(bytes_identical
, &dev
->bytes_identical
);
739 atomic_add(bytes_rendered
, &dev
->bytes_rendered
);
740 end_cycles
= get_cycles();
741 atomic_add(((unsigned int) ((end_cycles
- start_cycles
)
742 >> 10)), /* Kcycles */
743 &dev
->cpu_kcycles_used
);
746 static int dlfb_get_edid(struct dlfb_data
*dev
, char *edid
, int len
)
752 rbuf
= kmalloc(2, GFP_KERNEL
);
756 for (i
= 0; i
< len
; i
++) {
757 ret
= usb_control_msg(dev
->udev
,
758 usb_rcvctrlpipe(dev
->udev
, 0), (0x02),
759 (0x80 | (0x02 << 5)), i
<< 8, 0xA1, rbuf
, 2,
762 pr_err("Read EDID byte %d failed err %x\n", i
, ret
);
774 static int dlfb_ops_ioctl(struct fb_info
*info
, unsigned int cmd
,
778 struct dlfb_data
*dev
= info
->par
;
780 if (!atomic_read(&dev
->usb_active
))
783 /* TODO: Update X server to get this from sysfs instead */
784 if (cmd
== DLFB_IOCTL_RETURN_EDID
) {
785 void __user
*edid
= (void __user
*)arg
;
786 if (copy_to_user(edid
, dev
->edid
, dev
->edid_size
))
791 /* TODO: Help propose a standard fb.h ioctl to report mmap damage */
792 if (cmd
== DLFB_IOCTL_REPORT_DAMAGE
) {
795 if (copy_from_user(&area
, (void __user
*)arg
,
796 sizeof(struct dloarea
)))
800 * If we have a damage-aware client, turn fb_defio "off"
801 * To avoid perf imact of unnecessary page fault handling.
802 * Done by resetting the delay for this fb_info to a very
803 * long period. Pages will become writable and stay that way.
804 * Reset to normal value when all clients have closed this fb.
807 info
->fbdefio
->delay
= DL_DEFIO_WRITE_DISABLE
;
812 if (area
.x
> info
->var
.xres
)
813 area
.x
= info
->var
.xres
;
818 if (area
.y
> info
->var
.yres
)
819 area
.y
= info
->var
.yres
;
821 dlfb_handle_damage(dev
, area
.x
, area
.y
, area
.w
, area
.h
,
828 /* taken from vesafb */
830 dlfb_ops_setcolreg(unsigned regno
, unsigned red
, unsigned green
,
831 unsigned blue
, unsigned transp
, struct fb_info
*info
)
835 if (regno
>= info
->cmap
.len
)
839 if (info
->var
.red
.offset
== 10) {
841 ((u32
*) (info
->pseudo_palette
))[regno
] =
842 ((red
& 0xf800) >> 1) |
843 ((green
& 0xf800) >> 6) | ((blue
& 0xf800) >> 11);
846 ((u32
*) (info
->pseudo_palette
))[regno
] =
848 ((green
& 0xfc00) >> 5) | ((blue
& 0xf800) >> 11);
856 * It's common for several clients to have framebuffer open simultaneously.
857 * e.g. both fbcon and X. Makes things interesting.
858 * Assumes caller is holding info->lock (for open and release at least)
860 static int dlfb_ops_open(struct fb_info
*info
, int user
)
862 struct dlfb_data
*dev
= info
->par
;
865 * fbcon aggressively connects to first framebuffer it finds,
866 * preventing other clients (X) from working properly. Usually
867 * not what the user wants. Fail by default with option to enable.
869 if ((user
== 0) && (!console
))
872 /* If the USB device is gone, we don't accept new opens */
873 if (dev
->virtualized
)
878 kref_get(&dev
->kref
);
880 if (fb_defio
&& (info
->fbdefio
== NULL
)) {
881 /* enable defio at last moment if not disabled by client */
883 struct fb_deferred_io
*fbdefio
;
885 fbdefio
= kmalloc(sizeof(struct fb_deferred_io
), GFP_KERNEL
);
888 fbdefio
->delay
= DL_DEFIO_WRITE_DELAY
;
889 fbdefio
->deferred_io
= dlfb_dpy_deferred_io
;
892 info
->fbdefio
= fbdefio
;
893 fb_deferred_io_init(info
);
896 pr_notice("open /dev/fb%d user=%d fb_info=%p count=%d\n",
897 info
->node
, user
, info
, dev
->fb_count
);
903 * Called when all client interfaces to start transactions have been disabled,
904 * and all references to our device instance (dlfb_data) are released.
905 * Every transaction must have a reference, so we know are fully spun down
907 static void dlfb_free(struct kref
*kref
)
909 struct dlfb_data
*dev
= container_of(kref
, struct dlfb_data
, kref
);
911 /* this function will wait for all in-flight urbs to complete */
912 if (dev
->urbs
.count
> 0)
913 dlfb_free_urb_list(dev
);
915 if (dev
->backing_buffer
)
916 vfree(dev
->backing_buffer
);
920 pr_warn("freeing dlfb_data %p\n", dev
);
925 static void dlfb_release_urb_work(struct work_struct
*work
)
927 struct urb_node
*unode
= container_of(work
, struct urb_node
,
928 release_urb_work
.work
);
930 up(&unode
->dev
->urbs
.limit_sem
);
933 static void dlfb_free_framebuffer_work(struct work_struct
*work
)
935 struct dlfb_data
*dev
= container_of(work
, struct dlfb_data
,
936 free_framebuffer_work
.work
);
937 struct fb_info
*info
= dev
->info
;
938 int node
= info
->node
;
940 unregister_framebuffer(info
);
942 if (info
->cmap
.len
!= 0)
943 fb_dealloc_cmap(&info
->cmap
);
944 if (info
->monspecs
.modedb
)
945 fb_destroy_modedb(info
->monspecs
.modedb
);
946 if (info
->screen_base
)
947 vfree(info
->screen_base
);
949 fb_destroy_modelist(&info
->modelist
);
953 /* Assume info structure is freed after this point */
954 framebuffer_release(info
);
956 pr_warn("fb_info for /dev/fb%d has been freed\n", node
);
958 /* ref taken in probe() as part of registering framebfufer */
959 kref_put(&dev
->kref
, dlfb_free
);
963 * Assumes caller is holding info->lock mutex (for open and release at least)
965 static int dlfb_ops_release(struct fb_info
*info
, int user
)
967 struct dlfb_data
*dev
= info
->par
;
971 /* We can't free fb_info here - fbmem will touch it when we return */
972 if (dev
->virtualized
&& (dev
->fb_count
== 0))
973 schedule_delayed_work(&dev
->free_framebuffer_work
, HZ
);
975 if ((dev
->fb_count
== 0) && (info
->fbdefio
)) {
976 fb_deferred_io_cleanup(info
);
977 kfree(info
->fbdefio
);
978 info
->fbdefio
= NULL
;
979 info
->fbops
->fb_mmap
= dlfb_ops_mmap
;
982 pr_warn("released /dev/fb%d user=%d count=%d\n",
983 info
->node
, user
, dev
->fb_count
);
985 kref_put(&dev
->kref
, dlfb_free
);
991 * Check whether a video mode is supported by the DisplayLink chip
992 * We start from monitor's modes, so don't need to filter that here
994 static int dlfb_is_valid_mode(struct fb_videomode
*mode
,
995 struct fb_info
*info
)
997 struct dlfb_data
*dev
= info
->par
;
999 if (mode
->xres
* mode
->yres
> dev
->sku_pixel_limit
) {
1000 pr_warn("%dx%d beyond chip capabilities\n",
1001 mode
->xres
, mode
->yres
);
1005 pr_info("%dx%d valid mode\n", mode
->xres
, mode
->yres
);
1010 static void dlfb_var_color_format(struct fb_var_screeninfo
*var
)
1012 const struct fb_bitfield red
= { 11, 5, 0 };
1013 const struct fb_bitfield green
= { 5, 6, 0 };
1014 const struct fb_bitfield blue
= { 0, 5, 0 };
1016 var
->bits_per_pixel
= 16;
1022 static int dlfb_ops_check_var(struct fb_var_screeninfo
*var
,
1023 struct fb_info
*info
)
1025 struct fb_videomode mode
;
1027 /* TODO: support dynamically changing framebuffer size */
1028 if ((var
->xres
* var
->yres
* 2) > info
->fix
.smem_len
)
1031 /* set device-specific elements of var unrelated to mode */
1032 dlfb_var_color_format(var
);
1034 fb_var_to_videomode(&mode
, var
);
1036 if (!dlfb_is_valid_mode(&mode
, info
))
1042 static int dlfb_ops_set_par(struct fb_info
*info
)
1044 struct dlfb_data
*dev
= info
->par
;
1046 u16
*pix_framebuffer
;
1049 pr_notice("set_par mode %dx%d\n", info
->var
.xres
, info
->var
.yres
);
1051 result
= dlfb_set_video_mode(dev
, &info
->var
);
1053 if ((result
== 0) && (dev
->fb_count
== 0)) {
1055 /* paint greenscreen */
1057 pix_framebuffer
= (u16
*) info
->screen_base
;
1058 for (i
= 0; i
< info
->fix
.smem_len
/ 2; i
++)
1059 pix_framebuffer
[i
] = 0x37e6;
1061 dlfb_handle_damage(dev
, 0, 0, info
->var
.xres
, info
->var
.yres
,
1068 /* To fonzi the jukebox (e.g. make blanking changes take effect) */
1069 static char *dlfb_dummy_render(char *buf
)
1072 *buf
++ = 0x6A; /* copy */
1073 *buf
++ = 0x00; /* from address*/
1076 *buf
++ = 0x01; /* one pixel */
1077 *buf
++ = 0x00; /* to address */
1084 * In order to come back from full DPMS off, we need to set the mode again
1086 static int dlfb_ops_blank(int blank_mode
, struct fb_info
*info
)
1088 struct dlfb_data
*dev
= info
->par
;
1092 pr_info("/dev/fb%d FB_BLANK mode %d --> %d\n",
1093 info
->node
, dev
->blank_mode
, blank_mode
);
1095 if ((dev
->blank_mode
== FB_BLANK_POWERDOWN
) &&
1096 (blank_mode
!= FB_BLANK_POWERDOWN
)) {
1098 /* returning from powerdown requires a fresh modeset */
1099 dlfb_set_video_mode(dev
, &info
->var
);
1102 urb
= dlfb_get_urb(dev
);
1106 bufptr
= (char *) urb
->transfer_buffer
;
1107 bufptr
= dlfb_vidreg_lock(bufptr
);
1108 bufptr
= dlfb_blanking(bufptr
, blank_mode
);
1109 bufptr
= dlfb_vidreg_unlock(bufptr
);
1111 /* seems like a render op is needed to have blank change take effect */
1112 bufptr
= dlfb_dummy_render(bufptr
);
1114 dlfb_submit_urb(dev
, urb
, bufptr
-
1115 (char *) urb
->transfer_buffer
);
1117 dev
->blank_mode
= blank_mode
;
1122 static struct fb_ops dlfb_ops
= {
1123 .owner
= THIS_MODULE
,
1124 .fb_read
= fb_sys_read
,
1125 .fb_write
= dlfb_ops_write
,
1126 .fb_setcolreg
= dlfb_ops_setcolreg
,
1127 .fb_fillrect
= dlfb_ops_fillrect
,
1128 .fb_copyarea
= dlfb_ops_copyarea
,
1129 .fb_imageblit
= dlfb_ops_imageblit
,
1130 .fb_mmap
= dlfb_ops_mmap
,
1131 .fb_ioctl
= dlfb_ops_ioctl
,
1132 .fb_open
= dlfb_ops_open
,
1133 .fb_release
= dlfb_ops_release
,
1134 .fb_blank
= dlfb_ops_blank
,
1135 .fb_check_var
= dlfb_ops_check_var
,
1136 .fb_set_par
= dlfb_ops_set_par
,
1141 * Assumes &info->lock held by caller
1142 * Assumes no active clients have framebuffer open
1144 static int dlfb_realloc_framebuffer(struct dlfb_data
*dev
, struct fb_info
*info
)
1146 int retval
= -ENOMEM
;
1147 int old_len
= info
->fix
.smem_len
;
1149 unsigned char *old_fb
= info
->screen_base
;
1150 unsigned char *new_fb
;
1151 unsigned char *new_back
;
1153 pr_warn("Reallocating framebuffer. Addresses will change!\n");
1155 new_len
= info
->fix
.line_length
* info
->var
.yres
;
1157 if (PAGE_ALIGN(new_len
) > old_len
) {
1159 * Alloc system memory for virtual framebuffer
1161 new_fb
= vmalloc(new_len
);
1163 pr_err("Virtual framebuffer alloc failed\n");
1167 if (info
->screen_base
) {
1168 memcpy(new_fb
, old_fb
, old_len
);
1169 vfree(info
->screen_base
);
1172 info
->screen_base
= new_fb
;
1173 info
->fix
.smem_len
= PAGE_ALIGN(new_len
);
1174 info
->fix
.smem_start
= (unsigned long) new_fb
;
1175 info
->flags
= udlfb_info_flags
;
1178 * Second framebuffer copy to mirror the framebuffer state
1179 * on the physical USB device. We can function without this.
1180 * But with imperfect damage info we may send pixels over USB
1181 * that were, in fact, unchanged - wasting limited USB bandwidth
1183 new_back
= vzalloc(new_len
);
1185 pr_info("No shadow/backing buffer allocated\n");
1187 if (dev
->backing_buffer
)
1188 vfree(dev
->backing_buffer
);
1189 dev
->backing_buffer
= new_back
;
1200 * 1) Get EDID from hw, or use sw default
1201 * 2) Parse into various fb_info structs
1202 * 3) Allocate virtual framebuffer memory to back highest res mode
1204 * Parses EDID into three places used by various parts of fbdev:
1205 * fb_var_screeninfo contains the timing of the monitor's preferred mode
1206 * fb_info.monspecs is full parsed EDID info, including monspecs.modedb
1207 * fb_info.modelist is a linked list of all monitor & VESA modes which work
1209 * If EDID is not readable/valid, then modelist is all VESA modes,
1210 * monspecs is NULL, and fb_var_screeninfo is set to safe VESA mode
1211 * Returns 0 if successful
1213 static int dlfb_setup_modes(struct dlfb_data
*dev
,
1214 struct fb_info
*info
,
1215 char *default_edid
, size_t default_edid_size
)
1218 const struct fb_videomode
*default_vmode
= NULL
;
1223 if (info
->dev
) /* only use mutex if info has been registered */
1224 mutex_lock(&info
->lock
);
1226 edid
= kmalloc(EDID_LENGTH
, GFP_KERNEL
);
1232 fb_destroy_modelist(&info
->modelist
);
1233 memset(&info
->monspecs
, 0, sizeof(info
->monspecs
));
1236 * Try to (re)read EDID from hardware first
1237 * EDID data may return, but not parse as valid
1238 * Try again a few times, in case of e.g. analog cable noise
1242 i
= dlfb_get_edid(dev
, edid
, EDID_LENGTH
);
1244 if (i
>= EDID_LENGTH
)
1245 fb_edid_to_monspecs(edid
, &info
->monspecs
);
1247 if (info
->monspecs
.modedb_len
> 0) {
1254 /* If that fails, use a previously returned EDID if available */
1255 if (info
->monspecs
.modedb_len
== 0) {
1257 pr_err("Unable to get valid EDID from device/display\n");
1260 fb_edid_to_monspecs(dev
->edid
, &info
->monspecs
);
1261 if (info
->monspecs
.modedb_len
> 0)
1262 pr_err("Using previously queried EDID\n");
1266 /* If that fails, use the default EDID we were handed */
1267 if (info
->monspecs
.modedb_len
== 0) {
1268 if (default_edid_size
>= EDID_LENGTH
) {
1269 fb_edid_to_monspecs(default_edid
, &info
->monspecs
);
1270 if (info
->monspecs
.modedb_len
> 0) {
1271 memcpy(edid
, default_edid
, default_edid_size
);
1273 dev
->edid_size
= default_edid_size
;
1274 pr_err("Using default/backup EDID\n");
1279 /* If we've got modes, let's pick a best default mode */
1280 if (info
->monspecs
.modedb_len
> 0) {
1282 for (i
= 0; i
< info
->monspecs
.modedb_len
; i
++) {
1283 if (dlfb_is_valid_mode(&info
->monspecs
.modedb
[i
], info
))
1284 fb_add_videomode(&info
->monspecs
.modedb
[i
],
1288 /* if we've removed top/best mode */
1290 &= ~FB_MISC_1ST_DETAIL
;
1294 default_vmode
= fb_find_best_display(&info
->monspecs
,
1298 /* If everything else has failed, fall back to safe default mode */
1299 if (default_vmode
== NULL
) {
1301 struct fb_videomode fb_vmode
= {0};
1304 * Add the standard VESA modes to our modelist
1305 * Since we don't have EDID, there may be modes that
1306 * overspec monitor and/or are incorrect aspect ratio, etc.
1307 * But at least the user has a chance to choose
1309 for (i
= 0; i
< VESA_MODEDB_SIZE
; i
++) {
1310 if (dlfb_is_valid_mode((struct fb_videomode
*)
1311 &vesa_modes
[i
], info
))
1312 fb_add_videomode(&vesa_modes
[i
],
1317 * default to resolution safe for projectors
1318 * (since they are most common case without EDID)
1320 fb_vmode
.xres
= 800;
1321 fb_vmode
.yres
= 600;
1322 fb_vmode
.refresh
= 60;
1323 default_vmode
= fb_find_nearest_mode(&fb_vmode
,
1327 /* If we have good mode and no active clients*/
1328 if ((default_vmode
!= NULL
) && (dev
->fb_count
== 0)) {
1330 fb_videomode_to_var(&info
->var
, default_vmode
);
1331 dlfb_var_color_format(&info
->var
);
1334 * with mode size info, we can now alloc our framebuffer.
1336 memcpy(&info
->fix
, &dlfb_fix
, sizeof(dlfb_fix
));
1337 info
->fix
.line_length
= info
->var
.xres
*
1338 (info
->var
.bits_per_pixel
/ 8);
1340 result
= dlfb_realloc_framebuffer(dev
, info
);
1346 if (edid
&& (dev
->edid
!= edid
))
1350 mutex_unlock(&info
->lock
);
1355 static ssize_t
metrics_bytes_rendered_show(struct device
*fbdev
,
1356 struct device_attribute
*a
, char *buf
) {
1357 struct fb_info
*fb_info
= dev_get_drvdata(fbdev
);
1358 struct dlfb_data
*dev
= fb_info
->par
;
1359 return snprintf(buf
, PAGE_SIZE
, "%u\n",
1360 atomic_read(&dev
->bytes_rendered
));
1363 static ssize_t
metrics_bytes_identical_show(struct device
*fbdev
,
1364 struct device_attribute
*a
, char *buf
) {
1365 struct fb_info
*fb_info
= dev_get_drvdata(fbdev
);
1366 struct dlfb_data
*dev
= fb_info
->par
;
1367 return snprintf(buf
, PAGE_SIZE
, "%u\n",
1368 atomic_read(&dev
->bytes_identical
));
1371 static ssize_t
metrics_bytes_sent_show(struct device
*fbdev
,
1372 struct device_attribute
*a
, char *buf
) {
1373 struct fb_info
*fb_info
= dev_get_drvdata(fbdev
);
1374 struct dlfb_data
*dev
= fb_info
->par
;
1375 return snprintf(buf
, PAGE_SIZE
, "%u\n",
1376 atomic_read(&dev
->bytes_sent
));
1379 static ssize_t
metrics_cpu_kcycles_used_show(struct device
*fbdev
,
1380 struct device_attribute
*a
, char *buf
) {
1381 struct fb_info
*fb_info
= dev_get_drvdata(fbdev
);
1382 struct dlfb_data
*dev
= fb_info
->par
;
1383 return snprintf(buf
, PAGE_SIZE
, "%u\n",
1384 atomic_read(&dev
->cpu_kcycles_used
));
1387 static ssize_t
edid_show(
1389 struct kobject
*kobj
, struct bin_attribute
*a
,
1390 char *buf
, loff_t off
, size_t count
) {
1391 struct device
*fbdev
= container_of(kobj
, struct device
, kobj
);
1392 struct fb_info
*fb_info
= dev_get_drvdata(fbdev
);
1393 struct dlfb_data
*dev
= fb_info
->par
;
1395 if (dev
->edid
== NULL
)
1398 if ((off
>= dev
->edid_size
) || (count
> dev
->edid_size
))
1401 if (off
+ count
> dev
->edid_size
)
1402 count
= dev
->edid_size
- off
;
1404 pr_info("sysfs edid copy %p to %p, %d bytes\n",
1405 dev
->edid
, buf
, (int) count
);
1407 memcpy(buf
, dev
->edid
, count
);
1412 static ssize_t
edid_store(
1414 struct kobject
*kobj
, struct bin_attribute
*a
,
1415 char *src
, loff_t src_off
, size_t src_size
) {
1416 struct device
*fbdev
= container_of(kobj
, struct device
, kobj
);
1417 struct fb_info
*fb_info
= dev_get_drvdata(fbdev
);
1418 struct dlfb_data
*dev
= fb_info
->par
;
1420 /* We only support write of entire EDID at once, no offset*/
1421 if ((src_size
!= EDID_LENGTH
) || (src_off
!= 0))
1424 dlfb_setup_modes(dev
, fb_info
, src
, src_size
);
1426 if (dev
->edid
&& (memcmp(src
, dev
->edid
, src_size
) == 0)) {
1427 pr_info("sysfs written EDID is new default\n");
1428 dlfb_ops_set_par(fb_info
);
1434 static ssize_t
metrics_reset_store(struct device
*fbdev
,
1435 struct device_attribute
*attr
,
1436 const char *buf
, size_t count
)
1438 struct fb_info
*fb_info
= dev_get_drvdata(fbdev
);
1439 struct dlfb_data
*dev
= fb_info
->par
;
1441 atomic_set(&dev
->bytes_rendered
, 0);
1442 atomic_set(&dev
->bytes_identical
, 0);
1443 atomic_set(&dev
->bytes_sent
, 0);
1444 atomic_set(&dev
->cpu_kcycles_used
, 0);
1449 static struct bin_attribute edid_attr
= {
1450 .attr
.name
= "edid",
1452 .size
= EDID_LENGTH
,
1457 static struct device_attribute fb_device_attrs
[] = {
1458 __ATTR_RO(metrics_bytes_rendered
),
1459 __ATTR_RO(metrics_bytes_identical
),
1460 __ATTR_RO(metrics_bytes_sent
),
1461 __ATTR_RO(metrics_cpu_kcycles_used
),
1462 __ATTR(metrics_reset
, S_IWUSR
, NULL
, metrics_reset_store
),
1466 * This is necessary before we can communicate with the display controller.
1468 static int dlfb_select_std_channel(struct dlfb_data
*dev
)
1471 u8 set_def_chn
[] = { 0x57, 0xCD, 0xDC, 0xA7,
1472 0x1C, 0x88, 0x5E, 0x15,
1473 0x60, 0xFE, 0xC6, 0x97,
1474 0x16, 0x3D, 0x47, 0xF2 };
1476 ret
= usb_control_msg(dev
->udev
, usb_sndctrlpipe(dev
->udev
, 0),
1477 NR_USB_REQUEST_CHANNEL
,
1478 (USB_DIR_OUT
| USB_TYPE_VENDOR
), 0, 0,
1479 set_def_chn
, sizeof(set_def_chn
), USB_CTRL_SET_TIMEOUT
);
1483 static int dlfb_parse_vendor_descriptor(struct dlfb_data
*dev
,
1484 struct usb_interface
*interface
)
1492 buf
= kzalloc(MAX_VENDOR_DESCRIPTOR_SIZE
, GFP_KERNEL
);
1497 total_len
= usb_get_descriptor(interface_to_usbdev(interface
),
1498 0x5f, /* vendor specific */
1499 0, desc
, MAX_VENDOR_DESCRIPTOR_SIZE
);
1501 /* if not found, look in configuration descriptor */
1502 if (total_len
< 0) {
1503 if (0 == usb_get_extra_descriptor(interface
->cur_altsetting
,
1505 total_len
= (int) desc
[0];
1508 if (total_len
> 5) {
1509 pr_info("vendor descriptor length:%x data:%02x %02x %02x %02x" \
1510 "%02x %02x %02x %02x %02x %02x %02x\n",
1512 desc
[1], desc
[2], desc
[3], desc
[4], desc
[5], desc
[6],
1513 desc
[7], desc
[8], desc
[9], desc
[10]);
1515 if ((desc
[0] != total_len
) || /* descriptor length */
1516 (desc
[1] != 0x5f) || /* vendor descriptor type */
1517 (desc
[2] != 0x01) || /* version (2 bytes) */
1518 (desc
[3] != 0x00) ||
1519 (desc
[4] != total_len
- 2)) /* length after type */
1522 desc_end
= desc
+ total_len
;
1523 desc
+= 5; /* the fixed header we've already parsed */
1525 while (desc
< desc_end
) {
1529 key
= *((u16
*) desc
);
1530 desc
+= sizeof(u16
);
1535 case 0x0200: { /* max_area */
1537 max_area
= le32_to_cpu(*((u32
*)desc
));
1538 pr_warn("DL chip limited to %d pixel modes\n",
1540 dev
->sku_pixel_limit
= max_area
;
1549 pr_info("vendor descriptor not available (%d)\n", total_len
);
1555 /* allow udlfb to load for now even if firmware unrecognized */
1556 pr_err("Unrecognized vendor firmware descriptor\n");
1562 static int dlfb_usb_probe(struct usb_interface
*interface
,
1563 const struct usb_device_id
*id
)
1565 struct usb_device
*usbdev
;
1566 struct dlfb_data
*dev
= 0;
1567 struct fb_info
*info
= 0;
1568 int retval
= -ENOMEM
;
1571 /* usb initialization */
1573 usbdev
= interface_to_usbdev(interface
);
1575 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
1577 err("dlfb_usb_probe: failed alloc of dev struct\n");
1581 /* we need to wait for both usb and fbdev to spin down on disconnect */
1582 kref_init(&dev
->kref
); /* matching kref_put in usb .disconnect fn */
1583 kref_get(&dev
->kref
); /* matching kref_put in free_framebuffer_work */
1586 dev
->gdev
= &usbdev
->dev
; /* our generic struct device * */
1587 usb_set_intfdata(interface
, dev
);
1589 pr_info("%s %s - serial #%s\n",
1590 usbdev
->manufacturer
, usbdev
->product
, usbdev
->serial
);
1591 pr_info("vid_%04x&pid_%04x&rev_%04x driver's dlfb_data struct at %p\n",
1592 usbdev
->descriptor
.idVendor
, usbdev
->descriptor
.idProduct
,
1593 usbdev
->descriptor
.bcdDevice
, dev
);
1594 pr_info("console enable=%d\n", console
);
1595 pr_info("fb_defio enable=%d\n", fb_defio
);
1597 dev
->sku_pixel_limit
= 2048 * 1152; /* default to maximum */
1599 if (!dlfb_parse_vendor_descriptor(dev
, interface
)) {
1600 pr_err("firmware not recognized. Assume incompatible device\n");
1604 if (!dlfb_alloc_urb_list(dev
, WRITES_IN_FLIGHT
, MAX_TRANSFER
)) {
1606 pr_err("dlfb_alloc_urb_list failed\n");
1610 /* We don't register a new USB class. Our client interface is fbdev */
1612 /* allocates framebuffer driver structure, not framebuffer memory */
1613 info
= framebuffer_alloc(0, &usbdev
->dev
);
1616 pr_err("framebuffer_alloc failed\n");
1622 info
->pseudo_palette
= dev
->pseudo_palette
;
1623 info
->fbops
= &dlfb_ops
;
1625 retval
= fb_alloc_cmap(&info
->cmap
, 256, 0);
1627 pr_err("fb_alloc_cmap failed %x\n", retval
);
1631 INIT_DELAYED_WORK(&dev
->free_framebuffer_work
,
1632 dlfb_free_framebuffer_work
);
1634 INIT_LIST_HEAD(&info
->modelist
);
1636 retval
= dlfb_setup_modes(dev
, info
, NULL
, 0);
1638 pr_err("unable to find common mode for display and adapter\n");
1642 /* ready to begin using device */
1644 atomic_set(&dev
->usb_active
, 1);
1645 dlfb_select_std_channel(dev
);
1647 dlfb_ops_check_var(&info
->var
, info
);
1648 dlfb_ops_set_par(info
);
1650 retval
= register_framebuffer(info
);
1652 pr_err("register_framebuffer failed %d\n", retval
);
1656 for (i
= 0; i
< ARRAY_SIZE(fb_device_attrs
); i
++) {
1657 retval
= device_create_file(info
->dev
, &fb_device_attrs
[i
]);
1659 pr_err("device_create_file failed %d\n", retval
);
1664 retval
= device_create_bin_file(info
->dev
, &edid_attr
);
1666 pr_err("device_create_bin_file failed %d\n", retval
);
1670 pr_info("DisplayLink USB device /dev/fb%d attached. %dx%d resolution."
1671 " Using %dK framebuffer memory\n", info
->node
,
1672 info
->var
.xres
, info
->var
.yres
,
1673 ((dev
->backing_buffer
) ?
1674 info
->fix
.smem_len
* 2 : info
->fix
.smem_len
) >> 10);
1678 for (i
-= 1; i
>= 0; i
--)
1679 device_remove_file(info
->dev
, &fb_device_attrs
[i
]);
1685 if (info
->cmap
.len
!= 0)
1686 fb_dealloc_cmap(&info
->cmap
);
1687 if (info
->monspecs
.modedb
)
1688 fb_destroy_modedb(info
->monspecs
.modedb
);
1689 if (info
->screen_base
)
1690 vfree(info
->screen_base
);
1692 fb_destroy_modelist(&info
->modelist
);
1694 framebuffer_release(info
);
1697 if (dev
->backing_buffer
)
1698 vfree(dev
->backing_buffer
);
1700 kref_put(&dev
->kref
, dlfb_free
); /* ref for framebuffer */
1701 kref_put(&dev
->kref
, dlfb_free
); /* last ref from kref_init */
1703 /* dev has been deallocated. Do not dereference */
1709 static void dlfb_usb_disconnect(struct usb_interface
*interface
)
1711 struct dlfb_data
*dev
;
1712 struct fb_info
*info
;
1715 dev
= usb_get_intfdata(interface
);
1718 pr_info("USB disconnect starting\n");
1720 /* we virtualize until all fb clients release. Then we free */
1721 dev
->virtualized
= true;
1723 /* When non-active we'll update virtual framebuffer, but no new urbs */
1724 atomic_set(&dev
->usb_active
, 0);
1726 /* remove udlfb's sysfs interfaces */
1727 for (i
= 0; i
< ARRAY_SIZE(fb_device_attrs
); i
++)
1728 device_remove_file(info
->dev
, &fb_device_attrs
[i
]);
1729 device_remove_bin_file(info
->dev
, &edid_attr
);
1731 usb_set_intfdata(interface
, NULL
);
1733 /* if clients still have us open, will be freed on last close */
1734 if (dev
->fb_count
== 0)
1735 schedule_delayed_work(&dev
->free_framebuffer_work
, 0);
1737 /* release reference taken by kref_init in probe() */
1738 kref_put(&dev
->kref
, dlfb_free
);
1740 /* consider dlfb_data freed */
1745 static struct usb_driver dlfb_driver
= {
1747 .probe
= dlfb_usb_probe
,
1748 .disconnect
= dlfb_usb_disconnect
,
1749 .id_table
= id_table
,
1752 static int __init
dlfb_module_init(void)
1756 res
= usb_register(&dlfb_driver
);
1758 err("usb_register failed. Error number %d", res
);
1763 static void __exit
dlfb_module_exit(void)
1765 usb_deregister(&dlfb_driver
);
1768 module_init(dlfb_module_init
);
1769 module_exit(dlfb_module_exit
);
1771 static void dlfb_urb_completion(struct urb
*urb
)
1773 struct urb_node
*unode
= urb
->context
;
1774 struct dlfb_data
*dev
= unode
->dev
;
1775 unsigned long flags
;
1777 /* sync/async unlink faults aren't errors */
1779 if (!(urb
->status
== -ENOENT
||
1780 urb
->status
== -ECONNRESET
||
1781 urb
->status
== -ESHUTDOWN
)) {
1782 pr_err("%s - nonzero write bulk status received: %d\n",
1783 __func__
, urb
->status
);
1784 atomic_set(&dev
->lost_pixels
, 1);
1788 urb
->transfer_buffer_length
= dev
->urbs
.size
; /* reset to actual */
1790 spin_lock_irqsave(&dev
->urbs
.lock
, flags
);
1791 list_add_tail(&unode
->entry
, &dev
->urbs
.list
);
1792 dev
->urbs
.available
++;
1793 spin_unlock_irqrestore(&dev
->urbs
.lock
, flags
);
1796 * When using fb_defio, we deadlock if up() is called
1797 * while another is waiting. So queue to another process.
1800 schedule_delayed_work(&unode
->release_urb_work
, 0);
1802 up(&dev
->urbs
.limit_sem
);
1805 static void dlfb_free_urb_list(struct dlfb_data
*dev
)
1807 int count
= dev
->urbs
.count
;
1808 struct list_head
*node
;
1809 struct urb_node
*unode
;
1812 unsigned long flags
;
1814 pr_notice("Waiting for completes and freeing all render urbs\n");
1816 /* keep waiting and freeing, until we've got 'em all */
1819 /* Getting interrupted means a leak, but ok at shutdown*/
1820 ret
= down_interruptible(&dev
->urbs
.limit_sem
);
1824 spin_lock_irqsave(&dev
->urbs
.lock
, flags
);
1826 node
= dev
->urbs
.list
.next
; /* have reserved one with sem */
1827 list_del_init(node
);
1829 spin_unlock_irqrestore(&dev
->urbs
.lock
, flags
);
1831 unode
= list_entry(node
, struct urb_node
, entry
);
1834 /* Free each separately allocated piece */
1835 usb_free_coherent(urb
->dev
, dev
->urbs
.size
,
1836 urb
->transfer_buffer
, urb
->transfer_dma
);
1843 static int dlfb_alloc_urb_list(struct dlfb_data
*dev
, int count
, size_t size
)
1847 struct urb_node
*unode
;
1850 spin_lock_init(&dev
->urbs
.lock
);
1852 dev
->urbs
.size
= size
;
1853 INIT_LIST_HEAD(&dev
->urbs
.list
);
1856 unode
= kzalloc(sizeof(struct urb_node
), GFP_KERNEL
);
1861 INIT_DELAYED_WORK(&unode
->release_urb_work
,
1862 dlfb_release_urb_work
);
1864 urb
= usb_alloc_urb(0, GFP_KERNEL
);
1871 buf
= usb_alloc_coherent(dev
->udev
, MAX_TRANSFER
, GFP_KERNEL
,
1872 &urb
->transfer_dma
);
1879 /* urb->transfer_buffer_length set to actual before submit */
1880 usb_fill_bulk_urb(urb
, dev
->udev
, usb_sndbulkpipe(dev
->udev
, 1),
1881 buf
, size
, dlfb_urb_completion
, unode
);
1882 urb
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
1884 list_add_tail(&unode
->entry
, &dev
->urbs
.list
);
1889 sema_init(&dev
->urbs
.limit_sem
, i
);
1890 dev
->urbs
.count
= i
;
1891 dev
->urbs
.available
= i
;
1893 pr_notice("allocated %d %d byte urbs\n", i
, (int) size
);
1898 static struct urb
*dlfb_get_urb(struct dlfb_data
*dev
)
1901 struct list_head
*entry
;
1902 struct urb_node
*unode
;
1903 struct urb
*urb
= NULL
;
1904 unsigned long flags
;
1906 /* Wait for an in-flight buffer to complete and get re-queued */
1907 ret
= down_timeout(&dev
->urbs
.limit_sem
, GET_URB_TIMEOUT
);
1909 atomic_set(&dev
->lost_pixels
, 1);
1910 pr_warn("wait for urb interrupted: %x available: %d\n",
1911 ret
, dev
->urbs
.available
);
1915 spin_lock_irqsave(&dev
->urbs
.lock
, flags
);
1917 BUG_ON(list_empty(&dev
->urbs
.list
)); /* reserved one with limit_sem */
1918 entry
= dev
->urbs
.list
.next
;
1919 list_del_init(entry
);
1920 dev
->urbs
.available
--;
1922 spin_unlock_irqrestore(&dev
->urbs
.lock
, flags
);
1924 unode
= list_entry(entry
, struct urb_node
, entry
);
1931 static int dlfb_submit_urb(struct dlfb_data
*dev
, struct urb
*urb
, size_t len
)
1935 BUG_ON(len
> dev
->urbs
.size
);
1937 urb
->transfer_buffer_length
= len
; /* set to actual payload len */
1938 ret
= usb_submit_urb(urb
, GFP_KERNEL
);
1940 dlfb_urb_completion(urb
); /* because no one else will */
1941 atomic_set(&dev
->lost_pixels
, 1);
1942 pr_err("usb_submit_urb error %x\n", ret
);
1947 module_param(console
, bool, S_IWUSR
| S_IRUSR
| S_IWGRP
| S_IRGRP
);
1948 MODULE_PARM_DESC(console
, "Allow fbcon to consume first framebuffer found");
1950 module_param(fb_defio
, bool, S_IWUSR
| S_IRUSR
| S_IWGRP
| S_IRGRP
);
1951 MODULE_PARM_DESC(fb_defio
, "Enable fb_defio mmap support. *Experimental*");
1953 MODULE_AUTHOR("Roberto De Ioris <roberto@unbit.it>, "
1954 "Jaya Kumar <jayakumar.lkml@gmail.com>, "
1955 "Bernie Thompson <bernie@plugable.com>");
1956 MODULE_DESCRIPTION("DisplayLink kernel framebuffer driver");
1957 MODULE_LICENSE("GPL");