gma500: add an mmap ioctl
[linux-2.6/next.git] / drivers / video / udlfb.c
blob816a4fda04f5b9c0637721054a5c7aba1a456932
1 /*
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
10 * more details.
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>
26 #include <linux/mm.h>
27 #include <linux/fb.h>
28 #include <linux/vmalloc.h>
29 #include <linux/slab.h>
30 #include <linux/prefetch.h>
31 #include <linux/delay.h>
32 #include <linux/prefetch.h>
33 #include <video/udlfb.h>
34 #include "edid.h"
36 static struct fb_fix_screeninfo dlfb_fix = {
37 .id = "udlfb",
38 .type = FB_TYPE_PACKED_PIXELS,
39 .visual = FB_VISUAL_TRUECOLOR,
40 .xpanstep = 0,
41 .ypanstep = 0,
42 .ywrapstep = 0,
43 .accel = FB_ACCEL_NONE,
46 static const u32 udlfb_info_flags = FBINFO_DEFAULT | FBINFO_READS_FAST |
47 FBINFO_VIRTFB |
48 FBINFO_HWACCEL_IMAGEBLIT | FBINFO_HWACCEL_FILLRECT |
49 FBINFO_HWACCEL_COPYAREA | FBINFO_MISC_ALWAYS_SETPAR;
52 * There are many DisplayLink-based products, all with unique PIDs. We are able
53 * to support all volume ones (circa 2009) with a single driver, so we match
54 * globally on VID. TODO: Probe() needs to detect when we might be running
55 * "future" chips, and bail on those, so a compatible driver can match.
57 static struct usb_device_id id_table[] = {
58 {.idVendor = 0x17e9, .match_flags = USB_DEVICE_ID_MATCH_VENDOR,},
59 {},
61 MODULE_DEVICE_TABLE(usb, id_table);
63 /* module options */
64 static int console; /* Optionally allow fbcon to consume first framebuffer */
65 static int fb_defio; /* Optionally enable experimental fb_defio mmap support */
67 /* dlfb keeps a list of urbs for efficient bulk transfers */
68 static void dlfb_urb_completion(struct urb *urb);
69 static struct urb *dlfb_get_urb(struct dlfb_data *dev);
70 static int dlfb_submit_urb(struct dlfb_data *dev, struct urb * urb, size_t len);
71 static int dlfb_alloc_urb_list(struct dlfb_data *dev, int count, size_t size);
72 static void dlfb_free_urb_list(struct dlfb_data *dev);
75 * All DisplayLink bulk operations start with 0xAF, followed by specific code
76 * All operations are written to buffers which then later get sent to device
78 static char *dlfb_set_register(char *buf, u8 reg, u8 val)
80 *buf++ = 0xAF;
81 *buf++ = 0x20;
82 *buf++ = reg;
83 *buf++ = val;
84 return buf;
87 static char *dlfb_vidreg_lock(char *buf)
89 return dlfb_set_register(buf, 0xFF, 0x00);
92 static char *dlfb_vidreg_unlock(char *buf)
94 return dlfb_set_register(buf, 0xFF, 0xFF);
98 * On/Off for driving the DisplayLink framebuffer to the display
99 * 0x00 H and V sync on
100 * 0x01 H and V sync off (screen blank but powered)
101 * 0x07 DPMS powerdown (requires modeset to come back)
103 static char *dlfb_enable_hvsync(char *buf, bool enable)
105 if (enable)
106 return dlfb_set_register(buf, 0x1F, 0x00);
107 else
108 return dlfb_set_register(buf, 0x1F, 0x07);
111 static char *dlfb_set_color_depth(char *buf, u8 selection)
113 return dlfb_set_register(buf, 0x00, selection);
116 static char *dlfb_set_base16bpp(char *wrptr, u32 base)
118 /* the base pointer is 16 bits wide, 0x20 is hi byte. */
119 wrptr = dlfb_set_register(wrptr, 0x20, base >> 16);
120 wrptr = dlfb_set_register(wrptr, 0x21, base >> 8);
121 return dlfb_set_register(wrptr, 0x22, base);
125 * DisplayLink HW has separate 16bpp and 8bpp framebuffers.
126 * In 24bpp modes, the low 323 RGB bits go in the 8bpp framebuffer
128 static char *dlfb_set_base8bpp(char *wrptr, u32 base)
130 wrptr = dlfb_set_register(wrptr, 0x26, base >> 16);
131 wrptr = dlfb_set_register(wrptr, 0x27, base >> 8);
132 return dlfb_set_register(wrptr, 0x28, base);
135 static char *dlfb_set_register_16(char *wrptr, u8 reg, u16 value)
137 wrptr = dlfb_set_register(wrptr, reg, value >> 8);
138 return dlfb_set_register(wrptr, reg+1, value);
142 * This is kind of weird because the controller takes some
143 * register values in a different byte order than other registers.
145 static char *dlfb_set_register_16be(char *wrptr, u8 reg, u16 value)
147 wrptr = dlfb_set_register(wrptr, reg, value);
148 return dlfb_set_register(wrptr, reg+1, value >> 8);
152 * LFSR is linear feedback shift register. The reason we have this is
153 * because the display controller needs to minimize the clock depth of
154 * various counters used in the display path. So this code reverses the
155 * provided value into the lfsr16 value by counting backwards to get
156 * the value that needs to be set in the hardware comparator to get the
157 * same actual count. This makes sense once you read above a couple of
158 * times and think about it from a hardware perspective.
160 static u16 dlfb_lfsr16(u16 actual_count)
162 u32 lv = 0xFFFF; /* This is the lfsr value that the hw starts with */
164 while (actual_count--) {
165 lv = ((lv << 1) |
166 (((lv >> 15) ^ (lv >> 4) ^ (lv >> 2) ^ (lv >> 1)) & 1))
167 & 0xFFFF;
170 return (u16) lv;
174 * This does LFSR conversion on the value that is to be written.
175 * See LFSR explanation above for more detail.
177 static char *dlfb_set_register_lfsr16(char *wrptr, u8 reg, u16 value)
179 return dlfb_set_register_16(wrptr, reg, dlfb_lfsr16(value));
183 * This takes a standard fbdev screeninfo struct and all of its monitor mode
184 * details and converts them into the DisplayLink equivalent register commands.
186 static char *dlfb_set_vid_cmds(char *wrptr, struct fb_var_screeninfo *var)
188 u16 xds, yds;
189 u16 xde, yde;
190 u16 yec;
192 /* x display start */
193 xds = var->left_margin + var->hsync_len;
194 wrptr = dlfb_set_register_lfsr16(wrptr, 0x01, xds);
195 /* x display end */
196 xde = xds + var->xres;
197 wrptr = dlfb_set_register_lfsr16(wrptr, 0x03, xde);
199 /* y display start */
200 yds = var->upper_margin + var->vsync_len;
201 wrptr = dlfb_set_register_lfsr16(wrptr, 0x05, yds);
202 /* y display end */
203 yde = yds + var->yres;
204 wrptr = dlfb_set_register_lfsr16(wrptr, 0x07, yde);
206 /* x end count is active + blanking - 1 */
207 wrptr = dlfb_set_register_lfsr16(wrptr, 0x09,
208 xde + var->right_margin - 1);
210 /* libdlo hardcodes hsync start to 1 */
211 wrptr = dlfb_set_register_lfsr16(wrptr, 0x0B, 1);
213 /* hsync end is width of sync pulse + 1 */
214 wrptr = dlfb_set_register_lfsr16(wrptr, 0x0D, var->hsync_len + 1);
216 /* hpixels is active pixels */
217 wrptr = dlfb_set_register_16(wrptr, 0x0F, var->xres);
219 /* yendcount is vertical active + vertical blanking */
220 yec = var->yres + var->upper_margin + var->lower_margin +
221 var->vsync_len;
222 wrptr = dlfb_set_register_lfsr16(wrptr, 0x11, yec);
224 /* libdlo hardcodes vsync start to 0 */
225 wrptr = dlfb_set_register_lfsr16(wrptr, 0x13, 0);
227 /* vsync end is width of vsync pulse */
228 wrptr = dlfb_set_register_lfsr16(wrptr, 0x15, var->vsync_len);
230 /* vpixels is active pixels */
231 wrptr = dlfb_set_register_16(wrptr, 0x17, var->yres);
233 /* convert picoseconds to 5kHz multiple for pclk5k = x * 1E12/5k */
234 wrptr = dlfb_set_register_16be(wrptr, 0x1B,
235 200*1000*1000/var->pixclock);
237 return wrptr;
241 * This takes a standard fbdev screeninfo struct that was fetched or prepared
242 * and then generates the appropriate command sequence that then drives the
243 * display controller.
245 static int dlfb_set_video_mode(struct dlfb_data *dev,
246 struct fb_var_screeninfo *var)
248 char *buf;
249 char *wrptr;
250 int retval = 0;
251 int writesize;
252 struct urb *urb;
254 if (!atomic_read(&dev->usb_active))
255 return -EPERM;
257 urb = dlfb_get_urb(dev);
258 if (!urb)
259 return -ENOMEM;
261 buf = (char *) urb->transfer_buffer;
264 * This first section has to do with setting the base address on the
265 * controller * associated with the display. There are 2 base
266 * pointers, currently, we only * use the 16 bpp segment.
268 wrptr = dlfb_vidreg_lock(buf);
269 wrptr = dlfb_set_color_depth(wrptr, 0x00);
270 /* set base for 16bpp segment to 0 */
271 wrptr = dlfb_set_base16bpp(wrptr, 0);
272 /* set base for 8bpp segment to end of fb */
273 wrptr = dlfb_set_base8bpp(wrptr, dev->info->fix.smem_len);
275 wrptr = dlfb_set_vid_cmds(wrptr, var);
276 wrptr = dlfb_enable_hvsync(wrptr, true);
277 wrptr = dlfb_vidreg_unlock(wrptr);
279 writesize = wrptr - buf;
281 retval = dlfb_submit_urb(dev, urb, writesize);
283 return retval;
286 static int dlfb_ops_mmap(struct fb_info *info, struct vm_area_struct *vma)
288 unsigned long start = vma->vm_start;
289 unsigned long size = vma->vm_end - vma->vm_start;
290 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
291 unsigned long page, pos;
293 if (offset + size > info->fix.smem_len)
294 return -EINVAL;
296 pos = (unsigned long)info->fix.smem_start + offset;
298 pr_notice("mmap() framebuffer addr:%lu size:%lu\n",
299 pos, size);
301 while (size > 0) {
302 page = vmalloc_to_pfn((void *)pos);
303 if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED))
304 return -EAGAIN;
306 start += PAGE_SIZE;
307 pos += PAGE_SIZE;
308 if (size > PAGE_SIZE)
309 size -= PAGE_SIZE;
310 else
311 size = 0;
314 vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
315 return 0;
319 * Trims identical data from front and back of line
320 * Sets new front buffer address and width
321 * And returns byte count of identical pixels
322 * Assumes CPU natural alignment (unsigned long)
323 * for back and front buffer ptrs and width
325 static int dlfb_trim_hline(const u8 *bback, const u8 **bfront, int *width_bytes)
327 int j, k;
328 const unsigned long *back = (const unsigned long *) bback;
329 const unsigned long *front = (const unsigned long *) *bfront;
330 const int width = *width_bytes / sizeof(unsigned long);
331 int identical = width;
332 int start = width;
333 int end = width;
335 prefetch((void *) front);
336 prefetch((void *) back);
338 for (j = 0; j < width; j++) {
339 if (back[j] != front[j]) {
340 start = j;
341 break;
345 for (k = width - 1; k > j; k--) {
346 if (back[k] != front[k]) {
347 end = k+1;
348 break;
352 identical = start + (width - end);
353 *bfront = (u8 *) &front[start];
354 *width_bytes = (end - start) * sizeof(unsigned long);
356 return identical * sizeof(unsigned long);
360 * Render a command stream for an encoded horizontal line segment of pixels.
362 * A command buffer holds several commands.
363 * It always begins with a fresh command header
364 * (the protocol doesn't require this, but we enforce it to allow
365 * multiple buffers to be potentially encoded and sent in parallel).
366 * A single command encodes one contiguous horizontal line of pixels
368 * The function relies on the client to do all allocation, so that
369 * rendering can be done directly to output buffers (e.g. USB URBs).
370 * The function fills the supplied command buffer, providing information
371 * on where it left off, so the client may call in again with additional
372 * buffers if the line will take several buffers to complete.
374 * A single command can transmit a maximum of 256 pixels,
375 * regardless of the compression ratio (protocol design limit).
376 * To the hardware, 0 for a size byte means 256
378 * Rather than 256 pixel commands which are either rl or raw encoded,
379 * the rlx command simply assumes alternating raw and rl spans within one cmd.
380 * This has a slightly larger header overhead, but produces more even results.
381 * It also processes all data (read and write) in a single pass.
382 * Performance benchmarks of common cases show it having just slightly better
383 * compression than 256 pixel raw or rle commands, with similar CPU consumpion.
384 * But for very rl friendly data, will compress not quite as well.
386 static void dlfb_compress_hline(
387 const uint16_t **pixel_start_ptr,
388 const uint16_t *const pixel_end,
389 uint32_t *device_address_ptr,
390 uint8_t **command_buffer_ptr,
391 const uint8_t *const cmd_buffer_end)
393 const uint16_t *pixel = *pixel_start_ptr;
394 uint32_t dev_addr = *device_address_ptr;
395 uint8_t *cmd = *command_buffer_ptr;
396 const int bpp = 2;
398 while ((pixel_end > pixel) &&
399 (cmd_buffer_end - MIN_RLX_CMD_BYTES > cmd)) {
400 uint8_t *raw_pixels_count_byte = 0;
401 uint8_t *cmd_pixels_count_byte = 0;
402 const uint16_t *raw_pixel_start = 0;
403 const uint16_t *cmd_pixel_start, *cmd_pixel_end = 0;
405 prefetchw((void *) cmd); /* pull in one cache line at least */
407 *cmd++ = 0xAF;
408 *cmd++ = 0x6B;
409 *cmd++ = (uint8_t) ((dev_addr >> 16) & 0xFF);
410 *cmd++ = (uint8_t) ((dev_addr >> 8) & 0xFF);
411 *cmd++ = (uint8_t) ((dev_addr) & 0xFF);
413 cmd_pixels_count_byte = cmd++; /* we'll know this later */
414 cmd_pixel_start = pixel;
416 raw_pixels_count_byte = cmd++; /* we'll know this later */
417 raw_pixel_start = pixel;
419 cmd_pixel_end = pixel + min(MAX_CMD_PIXELS + 1,
420 min((int)(pixel_end - pixel),
421 (int)(cmd_buffer_end - cmd) / bpp));
423 prefetch_range((void *) pixel, (cmd_pixel_end - pixel) * bpp);
425 while (pixel < cmd_pixel_end) {
426 const uint16_t * const repeating_pixel = pixel;
428 *(uint16_t *)cmd = cpu_to_be16p(pixel);
429 cmd += 2;
430 pixel++;
432 if (unlikely((pixel < cmd_pixel_end) &&
433 (*pixel == *repeating_pixel))) {
434 /* go back and fill in raw pixel count */
435 *raw_pixels_count_byte = ((repeating_pixel -
436 raw_pixel_start) + 1) & 0xFF;
438 while ((pixel < cmd_pixel_end)
439 && (*pixel == *repeating_pixel)) {
440 pixel++;
443 /* immediately after raw data is repeat byte */
444 *cmd++ = ((pixel - repeating_pixel) - 1) & 0xFF;
446 /* Then start another raw pixel span */
447 raw_pixel_start = pixel;
448 raw_pixels_count_byte = cmd++;
452 if (pixel > raw_pixel_start) {
453 /* finalize last RAW span */
454 *raw_pixels_count_byte = (pixel-raw_pixel_start) & 0xFF;
457 *cmd_pixels_count_byte = (pixel - cmd_pixel_start) & 0xFF;
458 dev_addr += (pixel - cmd_pixel_start) * bpp;
461 if (cmd_buffer_end <= MIN_RLX_CMD_BYTES + cmd) {
462 /* Fill leftover bytes with no-ops */
463 if (cmd_buffer_end > cmd)
464 memset(cmd, 0xAF, cmd_buffer_end - cmd);
465 cmd = (uint8_t *) cmd_buffer_end;
468 *command_buffer_ptr = cmd;
469 *pixel_start_ptr = pixel;
470 *device_address_ptr = dev_addr;
472 return;
476 * There are 3 copies of every pixel: The front buffer that the fbdev
477 * client renders to, the actual framebuffer across the USB bus in hardware
478 * (that we can only write to, slowly, and can never read), and (optionally)
479 * our shadow copy that tracks what's been sent to that hardware buffer.
481 static int dlfb_render_hline(struct dlfb_data *dev, struct urb **urb_ptr,
482 const char *front, char **urb_buf_ptr,
483 u32 byte_offset, u32 byte_width,
484 int *ident_ptr, int *sent_ptr)
486 const u8 *line_start, *line_end, *next_pixel;
487 u32 dev_addr = dev->base16 + byte_offset;
488 struct urb *urb = *urb_ptr;
489 u8 *cmd = *urb_buf_ptr;
490 u8 *cmd_end = (u8 *) urb->transfer_buffer + urb->transfer_buffer_length;
492 line_start = (u8 *) (front + byte_offset);
493 next_pixel = line_start;
494 line_end = next_pixel + byte_width;
496 if (dev->backing_buffer) {
497 int offset;
498 const u8 *back_start = (u8 *) (dev->backing_buffer
499 + byte_offset);
501 *ident_ptr += dlfb_trim_hline(back_start, &next_pixel,
502 &byte_width);
504 offset = next_pixel - line_start;
505 line_end = next_pixel + byte_width;
506 dev_addr += offset;
507 back_start += offset;
508 line_start += offset;
510 memcpy((char *)back_start, (char *) line_start,
511 byte_width);
514 while (next_pixel < line_end) {
516 dlfb_compress_hline((const uint16_t **) &next_pixel,
517 (const uint16_t *) line_end, &dev_addr,
518 (u8 **) &cmd, (u8 *) cmd_end);
520 if (cmd >= cmd_end) {
521 int len = cmd - (u8 *) urb->transfer_buffer;
522 if (dlfb_submit_urb(dev, urb, len))
523 return 1; /* lost pixels is set */
524 *sent_ptr += len;
525 urb = dlfb_get_urb(dev);
526 if (!urb)
527 return 1; /* lost_pixels is set */
528 *urb_ptr = urb;
529 cmd = urb->transfer_buffer;
530 cmd_end = &cmd[urb->transfer_buffer_length];
534 *urb_buf_ptr = cmd;
536 return 0;
539 int dlfb_handle_damage(struct dlfb_data *dev, int x, int y,
540 int width, int height, char *data)
542 int i, ret;
543 char *cmd;
544 cycles_t start_cycles, end_cycles;
545 int bytes_sent = 0;
546 int bytes_identical = 0;
547 struct urb *urb;
548 int aligned_x;
550 start_cycles = get_cycles();
552 aligned_x = DL_ALIGN_DOWN(x, sizeof(unsigned long));
553 width = DL_ALIGN_UP(width + (x-aligned_x), sizeof(unsigned long));
554 x = aligned_x;
556 if ((width <= 0) ||
557 (x + width > dev->info->var.xres) ||
558 (y + height > dev->info->var.yres))
559 return -EINVAL;
561 if (!atomic_read(&dev->usb_active))
562 return 0;
564 urb = dlfb_get_urb(dev);
565 if (!urb)
566 return 0;
567 cmd = urb->transfer_buffer;
569 for (i = y; i < y + height ; i++) {
570 const int line_offset = dev->info->fix.line_length * i;
571 const int byte_offset = line_offset + (x * BPP);
573 if (dlfb_render_hline(dev, &urb,
574 (char *) dev->info->fix.smem_start,
575 &cmd, byte_offset, width * BPP,
576 &bytes_identical, &bytes_sent))
577 goto error;
580 if (cmd > (char *) urb->transfer_buffer) {
581 /* Send partial buffer remaining before exiting */
582 int len = cmd - (char *) urb->transfer_buffer;
583 ret = dlfb_submit_urb(dev, urb, len);
584 bytes_sent += len;
585 } else
586 dlfb_urb_completion(urb);
588 error:
589 atomic_add(bytes_sent, &dev->bytes_sent);
590 atomic_add(bytes_identical, &dev->bytes_identical);
591 atomic_add(width*height*2, &dev->bytes_rendered);
592 end_cycles = get_cycles();
593 atomic_add(((unsigned int) ((end_cycles - start_cycles)
594 >> 10)), /* Kcycles */
595 &dev->cpu_kcycles_used);
597 return 0;
601 * Path triggered by usermode clients who write to filesystem
602 * e.g. cat filename > /dev/fb1
603 * Not used by X Windows or text-mode console. But useful for testing.
604 * Slow because of extra copy and we must assume all pixels dirty.
606 static ssize_t dlfb_ops_write(struct fb_info *info, const char __user *buf,
607 size_t count, loff_t *ppos)
609 ssize_t result;
610 struct dlfb_data *dev = info->par;
611 u32 offset = (u32) *ppos;
613 result = fb_sys_write(info, buf, count, ppos);
615 if (result > 0) {
616 int start = max((int)(offset / info->fix.line_length) - 1, 0);
617 int lines = min((u32)((result / info->fix.line_length) + 1),
618 (u32)info->var.yres);
620 dlfb_handle_damage(dev, 0, start, info->var.xres,
621 lines, info->screen_base);
624 return result;
627 /* hardware has native COPY command (see libdlo), but not worth it for fbcon */
628 static void dlfb_ops_copyarea(struct fb_info *info,
629 const struct fb_copyarea *area)
632 struct dlfb_data *dev = info->par;
634 sys_copyarea(info, area);
636 dlfb_handle_damage(dev, area->dx, area->dy,
637 area->width, area->height, info->screen_base);
640 static void dlfb_ops_imageblit(struct fb_info *info,
641 const struct fb_image *image)
643 struct dlfb_data *dev = info->par;
645 sys_imageblit(info, image);
647 dlfb_handle_damage(dev, image->dx, image->dy,
648 image->width, image->height, info->screen_base);
651 static void dlfb_ops_fillrect(struct fb_info *info,
652 const struct fb_fillrect *rect)
654 struct dlfb_data *dev = info->par;
656 sys_fillrect(info, rect);
658 dlfb_handle_damage(dev, rect->dx, rect->dy, rect->width,
659 rect->height, info->screen_base);
663 * NOTE: fb_defio.c is holding info->fbdefio.mutex
664 * Touching ANY framebuffer memory that triggers a page fault
665 * in fb_defio will cause a deadlock, when it also tries to
666 * grab the same mutex.
668 static void dlfb_dpy_deferred_io(struct fb_info *info,
669 struct list_head *pagelist)
671 struct page *cur;
672 struct fb_deferred_io *fbdefio = info->fbdefio;
673 struct dlfb_data *dev = info->par;
674 struct urb *urb;
675 char *cmd;
676 cycles_t start_cycles, end_cycles;
677 int bytes_sent = 0;
678 int bytes_identical = 0;
679 int bytes_rendered = 0;
681 if (!fb_defio)
682 return;
684 if (!atomic_read(&dev->usb_active))
685 return;
687 start_cycles = get_cycles();
689 urb = dlfb_get_urb(dev);
690 if (!urb)
691 return;
693 cmd = urb->transfer_buffer;
695 /* walk the written page list and render each to device */
696 list_for_each_entry(cur, &fbdefio->pagelist, lru) {
698 if (dlfb_render_hline(dev, &urb, (char *) info->fix.smem_start,
699 &cmd, cur->index << PAGE_SHIFT,
700 PAGE_SIZE, &bytes_identical, &bytes_sent))
701 goto error;
702 bytes_rendered += PAGE_SIZE;
705 if (cmd > (char *) urb->transfer_buffer) {
706 /* Send partial buffer remaining before exiting */
707 int len = cmd - (char *) urb->transfer_buffer;
708 dlfb_submit_urb(dev, urb, len);
709 bytes_sent += len;
710 } else
711 dlfb_urb_completion(urb);
713 error:
714 atomic_add(bytes_sent, &dev->bytes_sent);
715 atomic_add(bytes_identical, &dev->bytes_identical);
716 atomic_add(bytes_rendered, &dev->bytes_rendered);
717 end_cycles = get_cycles();
718 atomic_add(((unsigned int) ((end_cycles - start_cycles)
719 >> 10)), /* Kcycles */
720 &dev->cpu_kcycles_used);
723 static int dlfb_get_edid(struct dlfb_data *dev, char *edid, int len)
725 int i;
726 int ret;
727 char *rbuf;
729 rbuf = kmalloc(2, GFP_KERNEL);
730 if (!rbuf)
731 return 0;
733 for (i = 0; i < len; i++) {
734 ret = usb_control_msg(dev->udev,
735 usb_rcvctrlpipe(dev->udev, 0), (0x02),
736 (0x80 | (0x02 << 5)), i << 8, 0xA1, rbuf, 2,
737 HZ);
738 if (ret < 1) {
739 pr_err("Read EDID byte %d failed err %x\n", i, ret);
740 i--;
741 break;
743 edid[i] = rbuf[1];
746 kfree(rbuf);
748 return i;
751 static int dlfb_ops_ioctl(struct fb_info *info, unsigned int cmd,
752 unsigned long arg)
755 struct dlfb_data *dev = info->par;
756 struct dloarea *area = NULL;
758 if (!atomic_read(&dev->usb_active))
759 return 0;
761 /* TODO: Update X server to get this from sysfs instead */
762 if (cmd == DLFB_IOCTL_RETURN_EDID) {
763 char *edid = (char *)arg;
764 if (copy_to_user(edid, dev->edid, dev->edid_size))
765 return -EFAULT;
766 return 0;
769 /* TODO: Help propose a standard fb.h ioctl to report mmap damage */
770 if (cmd == DLFB_IOCTL_REPORT_DAMAGE) {
773 * If we have a damage-aware client, turn fb_defio "off"
774 * To avoid perf imact of unnecessary page fault handling.
775 * Done by resetting the delay for this fb_info to a very
776 * long period. Pages will become writable and stay that way.
777 * Reset to normal value when all clients have closed this fb.
779 if (info->fbdefio)
780 info->fbdefio->delay = DL_DEFIO_WRITE_DISABLE;
782 area = (struct dloarea *)arg;
784 if (area->x < 0)
785 area->x = 0;
787 if (area->x > info->var.xres)
788 area->x = info->var.xres;
790 if (area->y < 0)
791 area->y = 0;
793 if (area->y > info->var.yres)
794 area->y = info->var.yres;
796 dlfb_handle_damage(dev, area->x, area->y, area->w, area->h,
797 info->screen_base);
800 return 0;
803 /* taken from vesafb */
804 static int
805 dlfb_ops_setcolreg(unsigned regno, unsigned red, unsigned green,
806 unsigned blue, unsigned transp, struct fb_info *info)
808 int err = 0;
810 if (regno >= info->cmap.len)
811 return 1;
813 if (regno < 16) {
814 if (info->var.red.offset == 10) {
815 /* 1:5:5:5 */
816 ((u32 *) (info->pseudo_palette))[regno] =
817 ((red & 0xf800) >> 1) |
818 ((green & 0xf800) >> 6) | ((blue & 0xf800) >> 11);
819 } else {
820 /* 0:5:6:5 */
821 ((u32 *) (info->pseudo_palette))[regno] =
822 ((red & 0xf800)) |
823 ((green & 0xfc00) >> 5) | ((blue & 0xf800) >> 11);
827 return err;
831 * It's common for several clients to have framebuffer open simultaneously.
832 * e.g. both fbcon and X. Makes things interesting.
833 * Assumes caller is holding info->lock (for open and release at least)
835 static int dlfb_ops_open(struct fb_info *info, int user)
837 struct dlfb_data *dev = info->par;
840 * fbcon aggressively connects to first framebuffer it finds,
841 * preventing other clients (X) from working properly. Usually
842 * not what the user wants. Fail by default with option to enable.
844 if ((user == 0) & (!console))
845 return -EBUSY;
847 /* If the USB device is gone, we don't accept new opens */
848 if (dev->virtualized)
849 return -ENODEV;
851 dev->fb_count++;
853 kref_get(&dev->kref);
855 if (fb_defio && (info->fbdefio == NULL)) {
856 /* enable defio at last moment if not disabled by client */
858 struct fb_deferred_io *fbdefio;
860 fbdefio = kmalloc(sizeof(struct fb_deferred_io), GFP_KERNEL);
862 if (fbdefio) {
863 fbdefio->delay = DL_DEFIO_WRITE_DELAY;
864 fbdefio->deferred_io = dlfb_dpy_deferred_io;
867 info->fbdefio = fbdefio;
868 fb_deferred_io_init(info);
871 pr_notice("open /dev/fb%d user=%d fb_info=%p count=%d\n",
872 info->node, user, info, dev->fb_count);
874 return 0;
878 * Called when all client interfaces to start transactions have been disabled,
879 * and all references to our device instance (dlfb_data) are released.
880 * Every transaction must have a reference, so we know are fully spun down
882 static void dlfb_free(struct kref *kref)
884 struct dlfb_data *dev = container_of(kref, struct dlfb_data, kref);
886 /* this function will wait for all in-flight urbs to complete */
887 if (dev->urbs.count > 0)
888 dlfb_free_urb_list(dev);
890 if (dev->backing_buffer)
891 vfree(dev->backing_buffer);
893 kfree(dev->edid);
895 pr_warn("freeing dlfb_data %p\n", dev);
897 kfree(dev);
900 static void dlfb_release_urb_work(struct work_struct *work)
902 struct urb_node *unode = container_of(work, struct urb_node,
903 release_urb_work.work);
905 up(&unode->dev->urbs.limit_sem);
908 static void dlfb_free_framebuffer_work(struct work_struct *work)
910 struct dlfb_data *dev = container_of(work, struct dlfb_data,
911 free_framebuffer_work.work);
912 struct fb_info *info = dev->info;
913 int node = info->node;
915 unregister_framebuffer(info);
917 if (info->cmap.len != 0)
918 fb_dealloc_cmap(&info->cmap);
919 if (info->monspecs.modedb)
920 fb_destroy_modedb(info->monspecs.modedb);
921 if (info->screen_base)
922 vfree(info->screen_base);
924 fb_destroy_modelist(&info->modelist);
926 dev->info = 0;
928 /* Assume info structure is freed after this point */
929 framebuffer_release(info);
931 pr_warn("fb_info for /dev/fb%d has been freed\n", node);
933 /* ref taken in probe() as part of registering framebfufer */
934 kref_put(&dev->kref, dlfb_free);
938 * Assumes caller is holding info->lock mutex (for open and release at least)
940 static int dlfb_ops_release(struct fb_info *info, int user)
942 struct dlfb_data *dev = info->par;
944 dev->fb_count--;
946 /* We can't free fb_info here - fbmem will touch it when we return */
947 if (dev->virtualized && (dev->fb_count == 0))
948 schedule_delayed_work(&dev->free_framebuffer_work, HZ);
950 if ((dev->fb_count == 0) && (info->fbdefio)) {
951 fb_deferred_io_cleanup(info);
952 kfree(info->fbdefio);
953 info->fbdefio = NULL;
954 info->fbops->fb_mmap = dlfb_ops_mmap;
957 pr_warn("released /dev/fb%d user=%d count=%d\n",
958 info->node, user, dev->fb_count);
960 kref_put(&dev->kref, dlfb_free);
962 return 0;
966 * Check whether a video mode is supported by the DisplayLink chip
967 * We start from monitor's modes, so don't need to filter that here
969 static int dlfb_is_valid_mode(struct fb_videomode *mode,
970 struct fb_info *info)
972 struct dlfb_data *dev = info->par;
974 if (mode->xres * mode->yres > dev->sku_pixel_limit) {
975 pr_warn("%dx%d beyond chip capabilities\n",
976 mode->xres, mode->yres);
977 return 0;
980 pr_info("%dx%d valid mode\n", mode->xres, mode->yres);
982 return 1;
985 static void dlfb_var_color_format(struct fb_var_screeninfo *var)
987 const struct fb_bitfield red = { 11, 5, 0 };
988 const struct fb_bitfield green = { 5, 6, 0 };
989 const struct fb_bitfield blue = { 0, 5, 0 };
991 var->bits_per_pixel = 16;
992 var->red = red;
993 var->green = green;
994 var->blue = blue;
997 static int dlfb_ops_check_var(struct fb_var_screeninfo *var,
998 struct fb_info *info)
1000 struct fb_videomode mode;
1002 /* TODO: support dynamically changing framebuffer size */
1003 if ((var->xres * var->yres * 2) > info->fix.smem_len)
1004 return -EINVAL;
1006 /* set device-specific elements of var unrelated to mode */
1007 dlfb_var_color_format(var);
1009 fb_var_to_videomode(&mode, var);
1011 if (!dlfb_is_valid_mode(&mode, info))
1012 return -EINVAL;
1014 return 0;
1017 static int dlfb_ops_set_par(struct fb_info *info)
1019 struct dlfb_data *dev = info->par;
1020 int result;
1021 u16 *pix_framebuffer;
1022 int i;
1024 pr_notice("set_par mode %dx%d\n", info->var.xres, info->var.yres);
1026 result = dlfb_set_video_mode(dev, &info->var);
1028 if ((result == 0) && (dev->fb_count == 0)) {
1030 /* paint greenscreen */
1032 pix_framebuffer = (u16 *) info->screen_base;
1033 for (i = 0; i < info->fix.smem_len / 2; i++)
1034 pix_framebuffer[i] = 0x37e6;
1036 dlfb_handle_damage(dev, 0, 0, info->var.xres, info->var.yres,
1037 info->screen_base);
1040 return result;
1044 * In order to come back from full DPMS off, we need to set the mode again
1046 static int dlfb_ops_blank(int blank_mode, struct fb_info *info)
1048 struct dlfb_data *dev = info->par;
1050 if (blank_mode != FB_BLANK_UNBLANK) {
1051 char *bufptr;
1052 struct urb *urb;
1054 urb = dlfb_get_urb(dev);
1055 if (!urb)
1056 return 0;
1058 bufptr = (char *) urb->transfer_buffer;
1059 bufptr = dlfb_vidreg_lock(bufptr);
1060 bufptr = dlfb_enable_hvsync(bufptr, false);
1061 bufptr = dlfb_vidreg_unlock(bufptr);
1063 dlfb_submit_urb(dev, urb, bufptr -
1064 (char *) urb->transfer_buffer);
1065 } else {
1066 dlfb_set_video_mode(dev, &info->var);
1069 return 0;
1072 static struct fb_ops dlfb_ops = {
1073 .owner = THIS_MODULE,
1074 .fb_read = fb_sys_read,
1075 .fb_write = dlfb_ops_write,
1076 .fb_setcolreg = dlfb_ops_setcolreg,
1077 .fb_fillrect = dlfb_ops_fillrect,
1078 .fb_copyarea = dlfb_ops_copyarea,
1079 .fb_imageblit = dlfb_ops_imageblit,
1080 .fb_mmap = dlfb_ops_mmap,
1081 .fb_ioctl = dlfb_ops_ioctl,
1082 .fb_open = dlfb_ops_open,
1083 .fb_release = dlfb_ops_release,
1084 .fb_blank = dlfb_ops_blank,
1085 .fb_check_var = dlfb_ops_check_var,
1086 .fb_set_par = dlfb_ops_set_par,
1091 * Assumes &info->lock held by caller
1092 * Assumes no active clients have framebuffer open
1094 static int dlfb_realloc_framebuffer(struct dlfb_data *dev, struct fb_info *info)
1096 int retval = -ENOMEM;
1097 int old_len = info->fix.smem_len;
1098 int new_len;
1099 unsigned char *old_fb = info->screen_base;
1100 unsigned char *new_fb;
1101 unsigned char *new_back;
1103 pr_warn("Reallocating framebuffer. Addresses will change!\n");
1105 new_len = info->fix.line_length * info->var.yres;
1107 if (PAGE_ALIGN(new_len) > old_len) {
1109 * Alloc system memory for virtual framebuffer
1111 new_fb = vmalloc(new_len);
1112 if (!new_fb) {
1113 pr_err("Virtual framebuffer alloc failed\n");
1114 goto error;
1117 if (info->screen_base) {
1118 memcpy(new_fb, old_fb, old_len);
1119 vfree(info->screen_base);
1122 info->screen_base = new_fb;
1123 info->fix.smem_len = PAGE_ALIGN(new_len);
1124 info->fix.smem_start = (unsigned long) new_fb;
1125 info->flags = udlfb_info_flags;
1128 * Second framebuffer copy to mirror the framebuffer state
1129 * on the physical USB device. We can function without this.
1130 * But with imperfect damage info we may send pixels over USB
1131 * that were, in fact, unchanged - wasting limited USB bandwidth
1133 new_back = vzalloc(new_len);
1134 if (!new_back)
1135 pr_info("No shadow/backing buffer allocated\n");
1136 else {
1137 if (dev->backing_buffer)
1138 vfree(dev->backing_buffer);
1139 dev->backing_buffer = new_back;
1143 retval = 0;
1145 error:
1146 return retval;
1150 * 1) Get EDID from hw, or use sw default
1151 * 2) Parse into various fb_info structs
1152 * 3) Allocate virtual framebuffer memory to back highest res mode
1154 * Parses EDID into three places used by various parts of fbdev:
1155 * fb_var_screeninfo contains the timing of the monitor's preferred mode
1156 * fb_info.monspecs is full parsed EDID info, including monspecs.modedb
1157 * fb_info.modelist is a linked list of all monitor & VESA modes which work
1159 * If EDID is not readable/valid, then modelist is all VESA modes,
1160 * monspecs is NULL, and fb_var_screeninfo is set to safe VESA mode
1161 * Returns 0 if successful
1163 static int dlfb_setup_modes(struct dlfb_data *dev,
1164 struct fb_info *info,
1165 char *default_edid, size_t default_edid_size)
1167 int i;
1168 const struct fb_videomode *default_vmode = NULL;
1169 int result = 0;
1170 char *edid;
1171 int tries = 3;
1173 if (info->dev) /* only use mutex if info has been registered */
1174 mutex_lock(&info->lock);
1176 edid = kmalloc(EDID_LENGTH, GFP_KERNEL);
1177 if (!edid) {
1178 result = -ENOMEM;
1179 goto error;
1182 fb_destroy_modelist(&info->modelist);
1183 memset(&info->monspecs, 0, sizeof(info->monspecs));
1186 * Try to (re)read EDID from hardware first
1187 * EDID data may return, but not parse as valid
1188 * Try again a few times, in case of e.g. analog cable noise
1190 while (tries--) {
1192 i = dlfb_get_edid(dev, edid, EDID_LENGTH);
1194 if (i >= EDID_LENGTH)
1195 fb_edid_to_monspecs(edid, &info->monspecs);
1197 if (info->monspecs.modedb_len > 0) {
1198 dev->edid = edid;
1199 dev->edid_size = i;
1200 break;
1204 /* If that fails, use a previously returned EDID if available */
1205 if (info->monspecs.modedb_len == 0) {
1207 pr_err("Unable to get valid EDID from device/display\n");
1209 if (dev->edid) {
1210 fb_edid_to_monspecs(dev->edid, &info->monspecs);
1211 if (info->monspecs.modedb_len > 0)
1212 pr_err("Using previously queried EDID\n");
1216 /* If that fails, use the default EDID we were handed */
1217 if (info->monspecs.modedb_len == 0) {
1218 if (default_edid_size >= EDID_LENGTH) {
1219 fb_edid_to_monspecs(default_edid, &info->monspecs);
1220 if (info->monspecs.modedb_len > 0) {
1221 memcpy(edid, default_edid, default_edid_size);
1222 dev->edid = edid;
1223 dev->edid_size = default_edid_size;
1224 pr_err("Using default/backup EDID\n");
1229 /* If we've got modes, let's pick a best default mode */
1230 if (info->monspecs.modedb_len > 0) {
1232 for (i = 0; i < info->monspecs.modedb_len; i++) {
1233 if (dlfb_is_valid_mode(&info->monspecs.modedb[i], info))
1234 fb_add_videomode(&info->monspecs.modedb[i],
1235 &info->modelist);
1236 else {
1237 if (i == 0)
1238 /* if we've removed top/best mode */
1239 info->monspecs.misc
1240 &= ~FB_MISC_1ST_DETAIL;
1244 default_vmode = fb_find_best_display(&info->monspecs,
1245 &info->modelist);
1248 /* If everything else has failed, fall back to safe default mode */
1249 if (default_vmode == NULL) {
1251 struct fb_videomode fb_vmode = {0};
1254 * Add the standard VESA modes to our modelist
1255 * Since we don't have EDID, there may be modes that
1256 * overspec monitor and/or are incorrect aspect ratio, etc.
1257 * But at least the user has a chance to choose
1259 for (i = 0; i < VESA_MODEDB_SIZE; i++) {
1260 if (dlfb_is_valid_mode((struct fb_videomode *)
1261 &vesa_modes[i], info))
1262 fb_add_videomode(&vesa_modes[i],
1263 &info->modelist);
1267 * default to resolution safe for projectors
1268 * (since they are most common case without EDID)
1270 fb_vmode.xres = 800;
1271 fb_vmode.yres = 600;
1272 fb_vmode.refresh = 60;
1273 default_vmode = fb_find_nearest_mode(&fb_vmode,
1274 &info->modelist);
1277 /* If we have good mode and no active clients*/
1278 if ((default_vmode != NULL) && (dev->fb_count == 0)) {
1280 fb_videomode_to_var(&info->var, default_vmode);
1281 dlfb_var_color_format(&info->var);
1284 * with mode size info, we can now alloc our framebuffer.
1286 memcpy(&info->fix, &dlfb_fix, sizeof(dlfb_fix));
1287 info->fix.line_length = info->var.xres *
1288 (info->var.bits_per_pixel / 8);
1290 result = dlfb_realloc_framebuffer(dev, info);
1292 } else
1293 result = -EINVAL;
1295 error:
1296 if (edid && (dev->edid != edid))
1297 kfree(edid);
1299 if (info->dev)
1300 mutex_unlock(&info->lock);
1302 return result;
1305 static ssize_t metrics_bytes_rendered_show(struct device *fbdev,
1306 struct device_attribute *a, char *buf) {
1307 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1308 struct dlfb_data *dev = fb_info->par;
1309 return snprintf(buf, PAGE_SIZE, "%u\n",
1310 atomic_read(&dev->bytes_rendered));
1313 static ssize_t metrics_bytes_identical_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_identical));
1321 static ssize_t metrics_bytes_sent_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_sent));
1329 static ssize_t metrics_cpu_kcycles_used_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->cpu_kcycles_used));
1337 static ssize_t edid_show(
1338 struct file *filp,
1339 struct kobject *kobj, struct bin_attribute *a,
1340 char *buf, loff_t off, size_t count) {
1341 struct device *fbdev = container_of(kobj, struct device, kobj);
1342 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1343 struct dlfb_data *dev = fb_info->par;
1345 if (dev->edid == NULL)
1346 return 0;
1348 if ((off >= dev->edid_size) || (count > dev->edid_size))
1349 return 0;
1351 if (off + count > dev->edid_size)
1352 count = dev->edid_size - off;
1354 pr_info("sysfs edid copy %p to %p, %d bytes\n",
1355 dev->edid, buf, (int) count);
1357 memcpy(buf, dev->edid, count);
1359 return count;
1362 static ssize_t edid_store(
1363 struct file *filp,
1364 struct kobject *kobj, struct bin_attribute *a,
1365 char *src, loff_t src_off, size_t src_size) {
1366 struct device *fbdev = container_of(kobj, struct device, kobj);
1367 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1368 struct dlfb_data *dev = fb_info->par;
1370 /* We only support write of entire EDID at once, no offset*/
1371 if ((src_size != EDID_LENGTH) || (src_off != 0))
1372 return 0;
1374 dlfb_setup_modes(dev, fb_info, src, src_size);
1376 if (dev->edid && (memcmp(src, dev->edid, src_size) == 0)) {
1377 pr_info("sysfs written EDID is new default\n");
1378 dlfb_ops_set_par(fb_info);
1379 return src_size;
1380 } else
1381 return 0;
1384 static ssize_t metrics_reset_store(struct device *fbdev,
1385 struct device_attribute *attr,
1386 const char *buf, size_t count)
1388 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1389 struct dlfb_data *dev = fb_info->par;
1391 atomic_set(&dev->bytes_rendered, 0);
1392 atomic_set(&dev->bytes_identical, 0);
1393 atomic_set(&dev->bytes_sent, 0);
1394 atomic_set(&dev->cpu_kcycles_used, 0);
1396 return count;
1399 static struct bin_attribute edid_attr = {
1400 .attr.name = "edid",
1401 .attr.mode = 0666,
1402 .size = EDID_LENGTH,
1403 .read = edid_show,
1404 .write = edid_store
1407 static struct device_attribute fb_device_attrs[] = {
1408 __ATTR_RO(metrics_bytes_rendered),
1409 __ATTR_RO(metrics_bytes_identical),
1410 __ATTR_RO(metrics_bytes_sent),
1411 __ATTR_RO(metrics_cpu_kcycles_used),
1412 __ATTR(metrics_reset, S_IWUSR, NULL, metrics_reset_store),
1416 * This is necessary before we can communicate with the display controller.
1418 static int dlfb_select_std_channel(struct dlfb_data *dev)
1420 int ret;
1421 u8 set_def_chn[] = { 0x57, 0xCD, 0xDC, 0xA7,
1422 0x1C, 0x88, 0x5E, 0x15,
1423 0x60, 0xFE, 0xC6, 0x97,
1424 0x16, 0x3D, 0x47, 0xF2 };
1426 ret = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
1427 NR_USB_REQUEST_CHANNEL,
1428 (USB_DIR_OUT | USB_TYPE_VENDOR), 0, 0,
1429 set_def_chn, sizeof(set_def_chn), USB_CTRL_SET_TIMEOUT);
1430 return ret;
1433 static int dlfb_parse_vendor_descriptor(struct dlfb_data *dev,
1434 struct usb_device *usbdev)
1436 char *desc;
1437 char *buf;
1438 char *desc_end;
1440 u8 total_len = 0;
1442 buf = kzalloc(MAX_VENDOR_DESCRIPTOR_SIZE, GFP_KERNEL);
1443 if (!buf)
1444 return false;
1445 desc = buf;
1447 total_len = usb_get_descriptor(usbdev, 0x5f, /* vendor specific */
1448 0, desc, MAX_VENDOR_DESCRIPTOR_SIZE);
1449 if (total_len > 5) {
1450 pr_info("vendor descriptor length:%x data:%02x %02x %02x %02x" \
1451 "%02x %02x %02x %02x %02x %02x %02x\n",
1452 total_len, desc[0],
1453 desc[1], desc[2], desc[3], desc[4], desc[5], desc[6],
1454 desc[7], desc[8], desc[9], desc[10]);
1456 if ((desc[0] != total_len) || /* descriptor length */
1457 (desc[1] != 0x5f) || /* vendor descriptor type */
1458 (desc[2] != 0x01) || /* version (2 bytes) */
1459 (desc[3] != 0x00) ||
1460 (desc[4] != total_len - 2)) /* length after type */
1461 goto unrecognized;
1463 desc_end = desc + total_len;
1464 desc += 5; /* the fixed header we've already parsed */
1466 while (desc < desc_end) {
1467 u8 length;
1468 u16 key;
1470 key = *((u16 *) desc);
1471 desc += sizeof(u16);
1472 length = *desc;
1473 desc++;
1475 switch (key) {
1476 case 0x0200: { /* max_area */
1477 u32 max_area;
1478 max_area = le32_to_cpu(*((u32 *)desc));
1479 pr_warn("DL chip limited to %d pixel modes\n",
1480 max_area);
1481 dev->sku_pixel_limit = max_area;
1482 break;
1484 default:
1485 break;
1487 desc += length;
1491 goto success;
1493 unrecognized:
1494 /* allow udlfb to load for now even if firmware unrecognized */
1495 pr_err("Unrecognized vendor firmware descriptor\n");
1497 success:
1498 kfree(buf);
1499 return true;
1501 static int dlfb_usb_probe(struct usb_interface *interface,
1502 const struct usb_device_id *id)
1504 struct usb_device *usbdev;
1505 struct dlfb_data *dev = 0;
1506 struct fb_info *info = 0;
1507 int retval = -ENOMEM;
1508 int i;
1510 /* usb initialization */
1512 usbdev = interface_to_usbdev(interface);
1514 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1515 if (dev == NULL) {
1516 err("dlfb_usb_probe: failed alloc of dev struct\n");
1517 goto error;
1520 /* we need to wait for both usb and fbdev to spin down on disconnect */
1521 kref_init(&dev->kref); /* matching kref_put in usb .disconnect fn */
1522 kref_get(&dev->kref); /* matching kref_put in free_framebuffer_work */
1524 dev->udev = usbdev;
1525 dev->gdev = &usbdev->dev; /* our generic struct device * */
1526 usb_set_intfdata(interface, dev);
1528 pr_info("%s %s - serial #%s\n",
1529 usbdev->manufacturer, usbdev->product, usbdev->serial);
1530 pr_info("vid_%04x&pid_%04x&rev_%04x driver's dlfb_data struct at %p\n",
1531 usbdev->descriptor.idVendor, usbdev->descriptor.idProduct,
1532 usbdev->descriptor.bcdDevice, dev);
1533 pr_info("console enable=%d\n", console);
1534 pr_info("fb_defio enable=%d\n", fb_defio);
1536 dev->sku_pixel_limit = 2048 * 1152; /* default to maximum */
1538 if (!dlfb_parse_vendor_descriptor(dev, usbdev)) {
1539 pr_err("firmware not recognized. Assume incompatible device\n");
1540 goto error;
1543 if (!dlfb_alloc_urb_list(dev, WRITES_IN_FLIGHT, MAX_TRANSFER)) {
1544 retval = -ENOMEM;
1545 pr_err("dlfb_alloc_urb_list failed\n");
1546 goto error;
1549 /* We don't register a new USB class. Our client interface is fbdev */
1551 /* allocates framebuffer driver structure, not framebuffer memory */
1552 info = framebuffer_alloc(0, &usbdev->dev);
1553 if (!info) {
1554 retval = -ENOMEM;
1555 pr_err("framebuffer_alloc failed\n");
1556 goto error;
1559 dev->info = info;
1560 info->par = dev;
1561 info->pseudo_palette = dev->pseudo_palette;
1562 info->fbops = &dlfb_ops;
1564 retval = fb_alloc_cmap(&info->cmap, 256, 0);
1565 if (retval < 0) {
1566 pr_err("fb_alloc_cmap failed %x\n", retval);
1567 goto error;
1570 INIT_DELAYED_WORK(&dev->free_framebuffer_work,
1571 dlfb_free_framebuffer_work);
1573 INIT_LIST_HEAD(&info->modelist);
1575 retval = dlfb_setup_modes(dev, info, NULL, 0);
1576 if (retval != 0) {
1577 pr_err("unable to find common mode for display and adapter\n");
1578 goto error;
1581 /* ready to begin using device */
1583 atomic_set(&dev->usb_active, 1);
1584 dlfb_select_std_channel(dev);
1586 dlfb_ops_check_var(&info->var, info);
1587 dlfb_ops_set_par(info);
1589 retval = register_framebuffer(info);
1590 if (retval < 0) {
1591 pr_err("register_framebuffer failed %d\n", retval);
1592 goto error;
1595 for (i = 0; i < ARRAY_SIZE(fb_device_attrs); i++) {
1596 retval = device_create_file(info->dev, &fb_device_attrs[i]);
1597 if (retval) {
1598 pr_err("device_create_file failed %d\n", retval);
1599 goto err_del_attrs;
1603 retval = device_create_bin_file(info->dev, &edid_attr);
1604 if (retval) {
1605 pr_err("device_create_bin_file failed %d\n", retval);
1606 goto err_del_attrs;
1609 pr_info("DisplayLink USB device /dev/fb%d attached. %dx%d resolution."
1610 " Using %dK framebuffer memory\n", info->node,
1611 info->var.xres, info->var.yres,
1612 ((dev->backing_buffer) ?
1613 info->fix.smem_len * 2 : info->fix.smem_len) >> 10);
1614 return 0;
1616 err_del_attrs:
1617 for (i -= 1; i >= 0; i--)
1618 device_remove_file(info->dev, &fb_device_attrs[i]);
1620 error:
1621 if (dev) {
1623 if (info) {
1624 if (info->cmap.len != 0)
1625 fb_dealloc_cmap(&info->cmap);
1626 if (info->monspecs.modedb)
1627 fb_destroy_modedb(info->monspecs.modedb);
1628 if (info->screen_base)
1629 vfree(info->screen_base);
1631 fb_destroy_modelist(&info->modelist);
1633 framebuffer_release(info);
1636 if (dev->backing_buffer)
1637 vfree(dev->backing_buffer);
1639 kref_put(&dev->kref, dlfb_free); /* ref for framebuffer */
1640 kref_put(&dev->kref, dlfb_free); /* last ref from kref_init */
1642 /* dev has been deallocated. Do not dereference */
1645 return retval;
1648 static void dlfb_usb_disconnect(struct usb_interface *interface)
1650 struct dlfb_data *dev;
1651 struct fb_info *info;
1652 int i;
1654 dev = usb_get_intfdata(interface);
1655 info = dev->info;
1657 pr_info("USB disconnect starting\n");
1659 /* we virtualize until all fb clients release. Then we free */
1660 dev->virtualized = true;
1662 /* When non-active we'll update virtual framebuffer, but no new urbs */
1663 atomic_set(&dev->usb_active, 0);
1665 /* remove udlfb's sysfs interfaces */
1666 for (i = 0; i < ARRAY_SIZE(fb_device_attrs); i++)
1667 device_remove_file(info->dev, &fb_device_attrs[i]);
1668 device_remove_bin_file(info->dev, &edid_attr);
1670 usb_set_intfdata(interface, NULL);
1672 /* if clients still have us open, will be freed on last close */
1673 if (dev->fb_count == 0)
1674 schedule_delayed_work(&dev->free_framebuffer_work, 0);
1676 /* release reference taken by kref_init in probe() */
1677 kref_put(&dev->kref, dlfb_free);
1679 /* consider dlfb_data freed */
1681 return;
1684 static struct usb_driver dlfb_driver = {
1685 .name = "udlfb",
1686 .probe = dlfb_usb_probe,
1687 .disconnect = dlfb_usb_disconnect,
1688 .id_table = id_table,
1691 static int __init dlfb_module_init(void)
1693 int res;
1695 res = usb_register(&dlfb_driver);
1696 if (res)
1697 err("usb_register failed. Error number %d", res);
1699 return res;
1702 static void __exit dlfb_module_exit(void)
1704 usb_deregister(&dlfb_driver);
1707 module_init(dlfb_module_init);
1708 module_exit(dlfb_module_exit);
1710 static void dlfb_urb_completion(struct urb *urb)
1712 struct urb_node *unode = urb->context;
1713 struct dlfb_data *dev = unode->dev;
1714 unsigned long flags;
1716 /* sync/async unlink faults aren't errors */
1717 if (urb->status) {
1718 if (!(urb->status == -ENOENT ||
1719 urb->status == -ECONNRESET ||
1720 urb->status == -ESHUTDOWN)) {
1721 pr_err("%s - nonzero write bulk status received: %d\n",
1722 __func__, urb->status);
1723 atomic_set(&dev->lost_pixels, 1);
1727 urb->transfer_buffer_length = dev->urbs.size; /* reset to actual */
1729 spin_lock_irqsave(&dev->urbs.lock, flags);
1730 list_add_tail(&unode->entry, &dev->urbs.list);
1731 dev->urbs.available++;
1732 spin_unlock_irqrestore(&dev->urbs.lock, flags);
1735 * When using fb_defio, we deadlock if up() is called
1736 * while another is waiting. So queue to another process.
1738 if (fb_defio)
1739 schedule_delayed_work(&unode->release_urb_work, 0);
1740 else
1741 up(&dev->urbs.limit_sem);
1744 static void dlfb_free_urb_list(struct dlfb_data *dev)
1746 int count = dev->urbs.count;
1747 struct list_head *node;
1748 struct urb_node *unode;
1749 struct urb *urb;
1750 int ret;
1751 unsigned long flags;
1753 pr_notice("Waiting for completes and freeing all render urbs\n");
1755 /* keep waiting and freeing, until we've got 'em all */
1756 while (count--) {
1758 /* Getting interrupted means a leak, but ok at shutdown*/
1759 ret = down_interruptible(&dev->urbs.limit_sem);
1760 if (ret)
1761 break;
1763 spin_lock_irqsave(&dev->urbs.lock, flags);
1765 node = dev->urbs.list.next; /* have reserved one with sem */
1766 list_del_init(node);
1768 spin_unlock_irqrestore(&dev->urbs.lock, flags);
1770 unode = list_entry(node, struct urb_node, entry);
1771 urb = unode->urb;
1773 /* Free each separately allocated piece */
1774 usb_free_coherent(urb->dev, dev->urbs.size,
1775 urb->transfer_buffer, urb->transfer_dma);
1776 usb_free_urb(urb);
1777 kfree(node);
1782 static int dlfb_alloc_urb_list(struct dlfb_data *dev, int count, size_t size)
1784 int i = 0;
1785 struct urb *urb;
1786 struct urb_node *unode;
1787 char *buf;
1789 spin_lock_init(&dev->urbs.lock);
1791 dev->urbs.size = size;
1792 INIT_LIST_HEAD(&dev->urbs.list);
1794 while (i < count) {
1795 unode = kzalloc(sizeof(struct urb_node), GFP_KERNEL);
1796 if (!unode)
1797 break;
1798 unode->dev = dev;
1800 INIT_DELAYED_WORK(&unode->release_urb_work,
1801 dlfb_release_urb_work);
1803 urb = usb_alloc_urb(0, GFP_KERNEL);
1804 if (!urb) {
1805 kfree(unode);
1806 break;
1808 unode->urb = urb;
1810 buf = usb_alloc_coherent(dev->udev, MAX_TRANSFER, GFP_KERNEL,
1811 &urb->transfer_dma);
1812 if (!buf) {
1813 kfree(unode);
1814 usb_free_urb(urb);
1815 break;
1818 /* urb->transfer_buffer_length set to actual before submit */
1819 usb_fill_bulk_urb(urb, dev->udev, usb_sndbulkpipe(dev->udev, 1),
1820 buf, size, dlfb_urb_completion, unode);
1821 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1823 list_add_tail(&unode->entry, &dev->urbs.list);
1825 i++;
1828 sema_init(&dev->urbs.limit_sem, i);
1829 dev->urbs.count = i;
1830 dev->urbs.available = i;
1832 pr_notice("allocated %d %d byte urbs\n", i, (int) size);
1834 return i;
1837 static struct urb *dlfb_get_urb(struct dlfb_data *dev)
1839 int ret = 0;
1840 struct list_head *entry;
1841 struct urb_node *unode;
1842 struct urb *urb = NULL;
1843 unsigned long flags;
1845 /* Wait for an in-flight buffer to complete and get re-queued */
1846 ret = down_timeout(&dev->urbs.limit_sem, GET_URB_TIMEOUT);
1847 if (ret) {
1848 atomic_set(&dev->lost_pixels, 1);
1849 pr_warn("wait for urb interrupted: %x available: %d\n",
1850 ret, dev->urbs.available);
1851 goto error;
1854 spin_lock_irqsave(&dev->urbs.lock, flags);
1856 BUG_ON(list_empty(&dev->urbs.list)); /* reserved one with limit_sem */
1857 entry = dev->urbs.list.next;
1858 list_del_init(entry);
1859 dev->urbs.available--;
1861 spin_unlock_irqrestore(&dev->urbs.lock, flags);
1863 unode = list_entry(entry, struct urb_node, entry);
1864 urb = unode->urb;
1866 error:
1867 return urb;
1870 static int dlfb_submit_urb(struct dlfb_data *dev, struct urb *urb, size_t len)
1872 int ret;
1874 BUG_ON(len > dev->urbs.size);
1876 urb->transfer_buffer_length = len; /* set to actual payload len */
1877 ret = usb_submit_urb(urb, GFP_KERNEL);
1878 if (ret) {
1879 dlfb_urb_completion(urb); /* because no one else will */
1880 atomic_set(&dev->lost_pixels, 1);
1881 pr_err("usb_submit_urb error %x\n", ret);
1883 return ret;
1886 module_param(console, bool, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
1887 MODULE_PARM_DESC(console, "Allow fbcon to consume first framebuffer found");
1889 module_param(fb_defio, bool, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
1890 MODULE_PARM_DESC(fb_defio, "Enable fb_defio mmap support. *Experimental*");
1892 MODULE_AUTHOR("Roberto De Ioris <roberto@unbit.it>, "
1893 "Jaya Kumar <jayakumar.lkml@gmail.com>, "
1894 "Bernie Thompson <bernie@plugable.com>");
1895 MODULE_DESCRIPTION("DisplayLink kernel framebuffer driver");
1896 MODULE_LICENSE("GPL");