blob: 52b0f3e8ccac694743aa4ba6b2d3db0c86448a0e [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>
Stephen Rothwell12e572d2011-05-20 16:29:01 +100032#include <linux/prefetch.h>
Paul Mundt96f8d862010-11-16 14:00:24 +090033#include <video/udlfb.h>
Paul Mundtb9f03a32011-01-06 18:04:02 +090034#include "edid.h"
Roberto De Ioris88e58b12009-06-03 14:03:06 -070035
Bernie Thompson59277b62009-11-24 15:52:21 -080036static struct fb_fix_screeninfo dlfb_fix = {
Bernie Thompson2469d5d2010-02-15 06:46:13 -080037 .id = "udlfb",
Bernie Thompson1d31a9e2010-02-15 06:45:43 -080038 .type = FB_TYPE_PACKED_PIXELS,
39 .visual = FB_VISUAL_TRUECOLOR,
40 .xpanstep = 0,
41 .ypanstep = 0,
42 .ywrapstep = 0,
43 .accel = FB_ACCEL_NONE,
Bernie Thompson59277b62009-11-24 15:52:21 -080044};
Roberto De Ioris88e58b12009-06-03 14:03:06 -070045
Bernie Thompson2469d5d2010-02-15 06:46:13 -080046static const u32 udlfb_info_flags = FBINFO_DEFAULT | FBINFO_READS_FAST |
Bernie Thompson2469d5d2010-02-15 06:46:13 -080047 FBINFO_VIRTFB |
Bernie Thompson2469d5d2010-02-15 06:46:13 -080048 FBINFO_HWACCEL_IMAGEBLIT | FBINFO_HWACCEL_FILLRECT |
49 FBINFO_HWACCEL_COPYAREA | FBINFO_MISC_ALWAYS_SETPAR;
50
Bernie Thompsoncc403dc2010-02-15 06:45:49 -080051/*
52 * There are many DisplayLink-based products, all with unique PIDs. We are able
53 * to support all volume ones (circa 2009) with a single driver, so we match
54 * globally on VID. TODO: Probe() needs to detect when we might be running
55 * "future" chips, and bail on those, so a compatible driver can match.
56 */
57static struct usb_device_id id_table[] = {
58 {.idVendor = 0x17e9, .match_flags = USB_DEVICE_ID_MATCH_VENDOR,},
59 {},
60};
61MODULE_DEVICE_TABLE(usb, id_table);
Bernie Thompson59277b62009-11-24 15:52:21 -080062
Bernie Thompsond5ed5432010-09-05 16:35:39 -070063/* module options */
64static int console; /* Optionally allow fbcon to consume first framebuffer */
65static int fb_defio; /* Optionally enable experimental fb_defio mmap support */
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 Thompson530f43a2010-02-15 06:46:21 -080098 * On/Off for driving the DisplayLink framebuffer to the display
Bernie Thompson9825f702010-09-05 16:35:10 -070099 * 0x00 H and V sync on
100 * 0x01 H and V sync off (screen blank but powered)
101 * 0x07 DPMS powerdown (requires modeset to come back)
Bernie Thompson59277b62009-11-24 15:52:21 -0800102 */
Bernie Thompson530f43a2010-02-15 06:46:21 -0800103static char *dlfb_enable_hvsync(char *buf, bool enable)
Bernie Thompson59277b62009-11-24 15:52:21 -0800104{
Bernie Thompson530f43a2010-02-15 06:46:21 -0800105 if (enable)
106 return dlfb_set_register(buf, 0x1F, 0x00);
107 else
Bernie Thompson9825f702010-09-05 16:35:10 -0700108 return dlfb_set_register(buf, 0x1F, 0x07);
Bernie Thompson59277b62009-11-24 15:52:21 -0800109}
110
Bernie Thompson45742032010-02-15 06:46:04 -0800111static char *dlfb_set_color_depth(char *buf, u8 selection)
Bernie Thompson59277b62009-11-24 15:52:21 -0800112{
Bernie Thompson45742032010-02-15 06:46:04 -0800113 return dlfb_set_register(buf, 0x00, selection);
Bernie Thompson59277b62009-11-24 15:52:21 -0800114}
115
Bernie Thompson45742032010-02-15 06:46:04 -0800116static char *dlfb_set_base16bpp(char *wrptr, u32 base)
Bernie Thompson59277b62009-11-24 15:52:21 -0800117{
Bernie Thompson1d31a9e2010-02-15 06:45:43 -0800118 /* the base pointer is 16 bits wide, 0x20 is hi byte. */
Bernie Thompson45742032010-02-15 06:46:04 -0800119 wrptr = dlfb_set_register(wrptr, 0x20, base >> 16);
120 wrptr = dlfb_set_register(wrptr, 0x21, base >> 8);
121 return dlfb_set_register(wrptr, 0x22, base);
Bernie Thompson59277b62009-11-24 15:52:21 -0800122}
123
Bernie Thompsonbd808162010-02-15 06:46:48 -0800124/*
125 * DisplayLink HW has separate 16bpp and 8bpp framebuffers.
126 * In 24bpp modes, the low 323 RGB bits go in the 8bpp framebuffer
127 */
Bernie Thompson45742032010-02-15 06:46:04 -0800128static char *dlfb_set_base8bpp(char *wrptr, u32 base)
Bernie Thompson59277b62009-11-24 15:52:21 -0800129{
Bernie Thompson45742032010-02-15 06:46:04 -0800130 wrptr = dlfb_set_register(wrptr, 0x26, base >> 16);
131 wrptr = dlfb_set_register(wrptr, 0x27, base >> 8);
132 return dlfb_set_register(wrptr, 0x28, base);
Bernie Thompson59277b62009-11-24 15:52:21 -0800133}
134
Bernie Thompson45742032010-02-15 06:46:04 -0800135static char *dlfb_set_register_16(char *wrptr, u8 reg, u16 value)
Bernie Thompson59277b62009-11-24 15:52:21 -0800136{
Bernie Thompson45742032010-02-15 06:46:04 -0800137 wrptr = dlfb_set_register(wrptr, reg, value >> 8);
138 return dlfb_set_register(wrptr, reg+1, value);
Bernie Thompson59277b62009-11-24 15:52:21 -0800139}
140
141/*
142 * This is kind of weird because the controller takes some
143 * register values in a different byte order than other registers.
144 */
Bernie Thompson45742032010-02-15 06:46:04 -0800145static char *dlfb_set_register_16be(char *wrptr, u8 reg, u16 value)
Bernie Thompson59277b62009-11-24 15:52:21 -0800146{
Bernie Thompson45742032010-02-15 06:46:04 -0800147 wrptr = dlfb_set_register(wrptr, reg, value);
148 return dlfb_set_register(wrptr, reg+1, value >> 8);
Bernie Thompson59277b62009-11-24 15:52:21 -0800149}
150
151/*
152 * LFSR is linear feedback shift register. The reason we have this is
153 * because the display controller needs to minimize the clock depth of
154 * various counters used in the display path. So this code reverses the
155 * provided value into the lfsr16 value by counting backwards to get
156 * the value that needs to be set in the hardware comparator to get the
157 * same actual count. This makes sense once you read above a couple of
158 * times and think about it from a hardware perspective.
159 */
Bernie Thompsonbd808162010-02-15 06:46:48 -0800160static u16 dlfb_lfsr16(u16 actual_count)
Bernie Thompson59277b62009-11-24 15:52:21 -0800161{
162 u32 lv = 0xFFFF; /* This is the lfsr value that the hw starts with */
163
164 while (actual_count--) {
165 lv = ((lv << 1) |
166 (((lv >> 15) ^ (lv >> 4) ^ (lv >> 2) ^ (lv >> 1)) & 1))
167 & 0xFFFF;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700168 }
Bernie Thompson59277b62009-11-24 15:52:21 -0800169
170 return (u16) lv;
171}
172
173/*
174 * This does LFSR conversion on the value that is to be written.
175 * See LFSR explanation above for more detail.
176 */
Bernie Thompson45742032010-02-15 06:46:04 -0800177static char *dlfb_set_register_lfsr16(char *wrptr, u8 reg, u16 value)
Bernie Thompson59277b62009-11-24 15:52:21 -0800178{
Bernie Thompsonbd808162010-02-15 06:46:48 -0800179 return dlfb_set_register_16(wrptr, reg, dlfb_lfsr16(value));
Bernie Thompson59277b62009-11-24 15:52:21 -0800180}
181
182/*
183 * This takes a standard fbdev screeninfo struct and all of its monitor mode
184 * details and converts them into the DisplayLink equivalent register commands.
185 */
Bernie Thompson45742032010-02-15 06:46:04 -0800186static char *dlfb_set_vid_cmds(char *wrptr, struct fb_var_screeninfo *var)
Bernie Thompson59277b62009-11-24 15:52:21 -0800187{
188 u16 xds, yds;
189 u16 xde, yde;
190 u16 yec;
191
Bernie Thompson59277b62009-11-24 15:52:21 -0800192 /* x display start */
193 xds = var->left_margin + var->hsync_len;
Bernie Thompson45742032010-02-15 06:46:04 -0800194 wrptr = dlfb_set_register_lfsr16(wrptr, 0x01, xds);
Bernie Thompson59277b62009-11-24 15:52:21 -0800195 /* x display end */
196 xde = xds + var->xres;
Bernie Thompson45742032010-02-15 06:46:04 -0800197 wrptr = dlfb_set_register_lfsr16(wrptr, 0x03, xde);
Bernie Thompson59277b62009-11-24 15:52:21 -0800198
199 /* y display start */
200 yds = var->upper_margin + var->vsync_len;
Bernie Thompson45742032010-02-15 06:46:04 -0800201 wrptr = dlfb_set_register_lfsr16(wrptr, 0x05, yds);
Bernie Thompson59277b62009-11-24 15:52:21 -0800202 /* y display end */
203 yde = yds + var->yres;
Bernie Thompson45742032010-02-15 06:46:04 -0800204 wrptr = dlfb_set_register_lfsr16(wrptr, 0x07, yde);
Bernie Thompson59277b62009-11-24 15:52:21 -0800205
206 /* x end count is active + blanking - 1 */
Bernie Thompson45742032010-02-15 06:46:04 -0800207 wrptr = dlfb_set_register_lfsr16(wrptr, 0x09,
208 xde + var->right_margin - 1);
Bernie Thompson59277b62009-11-24 15:52:21 -0800209
210 /* libdlo hardcodes hsync start to 1 */
Bernie Thompson45742032010-02-15 06:46:04 -0800211 wrptr = dlfb_set_register_lfsr16(wrptr, 0x0B, 1);
Bernie Thompson59277b62009-11-24 15:52:21 -0800212
213 /* hsync end is width of sync pulse + 1 */
Bernie Thompson45742032010-02-15 06:46:04 -0800214 wrptr = dlfb_set_register_lfsr16(wrptr, 0x0D, var->hsync_len + 1);
Bernie Thompson59277b62009-11-24 15:52:21 -0800215
216 /* hpixels is active pixels */
Bernie Thompson45742032010-02-15 06:46:04 -0800217 wrptr = dlfb_set_register_16(wrptr, 0x0F, var->xres);
Bernie Thompson59277b62009-11-24 15:52:21 -0800218
219 /* yendcount is vertical active + vertical blanking */
220 yec = var->yres + var->upper_margin + var->lower_margin +
221 var->vsync_len;
Bernie Thompson45742032010-02-15 06:46:04 -0800222 wrptr = dlfb_set_register_lfsr16(wrptr, 0x11, yec);
Bernie Thompson59277b62009-11-24 15:52:21 -0800223
224 /* libdlo hardcodes vsync start to 0 */
Bernie Thompson45742032010-02-15 06:46:04 -0800225 wrptr = dlfb_set_register_lfsr16(wrptr, 0x13, 0);
Bernie Thompson59277b62009-11-24 15:52:21 -0800226
227 /* vsync end is width of vsync pulse */
Bernie Thompson45742032010-02-15 06:46:04 -0800228 wrptr = dlfb_set_register_lfsr16(wrptr, 0x15, var->vsync_len);
Bernie Thompson59277b62009-11-24 15:52:21 -0800229
230 /* vpixels is active pixels */
Bernie Thompson45742032010-02-15 06:46:04 -0800231 wrptr = dlfb_set_register_16(wrptr, 0x17, var->yres);
Bernie Thompson59277b62009-11-24 15:52:21 -0800232
233 /* convert picoseconds to 5kHz multiple for pclk5k = x * 1E12/5k */
Bernie Thompson45742032010-02-15 06:46:04 -0800234 wrptr = dlfb_set_register_16be(wrptr, 0x1B,
235 200*1000*1000/var->pixclock);
Bernie Thompson59277b62009-11-24 15:52:21 -0800236
237 return wrptr;
238}
239
240/*
241 * This takes a standard fbdev screeninfo struct that was fetched or prepared
242 * and then generates the appropriate command sequence that then drives the
243 * display controller.
244 */
245static int dlfb_set_video_mode(struct dlfb_data *dev,
246 struct fb_var_screeninfo *var)
247{
248 char *buf;
249 char *wrptr;
250 int retval = 0;
251 int writesize;
Bernie Thompson530f43a2010-02-15 06:46:21 -0800252 struct urb *urb;
Bernie Thompson59277b62009-11-24 15:52:21 -0800253
Bernie Thompson530f43a2010-02-15 06:46:21 -0800254 if (!atomic_read(&dev->usb_active))
255 return -EPERM;
256
257 urb = dlfb_get_urb(dev);
258 if (!urb)
259 return -ENOMEM;
Bernie Thompson2685cff2010-09-05 18:29:56 -0700260
Bernie Thompson530f43a2010-02-15 06:46:21 -0800261 buf = (char *) urb->transfer_buffer;
Bernie Thompson59277b62009-11-24 15:52:21 -0800262
263 /*
264 * This first section has to do with setting the base address on the
265 * controller * associated with the display. There are 2 base
266 * pointers, currently, we only * use the 16 bpp segment.
267 */
Bernie Thompson45742032010-02-15 06:46:04 -0800268 wrptr = dlfb_vidreg_lock(buf);
269 wrptr = dlfb_set_color_depth(wrptr, 0x00);
Bernie Thompson59277b62009-11-24 15:52:21 -0800270 /* set base for 16bpp segment to 0 */
Bernie Thompson45742032010-02-15 06:46:04 -0800271 wrptr = dlfb_set_base16bpp(wrptr, 0);
Bernie Thompson59277b62009-11-24 15:52:21 -0800272 /* set base for 8bpp segment to end of fb */
Bernie Thompson45742032010-02-15 06:46:04 -0800273 wrptr = dlfb_set_base8bpp(wrptr, dev->info->fix.smem_len);
Bernie Thompson59277b62009-11-24 15:52:21 -0800274
Bernie Thompson45742032010-02-15 06:46:04 -0800275 wrptr = dlfb_set_vid_cmds(wrptr, var);
Bernie Thompson530f43a2010-02-15 06:46:21 -0800276 wrptr = dlfb_enable_hvsync(wrptr, true);
Bernie Thompson45742032010-02-15 06:46:04 -0800277 wrptr = dlfb_vidreg_unlock(wrptr);
Bernie Thompson59277b62009-11-24 15:52:21 -0800278
279 writesize = wrptr - buf;
280
Bernie Thompson530f43a2010-02-15 06:46:21 -0800281 retval = dlfb_submit_urb(dev, urb, writesize);
Bernie Thompson59277b62009-11-24 15:52:21 -0800282
Bernie Thompson59277b62009-11-24 15:52:21 -0800283 return retval;
284}
285
Bernie Thompson45742032010-02-15 06:46:04 -0800286static int dlfb_ops_mmap(struct fb_info *info, struct vm_area_struct *vma)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700287{
288 unsigned long start = vma->vm_start;
289 unsigned long size = vma->vm_end - vma->vm_start;
290 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
291 unsigned long page, pos;
292
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700293 if (offset + size > info->fix.smem_len)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700294 return -EINVAL;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700295
296 pos = (unsigned long)info->fix.smem_start + offset;
297
Paul Mundt81f6f3c2011-01-06 18:07:54 +0900298 pr_notice("mmap() framebuffer addr:%lu size:%lu\n",
Bernie Thompson2685cff2010-09-05 18:29:56 -0700299 pos, size);
300
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700301 while (size > 0) {
302 page = vmalloc_to_pfn((void *)pos);
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700303 if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED))
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700304 return -EAGAIN;
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700305
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700306 start += PAGE_SIZE;
307 pos += PAGE_SIZE;
308 if (size > PAGE_SIZE)
309 size -= PAGE_SIZE;
310 else
311 size = 0;
312 }
313
314 vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
315 return 0;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700316}
317
Bernie Thompson530f43a2010-02-15 06:46:21 -0800318/*
319 * Trims identical data from front and back of line
320 * Sets new front buffer address and width
321 * And returns byte count of identical pixels
322 * Assumes CPU natural alignment (unsigned long)
323 * for back and front buffer ptrs and width
324 */
325static int dlfb_trim_hline(const u8 *bback, const u8 **bfront, int *width_bytes)
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700326{
Bernie Thompson530f43a2010-02-15 06:46:21 -0800327 int j, k;
328 const unsigned long *back = (const unsigned long *) bback;
329 const unsigned long *front = (const unsigned long *) *bfront;
330 const int width = *width_bytes / sizeof(unsigned long);
331 int identical = width;
332 int start = width;
333 int end = width;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700334
Bernie Thompson530f43a2010-02-15 06:46:21 -0800335 prefetch((void *) front);
336 prefetch((void *) back);
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700337
Bernie Thompson530f43a2010-02-15 06:46:21 -0800338 for (j = 0; j < width; j++) {
339 if (back[j] != front[j]) {
340 start = j;
341 break;
342 }
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700343 }
344
Bernie Thompson530f43a2010-02-15 06:46:21 -0800345 for (k = width - 1; k > j; k--) {
346 if (back[k] != front[k]) {
347 end = k+1;
348 break;
349 }
350 }
351
352 identical = start + (width - end);
353 *bfront = (u8 *) &front[start];
354 *width_bytes = (end - start) * sizeof(unsigned long);
355
356 return identical * sizeof(unsigned long);
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700357}
358
359/*
Pavel Machek3b7b31f2010-04-03 07:00:37 +0200360 * Render a command stream for an encoded horizontal line segment of pixels.
361 *
362 * A command buffer holds several commands.
363 * It always begins with a fresh command header
364 * (the protocol doesn't require this, but we enforce it to allow
365 * multiple buffers to be potentially encoded and sent in parallel).
366 * A single command encodes one contiguous horizontal line of pixels
367 *
368 * The function relies on the client to do all allocation, so that
369 * rendering can be done directly to output buffers (e.g. USB URBs).
370 * The function fills the supplied command buffer, providing information
371 * on where it left off, so the client may call in again with additional
372 * buffers if the line will take several buffers to complete.
373 *
374 * A single command can transmit a maximum of 256 pixels,
375 * regardless of the compression ratio (protocol design limit).
376 * To the hardware, 0 for a size byte means 256
Bernie Thompson2685cff2010-09-05 18:29:56 -0700377 *
Pavel Machek3b7b31f2010-04-03 07:00:37 +0200378 * Rather than 256 pixel commands which are either rl or raw encoded,
379 * the rlx command simply assumes alternating raw and rl spans within one cmd.
380 * This has a slightly larger header overhead, but produces more even results.
381 * It also processes all data (read and write) in a single pass.
382 * Performance benchmarks of common cases show it having just slightly better
Bernie Thompson2685cff2010-09-05 18:29:56 -0700383 * compression than 256 pixel raw or rle commands, with similar CPU consumpion.
Pavel Machek3b7b31f2010-04-03 07:00:37 +0200384 * But for very rl friendly data, will compress not quite as well.
385 */
Bernie Thompson530f43a2010-02-15 06:46:21 -0800386static void dlfb_compress_hline(
387 const uint16_t **pixel_start_ptr,
388 const uint16_t *const pixel_end,
389 uint32_t *device_address_ptr,
390 uint8_t **command_buffer_ptr,
391 const uint8_t *const cmd_buffer_end)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700392{
Bernie Thompson530f43a2010-02-15 06:46:21 -0800393 const uint16_t *pixel = *pixel_start_ptr;
394 uint32_t dev_addr = *device_address_ptr;
395 uint8_t *cmd = *command_buffer_ptr;
396 const int bpp = 2;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700397
Bernie Thompson530f43a2010-02-15 06:46:21 -0800398 while ((pixel_end > pixel) &&
399 (cmd_buffer_end - MIN_RLX_CMD_BYTES > cmd)) {
400 uint8_t *raw_pixels_count_byte = 0;
401 uint8_t *cmd_pixels_count_byte = 0;
402 const uint16_t *raw_pixel_start = 0;
403 const uint16_t *cmd_pixel_start, *cmd_pixel_end = 0;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700404
Bernie Thompson530f43a2010-02-15 06:46:21 -0800405 prefetchw((void *) cmd); /* pull in one cache line at least */
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700406
Bernie Thompson530f43a2010-02-15 06:46:21 -0800407 *cmd++ = 0xAF;
408 *cmd++ = 0x6B;
Bernie Thompson1572f912010-09-05 16:35:27 -0700409 *cmd++ = (uint8_t) ((dev_addr >> 16) & 0xFF);
410 *cmd++ = (uint8_t) ((dev_addr >> 8) & 0xFF);
411 *cmd++ = (uint8_t) ((dev_addr) & 0xFF);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700412
Bernie Thompson530f43a2010-02-15 06:46:21 -0800413 cmd_pixels_count_byte = cmd++; /* we'll know this later */
414 cmd_pixel_start = pixel;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700415
Bernie Thompson530f43a2010-02-15 06:46:21 -0800416 raw_pixels_count_byte = cmd++; /* we'll know this later */
417 raw_pixel_start = pixel;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700418
Bernie Thompson530f43a2010-02-15 06:46:21 -0800419 cmd_pixel_end = pixel + min(MAX_CMD_PIXELS + 1,
420 min((int)(pixel_end - pixel),
421 (int)(cmd_buffer_end - cmd) / bpp));
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700422
Bernie Thompson530f43a2010-02-15 06:46:21 -0800423 prefetch_range((void *) pixel, (cmd_pixel_end - pixel) * bpp);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700424
Bernie Thompson530f43a2010-02-15 06:46:21 -0800425 while (pixel < cmd_pixel_end) {
426 const uint16_t * const repeating_pixel = pixel;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700427
Bernie Thompson530f43a2010-02-15 06:46:21 -0800428 *(uint16_t *)cmd = cpu_to_be16p(pixel);
429 cmd += 2;
430 pixel++;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700431
Bernie Thompson530f43a2010-02-15 06:46:21 -0800432 if (unlikely((pixel < cmd_pixel_end) &&
433 (*pixel == *repeating_pixel))) {
434 /* go back and fill in raw pixel count */
435 *raw_pixels_count_byte = ((repeating_pixel -
436 raw_pixel_start) + 1) & 0xFF;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700437
Bernie Thompson530f43a2010-02-15 06:46:21 -0800438 while ((pixel < cmd_pixel_end)
439 && (*pixel == *repeating_pixel)) {
440 pixel++;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700441 }
Bernie Thompson59277b62009-11-24 15:52:21 -0800442
Bernie Thompson530f43a2010-02-15 06:46:21 -0800443 /* immediately after raw data is repeat byte */
444 *cmd++ = ((pixel - repeating_pixel) - 1) & 0xFF;
Bernie Thompson59277b62009-11-24 15:52:21 -0800445
Bernie Thompson530f43a2010-02-15 06:46:21 -0800446 /* Then start another raw pixel span */
447 raw_pixel_start = pixel;
448 raw_pixels_count_byte = cmd++;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700449 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700450 }
451
Bernie Thompson530f43a2010-02-15 06:46:21 -0800452 if (pixel > raw_pixel_start) {
453 /* finalize last RAW span */
454 *raw_pixels_count_byte = (pixel-raw_pixel_start) & 0xFF;
455 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700456
Bernie Thompson530f43a2010-02-15 06:46:21 -0800457 *cmd_pixels_count_byte = (pixel - cmd_pixel_start) & 0xFF;
458 dev_addr += (pixel - cmd_pixel_start) * bpp;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700459 }
460
Bernie Thompson530f43a2010-02-15 06:46:21 -0800461 if (cmd_buffer_end <= MIN_RLX_CMD_BYTES + cmd) {
462 /* Fill leftover bytes with no-ops */
463 if (cmd_buffer_end > cmd)
464 memset(cmd, 0xAF, cmd_buffer_end - cmd);
465 cmd = (uint8_t *) cmd_buffer_end;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700466 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700467
Bernie Thompson530f43a2010-02-15 06:46:21 -0800468 *command_buffer_ptr = cmd;
469 *pixel_start_ptr = pixel;
470 *device_address_ptr = dev_addr;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700471
Bernie Thompson530f43a2010-02-15 06:46:21 -0800472 return;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700473}
474
Bernie Thompson530f43a2010-02-15 06:46:21 -0800475/*
476 * There are 3 copies of every pixel: The front buffer that the fbdev
477 * client renders to, the actual framebuffer across the USB bus in hardware
478 * (that we can only write to, slowly, and can never read), and (optionally)
479 * our shadow copy that tracks what's been sent to that hardware buffer.
480 */
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700481static int dlfb_render_hline(struct dlfb_data *dev, struct urb **urb_ptr,
Bernie Thompson530f43a2010-02-15 06:46:21 -0800482 const char *front, char **urb_buf_ptr,
483 u32 byte_offset, u32 byte_width,
484 int *ident_ptr, int *sent_ptr)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700485{
Bernie Thompson530f43a2010-02-15 06:46:21 -0800486 const u8 *line_start, *line_end, *next_pixel;
487 u32 dev_addr = dev->base16 + byte_offset;
488 struct urb *urb = *urb_ptr;
489 u8 *cmd = *urb_buf_ptr;
490 u8 *cmd_end = (u8 *) urb->transfer_buffer + urb->transfer_buffer_length;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700491
Bernie Thompson530f43a2010-02-15 06:46:21 -0800492 line_start = (u8 *) (front + byte_offset);
493 next_pixel = line_start;
494 line_end = next_pixel + byte_width;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700495
Bernie Thompson530f43a2010-02-15 06:46:21 -0800496 if (dev->backing_buffer) {
497 int offset;
498 const u8 *back_start = (u8 *) (dev->backing_buffer
499 + byte_offset);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700500
Bernie Thompson530f43a2010-02-15 06:46:21 -0800501 *ident_ptr += dlfb_trim_hline(back_start, &next_pixel,
502 &byte_width);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700503
Bernie Thompson530f43a2010-02-15 06:46:21 -0800504 offset = next_pixel - line_start;
505 line_end = next_pixel + byte_width;
506 dev_addr += offset;
507 back_start += offset;
508 line_start += offset;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700509
Bernie Thompson530f43a2010-02-15 06:46:21 -0800510 memcpy((char *)back_start, (char *) line_start,
511 byte_width);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700512 }
513
Bernie Thompson530f43a2010-02-15 06:46:21 -0800514 while (next_pixel < line_end) {
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700515
Bernie Thompson530f43a2010-02-15 06:46:21 -0800516 dlfb_compress_hline((const uint16_t **) &next_pixel,
517 (const uint16_t *) line_end, &dev_addr,
518 (u8 **) &cmd, (u8 *) cmd_end);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700519
Bernie Thompson530f43a2010-02-15 06:46:21 -0800520 if (cmd >= cmd_end) {
521 int len = cmd - (u8 *) urb->transfer_buffer;
522 if (dlfb_submit_urb(dev, urb, len))
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700523 return 1; /* lost pixels is set */
Bernie Thompson530f43a2010-02-15 06:46:21 -0800524 *sent_ptr += len;
525 urb = dlfb_get_urb(dev);
526 if (!urb)
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700527 return 1; /* lost_pixels is set */
Bernie Thompson530f43a2010-02-15 06:46:21 -0800528 *urb_ptr = urb;
529 cmd = urb->transfer_buffer;
530 cmd_end = &cmd[urb->transfer_buffer_length];
531 }
532 }
533
534 *urb_buf_ptr = cmd;
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700535
536 return 0;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700537}
538
Bernie Thompson530f43a2010-02-15 06:46:21 -0800539int dlfb_handle_damage(struct dlfb_data *dev, int x, int y,
540 int width, int height, char *data)
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700541{
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700542 int i, ret;
Bernie Thompson530f43a2010-02-15 06:46:21 -0800543 char *cmd;
544 cycles_t start_cycles, end_cycles;
545 int bytes_sent = 0;
546 int bytes_identical = 0;
547 struct urb *urb;
548 int aligned_x;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700549
Bernie Thompson530f43a2010-02-15 06:46:21 -0800550 start_cycles = get_cycles();
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700551
Bernie Thompson530f43a2010-02-15 06:46:21 -0800552 aligned_x = DL_ALIGN_DOWN(x, sizeof(unsigned long));
553 width = DL_ALIGN_UP(width + (x-aligned_x), sizeof(unsigned long));
554 x = aligned_x;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700555
Bernie Thompson530f43a2010-02-15 06:46:21 -0800556 if ((width <= 0) ||
557 (x + width > dev->info->var.xres) ||
558 (y + height > dev->info->var.yres))
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700559 return -EINVAL;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700560
Bernie Thompson530f43a2010-02-15 06:46:21 -0800561 if (!atomic_read(&dev->usb_active))
562 return 0;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700563
Bernie Thompson530f43a2010-02-15 06:46:21 -0800564 urb = dlfb_get_urb(dev);
565 if (!urb)
566 return 0;
567 cmd = urb->transfer_buffer;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700568
Bernie Thompson530f43a2010-02-15 06:46:21 -0800569 for (i = y; i < y + height ; i++) {
570 const int line_offset = dev->info->fix.line_length * i;
571 const int byte_offset = line_offset + (x * BPP);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700572
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700573 if (dlfb_render_hline(dev, &urb,
574 (char *) dev->info->fix.smem_start,
Bernie Thompson2685cff2010-09-05 18:29:56 -0700575 &cmd, byte_offset, width * BPP,
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700576 &bytes_identical, &bytes_sent))
577 goto error;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700578 }
579
Bernie Thompson530f43a2010-02-15 06:46:21 -0800580 if (cmd > (char *) urb->transfer_buffer) {
581 /* Send partial buffer remaining before exiting */
582 int len = cmd - (char *) urb->transfer_buffer;
583 ret = dlfb_submit_urb(dev, urb, len);
584 bytes_sent += len;
585 } else
586 dlfb_urb_completion(urb);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700587
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700588error:
Bernie Thompson530f43a2010-02-15 06:46:21 -0800589 atomic_add(bytes_sent, &dev->bytes_sent);
590 atomic_add(bytes_identical, &dev->bytes_identical);
591 atomic_add(width*height*2, &dev->bytes_rendered);
592 end_cycles = get_cycles();
593 atomic_add(((unsigned int) ((end_cycles - start_cycles)
594 >> 10)), /* Kcycles */
595 &dev->cpu_kcycles_used);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700596
Bernie Thompson530f43a2010-02-15 06:46:21 -0800597 return 0;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700598}
599
Bernie Thompsond46ecb92010-09-05 16:35:04 -0700600/*
601 * Path triggered by usermode clients who write to filesystem
602 * e.g. cat filename > /dev/fb1
603 * Not used by X Windows or text-mode console. But useful for testing.
604 * Slow because of extra copy and we must assume all pixels dirty.
605 */
606static ssize_t dlfb_ops_write(struct fb_info *info, const char __user *buf,
607 size_t count, loff_t *ppos)
608{
Paul Mundt1a3e5282011-01-06 17:29:24 +0900609 ssize_t result;
Bernie Thompsond46ecb92010-09-05 16:35:04 -0700610 struct dlfb_data *dev = info->par;
611 u32 offset = (u32) *ppos;
612
Bernie Thompsond46ecb92010-09-05 16:35:04 -0700613 result = fb_sys_write(info, buf, count, ppos);
614
615 if (result > 0) {
616 int start = max((int)(offset / info->fix.line_length) - 1, 0);
617 int lines = min((u32)((result / info->fix.line_length) + 1),
618 (u32)info->var.yres);
619
620 dlfb_handle_damage(dev, 0, start, info->var.xres,
621 lines, info->screen_base);
622 }
Bernie Thompsond46ecb92010-09-05 16:35:04 -0700623
624 return result;
625}
626
Bernie Thompson530f43a2010-02-15 06:46:21 -0800627/* hardware has native COPY command (see libdlo), but not worth it for fbcon */
Bernie Thompson45742032010-02-15 06:46:04 -0800628static void dlfb_ops_copyarea(struct fb_info *info,
Bernie Thompson530f43a2010-02-15 06:46:21 -0800629 const struct fb_copyarea *area)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700630{
Bernie Thompson530f43a2010-02-15 06:46:21 -0800631
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700632 struct dlfb_data *dev = info->par;
633
Bernie Thompson530f43a2010-02-15 06:46:21 -0800634 sys_copyarea(info, area);
635
636 dlfb_handle_damage(dev, area->dx, area->dy,
637 area->width, area->height, info->screen_base);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700638}
639
Bernie Thompson45742032010-02-15 06:46:04 -0800640static void dlfb_ops_imageblit(struct fb_info *info,
Bernie Thompson530f43a2010-02-15 06:46:21 -0800641 const struct fb_image *image)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700642{
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700643 struct dlfb_data *dev = info->par;
Bernie Thompson530f43a2010-02-15 06:46:21 -0800644
Bernie Thompson530f43a2010-02-15 06:46:21 -0800645 sys_imageblit(info, image);
646
647 dlfb_handle_damage(dev, image->dx, image->dy,
648 image->width, image->height, info->screen_base);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700649}
650
Bernie Thompson45742032010-02-15 06:46:04 -0800651static void dlfb_ops_fillrect(struct fb_info *info,
Bernie Thompson530f43a2010-02-15 06:46:21 -0800652 const struct fb_fillrect *rect)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700653{
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700654 struct dlfb_data *dev = info->par;
655
Bernie Thompson530f43a2010-02-15 06:46:21 -0800656 sys_fillrect(info, rect);
657
658 dlfb_handle_damage(dev, rect->dx, rect->dy, rect->width,
659 rect->height, info->screen_base);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700660}
661
Bernie Thompsond5ed5432010-09-05 16:35:39 -0700662/*
663 * NOTE: fb_defio.c is holding info->fbdefio.mutex
664 * Touching ANY framebuffer memory that triggers a page fault
665 * in fb_defio will cause a deadlock, when it also tries to
666 * grab the same mutex.
667 */
668static void dlfb_dpy_deferred_io(struct fb_info *info,
669 struct list_head *pagelist)
670{
671 struct page *cur;
672 struct fb_deferred_io *fbdefio = info->fbdefio;
673 struct dlfb_data *dev = info->par;
674 struct urb *urb;
675 char *cmd;
676 cycles_t start_cycles, end_cycles;
677 int bytes_sent = 0;
678 int bytes_identical = 0;
679 int bytes_rendered = 0;
680
681 if (!fb_defio)
682 return;
683
684 if (!atomic_read(&dev->usb_active))
685 return;
686
687 start_cycles = get_cycles();
688
689 urb = dlfb_get_urb(dev);
690 if (!urb)
691 return;
692
693 cmd = urb->transfer_buffer;
694
695 /* walk the written page list and render each to device */
696 list_for_each_entry(cur, &fbdefio->pagelist, lru) {
697
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700698 if (dlfb_render_hline(dev, &urb, (char *) info->fix.smem_start,
Bernie Thompsond5ed5432010-09-05 16:35:39 -0700699 &cmd, cur->index << PAGE_SHIFT,
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700700 PAGE_SIZE, &bytes_identical, &bytes_sent))
701 goto error;
Bernie Thompsond5ed5432010-09-05 16:35:39 -0700702 bytes_rendered += PAGE_SIZE;
703 }
704
705 if (cmd > (char *) urb->transfer_buffer) {
706 /* Send partial buffer remaining before exiting */
707 int len = cmd - (char *) urb->transfer_buffer;
708 dlfb_submit_urb(dev, urb, len);
709 bytes_sent += len;
710 } else
711 dlfb_urb_completion(urb);
712
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700713error:
Bernie Thompsond5ed5432010-09-05 16:35:39 -0700714 atomic_add(bytes_sent, &dev->bytes_sent);
715 atomic_add(bytes_identical, &dev->bytes_identical);
716 atomic_add(bytes_rendered, &dev->bytes_rendered);
717 end_cycles = get_cycles();
718 atomic_add(((unsigned int) ((end_cycles - start_cycles)
719 >> 10)), /* Kcycles */
720 &dev->cpu_kcycles_used);
721}
722
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700723static int dlfb_get_edid(struct dlfb_data *dev, char *edid, int len)
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800724{
725 int i;
726 int ret;
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700727 char *rbuf;
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800728
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700729 rbuf = kmalloc(2, GFP_KERNEL);
730 if (!rbuf)
731 return 0;
732
733 for (i = 0; i < len; i++) {
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800734 ret = usb_control_msg(dev->udev,
735 usb_rcvctrlpipe(dev->udev, 0), (0x02),
736 (0x80 | (0x02 << 5)), i << 8, 0xA1, rbuf, 2,
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700737 HZ);
738 if (ret < 1) {
Paul Mundt81f6f3c2011-01-06 18:07:54 +0900739 pr_err("Read EDID byte %d failed err %x\n", i, ret);
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700740 i--;
741 break;
742 }
743 edid[i] = rbuf[1];
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800744 }
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700745
746 kfree(rbuf);
747
748 return i;
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800749}
750
Bernie Thompson45742032010-02-15 06:46:04 -0800751static int dlfb_ops_ioctl(struct fb_info *info, unsigned int cmd,
Bernie Thompson530f43a2010-02-15 06:46:21 -0800752 unsigned long arg)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700753{
Bernie Thompson530f43a2010-02-15 06:46:21 -0800754
755 struct dlfb_data *dev = info->par;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700756 struct dloarea *area = NULL;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700757
Bernie Thompson530f43a2010-02-15 06:46:21 -0800758 if (!atomic_read(&dev->usb_active))
759 return 0;
760
761 /* TODO: Update X server to get this from sysfs instead */
762 if (cmd == DLFB_IOCTL_RETURN_EDID) {
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700763 char *edid = (char *)arg;
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700764 if (copy_to_user(edid, dev->edid, dev->edid_size))
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700765 return -EFAULT;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700766 return 0;
767 }
768
Bernie Thompson530f43a2010-02-15 06:46:21 -0800769 /* TODO: Help propose a standard fb.h ioctl to report mmap damage */
770 if (cmd == DLFB_IOCTL_REPORT_DAMAGE) {
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700771
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700772 /*
773 * If we have a damage-aware client, turn fb_defio "off"
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300774 * To avoid perf imact of unnecessary page fault handling.
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700775 * Done by resetting the delay for this fb_info to a very
776 * long period. Pages will become writable and stay that way.
777 * Reset to normal value when all clients have closed this fb.
778 */
779 if (info->fbdefio)
780 info->fbdefio->delay = DL_DEFIO_WRITE_DISABLE;
781
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700782 area = (struct dloarea *)arg;
783
784 if (area->x < 0)
785 area->x = 0;
786
787 if (area->x > info->var.xres)
788 area->x = info->var.xres;
789
790 if (area->y < 0)
791 area->y = 0;
792
793 if (area->y > info->var.yres)
794 area->y = info->var.yres;
795
Bernie Thompson530f43a2010-02-15 06:46:21 -0800796 dlfb_handle_damage(dev, area->x, area->y, area->w, area->h,
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700797 info->screen_base);
798 }
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700799
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700800 return 0;
801}
802
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700803/* taken from vesafb */
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700804static int
Bernie Thompson45742032010-02-15 06:46:04 -0800805dlfb_ops_setcolreg(unsigned regno, unsigned red, unsigned green,
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700806 unsigned blue, unsigned transp, struct fb_info *info)
807{
808 int err = 0;
809
810 if (regno >= info->cmap.len)
811 return 1;
812
813 if (regno < 16) {
814 if (info->var.red.offset == 10) {
815 /* 1:5:5:5 */
816 ((u32 *) (info->pseudo_palette))[regno] =
817 ((red & 0xf800) >> 1) |
818 ((green & 0xf800) >> 6) | ((blue & 0xf800) >> 11);
819 } else {
820 /* 0:5:6:5 */
821 ((u32 *) (info->pseudo_palette))[regno] =
822 ((red & 0xf800)) |
823 ((green & 0xfc00) >> 5) | ((blue & 0xf800) >> 11);
824 }
825 }
826
827 return err;
828}
829
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800830/*
831 * It's common for several clients to have framebuffer open simultaneously.
832 * e.g. both fbcon and X. Makes things interesting.
Bernie Thompson33077b82010-09-05 16:35:19 -0700833 * Assumes caller is holding info->lock (for open and release at least)
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800834 */
835static int dlfb_ops_open(struct fb_info *info, int user)
836{
837 struct dlfb_data *dev = info->par;
838
Bernie Thompsond5ed5432010-09-05 16:35:39 -0700839 /*
840 * fbcon aggressively connects to first framebuffer it finds,
841 * preventing other clients (X) from working properly. Usually
842 * not what the user wants. Fail by default with option to enable.
843 */
844 if ((user == 0) & (!console))
845 return -EBUSY;
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800846
Bernie Thompson33077b82010-09-05 16:35:19 -0700847 /* If the USB device is gone, we don't accept new opens */
848 if (dev->virtualized)
849 return -ENODEV;
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800850
851 dev->fb_count++;
852
Bernie Thompson33077b82010-09-05 16:35:19 -0700853 kref_get(&dev->kref);
854
Bernie Thompsond5ed5432010-09-05 16:35:39 -0700855 if (fb_defio && (info->fbdefio == NULL)) {
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700856 /* enable defio at last moment if not disabled by client */
857
858 struct fb_deferred_io *fbdefio;
859
Joe Perches31a9f472010-10-31 15:33:55 -0700860 fbdefio = kmalloc(sizeof(struct fb_deferred_io), GFP_KERNEL);
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700861
862 if (fbdefio) {
863 fbdefio->delay = DL_DEFIO_WRITE_DELAY;
864 fbdefio->deferred_io = dlfb_dpy_deferred_io;
865 }
866
867 info->fbdefio = fbdefio;
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800868 fb_deferred_io_init(info);
869 }
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800870
Paul Mundt81f6f3c2011-01-06 18:07:54 +0900871 pr_notice("open /dev/fb%d user=%d fb_info=%p count=%d\n",
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800872 info->node, user, info, dev->fb_count);
873
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700874 return 0;
875}
876
Bernie Thompson4a4854d2010-02-15 06:45:55 -0800877/*
878 * Called when all client interfaces to start transactions have been disabled,
879 * and all references to our device instance (dlfb_data) are released.
880 * Every transaction must have a reference, so we know are fully spun down
881 */
Bernie Thompson33077b82010-09-05 16:35:19 -0700882static void dlfb_free(struct kref *kref)
Bernie Thompson4a4854d2010-02-15 06:45:55 -0800883{
884 struct dlfb_data *dev = container_of(kref, struct dlfb_data, kref);
885
Bernie Thompson33077b82010-09-05 16:35:19 -0700886 /* this function will wait for all in-flight urbs to complete */
887 if (dev->urbs.count > 0)
888 dlfb_free_urb_list(dev);
889
Bernie Thompson4a4854d2010-02-15 06:45:55 -0800890 if (dev->backing_buffer)
891 vfree(dev->backing_buffer);
892
Bernie Thompson33077b82010-09-05 16:35:19 -0700893 kfree(dev->edid);
894
Paul Mundt81f6f3c2011-01-06 18:07:54 +0900895 pr_warn("freeing dlfb_data %p\n", dev);
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800896
Bernie Thompson4a4854d2010-02-15 06:45:55 -0800897 kfree(dev);
898}
899
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700900static void dlfb_release_urb_work(struct work_struct *work)
901{
902 struct urb_node *unode = container_of(work, struct urb_node,
903 release_urb_work.work);
904
905 up(&unode->dev->urbs.limit_sem);
906}
Bernie Thompson33077b82010-09-05 16:35:19 -0700907
908static void dlfb_free_framebuffer_work(struct work_struct *work)
Bernie Thompson2469d5d2010-02-15 06:46:13 -0800909{
Bernie Thompson33077b82010-09-05 16:35:19 -0700910 struct dlfb_data *dev = container_of(work, struct dlfb_data,
911 free_framebuffer_work.work);
912 struct fb_info *info = dev->info;
913 int node = info->node;
914
915 unregister_framebuffer(info);
Bernie Thompson2469d5d2010-02-15 06:46:13 -0800916
917 if (info->cmap.len != 0)
918 fb_dealloc_cmap(&info->cmap);
919 if (info->monspecs.modedb)
920 fb_destroy_modedb(info->monspecs.modedb);
921 if (info->screen_base)
922 vfree(info->screen_base);
923
924 fb_destroy_modelist(&info->modelist);
925
Bernie Thompson33077b82010-09-05 16:35:19 -0700926 dev->info = 0;
927
928 /* Assume info structure is freed after this point */
Bernie Thompson2469d5d2010-02-15 06:46:13 -0800929 framebuffer_release(info);
930
Paul Mundt81f6f3c2011-01-06 18:07:54 +0900931 pr_warn("fb_info for /dev/fb%d has been freed\n", node);
Bernie Thompson33077b82010-09-05 16:35:19 -0700932
933 /* ref taken in probe() as part of registering framebfufer */
934 kref_put(&dev->kref, dlfb_free);
935}
936
937/*
938 * Assumes caller is holding info->lock mutex (for open and release at least)
939 */
940static int dlfb_ops_release(struct fb_info *info, int user)
941{
942 struct dlfb_data *dev = info->par;
943
944 dev->fb_count--;
945
946 /* We can't free fb_info here - fbmem will touch it when we return */
947 if (dev->virtualized && (dev->fb_count == 0))
948 schedule_delayed_work(&dev->free_framebuffer_work, HZ);
949
Bernie Thompson33077b82010-09-05 16:35:19 -0700950 if ((dev->fb_count == 0) && (info->fbdefio)) {
951 fb_deferred_io_cleanup(info);
952 kfree(info->fbdefio);
953 info->fbdefio = NULL;
954 info->fbops->fb_mmap = dlfb_ops_mmap;
955 }
Bernie Thompson33077b82010-09-05 16:35:19 -0700956
Paul Mundt81f6f3c2011-01-06 18:07:54 +0900957 pr_warn("released /dev/fb%d user=%d count=%d\n",
Bernie Thompson33077b82010-09-05 16:35:19 -0700958 info->node, user, dev->fb_count);
959
960 kref_put(&dev->kref, dlfb_free);
961
962 return 0;
Bernie Thompson2469d5d2010-02-15 06:46:13 -0800963}
964
965/*
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800966 * Check whether a video mode is supported by the DisplayLink chip
967 * We start from monitor's modes, so don't need to filter that here
968 */
969static int dlfb_is_valid_mode(struct fb_videomode *mode,
970 struct fb_info *info)
971{
972 struct dlfb_data *dev = info->par;
973
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700974 if (mode->xres * mode->yres > dev->sku_pixel_limit) {
Paul Mundt81f6f3c2011-01-06 18:07:54 +0900975 pr_warn("%dx%d beyond chip capabilities\n",
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700976 mode->xres, mode->yres);
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800977 return 0;
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700978 }
979
Paul Mundt81f6f3c2011-01-06 18:07:54 +0900980 pr_info("%dx%d valid mode\n", mode->xres, mode->yres);
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800981
982 return 1;
983}
984
985static void dlfb_var_color_format(struct fb_var_screeninfo *var)
986{
987 const struct fb_bitfield red = { 11, 5, 0 };
988 const struct fb_bitfield green = { 5, 6, 0 };
989 const struct fb_bitfield blue = { 0, 5, 0 };
990
991 var->bits_per_pixel = 16;
992 var->red = red;
993 var->green = green;
994 var->blue = blue;
995}
996
Bernie Thompson2469d5d2010-02-15 06:46:13 -0800997static int dlfb_ops_check_var(struct fb_var_screeninfo *var,
998 struct fb_info *info)
999{
1000 struct fb_videomode mode;
1001
1002 /* TODO: support dynamically changing framebuffer size */
1003 if ((var->xres * var->yres * 2) > info->fix.smem_len)
1004 return -EINVAL;
1005
1006 /* set device-specific elements of var unrelated to mode */
1007 dlfb_var_color_format(var);
1008
1009 fb_var_to_videomode(&mode, var);
1010
1011 if (!dlfb_is_valid_mode(&mode, info))
1012 return -EINVAL;
1013
1014 return 0;
1015}
1016
1017static int dlfb_ops_set_par(struct fb_info *info)
1018{
1019 struct dlfb_data *dev = info->par;
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001020 int result;
1021 u16 *pix_framebuffer;
1022 int i;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001023
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001024 pr_notice("set_par mode %dx%d\n", info->var.xres, info->var.yres);
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001025
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001026 result = dlfb_set_video_mode(dev, &info->var);
1027
1028 if ((result == 0) && (dev->fb_count == 0)) {
1029
1030 /* paint greenscreen */
1031
1032 pix_framebuffer = (u16 *) info->screen_base;
1033 for (i = 0; i < info->fix.smem_len / 2; i++)
1034 pix_framebuffer[i] = 0x37e6;
1035
1036 dlfb_handle_damage(dev, 0, 0, info->var.xres, info->var.yres,
1037 info->screen_base);
1038 }
1039
1040 return result;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001041}
1042
Bernie Thompson9825f702010-09-05 16:35:10 -07001043/*
1044 * In order to come back from full DPMS off, we need to set the mode again
1045 */
Bernie Thompson45742032010-02-15 06:46:04 -08001046static int dlfb_ops_blank(int blank_mode, struct fb_info *info)
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -07001047{
Bernie Thompson530f43a2010-02-15 06:46:21 -08001048 struct dlfb_data *dev = info->par;
Roberto De Ioris7316bc52009-06-10 23:02:19 -07001049
Roberto De Ioris7316bc52009-06-10 23:02:19 -07001050 if (blank_mode != FB_BLANK_UNBLANK) {
Bernie Thompson9825f702010-09-05 16:35:10 -07001051 char *bufptr;
1052 struct urb *urb;
Roberto De Ioris7316bc52009-06-10 23:02:19 -07001053
Bernie Thompson9825f702010-09-05 16:35:10 -07001054 urb = dlfb_get_urb(dev);
1055 if (!urb)
1056 return 0;
1057
1058 bufptr = (char *) urb->transfer_buffer;
1059 bufptr = dlfb_vidreg_lock(bufptr);
1060 bufptr = dlfb_enable_hvsync(bufptr, false);
1061 bufptr = dlfb_vidreg_unlock(bufptr);
1062
1063 dlfb_submit_urb(dev, urb, bufptr -
1064 (char *) urb->transfer_buffer);
1065 } else {
1066 dlfb_set_video_mode(dev, &info->var);
1067 }
Roberto De Ioris7316bc52009-06-10 23:02:19 -07001068
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001069 return 0;
1070}
1071
1072static struct fb_ops dlfb_ops = {
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001073 .owner = THIS_MODULE,
Paul Mundt1a3e5282011-01-06 17:29:24 +09001074 .fb_read = fb_sys_read,
Bernie Thompsond46ecb92010-09-05 16:35:04 -07001075 .fb_write = dlfb_ops_write,
Bernie Thompson45742032010-02-15 06:46:04 -08001076 .fb_setcolreg = dlfb_ops_setcolreg,
1077 .fb_fillrect = dlfb_ops_fillrect,
1078 .fb_copyarea = dlfb_ops_copyarea,
1079 .fb_imageblit = dlfb_ops_imageblit,
1080 .fb_mmap = dlfb_ops_mmap,
1081 .fb_ioctl = dlfb_ops_ioctl,
Bernie Thompson3e8f3d62010-02-15 06:46:26 -08001082 .fb_open = dlfb_ops_open,
Bernie Thompson45742032010-02-15 06:46:04 -08001083 .fb_release = dlfb_ops_release,
1084 .fb_blank = dlfb_ops_blank,
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001085 .fb_check_var = dlfb_ops_check_var,
1086 .fb_set_par = dlfb_ops_set_par,
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001087};
1088
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001089
Bernie Thompsoncc403dc2010-02-15 06:45:49 -08001090/*
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001091 * Assumes &info->lock held by caller
1092 * Assumes no active clients have framebuffer open
1093 */
1094static int dlfb_realloc_framebuffer(struct dlfb_data *dev, struct fb_info *info)
1095{
1096 int retval = -ENOMEM;
1097 int old_len = info->fix.smem_len;
1098 int new_len;
1099 unsigned char *old_fb = info->screen_base;
1100 unsigned char *new_fb;
1101 unsigned char *new_back;
1102
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001103 pr_warn("Reallocating framebuffer. Addresses will change!\n");
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001104
1105 new_len = info->fix.line_length * info->var.yres;
1106
1107 if (PAGE_ALIGN(new_len) > old_len) {
1108 /*
1109 * Alloc system memory for virtual framebuffer
1110 */
1111 new_fb = vmalloc(new_len);
1112 if (!new_fb) {
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001113 pr_err("Virtual framebuffer alloc failed\n");
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001114 goto error;
1115 }
1116
1117 if (info->screen_base) {
1118 memcpy(new_fb, old_fb, old_len);
1119 vfree(info->screen_base);
1120 }
1121
1122 info->screen_base = new_fb;
1123 info->fix.smem_len = PAGE_ALIGN(new_len);
1124 info->fix.smem_start = (unsigned long) new_fb;
1125 info->flags = udlfb_info_flags;
1126
1127 /*
1128 * Second framebuffer copy to mirror the framebuffer state
1129 * on the physical USB device. We can function without this.
1130 * But with imperfect damage info we may send pixels over USB
1131 * that were, in fact, unchanged - wasting limited USB bandwidth
1132 */
Joe Perches5b84cc72010-11-04 20:07:59 -07001133 new_back = vzalloc(new_len);
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001134 if (!new_back)
Linus Torvalds949f6712011-01-10 16:04:53 -08001135 pr_info("No shadow/backing buffer allocated\n");
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001136 else {
1137 if (dev->backing_buffer)
1138 vfree(dev->backing_buffer);
1139 dev->backing_buffer = new_back;
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001140 }
1141 }
1142
1143 retval = 0;
1144
1145error:
1146 return retval;
1147}
1148
1149/*
1150 * 1) Get EDID from hw, or use sw default
1151 * 2) Parse into various fb_info structs
1152 * 3) Allocate virtual framebuffer memory to back highest res mode
1153 *
1154 * Parses EDID into three places used by various parts of fbdev:
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001155 * fb_var_screeninfo contains the timing of the monitor's preferred mode
1156 * fb_info.monspecs is full parsed EDID info, including monspecs.modedb
1157 * fb_info.modelist is a linked list of all monitor & VESA modes which work
1158 *
1159 * If EDID is not readable/valid, then modelist is all VESA modes,
1160 * monspecs is NULL, and fb_var_screeninfo is set to safe VESA mode
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001161 * Returns 0 if successful
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001162 */
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001163static int dlfb_setup_modes(struct dlfb_data *dev,
1164 struct fb_info *info,
1165 char *default_edid, size_t default_edid_size)
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001166{
1167 int i;
1168 const struct fb_videomode *default_vmode = NULL;
1169 int result = 0;
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001170 char *edid;
1171 int tries = 3;
1172
1173 if (info->dev) /* only use mutex if info has been registered */
1174 mutex_lock(&info->lock);
1175
Paul Mundtb9f03a32011-01-06 18:04:02 +09001176 edid = kmalloc(EDID_LENGTH, GFP_KERNEL);
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001177 if (!edid) {
1178 result = -ENOMEM;
1179 goto error;
1180 }
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001181
1182 fb_destroy_modelist(&info->modelist);
1183 memset(&info->monspecs, 0, sizeof(info->monspecs));
1184
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001185 /*
1186 * Try to (re)read EDID from hardware first
1187 * EDID data may return, but not parse as valid
1188 * Try again a few times, in case of e.g. analog cable noise
1189 */
1190 while (tries--) {
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001191
Paul Mundtb9f03a32011-01-06 18:04:02 +09001192 i = dlfb_get_edid(dev, edid, EDID_LENGTH);
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001193
Paul Mundtb9f03a32011-01-06 18:04:02 +09001194 if (i >= EDID_LENGTH)
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001195 fb_edid_to_monspecs(edid, &info->monspecs);
1196
1197 if (info->monspecs.modedb_len > 0) {
1198 dev->edid = edid;
1199 dev->edid_size = i;
1200 break;
1201 }
1202 }
1203
1204 /* If that fails, use a previously returned EDID if available */
1205 if (info->monspecs.modedb_len == 0) {
1206
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001207 pr_err("Unable to get valid EDID from device/display\n");
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001208
1209 if (dev->edid) {
1210 fb_edid_to_monspecs(dev->edid, &info->monspecs);
1211 if (info->monspecs.modedb_len > 0)
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001212 pr_err("Using previously queried EDID\n");
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001213 }
1214 }
1215
1216 /* If that fails, use the default EDID we were handed */
1217 if (info->monspecs.modedb_len == 0) {
Paul Mundtb9f03a32011-01-06 18:04:02 +09001218 if (default_edid_size >= EDID_LENGTH) {
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001219 fb_edid_to_monspecs(default_edid, &info->monspecs);
1220 if (info->monspecs.modedb_len > 0) {
1221 memcpy(edid, default_edid, default_edid_size);
1222 dev->edid = edid;
1223 dev->edid_size = default_edid_size;
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001224 pr_err("Using default/backup EDID\n");
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001225 }
1226 }
1227 }
1228
1229 /* If we've got modes, let's pick a best default mode */
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001230 if (info->monspecs.modedb_len > 0) {
1231
1232 for (i = 0; i < info->monspecs.modedb_len; i++) {
1233 if (dlfb_is_valid_mode(&info->monspecs.modedb[i], info))
1234 fb_add_videomode(&info->monspecs.modedb[i],
1235 &info->modelist);
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001236 else /* if we've removed top/best mode */
1237 info->monspecs.misc &= ~FB_MISC_1ST_DETAIL;
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001238 }
1239
1240 default_vmode = fb_find_best_display(&info->monspecs,
1241 &info->modelist);
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001242 }
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001243
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001244 /* If everything else has failed, fall back to safe default mode */
1245 if (default_vmode == NULL) {
1246
1247 struct fb_videomode fb_vmode = {0};
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001248
1249 /*
1250 * Add the standard VESA modes to our modelist
1251 * Since we don't have EDID, there may be modes that
1252 * overspec monitor and/or are incorrect aspect ratio, etc.
1253 * But at least the user has a chance to choose
1254 */
1255 for (i = 0; i < VESA_MODEDB_SIZE; i++) {
1256 if (dlfb_is_valid_mode((struct fb_videomode *)
1257 &vesa_modes[i], info))
1258 fb_add_videomode(&vesa_modes[i],
1259 &info->modelist);
1260 }
1261
1262 /*
1263 * default to resolution safe for projectors
1264 * (since they are most common case without EDID)
1265 */
1266 fb_vmode.xres = 800;
1267 fb_vmode.yres = 600;
1268 fb_vmode.refresh = 60;
1269 default_vmode = fb_find_nearest_mode(&fb_vmode,
1270 &info->modelist);
1271 }
1272
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001273 /* If we have good mode and no active clients*/
1274 if ((default_vmode != NULL) && (dev->fb_count == 0)) {
1275
1276 fb_videomode_to_var(&info->var, default_vmode);
1277 dlfb_var_color_format(&info->var);
1278
1279 /*
1280 * with mode size info, we can now alloc our framebuffer.
1281 */
1282 memcpy(&info->fix, &dlfb_fix, sizeof(dlfb_fix));
1283 info->fix.line_length = info->var.xres *
1284 (info->var.bits_per_pixel / 8);
1285
1286 result = dlfb_realloc_framebuffer(dev, info);
1287
1288 } else
1289 result = -EINVAL;
1290
1291error:
1292 if (edid && (dev->edid != edid))
1293 kfree(edid);
1294
1295 if (info->dev)
1296 mutex_unlock(&info->lock);
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001297
1298 return result;
1299}
1300
1301static ssize_t metrics_bytes_rendered_show(struct device *fbdev,
1302 struct device_attribute *a, char *buf) {
1303 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1304 struct dlfb_data *dev = fb_info->par;
1305 return snprintf(buf, PAGE_SIZE, "%u\n",
1306 atomic_read(&dev->bytes_rendered));
1307}
1308
1309static ssize_t metrics_bytes_identical_show(struct device *fbdev,
1310 struct device_attribute *a, char *buf) {
1311 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1312 struct dlfb_data *dev = fb_info->par;
1313 return snprintf(buf, PAGE_SIZE, "%u\n",
1314 atomic_read(&dev->bytes_identical));
1315}
1316
1317static ssize_t metrics_bytes_sent_show(struct device *fbdev,
1318 struct device_attribute *a, char *buf) {
1319 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1320 struct dlfb_data *dev = fb_info->par;
1321 return snprintf(buf, PAGE_SIZE, "%u\n",
1322 atomic_read(&dev->bytes_sent));
1323}
1324
1325static ssize_t metrics_cpu_kcycles_used_show(struct device *fbdev,
1326 struct device_attribute *a, char *buf) {
1327 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1328 struct dlfb_data *dev = fb_info->par;
1329 return snprintf(buf, PAGE_SIZE, "%u\n",
1330 atomic_read(&dev->cpu_kcycles_used));
1331}
1332
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001333static ssize_t edid_show(
1334 struct file *filp,
1335 struct kobject *kobj, struct bin_attribute *a,
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001336 char *buf, loff_t off, size_t count) {
1337 struct device *fbdev = container_of(kobj, struct device, kobj);
1338 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1339 struct dlfb_data *dev = fb_info->par;
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001340
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001341 if (dev->edid == NULL)
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001342 return 0;
1343
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001344 if ((off >= dev->edid_size) || (count > dev->edid_size))
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001345 return 0;
1346
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001347 if (off + count > dev->edid_size)
1348 count = dev->edid_size - off;
1349
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001350 pr_info("sysfs edid copy %p to %p, %d bytes\n",
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001351 dev->edid, buf, (int) count);
1352
1353 memcpy(buf, dev->edid, count);
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001354
1355 return count;
1356}
1357
Bernie Thompson8ef8cc42010-09-05 16:35:31 -07001358static ssize_t edid_store(
1359 struct file *filp,
1360 struct kobject *kobj, struct bin_attribute *a,
1361 char *src, loff_t src_off, size_t src_size) {
1362 struct device *fbdev = container_of(kobj, struct device, kobj);
1363 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1364 struct dlfb_data *dev = fb_info->par;
1365
1366 /* We only support write of entire EDID at once, no offset*/
Paul Mundtb9f03a32011-01-06 18:04:02 +09001367 if ((src_size != EDID_LENGTH) || (src_off != 0))
Bernie Thompson8ef8cc42010-09-05 16:35:31 -07001368 return 0;
1369
1370 dlfb_setup_modes(dev, fb_info, src, src_size);
1371
1372 if (dev->edid && (memcmp(src, dev->edid, src_size) == 0)) {
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001373 pr_info("sysfs written EDID is new default\n");
Bernie Thompson8ef8cc42010-09-05 16:35:31 -07001374 dlfb_ops_set_par(fb_info);
1375 return src_size;
1376 } else
1377 return 0;
1378}
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001379
1380static ssize_t metrics_reset_store(struct device *fbdev,
1381 struct device_attribute *attr,
1382 const char *buf, size_t count)
1383{
1384 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1385 struct dlfb_data *dev = fb_info->par;
1386
1387 atomic_set(&dev->bytes_rendered, 0);
1388 atomic_set(&dev->bytes_identical, 0);
1389 atomic_set(&dev->bytes_sent, 0);
1390 atomic_set(&dev->cpu_kcycles_used, 0);
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001391
1392 return count;
1393}
1394
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001395static struct bin_attribute edid_attr = {
1396 .attr.name = "edid",
Bernie Thompson8ef8cc42010-09-05 16:35:31 -07001397 .attr.mode = 0666,
Paul Mundtb9f03a32011-01-06 18:04:02 +09001398 .size = EDID_LENGTH,
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001399 .read = edid_show,
Bernie Thompson8ef8cc42010-09-05 16:35:31 -07001400 .write = edid_store
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001401};
1402
1403static struct device_attribute fb_device_attrs[] = {
1404 __ATTR_RO(metrics_bytes_rendered),
1405 __ATTR_RO(metrics_bytes_identical),
1406 __ATTR_RO(metrics_bytes_sent),
1407 __ATTR_RO(metrics_cpu_kcycles_used),
Greg Kroah-Hartman926c1112010-11-18 11:21:04 -08001408 __ATTR(metrics_reset, S_IWUSR, NULL, metrics_reset_store),
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001409};
1410
1411/*
Bernie Thompsoncc403dc2010-02-15 06:45:49 -08001412 * This is necessary before we can communicate with the display controller.
1413 */
1414static int dlfb_select_std_channel(struct dlfb_data *dev)
1415{
1416 int ret;
1417 u8 set_def_chn[] = { 0x57, 0xCD, 0xDC, 0xA7,
1418 0x1C, 0x88, 0x5E, 0x15,
1419 0x60, 0xFE, 0xC6, 0x97,
1420 0x16, 0x3D, 0x47, 0xF2 };
1421
1422 ret = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
1423 NR_USB_REQUEST_CHANNEL,
1424 (USB_DIR_OUT | USB_TYPE_VENDOR), 0, 0,
1425 set_def_chn, sizeof(set_def_chn), USB_CTRL_SET_TIMEOUT);
1426 return ret;
1427}
1428
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001429static int dlfb_parse_vendor_descriptor(struct dlfb_data *dev,
1430 struct usb_device *usbdev)
1431{
1432 char *desc;
1433 char *buf;
1434 char *desc_end;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001435
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001436 u8 total_len = 0;
1437
1438 buf = kzalloc(MAX_VENDOR_DESCRIPTOR_SIZE, GFP_KERNEL);
1439 if (!buf)
1440 return false;
1441 desc = buf;
1442
1443 total_len = usb_get_descriptor(usbdev, 0x5f, /* vendor specific */
1444 0, desc, MAX_VENDOR_DESCRIPTOR_SIZE);
1445 if (total_len > 5) {
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001446 pr_info("vendor descriptor length:%x data:%02x %02x %02x %02x" \
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001447 "%02x %02x %02x %02x %02x %02x %02x\n",
1448 total_len, desc[0],
1449 desc[1], desc[2], desc[3], desc[4], desc[5], desc[6],
1450 desc[7], desc[8], desc[9], desc[10]);
1451
1452 if ((desc[0] != total_len) || /* descriptor length */
1453 (desc[1] != 0x5f) || /* vendor descriptor type */
1454 (desc[2] != 0x01) || /* version (2 bytes) */
1455 (desc[3] != 0x00) ||
1456 (desc[4] != total_len - 2)) /* length after type */
1457 goto unrecognized;
1458
1459 desc_end = desc + total_len;
1460 desc += 5; /* the fixed header we've already parsed */
1461
1462 while (desc < desc_end) {
1463 u8 length;
1464 u16 key;
1465
1466 key = *((u16 *) desc);
1467 desc += sizeof(u16);
1468 length = *desc;
1469 desc++;
1470
1471 switch (key) {
1472 case 0x0200: { /* max_area */
1473 u32 max_area;
1474 max_area = le32_to_cpu(*((u32 *)desc));
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001475 pr_warn("DL chip limited to %d pixel modes\n",
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001476 max_area);
1477 dev->sku_pixel_limit = max_area;
1478 break;
1479 }
1480 default:
1481 break;
1482 }
1483 desc += length;
1484 }
1485 }
1486
1487 goto success;
1488
1489unrecognized:
1490 /* allow udlfb to load for now even if firmware unrecognized */
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001491 pr_err("Unrecognized vendor firmware descriptor\n");
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001492
1493success:
1494 kfree(buf);
1495 return true;
1496}
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001497static int dlfb_usb_probe(struct usb_interface *interface,
Bernie Thompson59277b62009-11-24 15:52:21 -08001498 const struct usb_device_id *id)
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001499{
Bernie Thompson59277b62009-11-24 15:52:21 -08001500 struct usb_device *usbdev;
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001501 struct dlfb_data *dev = 0;
Bernie Thompson33077b82010-09-05 16:35:19 -07001502 struct fb_info *info = 0;
Bernie Thompson59277b62009-11-24 15:52:21 -08001503 int retval = -ENOMEM;
Bernie Thompson2685cff2010-09-05 18:29:56 -07001504 int i;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001505
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001506 /* usb initialization */
1507
1508 usbdev = interface_to_usbdev(interface);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001509
Bernie Thompson59277b62009-11-24 15:52:21 -08001510 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1511 if (dev == NULL) {
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001512 err("dlfb_usb_probe: failed alloc of dev struct\n");
1513 goto error;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001514 }
1515
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001516 /* we need to wait for both usb and fbdev to spin down on disconnect */
1517 kref_init(&dev->kref); /* matching kref_put in usb .disconnect fn */
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001518 kref_get(&dev->kref); /* matching kref_put in free_framebuffer_work */
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001519
Bernie Thompson59277b62009-11-24 15:52:21 -08001520 dev->udev = usbdev;
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001521 dev->gdev = &usbdev->dev; /* our generic struct device * */
Bernie Thompson59277b62009-11-24 15:52:21 -08001522 usb_set_intfdata(interface, dev);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001523
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001524 pr_info("%s %s - serial #%s\n",
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001525 usbdev->manufacturer, usbdev->product, usbdev->serial);
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001526 pr_info("vid_%04x&pid_%04x&rev_%04x driver's dlfb_data struct at %p\n",
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001527 usbdev->descriptor.idVendor, usbdev->descriptor.idProduct,
1528 usbdev->descriptor.bcdDevice, dev);
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001529 pr_info("console enable=%d\n", console);
1530 pr_info("fb_defio enable=%d\n", fb_defio);
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001531
1532 dev->sku_pixel_limit = 2048 * 1152; /* default to maximum */
1533
1534 if (!dlfb_parse_vendor_descriptor(dev, usbdev)) {
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001535 pr_err("firmware not recognized. Assume incompatible device\n");
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001536 goto error;
1537 }
1538
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001539 if (!dlfb_alloc_urb_list(dev, WRITES_IN_FLIGHT, MAX_TRANSFER)) {
1540 retval = -ENOMEM;
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001541 pr_err("dlfb_alloc_urb_list failed\n");
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001542 goto error;
1543 }
1544
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001545 /* We don't register a new USB class. Our client interface is fbdev */
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001546
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001547 /* allocates framebuffer driver structure, not framebuffer memory */
1548 info = framebuffer_alloc(0, &usbdev->dev);
1549 if (!info) {
1550 retval = -ENOMEM;
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001551 pr_err("framebuffer_alloc failed\n");
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001552 goto error;
1553 }
Bernie Thompson33077b82010-09-05 16:35:19 -07001554
Bernie Thompson59277b62009-11-24 15:52:21 -08001555 dev->info = info;
1556 info->par = dev;
1557 info->pseudo_palette = dev->pseudo_palette;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001558 info->fbops = &dlfb_ops;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001559
Bernie Thompson59277b62009-11-24 15:52:21 -08001560 retval = fb_alloc_cmap(&info->cmap, 256, 0);
1561 if (retval < 0) {
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001562 pr_err("fb_alloc_cmap failed %x\n", retval);
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001563 goto error;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001564 }
1565
Bernie Thompson33077b82010-09-05 16:35:19 -07001566 INIT_DELAYED_WORK(&dev->free_framebuffer_work,
1567 dlfb_free_framebuffer_work);
1568
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001569 INIT_LIST_HEAD(&info->modelist);
1570
1571 retval = dlfb_setup_modes(dev, info, NULL, 0);
1572 if (retval != 0) {
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001573 pr_err("unable to find common mode for display and adapter\n");
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001574 goto error;
1575 }
1576
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001577 /* ready to begin using device */
1578
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001579 atomic_set(&dev->usb_active, 1);
Bernie Thompson59277b62009-11-24 15:52:21 -08001580 dlfb_select_std_channel(dev);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001581
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001582 dlfb_ops_check_var(&info->var, info);
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001583 dlfb_ops_set_par(info);
1584
Bernie Thompson59277b62009-11-24 15:52:21 -08001585 retval = register_framebuffer(info);
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001586 if (retval < 0) {
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001587 pr_err("register_framebuffer failed %d\n", retval);
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001588 goto error;
1589 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001590
Liu Yuan94cd1ae2011-04-18 10:44:38 +00001591 for (i = 0; i < ARRAY_SIZE(fb_device_attrs); i++) {
1592 retval = device_create_file(info->dev, &fb_device_attrs[i]);
1593 if (retval) {
1594 pr_err("device_create_file failed %d\n", retval);
1595 goto err_del_attrs;
1596 }
1597 }
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -07001598
Liu Yuan94cd1ae2011-04-18 10:44:38 +00001599 retval = device_create_bin_file(info->dev, &edid_attr);
1600 if (retval) {
1601 pr_err("device_create_bin_file failed %d\n", retval);
1602 goto err_del_attrs;
1603 }
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001604
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001605 pr_info("DisplayLink USB device /dev/fb%d attached. %dx%d resolution."
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001606 " Using %dK framebuffer memory\n", info->node,
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001607 info->var.xres, info->var.yres,
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001608 ((dev->backing_buffer) ?
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001609 info->fix.smem_len * 2 : info->fix.smem_len) >> 10);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001610 return 0;
1611
Liu Yuan94cd1ae2011-04-18 10:44:38 +00001612err_del_attrs:
1613 for (i -= 1; i >= 0; i--)
1614 device_remove_file(info->dev, &fb_device_attrs[i]);
1615
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001616error:
1617 if (dev) {
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001618
Bernie Thompson33077b82010-09-05 16:35:19 -07001619 if (info) {
1620 if (info->cmap.len != 0)
1621 fb_dealloc_cmap(&info->cmap);
1622 if (info->monspecs.modedb)
1623 fb_destroy_modedb(info->monspecs.modedb);
1624 if (info->screen_base)
1625 vfree(info->screen_base);
1626
1627 fb_destroy_modelist(&info->modelist);
1628
1629 framebuffer_release(info);
1630 }
1631
1632 if (dev->backing_buffer)
1633 vfree(dev->backing_buffer);
1634
1635 kref_put(&dev->kref, dlfb_free); /* ref for framebuffer */
1636 kref_put(&dev->kref, dlfb_free); /* last ref from kref_init */
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001637
1638 /* dev has been deallocated. Do not dereference */
1639 }
1640
Bernie Thompson59277b62009-11-24 15:52:21 -08001641 return retval;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001642}
1643
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001644static void dlfb_usb_disconnect(struct usb_interface *interface)
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001645{
Bernie Thompson59277b62009-11-24 15:52:21 -08001646 struct dlfb_data *dev;
1647 struct fb_info *info;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001648 int i;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001649
Bernie Thompson59277b62009-11-24 15:52:21 -08001650 dev = usb_get_intfdata(interface);
Bernie Thompson59277b62009-11-24 15:52:21 -08001651 info = dev->info;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001652
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001653 pr_info("USB disconnect starting\n");
Bernie Thompson33077b82010-09-05 16:35:19 -07001654
1655 /* we virtualize until all fb clients release. Then we free */
1656 dev->virtualized = true;
1657
1658 /* When non-active we'll update virtual framebuffer, but no new urbs */
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001659 atomic_set(&dev->usb_active, 0);
1660
Bernie Thompson33077b82010-09-05 16:35:19 -07001661 /* remove udlfb's sysfs interfaces */
1662 for (i = 0; i < ARRAY_SIZE(fb_device_attrs); i++)
1663 device_remove_file(info->dev, &fb_device_attrs[i]);
1664 device_remove_bin_file(info->dev, &edid_attr);
1665
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001666 usb_set_intfdata(interface, NULL);
1667
Bernie Thompson33077b82010-09-05 16:35:19 -07001668 /* if clients still have us open, will be freed on last close */
1669 if (dev->fb_count == 0)
1670 schedule_delayed_work(&dev->free_framebuffer_work, 0);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001671
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001672 /* release reference taken by kref_init in probe() */
Bernie Thompson33077b82010-09-05 16:35:19 -07001673 kref_put(&dev->kref, dlfb_free);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001674
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001675 /* consider dlfb_data freed */
1676
1677 return;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001678}
1679
1680static struct usb_driver dlfb_driver = {
1681 .name = "udlfb",
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001682 .probe = dlfb_usb_probe,
1683 .disconnect = dlfb_usb_disconnect,
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001684 .id_table = id_table,
1685};
1686
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001687static int __init dlfb_module_init(void)
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001688{
1689 int res;
1690
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001691 res = usb_register(&dlfb_driver);
1692 if (res)
1693 err("usb_register failed. Error number %d", res);
1694
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001695 return res;
1696}
1697
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001698static void __exit dlfb_module_exit(void)
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001699{
1700 usb_deregister(&dlfb_driver);
1701}
1702
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001703module_init(dlfb_module_init);
1704module_exit(dlfb_module_exit);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001705
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001706static void dlfb_urb_completion(struct urb *urb)
1707{
1708 struct urb_node *unode = urb->context;
1709 struct dlfb_data *dev = unode->dev;
1710 unsigned long flags;
1711
1712 /* sync/async unlink faults aren't errors */
1713 if (urb->status) {
1714 if (!(urb->status == -ENOENT ||
1715 urb->status == -ECONNRESET ||
1716 urb->status == -ESHUTDOWN)) {
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001717 pr_err("%s - nonzero write bulk status received: %d\n",
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001718 __func__, urb->status);
1719 atomic_set(&dev->lost_pixels, 1);
1720 }
1721 }
1722
1723 urb->transfer_buffer_length = dev->urbs.size; /* reset to actual */
1724
1725 spin_lock_irqsave(&dev->urbs.lock, flags);
1726 list_add_tail(&unode->entry, &dev->urbs.list);
1727 dev->urbs.available++;
1728 spin_unlock_irqrestore(&dev->urbs.lock, flags);
1729
Bernie Thompson5bea1fb2010-09-05 18:28:29 -07001730 /*
1731 * When using fb_defio, we deadlock if up() is called
1732 * while another is waiting. So queue to another process.
1733 */
1734 if (fb_defio)
1735 schedule_delayed_work(&unode->release_urb_work, 0);
1736 else
1737 up(&dev->urbs.limit_sem);
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001738}
1739
1740static void dlfb_free_urb_list(struct dlfb_data *dev)
1741{
1742 int count = dev->urbs.count;
1743 struct list_head *node;
1744 struct urb_node *unode;
1745 struct urb *urb;
1746 int ret;
1747 unsigned long flags;
1748
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001749 pr_notice("Waiting for completes and freeing all render urbs\n");
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001750
1751 /* keep waiting and freeing, until we've got 'em all */
1752 while (count--) {
Bernie Thompson33077b82010-09-05 16:35:19 -07001753
1754 /* Getting interrupted means a leak, but ok at shutdown*/
1755 ret = down_interruptible(&dev->urbs.limit_sem);
1756 if (ret)
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001757 break;
Bernie Thompson33077b82010-09-05 16:35:19 -07001758
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001759 spin_lock_irqsave(&dev->urbs.lock, flags);
1760
1761 node = dev->urbs.list.next; /* have reserved one with sem */
1762 list_del_init(node);
1763
1764 spin_unlock_irqrestore(&dev->urbs.lock, flags);
1765
1766 unode = list_entry(node, struct urb_node, entry);
1767 urb = unode->urb;
1768
1769 /* Free each separately allocated piece */
Greg Kroah-Hartmanc220cc32010-04-29 15:36:29 -07001770 usb_free_coherent(urb->dev, dev->urbs.size,
1771 urb->transfer_buffer, urb->transfer_dma);
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001772 usb_free_urb(urb);
1773 kfree(node);
1774 }
1775
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001776}
1777
1778static int dlfb_alloc_urb_list(struct dlfb_data *dev, int count, size_t size)
1779{
1780 int i = 0;
1781 struct urb *urb;
1782 struct urb_node *unode;
1783 char *buf;
1784
1785 spin_lock_init(&dev->urbs.lock);
1786
1787 dev->urbs.size = size;
1788 INIT_LIST_HEAD(&dev->urbs.list);
1789
1790 while (i < count) {
1791 unode = kzalloc(sizeof(struct urb_node), GFP_KERNEL);
1792 if (!unode)
1793 break;
1794 unode->dev = dev;
1795
Bernie Thompson5bea1fb2010-09-05 18:28:29 -07001796 INIT_DELAYED_WORK(&unode->release_urb_work,
1797 dlfb_release_urb_work);
1798
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001799 urb = usb_alloc_urb(0, GFP_KERNEL);
1800 if (!urb) {
1801 kfree(unode);
1802 break;
1803 }
1804 unode->urb = urb;
1805
Greg Kroah-Hartmanc220cc32010-04-29 15:36:29 -07001806 buf = usb_alloc_coherent(dev->udev, MAX_TRANSFER, GFP_KERNEL,
1807 &urb->transfer_dma);
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001808 if (!buf) {
1809 kfree(unode);
1810 usb_free_urb(urb);
1811 break;
1812 }
1813
1814 /* urb->transfer_buffer_length set to actual before submit */
1815 usb_fill_bulk_urb(urb, dev->udev, usb_sndbulkpipe(dev->udev, 1),
1816 buf, size, dlfb_urb_completion, unode);
1817 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1818
1819 list_add_tail(&unode->entry, &dev->urbs.list);
1820
1821 i++;
1822 }
1823
1824 sema_init(&dev->urbs.limit_sem, i);
1825 dev->urbs.count = i;
1826 dev->urbs.available = i;
1827
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001828 pr_notice("allocated %d %d byte urbs\n", i, (int) size);
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001829
1830 return i;
1831}
1832
1833static struct urb *dlfb_get_urb(struct dlfb_data *dev)
1834{
1835 int ret = 0;
1836 struct list_head *entry;
1837 struct urb_node *unode;
1838 struct urb *urb = NULL;
1839 unsigned long flags;
1840
1841 /* Wait for an in-flight buffer to complete and get re-queued */
1842 ret = down_timeout(&dev->urbs.limit_sem, GET_URB_TIMEOUT);
1843 if (ret) {
1844 atomic_set(&dev->lost_pixels, 1);
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001845 pr_warn("wait for urb interrupted: %x available: %d\n",
Bernie Thompson5bea1fb2010-09-05 18:28:29 -07001846 ret, dev->urbs.available);
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001847 goto error;
1848 }
1849
1850 spin_lock_irqsave(&dev->urbs.lock, flags);
1851
1852 BUG_ON(list_empty(&dev->urbs.list)); /* reserved one with limit_sem */
1853 entry = dev->urbs.list.next;
1854 list_del_init(entry);
1855 dev->urbs.available--;
1856
1857 spin_unlock_irqrestore(&dev->urbs.lock, flags);
1858
1859 unode = list_entry(entry, struct urb_node, entry);
1860 urb = unode->urb;
1861
1862error:
1863 return urb;
1864}
1865
1866static int dlfb_submit_urb(struct dlfb_data *dev, struct urb *urb, size_t len)
1867{
1868 int ret;
1869
1870 BUG_ON(len > dev->urbs.size);
1871
1872 urb->transfer_buffer_length = len; /* set to actual payload len */
1873 ret = usb_submit_urb(urb, GFP_KERNEL);
1874 if (ret) {
1875 dlfb_urb_completion(urb); /* because no one else will */
1876 atomic_set(&dev->lost_pixels, 1);
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001877 pr_err("usb_submit_urb error %x\n", ret);
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001878 }
1879 return ret;
1880}
1881
Bernie Thompsond5ed5432010-09-05 16:35:39 -07001882module_param(console, bool, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
1883MODULE_PARM_DESC(console, "Allow fbcon to consume first framebuffer found");
1884
1885module_param(fb_defio, bool, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
1886MODULE_PARM_DESC(fb_defio, "Enable fb_defio mmap support. *Experimental*");
1887
Bernie Thompson59277b62009-11-24 15:52:21 -08001888MODULE_AUTHOR("Roberto De Ioris <roberto@unbit.it>, "
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001889 "Jaya Kumar <jayakumar.lkml@gmail.com>, "
1890 "Bernie Thompson <bernie@plugable.com>");
1891MODULE_DESCRIPTION("DisplayLink kernel framebuffer driver");
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001892MODULE_LICENSE("GPL");
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001893