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>
33 static struct fb_fix_screeninfo dlfb_fix = {
35 .type = FB_TYPE_PACKED_PIXELS,
36 .visual = FB_VISUAL_TRUECOLOR,
40 .accel = FB_ACCEL_NONE,
43 static const u32 udlfb_info_flags = FBINFO_DEFAULT | FBINFO_READS_FAST |
47 FBINFO_HWACCEL_IMAGEBLIT | FBINFO_HWACCEL_FILLRECT |
48 FBINFO_HWACCEL_COPYAREA | FBINFO_MISC_ALWAYS_SETPAR;
51 * There are many DisplayLink-based products, all with unique PIDs. We are able
52 * to support all volume ones (circa 2009) with a single driver, so we match
53 * globally on VID. TODO: Probe() needs to detect when we might be running
54 * "future" chips, and bail on those, so a compatible driver can match.
56 static struct usb_device_id id_table[] = {
57 {.idVendor = 0x17e9, .match_flags = USB_DEVICE_ID_MATCH_VENDOR,},
60 MODULE_DEVICE_TABLE(usb, id_table);
62 #ifndef CONFIG_FB_DEFERRED_IO
63 #warning Please set CONFIG_FB_DEFFERRED_IO option to support generic fbdev apps
66 #ifndef CONFIG_FB_SYS_IMAGEBLIT
67 #ifndef CONFIG_FB_SYS_IMAGEBLIT_MODULE
68 #warning Please set CONFIG_FB_SYS_IMAGEBLIT option to support fb console
72 #ifndef CONFIG_FB_MODE_HELPERS
73 #warning CONFIG_FB_MODE_HELPERS required. Expect build break
76 /* dlfb keeps a list of urbs for efficient bulk transfers */
77 static void dlfb_urb_completion(struct urb *urb);
78 static struct urb *dlfb_get_urb(struct dlfb_data *dev);
79 static int dlfb_submit_urb(struct dlfb_data *dev, struct urb * urb, size_t len);
80 static int dlfb_alloc_urb_list(struct dlfb_data *dev, int count, size_t size);
81 static void dlfb_free_urb_list(struct dlfb_data *dev);
83 /* other symbols with dependents */
84 #ifdef CONFIG_FB_DEFERRED_IO
85 static struct fb_deferred_io dlfb_defio;
89 * All DisplayLink bulk operations start with 0xAF, followed by specific code
90 * All operations are written to buffers which then later get sent to device
92 static char *dlfb_set_register(char *buf, u8 reg, u8 val)
101 static char *dlfb_vidreg_lock(char *buf)
103 return dlfb_set_register(buf, 0xFF, 0x00);
106 static char *dlfb_vidreg_unlock(char *buf)
108 return dlfb_set_register(buf, 0xFF, 0xFF);
112 * On/Off for driving the DisplayLink framebuffer to the display
113 * 0x00 H and V sync on
114 * 0x01 H and V sync off (screen blank but powered)
115 * 0x07 DPMS powerdown (requires modeset to come back)
117 static char *dlfb_enable_hvsync(char *buf, bool enable)
120 return dlfb_set_register(buf, 0x1F, 0x00);
122 return dlfb_set_register(buf, 0x1F, 0x07);
125 static char *dlfb_set_color_depth(char *buf, u8 selection)
127 return dlfb_set_register(buf, 0x00, selection);
130 static char *dlfb_set_base16bpp(char *wrptr, u32 base)
132 /* the base pointer is 16 bits wide, 0x20 is hi byte. */
133 wrptr = dlfb_set_register(wrptr, 0x20, base >> 16);
134 wrptr = dlfb_set_register(wrptr, 0x21, base >> 8);
135 return dlfb_set_register(wrptr, 0x22, base);
139 * DisplayLink HW has separate 16bpp and 8bpp framebuffers.
140 * In 24bpp modes, the low 323 RGB bits go in the 8bpp framebuffer
142 static char *dlfb_set_base8bpp(char *wrptr, u32 base)
144 wrptr = dlfb_set_register(wrptr, 0x26, base >> 16);
145 wrptr = dlfb_set_register(wrptr, 0x27, base >> 8);
146 return dlfb_set_register(wrptr, 0x28, base);
149 static char *dlfb_set_register_16(char *wrptr, u8 reg, u16 value)
151 wrptr = dlfb_set_register(wrptr, reg, value >> 8);
152 return dlfb_set_register(wrptr, reg+1, value);
156 * This is kind of weird because the controller takes some
157 * register values in a different byte order than other registers.
159 static char *dlfb_set_register_16be(char *wrptr, u8 reg, u16 value)
161 wrptr = dlfb_set_register(wrptr, reg, value);
162 return dlfb_set_register(wrptr, reg+1, value >> 8);
166 * LFSR is linear feedback shift register. The reason we have this is
167 * because the display controller needs to minimize the clock depth of
168 * various counters used in the display path. So this code reverses the
169 * provided value into the lfsr16 value by counting backwards to get
170 * the value that needs to be set in the hardware comparator to get the
171 * same actual count. This makes sense once you read above a couple of
172 * times and think about it from a hardware perspective.
174 static u16 dlfb_lfsr16(u16 actual_count)
176 u32 lv = 0xFFFF; /* This is the lfsr value that the hw starts with */
178 while (actual_count--) {
180 (((lv >> 15) ^ (lv >> 4) ^ (lv >> 2) ^ (lv >> 1)) & 1))
188 * This does LFSR conversion on the value that is to be written.
189 * See LFSR explanation above for more detail.
191 static char *dlfb_set_register_lfsr16(char *wrptr, u8 reg, u16 value)
193 return dlfb_set_register_16(wrptr, reg, dlfb_lfsr16(value));
197 * This takes a standard fbdev screeninfo struct and all of its monitor mode
198 * details and converts them into the DisplayLink equivalent register commands.
200 static char *dlfb_set_vid_cmds(char *wrptr, struct fb_var_screeninfo *var)
206 /* x display start */
207 xds = var->left_margin + var->hsync_len;
208 wrptr = dlfb_set_register_lfsr16(wrptr, 0x01, xds);
210 xde = xds + var->xres;
211 wrptr = dlfb_set_register_lfsr16(wrptr, 0x03, xde);
213 /* y display start */
214 yds = var->upper_margin + var->vsync_len;
215 wrptr = dlfb_set_register_lfsr16(wrptr, 0x05, yds);
217 yde = yds + var->yres;
218 wrptr = dlfb_set_register_lfsr16(wrptr, 0x07, yde);
220 /* x end count is active + blanking - 1 */
221 wrptr = dlfb_set_register_lfsr16(wrptr, 0x09,
222 xde + var->right_margin - 1);
224 /* libdlo hardcodes hsync start to 1 */
225 wrptr = dlfb_set_register_lfsr16(wrptr, 0x0B, 1);
227 /* hsync end is width of sync pulse + 1 */
228 wrptr = dlfb_set_register_lfsr16(wrptr, 0x0D, var->hsync_len + 1);
230 /* hpixels is active pixels */
231 wrptr = dlfb_set_register_16(wrptr, 0x0F, var->xres);
233 /* yendcount is vertical active + vertical blanking */
234 yec = var->yres + var->upper_margin + var->lower_margin +
236 wrptr = dlfb_set_register_lfsr16(wrptr, 0x11, yec);
238 /* libdlo hardcodes vsync start to 0 */
239 wrptr = dlfb_set_register_lfsr16(wrptr, 0x13, 0);
241 /* vsync end is width of vsync pulse */
242 wrptr = dlfb_set_register_lfsr16(wrptr, 0x15, var->vsync_len);
244 /* vpixels is active pixels */
245 wrptr = dlfb_set_register_16(wrptr, 0x17, var->yres);
247 /* convert picoseconds to 5kHz multiple for pclk5k = x * 1E12/5k */
248 wrptr = dlfb_set_register_16be(wrptr, 0x1B,
249 200*1000*1000/var->pixclock);
255 * This takes a standard fbdev screeninfo struct that was fetched or prepared
256 * and then generates the appropriate command sequence that then drives the
257 * display controller.
259 static int dlfb_set_video_mode(struct dlfb_data *dev,
260 struct fb_var_screeninfo *var)
268 if (!atomic_read(&dev->usb_active))
271 urb = dlfb_get_urb(dev);
274 buf = (char *) urb->transfer_buffer;
277 * This first section has to do with setting the base address on the
278 * controller * associated with the display. There are 2 base
279 * pointers, currently, we only * use the 16 bpp segment.
281 wrptr = dlfb_vidreg_lock(buf);
282 wrptr = dlfb_set_color_depth(wrptr, 0x00);
283 /* set base for 16bpp segment to 0 */
284 wrptr = dlfb_set_base16bpp(wrptr, 0);
285 /* set base for 8bpp segment to end of fb */
286 wrptr = dlfb_set_base8bpp(wrptr, dev->info->fix.smem_len);
288 wrptr = dlfb_set_vid_cmds(wrptr, var);
289 wrptr = dlfb_enable_hvsync(wrptr, true);
290 wrptr = dlfb_vidreg_unlock(wrptr);
292 writesize = wrptr - buf;
294 retval = dlfb_submit_urb(dev, urb, writesize);
299 static int dlfb_ops_mmap(struct fb_info *info, struct vm_area_struct *vma)
301 unsigned long start = vma->vm_start;
302 unsigned long size = vma->vm_end - vma->vm_start;
303 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
304 unsigned long page, pos;
306 dl_notice("MMAP: %lu %u\n", offset + size, info->fix.smem_len);
308 if (offset + size > info->fix.smem_len)
311 pos = (unsigned long)info->fix.smem_start + offset;
314 page = vmalloc_to_pfn((void *)pos);
315 if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED))
320 if (size > PAGE_SIZE)
326 vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
332 * Trims identical data from front and back of line
333 * Sets new front buffer address and width
334 * And returns byte count of identical pixels
335 * Assumes CPU natural alignment (unsigned long)
336 * for back and front buffer ptrs and width
338 static int dlfb_trim_hline(const u8 *bback, const u8 **bfront, int *width_bytes)
341 const unsigned long *back = (const unsigned long *) bback;
342 const unsigned long *front = (const unsigned long *) *bfront;
343 const int width = *width_bytes / sizeof(unsigned long);
344 int identical = width;
348 prefetch((void *) front);
349 prefetch((void *) back);
351 for (j = 0; j < width; j++) {
352 if (back[j] != front[j]) {
358 for (k = width - 1; k > j; k--) {
359 if (back[k] != front[k]) {
365 identical = start + (width - end);
366 *bfront = (u8 *) &front[start];
367 *width_bytes = (end - start) * sizeof(unsigned long);
369 return identical * sizeof(unsigned long);
373 * Render a command stream for an encoded horizontal line segment of pixels.
375 * A command buffer holds several commands.
376 * It always begins with a fresh command header
377 * (the protocol doesn't require this, but we enforce it to allow
378 * multiple buffers to be potentially encoded and sent in parallel).
379 * A single command encodes one contiguous horizontal line of pixels
381 * The function relies on the client to do all allocation, so that
382 * rendering can be done directly to output buffers (e.g. USB URBs).
383 * The function fills the supplied command buffer, providing information
384 * on where it left off, so the client may call in again with additional
385 * buffers if the line will take several buffers to complete.
387 * A single command can transmit a maximum of 256 pixels,
388 * regardless of the compression ratio (protocol design limit).
389 * To the hardware, 0 for a size byte means 256
391 * Rather than 256 pixel commands which are either rl or raw encoded,
392 * the rlx command simply assumes alternating raw and rl spans within one cmd.
393 * This has a slightly larger header overhead, but produces more even results.
394 * It also processes all data (read and write) in a single pass.
395 * Performance benchmarks of common cases show it having just slightly better
396 * compression than 256 pixel raw -or- rle commands, with similar CPU consumpion.
397 * But for very rl friendly data, will compress not quite as well.
399 static void dlfb_compress_hline(
400 const uint16_t **pixel_start_ptr,
401 const uint16_t *const pixel_end,
402 uint32_t *device_address_ptr,
403 uint8_t **command_buffer_ptr,
404 const uint8_t *const cmd_buffer_end)
406 const uint16_t *pixel = *pixel_start_ptr;
407 uint32_t dev_addr = *device_address_ptr;
408 uint8_t *cmd = *command_buffer_ptr;
411 while ((pixel_end > pixel) &&
412 (cmd_buffer_end - MIN_RLX_CMD_BYTES > cmd)) {
413 uint8_t *raw_pixels_count_byte = 0;
414 uint8_t *cmd_pixels_count_byte = 0;
415 const uint16_t *raw_pixel_start = 0;
416 const uint16_t *cmd_pixel_start, *cmd_pixel_end = 0;
417 const uint32_t be_dev_addr = cpu_to_be32(dev_addr);
419 prefetchw((void *) cmd); /* pull in one cache line at least */
423 *cmd++ = (uint8_t) ((be_dev_addr >> 8) & 0xFF);
424 *cmd++ = (uint8_t) ((be_dev_addr >> 16) & 0xFF);
425 *cmd++ = (uint8_t) ((be_dev_addr >> 24) & 0xFF);
427 cmd_pixels_count_byte = cmd++; /* we'll know this later */
428 cmd_pixel_start = pixel;
430 raw_pixels_count_byte = cmd++; /* we'll know this later */
431 raw_pixel_start = pixel;
433 cmd_pixel_end = pixel + min(MAX_CMD_PIXELS + 1,
434 min((int)(pixel_end - pixel),
435 (int)(cmd_buffer_end - cmd) / bpp));
437 prefetch_range((void *) pixel, (cmd_pixel_end - pixel) * bpp);
439 while (pixel < cmd_pixel_end) {
440 const uint16_t * const repeating_pixel = pixel;
442 *(uint16_t *)cmd = cpu_to_be16p(pixel);
446 if (unlikely((pixel < cmd_pixel_end) &&
447 (*pixel == *repeating_pixel))) {
448 /* go back and fill in raw pixel count */
449 *raw_pixels_count_byte = ((repeating_pixel -
450 raw_pixel_start) + 1) & 0xFF;
452 while ((pixel < cmd_pixel_end)
453 && (*pixel == *repeating_pixel)) {
457 /* immediately after raw data is repeat byte */
458 *cmd++ = ((pixel - repeating_pixel) - 1) & 0xFF;
460 /* Then start another raw pixel span */
461 raw_pixel_start = pixel;
462 raw_pixels_count_byte = cmd++;
466 if (pixel > raw_pixel_start) {
467 /* finalize last RAW span */
468 *raw_pixels_count_byte = (pixel-raw_pixel_start) & 0xFF;
471 *cmd_pixels_count_byte = (pixel - cmd_pixel_start) & 0xFF;
472 dev_addr += (pixel - cmd_pixel_start) * bpp;
475 if (cmd_buffer_end <= MIN_RLX_CMD_BYTES + cmd) {
476 /* Fill leftover bytes with no-ops */
477 if (cmd_buffer_end > cmd)
478 memset(cmd, 0xAF, cmd_buffer_end - cmd);
479 cmd = (uint8_t *) cmd_buffer_end;
482 *command_buffer_ptr = cmd;
483 *pixel_start_ptr = pixel;
484 *device_address_ptr = dev_addr;
490 * There are 3 copies of every pixel: The front buffer that the fbdev
491 * client renders to, the actual framebuffer across the USB bus in hardware
492 * (that we can only write to, slowly, and can never read), and (optionally)
493 * our shadow copy that tracks what's been sent to that hardware buffer.
495 static void dlfb_render_hline(struct dlfb_data *dev, struct urb **urb_ptr,
496 const char *front, char **urb_buf_ptr,
497 u32 byte_offset, u32 byte_width,
498 int *ident_ptr, int *sent_ptr)
500 const u8 *line_start, *line_end, *next_pixel;
501 u32 dev_addr = dev->base16 + byte_offset;
502 struct urb *urb = *urb_ptr;
503 u8 *cmd = *urb_buf_ptr;
504 u8 *cmd_end = (u8 *) urb->transfer_buffer + urb->transfer_buffer_length;
506 line_start = (u8 *) (front + byte_offset);
507 next_pixel = line_start;
508 line_end = next_pixel + byte_width;
510 if (dev->backing_buffer) {
512 const u8 *back_start = (u8 *) (dev->backing_buffer
515 *ident_ptr += dlfb_trim_hline(back_start, &next_pixel,
518 offset = next_pixel - line_start;
519 line_end = next_pixel + byte_width;
521 back_start += offset;
522 line_start += offset;
524 memcpy((char *)back_start, (char *) line_start,
528 while (next_pixel < line_end) {
530 dlfb_compress_hline((const uint16_t **) &next_pixel,
531 (const uint16_t *) line_end, &dev_addr,
532 (u8 **) &cmd, (u8 *) cmd_end);
534 if (cmd >= cmd_end) {
535 int len = cmd - (u8 *) urb->transfer_buffer;
536 if (dlfb_submit_urb(dev, urb, len))
537 return; /* lost pixels is set */
539 urb = dlfb_get_urb(dev);
541 return; /* lost_pixels is set */
543 cmd = urb->transfer_buffer;
544 cmd_end = &cmd[urb->transfer_buffer_length];
551 int dlfb_handle_damage(struct dlfb_data *dev, int x, int y,
552 int width, int height, char *data)
556 cycles_t start_cycles, end_cycles;
558 int bytes_identical = 0;
562 start_cycles = get_cycles();
564 aligned_x = DL_ALIGN_DOWN(x, sizeof(unsigned long));
565 width = DL_ALIGN_UP(width + (x-aligned_x), sizeof(unsigned long));
569 (x + width > dev->info->var.xres) ||
570 (y + height > dev->info->var.yres))
573 if (!atomic_read(&dev->usb_active))
576 urb = dlfb_get_urb(dev);
579 cmd = urb->transfer_buffer;
581 for (i = y; i < y + height ; i++) {
582 const int line_offset = dev->info->fix.line_length * i;
583 const int byte_offset = line_offset + (x * BPP);
585 dlfb_render_hline(dev, &urb, (char *) dev->info->fix.smem_start,
586 &cmd, byte_offset, width * BPP,
587 &bytes_identical, &bytes_sent);
590 if (cmd > (char *) urb->transfer_buffer) {
591 /* Send partial buffer remaining before exiting */
592 int len = cmd - (char *) urb->transfer_buffer;
593 ret = dlfb_submit_urb(dev, urb, len);
596 dlfb_urb_completion(urb);
598 atomic_add(bytes_sent, &dev->bytes_sent);
599 atomic_add(bytes_identical, &dev->bytes_identical);
600 atomic_add(width*height*2, &dev->bytes_rendered);
601 end_cycles = get_cycles();
602 atomic_add(((unsigned int) ((end_cycles - start_cycles)
603 >> 10)), /* Kcycles */
604 &dev->cpu_kcycles_used);
609 static ssize_t dlfb_ops_read(struct fb_info *info, char __user *buf,
610 size_t count, loff_t *ppos)
612 ssize_t result = -ENOSYS;
614 #if defined CONFIG_FB_SYS_FOPS || defined CONFIG_FB_SYS_FOPS_MODULE
615 result = fb_sys_read(info, buf, count, ppos);
622 * Path triggered by usermode clients who write to filesystem
623 * e.g. cat filename > /dev/fb1
624 * Not used by X Windows or text-mode console. But useful for testing.
625 * Slow because of extra copy and we must assume all pixels dirty.
627 static ssize_t dlfb_ops_write(struct fb_info *info, const char __user *buf,
628 size_t count, loff_t *ppos)
630 ssize_t result = -ENOSYS;
631 struct dlfb_data *dev = info->par;
632 u32 offset = (u32) *ppos;
634 #if defined CONFIG_FB_SYS_FOPS || defined CONFIG_FB_SYS_FOPS_MODULE
636 result = fb_sys_write(info, buf, count, ppos);
639 int start = max((int)(offset / info->fix.line_length) - 1, 0);
640 int lines = min((u32)((result / info->fix.line_length) + 1),
641 (u32)info->var.yres);
643 dlfb_handle_damage(dev, 0, start, info->var.xres,
644 lines, info->screen_base);
651 /* hardware has native COPY command (see libdlo), but not worth it for fbcon */
652 static void dlfb_ops_copyarea(struct fb_info *info,
653 const struct fb_copyarea *area)
656 struct dlfb_data *dev = info->par;
658 #if defined CONFIG_FB_SYS_COPYAREA || defined CONFIG_FB_SYS_COPYAREA_MODULE
660 sys_copyarea(info, area);
662 dlfb_handle_damage(dev, area->dx, area->dy,
663 area->width, area->height, info->screen_base);
668 static void dlfb_ops_imageblit(struct fb_info *info,
669 const struct fb_image *image)
671 struct dlfb_data *dev = info->par;
673 #if defined CONFIG_FB_SYS_IMAGEBLIT || defined CONFIG_FB_SYS_IMAGEBLIT_MODULE
675 sys_imageblit(info, image);
677 dlfb_handle_damage(dev, image->dx, image->dy,
678 image->width, image->height, info->screen_base);
684 static void dlfb_ops_fillrect(struct fb_info *info,
685 const struct fb_fillrect *rect)
687 struct dlfb_data *dev = info->par;
689 #if defined CONFIG_FB_SYS_FILLRECT || defined CONFIG_FB_SYS_FILLRECT_MODULE
691 sys_fillrect(info, rect);
693 dlfb_handle_damage(dev, rect->dx, rect->dy, rect->width,
694 rect->height, info->screen_base);
699 static int dlfb_get_edid(struct dlfb_data *dev, char *edid, int len)
705 rbuf = kmalloc(2, GFP_KERNEL);
709 for (i = 0; i < len; i++) {
710 ret = usb_control_msg(dev->udev,
711 usb_rcvctrlpipe(dev->udev, 0), (0x02),
712 (0x80 | (0x02 << 5)), i << 8, 0xA1, rbuf, 2,
715 dl_err("Read EDID byte %d failed err %x\n", i, ret);
727 static int dlfb_ops_ioctl(struct fb_info *info, unsigned int cmd,
731 struct dlfb_data *dev = info->par;
732 struct dloarea *area = NULL;
734 if (!atomic_read(&dev->usb_active))
737 /* TODO: Update X server to get this from sysfs instead */
738 if (cmd == DLFB_IOCTL_RETURN_EDID) {
739 char *edid = (char *)arg;
740 if (copy_to_user(edid, dev->edid, dev->edid_size))
745 /* TODO: Help propose a standard fb.h ioctl to report mmap damage */
746 if (cmd == DLFB_IOCTL_REPORT_DAMAGE) {
748 area = (struct dloarea *)arg;
753 if (area->x > info->var.xres)
754 area->x = info->var.xres;
759 if (area->y > info->var.yres)
760 area->y = info->var.yres;
762 atomic_set(&dev->use_defio, 0);
764 dlfb_handle_damage(dev, area->x, area->y, area->w, area->h,
771 /* taken from vesafb */
773 dlfb_ops_setcolreg(unsigned regno, unsigned red, unsigned green,
774 unsigned blue, unsigned transp, struct fb_info *info)
778 if (regno >= info->cmap.len)
782 if (info->var.red.offset == 10) {
784 ((u32 *) (info->pseudo_palette))[regno] =
785 ((red & 0xf800) >> 1) |
786 ((green & 0xf800) >> 6) | ((blue & 0xf800) >> 11);
789 ((u32 *) (info->pseudo_palette))[regno] =
791 ((green & 0xfc00) >> 5) | ((blue & 0xf800) >> 11);
799 * It's common for several clients to have framebuffer open simultaneously.
800 * e.g. both fbcon and X. Makes things interesting.
801 * Assumes caller is holding info->lock (for open and release at least)
803 static int dlfb_ops_open(struct fb_info *info, int user)
805 struct dlfb_data *dev = info->par;
808 * We could special case kernel mode clients (fbcon) here
811 /* If the USB device is gone, we don't accept new opens */
812 if (dev->virtualized)
817 kref_get(&dev->kref);
819 #ifdef CONFIG_FB_DEFERRED_IO
820 if ((atomic_read(&dev->use_defio)) && (info->fbdefio == NULL)) {
822 info->fbdefio = &dlfb_defio;
823 fb_deferred_io_init(info);
827 dl_notice("open /dev/fb%d user=%d fb_info=%p count=%d\n",
828 info->node, user, info, dev->fb_count);
834 * Called when all client interfaces to start transactions have been disabled,
835 * and all references to our device instance (dlfb_data) are released.
836 * Every transaction must have a reference, so we know are fully spun down
838 static void dlfb_free(struct kref *kref)
840 struct dlfb_data *dev = container_of(kref, struct dlfb_data, kref);
842 /* this function will wait for all in-flight urbs to complete */
843 if (dev->urbs.count > 0)
844 dlfb_free_urb_list(dev);
846 if (dev->backing_buffer)
847 vfree(dev->backing_buffer);
851 dl_warn("freeing dlfb_data %p\n", dev);
857 static void dlfb_free_framebuffer_work(struct work_struct *work)
859 struct dlfb_data *dev = container_of(work, struct dlfb_data,
860 free_framebuffer_work.work);
861 struct fb_info *info = dev->info;
862 int node = info->node;
864 unregister_framebuffer(info);
866 if (info->cmap.len != 0)
867 fb_dealloc_cmap(&info->cmap);
868 if (info->monspecs.modedb)
869 fb_destroy_modedb(info->monspecs.modedb);
870 if (info->screen_base)
871 vfree(info->screen_base);
873 fb_destroy_modelist(&info->modelist);
877 /* Assume info structure is freed after this point */
878 framebuffer_release(info);
880 dl_warn("fb_info for /dev/fb%d has been freed\n", node);
882 /* ref taken in probe() as part of registering framebfufer */
883 kref_put(&dev->kref, dlfb_free);
887 * Assumes caller is holding info->lock mutex (for open and release at least)
889 static int dlfb_ops_release(struct fb_info *info, int user)
891 struct dlfb_data *dev = info->par;
895 /* We can't free fb_info here - fbmem will touch it when we return */
896 if (dev->virtualized && (dev->fb_count == 0))
897 schedule_delayed_work(&dev->free_framebuffer_work, HZ);
899 #ifdef CONFIG_FB_DEFERRED_IO
900 if ((dev->fb_count == 0) && (info->fbdefio)) {
901 fb_deferred_io_cleanup(info);
902 kfree(info->fbdefio);
903 info->fbdefio = NULL;
904 info->fbops->fb_mmap = dlfb_ops_mmap;
908 dl_warn("released /dev/fb%d user=%d count=%d\n",
909 info->node, user, dev->fb_count);
911 kref_put(&dev->kref, dlfb_free);
917 * Check whether a video mode is supported by the DisplayLink chip
918 * We start from monitor's modes, so don't need to filter that here
920 static int dlfb_is_valid_mode(struct fb_videomode *mode,
921 struct fb_info *info)
923 struct dlfb_data *dev = info->par;
925 if (mode->xres * mode->yres > dev->sku_pixel_limit) {
926 dl_warn("%dx%d beyond chip capabilities\n",
927 mode->xres, mode->yres);
931 dl_info("%dx%d valid mode\n", mode->xres, mode->yres);
936 static void dlfb_var_color_format(struct fb_var_screeninfo *var)
938 const struct fb_bitfield red = { 11, 5, 0 };
939 const struct fb_bitfield green = { 5, 6, 0 };
940 const struct fb_bitfield blue = { 0, 5, 0 };
942 var->bits_per_pixel = 16;
948 static int dlfb_ops_check_var(struct fb_var_screeninfo *var,
949 struct fb_info *info)
951 struct fb_videomode mode;
953 /* TODO: support dynamically changing framebuffer size */
954 if ((var->xres * var->yres * 2) > info->fix.smem_len)
957 /* set device-specific elements of var unrelated to mode */
958 dlfb_var_color_format(var);
960 fb_var_to_videomode(&mode, var);
962 if (!dlfb_is_valid_mode(&mode, info))
968 static int dlfb_ops_set_par(struct fb_info *info)
970 struct dlfb_data *dev = info->par;
972 u16 *pix_framebuffer;
975 dl_notice("set_par mode %dx%d\n", info->var.xres, info->var.yres);
977 result = dlfb_set_video_mode(dev, &info->var);
979 if ((result == 0) && (dev->fb_count == 0)) {
981 /* paint greenscreen */
983 pix_framebuffer = (u16 *) info->screen_base;
984 for (i = 0; i < info->fix.smem_len / 2; i++)
985 pix_framebuffer[i] = 0x37e6;
987 dlfb_handle_damage(dev, 0, 0, info->var.xres, info->var.yres,
995 * In order to come back from full DPMS off, we need to set the mode again
997 static int dlfb_ops_blank(int blank_mode, struct fb_info *info)
999 struct dlfb_data *dev = info->par;
1001 if (blank_mode != FB_BLANK_UNBLANK) {
1005 urb = dlfb_get_urb(dev);
1009 bufptr = (char *) urb->transfer_buffer;
1010 bufptr = dlfb_vidreg_lock(bufptr);
1011 bufptr = dlfb_enable_hvsync(bufptr, false);
1012 bufptr = dlfb_vidreg_unlock(bufptr);
1014 dlfb_submit_urb(dev, urb, bufptr -
1015 (char *) urb->transfer_buffer);
1017 dlfb_set_video_mode(dev, &info->var);
1023 static struct fb_ops dlfb_ops = {
1024 .owner = THIS_MODULE,
1025 .fb_read = dlfb_ops_read,
1026 .fb_write = dlfb_ops_write,
1027 .fb_setcolreg = dlfb_ops_setcolreg,
1028 .fb_fillrect = dlfb_ops_fillrect,
1029 .fb_copyarea = dlfb_ops_copyarea,
1030 .fb_imageblit = dlfb_ops_imageblit,
1031 .fb_mmap = dlfb_ops_mmap,
1032 .fb_ioctl = dlfb_ops_ioctl,
1033 .fb_open = dlfb_ops_open,
1034 .fb_release = dlfb_ops_release,
1035 .fb_blank = dlfb_ops_blank,
1036 .fb_check_var = dlfb_ops_check_var,
1037 .fb_set_par = dlfb_ops_set_par,
1042 * Assumes &info->lock held by caller
1043 * Assumes no active clients have framebuffer open
1045 static int dlfb_realloc_framebuffer(struct dlfb_data *dev, struct fb_info *info)
1047 int retval = -ENOMEM;
1048 int old_len = info->fix.smem_len;
1050 unsigned char *old_fb = info->screen_base;
1051 unsigned char *new_fb;
1052 unsigned char *new_back;
1054 dl_warn("Reallocating framebuffer. Addresses will change!\n");
1056 new_len = info->fix.line_length * info->var.yres;
1058 if (PAGE_ALIGN(new_len) > old_len) {
1060 * Alloc system memory for virtual framebuffer
1062 new_fb = vmalloc(new_len);
1064 dl_err("Virtual framebuffer alloc failed\n");
1068 if (info->screen_base) {
1069 memcpy(new_fb, old_fb, old_len);
1070 vfree(info->screen_base);
1073 info->screen_base = new_fb;
1074 info->fix.smem_len = PAGE_ALIGN(new_len);
1075 info->fix.smem_start = (unsigned long) new_fb;
1076 info->flags = udlfb_info_flags;
1079 * Second framebuffer copy to mirror the framebuffer state
1080 * on the physical USB device. We can function without this.
1081 * But with imperfect damage info we may send pixels over USB
1082 * that were, in fact, unchanged - wasting limited USB bandwidth
1084 new_back = vmalloc(new_len);
1086 dl_info("No shadow/backing buffer allcoated\n");
1088 if (dev->backing_buffer)
1089 vfree(dev->backing_buffer);
1090 dev->backing_buffer = new_back;
1091 memset(dev->backing_buffer, 0, new_len);
1102 * 1) Get EDID from hw, or use sw default
1103 * 2) Parse into various fb_info structs
1104 * 3) Allocate virtual framebuffer memory to back highest res mode
1106 * Parses EDID into three places used by various parts of fbdev:
1107 * fb_var_screeninfo contains the timing of the monitor's preferred mode
1108 * fb_info.monspecs is full parsed EDID info, including monspecs.modedb
1109 * fb_info.modelist is a linked list of all monitor & VESA modes which work
1111 * If EDID is not readable/valid, then modelist is all VESA modes,
1112 * monspecs is NULL, and fb_var_screeninfo is set to safe VESA mode
1113 * Returns 0 if successful
1115 static int dlfb_setup_modes(struct dlfb_data *dev,
1116 struct fb_info *info,
1117 char *default_edid, size_t default_edid_size)
1120 const struct fb_videomode *default_vmode = NULL;
1125 if (info->dev) /* only use mutex if info has been registered */
1126 mutex_lock(&info->lock);
1128 edid = kmalloc(MAX_EDID_SIZE, GFP_KERNEL);
1134 fb_destroy_modelist(&info->modelist);
1135 memset(&info->monspecs, 0, sizeof(info->monspecs));
1138 * Try to (re)read EDID from hardware first
1139 * EDID data may return, but not parse as valid
1140 * Try again a few times, in case of e.g. analog cable noise
1144 i = dlfb_get_edid(dev, edid, MAX_EDID_SIZE);
1146 if (i >= MIN_EDID_SIZE)
1147 fb_edid_to_monspecs(edid, &info->monspecs);
1149 if (info->monspecs.modedb_len > 0) {
1156 /* If that fails, use a previously returned EDID if available */
1157 if (info->monspecs.modedb_len == 0) {
1159 dl_err("Unable to get valid EDID from device/display\n");
1162 fb_edid_to_monspecs(dev->edid, &info->monspecs);
1163 if (info->monspecs.modedb_len > 0)
1164 dl_err("Using previously queried EDID\n");
1168 /* If that fails, use the default EDID we were handed */
1169 if (info->monspecs.modedb_len == 0) {
1170 if (default_edid_size >= MIN_EDID_SIZE) {
1171 fb_edid_to_monspecs(default_edid, &info->monspecs);
1172 if (info->monspecs.modedb_len > 0) {
1173 memcpy(edid, default_edid, default_edid_size);
1175 dev->edid_size = default_edid_size;
1176 dl_err("Using default/backup EDID\n");
1181 /* If we've got modes, let's pick a best default mode */
1182 if (info->monspecs.modedb_len > 0) {
1184 for (i = 0; i < info->monspecs.modedb_len; i++) {
1185 if (dlfb_is_valid_mode(&info->monspecs.modedb[i], info))
1186 fb_add_videomode(&info->monspecs.modedb[i],
1188 else /* if we've removed top/best mode */
1189 info->monspecs.misc &= ~FB_MISC_1ST_DETAIL;
1192 default_vmode = fb_find_best_display(&info->monspecs,
1196 /* If everything else has failed, fall back to safe default mode */
1197 if (default_vmode == NULL) {
1199 struct fb_videomode fb_vmode = {0};
1202 * Add the standard VESA modes to our modelist
1203 * Since we don't have EDID, there may be modes that
1204 * overspec monitor and/or are incorrect aspect ratio, etc.
1205 * But at least the user has a chance to choose
1207 for (i = 0; i < VESA_MODEDB_SIZE; i++) {
1208 if (dlfb_is_valid_mode((struct fb_videomode *)
1209 &vesa_modes[i], info))
1210 fb_add_videomode(&vesa_modes[i],
1215 * default to resolution safe for projectors
1216 * (since they are most common case without EDID)
1218 fb_vmode.xres = 800;
1219 fb_vmode.yres = 600;
1220 fb_vmode.refresh = 60;
1221 default_vmode = fb_find_nearest_mode(&fb_vmode,
1225 /* If we have good mode and no active clients*/
1226 if ((default_vmode != NULL) && (dev->fb_count == 0)) {
1228 fb_videomode_to_var(&info->var, default_vmode);
1229 dlfb_var_color_format(&info->var);
1232 * with mode size info, we can now alloc our framebuffer.
1234 memcpy(&info->fix, &dlfb_fix, sizeof(dlfb_fix));
1235 info->fix.line_length = info->var.xres *
1236 (info->var.bits_per_pixel / 8);
1238 result = dlfb_realloc_framebuffer(dev, info);
1244 if (edid && (dev->edid != edid))
1248 mutex_unlock(&info->lock);
1253 static ssize_t metrics_bytes_rendered_show(struct device *fbdev,
1254 struct device_attribute *a, char *buf) {
1255 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1256 struct dlfb_data *dev = fb_info->par;
1257 return snprintf(buf, PAGE_SIZE, "%u\n",
1258 atomic_read(&dev->bytes_rendered));
1261 static ssize_t metrics_bytes_identical_show(struct device *fbdev,
1262 struct device_attribute *a, char *buf) {
1263 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1264 struct dlfb_data *dev = fb_info->par;
1265 return snprintf(buf, PAGE_SIZE, "%u\n",
1266 atomic_read(&dev->bytes_identical));
1269 static ssize_t metrics_bytes_sent_show(struct device *fbdev,
1270 struct device_attribute *a, char *buf) {
1271 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1272 struct dlfb_data *dev = fb_info->par;
1273 return snprintf(buf, PAGE_SIZE, "%u\n",
1274 atomic_read(&dev->bytes_sent));
1277 static ssize_t metrics_cpu_kcycles_used_show(struct device *fbdev,
1278 struct device_attribute *a, char *buf) {
1279 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1280 struct dlfb_data *dev = fb_info->par;
1281 return snprintf(buf, PAGE_SIZE, "%u\n",
1282 atomic_read(&dev->cpu_kcycles_used));
1285 static ssize_t edid_show(
1287 struct kobject *kobj, struct bin_attribute *a,
1288 char *buf, loff_t off, size_t count) {
1289 struct device *fbdev = container_of(kobj, struct device, kobj);
1290 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1291 struct dlfb_data *dev = fb_info->par;
1293 if (dev->edid == NULL)
1296 if ((off >= dev->edid_size) || (count > dev->edid_size))
1299 if (off + count > dev->edid_size)
1300 count = dev->edid_size - off;
1302 dl_info("sysfs edid copy %p to %p, %d bytes\n",
1303 dev->edid, buf, (int) count);
1305 memcpy(buf, dev->edid, count);
1311 static ssize_t metrics_reset_store(struct device *fbdev,
1312 struct device_attribute *attr,
1313 const char *buf, size_t count)
1315 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1316 struct dlfb_data *dev = fb_info->par;
1318 atomic_set(&dev->bytes_rendered, 0);
1319 atomic_set(&dev->bytes_identical, 0);
1320 atomic_set(&dev->bytes_sent, 0);
1321 atomic_set(&dev->cpu_kcycles_used, 0);
1326 static ssize_t use_defio_show(struct device *fbdev,
1327 struct device_attribute *a, char *buf) {
1328 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1329 struct dlfb_data *dev = fb_info->par;
1330 return snprintf(buf, PAGE_SIZE, "%d\n",
1331 atomic_read(&dev->use_defio));
1334 static ssize_t use_defio_store(struct device *fbdev,
1335 struct device_attribute *attr,
1336 const char *buf, size_t count)
1338 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1339 struct dlfb_data *dev = fb_info->par;
1343 atomic_set(&dev->use_defio, 0);
1345 atomic_set(&dev->use_defio, 1);
1350 static struct bin_attribute edid_attr = {
1351 .attr.name = "edid",
1357 static struct device_attribute fb_device_attrs[] = {
1358 __ATTR_RO(metrics_bytes_rendered),
1359 __ATTR_RO(metrics_bytes_identical),
1360 __ATTR_RO(metrics_bytes_sent),
1361 __ATTR_RO(metrics_cpu_kcycles_used),
1362 __ATTR(metrics_reset, S_IWUGO, NULL, metrics_reset_store),
1363 __ATTR_RW(use_defio),
1366 #ifdef CONFIG_FB_DEFERRED_IO
1367 static void dlfb_dpy_deferred_io(struct fb_info *info,
1368 struct list_head *pagelist)
1371 struct fb_deferred_io *fbdefio = info->fbdefio;
1372 struct dlfb_data *dev = info->par;
1375 cycles_t start_cycles, end_cycles;
1377 int bytes_identical = 0;
1378 int bytes_rendered = 0;
1380 if (!atomic_read(&dev->use_defio))
1383 if (!atomic_read(&dev->usb_active))
1386 start_cycles = get_cycles();
1388 urb = dlfb_get_urb(dev);
1391 cmd = urb->transfer_buffer;
1393 /* walk the written page list and render each to device */
1394 list_for_each_entry(cur, &fbdefio->pagelist, lru) {
1395 dlfb_render_hline(dev, &urb, (char *) info->fix.smem_start,
1396 &cmd, cur->index << PAGE_SHIFT,
1397 PAGE_SIZE, &bytes_identical, &bytes_sent);
1398 bytes_rendered += PAGE_SIZE;
1401 if (cmd > (char *) urb->transfer_buffer) {
1402 /* Send partial buffer remaining before exiting */
1403 int len = cmd - (char *) urb->transfer_buffer;
1404 dlfb_submit_urb(dev, urb, len);
1407 dlfb_urb_completion(urb);
1409 atomic_add(bytes_sent, &dev->bytes_sent);
1410 atomic_add(bytes_identical, &dev->bytes_identical);
1411 atomic_add(bytes_rendered, &dev->bytes_rendered);
1412 end_cycles = get_cycles();
1413 atomic_add(((unsigned int) ((end_cycles - start_cycles)
1414 >> 10)), /* Kcycles */
1415 &dev->cpu_kcycles_used);
1418 static struct fb_deferred_io dlfb_defio = {
1420 .deferred_io = dlfb_dpy_deferred_io,
1426 * This is necessary before we can communicate with the display controller.
1428 static int dlfb_select_std_channel(struct dlfb_data *dev)
1431 u8 set_def_chn[] = { 0x57, 0xCD, 0xDC, 0xA7,
1432 0x1C, 0x88, 0x5E, 0x15,
1433 0x60, 0xFE, 0xC6, 0x97,
1434 0x16, 0x3D, 0x47, 0xF2 };
1436 ret = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
1437 NR_USB_REQUEST_CHANNEL,
1438 (USB_DIR_OUT | USB_TYPE_VENDOR), 0, 0,
1439 set_def_chn, sizeof(set_def_chn), USB_CTRL_SET_TIMEOUT);
1443 static int dlfb_parse_vendor_descriptor(struct dlfb_data *dev,
1444 struct usb_device *usbdev)
1452 buf = kzalloc(MAX_VENDOR_DESCRIPTOR_SIZE, GFP_KERNEL);
1457 total_len = usb_get_descriptor(usbdev, 0x5f, /* vendor specific */
1458 0, desc, MAX_VENDOR_DESCRIPTOR_SIZE);
1459 if (total_len > 5) {
1460 dl_info("vendor descriptor length:%x data:%02x %02x %02x %02x" \
1461 "%02x %02x %02x %02x %02x %02x %02x\n",
1463 desc[1], desc[2], desc[3], desc[4], desc[5], desc[6],
1464 desc[7], desc[8], desc[9], desc[10]);
1466 if ((desc[0] != total_len) || /* descriptor length */
1467 (desc[1] != 0x5f) || /* vendor descriptor type */
1468 (desc[2] != 0x01) || /* version (2 bytes) */
1469 (desc[3] != 0x00) ||
1470 (desc[4] != total_len - 2)) /* length after type */
1473 desc_end = desc + total_len;
1474 desc += 5; /* the fixed header we've already parsed */
1476 while (desc < desc_end) {
1480 key = *((u16 *) desc);
1481 desc += sizeof(u16);
1486 case 0x0200: { /* max_area */
1488 max_area = le32_to_cpu(*((u32 *)desc));
1489 dl_warn("DL chip limited to %d pixel modes\n",
1491 dev->sku_pixel_limit = max_area;
1504 /* allow udlfb to load for now even if firmware unrecognized */
1505 dl_err("Unrecognized vendor firmware descriptor\n");
1511 static int dlfb_usb_probe(struct usb_interface *interface,
1512 const struct usb_device_id *id)
1514 struct usb_device *usbdev;
1515 struct dlfb_data *dev = 0;
1516 struct fb_info *info = 0;
1518 int retval = -ENOMEM;
1520 /* usb initialization */
1522 usbdev = interface_to_usbdev(interface);
1524 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1526 err("dlfb_usb_probe: failed alloc of dev struct\n");
1530 /* we need to wait for both usb and fbdev to spin down on disconnect */
1531 kref_init(&dev->kref); /* matching kref_put in usb .disconnect fn */
1532 kref_get(&dev->kref); /* matching kref_put in free_framebuffer_work */
1535 dev->gdev = &usbdev->dev; /* our generic struct device * */
1536 usb_set_intfdata(interface, dev);
1538 dl_info("%s %s - serial #%s\n",
1539 usbdev->manufacturer, usbdev->product, usbdev->serial);
1540 dl_info("vid_%04x&pid_%04x&rev_%04x driver's dlfb_data struct at %p\n",
1541 usbdev->descriptor.idVendor, usbdev->descriptor.idProduct,
1542 usbdev->descriptor.bcdDevice, dev);
1544 dev->sku_pixel_limit = 2048 * 1152; /* default to maximum */
1546 if (!dlfb_parse_vendor_descriptor(dev, usbdev)) {
1547 dl_err("firmware not recognized. Assume incompatible device\n");
1551 if (!dlfb_alloc_urb_list(dev, WRITES_IN_FLIGHT, MAX_TRANSFER)) {
1553 dl_err("dlfb_alloc_urb_list failed\n");
1557 /* We don't register a new USB class. Our client interface is fbdev */
1559 /* allocates framebuffer driver structure, not framebuffer memory */
1560 info = framebuffer_alloc(0, &usbdev->dev);
1563 dl_err("framebuffer_alloc failed\n");
1569 info->pseudo_palette = dev->pseudo_palette;
1570 info->fbops = &dlfb_ops;
1572 retval = fb_alloc_cmap(&info->cmap, 256, 0);
1574 dl_err("fb_alloc_cmap failed %x\n", retval);
1578 INIT_DELAYED_WORK(&dev->free_framebuffer_work,
1579 dlfb_free_framebuffer_work);
1581 INIT_LIST_HEAD(&info->modelist);
1583 retval = dlfb_setup_modes(dev, info, NULL, 0);
1585 dl_err("unable to find common mode for display and adapter\n");
1589 /* ready to begin using device */
1591 #ifdef CONFIG_FB_DEFERRED_IO
1592 atomic_set(&dev->use_defio, 1);
1594 atomic_set(&dev->usb_active, 1);
1595 dlfb_select_std_channel(dev);
1597 dlfb_ops_check_var(&info->var, info);
1598 dlfb_ops_set_par(info);
1600 retval = register_framebuffer(info);
1602 dl_err("register_framebuffer failed %d\n", retval);
1606 for (i = 0; i < ARRAY_SIZE(fb_device_attrs); i++)
1607 device_create_file(info->dev, &fb_device_attrs[i]);
1609 device_create_bin_file(info->dev, &edid_attr);
1611 dl_info("DisplayLink USB device /dev/fb%d attached. %dx%d resolution."
1612 " Using %dK framebuffer memory\n", info->node,
1613 info->var.xres, info->var.yres,
1614 ((dev->backing_buffer) ?
1615 info->fix.smem_len * 2 : info->fix.smem_len) >> 10);
1622 if (info->cmap.len != 0)
1623 fb_dealloc_cmap(&info->cmap);
1624 if (info->monspecs.modedb)
1625 fb_destroy_modedb(info->monspecs.modedb);
1626 if (info->screen_base)
1627 vfree(info->screen_base);
1629 fb_destroy_modelist(&info->modelist);
1631 framebuffer_release(info);
1634 if (dev->backing_buffer)
1635 vfree(dev->backing_buffer);
1637 kref_put(&dev->kref, dlfb_free); /* ref for framebuffer */
1638 kref_put(&dev->kref, dlfb_free); /* last ref from kref_init */
1640 /* dev has been deallocated. Do not dereference */
1646 static void dlfb_usb_disconnect(struct usb_interface *interface)
1648 struct dlfb_data *dev;
1649 struct fb_info *info;
1652 dev = usb_get_intfdata(interface);
1655 dl_info("USB disconnect starting\n");
1657 /* we virtualize until all fb clients release. Then we free */
1658 dev->virtualized = true;
1660 /* When non-active we'll update virtual framebuffer, but no new urbs */
1661 atomic_set(&dev->usb_active, 0);
1663 /* remove udlfb's sysfs interfaces */
1664 for (i = 0; i < ARRAY_SIZE(fb_device_attrs); i++)
1665 device_remove_file(info->dev, &fb_device_attrs[i]);
1666 device_remove_bin_file(info->dev, &edid_attr);
1668 usb_set_intfdata(interface, NULL);
1670 /* if clients still have us open, will be freed on last close */
1671 if (dev->fb_count == 0)
1672 schedule_delayed_work(&dev->free_framebuffer_work, 0);
1674 /* release reference taken by kref_init in probe() */
1675 kref_put(&dev->kref, dlfb_free);
1677 /* consider dlfb_data freed */
1682 static struct usb_driver dlfb_driver = {
1684 .probe = dlfb_usb_probe,
1685 .disconnect = dlfb_usb_disconnect,
1686 .id_table = id_table,
1689 static int __init dlfb_module_init(void)
1693 res = usb_register(&dlfb_driver);
1695 err("usb_register failed. Error number %d", res);
1700 static void __exit dlfb_module_exit(void)
1702 usb_deregister(&dlfb_driver);
1705 module_init(dlfb_module_init);
1706 module_exit(dlfb_module_exit);
1708 static void dlfb_urb_completion(struct urb *urb)
1710 struct urb_node *unode = urb->context;
1711 struct dlfb_data *dev = unode->dev;
1712 unsigned long flags;
1714 /* sync/async unlink faults aren't errors */
1716 if (!(urb->status == -ENOENT ||
1717 urb->status == -ECONNRESET ||
1718 urb->status == -ESHUTDOWN)) {
1719 dl_err("%s - nonzero write bulk status received: %d\n",
1720 __func__, urb->status);
1721 atomic_set(&dev->lost_pixels, 1);
1725 urb->transfer_buffer_length = dev->urbs.size; /* reset to actual */
1727 spin_lock_irqsave(&dev->urbs.lock, flags);
1728 list_add_tail(&unode->entry, &dev->urbs.list);
1729 dev->urbs.available++;
1730 spin_unlock_irqrestore(&dev->urbs.lock, flags);
1732 up(&dev->urbs.limit_sem);
1735 static void dlfb_free_urb_list(struct dlfb_data *dev)
1737 int count = dev->urbs.count;
1738 struct list_head *node;
1739 struct urb_node *unode;
1742 unsigned long flags;
1744 dl_notice("Waiting for completes and freeing all render urbs\n");
1746 /* keep waiting and freeing, until we've got 'em all */
1749 /* Getting interrupted means a leak, but ok at shutdown*/
1750 ret = down_interruptible(&dev->urbs.limit_sem);
1754 spin_lock_irqsave(&dev->urbs.lock, flags);
1756 node = dev->urbs.list.next; /* have reserved one with sem */
1757 list_del_init(node);
1759 spin_unlock_irqrestore(&dev->urbs.lock, flags);
1761 unode = list_entry(node, struct urb_node, entry);
1764 /* Free each separately allocated piece */
1765 usb_free_coherent(urb->dev, dev->urbs.size,
1766 urb->transfer_buffer, urb->transfer_dma);
1773 static int dlfb_alloc_urb_list(struct dlfb_data *dev, int count, size_t size)
1777 struct urb_node *unode;
1780 spin_lock_init(&dev->urbs.lock);
1782 dev->urbs.size = size;
1783 INIT_LIST_HEAD(&dev->urbs.list);
1786 unode = kzalloc(sizeof(struct urb_node), GFP_KERNEL);
1791 urb = usb_alloc_urb(0, GFP_KERNEL);
1798 buf = usb_alloc_coherent(dev->udev, MAX_TRANSFER, GFP_KERNEL,
1799 &urb->transfer_dma);
1806 /* urb->transfer_buffer_length set to actual before submit */
1807 usb_fill_bulk_urb(urb, dev->udev, usb_sndbulkpipe(dev->udev, 1),
1808 buf, size, dlfb_urb_completion, unode);
1809 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1811 list_add_tail(&unode->entry, &dev->urbs.list);
1816 sema_init(&dev->urbs.limit_sem, i);
1817 dev->urbs.count = i;
1818 dev->urbs.available = i;
1820 dl_notice("allocated %d %d byte urbs\n", i, (int) size);
1825 static struct urb *dlfb_get_urb(struct dlfb_data *dev)
1828 struct list_head *entry;
1829 struct urb_node *unode;
1830 struct urb *urb = NULL;
1831 unsigned long flags;
1833 /* Wait for an in-flight buffer to complete and get re-queued */
1834 ret = down_timeout(&dev->urbs.limit_sem, GET_URB_TIMEOUT);
1836 atomic_set(&dev->lost_pixels, 1);
1837 dl_err("wait for urb interrupted: %x\n", ret);
1841 spin_lock_irqsave(&dev->urbs.lock, flags);
1843 BUG_ON(list_empty(&dev->urbs.list)); /* reserved one with limit_sem */
1844 entry = dev->urbs.list.next;
1845 list_del_init(entry);
1846 dev->urbs.available--;
1848 spin_unlock_irqrestore(&dev->urbs.lock, flags);
1850 unode = list_entry(entry, struct urb_node, entry);
1857 static int dlfb_submit_urb(struct dlfb_data *dev, struct urb *urb, size_t len)
1861 BUG_ON(len > dev->urbs.size);
1863 urb->transfer_buffer_length = len; /* set to actual payload len */
1864 ret = usb_submit_urb(urb, GFP_KERNEL);
1866 dlfb_urb_completion(urb); /* because no one else will */
1867 atomic_set(&dev->lost_pixels, 1);
1868 dl_err("usb_submit_urb error %x\n", ret);
1873 MODULE_AUTHOR("Roberto De Ioris <roberto@unbit.it>, "
1874 "Jaya Kumar <jayakumar.lkml@gmail.com>, "
1875 "Bernie Thompson <bernie@plugable.com>");
1876 MODULE_DESCRIPTION("DisplayLink kernel framebuffer driver");
1877 MODULE_LICENSE("GPL");