blob: a2bd5f7d2c50f4d111ef4a8deadd85438d4573ea [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>
Roberto De Ioris88e58b12009-06-03 14:03:06 -070028
29#include "udlfb.h"
30
Bernie Thompson59277b62009-11-24 15:52:21 -080031static struct fb_fix_screeninfo dlfb_fix = {
Bernie Thompson2469d5d2010-02-15 06:46:13 -080032 .id = "udlfb",
Bernie Thompson1d31a9e2010-02-15 06:45:43 -080033 .type = FB_TYPE_PACKED_PIXELS,
34 .visual = FB_VISUAL_TRUECOLOR,
35 .xpanstep = 0,
36 .ypanstep = 0,
37 .ywrapstep = 0,
38 .accel = FB_ACCEL_NONE,
Bernie Thompson59277b62009-11-24 15:52:21 -080039};
Roberto De Ioris88e58b12009-06-03 14:03:06 -070040
Bernie Thompson2469d5d2010-02-15 06:46:13 -080041static const u32 udlfb_info_flags = FBINFO_DEFAULT | FBINFO_READS_FAST |
42#ifdef FBINFO_VIRTFB
43 FBINFO_VIRTFB |
44#endif
45 FBINFO_HWACCEL_IMAGEBLIT | FBINFO_HWACCEL_FILLRECT |
46 FBINFO_HWACCEL_COPYAREA | FBINFO_MISC_ALWAYS_SETPAR;
47
Bernie Thompsoncc403dc2010-02-15 06:45:49 -080048/*
49 * There are many DisplayLink-based products, all with unique PIDs. We are able
50 * to support all volume ones (circa 2009) with a single driver, so we match
51 * globally on VID. TODO: Probe() needs to detect when we might be running
52 * "future" chips, and bail on those, so a compatible driver can match.
53 */
54static struct usb_device_id id_table[] = {
55 {.idVendor = 0x17e9, .match_flags = USB_DEVICE_ID_MATCH_VENDOR,},
56 {},
57};
58MODULE_DEVICE_TABLE(usb, id_table);
Bernie Thompson59277b62009-11-24 15:52:21 -080059
Bernie Thompsondd8015f2010-02-15 06:46:35 -080060#ifndef CONFIG_FB_DEFERRED_IO
Pavel Machek3b7b31f2010-04-03 07:00:37 +020061#warning Please set CONFIG_FB_DEFFERRED_IO option to support generic fbdev apps
Bernie Thompsondd8015f2010-02-15 06:46:35 -080062#endif
63
64#ifndef CONFIG_FB_SYS_IMAGEBLIT
65#ifndef CONFIG_FB_SYS_IMAGEBLIT_MODULE
Pavel Machek3b7b31f2010-04-03 07:00:37 +020066#warning Please set CONFIG_FB_SYS_IMAGEBLIT option to support fb console
Bernie Thompsondd8015f2010-02-15 06:46:35 -080067#endif
68#endif
69
70#ifndef CONFIG_FB_MODE_HELPERS
Pavel Machek3b7b31f2010-04-03 07:00:37 +020071#warning CONFIG_FB_MODE_HELPERS required. Expect build break
Bernie Thompsondd8015f2010-02-15 06:46:35 -080072#endif
73
Bernie Thompson4a4854d2010-02-15 06:45:55 -080074/* dlfb keeps a list of urbs for efficient bulk transfers */
75static void dlfb_urb_completion(struct urb *urb);
76static struct urb *dlfb_get_urb(struct dlfb_data *dev);
77static int dlfb_submit_urb(struct dlfb_data *dev, struct urb * urb, size_t len);
78static int dlfb_alloc_urb_list(struct dlfb_data *dev, int count, size_t size);
79static void dlfb_free_urb_list(struct dlfb_data *dev);
80
Bernie Thompson3e8f3d62010-02-15 06:46:26 -080081/* other symbols with dependents */
82#ifdef CONFIG_FB_DEFERRED_IO
83static struct fb_deferred_io dlfb_defio;
84#endif
85
Bernie Thompson59277b62009-11-24 15:52:21 -080086/*
Bernie Thompsonbd808162010-02-15 06:46:48 -080087 * All DisplayLink bulk operations start with 0xAF, followed by specific code
88 * All operations are written to buffers which then later get sent to device
Bernie Thompson59277b62009-11-24 15:52:21 -080089 */
Bernie Thompson45742032010-02-15 06:46:04 -080090static char *dlfb_set_register(char *buf, u8 reg, u8 val)
Roberto De Ioris88e58b12009-06-03 14:03:06 -070091{
Bernie Thompson1d31a9e2010-02-15 06:45:43 -080092 *buf++ = 0xAF;
93 *buf++ = 0x20;
94 *buf++ = reg;
95 *buf++ = val;
96 return buf;
Roberto De Ioris88e58b12009-06-03 14:03:06 -070097}
98
Bernie Thompson45742032010-02-15 06:46:04 -080099static char *dlfb_vidreg_lock(char *buf)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700100{
Bernie Thompson45742032010-02-15 06:46:04 -0800101 return dlfb_set_register(buf, 0xFF, 0x00);
Bernie Thompson59277b62009-11-24 15:52:21 -0800102}
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700103
Bernie Thompson45742032010-02-15 06:46:04 -0800104static char *dlfb_vidreg_unlock(char *buf)
Bernie Thompson59277b62009-11-24 15:52:21 -0800105{
Bernie Thompson45742032010-02-15 06:46:04 -0800106 return dlfb_set_register(buf, 0xFF, 0xFF);
Bernie Thompson59277b62009-11-24 15:52:21 -0800107}
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700108
Bernie Thompson59277b62009-11-24 15:52:21 -0800109/*
Bernie Thompson530f43a2010-02-15 06:46:21 -0800110 * On/Off for driving the DisplayLink framebuffer to the display
Bernie Thompson59277b62009-11-24 15:52:21 -0800111 */
Bernie Thompson530f43a2010-02-15 06:46:21 -0800112static char *dlfb_enable_hvsync(char *buf, bool enable)
Bernie Thompson59277b62009-11-24 15:52:21 -0800113{
Bernie Thompson530f43a2010-02-15 06:46:21 -0800114 if (enable)
115 return dlfb_set_register(buf, 0x1F, 0x00);
116 else
117 return dlfb_set_register(buf, 0x1F, 0x01);
Bernie Thompson59277b62009-11-24 15:52:21 -0800118}
119
Bernie Thompson45742032010-02-15 06:46:04 -0800120static char *dlfb_set_color_depth(char *buf, u8 selection)
Bernie Thompson59277b62009-11-24 15:52:21 -0800121{
Bernie Thompson45742032010-02-15 06:46:04 -0800122 return dlfb_set_register(buf, 0x00, selection);
Bernie Thompson59277b62009-11-24 15:52:21 -0800123}
124
Bernie Thompson45742032010-02-15 06:46:04 -0800125static char *dlfb_set_base16bpp(char *wrptr, u32 base)
Bernie Thompson59277b62009-11-24 15:52:21 -0800126{
Bernie Thompson1d31a9e2010-02-15 06:45:43 -0800127 /* the base pointer is 16 bits wide, 0x20 is hi byte. */
Bernie Thompson45742032010-02-15 06:46:04 -0800128 wrptr = dlfb_set_register(wrptr, 0x20, base >> 16);
129 wrptr = dlfb_set_register(wrptr, 0x21, base >> 8);
130 return dlfb_set_register(wrptr, 0x22, base);
Bernie Thompson59277b62009-11-24 15:52:21 -0800131}
132
Bernie Thompsonbd808162010-02-15 06:46:48 -0800133/*
134 * DisplayLink HW has separate 16bpp and 8bpp framebuffers.
135 * In 24bpp modes, the low 323 RGB bits go in the 8bpp framebuffer
136 */
Bernie Thompson45742032010-02-15 06:46:04 -0800137static char *dlfb_set_base8bpp(char *wrptr, u32 base)
Bernie Thompson59277b62009-11-24 15:52:21 -0800138{
Bernie Thompson45742032010-02-15 06:46:04 -0800139 wrptr = dlfb_set_register(wrptr, 0x26, base >> 16);
140 wrptr = dlfb_set_register(wrptr, 0x27, base >> 8);
141 return dlfb_set_register(wrptr, 0x28, base);
Bernie Thompson59277b62009-11-24 15:52:21 -0800142}
143
Bernie Thompson45742032010-02-15 06:46:04 -0800144static char *dlfb_set_register_16(char *wrptr, u8 reg, u16 value)
Bernie Thompson59277b62009-11-24 15:52:21 -0800145{
Bernie Thompson45742032010-02-15 06:46:04 -0800146 wrptr = dlfb_set_register(wrptr, reg, value >> 8);
147 return dlfb_set_register(wrptr, reg+1, value);
Bernie Thompson59277b62009-11-24 15:52:21 -0800148}
149
150/*
151 * This is kind of weird because the controller takes some
152 * register values in a different byte order than other registers.
153 */
Bernie Thompson45742032010-02-15 06:46:04 -0800154static char *dlfb_set_register_16be(char *wrptr, u8 reg, u16 value)
Bernie Thompson59277b62009-11-24 15:52:21 -0800155{
Bernie Thompson45742032010-02-15 06:46:04 -0800156 wrptr = dlfb_set_register(wrptr, reg, value);
157 return dlfb_set_register(wrptr, reg+1, value >> 8);
Bernie Thompson59277b62009-11-24 15:52:21 -0800158}
159
160/*
161 * LFSR is linear feedback shift register. The reason we have this is
162 * because the display controller needs to minimize the clock depth of
163 * various counters used in the display path. So this code reverses the
164 * provided value into the lfsr16 value by counting backwards to get
165 * the value that needs to be set in the hardware comparator to get the
166 * same actual count. This makes sense once you read above a couple of
167 * times and think about it from a hardware perspective.
168 */
Bernie Thompsonbd808162010-02-15 06:46:48 -0800169static u16 dlfb_lfsr16(u16 actual_count)
Bernie Thompson59277b62009-11-24 15:52:21 -0800170{
171 u32 lv = 0xFFFF; /* This is the lfsr value that the hw starts with */
172
173 while (actual_count--) {
174 lv = ((lv << 1) |
175 (((lv >> 15) ^ (lv >> 4) ^ (lv >> 2) ^ (lv >> 1)) & 1))
176 & 0xFFFF;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700177 }
Bernie Thompson59277b62009-11-24 15:52:21 -0800178
179 return (u16) lv;
180}
181
182/*
183 * This does LFSR conversion on the value that is to be written.
184 * See LFSR explanation above for more detail.
185 */
Bernie Thompson45742032010-02-15 06:46:04 -0800186static char *dlfb_set_register_lfsr16(char *wrptr, u8 reg, u16 value)
Bernie Thompson59277b62009-11-24 15:52:21 -0800187{
Bernie Thompsonbd808162010-02-15 06:46:48 -0800188 return dlfb_set_register_16(wrptr, reg, dlfb_lfsr16(value));
Bernie Thompson59277b62009-11-24 15:52:21 -0800189}
190
191/*
192 * This takes a standard fbdev screeninfo struct and all of its monitor mode
193 * details and converts them into the DisplayLink equivalent register commands.
194 */
Bernie Thompson45742032010-02-15 06:46:04 -0800195static char *dlfb_set_vid_cmds(char *wrptr, struct fb_var_screeninfo *var)
Bernie Thompson59277b62009-11-24 15:52:21 -0800196{
197 u16 xds, yds;
198 u16 xde, yde;
199 u16 yec;
200
Bernie Thompson59277b62009-11-24 15:52:21 -0800201 /* x display start */
202 xds = var->left_margin + var->hsync_len;
Bernie Thompson45742032010-02-15 06:46:04 -0800203 wrptr = dlfb_set_register_lfsr16(wrptr, 0x01, xds);
Bernie Thompson59277b62009-11-24 15:52:21 -0800204 /* x display end */
205 xde = xds + var->xres;
Bernie Thompson45742032010-02-15 06:46:04 -0800206 wrptr = dlfb_set_register_lfsr16(wrptr, 0x03, xde);
Bernie Thompson59277b62009-11-24 15:52:21 -0800207
208 /* y display start */
209 yds = var->upper_margin + var->vsync_len;
Bernie Thompson45742032010-02-15 06:46:04 -0800210 wrptr = dlfb_set_register_lfsr16(wrptr, 0x05, yds);
Bernie Thompson59277b62009-11-24 15:52:21 -0800211 /* y display end */
212 yde = yds + var->yres;
Bernie Thompson45742032010-02-15 06:46:04 -0800213 wrptr = dlfb_set_register_lfsr16(wrptr, 0x07, yde);
Bernie Thompson59277b62009-11-24 15:52:21 -0800214
215 /* x end count is active + blanking - 1 */
Bernie Thompson45742032010-02-15 06:46:04 -0800216 wrptr = dlfb_set_register_lfsr16(wrptr, 0x09,
217 xde + var->right_margin - 1);
Bernie Thompson59277b62009-11-24 15:52:21 -0800218
219 /* libdlo hardcodes hsync start to 1 */
Bernie Thompson45742032010-02-15 06:46:04 -0800220 wrptr = dlfb_set_register_lfsr16(wrptr, 0x0B, 1);
Bernie Thompson59277b62009-11-24 15:52:21 -0800221
222 /* hsync end is width of sync pulse + 1 */
Bernie Thompson45742032010-02-15 06:46:04 -0800223 wrptr = dlfb_set_register_lfsr16(wrptr, 0x0D, var->hsync_len + 1);
Bernie Thompson59277b62009-11-24 15:52:21 -0800224
225 /* hpixels is active pixels */
Bernie Thompson45742032010-02-15 06:46:04 -0800226 wrptr = dlfb_set_register_16(wrptr, 0x0F, var->xres);
Bernie Thompson59277b62009-11-24 15:52:21 -0800227
228 /* yendcount is vertical active + vertical blanking */
229 yec = var->yres + var->upper_margin + var->lower_margin +
230 var->vsync_len;
Bernie Thompson45742032010-02-15 06:46:04 -0800231 wrptr = dlfb_set_register_lfsr16(wrptr, 0x11, yec);
Bernie Thompson59277b62009-11-24 15:52:21 -0800232
233 /* libdlo hardcodes vsync start to 0 */
Bernie Thompson45742032010-02-15 06:46:04 -0800234 wrptr = dlfb_set_register_lfsr16(wrptr, 0x13, 0);
Bernie Thompson59277b62009-11-24 15:52:21 -0800235
236 /* vsync end is width of vsync pulse */
Bernie Thompson45742032010-02-15 06:46:04 -0800237 wrptr = dlfb_set_register_lfsr16(wrptr, 0x15, var->vsync_len);
Bernie Thompson59277b62009-11-24 15:52:21 -0800238
239 /* vpixels is active pixels */
Bernie Thompson45742032010-02-15 06:46:04 -0800240 wrptr = dlfb_set_register_16(wrptr, 0x17, var->yres);
Bernie Thompson59277b62009-11-24 15:52:21 -0800241
242 /* convert picoseconds to 5kHz multiple for pclk5k = x * 1E12/5k */
Bernie Thompson45742032010-02-15 06:46:04 -0800243 wrptr = dlfb_set_register_16be(wrptr, 0x1B,
244 200*1000*1000/var->pixclock);
Bernie Thompson59277b62009-11-24 15:52:21 -0800245
246 return wrptr;
247}
248
249/*
250 * This takes a standard fbdev screeninfo struct that was fetched or prepared
251 * and then generates the appropriate command sequence that then drives the
252 * display controller.
253 */
254static int dlfb_set_video_mode(struct dlfb_data *dev,
255 struct fb_var_screeninfo *var)
256{
257 char *buf;
258 char *wrptr;
259 int retval = 0;
260 int writesize;
Bernie Thompson530f43a2010-02-15 06:46:21 -0800261 struct urb *urb;
Bernie Thompson59277b62009-11-24 15:52:21 -0800262
Bernie Thompson530f43a2010-02-15 06:46:21 -0800263 if (!atomic_read(&dev->usb_active))
264 return -EPERM;
265
266 urb = dlfb_get_urb(dev);
267 if (!urb)
268 return -ENOMEM;
269 buf = (char *) urb->transfer_buffer;
Bernie Thompson59277b62009-11-24 15:52:21 -0800270
271 /*
272 * This first section has to do with setting the base address on the
273 * controller * associated with the display. There are 2 base
274 * pointers, currently, we only * use the 16 bpp segment.
275 */
Bernie Thompson45742032010-02-15 06:46:04 -0800276 wrptr = dlfb_vidreg_lock(buf);
277 wrptr = dlfb_set_color_depth(wrptr, 0x00);
Bernie Thompson59277b62009-11-24 15:52:21 -0800278 /* set base for 16bpp segment to 0 */
Bernie Thompson45742032010-02-15 06:46:04 -0800279 wrptr = dlfb_set_base16bpp(wrptr, 0);
Bernie Thompson59277b62009-11-24 15:52:21 -0800280 /* set base for 8bpp segment to end of fb */
Bernie Thompson45742032010-02-15 06:46:04 -0800281 wrptr = dlfb_set_base8bpp(wrptr, dev->info->fix.smem_len);
Bernie Thompson59277b62009-11-24 15:52:21 -0800282
Bernie Thompson45742032010-02-15 06:46:04 -0800283 wrptr = dlfb_set_vid_cmds(wrptr, var);
Bernie Thompson530f43a2010-02-15 06:46:21 -0800284 wrptr = dlfb_enable_hvsync(wrptr, true);
Bernie Thompson45742032010-02-15 06:46:04 -0800285 wrptr = dlfb_vidreg_unlock(wrptr);
Bernie Thompson59277b62009-11-24 15:52:21 -0800286
287 writesize = wrptr - buf;
288
Bernie Thompson530f43a2010-02-15 06:46:21 -0800289 retval = dlfb_submit_urb(dev, urb, writesize);
Bernie Thompson59277b62009-11-24 15:52:21 -0800290
Bernie Thompson59277b62009-11-24 15:52:21 -0800291 return retval;
292}
293
Bernie Thompson45742032010-02-15 06:46:04 -0800294static int dlfb_ops_mmap(struct fb_info *info, struct vm_area_struct *vma)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700295{
296 unsigned long start = vma->vm_start;
297 unsigned long size = vma->vm_end - vma->vm_start;
298 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
299 unsigned long page, pos;
Bernie Thompsonbd808162010-02-15 06:46:48 -0800300 struct dlfb_data *dev = info->par;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700301
Bernie Thompsonbd808162010-02-15 06:46:48 -0800302 dl_notice("MMAP: %lu %u\n", offset + size, info->fix.smem_len);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700303
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700304 if (offset + size > info->fix.smem_len)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700305 return -EINVAL;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700306
307 pos = (unsigned long)info->fix.smem_start + offset;
308
309 while (size > 0) {
310 page = vmalloc_to_pfn((void *)pos);
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700311 if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED))
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700312 return -EAGAIN;
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700313
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700314 start += PAGE_SIZE;
315 pos += PAGE_SIZE;
316 if (size > PAGE_SIZE)
317 size -= PAGE_SIZE;
318 else
319 size = 0;
320 }
321
322 vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
323 return 0;
324
325}
326
Bernie Thompson530f43a2010-02-15 06:46:21 -0800327/*
328 * Trims identical data from front and back of line
329 * Sets new front buffer address and width
330 * And returns byte count of identical pixels
331 * Assumes CPU natural alignment (unsigned long)
332 * for back and front buffer ptrs and width
333 */
334static int dlfb_trim_hline(const u8 *bback, const u8 **bfront, int *width_bytes)
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700335{
Bernie Thompson530f43a2010-02-15 06:46:21 -0800336 int j, k;
337 const unsigned long *back = (const unsigned long *) bback;
338 const unsigned long *front = (const unsigned long *) *bfront;
339 const int width = *width_bytes / sizeof(unsigned long);
340 int identical = width;
341 int start = width;
342 int end = width;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700343
Bernie Thompson530f43a2010-02-15 06:46:21 -0800344 prefetch((void *) front);
345 prefetch((void *) back);
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700346
Bernie Thompson530f43a2010-02-15 06:46:21 -0800347 for (j = 0; j < width; j++) {
348 if (back[j] != front[j]) {
349 start = j;
350 break;
351 }
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700352 }
353
Bernie Thompson530f43a2010-02-15 06:46:21 -0800354 for (k = width - 1; k > j; k--) {
355 if (back[k] != front[k]) {
356 end = k+1;
357 break;
358 }
359 }
360
361 identical = start + (width - end);
362 *bfront = (u8 *) &front[start];
363 *width_bytes = (end - start) * sizeof(unsigned long);
364
365 return identical * sizeof(unsigned long);
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700366}
367
368/*
Pavel Machek3b7b31f2010-04-03 07:00:37 +0200369 * Render a command stream for an encoded horizontal line segment of pixels.
370 *
371 * A command buffer holds several commands.
372 * It always begins with a fresh command header
373 * (the protocol doesn't require this, but we enforce it to allow
374 * multiple buffers to be potentially encoded and sent in parallel).
375 * A single command encodes one contiguous horizontal line of pixels
376 *
377 * The function relies on the client to do all allocation, so that
378 * rendering can be done directly to output buffers (e.g. USB URBs).
379 * The function fills the supplied command buffer, providing information
380 * on where it left off, so the client may call in again with additional
381 * buffers if the line will take several buffers to complete.
382 *
383 * A single command can transmit a maximum of 256 pixels,
384 * regardless of the compression ratio (protocol design limit).
385 * To the hardware, 0 for a size byte means 256
386 *
387 * Rather than 256 pixel commands which are either rl or raw encoded,
388 * the rlx command simply assumes alternating raw and rl spans within one cmd.
389 * This has a slightly larger header overhead, but produces more even results.
390 * It also processes all data (read and write) in a single pass.
391 * Performance benchmarks of common cases show it having just slightly better
392 * compression than 256 pixel raw -or- rle commands, with similar CPU consumpion.
393 * But for very rl friendly data, will compress not quite as well.
394 */
Bernie Thompson530f43a2010-02-15 06:46:21 -0800395static void dlfb_compress_hline(
396 const uint16_t **pixel_start_ptr,
397 const uint16_t *const pixel_end,
398 uint32_t *device_address_ptr,
399 uint8_t **command_buffer_ptr,
400 const uint8_t *const cmd_buffer_end)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700401{
Bernie Thompson530f43a2010-02-15 06:46:21 -0800402 const uint16_t *pixel = *pixel_start_ptr;
403 uint32_t dev_addr = *device_address_ptr;
404 uint8_t *cmd = *command_buffer_ptr;
405 const int bpp = 2;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700406
Bernie Thompson530f43a2010-02-15 06:46:21 -0800407 while ((pixel_end > pixel) &&
408 (cmd_buffer_end - MIN_RLX_CMD_BYTES > cmd)) {
409 uint8_t *raw_pixels_count_byte = 0;
410 uint8_t *cmd_pixels_count_byte = 0;
411 const uint16_t *raw_pixel_start = 0;
412 const uint16_t *cmd_pixel_start, *cmd_pixel_end = 0;
413 const uint32_t be_dev_addr = cpu_to_be32(dev_addr);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700414
Bernie Thompson530f43a2010-02-15 06:46:21 -0800415 prefetchw((void *) cmd); /* pull in one cache line at least */
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700416
Bernie Thompson530f43a2010-02-15 06:46:21 -0800417 *cmd++ = 0xAF;
418 *cmd++ = 0x6B;
419 *cmd++ = (uint8_t) ((be_dev_addr >> 8) & 0xFF);
420 *cmd++ = (uint8_t) ((be_dev_addr >> 16) & 0xFF);
421 *cmd++ = (uint8_t) ((be_dev_addr >> 24) & 0xFF);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700422
Bernie Thompson530f43a2010-02-15 06:46:21 -0800423 cmd_pixels_count_byte = cmd++; /* we'll know this later */
424 cmd_pixel_start = pixel;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700425
Bernie Thompson530f43a2010-02-15 06:46:21 -0800426 raw_pixels_count_byte = cmd++; /* we'll know this later */
427 raw_pixel_start = pixel;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700428
Bernie Thompson530f43a2010-02-15 06:46:21 -0800429 cmd_pixel_end = pixel + min(MAX_CMD_PIXELS + 1,
430 min((int)(pixel_end - pixel),
431 (int)(cmd_buffer_end - cmd) / bpp));
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700432
Bernie Thompson530f43a2010-02-15 06:46:21 -0800433 prefetch_range((void *) pixel, (cmd_pixel_end - pixel) * bpp);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700434
Bernie Thompson530f43a2010-02-15 06:46:21 -0800435 while (pixel < cmd_pixel_end) {
436 const uint16_t * const repeating_pixel = pixel;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700437
Bernie Thompson530f43a2010-02-15 06:46:21 -0800438 *(uint16_t *)cmd = cpu_to_be16p(pixel);
439 cmd += 2;
440 pixel++;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700441
Bernie Thompson530f43a2010-02-15 06:46:21 -0800442 if (unlikely((pixel < cmd_pixel_end) &&
443 (*pixel == *repeating_pixel))) {
444 /* go back and fill in raw pixel count */
445 *raw_pixels_count_byte = ((repeating_pixel -
446 raw_pixel_start) + 1) & 0xFF;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700447
Bernie Thompson530f43a2010-02-15 06:46:21 -0800448 while ((pixel < cmd_pixel_end)
449 && (*pixel == *repeating_pixel)) {
450 pixel++;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700451 }
Bernie Thompson59277b62009-11-24 15:52:21 -0800452
Bernie Thompson530f43a2010-02-15 06:46:21 -0800453 /* immediately after raw data is repeat byte */
454 *cmd++ = ((pixel - repeating_pixel) - 1) & 0xFF;
Bernie Thompson59277b62009-11-24 15:52:21 -0800455
Bernie Thompson530f43a2010-02-15 06:46:21 -0800456 /* Then start another raw pixel span */
457 raw_pixel_start = pixel;
458 raw_pixels_count_byte = cmd++;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700459 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700460 }
461
Bernie Thompson530f43a2010-02-15 06:46:21 -0800462 if (pixel > raw_pixel_start) {
463 /* finalize last RAW span */
464 *raw_pixels_count_byte = (pixel-raw_pixel_start) & 0xFF;
465 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700466
Bernie Thompson530f43a2010-02-15 06:46:21 -0800467 *cmd_pixels_count_byte = (pixel - cmd_pixel_start) & 0xFF;
468 dev_addr += (pixel - cmd_pixel_start) * bpp;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700469 }
470
Bernie Thompson530f43a2010-02-15 06:46:21 -0800471 if (cmd_buffer_end <= MIN_RLX_CMD_BYTES + cmd) {
472 /* Fill leftover bytes with no-ops */
473 if (cmd_buffer_end > cmd)
474 memset(cmd, 0xAF, cmd_buffer_end - cmd);
475 cmd = (uint8_t *) cmd_buffer_end;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700476 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700477
Bernie Thompson530f43a2010-02-15 06:46:21 -0800478 *command_buffer_ptr = cmd;
479 *pixel_start_ptr = pixel;
480 *device_address_ptr = dev_addr;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700481
Bernie Thompson530f43a2010-02-15 06:46:21 -0800482 return;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700483}
484
Bernie Thompson530f43a2010-02-15 06:46:21 -0800485/*
486 * There are 3 copies of every pixel: The front buffer that the fbdev
487 * client renders to, the actual framebuffer across the USB bus in hardware
488 * (that we can only write to, slowly, and can never read), and (optionally)
489 * our shadow copy that tracks what's been sent to that hardware buffer.
490 */
491static void dlfb_render_hline(struct dlfb_data *dev, struct urb **urb_ptr,
492 const char *front, char **urb_buf_ptr,
493 u32 byte_offset, u32 byte_width,
494 int *ident_ptr, int *sent_ptr)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700495{
Bernie Thompson530f43a2010-02-15 06:46:21 -0800496 const u8 *line_start, *line_end, *next_pixel;
497 u32 dev_addr = dev->base16 + byte_offset;
498 struct urb *urb = *urb_ptr;
499 u8 *cmd = *urb_buf_ptr;
500 u8 *cmd_end = (u8 *) urb->transfer_buffer + urb->transfer_buffer_length;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700501
Bernie Thompson530f43a2010-02-15 06:46:21 -0800502 line_start = (u8 *) (front + byte_offset);
503 next_pixel = line_start;
504 line_end = next_pixel + byte_width;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700505
Bernie Thompson530f43a2010-02-15 06:46:21 -0800506 if (dev->backing_buffer) {
507 int offset;
508 const u8 *back_start = (u8 *) (dev->backing_buffer
509 + byte_offset);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700510
Bernie Thompson530f43a2010-02-15 06:46:21 -0800511 *ident_ptr += dlfb_trim_hline(back_start, &next_pixel,
512 &byte_width);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700513
Bernie Thompson530f43a2010-02-15 06:46:21 -0800514 offset = next_pixel - line_start;
515 line_end = next_pixel + byte_width;
516 dev_addr += offset;
517 back_start += offset;
518 line_start += offset;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700519
Bernie Thompson530f43a2010-02-15 06:46:21 -0800520 memcpy((char *)back_start, (char *) line_start,
521 byte_width);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700522 }
523
Bernie Thompson530f43a2010-02-15 06:46:21 -0800524 while (next_pixel < line_end) {
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700525
Bernie Thompson530f43a2010-02-15 06:46:21 -0800526 dlfb_compress_hline((const uint16_t **) &next_pixel,
527 (const uint16_t *) line_end, &dev_addr,
528 (u8 **) &cmd, (u8 *) cmd_end);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700529
Bernie Thompson530f43a2010-02-15 06:46:21 -0800530 if (cmd >= cmd_end) {
531 int len = cmd - (u8 *) urb->transfer_buffer;
532 if (dlfb_submit_urb(dev, urb, len))
533 return; /* lost pixels is set */
534 *sent_ptr += len;
535 urb = dlfb_get_urb(dev);
536 if (!urb)
537 return; /* lost_pixels is set */
538 *urb_ptr = urb;
539 cmd = urb->transfer_buffer;
540 cmd_end = &cmd[urb->transfer_buffer_length];
541 }
542 }
543
544 *urb_buf_ptr = cmd;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700545}
546
Bernie Thompson530f43a2010-02-15 06:46:21 -0800547int dlfb_handle_damage(struct dlfb_data *dev, int x, int y,
548 int width, int height, char *data)
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700549{
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700550 int i, ret;
Bernie Thompson530f43a2010-02-15 06:46:21 -0800551 char *cmd;
552 cycles_t start_cycles, end_cycles;
553 int bytes_sent = 0;
554 int bytes_identical = 0;
555 struct urb *urb;
556 int aligned_x;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700557
Bernie Thompson530f43a2010-02-15 06:46:21 -0800558 start_cycles = get_cycles();
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700559
Bernie Thompson530f43a2010-02-15 06:46:21 -0800560 aligned_x = DL_ALIGN_DOWN(x, sizeof(unsigned long));
561 width = DL_ALIGN_UP(width + (x-aligned_x), sizeof(unsigned long));
562 x = aligned_x;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700563
Bernie Thompson530f43a2010-02-15 06:46:21 -0800564 if ((width <= 0) ||
565 (x + width > dev->info->var.xres) ||
566 (y + height > dev->info->var.yres))
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700567 return -EINVAL;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700568
Bernie Thompson530f43a2010-02-15 06:46:21 -0800569 if (!atomic_read(&dev->usb_active))
570 return 0;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700571
Bernie Thompson530f43a2010-02-15 06:46:21 -0800572 urb = dlfb_get_urb(dev);
573 if (!urb)
574 return 0;
575 cmd = urb->transfer_buffer;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700576
Bernie Thompson530f43a2010-02-15 06:46:21 -0800577 for (i = y; i < y + height ; i++) {
578 const int line_offset = dev->info->fix.line_length * i;
579 const int byte_offset = line_offset + (x * BPP);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700580
Bernie Thompson530f43a2010-02-15 06:46:21 -0800581 dlfb_render_hline(dev, &urb, (char *) dev->info->fix.smem_start,
582 &cmd, byte_offset, width * BPP,
583 &bytes_identical, &bytes_sent);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700584 }
585
Bernie Thompson530f43a2010-02-15 06:46:21 -0800586 if (cmd > (char *) urb->transfer_buffer) {
587 /* Send partial buffer remaining before exiting */
588 int len = cmd - (char *) urb->transfer_buffer;
589 ret = dlfb_submit_urb(dev, urb, len);
590 bytes_sent += len;
591 } else
592 dlfb_urb_completion(urb);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700593
Bernie Thompson530f43a2010-02-15 06:46:21 -0800594 atomic_add(bytes_sent, &dev->bytes_sent);
595 atomic_add(bytes_identical, &dev->bytes_identical);
596 atomic_add(width*height*2, &dev->bytes_rendered);
597 end_cycles = get_cycles();
598 atomic_add(((unsigned int) ((end_cycles - start_cycles)
599 >> 10)), /* Kcycles */
600 &dev->cpu_kcycles_used);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700601
Bernie Thompson530f43a2010-02-15 06:46:21 -0800602 return 0;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700603}
604
Bernie Thompsond46ecb92010-09-05 16:35:04 -0700605static ssize_t dlfb_ops_read(struct fb_info *info, char __user *buf,
606 size_t count, loff_t *ppos)
607{
608 ssize_t result = -ENOSYS;
609
610#if defined CONFIG_FB_SYS_FOPS || defined CONFIG_FB_SYS_FOPS_MODULE
611 result = fb_sys_read(info, buf, count, ppos);
612#endif
613
614 return result;
615}
616
617/*
618 * Path triggered by usermode clients who write to filesystem
619 * e.g. cat filename > /dev/fb1
620 * Not used by X Windows or text-mode console. But useful for testing.
621 * Slow because of extra copy and we must assume all pixels dirty.
622 */
623static ssize_t dlfb_ops_write(struct fb_info *info, const char __user *buf,
624 size_t count, loff_t *ppos)
625{
626 ssize_t result = -ENOSYS;
627 struct dlfb_data *dev = info->par;
628 u32 offset = (u32) *ppos;
629
630#if defined CONFIG_FB_SYS_FOPS || defined CONFIG_FB_SYS_FOPS_MODULE
631
632 result = fb_sys_write(info, buf, count, ppos);
633
634 if (result > 0) {
635 int start = max((int)(offset / info->fix.line_length) - 1, 0);
636 int lines = min((u32)((result / info->fix.line_length) + 1),
637 (u32)info->var.yres);
638
639 dlfb_handle_damage(dev, 0, start, info->var.xres,
640 lines, info->screen_base);
641 }
642#endif
643
644 return result;
645}
646
Bernie Thompson530f43a2010-02-15 06:46:21 -0800647/* hardware has native COPY command (see libdlo), but not worth it for fbcon */
Bernie Thompson45742032010-02-15 06:46:04 -0800648static void dlfb_ops_copyarea(struct fb_info *info,
Bernie Thompson530f43a2010-02-15 06:46:21 -0800649 const struct fb_copyarea *area)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700650{
Bernie Thompson530f43a2010-02-15 06:46:21 -0800651
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700652 struct dlfb_data *dev = info->par;
653
Bernie Thompson530f43a2010-02-15 06:46:21 -0800654#if defined CONFIG_FB_SYS_COPYAREA || defined CONFIG_FB_SYS_COPYAREA_MODULE
655
656 sys_copyarea(info, area);
657
658 dlfb_handle_damage(dev, area->dx, area->dy,
659 area->width, area->height, info->screen_base);
660#endif
661 atomic_inc(&dev->copy_count);
662
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700663}
664
Bernie Thompson45742032010-02-15 06:46:04 -0800665static void dlfb_ops_imageblit(struct fb_info *info,
Bernie Thompson530f43a2010-02-15 06:46:21 -0800666 const struct fb_image *image)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700667{
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700668 struct dlfb_data *dev = info->par;
Bernie Thompson530f43a2010-02-15 06:46:21 -0800669
670#if defined CONFIG_FB_SYS_IMAGEBLIT || defined CONFIG_FB_SYS_IMAGEBLIT_MODULE
671
672 sys_imageblit(info, image);
673
674 dlfb_handle_damage(dev, image->dx, image->dy,
675 image->width, image->height, info->screen_base);
676
677#endif
678
679 atomic_inc(&dev->blit_count);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700680}
681
Bernie Thompson45742032010-02-15 06:46:04 -0800682static void dlfb_ops_fillrect(struct fb_info *info,
Bernie Thompson530f43a2010-02-15 06:46:21 -0800683 const struct fb_fillrect *rect)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700684{
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700685 struct dlfb_data *dev = info->par;
686
Bernie Thompson530f43a2010-02-15 06:46:21 -0800687#if defined CONFIG_FB_SYS_FILLRECT || defined CONFIG_FB_SYS_FILLRECT_MODULE
688
689 sys_fillrect(info, rect);
690
691 dlfb_handle_damage(dev, rect->dx, rect->dy, rect->width,
692 rect->height, info->screen_base);
693#endif
694
695 atomic_inc(&dev->fill_count);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700696
697}
698
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800699static void dlfb_get_edid(struct dlfb_data *dev)
700{
701 int i;
702 int ret;
703 char rbuf[2];
704
705 for (i = 0; i < sizeof(dev->edid); i++) {
706 ret = usb_control_msg(dev->udev,
707 usb_rcvctrlpipe(dev->udev, 0), (0x02),
708 (0x80 | (0x02 << 5)), i << 8, 0xA1, rbuf, 2,
709 0);
710 dev->edid[i] = rbuf[1];
711 }
712}
713
Bernie Thompson45742032010-02-15 06:46:04 -0800714static int dlfb_ops_ioctl(struct fb_info *info, unsigned int cmd,
Bernie Thompson530f43a2010-02-15 06:46:21 -0800715 unsigned long arg)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700716{
Bernie Thompson530f43a2010-02-15 06:46:21 -0800717
718 struct dlfb_data *dev = info->par;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700719 struct dloarea *area = NULL;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700720
Bernie Thompson530f43a2010-02-15 06:46:21 -0800721 if (!atomic_read(&dev->usb_active))
722 return 0;
723
724 /* TODO: Update X server to get this from sysfs instead */
725 if (cmd == DLFB_IOCTL_RETURN_EDID) {
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700726 char *edid = (char *)arg;
Bernie Thompson530f43a2010-02-15 06:46:21 -0800727 dlfb_get_edid(dev);
728 if (copy_to_user(edid, dev->edid, sizeof(dev->edid)))
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700729 return -EFAULT;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700730 return 0;
731 }
732
Bernie Thompson530f43a2010-02-15 06:46:21 -0800733 /* TODO: Help propose a standard fb.h ioctl to report mmap damage */
734 if (cmd == DLFB_IOCTL_REPORT_DAMAGE) {
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700735
736 area = (struct dloarea *)arg;
737
738 if (area->x < 0)
739 area->x = 0;
740
741 if (area->x > info->var.xres)
742 area->x = info->var.xres;
743
744 if (area->y < 0)
745 area->y = 0;
746
747 if (area->y > info->var.yres)
748 area->y = info->var.yres;
749
Bernie Thompson530f43a2010-02-15 06:46:21 -0800750 atomic_set(&dev->use_defio, 0);
751
752 dlfb_handle_damage(dev, area->x, area->y, area->w, area->h,
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700753 info->screen_base);
Bernie Thompson530f43a2010-02-15 06:46:21 -0800754 atomic_inc(&dev->damage_count);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700755 }
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700756
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700757 return 0;
758}
759
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700760/* taken from vesafb */
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700761static int
Bernie Thompson45742032010-02-15 06:46:04 -0800762dlfb_ops_setcolreg(unsigned regno, unsigned red, unsigned green,
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700763 unsigned blue, unsigned transp, struct fb_info *info)
764{
765 int err = 0;
766
767 if (regno >= info->cmap.len)
768 return 1;
769
770 if (regno < 16) {
771 if (info->var.red.offset == 10) {
772 /* 1:5:5:5 */
773 ((u32 *) (info->pseudo_palette))[regno] =
774 ((red & 0xf800) >> 1) |
775 ((green & 0xf800) >> 6) | ((blue & 0xf800) >> 11);
776 } else {
777 /* 0:5:6:5 */
778 ((u32 *) (info->pseudo_palette))[regno] =
779 ((red & 0xf800)) |
780 ((green & 0xfc00) >> 5) | ((blue & 0xf800) >> 11);
781 }
782 }
783
784 return err;
785}
786
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800787/*
788 * It's common for several clients to have framebuffer open simultaneously.
789 * e.g. both fbcon and X. Makes things interesting.
790 */
791static int dlfb_ops_open(struct fb_info *info, int user)
792{
793 struct dlfb_data *dev = info->par;
794
795/* if (user == 0)
796 * We could special case kernel mode clients (fbcon) here
797 */
798
799 mutex_lock(&dev->fb_open_lock);
800
801 dev->fb_count++;
802
803#ifdef CONFIG_FB_DEFERRED_IO
804 if ((atomic_read(&dev->use_defio)) && (info->fbdefio == NULL)) {
805 /* enable defio */
806 info->fbdefio = &dlfb_defio;
807 fb_deferred_io_init(info);
808 }
809#endif
810
811 dl_notice("open /dev/fb%d user=%d fb_info=%p count=%d\n",
812 info->node, user, info, dev->fb_count);
813
814 mutex_unlock(&dev->fb_open_lock);
815
816 return 0;
817}
818
Bernie Thompson45742032010-02-15 06:46:04 -0800819static int dlfb_ops_release(struct fb_info *info, int user)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700820{
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800821 struct dlfb_data *dev = info->par;
822
823 mutex_lock(&dev->fb_open_lock);
824
825 dev->fb_count--;
826
827#ifdef CONFIG_FB_DEFERRED_IO
828 if ((dev->fb_count == 0) && (info->fbdefio)) {
829 fb_deferred_io_cleanup(info);
830 info->fbdefio = NULL;
831 info->fbops->fb_mmap = dlfb_ops_mmap;
832 }
833#endif
834
835 dl_notice("release /dev/fb%d user=%d count=%d\n",
836 info->node, user, dev->fb_count);
837
838 mutex_unlock(&dev->fb_open_lock);
839
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700840 return 0;
841}
842
Bernie Thompson4a4854d2010-02-15 06:45:55 -0800843/*
844 * Called when all client interfaces to start transactions have been disabled,
845 * and all references to our device instance (dlfb_data) are released.
846 * Every transaction must have a reference, so we know are fully spun down
847 */
848static void dlfb_delete(struct kref *kref)
849{
850 struct dlfb_data *dev = container_of(kref, struct dlfb_data, kref);
851
852 if (dev->backing_buffer)
853 vfree(dev->backing_buffer);
854
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800855 mutex_destroy(&dev->fb_open_lock);
856
Bernie Thompson4a4854d2010-02-15 06:45:55 -0800857 kfree(dev);
858}
859
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800860/*
Bernie Thompson2469d5d2010-02-15 06:46:13 -0800861 * Called by fbdev as last part of unregister_framebuffer() process
862 * No new clients can open connections. Deallocate everything fb_info.
863 */
864static void dlfb_ops_destroy(struct fb_info *info)
865{
866 struct dlfb_data *dev = info->par;
867
868 if (info->cmap.len != 0)
869 fb_dealloc_cmap(&info->cmap);
870 if (info->monspecs.modedb)
871 fb_destroy_modedb(info->monspecs.modedb);
872 if (info->screen_base)
873 vfree(info->screen_base);
874
875 fb_destroy_modelist(&info->modelist);
876
877 framebuffer_release(info);
878
879 /* ref taken before register_framebuffer() for dlfb_data clients */
880 kref_put(&dev->kref, dlfb_delete);
881}
882
883/*
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800884 * Check whether a video mode is supported by the DisplayLink chip
885 * We start from monitor's modes, so don't need to filter that here
886 */
887static int dlfb_is_valid_mode(struct fb_videomode *mode,
888 struct fb_info *info)
889{
890 struct dlfb_data *dev = info->par;
891
892 if (mode->xres * mode->yres > dev->sku_pixel_limit)
893 return 0;
894
895 return 1;
896}
897
898static void dlfb_var_color_format(struct fb_var_screeninfo *var)
899{
900 const struct fb_bitfield red = { 11, 5, 0 };
901 const struct fb_bitfield green = { 5, 6, 0 };
902 const struct fb_bitfield blue = { 0, 5, 0 };
903
904 var->bits_per_pixel = 16;
905 var->red = red;
906 var->green = green;
907 var->blue = blue;
908}
909
Bernie Thompson2469d5d2010-02-15 06:46:13 -0800910static int dlfb_ops_check_var(struct fb_var_screeninfo *var,
911 struct fb_info *info)
912{
913 struct fb_videomode mode;
914
915 /* TODO: support dynamically changing framebuffer size */
916 if ((var->xres * var->yres * 2) > info->fix.smem_len)
917 return -EINVAL;
918
919 /* set device-specific elements of var unrelated to mode */
920 dlfb_var_color_format(var);
921
922 fb_var_to_videomode(&mode, var);
923
924 if (!dlfb_is_valid_mode(&mode, info))
925 return -EINVAL;
926
927 return 0;
928}
929
930static int dlfb_ops_set_par(struct fb_info *info)
931{
932 struct dlfb_data *dev = info->par;
933
934 dl_notice("set_par mode %dx%d\n", info->var.xres, info->var.yres);
935
936 return dlfb_set_video_mode(dev, &info->var);
937}
938
Bernie Thompson45742032010-02-15 06:46:04 -0800939static int dlfb_ops_blank(int blank_mode, struct fb_info *info)
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700940{
Bernie Thompson530f43a2010-02-15 06:46:21 -0800941 struct dlfb_data *dev = info->par;
942 char *bufptr;
943 struct urb *urb;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700944
Bernie Thompson530f43a2010-02-15 06:46:21 -0800945 urb = dlfb_get_urb(dev);
946 if (!urb)
947 return 0;
948 bufptr = (char *) urb->transfer_buffer;
949
950 /* overloading usb_active. UNBLANK can conflict with teardown */
951
952 bufptr = dlfb_vidreg_lock(bufptr);
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700953 if (blank_mode != FB_BLANK_UNBLANK) {
Bernie Thompson530f43a2010-02-15 06:46:21 -0800954 atomic_set(&dev->usb_active, 0);
955 bufptr = dlfb_enable_hvsync(bufptr, false);
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700956 } else {
Bernie Thompson530f43a2010-02-15 06:46:21 -0800957 atomic_set(&dev->usb_active, 1);
958 bufptr = dlfb_enable_hvsync(bufptr, true);
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700959 }
Bernie Thompson530f43a2010-02-15 06:46:21 -0800960 bufptr = dlfb_vidreg_unlock(bufptr);
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700961
Bernie Thompson530f43a2010-02-15 06:46:21 -0800962 dlfb_submit_urb(dev, urb, bufptr - (char *) urb->transfer_buffer);
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700963
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700964 return 0;
965}
966
967static struct fb_ops dlfb_ops = {
Bernie Thompson2469d5d2010-02-15 06:46:13 -0800968 .owner = THIS_MODULE,
Bernie Thompsond46ecb92010-09-05 16:35:04 -0700969 .fb_read = dlfb_ops_read,
970 .fb_write = dlfb_ops_write,
Bernie Thompson45742032010-02-15 06:46:04 -0800971 .fb_setcolreg = dlfb_ops_setcolreg,
972 .fb_fillrect = dlfb_ops_fillrect,
973 .fb_copyarea = dlfb_ops_copyarea,
974 .fb_imageblit = dlfb_ops_imageblit,
975 .fb_mmap = dlfb_ops_mmap,
976 .fb_ioctl = dlfb_ops_ioctl,
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800977 .fb_open = dlfb_ops_open,
Bernie Thompson45742032010-02-15 06:46:04 -0800978 .fb_release = dlfb_ops_release,
979 .fb_blank = dlfb_ops_blank,
Bernie Thompson2469d5d2010-02-15 06:46:13 -0800980 .fb_check_var = dlfb_ops_check_var,
981 .fb_set_par = dlfb_ops_set_par,
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700982};
983
Bernie Thompsoncc403dc2010-02-15 06:45:49 -0800984/*
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800985 * Calls dlfb_get_edid() to query the EDID of attached monitor via usb cmds
986 * Then parses EDID into three places used by various parts of fbdev:
987 * fb_var_screeninfo contains the timing of the monitor's preferred mode
988 * fb_info.monspecs is full parsed EDID info, including monspecs.modedb
989 * fb_info.modelist is a linked list of all monitor & VESA modes which work
990 *
991 * If EDID is not readable/valid, then modelist is all VESA modes,
992 * monspecs is NULL, and fb_var_screeninfo is set to safe VESA mode
993 * Returns 0 if EDID parses successfully
994 */
995static int dlfb_parse_edid(struct dlfb_data *dev,
996 struct fb_var_screeninfo *var,
997 struct fb_info *info)
998{
999 int i;
1000 const struct fb_videomode *default_vmode = NULL;
1001 int result = 0;
1002
1003 fb_destroy_modelist(&info->modelist);
1004 memset(&info->monspecs, 0, sizeof(info->monspecs));
1005
1006 dlfb_get_edid(dev);
1007 fb_edid_to_monspecs(dev->edid, &info->monspecs);
1008
1009 if (info->monspecs.modedb_len > 0) {
1010
1011 for (i = 0; i < info->monspecs.modedb_len; i++) {
1012 if (dlfb_is_valid_mode(&info->monspecs.modedb[i], info))
1013 fb_add_videomode(&info->monspecs.modedb[i],
1014 &info->modelist);
1015 }
1016
1017 default_vmode = fb_find_best_display(&info->monspecs,
1018 &info->modelist);
1019 } else {
1020 struct fb_videomode fb_vmode = {0};
1021
1022 dl_err("Unable to get valid EDID from device/display\n");
1023 result = 1;
1024
1025 /*
1026 * Add the standard VESA modes to our modelist
1027 * Since we don't have EDID, there may be modes that
1028 * overspec monitor and/or are incorrect aspect ratio, etc.
1029 * But at least the user has a chance to choose
1030 */
1031 for (i = 0; i < VESA_MODEDB_SIZE; i++) {
1032 if (dlfb_is_valid_mode((struct fb_videomode *)
1033 &vesa_modes[i], info))
1034 fb_add_videomode(&vesa_modes[i],
1035 &info->modelist);
1036 }
1037
1038 /*
1039 * default to resolution safe for projectors
1040 * (since they are most common case without EDID)
1041 */
1042 fb_vmode.xres = 800;
1043 fb_vmode.yres = 600;
1044 fb_vmode.refresh = 60;
1045 default_vmode = fb_find_nearest_mode(&fb_vmode,
1046 &info->modelist);
1047 }
1048
1049 fb_videomode_to_var(var, default_vmode);
1050 dlfb_var_color_format(var);
1051
1052 return result;
1053}
1054
1055static ssize_t metrics_bytes_rendered_show(struct device *fbdev,
1056 struct device_attribute *a, char *buf) {
1057 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1058 struct dlfb_data *dev = fb_info->par;
1059 return snprintf(buf, PAGE_SIZE, "%u\n",
1060 atomic_read(&dev->bytes_rendered));
1061}
1062
1063static ssize_t metrics_bytes_identical_show(struct device *fbdev,
1064 struct device_attribute *a, char *buf) {
1065 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1066 struct dlfb_data *dev = fb_info->par;
1067 return snprintf(buf, PAGE_SIZE, "%u\n",
1068 atomic_read(&dev->bytes_identical));
1069}
1070
1071static ssize_t metrics_bytes_sent_show(struct device *fbdev,
1072 struct device_attribute *a, char *buf) {
1073 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1074 struct dlfb_data *dev = fb_info->par;
1075 return snprintf(buf, PAGE_SIZE, "%u\n",
1076 atomic_read(&dev->bytes_sent));
1077}
1078
1079static ssize_t metrics_cpu_kcycles_used_show(struct device *fbdev,
1080 struct device_attribute *a, char *buf) {
1081 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1082 struct dlfb_data *dev = fb_info->par;
1083 return snprintf(buf, PAGE_SIZE, "%u\n",
1084 atomic_read(&dev->cpu_kcycles_used));
1085}
1086
1087static ssize_t metrics_misc_show(struct device *fbdev,
1088 struct device_attribute *a, char *buf) {
1089 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1090 struct dlfb_data *dev = fb_info->par;
1091 return snprintf(buf, PAGE_SIZE,
1092 "Calls to\ndamage: %u\nblit: %u\n"
1093 "defio faults: %u\ncopy: %u\n"
1094 "fill: %u\n\n"
1095 "active framebuffer clients: %d\n"
1096 "urbs available %d(%d)\n"
1097 "Shadow framebuffer in use? %s\n"
1098 "Any lost pixels? %s\n",
1099 atomic_read(&dev->damage_count),
1100 atomic_read(&dev->blit_count),
1101 atomic_read(&dev->defio_fault_count),
1102 atomic_read(&dev->copy_count),
1103 atomic_read(&dev->fill_count),
1104 dev->fb_count,
1105 dev->urbs.available, dev->urbs.limit_sem.count,
1106 (dev->backing_buffer) ? "yes" : "no",
1107 atomic_read(&dev->lost_pixels) ? "yes" : "no");
1108}
1109
Chris Wright2c3c8be2010-05-12 18:28:57 -07001110static ssize_t edid_show(struct file *filp, struct kobject *kobj,
1111 struct bin_attribute *a,
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001112 char *buf, loff_t off, size_t count) {
1113 struct device *fbdev = container_of(kobj, struct device, kobj);
1114 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1115 struct dlfb_data *dev = fb_info->par;
1116 char *edid = &dev->edid[0];
1117 const size_t size = sizeof(dev->edid);
1118
1119 if (dlfb_parse_edid(dev, &fb_info->var, fb_info))
1120 return 0;
1121
1122 if (off >= size)
1123 return 0;
1124
1125 if (off + count > size)
1126 count = size - off;
1127 memcpy(buf, edid + off, count);
1128
1129 return count;
1130}
1131
1132
1133static ssize_t metrics_reset_store(struct device *fbdev,
1134 struct device_attribute *attr,
1135 const char *buf, size_t count)
1136{
1137 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1138 struct dlfb_data *dev = fb_info->par;
1139
1140 atomic_set(&dev->bytes_rendered, 0);
1141 atomic_set(&dev->bytes_identical, 0);
1142 atomic_set(&dev->bytes_sent, 0);
1143 atomic_set(&dev->cpu_kcycles_used, 0);
1144 atomic_set(&dev->blit_count, 0);
1145 atomic_set(&dev->copy_count, 0);
1146 atomic_set(&dev->fill_count, 0);
1147 atomic_set(&dev->defio_fault_count, 0);
1148 atomic_set(&dev->damage_count, 0);
1149
1150 return count;
1151}
1152
1153static ssize_t use_defio_show(struct device *fbdev,
1154 struct device_attribute *a, char *buf) {
1155 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1156 struct dlfb_data *dev = fb_info->par;
1157 return snprintf(buf, PAGE_SIZE, "%d\n",
1158 atomic_read(&dev->use_defio));
1159}
1160
1161static ssize_t use_defio_store(struct device *fbdev,
1162 struct device_attribute *attr,
1163 const char *buf, size_t count)
1164{
1165 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1166 struct dlfb_data *dev = fb_info->par;
1167
1168 if (count > 0) {
1169 if (buf[0] == '0')
1170 atomic_set(&dev->use_defio, 0);
1171 if (buf[0] == '1')
1172 atomic_set(&dev->use_defio, 1);
1173 }
1174 return count;
1175}
1176
1177static struct bin_attribute edid_attr = {
1178 .attr.name = "edid",
1179 .attr.mode = 0444,
1180 .size = 128,
1181 .read = edid_show,
1182};
1183
1184static struct device_attribute fb_device_attrs[] = {
1185 __ATTR_RO(metrics_bytes_rendered),
1186 __ATTR_RO(metrics_bytes_identical),
1187 __ATTR_RO(metrics_bytes_sent),
1188 __ATTR_RO(metrics_cpu_kcycles_used),
1189 __ATTR_RO(metrics_misc),
1190 __ATTR(metrics_reset, S_IWUGO, NULL, metrics_reset_store),
1191 __ATTR_RW(use_defio),
1192};
1193
Bernie Thompson3e8f3d62010-02-15 06:46:26 -08001194#ifdef CONFIG_FB_DEFERRED_IO
1195static void dlfb_dpy_deferred_io(struct fb_info *info,
1196 struct list_head *pagelist)
1197{
1198 struct page *cur;
1199 struct fb_deferred_io *fbdefio = info->fbdefio;
1200 struct dlfb_data *dev = info->par;
1201 struct urb *urb;
1202 char *cmd;
1203 cycles_t start_cycles, end_cycles;
1204 int bytes_sent = 0;
1205 int bytes_identical = 0;
1206 int bytes_rendered = 0;
1207 int fault_count = 0;
1208
1209 if (!atomic_read(&dev->use_defio))
1210 return;
1211
1212 if (!atomic_read(&dev->usb_active))
1213 return;
1214
1215 start_cycles = get_cycles();
1216
1217 urb = dlfb_get_urb(dev);
1218 if (!urb)
1219 return;
1220 cmd = urb->transfer_buffer;
1221
1222 /* walk the written page list and render each to device */
1223 list_for_each_entry(cur, &fbdefio->pagelist, lru) {
1224 dlfb_render_hline(dev, &urb, (char *) info->fix.smem_start,
1225 &cmd, cur->index << PAGE_SHIFT,
1226 PAGE_SIZE, &bytes_identical, &bytes_sent);
1227 bytes_rendered += PAGE_SIZE;
1228 fault_count++;
1229 }
1230
1231 if (cmd > (char *) urb->transfer_buffer) {
1232 /* Send partial buffer remaining before exiting */
1233 int len = cmd - (char *) urb->transfer_buffer;
1234 dlfb_submit_urb(dev, urb, len);
1235 bytes_sent += len;
1236 } else
1237 dlfb_urb_completion(urb);
1238
1239 atomic_add(fault_count, &dev->defio_fault_count);
1240 atomic_add(bytes_sent, &dev->bytes_sent);
1241 atomic_add(bytes_identical, &dev->bytes_identical);
1242 atomic_add(bytes_rendered, &dev->bytes_rendered);
1243 end_cycles = get_cycles();
1244 atomic_add(((unsigned int) ((end_cycles - start_cycles)
1245 >> 10)), /* Kcycles */
1246 &dev->cpu_kcycles_used);
1247}
1248
1249static struct fb_deferred_io dlfb_defio = {
1250 .delay = 5,
1251 .deferred_io = dlfb_dpy_deferred_io,
1252};
1253
1254#endif
1255
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001256/*
Bernie Thompsoncc403dc2010-02-15 06:45:49 -08001257 * This is necessary before we can communicate with the display controller.
1258 */
1259static int dlfb_select_std_channel(struct dlfb_data *dev)
1260{
1261 int ret;
1262 u8 set_def_chn[] = { 0x57, 0xCD, 0xDC, 0xA7,
1263 0x1C, 0x88, 0x5E, 0x15,
1264 0x60, 0xFE, 0xC6, 0x97,
1265 0x16, 0x3D, 0x47, 0xF2 };
1266
1267 ret = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
1268 NR_USB_REQUEST_CHANNEL,
1269 (USB_DIR_OUT | USB_TYPE_VENDOR), 0, 0,
1270 set_def_chn, sizeof(set_def_chn), USB_CTRL_SET_TIMEOUT);
1271 return ret;
1272}
1273
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001274
1275static int dlfb_usb_probe(struct usb_interface *interface,
Bernie Thompson59277b62009-11-24 15:52:21 -08001276 const struct usb_device_id *id)
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001277{
Bernie Thompson59277b62009-11-24 15:52:21 -08001278 struct usb_device *usbdev;
1279 struct dlfb_data *dev;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001280 struct fb_info *info;
Bernie Thompson59277b62009-11-24 15:52:21 -08001281 int videomemorysize;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001282 int i;
Bernie Thompson59277b62009-11-24 15:52:21 -08001283 unsigned char *videomemory;
1284 int retval = -ENOMEM;
1285 struct fb_var_screeninfo *var;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001286 int registered = 0;
1287 u16 *pix_framebuffer;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001288
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001289 /* usb initialization */
1290
1291 usbdev = interface_to_usbdev(interface);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001292
Bernie Thompson59277b62009-11-24 15:52:21 -08001293 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1294 if (dev == NULL) {
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001295 err("dlfb_usb_probe: failed alloc of dev struct\n");
1296 goto error;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001297 }
1298
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001299 /* we need to wait for both usb and fbdev to spin down on disconnect */
1300 kref_init(&dev->kref); /* matching kref_put in usb .disconnect fn */
1301 kref_get(&dev->kref); /* matching kref_put in .fb_destroy function*/
1302
Bernie Thompson59277b62009-11-24 15:52:21 -08001303 dev->udev = usbdev;
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001304 dev->gdev = &usbdev->dev; /* our generic struct device * */
Bernie Thompson59277b62009-11-24 15:52:21 -08001305 usb_set_intfdata(interface, dev);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001306
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001307 if (!dlfb_alloc_urb_list(dev, WRITES_IN_FLIGHT, MAX_TRANSFER)) {
1308 retval = -ENOMEM;
1309 dl_err("dlfb_alloc_urb_list failed\n");
1310 goto error;
1311 }
1312
1313 mutex_init(&dev->fb_open_lock);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001314
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001315 /* We don't register a new USB class. Our client interface is fbdev */
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001316
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001317 /* allocates framebuffer driver structure, not framebuffer memory */
1318 info = framebuffer_alloc(0, &usbdev->dev);
1319 if (!info) {
1320 retval = -ENOMEM;
1321 dl_err("framebuffer_alloc failed\n");
1322 goto error;
1323 }
Bernie Thompson59277b62009-11-24 15:52:21 -08001324 dev->info = info;
1325 info->par = dev;
1326 info->pseudo_palette = dev->pseudo_palette;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001327 info->fbops = &dlfb_ops;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001328
Bernie Thompson59277b62009-11-24 15:52:21 -08001329 var = &info->var;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001330
1331 /* TODO set limit based on actual SKU detection */
1332 dev->sku_pixel_limit = 2048 * 1152;
1333
1334 INIT_LIST_HEAD(&info->modelist);
1335 dlfb_parse_edid(dev, var, info);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001336
Bernie Thompson59277b62009-11-24 15:52:21 -08001337 /*
1338 * ok, now that we've got the size info, we can alloc our framebuffer.
Bernie Thompson59277b62009-11-24 15:52:21 -08001339 */
Bernie Thompson59277b62009-11-24 15:52:21 -08001340 info->fix = dlfb_fix;
1341 info->fix.line_length = var->xres * (var->bits_per_pixel / 8);
1342 videomemorysize = info->fix.line_length * var->yres;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001343
Bernie Thompson59277b62009-11-24 15:52:21 -08001344 /*
1345 * The big chunk of system memory we use as a virtual framebuffer.
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001346 * TODO: Handle fbcon cursor code calling blit in interrupt context
Bernie Thompson59277b62009-11-24 15:52:21 -08001347 */
1348 videomemory = vmalloc(videomemorysize);
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001349 if (!videomemory) {
1350 retval = -ENOMEM;
1351 dl_err("Virtual framebuffer alloc failed\n");
1352 goto error;
1353 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001354
Bernie Thompson59277b62009-11-24 15:52:21 -08001355 info->screen_base = videomemory;
1356 info->fix.smem_len = PAGE_ALIGN(videomemorysize);
1357 info->fix.smem_start = (unsigned long) videomemory;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001358 info->flags = udlfb_info_flags;
1359
Bernie Thompson59277b62009-11-24 15:52:21 -08001360
1361 /*
1362 * Second framebuffer copy, mirroring the state of the framebuffer
1363 * on the physical USB device. We can function without this.
1364 * But with imperfect damage info we may end up sending pixels over USB
1365 * that were, in fact, unchanged -- wasting limited USB bandwidth
1366 */
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001367 dev->backing_buffer = vmalloc(videomemorysize);
Bernie Thompson59277b62009-11-24 15:52:21 -08001368 if (!dev->backing_buffer)
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001369 dl_warn("No shadow/backing buffer allcoated\n");
1370 else
1371 memset(dev->backing_buffer, 0, videomemorysize);
Bernie Thompson59277b62009-11-24 15:52:21 -08001372
1373 retval = fb_alloc_cmap(&info->cmap, 256, 0);
1374 if (retval < 0) {
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001375 dl_err("fb_alloc_cmap failed %x\n", retval);
1376 goto error;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001377 }
1378
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001379 /* ready to begin using device */
1380
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001381#ifdef CONFIG_FB_DEFERRED_IO
1382 atomic_set(&dev->use_defio, 1);
1383#endif
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001384 atomic_set(&dev->usb_active, 1);
Bernie Thompson59277b62009-11-24 15:52:21 -08001385 dlfb_select_std_channel(dev);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001386
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001387 dlfb_ops_check_var(var, info);
1388 dlfb_ops_set_par(info);
1389
1390 /* paint greenscreen */
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001391 pix_framebuffer = (u16 *) videomemory;
1392 for (i = 0; i < videomemorysize / 2; i++)
1393 pix_framebuffer[i] = 0x37e6;
1394
1395 dlfb_handle_damage(dev, 0, 0, info->var.xres, info->var.yres,
1396 videomemory);
Bernie Thompson530f43a2010-02-15 06:46:21 -08001397
Bernie Thompson59277b62009-11-24 15:52:21 -08001398 retval = register_framebuffer(info);
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001399 if (retval < 0) {
1400 dl_err("register_framebuffer failed %d\n", retval);
1401 goto error;
1402 }
1403 registered = 1;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001404
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001405 for (i = 0; i < ARRAY_SIZE(fb_device_attrs); i++)
1406 device_create_file(info->dev, &fb_device_attrs[i]);
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -07001407
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001408 device_create_bin_file(info->dev, &edid_attr);
1409
1410 dl_err("DisplayLink USB device /dev/fb%d attached. %dx%d resolution."
1411 " Using %dK framebuffer memory\n", info->node,
1412 var->xres, var->yres,
1413 ((dev->backing_buffer) ?
1414 videomemorysize * 2 : videomemorysize) >> 10);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001415 return 0;
1416
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001417error:
1418 if (dev) {
1419 if (registered) {
1420 unregister_framebuffer(info);
1421 dlfb_ops_destroy(info);
1422 } else
1423 kref_put(&dev->kref, dlfb_delete);
1424
1425 if (dev->urbs.count > 0)
1426 dlfb_free_urb_list(dev);
1427 kref_put(&dev->kref, dlfb_delete); /* last ref from kref_init */
1428
1429 /* dev has been deallocated. Do not dereference */
1430 }
1431
Bernie Thompson59277b62009-11-24 15:52:21 -08001432 return retval;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001433}
1434
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001435static void dlfb_usb_disconnect(struct usb_interface *interface)
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001436{
Bernie Thompson59277b62009-11-24 15:52:21 -08001437 struct dlfb_data *dev;
1438 struct fb_info *info;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001439 int i;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001440
Bernie Thompson59277b62009-11-24 15:52:21 -08001441 dev = usb_get_intfdata(interface);
Bernie Thompson59277b62009-11-24 15:52:21 -08001442 info = dev->info;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001443
1444 /* when non-active we'll update virtual framebuffer, but no new urbs */
1445 atomic_set(&dev->usb_active, 0);
1446
1447 usb_set_intfdata(interface, NULL);
1448
1449 for (i = 0; i < ARRAY_SIZE(fb_device_attrs); i++)
1450 device_remove_file(info->dev, &fb_device_attrs[i]);
1451
1452 device_remove_bin_file(info->dev, &edid_attr);
1453
1454 /* this function will wait for all in-flight urbs to complete */
1455 dlfb_free_urb_list(dev);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001456
Bernie Thompson59277b62009-11-24 15:52:21 -08001457 if (info) {
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001458 dl_notice("Detaching /dev/fb%d\n", info->node);
Bernie Thompson59277b62009-11-24 15:52:21 -08001459 unregister_framebuffer(info);
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001460 dlfb_ops_destroy(info);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001461 }
1462
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001463 /* release reference taken by kref_init in probe() */
1464 kref_put(&dev->kref, dlfb_delete);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001465
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001466 /* consider dlfb_data freed */
1467
1468 return;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001469}
1470
1471static struct usb_driver dlfb_driver = {
1472 .name = "udlfb",
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001473 .probe = dlfb_usb_probe,
1474 .disconnect = dlfb_usb_disconnect,
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001475 .id_table = id_table,
1476};
1477
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001478static int __init dlfb_module_init(void)
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001479{
1480 int res;
1481
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001482 res = usb_register(&dlfb_driver);
1483 if (res)
1484 err("usb_register failed. Error number %d", res);
1485
Soeren Moellerb5a21042010-05-14 19:03:00 +00001486 printk(KERN_INFO "VMODES initialized\n");
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001487
1488 return res;
1489}
1490
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001491static void __exit dlfb_module_exit(void)
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001492{
1493 usb_deregister(&dlfb_driver);
1494}
1495
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001496module_init(dlfb_module_init);
1497module_exit(dlfb_module_exit);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001498
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001499static void dlfb_urb_completion(struct urb *urb)
1500{
1501 struct urb_node *unode = urb->context;
1502 struct dlfb_data *dev = unode->dev;
1503 unsigned long flags;
1504
1505 /* sync/async unlink faults aren't errors */
1506 if (urb->status) {
1507 if (!(urb->status == -ENOENT ||
1508 urb->status == -ECONNRESET ||
1509 urb->status == -ESHUTDOWN)) {
1510 dl_err("%s - nonzero write bulk status received: %d\n",
1511 __func__, urb->status);
1512 atomic_set(&dev->lost_pixels, 1);
1513 }
1514 }
1515
1516 urb->transfer_buffer_length = dev->urbs.size; /* reset to actual */
1517
1518 spin_lock_irqsave(&dev->urbs.lock, flags);
1519 list_add_tail(&unode->entry, &dev->urbs.list);
1520 dev->urbs.available++;
1521 spin_unlock_irqrestore(&dev->urbs.lock, flags);
1522
1523 up(&dev->urbs.limit_sem);
1524}
1525
1526static void dlfb_free_urb_list(struct dlfb_data *dev)
1527{
1528 int count = dev->urbs.count;
1529 struct list_head *node;
1530 struct urb_node *unode;
1531 struct urb *urb;
1532 int ret;
1533 unsigned long flags;
1534
1535 dl_notice("Waiting for completes and freeing all render urbs\n");
1536
1537 /* keep waiting and freeing, until we've got 'em all */
1538 while (count--) {
1539 /* Timeout means a memory leak and/or fault */
1540 ret = down_timeout(&dev->urbs.limit_sem, FREE_URB_TIMEOUT);
1541 if (ret) {
1542 BUG_ON(ret);
1543 break;
1544 }
1545 spin_lock_irqsave(&dev->urbs.lock, flags);
1546
1547 node = dev->urbs.list.next; /* have reserved one with sem */
1548 list_del_init(node);
1549
1550 spin_unlock_irqrestore(&dev->urbs.lock, flags);
1551
1552 unode = list_entry(node, struct urb_node, entry);
1553 urb = unode->urb;
1554
1555 /* Free each separately allocated piece */
Greg Kroah-Hartmanc220cc32010-04-29 15:36:29 -07001556 usb_free_coherent(urb->dev, dev->urbs.size,
1557 urb->transfer_buffer, urb->transfer_dma);
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001558 usb_free_urb(urb);
1559 kfree(node);
1560 }
1561
1562 kref_put(&dev->kref, dlfb_delete);
1563
1564}
1565
1566static int dlfb_alloc_urb_list(struct dlfb_data *dev, int count, size_t size)
1567{
1568 int i = 0;
1569 struct urb *urb;
1570 struct urb_node *unode;
1571 char *buf;
1572
1573 spin_lock_init(&dev->urbs.lock);
1574
1575 dev->urbs.size = size;
1576 INIT_LIST_HEAD(&dev->urbs.list);
1577
1578 while (i < count) {
1579 unode = kzalloc(sizeof(struct urb_node), GFP_KERNEL);
1580 if (!unode)
1581 break;
1582 unode->dev = dev;
1583
1584 urb = usb_alloc_urb(0, GFP_KERNEL);
1585 if (!urb) {
1586 kfree(unode);
1587 break;
1588 }
1589 unode->urb = urb;
1590
Greg Kroah-Hartmanc220cc32010-04-29 15:36:29 -07001591 buf = usb_alloc_coherent(dev->udev, MAX_TRANSFER, GFP_KERNEL,
1592 &urb->transfer_dma);
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001593 if (!buf) {
1594 kfree(unode);
1595 usb_free_urb(urb);
1596 break;
1597 }
1598
1599 /* urb->transfer_buffer_length set to actual before submit */
1600 usb_fill_bulk_urb(urb, dev->udev, usb_sndbulkpipe(dev->udev, 1),
1601 buf, size, dlfb_urb_completion, unode);
1602 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1603
1604 list_add_tail(&unode->entry, &dev->urbs.list);
1605
1606 i++;
1607 }
1608
1609 sema_init(&dev->urbs.limit_sem, i);
1610 dev->urbs.count = i;
1611 dev->urbs.available = i;
1612
1613 kref_get(&dev->kref); /* released in free_render_urbs() */
1614
Soeren Moellerb5a21042010-05-14 19:03:00 +00001615 dl_notice("allocated %d %d byte urbs\n", i, (int) size);
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001616
1617 return i;
1618}
1619
1620static struct urb *dlfb_get_urb(struct dlfb_data *dev)
1621{
1622 int ret = 0;
1623 struct list_head *entry;
1624 struct urb_node *unode;
1625 struct urb *urb = NULL;
1626 unsigned long flags;
1627
1628 /* Wait for an in-flight buffer to complete and get re-queued */
1629 ret = down_timeout(&dev->urbs.limit_sem, GET_URB_TIMEOUT);
1630 if (ret) {
1631 atomic_set(&dev->lost_pixels, 1);
1632 dl_err("wait for urb interrupted: %x\n", ret);
1633 goto error;
1634 }
1635
1636 spin_lock_irqsave(&dev->urbs.lock, flags);
1637
1638 BUG_ON(list_empty(&dev->urbs.list)); /* reserved one with limit_sem */
1639 entry = dev->urbs.list.next;
1640 list_del_init(entry);
1641 dev->urbs.available--;
1642
1643 spin_unlock_irqrestore(&dev->urbs.lock, flags);
1644
1645 unode = list_entry(entry, struct urb_node, entry);
1646 urb = unode->urb;
1647
1648error:
1649 return urb;
1650}
1651
1652static int dlfb_submit_urb(struct dlfb_data *dev, struct urb *urb, size_t len)
1653{
1654 int ret;
1655
1656 BUG_ON(len > dev->urbs.size);
1657
1658 urb->transfer_buffer_length = len; /* set to actual payload len */
1659 ret = usb_submit_urb(urb, GFP_KERNEL);
1660 if (ret) {
1661 dlfb_urb_completion(urb); /* because no one else will */
1662 atomic_set(&dev->lost_pixels, 1);
1663 dl_err("usb_submit_urb error %x\n", ret);
1664 }
1665 return ret;
1666}
1667
Bernie Thompson59277b62009-11-24 15:52:21 -08001668MODULE_AUTHOR("Roberto De Ioris <roberto@unbit.it>, "
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001669 "Jaya Kumar <jayakumar.lkml@gmail.com>, "
1670 "Bernie Thompson <bernie@plugable.com>");
1671MODULE_DESCRIPTION("DisplayLink kernel framebuffer driver");
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001672MODULE_LICENSE("GPL");
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001673