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