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