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