Blame view

drivers/video/udlfb.c 50.6 KB
59277b679   Bernie Thompson   Staging: udlfb: a...
1
2
3
4
5
  /*
   * udlfb.c -- Framebuffer driver for DisplayLink USB controller
   *
   * Copyright (C) 2009 Roberto De Ioris <roberto@unbit.it>
   * Copyright (C) 2009 Jaya Kumar <jayakumar.lkml@gmail.com>
2469d5dbc   Bernie Thompson   Staging: udlfb: R...
6
   * Copyright (C) 2009 Bernie Thompson <bernie@plugable.com>
59277b679   Bernie Thompson   Staging: udlfb: a...
7
8
9
10
11
12
13
14
15
16
17
   *
   * This file is subject to the terms and conditions of the GNU General Public
   * License v2. See the file COPYING in the main directory of this archive for
   * more details.
   *
   * Layout is based on skeletonfb by James Simmons and Geert Uytterhoeven,
   * usb-skeleton by GregKH.
   *
   * Device-specific portions based on information from Displaylink, with work
   * from Florian Echtler, Henrik Bjerregaard Pedersen, and others.
   */
88e58b1a4   Roberto De Ioris   Staging: add udlf...
18

81f6f3c10   Paul Mundt   video: udlfb: Kil...
19
  #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
88e58b1a4   Roberto De Ioris   Staging: add udlf...
20
21
22
23
24
25
26
  #include <linux/module.h>
  #include <linux/kernel.h>
  #include <linux/init.h>
  #include <linux/usb.h>
  #include <linux/uaccess.h>
  #include <linux/mm.h>
  #include <linux/fb.h>
fb2990021   Amit Kucheria   staging: udlfb: A...
27
  #include <linux/vmalloc.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
28
  #include <linux/slab.h>
268bb0ce3   Linus Torvalds   sanitize <linux/p...
29
  #include <linux/prefetch.h>
33077b8d3   Bernie Thompson   staging: udlfb: r...
30
  #include <linux/delay.h>
96f8d864a   Paul Mundt   fbdev: move udlfb...
31
  #include <video/udlfb.h>
b9f03a3cd   Paul Mundt   video: udlfb: Kil...
32
  #include "edid.h"
88e58b1a4   Roberto De Ioris   Staging: add udlf...
33

59277b679   Bernie Thompson   Staging: udlfb: a...
34
  static struct fb_fix_screeninfo dlfb_fix = {
2469d5dbc   Bernie Thompson   Staging: udlfb: R...
35
  	.id =           "udlfb",
1d31a9ee6   Bernie Thompson   Staging: udlfb: c...
36
37
38
39
40
41
  	.type =         FB_TYPE_PACKED_PIXELS,
  	.visual =       FB_VISUAL_TRUECOLOR,
  	.xpanstep =     0,
  	.ypanstep =     0,
  	.ywrapstep =    0,
  	.accel =        FB_ACCEL_NONE,
59277b679   Bernie Thompson   Staging: udlfb: a...
42
  };
88e58b1a4   Roberto De Ioris   Staging: add udlf...
43

2469d5dbc   Bernie Thompson   Staging: udlfb: R...
44
  static const u32 udlfb_info_flags = FBINFO_DEFAULT | FBINFO_READS_FAST |
2469d5dbc   Bernie Thompson   Staging: udlfb: R...
45
  		FBINFO_VIRTFB |
2469d5dbc   Bernie Thompson   Staging: udlfb: R...
46
47
  		FBINFO_HWACCEL_IMAGEBLIT | FBINFO_HWACCEL_FILLRECT |
  		FBINFO_HWACCEL_COPYAREA | FBINFO_MISC_ALWAYS_SETPAR;
cc403dc67   Bernie Thompson   Staging: udlfb: r...
48
  /*
b63d10130   Bernie Thompson   drivers/video/udl...
49
50
51
52
53
   * There are many DisplayLink-based graphics products, all with unique PIDs.
   * So we match on DisplayLink's VID + Vendor-Defined Interface Class (0xff)
   * We also require a match on SubClass (0x00) and Protocol (0x00),
   * which is compatible with all known USB 2.0 era graphics chips and firmware,
   * but allows DisplayLink to increment those for any future incompatible chips
cc403dc67   Bernie Thompson   Staging: udlfb: r...
54
55
   */
  static struct usb_device_id id_table[] = {
b63d10130   Bernie Thompson   drivers/video/udl...
56
57
58
59
60
61
62
63
64
  	{.idVendor = 0x17e9,
  	 .bInterfaceClass = 0xff,
  	 .bInterfaceSubClass = 0x00,
  	 .bInterfaceProtocol = 0x00,
  	 .match_flags = USB_DEVICE_ID_MATCH_VENDOR |
  		USB_DEVICE_ID_MATCH_INT_CLASS |
  		USB_DEVICE_ID_MATCH_INT_SUBCLASS |
  		USB_DEVICE_ID_MATCH_INT_PROTOCOL,
  	},
cc403dc67   Bernie Thompson   Staging: udlfb: r...
65
66
67
  	{},
  };
  MODULE_DEVICE_TABLE(usb, id_table);
88e58b1a4   Roberto De Ioris   Staging: add udlf...
68

d5ed54322   Bernie Thompson   staging: udlfb: a...
69
  /* module options */
90ab5ee94   Rusty Russell   module_param: mak...
70
71
72
  static bool console = 1; /* Allow fbcon to open framebuffer */
  static bool fb_defio = 1;  /* Detect mmap writes using page faults */
  static bool shadow = 1; /* Optionally disable shadow framebuffer */
dd8015f1c   Bernie Thompson   Staging: udlfb: e...
73

4a4854dd2   Bernie Thompson   Staging: udlfb: p...
74
75
76
77
78
79
  /* dlfb keeps a list of urbs for efficient bulk transfers */
  static void dlfb_urb_completion(struct urb *urb);
  static struct urb *dlfb_get_urb(struct dlfb_data *dev);
  static int dlfb_submit_urb(struct dlfb_data *dev, struct urb * urb, size_t len);
  static int dlfb_alloc_urb_list(struct dlfb_data *dev, int count, size_t size);
  static void dlfb_free_urb_list(struct dlfb_data *dev);
59277b679   Bernie Thompson   Staging: udlfb: a...
80
  /*
bd80816b2   Bernie Thompson   Staging: udlfb: r...
81
82
   * All DisplayLink bulk operations start with 0xAF, followed by specific code
   * All operations are written to buffers which then later get sent to device
59277b679   Bernie Thompson   Staging: udlfb: a...
83
   */
4574203f4   Bernie Thompson   Staging: udlfb: c...
84
  static char *dlfb_set_register(char *buf, u8 reg, u8 val)
88e58b1a4   Roberto De Ioris   Staging: add udlf...
85
  {
1d31a9ee6   Bernie Thompson   Staging: udlfb: c...
86
87
88
89
90
  	*buf++ = 0xAF;
  	*buf++ = 0x20;
  	*buf++ = reg;
  	*buf++ = val;
  	return buf;
59277b679   Bernie Thompson   Staging: udlfb: a...
91
  }
88e58b1a4   Roberto De Ioris   Staging: add udlf...
92

4574203f4   Bernie Thompson   Staging: udlfb: c...
93
  static char *dlfb_vidreg_lock(char *buf)
59277b679   Bernie Thompson   Staging: udlfb: a...
94
  {
4574203f4   Bernie Thompson   Staging: udlfb: c...
95
  	return dlfb_set_register(buf, 0xFF, 0x00);
59277b679   Bernie Thompson   Staging: udlfb: a...
96
  }
88e58b1a4   Roberto De Ioris   Staging: add udlf...
97

4574203f4   Bernie Thompson   Staging: udlfb: c...
98
  static char *dlfb_vidreg_unlock(char *buf)
59277b679   Bernie Thompson   Staging: udlfb: a...
99
  {
4574203f4   Bernie Thompson   Staging: udlfb: c...
100
  	return dlfb_set_register(buf, 0xFF, 0xFF);
59277b679   Bernie Thompson   Staging: udlfb: a...
101
102
103
  }
  
  /*
58e7c3b00   Bernie Thompson   udlfb: add more c...
104
105
106
107
108
109
110
111
   * Map FB_BLANK_* to DisplayLink register
   * DLReg FB_BLANK_*
   * ----- -----------------------------
   *  0x00 FB_BLANK_UNBLANK (0)
   *  0x01 FB_BLANK (1)
   *  0x03 FB_BLANK_VSYNC_SUSPEND (2)
   *  0x05 FB_BLANK_HSYNC_SUSPEND (3)
   *  0x07 FB_BLANK_POWERDOWN (4) Note: requires modeset to come back
59277b679   Bernie Thompson   Staging: udlfb: a...
112
   */
58e7c3b00   Bernie Thompson   udlfb: add more c...
113
  static char *dlfb_blanking(char *buf, int fb_blank)
59277b679   Bernie Thompson   Staging: udlfb: a...
114
  {
58e7c3b00   Bernie Thompson   udlfb: add more c...
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
  	u8 reg;
  
  	switch (fb_blank) {
  	case FB_BLANK_POWERDOWN:
  		reg = 0x07;
  		break;
  	case FB_BLANK_HSYNC_SUSPEND:
  		reg = 0x05;
  		break;
  	case FB_BLANK_VSYNC_SUSPEND:
  		reg = 0x03;
  		break;
  	case FB_BLANK_NORMAL:
  		reg = 0x01;
  		break;
  	default:
  		reg = 0x00;
  	}
  
  	buf = dlfb_set_register(buf, 0x1F, reg);
  
  	return buf;
59277b679   Bernie Thompson   Staging: udlfb: a...
137
  }
4574203f4   Bernie Thompson   Staging: udlfb: c...
138
  static char *dlfb_set_color_depth(char *buf, u8 selection)
59277b679   Bernie Thompson   Staging: udlfb: a...
139
  {
4574203f4   Bernie Thompson   Staging: udlfb: c...
140
  	return dlfb_set_register(buf, 0x00, selection);
59277b679   Bernie Thompson   Staging: udlfb: a...
141
  }
4574203f4   Bernie Thompson   Staging: udlfb: c...
142
  static char *dlfb_set_base16bpp(char *wrptr, u32 base)
59277b679   Bernie Thompson   Staging: udlfb: a...
143
  {
1d31a9ee6   Bernie Thompson   Staging: udlfb: c...
144
  	/* the base pointer is 16 bits wide, 0x20 is hi byte. */
4574203f4   Bernie Thompson   Staging: udlfb: c...
145
146
147
  	wrptr = dlfb_set_register(wrptr, 0x20, base >> 16);
  	wrptr = dlfb_set_register(wrptr, 0x21, base >> 8);
  	return dlfb_set_register(wrptr, 0x22, base);
59277b679   Bernie Thompson   Staging: udlfb: a...
148
  }
bd80816b2   Bernie Thompson   Staging: udlfb: r...
149
150
151
152
  /*
   * DisplayLink HW has separate 16bpp and 8bpp framebuffers.
   * In 24bpp modes, the low 323 RGB bits go in the 8bpp framebuffer
   */
4574203f4   Bernie Thompson   Staging: udlfb: c...
153
  static char *dlfb_set_base8bpp(char *wrptr, u32 base)
59277b679   Bernie Thompson   Staging: udlfb: a...
154
  {
4574203f4   Bernie Thompson   Staging: udlfb: c...
155
156
157
  	wrptr = dlfb_set_register(wrptr, 0x26, base >> 16);
  	wrptr = dlfb_set_register(wrptr, 0x27, base >> 8);
  	return dlfb_set_register(wrptr, 0x28, base);
59277b679   Bernie Thompson   Staging: udlfb: a...
158
  }
4574203f4   Bernie Thompson   Staging: udlfb: c...
159
  static char *dlfb_set_register_16(char *wrptr, u8 reg, u16 value)
59277b679   Bernie Thompson   Staging: udlfb: a...
160
  {
4574203f4   Bernie Thompson   Staging: udlfb: c...
161
162
  	wrptr = dlfb_set_register(wrptr, reg, value >> 8);
  	return dlfb_set_register(wrptr, reg+1, value);
59277b679   Bernie Thompson   Staging: udlfb: a...
163
164
165
166
167
168
  }
  
  /*
   * This is kind of weird because the controller takes some
   * register values in a different byte order than other registers.
   */
4574203f4   Bernie Thompson   Staging: udlfb: c...
169
  static char *dlfb_set_register_16be(char *wrptr, u8 reg, u16 value)
59277b679   Bernie Thompson   Staging: udlfb: a...
170
  {
4574203f4   Bernie Thompson   Staging: udlfb: c...
171
172
  	wrptr = dlfb_set_register(wrptr, reg, value);
  	return dlfb_set_register(wrptr, reg+1, value >> 8);
59277b679   Bernie Thompson   Staging: udlfb: a...
173
174
175
176
177
178
179
180
181
182
183
  }
  
  /*
   * LFSR is linear feedback shift register. The reason we have this is
   * because the display controller needs to minimize the clock depth of
   * various counters used in the display path. So this code reverses the
   * provided value into the lfsr16 value by counting backwards to get
   * the value that needs to be set in the hardware comparator to get the
   * same actual count. This makes sense once you read above a couple of
   * times and think about it from a hardware perspective.
   */
bd80816b2   Bernie Thompson   Staging: udlfb: r...
184
  static u16 dlfb_lfsr16(u16 actual_count)
59277b679   Bernie Thompson   Staging: udlfb: a...
185
186
187
188
189
190
191
  {
  	u32 lv = 0xFFFF; /* This is the lfsr value that the hw starts with */
  
  	while (actual_count--) {
  		lv =	 ((lv << 1) |
  			(((lv >> 15) ^ (lv >> 4) ^ (lv >> 2) ^ (lv >> 1)) & 1))
  			& 0xFFFF;
88e58b1a4   Roberto De Ioris   Staging: add udlf...
192
  	}
59277b679   Bernie Thompson   Staging: udlfb: a...
193
194
195
196
197
198
199
  	return (u16) lv;
  }
  
  /*
   * This does LFSR conversion on the value that is to be written.
   * See LFSR explanation above for more detail.
   */
4574203f4   Bernie Thompson   Staging: udlfb: c...
200
  static char *dlfb_set_register_lfsr16(char *wrptr, u8 reg, u16 value)
59277b679   Bernie Thompson   Staging: udlfb: a...
201
  {
bd80816b2   Bernie Thompson   Staging: udlfb: r...
202
  	return dlfb_set_register_16(wrptr, reg, dlfb_lfsr16(value));
88e58b1a4   Roberto De Ioris   Staging: add udlf...
203
  }
59277b679   Bernie Thompson   Staging: udlfb: a...
204
205
206
207
  /*
   * This takes a standard fbdev screeninfo struct and all of its monitor mode
   * details and converts them into the DisplayLink equivalent register commands.
   */
4574203f4   Bernie Thompson   Staging: udlfb: c...
208
  static char *dlfb_set_vid_cmds(char *wrptr, struct fb_var_screeninfo *var)
88e58b1a4   Roberto De Ioris   Staging: add udlf...
209
  {
59277b679   Bernie Thompson   Staging: udlfb: a...
210
211
212
  	u16 xds, yds;
  	u16 xde, yde;
  	u16 yec;
59277b679   Bernie Thompson   Staging: udlfb: a...
213
214
  	/* x display start */
  	xds = var->left_margin + var->hsync_len;
4574203f4   Bernie Thompson   Staging: udlfb: c...
215
  	wrptr = dlfb_set_register_lfsr16(wrptr, 0x01, xds);
59277b679   Bernie Thompson   Staging: udlfb: a...
216
217
  	/* x display end */
  	xde = xds + var->xres;
4574203f4   Bernie Thompson   Staging: udlfb: c...
218
  	wrptr = dlfb_set_register_lfsr16(wrptr, 0x03, xde);
59277b679   Bernie Thompson   Staging: udlfb: a...
219
220
221
  
  	/* y display start */
  	yds = var->upper_margin + var->vsync_len;
4574203f4   Bernie Thompson   Staging: udlfb: c...
222
  	wrptr = dlfb_set_register_lfsr16(wrptr, 0x05, yds);
59277b679   Bernie Thompson   Staging: udlfb: a...
223
224
  	/* y display end */
  	yde = yds + var->yres;
4574203f4   Bernie Thompson   Staging: udlfb: c...
225
  	wrptr = dlfb_set_register_lfsr16(wrptr, 0x07, yde);
59277b679   Bernie Thompson   Staging: udlfb: a...
226
227
  
  	/* x end count is active + blanking - 1 */
4574203f4   Bernie Thompson   Staging: udlfb: c...
228
229
  	wrptr = dlfb_set_register_lfsr16(wrptr, 0x09,
  			xde + var->right_margin - 1);
59277b679   Bernie Thompson   Staging: udlfb: a...
230
231
  
  	/* libdlo hardcodes hsync start to 1 */
4574203f4   Bernie Thompson   Staging: udlfb: c...
232
  	wrptr = dlfb_set_register_lfsr16(wrptr, 0x0B, 1);
59277b679   Bernie Thompson   Staging: udlfb: a...
233
234
  
  	/* hsync end is width of sync pulse + 1 */
4574203f4   Bernie Thompson   Staging: udlfb: c...
235
  	wrptr = dlfb_set_register_lfsr16(wrptr, 0x0D, var->hsync_len + 1);
59277b679   Bernie Thompson   Staging: udlfb: a...
236
237
  
  	/* hpixels is active pixels */
4574203f4   Bernie Thompson   Staging: udlfb: c...
238
  	wrptr = dlfb_set_register_16(wrptr, 0x0F, var->xres);
88e58b1a4   Roberto De Ioris   Staging: add udlf...
239

59277b679   Bernie Thompson   Staging: udlfb: a...
240
241
242
  	/* yendcount is vertical active + vertical blanking */
  	yec = var->yres + var->upper_margin + var->lower_margin +
  			var->vsync_len;
4574203f4   Bernie Thompson   Staging: udlfb: c...
243
  	wrptr = dlfb_set_register_lfsr16(wrptr, 0x11, yec);
88e58b1a4   Roberto De Ioris   Staging: add udlf...
244

59277b679   Bernie Thompson   Staging: udlfb: a...
245
  	/* libdlo hardcodes vsync start to 0 */
4574203f4   Bernie Thompson   Staging: udlfb: c...
246
  	wrptr = dlfb_set_register_lfsr16(wrptr, 0x13, 0);
59277b679   Bernie Thompson   Staging: udlfb: a...
247
248
  
  	/* vsync end is width of vsync pulse */
4574203f4   Bernie Thompson   Staging: udlfb: c...
249
  	wrptr = dlfb_set_register_lfsr16(wrptr, 0x15, var->vsync_len);
59277b679   Bernie Thompson   Staging: udlfb: a...
250
251
  
  	/* vpixels is active pixels */
4574203f4   Bernie Thompson   Staging: udlfb: c...
252
  	wrptr = dlfb_set_register_16(wrptr, 0x17, var->yres);
59277b679   Bernie Thompson   Staging: udlfb: a...
253
254
  
  	/* convert picoseconds to 5kHz multiple for pclk5k = x * 1E12/5k */
4574203f4   Bernie Thompson   Staging: udlfb: c...
255
256
  	wrptr = dlfb_set_register_16be(wrptr, 0x1B,
  			200*1000*1000/var->pixclock);
59277b679   Bernie Thompson   Staging: udlfb: a...
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
  
  	return wrptr;
  }
  
  /*
   * This takes a standard fbdev screeninfo struct that was fetched or prepared
   * and then generates the appropriate command sequence that then drives the
   * display controller.
   */
  static int dlfb_set_video_mode(struct dlfb_data *dev,
  				struct fb_var_screeninfo *var)
  {
  	char *buf;
  	char *wrptr;
  	int retval = 0;
  	int writesize;
530f43a8a   Bernie Thompson   Staging: udlfb: i...
273
274
275
276
  	struct urb *urb;
  
  	if (!atomic_read(&dev->usb_active))
  		return -EPERM;
59277b679   Bernie Thompson   Staging: udlfb: a...
277

530f43a8a   Bernie Thompson   Staging: udlfb: i...
278
279
280
  	urb = dlfb_get_urb(dev);
  	if (!urb)
  		return -ENOMEM;
2685cffa9   Bernie Thompson   staging: udlfb: f...
281

530f43a8a   Bernie Thompson   Staging: udlfb: i...
282
  	buf = (char *) urb->transfer_buffer;
59277b679   Bernie Thompson   Staging: udlfb: a...
283
284
285
286
287
288
  
  	/*
  	* This first section has to do with setting the base address on the
  	* controller * associated with the display. There are 2 base
  	* pointers, currently, we only * use the 16 bpp segment.
  	*/
4574203f4   Bernie Thompson   Staging: udlfb: c...
289
290
  	wrptr = dlfb_vidreg_lock(buf);
  	wrptr = dlfb_set_color_depth(wrptr, 0x00);
59277b679   Bernie Thompson   Staging: udlfb: a...
291
  	/* set base for 16bpp segment to 0 */
4574203f4   Bernie Thompson   Staging: udlfb: c...
292
  	wrptr = dlfb_set_base16bpp(wrptr, 0);
59277b679   Bernie Thompson   Staging: udlfb: a...
293
  	/* set base for 8bpp segment to end of fb */
4574203f4   Bernie Thompson   Staging: udlfb: c...
294
  	wrptr = dlfb_set_base8bpp(wrptr, dev->info->fix.smem_len);
59277b679   Bernie Thompson   Staging: udlfb: a...
295

4574203f4   Bernie Thompson   Staging: udlfb: c...
296
  	wrptr = dlfb_set_vid_cmds(wrptr, var);
58e7c3b00   Bernie Thompson   udlfb: add more c...
297
  	wrptr = dlfb_blanking(wrptr, FB_BLANK_UNBLANK);
4574203f4   Bernie Thompson   Staging: udlfb: c...
298
  	wrptr = dlfb_vidreg_unlock(wrptr);
59277b679   Bernie Thompson   Staging: udlfb: a...
299
300
  
  	writesize = wrptr - buf;
530f43a8a   Bernie Thompson   Staging: udlfb: i...
301
  	retval = dlfb_submit_urb(dev, urb, writesize);
59277b679   Bernie Thompson   Staging: udlfb: a...
302

58e7c3b00   Bernie Thompson   udlfb: add more c...
303
  	dev->blank_mode = FB_BLANK_UNBLANK;
59277b679   Bernie Thompson   Staging: udlfb: a...
304
305
  	return retval;
  }
4574203f4   Bernie Thompson   Staging: udlfb: c...
306
  static int dlfb_ops_mmap(struct fb_info *info, struct vm_area_struct *vma)
88e58b1a4   Roberto De Ioris   Staging: add udlf...
307
308
309
310
311
  {
  	unsigned long start = vma->vm_start;
  	unsigned long size = vma->vm_end - vma->vm_start;
  	unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  	unsigned long page, pos;
f05e0575e   Greg Kroah-Hartman   Staging: udlfb: c...
312
  	if (offset + size > info->fix.smem_len)
88e58b1a4   Roberto De Ioris   Staging: add udlf...
313
  		return -EINVAL;
88e58b1a4   Roberto De Ioris   Staging: add udlf...
314
315
  
  	pos = (unsigned long)info->fix.smem_start + offset;
81f6f3c10   Paul Mundt   video: udlfb: Kil...
316
317
  	pr_notice("mmap() framebuffer addr:%lu size:%lu
  ",
2685cffa9   Bernie Thompson   staging: udlfb: f...
318
  		  pos, size);
88e58b1a4   Roberto De Ioris   Staging: add udlf...
319
320
  	while (size > 0) {
  		page = vmalloc_to_pfn((void *)pos);
f05e0575e   Greg Kroah-Hartman   Staging: udlfb: c...
321
  		if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED))
88e58b1a4   Roberto De Ioris   Staging: add udlf...
322
  			return -EAGAIN;
f05e0575e   Greg Kroah-Hartman   Staging: udlfb: c...
323

88e58b1a4   Roberto De Ioris   Staging: add udlf...
324
325
326
327
328
329
330
331
332
333
  		start += PAGE_SIZE;
  		pos += PAGE_SIZE;
  		if (size > PAGE_SIZE)
  			size -= PAGE_SIZE;
  		else
  			size = 0;
  	}
  
  	vma->vm_flags |= VM_RESERVED;	/* avoid to swap out this VMA */
  	return 0;
88e58b1a4   Roberto De Ioris   Staging: add udlf...
334
  }
530f43a8a   Bernie Thompson   Staging: udlfb: i...
335
336
337
338
339
340
341
342
  /*
   * Trims identical data from front and back of line
   * Sets new front buffer address and width
   * And returns byte count of identical pixels
   * Assumes CPU natural alignment (unsigned long)
   * for back and front buffer ptrs and width
   */
  static int dlfb_trim_hline(const u8 *bback, const u8 **bfront, int *width_bytes)
7316bc55e   Roberto De Ioris   Staging: udlfb: u...
343
  {
530f43a8a   Bernie Thompson   Staging: udlfb: i...
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
  	int j, k;
  	const unsigned long *back = (const unsigned long *) bback;
  	const unsigned long *front = (const unsigned long *) *bfront;
  	const int width = *width_bytes / sizeof(unsigned long);
  	int identical = width;
  	int start = width;
  	int end = width;
  
  	prefetch((void *) front);
  	prefetch((void *) back);
  
  	for (j = 0; j < width; j++) {
  		if (back[j] != front[j]) {
  			start = j;
  			break;
  		}
  	}
7316bc55e   Roberto De Ioris   Staging: udlfb: u...
361

530f43a8a   Bernie Thompson   Staging: udlfb: i...
362
363
364
365
366
  	for (k = width - 1; k > j; k--) {
  		if (back[k] != front[k]) {
  			end = k+1;
  			break;
  		}
7316bc55e   Roberto De Ioris   Staging: udlfb: u...
367
  	}
530f43a8a   Bernie Thompson   Staging: udlfb: i...
368
369
370
371
372
  	identical = start + (width - end);
  	*bfront = (u8 *) &front[start];
  	*width_bytes = (end - start) * sizeof(unsigned long);
  
  	return identical * sizeof(unsigned long);
7316bc55e   Roberto De Ioris   Staging: udlfb: u...
373
374
375
  }
  
  /*
3b7b31fa7   Pavel Machek   Staging: udlfb: m...
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
   * Render a command stream for an encoded horizontal line segment of pixels.
   *
   * A command buffer holds several commands.
   * It always begins with a fresh command header
   * (the protocol doesn't require this, but we enforce it to allow
   * multiple buffers to be potentially encoded and sent in parallel).
   * A single command encodes one contiguous horizontal line of pixels
   *
   * The function relies on the client to do all allocation, so that
   * rendering can be done directly to output buffers (e.g. USB URBs).
   * The function fills the supplied command buffer, providing information
   * on where it left off, so the client may call in again with additional
   * buffers if the line will take several buffers to complete.
   *
   * A single command can transmit a maximum of 256 pixels,
   * regardless of the compression ratio (protocol design limit).
   * To the hardware, 0 for a size byte means 256
2685cffa9   Bernie Thompson   staging: udlfb: f...
393
   *
3b7b31fa7   Pavel Machek   Staging: udlfb: m...
394
395
396
397
398
   * Rather than 256 pixel commands which are either rl or raw encoded,
   * the rlx command simply assumes alternating raw and rl spans within one cmd.
   * This has a slightly larger header overhead, but produces more even results.
   * It also processes all data (read and write) in a single pass.
   * Performance benchmarks of common cases show it having just slightly better
2685cffa9   Bernie Thompson   staging: udlfb: f...
399
   * compression than 256 pixel raw or rle commands, with similar CPU consumpion.
3b7b31fa7   Pavel Machek   Staging: udlfb: m...
400
401
   * But for very rl friendly data, will compress not quite as well.
   */
530f43a8a   Bernie Thompson   Staging: udlfb: i...
402
403
404
405
406
407
  static void 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)
88e58b1a4   Roberto De Ioris   Staging: add udlf...
408
  {
530f43a8a   Bernie Thompson   Staging: udlfb: i...
409
410
411
412
413
414
415
416
417
418
419
  	const uint16_t *pixel = *pixel_start_ptr;
  	uint32_t dev_addr  = *device_address_ptr;
  	uint8_t *cmd = *command_buffer_ptr;
  	const int bpp = 2;
  
  	while ((pixel_end > pixel) &&
  	       (cmd_buffer_end - MIN_RLX_CMD_BYTES > cmd)) {
  		uint8_t *raw_pixels_count_byte = 0;
  		uint8_t *cmd_pixels_count_byte = 0;
  		const uint16_t *raw_pixel_start = 0;
  		const uint16_t *cmd_pixel_start, *cmd_pixel_end = 0;
530f43a8a   Bernie Thompson   Staging: udlfb: i...
420
421
422
423
424
  
  		prefetchw((void *) cmd); /* pull in one cache line at least */
  
  		*cmd++ = 0xAF;
  		*cmd++ = 0x6B;
1572f91cf   Bernie Thompson   staging: udlfb: f...
425
426
427
  		*cmd++ = (uint8_t) ((dev_addr >> 16) & 0xFF);
  		*cmd++ = (uint8_t) ((dev_addr >> 8) & 0xFF);
  		*cmd++ = (uint8_t) ((dev_addr) & 0xFF);
530f43a8a   Bernie Thompson   Staging: udlfb: i...
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
  
  		cmd_pixels_count_byte = cmd++; /*  we'll know this later */
  		cmd_pixel_start = pixel;
  
  		raw_pixels_count_byte = cmd++; /*  we'll know this later */
  		raw_pixel_start = pixel;
  
  		cmd_pixel_end = pixel + min(MAX_CMD_PIXELS + 1,
  			min((int)(pixel_end - pixel),
  			    (int)(cmd_buffer_end - cmd) / bpp));
  
  		prefetch_range((void *) pixel, (cmd_pixel_end - pixel) * bpp);
  
  		while (pixel < cmd_pixel_end) {
  			const uint16_t * const repeating_pixel = pixel;
  
  			*(uint16_t *)cmd = cpu_to_be16p(pixel);
  			cmd += 2;
  			pixel++;
  
  			if (unlikely((pixel < cmd_pixel_end) &&
  				     (*pixel == *repeating_pixel))) {
  				/* go back and fill in raw pixel count */
  				*raw_pixels_count_byte = ((repeating_pixel -
  						raw_pixel_start) + 1) & 0xFF;
  
  				while ((pixel < cmd_pixel_end)
  				       && (*pixel == *repeating_pixel)) {
  					pixel++;
88e58b1a4   Roberto De Ioris   Staging: add udlf...
457
  				}
59277b679   Bernie Thompson   Staging: udlfb: a...
458

530f43a8a   Bernie Thompson   Staging: udlfb: i...
459
460
  				/* immediately after raw data is repeat byte */
  				*cmd++ = ((pixel - repeating_pixel) - 1) & 0xFF;
59277b679   Bernie Thompson   Staging: udlfb: a...
461

530f43a8a   Bernie Thompson   Staging: udlfb: i...
462
463
464
  				/* Then start another raw pixel span */
  				raw_pixel_start = pixel;
  				raw_pixels_count_byte = cmd++;
7316bc55e   Roberto De Ioris   Staging: udlfb: u...
465
  			}
88e58b1a4   Roberto De Ioris   Staging: add udlf...
466
  		}
530f43a8a   Bernie Thompson   Staging: udlfb: i...
467
468
469
470
  		if (pixel > raw_pixel_start) {
  			/* finalize last RAW span */
  			*raw_pixels_count_byte = (pixel-raw_pixel_start) & 0xFF;
  		}
88e58b1a4   Roberto De Ioris   Staging: add udlf...
471

530f43a8a   Bernie Thompson   Staging: udlfb: i...
472
473
  		*cmd_pixels_count_byte = (pixel - cmd_pixel_start) & 0xFF;
  		dev_addr += (pixel - cmd_pixel_start) * bpp;
88e58b1a4   Roberto De Ioris   Staging: add udlf...
474
  	}
530f43a8a   Bernie Thompson   Staging: udlfb: i...
475
476
477
478
479
  	if (cmd_buffer_end <= MIN_RLX_CMD_BYTES + cmd) {
  		/* Fill leftover bytes with no-ops */
  		if (cmd_buffer_end > cmd)
  			memset(cmd, 0xAF, cmd_buffer_end - cmd);
  		cmd = (uint8_t *) cmd_buffer_end;
7316bc55e   Roberto De Ioris   Staging: udlfb: u...
480
  	}
88e58b1a4   Roberto De Ioris   Staging: add udlf...
481

530f43a8a   Bernie Thompson   Staging: udlfb: i...
482
483
484
  	*command_buffer_ptr = cmd;
  	*pixel_start_ptr = pixel;
  	*device_address_ptr = dev_addr;
88e58b1a4   Roberto De Ioris   Staging: add udlf...
485

530f43a8a   Bernie Thompson   Staging: udlfb: i...
486
  	return;
88e58b1a4   Roberto De Ioris   Staging: add udlf...
487
  }
530f43a8a   Bernie Thompson   Staging: udlfb: i...
488
489
490
491
492
493
  /*
   * There are 3 copies of every pixel: The front buffer that the fbdev
   * client renders to, the actual framebuffer across the USB bus in hardware
   * (that we can only write to, slowly, and can never read), and (optionally)
   * our shadow copy that tracks what's been sent to that hardware buffer.
   */
5bea1fbf9   Bernie Thompson   staging: udlfb: f...
494
  static int dlfb_render_hline(struct dlfb_data *dev, struct urb **urb_ptr,
530f43a8a   Bernie Thompson   Staging: udlfb: i...
495
496
497
  			      const char *front, char **urb_buf_ptr,
  			      u32 byte_offset, u32 byte_width,
  			      int *ident_ptr, int *sent_ptr)
88e58b1a4   Roberto De Ioris   Staging: add udlf...
498
  {
530f43a8a   Bernie Thompson   Staging: udlfb: i...
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
  	const u8 *line_start, *line_end, *next_pixel;
  	u32 dev_addr = dev->base16 + byte_offset;
  	struct urb *urb = *urb_ptr;
  	u8 *cmd = *urb_buf_ptr;
  	u8 *cmd_end = (u8 *) urb->transfer_buffer + urb->transfer_buffer_length;
  
  	line_start = (u8 *) (front + byte_offset);
  	next_pixel = line_start;
  	line_end = next_pixel + byte_width;
  
  	if (dev->backing_buffer) {
  		int offset;
  		const u8 *back_start = (u8 *) (dev->backing_buffer
  						+ byte_offset);
  
  		*ident_ptr += dlfb_trim_hline(back_start, &next_pixel,
  			&byte_width);
  
  		offset = next_pixel - line_start;
  		line_end = next_pixel + byte_width;
  		dev_addr += offset;
  		back_start += offset;
  		line_start += offset;
  
  		memcpy((char *)back_start, (char *) line_start,
  		       byte_width);
  	}
88e58b1a4   Roberto De Ioris   Staging: add udlf...
526

530f43a8a   Bernie Thompson   Staging: udlfb: i...
527
528
529
530
531
532
533
534
535
  	while (next_pixel < line_end) {
  
  		dlfb_compress_hline((const uint16_t **) &next_pixel,
  			     (const uint16_t *) line_end, &dev_addr,
  			(u8 **) &cmd, (u8 *) cmd_end);
  
  		if (cmd >= cmd_end) {
  			int len = cmd - (u8 *) urb->transfer_buffer;
  			if (dlfb_submit_urb(dev, urb, len))
5bea1fbf9   Bernie Thompson   staging: udlfb: f...
536
  				return 1; /* lost pixels is set */
530f43a8a   Bernie Thompson   Staging: udlfb: i...
537
538
539
  			*sent_ptr += len;
  			urb = dlfb_get_urb(dev);
  			if (!urb)
5bea1fbf9   Bernie Thompson   staging: udlfb: f...
540
  				return 1; /* lost_pixels is set */
530f43a8a   Bernie Thompson   Staging: udlfb: i...
541
542
543
  			*urb_ptr = urb;
  			cmd = urb->transfer_buffer;
  			cmd_end = &cmd[urb->transfer_buffer_length];
88e58b1a4   Roberto De Ioris   Staging: add udlf...
544
  		}
88e58b1a4   Roberto De Ioris   Staging: add udlf...
545
  	}
530f43a8a   Bernie Thompson   Staging: udlfb: i...
546
  	*urb_buf_ptr = cmd;
5bea1fbf9   Bernie Thompson   staging: udlfb: f...
547
548
  
  	return 0;
88e58b1a4   Roberto De Ioris   Staging: add udlf...
549
  }
530f43a8a   Bernie Thompson   Staging: udlfb: i...
550
551
  int dlfb_handle_damage(struct dlfb_data *dev, int x, int y,
  	       int width, int height, char *data)
7316bc55e   Roberto De Ioris   Staging: udlfb: u...
552
  {
7316bc55e   Roberto De Ioris   Staging: udlfb: u...
553
  	int i, ret;
530f43a8a   Bernie Thompson   Staging: udlfb: i...
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
  	char *cmd;
  	cycles_t start_cycles, end_cycles;
  	int bytes_sent = 0;
  	int bytes_identical = 0;
  	struct urb *urb;
  	int aligned_x;
  
  	start_cycles = get_cycles();
  
  	aligned_x = DL_ALIGN_DOWN(x, sizeof(unsigned long));
  	width = DL_ALIGN_UP(width + (x-aligned_x), sizeof(unsigned long));
  	x = aligned_x;
  
  	if ((width <= 0) ||
  	    (x + width > dev->info->var.xres) ||
  	    (y + height > dev->info->var.yres))
  		return -EINVAL;
7316bc55e   Roberto De Ioris   Staging: udlfb: u...
571

530f43a8a   Bernie Thompson   Staging: udlfb: i...
572
573
  	if (!atomic_read(&dev->usb_active))
  		return 0;
7316bc55e   Roberto De Ioris   Staging: udlfb: u...
574

530f43a8a   Bernie Thompson   Staging: udlfb: i...
575
576
577
578
  	urb = dlfb_get_urb(dev);
  	if (!urb)
  		return 0;
  	cmd = urb->transfer_buffer;
7316bc55e   Roberto De Ioris   Staging: udlfb: u...
579

530f43a8a   Bernie Thompson   Staging: udlfb: i...
580
581
582
  	for (i = y; i < y + height ; i++) {
  		const int line_offset = dev->info->fix.line_length * i;
  		const int byte_offset = line_offset + (x * BPP);
7316bc55e   Roberto De Ioris   Staging: udlfb: u...
583

5bea1fbf9   Bernie Thompson   staging: udlfb: f...
584
585
  		if (dlfb_render_hline(dev, &urb,
  				      (char *) dev->info->fix.smem_start,
2685cffa9   Bernie Thompson   staging: udlfb: f...
586
  				      &cmd, byte_offset, width * BPP,
5bea1fbf9   Bernie Thompson   staging: udlfb: f...
587
588
  				      &bytes_identical, &bytes_sent))
  			goto error;
7316bc55e   Roberto De Ioris   Staging: udlfb: u...
589
  	}
530f43a8a   Bernie Thompson   Staging: udlfb: i...
590
591
592
593
594
595
596
  	if (cmd > (char *) urb->transfer_buffer) {
  		/* Send partial buffer remaining before exiting */
  		int len = cmd - (char *) urb->transfer_buffer;
  		ret = dlfb_submit_urb(dev, urb, len);
  		bytes_sent += len;
  	} else
  		dlfb_urb_completion(urb);
5bea1fbf9   Bernie Thompson   staging: udlfb: f...
597
  error:
530f43a8a   Bernie Thompson   Staging: udlfb: i...
598
599
600
601
602
603
604
  	atomic_add(bytes_sent, &dev->bytes_sent);
  	atomic_add(bytes_identical, &dev->bytes_identical);
  	atomic_add(width*height*2, &dev->bytes_rendered);
  	end_cycles = get_cycles();
  	atomic_add(((unsigned int) ((end_cycles - start_cycles)
  		    >> 10)), /* Kcycles */
  		   &dev->cpu_kcycles_used);
7316bc55e   Roberto De Ioris   Staging: udlfb: u...
605

530f43a8a   Bernie Thompson   Staging: udlfb: i...
606
  	return 0;
7316bc55e   Roberto De Ioris   Staging: udlfb: u...
607
  }
d46ecb9f7   Bernie Thompson   staging: udlfb: f...
608
609
610
611
612
613
614
615
616
  /*
   * Path triggered by usermode clients who write to filesystem
   * e.g. cat filename > /dev/fb1
   * Not used by X Windows or text-mode console. But useful for testing.
   * Slow because of extra copy and we must assume all pixels dirty.
   */
  static ssize_t dlfb_ops_write(struct fb_info *info, const char __user *buf,
  			  size_t count, loff_t *ppos)
  {
1a3e528cf   Paul Mundt   video: udlfb: dei...
617
  	ssize_t result;
d46ecb9f7   Bernie Thompson   staging: udlfb: f...
618
619
  	struct dlfb_data *dev = info->par;
  	u32 offset = (u32) *ppos;
d46ecb9f7   Bernie Thompson   staging: udlfb: f...
620
621
622
623
624
625
626
627
628
629
  	result = fb_sys_write(info, buf, count, ppos);
  
  	if (result > 0) {
  		int start = max((int)(offset / info->fix.line_length) - 1, 0);
  		int lines = min((u32)((result / info->fix.line_length) + 1),
  				(u32)info->var.yres);
  
  		dlfb_handle_damage(dev, 0, start, info->var.xres,
  			lines, info->screen_base);
  	}
d46ecb9f7   Bernie Thompson   staging: udlfb: f...
630
631
632
  
  	return result;
  }
530f43a8a   Bernie Thompson   Staging: udlfb: i...
633
634
635
  /* hardware has native COPY command (see libdlo), but not worth it for fbcon */
  static void dlfb_ops_copyarea(struct fb_info *info,
  				const struct fb_copyarea *area)
88e58b1a4   Roberto De Ioris   Staging: add udlf...
636
  {
88e58b1a4   Roberto De Ioris   Staging: add udlf...
637

530f43a8a   Bernie Thompson   Staging: udlfb: i...
638
  	struct dlfb_data *dev = info->par;
88e58b1a4   Roberto De Ioris   Staging: add udlf...
639

530f43a8a   Bernie Thompson   Staging: udlfb: i...
640
  	sys_copyarea(info, area);
88e58b1a4   Roberto De Ioris   Staging: add udlf...
641

530f43a8a   Bernie Thompson   Staging: udlfb: i...
642
643
  	dlfb_handle_damage(dev, area->dx, area->dy,
  			area->width, area->height, info->screen_base);
530f43a8a   Bernie Thompson   Staging: udlfb: i...
644
  }
88e58b1a4   Roberto De Ioris   Staging: add udlf...
645

530f43a8a   Bernie Thompson   Staging: udlfb: i...
646
647
648
649
  static void dlfb_ops_imageblit(struct fb_info *info,
  				const struct fb_image *image)
  {
  	struct dlfb_data *dev = info->par;
88e58b1a4   Roberto De Ioris   Staging: add udlf...
650

530f43a8a   Bernie Thompson   Staging: udlfb: i...
651
  	sys_imageblit(info, image);
88e58b1a4   Roberto De Ioris   Staging: add udlf...
652

530f43a8a   Bernie Thompson   Staging: udlfb: i...
653
654
  	dlfb_handle_damage(dev, image->dx, image->dy,
  			image->width, image->height, info->screen_base);
88e58b1a4   Roberto De Ioris   Staging: add udlf...
655
  }
530f43a8a   Bernie Thompson   Staging: udlfb: i...
656
657
  static void dlfb_ops_fillrect(struct fb_info *info,
  			  const struct fb_fillrect *rect)
88e58b1a4   Roberto De Ioris   Staging: add udlf...
658
  {
88e58b1a4   Roberto De Ioris   Staging: add udlf...
659
  	struct dlfb_data *dev = info->par;
530f43a8a   Bernie Thompson   Staging: udlfb: i...
660
  	sys_fillrect(info, rect);
88e58b1a4   Roberto De Ioris   Staging: add udlf...
661

530f43a8a   Bernie Thompson   Staging: udlfb: i...
662
663
  	dlfb_handle_damage(dev, rect->dx, rect->dy, rect->width,
  			      rect->height, info->screen_base);
88e58b1a4   Roberto De Ioris   Staging: add udlf...
664
  }
d5ed54322   Bernie Thompson   staging: udlfb: a...
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
  /*
   * NOTE: fb_defio.c is holding info->fbdefio.mutex
   *   Touching ANY framebuffer memory that triggers a page fault
   *   in fb_defio will cause a deadlock, when it also tries to
   *   grab the same mutex.
   */
  static void dlfb_dpy_deferred_io(struct fb_info *info,
  				struct list_head *pagelist)
  {
  	struct page *cur;
  	struct fb_deferred_io *fbdefio = info->fbdefio;
  	struct dlfb_data *dev = info->par;
  	struct urb *urb;
  	char *cmd;
  	cycles_t start_cycles, end_cycles;
  	int bytes_sent = 0;
  	int bytes_identical = 0;
  	int bytes_rendered = 0;
  
  	if (!fb_defio)
  		return;
  
  	if (!atomic_read(&dev->usb_active))
  		return;
  
  	start_cycles = get_cycles();
  
  	urb = dlfb_get_urb(dev);
  	if (!urb)
  		return;
  
  	cmd = urb->transfer_buffer;
  
  	/* walk the written page list and render each to device */
  	list_for_each_entry(cur, &fbdefio->pagelist, lru) {
5bea1fbf9   Bernie Thompson   staging: udlfb: f...
700
  		if (dlfb_render_hline(dev, &urb, (char *) info->fix.smem_start,
d5ed54322   Bernie Thompson   staging: udlfb: a...
701
  				  &cmd, cur->index << PAGE_SHIFT,
5bea1fbf9   Bernie Thompson   staging: udlfb: f...
702
703
  				  PAGE_SIZE, &bytes_identical, &bytes_sent))
  			goto error;
d5ed54322   Bernie Thompson   staging: udlfb: a...
704
705
706
707
708
709
710
711
712
713
  		bytes_rendered += PAGE_SIZE;
  	}
  
  	if (cmd > (char *) urb->transfer_buffer) {
  		/* Send partial buffer remaining before exiting */
  		int len = cmd - (char *) urb->transfer_buffer;
  		dlfb_submit_urb(dev, urb, len);
  		bytes_sent += len;
  	} else
  		dlfb_urb_completion(urb);
5bea1fbf9   Bernie Thompson   staging: udlfb: f...
714
  error:
d5ed54322   Bernie Thompson   staging: udlfb: a...
715
716
717
718
719
720
721
722
  	atomic_add(bytes_sent, &dev->bytes_sent);
  	atomic_add(bytes_identical, &dev->bytes_identical);
  	atomic_add(bytes_rendered, &dev->bytes_rendered);
  	end_cycles = get_cycles();
  	atomic_add(((unsigned int) ((end_cycles - start_cycles)
  		    >> 10)), /* Kcycles */
  		   &dev->cpu_kcycles_used);
  }
18dffdf89   Bernie Thompson   staging: udlfb: e...
723
  static int dlfb_get_edid(struct dlfb_data *dev, char *edid, int len)
7d9485e2c   Bernie Thompson   Staging: udlfb: A...
724
725
726
  {
  	int i;
  	int ret;
18dffdf89   Bernie Thompson   staging: udlfb: e...
727
  	char *rbuf;
7d9485e2c   Bernie Thompson   Staging: udlfb: A...
728

18dffdf89   Bernie Thompson   staging: udlfb: e...
729
730
731
732
733
  	rbuf = kmalloc(2, GFP_KERNEL);
  	if (!rbuf)
  		return 0;
  
  	for (i = 0; i < len; i++) {
7d9485e2c   Bernie Thompson   Staging: udlfb: A...
734
735
736
  		ret = usb_control_msg(dev->udev,
  				    usb_rcvctrlpipe(dev->udev, 0), (0x02),
  				    (0x80 | (0x02 << 5)), i << 8, 0xA1, rbuf, 2,
18dffdf89   Bernie Thompson   staging: udlfb: e...
737
738
  				    HZ);
  		if (ret < 1) {
81f6f3c10   Paul Mundt   video: udlfb: Kil...
739
740
  			pr_err("Read EDID byte %d failed err %x
  ", i, ret);
18dffdf89   Bernie Thompson   staging: udlfb: e...
741
742
743
744
  			i--;
  			break;
  		}
  		edid[i] = rbuf[1];
7d9485e2c   Bernie Thompson   Staging: udlfb: A...
745
  	}
18dffdf89   Bernie Thompson   staging: udlfb: e...
746
747
748
749
  
  	kfree(rbuf);
  
  	return i;
7d9485e2c   Bernie Thompson   Staging: udlfb: A...
750
  }
4574203f4   Bernie Thompson   Staging: udlfb: c...
751
  static int dlfb_ops_ioctl(struct fb_info *info, unsigned int cmd,
530f43a8a   Bernie Thompson   Staging: udlfb: i...
752
  				unsigned long arg)
88e58b1a4   Roberto De Ioris   Staging: add udlf...
753
  {
530f43a8a   Bernie Thompson   Staging: udlfb: i...
754
755
  
  	struct dlfb_data *dev = info->par;
88e58b1a4   Roberto De Ioris   Staging: add udlf...
756

530f43a8a   Bernie Thompson   Staging: udlfb: i...
757
758
759
760
761
  	if (!atomic_read(&dev->usb_active))
  		return 0;
  
  	/* TODO: Update X server to get this from sysfs instead */
  	if (cmd == DLFB_IOCTL_RETURN_EDID) {
def766086   Dr. David Alan Gilbert   udlfb: fix issues...
762
  		void __user *edid = (void __user *)arg;
18dffdf89   Bernie Thompson   staging: udlfb: e...
763
  		if (copy_to_user(edid, dev->edid, dev->edid_size))
7316bc55e   Roberto De Ioris   Staging: udlfb: u...
764
  			return -EFAULT;
7316bc55e   Roberto De Ioris   Staging: udlfb: u...
765
766
  		return 0;
  	}
530f43a8a   Bernie Thompson   Staging: udlfb: i...
767
768
  	/* TODO: Help propose a standard fb.h ioctl to report mmap damage */
  	if (cmd == DLFB_IOCTL_REPORT_DAMAGE) {
def766086   Dr. David Alan Gilbert   udlfb: fix issues...
769
770
771
772
773
  		struct dloarea area;
  
  		if (copy_from_user(&area, (void __user *)arg,
  				  sizeof(struct dloarea)))
  			return -EFAULT;
88e58b1a4   Roberto De Ioris   Staging: add udlf...
774

5bea1fbf9   Bernie Thompson   staging: udlfb: f...
775
776
  		/*
  		 * If we have a damage-aware client, turn fb_defio "off"
25985edce   Lucas De Marchi   Fix common misspe...
777
  		 * To avoid perf imact of unnecessary page fault handling.
5bea1fbf9   Bernie Thompson   staging: udlfb: f...
778
779
780
781
782
783
  		 * Done by resetting the delay for this fb_info to a very
  		 * long period. Pages will become writable and stay that way.
  		 * Reset to normal value when all clients have closed this fb.
  		 */
  		if (info->fbdefio)
  			info->fbdefio->delay = DL_DEFIO_WRITE_DISABLE;
def766086   Dr. David Alan Gilbert   udlfb: fix issues...
784
785
  		if (area.x < 0)
  			area.x = 0;
88e58b1a4   Roberto De Ioris   Staging: add udlf...
786

def766086   Dr. David Alan Gilbert   udlfb: fix issues...
787
788
  		if (area.x > info->var.xres)
  			area.x = info->var.xres;
88e58b1a4   Roberto De Ioris   Staging: add udlf...
789

def766086   Dr. David Alan Gilbert   udlfb: fix issues...
790
791
  		if (area.y < 0)
  			area.y = 0;
88e58b1a4   Roberto De Ioris   Staging: add udlf...
792

def766086   Dr. David Alan Gilbert   udlfb: fix issues...
793
794
  		if (area.y > info->var.yres)
  			area.y = info->var.yres;
88e58b1a4   Roberto De Ioris   Staging: add udlf...
795

def766086   Dr. David Alan Gilbert   udlfb: fix issues...
796
  		dlfb_handle_damage(dev, area.x, area.y, area.w, area.h,
88e58b1a4   Roberto De Ioris   Staging: add udlf...
797
798
  			   info->screen_base);
  	}
7316bc55e   Roberto De Ioris   Staging: udlfb: u...
799

88e58b1a4   Roberto De Ioris   Staging: add udlf...
800
801
  	return 0;
  }
f05e0575e   Greg Kroah-Hartman   Staging: udlfb: c...
802
  /* taken from vesafb */
88e58b1a4   Roberto De Ioris   Staging: add udlf...
803
  static int
4574203f4   Bernie Thompson   Staging: udlfb: c...
804
  dlfb_ops_setcolreg(unsigned regno, unsigned red, unsigned green,
88e58b1a4   Roberto De Ioris   Staging: add udlf...
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
  	       unsigned blue, unsigned transp, struct fb_info *info)
  {
  	int err = 0;
  
  	if (regno >= info->cmap.len)
  		return 1;
  
  	if (regno < 16) {
  		if (info->var.red.offset == 10) {
  			/* 1:5:5:5 */
  			((u32 *) (info->pseudo_palette))[regno] =
  			    ((red & 0xf800) >> 1) |
  			    ((green & 0xf800) >> 6) | ((blue & 0xf800) >> 11);
  		} else {
  			/* 0:5:6:5 */
  			((u32 *) (info->pseudo_palette))[regno] =
  			    ((red & 0xf800)) |
  			    ((green & 0xfc00) >> 5) | ((blue & 0xf800) >> 11);
  		}
  	}
  
  	return err;
  }
3e8f3d6fa   Bernie Thompson   Staging: udlfb: S...
828
829
830
  /*
   * It's common for several clients to have framebuffer open simultaneously.
   * e.g. both fbcon and X. Makes things interesting.
33077b8d3   Bernie Thompson   staging: udlfb: r...
831
   * Assumes caller is holding info->lock (for open and release at least)
3e8f3d6fa   Bernie Thompson   Staging: udlfb: S...
832
833
834
835
   */
  static int dlfb_ops_open(struct fb_info *info, int user)
  {
  	struct dlfb_data *dev = info->par;
d5ed54322   Bernie Thompson   staging: udlfb: a...
836
837
838
839
840
  	/*
  	 * fbcon aggressively connects to first framebuffer it finds,
  	 * preventing other clients (X) from working properly. Usually
  	 * not what the user wants. Fail by default with option to enable.
  	 */
def766086   Dr. David Alan Gilbert   udlfb: fix issues...
841
  	if ((user == 0) && (!console))
d5ed54322   Bernie Thompson   staging: udlfb: a...
842
  		return -EBUSY;
3e8f3d6fa   Bernie Thompson   Staging: udlfb: S...
843

33077b8d3   Bernie Thompson   staging: udlfb: r...
844
845
846
  	/* If the USB device is gone, we don't accept new opens */
  	if (dev->virtualized)
  		return -ENODEV;
3e8f3d6fa   Bernie Thompson   Staging: udlfb: S...
847
848
  
  	dev->fb_count++;
33077b8d3   Bernie Thompson   staging: udlfb: r...
849
  	kref_get(&dev->kref);
d5ed54322   Bernie Thompson   staging: udlfb: a...
850
  	if (fb_defio && (info->fbdefio == NULL)) {
5bea1fbf9   Bernie Thompson   staging: udlfb: f...
851
852
853
  		/* enable defio at last moment if not disabled by client */
  
  		struct fb_deferred_io *fbdefio;
31a9f47aa   Joe Perches   Staging: udlfb.c:...
854
  		fbdefio = kmalloc(sizeof(struct fb_deferred_io), GFP_KERNEL);
5bea1fbf9   Bernie Thompson   staging: udlfb: f...
855
856
857
858
859
860
861
  
  		if (fbdefio) {
  			fbdefio->delay = DL_DEFIO_WRITE_DELAY;
  			fbdefio->deferred_io = dlfb_dpy_deferred_io;
  		}
  
  		info->fbdefio = fbdefio;
3e8f3d6fa   Bernie Thompson   Staging: udlfb: S...
862
863
  		fb_deferred_io_init(info);
  	}
3e8f3d6fa   Bernie Thompson   Staging: udlfb: S...
864

81f6f3c10   Paul Mundt   video: udlfb: Kil...
865
866
  	pr_notice("open /dev/fb%d user=%d fb_info=%p count=%d
  ",
3e8f3d6fa   Bernie Thompson   Staging: udlfb: S...
867
  	    info->node, user, info, dev->fb_count);
88e58b1a4   Roberto De Ioris   Staging: add udlf...
868
869
  	return 0;
  }
4a4854dd2   Bernie Thompson   Staging: udlfb: p...
870
871
872
873
874
  /*
   * Called when all client interfaces to start transactions have been disabled,
   * and all references to our device instance (dlfb_data) are released.
   * Every transaction must have a reference, so we know are fully spun down
   */
33077b8d3   Bernie Thompson   staging: udlfb: r...
875
  static void dlfb_free(struct kref *kref)
4a4854dd2   Bernie Thompson   Staging: udlfb: p...
876
877
  {
  	struct dlfb_data *dev = container_of(kref, struct dlfb_data, kref);
33077b8d3   Bernie Thompson   staging: udlfb: r...
878
879
880
  	/* this function will wait for all in-flight urbs to complete */
  	if (dev->urbs.count > 0)
  		dlfb_free_urb_list(dev);
4a4854dd2   Bernie Thompson   Staging: udlfb: p...
881
882
  	if (dev->backing_buffer)
  		vfree(dev->backing_buffer);
33077b8d3   Bernie Thompson   staging: udlfb: r...
883
  	kfree(dev->edid);
81f6f3c10   Paul Mundt   video: udlfb: Kil...
884
885
  	pr_warn("freeing dlfb_data %p
  ", dev);
3e8f3d6fa   Bernie Thompson   Staging: udlfb: S...
886

4a4854dd2   Bernie Thompson   Staging: udlfb: p...
887
888
  	kfree(dev);
  }
5bea1fbf9   Bernie Thompson   staging: udlfb: f...
889
890
891
892
893
894
895
  static void dlfb_release_urb_work(struct work_struct *work)
  {
  	struct urb_node *unode = container_of(work, struct urb_node,
  					      release_urb_work.work);
  
  	up(&unode->dev->urbs.limit_sem);
  }
33077b8d3   Bernie Thompson   staging: udlfb: r...
896
897
  
  static void dlfb_free_framebuffer_work(struct work_struct *work)
2469d5dbc   Bernie Thompson   Staging: udlfb: R...
898
  {
33077b8d3   Bernie Thompson   staging: udlfb: r...
899
900
901
902
903
904
  	struct dlfb_data *dev = container_of(work, struct dlfb_data,
  					     free_framebuffer_work.work);
  	struct fb_info *info = dev->info;
  	int node = info->node;
  
  	unregister_framebuffer(info);
2469d5dbc   Bernie Thompson   Staging: udlfb: R...
905
906
907
908
909
910
911
912
913
  
  	if (info->cmap.len != 0)
  		fb_dealloc_cmap(&info->cmap);
  	if (info->monspecs.modedb)
  		fb_destroy_modedb(info->monspecs.modedb);
  	if (info->screen_base)
  		vfree(info->screen_base);
  
  	fb_destroy_modelist(&info->modelist);
33077b8d3   Bernie Thompson   staging: udlfb: r...
914
915
916
  	dev->info = 0;
  
  	/* Assume info structure is freed after this point */
2469d5dbc   Bernie Thompson   Staging: udlfb: R...
917
  	framebuffer_release(info);
81f6f3c10   Paul Mundt   video: udlfb: Kil...
918
919
  	pr_warn("fb_info for /dev/fb%d has been freed
  ", node);
33077b8d3   Bernie Thompson   staging: udlfb: r...
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
  
  	/* ref taken in probe() as part of registering framebfufer */
  	kref_put(&dev->kref, dlfb_free);
  }
  
  /*
   * Assumes caller is holding info->lock mutex (for open and release at least)
   */
  static int dlfb_ops_release(struct fb_info *info, int user)
  {
  	struct dlfb_data *dev = info->par;
  
  	dev->fb_count--;
  
  	/* We can't free fb_info here - fbmem will touch it when we return */
  	if (dev->virtualized && (dev->fb_count == 0))
  		schedule_delayed_work(&dev->free_framebuffer_work, HZ);
33077b8d3   Bernie Thompson   staging: udlfb: r...
937
938
939
940
941
942
  	if ((dev->fb_count == 0) && (info->fbdefio)) {
  		fb_deferred_io_cleanup(info);
  		kfree(info->fbdefio);
  		info->fbdefio = NULL;
  		info->fbops->fb_mmap = dlfb_ops_mmap;
  	}
33077b8d3   Bernie Thompson   staging: udlfb: r...
943

81f6f3c10   Paul Mundt   video: udlfb: Kil...
944
945
  	pr_warn("released /dev/fb%d user=%d count=%d
  ",
33077b8d3   Bernie Thompson   staging: udlfb: r...
946
947
948
949
950
  		  info->node, user, dev->fb_count);
  
  	kref_put(&dev->kref, dlfb_free);
  
  	return 0;
2469d5dbc   Bernie Thompson   Staging: udlfb: R...
951
952
953
  }
  
  /*
7d9485e2c   Bernie Thompson   Staging: udlfb: A...
954
955
956
957
958
959
960
   * Check whether a video mode is supported by the DisplayLink chip
   * We start from monitor's modes, so don't need to filter that here
   */
  static int dlfb_is_valid_mode(struct fb_videomode *mode,
  		struct fb_info *info)
  {
  	struct dlfb_data *dev = info->par;
18dffdf89   Bernie Thompson   staging: udlfb: e...
961
  	if (mode->xres * mode->yres > dev->sku_pixel_limit) {
81f6f3c10   Paul Mundt   video: udlfb: Kil...
962
963
  		pr_warn("%dx%d beyond chip capabilities
  ",
18dffdf89   Bernie Thompson   staging: udlfb: e...
964
  		       mode->xres, mode->yres);
7d9485e2c   Bernie Thompson   Staging: udlfb: A...
965
  		return 0;
18dffdf89   Bernie Thompson   staging: udlfb: e...
966
  	}
81f6f3c10   Paul Mundt   video: udlfb: Kil...
967
968
  	pr_info("%dx%d valid mode
  ", mode->xres, mode->yres);
7d9485e2c   Bernie Thompson   Staging: udlfb: A...
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
  
  	return 1;
  }
  
  static void dlfb_var_color_format(struct fb_var_screeninfo *var)
  {
  	const struct fb_bitfield red = { 11, 5, 0 };
  	const struct fb_bitfield green = { 5, 6, 0 };
  	const struct fb_bitfield blue = { 0, 5, 0 };
  
  	var->bits_per_pixel = 16;
  	var->red = red;
  	var->green = green;
  	var->blue = blue;
  }
2469d5dbc   Bernie Thompson   Staging: udlfb: R...
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
  static int dlfb_ops_check_var(struct fb_var_screeninfo *var,
  				struct fb_info *info)
  {
  	struct fb_videomode mode;
  
  	/* TODO: support dynamically changing framebuffer size */
  	if ((var->xres * var->yres * 2) > info->fix.smem_len)
  		return -EINVAL;
  
  	/* set device-specific elements of var unrelated to mode */
  	dlfb_var_color_format(var);
  
  	fb_var_to_videomode(&mode, var);
  
  	if (!dlfb_is_valid_mode(&mode, info))
  		return -EINVAL;
  
  	return 0;
  }
  
  static int dlfb_ops_set_par(struct fb_info *info)
  {
  	struct dlfb_data *dev = info->par;
18dffdf89   Bernie Thompson   staging: udlfb: e...
1007
1008
1009
  	int result;
  	u16 *pix_framebuffer;
  	int i;
2469d5dbc   Bernie Thompson   Staging: udlfb: R...
1010

81f6f3c10   Paul Mundt   video: udlfb: Kil...
1011
1012
  	pr_notice("set_par mode %dx%d
  ", info->var.xres, info->var.yres);
2469d5dbc   Bernie Thompson   Staging: udlfb: R...
1013

18dffdf89   Bernie Thompson   staging: udlfb: e...
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
  	result = dlfb_set_video_mode(dev, &info->var);
  
  	if ((result == 0) && (dev->fb_count == 0)) {
  
  		/* paint greenscreen */
  
  		pix_framebuffer = (u16 *) info->screen_base;
  		for (i = 0; i < info->fix.smem_len / 2; i++)
  			pix_framebuffer[i] = 0x37e6;
  
  		dlfb_handle_damage(dev, 0, 0, info->var.xres, info->var.yres,
  				   info->screen_base);
  	}
  
  	return result;
2469d5dbc   Bernie Thompson   Staging: udlfb: R...
1029
  }
58e7c3b00   Bernie Thompson   udlfb: add more c...
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
  /* To fonzi the jukebox (e.g. make blanking changes take effect) */
  static char *dlfb_dummy_render(char *buf)
  {
  	*buf++ = 0xAF;
  	*buf++ = 0x6A; /* copy */
  	*buf++ = 0x00; /* from address*/
  	*buf++ = 0x00;
  	*buf++ = 0x00;
  	*buf++ = 0x01; /* one pixel */
  	*buf++ = 0x00; /* to address */
  	*buf++ = 0x00;
  	*buf++ = 0x00;
  	return buf;
  }
9825f70f5   Bernie Thompson   staging: udlfb: a...
1044
1045
1046
  /*
   * In order to come back from full DPMS off, we need to set the mode again
   */
4574203f4   Bernie Thompson   Staging: udlfb: c...
1047
  static int dlfb_ops_blank(int blank_mode, struct fb_info *info)
f05e0575e   Greg Kroah-Hartman   Staging: udlfb: c...
1048
  {
530f43a8a   Bernie Thompson   Staging: udlfb: i...
1049
  	struct dlfb_data *dev = info->par;
58e7c3b00   Bernie Thompson   udlfb: add more c...
1050
1051
  	char *bufptr;
  	struct urb *urb;
7316bc55e   Roberto De Ioris   Staging: udlfb: u...
1052

58e7c3b00   Bernie Thompson   udlfb: add more c...
1053
1054
1055
  	pr_info("/dev/fb%d FB_BLANK mode %d --> %d
  ",
  		info->node, dev->blank_mode, blank_mode);
530f43a8a   Bernie Thompson   Staging: udlfb: i...
1056

58e7c3b00   Bernie Thompson   udlfb: add more c...
1057
1058
  	if ((dev->blank_mode == FB_BLANK_POWERDOWN) &&
  	    (blank_mode != FB_BLANK_POWERDOWN)) {
9825f70f5   Bernie Thompson   staging: udlfb: a...
1059

58e7c3b00   Bernie Thompson   udlfb: add more c...
1060
  		/* returning from powerdown requires a fresh modeset */
9825f70f5   Bernie Thompson   staging: udlfb: a...
1061
  		dlfb_set_video_mode(dev, &info->var);
7316bc55e   Roberto De Ioris   Staging: udlfb: u...
1062
  	}
7316bc55e   Roberto De Ioris   Staging: udlfb: u...
1063

58e7c3b00   Bernie Thompson   udlfb: add more c...
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
  	urb = dlfb_get_urb(dev);
  	if (!urb)
  		return 0;
  
  	bufptr = (char *) urb->transfer_buffer;
  	bufptr = dlfb_vidreg_lock(bufptr);
  	bufptr = dlfb_blanking(bufptr, blank_mode);
  	bufptr = dlfb_vidreg_unlock(bufptr);
  
  	/* seems like a render op is needed to have blank change take effect */
  	bufptr = dlfb_dummy_render(bufptr);
  
  	dlfb_submit_urb(dev, urb, bufptr -
  			(char *) urb->transfer_buffer);
  
  	dev->blank_mode = blank_mode;
88e58b1a4   Roberto De Ioris   Staging: add udlf...
1080
1081
1082
1083
  	return 0;
  }
  
  static struct fb_ops dlfb_ops = {
2469d5dbc   Bernie Thompson   Staging: udlfb: R...
1084
  	.owner = THIS_MODULE,
1a3e528cf   Paul Mundt   video: udlfb: dei...
1085
  	.fb_read = fb_sys_read,
d46ecb9f7   Bernie Thompson   staging: udlfb: f...
1086
  	.fb_write = dlfb_ops_write,
4574203f4   Bernie Thompson   Staging: udlfb: c...
1087
1088
1089
1090
1091
1092
  	.fb_setcolreg = dlfb_ops_setcolreg,
  	.fb_fillrect = dlfb_ops_fillrect,
  	.fb_copyarea = dlfb_ops_copyarea,
  	.fb_imageblit = dlfb_ops_imageblit,
  	.fb_mmap = dlfb_ops_mmap,
  	.fb_ioctl = dlfb_ops_ioctl,
3e8f3d6fa   Bernie Thompson   Staging: udlfb: S...
1093
  	.fb_open = dlfb_ops_open,
4574203f4   Bernie Thompson   Staging: udlfb: c...
1094
1095
  	.fb_release = dlfb_ops_release,
  	.fb_blank = dlfb_ops_blank,
2469d5dbc   Bernie Thompson   Staging: udlfb: R...
1096
1097
  	.fb_check_var = dlfb_ops_check_var,
  	.fb_set_par = dlfb_ops_set_par,
88e58b1a4   Roberto De Ioris   Staging: add udlf...
1098
  };
18dffdf89   Bernie Thompson   staging: udlfb: e...
1099

cc403dc67   Bernie Thompson   Staging: udlfb: r...
1100
  /*
18dffdf89   Bernie Thompson   staging: udlfb: e...
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
   * Assumes &info->lock held by caller
   * Assumes no active clients have framebuffer open
   */
  static int dlfb_realloc_framebuffer(struct dlfb_data *dev, struct fb_info *info)
  {
  	int retval = -ENOMEM;
  	int old_len = info->fix.smem_len;
  	int new_len;
  	unsigned char *old_fb = info->screen_base;
  	unsigned char *new_fb;
d3189545e   Stuart Hopkins   udlfb: Add module...
1111
  	unsigned char *new_back = 0;
18dffdf89   Bernie Thompson   staging: udlfb: e...
1112

81f6f3c10   Paul Mundt   video: udlfb: Kil...
1113
1114
  	pr_warn("Reallocating framebuffer. Addresses will change!
  ");
18dffdf89   Bernie Thompson   staging: udlfb: e...
1115
1116
1117
1118
1119
1120
1121
1122
1123
  
  	new_len = info->fix.line_length * info->var.yres;
  
  	if (PAGE_ALIGN(new_len) > old_len) {
  		/*
  		 * Alloc system memory for virtual framebuffer
  		 */
  		new_fb = vmalloc(new_len);
  		if (!new_fb) {
81f6f3c10   Paul Mundt   video: udlfb: Kil...
1124
1125
  			pr_err("Virtual framebuffer alloc failed
  ");
18dffdf89   Bernie Thompson   staging: udlfb: e...
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
  			goto error;
  		}
  
  		if (info->screen_base) {
  			memcpy(new_fb, old_fb, old_len);
  			vfree(info->screen_base);
  		}
  
  		info->screen_base = new_fb;
  		info->fix.smem_len = PAGE_ALIGN(new_len);
  		info->fix.smem_start = (unsigned long) new_fb;
  		info->flags = udlfb_info_flags;
  
  		/*
  		 * Second framebuffer copy to mirror the framebuffer state
  		 * on the physical USB device. We can function without this.
  		 * But with imperfect damage info we may send pixels over USB
  		 * that were, in fact, unchanged - wasting limited USB bandwidth
  		 */
d3189545e   Stuart Hopkins   udlfb: Add module...
1145
1146
  		if (shadow)
  			new_back = vzalloc(new_len);
18dffdf89   Bernie Thompson   staging: udlfb: e...
1147
  		if (!new_back)
949f6711b   Linus Torvalds   Merge branch 'sta...
1148
1149
  			pr_info("No shadow/backing buffer allocated
  ");
18dffdf89   Bernie Thompson   staging: udlfb: e...
1150
1151
1152
1153
  		else {
  			if (dev->backing_buffer)
  				vfree(dev->backing_buffer);
  			dev->backing_buffer = new_back;
18dffdf89   Bernie Thompson   staging: udlfb: e...
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
  		}
  	}
  
  	retval = 0;
  
  error:
  	return retval;
  }
  
  /*
   * 1) Get EDID from hw, or use sw default
   * 2) Parse into various fb_info structs
   * 3) Allocate virtual framebuffer memory to back highest res mode
   *
   * Parses EDID into three places used by various parts of fbdev:
7d9485e2c   Bernie Thompson   Staging: udlfb: A...
1169
1170
1171
1172
1173
1174
   * fb_var_screeninfo contains the timing of the monitor's preferred mode
   * fb_info.monspecs is full parsed EDID info, including monspecs.modedb
   * fb_info.modelist is a linked list of all monitor & VESA modes which work
   *
   * If EDID is not readable/valid, then modelist is all VESA modes,
   * monspecs is NULL, and fb_var_screeninfo is set to safe VESA mode
18dffdf89   Bernie Thompson   staging: udlfb: e...
1175
   * Returns 0 if successful
7d9485e2c   Bernie Thompson   Staging: udlfb: A...
1176
   */
18dffdf89   Bernie Thompson   staging: udlfb: e...
1177
1178
1179
  static int dlfb_setup_modes(struct dlfb_data *dev,
  			   struct fb_info *info,
  			   char *default_edid, size_t default_edid_size)
7d9485e2c   Bernie Thompson   Staging: udlfb: A...
1180
1181
1182
1183
  {
  	int i;
  	const struct fb_videomode *default_vmode = NULL;
  	int result = 0;
18dffdf89   Bernie Thompson   staging: udlfb: e...
1184
1185
1186
1187
1188
  	char *edid;
  	int tries = 3;
  
  	if (info->dev) /* only use mutex if info has been registered */
  		mutex_lock(&info->lock);
b9f03a3cd   Paul Mundt   video: udlfb: Kil...
1189
  	edid = kmalloc(EDID_LENGTH, GFP_KERNEL);
18dffdf89   Bernie Thompson   staging: udlfb: e...
1190
1191
1192
1193
  	if (!edid) {
  		result = -ENOMEM;
  		goto error;
  	}
7d9485e2c   Bernie Thompson   Staging: udlfb: A...
1194
1195
1196
  
  	fb_destroy_modelist(&info->modelist);
  	memset(&info->monspecs, 0, sizeof(info->monspecs));
18dffdf89   Bernie Thompson   staging: udlfb: e...
1197
1198
1199
1200
1201
1202
  	/*
  	 * Try to (re)read EDID from hardware first
  	 * EDID data may return, but not parse as valid
  	 * Try again a few times, in case of e.g. analog cable noise
  	 */
  	while (tries--) {
b9f03a3cd   Paul Mundt   video: udlfb: Kil...
1203
  		i = dlfb_get_edid(dev, edid, EDID_LENGTH);
18dffdf89   Bernie Thompson   staging: udlfb: e...
1204

b9f03a3cd   Paul Mundt   video: udlfb: Kil...
1205
  		if (i >= EDID_LENGTH)
18dffdf89   Bernie Thompson   staging: udlfb: e...
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
  			fb_edid_to_monspecs(edid, &info->monspecs);
  
  		if (info->monspecs.modedb_len > 0) {
  			dev->edid = edid;
  			dev->edid_size = i;
  			break;
  		}
  	}
  
  	/* If that fails, use a previously returned EDID if available */
  	if (info->monspecs.modedb_len == 0) {
81f6f3c10   Paul Mundt   video: udlfb: Kil...
1217
1218
  		pr_err("Unable to get valid EDID from device/display
  ");
18dffdf89   Bernie Thompson   staging: udlfb: e...
1219
1220
1221
1222
  
  		if (dev->edid) {
  			fb_edid_to_monspecs(dev->edid, &info->monspecs);
  			if (info->monspecs.modedb_len > 0)
81f6f3c10   Paul Mundt   video: udlfb: Kil...
1223
1224
  				pr_err("Using previously queried EDID
  ");
18dffdf89   Bernie Thompson   staging: udlfb: e...
1225
1226
1227
1228
1229
  		}
  	}
  
  	/* If that fails, use the default EDID we were handed */
  	if (info->monspecs.modedb_len == 0) {
b9f03a3cd   Paul Mundt   video: udlfb: Kil...
1230
  		if (default_edid_size >= EDID_LENGTH) {
18dffdf89   Bernie Thompson   staging: udlfb: e...
1231
1232
1233
1234
1235
  			fb_edid_to_monspecs(default_edid, &info->monspecs);
  			if (info->monspecs.modedb_len > 0) {
  				memcpy(edid, default_edid, default_edid_size);
  				dev->edid = edid;
  				dev->edid_size = default_edid_size;
81f6f3c10   Paul Mundt   video: udlfb: Kil...
1236
1237
  				pr_err("Using default/backup EDID
  ");
18dffdf89   Bernie Thompson   staging: udlfb: e...
1238
1239
1240
  			}
  		}
  	}
7d9485e2c   Bernie Thompson   Staging: udlfb: A...
1241

18dffdf89   Bernie Thompson   staging: udlfb: e...
1242
  	/* If we've got modes, let's pick a best default mode */
7d9485e2c   Bernie Thompson   Staging: udlfb: A...
1243
1244
1245
1246
1247
1248
  	if (info->monspecs.modedb_len > 0) {
  
  		for (i = 0; i < info->monspecs.modedb_len; i++) {
  			if (dlfb_is_valid_mode(&info->monspecs.modedb[i], info))
  				fb_add_videomode(&info->monspecs.modedb[i],
  					&info->modelist);
9377c5175   William Katsak   udlfb: Correct su...
1249
1250
1251
1252
1253
1254
  			else {
  				if (i == 0)
  					/* if we've removed top/best mode */
  					info->monspecs.misc
  						&= ~FB_MISC_1ST_DETAIL;
  			}
7d9485e2c   Bernie Thompson   Staging: udlfb: A...
1255
1256
1257
1258
  		}
  
  		default_vmode = fb_find_best_display(&info->monspecs,
  						     &info->modelist);
18dffdf89   Bernie Thompson   staging: udlfb: e...
1259
  	}
7d9485e2c   Bernie Thompson   Staging: udlfb: A...
1260

18dffdf89   Bernie Thompson   staging: udlfb: e...
1261
1262
1263
1264
  	/* If everything else has failed, fall back to safe default mode */
  	if (default_vmode == NULL) {
  
  		struct fb_videomode fb_vmode = {0};
7d9485e2c   Bernie Thompson   Staging: udlfb: A...
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
  
  		/*
  		 * Add the standard VESA modes to our modelist
  		 * Since we don't have EDID, there may be modes that
  		 * overspec monitor and/or are incorrect aspect ratio, etc.
  		 * But at least the user has a chance to choose
  		 */
  		for (i = 0; i < VESA_MODEDB_SIZE; i++) {
  			if (dlfb_is_valid_mode((struct fb_videomode *)
  						&vesa_modes[i], info))
  				fb_add_videomode(&vesa_modes[i],
  						 &info->modelist);
  		}
  
  		/*
  		 * default to resolution safe for projectors
  		 * (since they are most common case without EDID)
  		 */
  		fb_vmode.xres = 800;
  		fb_vmode.yres = 600;
  		fb_vmode.refresh = 60;
  		default_vmode = fb_find_nearest_mode(&fb_vmode,
  						     &info->modelist);
  	}
18dffdf89   Bernie Thompson   staging: udlfb: e...
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
  	/* If we have good mode and no active clients*/
  	if ((default_vmode != NULL) && (dev->fb_count == 0)) {
  
  		fb_videomode_to_var(&info->var, default_vmode);
  		dlfb_var_color_format(&info->var);
  
  		/*
  		 * with mode size info, we can now alloc our framebuffer.
  		 */
  		memcpy(&info->fix, &dlfb_fix, sizeof(dlfb_fix));
  		info->fix.line_length = info->var.xres *
  			(info->var.bits_per_pixel / 8);
  
  		result = dlfb_realloc_framebuffer(dev, info);
  
  	} else
  		result = -EINVAL;
  
  error:
  	if (edid && (dev->edid != edid))
  		kfree(edid);
  
  	if (info->dev)
  		mutex_unlock(&info->lock);
7d9485e2c   Bernie Thompson   Staging: udlfb: A...
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
  
  	return result;
  }
  
  static ssize_t metrics_bytes_rendered_show(struct device *fbdev,
  				   struct device_attribute *a, char *buf) {
  	struct fb_info *fb_info = dev_get_drvdata(fbdev);
  	struct dlfb_data *dev = fb_info->par;
  	return snprintf(buf, PAGE_SIZE, "%u
  ",
  			atomic_read(&dev->bytes_rendered));
  }
  
  static ssize_t metrics_bytes_identical_show(struct device *fbdev,
  				   struct device_attribute *a, char *buf) {
  	struct fb_info *fb_info = dev_get_drvdata(fbdev);
  	struct dlfb_data *dev = fb_info->par;
  	return snprintf(buf, PAGE_SIZE, "%u
  ",
  			atomic_read(&dev->bytes_identical));
  }
  
  static ssize_t metrics_bytes_sent_show(struct device *fbdev,
  				   struct device_attribute *a, char *buf) {
  	struct fb_info *fb_info = dev_get_drvdata(fbdev);
  	struct dlfb_data *dev = fb_info->par;
  	return snprintf(buf, PAGE_SIZE, "%u
  ",
  			atomic_read(&dev->bytes_sent));
  }
  
  static ssize_t metrics_cpu_kcycles_used_show(struct device *fbdev,
  				   struct device_attribute *a, char *buf) {
  	struct fb_info *fb_info = dev_get_drvdata(fbdev);
  	struct dlfb_data *dev = fb_info->par;
  	return snprintf(buf, PAGE_SIZE, "%u
  ",
  			atomic_read(&dev->cpu_kcycles_used));
  }
18dffdf89   Bernie Thompson   staging: udlfb: e...
1352
1353
1354
  static ssize_t edid_show(
  			struct file *filp,
  			struct kobject *kobj, struct bin_attribute *a,
7d9485e2c   Bernie Thompson   Staging: udlfb: A...
1355
1356
1357
1358
  			 char *buf, loff_t off, size_t count) {
  	struct device *fbdev = container_of(kobj, struct device, kobj);
  	struct fb_info *fb_info = dev_get_drvdata(fbdev);
  	struct dlfb_data *dev = fb_info->par;
7d9485e2c   Bernie Thompson   Staging: udlfb: A...
1359

18dffdf89   Bernie Thompson   staging: udlfb: e...
1360
  	if (dev->edid == NULL)
7d9485e2c   Bernie Thompson   Staging: udlfb: A...
1361
  		return 0;
18dffdf89   Bernie Thompson   staging: udlfb: e...
1362
  	if ((off >= dev->edid_size) || (count > dev->edid_size))
7d9485e2c   Bernie Thompson   Staging: udlfb: A...
1363
  		return 0;
18dffdf89   Bernie Thompson   staging: udlfb: e...
1364
1365
  	if (off + count > dev->edid_size)
  		count = dev->edid_size - off;
81f6f3c10   Paul Mundt   video: udlfb: Kil...
1366
1367
  	pr_info("sysfs edid copy %p to %p, %d bytes
  ",
18dffdf89   Bernie Thompson   staging: udlfb: e...
1368
1369
1370
  		dev->edid, buf, (int) count);
  
  	memcpy(buf, dev->edid, count);
7d9485e2c   Bernie Thompson   Staging: udlfb: A...
1371
1372
1373
  
  	return count;
  }
8ef8cc4fc   Bernie Thompson   staging: udlfb: s...
1374
1375
1376
1377
1378
1379
1380
1381
1382
  static ssize_t edid_store(
  			struct file *filp,
  			struct kobject *kobj, struct bin_attribute *a,
  			char *src, loff_t src_off, size_t src_size) {
  	struct device *fbdev = container_of(kobj, struct device, kobj);
  	struct fb_info *fb_info = dev_get_drvdata(fbdev);
  	struct dlfb_data *dev = fb_info->par;
  
  	/* We only support write of entire EDID at once, no offset*/
b9f03a3cd   Paul Mundt   video: udlfb: Kil...
1383
  	if ((src_size != EDID_LENGTH) || (src_off != 0))
8ef8cc4fc   Bernie Thompson   staging: udlfb: s...
1384
1385
1386
1387
1388
  		return 0;
  
  	dlfb_setup_modes(dev, fb_info, src, src_size);
  
  	if (dev->edid && (memcmp(src, dev->edid, src_size) == 0)) {
81f6f3c10   Paul Mundt   video: udlfb: Kil...
1389
1390
  		pr_info("sysfs written EDID is new default
  ");
8ef8cc4fc   Bernie Thompson   staging: udlfb: s...
1391
1392
1393
1394
1395
  		dlfb_ops_set_par(fb_info);
  		return src_size;
  	} else
  		return 0;
  }
7d9485e2c   Bernie Thompson   Staging: udlfb: A...
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
  
  static ssize_t metrics_reset_store(struct device *fbdev,
  			   struct device_attribute *attr,
  			   const char *buf, size_t count)
  {
  	struct fb_info *fb_info = dev_get_drvdata(fbdev);
  	struct dlfb_data *dev = fb_info->par;
  
  	atomic_set(&dev->bytes_rendered, 0);
  	atomic_set(&dev->bytes_identical, 0);
  	atomic_set(&dev->bytes_sent, 0);
  	atomic_set(&dev->cpu_kcycles_used, 0);
7d9485e2c   Bernie Thompson   Staging: udlfb: A...
1408
1409
1410
  
  	return count;
  }
7d9485e2c   Bernie Thompson   Staging: udlfb: A...
1411
1412
  static struct bin_attribute edid_attr = {
  	.attr.name = "edid",
8ef8cc4fc   Bernie Thompson   staging: udlfb: s...
1413
  	.attr.mode = 0666,
b9f03a3cd   Paul Mundt   video: udlfb: Kil...
1414
  	.size = EDID_LENGTH,
7d9485e2c   Bernie Thompson   Staging: udlfb: A...
1415
  	.read = edid_show,
8ef8cc4fc   Bernie Thompson   staging: udlfb: s...
1416
  	.write = edid_store
7d9485e2c   Bernie Thompson   Staging: udlfb: A...
1417
1418
1419
1420
1421
1422
1423
  };
  
  static struct device_attribute fb_device_attrs[] = {
  	__ATTR_RO(metrics_bytes_rendered),
  	__ATTR_RO(metrics_bytes_identical),
  	__ATTR_RO(metrics_bytes_sent),
  	__ATTR_RO(metrics_cpu_kcycles_used),
926c11151   Greg Kroah-Hartman   Staging: udlfb: f...
1424
  	__ATTR(metrics_reset, S_IWUSR, NULL, metrics_reset_store),
7d9485e2c   Bernie Thompson   Staging: udlfb: A...
1425
1426
1427
  };
  
  /*
cc403dc67   Bernie Thompson   Staging: udlfb: r...
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
   * This is necessary before we can communicate with the display controller.
   */
  static int dlfb_select_std_channel(struct dlfb_data *dev)
  {
  	int ret;
  	u8 set_def_chn[] = {	   0x57, 0xCD, 0xDC, 0xA7,
  				0x1C, 0x88, 0x5E, 0x15,
  				0x60, 0xFE, 0xC6, 0x97,
  				0x16, 0x3D, 0x47, 0xF2  };
  
  	ret = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
  			NR_USB_REQUEST_CHANNEL,
  			(USB_DIR_OUT | USB_TYPE_VENDOR), 0, 0,
  			set_def_chn, sizeof(set_def_chn), USB_CTRL_SET_TIMEOUT);
  	return ret;
  }
18dffdf89   Bernie Thompson   staging: udlfb: e...
1444
  static int dlfb_parse_vendor_descriptor(struct dlfb_data *dev,
f2e1fc9d5   Andrew Kephart   udlfb: Search con...
1445
  					struct usb_interface *interface)
18dffdf89   Bernie Thompson   staging: udlfb: e...
1446
1447
1448
1449
  {
  	char *desc;
  	char *buf;
  	char *desc_end;
f2e1fc9d5   Andrew Kephart   udlfb: Search con...
1450
  	int total_len = 0;
18dffdf89   Bernie Thompson   staging: udlfb: e...
1451
1452
1453
1454
1455
  
  	buf = kzalloc(MAX_VENDOR_DESCRIPTOR_SIZE, GFP_KERNEL);
  	if (!buf)
  		return false;
  	desc = buf;
f2e1fc9d5   Andrew Kephart   udlfb: Search con...
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
  	total_len = usb_get_descriptor(interface_to_usbdev(interface),
  					0x5f, /* vendor specific */
  					0, desc, MAX_VENDOR_DESCRIPTOR_SIZE);
  
  	/* if not found, look in configuration descriptor */
  	if (total_len < 0) {
  		if (0 == usb_get_extra_descriptor(interface->cur_altsetting,
  			0x5f, &desc))
  			total_len = (int) desc[0];
  	}
18dffdf89   Bernie Thompson   staging: udlfb: e...
1466
  	if (total_len > 5) {
81f6f3c10   Paul Mundt   video: udlfb: Kil...
1467
  		pr_info("vendor descriptor length:%x data:%02x %02x %02x %02x" \
18dffdf89   Bernie Thompson   staging: udlfb: e...
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
  			"%02x %02x %02x %02x %02x %02x %02x
  ",
  			total_len, desc[0],
  			desc[1], desc[2], desc[3], desc[4], desc[5], desc[6],
  			desc[7], desc[8], desc[9], desc[10]);
  
  		if ((desc[0] != total_len) || /* descriptor length */
  		    (desc[1] != 0x5f) ||   /* vendor descriptor type */
  		    (desc[2] != 0x01) ||   /* version (2 bytes) */
  		    (desc[3] != 0x00) ||
  		    (desc[4] != total_len - 2)) /* length after type */
  			goto unrecognized;
  
  		desc_end = desc + total_len;
  		desc += 5; /* the fixed header we've already parsed */
  
  		while (desc < desc_end) {
  			u8 length;
  			u16 key;
  
  			key = *((u16 *) desc);
  			desc += sizeof(u16);
  			length = *desc;
  			desc++;
  
  			switch (key) {
  			case 0x0200: { /* max_area */
  				u32 max_area;
  				max_area = le32_to_cpu(*((u32 *)desc));
81f6f3c10   Paul Mundt   video: udlfb: Kil...
1497
1498
  				pr_warn("DL chip limited to %d pixel modes
  ",
18dffdf89   Bernie Thompson   staging: udlfb: e...
1499
1500
1501
1502
1503
1504
1505
1506
1507
  					max_area);
  				dev->sku_pixel_limit = max_area;
  				break;
  			}
  			default:
  				break;
  			}
  			desc += length;
  		}
f2e1fc9d5   Andrew Kephart   udlfb: Search con...
1508
1509
1510
  	} else {
  		pr_info("vendor descriptor not available (%d)
  ", total_len);
18dffdf89   Bernie Thompson   staging: udlfb: e...
1511
1512
1513
  	}
  
  	goto success;
2469d5dbc   Bernie Thompson   Staging: udlfb: R...
1514

18dffdf89   Bernie Thompson   staging: udlfb: e...
1515
1516
  unrecognized:
  	/* allow udlfb to load for now even if firmware unrecognized */
81f6f3c10   Paul Mundt   video: udlfb: Kil...
1517
1518
  	pr_err("Unrecognized vendor firmware descriptor
  ");
18dffdf89   Bernie Thompson   staging: udlfb: e...
1519
1520
1521
1522
1523
  
  success:
  	kfree(buf);
  	return true;
  }
2469d5dbc   Bernie Thompson   Staging: udlfb: R...
1524
  static int dlfb_usb_probe(struct usb_interface *interface,
59277b679   Bernie Thompson   Staging: udlfb: a...
1525
  			const struct usb_device_id *id)
88e58b1a4   Roberto De Ioris   Staging: add udlf...
1526
  {
59277b679   Bernie Thompson   Staging: udlfb: a...
1527
  	struct usb_device *usbdev;
18dffdf89   Bernie Thompson   staging: udlfb: e...
1528
  	struct dlfb_data *dev = 0;
33077b8d3   Bernie Thompson   staging: udlfb: r...
1529
  	struct fb_info *info = 0;
59277b679   Bernie Thompson   Staging: udlfb: a...
1530
  	int retval = -ENOMEM;
2685cffa9   Bernie Thompson   staging: udlfb: f...
1531
  	int i;
59277b679   Bernie Thompson   Staging: udlfb: a...
1532

2469d5dbc   Bernie Thompson   Staging: udlfb: R...
1533
1534
1535
  	/* usb initialization */
  
  	usbdev = interface_to_usbdev(interface);
59277b679   Bernie Thompson   Staging: udlfb: a...
1536
1537
1538
  
  	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  	if (dev == NULL) {
2469d5dbc   Bernie Thompson   Staging: udlfb: R...
1539
1540
1541
  		err("dlfb_usb_probe: failed alloc of dev struct
  ");
  		goto error;
88e58b1a4   Roberto De Ioris   Staging: add udlf...
1542
  	}
88e58b1a4   Roberto De Ioris   Staging: add udlf...
1543

2469d5dbc   Bernie Thompson   Staging: udlfb: R...
1544
1545
  	/* we need to wait for both usb and fbdev to spin down on disconnect */
  	kref_init(&dev->kref); /* matching kref_put in usb .disconnect fn */
18dffdf89   Bernie Thompson   staging: udlfb: e...
1546
  	kref_get(&dev->kref); /* matching kref_put in free_framebuffer_work */
2469d5dbc   Bernie Thompson   Staging: udlfb: R...
1547

59277b679   Bernie Thompson   Staging: udlfb: a...
1548
  	dev->udev = usbdev;
4a4854dd2   Bernie Thompson   Staging: udlfb: p...
1549
  	dev->gdev = &usbdev->dev; /* our generic struct device * */
59277b679   Bernie Thompson   Staging: udlfb: a...
1550
  	usb_set_intfdata(interface, dev);
81f6f3c10   Paul Mundt   video: udlfb: Kil...
1551
1552
  	pr_info("%s %s - serial #%s
  ",
18dffdf89   Bernie Thompson   staging: udlfb: e...
1553
  		usbdev->manufacturer, usbdev->product, usbdev->serial);
81f6f3c10   Paul Mundt   video: udlfb: Kil...
1554
1555
  	pr_info("vid_%04x&pid_%04x&rev_%04x driver's dlfb_data struct at %p
  ",
18dffdf89   Bernie Thompson   staging: udlfb: e...
1556
1557
  		usbdev->descriptor.idVendor, usbdev->descriptor.idProduct,
  		usbdev->descriptor.bcdDevice, dev);
81f6f3c10   Paul Mundt   video: udlfb: Kil...
1558
1559
1560
1561
  	pr_info("console enable=%d
  ", console);
  	pr_info("fb_defio enable=%d
  ", fb_defio);
d3189545e   Stuart Hopkins   udlfb: Add module...
1562
1563
  	pr_info("shadow enable=%d
  ", shadow);
18dffdf89   Bernie Thompson   staging: udlfb: e...
1564
1565
  
  	dev->sku_pixel_limit = 2048 * 1152; /* default to maximum */
f2e1fc9d5   Andrew Kephart   udlfb: Search con...
1566
  	if (!dlfb_parse_vendor_descriptor(dev, interface)) {
81f6f3c10   Paul Mundt   video: udlfb: Kil...
1567
1568
  		pr_err("firmware not recognized. Assume incompatible device
  ");
18dffdf89   Bernie Thompson   staging: udlfb: e...
1569
1570
  		goto error;
  	}
2469d5dbc   Bernie Thompson   Staging: udlfb: R...
1571
1572
  	if (!dlfb_alloc_urb_list(dev, WRITES_IN_FLIGHT, MAX_TRANSFER)) {
  		retval = -ENOMEM;
81f6f3c10   Paul Mundt   video: udlfb: Kil...
1573
1574
  		pr_err("dlfb_alloc_urb_list failed
  ");
2469d5dbc   Bernie Thompson   Staging: udlfb: R...
1575
1576
  		goto error;
  	}
2469d5dbc   Bernie Thompson   Staging: udlfb: R...
1577
  	/* We don't register a new USB class. Our client interface is fbdev */
59277b679   Bernie Thompson   Staging: udlfb: a...
1578

2469d5dbc   Bernie Thompson   Staging: udlfb: R...
1579
  	/* allocates framebuffer driver structure, not framebuffer memory */
c91a793f6   Kay Sievers   drivers/video/udl...
1580
  	info = framebuffer_alloc(0, &interface->dev);
2469d5dbc   Bernie Thompson   Staging: udlfb: R...
1581
1582
  	if (!info) {
  		retval = -ENOMEM;
81f6f3c10   Paul Mundt   video: udlfb: Kil...
1583
1584
  		pr_err("framebuffer_alloc failed
  ");
2469d5dbc   Bernie Thompson   Staging: udlfb: R...
1585
1586
  		goto error;
  	}
33077b8d3   Bernie Thompson   staging: udlfb: r...
1587

59277b679   Bernie Thompson   Staging: udlfb: a...
1588
1589
1590
  	dev->info = info;
  	info->par = dev;
  	info->pseudo_palette = dev->pseudo_palette;
2469d5dbc   Bernie Thompson   Staging: udlfb: R...
1591
  	info->fbops = &dlfb_ops;
59277b679   Bernie Thompson   Staging: udlfb: a...
1592

2469d5dbc   Bernie Thompson   Staging: udlfb: R...
1593
1594
  	retval = fb_alloc_cmap(&info->cmap, 256, 0);
  	if (retval < 0) {
81f6f3c10   Paul Mundt   video: udlfb: Kil...
1595
1596
  		pr_err("fb_alloc_cmap failed %x
  ", retval);
2469d5dbc   Bernie Thompson   Staging: udlfb: R...
1597
1598
  		goto error;
  	}
88e58b1a4   Roberto De Ioris   Staging: add udlf...
1599

33077b8d3   Bernie Thompson   staging: udlfb: r...
1600
1601
  	INIT_DELAYED_WORK(&dev->free_framebuffer_work,
  			  dlfb_free_framebuffer_work);
18dffdf89   Bernie Thompson   staging: udlfb: e...
1602
1603
1604
1605
  	INIT_LIST_HEAD(&info->modelist);
  
  	retval = dlfb_setup_modes(dev, info, NULL, 0);
  	if (retval != 0) {
81f6f3c10   Paul Mundt   video: udlfb: Kil...
1606
1607
  		pr_err("unable to find common mode for display and adapter
  ");
18dffdf89   Bernie Thompson   staging: udlfb: e...
1608
1609
  		goto error;
  	}
2469d5dbc   Bernie Thompson   Staging: udlfb: R...
1610
  	/* ready to begin using device */
88e58b1a4   Roberto De Ioris   Staging: add udlf...
1611

2469d5dbc   Bernie Thompson   Staging: udlfb: R...
1612
1613
  	atomic_set(&dev->usb_active, 1);
  	dlfb_select_std_channel(dev);
88e58b1a4   Roberto De Ioris   Staging: add udlf...
1614

18dffdf89   Bernie Thompson   staging: udlfb: e...
1615
  	dlfb_ops_check_var(&info->var, info);
2469d5dbc   Bernie Thompson   Staging: udlfb: R...
1616
  	dlfb_ops_set_par(info);
88e58b1a4   Roberto De Ioris   Staging: add udlf...
1617

2469d5dbc   Bernie Thompson   Staging: udlfb: R...
1618
  	retval = register_framebuffer(info);
59277b679   Bernie Thompson   Staging: udlfb: a...
1619
  	if (retval < 0) {
81f6f3c10   Paul Mundt   video: udlfb: Kil...
1620
1621
  		pr_err("register_framebuffer failed %d
  ", retval);
2469d5dbc   Bernie Thompson   Staging: udlfb: R...
1622
  		goto error;
7316bc55e   Roberto De Ioris   Staging: udlfb: u...
1623
  	}
88e58b1a4   Roberto De Ioris   Staging: add udlf...
1624

94cd1ae2f   Liu Yuan   video, udlfb: Fix...
1625
1626
1627
1628
1629
1630
1631
1632
  	for (i = 0; i < ARRAY_SIZE(fb_device_attrs); i++) {
  		retval = device_create_file(info->dev, &fb_device_attrs[i]);
  		if (retval) {
  			pr_err("device_create_file failed %d
  ", retval);
  			goto err_del_attrs;
  		}
  	}
88e58b1a4   Roberto De Ioris   Staging: add udlf...
1633

94cd1ae2f   Liu Yuan   video, udlfb: Fix...
1634
1635
1636
1637
1638
1639
  	retval = device_create_bin_file(info->dev, &edid_attr);
  	if (retval) {
  		pr_err("device_create_bin_file failed %d
  ", retval);
  		goto err_del_attrs;
  	}
88e58b1a4   Roberto De Ioris   Staging: add udlf...
1640

81f6f3c10   Paul Mundt   video: udlfb: Kil...
1641
  	pr_info("DisplayLink USB device /dev/fb%d attached. %dx%d resolution."
2469d5dbc   Bernie Thompson   Staging: udlfb: R...
1642
1643
  			" Using %dK framebuffer memory
  ", info->node,
18dffdf89   Bernie Thompson   staging: udlfb: e...
1644
  			info->var.xres, info->var.yres,
2469d5dbc   Bernie Thompson   Staging: udlfb: R...
1645
  			((dev->backing_buffer) ?
18dffdf89   Bernie Thompson   staging: udlfb: e...
1646
  			info->fix.smem_len * 2 : info->fix.smem_len) >> 10);
88e58b1a4   Roberto De Ioris   Staging: add udlf...
1647
  	return 0;
94cd1ae2f   Liu Yuan   video, udlfb: Fix...
1648
1649
1650
  err_del_attrs:
  	for (i -= 1; i >= 0; i--)
  		device_remove_file(info->dev, &fb_device_attrs[i]);
2469d5dbc   Bernie Thompson   Staging: udlfb: R...
1651
1652
  error:
  	if (dev) {
2469d5dbc   Bernie Thompson   Staging: udlfb: R...
1653

33077b8d3   Bernie Thompson   staging: udlfb: r...
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
  		if (info) {
  			if (info->cmap.len != 0)
  				fb_dealloc_cmap(&info->cmap);
  			if (info->monspecs.modedb)
  				fb_destroy_modedb(info->monspecs.modedb);
  			if (info->screen_base)
  				vfree(info->screen_base);
  
  			fb_destroy_modelist(&info->modelist);
  
  			framebuffer_release(info);
  		}
  
  		if (dev->backing_buffer)
  			vfree(dev->backing_buffer);
  
  		kref_put(&dev->kref, dlfb_free); /* ref for framebuffer */
  		kref_put(&dev->kref, dlfb_free); /* last ref from kref_init */
2469d5dbc   Bernie Thompson   Staging: udlfb: R...
1672
1673
1674
  
  		/* dev has been deallocated. Do not dereference */
  	}
59277b679   Bernie Thompson   Staging: udlfb: a...
1675
  	return retval;
88e58b1a4   Roberto De Ioris   Staging: add udlf...
1676
  }
2469d5dbc   Bernie Thompson   Staging: udlfb: R...
1677
  static void dlfb_usb_disconnect(struct usb_interface *interface)
88e58b1a4   Roberto De Ioris   Staging: add udlf...
1678
  {
59277b679   Bernie Thompson   Staging: udlfb: a...
1679
1680
  	struct dlfb_data *dev;
  	struct fb_info *info;
2469d5dbc   Bernie Thompson   Staging: udlfb: R...
1681
  	int i;
88e58b1a4   Roberto De Ioris   Staging: add udlf...
1682

59277b679   Bernie Thompson   Staging: udlfb: a...
1683
  	dev = usb_get_intfdata(interface);
2469d5dbc   Bernie Thompson   Staging: udlfb: R...
1684
  	info = dev->info;
81f6f3c10   Paul Mundt   video: udlfb: Kil...
1685
1686
  	pr_info("USB disconnect starting
  ");
2469d5dbc   Bernie Thompson   Staging: udlfb: R...
1687

33077b8d3   Bernie Thompson   staging: udlfb: r...
1688
1689
1690
1691
1692
  	/* we virtualize until all fb clients release. Then we free */
  	dev->virtualized = true;
  
  	/* When non-active we'll update virtual framebuffer, but no new urbs */
  	atomic_set(&dev->usb_active, 0);
59277b679   Bernie Thompson   Staging: udlfb: a...
1693

33077b8d3   Bernie Thompson   staging: udlfb: r...
1694
  	/* remove udlfb's sysfs interfaces */
2469d5dbc   Bernie Thompson   Staging: udlfb: R...
1695
1696
  	for (i = 0; i < ARRAY_SIZE(fb_device_attrs); i++)
  		device_remove_file(info->dev, &fb_device_attrs[i]);
2469d5dbc   Bernie Thompson   Staging: udlfb: R...
1697
  	device_remove_bin_file(info->dev, &edid_attr);
33077b8d3   Bernie Thompson   staging: udlfb: r...
1698
  	usb_set_intfdata(interface, NULL);
59277b679   Bernie Thompson   Staging: udlfb: a...
1699

33077b8d3   Bernie Thompson   staging: udlfb: r...
1700
1701
1702
  	/* if clients still have us open, will be freed on last close */
  	if (dev->fb_count == 0)
  		schedule_delayed_work(&dev->free_framebuffer_work, 0);
88e58b1a4   Roberto De Ioris   Staging: add udlf...
1703

2469d5dbc   Bernie Thompson   Staging: udlfb: R...
1704
  	/* release reference taken by kref_init in probe() */
33077b8d3   Bernie Thompson   staging: udlfb: r...
1705
  	kref_put(&dev->kref, dlfb_free);
88e58b1a4   Roberto De Ioris   Staging: add udlf...
1706

2469d5dbc   Bernie Thompson   Staging: udlfb: R...
1707
1708
1709
  	/* consider dlfb_data freed */
  
  	return;
88e58b1a4   Roberto De Ioris   Staging: add udlf...
1710
1711
1712
1713
  }
  
  static struct usb_driver dlfb_driver = {
  	.name = "udlfb",
2469d5dbc   Bernie Thompson   Staging: udlfb: R...
1714
1715
  	.probe = dlfb_usb_probe,
  	.disconnect = dlfb_usb_disconnect,
88e58b1a4   Roberto De Ioris   Staging: add udlf...
1716
1717
  	.id_table = id_table,
  };
fe7484834   Greg Kroah-Hartman   USB: convert some...
1718
  module_usb_driver(dlfb_driver);
88e58b1a4   Roberto De Ioris   Staging: add udlf...
1719

4a4854dd2   Bernie Thompson   Staging: udlfb: p...
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
  static void dlfb_urb_completion(struct urb *urb)
  {
  	struct urb_node *unode = urb->context;
  	struct dlfb_data *dev = unode->dev;
  	unsigned long flags;
  
  	/* sync/async unlink faults aren't errors */
  	if (urb->status) {
  		if (!(urb->status == -ENOENT ||
  		    urb->status == -ECONNRESET ||
  		    urb->status == -ESHUTDOWN)) {
81f6f3c10   Paul Mundt   video: udlfb: Kil...
1731
1732
  			pr_err("%s - nonzero write bulk status received: %d
  ",
4a4854dd2   Bernie Thompson   Staging: udlfb: p...
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
  				__func__, urb->status);
  			atomic_set(&dev->lost_pixels, 1);
  		}
  	}
  
  	urb->transfer_buffer_length = dev->urbs.size; /* reset to actual */
  
  	spin_lock_irqsave(&dev->urbs.lock, flags);
  	list_add_tail(&unode->entry, &dev->urbs.list);
  	dev->urbs.available++;
  	spin_unlock_irqrestore(&dev->urbs.lock, flags);
5bea1fbf9   Bernie Thompson   staging: udlfb: f...
1744
1745
1746
1747
1748
1749
1750
1751
  	/*
  	 * When using fb_defio, we deadlock if up() is called
  	 * while another is waiting. So queue to another process.
  	 */
  	if (fb_defio)
  		schedule_delayed_work(&unode->release_urb_work, 0);
  	else
  		up(&dev->urbs.limit_sem);
4a4854dd2   Bernie Thompson   Staging: udlfb: p...
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
  }
  
  static void dlfb_free_urb_list(struct dlfb_data *dev)
  {
  	int count = dev->urbs.count;
  	struct list_head *node;
  	struct urb_node *unode;
  	struct urb *urb;
  	int ret;
  	unsigned long flags;
81f6f3c10   Paul Mundt   video: udlfb: Kil...
1762
1763
  	pr_notice("Waiting for completes and freeing all render urbs
  ");
4a4854dd2   Bernie Thompson   Staging: udlfb: p...
1764
1765
1766
  
  	/* keep waiting and freeing, until we've got 'em all */
  	while (count--) {
33077b8d3   Bernie Thompson   staging: udlfb: r...
1767
1768
1769
1770
  
  		/* Getting interrupted means a leak, but ok at shutdown*/
  		ret = down_interruptible(&dev->urbs.limit_sem);
  		if (ret)
4a4854dd2   Bernie Thompson   Staging: udlfb: p...
1771
  			break;
33077b8d3   Bernie Thompson   staging: udlfb: r...
1772

4a4854dd2   Bernie Thompson   Staging: udlfb: p...
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
  		spin_lock_irqsave(&dev->urbs.lock, flags);
  
  		node = dev->urbs.list.next; /* have reserved one with sem */
  		list_del_init(node);
  
  		spin_unlock_irqrestore(&dev->urbs.lock, flags);
  
  		unode = list_entry(node, struct urb_node, entry);
  		urb = unode->urb;
  
  		/* Free each separately allocated piece */
c220cc3e3   Greg Kroah-Hartman   USB: staging: fix...
1784
1785
  		usb_free_coherent(urb->dev, dev->urbs.size,
  				  urb->transfer_buffer, urb->transfer_dma);
4a4854dd2   Bernie Thompson   Staging: udlfb: p...
1786
1787
1788
  		usb_free_urb(urb);
  		kfree(node);
  	}
4a4854dd2   Bernie Thompson   Staging: udlfb: p...
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
  }
  
  static int dlfb_alloc_urb_list(struct dlfb_data *dev, int count, size_t size)
  {
  	int i = 0;
  	struct urb *urb;
  	struct urb_node *unode;
  	char *buf;
  
  	spin_lock_init(&dev->urbs.lock);
  
  	dev->urbs.size = size;
  	INIT_LIST_HEAD(&dev->urbs.list);
  
  	while (i < count) {
  		unode = kzalloc(sizeof(struct urb_node), GFP_KERNEL);
  		if (!unode)
  			break;
  		unode->dev = dev;
5bea1fbf9   Bernie Thompson   staging: udlfb: f...
1808
1809
  		INIT_DELAYED_WORK(&unode->release_urb_work,
  			  dlfb_release_urb_work);
4a4854dd2   Bernie Thompson   Staging: udlfb: p...
1810
1811
1812
1813
1814
1815
  		urb = usb_alloc_urb(0, GFP_KERNEL);
  		if (!urb) {
  			kfree(unode);
  			break;
  		}
  		unode->urb = urb;
c220cc3e3   Greg Kroah-Hartman   USB: staging: fix...
1816
1817
  		buf = usb_alloc_coherent(dev->udev, MAX_TRANSFER, GFP_KERNEL,
  					 &urb->transfer_dma);
4a4854dd2   Bernie Thompson   Staging: udlfb: p...
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
  		if (!buf) {
  			kfree(unode);
  			usb_free_urb(urb);
  			break;
  		}
  
  		/* urb->transfer_buffer_length set to actual before submit */
  		usb_fill_bulk_urb(urb, dev->udev, usb_sndbulkpipe(dev->udev, 1),
  			buf, size, dlfb_urb_completion, unode);
  		urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  
  		list_add_tail(&unode->entry, &dev->urbs.list);
  
  		i++;
  	}
  
  	sema_init(&dev->urbs.limit_sem, i);
  	dev->urbs.count = i;
  	dev->urbs.available = i;
81f6f3c10   Paul Mundt   video: udlfb: Kil...
1837
1838
  	pr_notice("allocated %d %d byte urbs
  ", i, (int) size);
4a4854dd2   Bernie Thompson   Staging: udlfb: p...
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
  
  	return i;
  }
  
  static struct urb *dlfb_get_urb(struct dlfb_data *dev)
  {
  	int ret = 0;
  	struct list_head *entry;
  	struct urb_node *unode;
  	struct urb *urb = NULL;
  	unsigned long flags;
  
  	/* Wait for an in-flight buffer to complete and get re-queued */
  	ret = down_timeout(&dev->urbs.limit_sem, GET_URB_TIMEOUT);
  	if (ret) {
  		atomic_set(&dev->lost_pixels, 1);
81f6f3c10   Paul Mundt   video: udlfb: Kil...
1855
1856
  		pr_warn("wait for urb interrupted: %x available: %d
  ",
5bea1fbf9   Bernie Thompson   staging: udlfb: f...
1857
  		       ret, dev->urbs.available);
4a4854dd2   Bernie Thompson   Staging: udlfb: p...
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
  		goto error;
  	}
  
  	spin_lock_irqsave(&dev->urbs.lock, flags);
  
  	BUG_ON(list_empty(&dev->urbs.list)); /* reserved one with limit_sem */
  	entry = dev->urbs.list.next;
  	list_del_init(entry);
  	dev->urbs.available--;
  
  	spin_unlock_irqrestore(&dev->urbs.lock, flags);
  
  	unode = list_entry(entry, struct urb_node, entry);
  	urb = unode->urb;
  
  error:
  	return urb;
  }
  
  static int dlfb_submit_urb(struct dlfb_data *dev, struct urb *urb, size_t len)
  {
  	int ret;
  
  	BUG_ON(len > dev->urbs.size);
  
  	urb->transfer_buffer_length = len; /* set to actual payload len */
  	ret = usb_submit_urb(urb, GFP_KERNEL);
  	if (ret) {
  		dlfb_urb_completion(urb); /* because no one else will */
  		atomic_set(&dev->lost_pixels, 1);
81f6f3c10   Paul Mundt   video: udlfb: Kil...
1888
1889
  		pr_err("usb_submit_urb error %x
  ", ret);
4a4854dd2   Bernie Thompson   Staging: udlfb: p...
1890
1891
1892
  	}
  	return ret;
  }
d5ed54322   Bernie Thompson   staging: udlfb: a...
1893
  module_param(console, bool, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
4aa7faffe   Bernie Thompson   udlfb: Enable fbc...
1894
  MODULE_PARM_DESC(console, "Allow fbcon to open framebuffer");
d5ed54322   Bernie Thompson   staging: udlfb: a...
1895
1896
  
  module_param(fb_defio, bool, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
9f811b72c   Bernie Thompson   udlfb: Enable fb_...
1897
  MODULE_PARM_DESC(fb_defio, "Page fault detection of mmap writes");
d5ed54322   Bernie Thompson   staging: udlfb: a...
1898

d3189545e   Stuart Hopkins   udlfb: Add module...
1899
1900
  module_param(shadow, bool, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
  MODULE_PARM_DESC(shadow, "Shadow vid mem. Disable to save mem but lose perf");
59277b679   Bernie Thompson   Staging: udlfb: a...
1901
  MODULE_AUTHOR("Roberto De Ioris <roberto@unbit.it>, "
2469d5dbc   Bernie Thompson   Staging: udlfb: R...
1902
1903
1904
  	      "Jaya Kumar <jayakumar.lkml@gmail.com>, "
  	      "Bernie Thompson <bernie@plugable.com>");
  MODULE_DESCRIPTION("DisplayLink kernel framebuffer driver");
88e58b1a4   Roberto De Ioris   Staging: add udlf...
1905
  MODULE_LICENSE("GPL");
2469d5dbc   Bernie Thompson   Staging: udlfb: R...
1906