2 * udlfb.c -- Framebuffer driver for DisplayLink USB controller
4 * Copyright (C) 2009 Roberto De Ioris <roberto@unbit.it>
5 * Copyright (C) 2009 Jaya Kumar <jayakumar.lkml@gmail.com>
6 * Copyright (C) 2009 Bernie Thompson <bernie@plugable.com>
8 * This file is subject to the terms and conditions of the GNU General Public
9 * License v2. See the file COPYING in the main directory of this archive for
12 * Layout is based on skeletonfb by James Simmons and Geert Uytterhoeven,
13 * usb-skeleton by GregKH.
15 * Device-specific portions based on information from Displaylink, with work
16 * from Florian Echtler, Henrik Bjerregaard Pedersen, and others.
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21 #include <linux/module.h>
22 #include <linux/kernel.h>
23 #include <linux/init.h>
24 #include <linux/usb.h>
25 #include <linux/uaccess.h>
28 #include <linux/vmalloc.h>
29 #include <linux/slab.h>
30 #include <linux/prefetch.h>
31 #include <linux/delay.h>
32 #include <video/udlfb.h>
35 static struct fb_fix_screeninfo dlfb_fix
= {
37 .type
= FB_TYPE_PACKED_PIXELS
,
38 .visual
= FB_VISUAL_TRUECOLOR
,
42 .accel
= FB_ACCEL_NONE
,
45 static const u32 udlfb_info_flags
= FBINFO_DEFAULT
| FBINFO_READS_FAST
|
47 FBINFO_HWACCEL_IMAGEBLIT
| FBINFO_HWACCEL_FILLRECT
|
48 FBINFO_HWACCEL_COPYAREA
| FBINFO_MISC_ALWAYS_SETPAR
;
51 * There are many DisplayLink-based graphics products, all with unique PIDs.
52 * So we match on DisplayLink's VID + Vendor-Defined Interface Class (0xff)
53 * We also require a match on SubClass (0x00) and Protocol (0x00),
54 * which is compatible with all known USB 2.0 era graphics chips and firmware,
55 * but allows DisplayLink to increment those for any future incompatible chips
57 static struct usb_device_id id_table
[] = {
59 .bInterfaceClass
= 0xff,
60 .bInterfaceSubClass
= 0x00,
61 .bInterfaceProtocol
= 0x00,
62 .match_flags
= USB_DEVICE_ID_MATCH_VENDOR
|
63 USB_DEVICE_ID_MATCH_INT_CLASS
|
64 USB_DEVICE_ID_MATCH_INT_SUBCLASS
|
65 USB_DEVICE_ID_MATCH_INT_PROTOCOL
,
69 MODULE_DEVICE_TABLE(usb
, id_table
);
72 static int console
; /* Optionally allow fbcon to consume first framebuffer */
73 static int fb_defio
; /* Optionally enable experimental fb_defio mmap support */
75 /* dlfb keeps a list of urbs for efficient bulk transfers */
76 static void dlfb_urb_completion(struct urb
*urb
);
77 static struct urb
*dlfb_get_urb(struct dlfb_data
*dev
);
78 static int dlfb_submit_urb(struct dlfb_data
*dev
, struct urb
* urb
, size_t len
);
79 static int dlfb_alloc_urb_list(struct dlfb_data
*dev
, int count
, size_t size
);
80 static void dlfb_free_urb_list(struct dlfb_data
*dev
);
83 * All DisplayLink bulk operations start with 0xAF, followed by specific code
84 * All operations are written to buffers which then later get sent to device
86 static char *dlfb_set_register(char *buf
, u8 reg
, u8 val
)
95 static char *dlfb_vidreg_lock(char *buf
)
97 return dlfb_set_register(buf
, 0xFF, 0x00);
100 static char *dlfb_vidreg_unlock(char *buf
)
102 return dlfb_set_register(buf
, 0xFF, 0xFF);
106 * On/Off for driving the DisplayLink framebuffer to the display
107 * 0x00 H and V sync on
108 * 0x01 H and V sync off (screen blank but powered)
109 * 0x07 DPMS powerdown (requires modeset to come back)
111 static char *dlfb_enable_hvsync(char *buf
, bool enable
)
114 return dlfb_set_register(buf
, 0x1F, 0x00);
116 return dlfb_set_register(buf
, 0x1F, 0x07);
119 static char *dlfb_set_color_depth(char *buf
, u8 selection
)
121 return dlfb_set_register(buf
, 0x00, selection
);
124 static char *dlfb_set_base16bpp(char *wrptr
, u32 base
)
126 /* the base pointer is 16 bits wide, 0x20 is hi byte. */
127 wrptr
= dlfb_set_register(wrptr
, 0x20, base
>> 16);
128 wrptr
= dlfb_set_register(wrptr
, 0x21, base
>> 8);
129 return dlfb_set_register(wrptr
, 0x22, base
);
133 * DisplayLink HW has separate 16bpp and 8bpp framebuffers.
134 * In 24bpp modes, the low 323 RGB bits go in the 8bpp framebuffer
136 static char *dlfb_set_base8bpp(char *wrptr
, u32 base
)
138 wrptr
= dlfb_set_register(wrptr
, 0x26, base
>> 16);
139 wrptr
= dlfb_set_register(wrptr
, 0x27, base
>> 8);
140 return dlfb_set_register(wrptr
, 0x28, base
);
143 static char *dlfb_set_register_16(char *wrptr
, u8 reg
, u16 value
)
145 wrptr
= dlfb_set_register(wrptr
, reg
, value
>> 8);
146 return dlfb_set_register(wrptr
, reg
+1, value
);
150 * This is kind of weird because the controller takes some
151 * register values in a different byte order than other registers.
153 static char *dlfb_set_register_16be(char *wrptr
, u8 reg
, u16 value
)
155 wrptr
= dlfb_set_register(wrptr
, reg
, value
);
156 return dlfb_set_register(wrptr
, reg
+1, value
>> 8);
160 * LFSR is linear feedback shift register. The reason we have this is
161 * because the display controller needs to minimize the clock depth of
162 * various counters used in the display path. So this code reverses the
163 * provided value into the lfsr16 value by counting backwards to get
164 * the value that needs to be set in the hardware comparator to get the
165 * same actual count. This makes sense once you read above a couple of
166 * times and think about it from a hardware perspective.
168 static u16
dlfb_lfsr16(u16 actual_count
)
170 u32 lv
= 0xFFFF; /* This is the lfsr value that the hw starts with */
172 while (actual_count
--) {
174 (((lv
>> 15) ^ (lv
>> 4) ^ (lv
>> 2) ^ (lv
>> 1)) & 1))
182 * This does LFSR conversion on the value that is to be written.
183 * See LFSR explanation above for more detail.
185 static char *dlfb_set_register_lfsr16(char *wrptr
, u8 reg
, u16 value
)
187 return dlfb_set_register_16(wrptr
, reg
, dlfb_lfsr16(value
));
191 * This takes a standard fbdev screeninfo struct and all of its monitor mode
192 * details and converts them into the DisplayLink equivalent register commands.
194 static char *dlfb_set_vid_cmds(char *wrptr
, struct fb_var_screeninfo
*var
)
200 /* x display start */
201 xds
= var
->left_margin
+ var
->hsync_len
;
202 wrptr
= dlfb_set_register_lfsr16(wrptr
, 0x01, xds
);
204 xde
= xds
+ var
->xres
;
205 wrptr
= dlfb_set_register_lfsr16(wrptr
, 0x03, xde
);
207 /* y display start */
208 yds
= var
->upper_margin
+ var
->vsync_len
;
209 wrptr
= dlfb_set_register_lfsr16(wrptr
, 0x05, yds
);
211 yde
= yds
+ var
->yres
;
212 wrptr
= dlfb_set_register_lfsr16(wrptr
, 0x07, yde
);
214 /* x end count is active + blanking - 1 */
215 wrptr
= dlfb_set_register_lfsr16(wrptr
, 0x09,
216 xde
+ var
->right_margin
- 1);
218 /* libdlo hardcodes hsync start to 1 */
219 wrptr
= dlfb_set_register_lfsr16(wrptr
, 0x0B, 1);
221 /* hsync end is width of sync pulse + 1 */
222 wrptr
= dlfb_set_register_lfsr16(wrptr
, 0x0D, var
->hsync_len
+ 1);
224 /* hpixels is active pixels */
225 wrptr
= dlfb_set_register_16(wrptr
, 0x0F, var
->xres
);
227 /* yendcount is vertical active + vertical blanking */
228 yec
= var
->yres
+ var
->upper_margin
+ var
->lower_margin
+
230 wrptr
= dlfb_set_register_lfsr16(wrptr
, 0x11, yec
);
232 /* libdlo hardcodes vsync start to 0 */
233 wrptr
= dlfb_set_register_lfsr16(wrptr
, 0x13, 0);
235 /* vsync end is width of vsync pulse */
236 wrptr
= dlfb_set_register_lfsr16(wrptr
, 0x15, var
->vsync_len
);
238 /* vpixels is active pixels */
239 wrptr
= dlfb_set_register_16(wrptr
, 0x17, var
->yres
);
241 /* convert picoseconds to 5kHz multiple for pclk5k = x * 1E12/5k */
242 wrptr
= dlfb_set_register_16be(wrptr
, 0x1B,
243 200*1000*1000/var
->pixclock
);
249 * This takes a standard fbdev screeninfo struct that was fetched or prepared
250 * and then generates the appropriate command sequence that then drives the
251 * display controller.
253 static int dlfb_set_video_mode(struct dlfb_data
*dev
,
254 struct fb_var_screeninfo
*var
)
262 if (!atomic_read(&dev
->usb_active
))
265 urb
= dlfb_get_urb(dev
);
269 buf
= (char *) urb
->transfer_buffer
;
272 * This first section has to do with setting the base address on the
273 * controller * associated with the display. There are 2 base
274 * pointers, currently, we only * use the 16 bpp segment.
276 wrptr
= dlfb_vidreg_lock(buf
);
277 wrptr
= dlfb_set_color_depth(wrptr
, 0x00);
278 /* set base for 16bpp segment to 0 */
279 wrptr
= dlfb_set_base16bpp(wrptr
, 0);
280 /* set base for 8bpp segment to end of fb */
281 wrptr
= dlfb_set_base8bpp(wrptr
, dev
->info
->fix
.smem_len
);
283 wrptr
= dlfb_set_vid_cmds(wrptr
, var
);
284 wrptr
= dlfb_enable_hvsync(wrptr
, true);
285 wrptr
= dlfb_vidreg_unlock(wrptr
);
287 writesize
= wrptr
- buf
;
289 retval
= dlfb_submit_urb(dev
, urb
, writesize
);
294 static int dlfb_ops_mmap(struct fb_info
*info
, struct vm_area_struct
*vma
)
296 unsigned long start
= vma
->vm_start
;
297 unsigned long size
= vma
->vm_end
- vma
->vm_start
;
298 unsigned long offset
= vma
->vm_pgoff
<< PAGE_SHIFT
;
299 unsigned long page
, pos
;
301 if (offset
+ size
> info
->fix
.smem_len
)
304 pos
= (unsigned long)info
->fix
.smem_start
+ offset
;
306 pr_notice("mmap() framebuffer addr:%lu size:%lu\n",
310 page
= vmalloc_to_pfn((void *)pos
);
311 if (remap_pfn_range(vma
, start
, page
, PAGE_SIZE
, PAGE_SHARED
))
316 if (size
> PAGE_SIZE
)
322 vma
->vm_flags
|= VM_RESERVED
; /* avoid to swap out this VMA */
327 * Trims identical data from front and back of line
328 * Sets new front buffer address and width
329 * And returns byte count of identical pixels
330 * Assumes CPU natural alignment (unsigned long)
331 * for back and front buffer ptrs and width
333 static int dlfb_trim_hline(const u8
*bback
, const u8
**bfront
, int *width_bytes
)
336 const unsigned long *back
= (const unsigned long *) bback
;
337 const unsigned long *front
= (const unsigned long *) *bfront
;
338 const int width
= *width_bytes
/ sizeof(unsigned long);
339 int identical
= width
;
343 prefetch((void *) front
);
344 prefetch((void *) back
);
346 for (j
= 0; j
< width
; j
++) {
347 if (back
[j
] != front
[j
]) {
353 for (k
= width
- 1; k
> j
; k
--) {
354 if (back
[k
] != front
[k
]) {
360 identical
= start
+ (width
- end
);
361 *bfront
= (u8
*) &front
[start
];
362 *width_bytes
= (end
- start
) * sizeof(unsigned long);
364 return identical
* sizeof(unsigned long);
368 * Render a command stream for an encoded horizontal line segment of pixels.
370 * A command buffer holds several commands.
371 * It always begins with a fresh command header
372 * (the protocol doesn't require this, but we enforce it to allow
373 * multiple buffers to be potentially encoded and sent in parallel).
374 * A single command encodes one contiguous horizontal line of pixels
376 * The function relies on the client to do all allocation, so that
377 * rendering can be done directly to output buffers (e.g. USB URBs).
378 * The function fills the supplied command buffer, providing information
379 * on where it left off, so the client may call in again with additional
380 * buffers if the line will take several buffers to complete.
382 * A single command can transmit a maximum of 256 pixels,
383 * regardless of the compression ratio (protocol design limit).
384 * To the hardware, 0 for a size byte means 256
386 * Rather than 256 pixel commands which are either rl or raw encoded,
387 * the rlx command simply assumes alternating raw and rl spans within one cmd.
388 * This has a slightly larger header overhead, but produces more even results.
389 * It also processes all data (read and write) in a single pass.
390 * Performance benchmarks of common cases show it having just slightly better
391 * compression than 256 pixel raw or rle commands, with similar CPU consumpion.
392 * But for very rl friendly data, will compress not quite as well.
394 static void dlfb_compress_hline(
395 const uint16_t **pixel_start_ptr
,
396 const uint16_t *const pixel_end
,
397 uint32_t *device_address_ptr
,
398 uint8_t **command_buffer_ptr
,
399 const uint8_t *const cmd_buffer_end
)
401 const uint16_t *pixel
= *pixel_start_ptr
;
402 uint32_t dev_addr
= *device_address_ptr
;
403 uint8_t *cmd
= *command_buffer_ptr
;
406 while ((pixel_end
> pixel
) &&
407 (cmd_buffer_end
- MIN_RLX_CMD_BYTES
> cmd
)) {
408 uint8_t *raw_pixels_count_byte
= 0;
409 uint8_t *cmd_pixels_count_byte
= 0;
410 const uint16_t *raw_pixel_start
= 0;
411 const uint16_t *cmd_pixel_start
, *cmd_pixel_end
= 0;
413 prefetchw((void *) cmd
); /* pull in one cache line at least */
417 *cmd
++ = (uint8_t) ((dev_addr
>> 16) & 0xFF);
418 *cmd
++ = (uint8_t) ((dev_addr
>> 8) & 0xFF);
419 *cmd
++ = (uint8_t) ((dev_addr
) & 0xFF);
421 cmd_pixels_count_byte
= cmd
++; /* we'll know this later */
422 cmd_pixel_start
= pixel
;
424 raw_pixels_count_byte
= cmd
++; /* we'll know this later */
425 raw_pixel_start
= pixel
;
427 cmd_pixel_end
= pixel
+ min(MAX_CMD_PIXELS
+ 1,
428 min((int)(pixel_end
- pixel
),
429 (int)(cmd_buffer_end
- cmd
) / bpp
));
431 prefetch_range((void *) pixel
, (cmd_pixel_end
- pixel
) * bpp
);
433 while (pixel
< cmd_pixel_end
) {
434 const uint16_t * const repeating_pixel
= pixel
;
436 *(uint16_t *)cmd
= cpu_to_be16p(pixel
);
440 if (unlikely((pixel
< cmd_pixel_end
) &&
441 (*pixel
== *repeating_pixel
))) {
442 /* go back and fill in raw pixel count */
443 *raw_pixels_count_byte
= ((repeating_pixel
-
444 raw_pixel_start
) + 1) & 0xFF;
446 while ((pixel
< cmd_pixel_end
)
447 && (*pixel
== *repeating_pixel
)) {
451 /* immediately after raw data is repeat byte */
452 *cmd
++ = ((pixel
- repeating_pixel
) - 1) & 0xFF;
454 /* Then start another raw pixel span */
455 raw_pixel_start
= pixel
;
456 raw_pixels_count_byte
= cmd
++;
460 if (pixel
> raw_pixel_start
) {
461 /* finalize last RAW span */
462 *raw_pixels_count_byte
= (pixel
-raw_pixel_start
) & 0xFF;
465 *cmd_pixels_count_byte
= (pixel
- cmd_pixel_start
) & 0xFF;
466 dev_addr
+= (pixel
- cmd_pixel_start
) * bpp
;
469 if (cmd_buffer_end
<= MIN_RLX_CMD_BYTES
+ cmd
) {
470 /* Fill leftover bytes with no-ops */
471 if (cmd_buffer_end
> cmd
)
472 memset(cmd
, 0xAF, cmd_buffer_end
- cmd
);
473 cmd
= (uint8_t *) cmd_buffer_end
;
476 *command_buffer_ptr
= cmd
;
477 *pixel_start_ptr
= pixel
;
478 *device_address_ptr
= dev_addr
;
484 * There are 3 copies of every pixel: The front buffer that the fbdev
485 * client renders to, the actual framebuffer across the USB bus in hardware
486 * (that we can only write to, slowly, and can never read), and (optionally)
487 * our shadow copy that tracks what's been sent to that hardware buffer.
489 static int dlfb_render_hline(struct dlfb_data
*dev
, struct urb
**urb_ptr
,
490 const char *front
, char **urb_buf_ptr
,
491 u32 byte_offset
, u32 byte_width
,
492 int *ident_ptr
, int *sent_ptr
)
494 const u8
*line_start
, *line_end
, *next_pixel
;
495 u32 dev_addr
= dev
->base16
+ byte_offset
;
496 struct urb
*urb
= *urb_ptr
;
497 u8
*cmd
= *urb_buf_ptr
;
498 u8
*cmd_end
= (u8
*) urb
->transfer_buffer
+ urb
->transfer_buffer_length
;
500 line_start
= (u8
*) (front
+ byte_offset
);
501 next_pixel
= line_start
;
502 line_end
= next_pixel
+ byte_width
;
504 if (dev
->backing_buffer
) {
506 const u8
*back_start
= (u8
*) (dev
->backing_buffer
509 *ident_ptr
+= dlfb_trim_hline(back_start
, &next_pixel
,
512 offset
= next_pixel
- line_start
;
513 line_end
= next_pixel
+ byte_width
;
515 back_start
+= offset
;
516 line_start
+= offset
;
518 memcpy((char *)back_start
, (char *) line_start
,
522 while (next_pixel
< line_end
) {
524 dlfb_compress_hline((const uint16_t **) &next_pixel
,
525 (const uint16_t *) line_end
, &dev_addr
,
526 (u8
**) &cmd
, (u8
*) cmd_end
);
528 if (cmd
>= cmd_end
) {
529 int len
= cmd
- (u8
*) urb
->transfer_buffer
;
530 if (dlfb_submit_urb(dev
, urb
, len
))
531 return 1; /* lost pixels is set */
533 urb
= dlfb_get_urb(dev
);
535 return 1; /* lost_pixels is set */
537 cmd
= urb
->transfer_buffer
;
538 cmd_end
= &cmd
[urb
->transfer_buffer_length
];
547 int dlfb_handle_damage(struct dlfb_data
*dev
, int x
, int y
,
548 int width
, int height
, char *data
)
552 cycles_t start_cycles
, end_cycles
;
554 int bytes_identical
= 0;
558 start_cycles
= get_cycles();
560 aligned_x
= DL_ALIGN_DOWN(x
, sizeof(unsigned long));
561 width
= DL_ALIGN_UP(width
+ (x
-aligned_x
), sizeof(unsigned long));
565 (x
+ width
> dev
->info
->var
.xres
) ||
566 (y
+ height
> dev
->info
->var
.yres
))
569 if (!atomic_read(&dev
->usb_active
))
572 urb
= dlfb_get_urb(dev
);
575 cmd
= urb
->transfer_buffer
;
577 for (i
= y
; i
< y
+ height
; i
++) {
578 const int line_offset
= dev
->info
->fix
.line_length
* i
;
579 const int byte_offset
= line_offset
+ (x
* BPP
);
581 if (dlfb_render_hline(dev
, &urb
,
582 (char *) dev
->info
->fix
.smem_start
,
583 &cmd
, byte_offset
, width
* BPP
,
584 &bytes_identical
, &bytes_sent
))
588 if (cmd
> (char *) urb
->transfer_buffer
) {
589 /* Send partial buffer remaining before exiting */
590 int len
= cmd
- (char *) urb
->transfer_buffer
;
591 ret
= dlfb_submit_urb(dev
, urb
, len
);
594 dlfb_urb_completion(urb
);
597 atomic_add(bytes_sent
, &dev
->bytes_sent
);
598 atomic_add(bytes_identical
, &dev
->bytes_identical
);
599 atomic_add(width
*height
*2, &dev
->bytes_rendered
);
600 end_cycles
= get_cycles();
601 atomic_add(((unsigned int) ((end_cycles
- start_cycles
)
602 >> 10)), /* Kcycles */
603 &dev
->cpu_kcycles_used
);
609 * Path triggered by usermode clients who write to filesystem
610 * e.g. cat filename > /dev/fb1
611 * Not used by X Windows or text-mode console. But useful for testing.
612 * Slow because of extra copy and we must assume all pixels dirty.
614 static ssize_t
dlfb_ops_write(struct fb_info
*info
, const char __user
*buf
,
615 size_t count
, loff_t
*ppos
)
618 struct dlfb_data
*dev
= info
->par
;
619 u32 offset
= (u32
) *ppos
;
621 result
= fb_sys_write(info
, buf
, count
, ppos
);
624 int start
= max((int)(offset
/ info
->fix
.line_length
) - 1, 0);
625 int lines
= min((u32
)((result
/ info
->fix
.line_length
) + 1),
626 (u32
)info
->var
.yres
);
628 dlfb_handle_damage(dev
, 0, start
, info
->var
.xres
,
629 lines
, info
->screen_base
);
635 /* hardware has native COPY command (see libdlo), but not worth it for fbcon */
636 static void dlfb_ops_copyarea(struct fb_info
*info
,
637 const struct fb_copyarea
*area
)
640 struct dlfb_data
*dev
= info
->par
;
642 sys_copyarea(info
, area
);
644 dlfb_handle_damage(dev
, area
->dx
, area
->dy
,
645 area
->width
, area
->height
, info
->screen_base
);
648 static void dlfb_ops_imageblit(struct fb_info
*info
,
649 const struct fb_image
*image
)
651 struct dlfb_data
*dev
= info
->par
;
653 sys_imageblit(info
, image
);
655 dlfb_handle_damage(dev
, image
->dx
, image
->dy
,
656 image
->width
, image
->height
, info
->screen_base
);
659 static void dlfb_ops_fillrect(struct fb_info
*info
,
660 const struct fb_fillrect
*rect
)
662 struct dlfb_data
*dev
= info
->par
;
664 sys_fillrect(info
, rect
);
666 dlfb_handle_damage(dev
, rect
->dx
, rect
->dy
, rect
->width
,
667 rect
->height
, info
->screen_base
);
671 * NOTE: fb_defio.c is holding info->fbdefio.mutex
672 * Touching ANY framebuffer memory that triggers a page fault
673 * in fb_defio will cause a deadlock, when it also tries to
674 * grab the same mutex.
676 static void dlfb_dpy_deferred_io(struct fb_info
*info
,
677 struct list_head
*pagelist
)
680 struct fb_deferred_io
*fbdefio
= info
->fbdefio
;
681 struct dlfb_data
*dev
= info
->par
;
684 cycles_t start_cycles
, end_cycles
;
686 int bytes_identical
= 0;
687 int bytes_rendered
= 0;
692 if (!atomic_read(&dev
->usb_active
))
695 start_cycles
= get_cycles();
697 urb
= dlfb_get_urb(dev
);
701 cmd
= urb
->transfer_buffer
;
703 /* walk the written page list and render each to device */
704 list_for_each_entry(cur
, &fbdefio
->pagelist
, lru
) {
706 if (dlfb_render_hline(dev
, &urb
, (char *) info
->fix
.smem_start
,
707 &cmd
, cur
->index
<< PAGE_SHIFT
,
708 PAGE_SIZE
, &bytes_identical
, &bytes_sent
))
710 bytes_rendered
+= PAGE_SIZE
;
713 if (cmd
> (char *) urb
->transfer_buffer
) {
714 /* Send partial buffer remaining before exiting */
715 int len
= cmd
- (char *) urb
->transfer_buffer
;
716 dlfb_submit_urb(dev
, urb
, len
);
719 dlfb_urb_completion(urb
);
722 atomic_add(bytes_sent
, &dev
->bytes_sent
);
723 atomic_add(bytes_identical
, &dev
->bytes_identical
);
724 atomic_add(bytes_rendered
, &dev
->bytes_rendered
);
725 end_cycles
= get_cycles();
726 atomic_add(((unsigned int) ((end_cycles
- start_cycles
)
727 >> 10)), /* Kcycles */
728 &dev
->cpu_kcycles_used
);
731 static int dlfb_get_edid(struct dlfb_data
*dev
, char *edid
, int len
)
737 rbuf
= kmalloc(2, GFP_KERNEL
);
741 for (i
= 0; i
< len
; i
++) {
742 ret
= usb_control_msg(dev
->udev
,
743 usb_rcvctrlpipe(dev
->udev
, 0), (0x02),
744 (0x80 | (0x02 << 5)), i
<< 8, 0xA1, rbuf
, 2,
747 pr_err("Read EDID byte %d failed err %x\n", i
, ret
);
759 static int dlfb_ops_ioctl(struct fb_info
*info
, unsigned int cmd
,
763 struct dlfb_data
*dev
= info
->par
;
764 struct dloarea
*area
= NULL
;
766 if (!atomic_read(&dev
->usb_active
))
769 /* TODO: Update X server to get this from sysfs instead */
770 if (cmd
== DLFB_IOCTL_RETURN_EDID
) {
771 char *edid
= (char *)arg
;
772 if (copy_to_user(edid
, dev
->edid
, dev
->edid_size
))
777 /* TODO: Help propose a standard fb.h ioctl to report mmap damage */
778 if (cmd
== DLFB_IOCTL_REPORT_DAMAGE
) {
781 * If we have a damage-aware client, turn fb_defio "off"
782 * To avoid perf imact of unnecessary page fault handling.
783 * Done by resetting the delay for this fb_info to a very
784 * long period. Pages will become writable and stay that way.
785 * Reset to normal value when all clients have closed this fb.
788 info
->fbdefio
->delay
= DL_DEFIO_WRITE_DISABLE
;
790 area
= (struct dloarea
*)arg
;
795 if (area
->x
> info
->var
.xres
)
796 area
->x
= info
->var
.xres
;
801 if (area
->y
> info
->var
.yres
)
802 area
->y
= info
->var
.yres
;
804 dlfb_handle_damage(dev
, area
->x
, area
->y
, area
->w
, area
->h
,
811 /* taken from vesafb */
813 dlfb_ops_setcolreg(unsigned regno
, unsigned red
, unsigned green
,
814 unsigned blue
, unsigned transp
, struct fb_info
*info
)
818 if (regno
>= info
->cmap
.len
)
822 if (info
->var
.red
.offset
== 10) {
824 ((u32
*) (info
->pseudo_palette
))[regno
] =
825 ((red
& 0xf800) >> 1) |
826 ((green
& 0xf800) >> 6) | ((blue
& 0xf800) >> 11);
829 ((u32
*) (info
->pseudo_palette
))[regno
] =
831 ((green
& 0xfc00) >> 5) | ((blue
& 0xf800) >> 11);
839 * It's common for several clients to have framebuffer open simultaneously.
840 * e.g. both fbcon and X. Makes things interesting.
841 * Assumes caller is holding info->lock (for open and release at least)
843 static int dlfb_ops_open(struct fb_info
*info
, int user
)
845 struct dlfb_data
*dev
= info
->par
;
848 * fbcon aggressively connects to first framebuffer it finds,
849 * preventing other clients (X) from working properly. Usually
850 * not what the user wants. Fail by default with option to enable.
852 if ((user
== 0) & (!console
))
855 /* If the USB device is gone, we don't accept new opens */
856 if (dev
->virtualized
)
861 kref_get(&dev
->kref
);
863 if (fb_defio
&& (info
->fbdefio
== NULL
)) {
864 /* enable defio at last moment if not disabled by client */
866 struct fb_deferred_io
*fbdefio
;
868 fbdefio
= kmalloc(sizeof(struct fb_deferred_io
), GFP_KERNEL
);
871 fbdefio
->delay
= DL_DEFIO_WRITE_DELAY
;
872 fbdefio
->deferred_io
= dlfb_dpy_deferred_io
;
875 info
->fbdefio
= fbdefio
;
876 fb_deferred_io_init(info
);
879 pr_notice("open /dev/fb%d user=%d fb_info=%p count=%d\n",
880 info
->node
, user
, info
, dev
->fb_count
);
886 * Called when all client interfaces to start transactions have been disabled,
887 * and all references to our device instance (dlfb_data) are released.
888 * Every transaction must have a reference, so we know are fully spun down
890 static void dlfb_free(struct kref
*kref
)
892 struct dlfb_data
*dev
= container_of(kref
, struct dlfb_data
, kref
);
894 /* this function will wait for all in-flight urbs to complete */
895 if (dev
->urbs
.count
> 0)
896 dlfb_free_urb_list(dev
);
898 if (dev
->backing_buffer
)
899 vfree(dev
->backing_buffer
);
903 pr_warn("freeing dlfb_data %p\n", dev
);
908 static void dlfb_release_urb_work(struct work_struct
*work
)
910 struct urb_node
*unode
= container_of(work
, struct urb_node
,
911 release_urb_work
.work
);
913 up(&unode
->dev
->urbs
.limit_sem
);
916 static void dlfb_free_framebuffer_work(struct work_struct
*work
)
918 struct dlfb_data
*dev
= container_of(work
, struct dlfb_data
,
919 free_framebuffer_work
.work
);
920 struct fb_info
*info
= dev
->info
;
921 int node
= info
->node
;
923 unregister_framebuffer(info
);
925 if (info
->cmap
.len
!= 0)
926 fb_dealloc_cmap(&info
->cmap
);
927 if (info
->monspecs
.modedb
)
928 fb_destroy_modedb(info
->monspecs
.modedb
);
929 if (info
->screen_base
)
930 vfree(info
->screen_base
);
932 fb_destroy_modelist(&info
->modelist
);
936 /* Assume info structure is freed after this point */
937 framebuffer_release(info
);
939 pr_warn("fb_info for /dev/fb%d has been freed\n", node
);
941 /* ref taken in probe() as part of registering framebfufer */
942 kref_put(&dev
->kref
, dlfb_free
);
946 * Assumes caller is holding info->lock mutex (for open and release at least)
948 static int dlfb_ops_release(struct fb_info
*info
, int user
)
950 struct dlfb_data
*dev
= info
->par
;
954 /* We can't free fb_info here - fbmem will touch it when we return */
955 if (dev
->virtualized
&& (dev
->fb_count
== 0))
956 schedule_delayed_work(&dev
->free_framebuffer_work
, HZ
);
958 if ((dev
->fb_count
== 0) && (info
->fbdefio
)) {
959 fb_deferred_io_cleanup(info
);
960 kfree(info
->fbdefio
);
961 info
->fbdefio
= NULL
;
962 info
->fbops
->fb_mmap
= dlfb_ops_mmap
;
965 pr_warn("released /dev/fb%d user=%d count=%d\n",
966 info
->node
, user
, dev
->fb_count
);
968 kref_put(&dev
->kref
, dlfb_free
);
974 * Check whether a video mode is supported by the DisplayLink chip
975 * We start from monitor's modes, so don't need to filter that here
977 static int dlfb_is_valid_mode(struct fb_videomode
*mode
,
978 struct fb_info
*info
)
980 struct dlfb_data
*dev
= info
->par
;
982 if (mode
->xres
* mode
->yres
> dev
->sku_pixel_limit
) {
983 pr_warn("%dx%d beyond chip capabilities\n",
984 mode
->xres
, mode
->yres
);
988 pr_info("%dx%d valid mode\n", mode
->xres
, mode
->yres
);
993 static void dlfb_var_color_format(struct fb_var_screeninfo
*var
)
995 const struct fb_bitfield red
= { 11, 5, 0 };
996 const struct fb_bitfield green
= { 5, 6, 0 };
997 const struct fb_bitfield blue
= { 0, 5, 0 };
999 var
->bits_per_pixel
= 16;
1005 static int dlfb_ops_check_var(struct fb_var_screeninfo
*var
,
1006 struct fb_info
*info
)
1008 struct fb_videomode mode
;
1010 /* TODO: support dynamically changing framebuffer size */
1011 if ((var
->xres
* var
->yres
* 2) > info
->fix
.smem_len
)
1014 /* set device-specific elements of var unrelated to mode */
1015 dlfb_var_color_format(var
);
1017 fb_var_to_videomode(&mode
, var
);
1019 if (!dlfb_is_valid_mode(&mode
, info
))
1025 static int dlfb_ops_set_par(struct fb_info
*info
)
1027 struct dlfb_data
*dev
= info
->par
;
1029 u16
*pix_framebuffer
;
1032 pr_notice("set_par mode %dx%d\n", info
->var
.xres
, info
->var
.yres
);
1034 result
= dlfb_set_video_mode(dev
, &info
->var
);
1036 if ((result
== 0) && (dev
->fb_count
== 0)) {
1038 /* paint greenscreen */
1040 pix_framebuffer
= (u16
*) info
->screen_base
;
1041 for (i
= 0; i
< info
->fix
.smem_len
/ 2; i
++)
1042 pix_framebuffer
[i
] = 0x37e6;
1044 dlfb_handle_damage(dev
, 0, 0, info
->var
.xres
, info
->var
.yres
,
1052 * In order to come back from full DPMS off, we need to set the mode again
1054 static int dlfb_ops_blank(int blank_mode
, struct fb_info
*info
)
1056 struct dlfb_data
*dev
= info
->par
;
1058 if (blank_mode
!= FB_BLANK_UNBLANK
) {
1062 urb
= dlfb_get_urb(dev
);
1066 bufptr
= (char *) urb
->transfer_buffer
;
1067 bufptr
= dlfb_vidreg_lock(bufptr
);
1068 bufptr
= dlfb_enable_hvsync(bufptr
, false);
1069 bufptr
= dlfb_vidreg_unlock(bufptr
);
1071 dlfb_submit_urb(dev
, urb
, bufptr
-
1072 (char *) urb
->transfer_buffer
);
1074 dlfb_set_video_mode(dev
, &info
->var
);
1080 static struct fb_ops dlfb_ops
= {
1081 .owner
= THIS_MODULE
,
1082 .fb_read
= fb_sys_read
,
1083 .fb_write
= dlfb_ops_write
,
1084 .fb_setcolreg
= dlfb_ops_setcolreg
,
1085 .fb_fillrect
= dlfb_ops_fillrect
,
1086 .fb_copyarea
= dlfb_ops_copyarea
,
1087 .fb_imageblit
= dlfb_ops_imageblit
,
1088 .fb_mmap
= dlfb_ops_mmap
,
1089 .fb_ioctl
= dlfb_ops_ioctl
,
1090 .fb_open
= dlfb_ops_open
,
1091 .fb_release
= dlfb_ops_release
,
1092 .fb_blank
= dlfb_ops_blank
,
1093 .fb_check_var
= dlfb_ops_check_var
,
1094 .fb_set_par
= dlfb_ops_set_par
,
1099 * Assumes &info->lock held by caller
1100 * Assumes no active clients have framebuffer open
1102 static int dlfb_realloc_framebuffer(struct dlfb_data
*dev
, struct fb_info
*info
)
1104 int retval
= -ENOMEM
;
1105 int old_len
= info
->fix
.smem_len
;
1107 unsigned char *old_fb
= info
->screen_base
;
1108 unsigned char *new_fb
;
1109 unsigned char *new_back
;
1111 pr_warn("Reallocating framebuffer. Addresses will change!\n");
1113 new_len
= info
->fix
.line_length
* info
->var
.yres
;
1115 if (PAGE_ALIGN(new_len
) > old_len
) {
1117 * Alloc system memory for virtual framebuffer
1119 new_fb
= vmalloc(new_len
);
1121 pr_err("Virtual framebuffer alloc failed\n");
1125 if (info
->screen_base
) {
1126 memcpy(new_fb
, old_fb
, old_len
);
1127 vfree(info
->screen_base
);
1130 info
->screen_base
= new_fb
;
1131 info
->fix
.smem_len
= PAGE_ALIGN(new_len
);
1132 info
->fix
.smem_start
= (unsigned long) new_fb
;
1133 info
->flags
= udlfb_info_flags
;
1136 * Second framebuffer copy to mirror the framebuffer state
1137 * on the physical USB device. We can function without this.
1138 * But with imperfect damage info we may send pixels over USB
1139 * that were, in fact, unchanged - wasting limited USB bandwidth
1141 new_back
= vzalloc(new_len
);
1143 pr_info("No shadow/backing buffer allocated\n");
1145 if (dev
->backing_buffer
)
1146 vfree(dev
->backing_buffer
);
1147 dev
->backing_buffer
= new_back
;
1158 * 1) Get EDID from hw, or use sw default
1159 * 2) Parse into various fb_info structs
1160 * 3) Allocate virtual framebuffer memory to back highest res mode
1162 * Parses EDID into three places used by various parts of fbdev:
1163 * fb_var_screeninfo contains the timing of the monitor's preferred mode
1164 * fb_info.monspecs is full parsed EDID info, including monspecs.modedb
1165 * fb_info.modelist is a linked list of all monitor & VESA modes which work
1167 * If EDID is not readable/valid, then modelist is all VESA modes,
1168 * monspecs is NULL, and fb_var_screeninfo is set to safe VESA mode
1169 * Returns 0 if successful
1171 static int dlfb_setup_modes(struct dlfb_data
*dev
,
1172 struct fb_info
*info
,
1173 char *default_edid
, size_t default_edid_size
)
1176 const struct fb_videomode
*default_vmode
= NULL
;
1181 if (info
->dev
) /* only use mutex if info has been registered */
1182 mutex_lock(&info
->lock
);
1184 edid
= kmalloc(EDID_LENGTH
, GFP_KERNEL
);
1190 fb_destroy_modelist(&info
->modelist
);
1191 memset(&info
->monspecs
, 0, sizeof(info
->monspecs
));
1194 * Try to (re)read EDID from hardware first
1195 * EDID data may return, but not parse as valid
1196 * Try again a few times, in case of e.g. analog cable noise
1200 i
= dlfb_get_edid(dev
, edid
, EDID_LENGTH
);
1202 if (i
>= EDID_LENGTH
)
1203 fb_edid_to_monspecs(edid
, &info
->monspecs
);
1205 if (info
->monspecs
.modedb_len
> 0) {
1212 /* If that fails, use a previously returned EDID if available */
1213 if (info
->monspecs
.modedb_len
== 0) {
1215 pr_err("Unable to get valid EDID from device/display\n");
1218 fb_edid_to_monspecs(dev
->edid
, &info
->monspecs
);
1219 if (info
->monspecs
.modedb_len
> 0)
1220 pr_err("Using previously queried EDID\n");
1224 /* If that fails, use the default EDID we were handed */
1225 if (info
->monspecs
.modedb_len
== 0) {
1226 if (default_edid_size
>= EDID_LENGTH
) {
1227 fb_edid_to_monspecs(default_edid
, &info
->monspecs
);
1228 if (info
->monspecs
.modedb_len
> 0) {
1229 memcpy(edid
, default_edid
, default_edid_size
);
1231 dev
->edid_size
= default_edid_size
;
1232 pr_err("Using default/backup EDID\n");
1237 /* If we've got modes, let's pick a best default mode */
1238 if (info
->monspecs
.modedb_len
> 0) {
1240 for (i
= 0; i
< info
->monspecs
.modedb_len
; i
++) {
1241 if (dlfb_is_valid_mode(&info
->monspecs
.modedb
[i
], info
))
1242 fb_add_videomode(&info
->monspecs
.modedb
[i
],
1246 /* if we've removed top/best mode */
1248 &= ~FB_MISC_1ST_DETAIL
;
1252 default_vmode
= fb_find_best_display(&info
->monspecs
,
1256 /* If everything else has failed, fall back to safe default mode */
1257 if (default_vmode
== NULL
) {
1259 struct fb_videomode fb_vmode
= {0};
1262 * Add the standard VESA modes to our modelist
1263 * Since we don't have EDID, there may be modes that
1264 * overspec monitor and/or are incorrect aspect ratio, etc.
1265 * But at least the user has a chance to choose
1267 for (i
= 0; i
< VESA_MODEDB_SIZE
; i
++) {
1268 if (dlfb_is_valid_mode((struct fb_videomode
*)
1269 &vesa_modes
[i
], info
))
1270 fb_add_videomode(&vesa_modes
[i
],
1275 * default to resolution safe for projectors
1276 * (since they are most common case without EDID)
1278 fb_vmode
.xres
= 800;
1279 fb_vmode
.yres
= 600;
1280 fb_vmode
.refresh
= 60;
1281 default_vmode
= fb_find_nearest_mode(&fb_vmode
,
1285 /* If we have good mode and no active clients*/
1286 if ((default_vmode
!= NULL
) && (dev
->fb_count
== 0)) {
1288 fb_videomode_to_var(&info
->var
, default_vmode
);
1289 dlfb_var_color_format(&info
->var
);
1292 * with mode size info, we can now alloc our framebuffer.
1294 memcpy(&info
->fix
, &dlfb_fix
, sizeof(dlfb_fix
));
1295 info
->fix
.line_length
= info
->var
.xres
*
1296 (info
->var
.bits_per_pixel
/ 8);
1298 result
= dlfb_realloc_framebuffer(dev
, info
);
1304 if (edid
&& (dev
->edid
!= edid
))
1308 mutex_unlock(&info
->lock
);
1313 static ssize_t
metrics_bytes_rendered_show(struct device
*fbdev
,
1314 struct device_attribute
*a
, char *buf
) {
1315 struct fb_info
*fb_info
= dev_get_drvdata(fbdev
);
1316 struct dlfb_data
*dev
= fb_info
->par
;
1317 return snprintf(buf
, PAGE_SIZE
, "%u\n",
1318 atomic_read(&dev
->bytes_rendered
));
1321 static ssize_t
metrics_bytes_identical_show(struct device
*fbdev
,
1322 struct device_attribute
*a
, char *buf
) {
1323 struct fb_info
*fb_info
= dev_get_drvdata(fbdev
);
1324 struct dlfb_data
*dev
= fb_info
->par
;
1325 return snprintf(buf
, PAGE_SIZE
, "%u\n",
1326 atomic_read(&dev
->bytes_identical
));
1329 static ssize_t
metrics_bytes_sent_show(struct device
*fbdev
,
1330 struct device_attribute
*a
, char *buf
) {
1331 struct fb_info
*fb_info
= dev_get_drvdata(fbdev
);
1332 struct dlfb_data
*dev
= fb_info
->par
;
1333 return snprintf(buf
, PAGE_SIZE
, "%u\n",
1334 atomic_read(&dev
->bytes_sent
));
1337 static ssize_t
metrics_cpu_kcycles_used_show(struct device
*fbdev
,
1338 struct device_attribute
*a
, char *buf
) {
1339 struct fb_info
*fb_info
= dev_get_drvdata(fbdev
);
1340 struct dlfb_data
*dev
= fb_info
->par
;
1341 return snprintf(buf
, PAGE_SIZE
, "%u\n",
1342 atomic_read(&dev
->cpu_kcycles_used
));
1345 static ssize_t
edid_show(
1347 struct kobject
*kobj
, struct bin_attribute
*a
,
1348 char *buf
, loff_t off
, size_t count
) {
1349 struct device
*fbdev
= container_of(kobj
, struct device
, kobj
);
1350 struct fb_info
*fb_info
= dev_get_drvdata(fbdev
);
1351 struct dlfb_data
*dev
= fb_info
->par
;
1353 if (dev
->edid
== NULL
)
1356 if ((off
>= dev
->edid_size
) || (count
> dev
->edid_size
))
1359 if (off
+ count
> dev
->edid_size
)
1360 count
= dev
->edid_size
- off
;
1362 pr_info("sysfs edid copy %p to %p, %d bytes\n",
1363 dev
->edid
, buf
, (int) count
);
1365 memcpy(buf
, dev
->edid
, count
);
1370 static ssize_t
edid_store(
1372 struct kobject
*kobj
, struct bin_attribute
*a
,
1373 char *src
, loff_t src_off
, size_t src_size
) {
1374 struct device
*fbdev
= container_of(kobj
, struct device
, kobj
);
1375 struct fb_info
*fb_info
= dev_get_drvdata(fbdev
);
1376 struct dlfb_data
*dev
= fb_info
->par
;
1378 /* We only support write of entire EDID at once, no offset*/
1379 if ((src_size
!= EDID_LENGTH
) || (src_off
!= 0))
1382 dlfb_setup_modes(dev
, fb_info
, src
, src_size
);
1384 if (dev
->edid
&& (memcmp(src
, dev
->edid
, src_size
) == 0)) {
1385 pr_info("sysfs written EDID is new default\n");
1386 dlfb_ops_set_par(fb_info
);
1392 static ssize_t
metrics_reset_store(struct device
*fbdev
,
1393 struct device_attribute
*attr
,
1394 const char *buf
, size_t count
)
1396 struct fb_info
*fb_info
= dev_get_drvdata(fbdev
);
1397 struct dlfb_data
*dev
= fb_info
->par
;
1399 atomic_set(&dev
->bytes_rendered
, 0);
1400 atomic_set(&dev
->bytes_identical
, 0);
1401 atomic_set(&dev
->bytes_sent
, 0);
1402 atomic_set(&dev
->cpu_kcycles_used
, 0);
1407 static struct bin_attribute edid_attr
= {
1408 .attr
.name
= "edid",
1410 .size
= EDID_LENGTH
,
1415 static struct device_attribute fb_device_attrs
[] = {
1416 __ATTR_RO(metrics_bytes_rendered
),
1417 __ATTR_RO(metrics_bytes_identical
),
1418 __ATTR_RO(metrics_bytes_sent
),
1419 __ATTR_RO(metrics_cpu_kcycles_used
),
1420 __ATTR(metrics_reset
, S_IWUSR
, NULL
, metrics_reset_store
),
1424 * This is necessary before we can communicate with the display controller.
1426 static int dlfb_select_std_channel(struct dlfb_data
*dev
)
1429 u8 set_def_chn
[] = { 0x57, 0xCD, 0xDC, 0xA7,
1430 0x1C, 0x88, 0x5E, 0x15,
1431 0x60, 0xFE, 0xC6, 0x97,
1432 0x16, 0x3D, 0x47, 0xF2 };
1434 ret
= usb_control_msg(dev
->udev
, usb_sndctrlpipe(dev
->udev
, 0),
1435 NR_USB_REQUEST_CHANNEL
,
1436 (USB_DIR_OUT
| USB_TYPE_VENDOR
), 0, 0,
1437 set_def_chn
, sizeof(set_def_chn
), USB_CTRL_SET_TIMEOUT
);
1441 static int dlfb_parse_vendor_descriptor(struct dlfb_data
*dev
,
1442 struct usb_device
*usbdev
)
1450 buf
= kzalloc(MAX_VENDOR_DESCRIPTOR_SIZE
, GFP_KERNEL
);
1455 total_len
= usb_get_descriptor(usbdev
, 0x5f, /* vendor specific */
1456 0, desc
, MAX_VENDOR_DESCRIPTOR_SIZE
);
1457 if (total_len
> 5) {
1458 pr_info("vendor descriptor length:%x data:%02x %02x %02x %02x" \
1459 "%02x %02x %02x %02x %02x %02x %02x\n",
1461 desc
[1], desc
[2], desc
[3], desc
[4], desc
[5], desc
[6],
1462 desc
[7], desc
[8], desc
[9], desc
[10]);
1464 if ((desc
[0] != total_len
) || /* descriptor length */
1465 (desc
[1] != 0x5f) || /* vendor descriptor type */
1466 (desc
[2] != 0x01) || /* version (2 bytes) */
1467 (desc
[3] != 0x00) ||
1468 (desc
[4] != total_len
- 2)) /* length after type */
1471 desc_end
= desc
+ total_len
;
1472 desc
+= 5; /* the fixed header we've already parsed */
1474 while (desc
< desc_end
) {
1478 key
= *((u16
*) desc
);
1479 desc
+= sizeof(u16
);
1484 case 0x0200: { /* max_area */
1486 max_area
= le32_to_cpu(*((u32
*)desc
));
1487 pr_warn("DL chip limited to %d pixel modes\n",
1489 dev
->sku_pixel_limit
= max_area
;
1502 /* allow udlfb to load for now even if firmware unrecognized */
1503 pr_err("Unrecognized vendor firmware descriptor\n");
1509 static int dlfb_usb_probe(struct usb_interface
*interface
,
1510 const struct usb_device_id
*id
)
1512 struct usb_device
*usbdev
;
1513 struct dlfb_data
*dev
= 0;
1514 struct fb_info
*info
= 0;
1515 int retval
= -ENOMEM
;
1518 /* usb initialization */
1520 usbdev
= interface_to_usbdev(interface
);
1522 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
1524 err("dlfb_usb_probe: failed alloc of dev struct\n");
1528 /* we need to wait for both usb and fbdev to spin down on disconnect */
1529 kref_init(&dev
->kref
); /* matching kref_put in usb .disconnect fn */
1530 kref_get(&dev
->kref
); /* matching kref_put in free_framebuffer_work */
1533 dev
->gdev
= &usbdev
->dev
; /* our generic struct device * */
1534 usb_set_intfdata(interface
, dev
);
1536 pr_info("%s %s - serial #%s\n",
1537 usbdev
->manufacturer
, usbdev
->product
, usbdev
->serial
);
1538 pr_info("vid_%04x&pid_%04x&rev_%04x driver's dlfb_data struct at %p\n",
1539 usbdev
->descriptor
.idVendor
, usbdev
->descriptor
.idProduct
,
1540 usbdev
->descriptor
.bcdDevice
, dev
);
1541 pr_info("console enable=%d\n", console
);
1542 pr_info("fb_defio enable=%d\n", fb_defio
);
1544 dev
->sku_pixel_limit
= 2048 * 1152; /* default to maximum */
1546 if (!dlfb_parse_vendor_descriptor(dev
, usbdev
)) {
1547 pr_err("firmware not recognized. Assume incompatible device\n");
1551 if (!dlfb_alloc_urb_list(dev
, WRITES_IN_FLIGHT
, MAX_TRANSFER
)) {
1553 pr_err("dlfb_alloc_urb_list failed\n");
1557 /* We don't register a new USB class. Our client interface is fbdev */
1559 /* allocates framebuffer driver structure, not framebuffer memory */
1560 info
= framebuffer_alloc(0, &interface
->dev
);
1563 pr_err("framebuffer_alloc failed\n");
1569 info
->pseudo_palette
= dev
->pseudo_palette
;
1570 info
->fbops
= &dlfb_ops
;
1572 retval
= fb_alloc_cmap(&info
->cmap
, 256, 0);
1574 pr_err("fb_alloc_cmap failed %x\n", retval
);
1578 INIT_DELAYED_WORK(&dev
->free_framebuffer_work
,
1579 dlfb_free_framebuffer_work
);
1581 INIT_LIST_HEAD(&info
->modelist
);
1583 retval
= dlfb_setup_modes(dev
, info
, NULL
, 0);
1585 pr_err("unable to find common mode for display and adapter\n");
1589 /* ready to begin using device */
1591 atomic_set(&dev
->usb_active
, 1);
1592 dlfb_select_std_channel(dev
);
1594 dlfb_ops_check_var(&info
->var
, info
);
1595 dlfb_ops_set_par(info
);
1597 retval
= register_framebuffer(info
);
1599 pr_err("register_framebuffer failed %d\n", retval
);
1603 for (i
= 0; i
< ARRAY_SIZE(fb_device_attrs
); i
++) {
1604 retval
= device_create_file(info
->dev
, &fb_device_attrs
[i
]);
1606 pr_err("device_create_file failed %d\n", retval
);
1611 retval
= device_create_bin_file(info
->dev
, &edid_attr
);
1613 pr_err("device_create_bin_file failed %d\n", retval
);
1617 pr_info("DisplayLink USB device /dev/fb%d attached. %dx%d resolution."
1618 " Using %dK framebuffer memory\n", info
->node
,
1619 info
->var
.xres
, info
->var
.yres
,
1620 ((dev
->backing_buffer
) ?
1621 info
->fix
.smem_len
* 2 : info
->fix
.smem_len
) >> 10);
1625 for (i
-= 1; i
>= 0; i
--)
1626 device_remove_file(info
->dev
, &fb_device_attrs
[i
]);
1632 if (info
->cmap
.len
!= 0)
1633 fb_dealloc_cmap(&info
->cmap
);
1634 if (info
->monspecs
.modedb
)
1635 fb_destroy_modedb(info
->monspecs
.modedb
);
1636 if (info
->screen_base
)
1637 vfree(info
->screen_base
);
1639 fb_destroy_modelist(&info
->modelist
);
1641 framebuffer_release(info
);
1644 if (dev
->backing_buffer
)
1645 vfree(dev
->backing_buffer
);
1647 kref_put(&dev
->kref
, dlfb_free
); /* ref for framebuffer */
1648 kref_put(&dev
->kref
, dlfb_free
); /* last ref from kref_init */
1650 /* dev has been deallocated. Do not dereference */
1656 static void dlfb_usb_disconnect(struct usb_interface
*interface
)
1658 struct dlfb_data
*dev
;
1659 struct fb_info
*info
;
1662 dev
= usb_get_intfdata(interface
);
1665 pr_info("USB disconnect starting\n");
1667 /* we virtualize until all fb clients release. Then we free */
1668 dev
->virtualized
= true;
1670 /* When non-active we'll update virtual framebuffer, but no new urbs */
1671 atomic_set(&dev
->usb_active
, 0);
1673 /* remove udlfb's sysfs interfaces */
1674 for (i
= 0; i
< ARRAY_SIZE(fb_device_attrs
); i
++)
1675 device_remove_file(info
->dev
, &fb_device_attrs
[i
]);
1676 device_remove_bin_file(info
->dev
, &edid_attr
);
1678 usb_set_intfdata(interface
, NULL
);
1680 /* if clients still have us open, will be freed on last close */
1681 if (dev
->fb_count
== 0)
1682 schedule_delayed_work(&dev
->free_framebuffer_work
, 0);
1684 /* release reference taken by kref_init in probe() */
1685 kref_put(&dev
->kref
, dlfb_free
);
1687 /* consider dlfb_data freed */
1692 static struct usb_driver dlfb_driver
= {
1694 .probe
= dlfb_usb_probe
,
1695 .disconnect
= dlfb_usb_disconnect
,
1696 .id_table
= id_table
,
1699 static int __init
dlfb_module_init(void)
1703 res
= usb_register(&dlfb_driver
);
1705 err("usb_register failed. Error number %d", res
);
1710 static void __exit
dlfb_module_exit(void)
1712 usb_deregister(&dlfb_driver
);
1715 module_init(dlfb_module_init
);
1716 module_exit(dlfb_module_exit
);
1718 static void dlfb_urb_completion(struct urb
*urb
)
1720 struct urb_node
*unode
= urb
->context
;
1721 struct dlfb_data
*dev
= unode
->dev
;
1722 unsigned long flags
;
1724 /* sync/async unlink faults aren't errors */
1726 if (!(urb
->status
== -ENOENT
||
1727 urb
->status
== -ECONNRESET
||
1728 urb
->status
== -ESHUTDOWN
)) {
1729 pr_err("%s - nonzero write bulk status received: %d\n",
1730 __func__
, urb
->status
);
1731 atomic_set(&dev
->lost_pixels
, 1);
1735 urb
->transfer_buffer_length
= dev
->urbs
.size
; /* reset to actual */
1737 spin_lock_irqsave(&dev
->urbs
.lock
, flags
);
1738 list_add_tail(&unode
->entry
, &dev
->urbs
.list
);
1739 dev
->urbs
.available
++;
1740 spin_unlock_irqrestore(&dev
->urbs
.lock
, flags
);
1743 * When using fb_defio, we deadlock if up() is called
1744 * while another is waiting. So queue to another process.
1747 schedule_delayed_work(&unode
->release_urb_work
, 0);
1749 up(&dev
->urbs
.limit_sem
);
1752 static void dlfb_free_urb_list(struct dlfb_data
*dev
)
1754 int count
= dev
->urbs
.count
;
1755 struct list_head
*node
;
1756 struct urb_node
*unode
;
1759 unsigned long flags
;
1761 pr_notice("Waiting for completes and freeing all render urbs\n");
1763 /* keep waiting and freeing, until we've got 'em all */
1766 /* Getting interrupted means a leak, but ok at shutdown*/
1767 ret
= down_interruptible(&dev
->urbs
.limit_sem
);
1771 spin_lock_irqsave(&dev
->urbs
.lock
, flags
);
1773 node
= dev
->urbs
.list
.next
; /* have reserved one with sem */
1774 list_del_init(node
);
1776 spin_unlock_irqrestore(&dev
->urbs
.lock
, flags
);
1778 unode
= list_entry(node
, struct urb_node
, entry
);
1781 /* Free each separately allocated piece */
1782 usb_free_coherent(urb
->dev
, dev
->urbs
.size
,
1783 urb
->transfer_buffer
, urb
->transfer_dma
);
1790 static int dlfb_alloc_urb_list(struct dlfb_data
*dev
, int count
, size_t size
)
1794 struct urb_node
*unode
;
1797 spin_lock_init(&dev
->urbs
.lock
);
1799 dev
->urbs
.size
= size
;
1800 INIT_LIST_HEAD(&dev
->urbs
.list
);
1803 unode
= kzalloc(sizeof(struct urb_node
), GFP_KERNEL
);
1808 INIT_DELAYED_WORK(&unode
->release_urb_work
,
1809 dlfb_release_urb_work
);
1811 urb
= usb_alloc_urb(0, GFP_KERNEL
);
1818 buf
= usb_alloc_coherent(dev
->udev
, MAX_TRANSFER
, GFP_KERNEL
,
1819 &urb
->transfer_dma
);
1826 /* urb->transfer_buffer_length set to actual before submit */
1827 usb_fill_bulk_urb(urb
, dev
->udev
, usb_sndbulkpipe(dev
->udev
, 1),
1828 buf
, size
, dlfb_urb_completion
, unode
);
1829 urb
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
1831 list_add_tail(&unode
->entry
, &dev
->urbs
.list
);
1836 sema_init(&dev
->urbs
.limit_sem
, i
);
1837 dev
->urbs
.count
= i
;
1838 dev
->urbs
.available
= i
;
1840 pr_notice("allocated %d %d byte urbs\n", i
, (int) size
);
1845 static struct urb
*dlfb_get_urb(struct dlfb_data
*dev
)
1848 struct list_head
*entry
;
1849 struct urb_node
*unode
;
1850 struct urb
*urb
= NULL
;
1851 unsigned long flags
;
1853 /* Wait for an in-flight buffer to complete and get re-queued */
1854 ret
= down_timeout(&dev
->urbs
.limit_sem
, GET_URB_TIMEOUT
);
1856 atomic_set(&dev
->lost_pixels
, 1);
1857 pr_warn("wait for urb interrupted: %x available: %d\n",
1858 ret
, dev
->urbs
.available
);
1862 spin_lock_irqsave(&dev
->urbs
.lock
, flags
);
1864 BUG_ON(list_empty(&dev
->urbs
.list
)); /* reserved one with limit_sem */
1865 entry
= dev
->urbs
.list
.next
;
1866 list_del_init(entry
);
1867 dev
->urbs
.available
--;
1869 spin_unlock_irqrestore(&dev
->urbs
.lock
, flags
);
1871 unode
= list_entry(entry
, struct urb_node
, entry
);
1878 static int dlfb_submit_urb(struct dlfb_data
*dev
, struct urb
*urb
, size_t len
)
1882 BUG_ON(len
> dev
->urbs
.size
);
1884 urb
->transfer_buffer_length
= len
; /* set to actual payload len */
1885 ret
= usb_submit_urb(urb
, GFP_KERNEL
);
1887 dlfb_urb_completion(urb
); /* because no one else will */
1888 atomic_set(&dev
->lost_pixels
, 1);
1889 pr_err("usb_submit_urb error %x\n", ret
);
1894 module_param(console
, bool, S_IWUSR
| S_IRUSR
| S_IWGRP
| S_IRGRP
);
1895 MODULE_PARM_DESC(console
, "Allow fbcon to consume first framebuffer found");
1897 module_param(fb_defio
, bool, S_IWUSR
| S_IRUSR
| S_IWGRP
| S_IRGRP
);
1898 MODULE_PARM_DESC(fb_defio
, "Enable fb_defio mmap support. *Experimental*");
1900 MODULE_AUTHOR("Roberto De Ioris <roberto@unbit.it>, "
1901 "Jaya Kumar <jayakumar.lkml@gmail.com>, "
1902 "Bernie Thompson <bernie@plugable.com>");
1903 MODULE_DESCRIPTION("DisplayLink kernel framebuffer driver");
1904 MODULE_LICENSE("GPL");