blob: e2283294c71afe505b7c88ff17b7a07edb4b2c63 [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>
29
Roberto De Ioris88e58b12009-06-03 14:03:06 -070030#include "udlfb.h"
31
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 |
43#ifdef FBINFO_VIRTFB
44 FBINFO_VIRTFB |
45#endif
46 FBINFO_HWACCEL_IMAGEBLIT | FBINFO_HWACCEL_FILLRECT |
47 FBINFO_HWACCEL_COPYAREA | FBINFO_MISC_ALWAYS_SETPAR;
48
Bernie Thompsoncc403dc2010-02-15 06:45:49 -080049/*
50 * There are many DisplayLink-based products, all with unique PIDs. We are able
51 * to support all volume ones (circa 2009) with a single driver, so we match
52 * globally on VID. TODO: Probe() needs to detect when we might be running
53 * "future" chips, and bail on those, so a compatible driver can match.
54 */
55static struct usb_device_id id_table[] = {
56 {.idVendor = 0x17e9, .match_flags = USB_DEVICE_ID_MATCH_VENDOR,},
57 {},
58};
59MODULE_DEVICE_TABLE(usb, id_table);
Bernie Thompson59277b62009-11-24 15:52:21 -080060
Bernie Thompsond5ed5432010-09-05 16:35:39 -070061/* module options */
62static int console; /* Optionally allow fbcon to consume first framebuffer */
63static int fb_defio; /* Optionally enable experimental fb_defio mmap support */
Bernie Thompsondd8015f2010-02-15 06:46:35 -080064
Bernie Thompson4a4854d2010-02-15 06:45:55 -080065/* dlfb keeps a list of urbs for efficient bulk transfers */
66static void dlfb_urb_completion(struct urb *urb);
67static struct urb *dlfb_get_urb(struct dlfb_data *dev);
68static int dlfb_submit_urb(struct dlfb_data *dev, struct urb * urb, size_t len);
69static int dlfb_alloc_urb_list(struct dlfb_data *dev, int count, size_t size);
70static void dlfb_free_urb_list(struct dlfb_data *dev);
71
Bernie Thompson59277b62009-11-24 15:52:21 -080072/*
Bernie Thompsonbd808162010-02-15 06:46:48 -080073 * All DisplayLink bulk operations start with 0xAF, followed by specific code
74 * All operations are written to buffers which then later get sent to device
Bernie Thompson59277b62009-11-24 15:52:21 -080075 */
Bernie Thompson45742032010-02-15 06:46:04 -080076static char *dlfb_set_register(char *buf, u8 reg, u8 val)
Roberto De Ioris88e58b12009-06-03 14:03:06 -070077{
Bernie Thompson1d31a9e2010-02-15 06:45:43 -080078 *buf++ = 0xAF;
79 *buf++ = 0x20;
80 *buf++ = reg;
81 *buf++ = val;
82 return buf;
Roberto De Ioris88e58b12009-06-03 14:03:06 -070083}
84
Bernie Thompson45742032010-02-15 06:46:04 -080085static char *dlfb_vidreg_lock(char *buf)
Roberto De Ioris88e58b12009-06-03 14:03:06 -070086{
Bernie Thompson45742032010-02-15 06:46:04 -080087 return dlfb_set_register(buf, 0xFF, 0x00);
Bernie Thompson59277b62009-11-24 15:52:21 -080088}
Roberto De Ioris88e58b12009-06-03 14:03:06 -070089
Bernie Thompson45742032010-02-15 06:46:04 -080090static char *dlfb_vidreg_unlock(char *buf)
Bernie Thompson59277b62009-11-24 15:52:21 -080091{
Bernie Thompson45742032010-02-15 06:46:04 -080092 return dlfb_set_register(buf, 0xFF, 0xFF);
Bernie Thompson59277b62009-11-24 15:52:21 -080093}
Roberto De Ioris88e58b12009-06-03 14:03:06 -070094
Bernie Thompson59277b62009-11-24 15:52:21 -080095/*
Bernie Thompson530f43a2010-02-15 06:46:21 -080096 * On/Off for driving the DisplayLink framebuffer to the display
Bernie Thompson9825f702010-09-05 16:35:10 -070097 * 0x00 H and V sync on
98 * 0x01 H and V sync off (screen blank but powered)
99 * 0x07 DPMS powerdown (requires modeset to come back)
Bernie Thompson59277b62009-11-24 15:52:21 -0800100 */
Bernie Thompson530f43a2010-02-15 06:46:21 -0800101static char *dlfb_enable_hvsync(char *buf, bool enable)
Bernie Thompson59277b62009-11-24 15:52:21 -0800102{
Bernie Thompson530f43a2010-02-15 06:46:21 -0800103 if (enable)
104 return dlfb_set_register(buf, 0x1F, 0x00);
105 else
Bernie Thompson9825f702010-09-05 16:35:10 -0700106 return dlfb_set_register(buf, 0x1F, 0x07);
Bernie Thompson59277b62009-11-24 15:52:21 -0800107}
108
Bernie Thompson45742032010-02-15 06:46:04 -0800109static char *dlfb_set_color_depth(char *buf, u8 selection)
Bernie Thompson59277b62009-11-24 15:52:21 -0800110{
Bernie Thompson45742032010-02-15 06:46:04 -0800111 return dlfb_set_register(buf, 0x00, selection);
Bernie Thompson59277b62009-11-24 15:52:21 -0800112}
113
Bernie Thompson45742032010-02-15 06:46:04 -0800114static char *dlfb_set_base16bpp(char *wrptr, u32 base)
Bernie Thompson59277b62009-11-24 15:52:21 -0800115{
Bernie Thompson1d31a9e2010-02-15 06:45:43 -0800116 /* the base pointer is 16 bits wide, 0x20 is hi byte. */
Bernie Thompson45742032010-02-15 06:46:04 -0800117 wrptr = dlfb_set_register(wrptr, 0x20, base >> 16);
118 wrptr = dlfb_set_register(wrptr, 0x21, base >> 8);
119 return dlfb_set_register(wrptr, 0x22, base);
Bernie Thompson59277b62009-11-24 15:52:21 -0800120}
121
Bernie Thompsonbd808162010-02-15 06:46:48 -0800122/*
123 * DisplayLink HW has separate 16bpp and 8bpp framebuffers.
124 * In 24bpp modes, the low 323 RGB bits go in the 8bpp framebuffer
125 */
Bernie Thompson45742032010-02-15 06:46:04 -0800126static char *dlfb_set_base8bpp(char *wrptr, u32 base)
Bernie Thompson59277b62009-11-24 15:52:21 -0800127{
Bernie Thompson45742032010-02-15 06:46:04 -0800128 wrptr = dlfb_set_register(wrptr, 0x26, base >> 16);
129 wrptr = dlfb_set_register(wrptr, 0x27, base >> 8);
130 return dlfb_set_register(wrptr, 0x28, base);
Bernie Thompson59277b62009-11-24 15:52:21 -0800131}
132
Bernie Thompson45742032010-02-15 06:46:04 -0800133static char *dlfb_set_register_16(char *wrptr, u8 reg, u16 value)
Bernie Thompson59277b62009-11-24 15:52:21 -0800134{
Bernie Thompson45742032010-02-15 06:46:04 -0800135 wrptr = dlfb_set_register(wrptr, reg, value >> 8);
136 return dlfb_set_register(wrptr, reg+1, value);
Bernie Thompson59277b62009-11-24 15:52:21 -0800137}
138
139/*
140 * This is kind of weird because the controller takes some
141 * register values in a different byte order than other registers.
142 */
Bernie Thompson45742032010-02-15 06:46:04 -0800143static char *dlfb_set_register_16be(char *wrptr, u8 reg, u16 value)
Bernie Thompson59277b62009-11-24 15:52:21 -0800144{
Bernie Thompson45742032010-02-15 06:46:04 -0800145 wrptr = dlfb_set_register(wrptr, reg, value);
146 return dlfb_set_register(wrptr, reg+1, value >> 8);
Bernie Thompson59277b62009-11-24 15:52:21 -0800147}
148
149/*
150 * LFSR is linear feedback shift register. The reason we have this is
151 * because the display controller needs to minimize the clock depth of
152 * various counters used in the display path. So this code reverses the
153 * provided value into the lfsr16 value by counting backwards to get
154 * the value that needs to be set in the hardware comparator to get the
155 * same actual count. This makes sense once you read above a couple of
156 * times and think about it from a hardware perspective.
157 */
Bernie Thompsonbd808162010-02-15 06:46:48 -0800158static u16 dlfb_lfsr16(u16 actual_count)
Bernie Thompson59277b62009-11-24 15:52:21 -0800159{
160 u32 lv = 0xFFFF; /* This is the lfsr value that the hw starts with */
161
162 while (actual_count--) {
163 lv = ((lv << 1) |
164 (((lv >> 15) ^ (lv >> 4) ^ (lv >> 2) ^ (lv >> 1)) & 1))
165 & 0xFFFF;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700166 }
Bernie Thompson59277b62009-11-24 15:52:21 -0800167
168 return (u16) lv;
169}
170
171/*
172 * This does LFSR conversion on the value that is to be written.
173 * See LFSR explanation above for more detail.
174 */
Bernie Thompson45742032010-02-15 06:46:04 -0800175static char *dlfb_set_register_lfsr16(char *wrptr, u8 reg, u16 value)
Bernie Thompson59277b62009-11-24 15:52:21 -0800176{
Bernie Thompsonbd808162010-02-15 06:46:48 -0800177 return dlfb_set_register_16(wrptr, reg, dlfb_lfsr16(value));
Bernie Thompson59277b62009-11-24 15:52:21 -0800178}
179
180/*
181 * This takes a standard fbdev screeninfo struct and all of its monitor mode
182 * details and converts them into the DisplayLink equivalent register commands.
183 */
Bernie Thompson45742032010-02-15 06:46:04 -0800184static char *dlfb_set_vid_cmds(char *wrptr, struct fb_var_screeninfo *var)
Bernie Thompson59277b62009-11-24 15:52:21 -0800185{
186 u16 xds, yds;
187 u16 xde, yde;
188 u16 yec;
189
Bernie Thompson59277b62009-11-24 15:52:21 -0800190 /* x display start */
191 xds = var->left_margin + var->hsync_len;
Bernie Thompson45742032010-02-15 06:46:04 -0800192 wrptr = dlfb_set_register_lfsr16(wrptr, 0x01, xds);
Bernie Thompson59277b62009-11-24 15:52:21 -0800193 /* x display end */
194 xde = xds + var->xres;
Bernie Thompson45742032010-02-15 06:46:04 -0800195 wrptr = dlfb_set_register_lfsr16(wrptr, 0x03, xde);
Bernie Thompson59277b62009-11-24 15:52:21 -0800196
197 /* y display start */
198 yds = var->upper_margin + var->vsync_len;
Bernie Thompson45742032010-02-15 06:46:04 -0800199 wrptr = dlfb_set_register_lfsr16(wrptr, 0x05, yds);
Bernie Thompson59277b62009-11-24 15:52:21 -0800200 /* y display end */
201 yde = yds + var->yres;
Bernie Thompson45742032010-02-15 06:46:04 -0800202 wrptr = dlfb_set_register_lfsr16(wrptr, 0x07, yde);
Bernie Thompson59277b62009-11-24 15:52:21 -0800203
204 /* x end count is active + blanking - 1 */
Bernie Thompson45742032010-02-15 06:46:04 -0800205 wrptr = dlfb_set_register_lfsr16(wrptr, 0x09,
206 xde + var->right_margin - 1);
Bernie Thompson59277b62009-11-24 15:52:21 -0800207
208 /* libdlo hardcodes hsync start to 1 */
Bernie Thompson45742032010-02-15 06:46:04 -0800209 wrptr = dlfb_set_register_lfsr16(wrptr, 0x0B, 1);
Bernie Thompson59277b62009-11-24 15:52:21 -0800210
211 /* hsync end is width of sync pulse + 1 */
Bernie Thompson45742032010-02-15 06:46:04 -0800212 wrptr = dlfb_set_register_lfsr16(wrptr, 0x0D, var->hsync_len + 1);
Bernie Thompson59277b62009-11-24 15:52:21 -0800213
214 /* hpixels is active pixels */
Bernie Thompson45742032010-02-15 06:46:04 -0800215 wrptr = dlfb_set_register_16(wrptr, 0x0F, var->xres);
Bernie Thompson59277b62009-11-24 15:52:21 -0800216
217 /* yendcount is vertical active + vertical blanking */
218 yec = var->yres + var->upper_margin + var->lower_margin +
219 var->vsync_len;
Bernie Thompson45742032010-02-15 06:46:04 -0800220 wrptr = dlfb_set_register_lfsr16(wrptr, 0x11, yec);
Bernie Thompson59277b62009-11-24 15:52:21 -0800221
222 /* libdlo hardcodes vsync start to 0 */
Bernie Thompson45742032010-02-15 06:46:04 -0800223 wrptr = dlfb_set_register_lfsr16(wrptr, 0x13, 0);
Bernie Thompson59277b62009-11-24 15:52:21 -0800224
225 /* vsync end is width of vsync pulse */
Bernie Thompson45742032010-02-15 06:46:04 -0800226 wrptr = dlfb_set_register_lfsr16(wrptr, 0x15, var->vsync_len);
Bernie Thompson59277b62009-11-24 15:52:21 -0800227
228 /* vpixels is active pixels */
Bernie Thompson45742032010-02-15 06:46:04 -0800229 wrptr = dlfb_set_register_16(wrptr, 0x17, var->yres);
Bernie Thompson59277b62009-11-24 15:52:21 -0800230
231 /* convert picoseconds to 5kHz multiple for pclk5k = x * 1E12/5k */
Bernie Thompson45742032010-02-15 06:46:04 -0800232 wrptr = dlfb_set_register_16be(wrptr, 0x1B,
233 200*1000*1000/var->pixclock);
Bernie Thompson59277b62009-11-24 15:52:21 -0800234
235 return wrptr;
236}
237
238/*
239 * This takes a standard fbdev screeninfo struct that was fetched or prepared
240 * and then generates the appropriate command sequence that then drives the
241 * display controller.
242 */
243static int dlfb_set_video_mode(struct dlfb_data *dev,
244 struct fb_var_screeninfo *var)
245{
246 char *buf;
247 char *wrptr;
248 int retval = 0;
249 int writesize;
Bernie Thompson530f43a2010-02-15 06:46:21 -0800250 struct urb *urb;
Bernie Thompson59277b62009-11-24 15:52:21 -0800251
Bernie Thompson530f43a2010-02-15 06:46:21 -0800252 if (!atomic_read(&dev->usb_active))
253 return -EPERM;
254
255 urb = dlfb_get_urb(dev);
256 if (!urb)
257 return -ENOMEM;
Bernie Thompson2685cff2010-09-05 18:29:56 -0700258
Bernie Thompson530f43a2010-02-15 06:46:21 -0800259 buf = (char *) urb->transfer_buffer;
Bernie Thompson59277b62009-11-24 15:52:21 -0800260
261 /*
262 * This first section has to do with setting the base address on the
263 * controller * associated with the display. There are 2 base
264 * pointers, currently, we only * use the 16 bpp segment.
265 */
Bernie Thompson45742032010-02-15 06:46:04 -0800266 wrptr = dlfb_vidreg_lock(buf);
267 wrptr = dlfb_set_color_depth(wrptr, 0x00);
Bernie Thompson59277b62009-11-24 15:52:21 -0800268 /* set base for 16bpp segment to 0 */
Bernie Thompson45742032010-02-15 06:46:04 -0800269 wrptr = dlfb_set_base16bpp(wrptr, 0);
Bernie Thompson59277b62009-11-24 15:52:21 -0800270 /* set base for 8bpp segment to end of fb */
Bernie Thompson45742032010-02-15 06:46:04 -0800271 wrptr = dlfb_set_base8bpp(wrptr, dev->info->fix.smem_len);
Bernie Thompson59277b62009-11-24 15:52:21 -0800272
Bernie Thompson45742032010-02-15 06:46:04 -0800273 wrptr = dlfb_set_vid_cmds(wrptr, var);
Bernie Thompson530f43a2010-02-15 06:46:21 -0800274 wrptr = dlfb_enable_hvsync(wrptr, true);
Bernie Thompson45742032010-02-15 06:46:04 -0800275 wrptr = dlfb_vidreg_unlock(wrptr);
Bernie Thompson59277b62009-11-24 15:52:21 -0800276
277 writesize = wrptr - buf;
278
Bernie Thompson530f43a2010-02-15 06:46:21 -0800279 retval = dlfb_submit_urb(dev, urb, writesize);
Bernie Thompson59277b62009-11-24 15:52:21 -0800280
Bernie Thompson59277b62009-11-24 15:52:21 -0800281 return retval;
282}
283
Bernie Thompson45742032010-02-15 06:46:04 -0800284static int dlfb_ops_mmap(struct fb_info *info, struct vm_area_struct *vma)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700285{
286 unsigned long start = vma->vm_start;
287 unsigned long size = vma->vm_end - vma->vm_start;
288 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
289 unsigned long page, pos;
290
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700291 if (offset + size > info->fix.smem_len)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700292 return -EINVAL;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700293
294 pos = (unsigned long)info->fix.smem_start + offset;
295
Bernie Thompson2685cff2010-09-05 18:29:56 -0700296 dl_notice("mmap() framebuffer addr:%lu size:%lu\n",
297 pos, size);
298
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700299 while (size > 0) {
300 page = vmalloc_to_pfn((void *)pos);
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700301 if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED))
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700302 return -EAGAIN;
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700303
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700304 start += PAGE_SIZE;
305 pos += PAGE_SIZE;
306 if (size > PAGE_SIZE)
307 size -= PAGE_SIZE;
308 else
309 size = 0;
310 }
311
312 vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
313 return 0;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700314}
315
Bernie Thompson530f43a2010-02-15 06:46:21 -0800316/*
317 * Trims identical data from front and back of line
318 * Sets new front buffer address and width
319 * And returns byte count of identical pixels
320 * Assumes CPU natural alignment (unsigned long)
321 * for back and front buffer ptrs and width
322 */
323static int dlfb_trim_hline(const u8 *bback, const u8 **bfront, int *width_bytes)
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700324{
Bernie Thompson530f43a2010-02-15 06:46:21 -0800325 int j, k;
326 const unsigned long *back = (const unsigned long *) bback;
327 const unsigned long *front = (const unsigned long *) *bfront;
328 const int width = *width_bytes / sizeof(unsigned long);
329 int identical = width;
330 int start = width;
331 int end = width;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700332
Bernie Thompson530f43a2010-02-15 06:46:21 -0800333 prefetch((void *) front);
334 prefetch((void *) back);
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700335
Bernie Thompson530f43a2010-02-15 06:46:21 -0800336 for (j = 0; j < width; j++) {
337 if (back[j] != front[j]) {
338 start = j;
339 break;
340 }
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700341 }
342
Bernie Thompson530f43a2010-02-15 06:46:21 -0800343 for (k = width - 1; k > j; k--) {
344 if (back[k] != front[k]) {
345 end = k+1;
346 break;
347 }
348 }
349
350 identical = start + (width - end);
351 *bfront = (u8 *) &front[start];
352 *width_bytes = (end - start) * sizeof(unsigned long);
353
354 return identical * sizeof(unsigned long);
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700355}
356
357/*
Pavel Machek3b7b31f2010-04-03 07:00:37 +0200358 * Render a command stream for an encoded horizontal line segment of pixels.
359 *
360 * A command buffer holds several commands.
361 * It always begins with a fresh command header
362 * (the protocol doesn't require this, but we enforce it to allow
363 * multiple buffers to be potentially encoded and sent in parallel).
364 * A single command encodes one contiguous horizontal line of pixels
365 *
366 * The function relies on the client to do all allocation, so that
367 * rendering can be done directly to output buffers (e.g. USB URBs).
368 * The function fills the supplied command buffer, providing information
369 * on where it left off, so the client may call in again with additional
370 * buffers if the line will take several buffers to complete.
371 *
372 * A single command can transmit a maximum of 256 pixels,
373 * regardless of the compression ratio (protocol design limit).
374 * To the hardware, 0 for a size byte means 256
Bernie Thompson2685cff2010-09-05 18:29:56 -0700375 *
Pavel Machek3b7b31f2010-04-03 07:00:37 +0200376 * Rather than 256 pixel commands which are either rl or raw encoded,
377 * the rlx command simply assumes alternating raw and rl spans within one cmd.
378 * This has a slightly larger header overhead, but produces more even results.
379 * It also processes all data (read and write) in a single pass.
380 * Performance benchmarks of common cases show it having just slightly better
Bernie Thompson2685cff2010-09-05 18:29:56 -0700381 * compression than 256 pixel raw or rle commands, with similar CPU consumpion.
Pavel Machek3b7b31f2010-04-03 07:00:37 +0200382 * But for very rl friendly data, will compress not quite as well.
383 */
Bernie Thompson530f43a2010-02-15 06:46:21 -0800384static void dlfb_compress_hline(
385 const uint16_t **pixel_start_ptr,
386 const uint16_t *const pixel_end,
387 uint32_t *device_address_ptr,
388 uint8_t **command_buffer_ptr,
389 const uint8_t *const cmd_buffer_end)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700390{
Bernie Thompson530f43a2010-02-15 06:46:21 -0800391 const uint16_t *pixel = *pixel_start_ptr;
392 uint32_t dev_addr = *device_address_ptr;
393 uint8_t *cmd = *command_buffer_ptr;
394 const int bpp = 2;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700395
Bernie Thompson530f43a2010-02-15 06:46:21 -0800396 while ((pixel_end > pixel) &&
397 (cmd_buffer_end - MIN_RLX_CMD_BYTES > cmd)) {
398 uint8_t *raw_pixels_count_byte = 0;
399 uint8_t *cmd_pixels_count_byte = 0;
400 const uint16_t *raw_pixel_start = 0;
401 const uint16_t *cmd_pixel_start, *cmd_pixel_end = 0;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700402
Bernie Thompson530f43a2010-02-15 06:46:21 -0800403 prefetchw((void *) cmd); /* pull in one cache line at least */
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700404
Bernie Thompson530f43a2010-02-15 06:46:21 -0800405 *cmd++ = 0xAF;
406 *cmd++ = 0x6B;
Bernie Thompson1572f912010-09-05 16:35:27 -0700407 *cmd++ = (uint8_t) ((dev_addr >> 16) & 0xFF);
408 *cmd++ = (uint8_t) ((dev_addr >> 8) & 0xFF);
409 *cmd++ = (uint8_t) ((dev_addr) & 0xFF);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700410
Bernie Thompson530f43a2010-02-15 06:46:21 -0800411 cmd_pixels_count_byte = cmd++; /* we'll know this later */
412 cmd_pixel_start = pixel;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700413
Bernie Thompson530f43a2010-02-15 06:46:21 -0800414 raw_pixels_count_byte = cmd++; /* we'll know this later */
415 raw_pixel_start = pixel;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700416
Bernie Thompson530f43a2010-02-15 06:46:21 -0800417 cmd_pixel_end = pixel + min(MAX_CMD_PIXELS + 1,
418 min((int)(pixel_end - pixel),
419 (int)(cmd_buffer_end - cmd) / bpp));
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700420
Bernie Thompson530f43a2010-02-15 06:46:21 -0800421 prefetch_range((void *) pixel, (cmd_pixel_end - pixel) * bpp);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700422
Bernie Thompson530f43a2010-02-15 06:46:21 -0800423 while (pixel < cmd_pixel_end) {
424 const uint16_t * const repeating_pixel = pixel;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700425
Bernie Thompson530f43a2010-02-15 06:46:21 -0800426 *(uint16_t *)cmd = cpu_to_be16p(pixel);
427 cmd += 2;
428 pixel++;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700429
Bernie Thompson530f43a2010-02-15 06:46:21 -0800430 if (unlikely((pixel < cmd_pixel_end) &&
431 (*pixel == *repeating_pixel))) {
432 /* go back and fill in raw pixel count */
433 *raw_pixels_count_byte = ((repeating_pixel -
434 raw_pixel_start) + 1) & 0xFF;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700435
Bernie Thompson530f43a2010-02-15 06:46:21 -0800436 while ((pixel < cmd_pixel_end)
437 && (*pixel == *repeating_pixel)) {
438 pixel++;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700439 }
Bernie Thompson59277b62009-11-24 15:52:21 -0800440
Bernie Thompson530f43a2010-02-15 06:46:21 -0800441 /* immediately after raw data is repeat byte */
442 *cmd++ = ((pixel - repeating_pixel) - 1) & 0xFF;
Bernie Thompson59277b62009-11-24 15:52:21 -0800443
Bernie Thompson530f43a2010-02-15 06:46:21 -0800444 /* Then start another raw pixel span */
445 raw_pixel_start = pixel;
446 raw_pixels_count_byte = cmd++;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700447 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700448 }
449
Bernie Thompson530f43a2010-02-15 06:46:21 -0800450 if (pixel > raw_pixel_start) {
451 /* finalize last RAW span */
452 *raw_pixels_count_byte = (pixel-raw_pixel_start) & 0xFF;
453 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700454
Bernie Thompson530f43a2010-02-15 06:46:21 -0800455 *cmd_pixels_count_byte = (pixel - cmd_pixel_start) & 0xFF;
456 dev_addr += (pixel - cmd_pixel_start) * bpp;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700457 }
458
Bernie Thompson530f43a2010-02-15 06:46:21 -0800459 if (cmd_buffer_end <= MIN_RLX_CMD_BYTES + cmd) {
460 /* Fill leftover bytes with no-ops */
461 if (cmd_buffer_end > cmd)
462 memset(cmd, 0xAF, cmd_buffer_end - cmd);
463 cmd = (uint8_t *) cmd_buffer_end;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700464 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700465
Bernie Thompson530f43a2010-02-15 06:46:21 -0800466 *command_buffer_ptr = cmd;
467 *pixel_start_ptr = pixel;
468 *device_address_ptr = dev_addr;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700469
Bernie Thompson530f43a2010-02-15 06:46:21 -0800470 return;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700471}
472
Bernie Thompson530f43a2010-02-15 06:46:21 -0800473/*
474 * There are 3 copies of every pixel: The front buffer that the fbdev
475 * client renders to, the actual framebuffer across the USB bus in hardware
476 * (that we can only write to, slowly, and can never read), and (optionally)
477 * our shadow copy that tracks what's been sent to that hardware buffer.
478 */
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700479static int dlfb_render_hline(struct dlfb_data *dev, struct urb **urb_ptr,
Bernie Thompson530f43a2010-02-15 06:46:21 -0800480 const char *front, char **urb_buf_ptr,
481 u32 byte_offset, u32 byte_width,
482 int *ident_ptr, int *sent_ptr)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700483{
Bernie Thompson530f43a2010-02-15 06:46:21 -0800484 const u8 *line_start, *line_end, *next_pixel;
485 u32 dev_addr = dev->base16 + byte_offset;
486 struct urb *urb = *urb_ptr;
487 u8 *cmd = *urb_buf_ptr;
488 u8 *cmd_end = (u8 *) urb->transfer_buffer + urb->transfer_buffer_length;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700489
Bernie Thompson530f43a2010-02-15 06:46:21 -0800490 line_start = (u8 *) (front + byte_offset);
491 next_pixel = line_start;
492 line_end = next_pixel + byte_width;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700493
Bernie Thompson530f43a2010-02-15 06:46:21 -0800494 if (dev->backing_buffer) {
495 int offset;
496 const u8 *back_start = (u8 *) (dev->backing_buffer
497 + byte_offset);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700498
Bernie Thompson530f43a2010-02-15 06:46:21 -0800499 *ident_ptr += dlfb_trim_hline(back_start, &next_pixel,
500 &byte_width);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700501
Bernie Thompson530f43a2010-02-15 06:46:21 -0800502 offset = next_pixel - line_start;
503 line_end = next_pixel + byte_width;
504 dev_addr += offset;
505 back_start += offset;
506 line_start += offset;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700507
Bernie Thompson530f43a2010-02-15 06:46:21 -0800508 memcpy((char *)back_start, (char *) line_start,
509 byte_width);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700510 }
511
Bernie Thompson530f43a2010-02-15 06:46:21 -0800512 while (next_pixel < line_end) {
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700513
Bernie Thompson530f43a2010-02-15 06:46:21 -0800514 dlfb_compress_hline((const uint16_t **) &next_pixel,
515 (const uint16_t *) line_end, &dev_addr,
516 (u8 **) &cmd, (u8 *) cmd_end);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700517
Bernie Thompson530f43a2010-02-15 06:46:21 -0800518 if (cmd >= cmd_end) {
519 int len = cmd - (u8 *) urb->transfer_buffer;
520 if (dlfb_submit_urb(dev, urb, len))
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700521 return 1; /* lost pixels is set */
Bernie Thompson530f43a2010-02-15 06:46:21 -0800522 *sent_ptr += len;
523 urb = dlfb_get_urb(dev);
524 if (!urb)
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700525 return 1; /* lost_pixels is set */
Bernie Thompson530f43a2010-02-15 06:46:21 -0800526 *urb_ptr = urb;
527 cmd = urb->transfer_buffer;
528 cmd_end = &cmd[urb->transfer_buffer_length];
529 }
530 }
531
532 *urb_buf_ptr = cmd;
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700533
534 return 0;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700535}
536
Bernie Thompson530f43a2010-02-15 06:46:21 -0800537int dlfb_handle_damage(struct dlfb_data *dev, int x, int y,
538 int width, int height, char *data)
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700539{
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700540 int i, ret;
Bernie Thompson530f43a2010-02-15 06:46:21 -0800541 char *cmd;
542 cycles_t start_cycles, end_cycles;
543 int bytes_sent = 0;
544 int bytes_identical = 0;
545 struct urb *urb;
546 int aligned_x;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700547
Bernie Thompson530f43a2010-02-15 06:46:21 -0800548 start_cycles = get_cycles();
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700549
Bernie Thompson530f43a2010-02-15 06:46:21 -0800550 aligned_x = DL_ALIGN_DOWN(x, sizeof(unsigned long));
551 width = DL_ALIGN_UP(width + (x-aligned_x), sizeof(unsigned long));
552 x = aligned_x;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700553
Bernie Thompson530f43a2010-02-15 06:46:21 -0800554 if ((width <= 0) ||
555 (x + width > dev->info->var.xres) ||
556 (y + height > dev->info->var.yres))
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700557 return -EINVAL;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700558
Bernie Thompson530f43a2010-02-15 06:46:21 -0800559 if (!atomic_read(&dev->usb_active))
560 return 0;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700561
Bernie Thompson530f43a2010-02-15 06:46:21 -0800562 urb = dlfb_get_urb(dev);
563 if (!urb)
564 return 0;
565 cmd = urb->transfer_buffer;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700566
Bernie Thompson530f43a2010-02-15 06:46:21 -0800567 for (i = y; i < y + height ; i++) {
568 const int line_offset = dev->info->fix.line_length * i;
569 const int byte_offset = line_offset + (x * BPP);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700570
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700571 if (dlfb_render_hline(dev, &urb,
572 (char *) dev->info->fix.smem_start,
Bernie Thompson2685cff2010-09-05 18:29:56 -0700573 &cmd, byte_offset, width * BPP,
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700574 &bytes_identical, &bytes_sent))
575 goto error;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700576 }
577
Bernie Thompson530f43a2010-02-15 06:46:21 -0800578 if (cmd > (char *) urb->transfer_buffer) {
579 /* Send partial buffer remaining before exiting */
580 int len = cmd - (char *) urb->transfer_buffer;
581 ret = dlfb_submit_urb(dev, urb, len);
582 bytes_sent += len;
583 } else
584 dlfb_urb_completion(urb);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700585
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700586error:
Bernie Thompson530f43a2010-02-15 06:46:21 -0800587 atomic_add(bytes_sent, &dev->bytes_sent);
588 atomic_add(bytes_identical, &dev->bytes_identical);
589 atomic_add(width*height*2, &dev->bytes_rendered);
590 end_cycles = get_cycles();
591 atomic_add(((unsigned int) ((end_cycles - start_cycles)
592 >> 10)), /* Kcycles */
593 &dev->cpu_kcycles_used);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700594
Bernie Thompson530f43a2010-02-15 06:46:21 -0800595 return 0;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700596}
597
Bernie Thompsond46ecb92010-09-05 16:35:04 -0700598static ssize_t dlfb_ops_read(struct fb_info *info, char __user *buf,
599 size_t count, loff_t *ppos)
600{
601 ssize_t result = -ENOSYS;
602
603#if defined CONFIG_FB_SYS_FOPS || defined CONFIG_FB_SYS_FOPS_MODULE
604 result = fb_sys_read(info, buf, count, ppos);
605#endif
606
607 return result;
608}
609
610/*
611 * Path triggered by usermode clients who write to filesystem
612 * e.g. cat filename > /dev/fb1
613 * Not used by X Windows or text-mode console. But useful for testing.
614 * Slow because of extra copy and we must assume all pixels dirty.
615 */
616static ssize_t dlfb_ops_write(struct fb_info *info, const char __user *buf,
617 size_t count, loff_t *ppos)
618{
619 ssize_t result = -ENOSYS;
620 struct dlfb_data *dev = info->par;
621 u32 offset = (u32) *ppos;
622
623#if defined CONFIG_FB_SYS_FOPS || defined CONFIG_FB_SYS_FOPS_MODULE
624
625 result = fb_sys_write(info, buf, count, ppos);
626
627 if (result > 0) {
628 int start = max((int)(offset / info->fix.line_length) - 1, 0);
629 int lines = min((u32)((result / info->fix.line_length) + 1),
630 (u32)info->var.yres);
631
632 dlfb_handle_damage(dev, 0, start, info->var.xres,
633 lines, info->screen_base);
634 }
635#endif
636
637 return result;
638}
639
Bernie Thompson530f43a2010-02-15 06:46:21 -0800640/* hardware has native COPY command (see libdlo), but not worth it for fbcon */
Bernie Thompson45742032010-02-15 06:46:04 -0800641static void dlfb_ops_copyarea(struct fb_info *info,
Bernie Thompson530f43a2010-02-15 06:46:21 -0800642 const struct fb_copyarea *area)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700643{
Bernie Thompson530f43a2010-02-15 06:46:21 -0800644
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700645 struct dlfb_data *dev = info->par;
646
Bernie Thompson530f43a2010-02-15 06:46:21 -0800647#if defined CONFIG_FB_SYS_COPYAREA || defined CONFIG_FB_SYS_COPYAREA_MODULE
648
649 sys_copyarea(info, area);
650
651 dlfb_handle_damage(dev, area->dx, area->dy,
652 area->width, area->height, info->screen_base);
653#endif
Bernie Thompson530f43a2010-02-15 06:46:21 -0800654
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700655}
656
Bernie Thompson45742032010-02-15 06:46:04 -0800657static void dlfb_ops_imageblit(struct fb_info *info,
Bernie Thompson530f43a2010-02-15 06:46:21 -0800658 const struct fb_image *image)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700659{
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700660 struct dlfb_data *dev = info->par;
Bernie Thompson530f43a2010-02-15 06:46:21 -0800661
662#if defined CONFIG_FB_SYS_IMAGEBLIT || defined CONFIG_FB_SYS_IMAGEBLIT_MODULE
663
664 sys_imageblit(info, image);
665
666 dlfb_handle_damage(dev, image->dx, image->dy,
667 image->width, image->height, info->screen_base);
668
669#endif
670
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700671}
672
Bernie Thompson45742032010-02-15 06:46:04 -0800673static void dlfb_ops_fillrect(struct fb_info *info,
Bernie Thompson530f43a2010-02-15 06:46:21 -0800674 const struct fb_fillrect *rect)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700675{
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700676 struct dlfb_data *dev = info->par;
677
Bernie Thompson530f43a2010-02-15 06:46:21 -0800678#if defined CONFIG_FB_SYS_FILLRECT || defined CONFIG_FB_SYS_FILLRECT_MODULE
679
680 sys_fillrect(info, rect);
681
682 dlfb_handle_damage(dev, rect->dx, rect->dy, rect->width,
683 rect->height, info->screen_base);
684#endif
685
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700686}
687
Bernie Thompsond5ed5432010-09-05 16:35:39 -0700688#ifdef CONFIG_FB_DEFERRED_IO
689/*
690 * NOTE: fb_defio.c is holding info->fbdefio.mutex
691 * Touching ANY framebuffer memory that triggers a page fault
692 * in fb_defio will cause a deadlock, when it also tries to
693 * grab the same mutex.
694 */
695static void dlfb_dpy_deferred_io(struct fb_info *info,
696 struct list_head *pagelist)
697{
698 struct page *cur;
699 struct fb_deferred_io *fbdefio = info->fbdefio;
700 struct dlfb_data *dev = info->par;
701 struct urb *urb;
702 char *cmd;
703 cycles_t start_cycles, end_cycles;
704 int bytes_sent = 0;
705 int bytes_identical = 0;
706 int bytes_rendered = 0;
707
708 if (!fb_defio)
709 return;
710
711 if (!atomic_read(&dev->usb_active))
712 return;
713
714 start_cycles = get_cycles();
715
716 urb = dlfb_get_urb(dev);
717 if (!urb)
718 return;
719
720 cmd = urb->transfer_buffer;
721
722 /* walk the written page list and render each to device */
723 list_for_each_entry(cur, &fbdefio->pagelist, lru) {
724
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700725 if (dlfb_render_hline(dev, &urb, (char *) info->fix.smem_start,
Bernie Thompsond5ed5432010-09-05 16:35:39 -0700726 &cmd, cur->index << PAGE_SHIFT,
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700727 PAGE_SIZE, &bytes_identical, &bytes_sent))
728 goto error;
Bernie Thompsond5ed5432010-09-05 16:35:39 -0700729 bytes_rendered += PAGE_SIZE;
730 }
731
732 if (cmd > (char *) urb->transfer_buffer) {
733 /* Send partial buffer remaining before exiting */
734 int len = cmd - (char *) urb->transfer_buffer;
735 dlfb_submit_urb(dev, urb, len);
736 bytes_sent += len;
737 } else
738 dlfb_urb_completion(urb);
739
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700740error:
Bernie Thompsond5ed5432010-09-05 16:35:39 -0700741 atomic_add(bytes_sent, &dev->bytes_sent);
742 atomic_add(bytes_identical, &dev->bytes_identical);
743 atomic_add(bytes_rendered, &dev->bytes_rendered);
744 end_cycles = get_cycles();
745 atomic_add(((unsigned int) ((end_cycles - start_cycles)
746 >> 10)), /* Kcycles */
747 &dev->cpu_kcycles_used);
748}
749
750#endif
751
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700752static int dlfb_get_edid(struct dlfb_data *dev, char *edid, int len)
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800753{
754 int i;
755 int ret;
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700756 char *rbuf;
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800757
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700758 rbuf = kmalloc(2, GFP_KERNEL);
759 if (!rbuf)
760 return 0;
761
762 for (i = 0; i < len; i++) {
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800763 ret = usb_control_msg(dev->udev,
764 usb_rcvctrlpipe(dev->udev, 0), (0x02),
765 (0x80 | (0x02 << 5)), i << 8, 0xA1, rbuf, 2,
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700766 HZ);
767 if (ret < 1) {
768 dl_err("Read EDID byte %d failed err %x\n", i, ret);
769 i--;
770 break;
771 }
772 edid[i] = rbuf[1];
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800773 }
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700774
775 kfree(rbuf);
776
777 return i;
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800778}
779
Bernie Thompson45742032010-02-15 06:46:04 -0800780static int dlfb_ops_ioctl(struct fb_info *info, unsigned int cmd,
Bernie Thompson530f43a2010-02-15 06:46:21 -0800781 unsigned long arg)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700782{
Bernie Thompson530f43a2010-02-15 06:46:21 -0800783
784 struct dlfb_data *dev = info->par;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700785 struct dloarea *area = NULL;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700786
Bernie Thompson530f43a2010-02-15 06:46:21 -0800787 if (!atomic_read(&dev->usb_active))
788 return 0;
789
790 /* TODO: Update X server to get this from sysfs instead */
791 if (cmd == DLFB_IOCTL_RETURN_EDID) {
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700792 char *edid = (char *)arg;
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700793 if (copy_to_user(edid, dev->edid, dev->edid_size))
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700794 return -EFAULT;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700795 return 0;
796 }
797
Bernie Thompson530f43a2010-02-15 06:46:21 -0800798 /* TODO: Help propose a standard fb.h ioctl to report mmap damage */
799 if (cmd == DLFB_IOCTL_REPORT_DAMAGE) {
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700800
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700801 /*
802 * If we have a damage-aware client, turn fb_defio "off"
803 * To avoid perf imact of unecessary page fault handling.
804 * Done by resetting the delay for this fb_info to a very
805 * long period. Pages will become writable and stay that way.
806 * Reset to normal value when all clients have closed this fb.
807 */
808 if (info->fbdefio)
809 info->fbdefio->delay = DL_DEFIO_WRITE_DISABLE;
810
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700811 area = (struct dloarea *)arg;
812
813 if (area->x < 0)
814 area->x = 0;
815
816 if (area->x > info->var.xres)
817 area->x = info->var.xres;
818
819 if (area->y < 0)
820 area->y = 0;
821
822 if (area->y > info->var.yres)
823 area->y = info->var.yres;
824
Bernie Thompson530f43a2010-02-15 06:46:21 -0800825 dlfb_handle_damage(dev, area->x, area->y, area->w, area->h,
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700826 info->screen_base);
827 }
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700828
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700829 return 0;
830}
831
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700832/* taken from vesafb */
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700833static int
Bernie Thompson45742032010-02-15 06:46:04 -0800834dlfb_ops_setcolreg(unsigned regno, unsigned red, unsigned green,
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700835 unsigned blue, unsigned transp, struct fb_info *info)
836{
837 int err = 0;
838
839 if (regno >= info->cmap.len)
840 return 1;
841
842 if (regno < 16) {
843 if (info->var.red.offset == 10) {
844 /* 1:5:5:5 */
845 ((u32 *) (info->pseudo_palette))[regno] =
846 ((red & 0xf800) >> 1) |
847 ((green & 0xf800) >> 6) | ((blue & 0xf800) >> 11);
848 } else {
849 /* 0:5:6:5 */
850 ((u32 *) (info->pseudo_palette))[regno] =
851 ((red & 0xf800)) |
852 ((green & 0xfc00) >> 5) | ((blue & 0xf800) >> 11);
853 }
854 }
855
856 return err;
857}
858
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800859/*
860 * It's common for several clients to have framebuffer open simultaneously.
861 * e.g. both fbcon and X. Makes things interesting.
Bernie Thompson33077b82010-09-05 16:35:19 -0700862 * Assumes caller is holding info->lock (for open and release at least)
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800863 */
864static int dlfb_ops_open(struct fb_info *info, int user)
865{
866 struct dlfb_data *dev = info->par;
867
Bernie Thompsond5ed5432010-09-05 16:35:39 -0700868 /*
869 * fbcon aggressively connects to first framebuffer it finds,
870 * preventing other clients (X) from working properly. Usually
871 * not what the user wants. Fail by default with option to enable.
872 */
873 if ((user == 0) & (!console))
874 return -EBUSY;
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800875
Bernie Thompson33077b82010-09-05 16:35:19 -0700876 /* If the USB device is gone, we don't accept new opens */
877 if (dev->virtualized)
878 return -ENODEV;
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800879
880 dev->fb_count++;
881
Bernie Thompson33077b82010-09-05 16:35:19 -0700882 kref_get(&dev->kref);
883
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800884#ifdef CONFIG_FB_DEFERRED_IO
Bernie Thompsond5ed5432010-09-05 16:35:39 -0700885 if (fb_defio && (info->fbdefio == NULL)) {
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700886 /* enable defio at last moment if not disabled by client */
887
888 struct fb_deferred_io *fbdefio;
889
890 fbdefio = kmalloc(GFP_KERNEL, sizeof(struct fb_deferred_io));
891
892 if (fbdefio) {
893 fbdefio->delay = DL_DEFIO_WRITE_DELAY;
894 fbdefio->deferred_io = dlfb_dpy_deferred_io;
895 }
896
897 info->fbdefio = fbdefio;
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800898 fb_deferred_io_init(info);
899 }
900#endif
901
902 dl_notice("open /dev/fb%d user=%d fb_info=%p count=%d\n",
903 info->node, user, info, dev->fb_count);
904
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700905 return 0;
906}
907
Bernie Thompson4a4854d2010-02-15 06:45:55 -0800908/*
909 * Called when all client interfaces to start transactions have been disabled,
910 * and all references to our device instance (dlfb_data) are released.
911 * Every transaction must have a reference, so we know are fully spun down
912 */
Bernie Thompson33077b82010-09-05 16:35:19 -0700913static void dlfb_free(struct kref *kref)
Bernie Thompson4a4854d2010-02-15 06:45:55 -0800914{
915 struct dlfb_data *dev = container_of(kref, struct dlfb_data, kref);
916
Bernie Thompson33077b82010-09-05 16:35:19 -0700917 /* this function will wait for all in-flight urbs to complete */
918 if (dev->urbs.count > 0)
919 dlfb_free_urb_list(dev);
920
Bernie Thompson4a4854d2010-02-15 06:45:55 -0800921 if (dev->backing_buffer)
922 vfree(dev->backing_buffer);
923
Bernie Thompson33077b82010-09-05 16:35:19 -0700924 kfree(dev->edid);
925
926 dl_warn("freeing dlfb_data %p\n", dev);
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800927
Bernie Thompson4a4854d2010-02-15 06:45:55 -0800928 kfree(dev);
929}
930
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700931static void dlfb_release_urb_work(struct work_struct *work)
932{
933 struct urb_node *unode = container_of(work, struct urb_node,
934 release_urb_work.work);
935
936 up(&unode->dev->urbs.limit_sem);
937}
Bernie Thompson33077b82010-09-05 16:35:19 -0700938
939static void dlfb_free_framebuffer_work(struct work_struct *work)
Bernie Thompson2469d5d2010-02-15 06:46:13 -0800940{
Bernie Thompson33077b82010-09-05 16:35:19 -0700941 struct dlfb_data *dev = container_of(work, struct dlfb_data,
942 free_framebuffer_work.work);
943 struct fb_info *info = dev->info;
944 int node = info->node;
945
946 unregister_framebuffer(info);
Bernie Thompson2469d5d2010-02-15 06:46:13 -0800947
948 if (info->cmap.len != 0)
949 fb_dealloc_cmap(&info->cmap);
950 if (info->monspecs.modedb)
951 fb_destroy_modedb(info->monspecs.modedb);
952 if (info->screen_base)
953 vfree(info->screen_base);
954
955 fb_destroy_modelist(&info->modelist);
956
Bernie Thompson33077b82010-09-05 16:35:19 -0700957 dev->info = 0;
958
959 /* Assume info structure is freed after this point */
Bernie Thompson2469d5d2010-02-15 06:46:13 -0800960 framebuffer_release(info);
961
Bernie Thompson33077b82010-09-05 16:35:19 -0700962 dl_warn("fb_info for /dev/fb%d has been freed\n", node);
963
964 /* ref taken in probe() as part of registering framebfufer */
965 kref_put(&dev->kref, dlfb_free);
966}
967
968/*
969 * Assumes caller is holding info->lock mutex (for open and release at least)
970 */
971static int dlfb_ops_release(struct fb_info *info, int user)
972{
973 struct dlfb_data *dev = info->par;
974
975 dev->fb_count--;
976
977 /* We can't free fb_info here - fbmem will touch it when we return */
978 if (dev->virtualized && (dev->fb_count == 0))
979 schedule_delayed_work(&dev->free_framebuffer_work, HZ);
980
981#ifdef CONFIG_FB_DEFERRED_IO
982 if ((dev->fb_count == 0) && (info->fbdefio)) {
983 fb_deferred_io_cleanup(info);
984 kfree(info->fbdefio);
985 info->fbdefio = NULL;
986 info->fbops->fb_mmap = dlfb_ops_mmap;
987 }
988#endif
989
990 dl_warn("released /dev/fb%d user=%d count=%d\n",
991 info->node, user, dev->fb_count);
992
993 kref_put(&dev->kref, dlfb_free);
994
995 return 0;
Bernie Thompson2469d5d2010-02-15 06:46:13 -0800996}
997
998/*
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800999 * Check whether a video mode is supported by the DisplayLink chip
1000 * We start from monitor's modes, so don't need to filter that here
1001 */
1002static int dlfb_is_valid_mode(struct fb_videomode *mode,
1003 struct fb_info *info)
1004{
1005 struct dlfb_data *dev = info->par;
1006
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001007 if (mode->xres * mode->yres > dev->sku_pixel_limit) {
1008 dl_warn("%dx%d beyond chip capabilities\n",
1009 mode->xres, mode->yres);
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001010 return 0;
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001011 }
1012
1013 dl_info("%dx%d valid mode\n", mode->xres, mode->yres);
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001014
1015 return 1;
1016}
1017
1018static void dlfb_var_color_format(struct fb_var_screeninfo *var)
1019{
1020 const struct fb_bitfield red = { 11, 5, 0 };
1021 const struct fb_bitfield green = { 5, 6, 0 };
1022 const struct fb_bitfield blue = { 0, 5, 0 };
1023
1024 var->bits_per_pixel = 16;
1025 var->red = red;
1026 var->green = green;
1027 var->blue = blue;
1028}
1029
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001030static int dlfb_ops_check_var(struct fb_var_screeninfo *var,
1031 struct fb_info *info)
1032{
1033 struct fb_videomode mode;
1034
1035 /* TODO: support dynamically changing framebuffer size */
1036 if ((var->xres * var->yres * 2) > info->fix.smem_len)
1037 return -EINVAL;
1038
1039 /* set device-specific elements of var unrelated to mode */
1040 dlfb_var_color_format(var);
1041
1042 fb_var_to_videomode(&mode, var);
1043
1044 if (!dlfb_is_valid_mode(&mode, info))
1045 return -EINVAL;
1046
1047 return 0;
1048}
1049
1050static int dlfb_ops_set_par(struct fb_info *info)
1051{
1052 struct dlfb_data *dev = info->par;
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001053 int result;
1054 u16 *pix_framebuffer;
1055 int i;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001056
1057 dl_notice("set_par mode %dx%d\n", info->var.xres, info->var.yres);
1058
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001059 result = dlfb_set_video_mode(dev, &info->var);
1060
1061 if ((result == 0) && (dev->fb_count == 0)) {
1062
1063 /* paint greenscreen */
1064
1065 pix_framebuffer = (u16 *) info->screen_base;
1066 for (i = 0; i < info->fix.smem_len / 2; i++)
1067 pix_framebuffer[i] = 0x37e6;
1068
1069 dlfb_handle_damage(dev, 0, 0, info->var.xres, info->var.yres,
1070 info->screen_base);
1071 }
1072
1073 return result;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001074}
1075
Bernie Thompson9825f702010-09-05 16:35:10 -07001076/*
1077 * In order to come back from full DPMS off, we need to set the mode again
1078 */
Bernie Thompson45742032010-02-15 06:46:04 -08001079static int dlfb_ops_blank(int blank_mode, struct fb_info *info)
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -07001080{
Bernie Thompson530f43a2010-02-15 06:46:21 -08001081 struct dlfb_data *dev = info->par;
Roberto De Ioris7316bc52009-06-10 23:02:19 -07001082
Roberto De Ioris7316bc52009-06-10 23:02:19 -07001083 if (blank_mode != FB_BLANK_UNBLANK) {
Bernie Thompson9825f702010-09-05 16:35:10 -07001084 char *bufptr;
1085 struct urb *urb;
Roberto De Ioris7316bc52009-06-10 23:02:19 -07001086
Bernie Thompson9825f702010-09-05 16:35:10 -07001087 urb = dlfb_get_urb(dev);
1088 if (!urb)
1089 return 0;
1090
1091 bufptr = (char *) urb->transfer_buffer;
1092 bufptr = dlfb_vidreg_lock(bufptr);
1093 bufptr = dlfb_enable_hvsync(bufptr, false);
1094 bufptr = dlfb_vidreg_unlock(bufptr);
1095
1096 dlfb_submit_urb(dev, urb, bufptr -
1097 (char *) urb->transfer_buffer);
1098 } else {
1099 dlfb_set_video_mode(dev, &info->var);
1100 }
Roberto De Ioris7316bc52009-06-10 23:02:19 -07001101
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001102 return 0;
1103}
1104
1105static struct fb_ops dlfb_ops = {
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001106 .owner = THIS_MODULE,
Bernie Thompsond46ecb92010-09-05 16:35:04 -07001107 .fb_read = dlfb_ops_read,
1108 .fb_write = dlfb_ops_write,
Bernie Thompson45742032010-02-15 06:46:04 -08001109 .fb_setcolreg = dlfb_ops_setcolreg,
1110 .fb_fillrect = dlfb_ops_fillrect,
1111 .fb_copyarea = dlfb_ops_copyarea,
1112 .fb_imageblit = dlfb_ops_imageblit,
1113 .fb_mmap = dlfb_ops_mmap,
1114 .fb_ioctl = dlfb_ops_ioctl,
Bernie Thompson3e8f3d62010-02-15 06:46:26 -08001115 .fb_open = dlfb_ops_open,
Bernie Thompson45742032010-02-15 06:46:04 -08001116 .fb_release = dlfb_ops_release,
1117 .fb_blank = dlfb_ops_blank,
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001118 .fb_check_var = dlfb_ops_check_var,
1119 .fb_set_par = dlfb_ops_set_par,
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001120};
1121
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001122
Bernie Thompsoncc403dc2010-02-15 06:45:49 -08001123/*
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001124 * Assumes &info->lock held by caller
1125 * Assumes no active clients have framebuffer open
1126 */
1127static int dlfb_realloc_framebuffer(struct dlfb_data *dev, struct fb_info *info)
1128{
1129 int retval = -ENOMEM;
1130 int old_len = info->fix.smem_len;
1131 int new_len;
1132 unsigned char *old_fb = info->screen_base;
1133 unsigned char *new_fb;
1134 unsigned char *new_back;
1135
1136 dl_warn("Reallocating framebuffer. Addresses will change!\n");
1137
1138 new_len = info->fix.line_length * info->var.yres;
1139
1140 if (PAGE_ALIGN(new_len) > old_len) {
1141 /*
1142 * Alloc system memory for virtual framebuffer
1143 */
1144 new_fb = vmalloc(new_len);
1145 if (!new_fb) {
1146 dl_err("Virtual framebuffer alloc failed\n");
1147 goto error;
1148 }
1149
1150 if (info->screen_base) {
1151 memcpy(new_fb, old_fb, old_len);
1152 vfree(info->screen_base);
1153 }
1154
1155 info->screen_base = new_fb;
1156 info->fix.smem_len = PAGE_ALIGN(new_len);
1157 info->fix.smem_start = (unsigned long) new_fb;
1158 info->flags = udlfb_info_flags;
1159
1160 /*
1161 * Second framebuffer copy to mirror the framebuffer state
1162 * on the physical USB device. We can function without this.
1163 * But with imperfect damage info we may send pixels over USB
1164 * that were, in fact, unchanged - wasting limited USB bandwidth
1165 */
Joe Perches5b84cc72010-11-04 20:07:59 -07001166 new_back = vzalloc(new_len);
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001167 if (!new_back)
Joe Perches5b84cc72010-11-04 20:07:59 -07001168 dl_info("No shadow/backing buffer allocated\n");
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001169 else {
1170 if (dev->backing_buffer)
1171 vfree(dev->backing_buffer);
1172 dev->backing_buffer = new_back;
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001173 }
1174 }
1175
1176 retval = 0;
1177
1178error:
1179 return retval;
1180}
1181
1182/*
1183 * 1) Get EDID from hw, or use sw default
1184 * 2) Parse into various fb_info structs
1185 * 3) Allocate virtual framebuffer memory to back highest res mode
1186 *
1187 * Parses EDID into three places used by various parts of fbdev:
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001188 * fb_var_screeninfo contains the timing of the monitor's preferred mode
1189 * fb_info.monspecs is full parsed EDID info, including monspecs.modedb
1190 * fb_info.modelist is a linked list of all monitor & VESA modes which work
1191 *
1192 * If EDID is not readable/valid, then modelist is all VESA modes,
1193 * monspecs is NULL, and fb_var_screeninfo is set to safe VESA mode
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001194 * Returns 0 if successful
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001195 */
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001196static int dlfb_setup_modes(struct dlfb_data *dev,
1197 struct fb_info *info,
1198 char *default_edid, size_t default_edid_size)
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001199{
1200 int i;
1201 const struct fb_videomode *default_vmode = NULL;
1202 int result = 0;
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001203 char *edid;
1204 int tries = 3;
1205
1206 if (info->dev) /* only use mutex if info has been registered */
1207 mutex_lock(&info->lock);
1208
1209 edid = kmalloc(MAX_EDID_SIZE, GFP_KERNEL);
1210 if (!edid) {
1211 result = -ENOMEM;
1212 goto error;
1213 }
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001214
1215 fb_destroy_modelist(&info->modelist);
1216 memset(&info->monspecs, 0, sizeof(info->monspecs));
1217
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001218 /*
1219 * Try to (re)read EDID from hardware first
1220 * EDID data may return, but not parse as valid
1221 * Try again a few times, in case of e.g. analog cable noise
1222 */
1223 while (tries--) {
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001224
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001225 i = dlfb_get_edid(dev, edid, MAX_EDID_SIZE);
1226
1227 if (i >= MIN_EDID_SIZE)
1228 fb_edid_to_monspecs(edid, &info->monspecs);
1229
1230 if (info->monspecs.modedb_len > 0) {
1231 dev->edid = edid;
1232 dev->edid_size = i;
1233 break;
1234 }
1235 }
1236
1237 /* If that fails, use a previously returned EDID if available */
1238 if (info->monspecs.modedb_len == 0) {
1239
1240 dl_err("Unable to get valid EDID from device/display\n");
1241
1242 if (dev->edid) {
1243 fb_edid_to_monspecs(dev->edid, &info->monspecs);
1244 if (info->monspecs.modedb_len > 0)
1245 dl_err("Using previously queried EDID\n");
1246 }
1247 }
1248
1249 /* If that fails, use the default EDID we were handed */
1250 if (info->monspecs.modedb_len == 0) {
1251 if (default_edid_size >= MIN_EDID_SIZE) {
1252 fb_edid_to_monspecs(default_edid, &info->monspecs);
1253 if (info->monspecs.modedb_len > 0) {
1254 memcpy(edid, default_edid, default_edid_size);
1255 dev->edid = edid;
1256 dev->edid_size = default_edid_size;
1257 dl_err("Using default/backup EDID\n");
1258 }
1259 }
1260 }
1261
1262 /* If we've got modes, let's pick a best default mode */
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001263 if (info->monspecs.modedb_len > 0) {
1264
1265 for (i = 0; i < info->monspecs.modedb_len; i++) {
1266 if (dlfb_is_valid_mode(&info->monspecs.modedb[i], info))
1267 fb_add_videomode(&info->monspecs.modedb[i],
1268 &info->modelist);
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001269 else /* if we've removed top/best mode */
1270 info->monspecs.misc &= ~FB_MISC_1ST_DETAIL;
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001271 }
1272
1273 default_vmode = fb_find_best_display(&info->monspecs,
1274 &info->modelist);
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001275 }
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001276
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001277 /* If everything else has failed, fall back to safe default mode */
1278 if (default_vmode == NULL) {
1279
1280 struct fb_videomode fb_vmode = {0};
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001281
1282 /*
1283 * Add the standard VESA modes to our modelist
1284 * Since we don't have EDID, there may be modes that
1285 * overspec monitor and/or are incorrect aspect ratio, etc.
1286 * But at least the user has a chance to choose
1287 */
1288 for (i = 0; i < VESA_MODEDB_SIZE; i++) {
1289 if (dlfb_is_valid_mode((struct fb_videomode *)
1290 &vesa_modes[i], info))
1291 fb_add_videomode(&vesa_modes[i],
1292 &info->modelist);
1293 }
1294
1295 /*
1296 * default to resolution safe for projectors
1297 * (since they are most common case without EDID)
1298 */
1299 fb_vmode.xres = 800;
1300 fb_vmode.yres = 600;
1301 fb_vmode.refresh = 60;
1302 default_vmode = fb_find_nearest_mode(&fb_vmode,
1303 &info->modelist);
1304 }
1305
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001306 /* If we have good mode and no active clients*/
1307 if ((default_vmode != NULL) && (dev->fb_count == 0)) {
1308
1309 fb_videomode_to_var(&info->var, default_vmode);
1310 dlfb_var_color_format(&info->var);
1311
1312 /*
1313 * with mode size info, we can now alloc our framebuffer.
1314 */
1315 memcpy(&info->fix, &dlfb_fix, sizeof(dlfb_fix));
1316 info->fix.line_length = info->var.xres *
1317 (info->var.bits_per_pixel / 8);
1318
1319 result = dlfb_realloc_framebuffer(dev, info);
1320
1321 } else
1322 result = -EINVAL;
1323
1324error:
1325 if (edid && (dev->edid != edid))
1326 kfree(edid);
1327
1328 if (info->dev)
1329 mutex_unlock(&info->lock);
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001330
1331 return result;
1332}
1333
1334static ssize_t metrics_bytes_rendered_show(struct device *fbdev,
1335 struct device_attribute *a, char *buf) {
1336 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1337 struct dlfb_data *dev = fb_info->par;
1338 return snprintf(buf, PAGE_SIZE, "%u\n",
1339 atomic_read(&dev->bytes_rendered));
1340}
1341
1342static ssize_t metrics_bytes_identical_show(struct device *fbdev,
1343 struct device_attribute *a, char *buf) {
1344 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1345 struct dlfb_data *dev = fb_info->par;
1346 return snprintf(buf, PAGE_SIZE, "%u\n",
1347 atomic_read(&dev->bytes_identical));
1348}
1349
1350static ssize_t metrics_bytes_sent_show(struct device *fbdev,
1351 struct device_attribute *a, char *buf) {
1352 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1353 struct dlfb_data *dev = fb_info->par;
1354 return snprintf(buf, PAGE_SIZE, "%u\n",
1355 atomic_read(&dev->bytes_sent));
1356}
1357
1358static ssize_t metrics_cpu_kcycles_used_show(struct device *fbdev,
1359 struct device_attribute *a, char *buf) {
1360 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1361 struct dlfb_data *dev = fb_info->par;
1362 return snprintf(buf, PAGE_SIZE, "%u\n",
1363 atomic_read(&dev->cpu_kcycles_used));
1364}
1365
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001366static ssize_t edid_show(
1367 struct file *filp,
1368 struct kobject *kobj, struct bin_attribute *a,
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001369 char *buf, loff_t off, size_t count) {
1370 struct device *fbdev = container_of(kobj, struct device, kobj);
1371 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1372 struct dlfb_data *dev = fb_info->par;
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001373
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001374 if (dev->edid == NULL)
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001375 return 0;
1376
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001377 if ((off >= dev->edid_size) || (count > dev->edid_size))
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001378 return 0;
1379
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001380 if (off + count > dev->edid_size)
1381 count = dev->edid_size - off;
1382
1383 dl_info("sysfs edid copy %p to %p, %d bytes\n",
1384 dev->edid, buf, (int) count);
1385
1386 memcpy(buf, dev->edid, count);
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001387
1388 return count;
1389}
1390
Bernie Thompson8ef8cc42010-09-05 16:35:31 -07001391static ssize_t edid_store(
1392 struct file *filp,
1393 struct kobject *kobj, struct bin_attribute *a,
1394 char *src, loff_t src_off, size_t src_size) {
1395 struct device *fbdev = container_of(kobj, struct device, kobj);
1396 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1397 struct dlfb_data *dev = fb_info->par;
1398
1399 /* We only support write of entire EDID at once, no offset*/
1400 if ((src_size < MIN_EDID_SIZE) ||
1401 (src_size > MAX_EDID_SIZE) ||
1402 (src_off != 0))
1403 return 0;
1404
1405 dlfb_setup_modes(dev, fb_info, src, src_size);
1406
1407 if (dev->edid && (memcmp(src, dev->edid, src_size) == 0)) {
1408 dl_info("sysfs written EDID is new default\n");
1409 dlfb_ops_set_par(fb_info);
1410 return src_size;
1411 } else
1412 return 0;
1413}
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001414
1415static ssize_t metrics_reset_store(struct device *fbdev,
1416 struct device_attribute *attr,
1417 const char *buf, size_t count)
1418{
1419 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1420 struct dlfb_data *dev = fb_info->par;
1421
1422 atomic_set(&dev->bytes_rendered, 0);
1423 atomic_set(&dev->bytes_identical, 0);
1424 atomic_set(&dev->bytes_sent, 0);
1425 atomic_set(&dev->cpu_kcycles_used, 0);
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001426
1427 return count;
1428}
1429
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001430static struct bin_attribute edid_attr = {
1431 .attr.name = "edid",
Bernie Thompson8ef8cc42010-09-05 16:35:31 -07001432 .attr.mode = 0666,
1433 .size = MAX_EDID_SIZE,
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001434 .read = edid_show,
Bernie Thompson8ef8cc42010-09-05 16:35:31 -07001435 .write = edid_store
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001436};
1437
1438static struct device_attribute fb_device_attrs[] = {
1439 __ATTR_RO(metrics_bytes_rendered),
1440 __ATTR_RO(metrics_bytes_identical),
1441 __ATTR_RO(metrics_bytes_sent),
1442 __ATTR_RO(metrics_cpu_kcycles_used),
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001443 __ATTR(metrics_reset, S_IWUGO, NULL, metrics_reset_store),
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001444};
1445
1446/*
Bernie Thompsoncc403dc2010-02-15 06:45:49 -08001447 * This is necessary before we can communicate with the display controller.
1448 */
1449static int dlfb_select_std_channel(struct dlfb_data *dev)
1450{
1451 int ret;
1452 u8 set_def_chn[] = { 0x57, 0xCD, 0xDC, 0xA7,
1453 0x1C, 0x88, 0x5E, 0x15,
1454 0x60, 0xFE, 0xC6, 0x97,
1455 0x16, 0x3D, 0x47, 0xF2 };
1456
1457 ret = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
1458 NR_USB_REQUEST_CHANNEL,
1459 (USB_DIR_OUT | USB_TYPE_VENDOR), 0, 0,
1460 set_def_chn, sizeof(set_def_chn), USB_CTRL_SET_TIMEOUT);
1461 return ret;
1462}
1463
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001464static int dlfb_parse_vendor_descriptor(struct dlfb_data *dev,
1465 struct usb_device *usbdev)
1466{
1467 char *desc;
1468 char *buf;
1469 char *desc_end;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001470
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001471 u8 total_len = 0;
1472
1473 buf = kzalloc(MAX_VENDOR_DESCRIPTOR_SIZE, GFP_KERNEL);
1474 if (!buf)
1475 return false;
1476 desc = buf;
1477
1478 total_len = usb_get_descriptor(usbdev, 0x5f, /* vendor specific */
1479 0, desc, MAX_VENDOR_DESCRIPTOR_SIZE);
1480 if (total_len > 5) {
1481 dl_info("vendor descriptor length:%x data:%02x %02x %02x %02x" \
1482 "%02x %02x %02x %02x %02x %02x %02x\n",
1483 total_len, desc[0],
1484 desc[1], desc[2], desc[3], desc[4], desc[5], desc[6],
1485 desc[7], desc[8], desc[9], desc[10]);
1486
1487 if ((desc[0] != total_len) || /* descriptor length */
1488 (desc[1] != 0x5f) || /* vendor descriptor type */
1489 (desc[2] != 0x01) || /* version (2 bytes) */
1490 (desc[3] != 0x00) ||
1491 (desc[4] != total_len - 2)) /* length after type */
1492 goto unrecognized;
1493
1494 desc_end = desc + total_len;
1495 desc += 5; /* the fixed header we've already parsed */
1496
1497 while (desc < desc_end) {
1498 u8 length;
1499 u16 key;
1500
1501 key = *((u16 *) desc);
1502 desc += sizeof(u16);
1503 length = *desc;
1504 desc++;
1505
1506 switch (key) {
1507 case 0x0200: { /* max_area */
1508 u32 max_area;
1509 max_area = le32_to_cpu(*((u32 *)desc));
1510 dl_warn("DL chip limited to %d pixel modes\n",
1511 max_area);
1512 dev->sku_pixel_limit = max_area;
1513 break;
1514 }
1515 default:
1516 break;
1517 }
1518 desc += length;
1519 }
1520 }
1521
1522 goto success;
1523
1524unrecognized:
1525 /* allow udlfb to load for now even if firmware unrecognized */
1526 dl_err("Unrecognized vendor firmware descriptor\n");
1527
1528success:
1529 kfree(buf);
1530 return true;
1531}
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001532static int dlfb_usb_probe(struct usb_interface *interface,
Bernie Thompson59277b62009-11-24 15:52:21 -08001533 const struct usb_device_id *id)
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001534{
Bernie Thompson59277b62009-11-24 15:52:21 -08001535 struct usb_device *usbdev;
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001536 struct dlfb_data *dev = 0;
Bernie Thompson33077b82010-09-05 16:35:19 -07001537 struct fb_info *info = 0;
Bernie Thompson59277b62009-11-24 15:52:21 -08001538 int retval = -ENOMEM;
Bernie Thompson2685cff2010-09-05 18:29:56 -07001539 int i;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001540
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001541 /* usb initialization */
1542
1543 usbdev = interface_to_usbdev(interface);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001544
Bernie Thompson59277b62009-11-24 15:52:21 -08001545 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1546 if (dev == NULL) {
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001547 err("dlfb_usb_probe: failed alloc of dev struct\n");
1548 goto error;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001549 }
1550
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001551 /* we need to wait for both usb and fbdev to spin down on disconnect */
1552 kref_init(&dev->kref); /* matching kref_put in usb .disconnect fn */
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001553 kref_get(&dev->kref); /* matching kref_put in free_framebuffer_work */
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001554
Bernie Thompson59277b62009-11-24 15:52:21 -08001555 dev->udev = usbdev;
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001556 dev->gdev = &usbdev->dev; /* our generic struct device * */
Bernie Thompson59277b62009-11-24 15:52:21 -08001557 usb_set_intfdata(interface, dev);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001558
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001559 dl_info("%s %s - serial #%s\n",
1560 usbdev->manufacturer, usbdev->product, usbdev->serial);
1561 dl_info("vid_%04x&pid_%04x&rev_%04x driver's dlfb_data struct at %p\n",
1562 usbdev->descriptor.idVendor, usbdev->descriptor.idProduct,
1563 usbdev->descriptor.bcdDevice, dev);
Bernie Thompsond5ed5432010-09-05 16:35:39 -07001564 dl_info("console enable=%d\n", console);
1565 dl_info("fb_defio enable=%d\n", fb_defio);
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001566
1567 dev->sku_pixel_limit = 2048 * 1152; /* default to maximum */
1568
1569 if (!dlfb_parse_vendor_descriptor(dev, usbdev)) {
1570 dl_err("firmware not recognized. Assume incompatible device\n");
1571 goto error;
1572 }
1573
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001574 if (!dlfb_alloc_urb_list(dev, WRITES_IN_FLIGHT, MAX_TRANSFER)) {
1575 retval = -ENOMEM;
1576 dl_err("dlfb_alloc_urb_list failed\n");
1577 goto error;
1578 }
1579
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001580 /* We don't register a new USB class. Our client interface is fbdev */
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001581
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001582 /* allocates framebuffer driver structure, not framebuffer memory */
1583 info = framebuffer_alloc(0, &usbdev->dev);
1584 if (!info) {
1585 retval = -ENOMEM;
1586 dl_err("framebuffer_alloc failed\n");
1587 goto error;
1588 }
Bernie Thompson33077b82010-09-05 16:35:19 -07001589
Bernie Thompson59277b62009-11-24 15:52:21 -08001590 dev->info = info;
1591 info->par = dev;
1592 info->pseudo_palette = dev->pseudo_palette;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001593 info->fbops = &dlfb_ops;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001594
Bernie Thompson59277b62009-11-24 15:52:21 -08001595 retval = fb_alloc_cmap(&info->cmap, 256, 0);
1596 if (retval < 0) {
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001597 dl_err("fb_alloc_cmap failed %x\n", retval);
1598 goto error;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001599 }
1600
Bernie Thompson33077b82010-09-05 16:35:19 -07001601 INIT_DELAYED_WORK(&dev->free_framebuffer_work,
1602 dlfb_free_framebuffer_work);
1603
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001604 INIT_LIST_HEAD(&info->modelist);
1605
1606 retval = dlfb_setup_modes(dev, info, NULL, 0);
1607 if (retval != 0) {
1608 dl_err("unable to find common mode for display and adapter\n");
1609 goto error;
1610 }
1611
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001612 /* ready to begin using device */
1613
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001614 atomic_set(&dev->usb_active, 1);
Bernie Thompson59277b62009-11-24 15:52:21 -08001615 dlfb_select_std_channel(dev);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001616
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001617 dlfb_ops_check_var(&info->var, info);
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001618 dlfb_ops_set_par(info);
1619
Bernie Thompson59277b62009-11-24 15:52:21 -08001620 retval = register_framebuffer(info);
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001621 if (retval < 0) {
1622 dl_err("register_framebuffer failed %d\n", retval);
1623 goto error;
1624 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001625
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001626 for (i = 0; i < ARRAY_SIZE(fb_device_attrs); i++)
1627 device_create_file(info->dev, &fb_device_attrs[i]);
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -07001628
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001629 device_create_bin_file(info->dev, &edid_attr);
1630
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001631 dl_info("DisplayLink USB device /dev/fb%d attached. %dx%d resolution."
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001632 " Using %dK framebuffer memory\n", info->node,
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001633 info->var.xres, info->var.yres,
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001634 ((dev->backing_buffer) ?
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001635 info->fix.smem_len * 2 : info->fix.smem_len) >> 10);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001636 return 0;
1637
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001638error:
1639 if (dev) {
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001640
Bernie Thompson33077b82010-09-05 16:35:19 -07001641 if (info) {
1642 if (info->cmap.len != 0)
1643 fb_dealloc_cmap(&info->cmap);
1644 if (info->monspecs.modedb)
1645 fb_destroy_modedb(info->monspecs.modedb);
1646 if (info->screen_base)
1647 vfree(info->screen_base);
1648
1649 fb_destroy_modelist(&info->modelist);
1650
1651 framebuffer_release(info);
1652 }
1653
1654 if (dev->backing_buffer)
1655 vfree(dev->backing_buffer);
1656
1657 kref_put(&dev->kref, dlfb_free); /* ref for framebuffer */
1658 kref_put(&dev->kref, dlfb_free); /* last ref from kref_init */
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001659
1660 /* dev has been deallocated. Do not dereference */
1661 }
1662
Bernie Thompson59277b62009-11-24 15:52:21 -08001663 return retval;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001664}
1665
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001666static void dlfb_usb_disconnect(struct usb_interface *interface)
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001667{
Bernie Thompson59277b62009-11-24 15:52:21 -08001668 struct dlfb_data *dev;
1669 struct fb_info *info;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001670 int i;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001671
Bernie Thompson59277b62009-11-24 15:52:21 -08001672 dev = usb_get_intfdata(interface);
Bernie Thompson59277b62009-11-24 15:52:21 -08001673 info = dev->info;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001674
Bernie Thompson33077b82010-09-05 16:35:19 -07001675 dl_info("USB disconnect starting\n");
1676
1677 /* we virtualize until all fb clients release. Then we free */
1678 dev->virtualized = true;
1679
1680 /* When non-active we'll update virtual framebuffer, but no new urbs */
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001681 atomic_set(&dev->usb_active, 0);
1682
Bernie Thompson33077b82010-09-05 16:35:19 -07001683 /* remove udlfb's sysfs interfaces */
1684 for (i = 0; i < ARRAY_SIZE(fb_device_attrs); i++)
1685 device_remove_file(info->dev, &fb_device_attrs[i]);
1686 device_remove_bin_file(info->dev, &edid_attr);
1687
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001688 usb_set_intfdata(interface, NULL);
1689
Bernie Thompson33077b82010-09-05 16:35:19 -07001690 /* if clients still have us open, will be freed on last close */
1691 if (dev->fb_count == 0)
1692 schedule_delayed_work(&dev->free_framebuffer_work, 0);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001693
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001694 /* release reference taken by kref_init in probe() */
Bernie Thompson33077b82010-09-05 16:35:19 -07001695 kref_put(&dev->kref, dlfb_free);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001696
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001697 /* consider dlfb_data freed */
1698
1699 return;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001700}
1701
1702static struct usb_driver dlfb_driver = {
1703 .name = "udlfb",
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001704 .probe = dlfb_usb_probe,
1705 .disconnect = dlfb_usb_disconnect,
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001706 .id_table = id_table,
1707};
1708
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001709static int __init dlfb_module_init(void)
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001710{
1711 int res;
1712
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001713 res = usb_register(&dlfb_driver);
1714 if (res)
1715 err("usb_register failed. Error number %d", res);
1716
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001717 return res;
1718}
1719
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001720static void __exit dlfb_module_exit(void)
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001721{
1722 usb_deregister(&dlfb_driver);
1723}
1724
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001725module_init(dlfb_module_init);
1726module_exit(dlfb_module_exit);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001727
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001728static void dlfb_urb_completion(struct urb *urb)
1729{
1730 struct urb_node *unode = urb->context;
1731 struct dlfb_data *dev = unode->dev;
1732 unsigned long flags;
1733
1734 /* sync/async unlink faults aren't errors */
1735 if (urb->status) {
1736 if (!(urb->status == -ENOENT ||
1737 urb->status == -ECONNRESET ||
1738 urb->status == -ESHUTDOWN)) {
1739 dl_err("%s - nonzero write bulk status received: %d\n",
1740 __func__, urb->status);
1741 atomic_set(&dev->lost_pixels, 1);
1742 }
1743 }
1744
1745 urb->transfer_buffer_length = dev->urbs.size; /* reset to actual */
1746
1747 spin_lock_irqsave(&dev->urbs.lock, flags);
1748 list_add_tail(&unode->entry, &dev->urbs.list);
1749 dev->urbs.available++;
1750 spin_unlock_irqrestore(&dev->urbs.lock, flags);
1751
Bernie Thompson5bea1fb2010-09-05 18:28:29 -07001752 /*
1753 * When using fb_defio, we deadlock if up() is called
1754 * while another is waiting. So queue to another process.
1755 */
1756 if (fb_defio)
1757 schedule_delayed_work(&unode->release_urb_work, 0);
1758 else
1759 up(&dev->urbs.limit_sem);
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001760}
1761
1762static void dlfb_free_urb_list(struct dlfb_data *dev)
1763{
1764 int count = dev->urbs.count;
1765 struct list_head *node;
1766 struct urb_node *unode;
1767 struct urb *urb;
1768 int ret;
1769 unsigned long flags;
1770
1771 dl_notice("Waiting for completes and freeing all render urbs\n");
1772
1773 /* keep waiting and freeing, until we've got 'em all */
1774 while (count--) {
Bernie Thompson33077b82010-09-05 16:35:19 -07001775
1776 /* Getting interrupted means a leak, but ok at shutdown*/
1777 ret = down_interruptible(&dev->urbs.limit_sem);
1778 if (ret)
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001779 break;
Bernie Thompson33077b82010-09-05 16:35:19 -07001780
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001781 spin_lock_irqsave(&dev->urbs.lock, flags);
1782
1783 node = dev->urbs.list.next; /* have reserved one with sem */
1784 list_del_init(node);
1785
1786 spin_unlock_irqrestore(&dev->urbs.lock, flags);
1787
1788 unode = list_entry(node, struct urb_node, entry);
1789 urb = unode->urb;
1790
1791 /* Free each separately allocated piece */
Greg Kroah-Hartmanc220cc32010-04-29 15:36:29 -07001792 usb_free_coherent(urb->dev, dev->urbs.size,
1793 urb->transfer_buffer, urb->transfer_dma);
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001794 usb_free_urb(urb);
1795 kfree(node);
1796 }
1797
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001798}
1799
1800static int dlfb_alloc_urb_list(struct dlfb_data *dev, int count, size_t size)
1801{
1802 int i = 0;
1803 struct urb *urb;
1804 struct urb_node *unode;
1805 char *buf;
1806
1807 spin_lock_init(&dev->urbs.lock);
1808
1809 dev->urbs.size = size;
1810 INIT_LIST_HEAD(&dev->urbs.list);
1811
1812 while (i < count) {
1813 unode = kzalloc(sizeof(struct urb_node), GFP_KERNEL);
1814 if (!unode)
1815 break;
1816 unode->dev = dev;
1817
Bernie Thompson5bea1fb2010-09-05 18:28:29 -07001818 INIT_DELAYED_WORK(&unode->release_urb_work,
1819 dlfb_release_urb_work);
1820
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001821 urb = usb_alloc_urb(0, GFP_KERNEL);
1822 if (!urb) {
1823 kfree(unode);
1824 break;
1825 }
1826 unode->urb = urb;
1827
Greg Kroah-Hartmanc220cc32010-04-29 15:36:29 -07001828 buf = usb_alloc_coherent(dev->udev, MAX_TRANSFER, GFP_KERNEL,
1829 &urb->transfer_dma);
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001830 if (!buf) {
1831 kfree(unode);
1832 usb_free_urb(urb);
1833 break;
1834 }
1835
1836 /* urb->transfer_buffer_length set to actual before submit */
1837 usb_fill_bulk_urb(urb, dev->udev, usb_sndbulkpipe(dev->udev, 1),
1838 buf, size, dlfb_urb_completion, unode);
1839 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1840
1841 list_add_tail(&unode->entry, &dev->urbs.list);
1842
1843 i++;
1844 }
1845
1846 sema_init(&dev->urbs.limit_sem, i);
1847 dev->urbs.count = i;
1848 dev->urbs.available = i;
1849
Soeren Moellerb5a21042010-05-14 19:03:00 +00001850 dl_notice("allocated %d %d byte urbs\n", i, (int) size);
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001851
1852 return i;
1853}
1854
1855static struct urb *dlfb_get_urb(struct dlfb_data *dev)
1856{
1857 int ret = 0;
1858 struct list_head *entry;
1859 struct urb_node *unode;
1860 struct urb *urb = NULL;
1861 unsigned long flags;
1862
1863 /* Wait for an in-flight buffer to complete and get re-queued */
1864 ret = down_timeout(&dev->urbs.limit_sem, GET_URB_TIMEOUT);
1865 if (ret) {
1866 atomic_set(&dev->lost_pixels, 1);
Bernie Thompson5bea1fb2010-09-05 18:28:29 -07001867 dl_warn("wait for urb interrupted: %x available: %d\n",
1868 ret, dev->urbs.available);
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001869 goto error;
1870 }
1871
1872 spin_lock_irqsave(&dev->urbs.lock, flags);
1873
1874 BUG_ON(list_empty(&dev->urbs.list)); /* reserved one with limit_sem */
1875 entry = dev->urbs.list.next;
1876 list_del_init(entry);
1877 dev->urbs.available--;
1878
1879 spin_unlock_irqrestore(&dev->urbs.lock, flags);
1880
1881 unode = list_entry(entry, struct urb_node, entry);
1882 urb = unode->urb;
1883
1884error:
1885 return urb;
1886}
1887
1888static int dlfb_submit_urb(struct dlfb_data *dev, struct urb *urb, size_t len)
1889{
1890 int ret;
1891
1892 BUG_ON(len > dev->urbs.size);
1893
1894 urb->transfer_buffer_length = len; /* set to actual payload len */
1895 ret = usb_submit_urb(urb, GFP_KERNEL);
1896 if (ret) {
1897 dlfb_urb_completion(urb); /* because no one else will */
1898 atomic_set(&dev->lost_pixels, 1);
1899 dl_err("usb_submit_urb error %x\n", ret);
1900 }
1901 return ret;
1902}
1903
Bernie Thompsond5ed5432010-09-05 16:35:39 -07001904module_param(console, bool, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
1905MODULE_PARM_DESC(console, "Allow fbcon to consume first framebuffer found");
1906
1907module_param(fb_defio, bool, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
1908MODULE_PARM_DESC(fb_defio, "Enable fb_defio mmap support. *Experimental*");
1909
Bernie Thompson59277b62009-11-24 15:52:21 -08001910MODULE_AUTHOR("Roberto De Ioris <roberto@unbit.it>, "
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001911 "Jaya Kumar <jayakumar.lkml@gmail.com>, "
1912 "Bernie Thompson <bernie@plugable.com>");
1913MODULE_DESCRIPTION("DisplayLink kernel framebuffer driver");
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001914MODULE_LICENSE("GPL");
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001915