]>
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 | ||
7d9485e2 BT |
699 | static void dlfb_get_edid(struct dlfb_data *dev) |
700 | { | |
701 | int i; | |
702 | int ret; | |
703 | char rbuf[2]; | |
704 | ||
705 | for (i = 0; i < sizeof(dev->edid); i++) { | |
706 | ret = usb_control_msg(dev->udev, | |
707 | usb_rcvctrlpipe(dev->udev, 0), (0x02), | |
708 | (0x80 | (0x02 << 5)), i << 8, 0xA1, rbuf, 2, | |
709 | 0); | |
710 | dev->edid[i] = rbuf[1]; | |
711 | } | |
712 | } | |
713 | ||
4574203f | 714 | static int dlfb_ops_ioctl(struct fb_info *info, unsigned int cmd, |
530f43a8 | 715 | unsigned long arg) |
88e58b1a | 716 | { |
530f43a8 BT |
717 | |
718 | struct dlfb_data *dev = info->par; | |
7316bc55 | 719 | struct dloarea *area = NULL; |
88e58b1a | 720 | |
530f43a8 BT |
721 | if (!atomic_read(&dev->usb_active)) |
722 | return 0; | |
723 | ||
724 | /* TODO: Update X server to get this from sysfs instead */ | |
725 | if (cmd == DLFB_IOCTL_RETURN_EDID) { | |
7316bc55 | 726 | char *edid = (char *)arg; |
530f43a8 BT |
727 | dlfb_get_edid(dev); |
728 | if (copy_to_user(edid, dev->edid, sizeof(dev->edid))) | |
7316bc55 | 729 | return -EFAULT; |
7316bc55 RDI |
730 | return 0; |
731 | } | |
732 | ||
530f43a8 BT |
733 | /* TODO: Help propose a standard fb.h ioctl to report mmap damage */ |
734 | if (cmd == DLFB_IOCTL_REPORT_DAMAGE) { | |
88e58b1a RDI |
735 | |
736 | area = (struct dloarea *)arg; | |
737 | ||
738 | if (area->x < 0) | |
739 | area->x = 0; | |
740 | ||
741 | if (area->x > info->var.xres) | |
742 | area->x = info->var.xres; | |
743 | ||
744 | if (area->y < 0) | |
745 | area->y = 0; | |
746 | ||
747 | if (area->y > info->var.yres) | |
748 | area->y = info->var.yres; | |
749 | ||
530f43a8 BT |
750 | atomic_set(&dev->use_defio, 0); |
751 | ||
752 | dlfb_handle_damage(dev, area->x, area->y, area->w, area->h, | |
88e58b1a RDI |
753 | info->screen_base); |
754 | } | |
7316bc55 | 755 | |
88e58b1a RDI |
756 | return 0; |
757 | } | |
758 | ||
f05e0575 | 759 | /* taken from vesafb */ |
88e58b1a | 760 | static int |
4574203f | 761 | dlfb_ops_setcolreg(unsigned regno, unsigned red, unsigned green, |
88e58b1a RDI |
762 | unsigned blue, unsigned transp, struct fb_info *info) |
763 | { | |
764 | int err = 0; | |
765 | ||
766 | if (regno >= info->cmap.len) | |
767 | return 1; | |
768 | ||
769 | if (regno < 16) { | |
770 | if (info->var.red.offset == 10) { | |
771 | /* 1:5:5:5 */ | |
772 | ((u32 *) (info->pseudo_palette))[regno] = | |
773 | ((red & 0xf800) >> 1) | | |
774 | ((green & 0xf800) >> 6) | ((blue & 0xf800) >> 11); | |
775 | } else { | |
776 | /* 0:5:6:5 */ | |
777 | ((u32 *) (info->pseudo_palette))[regno] = | |
778 | ((red & 0xf800)) | | |
779 | ((green & 0xfc00) >> 5) | ((blue & 0xf800) >> 11); | |
780 | } | |
781 | } | |
782 | ||
783 | return err; | |
784 | } | |
785 | ||
3e8f3d6f BT |
786 | /* |
787 | * It's common for several clients to have framebuffer open simultaneously. | |
788 | * e.g. both fbcon and X. Makes things interesting. | |
33077b8d | 789 | * Assumes caller is holding info->lock (for open and release at least) |
3e8f3d6f BT |
790 | */ |
791 | static int dlfb_ops_open(struct fb_info *info, int user) | |
792 | { | |
793 | struct dlfb_data *dev = info->par; | |
794 | ||
795 | /* if (user == 0) | |
796 | * We could special case kernel mode clients (fbcon) here | |
797 | */ | |
798 | ||
33077b8d BT |
799 | /* If the USB device is gone, we don't accept new opens */ |
800 | if (dev->virtualized) | |
801 | return -ENODEV; | |
3e8f3d6f BT |
802 | |
803 | dev->fb_count++; | |
804 | ||
33077b8d BT |
805 | kref_get(&dev->kref); |
806 | ||
3e8f3d6f BT |
807 | #ifdef CONFIG_FB_DEFERRED_IO |
808 | if ((atomic_read(&dev->use_defio)) && (info->fbdefio == NULL)) { | |
809 | /* enable defio */ | |
810 | info->fbdefio = &dlfb_defio; | |
811 | fb_deferred_io_init(info); | |
812 | } | |
813 | #endif | |
814 | ||
815 | dl_notice("open /dev/fb%d user=%d fb_info=%p count=%d\n", | |
816 | info->node, user, info, dev->fb_count); | |
817 | ||
88e58b1a RDI |
818 | return 0; |
819 | } | |
820 | ||
4a4854dd BT |
821 | /* |
822 | * Called when all client interfaces to start transactions have been disabled, | |
823 | * and all references to our device instance (dlfb_data) are released. | |
824 | * Every transaction must have a reference, so we know are fully spun down | |
825 | */ | |
33077b8d | 826 | static void dlfb_free(struct kref *kref) |
4a4854dd BT |
827 | { |
828 | struct dlfb_data *dev = container_of(kref, struct dlfb_data, kref); | |
829 | ||
33077b8d BT |
830 | /* this function will wait for all in-flight urbs to complete */ |
831 | if (dev->urbs.count > 0) | |
832 | dlfb_free_urb_list(dev); | |
833 | ||
4a4854dd BT |
834 | if (dev->backing_buffer) |
835 | vfree(dev->backing_buffer); | |
836 | ||
33077b8d BT |
837 | kfree(dev->edid); |
838 | ||
839 | dl_warn("freeing dlfb_data %p\n", dev); | |
3e8f3d6f | 840 | |
4a4854dd BT |
841 | kfree(dev); |
842 | } | |
843 | ||
33077b8d BT |
844 | |
845 | static void dlfb_free_framebuffer_work(struct work_struct *work) | |
2469d5db | 846 | { |
33077b8d BT |
847 | struct dlfb_data *dev = container_of(work, struct dlfb_data, |
848 | free_framebuffer_work.work); | |
849 | struct fb_info *info = dev->info; | |
850 | int node = info->node; | |
851 | ||
852 | unregister_framebuffer(info); | |
2469d5db BT |
853 | |
854 | if (info->cmap.len != 0) | |
855 | fb_dealloc_cmap(&info->cmap); | |
856 | if (info->monspecs.modedb) | |
857 | fb_destroy_modedb(info->monspecs.modedb); | |
858 | if (info->screen_base) | |
859 | vfree(info->screen_base); | |
860 | ||
861 | fb_destroy_modelist(&info->modelist); | |
862 | ||
33077b8d BT |
863 | dev->info = 0; |
864 | ||
865 | /* Assume info structure is freed after this point */ | |
2469d5db BT |
866 | framebuffer_release(info); |
867 | ||
33077b8d BT |
868 | dl_warn("fb_info for /dev/fb%d has been freed\n", node); |
869 | ||
870 | /* ref taken in probe() as part of registering framebfufer */ | |
871 | kref_put(&dev->kref, dlfb_free); | |
872 | } | |
873 | ||
874 | /* | |
875 | * Assumes caller is holding info->lock mutex (for open and release at least) | |
876 | */ | |
877 | static int dlfb_ops_release(struct fb_info *info, int user) | |
878 | { | |
879 | struct dlfb_data *dev = info->par; | |
880 | ||
881 | dev->fb_count--; | |
882 | ||
883 | /* We can't free fb_info here - fbmem will touch it when we return */ | |
884 | if (dev->virtualized && (dev->fb_count == 0)) | |
885 | schedule_delayed_work(&dev->free_framebuffer_work, HZ); | |
886 | ||
887 | #ifdef CONFIG_FB_DEFERRED_IO | |
888 | if ((dev->fb_count == 0) && (info->fbdefio)) { | |
889 | fb_deferred_io_cleanup(info); | |
890 | kfree(info->fbdefio); | |
891 | info->fbdefio = NULL; | |
892 | info->fbops->fb_mmap = dlfb_ops_mmap; | |
893 | } | |
894 | #endif | |
895 | ||
896 | dl_warn("released /dev/fb%d user=%d count=%d\n", | |
897 | info->node, user, dev->fb_count); | |
898 | ||
899 | kref_put(&dev->kref, dlfb_free); | |
900 | ||
901 | return 0; | |
2469d5db BT |
902 | } |
903 | ||
7d9485e2 BT |
904 | /* |
905 | * Check whether a video mode is supported by the DisplayLink chip | |
906 | * We start from monitor's modes, so don't need to filter that here | |
907 | */ | |
908 | static int dlfb_is_valid_mode(struct fb_videomode *mode, | |
909 | struct fb_info *info) | |
910 | { | |
911 | struct dlfb_data *dev = info->par; | |
912 | ||
913 | if (mode->xres * mode->yres > dev->sku_pixel_limit) | |
914 | return 0; | |
915 | ||
916 | return 1; | |
917 | } | |
918 | ||
919 | static void dlfb_var_color_format(struct fb_var_screeninfo *var) | |
920 | { | |
921 | const struct fb_bitfield red = { 11, 5, 0 }; | |
922 | const struct fb_bitfield green = { 5, 6, 0 }; | |
923 | const struct fb_bitfield blue = { 0, 5, 0 }; | |
924 | ||
925 | var->bits_per_pixel = 16; | |
926 | var->red = red; | |
927 | var->green = green; | |
928 | var->blue = blue; | |
929 | } | |
930 | ||
2469d5db BT |
931 | static int dlfb_ops_check_var(struct fb_var_screeninfo *var, |
932 | struct fb_info *info) | |
933 | { | |
934 | struct fb_videomode mode; | |
935 | ||
936 | /* TODO: support dynamically changing framebuffer size */ | |
937 | if ((var->xres * var->yres * 2) > info->fix.smem_len) | |
938 | return -EINVAL; | |
939 | ||
940 | /* set device-specific elements of var unrelated to mode */ | |
941 | dlfb_var_color_format(var); | |
942 | ||
943 | fb_var_to_videomode(&mode, var); | |
944 | ||
945 | if (!dlfb_is_valid_mode(&mode, info)) | |
946 | return -EINVAL; | |
947 | ||
948 | return 0; | |
949 | } | |
950 | ||
951 | static int dlfb_ops_set_par(struct fb_info *info) | |
952 | { | |
953 | struct dlfb_data *dev = info->par; | |
954 | ||
955 | dl_notice("set_par mode %dx%d\n", info->var.xres, info->var.yres); | |
956 | ||
957 | return dlfb_set_video_mode(dev, &info->var); | |
958 | } | |
959 | ||
9825f70f BT |
960 | /* |
961 | * In order to come back from full DPMS off, we need to set the mode again | |
962 | */ | |
4574203f | 963 | static int dlfb_ops_blank(int blank_mode, struct fb_info *info) |
f05e0575 | 964 | { |
530f43a8 | 965 | struct dlfb_data *dev = info->par; |
7316bc55 | 966 | |
9825f70f BT |
967 | if (blank_mode != FB_BLANK_UNBLANK) { |
968 | char *bufptr; | |
969 | struct urb *urb; | |
530f43a8 | 970 | |
9825f70f BT |
971 | urb = dlfb_get_urb(dev); |
972 | if (!urb) | |
973 | return 0; | |
530f43a8 | 974 | |
9825f70f BT |
975 | bufptr = (char *) urb->transfer_buffer; |
976 | bufptr = dlfb_vidreg_lock(bufptr); | |
530f43a8 | 977 | bufptr = dlfb_enable_hvsync(bufptr, false); |
9825f70f BT |
978 | bufptr = dlfb_vidreg_unlock(bufptr); |
979 | ||
980 | dlfb_submit_urb(dev, urb, bufptr - | |
981 | (char *) urb->transfer_buffer); | |
7316bc55 | 982 | } else { |
9825f70f | 983 | dlfb_set_video_mode(dev, &info->var); |
7316bc55 | 984 | } |
7316bc55 | 985 | |
88e58b1a RDI |
986 | return 0; |
987 | } | |
988 | ||
989 | static struct fb_ops dlfb_ops = { | |
2469d5db | 990 | .owner = THIS_MODULE, |
d46ecb9f BT |
991 | .fb_read = dlfb_ops_read, |
992 | .fb_write = dlfb_ops_write, | |
4574203f BT |
993 | .fb_setcolreg = dlfb_ops_setcolreg, |
994 | .fb_fillrect = dlfb_ops_fillrect, | |
995 | .fb_copyarea = dlfb_ops_copyarea, | |
996 | .fb_imageblit = dlfb_ops_imageblit, | |
997 | .fb_mmap = dlfb_ops_mmap, | |
998 | .fb_ioctl = dlfb_ops_ioctl, | |
3e8f3d6f | 999 | .fb_open = dlfb_ops_open, |
4574203f BT |
1000 | .fb_release = dlfb_ops_release, |
1001 | .fb_blank = dlfb_ops_blank, | |
2469d5db BT |
1002 | .fb_check_var = dlfb_ops_check_var, |
1003 | .fb_set_par = dlfb_ops_set_par, | |
88e58b1a RDI |
1004 | }; |
1005 | ||
7d9485e2 BT |
1006 | /* |
1007 | * Calls dlfb_get_edid() to query the EDID of attached monitor via usb cmds | |
1008 | * Then parses EDID into three places used by various parts of fbdev: | |
1009 | * fb_var_screeninfo contains the timing of the monitor's preferred mode | |
1010 | * fb_info.monspecs is full parsed EDID info, including monspecs.modedb | |
1011 | * fb_info.modelist is a linked list of all monitor & VESA modes which work | |
1012 | * | |
1013 | * If EDID is not readable/valid, then modelist is all VESA modes, | |
1014 | * monspecs is NULL, and fb_var_screeninfo is set to safe VESA mode | |
1015 | * Returns 0 if EDID parses successfully | |
1016 | */ | |
1017 | static int dlfb_parse_edid(struct dlfb_data *dev, | |
1018 | struct fb_var_screeninfo *var, | |
1019 | struct fb_info *info) | |
1020 | { | |
1021 | int i; | |
1022 | const struct fb_videomode *default_vmode = NULL; | |
1023 | int result = 0; | |
1024 | ||
1025 | fb_destroy_modelist(&info->modelist); | |
1026 | memset(&info->monspecs, 0, sizeof(info->monspecs)); | |
1027 | ||
1028 | dlfb_get_edid(dev); | |
1029 | fb_edid_to_monspecs(dev->edid, &info->monspecs); | |
1030 | ||
1031 | if (info->monspecs.modedb_len > 0) { | |
1032 | ||
1033 | for (i = 0; i < info->monspecs.modedb_len; i++) { | |
1034 | if (dlfb_is_valid_mode(&info->monspecs.modedb[i], info)) | |
1035 | fb_add_videomode(&info->monspecs.modedb[i], | |
1036 | &info->modelist); | |
1037 | } | |
1038 | ||
1039 | default_vmode = fb_find_best_display(&info->monspecs, | |
1040 | &info->modelist); | |
1041 | } else { | |
1042 | struct fb_videomode fb_vmode = {0}; | |
1043 | ||
1044 | dl_err("Unable to get valid EDID from device/display\n"); | |
1045 | result = 1; | |
1046 | ||
1047 | /* | |
1048 | * Add the standard VESA modes to our modelist | |
1049 | * Since we don't have EDID, there may be modes that | |
1050 | * overspec monitor and/or are incorrect aspect ratio, etc. | |
1051 | * But at least the user has a chance to choose | |
1052 | */ | |
1053 | for (i = 0; i < VESA_MODEDB_SIZE; i++) { | |
1054 | if (dlfb_is_valid_mode((struct fb_videomode *) | |
1055 | &vesa_modes[i], info)) | |
1056 | fb_add_videomode(&vesa_modes[i], | |
1057 | &info->modelist); | |
1058 | } | |
1059 | ||
1060 | /* | |
1061 | * default to resolution safe for projectors | |
1062 | * (since they are most common case without EDID) | |
1063 | */ | |
1064 | fb_vmode.xres = 800; | |
1065 | fb_vmode.yres = 600; | |
1066 | fb_vmode.refresh = 60; | |
1067 | default_vmode = fb_find_nearest_mode(&fb_vmode, | |
1068 | &info->modelist); | |
1069 | } | |
1070 | ||
1071 | fb_videomode_to_var(var, default_vmode); | |
1072 | dlfb_var_color_format(var); | |
1073 | ||
1074 | return result; | |
1075 | } | |
1076 | ||
1077 | static ssize_t metrics_bytes_rendered_show(struct device *fbdev, | |
1078 | struct device_attribute *a, char *buf) { | |
1079 | struct fb_info *fb_info = dev_get_drvdata(fbdev); | |
1080 | struct dlfb_data *dev = fb_info->par; | |
1081 | return snprintf(buf, PAGE_SIZE, "%u\n", | |
1082 | atomic_read(&dev->bytes_rendered)); | |
1083 | } | |
1084 | ||
1085 | static ssize_t metrics_bytes_identical_show(struct device *fbdev, | |
1086 | struct device_attribute *a, char *buf) { | |
1087 | struct fb_info *fb_info = dev_get_drvdata(fbdev); | |
1088 | struct dlfb_data *dev = fb_info->par; | |
1089 | return snprintf(buf, PAGE_SIZE, "%u\n", | |
1090 | atomic_read(&dev->bytes_identical)); | |
1091 | } | |
1092 | ||
1093 | static ssize_t metrics_bytes_sent_show(struct device *fbdev, | |
1094 | struct device_attribute *a, char *buf) { | |
1095 | struct fb_info *fb_info = dev_get_drvdata(fbdev); | |
1096 | struct dlfb_data *dev = fb_info->par; | |
1097 | return snprintf(buf, PAGE_SIZE, "%u\n", | |
1098 | atomic_read(&dev->bytes_sent)); | |
1099 | } | |
1100 | ||
1101 | static ssize_t metrics_cpu_kcycles_used_show(struct device *fbdev, | |
1102 | struct device_attribute *a, char *buf) { | |
1103 | struct fb_info *fb_info = dev_get_drvdata(fbdev); | |
1104 | struct dlfb_data *dev = fb_info->par; | |
1105 | return snprintf(buf, PAGE_SIZE, "%u\n", | |
1106 | atomic_read(&dev->cpu_kcycles_used)); | |
1107 | } | |
1108 | ||
2c3c8bea CW |
1109 | static ssize_t edid_show(struct file *filp, struct kobject *kobj, |
1110 | struct bin_attribute *a, | |
7d9485e2 BT |
1111 | char *buf, loff_t off, size_t count) { |
1112 | struct device *fbdev = container_of(kobj, struct device, kobj); | |
1113 | struct fb_info *fb_info = dev_get_drvdata(fbdev); | |
1114 | struct dlfb_data *dev = fb_info->par; | |
1115 | char *edid = &dev->edid[0]; | |
1116 | const size_t size = sizeof(dev->edid); | |
1117 | ||
1118 | if (dlfb_parse_edid(dev, &fb_info->var, fb_info)) | |
1119 | return 0; | |
1120 | ||
1121 | if (off >= size) | |
1122 | return 0; | |
1123 | ||
1124 | if (off + count > size) | |
1125 | count = size - off; | |
1126 | memcpy(buf, edid + off, count); | |
1127 | ||
1128 | return count; | |
1129 | } | |
1130 | ||
1131 | ||
1132 | static ssize_t metrics_reset_store(struct device *fbdev, | |
1133 | struct device_attribute *attr, | |
1134 | const char *buf, size_t count) | |
1135 | { | |
1136 | struct fb_info *fb_info = dev_get_drvdata(fbdev); | |
1137 | struct dlfb_data *dev = fb_info->par; | |
1138 | ||
1139 | atomic_set(&dev->bytes_rendered, 0); | |
1140 | atomic_set(&dev->bytes_identical, 0); | |
1141 | atomic_set(&dev->bytes_sent, 0); | |
1142 | atomic_set(&dev->cpu_kcycles_used, 0); | |
7d9485e2 BT |
1143 | |
1144 | return count; | |
1145 | } | |
1146 | ||
1147 | static ssize_t use_defio_show(struct device *fbdev, | |
1148 | struct device_attribute *a, char *buf) { | |
1149 | struct fb_info *fb_info = dev_get_drvdata(fbdev); | |
1150 | struct dlfb_data *dev = fb_info->par; | |
1151 | return snprintf(buf, PAGE_SIZE, "%d\n", | |
1152 | atomic_read(&dev->use_defio)); | |
1153 | } | |
1154 | ||
1155 | static ssize_t use_defio_store(struct device *fbdev, | |
1156 | struct device_attribute *attr, | |
1157 | const char *buf, size_t count) | |
1158 | { | |
1159 | struct fb_info *fb_info = dev_get_drvdata(fbdev); | |
1160 | struct dlfb_data *dev = fb_info->par; | |
1161 | ||
1162 | if (count > 0) { | |
1163 | if (buf[0] == '0') | |
1164 | atomic_set(&dev->use_defio, 0); | |
1165 | if (buf[0] == '1') | |
1166 | atomic_set(&dev->use_defio, 1); | |
1167 | } | |
1168 | return count; | |
1169 | } | |
1170 | ||
1171 | static struct bin_attribute edid_attr = { | |
1172 | .attr.name = "edid", | |
1173 | .attr.mode = 0444, | |
1174 | .size = 128, | |
1175 | .read = edid_show, | |
1176 | }; | |
1177 | ||
1178 | static struct device_attribute fb_device_attrs[] = { | |
1179 | __ATTR_RO(metrics_bytes_rendered), | |
1180 | __ATTR_RO(metrics_bytes_identical), | |
1181 | __ATTR_RO(metrics_bytes_sent), | |
1182 | __ATTR_RO(metrics_cpu_kcycles_used), | |
7d9485e2 BT |
1183 | __ATTR(metrics_reset, S_IWUGO, NULL, metrics_reset_store), |
1184 | __ATTR_RW(use_defio), | |
1185 | }; | |
1186 | ||
3e8f3d6f BT |
1187 | #ifdef CONFIG_FB_DEFERRED_IO |
1188 | static void dlfb_dpy_deferred_io(struct fb_info *info, | |
1189 | struct list_head *pagelist) | |
1190 | { | |
1191 | struct page *cur; | |
1192 | struct fb_deferred_io *fbdefio = info->fbdefio; | |
1193 | struct dlfb_data *dev = info->par; | |
1194 | struct urb *urb; | |
1195 | char *cmd; | |
1196 | cycles_t start_cycles, end_cycles; | |
1197 | int bytes_sent = 0; | |
1198 | int bytes_identical = 0; | |
1199 | int bytes_rendered = 0; | |
3e8f3d6f BT |
1200 | |
1201 | if (!atomic_read(&dev->use_defio)) | |
1202 | return; | |
1203 | ||
1204 | if (!atomic_read(&dev->usb_active)) | |
1205 | return; | |
1206 | ||
1207 | start_cycles = get_cycles(); | |
1208 | ||
1209 | urb = dlfb_get_urb(dev); | |
1210 | if (!urb) | |
1211 | return; | |
1212 | cmd = urb->transfer_buffer; | |
1213 | ||
1214 | /* walk the written page list and render each to device */ | |
1215 | list_for_each_entry(cur, &fbdefio->pagelist, lru) { | |
1216 | dlfb_render_hline(dev, &urb, (char *) info->fix.smem_start, | |
1217 | &cmd, cur->index << PAGE_SHIFT, | |
1218 | PAGE_SIZE, &bytes_identical, &bytes_sent); | |
1219 | bytes_rendered += PAGE_SIZE; | |
3e8f3d6f BT |
1220 | } |
1221 | ||
1222 | if (cmd > (char *) urb->transfer_buffer) { | |
1223 | /* Send partial buffer remaining before exiting */ | |
1224 | int len = cmd - (char *) urb->transfer_buffer; | |
1225 | dlfb_submit_urb(dev, urb, len); | |
1226 | bytes_sent += len; | |
1227 | } else | |
1228 | dlfb_urb_completion(urb); | |
1229 | ||
3e8f3d6f BT |
1230 | atomic_add(bytes_sent, &dev->bytes_sent); |
1231 | atomic_add(bytes_identical, &dev->bytes_identical); | |
1232 | atomic_add(bytes_rendered, &dev->bytes_rendered); | |
1233 | end_cycles = get_cycles(); | |
1234 | atomic_add(((unsigned int) ((end_cycles - start_cycles) | |
1235 | >> 10)), /* Kcycles */ | |
1236 | &dev->cpu_kcycles_used); | |
1237 | } | |
1238 | ||
1239 | static struct fb_deferred_io dlfb_defio = { | |
1240 | .delay = 5, | |
1241 | .deferred_io = dlfb_dpy_deferred_io, | |
1242 | }; | |
1243 | ||
1244 | #endif | |
1245 | ||
cc403dc6 BT |
1246 | /* |
1247 | * This is necessary before we can communicate with the display controller. | |
1248 | */ | |
1249 | static int dlfb_select_std_channel(struct dlfb_data *dev) | |
1250 | { | |
1251 | int ret; | |
1252 | u8 set_def_chn[] = { 0x57, 0xCD, 0xDC, 0xA7, | |
1253 | 0x1C, 0x88, 0x5E, 0x15, | |
1254 | 0x60, 0xFE, 0xC6, 0x97, | |
1255 | 0x16, 0x3D, 0x47, 0xF2 }; | |
1256 | ||
1257 | ret = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0), | |
1258 | NR_USB_REQUEST_CHANNEL, | |
1259 | (USB_DIR_OUT | USB_TYPE_VENDOR), 0, 0, | |
1260 | set_def_chn, sizeof(set_def_chn), USB_CTRL_SET_TIMEOUT); | |
1261 | return ret; | |
1262 | } | |
1263 | ||
2469d5db BT |
1264 | |
1265 | static int dlfb_usb_probe(struct usb_interface *interface, | |
59277b67 | 1266 | const struct usb_device_id *id) |
88e58b1a | 1267 | { |
59277b67 BT |
1268 | struct usb_device *usbdev; |
1269 | struct dlfb_data *dev; | |
33077b8d | 1270 | struct fb_info *info = 0; |
59277b67 | 1271 | int videomemorysize; |
2469d5db | 1272 | int i; |
59277b67 BT |
1273 | unsigned char *videomemory; |
1274 | int retval = -ENOMEM; | |
1275 | struct fb_var_screeninfo *var; | |
2469d5db | 1276 | u16 *pix_framebuffer; |
59277b67 | 1277 | |
2469d5db BT |
1278 | /* usb initialization */ |
1279 | ||
1280 | usbdev = interface_to_usbdev(interface); | |
59277b67 BT |
1281 | |
1282 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); | |
1283 | if (dev == NULL) { | |
2469d5db BT |
1284 | err("dlfb_usb_probe: failed alloc of dev struct\n"); |
1285 | goto error; | |
88e58b1a | 1286 | } |
88e58b1a | 1287 | |
2469d5db BT |
1288 | /* we need to wait for both usb and fbdev to spin down on disconnect */ |
1289 | kref_init(&dev->kref); /* matching kref_put in usb .disconnect fn */ | |
1290 | kref_get(&dev->kref); /* matching kref_put in .fb_destroy function*/ | |
1291 | ||
59277b67 | 1292 | dev->udev = usbdev; |
4a4854dd | 1293 | dev->gdev = &usbdev->dev; /* our generic struct device * */ |
59277b67 BT |
1294 | usb_set_intfdata(interface, dev); |
1295 | ||
2469d5db BT |
1296 | if (!dlfb_alloc_urb_list(dev, WRITES_IN_FLIGHT, MAX_TRANSFER)) { |
1297 | retval = -ENOMEM; | |
1298 | dl_err("dlfb_alloc_urb_list failed\n"); | |
1299 | goto error; | |
1300 | } | |
1301 | ||
2469d5db | 1302 | /* We don't register a new USB class. Our client interface is fbdev */ |
59277b67 | 1303 | |
2469d5db BT |
1304 | /* allocates framebuffer driver structure, not framebuffer memory */ |
1305 | info = framebuffer_alloc(0, &usbdev->dev); | |
1306 | if (!info) { | |
1307 | retval = -ENOMEM; | |
1308 | dl_err("framebuffer_alloc failed\n"); | |
1309 | goto error; | |
1310 | } | |
33077b8d | 1311 | |
59277b67 BT |
1312 | dev->info = info; |
1313 | info->par = dev; | |
1314 | info->pseudo_palette = dev->pseudo_palette; | |
2469d5db | 1315 | info->fbops = &dlfb_ops; |
59277b67 BT |
1316 | |
1317 | var = &info->var; | |
2469d5db BT |
1318 | |
1319 | /* TODO set limit based on actual SKU detection */ | |
1320 | dev->sku_pixel_limit = 2048 * 1152; | |
1321 | ||
1322 | INIT_LIST_HEAD(&info->modelist); | |
1323 | dlfb_parse_edid(dev, var, info); | |
88e58b1a | 1324 | |
59277b67 BT |
1325 | /* |
1326 | * ok, now that we've got the size info, we can alloc our framebuffer. | |
59277b67 | 1327 | */ |
59277b67 BT |
1328 | info->fix = dlfb_fix; |
1329 | info->fix.line_length = var->xres * (var->bits_per_pixel / 8); | |
1330 | videomemorysize = info->fix.line_length * var->yres; | |
1331 | ||
1332 | /* | |
1333 | * The big chunk of system memory we use as a virtual framebuffer. | |
2469d5db | 1334 | * TODO: Handle fbcon cursor code calling blit in interrupt context |
59277b67 BT |
1335 | */ |
1336 | videomemory = vmalloc(videomemorysize); | |
2469d5db BT |
1337 | if (!videomemory) { |
1338 | retval = -ENOMEM; | |
1339 | dl_err("Virtual framebuffer alloc failed\n"); | |
1340 | goto error; | |
1341 | } | |
59277b67 BT |
1342 | |
1343 | info->screen_base = videomemory; | |
1344 | info->fix.smem_len = PAGE_ALIGN(videomemorysize); | |
1345 | info->fix.smem_start = (unsigned long) videomemory; | |
2469d5db BT |
1346 | info->flags = udlfb_info_flags; |
1347 | ||
88e58b1a | 1348 | |
59277b67 BT |
1349 | /* |
1350 | * Second framebuffer copy, mirroring the state of the framebuffer | |
1351 | * on the physical USB device. We can function without this. | |
1352 | * But with imperfect damage info we may end up sending pixels over USB | |
1353 | * that were, in fact, unchanged -- wasting limited USB bandwidth | |
1354 | */ | |
2469d5db | 1355 | dev->backing_buffer = vmalloc(videomemorysize); |
59277b67 | 1356 | if (!dev->backing_buffer) |
2469d5db BT |
1357 | dl_warn("No shadow/backing buffer allcoated\n"); |
1358 | else | |
1359 | memset(dev->backing_buffer, 0, videomemorysize); | |
f05e0575 | 1360 | |
2469d5db BT |
1361 | retval = fb_alloc_cmap(&info->cmap, 256, 0); |
1362 | if (retval < 0) { | |
1363 | dl_err("fb_alloc_cmap failed %x\n", retval); | |
1364 | goto error; | |
1365 | } | |
88e58b1a | 1366 | |
33077b8d BT |
1367 | INIT_DELAYED_WORK(&dev->free_framebuffer_work, |
1368 | dlfb_free_framebuffer_work); | |
1369 | ||
2469d5db | 1370 | /* ready to begin using device */ |
88e58b1a | 1371 | |
2469d5db BT |
1372 | #ifdef CONFIG_FB_DEFERRED_IO |
1373 | atomic_set(&dev->use_defio, 1); | |
1374 | #endif | |
2469d5db BT |
1375 | atomic_set(&dev->usb_active, 1); |
1376 | dlfb_select_std_channel(dev); | |
88e58b1a | 1377 | |
2469d5db BT |
1378 | dlfb_ops_check_var(var, info); |
1379 | dlfb_ops_set_par(info); | |
88e58b1a | 1380 | |
2469d5db | 1381 | /* paint greenscreen */ |
2469d5db BT |
1382 | pix_framebuffer = (u16 *) videomemory; |
1383 | for (i = 0; i < videomemorysize / 2; i++) | |
1384 | pix_framebuffer[i] = 0x37e6; | |
88e58b1a | 1385 | |
2469d5db BT |
1386 | dlfb_handle_damage(dev, 0, 0, info->var.xres, info->var.yres, |
1387 | videomemory); | |
530f43a8 | 1388 | |
2469d5db | 1389 | retval = register_framebuffer(info); |
59277b67 | 1390 | if (retval < 0) { |
2469d5db BT |
1391 | dl_err("register_framebuffer failed %d\n", retval); |
1392 | goto error; | |
7316bc55 | 1393 | } |
88e58b1a | 1394 | |
2469d5db BT |
1395 | for (i = 0; i < ARRAY_SIZE(fb_device_attrs); i++) |
1396 | device_create_file(info->dev, &fb_device_attrs[i]); | |
88e58b1a | 1397 | |
2469d5db | 1398 | device_create_bin_file(info->dev, &edid_attr); |
88e58b1a | 1399 | |
2469d5db BT |
1400 | dl_err("DisplayLink USB device /dev/fb%d attached. %dx%d resolution." |
1401 | " Using %dK framebuffer memory\n", info->node, | |
1402 | var->xres, var->yres, | |
1403 | ((dev->backing_buffer) ? | |
1404 | videomemorysize * 2 : videomemorysize) >> 10); | |
88e58b1a RDI |
1405 | return 0; |
1406 | ||
2469d5db BT |
1407 | error: |
1408 | if (dev) { | |
2469d5db | 1409 | |
33077b8d BT |
1410 | if (info) { |
1411 | if (info->cmap.len != 0) | |
1412 | fb_dealloc_cmap(&info->cmap); | |
1413 | if (info->monspecs.modedb) | |
1414 | fb_destroy_modedb(info->monspecs.modedb); | |
1415 | if (info->screen_base) | |
1416 | vfree(info->screen_base); | |
1417 | ||
1418 | fb_destroy_modelist(&info->modelist); | |
1419 | ||
1420 | framebuffer_release(info); | |
1421 | } | |
1422 | ||
1423 | if (dev->backing_buffer) | |
1424 | vfree(dev->backing_buffer); | |
1425 | ||
1426 | kref_put(&dev->kref, dlfb_free); /* ref for framebuffer */ | |
1427 | kref_put(&dev->kref, dlfb_free); /* last ref from kref_init */ | |
2469d5db BT |
1428 | |
1429 | /* dev has been deallocated. Do not dereference */ | |
1430 | } | |
1431 | ||
59277b67 | 1432 | return retval; |
88e58b1a RDI |
1433 | } |
1434 | ||
2469d5db | 1435 | static void dlfb_usb_disconnect(struct usb_interface *interface) |
88e58b1a | 1436 | { |
59277b67 BT |
1437 | struct dlfb_data *dev; |
1438 | struct fb_info *info; | |
2469d5db | 1439 | int i; |
88e58b1a | 1440 | |
59277b67 | 1441 | dev = usb_get_intfdata(interface); |
2469d5db BT |
1442 | info = dev->info; |
1443 | ||
33077b8d | 1444 | dl_info("USB disconnect starting\n"); |
2469d5db | 1445 | |
33077b8d BT |
1446 | /* we virtualize until all fb clients release. Then we free */ |
1447 | dev->virtualized = true; | |
1448 | ||
1449 | /* When non-active we'll update virtual framebuffer, but no new urbs */ | |
1450 | atomic_set(&dev->usb_active, 0); | |
59277b67 | 1451 | |
33077b8d | 1452 | /* remove udlfb's sysfs interfaces */ |
2469d5db BT |
1453 | for (i = 0; i < ARRAY_SIZE(fb_device_attrs); i++) |
1454 | device_remove_file(info->dev, &fb_device_attrs[i]); | |
2469d5db BT |
1455 | device_remove_bin_file(info->dev, &edid_attr); |
1456 | ||
33077b8d | 1457 | usb_set_intfdata(interface, NULL); |
59277b67 | 1458 | |
33077b8d BT |
1459 | /* if clients still have us open, will be freed on last close */ |
1460 | if (dev->fb_count == 0) | |
1461 | schedule_delayed_work(&dev->free_framebuffer_work, 0); | |
88e58b1a | 1462 | |
2469d5db | 1463 | /* release reference taken by kref_init in probe() */ |
33077b8d | 1464 | kref_put(&dev->kref, dlfb_free); |
88e58b1a | 1465 | |
2469d5db BT |
1466 | /* consider dlfb_data freed */ |
1467 | ||
1468 | return; | |
88e58b1a RDI |
1469 | } |
1470 | ||
1471 | static struct usb_driver dlfb_driver = { | |
1472 | .name = "udlfb", | |
2469d5db BT |
1473 | .probe = dlfb_usb_probe, |
1474 | .disconnect = dlfb_usb_disconnect, | |
88e58b1a RDI |
1475 | .id_table = id_table, |
1476 | }; | |
1477 | ||
2469d5db | 1478 | static int __init dlfb_module_init(void) |
88e58b1a RDI |
1479 | { |
1480 | int res; | |
1481 | ||
88e58b1a RDI |
1482 | res = usb_register(&dlfb_driver); |
1483 | if (res) | |
1484 | err("usb_register failed. Error number %d", res); | |
1485 | ||
88e58b1a RDI |
1486 | return res; |
1487 | } | |
1488 | ||
2469d5db | 1489 | static void __exit dlfb_module_exit(void) |
88e58b1a RDI |
1490 | { |
1491 | usb_deregister(&dlfb_driver); | |
1492 | } | |
1493 | ||
2469d5db BT |
1494 | module_init(dlfb_module_init); |
1495 | module_exit(dlfb_module_exit); | |
88e58b1a | 1496 | |
4a4854dd BT |
1497 | static void dlfb_urb_completion(struct urb *urb) |
1498 | { | |
1499 | struct urb_node *unode = urb->context; | |
1500 | struct dlfb_data *dev = unode->dev; | |
1501 | unsigned long flags; | |
1502 | ||
1503 | /* sync/async unlink faults aren't errors */ | |
1504 | if (urb->status) { | |
1505 | if (!(urb->status == -ENOENT || | |
1506 | urb->status == -ECONNRESET || | |
1507 | urb->status == -ESHUTDOWN)) { | |
1508 | dl_err("%s - nonzero write bulk status received: %d\n", | |
1509 | __func__, urb->status); | |
1510 | atomic_set(&dev->lost_pixels, 1); | |
1511 | } | |
1512 | } | |
1513 | ||
1514 | urb->transfer_buffer_length = dev->urbs.size; /* reset to actual */ | |
1515 | ||
1516 | spin_lock_irqsave(&dev->urbs.lock, flags); | |
1517 | list_add_tail(&unode->entry, &dev->urbs.list); | |
1518 | dev->urbs.available++; | |
1519 | spin_unlock_irqrestore(&dev->urbs.lock, flags); | |
1520 | ||
1521 | up(&dev->urbs.limit_sem); | |
1522 | } | |
1523 | ||
1524 | static void dlfb_free_urb_list(struct dlfb_data *dev) | |
1525 | { | |
1526 | int count = dev->urbs.count; | |
1527 | struct list_head *node; | |
1528 | struct urb_node *unode; | |
1529 | struct urb *urb; | |
1530 | int ret; | |
1531 | unsigned long flags; | |
1532 | ||
1533 | dl_notice("Waiting for completes and freeing all render urbs\n"); | |
1534 | ||
1535 | /* keep waiting and freeing, until we've got 'em all */ | |
1536 | while (count--) { | |
33077b8d BT |
1537 | |
1538 | /* Getting interrupted means a leak, but ok at shutdown*/ | |
1539 | ret = down_interruptible(&dev->urbs.limit_sem); | |
1540 | if (ret) | |
4a4854dd | 1541 | break; |
33077b8d | 1542 | |
4a4854dd BT |
1543 | spin_lock_irqsave(&dev->urbs.lock, flags); |
1544 | ||
1545 | node = dev->urbs.list.next; /* have reserved one with sem */ | |
1546 | list_del_init(node); | |
1547 | ||
1548 | spin_unlock_irqrestore(&dev->urbs.lock, flags); | |
1549 | ||
1550 | unode = list_entry(node, struct urb_node, entry); | |
1551 | urb = unode->urb; | |
1552 | ||
1553 | /* Free each separately allocated piece */ | |
c220cc3e GKH |
1554 | usb_free_coherent(urb->dev, dev->urbs.size, |
1555 | urb->transfer_buffer, urb->transfer_dma); | |
4a4854dd BT |
1556 | usb_free_urb(urb); |
1557 | kfree(node); | |
1558 | } | |
1559 | ||
4a4854dd BT |
1560 | } |
1561 | ||
1562 | static int dlfb_alloc_urb_list(struct dlfb_data *dev, int count, size_t size) | |
1563 | { | |
1564 | int i = 0; | |
1565 | struct urb *urb; | |
1566 | struct urb_node *unode; | |
1567 | char *buf; | |
1568 | ||
1569 | spin_lock_init(&dev->urbs.lock); | |
1570 | ||
1571 | dev->urbs.size = size; | |
1572 | INIT_LIST_HEAD(&dev->urbs.list); | |
1573 | ||
1574 | while (i < count) { | |
1575 | unode = kzalloc(sizeof(struct urb_node), GFP_KERNEL); | |
1576 | if (!unode) | |
1577 | break; | |
1578 | unode->dev = dev; | |
1579 | ||
1580 | urb = usb_alloc_urb(0, GFP_KERNEL); | |
1581 | if (!urb) { | |
1582 | kfree(unode); | |
1583 | break; | |
1584 | } | |
1585 | unode->urb = urb; | |
1586 | ||
c220cc3e GKH |
1587 | buf = usb_alloc_coherent(dev->udev, MAX_TRANSFER, GFP_KERNEL, |
1588 | &urb->transfer_dma); | |
4a4854dd BT |
1589 | if (!buf) { |
1590 | kfree(unode); | |
1591 | usb_free_urb(urb); | |
1592 | break; | |
1593 | } | |
1594 | ||
1595 | /* urb->transfer_buffer_length set to actual before submit */ | |
1596 | usb_fill_bulk_urb(urb, dev->udev, usb_sndbulkpipe(dev->udev, 1), | |
1597 | buf, size, dlfb_urb_completion, unode); | |
1598 | urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; | |
1599 | ||
1600 | list_add_tail(&unode->entry, &dev->urbs.list); | |
1601 | ||
1602 | i++; | |
1603 | } | |
1604 | ||
1605 | sema_init(&dev->urbs.limit_sem, i); | |
1606 | dev->urbs.count = i; | |
1607 | dev->urbs.available = i; | |
1608 | ||
b5a2104c | 1609 | dl_notice("allocated %d %d byte urbs\n", i, (int) size); |
4a4854dd BT |
1610 | |
1611 | return i; | |
1612 | } | |
1613 | ||
1614 | static struct urb *dlfb_get_urb(struct dlfb_data *dev) | |
1615 | { | |
1616 | int ret = 0; | |
1617 | struct list_head *entry; | |
1618 | struct urb_node *unode; | |
1619 | struct urb *urb = NULL; | |
1620 | unsigned long flags; | |
1621 | ||
1622 | /* Wait for an in-flight buffer to complete and get re-queued */ | |
1623 | ret = down_timeout(&dev->urbs.limit_sem, GET_URB_TIMEOUT); | |
1624 | if (ret) { | |
1625 | atomic_set(&dev->lost_pixels, 1); | |
1626 | dl_err("wait for urb interrupted: %x\n", ret); | |
1627 | goto error; | |
1628 | } | |
1629 | ||
1630 | spin_lock_irqsave(&dev->urbs.lock, flags); | |
1631 | ||
1632 | BUG_ON(list_empty(&dev->urbs.list)); /* reserved one with limit_sem */ | |
1633 | entry = dev->urbs.list.next; | |
1634 | list_del_init(entry); | |
1635 | dev->urbs.available--; | |
1636 | ||
1637 | spin_unlock_irqrestore(&dev->urbs.lock, flags); | |
1638 | ||
1639 | unode = list_entry(entry, struct urb_node, entry); | |
1640 | urb = unode->urb; | |
1641 | ||
1642 | error: | |
1643 | return urb; | |
1644 | } | |
1645 | ||
1646 | static int dlfb_submit_urb(struct dlfb_data *dev, struct urb *urb, size_t len) | |
1647 | { | |
1648 | int ret; | |
1649 | ||
1650 | BUG_ON(len > dev->urbs.size); | |
1651 | ||
1652 | urb->transfer_buffer_length = len; /* set to actual payload len */ | |
1653 | ret = usb_submit_urb(urb, GFP_KERNEL); | |
1654 | if (ret) { | |
1655 | dlfb_urb_completion(urb); /* because no one else will */ | |
1656 | atomic_set(&dev->lost_pixels, 1); | |
1657 | dl_err("usb_submit_urb error %x\n", ret); | |
1658 | } | |
1659 | return ret; | |
1660 | } | |
1661 | ||
59277b67 | 1662 | MODULE_AUTHOR("Roberto De Ioris <roberto@unbit.it>, " |
2469d5db BT |
1663 | "Jaya Kumar <jayakumar.lkml@gmail.com>, " |
1664 | "Bernie Thompson <bernie@plugable.com>"); | |
1665 | MODULE_DESCRIPTION("DisplayLink kernel framebuffer driver"); | |
88e58b1a | 1666 | MODULE_LICENSE("GPL"); |
2469d5db | 1667 |