blob: 2341b275e81533ca0974f3886fbcd7cc6be1a36f [file] [log] [blame]
Bernie Thompson59277b62009-11-24 15:52:21 -08001/*
2 * udlfb.c -- Framebuffer driver for DisplayLink USB controller
3 *
4 * Copyright (C) 2009 Roberto De Ioris <roberto@unbit.it>
5 * Copyright (C) 2009 Jaya Kumar <jayakumar.lkml@gmail.com>
Bernie Thompson2469d5d2010-02-15 06:46:13 -08006 * Copyright (C) 2009 Bernie Thompson <bernie@plugable.com>
Bernie Thompson59277b62009-11-24 15:52:21 -08007 *
8 * This file is subject to the terms and conditions of the GNU General Public
9 * License v2. See the file COPYING in the main directory of this archive for
10 * more details.
11 *
12 * Layout is based on skeletonfb by James Simmons and Geert Uytterhoeven,
13 * usb-skeleton by GregKH.
14 *
15 * Device-specific portions based on information from Displaylink, with work
16 * from Florian Echtler, Henrik Bjerregaard Pedersen, and others.
17 */
Roberto De Ioris88e58b12009-06-03 14:03:06 -070018
Paul Mundt81f6f3c2011-01-06 18:07:54 +090019#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
Roberto De Ioris88e58b12009-06-03 14:03:06 -070021#include <linux/module.h>
22#include <linux/kernel.h>
23#include <linux/init.h>
24#include <linux/usb.h>
25#include <linux/uaccess.h>
26#include <linux/mm.h>
27#include <linux/fb.h>
Amit Kucheriafb299002009-07-27 12:01:03 +030028#include <linux/vmalloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Linus Torvalds268bb0c2011-05-20 12:50:29 -070030#include <linux/prefetch.h>
Bernie Thompson33077b82010-09-05 16:35:19 -070031#include <linux/delay.h>
Paul Mundt96f8d862010-11-16 14:00:24 +090032#include <video/udlfb.h>
Paul Mundtb9f03a32011-01-06 18:04:02 +090033#include "edid.h"
Roberto De Ioris88e58b12009-06-03 14:03:06 -070034
Bernie Thompson59277b62009-11-24 15:52:21 -080035static struct fb_fix_screeninfo dlfb_fix = {
Bernie Thompson2469d5d2010-02-15 06:46:13 -080036 .id = "udlfb",
Bernie Thompson1d31a9e2010-02-15 06:45:43 -080037 .type = FB_TYPE_PACKED_PIXELS,
38 .visual = FB_VISUAL_TRUECOLOR,
39 .xpanstep = 0,
40 .ypanstep = 0,
41 .ywrapstep = 0,
42 .accel = FB_ACCEL_NONE,
Bernie Thompson59277b62009-11-24 15:52:21 -080043};
Roberto De Ioris88e58b12009-06-03 14:03:06 -070044
Bernie Thompson2469d5d2010-02-15 06:46:13 -080045static const u32 udlfb_info_flags = FBINFO_DEFAULT | FBINFO_READS_FAST |
Bernie Thompson2469d5d2010-02-15 06:46:13 -080046 FBINFO_VIRTFB |
Bernie Thompson2469d5d2010-02-15 06:46:13 -080047 FBINFO_HWACCEL_IMAGEBLIT | FBINFO_HWACCEL_FILLRECT |
48 FBINFO_HWACCEL_COPYAREA | FBINFO_MISC_ALWAYS_SETPAR;
49
Bernie Thompsoncc403dc2010-02-15 06:45:49 -080050/*
51 * There are many DisplayLink-based products, all with unique PIDs. We are able
52 * to support all volume ones (circa 2009) with a single driver, so we match
53 * globally on VID. TODO: Probe() needs to detect when we might be running
54 * "future" chips, and bail on those, so a compatible driver can match.
55 */
56static struct usb_device_id id_table[] = {
57 {.idVendor = 0x17e9, .match_flags = USB_DEVICE_ID_MATCH_VENDOR,},
58 {},
59};
60MODULE_DEVICE_TABLE(usb, id_table);
Bernie Thompson59277b62009-11-24 15:52:21 -080061
Bernie Thompsond5ed5432010-09-05 16:35:39 -070062/* module options */
63static int console; /* Optionally allow fbcon to consume first framebuffer */
64static int fb_defio; /* Optionally enable experimental fb_defio mmap support */
Stuart Hopkinsd3189542011-08-21 13:34:17 -070065static int shadow = 1; /* Optionally disable shadow framebuffer */
Bernie Thompsondd8015f2010-02-15 06:46:35 -080066
Bernie Thompson4a4854d2010-02-15 06:45:55 -080067/* dlfb keeps a list of urbs for efficient bulk transfers */
68static void dlfb_urb_completion(struct urb *urb);
69static struct urb *dlfb_get_urb(struct dlfb_data *dev);
70static int dlfb_submit_urb(struct dlfb_data *dev, struct urb * urb, size_t len);
71static int dlfb_alloc_urb_list(struct dlfb_data *dev, int count, size_t size);
72static void dlfb_free_urb_list(struct dlfb_data *dev);
73
Bernie Thompson59277b62009-11-24 15:52:21 -080074/*
Bernie Thompsonbd808162010-02-15 06:46:48 -080075 * All DisplayLink bulk operations start with 0xAF, followed by specific code
76 * All operations are written to buffers which then later get sent to device
Bernie Thompson59277b62009-11-24 15:52:21 -080077 */
Bernie Thompson45742032010-02-15 06:46:04 -080078static char *dlfb_set_register(char *buf, u8 reg, u8 val)
Roberto De Ioris88e58b12009-06-03 14:03:06 -070079{
Bernie Thompson1d31a9e2010-02-15 06:45:43 -080080 *buf++ = 0xAF;
81 *buf++ = 0x20;
82 *buf++ = reg;
83 *buf++ = val;
84 return buf;
Roberto De Ioris88e58b12009-06-03 14:03:06 -070085}
86
Bernie Thompson45742032010-02-15 06:46:04 -080087static char *dlfb_vidreg_lock(char *buf)
Roberto De Ioris88e58b12009-06-03 14:03:06 -070088{
Bernie Thompson45742032010-02-15 06:46:04 -080089 return dlfb_set_register(buf, 0xFF, 0x00);
Bernie Thompson59277b62009-11-24 15:52:21 -080090}
Roberto De Ioris88e58b12009-06-03 14:03:06 -070091
Bernie Thompson45742032010-02-15 06:46:04 -080092static char *dlfb_vidreg_unlock(char *buf)
Bernie Thompson59277b62009-11-24 15:52:21 -080093{
Bernie Thompson45742032010-02-15 06:46:04 -080094 return dlfb_set_register(buf, 0xFF, 0xFF);
Bernie Thompson59277b62009-11-24 15:52:21 -080095}
Roberto De Ioris88e58b12009-06-03 14:03:06 -070096
Bernie Thompson59277b62009-11-24 15:52:21 -080097/*
Bernie Thompson58e7c3b2011-08-21 13:34:11 -070098 * Map FB_BLANK_* to DisplayLink register
99 * DLReg FB_BLANK_*
100 * ----- -----------------------------
101 * 0x00 FB_BLANK_UNBLANK (0)
102 * 0x01 FB_BLANK (1)
103 * 0x03 FB_BLANK_VSYNC_SUSPEND (2)
104 * 0x05 FB_BLANK_HSYNC_SUSPEND (3)
105 * 0x07 FB_BLANK_POWERDOWN (4) Note: requires modeset to come back
Bernie Thompson59277b62009-11-24 15:52:21 -0800106 */
Bernie Thompson58e7c3b2011-08-21 13:34:11 -0700107static char *dlfb_blanking(char *buf, int fb_blank)
Bernie Thompson59277b62009-11-24 15:52:21 -0800108{
Bernie Thompson58e7c3b2011-08-21 13:34:11 -0700109 u8 reg;
110
111 switch (fb_blank) {
112 case FB_BLANK_POWERDOWN:
113 reg = 0x07;
114 break;
115 case FB_BLANK_HSYNC_SUSPEND:
116 reg = 0x05;
117 break;
118 case FB_BLANK_VSYNC_SUSPEND:
119 reg = 0x03;
120 break;
121 case FB_BLANK_NORMAL:
122 reg = 0x01;
123 break;
124 default:
125 reg = 0x00;
126 }
127
128 buf = dlfb_set_register(buf, 0x1F, reg);
129
130 return buf;
Bernie Thompson59277b62009-11-24 15:52:21 -0800131}
132
Bernie Thompson45742032010-02-15 06:46:04 -0800133static char *dlfb_set_color_depth(char *buf, u8 selection)
Bernie Thompson59277b62009-11-24 15:52:21 -0800134{
Bernie Thompson45742032010-02-15 06:46:04 -0800135 return dlfb_set_register(buf, 0x00, selection);
Bernie Thompson59277b62009-11-24 15:52:21 -0800136}
137
Bernie Thompson45742032010-02-15 06:46:04 -0800138static char *dlfb_set_base16bpp(char *wrptr, u32 base)
Bernie Thompson59277b62009-11-24 15:52:21 -0800139{
Bernie Thompson1d31a9e2010-02-15 06:45:43 -0800140 /* the base pointer is 16 bits wide, 0x20 is hi byte. */
Bernie Thompson45742032010-02-15 06:46:04 -0800141 wrptr = dlfb_set_register(wrptr, 0x20, base >> 16);
142 wrptr = dlfb_set_register(wrptr, 0x21, base >> 8);
143 return dlfb_set_register(wrptr, 0x22, base);
Bernie Thompson59277b62009-11-24 15:52:21 -0800144}
145
Bernie Thompsonbd808162010-02-15 06:46:48 -0800146/*
147 * DisplayLink HW has separate 16bpp and 8bpp framebuffers.
148 * In 24bpp modes, the low 323 RGB bits go in the 8bpp framebuffer
149 */
Bernie Thompson45742032010-02-15 06:46:04 -0800150static char *dlfb_set_base8bpp(char *wrptr, u32 base)
Bernie Thompson59277b62009-11-24 15:52:21 -0800151{
Bernie Thompson45742032010-02-15 06:46:04 -0800152 wrptr = dlfb_set_register(wrptr, 0x26, base >> 16);
153 wrptr = dlfb_set_register(wrptr, 0x27, base >> 8);
154 return dlfb_set_register(wrptr, 0x28, base);
Bernie Thompson59277b62009-11-24 15:52:21 -0800155}
156
Bernie Thompson45742032010-02-15 06:46:04 -0800157static char *dlfb_set_register_16(char *wrptr, u8 reg, u16 value)
Bernie Thompson59277b62009-11-24 15:52:21 -0800158{
Bernie Thompson45742032010-02-15 06:46:04 -0800159 wrptr = dlfb_set_register(wrptr, reg, value >> 8);
160 return dlfb_set_register(wrptr, reg+1, value);
Bernie Thompson59277b62009-11-24 15:52:21 -0800161}
162
163/*
164 * This is kind of weird because the controller takes some
165 * register values in a different byte order than other registers.
166 */
Bernie Thompson45742032010-02-15 06:46:04 -0800167static char *dlfb_set_register_16be(char *wrptr, u8 reg, u16 value)
Bernie Thompson59277b62009-11-24 15:52:21 -0800168{
Bernie Thompson45742032010-02-15 06:46:04 -0800169 wrptr = dlfb_set_register(wrptr, reg, value);
170 return dlfb_set_register(wrptr, reg+1, value >> 8);
Bernie Thompson59277b62009-11-24 15:52:21 -0800171}
172
173/*
174 * LFSR is linear feedback shift register. The reason we have this is
175 * because the display controller needs to minimize the clock depth of
176 * various counters used in the display path. So this code reverses the
177 * provided value into the lfsr16 value by counting backwards to get
178 * the value that needs to be set in the hardware comparator to get the
179 * same actual count. This makes sense once you read above a couple of
180 * times and think about it from a hardware perspective.
181 */
Bernie Thompsonbd808162010-02-15 06:46:48 -0800182static u16 dlfb_lfsr16(u16 actual_count)
Bernie Thompson59277b62009-11-24 15:52:21 -0800183{
184 u32 lv = 0xFFFF; /* This is the lfsr value that the hw starts with */
185
186 while (actual_count--) {
187 lv = ((lv << 1) |
188 (((lv >> 15) ^ (lv >> 4) ^ (lv >> 2) ^ (lv >> 1)) & 1))
189 & 0xFFFF;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700190 }
Bernie Thompson59277b62009-11-24 15:52:21 -0800191
192 return (u16) lv;
193}
194
195/*
196 * This does LFSR conversion on the value that is to be written.
197 * See LFSR explanation above for more detail.
198 */
Bernie Thompson45742032010-02-15 06:46:04 -0800199static char *dlfb_set_register_lfsr16(char *wrptr, u8 reg, u16 value)
Bernie Thompson59277b62009-11-24 15:52:21 -0800200{
Bernie Thompsonbd808162010-02-15 06:46:48 -0800201 return dlfb_set_register_16(wrptr, reg, dlfb_lfsr16(value));
Bernie Thompson59277b62009-11-24 15:52:21 -0800202}
203
204/*
205 * This takes a standard fbdev screeninfo struct and all of its monitor mode
206 * details and converts them into the DisplayLink equivalent register commands.
207 */
Bernie Thompson45742032010-02-15 06:46:04 -0800208static char *dlfb_set_vid_cmds(char *wrptr, struct fb_var_screeninfo *var)
Bernie Thompson59277b62009-11-24 15:52:21 -0800209{
210 u16 xds, yds;
211 u16 xde, yde;
212 u16 yec;
213
Bernie Thompson59277b62009-11-24 15:52:21 -0800214 /* x display start */
215 xds = var->left_margin + var->hsync_len;
Bernie Thompson45742032010-02-15 06:46:04 -0800216 wrptr = dlfb_set_register_lfsr16(wrptr, 0x01, xds);
Bernie Thompson59277b62009-11-24 15:52:21 -0800217 /* x display end */
218 xde = xds + var->xres;
Bernie Thompson45742032010-02-15 06:46:04 -0800219 wrptr = dlfb_set_register_lfsr16(wrptr, 0x03, xde);
Bernie Thompson59277b62009-11-24 15:52:21 -0800220
221 /* y display start */
222 yds = var->upper_margin + var->vsync_len;
Bernie Thompson45742032010-02-15 06:46:04 -0800223 wrptr = dlfb_set_register_lfsr16(wrptr, 0x05, yds);
Bernie Thompson59277b62009-11-24 15:52:21 -0800224 /* y display end */
225 yde = yds + var->yres;
Bernie Thompson45742032010-02-15 06:46:04 -0800226 wrptr = dlfb_set_register_lfsr16(wrptr, 0x07, yde);
Bernie Thompson59277b62009-11-24 15:52:21 -0800227
228 /* x end count is active + blanking - 1 */
Bernie Thompson45742032010-02-15 06:46:04 -0800229 wrptr = dlfb_set_register_lfsr16(wrptr, 0x09,
230 xde + var->right_margin - 1);
Bernie Thompson59277b62009-11-24 15:52:21 -0800231
232 /* libdlo hardcodes hsync start to 1 */
Bernie Thompson45742032010-02-15 06:46:04 -0800233 wrptr = dlfb_set_register_lfsr16(wrptr, 0x0B, 1);
Bernie Thompson59277b62009-11-24 15:52:21 -0800234
235 /* hsync end is width of sync pulse + 1 */
Bernie Thompson45742032010-02-15 06:46:04 -0800236 wrptr = dlfb_set_register_lfsr16(wrptr, 0x0D, var->hsync_len + 1);
Bernie Thompson59277b62009-11-24 15:52:21 -0800237
238 /* hpixels is active pixels */
Bernie Thompson45742032010-02-15 06:46:04 -0800239 wrptr = dlfb_set_register_16(wrptr, 0x0F, var->xres);
Bernie Thompson59277b62009-11-24 15:52:21 -0800240
241 /* yendcount is vertical active + vertical blanking */
242 yec = var->yres + var->upper_margin + var->lower_margin +
243 var->vsync_len;
Bernie Thompson45742032010-02-15 06:46:04 -0800244 wrptr = dlfb_set_register_lfsr16(wrptr, 0x11, yec);
Bernie Thompson59277b62009-11-24 15:52:21 -0800245
246 /* libdlo hardcodes vsync start to 0 */
Bernie Thompson45742032010-02-15 06:46:04 -0800247 wrptr = dlfb_set_register_lfsr16(wrptr, 0x13, 0);
Bernie Thompson59277b62009-11-24 15:52:21 -0800248
249 /* vsync end is width of vsync pulse */
Bernie Thompson45742032010-02-15 06:46:04 -0800250 wrptr = dlfb_set_register_lfsr16(wrptr, 0x15, var->vsync_len);
Bernie Thompson59277b62009-11-24 15:52:21 -0800251
252 /* vpixels is active pixels */
Bernie Thompson45742032010-02-15 06:46:04 -0800253 wrptr = dlfb_set_register_16(wrptr, 0x17, var->yres);
Bernie Thompson59277b62009-11-24 15:52:21 -0800254
255 /* convert picoseconds to 5kHz multiple for pclk5k = x * 1E12/5k */
Bernie Thompson45742032010-02-15 06:46:04 -0800256 wrptr = dlfb_set_register_16be(wrptr, 0x1B,
257 200*1000*1000/var->pixclock);
Bernie Thompson59277b62009-11-24 15:52:21 -0800258
259 return wrptr;
260}
261
262/*
263 * This takes a standard fbdev screeninfo struct that was fetched or prepared
264 * and then generates the appropriate command sequence that then drives the
265 * display controller.
266 */
267static int dlfb_set_video_mode(struct dlfb_data *dev,
268 struct fb_var_screeninfo *var)
269{
270 char *buf;
271 char *wrptr;
272 int retval = 0;
273 int writesize;
Bernie Thompson530f43a2010-02-15 06:46:21 -0800274 struct urb *urb;
Bernie Thompson59277b62009-11-24 15:52:21 -0800275
Bernie Thompson530f43a2010-02-15 06:46:21 -0800276 if (!atomic_read(&dev->usb_active))
277 return -EPERM;
278
279 urb = dlfb_get_urb(dev);
280 if (!urb)
281 return -ENOMEM;
Bernie Thompson2685cff2010-09-05 18:29:56 -0700282
Bernie Thompson530f43a2010-02-15 06:46:21 -0800283 buf = (char *) urb->transfer_buffer;
Bernie Thompson59277b62009-11-24 15:52:21 -0800284
285 /*
286 * This first section has to do with setting the base address on the
287 * controller * associated with the display. There are 2 base
288 * pointers, currently, we only * use the 16 bpp segment.
289 */
Bernie Thompson45742032010-02-15 06:46:04 -0800290 wrptr = dlfb_vidreg_lock(buf);
291 wrptr = dlfb_set_color_depth(wrptr, 0x00);
Bernie Thompson59277b62009-11-24 15:52:21 -0800292 /* set base for 16bpp segment to 0 */
Bernie Thompson45742032010-02-15 06:46:04 -0800293 wrptr = dlfb_set_base16bpp(wrptr, 0);
Bernie Thompson59277b62009-11-24 15:52:21 -0800294 /* set base for 8bpp segment to end of fb */
Bernie Thompson45742032010-02-15 06:46:04 -0800295 wrptr = dlfb_set_base8bpp(wrptr, dev->info->fix.smem_len);
Bernie Thompson59277b62009-11-24 15:52:21 -0800296
Bernie Thompson45742032010-02-15 06:46:04 -0800297 wrptr = dlfb_set_vid_cmds(wrptr, var);
Bernie Thompson58e7c3b2011-08-21 13:34:11 -0700298 wrptr = dlfb_blanking(wrptr, FB_BLANK_UNBLANK);
Bernie Thompson45742032010-02-15 06:46:04 -0800299 wrptr = dlfb_vidreg_unlock(wrptr);
Bernie Thompson59277b62009-11-24 15:52:21 -0800300
301 writesize = wrptr - buf;
302
Bernie Thompson530f43a2010-02-15 06:46:21 -0800303 retval = dlfb_submit_urb(dev, urb, writesize);
Bernie Thompson59277b62009-11-24 15:52:21 -0800304
Bernie Thompson58e7c3b2011-08-21 13:34:11 -0700305 dev->blank_mode = FB_BLANK_UNBLANK;
306
Bernie Thompson59277b62009-11-24 15:52:21 -0800307 return retval;
308}
309
Bernie Thompson45742032010-02-15 06:46:04 -0800310static int dlfb_ops_mmap(struct fb_info *info, struct vm_area_struct *vma)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700311{
312 unsigned long start = vma->vm_start;
313 unsigned long size = vma->vm_end - vma->vm_start;
314 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
315 unsigned long page, pos;
316
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700317 if (offset + size > info->fix.smem_len)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700318 return -EINVAL;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700319
320 pos = (unsigned long)info->fix.smem_start + offset;
321
Paul Mundt81f6f3c2011-01-06 18:07:54 +0900322 pr_notice("mmap() framebuffer addr:%lu size:%lu\n",
Bernie Thompson2685cff2010-09-05 18:29:56 -0700323 pos, size);
324
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700325 while (size > 0) {
326 page = vmalloc_to_pfn((void *)pos);
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700327 if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED))
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700328 return -EAGAIN;
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700329
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700330 start += PAGE_SIZE;
331 pos += PAGE_SIZE;
332 if (size > PAGE_SIZE)
333 size -= PAGE_SIZE;
334 else
335 size = 0;
336 }
337
338 vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
339 return 0;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700340}
341
Bernie Thompson530f43a2010-02-15 06:46:21 -0800342/*
343 * Trims identical data from front and back of line
344 * Sets new front buffer address and width
345 * And returns byte count of identical pixels
346 * Assumes CPU natural alignment (unsigned long)
347 * for back and front buffer ptrs and width
348 */
349static int dlfb_trim_hline(const u8 *bback, const u8 **bfront, int *width_bytes)
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700350{
Bernie Thompson530f43a2010-02-15 06:46:21 -0800351 int j, k;
352 const unsigned long *back = (const unsigned long *) bback;
353 const unsigned long *front = (const unsigned long *) *bfront;
354 const int width = *width_bytes / sizeof(unsigned long);
355 int identical = width;
356 int start = width;
357 int end = width;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700358
Bernie Thompson530f43a2010-02-15 06:46:21 -0800359 prefetch((void *) front);
360 prefetch((void *) back);
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700361
Bernie Thompson530f43a2010-02-15 06:46:21 -0800362 for (j = 0; j < width; j++) {
363 if (back[j] != front[j]) {
364 start = j;
365 break;
366 }
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700367 }
368
Bernie Thompson530f43a2010-02-15 06:46:21 -0800369 for (k = width - 1; k > j; k--) {
370 if (back[k] != front[k]) {
371 end = k+1;
372 break;
373 }
374 }
375
376 identical = start + (width - end);
377 *bfront = (u8 *) &front[start];
378 *width_bytes = (end - start) * sizeof(unsigned long);
379
380 return identical * sizeof(unsigned long);
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700381}
382
383/*
Pavel Machek3b7b31f2010-04-03 07:00:37 +0200384 * Render a command stream for an encoded horizontal line segment of pixels.
385 *
386 * A command buffer holds several commands.
387 * It always begins with a fresh command header
388 * (the protocol doesn't require this, but we enforce it to allow
389 * multiple buffers to be potentially encoded and sent in parallel).
390 * A single command encodes one contiguous horizontal line of pixels
391 *
392 * The function relies on the client to do all allocation, so that
393 * rendering can be done directly to output buffers (e.g. USB URBs).
394 * The function fills the supplied command buffer, providing information
395 * on where it left off, so the client may call in again with additional
396 * buffers if the line will take several buffers to complete.
397 *
398 * A single command can transmit a maximum of 256 pixels,
399 * regardless of the compression ratio (protocol design limit).
400 * To the hardware, 0 for a size byte means 256
Bernie Thompson2685cff2010-09-05 18:29:56 -0700401 *
Pavel Machek3b7b31f2010-04-03 07:00:37 +0200402 * Rather than 256 pixel commands which are either rl or raw encoded,
403 * the rlx command simply assumes alternating raw and rl spans within one cmd.
404 * This has a slightly larger header overhead, but produces more even results.
405 * It also processes all data (read and write) in a single pass.
406 * Performance benchmarks of common cases show it having just slightly better
Bernie Thompson2685cff2010-09-05 18:29:56 -0700407 * compression than 256 pixel raw or rle commands, with similar CPU consumpion.
Pavel Machek3b7b31f2010-04-03 07:00:37 +0200408 * But for very rl friendly data, will compress not quite as well.
409 */
Bernie Thompson530f43a2010-02-15 06:46:21 -0800410static void dlfb_compress_hline(
411 const uint16_t **pixel_start_ptr,
412 const uint16_t *const pixel_end,
413 uint32_t *device_address_ptr,
414 uint8_t **command_buffer_ptr,
415 const uint8_t *const cmd_buffer_end)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700416{
Bernie Thompson530f43a2010-02-15 06:46:21 -0800417 const uint16_t *pixel = *pixel_start_ptr;
418 uint32_t dev_addr = *device_address_ptr;
419 uint8_t *cmd = *command_buffer_ptr;
420 const int bpp = 2;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700421
Bernie Thompson530f43a2010-02-15 06:46:21 -0800422 while ((pixel_end > pixel) &&
423 (cmd_buffer_end - MIN_RLX_CMD_BYTES > cmd)) {
424 uint8_t *raw_pixels_count_byte = 0;
425 uint8_t *cmd_pixels_count_byte = 0;
426 const uint16_t *raw_pixel_start = 0;
427 const uint16_t *cmd_pixel_start, *cmd_pixel_end = 0;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700428
Bernie Thompson530f43a2010-02-15 06:46:21 -0800429 prefetchw((void *) cmd); /* pull in one cache line at least */
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700430
Bernie Thompson530f43a2010-02-15 06:46:21 -0800431 *cmd++ = 0xAF;
432 *cmd++ = 0x6B;
Bernie Thompson1572f912010-09-05 16:35:27 -0700433 *cmd++ = (uint8_t) ((dev_addr >> 16) & 0xFF);
434 *cmd++ = (uint8_t) ((dev_addr >> 8) & 0xFF);
435 *cmd++ = (uint8_t) ((dev_addr) & 0xFF);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700436
Bernie Thompson530f43a2010-02-15 06:46:21 -0800437 cmd_pixels_count_byte = cmd++; /* we'll know this later */
438 cmd_pixel_start = pixel;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700439
Bernie Thompson530f43a2010-02-15 06:46:21 -0800440 raw_pixels_count_byte = cmd++; /* we'll know this later */
441 raw_pixel_start = pixel;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700442
Bernie Thompson530f43a2010-02-15 06:46:21 -0800443 cmd_pixel_end = pixel + min(MAX_CMD_PIXELS + 1,
444 min((int)(pixel_end - pixel),
445 (int)(cmd_buffer_end - cmd) / bpp));
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700446
Bernie Thompson530f43a2010-02-15 06:46:21 -0800447 prefetch_range((void *) pixel, (cmd_pixel_end - pixel) * bpp);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700448
Bernie Thompson530f43a2010-02-15 06:46:21 -0800449 while (pixel < cmd_pixel_end) {
450 const uint16_t * const repeating_pixel = pixel;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700451
Bernie Thompson530f43a2010-02-15 06:46:21 -0800452 *(uint16_t *)cmd = cpu_to_be16p(pixel);
453 cmd += 2;
454 pixel++;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700455
Bernie Thompson530f43a2010-02-15 06:46:21 -0800456 if (unlikely((pixel < cmd_pixel_end) &&
457 (*pixel == *repeating_pixel))) {
458 /* go back and fill in raw pixel count */
459 *raw_pixels_count_byte = ((repeating_pixel -
460 raw_pixel_start) + 1) & 0xFF;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700461
Bernie Thompson530f43a2010-02-15 06:46:21 -0800462 while ((pixel < cmd_pixel_end)
463 && (*pixel == *repeating_pixel)) {
464 pixel++;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700465 }
Bernie Thompson59277b62009-11-24 15:52:21 -0800466
Bernie Thompson530f43a2010-02-15 06:46:21 -0800467 /* immediately after raw data is repeat byte */
468 *cmd++ = ((pixel - repeating_pixel) - 1) & 0xFF;
Bernie Thompson59277b62009-11-24 15:52:21 -0800469
Bernie Thompson530f43a2010-02-15 06:46:21 -0800470 /* Then start another raw pixel span */
471 raw_pixel_start = pixel;
472 raw_pixels_count_byte = cmd++;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700473 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700474 }
475
Bernie Thompson530f43a2010-02-15 06:46:21 -0800476 if (pixel > raw_pixel_start) {
477 /* finalize last RAW span */
478 *raw_pixels_count_byte = (pixel-raw_pixel_start) & 0xFF;
479 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700480
Bernie Thompson530f43a2010-02-15 06:46:21 -0800481 *cmd_pixels_count_byte = (pixel - cmd_pixel_start) & 0xFF;
482 dev_addr += (pixel - cmd_pixel_start) * bpp;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700483 }
484
Bernie Thompson530f43a2010-02-15 06:46:21 -0800485 if (cmd_buffer_end <= MIN_RLX_CMD_BYTES + cmd) {
486 /* Fill leftover bytes with no-ops */
487 if (cmd_buffer_end > cmd)
488 memset(cmd, 0xAF, cmd_buffer_end - cmd);
489 cmd = (uint8_t *) cmd_buffer_end;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700490 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700491
Bernie Thompson530f43a2010-02-15 06:46:21 -0800492 *command_buffer_ptr = cmd;
493 *pixel_start_ptr = pixel;
494 *device_address_ptr = dev_addr;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700495
Bernie Thompson530f43a2010-02-15 06:46:21 -0800496 return;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700497}
498
Bernie Thompson530f43a2010-02-15 06:46:21 -0800499/*
500 * There are 3 copies of every pixel: The front buffer that the fbdev
501 * client renders to, the actual framebuffer across the USB bus in hardware
502 * (that we can only write to, slowly, and can never read), and (optionally)
503 * our shadow copy that tracks what's been sent to that hardware buffer.
504 */
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700505static int dlfb_render_hline(struct dlfb_data *dev, struct urb **urb_ptr,
Bernie Thompson530f43a2010-02-15 06:46:21 -0800506 const char *front, char **urb_buf_ptr,
507 u32 byte_offset, u32 byte_width,
508 int *ident_ptr, int *sent_ptr)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700509{
Bernie Thompson530f43a2010-02-15 06:46:21 -0800510 const u8 *line_start, *line_end, *next_pixel;
511 u32 dev_addr = dev->base16 + byte_offset;
512 struct urb *urb = *urb_ptr;
513 u8 *cmd = *urb_buf_ptr;
514 u8 *cmd_end = (u8 *) urb->transfer_buffer + urb->transfer_buffer_length;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700515
Bernie Thompson530f43a2010-02-15 06:46:21 -0800516 line_start = (u8 *) (front + byte_offset);
517 next_pixel = line_start;
518 line_end = next_pixel + byte_width;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700519
Bernie Thompson530f43a2010-02-15 06:46:21 -0800520 if (dev->backing_buffer) {
521 int offset;
522 const u8 *back_start = (u8 *) (dev->backing_buffer
523 + byte_offset);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700524
Bernie Thompson530f43a2010-02-15 06:46:21 -0800525 *ident_ptr += dlfb_trim_hline(back_start, &next_pixel,
526 &byte_width);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700527
Bernie Thompson530f43a2010-02-15 06:46:21 -0800528 offset = next_pixel - line_start;
529 line_end = next_pixel + byte_width;
530 dev_addr += offset;
531 back_start += offset;
532 line_start += offset;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700533
Bernie Thompson530f43a2010-02-15 06:46:21 -0800534 memcpy((char *)back_start, (char *) line_start,
535 byte_width);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700536 }
537
Bernie Thompson530f43a2010-02-15 06:46:21 -0800538 while (next_pixel < line_end) {
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700539
Bernie Thompson530f43a2010-02-15 06:46:21 -0800540 dlfb_compress_hline((const uint16_t **) &next_pixel,
541 (const uint16_t *) line_end, &dev_addr,
542 (u8 **) &cmd, (u8 *) cmd_end);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700543
Bernie Thompson530f43a2010-02-15 06:46:21 -0800544 if (cmd >= cmd_end) {
545 int len = cmd - (u8 *) urb->transfer_buffer;
546 if (dlfb_submit_urb(dev, urb, len))
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700547 return 1; /* lost pixels is set */
Bernie Thompson530f43a2010-02-15 06:46:21 -0800548 *sent_ptr += len;
549 urb = dlfb_get_urb(dev);
550 if (!urb)
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700551 return 1; /* lost_pixels is set */
Bernie Thompson530f43a2010-02-15 06:46:21 -0800552 *urb_ptr = urb;
553 cmd = urb->transfer_buffer;
554 cmd_end = &cmd[urb->transfer_buffer_length];
555 }
556 }
557
558 *urb_buf_ptr = cmd;
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700559
560 return 0;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700561}
562
Bernie Thompson530f43a2010-02-15 06:46:21 -0800563int dlfb_handle_damage(struct dlfb_data *dev, int x, int y,
564 int width, int height, char *data)
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700565{
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700566 int i, ret;
Bernie Thompson530f43a2010-02-15 06:46:21 -0800567 char *cmd;
568 cycles_t start_cycles, end_cycles;
569 int bytes_sent = 0;
570 int bytes_identical = 0;
571 struct urb *urb;
572 int aligned_x;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700573
Bernie Thompson530f43a2010-02-15 06:46:21 -0800574 start_cycles = get_cycles();
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700575
Bernie Thompson530f43a2010-02-15 06:46:21 -0800576 aligned_x = DL_ALIGN_DOWN(x, sizeof(unsigned long));
577 width = DL_ALIGN_UP(width + (x-aligned_x), sizeof(unsigned long));
578 x = aligned_x;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700579
Bernie Thompson530f43a2010-02-15 06:46:21 -0800580 if ((width <= 0) ||
581 (x + width > dev->info->var.xres) ||
582 (y + height > dev->info->var.yres))
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700583 return -EINVAL;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700584
Bernie Thompson530f43a2010-02-15 06:46:21 -0800585 if (!atomic_read(&dev->usb_active))
586 return 0;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700587
Bernie Thompson530f43a2010-02-15 06:46:21 -0800588 urb = dlfb_get_urb(dev);
589 if (!urb)
590 return 0;
591 cmd = urb->transfer_buffer;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700592
Bernie Thompson530f43a2010-02-15 06:46:21 -0800593 for (i = y; i < y + height ; i++) {
594 const int line_offset = dev->info->fix.line_length * i;
595 const int byte_offset = line_offset + (x * BPP);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700596
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700597 if (dlfb_render_hline(dev, &urb,
598 (char *) dev->info->fix.smem_start,
Bernie Thompson2685cff2010-09-05 18:29:56 -0700599 &cmd, byte_offset, width * BPP,
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700600 &bytes_identical, &bytes_sent))
601 goto error;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700602 }
603
Bernie Thompson530f43a2010-02-15 06:46:21 -0800604 if (cmd > (char *) urb->transfer_buffer) {
605 /* Send partial buffer remaining before exiting */
606 int len = cmd - (char *) urb->transfer_buffer;
607 ret = dlfb_submit_urb(dev, urb, len);
608 bytes_sent += len;
609 } else
610 dlfb_urb_completion(urb);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700611
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700612error:
Bernie Thompson530f43a2010-02-15 06:46:21 -0800613 atomic_add(bytes_sent, &dev->bytes_sent);
614 atomic_add(bytes_identical, &dev->bytes_identical);
615 atomic_add(width*height*2, &dev->bytes_rendered);
616 end_cycles = get_cycles();
617 atomic_add(((unsigned int) ((end_cycles - start_cycles)
618 >> 10)), /* Kcycles */
619 &dev->cpu_kcycles_used);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700620
Bernie Thompson530f43a2010-02-15 06:46:21 -0800621 return 0;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700622}
623
Bernie Thompsond46ecb92010-09-05 16:35:04 -0700624/*
625 * Path triggered by usermode clients who write to filesystem
626 * e.g. cat filename > /dev/fb1
627 * Not used by X Windows or text-mode console. But useful for testing.
628 * Slow because of extra copy and we must assume all pixels dirty.
629 */
630static ssize_t dlfb_ops_write(struct fb_info *info, const char __user *buf,
631 size_t count, loff_t *ppos)
632{
Paul Mundt1a3e5282011-01-06 17:29:24 +0900633 ssize_t result;
Bernie Thompsond46ecb92010-09-05 16:35:04 -0700634 struct dlfb_data *dev = info->par;
635 u32 offset = (u32) *ppos;
636
Bernie Thompsond46ecb92010-09-05 16:35:04 -0700637 result = fb_sys_write(info, buf, count, ppos);
638
639 if (result > 0) {
640 int start = max((int)(offset / info->fix.line_length) - 1, 0);
641 int lines = min((u32)((result / info->fix.line_length) + 1),
642 (u32)info->var.yres);
643
644 dlfb_handle_damage(dev, 0, start, info->var.xres,
645 lines, info->screen_base);
646 }
Bernie Thompsond46ecb92010-09-05 16:35:04 -0700647
648 return result;
649}
650
Bernie Thompson530f43a2010-02-15 06:46:21 -0800651/* hardware has native COPY command (see libdlo), but not worth it for fbcon */
Bernie Thompson45742032010-02-15 06:46:04 -0800652static void dlfb_ops_copyarea(struct fb_info *info,
Bernie Thompson530f43a2010-02-15 06:46:21 -0800653 const struct fb_copyarea *area)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700654{
Bernie Thompson530f43a2010-02-15 06:46:21 -0800655
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700656 struct dlfb_data *dev = info->par;
657
Bernie Thompson530f43a2010-02-15 06:46:21 -0800658 sys_copyarea(info, area);
659
660 dlfb_handle_damage(dev, area->dx, area->dy,
661 area->width, area->height, info->screen_base);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700662}
663
Bernie Thompson45742032010-02-15 06:46:04 -0800664static void dlfb_ops_imageblit(struct fb_info *info,
Bernie Thompson530f43a2010-02-15 06:46:21 -0800665 const struct fb_image *image)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700666{
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700667 struct dlfb_data *dev = info->par;
Bernie Thompson530f43a2010-02-15 06:46:21 -0800668
Bernie Thompson530f43a2010-02-15 06:46:21 -0800669 sys_imageblit(info, image);
670
671 dlfb_handle_damage(dev, image->dx, image->dy,
672 image->width, image->height, info->screen_base);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700673}
674
Bernie Thompson45742032010-02-15 06:46:04 -0800675static void dlfb_ops_fillrect(struct fb_info *info,
Bernie Thompson530f43a2010-02-15 06:46:21 -0800676 const struct fb_fillrect *rect)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700677{
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700678 struct dlfb_data *dev = info->par;
679
Bernie Thompson530f43a2010-02-15 06:46:21 -0800680 sys_fillrect(info, rect);
681
682 dlfb_handle_damage(dev, rect->dx, rect->dy, rect->width,
683 rect->height, info->screen_base);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700684}
685
Bernie Thompsond5ed5432010-09-05 16:35:39 -0700686/*
687 * NOTE: fb_defio.c is holding info->fbdefio.mutex
688 * Touching ANY framebuffer memory that triggers a page fault
689 * in fb_defio will cause a deadlock, when it also tries to
690 * grab the same mutex.
691 */
692static void dlfb_dpy_deferred_io(struct fb_info *info,
693 struct list_head *pagelist)
694{
695 struct page *cur;
696 struct fb_deferred_io *fbdefio = info->fbdefio;
697 struct dlfb_data *dev = info->par;
698 struct urb *urb;
699 char *cmd;
700 cycles_t start_cycles, end_cycles;
701 int bytes_sent = 0;
702 int bytes_identical = 0;
703 int bytes_rendered = 0;
704
705 if (!fb_defio)
706 return;
707
708 if (!atomic_read(&dev->usb_active))
709 return;
710
711 start_cycles = get_cycles();
712
713 urb = dlfb_get_urb(dev);
714 if (!urb)
715 return;
716
717 cmd = urb->transfer_buffer;
718
719 /* walk the written page list and render each to device */
720 list_for_each_entry(cur, &fbdefio->pagelist, lru) {
721
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700722 if (dlfb_render_hline(dev, &urb, (char *) info->fix.smem_start,
Bernie Thompsond5ed5432010-09-05 16:35:39 -0700723 &cmd, cur->index << PAGE_SHIFT,
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700724 PAGE_SIZE, &bytes_identical, &bytes_sent))
725 goto error;
Bernie Thompsond5ed5432010-09-05 16:35:39 -0700726 bytes_rendered += PAGE_SIZE;
727 }
728
729 if (cmd > (char *) urb->transfer_buffer) {
730 /* Send partial buffer remaining before exiting */
731 int len = cmd - (char *) urb->transfer_buffer;
732 dlfb_submit_urb(dev, urb, len);
733 bytes_sent += len;
734 } else
735 dlfb_urb_completion(urb);
736
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700737error:
Bernie Thompsond5ed5432010-09-05 16:35:39 -0700738 atomic_add(bytes_sent, &dev->bytes_sent);
739 atomic_add(bytes_identical, &dev->bytes_identical);
740 atomic_add(bytes_rendered, &dev->bytes_rendered);
741 end_cycles = get_cycles();
742 atomic_add(((unsigned int) ((end_cycles - start_cycles)
743 >> 10)), /* Kcycles */
744 &dev->cpu_kcycles_used);
745}
746
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700747static int dlfb_get_edid(struct dlfb_data *dev, char *edid, int len)
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800748{
749 int i;
750 int ret;
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700751 char *rbuf;
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800752
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700753 rbuf = kmalloc(2, GFP_KERNEL);
754 if (!rbuf)
755 return 0;
756
757 for (i = 0; i < len; i++) {
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800758 ret = usb_control_msg(dev->udev,
759 usb_rcvctrlpipe(dev->udev, 0), (0x02),
760 (0x80 | (0x02 << 5)), i << 8, 0xA1, rbuf, 2,
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700761 HZ);
762 if (ret < 1) {
Paul Mundt81f6f3c2011-01-06 18:07:54 +0900763 pr_err("Read EDID byte %d failed err %x\n", i, ret);
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700764 i--;
765 break;
766 }
767 edid[i] = rbuf[1];
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800768 }
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700769
770 kfree(rbuf);
771
772 return i;
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800773}
774
Bernie Thompson45742032010-02-15 06:46:04 -0800775static int dlfb_ops_ioctl(struct fb_info *info, unsigned int cmd,
Bernie Thompson530f43a2010-02-15 06:46:21 -0800776 unsigned long arg)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700777{
Bernie Thompson530f43a2010-02-15 06:46:21 -0800778
779 struct dlfb_data *dev = info->par;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700780
Bernie Thompson530f43a2010-02-15 06:46:21 -0800781 if (!atomic_read(&dev->usb_active))
782 return 0;
783
784 /* TODO: Update X server to get this from sysfs instead */
785 if (cmd == DLFB_IOCTL_RETURN_EDID) {
Dr. David Alan Gilbertdef76602011-08-21 13:34:15 -0700786 void __user *edid = (void __user *)arg;
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700787 if (copy_to_user(edid, dev->edid, dev->edid_size))
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700788 return -EFAULT;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700789 return 0;
790 }
791
Bernie Thompson530f43a2010-02-15 06:46:21 -0800792 /* TODO: Help propose a standard fb.h ioctl to report mmap damage */
793 if (cmd == DLFB_IOCTL_REPORT_DAMAGE) {
Dr. David Alan Gilbertdef76602011-08-21 13:34:15 -0700794 struct dloarea area;
795
796 if (copy_from_user(&area, (void __user *)arg,
797 sizeof(struct dloarea)))
798 return -EFAULT;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700799
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700800 /*
801 * If we have a damage-aware client, turn fb_defio "off"
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300802 * To avoid perf imact of unnecessary page fault handling.
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700803 * Done by resetting the delay for this fb_info to a very
804 * long period. Pages will become writable and stay that way.
805 * Reset to normal value when all clients have closed this fb.
806 */
807 if (info->fbdefio)
808 info->fbdefio->delay = DL_DEFIO_WRITE_DISABLE;
809
Dr. David Alan Gilbertdef76602011-08-21 13:34:15 -0700810 if (area.x < 0)
811 area.x = 0;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700812
Dr. David Alan Gilbertdef76602011-08-21 13:34:15 -0700813 if (area.x > info->var.xres)
814 area.x = info->var.xres;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700815
Dr. David Alan Gilbertdef76602011-08-21 13:34:15 -0700816 if (area.y < 0)
817 area.y = 0;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700818
Dr. David Alan Gilbertdef76602011-08-21 13:34:15 -0700819 if (area.y > info->var.yres)
820 area.y = info->var.yres;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700821
Dr. David Alan Gilbertdef76602011-08-21 13:34:15 -0700822 dlfb_handle_damage(dev, area.x, area.y, area.w, area.h,
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700823 info->screen_base);
824 }
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700825
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700826 return 0;
827}
828
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700829/* taken from vesafb */
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700830static int
Bernie Thompson45742032010-02-15 06:46:04 -0800831dlfb_ops_setcolreg(unsigned regno, unsigned red, unsigned green,
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700832 unsigned blue, unsigned transp, struct fb_info *info)
833{
834 int err = 0;
835
836 if (regno >= info->cmap.len)
837 return 1;
838
839 if (regno < 16) {
840 if (info->var.red.offset == 10) {
841 /* 1:5:5:5 */
842 ((u32 *) (info->pseudo_palette))[regno] =
843 ((red & 0xf800) >> 1) |
844 ((green & 0xf800) >> 6) | ((blue & 0xf800) >> 11);
845 } else {
846 /* 0:5:6:5 */
847 ((u32 *) (info->pseudo_palette))[regno] =
848 ((red & 0xf800)) |
849 ((green & 0xfc00) >> 5) | ((blue & 0xf800) >> 11);
850 }
851 }
852
853 return err;
854}
855
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800856/*
857 * It's common for several clients to have framebuffer open simultaneously.
858 * e.g. both fbcon and X. Makes things interesting.
Bernie Thompson33077b82010-09-05 16:35:19 -0700859 * Assumes caller is holding info->lock (for open and release at least)
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800860 */
861static int dlfb_ops_open(struct fb_info *info, int user)
862{
863 struct dlfb_data *dev = info->par;
864
Bernie Thompsond5ed5432010-09-05 16:35:39 -0700865 /*
866 * fbcon aggressively connects to first framebuffer it finds,
867 * preventing other clients (X) from working properly. Usually
868 * not what the user wants. Fail by default with option to enable.
869 */
Dr. David Alan Gilbertdef76602011-08-21 13:34:15 -0700870 if ((user == 0) && (!console))
Bernie Thompsond5ed5432010-09-05 16:35:39 -0700871 return -EBUSY;
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800872
Bernie Thompson33077b82010-09-05 16:35:19 -0700873 /* If the USB device is gone, we don't accept new opens */
874 if (dev->virtualized)
875 return -ENODEV;
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800876
877 dev->fb_count++;
878
Bernie Thompson33077b82010-09-05 16:35:19 -0700879 kref_get(&dev->kref);
880
Bernie Thompsond5ed5432010-09-05 16:35:39 -0700881 if (fb_defio && (info->fbdefio == NULL)) {
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700882 /* enable defio at last moment if not disabled by client */
883
884 struct fb_deferred_io *fbdefio;
885
Joe Perches31a9f472010-10-31 15:33:55 -0700886 fbdefio = kmalloc(sizeof(struct fb_deferred_io), GFP_KERNEL);
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700887
888 if (fbdefio) {
889 fbdefio->delay = DL_DEFIO_WRITE_DELAY;
890 fbdefio->deferred_io = dlfb_dpy_deferred_io;
891 }
892
893 info->fbdefio = fbdefio;
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800894 fb_deferred_io_init(info);
895 }
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800896
Paul Mundt81f6f3c2011-01-06 18:07:54 +0900897 pr_notice("open /dev/fb%d user=%d fb_info=%p count=%d\n",
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800898 info->node, user, info, dev->fb_count);
899
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700900 return 0;
901}
902
Bernie Thompson4a4854d2010-02-15 06:45:55 -0800903/*
904 * Called when all client interfaces to start transactions have been disabled,
905 * and all references to our device instance (dlfb_data) are released.
906 * Every transaction must have a reference, so we know are fully spun down
907 */
Bernie Thompson33077b82010-09-05 16:35:19 -0700908static void dlfb_free(struct kref *kref)
Bernie Thompson4a4854d2010-02-15 06:45:55 -0800909{
910 struct dlfb_data *dev = container_of(kref, struct dlfb_data, kref);
911
Bernie Thompson33077b82010-09-05 16:35:19 -0700912 /* this function will wait for all in-flight urbs to complete */
913 if (dev->urbs.count > 0)
914 dlfb_free_urb_list(dev);
915
Bernie Thompson4a4854d2010-02-15 06:45:55 -0800916 if (dev->backing_buffer)
917 vfree(dev->backing_buffer);
918
Bernie Thompson33077b82010-09-05 16:35:19 -0700919 kfree(dev->edid);
920
Paul Mundt81f6f3c2011-01-06 18:07:54 +0900921 pr_warn("freeing dlfb_data %p\n", dev);
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800922
Bernie Thompson4a4854d2010-02-15 06:45:55 -0800923 kfree(dev);
924}
925
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700926static void dlfb_release_urb_work(struct work_struct *work)
927{
928 struct urb_node *unode = container_of(work, struct urb_node,
929 release_urb_work.work);
930
931 up(&unode->dev->urbs.limit_sem);
932}
Bernie Thompson33077b82010-09-05 16:35:19 -0700933
934static void dlfb_free_framebuffer_work(struct work_struct *work)
Bernie Thompson2469d5d2010-02-15 06:46:13 -0800935{
Bernie Thompson33077b82010-09-05 16:35:19 -0700936 struct dlfb_data *dev = container_of(work, struct dlfb_data,
937 free_framebuffer_work.work);
938 struct fb_info *info = dev->info;
939 int node = info->node;
940
941 unregister_framebuffer(info);
Bernie Thompson2469d5d2010-02-15 06:46:13 -0800942
943 if (info->cmap.len != 0)
944 fb_dealloc_cmap(&info->cmap);
945 if (info->monspecs.modedb)
946 fb_destroy_modedb(info->monspecs.modedb);
947 if (info->screen_base)
948 vfree(info->screen_base);
949
950 fb_destroy_modelist(&info->modelist);
951
Bernie Thompson33077b82010-09-05 16:35:19 -0700952 dev->info = 0;
953
954 /* Assume info structure is freed after this point */
Bernie Thompson2469d5d2010-02-15 06:46:13 -0800955 framebuffer_release(info);
956
Paul Mundt81f6f3c2011-01-06 18:07:54 +0900957 pr_warn("fb_info for /dev/fb%d has been freed\n", node);
Bernie Thompson33077b82010-09-05 16:35:19 -0700958
959 /* ref taken in probe() as part of registering framebfufer */
960 kref_put(&dev->kref, dlfb_free);
961}
962
963/*
964 * Assumes caller is holding info->lock mutex (for open and release at least)
965 */
966static int dlfb_ops_release(struct fb_info *info, int user)
967{
968 struct dlfb_data *dev = info->par;
969
970 dev->fb_count--;
971
972 /* We can't free fb_info here - fbmem will touch it when we return */
973 if (dev->virtualized && (dev->fb_count == 0))
974 schedule_delayed_work(&dev->free_framebuffer_work, HZ);
975
Bernie Thompson33077b82010-09-05 16:35:19 -0700976 if ((dev->fb_count == 0) && (info->fbdefio)) {
977 fb_deferred_io_cleanup(info);
978 kfree(info->fbdefio);
979 info->fbdefio = NULL;
980 info->fbops->fb_mmap = dlfb_ops_mmap;
981 }
Bernie Thompson33077b82010-09-05 16:35:19 -0700982
Paul Mundt81f6f3c2011-01-06 18:07:54 +0900983 pr_warn("released /dev/fb%d user=%d count=%d\n",
Bernie Thompson33077b82010-09-05 16:35:19 -0700984 info->node, user, dev->fb_count);
985
986 kref_put(&dev->kref, dlfb_free);
987
988 return 0;
Bernie Thompson2469d5d2010-02-15 06:46:13 -0800989}
990
991/*
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800992 * Check whether a video mode is supported by the DisplayLink chip
993 * We start from monitor's modes, so don't need to filter that here
994 */
995static int dlfb_is_valid_mode(struct fb_videomode *mode,
996 struct fb_info *info)
997{
998 struct dlfb_data *dev = info->par;
999
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001000 if (mode->xres * mode->yres > dev->sku_pixel_limit) {
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001001 pr_warn("%dx%d beyond chip capabilities\n",
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001002 mode->xres, mode->yres);
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001003 return 0;
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001004 }
1005
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001006 pr_info("%dx%d valid mode\n", mode->xres, mode->yres);
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001007
1008 return 1;
1009}
1010
1011static void dlfb_var_color_format(struct fb_var_screeninfo *var)
1012{
1013 const struct fb_bitfield red = { 11, 5, 0 };
1014 const struct fb_bitfield green = { 5, 6, 0 };
1015 const struct fb_bitfield blue = { 0, 5, 0 };
1016
1017 var->bits_per_pixel = 16;
1018 var->red = red;
1019 var->green = green;
1020 var->blue = blue;
1021}
1022
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001023static int dlfb_ops_check_var(struct fb_var_screeninfo *var,
1024 struct fb_info *info)
1025{
1026 struct fb_videomode mode;
1027
1028 /* TODO: support dynamically changing framebuffer size */
1029 if ((var->xres * var->yres * 2) > info->fix.smem_len)
1030 return -EINVAL;
1031
1032 /* set device-specific elements of var unrelated to mode */
1033 dlfb_var_color_format(var);
1034
1035 fb_var_to_videomode(&mode, var);
1036
1037 if (!dlfb_is_valid_mode(&mode, info))
1038 return -EINVAL;
1039
1040 return 0;
1041}
1042
1043static int dlfb_ops_set_par(struct fb_info *info)
1044{
1045 struct dlfb_data *dev = info->par;
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001046 int result;
1047 u16 *pix_framebuffer;
1048 int i;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001049
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001050 pr_notice("set_par mode %dx%d\n", info->var.xres, info->var.yres);
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001051
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001052 result = dlfb_set_video_mode(dev, &info->var);
1053
1054 if ((result == 0) && (dev->fb_count == 0)) {
1055
1056 /* paint greenscreen */
1057
1058 pix_framebuffer = (u16 *) info->screen_base;
1059 for (i = 0; i < info->fix.smem_len / 2; i++)
1060 pix_framebuffer[i] = 0x37e6;
1061
1062 dlfb_handle_damage(dev, 0, 0, info->var.xres, info->var.yres,
1063 info->screen_base);
1064 }
1065
1066 return result;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001067}
1068
Bernie Thompson58e7c3b2011-08-21 13:34:11 -07001069/* To fonzi the jukebox (e.g. make blanking changes take effect) */
1070static char *dlfb_dummy_render(char *buf)
1071{
1072 *buf++ = 0xAF;
1073 *buf++ = 0x6A; /* copy */
1074 *buf++ = 0x00; /* from address*/
1075 *buf++ = 0x00;
1076 *buf++ = 0x00;
1077 *buf++ = 0x01; /* one pixel */
1078 *buf++ = 0x00; /* to address */
1079 *buf++ = 0x00;
1080 *buf++ = 0x00;
1081 return buf;
1082}
1083
Bernie Thompson9825f702010-09-05 16:35:10 -07001084/*
1085 * In order to come back from full DPMS off, we need to set the mode again
1086 */
Bernie Thompson45742032010-02-15 06:46:04 -08001087static int dlfb_ops_blank(int blank_mode, struct fb_info *info)
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -07001088{
Bernie Thompson530f43a2010-02-15 06:46:21 -08001089 struct dlfb_data *dev = info->par;
Bernie Thompson58e7c3b2011-08-21 13:34:11 -07001090 char *bufptr;
1091 struct urb *urb;
Roberto De Ioris7316bc52009-06-10 23:02:19 -07001092
Bernie Thompson58e7c3b2011-08-21 13:34:11 -07001093 pr_info("/dev/fb%d FB_BLANK mode %d --> %d\n",
1094 info->node, dev->blank_mode, blank_mode);
Roberto De Ioris7316bc52009-06-10 23:02:19 -07001095
Bernie Thompson58e7c3b2011-08-21 13:34:11 -07001096 if ((dev->blank_mode == FB_BLANK_POWERDOWN) &&
1097 (blank_mode != FB_BLANK_POWERDOWN)) {
Bernie Thompson9825f702010-09-05 16:35:10 -07001098
Bernie Thompson58e7c3b2011-08-21 13:34:11 -07001099 /* returning from powerdown requires a fresh modeset */
Bernie Thompson9825f702010-09-05 16:35:10 -07001100 dlfb_set_video_mode(dev, &info->var);
1101 }
Roberto De Ioris7316bc52009-06-10 23:02:19 -07001102
Bernie Thompson58e7c3b2011-08-21 13:34:11 -07001103 urb = dlfb_get_urb(dev);
1104 if (!urb)
1105 return 0;
1106
1107 bufptr = (char *) urb->transfer_buffer;
1108 bufptr = dlfb_vidreg_lock(bufptr);
1109 bufptr = dlfb_blanking(bufptr, blank_mode);
1110 bufptr = dlfb_vidreg_unlock(bufptr);
1111
1112 /* seems like a render op is needed to have blank change take effect */
1113 bufptr = dlfb_dummy_render(bufptr);
1114
1115 dlfb_submit_urb(dev, urb, bufptr -
1116 (char *) urb->transfer_buffer);
1117
1118 dev->blank_mode = blank_mode;
1119
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001120 return 0;
1121}
1122
1123static struct fb_ops dlfb_ops = {
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001124 .owner = THIS_MODULE,
Paul Mundt1a3e5282011-01-06 17:29:24 +09001125 .fb_read = fb_sys_read,
Bernie Thompsond46ecb92010-09-05 16:35:04 -07001126 .fb_write = dlfb_ops_write,
Bernie Thompson45742032010-02-15 06:46:04 -08001127 .fb_setcolreg = dlfb_ops_setcolreg,
1128 .fb_fillrect = dlfb_ops_fillrect,
1129 .fb_copyarea = dlfb_ops_copyarea,
1130 .fb_imageblit = dlfb_ops_imageblit,
1131 .fb_mmap = dlfb_ops_mmap,
1132 .fb_ioctl = dlfb_ops_ioctl,
Bernie Thompson3e8f3d62010-02-15 06:46:26 -08001133 .fb_open = dlfb_ops_open,
Bernie Thompson45742032010-02-15 06:46:04 -08001134 .fb_release = dlfb_ops_release,
1135 .fb_blank = dlfb_ops_blank,
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001136 .fb_check_var = dlfb_ops_check_var,
1137 .fb_set_par = dlfb_ops_set_par,
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001138};
1139
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001140
Bernie Thompsoncc403dc2010-02-15 06:45:49 -08001141/*
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001142 * Assumes &info->lock held by caller
1143 * Assumes no active clients have framebuffer open
1144 */
1145static int dlfb_realloc_framebuffer(struct dlfb_data *dev, struct fb_info *info)
1146{
1147 int retval = -ENOMEM;
1148 int old_len = info->fix.smem_len;
1149 int new_len;
1150 unsigned char *old_fb = info->screen_base;
1151 unsigned char *new_fb;
Stuart Hopkinsd3189542011-08-21 13:34:17 -07001152 unsigned char *new_back = 0;
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001153
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001154 pr_warn("Reallocating framebuffer. Addresses will change!\n");
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001155
1156 new_len = info->fix.line_length * info->var.yres;
1157
1158 if (PAGE_ALIGN(new_len) > old_len) {
1159 /*
1160 * Alloc system memory for virtual framebuffer
1161 */
1162 new_fb = vmalloc(new_len);
1163 if (!new_fb) {
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001164 pr_err("Virtual framebuffer alloc failed\n");
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001165 goto error;
1166 }
1167
1168 if (info->screen_base) {
1169 memcpy(new_fb, old_fb, old_len);
1170 vfree(info->screen_base);
1171 }
1172
1173 info->screen_base = new_fb;
1174 info->fix.smem_len = PAGE_ALIGN(new_len);
1175 info->fix.smem_start = (unsigned long) new_fb;
1176 info->flags = udlfb_info_flags;
1177
1178 /*
1179 * Second framebuffer copy to mirror the framebuffer state
1180 * on the physical USB device. We can function without this.
1181 * But with imperfect damage info we may send pixels over USB
1182 * that were, in fact, unchanged - wasting limited USB bandwidth
1183 */
Stuart Hopkinsd3189542011-08-21 13:34:17 -07001184 if (shadow)
1185 new_back = vzalloc(new_len);
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001186 if (!new_back)
Linus Torvalds949f6712011-01-10 16:04:53 -08001187 pr_info("No shadow/backing buffer allocated\n");
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001188 else {
1189 if (dev->backing_buffer)
1190 vfree(dev->backing_buffer);
1191 dev->backing_buffer = new_back;
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001192 }
1193 }
1194
1195 retval = 0;
1196
1197error:
1198 return retval;
1199}
1200
1201/*
1202 * 1) Get EDID from hw, or use sw default
1203 * 2) Parse into various fb_info structs
1204 * 3) Allocate virtual framebuffer memory to back highest res mode
1205 *
1206 * Parses EDID into three places used by various parts of fbdev:
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001207 * fb_var_screeninfo contains the timing of the monitor's preferred mode
1208 * fb_info.monspecs is full parsed EDID info, including monspecs.modedb
1209 * fb_info.modelist is a linked list of all monitor & VESA modes which work
1210 *
1211 * If EDID is not readable/valid, then modelist is all VESA modes,
1212 * monspecs is NULL, and fb_var_screeninfo is set to safe VESA mode
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001213 * Returns 0 if successful
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001214 */
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001215static int dlfb_setup_modes(struct dlfb_data *dev,
1216 struct fb_info *info,
1217 char *default_edid, size_t default_edid_size)
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001218{
1219 int i;
1220 const struct fb_videomode *default_vmode = NULL;
1221 int result = 0;
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001222 char *edid;
1223 int tries = 3;
1224
1225 if (info->dev) /* only use mutex if info has been registered */
1226 mutex_lock(&info->lock);
1227
Paul Mundtb9f03a32011-01-06 18:04:02 +09001228 edid = kmalloc(EDID_LENGTH, GFP_KERNEL);
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001229 if (!edid) {
1230 result = -ENOMEM;
1231 goto error;
1232 }
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001233
1234 fb_destroy_modelist(&info->modelist);
1235 memset(&info->monspecs, 0, sizeof(info->monspecs));
1236
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001237 /*
1238 * Try to (re)read EDID from hardware first
1239 * EDID data may return, but not parse as valid
1240 * Try again a few times, in case of e.g. analog cable noise
1241 */
1242 while (tries--) {
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001243
Paul Mundtb9f03a32011-01-06 18:04:02 +09001244 i = dlfb_get_edid(dev, edid, EDID_LENGTH);
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001245
Paul Mundtb9f03a32011-01-06 18:04:02 +09001246 if (i >= EDID_LENGTH)
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001247 fb_edid_to_monspecs(edid, &info->monspecs);
1248
1249 if (info->monspecs.modedb_len > 0) {
1250 dev->edid = edid;
1251 dev->edid_size = i;
1252 break;
1253 }
1254 }
1255
1256 /* If that fails, use a previously returned EDID if available */
1257 if (info->monspecs.modedb_len == 0) {
1258
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001259 pr_err("Unable to get valid EDID from device/display\n");
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001260
1261 if (dev->edid) {
1262 fb_edid_to_monspecs(dev->edid, &info->monspecs);
1263 if (info->monspecs.modedb_len > 0)
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001264 pr_err("Using previously queried EDID\n");
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001265 }
1266 }
1267
1268 /* If that fails, use the default EDID we were handed */
1269 if (info->monspecs.modedb_len == 0) {
Paul Mundtb9f03a32011-01-06 18:04:02 +09001270 if (default_edid_size >= EDID_LENGTH) {
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001271 fb_edid_to_monspecs(default_edid, &info->monspecs);
1272 if (info->monspecs.modedb_len > 0) {
1273 memcpy(edid, default_edid, default_edid_size);
1274 dev->edid = edid;
1275 dev->edid_size = default_edid_size;
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001276 pr_err("Using default/backup EDID\n");
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001277 }
1278 }
1279 }
1280
1281 /* If we've got modes, let's pick a best default mode */
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001282 if (info->monspecs.modedb_len > 0) {
1283
1284 for (i = 0; i < info->monspecs.modedb_len; i++) {
1285 if (dlfb_is_valid_mode(&info->monspecs.modedb[i], info))
1286 fb_add_videomode(&info->monspecs.modedb[i],
1287 &info->modelist);
William Katsak9377c512011-06-23 13:16:29 +00001288 else {
1289 if (i == 0)
1290 /* if we've removed top/best mode */
1291 info->monspecs.misc
1292 &= ~FB_MISC_1ST_DETAIL;
1293 }
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001294 }
1295
1296 default_vmode = fb_find_best_display(&info->monspecs,
1297 &info->modelist);
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001298 }
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001299
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001300 /* If everything else has failed, fall back to safe default mode */
1301 if (default_vmode == NULL) {
1302
1303 struct fb_videomode fb_vmode = {0};
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001304
1305 /*
1306 * Add the standard VESA modes to our modelist
1307 * Since we don't have EDID, there may be modes that
1308 * overspec monitor and/or are incorrect aspect ratio, etc.
1309 * But at least the user has a chance to choose
1310 */
1311 for (i = 0; i < VESA_MODEDB_SIZE; i++) {
1312 if (dlfb_is_valid_mode((struct fb_videomode *)
1313 &vesa_modes[i], info))
1314 fb_add_videomode(&vesa_modes[i],
1315 &info->modelist);
1316 }
1317
1318 /*
1319 * default to resolution safe for projectors
1320 * (since they are most common case without EDID)
1321 */
1322 fb_vmode.xres = 800;
1323 fb_vmode.yres = 600;
1324 fb_vmode.refresh = 60;
1325 default_vmode = fb_find_nearest_mode(&fb_vmode,
1326 &info->modelist);
1327 }
1328
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001329 /* If we have good mode and no active clients*/
1330 if ((default_vmode != NULL) && (dev->fb_count == 0)) {
1331
1332 fb_videomode_to_var(&info->var, default_vmode);
1333 dlfb_var_color_format(&info->var);
1334
1335 /*
1336 * with mode size info, we can now alloc our framebuffer.
1337 */
1338 memcpy(&info->fix, &dlfb_fix, sizeof(dlfb_fix));
1339 info->fix.line_length = info->var.xres *
1340 (info->var.bits_per_pixel / 8);
1341
1342 result = dlfb_realloc_framebuffer(dev, info);
1343
1344 } else
1345 result = -EINVAL;
1346
1347error:
1348 if (edid && (dev->edid != edid))
1349 kfree(edid);
1350
1351 if (info->dev)
1352 mutex_unlock(&info->lock);
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001353
1354 return result;
1355}
1356
1357static ssize_t metrics_bytes_rendered_show(struct device *fbdev,
1358 struct device_attribute *a, char *buf) {
1359 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1360 struct dlfb_data *dev = fb_info->par;
1361 return snprintf(buf, PAGE_SIZE, "%u\n",
1362 atomic_read(&dev->bytes_rendered));
1363}
1364
1365static ssize_t metrics_bytes_identical_show(struct device *fbdev,
1366 struct device_attribute *a, char *buf) {
1367 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1368 struct dlfb_data *dev = fb_info->par;
1369 return snprintf(buf, PAGE_SIZE, "%u\n",
1370 atomic_read(&dev->bytes_identical));
1371}
1372
1373static ssize_t metrics_bytes_sent_show(struct device *fbdev,
1374 struct device_attribute *a, char *buf) {
1375 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1376 struct dlfb_data *dev = fb_info->par;
1377 return snprintf(buf, PAGE_SIZE, "%u\n",
1378 atomic_read(&dev->bytes_sent));
1379}
1380
1381static ssize_t metrics_cpu_kcycles_used_show(struct device *fbdev,
1382 struct device_attribute *a, char *buf) {
1383 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1384 struct dlfb_data *dev = fb_info->par;
1385 return snprintf(buf, PAGE_SIZE, "%u\n",
1386 atomic_read(&dev->cpu_kcycles_used));
1387}
1388
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001389static ssize_t edid_show(
1390 struct file *filp,
1391 struct kobject *kobj, struct bin_attribute *a,
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001392 char *buf, loff_t off, size_t count) {
1393 struct device *fbdev = container_of(kobj, struct device, kobj);
1394 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1395 struct dlfb_data *dev = fb_info->par;
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001396
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001397 if (dev->edid == NULL)
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001398 return 0;
1399
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001400 if ((off >= dev->edid_size) || (count > dev->edid_size))
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001401 return 0;
1402
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001403 if (off + count > dev->edid_size)
1404 count = dev->edid_size - off;
1405
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001406 pr_info("sysfs edid copy %p to %p, %d bytes\n",
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001407 dev->edid, buf, (int) count);
1408
1409 memcpy(buf, dev->edid, count);
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001410
1411 return count;
1412}
1413
Bernie Thompson8ef8cc42010-09-05 16:35:31 -07001414static ssize_t edid_store(
1415 struct file *filp,
1416 struct kobject *kobj, struct bin_attribute *a,
1417 char *src, loff_t src_off, size_t src_size) {
1418 struct device *fbdev = container_of(kobj, struct device, kobj);
1419 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1420 struct dlfb_data *dev = fb_info->par;
1421
1422 /* We only support write of entire EDID at once, no offset*/
Paul Mundtb9f03a32011-01-06 18:04:02 +09001423 if ((src_size != EDID_LENGTH) || (src_off != 0))
Bernie Thompson8ef8cc42010-09-05 16:35:31 -07001424 return 0;
1425
1426 dlfb_setup_modes(dev, fb_info, src, src_size);
1427
1428 if (dev->edid && (memcmp(src, dev->edid, src_size) == 0)) {
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001429 pr_info("sysfs written EDID is new default\n");
Bernie Thompson8ef8cc42010-09-05 16:35:31 -07001430 dlfb_ops_set_par(fb_info);
1431 return src_size;
1432 } else
1433 return 0;
1434}
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001435
1436static ssize_t metrics_reset_store(struct device *fbdev,
1437 struct device_attribute *attr,
1438 const char *buf, size_t count)
1439{
1440 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1441 struct dlfb_data *dev = fb_info->par;
1442
1443 atomic_set(&dev->bytes_rendered, 0);
1444 atomic_set(&dev->bytes_identical, 0);
1445 atomic_set(&dev->bytes_sent, 0);
1446 atomic_set(&dev->cpu_kcycles_used, 0);
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001447
1448 return count;
1449}
1450
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001451static struct bin_attribute edid_attr = {
1452 .attr.name = "edid",
Bernie Thompson8ef8cc42010-09-05 16:35:31 -07001453 .attr.mode = 0666,
Paul Mundtb9f03a32011-01-06 18:04:02 +09001454 .size = EDID_LENGTH,
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001455 .read = edid_show,
Bernie Thompson8ef8cc42010-09-05 16:35:31 -07001456 .write = edid_store
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001457};
1458
1459static struct device_attribute fb_device_attrs[] = {
1460 __ATTR_RO(metrics_bytes_rendered),
1461 __ATTR_RO(metrics_bytes_identical),
1462 __ATTR_RO(metrics_bytes_sent),
1463 __ATTR_RO(metrics_cpu_kcycles_used),
Greg Kroah-Hartman926c1112010-11-18 11:21:04 -08001464 __ATTR(metrics_reset, S_IWUSR, NULL, metrics_reset_store),
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001465};
1466
1467/*
Bernie Thompsoncc403dc2010-02-15 06:45:49 -08001468 * This is necessary before we can communicate with the display controller.
1469 */
1470static int dlfb_select_std_channel(struct dlfb_data *dev)
1471{
1472 int ret;
1473 u8 set_def_chn[] = { 0x57, 0xCD, 0xDC, 0xA7,
1474 0x1C, 0x88, 0x5E, 0x15,
1475 0x60, 0xFE, 0xC6, 0x97,
1476 0x16, 0x3D, 0x47, 0xF2 };
1477
1478 ret = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
1479 NR_USB_REQUEST_CHANNEL,
1480 (USB_DIR_OUT | USB_TYPE_VENDOR), 0, 0,
1481 set_def_chn, sizeof(set_def_chn), USB_CTRL_SET_TIMEOUT);
1482 return ret;
1483}
1484
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001485static int dlfb_parse_vendor_descriptor(struct dlfb_data *dev,
Andrew Kephartf2e1fc92011-08-21 13:34:13 -07001486 struct usb_interface *interface)
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001487{
1488 char *desc;
1489 char *buf;
1490 char *desc_end;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001491
Andrew Kephartf2e1fc92011-08-21 13:34:13 -07001492 int total_len = 0;
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001493
1494 buf = kzalloc(MAX_VENDOR_DESCRIPTOR_SIZE, GFP_KERNEL);
1495 if (!buf)
1496 return false;
1497 desc = buf;
1498
Andrew Kephartf2e1fc92011-08-21 13:34:13 -07001499 total_len = usb_get_descriptor(interface_to_usbdev(interface),
1500 0x5f, /* vendor specific */
1501 0, desc, MAX_VENDOR_DESCRIPTOR_SIZE);
1502
1503 /* if not found, look in configuration descriptor */
1504 if (total_len < 0) {
1505 if (0 == usb_get_extra_descriptor(interface->cur_altsetting,
1506 0x5f, &desc))
1507 total_len = (int) desc[0];
1508 }
1509
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001510 if (total_len > 5) {
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001511 pr_info("vendor descriptor length:%x data:%02x %02x %02x %02x" \
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001512 "%02x %02x %02x %02x %02x %02x %02x\n",
1513 total_len, desc[0],
1514 desc[1], desc[2], desc[3], desc[4], desc[5], desc[6],
1515 desc[7], desc[8], desc[9], desc[10]);
1516
1517 if ((desc[0] != total_len) || /* descriptor length */
1518 (desc[1] != 0x5f) || /* vendor descriptor type */
1519 (desc[2] != 0x01) || /* version (2 bytes) */
1520 (desc[3] != 0x00) ||
1521 (desc[4] != total_len - 2)) /* length after type */
1522 goto unrecognized;
1523
1524 desc_end = desc + total_len;
1525 desc += 5; /* the fixed header we've already parsed */
1526
1527 while (desc < desc_end) {
1528 u8 length;
1529 u16 key;
1530
1531 key = *((u16 *) desc);
1532 desc += sizeof(u16);
1533 length = *desc;
1534 desc++;
1535
1536 switch (key) {
1537 case 0x0200: { /* max_area */
1538 u32 max_area;
1539 max_area = le32_to_cpu(*((u32 *)desc));
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001540 pr_warn("DL chip limited to %d pixel modes\n",
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001541 max_area);
1542 dev->sku_pixel_limit = max_area;
1543 break;
1544 }
1545 default:
1546 break;
1547 }
1548 desc += length;
1549 }
Andrew Kephartf2e1fc92011-08-21 13:34:13 -07001550 } else {
1551 pr_info("vendor descriptor not available (%d)\n", total_len);
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001552 }
1553
1554 goto success;
1555
1556unrecognized:
1557 /* allow udlfb to load for now even if firmware unrecognized */
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001558 pr_err("Unrecognized vendor firmware descriptor\n");
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001559
1560success:
1561 kfree(buf);
1562 return true;
1563}
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001564static int dlfb_usb_probe(struct usb_interface *interface,
Bernie Thompson59277b62009-11-24 15:52:21 -08001565 const struct usb_device_id *id)
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001566{
Bernie Thompson59277b62009-11-24 15:52:21 -08001567 struct usb_device *usbdev;
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001568 struct dlfb_data *dev = 0;
Bernie Thompson33077b82010-09-05 16:35:19 -07001569 struct fb_info *info = 0;
Bernie Thompson59277b62009-11-24 15:52:21 -08001570 int retval = -ENOMEM;
Bernie Thompson2685cff2010-09-05 18:29:56 -07001571 int i;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001572
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001573 /* usb initialization */
1574
1575 usbdev = interface_to_usbdev(interface);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001576
Bernie Thompson59277b62009-11-24 15:52:21 -08001577 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1578 if (dev == NULL) {
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001579 err("dlfb_usb_probe: failed alloc of dev struct\n");
1580 goto error;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001581 }
1582
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001583 /* we need to wait for both usb and fbdev to spin down on disconnect */
1584 kref_init(&dev->kref); /* matching kref_put in usb .disconnect fn */
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001585 kref_get(&dev->kref); /* matching kref_put in free_framebuffer_work */
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001586
Bernie Thompson59277b62009-11-24 15:52:21 -08001587 dev->udev = usbdev;
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001588 dev->gdev = &usbdev->dev; /* our generic struct device * */
Bernie Thompson59277b62009-11-24 15:52:21 -08001589 usb_set_intfdata(interface, dev);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001590
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001591 pr_info("%s %s - serial #%s\n",
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001592 usbdev->manufacturer, usbdev->product, usbdev->serial);
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001593 pr_info("vid_%04x&pid_%04x&rev_%04x driver's dlfb_data struct at %p\n",
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001594 usbdev->descriptor.idVendor, usbdev->descriptor.idProduct,
1595 usbdev->descriptor.bcdDevice, dev);
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001596 pr_info("console enable=%d\n", console);
1597 pr_info("fb_defio enable=%d\n", fb_defio);
Stuart Hopkinsd3189542011-08-21 13:34:17 -07001598 pr_info("shadow enable=%d\n", shadow);
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001599
1600 dev->sku_pixel_limit = 2048 * 1152; /* default to maximum */
1601
Andrew Kephartf2e1fc92011-08-21 13:34:13 -07001602 if (!dlfb_parse_vendor_descriptor(dev, interface)) {
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001603 pr_err("firmware not recognized. Assume incompatible device\n");
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001604 goto error;
1605 }
1606
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001607 if (!dlfb_alloc_urb_list(dev, WRITES_IN_FLIGHT, MAX_TRANSFER)) {
1608 retval = -ENOMEM;
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001609 pr_err("dlfb_alloc_urb_list failed\n");
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001610 goto error;
1611 }
1612
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001613 /* We don't register a new USB class. Our client interface is fbdev */
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001614
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001615 /* allocates framebuffer driver structure, not framebuffer memory */
1616 info = framebuffer_alloc(0, &usbdev->dev);
1617 if (!info) {
1618 retval = -ENOMEM;
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001619 pr_err("framebuffer_alloc failed\n");
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001620 goto error;
1621 }
Bernie Thompson33077b82010-09-05 16:35:19 -07001622
Bernie Thompson59277b62009-11-24 15:52:21 -08001623 dev->info = info;
1624 info->par = dev;
1625 info->pseudo_palette = dev->pseudo_palette;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001626 info->fbops = &dlfb_ops;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001627
Bernie Thompson59277b62009-11-24 15:52:21 -08001628 retval = fb_alloc_cmap(&info->cmap, 256, 0);
1629 if (retval < 0) {
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001630 pr_err("fb_alloc_cmap failed %x\n", retval);
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001631 goto error;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001632 }
1633
Bernie Thompson33077b82010-09-05 16:35:19 -07001634 INIT_DELAYED_WORK(&dev->free_framebuffer_work,
1635 dlfb_free_framebuffer_work);
1636
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001637 INIT_LIST_HEAD(&info->modelist);
1638
1639 retval = dlfb_setup_modes(dev, info, NULL, 0);
1640 if (retval != 0) {
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001641 pr_err("unable to find common mode for display and adapter\n");
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001642 goto error;
1643 }
1644
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001645 /* ready to begin using device */
1646
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001647 atomic_set(&dev->usb_active, 1);
Bernie Thompson59277b62009-11-24 15:52:21 -08001648 dlfb_select_std_channel(dev);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001649
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001650 dlfb_ops_check_var(&info->var, info);
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001651 dlfb_ops_set_par(info);
1652
Bernie Thompson59277b62009-11-24 15:52:21 -08001653 retval = register_framebuffer(info);
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001654 if (retval < 0) {
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001655 pr_err("register_framebuffer failed %d\n", retval);
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001656 goto error;
1657 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001658
Liu Yuan94cd1ae2011-04-18 10:44:38 +00001659 for (i = 0; i < ARRAY_SIZE(fb_device_attrs); i++) {
1660 retval = device_create_file(info->dev, &fb_device_attrs[i]);
1661 if (retval) {
1662 pr_err("device_create_file failed %d\n", retval);
1663 goto err_del_attrs;
1664 }
1665 }
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -07001666
Liu Yuan94cd1ae2011-04-18 10:44:38 +00001667 retval = device_create_bin_file(info->dev, &edid_attr);
1668 if (retval) {
1669 pr_err("device_create_bin_file failed %d\n", retval);
1670 goto err_del_attrs;
1671 }
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001672
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001673 pr_info("DisplayLink USB device /dev/fb%d attached. %dx%d resolution."
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001674 " Using %dK framebuffer memory\n", info->node,
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001675 info->var.xres, info->var.yres,
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001676 ((dev->backing_buffer) ?
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001677 info->fix.smem_len * 2 : info->fix.smem_len) >> 10);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001678 return 0;
1679
Liu Yuan94cd1ae2011-04-18 10:44:38 +00001680err_del_attrs:
1681 for (i -= 1; i >= 0; i--)
1682 device_remove_file(info->dev, &fb_device_attrs[i]);
1683
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001684error:
1685 if (dev) {
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001686
Bernie Thompson33077b82010-09-05 16:35:19 -07001687 if (info) {
1688 if (info->cmap.len != 0)
1689 fb_dealloc_cmap(&info->cmap);
1690 if (info->monspecs.modedb)
1691 fb_destroy_modedb(info->monspecs.modedb);
1692 if (info->screen_base)
1693 vfree(info->screen_base);
1694
1695 fb_destroy_modelist(&info->modelist);
1696
1697 framebuffer_release(info);
1698 }
1699
1700 if (dev->backing_buffer)
1701 vfree(dev->backing_buffer);
1702
1703 kref_put(&dev->kref, dlfb_free); /* ref for framebuffer */
1704 kref_put(&dev->kref, dlfb_free); /* last ref from kref_init */
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001705
1706 /* dev has been deallocated. Do not dereference */
1707 }
1708
Bernie Thompson59277b62009-11-24 15:52:21 -08001709 return retval;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001710}
1711
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001712static void dlfb_usb_disconnect(struct usb_interface *interface)
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001713{
Bernie Thompson59277b62009-11-24 15:52:21 -08001714 struct dlfb_data *dev;
1715 struct fb_info *info;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001716 int i;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001717
Bernie Thompson59277b62009-11-24 15:52:21 -08001718 dev = usb_get_intfdata(interface);
Bernie Thompson59277b62009-11-24 15:52:21 -08001719 info = dev->info;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001720
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001721 pr_info("USB disconnect starting\n");
Bernie Thompson33077b82010-09-05 16:35:19 -07001722
1723 /* we virtualize until all fb clients release. Then we free */
1724 dev->virtualized = true;
1725
1726 /* When non-active we'll update virtual framebuffer, but no new urbs */
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001727 atomic_set(&dev->usb_active, 0);
1728
Bernie Thompson33077b82010-09-05 16:35:19 -07001729 /* remove udlfb's sysfs interfaces */
1730 for (i = 0; i < ARRAY_SIZE(fb_device_attrs); i++)
1731 device_remove_file(info->dev, &fb_device_attrs[i]);
1732 device_remove_bin_file(info->dev, &edid_attr);
1733
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001734 usb_set_intfdata(interface, NULL);
1735
Bernie Thompson33077b82010-09-05 16:35:19 -07001736 /* if clients still have us open, will be freed on last close */
1737 if (dev->fb_count == 0)
1738 schedule_delayed_work(&dev->free_framebuffer_work, 0);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001739
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001740 /* release reference taken by kref_init in probe() */
Bernie Thompson33077b82010-09-05 16:35:19 -07001741 kref_put(&dev->kref, dlfb_free);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001742
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001743 /* consider dlfb_data freed */
1744
1745 return;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001746}
1747
1748static struct usb_driver dlfb_driver = {
1749 .name = "udlfb",
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001750 .probe = dlfb_usb_probe,
1751 .disconnect = dlfb_usb_disconnect,
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001752 .id_table = id_table,
1753};
1754
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001755static int __init dlfb_module_init(void)
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001756{
1757 int res;
1758
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001759 res = usb_register(&dlfb_driver);
1760 if (res)
1761 err("usb_register failed. Error number %d", res);
1762
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001763 return res;
1764}
1765
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001766static void __exit dlfb_module_exit(void)
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001767{
1768 usb_deregister(&dlfb_driver);
1769}
1770
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001771module_init(dlfb_module_init);
1772module_exit(dlfb_module_exit);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001773
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001774static void dlfb_urb_completion(struct urb *urb)
1775{
1776 struct urb_node *unode = urb->context;
1777 struct dlfb_data *dev = unode->dev;
1778 unsigned long flags;
1779
1780 /* sync/async unlink faults aren't errors */
1781 if (urb->status) {
1782 if (!(urb->status == -ENOENT ||
1783 urb->status == -ECONNRESET ||
1784 urb->status == -ESHUTDOWN)) {
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001785 pr_err("%s - nonzero write bulk status received: %d\n",
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001786 __func__, urb->status);
1787 atomic_set(&dev->lost_pixels, 1);
1788 }
1789 }
1790
1791 urb->transfer_buffer_length = dev->urbs.size; /* reset to actual */
1792
1793 spin_lock_irqsave(&dev->urbs.lock, flags);
1794 list_add_tail(&unode->entry, &dev->urbs.list);
1795 dev->urbs.available++;
1796 spin_unlock_irqrestore(&dev->urbs.lock, flags);
1797
Bernie Thompson5bea1fb2010-09-05 18:28:29 -07001798 /*
1799 * When using fb_defio, we deadlock if up() is called
1800 * while another is waiting. So queue to another process.
1801 */
1802 if (fb_defio)
1803 schedule_delayed_work(&unode->release_urb_work, 0);
1804 else
1805 up(&dev->urbs.limit_sem);
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001806}
1807
1808static void dlfb_free_urb_list(struct dlfb_data *dev)
1809{
1810 int count = dev->urbs.count;
1811 struct list_head *node;
1812 struct urb_node *unode;
1813 struct urb *urb;
1814 int ret;
1815 unsigned long flags;
1816
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001817 pr_notice("Waiting for completes and freeing all render urbs\n");
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001818
1819 /* keep waiting and freeing, until we've got 'em all */
1820 while (count--) {
Bernie Thompson33077b82010-09-05 16:35:19 -07001821
1822 /* Getting interrupted means a leak, but ok at shutdown*/
1823 ret = down_interruptible(&dev->urbs.limit_sem);
1824 if (ret)
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001825 break;
Bernie Thompson33077b82010-09-05 16:35:19 -07001826
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001827 spin_lock_irqsave(&dev->urbs.lock, flags);
1828
1829 node = dev->urbs.list.next; /* have reserved one with sem */
1830 list_del_init(node);
1831
1832 spin_unlock_irqrestore(&dev->urbs.lock, flags);
1833
1834 unode = list_entry(node, struct urb_node, entry);
1835 urb = unode->urb;
1836
1837 /* Free each separately allocated piece */
Greg Kroah-Hartmanc220cc32010-04-29 15:36:29 -07001838 usb_free_coherent(urb->dev, dev->urbs.size,
1839 urb->transfer_buffer, urb->transfer_dma);
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001840 usb_free_urb(urb);
1841 kfree(node);
1842 }
1843
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001844}
1845
1846static int dlfb_alloc_urb_list(struct dlfb_data *dev, int count, size_t size)
1847{
1848 int i = 0;
1849 struct urb *urb;
1850 struct urb_node *unode;
1851 char *buf;
1852
1853 spin_lock_init(&dev->urbs.lock);
1854
1855 dev->urbs.size = size;
1856 INIT_LIST_HEAD(&dev->urbs.list);
1857
1858 while (i < count) {
1859 unode = kzalloc(sizeof(struct urb_node), GFP_KERNEL);
1860 if (!unode)
1861 break;
1862 unode->dev = dev;
1863
Bernie Thompson5bea1fb2010-09-05 18:28:29 -07001864 INIT_DELAYED_WORK(&unode->release_urb_work,
1865 dlfb_release_urb_work);
1866
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001867 urb = usb_alloc_urb(0, GFP_KERNEL);
1868 if (!urb) {
1869 kfree(unode);
1870 break;
1871 }
1872 unode->urb = urb;
1873
Greg Kroah-Hartmanc220cc32010-04-29 15:36:29 -07001874 buf = usb_alloc_coherent(dev->udev, MAX_TRANSFER, GFP_KERNEL,
1875 &urb->transfer_dma);
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001876 if (!buf) {
1877 kfree(unode);
1878 usb_free_urb(urb);
1879 break;
1880 }
1881
1882 /* urb->transfer_buffer_length set to actual before submit */
1883 usb_fill_bulk_urb(urb, dev->udev, usb_sndbulkpipe(dev->udev, 1),
1884 buf, size, dlfb_urb_completion, unode);
1885 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1886
1887 list_add_tail(&unode->entry, &dev->urbs.list);
1888
1889 i++;
1890 }
1891
1892 sema_init(&dev->urbs.limit_sem, i);
1893 dev->urbs.count = i;
1894 dev->urbs.available = i;
1895
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001896 pr_notice("allocated %d %d byte urbs\n", i, (int) size);
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001897
1898 return i;
1899}
1900
1901static struct urb *dlfb_get_urb(struct dlfb_data *dev)
1902{
1903 int ret = 0;
1904 struct list_head *entry;
1905 struct urb_node *unode;
1906 struct urb *urb = NULL;
1907 unsigned long flags;
1908
1909 /* Wait for an in-flight buffer to complete and get re-queued */
1910 ret = down_timeout(&dev->urbs.limit_sem, GET_URB_TIMEOUT);
1911 if (ret) {
1912 atomic_set(&dev->lost_pixels, 1);
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001913 pr_warn("wait for urb interrupted: %x available: %d\n",
Bernie Thompson5bea1fb2010-09-05 18:28:29 -07001914 ret, dev->urbs.available);
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001915 goto error;
1916 }
1917
1918 spin_lock_irqsave(&dev->urbs.lock, flags);
1919
1920 BUG_ON(list_empty(&dev->urbs.list)); /* reserved one with limit_sem */
1921 entry = dev->urbs.list.next;
1922 list_del_init(entry);
1923 dev->urbs.available--;
1924
1925 spin_unlock_irqrestore(&dev->urbs.lock, flags);
1926
1927 unode = list_entry(entry, struct urb_node, entry);
1928 urb = unode->urb;
1929
1930error:
1931 return urb;
1932}
1933
1934static int dlfb_submit_urb(struct dlfb_data *dev, struct urb *urb, size_t len)
1935{
1936 int ret;
1937
1938 BUG_ON(len > dev->urbs.size);
1939
1940 urb->transfer_buffer_length = len; /* set to actual payload len */
1941 ret = usb_submit_urb(urb, GFP_KERNEL);
1942 if (ret) {
1943 dlfb_urb_completion(urb); /* because no one else will */
1944 atomic_set(&dev->lost_pixels, 1);
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001945 pr_err("usb_submit_urb error %x\n", ret);
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001946 }
1947 return ret;
1948}
1949
Bernie Thompsond5ed5432010-09-05 16:35:39 -07001950module_param(console, bool, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
1951MODULE_PARM_DESC(console, "Allow fbcon to consume first framebuffer found");
1952
1953module_param(fb_defio, bool, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
1954MODULE_PARM_DESC(fb_defio, "Enable fb_defio mmap support. *Experimental*");
1955
Stuart Hopkinsd3189542011-08-21 13:34:17 -07001956module_param(shadow, bool, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
1957MODULE_PARM_DESC(shadow, "Shadow vid mem. Disable to save mem but lose perf");
1958
Bernie Thompson59277b62009-11-24 15:52:21 -08001959MODULE_AUTHOR("Roberto De Ioris <roberto@unbit.it>, "
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001960 "Jaya Kumar <jayakumar.lkml@gmail.com>, "
1961 "Bernie Thompson <bernie@plugable.com>");
1962MODULE_DESCRIPTION("DisplayLink kernel framebuffer driver");
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001963MODULE_LICENSE("GPL");
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001964