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