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