Bernie Thompson | 59277b6 | 2009-11-24 15:52:21 -0800 | [diff] [blame] | 1 | /* |
| 2 | * udlfb.c -- Framebuffer driver for DisplayLink USB controller |
| 3 | * |
| 4 | * Copyright (C) 2009 Roberto De Ioris <roberto@unbit.it> |
| 5 | * Copyright (C) 2009 Jaya Kumar <jayakumar.lkml@gmail.com> |
Bernie Thompson | 2469d5d | 2010-02-15 06:46:13 -0800 | [diff] [blame] | 6 | * Copyright (C) 2009 Bernie Thompson <bernie@plugable.com> |
Bernie Thompson | 59277b6 | 2009-11-24 15:52:21 -0800 | [diff] [blame] | 7 | * |
| 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. |
| 11 | * |
| 12 | * Layout is based on skeletonfb by James Simmons and Geert Uytterhoeven, |
| 13 | * usb-skeleton by GregKH. |
| 14 | * |
| 15 | * Device-specific portions based on information from Displaylink, with work |
| 16 | * from Florian Echtler, Henrik Bjerregaard Pedersen, and others. |
| 17 | */ |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 18 | |
Paul Mundt | 81f6f3c | 2011-01-06 18:07:54 +0900 | [diff] [blame] | 19 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 20 | |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 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> |
Amit Kucheria | fb29900 | 2009-07-27 12:01:03 +0300 | [diff] [blame] | 28 | #include <linux/vmalloc.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 29 | #include <linux/slab.h> |
Linus Torvalds | 268bb0c | 2011-05-20 12:50:29 -0700 | [diff] [blame] | 30 | #include <linux/prefetch.h> |
Bernie Thompson | 33077b8 | 2010-09-05 16:35:19 -0700 | [diff] [blame] | 31 | #include <linux/delay.h> |
Paul Mundt | 96f8d864 | 2010-11-16 14:00:24 +0900 | [diff] [blame] | 32 | #include <video/udlfb.h> |
Paul Mundt | b9f03a3 | 2011-01-06 18:04:02 +0900 | [diff] [blame] | 33 | #include "edid.h" |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 34 | |
Bernie Thompson | 59277b6 | 2009-11-24 15:52:21 -0800 | [diff] [blame] | 35 | static struct fb_fix_screeninfo dlfb_fix = { |
Bernie Thompson | 2469d5d | 2010-02-15 06:46:13 -0800 | [diff] [blame] | 36 | .id = "udlfb", |
Bernie Thompson | 1d31a9e | 2010-02-15 06:45:43 -0800 | [diff] [blame] | 37 | .type = FB_TYPE_PACKED_PIXELS, |
| 38 | .visual = FB_VISUAL_TRUECOLOR, |
| 39 | .xpanstep = 0, |
| 40 | .ypanstep = 0, |
| 41 | .ywrapstep = 0, |
| 42 | .accel = FB_ACCEL_NONE, |
Bernie Thompson | 59277b6 | 2009-11-24 15:52:21 -0800 | [diff] [blame] | 43 | }; |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 44 | |
Bernie Thompson | 2469d5d | 2010-02-15 06:46:13 -0800 | [diff] [blame] | 45 | static const u32 udlfb_info_flags = FBINFO_DEFAULT | FBINFO_READS_FAST | |
Bernie Thompson | 2469d5d | 2010-02-15 06:46:13 -0800 | [diff] [blame] | 46 | FBINFO_VIRTFB | |
Bernie Thompson | 2469d5d | 2010-02-15 06:46:13 -0800 | [diff] [blame] | 47 | FBINFO_HWACCEL_IMAGEBLIT | FBINFO_HWACCEL_FILLRECT | |
| 48 | FBINFO_HWACCEL_COPYAREA | FBINFO_MISC_ALWAYS_SETPAR; |
| 49 | |
Bernie Thompson | cc403dc | 2010-02-15 06:45:49 -0800 | [diff] [blame] | 50 | /* |
Bernie Thompson | b63d101 | 2011-07-10 00:30:00 -0700 | [diff] [blame] | 51 | * There are many DisplayLink-based graphics products, all with unique PIDs. |
| 52 | * So we match on DisplayLink's VID + Vendor-Defined Interface Class (0xff) |
| 53 | * We also require a match on SubClass (0x00) and Protocol (0x00), |
| 54 | * which is compatible with all known USB 2.0 era graphics chips and firmware, |
| 55 | * but allows DisplayLink to increment those for any future incompatible chips |
Bernie Thompson | cc403dc | 2010-02-15 06:45:49 -0800 | [diff] [blame] | 56 | */ |
| 57 | static struct usb_device_id id_table[] = { |
Bernie Thompson | b63d101 | 2011-07-10 00:30:00 -0700 | [diff] [blame] | 58 | {.idVendor = 0x17e9, |
| 59 | .bInterfaceClass = 0xff, |
| 60 | .bInterfaceSubClass = 0x00, |
| 61 | .bInterfaceProtocol = 0x00, |
| 62 | .match_flags = USB_DEVICE_ID_MATCH_VENDOR | |
| 63 | USB_DEVICE_ID_MATCH_INT_CLASS | |
| 64 | USB_DEVICE_ID_MATCH_INT_SUBCLASS | |
| 65 | USB_DEVICE_ID_MATCH_INT_PROTOCOL, |
| 66 | }, |
Bernie Thompson | cc403dc | 2010-02-15 06:45:49 -0800 | [diff] [blame] | 67 | {}, |
| 68 | }; |
| 69 | MODULE_DEVICE_TABLE(usb, id_table); |
Bernie Thompson | 59277b6 | 2009-11-24 15:52:21 -0800 | [diff] [blame] | 70 | |
Bernie Thompson | d5ed543 | 2010-09-05 16:35:39 -0700 | [diff] [blame] | 71 | /* module options */ |
Rusty Russell | 90ab5ee | 2012-01-13 09:32:20 +1030 | [diff] [blame] | 72 | static bool console = 1; /* Allow fbcon to open framebuffer */ |
| 73 | static bool fb_defio = 1; /* Detect mmap writes using page faults */ |
| 74 | static bool shadow = 1; /* Optionally disable shadow framebuffer */ |
Ben Collins | 664c5f1 | 2012-03-03 12:57:37 -0800 | [diff] [blame] | 75 | static int pixel_limit; /* Optionally force a pixel resolution limit */ |
Bernie Thompson | dd8015f | 2010-02-15 06:46:35 -0800 | [diff] [blame] | 76 | |
Bernie Thompson | 4a4854d | 2010-02-15 06:45:55 -0800 | [diff] [blame] | 77 | /* dlfb keeps a list of urbs for efficient bulk transfers */ |
| 78 | static void dlfb_urb_completion(struct urb *urb); |
| 79 | static struct urb *dlfb_get_urb(struct dlfb_data *dev); |
| 80 | static int dlfb_submit_urb(struct dlfb_data *dev, struct urb * urb, size_t len); |
| 81 | static int dlfb_alloc_urb_list(struct dlfb_data *dev, int count, size_t size); |
| 82 | static void dlfb_free_urb_list(struct dlfb_data *dev); |
| 83 | |
Bernie Thompson | 59277b6 | 2009-11-24 15:52:21 -0800 | [diff] [blame] | 84 | /* |
Bernie Thompson | bd80816 | 2010-02-15 06:46:48 -0800 | [diff] [blame] | 85 | * All DisplayLink bulk operations start with 0xAF, followed by specific code |
| 86 | * All operations are written to buffers which then later get sent to device |
Bernie Thompson | 59277b6 | 2009-11-24 15:52:21 -0800 | [diff] [blame] | 87 | */ |
Bernie Thompson | 4574203 | 2010-02-15 06:46:04 -0800 | [diff] [blame] | 88 | static char *dlfb_set_register(char *buf, u8 reg, u8 val) |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 89 | { |
Bernie Thompson | 1d31a9e | 2010-02-15 06:45:43 -0800 | [diff] [blame] | 90 | *buf++ = 0xAF; |
| 91 | *buf++ = 0x20; |
| 92 | *buf++ = reg; |
| 93 | *buf++ = val; |
| 94 | return buf; |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Bernie Thompson | 4574203 | 2010-02-15 06:46:04 -0800 | [diff] [blame] | 97 | static char *dlfb_vidreg_lock(char *buf) |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 98 | { |
Bernie Thompson | 4574203 | 2010-02-15 06:46:04 -0800 | [diff] [blame] | 99 | return dlfb_set_register(buf, 0xFF, 0x00); |
Bernie Thompson | 59277b6 | 2009-11-24 15:52:21 -0800 | [diff] [blame] | 100 | } |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 101 | |
Bernie Thompson | 4574203 | 2010-02-15 06:46:04 -0800 | [diff] [blame] | 102 | static char *dlfb_vidreg_unlock(char *buf) |
Bernie Thompson | 59277b6 | 2009-11-24 15:52:21 -0800 | [diff] [blame] | 103 | { |
Bernie Thompson | 4574203 | 2010-02-15 06:46:04 -0800 | [diff] [blame] | 104 | return dlfb_set_register(buf, 0xFF, 0xFF); |
Bernie Thompson | 59277b6 | 2009-11-24 15:52:21 -0800 | [diff] [blame] | 105 | } |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 106 | |
Bernie Thompson | 59277b6 | 2009-11-24 15:52:21 -0800 | [diff] [blame] | 107 | /* |
Bernie Thompson | 58e7c3b | 2011-08-21 13:34:11 -0700 | [diff] [blame] | 108 | * Map FB_BLANK_* to DisplayLink register |
| 109 | * DLReg FB_BLANK_* |
| 110 | * ----- ----------------------------- |
| 111 | * 0x00 FB_BLANK_UNBLANK (0) |
| 112 | * 0x01 FB_BLANK (1) |
| 113 | * 0x03 FB_BLANK_VSYNC_SUSPEND (2) |
| 114 | * 0x05 FB_BLANK_HSYNC_SUSPEND (3) |
| 115 | * 0x07 FB_BLANK_POWERDOWN (4) Note: requires modeset to come back |
Bernie Thompson | 59277b6 | 2009-11-24 15:52:21 -0800 | [diff] [blame] | 116 | */ |
Bernie Thompson | 58e7c3b | 2011-08-21 13:34:11 -0700 | [diff] [blame] | 117 | static char *dlfb_blanking(char *buf, int fb_blank) |
Bernie Thompson | 59277b6 | 2009-11-24 15:52:21 -0800 | [diff] [blame] | 118 | { |
Bernie Thompson | 58e7c3b | 2011-08-21 13:34:11 -0700 | [diff] [blame] | 119 | u8 reg; |
| 120 | |
| 121 | switch (fb_blank) { |
| 122 | case FB_BLANK_POWERDOWN: |
| 123 | reg = 0x07; |
| 124 | break; |
| 125 | case FB_BLANK_HSYNC_SUSPEND: |
| 126 | reg = 0x05; |
| 127 | break; |
| 128 | case FB_BLANK_VSYNC_SUSPEND: |
| 129 | reg = 0x03; |
| 130 | break; |
| 131 | case FB_BLANK_NORMAL: |
| 132 | reg = 0x01; |
| 133 | break; |
| 134 | default: |
| 135 | reg = 0x00; |
| 136 | } |
| 137 | |
| 138 | buf = dlfb_set_register(buf, 0x1F, reg); |
| 139 | |
| 140 | return buf; |
Bernie Thompson | 59277b6 | 2009-11-24 15:52:21 -0800 | [diff] [blame] | 141 | } |
| 142 | |
Bernie Thompson | 4574203 | 2010-02-15 06:46:04 -0800 | [diff] [blame] | 143 | static char *dlfb_set_color_depth(char *buf, u8 selection) |
Bernie Thompson | 59277b6 | 2009-11-24 15:52:21 -0800 | [diff] [blame] | 144 | { |
Bernie Thompson | 4574203 | 2010-02-15 06:46:04 -0800 | [diff] [blame] | 145 | return dlfb_set_register(buf, 0x00, selection); |
Bernie Thompson | 59277b6 | 2009-11-24 15:52:21 -0800 | [diff] [blame] | 146 | } |
| 147 | |
Bernie Thompson | 4574203 | 2010-02-15 06:46:04 -0800 | [diff] [blame] | 148 | static char *dlfb_set_base16bpp(char *wrptr, u32 base) |
Bernie Thompson | 59277b6 | 2009-11-24 15:52:21 -0800 | [diff] [blame] | 149 | { |
Bernie Thompson | 1d31a9e | 2010-02-15 06:45:43 -0800 | [diff] [blame] | 150 | /* the base pointer is 16 bits wide, 0x20 is hi byte. */ |
Bernie Thompson | 4574203 | 2010-02-15 06:46:04 -0800 | [diff] [blame] | 151 | wrptr = dlfb_set_register(wrptr, 0x20, base >> 16); |
| 152 | wrptr = dlfb_set_register(wrptr, 0x21, base >> 8); |
| 153 | return dlfb_set_register(wrptr, 0x22, base); |
Bernie Thompson | 59277b6 | 2009-11-24 15:52:21 -0800 | [diff] [blame] | 154 | } |
| 155 | |
Bernie Thompson | bd80816 | 2010-02-15 06:46:48 -0800 | [diff] [blame] | 156 | /* |
| 157 | * DisplayLink HW has separate 16bpp and 8bpp framebuffers. |
| 158 | * In 24bpp modes, the low 323 RGB bits go in the 8bpp framebuffer |
| 159 | */ |
Bernie Thompson | 4574203 | 2010-02-15 06:46:04 -0800 | [diff] [blame] | 160 | static char *dlfb_set_base8bpp(char *wrptr, u32 base) |
Bernie Thompson | 59277b6 | 2009-11-24 15:52:21 -0800 | [diff] [blame] | 161 | { |
Bernie Thompson | 4574203 | 2010-02-15 06:46:04 -0800 | [diff] [blame] | 162 | wrptr = dlfb_set_register(wrptr, 0x26, base >> 16); |
| 163 | wrptr = dlfb_set_register(wrptr, 0x27, base >> 8); |
| 164 | return dlfb_set_register(wrptr, 0x28, base); |
Bernie Thompson | 59277b6 | 2009-11-24 15:52:21 -0800 | [diff] [blame] | 165 | } |
| 166 | |
Bernie Thompson | 4574203 | 2010-02-15 06:46:04 -0800 | [diff] [blame] | 167 | static char *dlfb_set_register_16(char *wrptr, u8 reg, u16 value) |
Bernie Thompson | 59277b6 | 2009-11-24 15:52:21 -0800 | [diff] [blame] | 168 | { |
Bernie Thompson | 4574203 | 2010-02-15 06:46:04 -0800 | [diff] [blame] | 169 | wrptr = dlfb_set_register(wrptr, reg, value >> 8); |
| 170 | return dlfb_set_register(wrptr, reg+1, value); |
Bernie Thompson | 59277b6 | 2009-11-24 15:52:21 -0800 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | /* |
| 174 | * This is kind of weird because the controller takes some |
| 175 | * register values in a different byte order than other registers. |
| 176 | */ |
Bernie Thompson | 4574203 | 2010-02-15 06:46:04 -0800 | [diff] [blame] | 177 | static char *dlfb_set_register_16be(char *wrptr, u8 reg, u16 value) |
Bernie Thompson | 59277b6 | 2009-11-24 15:52:21 -0800 | [diff] [blame] | 178 | { |
Bernie Thompson | 4574203 | 2010-02-15 06:46:04 -0800 | [diff] [blame] | 179 | wrptr = dlfb_set_register(wrptr, reg, value); |
| 180 | return dlfb_set_register(wrptr, reg+1, value >> 8); |
Bernie Thompson | 59277b6 | 2009-11-24 15:52:21 -0800 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | /* |
| 184 | * LFSR is linear feedback shift register. The reason we have this is |
| 185 | * because the display controller needs to minimize the clock depth of |
| 186 | * various counters used in the display path. So this code reverses the |
| 187 | * provided value into the lfsr16 value by counting backwards to get |
| 188 | * the value that needs to be set in the hardware comparator to get the |
| 189 | * same actual count. This makes sense once you read above a couple of |
| 190 | * times and think about it from a hardware perspective. |
| 191 | */ |
Bernie Thompson | bd80816 | 2010-02-15 06:46:48 -0800 | [diff] [blame] | 192 | static u16 dlfb_lfsr16(u16 actual_count) |
Bernie Thompson | 59277b6 | 2009-11-24 15:52:21 -0800 | [diff] [blame] | 193 | { |
| 194 | u32 lv = 0xFFFF; /* This is the lfsr value that the hw starts with */ |
| 195 | |
| 196 | while (actual_count--) { |
| 197 | lv = ((lv << 1) | |
| 198 | (((lv >> 15) ^ (lv >> 4) ^ (lv >> 2) ^ (lv >> 1)) & 1)) |
| 199 | & 0xFFFF; |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 200 | } |
Bernie Thompson | 59277b6 | 2009-11-24 15:52:21 -0800 | [diff] [blame] | 201 | |
| 202 | return (u16) lv; |
| 203 | } |
| 204 | |
| 205 | /* |
| 206 | * This does LFSR conversion on the value that is to be written. |
| 207 | * See LFSR explanation above for more detail. |
| 208 | */ |
Bernie Thompson | 4574203 | 2010-02-15 06:46:04 -0800 | [diff] [blame] | 209 | static char *dlfb_set_register_lfsr16(char *wrptr, u8 reg, u16 value) |
Bernie Thompson | 59277b6 | 2009-11-24 15:52:21 -0800 | [diff] [blame] | 210 | { |
Bernie Thompson | bd80816 | 2010-02-15 06:46:48 -0800 | [diff] [blame] | 211 | return dlfb_set_register_16(wrptr, reg, dlfb_lfsr16(value)); |
Bernie Thompson | 59277b6 | 2009-11-24 15:52:21 -0800 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | /* |
| 215 | * This takes a standard fbdev screeninfo struct and all of its monitor mode |
| 216 | * details and converts them into the DisplayLink equivalent register commands. |
| 217 | */ |
Bernie Thompson | 4574203 | 2010-02-15 06:46:04 -0800 | [diff] [blame] | 218 | static char *dlfb_set_vid_cmds(char *wrptr, struct fb_var_screeninfo *var) |
Bernie Thompson | 59277b6 | 2009-11-24 15:52:21 -0800 | [diff] [blame] | 219 | { |
| 220 | u16 xds, yds; |
| 221 | u16 xde, yde; |
| 222 | u16 yec; |
| 223 | |
Bernie Thompson | 59277b6 | 2009-11-24 15:52:21 -0800 | [diff] [blame] | 224 | /* x display start */ |
| 225 | xds = var->left_margin + var->hsync_len; |
Bernie Thompson | 4574203 | 2010-02-15 06:46:04 -0800 | [diff] [blame] | 226 | wrptr = dlfb_set_register_lfsr16(wrptr, 0x01, xds); |
Bernie Thompson | 59277b6 | 2009-11-24 15:52:21 -0800 | [diff] [blame] | 227 | /* x display end */ |
| 228 | xde = xds + var->xres; |
Bernie Thompson | 4574203 | 2010-02-15 06:46:04 -0800 | [diff] [blame] | 229 | wrptr = dlfb_set_register_lfsr16(wrptr, 0x03, xde); |
Bernie Thompson | 59277b6 | 2009-11-24 15:52:21 -0800 | [diff] [blame] | 230 | |
| 231 | /* y display start */ |
| 232 | yds = var->upper_margin + var->vsync_len; |
Bernie Thompson | 4574203 | 2010-02-15 06:46:04 -0800 | [diff] [blame] | 233 | wrptr = dlfb_set_register_lfsr16(wrptr, 0x05, yds); |
Bernie Thompson | 59277b6 | 2009-11-24 15:52:21 -0800 | [diff] [blame] | 234 | /* y display end */ |
| 235 | yde = yds + var->yres; |
Bernie Thompson | 4574203 | 2010-02-15 06:46:04 -0800 | [diff] [blame] | 236 | wrptr = dlfb_set_register_lfsr16(wrptr, 0x07, yde); |
Bernie Thompson | 59277b6 | 2009-11-24 15:52:21 -0800 | [diff] [blame] | 237 | |
| 238 | /* x end count is active + blanking - 1 */ |
Bernie Thompson | 4574203 | 2010-02-15 06:46:04 -0800 | [diff] [blame] | 239 | wrptr = dlfb_set_register_lfsr16(wrptr, 0x09, |
| 240 | xde + var->right_margin - 1); |
Bernie Thompson | 59277b6 | 2009-11-24 15:52:21 -0800 | [diff] [blame] | 241 | |
| 242 | /* libdlo hardcodes hsync start to 1 */ |
Bernie Thompson | 4574203 | 2010-02-15 06:46:04 -0800 | [diff] [blame] | 243 | wrptr = dlfb_set_register_lfsr16(wrptr, 0x0B, 1); |
Bernie Thompson | 59277b6 | 2009-11-24 15:52:21 -0800 | [diff] [blame] | 244 | |
| 245 | /* hsync end is width of sync pulse + 1 */ |
Bernie Thompson | 4574203 | 2010-02-15 06:46:04 -0800 | [diff] [blame] | 246 | wrptr = dlfb_set_register_lfsr16(wrptr, 0x0D, var->hsync_len + 1); |
Bernie Thompson | 59277b6 | 2009-11-24 15:52:21 -0800 | [diff] [blame] | 247 | |
| 248 | /* hpixels is active pixels */ |
Bernie Thompson | 4574203 | 2010-02-15 06:46:04 -0800 | [diff] [blame] | 249 | wrptr = dlfb_set_register_16(wrptr, 0x0F, var->xres); |
Bernie Thompson | 59277b6 | 2009-11-24 15:52:21 -0800 | [diff] [blame] | 250 | |
| 251 | /* yendcount is vertical active + vertical blanking */ |
| 252 | yec = var->yres + var->upper_margin + var->lower_margin + |
| 253 | var->vsync_len; |
Bernie Thompson | 4574203 | 2010-02-15 06:46:04 -0800 | [diff] [blame] | 254 | wrptr = dlfb_set_register_lfsr16(wrptr, 0x11, yec); |
Bernie Thompson | 59277b6 | 2009-11-24 15:52:21 -0800 | [diff] [blame] | 255 | |
| 256 | /* libdlo hardcodes vsync start to 0 */ |
Bernie Thompson | 4574203 | 2010-02-15 06:46:04 -0800 | [diff] [blame] | 257 | wrptr = dlfb_set_register_lfsr16(wrptr, 0x13, 0); |
Bernie Thompson | 59277b6 | 2009-11-24 15:52:21 -0800 | [diff] [blame] | 258 | |
| 259 | /* vsync end is width of vsync pulse */ |
Bernie Thompson | 4574203 | 2010-02-15 06:46:04 -0800 | [diff] [blame] | 260 | wrptr = dlfb_set_register_lfsr16(wrptr, 0x15, var->vsync_len); |
Bernie Thompson | 59277b6 | 2009-11-24 15:52:21 -0800 | [diff] [blame] | 261 | |
| 262 | /* vpixels is active pixels */ |
Bernie Thompson | 4574203 | 2010-02-15 06:46:04 -0800 | [diff] [blame] | 263 | wrptr = dlfb_set_register_16(wrptr, 0x17, var->yres); |
Bernie Thompson | 59277b6 | 2009-11-24 15:52:21 -0800 | [diff] [blame] | 264 | |
| 265 | /* convert picoseconds to 5kHz multiple for pclk5k = x * 1E12/5k */ |
Bernie Thompson | 4574203 | 2010-02-15 06:46:04 -0800 | [diff] [blame] | 266 | wrptr = dlfb_set_register_16be(wrptr, 0x1B, |
| 267 | 200*1000*1000/var->pixclock); |
Bernie Thompson | 59277b6 | 2009-11-24 15:52:21 -0800 | [diff] [blame] | 268 | |
| 269 | return wrptr; |
| 270 | } |
| 271 | |
| 272 | /* |
| 273 | * This takes a standard fbdev screeninfo struct that was fetched or prepared |
| 274 | * and then generates the appropriate command sequence that then drives the |
| 275 | * display controller. |
| 276 | */ |
| 277 | static int dlfb_set_video_mode(struct dlfb_data *dev, |
| 278 | struct fb_var_screeninfo *var) |
| 279 | { |
| 280 | char *buf; |
| 281 | char *wrptr; |
| 282 | int retval = 0; |
| 283 | int writesize; |
Bernie Thompson | 530f43a | 2010-02-15 06:46:21 -0800 | [diff] [blame] | 284 | struct urb *urb; |
Bernie Thompson | 59277b6 | 2009-11-24 15:52:21 -0800 | [diff] [blame] | 285 | |
Bernie Thompson | 530f43a | 2010-02-15 06:46:21 -0800 | [diff] [blame] | 286 | if (!atomic_read(&dev->usb_active)) |
| 287 | return -EPERM; |
| 288 | |
| 289 | urb = dlfb_get_urb(dev); |
| 290 | if (!urb) |
| 291 | return -ENOMEM; |
Bernie Thompson | 2685cff | 2010-09-05 18:29:56 -0700 | [diff] [blame] | 292 | |
Bernie Thompson | 530f43a | 2010-02-15 06:46:21 -0800 | [diff] [blame] | 293 | buf = (char *) urb->transfer_buffer; |
Bernie Thompson | 59277b6 | 2009-11-24 15:52:21 -0800 | [diff] [blame] | 294 | |
| 295 | /* |
| 296 | * This first section has to do with setting the base address on the |
| 297 | * controller * associated with the display. There are 2 base |
| 298 | * pointers, currently, we only * use the 16 bpp segment. |
| 299 | */ |
Bernie Thompson | 4574203 | 2010-02-15 06:46:04 -0800 | [diff] [blame] | 300 | wrptr = dlfb_vidreg_lock(buf); |
| 301 | wrptr = dlfb_set_color_depth(wrptr, 0x00); |
Bernie Thompson | 59277b6 | 2009-11-24 15:52:21 -0800 | [diff] [blame] | 302 | /* set base for 16bpp segment to 0 */ |
Bernie Thompson | 4574203 | 2010-02-15 06:46:04 -0800 | [diff] [blame] | 303 | wrptr = dlfb_set_base16bpp(wrptr, 0); |
Bernie Thompson | 59277b6 | 2009-11-24 15:52:21 -0800 | [diff] [blame] | 304 | /* set base for 8bpp segment to end of fb */ |
Bernie Thompson | 4574203 | 2010-02-15 06:46:04 -0800 | [diff] [blame] | 305 | wrptr = dlfb_set_base8bpp(wrptr, dev->info->fix.smem_len); |
Bernie Thompson | 59277b6 | 2009-11-24 15:52:21 -0800 | [diff] [blame] | 306 | |
Bernie Thompson | 4574203 | 2010-02-15 06:46:04 -0800 | [diff] [blame] | 307 | wrptr = dlfb_set_vid_cmds(wrptr, var); |
Bernie Thompson | 58e7c3b | 2011-08-21 13:34:11 -0700 | [diff] [blame] | 308 | wrptr = dlfb_blanking(wrptr, FB_BLANK_UNBLANK); |
Bernie Thompson | 4574203 | 2010-02-15 06:46:04 -0800 | [diff] [blame] | 309 | wrptr = dlfb_vidreg_unlock(wrptr); |
Bernie Thompson | 59277b6 | 2009-11-24 15:52:21 -0800 | [diff] [blame] | 310 | |
| 311 | writesize = wrptr - buf; |
| 312 | |
Bernie Thompson | 530f43a | 2010-02-15 06:46:21 -0800 | [diff] [blame] | 313 | retval = dlfb_submit_urb(dev, urb, writesize); |
Bernie Thompson | 59277b6 | 2009-11-24 15:52:21 -0800 | [diff] [blame] | 314 | |
Bernie Thompson | 58e7c3b | 2011-08-21 13:34:11 -0700 | [diff] [blame] | 315 | dev->blank_mode = FB_BLANK_UNBLANK; |
| 316 | |
Bernie Thompson | 59277b6 | 2009-11-24 15:52:21 -0800 | [diff] [blame] | 317 | return retval; |
| 318 | } |
| 319 | |
Bernie Thompson | 4574203 | 2010-02-15 06:46:04 -0800 | [diff] [blame] | 320 | static int dlfb_ops_mmap(struct fb_info *info, struct vm_area_struct *vma) |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 321 | { |
| 322 | unsigned long start = vma->vm_start; |
| 323 | unsigned long size = vma->vm_end - vma->vm_start; |
| 324 | unsigned long offset = vma->vm_pgoff << PAGE_SHIFT; |
| 325 | unsigned long page, pos; |
| 326 | |
Greg Kroah-Hartman | f05e057 | 2009-06-03 14:47:08 -0700 | [diff] [blame] | 327 | if (offset + size > info->fix.smem_len) |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 328 | return -EINVAL; |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 329 | |
| 330 | pos = (unsigned long)info->fix.smem_start + offset; |
| 331 | |
Paul Mundt | 81f6f3c | 2011-01-06 18:07:54 +0900 | [diff] [blame] | 332 | pr_notice("mmap() framebuffer addr:%lu size:%lu\n", |
Bernie Thompson | 2685cff | 2010-09-05 18:29:56 -0700 | [diff] [blame] | 333 | pos, size); |
| 334 | |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 335 | while (size > 0) { |
| 336 | page = vmalloc_to_pfn((void *)pos); |
Greg Kroah-Hartman | f05e057 | 2009-06-03 14:47:08 -0700 | [diff] [blame] | 337 | if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED)) |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 338 | return -EAGAIN; |
Greg Kroah-Hartman | f05e057 | 2009-06-03 14:47:08 -0700 | [diff] [blame] | 339 | |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 340 | start += PAGE_SIZE; |
| 341 | pos += PAGE_SIZE; |
| 342 | if (size > PAGE_SIZE) |
| 343 | size -= PAGE_SIZE; |
| 344 | else |
| 345 | size = 0; |
| 346 | } |
| 347 | |
| 348 | vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */ |
| 349 | return 0; |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 350 | } |
| 351 | |
Bernie Thompson | 530f43a | 2010-02-15 06:46:21 -0800 | [diff] [blame] | 352 | /* |
| 353 | * Trims identical data from front and back of line |
| 354 | * Sets new front buffer address and width |
| 355 | * And returns byte count of identical pixels |
| 356 | * Assumes CPU natural alignment (unsigned long) |
| 357 | * for back and front buffer ptrs and width |
| 358 | */ |
| 359 | static int dlfb_trim_hline(const u8 *bback, const u8 **bfront, int *width_bytes) |
Roberto De Ioris | 7316bc5 | 2009-06-10 23:02:19 -0700 | [diff] [blame] | 360 | { |
Bernie Thompson | 530f43a | 2010-02-15 06:46:21 -0800 | [diff] [blame] | 361 | int j, k; |
| 362 | const unsigned long *back = (const unsigned long *) bback; |
| 363 | const unsigned long *front = (const unsigned long *) *bfront; |
| 364 | const int width = *width_bytes / sizeof(unsigned long); |
| 365 | int identical = width; |
| 366 | int start = width; |
| 367 | int end = width; |
Roberto De Ioris | 7316bc5 | 2009-06-10 23:02:19 -0700 | [diff] [blame] | 368 | |
Bernie Thompson | 530f43a | 2010-02-15 06:46:21 -0800 | [diff] [blame] | 369 | prefetch((void *) front); |
| 370 | prefetch((void *) back); |
Roberto De Ioris | 7316bc5 | 2009-06-10 23:02:19 -0700 | [diff] [blame] | 371 | |
Bernie Thompson | 530f43a | 2010-02-15 06:46:21 -0800 | [diff] [blame] | 372 | for (j = 0; j < width; j++) { |
| 373 | if (back[j] != front[j]) { |
| 374 | start = j; |
| 375 | break; |
| 376 | } |
Roberto De Ioris | 7316bc5 | 2009-06-10 23:02:19 -0700 | [diff] [blame] | 377 | } |
| 378 | |
Bernie Thompson | 530f43a | 2010-02-15 06:46:21 -0800 | [diff] [blame] | 379 | for (k = width - 1; k > j; k--) { |
| 380 | if (back[k] != front[k]) { |
| 381 | end = k+1; |
| 382 | break; |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | identical = start + (width - end); |
| 387 | *bfront = (u8 *) &front[start]; |
| 388 | *width_bytes = (end - start) * sizeof(unsigned long); |
| 389 | |
| 390 | return identical * sizeof(unsigned long); |
Roberto De Ioris | 7316bc5 | 2009-06-10 23:02:19 -0700 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | /* |
Pavel Machek | 3b7b31f | 2010-04-03 07:00:37 +0200 | [diff] [blame] | 394 | * Render a command stream for an encoded horizontal line segment of pixels. |
| 395 | * |
| 396 | * A command buffer holds several commands. |
| 397 | * It always begins with a fresh command header |
| 398 | * (the protocol doesn't require this, but we enforce it to allow |
| 399 | * multiple buffers to be potentially encoded and sent in parallel). |
| 400 | * A single command encodes one contiguous horizontal line of pixels |
| 401 | * |
| 402 | * The function relies on the client to do all allocation, so that |
| 403 | * rendering can be done directly to output buffers (e.g. USB URBs). |
| 404 | * The function fills the supplied command buffer, providing information |
| 405 | * on where it left off, so the client may call in again with additional |
| 406 | * buffers if the line will take several buffers to complete. |
| 407 | * |
| 408 | * A single command can transmit a maximum of 256 pixels, |
| 409 | * regardless of the compression ratio (protocol design limit). |
| 410 | * To the hardware, 0 for a size byte means 256 |
Bernie Thompson | 2685cff | 2010-09-05 18:29:56 -0700 | [diff] [blame] | 411 | * |
Pavel Machek | 3b7b31f | 2010-04-03 07:00:37 +0200 | [diff] [blame] | 412 | * Rather than 256 pixel commands which are either rl or raw encoded, |
| 413 | * the rlx command simply assumes alternating raw and rl spans within one cmd. |
| 414 | * This has a slightly larger header overhead, but produces more even results. |
| 415 | * It also processes all data (read and write) in a single pass. |
| 416 | * Performance benchmarks of common cases show it having just slightly better |
Bernie Thompson | 2685cff | 2010-09-05 18:29:56 -0700 | [diff] [blame] | 417 | * compression than 256 pixel raw or rle commands, with similar CPU consumpion. |
Pavel Machek | 3b7b31f | 2010-04-03 07:00:37 +0200 | [diff] [blame] | 418 | * But for very rl friendly data, will compress not quite as well. |
| 419 | */ |
Bernie Thompson | 530f43a | 2010-02-15 06:46:21 -0800 | [diff] [blame] | 420 | static void dlfb_compress_hline( |
| 421 | const uint16_t **pixel_start_ptr, |
| 422 | const uint16_t *const pixel_end, |
| 423 | uint32_t *device_address_ptr, |
| 424 | uint8_t **command_buffer_ptr, |
| 425 | const uint8_t *const cmd_buffer_end) |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 426 | { |
Bernie Thompson | 530f43a | 2010-02-15 06:46:21 -0800 | [diff] [blame] | 427 | const uint16_t *pixel = *pixel_start_ptr; |
| 428 | uint32_t dev_addr = *device_address_ptr; |
| 429 | uint8_t *cmd = *command_buffer_ptr; |
| 430 | const int bpp = 2; |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 431 | |
Bernie Thompson | 530f43a | 2010-02-15 06:46:21 -0800 | [diff] [blame] | 432 | while ((pixel_end > pixel) && |
| 433 | (cmd_buffer_end - MIN_RLX_CMD_BYTES > cmd)) { |
| 434 | uint8_t *raw_pixels_count_byte = 0; |
| 435 | uint8_t *cmd_pixels_count_byte = 0; |
| 436 | const uint16_t *raw_pixel_start = 0; |
| 437 | const uint16_t *cmd_pixel_start, *cmd_pixel_end = 0; |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 438 | |
Bernie Thompson | 530f43a | 2010-02-15 06:46:21 -0800 | [diff] [blame] | 439 | prefetchw((void *) cmd); /* pull in one cache line at least */ |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 440 | |
Bernie Thompson | 530f43a | 2010-02-15 06:46:21 -0800 | [diff] [blame] | 441 | *cmd++ = 0xAF; |
| 442 | *cmd++ = 0x6B; |
Bernie Thompson | 1572f91 | 2010-09-05 16:35:27 -0700 | [diff] [blame] | 443 | *cmd++ = (uint8_t) ((dev_addr >> 16) & 0xFF); |
| 444 | *cmd++ = (uint8_t) ((dev_addr >> 8) & 0xFF); |
| 445 | *cmd++ = (uint8_t) ((dev_addr) & 0xFF); |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 446 | |
Bernie Thompson | 530f43a | 2010-02-15 06:46:21 -0800 | [diff] [blame] | 447 | cmd_pixels_count_byte = cmd++; /* we'll know this later */ |
| 448 | cmd_pixel_start = pixel; |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 449 | |
Bernie Thompson | 530f43a | 2010-02-15 06:46:21 -0800 | [diff] [blame] | 450 | raw_pixels_count_byte = cmd++; /* we'll know this later */ |
| 451 | raw_pixel_start = pixel; |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 452 | |
Bernie Thompson | 530f43a | 2010-02-15 06:46:21 -0800 | [diff] [blame] | 453 | cmd_pixel_end = pixel + min(MAX_CMD_PIXELS + 1, |
| 454 | min((int)(pixel_end - pixel), |
| 455 | (int)(cmd_buffer_end - cmd) / bpp)); |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 456 | |
Bernie Thompson | 530f43a | 2010-02-15 06:46:21 -0800 | [diff] [blame] | 457 | prefetch_range((void *) pixel, (cmd_pixel_end - pixel) * bpp); |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 458 | |
Bernie Thompson | 530f43a | 2010-02-15 06:46:21 -0800 | [diff] [blame] | 459 | while (pixel < cmd_pixel_end) { |
| 460 | const uint16_t * const repeating_pixel = pixel; |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 461 | |
Bernie Thompson | 530f43a | 2010-02-15 06:46:21 -0800 | [diff] [blame] | 462 | *(uint16_t *)cmd = cpu_to_be16p(pixel); |
| 463 | cmd += 2; |
| 464 | pixel++; |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 465 | |
Bernie Thompson | 530f43a | 2010-02-15 06:46:21 -0800 | [diff] [blame] | 466 | if (unlikely((pixel < cmd_pixel_end) && |
| 467 | (*pixel == *repeating_pixel))) { |
| 468 | /* go back and fill in raw pixel count */ |
| 469 | *raw_pixels_count_byte = ((repeating_pixel - |
| 470 | raw_pixel_start) + 1) & 0xFF; |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 471 | |
Bernie Thompson | 530f43a | 2010-02-15 06:46:21 -0800 | [diff] [blame] | 472 | while ((pixel < cmd_pixel_end) |
| 473 | && (*pixel == *repeating_pixel)) { |
| 474 | pixel++; |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 475 | } |
Bernie Thompson | 59277b6 | 2009-11-24 15:52:21 -0800 | [diff] [blame] | 476 | |
Bernie Thompson | 530f43a | 2010-02-15 06:46:21 -0800 | [diff] [blame] | 477 | /* immediately after raw data is repeat byte */ |
| 478 | *cmd++ = ((pixel - repeating_pixel) - 1) & 0xFF; |
Bernie Thompson | 59277b6 | 2009-11-24 15:52:21 -0800 | [diff] [blame] | 479 | |
Bernie Thompson | 530f43a | 2010-02-15 06:46:21 -0800 | [diff] [blame] | 480 | /* Then start another raw pixel span */ |
| 481 | raw_pixel_start = pixel; |
| 482 | raw_pixels_count_byte = cmd++; |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 483 | } |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 484 | } |
| 485 | |
Bernie Thompson | 530f43a | 2010-02-15 06:46:21 -0800 | [diff] [blame] | 486 | if (pixel > raw_pixel_start) { |
| 487 | /* finalize last RAW span */ |
| 488 | *raw_pixels_count_byte = (pixel-raw_pixel_start) & 0xFF; |
| 489 | } |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 490 | |
Bernie Thompson | 530f43a | 2010-02-15 06:46:21 -0800 | [diff] [blame] | 491 | *cmd_pixels_count_byte = (pixel - cmd_pixel_start) & 0xFF; |
| 492 | dev_addr += (pixel - cmd_pixel_start) * bpp; |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 493 | } |
| 494 | |
Bernie Thompson | 530f43a | 2010-02-15 06:46:21 -0800 | [diff] [blame] | 495 | if (cmd_buffer_end <= MIN_RLX_CMD_BYTES + cmd) { |
| 496 | /* Fill leftover bytes with no-ops */ |
| 497 | if (cmd_buffer_end > cmd) |
| 498 | memset(cmd, 0xAF, cmd_buffer_end - cmd); |
| 499 | cmd = (uint8_t *) cmd_buffer_end; |
Roberto De Ioris | 7316bc5 | 2009-06-10 23:02:19 -0700 | [diff] [blame] | 500 | } |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 501 | |
Bernie Thompson | 530f43a | 2010-02-15 06:46:21 -0800 | [diff] [blame] | 502 | *command_buffer_ptr = cmd; |
| 503 | *pixel_start_ptr = pixel; |
| 504 | *device_address_ptr = dev_addr; |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 505 | |
Bernie Thompson | 530f43a | 2010-02-15 06:46:21 -0800 | [diff] [blame] | 506 | return; |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 507 | } |
| 508 | |
Bernie Thompson | 530f43a | 2010-02-15 06:46:21 -0800 | [diff] [blame] | 509 | /* |
| 510 | * There are 3 copies of every pixel: The front buffer that the fbdev |
| 511 | * client renders to, the actual framebuffer across the USB bus in hardware |
| 512 | * (that we can only write to, slowly, and can never read), and (optionally) |
| 513 | * our shadow copy that tracks what's been sent to that hardware buffer. |
| 514 | */ |
Bernie Thompson | 5bea1fb | 2010-09-05 18:28:29 -0700 | [diff] [blame] | 515 | static int dlfb_render_hline(struct dlfb_data *dev, struct urb **urb_ptr, |
Bernie Thompson | 530f43a | 2010-02-15 06:46:21 -0800 | [diff] [blame] | 516 | const char *front, char **urb_buf_ptr, |
| 517 | u32 byte_offset, u32 byte_width, |
| 518 | int *ident_ptr, int *sent_ptr) |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 519 | { |
Bernie Thompson | 530f43a | 2010-02-15 06:46:21 -0800 | [diff] [blame] | 520 | const u8 *line_start, *line_end, *next_pixel; |
| 521 | u32 dev_addr = dev->base16 + byte_offset; |
| 522 | struct urb *urb = *urb_ptr; |
| 523 | u8 *cmd = *urb_buf_ptr; |
| 524 | u8 *cmd_end = (u8 *) urb->transfer_buffer + urb->transfer_buffer_length; |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 525 | |
Bernie Thompson | 530f43a | 2010-02-15 06:46:21 -0800 | [diff] [blame] | 526 | line_start = (u8 *) (front + byte_offset); |
| 527 | next_pixel = line_start; |
| 528 | line_end = next_pixel + byte_width; |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 529 | |
Bernie Thompson | 530f43a | 2010-02-15 06:46:21 -0800 | [diff] [blame] | 530 | if (dev->backing_buffer) { |
| 531 | int offset; |
| 532 | const u8 *back_start = (u8 *) (dev->backing_buffer |
| 533 | + byte_offset); |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 534 | |
Bernie Thompson | 530f43a | 2010-02-15 06:46:21 -0800 | [diff] [blame] | 535 | *ident_ptr += dlfb_trim_hline(back_start, &next_pixel, |
| 536 | &byte_width); |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 537 | |
Bernie Thompson | 530f43a | 2010-02-15 06:46:21 -0800 | [diff] [blame] | 538 | offset = next_pixel - line_start; |
| 539 | line_end = next_pixel + byte_width; |
| 540 | dev_addr += offset; |
| 541 | back_start += offset; |
| 542 | line_start += offset; |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 543 | |
Bernie Thompson | 530f43a | 2010-02-15 06:46:21 -0800 | [diff] [blame] | 544 | memcpy((char *)back_start, (char *) line_start, |
| 545 | byte_width); |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 546 | } |
| 547 | |
Bernie Thompson | 530f43a | 2010-02-15 06:46:21 -0800 | [diff] [blame] | 548 | while (next_pixel < line_end) { |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 549 | |
Bernie Thompson | 530f43a | 2010-02-15 06:46:21 -0800 | [diff] [blame] | 550 | dlfb_compress_hline((const uint16_t **) &next_pixel, |
| 551 | (const uint16_t *) line_end, &dev_addr, |
| 552 | (u8 **) &cmd, (u8 *) cmd_end); |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 553 | |
Bernie Thompson | 530f43a | 2010-02-15 06:46:21 -0800 | [diff] [blame] | 554 | if (cmd >= cmd_end) { |
| 555 | int len = cmd - (u8 *) urb->transfer_buffer; |
| 556 | if (dlfb_submit_urb(dev, urb, len)) |
Bernie Thompson | 5bea1fb | 2010-09-05 18:28:29 -0700 | [diff] [blame] | 557 | return 1; /* lost pixels is set */ |
Bernie Thompson | 530f43a | 2010-02-15 06:46:21 -0800 | [diff] [blame] | 558 | *sent_ptr += len; |
| 559 | urb = dlfb_get_urb(dev); |
| 560 | if (!urb) |
Bernie Thompson | 5bea1fb | 2010-09-05 18:28:29 -0700 | [diff] [blame] | 561 | return 1; /* lost_pixels is set */ |
Bernie Thompson | 530f43a | 2010-02-15 06:46:21 -0800 | [diff] [blame] | 562 | *urb_ptr = urb; |
| 563 | cmd = urb->transfer_buffer; |
| 564 | cmd_end = &cmd[urb->transfer_buffer_length]; |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | *urb_buf_ptr = cmd; |
Bernie Thompson | 5bea1fb | 2010-09-05 18:28:29 -0700 | [diff] [blame] | 569 | |
| 570 | return 0; |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 571 | } |
| 572 | |
Bernie Thompson | 530f43a | 2010-02-15 06:46:21 -0800 | [diff] [blame] | 573 | int dlfb_handle_damage(struct dlfb_data *dev, int x, int y, |
| 574 | int width, int height, char *data) |
Roberto De Ioris | 7316bc5 | 2009-06-10 23:02:19 -0700 | [diff] [blame] | 575 | { |
Roberto De Ioris | 7316bc5 | 2009-06-10 23:02:19 -0700 | [diff] [blame] | 576 | int i, ret; |
Bernie Thompson | 530f43a | 2010-02-15 06:46:21 -0800 | [diff] [blame] | 577 | char *cmd; |
| 578 | cycles_t start_cycles, end_cycles; |
| 579 | int bytes_sent = 0; |
| 580 | int bytes_identical = 0; |
| 581 | struct urb *urb; |
| 582 | int aligned_x; |
Roberto De Ioris | 7316bc5 | 2009-06-10 23:02:19 -0700 | [diff] [blame] | 583 | |
Bernie Thompson | 530f43a | 2010-02-15 06:46:21 -0800 | [diff] [blame] | 584 | start_cycles = get_cycles(); |
Roberto De Ioris | 7316bc5 | 2009-06-10 23:02:19 -0700 | [diff] [blame] | 585 | |
Bernie Thompson | 530f43a | 2010-02-15 06:46:21 -0800 | [diff] [blame] | 586 | aligned_x = DL_ALIGN_DOWN(x, sizeof(unsigned long)); |
| 587 | width = DL_ALIGN_UP(width + (x-aligned_x), sizeof(unsigned long)); |
| 588 | x = aligned_x; |
Roberto De Ioris | 7316bc5 | 2009-06-10 23:02:19 -0700 | [diff] [blame] | 589 | |
Bernie Thompson | 530f43a | 2010-02-15 06:46:21 -0800 | [diff] [blame] | 590 | if ((width <= 0) || |
| 591 | (x + width > dev->info->var.xres) || |
| 592 | (y + height > dev->info->var.yres)) |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 593 | return -EINVAL; |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 594 | |
Bernie Thompson | 530f43a | 2010-02-15 06:46:21 -0800 | [diff] [blame] | 595 | if (!atomic_read(&dev->usb_active)) |
| 596 | return 0; |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 597 | |
Bernie Thompson | 530f43a | 2010-02-15 06:46:21 -0800 | [diff] [blame] | 598 | urb = dlfb_get_urb(dev); |
| 599 | if (!urb) |
| 600 | return 0; |
| 601 | cmd = urb->transfer_buffer; |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 602 | |
Bernie Thompson | 530f43a | 2010-02-15 06:46:21 -0800 | [diff] [blame] | 603 | for (i = y; i < y + height ; i++) { |
| 604 | const int line_offset = dev->info->fix.line_length * i; |
| 605 | const int byte_offset = line_offset + (x * BPP); |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 606 | |
Bernie Thompson | 5bea1fb | 2010-09-05 18:28:29 -0700 | [diff] [blame] | 607 | if (dlfb_render_hline(dev, &urb, |
| 608 | (char *) dev->info->fix.smem_start, |
Bernie Thompson | 2685cff | 2010-09-05 18:29:56 -0700 | [diff] [blame] | 609 | &cmd, byte_offset, width * BPP, |
Bernie Thompson | 5bea1fb | 2010-09-05 18:28:29 -0700 | [diff] [blame] | 610 | &bytes_identical, &bytes_sent)) |
| 611 | goto error; |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 612 | } |
| 613 | |
Bernie Thompson | 530f43a | 2010-02-15 06:46:21 -0800 | [diff] [blame] | 614 | if (cmd > (char *) urb->transfer_buffer) { |
| 615 | /* Send partial buffer remaining before exiting */ |
| 616 | int len = cmd - (char *) urb->transfer_buffer; |
| 617 | ret = dlfb_submit_urb(dev, urb, len); |
| 618 | bytes_sent += len; |
| 619 | } else |
| 620 | dlfb_urb_completion(urb); |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 621 | |
Bernie Thompson | 5bea1fb | 2010-09-05 18:28:29 -0700 | [diff] [blame] | 622 | error: |
Bernie Thompson | 530f43a | 2010-02-15 06:46:21 -0800 | [diff] [blame] | 623 | atomic_add(bytes_sent, &dev->bytes_sent); |
| 624 | atomic_add(bytes_identical, &dev->bytes_identical); |
| 625 | atomic_add(width*height*2, &dev->bytes_rendered); |
| 626 | end_cycles = get_cycles(); |
| 627 | atomic_add(((unsigned int) ((end_cycles - start_cycles) |
| 628 | >> 10)), /* Kcycles */ |
| 629 | &dev->cpu_kcycles_used); |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 630 | |
Bernie Thompson | 530f43a | 2010-02-15 06:46:21 -0800 | [diff] [blame] | 631 | return 0; |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 632 | } |
| 633 | |
Bernie Thompson | d46ecb9 | 2010-09-05 16:35:04 -0700 | [diff] [blame] | 634 | /* |
| 635 | * Path triggered by usermode clients who write to filesystem |
| 636 | * e.g. cat filename > /dev/fb1 |
| 637 | * Not used by X Windows or text-mode console. But useful for testing. |
| 638 | * Slow because of extra copy and we must assume all pixels dirty. |
| 639 | */ |
| 640 | static ssize_t dlfb_ops_write(struct fb_info *info, const char __user *buf, |
| 641 | size_t count, loff_t *ppos) |
| 642 | { |
Paul Mundt | 1a3e528 | 2011-01-06 17:29:24 +0900 | [diff] [blame] | 643 | ssize_t result; |
Bernie Thompson | d46ecb9 | 2010-09-05 16:35:04 -0700 | [diff] [blame] | 644 | struct dlfb_data *dev = info->par; |
| 645 | u32 offset = (u32) *ppos; |
| 646 | |
Bernie Thompson | d46ecb9 | 2010-09-05 16:35:04 -0700 | [diff] [blame] | 647 | result = fb_sys_write(info, buf, count, ppos); |
| 648 | |
| 649 | if (result > 0) { |
| 650 | int start = max((int)(offset / info->fix.line_length) - 1, 0); |
| 651 | int lines = min((u32)((result / info->fix.line_length) + 1), |
| 652 | (u32)info->var.yres); |
| 653 | |
| 654 | dlfb_handle_damage(dev, 0, start, info->var.xres, |
| 655 | lines, info->screen_base); |
| 656 | } |
Bernie Thompson | d46ecb9 | 2010-09-05 16:35:04 -0700 | [diff] [blame] | 657 | |
| 658 | return result; |
| 659 | } |
| 660 | |
Bernie Thompson | 530f43a | 2010-02-15 06:46:21 -0800 | [diff] [blame] | 661 | /* hardware has native COPY command (see libdlo), but not worth it for fbcon */ |
Bernie Thompson | 4574203 | 2010-02-15 06:46:04 -0800 | [diff] [blame] | 662 | static void dlfb_ops_copyarea(struct fb_info *info, |
Bernie Thompson | 530f43a | 2010-02-15 06:46:21 -0800 | [diff] [blame] | 663 | const struct fb_copyarea *area) |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 664 | { |
Bernie Thompson | 530f43a | 2010-02-15 06:46:21 -0800 | [diff] [blame] | 665 | |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 666 | struct dlfb_data *dev = info->par; |
| 667 | |
Bernie Thompson | 530f43a | 2010-02-15 06:46:21 -0800 | [diff] [blame] | 668 | sys_copyarea(info, area); |
| 669 | |
| 670 | dlfb_handle_damage(dev, area->dx, area->dy, |
| 671 | area->width, area->height, info->screen_base); |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 672 | } |
| 673 | |
Bernie Thompson | 4574203 | 2010-02-15 06:46:04 -0800 | [diff] [blame] | 674 | static void dlfb_ops_imageblit(struct fb_info *info, |
Bernie Thompson | 530f43a | 2010-02-15 06:46:21 -0800 | [diff] [blame] | 675 | const struct fb_image *image) |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 676 | { |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 677 | struct dlfb_data *dev = info->par; |
Bernie Thompson | 530f43a | 2010-02-15 06:46:21 -0800 | [diff] [blame] | 678 | |
Bernie Thompson | 530f43a | 2010-02-15 06:46:21 -0800 | [diff] [blame] | 679 | sys_imageblit(info, image); |
| 680 | |
| 681 | dlfb_handle_damage(dev, image->dx, image->dy, |
| 682 | image->width, image->height, info->screen_base); |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 683 | } |
| 684 | |
Bernie Thompson | 4574203 | 2010-02-15 06:46:04 -0800 | [diff] [blame] | 685 | static void dlfb_ops_fillrect(struct fb_info *info, |
Bernie Thompson | 530f43a | 2010-02-15 06:46:21 -0800 | [diff] [blame] | 686 | const struct fb_fillrect *rect) |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 687 | { |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 688 | struct dlfb_data *dev = info->par; |
| 689 | |
Bernie Thompson | 530f43a | 2010-02-15 06:46:21 -0800 | [diff] [blame] | 690 | sys_fillrect(info, rect); |
| 691 | |
| 692 | dlfb_handle_damage(dev, rect->dx, rect->dy, rect->width, |
| 693 | rect->height, info->screen_base); |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 694 | } |
| 695 | |
Bernie Thompson | d5ed543 | 2010-09-05 16:35:39 -0700 | [diff] [blame] | 696 | /* |
| 697 | * NOTE: fb_defio.c is holding info->fbdefio.mutex |
| 698 | * Touching ANY framebuffer memory that triggers a page fault |
| 699 | * in fb_defio will cause a deadlock, when it also tries to |
| 700 | * grab the same mutex. |
| 701 | */ |
| 702 | static void dlfb_dpy_deferred_io(struct fb_info *info, |
| 703 | struct list_head *pagelist) |
| 704 | { |
| 705 | struct page *cur; |
| 706 | struct fb_deferred_io *fbdefio = info->fbdefio; |
| 707 | struct dlfb_data *dev = info->par; |
| 708 | struct urb *urb; |
| 709 | char *cmd; |
| 710 | cycles_t start_cycles, end_cycles; |
| 711 | int bytes_sent = 0; |
| 712 | int bytes_identical = 0; |
| 713 | int bytes_rendered = 0; |
| 714 | |
| 715 | if (!fb_defio) |
| 716 | return; |
| 717 | |
| 718 | if (!atomic_read(&dev->usb_active)) |
| 719 | return; |
| 720 | |
| 721 | start_cycles = get_cycles(); |
| 722 | |
| 723 | urb = dlfb_get_urb(dev); |
| 724 | if (!urb) |
| 725 | return; |
| 726 | |
| 727 | cmd = urb->transfer_buffer; |
| 728 | |
| 729 | /* walk the written page list and render each to device */ |
| 730 | list_for_each_entry(cur, &fbdefio->pagelist, lru) { |
| 731 | |
Bernie Thompson | 5bea1fb | 2010-09-05 18:28:29 -0700 | [diff] [blame] | 732 | if (dlfb_render_hline(dev, &urb, (char *) info->fix.smem_start, |
Bernie Thompson | d5ed543 | 2010-09-05 16:35:39 -0700 | [diff] [blame] | 733 | &cmd, cur->index << PAGE_SHIFT, |
Bernie Thompson | 5bea1fb | 2010-09-05 18:28:29 -0700 | [diff] [blame] | 734 | PAGE_SIZE, &bytes_identical, &bytes_sent)) |
| 735 | goto error; |
Bernie Thompson | d5ed543 | 2010-09-05 16:35:39 -0700 | [diff] [blame] | 736 | bytes_rendered += PAGE_SIZE; |
| 737 | } |
| 738 | |
| 739 | if (cmd > (char *) urb->transfer_buffer) { |
| 740 | /* Send partial buffer remaining before exiting */ |
| 741 | int len = cmd - (char *) urb->transfer_buffer; |
| 742 | dlfb_submit_urb(dev, urb, len); |
| 743 | bytes_sent += len; |
| 744 | } else |
| 745 | dlfb_urb_completion(urb); |
| 746 | |
Bernie Thompson | 5bea1fb | 2010-09-05 18:28:29 -0700 | [diff] [blame] | 747 | error: |
Bernie Thompson | d5ed543 | 2010-09-05 16:35:39 -0700 | [diff] [blame] | 748 | atomic_add(bytes_sent, &dev->bytes_sent); |
| 749 | atomic_add(bytes_identical, &dev->bytes_identical); |
| 750 | atomic_add(bytes_rendered, &dev->bytes_rendered); |
| 751 | end_cycles = get_cycles(); |
| 752 | atomic_add(((unsigned int) ((end_cycles - start_cycles) |
| 753 | >> 10)), /* Kcycles */ |
| 754 | &dev->cpu_kcycles_used); |
| 755 | } |
| 756 | |
Bernie Thompson | 18dffdf | 2010-09-05 16:35:23 -0700 | [diff] [blame] | 757 | static int dlfb_get_edid(struct dlfb_data *dev, char *edid, int len) |
Bernie Thompson | 7d9485e | 2010-02-15 06:46:08 -0800 | [diff] [blame] | 758 | { |
| 759 | int i; |
| 760 | int ret; |
Bernie Thompson | 18dffdf | 2010-09-05 16:35:23 -0700 | [diff] [blame] | 761 | char *rbuf; |
Bernie Thompson | 7d9485e | 2010-02-15 06:46:08 -0800 | [diff] [blame] | 762 | |
Bernie Thompson | 18dffdf | 2010-09-05 16:35:23 -0700 | [diff] [blame] | 763 | rbuf = kmalloc(2, GFP_KERNEL); |
| 764 | if (!rbuf) |
| 765 | return 0; |
| 766 | |
| 767 | for (i = 0; i < len; i++) { |
Bernie Thompson | 7d9485e | 2010-02-15 06:46:08 -0800 | [diff] [blame] | 768 | ret = usb_control_msg(dev->udev, |
| 769 | usb_rcvctrlpipe(dev->udev, 0), (0x02), |
| 770 | (0x80 | (0x02 << 5)), i << 8, 0xA1, rbuf, 2, |
Bernie Thompson | 18dffdf | 2010-09-05 16:35:23 -0700 | [diff] [blame] | 771 | HZ); |
| 772 | if (ret < 1) { |
Paul Mundt | 81f6f3c | 2011-01-06 18:07:54 +0900 | [diff] [blame] | 773 | pr_err("Read EDID byte %d failed err %x\n", i, ret); |
Bernie Thompson | 18dffdf | 2010-09-05 16:35:23 -0700 | [diff] [blame] | 774 | i--; |
| 775 | break; |
| 776 | } |
| 777 | edid[i] = rbuf[1]; |
Bernie Thompson | 7d9485e | 2010-02-15 06:46:08 -0800 | [diff] [blame] | 778 | } |
Bernie Thompson | 18dffdf | 2010-09-05 16:35:23 -0700 | [diff] [blame] | 779 | |
| 780 | kfree(rbuf); |
| 781 | |
| 782 | return i; |
Bernie Thompson | 7d9485e | 2010-02-15 06:46:08 -0800 | [diff] [blame] | 783 | } |
| 784 | |
Bernie Thompson | 4574203 | 2010-02-15 06:46:04 -0800 | [diff] [blame] | 785 | static int dlfb_ops_ioctl(struct fb_info *info, unsigned int cmd, |
Bernie Thompson | 530f43a | 2010-02-15 06:46:21 -0800 | [diff] [blame] | 786 | unsigned long arg) |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 787 | { |
Bernie Thompson | 530f43a | 2010-02-15 06:46:21 -0800 | [diff] [blame] | 788 | |
| 789 | struct dlfb_data *dev = info->par; |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 790 | |
Bernie Thompson | 530f43a | 2010-02-15 06:46:21 -0800 | [diff] [blame] | 791 | if (!atomic_read(&dev->usb_active)) |
| 792 | return 0; |
| 793 | |
| 794 | /* TODO: Update X server to get this from sysfs instead */ |
| 795 | if (cmd == DLFB_IOCTL_RETURN_EDID) { |
Dr. David Alan Gilbert | def7660 | 2011-08-21 13:34:15 -0700 | [diff] [blame] | 796 | void __user *edid = (void __user *)arg; |
Bernie Thompson | 18dffdf | 2010-09-05 16:35:23 -0700 | [diff] [blame] | 797 | if (copy_to_user(edid, dev->edid, dev->edid_size)) |
Roberto De Ioris | 7316bc5 | 2009-06-10 23:02:19 -0700 | [diff] [blame] | 798 | return -EFAULT; |
Roberto De Ioris | 7316bc5 | 2009-06-10 23:02:19 -0700 | [diff] [blame] | 799 | return 0; |
| 800 | } |
| 801 | |
Bernie Thompson | 530f43a | 2010-02-15 06:46:21 -0800 | [diff] [blame] | 802 | /* TODO: Help propose a standard fb.h ioctl to report mmap damage */ |
| 803 | if (cmd == DLFB_IOCTL_REPORT_DAMAGE) { |
Dr. David Alan Gilbert | def7660 | 2011-08-21 13:34:15 -0700 | [diff] [blame] | 804 | struct dloarea area; |
| 805 | |
| 806 | if (copy_from_user(&area, (void __user *)arg, |
| 807 | sizeof(struct dloarea))) |
| 808 | return -EFAULT; |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 809 | |
Bernie Thompson | 5bea1fb | 2010-09-05 18:28:29 -0700 | [diff] [blame] | 810 | /* |
| 811 | * If we have a damage-aware client, turn fb_defio "off" |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 812 | * To avoid perf imact of unnecessary page fault handling. |
Bernie Thompson | 5bea1fb | 2010-09-05 18:28:29 -0700 | [diff] [blame] | 813 | * Done by resetting the delay for this fb_info to a very |
| 814 | * long period. Pages will become writable and stay that way. |
| 815 | * Reset to normal value when all clients have closed this fb. |
| 816 | */ |
| 817 | if (info->fbdefio) |
| 818 | info->fbdefio->delay = DL_DEFIO_WRITE_DISABLE; |
| 819 | |
Dr. David Alan Gilbert | def7660 | 2011-08-21 13:34:15 -0700 | [diff] [blame] | 820 | if (area.x < 0) |
| 821 | area.x = 0; |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 822 | |
Dr. David Alan Gilbert | def7660 | 2011-08-21 13:34:15 -0700 | [diff] [blame] | 823 | if (area.x > info->var.xres) |
| 824 | area.x = info->var.xres; |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 825 | |
Dr. David Alan Gilbert | def7660 | 2011-08-21 13:34:15 -0700 | [diff] [blame] | 826 | if (area.y < 0) |
| 827 | area.y = 0; |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 828 | |
Dr. David Alan Gilbert | def7660 | 2011-08-21 13:34:15 -0700 | [diff] [blame] | 829 | if (area.y > info->var.yres) |
| 830 | area.y = info->var.yres; |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 831 | |
Dr. David Alan Gilbert | def7660 | 2011-08-21 13:34:15 -0700 | [diff] [blame] | 832 | dlfb_handle_damage(dev, area.x, area.y, area.w, area.h, |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 833 | info->screen_base); |
| 834 | } |
Roberto De Ioris | 7316bc5 | 2009-06-10 23:02:19 -0700 | [diff] [blame] | 835 | |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 836 | return 0; |
| 837 | } |
| 838 | |
Greg Kroah-Hartman | f05e057 | 2009-06-03 14:47:08 -0700 | [diff] [blame] | 839 | /* taken from vesafb */ |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 840 | static int |
Bernie Thompson | 4574203 | 2010-02-15 06:46:04 -0800 | [diff] [blame] | 841 | dlfb_ops_setcolreg(unsigned regno, unsigned red, unsigned green, |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 842 | unsigned blue, unsigned transp, struct fb_info *info) |
| 843 | { |
| 844 | int err = 0; |
| 845 | |
| 846 | if (regno >= info->cmap.len) |
| 847 | return 1; |
| 848 | |
| 849 | if (regno < 16) { |
| 850 | if (info->var.red.offset == 10) { |
| 851 | /* 1:5:5:5 */ |
| 852 | ((u32 *) (info->pseudo_palette))[regno] = |
| 853 | ((red & 0xf800) >> 1) | |
| 854 | ((green & 0xf800) >> 6) | ((blue & 0xf800) >> 11); |
| 855 | } else { |
| 856 | /* 0:5:6:5 */ |
| 857 | ((u32 *) (info->pseudo_palette))[regno] = |
| 858 | ((red & 0xf800)) | |
| 859 | ((green & 0xfc00) >> 5) | ((blue & 0xf800) >> 11); |
| 860 | } |
| 861 | } |
| 862 | |
| 863 | return err; |
| 864 | } |
| 865 | |
Bernie Thompson | 3e8f3d6 | 2010-02-15 06:46:26 -0800 | [diff] [blame] | 866 | /* |
| 867 | * It's common for several clients to have framebuffer open simultaneously. |
| 868 | * e.g. both fbcon and X. Makes things interesting. |
Bernie Thompson | 33077b8 | 2010-09-05 16:35:19 -0700 | [diff] [blame] | 869 | * Assumes caller is holding info->lock (for open and release at least) |
Bernie Thompson | 3e8f3d6 | 2010-02-15 06:46:26 -0800 | [diff] [blame] | 870 | */ |
| 871 | static int dlfb_ops_open(struct fb_info *info, int user) |
| 872 | { |
| 873 | struct dlfb_data *dev = info->par; |
| 874 | |
Bernie Thompson | d5ed543 | 2010-09-05 16:35:39 -0700 | [diff] [blame] | 875 | /* |
| 876 | * fbcon aggressively connects to first framebuffer it finds, |
| 877 | * preventing other clients (X) from working properly. Usually |
| 878 | * not what the user wants. Fail by default with option to enable. |
| 879 | */ |
Dr. David Alan Gilbert | def7660 | 2011-08-21 13:34:15 -0700 | [diff] [blame] | 880 | if ((user == 0) && (!console)) |
Bernie Thompson | d5ed543 | 2010-09-05 16:35:39 -0700 | [diff] [blame] | 881 | return -EBUSY; |
Bernie Thompson | 3e8f3d6 | 2010-02-15 06:46:26 -0800 | [diff] [blame] | 882 | |
Bernie Thompson | 33077b8 | 2010-09-05 16:35:19 -0700 | [diff] [blame] | 883 | /* If the USB device is gone, we don't accept new opens */ |
| 884 | if (dev->virtualized) |
| 885 | return -ENODEV; |
Bernie Thompson | 3e8f3d6 | 2010-02-15 06:46:26 -0800 | [diff] [blame] | 886 | |
| 887 | dev->fb_count++; |
| 888 | |
Bernie Thompson | 33077b8 | 2010-09-05 16:35:19 -0700 | [diff] [blame] | 889 | kref_get(&dev->kref); |
| 890 | |
Bernie Thompson | d5ed543 | 2010-09-05 16:35:39 -0700 | [diff] [blame] | 891 | if (fb_defio && (info->fbdefio == NULL)) { |
Bernie Thompson | 5bea1fb | 2010-09-05 18:28:29 -0700 | [diff] [blame] | 892 | /* enable defio at last moment if not disabled by client */ |
| 893 | |
| 894 | struct fb_deferred_io *fbdefio; |
| 895 | |
Heiko Stübner | 1f45f9d | 2012-04-28 12:19:10 +0200 | [diff] [blame] | 896 | fbdefio = kzalloc(sizeof(struct fb_deferred_io), GFP_KERNEL); |
Bernie Thompson | 5bea1fb | 2010-09-05 18:28:29 -0700 | [diff] [blame] | 897 | |
| 898 | if (fbdefio) { |
| 899 | fbdefio->delay = DL_DEFIO_WRITE_DELAY; |
| 900 | fbdefio->deferred_io = dlfb_dpy_deferred_io; |
| 901 | } |
| 902 | |
| 903 | info->fbdefio = fbdefio; |
Bernie Thompson | 3e8f3d6 | 2010-02-15 06:46:26 -0800 | [diff] [blame] | 904 | fb_deferred_io_init(info); |
| 905 | } |
Bernie Thompson | 3e8f3d6 | 2010-02-15 06:46:26 -0800 | [diff] [blame] | 906 | |
Paul Mundt | 81f6f3c | 2011-01-06 18:07:54 +0900 | [diff] [blame] | 907 | pr_notice("open /dev/fb%d user=%d fb_info=%p count=%d\n", |
Bernie Thompson | 3e8f3d6 | 2010-02-15 06:46:26 -0800 | [diff] [blame] | 908 | info->node, user, info, dev->fb_count); |
| 909 | |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 910 | return 0; |
| 911 | } |
| 912 | |
Bernie Thompson | 4a4854d | 2010-02-15 06:45:55 -0800 | [diff] [blame] | 913 | /* |
| 914 | * Called when all client interfaces to start transactions have been disabled, |
| 915 | * and all references to our device instance (dlfb_data) are released. |
| 916 | * Every transaction must have a reference, so we know are fully spun down |
| 917 | */ |
Bernie Thompson | 33077b8 | 2010-09-05 16:35:19 -0700 | [diff] [blame] | 918 | static void dlfb_free(struct kref *kref) |
Bernie Thompson | 4a4854d | 2010-02-15 06:45:55 -0800 | [diff] [blame] | 919 | { |
| 920 | struct dlfb_data *dev = container_of(kref, struct dlfb_data, kref); |
| 921 | |
| 922 | if (dev->backing_buffer) |
| 923 | vfree(dev->backing_buffer); |
| 924 | |
Bernie Thompson | 33077b8 | 2010-09-05 16:35:19 -0700 | [diff] [blame] | 925 | kfree(dev->edid); |
| 926 | |
Paul Mundt | 81f6f3c | 2011-01-06 18:07:54 +0900 | [diff] [blame] | 927 | pr_warn("freeing dlfb_data %p\n", dev); |
Bernie Thompson | 3e8f3d6 | 2010-02-15 06:46:26 -0800 | [diff] [blame] | 928 | |
Bernie Thompson | 4a4854d | 2010-02-15 06:45:55 -0800 | [diff] [blame] | 929 | kfree(dev); |
| 930 | } |
| 931 | |
Bernie Thompson | 5bea1fb | 2010-09-05 18:28:29 -0700 | [diff] [blame] | 932 | static void dlfb_release_urb_work(struct work_struct *work) |
| 933 | { |
| 934 | struct urb_node *unode = container_of(work, struct urb_node, |
| 935 | release_urb_work.work); |
| 936 | |
| 937 | up(&unode->dev->urbs.limit_sem); |
| 938 | } |
Bernie Thompson | 33077b8 | 2010-09-05 16:35:19 -0700 | [diff] [blame] | 939 | |
Bernie Thompson | 8d21547 | 2012-03-01 17:35:48 -0800 | [diff] [blame] | 940 | static void dlfb_free_framebuffer(struct dlfb_data *dev) |
Bernie Thompson | 2469d5d | 2010-02-15 06:46:13 -0800 | [diff] [blame] | 941 | { |
Bernie Thompson | 33077b8 | 2010-09-05 16:35:19 -0700 | [diff] [blame] | 942 | struct fb_info *info = dev->info; |
Bernie Thompson | 33077b8 | 2010-09-05 16:35:19 -0700 | [diff] [blame] | 943 | |
Bernie Thompson | 8d21547 | 2012-03-01 17:35:48 -0800 | [diff] [blame] | 944 | if (info) { |
| 945 | int node = info->node; |
Bernie Thompson | 2469d5d | 2010-02-15 06:46:13 -0800 | [diff] [blame] | 946 | |
Bernie Thompson | 8d21547 | 2012-03-01 17:35:48 -0800 | [diff] [blame] | 947 | unregister_framebuffer(info); |
Bernie Thompson | 2469d5d | 2010-02-15 06:46:13 -0800 | [diff] [blame] | 948 | |
Bernie Thompson | 8d21547 | 2012-03-01 17:35:48 -0800 | [diff] [blame] | 949 | if (info->cmap.len != 0) |
| 950 | fb_dealloc_cmap(&info->cmap); |
| 951 | if (info->monspecs.modedb) |
| 952 | fb_destroy_modedb(info->monspecs.modedb); |
| 953 | if (info->screen_base) |
| 954 | vfree(info->screen_base); |
Bernie Thompson | 2469d5d | 2010-02-15 06:46:13 -0800 | [diff] [blame] | 955 | |
Bernie Thompson | 8d21547 | 2012-03-01 17:35:48 -0800 | [diff] [blame] | 956 | fb_destroy_modelist(&info->modelist); |
Bernie Thompson | 33077b8 | 2010-09-05 16:35:19 -0700 | [diff] [blame] | 957 | |
Bernie Thompson | 8d21547 | 2012-03-01 17:35:48 -0800 | [diff] [blame] | 958 | dev->info = NULL; |
Bernie Thompson | 2469d5d | 2010-02-15 06:46:13 -0800 | [diff] [blame] | 959 | |
Bernie Thompson | 8d21547 | 2012-03-01 17:35:48 -0800 | [diff] [blame] | 960 | /* Assume info structure is freed after this point */ |
| 961 | framebuffer_release(info); |
| 962 | |
| 963 | pr_warn("fb_info for /dev/fb%d has been freed\n", node); |
| 964 | } |
Bernie Thompson | 33077b8 | 2010-09-05 16:35:19 -0700 | [diff] [blame] | 965 | |
| 966 | /* ref taken in probe() as part of registering framebfufer */ |
| 967 | kref_put(&dev->kref, dlfb_free); |
| 968 | } |
| 969 | |
Bernie Thompson | 8d21547 | 2012-03-01 17:35:48 -0800 | [diff] [blame] | 970 | static void dlfb_free_framebuffer_work(struct work_struct *work) |
| 971 | { |
| 972 | struct dlfb_data *dev = container_of(work, struct dlfb_data, |
| 973 | free_framebuffer_work.work); |
| 974 | dlfb_free_framebuffer(dev); |
| 975 | } |
Bernie Thompson | 33077b8 | 2010-09-05 16:35:19 -0700 | [diff] [blame] | 976 | /* |
| 977 | * Assumes caller is holding info->lock mutex (for open and release at least) |
| 978 | */ |
| 979 | static int dlfb_ops_release(struct fb_info *info, int user) |
| 980 | { |
| 981 | struct dlfb_data *dev = info->par; |
| 982 | |
| 983 | dev->fb_count--; |
| 984 | |
| 985 | /* We can't free fb_info here - fbmem will touch it when we return */ |
| 986 | if (dev->virtualized && (dev->fb_count == 0)) |
| 987 | schedule_delayed_work(&dev->free_framebuffer_work, HZ); |
| 988 | |
Bernie Thompson | 33077b8 | 2010-09-05 16:35:19 -0700 | [diff] [blame] | 989 | if ((dev->fb_count == 0) && (info->fbdefio)) { |
| 990 | fb_deferred_io_cleanup(info); |
| 991 | kfree(info->fbdefio); |
| 992 | info->fbdefio = NULL; |
| 993 | info->fbops->fb_mmap = dlfb_ops_mmap; |
| 994 | } |
Bernie Thompson | 33077b8 | 2010-09-05 16:35:19 -0700 | [diff] [blame] | 995 | |
Paul Mundt | 81f6f3c | 2011-01-06 18:07:54 +0900 | [diff] [blame] | 996 | pr_warn("released /dev/fb%d user=%d count=%d\n", |
Bernie Thompson | 33077b8 | 2010-09-05 16:35:19 -0700 | [diff] [blame] | 997 | info->node, user, dev->fb_count); |
| 998 | |
| 999 | kref_put(&dev->kref, dlfb_free); |
| 1000 | |
| 1001 | return 0; |
Bernie Thompson | 2469d5d | 2010-02-15 06:46:13 -0800 | [diff] [blame] | 1002 | } |
| 1003 | |
| 1004 | /* |
Bernie Thompson | 7d9485e | 2010-02-15 06:46:08 -0800 | [diff] [blame] | 1005 | * Check whether a video mode is supported by the DisplayLink chip |
| 1006 | * We start from monitor's modes, so don't need to filter that here |
| 1007 | */ |
| 1008 | static int dlfb_is_valid_mode(struct fb_videomode *mode, |
| 1009 | struct fb_info *info) |
| 1010 | { |
| 1011 | struct dlfb_data *dev = info->par; |
| 1012 | |
Bernie Thompson | 18dffdf | 2010-09-05 16:35:23 -0700 | [diff] [blame] | 1013 | if (mode->xres * mode->yres > dev->sku_pixel_limit) { |
Paul Mundt | 81f6f3c | 2011-01-06 18:07:54 +0900 | [diff] [blame] | 1014 | pr_warn("%dx%d beyond chip capabilities\n", |
Bernie Thompson | 18dffdf | 2010-09-05 16:35:23 -0700 | [diff] [blame] | 1015 | mode->xres, mode->yres); |
Bernie Thompson | 7d9485e | 2010-02-15 06:46:08 -0800 | [diff] [blame] | 1016 | return 0; |
Bernie Thompson | 18dffdf | 2010-09-05 16:35:23 -0700 | [diff] [blame] | 1017 | } |
| 1018 | |
Martin Decky | 9daee73 | 2012-03-01 16:31:11 -0800 | [diff] [blame] | 1019 | pr_info("%dx%d @ %d Hz valid mode\n", mode->xres, mode->yres, |
| 1020 | mode->refresh); |
Bernie Thompson | 7d9485e | 2010-02-15 06:46:08 -0800 | [diff] [blame] | 1021 | |
| 1022 | return 1; |
| 1023 | } |
| 1024 | |
| 1025 | static void dlfb_var_color_format(struct fb_var_screeninfo *var) |
| 1026 | { |
| 1027 | const struct fb_bitfield red = { 11, 5, 0 }; |
| 1028 | const struct fb_bitfield green = { 5, 6, 0 }; |
| 1029 | const struct fb_bitfield blue = { 0, 5, 0 }; |
| 1030 | |
| 1031 | var->bits_per_pixel = 16; |
| 1032 | var->red = red; |
| 1033 | var->green = green; |
| 1034 | var->blue = blue; |
| 1035 | } |
| 1036 | |
Bernie Thompson | 2469d5d | 2010-02-15 06:46:13 -0800 | [diff] [blame] | 1037 | static int dlfb_ops_check_var(struct fb_var_screeninfo *var, |
| 1038 | struct fb_info *info) |
| 1039 | { |
| 1040 | struct fb_videomode mode; |
| 1041 | |
| 1042 | /* TODO: support dynamically changing framebuffer size */ |
| 1043 | if ((var->xres * var->yres * 2) > info->fix.smem_len) |
| 1044 | return -EINVAL; |
| 1045 | |
| 1046 | /* set device-specific elements of var unrelated to mode */ |
| 1047 | dlfb_var_color_format(var); |
| 1048 | |
| 1049 | fb_var_to_videomode(&mode, var); |
| 1050 | |
| 1051 | if (!dlfb_is_valid_mode(&mode, info)) |
| 1052 | return -EINVAL; |
| 1053 | |
| 1054 | return 0; |
| 1055 | } |
| 1056 | |
| 1057 | static int dlfb_ops_set_par(struct fb_info *info) |
| 1058 | { |
| 1059 | struct dlfb_data *dev = info->par; |
Bernie Thompson | 18dffdf | 2010-09-05 16:35:23 -0700 | [diff] [blame] | 1060 | int result; |
| 1061 | u16 *pix_framebuffer; |
| 1062 | int i; |
Bernie Thompson | 2469d5d | 2010-02-15 06:46:13 -0800 | [diff] [blame] | 1063 | |
Paul Mundt | 81f6f3c | 2011-01-06 18:07:54 +0900 | [diff] [blame] | 1064 | pr_notice("set_par mode %dx%d\n", info->var.xres, info->var.yres); |
Bernie Thompson | 2469d5d | 2010-02-15 06:46:13 -0800 | [diff] [blame] | 1065 | |
Bernie Thompson | 18dffdf | 2010-09-05 16:35:23 -0700 | [diff] [blame] | 1066 | result = dlfb_set_video_mode(dev, &info->var); |
| 1067 | |
| 1068 | if ((result == 0) && (dev->fb_count == 0)) { |
| 1069 | |
| 1070 | /* paint greenscreen */ |
| 1071 | |
| 1072 | pix_framebuffer = (u16 *) info->screen_base; |
| 1073 | for (i = 0; i < info->fix.smem_len / 2; i++) |
| 1074 | pix_framebuffer[i] = 0x37e6; |
| 1075 | |
| 1076 | dlfb_handle_damage(dev, 0, 0, info->var.xres, info->var.yres, |
| 1077 | info->screen_base); |
| 1078 | } |
| 1079 | |
| 1080 | return result; |
Bernie Thompson | 2469d5d | 2010-02-15 06:46:13 -0800 | [diff] [blame] | 1081 | } |
| 1082 | |
Bernie Thompson | 58e7c3b | 2011-08-21 13:34:11 -0700 | [diff] [blame] | 1083 | /* To fonzi the jukebox (e.g. make blanking changes take effect) */ |
| 1084 | static char *dlfb_dummy_render(char *buf) |
| 1085 | { |
| 1086 | *buf++ = 0xAF; |
| 1087 | *buf++ = 0x6A; /* copy */ |
| 1088 | *buf++ = 0x00; /* from address*/ |
| 1089 | *buf++ = 0x00; |
| 1090 | *buf++ = 0x00; |
| 1091 | *buf++ = 0x01; /* one pixel */ |
| 1092 | *buf++ = 0x00; /* to address */ |
| 1093 | *buf++ = 0x00; |
| 1094 | *buf++ = 0x00; |
| 1095 | return buf; |
| 1096 | } |
| 1097 | |
Bernie Thompson | 9825f70f | 2010-09-05 16:35:10 -0700 | [diff] [blame] | 1098 | /* |
| 1099 | * In order to come back from full DPMS off, we need to set the mode again |
| 1100 | */ |
Bernie Thompson | 4574203 | 2010-02-15 06:46:04 -0800 | [diff] [blame] | 1101 | static int dlfb_ops_blank(int blank_mode, struct fb_info *info) |
Greg Kroah-Hartman | f05e057 | 2009-06-03 14:47:08 -0700 | [diff] [blame] | 1102 | { |
Bernie Thompson | 530f43a | 2010-02-15 06:46:21 -0800 | [diff] [blame] | 1103 | struct dlfb_data *dev = info->par; |
Bernie Thompson | 58e7c3b | 2011-08-21 13:34:11 -0700 | [diff] [blame] | 1104 | char *bufptr; |
| 1105 | struct urb *urb; |
Roberto De Ioris | 7316bc5 | 2009-06-10 23:02:19 -0700 | [diff] [blame] | 1106 | |
Bernie Thompson | 58e7c3b | 2011-08-21 13:34:11 -0700 | [diff] [blame] | 1107 | pr_info("/dev/fb%d FB_BLANK mode %d --> %d\n", |
| 1108 | info->node, dev->blank_mode, blank_mode); |
Roberto De Ioris | 7316bc5 | 2009-06-10 23:02:19 -0700 | [diff] [blame] | 1109 | |
Bernie Thompson | 58e7c3b | 2011-08-21 13:34:11 -0700 | [diff] [blame] | 1110 | if ((dev->blank_mode == FB_BLANK_POWERDOWN) && |
| 1111 | (blank_mode != FB_BLANK_POWERDOWN)) { |
Bernie Thompson | 9825f70f | 2010-09-05 16:35:10 -0700 | [diff] [blame] | 1112 | |
Bernie Thompson | 58e7c3b | 2011-08-21 13:34:11 -0700 | [diff] [blame] | 1113 | /* returning from powerdown requires a fresh modeset */ |
Bernie Thompson | 9825f70f | 2010-09-05 16:35:10 -0700 | [diff] [blame] | 1114 | dlfb_set_video_mode(dev, &info->var); |
| 1115 | } |
Roberto De Ioris | 7316bc5 | 2009-06-10 23:02:19 -0700 | [diff] [blame] | 1116 | |
Bernie Thompson | 58e7c3b | 2011-08-21 13:34:11 -0700 | [diff] [blame] | 1117 | urb = dlfb_get_urb(dev); |
| 1118 | if (!urb) |
| 1119 | return 0; |
| 1120 | |
| 1121 | bufptr = (char *) urb->transfer_buffer; |
| 1122 | bufptr = dlfb_vidreg_lock(bufptr); |
| 1123 | bufptr = dlfb_blanking(bufptr, blank_mode); |
| 1124 | bufptr = dlfb_vidreg_unlock(bufptr); |
| 1125 | |
| 1126 | /* seems like a render op is needed to have blank change take effect */ |
| 1127 | bufptr = dlfb_dummy_render(bufptr); |
| 1128 | |
| 1129 | dlfb_submit_urb(dev, urb, bufptr - |
| 1130 | (char *) urb->transfer_buffer); |
| 1131 | |
| 1132 | dev->blank_mode = blank_mode; |
| 1133 | |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 1134 | return 0; |
| 1135 | } |
| 1136 | |
| 1137 | static struct fb_ops dlfb_ops = { |
Bernie Thompson | 2469d5d | 2010-02-15 06:46:13 -0800 | [diff] [blame] | 1138 | .owner = THIS_MODULE, |
Paul Mundt | 1a3e528 | 2011-01-06 17:29:24 +0900 | [diff] [blame] | 1139 | .fb_read = fb_sys_read, |
Bernie Thompson | d46ecb9 | 2010-09-05 16:35:04 -0700 | [diff] [blame] | 1140 | .fb_write = dlfb_ops_write, |
Bernie Thompson | 4574203 | 2010-02-15 06:46:04 -0800 | [diff] [blame] | 1141 | .fb_setcolreg = dlfb_ops_setcolreg, |
| 1142 | .fb_fillrect = dlfb_ops_fillrect, |
| 1143 | .fb_copyarea = dlfb_ops_copyarea, |
| 1144 | .fb_imageblit = dlfb_ops_imageblit, |
| 1145 | .fb_mmap = dlfb_ops_mmap, |
| 1146 | .fb_ioctl = dlfb_ops_ioctl, |
Bernie Thompson | 3e8f3d6 | 2010-02-15 06:46:26 -0800 | [diff] [blame] | 1147 | .fb_open = dlfb_ops_open, |
Bernie Thompson | 4574203 | 2010-02-15 06:46:04 -0800 | [diff] [blame] | 1148 | .fb_release = dlfb_ops_release, |
| 1149 | .fb_blank = dlfb_ops_blank, |
Bernie Thompson | 2469d5d | 2010-02-15 06:46:13 -0800 | [diff] [blame] | 1150 | .fb_check_var = dlfb_ops_check_var, |
| 1151 | .fb_set_par = dlfb_ops_set_par, |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 1152 | }; |
| 1153 | |
Bernie Thompson | 18dffdf | 2010-09-05 16:35:23 -0700 | [diff] [blame] | 1154 | |
Bernie Thompson | cc403dc | 2010-02-15 06:45:49 -0800 | [diff] [blame] | 1155 | /* |
Bernie Thompson | 18dffdf | 2010-09-05 16:35:23 -0700 | [diff] [blame] | 1156 | * Assumes &info->lock held by caller |
| 1157 | * Assumes no active clients have framebuffer open |
| 1158 | */ |
| 1159 | static int dlfb_realloc_framebuffer(struct dlfb_data *dev, struct fb_info *info) |
| 1160 | { |
| 1161 | int retval = -ENOMEM; |
| 1162 | int old_len = info->fix.smem_len; |
| 1163 | int new_len; |
| 1164 | unsigned char *old_fb = info->screen_base; |
| 1165 | unsigned char *new_fb; |
Stuart Hopkins | d318954 | 2011-08-21 13:34:17 -0700 | [diff] [blame] | 1166 | unsigned char *new_back = 0; |
Bernie Thompson | 18dffdf | 2010-09-05 16:35:23 -0700 | [diff] [blame] | 1167 | |
Paul Mundt | 81f6f3c | 2011-01-06 18:07:54 +0900 | [diff] [blame] | 1168 | pr_warn("Reallocating framebuffer. Addresses will change!\n"); |
Bernie Thompson | 18dffdf | 2010-09-05 16:35:23 -0700 | [diff] [blame] | 1169 | |
| 1170 | new_len = info->fix.line_length * info->var.yres; |
| 1171 | |
| 1172 | if (PAGE_ALIGN(new_len) > old_len) { |
| 1173 | /* |
| 1174 | * Alloc system memory for virtual framebuffer |
| 1175 | */ |
| 1176 | new_fb = vmalloc(new_len); |
| 1177 | if (!new_fb) { |
Paul Mundt | 81f6f3c | 2011-01-06 18:07:54 +0900 | [diff] [blame] | 1178 | pr_err("Virtual framebuffer alloc failed\n"); |
Bernie Thompson | 18dffdf | 2010-09-05 16:35:23 -0700 | [diff] [blame] | 1179 | goto error; |
| 1180 | } |
| 1181 | |
| 1182 | if (info->screen_base) { |
| 1183 | memcpy(new_fb, old_fb, old_len); |
| 1184 | vfree(info->screen_base); |
| 1185 | } |
| 1186 | |
| 1187 | info->screen_base = new_fb; |
| 1188 | info->fix.smem_len = PAGE_ALIGN(new_len); |
| 1189 | info->fix.smem_start = (unsigned long) new_fb; |
| 1190 | info->flags = udlfb_info_flags; |
| 1191 | |
| 1192 | /* |
| 1193 | * Second framebuffer copy to mirror the framebuffer state |
| 1194 | * on the physical USB device. We can function without this. |
| 1195 | * But with imperfect damage info we may send pixels over USB |
| 1196 | * that were, in fact, unchanged - wasting limited USB bandwidth |
| 1197 | */ |
Stuart Hopkins | d318954 | 2011-08-21 13:34:17 -0700 | [diff] [blame] | 1198 | if (shadow) |
| 1199 | new_back = vzalloc(new_len); |
Bernie Thompson | 18dffdf | 2010-09-05 16:35:23 -0700 | [diff] [blame] | 1200 | if (!new_back) |
Linus Torvalds | 949f671 | 2011-01-10 16:04:53 -0800 | [diff] [blame] | 1201 | pr_info("No shadow/backing buffer allocated\n"); |
Bernie Thompson | 18dffdf | 2010-09-05 16:35:23 -0700 | [diff] [blame] | 1202 | else { |
| 1203 | if (dev->backing_buffer) |
| 1204 | vfree(dev->backing_buffer); |
| 1205 | dev->backing_buffer = new_back; |
Bernie Thompson | 18dffdf | 2010-09-05 16:35:23 -0700 | [diff] [blame] | 1206 | } |
| 1207 | } |
| 1208 | |
| 1209 | retval = 0; |
| 1210 | |
| 1211 | error: |
| 1212 | return retval; |
| 1213 | } |
| 1214 | |
| 1215 | /* |
| 1216 | * 1) Get EDID from hw, or use sw default |
| 1217 | * 2) Parse into various fb_info structs |
| 1218 | * 3) Allocate virtual framebuffer memory to back highest res mode |
| 1219 | * |
| 1220 | * Parses EDID into three places used by various parts of fbdev: |
Bernie Thompson | 7d9485e | 2010-02-15 06:46:08 -0800 | [diff] [blame] | 1221 | * fb_var_screeninfo contains the timing of the monitor's preferred mode |
| 1222 | * fb_info.monspecs is full parsed EDID info, including monspecs.modedb |
| 1223 | * fb_info.modelist is a linked list of all monitor & VESA modes which work |
| 1224 | * |
| 1225 | * If EDID is not readable/valid, then modelist is all VESA modes, |
| 1226 | * monspecs is NULL, and fb_var_screeninfo is set to safe VESA mode |
Bernie Thompson | 18dffdf | 2010-09-05 16:35:23 -0700 | [diff] [blame] | 1227 | * Returns 0 if successful |
Bernie Thompson | 7d9485e | 2010-02-15 06:46:08 -0800 | [diff] [blame] | 1228 | */ |
Bernie Thompson | 18dffdf | 2010-09-05 16:35:23 -0700 | [diff] [blame] | 1229 | static int dlfb_setup_modes(struct dlfb_data *dev, |
| 1230 | struct fb_info *info, |
| 1231 | char *default_edid, size_t default_edid_size) |
Bernie Thompson | 7d9485e | 2010-02-15 06:46:08 -0800 | [diff] [blame] | 1232 | { |
| 1233 | int i; |
| 1234 | const struct fb_videomode *default_vmode = NULL; |
| 1235 | int result = 0; |
Bernie Thompson | 18dffdf | 2010-09-05 16:35:23 -0700 | [diff] [blame] | 1236 | char *edid; |
| 1237 | int tries = 3; |
| 1238 | |
| 1239 | if (info->dev) /* only use mutex if info has been registered */ |
| 1240 | mutex_lock(&info->lock); |
| 1241 | |
Paul Mundt | b9f03a3 | 2011-01-06 18:04:02 +0900 | [diff] [blame] | 1242 | edid = kmalloc(EDID_LENGTH, GFP_KERNEL); |
Bernie Thompson | 18dffdf | 2010-09-05 16:35:23 -0700 | [diff] [blame] | 1243 | if (!edid) { |
| 1244 | result = -ENOMEM; |
| 1245 | goto error; |
| 1246 | } |
Bernie Thompson | 7d9485e | 2010-02-15 06:46:08 -0800 | [diff] [blame] | 1247 | |
| 1248 | fb_destroy_modelist(&info->modelist); |
| 1249 | memset(&info->monspecs, 0, sizeof(info->monspecs)); |
| 1250 | |
Bernie Thompson | 18dffdf | 2010-09-05 16:35:23 -0700 | [diff] [blame] | 1251 | /* |
| 1252 | * Try to (re)read EDID from hardware first |
| 1253 | * EDID data may return, but not parse as valid |
| 1254 | * Try again a few times, in case of e.g. analog cable noise |
| 1255 | */ |
| 1256 | while (tries--) { |
Bernie Thompson | 7d9485e | 2010-02-15 06:46:08 -0800 | [diff] [blame] | 1257 | |
Paul Mundt | b9f03a3 | 2011-01-06 18:04:02 +0900 | [diff] [blame] | 1258 | i = dlfb_get_edid(dev, edid, EDID_LENGTH); |
Bernie Thompson | 18dffdf | 2010-09-05 16:35:23 -0700 | [diff] [blame] | 1259 | |
Paul Mundt | b9f03a3 | 2011-01-06 18:04:02 +0900 | [diff] [blame] | 1260 | if (i >= EDID_LENGTH) |
Bernie Thompson | 18dffdf | 2010-09-05 16:35:23 -0700 | [diff] [blame] | 1261 | fb_edid_to_monspecs(edid, &info->monspecs); |
| 1262 | |
| 1263 | if (info->monspecs.modedb_len > 0) { |
| 1264 | dev->edid = edid; |
| 1265 | dev->edid_size = i; |
| 1266 | break; |
| 1267 | } |
| 1268 | } |
| 1269 | |
| 1270 | /* If that fails, use a previously returned EDID if available */ |
| 1271 | if (info->monspecs.modedb_len == 0) { |
| 1272 | |
Paul Mundt | 81f6f3c | 2011-01-06 18:07:54 +0900 | [diff] [blame] | 1273 | pr_err("Unable to get valid EDID from device/display\n"); |
Bernie Thompson | 18dffdf | 2010-09-05 16:35:23 -0700 | [diff] [blame] | 1274 | |
| 1275 | if (dev->edid) { |
| 1276 | fb_edid_to_monspecs(dev->edid, &info->monspecs); |
| 1277 | if (info->monspecs.modedb_len > 0) |
Paul Mundt | 81f6f3c | 2011-01-06 18:07:54 +0900 | [diff] [blame] | 1278 | pr_err("Using previously queried EDID\n"); |
Bernie Thompson | 18dffdf | 2010-09-05 16:35:23 -0700 | [diff] [blame] | 1279 | } |
| 1280 | } |
| 1281 | |
| 1282 | /* If that fails, use the default EDID we were handed */ |
| 1283 | if (info->monspecs.modedb_len == 0) { |
Paul Mundt | b9f03a3 | 2011-01-06 18:04:02 +0900 | [diff] [blame] | 1284 | if (default_edid_size >= EDID_LENGTH) { |
Bernie Thompson | 18dffdf | 2010-09-05 16:35:23 -0700 | [diff] [blame] | 1285 | fb_edid_to_monspecs(default_edid, &info->monspecs); |
| 1286 | if (info->monspecs.modedb_len > 0) { |
| 1287 | memcpy(edid, default_edid, default_edid_size); |
| 1288 | dev->edid = edid; |
| 1289 | dev->edid_size = default_edid_size; |
Paul Mundt | 81f6f3c | 2011-01-06 18:07:54 +0900 | [diff] [blame] | 1290 | pr_err("Using default/backup EDID\n"); |
Bernie Thompson | 18dffdf | 2010-09-05 16:35:23 -0700 | [diff] [blame] | 1291 | } |
| 1292 | } |
| 1293 | } |
| 1294 | |
| 1295 | /* If we've got modes, let's pick a best default mode */ |
Bernie Thompson | 7d9485e | 2010-02-15 06:46:08 -0800 | [diff] [blame] | 1296 | if (info->monspecs.modedb_len > 0) { |
| 1297 | |
| 1298 | for (i = 0; i < info->monspecs.modedb_len; i++) { |
| 1299 | if (dlfb_is_valid_mode(&info->monspecs.modedb[i], info)) |
| 1300 | fb_add_videomode(&info->monspecs.modedb[i], |
| 1301 | &info->modelist); |
William Katsak | 9377c51 | 2011-06-23 13:16:29 +0000 | [diff] [blame] | 1302 | else { |
| 1303 | if (i == 0) |
| 1304 | /* if we've removed top/best mode */ |
| 1305 | info->monspecs.misc |
| 1306 | &= ~FB_MISC_1ST_DETAIL; |
| 1307 | } |
Bernie Thompson | 7d9485e | 2010-02-15 06:46:08 -0800 | [diff] [blame] | 1308 | } |
| 1309 | |
| 1310 | default_vmode = fb_find_best_display(&info->monspecs, |
| 1311 | &info->modelist); |
Bernie Thompson | 18dffdf | 2010-09-05 16:35:23 -0700 | [diff] [blame] | 1312 | } |
Bernie Thompson | 7d9485e | 2010-02-15 06:46:08 -0800 | [diff] [blame] | 1313 | |
Bernie Thompson | 18dffdf | 2010-09-05 16:35:23 -0700 | [diff] [blame] | 1314 | /* If everything else has failed, fall back to safe default mode */ |
| 1315 | if (default_vmode == NULL) { |
| 1316 | |
| 1317 | struct fb_videomode fb_vmode = {0}; |
Bernie Thompson | 7d9485e | 2010-02-15 06:46:08 -0800 | [diff] [blame] | 1318 | |
| 1319 | /* |
| 1320 | * Add the standard VESA modes to our modelist |
| 1321 | * Since we don't have EDID, there may be modes that |
| 1322 | * overspec monitor and/or are incorrect aspect ratio, etc. |
| 1323 | * But at least the user has a chance to choose |
| 1324 | */ |
| 1325 | for (i = 0; i < VESA_MODEDB_SIZE; i++) { |
| 1326 | if (dlfb_is_valid_mode((struct fb_videomode *) |
| 1327 | &vesa_modes[i], info)) |
| 1328 | fb_add_videomode(&vesa_modes[i], |
| 1329 | &info->modelist); |
| 1330 | } |
| 1331 | |
| 1332 | /* |
| 1333 | * default to resolution safe for projectors |
| 1334 | * (since they are most common case without EDID) |
| 1335 | */ |
| 1336 | fb_vmode.xres = 800; |
| 1337 | fb_vmode.yres = 600; |
| 1338 | fb_vmode.refresh = 60; |
| 1339 | default_vmode = fb_find_nearest_mode(&fb_vmode, |
| 1340 | &info->modelist); |
| 1341 | } |
| 1342 | |
Bernie Thompson | 18dffdf | 2010-09-05 16:35:23 -0700 | [diff] [blame] | 1343 | /* If we have good mode and no active clients*/ |
| 1344 | if ((default_vmode != NULL) && (dev->fb_count == 0)) { |
| 1345 | |
| 1346 | fb_videomode_to_var(&info->var, default_vmode); |
| 1347 | dlfb_var_color_format(&info->var); |
| 1348 | |
| 1349 | /* |
| 1350 | * with mode size info, we can now alloc our framebuffer. |
| 1351 | */ |
| 1352 | memcpy(&info->fix, &dlfb_fix, sizeof(dlfb_fix)); |
| 1353 | info->fix.line_length = info->var.xres * |
| 1354 | (info->var.bits_per_pixel / 8); |
| 1355 | |
| 1356 | result = dlfb_realloc_framebuffer(dev, info); |
| 1357 | |
| 1358 | } else |
| 1359 | result = -EINVAL; |
| 1360 | |
| 1361 | error: |
| 1362 | if (edid && (dev->edid != edid)) |
| 1363 | kfree(edid); |
| 1364 | |
| 1365 | if (info->dev) |
| 1366 | mutex_unlock(&info->lock); |
Bernie Thompson | 7d9485e | 2010-02-15 06:46:08 -0800 | [diff] [blame] | 1367 | |
| 1368 | return result; |
| 1369 | } |
| 1370 | |
| 1371 | static ssize_t metrics_bytes_rendered_show(struct device *fbdev, |
| 1372 | struct device_attribute *a, char *buf) { |
| 1373 | struct fb_info *fb_info = dev_get_drvdata(fbdev); |
| 1374 | struct dlfb_data *dev = fb_info->par; |
| 1375 | return snprintf(buf, PAGE_SIZE, "%u\n", |
| 1376 | atomic_read(&dev->bytes_rendered)); |
| 1377 | } |
| 1378 | |
| 1379 | static ssize_t metrics_bytes_identical_show(struct device *fbdev, |
| 1380 | struct device_attribute *a, char *buf) { |
| 1381 | struct fb_info *fb_info = dev_get_drvdata(fbdev); |
| 1382 | struct dlfb_data *dev = fb_info->par; |
| 1383 | return snprintf(buf, PAGE_SIZE, "%u\n", |
| 1384 | atomic_read(&dev->bytes_identical)); |
| 1385 | } |
| 1386 | |
| 1387 | static ssize_t metrics_bytes_sent_show(struct device *fbdev, |
| 1388 | struct device_attribute *a, char *buf) { |
| 1389 | struct fb_info *fb_info = dev_get_drvdata(fbdev); |
| 1390 | struct dlfb_data *dev = fb_info->par; |
| 1391 | return snprintf(buf, PAGE_SIZE, "%u\n", |
| 1392 | atomic_read(&dev->bytes_sent)); |
| 1393 | } |
| 1394 | |
| 1395 | static ssize_t metrics_cpu_kcycles_used_show(struct device *fbdev, |
| 1396 | struct device_attribute *a, char *buf) { |
| 1397 | struct fb_info *fb_info = dev_get_drvdata(fbdev); |
| 1398 | struct dlfb_data *dev = fb_info->par; |
| 1399 | return snprintf(buf, PAGE_SIZE, "%u\n", |
| 1400 | atomic_read(&dev->cpu_kcycles_used)); |
| 1401 | } |
| 1402 | |
Bernie Thompson | 18dffdf | 2010-09-05 16:35:23 -0700 | [diff] [blame] | 1403 | static ssize_t edid_show( |
| 1404 | struct file *filp, |
| 1405 | struct kobject *kobj, struct bin_attribute *a, |
Bernie Thompson | 7d9485e | 2010-02-15 06:46:08 -0800 | [diff] [blame] | 1406 | char *buf, loff_t off, size_t count) { |
| 1407 | struct device *fbdev = container_of(kobj, struct device, kobj); |
| 1408 | struct fb_info *fb_info = dev_get_drvdata(fbdev); |
| 1409 | struct dlfb_data *dev = fb_info->par; |
Bernie Thompson | 7d9485e | 2010-02-15 06:46:08 -0800 | [diff] [blame] | 1410 | |
Bernie Thompson | 18dffdf | 2010-09-05 16:35:23 -0700 | [diff] [blame] | 1411 | if (dev->edid == NULL) |
Bernie Thompson | 7d9485e | 2010-02-15 06:46:08 -0800 | [diff] [blame] | 1412 | return 0; |
| 1413 | |
Bernie Thompson | 18dffdf | 2010-09-05 16:35:23 -0700 | [diff] [blame] | 1414 | if ((off >= dev->edid_size) || (count > dev->edid_size)) |
Bernie Thompson | 7d9485e | 2010-02-15 06:46:08 -0800 | [diff] [blame] | 1415 | return 0; |
| 1416 | |
Bernie Thompson | 18dffdf | 2010-09-05 16:35:23 -0700 | [diff] [blame] | 1417 | if (off + count > dev->edid_size) |
| 1418 | count = dev->edid_size - off; |
| 1419 | |
Paul Mundt | 81f6f3c | 2011-01-06 18:07:54 +0900 | [diff] [blame] | 1420 | pr_info("sysfs edid copy %p to %p, %d bytes\n", |
Bernie Thompson | 18dffdf | 2010-09-05 16:35:23 -0700 | [diff] [blame] | 1421 | dev->edid, buf, (int) count); |
| 1422 | |
| 1423 | memcpy(buf, dev->edid, count); |
Bernie Thompson | 7d9485e | 2010-02-15 06:46:08 -0800 | [diff] [blame] | 1424 | |
| 1425 | return count; |
| 1426 | } |
| 1427 | |
Bernie Thompson | 8ef8cc4 | 2010-09-05 16:35:31 -0700 | [diff] [blame] | 1428 | static ssize_t edid_store( |
| 1429 | struct file *filp, |
| 1430 | struct kobject *kobj, struct bin_attribute *a, |
| 1431 | char *src, loff_t src_off, size_t src_size) { |
| 1432 | struct device *fbdev = container_of(kobj, struct device, kobj); |
| 1433 | struct fb_info *fb_info = dev_get_drvdata(fbdev); |
| 1434 | struct dlfb_data *dev = fb_info->par; |
Olivier Sobrie | e71ff6f | 2012-02-29 08:06:40 +0100 | [diff] [blame] | 1435 | int ret; |
Bernie Thompson | 8ef8cc4 | 2010-09-05 16:35:31 -0700 | [diff] [blame] | 1436 | |
| 1437 | /* We only support write of entire EDID at once, no offset*/ |
Paul Mundt | b9f03a3 | 2011-01-06 18:04:02 +0900 | [diff] [blame] | 1438 | if ((src_size != EDID_LENGTH) || (src_off != 0)) |
Olivier Sobrie | e71ff6f | 2012-02-29 08:06:40 +0100 | [diff] [blame] | 1439 | return -EINVAL; |
Bernie Thompson | 8ef8cc4 | 2010-09-05 16:35:31 -0700 | [diff] [blame] | 1440 | |
Olivier Sobrie | e71ff6f | 2012-02-29 08:06:40 +0100 | [diff] [blame] | 1441 | ret = dlfb_setup_modes(dev, fb_info, src, src_size); |
| 1442 | if (ret) |
| 1443 | return ret; |
Bernie Thompson | 8ef8cc4 | 2010-09-05 16:35:31 -0700 | [diff] [blame] | 1444 | |
Olivier Sobrie | e71ff6f | 2012-02-29 08:06:40 +0100 | [diff] [blame] | 1445 | if (!dev->edid || memcmp(src, dev->edid, src_size)) |
| 1446 | return -EINVAL; |
| 1447 | |
| 1448 | pr_info("sysfs written EDID is new default\n"); |
| 1449 | dlfb_ops_set_par(fb_info); |
| 1450 | return src_size; |
Bernie Thompson | 8ef8cc4 | 2010-09-05 16:35:31 -0700 | [diff] [blame] | 1451 | } |
Bernie Thompson | 7d9485e | 2010-02-15 06:46:08 -0800 | [diff] [blame] | 1452 | |
| 1453 | static ssize_t metrics_reset_store(struct device *fbdev, |
| 1454 | struct device_attribute *attr, |
| 1455 | const char *buf, size_t count) |
| 1456 | { |
| 1457 | struct fb_info *fb_info = dev_get_drvdata(fbdev); |
| 1458 | struct dlfb_data *dev = fb_info->par; |
| 1459 | |
| 1460 | atomic_set(&dev->bytes_rendered, 0); |
| 1461 | atomic_set(&dev->bytes_identical, 0); |
| 1462 | atomic_set(&dev->bytes_sent, 0); |
| 1463 | atomic_set(&dev->cpu_kcycles_used, 0); |
Bernie Thompson | 7d9485e | 2010-02-15 06:46:08 -0800 | [diff] [blame] | 1464 | |
| 1465 | return count; |
| 1466 | } |
| 1467 | |
Bernie Thompson | 7d9485e | 2010-02-15 06:46:08 -0800 | [diff] [blame] | 1468 | static struct bin_attribute edid_attr = { |
| 1469 | .attr.name = "edid", |
Bernie Thompson | 8ef8cc4 | 2010-09-05 16:35:31 -0700 | [diff] [blame] | 1470 | .attr.mode = 0666, |
Paul Mundt | b9f03a3 | 2011-01-06 18:04:02 +0900 | [diff] [blame] | 1471 | .size = EDID_LENGTH, |
Bernie Thompson | 7d9485e | 2010-02-15 06:46:08 -0800 | [diff] [blame] | 1472 | .read = edid_show, |
Bernie Thompson | 8ef8cc4 | 2010-09-05 16:35:31 -0700 | [diff] [blame] | 1473 | .write = edid_store |
Bernie Thompson | 7d9485e | 2010-02-15 06:46:08 -0800 | [diff] [blame] | 1474 | }; |
| 1475 | |
| 1476 | static struct device_attribute fb_device_attrs[] = { |
| 1477 | __ATTR_RO(metrics_bytes_rendered), |
| 1478 | __ATTR_RO(metrics_bytes_identical), |
| 1479 | __ATTR_RO(metrics_bytes_sent), |
| 1480 | __ATTR_RO(metrics_cpu_kcycles_used), |
Greg Kroah-Hartman | 926c111 | 2010-11-18 11:21:04 -0800 | [diff] [blame] | 1481 | __ATTR(metrics_reset, S_IWUSR, NULL, metrics_reset_store), |
Bernie Thompson | 7d9485e | 2010-02-15 06:46:08 -0800 | [diff] [blame] | 1482 | }; |
| 1483 | |
| 1484 | /* |
Bernie Thompson | cc403dc | 2010-02-15 06:45:49 -0800 | [diff] [blame] | 1485 | * This is necessary before we can communicate with the display controller. |
| 1486 | */ |
| 1487 | static int dlfb_select_std_channel(struct dlfb_data *dev) |
| 1488 | { |
| 1489 | int ret; |
| 1490 | u8 set_def_chn[] = { 0x57, 0xCD, 0xDC, 0xA7, |
| 1491 | 0x1C, 0x88, 0x5E, 0x15, |
| 1492 | 0x60, 0xFE, 0xC6, 0x97, |
| 1493 | 0x16, 0x3D, 0x47, 0xF2 }; |
| 1494 | |
| 1495 | ret = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0), |
| 1496 | NR_USB_REQUEST_CHANNEL, |
| 1497 | (USB_DIR_OUT | USB_TYPE_VENDOR), 0, 0, |
| 1498 | set_def_chn, sizeof(set_def_chn), USB_CTRL_SET_TIMEOUT); |
| 1499 | return ret; |
| 1500 | } |
| 1501 | |
Bernie Thompson | 18dffdf | 2010-09-05 16:35:23 -0700 | [diff] [blame] | 1502 | static int dlfb_parse_vendor_descriptor(struct dlfb_data *dev, |
Andrew Kephart | f2e1fc9 | 2011-08-21 13:34:13 -0700 | [diff] [blame] | 1503 | struct usb_interface *interface) |
Bernie Thompson | 18dffdf | 2010-09-05 16:35:23 -0700 | [diff] [blame] | 1504 | { |
| 1505 | char *desc; |
| 1506 | char *buf; |
| 1507 | char *desc_end; |
Bernie Thompson | 2469d5d | 2010-02-15 06:46:13 -0800 | [diff] [blame] | 1508 | |
Andrew Kephart | f2e1fc9 | 2011-08-21 13:34:13 -0700 | [diff] [blame] | 1509 | int total_len = 0; |
Bernie Thompson | 18dffdf | 2010-09-05 16:35:23 -0700 | [diff] [blame] | 1510 | |
| 1511 | buf = kzalloc(MAX_VENDOR_DESCRIPTOR_SIZE, GFP_KERNEL); |
| 1512 | if (!buf) |
| 1513 | return false; |
| 1514 | desc = buf; |
| 1515 | |
Andrew Kephart | f2e1fc9 | 2011-08-21 13:34:13 -0700 | [diff] [blame] | 1516 | total_len = usb_get_descriptor(interface_to_usbdev(interface), |
| 1517 | 0x5f, /* vendor specific */ |
| 1518 | 0, desc, MAX_VENDOR_DESCRIPTOR_SIZE); |
| 1519 | |
| 1520 | /* if not found, look in configuration descriptor */ |
| 1521 | if (total_len < 0) { |
| 1522 | if (0 == usb_get_extra_descriptor(interface->cur_altsetting, |
| 1523 | 0x5f, &desc)) |
| 1524 | total_len = (int) desc[0]; |
| 1525 | } |
| 1526 | |
Bernie Thompson | 18dffdf | 2010-09-05 16:35:23 -0700 | [diff] [blame] | 1527 | if (total_len > 5) { |
Paul Mundt | 81f6f3c | 2011-01-06 18:07:54 +0900 | [diff] [blame] | 1528 | pr_info("vendor descriptor length:%x data:%02x %02x %02x %02x" \ |
Bernie Thompson | 18dffdf | 2010-09-05 16:35:23 -0700 | [diff] [blame] | 1529 | "%02x %02x %02x %02x %02x %02x %02x\n", |
| 1530 | total_len, desc[0], |
| 1531 | desc[1], desc[2], desc[3], desc[4], desc[5], desc[6], |
| 1532 | desc[7], desc[8], desc[9], desc[10]); |
| 1533 | |
| 1534 | if ((desc[0] != total_len) || /* descriptor length */ |
| 1535 | (desc[1] != 0x5f) || /* vendor descriptor type */ |
| 1536 | (desc[2] != 0x01) || /* version (2 bytes) */ |
| 1537 | (desc[3] != 0x00) || |
| 1538 | (desc[4] != total_len - 2)) /* length after type */ |
| 1539 | goto unrecognized; |
| 1540 | |
| 1541 | desc_end = desc + total_len; |
| 1542 | desc += 5; /* the fixed header we've already parsed */ |
| 1543 | |
| 1544 | while (desc < desc_end) { |
| 1545 | u8 length; |
| 1546 | u16 key; |
| 1547 | |
Ben Collins | b49f184 | 2012-03-03 12:43:27 -0800 | [diff] [blame] | 1548 | key = le16_to_cpu(*((u16 *) desc)); |
Bernie Thompson | 18dffdf | 2010-09-05 16:35:23 -0700 | [diff] [blame] | 1549 | desc += sizeof(u16); |
| 1550 | length = *desc; |
| 1551 | desc++; |
| 1552 | |
| 1553 | switch (key) { |
| 1554 | case 0x0200: { /* max_area */ |
| 1555 | u32 max_area; |
| 1556 | max_area = le32_to_cpu(*((u32 *)desc)); |
Paul Mundt | 81f6f3c | 2011-01-06 18:07:54 +0900 | [diff] [blame] | 1557 | pr_warn("DL chip limited to %d pixel modes\n", |
Bernie Thompson | 18dffdf | 2010-09-05 16:35:23 -0700 | [diff] [blame] | 1558 | max_area); |
| 1559 | dev->sku_pixel_limit = max_area; |
| 1560 | break; |
| 1561 | } |
| 1562 | default: |
| 1563 | break; |
| 1564 | } |
| 1565 | desc += length; |
| 1566 | } |
Andrew Kephart | f2e1fc9 | 2011-08-21 13:34:13 -0700 | [diff] [blame] | 1567 | } else { |
| 1568 | pr_info("vendor descriptor not available (%d)\n", total_len); |
Bernie Thompson | 18dffdf | 2010-09-05 16:35:23 -0700 | [diff] [blame] | 1569 | } |
| 1570 | |
| 1571 | goto success; |
| 1572 | |
| 1573 | unrecognized: |
| 1574 | /* allow udlfb to load for now even if firmware unrecognized */ |
Paul Mundt | 81f6f3c | 2011-01-06 18:07:54 +0900 | [diff] [blame] | 1575 | pr_err("Unrecognized vendor firmware descriptor\n"); |
Bernie Thompson | 18dffdf | 2010-09-05 16:35:23 -0700 | [diff] [blame] | 1576 | |
| 1577 | success: |
| 1578 | kfree(buf); |
| 1579 | return true; |
| 1580 | } |
Bernie Thompson | 8d21547 | 2012-03-01 17:35:48 -0800 | [diff] [blame] | 1581 | |
| 1582 | static void dlfb_init_framebuffer_work(struct work_struct *work); |
| 1583 | |
Bernie Thompson | 2469d5d | 2010-02-15 06:46:13 -0800 | [diff] [blame] | 1584 | static int dlfb_usb_probe(struct usb_interface *interface, |
Bernie Thompson | 59277b6 | 2009-11-24 15:52:21 -0800 | [diff] [blame] | 1585 | const struct usb_device_id *id) |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 1586 | { |
Bernie Thompson | 59277b6 | 2009-11-24 15:52:21 -0800 | [diff] [blame] | 1587 | struct usb_device *usbdev; |
Bernie Thompson | 18dffdf | 2010-09-05 16:35:23 -0700 | [diff] [blame] | 1588 | struct dlfb_data *dev = 0; |
Bernie Thompson | 59277b6 | 2009-11-24 15:52:21 -0800 | [diff] [blame] | 1589 | int retval = -ENOMEM; |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 1590 | |
Bernie Thompson | 2469d5d | 2010-02-15 06:46:13 -0800 | [diff] [blame] | 1591 | /* usb initialization */ |
| 1592 | |
| 1593 | usbdev = interface_to_usbdev(interface); |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 1594 | |
Bernie Thompson | 59277b6 | 2009-11-24 15:52:21 -0800 | [diff] [blame] | 1595 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
| 1596 | if (dev == NULL) { |
Greg Kroah-Hartman | fee5952 | 2012-04-25 12:37:49 -0700 | [diff] [blame] | 1597 | dev_err(&interface->dev, "dlfb_usb_probe: failed alloc of dev struct\n"); |
Bernie Thompson | 2469d5d | 2010-02-15 06:46:13 -0800 | [diff] [blame] | 1598 | goto error; |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 1599 | } |
| 1600 | |
Bernie Thompson | 2469d5d | 2010-02-15 06:46:13 -0800 | [diff] [blame] | 1601 | kref_init(&dev->kref); /* matching kref_put in usb .disconnect fn */ |
Bernie Thompson | 2469d5d | 2010-02-15 06:46:13 -0800 | [diff] [blame] | 1602 | |
Bernie Thompson | 59277b6 | 2009-11-24 15:52:21 -0800 | [diff] [blame] | 1603 | dev->udev = usbdev; |
Bernie Thompson | 4a4854d | 2010-02-15 06:45:55 -0800 | [diff] [blame] | 1604 | dev->gdev = &usbdev->dev; /* our generic struct device * */ |
Bernie Thompson | 59277b6 | 2009-11-24 15:52:21 -0800 | [diff] [blame] | 1605 | usb_set_intfdata(interface, dev); |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 1606 | |
Paul Mundt | 81f6f3c | 2011-01-06 18:07:54 +0900 | [diff] [blame] | 1607 | pr_info("%s %s - serial #%s\n", |
Bernie Thompson | 18dffdf | 2010-09-05 16:35:23 -0700 | [diff] [blame] | 1608 | usbdev->manufacturer, usbdev->product, usbdev->serial); |
Paul Mundt | 81f6f3c | 2011-01-06 18:07:54 +0900 | [diff] [blame] | 1609 | pr_info("vid_%04x&pid_%04x&rev_%04x driver's dlfb_data struct at %p\n", |
Bernie Thompson | 18dffdf | 2010-09-05 16:35:23 -0700 | [diff] [blame] | 1610 | usbdev->descriptor.idVendor, usbdev->descriptor.idProduct, |
| 1611 | usbdev->descriptor.bcdDevice, dev); |
Paul Mundt | 81f6f3c | 2011-01-06 18:07:54 +0900 | [diff] [blame] | 1612 | pr_info("console enable=%d\n", console); |
| 1613 | pr_info("fb_defio enable=%d\n", fb_defio); |
Stuart Hopkins | d318954 | 2011-08-21 13:34:17 -0700 | [diff] [blame] | 1614 | pr_info("shadow enable=%d\n", shadow); |
Bernie Thompson | 18dffdf | 2010-09-05 16:35:23 -0700 | [diff] [blame] | 1615 | |
| 1616 | dev->sku_pixel_limit = 2048 * 1152; /* default to maximum */ |
| 1617 | |
Andrew Kephart | f2e1fc9 | 2011-08-21 13:34:13 -0700 | [diff] [blame] | 1618 | if (!dlfb_parse_vendor_descriptor(dev, interface)) { |
Paul Mundt | 81f6f3c | 2011-01-06 18:07:54 +0900 | [diff] [blame] | 1619 | pr_err("firmware not recognized. Assume incompatible device\n"); |
Bernie Thompson | 18dffdf | 2010-09-05 16:35:23 -0700 | [diff] [blame] | 1620 | goto error; |
| 1621 | } |
| 1622 | |
Ben Collins | 664c5f1 | 2012-03-03 12:57:37 -0800 | [diff] [blame] | 1623 | if (pixel_limit) { |
| 1624 | pr_warn("DL chip limit of %d overriden" |
| 1625 | " by module param to %d\n", |
| 1626 | dev->sku_pixel_limit, pixel_limit); |
| 1627 | dev->sku_pixel_limit = pixel_limit; |
| 1628 | } |
| 1629 | |
| 1630 | |
Bernie Thompson | 2469d5d | 2010-02-15 06:46:13 -0800 | [diff] [blame] | 1631 | if (!dlfb_alloc_urb_list(dev, WRITES_IN_FLIGHT, MAX_TRANSFER)) { |
| 1632 | retval = -ENOMEM; |
Paul Mundt | 81f6f3c | 2011-01-06 18:07:54 +0900 | [diff] [blame] | 1633 | pr_err("dlfb_alloc_urb_list failed\n"); |
Bernie Thompson | 2469d5d | 2010-02-15 06:46:13 -0800 | [diff] [blame] | 1634 | goto error; |
| 1635 | } |
| 1636 | |
Bernie Thompson | 8d21547 | 2012-03-01 17:35:48 -0800 | [diff] [blame] | 1637 | kref_get(&dev->kref); /* matching kref_put in free_framebuffer_work */ |
| 1638 | |
Bernie Thompson | 2469d5d | 2010-02-15 06:46:13 -0800 | [diff] [blame] | 1639 | /* We don't register a new USB class. Our client interface is fbdev */ |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 1640 | |
Bernie Thompson | 8d21547 | 2012-03-01 17:35:48 -0800 | [diff] [blame] | 1641 | /* Workitem keep things fast & simple during USB enumeration */ |
| 1642 | INIT_DELAYED_WORK(&dev->init_framebuffer_work, |
| 1643 | dlfb_init_framebuffer_work); |
| 1644 | schedule_delayed_work(&dev->init_framebuffer_work, 0); |
| 1645 | |
| 1646 | return 0; |
| 1647 | |
| 1648 | error: |
| 1649 | if (dev) { |
| 1650 | |
| 1651 | kref_put(&dev->kref, dlfb_free); /* ref for framebuffer */ |
| 1652 | kref_put(&dev->kref, dlfb_free); /* last ref from kref_init */ |
| 1653 | |
| 1654 | /* dev has been deallocated. Do not dereference */ |
| 1655 | } |
| 1656 | |
| 1657 | return retval; |
| 1658 | } |
| 1659 | |
| 1660 | static void dlfb_init_framebuffer_work(struct work_struct *work) |
| 1661 | { |
| 1662 | struct dlfb_data *dev = container_of(work, struct dlfb_data, |
| 1663 | init_framebuffer_work.work); |
| 1664 | struct fb_info *info; |
| 1665 | int retval; |
| 1666 | int i; |
| 1667 | |
Bernie Thompson | 2469d5d | 2010-02-15 06:46:13 -0800 | [diff] [blame] | 1668 | /* allocates framebuffer driver structure, not framebuffer memory */ |
Bernie Thompson | 8d21547 | 2012-03-01 17:35:48 -0800 | [diff] [blame] | 1669 | info = framebuffer_alloc(0, dev->gdev); |
Bernie Thompson | 2469d5d | 2010-02-15 06:46:13 -0800 | [diff] [blame] | 1670 | if (!info) { |
| 1671 | retval = -ENOMEM; |
Paul Mundt | 81f6f3c | 2011-01-06 18:07:54 +0900 | [diff] [blame] | 1672 | pr_err("framebuffer_alloc failed\n"); |
Bernie Thompson | 2469d5d | 2010-02-15 06:46:13 -0800 | [diff] [blame] | 1673 | goto error; |
| 1674 | } |
Bernie Thompson | 33077b8 | 2010-09-05 16:35:19 -0700 | [diff] [blame] | 1675 | |
Bernie Thompson | 59277b6 | 2009-11-24 15:52:21 -0800 | [diff] [blame] | 1676 | dev->info = info; |
| 1677 | info->par = dev; |
| 1678 | info->pseudo_palette = dev->pseudo_palette; |
Bernie Thompson | 2469d5d | 2010-02-15 06:46:13 -0800 | [diff] [blame] | 1679 | info->fbops = &dlfb_ops; |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 1680 | |
Bernie Thompson | 59277b6 | 2009-11-24 15:52:21 -0800 | [diff] [blame] | 1681 | retval = fb_alloc_cmap(&info->cmap, 256, 0); |
| 1682 | if (retval < 0) { |
Paul Mundt | 81f6f3c | 2011-01-06 18:07:54 +0900 | [diff] [blame] | 1683 | pr_err("fb_alloc_cmap failed %x\n", retval); |
Bernie Thompson | 2469d5d | 2010-02-15 06:46:13 -0800 | [diff] [blame] | 1684 | goto error; |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 1685 | } |
| 1686 | |
Bernie Thompson | 33077b8 | 2010-09-05 16:35:19 -0700 | [diff] [blame] | 1687 | INIT_DELAYED_WORK(&dev->free_framebuffer_work, |
| 1688 | dlfb_free_framebuffer_work); |
| 1689 | |
Bernie Thompson | 18dffdf | 2010-09-05 16:35:23 -0700 | [diff] [blame] | 1690 | INIT_LIST_HEAD(&info->modelist); |
| 1691 | |
| 1692 | retval = dlfb_setup_modes(dev, info, NULL, 0); |
| 1693 | if (retval != 0) { |
Paul Mundt | 81f6f3c | 2011-01-06 18:07:54 +0900 | [diff] [blame] | 1694 | pr_err("unable to find common mode for display and adapter\n"); |
Bernie Thompson | 18dffdf | 2010-09-05 16:35:23 -0700 | [diff] [blame] | 1695 | goto error; |
| 1696 | } |
| 1697 | |
Bernie Thompson | 2469d5d | 2010-02-15 06:46:13 -0800 | [diff] [blame] | 1698 | /* ready to begin using device */ |
| 1699 | |
Bernie Thompson | 2469d5d | 2010-02-15 06:46:13 -0800 | [diff] [blame] | 1700 | atomic_set(&dev->usb_active, 1); |
Bernie Thompson | 59277b6 | 2009-11-24 15:52:21 -0800 | [diff] [blame] | 1701 | dlfb_select_std_channel(dev); |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 1702 | |
Bernie Thompson | 18dffdf | 2010-09-05 16:35:23 -0700 | [diff] [blame] | 1703 | dlfb_ops_check_var(&info->var, info); |
Bernie Thompson | 2469d5d | 2010-02-15 06:46:13 -0800 | [diff] [blame] | 1704 | dlfb_ops_set_par(info); |
| 1705 | |
Bernie Thompson | 59277b6 | 2009-11-24 15:52:21 -0800 | [diff] [blame] | 1706 | retval = register_framebuffer(info); |
Bernie Thompson | 2469d5d | 2010-02-15 06:46:13 -0800 | [diff] [blame] | 1707 | if (retval < 0) { |
Paul Mundt | 81f6f3c | 2011-01-06 18:07:54 +0900 | [diff] [blame] | 1708 | pr_err("register_framebuffer failed %d\n", retval); |
Bernie Thompson | 2469d5d | 2010-02-15 06:46:13 -0800 | [diff] [blame] | 1709 | goto error; |
| 1710 | } |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 1711 | |
Liu Yuan | 94cd1ae | 2011-04-18 10:44:38 +0000 | [diff] [blame] | 1712 | for (i = 0; i < ARRAY_SIZE(fb_device_attrs); i++) { |
| 1713 | retval = device_create_file(info->dev, &fb_device_attrs[i]); |
| 1714 | if (retval) { |
Bernie Thompson | 8d21547 | 2012-03-01 17:35:48 -0800 | [diff] [blame] | 1715 | pr_warn("device_create_file failed %d\n", retval); |
Liu Yuan | 94cd1ae | 2011-04-18 10:44:38 +0000 | [diff] [blame] | 1716 | } |
| 1717 | } |
Greg Kroah-Hartman | f05e057 | 2009-06-03 14:47:08 -0700 | [diff] [blame] | 1718 | |
Liu Yuan | 94cd1ae | 2011-04-18 10:44:38 +0000 | [diff] [blame] | 1719 | retval = device_create_bin_file(info->dev, &edid_attr); |
| 1720 | if (retval) { |
Bernie Thompson | 8d21547 | 2012-03-01 17:35:48 -0800 | [diff] [blame] | 1721 | pr_warn("device_create_bin_file failed %d\n", retval); |
Liu Yuan | 94cd1ae | 2011-04-18 10:44:38 +0000 | [diff] [blame] | 1722 | } |
Bernie Thompson | 2469d5d | 2010-02-15 06:46:13 -0800 | [diff] [blame] | 1723 | |
Paul Mundt | 81f6f3c | 2011-01-06 18:07:54 +0900 | [diff] [blame] | 1724 | pr_info("DisplayLink USB device /dev/fb%d attached. %dx%d resolution." |
Bernie Thompson | 2469d5d | 2010-02-15 06:46:13 -0800 | [diff] [blame] | 1725 | " Using %dK framebuffer memory\n", info->node, |
Bernie Thompson | 18dffdf | 2010-09-05 16:35:23 -0700 | [diff] [blame] | 1726 | info->var.xres, info->var.yres, |
Bernie Thompson | 2469d5d | 2010-02-15 06:46:13 -0800 | [diff] [blame] | 1727 | ((dev->backing_buffer) ? |
Bernie Thompson | 18dffdf | 2010-09-05 16:35:23 -0700 | [diff] [blame] | 1728 | info->fix.smem_len * 2 : info->fix.smem_len) >> 10); |
Bernie Thompson | 8d21547 | 2012-03-01 17:35:48 -0800 | [diff] [blame] | 1729 | return; |
Liu Yuan | 94cd1ae | 2011-04-18 10:44:38 +0000 | [diff] [blame] | 1730 | |
Bernie Thompson | 2469d5d | 2010-02-15 06:46:13 -0800 | [diff] [blame] | 1731 | error: |
Bernie Thompson | 8d21547 | 2012-03-01 17:35:48 -0800 | [diff] [blame] | 1732 | dlfb_free_framebuffer(dev); |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 1733 | } |
| 1734 | |
Bernie Thompson | 2469d5d | 2010-02-15 06:46:13 -0800 | [diff] [blame] | 1735 | static void dlfb_usb_disconnect(struct usb_interface *interface) |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 1736 | { |
Bernie Thompson | 59277b6 | 2009-11-24 15:52:21 -0800 | [diff] [blame] | 1737 | struct dlfb_data *dev; |
| 1738 | struct fb_info *info; |
Bernie Thompson | 2469d5d | 2010-02-15 06:46:13 -0800 | [diff] [blame] | 1739 | int i; |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 1740 | |
Bernie Thompson | 59277b6 | 2009-11-24 15:52:21 -0800 | [diff] [blame] | 1741 | dev = usb_get_intfdata(interface); |
Bernie Thompson | 59277b6 | 2009-11-24 15:52:21 -0800 | [diff] [blame] | 1742 | info = dev->info; |
Bernie Thompson | 2469d5d | 2010-02-15 06:46:13 -0800 | [diff] [blame] | 1743 | |
Paul Mundt | 81f6f3c | 2011-01-06 18:07:54 +0900 | [diff] [blame] | 1744 | pr_info("USB disconnect starting\n"); |
Bernie Thompson | 33077b8 | 2010-09-05 16:35:19 -0700 | [diff] [blame] | 1745 | |
| 1746 | /* we virtualize until all fb clients release. Then we free */ |
| 1747 | dev->virtualized = true; |
| 1748 | |
| 1749 | /* When non-active we'll update virtual framebuffer, but no new urbs */ |
Bernie Thompson | 2469d5d | 2010-02-15 06:46:13 -0800 | [diff] [blame] | 1750 | atomic_set(&dev->usb_active, 0); |
| 1751 | |
Bernie Thompson | 8d21547 | 2012-03-01 17:35:48 -0800 | [diff] [blame] | 1752 | /* this function will wait for all in-flight urbs to complete */ |
| 1753 | dlfb_free_urb_list(dev); |
| 1754 | |
| 1755 | if (info) { |
Bernie Thompson | 8d21547 | 2012-03-01 17:35:48 -0800 | [diff] [blame] | 1756 | /* remove udlfb's sysfs interfaces */ |
| 1757 | for (i = 0; i < ARRAY_SIZE(fb_device_attrs); i++) |
| 1758 | device_remove_file(info->dev, &fb_device_attrs[i]); |
| 1759 | device_remove_bin_file(info->dev, &edid_attr); |
Bernie Thompson | 8d21547 | 2012-03-01 17:35:48 -0800 | [diff] [blame] | 1760 | unlink_framebuffer(info); |
| 1761 | } |
| 1762 | |
Bernie Thompson | 2469d5d | 2010-02-15 06:46:13 -0800 | [diff] [blame] | 1763 | usb_set_intfdata(interface, NULL); |
Bernie Thompson | 8d21547 | 2012-03-01 17:35:48 -0800 | [diff] [blame] | 1764 | dev->udev = NULL; |
| 1765 | dev->gdev = NULL; |
Bernie Thompson | 2469d5d | 2010-02-15 06:46:13 -0800 | [diff] [blame] | 1766 | |
Bernie Thompson | 33077b8 | 2010-09-05 16:35:19 -0700 | [diff] [blame] | 1767 | /* if clients still have us open, will be freed on last close */ |
| 1768 | if (dev->fb_count == 0) |
| 1769 | schedule_delayed_work(&dev->free_framebuffer_work, 0); |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 1770 | |
Bernie Thompson | 2469d5d | 2010-02-15 06:46:13 -0800 | [diff] [blame] | 1771 | /* release reference taken by kref_init in probe() */ |
Bernie Thompson | 33077b8 | 2010-09-05 16:35:19 -0700 | [diff] [blame] | 1772 | kref_put(&dev->kref, dlfb_free); |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 1773 | |
Bernie Thompson | 2469d5d | 2010-02-15 06:46:13 -0800 | [diff] [blame] | 1774 | /* consider dlfb_data freed */ |
| 1775 | |
| 1776 | return; |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 1777 | } |
| 1778 | |
| 1779 | static struct usb_driver dlfb_driver = { |
| 1780 | .name = "udlfb", |
Bernie Thompson | 2469d5d | 2010-02-15 06:46:13 -0800 | [diff] [blame] | 1781 | .probe = dlfb_usb_probe, |
| 1782 | .disconnect = dlfb_usb_disconnect, |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 1783 | .id_table = id_table, |
| 1784 | }; |
| 1785 | |
Greg Kroah-Hartman | fe74848 | 2011-11-18 09:52:10 -0800 | [diff] [blame] | 1786 | module_usb_driver(dlfb_driver); |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 1787 | |
Bernie Thompson | 4a4854d | 2010-02-15 06:45:55 -0800 | [diff] [blame] | 1788 | static void dlfb_urb_completion(struct urb *urb) |
| 1789 | { |
| 1790 | struct urb_node *unode = urb->context; |
| 1791 | struct dlfb_data *dev = unode->dev; |
| 1792 | unsigned long flags; |
| 1793 | |
| 1794 | /* sync/async unlink faults aren't errors */ |
| 1795 | if (urb->status) { |
| 1796 | if (!(urb->status == -ENOENT || |
| 1797 | urb->status == -ECONNRESET || |
| 1798 | urb->status == -ESHUTDOWN)) { |
Paul Mundt | 81f6f3c | 2011-01-06 18:07:54 +0900 | [diff] [blame] | 1799 | pr_err("%s - nonzero write bulk status received: %d\n", |
Bernie Thompson | 4a4854d | 2010-02-15 06:45:55 -0800 | [diff] [blame] | 1800 | __func__, urb->status); |
| 1801 | atomic_set(&dev->lost_pixels, 1); |
| 1802 | } |
| 1803 | } |
| 1804 | |
| 1805 | urb->transfer_buffer_length = dev->urbs.size; /* reset to actual */ |
| 1806 | |
| 1807 | spin_lock_irqsave(&dev->urbs.lock, flags); |
| 1808 | list_add_tail(&unode->entry, &dev->urbs.list); |
| 1809 | dev->urbs.available++; |
| 1810 | spin_unlock_irqrestore(&dev->urbs.lock, flags); |
| 1811 | |
Bernie Thompson | 5bea1fb | 2010-09-05 18:28:29 -0700 | [diff] [blame] | 1812 | /* |
| 1813 | * When using fb_defio, we deadlock if up() is called |
| 1814 | * while another is waiting. So queue to another process. |
| 1815 | */ |
| 1816 | if (fb_defio) |
| 1817 | schedule_delayed_work(&unode->release_urb_work, 0); |
| 1818 | else |
| 1819 | up(&dev->urbs.limit_sem); |
Bernie Thompson | 4a4854d | 2010-02-15 06:45:55 -0800 | [diff] [blame] | 1820 | } |
| 1821 | |
| 1822 | static void dlfb_free_urb_list(struct dlfb_data *dev) |
| 1823 | { |
| 1824 | int count = dev->urbs.count; |
| 1825 | struct list_head *node; |
| 1826 | struct urb_node *unode; |
| 1827 | struct urb *urb; |
| 1828 | int ret; |
| 1829 | unsigned long flags; |
| 1830 | |
Bernie Thompson | 8d21547 | 2012-03-01 17:35:48 -0800 | [diff] [blame] | 1831 | pr_notice("Freeing all render urbs\n"); |
Bernie Thompson | 4a4854d | 2010-02-15 06:45:55 -0800 | [diff] [blame] | 1832 | |
| 1833 | /* keep waiting and freeing, until we've got 'em all */ |
| 1834 | while (count--) { |
Bernie Thompson | 33077b8 | 2010-09-05 16:35:19 -0700 | [diff] [blame] | 1835 | |
Bernie Thompson | 8d21547 | 2012-03-01 17:35:48 -0800 | [diff] [blame] | 1836 | /* Getting interrupted means a leak, but ok at disconnect */ |
Bernie Thompson | 33077b8 | 2010-09-05 16:35:19 -0700 | [diff] [blame] | 1837 | ret = down_interruptible(&dev->urbs.limit_sem); |
| 1838 | if (ret) |
Bernie Thompson | 4a4854d | 2010-02-15 06:45:55 -0800 | [diff] [blame] | 1839 | break; |
Bernie Thompson | 33077b8 | 2010-09-05 16:35:19 -0700 | [diff] [blame] | 1840 | |
Bernie Thompson | 4a4854d | 2010-02-15 06:45:55 -0800 | [diff] [blame] | 1841 | spin_lock_irqsave(&dev->urbs.lock, flags); |
| 1842 | |
| 1843 | node = dev->urbs.list.next; /* have reserved one with sem */ |
| 1844 | list_del_init(node); |
| 1845 | |
| 1846 | spin_unlock_irqrestore(&dev->urbs.lock, flags); |
| 1847 | |
| 1848 | unode = list_entry(node, struct urb_node, entry); |
| 1849 | urb = unode->urb; |
| 1850 | |
| 1851 | /* Free each separately allocated piece */ |
Greg Kroah-Hartman | c220cc3 | 2010-04-29 15:36:29 -0700 | [diff] [blame] | 1852 | usb_free_coherent(urb->dev, dev->urbs.size, |
| 1853 | urb->transfer_buffer, urb->transfer_dma); |
Bernie Thompson | 4a4854d | 2010-02-15 06:45:55 -0800 | [diff] [blame] | 1854 | usb_free_urb(urb); |
| 1855 | kfree(node); |
| 1856 | } |
| 1857 | |
Bernie Thompson | 8d21547 | 2012-03-01 17:35:48 -0800 | [diff] [blame] | 1858 | dev->urbs.count = 0; |
Bernie Thompson | 4a4854d | 2010-02-15 06:45:55 -0800 | [diff] [blame] | 1859 | } |
| 1860 | |
| 1861 | static int dlfb_alloc_urb_list(struct dlfb_data *dev, int count, size_t size) |
| 1862 | { |
| 1863 | int i = 0; |
| 1864 | struct urb *urb; |
| 1865 | struct urb_node *unode; |
| 1866 | char *buf; |
| 1867 | |
| 1868 | spin_lock_init(&dev->urbs.lock); |
| 1869 | |
| 1870 | dev->urbs.size = size; |
| 1871 | INIT_LIST_HEAD(&dev->urbs.list); |
| 1872 | |
| 1873 | while (i < count) { |
| 1874 | unode = kzalloc(sizeof(struct urb_node), GFP_KERNEL); |
| 1875 | if (!unode) |
| 1876 | break; |
| 1877 | unode->dev = dev; |
| 1878 | |
Bernie Thompson | 5bea1fb | 2010-09-05 18:28:29 -0700 | [diff] [blame] | 1879 | INIT_DELAYED_WORK(&unode->release_urb_work, |
| 1880 | dlfb_release_urb_work); |
| 1881 | |
Bernie Thompson | 4a4854d | 2010-02-15 06:45:55 -0800 | [diff] [blame] | 1882 | urb = usb_alloc_urb(0, GFP_KERNEL); |
| 1883 | if (!urb) { |
| 1884 | kfree(unode); |
| 1885 | break; |
| 1886 | } |
| 1887 | unode->urb = urb; |
| 1888 | |
Greg Kroah-Hartman | c220cc3 | 2010-04-29 15:36:29 -0700 | [diff] [blame] | 1889 | buf = usb_alloc_coherent(dev->udev, MAX_TRANSFER, GFP_KERNEL, |
| 1890 | &urb->transfer_dma); |
Bernie Thompson | 4a4854d | 2010-02-15 06:45:55 -0800 | [diff] [blame] | 1891 | if (!buf) { |
| 1892 | kfree(unode); |
| 1893 | usb_free_urb(urb); |
| 1894 | break; |
| 1895 | } |
| 1896 | |
| 1897 | /* urb->transfer_buffer_length set to actual before submit */ |
| 1898 | usb_fill_bulk_urb(urb, dev->udev, usb_sndbulkpipe(dev->udev, 1), |
| 1899 | buf, size, dlfb_urb_completion, unode); |
| 1900 | urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; |
| 1901 | |
| 1902 | list_add_tail(&unode->entry, &dev->urbs.list); |
| 1903 | |
| 1904 | i++; |
| 1905 | } |
| 1906 | |
| 1907 | sema_init(&dev->urbs.limit_sem, i); |
| 1908 | dev->urbs.count = i; |
| 1909 | dev->urbs.available = i; |
| 1910 | |
Paul Mundt | 81f6f3c | 2011-01-06 18:07:54 +0900 | [diff] [blame] | 1911 | pr_notice("allocated %d %d byte urbs\n", i, (int) size); |
Bernie Thompson | 4a4854d | 2010-02-15 06:45:55 -0800 | [diff] [blame] | 1912 | |
| 1913 | return i; |
| 1914 | } |
| 1915 | |
| 1916 | static struct urb *dlfb_get_urb(struct dlfb_data *dev) |
| 1917 | { |
| 1918 | int ret = 0; |
| 1919 | struct list_head *entry; |
| 1920 | struct urb_node *unode; |
| 1921 | struct urb *urb = NULL; |
| 1922 | unsigned long flags; |
| 1923 | |
| 1924 | /* Wait for an in-flight buffer to complete and get re-queued */ |
| 1925 | ret = down_timeout(&dev->urbs.limit_sem, GET_URB_TIMEOUT); |
| 1926 | if (ret) { |
| 1927 | atomic_set(&dev->lost_pixels, 1); |
Paul Mundt | 81f6f3c | 2011-01-06 18:07:54 +0900 | [diff] [blame] | 1928 | pr_warn("wait for urb interrupted: %x available: %d\n", |
Bernie Thompson | 5bea1fb | 2010-09-05 18:28:29 -0700 | [diff] [blame] | 1929 | ret, dev->urbs.available); |
Bernie Thompson | 4a4854d | 2010-02-15 06:45:55 -0800 | [diff] [blame] | 1930 | goto error; |
| 1931 | } |
| 1932 | |
| 1933 | spin_lock_irqsave(&dev->urbs.lock, flags); |
| 1934 | |
| 1935 | BUG_ON(list_empty(&dev->urbs.list)); /* reserved one with limit_sem */ |
| 1936 | entry = dev->urbs.list.next; |
| 1937 | list_del_init(entry); |
| 1938 | dev->urbs.available--; |
| 1939 | |
| 1940 | spin_unlock_irqrestore(&dev->urbs.lock, flags); |
| 1941 | |
| 1942 | unode = list_entry(entry, struct urb_node, entry); |
| 1943 | urb = unode->urb; |
| 1944 | |
| 1945 | error: |
| 1946 | return urb; |
| 1947 | } |
| 1948 | |
| 1949 | static int dlfb_submit_urb(struct dlfb_data *dev, struct urb *urb, size_t len) |
| 1950 | { |
| 1951 | int ret; |
| 1952 | |
| 1953 | BUG_ON(len > dev->urbs.size); |
| 1954 | |
| 1955 | urb->transfer_buffer_length = len; /* set to actual payload len */ |
| 1956 | ret = usb_submit_urb(urb, GFP_KERNEL); |
| 1957 | if (ret) { |
| 1958 | dlfb_urb_completion(urb); /* because no one else will */ |
| 1959 | atomic_set(&dev->lost_pixels, 1); |
Paul Mundt | 81f6f3c | 2011-01-06 18:07:54 +0900 | [diff] [blame] | 1960 | pr_err("usb_submit_urb error %x\n", ret); |
Bernie Thompson | 4a4854d | 2010-02-15 06:45:55 -0800 | [diff] [blame] | 1961 | } |
| 1962 | return ret; |
| 1963 | } |
| 1964 | |
Bernie Thompson | d5ed543 | 2010-09-05 16:35:39 -0700 | [diff] [blame] | 1965 | module_param(console, bool, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP); |
Bernie Thompson | 4aa7faf | 2011-08-21 13:35:39 -0700 | [diff] [blame] | 1966 | MODULE_PARM_DESC(console, "Allow fbcon to open framebuffer"); |
Bernie Thompson | d5ed543 | 2010-09-05 16:35:39 -0700 | [diff] [blame] | 1967 | |
| 1968 | module_param(fb_defio, bool, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP); |
Bernie Thompson | 9f811b7 | 2011-08-21 13:35:38 -0700 | [diff] [blame] | 1969 | MODULE_PARM_DESC(fb_defio, "Page fault detection of mmap writes"); |
Bernie Thompson | d5ed543 | 2010-09-05 16:35:39 -0700 | [diff] [blame] | 1970 | |
Stuart Hopkins | d318954 | 2011-08-21 13:34:17 -0700 | [diff] [blame] | 1971 | module_param(shadow, bool, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP); |
| 1972 | MODULE_PARM_DESC(shadow, "Shadow vid mem. Disable to save mem but lose perf"); |
| 1973 | |
Ben Collins | 664c5f1 | 2012-03-03 12:57:37 -0800 | [diff] [blame] | 1974 | module_param(pixel_limit, int, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP); |
| 1975 | MODULE_PARM_DESC(pixel_limit, "Force limit on max mode (in x*y pixels)"); |
| 1976 | |
Bernie Thompson | 59277b6 | 2009-11-24 15:52:21 -0800 | [diff] [blame] | 1977 | MODULE_AUTHOR("Roberto De Ioris <roberto@unbit.it>, " |
Bernie Thompson | 2469d5d | 2010-02-15 06:46:13 -0800 | [diff] [blame] | 1978 | "Jaya Kumar <jayakumar.lkml@gmail.com>, " |
| 1979 | "Bernie Thompson <bernie@plugable.com>"); |
| 1980 | MODULE_DESCRIPTION("DisplayLink kernel framebuffer driver"); |
Roberto De Ioris | 88e58b1 | 2009-06-03 14:03:06 -0700 | [diff] [blame] | 1981 | MODULE_LICENSE("GPL"); |
Bernie Thompson | 2469d5d | 2010-02-15 06:46:13 -0800 | [diff] [blame] | 1982 | |