blob: 37498abcd917615fdc3675fc3d27dee81642a96b [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 */
Bernie Thompsondd8015f2010-02-15 06:46:35 -080065
Bernie Thompson4a4854d2010-02-15 06:45:55 -080066/* dlfb keeps a list of urbs for efficient bulk transfers */
67static void dlfb_urb_completion(struct urb *urb);
68static struct urb *dlfb_get_urb(struct dlfb_data *dev);
69static int dlfb_submit_urb(struct dlfb_data *dev, struct urb * urb, size_t len);
70static int dlfb_alloc_urb_list(struct dlfb_data *dev, int count, size_t size);
71static void dlfb_free_urb_list(struct dlfb_data *dev);
72
Bernie Thompson59277b62009-11-24 15:52:21 -080073/*
Bernie Thompsonbd808162010-02-15 06:46:48 -080074 * All DisplayLink bulk operations start with 0xAF, followed by specific code
75 * All operations are written to buffers which then later get sent to device
Bernie Thompson59277b62009-11-24 15:52:21 -080076 */
Bernie Thompson45742032010-02-15 06:46:04 -080077static char *dlfb_set_register(char *buf, u8 reg, u8 val)
Roberto De Ioris88e58b12009-06-03 14:03:06 -070078{
Bernie Thompson1d31a9e2010-02-15 06:45:43 -080079 *buf++ = 0xAF;
80 *buf++ = 0x20;
81 *buf++ = reg;
82 *buf++ = val;
83 return buf;
Roberto De Ioris88e58b12009-06-03 14:03:06 -070084}
85
Bernie Thompson45742032010-02-15 06:46:04 -080086static char *dlfb_vidreg_lock(char *buf)
Roberto De Ioris88e58b12009-06-03 14:03:06 -070087{
Bernie Thompson45742032010-02-15 06:46:04 -080088 return dlfb_set_register(buf, 0xFF, 0x00);
Bernie Thompson59277b62009-11-24 15:52:21 -080089}
Roberto De Ioris88e58b12009-06-03 14:03:06 -070090
Bernie Thompson45742032010-02-15 06:46:04 -080091static char *dlfb_vidreg_unlock(char *buf)
Bernie Thompson59277b62009-11-24 15:52:21 -080092{
Bernie Thompson45742032010-02-15 06:46:04 -080093 return dlfb_set_register(buf, 0xFF, 0xFF);
Bernie Thompson59277b62009-11-24 15:52:21 -080094}
Roberto De Ioris88e58b12009-06-03 14:03:06 -070095
Bernie Thompson59277b62009-11-24 15:52:21 -080096/*
Bernie Thompson58e7c3b2011-08-21 13:34:11 -070097 * Map FB_BLANK_* to DisplayLink register
98 * DLReg FB_BLANK_*
99 * ----- -----------------------------
100 * 0x00 FB_BLANK_UNBLANK (0)
101 * 0x01 FB_BLANK (1)
102 * 0x03 FB_BLANK_VSYNC_SUSPEND (2)
103 * 0x05 FB_BLANK_HSYNC_SUSPEND (3)
104 * 0x07 FB_BLANK_POWERDOWN (4) Note: requires modeset to come back
Bernie Thompson59277b62009-11-24 15:52:21 -0800105 */
Bernie Thompson58e7c3b2011-08-21 13:34:11 -0700106static char *dlfb_blanking(char *buf, int fb_blank)
Bernie Thompson59277b62009-11-24 15:52:21 -0800107{
Bernie Thompson58e7c3b2011-08-21 13:34:11 -0700108 u8 reg;
109
110 switch (fb_blank) {
111 case FB_BLANK_POWERDOWN:
112 reg = 0x07;
113 break;
114 case FB_BLANK_HSYNC_SUSPEND:
115 reg = 0x05;
116 break;
117 case FB_BLANK_VSYNC_SUSPEND:
118 reg = 0x03;
119 break;
120 case FB_BLANK_NORMAL:
121 reg = 0x01;
122 break;
123 default:
124 reg = 0x00;
125 }
126
127 buf = dlfb_set_register(buf, 0x1F, reg);
128
129 return buf;
Bernie Thompson59277b62009-11-24 15:52:21 -0800130}
131
Bernie Thompson45742032010-02-15 06:46:04 -0800132static char *dlfb_set_color_depth(char *buf, u8 selection)
Bernie Thompson59277b62009-11-24 15:52:21 -0800133{
Bernie Thompson45742032010-02-15 06:46:04 -0800134 return dlfb_set_register(buf, 0x00, selection);
Bernie Thompson59277b62009-11-24 15:52:21 -0800135}
136
Bernie Thompson45742032010-02-15 06:46:04 -0800137static char *dlfb_set_base16bpp(char *wrptr, u32 base)
Bernie Thompson59277b62009-11-24 15:52:21 -0800138{
Bernie Thompson1d31a9e2010-02-15 06:45:43 -0800139 /* the base pointer is 16 bits wide, 0x20 is hi byte. */
Bernie Thompson45742032010-02-15 06:46:04 -0800140 wrptr = dlfb_set_register(wrptr, 0x20, base >> 16);
141 wrptr = dlfb_set_register(wrptr, 0x21, base >> 8);
142 return dlfb_set_register(wrptr, 0x22, base);
Bernie Thompson59277b62009-11-24 15:52:21 -0800143}
144
Bernie Thompsonbd808162010-02-15 06:46:48 -0800145/*
146 * DisplayLink HW has separate 16bpp and 8bpp framebuffers.
147 * In 24bpp modes, the low 323 RGB bits go in the 8bpp framebuffer
148 */
Bernie Thompson45742032010-02-15 06:46:04 -0800149static char *dlfb_set_base8bpp(char *wrptr, u32 base)
Bernie Thompson59277b62009-11-24 15:52:21 -0800150{
Bernie Thompson45742032010-02-15 06:46:04 -0800151 wrptr = dlfb_set_register(wrptr, 0x26, base >> 16);
152 wrptr = dlfb_set_register(wrptr, 0x27, base >> 8);
153 return dlfb_set_register(wrptr, 0x28, base);
Bernie Thompson59277b62009-11-24 15:52:21 -0800154}
155
Bernie Thompson45742032010-02-15 06:46:04 -0800156static char *dlfb_set_register_16(char *wrptr, u8 reg, u16 value)
Bernie Thompson59277b62009-11-24 15:52:21 -0800157{
Bernie Thompson45742032010-02-15 06:46:04 -0800158 wrptr = dlfb_set_register(wrptr, reg, value >> 8);
159 return dlfb_set_register(wrptr, reg+1, value);
Bernie Thompson59277b62009-11-24 15:52:21 -0800160}
161
162/*
163 * This is kind of weird because the controller takes some
164 * register values in a different byte order than other registers.
165 */
Bernie Thompson45742032010-02-15 06:46:04 -0800166static char *dlfb_set_register_16be(char *wrptr, u8 reg, u16 value)
Bernie Thompson59277b62009-11-24 15:52:21 -0800167{
Bernie Thompson45742032010-02-15 06:46:04 -0800168 wrptr = dlfb_set_register(wrptr, reg, value);
169 return dlfb_set_register(wrptr, reg+1, value >> 8);
Bernie Thompson59277b62009-11-24 15:52:21 -0800170}
171
172/*
173 * LFSR is linear feedback shift register. The reason we have this is
174 * because the display controller needs to minimize the clock depth of
175 * various counters used in the display path. So this code reverses the
176 * provided value into the lfsr16 value by counting backwards to get
177 * the value that needs to be set in the hardware comparator to get the
178 * same actual count. This makes sense once you read above a couple of
179 * times and think about it from a hardware perspective.
180 */
Bernie Thompsonbd808162010-02-15 06:46:48 -0800181static u16 dlfb_lfsr16(u16 actual_count)
Bernie Thompson59277b62009-11-24 15:52:21 -0800182{
183 u32 lv = 0xFFFF; /* This is the lfsr value that the hw starts with */
184
185 while (actual_count--) {
186 lv = ((lv << 1) |
187 (((lv >> 15) ^ (lv >> 4) ^ (lv >> 2) ^ (lv >> 1)) & 1))
188 & 0xFFFF;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700189 }
Bernie Thompson59277b62009-11-24 15:52:21 -0800190
191 return (u16) lv;
192}
193
194/*
195 * This does LFSR conversion on the value that is to be written.
196 * See LFSR explanation above for more detail.
197 */
Bernie Thompson45742032010-02-15 06:46:04 -0800198static char *dlfb_set_register_lfsr16(char *wrptr, u8 reg, u16 value)
Bernie Thompson59277b62009-11-24 15:52:21 -0800199{
Bernie Thompsonbd808162010-02-15 06:46:48 -0800200 return dlfb_set_register_16(wrptr, reg, dlfb_lfsr16(value));
Bernie Thompson59277b62009-11-24 15:52:21 -0800201}
202
203/*
204 * This takes a standard fbdev screeninfo struct and all of its monitor mode
205 * details and converts them into the DisplayLink equivalent register commands.
206 */
Bernie Thompson45742032010-02-15 06:46:04 -0800207static char *dlfb_set_vid_cmds(char *wrptr, struct fb_var_screeninfo *var)
Bernie Thompson59277b62009-11-24 15:52:21 -0800208{
209 u16 xds, yds;
210 u16 xde, yde;
211 u16 yec;
212
Bernie Thompson59277b62009-11-24 15:52:21 -0800213 /* x display start */
214 xds = var->left_margin + var->hsync_len;
Bernie Thompson45742032010-02-15 06:46:04 -0800215 wrptr = dlfb_set_register_lfsr16(wrptr, 0x01, xds);
Bernie Thompson59277b62009-11-24 15:52:21 -0800216 /* x display end */
217 xde = xds + var->xres;
Bernie Thompson45742032010-02-15 06:46:04 -0800218 wrptr = dlfb_set_register_lfsr16(wrptr, 0x03, xde);
Bernie Thompson59277b62009-11-24 15:52:21 -0800219
220 /* y display start */
221 yds = var->upper_margin + var->vsync_len;
Bernie Thompson45742032010-02-15 06:46:04 -0800222 wrptr = dlfb_set_register_lfsr16(wrptr, 0x05, yds);
Bernie Thompson59277b62009-11-24 15:52:21 -0800223 /* y display end */
224 yde = yds + var->yres;
Bernie Thompson45742032010-02-15 06:46:04 -0800225 wrptr = dlfb_set_register_lfsr16(wrptr, 0x07, yde);
Bernie Thompson59277b62009-11-24 15:52:21 -0800226
227 /* x end count is active + blanking - 1 */
Bernie Thompson45742032010-02-15 06:46:04 -0800228 wrptr = dlfb_set_register_lfsr16(wrptr, 0x09,
229 xde + var->right_margin - 1);
Bernie Thompson59277b62009-11-24 15:52:21 -0800230
231 /* libdlo hardcodes hsync start to 1 */
Bernie Thompson45742032010-02-15 06:46:04 -0800232 wrptr = dlfb_set_register_lfsr16(wrptr, 0x0B, 1);
Bernie Thompson59277b62009-11-24 15:52:21 -0800233
234 /* hsync end is width of sync pulse + 1 */
Bernie Thompson45742032010-02-15 06:46:04 -0800235 wrptr = dlfb_set_register_lfsr16(wrptr, 0x0D, var->hsync_len + 1);
Bernie Thompson59277b62009-11-24 15:52:21 -0800236
237 /* hpixels is active pixels */
Bernie Thompson45742032010-02-15 06:46:04 -0800238 wrptr = dlfb_set_register_16(wrptr, 0x0F, var->xres);
Bernie Thompson59277b62009-11-24 15:52:21 -0800239
240 /* yendcount is vertical active + vertical blanking */
241 yec = var->yres + var->upper_margin + var->lower_margin +
242 var->vsync_len;
Bernie Thompson45742032010-02-15 06:46:04 -0800243 wrptr = dlfb_set_register_lfsr16(wrptr, 0x11, yec);
Bernie Thompson59277b62009-11-24 15:52:21 -0800244
245 /* libdlo hardcodes vsync start to 0 */
Bernie Thompson45742032010-02-15 06:46:04 -0800246 wrptr = dlfb_set_register_lfsr16(wrptr, 0x13, 0);
Bernie Thompson59277b62009-11-24 15:52:21 -0800247
248 /* vsync end is width of vsync pulse */
Bernie Thompson45742032010-02-15 06:46:04 -0800249 wrptr = dlfb_set_register_lfsr16(wrptr, 0x15, var->vsync_len);
Bernie Thompson59277b62009-11-24 15:52:21 -0800250
251 /* vpixels is active pixels */
Bernie Thompson45742032010-02-15 06:46:04 -0800252 wrptr = dlfb_set_register_16(wrptr, 0x17, var->yres);
Bernie Thompson59277b62009-11-24 15:52:21 -0800253
254 /* convert picoseconds to 5kHz multiple for pclk5k = x * 1E12/5k */
Bernie Thompson45742032010-02-15 06:46:04 -0800255 wrptr = dlfb_set_register_16be(wrptr, 0x1B,
256 200*1000*1000/var->pixclock);
Bernie Thompson59277b62009-11-24 15:52:21 -0800257
258 return wrptr;
259}
260
261/*
262 * This takes a standard fbdev screeninfo struct that was fetched or prepared
263 * and then generates the appropriate command sequence that then drives the
264 * display controller.
265 */
266static int dlfb_set_video_mode(struct dlfb_data *dev,
267 struct fb_var_screeninfo *var)
268{
269 char *buf;
270 char *wrptr;
271 int retval = 0;
272 int writesize;
Bernie Thompson530f43a2010-02-15 06:46:21 -0800273 struct urb *urb;
Bernie Thompson59277b62009-11-24 15:52:21 -0800274
Bernie Thompson530f43a2010-02-15 06:46:21 -0800275 if (!atomic_read(&dev->usb_active))
276 return -EPERM;
277
278 urb = dlfb_get_urb(dev);
279 if (!urb)
280 return -ENOMEM;
Bernie Thompson2685cff2010-09-05 18:29:56 -0700281
Bernie Thompson530f43a2010-02-15 06:46:21 -0800282 buf = (char *) urb->transfer_buffer;
Bernie Thompson59277b62009-11-24 15:52:21 -0800283
284 /*
285 * This first section has to do with setting the base address on the
286 * controller * associated with the display. There are 2 base
287 * pointers, currently, we only * use the 16 bpp segment.
288 */
Bernie Thompson45742032010-02-15 06:46:04 -0800289 wrptr = dlfb_vidreg_lock(buf);
290 wrptr = dlfb_set_color_depth(wrptr, 0x00);
Bernie Thompson59277b62009-11-24 15:52:21 -0800291 /* set base for 16bpp segment to 0 */
Bernie Thompson45742032010-02-15 06:46:04 -0800292 wrptr = dlfb_set_base16bpp(wrptr, 0);
Bernie Thompson59277b62009-11-24 15:52:21 -0800293 /* set base for 8bpp segment to end of fb */
Bernie Thompson45742032010-02-15 06:46:04 -0800294 wrptr = dlfb_set_base8bpp(wrptr, dev->info->fix.smem_len);
Bernie Thompson59277b62009-11-24 15:52:21 -0800295
Bernie Thompson45742032010-02-15 06:46:04 -0800296 wrptr = dlfb_set_vid_cmds(wrptr, var);
Bernie Thompson58e7c3b2011-08-21 13:34:11 -0700297 wrptr = dlfb_blanking(wrptr, FB_BLANK_UNBLANK);
Bernie Thompson45742032010-02-15 06:46:04 -0800298 wrptr = dlfb_vidreg_unlock(wrptr);
Bernie Thompson59277b62009-11-24 15:52:21 -0800299
300 writesize = wrptr - buf;
301
Bernie Thompson530f43a2010-02-15 06:46:21 -0800302 retval = dlfb_submit_urb(dev, urb, writesize);
Bernie Thompson59277b62009-11-24 15:52:21 -0800303
Bernie Thompson58e7c3b2011-08-21 13:34:11 -0700304 dev->blank_mode = FB_BLANK_UNBLANK;
305
Bernie Thompson59277b62009-11-24 15:52:21 -0800306 return retval;
307}
308
Bernie Thompson45742032010-02-15 06:46:04 -0800309static int dlfb_ops_mmap(struct fb_info *info, struct vm_area_struct *vma)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700310{
311 unsigned long start = vma->vm_start;
312 unsigned long size = vma->vm_end - vma->vm_start;
313 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
314 unsigned long page, pos;
315
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700316 if (offset + size > info->fix.smem_len)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700317 return -EINVAL;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700318
319 pos = (unsigned long)info->fix.smem_start + offset;
320
Paul Mundt81f6f3c2011-01-06 18:07:54 +0900321 pr_notice("mmap() framebuffer addr:%lu size:%lu\n",
Bernie Thompson2685cff2010-09-05 18:29:56 -0700322 pos, size);
323
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700324 while (size > 0) {
325 page = vmalloc_to_pfn((void *)pos);
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700326 if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED))
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700327 return -EAGAIN;
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700328
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700329 start += PAGE_SIZE;
330 pos += PAGE_SIZE;
331 if (size > PAGE_SIZE)
332 size -= PAGE_SIZE;
333 else
334 size = 0;
335 }
336
337 vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
338 return 0;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700339}
340
Bernie Thompson530f43a2010-02-15 06:46:21 -0800341/*
342 * Trims identical data from front and back of line
343 * Sets new front buffer address and width
344 * And returns byte count of identical pixels
345 * Assumes CPU natural alignment (unsigned long)
346 * for back and front buffer ptrs and width
347 */
348static int dlfb_trim_hline(const u8 *bback, const u8 **bfront, int *width_bytes)
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700349{
Bernie Thompson530f43a2010-02-15 06:46:21 -0800350 int j, k;
351 const unsigned long *back = (const unsigned long *) bback;
352 const unsigned long *front = (const unsigned long *) *bfront;
353 const int width = *width_bytes / sizeof(unsigned long);
354 int identical = width;
355 int start = width;
356 int end = width;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700357
Bernie Thompson530f43a2010-02-15 06:46:21 -0800358 prefetch((void *) front);
359 prefetch((void *) back);
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700360
Bernie Thompson530f43a2010-02-15 06:46:21 -0800361 for (j = 0; j < width; j++) {
362 if (back[j] != front[j]) {
363 start = j;
364 break;
365 }
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700366 }
367
Bernie Thompson530f43a2010-02-15 06:46:21 -0800368 for (k = width - 1; k > j; k--) {
369 if (back[k] != front[k]) {
370 end = k+1;
371 break;
372 }
373 }
374
375 identical = start + (width - end);
376 *bfront = (u8 *) &front[start];
377 *width_bytes = (end - start) * sizeof(unsigned long);
378
379 return identical * sizeof(unsigned long);
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700380}
381
382/*
Pavel Machek3b7b31f2010-04-03 07:00:37 +0200383 * Render a command stream for an encoded horizontal line segment of pixels.
384 *
385 * A command buffer holds several commands.
386 * It always begins with a fresh command header
387 * (the protocol doesn't require this, but we enforce it to allow
388 * multiple buffers to be potentially encoded and sent in parallel).
389 * A single command encodes one contiguous horizontal line of pixels
390 *
391 * The function relies on the client to do all allocation, so that
392 * rendering can be done directly to output buffers (e.g. USB URBs).
393 * The function fills the supplied command buffer, providing information
394 * on where it left off, so the client may call in again with additional
395 * buffers if the line will take several buffers to complete.
396 *
397 * A single command can transmit a maximum of 256 pixels,
398 * regardless of the compression ratio (protocol design limit).
399 * To the hardware, 0 for a size byte means 256
Bernie Thompson2685cff2010-09-05 18:29:56 -0700400 *
Pavel Machek3b7b31f2010-04-03 07:00:37 +0200401 * Rather than 256 pixel commands which are either rl or raw encoded,
402 * the rlx command simply assumes alternating raw and rl spans within one cmd.
403 * This has a slightly larger header overhead, but produces more even results.
404 * It also processes all data (read and write) in a single pass.
405 * Performance benchmarks of common cases show it having just slightly better
Bernie Thompson2685cff2010-09-05 18:29:56 -0700406 * compression than 256 pixel raw or rle commands, with similar CPU consumpion.
Pavel Machek3b7b31f2010-04-03 07:00:37 +0200407 * But for very rl friendly data, will compress not quite as well.
408 */
Bernie Thompson530f43a2010-02-15 06:46:21 -0800409static void dlfb_compress_hline(
410 const uint16_t **pixel_start_ptr,
411 const uint16_t *const pixel_end,
412 uint32_t *device_address_ptr,
413 uint8_t **command_buffer_ptr,
414 const uint8_t *const cmd_buffer_end)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700415{
Bernie Thompson530f43a2010-02-15 06:46:21 -0800416 const uint16_t *pixel = *pixel_start_ptr;
417 uint32_t dev_addr = *device_address_ptr;
418 uint8_t *cmd = *command_buffer_ptr;
419 const int bpp = 2;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700420
Bernie Thompson530f43a2010-02-15 06:46:21 -0800421 while ((pixel_end > pixel) &&
422 (cmd_buffer_end - MIN_RLX_CMD_BYTES > cmd)) {
423 uint8_t *raw_pixels_count_byte = 0;
424 uint8_t *cmd_pixels_count_byte = 0;
425 const uint16_t *raw_pixel_start = 0;
426 const uint16_t *cmd_pixel_start, *cmd_pixel_end = 0;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700427
Bernie Thompson530f43a2010-02-15 06:46:21 -0800428 prefetchw((void *) cmd); /* pull in one cache line at least */
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700429
Bernie Thompson530f43a2010-02-15 06:46:21 -0800430 *cmd++ = 0xAF;
431 *cmd++ = 0x6B;
Bernie Thompson1572f912010-09-05 16:35:27 -0700432 *cmd++ = (uint8_t) ((dev_addr >> 16) & 0xFF);
433 *cmd++ = (uint8_t) ((dev_addr >> 8) & 0xFF);
434 *cmd++ = (uint8_t) ((dev_addr) & 0xFF);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700435
Bernie Thompson530f43a2010-02-15 06:46:21 -0800436 cmd_pixels_count_byte = cmd++; /* we'll know this later */
437 cmd_pixel_start = pixel;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700438
Bernie Thompson530f43a2010-02-15 06:46:21 -0800439 raw_pixels_count_byte = cmd++; /* we'll know this later */
440 raw_pixel_start = pixel;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700441
Bernie Thompson530f43a2010-02-15 06:46:21 -0800442 cmd_pixel_end = pixel + min(MAX_CMD_PIXELS + 1,
443 min((int)(pixel_end - pixel),
444 (int)(cmd_buffer_end - cmd) / bpp));
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700445
Bernie Thompson530f43a2010-02-15 06:46:21 -0800446 prefetch_range((void *) pixel, (cmd_pixel_end - pixel) * bpp);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700447
Bernie Thompson530f43a2010-02-15 06:46:21 -0800448 while (pixel < cmd_pixel_end) {
449 const uint16_t * const repeating_pixel = pixel;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700450
Bernie Thompson530f43a2010-02-15 06:46:21 -0800451 *(uint16_t *)cmd = cpu_to_be16p(pixel);
452 cmd += 2;
453 pixel++;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700454
Bernie Thompson530f43a2010-02-15 06:46:21 -0800455 if (unlikely((pixel < cmd_pixel_end) &&
456 (*pixel == *repeating_pixel))) {
457 /* go back and fill in raw pixel count */
458 *raw_pixels_count_byte = ((repeating_pixel -
459 raw_pixel_start) + 1) & 0xFF;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700460
Bernie Thompson530f43a2010-02-15 06:46:21 -0800461 while ((pixel < cmd_pixel_end)
462 && (*pixel == *repeating_pixel)) {
463 pixel++;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700464 }
Bernie Thompson59277b62009-11-24 15:52:21 -0800465
Bernie Thompson530f43a2010-02-15 06:46:21 -0800466 /* immediately after raw data is repeat byte */
467 *cmd++ = ((pixel - repeating_pixel) - 1) & 0xFF;
Bernie Thompson59277b62009-11-24 15:52:21 -0800468
Bernie Thompson530f43a2010-02-15 06:46:21 -0800469 /* Then start another raw pixel span */
470 raw_pixel_start = pixel;
471 raw_pixels_count_byte = cmd++;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700472 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700473 }
474
Bernie Thompson530f43a2010-02-15 06:46:21 -0800475 if (pixel > raw_pixel_start) {
476 /* finalize last RAW span */
477 *raw_pixels_count_byte = (pixel-raw_pixel_start) & 0xFF;
478 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700479
Bernie Thompson530f43a2010-02-15 06:46:21 -0800480 *cmd_pixels_count_byte = (pixel - cmd_pixel_start) & 0xFF;
481 dev_addr += (pixel - cmd_pixel_start) * bpp;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700482 }
483
Bernie Thompson530f43a2010-02-15 06:46:21 -0800484 if (cmd_buffer_end <= MIN_RLX_CMD_BYTES + cmd) {
485 /* Fill leftover bytes with no-ops */
486 if (cmd_buffer_end > cmd)
487 memset(cmd, 0xAF, cmd_buffer_end - cmd);
488 cmd = (uint8_t *) cmd_buffer_end;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700489 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700490
Bernie Thompson530f43a2010-02-15 06:46:21 -0800491 *command_buffer_ptr = cmd;
492 *pixel_start_ptr = pixel;
493 *device_address_ptr = dev_addr;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700494
Bernie Thompson530f43a2010-02-15 06:46:21 -0800495 return;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700496}
497
Bernie Thompson530f43a2010-02-15 06:46:21 -0800498/*
499 * There are 3 copies of every pixel: The front buffer that the fbdev
500 * client renders to, the actual framebuffer across the USB bus in hardware
501 * (that we can only write to, slowly, and can never read), and (optionally)
502 * our shadow copy that tracks what's been sent to that hardware buffer.
503 */
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700504static int dlfb_render_hline(struct dlfb_data *dev, struct urb **urb_ptr,
Bernie Thompson530f43a2010-02-15 06:46:21 -0800505 const char *front, char **urb_buf_ptr,
506 u32 byte_offset, u32 byte_width,
507 int *ident_ptr, int *sent_ptr)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700508{
Bernie Thompson530f43a2010-02-15 06:46:21 -0800509 const u8 *line_start, *line_end, *next_pixel;
510 u32 dev_addr = dev->base16 + byte_offset;
511 struct urb *urb = *urb_ptr;
512 u8 *cmd = *urb_buf_ptr;
513 u8 *cmd_end = (u8 *) urb->transfer_buffer + urb->transfer_buffer_length;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700514
Bernie Thompson530f43a2010-02-15 06:46:21 -0800515 line_start = (u8 *) (front + byte_offset);
516 next_pixel = line_start;
517 line_end = next_pixel + byte_width;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700518
Bernie Thompson530f43a2010-02-15 06:46:21 -0800519 if (dev->backing_buffer) {
520 int offset;
521 const u8 *back_start = (u8 *) (dev->backing_buffer
522 + byte_offset);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700523
Bernie Thompson530f43a2010-02-15 06:46:21 -0800524 *ident_ptr += dlfb_trim_hline(back_start, &next_pixel,
525 &byte_width);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700526
Bernie Thompson530f43a2010-02-15 06:46:21 -0800527 offset = next_pixel - line_start;
528 line_end = next_pixel + byte_width;
529 dev_addr += offset;
530 back_start += offset;
531 line_start += offset;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700532
Bernie Thompson530f43a2010-02-15 06:46:21 -0800533 memcpy((char *)back_start, (char *) line_start,
534 byte_width);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700535 }
536
Bernie Thompson530f43a2010-02-15 06:46:21 -0800537 while (next_pixel < line_end) {
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700538
Bernie Thompson530f43a2010-02-15 06:46:21 -0800539 dlfb_compress_hline((const uint16_t **) &next_pixel,
540 (const uint16_t *) line_end, &dev_addr,
541 (u8 **) &cmd, (u8 *) cmd_end);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700542
Bernie Thompson530f43a2010-02-15 06:46:21 -0800543 if (cmd >= cmd_end) {
544 int len = cmd - (u8 *) urb->transfer_buffer;
545 if (dlfb_submit_urb(dev, urb, len))
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700546 return 1; /* lost pixels is set */
Bernie Thompson530f43a2010-02-15 06:46:21 -0800547 *sent_ptr += len;
548 urb = dlfb_get_urb(dev);
549 if (!urb)
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700550 return 1; /* lost_pixels is set */
Bernie Thompson530f43a2010-02-15 06:46:21 -0800551 *urb_ptr = urb;
552 cmd = urb->transfer_buffer;
553 cmd_end = &cmd[urb->transfer_buffer_length];
554 }
555 }
556
557 *urb_buf_ptr = cmd;
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700558
559 return 0;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700560}
561
Bernie Thompson530f43a2010-02-15 06:46:21 -0800562int dlfb_handle_damage(struct dlfb_data *dev, int x, int y,
563 int width, int height, char *data)
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700564{
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700565 int i, ret;
Bernie Thompson530f43a2010-02-15 06:46:21 -0800566 char *cmd;
567 cycles_t start_cycles, end_cycles;
568 int bytes_sent = 0;
569 int bytes_identical = 0;
570 struct urb *urb;
571 int aligned_x;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700572
Bernie Thompson530f43a2010-02-15 06:46:21 -0800573 start_cycles = get_cycles();
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700574
Bernie Thompson530f43a2010-02-15 06:46:21 -0800575 aligned_x = DL_ALIGN_DOWN(x, sizeof(unsigned long));
576 width = DL_ALIGN_UP(width + (x-aligned_x), sizeof(unsigned long));
577 x = aligned_x;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700578
Bernie Thompson530f43a2010-02-15 06:46:21 -0800579 if ((width <= 0) ||
580 (x + width > dev->info->var.xres) ||
581 (y + height > dev->info->var.yres))
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700582 return -EINVAL;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700583
Bernie Thompson530f43a2010-02-15 06:46:21 -0800584 if (!atomic_read(&dev->usb_active))
585 return 0;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700586
Bernie Thompson530f43a2010-02-15 06:46:21 -0800587 urb = dlfb_get_urb(dev);
588 if (!urb)
589 return 0;
590 cmd = urb->transfer_buffer;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700591
Bernie Thompson530f43a2010-02-15 06:46:21 -0800592 for (i = y; i < y + height ; i++) {
593 const int line_offset = dev->info->fix.line_length * i;
594 const int byte_offset = line_offset + (x * BPP);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700595
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700596 if (dlfb_render_hline(dev, &urb,
597 (char *) dev->info->fix.smem_start,
Bernie Thompson2685cff2010-09-05 18:29:56 -0700598 &cmd, byte_offset, width * BPP,
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700599 &bytes_identical, &bytes_sent))
600 goto error;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700601 }
602
Bernie Thompson530f43a2010-02-15 06:46:21 -0800603 if (cmd > (char *) urb->transfer_buffer) {
604 /* Send partial buffer remaining before exiting */
605 int len = cmd - (char *) urb->transfer_buffer;
606 ret = dlfb_submit_urb(dev, urb, len);
607 bytes_sent += len;
608 } else
609 dlfb_urb_completion(urb);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700610
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700611error:
Bernie Thompson530f43a2010-02-15 06:46:21 -0800612 atomic_add(bytes_sent, &dev->bytes_sent);
613 atomic_add(bytes_identical, &dev->bytes_identical);
614 atomic_add(width*height*2, &dev->bytes_rendered);
615 end_cycles = get_cycles();
616 atomic_add(((unsigned int) ((end_cycles - start_cycles)
617 >> 10)), /* Kcycles */
618 &dev->cpu_kcycles_used);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700619
Bernie Thompson530f43a2010-02-15 06:46:21 -0800620 return 0;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700621}
622
Bernie Thompsond46ecb92010-09-05 16:35:04 -0700623/*
624 * Path triggered by usermode clients who write to filesystem
625 * e.g. cat filename > /dev/fb1
626 * Not used by X Windows or text-mode console. But useful for testing.
627 * Slow because of extra copy and we must assume all pixels dirty.
628 */
629static ssize_t dlfb_ops_write(struct fb_info *info, const char __user *buf,
630 size_t count, loff_t *ppos)
631{
Paul Mundt1a3e5282011-01-06 17:29:24 +0900632 ssize_t result;
Bernie Thompsond46ecb92010-09-05 16:35:04 -0700633 struct dlfb_data *dev = info->par;
634 u32 offset = (u32) *ppos;
635
Bernie Thompsond46ecb92010-09-05 16:35:04 -0700636 result = fb_sys_write(info, buf, count, ppos);
637
638 if (result > 0) {
639 int start = max((int)(offset / info->fix.line_length) - 1, 0);
640 int lines = min((u32)((result / info->fix.line_length) + 1),
641 (u32)info->var.yres);
642
643 dlfb_handle_damage(dev, 0, start, info->var.xres,
644 lines, info->screen_base);
645 }
Bernie Thompsond46ecb92010-09-05 16:35:04 -0700646
647 return result;
648}
649
Bernie Thompson530f43a2010-02-15 06:46:21 -0800650/* hardware has native COPY command (see libdlo), but not worth it for fbcon */
Bernie Thompson45742032010-02-15 06:46:04 -0800651static void dlfb_ops_copyarea(struct fb_info *info,
Bernie Thompson530f43a2010-02-15 06:46:21 -0800652 const struct fb_copyarea *area)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700653{
Bernie Thompson530f43a2010-02-15 06:46:21 -0800654
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700655 struct dlfb_data *dev = info->par;
656
Bernie Thompson530f43a2010-02-15 06:46:21 -0800657 sys_copyarea(info, area);
658
659 dlfb_handle_damage(dev, area->dx, area->dy,
660 area->width, area->height, info->screen_base);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700661}
662
Bernie Thompson45742032010-02-15 06:46:04 -0800663static void dlfb_ops_imageblit(struct fb_info *info,
Bernie Thompson530f43a2010-02-15 06:46:21 -0800664 const struct fb_image *image)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700665{
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700666 struct dlfb_data *dev = info->par;
Bernie Thompson530f43a2010-02-15 06:46:21 -0800667
Bernie Thompson530f43a2010-02-15 06:46:21 -0800668 sys_imageblit(info, image);
669
670 dlfb_handle_damage(dev, image->dx, image->dy,
671 image->width, image->height, info->screen_base);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700672}
673
Bernie Thompson45742032010-02-15 06:46:04 -0800674static void dlfb_ops_fillrect(struct fb_info *info,
Bernie Thompson530f43a2010-02-15 06:46:21 -0800675 const struct fb_fillrect *rect)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700676{
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700677 struct dlfb_data *dev = info->par;
678
Bernie Thompson530f43a2010-02-15 06:46:21 -0800679 sys_fillrect(info, rect);
680
681 dlfb_handle_damage(dev, rect->dx, rect->dy, rect->width,
682 rect->height, info->screen_base);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700683}
684
Bernie Thompsond5ed5432010-09-05 16:35:39 -0700685/*
686 * NOTE: fb_defio.c is holding info->fbdefio.mutex
687 * Touching ANY framebuffer memory that triggers a page fault
688 * in fb_defio will cause a deadlock, when it also tries to
689 * grab the same mutex.
690 */
691static void dlfb_dpy_deferred_io(struct fb_info *info,
692 struct list_head *pagelist)
693{
694 struct page *cur;
695 struct fb_deferred_io *fbdefio = info->fbdefio;
696 struct dlfb_data *dev = info->par;
697 struct urb *urb;
698 char *cmd;
699 cycles_t start_cycles, end_cycles;
700 int bytes_sent = 0;
701 int bytes_identical = 0;
702 int bytes_rendered = 0;
703
704 if (!fb_defio)
705 return;
706
707 if (!atomic_read(&dev->usb_active))
708 return;
709
710 start_cycles = get_cycles();
711
712 urb = dlfb_get_urb(dev);
713 if (!urb)
714 return;
715
716 cmd = urb->transfer_buffer;
717
718 /* walk the written page list and render each to device */
719 list_for_each_entry(cur, &fbdefio->pagelist, lru) {
720
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700721 if (dlfb_render_hline(dev, &urb, (char *) info->fix.smem_start,
Bernie Thompsond5ed5432010-09-05 16:35:39 -0700722 &cmd, cur->index << PAGE_SHIFT,
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700723 PAGE_SIZE, &bytes_identical, &bytes_sent))
724 goto error;
Bernie Thompsond5ed5432010-09-05 16:35:39 -0700725 bytes_rendered += PAGE_SIZE;
726 }
727
728 if (cmd > (char *) urb->transfer_buffer) {
729 /* Send partial buffer remaining before exiting */
730 int len = cmd - (char *) urb->transfer_buffer;
731 dlfb_submit_urb(dev, urb, len);
732 bytes_sent += len;
733 } else
734 dlfb_urb_completion(urb);
735
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700736error:
Bernie Thompsond5ed5432010-09-05 16:35:39 -0700737 atomic_add(bytes_sent, &dev->bytes_sent);
738 atomic_add(bytes_identical, &dev->bytes_identical);
739 atomic_add(bytes_rendered, &dev->bytes_rendered);
740 end_cycles = get_cycles();
741 atomic_add(((unsigned int) ((end_cycles - start_cycles)
742 >> 10)), /* Kcycles */
743 &dev->cpu_kcycles_used);
744}
745
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700746static int dlfb_get_edid(struct dlfb_data *dev, char *edid, int len)
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800747{
748 int i;
749 int ret;
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700750 char *rbuf;
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800751
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700752 rbuf = kmalloc(2, GFP_KERNEL);
753 if (!rbuf)
754 return 0;
755
756 for (i = 0; i < len; i++) {
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800757 ret = usb_control_msg(dev->udev,
758 usb_rcvctrlpipe(dev->udev, 0), (0x02),
759 (0x80 | (0x02 << 5)), i << 8, 0xA1, rbuf, 2,
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700760 HZ);
761 if (ret < 1) {
Paul Mundt81f6f3c2011-01-06 18:07:54 +0900762 pr_err("Read EDID byte %d failed err %x\n", i, ret);
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700763 i--;
764 break;
765 }
766 edid[i] = rbuf[1];
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800767 }
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700768
769 kfree(rbuf);
770
771 return i;
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800772}
773
Bernie Thompson45742032010-02-15 06:46:04 -0800774static int dlfb_ops_ioctl(struct fb_info *info, unsigned int cmd,
Bernie Thompson530f43a2010-02-15 06:46:21 -0800775 unsigned long arg)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700776{
Bernie Thompson530f43a2010-02-15 06:46:21 -0800777
778 struct dlfb_data *dev = info->par;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700779
Bernie Thompson530f43a2010-02-15 06:46:21 -0800780 if (!atomic_read(&dev->usb_active))
781 return 0;
782
783 /* TODO: Update X server to get this from sysfs instead */
784 if (cmd == DLFB_IOCTL_RETURN_EDID) {
Dr. David Alan Gilbertdef76602011-08-21 13:34:15 -0700785 void __user *edid = (void __user *)arg;
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700786 if (copy_to_user(edid, dev->edid, dev->edid_size))
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700787 return -EFAULT;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700788 return 0;
789 }
790
Bernie Thompson530f43a2010-02-15 06:46:21 -0800791 /* TODO: Help propose a standard fb.h ioctl to report mmap damage */
792 if (cmd == DLFB_IOCTL_REPORT_DAMAGE) {
Dr. David Alan Gilbertdef76602011-08-21 13:34:15 -0700793 struct dloarea area;
794
795 if (copy_from_user(&area, (void __user *)arg,
796 sizeof(struct dloarea)))
797 return -EFAULT;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700798
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700799 /*
800 * If we have a damage-aware client, turn fb_defio "off"
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300801 * To avoid perf imact of unnecessary page fault handling.
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700802 * Done by resetting the delay for this fb_info to a very
803 * long period. Pages will become writable and stay that way.
804 * Reset to normal value when all clients have closed this fb.
805 */
806 if (info->fbdefio)
807 info->fbdefio->delay = DL_DEFIO_WRITE_DISABLE;
808
Dr. David Alan Gilbertdef76602011-08-21 13:34:15 -0700809 if (area.x < 0)
810 area.x = 0;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700811
Dr. David Alan Gilbertdef76602011-08-21 13:34:15 -0700812 if (area.x > info->var.xres)
813 area.x = info->var.xres;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700814
Dr. David Alan Gilbertdef76602011-08-21 13:34:15 -0700815 if (area.y < 0)
816 area.y = 0;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700817
Dr. David Alan Gilbertdef76602011-08-21 13:34:15 -0700818 if (area.y > info->var.yres)
819 area.y = info->var.yres;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700820
Dr. David Alan Gilbertdef76602011-08-21 13:34:15 -0700821 dlfb_handle_damage(dev, area.x, area.y, area.w, area.h,
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700822 info->screen_base);
823 }
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700824
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700825 return 0;
826}
827
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700828/* taken from vesafb */
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700829static int
Bernie Thompson45742032010-02-15 06:46:04 -0800830dlfb_ops_setcolreg(unsigned regno, unsigned red, unsigned green,
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700831 unsigned blue, unsigned transp, struct fb_info *info)
832{
833 int err = 0;
834
835 if (regno >= info->cmap.len)
836 return 1;
837
838 if (regno < 16) {
839 if (info->var.red.offset == 10) {
840 /* 1:5:5:5 */
841 ((u32 *) (info->pseudo_palette))[regno] =
842 ((red & 0xf800) >> 1) |
843 ((green & 0xf800) >> 6) | ((blue & 0xf800) >> 11);
844 } else {
845 /* 0:5:6:5 */
846 ((u32 *) (info->pseudo_palette))[regno] =
847 ((red & 0xf800)) |
848 ((green & 0xfc00) >> 5) | ((blue & 0xf800) >> 11);
849 }
850 }
851
852 return err;
853}
854
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800855/*
856 * It's common for several clients to have framebuffer open simultaneously.
857 * e.g. both fbcon and X. Makes things interesting.
Bernie Thompson33077b82010-09-05 16:35:19 -0700858 * Assumes caller is holding info->lock (for open and release at least)
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800859 */
860static int dlfb_ops_open(struct fb_info *info, int user)
861{
862 struct dlfb_data *dev = info->par;
863
Bernie Thompsond5ed5432010-09-05 16:35:39 -0700864 /*
865 * fbcon aggressively connects to first framebuffer it finds,
866 * preventing other clients (X) from working properly. Usually
867 * not what the user wants. Fail by default with option to enable.
868 */
Dr. David Alan Gilbertdef76602011-08-21 13:34:15 -0700869 if ((user == 0) && (!console))
Bernie Thompsond5ed5432010-09-05 16:35:39 -0700870 return -EBUSY;
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800871
Bernie Thompson33077b82010-09-05 16:35:19 -0700872 /* If the USB device is gone, we don't accept new opens */
873 if (dev->virtualized)
874 return -ENODEV;
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800875
876 dev->fb_count++;
877
Bernie Thompson33077b82010-09-05 16:35:19 -0700878 kref_get(&dev->kref);
879
Bernie Thompsond5ed5432010-09-05 16:35:39 -0700880 if (fb_defio && (info->fbdefio == NULL)) {
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700881 /* enable defio at last moment if not disabled by client */
882
883 struct fb_deferred_io *fbdefio;
884
Joe Perches31a9f472010-10-31 15:33:55 -0700885 fbdefio = kmalloc(sizeof(struct fb_deferred_io), GFP_KERNEL);
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700886
887 if (fbdefio) {
888 fbdefio->delay = DL_DEFIO_WRITE_DELAY;
889 fbdefio->deferred_io = dlfb_dpy_deferred_io;
890 }
891
892 info->fbdefio = fbdefio;
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800893 fb_deferred_io_init(info);
894 }
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800895
Paul Mundt81f6f3c2011-01-06 18:07:54 +0900896 pr_notice("open /dev/fb%d user=%d fb_info=%p count=%d\n",
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800897 info->node, user, info, dev->fb_count);
898
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700899 return 0;
900}
901
Bernie Thompson4a4854d2010-02-15 06:45:55 -0800902/*
903 * Called when all client interfaces to start transactions have been disabled,
904 * and all references to our device instance (dlfb_data) are released.
905 * Every transaction must have a reference, so we know are fully spun down
906 */
Bernie Thompson33077b82010-09-05 16:35:19 -0700907static void dlfb_free(struct kref *kref)
Bernie Thompson4a4854d2010-02-15 06:45:55 -0800908{
909 struct dlfb_data *dev = container_of(kref, struct dlfb_data, kref);
910
Bernie Thompson33077b82010-09-05 16:35:19 -0700911 /* this function will wait for all in-flight urbs to complete */
912 if (dev->urbs.count > 0)
913 dlfb_free_urb_list(dev);
914
Bernie Thompson4a4854d2010-02-15 06:45:55 -0800915 if (dev->backing_buffer)
916 vfree(dev->backing_buffer);
917
Bernie Thompson33077b82010-09-05 16:35:19 -0700918 kfree(dev->edid);
919
Paul Mundt81f6f3c2011-01-06 18:07:54 +0900920 pr_warn("freeing dlfb_data %p\n", dev);
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800921
Bernie Thompson4a4854d2010-02-15 06:45:55 -0800922 kfree(dev);
923}
924
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700925static void dlfb_release_urb_work(struct work_struct *work)
926{
927 struct urb_node *unode = container_of(work, struct urb_node,
928 release_urb_work.work);
929
930 up(&unode->dev->urbs.limit_sem);
931}
Bernie Thompson33077b82010-09-05 16:35:19 -0700932
933static void dlfb_free_framebuffer_work(struct work_struct *work)
Bernie Thompson2469d5d2010-02-15 06:46:13 -0800934{
Bernie Thompson33077b82010-09-05 16:35:19 -0700935 struct dlfb_data *dev = container_of(work, struct dlfb_data,
936 free_framebuffer_work.work);
937 struct fb_info *info = dev->info;
938 int node = info->node;
939
940 unregister_framebuffer(info);
Bernie Thompson2469d5d2010-02-15 06:46:13 -0800941
942 if (info->cmap.len != 0)
943 fb_dealloc_cmap(&info->cmap);
944 if (info->monspecs.modedb)
945 fb_destroy_modedb(info->monspecs.modedb);
946 if (info->screen_base)
947 vfree(info->screen_base);
948
949 fb_destroy_modelist(&info->modelist);
950
Bernie Thompson33077b82010-09-05 16:35:19 -0700951 dev->info = 0;
952
953 /* Assume info structure is freed after this point */
Bernie Thompson2469d5d2010-02-15 06:46:13 -0800954 framebuffer_release(info);
955
Paul Mundt81f6f3c2011-01-06 18:07:54 +0900956 pr_warn("fb_info for /dev/fb%d has been freed\n", node);
Bernie Thompson33077b82010-09-05 16:35:19 -0700957
958 /* ref taken in probe() as part of registering framebfufer */
959 kref_put(&dev->kref, dlfb_free);
960}
961
962/*
963 * Assumes caller is holding info->lock mutex (for open and release at least)
964 */
965static int dlfb_ops_release(struct fb_info *info, int user)
966{
967 struct dlfb_data *dev = info->par;
968
969 dev->fb_count--;
970
971 /* We can't free fb_info here - fbmem will touch it when we return */
972 if (dev->virtualized && (dev->fb_count == 0))
973 schedule_delayed_work(&dev->free_framebuffer_work, HZ);
974
Bernie Thompson33077b82010-09-05 16:35:19 -0700975 if ((dev->fb_count == 0) && (info->fbdefio)) {
976 fb_deferred_io_cleanup(info);
977 kfree(info->fbdefio);
978 info->fbdefio = NULL;
979 info->fbops->fb_mmap = dlfb_ops_mmap;
980 }
Bernie Thompson33077b82010-09-05 16:35:19 -0700981
Paul Mundt81f6f3c2011-01-06 18:07:54 +0900982 pr_warn("released /dev/fb%d user=%d count=%d\n",
Bernie Thompson33077b82010-09-05 16:35:19 -0700983 info->node, user, dev->fb_count);
984
985 kref_put(&dev->kref, dlfb_free);
986
987 return 0;
Bernie Thompson2469d5d2010-02-15 06:46:13 -0800988}
989
990/*
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800991 * Check whether a video mode is supported by the DisplayLink chip
992 * We start from monitor's modes, so don't need to filter that here
993 */
994static int dlfb_is_valid_mode(struct fb_videomode *mode,
995 struct fb_info *info)
996{
997 struct dlfb_data *dev = info->par;
998
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700999 if (mode->xres * mode->yres > dev->sku_pixel_limit) {
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001000 pr_warn("%dx%d beyond chip capabilities\n",
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001001 mode->xres, mode->yres);
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001002 return 0;
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001003 }
1004
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001005 pr_info("%dx%d valid mode\n", mode->xres, mode->yres);
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001006
1007 return 1;
1008}
1009
1010static void dlfb_var_color_format(struct fb_var_screeninfo *var)
1011{
1012 const struct fb_bitfield red = { 11, 5, 0 };
1013 const struct fb_bitfield green = { 5, 6, 0 };
1014 const struct fb_bitfield blue = { 0, 5, 0 };
1015
1016 var->bits_per_pixel = 16;
1017 var->red = red;
1018 var->green = green;
1019 var->blue = blue;
1020}
1021
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001022static int dlfb_ops_check_var(struct fb_var_screeninfo *var,
1023 struct fb_info *info)
1024{
1025 struct fb_videomode mode;
1026
1027 /* TODO: support dynamically changing framebuffer size */
1028 if ((var->xres * var->yres * 2) > info->fix.smem_len)
1029 return -EINVAL;
1030
1031 /* set device-specific elements of var unrelated to mode */
1032 dlfb_var_color_format(var);
1033
1034 fb_var_to_videomode(&mode, var);
1035
1036 if (!dlfb_is_valid_mode(&mode, info))
1037 return -EINVAL;
1038
1039 return 0;
1040}
1041
1042static int dlfb_ops_set_par(struct fb_info *info)
1043{
1044 struct dlfb_data *dev = info->par;
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001045 int result;
1046 u16 *pix_framebuffer;
1047 int i;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001048
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001049 pr_notice("set_par mode %dx%d\n", info->var.xres, info->var.yres);
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001050
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001051 result = dlfb_set_video_mode(dev, &info->var);
1052
1053 if ((result == 0) && (dev->fb_count == 0)) {
1054
1055 /* paint greenscreen */
1056
1057 pix_framebuffer = (u16 *) info->screen_base;
1058 for (i = 0; i < info->fix.smem_len / 2; i++)
1059 pix_framebuffer[i] = 0x37e6;
1060
1061 dlfb_handle_damage(dev, 0, 0, info->var.xres, info->var.yres,
1062 info->screen_base);
1063 }
1064
1065 return result;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001066}
1067
Bernie Thompson58e7c3b2011-08-21 13:34:11 -07001068/* To fonzi the jukebox (e.g. make blanking changes take effect) */
1069static char *dlfb_dummy_render(char *buf)
1070{
1071 *buf++ = 0xAF;
1072 *buf++ = 0x6A; /* copy */
1073 *buf++ = 0x00; /* from address*/
1074 *buf++ = 0x00;
1075 *buf++ = 0x00;
1076 *buf++ = 0x01; /* one pixel */
1077 *buf++ = 0x00; /* to address */
1078 *buf++ = 0x00;
1079 *buf++ = 0x00;
1080 return buf;
1081}
1082
Bernie Thompson9825f702010-09-05 16:35:10 -07001083/*
1084 * In order to come back from full DPMS off, we need to set the mode again
1085 */
Bernie Thompson45742032010-02-15 06:46:04 -08001086static int dlfb_ops_blank(int blank_mode, struct fb_info *info)
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -07001087{
Bernie Thompson530f43a2010-02-15 06:46:21 -08001088 struct dlfb_data *dev = info->par;
Bernie Thompson58e7c3b2011-08-21 13:34:11 -07001089 char *bufptr;
1090 struct urb *urb;
Roberto De Ioris7316bc52009-06-10 23:02:19 -07001091
Bernie Thompson58e7c3b2011-08-21 13:34:11 -07001092 pr_info("/dev/fb%d FB_BLANK mode %d --> %d\n",
1093 info->node, dev->blank_mode, blank_mode);
Roberto De Ioris7316bc52009-06-10 23:02:19 -07001094
Bernie Thompson58e7c3b2011-08-21 13:34:11 -07001095 if ((dev->blank_mode == FB_BLANK_POWERDOWN) &&
1096 (blank_mode != FB_BLANK_POWERDOWN)) {
Bernie Thompson9825f702010-09-05 16:35:10 -07001097
Bernie Thompson58e7c3b2011-08-21 13:34:11 -07001098 /* returning from powerdown requires a fresh modeset */
Bernie Thompson9825f702010-09-05 16:35:10 -07001099 dlfb_set_video_mode(dev, &info->var);
1100 }
Roberto De Ioris7316bc52009-06-10 23:02:19 -07001101
Bernie Thompson58e7c3b2011-08-21 13:34:11 -07001102 urb = dlfb_get_urb(dev);
1103 if (!urb)
1104 return 0;
1105
1106 bufptr = (char *) urb->transfer_buffer;
1107 bufptr = dlfb_vidreg_lock(bufptr);
1108 bufptr = dlfb_blanking(bufptr, blank_mode);
1109 bufptr = dlfb_vidreg_unlock(bufptr);
1110
1111 /* seems like a render op is needed to have blank change take effect */
1112 bufptr = dlfb_dummy_render(bufptr);
1113
1114 dlfb_submit_urb(dev, urb, bufptr -
1115 (char *) urb->transfer_buffer);
1116
1117 dev->blank_mode = blank_mode;
1118
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001119 return 0;
1120}
1121
1122static struct fb_ops dlfb_ops = {
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001123 .owner = THIS_MODULE,
Paul Mundt1a3e5282011-01-06 17:29:24 +09001124 .fb_read = fb_sys_read,
Bernie Thompsond46ecb92010-09-05 16:35:04 -07001125 .fb_write = dlfb_ops_write,
Bernie Thompson45742032010-02-15 06:46:04 -08001126 .fb_setcolreg = dlfb_ops_setcolreg,
1127 .fb_fillrect = dlfb_ops_fillrect,
1128 .fb_copyarea = dlfb_ops_copyarea,
1129 .fb_imageblit = dlfb_ops_imageblit,
1130 .fb_mmap = dlfb_ops_mmap,
1131 .fb_ioctl = dlfb_ops_ioctl,
Bernie Thompson3e8f3d62010-02-15 06:46:26 -08001132 .fb_open = dlfb_ops_open,
Bernie Thompson45742032010-02-15 06:46:04 -08001133 .fb_release = dlfb_ops_release,
1134 .fb_blank = dlfb_ops_blank,
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001135 .fb_check_var = dlfb_ops_check_var,
1136 .fb_set_par = dlfb_ops_set_par,
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001137};
1138
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001139
Bernie Thompsoncc403dc2010-02-15 06:45:49 -08001140/*
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001141 * Assumes &info->lock held by caller
1142 * Assumes no active clients have framebuffer open
1143 */
1144static int dlfb_realloc_framebuffer(struct dlfb_data *dev, struct fb_info *info)
1145{
1146 int retval = -ENOMEM;
1147 int old_len = info->fix.smem_len;
1148 int new_len;
1149 unsigned char *old_fb = info->screen_base;
1150 unsigned char *new_fb;
1151 unsigned char *new_back;
1152
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001153 pr_warn("Reallocating framebuffer. Addresses will change!\n");
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001154
1155 new_len = info->fix.line_length * info->var.yres;
1156
1157 if (PAGE_ALIGN(new_len) > old_len) {
1158 /*
1159 * Alloc system memory for virtual framebuffer
1160 */
1161 new_fb = vmalloc(new_len);
1162 if (!new_fb) {
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001163 pr_err("Virtual framebuffer alloc failed\n");
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001164 goto error;
1165 }
1166
1167 if (info->screen_base) {
1168 memcpy(new_fb, old_fb, old_len);
1169 vfree(info->screen_base);
1170 }
1171
1172 info->screen_base = new_fb;
1173 info->fix.smem_len = PAGE_ALIGN(new_len);
1174 info->fix.smem_start = (unsigned long) new_fb;
1175 info->flags = udlfb_info_flags;
1176
1177 /*
1178 * Second framebuffer copy to mirror the framebuffer state
1179 * on the physical USB device. We can function without this.
1180 * But with imperfect damage info we may send pixels over USB
1181 * that were, in fact, unchanged - wasting limited USB bandwidth
1182 */
Joe Perches5b84cc72010-11-04 20:07:59 -07001183 new_back = vzalloc(new_len);
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001184 if (!new_back)
Linus Torvalds949f6712011-01-10 16:04:53 -08001185 pr_info("No shadow/backing buffer allocated\n");
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001186 else {
1187 if (dev->backing_buffer)
1188 vfree(dev->backing_buffer);
1189 dev->backing_buffer = new_back;
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001190 }
1191 }
1192
1193 retval = 0;
1194
1195error:
1196 return retval;
1197}
1198
1199/*
1200 * 1) Get EDID from hw, or use sw default
1201 * 2) Parse into various fb_info structs
1202 * 3) Allocate virtual framebuffer memory to back highest res mode
1203 *
1204 * Parses EDID into three places used by various parts of fbdev:
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001205 * fb_var_screeninfo contains the timing of the monitor's preferred mode
1206 * fb_info.monspecs is full parsed EDID info, including monspecs.modedb
1207 * fb_info.modelist is a linked list of all monitor & VESA modes which work
1208 *
1209 * If EDID is not readable/valid, then modelist is all VESA modes,
1210 * monspecs is NULL, and fb_var_screeninfo is set to safe VESA mode
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001211 * Returns 0 if successful
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001212 */
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001213static int dlfb_setup_modes(struct dlfb_data *dev,
1214 struct fb_info *info,
1215 char *default_edid, size_t default_edid_size)
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001216{
1217 int i;
1218 const struct fb_videomode *default_vmode = NULL;
1219 int result = 0;
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001220 char *edid;
1221 int tries = 3;
1222
1223 if (info->dev) /* only use mutex if info has been registered */
1224 mutex_lock(&info->lock);
1225
Paul Mundtb9f03a32011-01-06 18:04:02 +09001226 edid = kmalloc(EDID_LENGTH, GFP_KERNEL);
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001227 if (!edid) {
1228 result = -ENOMEM;
1229 goto error;
1230 }
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001231
1232 fb_destroy_modelist(&info->modelist);
1233 memset(&info->monspecs, 0, sizeof(info->monspecs));
1234
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001235 /*
1236 * Try to (re)read EDID from hardware first
1237 * EDID data may return, but not parse as valid
1238 * Try again a few times, in case of e.g. analog cable noise
1239 */
1240 while (tries--) {
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001241
Paul Mundtb9f03a32011-01-06 18:04:02 +09001242 i = dlfb_get_edid(dev, edid, EDID_LENGTH);
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001243
Paul Mundtb9f03a32011-01-06 18:04:02 +09001244 if (i >= EDID_LENGTH)
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001245 fb_edid_to_monspecs(edid, &info->monspecs);
1246
1247 if (info->monspecs.modedb_len > 0) {
1248 dev->edid = edid;
1249 dev->edid_size = i;
1250 break;
1251 }
1252 }
1253
1254 /* If that fails, use a previously returned EDID if available */
1255 if (info->monspecs.modedb_len == 0) {
1256
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001257 pr_err("Unable to get valid EDID from device/display\n");
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001258
1259 if (dev->edid) {
1260 fb_edid_to_monspecs(dev->edid, &info->monspecs);
1261 if (info->monspecs.modedb_len > 0)
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001262 pr_err("Using previously queried EDID\n");
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001263 }
1264 }
1265
1266 /* If that fails, use the default EDID we were handed */
1267 if (info->monspecs.modedb_len == 0) {
Paul Mundtb9f03a32011-01-06 18:04:02 +09001268 if (default_edid_size >= EDID_LENGTH) {
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001269 fb_edid_to_monspecs(default_edid, &info->monspecs);
1270 if (info->monspecs.modedb_len > 0) {
1271 memcpy(edid, default_edid, default_edid_size);
1272 dev->edid = edid;
1273 dev->edid_size = default_edid_size;
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001274 pr_err("Using default/backup EDID\n");
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001275 }
1276 }
1277 }
1278
1279 /* If we've got modes, let's pick a best default mode */
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001280 if (info->monspecs.modedb_len > 0) {
1281
1282 for (i = 0; i < info->monspecs.modedb_len; i++) {
1283 if (dlfb_is_valid_mode(&info->monspecs.modedb[i], info))
1284 fb_add_videomode(&info->monspecs.modedb[i],
1285 &info->modelist);
William Katsak9377c512011-06-23 13:16:29 +00001286 else {
1287 if (i == 0)
1288 /* if we've removed top/best mode */
1289 info->monspecs.misc
1290 &= ~FB_MISC_1ST_DETAIL;
1291 }
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001292 }
1293
1294 default_vmode = fb_find_best_display(&info->monspecs,
1295 &info->modelist);
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001296 }
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001297
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001298 /* If everything else has failed, fall back to safe default mode */
1299 if (default_vmode == NULL) {
1300
1301 struct fb_videomode fb_vmode = {0};
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001302
1303 /*
1304 * Add the standard VESA modes to our modelist
1305 * Since we don't have EDID, there may be modes that
1306 * overspec monitor and/or are incorrect aspect ratio, etc.
1307 * But at least the user has a chance to choose
1308 */
1309 for (i = 0; i < VESA_MODEDB_SIZE; i++) {
1310 if (dlfb_is_valid_mode((struct fb_videomode *)
1311 &vesa_modes[i], info))
1312 fb_add_videomode(&vesa_modes[i],
1313 &info->modelist);
1314 }
1315
1316 /*
1317 * default to resolution safe for projectors
1318 * (since they are most common case without EDID)
1319 */
1320 fb_vmode.xres = 800;
1321 fb_vmode.yres = 600;
1322 fb_vmode.refresh = 60;
1323 default_vmode = fb_find_nearest_mode(&fb_vmode,
1324 &info->modelist);
1325 }
1326
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001327 /* If we have good mode and no active clients*/
1328 if ((default_vmode != NULL) && (dev->fb_count == 0)) {
1329
1330 fb_videomode_to_var(&info->var, default_vmode);
1331 dlfb_var_color_format(&info->var);
1332
1333 /*
1334 * with mode size info, we can now alloc our framebuffer.
1335 */
1336 memcpy(&info->fix, &dlfb_fix, sizeof(dlfb_fix));
1337 info->fix.line_length = info->var.xres *
1338 (info->var.bits_per_pixel / 8);
1339
1340 result = dlfb_realloc_framebuffer(dev, info);
1341
1342 } else
1343 result = -EINVAL;
1344
1345error:
1346 if (edid && (dev->edid != edid))
1347 kfree(edid);
1348
1349 if (info->dev)
1350 mutex_unlock(&info->lock);
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001351
1352 return result;
1353}
1354
1355static ssize_t metrics_bytes_rendered_show(struct device *fbdev,
1356 struct device_attribute *a, char *buf) {
1357 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1358 struct dlfb_data *dev = fb_info->par;
1359 return snprintf(buf, PAGE_SIZE, "%u\n",
1360 atomic_read(&dev->bytes_rendered));
1361}
1362
1363static ssize_t metrics_bytes_identical_show(struct device *fbdev,
1364 struct device_attribute *a, char *buf) {
1365 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1366 struct dlfb_data *dev = fb_info->par;
1367 return snprintf(buf, PAGE_SIZE, "%u\n",
1368 atomic_read(&dev->bytes_identical));
1369}
1370
1371static ssize_t metrics_bytes_sent_show(struct device *fbdev,
1372 struct device_attribute *a, char *buf) {
1373 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1374 struct dlfb_data *dev = fb_info->par;
1375 return snprintf(buf, PAGE_SIZE, "%u\n",
1376 atomic_read(&dev->bytes_sent));
1377}
1378
1379static ssize_t metrics_cpu_kcycles_used_show(struct device *fbdev,
1380 struct device_attribute *a, char *buf) {
1381 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1382 struct dlfb_data *dev = fb_info->par;
1383 return snprintf(buf, PAGE_SIZE, "%u\n",
1384 atomic_read(&dev->cpu_kcycles_used));
1385}
1386
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001387static ssize_t edid_show(
1388 struct file *filp,
1389 struct kobject *kobj, struct bin_attribute *a,
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001390 char *buf, loff_t off, size_t count) {
1391 struct device *fbdev = container_of(kobj, struct device, kobj);
1392 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1393 struct dlfb_data *dev = fb_info->par;
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001394
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001395 if (dev->edid == NULL)
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001396 return 0;
1397
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001398 if ((off >= dev->edid_size) || (count > dev->edid_size))
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001399 return 0;
1400
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001401 if (off + count > dev->edid_size)
1402 count = dev->edid_size - off;
1403
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001404 pr_info("sysfs edid copy %p to %p, %d bytes\n",
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001405 dev->edid, buf, (int) count);
1406
1407 memcpy(buf, dev->edid, count);
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001408
1409 return count;
1410}
1411
Bernie Thompson8ef8cc42010-09-05 16:35:31 -07001412static ssize_t edid_store(
1413 struct file *filp,
1414 struct kobject *kobj, struct bin_attribute *a,
1415 char *src, loff_t src_off, size_t src_size) {
1416 struct device *fbdev = container_of(kobj, struct device, kobj);
1417 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1418 struct dlfb_data *dev = fb_info->par;
1419
1420 /* We only support write of entire EDID at once, no offset*/
Paul Mundtb9f03a32011-01-06 18:04:02 +09001421 if ((src_size != EDID_LENGTH) || (src_off != 0))
Bernie Thompson8ef8cc42010-09-05 16:35:31 -07001422 return 0;
1423
1424 dlfb_setup_modes(dev, fb_info, src, src_size);
1425
1426 if (dev->edid && (memcmp(src, dev->edid, src_size) == 0)) {
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001427 pr_info("sysfs written EDID is new default\n");
Bernie Thompson8ef8cc42010-09-05 16:35:31 -07001428 dlfb_ops_set_par(fb_info);
1429 return src_size;
1430 } else
1431 return 0;
1432}
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001433
1434static ssize_t metrics_reset_store(struct device *fbdev,
1435 struct device_attribute *attr,
1436 const char *buf, size_t count)
1437{
1438 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1439 struct dlfb_data *dev = fb_info->par;
1440
1441 atomic_set(&dev->bytes_rendered, 0);
1442 atomic_set(&dev->bytes_identical, 0);
1443 atomic_set(&dev->bytes_sent, 0);
1444 atomic_set(&dev->cpu_kcycles_used, 0);
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001445
1446 return count;
1447}
1448
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001449static struct bin_attribute edid_attr = {
1450 .attr.name = "edid",
Bernie Thompson8ef8cc42010-09-05 16:35:31 -07001451 .attr.mode = 0666,
Paul Mundtb9f03a32011-01-06 18:04:02 +09001452 .size = EDID_LENGTH,
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001453 .read = edid_show,
Bernie Thompson8ef8cc42010-09-05 16:35:31 -07001454 .write = edid_store
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001455};
1456
1457static struct device_attribute fb_device_attrs[] = {
1458 __ATTR_RO(metrics_bytes_rendered),
1459 __ATTR_RO(metrics_bytes_identical),
1460 __ATTR_RO(metrics_bytes_sent),
1461 __ATTR_RO(metrics_cpu_kcycles_used),
Greg Kroah-Hartman926c1112010-11-18 11:21:04 -08001462 __ATTR(metrics_reset, S_IWUSR, NULL, metrics_reset_store),
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001463};
1464
1465/*
Bernie Thompsoncc403dc2010-02-15 06:45:49 -08001466 * This is necessary before we can communicate with the display controller.
1467 */
1468static int dlfb_select_std_channel(struct dlfb_data *dev)
1469{
1470 int ret;
1471 u8 set_def_chn[] = { 0x57, 0xCD, 0xDC, 0xA7,
1472 0x1C, 0x88, 0x5E, 0x15,
1473 0x60, 0xFE, 0xC6, 0x97,
1474 0x16, 0x3D, 0x47, 0xF2 };
1475
1476 ret = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
1477 NR_USB_REQUEST_CHANNEL,
1478 (USB_DIR_OUT | USB_TYPE_VENDOR), 0, 0,
1479 set_def_chn, sizeof(set_def_chn), USB_CTRL_SET_TIMEOUT);
1480 return ret;
1481}
1482
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001483static int dlfb_parse_vendor_descriptor(struct dlfb_data *dev,
Andrew Kephartf2e1fc92011-08-21 13:34:13 -07001484 struct usb_interface *interface)
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001485{
1486 char *desc;
1487 char *buf;
1488 char *desc_end;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001489
Andrew Kephartf2e1fc92011-08-21 13:34:13 -07001490 int total_len = 0;
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001491
1492 buf = kzalloc(MAX_VENDOR_DESCRIPTOR_SIZE, GFP_KERNEL);
1493 if (!buf)
1494 return false;
1495 desc = buf;
1496
Andrew Kephartf2e1fc92011-08-21 13:34:13 -07001497 total_len = usb_get_descriptor(interface_to_usbdev(interface),
1498 0x5f, /* vendor specific */
1499 0, desc, MAX_VENDOR_DESCRIPTOR_SIZE);
1500
1501 /* if not found, look in configuration descriptor */
1502 if (total_len < 0) {
1503 if (0 == usb_get_extra_descriptor(interface->cur_altsetting,
1504 0x5f, &desc))
1505 total_len = (int) desc[0];
1506 }
1507
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001508 if (total_len > 5) {
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001509 pr_info("vendor descriptor length:%x data:%02x %02x %02x %02x" \
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001510 "%02x %02x %02x %02x %02x %02x %02x\n",
1511 total_len, desc[0],
1512 desc[1], desc[2], desc[3], desc[4], desc[5], desc[6],
1513 desc[7], desc[8], desc[9], desc[10]);
1514
1515 if ((desc[0] != total_len) || /* descriptor length */
1516 (desc[1] != 0x5f) || /* vendor descriptor type */
1517 (desc[2] != 0x01) || /* version (2 bytes) */
1518 (desc[3] != 0x00) ||
1519 (desc[4] != total_len - 2)) /* length after type */
1520 goto unrecognized;
1521
1522 desc_end = desc + total_len;
1523 desc += 5; /* the fixed header we've already parsed */
1524
1525 while (desc < desc_end) {
1526 u8 length;
1527 u16 key;
1528
1529 key = *((u16 *) desc);
1530 desc += sizeof(u16);
1531 length = *desc;
1532 desc++;
1533
1534 switch (key) {
1535 case 0x0200: { /* max_area */
1536 u32 max_area;
1537 max_area = le32_to_cpu(*((u32 *)desc));
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001538 pr_warn("DL chip limited to %d pixel modes\n",
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001539 max_area);
1540 dev->sku_pixel_limit = max_area;
1541 break;
1542 }
1543 default:
1544 break;
1545 }
1546 desc += length;
1547 }
Andrew Kephartf2e1fc92011-08-21 13:34:13 -07001548 } else {
1549 pr_info("vendor descriptor not available (%d)\n", total_len);
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001550 }
1551
1552 goto success;
1553
1554unrecognized:
1555 /* allow udlfb to load for now even if firmware unrecognized */
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001556 pr_err("Unrecognized vendor firmware descriptor\n");
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001557
1558success:
1559 kfree(buf);
1560 return true;
1561}
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001562static int dlfb_usb_probe(struct usb_interface *interface,
Bernie Thompson59277b62009-11-24 15:52:21 -08001563 const struct usb_device_id *id)
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001564{
Bernie Thompson59277b62009-11-24 15:52:21 -08001565 struct usb_device *usbdev;
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001566 struct dlfb_data *dev = 0;
Bernie Thompson33077b82010-09-05 16:35:19 -07001567 struct fb_info *info = 0;
Bernie Thompson59277b62009-11-24 15:52:21 -08001568 int retval = -ENOMEM;
Bernie Thompson2685cff2010-09-05 18:29:56 -07001569 int i;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001570
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001571 /* usb initialization */
1572
1573 usbdev = interface_to_usbdev(interface);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001574
Bernie Thompson59277b62009-11-24 15:52:21 -08001575 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1576 if (dev == NULL) {
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001577 err("dlfb_usb_probe: failed alloc of dev struct\n");
1578 goto error;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001579 }
1580
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001581 /* we need to wait for both usb and fbdev to spin down on disconnect */
1582 kref_init(&dev->kref); /* matching kref_put in usb .disconnect fn */
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001583 kref_get(&dev->kref); /* matching kref_put in free_framebuffer_work */
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001584
Bernie Thompson59277b62009-11-24 15:52:21 -08001585 dev->udev = usbdev;
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001586 dev->gdev = &usbdev->dev; /* our generic struct device * */
Bernie Thompson59277b62009-11-24 15:52:21 -08001587 usb_set_intfdata(interface, dev);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001588
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001589 pr_info("%s %s - serial #%s\n",
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001590 usbdev->manufacturer, usbdev->product, usbdev->serial);
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001591 pr_info("vid_%04x&pid_%04x&rev_%04x driver's dlfb_data struct at %p\n",
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001592 usbdev->descriptor.idVendor, usbdev->descriptor.idProduct,
1593 usbdev->descriptor.bcdDevice, dev);
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001594 pr_info("console enable=%d\n", console);
1595 pr_info("fb_defio enable=%d\n", fb_defio);
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001596
1597 dev->sku_pixel_limit = 2048 * 1152; /* default to maximum */
1598
Andrew Kephartf2e1fc92011-08-21 13:34:13 -07001599 if (!dlfb_parse_vendor_descriptor(dev, interface)) {
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001600 pr_err("firmware not recognized. Assume incompatible device\n");
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001601 goto error;
1602 }
1603
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001604 if (!dlfb_alloc_urb_list(dev, WRITES_IN_FLIGHT, MAX_TRANSFER)) {
1605 retval = -ENOMEM;
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001606 pr_err("dlfb_alloc_urb_list failed\n");
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001607 goto error;
1608 }
1609
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001610 /* We don't register a new USB class. Our client interface is fbdev */
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001611
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001612 /* allocates framebuffer driver structure, not framebuffer memory */
1613 info = framebuffer_alloc(0, &usbdev->dev);
1614 if (!info) {
1615 retval = -ENOMEM;
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001616 pr_err("framebuffer_alloc failed\n");
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001617 goto error;
1618 }
Bernie Thompson33077b82010-09-05 16:35:19 -07001619
Bernie Thompson59277b62009-11-24 15:52:21 -08001620 dev->info = info;
1621 info->par = dev;
1622 info->pseudo_palette = dev->pseudo_palette;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001623 info->fbops = &dlfb_ops;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001624
Bernie Thompson59277b62009-11-24 15:52:21 -08001625 retval = fb_alloc_cmap(&info->cmap, 256, 0);
1626 if (retval < 0) {
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001627 pr_err("fb_alloc_cmap failed %x\n", retval);
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001628 goto error;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001629 }
1630
Bernie Thompson33077b82010-09-05 16:35:19 -07001631 INIT_DELAYED_WORK(&dev->free_framebuffer_work,
1632 dlfb_free_framebuffer_work);
1633
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001634 INIT_LIST_HEAD(&info->modelist);
1635
1636 retval = dlfb_setup_modes(dev, info, NULL, 0);
1637 if (retval != 0) {
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001638 pr_err("unable to find common mode for display and adapter\n");
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001639 goto error;
1640 }
1641
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001642 /* ready to begin using device */
1643
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001644 atomic_set(&dev->usb_active, 1);
Bernie Thompson59277b62009-11-24 15:52:21 -08001645 dlfb_select_std_channel(dev);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001646
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001647 dlfb_ops_check_var(&info->var, info);
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001648 dlfb_ops_set_par(info);
1649
Bernie Thompson59277b62009-11-24 15:52:21 -08001650 retval = register_framebuffer(info);
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001651 if (retval < 0) {
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001652 pr_err("register_framebuffer failed %d\n", retval);
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001653 goto error;
1654 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001655
Liu Yuan94cd1ae2011-04-18 10:44:38 +00001656 for (i = 0; i < ARRAY_SIZE(fb_device_attrs); i++) {
1657 retval = device_create_file(info->dev, &fb_device_attrs[i]);
1658 if (retval) {
1659 pr_err("device_create_file failed %d\n", retval);
1660 goto err_del_attrs;
1661 }
1662 }
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -07001663
Liu Yuan94cd1ae2011-04-18 10:44:38 +00001664 retval = device_create_bin_file(info->dev, &edid_attr);
1665 if (retval) {
1666 pr_err("device_create_bin_file failed %d\n", retval);
1667 goto err_del_attrs;
1668 }
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001669
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001670 pr_info("DisplayLink USB device /dev/fb%d attached. %dx%d resolution."
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001671 " Using %dK framebuffer memory\n", info->node,
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001672 info->var.xres, info->var.yres,
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001673 ((dev->backing_buffer) ?
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001674 info->fix.smem_len * 2 : info->fix.smem_len) >> 10);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001675 return 0;
1676
Liu Yuan94cd1ae2011-04-18 10:44:38 +00001677err_del_attrs:
1678 for (i -= 1; i >= 0; i--)
1679 device_remove_file(info->dev, &fb_device_attrs[i]);
1680
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001681error:
1682 if (dev) {
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001683
Bernie Thompson33077b82010-09-05 16:35:19 -07001684 if (info) {
1685 if (info->cmap.len != 0)
1686 fb_dealloc_cmap(&info->cmap);
1687 if (info->monspecs.modedb)
1688 fb_destroy_modedb(info->monspecs.modedb);
1689 if (info->screen_base)
1690 vfree(info->screen_base);
1691
1692 fb_destroy_modelist(&info->modelist);
1693
1694 framebuffer_release(info);
1695 }
1696
1697 if (dev->backing_buffer)
1698 vfree(dev->backing_buffer);
1699
1700 kref_put(&dev->kref, dlfb_free); /* ref for framebuffer */
1701 kref_put(&dev->kref, dlfb_free); /* last ref from kref_init */
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001702
1703 /* dev has been deallocated. Do not dereference */
1704 }
1705
Bernie Thompson59277b62009-11-24 15:52:21 -08001706 return retval;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001707}
1708
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001709static void dlfb_usb_disconnect(struct usb_interface *interface)
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001710{
Bernie Thompson59277b62009-11-24 15:52:21 -08001711 struct dlfb_data *dev;
1712 struct fb_info *info;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001713 int i;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001714
Bernie Thompson59277b62009-11-24 15:52:21 -08001715 dev = usb_get_intfdata(interface);
Bernie Thompson59277b62009-11-24 15:52:21 -08001716 info = dev->info;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001717
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001718 pr_info("USB disconnect starting\n");
Bernie Thompson33077b82010-09-05 16:35:19 -07001719
1720 /* we virtualize until all fb clients release. Then we free */
1721 dev->virtualized = true;
1722
1723 /* When non-active we'll update virtual framebuffer, but no new urbs */
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001724 atomic_set(&dev->usb_active, 0);
1725
Bernie Thompson33077b82010-09-05 16:35:19 -07001726 /* remove udlfb's sysfs interfaces */
1727 for (i = 0; i < ARRAY_SIZE(fb_device_attrs); i++)
1728 device_remove_file(info->dev, &fb_device_attrs[i]);
1729 device_remove_bin_file(info->dev, &edid_attr);
1730
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001731 usb_set_intfdata(interface, NULL);
1732
Bernie Thompson33077b82010-09-05 16:35:19 -07001733 /* if clients still have us open, will be freed on last close */
1734 if (dev->fb_count == 0)
1735 schedule_delayed_work(&dev->free_framebuffer_work, 0);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001736
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001737 /* release reference taken by kref_init in probe() */
Bernie Thompson33077b82010-09-05 16:35:19 -07001738 kref_put(&dev->kref, dlfb_free);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001739
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001740 /* consider dlfb_data freed */
1741
1742 return;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001743}
1744
1745static struct usb_driver dlfb_driver = {
1746 .name = "udlfb",
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001747 .probe = dlfb_usb_probe,
1748 .disconnect = dlfb_usb_disconnect,
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001749 .id_table = id_table,
1750};
1751
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001752static int __init dlfb_module_init(void)
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001753{
1754 int res;
1755
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001756 res = usb_register(&dlfb_driver);
1757 if (res)
1758 err("usb_register failed. Error number %d", res);
1759
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001760 return res;
1761}
1762
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001763static void __exit dlfb_module_exit(void)
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001764{
1765 usb_deregister(&dlfb_driver);
1766}
1767
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001768module_init(dlfb_module_init);
1769module_exit(dlfb_module_exit);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001770
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001771static void dlfb_urb_completion(struct urb *urb)
1772{
1773 struct urb_node *unode = urb->context;
1774 struct dlfb_data *dev = unode->dev;
1775 unsigned long flags;
1776
1777 /* sync/async unlink faults aren't errors */
1778 if (urb->status) {
1779 if (!(urb->status == -ENOENT ||
1780 urb->status == -ECONNRESET ||
1781 urb->status == -ESHUTDOWN)) {
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001782 pr_err("%s - nonzero write bulk status received: %d\n",
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001783 __func__, urb->status);
1784 atomic_set(&dev->lost_pixels, 1);
1785 }
1786 }
1787
1788 urb->transfer_buffer_length = dev->urbs.size; /* reset to actual */
1789
1790 spin_lock_irqsave(&dev->urbs.lock, flags);
1791 list_add_tail(&unode->entry, &dev->urbs.list);
1792 dev->urbs.available++;
1793 spin_unlock_irqrestore(&dev->urbs.lock, flags);
1794
Bernie Thompson5bea1fb2010-09-05 18:28:29 -07001795 /*
1796 * When using fb_defio, we deadlock if up() is called
1797 * while another is waiting. So queue to another process.
1798 */
1799 if (fb_defio)
1800 schedule_delayed_work(&unode->release_urb_work, 0);
1801 else
1802 up(&dev->urbs.limit_sem);
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001803}
1804
1805static void dlfb_free_urb_list(struct dlfb_data *dev)
1806{
1807 int count = dev->urbs.count;
1808 struct list_head *node;
1809 struct urb_node *unode;
1810 struct urb *urb;
1811 int ret;
1812 unsigned long flags;
1813
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001814 pr_notice("Waiting for completes and freeing all render urbs\n");
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001815
1816 /* keep waiting and freeing, until we've got 'em all */
1817 while (count--) {
Bernie Thompson33077b82010-09-05 16:35:19 -07001818
1819 /* Getting interrupted means a leak, but ok at shutdown*/
1820 ret = down_interruptible(&dev->urbs.limit_sem);
1821 if (ret)
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001822 break;
Bernie Thompson33077b82010-09-05 16:35:19 -07001823
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001824 spin_lock_irqsave(&dev->urbs.lock, flags);
1825
1826 node = dev->urbs.list.next; /* have reserved one with sem */
1827 list_del_init(node);
1828
1829 spin_unlock_irqrestore(&dev->urbs.lock, flags);
1830
1831 unode = list_entry(node, struct urb_node, entry);
1832 urb = unode->urb;
1833
1834 /* Free each separately allocated piece */
Greg Kroah-Hartmanc220cc32010-04-29 15:36:29 -07001835 usb_free_coherent(urb->dev, dev->urbs.size,
1836 urb->transfer_buffer, urb->transfer_dma);
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001837 usb_free_urb(urb);
1838 kfree(node);
1839 }
1840
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001841}
1842
1843static int dlfb_alloc_urb_list(struct dlfb_data *dev, int count, size_t size)
1844{
1845 int i = 0;
1846 struct urb *urb;
1847 struct urb_node *unode;
1848 char *buf;
1849
1850 spin_lock_init(&dev->urbs.lock);
1851
1852 dev->urbs.size = size;
1853 INIT_LIST_HEAD(&dev->urbs.list);
1854
1855 while (i < count) {
1856 unode = kzalloc(sizeof(struct urb_node), GFP_KERNEL);
1857 if (!unode)
1858 break;
1859 unode->dev = dev;
1860
Bernie Thompson5bea1fb2010-09-05 18:28:29 -07001861 INIT_DELAYED_WORK(&unode->release_urb_work,
1862 dlfb_release_urb_work);
1863
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001864 urb = usb_alloc_urb(0, GFP_KERNEL);
1865 if (!urb) {
1866 kfree(unode);
1867 break;
1868 }
1869 unode->urb = urb;
1870
Greg Kroah-Hartmanc220cc32010-04-29 15:36:29 -07001871 buf = usb_alloc_coherent(dev->udev, MAX_TRANSFER, GFP_KERNEL,
1872 &urb->transfer_dma);
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001873 if (!buf) {
1874 kfree(unode);
1875 usb_free_urb(urb);
1876 break;
1877 }
1878
1879 /* urb->transfer_buffer_length set to actual before submit */
1880 usb_fill_bulk_urb(urb, dev->udev, usb_sndbulkpipe(dev->udev, 1),
1881 buf, size, dlfb_urb_completion, unode);
1882 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1883
1884 list_add_tail(&unode->entry, &dev->urbs.list);
1885
1886 i++;
1887 }
1888
1889 sema_init(&dev->urbs.limit_sem, i);
1890 dev->urbs.count = i;
1891 dev->urbs.available = i;
1892
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001893 pr_notice("allocated %d %d byte urbs\n", i, (int) size);
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001894
1895 return i;
1896}
1897
1898static struct urb *dlfb_get_urb(struct dlfb_data *dev)
1899{
1900 int ret = 0;
1901 struct list_head *entry;
1902 struct urb_node *unode;
1903 struct urb *urb = NULL;
1904 unsigned long flags;
1905
1906 /* Wait for an in-flight buffer to complete and get re-queued */
1907 ret = down_timeout(&dev->urbs.limit_sem, GET_URB_TIMEOUT);
1908 if (ret) {
1909 atomic_set(&dev->lost_pixels, 1);
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001910 pr_warn("wait for urb interrupted: %x available: %d\n",
Bernie Thompson5bea1fb2010-09-05 18:28:29 -07001911 ret, dev->urbs.available);
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001912 goto error;
1913 }
1914
1915 spin_lock_irqsave(&dev->urbs.lock, flags);
1916
1917 BUG_ON(list_empty(&dev->urbs.list)); /* reserved one with limit_sem */
1918 entry = dev->urbs.list.next;
1919 list_del_init(entry);
1920 dev->urbs.available--;
1921
1922 spin_unlock_irqrestore(&dev->urbs.lock, flags);
1923
1924 unode = list_entry(entry, struct urb_node, entry);
1925 urb = unode->urb;
1926
1927error:
1928 return urb;
1929}
1930
1931static int dlfb_submit_urb(struct dlfb_data *dev, struct urb *urb, size_t len)
1932{
1933 int ret;
1934
1935 BUG_ON(len > dev->urbs.size);
1936
1937 urb->transfer_buffer_length = len; /* set to actual payload len */
1938 ret = usb_submit_urb(urb, GFP_KERNEL);
1939 if (ret) {
1940 dlfb_urb_completion(urb); /* because no one else will */
1941 atomic_set(&dev->lost_pixels, 1);
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001942 pr_err("usb_submit_urb error %x\n", ret);
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001943 }
1944 return ret;
1945}
1946
Bernie Thompsond5ed5432010-09-05 16:35:39 -07001947module_param(console, bool, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
1948MODULE_PARM_DESC(console, "Allow fbcon to consume first framebuffer found");
1949
1950module_param(fb_defio, bool, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
1951MODULE_PARM_DESC(fb_defio, "Enable fb_defio mmap support. *Experimental*");
1952
Bernie Thompson59277b62009-11-24 15:52:21 -08001953MODULE_AUTHOR("Roberto De Ioris <roberto@unbit.it>, "
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001954 "Jaya Kumar <jayakumar.lkml@gmail.com>, "
1955 "Bernie Thompson <bernie@plugable.com>");
1956MODULE_DESCRIPTION("DisplayLink kernel framebuffer driver");
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001957MODULE_LICENSE("GPL");
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001958