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