blob: 80a1e0e0ab7e482b60d2a19e848e2e821c3b0afe [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
31#include "udlfb.h"
32
Bernie Thompson59277b62009-11-24 15:52:21 -080033static struct fb_fix_screeninfo dlfb_fix = {
Bernie Thompson2469d5d2010-02-15 06:46:13 -080034 .id = "udlfb",
Bernie Thompson1d31a9e2010-02-15 06:45:43 -080035 .type = FB_TYPE_PACKED_PIXELS,
36 .visual = FB_VISUAL_TRUECOLOR,
37 .xpanstep = 0,
38 .ypanstep = 0,
39 .ywrapstep = 0,
40 .accel = FB_ACCEL_NONE,
Bernie Thompson59277b62009-11-24 15:52:21 -080041};
Roberto De Ioris88e58b12009-06-03 14:03:06 -070042
Bernie Thompson2469d5d2010-02-15 06:46:13 -080043static const u32 udlfb_info_flags = FBINFO_DEFAULT | FBINFO_READS_FAST |
44#ifdef FBINFO_VIRTFB
45 FBINFO_VIRTFB |
46#endif
47 FBINFO_HWACCEL_IMAGEBLIT | FBINFO_HWACCEL_FILLRECT |
48 FBINFO_HWACCEL_COPYAREA | FBINFO_MISC_ALWAYS_SETPAR;
49
Bernie Thompsoncc403dc2010-02-15 06:45:49 -080050/*
51 * There are many DisplayLink-based products, all with unique PIDs. We are able
52 * to support all volume ones (circa 2009) with a single driver, so we match
53 * globally on VID. TODO: Probe() needs to detect when we might be running
54 * "future" chips, and bail on those, so a compatible driver can match.
55 */
56static struct usb_device_id id_table[] = {
57 {.idVendor = 0x17e9, .match_flags = USB_DEVICE_ID_MATCH_VENDOR,},
58 {},
59};
60MODULE_DEVICE_TABLE(usb, id_table);
Bernie Thompson59277b62009-11-24 15:52:21 -080061
Bernie Thompsond5ed5432010-09-05 16:35:39 -070062/* module options */
63static int console; /* Optionally allow fbcon to consume first framebuffer */
64static int fb_defio; /* Optionally enable experimental fb_defio mmap support */
Bernie Thompsondd8015f2010-02-15 06:46:35 -080065
Bernie Thompson4a4854d2010-02-15 06:45:55 -080066/* dlfb keeps a list of urbs for efficient bulk transfers */
67static void dlfb_urb_completion(struct urb *urb);
68static struct urb *dlfb_get_urb(struct dlfb_data *dev);
69static int dlfb_submit_urb(struct dlfb_data *dev, struct urb * urb, size_t len);
70static int dlfb_alloc_urb_list(struct dlfb_data *dev, int count, size_t size);
71static void dlfb_free_urb_list(struct dlfb_data *dev);
72
Bernie Thompson3e8f3d62010-02-15 06:46:26 -080073/* other symbols with dependents */
74#ifdef CONFIG_FB_DEFERRED_IO
75static struct fb_deferred_io dlfb_defio;
76#endif
77
Bernie Thompson59277b62009-11-24 15:52:21 -080078/*
Bernie Thompsonbd808162010-02-15 06:46:48 -080079 * All DisplayLink bulk operations start with 0xAF, followed by specific code
80 * All operations are written to buffers which then later get sent to device
Bernie Thompson59277b62009-11-24 15:52:21 -080081 */
Bernie Thompson45742032010-02-15 06:46:04 -080082static char *dlfb_set_register(char *buf, u8 reg, u8 val)
Roberto De Ioris88e58b12009-06-03 14:03:06 -070083{
Bernie Thompson1d31a9e2010-02-15 06:45:43 -080084 *buf++ = 0xAF;
85 *buf++ = 0x20;
86 *buf++ = reg;
87 *buf++ = val;
88 return buf;
Roberto De Ioris88e58b12009-06-03 14:03:06 -070089}
90
Bernie Thompson45742032010-02-15 06:46:04 -080091static char *dlfb_vidreg_lock(char *buf)
Roberto De Ioris88e58b12009-06-03 14:03:06 -070092{
Bernie Thompson45742032010-02-15 06:46:04 -080093 return dlfb_set_register(buf, 0xFF, 0x00);
Bernie Thompson59277b62009-11-24 15:52:21 -080094}
Roberto De Ioris88e58b12009-06-03 14:03:06 -070095
Bernie Thompson45742032010-02-15 06:46:04 -080096static char *dlfb_vidreg_unlock(char *buf)
Bernie Thompson59277b62009-11-24 15:52:21 -080097{
Bernie Thompson45742032010-02-15 06:46:04 -080098 return dlfb_set_register(buf, 0xFF, 0xFF);
Bernie Thompson59277b62009-11-24 15:52:21 -080099}
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700100
Bernie Thompson59277b62009-11-24 15:52:21 -0800101/*
Bernie Thompson530f43a2010-02-15 06:46:21 -0800102 * On/Off for driving the DisplayLink framebuffer to the display
Bernie Thompson9825f702010-09-05 16:35:10 -0700103 * 0x00 H and V sync on
104 * 0x01 H and V sync off (screen blank but powered)
105 * 0x07 DPMS powerdown (requires modeset to come back)
Bernie Thompson59277b62009-11-24 15:52:21 -0800106 */
Bernie Thompson530f43a2010-02-15 06:46:21 -0800107static char *dlfb_enable_hvsync(char *buf, bool enable)
Bernie Thompson59277b62009-11-24 15:52:21 -0800108{
Bernie Thompson530f43a2010-02-15 06:46:21 -0800109 if (enable)
110 return dlfb_set_register(buf, 0x1F, 0x00);
111 else
Bernie Thompson9825f702010-09-05 16:35:10 -0700112 return dlfb_set_register(buf, 0x1F, 0x07);
Bernie Thompson59277b62009-11-24 15:52:21 -0800113}
114
Bernie Thompson45742032010-02-15 06:46:04 -0800115static char *dlfb_set_color_depth(char *buf, u8 selection)
Bernie Thompson59277b62009-11-24 15:52:21 -0800116{
Bernie Thompson45742032010-02-15 06:46:04 -0800117 return dlfb_set_register(buf, 0x00, selection);
Bernie Thompson59277b62009-11-24 15:52:21 -0800118}
119
Bernie Thompson45742032010-02-15 06:46:04 -0800120static char *dlfb_set_base16bpp(char *wrptr, u32 base)
Bernie Thompson59277b62009-11-24 15:52:21 -0800121{
Bernie Thompson1d31a9e2010-02-15 06:45:43 -0800122 /* the base pointer is 16 bits wide, 0x20 is hi byte. */
Bernie Thompson45742032010-02-15 06:46:04 -0800123 wrptr = dlfb_set_register(wrptr, 0x20, base >> 16);
124 wrptr = dlfb_set_register(wrptr, 0x21, base >> 8);
125 return dlfb_set_register(wrptr, 0x22, base);
Bernie Thompson59277b62009-11-24 15:52:21 -0800126}
127
Bernie Thompsonbd808162010-02-15 06:46:48 -0800128/*
129 * DisplayLink HW has separate 16bpp and 8bpp framebuffers.
130 * In 24bpp modes, the low 323 RGB bits go in the 8bpp framebuffer
131 */
Bernie Thompson45742032010-02-15 06:46:04 -0800132static char *dlfb_set_base8bpp(char *wrptr, u32 base)
Bernie Thompson59277b62009-11-24 15:52:21 -0800133{
Bernie Thompson45742032010-02-15 06:46:04 -0800134 wrptr = dlfb_set_register(wrptr, 0x26, base >> 16);
135 wrptr = dlfb_set_register(wrptr, 0x27, base >> 8);
136 return dlfb_set_register(wrptr, 0x28, base);
Bernie Thompson59277b62009-11-24 15:52:21 -0800137}
138
Bernie Thompson45742032010-02-15 06:46:04 -0800139static char *dlfb_set_register_16(char *wrptr, u8 reg, u16 value)
Bernie Thompson59277b62009-11-24 15:52:21 -0800140{
Bernie Thompson45742032010-02-15 06:46:04 -0800141 wrptr = dlfb_set_register(wrptr, reg, value >> 8);
142 return dlfb_set_register(wrptr, reg+1, value);
Bernie Thompson59277b62009-11-24 15:52:21 -0800143}
144
145/*
146 * This is kind of weird because the controller takes some
147 * register values in a different byte order than other registers.
148 */
Bernie Thompson45742032010-02-15 06:46:04 -0800149static char *dlfb_set_register_16be(char *wrptr, u8 reg, u16 value)
Bernie Thompson59277b62009-11-24 15:52:21 -0800150{
Bernie Thompson45742032010-02-15 06:46:04 -0800151 wrptr = dlfb_set_register(wrptr, reg, value);
152 return dlfb_set_register(wrptr, reg+1, value >> 8);
Bernie Thompson59277b62009-11-24 15:52:21 -0800153}
154
155/*
156 * LFSR is linear feedback shift register. The reason we have this is
157 * because the display controller needs to minimize the clock depth of
158 * various counters used in the display path. So this code reverses the
159 * provided value into the lfsr16 value by counting backwards to get
160 * the value that needs to be set in the hardware comparator to get the
161 * same actual count. This makes sense once you read above a couple of
162 * times and think about it from a hardware perspective.
163 */
Bernie Thompsonbd808162010-02-15 06:46:48 -0800164static u16 dlfb_lfsr16(u16 actual_count)
Bernie Thompson59277b62009-11-24 15:52:21 -0800165{
166 u32 lv = 0xFFFF; /* This is the lfsr value that the hw starts with */
167
168 while (actual_count--) {
169 lv = ((lv << 1) |
170 (((lv >> 15) ^ (lv >> 4) ^ (lv >> 2) ^ (lv >> 1)) & 1))
171 & 0xFFFF;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700172 }
Bernie Thompson59277b62009-11-24 15:52:21 -0800173
174 return (u16) lv;
175}
176
177/*
178 * This does LFSR conversion on the value that is to be written.
179 * See LFSR explanation above for more detail.
180 */
Bernie Thompson45742032010-02-15 06:46:04 -0800181static char *dlfb_set_register_lfsr16(char *wrptr, u8 reg, u16 value)
Bernie Thompson59277b62009-11-24 15:52:21 -0800182{
Bernie Thompsonbd808162010-02-15 06:46:48 -0800183 return dlfb_set_register_16(wrptr, reg, dlfb_lfsr16(value));
Bernie Thompson59277b62009-11-24 15:52:21 -0800184}
185
186/*
187 * This takes a standard fbdev screeninfo struct and all of its monitor mode
188 * details and converts them into the DisplayLink equivalent register commands.
189 */
Bernie Thompson45742032010-02-15 06:46:04 -0800190static char *dlfb_set_vid_cmds(char *wrptr, struct fb_var_screeninfo *var)
Bernie Thompson59277b62009-11-24 15:52:21 -0800191{
192 u16 xds, yds;
193 u16 xde, yde;
194 u16 yec;
195
Bernie Thompson59277b62009-11-24 15:52:21 -0800196 /* x display start */
197 xds = var->left_margin + var->hsync_len;
Bernie Thompson45742032010-02-15 06:46:04 -0800198 wrptr = dlfb_set_register_lfsr16(wrptr, 0x01, xds);
Bernie Thompson59277b62009-11-24 15:52:21 -0800199 /* x display end */
200 xde = xds + var->xres;
Bernie Thompson45742032010-02-15 06:46:04 -0800201 wrptr = dlfb_set_register_lfsr16(wrptr, 0x03, xde);
Bernie Thompson59277b62009-11-24 15:52:21 -0800202
203 /* y display start */
204 yds = var->upper_margin + var->vsync_len;
Bernie Thompson45742032010-02-15 06:46:04 -0800205 wrptr = dlfb_set_register_lfsr16(wrptr, 0x05, yds);
Bernie Thompson59277b62009-11-24 15:52:21 -0800206 /* y display end */
207 yde = yds + var->yres;
Bernie Thompson45742032010-02-15 06:46:04 -0800208 wrptr = dlfb_set_register_lfsr16(wrptr, 0x07, yde);
Bernie Thompson59277b62009-11-24 15:52:21 -0800209
210 /* x end count is active + blanking - 1 */
Bernie Thompson45742032010-02-15 06:46:04 -0800211 wrptr = dlfb_set_register_lfsr16(wrptr, 0x09,
212 xde + var->right_margin - 1);
Bernie Thompson59277b62009-11-24 15:52:21 -0800213
214 /* libdlo hardcodes hsync start to 1 */
Bernie Thompson45742032010-02-15 06:46:04 -0800215 wrptr = dlfb_set_register_lfsr16(wrptr, 0x0B, 1);
Bernie Thompson59277b62009-11-24 15:52:21 -0800216
217 /* hsync end is width of sync pulse + 1 */
Bernie Thompson45742032010-02-15 06:46:04 -0800218 wrptr = dlfb_set_register_lfsr16(wrptr, 0x0D, var->hsync_len + 1);
Bernie Thompson59277b62009-11-24 15:52:21 -0800219
220 /* hpixels is active pixels */
Bernie Thompson45742032010-02-15 06:46:04 -0800221 wrptr = dlfb_set_register_16(wrptr, 0x0F, var->xres);
Bernie Thompson59277b62009-11-24 15:52:21 -0800222
223 /* yendcount is vertical active + vertical blanking */
224 yec = var->yres + var->upper_margin + var->lower_margin +
225 var->vsync_len;
Bernie Thompson45742032010-02-15 06:46:04 -0800226 wrptr = dlfb_set_register_lfsr16(wrptr, 0x11, yec);
Bernie Thompson59277b62009-11-24 15:52:21 -0800227
228 /* libdlo hardcodes vsync start to 0 */
Bernie Thompson45742032010-02-15 06:46:04 -0800229 wrptr = dlfb_set_register_lfsr16(wrptr, 0x13, 0);
Bernie Thompson59277b62009-11-24 15:52:21 -0800230
231 /* vsync end is width of vsync pulse */
Bernie Thompson45742032010-02-15 06:46:04 -0800232 wrptr = dlfb_set_register_lfsr16(wrptr, 0x15, var->vsync_len);
Bernie Thompson59277b62009-11-24 15:52:21 -0800233
234 /* vpixels is active pixels */
Bernie Thompson45742032010-02-15 06:46:04 -0800235 wrptr = dlfb_set_register_16(wrptr, 0x17, var->yres);
Bernie Thompson59277b62009-11-24 15:52:21 -0800236
237 /* convert picoseconds to 5kHz multiple for pclk5k = x * 1E12/5k */
Bernie Thompson45742032010-02-15 06:46:04 -0800238 wrptr = dlfb_set_register_16be(wrptr, 0x1B,
239 200*1000*1000/var->pixclock);
Bernie Thompson59277b62009-11-24 15:52:21 -0800240
241 return wrptr;
242}
243
244/*
245 * This takes a standard fbdev screeninfo struct that was fetched or prepared
246 * and then generates the appropriate command sequence that then drives the
247 * display controller.
248 */
249static int dlfb_set_video_mode(struct dlfb_data *dev,
250 struct fb_var_screeninfo *var)
251{
252 char *buf;
253 char *wrptr;
254 int retval = 0;
255 int writesize;
Bernie Thompson530f43a2010-02-15 06:46:21 -0800256 struct urb *urb;
Bernie Thompson59277b62009-11-24 15:52:21 -0800257
Bernie Thompson530f43a2010-02-15 06:46:21 -0800258 if (!atomic_read(&dev->usb_active))
259 return -EPERM;
260
261 urb = dlfb_get_urb(dev);
262 if (!urb)
263 return -ENOMEM;
264 buf = (char *) urb->transfer_buffer;
Bernie Thompson59277b62009-11-24 15:52:21 -0800265
266 /*
267 * This first section has to do with setting the base address on the
268 * controller * associated with the display. There are 2 base
269 * pointers, currently, we only * use the 16 bpp segment.
270 */
Bernie Thompson45742032010-02-15 06:46:04 -0800271 wrptr = dlfb_vidreg_lock(buf);
272 wrptr = dlfb_set_color_depth(wrptr, 0x00);
Bernie Thompson59277b62009-11-24 15:52:21 -0800273 /* set base for 16bpp segment to 0 */
Bernie Thompson45742032010-02-15 06:46:04 -0800274 wrptr = dlfb_set_base16bpp(wrptr, 0);
Bernie Thompson59277b62009-11-24 15:52:21 -0800275 /* set base for 8bpp segment to end of fb */
Bernie Thompson45742032010-02-15 06:46:04 -0800276 wrptr = dlfb_set_base8bpp(wrptr, dev->info->fix.smem_len);
Bernie Thompson59277b62009-11-24 15:52:21 -0800277
Bernie Thompson45742032010-02-15 06:46:04 -0800278 wrptr = dlfb_set_vid_cmds(wrptr, var);
Bernie Thompson530f43a2010-02-15 06:46:21 -0800279 wrptr = dlfb_enable_hvsync(wrptr, true);
Bernie Thompson45742032010-02-15 06:46:04 -0800280 wrptr = dlfb_vidreg_unlock(wrptr);
Bernie Thompson59277b62009-11-24 15:52:21 -0800281
282 writesize = wrptr - buf;
283
Bernie Thompson530f43a2010-02-15 06:46:21 -0800284 retval = dlfb_submit_urb(dev, urb, writesize);
Bernie Thompson59277b62009-11-24 15:52:21 -0800285
Bernie Thompson59277b62009-11-24 15:52:21 -0800286 return retval;
287}
288
Bernie Thompson45742032010-02-15 06:46:04 -0800289static int dlfb_ops_mmap(struct fb_info *info, struct vm_area_struct *vma)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700290{
291 unsigned long start = vma->vm_start;
292 unsigned long size = vma->vm_end - vma->vm_start;
293 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
294 unsigned long page, pos;
295
Bernie Thompsonbd808162010-02-15 06:46:48 -0800296 dl_notice("MMAP: %lu %u\n", offset + size, info->fix.smem_len);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700297
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700298 if (offset + size > info->fix.smem_len)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700299 return -EINVAL;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700300
301 pos = (unsigned long)info->fix.smem_start + offset;
302
303 while (size > 0) {
304 page = vmalloc_to_pfn((void *)pos);
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700305 if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED))
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700306 return -EAGAIN;
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700307
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700308 start += PAGE_SIZE;
309 pos += PAGE_SIZE;
310 if (size > PAGE_SIZE)
311 size -= PAGE_SIZE;
312 else
313 size = 0;
314 }
315
316 vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
317 return 0;
318
319}
320
Bernie Thompson530f43a2010-02-15 06:46:21 -0800321/*
322 * Trims identical data from front and back of line
323 * Sets new front buffer address and width
324 * And returns byte count of identical pixels
325 * Assumes CPU natural alignment (unsigned long)
326 * for back and front buffer ptrs and width
327 */
328static int dlfb_trim_hline(const u8 *bback, const u8 **bfront, int *width_bytes)
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700329{
Bernie Thompson530f43a2010-02-15 06:46:21 -0800330 int j, k;
331 const unsigned long *back = (const unsigned long *) bback;
332 const unsigned long *front = (const unsigned long *) *bfront;
333 const int width = *width_bytes / sizeof(unsigned long);
334 int identical = width;
335 int start = width;
336 int end = width;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700337
Bernie Thompson530f43a2010-02-15 06:46:21 -0800338 prefetch((void *) front);
339 prefetch((void *) back);
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700340
Bernie Thompson530f43a2010-02-15 06:46:21 -0800341 for (j = 0; j < width; j++) {
342 if (back[j] != front[j]) {
343 start = j;
344 break;
345 }
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700346 }
347
Bernie Thompson530f43a2010-02-15 06:46:21 -0800348 for (k = width - 1; k > j; k--) {
349 if (back[k] != front[k]) {
350 end = k+1;
351 break;
352 }
353 }
354
355 identical = start + (width - end);
356 *bfront = (u8 *) &front[start];
357 *width_bytes = (end - start) * sizeof(unsigned long);
358
359 return identical * sizeof(unsigned long);
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700360}
361
362/*
Pavel Machek3b7b31f2010-04-03 07:00:37 +0200363 * Render a command stream for an encoded horizontal line segment of pixels.
364 *
365 * A command buffer holds several commands.
366 * It always begins with a fresh command header
367 * (the protocol doesn't require this, but we enforce it to allow
368 * multiple buffers to be potentially encoded and sent in parallel).
369 * A single command encodes one contiguous horizontal line of pixels
370 *
371 * The function relies on the client to do all allocation, so that
372 * rendering can be done directly to output buffers (e.g. USB URBs).
373 * The function fills the supplied command buffer, providing information
374 * on where it left off, so the client may call in again with additional
375 * buffers if the line will take several buffers to complete.
376 *
377 * A single command can transmit a maximum of 256 pixels,
378 * regardless of the compression ratio (protocol design limit).
379 * To the hardware, 0 for a size byte means 256
380 *
381 * Rather than 256 pixel commands which are either rl or raw encoded,
382 * the rlx command simply assumes alternating raw and rl spans within one cmd.
383 * This has a slightly larger header overhead, but produces more even results.
384 * It also processes all data (read and write) in a single pass.
385 * Performance benchmarks of common cases show it having just slightly better
386 * compression than 256 pixel raw -or- rle commands, with similar CPU consumpion.
387 * But for very rl friendly data, will compress not quite as well.
388 */
Bernie Thompson530f43a2010-02-15 06:46:21 -0800389static void dlfb_compress_hline(
390 const uint16_t **pixel_start_ptr,
391 const uint16_t *const pixel_end,
392 uint32_t *device_address_ptr,
393 uint8_t **command_buffer_ptr,
394 const uint8_t *const cmd_buffer_end)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700395{
Bernie Thompson530f43a2010-02-15 06:46:21 -0800396 const uint16_t *pixel = *pixel_start_ptr;
397 uint32_t dev_addr = *device_address_ptr;
398 uint8_t *cmd = *command_buffer_ptr;
399 const int bpp = 2;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700400
Bernie Thompson530f43a2010-02-15 06:46:21 -0800401 while ((pixel_end > pixel) &&
402 (cmd_buffer_end - MIN_RLX_CMD_BYTES > cmd)) {
403 uint8_t *raw_pixels_count_byte = 0;
404 uint8_t *cmd_pixels_count_byte = 0;
405 const uint16_t *raw_pixel_start = 0;
406 const uint16_t *cmd_pixel_start, *cmd_pixel_end = 0;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700407
Bernie Thompson530f43a2010-02-15 06:46:21 -0800408 prefetchw((void *) cmd); /* pull in one cache line at least */
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700409
Bernie Thompson530f43a2010-02-15 06:46:21 -0800410 *cmd++ = 0xAF;
411 *cmd++ = 0x6B;
Bernie Thompson1572f912010-09-05 16:35:27 -0700412 *cmd++ = (uint8_t) ((dev_addr >> 16) & 0xFF);
413 *cmd++ = (uint8_t) ((dev_addr >> 8) & 0xFF);
414 *cmd++ = (uint8_t) ((dev_addr) & 0xFF);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700415
Bernie Thompson530f43a2010-02-15 06:46:21 -0800416 cmd_pixels_count_byte = cmd++; /* we'll know this later */
417 cmd_pixel_start = pixel;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700418
Bernie Thompson530f43a2010-02-15 06:46:21 -0800419 raw_pixels_count_byte = cmd++; /* we'll know this later */
420 raw_pixel_start = pixel;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700421
Bernie Thompson530f43a2010-02-15 06:46:21 -0800422 cmd_pixel_end = pixel + min(MAX_CMD_PIXELS + 1,
423 min((int)(pixel_end - pixel),
424 (int)(cmd_buffer_end - cmd) / bpp));
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700425
Bernie Thompson530f43a2010-02-15 06:46:21 -0800426 prefetch_range((void *) pixel, (cmd_pixel_end - pixel) * bpp);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700427
Bernie Thompson530f43a2010-02-15 06:46:21 -0800428 while (pixel < cmd_pixel_end) {
429 const uint16_t * const repeating_pixel = pixel;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700430
Bernie Thompson530f43a2010-02-15 06:46:21 -0800431 *(uint16_t *)cmd = cpu_to_be16p(pixel);
432 cmd += 2;
433 pixel++;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700434
Bernie Thompson530f43a2010-02-15 06:46:21 -0800435 if (unlikely((pixel < cmd_pixel_end) &&
436 (*pixel == *repeating_pixel))) {
437 /* go back and fill in raw pixel count */
438 *raw_pixels_count_byte = ((repeating_pixel -
439 raw_pixel_start) + 1) & 0xFF;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700440
Bernie Thompson530f43a2010-02-15 06:46:21 -0800441 while ((pixel < cmd_pixel_end)
442 && (*pixel == *repeating_pixel)) {
443 pixel++;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700444 }
Bernie Thompson59277b62009-11-24 15:52:21 -0800445
Bernie Thompson530f43a2010-02-15 06:46:21 -0800446 /* immediately after raw data is repeat byte */
447 *cmd++ = ((pixel - repeating_pixel) - 1) & 0xFF;
Bernie Thompson59277b62009-11-24 15:52:21 -0800448
Bernie Thompson530f43a2010-02-15 06:46:21 -0800449 /* Then start another raw pixel span */
450 raw_pixel_start = pixel;
451 raw_pixels_count_byte = cmd++;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700452 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700453 }
454
Bernie Thompson530f43a2010-02-15 06:46:21 -0800455 if (pixel > raw_pixel_start) {
456 /* finalize last RAW span */
457 *raw_pixels_count_byte = (pixel-raw_pixel_start) & 0xFF;
458 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700459
Bernie Thompson530f43a2010-02-15 06:46:21 -0800460 *cmd_pixels_count_byte = (pixel - cmd_pixel_start) & 0xFF;
461 dev_addr += (pixel - cmd_pixel_start) * bpp;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700462 }
463
Bernie Thompson530f43a2010-02-15 06:46:21 -0800464 if (cmd_buffer_end <= MIN_RLX_CMD_BYTES + cmd) {
465 /* Fill leftover bytes with no-ops */
466 if (cmd_buffer_end > cmd)
467 memset(cmd, 0xAF, cmd_buffer_end - cmd);
468 cmd = (uint8_t *) cmd_buffer_end;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700469 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700470
Bernie Thompson530f43a2010-02-15 06:46:21 -0800471 *command_buffer_ptr = cmd;
472 *pixel_start_ptr = pixel;
473 *device_address_ptr = dev_addr;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700474
Bernie Thompson530f43a2010-02-15 06:46:21 -0800475 return;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700476}
477
Bernie Thompson530f43a2010-02-15 06:46:21 -0800478/*
479 * There are 3 copies of every pixel: The front buffer that the fbdev
480 * client renders to, the actual framebuffer across the USB bus in hardware
481 * (that we can only write to, slowly, and can never read), and (optionally)
482 * our shadow copy that tracks what's been sent to that hardware buffer.
483 */
484static void dlfb_render_hline(struct dlfb_data *dev, struct urb **urb_ptr,
485 const char *front, char **urb_buf_ptr,
486 u32 byte_offset, u32 byte_width,
487 int *ident_ptr, int *sent_ptr)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700488{
Bernie Thompson530f43a2010-02-15 06:46:21 -0800489 const u8 *line_start, *line_end, *next_pixel;
490 u32 dev_addr = dev->base16 + byte_offset;
491 struct urb *urb = *urb_ptr;
492 u8 *cmd = *urb_buf_ptr;
493 u8 *cmd_end = (u8 *) urb->transfer_buffer + urb->transfer_buffer_length;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700494
Bernie Thompson530f43a2010-02-15 06:46:21 -0800495 line_start = (u8 *) (front + byte_offset);
496 next_pixel = line_start;
497 line_end = next_pixel + byte_width;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700498
Bernie Thompson530f43a2010-02-15 06:46:21 -0800499 if (dev->backing_buffer) {
500 int offset;
501 const u8 *back_start = (u8 *) (dev->backing_buffer
502 + byte_offset);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700503
Bernie Thompson530f43a2010-02-15 06:46:21 -0800504 *ident_ptr += dlfb_trim_hline(back_start, &next_pixel,
505 &byte_width);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700506
Bernie Thompson530f43a2010-02-15 06:46:21 -0800507 offset = next_pixel - line_start;
508 line_end = next_pixel + byte_width;
509 dev_addr += offset;
510 back_start += offset;
511 line_start += offset;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700512
Bernie Thompson530f43a2010-02-15 06:46:21 -0800513 memcpy((char *)back_start, (char *) line_start,
514 byte_width);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700515 }
516
Bernie Thompson530f43a2010-02-15 06:46:21 -0800517 while (next_pixel < line_end) {
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700518
Bernie Thompson530f43a2010-02-15 06:46:21 -0800519 dlfb_compress_hline((const uint16_t **) &next_pixel,
520 (const uint16_t *) line_end, &dev_addr,
521 (u8 **) &cmd, (u8 *) cmd_end);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700522
Bernie Thompson530f43a2010-02-15 06:46:21 -0800523 if (cmd >= cmd_end) {
524 int len = cmd - (u8 *) urb->transfer_buffer;
525 if (dlfb_submit_urb(dev, urb, len))
526 return; /* lost pixels is set */
527 *sent_ptr += len;
528 urb = dlfb_get_urb(dev);
529 if (!urb)
530 return; /* lost_pixels is set */
531 *urb_ptr = urb;
532 cmd = urb->transfer_buffer;
533 cmd_end = &cmd[urb->transfer_buffer_length];
534 }
535 }
536
537 *urb_buf_ptr = cmd;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700538}
539
Bernie Thompson530f43a2010-02-15 06:46:21 -0800540int dlfb_handle_damage(struct dlfb_data *dev, int x, int y,
541 int width, int height, char *data)
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700542{
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700543 int i, ret;
Bernie Thompson530f43a2010-02-15 06:46:21 -0800544 char *cmd;
545 cycles_t start_cycles, end_cycles;
546 int bytes_sent = 0;
547 int bytes_identical = 0;
548 struct urb *urb;
549 int aligned_x;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700550
Bernie Thompson530f43a2010-02-15 06:46:21 -0800551 start_cycles = get_cycles();
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700552
Bernie Thompson530f43a2010-02-15 06:46:21 -0800553 aligned_x = DL_ALIGN_DOWN(x, sizeof(unsigned long));
554 width = DL_ALIGN_UP(width + (x-aligned_x), sizeof(unsigned long));
555 x = aligned_x;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700556
Bernie Thompson530f43a2010-02-15 06:46:21 -0800557 if ((width <= 0) ||
558 (x + width > dev->info->var.xres) ||
559 (y + height > dev->info->var.yres))
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700560 return -EINVAL;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700561
Bernie Thompson530f43a2010-02-15 06:46:21 -0800562 if (!atomic_read(&dev->usb_active))
563 return 0;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700564
Bernie Thompson530f43a2010-02-15 06:46:21 -0800565 urb = dlfb_get_urb(dev);
566 if (!urb)
567 return 0;
568 cmd = urb->transfer_buffer;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700569
Bernie Thompson530f43a2010-02-15 06:46:21 -0800570 for (i = y; i < y + height ; i++) {
571 const int line_offset = dev->info->fix.line_length * i;
572 const int byte_offset = line_offset + (x * BPP);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700573
Bernie Thompson530f43a2010-02-15 06:46:21 -0800574 dlfb_render_hline(dev, &urb, (char *) dev->info->fix.smem_start,
575 &cmd, byte_offset, width * BPP,
576 &bytes_identical, &bytes_sent);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700577 }
578
Bernie Thompson530f43a2010-02-15 06:46:21 -0800579 if (cmd > (char *) urb->transfer_buffer) {
580 /* Send partial buffer remaining before exiting */
581 int len = cmd - (char *) urb->transfer_buffer;
582 ret = dlfb_submit_urb(dev, urb, len);
583 bytes_sent += len;
584 } else
585 dlfb_urb_completion(urb);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700586
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
725 dlfb_render_hline(dev, &urb, (char *) info->fix.smem_start,
726 &cmd, cur->index << PAGE_SHIFT,
727 PAGE_SIZE, &bytes_identical, &bytes_sent);
728 bytes_rendered += PAGE_SIZE;
729 }
730
731 if (cmd > (char *) urb->transfer_buffer) {
732 /* Send partial buffer remaining before exiting */
733 int len = cmd - (char *) urb->transfer_buffer;
734 dlfb_submit_urb(dev, urb, len);
735 bytes_sent += len;
736 } else
737 dlfb_urb_completion(urb);
738
739 atomic_add(bytes_sent, &dev->bytes_sent);
740 atomic_add(bytes_identical, &dev->bytes_identical);
741 atomic_add(bytes_rendered, &dev->bytes_rendered);
742 end_cycles = get_cycles();
743 atomic_add(((unsigned int) ((end_cycles - start_cycles)
744 >> 10)), /* Kcycles */
745 &dev->cpu_kcycles_used);
746}
747
748#endif
749
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700750static int dlfb_get_edid(struct dlfb_data *dev, char *edid, int len)
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800751{
752 int i;
753 int ret;
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700754 char *rbuf;
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800755
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700756 rbuf = kmalloc(2, GFP_KERNEL);
757 if (!rbuf)
758 return 0;
759
760 for (i = 0; i < len; i++) {
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800761 ret = usb_control_msg(dev->udev,
762 usb_rcvctrlpipe(dev->udev, 0), (0x02),
763 (0x80 | (0x02 << 5)), i << 8, 0xA1, rbuf, 2,
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700764 HZ);
765 if (ret < 1) {
766 dl_err("Read EDID byte %d failed err %x\n", i, ret);
767 i--;
768 break;
769 }
770 edid[i] = rbuf[1];
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800771 }
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700772
773 kfree(rbuf);
774
775 return i;
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800776}
777
Bernie Thompson45742032010-02-15 06:46:04 -0800778static int dlfb_ops_ioctl(struct fb_info *info, unsigned int cmd,
Bernie Thompson530f43a2010-02-15 06:46:21 -0800779 unsigned long arg)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700780{
Bernie Thompson530f43a2010-02-15 06:46:21 -0800781
782 struct dlfb_data *dev = info->par;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700783 struct dloarea *area = NULL;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700784
Bernie Thompson530f43a2010-02-15 06:46:21 -0800785 if (!atomic_read(&dev->usb_active))
786 return 0;
787
788 /* TODO: Update X server to get this from sysfs instead */
789 if (cmd == DLFB_IOCTL_RETURN_EDID) {
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700790 char *edid = (char *)arg;
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700791 if (copy_to_user(edid, dev->edid, dev->edid_size))
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700792 return -EFAULT;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700793 return 0;
794 }
795
Bernie Thompson530f43a2010-02-15 06:46:21 -0800796 /* TODO: Help propose a standard fb.h ioctl to report mmap damage */
797 if (cmd == DLFB_IOCTL_REPORT_DAMAGE) {
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700798
799 area = (struct dloarea *)arg;
800
801 if (area->x < 0)
802 area->x = 0;
803
804 if (area->x > info->var.xres)
805 area->x = info->var.xres;
806
807 if (area->y < 0)
808 area->y = 0;
809
810 if (area->y > info->var.yres)
811 area->y = info->var.yres;
812
Bernie Thompson530f43a2010-02-15 06:46:21 -0800813 dlfb_handle_damage(dev, area->x, area->y, area->w, area->h,
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700814 info->screen_base);
815 }
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700816
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700817 return 0;
818}
819
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700820/* taken from vesafb */
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700821static int
Bernie Thompson45742032010-02-15 06:46:04 -0800822dlfb_ops_setcolreg(unsigned regno, unsigned red, unsigned green,
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700823 unsigned blue, unsigned transp, struct fb_info *info)
824{
825 int err = 0;
826
827 if (regno >= info->cmap.len)
828 return 1;
829
830 if (regno < 16) {
831 if (info->var.red.offset == 10) {
832 /* 1:5:5:5 */
833 ((u32 *) (info->pseudo_palette))[regno] =
834 ((red & 0xf800) >> 1) |
835 ((green & 0xf800) >> 6) | ((blue & 0xf800) >> 11);
836 } else {
837 /* 0:5:6:5 */
838 ((u32 *) (info->pseudo_palette))[regno] =
839 ((red & 0xf800)) |
840 ((green & 0xfc00) >> 5) | ((blue & 0xf800) >> 11);
841 }
842 }
843
844 return err;
845}
846
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800847/*
848 * It's common for several clients to have framebuffer open simultaneously.
849 * e.g. both fbcon and X. Makes things interesting.
Bernie Thompson33077b82010-09-05 16:35:19 -0700850 * Assumes caller is holding info->lock (for open and release at least)
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800851 */
852static int dlfb_ops_open(struct fb_info *info, int user)
853{
854 struct dlfb_data *dev = info->par;
855
Bernie Thompsond5ed5432010-09-05 16:35:39 -0700856 /*
857 * fbcon aggressively connects to first framebuffer it finds,
858 * preventing other clients (X) from working properly. Usually
859 * not what the user wants. Fail by default with option to enable.
860 */
861 if ((user == 0) & (!console))
862 return -EBUSY;
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800863
Bernie Thompson33077b82010-09-05 16:35:19 -0700864 /* If the USB device is gone, we don't accept new opens */
865 if (dev->virtualized)
866 return -ENODEV;
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800867
868 dev->fb_count++;
869
Bernie Thompson33077b82010-09-05 16:35:19 -0700870 kref_get(&dev->kref);
871
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800872#ifdef CONFIG_FB_DEFERRED_IO
Bernie Thompsond5ed5432010-09-05 16:35:39 -0700873 if (fb_defio && (info->fbdefio == NULL)) {
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800874 /* enable defio */
875 info->fbdefio = &dlfb_defio;
876 fb_deferred_io_init(info);
877 }
878#endif
879
880 dl_notice("open /dev/fb%d user=%d fb_info=%p count=%d\n",
881 info->node, user, info, dev->fb_count);
882
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700883 return 0;
884}
885
Bernie Thompson4a4854d2010-02-15 06:45:55 -0800886/*
887 * Called when all client interfaces to start transactions have been disabled,
888 * and all references to our device instance (dlfb_data) are released.
889 * Every transaction must have a reference, so we know are fully spun down
890 */
Bernie Thompson33077b82010-09-05 16:35:19 -0700891static void dlfb_free(struct kref *kref)
Bernie Thompson4a4854d2010-02-15 06:45:55 -0800892{
893 struct dlfb_data *dev = container_of(kref, struct dlfb_data, kref);
894
Bernie Thompson33077b82010-09-05 16:35:19 -0700895 /* this function will wait for all in-flight urbs to complete */
896 if (dev->urbs.count > 0)
897 dlfb_free_urb_list(dev);
898
Bernie Thompson4a4854d2010-02-15 06:45:55 -0800899 if (dev->backing_buffer)
900 vfree(dev->backing_buffer);
901
Bernie Thompson33077b82010-09-05 16:35:19 -0700902 kfree(dev->edid);
903
904 dl_warn("freeing dlfb_data %p\n", dev);
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800905
Bernie Thompson4a4854d2010-02-15 06:45:55 -0800906 kfree(dev);
907}
908
Bernie Thompson33077b82010-09-05 16:35:19 -0700909
910static void dlfb_free_framebuffer_work(struct work_struct *work)
Bernie Thompson2469d5d2010-02-15 06:46:13 -0800911{
Bernie Thompson33077b82010-09-05 16:35:19 -0700912 struct dlfb_data *dev = container_of(work, struct dlfb_data,
913 free_framebuffer_work.work);
914 struct fb_info *info = dev->info;
915 int node = info->node;
916
917 unregister_framebuffer(info);
Bernie Thompson2469d5d2010-02-15 06:46:13 -0800918
919 if (info->cmap.len != 0)
920 fb_dealloc_cmap(&info->cmap);
921 if (info->monspecs.modedb)
922 fb_destroy_modedb(info->monspecs.modedb);
923 if (info->screen_base)
924 vfree(info->screen_base);
925
926 fb_destroy_modelist(&info->modelist);
927
Bernie Thompson33077b82010-09-05 16:35:19 -0700928 dev->info = 0;
929
930 /* Assume info structure is freed after this point */
Bernie Thompson2469d5d2010-02-15 06:46:13 -0800931 framebuffer_release(info);
932
Bernie Thompson33077b82010-09-05 16:35:19 -0700933 dl_warn("fb_info for /dev/fb%d has been freed\n", node);
934
935 /* ref taken in probe() as part of registering framebfufer */
936 kref_put(&dev->kref, dlfb_free);
937}
938
939/*
940 * Assumes caller is holding info->lock mutex (for open and release at least)
941 */
942static int dlfb_ops_release(struct fb_info *info, int user)
943{
944 struct dlfb_data *dev = info->par;
945
946 dev->fb_count--;
947
948 /* We can't free fb_info here - fbmem will touch it when we return */
949 if (dev->virtualized && (dev->fb_count == 0))
950 schedule_delayed_work(&dev->free_framebuffer_work, HZ);
951
952#ifdef CONFIG_FB_DEFERRED_IO
953 if ((dev->fb_count == 0) && (info->fbdefio)) {
954 fb_deferred_io_cleanup(info);
955 kfree(info->fbdefio);
956 info->fbdefio = NULL;
957 info->fbops->fb_mmap = dlfb_ops_mmap;
958 }
959#endif
960
961 dl_warn("released /dev/fb%d user=%d count=%d\n",
962 info->node, user, dev->fb_count);
963
964 kref_put(&dev->kref, dlfb_free);
965
966 return 0;
Bernie Thompson2469d5d2010-02-15 06:46:13 -0800967}
968
969/*
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800970 * Check whether a video mode is supported by the DisplayLink chip
971 * We start from monitor's modes, so don't need to filter that here
972 */
973static int dlfb_is_valid_mode(struct fb_videomode *mode,
974 struct fb_info *info)
975{
976 struct dlfb_data *dev = info->par;
977
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700978 if (mode->xres * mode->yres > dev->sku_pixel_limit) {
979 dl_warn("%dx%d beyond chip capabilities\n",
980 mode->xres, mode->yres);
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800981 return 0;
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700982 }
983
984 dl_info("%dx%d valid mode\n", mode->xres, mode->yres);
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800985
986 return 1;
987}
988
989static void dlfb_var_color_format(struct fb_var_screeninfo *var)
990{
991 const struct fb_bitfield red = { 11, 5, 0 };
992 const struct fb_bitfield green = { 5, 6, 0 };
993 const struct fb_bitfield blue = { 0, 5, 0 };
994
995 var->bits_per_pixel = 16;
996 var->red = red;
997 var->green = green;
998 var->blue = blue;
999}
1000
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001001static int dlfb_ops_check_var(struct fb_var_screeninfo *var,
1002 struct fb_info *info)
1003{
1004 struct fb_videomode mode;
1005
1006 /* TODO: support dynamically changing framebuffer size */
1007 if ((var->xres * var->yres * 2) > info->fix.smem_len)
1008 return -EINVAL;
1009
1010 /* set device-specific elements of var unrelated to mode */
1011 dlfb_var_color_format(var);
1012
1013 fb_var_to_videomode(&mode, var);
1014
1015 if (!dlfb_is_valid_mode(&mode, info))
1016 return -EINVAL;
1017
1018 return 0;
1019}
1020
1021static int dlfb_ops_set_par(struct fb_info *info)
1022{
1023 struct dlfb_data *dev = info->par;
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001024 int result;
1025 u16 *pix_framebuffer;
1026 int i;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001027
1028 dl_notice("set_par mode %dx%d\n", info->var.xres, info->var.yres);
1029
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001030 result = dlfb_set_video_mode(dev, &info->var);
1031
1032 if ((result == 0) && (dev->fb_count == 0)) {
1033
1034 /* paint greenscreen */
1035
1036 pix_framebuffer = (u16 *) info->screen_base;
1037 for (i = 0; i < info->fix.smem_len / 2; i++)
1038 pix_framebuffer[i] = 0x37e6;
1039
1040 dlfb_handle_damage(dev, 0, 0, info->var.xres, info->var.yres,
1041 info->screen_base);
1042 }
1043
1044 return result;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001045}
1046
Bernie Thompson9825f702010-09-05 16:35:10 -07001047/*
1048 * In order to come back from full DPMS off, we need to set the mode again
1049 */
Bernie Thompson45742032010-02-15 06:46:04 -08001050static int dlfb_ops_blank(int blank_mode, struct fb_info *info)
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -07001051{
Bernie Thompson530f43a2010-02-15 06:46:21 -08001052 struct dlfb_data *dev = info->par;
Roberto De Ioris7316bc52009-06-10 23:02:19 -07001053
Roberto De Ioris7316bc52009-06-10 23:02:19 -07001054 if (blank_mode != FB_BLANK_UNBLANK) {
Bernie Thompson9825f702010-09-05 16:35:10 -07001055 char *bufptr;
1056 struct urb *urb;
Roberto De Ioris7316bc52009-06-10 23:02:19 -07001057
Bernie Thompson9825f702010-09-05 16:35:10 -07001058 urb = dlfb_get_urb(dev);
1059 if (!urb)
1060 return 0;
1061
1062 bufptr = (char *) urb->transfer_buffer;
1063 bufptr = dlfb_vidreg_lock(bufptr);
1064 bufptr = dlfb_enable_hvsync(bufptr, false);
1065 bufptr = dlfb_vidreg_unlock(bufptr);
1066
1067 dlfb_submit_urb(dev, urb, bufptr -
1068 (char *) urb->transfer_buffer);
1069 } else {
1070 dlfb_set_video_mode(dev, &info->var);
1071 }
Roberto De Ioris7316bc52009-06-10 23:02:19 -07001072
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001073 return 0;
1074}
1075
1076static struct fb_ops dlfb_ops = {
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001077 .owner = THIS_MODULE,
Bernie Thompsond46ecb92010-09-05 16:35:04 -07001078 .fb_read = dlfb_ops_read,
1079 .fb_write = dlfb_ops_write,
Bernie Thompson45742032010-02-15 06:46:04 -08001080 .fb_setcolreg = dlfb_ops_setcolreg,
1081 .fb_fillrect = dlfb_ops_fillrect,
1082 .fb_copyarea = dlfb_ops_copyarea,
1083 .fb_imageblit = dlfb_ops_imageblit,
1084 .fb_mmap = dlfb_ops_mmap,
1085 .fb_ioctl = dlfb_ops_ioctl,
Bernie Thompson3e8f3d62010-02-15 06:46:26 -08001086 .fb_open = dlfb_ops_open,
Bernie Thompson45742032010-02-15 06:46:04 -08001087 .fb_release = dlfb_ops_release,
1088 .fb_blank = dlfb_ops_blank,
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001089 .fb_check_var = dlfb_ops_check_var,
1090 .fb_set_par = dlfb_ops_set_par,
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001091};
1092
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001093
Bernie Thompsoncc403dc2010-02-15 06:45:49 -08001094/*
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001095 * Assumes &info->lock held by caller
1096 * Assumes no active clients have framebuffer open
1097 */
1098static int dlfb_realloc_framebuffer(struct dlfb_data *dev, struct fb_info *info)
1099{
1100 int retval = -ENOMEM;
1101 int old_len = info->fix.smem_len;
1102 int new_len;
1103 unsigned char *old_fb = info->screen_base;
1104 unsigned char *new_fb;
1105 unsigned char *new_back;
1106
1107 dl_warn("Reallocating framebuffer. Addresses will change!\n");
1108
1109 new_len = info->fix.line_length * info->var.yres;
1110
1111 if (PAGE_ALIGN(new_len) > old_len) {
1112 /*
1113 * Alloc system memory for virtual framebuffer
1114 */
1115 new_fb = vmalloc(new_len);
1116 if (!new_fb) {
1117 dl_err("Virtual framebuffer alloc failed\n");
1118 goto error;
1119 }
1120
1121 if (info->screen_base) {
1122 memcpy(new_fb, old_fb, old_len);
1123 vfree(info->screen_base);
1124 }
1125
1126 info->screen_base = new_fb;
1127 info->fix.smem_len = PAGE_ALIGN(new_len);
1128 info->fix.smem_start = (unsigned long) new_fb;
1129 info->flags = udlfb_info_flags;
1130
1131 /*
1132 * Second framebuffer copy to mirror the framebuffer state
1133 * on the physical USB device. We can function without this.
1134 * But with imperfect damage info we may send pixels over USB
1135 * that were, in fact, unchanged - wasting limited USB bandwidth
1136 */
1137 new_back = vmalloc(new_len);
1138 if (!new_back)
1139 dl_info("No shadow/backing buffer allcoated\n");
1140 else {
1141 if (dev->backing_buffer)
1142 vfree(dev->backing_buffer);
1143 dev->backing_buffer = new_back;
1144 memset(dev->backing_buffer, 0, new_len);
1145 }
1146 }
1147
1148 retval = 0;
1149
1150error:
1151 return retval;
1152}
1153
1154/*
1155 * 1) Get EDID from hw, or use sw default
1156 * 2) Parse into various fb_info structs
1157 * 3) Allocate virtual framebuffer memory to back highest res mode
1158 *
1159 * Parses EDID into three places used by various parts of fbdev:
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001160 * fb_var_screeninfo contains the timing of the monitor's preferred mode
1161 * fb_info.monspecs is full parsed EDID info, including monspecs.modedb
1162 * fb_info.modelist is a linked list of all monitor & VESA modes which work
1163 *
1164 * If EDID is not readable/valid, then modelist is all VESA modes,
1165 * monspecs is NULL, and fb_var_screeninfo is set to safe VESA mode
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001166 * Returns 0 if successful
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001167 */
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001168static int dlfb_setup_modes(struct dlfb_data *dev,
1169 struct fb_info *info,
1170 char *default_edid, size_t default_edid_size)
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001171{
1172 int i;
1173 const struct fb_videomode *default_vmode = NULL;
1174 int result = 0;
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001175 char *edid;
1176 int tries = 3;
1177
1178 if (info->dev) /* only use mutex if info has been registered */
1179 mutex_lock(&info->lock);
1180
1181 edid = kmalloc(MAX_EDID_SIZE, GFP_KERNEL);
1182 if (!edid) {
1183 result = -ENOMEM;
1184 goto error;
1185 }
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001186
1187 fb_destroy_modelist(&info->modelist);
1188 memset(&info->monspecs, 0, sizeof(info->monspecs));
1189
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001190 /*
1191 * Try to (re)read EDID from hardware first
1192 * EDID data may return, but not parse as valid
1193 * Try again a few times, in case of e.g. analog cable noise
1194 */
1195 while (tries--) {
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001196
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001197 i = dlfb_get_edid(dev, edid, MAX_EDID_SIZE);
1198
1199 if (i >= MIN_EDID_SIZE)
1200 fb_edid_to_monspecs(edid, &info->monspecs);
1201
1202 if (info->monspecs.modedb_len > 0) {
1203 dev->edid = edid;
1204 dev->edid_size = i;
1205 break;
1206 }
1207 }
1208
1209 /* If that fails, use a previously returned EDID if available */
1210 if (info->monspecs.modedb_len == 0) {
1211
1212 dl_err("Unable to get valid EDID from device/display\n");
1213
1214 if (dev->edid) {
1215 fb_edid_to_monspecs(dev->edid, &info->monspecs);
1216 if (info->monspecs.modedb_len > 0)
1217 dl_err("Using previously queried EDID\n");
1218 }
1219 }
1220
1221 /* If that fails, use the default EDID we were handed */
1222 if (info->monspecs.modedb_len == 0) {
1223 if (default_edid_size >= MIN_EDID_SIZE) {
1224 fb_edid_to_monspecs(default_edid, &info->monspecs);
1225 if (info->monspecs.modedb_len > 0) {
1226 memcpy(edid, default_edid, default_edid_size);
1227 dev->edid = edid;
1228 dev->edid_size = default_edid_size;
1229 dl_err("Using default/backup EDID\n");
1230 }
1231 }
1232 }
1233
1234 /* If we've got modes, let's pick a best default mode */
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001235 if (info->monspecs.modedb_len > 0) {
1236
1237 for (i = 0; i < info->monspecs.modedb_len; i++) {
1238 if (dlfb_is_valid_mode(&info->monspecs.modedb[i], info))
1239 fb_add_videomode(&info->monspecs.modedb[i],
1240 &info->modelist);
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001241 else /* if we've removed top/best mode */
1242 info->monspecs.misc &= ~FB_MISC_1ST_DETAIL;
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001243 }
1244
1245 default_vmode = fb_find_best_display(&info->monspecs,
1246 &info->modelist);
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001247 }
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001248
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001249 /* If everything else has failed, fall back to safe default mode */
1250 if (default_vmode == NULL) {
1251
1252 struct fb_videomode fb_vmode = {0};
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001253
1254 /*
1255 * Add the standard VESA modes to our modelist
1256 * Since we don't have EDID, there may be modes that
1257 * overspec monitor and/or are incorrect aspect ratio, etc.
1258 * But at least the user has a chance to choose
1259 */
1260 for (i = 0; i < VESA_MODEDB_SIZE; i++) {
1261 if (dlfb_is_valid_mode((struct fb_videomode *)
1262 &vesa_modes[i], info))
1263 fb_add_videomode(&vesa_modes[i],
1264 &info->modelist);
1265 }
1266
1267 /*
1268 * default to resolution safe for projectors
1269 * (since they are most common case without EDID)
1270 */
1271 fb_vmode.xres = 800;
1272 fb_vmode.yres = 600;
1273 fb_vmode.refresh = 60;
1274 default_vmode = fb_find_nearest_mode(&fb_vmode,
1275 &info->modelist);
1276 }
1277
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001278 /* If we have good mode and no active clients*/
1279 if ((default_vmode != NULL) && (dev->fb_count == 0)) {
1280
1281 fb_videomode_to_var(&info->var, default_vmode);
1282 dlfb_var_color_format(&info->var);
1283
1284 /*
1285 * with mode size info, we can now alloc our framebuffer.
1286 */
1287 memcpy(&info->fix, &dlfb_fix, sizeof(dlfb_fix));
1288 info->fix.line_length = info->var.xres *
1289 (info->var.bits_per_pixel / 8);
1290
1291 result = dlfb_realloc_framebuffer(dev, info);
1292
1293 } else
1294 result = -EINVAL;
1295
1296error:
1297 if (edid && (dev->edid != edid))
1298 kfree(edid);
1299
1300 if (info->dev)
1301 mutex_unlock(&info->lock);
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001302
1303 return result;
1304}
1305
1306static ssize_t metrics_bytes_rendered_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_rendered));
1312}
1313
1314static ssize_t metrics_bytes_identical_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_identical));
1320}
1321
1322static ssize_t metrics_bytes_sent_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->bytes_sent));
1328}
1329
1330static ssize_t metrics_cpu_kcycles_used_show(struct device *fbdev,
1331 struct device_attribute *a, char *buf) {
1332 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1333 struct dlfb_data *dev = fb_info->par;
1334 return snprintf(buf, PAGE_SIZE, "%u\n",
1335 atomic_read(&dev->cpu_kcycles_used));
1336}
1337
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001338static ssize_t edid_show(
1339 struct file *filp,
1340 struct kobject *kobj, struct bin_attribute *a,
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001341 char *buf, loff_t off, size_t count) {
1342 struct device *fbdev = container_of(kobj, struct device, kobj);
1343 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1344 struct dlfb_data *dev = fb_info->par;
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001345
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001346 if (dev->edid == NULL)
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001347 return 0;
1348
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001349 if ((off >= dev->edid_size) || (count > dev->edid_size))
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001350 return 0;
1351
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001352 if (off + count > dev->edid_size)
1353 count = dev->edid_size - off;
1354
1355 dl_info("sysfs edid copy %p to %p, %d bytes\n",
1356 dev->edid, buf, (int) count);
1357
1358 memcpy(buf, dev->edid, count);
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001359
1360 return count;
1361}
1362
Bernie Thompson8ef8cc42010-09-05 16:35:31 -07001363static ssize_t edid_store(
1364 struct file *filp,
1365 struct kobject *kobj, struct bin_attribute *a,
1366 char *src, loff_t src_off, size_t src_size) {
1367 struct device *fbdev = container_of(kobj, struct device, kobj);
1368 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1369 struct dlfb_data *dev = fb_info->par;
1370
1371 /* We only support write of entire EDID at once, no offset*/
1372 if ((src_size < MIN_EDID_SIZE) ||
1373 (src_size > MAX_EDID_SIZE) ||
1374 (src_off != 0))
1375 return 0;
1376
1377 dlfb_setup_modes(dev, fb_info, src, src_size);
1378
1379 if (dev->edid && (memcmp(src, dev->edid, src_size) == 0)) {
1380 dl_info("sysfs written EDID is new default\n");
1381 dlfb_ops_set_par(fb_info);
1382 return src_size;
1383 } else
1384 return 0;
1385}
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001386
1387static ssize_t metrics_reset_store(struct device *fbdev,
1388 struct device_attribute *attr,
1389 const char *buf, size_t count)
1390{
1391 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1392 struct dlfb_data *dev = fb_info->par;
1393
1394 atomic_set(&dev->bytes_rendered, 0);
1395 atomic_set(&dev->bytes_identical, 0);
1396 atomic_set(&dev->bytes_sent, 0);
1397 atomic_set(&dev->cpu_kcycles_used, 0);
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001398
1399 return count;
1400}
1401
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001402static struct bin_attribute edid_attr = {
1403 .attr.name = "edid",
Bernie Thompson8ef8cc42010-09-05 16:35:31 -07001404 .attr.mode = 0666,
1405 .size = MAX_EDID_SIZE,
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001406 .read = edid_show,
Bernie Thompson8ef8cc42010-09-05 16:35:31 -07001407 .write = edid_store
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001408};
1409
1410static struct device_attribute fb_device_attrs[] = {
1411 __ATTR_RO(metrics_bytes_rendered),
1412 __ATTR_RO(metrics_bytes_identical),
1413 __ATTR_RO(metrics_bytes_sent),
1414 __ATTR_RO(metrics_cpu_kcycles_used),
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001415 __ATTR(metrics_reset, S_IWUGO, NULL, metrics_reset_store),
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001416};
1417
Bernie Thompson3e8f3d62010-02-15 06:46:26 -08001418#ifdef CONFIG_FB_DEFERRED_IO
Bernie Thompson3e8f3d62010-02-15 06:46:26 -08001419
1420static struct fb_deferred_io dlfb_defio = {
1421 .delay = 5,
1422 .deferred_io = dlfb_dpy_deferred_io,
1423};
1424
1425#endif
1426
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001427/*
Bernie Thompsoncc403dc2010-02-15 06:45:49 -08001428 * This is necessary before we can communicate with the display controller.
1429 */
1430static int dlfb_select_std_channel(struct dlfb_data *dev)
1431{
1432 int ret;
1433 u8 set_def_chn[] = { 0x57, 0xCD, 0xDC, 0xA7,
1434 0x1C, 0x88, 0x5E, 0x15,
1435 0x60, 0xFE, 0xC6, 0x97,
1436 0x16, 0x3D, 0x47, 0xF2 };
1437
1438 ret = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
1439 NR_USB_REQUEST_CHANNEL,
1440 (USB_DIR_OUT | USB_TYPE_VENDOR), 0, 0,
1441 set_def_chn, sizeof(set_def_chn), USB_CTRL_SET_TIMEOUT);
1442 return ret;
1443}
1444
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001445static int dlfb_parse_vendor_descriptor(struct dlfb_data *dev,
1446 struct usb_device *usbdev)
1447{
1448 char *desc;
1449 char *buf;
1450 char *desc_end;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001451
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001452 u8 total_len = 0;
1453
1454 buf = kzalloc(MAX_VENDOR_DESCRIPTOR_SIZE, GFP_KERNEL);
1455 if (!buf)
1456 return false;
1457 desc = buf;
1458
1459 total_len = usb_get_descriptor(usbdev, 0x5f, /* vendor specific */
1460 0, desc, MAX_VENDOR_DESCRIPTOR_SIZE);
1461 if (total_len > 5) {
1462 dl_info("vendor descriptor length:%x data:%02x %02x %02x %02x" \
1463 "%02x %02x %02x %02x %02x %02x %02x\n",
1464 total_len, desc[0],
1465 desc[1], desc[2], desc[3], desc[4], desc[5], desc[6],
1466 desc[7], desc[8], desc[9], desc[10]);
1467
1468 if ((desc[0] != total_len) || /* descriptor length */
1469 (desc[1] != 0x5f) || /* vendor descriptor type */
1470 (desc[2] != 0x01) || /* version (2 bytes) */
1471 (desc[3] != 0x00) ||
1472 (desc[4] != total_len - 2)) /* length after type */
1473 goto unrecognized;
1474
1475 desc_end = desc + total_len;
1476 desc += 5; /* the fixed header we've already parsed */
1477
1478 while (desc < desc_end) {
1479 u8 length;
1480 u16 key;
1481
1482 key = *((u16 *) desc);
1483 desc += sizeof(u16);
1484 length = *desc;
1485 desc++;
1486
1487 switch (key) {
1488 case 0x0200: { /* max_area */
1489 u32 max_area;
1490 max_area = le32_to_cpu(*((u32 *)desc));
1491 dl_warn("DL chip limited to %d pixel modes\n",
1492 max_area);
1493 dev->sku_pixel_limit = max_area;
1494 break;
1495 }
1496 default:
1497 break;
1498 }
1499 desc += length;
1500 }
1501 }
1502
1503 goto success;
1504
1505unrecognized:
1506 /* allow udlfb to load for now even if firmware unrecognized */
1507 dl_err("Unrecognized vendor firmware descriptor\n");
1508
1509success:
1510 kfree(buf);
1511 return true;
1512}
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001513static int dlfb_usb_probe(struct usb_interface *interface,
Bernie Thompson59277b62009-11-24 15:52:21 -08001514 const struct usb_device_id *id)
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001515{
Bernie Thompson59277b62009-11-24 15:52:21 -08001516 struct usb_device *usbdev;
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001517 struct dlfb_data *dev = 0;
Bernie Thompson33077b82010-09-05 16:35:19 -07001518 struct fb_info *info = 0;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001519 int i;
Bernie Thompson59277b62009-11-24 15:52:21 -08001520 int retval = -ENOMEM;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001521
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001522 /* usb initialization */
1523
1524 usbdev = interface_to_usbdev(interface);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001525
Bernie Thompson59277b62009-11-24 15:52:21 -08001526 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1527 if (dev == NULL) {
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001528 err("dlfb_usb_probe: failed alloc of dev struct\n");
1529 goto error;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001530 }
1531
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001532 /* we need to wait for both usb and fbdev to spin down on disconnect */
1533 kref_init(&dev->kref); /* matching kref_put in usb .disconnect fn */
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001534 kref_get(&dev->kref); /* matching kref_put in free_framebuffer_work */
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001535
Bernie Thompson59277b62009-11-24 15:52:21 -08001536 dev->udev = usbdev;
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001537 dev->gdev = &usbdev->dev; /* our generic struct device * */
Bernie Thompson59277b62009-11-24 15:52:21 -08001538 usb_set_intfdata(interface, dev);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001539
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001540 dl_info("%s %s - serial #%s\n",
1541 usbdev->manufacturer, usbdev->product, usbdev->serial);
1542 dl_info("vid_%04x&pid_%04x&rev_%04x driver's dlfb_data struct at %p\n",
1543 usbdev->descriptor.idVendor, usbdev->descriptor.idProduct,
1544 usbdev->descriptor.bcdDevice, dev);
Bernie Thompsond5ed5432010-09-05 16:35:39 -07001545 dl_info("console enable=%d\n", console);
1546 dl_info("fb_defio enable=%d\n", fb_defio);
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001547
1548 dev->sku_pixel_limit = 2048 * 1152; /* default to maximum */
1549
1550 if (!dlfb_parse_vendor_descriptor(dev, usbdev)) {
1551 dl_err("firmware not recognized. Assume incompatible device\n");
1552 goto error;
1553 }
1554
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001555 if (!dlfb_alloc_urb_list(dev, WRITES_IN_FLIGHT, MAX_TRANSFER)) {
1556 retval = -ENOMEM;
1557 dl_err("dlfb_alloc_urb_list failed\n");
1558 goto error;
1559 }
1560
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001561 /* We don't register a new USB class. Our client interface is fbdev */
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001562
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001563 /* allocates framebuffer driver structure, not framebuffer memory */
1564 info = framebuffer_alloc(0, &usbdev->dev);
1565 if (!info) {
1566 retval = -ENOMEM;
1567 dl_err("framebuffer_alloc failed\n");
1568 goto error;
1569 }
Bernie Thompson33077b82010-09-05 16:35:19 -07001570
Bernie Thompson59277b62009-11-24 15:52:21 -08001571 dev->info = info;
1572 info->par = dev;
1573 info->pseudo_palette = dev->pseudo_palette;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001574 info->fbops = &dlfb_ops;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001575
Bernie Thompson59277b62009-11-24 15:52:21 -08001576 retval = fb_alloc_cmap(&info->cmap, 256, 0);
1577 if (retval < 0) {
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001578 dl_err("fb_alloc_cmap failed %x\n", retval);
1579 goto error;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001580 }
1581
Bernie Thompson33077b82010-09-05 16:35:19 -07001582 INIT_DELAYED_WORK(&dev->free_framebuffer_work,
1583 dlfb_free_framebuffer_work);
1584
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001585 INIT_LIST_HEAD(&info->modelist);
1586
1587 retval = dlfb_setup_modes(dev, info, NULL, 0);
1588 if (retval != 0) {
1589 dl_err("unable to find common mode for display and adapter\n");
1590 goto error;
1591 }
1592
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001593 /* ready to begin using device */
1594
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001595 atomic_set(&dev->usb_active, 1);
Bernie Thompson59277b62009-11-24 15:52:21 -08001596 dlfb_select_std_channel(dev);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001597
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001598 dlfb_ops_check_var(&info->var, info);
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001599 dlfb_ops_set_par(info);
1600
Bernie Thompson59277b62009-11-24 15:52:21 -08001601 retval = register_framebuffer(info);
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001602 if (retval < 0) {
1603 dl_err("register_framebuffer failed %d\n", retval);
1604 goto error;
1605 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001606
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001607 for (i = 0; i < ARRAY_SIZE(fb_device_attrs); i++)
1608 device_create_file(info->dev, &fb_device_attrs[i]);
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -07001609
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001610 device_create_bin_file(info->dev, &edid_attr);
1611
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001612 dl_info("DisplayLink USB device /dev/fb%d attached. %dx%d resolution."
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001613 " Using %dK framebuffer memory\n", info->node,
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001614 info->var.xres, info->var.yres,
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001615 ((dev->backing_buffer) ?
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001616 info->fix.smem_len * 2 : info->fix.smem_len) >> 10);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001617 return 0;
1618
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001619error:
1620 if (dev) {
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001621
Bernie Thompson33077b82010-09-05 16:35:19 -07001622 if (info) {
1623 if (info->cmap.len != 0)
1624 fb_dealloc_cmap(&info->cmap);
1625 if (info->monspecs.modedb)
1626 fb_destroy_modedb(info->monspecs.modedb);
1627 if (info->screen_base)
1628 vfree(info->screen_base);
1629
1630 fb_destroy_modelist(&info->modelist);
1631
1632 framebuffer_release(info);
1633 }
1634
1635 if (dev->backing_buffer)
1636 vfree(dev->backing_buffer);
1637
1638 kref_put(&dev->kref, dlfb_free); /* ref for framebuffer */
1639 kref_put(&dev->kref, dlfb_free); /* last ref from kref_init */
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001640
1641 /* dev has been deallocated. Do not dereference */
1642 }
1643
Bernie Thompson59277b62009-11-24 15:52:21 -08001644 return retval;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001645}
1646
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001647static void dlfb_usb_disconnect(struct usb_interface *interface)
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001648{
Bernie Thompson59277b62009-11-24 15:52:21 -08001649 struct dlfb_data *dev;
1650 struct fb_info *info;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001651 int i;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001652
Bernie Thompson59277b62009-11-24 15:52:21 -08001653 dev = usb_get_intfdata(interface);
Bernie Thompson59277b62009-11-24 15:52:21 -08001654 info = dev->info;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001655
Bernie Thompson33077b82010-09-05 16:35:19 -07001656 dl_info("USB disconnect starting\n");
1657
1658 /* we virtualize until all fb clients release. Then we free */
1659 dev->virtualized = true;
1660
1661 /* When non-active we'll update virtual framebuffer, but no new urbs */
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001662 atomic_set(&dev->usb_active, 0);
1663
Bernie Thompson33077b82010-09-05 16:35:19 -07001664 /* remove udlfb's sysfs interfaces */
1665 for (i = 0; i < ARRAY_SIZE(fb_device_attrs); i++)
1666 device_remove_file(info->dev, &fb_device_attrs[i]);
1667 device_remove_bin_file(info->dev, &edid_attr);
1668
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001669 usb_set_intfdata(interface, NULL);
1670
Bernie Thompson33077b82010-09-05 16:35:19 -07001671 /* if clients still have us open, will be freed on last close */
1672 if (dev->fb_count == 0)
1673 schedule_delayed_work(&dev->free_framebuffer_work, 0);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001674
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001675 /* release reference taken by kref_init in probe() */
Bernie Thompson33077b82010-09-05 16:35:19 -07001676 kref_put(&dev->kref, dlfb_free);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001677
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001678 /* consider dlfb_data freed */
1679
1680 return;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001681}
1682
1683static struct usb_driver dlfb_driver = {
1684 .name = "udlfb",
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001685 .probe = dlfb_usb_probe,
1686 .disconnect = dlfb_usb_disconnect,
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001687 .id_table = id_table,
1688};
1689
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001690static int __init dlfb_module_init(void)
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001691{
1692 int res;
1693
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001694 res = usb_register(&dlfb_driver);
1695 if (res)
1696 err("usb_register failed. Error number %d", res);
1697
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001698 return res;
1699}
1700
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001701static void __exit dlfb_module_exit(void)
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001702{
1703 usb_deregister(&dlfb_driver);
1704}
1705
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001706module_init(dlfb_module_init);
1707module_exit(dlfb_module_exit);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001708
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001709static void dlfb_urb_completion(struct urb *urb)
1710{
1711 struct urb_node *unode = urb->context;
1712 struct dlfb_data *dev = unode->dev;
1713 unsigned long flags;
1714
1715 /* sync/async unlink faults aren't errors */
1716 if (urb->status) {
1717 if (!(urb->status == -ENOENT ||
1718 urb->status == -ECONNRESET ||
1719 urb->status == -ESHUTDOWN)) {
1720 dl_err("%s - nonzero write bulk status received: %d\n",
1721 __func__, urb->status);
1722 atomic_set(&dev->lost_pixels, 1);
1723 }
1724 }
1725
1726 urb->transfer_buffer_length = dev->urbs.size; /* reset to actual */
1727
1728 spin_lock_irqsave(&dev->urbs.lock, flags);
1729 list_add_tail(&unode->entry, &dev->urbs.list);
1730 dev->urbs.available++;
1731 spin_unlock_irqrestore(&dev->urbs.lock, flags);
1732
1733 up(&dev->urbs.limit_sem);
1734}
1735
1736static void dlfb_free_urb_list(struct dlfb_data *dev)
1737{
1738 int count = dev->urbs.count;
1739 struct list_head *node;
1740 struct urb_node *unode;
1741 struct urb *urb;
1742 int ret;
1743 unsigned long flags;
1744
1745 dl_notice("Waiting for completes and freeing all render urbs\n");
1746
1747 /* keep waiting and freeing, until we've got 'em all */
1748 while (count--) {
Bernie Thompson33077b82010-09-05 16:35:19 -07001749
1750 /* Getting interrupted means a leak, but ok at shutdown*/
1751 ret = down_interruptible(&dev->urbs.limit_sem);
1752 if (ret)
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001753 break;
Bernie Thompson33077b82010-09-05 16:35:19 -07001754
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001755 spin_lock_irqsave(&dev->urbs.lock, flags);
1756
1757 node = dev->urbs.list.next; /* have reserved one with sem */
1758 list_del_init(node);
1759
1760 spin_unlock_irqrestore(&dev->urbs.lock, flags);
1761
1762 unode = list_entry(node, struct urb_node, entry);
1763 urb = unode->urb;
1764
1765 /* Free each separately allocated piece */
Greg Kroah-Hartmanc220cc32010-04-29 15:36:29 -07001766 usb_free_coherent(urb->dev, dev->urbs.size,
1767 urb->transfer_buffer, urb->transfer_dma);
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001768 usb_free_urb(urb);
1769 kfree(node);
1770 }
1771
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001772}
1773
1774static int dlfb_alloc_urb_list(struct dlfb_data *dev, int count, size_t size)
1775{
1776 int i = 0;
1777 struct urb *urb;
1778 struct urb_node *unode;
1779 char *buf;
1780
1781 spin_lock_init(&dev->urbs.lock);
1782
1783 dev->urbs.size = size;
1784 INIT_LIST_HEAD(&dev->urbs.list);
1785
1786 while (i < count) {
1787 unode = kzalloc(sizeof(struct urb_node), GFP_KERNEL);
1788 if (!unode)
1789 break;
1790 unode->dev = dev;
1791
1792 urb = usb_alloc_urb(0, GFP_KERNEL);
1793 if (!urb) {
1794 kfree(unode);
1795 break;
1796 }
1797 unode->urb = urb;
1798
Greg Kroah-Hartmanc220cc32010-04-29 15:36:29 -07001799 buf = usb_alloc_coherent(dev->udev, MAX_TRANSFER, GFP_KERNEL,
1800 &urb->transfer_dma);
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001801 if (!buf) {
1802 kfree(unode);
1803 usb_free_urb(urb);
1804 break;
1805 }
1806
1807 /* urb->transfer_buffer_length set to actual before submit */
1808 usb_fill_bulk_urb(urb, dev->udev, usb_sndbulkpipe(dev->udev, 1),
1809 buf, size, dlfb_urb_completion, unode);
1810 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1811
1812 list_add_tail(&unode->entry, &dev->urbs.list);
1813
1814 i++;
1815 }
1816
1817 sema_init(&dev->urbs.limit_sem, i);
1818 dev->urbs.count = i;
1819 dev->urbs.available = i;
1820
Soeren Moellerb5a21042010-05-14 19:03:00 +00001821 dl_notice("allocated %d %d byte urbs\n", i, (int) size);
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001822
1823 return i;
1824}
1825
1826static struct urb *dlfb_get_urb(struct dlfb_data *dev)
1827{
1828 int ret = 0;
1829 struct list_head *entry;
1830 struct urb_node *unode;
1831 struct urb *urb = NULL;
1832 unsigned long flags;
1833
1834 /* Wait for an in-flight buffer to complete and get re-queued */
1835 ret = down_timeout(&dev->urbs.limit_sem, GET_URB_TIMEOUT);
1836 if (ret) {
1837 atomic_set(&dev->lost_pixels, 1);
1838 dl_err("wait for urb interrupted: %x\n", ret);
1839 goto error;
1840 }
1841
1842 spin_lock_irqsave(&dev->urbs.lock, flags);
1843
1844 BUG_ON(list_empty(&dev->urbs.list)); /* reserved one with limit_sem */
1845 entry = dev->urbs.list.next;
1846 list_del_init(entry);
1847 dev->urbs.available--;
1848
1849 spin_unlock_irqrestore(&dev->urbs.lock, flags);
1850
1851 unode = list_entry(entry, struct urb_node, entry);
1852 urb = unode->urb;
1853
1854error:
1855 return urb;
1856}
1857
1858static int dlfb_submit_urb(struct dlfb_data *dev, struct urb *urb, size_t len)
1859{
1860 int ret;
1861
1862 BUG_ON(len > dev->urbs.size);
1863
1864 urb->transfer_buffer_length = len; /* set to actual payload len */
1865 ret = usb_submit_urb(urb, GFP_KERNEL);
1866 if (ret) {
1867 dlfb_urb_completion(urb); /* because no one else will */
1868 atomic_set(&dev->lost_pixels, 1);
1869 dl_err("usb_submit_urb error %x\n", ret);
1870 }
1871 return ret;
1872}
1873
Bernie Thompsond5ed5432010-09-05 16:35:39 -07001874module_param(console, bool, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
1875MODULE_PARM_DESC(console, "Allow fbcon to consume first framebuffer found");
1876
1877module_param(fb_defio, bool, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
1878MODULE_PARM_DESC(fb_defio, "Enable fb_defio mmap support. *Experimental*");
1879
Bernie Thompson59277b62009-11-24 15:52:21 -08001880MODULE_AUTHOR("Roberto De Ioris <roberto@unbit.it>, "
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001881 "Jaya Kumar <jayakumar.lkml@gmail.com>, "
1882 "Bernie Thompson <bernie@plugable.com>");
1883MODULE_DESCRIPTION("DisplayLink kernel framebuffer driver");
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001884MODULE_LICENSE("GPL");
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001885