blob: b13869bbe09d71ffbbb422d071940efb3ec51f11 [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;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700417
Bernie Thompson530f43a2010-02-15 06:46:21 -0800418 prefetchw((void *) cmd); /* pull in one cache line at least */
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700419
Bernie Thompson530f43a2010-02-15 06:46:21 -0800420 *cmd++ = 0xAF;
421 *cmd++ = 0x6B;
Bernie Thompson1572f912010-09-05 16:35:27 -0700422 *cmd++ = (uint8_t) ((dev_addr >> 16) & 0xFF);
423 *cmd++ = (uint8_t) ((dev_addr >> 8) & 0xFF);
424 *cmd++ = (uint8_t) ((dev_addr) & 0xFF);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700425
Bernie Thompson530f43a2010-02-15 06:46:21 -0800426 cmd_pixels_count_byte = cmd++; /* we'll know this later */
427 cmd_pixel_start = pixel;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700428
Bernie Thompson530f43a2010-02-15 06:46:21 -0800429 raw_pixels_count_byte = cmd++; /* we'll know this later */
430 raw_pixel_start = pixel;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700431
Bernie Thompson530f43a2010-02-15 06:46:21 -0800432 cmd_pixel_end = pixel + min(MAX_CMD_PIXELS + 1,
433 min((int)(pixel_end - pixel),
434 (int)(cmd_buffer_end - cmd) / bpp));
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700435
Bernie Thompson530f43a2010-02-15 06:46:21 -0800436 prefetch_range((void *) pixel, (cmd_pixel_end - pixel) * bpp);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700437
Bernie Thompson530f43a2010-02-15 06:46:21 -0800438 while (pixel < cmd_pixel_end) {
439 const uint16_t * const repeating_pixel = pixel;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700440
Bernie Thompson530f43a2010-02-15 06:46:21 -0800441 *(uint16_t *)cmd = cpu_to_be16p(pixel);
442 cmd += 2;
443 pixel++;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700444
Bernie Thompson530f43a2010-02-15 06:46:21 -0800445 if (unlikely((pixel < cmd_pixel_end) &&
446 (*pixel == *repeating_pixel))) {
447 /* go back and fill in raw pixel count */
448 *raw_pixels_count_byte = ((repeating_pixel -
449 raw_pixel_start) + 1) & 0xFF;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700450
Bernie Thompson530f43a2010-02-15 06:46:21 -0800451 while ((pixel < cmd_pixel_end)
452 && (*pixel == *repeating_pixel)) {
453 pixel++;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700454 }
Bernie Thompson59277b62009-11-24 15:52:21 -0800455
Bernie Thompson530f43a2010-02-15 06:46:21 -0800456 /* immediately after raw data is repeat byte */
457 *cmd++ = ((pixel - repeating_pixel) - 1) & 0xFF;
Bernie Thompson59277b62009-11-24 15:52:21 -0800458
Bernie Thompson530f43a2010-02-15 06:46:21 -0800459 /* Then start another raw pixel span */
460 raw_pixel_start = pixel;
461 raw_pixels_count_byte = cmd++;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700462 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700463 }
464
Bernie Thompson530f43a2010-02-15 06:46:21 -0800465 if (pixel > raw_pixel_start) {
466 /* finalize last RAW span */
467 *raw_pixels_count_byte = (pixel-raw_pixel_start) & 0xFF;
468 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700469
Bernie Thompson530f43a2010-02-15 06:46:21 -0800470 *cmd_pixels_count_byte = (pixel - cmd_pixel_start) & 0xFF;
471 dev_addr += (pixel - cmd_pixel_start) * bpp;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700472 }
473
Bernie Thompson530f43a2010-02-15 06:46:21 -0800474 if (cmd_buffer_end <= MIN_RLX_CMD_BYTES + cmd) {
475 /* Fill leftover bytes with no-ops */
476 if (cmd_buffer_end > cmd)
477 memset(cmd, 0xAF, cmd_buffer_end - cmd);
478 cmd = (uint8_t *) cmd_buffer_end;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700479 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700480
Bernie Thompson530f43a2010-02-15 06:46:21 -0800481 *command_buffer_ptr = cmd;
482 *pixel_start_ptr = pixel;
483 *device_address_ptr = dev_addr;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700484
Bernie Thompson530f43a2010-02-15 06:46:21 -0800485 return;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700486}
487
Bernie Thompson530f43a2010-02-15 06:46:21 -0800488/*
489 * There are 3 copies of every pixel: The front buffer that the fbdev
490 * client renders to, the actual framebuffer across the USB bus in hardware
491 * (that we can only write to, slowly, and can never read), and (optionally)
492 * our shadow copy that tracks what's been sent to that hardware buffer.
493 */
494static void dlfb_render_hline(struct dlfb_data *dev, struct urb **urb_ptr,
495 const char *front, char **urb_buf_ptr,
496 u32 byte_offset, u32 byte_width,
497 int *ident_ptr, int *sent_ptr)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700498{
Bernie Thompson530f43a2010-02-15 06:46:21 -0800499 const u8 *line_start, *line_end, *next_pixel;
500 u32 dev_addr = dev->base16 + byte_offset;
501 struct urb *urb = *urb_ptr;
502 u8 *cmd = *urb_buf_ptr;
503 u8 *cmd_end = (u8 *) urb->transfer_buffer + urb->transfer_buffer_length;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700504
Bernie Thompson530f43a2010-02-15 06:46:21 -0800505 line_start = (u8 *) (front + byte_offset);
506 next_pixel = line_start;
507 line_end = next_pixel + byte_width;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700508
Bernie Thompson530f43a2010-02-15 06:46:21 -0800509 if (dev->backing_buffer) {
510 int offset;
511 const u8 *back_start = (u8 *) (dev->backing_buffer
512 + byte_offset);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700513
Bernie Thompson530f43a2010-02-15 06:46:21 -0800514 *ident_ptr += dlfb_trim_hline(back_start, &next_pixel,
515 &byte_width);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700516
Bernie Thompson530f43a2010-02-15 06:46:21 -0800517 offset = next_pixel - line_start;
518 line_end = next_pixel + byte_width;
519 dev_addr += offset;
520 back_start += offset;
521 line_start += offset;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700522
Bernie Thompson530f43a2010-02-15 06:46:21 -0800523 memcpy((char *)back_start, (char *) line_start,
524 byte_width);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700525 }
526
Bernie Thompson530f43a2010-02-15 06:46:21 -0800527 while (next_pixel < line_end) {
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700528
Bernie Thompson530f43a2010-02-15 06:46:21 -0800529 dlfb_compress_hline((const uint16_t **) &next_pixel,
530 (const uint16_t *) line_end, &dev_addr,
531 (u8 **) &cmd, (u8 *) cmd_end);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700532
Bernie Thompson530f43a2010-02-15 06:46:21 -0800533 if (cmd >= cmd_end) {
534 int len = cmd - (u8 *) urb->transfer_buffer;
535 if (dlfb_submit_urb(dev, urb, len))
536 return; /* lost pixels is set */
537 *sent_ptr += len;
538 urb = dlfb_get_urb(dev);
539 if (!urb)
540 return; /* lost_pixels is set */
541 *urb_ptr = urb;
542 cmd = urb->transfer_buffer;
543 cmd_end = &cmd[urb->transfer_buffer_length];
544 }
545 }
546
547 *urb_buf_ptr = cmd;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700548}
549
Bernie Thompson530f43a2010-02-15 06:46:21 -0800550int dlfb_handle_damage(struct dlfb_data *dev, int x, int y,
551 int width, int height, char *data)
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700552{
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700553 int i, ret;
Bernie Thompson530f43a2010-02-15 06:46:21 -0800554 char *cmd;
555 cycles_t start_cycles, end_cycles;
556 int bytes_sent = 0;
557 int bytes_identical = 0;
558 struct urb *urb;
559 int aligned_x;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700560
Bernie Thompson530f43a2010-02-15 06:46:21 -0800561 start_cycles = get_cycles();
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700562
Bernie Thompson530f43a2010-02-15 06:46:21 -0800563 aligned_x = DL_ALIGN_DOWN(x, sizeof(unsigned long));
564 width = DL_ALIGN_UP(width + (x-aligned_x), sizeof(unsigned long));
565 x = aligned_x;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700566
Bernie Thompson530f43a2010-02-15 06:46:21 -0800567 if ((width <= 0) ||
568 (x + width > dev->info->var.xres) ||
569 (y + height > dev->info->var.yres))
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700570 return -EINVAL;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700571
Bernie Thompson530f43a2010-02-15 06:46:21 -0800572 if (!atomic_read(&dev->usb_active))
573 return 0;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700574
Bernie Thompson530f43a2010-02-15 06:46:21 -0800575 urb = dlfb_get_urb(dev);
576 if (!urb)
577 return 0;
578 cmd = urb->transfer_buffer;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700579
Bernie Thompson530f43a2010-02-15 06:46:21 -0800580 for (i = y; i < y + height ; i++) {
581 const int line_offset = dev->info->fix.line_length * i;
582 const int byte_offset = line_offset + (x * BPP);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700583
Bernie Thompson530f43a2010-02-15 06:46:21 -0800584 dlfb_render_hline(dev, &urb, (char *) dev->info->fix.smem_start,
585 &cmd, byte_offset, width * BPP,
586 &bytes_identical, &bytes_sent);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700587 }
588
Bernie Thompson530f43a2010-02-15 06:46:21 -0800589 if (cmd > (char *) urb->transfer_buffer) {
590 /* Send partial buffer remaining before exiting */
591 int len = cmd - (char *) urb->transfer_buffer;
592 ret = dlfb_submit_urb(dev, urb, len);
593 bytes_sent += len;
594 } else
595 dlfb_urb_completion(urb);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700596
Bernie Thompson530f43a2010-02-15 06:46:21 -0800597 atomic_add(bytes_sent, &dev->bytes_sent);
598 atomic_add(bytes_identical, &dev->bytes_identical);
599 atomic_add(width*height*2, &dev->bytes_rendered);
600 end_cycles = get_cycles();
601 atomic_add(((unsigned int) ((end_cycles - start_cycles)
602 >> 10)), /* Kcycles */
603 &dev->cpu_kcycles_used);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700604
Bernie Thompson530f43a2010-02-15 06:46:21 -0800605 return 0;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700606}
607
Bernie Thompsond46ecb92010-09-05 16:35:04 -0700608static ssize_t dlfb_ops_read(struct fb_info *info, char __user *buf,
609 size_t count, loff_t *ppos)
610{
611 ssize_t result = -ENOSYS;
612
613#if defined CONFIG_FB_SYS_FOPS || defined CONFIG_FB_SYS_FOPS_MODULE
614 result = fb_sys_read(info, buf, count, ppos);
615#endif
616
617 return result;
618}
619
620/*
621 * Path triggered by usermode clients who write to filesystem
622 * e.g. cat filename > /dev/fb1
623 * Not used by X Windows or text-mode console. But useful for testing.
624 * Slow because of extra copy and we must assume all pixels dirty.
625 */
626static ssize_t dlfb_ops_write(struct fb_info *info, const char __user *buf,
627 size_t count, loff_t *ppos)
628{
629 ssize_t result = -ENOSYS;
630 struct dlfb_data *dev = info->par;
631 u32 offset = (u32) *ppos;
632
633#if defined CONFIG_FB_SYS_FOPS || defined CONFIG_FB_SYS_FOPS_MODULE
634
635 result = fb_sys_write(info, buf, count, ppos);
636
637 if (result > 0) {
638 int start = max((int)(offset / info->fix.line_length) - 1, 0);
639 int lines = min((u32)((result / info->fix.line_length) + 1),
640 (u32)info->var.yres);
641
642 dlfb_handle_damage(dev, 0, start, info->var.xres,
643 lines, info->screen_base);
644 }
645#endif
646
647 return result;
648}
649
Bernie Thompson530f43a2010-02-15 06:46:21 -0800650/* hardware has native COPY command (see libdlo), but not worth it for fbcon */
Bernie Thompson45742032010-02-15 06:46:04 -0800651static void dlfb_ops_copyarea(struct fb_info *info,
Bernie Thompson530f43a2010-02-15 06:46:21 -0800652 const struct fb_copyarea *area)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700653{
Bernie Thompson530f43a2010-02-15 06:46:21 -0800654
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700655 struct dlfb_data *dev = info->par;
656
Bernie Thompson530f43a2010-02-15 06:46:21 -0800657#if defined CONFIG_FB_SYS_COPYAREA || defined CONFIG_FB_SYS_COPYAREA_MODULE
658
659 sys_copyarea(info, area);
660
661 dlfb_handle_damage(dev, area->dx, area->dy,
662 area->width, area->height, info->screen_base);
663#endif
Bernie Thompson530f43a2010-02-15 06:46:21 -0800664
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700665}
666
Bernie Thompson45742032010-02-15 06:46:04 -0800667static void dlfb_ops_imageblit(struct fb_info *info,
Bernie Thompson530f43a2010-02-15 06:46:21 -0800668 const struct fb_image *image)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700669{
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700670 struct dlfb_data *dev = info->par;
Bernie Thompson530f43a2010-02-15 06:46:21 -0800671
672#if defined CONFIG_FB_SYS_IMAGEBLIT || defined CONFIG_FB_SYS_IMAGEBLIT_MODULE
673
674 sys_imageblit(info, image);
675
676 dlfb_handle_damage(dev, image->dx, image->dy,
677 image->width, image->height, info->screen_base);
678
679#endif
680
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700681}
682
Bernie Thompson45742032010-02-15 06:46:04 -0800683static void dlfb_ops_fillrect(struct fb_info *info,
Bernie Thompson530f43a2010-02-15 06:46:21 -0800684 const struct fb_fillrect *rect)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700685{
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700686 struct dlfb_data *dev = info->par;
687
Bernie Thompson530f43a2010-02-15 06:46:21 -0800688#if defined CONFIG_FB_SYS_FILLRECT || defined CONFIG_FB_SYS_FILLRECT_MODULE
689
690 sys_fillrect(info, rect);
691
692 dlfb_handle_damage(dev, rect->dx, rect->dy, rect->width,
693 rect->height, info->screen_base);
694#endif
695
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700696}
697
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700698static int dlfb_get_edid(struct dlfb_data *dev, char *edid, int len)
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800699{
700 int i;
701 int ret;
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700702 char *rbuf;
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800703
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700704 rbuf = kmalloc(2, GFP_KERNEL);
705 if (!rbuf)
706 return 0;
707
708 for (i = 0; i < len; i++) {
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800709 ret = usb_control_msg(dev->udev,
710 usb_rcvctrlpipe(dev->udev, 0), (0x02),
711 (0x80 | (0x02 << 5)), i << 8, 0xA1, rbuf, 2,
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700712 HZ);
713 if (ret < 1) {
714 dl_err("Read EDID byte %d failed err %x\n", i, ret);
715 i--;
716 break;
717 }
718 edid[i] = rbuf[1];
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800719 }
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700720
721 kfree(rbuf);
722
723 return i;
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800724}
725
Bernie Thompson45742032010-02-15 06:46:04 -0800726static int dlfb_ops_ioctl(struct fb_info *info, unsigned int cmd,
Bernie Thompson530f43a2010-02-15 06:46:21 -0800727 unsigned long arg)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700728{
Bernie Thompson530f43a2010-02-15 06:46:21 -0800729
730 struct dlfb_data *dev = info->par;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700731 struct dloarea *area = NULL;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700732
Bernie Thompson530f43a2010-02-15 06:46:21 -0800733 if (!atomic_read(&dev->usb_active))
734 return 0;
735
736 /* TODO: Update X server to get this from sysfs instead */
737 if (cmd == DLFB_IOCTL_RETURN_EDID) {
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700738 char *edid = (char *)arg;
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700739 if (copy_to_user(edid, dev->edid, dev->edid_size))
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700740 return -EFAULT;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700741 return 0;
742 }
743
Bernie Thompson530f43a2010-02-15 06:46:21 -0800744 /* TODO: Help propose a standard fb.h ioctl to report mmap damage */
745 if (cmd == DLFB_IOCTL_REPORT_DAMAGE) {
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700746
747 area = (struct dloarea *)arg;
748
749 if (area->x < 0)
750 area->x = 0;
751
752 if (area->x > info->var.xres)
753 area->x = info->var.xres;
754
755 if (area->y < 0)
756 area->y = 0;
757
758 if (area->y > info->var.yres)
759 area->y = info->var.yres;
760
Bernie Thompson530f43a2010-02-15 06:46:21 -0800761 atomic_set(&dev->use_defio, 0);
762
763 dlfb_handle_damage(dev, area->x, area->y, area->w, area->h,
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700764 info->screen_base);
765 }
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700766
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700767 return 0;
768}
769
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700770/* taken from vesafb */
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700771static int
Bernie Thompson45742032010-02-15 06:46:04 -0800772dlfb_ops_setcolreg(unsigned regno, unsigned red, unsigned green,
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700773 unsigned blue, unsigned transp, struct fb_info *info)
774{
775 int err = 0;
776
777 if (regno >= info->cmap.len)
778 return 1;
779
780 if (regno < 16) {
781 if (info->var.red.offset == 10) {
782 /* 1:5:5:5 */
783 ((u32 *) (info->pseudo_palette))[regno] =
784 ((red & 0xf800) >> 1) |
785 ((green & 0xf800) >> 6) | ((blue & 0xf800) >> 11);
786 } else {
787 /* 0:5:6:5 */
788 ((u32 *) (info->pseudo_palette))[regno] =
789 ((red & 0xf800)) |
790 ((green & 0xfc00) >> 5) | ((blue & 0xf800) >> 11);
791 }
792 }
793
794 return err;
795}
796
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800797/*
798 * It's common for several clients to have framebuffer open simultaneously.
799 * e.g. both fbcon and X. Makes things interesting.
Bernie Thompson33077b82010-09-05 16:35:19 -0700800 * Assumes caller is holding info->lock (for open and release at least)
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800801 */
802static int dlfb_ops_open(struct fb_info *info, int user)
803{
804 struct dlfb_data *dev = info->par;
805
806/* if (user == 0)
807 * We could special case kernel mode clients (fbcon) here
808 */
809
Bernie Thompson33077b82010-09-05 16:35:19 -0700810 /* If the USB device is gone, we don't accept new opens */
811 if (dev->virtualized)
812 return -ENODEV;
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800813
814 dev->fb_count++;
815
Bernie Thompson33077b82010-09-05 16:35:19 -0700816 kref_get(&dev->kref);
817
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800818#ifdef CONFIG_FB_DEFERRED_IO
819 if ((atomic_read(&dev->use_defio)) && (info->fbdefio == NULL)) {
820 /* enable defio */
821 info->fbdefio = &dlfb_defio;
822 fb_deferred_io_init(info);
823 }
824#endif
825
826 dl_notice("open /dev/fb%d user=%d fb_info=%p count=%d\n",
827 info->node, user, info, dev->fb_count);
828
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700829 return 0;
830}
831
Bernie Thompson4a4854d2010-02-15 06:45:55 -0800832/*
833 * Called when all client interfaces to start transactions have been disabled,
834 * and all references to our device instance (dlfb_data) are released.
835 * Every transaction must have a reference, so we know are fully spun down
836 */
Bernie Thompson33077b82010-09-05 16:35:19 -0700837static void dlfb_free(struct kref *kref)
Bernie Thompson4a4854d2010-02-15 06:45:55 -0800838{
839 struct dlfb_data *dev = container_of(kref, struct dlfb_data, kref);
840
Bernie Thompson33077b82010-09-05 16:35:19 -0700841 /* this function will wait for all in-flight urbs to complete */
842 if (dev->urbs.count > 0)
843 dlfb_free_urb_list(dev);
844
Bernie Thompson4a4854d2010-02-15 06:45:55 -0800845 if (dev->backing_buffer)
846 vfree(dev->backing_buffer);
847
Bernie Thompson33077b82010-09-05 16:35:19 -0700848 kfree(dev->edid);
849
850 dl_warn("freeing dlfb_data %p\n", dev);
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800851
Bernie Thompson4a4854d2010-02-15 06:45:55 -0800852 kfree(dev);
853}
854
Bernie Thompson33077b82010-09-05 16:35:19 -0700855
856static void dlfb_free_framebuffer_work(struct work_struct *work)
Bernie Thompson2469d5d2010-02-15 06:46:13 -0800857{
Bernie Thompson33077b82010-09-05 16:35:19 -0700858 struct dlfb_data *dev = container_of(work, struct dlfb_data,
859 free_framebuffer_work.work);
860 struct fb_info *info = dev->info;
861 int node = info->node;
862
863 unregister_framebuffer(info);
Bernie Thompson2469d5d2010-02-15 06:46:13 -0800864
865 if (info->cmap.len != 0)
866 fb_dealloc_cmap(&info->cmap);
867 if (info->monspecs.modedb)
868 fb_destroy_modedb(info->monspecs.modedb);
869 if (info->screen_base)
870 vfree(info->screen_base);
871
872 fb_destroy_modelist(&info->modelist);
873
Bernie Thompson33077b82010-09-05 16:35:19 -0700874 dev->info = 0;
875
876 /* Assume info structure is freed after this point */
Bernie Thompson2469d5d2010-02-15 06:46:13 -0800877 framebuffer_release(info);
878
Bernie Thompson33077b82010-09-05 16:35:19 -0700879 dl_warn("fb_info for /dev/fb%d has been freed\n", node);
880
881 /* ref taken in probe() as part of registering framebfufer */
882 kref_put(&dev->kref, dlfb_free);
883}
884
885/*
886 * Assumes caller is holding info->lock mutex (for open and release at least)
887 */
888static int dlfb_ops_release(struct fb_info *info, int user)
889{
890 struct dlfb_data *dev = info->par;
891
892 dev->fb_count--;
893
894 /* We can't free fb_info here - fbmem will touch it when we return */
895 if (dev->virtualized && (dev->fb_count == 0))
896 schedule_delayed_work(&dev->free_framebuffer_work, HZ);
897
898#ifdef CONFIG_FB_DEFERRED_IO
899 if ((dev->fb_count == 0) && (info->fbdefio)) {
900 fb_deferred_io_cleanup(info);
901 kfree(info->fbdefio);
902 info->fbdefio = NULL;
903 info->fbops->fb_mmap = dlfb_ops_mmap;
904 }
905#endif
906
907 dl_warn("released /dev/fb%d user=%d count=%d\n",
908 info->node, user, dev->fb_count);
909
910 kref_put(&dev->kref, dlfb_free);
911
912 return 0;
Bernie Thompson2469d5d2010-02-15 06:46:13 -0800913}
914
915/*
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800916 * Check whether a video mode is supported by the DisplayLink chip
917 * We start from monitor's modes, so don't need to filter that here
918 */
919static int dlfb_is_valid_mode(struct fb_videomode *mode,
920 struct fb_info *info)
921{
922 struct dlfb_data *dev = info->par;
923
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700924 if (mode->xres * mode->yres > dev->sku_pixel_limit) {
925 dl_warn("%dx%d beyond chip capabilities\n",
926 mode->xres, mode->yres);
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800927 return 0;
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700928 }
929
930 dl_info("%dx%d valid mode\n", mode->xres, mode->yres);
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800931
932 return 1;
933}
934
935static void dlfb_var_color_format(struct fb_var_screeninfo *var)
936{
937 const struct fb_bitfield red = { 11, 5, 0 };
938 const struct fb_bitfield green = { 5, 6, 0 };
939 const struct fb_bitfield blue = { 0, 5, 0 };
940
941 var->bits_per_pixel = 16;
942 var->red = red;
943 var->green = green;
944 var->blue = blue;
945}
946
Bernie Thompson2469d5d2010-02-15 06:46:13 -0800947static int dlfb_ops_check_var(struct fb_var_screeninfo *var,
948 struct fb_info *info)
949{
950 struct fb_videomode mode;
951
952 /* TODO: support dynamically changing framebuffer size */
953 if ((var->xres * var->yres * 2) > info->fix.smem_len)
954 return -EINVAL;
955
956 /* set device-specific elements of var unrelated to mode */
957 dlfb_var_color_format(var);
958
959 fb_var_to_videomode(&mode, var);
960
961 if (!dlfb_is_valid_mode(&mode, info))
962 return -EINVAL;
963
964 return 0;
965}
966
967static int dlfb_ops_set_par(struct fb_info *info)
968{
969 struct dlfb_data *dev = info->par;
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700970 int result;
971 u16 *pix_framebuffer;
972 int i;
Bernie Thompson2469d5d2010-02-15 06:46:13 -0800973
974 dl_notice("set_par mode %dx%d\n", info->var.xres, info->var.yres);
975
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700976 result = dlfb_set_video_mode(dev, &info->var);
977
978 if ((result == 0) && (dev->fb_count == 0)) {
979
980 /* paint greenscreen */
981
982 pix_framebuffer = (u16 *) info->screen_base;
983 for (i = 0; i < info->fix.smem_len / 2; i++)
984 pix_framebuffer[i] = 0x37e6;
985
986 dlfb_handle_damage(dev, 0, 0, info->var.xres, info->var.yres,
987 info->screen_base);
988 }
989
990 return result;
Bernie Thompson2469d5d2010-02-15 06:46:13 -0800991}
992
Bernie Thompson9825f702010-09-05 16:35:10 -0700993/*
994 * In order to come back from full DPMS off, we need to set the mode again
995 */
Bernie Thompson45742032010-02-15 06:46:04 -0800996static int dlfb_ops_blank(int blank_mode, struct fb_info *info)
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700997{
Bernie Thompson530f43a2010-02-15 06:46:21 -0800998 struct dlfb_data *dev = info->par;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700999
Roberto De Ioris7316bc52009-06-10 23:02:19 -07001000 if (blank_mode != FB_BLANK_UNBLANK) {
Bernie Thompson9825f702010-09-05 16:35:10 -07001001 char *bufptr;
1002 struct urb *urb;
Roberto De Ioris7316bc52009-06-10 23:02:19 -07001003
Bernie Thompson9825f702010-09-05 16:35:10 -07001004 urb = dlfb_get_urb(dev);
1005 if (!urb)
1006 return 0;
1007
1008 bufptr = (char *) urb->transfer_buffer;
1009 bufptr = dlfb_vidreg_lock(bufptr);
1010 bufptr = dlfb_enable_hvsync(bufptr, false);
1011 bufptr = dlfb_vidreg_unlock(bufptr);
1012
1013 dlfb_submit_urb(dev, urb, bufptr -
1014 (char *) urb->transfer_buffer);
1015 } else {
1016 dlfb_set_video_mode(dev, &info->var);
1017 }
Roberto De Ioris7316bc52009-06-10 23:02:19 -07001018
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001019 return 0;
1020}
1021
1022static struct fb_ops dlfb_ops = {
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001023 .owner = THIS_MODULE,
Bernie Thompsond46ecb92010-09-05 16:35:04 -07001024 .fb_read = dlfb_ops_read,
1025 .fb_write = dlfb_ops_write,
Bernie Thompson45742032010-02-15 06:46:04 -08001026 .fb_setcolreg = dlfb_ops_setcolreg,
1027 .fb_fillrect = dlfb_ops_fillrect,
1028 .fb_copyarea = dlfb_ops_copyarea,
1029 .fb_imageblit = dlfb_ops_imageblit,
1030 .fb_mmap = dlfb_ops_mmap,
1031 .fb_ioctl = dlfb_ops_ioctl,
Bernie Thompson3e8f3d62010-02-15 06:46:26 -08001032 .fb_open = dlfb_ops_open,
Bernie Thompson45742032010-02-15 06:46:04 -08001033 .fb_release = dlfb_ops_release,
1034 .fb_blank = dlfb_ops_blank,
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001035 .fb_check_var = dlfb_ops_check_var,
1036 .fb_set_par = dlfb_ops_set_par,
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001037};
1038
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001039
Bernie Thompsoncc403dc2010-02-15 06:45:49 -08001040/*
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001041 * Assumes &info->lock held by caller
1042 * Assumes no active clients have framebuffer open
1043 */
1044static int dlfb_realloc_framebuffer(struct dlfb_data *dev, struct fb_info *info)
1045{
1046 int retval = -ENOMEM;
1047 int old_len = info->fix.smem_len;
1048 int new_len;
1049 unsigned char *old_fb = info->screen_base;
1050 unsigned char *new_fb;
1051 unsigned char *new_back;
1052
1053 dl_warn("Reallocating framebuffer. Addresses will change!\n");
1054
1055 new_len = info->fix.line_length * info->var.yres;
1056
1057 if (PAGE_ALIGN(new_len) > old_len) {
1058 /*
1059 * Alloc system memory for virtual framebuffer
1060 */
1061 new_fb = vmalloc(new_len);
1062 if (!new_fb) {
1063 dl_err("Virtual framebuffer alloc failed\n");
1064 goto error;
1065 }
1066
1067 if (info->screen_base) {
1068 memcpy(new_fb, old_fb, old_len);
1069 vfree(info->screen_base);
1070 }
1071
1072 info->screen_base = new_fb;
1073 info->fix.smem_len = PAGE_ALIGN(new_len);
1074 info->fix.smem_start = (unsigned long) new_fb;
1075 info->flags = udlfb_info_flags;
1076
1077 /*
1078 * Second framebuffer copy to mirror the framebuffer state
1079 * on the physical USB device. We can function without this.
1080 * But with imperfect damage info we may send pixels over USB
1081 * that were, in fact, unchanged - wasting limited USB bandwidth
1082 */
1083 new_back = vmalloc(new_len);
1084 if (!new_back)
1085 dl_info("No shadow/backing buffer allcoated\n");
1086 else {
1087 if (dev->backing_buffer)
1088 vfree(dev->backing_buffer);
1089 dev->backing_buffer = new_back;
1090 memset(dev->backing_buffer, 0, new_len);
1091 }
1092 }
1093
1094 retval = 0;
1095
1096error:
1097 return retval;
1098}
1099
1100/*
1101 * 1) Get EDID from hw, or use sw default
1102 * 2) Parse into various fb_info structs
1103 * 3) Allocate virtual framebuffer memory to back highest res mode
1104 *
1105 * Parses EDID into three places used by various parts of fbdev:
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001106 * fb_var_screeninfo contains the timing of the monitor's preferred mode
1107 * fb_info.monspecs is full parsed EDID info, including monspecs.modedb
1108 * fb_info.modelist is a linked list of all monitor & VESA modes which work
1109 *
1110 * If EDID is not readable/valid, then modelist is all VESA modes,
1111 * monspecs is NULL, and fb_var_screeninfo is set to safe VESA mode
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001112 * Returns 0 if successful
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001113 */
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001114static int dlfb_setup_modes(struct dlfb_data *dev,
1115 struct fb_info *info,
1116 char *default_edid, size_t default_edid_size)
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001117{
1118 int i;
1119 const struct fb_videomode *default_vmode = NULL;
1120 int result = 0;
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001121 char *edid;
1122 int tries = 3;
1123
1124 if (info->dev) /* only use mutex if info has been registered */
1125 mutex_lock(&info->lock);
1126
1127 edid = kmalloc(MAX_EDID_SIZE, GFP_KERNEL);
1128 if (!edid) {
1129 result = -ENOMEM;
1130 goto error;
1131 }
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001132
1133 fb_destroy_modelist(&info->modelist);
1134 memset(&info->monspecs, 0, sizeof(info->monspecs));
1135
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001136 /*
1137 * Try to (re)read EDID from hardware first
1138 * EDID data may return, but not parse as valid
1139 * Try again a few times, in case of e.g. analog cable noise
1140 */
1141 while (tries--) {
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001142
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001143 i = dlfb_get_edid(dev, edid, MAX_EDID_SIZE);
1144
1145 if (i >= MIN_EDID_SIZE)
1146 fb_edid_to_monspecs(edid, &info->monspecs);
1147
1148 if (info->monspecs.modedb_len > 0) {
1149 dev->edid = edid;
1150 dev->edid_size = i;
1151 break;
1152 }
1153 }
1154
1155 /* If that fails, use a previously returned EDID if available */
1156 if (info->monspecs.modedb_len == 0) {
1157
1158 dl_err("Unable to get valid EDID from device/display\n");
1159
1160 if (dev->edid) {
1161 fb_edid_to_monspecs(dev->edid, &info->monspecs);
1162 if (info->monspecs.modedb_len > 0)
1163 dl_err("Using previously queried EDID\n");
1164 }
1165 }
1166
1167 /* If that fails, use the default EDID we were handed */
1168 if (info->monspecs.modedb_len == 0) {
1169 if (default_edid_size >= MIN_EDID_SIZE) {
1170 fb_edid_to_monspecs(default_edid, &info->monspecs);
1171 if (info->monspecs.modedb_len > 0) {
1172 memcpy(edid, default_edid, default_edid_size);
1173 dev->edid = edid;
1174 dev->edid_size = default_edid_size;
1175 dl_err("Using default/backup EDID\n");
1176 }
1177 }
1178 }
1179
1180 /* If we've got modes, let's pick a best default mode */
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001181 if (info->monspecs.modedb_len > 0) {
1182
1183 for (i = 0; i < info->monspecs.modedb_len; i++) {
1184 if (dlfb_is_valid_mode(&info->monspecs.modedb[i], info))
1185 fb_add_videomode(&info->monspecs.modedb[i],
1186 &info->modelist);
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001187 else /* if we've removed top/best mode */
1188 info->monspecs.misc &= ~FB_MISC_1ST_DETAIL;
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001189 }
1190
1191 default_vmode = fb_find_best_display(&info->monspecs,
1192 &info->modelist);
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001193 }
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001194
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001195 /* If everything else has failed, fall back to safe default mode */
1196 if (default_vmode == NULL) {
1197
1198 struct fb_videomode fb_vmode = {0};
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001199
1200 /*
1201 * Add the standard VESA modes to our modelist
1202 * Since we don't have EDID, there may be modes that
1203 * overspec monitor and/or are incorrect aspect ratio, etc.
1204 * But at least the user has a chance to choose
1205 */
1206 for (i = 0; i < VESA_MODEDB_SIZE; i++) {
1207 if (dlfb_is_valid_mode((struct fb_videomode *)
1208 &vesa_modes[i], info))
1209 fb_add_videomode(&vesa_modes[i],
1210 &info->modelist);
1211 }
1212
1213 /*
1214 * default to resolution safe for projectors
1215 * (since they are most common case without EDID)
1216 */
1217 fb_vmode.xres = 800;
1218 fb_vmode.yres = 600;
1219 fb_vmode.refresh = 60;
1220 default_vmode = fb_find_nearest_mode(&fb_vmode,
1221 &info->modelist);
1222 }
1223
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001224 /* If we have good mode and no active clients*/
1225 if ((default_vmode != NULL) && (dev->fb_count == 0)) {
1226
1227 fb_videomode_to_var(&info->var, default_vmode);
1228 dlfb_var_color_format(&info->var);
1229
1230 /*
1231 * with mode size info, we can now alloc our framebuffer.
1232 */
1233 memcpy(&info->fix, &dlfb_fix, sizeof(dlfb_fix));
1234 info->fix.line_length = info->var.xres *
1235 (info->var.bits_per_pixel / 8);
1236
1237 result = dlfb_realloc_framebuffer(dev, info);
1238
1239 } else
1240 result = -EINVAL;
1241
1242error:
1243 if (edid && (dev->edid != edid))
1244 kfree(edid);
1245
1246 if (info->dev)
1247 mutex_unlock(&info->lock);
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001248
1249 return result;
1250}
1251
1252static ssize_t metrics_bytes_rendered_show(struct device *fbdev,
1253 struct device_attribute *a, char *buf) {
1254 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1255 struct dlfb_data *dev = fb_info->par;
1256 return snprintf(buf, PAGE_SIZE, "%u\n",
1257 atomic_read(&dev->bytes_rendered));
1258}
1259
1260static ssize_t metrics_bytes_identical_show(struct device *fbdev,
1261 struct device_attribute *a, char *buf) {
1262 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1263 struct dlfb_data *dev = fb_info->par;
1264 return snprintf(buf, PAGE_SIZE, "%u\n",
1265 atomic_read(&dev->bytes_identical));
1266}
1267
1268static ssize_t metrics_bytes_sent_show(struct device *fbdev,
1269 struct device_attribute *a, char *buf) {
1270 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1271 struct dlfb_data *dev = fb_info->par;
1272 return snprintf(buf, PAGE_SIZE, "%u\n",
1273 atomic_read(&dev->bytes_sent));
1274}
1275
1276static ssize_t metrics_cpu_kcycles_used_show(struct device *fbdev,
1277 struct device_attribute *a, char *buf) {
1278 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1279 struct dlfb_data *dev = fb_info->par;
1280 return snprintf(buf, PAGE_SIZE, "%u\n",
1281 atomic_read(&dev->cpu_kcycles_used));
1282}
1283
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001284static ssize_t edid_show(
1285 struct file *filp,
1286 struct kobject *kobj, struct bin_attribute *a,
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001287 char *buf, loff_t off, size_t count) {
1288 struct device *fbdev = container_of(kobj, struct device, kobj);
1289 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1290 struct dlfb_data *dev = fb_info->par;
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001291
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001292 if (dev->edid == NULL)
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001293 return 0;
1294
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001295 if ((off >= dev->edid_size) || (count > dev->edid_size))
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001296 return 0;
1297
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001298 if (off + count > dev->edid_size)
1299 count = dev->edid_size - off;
1300
1301 dl_info("sysfs edid copy %p to %p, %d bytes\n",
1302 dev->edid, buf, (int) count);
1303
1304 memcpy(buf, dev->edid, count);
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001305
1306 return count;
1307}
1308
Bernie Thompson8ef8cc42010-09-05 16:35:31 -07001309static ssize_t edid_store(
1310 struct file *filp,
1311 struct kobject *kobj, struct bin_attribute *a,
1312 char *src, loff_t src_off, size_t src_size) {
1313 struct device *fbdev = container_of(kobj, struct device, kobj);
1314 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1315 struct dlfb_data *dev = fb_info->par;
1316
1317 /* We only support write of entire EDID at once, no offset*/
1318 if ((src_size < MIN_EDID_SIZE) ||
1319 (src_size > MAX_EDID_SIZE) ||
1320 (src_off != 0))
1321 return 0;
1322
1323 dlfb_setup_modes(dev, fb_info, src, src_size);
1324
1325 if (dev->edid && (memcmp(src, dev->edid, src_size) == 0)) {
1326 dl_info("sysfs written EDID is new default\n");
1327 dlfb_ops_set_par(fb_info);
1328 return src_size;
1329 } else
1330 return 0;
1331}
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001332
1333static ssize_t metrics_reset_store(struct device *fbdev,
1334 struct device_attribute *attr,
1335 const char *buf, size_t count)
1336{
1337 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1338 struct dlfb_data *dev = fb_info->par;
1339
1340 atomic_set(&dev->bytes_rendered, 0);
1341 atomic_set(&dev->bytes_identical, 0);
1342 atomic_set(&dev->bytes_sent, 0);
1343 atomic_set(&dev->cpu_kcycles_used, 0);
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001344
1345 return count;
1346}
1347
1348static ssize_t use_defio_show(struct device *fbdev,
1349 struct device_attribute *a, char *buf) {
1350 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1351 struct dlfb_data *dev = fb_info->par;
1352 return snprintf(buf, PAGE_SIZE, "%d\n",
1353 atomic_read(&dev->use_defio));
1354}
1355
1356static ssize_t use_defio_store(struct device *fbdev,
1357 struct device_attribute *attr,
1358 const char *buf, size_t count)
1359{
1360 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1361 struct dlfb_data *dev = fb_info->par;
1362
1363 if (count > 0) {
1364 if (buf[0] == '0')
1365 atomic_set(&dev->use_defio, 0);
1366 if (buf[0] == '1')
1367 atomic_set(&dev->use_defio, 1);
1368 }
1369 return count;
1370}
1371
1372static struct bin_attribute edid_attr = {
1373 .attr.name = "edid",
Bernie Thompson8ef8cc42010-09-05 16:35:31 -07001374 .attr.mode = 0666,
1375 .size = MAX_EDID_SIZE,
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001376 .read = edid_show,
Bernie Thompson8ef8cc42010-09-05 16:35:31 -07001377 .write = edid_store
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001378};
1379
1380static struct device_attribute fb_device_attrs[] = {
1381 __ATTR_RO(metrics_bytes_rendered),
1382 __ATTR_RO(metrics_bytes_identical),
1383 __ATTR_RO(metrics_bytes_sent),
1384 __ATTR_RO(metrics_cpu_kcycles_used),
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001385 __ATTR(metrics_reset, S_IWUGO, NULL, metrics_reset_store),
1386 __ATTR_RW(use_defio),
1387};
1388
Bernie Thompson3e8f3d62010-02-15 06:46:26 -08001389#ifdef CONFIG_FB_DEFERRED_IO
1390static void dlfb_dpy_deferred_io(struct fb_info *info,
1391 struct list_head *pagelist)
1392{
1393 struct page *cur;
1394 struct fb_deferred_io *fbdefio = info->fbdefio;
1395 struct dlfb_data *dev = info->par;
1396 struct urb *urb;
1397 char *cmd;
1398 cycles_t start_cycles, end_cycles;
1399 int bytes_sent = 0;
1400 int bytes_identical = 0;
1401 int bytes_rendered = 0;
Bernie Thompson3e8f3d62010-02-15 06:46:26 -08001402
1403 if (!atomic_read(&dev->use_defio))
1404 return;
1405
1406 if (!atomic_read(&dev->usb_active))
1407 return;
1408
1409 start_cycles = get_cycles();
1410
1411 urb = dlfb_get_urb(dev);
1412 if (!urb)
1413 return;
1414 cmd = urb->transfer_buffer;
1415
1416 /* walk the written page list and render each to device */
1417 list_for_each_entry(cur, &fbdefio->pagelist, lru) {
1418 dlfb_render_hline(dev, &urb, (char *) info->fix.smem_start,
1419 &cmd, cur->index << PAGE_SHIFT,
1420 PAGE_SIZE, &bytes_identical, &bytes_sent);
1421 bytes_rendered += PAGE_SIZE;
Bernie Thompson3e8f3d62010-02-15 06:46:26 -08001422 }
1423
1424 if (cmd > (char *) urb->transfer_buffer) {
1425 /* Send partial buffer remaining before exiting */
1426 int len = cmd - (char *) urb->transfer_buffer;
1427 dlfb_submit_urb(dev, urb, len);
1428 bytes_sent += len;
1429 } else
1430 dlfb_urb_completion(urb);
1431
Bernie Thompson3e8f3d62010-02-15 06:46:26 -08001432 atomic_add(bytes_sent, &dev->bytes_sent);
1433 atomic_add(bytes_identical, &dev->bytes_identical);
1434 atomic_add(bytes_rendered, &dev->bytes_rendered);
1435 end_cycles = get_cycles();
1436 atomic_add(((unsigned int) ((end_cycles - start_cycles)
1437 >> 10)), /* Kcycles */
1438 &dev->cpu_kcycles_used);
1439}
1440
1441static struct fb_deferred_io dlfb_defio = {
1442 .delay = 5,
1443 .deferred_io = dlfb_dpy_deferred_io,
1444};
1445
1446#endif
1447
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001448/*
Bernie Thompsoncc403dc2010-02-15 06:45:49 -08001449 * This is necessary before we can communicate with the display controller.
1450 */
1451static int dlfb_select_std_channel(struct dlfb_data *dev)
1452{
1453 int ret;
1454 u8 set_def_chn[] = { 0x57, 0xCD, 0xDC, 0xA7,
1455 0x1C, 0x88, 0x5E, 0x15,
1456 0x60, 0xFE, 0xC6, 0x97,
1457 0x16, 0x3D, 0x47, 0xF2 };
1458
1459 ret = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
1460 NR_USB_REQUEST_CHANNEL,
1461 (USB_DIR_OUT | USB_TYPE_VENDOR), 0, 0,
1462 set_def_chn, sizeof(set_def_chn), USB_CTRL_SET_TIMEOUT);
1463 return ret;
1464}
1465
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001466static int dlfb_parse_vendor_descriptor(struct dlfb_data *dev,
1467 struct usb_device *usbdev)
1468{
1469 char *desc;
1470 char *buf;
1471 char *desc_end;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001472
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001473 u8 total_len = 0;
1474
1475 buf = kzalloc(MAX_VENDOR_DESCRIPTOR_SIZE, GFP_KERNEL);
1476 if (!buf)
1477 return false;
1478 desc = buf;
1479
1480 total_len = usb_get_descriptor(usbdev, 0x5f, /* vendor specific */
1481 0, desc, MAX_VENDOR_DESCRIPTOR_SIZE);
1482 if (total_len > 5) {
1483 dl_info("vendor descriptor length:%x data:%02x %02x %02x %02x" \
1484 "%02x %02x %02x %02x %02x %02x %02x\n",
1485 total_len, desc[0],
1486 desc[1], desc[2], desc[3], desc[4], desc[5], desc[6],
1487 desc[7], desc[8], desc[9], desc[10]);
1488
1489 if ((desc[0] != total_len) || /* descriptor length */
1490 (desc[1] != 0x5f) || /* vendor descriptor type */
1491 (desc[2] != 0x01) || /* version (2 bytes) */
1492 (desc[3] != 0x00) ||
1493 (desc[4] != total_len - 2)) /* length after type */
1494 goto unrecognized;
1495
1496 desc_end = desc + total_len;
1497 desc += 5; /* the fixed header we've already parsed */
1498
1499 while (desc < desc_end) {
1500 u8 length;
1501 u16 key;
1502
1503 key = *((u16 *) desc);
1504 desc += sizeof(u16);
1505 length = *desc;
1506 desc++;
1507
1508 switch (key) {
1509 case 0x0200: { /* max_area */
1510 u32 max_area;
1511 max_area = le32_to_cpu(*((u32 *)desc));
1512 dl_warn("DL chip limited to %d pixel modes\n",
1513 max_area);
1514 dev->sku_pixel_limit = max_area;
1515 break;
1516 }
1517 default:
1518 break;
1519 }
1520 desc += length;
1521 }
1522 }
1523
1524 goto success;
1525
1526unrecognized:
1527 /* allow udlfb to load for now even if firmware unrecognized */
1528 dl_err("Unrecognized vendor firmware descriptor\n");
1529
1530success:
1531 kfree(buf);
1532 return true;
1533}
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001534static int dlfb_usb_probe(struct usb_interface *interface,
Bernie Thompson59277b62009-11-24 15:52:21 -08001535 const struct usb_device_id *id)
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001536{
Bernie Thompson59277b62009-11-24 15:52:21 -08001537 struct usb_device *usbdev;
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001538 struct dlfb_data *dev = 0;
Bernie Thompson33077b82010-09-05 16:35:19 -07001539 struct fb_info *info = 0;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001540 int i;
Bernie Thompson59277b62009-11-24 15:52:21 -08001541 int retval = -ENOMEM;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001542
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001543 /* usb initialization */
1544
1545 usbdev = interface_to_usbdev(interface);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001546
Bernie Thompson59277b62009-11-24 15:52:21 -08001547 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1548 if (dev == NULL) {
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001549 err("dlfb_usb_probe: failed alloc of dev struct\n");
1550 goto error;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001551 }
1552
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001553 /* we need to wait for both usb and fbdev to spin down on disconnect */
1554 kref_init(&dev->kref); /* matching kref_put in usb .disconnect fn */
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001555 kref_get(&dev->kref); /* matching kref_put in free_framebuffer_work */
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001556
Bernie Thompson59277b62009-11-24 15:52:21 -08001557 dev->udev = usbdev;
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001558 dev->gdev = &usbdev->dev; /* our generic struct device * */
Bernie Thompson59277b62009-11-24 15:52:21 -08001559 usb_set_intfdata(interface, dev);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001560
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001561 dl_info("%s %s - serial #%s\n",
1562 usbdev->manufacturer, usbdev->product, usbdev->serial);
1563 dl_info("vid_%04x&pid_%04x&rev_%04x driver's dlfb_data struct at %p\n",
1564 usbdev->descriptor.idVendor, usbdev->descriptor.idProduct,
1565 usbdev->descriptor.bcdDevice, dev);
1566
1567 dev->sku_pixel_limit = 2048 * 1152; /* default to maximum */
1568
1569 if (!dlfb_parse_vendor_descriptor(dev, usbdev)) {
1570 dl_err("firmware not recognized. Assume incompatible device\n");
1571 goto error;
1572 }
1573
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001574 if (!dlfb_alloc_urb_list(dev, WRITES_IN_FLIGHT, MAX_TRANSFER)) {
1575 retval = -ENOMEM;
1576 dl_err("dlfb_alloc_urb_list failed\n");
1577 goto error;
1578 }
1579
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001580 /* We don't register a new USB class. Our client interface is fbdev */
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001581
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001582 /* allocates framebuffer driver structure, not framebuffer memory */
1583 info = framebuffer_alloc(0, &usbdev->dev);
1584 if (!info) {
1585 retval = -ENOMEM;
1586 dl_err("framebuffer_alloc failed\n");
1587 goto error;
1588 }
Bernie Thompson33077b82010-09-05 16:35:19 -07001589
Bernie Thompson59277b62009-11-24 15:52:21 -08001590 dev->info = info;
1591 info->par = dev;
1592 info->pseudo_palette = dev->pseudo_palette;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001593 info->fbops = &dlfb_ops;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001594
Bernie Thompson59277b62009-11-24 15:52:21 -08001595 retval = fb_alloc_cmap(&info->cmap, 256, 0);
1596 if (retval < 0) {
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001597 dl_err("fb_alloc_cmap failed %x\n", retval);
1598 goto error;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001599 }
1600
Bernie Thompson33077b82010-09-05 16:35:19 -07001601 INIT_DELAYED_WORK(&dev->free_framebuffer_work,
1602 dlfb_free_framebuffer_work);
1603
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001604 INIT_LIST_HEAD(&info->modelist);
1605
1606 retval = dlfb_setup_modes(dev, info, NULL, 0);
1607 if (retval != 0) {
1608 dl_err("unable to find common mode for display and adapter\n");
1609 goto error;
1610 }
1611
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001612 /* ready to begin using device */
1613
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001614#ifdef CONFIG_FB_DEFERRED_IO
1615 atomic_set(&dev->use_defio, 1);
1616#endif
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001617 atomic_set(&dev->usb_active, 1);
Bernie Thompson59277b62009-11-24 15:52:21 -08001618 dlfb_select_std_channel(dev);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001619
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001620 dlfb_ops_check_var(&info->var, info);
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001621 dlfb_ops_set_par(info);
1622
Bernie Thompson59277b62009-11-24 15:52:21 -08001623 retval = register_framebuffer(info);
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001624 if (retval < 0) {
1625 dl_err("register_framebuffer failed %d\n", retval);
1626 goto error;
1627 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001628
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001629 for (i = 0; i < ARRAY_SIZE(fb_device_attrs); i++)
1630 device_create_file(info->dev, &fb_device_attrs[i]);
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -07001631
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001632 device_create_bin_file(info->dev, &edid_attr);
1633
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001634 dl_info("DisplayLink USB device /dev/fb%d attached. %dx%d resolution."
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001635 " Using %dK framebuffer memory\n", info->node,
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001636 info->var.xres, info->var.yres,
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001637 ((dev->backing_buffer) ?
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001638 info->fix.smem_len * 2 : info->fix.smem_len) >> 10);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001639 return 0;
1640
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001641error:
1642 if (dev) {
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001643
Bernie Thompson33077b82010-09-05 16:35:19 -07001644 if (info) {
1645 if (info->cmap.len != 0)
1646 fb_dealloc_cmap(&info->cmap);
1647 if (info->monspecs.modedb)
1648 fb_destroy_modedb(info->monspecs.modedb);
1649 if (info->screen_base)
1650 vfree(info->screen_base);
1651
1652 fb_destroy_modelist(&info->modelist);
1653
1654 framebuffer_release(info);
1655 }
1656
1657 if (dev->backing_buffer)
1658 vfree(dev->backing_buffer);
1659
1660 kref_put(&dev->kref, dlfb_free); /* ref for framebuffer */
1661 kref_put(&dev->kref, dlfb_free); /* last ref from kref_init */
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001662
1663 /* dev has been deallocated. Do not dereference */
1664 }
1665
Bernie Thompson59277b62009-11-24 15:52:21 -08001666 return retval;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001667}
1668
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001669static void dlfb_usb_disconnect(struct usb_interface *interface)
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001670{
Bernie Thompson59277b62009-11-24 15:52:21 -08001671 struct dlfb_data *dev;
1672 struct fb_info *info;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001673 int i;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001674
Bernie Thompson59277b62009-11-24 15:52:21 -08001675 dev = usb_get_intfdata(interface);
Bernie Thompson59277b62009-11-24 15:52:21 -08001676 info = dev->info;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001677
Bernie Thompson33077b82010-09-05 16:35:19 -07001678 dl_info("USB disconnect starting\n");
1679
1680 /* we virtualize until all fb clients release. Then we free */
1681 dev->virtualized = true;
1682
1683 /* When non-active we'll update virtual framebuffer, but no new urbs */
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001684 atomic_set(&dev->usb_active, 0);
1685
Bernie Thompson33077b82010-09-05 16:35:19 -07001686 /* remove udlfb's sysfs interfaces */
1687 for (i = 0; i < ARRAY_SIZE(fb_device_attrs); i++)
1688 device_remove_file(info->dev, &fb_device_attrs[i]);
1689 device_remove_bin_file(info->dev, &edid_attr);
1690
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001691 usb_set_intfdata(interface, NULL);
1692
Bernie Thompson33077b82010-09-05 16:35:19 -07001693 /* if clients still have us open, will be freed on last close */
1694 if (dev->fb_count == 0)
1695 schedule_delayed_work(&dev->free_framebuffer_work, 0);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001696
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001697 /* release reference taken by kref_init in probe() */
Bernie Thompson33077b82010-09-05 16:35:19 -07001698 kref_put(&dev->kref, dlfb_free);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001699
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001700 /* consider dlfb_data freed */
1701
1702 return;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001703}
1704
1705static struct usb_driver dlfb_driver = {
1706 .name = "udlfb",
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001707 .probe = dlfb_usb_probe,
1708 .disconnect = dlfb_usb_disconnect,
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001709 .id_table = id_table,
1710};
1711
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001712static int __init dlfb_module_init(void)
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001713{
1714 int res;
1715
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001716 res = usb_register(&dlfb_driver);
1717 if (res)
1718 err("usb_register failed. Error number %d", res);
1719
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001720 return res;
1721}
1722
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001723static void __exit dlfb_module_exit(void)
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001724{
1725 usb_deregister(&dlfb_driver);
1726}
1727
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001728module_init(dlfb_module_init);
1729module_exit(dlfb_module_exit);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001730
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001731static void dlfb_urb_completion(struct urb *urb)
1732{
1733 struct urb_node *unode = urb->context;
1734 struct dlfb_data *dev = unode->dev;
1735 unsigned long flags;
1736
1737 /* sync/async unlink faults aren't errors */
1738 if (urb->status) {
1739 if (!(urb->status == -ENOENT ||
1740 urb->status == -ECONNRESET ||
1741 urb->status == -ESHUTDOWN)) {
1742 dl_err("%s - nonzero write bulk status received: %d\n",
1743 __func__, urb->status);
1744 atomic_set(&dev->lost_pixels, 1);
1745 }
1746 }
1747
1748 urb->transfer_buffer_length = dev->urbs.size; /* reset to actual */
1749
1750 spin_lock_irqsave(&dev->urbs.lock, flags);
1751 list_add_tail(&unode->entry, &dev->urbs.list);
1752 dev->urbs.available++;
1753 spin_unlock_irqrestore(&dev->urbs.lock, flags);
1754
1755 up(&dev->urbs.limit_sem);
1756}
1757
1758static void dlfb_free_urb_list(struct dlfb_data *dev)
1759{
1760 int count = dev->urbs.count;
1761 struct list_head *node;
1762 struct urb_node *unode;
1763 struct urb *urb;
1764 int ret;
1765 unsigned long flags;
1766
1767 dl_notice("Waiting for completes and freeing all render urbs\n");
1768
1769 /* keep waiting and freeing, until we've got 'em all */
1770 while (count--) {
Bernie Thompson33077b82010-09-05 16:35:19 -07001771
1772 /* Getting interrupted means a leak, but ok at shutdown*/
1773 ret = down_interruptible(&dev->urbs.limit_sem);
1774 if (ret)
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001775 break;
Bernie Thompson33077b82010-09-05 16:35:19 -07001776
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001777 spin_lock_irqsave(&dev->urbs.lock, flags);
1778
1779 node = dev->urbs.list.next; /* have reserved one with sem */
1780 list_del_init(node);
1781
1782 spin_unlock_irqrestore(&dev->urbs.lock, flags);
1783
1784 unode = list_entry(node, struct urb_node, entry);
1785 urb = unode->urb;
1786
1787 /* Free each separately allocated piece */
Greg Kroah-Hartmanc220cc32010-04-29 15:36:29 -07001788 usb_free_coherent(urb->dev, dev->urbs.size,
1789 urb->transfer_buffer, urb->transfer_dma);
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001790 usb_free_urb(urb);
1791 kfree(node);
1792 }
1793
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001794}
1795
1796static int dlfb_alloc_urb_list(struct dlfb_data *dev, int count, size_t size)
1797{
1798 int i = 0;
1799 struct urb *urb;
1800 struct urb_node *unode;
1801 char *buf;
1802
1803 spin_lock_init(&dev->urbs.lock);
1804
1805 dev->urbs.size = size;
1806 INIT_LIST_HEAD(&dev->urbs.list);
1807
1808 while (i < count) {
1809 unode = kzalloc(sizeof(struct urb_node), GFP_KERNEL);
1810 if (!unode)
1811 break;
1812 unode->dev = dev;
1813
1814 urb = usb_alloc_urb(0, GFP_KERNEL);
1815 if (!urb) {
1816 kfree(unode);
1817 break;
1818 }
1819 unode->urb = urb;
1820
Greg Kroah-Hartmanc220cc32010-04-29 15:36:29 -07001821 buf = usb_alloc_coherent(dev->udev, MAX_TRANSFER, GFP_KERNEL,
1822 &urb->transfer_dma);
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001823 if (!buf) {
1824 kfree(unode);
1825 usb_free_urb(urb);
1826 break;
1827 }
1828
1829 /* urb->transfer_buffer_length set to actual before submit */
1830 usb_fill_bulk_urb(urb, dev->udev, usb_sndbulkpipe(dev->udev, 1),
1831 buf, size, dlfb_urb_completion, unode);
1832 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1833
1834 list_add_tail(&unode->entry, &dev->urbs.list);
1835
1836 i++;
1837 }
1838
1839 sema_init(&dev->urbs.limit_sem, i);
1840 dev->urbs.count = i;
1841 dev->urbs.available = i;
1842
Soeren Moellerb5a21042010-05-14 19:03:00 +00001843 dl_notice("allocated %d %d byte urbs\n", i, (int) size);
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001844
1845 return i;
1846}
1847
1848static struct urb *dlfb_get_urb(struct dlfb_data *dev)
1849{
1850 int ret = 0;
1851 struct list_head *entry;
1852 struct urb_node *unode;
1853 struct urb *urb = NULL;
1854 unsigned long flags;
1855
1856 /* Wait for an in-flight buffer to complete and get re-queued */
1857 ret = down_timeout(&dev->urbs.limit_sem, GET_URB_TIMEOUT);
1858 if (ret) {
1859 atomic_set(&dev->lost_pixels, 1);
1860 dl_err("wait for urb interrupted: %x\n", ret);
1861 goto error;
1862 }
1863
1864 spin_lock_irqsave(&dev->urbs.lock, flags);
1865
1866 BUG_ON(list_empty(&dev->urbs.list)); /* reserved one with limit_sem */
1867 entry = dev->urbs.list.next;
1868 list_del_init(entry);
1869 dev->urbs.available--;
1870
1871 spin_unlock_irqrestore(&dev->urbs.lock, flags);
1872
1873 unode = list_entry(entry, struct urb_node, entry);
1874 urb = unode->urb;
1875
1876error:
1877 return urb;
1878}
1879
1880static int dlfb_submit_urb(struct dlfb_data *dev, struct urb *urb, size_t len)
1881{
1882 int ret;
1883
1884 BUG_ON(len > dev->urbs.size);
1885
1886 urb->transfer_buffer_length = len; /* set to actual payload len */
1887 ret = usb_submit_urb(urb, GFP_KERNEL);
1888 if (ret) {
1889 dlfb_urb_completion(urb); /* because no one else will */
1890 atomic_set(&dev->lost_pixels, 1);
1891 dl_err("usb_submit_urb error %x\n", ret);
1892 }
1893 return ret;
1894}
1895
Bernie Thompson59277b62009-11-24 15:52:21 -08001896MODULE_AUTHOR("Roberto De Ioris <roberto@unbit.it>, "
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001897 "Jaya Kumar <jayakumar.lkml@gmail.com>, "
1898 "Bernie Thompson <bernie@plugable.com>");
1899MODULE_DESCRIPTION("DisplayLink kernel framebuffer driver");
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001900MODULE_LICENSE("GPL");
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001901