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