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