112eb90f1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2f7018c21STomi Valkeinen /*
3f7018c21STomi Valkeinen * udlfb.c -- Framebuffer driver for DisplayLink USB controller
4f7018c21STomi Valkeinen *
5f7018c21STomi Valkeinen * Copyright (C) 2009 Roberto De Ioris <roberto@unbit.it>
6f7018c21STomi Valkeinen * Copyright (C) 2009 Jaya Kumar <jayakumar.lkml@gmail.com>
7f7018c21STomi Valkeinen * Copyright (C) 2009 Bernie Thompson <bernie@plugable.com>
8f7018c21STomi Valkeinen *
9f7018c21STomi Valkeinen * Layout is based on skeletonfb by James Simmons and Geert Uytterhoeven,
10f7018c21STomi Valkeinen * usb-skeleton by GregKH.
11f7018c21STomi Valkeinen *
12f7018c21STomi Valkeinen * Device-specific portions based on information from Displaylink, with work
13f7018c21STomi Valkeinen * from Florian Echtler, Henrik Bjerregaard Pedersen, and others.
14f7018c21STomi Valkeinen */
15f7018c21STomi Valkeinen
16f7018c21STomi Valkeinen #include <linux/module.h>
17f7018c21STomi Valkeinen #include <linux/kernel.h>
18f7018c21STomi Valkeinen #include <linux/init.h>
19f7018c21STomi Valkeinen #include <linux/usb.h>
20f7018c21STomi Valkeinen #include <linux/uaccess.h>
21f7018c21STomi Valkeinen #include <linux/mm.h>
22f7018c21STomi Valkeinen #include <linux/fb.h>
23f7018c21STomi Valkeinen #include <linux/vmalloc.h>
24f7018c21STomi Valkeinen #include <linux/slab.h>
25f7018c21STomi Valkeinen #include <linux/delay.h>
26*5f60d5f6SAl Viro #include <linux/unaligned.h>
27f7018c21STomi Valkeinen #include <video/udlfb.h>
28f7018c21STomi Valkeinen #include "edid.h"
29f7018c21STomi Valkeinen
30ed9de4edSAlan Stern #define OUT_EP_NUM 1 /* The endpoint number we will use */
31ed9de4edSAlan Stern
32fa738a5cSLadislav Michl static const struct fb_fix_screeninfo dlfb_fix = {
33f7018c21STomi Valkeinen .id = "udlfb",
34f7018c21STomi Valkeinen .type = FB_TYPE_PACKED_PIXELS,
35f7018c21STomi Valkeinen .visual = FB_VISUAL_TRUECOLOR,
36f7018c21STomi Valkeinen .xpanstep = 0,
37f7018c21STomi Valkeinen .ypanstep = 0,
38f7018c21STomi Valkeinen .ywrapstep = 0,
39f7018c21STomi Valkeinen .accel = FB_ACCEL_NONE,
40f7018c21STomi Valkeinen };
41f7018c21STomi Valkeinen
42b3e148d7SThomas Zimmermann static const u32 udlfb_info_flags = FBINFO_READS_FAST |
43f7018c21STomi Valkeinen FBINFO_VIRTFB |
44f7018c21STomi Valkeinen FBINFO_HWACCEL_IMAGEBLIT | FBINFO_HWACCEL_FILLRECT |
45f7018c21STomi Valkeinen FBINFO_HWACCEL_COPYAREA | FBINFO_MISC_ALWAYS_SETPAR;
46f7018c21STomi Valkeinen
47f7018c21STomi Valkeinen /*
48f7018c21STomi Valkeinen * There are many DisplayLink-based graphics products, all with unique PIDs.
49f7018c21STomi Valkeinen * So we match on DisplayLink's VID + Vendor-Defined Interface Class (0xff)
50f7018c21STomi Valkeinen * We also require a match on SubClass (0x00) and Protocol (0x00),
51f7018c21STomi Valkeinen * which is compatible with all known USB 2.0 era graphics chips and firmware,
52f7018c21STomi Valkeinen * but allows DisplayLink to increment those for any future incompatible chips
53f7018c21STomi Valkeinen */
5469de8496SArvind Yadav static const struct usb_device_id id_table[] = {
55f7018c21STomi Valkeinen {.idVendor = 0x17e9,
56f7018c21STomi Valkeinen .bInterfaceClass = 0xff,
57f7018c21STomi Valkeinen .bInterfaceSubClass = 0x00,
58f7018c21STomi Valkeinen .bInterfaceProtocol = 0x00,
59f7018c21STomi Valkeinen .match_flags = USB_DEVICE_ID_MATCH_VENDOR |
60f7018c21STomi Valkeinen USB_DEVICE_ID_MATCH_INT_CLASS |
61f7018c21STomi Valkeinen USB_DEVICE_ID_MATCH_INT_SUBCLASS |
62f7018c21STomi Valkeinen USB_DEVICE_ID_MATCH_INT_PROTOCOL,
63f7018c21STomi Valkeinen },
64f7018c21STomi Valkeinen {},
65f7018c21STomi Valkeinen };
66f7018c21STomi Valkeinen MODULE_DEVICE_TABLE(usb, id_table);
67f7018c21STomi Valkeinen
68f7018c21STomi Valkeinen /* module options */
697022537bSJason Yan static bool console = true; /* Allow fbcon to open framebuffer */
707022537bSJason Yan static bool fb_defio = true; /* Detect mmap writes using page faults */
717022537bSJason Yan static bool shadow = true; /* Optionally disable shadow framebuffer */
72f7018c21STomi Valkeinen static int pixel_limit; /* Optionally force a pixel resolution limit */
73f7018c21STomi Valkeinen
747433914eSMikulas Patocka struct dlfb_deferred_free {
757433914eSMikulas Patocka struct list_head list;
767433914eSMikulas Patocka void *mem;
777433914eSMikulas Patocka };
787433914eSMikulas Patocka
797433914eSMikulas Patocka static int dlfb_realloc_framebuffer(struct dlfb_data *dlfb, struct fb_info *info, u32 new_len);
807433914eSMikulas Patocka
81f7018c21STomi Valkeinen /* dlfb keeps a list of urbs for efficient bulk transfers */
82f7018c21STomi Valkeinen static void dlfb_urb_completion(struct urb *urb);
837ea46206SLadislav Michl static struct urb *dlfb_get_urb(struct dlfb_data *dlfb);
847ea46206SLadislav Michl static int dlfb_submit_urb(struct dlfb_data *dlfb, struct urb * urb, size_t len);
857ea46206SLadislav Michl static int dlfb_alloc_urb_list(struct dlfb_data *dlfb, int count, size_t size);
867ea46206SLadislav Michl static void dlfb_free_urb_list(struct dlfb_data *dlfb);
87f7018c21STomi Valkeinen
88f7018c21STomi Valkeinen /*
89f7018c21STomi Valkeinen * All DisplayLink bulk operations start with 0xAF, followed by specific code
90f7018c21STomi Valkeinen * All operations are written to buffers which then later get sent to device
91f7018c21STomi Valkeinen */
dlfb_set_register(char * buf,u8 reg,u8 val)92f7018c21STomi Valkeinen static char *dlfb_set_register(char *buf, u8 reg, u8 val)
93f7018c21STomi Valkeinen {
94f7018c21STomi Valkeinen *buf++ = 0xAF;
95f7018c21STomi Valkeinen *buf++ = 0x20;
96f7018c21STomi Valkeinen *buf++ = reg;
97f7018c21STomi Valkeinen *buf++ = val;
98f7018c21STomi Valkeinen return buf;
99f7018c21STomi Valkeinen }
100f7018c21STomi Valkeinen
dlfb_vidreg_lock(char * buf)101f7018c21STomi Valkeinen static char *dlfb_vidreg_lock(char *buf)
102f7018c21STomi Valkeinen {
103f7018c21STomi Valkeinen return dlfb_set_register(buf, 0xFF, 0x00);
104f7018c21STomi Valkeinen }
105f7018c21STomi Valkeinen
dlfb_vidreg_unlock(char * buf)106f7018c21STomi Valkeinen static char *dlfb_vidreg_unlock(char *buf)
107f7018c21STomi Valkeinen {
108f7018c21STomi Valkeinen return dlfb_set_register(buf, 0xFF, 0xFF);
109f7018c21STomi Valkeinen }
110f7018c21STomi Valkeinen
111f7018c21STomi Valkeinen /*
112f7018c21STomi Valkeinen * Map FB_BLANK_* to DisplayLink register
113f7018c21STomi Valkeinen * DLReg FB_BLANK_*
114f7018c21STomi Valkeinen * ----- -----------------------------
115f7018c21STomi Valkeinen * 0x00 FB_BLANK_UNBLANK (0)
116f7018c21STomi Valkeinen * 0x01 FB_BLANK (1)
117f7018c21STomi Valkeinen * 0x03 FB_BLANK_VSYNC_SUSPEND (2)
118f7018c21STomi Valkeinen * 0x05 FB_BLANK_HSYNC_SUSPEND (3)
119f7018c21STomi Valkeinen * 0x07 FB_BLANK_POWERDOWN (4) Note: requires modeset to come back
120f7018c21STomi Valkeinen */
dlfb_blanking(char * buf,int fb_blank)121f7018c21STomi Valkeinen static char *dlfb_blanking(char *buf, int fb_blank)
122f7018c21STomi Valkeinen {
123f7018c21STomi Valkeinen u8 reg;
124f7018c21STomi Valkeinen
125f7018c21STomi Valkeinen switch (fb_blank) {
126f7018c21STomi Valkeinen case FB_BLANK_POWERDOWN:
127f7018c21STomi Valkeinen reg = 0x07;
128f7018c21STomi Valkeinen break;
129f7018c21STomi Valkeinen case FB_BLANK_HSYNC_SUSPEND:
130f7018c21STomi Valkeinen reg = 0x05;
131f7018c21STomi Valkeinen break;
132f7018c21STomi Valkeinen case FB_BLANK_VSYNC_SUSPEND:
133f7018c21STomi Valkeinen reg = 0x03;
134f7018c21STomi Valkeinen break;
135f7018c21STomi Valkeinen case FB_BLANK_NORMAL:
136f7018c21STomi Valkeinen reg = 0x01;
137f7018c21STomi Valkeinen break;
138f7018c21STomi Valkeinen default:
139f7018c21STomi Valkeinen reg = 0x00;
140f7018c21STomi Valkeinen }
141f7018c21STomi Valkeinen
142f7018c21STomi Valkeinen buf = dlfb_set_register(buf, 0x1F, reg);
143f7018c21STomi Valkeinen
144f7018c21STomi Valkeinen return buf;
145f7018c21STomi Valkeinen }
146f7018c21STomi Valkeinen
dlfb_set_color_depth(char * buf,u8 selection)147f7018c21STomi Valkeinen static char *dlfb_set_color_depth(char *buf, u8 selection)
148f7018c21STomi Valkeinen {
149f7018c21STomi Valkeinen return dlfb_set_register(buf, 0x00, selection);
150f7018c21STomi Valkeinen }
151f7018c21STomi Valkeinen
dlfb_set_base16bpp(char * wrptr,u32 base)152f7018c21STomi Valkeinen static char *dlfb_set_base16bpp(char *wrptr, u32 base)
153f7018c21STomi Valkeinen {
154f7018c21STomi Valkeinen /* the base pointer is 16 bits wide, 0x20 is hi byte. */
155f7018c21STomi Valkeinen wrptr = dlfb_set_register(wrptr, 0x20, base >> 16);
156f7018c21STomi Valkeinen wrptr = dlfb_set_register(wrptr, 0x21, base >> 8);
157f7018c21STomi Valkeinen return dlfb_set_register(wrptr, 0x22, base);
158f7018c21STomi Valkeinen }
159f7018c21STomi Valkeinen
160f7018c21STomi Valkeinen /*
161f7018c21STomi Valkeinen * DisplayLink HW has separate 16bpp and 8bpp framebuffers.
162f7018c21STomi Valkeinen * In 24bpp modes, the low 323 RGB bits go in the 8bpp framebuffer
163f7018c21STomi Valkeinen */
dlfb_set_base8bpp(char * wrptr,u32 base)164f7018c21STomi Valkeinen static char *dlfb_set_base8bpp(char *wrptr, u32 base)
165f7018c21STomi Valkeinen {
166f7018c21STomi Valkeinen wrptr = dlfb_set_register(wrptr, 0x26, base >> 16);
167f7018c21STomi Valkeinen wrptr = dlfb_set_register(wrptr, 0x27, base >> 8);
168f7018c21STomi Valkeinen return dlfb_set_register(wrptr, 0x28, base);
169f7018c21STomi Valkeinen }
170f7018c21STomi Valkeinen
dlfb_set_register_16(char * wrptr,u8 reg,u16 value)171f7018c21STomi Valkeinen static char *dlfb_set_register_16(char *wrptr, u8 reg, u16 value)
172f7018c21STomi Valkeinen {
173f7018c21STomi Valkeinen wrptr = dlfb_set_register(wrptr, reg, value >> 8);
174f7018c21STomi Valkeinen return dlfb_set_register(wrptr, reg+1, value);
175f7018c21STomi Valkeinen }
176f7018c21STomi Valkeinen
177f7018c21STomi Valkeinen /*
178f7018c21STomi Valkeinen * This is kind of weird because the controller takes some
179f7018c21STomi Valkeinen * register values in a different byte order than other registers.
180f7018c21STomi Valkeinen */
dlfb_set_register_16be(char * wrptr,u8 reg,u16 value)181f7018c21STomi Valkeinen static char *dlfb_set_register_16be(char *wrptr, u8 reg, u16 value)
182f7018c21STomi Valkeinen {
183f7018c21STomi Valkeinen wrptr = dlfb_set_register(wrptr, reg, value);
184f7018c21STomi Valkeinen return dlfb_set_register(wrptr, reg+1, value >> 8);
185f7018c21STomi Valkeinen }
186f7018c21STomi Valkeinen
187f7018c21STomi Valkeinen /*
188f7018c21STomi Valkeinen * LFSR is linear feedback shift register. The reason we have this is
189f7018c21STomi Valkeinen * because the display controller needs to minimize the clock depth of
190f7018c21STomi Valkeinen * various counters used in the display path. So this code reverses the
191f7018c21STomi Valkeinen * provided value into the lfsr16 value by counting backwards to get
192f7018c21STomi Valkeinen * the value that needs to be set in the hardware comparator to get the
193f7018c21STomi Valkeinen * same actual count. This makes sense once you read above a couple of
194f7018c21STomi Valkeinen * times and think about it from a hardware perspective.
195f7018c21STomi Valkeinen */
dlfb_lfsr16(u16 actual_count)196f7018c21STomi Valkeinen static u16 dlfb_lfsr16(u16 actual_count)
197f7018c21STomi Valkeinen {
198f7018c21STomi Valkeinen u32 lv = 0xFFFF; /* This is the lfsr value that the hw starts with */
199f7018c21STomi Valkeinen
200f7018c21STomi Valkeinen while (actual_count--) {
201f7018c21STomi Valkeinen lv = ((lv << 1) |
202f7018c21STomi Valkeinen (((lv >> 15) ^ (lv >> 4) ^ (lv >> 2) ^ (lv >> 1)) & 1))
203f7018c21STomi Valkeinen & 0xFFFF;
204f7018c21STomi Valkeinen }
205f7018c21STomi Valkeinen
206f7018c21STomi Valkeinen return (u16) lv;
207f7018c21STomi Valkeinen }
208f7018c21STomi Valkeinen
209f7018c21STomi Valkeinen /*
210f7018c21STomi Valkeinen * This does LFSR conversion on the value that is to be written.
211f7018c21STomi Valkeinen * See LFSR explanation above for more detail.
212f7018c21STomi Valkeinen */
dlfb_set_register_lfsr16(char * wrptr,u8 reg,u16 value)213f7018c21STomi Valkeinen static char *dlfb_set_register_lfsr16(char *wrptr, u8 reg, u16 value)
214f7018c21STomi Valkeinen {
215f7018c21STomi Valkeinen return dlfb_set_register_16(wrptr, reg, dlfb_lfsr16(value));
216f7018c21STomi Valkeinen }
217f7018c21STomi Valkeinen
218f7018c21STomi Valkeinen /*
219f7018c21STomi Valkeinen * This takes a standard fbdev screeninfo struct and all of its monitor mode
220f7018c21STomi Valkeinen * details and converts them into the DisplayLink equivalent register commands.
221f7018c21STomi Valkeinen */
dlfb_set_vid_cmds(char * wrptr,struct fb_var_screeninfo * var)222f7018c21STomi Valkeinen static char *dlfb_set_vid_cmds(char *wrptr, struct fb_var_screeninfo *var)
223f7018c21STomi Valkeinen {
224f7018c21STomi Valkeinen u16 xds, yds;
225f7018c21STomi Valkeinen u16 xde, yde;
226f7018c21STomi Valkeinen u16 yec;
227f7018c21STomi Valkeinen
228f7018c21STomi Valkeinen /* x display start */
229f7018c21STomi Valkeinen xds = var->left_margin + var->hsync_len;
230f7018c21STomi Valkeinen wrptr = dlfb_set_register_lfsr16(wrptr, 0x01, xds);
231f7018c21STomi Valkeinen /* x display end */
232f7018c21STomi Valkeinen xde = xds + var->xres;
233f7018c21STomi Valkeinen wrptr = dlfb_set_register_lfsr16(wrptr, 0x03, xde);
234f7018c21STomi Valkeinen
235f7018c21STomi Valkeinen /* y display start */
236f7018c21STomi Valkeinen yds = var->upper_margin + var->vsync_len;
237f7018c21STomi Valkeinen wrptr = dlfb_set_register_lfsr16(wrptr, 0x05, yds);
238f7018c21STomi Valkeinen /* y display end */
239f7018c21STomi Valkeinen yde = yds + var->yres;
240f7018c21STomi Valkeinen wrptr = dlfb_set_register_lfsr16(wrptr, 0x07, yde);
241f7018c21STomi Valkeinen
242f7018c21STomi Valkeinen /* x end count is active + blanking - 1 */
243f7018c21STomi Valkeinen wrptr = dlfb_set_register_lfsr16(wrptr, 0x09,
244f7018c21STomi Valkeinen xde + var->right_margin - 1);
245f7018c21STomi Valkeinen
246f7018c21STomi Valkeinen /* libdlo hardcodes hsync start to 1 */
247f7018c21STomi Valkeinen wrptr = dlfb_set_register_lfsr16(wrptr, 0x0B, 1);
248f7018c21STomi Valkeinen
249f7018c21STomi Valkeinen /* hsync end is width of sync pulse + 1 */
250f7018c21STomi Valkeinen wrptr = dlfb_set_register_lfsr16(wrptr, 0x0D, var->hsync_len + 1);
251f7018c21STomi Valkeinen
252f7018c21STomi Valkeinen /* hpixels is active pixels */
253f7018c21STomi Valkeinen wrptr = dlfb_set_register_16(wrptr, 0x0F, var->xres);
254f7018c21STomi Valkeinen
255f7018c21STomi Valkeinen /* yendcount is vertical active + vertical blanking */
256f7018c21STomi Valkeinen yec = var->yres + var->upper_margin + var->lower_margin +
257f7018c21STomi Valkeinen var->vsync_len;
258f7018c21STomi Valkeinen wrptr = dlfb_set_register_lfsr16(wrptr, 0x11, yec);
259f7018c21STomi Valkeinen
260f7018c21STomi Valkeinen /* libdlo hardcodes vsync start to 0 */
261f7018c21STomi Valkeinen wrptr = dlfb_set_register_lfsr16(wrptr, 0x13, 0);
262f7018c21STomi Valkeinen
263f7018c21STomi Valkeinen /* vsync end is width of vsync pulse */
264f7018c21STomi Valkeinen wrptr = dlfb_set_register_lfsr16(wrptr, 0x15, var->vsync_len);
265f7018c21STomi Valkeinen
266f7018c21STomi Valkeinen /* vpixels is active pixels */
267f7018c21STomi Valkeinen wrptr = dlfb_set_register_16(wrptr, 0x17, var->yres);
268f7018c21STomi Valkeinen
269f7018c21STomi Valkeinen /* convert picoseconds to 5kHz multiple for pclk5k = x * 1E12/5k */
270f7018c21STomi Valkeinen wrptr = dlfb_set_register_16be(wrptr, 0x1B,
271f7018c21STomi Valkeinen 200*1000*1000/var->pixclock);
272f7018c21STomi Valkeinen
273f7018c21STomi Valkeinen return wrptr;
274f7018c21STomi Valkeinen }
275f7018c21STomi Valkeinen
276f7018c21STomi Valkeinen /*
277f7018c21STomi Valkeinen * This takes a standard fbdev screeninfo struct that was fetched or prepared
278f7018c21STomi Valkeinen * and then generates the appropriate command sequence that then drives the
279f7018c21STomi Valkeinen * display controller.
280f7018c21STomi Valkeinen */
dlfb_set_video_mode(struct dlfb_data * dlfb,struct fb_var_screeninfo * var)2817ea46206SLadislav Michl static int dlfb_set_video_mode(struct dlfb_data *dlfb,
282f7018c21STomi Valkeinen struct fb_var_screeninfo *var)
283f7018c21STomi Valkeinen {
284f7018c21STomi Valkeinen char *buf;
285f7018c21STomi Valkeinen char *wrptr;
286f63cb8d7SAlexey Klimov int retval;
287f7018c21STomi Valkeinen int writesize;
288f7018c21STomi Valkeinen struct urb *urb;
289f7018c21STomi Valkeinen
2907ea46206SLadislav Michl if (!atomic_read(&dlfb->usb_active))
291f7018c21STomi Valkeinen return -EPERM;
292f7018c21STomi Valkeinen
2937ea46206SLadislav Michl urb = dlfb_get_urb(dlfb);
294f7018c21STomi Valkeinen if (!urb)
295f7018c21STomi Valkeinen return -ENOMEM;
296f7018c21STomi Valkeinen
297f7018c21STomi Valkeinen buf = (char *) urb->transfer_buffer;
298f7018c21STomi Valkeinen
299f7018c21STomi Valkeinen /*
300f7018c21STomi Valkeinen * This first section has to do with setting the base address on the
301f7018c21STomi Valkeinen * controller * associated with the display. There are 2 base
302f7018c21STomi Valkeinen * pointers, currently, we only * use the 16 bpp segment.
303f7018c21STomi Valkeinen */
304f7018c21STomi Valkeinen wrptr = dlfb_vidreg_lock(buf);
305f7018c21STomi Valkeinen wrptr = dlfb_set_color_depth(wrptr, 0x00);
306f7018c21STomi Valkeinen /* set base for 16bpp segment to 0 */
307f7018c21STomi Valkeinen wrptr = dlfb_set_base16bpp(wrptr, 0);
308f7018c21STomi Valkeinen /* set base for 8bpp segment to end of fb */
3097ea46206SLadislav Michl wrptr = dlfb_set_base8bpp(wrptr, dlfb->info->fix.smem_len);
310f7018c21STomi Valkeinen
311f7018c21STomi Valkeinen wrptr = dlfb_set_vid_cmds(wrptr, var);
312f7018c21STomi Valkeinen wrptr = dlfb_blanking(wrptr, FB_BLANK_UNBLANK);
313f7018c21STomi Valkeinen wrptr = dlfb_vidreg_unlock(wrptr);
314f7018c21STomi Valkeinen
315f7018c21STomi Valkeinen writesize = wrptr - buf;
316f7018c21STomi Valkeinen
3177ea46206SLadislav Michl retval = dlfb_submit_urb(dlfb, urb, writesize);
318f7018c21STomi Valkeinen
3197ea46206SLadislav Michl dlfb->blank_mode = FB_BLANK_UNBLANK;
320f7018c21STomi Valkeinen
321f7018c21STomi Valkeinen return retval;
322f7018c21STomi Valkeinen }
323f7018c21STomi Valkeinen
dlfb_ops_mmap(struct fb_info * info,struct vm_area_struct * vma)324f7018c21STomi Valkeinen static int dlfb_ops_mmap(struct fb_info *info, struct vm_area_struct *vma)
325f7018c21STomi Valkeinen {
326f7018c21STomi Valkeinen unsigned long start = vma->vm_start;
327f7018c21STomi Valkeinen unsigned long size = vma->vm_end - vma->vm_start;
328f7018c21STomi Valkeinen unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
329f7018c21STomi Valkeinen unsigned long page, pos;
330f7018c21STomi Valkeinen
33159055851SThomas Zimmermann if (info->fbdefio)
33259055851SThomas Zimmermann return fb_deferred_io_mmap(info, vma);
33359055851SThomas Zimmermann
33476f92201SThomas Zimmermann vma->vm_page_prot = pgprot_decrypted(vma->vm_page_prot);
33576f92201SThomas Zimmermann
336f7018c21STomi Valkeinen if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT))
337f7018c21STomi Valkeinen return -EINVAL;
338f7018c21STomi Valkeinen if (size > info->fix.smem_len)
339f7018c21STomi Valkeinen return -EINVAL;
340f7018c21STomi Valkeinen if (offset > info->fix.smem_len - size)
341f7018c21STomi Valkeinen return -EINVAL;
342f7018c21STomi Valkeinen
343f7018c21STomi Valkeinen pos = (unsigned long)info->fix.smem_start + offset;
344f7018c21STomi Valkeinen
3455865889fSLadislav Michl dev_dbg(info->dev, "mmap() framebuffer addr:%lu size:%lu\n",
346f7018c21STomi Valkeinen pos, size);
347f7018c21STomi Valkeinen
348f7018c21STomi Valkeinen while (size > 0) {
349f7018c21STomi Valkeinen page = vmalloc_to_pfn((void *)pos);
350f7018c21STomi Valkeinen if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED))
351f7018c21STomi Valkeinen return -EAGAIN;
352f7018c21STomi Valkeinen
353f7018c21STomi Valkeinen start += PAGE_SIZE;
354f7018c21STomi Valkeinen pos += PAGE_SIZE;
355f7018c21STomi Valkeinen if (size > PAGE_SIZE)
356f7018c21STomi Valkeinen size -= PAGE_SIZE;
357f7018c21STomi Valkeinen else
358f7018c21STomi Valkeinen size = 0;
359f7018c21STomi Valkeinen }
360f7018c21STomi Valkeinen
361f7018c21STomi Valkeinen return 0;
362f7018c21STomi Valkeinen }
363f7018c21STomi Valkeinen
364f7018c21STomi Valkeinen /*
365f7018c21STomi Valkeinen * Trims identical data from front and back of line
366f7018c21STomi Valkeinen * Sets new front buffer address and width
367f7018c21STomi Valkeinen * And returns byte count of identical pixels
368f7018c21STomi Valkeinen * Assumes CPU natural alignment (unsigned long)
369f7018c21STomi Valkeinen * for back and front buffer ptrs and width
370f7018c21STomi Valkeinen */
dlfb_trim_hline(const u8 * bback,const u8 ** bfront,int * width_bytes)371f7018c21STomi Valkeinen static int dlfb_trim_hline(const u8 *bback, const u8 **bfront, int *width_bytes)
372f7018c21STomi Valkeinen {
373f7018c21STomi Valkeinen int j, k;
374f7018c21STomi Valkeinen const unsigned long *back = (const unsigned long *) bback;
375f7018c21STomi Valkeinen const unsigned long *front = (const unsigned long *) *bfront;
376f7018c21STomi Valkeinen const int width = *width_bytes / sizeof(unsigned long);
37759945216SColin Ian King int identical;
378f7018c21STomi Valkeinen int start = width;
379f7018c21STomi Valkeinen int end = width;
380f7018c21STomi Valkeinen
381f7018c21STomi Valkeinen for (j = 0; j < width; j++) {
382f7018c21STomi Valkeinen if (back[j] != front[j]) {
383f7018c21STomi Valkeinen start = j;
384f7018c21STomi Valkeinen break;
385f7018c21STomi Valkeinen }
386f7018c21STomi Valkeinen }
387f7018c21STomi Valkeinen
388f7018c21STomi Valkeinen for (k = width - 1; k > j; k--) {
389f7018c21STomi Valkeinen if (back[k] != front[k]) {
390f7018c21STomi Valkeinen end = k+1;
391f7018c21STomi Valkeinen break;
392f7018c21STomi Valkeinen }
393f7018c21STomi Valkeinen }
394f7018c21STomi Valkeinen
395f7018c21STomi Valkeinen identical = start + (width - end);
396f7018c21STomi Valkeinen *bfront = (u8 *) &front[start];
397f7018c21STomi Valkeinen *width_bytes = (end - start) * sizeof(unsigned long);
398f7018c21STomi Valkeinen
399f7018c21STomi Valkeinen return identical * sizeof(unsigned long);
400f7018c21STomi Valkeinen }
401f7018c21STomi Valkeinen
402f7018c21STomi Valkeinen /*
403f7018c21STomi Valkeinen * Render a command stream for an encoded horizontal line segment of pixels.
404f7018c21STomi Valkeinen *
405f7018c21STomi Valkeinen * A command buffer holds several commands.
406f7018c21STomi Valkeinen * It always begins with a fresh command header
407f7018c21STomi Valkeinen * (the protocol doesn't require this, but we enforce it to allow
408f7018c21STomi Valkeinen * multiple buffers to be potentially encoded and sent in parallel).
409f7018c21STomi Valkeinen * A single command encodes one contiguous horizontal line of pixels
410f7018c21STomi Valkeinen *
411f7018c21STomi Valkeinen * The function relies on the client to do all allocation, so that
412f7018c21STomi Valkeinen * rendering can be done directly to output buffers (e.g. USB URBs).
413f7018c21STomi Valkeinen * The function fills the supplied command buffer, providing information
414f7018c21STomi Valkeinen * on where it left off, so the client may call in again with additional
415f7018c21STomi Valkeinen * buffers if the line will take several buffers to complete.
416f7018c21STomi Valkeinen *
417f7018c21STomi Valkeinen * A single command can transmit a maximum of 256 pixels,
418f7018c21STomi Valkeinen * regardless of the compression ratio (protocol design limit).
419f7018c21STomi Valkeinen * To the hardware, 0 for a size byte means 256
420f7018c21STomi Valkeinen *
421f7018c21STomi Valkeinen * Rather than 256 pixel commands which are either rl or raw encoded,
422f7018c21STomi Valkeinen * the rlx command simply assumes alternating raw and rl spans within one cmd.
423f7018c21STomi Valkeinen * This has a slightly larger header overhead, but produces more even results.
424f7018c21STomi Valkeinen * It also processes all data (read and write) in a single pass.
425f7018c21STomi Valkeinen * Performance benchmarks of common cases show it having just slightly better
426f7018c21STomi Valkeinen * compression than 256 pixel raw or rle commands, with similar CPU consumpion.
427f7018c21STomi Valkeinen * But for very rl friendly data, will compress not quite as well.
428f7018c21STomi Valkeinen */
dlfb_compress_hline(const uint16_t ** pixel_start_ptr,const uint16_t * const pixel_end,uint32_t * device_address_ptr,uint8_t ** command_buffer_ptr,const uint8_t * const cmd_buffer_end,unsigned long back_buffer_offset,int * ident_ptr)429f7018c21STomi Valkeinen static void dlfb_compress_hline(
430f7018c21STomi Valkeinen const uint16_t **pixel_start_ptr,
431f7018c21STomi Valkeinen const uint16_t *const pixel_end,
432f7018c21STomi Valkeinen uint32_t *device_address_ptr,
433f7018c21STomi Valkeinen uint8_t **command_buffer_ptr,
4348f3c39b8SMikulas Patocka const uint8_t *const cmd_buffer_end,
4358f3c39b8SMikulas Patocka unsigned long back_buffer_offset,
4368f3c39b8SMikulas Patocka int *ident_ptr)
437f7018c21STomi Valkeinen {
438f7018c21STomi Valkeinen const uint16_t *pixel = *pixel_start_ptr;
439f7018c21STomi Valkeinen uint32_t dev_addr = *device_address_ptr;
440f7018c21STomi Valkeinen uint8_t *cmd = *command_buffer_ptr;
441f7018c21STomi Valkeinen
442f7018c21STomi Valkeinen while ((pixel_end > pixel) &&
443f7018c21STomi Valkeinen (cmd_buffer_end - MIN_RLX_CMD_BYTES > cmd)) {
444f7018c21STomi Valkeinen uint8_t *raw_pixels_count_byte = NULL;
445f7018c21STomi Valkeinen uint8_t *cmd_pixels_count_byte = NULL;
446f7018c21STomi Valkeinen const uint16_t *raw_pixel_start = NULL;
447f7018c21STomi Valkeinen const uint16_t *cmd_pixel_start, *cmd_pixel_end = NULL;
448f7018c21STomi Valkeinen
4498f3c39b8SMikulas Patocka if (back_buffer_offset &&
4508f3c39b8SMikulas Patocka *pixel == *(u16 *)((u8 *)pixel + back_buffer_offset)) {
4518f3c39b8SMikulas Patocka pixel++;
4528f3c39b8SMikulas Patocka dev_addr += BPP;
4538f3c39b8SMikulas Patocka (*ident_ptr)++;
4548f3c39b8SMikulas Patocka continue;
4558f3c39b8SMikulas Patocka }
4568f3c39b8SMikulas Patocka
457f7018c21STomi Valkeinen *cmd++ = 0xAF;
458f7018c21STomi Valkeinen *cmd++ = 0x6B;
459115e7759SLadislav Michl *cmd++ = dev_addr >> 16;
460115e7759SLadislav Michl *cmd++ = dev_addr >> 8;
461115e7759SLadislav Michl *cmd++ = dev_addr;
462f7018c21STomi Valkeinen
463f7018c21STomi Valkeinen cmd_pixels_count_byte = cmd++; /* we'll know this later */
464f7018c21STomi Valkeinen cmd_pixel_start = pixel;
465f7018c21STomi Valkeinen
466f7018c21STomi Valkeinen raw_pixels_count_byte = cmd++; /* we'll know this later */
467f7018c21STomi Valkeinen raw_pixel_start = pixel;
468f7018c21STomi Valkeinen
4694e705e17SMikulas Patocka cmd_pixel_end = pixel + min3(MAX_CMD_PIXELS + 1UL,
4704e705e17SMikulas Patocka (unsigned long)(pixel_end - pixel),
4714e705e17SMikulas Patocka (unsigned long)(cmd_buffer_end - 1 - cmd) / BPP);
472f7018c21STomi Valkeinen
4738f3c39b8SMikulas Patocka if (back_buffer_offset) {
4748f3c39b8SMikulas Patocka /* note: the framebuffer may change under us, so we must test for underflow */
4758f3c39b8SMikulas Patocka while (cmd_pixel_end - 1 > pixel &&
4768f3c39b8SMikulas Patocka *(cmd_pixel_end - 1) == *(u16 *)((u8 *)(cmd_pixel_end - 1) + back_buffer_offset))
4778f3c39b8SMikulas Patocka cmd_pixel_end--;
4788f3c39b8SMikulas Patocka }
4798f3c39b8SMikulas Patocka
480f7018c21STomi Valkeinen while (pixel < cmd_pixel_end) {
481f7018c21STomi Valkeinen const uint16_t * const repeating_pixel = pixel;
4828f3c39b8SMikulas Patocka u16 pixel_value = *pixel;
483f7018c21STomi Valkeinen
4848f3c39b8SMikulas Patocka put_unaligned_be16(pixel_value, cmd);
4858f3c39b8SMikulas Patocka if (back_buffer_offset)
4868f3c39b8SMikulas Patocka *(u16 *)((u8 *)pixel + back_buffer_offset) = pixel_value;
4874e705e17SMikulas Patocka cmd += 2;
488f7018c21STomi Valkeinen pixel++;
489f7018c21STomi Valkeinen
490f7018c21STomi Valkeinen if (unlikely((pixel < cmd_pixel_end) &&
4918f3c39b8SMikulas Patocka (*pixel == pixel_value))) {
492f7018c21STomi Valkeinen /* go back and fill in raw pixel count */
493f7018c21STomi Valkeinen *raw_pixels_count_byte = ((repeating_pixel -
494f7018c21STomi Valkeinen raw_pixel_start) + 1) & 0xFF;
495f7018c21STomi Valkeinen
4968f3c39b8SMikulas Patocka do {
4978f3c39b8SMikulas Patocka if (back_buffer_offset)
4988f3c39b8SMikulas Patocka *(u16 *)((u8 *)pixel + back_buffer_offset) = pixel_value;
499f7018c21STomi Valkeinen pixel++;
5008f3c39b8SMikulas Patocka } while ((pixel < cmd_pixel_end) &&
5018f3c39b8SMikulas Patocka (*pixel == pixel_value));
502f7018c21STomi Valkeinen
503f7018c21STomi Valkeinen /* immediately after raw data is repeat byte */
504f7018c21STomi Valkeinen *cmd++ = ((pixel - repeating_pixel) - 1) & 0xFF;
505f7018c21STomi Valkeinen
506f7018c21STomi Valkeinen /* Then start another raw pixel span */
507f7018c21STomi Valkeinen raw_pixel_start = pixel;
508f7018c21STomi Valkeinen raw_pixels_count_byte = cmd++;
509f7018c21STomi Valkeinen }
510f7018c21STomi Valkeinen }
511f7018c21STomi Valkeinen
512f7018c21STomi Valkeinen if (pixel > raw_pixel_start) {
513f7018c21STomi Valkeinen /* finalize last RAW span */
514f7018c21STomi Valkeinen *raw_pixels_count_byte = (pixel-raw_pixel_start) & 0xFF;
5154e705e17SMikulas Patocka } else {
5164e705e17SMikulas Patocka /* undo unused byte */
5174e705e17SMikulas Patocka cmd--;
518f7018c21STomi Valkeinen }
519f7018c21STomi Valkeinen
520f7018c21STomi Valkeinen *cmd_pixels_count_byte = (pixel - cmd_pixel_start) & 0xFF;
5214e705e17SMikulas Patocka dev_addr += (u8 *)pixel - (u8 *)cmd_pixel_start;
522f7018c21STomi Valkeinen }
523f7018c21STomi Valkeinen
5244e705e17SMikulas Patocka if (cmd_buffer_end - MIN_RLX_CMD_BYTES <= cmd) {
525f7018c21STomi Valkeinen /* Fill leftover bytes with no-ops */
526f7018c21STomi Valkeinen if (cmd_buffer_end > cmd)
527f7018c21STomi Valkeinen memset(cmd, 0xAF, cmd_buffer_end - cmd);
528f7018c21STomi Valkeinen cmd = (uint8_t *) cmd_buffer_end;
529f7018c21STomi Valkeinen }
530f7018c21STomi Valkeinen
531f7018c21STomi Valkeinen *command_buffer_ptr = cmd;
532f7018c21STomi Valkeinen *pixel_start_ptr = pixel;
533f7018c21STomi Valkeinen *device_address_ptr = dev_addr;
534f7018c21STomi Valkeinen }
535f7018c21STomi Valkeinen
536f7018c21STomi Valkeinen /*
537f7018c21STomi Valkeinen * There are 3 copies of every pixel: The front buffer that the fbdev
538f7018c21STomi Valkeinen * client renders to, the actual framebuffer across the USB bus in hardware
539f7018c21STomi Valkeinen * (that we can only write to, slowly, and can never read), and (optionally)
540f7018c21STomi Valkeinen * our shadow copy that tracks what's been sent to that hardware buffer.
541f7018c21STomi Valkeinen */
dlfb_render_hline(struct dlfb_data * dlfb,struct urb ** urb_ptr,const char * front,char ** urb_buf_ptr,u32 byte_offset,u32 byte_width,int * ident_ptr,int * sent_ptr)5427ea46206SLadislav Michl static int dlfb_render_hline(struct dlfb_data *dlfb, struct urb **urb_ptr,
543f7018c21STomi Valkeinen const char *front, char **urb_buf_ptr,
544f7018c21STomi Valkeinen u32 byte_offset, u32 byte_width,
545f7018c21STomi Valkeinen int *ident_ptr, int *sent_ptr)
546f7018c21STomi Valkeinen {
547f7018c21STomi Valkeinen const u8 *line_start, *line_end, *next_pixel;
5487ea46206SLadislav Michl u32 dev_addr = dlfb->base16 + byte_offset;
549f7018c21STomi Valkeinen struct urb *urb = *urb_ptr;
550f7018c21STomi Valkeinen u8 *cmd = *urb_buf_ptr;
551f7018c21STomi Valkeinen u8 *cmd_end = (u8 *) urb->transfer_buffer + urb->transfer_buffer_length;
5528f3c39b8SMikulas Patocka unsigned long back_buffer_offset = 0;
553f7018c21STomi Valkeinen
554f7018c21STomi Valkeinen line_start = (u8 *) (front + byte_offset);
555f7018c21STomi Valkeinen next_pixel = line_start;
556f7018c21STomi Valkeinen line_end = next_pixel + byte_width;
557f7018c21STomi Valkeinen
5587ea46206SLadislav Michl if (dlfb->backing_buffer) {
559f7018c21STomi Valkeinen int offset;
5607ea46206SLadislav Michl const u8 *back_start = (u8 *) (dlfb->backing_buffer
561f7018c21STomi Valkeinen + byte_offset);
562f7018c21STomi Valkeinen
5638f3c39b8SMikulas Patocka back_buffer_offset = (unsigned long)back_start - (unsigned long)line_start;
5648f3c39b8SMikulas Patocka
565f7018c21STomi Valkeinen *ident_ptr += dlfb_trim_hline(back_start, &next_pixel,
566f7018c21STomi Valkeinen &byte_width);
567f7018c21STomi Valkeinen
568f7018c21STomi Valkeinen offset = next_pixel - line_start;
569f7018c21STomi Valkeinen line_end = next_pixel + byte_width;
570f7018c21STomi Valkeinen dev_addr += offset;
571f7018c21STomi Valkeinen back_start += offset;
572f7018c21STomi Valkeinen line_start += offset;
573f7018c21STomi Valkeinen }
574f7018c21STomi Valkeinen
575f7018c21STomi Valkeinen while (next_pixel < line_end) {
576f7018c21STomi Valkeinen
577f7018c21STomi Valkeinen dlfb_compress_hline((const uint16_t **) &next_pixel,
578f7018c21STomi Valkeinen (const uint16_t *) line_end, &dev_addr,
5798f3c39b8SMikulas Patocka (u8 **) &cmd, (u8 *) cmd_end, back_buffer_offset,
5808f3c39b8SMikulas Patocka ident_ptr);
581f7018c21STomi Valkeinen
582f7018c21STomi Valkeinen if (cmd >= cmd_end) {
583f7018c21STomi Valkeinen int len = cmd - (u8 *) urb->transfer_buffer;
5847ea46206SLadislav Michl if (dlfb_submit_urb(dlfb, urb, len))
585f7018c21STomi Valkeinen return 1; /* lost pixels is set */
586f7018c21STomi Valkeinen *sent_ptr += len;
5877ea46206SLadislav Michl urb = dlfb_get_urb(dlfb);
588f7018c21STomi Valkeinen if (!urb)
589f7018c21STomi Valkeinen return 1; /* lost_pixels is set */
590f7018c21STomi Valkeinen *urb_ptr = urb;
591f7018c21STomi Valkeinen cmd = urb->transfer_buffer;
592f7018c21STomi Valkeinen cmd_end = &cmd[urb->transfer_buffer_length];
593f7018c21STomi Valkeinen }
594f7018c21STomi Valkeinen }
595f7018c21STomi Valkeinen
596f7018c21STomi Valkeinen *urb_buf_ptr = cmd;
597f7018c21STomi Valkeinen
598f7018c21STomi Valkeinen return 0;
599f7018c21STomi Valkeinen }
600f7018c21STomi Valkeinen
dlfb_handle_damage(struct dlfb_data * dlfb,int x,int y,int width,int height)601bd86b6c5SMikulas Patocka static int dlfb_handle_damage(struct dlfb_data *dlfb, int x, int y, int width, int height)
602f7018c21STomi Valkeinen {
603babc250eSMikulas Patocka int i, ret;
604f7018c21STomi Valkeinen char *cmd;
605f7018c21STomi Valkeinen cycles_t start_cycles, end_cycles;
606f7018c21STomi Valkeinen int bytes_sent = 0;
607f7018c21STomi Valkeinen int bytes_identical = 0;
608f7018c21STomi Valkeinen struct urb *urb;
609f7018c21STomi Valkeinen int aligned_x;
610f7018c21STomi Valkeinen
611f7018c21STomi Valkeinen start_cycles = get_cycles();
612f7018c21STomi Valkeinen
613babc250eSMikulas Patocka mutex_lock(&dlfb->render_mutex);
614babc250eSMikulas Patocka
615f7018c21STomi Valkeinen aligned_x = DL_ALIGN_DOWN(x, sizeof(unsigned long));
616f7018c21STomi Valkeinen width = DL_ALIGN_UP(width + (x-aligned_x), sizeof(unsigned long));
617f7018c21STomi Valkeinen x = aligned_x;
618f7018c21STomi Valkeinen
619f7018c21STomi Valkeinen if ((width <= 0) ||
6207ea46206SLadislav Michl (x + width > dlfb->info->var.xres) ||
621babc250eSMikulas Patocka (y + height > dlfb->info->var.yres)) {
622babc250eSMikulas Patocka ret = -EINVAL;
623babc250eSMikulas Patocka goto unlock_ret;
624babc250eSMikulas Patocka }
625f7018c21STomi Valkeinen
626babc250eSMikulas Patocka if (!atomic_read(&dlfb->usb_active)) {
627babc250eSMikulas Patocka ret = 0;
628babc250eSMikulas Patocka goto unlock_ret;
629babc250eSMikulas Patocka }
630f7018c21STomi Valkeinen
6317ea46206SLadislav Michl urb = dlfb_get_urb(dlfb);
632babc250eSMikulas Patocka if (!urb) {
633babc250eSMikulas Patocka ret = 0;
634babc250eSMikulas Patocka goto unlock_ret;
635babc250eSMikulas Patocka }
636f7018c21STomi Valkeinen cmd = urb->transfer_buffer;
637f7018c21STomi Valkeinen
638f7018c21STomi Valkeinen for (i = y; i < y + height ; i++) {
6397ea46206SLadislav Michl const int line_offset = dlfb->info->fix.line_length * i;
640f7018c21STomi Valkeinen const int byte_offset = line_offset + (x * BPP);
641f7018c21STomi Valkeinen
6427ea46206SLadislav Michl if (dlfb_render_hline(dlfb, &urb,
6437ea46206SLadislav Michl (char *) dlfb->info->fix.smem_start,
644f7018c21STomi Valkeinen &cmd, byte_offset, width * BPP,
645f7018c21STomi Valkeinen &bytes_identical, &bytes_sent))
646f7018c21STomi Valkeinen goto error;
647f7018c21STomi Valkeinen }
648f7018c21STomi Valkeinen
649f7018c21STomi Valkeinen if (cmd > (char *) urb->transfer_buffer) {
6504e705e17SMikulas Patocka int len;
6514e705e17SMikulas Patocka if (cmd < (char *) urb->transfer_buffer + urb->transfer_buffer_length)
6524e705e17SMikulas Patocka *cmd++ = 0xAF;
653f7018c21STomi Valkeinen /* Send partial buffer remaining before exiting */
6544e705e17SMikulas Patocka len = cmd - (char *) urb->transfer_buffer;
655bd86b6c5SMikulas Patocka dlfb_submit_urb(dlfb, urb, len);
656f7018c21STomi Valkeinen bytes_sent += len;
657f7018c21STomi Valkeinen } else
658f7018c21STomi Valkeinen dlfb_urb_completion(urb);
659f7018c21STomi Valkeinen
660f7018c21STomi Valkeinen error:
6617ea46206SLadislav Michl atomic_add(bytes_sent, &dlfb->bytes_sent);
6627ea46206SLadislav Michl atomic_add(bytes_identical, &dlfb->bytes_identical);
6637ea46206SLadislav Michl atomic_add(width*height*2, &dlfb->bytes_rendered);
664f7018c21STomi Valkeinen end_cycles = get_cycles();
665f7018c21STomi Valkeinen atomic_add(((unsigned int) ((end_cycles - start_cycles)
666f7018c21STomi Valkeinen >> 10)), /* Kcycles */
6677ea46206SLadislav Michl &dlfb->cpu_kcycles_used);
668f7018c21STomi Valkeinen
669babc250eSMikulas Patocka ret = 0;
670babc250eSMikulas Patocka
671babc250eSMikulas Patocka unlock_ret:
672babc250eSMikulas Patocka mutex_unlock(&dlfb->render_mutex);
673babc250eSMikulas Patocka return ret;
674f7018c21STomi Valkeinen }
675f7018c21STomi Valkeinen
dlfb_init_damage(struct dlfb_data * dlfb)6766b11f9d8SMikulas Patocka static void dlfb_init_damage(struct dlfb_data *dlfb)
6776b11f9d8SMikulas Patocka {
6786b11f9d8SMikulas Patocka dlfb->damage_x = INT_MAX;
6796b11f9d8SMikulas Patocka dlfb->damage_x2 = 0;
6806b11f9d8SMikulas Patocka dlfb->damage_y = INT_MAX;
6816b11f9d8SMikulas Patocka dlfb->damage_y2 = 0;
6826b11f9d8SMikulas Patocka }
6836b11f9d8SMikulas Patocka
dlfb_damage_work(struct work_struct * w)6846b11f9d8SMikulas Patocka static void dlfb_damage_work(struct work_struct *w)
6856b11f9d8SMikulas Patocka {
6866b11f9d8SMikulas Patocka struct dlfb_data *dlfb = container_of(w, struct dlfb_data, damage_work);
6876b11f9d8SMikulas Patocka int x, x2, y, y2;
6886b11f9d8SMikulas Patocka
6896b11f9d8SMikulas Patocka spin_lock_irq(&dlfb->damage_lock);
6906b11f9d8SMikulas Patocka x = dlfb->damage_x;
6916b11f9d8SMikulas Patocka x2 = dlfb->damage_x2;
6926b11f9d8SMikulas Patocka y = dlfb->damage_y;
6936b11f9d8SMikulas Patocka y2 = dlfb->damage_y2;
6946b11f9d8SMikulas Patocka dlfb_init_damage(dlfb);
6956b11f9d8SMikulas Patocka spin_unlock_irq(&dlfb->damage_lock);
6966b11f9d8SMikulas Patocka
6976b11f9d8SMikulas Patocka if (x < x2 && y < y2)
6986b11f9d8SMikulas Patocka dlfb_handle_damage(dlfb, x, y, x2 - x, y2 - y);
6996b11f9d8SMikulas Patocka }
7006b11f9d8SMikulas Patocka
dlfb_offload_damage(struct dlfb_data * dlfb,int x,int y,int width,int height)7016b11f9d8SMikulas Patocka static void dlfb_offload_damage(struct dlfb_data *dlfb, int x, int y, int width, int height)
7026b11f9d8SMikulas Patocka {
7036b11f9d8SMikulas Patocka unsigned long flags;
7046b11f9d8SMikulas Patocka int x2 = x + width;
7056b11f9d8SMikulas Patocka int y2 = y + height;
7066b11f9d8SMikulas Patocka
7076b11f9d8SMikulas Patocka if (x >= x2 || y >= y2)
7086b11f9d8SMikulas Patocka return;
7096b11f9d8SMikulas Patocka
7106b11f9d8SMikulas Patocka spin_lock_irqsave(&dlfb->damage_lock, flags);
7116b11f9d8SMikulas Patocka dlfb->damage_x = min(x, dlfb->damage_x);
7126b11f9d8SMikulas Patocka dlfb->damage_x2 = max(x2, dlfb->damage_x2);
7136b11f9d8SMikulas Patocka dlfb->damage_y = min(y, dlfb->damage_y);
7146b11f9d8SMikulas Patocka dlfb->damage_y2 = max(y2, dlfb->damage_y2);
7156b11f9d8SMikulas Patocka spin_unlock_irqrestore(&dlfb->damage_lock, flags);
7166b11f9d8SMikulas Patocka
7176b11f9d8SMikulas Patocka schedule_work(&dlfb->damage_work);
7186b11f9d8SMikulas Patocka }
7196b11f9d8SMikulas Patocka
720f7018c21STomi Valkeinen /*
721f7018c21STomi Valkeinen * NOTE: fb_defio.c is holding info->fbdefio.mutex
722f7018c21STomi Valkeinen * Touching ANY framebuffer memory that triggers a page fault
723f7018c21STomi Valkeinen * in fb_defio will cause a deadlock, when it also tries to
724f7018c21STomi Valkeinen * grab the same mutex.
725f7018c21STomi Valkeinen */
dlfb_dpy_deferred_io(struct fb_info * info,struct list_head * pagereflist)726e80eec1bSThomas Zimmermann static void dlfb_dpy_deferred_io(struct fb_info *info, struct list_head *pagereflist)
727f7018c21STomi Valkeinen {
72856c134f7SThomas Zimmermann struct fb_deferred_io_pageref *pageref;
7297ea46206SLadislav Michl struct dlfb_data *dlfb = info->par;
730f7018c21STomi Valkeinen struct urb *urb;
731f7018c21STomi Valkeinen char *cmd;
732f7018c21STomi Valkeinen cycles_t start_cycles, end_cycles;
733f7018c21STomi Valkeinen int bytes_sent = 0;
734f7018c21STomi Valkeinen int bytes_identical = 0;
735f7018c21STomi Valkeinen int bytes_rendered = 0;
736f7018c21STomi Valkeinen
737babc250eSMikulas Patocka mutex_lock(&dlfb->render_mutex);
738babc250eSMikulas Patocka
739f7018c21STomi Valkeinen if (!fb_defio)
740babc250eSMikulas Patocka goto unlock_ret;
741f7018c21STomi Valkeinen
7427ea46206SLadislav Michl if (!atomic_read(&dlfb->usb_active))
743babc250eSMikulas Patocka goto unlock_ret;
744f7018c21STomi Valkeinen
745f7018c21STomi Valkeinen start_cycles = get_cycles();
746f7018c21STomi Valkeinen
7477ea46206SLadislav Michl urb = dlfb_get_urb(dlfb);
748f7018c21STomi Valkeinen if (!urb)
749babc250eSMikulas Patocka goto unlock_ret;
750f7018c21STomi Valkeinen
751f7018c21STomi Valkeinen cmd = urb->transfer_buffer;
752f7018c21STomi Valkeinen
753f7018c21STomi Valkeinen /* walk the written page list and render each to device */
754e80eec1bSThomas Zimmermann list_for_each_entry(pageref, pagereflist, list) {
7557ea46206SLadislav Michl if (dlfb_render_hline(dlfb, &urb, (char *) info->fix.smem_start,
756e2d8b428SThomas Zimmermann &cmd, pageref->offset, PAGE_SIZE,
757e2d8b428SThomas Zimmermann &bytes_identical, &bytes_sent))
758f7018c21STomi Valkeinen goto error;
759f7018c21STomi Valkeinen bytes_rendered += PAGE_SIZE;
760f7018c21STomi Valkeinen }
761f7018c21STomi Valkeinen
762f7018c21STomi Valkeinen if (cmd > (char *) urb->transfer_buffer) {
7634e705e17SMikulas Patocka int len;
7644e705e17SMikulas Patocka if (cmd < (char *) urb->transfer_buffer + urb->transfer_buffer_length)
7654e705e17SMikulas Patocka *cmd++ = 0xAF;
766f7018c21STomi Valkeinen /* Send partial buffer remaining before exiting */
7674e705e17SMikulas Patocka len = cmd - (char *) urb->transfer_buffer;
7687ea46206SLadislav Michl dlfb_submit_urb(dlfb, urb, len);
769f7018c21STomi Valkeinen bytes_sent += len;
770f7018c21STomi Valkeinen } else
771f7018c21STomi Valkeinen dlfb_urb_completion(urb);
772f7018c21STomi Valkeinen
773f7018c21STomi Valkeinen error:
7747ea46206SLadislav Michl atomic_add(bytes_sent, &dlfb->bytes_sent);
7757ea46206SLadislav Michl atomic_add(bytes_identical, &dlfb->bytes_identical);
7767ea46206SLadislav Michl atomic_add(bytes_rendered, &dlfb->bytes_rendered);
777f7018c21STomi Valkeinen end_cycles = get_cycles();
778f7018c21STomi Valkeinen atomic_add(((unsigned int) ((end_cycles - start_cycles)
779f7018c21STomi Valkeinen >> 10)), /* Kcycles */
7807ea46206SLadislav Michl &dlfb->cpu_kcycles_used);
781babc250eSMikulas Patocka unlock_ret:
782babc250eSMikulas Patocka mutex_unlock(&dlfb->render_mutex);
783f7018c21STomi Valkeinen }
784f7018c21STomi Valkeinen
dlfb_get_edid(struct dlfb_data * dlfb,char * edid,int len)7857ea46206SLadislav Michl static int dlfb_get_edid(struct dlfb_data *dlfb, char *edid, int len)
786f7018c21STomi Valkeinen {
7875865889fSLadislav Michl int i, ret;
788f7018c21STomi Valkeinen char *rbuf;
789f7018c21STomi Valkeinen
790f7018c21STomi Valkeinen rbuf = kmalloc(2, GFP_KERNEL);
791f7018c21STomi Valkeinen if (!rbuf)
792f7018c21STomi Valkeinen return 0;
793f7018c21STomi Valkeinen
794f7018c21STomi Valkeinen for (i = 0; i < len; i++) {
7957ea46206SLadislav Michl ret = usb_control_msg(dlfb->udev,
7967ea46206SLadislav Michl usb_rcvctrlpipe(dlfb->udev, 0), 0x02,
797c9876947SLadislav Michl (0x80 | (0x02 << 5)), i << 8, 0xA1,
798c9876947SLadislav Michl rbuf, 2, USB_CTRL_GET_TIMEOUT);
799c9876947SLadislav Michl if (ret < 2) {
8005865889fSLadislav Michl dev_err(&dlfb->udev->dev,
8015865889fSLadislav Michl "Read EDID byte %d failed: %d\n", i, ret);
802f7018c21STomi Valkeinen i--;
803f7018c21STomi Valkeinen break;
804f7018c21STomi Valkeinen }
805f7018c21STomi Valkeinen edid[i] = rbuf[1];
806f7018c21STomi Valkeinen }
807f7018c21STomi Valkeinen
808f7018c21STomi Valkeinen kfree(rbuf);
809f7018c21STomi Valkeinen
810f7018c21STomi Valkeinen return i;
811f7018c21STomi Valkeinen }
812f7018c21STomi Valkeinen
dlfb_ops_ioctl(struct fb_info * info,unsigned int cmd,unsigned long arg)813f7018c21STomi Valkeinen static int dlfb_ops_ioctl(struct fb_info *info, unsigned int cmd,
814f7018c21STomi Valkeinen unsigned long arg)
815f7018c21STomi Valkeinen {
816f7018c21STomi Valkeinen
8177ea46206SLadislav Michl struct dlfb_data *dlfb = info->par;
818f7018c21STomi Valkeinen
8197ea46206SLadislav Michl if (!atomic_read(&dlfb->usb_active))
820f7018c21STomi Valkeinen return 0;
821f7018c21STomi Valkeinen
822f7018c21STomi Valkeinen /* TODO: Update X server to get this from sysfs instead */
823f7018c21STomi Valkeinen if (cmd == DLFB_IOCTL_RETURN_EDID) {
824f7018c21STomi Valkeinen void __user *edid = (void __user *)arg;
8257ea46206SLadislav Michl if (copy_to_user(edid, dlfb->edid, dlfb->edid_size))
826f7018c21STomi Valkeinen return -EFAULT;
827f7018c21STomi Valkeinen return 0;
828f7018c21STomi Valkeinen }
829f7018c21STomi Valkeinen
830f7018c21STomi Valkeinen /* TODO: Help propose a standard fb.h ioctl to report mmap damage */
831f7018c21STomi Valkeinen if (cmd == DLFB_IOCTL_REPORT_DAMAGE) {
832f7018c21STomi Valkeinen struct dloarea area;
833f7018c21STomi Valkeinen
834f7018c21STomi Valkeinen if (copy_from_user(&area, (void __user *)arg,
835f7018c21STomi Valkeinen sizeof(struct dloarea)))
836f7018c21STomi Valkeinen return -EFAULT;
837f7018c21STomi Valkeinen
838f7018c21STomi Valkeinen /*
839f7018c21STomi Valkeinen * If we have a damage-aware client, turn fb_defio "off"
840f7018c21STomi Valkeinen * To avoid perf imact of unnecessary page fault handling.
841f7018c21STomi Valkeinen * Done by resetting the delay for this fb_info to a very
842f7018c21STomi Valkeinen * long period. Pages will become writable and stay that way.
843f7018c21STomi Valkeinen * Reset to normal value when all clients have closed this fb.
844f7018c21STomi Valkeinen */
845f7018c21STomi Valkeinen if (info->fbdefio)
846f7018c21STomi Valkeinen info->fbdefio->delay = DL_DEFIO_WRITE_DISABLE;
847f7018c21STomi Valkeinen
848f7018c21STomi Valkeinen if (area.x < 0)
849f7018c21STomi Valkeinen area.x = 0;
850f7018c21STomi Valkeinen
851f7018c21STomi Valkeinen if (area.x > info->var.xres)
852f7018c21STomi Valkeinen area.x = info->var.xres;
853f7018c21STomi Valkeinen
854f7018c21STomi Valkeinen if (area.y < 0)
855f7018c21STomi Valkeinen area.y = 0;
856f7018c21STomi Valkeinen
857f7018c21STomi Valkeinen if (area.y > info->var.yres)
858f7018c21STomi Valkeinen area.y = info->var.yres;
859f7018c21STomi Valkeinen
860bd86b6c5SMikulas Patocka dlfb_handle_damage(dlfb, area.x, area.y, area.w, area.h);
861f7018c21STomi Valkeinen }
862f7018c21STomi Valkeinen
863f7018c21STomi Valkeinen return 0;
864f7018c21STomi Valkeinen }
865f7018c21STomi Valkeinen
866f7018c21STomi Valkeinen /* taken from vesafb */
867f7018c21STomi Valkeinen static int
dlfb_ops_setcolreg(unsigned regno,unsigned red,unsigned green,unsigned blue,unsigned transp,struct fb_info * info)868f7018c21STomi Valkeinen dlfb_ops_setcolreg(unsigned regno, unsigned red, unsigned green,
869f7018c21STomi Valkeinen unsigned blue, unsigned transp, struct fb_info *info)
870f7018c21STomi Valkeinen {
871f7018c21STomi Valkeinen int err = 0;
872f7018c21STomi Valkeinen
873f7018c21STomi Valkeinen if (regno >= info->cmap.len)
874f7018c21STomi Valkeinen return 1;
875f7018c21STomi Valkeinen
876f7018c21STomi Valkeinen if (regno < 16) {
877f7018c21STomi Valkeinen if (info->var.red.offset == 10) {
878f7018c21STomi Valkeinen /* 1:5:5:5 */
879f7018c21STomi Valkeinen ((u32 *) (info->pseudo_palette))[regno] =
880f7018c21STomi Valkeinen ((red & 0xf800) >> 1) |
881f7018c21STomi Valkeinen ((green & 0xf800) >> 6) | ((blue & 0xf800) >> 11);
882f7018c21STomi Valkeinen } else {
883f7018c21STomi Valkeinen /* 0:5:6:5 */
884f7018c21STomi Valkeinen ((u32 *) (info->pseudo_palette))[regno] =
885f7018c21STomi Valkeinen ((red & 0xf800)) |
886f7018c21STomi Valkeinen ((green & 0xfc00) >> 5) | ((blue & 0xf800) >> 11);
887f7018c21STomi Valkeinen }
888f7018c21STomi Valkeinen }
889f7018c21STomi Valkeinen
890f7018c21STomi Valkeinen return err;
891f7018c21STomi Valkeinen }
892f7018c21STomi Valkeinen
893f7018c21STomi Valkeinen /*
894f7018c21STomi Valkeinen * It's common for several clients to have framebuffer open simultaneously.
895f7018c21STomi Valkeinen * e.g. both fbcon and X. Makes things interesting.
896f7018c21STomi Valkeinen * Assumes caller is holding info->lock (for open and release at least)
897f7018c21STomi Valkeinen */
dlfb_ops_open(struct fb_info * info,int user)898f7018c21STomi Valkeinen static int dlfb_ops_open(struct fb_info *info, int user)
899f7018c21STomi Valkeinen {
9007ea46206SLadislav Michl struct dlfb_data *dlfb = info->par;
901f7018c21STomi Valkeinen
902f7018c21STomi Valkeinen /*
903f7018c21STomi Valkeinen * fbcon aggressively connects to first framebuffer it finds,
904f7018c21STomi Valkeinen * preventing other clients (X) from working properly. Usually
905f7018c21STomi Valkeinen * not what the user wants. Fail by default with option to enable.
906f7018c21STomi Valkeinen */
907f7018c21STomi Valkeinen if ((user == 0) && (!console))
908f7018c21STomi Valkeinen return -EBUSY;
909f7018c21STomi Valkeinen
910f7018c21STomi Valkeinen /* If the USB device is gone, we don't accept new opens */
9117ea46206SLadislav Michl if (dlfb->virtualized)
912f7018c21STomi Valkeinen return -ENODEV;
913f7018c21STomi Valkeinen
9147ea46206SLadislav Michl dlfb->fb_count++;
915f7018c21STomi Valkeinen
916f7018c21STomi Valkeinen if (fb_defio && (info->fbdefio == NULL)) {
917f7018c21STomi Valkeinen /* enable defio at last moment if not disabled by client */
918f7018c21STomi Valkeinen
919f7018c21STomi Valkeinen struct fb_deferred_io *fbdefio;
920f7018c21STomi Valkeinen
921f7018c21STomi Valkeinen fbdefio = kzalloc(sizeof(struct fb_deferred_io), GFP_KERNEL);
922f7018c21STomi Valkeinen
923f7018c21STomi Valkeinen if (fbdefio) {
924f7018c21STomi Valkeinen fbdefio->delay = DL_DEFIO_WRITE_DELAY;
925e80eec1bSThomas Zimmermann fbdefio->sort_pagereflist = true;
926f7018c21STomi Valkeinen fbdefio->deferred_io = dlfb_dpy_deferred_io;
927f7018c21STomi Valkeinen }
928f7018c21STomi Valkeinen
929f7018c21STomi Valkeinen info->fbdefio = fbdefio;
930f7018c21STomi Valkeinen fb_deferred_io_init(info);
931f7018c21STomi Valkeinen }
932f7018c21STomi Valkeinen
9335865889fSLadislav Michl dev_dbg(info->dev, "open, user=%d fb_info=%p count=%d\n",
9345865889fSLadislav Michl user, info, dlfb->fb_count);
935f7018c21STomi Valkeinen
936f7018c21STomi Valkeinen return 0;
937f7018c21STomi Valkeinen }
938f7018c21STomi Valkeinen
dlfb_ops_destroy(struct fb_info * info)93968a958a9SMikulas Patocka static void dlfb_ops_destroy(struct fb_info *info)
940f7018c21STomi Valkeinen {
94168a958a9SMikulas Patocka struct dlfb_data *dlfb = info->par;
942f7018c21STomi Valkeinen
9436b11f9d8SMikulas Patocka cancel_work_sync(&dlfb->damage_work);
9446b11f9d8SMikulas Patocka
945babc250eSMikulas Patocka mutex_destroy(&dlfb->render_mutex);
946babc250eSMikulas Patocka
947f7018c21STomi Valkeinen if (info->cmap.len != 0)
948f7018c21STomi Valkeinen fb_dealloc_cmap(&info->cmap);
949f7018c21STomi Valkeinen if (info->monspecs.modedb)
950f7018c21STomi Valkeinen fb_destroy_modedb(info->monspecs.modedb);
951962a3fafSThomas Zimmermann vfree(info->screen_buffer);
952f7018c21STomi Valkeinen
953f7018c21STomi Valkeinen fb_destroy_modelist(&info->modelist);
954f7018c21STomi Valkeinen
95568a958a9SMikulas Patocka while (!list_empty(&dlfb->deferred_free)) {
95668a958a9SMikulas Patocka struct dlfb_deferred_free *d = list_entry(dlfb->deferred_free.next, struct dlfb_deferred_free, list);
95768a958a9SMikulas Patocka list_del(&d->list);
95868a958a9SMikulas Patocka vfree(d->mem);
95968a958a9SMikulas Patocka kfree(d);
96068a958a9SMikulas Patocka }
96168a958a9SMikulas Patocka vfree(dlfb->backing_buffer);
96268a958a9SMikulas Patocka kfree(dlfb->edid);
9635c0e4110SZqiang dlfb_free_urb_list(dlfb);
96468a958a9SMikulas Patocka usb_put_dev(dlfb->udev);
96568a958a9SMikulas Patocka kfree(dlfb);
966f7018c21STomi Valkeinen
967f7018c21STomi Valkeinen /* Assume info structure is freed after this point */
968f7018c21STomi Valkeinen framebuffer_release(info);
969f7018c21STomi Valkeinen }
970f7018c21STomi Valkeinen
971f7018c21STomi Valkeinen /*
972f7018c21STomi Valkeinen * Assumes caller is holding info->lock mutex (for open and release at least)
973f7018c21STomi Valkeinen */
dlfb_ops_release(struct fb_info * info,int user)974f7018c21STomi Valkeinen static int dlfb_ops_release(struct fb_info *info, int user)
975f7018c21STomi Valkeinen {
9767ea46206SLadislav Michl struct dlfb_data *dlfb = info->par;
977f7018c21STomi Valkeinen
9787ea46206SLadislav Michl dlfb->fb_count--;
979f7018c21STomi Valkeinen
9807ea46206SLadislav Michl if ((dlfb->fb_count == 0) && (info->fbdefio)) {
981f7018c21STomi Valkeinen fb_deferred_io_cleanup(info);
982f7018c21STomi Valkeinen kfree(info->fbdefio);
983f7018c21STomi Valkeinen info->fbdefio = NULL;
984f7018c21STomi Valkeinen }
985f7018c21STomi Valkeinen
9865865889fSLadislav Michl dev_dbg(info->dev, "release, user=%d count=%d\n", user, dlfb->fb_count);
987f7018c21STomi Valkeinen
988f7018c21STomi Valkeinen return 0;
989f7018c21STomi Valkeinen }
990f7018c21STomi Valkeinen
991f7018c21STomi Valkeinen /*
992f7018c21STomi Valkeinen * Check whether a video mode is supported by the DisplayLink chip
993f7018c21STomi Valkeinen * We start from monitor's modes, so don't need to filter that here
994f7018c21STomi Valkeinen */
dlfb_is_valid_mode(struct fb_videomode * mode,struct dlfb_data * dlfb)9955865889fSLadislav Michl static int dlfb_is_valid_mode(struct fb_videomode *mode, struct dlfb_data *dlfb)
996f7018c21STomi Valkeinen {
9975865889fSLadislav Michl if (mode->xres * mode->yres > dlfb->sku_pixel_limit)
998f7018c21STomi Valkeinen return 0;
999f7018c21STomi Valkeinen
1000f7018c21STomi Valkeinen return 1;
1001f7018c21STomi Valkeinen }
1002f7018c21STomi Valkeinen
dlfb_var_color_format(struct fb_var_screeninfo * var)1003f7018c21STomi Valkeinen static void dlfb_var_color_format(struct fb_var_screeninfo *var)
1004f7018c21STomi Valkeinen {
1005f7018c21STomi Valkeinen const struct fb_bitfield red = { 11, 5, 0 };
1006f7018c21STomi Valkeinen const struct fb_bitfield green = { 5, 6, 0 };
1007f7018c21STomi Valkeinen const struct fb_bitfield blue = { 0, 5, 0 };
1008f7018c21STomi Valkeinen
1009f7018c21STomi Valkeinen var->bits_per_pixel = 16;
1010f7018c21STomi Valkeinen var->red = red;
1011f7018c21STomi Valkeinen var->green = green;
1012f7018c21STomi Valkeinen var->blue = blue;
1013f7018c21STomi Valkeinen }
1014f7018c21STomi Valkeinen
dlfb_ops_check_var(struct fb_var_screeninfo * var,struct fb_info * info)1015f7018c21STomi Valkeinen static int dlfb_ops_check_var(struct fb_var_screeninfo *var,
1016f7018c21STomi Valkeinen struct fb_info *info)
1017f7018c21STomi Valkeinen {
1018f7018c21STomi Valkeinen struct fb_videomode mode;
10195865889fSLadislav Michl struct dlfb_data *dlfb = info->par;
1020f7018c21STomi Valkeinen
1021f7018c21STomi Valkeinen /* set device-specific elements of var unrelated to mode */
1022f7018c21STomi Valkeinen dlfb_var_color_format(var);
1023f7018c21STomi Valkeinen
1024f7018c21STomi Valkeinen fb_var_to_videomode(&mode, var);
1025f7018c21STomi Valkeinen
10265865889fSLadislav Michl if (!dlfb_is_valid_mode(&mode, dlfb))
1027f7018c21STomi Valkeinen return -EINVAL;
1028f7018c21STomi Valkeinen
1029f7018c21STomi Valkeinen return 0;
1030f7018c21STomi Valkeinen }
1031f7018c21STomi Valkeinen
dlfb_ops_set_par(struct fb_info * info)1032f7018c21STomi Valkeinen static int dlfb_ops_set_par(struct fb_info *info)
1033f7018c21STomi Valkeinen {
10347ea46206SLadislav Michl struct dlfb_data *dlfb = info->par;
1035f7018c21STomi Valkeinen int result;
1036f7018c21STomi Valkeinen u16 *pix_framebuffer;
1037f7018c21STomi Valkeinen int i;
1038564f1807SMikulas Patocka struct fb_var_screeninfo fvs;
10397433914eSMikulas Patocka u32 line_length = info->var.xres * (info->var.bits_per_pixel / 8);
1040564f1807SMikulas Patocka
1041564f1807SMikulas Patocka /* clear the activate field because it causes spurious miscompares */
1042564f1807SMikulas Patocka fvs = info->var;
1043564f1807SMikulas Patocka fvs.activate = 0;
1044564f1807SMikulas Patocka fvs.vmode &= ~FB_VMODE_SMOOTH_XPAN;
1045564f1807SMikulas Patocka
1046564f1807SMikulas Patocka if (!memcmp(&dlfb->current_mode, &fvs, sizeof(struct fb_var_screeninfo)))
1047564f1807SMikulas Patocka return 0;
1048f7018c21STomi Valkeinen
10497433914eSMikulas Patocka result = dlfb_realloc_framebuffer(dlfb, info, info->var.yres * line_length);
10507433914eSMikulas Patocka if (result)
10517433914eSMikulas Patocka return result;
10527433914eSMikulas Patocka
10537ea46206SLadislav Michl result = dlfb_set_video_mode(dlfb, &info->var);
1054f7018c21STomi Valkeinen
1055564f1807SMikulas Patocka if (result)
1056564f1807SMikulas Patocka return result;
1057564f1807SMikulas Patocka
1058564f1807SMikulas Patocka dlfb->current_mode = fvs;
10597433914eSMikulas Patocka info->fix.line_length = line_length;
1060564f1807SMikulas Patocka
1061564f1807SMikulas Patocka if (dlfb->fb_count == 0) {
1062f7018c21STomi Valkeinen
1063f7018c21STomi Valkeinen /* paint greenscreen */
1064f7018c21STomi Valkeinen
1065962a3fafSThomas Zimmermann pix_framebuffer = (u16 *)info->screen_buffer;
1066f7018c21STomi Valkeinen for (i = 0; i < info->fix.smem_len / 2; i++)
1067f7018c21STomi Valkeinen pix_framebuffer[i] = 0x37e6;
10687433914eSMikulas Patocka }
1069f7018c21STomi Valkeinen
1070bd86b6c5SMikulas Patocka dlfb_handle_damage(dlfb, 0, 0, info->var.xres, info->var.yres);
1071f7018c21STomi Valkeinen
1072564f1807SMikulas Patocka return 0;
1073f7018c21STomi Valkeinen }
1074f7018c21STomi Valkeinen
1075f7018c21STomi Valkeinen /* To fonzi the jukebox (e.g. make blanking changes take effect) */
dlfb_dummy_render(char * buf)1076f7018c21STomi Valkeinen static char *dlfb_dummy_render(char *buf)
1077f7018c21STomi Valkeinen {
1078f7018c21STomi Valkeinen *buf++ = 0xAF;
1079f7018c21STomi Valkeinen *buf++ = 0x6A; /* copy */
1080f7018c21STomi Valkeinen *buf++ = 0x00; /* from address*/
1081f7018c21STomi Valkeinen *buf++ = 0x00;
1082f7018c21STomi Valkeinen *buf++ = 0x00;
1083f7018c21STomi Valkeinen *buf++ = 0x01; /* one pixel */
1084f7018c21STomi Valkeinen *buf++ = 0x00; /* to address */
1085f7018c21STomi Valkeinen *buf++ = 0x00;
1086f7018c21STomi Valkeinen *buf++ = 0x00;
1087f7018c21STomi Valkeinen return buf;
1088f7018c21STomi Valkeinen }
1089f7018c21STomi Valkeinen
1090f7018c21STomi Valkeinen /*
1091f7018c21STomi Valkeinen * In order to come back from full DPMS off, we need to set the mode again
1092f7018c21STomi Valkeinen */
dlfb_ops_blank(int blank_mode,struct fb_info * info)1093f7018c21STomi Valkeinen static int dlfb_ops_blank(int blank_mode, struct fb_info *info)
1094f7018c21STomi Valkeinen {
10957ea46206SLadislav Michl struct dlfb_data *dlfb = info->par;
1096f7018c21STomi Valkeinen char *bufptr;
1097f7018c21STomi Valkeinen struct urb *urb;
1098f7018c21STomi Valkeinen
10995865889fSLadislav Michl dev_dbg(info->dev, "blank, mode %d --> %d\n",
11005865889fSLadislav Michl dlfb->blank_mode, blank_mode);
1101f7018c21STomi Valkeinen
11027ea46206SLadislav Michl if ((dlfb->blank_mode == FB_BLANK_POWERDOWN) &&
1103f7018c21STomi Valkeinen (blank_mode != FB_BLANK_POWERDOWN)) {
1104f7018c21STomi Valkeinen
1105f7018c21STomi Valkeinen /* returning from powerdown requires a fresh modeset */
11067ea46206SLadislav Michl dlfb_set_video_mode(dlfb, &info->var);
1107f7018c21STomi Valkeinen }
1108f7018c21STomi Valkeinen
11097ea46206SLadislav Michl urb = dlfb_get_urb(dlfb);
1110f7018c21STomi Valkeinen if (!urb)
1111f7018c21STomi Valkeinen return 0;
1112f7018c21STomi Valkeinen
1113f7018c21STomi Valkeinen bufptr = (char *) urb->transfer_buffer;
1114f7018c21STomi Valkeinen bufptr = dlfb_vidreg_lock(bufptr);
1115f7018c21STomi Valkeinen bufptr = dlfb_blanking(bufptr, blank_mode);
1116f7018c21STomi Valkeinen bufptr = dlfb_vidreg_unlock(bufptr);
1117f7018c21STomi Valkeinen
1118f7018c21STomi Valkeinen /* seems like a render op is needed to have blank change take effect */
1119f7018c21STomi Valkeinen bufptr = dlfb_dummy_render(bufptr);
1120f7018c21STomi Valkeinen
11217ea46206SLadislav Michl dlfb_submit_urb(dlfb, urb, bufptr -
1122f7018c21STomi Valkeinen (char *) urb->transfer_buffer);
1123f7018c21STomi Valkeinen
11247ea46206SLadislav Michl dlfb->blank_mode = blank_mode;
1125f7018c21STomi Valkeinen
1126f7018c21STomi Valkeinen return 0;
1127f7018c21STomi Valkeinen }
1128f7018c21STomi Valkeinen
dlfb_ops_damage_range(struct fb_info * info,off_t off,size_t len)11291e3c7216SThomas Zimmermann static void dlfb_ops_damage_range(struct fb_info *info, off_t off, size_t len)
11301e3c7216SThomas Zimmermann {
11311e3c7216SThomas Zimmermann struct dlfb_data *dlfb = info->par;
11321e3c7216SThomas Zimmermann int start = max((int)(off / info->fix.line_length), 0);
11331e3c7216SThomas Zimmermann int lines = min((u32)((len / info->fix.line_length) + 1), (u32)info->var.yres);
11341e3c7216SThomas Zimmermann
11351e3c7216SThomas Zimmermann dlfb_handle_damage(dlfb, 0, start, info->var.xres, lines);
11361e3c7216SThomas Zimmermann }
11371e3c7216SThomas Zimmermann
dlfb_ops_damage_area(struct fb_info * info,u32 x,u32 y,u32 width,u32 height)11381e3c7216SThomas Zimmermann static void dlfb_ops_damage_area(struct fb_info *info, u32 x, u32 y, u32 width, u32 height)
11391e3c7216SThomas Zimmermann {
11401e3c7216SThomas Zimmermann struct dlfb_data *dlfb = info->par;
11411e3c7216SThomas Zimmermann
11421e3c7216SThomas Zimmermann dlfb_offload_damage(dlfb, x, y, width, height);
11431e3c7216SThomas Zimmermann }
11441e3c7216SThomas Zimmermann
11451e3c7216SThomas Zimmermann FB_GEN_DEFAULT_DEFERRED_SYSMEM_OPS(dlfb_ops,
11461e3c7216SThomas Zimmermann dlfb_ops_damage_range,
11471e3c7216SThomas Zimmermann dlfb_ops_damage_area)
11481e3c7216SThomas Zimmermann
1149df5eff6dSNishka Dasgupta static const struct fb_ops dlfb_ops = {
1150f7018c21STomi Valkeinen .owner = THIS_MODULE,
11511e3c7216SThomas Zimmermann __FB_DEFAULT_DEFERRED_OPS_RDWR(dlfb_ops),
1152f7018c21STomi Valkeinen .fb_setcolreg = dlfb_ops_setcolreg,
11531e3c7216SThomas Zimmermann __FB_DEFAULT_DEFERRED_OPS_DRAW(dlfb_ops),
1154f7018c21STomi Valkeinen .fb_mmap = dlfb_ops_mmap,
1155f7018c21STomi Valkeinen .fb_ioctl = dlfb_ops_ioctl,
1156f7018c21STomi Valkeinen .fb_open = dlfb_ops_open,
1157f7018c21STomi Valkeinen .fb_release = dlfb_ops_release,
1158f7018c21STomi Valkeinen .fb_blank = dlfb_ops_blank,
1159f7018c21STomi Valkeinen .fb_check_var = dlfb_ops_check_var,
1160f7018c21STomi Valkeinen .fb_set_par = dlfb_ops_set_par,
116168a958a9SMikulas Patocka .fb_destroy = dlfb_ops_destroy,
1162f7018c21STomi Valkeinen };
1163f7018c21STomi Valkeinen
1164f7018c21STomi Valkeinen
dlfb_deferred_vfree(struct dlfb_data * dlfb,void * mem)11657433914eSMikulas Patocka static void dlfb_deferred_vfree(struct dlfb_data *dlfb, void *mem)
11667433914eSMikulas Patocka {
11677433914eSMikulas Patocka struct dlfb_deferred_free *d = kmalloc(sizeof(struct dlfb_deferred_free), GFP_KERNEL);
11687433914eSMikulas Patocka if (!d)
11697433914eSMikulas Patocka return;
11707433914eSMikulas Patocka d->mem = mem;
11717433914eSMikulas Patocka list_add(&d->list, &dlfb->deferred_free);
11727433914eSMikulas Patocka }
11737433914eSMikulas Patocka
1174f7018c21STomi Valkeinen /*
1175f7018c21STomi Valkeinen * Assumes &info->lock held by caller
1176f7018c21STomi Valkeinen * Assumes no active clients have framebuffer open
1177f7018c21STomi Valkeinen */
dlfb_realloc_framebuffer(struct dlfb_data * dlfb,struct fb_info * info,u32 new_len)11787433914eSMikulas Patocka static int dlfb_realloc_framebuffer(struct dlfb_data *dlfb, struct fb_info *info, u32 new_len)
1179f7018c21STomi Valkeinen {
11807433914eSMikulas Patocka u32 old_len = info->fix.smem_len;
1181962a3fafSThomas Zimmermann const void *old_fb = info->screen_buffer;
1182f7018c21STomi Valkeinen unsigned char *new_fb;
1183f7018c21STomi Valkeinen unsigned char *new_back = NULL;
1184f7018c21STomi Valkeinen
11857433914eSMikulas Patocka new_len = PAGE_ALIGN(new_len);
1186f7018c21STomi Valkeinen
11877433914eSMikulas Patocka if (new_len > old_len) {
1188f7018c21STomi Valkeinen /*
1189f7018c21STomi Valkeinen * Alloc system memory for virtual framebuffer
1190f7018c21STomi Valkeinen */
1191f7018c21STomi Valkeinen new_fb = vmalloc(new_len);
1192f7018c21STomi Valkeinen if (!new_fb) {
11935865889fSLadislav Michl dev_err(info->dev, "Virtual framebuffer alloc failed\n");
11943c097b06SMarkus Elfring return -ENOMEM;
1195f7018c21STomi Valkeinen }
11967433914eSMikulas Patocka memset(new_fb, 0xff, new_len);
1197f7018c21STomi Valkeinen
1198962a3fafSThomas Zimmermann if (info->screen_buffer) {
1199f7018c21STomi Valkeinen memcpy(new_fb, old_fb, old_len);
1200962a3fafSThomas Zimmermann dlfb_deferred_vfree(dlfb, info->screen_buffer);
1201f7018c21STomi Valkeinen }
1202f7018c21STomi Valkeinen
1203962a3fafSThomas Zimmermann info->screen_buffer = new_fb;
12047433914eSMikulas Patocka info->fix.smem_len = new_len;
1205f7018c21STomi Valkeinen info->fix.smem_start = (unsigned long) new_fb;
1206f7018c21STomi Valkeinen info->flags = udlfb_info_flags;
1207f7018c21STomi Valkeinen
1208f7018c21STomi Valkeinen /*
1209f7018c21STomi Valkeinen * Second framebuffer copy to mirror the framebuffer state
1210f7018c21STomi Valkeinen * on the physical USB device. We can function without this.
1211f7018c21STomi Valkeinen * But with imperfect damage info we may send pixels over USB
1212f7018c21STomi Valkeinen * that were, in fact, unchanged - wasting limited USB bandwidth
1213f7018c21STomi Valkeinen */
1214f7018c21STomi Valkeinen if (shadow)
1215f7018c21STomi Valkeinen new_back = vzalloc(new_len);
1216f7018c21STomi Valkeinen if (!new_back)
12175865889fSLadislav Michl dev_info(info->dev,
12185865889fSLadislav Michl "No shadow/backing buffer allocated\n");
1219f7018c21STomi Valkeinen else {
12207433914eSMikulas Patocka dlfb_deferred_vfree(dlfb, dlfb->backing_buffer);
12217ea46206SLadislav Michl dlfb->backing_buffer = new_back;
1222f7018c21STomi Valkeinen }
1223f7018c21STomi Valkeinen }
12243c097b06SMarkus Elfring return 0;
1225f7018c21STomi Valkeinen }
1226f7018c21STomi Valkeinen
1227f7018c21STomi Valkeinen /*
1228f7018c21STomi Valkeinen * 1) Get EDID from hw, or use sw default
1229f7018c21STomi Valkeinen * 2) Parse into various fb_info structs
1230f7018c21STomi Valkeinen * 3) Allocate virtual framebuffer memory to back highest res mode
1231f7018c21STomi Valkeinen *
1232f7018c21STomi Valkeinen * Parses EDID into three places used by various parts of fbdev:
1233f7018c21STomi Valkeinen * fb_var_screeninfo contains the timing of the monitor's preferred mode
1234f7018c21STomi Valkeinen * fb_info.monspecs is full parsed EDID info, including monspecs.modedb
1235f7018c21STomi Valkeinen * fb_info.modelist is a linked list of all monitor & VESA modes which work
1236f7018c21STomi Valkeinen *
1237f7018c21STomi Valkeinen * If EDID is not readable/valid, then modelist is all VESA modes,
1238f7018c21STomi Valkeinen * monspecs is NULL, and fb_var_screeninfo is set to safe VESA mode
1239f7018c21STomi Valkeinen * Returns 0 if successful
1240f7018c21STomi Valkeinen */
dlfb_setup_modes(struct dlfb_data * dlfb,struct fb_info * info,char * default_edid,size_t default_edid_size)12417ea46206SLadislav Michl static int dlfb_setup_modes(struct dlfb_data *dlfb,
1242f7018c21STomi Valkeinen struct fb_info *info,
1243f7018c21STomi Valkeinen char *default_edid, size_t default_edid_size)
1244f7018c21STomi Valkeinen {
1245f7018c21STomi Valkeinen char *edid;
12465865889fSLadislav Michl int i, result = 0, tries = 3;
12475865889fSLadislav Michl struct device *dev = info->device;
12485865889fSLadislav Michl struct fb_videomode *mode;
12495865889fSLadislav Michl const struct fb_videomode *default_vmode = NULL;
1250f7018c21STomi Valkeinen
12515865889fSLadislav Michl if (info->dev) {
12525865889fSLadislav Michl /* only use mutex if info has been registered */
1253f7018c21STomi Valkeinen mutex_lock(&info->lock);
12545865889fSLadislav Michl /* parent device is used otherwise */
12555865889fSLadislav Michl dev = info->dev;
12565865889fSLadislav Michl }
1257f7018c21STomi Valkeinen
1258f7018c21STomi Valkeinen edid = kmalloc(EDID_LENGTH, GFP_KERNEL);
1259f7018c21STomi Valkeinen if (!edid) {
1260f7018c21STomi Valkeinen result = -ENOMEM;
1261f7018c21STomi Valkeinen goto error;
1262f7018c21STomi Valkeinen }
1263f7018c21STomi Valkeinen
1264f7018c21STomi Valkeinen fb_destroy_modelist(&info->modelist);
1265f7018c21STomi Valkeinen memset(&info->monspecs, 0, sizeof(info->monspecs));
1266f7018c21STomi Valkeinen
1267f7018c21STomi Valkeinen /*
1268f7018c21STomi Valkeinen * Try to (re)read EDID from hardware first
1269f7018c21STomi Valkeinen * EDID data may return, but not parse as valid
1270f7018c21STomi Valkeinen * Try again a few times, in case of e.g. analog cable noise
1271f7018c21STomi Valkeinen */
1272f7018c21STomi Valkeinen while (tries--) {
1273f7018c21STomi Valkeinen
12747ea46206SLadislav Michl i = dlfb_get_edid(dlfb, edid, EDID_LENGTH);
1275f7018c21STomi Valkeinen
1276f7018c21STomi Valkeinen if (i >= EDID_LENGTH)
1277f7018c21STomi Valkeinen fb_edid_to_monspecs(edid, &info->monspecs);
1278f7018c21STomi Valkeinen
1279f7018c21STomi Valkeinen if (info->monspecs.modedb_len > 0) {
12807ea46206SLadislav Michl dlfb->edid = edid;
12817ea46206SLadislav Michl dlfb->edid_size = i;
1282f7018c21STomi Valkeinen break;
1283f7018c21STomi Valkeinen }
1284f7018c21STomi Valkeinen }
1285f7018c21STomi Valkeinen
1286f7018c21STomi Valkeinen /* If that fails, use a previously returned EDID if available */
1287f7018c21STomi Valkeinen if (info->monspecs.modedb_len == 0) {
12885865889fSLadislav Michl dev_err(dev, "Unable to get valid EDID from device/display\n");
1289f7018c21STomi Valkeinen
12907ea46206SLadislav Michl if (dlfb->edid) {
12917ea46206SLadislav Michl fb_edid_to_monspecs(dlfb->edid, &info->monspecs);
1292f7018c21STomi Valkeinen if (info->monspecs.modedb_len > 0)
12935865889fSLadislav Michl dev_err(dev, "Using previously queried EDID\n");
1294f7018c21STomi Valkeinen }
1295f7018c21STomi Valkeinen }
1296f7018c21STomi Valkeinen
1297f7018c21STomi Valkeinen /* If that fails, use the default EDID we were handed */
1298f7018c21STomi Valkeinen if (info->monspecs.modedb_len == 0) {
1299f7018c21STomi Valkeinen if (default_edid_size >= EDID_LENGTH) {
1300f7018c21STomi Valkeinen fb_edid_to_monspecs(default_edid, &info->monspecs);
1301f7018c21STomi Valkeinen if (info->monspecs.modedb_len > 0) {
1302f7018c21STomi Valkeinen memcpy(edid, default_edid, default_edid_size);
13037ea46206SLadislav Michl dlfb->edid = edid;
13047ea46206SLadislav Michl dlfb->edid_size = default_edid_size;
13055865889fSLadislav Michl dev_err(dev, "Using default/backup EDID\n");
1306f7018c21STomi Valkeinen }
1307f7018c21STomi Valkeinen }
1308f7018c21STomi Valkeinen }
1309f7018c21STomi Valkeinen
1310f7018c21STomi Valkeinen /* If we've got modes, let's pick a best default mode */
1311f7018c21STomi Valkeinen if (info->monspecs.modedb_len > 0) {
1312f7018c21STomi Valkeinen
1313f7018c21STomi Valkeinen for (i = 0; i < info->monspecs.modedb_len; i++) {
13145865889fSLadislav Michl mode = &info->monspecs.modedb[i];
13155865889fSLadislav Michl if (dlfb_is_valid_mode(mode, dlfb)) {
13165865889fSLadislav Michl fb_add_videomode(mode, &info->modelist);
13175865889fSLadislav Michl } else {
13185865889fSLadislav Michl dev_dbg(dev, "Specified mode %dx%d too big\n",
13195865889fSLadislav Michl mode->xres, mode->yres);
1320f7018c21STomi Valkeinen if (i == 0)
1321f7018c21STomi Valkeinen /* if we've removed top/best mode */
1322f7018c21STomi Valkeinen info->monspecs.misc
1323f7018c21STomi Valkeinen &= ~FB_MISC_1ST_DETAIL;
1324f7018c21STomi Valkeinen }
1325f7018c21STomi Valkeinen }
1326f7018c21STomi Valkeinen
1327f7018c21STomi Valkeinen default_vmode = fb_find_best_display(&info->monspecs,
1328f7018c21STomi Valkeinen &info->modelist);
1329f7018c21STomi Valkeinen }
1330f7018c21STomi Valkeinen
1331f7018c21STomi Valkeinen /* If everything else has failed, fall back to safe default mode */
1332f7018c21STomi Valkeinen if (default_vmode == NULL) {
1333f7018c21STomi Valkeinen
1334f7018c21STomi Valkeinen struct fb_videomode fb_vmode = {0};
1335f7018c21STomi Valkeinen
1336f7018c21STomi Valkeinen /*
1337f7018c21STomi Valkeinen * Add the standard VESA modes to our modelist
1338f7018c21STomi Valkeinen * Since we don't have EDID, there may be modes that
1339f7018c21STomi Valkeinen * overspec monitor and/or are incorrect aspect ratio, etc.
1340f7018c21STomi Valkeinen * But at least the user has a chance to choose
1341f7018c21STomi Valkeinen */
1342f7018c21STomi Valkeinen for (i = 0; i < VESA_MODEDB_SIZE; i++) {
13435865889fSLadislav Michl mode = (struct fb_videomode *)&vesa_modes[i];
13445865889fSLadislav Michl if (dlfb_is_valid_mode(mode, dlfb))
13455865889fSLadislav Michl fb_add_videomode(mode, &info->modelist);
13465865889fSLadislav Michl else
13475865889fSLadislav Michl dev_dbg(dev, "VESA mode %dx%d too big\n",
13485865889fSLadislav Michl mode->xres, mode->yres);
1349f7018c21STomi Valkeinen }
1350f7018c21STomi Valkeinen
1351f7018c21STomi Valkeinen /*
1352f7018c21STomi Valkeinen * default to resolution safe for projectors
1353f7018c21STomi Valkeinen * (since they are most common case without EDID)
1354f7018c21STomi Valkeinen */
1355f7018c21STomi Valkeinen fb_vmode.xres = 800;
1356f7018c21STomi Valkeinen fb_vmode.yres = 600;
1357f7018c21STomi Valkeinen fb_vmode.refresh = 60;
1358f7018c21STomi Valkeinen default_vmode = fb_find_nearest_mode(&fb_vmode,
1359f7018c21STomi Valkeinen &info->modelist);
1360f7018c21STomi Valkeinen }
1361f7018c21STomi Valkeinen
1362f7018c21STomi Valkeinen /* If we have good mode and no active clients*/
13637ea46206SLadislav Michl if ((default_vmode != NULL) && (dlfb->fb_count == 0)) {
1364f7018c21STomi Valkeinen
1365f7018c21STomi Valkeinen fb_videomode_to_var(&info->var, default_vmode);
1366f7018c21STomi Valkeinen dlfb_var_color_format(&info->var);
1367f7018c21STomi Valkeinen
1368f7018c21STomi Valkeinen /*
1369f7018c21STomi Valkeinen * with mode size info, we can now alloc our framebuffer.
1370f7018c21STomi Valkeinen */
1371f7018c21STomi Valkeinen memcpy(&info->fix, &dlfb_fix, sizeof(dlfb_fix));
1372f7018c21STomi Valkeinen } else
1373f7018c21STomi Valkeinen result = -EINVAL;
1374f7018c21STomi Valkeinen
1375f7018c21STomi Valkeinen error:
13767ea46206SLadislav Michl if (edid && (dlfb->edid != edid))
1377f7018c21STomi Valkeinen kfree(edid);
1378f7018c21STomi Valkeinen
1379f7018c21STomi Valkeinen if (info->dev)
1380f7018c21STomi Valkeinen mutex_unlock(&info->lock);
1381f7018c21STomi Valkeinen
1382f7018c21STomi Valkeinen return result;
1383f7018c21STomi Valkeinen }
1384f7018c21STomi Valkeinen
metrics_bytes_rendered_show(struct device * fbdev,struct device_attribute * a,char * buf)1385f7018c21STomi Valkeinen static ssize_t metrics_bytes_rendered_show(struct device *fbdev,
1386f7018c21STomi Valkeinen struct device_attribute *a, char *buf) {
1387f7018c21STomi Valkeinen struct fb_info *fb_info = dev_get_drvdata(fbdev);
13887ea46206SLadislav Michl struct dlfb_data *dlfb = fb_info->par;
138981a99828SJing Yao return sysfs_emit(buf, "%u\n",
13907ea46206SLadislav Michl atomic_read(&dlfb->bytes_rendered));
1391f7018c21STomi Valkeinen }
1392f7018c21STomi Valkeinen
metrics_bytes_identical_show(struct device * fbdev,struct device_attribute * a,char * buf)1393f7018c21STomi Valkeinen static ssize_t metrics_bytes_identical_show(struct device *fbdev,
1394f7018c21STomi Valkeinen struct device_attribute *a, char *buf) {
1395f7018c21STomi Valkeinen struct fb_info *fb_info = dev_get_drvdata(fbdev);
13967ea46206SLadislav Michl struct dlfb_data *dlfb = fb_info->par;
139781a99828SJing Yao return sysfs_emit(buf, "%u\n",
13987ea46206SLadislav Michl atomic_read(&dlfb->bytes_identical));
1399f7018c21STomi Valkeinen }
1400f7018c21STomi Valkeinen
metrics_bytes_sent_show(struct device * fbdev,struct device_attribute * a,char * buf)1401f7018c21STomi Valkeinen static ssize_t metrics_bytes_sent_show(struct device *fbdev,
1402f7018c21STomi Valkeinen struct device_attribute *a, char *buf) {
1403f7018c21STomi Valkeinen struct fb_info *fb_info = dev_get_drvdata(fbdev);
14047ea46206SLadislav Michl struct dlfb_data *dlfb = fb_info->par;
140581a99828SJing Yao return sysfs_emit(buf, "%u\n",
14067ea46206SLadislav Michl atomic_read(&dlfb->bytes_sent));
1407f7018c21STomi Valkeinen }
1408f7018c21STomi Valkeinen
metrics_cpu_kcycles_used_show(struct device * fbdev,struct device_attribute * a,char * buf)1409f7018c21STomi Valkeinen static ssize_t metrics_cpu_kcycles_used_show(struct device *fbdev,
1410f7018c21STomi Valkeinen struct device_attribute *a, char *buf) {
1411f7018c21STomi Valkeinen struct fb_info *fb_info = dev_get_drvdata(fbdev);
14127ea46206SLadislav Michl struct dlfb_data *dlfb = fb_info->par;
141381a99828SJing Yao return sysfs_emit(buf, "%u\n",
14147ea46206SLadislav Michl atomic_read(&dlfb->cpu_kcycles_used));
1415f7018c21STomi Valkeinen }
1416f7018c21STomi Valkeinen
edid_show(struct file * filp,struct kobject * kobj,struct bin_attribute * a,char * buf,loff_t off,size_t count)1417f7018c21STomi Valkeinen static ssize_t edid_show(
1418f7018c21STomi Valkeinen struct file *filp,
1419f7018c21STomi Valkeinen struct kobject *kobj, struct bin_attribute *a,
1420f7018c21STomi Valkeinen char *buf, loff_t off, size_t count) {
14216bfea83eSkernel test robot struct device *fbdev = kobj_to_dev(kobj);
1422f7018c21STomi Valkeinen struct fb_info *fb_info = dev_get_drvdata(fbdev);
14237ea46206SLadislav Michl struct dlfb_data *dlfb = fb_info->par;
1424f7018c21STomi Valkeinen
14257ea46206SLadislav Michl if (dlfb->edid == NULL)
1426f7018c21STomi Valkeinen return 0;
1427f7018c21STomi Valkeinen
14287ea46206SLadislav Michl if ((off >= dlfb->edid_size) || (count > dlfb->edid_size))
1429f7018c21STomi Valkeinen return 0;
1430f7018c21STomi Valkeinen
14317ea46206SLadislav Michl if (off + count > dlfb->edid_size)
14327ea46206SLadislav Michl count = dlfb->edid_size - off;
1433f7018c21STomi Valkeinen
14347ea46206SLadislav Michl memcpy(buf, dlfb->edid, count);
1435f7018c21STomi Valkeinen
1436f7018c21STomi Valkeinen return count;
1437f7018c21STomi Valkeinen }
1438f7018c21STomi Valkeinen
edid_store(struct file * filp,struct kobject * kobj,struct bin_attribute * a,char * src,loff_t src_off,size_t src_size)1439f7018c21STomi Valkeinen static ssize_t edid_store(
1440f7018c21STomi Valkeinen struct file *filp,
1441f7018c21STomi Valkeinen struct kobject *kobj, struct bin_attribute *a,
1442f7018c21STomi Valkeinen char *src, loff_t src_off, size_t src_size) {
14436bfea83eSkernel test robot struct device *fbdev = kobj_to_dev(kobj);
1444f7018c21STomi Valkeinen struct fb_info *fb_info = dev_get_drvdata(fbdev);
14457ea46206SLadislav Michl struct dlfb_data *dlfb = fb_info->par;
1446f7018c21STomi Valkeinen int ret;
1447f7018c21STomi Valkeinen
1448f7018c21STomi Valkeinen /* We only support write of entire EDID at once, no offset*/
1449f7018c21STomi Valkeinen if ((src_size != EDID_LENGTH) || (src_off != 0))
1450f7018c21STomi Valkeinen return -EINVAL;
1451f7018c21STomi Valkeinen
14527ea46206SLadislav Michl ret = dlfb_setup_modes(dlfb, fb_info, src, src_size);
1453f7018c21STomi Valkeinen if (ret)
1454f7018c21STomi Valkeinen return ret;
1455f7018c21STomi Valkeinen
14567ea46206SLadislav Michl if (!dlfb->edid || memcmp(src, dlfb->edid, src_size))
1457f7018c21STomi Valkeinen return -EINVAL;
1458f7018c21STomi Valkeinen
14597433914eSMikulas Patocka ret = dlfb_ops_set_par(fb_info);
14607433914eSMikulas Patocka if (ret)
14617433914eSMikulas Patocka return ret;
14627433914eSMikulas Patocka
1463f7018c21STomi Valkeinen return src_size;
1464f7018c21STomi Valkeinen }
1465f7018c21STomi Valkeinen
metrics_reset_store(struct device * fbdev,struct device_attribute * attr,const char * buf,size_t count)1466f7018c21STomi Valkeinen static ssize_t metrics_reset_store(struct device *fbdev,
1467f7018c21STomi Valkeinen struct device_attribute *attr,
1468f7018c21STomi Valkeinen const char *buf, size_t count)
1469f7018c21STomi Valkeinen {
1470f7018c21STomi Valkeinen struct fb_info *fb_info = dev_get_drvdata(fbdev);
14717ea46206SLadislav Michl struct dlfb_data *dlfb = fb_info->par;
1472f7018c21STomi Valkeinen
14737ea46206SLadislav Michl atomic_set(&dlfb->bytes_rendered, 0);
14747ea46206SLadislav Michl atomic_set(&dlfb->bytes_identical, 0);
14757ea46206SLadislav Michl atomic_set(&dlfb->bytes_sent, 0);
14767ea46206SLadislav Michl atomic_set(&dlfb->cpu_kcycles_used, 0);
1477f7018c21STomi Valkeinen
1478f7018c21STomi Valkeinen return count;
1479f7018c21STomi Valkeinen }
1480f7018c21STomi Valkeinen
1481598b2eedSBhumika Goyal static const struct bin_attribute edid_attr = {
1482f7018c21STomi Valkeinen .attr.name = "edid",
1483f7018c21STomi Valkeinen .attr.mode = 0666,
1484f7018c21STomi Valkeinen .size = EDID_LENGTH,
1485f7018c21STomi Valkeinen .read = edid_show,
1486f7018c21STomi Valkeinen .write = edid_store
1487f7018c21STomi Valkeinen };
1488f7018c21STomi Valkeinen
1489fa738a5cSLadislav Michl static const struct device_attribute fb_device_attrs[] = {
1490f7018c21STomi Valkeinen __ATTR_RO(metrics_bytes_rendered),
1491f7018c21STomi Valkeinen __ATTR_RO(metrics_bytes_identical),
1492f7018c21STomi Valkeinen __ATTR_RO(metrics_bytes_sent),
1493f7018c21STomi Valkeinen __ATTR_RO(metrics_cpu_kcycles_used),
1494f7018c21STomi Valkeinen __ATTR(metrics_reset, S_IWUSR, NULL, metrics_reset_store),
1495f7018c21STomi Valkeinen };
1496f7018c21STomi Valkeinen
1497f7018c21STomi Valkeinen /*
1498f7018c21STomi Valkeinen * This is necessary before we can communicate with the display controller.
1499f7018c21STomi Valkeinen */
dlfb_select_std_channel(struct dlfb_data * dlfb)15007ea46206SLadislav Michl static int dlfb_select_std_channel(struct dlfb_data *dlfb)
1501f7018c21STomi Valkeinen {
1502f7018c21STomi Valkeinen int ret;
150345f580c4SMaksim Salau static const u8 set_def_chn[] = {
150445f580c4SMaksim Salau 0x57, 0xCD, 0xDC, 0xA7,
1505f7018c21STomi Valkeinen 0x1C, 0x88, 0x5E, 0x15,
1506f7018c21STomi Valkeinen 0x60, 0xFE, 0xC6, 0x97,
1507f7018c21STomi Valkeinen 0x16, 0x3D, 0x47, 0xF2 };
1508f7018c21STomi Valkeinen
1509537adba4SHelge Deller ret = usb_control_msg_send(dlfb->udev, 0, NR_USB_REQUEST_CHANNEL,
1510f7018c21STomi Valkeinen (USB_DIR_OUT | USB_TYPE_VENDOR), 0, 0,
1511537adba4SHelge Deller &set_def_chn, sizeof(set_def_chn), USB_CTRL_SET_TIMEOUT,
1512537adba4SHelge Deller GFP_KERNEL);
151345f580c4SMaksim Salau
1514f7018c21STomi Valkeinen return ret;
1515f7018c21STomi Valkeinen }
1516f7018c21STomi Valkeinen
dlfb_parse_vendor_descriptor(struct dlfb_data * dlfb,struct usb_interface * intf)15177ea46206SLadislav Michl static int dlfb_parse_vendor_descriptor(struct dlfb_data *dlfb,
15185865889fSLadislav Michl struct usb_interface *intf)
1519f7018c21STomi Valkeinen {
1520f7018c21STomi Valkeinen char *desc;
1521f7018c21STomi Valkeinen char *buf;
1522f7018c21STomi Valkeinen char *desc_end;
1523f63cb8d7SAlexey Klimov int total_len;
1524f7018c21STomi Valkeinen
1525f7018c21STomi Valkeinen buf = kzalloc(MAX_VENDOR_DESCRIPTOR_SIZE, GFP_KERNEL);
1526f7018c21STomi Valkeinen if (!buf)
1527f7018c21STomi Valkeinen return false;
1528f7018c21STomi Valkeinen desc = buf;
1529f7018c21STomi Valkeinen
15305865889fSLadislav Michl total_len = usb_get_descriptor(interface_to_usbdev(intf),
1531f7018c21STomi Valkeinen 0x5f, /* vendor specific */
1532f7018c21STomi Valkeinen 0, desc, MAX_VENDOR_DESCRIPTOR_SIZE);
1533f7018c21STomi Valkeinen
1534f7018c21STomi Valkeinen /* if not found, look in configuration descriptor */
1535f7018c21STomi Valkeinen if (total_len < 0) {
15365865889fSLadislav Michl if (0 == usb_get_extra_descriptor(intf->cur_altsetting,
1537f7018c21STomi Valkeinen 0x5f, &desc))
1538f7018c21STomi Valkeinen total_len = (int) desc[0];
1539f7018c21STomi Valkeinen }
1540f7018c21STomi Valkeinen
1541f7018c21STomi Valkeinen if (total_len > 5) {
15425865889fSLadislav Michl dev_info(&intf->dev,
15435865889fSLadislav Michl "vendor descriptor length: %d data: %11ph\n",
15445865889fSLadislav Michl total_len, desc);
1545f7018c21STomi Valkeinen
1546f7018c21STomi Valkeinen if ((desc[0] != total_len) || /* descriptor length */
1547f7018c21STomi Valkeinen (desc[1] != 0x5f) || /* vendor descriptor type */
1548f7018c21STomi Valkeinen (desc[2] != 0x01) || /* version (2 bytes) */
1549f7018c21STomi Valkeinen (desc[3] != 0x00) ||
1550f7018c21STomi Valkeinen (desc[4] != total_len - 2)) /* length after type */
1551f7018c21STomi Valkeinen goto unrecognized;
1552f7018c21STomi Valkeinen
1553f7018c21STomi Valkeinen desc_end = desc + total_len;
1554f7018c21STomi Valkeinen desc += 5; /* the fixed header we've already parsed */
1555f7018c21STomi Valkeinen
1556f7018c21STomi Valkeinen while (desc < desc_end) {
1557f7018c21STomi Valkeinen u8 length;
1558f7018c21STomi Valkeinen u16 key;
1559f7018c21STomi Valkeinen
1560115e7759SLadislav Michl key = *desc++;
1561115e7759SLadislav Michl key |= (u16)*desc++ << 8;
1562115e7759SLadislav Michl length = *desc++;
1563f7018c21STomi Valkeinen
1564f7018c21STomi Valkeinen switch (key) {
1565f7018c21STomi Valkeinen case 0x0200: { /* max_area */
1566115e7759SLadislav Michl u32 max_area = *desc++;
1567115e7759SLadislav Michl max_area |= (u32)*desc++ << 8;
1568115e7759SLadislav Michl max_area |= (u32)*desc++ << 16;
1569115e7759SLadislav Michl max_area |= (u32)*desc++ << 24;
15705865889fSLadislav Michl dev_warn(&intf->dev,
15715865889fSLadislav Michl "DL chip limited to %d pixel modes\n",
1572f7018c21STomi Valkeinen max_area);
15737ea46206SLadislav Michl dlfb->sku_pixel_limit = max_area;
1574f7018c21STomi Valkeinen break;
1575f7018c21STomi Valkeinen }
1576f7018c21STomi Valkeinen default:
1577f7018c21STomi Valkeinen break;
1578f7018c21STomi Valkeinen }
1579f7018c21STomi Valkeinen desc += length;
1580f7018c21STomi Valkeinen }
1581f7018c21STomi Valkeinen } else {
15825865889fSLadislav Michl dev_info(&intf->dev, "vendor descriptor not available (%d)\n",
15835865889fSLadislav Michl total_len);
1584f7018c21STomi Valkeinen }
1585f7018c21STomi Valkeinen
1586f7018c21STomi Valkeinen goto success;
1587f7018c21STomi Valkeinen
1588f7018c21STomi Valkeinen unrecognized:
1589f7018c21STomi Valkeinen /* allow udlfb to load for now even if firmware unrecognized */
15905865889fSLadislav Michl dev_err(&intf->dev, "Unrecognized vendor firmware descriptor\n");
1591f7018c21STomi Valkeinen
1592f7018c21STomi Valkeinen success:
1593f7018c21STomi Valkeinen kfree(buf);
1594f7018c21STomi Valkeinen return true;
1595f7018c21STomi Valkeinen }
1596f7018c21STomi Valkeinen
dlfb_usb_probe(struct usb_interface * intf,const struct usb_device_id * id)15975865889fSLadislav Michl static int dlfb_usb_probe(struct usb_interface *intf,
1598f7018c21STomi Valkeinen const struct usb_device_id *id)
1599f7018c21STomi Valkeinen {
160068a958a9SMikulas Patocka int i;
160168a958a9SMikulas Patocka const struct device_attribute *attr;
16027ea46206SLadislav Michl struct dlfb_data *dlfb;
160368a958a9SMikulas Patocka struct fb_info *info;
1604aaf7dbe0SPavel Skripkin int retval;
16055865889fSLadislav Michl struct usb_device *usbdev = interface_to_usbdev(intf);
1606ed9de4edSAlan Stern static u8 out_ep[] = {OUT_EP_NUM + USB_DIR_OUT, 0};
1607f7018c21STomi Valkeinen
1608f7018c21STomi Valkeinen /* usb initialization */
16097ea46206SLadislav Michl dlfb = kzalloc(sizeof(*dlfb), GFP_KERNEL);
16105865889fSLadislav Michl if (!dlfb) {
16115865889fSLadislav Michl dev_err(&intf->dev, "%s: failed to allocate dlfb\n", __func__);
1612c143a559SDan Carpenter return -ENOMEM;
1613f7018c21STomi Valkeinen }
1614f7018c21STomi Valkeinen
16157433914eSMikulas Patocka INIT_LIST_HEAD(&dlfb->deferred_free);
1616f7018c21STomi Valkeinen
161768a958a9SMikulas Patocka dlfb->udev = usb_get_dev(usbdev);
16185865889fSLadislav Michl usb_set_intfdata(intf, dlfb);
1619f7018c21STomi Valkeinen
1620ed9de4edSAlan Stern if (!usb_check_bulk_endpoints(intf, out_ep)) {
1621ed9de4edSAlan Stern dev_err(&intf->dev, "Invalid DisplayLink device!\n");
1622ed9de4edSAlan Stern retval = -EINVAL;
1623aaf7dbe0SPavel Skripkin goto error;
1624aaf7dbe0SPavel Skripkin }
1625aaf7dbe0SPavel Skripkin
16265865889fSLadislav Michl dev_dbg(&intf->dev, "console enable=%d\n", console);
16275865889fSLadislav Michl dev_dbg(&intf->dev, "fb_defio enable=%d\n", fb_defio);
16285865889fSLadislav Michl dev_dbg(&intf->dev, "shadow enable=%d\n", shadow);
1629f7018c21STomi Valkeinen
16307ea46206SLadislav Michl dlfb->sku_pixel_limit = 2048 * 1152; /* default to maximum */
1631f7018c21STomi Valkeinen
16325865889fSLadislav Michl if (!dlfb_parse_vendor_descriptor(dlfb, intf)) {
16335865889fSLadislav Michl dev_err(&intf->dev,
16345865889fSLadislav Michl "firmware not recognized, incompatible device?\n");
1635aaf7dbe0SPavel Skripkin retval = -ENODEV;
1636f7018c21STomi Valkeinen goto error;
1637f7018c21STomi Valkeinen }
1638f7018c21STomi Valkeinen
1639f7018c21STomi Valkeinen if (pixel_limit) {
16405865889fSLadislav Michl dev_warn(&intf->dev,
16415865889fSLadislav Michl "DL chip limit of %d overridden to %d\n",
16427ea46206SLadislav Michl dlfb->sku_pixel_limit, pixel_limit);
16437ea46206SLadislav Michl dlfb->sku_pixel_limit = pixel_limit;
1644f7018c21STomi Valkeinen }
1645f7018c21STomi Valkeinen
1646f7018c21STomi Valkeinen
1647f7018c21STomi Valkeinen /* allocates framebuffer driver structure, not framebuffer memory */
16487ea46206SLadislav Michl info = framebuffer_alloc(0, &dlfb->udev->dev);
1649aaf7dbe0SPavel Skripkin if (!info) {
1650aaf7dbe0SPavel Skripkin retval = -ENOMEM;
1651f7018c21STomi Valkeinen goto error;
1652aaf7dbe0SPavel Skripkin }
1653f7018c21STomi Valkeinen
16547ea46206SLadislav Michl dlfb->info = info;
16557ea46206SLadislav Michl info->par = dlfb;
16567ea46206SLadislav Michl info->pseudo_palette = dlfb->pseudo_palette;
16572c29cfc3SMikulas Patocka dlfb->ops = dlfb_ops;
16582c29cfc3SMikulas Patocka info->fbops = &dlfb->ops;
1659f7018c21STomi Valkeinen
1660babc250eSMikulas Patocka mutex_init(&dlfb->render_mutex);
16616b11f9d8SMikulas Patocka dlfb_init_damage(dlfb);
16626b11f9d8SMikulas Patocka spin_lock_init(&dlfb->damage_lock);
16636b11f9d8SMikulas Patocka INIT_WORK(&dlfb->damage_work, dlfb_damage_work);
16646b11f9d8SMikulas Patocka
166568a958a9SMikulas Patocka INIT_LIST_HEAD(&info->modelist);
166668a958a9SMikulas Patocka
166768a958a9SMikulas Patocka if (!dlfb_alloc_urb_list(dlfb, WRITES_IN_FLIGHT, MAX_TRANSFER)) {
166868a958a9SMikulas Patocka retval = -ENOMEM;
166968a958a9SMikulas Patocka dev_err(&intf->dev, "unable to allocate urb list\n");
167068a958a9SMikulas Patocka goto error;
167168a958a9SMikulas Patocka }
167268a958a9SMikulas Patocka
167368a958a9SMikulas Patocka /* We don't register a new USB class. Our client interface is dlfbev */
167468a958a9SMikulas Patocka
1675f7018c21STomi Valkeinen retval = fb_alloc_cmap(&info->cmap, 256, 0);
1676f7018c21STomi Valkeinen if (retval < 0) {
16775865889fSLadislav Michl dev_err(info->device, "cmap allocation failed: %d\n", retval);
1678f7018c21STomi Valkeinen goto error;
1679f7018c21STomi Valkeinen }
1680f7018c21STomi Valkeinen
16817ea46206SLadislav Michl retval = dlfb_setup_modes(dlfb, info, NULL, 0);
1682f7018c21STomi Valkeinen if (retval != 0) {
16835865889fSLadislav Michl dev_err(info->device,
16845865889fSLadislav Michl "unable to find common mode for display and adapter\n");
1685f7018c21STomi Valkeinen goto error;
1686f7018c21STomi Valkeinen }
1687f7018c21STomi Valkeinen
1688f7018c21STomi Valkeinen /* ready to begin using device */
1689f7018c21STomi Valkeinen
16907ea46206SLadislav Michl atomic_set(&dlfb->usb_active, 1);
16917ea46206SLadislav Michl dlfb_select_std_channel(dlfb);
1692f7018c21STomi Valkeinen
1693f7018c21STomi Valkeinen dlfb_ops_check_var(&info->var, info);
16947433914eSMikulas Patocka retval = dlfb_ops_set_par(info);
16957433914eSMikulas Patocka if (retval)
16967433914eSMikulas Patocka goto error;
1697f7018c21STomi Valkeinen
1698f7018c21STomi Valkeinen retval = register_framebuffer(info);
1699f7018c21STomi Valkeinen if (retval < 0) {
17005865889fSLadislav Michl dev_err(info->device, "unable to register framebuffer: %d\n",
17015865889fSLadislav Michl retval);
1702f7018c21STomi Valkeinen goto error;
1703f7018c21STomi Valkeinen }
1704f7018c21STomi Valkeinen
1705f7018c21STomi Valkeinen for (i = 0; i < ARRAY_SIZE(fb_device_attrs); i++) {
17065865889fSLadislav Michl attr = &fb_device_attrs[i];
17075865889fSLadislav Michl retval = device_create_file(info->dev, attr);
17085865889fSLadislav Michl if (retval)
17095865889fSLadislav Michl dev_warn(info->device,
17105865889fSLadislav Michl "failed to create '%s' attribute: %d\n",
17115865889fSLadislav Michl attr->attr.name, retval);
1712f7018c21STomi Valkeinen }
1713f7018c21STomi Valkeinen
1714f7018c21STomi Valkeinen retval = device_create_bin_file(info->dev, &edid_attr);
17155865889fSLadislav Michl if (retval)
17165865889fSLadislav Michl dev_warn(info->device, "failed to create '%s' attribute: %d\n",
17175865889fSLadislav Michl edid_attr.attr.name, retval);
1718f7018c21STomi Valkeinen
17195865889fSLadislav Michl dev_info(info->device,
17205865889fSLadislav Michl "%s is DisplayLink USB device (%dx%d, %dK framebuffer memory)\n",
17215865889fSLadislav Michl dev_name(info->dev), info->var.xres, info->var.yres,
17227ea46206SLadislav Michl ((dlfb->backing_buffer) ?
1723f7018c21STomi Valkeinen info->fix.smem_len * 2 : info->fix.smem_len) >> 10);
172468a958a9SMikulas Patocka return 0;
1725f7018c21STomi Valkeinen
1726f7018c21STomi Valkeinen error:
172768a958a9SMikulas Patocka if (dlfb->info) {
172868a958a9SMikulas Patocka dlfb_ops_destroy(dlfb->info);
1729c143a559SDan Carpenter } else {
173068a958a9SMikulas Patocka usb_put_dev(dlfb->udev);
173168a958a9SMikulas Patocka kfree(dlfb);
173268a958a9SMikulas Patocka }
173368a958a9SMikulas Patocka return retval;
1734f7018c21STomi Valkeinen }
1735f7018c21STomi Valkeinen
dlfb_usb_disconnect(struct usb_interface * intf)17365865889fSLadislav Michl static void dlfb_usb_disconnect(struct usb_interface *intf)
1737f7018c21STomi Valkeinen {
17387ea46206SLadislav Michl struct dlfb_data *dlfb;
1739f7018c21STomi Valkeinen struct fb_info *info;
1740f7018c21STomi Valkeinen int i;
1741f7018c21STomi Valkeinen
17425865889fSLadislav Michl dlfb = usb_get_intfdata(intf);
17437ea46206SLadislav Michl info = dlfb->info;
1744f7018c21STomi Valkeinen
17455865889fSLadislav Michl dev_dbg(&intf->dev, "USB disconnect starting\n");
1746f7018c21STomi Valkeinen
1747f7018c21STomi Valkeinen /* we virtualize until all fb clients release. Then we free */
17487ea46206SLadislav Michl dlfb->virtualized = true;
1749f7018c21STomi Valkeinen
1750f7018c21STomi Valkeinen /* When non-active we'll update virtual framebuffer, but no new urbs */
17517ea46206SLadislav Michl atomic_set(&dlfb->usb_active, 0);
1752f7018c21STomi Valkeinen
1753f7018c21STomi Valkeinen /* this function will wait for all in-flight urbs to complete */
17547ea46206SLadislav Michl dlfb_free_urb_list(dlfb);
1755f7018c21STomi Valkeinen
1756f7018c21STomi Valkeinen /* remove udlfb's sysfs interfaces */
1757f7018c21STomi Valkeinen for (i = 0; i < ARRAY_SIZE(fb_device_attrs); i++)
1758f7018c21STomi Valkeinen device_remove_file(info->dev, &fb_device_attrs[i]);
1759f7018c21STomi Valkeinen device_remove_bin_file(info->dev, &edid_attr);
1760f7018c21STomi Valkeinen
176168a958a9SMikulas Patocka unregister_framebuffer(info);
1762f7018c21STomi Valkeinen }
1763f7018c21STomi Valkeinen
1764f7018c21STomi Valkeinen static struct usb_driver dlfb_driver = {
1765f7018c21STomi Valkeinen .name = "udlfb",
1766f7018c21STomi Valkeinen .probe = dlfb_usb_probe,
1767f7018c21STomi Valkeinen .disconnect = dlfb_usb_disconnect,
1768f7018c21STomi Valkeinen .id_table = id_table,
1769f7018c21STomi Valkeinen };
1770f7018c21STomi Valkeinen
1771f7018c21STomi Valkeinen module_usb_driver(dlfb_driver);
1772f7018c21STomi Valkeinen
dlfb_urb_completion(struct urb * urb)1773f7018c21STomi Valkeinen static void dlfb_urb_completion(struct urb *urb)
1774f7018c21STomi Valkeinen {
1775f7018c21STomi Valkeinen struct urb_node *unode = urb->context;
17767ea46206SLadislav Michl struct dlfb_data *dlfb = unode->dlfb;
1777f7018c21STomi Valkeinen unsigned long flags;
1778f7018c21STomi Valkeinen
17795865889fSLadislav Michl switch (urb->status) {
17805865889fSLadislav Michl case 0:
17815865889fSLadislav Michl /* success */
17825865889fSLadislav Michl break;
17835865889fSLadislav Michl case -ECONNRESET:
17845865889fSLadislav Michl case -ENOENT:
17855865889fSLadislav Michl case -ESHUTDOWN:
1786f7018c21STomi Valkeinen /* sync/async unlink faults aren't errors */
17875865889fSLadislav Michl break;
17885865889fSLadislav Michl default:
17895865889fSLadislav Michl dev_err(&dlfb->udev->dev,
17905865889fSLadislav Michl "%s - nonzero write bulk status received: %d\n",
1791f7018c21STomi Valkeinen __func__, urb->status);
17927ea46206SLadislav Michl atomic_set(&dlfb->lost_pixels, 1);
17935865889fSLadislav Michl break;
1794f7018c21STomi Valkeinen }
1795f7018c21STomi Valkeinen
17967ea46206SLadislav Michl urb->transfer_buffer_length = dlfb->urbs.size; /* reset to actual */
1797f7018c21STomi Valkeinen
17987ea46206SLadislav Michl spin_lock_irqsave(&dlfb->urbs.lock, flags);
17997ea46206SLadislav Michl list_add_tail(&unode->entry, &dlfb->urbs.list);
18007ea46206SLadislav Michl dlfb->urbs.available++;
18017ea46206SLadislav Michl spin_unlock_irqrestore(&dlfb->urbs.lock, flags);
1802f7018c21STomi Valkeinen
18037ea46206SLadislav Michl up(&dlfb->urbs.limit_sem);
1804f7018c21STomi Valkeinen }
1805f7018c21STomi Valkeinen
dlfb_free_urb_list(struct dlfb_data * dlfb)18067ea46206SLadislav Michl static void dlfb_free_urb_list(struct dlfb_data *dlfb)
1807f7018c21STomi Valkeinen {
18087ea46206SLadislav Michl int count = dlfb->urbs.count;
1809f7018c21STomi Valkeinen struct list_head *node;
1810f7018c21STomi Valkeinen struct urb_node *unode;
1811f7018c21STomi Valkeinen struct urb *urb;
1812f7018c21STomi Valkeinen
1813f7018c21STomi Valkeinen /* keep waiting and freeing, until we've got 'em all */
1814f7018c21STomi Valkeinen while (count--) {
18159d0aa601SMikulas Patocka down(&dlfb->urbs.limit_sem);
1816f7018c21STomi Valkeinen
1817cb782a3fSMikulas Patocka spin_lock_irq(&dlfb->urbs.lock);
1818f7018c21STomi Valkeinen
18197ea46206SLadislav Michl node = dlfb->urbs.list.next; /* have reserved one with sem */
1820f7018c21STomi Valkeinen list_del_init(node);
1821f7018c21STomi Valkeinen
1822cb782a3fSMikulas Patocka spin_unlock_irq(&dlfb->urbs.lock);
1823f7018c21STomi Valkeinen
1824f7018c21STomi Valkeinen unode = list_entry(node, struct urb_node, entry);
1825f7018c21STomi Valkeinen urb = unode->urb;
1826f7018c21STomi Valkeinen
1827f7018c21STomi Valkeinen /* Free each separately allocated piece */
18287ea46206SLadislav Michl usb_free_coherent(urb->dev, dlfb->urbs.size,
1829f7018c21STomi Valkeinen urb->transfer_buffer, urb->transfer_dma);
1830f7018c21STomi Valkeinen usb_free_urb(urb);
1831f7018c21STomi Valkeinen kfree(node);
1832f7018c21STomi Valkeinen }
1833f7018c21STomi Valkeinen
18347ea46206SLadislav Michl dlfb->urbs.count = 0;
1835f7018c21STomi Valkeinen }
1836f7018c21STomi Valkeinen
dlfb_alloc_urb_list(struct dlfb_data * dlfb,int count,size_t size)18377ea46206SLadislav Michl static int dlfb_alloc_urb_list(struct dlfb_data *dlfb, int count, size_t size)
1838f7018c21STomi Valkeinen {
1839f7018c21STomi Valkeinen struct urb *urb;
1840f7018c21STomi Valkeinen struct urb_node *unode;
1841f7018c21STomi Valkeinen char *buf;
1842080fb524SMikulas Patocka size_t wanted_size = count * size;
1843f7018c21STomi Valkeinen
18447ea46206SLadislav Michl spin_lock_init(&dlfb->urbs.lock);
1845f7018c21STomi Valkeinen
1846080fb524SMikulas Patocka retry:
18477ea46206SLadislav Michl dlfb->urbs.size = size;
18487ea46206SLadislav Michl INIT_LIST_HEAD(&dlfb->urbs.list);
1849f7018c21STomi Valkeinen
1850080fb524SMikulas Patocka sema_init(&dlfb->urbs.limit_sem, 0);
1851080fb524SMikulas Patocka dlfb->urbs.count = 0;
1852080fb524SMikulas Patocka dlfb->urbs.available = 0;
1853080fb524SMikulas Patocka
1854080fb524SMikulas Patocka while (dlfb->urbs.count * size < wanted_size) {
185574fb2519SMarkus Elfring unode = kzalloc(sizeof(*unode), GFP_KERNEL);
1856f7018c21STomi Valkeinen if (!unode)
1857f7018c21STomi Valkeinen break;
18587ea46206SLadislav Michl unode->dlfb = dlfb;
1859f7018c21STomi Valkeinen
1860f7018c21STomi Valkeinen urb = usb_alloc_urb(0, GFP_KERNEL);
1861f7018c21STomi Valkeinen if (!urb) {
1862f7018c21STomi Valkeinen kfree(unode);
1863f7018c21STomi Valkeinen break;
1864f7018c21STomi Valkeinen }
1865f7018c21STomi Valkeinen unode->urb = urb;
1866f7018c21STomi Valkeinen
1867080fb524SMikulas Patocka buf = usb_alloc_coherent(dlfb->udev, size, GFP_KERNEL,
1868f7018c21STomi Valkeinen &urb->transfer_dma);
1869f7018c21STomi Valkeinen if (!buf) {
1870f7018c21STomi Valkeinen kfree(unode);
1871f7018c21STomi Valkeinen usb_free_urb(urb);
1872080fb524SMikulas Patocka if (size > PAGE_SIZE) {
1873080fb524SMikulas Patocka size /= 2;
1874080fb524SMikulas Patocka dlfb_free_urb_list(dlfb);
1875080fb524SMikulas Patocka goto retry;
1876080fb524SMikulas Patocka }
1877f7018c21STomi Valkeinen break;
1878f7018c21STomi Valkeinen }
1879f7018c21STomi Valkeinen
1880f7018c21STomi Valkeinen /* urb->transfer_buffer_length set to actual before submit */
1881ed9de4edSAlan Stern usb_fill_bulk_urb(urb, dlfb->udev,
1882ed9de4edSAlan Stern usb_sndbulkpipe(dlfb->udev, OUT_EP_NUM),
1883f7018c21STomi Valkeinen buf, size, dlfb_urb_completion, unode);
1884f7018c21STomi Valkeinen urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1885f7018c21STomi Valkeinen
18867ea46206SLadislav Michl list_add_tail(&unode->entry, &dlfb->urbs.list);
1887f7018c21STomi Valkeinen
1888080fb524SMikulas Patocka up(&dlfb->urbs.limit_sem);
1889080fb524SMikulas Patocka dlfb->urbs.count++;
1890080fb524SMikulas Patocka dlfb->urbs.available++;
1891f7018c21STomi Valkeinen }
1892f7018c21STomi Valkeinen
1893080fb524SMikulas Patocka return dlfb->urbs.count;
1894f7018c21STomi Valkeinen }
1895f7018c21STomi Valkeinen
dlfb_get_urb(struct dlfb_data * dlfb)18967ea46206SLadislav Michl static struct urb *dlfb_get_urb(struct dlfb_data *dlfb)
1897f7018c21STomi Valkeinen {
1898f63cb8d7SAlexey Klimov int ret;
1899f7018c21STomi Valkeinen struct list_head *entry;
1900f7018c21STomi Valkeinen struct urb_node *unode;
1901f7018c21STomi Valkeinen
1902f7018c21STomi Valkeinen /* Wait for an in-flight buffer to complete and get re-queued */
19037ea46206SLadislav Michl ret = down_timeout(&dlfb->urbs.limit_sem, GET_URB_TIMEOUT);
1904f7018c21STomi Valkeinen if (ret) {
19057ea46206SLadislav Michl atomic_set(&dlfb->lost_pixels, 1);
19065865889fSLadislav Michl dev_warn(&dlfb->udev->dev,
19075865889fSLadislav Michl "wait for urb interrupted: %d available: %d\n",
19087ea46206SLadislav Michl ret, dlfb->urbs.available);
1909acea8d5fSLadislav Michl return NULL;
1910f7018c21STomi Valkeinen }
1911f7018c21STomi Valkeinen
1912cb782a3fSMikulas Patocka spin_lock_irq(&dlfb->urbs.lock);
1913f7018c21STomi Valkeinen
19147ea46206SLadislav Michl BUG_ON(list_empty(&dlfb->urbs.list)); /* reserved one with limit_sem */
19157ea46206SLadislav Michl entry = dlfb->urbs.list.next;
1916f7018c21STomi Valkeinen list_del_init(entry);
19177ea46206SLadislav Michl dlfb->urbs.available--;
1918f7018c21STomi Valkeinen
1919cb782a3fSMikulas Patocka spin_unlock_irq(&dlfb->urbs.lock);
1920f7018c21STomi Valkeinen
1921f7018c21STomi Valkeinen unode = list_entry(entry, struct urb_node, entry);
1922acea8d5fSLadislav Michl return unode->urb;
1923f7018c21STomi Valkeinen }
1924f7018c21STomi Valkeinen
dlfb_submit_urb(struct dlfb_data * dlfb,struct urb * urb,size_t len)19257ea46206SLadislav Michl static int dlfb_submit_urb(struct dlfb_data *dlfb, struct urb *urb, size_t len)
1926f7018c21STomi Valkeinen {
1927f7018c21STomi Valkeinen int ret;
1928f7018c21STomi Valkeinen
19297ea46206SLadislav Michl BUG_ON(len > dlfb->urbs.size);
1930f7018c21STomi Valkeinen
1931f7018c21STomi Valkeinen urb->transfer_buffer_length = len; /* set to actual payload len */
1932f7018c21STomi Valkeinen ret = usb_submit_urb(urb, GFP_KERNEL);
1933f7018c21STomi Valkeinen if (ret) {
1934f7018c21STomi Valkeinen dlfb_urb_completion(urb); /* because no one else will */
19357ea46206SLadislav Michl atomic_set(&dlfb->lost_pixels, 1);
19365865889fSLadislav Michl dev_err(&dlfb->udev->dev, "submit urb error: %d\n", ret);
1937f7018c21STomi Valkeinen }
1938f7018c21STomi Valkeinen return ret;
1939f7018c21STomi Valkeinen }
1940f7018c21STomi Valkeinen
1941f7018c21STomi Valkeinen module_param(console, bool, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
1942f7018c21STomi Valkeinen MODULE_PARM_DESC(console, "Allow fbcon to open framebuffer");
1943f7018c21STomi Valkeinen
1944f7018c21STomi Valkeinen module_param(fb_defio, bool, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
1945f7018c21STomi Valkeinen MODULE_PARM_DESC(fb_defio, "Page fault detection of mmap writes");
1946f7018c21STomi Valkeinen
1947f7018c21STomi Valkeinen module_param(shadow, bool, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
1948f7018c21STomi Valkeinen MODULE_PARM_DESC(shadow, "Shadow vid mem. Disable to save mem but lose perf");
1949f7018c21STomi Valkeinen
1950f7018c21STomi Valkeinen module_param(pixel_limit, int, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
1951f7018c21STomi Valkeinen MODULE_PARM_DESC(pixel_limit, "Force limit on max mode (in x*y pixels)");
1952f7018c21STomi Valkeinen
1953f7018c21STomi Valkeinen MODULE_AUTHOR("Roberto De Ioris <roberto@unbit.it>, "
1954f7018c21STomi Valkeinen "Jaya Kumar <jayakumar.lkml@gmail.com>, "
1955f7018c21STomi Valkeinen "Bernie Thompson <bernie@plugable.com>");
1956f7018c21STomi Valkeinen MODULE_DESCRIPTION("DisplayLink kernel framebuffer driver");
1957f7018c21STomi Valkeinen MODULE_LICENSE("GPL");
1958f7018c21STomi Valkeinen
1959