blob: 0e03dd3227d1cefa0cb619a473efd2ace5468883 [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 Thompson9825f702010-09-05 16:35:10 -0700111 * 0x00 H and V sync on
112 * 0x01 H and V sync off (screen blank but powered)
113 * 0x07 DPMS powerdown (requires modeset to come back)
Bernie Thompson59277b62009-11-24 15:52:21 -0800114 */
Bernie Thompson530f43a2010-02-15 06:46:21 -0800115static char *dlfb_enable_hvsync(char *buf, bool enable)
Bernie Thompson59277b62009-11-24 15:52:21 -0800116{
Bernie Thompson530f43a2010-02-15 06:46:21 -0800117 if (enable)
118 return dlfb_set_register(buf, 0x1F, 0x00);
119 else
Bernie Thompson9825f702010-09-05 16:35:10 -0700120 return dlfb_set_register(buf, 0x1F, 0x07);
Bernie Thompson59277b62009-11-24 15:52:21 -0800121}
122
Bernie Thompson45742032010-02-15 06:46:04 -0800123static char *dlfb_set_color_depth(char *buf, u8 selection)
Bernie Thompson59277b62009-11-24 15:52:21 -0800124{
Bernie Thompson45742032010-02-15 06:46:04 -0800125 return dlfb_set_register(buf, 0x00, selection);
Bernie Thompson59277b62009-11-24 15:52:21 -0800126}
127
Bernie Thompson45742032010-02-15 06:46:04 -0800128static char *dlfb_set_base16bpp(char *wrptr, u32 base)
Bernie Thompson59277b62009-11-24 15:52:21 -0800129{
Bernie Thompson1d31a9e2010-02-15 06:45:43 -0800130 /* the base pointer is 16 bits wide, 0x20 is hi byte. */
Bernie Thompson45742032010-02-15 06:46:04 -0800131 wrptr = dlfb_set_register(wrptr, 0x20, base >> 16);
132 wrptr = dlfb_set_register(wrptr, 0x21, base >> 8);
133 return dlfb_set_register(wrptr, 0x22, base);
Bernie Thompson59277b62009-11-24 15:52:21 -0800134}
135
Bernie Thompsonbd808162010-02-15 06:46:48 -0800136/*
137 * DisplayLink HW has separate 16bpp and 8bpp framebuffers.
138 * In 24bpp modes, the low 323 RGB bits go in the 8bpp framebuffer
139 */
Bernie Thompson45742032010-02-15 06:46:04 -0800140static char *dlfb_set_base8bpp(char *wrptr, u32 base)
Bernie Thompson59277b62009-11-24 15:52:21 -0800141{
Bernie Thompson45742032010-02-15 06:46:04 -0800142 wrptr = dlfb_set_register(wrptr, 0x26, base >> 16);
143 wrptr = dlfb_set_register(wrptr, 0x27, base >> 8);
144 return dlfb_set_register(wrptr, 0x28, base);
Bernie Thompson59277b62009-11-24 15:52:21 -0800145}
146
Bernie Thompson45742032010-02-15 06:46:04 -0800147static char *dlfb_set_register_16(char *wrptr, u8 reg, u16 value)
Bernie Thompson59277b62009-11-24 15:52:21 -0800148{
Bernie Thompson45742032010-02-15 06:46:04 -0800149 wrptr = dlfb_set_register(wrptr, reg, value >> 8);
150 return dlfb_set_register(wrptr, reg+1, value);
Bernie Thompson59277b62009-11-24 15:52:21 -0800151}
152
153/*
154 * This is kind of weird because the controller takes some
155 * register values in a different byte order than other registers.
156 */
Bernie Thompson45742032010-02-15 06:46:04 -0800157static char *dlfb_set_register_16be(char *wrptr, u8 reg, u16 value)
Bernie Thompson59277b62009-11-24 15:52:21 -0800158{
Bernie Thompson45742032010-02-15 06:46:04 -0800159 wrptr = dlfb_set_register(wrptr, reg, value);
160 return dlfb_set_register(wrptr, reg+1, value >> 8);
Bernie Thompson59277b62009-11-24 15:52:21 -0800161}
162
163/*
164 * LFSR is linear feedback shift register. The reason we have this is
165 * because the display controller needs to minimize the clock depth of
166 * various counters used in the display path. So this code reverses the
167 * provided value into the lfsr16 value by counting backwards to get
168 * the value that needs to be set in the hardware comparator to get the
169 * same actual count. This makes sense once you read above a couple of
170 * times and think about it from a hardware perspective.
171 */
Bernie Thompsonbd808162010-02-15 06:46:48 -0800172static u16 dlfb_lfsr16(u16 actual_count)
Bernie Thompson59277b62009-11-24 15:52:21 -0800173{
174 u32 lv = 0xFFFF; /* This is the lfsr value that the hw starts with */
175
176 while (actual_count--) {
177 lv = ((lv << 1) |
178 (((lv >> 15) ^ (lv >> 4) ^ (lv >> 2) ^ (lv >> 1)) & 1))
179 & 0xFFFF;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700180 }
Bernie Thompson59277b62009-11-24 15:52:21 -0800181
182 return (u16) lv;
183}
184
185/*
186 * This does LFSR conversion on the value that is to be written.
187 * See LFSR explanation above for more detail.
188 */
Bernie Thompson45742032010-02-15 06:46:04 -0800189static char *dlfb_set_register_lfsr16(char *wrptr, u8 reg, u16 value)
Bernie Thompson59277b62009-11-24 15:52:21 -0800190{
Bernie Thompsonbd808162010-02-15 06:46:48 -0800191 return dlfb_set_register_16(wrptr, reg, dlfb_lfsr16(value));
Bernie Thompson59277b62009-11-24 15:52:21 -0800192}
193
194/*
195 * This takes a standard fbdev screeninfo struct and all of its monitor mode
196 * details and converts them into the DisplayLink equivalent register commands.
197 */
Bernie Thompson45742032010-02-15 06:46:04 -0800198static char *dlfb_set_vid_cmds(char *wrptr, struct fb_var_screeninfo *var)
Bernie Thompson59277b62009-11-24 15:52:21 -0800199{
200 u16 xds, yds;
201 u16 xde, yde;
202 u16 yec;
203
Bernie Thompson59277b62009-11-24 15:52:21 -0800204 /* x display start */
205 xds = var->left_margin + var->hsync_len;
Bernie Thompson45742032010-02-15 06:46:04 -0800206 wrptr = dlfb_set_register_lfsr16(wrptr, 0x01, xds);
Bernie Thompson59277b62009-11-24 15:52:21 -0800207 /* x display end */
208 xde = xds + var->xres;
Bernie Thompson45742032010-02-15 06:46:04 -0800209 wrptr = dlfb_set_register_lfsr16(wrptr, 0x03, xde);
Bernie Thompson59277b62009-11-24 15:52:21 -0800210
211 /* y display start */
212 yds = var->upper_margin + var->vsync_len;
Bernie Thompson45742032010-02-15 06:46:04 -0800213 wrptr = dlfb_set_register_lfsr16(wrptr, 0x05, yds);
Bernie Thompson59277b62009-11-24 15:52:21 -0800214 /* y display end */
215 yde = yds + var->yres;
Bernie Thompson45742032010-02-15 06:46:04 -0800216 wrptr = dlfb_set_register_lfsr16(wrptr, 0x07, yde);
Bernie Thompson59277b62009-11-24 15:52:21 -0800217
218 /* x end count is active + blanking - 1 */
Bernie Thompson45742032010-02-15 06:46:04 -0800219 wrptr = dlfb_set_register_lfsr16(wrptr, 0x09,
220 xde + var->right_margin - 1);
Bernie Thompson59277b62009-11-24 15:52:21 -0800221
222 /* libdlo hardcodes hsync start to 1 */
Bernie Thompson45742032010-02-15 06:46:04 -0800223 wrptr = dlfb_set_register_lfsr16(wrptr, 0x0B, 1);
Bernie Thompson59277b62009-11-24 15:52:21 -0800224
225 /* hsync end is width of sync pulse + 1 */
Bernie Thompson45742032010-02-15 06:46:04 -0800226 wrptr = dlfb_set_register_lfsr16(wrptr, 0x0D, var->hsync_len + 1);
Bernie Thompson59277b62009-11-24 15:52:21 -0800227
228 /* hpixels is active pixels */
Bernie Thompson45742032010-02-15 06:46:04 -0800229 wrptr = dlfb_set_register_16(wrptr, 0x0F, var->xres);
Bernie Thompson59277b62009-11-24 15:52:21 -0800230
231 /* yendcount is vertical active + vertical blanking */
232 yec = var->yres + var->upper_margin + var->lower_margin +
233 var->vsync_len;
Bernie Thompson45742032010-02-15 06:46:04 -0800234 wrptr = dlfb_set_register_lfsr16(wrptr, 0x11, yec);
Bernie Thompson59277b62009-11-24 15:52:21 -0800235
236 /* libdlo hardcodes vsync start to 0 */
Bernie Thompson45742032010-02-15 06:46:04 -0800237 wrptr = dlfb_set_register_lfsr16(wrptr, 0x13, 0);
Bernie Thompson59277b62009-11-24 15:52:21 -0800238
239 /* vsync end is width of vsync pulse */
Bernie Thompson45742032010-02-15 06:46:04 -0800240 wrptr = dlfb_set_register_lfsr16(wrptr, 0x15, var->vsync_len);
Bernie Thompson59277b62009-11-24 15:52:21 -0800241
242 /* vpixels is active pixels */
Bernie Thompson45742032010-02-15 06:46:04 -0800243 wrptr = dlfb_set_register_16(wrptr, 0x17, var->yres);
Bernie Thompson59277b62009-11-24 15:52:21 -0800244
245 /* convert picoseconds to 5kHz multiple for pclk5k = x * 1E12/5k */
Bernie Thompson45742032010-02-15 06:46:04 -0800246 wrptr = dlfb_set_register_16be(wrptr, 0x1B,
247 200*1000*1000/var->pixclock);
Bernie Thompson59277b62009-11-24 15:52:21 -0800248
249 return wrptr;
250}
251
252/*
253 * This takes a standard fbdev screeninfo struct that was fetched or prepared
254 * and then generates the appropriate command sequence that then drives the
255 * display controller.
256 */
257static int dlfb_set_video_mode(struct dlfb_data *dev,
258 struct fb_var_screeninfo *var)
259{
260 char *buf;
261 char *wrptr;
262 int retval = 0;
263 int writesize;
Bernie Thompson530f43a2010-02-15 06:46:21 -0800264 struct urb *urb;
Bernie Thompson59277b62009-11-24 15:52:21 -0800265
Bernie Thompson530f43a2010-02-15 06:46:21 -0800266 if (!atomic_read(&dev->usb_active))
267 return -EPERM;
268
269 urb = dlfb_get_urb(dev);
270 if (!urb)
271 return -ENOMEM;
272 buf = (char *) urb->transfer_buffer;
Bernie Thompson59277b62009-11-24 15:52:21 -0800273
274 /*
275 * This first section has to do with setting the base address on the
276 * controller * associated with the display. There are 2 base
277 * pointers, currently, we only * use the 16 bpp segment.
278 */
Bernie Thompson45742032010-02-15 06:46:04 -0800279 wrptr = dlfb_vidreg_lock(buf);
280 wrptr = dlfb_set_color_depth(wrptr, 0x00);
Bernie Thompson59277b62009-11-24 15:52:21 -0800281 /* set base for 16bpp segment to 0 */
Bernie Thompson45742032010-02-15 06:46:04 -0800282 wrptr = dlfb_set_base16bpp(wrptr, 0);
Bernie Thompson59277b62009-11-24 15:52:21 -0800283 /* set base for 8bpp segment to end of fb */
Bernie Thompson45742032010-02-15 06:46:04 -0800284 wrptr = dlfb_set_base8bpp(wrptr, dev->info->fix.smem_len);
Bernie Thompson59277b62009-11-24 15:52:21 -0800285
Bernie Thompson45742032010-02-15 06:46:04 -0800286 wrptr = dlfb_set_vid_cmds(wrptr, var);
Bernie Thompson530f43a2010-02-15 06:46:21 -0800287 wrptr = dlfb_enable_hvsync(wrptr, true);
Bernie Thompson45742032010-02-15 06:46:04 -0800288 wrptr = dlfb_vidreg_unlock(wrptr);
Bernie Thompson59277b62009-11-24 15:52:21 -0800289
290 writesize = wrptr - buf;
291
Bernie Thompson530f43a2010-02-15 06:46:21 -0800292 retval = dlfb_submit_urb(dev, urb, writesize);
Bernie Thompson59277b62009-11-24 15:52:21 -0800293
Bernie Thompson59277b62009-11-24 15:52:21 -0800294 return retval;
295}
296
Bernie Thompson45742032010-02-15 06:46:04 -0800297static int dlfb_ops_mmap(struct fb_info *info, struct vm_area_struct *vma)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700298{
299 unsigned long start = vma->vm_start;
300 unsigned long size = vma->vm_end - vma->vm_start;
301 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
302 unsigned long page, pos;
Bernie Thompsonbd808162010-02-15 06:46:48 -0800303 struct dlfb_data *dev = info->par;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700304
Bernie Thompsonbd808162010-02-15 06:46:48 -0800305 dl_notice("MMAP: %lu %u\n", offset + size, info->fix.smem_len);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700306
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700307 if (offset + size > info->fix.smem_len)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700308 return -EINVAL;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700309
310 pos = (unsigned long)info->fix.smem_start + offset;
311
312 while (size > 0) {
313 page = vmalloc_to_pfn((void *)pos);
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700314 if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED))
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700315 return -EAGAIN;
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700316
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700317 start += PAGE_SIZE;
318 pos += PAGE_SIZE;
319 if (size > PAGE_SIZE)
320 size -= PAGE_SIZE;
321 else
322 size = 0;
323 }
324
325 vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
326 return 0;
327
328}
329
Bernie Thompson530f43a2010-02-15 06:46:21 -0800330/*
331 * Trims identical data from front and back of line
332 * Sets new front buffer address and width
333 * And returns byte count of identical pixels
334 * Assumes CPU natural alignment (unsigned long)
335 * for back and front buffer ptrs and width
336 */
337static int dlfb_trim_hline(const u8 *bback, const u8 **bfront, int *width_bytes)
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700338{
Bernie Thompson530f43a2010-02-15 06:46:21 -0800339 int j, k;
340 const unsigned long *back = (const unsigned long *) bback;
341 const unsigned long *front = (const unsigned long *) *bfront;
342 const int width = *width_bytes / sizeof(unsigned long);
343 int identical = width;
344 int start = width;
345 int end = width;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700346
Bernie Thompson530f43a2010-02-15 06:46:21 -0800347 prefetch((void *) front);
348 prefetch((void *) back);
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700349
Bernie Thompson530f43a2010-02-15 06:46:21 -0800350 for (j = 0; j < width; j++) {
351 if (back[j] != front[j]) {
352 start = j;
353 break;
354 }
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700355 }
356
Bernie Thompson530f43a2010-02-15 06:46:21 -0800357 for (k = width - 1; k > j; k--) {
358 if (back[k] != front[k]) {
359 end = k+1;
360 break;
361 }
362 }
363
364 identical = start + (width - end);
365 *bfront = (u8 *) &front[start];
366 *width_bytes = (end - start) * sizeof(unsigned long);
367
368 return identical * sizeof(unsigned long);
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700369}
370
371/*
Pavel Machek3b7b31f2010-04-03 07:00:37 +0200372 * Render a command stream for an encoded horizontal line segment of pixels.
373 *
374 * A command buffer holds several commands.
375 * It always begins with a fresh command header
376 * (the protocol doesn't require this, but we enforce it to allow
377 * multiple buffers to be potentially encoded and sent in parallel).
378 * A single command encodes one contiguous horizontal line of pixels
379 *
380 * The function relies on the client to do all allocation, so that
381 * rendering can be done directly to output buffers (e.g. USB URBs).
382 * The function fills the supplied command buffer, providing information
383 * on where it left off, so the client may call in again with additional
384 * buffers if the line will take several buffers to complete.
385 *
386 * A single command can transmit a maximum of 256 pixels,
387 * regardless of the compression ratio (protocol design limit).
388 * To the hardware, 0 for a size byte means 256
389 *
390 * Rather than 256 pixel commands which are either rl or raw encoded,
391 * the rlx command simply assumes alternating raw and rl spans within one cmd.
392 * This has a slightly larger header overhead, but produces more even results.
393 * It also processes all data (read and write) in a single pass.
394 * Performance benchmarks of common cases show it having just slightly better
395 * compression than 256 pixel raw -or- rle commands, with similar CPU consumpion.
396 * But for very rl friendly data, will compress not quite as well.
397 */
Bernie Thompson530f43a2010-02-15 06:46:21 -0800398static void dlfb_compress_hline(
399 const uint16_t **pixel_start_ptr,
400 const uint16_t *const pixel_end,
401 uint32_t *device_address_ptr,
402 uint8_t **command_buffer_ptr,
403 const uint8_t *const cmd_buffer_end)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700404{
Bernie Thompson530f43a2010-02-15 06:46:21 -0800405 const uint16_t *pixel = *pixel_start_ptr;
406 uint32_t dev_addr = *device_address_ptr;
407 uint8_t *cmd = *command_buffer_ptr;
408 const int bpp = 2;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700409
Bernie Thompson530f43a2010-02-15 06:46:21 -0800410 while ((pixel_end > pixel) &&
411 (cmd_buffer_end - MIN_RLX_CMD_BYTES > cmd)) {
412 uint8_t *raw_pixels_count_byte = 0;
413 uint8_t *cmd_pixels_count_byte = 0;
414 const uint16_t *raw_pixel_start = 0;
415 const uint16_t *cmd_pixel_start, *cmd_pixel_end = 0;
416 const uint32_t be_dev_addr = cpu_to_be32(dev_addr);
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;
422 *cmd++ = (uint8_t) ((be_dev_addr >> 8) & 0xFF);
423 *cmd++ = (uint8_t) ((be_dev_addr >> 16) & 0xFF);
424 *cmd++ = (uint8_t) ((be_dev_addr >> 24) & 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
664 atomic_inc(&dev->copy_count);
665
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700666}
667
Bernie Thompson45742032010-02-15 06:46:04 -0800668static void dlfb_ops_imageblit(struct fb_info *info,
Bernie Thompson530f43a2010-02-15 06:46:21 -0800669 const struct fb_image *image)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700670{
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700671 struct dlfb_data *dev = info->par;
Bernie Thompson530f43a2010-02-15 06:46:21 -0800672
673#if defined CONFIG_FB_SYS_IMAGEBLIT || defined CONFIG_FB_SYS_IMAGEBLIT_MODULE
674
675 sys_imageblit(info, image);
676
677 dlfb_handle_damage(dev, image->dx, image->dy,
678 image->width, image->height, info->screen_base);
679
680#endif
681
682 atomic_inc(&dev->blit_count);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700683}
684
Bernie Thompson45742032010-02-15 06:46:04 -0800685static void dlfb_ops_fillrect(struct fb_info *info,
Bernie Thompson530f43a2010-02-15 06:46:21 -0800686 const struct fb_fillrect *rect)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700687{
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700688 struct dlfb_data *dev = info->par;
689
Bernie Thompson530f43a2010-02-15 06:46:21 -0800690#if defined CONFIG_FB_SYS_FILLRECT || defined CONFIG_FB_SYS_FILLRECT_MODULE
691
692 sys_fillrect(info, rect);
693
694 dlfb_handle_damage(dev, rect->dx, rect->dy, rect->width,
695 rect->height, info->screen_base);
696#endif
697
698 atomic_inc(&dev->fill_count);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700699
700}
701
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800702static void dlfb_get_edid(struct dlfb_data *dev)
703{
704 int i;
705 int ret;
706 char rbuf[2];
707
708 for (i = 0; i < sizeof(dev->edid); i++) {
709 ret = usb_control_msg(dev->udev,
710 usb_rcvctrlpipe(dev->udev, 0), (0x02),
711 (0x80 | (0x02 << 5)), i << 8, 0xA1, rbuf, 2,
712 0);
713 dev->edid[i] = rbuf[1];
714 }
715}
716
Bernie Thompson45742032010-02-15 06:46:04 -0800717static int dlfb_ops_ioctl(struct fb_info *info, unsigned int cmd,
Bernie Thompson530f43a2010-02-15 06:46:21 -0800718 unsigned long arg)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700719{
Bernie Thompson530f43a2010-02-15 06:46:21 -0800720
721 struct dlfb_data *dev = info->par;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700722 struct dloarea *area = NULL;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700723
Bernie Thompson530f43a2010-02-15 06:46:21 -0800724 if (!atomic_read(&dev->usb_active))
725 return 0;
726
727 /* TODO: Update X server to get this from sysfs instead */
728 if (cmd == DLFB_IOCTL_RETURN_EDID) {
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700729 char *edid = (char *)arg;
Bernie Thompson530f43a2010-02-15 06:46:21 -0800730 dlfb_get_edid(dev);
731 if (copy_to_user(edid, dev->edid, sizeof(dev->edid)))
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700732 return -EFAULT;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700733 return 0;
734 }
735
Bernie Thompson530f43a2010-02-15 06:46:21 -0800736 /* TODO: Help propose a standard fb.h ioctl to report mmap damage */
737 if (cmd == DLFB_IOCTL_REPORT_DAMAGE) {
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700738
739 area = (struct dloarea *)arg;
740
741 if (area->x < 0)
742 area->x = 0;
743
744 if (area->x > info->var.xres)
745 area->x = info->var.xres;
746
747 if (area->y < 0)
748 area->y = 0;
749
750 if (area->y > info->var.yres)
751 area->y = info->var.yres;
752
Bernie Thompson530f43a2010-02-15 06:46:21 -0800753 atomic_set(&dev->use_defio, 0);
754
755 dlfb_handle_damage(dev, area->x, area->y, area->w, area->h,
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700756 info->screen_base);
Bernie Thompson530f43a2010-02-15 06:46:21 -0800757 atomic_inc(&dev->damage_count);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700758 }
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700759
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700760 return 0;
761}
762
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700763/* taken from vesafb */
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700764static int
Bernie Thompson45742032010-02-15 06:46:04 -0800765dlfb_ops_setcolreg(unsigned regno, unsigned red, unsigned green,
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700766 unsigned blue, unsigned transp, struct fb_info *info)
767{
768 int err = 0;
769
770 if (regno >= info->cmap.len)
771 return 1;
772
773 if (regno < 16) {
774 if (info->var.red.offset == 10) {
775 /* 1:5:5:5 */
776 ((u32 *) (info->pseudo_palette))[regno] =
777 ((red & 0xf800) >> 1) |
778 ((green & 0xf800) >> 6) | ((blue & 0xf800) >> 11);
779 } else {
780 /* 0:5:6:5 */
781 ((u32 *) (info->pseudo_palette))[regno] =
782 ((red & 0xf800)) |
783 ((green & 0xfc00) >> 5) | ((blue & 0xf800) >> 11);
784 }
785 }
786
787 return err;
788}
789
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800790/*
791 * It's common for several clients to have framebuffer open simultaneously.
792 * e.g. both fbcon and X. Makes things interesting.
793 */
794static int dlfb_ops_open(struct fb_info *info, int user)
795{
796 struct dlfb_data *dev = info->par;
797
798/* if (user == 0)
799 * We could special case kernel mode clients (fbcon) here
800 */
801
802 mutex_lock(&dev->fb_open_lock);
803
804 dev->fb_count++;
805
806#ifdef CONFIG_FB_DEFERRED_IO
807 if ((atomic_read(&dev->use_defio)) && (info->fbdefio == NULL)) {
808 /* enable defio */
809 info->fbdefio = &dlfb_defio;
810 fb_deferred_io_init(info);
811 }
812#endif
813
814 dl_notice("open /dev/fb%d user=%d fb_info=%p count=%d\n",
815 info->node, user, info, dev->fb_count);
816
817 mutex_unlock(&dev->fb_open_lock);
818
819 return 0;
820}
821
Bernie Thompson45742032010-02-15 06:46:04 -0800822static int dlfb_ops_release(struct fb_info *info, int user)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700823{
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800824 struct dlfb_data *dev = info->par;
825
826 mutex_lock(&dev->fb_open_lock);
827
828 dev->fb_count--;
829
830#ifdef CONFIG_FB_DEFERRED_IO
831 if ((dev->fb_count == 0) && (info->fbdefio)) {
832 fb_deferred_io_cleanup(info);
833 info->fbdefio = NULL;
834 info->fbops->fb_mmap = dlfb_ops_mmap;
835 }
836#endif
837
838 dl_notice("release /dev/fb%d user=%d count=%d\n",
839 info->node, user, dev->fb_count);
840
841 mutex_unlock(&dev->fb_open_lock);
842
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700843 return 0;
844}
845
Bernie Thompson4a4854d2010-02-15 06:45:55 -0800846/*
847 * Called when all client interfaces to start transactions have been disabled,
848 * and all references to our device instance (dlfb_data) are released.
849 * Every transaction must have a reference, so we know are fully spun down
850 */
851static void dlfb_delete(struct kref *kref)
852{
853 struct dlfb_data *dev = container_of(kref, struct dlfb_data, kref);
854
855 if (dev->backing_buffer)
856 vfree(dev->backing_buffer);
857
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800858 mutex_destroy(&dev->fb_open_lock);
859
Bernie Thompson4a4854d2010-02-15 06:45:55 -0800860 kfree(dev);
861}
862
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800863/*
Bernie Thompson2469d5d2010-02-15 06:46:13 -0800864 * Called by fbdev as last part of unregister_framebuffer() process
865 * No new clients can open connections. Deallocate everything fb_info.
866 */
867static void dlfb_ops_destroy(struct fb_info *info)
868{
869 struct dlfb_data *dev = info->par;
870
871 if (info->cmap.len != 0)
872 fb_dealloc_cmap(&info->cmap);
873 if (info->monspecs.modedb)
874 fb_destroy_modedb(info->monspecs.modedb);
875 if (info->screen_base)
876 vfree(info->screen_base);
877
878 fb_destroy_modelist(&info->modelist);
879
880 framebuffer_release(info);
881
882 /* ref taken before register_framebuffer() for dlfb_data clients */
883 kref_put(&dev->kref, dlfb_delete);
884}
885
886/*
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800887 * Check whether a video mode is supported by the DisplayLink chip
888 * We start from monitor's modes, so don't need to filter that here
889 */
890static int dlfb_is_valid_mode(struct fb_videomode *mode,
891 struct fb_info *info)
892{
893 struct dlfb_data *dev = info->par;
894
895 if (mode->xres * mode->yres > dev->sku_pixel_limit)
896 return 0;
897
898 return 1;
899}
900
901static void dlfb_var_color_format(struct fb_var_screeninfo *var)
902{
903 const struct fb_bitfield red = { 11, 5, 0 };
904 const struct fb_bitfield green = { 5, 6, 0 };
905 const struct fb_bitfield blue = { 0, 5, 0 };
906
907 var->bits_per_pixel = 16;
908 var->red = red;
909 var->green = green;
910 var->blue = blue;
911}
912
Bernie Thompson2469d5d2010-02-15 06:46:13 -0800913static int dlfb_ops_check_var(struct fb_var_screeninfo *var,
914 struct fb_info *info)
915{
916 struct fb_videomode mode;
917
918 /* TODO: support dynamically changing framebuffer size */
919 if ((var->xres * var->yres * 2) > info->fix.smem_len)
920 return -EINVAL;
921
922 /* set device-specific elements of var unrelated to mode */
923 dlfb_var_color_format(var);
924
925 fb_var_to_videomode(&mode, var);
926
927 if (!dlfb_is_valid_mode(&mode, info))
928 return -EINVAL;
929
930 return 0;
931}
932
933static int dlfb_ops_set_par(struct fb_info *info)
934{
935 struct dlfb_data *dev = info->par;
936
937 dl_notice("set_par mode %dx%d\n", info->var.xres, info->var.yres);
938
939 return dlfb_set_video_mode(dev, &info->var);
940}
941
Bernie Thompson9825f702010-09-05 16:35:10 -0700942/*
943 * In order to come back from full DPMS off, we need to set the mode again
944 */
Bernie Thompson45742032010-02-15 06:46:04 -0800945static int dlfb_ops_blank(int blank_mode, struct fb_info *info)
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700946{
Bernie Thompson530f43a2010-02-15 06:46:21 -0800947 struct dlfb_data *dev = info->par;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700948
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700949 if (blank_mode != FB_BLANK_UNBLANK) {
Bernie Thompson9825f702010-09-05 16:35:10 -0700950 char *bufptr;
951 struct urb *urb;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700952
Bernie Thompson9825f702010-09-05 16:35:10 -0700953 urb = dlfb_get_urb(dev);
954 if (!urb)
955 return 0;
956
957 bufptr = (char *) urb->transfer_buffer;
958 bufptr = dlfb_vidreg_lock(bufptr);
959 bufptr = dlfb_enable_hvsync(bufptr, false);
960 bufptr = dlfb_vidreg_unlock(bufptr);
961
962 dlfb_submit_urb(dev, urb, bufptr -
963 (char *) urb->transfer_buffer);
964 } else {
965 dlfb_set_video_mode(dev, &info->var);
966 }
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700967
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700968 return 0;
969}
970
971static struct fb_ops dlfb_ops = {
Bernie Thompson2469d5d2010-02-15 06:46:13 -0800972 .owner = THIS_MODULE,
Bernie Thompsond46ecb92010-09-05 16:35:04 -0700973 .fb_read = dlfb_ops_read,
974 .fb_write = dlfb_ops_write,
Bernie Thompson45742032010-02-15 06:46:04 -0800975 .fb_setcolreg = dlfb_ops_setcolreg,
976 .fb_fillrect = dlfb_ops_fillrect,
977 .fb_copyarea = dlfb_ops_copyarea,
978 .fb_imageblit = dlfb_ops_imageblit,
979 .fb_mmap = dlfb_ops_mmap,
980 .fb_ioctl = dlfb_ops_ioctl,
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800981 .fb_open = dlfb_ops_open,
Bernie Thompson45742032010-02-15 06:46:04 -0800982 .fb_release = dlfb_ops_release,
983 .fb_blank = dlfb_ops_blank,
Bernie Thompson2469d5d2010-02-15 06:46:13 -0800984 .fb_check_var = dlfb_ops_check_var,
985 .fb_set_par = dlfb_ops_set_par,
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700986};
987
Bernie Thompsoncc403dc2010-02-15 06:45:49 -0800988/*
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800989 * Calls dlfb_get_edid() to query the EDID of attached monitor via usb cmds
990 * Then parses EDID into three places used by various parts of fbdev:
991 * fb_var_screeninfo contains the timing of the monitor's preferred mode
992 * fb_info.monspecs is full parsed EDID info, including monspecs.modedb
993 * fb_info.modelist is a linked list of all monitor & VESA modes which work
994 *
995 * If EDID is not readable/valid, then modelist is all VESA modes,
996 * monspecs is NULL, and fb_var_screeninfo is set to safe VESA mode
997 * Returns 0 if EDID parses successfully
998 */
999static int dlfb_parse_edid(struct dlfb_data *dev,
1000 struct fb_var_screeninfo *var,
1001 struct fb_info *info)
1002{
1003 int i;
1004 const struct fb_videomode *default_vmode = NULL;
1005 int result = 0;
1006
1007 fb_destroy_modelist(&info->modelist);
1008 memset(&info->monspecs, 0, sizeof(info->monspecs));
1009
1010 dlfb_get_edid(dev);
1011 fb_edid_to_monspecs(dev->edid, &info->monspecs);
1012
1013 if (info->monspecs.modedb_len > 0) {
1014
1015 for (i = 0; i < info->monspecs.modedb_len; i++) {
1016 if (dlfb_is_valid_mode(&info->monspecs.modedb[i], info))
1017 fb_add_videomode(&info->monspecs.modedb[i],
1018 &info->modelist);
1019 }
1020
1021 default_vmode = fb_find_best_display(&info->monspecs,
1022 &info->modelist);
1023 } else {
1024 struct fb_videomode fb_vmode = {0};
1025
1026 dl_err("Unable to get valid EDID from device/display\n");
1027 result = 1;
1028
1029 /*
1030 * Add the standard VESA modes to our modelist
1031 * Since we don't have EDID, there may be modes that
1032 * overspec monitor and/or are incorrect aspect ratio, etc.
1033 * But at least the user has a chance to choose
1034 */
1035 for (i = 0; i < VESA_MODEDB_SIZE; i++) {
1036 if (dlfb_is_valid_mode((struct fb_videomode *)
1037 &vesa_modes[i], info))
1038 fb_add_videomode(&vesa_modes[i],
1039 &info->modelist);
1040 }
1041
1042 /*
1043 * default to resolution safe for projectors
1044 * (since they are most common case without EDID)
1045 */
1046 fb_vmode.xres = 800;
1047 fb_vmode.yres = 600;
1048 fb_vmode.refresh = 60;
1049 default_vmode = fb_find_nearest_mode(&fb_vmode,
1050 &info->modelist);
1051 }
1052
1053 fb_videomode_to_var(var, default_vmode);
1054 dlfb_var_color_format(var);
1055
1056 return result;
1057}
1058
1059static ssize_t metrics_bytes_rendered_show(struct device *fbdev,
1060 struct device_attribute *a, char *buf) {
1061 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1062 struct dlfb_data *dev = fb_info->par;
1063 return snprintf(buf, PAGE_SIZE, "%u\n",
1064 atomic_read(&dev->bytes_rendered));
1065}
1066
1067static ssize_t metrics_bytes_identical_show(struct device *fbdev,
1068 struct device_attribute *a, char *buf) {
1069 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1070 struct dlfb_data *dev = fb_info->par;
1071 return snprintf(buf, PAGE_SIZE, "%u\n",
1072 atomic_read(&dev->bytes_identical));
1073}
1074
1075static ssize_t metrics_bytes_sent_show(struct device *fbdev,
1076 struct device_attribute *a, char *buf) {
1077 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1078 struct dlfb_data *dev = fb_info->par;
1079 return snprintf(buf, PAGE_SIZE, "%u\n",
1080 atomic_read(&dev->bytes_sent));
1081}
1082
1083static ssize_t metrics_cpu_kcycles_used_show(struct device *fbdev,
1084 struct device_attribute *a, char *buf) {
1085 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1086 struct dlfb_data *dev = fb_info->par;
1087 return snprintf(buf, PAGE_SIZE, "%u\n",
1088 atomic_read(&dev->cpu_kcycles_used));
1089}
1090
1091static ssize_t metrics_misc_show(struct device *fbdev,
1092 struct device_attribute *a, char *buf) {
1093 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1094 struct dlfb_data *dev = fb_info->par;
1095 return snprintf(buf, PAGE_SIZE,
1096 "Calls to\ndamage: %u\nblit: %u\n"
1097 "defio faults: %u\ncopy: %u\n"
1098 "fill: %u\n\n"
1099 "active framebuffer clients: %d\n"
1100 "urbs available %d(%d)\n"
1101 "Shadow framebuffer in use? %s\n"
1102 "Any lost pixels? %s\n",
1103 atomic_read(&dev->damage_count),
1104 atomic_read(&dev->blit_count),
1105 atomic_read(&dev->defio_fault_count),
1106 atomic_read(&dev->copy_count),
1107 atomic_read(&dev->fill_count),
1108 dev->fb_count,
1109 dev->urbs.available, dev->urbs.limit_sem.count,
1110 (dev->backing_buffer) ? "yes" : "no",
1111 atomic_read(&dev->lost_pixels) ? "yes" : "no");
1112}
1113
Chris Wright2c3c8be2010-05-12 18:28:57 -07001114static ssize_t edid_show(struct file *filp, struct kobject *kobj,
1115 struct bin_attribute *a,
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001116 char *buf, loff_t off, size_t count) {
1117 struct device *fbdev = container_of(kobj, struct device, kobj);
1118 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1119 struct dlfb_data *dev = fb_info->par;
1120 char *edid = &dev->edid[0];
1121 const size_t size = sizeof(dev->edid);
1122
1123 if (dlfb_parse_edid(dev, &fb_info->var, fb_info))
1124 return 0;
1125
1126 if (off >= size)
1127 return 0;
1128
1129 if (off + count > size)
1130 count = size - off;
1131 memcpy(buf, edid + off, count);
1132
1133 return count;
1134}
1135
1136
1137static ssize_t metrics_reset_store(struct device *fbdev,
1138 struct device_attribute *attr,
1139 const char *buf, size_t count)
1140{
1141 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1142 struct dlfb_data *dev = fb_info->par;
1143
1144 atomic_set(&dev->bytes_rendered, 0);
1145 atomic_set(&dev->bytes_identical, 0);
1146 atomic_set(&dev->bytes_sent, 0);
1147 atomic_set(&dev->cpu_kcycles_used, 0);
1148 atomic_set(&dev->blit_count, 0);
1149 atomic_set(&dev->copy_count, 0);
1150 atomic_set(&dev->fill_count, 0);
1151 atomic_set(&dev->defio_fault_count, 0);
1152 atomic_set(&dev->damage_count, 0);
1153
1154 return count;
1155}
1156
1157static ssize_t use_defio_show(struct device *fbdev,
1158 struct device_attribute *a, char *buf) {
1159 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1160 struct dlfb_data *dev = fb_info->par;
1161 return snprintf(buf, PAGE_SIZE, "%d\n",
1162 atomic_read(&dev->use_defio));
1163}
1164
1165static ssize_t use_defio_store(struct device *fbdev,
1166 struct device_attribute *attr,
1167 const char *buf, size_t count)
1168{
1169 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1170 struct dlfb_data *dev = fb_info->par;
1171
1172 if (count > 0) {
1173 if (buf[0] == '0')
1174 atomic_set(&dev->use_defio, 0);
1175 if (buf[0] == '1')
1176 atomic_set(&dev->use_defio, 1);
1177 }
1178 return count;
1179}
1180
1181static struct bin_attribute edid_attr = {
1182 .attr.name = "edid",
1183 .attr.mode = 0444,
1184 .size = 128,
1185 .read = edid_show,
1186};
1187
1188static struct device_attribute fb_device_attrs[] = {
1189 __ATTR_RO(metrics_bytes_rendered),
1190 __ATTR_RO(metrics_bytes_identical),
1191 __ATTR_RO(metrics_bytes_sent),
1192 __ATTR_RO(metrics_cpu_kcycles_used),
1193 __ATTR_RO(metrics_misc),
1194 __ATTR(metrics_reset, S_IWUGO, NULL, metrics_reset_store),
1195 __ATTR_RW(use_defio),
1196};
1197
Bernie Thompson3e8f3d62010-02-15 06:46:26 -08001198#ifdef CONFIG_FB_DEFERRED_IO
1199static void dlfb_dpy_deferred_io(struct fb_info *info,
1200 struct list_head *pagelist)
1201{
1202 struct page *cur;
1203 struct fb_deferred_io *fbdefio = info->fbdefio;
1204 struct dlfb_data *dev = info->par;
1205 struct urb *urb;
1206 char *cmd;
1207 cycles_t start_cycles, end_cycles;
1208 int bytes_sent = 0;
1209 int bytes_identical = 0;
1210 int bytes_rendered = 0;
1211 int fault_count = 0;
1212
1213 if (!atomic_read(&dev->use_defio))
1214 return;
1215
1216 if (!atomic_read(&dev->usb_active))
1217 return;
1218
1219 start_cycles = get_cycles();
1220
1221 urb = dlfb_get_urb(dev);
1222 if (!urb)
1223 return;
1224 cmd = urb->transfer_buffer;
1225
1226 /* walk the written page list and render each to device */
1227 list_for_each_entry(cur, &fbdefio->pagelist, lru) {
1228 dlfb_render_hline(dev, &urb, (char *) info->fix.smem_start,
1229 &cmd, cur->index << PAGE_SHIFT,
1230 PAGE_SIZE, &bytes_identical, &bytes_sent);
1231 bytes_rendered += PAGE_SIZE;
1232 fault_count++;
1233 }
1234
1235 if (cmd > (char *) urb->transfer_buffer) {
1236 /* Send partial buffer remaining before exiting */
1237 int len = cmd - (char *) urb->transfer_buffer;
1238 dlfb_submit_urb(dev, urb, len);
1239 bytes_sent += len;
1240 } else
1241 dlfb_urb_completion(urb);
1242
1243 atomic_add(fault_count, &dev->defio_fault_count);
1244 atomic_add(bytes_sent, &dev->bytes_sent);
1245 atomic_add(bytes_identical, &dev->bytes_identical);
1246 atomic_add(bytes_rendered, &dev->bytes_rendered);
1247 end_cycles = get_cycles();
1248 atomic_add(((unsigned int) ((end_cycles - start_cycles)
1249 >> 10)), /* Kcycles */
1250 &dev->cpu_kcycles_used);
1251}
1252
1253static struct fb_deferred_io dlfb_defio = {
1254 .delay = 5,
1255 .deferred_io = dlfb_dpy_deferred_io,
1256};
1257
1258#endif
1259
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001260/*
Bernie Thompsoncc403dc2010-02-15 06:45:49 -08001261 * This is necessary before we can communicate with the display controller.
1262 */
1263static int dlfb_select_std_channel(struct dlfb_data *dev)
1264{
1265 int ret;
1266 u8 set_def_chn[] = { 0x57, 0xCD, 0xDC, 0xA7,
1267 0x1C, 0x88, 0x5E, 0x15,
1268 0x60, 0xFE, 0xC6, 0x97,
1269 0x16, 0x3D, 0x47, 0xF2 };
1270
1271 ret = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
1272 NR_USB_REQUEST_CHANNEL,
1273 (USB_DIR_OUT | USB_TYPE_VENDOR), 0, 0,
1274 set_def_chn, sizeof(set_def_chn), USB_CTRL_SET_TIMEOUT);
1275 return ret;
1276}
1277
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001278
1279static int dlfb_usb_probe(struct usb_interface *interface,
Bernie Thompson59277b62009-11-24 15:52:21 -08001280 const struct usb_device_id *id)
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001281{
Bernie Thompson59277b62009-11-24 15:52:21 -08001282 struct usb_device *usbdev;
1283 struct dlfb_data *dev;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001284 struct fb_info *info;
Bernie Thompson59277b62009-11-24 15:52:21 -08001285 int videomemorysize;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001286 int i;
Bernie Thompson59277b62009-11-24 15:52:21 -08001287 unsigned char *videomemory;
1288 int retval = -ENOMEM;
1289 struct fb_var_screeninfo *var;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001290 int registered = 0;
1291 u16 *pix_framebuffer;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001292
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001293 /* usb initialization */
1294
1295 usbdev = interface_to_usbdev(interface);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001296
Bernie Thompson59277b62009-11-24 15:52:21 -08001297 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1298 if (dev == NULL) {
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001299 err("dlfb_usb_probe: failed alloc of dev struct\n");
1300 goto error;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001301 }
1302
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001303 /* we need to wait for both usb and fbdev to spin down on disconnect */
1304 kref_init(&dev->kref); /* matching kref_put in usb .disconnect fn */
1305 kref_get(&dev->kref); /* matching kref_put in .fb_destroy function*/
1306
Bernie Thompson59277b62009-11-24 15:52:21 -08001307 dev->udev = usbdev;
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001308 dev->gdev = &usbdev->dev; /* our generic struct device * */
Bernie Thompson59277b62009-11-24 15:52:21 -08001309 usb_set_intfdata(interface, dev);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001310
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001311 if (!dlfb_alloc_urb_list(dev, WRITES_IN_FLIGHT, MAX_TRANSFER)) {
1312 retval = -ENOMEM;
1313 dl_err("dlfb_alloc_urb_list failed\n");
1314 goto error;
1315 }
1316
1317 mutex_init(&dev->fb_open_lock);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001318
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001319 /* We don't register a new USB class. Our client interface is fbdev */
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001320
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001321 /* allocates framebuffer driver structure, not framebuffer memory */
1322 info = framebuffer_alloc(0, &usbdev->dev);
1323 if (!info) {
1324 retval = -ENOMEM;
1325 dl_err("framebuffer_alloc failed\n");
1326 goto error;
1327 }
Bernie Thompson59277b62009-11-24 15:52:21 -08001328 dev->info = info;
1329 info->par = dev;
1330 info->pseudo_palette = dev->pseudo_palette;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001331 info->fbops = &dlfb_ops;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001332
Bernie Thompson59277b62009-11-24 15:52:21 -08001333 var = &info->var;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001334
1335 /* TODO set limit based on actual SKU detection */
1336 dev->sku_pixel_limit = 2048 * 1152;
1337
1338 INIT_LIST_HEAD(&info->modelist);
1339 dlfb_parse_edid(dev, var, info);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001340
Bernie Thompson59277b62009-11-24 15:52:21 -08001341 /*
1342 * ok, now that we've got the size info, we can alloc our framebuffer.
Bernie Thompson59277b62009-11-24 15:52:21 -08001343 */
Bernie Thompson59277b62009-11-24 15:52:21 -08001344 info->fix = dlfb_fix;
1345 info->fix.line_length = var->xres * (var->bits_per_pixel / 8);
1346 videomemorysize = info->fix.line_length * var->yres;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001347
Bernie Thompson59277b62009-11-24 15:52:21 -08001348 /*
1349 * The big chunk of system memory we use as a virtual framebuffer.
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001350 * TODO: Handle fbcon cursor code calling blit in interrupt context
Bernie Thompson59277b62009-11-24 15:52:21 -08001351 */
1352 videomemory = vmalloc(videomemorysize);
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001353 if (!videomemory) {
1354 retval = -ENOMEM;
1355 dl_err("Virtual framebuffer alloc failed\n");
1356 goto error;
1357 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001358
Bernie Thompson59277b62009-11-24 15:52:21 -08001359 info->screen_base = videomemory;
1360 info->fix.smem_len = PAGE_ALIGN(videomemorysize);
1361 info->fix.smem_start = (unsigned long) videomemory;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001362 info->flags = udlfb_info_flags;
1363
Bernie Thompson59277b62009-11-24 15:52:21 -08001364
1365 /*
1366 * Second framebuffer copy, mirroring the state of the framebuffer
1367 * on the physical USB device. We can function without this.
1368 * But with imperfect damage info we may end up sending pixels over USB
1369 * that were, in fact, unchanged -- wasting limited USB bandwidth
1370 */
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001371 dev->backing_buffer = vmalloc(videomemorysize);
Bernie Thompson59277b62009-11-24 15:52:21 -08001372 if (!dev->backing_buffer)
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001373 dl_warn("No shadow/backing buffer allcoated\n");
1374 else
1375 memset(dev->backing_buffer, 0, videomemorysize);
Bernie Thompson59277b62009-11-24 15:52:21 -08001376
1377 retval = fb_alloc_cmap(&info->cmap, 256, 0);
1378 if (retval < 0) {
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001379 dl_err("fb_alloc_cmap failed %x\n", retval);
1380 goto error;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001381 }
1382
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001383 /* ready to begin using device */
1384
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001385#ifdef CONFIG_FB_DEFERRED_IO
1386 atomic_set(&dev->use_defio, 1);
1387#endif
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001388 atomic_set(&dev->usb_active, 1);
Bernie Thompson59277b62009-11-24 15:52:21 -08001389 dlfb_select_std_channel(dev);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001390
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001391 dlfb_ops_check_var(var, info);
1392 dlfb_ops_set_par(info);
1393
1394 /* paint greenscreen */
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001395 pix_framebuffer = (u16 *) videomemory;
1396 for (i = 0; i < videomemorysize / 2; i++)
1397 pix_framebuffer[i] = 0x37e6;
1398
1399 dlfb_handle_damage(dev, 0, 0, info->var.xres, info->var.yres,
1400 videomemory);
Bernie Thompson530f43a2010-02-15 06:46:21 -08001401
Bernie Thompson59277b62009-11-24 15:52:21 -08001402 retval = register_framebuffer(info);
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001403 if (retval < 0) {
1404 dl_err("register_framebuffer failed %d\n", retval);
1405 goto error;
1406 }
1407 registered = 1;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001408
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001409 for (i = 0; i < ARRAY_SIZE(fb_device_attrs); i++)
1410 device_create_file(info->dev, &fb_device_attrs[i]);
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -07001411
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001412 device_create_bin_file(info->dev, &edid_attr);
1413
1414 dl_err("DisplayLink USB device /dev/fb%d attached. %dx%d resolution."
1415 " Using %dK framebuffer memory\n", info->node,
1416 var->xres, var->yres,
1417 ((dev->backing_buffer) ?
1418 videomemorysize * 2 : videomemorysize) >> 10);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001419 return 0;
1420
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001421error:
1422 if (dev) {
1423 if (registered) {
1424 unregister_framebuffer(info);
1425 dlfb_ops_destroy(info);
1426 } else
1427 kref_put(&dev->kref, dlfb_delete);
1428
1429 if (dev->urbs.count > 0)
1430 dlfb_free_urb_list(dev);
1431 kref_put(&dev->kref, dlfb_delete); /* last ref from kref_init */
1432
1433 /* dev has been deallocated. Do not dereference */
1434 }
1435
Bernie Thompson59277b62009-11-24 15:52:21 -08001436 return retval;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001437}
1438
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001439static void dlfb_usb_disconnect(struct usb_interface *interface)
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001440{
Bernie Thompson59277b62009-11-24 15:52:21 -08001441 struct dlfb_data *dev;
1442 struct fb_info *info;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001443 int i;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001444
Bernie Thompson59277b62009-11-24 15:52:21 -08001445 dev = usb_get_intfdata(interface);
Bernie Thompson59277b62009-11-24 15:52:21 -08001446 info = dev->info;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001447
1448 /* when non-active we'll update virtual framebuffer, but no new urbs */
1449 atomic_set(&dev->usb_active, 0);
1450
1451 usb_set_intfdata(interface, NULL);
1452
1453 for (i = 0; i < ARRAY_SIZE(fb_device_attrs); i++)
1454 device_remove_file(info->dev, &fb_device_attrs[i]);
1455
1456 device_remove_bin_file(info->dev, &edid_attr);
1457
1458 /* this function will wait for all in-flight urbs to complete */
1459 dlfb_free_urb_list(dev);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001460
Bernie Thompson59277b62009-11-24 15:52:21 -08001461 if (info) {
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001462 dl_notice("Detaching /dev/fb%d\n", info->node);
Bernie Thompson59277b62009-11-24 15:52:21 -08001463 unregister_framebuffer(info);
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001464 dlfb_ops_destroy(info);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001465 }
1466
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001467 /* release reference taken by kref_init in probe() */
1468 kref_put(&dev->kref, dlfb_delete);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001469
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001470 /* consider dlfb_data freed */
1471
1472 return;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001473}
1474
1475static struct usb_driver dlfb_driver = {
1476 .name = "udlfb",
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001477 .probe = dlfb_usb_probe,
1478 .disconnect = dlfb_usb_disconnect,
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001479 .id_table = id_table,
1480};
1481
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001482static int __init dlfb_module_init(void)
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001483{
1484 int res;
1485
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001486 res = usb_register(&dlfb_driver);
1487 if (res)
1488 err("usb_register failed. Error number %d", res);
1489
Soeren Moellerb5a21042010-05-14 19:03:00 +00001490 printk(KERN_INFO "VMODES initialized\n");
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001491
1492 return res;
1493}
1494
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001495static void __exit dlfb_module_exit(void)
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001496{
1497 usb_deregister(&dlfb_driver);
1498}
1499
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001500module_init(dlfb_module_init);
1501module_exit(dlfb_module_exit);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001502
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001503static void dlfb_urb_completion(struct urb *urb)
1504{
1505 struct urb_node *unode = urb->context;
1506 struct dlfb_data *dev = unode->dev;
1507 unsigned long flags;
1508
1509 /* sync/async unlink faults aren't errors */
1510 if (urb->status) {
1511 if (!(urb->status == -ENOENT ||
1512 urb->status == -ECONNRESET ||
1513 urb->status == -ESHUTDOWN)) {
1514 dl_err("%s - nonzero write bulk status received: %d\n",
1515 __func__, urb->status);
1516 atomic_set(&dev->lost_pixels, 1);
1517 }
1518 }
1519
1520 urb->transfer_buffer_length = dev->urbs.size; /* reset to actual */
1521
1522 spin_lock_irqsave(&dev->urbs.lock, flags);
1523 list_add_tail(&unode->entry, &dev->urbs.list);
1524 dev->urbs.available++;
1525 spin_unlock_irqrestore(&dev->urbs.lock, flags);
1526
1527 up(&dev->urbs.limit_sem);
1528}
1529
1530static void dlfb_free_urb_list(struct dlfb_data *dev)
1531{
1532 int count = dev->urbs.count;
1533 struct list_head *node;
1534 struct urb_node *unode;
1535 struct urb *urb;
1536 int ret;
1537 unsigned long flags;
1538
1539 dl_notice("Waiting for completes and freeing all render urbs\n");
1540
1541 /* keep waiting and freeing, until we've got 'em all */
1542 while (count--) {
1543 /* Timeout means a memory leak and/or fault */
1544 ret = down_timeout(&dev->urbs.limit_sem, FREE_URB_TIMEOUT);
1545 if (ret) {
1546 BUG_ON(ret);
1547 break;
1548 }
1549 spin_lock_irqsave(&dev->urbs.lock, flags);
1550
1551 node = dev->urbs.list.next; /* have reserved one with sem */
1552 list_del_init(node);
1553
1554 spin_unlock_irqrestore(&dev->urbs.lock, flags);
1555
1556 unode = list_entry(node, struct urb_node, entry);
1557 urb = unode->urb;
1558
1559 /* Free each separately allocated piece */
Greg Kroah-Hartmanc220cc32010-04-29 15:36:29 -07001560 usb_free_coherent(urb->dev, dev->urbs.size,
1561 urb->transfer_buffer, urb->transfer_dma);
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001562 usb_free_urb(urb);
1563 kfree(node);
1564 }
1565
1566 kref_put(&dev->kref, dlfb_delete);
1567
1568}
1569
1570static int dlfb_alloc_urb_list(struct dlfb_data *dev, int count, size_t size)
1571{
1572 int i = 0;
1573 struct urb *urb;
1574 struct urb_node *unode;
1575 char *buf;
1576
1577 spin_lock_init(&dev->urbs.lock);
1578
1579 dev->urbs.size = size;
1580 INIT_LIST_HEAD(&dev->urbs.list);
1581
1582 while (i < count) {
1583 unode = kzalloc(sizeof(struct urb_node), GFP_KERNEL);
1584 if (!unode)
1585 break;
1586 unode->dev = dev;
1587
1588 urb = usb_alloc_urb(0, GFP_KERNEL);
1589 if (!urb) {
1590 kfree(unode);
1591 break;
1592 }
1593 unode->urb = urb;
1594
Greg Kroah-Hartmanc220cc32010-04-29 15:36:29 -07001595 buf = usb_alloc_coherent(dev->udev, MAX_TRANSFER, GFP_KERNEL,
1596 &urb->transfer_dma);
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001597 if (!buf) {
1598 kfree(unode);
1599 usb_free_urb(urb);
1600 break;
1601 }
1602
1603 /* urb->transfer_buffer_length set to actual before submit */
1604 usb_fill_bulk_urb(urb, dev->udev, usb_sndbulkpipe(dev->udev, 1),
1605 buf, size, dlfb_urb_completion, unode);
1606 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1607
1608 list_add_tail(&unode->entry, &dev->urbs.list);
1609
1610 i++;
1611 }
1612
1613 sema_init(&dev->urbs.limit_sem, i);
1614 dev->urbs.count = i;
1615 dev->urbs.available = i;
1616
1617 kref_get(&dev->kref); /* released in free_render_urbs() */
1618
Soeren Moellerb5a21042010-05-14 19:03:00 +00001619 dl_notice("allocated %d %d byte urbs\n", i, (int) size);
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001620
1621 return i;
1622}
1623
1624static struct urb *dlfb_get_urb(struct dlfb_data *dev)
1625{
1626 int ret = 0;
1627 struct list_head *entry;
1628 struct urb_node *unode;
1629 struct urb *urb = NULL;
1630 unsigned long flags;
1631
1632 /* Wait for an in-flight buffer to complete and get re-queued */
1633 ret = down_timeout(&dev->urbs.limit_sem, GET_URB_TIMEOUT);
1634 if (ret) {
1635 atomic_set(&dev->lost_pixels, 1);
1636 dl_err("wait for urb interrupted: %x\n", ret);
1637 goto error;
1638 }
1639
1640 spin_lock_irqsave(&dev->urbs.lock, flags);
1641
1642 BUG_ON(list_empty(&dev->urbs.list)); /* reserved one with limit_sem */
1643 entry = dev->urbs.list.next;
1644 list_del_init(entry);
1645 dev->urbs.available--;
1646
1647 spin_unlock_irqrestore(&dev->urbs.lock, flags);
1648
1649 unode = list_entry(entry, struct urb_node, entry);
1650 urb = unode->urb;
1651
1652error:
1653 return urb;
1654}
1655
1656static int dlfb_submit_urb(struct dlfb_data *dev, struct urb *urb, size_t len)
1657{
1658 int ret;
1659
1660 BUG_ON(len > dev->urbs.size);
1661
1662 urb->transfer_buffer_length = len; /* set to actual payload len */
1663 ret = usb_submit_urb(urb, GFP_KERNEL);
1664 if (ret) {
1665 dlfb_urb_completion(urb); /* because no one else will */
1666 atomic_set(&dev->lost_pixels, 1);
1667 dl_err("usb_submit_urb error %x\n", ret);
1668 }
1669 return ret;
1670}
1671
Bernie Thompson59277b62009-11-24 15:52:21 -08001672MODULE_AUTHOR("Roberto De Ioris <roberto@unbit.it>, "
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001673 "Jaya Kumar <jayakumar.lkml@gmail.com>, "
1674 "Bernie Thompson <bernie@plugable.com>");
1675MODULE_DESCRIPTION("DisplayLink kernel framebuffer driver");
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001676MODULE_LICENSE("GPL");
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001677