blob: 6977b71e8c3c83ebc15c3a56e7c6fcb84bd2ecb6 [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 Thompsondd8015f2010-02-15 06:46:35 -080062#ifndef CONFIG_FB_DEFERRED_IO
Pavel Machek3b7b31f2010-04-03 07:00:37 +020063#warning Please set CONFIG_FB_DEFFERRED_IO option to support generic fbdev apps
Bernie Thompsondd8015f2010-02-15 06:46:35 -080064#endif
65
66#ifndef CONFIG_FB_SYS_IMAGEBLIT
67#ifndef CONFIG_FB_SYS_IMAGEBLIT_MODULE
Pavel Machek3b7b31f2010-04-03 07:00:37 +020068#warning Please set CONFIG_FB_SYS_IMAGEBLIT option to support fb console
Bernie Thompsondd8015f2010-02-15 06:46:35 -080069#endif
70#endif
71
72#ifndef CONFIG_FB_MODE_HELPERS
Pavel Machek3b7b31f2010-04-03 07:00:37 +020073#warning CONFIG_FB_MODE_HELPERS required. Expect build break
Bernie Thompsondd8015f2010-02-15 06:46:35 -080074#endif
75
Bernie Thompson4a4854d2010-02-15 06:45:55 -080076/* dlfb keeps a list of urbs for efficient bulk transfers */
77static void dlfb_urb_completion(struct urb *urb);
78static struct urb *dlfb_get_urb(struct dlfb_data *dev);
79static int dlfb_submit_urb(struct dlfb_data *dev, struct urb * urb, size_t len);
80static int dlfb_alloc_urb_list(struct dlfb_data *dev, int count, size_t size);
81static void dlfb_free_urb_list(struct dlfb_data *dev);
82
Bernie Thompson3e8f3d62010-02-15 06:46:26 -080083/* other symbols with dependents */
84#ifdef CONFIG_FB_DEFERRED_IO
85static struct fb_deferred_io dlfb_defio;
86#endif
87
Bernie Thompson59277b62009-11-24 15:52:21 -080088/*
Bernie Thompsonbd808162010-02-15 06:46:48 -080089 * All DisplayLink bulk operations start with 0xAF, followed by specific code
90 * All operations are written to buffers which then later get sent to device
Bernie Thompson59277b62009-11-24 15:52:21 -080091 */
Bernie Thompson45742032010-02-15 06:46:04 -080092static char *dlfb_set_register(char *buf, u8 reg, u8 val)
Roberto De Ioris88e58b12009-06-03 14:03:06 -070093{
Bernie Thompson1d31a9e2010-02-15 06:45:43 -080094 *buf++ = 0xAF;
95 *buf++ = 0x20;
96 *buf++ = reg;
97 *buf++ = val;
98 return buf;
Roberto De Ioris88e58b12009-06-03 14:03:06 -070099}
100
Bernie Thompson45742032010-02-15 06:46:04 -0800101static char *dlfb_vidreg_lock(char *buf)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700102{
Bernie Thompson45742032010-02-15 06:46:04 -0800103 return dlfb_set_register(buf, 0xFF, 0x00);
Bernie Thompson59277b62009-11-24 15:52:21 -0800104}
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700105
Bernie Thompson45742032010-02-15 06:46:04 -0800106static char *dlfb_vidreg_unlock(char *buf)
Bernie Thompson59277b62009-11-24 15:52:21 -0800107{
Bernie Thompson45742032010-02-15 06:46:04 -0800108 return dlfb_set_register(buf, 0xFF, 0xFF);
Bernie Thompson59277b62009-11-24 15:52:21 -0800109}
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700110
Bernie Thompson59277b62009-11-24 15:52:21 -0800111/*
Bernie Thompson530f43a2010-02-15 06:46:21 -0800112 * On/Off for driving the DisplayLink framebuffer to the display
Bernie Thompson9825f702010-09-05 16:35:10 -0700113 * 0x00 H and V sync on
114 * 0x01 H and V sync off (screen blank but powered)
115 * 0x07 DPMS powerdown (requires modeset to come back)
Bernie Thompson59277b62009-11-24 15:52:21 -0800116 */
Bernie Thompson530f43a2010-02-15 06:46:21 -0800117static char *dlfb_enable_hvsync(char *buf, bool enable)
Bernie Thompson59277b62009-11-24 15:52:21 -0800118{
Bernie Thompson530f43a2010-02-15 06:46:21 -0800119 if (enable)
120 return dlfb_set_register(buf, 0x1F, 0x00);
121 else
Bernie Thompson9825f702010-09-05 16:35:10 -0700122 return dlfb_set_register(buf, 0x1F, 0x07);
Bernie Thompson59277b62009-11-24 15:52:21 -0800123}
124
Bernie Thompson45742032010-02-15 06:46:04 -0800125static char *dlfb_set_color_depth(char *buf, u8 selection)
Bernie Thompson59277b62009-11-24 15:52:21 -0800126{
Bernie Thompson45742032010-02-15 06:46:04 -0800127 return dlfb_set_register(buf, 0x00, selection);
Bernie Thompson59277b62009-11-24 15:52:21 -0800128}
129
Bernie Thompson45742032010-02-15 06:46:04 -0800130static char *dlfb_set_base16bpp(char *wrptr, u32 base)
Bernie Thompson59277b62009-11-24 15:52:21 -0800131{
Bernie Thompson1d31a9e2010-02-15 06:45:43 -0800132 /* the base pointer is 16 bits wide, 0x20 is hi byte. */
Bernie Thompson45742032010-02-15 06:46:04 -0800133 wrptr = dlfb_set_register(wrptr, 0x20, base >> 16);
134 wrptr = dlfb_set_register(wrptr, 0x21, base >> 8);
135 return dlfb_set_register(wrptr, 0x22, base);
Bernie Thompson59277b62009-11-24 15:52:21 -0800136}
137
Bernie Thompsonbd808162010-02-15 06:46:48 -0800138/*
139 * DisplayLink HW has separate 16bpp and 8bpp framebuffers.
140 * In 24bpp modes, the low 323 RGB bits go in the 8bpp framebuffer
141 */
Bernie Thompson45742032010-02-15 06:46:04 -0800142static char *dlfb_set_base8bpp(char *wrptr, u32 base)
Bernie Thompson59277b62009-11-24 15:52:21 -0800143{
Bernie Thompson45742032010-02-15 06:46:04 -0800144 wrptr = dlfb_set_register(wrptr, 0x26, base >> 16);
145 wrptr = dlfb_set_register(wrptr, 0x27, base >> 8);
146 return dlfb_set_register(wrptr, 0x28, base);
Bernie Thompson59277b62009-11-24 15:52:21 -0800147}
148
Bernie Thompson45742032010-02-15 06:46:04 -0800149static char *dlfb_set_register_16(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 >> 8);
152 return dlfb_set_register(wrptr, reg+1, value);
Bernie Thompson59277b62009-11-24 15:52:21 -0800153}
154
155/*
156 * This is kind of weird because the controller takes some
157 * register values in a different byte order than other registers.
158 */
Bernie Thompson45742032010-02-15 06:46:04 -0800159static char *dlfb_set_register_16be(char *wrptr, u8 reg, u16 value)
Bernie Thompson59277b62009-11-24 15:52:21 -0800160{
Bernie Thompson45742032010-02-15 06:46:04 -0800161 wrptr = dlfb_set_register(wrptr, reg, value);
162 return dlfb_set_register(wrptr, reg+1, value >> 8);
Bernie Thompson59277b62009-11-24 15:52:21 -0800163}
164
165/*
166 * LFSR is linear feedback shift register. The reason we have this is
167 * because the display controller needs to minimize the clock depth of
168 * various counters used in the display path. So this code reverses the
169 * provided value into the lfsr16 value by counting backwards to get
170 * the value that needs to be set in the hardware comparator to get the
171 * same actual count. This makes sense once you read above a couple of
172 * times and think about it from a hardware perspective.
173 */
Bernie Thompsonbd808162010-02-15 06:46:48 -0800174static u16 dlfb_lfsr16(u16 actual_count)
Bernie Thompson59277b62009-11-24 15:52:21 -0800175{
176 u32 lv = 0xFFFF; /* This is the lfsr value that the hw starts with */
177
178 while (actual_count--) {
179 lv = ((lv << 1) |
180 (((lv >> 15) ^ (lv >> 4) ^ (lv >> 2) ^ (lv >> 1)) & 1))
181 & 0xFFFF;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700182 }
Bernie Thompson59277b62009-11-24 15:52:21 -0800183
184 return (u16) lv;
185}
186
187/*
188 * This does LFSR conversion on the value that is to be written.
189 * See LFSR explanation above for more detail.
190 */
Bernie Thompson45742032010-02-15 06:46:04 -0800191static char *dlfb_set_register_lfsr16(char *wrptr, u8 reg, u16 value)
Bernie Thompson59277b62009-11-24 15:52:21 -0800192{
Bernie Thompsonbd808162010-02-15 06:46:48 -0800193 return dlfb_set_register_16(wrptr, reg, dlfb_lfsr16(value));
Bernie Thompson59277b62009-11-24 15:52:21 -0800194}
195
196/*
197 * This takes a standard fbdev screeninfo struct and all of its monitor mode
198 * details and converts them into the DisplayLink equivalent register commands.
199 */
Bernie Thompson45742032010-02-15 06:46:04 -0800200static char *dlfb_set_vid_cmds(char *wrptr, struct fb_var_screeninfo *var)
Bernie Thompson59277b62009-11-24 15:52:21 -0800201{
202 u16 xds, yds;
203 u16 xde, yde;
204 u16 yec;
205
Bernie Thompson59277b62009-11-24 15:52:21 -0800206 /* x display start */
207 xds = var->left_margin + var->hsync_len;
Bernie Thompson45742032010-02-15 06:46:04 -0800208 wrptr = dlfb_set_register_lfsr16(wrptr, 0x01, xds);
Bernie Thompson59277b62009-11-24 15:52:21 -0800209 /* x display end */
210 xde = xds + var->xres;
Bernie Thompson45742032010-02-15 06:46:04 -0800211 wrptr = dlfb_set_register_lfsr16(wrptr, 0x03, xde);
Bernie Thompson59277b62009-11-24 15:52:21 -0800212
213 /* y display start */
214 yds = var->upper_margin + var->vsync_len;
Bernie Thompson45742032010-02-15 06:46:04 -0800215 wrptr = dlfb_set_register_lfsr16(wrptr, 0x05, yds);
Bernie Thompson59277b62009-11-24 15:52:21 -0800216 /* y display end */
217 yde = yds + var->yres;
Bernie Thompson45742032010-02-15 06:46:04 -0800218 wrptr = dlfb_set_register_lfsr16(wrptr, 0x07, yde);
Bernie Thompson59277b62009-11-24 15:52:21 -0800219
220 /* x end count is active + blanking - 1 */
Bernie Thompson45742032010-02-15 06:46:04 -0800221 wrptr = dlfb_set_register_lfsr16(wrptr, 0x09,
222 xde + var->right_margin - 1);
Bernie Thompson59277b62009-11-24 15:52:21 -0800223
224 /* libdlo hardcodes hsync start to 1 */
Bernie Thompson45742032010-02-15 06:46:04 -0800225 wrptr = dlfb_set_register_lfsr16(wrptr, 0x0B, 1);
Bernie Thompson59277b62009-11-24 15:52:21 -0800226
227 /* hsync end is width of sync pulse + 1 */
Bernie Thompson45742032010-02-15 06:46:04 -0800228 wrptr = dlfb_set_register_lfsr16(wrptr, 0x0D, var->hsync_len + 1);
Bernie Thompson59277b62009-11-24 15:52:21 -0800229
230 /* hpixels is active pixels */
Bernie Thompson45742032010-02-15 06:46:04 -0800231 wrptr = dlfb_set_register_16(wrptr, 0x0F, var->xres);
Bernie Thompson59277b62009-11-24 15:52:21 -0800232
233 /* yendcount is vertical active + vertical blanking */
234 yec = var->yres + var->upper_margin + var->lower_margin +
235 var->vsync_len;
Bernie Thompson45742032010-02-15 06:46:04 -0800236 wrptr = dlfb_set_register_lfsr16(wrptr, 0x11, yec);
Bernie Thompson59277b62009-11-24 15:52:21 -0800237
238 /* libdlo hardcodes vsync start to 0 */
Bernie Thompson45742032010-02-15 06:46:04 -0800239 wrptr = dlfb_set_register_lfsr16(wrptr, 0x13, 0);
Bernie Thompson59277b62009-11-24 15:52:21 -0800240
241 /* vsync end is width of vsync pulse */
Bernie Thompson45742032010-02-15 06:46:04 -0800242 wrptr = dlfb_set_register_lfsr16(wrptr, 0x15, var->vsync_len);
Bernie Thompson59277b62009-11-24 15:52:21 -0800243
244 /* vpixels is active pixels */
Bernie Thompson45742032010-02-15 06:46:04 -0800245 wrptr = dlfb_set_register_16(wrptr, 0x17, var->yres);
Bernie Thompson59277b62009-11-24 15:52:21 -0800246
247 /* convert picoseconds to 5kHz multiple for pclk5k = x * 1E12/5k */
Bernie Thompson45742032010-02-15 06:46:04 -0800248 wrptr = dlfb_set_register_16be(wrptr, 0x1B,
249 200*1000*1000/var->pixclock);
Bernie Thompson59277b62009-11-24 15:52:21 -0800250
251 return wrptr;
252}
253
254/*
255 * This takes a standard fbdev screeninfo struct that was fetched or prepared
256 * and then generates the appropriate command sequence that then drives the
257 * display controller.
258 */
259static int dlfb_set_video_mode(struct dlfb_data *dev,
260 struct fb_var_screeninfo *var)
261{
262 char *buf;
263 char *wrptr;
264 int retval = 0;
265 int writesize;
Bernie Thompson530f43a2010-02-15 06:46:21 -0800266 struct urb *urb;
Bernie Thompson59277b62009-11-24 15:52:21 -0800267
Bernie Thompson530f43a2010-02-15 06:46:21 -0800268 if (!atomic_read(&dev->usb_active))
269 return -EPERM;
270
271 urb = dlfb_get_urb(dev);
272 if (!urb)
273 return -ENOMEM;
274 buf = (char *) urb->transfer_buffer;
Bernie Thompson59277b62009-11-24 15:52:21 -0800275
276 /*
277 * This first section has to do with setting the base address on the
278 * controller * associated with the display. There are 2 base
279 * pointers, currently, we only * use the 16 bpp segment.
280 */
Bernie Thompson45742032010-02-15 06:46:04 -0800281 wrptr = dlfb_vidreg_lock(buf);
282 wrptr = dlfb_set_color_depth(wrptr, 0x00);
Bernie Thompson59277b62009-11-24 15:52:21 -0800283 /* set base for 16bpp segment to 0 */
Bernie Thompson45742032010-02-15 06:46:04 -0800284 wrptr = dlfb_set_base16bpp(wrptr, 0);
Bernie Thompson59277b62009-11-24 15:52:21 -0800285 /* set base for 8bpp segment to end of fb */
Bernie Thompson45742032010-02-15 06:46:04 -0800286 wrptr = dlfb_set_base8bpp(wrptr, dev->info->fix.smem_len);
Bernie Thompson59277b62009-11-24 15:52:21 -0800287
Bernie Thompson45742032010-02-15 06:46:04 -0800288 wrptr = dlfb_set_vid_cmds(wrptr, var);
Bernie Thompson530f43a2010-02-15 06:46:21 -0800289 wrptr = dlfb_enable_hvsync(wrptr, true);
Bernie Thompson45742032010-02-15 06:46:04 -0800290 wrptr = dlfb_vidreg_unlock(wrptr);
Bernie Thompson59277b62009-11-24 15:52:21 -0800291
292 writesize = wrptr - buf;
293
Bernie Thompson530f43a2010-02-15 06:46:21 -0800294 retval = dlfb_submit_urb(dev, urb, writesize);
Bernie Thompson59277b62009-11-24 15:52:21 -0800295
Bernie Thompson59277b62009-11-24 15:52:21 -0800296 return retval;
297}
298
Bernie Thompson45742032010-02-15 06:46:04 -0800299static int dlfb_ops_mmap(struct fb_info *info, struct vm_area_struct *vma)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700300{
301 unsigned long start = vma->vm_start;
302 unsigned long size = vma->vm_end - vma->vm_start;
303 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
304 unsigned long page, pos;
305
Bernie Thompsonbd808162010-02-15 06:46:48 -0800306 dl_notice("MMAP: %lu %u\n", offset + size, info->fix.smem_len);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700307
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700308 if (offset + size > info->fix.smem_len)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700309 return -EINVAL;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700310
311 pos = (unsigned long)info->fix.smem_start + offset;
312
313 while (size > 0) {
314 page = vmalloc_to_pfn((void *)pos);
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700315 if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED))
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700316 return -EAGAIN;
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700317
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700318 start += PAGE_SIZE;
319 pos += PAGE_SIZE;
320 if (size > PAGE_SIZE)
321 size -= PAGE_SIZE;
322 else
323 size = 0;
324 }
325
326 vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
327 return 0;
328
329}
330
Bernie Thompson530f43a2010-02-15 06:46:21 -0800331/*
332 * Trims identical data from front and back of line
333 * Sets new front buffer address and width
334 * And returns byte count of identical pixels
335 * Assumes CPU natural alignment (unsigned long)
336 * for back and front buffer ptrs and width
337 */
338static int dlfb_trim_hline(const u8 *bback, const u8 **bfront, int *width_bytes)
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700339{
Bernie Thompson530f43a2010-02-15 06:46:21 -0800340 int j, k;
341 const unsigned long *back = (const unsigned long *) bback;
342 const unsigned long *front = (const unsigned long *) *bfront;
343 const int width = *width_bytes / sizeof(unsigned long);
344 int identical = width;
345 int start = width;
346 int end = width;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700347
Bernie Thompson530f43a2010-02-15 06:46:21 -0800348 prefetch((void *) front);
349 prefetch((void *) back);
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700350
Bernie Thompson530f43a2010-02-15 06:46:21 -0800351 for (j = 0; j < width; j++) {
352 if (back[j] != front[j]) {
353 start = j;
354 break;
355 }
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700356 }
357
Bernie Thompson530f43a2010-02-15 06:46:21 -0800358 for (k = width - 1; k > j; k--) {
359 if (back[k] != front[k]) {
360 end = k+1;
361 break;
362 }
363 }
364
365 identical = start + (width - end);
366 *bfront = (u8 *) &front[start];
367 *width_bytes = (end - start) * sizeof(unsigned long);
368
369 return identical * sizeof(unsigned long);
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700370}
371
372/*
Pavel Machek3b7b31f2010-04-03 07:00:37 +0200373 * Render a command stream for an encoded horizontal line segment of pixels.
374 *
375 * A command buffer holds several commands.
376 * It always begins with a fresh command header
377 * (the protocol doesn't require this, but we enforce it to allow
378 * multiple buffers to be potentially encoded and sent in parallel).
379 * A single command encodes one contiguous horizontal line of pixels
380 *
381 * The function relies on the client to do all allocation, so that
382 * rendering can be done directly to output buffers (e.g. USB URBs).
383 * The function fills the supplied command buffer, providing information
384 * on where it left off, so the client may call in again with additional
385 * buffers if the line will take several buffers to complete.
386 *
387 * A single command can transmit a maximum of 256 pixels,
388 * regardless of the compression ratio (protocol design limit).
389 * To the hardware, 0 for a size byte means 256
390 *
391 * Rather than 256 pixel commands which are either rl or raw encoded,
392 * the rlx command simply assumes alternating raw and rl spans within one cmd.
393 * This has a slightly larger header overhead, but produces more even results.
394 * It also processes all data (read and write) in a single pass.
395 * Performance benchmarks of common cases show it having just slightly better
396 * compression than 256 pixel raw -or- rle commands, with similar CPU consumpion.
397 * But for very rl friendly data, will compress not quite as well.
398 */
Bernie Thompson530f43a2010-02-15 06:46:21 -0800399static void dlfb_compress_hline(
400 const uint16_t **pixel_start_ptr,
401 const uint16_t *const pixel_end,
402 uint32_t *device_address_ptr,
403 uint8_t **command_buffer_ptr,
404 const uint8_t *const cmd_buffer_end)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700405{
Bernie Thompson530f43a2010-02-15 06:46:21 -0800406 const uint16_t *pixel = *pixel_start_ptr;
407 uint32_t dev_addr = *device_address_ptr;
408 uint8_t *cmd = *command_buffer_ptr;
409 const int bpp = 2;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700410
Bernie Thompson530f43a2010-02-15 06:46:21 -0800411 while ((pixel_end > pixel) &&
412 (cmd_buffer_end - MIN_RLX_CMD_BYTES > cmd)) {
413 uint8_t *raw_pixels_count_byte = 0;
414 uint8_t *cmd_pixels_count_byte = 0;
415 const uint16_t *raw_pixel_start = 0;
416 const uint16_t *cmd_pixel_start, *cmd_pixel_end = 0;
417 const uint32_t be_dev_addr = cpu_to_be32(dev_addr);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700418
Bernie Thompson530f43a2010-02-15 06:46:21 -0800419 prefetchw((void *) cmd); /* pull in one cache line at least */
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700420
Bernie Thompson530f43a2010-02-15 06:46:21 -0800421 *cmd++ = 0xAF;
422 *cmd++ = 0x6B;
423 *cmd++ = (uint8_t) ((be_dev_addr >> 8) & 0xFF);
424 *cmd++ = (uint8_t) ((be_dev_addr >> 16) & 0xFF);
425 *cmd++ = (uint8_t) ((be_dev_addr >> 24) & 0xFF);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700426
Bernie Thompson530f43a2010-02-15 06:46:21 -0800427 cmd_pixels_count_byte = cmd++; /* we'll know this later */
428 cmd_pixel_start = pixel;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700429
Bernie Thompson530f43a2010-02-15 06:46:21 -0800430 raw_pixels_count_byte = cmd++; /* we'll know this later */
431 raw_pixel_start = pixel;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700432
Bernie Thompson530f43a2010-02-15 06:46:21 -0800433 cmd_pixel_end = pixel + min(MAX_CMD_PIXELS + 1,
434 min((int)(pixel_end - pixel),
435 (int)(cmd_buffer_end - cmd) / bpp));
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700436
Bernie Thompson530f43a2010-02-15 06:46:21 -0800437 prefetch_range((void *) pixel, (cmd_pixel_end - pixel) * bpp);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700438
Bernie Thompson530f43a2010-02-15 06:46:21 -0800439 while (pixel < cmd_pixel_end) {
440 const uint16_t * const repeating_pixel = pixel;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700441
Bernie Thompson530f43a2010-02-15 06:46:21 -0800442 *(uint16_t *)cmd = cpu_to_be16p(pixel);
443 cmd += 2;
444 pixel++;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700445
Bernie Thompson530f43a2010-02-15 06:46:21 -0800446 if (unlikely((pixel < cmd_pixel_end) &&
447 (*pixel == *repeating_pixel))) {
448 /* go back and fill in raw pixel count */
449 *raw_pixels_count_byte = ((repeating_pixel -
450 raw_pixel_start) + 1) & 0xFF;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700451
Bernie Thompson530f43a2010-02-15 06:46:21 -0800452 while ((pixel < cmd_pixel_end)
453 && (*pixel == *repeating_pixel)) {
454 pixel++;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700455 }
Bernie Thompson59277b62009-11-24 15:52:21 -0800456
Bernie Thompson530f43a2010-02-15 06:46:21 -0800457 /* immediately after raw data is repeat byte */
458 *cmd++ = ((pixel - repeating_pixel) - 1) & 0xFF;
Bernie Thompson59277b62009-11-24 15:52:21 -0800459
Bernie Thompson530f43a2010-02-15 06:46:21 -0800460 /* Then start another raw pixel span */
461 raw_pixel_start = pixel;
462 raw_pixels_count_byte = cmd++;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700463 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700464 }
465
Bernie Thompson530f43a2010-02-15 06:46:21 -0800466 if (pixel > raw_pixel_start) {
467 /* finalize last RAW span */
468 *raw_pixels_count_byte = (pixel-raw_pixel_start) & 0xFF;
469 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700470
Bernie Thompson530f43a2010-02-15 06:46:21 -0800471 *cmd_pixels_count_byte = (pixel - cmd_pixel_start) & 0xFF;
472 dev_addr += (pixel - cmd_pixel_start) * bpp;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700473 }
474
Bernie Thompson530f43a2010-02-15 06:46:21 -0800475 if (cmd_buffer_end <= MIN_RLX_CMD_BYTES + cmd) {
476 /* Fill leftover bytes with no-ops */
477 if (cmd_buffer_end > cmd)
478 memset(cmd, 0xAF, cmd_buffer_end - cmd);
479 cmd = (uint8_t *) cmd_buffer_end;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700480 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700481
Bernie Thompson530f43a2010-02-15 06:46:21 -0800482 *command_buffer_ptr = cmd;
483 *pixel_start_ptr = pixel;
484 *device_address_ptr = dev_addr;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700485
Bernie Thompson530f43a2010-02-15 06:46:21 -0800486 return;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700487}
488
Bernie Thompson530f43a2010-02-15 06:46:21 -0800489/*
490 * There are 3 copies of every pixel: The front buffer that the fbdev
491 * client renders to, the actual framebuffer across the USB bus in hardware
492 * (that we can only write to, slowly, and can never read), and (optionally)
493 * our shadow copy that tracks what's been sent to that hardware buffer.
494 */
495static void dlfb_render_hline(struct dlfb_data *dev, struct urb **urb_ptr,
496 const char *front, char **urb_buf_ptr,
497 u32 byte_offset, u32 byte_width,
498 int *ident_ptr, int *sent_ptr)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700499{
Bernie Thompson530f43a2010-02-15 06:46:21 -0800500 const u8 *line_start, *line_end, *next_pixel;
501 u32 dev_addr = dev->base16 + byte_offset;
502 struct urb *urb = *urb_ptr;
503 u8 *cmd = *urb_buf_ptr;
504 u8 *cmd_end = (u8 *) urb->transfer_buffer + urb->transfer_buffer_length;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700505
Bernie Thompson530f43a2010-02-15 06:46:21 -0800506 line_start = (u8 *) (front + byte_offset);
507 next_pixel = line_start;
508 line_end = next_pixel + byte_width;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700509
Bernie Thompson530f43a2010-02-15 06:46:21 -0800510 if (dev->backing_buffer) {
511 int offset;
512 const u8 *back_start = (u8 *) (dev->backing_buffer
513 + byte_offset);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700514
Bernie Thompson530f43a2010-02-15 06:46:21 -0800515 *ident_ptr += dlfb_trim_hline(back_start, &next_pixel,
516 &byte_width);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700517
Bernie Thompson530f43a2010-02-15 06:46:21 -0800518 offset = next_pixel - line_start;
519 line_end = next_pixel + byte_width;
520 dev_addr += offset;
521 back_start += offset;
522 line_start += offset;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700523
Bernie Thompson530f43a2010-02-15 06:46:21 -0800524 memcpy((char *)back_start, (char *) line_start,
525 byte_width);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700526 }
527
Bernie Thompson530f43a2010-02-15 06:46:21 -0800528 while (next_pixel < line_end) {
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700529
Bernie Thompson530f43a2010-02-15 06:46:21 -0800530 dlfb_compress_hline((const uint16_t **) &next_pixel,
531 (const uint16_t *) line_end, &dev_addr,
532 (u8 **) &cmd, (u8 *) cmd_end);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700533
Bernie Thompson530f43a2010-02-15 06:46:21 -0800534 if (cmd >= cmd_end) {
535 int len = cmd - (u8 *) urb->transfer_buffer;
536 if (dlfb_submit_urb(dev, urb, len))
537 return; /* lost pixels is set */
538 *sent_ptr += len;
539 urb = dlfb_get_urb(dev);
540 if (!urb)
541 return; /* lost_pixels is set */
542 *urb_ptr = urb;
543 cmd = urb->transfer_buffer;
544 cmd_end = &cmd[urb->transfer_buffer_length];
545 }
546 }
547
548 *urb_buf_ptr = cmd;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700549}
550
Bernie Thompson530f43a2010-02-15 06:46:21 -0800551int dlfb_handle_damage(struct dlfb_data *dev, int x, int y,
552 int width, int height, char *data)
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700553{
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700554 int i, ret;
Bernie Thompson530f43a2010-02-15 06:46:21 -0800555 char *cmd;
556 cycles_t start_cycles, end_cycles;
557 int bytes_sent = 0;
558 int bytes_identical = 0;
559 struct urb *urb;
560 int aligned_x;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700561
Bernie Thompson530f43a2010-02-15 06:46:21 -0800562 start_cycles = get_cycles();
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700563
Bernie Thompson530f43a2010-02-15 06:46:21 -0800564 aligned_x = DL_ALIGN_DOWN(x, sizeof(unsigned long));
565 width = DL_ALIGN_UP(width + (x-aligned_x), sizeof(unsigned long));
566 x = aligned_x;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700567
Bernie Thompson530f43a2010-02-15 06:46:21 -0800568 if ((width <= 0) ||
569 (x + width > dev->info->var.xres) ||
570 (y + height > dev->info->var.yres))
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700571 return -EINVAL;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700572
Bernie Thompson530f43a2010-02-15 06:46:21 -0800573 if (!atomic_read(&dev->usb_active))
574 return 0;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700575
Bernie Thompson530f43a2010-02-15 06:46:21 -0800576 urb = dlfb_get_urb(dev);
577 if (!urb)
578 return 0;
579 cmd = urb->transfer_buffer;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700580
Bernie Thompson530f43a2010-02-15 06:46:21 -0800581 for (i = y; i < y + height ; i++) {
582 const int line_offset = dev->info->fix.line_length * i;
583 const int byte_offset = line_offset + (x * BPP);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700584
Bernie Thompson530f43a2010-02-15 06:46:21 -0800585 dlfb_render_hline(dev, &urb, (char *) dev->info->fix.smem_start,
586 &cmd, byte_offset, width * BPP,
587 &bytes_identical, &bytes_sent);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700588 }
589
Bernie Thompson530f43a2010-02-15 06:46:21 -0800590 if (cmd > (char *) urb->transfer_buffer) {
591 /* Send partial buffer remaining before exiting */
592 int len = cmd - (char *) urb->transfer_buffer;
593 ret = dlfb_submit_urb(dev, urb, len);
594 bytes_sent += len;
595 } else
596 dlfb_urb_completion(urb);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700597
Bernie Thompson530f43a2010-02-15 06:46:21 -0800598 atomic_add(bytes_sent, &dev->bytes_sent);
599 atomic_add(bytes_identical, &dev->bytes_identical);
600 atomic_add(width*height*2, &dev->bytes_rendered);
601 end_cycles = get_cycles();
602 atomic_add(((unsigned int) ((end_cycles - start_cycles)
603 >> 10)), /* Kcycles */
604 &dev->cpu_kcycles_used);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700605
Bernie Thompson530f43a2010-02-15 06:46:21 -0800606 return 0;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700607}
608
Bernie Thompsond46ecb92010-09-05 16:35:04 -0700609static ssize_t dlfb_ops_read(struct fb_info *info, char __user *buf,
610 size_t count, loff_t *ppos)
611{
612 ssize_t result = -ENOSYS;
613
614#if defined CONFIG_FB_SYS_FOPS || defined CONFIG_FB_SYS_FOPS_MODULE
615 result = fb_sys_read(info, buf, count, ppos);
616#endif
617
618 return result;
619}
620
621/*
622 * Path triggered by usermode clients who write to filesystem
623 * e.g. cat filename > /dev/fb1
624 * Not used by X Windows or text-mode console. But useful for testing.
625 * Slow because of extra copy and we must assume all pixels dirty.
626 */
627static ssize_t dlfb_ops_write(struct fb_info *info, const char __user *buf,
628 size_t count, loff_t *ppos)
629{
630 ssize_t result = -ENOSYS;
631 struct dlfb_data *dev = info->par;
632 u32 offset = (u32) *ppos;
633
634#if defined CONFIG_FB_SYS_FOPS || defined CONFIG_FB_SYS_FOPS_MODULE
635
636 result = fb_sys_write(info, buf, count, ppos);
637
638 if (result > 0) {
639 int start = max((int)(offset / info->fix.line_length) - 1, 0);
640 int lines = min((u32)((result / info->fix.line_length) + 1),
641 (u32)info->var.yres);
642
643 dlfb_handle_damage(dev, 0, start, info->var.xres,
644 lines, info->screen_base);
645 }
646#endif
647
648 return result;
649}
650
Bernie Thompson530f43a2010-02-15 06:46:21 -0800651/* hardware has native COPY command (see libdlo), but not worth it for fbcon */
Bernie Thompson45742032010-02-15 06:46:04 -0800652static void dlfb_ops_copyarea(struct fb_info *info,
Bernie Thompson530f43a2010-02-15 06:46:21 -0800653 const struct fb_copyarea *area)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700654{
Bernie Thompson530f43a2010-02-15 06:46:21 -0800655
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700656 struct dlfb_data *dev = info->par;
657
Bernie Thompson530f43a2010-02-15 06:46:21 -0800658#if defined CONFIG_FB_SYS_COPYAREA || defined CONFIG_FB_SYS_COPYAREA_MODULE
659
660 sys_copyarea(info, area);
661
662 dlfb_handle_damage(dev, area->dx, area->dy,
663 area->width, area->height, info->screen_base);
664#endif
Bernie Thompson530f43a2010-02-15 06:46:21 -0800665
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700666}
667
Bernie Thompson45742032010-02-15 06:46:04 -0800668static void dlfb_ops_imageblit(struct fb_info *info,
Bernie Thompson530f43a2010-02-15 06:46:21 -0800669 const struct fb_image *image)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700670{
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700671 struct dlfb_data *dev = info->par;
Bernie Thompson530f43a2010-02-15 06:46:21 -0800672
673#if defined CONFIG_FB_SYS_IMAGEBLIT || defined CONFIG_FB_SYS_IMAGEBLIT_MODULE
674
675 sys_imageblit(info, image);
676
677 dlfb_handle_damage(dev, image->dx, image->dy,
678 image->width, image->height, info->screen_base);
679
680#endif
681
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700682}
683
Bernie Thompson45742032010-02-15 06:46:04 -0800684static void dlfb_ops_fillrect(struct fb_info *info,
Bernie Thompson530f43a2010-02-15 06:46:21 -0800685 const struct fb_fillrect *rect)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700686{
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700687 struct dlfb_data *dev = info->par;
688
Bernie Thompson530f43a2010-02-15 06:46:21 -0800689#if defined CONFIG_FB_SYS_FILLRECT || defined CONFIG_FB_SYS_FILLRECT_MODULE
690
691 sys_fillrect(info, rect);
692
693 dlfb_handle_damage(dev, rect->dx, rect->dy, rect->width,
694 rect->height, info->screen_base);
695#endif
696
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700697}
698
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700699static int dlfb_get_edid(struct dlfb_data *dev, char *edid, int len)
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800700{
701 int i;
702 int ret;
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700703 char *rbuf;
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800704
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700705 rbuf = kmalloc(2, GFP_KERNEL);
706 if (!rbuf)
707 return 0;
708
709 for (i = 0; i < len; i++) {
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800710 ret = usb_control_msg(dev->udev,
711 usb_rcvctrlpipe(dev->udev, 0), (0x02),
712 (0x80 | (0x02 << 5)), i << 8, 0xA1, rbuf, 2,
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700713 HZ);
714 if (ret < 1) {
715 dl_err("Read EDID byte %d failed err %x\n", i, ret);
716 i--;
717 break;
718 }
719 edid[i] = rbuf[1];
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800720 }
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700721
722 kfree(rbuf);
723
724 return i;
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800725}
726
Bernie Thompson45742032010-02-15 06:46:04 -0800727static int dlfb_ops_ioctl(struct fb_info *info, unsigned int cmd,
Bernie Thompson530f43a2010-02-15 06:46:21 -0800728 unsigned long arg)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700729{
Bernie Thompson530f43a2010-02-15 06:46:21 -0800730
731 struct dlfb_data *dev = info->par;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700732 struct dloarea *area = NULL;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700733
Bernie Thompson530f43a2010-02-15 06:46:21 -0800734 if (!atomic_read(&dev->usb_active))
735 return 0;
736
737 /* TODO: Update X server to get this from sysfs instead */
738 if (cmd == DLFB_IOCTL_RETURN_EDID) {
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700739 char *edid = (char *)arg;
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700740 if (copy_to_user(edid, dev->edid, dev->edid_size))
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700741 return -EFAULT;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700742 return 0;
743 }
744
Bernie Thompson530f43a2010-02-15 06:46:21 -0800745 /* TODO: Help propose a standard fb.h ioctl to report mmap damage */
746 if (cmd == DLFB_IOCTL_REPORT_DAMAGE) {
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700747
748 area = (struct dloarea *)arg;
749
750 if (area->x < 0)
751 area->x = 0;
752
753 if (area->x > info->var.xres)
754 area->x = info->var.xres;
755
756 if (area->y < 0)
757 area->y = 0;
758
759 if (area->y > info->var.yres)
760 area->y = info->var.yres;
761
Bernie Thompson530f43a2010-02-15 06:46:21 -0800762 atomic_set(&dev->use_defio, 0);
763
764 dlfb_handle_damage(dev, area->x, area->y, area->w, area->h,
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700765 info->screen_base);
766 }
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700767
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700768 return 0;
769}
770
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700771/* taken from vesafb */
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700772static int
Bernie Thompson45742032010-02-15 06:46:04 -0800773dlfb_ops_setcolreg(unsigned regno, unsigned red, unsigned green,
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700774 unsigned blue, unsigned transp, struct fb_info *info)
775{
776 int err = 0;
777
778 if (regno >= info->cmap.len)
779 return 1;
780
781 if (regno < 16) {
782 if (info->var.red.offset == 10) {
783 /* 1:5:5:5 */
784 ((u32 *) (info->pseudo_palette))[regno] =
785 ((red & 0xf800) >> 1) |
786 ((green & 0xf800) >> 6) | ((blue & 0xf800) >> 11);
787 } else {
788 /* 0:5:6:5 */
789 ((u32 *) (info->pseudo_palette))[regno] =
790 ((red & 0xf800)) |
791 ((green & 0xfc00) >> 5) | ((blue & 0xf800) >> 11);
792 }
793 }
794
795 return err;
796}
797
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800798/*
799 * It's common for several clients to have framebuffer open simultaneously.
800 * e.g. both fbcon and X. Makes things interesting.
Bernie Thompson33077b82010-09-05 16:35:19 -0700801 * Assumes caller is holding info->lock (for open and release at least)
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800802 */
803static int dlfb_ops_open(struct fb_info *info, int user)
804{
805 struct dlfb_data *dev = info->par;
806
807/* if (user == 0)
808 * We could special case kernel mode clients (fbcon) here
809 */
810
Bernie Thompson33077b82010-09-05 16:35:19 -0700811 /* If the USB device is gone, we don't accept new opens */
812 if (dev->virtualized)
813 return -ENODEV;
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800814
815 dev->fb_count++;
816
Bernie Thompson33077b82010-09-05 16:35:19 -0700817 kref_get(&dev->kref);
818
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800819#ifdef CONFIG_FB_DEFERRED_IO
820 if ((atomic_read(&dev->use_defio)) && (info->fbdefio == NULL)) {
821 /* enable defio */
822 info->fbdefio = &dlfb_defio;
823 fb_deferred_io_init(info);
824 }
825#endif
826
827 dl_notice("open /dev/fb%d user=%d fb_info=%p count=%d\n",
828 info->node, user, info, dev->fb_count);
829
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700830 return 0;
831}
832
Bernie Thompson4a4854d2010-02-15 06:45:55 -0800833/*
834 * Called when all client interfaces to start transactions have been disabled,
835 * and all references to our device instance (dlfb_data) are released.
836 * Every transaction must have a reference, so we know are fully spun down
837 */
Bernie Thompson33077b82010-09-05 16:35:19 -0700838static void dlfb_free(struct kref *kref)
Bernie Thompson4a4854d2010-02-15 06:45:55 -0800839{
840 struct dlfb_data *dev = container_of(kref, struct dlfb_data, kref);
841
Bernie Thompson33077b82010-09-05 16:35:19 -0700842 /* this function will wait for all in-flight urbs to complete */
843 if (dev->urbs.count > 0)
844 dlfb_free_urb_list(dev);
845
Bernie Thompson4a4854d2010-02-15 06:45:55 -0800846 if (dev->backing_buffer)
847 vfree(dev->backing_buffer);
848
Bernie Thompson33077b82010-09-05 16:35:19 -0700849 kfree(dev->edid);
850
851 dl_warn("freeing dlfb_data %p\n", dev);
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800852
Bernie Thompson4a4854d2010-02-15 06:45:55 -0800853 kfree(dev);
854}
855
Bernie Thompson33077b82010-09-05 16:35:19 -0700856
857static void dlfb_free_framebuffer_work(struct work_struct *work)
Bernie Thompson2469d5d2010-02-15 06:46:13 -0800858{
Bernie Thompson33077b82010-09-05 16:35:19 -0700859 struct dlfb_data *dev = container_of(work, struct dlfb_data,
860 free_framebuffer_work.work);
861 struct fb_info *info = dev->info;
862 int node = info->node;
863
864 unregister_framebuffer(info);
Bernie Thompson2469d5d2010-02-15 06:46:13 -0800865
866 if (info->cmap.len != 0)
867 fb_dealloc_cmap(&info->cmap);
868 if (info->monspecs.modedb)
869 fb_destroy_modedb(info->monspecs.modedb);
870 if (info->screen_base)
871 vfree(info->screen_base);
872
873 fb_destroy_modelist(&info->modelist);
874
Bernie Thompson33077b82010-09-05 16:35:19 -0700875 dev->info = 0;
876
877 /* Assume info structure is freed after this point */
Bernie Thompson2469d5d2010-02-15 06:46:13 -0800878 framebuffer_release(info);
879
Bernie Thompson33077b82010-09-05 16:35:19 -0700880 dl_warn("fb_info for /dev/fb%d has been freed\n", node);
881
882 /* ref taken in probe() as part of registering framebfufer */
883 kref_put(&dev->kref, dlfb_free);
884}
885
886/*
887 * Assumes caller is holding info->lock mutex (for open and release at least)
888 */
889static int dlfb_ops_release(struct fb_info *info, int user)
890{
891 struct dlfb_data *dev = info->par;
892
893 dev->fb_count--;
894
895 /* We can't free fb_info here - fbmem will touch it when we return */
896 if (dev->virtualized && (dev->fb_count == 0))
897 schedule_delayed_work(&dev->free_framebuffer_work, HZ);
898
899#ifdef CONFIG_FB_DEFERRED_IO
900 if ((dev->fb_count == 0) && (info->fbdefio)) {
901 fb_deferred_io_cleanup(info);
902 kfree(info->fbdefio);
903 info->fbdefio = NULL;
904 info->fbops->fb_mmap = dlfb_ops_mmap;
905 }
906#endif
907
908 dl_warn("released /dev/fb%d user=%d count=%d\n",
909 info->node, user, dev->fb_count);
910
911 kref_put(&dev->kref, dlfb_free);
912
913 return 0;
Bernie Thompson2469d5d2010-02-15 06:46:13 -0800914}
915
916/*
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800917 * Check whether a video mode is supported by the DisplayLink chip
918 * We start from monitor's modes, so don't need to filter that here
919 */
920static int dlfb_is_valid_mode(struct fb_videomode *mode,
921 struct fb_info *info)
922{
923 struct dlfb_data *dev = info->par;
924
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700925 if (mode->xres * mode->yres > dev->sku_pixel_limit) {
926 dl_warn("%dx%d beyond chip capabilities\n",
927 mode->xres, mode->yres);
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800928 return 0;
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700929 }
930
931 dl_info("%dx%d valid mode\n", mode->xres, mode->yres);
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800932
933 return 1;
934}
935
936static void dlfb_var_color_format(struct fb_var_screeninfo *var)
937{
938 const struct fb_bitfield red = { 11, 5, 0 };
939 const struct fb_bitfield green = { 5, 6, 0 };
940 const struct fb_bitfield blue = { 0, 5, 0 };
941
942 var->bits_per_pixel = 16;
943 var->red = red;
944 var->green = green;
945 var->blue = blue;
946}
947
Bernie Thompson2469d5d2010-02-15 06:46:13 -0800948static int dlfb_ops_check_var(struct fb_var_screeninfo *var,
949 struct fb_info *info)
950{
951 struct fb_videomode mode;
952
953 /* TODO: support dynamically changing framebuffer size */
954 if ((var->xres * var->yres * 2) > info->fix.smem_len)
955 return -EINVAL;
956
957 /* set device-specific elements of var unrelated to mode */
958 dlfb_var_color_format(var);
959
960 fb_var_to_videomode(&mode, var);
961
962 if (!dlfb_is_valid_mode(&mode, info))
963 return -EINVAL;
964
965 return 0;
966}
967
968static int dlfb_ops_set_par(struct fb_info *info)
969{
970 struct dlfb_data *dev = info->par;
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700971 int result;
972 u16 *pix_framebuffer;
973 int i;
Bernie Thompson2469d5d2010-02-15 06:46:13 -0800974
975 dl_notice("set_par mode %dx%d\n", info->var.xres, info->var.yres);
976
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700977 result = dlfb_set_video_mode(dev, &info->var);
978
979 if ((result == 0) && (dev->fb_count == 0)) {
980
981 /* paint greenscreen */
982
983 pix_framebuffer = (u16 *) info->screen_base;
984 for (i = 0; i < info->fix.smem_len / 2; i++)
985 pix_framebuffer[i] = 0x37e6;
986
987 dlfb_handle_damage(dev, 0, 0, info->var.xres, info->var.yres,
988 info->screen_base);
989 }
990
991 return result;
Bernie Thompson2469d5d2010-02-15 06:46:13 -0800992}
993
Bernie Thompson9825f702010-09-05 16:35:10 -0700994/*
995 * In order to come back from full DPMS off, we need to set the mode again
996 */
Bernie Thompson45742032010-02-15 06:46:04 -0800997static int dlfb_ops_blank(int blank_mode, struct fb_info *info)
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700998{
Bernie Thompson530f43a2010-02-15 06:46:21 -0800999 struct dlfb_data *dev = info->par;
Roberto De Ioris7316bc52009-06-10 23:02:19 -07001000
Roberto De Ioris7316bc52009-06-10 23:02:19 -07001001 if (blank_mode != FB_BLANK_UNBLANK) {
Bernie Thompson9825f702010-09-05 16:35:10 -07001002 char *bufptr;
1003 struct urb *urb;
Roberto De Ioris7316bc52009-06-10 23:02:19 -07001004
Bernie Thompson9825f702010-09-05 16:35:10 -07001005 urb = dlfb_get_urb(dev);
1006 if (!urb)
1007 return 0;
1008
1009 bufptr = (char *) urb->transfer_buffer;
1010 bufptr = dlfb_vidreg_lock(bufptr);
1011 bufptr = dlfb_enable_hvsync(bufptr, false);
1012 bufptr = dlfb_vidreg_unlock(bufptr);
1013
1014 dlfb_submit_urb(dev, urb, bufptr -
1015 (char *) urb->transfer_buffer);
1016 } else {
1017 dlfb_set_video_mode(dev, &info->var);
1018 }
Roberto De Ioris7316bc52009-06-10 23:02:19 -07001019
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001020 return 0;
1021}
1022
1023static struct fb_ops dlfb_ops = {
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001024 .owner = THIS_MODULE,
Bernie Thompsond46ecb92010-09-05 16:35:04 -07001025 .fb_read = dlfb_ops_read,
1026 .fb_write = dlfb_ops_write,
Bernie Thompson45742032010-02-15 06:46:04 -08001027 .fb_setcolreg = dlfb_ops_setcolreg,
1028 .fb_fillrect = dlfb_ops_fillrect,
1029 .fb_copyarea = dlfb_ops_copyarea,
1030 .fb_imageblit = dlfb_ops_imageblit,
1031 .fb_mmap = dlfb_ops_mmap,
1032 .fb_ioctl = dlfb_ops_ioctl,
Bernie Thompson3e8f3d62010-02-15 06:46:26 -08001033 .fb_open = dlfb_ops_open,
Bernie Thompson45742032010-02-15 06:46:04 -08001034 .fb_release = dlfb_ops_release,
1035 .fb_blank = dlfb_ops_blank,
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001036 .fb_check_var = dlfb_ops_check_var,
1037 .fb_set_par = dlfb_ops_set_par,
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001038};
1039
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001040
Bernie Thompsoncc403dc2010-02-15 06:45:49 -08001041/*
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001042 * Assumes &info->lock held by caller
1043 * Assumes no active clients have framebuffer open
1044 */
1045static int dlfb_realloc_framebuffer(struct dlfb_data *dev, struct fb_info *info)
1046{
1047 int retval = -ENOMEM;
1048 int old_len = info->fix.smem_len;
1049 int new_len;
1050 unsigned char *old_fb = info->screen_base;
1051 unsigned char *new_fb;
1052 unsigned char *new_back;
1053
1054 dl_warn("Reallocating framebuffer. Addresses will change!\n");
1055
1056 new_len = info->fix.line_length * info->var.yres;
1057
1058 if (PAGE_ALIGN(new_len) > old_len) {
1059 /*
1060 * Alloc system memory for virtual framebuffer
1061 */
1062 new_fb = vmalloc(new_len);
1063 if (!new_fb) {
1064 dl_err("Virtual framebuffer alloc failed\n");
1065 goto error;
1066 }
1067
1068 if (info->screen_base) {
1069 memcpy(new_fb, old_fb, old_len);
1070 vfree(info->screen_base);
1071 }
1072
1073 info->screen_base = new_fb;
1074 info->fix.smem_len = PAGE_ALIGN(new_len);
1075 info->fix.smem_start = (unsigned long) new_fb;
1076 info->flags = udlfb_info_flags;
1077
1078 /*
1079 * Second framebuffer copy to mirror the framebuffer state
1080 * on the physical USB device. We can function without this.
1081 * But with imperfect damage info we may send pixels over USB
1082 * that were, in fact, unchanged - wasting limited USB bandwidth
1083 */
1084 new_back = vmalloc(new_len);
1085 if (!new_back)
1086 dl_info("No shadow/backing buffer allcoated\n");
1087 else {
1088 if (dev->backing_buffer)
1089 vfree(dev->backing_buffer);
1090 dev->backing_buffer = new_back;
1091 memset(dev->backing_buffer, 0, new_len);
1092 }
1093 }
1094
1095 retval = 0;
1096
1097error:
1098 return retval;
1099}
1100
1101/*
1102 * 1) Get EDID from hw, or use sw default
1103 * 2) Parse into various fb_info structs
1104 * 3) Allocate virtual framebuffer memory to back highest res mode
1105 *
1106 * Parses EDID into three places used by various parts of fbdev:
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001107 * fb_var_screeninfo contains the timing of the monitor's preferred mode
1108 * fb_info.monspecs is full parsed EDID info, including monspecs.modedb
1109 * fb_info.modelist is a linked list of all monitor & VESA modes which work
1110 *
1111 * If EDID is not readable/valid, then modelist is all VESA modes,
1112 * monspecs is NULL, and fb_var_screeninfo is set to safe VESA mode
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001113 * Returns 0 if successful
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001114 */
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001115static int dlfb_setup_modes(struct dlfb_data *dev,
1116 struct fb_info *info,
1117 char *default_edid, size_t default_edid_size)
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001118{
1119 int i;
1120 const struct fb_videomode *default_vmode = NULL;
1121 int result = 0;
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001122 char *edid;
1123 int tries = 3;
1124
1125 if (info->dev) /* only use mutex if info has been registered */
1126 mutex_lock(&info->lock);
1127
1128 edid = kmalloc(MAX_EDID_SIZE, GFP_KERNEL);
1129 if (!edid) {
1130 result = -ENOMEM;
1131 goto error;
1132 }
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001133
1134 fb_destroy_modelist(&info->modelist);
1135 memset(&info->monspecs, 0, sizeof(info->monspecs));
1136
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001137 /*
1138 * Try to (re)read EDID from hardware first
1139 * EDID data may return, but not parse as valid
1140 * Try again a few times, in case of e.g. analog cable noise
1141 */
1142 while (tries--) {
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001143
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001144 i = dlfb_get_edid(dev, edid, MAX_EDID_SIZE);
1145
1146 if (i >= MIN_EDID_SIZE)
1147 fb_edid_to_monspecs(edid, &info->monspecs);
1148
1149 if (info->monspecs.modedb_len > 0) {
1150 dev->edid = edid;
1151 dev->edid_size = i;
1152 break;
1153 }
1154 }
1155
1156 /* If that fails, use a previously returned EDID if available */
1157 if (info->monspecs.modedb_len == 0) {
1158
1159 dl_err("Unable to get valid EDID from device/display\n");
1160
1161 if (dev->edid) {
1162 fb_edid_to_monspecs(dev->edid, &info->monspecs);
1163 if (info->monspecs.modedb_len > 0)
1164 dl_err("Using previously queried EDID\n");
1165 }
1166 }
1167
1168 /* If that fails, use the default EDID we were handed */
1169 if (info->monspecs.modedb_len == 0) {
1170 if (default_edid_size >= MIN_EDID_SIZE) {
1171 fb_edid_to_monspecs(default_edid, &info->monspecs);
1172 if (info->monspecs.modedb_len > 0) {
1173 memcpy(edid, default_edid, default_edid_size);
1174 dev->edid = edid;
1175 dev->edid_size = default_edid_size;
1176 dl_err("Using default/backup EDID\n");
1177 }
1178 }
1179 }
1180
1181 /* If we've got modes, let's pick a best default mode */
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001182 if (info->monspecs.modedb_len > 0) {
1183
1184 for (i = 0; i < info->monspecs.modedb_len; i++) {
1185 if (dlfb_is_valid_mode(&info->monspecs.modedb[i], info))
1186 fb_add_videomode(&info->monspecs.modedb[i],
1187 &info->modelist);
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001188 else /* if we've removed top/best mode */
1189 info->monspecs.misc &= ~FB_MISC_1ST_DETAIL;
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001190 }
1191
1192 default_vmode = fb_find_best_display(&info->monspecs,
1193 &info->modelist);
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001194 }
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001195
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001196 /* If everything else has failed, fall back to safe default mode */
1197 if (default_vmode == NULL) {
1198
1199 struct fb_videomode fb_vmode = {0};
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001200
1201 /*
1202 * Add the standard VESA modes to our modelist
1203 * Since we don't have EDID, there may be modes that
1204 * overspec monitor and/or are incorrect aspect ratio, etc.
1205 * But at least the user has a chance to choose
1206 */
1207 for (i = 0; i < VESA_MODEDB_SIZE; i++) {
1208 if (dlfb_is_valid_mode((struct fb_videomode *)
1209 &vesa_modes[i], info))
1210 fb_add_videomode(&vesa_modes[i],
1211 &info->modelist);
1212 }
1213
1214 /*
1215 * default to resolution safe for projectors
1216 * (since they are most common case without EDID)
1217 */
1218 fb_vmode.xres = 800;
1219 fb_vmode.yres = 600;
1220 fb_vmode.refresh = 60;
1221 default_vmode = fb_find_nearest_mode(&fb_vmode,
1222 &info->modelist);
1223 }
1224
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001225 /* If we have good mode and no active clients*/
1226 if ((default_vmode != NULL) && (dev->fb_count == 0)) {
1227
1228 fb_videomode_to_var(&info->var, default_vmode);
1229 dlfb_var_color_format(&info->var);
1230
1231 /*
1232 * with mode size info, we can now alloc our framebuffer.
1233 */
1234 memcpy(&info->fix, &dlfb_fix, sizeof(dlfb_fix));
1235 info->fix.line_length = info->var.xres *
1236 (info->var.bits_per_pixel / 8);
1237
1238 result = dlfb_realloc_framebuffer(dev, info);
1239
1240 } else
1241 result = -EINVAL;
1242
1243error:
1244 if (edid && (dev->edid != edid))
1245 kfree(edid);
1246
1247 if (info->dev)
1248 mutex_unlock(&info->lock);
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001249
1250 return result;
1251}
1252
1253static ssize_t metrics_bytes_rendered_show(struct device *fbdev,
1254 struct device_attribute *a, char *buf) {
1255 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1256 struct dlfb_data *dev = fb_info->par;
1257 return snprintf(buf, PAGE_SIZE, "%u\n",
1258 atomic_read(&dev->bytes_rendered));
1259}
1260
1261static ssize_t metrics_bytes_identical_show(struct device *fbdev,
1262 struct device_attribute *a, char *buf) {
1263 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1264 struct dlfb_data *dev = fb_info->par;
1265 return snprintf(buf, PAGE_SIZE, "%u\n",
1266 atomic_read(&dev->bytes_identical));
1267}
1268
1269static ssize_t metrics_bytes_sent_show(struct device *fbdev,
1270 struct device_attribute *a, char *buf) {
1271 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1272 struct dlfb_data *dev = fb_info->par;
1273 return snprintf(buf, PAGE_SIZE, "%u\n",
1274 atomic_read(&dev->bytes_sent));
1275}
1276
1277static ssize_t metrics_cpu_kcycles_used_show(struct device *fbdev,
1278 struct device_attribute *a, char *buf) {
1279 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1280 struct dlfb_data *dev = fb_info->par;
1281 return snprintf(buf, PAGE_SIZE, "%u\n",
1282 atomic_read(&dev->cpu_kcycles_used));
1283}
1284
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001285static ssize_t edid_show(
1286 struct file *filp,
1287 struct kobject *kobj, struct bin_attribute *a,
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001288 char *buf, loff_t off, size_t count) {
1289 struct device *fbdev = container_of(kobj, struct device, kobj);
1290 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1291 struct dlfb_data *dev = fb_info->par;
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001292
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001293 if (dev->edid == NULL)
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001294 return 0;
1295
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001296 if ((off >= dev->edid_size) || (count > dev->edid_size))
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001297 return 0;
1298
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001299 if (off + count > dev->edid_size)
1300 count = dev->edid_size - off;
1301
1302 dl_info("sysfs edid copy %p to %p, %d bytes\n",
1303 dev->edid, buf, (int) count);
1304
1305 memcpy(buf, dev->edid, count);
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001306
1307 return count;
1308}
1309
1310
1311static ssize_t metrics_reset_store(struct device *fbdev,
1312 struct device_attribute *attr,
1313 const char *buf, size_t count)
1314{
1315 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1316 struct dlfb_data *dev = fb_info->par;
1317
1318 atomic_set(&dev->bytes_rendered, 0);
1319 atomic_set(&dev->bytes_identical, 0);
1320 atomic_set(&dev->bytes_sent, 0);
1321 atomic_set(&dev->cpu_kcycles_used, 0);
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001322
1323 return count;
1324}
1325
1326static ssize_t use_defio_show(struct device *fbdev,
1327 struct device_attribute *a, char *buf) {
1328 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1329 struct dlfb_data *dev = fb_info->par;
1330 return snprintf(buf, PAGE_SIZE, "%d\n",
1331 atomic_read(&dev->use_defio));
1332}
1333
1334static ssize_t use_defio_store(struct device *fbdev,
1335 struct device_attribute *attr,
1336 const char *buf, size_t count)
1337{
1338 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1339 struct dlfb_data *dev = fb_info->par;
1340
1341 if (count > 0) {
1342 if (buf[0] == '0')
1343 atomic_set(&dev->use_defio, 0);
1344 if (buf[0] == '1')
1345 atomic_set(&dev->use_defio, 1);
1346 }
1347 return count;
1348}
1349
1350static struct bin_attribute edid_attr = {
1351 .attr.name = "edid",
1352 .attr.mode = 0444,
1353 .size = 128,
1354 .read = edid_show,
1355};
1356
1357static struct device_attribute fb_device_attrs[] = {
1358 __ATTR_RO(metrics_bytes_rendered),
1359 __ATTR_RO(metrics_bytes_identical),
1360 __ATTR_RO(metrics_bytes_sent),
1361 __ATTR_RO(metrics_cpu_kcycles_used),
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001362 __ATTR(metrics_reset, S_IWUGO, NULL, metrics_reset_store),
1363 __ATTR_RW(use_defio),
1364};
1365
Bernie Thompson3e8f3d62010-02-15 06:46:26 -08001366#ifdef CONFIG_FB_DEFERRED_IO
1367static void dlfb_dpy_deferred_io(struct fb_info *info,
1368 struct list_head *pagelist)
1369{
1370 struct page *cur;
1371 struct fb_deferred_io *fbdefio = info->fbdefio;
1372 struct dlfb_data *dev = info->par;
1373 struct urb *urb;
1374 char *cmd;
1375 cycles_t start_cycles, end_cycles;
1376 int bytes_sent = 0;
1377 int bytes_identical = 0;
1378 int bytes_rendered = 0;
Bernie Thompson3e8f3d62010-02-15 06:46:26 -08001379
1380 if (!atomic_read(&dev->use_defio))
1381 return;
1382
1383 if (!atomic_read(&dev->usb_active))
1384 return;
1385
1386 start_cycles = get_cycles();
1387
1388 urb = dlfb_get_urb(dev);
1389 if (!urb)
1390 return;
1391 cmd = urb->transfer_buffer;
1392
1393 /* walk the written page list and render each to device */
1394 list_for_each_entry(cur, &fbdefio->pagelist, lru) {
1395 dlfb_render_hline(dev, &urb, (char *) info->fix.smem_start,
1396 &cmd, cur->index << PAGE_SHIFT,
1397 PAGE_SIZE, &bytes_identical, &bytes_sent);
1398 bytes_rendered += PAGE_SIZE;
Bernie Thompson3e8f3d62010-02-15 06:46:26 -08001399 }
1400
1401 if (cmd > (char *) urb->transfer_buffer) {
1402 /* Send partial buffer remaining before exiting */
1403 int len = cmd - (char *) urb->transfer_buffer;
1404 dlfb_submit_urb(dev, urb, len);
1405 bytes_sent += len;
1406 } else
1407 dlfb_urb_completion(urb);
1408
Bernie Thompson3e8f3d62010-02-15 06:46:26 -08001409 atomic_add(bytes_sent, &dev->bytes_sent);
1410 atomic_add(bytes_identical, &dev->bytes_identical);
1411 atomic_add(bytes_rendered, &dev->bytes_rendered);
1412 end_cycles = get_cycles();
1413 atomic_add(((unsigned int) ((end_cycles - start_cycles)
1414 >> 10)), /* Kcycles */
1415 &dev->cpu_kcycles_used);
1416}
1417
1418static struct fb_deferred_io dlfb_defio = {
1419 .delay = 5,
1420 .deferred_io = dlfb_dpy_deferred_io,
1421};
1422
1423#endif
1424
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001425/*
Bernie Thompsoncc403dc2010-02-15 06:45:49 -08001426 * This is necessary before we can communicate with the display controller.
1427 */
1428static int dlfb_select_std_channel(struct dlfb_data *dev)
1429{
1430 int ret;
1431 u8 set_def_chn[] = { 0x57, 0xCD, 0xDC, 0xA7,
1432 0x1C, 0x88, 0x5E, 0x15,
1433 0x60, 0xFE, 0xC6, 0x97,
1434 0x16, 0x3D, 0x47, 0xF2 };
1435
1436 ret = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
1437 NR_USB_REQUEST_CHANNEL,
1438 (USB_DIR_OUT | USB_TYPE_VENDOR), 0, 0,
1439 set_def_chn, sizeof(set_def_chn), USB_CTRL_SET_TIMEOUT);
1440 return ret;
1441}
1442
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001443static int dlfb_parse_vendor_descriptor(struct dlfb_data *dev,
1444 struct usb_device *usbdev)
1445{
1446 char *desc;
1447 char *buf;
1448 char *desc_end;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001449
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001450 u8 total_len = 0;
1451
1452 buf = kzalloc(MAX_VENDOR_DESCRIPTOR_SIZE, GFP_KERNEL);
1453 if (!buf)
1454 return false;
1455 desc = buf;
1456
1457 total_len = usb_get_descriptor(usbdev, 0x5f, /* vendor specific */
1458 0, desc, MAX_VENDOR_DESCRIPTOR_SIZE);
1459 if (total_len > 5) {
1460 dl_info("vendor descriptor length:%x data:%02x %02x %02x %02x" \
1461 "%02x %02x %02x %02x %02x %02x %02x\n",
1462 total_len, desc[0],
1463 desc[1], desc[2], desc[3], desc[4], desc[5], desc[6],
1464 desc[7], desc[8], desc[9], desc[10]);
1465
1466 if ((desc[0] != total_len) || /* descriptor length */
1467 (desc[1] != 0x5f) || /* vendor descriptor type */
1468 (desc[2] != 0x01) || /* version (2 bytes) */
1469 (desc[3] != 0x00) ||
1470 (desc[4] != total_len - 2)) /* length after type */
1471 goto unrecognized;
1472
1473 desc_end = desc + total_len;
1474 desc += 5; /* the fixed header we've already parsed */
1475
1476 while (desc < desc_end) {
1477 u8 length;
1478 u16 key;
1479
1480 key = *((u16 *) desc);
1481 desc += sizeof(u16);
1482 length = *desc;
1483 desc++;
1484
1485 switch (key) {
1486 case 0x0200: { /* max_area */
1487 u32 max_area;
1488 max_area = le32_to_cpu(*((u32 *)desc));
1489 dl_warn("DL chip limited to %d pixel modes\n",
1490 max_area);
1491 dev->sku_pixel_limit = max_area;
1492 break;
1493 }
1494 default:
1495 break;
1496 }
1497 desc += length;
1498 }
1499 }
1500
1501 goto success;
1502
1503unrecognized:
1504 /* allow udlfb to load for now even if firmware unrecognized */
1505 dl_err("Unrecognized vendor firmware descriptor\n");
1506
1507success:
1508 kfree(buf);
1509 return true;
1510}
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001511static int dlfb_usb_probe(struct usb_interface *interface,
Bernie Thompson59277b62009-11-24 15:52:21 -08001512 const struct usb_device_id *id)
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001513{
Bernie Thompson59277b62009-11-24 15:52:21 -08001514 struct usb_device *usbdev;
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001515 struct dlfb_data *dev = 0;
Bernie Thompson33077b82010-09-05 16:35:19 -07001516 struct fb_info *info = 0;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001517 int i;
Bernie Thompson59277b62009-11-24 15:52:21 -08001518 int retval = -ENOMEM;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001519
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001520 /* usb initialization */
1521
1522 usbdev = interface_to_usbdev(interface);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001523
Bernie Thompson59277b62009-11-24 15:52:21 -08001524 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1525 if (dev == NULL) {
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001526 err("dlfb_usb_probe: failed alloc of dev struct\n");
1527 goto error;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001528 }
1529
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001530 /* we need to wait for both usb and fbdev to spin down on disconnect */
1531 kref_init(&dev->kref); /* matching kref_put in usb .disconnect fn */
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001532 kref_get(&dev->kref); /* matching kref_put in free_framebuffer_work */
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001533
Bernie Thompson59277b62009-11-24 15:52:21 -08001534 dev->udev = usbdev;
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001535 dev->gdev = &usbdev->dev; /* our generic struct device * */
Bernie Thompson59277b62009-11-24 15:52:21 -08001536 usb_set_intfdata(interface, dev);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001537
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001538 dl_info("%s %s - serial #%s\n",
1539 usbdev->manufacturer, usbdev->product, usbdev->serial);
1540 dl_info("vid_%04x&pid_%04x&rev_%04x driver's dlfb_data struct at %p\n",
1541 usbdev->descriptor.idVendor, usbdev->descriptor.idProduct,
1542 usbdev->descriptor.bcdDevice, dev);
1543
1544 dev->sku_pixel_limit = 2048 * 1152; /* default to maximum */
1545
1546 if (!dlfb_parse_vendor_descriptor(dev, usbdev)) {
1547 dl_err("firmware not recognized. Assume incompatible device\n");
1548 goto error;
1549 }
1550
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001551 if (!dlfb_alloc_urb_list(dev, WRITES_IN_FLIGHT, MAX_TRANSFER)) {
1552 retval = -ENOMEM;
1553 dl_err("dlfb_alloc_urb_list failed\n");
1554 goto error;
1555 }
1556
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001557 /* We don't register a new USB class. Our client interface is fbdev */
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001558
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001559 /* allocates framebuffer driver structure, not framebuffer memory */
1560 info = framebuffer_alloc(0, &usbdev->dev);
1561 if (!info) {
1562 retval = -ENOMEM;
1563 dl_err("framebuffer_alloc failed\n");
1564 goto error;
1565 }
Bernie Thompson33077b82010-09-05 16:35:19 -07001566
Bernie Thompson59277b62009-11-24 15:52:21 -08001567 dev->info = info;
1568 info->par = dev;
1569 info->pseudo_palette = dev->pseudo_palette;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001570 info->fbops = &dlfb_ops;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001571
Bernie Thompson59277b62009-11-24 15:52:21 -08001572 retval = fb_alloc_cmap(&info->cmap, 256, 0);
1573 if (retval < 0) {
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001574 dl_err("fb_alloc_cmap failed %x\n", retval);
1575 goto error;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001576 }
1577
Bernie Thompson33077b82010-09-05 16:35:19 -07001578 INIT_DELAYED_WORK(&dev->free_framebuffer_work,
1579 dlfb_free_framebuffer_work);
1580
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001581 INIT_LIST_HEAD(&info->modelist);
1582
1583 retval = dlfb_setup_modes(dev, info, NULL, 0);
1584 if (retval != 0) {
1585 dl_err("unable to find common mode for display and adapter\n");
1586 goto error;
1587 }
1588
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001589 /* ready to begin using device */
1590
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001591#ifdef CONFIG_FB_DEFERRED_IO
1592 atomic_set(&dev->use_defio, 1);
1593#endif
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001594 atomic_set(&dev->usb_active, 1);
Bernie Thompson59277b62009-11-24 15:52:21 -08001595 dlfb_select_std_channel(dev);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001596
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001597 dlfb_ops_check_var(&info->var, info);
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001598 dlfb_ops_set_par(info);
1599
Bernie Thompson59277b62009-11-24 15:52:21 -08001600 retval = register_framebuffer(info);
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001601 if (retval < 0) {
1602 dl_err("register_framebuffer failed %d\n", retval);
1603 goto error;
1604 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001605
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001606 for (i = 0; i < ARRAY_SIZE(fb_device_attrs); i++)
1607 device_create_file(info->dev, &fb_device_attrs[i]);
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -07001608
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001609 device_create_bin_file(info->dev, &edid_attr);
1610
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001611 dl_info("DisplayLink USB device /dev/fb%d attached. %dx%d resolution."
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001612 " Using %dK framebuffer memory\n", info->node,
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001613 info->var.xres, info->var.yres,
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001614 ((dev->backing_buffer) ?
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001615 info->fix.smem_len * 2 : info->fix.smem_len) >> 10);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001616 return 0;
1617
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001618error:
1619 if (dev) {
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001620
Bernie Thompson33077b82010-09-05 16:35:19 -07001621 if (info) {
1622 if (info->cmap.len != 0)
1623 fb_dealloc_cmap(&info->cmap);
1624 if (info->monspecs.modedb)
1625 fb_destroy_modedb(info->monspecs.modedb);
1626 if (info->screen_base)
1627 vfree(info->screen_base);
1628
1629 fb_destroy_modelist(&info->modelist);
1630
1631 framebuffer_release(info);
1632 }
1633
1634 if (dev->backing_buffer)
1635 vfree(dev->backing_buffer);
1636
1637 kref_put(&dev->kref, dlfb_free); /* ref for framebuffer */
1638 kref_put(&dev->kref, dlfb_free); /* last ref from kref_init */
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001639
1640 /* dev has been deallocated. Do not dereference */
1641 }
1642
Bernie Thompson59277b62009-11-24 15:52:21 -08001643 return retval;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001644}
1645
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001646static void dlfb_usb_disconnect(struct usb_interface *interface)
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001647{
Bernie Thompson59277b62009-11-24 15:52:21 -08001648 struct dlfb_data *dev;
1649 struct fb_info *info;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001650 int i;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001651
Bernie Thompson59277b62009-11-24 15:52:21 -08001652 dev = usb_get_intfdata(interface);
Bernie Thompson59277b62009-11-24 15:52:21 -08001653 info = dev->info;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001654
Bernie Thompson33077b82010-09-05 16:35:19 -07001655 dl_info("USB disconnect starting\n");
1656
1657 /* we virtualize until all fb clients release. Then we free */
1658 dev->virtualized = true;
1659
1660 /* When non-active we'll update virtual framebuffer, but no new urbs */
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001661 atomic_set(&dev->usb_active, 0);
1662
Bernie Thompson33077b82010-09-05 16:35:19 -07001663 /* remove udlfb's sysfs interfaces */
1664 for (i = 0; i < ARRAY_SIZE(fb_device_attrs); i++)
1665 device_remove_file(info->dev, &fb_device_attrs[i]);
1666 device_remove_bin_file(info->dev, &edid_attr);
1667
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001668 usb_set_intfdata(interface, NULL);
1669
Bernie Thompson33077b82010-09-05 16:35:19 -07001670 /* if clients still have us open, will be freed on last close */
1671 if (dev->fb_count == 0)
1672 schedule_delayed_work(&dev->free_framebuffer_work, 0);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001673
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001674 /* release reference taken by kref_init in probe() */
Bernie Thompson33077b82010-09-05 16:35:19 -07001675 kref_put(&dev->kref, dlfb_free);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001676
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001677 /* consider dlfb_data freed */
1678
1679 return;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001680}
1681
1682static struct usb_driver dlfb_driver = {
1683 .name = "udlfb",
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001684 .probe = dlfb_usb_probe,
1685 .disconnect = dlfb_usb_disconnect,
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001686 .id_table = id_table,
1687};
1688
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001689static int __init dlfb_module_init(void)
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001690{
1691 int res;
1692
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001693 res = usb_register(&dlfb_driver);
1694 if (res)
1695 err("usb_register failed. Error number %d", res);
1696
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001697 return res;
1698}
1699
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001700static void __exit dlfb_module_exit(void)
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001701{
1702 usb_deregister(&dlfb_driver);
1703}
1704
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001705module_init(dlfb_module_init);
1706module_exit(dlfb_module_exit);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001707
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001708static void dlfb_urb_completion(struct urb *urb)
1709{
1710 struct urb_node *unode = urb->context;
1711 struct dlfb_data *dev = unode->dev;
1712 unsigned long flags;
1713
1714 /* sync/async unlink faults aren't errors */
1715 if (urb->status) {
1716 if (!(urb->status == -ENOENT ||
1717 urb->status == -ECONNRESET ||
1718 urb->status == -ESHUTDOWN)) {
1719 dl_err("%s - nonzero write bulk status received: %d\n",
1720 __func__, urb->status);
1721 atomic_set(&dev->lost_pixels, 1);
1722 }
1723 }
1724
1725 urb->transfer_buffer_length = dev->urbs.size; /* reset to actual */
1726
1727 spin_lock_irqsave(&dev->urbs.lock, flags);
1728 list_add_tail(&unode->entry, &dev->urbs.list);
1729 dev->urbs.available++;
1730 spin_unlock_irqrestore(&dev->urbs.lock, flags);
1731
1732 up(&dev->urbs.limit_sem);
1733}
1734
1735static void dlfb_free_urb_list(struct dlfb_data *dev)
1736{
1737 int count = dev->urbs.count;
1738 struct list_head *node;
1739 struct urb_node *unode;
1740 struct urb *urb;
1741 int ret;
1742 unsigned long flags;
1743
1744 dl_notice("Waiting for completes and freeing all render urbs\n");
1745
1746 /* keep waiting and freeing, until we've got 'em all */
1747 while (count--) {
Bernie Thompson33077b82010-09-05 16:35:19 -07001748
1749 /* Getting interrupted means a leak, but ok at shutdown*/
1750 ret = down_interruptible(&dev->urbs.limit_sem);
1751 if (ret)
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001752 break;
Bernie Thompson33077b82010-09-05 16:35:19 -07001753
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001754 spin_lock_irqsave(&dev->urbs.lock, flags);
1755
1756 node = dev->urbs.list.next; /* have reserved one with sem */
1757 list_del_init(node);
1758
1759 spin_unlock_irqrestore(&dev->urbs.lock, flags);
1760
1761 unode = list_entry(node, struct urb_node, entry);
1762 urb = unode->urb;
1763
1764 /* Free each separately allocated piece */
Greg Kroah-Hartmanc220cc32010-04-29 15:36:29 -07001765 usb_free_coherent(urb->dev, dev->urbs.size,
1766 urb->transfer_buffer, urb->transfer_dma);
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001767 usb_free_urb(urb);
1768 kfree(node);
1769 }
1770
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001771}
1772
1773static int dlfb_alloc_urb_list(struct dlfb_data *dev, int count, size_t size)
1774{
1775 int i = 0;
1776 struct urb *urb;
1777 struct urb_node *unode;
1778 char *buf;
1779
1780 spin_lock_init(&dev->urbs.lock);
1781
1782 dev->urbs.size = size;
1783 INIT_LIST_HEAD(&dev->urbs.list);
1784
1785 while (i < count) {
1786 unode = kzalloc(sizeof(struct urb_node), GFP_KERNEL);
1787 if (!unode)
1788 break;
1789 unode->dev = dev;
1790
1791 urb = usb_alloc_urb(0, GFP_KERNEL);
1792 if (!urb) {
1793 kfree(unode);
1794 break;
1795 }
1796 unode->urb = urb;
1797
Greg Kroah-Hartmanc220cc32010-04-29 15:36:29 -07001798 buf = usb_alloc_coherent(dev->udev, MAX_TRANSFER, GFP_KERNEL,
1799 &urb->transfer_dma);
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001800 if (!buf) {
1801 kfree(unode);
1802 usb_free_urb(urb);
1803 break;
1804 }
1805
1806 /* urb->transfer_buffer_length set to actual before submit */
1807 usb_fill_bulk_urb(urb, dev->udev, usb_sndbulkpipe(dev->udev, 1),
1808 buf, size, dlfb_urb_completion, unode);
1809 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1810
1811 list_add_tail(&unode->entry, &dev->urbs.list);
1812
1813 i++;
1814 }
1815
1816 sema_init(&dev->urbs.limit_sem, i);
1817 dev->urbs.count = i;
1818 dev->urbs.available = i;
1819
Soeren Moellerb5a21042010-05-14 19:03:00 +00001820 dl_notice("allocated %d %d byte urbs\n", i, (int) size);
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001821
1822 return i;
1823}
1824
1825static struct urb *dlfb_get_urb(struct dlfb_data *dev)
1826{
1827 int ret = 0;
1828 struct list_head *entry;
1829 struct urb_node *unode;
1830 struct urb *urb = NULL;
1831 unsigned long flags;
1832
1833 /* Wait for an in-flight buffer to complete and get re-queued */
1834 ret = down_timeout(&dev->urbs.limit_sem, GET_URB_TIMEOUT);
1835 if (ret) {
1836 atomic_set(&dev->lost_pixels, 1);
1837 dl_err("wait for urb interrupted: %x\n", ret);
1838 goto error;
1839 }
1840
1841 spin_lock_irqsave(&dev->urbs.lock, flags);
1842
1843 BUG_ON(list_empty(&dev->urbs.list)); /* reserved one with limit_sem */
1844 entry = dev->urbs.list.next;
1845 list_del_init(entry);
1846 dev->urbs.available--;
1847
1848 spin_unlock_irqrestore(&dev->urbs.lock, flags);
1849
1850 unode = list_entry(entry, struct urb_node, entry);
1851 urb = unode->urb;
1852
1853error:
1854 return urb;
1855}
1856
1857static int dlfb_submit_urb(struct dlfb_data *dev, struct urb *urb, size_t len)
1858{
1859 int ret;
1860
1861 BUG_ON(len > dev->urbs.size);
1862
1863 urb->transfer_buffer_length = len; /* set to actual payload len */
1864 ret = usb_submit_urb(urb, GFP_KERNEL);
1865 if (ret) {
1866 dlfb_urb_completion(urb); /* because no one else will */
1867 atomic_set(&dev->lost_pixels, 1);
1868 dl_err("usb_submit_urb error %x\n", ret);
1869 }
1870 return ret;
1871}
1872
Bernie Thompson59277b62009-11-24 15:52:21 -08001873MODULE_AUTHOR("Roberto De Ioris <roberto@unbit.it>, "
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001874 "Jaya Kumar <jayakumar.lkml@gmail.com>, "
1875 "Bernie Thompson <bernie@plugable.com>");
1876MODULE_DESCRIPTION("DisplayLink kernel framebuffer driver");
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001877MODULE_LICENSE("GPL");
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001878