blob: 530f1598de4e284c052b84b7502ad48acde34d2d [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
19#include <linux/module.h>
20#include <linux/kernel.h>
21#include <linux/init.h>
22#include <linux/usb.h>
23#include <linux/uaccess.h>
24#include <linux/mm.h>
25#include <linux/fb.h>
Amit Kucheriafb299002009-07-27 12:01:03 +030026#include <linux/vmalloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Bernie Thompson33077b82010-09-05 16:35:19 -070028#include <linux/delay.h>
Paul Mundt96f8d862010-11-16 14:00:24 +090029#include <video/udlfb.h>
Paul Mundtb9f03a32011-01-06 18:04:02 +090030#include "edid.h"
Roberto De Ioris88e58b12009-06-03 14:03:06 -070031
Bernie Thompson59277b62009-11-24 15:52:21 -080032static struct fb_fix_screeninfo dlfb_fix = {
Bernie Thompson2469d5d2010-02-15 06:46:13 -080033 .id = "udlfb",
Bernie Thompson1d31a9e2010-02-15 06:45:43 -080034 .type = FB_TYPE_PACKED_PIXELS,
35 .visual = FB_VISUAL_TRUECOLOR,
36 .xpanstep = 0,
37 .ypanstep = 0,
38 .ywrapstep = 0,
39 .accel = FB_ACCEL_NONE,
Bernie Thompson59277b62009-11-24 15:52:21 -080040};
Roberto De Ioris88e58b12009-06-03 14:03:06 -070041
Bernie Thompson2469d5d2010-02-15 06:46:13 -080042static const u32 udlfb_info_flags = FBINFO_DEFAULT | FBINFO_READS_FAST |
Bernie Thompson2469d5d2010-02-15 06:46:13 -080043 FBINFO_VIRTFB |
Bernie Thompson2469d5d2010-02-15 06:46:13 -080044 FBINFO_HWACCEL_IMAGEBLIT | FBINFO_HWACCEL_FILLRECT |
45 FBINFO_HWACCEL_COPYAREA | FBINFO_MISC_ALWAYS_SETPAR;
46
Bernie Thompsoncc403dc2010-02-15 06:45:49 -080047/*
48 * There are many DisplayLink-based products, all with unique PIDs. We are able
49 * to support all volume ones (circa 2009) with a single driver, so we match
50 * globally on VID. TODO: Probe() needs to detect when we might be running
51 * "future" chips, and bail on those, so a compatible driver can match.
52 */
53static struct usb_device_id id_table[] = {
54 {.idVendor = 0x17e9, .match_flags = USB_DEVICE_ID_MATCH_VENDOR,},
55 {},
56};
57MODULE_DEVICE_TABLE(usb, id_table);
Bernie Thompson59277b62009-11-24 15:52:21 -080058
Bernie Thompsond5ed5432010-09-05 16:35:39 -070059/* module options */
60static int console; /* Optionally allow fbcon to consume first framebuffer */
61static int fb_defio; /* Optionally enable experimental fb_defio mmap support */
Bernie Thompsondd8015f2010-02-15 06:46:35 -080062
Bernie Thompson4a4854d2010-02-15 06:45:55 -080063/* dlfb keeps a list of urbs for efficient bulk transfers */
64static void dlfb_urb_completion(struct urb *urb);
65static struct urb *dlfb_get_urb(struct dlfb_data *dev);
66static int dlfb_submit_urb(struct dlfb_data *dev, struct urb * urb, size_t len);
67static int dlfb_alloc_urb_list(struct dlfb_data *dev, int count, size_t size);
68static void dlfb_free_urb_list(struct dlfb_data *dev);
69
Bernie Thompson59277b62009-11-24 15:52:21 -080070/*
Bernie Thompsonbd808162010-02-15 06:46:48 -080071 * All DisplayLink bulk operations start with 0xAF, followed by specific code
72 * All operations are written to buffers which then later get sent to device
Bernie Thompson59277b62009-11-24 15:52:21 -080073 */
Bernie Thompson45742032010-02-15 06:46:04 -080074static char *dlfb_set_register(char *buf, u8 reg, u8 val)
Roberto De Ioris88e58b12009-06-03 14:03:06 -070075{
Bernie Thompson1d31a9e2010-02-15 06:45:43 -080076 *buf++ = 0xAF;
77 *buf++ = 0x20;
78 *buf++ = reg;
79 *buf++ = val;
80 return buf;
Roberto De Ioris88e58b12009-06-03 14:03:06 -070081}
82
Bernie Thompson45742032010-02-15 06:46:04 -080083static char *dlfb_vidreg_lock(char *buf)
Roberto De Ioris88e58b12009-06-03 14:03:06 -070084{
Bernie Thompson45742032010-02-15 06:46:04 -080085 return dlfb_set_register(buf, 0xFF, 0x00);
Bernie Thompson59277b62009-11-24 15:52:21 -080086}
Roberto De Ioris88e58b12009-06-03 14:03:06 -070087
Bernie Thompson45742032010-02-15 06:46:04 -080088static char *dlfb_vidreg_unlock(char *buf)
Bernie Thompson59277b62009-11-24 15:52:21 -080089{
Bernie Thompson45742032010-02-15 06:46:04 -080090 return dlfb_set_register(buf, 0xFF, 0xFF);
Bernie Thompson59277b62009-11-24 15:52:21 -080091}
Roberto De Ioris88e58b12009-06-03 14:03:06 -070092
Bernie Thompson59277b62009-11-24 15:52:21 -080093/*
Bernie Thompson530f43a2010-02-15 06:46:21 -080094 * On/Off for driving the DisplayLink framebuffer to the display
Bernie Thompson9825f702010-09-05 16:35:10 -070095 * 0x00 H and V sync on
96 * 0x01 H and V sync off (screen blank but powered)
97 * 0x07 DPMS powerdown (requires modeset to come back)
Bernie Thompson59277b62009-11-24 15:52:21 -080098 */
Bernie Thompson530f43a2010-02-15 06:46:21 -080099static char *dlfb_enable_hvsync(char *buf, bool enable)
Bernie Thompson59277b62009-11-24 15:52:21 -0800100{
Bernie Thompson530f43a2010-02-15 06:46:21 -0800101 if (enable)
102 return dlfb_set_register(buf, 0x1F, 0x00);
103 else
Bernie Thompson9825f702010-09-05 16:35:10 -0700104 return dlfb_set_register(buf, 0x1F, 0x07);
Bernie Thompson59277b62009-11-24 15:52:21 -0800105}
106
Bernie Thompson45742032010-02-15 06:46:04 -0800107static char *dlfb_set_color_depth(char *buf, u8 selection)
Bernie Thompson59277b62009-11-24 15:52:21 -0800108{
Bernie Thompson45742032010-02-15 06:46:04 -0800109 return dlfb_set_register(buf, 0x00, selection);
Bernie Thompson59277b62009-11-24 15:52:21 -0800110}
111
Bernie Thompson45742032010-02-15 06:46:04 -0800112static char *dlfb_set_base16bpp(char *wrptr, u32 base)
Bernie Thompson59277b62009-11-24 15:52:21 -0800113{
Bernie Thompson1d31a9e2010-02-15 06:45:43 -0800114 /* the base pointer is 16 bits wide, 0x20 is hi byte. */
Bernie Thompson45742032010-02-15 06:46:04 -0800115 wrptr = dlfb_set_register(wrptr, 0x20, base >> 16);
116 wrptr = dlfb_set_register(wrptr, 0x21, base >> 8);
117 return dlfb_set_register(wrptr, 0x22, base);
Bernie Thompson59277b62009-11-24 15:52:21 -0800118}
119
Bernie Thompsonbd808162010-02-15 06:46:48 -0800120/*
121 * DisplayLink HW has separate 16bpp and 8bpp framebuffers.
122 * In 24bpp modes, the low 323 RGB bits go in the 8bpp framebuffer
123 */
Bernie Thompson45742032010-02-15 06:46:04 -0800124static char *dlfb_set_base8bpp(char *wrptr, u32 base)
Bernie Thompson59277b62009-11-24 15:52:21 -0800125{
Bernie Thompson45742032010-02-15 06:46:04 -0800126 wrptr = dlfb_set_register(wrptr, 0x26, base >> 16);
127 wrptr = dlfb_set_register(wrptr, 0x27, base >> 8);
128 return dlfb_set_register(wrptr, 0x28, base);
Bernie Thompson59277b62009-11-24 15:52:21 -0800129}
130
Bernie Thompson45742032010-02-15 06:46:04 -0800131static char *dlfb_set_register_16(char *wrptr, u8 reg, u16 value)
Bernie Thompson59277b62009-11-24 15:52:21 -0800132{
Bernie Thompson45742032010-02-15 06:46:04 -0800133 wrptr = dlfb_set_register(wrptr, reg, value >> 8);
134 return dlfb_set_register(wrptr, reg+1, value);
Bernie Thompson59277b62009-11-24 15:52:21 -0800135}
136
137/*
138 * This is kind of weird because the controller takes some
139 * register values in a different byte order than other registers.
140 */
Bernie Thompson45742032010-02-15 06:46:04 -0800141static char *dlfb_set_register_16be(char *wrptr, u8 reg, u16 value)
Bernie Thompson59277b62009-11-24 15:52:21 -0800142{
Bernie Thompson45742032010-02-15 06:46:04 -0800143 wrptr = dlfb_set_register(wrptr, reg, value);
144 return dlfb_set_register(wrptr, reg+1, value >> 8);
Bernie Thompson59277b62009-11-24 15:52:21 -0800145}
146
147/*
148 * LFSR is linear feedback shift register. The reason we have this is
149 * because the display controller needs to minimize the clock depth of
150 * various counters used in the display path. So this code reverses the
151 * provided value into the lfsr16 value by counting backwards to get
152 * the value that needs to be set in the hardware comparator to get the
153 * same actual count. This makes sense once you read above a couple of
154 * times and think about it from a hardware perspective.
155 */
Bernie Thompsonbd808162010-02-15 06:46:48 -0800156static u16 dlfb_lfsr16(u16 actual_count)
Bernie Thompson59277b62009-11-24 15:52:21 -0800157{
158 u32 lv = 0xFFFF; /* This is the lfsr value that the hw starts with */
159
160 while (actual_count--) {
161 lv = ((lv << 1) |
162 (((lv >> 15) ^ (lv >> 4) ^ (lv >> 2) ^ (lv >> 1)) & 1))
163 & 0xFFFF;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700164 }
Bernie Thompson59277b62009-11-24 15:52:21 -0800165
166 return (u16) lv;
167}
168
169/*
170 * This does LFSR conversion on the value that is to be written.
171 * See LFSR explanation above for more detail.
172 */
Bernie Thompson45742032010-02-15 06:46:04 -0800173static char *dlfb_set_register_lfsr16(char *wrptr, u8 reg, u16 value)
Bernie Thompson59277b62009-11-24 15:52:21 -0800174{
Bernie Thompsonbd808162010-02-15 06:46:48 -0800175 return dlfb_set_register_16(wrptr, reg, dlfb_lfsr16(value));
Bernie Thompson59277b62009-11-24 15:52:21 -0800176}
177
178/*
179 * This takes a standard fbdev screeninfo struct and all of its monitor mode
180 * details and converts them into the DisplayLink equivalent register commands.
181 */
Bernie Thompson45742032010-02-15 06:46:04 -0800182static char *dlfb_set_vid_cmds(char *wrptr, struct fb_var_screeninfo *var)
Bernie Thompson59277b62009-11-24 15:52:21 -0800183{
184 u16 xds, yds;
185 u16 xde, yde;
186 u16 yec;
187
Bernie Thompson59277b62009-11-24 15:52:21 -0800188 /* x display start */
189 xds = var->left_margin + var->hsync_len;
Bernie Thompson45742032010-02-15 06:46:04 -0800190 wrptr = dlfb_set_register_lfsr16(wrptr, 0x01, xds);
Bernie Thompson59277b62009-11-24 15:52:21 -0800191 /* x display end */
192 xde = xds + var->xres;
Bernie Thompson45742032010-02-15 06:46:04 -0800193 wrptr = dlfb_set_register_lfsr16(wrptr, 0x03, xde);
Bernie Thompson59277b62009-11-24 15:52:21 -0800194
195 /* y display start */
196 yds = var->upper_margin + var->vsync_len;
Bernie Thompson45742032010-02-15 06:46:04 -0800197 wrptr = dlfb_set_register_lfsr16(wrptr, 0x05, yds);
Bernie Thompson59277b62009-11-24 15:52:21 -0800198 /* y display end */
199 yde = yds + var->yres;
Bernie Thompson45742032010-02-15 06:46:04 -0800200 wrptr = dlfb_set_register_lfsr16(wrptr, 0x07, yde);
Bernie Thompson59277b62009-11-24 15:52:21 -0800201
202 /* x end count is active + blanking - 1 */
Bernie Thompson45742032010-02-15 06:46:04 -0800203 wrptr = dlfb_set_register_lfsr16(wrptr, 0x09,
204 xde + var->right_margin - 1);
Bernie Thompson59277b62009-11-24 15:52:21 -0800205
206 /* libdlo hardcodes hsync start to 1 */
Bernie Thompson45742032010-02-15 06:46:04 -0800207 wrptr = dlfb_set_register_lfsr16(wrptr, 0x0B, 1);
Bernie Thompson59277b62009-11-24 15:52:21 -0800208
209 /* hsync end is width of sync pulse + 1 */
Bernie Thompson45742032010-02-15 06:46:04 -0800210 wrptr = dlfb_set_register_lfsr16(wrptr, 0x0D, var->hsync_len + 1);
Bernie Thompson59277b62009-11-24 15:52:21 -0800211
212 /* hpixels is active pixels */
Bernie Thompson45742032010-02-15 06:46:04 -0800213 wrptr = dlfb_set_register_16(wrptr, 0x0F, var->xres);
Bernie Thompson59277b62009-11-24 15:52:21 -0800214
215 /* yendcount is vertical active + vertical blanking */
216 yec = var->yres + var->upper_margin + var->lower_margin +
217 var->vsync_len;
Bernie Thompson45742032010-02-15 06:46:04 -0800218 wrptr = dlfb_set_register_lfsr16(wrptr, 0x11, yec);
Bernie Thompson59277b62009-11-24 15:52:21 -0800219
220 /* libdlo hardcodes vsync start to 0 */
Bernie Thompson45742032010-02-15 06:46:04 -0800221 wrptr = dlfb_set_register_lfsr16(wrptr, 0x13, 0);
Bernie Thompson59277b62009-11-24 15:52:21 -0800222
223 /* vsync end is width of vsync pulse */
Bernie Thompson45742032010-02-15 06:46:04 -0800224 wrptr = dlfb_set_register_lfsr16(wrptr, 0x15, var->vsync_len);
Bernie Thompson59277b62009-11-24 15:52:21 -0800225
226 /* vpixels is active pixels */
Bernie Thompson45742032010-02-15 06:46:04 -0800227 wrptr = dlfb_set_register_16(wrptr, 0x17, var->yres);
Bernie Thompson59277b62009-11-24 15:52:21 -0800228
229 /* convert picoseconds to 5kHz multiple for pclk5k = x * 1E12/5k */
Bernie Thompson45742032010-02-15 06:46:04 -0800230 wrptr = dlfb_set_register_16be(wrptr, 0x1B,
231 200*1000*1000/var->pixclock);
Bernie Thompson59277b62009-11-24 15:52:21 -0800232
233 return wrptr;
234}
235
236/*
237 * This takes a standard fbdev screeninfo struct that was fetched or prepared
238 * and then generates the appropriate command sequence that then drives the
239 * display controller.
240 */
241static int dlfb_set_video_mode(struct dlfb_data *dev,
242 struct fb_var_screeninfo *var)
243{
244 char *buf;
245 char *wrptr;
246 int retval = 0;
247 int writesize;
Bernie Thompson530f43a2010-02-15 06:46:21 -0800248 struct urb *urb;
Bernie Thompson59277b62009-11-24 15:52:21 -0800249
Bernie Thompson530f43a2010-02-15 06:46:21 -0800250 if (!atomic_read(&dev->usb_active))
251 return -EPERM;
252
253 urb = dlfb_get_urb(dev);
254 if (!urb)
255 return -ENOMEM;
Bernie Thompson2685cff2010-09-05 18:29:56 -0700256
Bernie Thompson530f43a2010-02-15 06:46:21 -0800257 buf = (char *) urb->transfer_buffer;
Bernie Thompson59277b62009-11-24 15:52:21 -0800258
259 /*
260 * This first section has to do with setting the base address on the
261 * controller * associated with the display. There are 2 base
262 * pointers, currently, we only * use the 16 bpp segment.
263 */
Bernie Thompson45742032010-02-15 06:46:04 -0800264 wrptr = dlfb_vidreg_lock(buf);
265 wrptr = dlfb_set_color_depth(wrptr, 0x00);
Bernie Thompson59277b62009-11-24 15:52:21 -0800266 /* set base for 16bpp segment to 0 */
Bernie Thompson45742032010-02-15 06:46:04 -0800267 wrptr = dlfb_set_base16bpp(wrptr, 0);
Bernie Thompson59277b62009-11-24 15:52:21 -0800268 /* set base for 8bpp segment to end of fb */
Bernie Thompson45742032010-02-15 06:46:04 -0800269 wrptr = dlfb_set_base8bpp(wrptr, dev->info->fix.smem_len);
Bernie Thompson59277b62009-11-24 15:52:21 -0800270
Bernie Thompson45742032010-02-15 06:46:04 -0800271 wrptr = dlfb_set_vid_cmds(wrptr, var);
Bernie Thompson530f43a2010-02-15 06:46:21 -0800272 wrptr = dlfb_enable_hvsync(wrptr, true);
Bernie Thompson45742032010-02-15 06:46:04 -0800273 wrptr = dlfb_vidreg_unlock(wrptr);
Bernie Thompson59277b62009-11-24 15:52:21 -0800274
275 writesize = wrptr - buf;
276
Bernie Thompson530f43a2010-02-15 06:46:21 -0800277 retval = dlfb_submit_urb(dev, urb, writesize);
Bernie Thompson59277b62009-11-24 15:52:21 -0800278
Bernie Thompson59277b62009-11-24 15:52:21 -0800279 return retval;
280}
281
Bernie Thompson45742032010-02-15 06:46:04 -0800282static int dlfb_ops_mmap(struct fb_info *info, struct vm_area_struct *vma)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700283{
284 unsigned long start = vma->vm_start;
285 unsigned long size = vma->vm_end - vma->vm_start;
286 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
287 unsigned long page, pos;
288
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700289 if (offset + size > info->fix.smem_len)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700290 return -EINVAL;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700291
292 pos = (unsigned long)info->fix.smem_start + offset;
293
Bernie Thompson2685cff2010-09-05 18:29:56 -0700294 dl_notice("mmap() framebuffer addr:%lu size:%lu\n",
295 pos, size);
296
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700297 while (size > 0) {
298 page = vmalloc_to_pfn((void *)pos);
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700299 if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED))
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700300 return -EAGAIN;
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700301
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700302 start += PAGE_SIZE;
303 pos += PAGE_SIZE;
304 if (size > PAGE_SIZE)
305 size -= PAGE_SIZE;
306 else
307 size = 0;
308 }
309
310 vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
311 return 0;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700312}
313
Bernie Thompson530f43a2010-02-15 06:46:21 -0800314/*
315 * Trims identical data from front and back of line
316 * Sets new front buffer address and width
317 * And returns byte count of identical pixels
318 * Assumes CPU natural alignment (unsigned long)
319 * for back and front buffer ptrs and width
320 */
321static int dlfb_trim_hline(const u8 *bback, const u8 **bfront, int *width_bytes)
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700322{
Bernie Thompson530f43a2010-02-15 06:46:21 -0800323 int j, k;
324 const unsigned long *back = (const unsigned long *) bback;
325 const unsigned long *front = (const unsigned long *) *bfront;
326 const int width = *width_bytes / sizeof(unsigned long);
327 int identical = width;
328 int start = width;
329 int end = width;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700330
Bernie Thompson530f43a2010-02-15 06:46:21 -0800331 prefetch((void *) front);
332 prefetch((void *) back);
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700333
Bernie Thompson530f43a2010-02-15 06:46:21 -0800334 for (j = 0; j < width; j++) {
335 if (back[j] != front[j]) {
336 start = j;
337 break;
338 }
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700339 }
340
Bernie Thompson530f43a2010-02-15 06:46:21 -0800341 for (k = width - 1; k > j; k--) {
342 if (back[k] != front[k]) {
343 end = k+1;
344 break;
345 }
346 }
347
348 identical = start + (width - end);
349 *bfront = (u8 *) &front[start];
350 *width_bytes = (end - start) * sizeof(unsigned long);
351
352 return identical * sizeof(unsigned long);
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700353}
354
355/*
Pavel Machek3b7b31f2010-04-03 07:00:37 +0200356 * Render a command stream for an encoded horizontal line segment of pixels.
357 *
358 * A command buffer holds several commands.
359 * It always begins with a fresh command header
360 * (the protocol doesn't require this, but we enforce it to allow
361 * multiple buffers to be potentially encoded and sent in parallel).
362 * A single command encodes one contiguous horizontal line of pixels
363 *
364 * The function relies on the client to do all allocation, so that
365 * rendering can be done directly to output buffers (e.g. USB URBs).
366 * The function fills the supplied command buffer, providing information
367 * on where it left off, so the client may call in again with additional
368 * buffers if the line will take several buffers to complete.
369 *
370 * A single command can transmit a maximum of 256 pixels,
371 * regardless of the compression ratio (protocol design limit).
372 * To the hardware, 0 for a size byte means 256
Bernie Thompson2685cff2010-09-05 18:29:56 -0700373 *
Pavel Machek3b7b31f2010-04-03 07:00:37 +0200374 * Rather than 256 pixel commands which are either rl or raw encoded,
375 * the rlx command simply assumes alternating raw and rl spans within one cmd.
376 * This has a slightly larger header overhead, but produces more even results.
377 * It also processes all data (read and write) in a single pass.
378 * Performance benchmarks of common cases show it having just slightly better
Bernie Thompson2685cff2010-09-05 18:29:56 -0700379 * compression than 256 pixel raw or rle commands, with similar CPU consumpion.
Pavel Machek3b7b31f2010-04-03 07:00:37 +0200380 * But for very rl friendly data, will compress not quite as well.
381 */
Bernie Thompson530f43a2010-02-15 06:46:21 -0800382static void dlfb_compress_hline(
383 const uint16_t **pixel_start_ptr,
384 const uint16_t *const pixel_end,
385 uint32_t *device_address_ptr,
386 uint8_t **command_buffer_ptr,
387 const uint8_t *const cmd_buffer_end)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700388{
Bernie Thompson530f43a2010-02-15 06:46:21 -0800389 const uint16_t *pixel = *pixel_start_ptr;
390 uint32_t dev_addr = *device_address_ptr;
391 uint8_t *cmd = *command_buffer_ptr;
392 const int bpp = 2;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700393
Bernie Thompson530f43a2010-02-15 06:46:21 -0800394 while ((pixel_end > pixel) &&
395 (cmd_buffer_end - MIN_RLX_CMD_BYTES > cmd)) {
396 uint8_t *raw_pixels_count_byte = 0;
397 uint8_t *cmd_pixels_count_byte = 0;
398 const uint16_t *raw_pixel_start = 0;
399 const uint16_t *cmd_pixel_start, *cmd_pixel_end = 0;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700400
Bernie Thompson530f43a2010-02-15 06:46:21 -0800401 prefetchw((void *) cmd); /* pull in one cache line at least */
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700402
Bernie Thompson530f43a2010-02-15 06:46:21 -0800403 *cmd++ = 0xAF;
404 *cmd++ = 0x6B;
Bernie Thompson1572f912010-09-05 16:35:27 -0700405 *cmd++ = (uint8_t) ((dev_addr >> 16) & 0xFF);
406 *cmd++ = (uint8_t) ((dev_addr >> 8) & 0xFF);
407 *cmd++ = (uint8_t) ((dev_addr) & 0xFF);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700408
Bernie Thompson530f43a2010-02-15 06:46:21 -0800409 cmd_pixels_count_byte = cmd++; /* we'll know this later */
410 cmd_pixel_start = pixel;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700411
Bernie Thompson530f43a2010-02-15 06:46:21 -0800412 raw_pixels_count_byte = cmd++; /* we'll know this later */
413 raw_pixel_start = pixel;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700414
Bernie Thompson530f43a2010-02-15 06:46:21 -0800415 cmd_pixel_end = pixel + min(MAX_CMD_PIXELS + 1,
416 min((int)(pixel_end - pixel),
417 (int)(cmd_buffer_end - cmd) / bpp));
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700418
Bernie Thompson530f43a2010-02-15 06:46:21 -0800419 prefetch_range((void *) pixel, (cmd_pixel_end - pixel) * bpp);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700420
Bernie Thompson530f43a2010-02-15 06:46:21 -0800421 while (pixel < cmd_pixel_end) {
422 const uint16_t * const repeating_pixel = pixel;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700423
Bernie Thompson530f43a2010-02-15 06:46:21 -0800424 *(uint16_t *)cmd = cpu_to_be16p(pixel);
425 cmd += 2;
426 pixel++;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700427
Bernie Thompson530f43a2010-02-15 06:46:21 -0800428 if (unlikely((pixel < cmd_pixel_end) &&
429 (*pixel == *repeating_pixel))) {
430 /* go back and fill in raw pixel count */
431 *raw_pixels_count_byte = ((repeating_pixel -
432 raw_pixel_start) + 1) & 0xFF;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700433
Bernie Thompson530f43a2010-02-15 06:46:21 -0800434 while ((pixel < cmd_pixel_end)
435 && (*pixel == *repeating_pixel)) {
436 pixel++;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700437 }
Bernie Thompson59277b62009-11-24 15:52:21 -0800438
Bernie Thompson530f43a2010-02-15 06:46:21 -0800439 /* immediately after raw data is repeat byte */
440 *cmd++ = ((pixel - repeating_pixel) - 1) & 0xFF;
Bernie Thompson59277b62009-11-24 15:52:21 -0800441
Bernie Thompson530f43a2010-02-15 06:46:21 -0800442 /* Then start another raw pixel span */
443 raw_pixel_start = pixel;
444 raw_pixels_count_byte = cmd++;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700445 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700446 }
447
Bernie Thompson530f43a2010-02-15 06:46:21 -0800448 if (pixel > raw_pixel_start) {
449 /* finalize last RAW span */
450 *raw_pixels_count_byte = (pixel-raw_pixel_start) & 0xFF;
451 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700452
Bernie Thompson530f43a2010-02-15 06:46:21 -0800453 *cmd_pixels_count_byte = (pixel - cmd_pixel_start) & 0xFF;
454 dev_addr += (pixel - cmd_pixel_start) * bpp;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700455 }
456
Bernie Thompson530f43a2010-02-15 06:46:21 -0800457 if (cmd_buffer_end <= MIN_RLX_CMD_BYTES + cmd) {
458 /* Fill leftover bytes with no-ops */
459 if (cmd_buffer_end > cmd)
460 memset(cmd, 0xAF, cmd_buffer_end - cmd);
461 cmd = (uint8_t *) cmd_buffer_end;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700462 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700463
Bernie Thompson530f43a2010-02-15 06:46:21 -0800464 *command_buffer_ptr = cmd;
465 *pixel_start_ptr = pixel;
466 *device_address_ptr = dev_addr;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700467
Bernie Thompson530f43a2010-02-15 06:46:21 -0800468 return;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700469}
470
Bernie Thompson530f43a2010-02-15 06:46:21 -0800471/*
472 * There are 3 copies of every pixel: The front buffer that the fbdev
473 * client renders to, the actual framebuffer across the USB bus in hardware
474 * (that we can only write to, slowly, and can never read), and (optionally)
475 * our shadow copy that tracks what's been sent to that hardware buffer.
476 */
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700477static int dlfb_render_hline(struct dlfb_data *dev, struct urb **urb_ptr,
Bernie Thompson530f43a2010-02-15 06:46:21 -0800478 const char *front, char **urb_buf_ptr,
479 u32 byte_offset, u32 byte_width,
480 int *ident_ptr, int *sent_ptr)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700481{
Bernie Thompson530f43a2010-02-15 06:46:21 -0800482 const u8 *line_start, *line_end, *next_pixel;
483 u32 dev_addr = dev->base16 + byte_offset;
484 struct urb *urb = *urb_ptr;
485 u8 *cmd = *urb_buf_ptr;
486 u8 *cmd_end = (u8 *) urb->transfer_buffer + urb->transfer_buffer_length;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700487
Bernie Thompson530f43a2010-02-15 06:46:21 -0800488 line_start = (u8 *) (front + byte_offset);
489 next_pixel = line_start;
490 line_end = next_pixel + byte_width;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700491
Bernie Thompson530f43a2010-02-15 06:46:21 -0800492 if (dev->backing_buffer) {
493 int offset;
494 const u8 *back_start = (u8 *) (dev->backing_buffer
495 + byte_offset);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700496
Bernie Thompson530f43a2010-02-15 06:46:21 -0800497 *ident_ptr += dlfb_trim_hline(back_start, &next_pixel,
498 &byte_width);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700499
Bernie Thompson530f43a2010-02-15 06:46:21 -0800500 offset = next_pixel - line_start;
501 line_end = next_pixel + byte_width;
502 dev_addr += offset;
503 back_start += offset;
504 line_start += offset;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700505
Bernie Thompson530f43a2010-02-15 06:46:21 -0800506 memcpy((char *)back_start, (char *) line_start,
507 byte_width);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700508 }
509
Bernie Thompson530f43a2010-02-15 06:46:21 -0800510 while (next_pixel < line_end) {
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700511
Bernie Thompson530f43a2010-02-15 06:46:21 -0800512 dlfb_compress_hline((const uint16_t **) &next_pixel,
513 (const uint16_t *) line_end, &dev_addr,
514 (u8 **) &cmd, (u8 *) cmd_end);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700515
Bernie Thompson530f43a2010-02-15 06:46:21 -0800516 if (cmd >= cmd_end) {
517 int len = cmd - (u8 *) urb->transfer_buffer;
518 if (dlfb_submit_urb(dev, urb, len))
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700519 return 1; /* lost pixels is set */
Bernie Thompson530f43a2010-02-15 06:46:21 -0800520 *sent_ptr += len;
521 urb = dlfb_get_urb(dev);
522 if (!urb)
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700523 return 1; /* lost_pixels is set */
Bernie Thompson530f43a2010-02-15 06:46:21 -0800524 *urb_ptr = urb;
525 cmd = urb->transfer_buffer;
526 cmd_end = &cmd[urb->transfer_buffer_length];
527 }
528 }
529
530 *urb_buf_ptr = cmd;
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700531
532 return 0;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700533}
534
Bernie Thompson530f43a2010-02-15 06:46:21 -0800535int dlfb_handle_damage(struct dlfb_data *dev, int x, int y,
536 int width, int height, char *data)
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700537{
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700538 int i, ret;
Bernie Thompson530f43a2010-02-15 06:46:21 -0800539 char *cmd;
540 cycles_t start_cycles, end_cycles;
541 int bytes_sent = 0;
542 int bytes_identical = 0;
543 struct urb *urb;
544 int aligned_x;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700545
Bernie Thompson530f43a2010-02-15 06:46:21 -0800546 start_cycles = get_cycles();
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700547
Bernie Thompson530f43a2010-02-15 06:46:21 -0800548 aligned_x = DL_ALIGN_DOWN(x, sizeof(unsigned long));
549 width = DL_ALIGN_UP(width + (x-aligned_x), sizeof(unsigned long));
550 x = aligned_x;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700551
Bernie Thompson530f43a2010-02-15 06:46:21 -0800552 if ((width <= 0) ||
553 (x + width > dev->info->var.xres) ||
554 (y + height > dev->info->var.yres))
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700555 return -EINVAL;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700556
Bernie Thompson530f43a2010-02-15 06:46:21 -0800557 if (!atomic_read(&dev->usb_active))
558 return 0;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700559
Bernie Thompson530f43a2010-02-15 06:46:21 -0800560 urb = dlfb_get_urb(dev);
561 if (!urb)
562 return 0;
563 cmd = urb->transfer_buffer;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700564
Bernie Thompson530f43a2010-02-15 06:46:21 -0800565 for (i = y; i < y + height ; i++) {
566 const int line_offset = dev->info->fix.line_length * i;
567 const int byte_offset = line_offset + (x * BPP);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700568
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700569 if (dlfb_render_hline(dev, &urb,
570 (char *) dev->info->fix.smem_start,
Bernie Thompson2685cff2010-09-05 18:29:56 -0700571 &cmd, byte_offset, width * BPP,
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700572 &bytes_identical, &bytes_sent))
573 goto error;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700574 }
575
Bernie Thompson530f43a2010-02-15 06:46:21 -0800576 if (cmd > (char *) urb->transfer_buffer) {
577 /* Send partial buffer remaining before exiting */
578 int len = cmd - (char *) urb->transfer_buffer;
579 ret = dlfb_submit_urb(dev, urb, len);
580 bytes_sent += len;
581 } else
582 dlfb_urb_completion(urb);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700583
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700584error:
Bernie Thompson530f43a2010-02-15 06:46:21 -0800585 atomic_add(bytes_sent, &dev->bytes_sent);
586 atomic_add(bytes_identical, &dev->bytes_identical);
587 atomic_add(width*height*2, &dev->bytes_rendered);
588 end_cycles = get_cycles();
589 atomic_add(((unsigned int) ((end_cycles - start_cycles)
590 >> 10)), /* Kcycles */
591 &dev->cpu_kcycles_used);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700592
Bernie Thompson530f43a2010-02-15 06:46:21 -0800593 return 0;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700594}
595
Bernie Thompsond46ecb92010-09-05 16:35:04 -0700596/*
597 * Path triggered by usermode clients who write to filesystem
598 * e.g. cat filename > /dev/fb1
599 * Not used by X Windows or text-mode console. But useful for testing.
600 * Slow because of extra copy and we must assume all pixels dirty.
601 */
602static ssize_t dlfb_ops_write(struct fb_info *info, const char __user *buf,
603 size_t count, loff_t *ppos)
604{
Paul Mundt1a3e5282011-01-06 17:29:24 +0900605 ssize_t result;
Bernie Thompsond46ecb92010-09-05 16:35:04 -0700606 struct dlfb_data *dev = info->par;
607 u32 offset = (u32) *ppos;
608
Bernie Thompsond46ecb92010-09-05 16:35:04 -0700609 result = fb_sys_write(info, buf, count, ppos);
610
611 if (result > 0) {
612 int start = max((int)(offset / info->fix.line_length) - 1, 0);
613 int lines = min((u32)((result / info->fix.line_length) + 1),
614 (u32)info->var.yres);
615
616 dlfb_handle_damage(dev, 0, start, info->var.xres,
617 lines, info->screen_base);
618 }
Bernie Thompsond46ecb92010-09-05 16:35:04 -0700619
620 return result;
621}
622
Bernie Thompson530f43a2010-02-15 06:46:21 -0800623/* hardware has native COPY command (see libdlo), but not worth it for fbcon */
Bernie Thompson45742032010-02-15 06:46:04 -0800624static void dlfb_ops_copyarea(struct fb_info *info,
Bernie Thompson530f43a2010-02-15 06:46:21 -0800625 const struct fb_copyarea *area)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700626{
Bernie Thompson530f43a2010-02-15 06:46:21 -0800627
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700628 struct dlfb_data *dev = info->par;
629
Bernie Thompson530f43a2010-02-15 06:46:21 -0800630 sys_copyarea(info, area);
631
632 dlfb_handle_damage(dev, area->dx, area->dy,
633 area->width, area->height, info->screen_base);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700634}
635
Bernie Thompson45742032010-02-15 06:46:04 -0800636static void dlfb_ops_imageblit(struct fb_info *info,
Bernie Thompson530f43a2010-02-15 06:46:21 -0800637 const struct fb_image *image)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700638{
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700639 struct dlfb_data *dev = info->par;
Bernie Thompson530f43a2010-02-15 06:46:21 -0800640
Bernie Thompson530f43a2010-02-15 06:46:21 -0800641 sys_imageblit(info, image);
642
643 dlfb_handle_damage(dev, image->dx, image->dy,
644 image->width, image->height, info->screen_base);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700645}
646
Bernie Thompson45742032010-02-15 06:46:04 -0800647static void dlfb_ops_fillrect(struct fb_info *info,
Bernie Thompson530f43a2010-02-15 06:46:21 -0800648 const struct fb_fillrect *rect)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700649{
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700650 struct dlfb_data *dev = info->par;
651
Bernie Thompson530f43a2010-02-15 06:46:21 -0800652 sys_fillrect(info, rect);
653
654 dlfb_handle_damage(dev, rect->dx, rect->dy, rect->width,
655 rect->height, info->screen_base);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700656}
657
Bernie Thompsond5ed5432010-09-05 16:35:39 -0700658/*
659 * NOTE: fb_defio.c is holding info->fbdefio.mutex
660 * Touching ANY framebuffer memory that triggers a page fault
661 * in fb_defio will cause a deadlock, when it also tries to
662 * grab the same mutex.
663 */
664static void dlfb_dpy_deferred_io(struct fb_info *info,
665 struct list_head *pagelist)
666{
667 struct page *cur;
668 struct fb_deferred_io *fbdefio = info->fbdefio;
669 struct dlfb_data *dev = info->par;
670 struct urb *urb;
671 char *cmd;
672 cycles_t start_cycles, end_cycles;
673 int bytes_sent = 0;
674 int bytes_identical = 0;
675 int bytes_rendered = 0;
676
677 if (!fb_defio)
678 return;
679
680 if (!atomic_read(&dev->usb_active))
681 return;
682
683 start_cycles = get_cycles();
684
685 urb = dlfb_get_urb(dev);
686 if (!urb)
687 return;
688
689 cmd = urb->transfer_buffer;
690
691 /* walk the written page list and render each to device */
692 list_for_each_entry(cur, &fbdefio->pagelist, lru) {
693
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700694 if (dlfb_render_hline(dev, &urb, (char *) info->fix.smem_start,
Bernie Thompsond5ed5432010-09-05 16:35:39 -0700695 &cmd, cur->index << PAGE_SHIFT,
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700696 PAGE_SIZE, &bytes_identical, &bytes_sent))
697 goto error;
Bernie Thompsond5ed5432010-09-05 16:35:39 -0700698 bytes_rendered += PAGE_SIZE;
699 }
700
701 if (cmd > (char *) urb->transfer_buffer) {
702 /* Send partial buffer remaining before exiting */
703 int len = cmd - (char *) urb->transfer_buffer;
704 dlfb_submit_urb(dev, urb, len);
705 bytes_sent += len;
706 } else
707 dlfb_urb_completion(urb);
708
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700709error:
Bernie Thompsond5ed5432010-09-05 16:35:39 -0700710 atomic_add(bytes_sent, &dev->bytes_sent);
711 atomic_add(bytes_identical, &dev->bytes_identical);
712 atomic_add(bytes_rendered, &dev->bytes_rendered);
713 end_cycles = get_cycles();
714 atomic_add(((unsigned int) ((end_cycles - start_cycles)
715 >> 10)), /* Kcycles */
716 &dev->cpu_kcycles_used);
717}
718
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700719static int dlfb_get_edid(struct dlfb_data *dev, char *edid, int len)
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800720{
721 int i;
722 int ret;
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700723 char *rbuf;
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800724
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700725 rbuf = kmalloc(2, GFP_KERNEL);
726 if (!rbuf)
727 return 0;
728
729 for (i = 0; i < len; i++) {
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800730 ret = usb_control_msg(dev->udev,
731 usb_rcvctrlpipe(dev->udev, 0), (0x02),
732 (0x80 | (0x02 << 5)), i << 8, 0xA1, rbuf, 2,
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700733 HZ);
734 if (ret < 1) {
735 dl_err("Read EDID byte %d failed err %x\n", i, ret);
736 i--;
737 break;
738 }
739 edid[i] = rbuf[1];
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800740 }
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700741
742 kfree(rbuf);
743
744 return i;
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800745}
746
Bernie Thompson45742032010-02-15 06:46:04 -0800747static int dlfb_ops_ioctl(struct fb_info *info, unsigned int cmd,
Bernie Thompson530f43a2010-02-15 06:46:21 -0800748 unsigned long arg)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700749{
Bernie Thompson530f43a2010-02-15 06:46:21 -0800750
751 struct dlfb_data *dev = info->par;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700752 struct dloarea *area = NULL;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700753
Bernie Thompson530f43a2010-02-15 06:46:21 -0800754 if (!atomic_read(&dev->usb_active))
755 return 0;
756
757 /* TODO: Update X server to get this from sysfs instead */
758 if (cmd == DLFB_IOCTL_RETURN_EDID) {
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700759 char *edid = (char *)arg;
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700760 if (copy_to_user(edid, dev->edid, dev->edid_size))
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700761 return -EFAULT;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700762 return 0;
763 }
764
Bernie Thompson530f43a2010-02-15 06:46:21 -0800765 /* TODO: Help propose a standard fb.h ioctl to report mmap damage */
766 if (cmd == DLFB_IOCTL_REPORT_DAMAGE) {
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700767
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700768 /*
769 * If we have a damage-aware client, turn fb_defio "off"
770 * To avoid perf imact of unecessary page fault handling.
771 * Done by resetting the delay for this fb_info to a very
772 * long period. Pages will become writable and stay that way.
773 * Reset to normal value when all clients have closed this fb.
774 */
775 if (info->fbdefio)
776 info->fbdefio->delay = DL_DEFIO_WRITE_DISABLE;
777
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700778 area = (struct dloarea *)arg;
779
780 if (area->x < 0)
781 area->x = 0;
782
783 if (area->x > info->var.xres)
784 area->x = info->var.xres;
785
786 if (area->y < 0)
787 area->y = 0;
788
789 if (area->y > info->var.yres)
790 area->y = info->var.yres;
791
Bernie Thompson530f43a2010-02-15 06:46:21 -0800792 dlfb_handle_damage(dev, area->x, area->y, area->w, area->h,
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700793 info->screen_base);
794 }
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700795
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700796 return 0;
797}
798
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700799/* taken from vesafb */
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700800static int
Bernie Thompson45742032010-02-15 06:46:04 -0800801dlfb_ops_setcolreg(unsigned regno, unsigned red, unsigned green,
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700802 unsigned blue, unsigned transp, struct fb_info *info)
803{
804 int err = 0;
805
806 if (regno >= info->cmap.len)
807 return 1;
808
809 if (regno < 16) {
810 if (info->var.red.offset == 10) {
811 /* 1:5:5:5 */
812 ((u32 *) (info->pseudo_palette))[regno] =
813 ((red & 0xf800) >> 1) |
814 ((green & 0xf800) >> 6) | ((blue & 0xf800) >> 11);
815 } else {
816 /* 0:5:6:5 */
817 ((u32 *) (info->pseudo_palette))[regno] =
818 ((red & 0xf800)) |
819 ((green & 0xfc00) >> 5) | ((blue & 0xf800) >> 11);
820 }
821 }
822
823 return err;
824}
825
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800826/*
827 * It's common for several clients to have framebuffer open simultaneously.
828 * e.g. both fbcon and X. Makes things interesting.
Bernie Thompson33077b82010-09-05 16:35:19 -0700829 * Assumes caller is holding info->lock (for open and release at least)
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800830 */
831static int dlfb_ops_open(struct fb_info *info, int user)
832{
833 struct dlfb_data *dev = info->par;
834
Bernie Thompsond5ed5432010-09-05 16:35:39 -0700835 /*
836 * fbcon aggressively connects to first framebuffer it finds,
837 * preventing other clients (X) from working properly. Usually
838 * not what the user wants. Fail by default with option to enable.
839 */
840 if ((user == 0) & (!console))
841 return -EBUSY;
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800842
Bernie Thompson33077b82010-09-05 16:35:19 -0700843 /* If the USB device is gone, we don't accept new opens */
844 if (dev->virtualized)
845 return -ENODEV;
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800846
847 dev->fb_count++;
848
Bernie Thompson33077b82010-09-05 16:35:19 -0700849 kref_get(&dev->kref);
850
Bernie Thompsond5ed5432010-09-05 16:35:39 -0700851 if (fb_defio && (info->fbdefio == NULL)) {
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700852 /* enable defio at last moment if not disabled by client */
853
854 struct fb_deferred_io *fbdefio;
855
Joe Perches31a9f472010-10-31 15:33:55 -0700856 fbdefio = kmalloc(sizeof(struct fb_deferred_io), GFP_KERNEL);
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700857
858 if (fbdefio) {
859 fbdefio->delay = DL_DEFIO_WRITE_DELAY;
860 fbdefio->deferred_io = dlfb_dpy_deferred_io;
861 }
862
863 info->fbdefio = fbdefio;
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800864 fb_deferred_io_init(info);
865 }
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800866
867 dl_notice("open /dev/fb%d user=%d fb_info=%p count=%d\n",
868 info->node, user, info, dev->fb_count);
869
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700870 return 0;
871}
872
Bernie Thompson4a4854d2010-02-15 06:45:55 -0800873/*
874 * Called when all client interfaces to start transactions have been disabled,
875 * and all references to our device instance (dlfb_data) are released.
876 * Every transaction must have a reference, so we know are fully spun down
877 */
Bernie Thompson33077b82010-09-05 16:35:19 -0700878static void dlfb_free(struct kref *kref)
Bernie Thompson4a4854d2010-02-15 06:45:55 -0800879{
880 struct dlfb_data *dev = container_of(kref, struct dlfb_data, kref);
881
Bernie Thompson33077b82010-09-05 16:35:19 -0700882 /* this function will wait for all in-flight urbs to complete */
883 if (dev->urbs.count > 0)
884 dlfb_free_urb_list(dev);
885
Bernie Thompson4a4854d2010-02-15 06:45:55 -0800886 if (dev->backing_buffer)
887 vfree(dev->backing_buffer);
888
Bernie Thompson33077b82010-09-05 16:35:19 -0700889 kfree(dev->edid);
890
891 dl_warn("freeing dlfb_data %p\n", dev);
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800892
Bernie Thompson4a4854d2010-02-15 06:45:55 -0800893 kfree(dev);
894}
895
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700896static void dlfb_release_urb_work(struct work_struct *work)
897{
898 struct urb_node *unode = container_of(work, struct urb_node,
899 release_urb_work.work);
900
901 up(&unode->dev->urbs.limit_sem);
902}
Bernie Thompson33077b82010-09-05 16:35:19 -0700903
904static void dlfb_free_framebuffer_work(struct work_struct *work)
Bernie Thompson2469d5d2010-02-15 06:46:13 -0800905{
Bernie Thompson33077b82010-09-05 16:35:19 -0700906 struct dlfb_data *dev = container_of(work, struct dlfb_data,
907 free_framebuffer_work.work);
908 struct fb_info *info = dev->info;
909 int node = info->node;
910
911 unregister_framebuffer(info);
Bernie Thompson2469d5d2010-02-15 06:46:13 -0800912
913 if (info->cmap.len != 0)
914 fb_dealloc_cmap(&info->cmap);
915 if (info->monspecs.modedb)
916 fb_destroy_modedb(info->monspecs.modedb);
917 if (info->screen_base)
918 vfree(info->screen_base);
919
920 fb_destroy_modelist(&info->modelist);
921
Bernie Thompson33077b82010-09-05 16:35:19 -0700922 dev->info = 0;
923
924 /* Assume info structure is freed after this point */
Bernie Thompson2469d5d2010-02-15 06:46:13 -0800925 framebuffer_release(info);
926
Bernie Thompson33077b82010-09-05 16:35:19 -0700927 dl_warn("fb_info for /dev/fb%d has been freed\n", node);
928
929 /* ref taken in probe() as part of registering framebfufer */
930 kref_put(&dev->kref, dlfb_free);
931}
932
933/*
934 * Assumes caller is holding info->lock mutex (for open and release at least)
935 */
936static int dlfb_ops_release(struct fb_info *info, int user)
937{
938 struct dlfb_data *dev = info->par;
939
940 dev->fb_count--;
941
942 /* We can't free fb_info here - fbmem will touch it when we return */
943 if (dev->virtualized && (dev->fb_count == 0))
944 schedule_delayed_work(&dev->free_framebuffer_work, HZ);
945
Bernie Thompson33077b82010-09-05 16:35:19 -0700946 if ((dev->fb_count == 0) && (info->fbdefio)) {
947 fb_deferred_io_cleanup(info);
948 kfree(info->fbdefio);
949 info->fbdefio = NULL;
950 info->fbops->fb_mmap = dlfb_ops_mmap;
951 }
Bernie Thompson33077b82010-09-05 16:35:19 -0700952
953 dl_warn("released /dev/fb%d user=%d count=%d\n",
954 info->node, user, dev->fb_count);
955
956 kref_put(&dev->kref, dlfb_free);
957
958 return 0;
Bernie Thompson2469d5d2010-02-15 06:46:13 -0800959}
960
961/*
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800962 * Check whether a video mode is supported by the DisplayLink chip
963 * We start from monitor's modes, so don't need to filter that here
964 */
965static int dlfb_is_valid_mode(struct fb_videomode *mode,
966 struct fb_info *info)
967{
968 struct dlfb_data *dev = info->par;
969
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700970 if (mode->xres * mode->yres > dev->sku_pixel_limit) {
971 dl_warn("%dx%d beyond chip capabilities\n",
972 mode->xres, mode->yres);
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800973 return 0;
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700974 }
975
976 dl_info("%dx%d valid mode\n", mode->xres, mode->yres);
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800977
978 return 1;
979}
980
981static void dlfb_var_color_format(struct fb_var_screeninfo *var)
982{
983 const struct fb_bitfield red = { 11, 5, 0 };
984 const struct fb_bitfield green = { 5, 6, 0 };
985 const struct fb_bitfield blue = { 0, 5, 0 };
986
987 var->bits_per_pixel = 16;
988 var->red = red;
989 var->green = green;
990 var->blue = blue;
991}
992
Bernie Thompson2469d5d2010-02-15 06:46:13 -0800993static int dlfb_ops_check_var(struct fb_var_screeninfo *var,
994 struct fb_info *info)
995{
996 struct fb_videomode mode;
997
998 /* TODO: support dynamically changing framebuffer size */
999 if ((var->xres * var->yres * 2) > info->fix.smem_len)
1000 return -EINVAL;
1001
1002 /* set device-specific elements of var unrelated to mode */
1003 dlfb_var_color_format(var);
1004
1005 fb_var_to_videomode(&mode, var);
1006
1007 if (!dlfb_is_valid_mode(&mode, info))
1008 return -EINVAL;
1009
1010 return 0;
1011}
1012
1013static int dlfb_ops_set_par(struct fb_info *info)
1014{
1015 struct dlfb_data *dev = info->par;
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001016 int result;
1017 u16 *pix_framebuffer;
1018 int i;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001019
1020 dl_notice("set_par mode %dx%d\n", info->var.xres, info->var.yres);
1021
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001022 result = dlfb_set_video_mode(dev, &info->var);
1023
1024 if ((result == 0) && (dev->fb_count == 0)) {
1025
1026 /* paint greenscreen */
1027
1028 pix_framebuffer = (u16 *) info->screen_base;
1029 for (i = 0; i < info->fix.smem_len / 2; i++)
1030 pix_framebuffer[i] = 0x37e6;
1031
1032 dlfb_handle_damage(dev, 0, 0, info->var.xres, info->var.yres,
1033 info->screen_base);
1034 }
1035
1036 return result;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001037}
1038
Bernie Thompson9825f702010-09-05 16:35:10 -07001039/*
1040 * In order to come back from full DPMS off, we need to set the mode again
1041 */
Bernie Thompson45742032010-02-15 06:46:04 -08001042static int dlfb_ops_blank(int blank_mode, struct fb_info *info)
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -07001043{
Bernie Thompson530f43a2010-02-15 06:46:21 -08001044 struct dlfb_data *dev = info->par;
Roberto De Ioris7316bc52009-06-10 23:02:19 -07001045
Roberto De Ioris7316bc52009-06-10 23:02:19 -07001046 if (blank_mode != FB_BLANK_UNBLANK) {
Bernie Thompson9825f702010-09-05 16:35:10 -07001047 char *bufptr;
1048 struct urb *urb;
Roberto De Ioris7316bc52009-06-10 23:02:19 -07001049
Bernie Thompson9825f702010-09-05 16:35:10 -07001050 urb = dlfb_get_urb(dev);
1051 if (!urb)
1052 return 0;
1053
1054 bufptr = (char *) urb->transfer_buffer;
1055 bufptr = dlfb_vidreg_lock(bufptr);
1056 bufptr = dlfb_enable_hvsync(bufptr, false);
1057 bufptr = dlfb_vidreg_unlock(bufptr);
1058
1059 dlfb_submit_urb(dev, urb, bufptr -
1060 (char *) urb->transfer_buffer);
1061 } else {
1062 dlfb_set_video_mode(dev, &info->var);
1063 }
Roberto De Ioris7316bc52009-06-10 23:02:19 -07001064
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001065 return 0;
1066}
1067
1068static struct fb_ops dlfb_ops = {
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001069 .owner = THIS_MODULE,
Paul Mundt1a3e5282011-01-06 17:29:24 +09001070 .fb_read = fb_sys_read,
Bernie Thompsond46ecb92010-09-05 16:35:04 -07001071 .fb_write = dlfb_ops_write,
Bernie Thompson45742032010-02-15 06:46:04 -08001072 .fb_setcolreg = dlfb_ops_setcolreg,
1073 .fb_fillrect = dlfb_ops_fillrect,
1074 .fb_copyarea = dlfb_ops_copyarea,
1075 .fb_imageblit = dlfb_ops_imageblit,
1076 .fb_mmap = dlfb_ops_mmap,
1077 .fb_ioctl = dlfb_ops_ioctl,
Bernie Thompson3e8f3d62010-02-15 06:46:26 -08001078 .fb_open = dlfb_ops_open,
Bernie Thompson45742032010-02-15 06:46:04 -08001079 .fb_release = dlfb_ops_release,
1080 .fb_blank = dlfb_ops_blank,
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001081 .fb_check_var = dlfb_ops_check_var,
1082 .fb_set_par = dlfb_ops_set_par,
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001083};
1084
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001085
Bernie Thompsoncc403dc2010-02-15 06:45:49 -08001086/*
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001087 * Assumes &info->lock held by caller
1088 * Assumes no active clients have framebuffer open
1089 */
1090static int dlfb_realloc_framebuffer(struct dlfb_data *dev, struct fb_info *info)
1091{
1092 int retval = -ENOMEM;
1093 int old_len = info->fix.smem_len;
1094 int new_len;
1095 unsigned char *old_fb = info->screen_base;
1096 unsigned char *new_fb;
1097 unsigned char *new_back;
1098
1099 dl_warn("Reallocating framebuffer. Addresses will change!\n");
1100
1101 new_len = info->fix.line_length * info->var.yres;
1102
1103 if (PAGE_ALIGN(new_len) > old_len) {
1104 /*
1105 * Alloc system memory for virtual framebuffer
1106 */
1107 new_fb = vmalloc(new_len);
1108 if (!new_fb) {
1109 dl_err("Virtual framebuffer alloc failed\n");
1110 goto error;
1111 }
1112
1113 if (info->screen_base) {
1114 memcpy(new_fb, old_fb, old_len);
1115 vfree(info->screen_base);
1116 }
1117
1118 info->screen_base = new_fb;
1119 info->fix.smem_len = PAGE_ALIGN(new_len);
1120 info->fix.smem_start = (unsigned long) new_fb;
1121 info->flags = udlfb_info_flags;
1122
1123 /*
1124 * Second framebuffer copy to mirror the framebuffer state
1125 * on the physical USB device. We can function without this.
1126 * But with imperfect damage info we may send pixels over USB
1127 * that were, in fact, unchanged - wasting limited USB bandwidth
1128 */
1129 new_back = vmalloc(new_len);
1130 if (!new_back)
1131 dl_info("No shadow/backing buffer allcoated\n");
1132 else {
1133 if (dev->backing_buffer)
1134 vfree(dev->backing_buffer);
1135 dev->backing_buffer = new_back;
1136 memset(dev->backing_buffer, 0, new_len);
1137 }
1138 }
1139
1140 retval = 0;
1141
1142error:
1143 return retval;
1144}
1145
1146/*
1147 * 1) Get EDID from hw, or use sw default
1148 * 2) Parse into various fb_info structs
1149 * 3) Allocate virtual framebuffer memory to back highest res mode
1150 *
1151 * Parses EDID into three places used by various parts of fbdev:
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001152 * fb_var_screeninfo contains the timing of the monitor's preferred mode
1153 * fb_info.monspecs is full parsed EDID info, including monspecs.modedb
1154 * fb_info.modelist is a linked list of all monitor & VESA modes which work
1155 *
1156 * If EDID is not readable/valid, then modelist is all VESA modes,
1157 * monspecs is NULL, and fb_var_screeninfo is set to safe VESA mode
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001158 * Returns 0 if successful
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001159 */
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001160static int dlfb_setup_modes(struct dlfb_data *dev,
1161 struct fb_info *info,
1162 char *default_edid, size_t default_edid_size)
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001163{
1164 int i;
1165 const struct fb_videomode *default_vmode = NULL;
1166 int result = 0;
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001167 char *edid;
1168 int tries = 3;
1169
1170 if (info->dev) /* only use mutex if info has been registered */
1171 mutex_lock(&info->lock);
1172
Paul Mundtb9f03a32011-01-06 18:04:02 +09001173 edid = kmalloc(EDID_LENGTH, GFP_KERNEL);
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001174 if (!edid) {
1175 result = -ENOMEM;
1176 goto error;
1177 }
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001178
1179 fb_destroy_modelist(&info->modelist);
1180 memset(&info->monspecs, 0, sizeof(info->monspecs));
1181
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001182 /*
1183 * Try to (re)read EDID from hardware first
1184 * EDID data may return, but not parse as valid
1185 * Try again a few times, in case of e.g. analog cable noise
1186 */
1187 while (tries--) {
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001188
Paul Mundtb9f03a32011-01-06 18:04:02 +09001189 i = dlfb_get_edid(dev, edid, EDID_LENGTH);
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001190
Paul Mundtb9f03a32011-01-06 18:04:02 +09001191 if (i >= EDID_LENGTH)
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001192 fb_edid_to_monspecs(edid, &info->monspecs);
1193
1194 if (info->monspecs.modedb_len > 0) {
1195 dev->edid = edid;
1196 dev->edid_size = i;
1197 break;
1198 }
1199 }
1200
1201 /* If that fails, use a previously returned EDID if available */
1202 if (info->monspecs.modedb_len == 0) {
1203
1204 dl_err("Unable to get valid EDID from device/display\n");
1205
1206 if (dev->edid) {
1207 fb_edid_to_monspecs(dev->edid, &info->monspecs);
1208 if (info->monspecs.modedb_len > 0)
1209 dl_err("Using previously queried EDID\n");
1210 }
1211 }
1212
1213 /* If that fails, use the default EDID we were handed */
1214 if (info->monspecs.modedb_len == 0) {
Paul Mundtb9f03a32011-01-06 18:04:02 +09001215 if (default_edid_size >= EDID_LENGTH) {
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001216 fb_edid_to_monspecs(default_edid, &info->monspecs);
1217 if (info->monspecs.modedb_len > 0) {
1218 memcpy(edid, default_edid, default_edid_size);
1219 dev->edid = edid;
1220 dev->edid_size = default_edid_size;
1221 dl_err("Using default/backup EDID\n");
1222 }
1223 }
1224 }
1225
1226 /* If we've got modes, let's pick a best default mode */
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001227 if (info->monspecs.modedb_len > 0) {
1228
1229 for (i = 0; i < info->monspecs.modedb_len; i++) {
1230 if (dlfb_is_valid_mode(&info->monspecs.modedb[i], info))
1231 fb_add_videomode(&info->monspecs.modedb[i],
1232 &info->modelist);
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001233 else /* if we've removed top/best mode */
1234 info->monspecs.misc &= ~FB_MISC_1ST_DETAIL;
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001235 }
1236
1237 default_vmode = fb_find_best_display(&info->monspecs,
1238 &info->modelist);
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001239 }
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001240
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001241 /* If everything else has failed, fall back to safe default mode */
1242 if (default_vmode == NULL) {
1243
1244 struct fb_videomode fb_vmode = {0};
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001245
1246 /*
1247 * Add the standard VESA modes to our modelist
1248 * Since we don't have EDID, there may be modes that
1249 * overspec monitor and/or are incorrect aspect ratio, etc.
1250 * But at least the user has a chance to choose
1251 */
1252 for (i = 0; i < VESA_MODEDB_SIZE; i++) {
1253 if (dlfb_is_valid_mode((struct fb_videomode *)
1254 &vesa_modes[i], info))
1255 fb_add_videomode(&vesa_modes[i],
1256 &info->modelist);
1257 }
1258
1259 /*
1260 * default to resolution safe for projectors
1261 * (since they are most common case without EDID)
1262 */
1263 fb_vmode.xres = 800;
1264 fb_vmode.yres = 600;
1265 fb_vmode.refresh = 60;
1266 default_vmode = fb_find_nearest_mode(&fb_vmode,
1267 &info->modelist);
1268 }
1269
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001270 /* If we have good mode and no active clients*/
1271 if ((default_vmode != NULL) && (dev->fb_count == 0)) {
1272
1273 fb_videomode_to_var(&info->var, default_vmode);
1274 dlfb_var_color_format(&info->var);
1275
1276 /*
1277 * with mode size info, we can now alloc our framebuffer.
1278 */
1279 memcpy(&info->fix, &dlfb_fix, sizeof(dlfb_fix));
1280 info->fix.line_length = info->var.xres *
1281 (info->var.bits_per_pixel / 8);
1282
1283 result = dlfb_realloc_framebuffer(dev, info);
1284
1285 } else
1286 result = -EINVAL;
1287
1288error:
1289 if (edid && (dev->edid != edid))
1290 kfree(edid);
1291
1292 if (info->dev)
1293 mutex_unlock(&info->lock);
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001294
1295 return result;
1296}
1297
1298static ssize_t metrics_bytes_rendered_show(struct device *fbdev,
1299 struct device_attribute *a, char *buf) {
1300 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1301 struct dlfb_data *dev = fb_info->par;
1302 return snprintf(buf, PAGE_SIZE, "%u\n",
1303 atomic_read(&dev->bytes_rendered));
1304}
1305
1306static ssize_t metrics_bytes_identical_show(struct device *fbdev,
1307 struct device_attribute *a, char *buf) {
1308 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1309 struct dlfb_data *dev = fb_info->par;
1310 return snprintf(buf, PAGE_SIZE, "%u\n",
1311 atomic_read(&dev->bytes_identical));
1312}
1313
1314static ssize_t metrics_bytes_sent_show(struct device *fbdev,
1315 struct device_attribute *a, char *buf) {
1316 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1317 struct dlfb_data *dev = fb_info->par;
1318 return snprintf(buf, PAGE_SIZE, "%u\n",
1319 atomic_read(&dev->bytes_sent));
1320}
1321
1322static ssize_t metrics_cpu_kcycles_used_show(struct device *fbdev,
1323 struct device_attribute *a, char *buf) {
1324 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1325 struct dlfb_data *dev = fb_info->par;
1326 return snprintf(buf, PAGE_SIZE, "%u\n",
1327 atomic_read(&dev->cpu_kcycles_used));
1328}
1329
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001330static ssize_t edid_show(
1331 struct file *filp,
1332 struct kobject *kobj, struct bin_attribute *a,
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001333 char *buf, loff_t off, size_t count) {
1334 struct device *fbdev = container_of(kobj, struct device, kobj);
1335 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1336 struct dlfb_data *dev = fb_info->par;
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001337
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001338 if (dev->edid == NULL)
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001339 return 0;
1340
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001341 if ((off >= dev->edid_size) || (count > dev->edid_size))
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001342 return 0;
1343
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001344 if (off + count > dev->edid_size)
1345 count = dev->edid_size - off;
1346
1347 dl_info("sysfs edid copy %p to %p, %d bytes\n",
1348 dev->edid, buf, (int) count);
1349
1350 memcpy(buf, dev->edid, count);
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001351
1352 return count;
1353}
1354
Bernie Thompson8ef8cc42010-09-05 16:35:31 -07001355static ssize_t edid_store(
1356 struct file *filp,
1357 struct kobject *kobj, struct bin_attribute *a,
1358 char *src, loff_t src_off, size_t src_size) {
1359 struct device *fbdev = container_of(kobj, struct device, kobj);
1360 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1361 struct dlfb_data *dev = fb_info->par;
1362
1363 /* We only support write of entire EDID at once, no offset*/
Paul Mundtb9f03a32011-01-06 18:04:02 +09001364 if ((src_size != EDID_LENGTH) || (src_off != 0))
Bernie Thompson8ef8cc42010-09-05 16:35:31 -07001365 return 0;
1366
1367 dlfb_setup_modes(dev, fb_info, src, src_size);
1368
1369 if (dev->edid && (memcmp(src, dev->edid, src_size) == 0)) {
1370 dl_info("sysfs written EDID is new default\n");
1371 dlfb_ops_set_par(fb_info);
1372 return src_size;
1373 } else
1374 return 0;
1375}
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001376
1377static ssize_t metrics_reset_store(struct device *fbdev,
1378 struct device_attribute *attr,
1379 const char *buf, size_t count)
1380{
1381 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1382 struct dlfb_data *dev = fb_info->par;
1383
1384 atomic_set(&dev->bytes_rendered, 0);
1385 atomic_set(&dev->bytes_identical, 0);
1386 atomic_set(&dev->bytes_sent, 0);
1387 atomic_set(&dev->cpu_kcycles_used, 0);
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001388
1389 return count;
1390}
1391
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001392static struct bin_attribute edid_attr = {
1393 .attr.name = "edid",
Bernie Thompson8ef8cc42010-09-05 16:35:31 -07001394 .attr.mode = 0666,
Paul Mundtb9f03a32011-01-06 18:04:02 +09001395 .size = EDID_LENGTH,
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001396 .read = edid_show,
Bernie Thompson8ef8cc42010-09-05 16:35:31 -07001397 .write = edid_store
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001398};
1399
1400static struct device_attribute fb_device_attrs[] = {
1401 __ATTR_RO(metrics_bytes_rendered),
1402 __ATTR_RO(metrics_bytes_identical),
1403 __ATTR_RO(metrics_bytes_sent),
1404 __ATTR_RO(metrics_cpu_kcycles_used),
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001405 __ATTR(metrics_reset, S_IWUGO, NULL, metrics_reset_store),
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001406};
1407
1408/*
Bernie Thompsoncc403dc2010-02-15 06:45:49 -08001409 * This is necessary before we can communicate with the display controller.
1410 */
1411static int dlfb_select_std_channel(struct dlfb_data *dev)
1412{
1413 int ret;
1414 u8 set_def_chn[] = { 0x57, 0xCD, 0xDC, 0xA7,
1415 0x1C, 0x88, 0x5E, 0x15,
1416 0x60, 0xFE, 0xC6, 0x97,
1417 0x16, 0x3D, 0x47, 0xF2 };
1418
1419 ret = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
1420 NR_USB_REQUEST_CHANNEL,
1421 (USB_DIR_OUT | USB_TYPE_VENDOR), 0, 0,
1422 set_def_chn, sizeof(set_def_chn), USB_CTRL_SET_TIMEOUT);
1423 return ret;
1424}
1425
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001426static int dlfb_parse_vendor_descriptor(struct dlfb_data *dev,
1427 struct usb_device *usbdev)
1428{
1429 char *desc;
1430 char *buf;
1431 char *desc_end;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001432
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001433 u8 total_len = 0;
1434
1435 buf = kzalloc(MAX_VENDOR_DESCRIPTOR_SIZE, GFP_KERNEL);
1436 if (!buf)
1437 return false;
1438 desc = buf;
1439
1440 total_len = usb_get_descriptor(usbdev, 0x5f, /* vendor specific */
1441 0, desc, MAX_VENDOR_DESCRIPTOR_SIZE);
1442 if (total_len > 5) {
1443 dl_info("vendor descriptor length:%x data:%02x %02x %02x %02x" \
1444 "%02x %02x %02x %02x %02x %02x %02x\n",
1445 total_len, desc[0],
1446 desc[1], desc[2], desc[3], desc[4], desc[5], desc[6],
1447 desc[7], desc[8], desc[9], desc[10]);
1448
1449 if ((desc[0] != total_len) || /* descriptor length */
1450 (desc[1] != 0x5f) || /* vendor descriptor type */
1451 (desc[2] != 0x01) || /* version (2 bytes) */
1452 (desc[3] != 0x00) ||
1453 (desc[4] != total_len - 2)) /* length after type */
1454 goto unrecognized;
1455
1456 desc_end = desc + total_len;
1457 desc += 5; /* the fixed header we've already parsed */
1458
1459 while (desc < desc_end) {
1460 u8 length;
1461 u16 key;
1462
1463 key = *((u16 *) desc);
1464 desc += sizeof(u16);
1465 length = *desc;
1466 desc++;
1467
1468 switch (key) {
1469 case 0x0200: { /* max_area */
1470 u32 max_area;
1471 max_area = le32_to_cpu(*((u32 *)desc));
1472 dl_warn("DL chip limited to %d pixel modes\n",
1473 max_area);
1474 dev->sku_pixel_limit = max_area;
1475 break;
1476 }
1477 default:
1478 break;
1479 }
1480 desc += length;
1481 }
1482 }
1483
1484 goto success;
1485
1486unrecognized:
1487 /* allow udlfb to load for now even if firmware unrecognized */
1488 dl_err("Unrecognized vendor firmware descriptor\n");
1489
1490success:
1491 kfree(buf);
1492 return true;
1493}
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001494static int dlfb_usb_probe(struct usb_interface *interface,
Bernie Thompson59277b62009-11-24 15:52:21 -08001495 const struct usb_device_id *id)
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001496{
Bernie Thompson59277b62009-11-24 15:52:21 -08001497 struct usb_device *usbdev;
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001498 struct dlfb_data *dev = 0;
Bernie Thompson33077b82010-09-05 16:35:19 -07001499 struct fb_info *info = 0;
Bernie Thompson59277b62009-11-24 15:52:21 -08001500 int retval = -ENOMEM;
Bernie Thompson2685cff2010-09-05 18:29:56 -07001501 int i;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001502
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001503 /* usb initialization */
1504
1505 usbdev = interface_to_usbdev(interface);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001506
Bernie Thompson59277b62009-11-24 15:52:21 -08001507 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1508 if (dev == NULL) {
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001509 err("dlfb_usb_probe: failed alloc of dev struct\n");
1510 goto error;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001511 }
1512
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001513 /* we need to wait for both usb and fbdev to spin down on disconnect */
1514 kref_init(&dev->kref); /* matching kref_put in usb .disconnect fn */
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001515 kref_get(&dev->kref); /* matching kref_put in free_framebuffer_work */
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001516
Bernie Thompson59277b62009-11-24 15:52:21 -08001517 dev->udev = usbdev;
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001518 dev->gdev = &usbdev->dev; /* our generic struct device * */
Bernie Thompson59277b62009-11-24 15:52:21 -08001519 usb_set_intfdata(interface, dev);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001520
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001521 dl_info("%s %s - serial #%s\n",
1522 usbdev->manufacturer, usbdev->product, usbdev->serial);
1523 dl_info("vid_%04x&pid_%04x&rev_%04x driver's dlfb_data struct at %p\n",
1524 usbdev->descriptor.idVendor, usbdev->descriptor.idProduct,
1525 usbdev->descriptor.bcdDevice, dev);
Bernie Thompsond5ed5432010-09-05 16:35:39 -07001526 dl_info("console enable=%d\n", console);
1527 dl_info("fb_defio enable=%d\n", fb_defio);
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001528
1529 dev->sku_pixel_limit = 2048 * 1152; /* default to maximum */
1530
1531 if (!dlfb_parse_vendor_descriptor(dev, usbdev)) {
1532 dl_err("firmware not recognized. Assume incompatible device\n");
1533 goto error;
1534 }
1535
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001536 if (!dlfb_alloc_urb_list(dev, WRITES_IN_FLIGHT, MAX_TRANSFER)) {
1537 retval = -ENOMEM;
1538 dl_err("dlfb_alloc_urb_list failed\n");
1539 goto error;
1540 }
1541
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001542 /* We don't register a new USB class. Our client interface is fbdev */
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001543
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001544 /* allocates framebuffer driver structure, not framebuffer memory */
1545 info = framebuffer_alloc(0, &usbdev->dev);
1546 if (!info) {
1547 retval = -ENOMEM;
1548 dl_err("framebuffer_alloc failed\n");
1549 goto error;
1550 }
Bernie Thompson33077b82010-09-05 16:35:19 -07001551
Bernie Thompson59277b62009-11-24 15:52:21 -08001552 dev->info = info;
1553 info->par = dev;
1554 info->pseudo_palette = dev->pseudo_palette;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001555 info->fbops = &dlfb_ops;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001556
Bernie Thompson59277b62009-11-24 15:52:21 -08001557 retval = fb_alloc_cmap(&info->cmap, 256, 0);
1558 if (retval < 0) {
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001559 dl_err("fb_alloc_cmap failed %x\n", retval);
1560 goto error;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001561 }
1562
Bernie Thompson33077b82010-09-05 16:35:19 -07001563 INIT_DELAYED_WORK(&dev->free_framebuffer_work,
1564 dlfb_free_framebuffer_work);
1565
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001566 INIT_LIST_HEAD(&info->modelist);
1567
1568 retval = dlfb_setup_modes(dev, info, NULL, 0);
1569 if (retval != 0) {
1570 dl_err("unable to find common mode for display and adapter\n");
1571 goto error;
1572 }
1573
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001574 /* ready to begin using device */
1575
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001576 atomic_set(&dev->usb_active, 1);
Bernie Thompson59277b62009-11-24 15:52:21 -08001577 dlfb_select_std_channel(dev);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001578
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001579 dlfb_ops_check_var(&info->var, info);
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001580 dlfb_ops_set_par(info);
1581
Bernie Thompson59277b62009-11-24 15:52:21 -08001582 retval = register_framebuffer(info);
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001583 if (retval < 0) {
1584 dl_err("register_framebuffer failed %d\n", retval);
1585 goto error;
1586 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001587
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001588 for (i = 0; i < ARRAY_SIZE(fb_device_attrs); i++)
1589 device_create_file(info->dev, &fb_device_attrs[i]);
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -07001590
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001591 device_create_bin_file(info->dev, &edid_attr);
1592
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001593 dl_info("DisplayLink USB device /dev/fb%d attached. %dx%d resolution."
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001594 " Using %dK framebuffer memory\n", info->node,
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001595 info->var.xres, info->var.yres,
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001596 ((dev->backing_buffer) ?
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001597 info->fix.smem_len * 2 : info->fix.smem_len) >> 10);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001598 return 0;
1599
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001600error:
1601 if (dev) {
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001602
Bernie Thompson33077b82010-09-05 16:35:19 -07001603 if (info) {
1604 if (info->cmap.len != 0)
1605 fb_dealloc_cmap(&info->cmap);
1606 if (info->monspecs.modedb)
1607 fb_destroy_modedb(info->monspecs.modedb);
1608 if (info->screen_base)
1609 vfree(info->screen_base);
1610
1611 fb_destroy_modelist(&info->modelist);
1612
1613 framebuffer_release(info);
1614 }
1615
1616 if (dev->backing_buffer)
1617 vfree(dev->backing_buffer);
1618
1619 kref_put(&dev->kref, dlfb_free); /* ref for framebuffer */
1620 kref_put(&dev->kref, dlfb_free); /* last ref from kref_init */
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001621
1622 /* dev has been deallocated. Do not dereference */
1623 }
1624
Bernie Thompson59277b62009-11-24 15:52:21 -08001625 return retval;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001626}
1627
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001628static void dlfb_usb_disconnect(struct usb_interface *interface)
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001629{
Bernie Thompson59277b62009-11-24 15:52:21 -08001630 struct dlfb_data *dev;
1631 struct fb_info *info;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001632 int i;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001633
Bernie Thompson59277b62009-11-24 15:52:21 -08001634 dev = usb_get_intfdata(interface);
Bernie Thompson59277b62009-11-24 15:52:21 -08001635 info = dev->info;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001636
Bernie Thompson33077b82010-09-05 16:35:19 -07001637 dl_info("USB disconnect starting\n");
1638
1639 /* we virtualize until all fb clients release. Then we free */
1640 dev->virtualized = true;
1641
1642 /* When non-active we'll update virtual framebuffer, but no new urbs */
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001643 atomic_set(&dev->usb_active, 0);
1644
Bernie Thompson33077b82010-09-05 16:35:19 -07001645 /* remove udlfb's sysfs interfaces */
1646 for (i = 0; i < ARRAY_SIZE(fb_device_attrs); i++)
1647 device_remove_file(info->dev, &fb_device_attrs[i]);
1648 device_remove_bin_file(info->dev, &edid_attr);
1649
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001650 usb_set_intfdata(interface, NULL);
1651
Bernie Thompson33077b82010-09-05 16:35:19 -07001652 /* if clients still have us open, will be freed on last close */
1653 if (dev->fb_count == 0)
1654 schedule_delayed_work(&dev->free_framebuffer_work, 0);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001655
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001656 /* release reference taken by kref_init in probe() */
Bernie Thompson33077b82010-09-05 16:35:19 -07001657 kref_put(&dev->kref, dlfb_free);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001658
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001659 /* consider dlfb_data freed */
1660
1661 return;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001662}
1663
1664static struct usb_driver dlfb_driver = {
1665 .name = "udlfb",
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001666 .probe = dlfb_usb_probe,
1667 .disconnect = dlfb_usb_disconnect,
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001668 .id_table = id_table,
1669};
1670
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001671static int __init dlfb_module_init(void)
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001672{
1673 int res;
1674
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001675 res = usb_register(&dlfb_driver);
1676 if (res)
1677 err("usb_register failed. Error number %d", res);
1678
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001679 return res;
1680}
1681
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001682static void __exit dlfb_module_exit(void)
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001683{
1684 usb_deregister(&dlfb_driver);
1685}
1686
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001687module_init(dlfb_module_init);
1688module_exit(dlfb_module_exit);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001689
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001690static void dlfb_urb_completion(struct urb *urb)
1691{
1692 struct urb_node *unode = urb->context;
1693 struct dlfb_data *dev = unode->dev;
1694 unsigned long flags;
1695
1696 /* sync/async unlink faults aren't errors */
1697 if (urb->status) {
1698 if (!(urb->status == -ENOENT ||
1699 urb->status == -ECONNRESET ||
1700 urb->status == -ESHUTDOWN)) {
1701 dl_err("%s - nonzero write bulk status received: %d\n",
1702 __func__, urb->status);
1703 atomic_set(&dev->lost_pixels, 1);
1704 }
1705 }
1706
1707 urb->transfer_buffer_length = dev->urbs.size; /* reset to actual */
1708
1709 spin_lock_irqsave(&dev->urbs.lock, flags);
1710 list_add_tail(&unode->entry, &dev->urbs.list);
1711 dev->urbs.available++;
1712 spin_unlock_irqrestore(&dev->urbs.lock, flags);
1713
Bernie Thompson5bea1fb2010-09-05 18:28:29 -07001714 /*
1715 * When using fb_defio, we deadlock if up() is called
1716 * while another is waiting. So queue to another process.
1717 */
1718 if (fb_defio)
1719 schedule_delayed_work(&unode->release_urb_work, 0);
1720 else
1721 up(&dev->urbs.limit_sem);
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001722}
1723
1724static void dlfb_free_urb_list(struct dlfb_data *dev)
1725{
1726 int count = dev->urbs.count;
1727 struct list_head *node;
1728 struct urb_node *unode;
1729 struct urb *urb;
1730 int ret;
1731 unsigned long flags;
1732
1733 dl_notice("Waiting for completes and freeing all render urbs\n");
1734
1735 /* keep waiting and freeing, until we've got 'em all */
1736 while (count--) {
Bernie Thompson33077b82010-09-05 16:35:19 -07001737
1738 /* Getting interrupted means a leak, but ok at shutdown*/
1739 ret = down_interruptible(&dev->urbs.limit_sem);
1740 if (ret)
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001741 break;
Bernie Thompson33077b82010-09-05 16:35:19 -07001742
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001743 spin_lock_irqsave(&dev->urbs.lock, flags);
1744
1745 node = dev->urbs.list.next; /* have reserved one with sem */
1746 list_del_init(node);
1747
1748 spin_unlock_irqrestore(&dev->urbs.lock, flags);
1749
1750 unode = list_entry(node, struct urb_node, entry);
1751 urb = unode->urb;
1752
1753 /* Free each separately allocated piece */
Greg Kroah-Hartmanc220cc32010-04-29 15:36:29 -07001754 usb_free_coherent(urb->dev, dev->urbs.size,
1755 urb->transfer_buffer, urb->transfer_dma);
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001756 usb_free_urb(urb);
1757 kfree(node);
1758 }
1759
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001760}
1761
1762static int dlfb_alloc_urb_list(struct dlfb_data *dev, int count, size_t size)
1763{
1764 int i = 0;
1765 struct urb *urb;
1766 struct urb_node *unode;
1767 char *buf;
1768
1769 spin_lock_init(&dev->urbs.lock);
1770
1771 dev->urbs.size = size;
1772 INIT_LIST_HEAD(&dev->urbs.list);
1773
1774 while (i < count) {
1775 unode = kzalloc(sizeof(struct urb_node), GFP_KERNEL);
1776 if (!unode)
1777 break;
1778 unode->dev = dev;
1779
Bernie Thompson5bea1fb2010-09-05 18:28:29 -07001780 INIT_DELAYED_WORK(&unode->release_urb_work,
1781 dlfb_release_urb_work);
1782
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001783 urb = usb_alloc_urb(0, GFP_KERNEL);
1784 if (!urb) {
1785 kfree(unode);
1786 break;
1787 }
1788 unode->urb = urb;
1789
Greg Kroah-Hartmanc220cc32010-04-29 15:36:29 -07001790 buf = usb_alloc_coherent(dev->udev, MAX_TRANSFER, GFP_KERNEL,
1791 &urb->transfer_dma);
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001792 if (!buf) {
1793 kfree(unode);
1794 usb_free_urb(urb);
1795 break;
1796 }
1797
1798 /* urb->transfer_buffer_length set to actual before submit */
1799 usb_fill_bulk_urb(urb, dev->udev, usb_sndbulkpipe(dev->udev, 1),
1800 buf, size, dlfb_urb_completion, unode);
1801 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1802
1803 list_add_tail(&unode->entry, &dev->urbs.list);
1804
1805 i++;
1806 }
1807
1808 sema_init(&dev->urbs.limit_sem, i);
1809 dev->urbs.count = i;
1810 dev->urbs.available = i;
1811
Soeren Moellerb5a21042010-05-14 19:03:00 +00001812 dl_notice("allocated %d %d byte urbs\n", i, (int) size);
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001813
1814 return i;
1815}
1816
1817static struct urb *dlfb_get_urb(struct dlfb_data *dev)
1818{
1819 int ret = 0;
1820 struct list_head *entry;
1821 struct urb_node *unode;
1822 struct urb *urb = NULL;
1823 unsigned long flags;
1824
1825 /* Wait for an in-flight buffer to complete and get re-queued */
1826 ret = down_timeout(&dev->urbs.limit_sem, GET_URB_TIMEOUT);
1827 if (ret) {
1828 atomic_set(&dev->lost_pixels, 1);
Bernie Thompson5bea1fb2010-09-05 18:28:29 -07001829 dl_warn("wait for urb interrupted: %x available: %d\n",
1830 ret, dev->urbs.available);
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001831 goto error;
1832 }
1833
1834 spin_lock_irqsave(&dev->urbs.lock, flags);
1835
1836 BUG_ON(list_empty(&dev->urbs.list)); /* reserved one with limit_sem */
1837 entry = dev->urbs.list.next;
1838 list_del_init(entry);
1839 dev->urbs.available--;
1840
1841 spin_unlock_irqrestore(&dev->urbs.lock, flags);
1842
1843 unode = list_entry(entry, struct urb_node, entry);
1844 urb = unode->urb;
1845
1846error:
1847 return urb;
1848}
1849
1850static int dlfb_submit_urb(struct dlfb_data *dev, struct urb *urb, size_t len)
1851{
1852 int ret;
1853
1854 BUG_ON(len > dev->urbs.size);
1855
1856 urb->transfer_buffer_length = len; /* set to actual payload len */
1857 ret = usb_submit_urb(urb, GFP_KERNEL);
1858 if (ret) {
1859 dlfb_urb_completion(urb); /* because no one else will */
1860 atomic_set(&dev->lost_pixels, 1);
1861 dl_err("usb_submit_urb error %x\n", ret);
1862 }
1863 return ret;
1864}
1865
Bernie Thompsond5ed5432010-09-05 16:35:39 -07001866module_param(console, bool, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
1867MODULE_PARM_DESC(console, "Allow fbcon to consume first framebuffer found");
1868
1869module_param(fb_defio, bool, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
1870MODULE_PARM_DESC(fb_defio, "Enable fb_defio mmap support. *Experimental*");
1871
Bernie Thompson59277b62009-11-24 15:52:21 -08001872MODULE_AUTHOR("Roberto De Ioris <roberto@unbit.it>, "
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001873 "Jaya Kumar <jayakumar.lkml@gmail.com>, "
1874 "Bernie Thompson <bernie@plugable.com>");
1875MODULE_DESCRIPTION("DisplayLink kernel framebuffer driver");
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001876MODULE_LICENSE("GPL");
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001877