xref: /linux/drivers/video/fbdev/smscufx.c (revision ae14fe6f367a9779e108449b4af41791bd6aaa47)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * smscufx.c -- Framebuffer driver for SMSC UFX USB controller
4  *
5  * Copyright (C) 2011 Steve Glendinning <steve.glendinning@shawell.net>
6  * Copyright (C) 2009 Roberto De Ioris <roberto@unbit.it>
7  * Copyright (C) 2009 Jaya Kumar <jayakumar.lkml@gmail.com>
8  * Copyright (C) 2009 Bernie Thompson <bernie@plugable.com>
9  *
10  * Based on udlfb, with work from Florian Echtler, Henrik Bjerregaard Pedersen,
11  * and others.
12  *
13  * Works well with Bernie Thompson's X DAMAGE patch to xf86-video-fbdev
14  * available from http://git.plugable.com
15  *
16  * Layout is based on skeletonfb by James Simmons and Geert Uytterhoeven,
17  * usb-skeleton by GregKH.
18  */
19 
20 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21 
22 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/init.h>
25 #include <linux/usb.h>
26 #include <linux/uaccess.h>
27 #include <linux/mm.h>
28 #include <linux/fb.h>
29 #include <linux/vmalloc.h>
30 #include <linux/slab.h>
31 #include <linux/delay.h>
32 #include "edid.h"
33 
34 #define check_warn(status, fmt, args...) \
35 	({ if (status < 0) pr_warn(fmt, ##args); })
36 
37 #define check_warn_return(status, fmt, args...) \
38 	({ if (status < 0) { pr_warn(fmt, ##args); return status; } })
39 
40 #define check_warn_goto_error(status, fmt, args...) \
41 	({ if (status < 0) { pr_warn(fmt, ##args); goto error; } })
42 
43 #define all_bits_set(x, bits) (((x) & (bits)) == (bits))
44 
45 #define USB_VENDOR_REQUEST_WRITE_REGISTER	0xA0
46 #define USB_VENDOR_REQUEST_READ_REGISTER	0xA1
47 
48 /*
49  * TODO: Propose standard fb.h ioctl for reporting damage,
50  * using _IOWR() and one of the existing area structs from fb.h
51  * Consider these ioctls deprecated, but they're still used by the
52  * DisplayLink X server as yet - need both to be modified in tandem
53  * when new ioctl(s) are ready.
54  */
55 #define UFX_IOCTL_RETURN_EDID	(0xAD)
56 #define UFX_IOCTL_REPORT_DAMAGE	(0xAA)
57 
58 /* -BULK_SIZE as per usb-skeleton. Can we get full page and avoid overhead? */
59 #define BULK_SIZE		(512)
60 #define MAX_TRANSFER		(PAGE_SIZE*16 - BULK_SIZE)
61 #define WRITES_IN_FLIGHT	(4)
62 
63 #define GET_URB_TIMEOUT		(HZ)
64 #define FREE_URB_TIMEOUT	(HZ*2)
65 
66 #define BPP			2
67 
68 #define UFX_DEFIO_WRITE_DELAY	5 /* fb_deferred_io.delay in jiffies */
69 #define UFX_DEFIO_WRITE_DISABLE	(HZ*60) /* "disable" with long delay */
70 
71 struct dloarea {
72 	int x, y;
73 	int w, h;
74 };
75 
76 struct urb_node {
77 	struct list_head entry;
78 	struct ufx_data *dev;
79 	struct delayed_work release_urb_work;
80 	struct urb *urb;
81 };
82 
83 struct urb_list {
84 	struct list_head list;
85 	spinlock_t lock;
86 	struct semaphore limit_sem;
87 	int available;
88 	int count;
89 	size_t size;
90 };
91 
92 struct ufx_data {
93 	struct usb_device *udev;
94 	struct device *gdev; /* &udev->dev */
95 	struct fb_info *info;
96 	struct urb_list urbs;
97 	struct kref kref;
98 	int fb_count;
99 	bool virtualized; /* true when physical usb device not present */
100 	atomic_t usb_active; /* 0 = update virtual buffer, but no usb traffic */
101 	atomic_t lost_pixels; /* 1 = a render op failed. Need screen refresh */
102 	u8 *edid; /* null until we read edid from hw or get from sysfs */
103 	size_t edid_size;
104 	u32 pseudo_palette[256];
105 };
106 
107 static struct fb_fix_screeninfo ufx_fix = {
108 	.id =           "smscufx",
109 	.type =         FB_TYPE_PACKED_PIXELS,
110 	.visual =       FB_VISUAL_TRUECOLOR,
111 	.xpanstep =     0,
112 	.ypanstep =     0,
113 	.ywrapstep =    0,
114 	.accel =        FB_ACCEL_NONE,
115 };
116 
117 static const u32 smscufx_info_flags = FBINFO_READS_FAST |
118 	FBINFO_VIRTFB |	FBINFO_HWACCEL_IMAGEBLIT | FBINFO_HWACCEL_FILLRECT |
119 	FBINFO_HWACCEL_COPYAREA | FBINFO_MISC_ALWAYS_SETPAR;
120 
121 static const struct usb_device_id id_table[] = {
122 	{USB_DEVICE(0x0424, 0x9d00),},
123 	{USB_DEVICE(0x0424, 0x9d01),},
124 	{},
125 };
126 MODULE_DEVICE_TABLE(usb, id_table);
127 
128 /* module options */
129 static bool console;   /* Optionally allow fbcon to consume first framebuffer */
130 static bool fb_defio = true;  /* Optionally enable fb_defio mmap support */
131 
132 /* ufx keeps a list of urbs for efficient bulk transfers */
133 static void ufx_urb_completion(struct urb *urb);
134 static struct urb *ufx_get_urb(struct ufx_data *dev);
135 static int ufx_submit_urb(struct ufx_data *dev, struct urb * urb, size_t len);
136 static int ufx_alloc_urb_list(struct ufx_data *dev, int count, size_t size);
137 static void ufx_free_urb_list(struct ufx_data *dev);
138 
139 static DEFINE_MUTEX(disconnect_mutex);
140 
141 /* reads a control register */
142 static int ufx_reg_read(struct ufx_data *dev, u32 index, u32 *data)
143 {
144 	u32 *buf = kmalloc(4, GFP_KERNEL);
145 	int ret;
146 
147 	BUG_ON(!dev);
148 
149 	if (!buf)
150 		return -ENOMEM;
151 
152 	ret = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
153 		USB_VENDOR_REQUEST_READ_REGISTER,
154 		USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
155 		00, index, buf, 4, USB_CTRL_GET_TIMEOUT);
156 
157 	le32_to_cpus(buf);
158 	*data = *buf;
159 	kfree(buf);
160 
161 	if (unlikely(ret < 0))
162 		pr_warn("Failed to read register index 0x%08x\n", index);
163 
164 	return ret;
165 }
166 
167 /* writes a control register */
168 static int ufx_reg_write(struct ufx_data *dev, u32 index, u32 data)
169 {
170 	u32 *buf = kmalloc(4, GFP_KERNEL);
171 	int ret;
172 
173 	BUG_ON(!dev);
174 
175 	if (!buf)
176 		return -ENOMEM;
177 
178 	*buf = data;
179 	cpu_to_le32s(buf);
180 
181 	ret = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
182 		USB_VENDOR_REQUEST_WRITE_REGISTER,
183 		USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
184 		00, index, buf, 4, USB_CTRL_SET_TIMEOUT);
185 
186 	kfree(buf);
187 
188 	if (unlikely(ret < 0))
189 		pr_warn("Failed to write register index 0x%08x with value "
190 			"0x%08x\n", index, data);
191 
192 	return ret;
193 }
194 
195 static int ufx_reg_clear_and_set_bits(struct ufx_data *dev, u32 index,
196 	u32 bits_to_clear, u32 bits_to_set)
197 {
198 	u32 data;
199 	int status = ufx_reg_read(dev, index, &data);
200 	check_warn_return(status, "ufx_reg_clear_and_set_bits error reading "
201 		"0x%x", index);
202 
203 	data &= (~bits_to_clear);
204 	data |= bits_to_set;
205 
206 	status = ufx_reg_write(dev, index, data);
207 	check_warn_return(status, "ufx_reg_clear_and_set_bits error writing "
208 		"0x%x", index);
209 
210 	return 0;
211 }
212 
213 static int ufx_reg_set_bits(struct ufx_data *dev, u32 index, u32 bits)
214 {
215 	return ufx_reg_clear_and_set_bits(dev, index, 0, bits);
216 }
217 
218 static int ufx_reg_clear_bits(struct ufx_data *dev, u32 index, u32 bits)
219 {
220 	return ufx_reg_clear_and_set_bits(dev, index, bits, 0);
221 }
222 
223 static int ufx_lite_reset(struct ufx_data *dev)
224 {
225 	int status;
226 	u32 value;
227 
228 	status = ufx_reg_write(dev, 0x3008, 0x00000001);
229 	check_warn_return(status, "ufx_lite_reset error writing 0x3008");
230 
231 	status = ufx_reg_read(dev, 0x3008, &value);
232 	check_warn_return(status, "ufx_lite_reset error reading 0x3008");
233 
234 	return (value == 0) ? 0 : -EIO;
235 }
236 
237 /* If display is unblanked, then blank it */
238 static int ufx_blank(struct ufx_data *dev, bool wait)
239 {
240 	u32 dc_ctrl, dc_sts;
241 	int i;
242 
243 	int status = ufx_reg_read(dev, 0x2004, &dc_sts);
244 	check_warn_return(status, "ufx_blank error reading 0x2004");
245 
246 	status = ufx_reg_read(dev, 0x2000, &dc_ctrl);
247 	check_warn_return(status, "ufx_blank error reading 0x2000");
248 
249 	/* return success if display is already blanked */
250 	if ((dc_sts & 0x00000100) || (dc_ctrl & 0x00000100))
251 		return 0;
252 
253 	/* request the DC to blank the display */
254 	dc_ctrl |= 0x00000100;
255 	status = ufx_reg_write(dev, 0x2000, dc_ctrl);
256 	check_warn_return(status, "ufx_blank error writing 0x2000");
257 
258 	/* return success immediately if we don't have to wait */
259 	if (!wait)
260 		return 0;
261 
262 	for (i = 0; i < 250; i++) {
263 		status = ufx_reg_read(dev, 0x2004, &dc_sts);
264 		check_warn_return(status, "ufx_blank error reading 0x2004");
265 
266 		if (dc_sts & 0x00000100)
267 			return 0;
268 	}
269 
270 	/* timed out waiting for display to blank */
271 	return -EIO;
272 }
273 
274 /* If display is blanked, then unblank it */
275 static int ufx_unblank(struct ufx_data *dev, bool wait)
276 {
277 	u32 dc_ctrl, dc_sts;
278 	int i;
279 
280 	int status = ufx_reg_read(dev, 0x2004, &dc_sts);
281 	check_warn_return(status, "ufx_unblank error reading 0x2004");
282 
283 	status = ufx_reg_read(dev, 0x2000, &dc_ctrl);
284 	check_warn_return(status, "ufx_unblank error reading 0x2000");
285 
286 	/* return success if display is already unblanked */
287 	if (((dc_sts & 0x00000100) == 0) || ((dc_ctrl & 0x00000100) == 0))
288 		return 0;
289 
290 	/* request the DC to unblank the display */
291 	dc_ctrl &= ~0x00000100;
292 	status = ufx_reg_write(dev, 0x2000, dc_ctrl);
293 	check_warn_return(status, "ufx_unblank error writing 0x2000");
294 
295 	/* return success immediately if we don't have to wait */
296 	if (!wait)
297 		return 0;
298 
299 	for (i = 0; i < 250; i++) {
300 		status = ufx_reg_read(dev, 0x2004, &dc_sts);
301 		check_warn_return(status, "ufx_unblank error reading 0x2004");
302 
303 		if ((dc_sts & 0x00000100) == 0)
304 			return 0;
305 	}
306 
307 	/* timed out waiting for display to unblank */
308 	return -EIO;
309 }
310 
311 /* If display is enabled, then disable it */
312 static int ufx_disable(struct ufx_data *dev, bool wait)
313 {
314 	u32 dc_ctrl, dc_sts;
315 	int i;
316 
317 	int status = ufx_reg_read(dev, 0x2004, &dc_sts);
318 	check_warn_return(status, "ufx_disable error reading 0x2004");
319 
320 	status = ufx_reg_read(dev, 0x2000, &dc_ctrl);
321 	check_warn_return(status, "ufx_disable error reading 0x2000");
322 
323 	/* return success if display is already disabled */
324 	if (((dc_sts & 0x00000001) == 0) || ((dc_ctrl & 0x00000001) == 0))
325 		return 0;
326 
327 	/* request the DC to disable the display */
328 	dc_ctrl &= ~(0x00000001);
329 	status = ufx_reg_write(dev, 0x2000, dc_ctrl);
330 	check_warn_return(status, "ufx_disable error writing 0x2000");
331 
332 	/* return success immediately if we don't have to wait */
333 	if (!wait)
334 		return 0;
335 
336 	for (i = 0; i < 250; i++) {
337 		status = ufx_reg_read(dev, 0x2004, &dc_sts);
338 		check_warn_return(status, "ufx_disable error reading 0x2004");
339 
340 		if ((dc_sts & 0x00000001) == 0)
341 			return 0;
342 	}
343 
344 	/* timed out waiting for display to disable */
345 	return -EIO;
346 }
347 
348 /* If display is disabled, then enable it */
349 static int ufx_enable(struct ufx_data *dev, bool wait)
350 {
351 	u32 dc_ctrl, dc_sts;
352 	int i;
353 
354 	int status = ufx_reg_read(dev, 0x2004, &dc_sts);
355 	check_warn_return(status, "ufx_enable error reading 0x2004");
356 
357 	status = ufx_reg_read(dev, 0x2000, &dc_ctrl);
358 	check_warn_return(status, "ufx_enable error reading 0x2000");
359 
360 	/* return success if display is already enabled */
361 	if ((dc_sts & 0x00000001) || (dc_ctrl & 0x00000001))
362 		return 0;
363 
364 	/* request the DC to enable the display */
365 	dc_ctrl |= 0x00000001;
366 	status = ufx_reg_write(dev, 0x2000, dc_ctrl);
367 	check_warn_return(status, "ufx_enable error writing 0x2000");
368 
369 	/* return success immediately if we don't have to wait */
370 	if (!wait)
371 		return 0;
372 
373 	for (i = 0; i < 250; i++) {
374 		status = ufx_reg_read(dev, 0x2004, &dc_sts);
375 		check_warn_return(status, "ufx_enable error reading 0x2004");
376 
377 		if (dc_sts & 0x00000001)
378 			return 0;
379 	}
380 
381 	/* timed out waiting for display to enable */
382 	return -EIO;
383 }
384 
385 static int ufx_config_sys_clk(struct ufx_data *dev)
386 {
387 	int status = ufx_reg_write(dev, 0x700C, 0x8000000F);
388 	check_warn_return(status, "error writing 0x700C");
389 
390 	status = ufx_reg_write(dev, 0x7014, 0x0010024F);
391 	check_warn_return(status, "error writing 0x7014");
392 
393 	status = ufx_reg_write(dev, 0x7010, 0x00000000);
394 	check_warn_return(status, "error writing 0x7010");
395 
396 	status = ufx_reg_clear_bits(dev, 0x700C, 0x0000000A);
397 	check_warn_return(status, "error clearing PLL1 bypass in 0x700C");
398 	msleep(1);
399 
400 	status = ufx_reg_clear_bits(dev, 0x700C, 0x80000000);
401 	check_warn_return(status, "error clearing output gate in 0x700C");
402 
403 	return 0;
404 }
405 
406 static int ufx_config_ddr2(struct ufx_data *dev)
407 {
408 	int status, i = 0;
409 	u32 tmp;
410 
411 	status = ufx_reg_write(dev, 0x0004, 0x001F0F77);
412 	check_warn_return(status, "error writing 0x0004");
413 
414 	status = ufx_reg_write(dev, 0x0008, 0xFFF00000);
415 	check_warn_return(status, "error writing 0x0008");
416 
417 	status = ufx_reg_write(dev, 0x000C, 0x0FFF2222);
418 	check_warn_return(status, "error writing 0x000C");
419 
420 	status = ufx_reg_write(dev, 0x0010, 0x00030814);
421 	check_warn_return(status, "error writing 0x0010");
422 
423 	status = ufx_reg_write(dev, 0x0014, 0x00500019);
424 	check_warn_return(status, "error writing 0x0014");
425 
426 	status = ufx_reg_write(dev, 0x0018, 0x020D0F15);
427 	check_warn_return(status, "error writing 0x0018");
428 
429 	status = ufx_reg_write(dev, 0x001C, 0x02532305);
430 	check_warn_return(status, "error writing 0x001C");
431 
432 	status = ufx_reg_write(dev, 0x0020, 0x0B030905);
433 	check_warn_return(status, "error writing 0x0020");
434 
435 	status = ufx_reg_write(dev, 0x0024, 0x00000827);
436 	check_warn_return(status, "error writing 0x0024");
437 
438 	status = ufx_reg_write(dev, 0x0028, 0x00000000);
439 	check_warn_return(status, "error writing 0x0028");
440 
441 	status = ufx_reg_write(dev, 0x002C, 0x00000042);
442 	check_warn_return(status, "error writing 0x002C");
443 
444 	status = ufx_reg_write(dev, 0x0030, 0x09520000);
445 	check_warn_return(status, "error writing 0x0030");
446 
447 	status = ufx_reg_write(dev, 0x0034, 0x02223314);
448 	check_warn_return(status, "error writing 0x0034");
449 
450 	status = ufx_reg_write(dev, 0x0038, 0x00430043);
451 	check_warn_return(status, "error writing 0x0038");
452 
453 	status = ufx_reg_write(dev, 0x003C, 0xF00F000F);
454 	check_warn_return(status, "error writing 0x003C");
455 
456 	status = ufx_reg_write(dev, 0x0040, 0xF380F00F);
457 	check_warn_return(status, "error writing 0x0040");
458 
459 	status = ufx_reg_write(dev, 0x0044, 0xF00F0496);
460 	check_warn_return(status, "error writing 0x0044");
461 
462 	status = ufx_reg_write(dev, 0x0048, 0x03080406);
463 	check_warn_return(status, "error writing 0x0048");
464 
465 	status = ufx_reg_write(dev, 0x004C, 0x00001000);
466 	check_warn_return(status, "error writing 0x004C");
467 
468 	status = ufx_reg_write(dev, 0x005C, 0x00000007);
469 	check_warn_return(status, "error writing 0x005C");
470 
471 	status = ufx_reg_write(dev, 0x0100, 0x54F00012);
472 	check_warn_return(status, "error writing 0x0100");
473 
474 	status = ufx_reg_write(dev, 0x0104, 0x00004012);
475 	check_warn_return(status, "error writing 0x0104");
476 
477 	status = ufx_reg_write(dev, 0x0118, 0x40404040);
478 	check_warn_return(status, "error writing 0x0118");
479 
480 	status = ufx_reg_write(dev, 0x0000, 0x00000001);
481 	check_warn_return(status, "error writing 0x0000");
482 
483 	while (i++ < 500) {
484 		status = ufx_reg_read(dev, 0x0000, &tmp);
485 		check_warn_return(status, "error reading 0x0000");
486 
487 		if (all_bits_set(tmp, 0xC0000000))
488 			return 0;
489 	}
490 
491 	pr_err("DDR2 initialisation timed out, reg 0x0000=0x%08x", tmp);
492 	return -ETIMEDOUT;
493 }
494 
495 struct pll_values {
496 	u32 div_r0;
497 	u32 div_f0;
498 	u32 div_q0;
499 	u32 range0;
500 	u32 div_r1;
501 	u32 div_f1;
502 	u32 div_q1;
503 	u32 range1;
504 };
505 
506 static u32 ufx_calc_range(u32 ref_freq)
507 {
508 	if (ref_freq >= 88000000)
509 		return 7;
510 
511 	if (ref_freq >= 54000000)
512 		return 6;
513 
514 	if (ref_freq >= 34000000)
515 		return 5;
516 
517 	if (ref_freq >= 21000000)
518 		return 4;
519 
520 	if (ref_freq >= 13000000)
521 		return 3;
522 
523 	if (ref_freq >= 8000000)
524 		return 2;
525 
526 	return 1;
527 }
528 
529 /* calculates PLL divider settings for a desired target frequency */
530 static void ufx_calc_pll_values(const u32 clk_pixel_pll, struct pll_values *asic_pll)
531 {
532 	const u32 ref_clk = 25000000;
533 	u32 div_r0, div_f0, div_q0, div_r1, div_f1, div_q1;
534 	u32 min_error = clk_pixel_pll;
535 
536 	for (div_r0 = 1; div_r0 <= 32; div_r0++) {
537 		u32 ref_freq0 = ref_clk / div_r0;
538 		if (ref_freq0 < 5000000)
539 			break;
540 
541 		if (ref_freq0 > 200000000)
542 			continue;
543 
544 		for (div_f0 = 1; div_f0 <= 256; div_f0++) {
545 			u32 vco_freq0 = ref_freq0 * div_f0;
546 
547 			if (vco_freq0 < 350000000)
548 				continue;
549 
550 			if (vco_freq0 > 700000000)
551 				break;
552 
553 			for (div_q0 = 0; div_q0 < 7; div_q0++) {
554 				u32 pllout_freq0 = vco_freq0 / (1 << div_q0);
555 
556 				if (pllout_freq0 < 5000000)
557 					break;
558 
559 				if (pllout_freq0 > 200000000)
560 					continue;
561 
562 				for (div_r1 = 1; div_r1 <= 32; div_r1++) {
563 					u32 ref_freq1 = pllout_freq0 / div_r1;
564 
565 					if (ref_freq1 < 5000000)
566 						break;
567 
568 					for (div_f1 = 1; div_f1 <= 256; div_f1++) {
569 						u32 vco_freq1 = ref_freq1 * div_f1;
570 
571 						if (vco_freq1 < 350000000)
572 							continue;
573 
574 						if (vco_freq1 > 700000000)
575 							break;
576 
577 						for (div_q1 = 0; div_q1 < 7; div_q1++) {
578 							u32 pllout_freq1 = vco_freq1 / (1 << div_q1);
579 							int error = abs(pllout_freq1 - clk_pixel_pll);
580 
581 							if (pllout_freq1 < 5000000)
582 								break;
583 
584 							if (pllout_freq1 > 700000000)
585 								continue;
586 
587 							if (error < min_error) {
588 								min_error = error;
589 
590 								/* final returned value is equal to calculated value - 1
591 								 * because a value of 0 = divide by 1 */
592 								asic_pll->div_r0 = div_r0 - 1;
593 								asic_pll->div_f0 = div_f0 - 1;
594 								asic_pll->div_q0 = div_q0;
595 								asic_pll->div_r1 = div_r1 - 1;
596 								asic_pll->div_f1 = div_f1 - 1;
597 								asic_pll->div_q1 = div_q1;
598 
599 								asic_pll->range0 = ufx_calc_range(ref_freq0);
600 								asic_pll->range1 = ufx_calc_range(ref_freq1);
601 
602 								if (min_error == 0)
603 									return;
604 							}
605 						}
606 					}
607 				}
608 			}
609 		}
610 	}
611 }
612 
613 /* sets analog bit PLL configuration values */
614 static int ufx_config_pix_clk(struct ufx_data *dev, u32 pixclock)
615 {
616 	struct pll_values asic_pll = {0};
617 	u32 value, clk_pixel, clk_pixel_pll;
618 	int status;
619 
620 	/* convert pixclock (in ps) to frequency (in Hz) */
621 	clk_pixel = PICOS2KHZ(pixclock) * 1000;
622 	pr_debug("pixclock %d ps = clk_pixel %d Hz", pixclock, clk_pixel);
623 
624 	/* clk_pixel = 1/2 clk_pixel_pll */
625 	clk_pixel_pll = clk_pixel * 2;
626 
627 	ufx_calc_pll_values(clk_pixel_pll, &asic_pll);
628 
629 	/* Keep BYPASS and RESET signals asserted until configured */
630 	status = ufx_reg_write(dev, 0x7000, 0x8000000F);
631 	check_warn_return(status, "error writing 0x7000");
632 
633 	value = (asic_pll.div_f1 | (asic_pll.div_r1 << 8) |
634 		(asic_pll.div_q1 << 16) | (asic_pll.range1 << 20));
635 	status = ufx_reg_write(dev, 0x7008, value);
636 	check_warn_return(status, "error writing 0x7008");
637 
638 	value = (asic_pll.div_f0 | (asic_pll.div_r0 << 8) |
639 		(asic_pll.div_q0 << 16) | (asic_pll.range0 << 20));
640 	status = ufx_reg_write(dev, 0x7004, value);
641 	check_warn_return(status, "error writing 0x7004");
642 
643 	status = ufx_reg_clear_bits(dev, 0x7000, 0x00000005);
644 	check_warn_return(status,
645 		"error clearing PLL0 bypass bits in 0x7000");
646 	msleep(1);
647 
648 	status = ufx_reg_clear_bits(dev, 0x7000, 0x0000000A);
649 	check_warn_return(status,
650 		"error clearing PLL1 bypass bits in 0x7000");
651 	msleep(1);
652 
653 	status = ufx_reg_clear_bits(dev, 0x7000, 0x80000000);
654 	check_warn_return(status, "error clearing gate bits in 0x7000");
655 
656 	return 0;
657 }
658 
659 static int ufx_set_vid_mode(struct ufx_data *dev, struct fb_var_screeninfo *var)
660 {
661 	u32 temp;
662 	u16 h_total, h_active, h_blank_start, h_blank_end, h_sync_start, h_sync_end;
663 	u16 v_total, v_active, v_blank_start, v_blank_end, v_sync_start, v_sync_end;
664 	int status;
665 
666 	if (!atomic_read(&dev->usb_active))
667 		return -ENODEV;
668 
669 	status = ufx_reg_write(dev, 0x8028, 0);
670 	check_warn_return(status, "ufx_set_vid_mode error disabling RGB pad");
671 
672 	status = ufx_reg_write(dev, 0x8024, 0);
673 	check_warn_return(status, "ufx_set_vid_mode error disabling VDAC");
674 
675 	/* shut everything down before changing timing */
676 	status = ufx_blank(dev, true);
677 	check_warn_return(status, "ufx_set_vid_mode error blanking display");
678 
679 	status = ufx_disable(dev, true);
680 	check_warn_return(status, "ufx_set_vid_mode error disabling display");
681 
682 	status = ufx_config_pix_clk(dev, var->pixclock);
683 	check_warn_return(status, "ufx_set_vid_mode error configuring pixclock");
684 
685 	status = ufx_reg_write(dev, 0x2000, 0x00000104);
686 	check_warn_return(status, "ufx_set_vid_mode error writing 0x2000");
687 
688 	/* set horizontal timings */
689 	h_total = var->xres + var->right_margin + var->hsync_len + var->left_margin;
690 	h_active = var->xres;
691 	h_blank_start = var->xres + var->right_margin;
692 	h_blank_end = var->xres + var->right_margin + var->hsync_len;
693 	h_sync_start = var->xres + var->right_margin;
694 	h_sync_end = var->xres + var->right_margin + var->hsync_len;
695 
696 	temp = ((h_total - 1) << 16) | (h_active - 1);
697 	status = ufx_reg_write(dev, 0x2008, temp);
698 	check_warn_return(status, "ufx_set_vid_mode error writing 0x2008");
699 
700 	temp = ((h_blank_start - 1) << 16) | (h_blank_end - 1);
701 	status = ufx_reg_write(dev, 0x200C, temp);
702 	check_warn_return(status, "ufx_set_vid_mode error writing 0x200C");
703 
704 	temp = ((h_sync_start - 1) << 16) | (h_sync_end - 1);
705 	status = ufx_reg_write(dev, 0x2010, temp);
706 	check_warn_return(status, "ufx_set_vid_mode error writing 0x2010");
707 
708 	/* set vertical timings */
709 	v_total = var->upper_margin + var->yres + var->lower_margin + var->vsync_len;
710 	v_active = var->yres;
711 	v_blank_start = var->yres + var->lower_margin;
712 	v_blank_end = var->yres + var->lower_margin + var->vsync_len;
713 	v_sync_start = var->yres + var->lower_margin;
714 	v_sync_end = var->yres + var->lower_margin + var->vsync_len;
715 
716 	temp = ((v_total - 1) << 16) | (v_active - 1);
717 	status = ufx_reg_write(dev, 0x2014, temp);
718 	check_warn_return(status, "ufx_set_vid_mode error writing 0x2014");
719 
720 	temp = ((v_blank_start - 1) << 16) | (v_blank_end - 1);
721 	status = ufx_reg_write(dev, 0x2018, temp);
722 	check_warn_return(status, "ufx_set_vid_mode error writing 0x2018");
723 
724 	temp = ((v_sync_start - 1) << 16) | (v_sync_end - 1);
725 	status = ufx_reg_write(dev, 0x201C, temp);
726 	check_warn_return(status, "ufx_set_vid_mode error writing 0x201C");
727 
728 	status = ufx_reg_write(dev, 0x2020, 0x00000000);
729 	check_warn_return(status, "ufx_set_vid_mode error writing 0x2020");
730 
731 	status = ufx_reg_write(dev, 0x2024, 0x00000000);
732 	check_warn_return(status, "ufx_set_vid_mode error writing 0x2024");
733 
734 	/* Set the frame length register (#pix * 2 bytes/pixel) */
735 	temp = var->xres * var->yres * 2;
736 	temp = (temp + 7) & (~0x7);
737 	status = ufx_reg_write(dev, 0x2028, temp);
738 	check_warn_return(status, "ufx_set_vid_mode error writing 0x2028");
739 
740 	/* enable desired output interface & disable others */
741 	status = ufx_reg_write(dev, 0x2040, 0);
742 	check_warn_return(status, "ufx_set_vid_mode error writing 0x2040");
743 
744 	status = ufx_reg_write(dev, 0x2044, 0);
745 	check_warn_return(status, "ufx_set_vid_mode error writing 0x2044");
746 
747 	status = ufx_reg_write(dev, 0x2048, 0);
748 	check_warn_return(status, "ufx_set_vid_mode error writing 0x2048");
749 
750 	/* set the sync polarities & enable bit */
751 	temp = 0x00000001;
752 	if (var->sync & FB_SYNC_HOR_HIGH_ACT)
753 		temp |= 0x00000010;
754 
755 	if (var->sync & FB_SYNC_VERT_HIGH_ACT)
756 		temp |= 0x00000008;
757 
758 	status = ufx_reg_write(dev, 0x2040, temp);
759 	check_warn_return(status, "ufx_set_vid_mode error writing 0x2040");
760 
761 	/* start everything back up */
762 	status = ufx_enable(dev, true);
763 	check_warn_return(status, "ufx_set_vid_mode error enabling display");
764 
765 	/* Unblank the display */
766 	status = ufx_unblank(dev, true);
767 	check_warn_return(status, "ufx_set_vid_mode error unblanking display");
768 
769 	/* enable RGB pad */
770 	status = ufx_reg_write(dev, 0x8028, 0x00000003);
771 	check_warn_return(status, "ufx_set_vid_mode error enabling RGB pad");
772 
773 	/* enable VDAC */
774 	status = ufx_reg_write(dev, 0x8024, 0x00000007);
775 	check_warn_return(status, "ufx_set_vid_mode error enabling VDAC");
776 
777 	return 0;
778 }
779 
780 static int ufx_ops_mmap(struct fb_info *info, struct vm_area_struct *vma)
781 {
782 	unsigned long start = vma->vm_start;
783 	unsigned long size = vma->vm_end - vma->vm_start;
784 	unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
785 	unsigned long page, pos;
786 
787 	if (info->fbdefio)
788 		return fb_deferred_io_mmap(info, vma);
789 
790 	vma->vm_page_prot = pgprot_decrypted(vma->vm_page_prot);
791 
792 	if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT))
793 		return -EINVAL;
794 	if (size > info->fix.smem_len)
795 		return -EINVAL;
796 	if (offset > info->fix.smem_len - size)
797 		return -EINVAL;
798 
799 	pos = (unsigned long)info->fix.smem_start + offset;
800 
801 	pr_debug("mmap() framebuffer addr:%lu size:%lu\n",
802 		  pos, size);
803 
804 	while (size > 0) {
805 		page = vmalloc_to_pfn((void *)pos);
806 		if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED))
807 			return -EAGAIN;
808 
809 		start += PAGE_SIZE;
810 		pos += PAGE_SIZE;
811 		if (size > PAGE_SIZE)
812 			size -= PAGE_SIZE;
813 		else
814 			size = 0;
815 	}
816 
817 	return 0;
818 }
819 
820 static void ufx_raw_rect(struct ufx_data *dev, u16 *cmd, int x, int y,
821 	int width, int height)
822 {
823 	size_t packed_line_len = ALIGN((width * 2), 4);
824 	size_t packed_rect_len = packed_line_len * height;
825 	int line;
826 
827 	BUG_ON(!dev);
828 	BUG_ON(!dev->info);
829 
830 	/* command word */
831 	*((u32 *)&cmd[0]) = cpu_to_le32(0x01);
832 
833 	/* length word */
834 	*((u32 *)&cmd[2]) = cpu_to_le32(packed_rect_len + 16);
835 
836 	cmd[4] = cpu_to_le16(x);
837 	cmd[5] = cpu_to_le16(y);
838 	cmd[6] = cpu_to_le16(width);
839 	cmd[7] = cpu_to_le16(height);
840 
841 	/* frame base address */
842 	*((u32 *)&cmd[8]) = cpu_to_le32(0);
843 
844 	/* color mode and horizontal resolution */
845 	cmd[10] = cpu_to_le16(0x4000 | dev->info->var.xres);
846 
847 	/* vertical resolution */
848 	cmd[11] = cpu_to_le16(dev->info->var.yres);
849 
850 	/* packed data */
851 	for (line = 0; line < height; line++) {
852 		const int line_offset = dev->info->fix.line_length * (y + line);
853 		const int byte_offset = line_offset + (x * BPP);
854 		memcpy(&cmd[(24 + (packed_line_len * line)) / 2],
855 			(char *)dev->info->fix.smem_start + byte_offset, width * BPP);
856 	}
857 }
858 
859 static int ufx_handle_damage(struct ufx_data *dev, int x, int y,
860 	int width, int height)
861 {
862 	size_t packed_line_len = ALIGN((width * 2), 4);
863 	int len, status, urb_lines, start_line = 0;
864 
865 	if ((width <= 0) || (height <= 0) ||
866 	    (x + width > dev->info->var.xres) ||
867 	    (y + height > dev->info->var.yres))
868 		return -EINVAL;
869 
870 	if (!atomic_read(&dev->usb_active))
871 		return 0;
872 
873 	while (start_line < height) {
874 		struct urb *urb = ufx_get_urb(dev);
875 		if (!urb) {
876 			pr_warn("ufx_handle_damage unable to get urb");
877 			return 0;
878 		}
879 
880 		/* assume we have enough space to transfer at least one line */
881 		BUG_ON(urb->transfer_buffer_length < (24 + (width * 2)));
882 
883 		/* calculate the maximum number of lines we could fit in */
884 		urb_lines = (urb->transfer_buffer_length - 24) / packed_line_len;
885 
886 		/* but we might not need this many */
887 		urb_lines = min(urb_lines, (height - start_line));
888 
889 		memset(urb->transfer_buffer, 0, urb->transfer_buffer_length);
890 
891 		ufx_raw_rect(dev, urb->transfer_buffer, x, (y + start_line), width, urb_lines);
892 		len = 24 + (packed_line_len * urb_lines);
893 
894 		status = ufx_submit_urb(dev, urb, len);
895 		check_warn_return(status, "Error submitting URB");
896 
897 		start_line += urb_lines;
898 	}
899 
900 	return 0;
901 }
902 
903 /* NOTE: fb_defio.c is holding info->fbdefio.mutex
904  *   Touching ANY framebuffer memory that triggers a page fault
905  *   in fb_defio will cause a deadlock, when it also tries to
906  *   grab the same mutex. */
907 static void ufx_dpy_deferred_io(struct fb_info *info, struct list_head *pagereflist)
908 {
909 	struct ufx_data *dev = info->par;
910 	struct fb_deferred_io_pageref *pageref;
911 
912 	if (!fb_defio)
913 		return;
914 
915 	if (!atomic_read(&dev->usb_active))
916 		return;
917 
918 	/* walk the written page list and render each to device */
919 	list_for_each_entry(pageref, pagereflist, list) {
920 		/* create a rectangle of full screen width that encloses the
921 		 * entire dirty framebuffer page */
922 		const int x = 0;
923 		const int width = dev->info->var.xres;
924 		const int y = pageref->offset / (width * 2);
925 		int height = (PAGE_SIZE / (width * 2)) + 1;
926 		height = min(height, (int)(dev->info->var.yres - y));
927 
928 		BUG_ON(y >= dev->info->var.yres);
929 		BUG_ON((y + height) > dev->info->var.yres);
930 
931 		ufx_handle_damage(dev, x, y, width, height);
932 	}
933 }
934 
935 static int ufx_ops_ioctl(struct fb_info *info, unsigned int cmd,
936 			 unsigned long arg)
937 {
938 	struct ufx_data *dev = info->par;
939 
940 	if (!atomic_read(&dev->usb_active))
941 		return 0;
942 
943 	/* TODO: Update X server to get this from sysfs instead */
944 	if (cmd == UFX_IOCTL_RETURN_EDID) {
945 		u8 __user *edid = (u8 __user *)arg;
946 		if (copy_to_user(edid, dev->edid, dev->edid_size))
947 			return -EFAULT;
948 		return 0;
949 	}
950 
951 	/* TODO: Help propose a standard fb.h ioctl to report mmap damage */
952 	if (cmd == UFX_IOCTL_REPORT_DAMAGE) {
953 		struct dloarea *area __free(kfree) = kmalloc_obj(*area);
954 		if (!area)
955 			return -ENOMEM;
956 
957 		/* If we have a damage-aware client, turn fb_defio "off"
958 		 * To avoid perf imact of unnecessary page fault handling.
959 		 * Done by resetting the delay for this fb_info to a very
960 		 * long period. Pages will become writable and stay that way.
961 		 * Reset to normal value when all clients have closed this fb.
962 		 */
963 		if (info->fbdefio)
964 			info->fbdefio->delay = UFX_DEFIO_WRITE_DISABLE;
965 
966 		if (copy_from_user(area, (u8 __user *)arg, sizeof(*area)))
967 			return -EFAULT;
968 
969 		if (area->x < 0)
970 			area->x = 0;
971 
972 		if (area->x > info->var.xres)
973 			area->x = info->var.xres;
974 
975 		if (area->y < 0)
976 			area->y = 0;
977 
978 		if (area->y > info->var.yres)
979 			area->y = info->var.yres;
980 
981 		ufx_handle_damage(dev, area->x, area->y, area->w, area->h);
982 	}
983 
984 	return 0;
985 }
986 
987 /* taken from vesafb */
988 static int
989 ufx_ops_setcolreg(unsigned regno, unsigned red, unsigned green,
990 	       unsigned blue, unsigned transp, struct fb_info *info)
991 {
992 	int err = 0;
993 
994 	if (regno >= info->cmap.len)
995 		return 1;
996 
997 	if (regno < 16) {
998 		if (info->var.red.offset == 10) {
999 			/* 1:5:5:5 */
1000 			((u32 *) (info->pseudo_palette))[regno] =
1001 			    ((red & 0xf800) >> 1) |
1002 			    ((green & 0xf800) >> 6) | ((blue & 0xf800) >> 11);
1003 		} else {
1004 			/* 0:5:6:5 */
1005 			((u32 *) (info->pseudo_palette))[regno] =
1006 			    ((red & 0xf800)) |
1007 			    ((green & 0xfc00) >> 5) | ((blue & 0xf800) >> 11);
1008 		}
1009 	}
1010 
1011 	return err;
1012 }
1013 
1014 /* It's common for several clients to have framebuffer open simultaneously.
1015  * e.g. both fbcon and X. Makes things interesting.
1016  * Assumes caller is holding info->lock (for open and release at least) */
1017 static int ufx_ops_open(struct fb_info *info, int user)
1018 {
1019 	struct ufx_data *dev = info->par;
1020 
1021 	/* fbcon aggressively connects to first framebuffer it finds,
1022 	 * preventing other clients (X) from working properly. Usually
1023 	 * not what the user wants. Fail by default with option to enable. */
1024 	if (user == 0 && !console)
1025 		return -EBUSY;
1026 
1027 	mutex_lock(&disconnect_mutex);
1028 
1029 	/* If the USB device is gone, we don't accept new opens */
1030 	if (dev->virtualized) {
1031 		mutex_unlock(&disconnect_mutex);
1032 		return -ENODEV;
1033 	}
1034 
1035 	dev->fb_count++;
1036 
1037 	kref_get(&dev->kref);
1038 
1039 	if (fb_defio && (info->fbdefio == NULL)) {
1040 		/* enable defio at last moment if not disabled by client */
1041 
1042 		struct fb_deferred_io *fbdefio;
1043 
1044 		fbdefio = kzalloc_obj(*fbdefio);
1045 		if (fbdefio) {
1046 			fbdefio->delay = UFX_DEFIO_WRITE_DELAY;
1047 			fbdefio->deferred_io = ufx_dpy_deferred_io;
1048 
1049 			info->fbdefio = fbdefio;
1050 			if (fb_deferred_io_init(info)) {
1051 				kfree(fbdefio);
1052 				info->fbdefio = NULL;
1053 			}
1054 		}
1055 	}
1056 
1057 	pr_debug("open /dev/fb%d user=%d fb_info=%p count=%d",
1058 		info->node, user, info, dev->fb_count);
1059 
1060 	mutex_unlock(&disconnect_mutex);
1061 
1062 	return 0;
1063 }
1064 
1065 /*
1066  * Called when all client interfaces to start transactions have been disabled,
1067  * and all references to our device instance (ufx_data) are released.
1068  * Every transaction must have a reference, so we know are fully spun down
1069  */
1070 static void ufx_free(struct kref *kref)
1071 {
1072 	struct ufx_data *dev = container_of(kref, struct ufx_data, kref);
1073 
1074 	usb_put_dev(dev->udev);
1075 	kfree(dev);
1076 }
1077 
1078 static void ufx_ops_destory(struct fb_info *info)
1079 {
1080 	struct ufx_data *dev = info->par;
1081 	int node = info->node;
1082 
1083 	/* Assume info structure is freed after this point */
1084 	framebuffer_release(info);
1085 
1086 	pr_debug("fb_info for /dev/fb%d has been freed", node);
1087 
1088 	/* release reference taken by kref_init in probe() */
1089 	kref_put(&dev->kref, ufx_free);
1090 }
1091 
1092 
1093 static void ufx_release_urb_work(struct work_struct *work)
1094 {
1095 	struct urb_node *unode = container_of(work, struct urb_node,
1096 					      release_urb_work.work);
1097 
1098 	up(&unode->dev->urbs.limit_sem);
1099 }
1100 
1101 static void ufx_free_framebuffer(struct ufx_data *dev)
1102 {
1103 	struct fb_info *info = dev->info;
1104 
1105 	if (info->cmap.len != 0)
1106 		fb_dealloc_cmap(&info->cmap);
1107 	if (info->monspecs.modedb)
1108 		fb_destroy_modedb(info->monspecs.modedb);
1109 	vfree(info->screen_buffer);
1110 
1111 	fb_destroy_modelist(&info->modelist);
1112 
1113 	dev->info = NULL;
1114 
1115 	/* ref taken in probe() as part of registering framebfufer */
1116 	kref_put(&dev->kref, ufx_free);
1117 }
1118 
1119 /*
1120  * Assumes caller is holding info->lock mutex (for open and release at least)
1121  */
1122 static int ufx_ops_release(struct fb_info *info, int user)
1123 {
1124 	struct ufx_data *dev = info->par;
1125 
1126 	mutex_lock(&disconnect_mutex);
1127 
1128 	dev->fb_count--;
1129 
1130 	/* We can't free fb_info here - fbmem will touch it when we return */
1131 	if (dev->virtualized && (dev->fb_count == 0))
1132 		ufx_free_framebuffer(dev);
1133 
1134 	if ((dev->fb_count == 0) && (info->fbdefio)) {
1135 		fb_deferred_io_cleanup(info);
1136 		kfree(info->fbdefio);
1137 		info->fbdefio = NULL;
1138 	}
1139 
1140 	pr_debug("released /dev/fb%d user=%d count=%d",
1141 		  info->node, user, dev->fb_count);
1142 
1143 	kref_put(&dev->kref, ufx_free);
1144 
1145 	mutex_unlock(&disconnect_mutex);
1146 
1147 	return 0;
1148 }
1149 
1150 /* Check whether a video mode is supported by the chip
1151  * We start from monitor's modes, so don't need to filter that here */
1152 static int ufx_is_valid_mode(struct fb_videomode *mode,
1153 		struct fb_info *info)
1154 {
1155 	if ((mode->xres * mode->yres) > (2048 * 1152)) {
1156 		pr_debug("%dx%d too many pixels",
1157 		       mode->xres, mode->yres);
1158 		return 0;
1159 	}
1160 
1161 	if (mode->pixclock < 5000) {
1162 		pr_debug("%dx%d %dps pixel clock too fast",
1163 		       mode->xres, mode->yres, mode->pixclock);
1164 		return 0;
1165 	}
1166 
1167 	pr_debug("%dx%d (pixclk %dps %dMHz) valid mode", mode->xres, mode->yres,
1168 		mode->pixclock, (1000000 / mode->pixclock));
1169 	return 1;
1170 }
1171 
1172 static void ufx_var_color_format(struct fb_var_screeninfo *var)
1173 {
1174 	const struct fb_bitfield red = { 11, 5, 0 };
1175 	const struct fb_bitfield green = { 5, 6, 0 };
1176 	const struct fb_bitfield blue = { 0, 5, 0 };
1177 
1178 	var->bits_per_pixel = 16;
1179 	var->red = red;
1180 	var->green = green;
1181 	var->blue = blue;
1182 }
1183 
1184 static int ufx_ops_check_var(struct fb_var_screeninfo *var,
1185 				struct fb_info *info)
1186 {
1187 	struct fb_videomode mode;
1188 
1189 	/* TODO: support dynamically changing framebuffer size */
1190 	if ((var->xres * var->yres * 2) > info->fix.smem_len)
1191 		return -EINVAL;
1192 
1193 	/* set device-specific elements of var unrelated to mode */
1194 	ufx_var_color_format(var);
1195 
1196 	fb_var_to_videomode(&mode, var);
1197 
1198 	if (!ufx_is_valid_mode(&mode, info))
1199 		return -EINVAL;
1200 
1201 	return 0;
1202 }
1203 
1204 static int ufx_ops_set_par(struct fb_info *info)
1205 {
1206 	struct ufx_data *dev = info->par;
1207 	int result;
1208 	u16 *pix_framebuffer;
1209 	int i;
1210 
1211 	pr_debug("set_par mode %dx%d", info->var.xres, info->var.yres);
1212 	result = ufx_set_vid_mode(dev, &info->var);
1213 
1214 	if ((result == 0) && (dev->fb_count == 0)) {
1215 		/* paint greenscreen */
1216 		pix_framebuffer = (u16 *)info->screen_buffer;
1217 		for (i = 0; i < info->fix.smem_len / 2; i++)
1218 			pix_framebuffer[i] = 0x37e6;
1219 
1220 		ufx_handle_damage(dev, 0, 0, info->var.xres, info->var.yres);
1221 	}
1222 
1223 	/* re-enable defio if previously disabled by damage tracking */
1224 	if (info->fbdefio)
1225 		info->fbdefio->delay = UFX_DEFIO_WRITE_DELAY;
1226 
1227 	return result;
1228 }
1229 
1230 /* In order to come back from full DPMS off, we need to set the mode again */
1231 static int ufx_ops_blank(int blank_mode, struct fb_info *info)
1232 {
1233 	struct ufx_data *dev = info->par;
1234 	ufx_set_vid_mode(dev, &info->var);
1235 	return 0;
1236 }
1237 
1238 static void ufx_ops_damage_range(struct fb_info *info, off_t off, size_t len)
1239 {
1240 	struct ufx_data *dev = info->par;
1241 	int start = max((int)(off / info->fix.line_length), 0);
1242 	int lines = min((u32)((len / info->fix.line_length) + 1), (u32)info->var.yres);
1243 
1244 	ufx_handle_damage(dev, 0, start, info->var.xres, lines);
1245 }
1246 
1247 static void ufx_ops_damage_area(struct fb_info *info, u32 x, u32 y, u32 width, u32 height)
1248 {
1249 	struct ufx_data *dev = info->par;
1250 
1251 	ufx_handle_damage(dev, x, y, width, height);
1252 }
1253 
1254 FB_GEN_DEFAULT_DEFERRED_SYSMEM_OPS(ufx_ops,
1255 				   ufx_ops_damage_range,
1256 				   ufx_ops_damage_area)
1257 
1258 static const struct fb_ops ufx_ops = {
1259 	.owner = THIS_MODULE,
1260 	__FB_DEFAULT_DEFERRED_OPS_RDWR(ufx_ops),
1261 	.fb_setcolreg = ufx_ops_setcolreg,
1262 	__FB_DEFAULT_DEFERRED_OPS_DRAW(ufx_ops),
1263 	.fb_mmap = ufx_ops_mmap,
1264 	.fb_ioctl = ufx_ops_ioctl,
1265 	.fb_open = ufx_ops_open,
1266 	.fb_release = ufx_ops_release,
1267 	.fb_blank = ufx_ops_blank,
1268 	.fb_check_var = ufx_ops_check_var,
1269 	.fb_set_par = ufx_ops_set_par,
1270 	.fb_destroy = ufx_ops_destory,
1271 };
1272 
1273 /* Assumes &info->lock held by caller
1274  * Assumes no active clients have framebuffer open */
1275 static int ufx_realloc_framebuffer(struct ufx_data *dev, struct fb_info *info)
1276 {
1277 	int old_len = info->fix.smem_len;
1278 	int new_len;
1279 	unsigned char *old_fb = info->screen_buffer;
1280 	unsigned char *new_fb;
1281 
1282 	pr_debug("Reallocating framebuffer. Addresses will change!");
1283 
1284 	new_len = info->fix.line_length * info->var.yres;
1285 
1286 	if (PAGE_ALIGN(new_len) > old_len) {
1287 		/*
1288 		 * Alloc system memory for virtual framebuffer.  smem_len is
1289 		 * rounded up to a page, so allocate that much as well.
1290 		 */
1291 		new_fb = vmalloc(PAGE_ALIGN(new_len));
1292 		if (!new_fb)
1293 			return -ENOMEM;
1294 
1295 		if (info->screen_buffer) {
1296 			memcpy(new_fb, old_fb, old_len);
1297 			vfree(info->screen_buffer);
1298 		}
1299 
1300 		info->screen_buffer = new_fb;
1301 		info->fix.smem_len = PAGE_ALIGN(new_len);
1302 		info->fix.smem_start = (unsigned long) new_fb;
1303 		info->flags = smscufx_info_flags;
1304 	}
1305 	return 0;
1306 }
1307 
1308 /* sets up DDC channel for 100 Kbps, std. speed, 7-bit addr, controller mode,
1309  * restart enabled, but no start byte, enable controller */
1310 static int ufx_i2c_init(struct ufx_data *dev)
1311 {
1312 	u32 tmp;
1313 
1314 	/* disable the controller before it can be reprogrammed */
1315 	int status = ufx_reg_write(dev, 0x106C, 0x00);
1316 	check_warn_return(status, "failed to disable I2C");
1317 
1318 	/* Setup the clock count registers
1319 	 * (12+1) = 13 clks @ 2.5 MHz = 5.2 uS */
1320 	status = ufx_reg_write(dev, 0x1018, 12);
1321 	check_warn_return(status, "error writing 0x1018");
1322 
1323 	/* (6+8) = 14 clks @ 2.5 MHz = 5.6 uS */
1324 	status = ufx_reg_write(dev, 0x1014, 6);
1325 	check_warn_return(status, "error writing 0x1014");
1326 
1327 	status = ufx_reg_read(dev, 0x1000, &tmp);
1328 	check_warn_return(status, "error reading 0x1000");
1329 
1330 	/* set speed to std mode */
1331 	tmp &= ~(0x06);
1332 	tmp |= 0x02;
1333 
1334 	/* 7-bit (not 10-bit) addressing */
1335 	tmp &= ~(0x10);
1336 
1337 	/* enable restart conditions and controller mode */
1338 	tmp |= 0x21;
1339 
1340 	status = ufx_reg_write(dev, 0x1000, tmp);
1341 	check_warn_return(status, "error writing 0x1000");
1342 
1343 	/* Set normal tx using target address 0 */
1344 	status = ufx_reg_clear_and_set_bits(dev, 0x1004, 0xC00, 0x000);
1345 	check_warn_return(status, "error setting TX mode bits in 0x1004");
1346 
1347 	/* Enable the controller */
1348 	status = ufx_reg_write(dev, 0x106C, 0x01);
1349 	check_warn_return(status, "failed to enable I2C");
1350 
1351 	return 0;
1352 }
1353 
1354 /* sets the I2C port mux and target address */
1355 static int ufx_i2c_configure(struct ufx_data *dev)
1356 {
1357 	int status = ufx_reg_write(dev, 0x106C, 0x00);
1358 	check_warn_return(status, "failed to disable I2C");
1359 
1360 	status = ufx_reg_write(dev, 0x3010, 0x00000000);
1361 	check_warn_return(status, "failed to write 0x3010");
1362 
1363 	/* A0h is std for any EDID, right shifted by one */
1364 	status = ufx_reg_clear_and_set_bits(dev, 0x1004, 0x3FF,	(0xA0 >> 1));
1365 	check_warn_return(status, "failed to set TAR bits in 0x1004");
1366 
1367 	status = ufx_reg_write(dev, 0x106C, 0x01);
1368 	check_warn_return(status, "failed to enable I2C");
1369 
1370 	return 0;
1371 }
1372 
1373 /* wait for BUSY to clear, with a timeout of 50ms with 10ms sleeps. if no
1374  * monitor is connected, there is no error except for timeout */
1375 static int ufx_i2c_wait_busy(struct ufx_data *dev)
1376 {
1377 	u32 tmp;
1378 	int i, status;
1379 
1380 	for (i = 0; i < 15; i++) {
1381 		status = ufx_reg_read(dev, 0x1100, &tmp);
1382 		check_warn_return(status, "0x1100 read failed");
1383 
1384 		/* if BUSY is clear, check for error */
1385 		if ((tmp & 0x80000000) == 0) {
1386 			if (tmp & 0x20000000) {
1387 				pr_warn("I2C read failed, 0x1100=0x%08x", tmp);
1388 				return -EIO;
1389 			}
1390 
1391 			return 0;
1392 		}
1393 
1394 		/* perform the first 10 retries without delay */
1395 		if (i >= 10)
1396 			msleep(10);
1397 	}
1398 
1399 	pr_warn("I2C access timed out, resetting I2C hardware");
1400 	status =  ufx_reg_write(dev, 0x1100, 0x40000000);
1401 	check_warn_return(status, "0x1100 write failed");
1402 
1403 	return -ETIMEDOUT;
1404 }
1405 
1406 /* reads a 128-byte EDID block from the currently selected port and TAR */
1407 static int ufx_read_edid(struct ufx_data *dev, u8 *edid, int edid_len)
1408 {
1409 	int i, j, status;
1410 	u32 *edid_u32 = (u32 *)edid;
1411 
1412 	BUG_ON(edid_len != EDID_LENGTH);
1413 
1414 	status = ufx_i2c_configure(dev);
1415 	if (status < 0) {
1416 		pr_err("ufx_i2c_configure failed");
1417 		return status;
1418 	}
1419 
1420 	memset(edid, 0xff, EDID_LENGTH);
1421 
1422 	/* Read the 128-byte EDID as 2 bursts of 64 bytes */
1423 	for (i = 0; i < 2; i++) {
1424 		u32 temp = 0x28070000 | (63 << 20) | (((u32)(i * 64)) << 8);
1425 		status = ufx_reg_write(dev, 0x1100, temp);
1426 		check_warn_return(status, "Failed to write 0x1100");
1427 
1428 		temp |= 0x80000000;
1429 		status = ufx_reg_write(dev, 0x1100, temp);
1430 		check_warn_return(status, "Failed to write 0x1100");
1431 
1432 		status = ufx_i2c_wait_busy(dev);
1433 		check_warn_return(status, "Timeout waiting for I2C BUSY to clear");
1434 
1435 		for (j = 0; j < 16; j++) {
1436 			u32 data_reg_addr = 0x1110 + (j * 4);
1437 			status = ufx_reg_read(dev, data_reg_addr, edid_u32++);
1438 			check_warn_return(status, "Error reading i2c data");
1439 		}
1440 	}
1441 
1442 	/* all FF's in the first 16 bytes indicates nothing is connected */
1443 	for (i = 0; i < 16; i++) {
1444 		if (edid[i] != 0xFF) {
1445 			pr_debug("edid data read successfully");
1446 			return EDID_LENGTH;
1447 		}
1448 	}
1449 
1450 	pr_warn("edid data contains all 0xff");
1451 	return -ETIMEDOUT;
1452 }
1453 
1454 /* 1) use sw default
1455  * 2) Parse into various fb_info structs
1456  * 3) Allocate virtual framebuffer memory to back highest res mode
1457  *
1458  * Parses EDID into three places used by various parts of fbdev:
1459  * fb_var_screeninfo contains the timing of the monitor's preferred mode
1460  * fb_info.monspecs is full parsed EDID info, including monspecs.modedb
1461  * fb_info.modelist is a linked list of all monitor & VESA modes which work
1462  *
1463  * If EDID is not readable/valid, then modelist is all VESA modes,
1464  * monspecs is NULL, and fb_var_screeninfo is set to safe VESA mode
1465  * Returns 0 if successful */
1466 static int ufx_setup_modes(struct ufx_data *dev, struct fb_info *info,
1467 	char *default_edid, size_t default_edid_size)
1468 {
1469 	const struct fb_videomode *default_vmode = NULL;
1470 	u8 *edid;
1471 	int i, result = 0, tries = 3;
1472 
1473 	if (refcount_read(&info->count)) /* only use mutex if info has been registered */
1474 		mutex_lock(&info->lock);
1475 
1476 	edid = kmalloc(EDID_LENGTH, GFP_KERNEL);
1477 	if (!edid) {
1478 		result = -ENOMEM;
1479 		goto error;
1480 	}
1481 
1482 	fb_destroy_modelist(&info->modelist);
1483 	memset(&info->monspecs, 0, sizeof(info->monspecs));
1484 
1485 	/* Try to (re)read EDID from hardware first
1486 	 * EDID data may return, but not parse as valid
1487 	 * Try again a few times, in case of e.g. analog cable noise */
1488 	while (tries--) {
1489 		i = ufx_read_edid(dev, edid, EDID_LENGTH);
1490 
1491 		if (i >= EDID_LENGTH)
1492 			fb_edid_to_monspecs(edid, &info->monspecs);
1493 
1494 		if (info->monspecs.modedb_len > 0) {
1495 			dev->edid = edid;
1496 			dev->edid_size = i;
1497 			break;
1498 		}
1499 	}
1500 
1501 	/* If that fails, use a previously returned EDID if available */
1502 	if (info->monspecs.modedb_len == 0) {
1503 		pr_err("Unable to get valid EDID from device/display\n");
1504 
1505 		if (dev->edid) {
1506 			fb_edid_to_monspecs(dev->edid, &info->monspecs);
1507 			if (info->monspecs.modedb_len > 0)
1508 				pr_err("Using previously queried EDID\n");
1509 		}
1510 	}
1511 
1512 	/* If that fails, use the default EDID we were handed */
1513 	if (info->monspecs.modedb_len == 0) {
1514 		if (default_edid_size >= EDID_LENGTH) {
1515 			fb_edid_to_monspecs(default_edid, &info->monspecs);
1516 			if (info->monspecs.modedb_len > 0) {
1517 				memcpy(edid, default_edid, default_edid_size);
1518 				dev->edid = edid;
1519 				dev->edid_size = default_edid_size;
1520 				pr_err("Using default/backup EDID\n");
1521 			}
1522 		}
1523 	}
1524 
1525 	/* If we've got modes, let's pick a best default mode */
1526 	if (info->monspecs.modedb_len > 0) {
1527 
1528 		for (i = 0; i < info->monspecs.modedb_len; i++) {
1529 			if (ufx_is_valid_mode(&info->monspecs.modedb[i], info))
1530 				fb_add_videomode(&info->monspecs.modedb[i],
1531 					&info->modelist);
1532 			else /* if we've removed top/best mode */
1533 				info->monspecs.misc &= ~FB_MISC_1ST_DETAIL;
1534 		}
1535 
1536 		default_vmode = fb_find_best_display(&info->monspecs,
1537 						     &info->modelist);
1538 	}
1539 
1540 	/* If everything else has failed, fall back to safe default mode */
1541 	if (default_vmode == NULL) {
1542 
1543 		struct fb_videomode fb_vmode = {0};
1544 
1545 		/* Add the standard VESA modes to our modelist
1546 		 * Since we don't have EDID, there may be modes that
1547 		 * overspec monitor and/or are incorrect aspect ratio, etc.
1548 		 * But at least the user has a chance to choose
1549 		 */
1550 		for (i = 0; i < VESA_MODEDB_SIZE; i++) {
1551 			if (ufx_is_valid_mode((struct fb_videomode *)
1552 						&vesa_modes[i], info))
1553 				fb_add_videomode(&vesa_modes[i],
1554 						 &info->modelist);
1555 		}
1556 
1557 		/* default to resolution safe for projectors
1558 		 * (since they are most common case without EDID)
1559 		 */
1560 		fb_vmode.xres = 800;
1561 		fb_vmode.yres = 600;
1562 		fb_vmode.refresh = 60;
1563 		default_vmode = fb_find_nearest_mode(&fb_vmode,
1564 						     &info->modelist);
1565 	}
1566 
1567 	/* If we have good mode and no active clients */
1568 	if ((default_vmode != NULL) && (dev->fb_count == 0)) {
1569 
1570 		fb_videomode_to_var(&info->var, default_vmode);
1571 		ufx_var_color_format(&info->var);
1572 
1573 		/* with mode size info, we can now alloc our framebuffer */
1574 		memcpy(&info->fix, &ufx_fix, sizeof(ufx_fix));
1575 		info->fix.line_length = info->var.xres *
1576 			(info->var.bits_per_pixel / 8);
1577 
1578 		result = ufx_realloc_framebuffer(dev, info);
1579 
1580 	} else
1581 		result = -EINVAL;
1582 
1583 error:
1584 	if (edid && (dev->edid != edid))
1585 		kfree(edid);
1586 
1587 	if (refcount_read(&info->count))
1588 		mutex_unlock(&info->lock);
1589 
1590 	return result;
1591 }
1592 
1593 static int ufx_usb_probe(struct usb_interface *interface,
1594 			const struct usb_device_id *id)
1595 {
1596 	struct usb_device *usbdev;
1597 	struct ufx_data *dev;
1598 	struct fb_info *info;
1599 	int retval = -ENOMEM;
1600 	u32 id_rev, fpga_rev;
1601 
1602 	/* usb initialization */
1603 	usbdev = interface_to_usbdev(interface);
1604 	BUG_ON(!usbdev);
1605 
1606 	dev = kzalloc_obj(*dev);
1607 	if (dev == NULL) {
1608 		dev_err(&usbdev->dev, "ufx_usb_probe: failed alloc of dev struct\n");
1609 		return -ENOMEM;
1610 	}
1611 
1612 	/* we need to wait for both usb and fbdev to spin down on disconnect */
1613 	kref_init(&dev->kref); /* matching kref_put in usb .disconnect fn */
1614 	kref_get(&dev->kref); /* matching kref_put in free_framebuffer_work */
1615 
1616 	dev->udev = usbdev;
1617 	dev->gdev = &usbdev->dev; /* our generic struct device * */
1618 	/* keep the device alive until the last ufx_data user is gone */
1619 	usb_get_dev(usbdev);
1620 	usb_set_intfdata(interface, dev);
1621 
1622 	dev_dbg(dev->gdev, "%s %s - serial #%s\n",
1623 		usbdev->manufacturer, usbdev->product, usbdev->serial);
1624 	dev_dbg(dev->gdev, "vid_%04x&pid_%04x&rev_%04x driver's ufx_data struct at %p\n",
1625 		le16_to_cpu(usbdev->descriptor.idVendor),
1626 		le16_to_cpu(usbdev->descriptor.idProduct),
1627 		le16_to_cpu(usbdev->descriptor.bcdDevice), dev);
1628 	dev_dbg(dev->gdev, "console enable=%d\n", console);
1629 	dev_dbg(dev->gdev, "fb_defio enable=%d\n", fb_defio);
1630 
1631 	if (!ufx_alloc_urb_list(dev, WRITES_IN_FLIGHT, MAX_TRANSFER)) {
1632 		dev_err(dev->gdev, "ufx_alloc_urb_list failed\n");
1633 		goto put_ref;
1634 	}
1635 
1636 	/* We don't register a new USB class. Our client interface is fbdev */
1637 
1638 	/* allocates framebuffer driver structure, not framebuffer memory */
1639 	info = framebuffer_alloc(0, &usbdev->dev);
1640 	if (!info) {
1641 		dev_err(dev->gdev, "framebuffer_alloc failed\n");
1642 		goto free_urb_list;
1643 	}
1644 
1645 	dev->info = info;
1646 	info->par = dev;
1647 	info->pseudo_palette = dev->pseudo_palette;
1648 	info->fbops = &ufx_ops;
1649 	INIT_LIST_HEAD(&info->modelist);
1650 
1651 	retval = fb_alloc_cmap(&info->cmap, 256, 0);
1652 	if (retval < 0) {
1653 		dev_err(dev->gdev, "fb_alloc_cmap failed %x\n", retval);
1654 		goto destroy_modedb;
1655 	}
1656 
1657 	retval = ufx_reg_read(dev, 0x3000, &id_rev);
1658 	check_warn_goto_error(retval, "error %d reading 0x3000 register from device", retval);
1659 	dev_dbg(dev->gdev, "ID_REV register value 0x%08x", id_rev);
1660 
1661 	retval = ufx_reg_read(dev, 0x3004, &fpga_rev);
1662 	check_warn_goto_error(retval, "error %d reading 0x3004 register from device", retval);
1663 	dev_dbg(dev->gdev, "FPGA_REV register value 0x%08x", fpga_rev);
1664 
1665 	dev_dbg(dev->gdev, "resetting device");
1666 	retval = ufx_lite_reset(dev);
1667 	check_warn_goto_error(retval, "error %d resetting device", retval);
1668 
1669 	dev_dbg(dev->gdev, "configuring system clock");
1670 	retval = ufx_config_sys_clk(dev);
1671 	check_warn_goto_error(retval, "error %d configuring system clock", retval);
1672 
1673 	dev_dbg(dev->gdev, "configuring DDR2 controller");
1674 	retval = ufx_config_ddr2(dev);
1675 	check_warn_goto_error(retval, "error %d initialising DDR2 controller", retval);
1676 
1677 	dev_dbg(dev->gdev, "configuring I2C controller");
1678 	retval = ufx_i2c_init(dev);
1679 	check_warn_goto_error(retval, "error %d initialising I2C controller", retval);
1680 
1681 	dev_dbg(dev->gdev, "selecting display mode");
1682 	retval = ufx_setup_modes(dev, info, NULL, 0);
1683 	check_warn_goto_error(retval, "unable to find common mode for display and adapter");
1684 
1685 	retval = ufx_reg_set_bits(dev, 0x4000, 0x00000001);
1686 	if (retval < 0) {
1687 		dev_err(dev->gdev, "error %d enabling graphics engine", retval);
1688 		goto setup_modes;
1689 	}
1690 
1691 	/* ready to begin using device */
1692 	atomic_set(&dev->usb_active, 1);
1693 
1694 	dev_dbg(dev->gdev, "checking var");
1695 	retval = ufx_ops_check_var(&info->var, info);
1696 	if (retval < 0) {
1697 		dev_err(dev->gdev, "error %d ufx_ops_check_var", retval);
1698 		goto reset_active;
1699 	}
1700 
1701 	dev_dbg(dev->gdev, "setting par");
1702 	retval = ufx_ops_set_par(info);
1703 	if (retval < 0) {
1704 		dev_err(dev->gdev, "error %d ufx_ops_set_par", retval);
1705 		goto reset_active;
1706 	}
1707 
1708 	dev_dbg(dev->gdev, "registering framebuffer");
1709 	retval = register_framebuffer(info);
1710 	if (retval < 0) {
1711 		/*
1712 		 * info is installed before the takeover can fail; freeing @dev
1713 		 * here would leave registered_fb[] and /dev/fbN pointing at
1714 		 * freed memory.
1715 		 */
1716 		if (!refcount_read(&info->count)) { /* not registered */
1717 			dev_err(dev->gdev, "error %d register_framebuffer", retval);
1718 			goto reset_active;
1719 		}
1720 		dev_warn(dev->gdev, "console takeover failed: %d\n", retval);
1721 	}
1722 
1723 	dev_info(dev->gdev, "SMSC UDX USB device /dev/fb%d attached. %dx%d resolution."
1724 		" Using %dK framebuffer memory\n", info->node,
1725 		info->var.xres, info->var.yres, info->fix.smem_len >> 10);
1726 
1727 	return 0;
1728 
1729 reset_active:
1730 	atomic_set(&dev->usb_active, 0);
1731 setup_modes:
1732 	fb_destroy_modedb(info->monspecs.modedb);
1733 	vfree(info->screen_buffer);
1734 	fb_destroy_modelist(&info->modelist);
1735 error:
1736 	fb_dealloc_cmap(&info->cmap);
1737 destroy_modedb:
1738 	framebuffer_release(info);
1739 free_urb_list:
1740 	if (dev->urbs.count > 0)
1741 		ufx_free_urb_list(dev);
1742 put_ref:
1743 	kref_put(&dev->kref, ufx_free); /* ref for framebuffer */
1744 	kref_put(&dev->kref, ufx_free); /* last ref from kref_init */
1745 	return retval;
1746 }
1747 
1748 static void ufx_usb_disconnect(struct usb_interface *interface)
1749 {
1750 	struct ufx_data *dev;
1751 	struct fb_info *info;
1752 
1753 	mutex_lock(&disconnect_mutex);
1754 
1755 	dev = usb_get_intfdata(interface);
1756 	info = dev->info;
1757 
1758 	pr_debug("USB disconnect starting\n");
1759 
1760 	/* we virtualize until all fb clients release. Then we free */
1761 	dev->virtualized = true;
1762 
1763 	/* When non-active we'll update virtual framebuffer, but no new urbs */
1764 	atomic_set(&dev->usb_active, 0);
1765 
1766 	usb_set_intfdata(interface, NULL);
1767 
1768 	/* if clients still have us open, will be freed on last close */
1769 	if (dev->fb_count == 0)
1770 		ufx_free_framebuffer(dev);
1771 
1772 	/* this function will wait for all in-flight urbs to complete */
1773 	if (dev->urbs.count > 0)
1774 		ufx_free_urb_list(dev);
1775 
1776 	pr_debug("freeing ufx_data %p", dev);
1777 
1778 	unregister_framebuffer(info);
1779 
1780 	mutex_unlock(&disconnect_mutex);
1781 }
1782 
1783 static struct usb_driver ufx_driver = {
1784 	.name = "smscufx",
1785 	.probe = ufx_usb_probe,
1786 	.disconnect = ufx_usb_disconnect,
1787 	.id_table = id_table,
1788 };
1789 
1790 module_usb_driver(ufx_driver);
1791 
1792 static void ufx_urb_completion(struct urb *urb)
1793 {
1794 	struct urb_node *unode = urb->context;
1795 	struct ufx_data *dev = unode->dev;
1796 	unsigned long flags;
1797 
1798 	/* sync/async unlink faults aren't errors */
1799 	if (urb->status) {
1800 		if (!(urb->status == -ENOENT ||
1801 		    urb->status == -ECONNRESET ||
1802 		    urb->status == -ESHUTDOWN)) {
1803 			pr_err("%s - nonzero write bulk status received: %d\n",
1804 				__func__, urb->status);
1805 			atomic_set(&dev->lost_pixels, 1);
1806 		}
1807 	}
1808 
1809 	urb->transfer_buffer_length = dev->urbs.size; /* reset to actual */
1810 
1811 	spin_lock_irqsave(&dev->urbs.lock, flags);
1812 	list_add_tail(&unode->entry, &dev->urbs.list);
1813 	dev->urbs.available++;
1814 	spin_unlock_irqrestore(&dev->urbs.lock, flags);
1815 
1816 	/* When using fb_defio, we deadlock if up() is called
1817 	 * while another is waiting. So queue to another process */
1818 	if (fb_defio)
1819 		schedule_delayed_work(&unode->release_urb_work, 0);
1820 	else
1821 		up(&dev->urbs.limit_sem);
1822 }
1823 
1824 static void ufx_free_urb_list(struct ufx_data *dev)
1825 {
1826 	int count = dev->urbs.count;
1827 	struct list_head *node;
1828 	struct urb_node *unode;
1829 	struct urb *urb;
1830 	int ret;
1831 	unsigned long flags;
1832 
1833 	pr_debug("Waiting for completes and freeing all render urbs\n");
1834 
1835 	/* keep waiting and freeing, until we've got 'em all */
1836 	while (count--) {
1837 		/* Getting interrupted means a leak, but ok at shutdown*/
1838 		ret = down_interruptible(&dev->urbs.limit_sem);
1839 		if (ret)
1840 			break;
1841 
1842 		spin_lock_irqsave(&dev->urbs.lock, flags);
1843 
1844 		node = dev->urbs.list.next; /* have reserved one with sem */
1845 		list_del_init(node);
1846 
1847 		spin_unlock_irqrestore(&dev->urbs.lock, flags);
1848 
1849 		unode = list_entry(node, struct urb_node, entry);
1850 		urb = unode->urb;
1851 
1852 		/* Free each separately allocated piece */
1853 		usb_free_coherent(urb->dev, dev->urbs.size,
1854 				  urb->transfer_buffer, urb->transfer_dma);
1855 		usb_free_urb(urb);
1856 		kfree(node);
1857 	}
1858 }
1859 
1860 static int ufx_alloc_urb_list(struct ufx_data *dev, int count, size_t size)
1861 {
1862 	int i = 0;
1863 	struct urb *urb;
1864 	struct urb_node *unode;
1865 	char *buf;
1866 
1867 	spin_lock_init(&dev->urbs.lock);
1868 
1869 	dev->urbs.size = size;
1870 	INIT_LIST_HEAD(&dev->urbs.list);
1871 
1872 	while (i < count) {
1873 		unode = kzalloc_obj(*unode);
1874 		if (!unode)
1875 			break;
1876 		unode->dev = dev;
1877 
1878 		INIT_DELAYED_WORK(&unode->release_urb_work,
1879 			  ufx_release_urb_work);
1880 
1881 		urb = usb_alloc_urb(0, GFP_KERNEL);
1882 		if (!urb) {
1883 			kfree(unode);
1884 			break;
1885 		}
1886 		unode->urb = urb;
1887 
1888 		buf = usb_alloc_coherent(dev->udev, size, GFP_KERNEL,
1889 					 &urb->transfer_dma);
1890 		if (!buf) {
1891 			kfree(unode);
1892 			usb_free_urb(urb);
1893 			break;
1894 		}
1895 
1896 		/* urb->transfer_buffer_length set to actual before submit */
1897 		usb_fill_bulk_urb(urb, dev->udev, usb_sndbulkpipe(dev->udev, 1),
1898 			buf, size, ufx_urb_completion, unode);
1899 		urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1900 
1901 		list_add_tail(&unode->entry, &dev->urbs.list);
1902 
1903 		i++;
1904 	}
1905 
1906 	sema_init(&dev->urbs.limit_sem, i);
1907 	dev->urbs.count = i;
1908 	dev->urbs.available = i;
1909 
1910 	pr_debug("allocated %d %d byte urbs\n", i, (int) size);
1911 
1912 	return i;
1913 }
1914 
1915 static struct urb *ufx_get_urb(struct ufx_data *dev)
1916 {
1917 	int ret = 0;
1918 	struct list_head *entry;
1919 	struct urb_node *unode;
1920 	struct urb *urb = NULL;
1921 	unsigned long flags;
1922 
1923 	/* Wait for an in-flight buffer to complete and get re-queued */
1924 	ret = down_timeout(&dev->urbs.limit_sem, GET_URB_TIMEOUT);
1925 	if (ret) {
1926 		atomic_set(&dev->lost_pixels, 1);
1927 		pr_warn("wait for urb interrupted: %x available: %d\n",
1928 		       ret, dev->urbs.available);
1929 		goto error;
1930 	}
1931 
1932 	spin_lock_irqsave(&dev->urbs.lock, flags);
1933 
1934 	BUG_ON(list_empty(&dev->urbs.list)); /* reserved one with limit_sem */
1935 	entry = dev->urbs.list.next;
1936 	list_del_init(entry);
1937 	dev->urbs.available--;
1938 
1939 	spin_unlock_irqrestore(&dev->urbs.lock, flags);
1940 
1941 	unode = list_entry(entry, struct urb_node, entry);
1942 	urb = unode->urb;
1943 
1944 error:
1945 	return urb;
1946 }
1947 
1948 static int ufx_submit_urb(struct ufx_data *dev, struct urb *urb, size_t len)
1949 {
1950 	int ret;
1951 
1952 	BUG_ON(len > dev->urbs.size);
1953 
1954 	urb->transfer_buffer_length = len; /* set to actual payload len */
1955 	ret = usb_submit_urb(urb, GFP_KERNEL);
1956 	if (ret) {
1957 		ufx_urb_completion(urb); /* because no one else will */
1958 		atomic_set(&dev->lost_pixels, 1);
1959 		pr_err("usb_submit_urb error %x\n", ret);
1960 	}
1961 	return ret;
1962 }
1963 
1964 module_param(console, bool, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
1965 MODULE_PARM_DESC(console, "Allow fbcon to be used on this display");
1966 
1967 module_param(fb_defio, bool, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
1968 MODULE_PARM_DESC(fb_defio, "Enable fb_defio mmap support");
1969 
1970 MODULE_AUTHOR("Steve Glendinning <steve.glendinning@shawell.net>");
1971 MODULE_DESCRIPTION("SMSC UFX kernel framebuffer driver");
1972 MODULE_LICENSE("GPL");
1973