2 * udlfb.c -- Framebuffer driver for DisplayLink USB controller
4 * Copyright (C) 2009 Roberto De Ioris <roberto@unbit.it>
5 * Copyright (C) 2009 Jaya Kumar <jayakumar.lkml@gmail.com>
6 * Copyright (C) 2009 Bernie Thompson <bernie@plugable.com>
8 * This file is subject to the terms and conditions of the GNU General Public
9 * License v2. See the file COPYING in the main directory of this archive for
12 * Layout is based on skeletonfb by James Simmons and Geert Uytterhoeven,
13 * usb-skeleton by GregKH.
15 * Device-specific portions based on information from Displaylink, with work
16 * from Florian Echtler, Henrik Bjerregaard Pedersen, and others.
19 #include <linux/module.h>
20 #include <linux/kernel.h>
21 #include <linux/init.h>
22 #include <linux/usb.h>
23 #include <linux/uaccess.h>
26 #include <linux/vmalloc.h>
27 #include <linux/slab.h>
28 #include <linux/delay.h>
32 static struct fb_fix_screeninfo dlfb_fix
= {
34 .type
= FB_TYPE_PACKED_PIXELS
,
35 .visual
= FB_VISUAL_TRUECOLOR
,
39 .accel
= FB_ACCEL_NONE
,
42 static const u32 udlfb_info_flags
= FBINFO_DEFAULT
| FBINFO_READS_FAST
|
46 FBINFO_HWACCEL_IMAGEBLIT
| FBINFO_HWACCEL_FILLRECT
|
47 FBINFO_HWACCEL_COPYAREA
| FBINFO_MISC_ALWAYS_SETPAR
;
50 * There are many DisplayLink-based products, all with unique PIDs. We are able
51 * to support all volume ones (circa 2009) with a single driver, so we match
52 * globally on VID. TODO: Probe() needs to detect when we might be running
53 * "future" chips, and bail on those, so a compatible driver can match.
55 static struct usb_device_id id_table
[] = {
56 {.idVendor
= 0x17e9, .match_flags
= USB_DEVICE_ID_MATCH_VENDOR
,},
59 MODULE_DEVICE_TABLE(usb
, id_table
);
62 static int console
; /* Optionally allow fbcon to consume first framebuffer */
63 static int fb_defio
; /* Optionally enable experimental fb_defio mmap support */
65 /* dlfb keeps a list of urbs for efficient bulk transfers */
66 static void dlfb_urb_completion(struct urb
*urb
);
67 static struct urb
*dlfb_get_urb(struct dlfb_data
*dev
);
68 static int dlfb_submit_urb(struct dlfb_data
*dev
, struct urb
* urb
, size_t len
);
69 static int dlfb_alloc_urb_list(struct dlfb_data
*dev
, int count
, size_t size
);
70 static void dlfb_free_urb_list(struct dlfb_data
*dev
);
73 * All DisplayLink bulk operations start with 0xAF, followed by specific code
74 * All operations are written to buffers which then later get sent to device
76 static char *dlfb_set_register(char *buf
, u8 reg
, u8 val
)
85 static char *dlfb_vidreg_lock(char *buf
)
87 return dlfb_set_register(buf
, 0xFF, 0x00);
90 static char *dlfb_vidreg_unlock(char *buf
)
92 return dlfb_set_register(buf
, 0xFF, 0xFF);
96 * On/Off for driving the DisplayLink framebuffer to the display
97 * 0x00 H and V sync on
98 * 0x01 H and V sync off (screen blank but powered)
99 * 0x07 DPMS powerdown (requires modeset to come back)
101 static char *dlfb_enable_hvsync(char *buf
, bool enable
)
104 return dlfb_set_register(buf
, 0x1F, 0x00);
106 return dlfb_set_register(buf
, 0x1F, 0x07);
109 static char *dlfb_set_color_depth(char *buf
, u8 selection
)
111 return dlfb_set_register(buf
, 0x00, selection
);
114 static char *dlfb_set_base16bpp(char *wrptr
, u32 base
)
116 /* the base pointer is 16 bits wide, 0x20 is hi byte. */
117 wrptr
= dlfb_set_register(wrptr
, 0x20, base
>> 16);
118 wrptr
= dlfb_set_register(wrptr
, 0x21, base
>> 8);
119 return dlfb_set_register(wrptr
, 0x22, base
);
123 * DisplayLink HW has separate 16bpp and 8bpp framebuffers.
124 * In 24bpp modes, the low 323 RGB bits go in the 8bpp framebuffer
126 static char *dlfb_set_base8bpp(char *wrptr
, u32 base
)
128 wrptr
= dlfb_set_register(wrptr
, 0x26, base
>> 16);
129 wrptr
= dlfb_set_register(wrptr
, 0x27, base
>> 8);
130 return dlfb_set_register(wrptr
, 0x28, base
);
133 static char *dlfb_set_register_16(char *wrptr
, u8 reg
, u16 value
)
135 wrptr
= dlfb_set_register(wrptr
, reg
, value
>> 8);
136 return dlfb_set_register(wrptr
, reg
+1, value
);
140 * This is kind of weird because the controller takes some
141 * register values in a different byte order than other registers.
143 static char *dlfb_set_register_16be(char *wrptr
, u8 reg
, u16 value
)
145 wrptr
= dlfb_set_register(wrptr
, reg
, value
);
146 return dlfb_set_register(wrptr
, reg
+1, value
>> 8);
150 * LFSR is linear feedback shift register. The reason we have this is
151 * because the display controller needs to minimize the clock depth of
152 * various counters used in the display path. So this code reverses the
153 * provided value into the lfsr16 value by counting backwards to get
154 * the value that needs to be set in the hardware comparator to get the
155 * same actual count. This makes sense once you read above a couple of
156 * times and think about it from a hardware perspective.
158 static u16
dlfb_lfsr16(u16 actual_count
)
160 u32 lv
= 0xFFFF; /* This is the lfsr value that the hw starts with */
162 while (actual_count
--) {
164 (((lv
>> 15) ^ (lv
>> 4) ^ (lv
>> 2) ^ (lv
>> 1)) & 1))
172 * This does LFSR conversion on the value that is to be written.
173 * See LFSR explanation above for more detail.
175 static char *dlfb_set_register_lfsr16(char *wrptr
, u8 reg
, u16 value
)
177 return dlfb_set_register_16(wrptr
, reg
, dlfb_lfsr16(value
));
181 * This takes a standard fbdev screeninfo struct and all of its monitor mode
182 * details and converts them into the DisplayLink equivalent register commands.
184 static char *dlfb_set_vid_cmds(char *wrptr
, struct fb_var_screeninfo
*var
)
190 /* x display start */
191 xds
= var
->left_margin
+ var
->hsync_len
;
192 wrptr
= dlfb_set_register_lfsr16(wrptr
, 0x01, xds
);
194 xde
= xds
+ var
->xres
;
195 wrptr
= dlfb_set_register_lfsr16(wrptr
, 0x03, xde
);
197 /* y display start */
198 yds
= var
->upper_margin
+ var
->vsync_len
;
199 wrptr
= dlfb_set_register_lfsr16(wrptr
, 0x05, yds
);
201 yde
= yds
+ var
->yres
;
202 wrptr
= dlfb_set_register_lfsr16(wrptr
, 0x07, yde
);
204 /* x end count is active + blanking - 1 */
205 wrptr
= dlfb_set_register_lfsr16(wrptr
, 0x09,
206 xde
+ var
->right_margin
- 1);
208 /* libdlo hardcodes hsync start to 1 */
209 wrptr
= dlfb_set_register_lfsr16(wrptr
, 0x0B, 1);
211 /* hsync end is width of sync pulse + 1 */
212 wrptr
= dlfb_set_register_lfsr16(wrptr
, 0x0D, var
->hsync_len
+ 1);
214 /* hpixels is active pixels */
215 wrptr
= dlfb_set_register_16(wrptr
, 0x0F, var
->xres
);
217 /* yendcount is vertical active + vertical blanking */
218 yec
= var
->yres
+ var
->upper_margin
+ var
->lower_margin
+
220 wrptr
= dlfb_set_register_lfsr16(wrptr
, 0x11, yec
);
222 /* libdlo hardcodes vsync start to 0 */
223 wrptr
= dlfb_set_register_lfsr16(wrptr
, 0x13, 0);
225 /* vsync end is width of vsync pulse */
226 wrptr
= dlfb_set_register_lfsr16(wrptr
, 0x15, var
->vsync_len
);
228 /* vpixels is active pixels */
229 wrptr
= dlfb_set_register_16(wrptr
, 0x17, var
->yres
);
231 /* convert picoseconds to 5kHz multiple for pclk5k = x * 1E12/5k */
232 wrptr
= dlfb_set_register_16be(wrptr
, 0x1B,
233 200*1000*1000/var
->pixclock
);
239 * This takes a standard fbdev screeninfo struct that was fetched or prepared
240 * and then generates the appropriate command sequence that then drives the
241 * display controller.
243 static int dlfb_set_video_mode(struct dlfb_data
*dev
,
244 struct fb_var_screeninfo
*var
)
252 if (!atomic_read(&dev
->usb_active
))
255 urb
= dlfb_get_urb(dev
);
259 buf
= (char *) urb
->transfer_buffer
;
262 * This first section has to do with setting the base address on the
263 * controller * associated with the display. There are 2 base
264 * pointers, currently, we only * use the 16 bpp segment.
266 wrptr
= dlfb_vidreg_lock(buf
);
267 wrptr
= dlfb_set_color_depth(wrptr
, 0x00);
268 /* set base for 16bpp segment to 0 */
269 wrptr
= dlfb_set_base16bpp(wrptr
, 0);
270 /* set base for 8bpp segment to end of fb */
271 wrptr
= dlfb_set_base8bpp(wrptr
, dev
->info
->fix
.smem_len
);
273 wrptr
= dlfb_set_vid_cmds(wrptr
, var
);
274 wrptr
= dlfb_enable_hvsync(wrptr
, true);
275 wrptr
= dlfb_vidreg_unlock(wrptr
);
277 writesize
= wrptr
- buf
;
279 retval
= dlfb_submit_urb(dev
, urb
, writesize
);
284 static int dlfb_ops_mmap(struct fb_info
*info
, struct vm_area_struct
*vma
)
286 unsigned long start
= vma
->vm_start
;
287 unsigned long size
= vma
->vm_end
- vma
->vm_start
;
288 unsigned long offset
= vma
->vm_pgoff
<< PAGE_SHIFT
;
289 unsigned long page
, pos
;
291 if (offset
+ size
> info
->fix
.smem_len
)
294 pos
= (unsigned long)info
->fix
.smem_start
+ offset
;
296 dl_notice("mmap() framebuffer addr:%lu size:%lu\n",
300 page
= vmalloc_to_pfn((void *)pos
);
301 if (remap_pfn_range(vma
, start
, page
, PAGE_SIZE
, PAGE_SHARED
))
306 if (size
> PAGE_SIZE
)
312 vma
->vm_flags
|= VM_RESERVED
; /* avoid to swap out this VMA */
317 * Trims identical data from front and back of line
318 * Sets new front buffer address and width
319 * And returns byte count of identical pixels
320 * Assumes CPU natural alignment (unsigned long)
321 * for back and front buffer ptrs and width
323 static int dlfb_trim_hline(const u8
*bback
, const u8
**bfront
, int *width_bytes
)
326 const unsigned long *back
= (const unsigned long *) bback
;
327 const unsigned long *front
= (const unsigned long *) *bfront
;
328 const int width
= *width_bytes
/ sizeof(unsigned long);
329 int identical
= width
;
333 prefetch((void *) front
);
334 prefetch((void *) back
);
336 for (j
= 0; j
< width
; j
++) {
337 if (back
[j
] != front
[j
]) {
343 for (k
= width
- 1; k
> j
; k
--) {
344 if (back
[k
] != front
[k
]) {
350 identical
= start
+ (width
- end
);
351 *bfront
= (u8
*) &front
[start
];
352 *width_bytes
= (end
- start
) * sizeof(unsigned long);
354 return identical
* sizeof(unsigned long);
358 * Render a command stream for an encoded horizontal line segment of pixels.
360 * A command buffer holds several commands.
361 * It always begins with a fresh command header
362 * (the protocol doesn't require this, but we enforce it to allow
363 * multiple buffers to be potentially encoded and sent in parallel).
364 * A single command encodes one contiguous horizontal line of pixels
366 * The function relies on the client to do all allocation, so that
367 * rendering can be done directly to output buffers (e.g. USB URBs).
368 * The function fills the supplied command buffer, providing information
369 * on where it left off, so the client may call in again with additional
370 * buffers if the line will take several buffers to complete.
372 * A single command can transmit a maximum of 256 pixels,
373 * regardless of the compression ratio (protocol design limit).
374 * To the hardware, 0 for a size byte means 256
376 * Rather than 256 pixel commands which are either rl or raw encoded,
377 * the rlx command simply assumes alternating raw and rl spans within one cmd.
378 * This has a slightly larger header overhead, but produces more even results.
379 * It also processes all data (read and write) in a single pass.
380 * Performance benchmarks of common cases show it having just slightly better
381 * compression than 256 pixel raw or rle commands, with similar CPU consumpion.
382 * But for very rl friendly data, will compress not quite as well.
384 static void dlfb_compress_hline(
385 const uint16_t **pixel_start_ptr
,
386 const uint16_t *const pixel_end
,
387 uint32_t *device_address_ptr
,
388 uint8_t **command_buffer_ptr
,
389 const uint8_t *const cmd_buffer_end
)
391 const uint16_t *pixel
= *pixel_start_ptr
;
392 uint32_t dev_addr
= *device_address_ptr
;
393 uint8_t *cmd
= *command_buffer_ptr
;
396 while ((pixel_end
> pixel
) &&
397 (cmd_buffer_end
- MIN_RLX_CMD_BYTES
> cmd
)) {
398 uint8_t *raw_pixels_count_byte
= 0;
399 uint8_t *cmd_pixels_count_byte
= 0;
400 const uint16_t *raw_pixel_start
= 0;
401 const uint16_t *cmd_pixel_start
, *cmd_pixel_end
= 0;
403 prefetchw((void *) cmd
); /* pull in one cache line at least */
407 *cmd
++ = (uint8_t) ((dev_addr
>> 16) & 0xFF);
408 *cmd
++ = (uint8_t) ((dev_addr
>> 8) & 0xFF);
409 *cmd
++ = (uint8_t) ((dev_addr
) & 0xFF);
411 cmd_pixels_count_byte
= cmd
++; /* we'll know this later */
412 cmd_pixel_start
= pixel
;
414 raw_pixels_count_byte
= cmd
++; /* we'll know this later */
415 raw_pixel_start
= pixel
;
417 cmd_pixel_end
= pixel
+ min(MAX_CMD_PIXELS
+ 1,
418 min((int)(pixel_end
- pixel
),
419 (int)(cmd_buffer_end
- cmd
) / bpp
));
421 prefetch_range((void *) pixel
, (cmd_pixel_end
- pixel
) * bpp
);
423 while (pixel
< cmd_pixel_end
) {
424 const uint16_t * const repeating_pixel
= pixel
;
426 *(uint16_t *)cmd
= cpu_to_be16p(pixel
);
430 if (unlikely((pixel
< cmd_pixel_end
) &&
431 (*pixel
== *repeating_pixel
))) {
432 /* go back and fill in raw pixel count */
433 *raw_pixels_count_byte
= ((repeating_pixel
-
434 raw_pixel_start
) + 1) & 0xFF;
436 while ((pixel
< cmd_pixel_end
)
437 && (*pixel
== *repeating_pixel
)) {
441 /* immediately after raw data is repeat byte */
442 *cmd
++ = ((pixel
- repeating_pixel
) - 1) & 0xFF;
444 /* Then start another raw pixel span */
445 raw_pixel_start
= pixel
;
446 raw_pixels_count_byte
= cmd
++;
450 if (pixel
> raw_pixel_start
) {
451 /* finalize last RAW span */
452 *raw_pixels_count_byte
= (pixel
-raw_pixel_start
) & 0xFF;
455 *cmd_pixels_count_byte
= (pixel
- cmd_pixel_start
) & 0xFF;
456 dev_addr
+= (pixel
- cmd_pixel_start
) * bpp
;
459 if (cmd_buffer_end
<= MIN_RLX_CMD_BYTES
+ cmd
) {
460 /* Fill leftover bytes with no-ops */
461 if (cmd_buffer_end
> cmd
)
462 memset(cmd
, 0xAF, cmd_buffer_end
- cmd
);
463 cmd
= (uint8_t *) cmd_buffer_end
;
466 *command_buffer_ptr
= cmd
;
467 *pixel_start_ptr
= pixel
;
468 *device_address_ptr
= dev_addr
;
474 * There are 3 copies of every pixel: The front buffer that the fbdev
475 * client renders to, the actual framebuffer across the USB bus in hardware
476 * (that we can only write to, slowly, and can never read), and (optionally)
477 * our shadow copy that tracks what's been sent to that hardware buffer.
479 static int dlfb_render_hline(struct dlfb_data
*dev
, struct urb
**urb_ptr
,
480 const char *front
, char **urb_buf_ptr
,
481 u32 byte_offset
, u32 byte_width
,
482 int *ident_ptr
, int *sent_ptr
)
484 const u8
*line_start
, *line_end
, *next_pixel
;
485 u32 dev_addr
= dev
->base16
+ byte_offset
;
486 struct urb
*urb
= *urb_ptr
;
487 u8
*cmd
= *urb_buf_ptr
;
488 u8
*cmd_end
= (u8
*) urb
->transfer_buffer
+ urb
->transfer_buffer_length
;
490 line_start
= (u8
*) (front
+ byte_offset
);
491 next_pixel
= line_start
;
492 line_end
= next_pixel
+ byte_width
;
494 if (dev
->backing_buffer
) {
496 const u8
*back_start
= (u8
*) (dev
->backing_buffer
499 *ident_ptr
+= dlfb_trim_hline(back_start
, &next_pixel
,
502 offset
= next_pixel
- line_start
;
503 line_end
= next_pixel
+ byte_width
;
505 back_start
+= offset
;
506 line_start
+= offset
;
508 memcpy((char *)back_start
, (char *) line_start
,
512 while (next_pixel
< line_end
) {
514 dlfb_compress_hline((const uint16_t **) &next_pixel
,
515 (const uint16_t *) line_end
, &dev_addr
,
516 (u8
**) &cmd
, (u8
*) cmd_end
);
518 if (cmd
>= cmd_end
) {
519 int len
= cmd
- (u8
*) urb
->transfer_buffer
;
520 if (dlfb_submit_urb(dev
, urb
, len
))
521 return 1; /* lost pixels is set */
523 urb
= dlfb_get_urb(dev
);
525 return 1; /* lost_pixels is set */
527 cmd
= urb
->transfer_buffer
;
528 cmd_end
= &cmd
[urb
->transfer_buffer_length
];
537 int dlfb_handle_damage(struct dlfb_data
*dev
, int x
, int y
,
538 int width
, int height
, char *data
)
542 cycles_t start_cycles
, end_cycles
;
544 int bytes_identical
= 0;
548 start_cycles
= get_cycles();
550 aligned_x
= DL_ALIGN_DOWN(x
, sizeof(unsigned long));
551 width
= DL_ALIGN_UP(width
+ (x
-aligned_x
), sizeof(unsigned long));
555 (x
+ width
> dev
->info
->var
.xres
) ||
556 (y
+ height
> dev
->info
->var
.yres
))
559 if (!atomic_read(&dev
->usb_active
))
562 urb
= dlfb_get_urb(dev
);
565 cmd
= urb
->transfer_buffer
;
567 for (i
= y
; i
< y
+ height
; i
++) {
568 const int line_offset
= dev
->info
->fix
.line_length
* i
;
569 const int byte_offset
= line_offset
+ (x
* BPP
);
571 if (dlfb_render_hline(dev
, &urb
,
572 (char *) dev
->info
->fix
.smem_start
,
573 &cmd
, byte_offset
, width
* BPP
,
574 &bytes_identical
, &bytes_sent
))
578 if (cmd
> (char *) urb
->transfer_buffer
) {
579 /* Send partial buffer remaining before exiting */
580 int len
= cmd
- (char *) urb
->transfer_buffer
;
581 ret
= dlfb_submit_urb(dev
, urb
, len
);
584 dlfb_urb_completion(urb
);
587 atomic_add(bytes_sent
, &dev
->bytes_sent
);
588 atomic_add(bytes_identical
, &dev
->bytes_identical
);
589 atomic_add(width
*height
*2, &dev
->bytes_rendered
);
590 end_cycles
= get_cycles();
591 atomic_add(((unsigned int) ((end_cycles
- start_cycles
)
592 >> 10)), /* Kcycles */
593 &dev
->cpu_kcycles_used
);
598 static ssize_t
dlfb_ops_read(struct fb_info
*info
, char __user
*buf
,
599 size_t count
, loff_t
*ppos
)
601 ssize_t result
= -ENOSYS
;
603 #if defined CONFIG_FB_SYS_FOPS || defined CONFIG_FB_SYS_FOPS_MODULE
604 result
= fb_sys_read(info
, buf
, count
, ppos
);
611 * Path triggered by usermode clients who write to filesystem
612 * e.g. cat filename > /dev/fb1
613 * Not used by X Windows or text-mode console. But useful for testing.
614 * Slow because of extra copy and we must assume all pixels dirty.
616 static ssize_t
dlfb_ops_write(struct fb_info
*info
, const char __user
*buf
,
617 size_t count
, loff_t
*ppos
)
619 ssize_t result
= -ENOSYS
;
620 struct dlfb_data
*dev
= info
->par
;
621 u32 offset
= (u32
) *ppos
;
623 #if defined CONFIG_FB_SYS_FOPS || defined CONFIG_FB_SYS_FOPS_MODULE
625 result
= fb_sys_write(info
, buf
, count
, ppos
);
628 int start
= max((int)(offset
/ info
->fix
.line_length
) - 1, 0);
629 int lines
= min((u32
)((result
/ info
->fix
.line_length
) + 1),
630 (u32
)info
->var
.yres
);
632 dlfb_handle_damage(dev
, 0, start
, info
->var
.xres
,
633 lines
, info
->screen_base
);
640 /* hardware has native COPY command (see libdlo), but not worth it for fbcon */
641 static void dlfb_ops_copyarea(struct fb_info
*info
,
642 const struct fb_copyarea
*area
)
645 struct dlfb_data
*dev
= info
->par
;
647 #if defined CONFIG_FB_SYS_COPYAREA || defined CONFIG_FB_SYS_COPYAREA_MODULE
649 sys_copyarea(info
, area
);
651 dlfb_handle_damage(dev
, area
->dx
, area
->dy
,
652 area
->width
, area
->height
, info
->screen_base
);
657 static void dlfb_ops_imageblit(struct fb_info
*info
,
658 const struct fb_image
*image
)
660 struct dlfb_data
*dev
= info
->par
;
662 #if defined CONFIG_FB_SYS_IMAGEBLIT || defined CONFIG_FB_SYS_IMAGEBLIT_MODULE
664 sys_imageblit(info
, image
);
666 dlfb_handle_damage(dev
, image
->dx
, image
->dy
,
667 image
->width
, image
->height
, info
->screen_base
);
673 static void dlfb_ops_fillrect(struct fb_info
*info
,
674 const struct fb_fillrect
*rect
)
676 struct dlfb_data
*dev
= info
->par
;
678 #if defined CONFIG_FB_SYS_FILLRECT || defined CONFIG_FB_SYS_FILLRECT_MODULE
680 sys_fillrect(info
, rect
);
682 dlfb_handle_damage(dev
, rect
->dx
, rect
->dy
, rect
->width
,
683 rect
->height
, info
->screen_base
);
688 #ifdef CONFIG_FB_DEFERRED_IO
690 * NOTE: fb_defio.c is holding info->fbdefio.mutex
691 * Touching ANY framebuffer memory that triggers a page fault
692 * in fb_defio will cause a deadlock, when it also tries to
693 * grab the same mutex.
695 static void dlfb_dpy_deferred_io(struct fb_info
*info
,
696 struct list_head
*pagelist
)
699 struct fb_deferred_io
*fbdefio
= info
->fbdefio
;
700 struct dlfb_data
*dev
= info
->par
;
703 cycles_t start_cycles
, end_cycles
;
705 int bytes_identical
= 0;
706 int bytes_rendered
= 0;
711 if (!atomic_read(&dev
->usb_active
))
714 start_cycles
= get_cycles();
716 urb
= dlfb_get_urb(dev
);
720 cmd
= urb
->transfer_buffer
;
722 /* walk the written page list and render each to device */
723 list_for_each_entry(cur
, &fbdefio
->pagelist
, lru
) {
725 if (dlfb_render_hline(dev
, &urb
, (char *) info
->fix
.smem_start
,
726 &cmd
, cur
->index
<< PAGE_SHIFT
,
727 PAGE_SIZE
, &bytes_identical
, &bytes_sent
))
729 bytes_rendered
+= PAGE_SIZE
;
732 if (cmd
> (char *) urb
->transfer_buffer
) {
733 /* Send partial buffer remaining before exiting */
734 int len
= cmd
- (char *) urb
->transfer_buffer
;
735 dlfb_submit_urb(dev
, urb
, len
);
738 dlfb_urb_completion(urb
);
741 atomic_add(bytes_sent
, &dev
->bytes_sent
);
742 atomic_add(bytes_identical
, &dev
->bytes_identical
);
743 atomic_add(bytes_rendered
, &dev
->bytes_rendered
);
744 end_cycles
= get_cycles();
745 atomic_add(((unsigned int) ((end_cycles
- start_cycles
)
746 >> 10)), /* Kcycles */
747 &dev
->cpu_kcycles_used
);
752 static int dlfb_get_edid(struct dlfb_data
*dev
, char *edid
, int len
)
758 rbuf
= kmalloc(2, GFP_KERNEL
);
762 for (i
= 0; i
< len
; i
++) {
763 ret
= usb_control_msg(dev
->udev
,
764 usb_rcvctrlpipe(dev
->udev
, 0), (0x02),
765 (0x80 | (0x02 << 5)), i
<< 8, 0xA1, rbuf
, 2,
768 dl_err("Read EDID byte %d failed err %x\n", i
, ret
);
780 static int dlfb_ops_ioctl(struct fb_info
*info
, unsigned int cmd
,
784 struct dlfb_data
*dev
= info
->par
;
785 struct dloarea
*area
= NULL
;
787 if (!atomic_read(&dev
->usb_active
))
790 /* TODO: Update X server to get this from sysfs instead */
791 if (cmd
== DLFB_IOCTL_RETURN_EDID
) {
792 char *edid
= (char *)arg
;
793 if (copy_to_user(edid
, dev
->edid
, dev
->edid_size
))
798 /* TODO: Help propose a standard fb.h ioctl to report mmap damage */
799 if (cmd
== DLFB_IOCTL_REPORT_DAMAGE
) {
802 * If we have a damage-aware client, turn fb_defio "off"
803 * To avoid perf imact of unecessary page fault handling.
804 * Done by resetting the delay for this fb_info to a very
805 * long period. Pages will become writable and stay that way.
806 * Reset to normal value when all clients have closed this fb.
809 info
->fbdefio
->delay
= DL_DEFIO_WRITE_DISABLE
;
811 area
= (struct dloarea
*)arg
;
816 if (area
->x
> info
->var
.xres
)
817 area
->x
= info
->var
.xres
;
822 if (area
->y
> info
->var
.yres
)
823 area
->y
= info
->var
.yres
;
825 dlfb_handle_damage(dev
, area
->x
, area
->y
, area
->w
, area
->h
,
832 /* taken from vesafb */
834 dlfb_ops_setcolreg(unsigned regno
, unsigned red
, unsigned green
,
835 unsigned blue
, unsigned transp
, struct fb_info
*info
)
839 if (regno
>= info
->cmap
.len
)
843 if (info
->var
.red
.offset
== 10) {
845 ((u32
*) (info
->pseudo_palette
))[regno
] =
846 ((red
& 0xf800) >> 1) |
847 ((green
& 0xf800) >> 6) | ((blue
& 0xf800) >> 11);
850 ((u32
*) (info
->pseudo_palette
))[regno
] =
852 ((green
& 0xfc00) >> 5) | ((blue
& 0xf800) >> 11);
860 * It's common for several clients to have framebuffer open simultaneously.
861 * e.g. both fbcon and X. Makes things interesting.
862 * Assumes caller is holding info->lock (for open and release at least)
864 static int dlfb_ops_open(struct fb_info
*info
, int user
)
866 struct dlfb_data
*dev
= info
->par
;
869 * fbcon aggressively connects to first framebuffer it finds,
870 * preventing other clients (X) from working properly. Usually
871 * not what the user wants. Fail by default with option to enable.
873 if ((user
== 0) & (!console
))
876 /* If the USB device is gone, we don't accept new opens */
877 if (dev
->virtualized
)
882 kref_get(&dev
->kref
);
884 #ifdef CONFIG_FB_DEFERRED_IO
885 if (fb_defio
&& (info
->fbdefio
== NULL
)) {
886 /* enable defio at last moment if not disabled by client */
888 struct fb_deferred_io
*fbdefio
;
890 fbdefio
= kmalloc(sizeof(struct fb_deferred_io
), GFP_KERNEL
);
893 fbdefio
->delay
= DL_DEFIO_WRITE_DELAY
;
894 fbdefio
->deferred_io
= dlfb_dpy_deferred_io
;
897 info
->fbdefio
= fbdefio
;
898 fb_deferred_io_init(info
);
902 dl_notice("open /dev/fb%d user=%d fb_info=%p count=%d\n",
903 info
->node
, user
, info
, dev
->fb_count
);
909 * Called when all client interfaces to start transactions have been disabled,
910 * and all references to our device instance (dlfb_data) are released.
911 * Every transaction must have a reference, so we know are fully spun down
913 static void dlfb_free(struct kref
*kref
)
915 struct dlfb_data
*dev
= container_of(kref
, struct dlfb_data
, kref
);
917 /* this function will wait for all in-flight urbs to complete */
918 if (dev
->urbs
.count
> 0)
919 dlfb_free_urb_list(dev
);
921 if (dev
->backing_buffer
)
922 vfree(dev
->backing_buffer
);
926 dl_warn("freeing dlfb_data %p\n", dev
);
931 static void dlfb_release_urb_work(struct work_struct
*work
)
933 struct urb_node
*unode
= container_of(work
, struct urb_node
,
934 release_urb_work
.work
);
936 up(&unode
->dev
->urbs
.limit_sem
);
939 static void dlfb_free_framebuffer_work(struct work_struct
*work
)
941 struct dlfb_data
*dev
= container_of(work
, struct dlfb_data
,
942 free_framebuffer_work
.work
);
943 struct fb_info
*info
= dev
->info
;
944 int node
= info
->node
;
946 unregister_framebuffer(info
);
948 if (info
->cmap
.len
!= 0)
949 fb_dealloc_cmap(&info
->cmap
);
950 if (info
->monspecs
.modedb
)
951 fb_destroy_modedb(info
->monspecs
.modedb
);
952 if (info
->screen_base
)
953 vfree(info
->screen_base
);
955 fb_destroy_modelist(&info
->modelist
);
959 /* Assume info structure is freed after this point */
960 framebuffer_release(info
);
962 dl_warn("fb_info for /dev/fb%d has been freed\n", node
);
964 /* ref taken in probe() as part of registering framebfufer */
965 kref_put(&dev
->kref
, dlfb_free
);
969 * Assumes caller is holding info->lock mutex (for open and release at least)
971 static int dlfb_ops_release(struct fb_info
*info
, int user
)
973 struct dlfb_data
*dev
= info
->par
;
977 /* We can't free fb_info here - fbmem will touch it when we return */
978 if (dev
->virtualized
&& (dev
->fb_count
== 0))
979 schedule_delayed_work(&dev
->free_framebuffer_work
, HZ
);
981 #ifdef CONFIG_FB_DEFERRED_IO
982 if ((dev
->fb_count
== 0) && (info
->fbdefio
)) {
983 fb_deferred_io_cleanup(info
);
984 kfree(info
->fbdefio
);
985 info
->fbdefio
= NULL
;
986 info
->fbops
->fb_mmap
= dlfb_ops_mmap
;
990 dl_warn("released /dev/fb%d user=%d count=%d\n",
991 info
->node
, user
, dev
->fb_count
);
993 kref_put(&dev
->kref
, dlfb_free
);
999 * Check whether a video mode is supported by the DisplayLink chip
1000 * We start from monitor's modes, so don't need to filter that here
1002 static int dlfb_is_valid_mode(struct fb_videomode
*mode
,
1003 struct fb_info
*info
)
1005 struct dlfb_data
*dev
= info
->par
;
1007 if (mode
->xres
* mode
->yres
> dev
->sku_pixel_limit
) {
1008 dl_warn("%dx%d beyond chip capabilities\n",
1009 mode
->xres
, mode
->yres
);
1013 dl_info("%dx%d valid mode\n", mode
->xres
, mode
->yres
);
1018 static void dlfb_var_color_format(struct fb_var_screeninfo
*var
)
1020 const struct fb_bitfield red
= { 11, 5, 0 };
1021 const struct fb_bitfield green
= { 5, 6, 0 };
1022 const struct fb_bitfield blue
= { 0, 5, 0 };
1024 var
->bits_per_pixel
= 16;
1030 static int dlfb_ops_check_var(struct fb_var_screeninfo
*var
,
1031 struct fb_info
*info
)
1033 struct fb_videomode mode
;
1035 /* TODO: support dynamically changing framebuffer size */
1036 if ((var
->xres
* var
->yres
* 2) > info
->fix
.smem_len
)
1039 /* set device-specific elements of var unrelated to mode */
1040 dlfb_var_color_format(var
);
1042 fb_var_to_videomode(&mode
, var
);
1044 if (!dlfb_is_valid_mode(&mode
, info
))
1050 static int dlfb_ops_set_par(struct fb_info
*info
)
1052 struct dlfb_data
*dev
= info
->par
;
1054 u16
*pix_framebuffer
;
1057 dl_notice("set_par mode %dx%d\n", info
->var
.xres
, info
->var
.yres
);
1059 result
= dlfb_set_video_mode(dev
, &info
->var
);
1061 if ((result
== 0) && (dev
->fb_count
== 0)) {
1063 /* paint greenscreen */
1065 pix_framebuffer
= (u16
*) info
->screen_base
;
1066 for (i
= 0; i
< info
->fix
.smem_len
/ 2; i
++)
1067 pix_framebuffer
[i
] = 0x37e6;
1069 dlfb_handle_damage(dev
, 0, 0, info
->var
.xres
, info
->var
.yres
,
1077 * In order to come back from full DPMS off, we need to set the mode again
1079 static int dlfb_ops_blank(int blank_mode
, struct fb_info
*info
)
1081 struct dlfb_data
*dev
= info
->par
;
1083 if (blank_mode
!= FB_BLANK_UNBLANK
) {
1087 urb
= dlfb_get_urb(dev
);
1091 bufptr
= (char *) urb
->transfer_buffer
;
1092 bufptr
= dlfb_vidreg_lock(bufptr
);
1093 bufptr
= dlfb_enable_hvsync(bufptr
, false);
1094 bufptr
= dlfb_vidreg_unlock(bufptr
);
1096 dlfb_submit_urb(dev
, urb
, bufptr
-
1097 (char *) urb
->transfer_buffer
);
1099 dlfb_set_video_mode(dev
, &info
->var
);
1105 static struct fb_ops dlfb_ops
= {
1106 .owner
= THIS_MODULE
,
1107 .fb_read
= dlfb_ops_read
,
1108 .fb_write
= dlfb_ops_write
,
1109 .fb_setcolreg
= dlfb_ops_setcolreg
,
1110 .fb_fillrect
= dlfb_ops_fillrect
,
1111 .fb_copyarea
= dlfb_ops_copyarea
,
1112 .fb_imageblit
= dlfb_ops_imageblit
,
1113 .fb_mmap
= dlfb_ops_mmap
,
1114 .fb_ioctl
= dlfb_ops_ioctl
,
1115 .fb_open
= dlfb_ops_open
,
1116 .fb_release
= dlfb_ops_release
,
1117 .fb_blank
= dlfb_ops_blank
,
1118 .fb_check_var
= dlfb_ops_check_var
,
1119 .fb_set_par
= dlfb_ops_set_par
,
1124 * Assumes &info->lock held by caller
1125 * Assumes no active clients have framebuffer open
1127 static int dlfb_realloc_framebuffer(struct dlfb_data
*dev
, struct fb_info
*info
)
1129 int retval
= -ENOMEM
;
1130 int old_len
= info
->fix
.smem_len
;
1132 unsigned char *old_fb
= info
->screen_base
;
1133 unsigned char *new_fb
;
1134 unsigned char *new_back
;
1136 dl_warn("Reallocating framebuffer. Addresses will change!\n");
1138 new_len
= info
->fix
.line_length
* info
->var
.yres
;
1140 if (PAGE_ALIGN(new_len
) > old_len
) {
1142 * Alloc system memory for virtual framebuffer
1144 new_fb
= vmalloc(new_len
);
1146 dl_err("Virtual framebuffer alloc failed\n");
1150 if (info
->screen_base
) {
1151 memcpy(new_fb
, old_fb
, old_len
);
1152 vfree(info
->screen_base
);
1155 info
->screen_base
= new_fb
;
1156 info
->fix
.smem_len
= PAGE_ALIGN(new_len
);
1157 info
->fix
.smem_start
= (unsigned long) new_fb
;
1158 info
->flags
= udlfb_info_flags
;
1161 * Second framebuffer copy to mirror the framebuffer state
1162 * on the physical USB device. We can function without this.
1163 * But with imperfect damage info we may send pixels over USB
1164 * that were, in fact, unchanged - wasting limited USB bandwidth
1166 new_back
= vmalloc(new_len
);
1168 dl_info("No shadow/backing buffer allcoated\n");
1170 if (dev
->backing_buffer
)
1171 vfree(dev
->backing_buffer
);
1172 dev
->backing_buffer
= new_back
;
1173 memset(dev
->backing_buffer
, 0, new_len
);
1184 * 1) Get EDID from hw, or use sw default
1185 * 2) Parse into various fb_info structs
1186 * 3) Allocate virtual framebuffer memory to back highest res mode
1188 * Parses EDID into three places used by various parts of fbdev:
1189 * fb_var_screeninfo contains the timing of the monitor's preferred mode
1190 * fb_info.monspecs is full parsed EDID info, including monspecs.modedb
1191 * fb_info.modelist is a linked list of all monitor & VESA modes which work
1193 * If EDID is not readable/valid, then modelist is all VESA modes,
1194 * monspecs is NULL, and fb_var_screeninfo is set to safe VESA mode
1195 * Returns 0 if successful
1197 static int dlfb_setup_modes(struct dlfb_data
*dev
,
1198 struct fb_info
*info
,
1199 char *default_edid
, size_t default_edid_size
)
1202 const struct fb_videomode
*default_vmode
= NULL
;
1207 if (info
->dev
) /* only use mutex if info has been registered */
1208 mutex_lock(&info
->lock
);
1210 edid
= kmalloc(MAX_EDID_SIZE
, GFP_KERNEL
);
1216 fb_destroy_modelist(&info
->modelist
);
1217 memset(&info
->monspecs
, 0, sizeof(info
->monspecs
));
1220 * Try to (re)read EDID from hardware first
1221 * EDID data may return, but not parse as valid
1222 * Try again a few times, in case of e.g. analog cable noise
1226 i
= dlfb_get_edid(dev
, edid
, MAX_EDID_SIZE
);
1228 if (i
>= MIN_EDID_SIZE
)
1229 fb_edid_to_monspecs(edid
, &info
->monspecs
);
1231 if (info
->monspecs
.modedb_len
> 0) {
1238 /* If that fails, use a previously returned EDID if available */
1239 if (info
->monspecs
.modedb_len
== 0) {
1241 dl_err("Unable to get valid EDID from device/display\n");
1244 fb_edid_to_monspecs(dev
->edid
, &info
->monspecs
);
1245 if (info
->monspecs
.modedb_len
> 0)
1246 dl_err("Using previously queried EDID\n");
1250 /* If that fails, use the default EDID we were handed */
1251 if (info
->monspecs
.modedb_len
== 0) {
1252 if (default_edid_size
>= MIN_EDID_SIZE
) {
1253 fb_edid_to_monspecs(default_edid
, &info
->monspecs
);
1254 if (info
->monspecs
.modedb_len
> 0) {
1255 memcpy(edid
, default_edid
, default_edid_size
);
1257 dev
->edid_size
= default_edid_size
;
1258 dl_err("Using default/backup EDID\n");
1263 /* If we've got modes, let's pick a best default mode */
1264 if (info
->monspecs
.modedb_len
> 0) {
1266 for (i
= 0; i
< info
->monspecs
.modedb_len
; i
++) {
1267 if (dlfb_is_valid_mode(&info
->monspecs
.modedb
[i
], info
))
1268 fb_add_videomode(&info
->monspecs
.modedb
[i
],
1270 else /* if we've removed top/best mode */
1271 info
->monspecs
.misc
&= ~FB_MISC_1ST_DETAIL
;
1274 default_vmode
= fb_find_best_display(&info
->monspecs
,
1278 /* If everything else has failed, fall back to safe default mode */
1279 if (default_vmode
== NULL
) {
1281 struct fb_videomode fb_vmode
= {0};
1284 * Add the standard VESA modes to our modelist
1285 * Since we don't have EDID, there may be modes that
1286 * overspec monitor and/or are incorrect aspect ratio, etc.
1287 * But at least the user has a chance to choose
1289 for (i
= 0; i
< VESA_MODEDB_SIZE
; i
++) {
1290 if (dlfb_is_valid_mode((struct fb_videomode
*)
1291 &vesa_modes
[i
], info
))
1292 fb_add_videomode(&vesa_modes
[i
],
1297 * default to resolution safe for projectors
1298 * (since they are most common case without EDID)
1300 fb_vmode
.xres
= 800;
1301 fb_vmode
.yres
= 600;
1302 fb_vmode
.refresh
= 60;
1303 default_vmode
= fb_find_nearest_mode(&fb_vmode
,
1307 /* If we have good mode and no active clients*/
1308 if ((default_vmode
!= NULL
) && (dev
->fb_count
== 0)) {
1310 fb_videomode_to_var(&info
->var
, default_vmode
);
1311 dlfb_var_color_format(&info
->var
);
1314 * with mode size info, we can now alloc our framebuffer.
1316 memcpy(&info
->fix
, &dlfb_fix
, sizeof(dlfb_fix
));
1317 info
->fix
.line_length
= info
->var
.xres
*
1318 (info
->var
.bits_per_pixel
/ 8);
1320 result
= dlfb_realloc_framebuffer(dev
, info
);
1326 if (edid
&& (dev
->edid
!= edid
))
1330 mutex_unlock(&info
->lock
);
1335 static ssize_t
metrics_bytes_rendered_show(struct device
*fbdev
,
1336 struct device_attribute
*a
, char *buf
) {
1337 struct fb_info
*fb_info
= dev_get_drvdata(fbdev
);
1338 struct dlfb_data
*dev
= fb_info
->par
;
1339 return snprintf(buf
, PAGE_SIZE
, "%u\n",
1340 atomic_read(&dev
->bytes_rendered
));
1343 static ssize_t
metrics_bytes_identical_show(struct device
*fbdev
,
1344 struct device_attribute
*a
, char *buf
) {
1345 struct fb_info
*fb_info
= dev_get_drvdata(fbdev
);
1346 struct dlfb_data
*dev
= fb_info
->par
;
1347 return snprintf(buf
, PAGE_SIZE
, "%u\n",
1348 atomic_read(&dev
->bytes_identical
));
1351 static ssize_t
metrics_bytes_sent_show(struct device
*fbdev
,
1352 struct device_attribute
*a
, char *buf
) {
1353 struct fb_info
*fb_info
= dev_get_drvdata(fbdev
);
1354 struct dlfb_data
*dev
= fb_info
->par
;
1355 return snprintf(buf
, PAGE_SIZE
, "%u\n",
1356 atomic_read(&dev
->bytes_sent
));
1359 static ssize_t
metrics_cpu_kcycles_used_show(struct device
*fbdev
,
1360 struct device_attribute
*a
, char *buf
) {
1361 struct fb_info
*fb_info
= dev_get_drvdata(fbdev
);
1362 struct dlfb_data
*dev
= fb_info
->par
;
1363 return snprintf(buf
, PAGE_SIZE
, "%u\n",
1364 atomic_read(&dev
->cpu_kcycles_used
));
1367 static ssize_t
edid_show(
1369 struct kobject
*kobj
, struct bin_attribute
*a
,
1370 char *buf
, loff_t off
, size_t count
) {
1371 struct device
*fbdev
= container_of(kobj
, struct device
, kobj
);
1372 struct fb_info
*fb_info
= dev_get_drvdata(fbdev
);
1373 struct dlfb_data
*dev
= fb_info
->par
;
1375 if (dev
->edid
== NULL
)
1378 if ((off
>= dev
->edid_size
) || (count
> dev
->edid_size
))
1381 if (off
+ count
> dev
->edid_size
)
1382 count
= dev
->edid_size
- off
;
1384 dl_info("sysfs edid copy %p to %p, %d bytes\n",
1385 dev
->edid
, buf
, (int) count
);
1387 memcpy(buf
, dev
->edid
, count
);
1392 static ssize_t
edid_store(
1394 struct kobject
*kobj
, struct bin_attribute
*a
,
1395 char *src
, loff_t src_off
, size_t src_size
) {
1396 struct device
*fbdev
= container_of(kobj
, struct device
, kobj
);
1397 struct fb_info
*fb_info
= dev_get_drvdata(fbdev
);
1398 struct dlfb_data
*dev
= fb_info
->par
;
1400 /* We only support write of entire EDID at once, no offset*/
1401 if ((src_size
< MIN_EDID_SIZE
) ||
1402 (src_size
> MAX_EDID_SIZE
) ||
1406 dlfb_setup_modes(dev
, fb_info
, src
, src_size
);
1408 if (dev
->edid
&& (memcmp(src
, dev
->edid
, src_size
) == 0)) {
1409 dl_info("sysfs written EDID is new default\n");
1410 dlfb_ops_set_par(fb_info
);
1416 static ssize_t
metrics_reset_store(struct device
*fbdev
,
1417 struct device_attribute
*attr
,
1418 const char *buf
, size_t count
)
1420 struct fb_info
*fb_info
= dev_get_drvdata(fbdev
);
1421 struct dlfb_data
*dev
= fb_info
->par
;
1423 atomic_set(&dev
->bytes_rendered
, 0);
1424 atomic_set(&dev
->bytes_identical
, 0);
1425 atomic_set(&dev
->bytes_sent
, 0);
1426 atomic_set(&dev
->cpu_kcycles_used
, 0);
1431 static struct bin_attribute edid_attr
= {
1432 .attr
.name
= "edid",
1434 .size
= MAX_EDID_SIZE
,
1439 static struct device_attribute fb_device_attrs
[] = {
1440 __ATTR_RO(metrics_bytes_rendered
),
1441 __ATTR_RO(metrics_bytes_identical
),
1442 __ATTR_RO(metrics_bytes_sent
),
1443 __ATTR_RO(metrics_cpu_kcycles_used
),
1444 __ATTR(metrics_reset
, S_IWUGO
, NULL
, metrics_reset_store
),
1448 * This is necessary before we can communicate with the display controller.
1450 static int dlfb_select_std_channel(struct dlfb_data
*dev
)
1453 u8 set_def_chn
[] = { 0x57, 0xCD, 0xDC, 0xA7,
1454 0x1C, 0x88, 0x5E, 0x15,
1455 0x60, 0xFE, 0xC6, 0x97,
1456 0x16, 0x3D, 0x47, 0xF2 };
1458 ret
= usb_control_msg(dev
->udev
, usb_sndctrlpipe(dev
->udev
, 0),
1459 NR_USB_REQUEST_CHANNEL
,
1460 (USB_DIR_OUT
| USB_TYPE_VENDOR
), 0, 0,
1461 set_def_chn
, sizeof(set_def_chn
), USB_CTRL_SET_TIMEOUT
);
1465 static int dlfb_parse_vendor_descriptor(struct dlfb_data
*dev
,
1466 struct usb_device
*usbdev
)
1474 buf
= kzalloc(MAX_VENDOR_DESCRIPTOR_SIZE
, GFP_KERNEL
);
1479 total_len
= usb_get_descriptor(usbdev
, 0x5f, /* vendor specific */
1480 0, desc
, MAX_VENDOR_DESCRIPTOR_SIZE
);
1481 if (total_len
> 5) {
1482 dl_info("vendor descriptor length:%x data:%02x %02x %02x %02x" \
1483 "%02x %02x %02x %02x %02x %02x %02x\n",
1485 desc
[1], desc
[2], desc
[3], desc
[4], desc
[5], desc
[6],
1486 desc
[7], desc
[8], desc
[9], desc
[10]);
1488 if ((desc
[0] != total_len
) || /* descriptor length */
1489 (desc
[1] != 0x5f) || /* vendor descriptor type */
1490 (desc
[2] != 0x01) || /* version (2 bytes) */
1491 (desc
[3] != 0x00) ||
1492 (desc
[4] != total_len
- 2)) /* length after type */
1495 desc_end
= desc
+ total_len
;
1496 desc
+= 5; /* the fixed header we've already parsed */
1498 while (desc
< desc_end
) {
1502 key
= *((u16
*) desc
);
1503 desc
+= sizeof(u16
);
1508 case 0x0200: { /* max_area */
1510 max_area
= le32_to_cpu(*((u32
*)desc
));
1511 dl_warn("DL chip limited to %d pixel modes\n",
1513 dev
->sku_pixel_limit
= max_area
;
1526 /* allow udlfb to load for now even if firmware unrecognized */
1527 dl_err("Unrecognized vendor firmware descriptor\n");
1533 static int dlfb_usb_probe(struct usb_interface
*interface
,
1534 const struct usb_device_id
*id
)
1536 struct usb_device
*usbdev
;
1537 struct dlfb_data
*dev
= 0;
1538 struct fb_info
*info
= 0;
1539 int retval
= -ENOMEM
;
1542 /* usb initialization */
1544 usbdev
= interface_to_usbdev(interface
);
1546 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
1548 err("dlfb_usb_probe: failed alloc of dev struct\n");
1552 /* we need to wait for both usb and fbdev to spin down on disconnect */
1553 kref_init(&dev
->kref
); /* matching kref_put in usb .disconnect fn */
1554 kref_get(&dev
->kref
); /* matching kref_put in free_framebuffer_work */
1557 dev
->gdev
= &usbdev
->dev
; /* our generic struct device * */
1558 usb_set_intfdata(interface
, dev
);
1560 dl_info("%s %s - serial #%s\n",
1561 usbdev
->manufacturer
, usbdev
->product
, usbdev
->serial
);
1562 dl_info("vid_%04x&pid_%04x&rev_%04x driver's dlfb_data struct at %p\n",
1563 usbdev
->descriptor
.idVendor
, usbdev
->descriptor
.idProduct
,
1564 usbdev
->descriptor
.bcdDevice
, dev
);
1565 dl_info("console enable=%d\n", console
);
1566 dl_info("fb_defio enable=%d\n", fb_defio
);
1568 dev
->sku_pixel_limit
= 2048 * 1152; /* default to maximum */
1570 if (!dlfb_parse_vendor_descriptor(dev
, usbdev
)) {
1571 dl_err("firmware not recognized. Assume incompatible device\n");
1575 if (!dlfb_alloc_urb_list(dev
, WRITES_IN_FLIGHT
, MAX_TRANSFER
)) {
1577 dl_err("dlfb_alloc_urb_list failed\n");
1581 /* We don't register a new USB class. Our client interface is fbdev */
1583 /* allocates framebuffer driver structure, not framebuffer memory */
1584 info
= framebuffer_alloc(0, &usbdev
->dev
);
1587 dl_err("framebuffer_alloc failed\n");
1593 info
->pseudo_palette
= dev
->pseudo_palette
;
1594 info
->fbops
= &dlfb_ops
;
1596 retval
= fb_alloc_cmap(&info
->cmap
, 256, 0);
1598 dl_err("fb_alloc_cmap failed %x\n", retval
);
1602 INIT_DELAYED_WORK(&dev
->free_framebuffer_work
,
1603 dlfb_free_framebuffer_work
);
1605 INIT_LIST_HEAD(&info
->modelist
);
1607 retval
= dlfb_setup_modes(dev
, info
, NULL
, 0);
1609 dl_err("unable to find common mode for display and adapter\n");
1613 /* ready to begin using device */
1615 atomic_set(&dev
->usb_active
, 1);
1616 dlfb_select_std_channel(dev
);
1618 dlfb_ops_check_var(&info
->var
, info
);
1619 dlfb_ops_set_par(info
);
1621 retval
= register_framebuffer(info
);
1623 dl_err("register_framebuffer failed %d\n", retval
);
1627 for (i
= 0; i
< ARRAY_SIZE(fb_device_attrs
); i
++)
1628 device_create_file(info
->dev
, &fb_device_attrs
[i
]);
1630 device_create_bin_file(info
->dev
, &edid_attr
);
1632 dl_info("DisplayLink USB device /dev/fb%d attached. %dx%d resolution."
1633 " Using %dK framebuffer memory\n", info
->node
,
1634 info
->var
.xres
, info
->var
.yres
,
1635 ((dev
->backing_buffer
) ?
1636 info
->fix
.smem_len
* 2 : info
->fix
.smem_len
) >> 10);
1643 if (info
->cmap
.len
!= 0)
1644 fb_dealloc_cmap(&info
->cmap
);
1645 if (info
->monspecs
.modedb
)
1646 fb_destroy_modedb(info
->monspecs
.modedb
);
1647 if (info
->screen_base
)
1648 vfree(info
->screen_base
);
1650 fb_destroy_modelist(&info
->modelist
);
1652 framebuffer_release(info
);
1655 if (dev
->backing_buffer
)
1656 vfree(dev
->backing_buffer
);
1658 kref_put(&dev
->kref
, dlfb_free
); /* ref for framebuffer */
1659 kref_put(&dev
->kref
, dlfb_free
); /* last ref from kref_init */
1661 /* dev has been deallocated. Do not dereference */
1667 static void dlfb_usb_disconnect(struct usb_interface
*interface
)
1669 struct dlfb_data
*dev
;
1670 struct fb_info
*info
;
1673 dev
= usb_get_intfdata(interface
);
1676 dl_info("USB disconnect starting\n");
1678 /* we virtualize until all fb clients release. Then we free */
1679 dev
->virtualized
= true;
1681 /* When non-active we'll update virtual framebuffer, but no new urbs */
1682 atomic_set(&dev
->usb_active
, 0);
1684 /* remove udlfb's sysfs interfaces */
1685 for (i
= 0; i
< ARRAY_SIZE(fb_device_attrs
); i
++)
1686 device_remove_file(info
->dev
, &fb_device_attrs
[i
]);
1687 device_remove_bin_file(info
->dev
, &edid_attr
);
1689 usb_set_intfdata(interface
, NULL
);
1691 /* if clients still have us open, will be freed on last close */
1692 if (dev
->fb_count
== 0)
1693 schedule_delayed_work(&dev
->free_framebuffer_work
, 0);
1695 /* release reference taken by kref_init in probe() */
1696 kref_put(&dev
->kref
, dlfb_free
);
1698 /* consider dlfb_data freed */
1703 static struct usb_driver dlfb_driver
= {
1705 .probe
= dlfb_usb_probe
,
1706 .disconnect
= dlfb_usb_disconnect
,
1707 .id_table
= id_table
,
1710 static int __init
dlfb_module_init(void)
1714 res
= usb_register(&dlfb_driver
);
1716 err("usb_register failed. Error number %d", res
);
1721 static void __exit
dlfb_module_exit(void)
1723 usb_deregister(&dlfb_driver
);
1726 module_init(dlfb_module_init
);
1727 module_exit(dlfb_module_exit
);
1729 static void dlfb_urb_completion(struct urb
*urb
)
1731 struct urb_node
*unode
= urb
->context
;
1732 struct dlfb_data
*dev
= unode
->dev
;
1733 unsigned long flags
;
1735 /* sync/async unlink faults aren't errors */
1737 if (!(urb
->status
== -ENOENT
||
1738 urb
->status
== -ECONNRESET
||
1739 urb
->status
== -ESHUTDOWN
)) {
1740 dl_err("%s - nonzero write bulk status received: %d\n",
1741 __func__
, urb
->status
);
1742 atomic_set(&dev
->lost_pixels
, 1);
1746 urb
->transfer_buffer_length
= dev
->urbs
.size
; /* reset to actual */
1748 spin_lock_irqsave(&dev
->urbs
.lock
, flags
);
1749 list_add_tail(&unode
->entry
, &dev
->urbs
.list
);
1750 dev
->urbs
.available
++;
1751 spin_unlock_irqrestore(&dev
->urbs
.lock
, flags
);
1754 * When using fb_defio, we deadlock if up() is called
1755 * while another is waiting. So queue to another process.
1758 schedule_delayed_work(&unode
->release_urb_work
, 0);
1760 up(&dev
->urbs
.limit_sem
);
1763 static void dlfb_free_urb_list(struct dlfb_data
*dev
)
1765 int count
= dev
->urbs
.count
;
1766 struct list_head
*node
;
1767 struct urb_node
*unode
;
1770 unsigned long flags
;
1772 dl_notice("Waiting for completes and freeing all render urbs\n");
1774 /* keep waiting and freeing, until we've got 'em all */
1777 /* Getting interrupted means a leak, but ok at shutdown*/
1778 ret
= down_interruptible(&dev
->urbs
.limit_sem
);
1782 spin_lock_irqsave(&dev
->urbs
.lock
, flags
);
1784 node
= dev
->urbs
.list
.next
; /* have reserved one with sem */
1785 list_del_init(node
);
1787 spin_unlock_irqrestore(&dev
->urbs
.lock
, flags
);
1789 unode
= list_entry(node
, struct urb_node
, entry
);
1792 /* Free each separately allocated piece */
1793 usb_free_coherent(urb
->dev
, dev
->urbs
.size
,
1794 urb
->transfer_buffer
, urb
->transfer_dma
);
1801 static int dlfb_alloc_urb_list(struct dlfb_data
*dev
, int count
, size_t size
)
1805 struct urb_node
*unode
;
1808 spin_lock_init(&dev
->urbs
.lock
);
1810 dev
->urbs
.size
= size
;
1811 INIT_LIST_HEAD(&dev
->urbs
.list
);
1814 unode
= kzalloc(sizeof(struct urb_node
), GFP_KERNEL
);
1819 INIT_DELAYED_WORK(&unode
->release_urb_work
,
1820 dlfb_release_urb_work
);
1822 urb
= usb_alloc_urb(0, GFP_KERNEL
);
1829 buf
= usb_alloc_coherent(dev
->udev
, MAX_TRANSFER
, GFP_KERNEL
,
1830 &urb
->transfer_dma
);
1837 /* urb->transfer_buffer_length set to actual before submit */
1838 usb_fill_bulk_urb(urb
, dev
->udev
, usb_sndbulkpipe(dev
->udev
, 1),
1839 buf
, size
, dlfb_urb_completion
, unode
);
1840 urb
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
1842 list_add_tail(&unode
->entry
, &dev
->urbs
.list
);
1847 sema_init(&dev
->urbs
.limit_sem
, i
);
1848 dev
->urbs
.count
= i
;
1849 dev
->urbs
.available
= i
;
1851 dl_notice("allocated %d %d byte urbs\n", i
, (int) size
);
1856 static struct urb
*dlfb_get_urb(struct dlfb_data
*dev
)
1859 struct list_head
*entry
;
1860 struct urb_node
*unode
;
1861 struct urb
*urb
= NULL
;
1862 unsigned long flags
;
1864 /* Wait for an in-flight buffer to complete and get re-queued */
1865 ret
= down_timeout(&dev
->urbs
.limit_sem
, GET_URB_TIMEOUT
);
1867 atomic_set(&dev
->lost_pixels
, 1);
1868 dl_warn("wait for urb interrupted: %x available: %d\n",
1869 ret
, dev
->urbs
.available
);
1873 spin_lock_irqsave(&dev
->urbs
.lock
, flags
);
1875 BUG_ON(list_empty(&dev
->urbs
.list
)); /* reserved one with limit_sem */
1876 entry
= dev
->urbs
.list
.next
;
1877 list_del_init(entry
);
1878 dev
->urbs
.available
--;
1880 spin_unlock_irqrestore(&dev
->urbs
.lock
, flags
);
1882 unode
= list_entry(entry
, struct urb_node
, entry
);
1889 static int dlfb_submit_urb(struct dlfb_data
*dev
, struct urb
*urb
, size_t len
)
1893 BUG_ON(len
> dev
->urbs
.size
);
1895 urb
->transfer_buffer_length
= len
; /* set to actual payload len */
1896 ret
= usb_submit_urb(urb
, GFP_KERNEL
);
1898 dlfb_urb_completion(urb
); /* because no one else will */
1899 atomic_set(&dev
->lost_pixels
, 1);
1900 dl_err("usb_submit_urb error %x\n", ret
);
1905 module_param(console
, bool, S_IWUSR
| S_IRUSR
| S_IWGRP
| S_IRGRP
);
1906 MODULE_PARM_DESC(console
, "Allow fbcon to consume first framebuffer found");
1908 module_param(fb_defio
, bool, S_IWUSR
| S_IRUSR
| S_IWGRP
| S_IRGRP
);
1909 MODULE_PARM_DESC(fb_defio
, "Enable fb_defio mmap support. *Experimental*");
1911 MODULE_AUTHOR("Roberto De Ioris <roberto@unbit.it>, "
1912 "Jaya Kumar <jayakumar.lkml@gmail.com>, "
1913 "Bernie Thompson <bernie@plugable.com>");
1914 MODULE_DESCRIPTION("DisplayLink kernel framebuffer driver");
1915 MODULE_LICENSE("GPL");