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