1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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>
26#include <linux/vmalloc.h>
27#include <linux/slab.h>
28#include <linux/prefetch.h>
29#include <linux/delay.h>
30#include <video/udlfb.h>
31#include "edid.h"
32
33static const struct fb_fix_screeninfo dlfb_fix = {
34 .id = "udlfb",
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,
41};
42
43static const u32 udlfb_info_flags = FBINFO_DEFAULT | FBINFO_READS_FAST |
44 FBINFO_VIRTFB |
45 FBINFO_HWACCEL_IMAGEBLIT | FBINFO_HWACCEL_FILLRECT |
46 FBINFO_HWACCEL_COPYAREA | FBINFO_MISC_ALWAYS_SETPAR;
47
48
49
50
51
52
53
54
55static const struct usb_device_id id_table[] = {
56 {.idVendor = 0x17e9,
57 .bInterfaceClass = 0xff,
58 .bInterfaceSubClass = 0x00,
59 .bInterfaceProtocol = 0x00,
60 .match_flags = USB_DEVICE_ID_MATCH_VENDOR |
61 USB_DEVICE_ID_MATCH_INT_CLASS |
62 USB_DEVICE_ID_MATCH_INT_SUBCLASS |
63 USB_DEVICE_ID_MATCH_INT_PROTOCOL,
64 },
65 {},
66};
67MODULE_DEVICE_TABLE(usb, id_table);
68
69
70static bool console = 1;
71static bool fb_defio = 1;
72static bool shadow = 1;
73static int pixel_limit;
74
75
76static void dlfb_urb_completion(struct urb *urb);
77static struct urb *dlfb_get_urb(struct dlfb_data *dlfb);
78static int dlfb_submit_urb(struct dlfb_data *dlfb, struct urb * urb, size_t len);
79static int dlfb_alloc_urb_list(struct dlfb_data *dlfb, int count, size_t size);
80static void dlfb_free_urb_list(struct dlfb_data *dlfb);
81
82
83
84
85
86static char *dlfb_set_register(char *buf, u8 reg, u8 val)
87{
88 *buf++ = 0xAF;
89 *buf++ = 0x20;
90 *buf++ = reg;
91 *buf++ = val;
92 return buf;
93}
94
95static char *dlfb_vidreg_lock(char *buf)
96{
97 return dlfb_set_register(buf, 0xFF, 0x00);
98}
99
100static char *dlfb_vidreg_unlock(char *buf)
101{
102 return dlfb_set_register(buf, 0xFF, 0xFF);
103}
104
105
106
107
108
109
110
111
112
113
114
115static char *dlfb_blanking(char *buf, int fb_blank)
116{
117 u8 reg;
118
119 switch (fb_blank) {
120 case FB_BLANK_POWERDOWN:
121 reg = 0x07;
122 break;
123 case FB_BLANK_HSYNC_SUSPEND:
124 reg = 0x05;
125 break;
126 case FB_BLANK_VSYNC_SUSPEND:
127 reg = 0x03;
128 break;
129 case FB_BLANK_NORMAL:
130 reg = 0x01;
131 break;
132 default:
133 reg = 0x00;
134 }
135
136 buf = dlfb_set_register(buf, 0x1F, reg);
137
138 return buf;
139}
140
141static char *dlfb_set_color_depth(char *buf, u8 selection)
142{
143 return dlfb_set_register(buf, 0x00, selection);
144}
145
146static char *dlfb_set_base16bpp(char *wrptr, u32 base)
147{
148
149 wrptr = dlfb_set_register(wrptr, 0x20, base >> 16);
150 wrptr = dlfb_set_register(wrptr, 0x21, base >> 8);
151 return dlfb_set_register(wrptr, 0x22, base);
152}
153
154
155
156
157
158static char *dlfb_set_base8bpp(char *wrptr, u32 base)
159{
160 wrptr = dlfb_set_register(wrptr, 0x26, base >> 16);
161 wrptr = dlfb_set_register(wrptr, 0x27, base >> 8);
162 return dlfb_set_register(wrptr, 0x28, base);
163}
164
165static char *dlfb_set_register_16(char *wrptr, u8 reg, u16 value)
166{
167 wrptr = dlfb_set_register(wrptr, reg, value >> 8);
168 return dlfb_set_register(wrptr, reg+1, value);
169}
170
171
172
173
174
175static char *dlfb_set_register_16be(char *wrptr, u8 reg, u16 value)
176{
177 wrptr = dlfb_set_register(wrptr, reg, value);
178 return dlfb_set_register(wrptr, reg+1, value >> 8);
179}
180
181
182
183
184
185
186
187
188
189
190static u16 dlfb_lfsr16(u16 actual_count)
191{
192 u32 lv = 0xFFFF;
193
194 while (actual_count--) {
195 lv = ((lv << 1) |
196 (((lv >> 15) ^ (lv >> 4) ^ (lv >> 2) ^ (lv >> 1)) & 1))
197 & 0xFFFF;
198 }
199
200 return (u16) lv;
201}
202
203
204
205
206
207static char *dlfb_set_register_lfsr16(char *wrptr, u8 reg, u16 value)
208{
209 return dlfb_set_register_16(wrptr, reg, dlfb_lfsr16(value));
210}
211
212
213
214
215
216static char *dlfb_set_vid_cmds(char *wrptr, struct fb_var_screeninfo *var)
217{
218 u16 xds, yds;
219 u16 xde, yde;
220 u16 yec;
221
222
223 xds = var->left_margin + var->hsync_len;
224 wrptr = dlfb_set_register_lfsr16(wrptr, 0x01, xds);
225
226 xde = xds + var->xres;
227 wrptr = dlfb_set_register_lfsr16(wrptr, 0x03, xde);
228
229
230 yds = var->upper_margin + var->vsync_len;
231 wrptr = dlfb_set_register_lfsr16(wrptr, 0x05, yds);
232
233 yde = yds + var->yres;
234 wrptr = dlfb_set_register_lfsr16(wrptr, 0x07, yde);
235
236
237 wrptr = dlfb_set_register_lfsr16(wrptr, 0x09,
238 xde + var->right_margin - 1);
239
240
241 wrptr = dlfb_set_register_lfsr16(wrptr, 0x0B, 1);
242
243
244 wrptr = dlfb_set_register_lfsr16(wrptr, 0x0D, var->hsync_len + 1);
245
246
247 wrptr = dlfb_set_register_16(wrptr, 0x0F, var->xres);
248
249
250 yec = var->yres + var->upper_margin + var->lower_margin +
251 var->vsync_len;
252 wrptr = dlfb_set_register_lfsr16(wrptr, 0x11, yec);
253
254
255 wrptr = dlfb_set_register_lfsr16(wrptr, 0x13, 0);
256
257
258 wrptr = dlfb_set_register_lfsr16(wrptr, 0x15, var->vsync_len);
259
260
261 wrptr = dlfb_set_register_16(wrptr, 0x17, var->yres);
262
263
264 wrptr = dlfb_set_register_16be(wrptr, 0x1B,
265 200*1000*1000/var->pixclock);
266
267 return wrptr;
268}
269
270
271
272
273
274
275static int dlfb_set_video_mode(struct dlfb_data *dlfb,
276 struct fb_var_screeninfo *var)
277{
278 char *buf;
279 char *wrptr;
280 int retval;
281 int writesize;
282 struct urb *urb;
283
284 if (!atomic_read(&dlfb->usb_active))
285 return -EPERM;
286
287 urb = dlfb_get_urb(dlfb);
288 if (!urb)
289 return -ENOMEM;
290
291 buf = (char *) urb->transfer_buffer;
292
293
294
295
296
297
298 wrptr = dlfb_vidreg_lock(buf);
299 wrptr = dlfb_set_color_depth(wrptr, 0x00);
300
301 wrptr = dlfb_set_base16bpp(wrptr, 0);
302
303 wrptr = dlfb_set_base8bpp(wrptr, dlfb->info->fix.smem_len);
304
305 wrptr = dlfb_set_vid_cmds(wrptr, var);
306 wrptr = dlfb_blanking(wrptr, FB_BLANK_UNBLANK);
307 wrptr = dlfb_vidreg_unlock(wrptr);
308
309 writesize = wrptr - buf;
310
311 retval = dlfb_submit_urb(dlfb, urb, writesize);
312
313 dlfb->blank_mode = FB_BLANK_UNBLANK;
314
315 return retval;
316}
317
318static int dlfb_ops_mmap(struct fb_info *info, struct vm_area_struct *vma)
319{
320 unsigned long start = vma->vm_start;
321 unsigned long size = vma->vm_end - vma->vm_start;
322 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
323 unsigned long page, pos;
324
325 if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT))
326 return -EINVAL;
327 if (size > info->fix.smem_len)
328 return -EINVAL;
329 if (offset > info->fix.smem_len - size)
330 return -EINVAL;
331
332 pos = (unsigned long)info->fix.smem_start + offset;
333
334 dev_dbg(info->dev, "mmap() framebuffer addr:%lu size:%lu\n",
335 pos, size);
336
337 while (size > 0) {
338 page = vmalloc_to_pfn((void *)pos);
339 if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED))
340 return -EAGAIN;
341
342 start += PAGE_SIZE;
343 pos += PAGE_SIZE;
344 if (size > PAGE_SIZE)
345 size -= PAGE_SIZE;
346 else
347 size = 0;
348 }
349
350 return 0;
351}
352
353
354
355
356
357
358
359
360static int dlfb_trim_hline(const u8 *bback, const u8 **bfront, int *width_bytes)
361{
362 int j, k;
363 const unsigned long *back = (const unsigned long *) bback;
364 const unsigned long *front = (const unsigned long *) *bfront;
365 const int width = *width_bytes / sizeof(unsigned long);
366 int identical = width;
367 int start = width;
368 int end = width;
369
370 prefetch((void *) front);
371 prefetch((void *) back);
372
373 for (j = 0; j < width; j++) {
374 if (back[j] != front[j]) {
375 start = j;
376 break;
377 }
378 }
379
380 for (k = width - 1; k > j; k--) {
381 if (back[k] != front[k]) {
382 end = k+1;
383 break;
384 }
385 }
386
387 identical = start + (width - end);
388 *bfront = (u8 *) &front[start];
389 *width_bytes = (end - start) * sizeof(unsigned long);
390
391 return identical * sizeof(unsigned long);
392}
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421static void dlfb_compress_hline(
422 const uint16_t **pixel_start_ptr,
423 const uint16_t *const pixel_end,
424 uint32_t *device_address_ptr,
425 uint8_t **command_buffer_ptr,
426 const uint8_t *const cmd_buffer_end)
427{
428 const uint16_t *pixel = *pixel_start_ptr;
429 uint32_t dev_addr = *device_address_ptr;
430 uint8_t *cmd = *command_buffer_ptr;
431
432 while ((pixel_end > pixel) &&
433 (cmd_buffer_end - MIN_RLX_CMD_BYTES > cmd)) {
434 uint8_t *raw_pixels_count_byte = NULL;
435 uint8_t *cmd_pixels_count_byte = NULL;
436 const uint16_t *raw_pixel_start = NULL;
437 const uint16_t *cmd_pixel_start, *cmd_pixel_end = NULL;
438
439 prefetchw((void *) cmd);
440
441 *cmd++ = 0xAF;
442 *cmd++ = 0x6B;
443 *cmd++ = dev_addr >> 16;
444 *cmd++ = dev_addr >> 8;
445 *cmd++ = dev_addr;
446
447 cmd_pixels_count_byte = cmd++;
448 cmd_pixel_start = pixel;
449
450 raw_pixels_count_byte = cmd++;
451 raw_pixel_start = pixel;
452
453 cmd_pixel_end = pixel + min(MAX_CMD_PIXELS + 1,
454 min((int)(pixel_end - pixel),
455 (int)(cmd_buffer_end - cmd) / BPP));
456
457 prefetch_range((void *) pixel, (cmd_pixel_end - pixel) * BPP);
458
459 while (pixel < cmd_pixel_end) {
460 const uint16_t * const repeating_pixel = pixel;
461
462 *cmd++ = *pixel >> 8;
463 *cmd++ = *pixel;
464 pixel++;
465
466 if (unlikely((pixel < cmd_pixel_end) &&
467 (*pixel == *repeating_pixel))) {
468
469 *raw_pixels_count_byte = ((repeating_pixel -
470 raw_pixel_start) + 1) & 0xFF;
471
472 while ((pixel < cmd_pixel_end)
473 && (*pixel == *repeating_pixel)) {
474 pixel++;
475 }
476
477
478 *cmd++ = ((pixel - repeating_pixel) - 1) & 0xFF;
479
480
481 raw_pixel_start = pixel;
482 raw_pixels_count_byte = cmd++;
483 }
484 }
485
486 if (pixel > raw_pixel_start) {
487
488 *raw_pixels_count_byte = (pixel-raw_pixel_start) & 0xFF;
489 }
490
491 *cmd_pixels_count_byte = (pixel - cmd_pixel_start) & 0xFF;
492 dev_addr += (pixel - cmd_pixel_start) * BPP;
493 }
494
495 if (cmd_buffer_end <= MIN_RLX_CMD_BYTES + cmd) {
496
497 if (cmd_buffer_end > cmd)
498 memset(cmd, 0xAF, cmd_buffer_end - cmd);
499 cmd = (uint8_t *) cmd_buffer_end;
500 }
501
502 *command_buffer_ptr = cmd;
503 *pixel_start_ptr = pixel;
504 *device_address_ptr = dev_addr;
505}
506
507
508
509
510
511
512
513static int dlfb_render_hline(struct dlfb_data *dlfb, struct urb **urb_ptr,
514 const char *front, char **urb_buf_ptr,
515 u32 byte_offset, u32 byte_width,
516 int *ident_ptr, int *sent_ptr)
517{
518 const u8 *line_start, *line_end, *next_pixel;
519 u32 dev_addr = dlfb->base16 + byte_offset;
520 struct urb *urb = *urb_ptr;
521 u8 *cmd = *urb_buf_ptr;
522 u8 *cmd_end = (u8 *) urb->transfer_buffer + urb->transfer_buffer_length;
523
524 line_start = (u8 *) (front + byte_offset);
525 next_pixel = line_start;
526 line_end = next_pixel + byte_width;
527
528 if (dlfb->backing_buffer) {
529 int offset;
530 const u8 *back_start = (u8 *) (dlfb->backing_buffer
531 + byte_offset);
532
533 *ident_ptr += dlfb_trim_hline(back_start, &next_pixel,
534 &byte_width);
535
536 offset = next_pixel - line_start;
537 line_end = next_pixel + byte_width;
538 dev_addr += offset;
539 back_start += offset;
540 line_start += offset;
541
542 memcpy((char *)back_start, (char *) line_start,
543 byte_width);
544 }
545
546 while (next_pixel < line_end) {
547
548 dlfb_compress_hline((const uint16_t **) &next_pixel,
549 (const uint16_t *) line_end, &dev_addr,
550 (u8 **) &cmd, (u8 *) cmd_end);
551
552 if (cmd >= cmd_end) {
553 int len = cmd - (u8 *) urb->transfer_buffer;
554 if (dlfb_submit_urb(dlfb, urb, len))
555 return 1;
556 *sent_ptr += len;
557 urb = dlfb_get_urb(dlfb);
558 if (!urb)
559 return 1;
560 *urb_ptr = urb;
561 cmd = urb->transfer_buffer;
562 cmd_end = &cmd[urb->transfer_buffer_length];
563 }
564 }
565
566 *urb_buf_ptr = cmd;
567
568 return 0;
569}
570
571static int dlfb_handle_damage(struct dlfb_data *dlfb, int x, int y,
572 int width, int height, char *data)
573{
574 int i, ret;
575 char *cmd;
576 cycles_t start_cycles, end_cycles;
577 int bytes_sent = 0;
578 int bytes_identical = 0;
579 struct urb *urb;
580 int aligned_x;
581
582 start_cycles = get_cycles();
583
584 aligned_x = DL_ALIGN_DOWN(x, sizeof(unsigned long));
585 width = DL_ALIGN_UP(width + (x-aligned_x), sizeof(unsigned long));
586 x = aligned_x;
587
588 if ((width <= 0) ||
589 (x + width > dlfb->info->var.xres) ||
590 (y + height > dlfb->info->var.yres))
591 return -EINVAL;
592
593 if (!atomic_read(&dlfb->usb_active))
594 return 0;
595
596 urb = dlfb_get_urb(dlfb);
597 if (!urb)
598 return 0;
599 cmd = urb->transfer_buffer;
600
601 for (i = y; i < y + height ; i++) {
602 const int line_offset = dlfb->info->fix.line_length * i;
603 const int byte_offset = line_offset + (x * BPP);
604
605 if (dlfb_render_hline(dlfb, &urb,
606 (char *) dlfb->info->fix.smem_start,
607 &cmd, byte_offset, width * BPP,
608 &bytes_identical, &bytes_sent))
609 goto error;
610 }
611
612 if (cmd > (char *) urb->transfer_buffer) {
613
614 int len = cmd - (char *) urb->transfer_buffer;
615 ret = dlfb_submit_urb(dlfb, urb, len);
616 bytes_sent += len;
617 } else
618 dlfb_urb_completion(urb);
619
620error:
621 atomic_add(bytes_sent, &dlfb->bytes_sent);
622 atomic_add(bytes_identical, &dlfb->bytes_identical);
623 atomic_add(width*height*2, &dlfb->bytes_rendered);
624 end_cycles = get_cycles();
625 atomic_add(((unsigned int) ((end_cycles - start_cycles)
626 >> 10)),
627 &dlfb->cpu_kcycles_used);
628
629 return 0;
630}
631
632
633
634
635
636
637
638static ssize_t dlfb_ops_write(struct fb_info *info, const char __user *buf,
639 size_t count, loff_t *ppos)
640{
641 ssize_t result;
642 struct dlfb_data *dlfb = info->par;
643 u32 offset = (u32) *ppos;
644
645 result = fb_sys_write(info, buf, count, ppos);
646
647 if (result > 0) {
648 int start = max((int)(offset / info->fix.line_length), 0);
649 int lines = min((u32)((result / info->fix.line_length) + 1),
650 (u32)info->var.yres);
651
652 dlfb_handle_damage(dlfb, 0, start, info->var.xres,
653 lines, info->screen_base);
654 }
655
656 return result;
657}
658
659
660static void dlfb_ops_copyarea(struct fb_info *info,
661 const struct fb_copyarea *area)
662{
663
664 struct dlfb_data *dlfb = info->par;
665
666 sys_copyarea(info, area);
667
668 dlfb_handle_damage(dlfb, area->dx, area->dy,
669 area->width, area->height, info->screen_base);
670}
671
672static void dlfb_ops_imageblit(struct fb_info *info,
673 const struct fb_image *image)
674{
675 struct dlfb_data *dlfb = info->par;
676
677 sys_imageblit(info, image);
678
679 dlfb_handle_damage(dlfb, image->dx, image->dy,
680 image->width, image->height, info->screen_base);
681}
682
683static void dlfb_ops_fillrect(struct fb_info *info,
684 const struct fb_fillrect *rect)
685{
686 struct dlfb_data *dlfb = info->par;
687
688 sys_fillrect(info, rect);
689
690 dlfb_handle_damage(dlfb, rect->dx, rect->dy, rect->width,
691 rect->height, info->screen_base);
692}
693
694
695
696
697
698
699
700static void dlfb_dpy_deferred_io(struct fb_info *info,
701 struct list_head *pagelist)
702{
703 struct page *cur;
704 struct fb_deferred_io *fbdefio = info->fbdefio;
705 struct dlfb_data *dlfb = info->par;
706 struct urb *urb;
707 char *cmd;
708 cycles_t start_cycles, end_cycles;
709 int bytes_sent = 0;
710 int bytes_identical = 0;
711 int bytes_rendered = 0;
712
713 if (!fb_defio)
714 return;
715
716 if (!atomic_read(&dlfb->usb_active))
717 return;
718
719 start_cycles = get_cycles();
720
721 urb = dlfb_get_urb(dlfb);
722 if (!urb)
723 return;
724
725 cmd = urb->transfer_buffer;
726
727
728 list_for_each_entry(cur, &fbdefio->pagelist, lru) {
729
730 if (dlfb_render_hline(dlfb, &urb, (char *) info->fix.smem_start,
731 &cmd, cur->index << PAGE_SHIFT,
732 PAGE_SIZE, &bytes_identical, &bytes_sent))
733 goto error;
734 bytes_rendered += PAGE_SIZE;
735 }
736
737 if (cmd > (char *) urb->transfer_buffer) {
738
739 int len = cmd - (char *) urb->transfer_buffer;
740 dlfb_submit_urb(dlfb, urb, len);
741 bytes_sent += len;
742 } else
743 dlfb_urb_completion(urb);
744
745error:
746 atomic_add(bytes_sent, &dlfb->bytes_sent);
747 atomic_add(bytes_identical, &dlfb->bytes_identical);
748 atomic_add(bytes_rendered, &dlfb->bytes_rendered);
749 end_cycles = get_cycles();
750 atomic_add(((unsigned int) ((end_cycles - start_cycles)
751 >> 10)),
752 &dlfb->cpu_kcycles_used);
753}
754
755static int dlfb_get_edid(struct dlfb_data *dlfb, char *edid, int len)
756{
757 int i, ret;
758 char *rbuf;
759
760 rbuf = kmalloc(2, GFP_KERNEL);
761 if (!rbuf)
762 return 0;
763
764 for (i = 0; i < len; i++) {
765 ret = usb_control_msg(dlfb->udev,
766 usb_rcvctrlpipe(dlfb->udev, 0), 0x02,
767 (0x80 | (0x02 << 5)), i << 8, 0xA1,
768 rbuf, 2, USB_CTRL_GET_TIMEOUT);
769 if (ret < 2) {
770 dev_err(&dlfb->udev->dev,
771 "Read EDID byte %d failed: %d\n", i, ret);
772 i--;
773 break;
774 }
775 edid[i] = rbuf[1];
776 }
777
778 kfree(rbuf);
779
780 return i;
781}
782
783static int dlfb_ops_ioctl(struct fb_info *info, unsigned int cmd,
784 unsigned long arg)
785{
786
787 struct dlfb_data *dlfb = info->par;
788
789 if (!atomic_read(&dlfb->usb_active))
790 return 0;
791
792
793 if (cmd == DLFB_IOCTL_RETURN_EDID) {
794 void __user *edid = (void __user *)arg;
795 if (copy_to_user(edid, dlfb->edid, dlfb->edid_size))
796 return -EFAULT;
797 return 0;
798 }
799
800
801 if (cmd == DLFB_IOCTL_REPORT_DAMAGE) {
802 struct dloarea area;
803
804 if (copy_from_user(&area, (void __user *)arg,
805 sizeof(struct dloarea)))
806 return -EFAULT;
807
808
809
810
811
812
813
814
815 if (info->fbdefio)
816 info->fbdefio->delay = DL_DEFIO_WRITE_DISABLE;
817
818 if (area.x < 0)
819 area.x = 0;
820
821 if (area.x > info->var.xres)
822 area.x = info->var.xres;
823
824 if (area.y < 0)
825 area.y = 0;
826
827 if (area.y > info->var.yres)
828 area.y = info->var.yres;
829
830 dlfb_handle_damage(dlfb, area.x, area.y, area.w, area.h,
831 info->screen_base);
832 }
833
834 return 0;
835}
836
837
838static int
839dlfb_ops_setcolreg(unsigned regno, unsigned red, unsigned green,
840 unsigned blue, unsigned transp, struct fb_info *info)
841{
842 int err = 0;
843
844 if (regno >= info->cmap.len)
845 return 1;
846
847 if (regno < 16) {
848 if (info->var.red.offset == 10) {
849
850 ((u32 *) (info->pseudo_palette))[regno] =
851 ((red & 0xf800) >> 1) |
852 ((green & 0xf800) >> 6) | ((blue & 0xf800) >> 11);
853 } else {
854
855 ((u32 *) (info->pseudo_palette))[regno] =
856 ((red & 0xf800)) |
857 ((green & 0xfc00) >> 5) | ((blue & 0xf800) >> 11);
858 }
859 }
860
861 return err;
862}
863
864
865
866
867
868
869static int dlfb_ops_open(struct fb_info *info, int user)
870{
871 struct dlfb_data *dlfb = info->par;
872
873
874
875
876
877
878 if ((user == 0) && (!console))
879 return -EBUSY;
880
881
882 if (dlfb->virtualized)
883 return -ENODEV;
884
885 dlfb->fb_count++;
886
887 kref_get(&dlfb->kref);
888
889 if (fb_defio && (info->fbdefio == NULL)) {
890
891
892 struct fb_deferred_io *fbdefio;
893
894 fbdefio = kzalloc(sizeof(struct fb_deferred_io), GFP_KERNEL);
895
896 if (fbdefio) {
897 fbdefio->delay = DL_DEFIO_WRITE_DELAY;
898 fbdefio->deferred_io = dlfb_dpy_deferred_io;
899 }
900
901 info->fbdefio = fbdefio;
902 fb_deferred_io_init(info);
903 }
904
905 dev_dbg(info->dev, "open, user=%d fb_info=%p count=%d\n",
906 user, info, dlfb->fb_count);
907
908 return 0;
909}
910
911
912
913
914
915
916static void dlfb_free(struct kref *kref)
917{
918 struct dlfb_data *dlfb = container_of(kref, struct dlfb_data, kref);
919
920 vfree(dlfb->backing_buffer);
921 kfree(dlfb->edid);
922 kfree(dlfb);
923}
924
925static void dlfb_release_urb_work(struct work_struct *work)
926{
927 struct urb_node *unode = container_of(work, struct urb_node,
928 release_urb_work.work);
929
930 up(&unode->dlfb->urbs.limit_sem);
931}
932
933static void dlfb_free_framebuffer(struct dlfb_data *dlfb)
934{
935 struct fb_info *info = dlfb->info;
936
937 if (info) {
938 unregister_framebuffer(info);
939
940 if (info->cmap.len != 0)
941 fb_dealloc_cmap(&info->cmap);
942 if (info->monspecs.modedb)
943 fb_destroy_modedb(info->monspecs.modedb);
944 vfree(info->screen_base);
945
946 fb_destroy_modelist(&info->modelist);
947
948 dlfb->info = NULL;
949
950
951 framebuffer_release(info);
952 }
953
954
955 kref_put(&dlfb->kref, dlfb_free);
956}
957
958static void dlfb_free_framebuffer_work(struct work_struct *work)
959{
960 struct dlfb_data *dlfb = container_of(work, struct dlfb_data,
961 free_framebuffer_work.work);
962 dlfb_free_framebuffer(dlfb);
963}
964
965
966
967static int dlfb_ops_release(struct fb_info *info, int user)
968{
969 struct dlfb_data *dlfb = info->par;
970
971 dlfb->fb_count--;
972
973
974 if (dlfb->virtualized && (dlfb->fb_count == 0))
975 schedule_delayed_work(&dlfb->free_framebuffer_work, HZ);
976
977 if ((dlfb->fb_count == 0) && (info->fbdefio)) {
978 fb_deferred_io_cleanup(info);
979 kfree(info->fbdefio);
980 info->fbdefio = NULL;
981 }
982
983 dev_dbg(info->dev, "release, user=%d count=%d\n", user, dlfb->fb_count);
984
985 kref_put(&dlfb->kref, dlfb_free);
986
987 return 0;
988}
989
990
991
992
993
994static int dlfb_is_valid_mode(struct fb_videomode *mode, struct dlfb_data *dlfb)
995{
996 if (mode->xres * mode->yres > dlfb->sku_pixel_limit)
997 return 0;
998
999 return 1;
1000}
1001
1002static void dlfb_var_color_format(struct fb_var_screeninfo *var)
1003{
1004 const struct fb_bitfield red = { 11, 5, 0 };
1005 const struct fb_bitfield green = { 5, 6, 0 };
1006 const struct fb_bitfield blue = { 0, 5, 0 };
1007
1008 var->bits_per_pixel = 16;
1009 var->red = red;
1010 var->green = green;
1011 var->blue = blue;
1012}
1013
1014static int dlfb_ops_check_var(struct fb_var_screeninfo *var,
1015 struct fb_info *info)
1016{
1017 struct fb_videomode mode;
1018 struct dlfb_data *dlfb = info->par;
1019
1020
1021 if ((var->xres * var->yres * 2) > info->fix.smem_len)
1022 return -EINVAL;
1023
1024
1025 dlfb_var_color_format(var);
1026
1027 fb_var_to_videomode(&mode, var);
1028
1029 if (!dlfb_is_valid_mode(&mode, dlfb))
1030 return -EINVAL;
1031
1032 return 0;
1033}
1034
1035static int dlfb_ops_set_par(struct fb_info *info)
1036{
1037 struct dlfb_data *dlfb = info->par;
1038 int result;
1039 u16 *pix_framebuffer;
1040 int i;
1041
1042 result = dlfb_set_video_mode(dlfb, &info->var);
1043
1044 if ((result == 0) && (dlfb->fb_count == 0)) {
1045
1046
1047
1048 pix_framebuffer = (u16 *) info->screen_base;
1049 for (i = 0; i < info->fix.smem_len / 2; i++)
1050 pix_framebuffer[i] = 0x37e6;
1051
1052 dlfb_handle_damage(dlfb, 0, 0, info->var.xres, info->var.yres,
1053 info->screen_base);
1054 }
1055
1056 return result;
1057}
1058
1059
1060static char *dlfb_dummy_render(char *buf)
1061{
1062 *buf++ = 0xAF;
1063 *buf++ = 0x6A;
1064 *buf++ = 0x00;
1065 *buf++ = 0x00;
1066 *buf++ = 0x00;
1067 *buf++ = 0x01;
1068 *buf++ = 0x00;
1069 *buf++ = 0x00;
1070 *buf++ = 0x00;
1071 return buf;
1072}
1073
1074
1075
1076
1077static int dlfb_ops_blank(int blank_mode, struct fb_info *info)
1078{
1079 struct dlfb_data *dlfb = info->par;
1080 char *bufptr;
1081 struct urb *urb;
1082
1083 dev_dbg(info->dev, "blank, mode %d --> %d\n",
1084 dlfb->blank_mode, blank_mode);
1085
1086 if ((dlfb->blank_mode == FB_BLANK_POWERDOWN) &&
1087 (blank_mode != FB_BLANK_POWERDOWN)) {
1088
1089
1090 dlfb_set_video_mode(dlfb, &info->var);
1091 }
1092
1093 urb = dlfb_get_urb(dlfb);
1094 if (!urb)
1095 return 0;
1096
1097 bufptr = (char *) urb->transfer_buffer;
1098 bufptr = dlfb_vidreg_lock(bufptr);
1099 bufptr = dlfb_blanking(bufptr, blank_mode);
1100 bufptr = dlfb_vidreg_unlock(bufptr);
1101
1102
1103 bufptr = dlfb_dummy_render(bufptr);
1104
1105 dlfb_submit_urb(dlfb, urb, bufptr -
1106 (char *) urb->transfer_buffer);
1107
1108 dlfb->blank_mode = blank_mode;
1109
1110 return 0;
1111}
1112
1113static struct fb_ops dlfb_ops = {
1114 .owner = THIS_MODULE,
1115 .fb_read = fb_sys_read,
1116 .fb_write = dlfb_ops_write,
1117 .fb_setcolreg = dlfb_ops_setcolreg,
1118 .fb_fillrect = dlfb_ops_fillrect,
1119 .fb_copyarea = dlfb_ops_copyarea,
1120 .fb_imageblit = dlfb_ops_imageblit,
1121 .fb_mmap = dlfb_ops_mmap,
1122 .fb_ioctl = dlfb_ops_ioctl,
1123 .fb_open = dlfb_ops_open,
1124 .fb_release = dlfb_ops_release,
1125 .fb_blank = dlfb_ops_blank,
1126 .fb_check_var = dlfb_ops_check_var,
1127 .fb_set_par = dlfb_ops_set_par,
1128};
1129
1130
1131
1132
1133
1134
1135static int dlfb_realloc_framebuffer(struct dlfb_data *dlfb, struct fb_info *info)
1136{
1137 int old_len = info->fix.smem_len;
1138 int new_len;
1139 unsigned char *old_fb = info->screen_base;
1140 unsigned char *new_fb;
1141 unsigned char *new_back = NULL;
1142
1143 new_len = info->fix.line_length * info->var.yres;
1144
1145 if (PAGE_ALIGN(new_len) > old_len) {
1146
1147
1148
1149 new_fb = vmalloc(new_len);
1150 if (!new_fb) {
1151 dev_err(info->dev, "Virtual framebuffer alloc failed\n");
1152 return -ENOMEM;
1153 }
1154
1155 if (info->screen_base) {
1156 memcpy(new_fb, old_fb, old_len);
1157 vfree(info->screen_base);
1158 }
1159
1160 info->screen_base = new_fb;
1161 info->fix.smem_len = PAGE_ALIGN(new_len);
1162 info->fix.smem_start = (unsigned long) new_fb;
1163 info->flags = udlfb_info_flags;
1164
1165
1166
1167
1168
1169
1170
1171 if (shadow)
1172 new_back = vzalloc(new_len);
1173 if (!new_back)
1174 dev_info(info->dev,
1175 "No shadow/backing buffer allocated\n");
1176 else {
1177 vfree(dlfb->backing_buffer);
1178 dlfb->backing_buffer = new_back;
1179 }
1180 }
1181 return 0;
1182}
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198static int dlfb_setup_modes(struct dlfb_data *dlfb,
1199 struct fb_info *info,
1200 char *default_edid, size_t default_edid_size)
1201{
1202 char *edid;
1203 int i, result = 0, tries = 3;
1204 struct device *dev = info->device;
1205 struct fb_videomode *mode;
1206 const struct fb_videomode *default_vmode = NULL;
1207
1208 if (info->dev) {
1209
1210 mutex_lock(&info->lock);
1211
1212 dev = info->dev;
1213 }
1214
1215 edid = kmalloc(EDID_LENGTH, GFP_KERNEL);
1216 if (!edid) {
1217 result = -ENOMEM;
1218 goto error;
1219 }
1220
1221 fb_destroy_modelist(&info->modelist);
1222 memset(&info->monspecs, 0, sizeof(info->monspecs));
1223
1224
1225
1226
1227
1228
1229 while (tries--) {
1230
1231 i = dlfb_get_edid(dlfb, edid, EDID_LENGTH);
1232
1233 if (i >= EDID_LENGTH)
1234 fb_edid_to_monspecs(edid, &info->monspecs);
1235
1236 if (info->monspecs.modedb_len > 0) {
1237 dlfb->edid = edid;
1238 dlfb->edid_size = i;
1239 break;
1240 }
1241 }
1242
1243
1244 if (info->monspecs.modedb_len == 0) {
1245 dev_err(dev, "Unable to get valid EDID from device/display\n");
1246
1247 if (dlfb->edid) {
1248 fb_edid_to_monspecs(dlfb->edid, &info->monspecs);
1249 if (info->monspecs.modedb_len > 0)
1250 dev_err(dev, "Using previously queried EDID\n");
1251 }
1252 }
1253
1254
1255 if (info->monspecs.modedb_len == 0) {
1256 if (default_edid_size >= EDID_LENGTH) {
1257 fb_edid_to_monspecs(default_edid, &info->monspecs);
1258 if (info->monspecs.modedb_len > 0) {
1259 memcpy(edid, default_edid, default_edid_size);
1260 dlfb->edid = edid;
1261 dlfb->edid_size = default_edid_size;
1262 dev_err(dev, "Using default/backup EDID\n");
1263 }
1264 }
1265 }
1266
1267
1268 if (info->monspecs.modedb_len > 0) {
1269
1270 for (i = 0; i < info->monspecs.modedb_len; i++) {
1271 mode = &info->monspecs.modedb[i];
1272 if (dlfb_is_valid_mode(mode, dlfb)) {
1273 fb_add_videomode(mode, &info->modelist);
1274 } else {
1275 dev_dbg(dev, "Specified mode %dx%d too big\n",
1276 mode->xres, mode->yres);
1277 if (i == 0)
1278
1279 info->monspecs.misc
1280 &= ~FB_MISC_1ST_DETAIL;
1281 }
1282 }
1283
1284 default_vmode = fb_find_best_display(&info->monspecs,
1285 &info->modelist);
1286 }
1287
1288
1289 if (default_vmode == NULL) {
1290
1291 struct fb_videomode fb_vmode = {0};
1292
1293
1294
1295
1296
1297
1298
1299 for (i = 0; i < VESA_MODEDB_SIZE; i++) {
1300 mode = (struct fb_videomode *)&vesa_modes[i];
1301 if (dlfb_is_valid_mode(mode, dlfb))
1302 fb_add_videomode(mode, &info->modelist);
1303 else
1304 dev_dbg(dev, "VESA mode %dx%d too big\n",
1305 mode->xres, mode->yres);
1306 }
1307
1308
1309
1310
1311
1312 fb_vmode.xres = 800;
1313 fb_vmode.yres = 600;
1314 fb_vmode.refresh = 60;
1315 default_vmode = fb_find_nearest_mode(&fb_vmode,
1316 &info->modelist);
1317 }
1318
1319
1320 if ((default_vmode != NULL) && (dlfb->fb_count == 0)) {
1321
1322 fb_videomode_to_var(&info->var, default_vmode);
1323 dlfb_var_color_format(&info->var);
1324
1325
1326
1327
1328 memcpy(&info->fix, &dlfb_fix, sizeof(dlfb_fix));
1329 info->fix.line_length = info->var.xres *
1330 (info->var.bits_per_pixel / 8);
1331
1332 result = dlfb_realloc_framebuffer(dlfb, info);
1333
1334 } else
1335 result = -EINVAL;
1336
1337error:
1338 if (edid && (dlfb->edid != edid))
1339 kfree(edid);
1340
1341 if (info->dev)
1342 mutex_unlock(&info->lock);
1343
1344 return result;
1345}
1346
1347static ssize_t metrics_bytes_rendered_show(struct device *fbdev,
1348 struct device_attribute *a, char *buf) {
1349 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1350 struct dlfb_data *dlfb = fb_info->par;
1351 return snprintf(buf, PAGE_SIZE, "%u\n",
1352 atomic_read(&dlfb->bytes_rendered));
1353}
1354
1355static ssize_t metrics_bytes_identical_show(struct device *fbdev,
1356 struct device_attribute *a, char *buf) {
1357 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1358 struct dlfb_data *dlfb = fb_info->par;
1359 return snprintf(buf, PAGE_SIZE, "%u\n",
1360 atomic_read(&dlfb->bytes_identical));
1361}
1362
1363static ssize_t metrics_bytes_sent_show(struct device *fbdev,
1364 struct device_attribute *a, char *buf) {
1365 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1366 struct dlfb_data *dlfb = fb_info->par;
1367 return snprintf(buf, PAGE_SIZE, "%u\n",
1368 atomic_read(&dlfb->bytes_sent));
1369}
1370
1371static ssize_t metrics_cpu_kcycles_used_show(struct device *fbdev,
1372 struct device_attribute *a, char *buf) {
1373 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1374 struct dlfb_data *dlfb = fb_info->par;
1375 return snprintf(buf, PAGE_SIZE, "%u\n",
1376 atomic_read(&dlfb->cpu_kcycles_used));
1377}
1378
1379static ssize_t edid_show(
1380 struct file *filp,
1381 struct kobject *kobj, struct bin_attribute *a,
1382 char *buf, loff_t off, size_t count) {
1383 struct device *fbdev = container_of(kobj, struct device, kobj);
1384 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1385 struct dlfb_data *dlfb = fb_info->par;
1386
1387 if (dlfb->edid == NULL)
1388 return 0;
1389
1390 if ((off >= dlfb->edid_size) || (count > dlfb->edid_size))
1391 return 0;
1392
1393 if (off + count > dlfb->edid_size)
1394 count = dlfb->edid_size - off;
1395
1396 memcpy(buf, dlfb->edid, count);
1397
1398 return count;
1399}
1400
1401static ssize_t edid_store(
1402 struct file *filp,
1403 struct kobject *kobj, struct bin_attribute *a,
1404 char *src, loff_t src_off, size_t src_size) {
1405 struct device *fbdev = container_of(kobj, struct device, kobj);
1406 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1407 struct dlfb_data *dlfb = fb_info->par;
1408 int ret;
1409
1410
1411 if ((src_size != EDID_LENGTH) || (src_off != 0))
1412 return -EINVAL;
1413
1414 ret = dlfb_setup_modes(dlfb, fb_info, src, src_size);
1415 if (ret)
1416 return ret;
1417
1418 if (!dlfb->edid || memcmp(src, dlfb->edid, src_size))
1419 return -EINVAL;
1420
1421 dlfb_ops_set_par(fb_info);
1422 return src_size;
1423}
1424
1425static ssize_t metrics_reset_store(struct device *fbdev,
1426 struct device_attribute *attr,
1427 const char *buf, size_t count)
1428{
1429 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1430 struct dlfb_data *dlfb = fb_info->par;
1431
1432 atomic_set(&dlfb->bytes_rendered, 0);
1433 atomic_set(&dlfb->bytes_identical, 0);
1434 atomic_set(&dlfb->bytes_sent, 0);
1435 atomic_set(&dlfb->cpu_kcycles_used, 0);
1436
1437 return count;
1438}
1439
1440static const struct bin_attribute edid_attr = {
1441 .attr.name = "edid",
1442 .attr.mode = 0666,
1443 .size = EDID_LENGTH,
1444 .read = edid_show,
1445 .write = edid_store
1446};
1447
1448static const struct device_attribute fb_device_attrs[] = {
1449 __ATTR_RO(metrics_bytes_rendered),
1450 __ATTR_RO(metrics_bytes_identical),
1451 __ATTR_RO(metrics_bytes_sent),
1452 __ATTR_RO(metrics_cpu_kcycles_used),
1453 __ATTR(metrics_reset, S_IWUSR, NULL, metrics_reset_store),
1454};
1455
1456
1457
1458
1459static int dlfb_select_std_channel(struct dlfb_data *dlfb)
1460{
1461 int ret;
1462 void *buf;
1463 static const u8 set_def_chn[] = {
1464 0x57, 0xCD, 0xDC, 0xA7,
1465 0x1C, 0x88, 0x5E, 0x15,
1466 0x60, 0xFE, 0xC6, 0x97,
1467 0x16, 0x3D, 0x47, 0xF2 };
1468
1469 buf = kmemdup(set_def_chn, sizeof(set_def_chn), GFP_KERNEL);
1470
1471 if (!buf)
1472 return -ENOMEM;
1473
1474 ret = usb_control_msg(dlfb->udev, usb_sndctrlpipe(dlfb->udev, 0),
1475 NR_USB_REQUEST_CHANNEL,
1476 (USB_DIR_OUT | USB_TYPE_VENDOR), 0, 0,
1477 buf, sizeof(set_def_chn), USB_CTRL_SET_TIMEOUT);
1478
1479 kfree(buf);
1480
1481 return ret;
1482}
1483
1484static int dlfb_parse_vendor_descriptor(struct dlfb_data *dlfb,
1485 struct usb_interface *intf)
1486{
1487 char *desc;
1488 char *buf;
1489 char *desc_end;
1490 int total_len;
1491
1492 buf = kzalloc(MAX_VENDOR_DESCRIPTOR_SIZE, GFP_KERNEL);
1493 if (!buf)
1494 return false;
1495 desc = buf;
1496
1497 total_len = usb_get_descriptor(interface_to_usbdev(intf),
1498 0x5f,
1499 0, desc, MAX_VENDOR_DESCRIPTOR_SIZE);
1500
1501
1502 if (total_len < 0) {
1503 if (0 == usb_get_extra_descriptor(intf->cur_altsetting,
1504 0x5f, &desc))
1505 total_len = (int) desc[0];
1506 }
1507
1508 if (total_len > 5) {
1509 dev_info(&intf->dev,
1510 "vendor descriptor length: %d data: %11ph\n",
1511 total_len, desc);
1512
1513 if ((desc[0] != total_len) ||
1514 (desc[1] != 0x5f) ||
1515 (desc[2] != 0x01) ||
1516 (desc[3] != 0x00) ||
1517 (desc[4] != total_len - 2))
1518 goto unrecognized;
1519
1520 desc_end = desc + total_len;
1521 desc += 5;
1522
1523 while (desc < desc_end) {
1524 u8 length;
1525 u16 key;
1526
1527 key = *desc++;
1528 key |= (u16)*desc++ << 8;
1529 length = *desc++;
1530
1531 switch (key) {
1532 case 0x0200: {
1533 u32 max_area = *desc++;
1534 max_area |= (u32)*desc++ << 8;
1535 max_area |= (u32)*desc++ << 16;
1536 max_area |= (u32)*desc++ << 24;
1537 dev_warn(&intf->dev,
1538 "DL chip limited to %d pixel modes\n",
1539 max_area);
1540 dlfb->sku_pixel_limit = max_area;
1541 break;
1542 }
1543 default:
1544 break;
1545 }
1546 desc += length;
1547 }
1548 } else {
1549 dev_info(&intf->dev, "vendor descriptor not available (%d)\n",
1550 total_len);
1551 }
1552
1553 goto success;
1554
1555unrecognized:
1556
1557 dev_err(&intf->dev, "Unrecognized vendor firmware descriptor\n");
1558
1559success:
1560 kfree(buf);
1561 return true;
1562}
1563
1564static void dlfb_init_framebuffer_work(struct work_struct *work);
1565
1566static int dlfb_usb_probe(struct usb_interface *intf,
1567 const struct usb_device_id *id)
1568{
1569 struct dlfb_data *dlfb;
1570 int retval = -ENOMEM;
1571 struct usb_device *usbdev = interface_to_usbdev(intf);
1572
1573
1574 dlfb = kzalloc(sizeof(*dlfb), GFP_KERNEL);
1575 if (!dlfb) {
1576 dev_err(&intf->dev, "%s: failed to allocate dlfb\n", __func__);
1577 goto error;
1578 }
1579
1580 kref_init(&dlfb->kref);
1581
1582 dlfb->udev = usbdev;
1583 usb_set_intfdata(intf, dlfb);
1584
1585 dev_dbg(&intf->dev, "console enable=%d\n", console);
1586 dev_dbg(&intf->dev, "fb_defio enable=%d\n", fb_defio);
1587 dev_dbg(&intf->dev, "shadow enable=%d\n", shadow);
1588
1589 dlfb->sku_pixel_limit = 2048 * 1152;
1590
1591 if (!dlfb_parse_vendor_descriptor(dlfb, intf)) {
1592 dev_err(&intf->dev,
1593 "firmware not recognized, incompatible device?\n");
1594 goto error;
1595 }
1596
1597 if (pixel_limit) {
1598 dev_warn(&intf->dev,
1599 "DL chip limit of %d overridden to %d\n",
1600 dlfb->sku_pixel_limit, pixel_limit);
1601 dlfb->sku_pixel_limit = pixel_limit;
1602 }
1603
1604
1605 if (!dlfb_alloc_urb_list(dlfb, WRITES_IN_FLIGHT, MAX_TRANSFER)) {
1606 retval = -ENOMEM;
1607 dev_err(&intf->dev, "unable to allocate urb list\n");
1608 goto error;
1609 }
1610
1611 kref_get(&dlfb->kref);
1612
1613
1614
1615
1616 INIT_DELAYED_WORK(&dlfb->init_framebuffer_work,
1617 dlfb_init_framebuffer_work);
1618 schedule_delayed_work(&dlfb->init_framebuffer_work, 0);
1619
1620 return 0;
1621
1622error:
1623 if (dlfb) {
1624
1625 kref_put(&dlfb->kref, dlfb_free);
1626
1627
1628 }
1629
1630 return retval;
1631}
1632
1633static void dlfb_init_framebuffer_work(struct work_struct *work)
1634{
1635 int i, retval;
1636 struct fb_info *info;
1637 const struct device_attribute *attr;
1638 struct dlfb_data *dlfb = container_of(work, struct dlfb_data,
1639 init_framebuffer_work.work);
1640
1641
1642 info = framebuffer_alloc(0, &dlfb->udev->dev);
1643 if (!info) {
1644 dev_err(&dlfb->udev->dev, "framebuffer_alloc failed\n");
1645 goto error;
1646 }
1647
1648 dlfb->info = info;
1649 info->par = dlfb;
1650 info->pseudo_palette = dlfb->pseudo_palette;
1651 info->fbops = &dlfb_ops;
1652
1653 retval = fb_alloc_cmap(&info->cmap, 256, 0);
1654 if (retval < 0) {
1655 dev_err(info->device, "cmap allocation failed: %d\n", retval);
1656 goto error;
1657 }
1658
1659 INIT_DELAYED_WORK(&dlfb->free_framebuffer_work,
1660 dlfb_free_framebuffer_work);
1661
1662 INIT_LIST_HEAD(&info->modelist);
1663
1664 retval = dlfb_setup_modes(dlfb, info, NULL, 0);
1665 if (retval != 0) {
1666 dev_err(info->device,
1667 "unable to find common mode for display and adapter\n");
1668 goto error;
1669 }
1670
1671
1672
1673 atomic_set(&dlfb->usb_active, 1);
1674 dlfb_select_std_channel(dlfb);
1675
1676 dlfb_ops_check_var(&info->var, info);
1677 dlfb_ops_set_par(info);
1678
1679 retval = register_framebuffer(info);
1680 if (retval < 0) {
1681 dev_err(info->device, "unable to register framebuffer: %d\n",
1682 retval);
1683 goto error;
1684 }
1685
1686 for (i = 0; i < ARRAY_SIZE(fb_device_attrs); i++) {
1687 attr = &fb_device_attrs[i];
1688 retval = device_create_file(info->dev, attr);
1689 if (retval)
1690 dev_warn(info->device,
1691 "failed to create '%s' attribute: %d\n",
1692 attr->attr.name, retval);
1693 }
1694
1695 retval = device_create_bin_file(info->dev, &edid_attr);
1696 if (retval)
1697 dev_warn(info->device, "failed to create '%s' attribute: %d\n",
1698 edid_attr.attr.name, retval);
1699
1700 dev_info(info->device,
1701 "%s is DisplayLink USB device (%dx%d, %dK framebuffer memory)\n",
1702 dev_name(info->dev), info->var.xres, info->var.yres,
1703 ((dlfb->backing_buffer) ?
1704 info->fix.smem_len * 2 : info->fix.smem_len) >> 10);
1705 return;
1706
1707error:
1708 dlfb_free_framebuffer(dlfb);
1709}
1710
1711static void dlfb_usb_disconnect(struct usb_interface *intf)
1712{
1713 struct dlfb_data *dlfb;
1714 struct fb_info *info;
1715 int i;
1716
1717 dlfb = usb_get_intfdata(intf);
1718 info = dlfb->info;
1719
1720 dev_dbg(&intf->dev, "USB disconnect starting\n");
1721
1722
1723 dlfb->virtualized = true;
1724
1725
1726 atomic_set(&dlfb->usb_active, 0);
1727
1728
1729 dlfb_free_urb_list(dlfb);
1730
1731 if (info) {
1732
1733 for (i = 0; i < ARRAY_SIZE(fb_device_attrs); i++)
1734 device_remove_file(info->dev, &fb_device_attrs[i]);
1735 device_remove_bin_file(info->dev, &edid_attr);
1736 unlink_framebuffer(info);
1737 }
1738
1739 usb_set_intfdata(intf, NULL);
1740 dlfb->udev = NULL;
1741
1742
1743 if (dlfb->fb_count == 0)
1744 schedule_delayed_work(&dlfb->free_framebuffer_work, 0);
1745
1746
1747 kref_put(&dlfb->kref, dlfb_free);
1748
1749
1750}
1751
1752static struct usb_driver dlfb_driver = {
1753 .name = "udlfb",
1754 .probe = dlfb_usb_probe,
1755 .disconnect = dlfb_usb_disconnect,
1756 .id_table = id_table,
1757};
1758
1759module_usb_driver(dlfb_driver);
1760
1761static void dlfb_urb_completion(struct urb *urb)
1762{
1763 struct urb_node *unode = urb->context;
1764 struct dlfb_data *dlfb = unode->dlfb;
1765 unsigned long flags;
1766
1767 switch (urb->status) {
1768 case 0:
1769
1770 break;
1771 case -ECONNRESET:
1772 case -ENOENT:
1773 case -ESHUTDOWN:
1774
1775 break;
1776 default:
1777 dev_err(&dlfb->udev->dev,
1778 "%s - nonzero write bulk status received: %d\n",
1779 __func__, urb->status);
1780 atomic_set(&dlfb->lost_pixels, 1);
1781 break;
1782 }
1783
1784 urb->transfer_buffer_length = dlfb->urbs.size;
1785
1786 spin_lock_irqsave(&dlfb->urbs.lock, flags);
1787 list_add_tail(&unode->entry, &dlfb->urbs.list);
1788 dlfb->urbs.available++;
1789 spin_unlock_irqrestore(&dlfb->urbs.lock, flags);
1790
1791
1792
1793
1794
1795 if (fb_defio)
1796 schedule_delayed_work(&unode->release_urb_work, 0);
1797 else
1798 up(&dlfb->urbs.limit_sem);
1799}
1800
1801static void dlfb_free_urb_list(struct dlfb_data *dlfb)
1802{
1803 int count = dlfb->urbs.count;
1804 struct list_head *node;
1805 struct urb_node *unode;
1806 struct urb *urb;
1807 int ret;
1808 unsigned long flags;
1809
1810
1811 while (count--) {
1812
1813
1814 ret = down_interruptible(&dlfb->urbs.limit_sem);
1815 if (ret)
1816 break;
1817
1818 spin_lock_irqsave(&dlfb->urbs.lock, flags);
1819
1820 node = dlfb->urbs.list.next;
1821 list_del_init(node);
1822
1823 spin_unlock_irqrestore(&dlfb->urbs.lock, flags);
1824
1825 unode = list_entry(node, struct urb_node, entry);
1826 urb = unode->urb;
1827
1828
1829 usb_free_coherent(urb->dev, dlfb->urbs.size,
1830 urb->transfer_buffer, urb->transfer_dma);
1831 usb_free_urb(urb);
1832 kfree(node);
1833 }
1834
1835 dlfb->urbs.count = 0;
1836}
1837
1838static int dlfb_alloc_urb_list(struct dlfb_data *dlfb, int count, size_t size)
1839{
1840 int i = 0;
1841 struct urb *urb;
1842 struct urb_node *unode;
1843 char *buf;
1844
1845 spin_lock_init(&dlfb->urbs.lock);
1846
1847 dlfb->urbs.size = size;
1848 INIT_LIST_HEAD(&dlfb->urbs.list);
1849
1850 while (i < count) {
1851 unode = kzalloc(sizeof(*unode), GFP_KERNEL);
1852 if (!unode)
1853 break;
1854 unode->dlfb = dlfb;
1855
1856 INIT_DELAYED_WORK(&unode->release_urb_work,
1857 dlfb_release_urb_work);
1858
1859 urb = usb_alloc_urb(0, GFP_KERNEL);
1860 if (!urb) {
1861 kfree(unode);
1862 break;
1863 }
1864 unode->urb = urb;
1865
1866 buf = usb_alloc_coherent(dlfb->udev, MAX_TRANSFER, GFP_KERNEL,
1867 &urb->transfer_dma);
1868 if (!buf) {
1869 kfree(unode);
1870 usb_free_urb(urb);
1871 break;
1872 }
1873
1874
1875 usb_fill_bulk_urb(urb, dlfb->udev, usb_sndbulkpipe(dlfb->udev, 1),
1876 buf, size, dlfb_urb_completion, unode);
1877 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1878
1879 list_add_tail(&unode->entry, &dlfb->urbs.list);
1880
1881 i++;
1882 }
1883
1884 sema_init(&dlfb->urbs.limit_sem, i);
1885 dlfb->urbs.count = i;
1886 dlfb->urbs.available = i;
1887
1888 return i;
1889}
1890
1891static struct urb *dlfb_get_urb(struct dlfb_data *dlfb)
1892{
1893 int ret;
1894 struct list_head *entry;
1895 struct urb_node *unode;
1896 unsigned long flags;
1897
1898
1899 ret = down_timeout(&dlfb->urbs.limit_sem, GET_URB_TIMEOUT);
1900 if (ret) {
1901 atomic_set(&dlfb->lost_pixels, 1);
1902 dev_warn(&dlfb->udev->dev,
1903 "wait for urb interrupted: %d available: %d\n",
1904 ret, dlfb->urbs.available);
1905 return NULL;
1906 }
1907
1908 spin_lock_irqsave(&dlfb->urbs.lock, flags);
1909
1910 BUG_ON(list_empty(&dlfb->urbs.list));
1911 entry = dlfb->urbs.list.next;
1912 list_del_init(entry);
1913 dlfb->urbs.available--;
1914
1915 spin_unlock_irqrestore(&dlfb->urbs.lock, flags);
1916
1917 unode = list_entry(entry, struct urb_node, entry);
1918 return unode->urb;
1919}
1920
1921static int dlfb_submit_urb(struct dlfb_data *dlfb, struct urb *urb, size_t len)
1922{
1923 int ret;
1924
1925 BUG_ON(len > dlfb->urbs.size);
1926
1927 urb->transfer_buffer_length = len;
1928 ret = usb_submit_urb(urb, GFP_KERNEL);
1929 if (ret) {
1930 dlfb_urb_completion(urb);
1931 atomic_set(&dlfb->lost_pixels, 1);
1932 dev_err(&dlfb->udev->dev, "submit urb error: %d\n", ret);
1933 }
1934 return ret;
1935}
1936
1937module_param(console, bool, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
1938MODULE_PARM_DESC(console, "Allow fbcon to open framebuffer");
1939
1940module_param(fb_defio, bool, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
1941MODULE_PARM_DESC(fb_defio, "Page fault detection of mmap writes");
1942
1943module_param(shadow, bool, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
1944MODULE_PARM_DESC(shadow, "Shadow vid mem. Disable to save mem but lose perf");
1945
1946module_param(pixel_limit, int, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
1947MODULE_PARM_DESC(pixel_limit, "Force limit on max mode (in x*y pixels)");
1948
1949MODULE_AUTHOR("Roberto De Ioris <roberto@unbit.it>, "
1950 "Jaya Kumar <jayakumar.lkml@gmail.com>, "
1951 "Bernie Thompson <bernie@plugable.com>");
1952MODULE_DESCRIPTION("DisplayLink kernel framebuffer driver");
1953MODULE_LICENSE("GPL");
1954
1955