blob: 4e133754ba2ba04c16b22e5fe17293f93d1eea95 [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
Paul Mundt81f6f3c2011-01-06 18:07:54 +090019#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
Roberto De Ioris88e58b12009-06-03 14:03:06 -070021#include <linux/module.h>
22#include <linux/kernel.h>
23#include <linux/init.h>
24#include <linux/usb.h>
25#include <linux/uaccess.h>
26#include <linux/mm.h>
27#include <linux/fb.h>
Amit Kucheriafb299002009-07-27 12:01:03 +030028#include <linux/vmalloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Linus Torvalds268bb0c2011-05-20 12:50:29 -070030#include <linux/prefetch.h>
Bernie Thompson33077b82010-09-05 16:35:19 -070031#include <linux/delay.h>
Stephen Rothwell12e572d2011-05-20 16:29:01 +100032#include <linux/prefetch.h>
Paul Mundt96f8d862010-11-16 14:00:24 +090033#include <video/udlfb.h>
Paul Mundtb9f03a32011-01-06 18:04:02 +090034#include "edid.h"
Roberto De Ioris88e58b12009-06-03 14:03:06 -070035
Bernie Thompson59277b62009-11-24 15:52:21 -080036static struct fb_fix_screeninfo dlfb_fix = {
Bernie Thompson2469d5d2010-02-15 06:46:13 -080037 .id = "udlfb",
Bernie Thompson1d31a9e2010-02-15 06:45:43 -080038 .type = FB_TYPE_PACKED_PIXELS,
39 .visual = FB_VISUAL_TRUECOLOR,
40 .xpanstep = 0,
41 .ypanstep = 0,
42 .ywrapstep = 0,
43 .accel = FB_ACCEL_NONE,
Bernie Thompson59277b62009-11-24 15:52:21 -080044};
Roberto De Ioris88e58b12009-06-03 14:03:06 -070045
Bernie Thompson2469d5d2010-02-15 06:46:13 -080046static const u32 udlfb_info_flags = FBINFO_DEFAULT | FBINFO_READS_FAST |
Bernie Thompson2469d5d2010-02-15 06:46:13 -080047 FBINFO_VIRTFB |
Bernie Thompson2469d5d2010-02-15 06:46:13 -080048 FBINFO_HWACCEL_IMAGEBLIT | FBINFO_HWACCEL_FILLRECT |
49 FBINFO_HWACCEL_COPYAREA | FBINFO_MISC_ALWAYS_SETPAR;
50
Bernie Thompsoncc403dc2010-02-15 06:45:49 -080051/*
Bernie Thompsonb63d1012011-07-10 00:30:00 -070052 * There are many DisplayLink-based graphics products, all with unique PIDs.
53 * So we match on DisplayLink's VID + Vendor-Defined Interface Class (0xff)
54 * We also require a match on SubClass (0x00) and Protocol (0x00),
55 * which is compatible with all known USB 2.0 era graphics chips and firmware,
56 * but allows DisplayLink to increment those for any future incompatible chips
Bernie Thompsoncc403dc2010-02-15 06:45:49 -080057 */
58static struct usb_device_id id_table[] = {
Bernie Thompsonb63d1012011-07-10 00:30:00 -070059 {.idVendor = 0x17e9,
60 .bInterfaceClass = 0xff,
61 .bInterfaceSubClass = 0x00,
62 .bInterfaceProtocol = 0x00,
63 .match_flags = USB_DEVICE_ID_MATCH_VENDOR |
64 USB_DEVICE_ID_MATCH_INT_CLASS |
65 USB_DEVICE_ID_MATCH_INT_SUBCLASS |
66 USB_DEVICE_ID_MATCH_INT_PROTOCOL,
67 },
Bernie Thompsoncc403dc2010-02-15 06:45:49 -080068 {},
69};
70MODULE_DEVICE_TABLE(usb, id_table);
Bernie Thompson59277b62009-11-24 15:52:21 -080071
Bernie Thompsond5ed5432010-09-05 16:35:39 -070072/* module options */
73static int console; /* Optionally allow fbcon to consume first framebuffer */
74static int fb_defio; /* Optionally enable experimental fb_defio mmap support */
Bernie Thompsondd8015f2010-02-15 06:46:35 -080075
Bernie Thompson4a4854d2010-02-15 06:45:55 -080076/* dlfb keeps a list of urbs for efficient bulk transfers */
77static void dlfb_urb_completion(struct urb *urb);
78static struct urb *dlfb_get_urb(struct dlfb_data *dev);
79static int dlfb_submit_urb(struct dlfb_data *dev, struct urb * urb, size_t len);
80static int dlfb_alloc_urb_list(struct dlfb_data *dev, int count, size_t size);
81static void dlfb_free_urb_list(struct dlfb_data *dev);
82
Bernie Thompson59277b62009-11-24 15:52:21 -080083/*
Bernie Thompsonbd808162010-02-15 06:46:48 -080084 * All DisplayLink bulk operations start with 0xAF, followed by specific code
85 * All operations are written to buffers which then later get sent to device
Bernie Thompson59277b62009-11-24 15:52:21 -080086 */
Bernie Thompson45742032010-02-15 06:46:04 -080087static char *dlfb_set_register(char *buf, u8 reg, u8 val)
Roberto De Ioris88e58b12009-06-03 14:03:06 -070088{
Bernie Thompson1d31a9e2010-02-15 06:45:43 -080089 *buf++ = 0xAF;
90 *buf++ = 0x20;
91 *buf++ = reg;
92 *buf++ = val;
93 return buf;
Roberto De Ioris88e58b12009-06-03 14:03:06 -070094}
95
Bernie Thompson45742032010-02-15 06:46:04 -080096static char *dlfb_vidreg_lock(char *buf)
Roberto De Ioris88e58b12009-06-03 14:03:06 -070097{
Bernie Thompson45742032010-02-15 06:46:04 -080098 return dlfb_set_register(buf, 0xFF, 0x00);
Bernie Thompson59277b62009-11-24 15:52:21 -080099}
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700100
Bernie Thompson45742032010-02-15 06:46:04 -0800101static char *dlfb_vidreg_unlock(char *buf)
Bernie Thompson59277b62009-11-24 15:52:21 -0800102{
Bernie Thompson45742032010-02-15 06:46:04 -0800103 return dlfb_set_register(buf, 0xFF, 0xFF);
Bernie Thompson59277b62009-11-24 15:52:21 -0800104}
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700105
Bernie Thompson59277b62009-11-24 15:52:21 -0800106/*
Bernie Thompson530f43a2010-02-15 06:46:21 -0800107 * On/Off for driving the DisplayLink framebuffer to the display
Bernie Thompson9825f702010-09-05 16:35:10 -0700108 * 0x00 H and V sync on
109 * 0x01 H and V sync off (screen blank but powered)
110 * 0x07 DPMS powerdown (requires modeset to come back)
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
Bernie Thompson9825f702010-09-05 16:35:10 -0700117 return dlfb_set_register(buf, 0x1F, 0x07);
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;
Bernie Thompson2685cff2010-09-05 18:29:56 -0700269
Bernie Thompson530f43a2010-02-15 06:46:21 -0800270 buf = (char *) urb->transfer_buffer;
Bernie Thompson59277b62009-11-24 15:52:21 -0800271
272 /*
273 * This first section has to do with setting the base address on the
274 * controller * associated with the display. There are 2 base
275 * pointers, currently, we only * use the 16 bpp segment.
276 */
Bernie Thompson45742032010-02-15 06:46:04 -0800277 wrptr = dlfb_vidreg_lock(buf);
278 wrptr = dlfb_set_color_depth(wrptr, 0x00);
Bernie Thompson59277b62009-11-24 15:52:21 -0800279 /* set base for 16bpp segment to 0 */
Bernie Thompson45742032010-02-15 06:46:04 -0800280 wrptr = dlfb_set_base16bpp(wrptr, 0);
Bernie Thompson59277b62009-11-24 15:52:21 -0800281 /* set base for 8bpp segment to end of fb */
Bernie Thompson45742032010-02-15 06:46:04 -0800282 wrptr = dlfb_set_base8bpp(wrptr, dev->info->fix.smem_len);
Bernie Thompson59277b62009-11-24 15:52:21 -0800283
Bernie Thompson45742032010-02-15 06:46:04 -0800284 wrptr = dlfb_set_vid_cmds(wrptr, var);
Bernie Thompson530f43a2010-02-15 06:46:21 -0800285 wrptr = dlfb_enable_hvsync(wrptr, true);
Bernie Thompson45742032010-02-15 06:46:04 -0800286 wrptr = dlfb_vidreg_unlock(wrptr);
Bernie Thompson59277b62009-11-24 15:52:21 -0800287
288 writesize = wrptr - buf;
289
Bernie Thompson530f43a2010-02-15 06:46:21 -0800290 retval = dlfb_submit_urb(dev, urb, writesize);
Bernie Thompson59277b62009-11-24 15:52:21 -0800291
Bernie Thompson59277b62009-11-24 15:52:21 -0800292 return retval;
293}
294
Bernie Thompson45742032010-02-15 06:46:04 -0800295static int dlfb_ops_mmap(struct fb_info *info, struct vm_area_struct *vma)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700296{
297 unsigned long start = vma->vm_start;
298 unsigned long size = vma->vm_end - vma->vm_start;
299 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
300 unsigned long page, pos;
301
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700302 if (offset + size > info->fix.smem_len)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700303 return -EINVAL;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700304
305 pos = (unsigned long)info->fix.smem_start + offset;
306
Paul Mundt81f6f3c2011-01-06 18:07:54 +0900307 pr_notice("mmap() framebuffer addr:%lu size:%lu\n",
Bernie Thompson2685cff2010-09-05 18:29:56 -0700308 pos, size);
309
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700310 while (size > 0) {
311 page = vmalloc_to_pfn((void *)pos);
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700312 if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED))
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700313 return -EAGAIN;
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700314
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700315 start += PAGE_SIZE;
316 pos += PAGE_SIZE;
317 if (size > PAGE_SIZE)
318 size -= PAGE_SIZE;
319 else
320 size = 0;
321 }
322
323 vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
324 return 0;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700325}
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
Bernie Thompson2685cff2010-09-05 18:29:56 -0700386 *
Pavel Machek3b7b31f2010-04-03 07:00:37 +0200387 * 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
Bernie Thompson2685cff2010-09-05 18:29:56 -0700392 * compression than 256 pixel raw or rle commands, with similar CPU consumpion.
Pavel Machek3b7b31f2010-04-03 07:00:37 +0200393 * 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;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700413
Bernie Thompson530f43a2010-02-15 06:46:21 -0800414 prefetchw((void *) cmd); /* pull in one cache line at least */
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700415
Bernie Thompson530f43a2010-02-15 06:46:21 -0800416 *cmd++ = 0xAF;
417 *cmd++ = 0x6B;
Bernie Thompson1572f912010-09-05 16:35:27 -0700418 *cmd++ = (uint8_t) ((dev_addr >> 16) & 0xFF);
419 *cmd++ = (uint8_t) ((dev_addr >> 8) & 0xFF);
420 *cmd++ = (uint8_t) ((dev_addr) & 0xFF);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700421
Bernie Thompson530f43a2010-02-15 06:46:21 -0800422 cmd_pixels_count_byte = cmd++; /* we'll know this later */
423 cmd_pixel_start = pixel;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700424
Bernie Thompson530f43a2010-02-15 06:46:21 -0800425 raw_pixels_count_byte = cmd++; /* we'll know this later */
426 raw_pixel_start = pixel;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700427
Bernie Thompson530f43a2010-02-15 06:46:21 -0800428 cmd_pixel_end = pixel + min(MAX_CMD_PIXELS + 1,
429 min((int)(pixel_end - pixel),
430 (int)(cmd_buffer_end - cmd) / bpp));
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700431
Bernie Thompson530f43a2010-02-15 06:46:21 -0800432 prefetch_range((void *) pixel, (cmd_pixel_end - pixel) * bpp);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700433
Bernie Thompson530f43a2010-02-15 06:46:21 -0800434 while (pixel < cmd_pixel_end) {
435 const uint16_t * const repeating_pixel = pixel;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700436
Bernie Thompson530f43a2010-02-15 06:46:21 -0800437 *(uint16_t *)cmd = cpu_to_be16p(pixel);
438 cmd += 2;
439 pixel++;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700440
Bernie Thompson530f43a2010-02-15 06:46:21 -0800441 if (unlikely((pixel < cmd_pixel_end) &&
442 (*pixel == *repeating_pixel))) {
443 /* go back and fill in raw pixel count */
444 *raw_pixels_count_byte = ((repeating_pixel -
445 raw_pixel_start) + 1) & 0xFF;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700446
Bernie Thompson530f43a2010-02-15 06:46:21 -0800447 while ((pixel < cmd_pixel_end)
448 && (*pixel == *repeating_pixel)) {
449 pixel++;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700450 }
Bernie Thompson59277b62009-11-24 15:52:21 -0800451
Bernie Thompson530f43a2010-02-15 06:46:21 -0800452 /* immediately after raw data is repeat byte */
453 *cmd++ = ((pixel - repeating_pixel) - 1) & 0xFF;
Bernie Thompson59277b62009-11-24 15:52:21 -0800454
Bernie Thompson530f43a2010-02-15 06:46:21 -0800455 /* Then start another raw pixel span */
456 raw_pixel_start = pixel;
457 raw_pixels_count_byte = cmd++;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700458 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700459 }
460
Bernie Thompson530f43a2010-02-15 06:46:21 -0800461 if (pixel > raw_pixel_start) {
462 /* finalize last RAW span */
463 *raw_pixels_count_byte = (pixel-raw_pixel_start) & 0xFF;
464 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700465
Bernie Thompson530f43a2010-02-15 06:46:21 -0800466 *cmd_pixels_count_byte = (pixel - cmd_pixel_start) & 0xFF;
467 dev_addr += (pixel - cmd_pixel_start) * bpp;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700468 }
469
Bernie Thompson530f43a2010-02-15 06:46:21 -0800470 if (cmd_buffer_end <= MIN_RLX_CMD_BYTES + cmd) {
471 /* Fill leftover bytes with no-ops */
472 if (cmd_buffer_end > cmd)
473 memset(cmd, 0xAF, cmd_buffer_end - cmd);
474 cmd = (uint8_t *) cmd_buffer_end;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700475 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700476
Bernie Thompson530f43a2010-02-15 06:46:21 -0800477 *command_buffer_ptr = cmd;
478 *pixel_start_ptr = pixel;
479 *device_address_ptr = dev_addr;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700480
Bernie Thompson530f43a2010-02-15 06:46:21 -0800481 return;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700482}
483
Bernie Thompson530f43a2010-02-15 06:46:21 -0800484/*
485 * There are 3 copies of every pixel: The front buffer that the fbdev
486 * client renders to, the actual framebuffer across the USB bus in hardware
487 * (that we can only write to, slowly, and can never read), and (optionally)
488 * our shadow copy that tracks what's been sent to that hardware buffer.
489 */
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700490static int dlfb_render_hline(struct dlfb_data *dev, struct urb **urb_ptr,
Bernie Thompson530f43a2010-02-15 06:46:21 -0800491 const char *front, char **urb_buf_ptr,
492 u32 byte_offset, u32 byte_width,
493 int *ident_ptr, int *sent_ptr)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700494{
Bernie Thompson530f43a2010-02-15 06:46:21 -0800495 const u8 *line_start, *line_end, *next_pixel;
496 u32 dev_addr = dev->base16 + byte_offset;
497 struct urb *urb = *urb_ptr;
498 u8 *cmd = *urb_buf_ptr;
499 u8 *cmd_end = (u8 *) urb->transfer_buffer + urb->transfer_buffer_length;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700500
Bernie Thompson530f43a2010-02-15 06:46:21 -0800501 line_start = (u8 *) (front + byte_offset);
502 next_pixel = line_start;
503 line_end = next_pixel + byte_width;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700504
Bernie Thompson530f43a2010-02-15 06:46:21 -0800505 if (dev->backing_buffer) {
506 int offset;
507 const u8 *back_start = (u8 *) (dev->backing_buffer
508 + byte_offset);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700509
Bernie Thompson530f43a2010-02-15 06:46:21 -0800510 *ident_ptr += dlfb_trim_hline(back_start, &next_pixel,
511 &byte_width);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700512
Bernie Thompson530f43a2010-02-15 06:46:21 -0800513 offset = next_pixel - line_start;
514 line_end = next_pixel + byte_width;
515 dev_addr += offset;
516 back_start += offset;
517 line_start += offset;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700518
Bernie Thompson530f43a2010-02-15 06:46:21 -0800519 memcpy((char *)back_start, (char *) line_start,
520 byte_width);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700521 }
522
Bernie Thompson530f43a2010-02-15 06:46:21 -0800523 while (next_pixel < line_end) {
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700524
Bernie Thompson530f43a2010-02-15 06:46:21 -0800525 dlfb_compress_hline((const uint16_t **) &next_pixel,
526 (const uint16_t *) line_end, &dev_addr,
527 (u8 **) &cmd, (u8 *) cmd_end);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700528
Bernie Thompson530f43a2010-02-15 06:46:21 -0800529 if (cmd >= cmd_end) {
530 int len = cmd - (u8 *) urb->transfer_buffer;
531 if (dlfb_submit_urb(dev, urb, len))
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700532 return 1; /* lost pixels is set */
Bernie Thompson530f43a2010-02-15 06:46:21 -0800533 *sent_ptr += len;
534 urb = dlfb_get_urb(dev);
535 if (!urb)
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700536 return 1; /* lost_pixels is set */
Bernie Thompson530f43a2010-02-15 06:46:21 -0800537 *urb_ptr = urb;
538 cmd = urb->transfer_buffer;
539 cmd_end = &cmd[urb->transfer_buffer_length];
540 }
541 }
542
543 *urb_buf_ptr = cmd;
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700544
545 return 0;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700546}
547
Bernie Thompson530f43a2010-02-15 06:46:21 -0800548int dlfb_handle_damage(struct dlfb_data *dev, int x, int y,
549 int width, int height, char *data)
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700550{
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700551 int i, ret;
Bernie Thompson530f43a2010-02-15 06:46:21 -0800552 char *cmd;
553 cycles_t start_cycles, end_cycles;
554 int bytes_sent = 0;
555 int bytes_identical = 0;
556 struct urb *urb;
557 int aligned_x;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700558
Bernie Thompson530f43a2010-02-15 06:46:21 -0800559 start_cycles = get_cycles();
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700560
Bernie Thompson530f43a2010-02-15 06:46:21 -0800561 aligned_x = DL_ALIGN_DOWN(x, sizeof(unsigned long));
562 width = DL_ALIGN_UP(width + (x-aligned_x), sizeof(unsigned long));
563 x = aligned_x;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700564
Bernie Thompson530f43a2010-02-15 06:46:21 -0800565 if ((width <= 0) ||
566 (x + width > dev->info->var.xres) ||
567 (y + height > dev->info->var.yres))
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700568 return -EINVAL;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700569
Bernie Thompson530f43a2010-02-15 06:46:21 -0800570 if (!atomic_read(&dev->usb_active))
571 return 0;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700572
Bernie Thompson530f43a2010-02-15 06:46:21 -0800573 urb = dlfb_get_urb(dev);
574 if (!urb)
575 return 0;
576 cmd = urb->transfer_buffer;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700577
Bernie Thompson530f43a2010-02-15 06:46:21 -0800578 for (i = y; i < y + height ; i++) {
579 const int line_offset = dev->info->fix.line_length * i;
580 const int byte_offset = line_offset + (x * BPP);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700581
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700582 if (dlfb_render_hline(dev, &urb,
583 (char *) dev->info->fix.smem_start,
Bernie Thompson2685cff2010-09-05 18:29:56 -0700584 &cmd, byte_offset, width * BPP,
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700585 &bytes_identical, &bytes_sent))
586 goto error;
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 Thompson5bea1fb2010-09-05 18:28:29 -0700597error:
Bernie Thompson530f43a2010-02-15 06:46:21 -0800598 atomic_add(bytes_sent, &dev->bytes_sent);
599 atomic_add(bytes_identical, &dev->bytes_identical);
600 atomic_add(width*height*2, &dev->bytes_rendered);
601 end_cycles = get_cycles();
602 atomic_add(((unsigned int) ((end_cycles - start_cycles)
603 >> 10)), /* Kcycles */
604 &dev->cpu_kcycles_used);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700605
Bernie Thompson530f43a2010-02-15 06:46:21 -0800606 return 0;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700607}
608
Bernie Thompsond46ecb92010-09-05 16:35:04 -0700609/*
610 * Path triggered by usermode clients who write to filesystem
611 * e.g. cat filename > /dev/fb1
612 * Not used by X Windows or text-mode console. But useful for testing.
613 * Slow because of extra copy and we must assume all pixels dirty.
614 */
615static ssize_t dlfb_ops_write(struct fb_info *info, const char __user *buf,
616 size_t count, loff_t *ppos)
617{
Paul Mundt1a3e5282011-01-06 17:29:24 +0900618 ssize_t result;
Bernie Thompsond46ecb92010-09-05 16:35:04 -0700619 struct dlfb_data *dev = info->par;
620 u32 offset = (u32) *ppos;
621
Bernie Thompsond46ecb92010-09-05 16:35:04 -0700622 result = fb_sys_write(info, buf, count, ppos);
623
624 if (result > 0) {
625 int start = max((int)(offset / info->fix.line_length) - 1, 0);
626 int lines = min((u32)((result / info->fix.line_length) + 1),
627 (u32)info->var.yres);
628
629 dlfb_handle_damage(dev, 0, start, info->var.xres,
630 lines, info->screen_base);
631 }
Bernie Thompsond46ecb92010-09-05 16:35:04 -0700632
633 return result;
634}
635
Bernie Thompson530f43a2010-02-15 06:46:21 -0800636/* hardware has native COPY command (see libdlo), but not worth it for fbcon */
Bernie Thompson45742032010-02-15 06:46:04 -0800637static void dlfb_ops_copyarea(struct fb_info *info,
Bernie Thompson530f43a2010-02-15 06:46:21 -0800638 const struct fb_copyarea *area)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700639{
Bernie Thompson530f43a2010-02-15 06:46:21 -0800640
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700641 struct dlfb_data *dev = info->par;
642
Bernie Thompson530f43a2010-02-15 06:46:21 -0800643 sys_copyarea(info, area);
644
645 dlfb_handle_damage(dev, area->dx, area->dy,
646 area->width, area->height, info->screen_base);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700647}
648
Bernie Thompson45742032010-02-15 06:46:04 -0800649static void dlfb_ops_imageblit(struct fb_info *info,
Bernie Thompson530f43a2010-02-15 06:46:21 -0800650 const struct fb_image *image)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700651{
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700652 struct dlfb_data *dev = info->par;
Bernie Thompson530f43a2010-02-15 06:46:21 -0800653
Bernie Thompson530f43a2010-02-15 06:46:21 -0800654 sys_imageblit(info, image);
655
656 dlfb_handle_damage(dev, image->dx, image->dy,
657 image->width, image->height, info->screen_base);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700658}
659
Bernie Thompson45742032010-02-15 06:46:04 -0800660static void dlfb_ops_fillrect(struct fb_info *info,
Bernie Thompson530f43a2010-02-15 06:46:21 -0800661 const struct fb_fillrect *rect)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700662{
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700663 struct dlfb_data *dev = info->par;
664
Bernie Thompson530f43a2010-02-15 06:46:21 -0800665 sys_fillrect(info, rect);
666
667 dlfb_handle_damage(dev, rect->dx, rect->dy, rect->width,
668 rect->height, info->screen_base);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700669}
670
Bernie Thompsond5ed5432010-09-05 16:35:39 -0700671/*
672 * NOTE: fb_defio.c is holding info->fbdefio.mutex
673 * Touching ANY framebuffer memory that triggers a page fault
674 * in fb_defio will cause a deadlock, when it also tries to
675 * grab the same mutex.
676 */
677static void dlfb_dpy_deferred_io(struct fb_info *info,
678 struct list_head *pagelist)
679{
680 struct page *cur;
681 struct fb_deferred_io *fbdefio = info->fbdefio;
682 struct dlfb_data *dev = info->par;
683 struct urb *urb;
684 char *cmd;
685 cycles_t start_cycles, end_cycles;
686 int bytes_sent = 0;
687 int bytes_identical = 0;
688 int bytes_rendered = 0;
689
690 if (!fb_defio)
691 return;
692
693 if (!atomic_read(&dev->usb_active))
694 return;
695
696 start_cycles = get_cycles();
697
698 urb = dlfb_get_urb(dev);
699 if (!urb)
700 return;
701
702 cmd = urb->transfer_buffer;
703
704 /* walk the written page list and render each to device */
705 list_for_each_entry(cur, &fbdefio->pagelist, lru) {
706
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700707 if (dlfb_render_hline(dev, &urb, (char *) info->fix.smem_start,
Bernie Thompsond5ed5432010-09-05 16:35:39 -0700708 &cmd, cur->index << PAGE_SHIFT,
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700709 PAGE_SIZE, &bytes_identical, &bytes_sent))
710 goto error;
Bernie Thompsond5ed5432010-09-05 16:35:39 -0700711 bytes_rendered += PAGE_SIZE;
712 }
713
714 if (cmd > (char *) urb->transfer_buffer) {
715 /* Send partial buffer remaining before exiting */
716 int len = cmd - (char *) urb->transfer_buffer;
717 dlfb_submit_urb(dev, urb, len);
718 bytes_sent += len;
719 } else
720 dlfb_urb_completion(urb);
721
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700722error:
Bernie Thompsond5ed5432010-09-05 16:35:39 -0700723 atomic_add(bytes_sent, &dev->bytes_sent);
724 atomic_add(bytes_identical, &dev->bytes_identical);
725 atomic_add(bytes_rendered, &dev->bytes_rendered);
726 end_cycles = get_cycles();
727 atomic_add(((unsigned int) ((end_cycles - start_cycles)
728 >> 10)), /* Kcycles */
729 &dev->cpu_kcycles_used);
730}
731
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700732static int dlfb_get_edid(struct dlfb_data *dev, char *edid, int len)
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800733{
734 int i;
735 int ret;
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700736 char *rbuf;
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800737
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700738 rbuf = kmalloc(2, GFP_KERNEL);
739 if (!rbuf)
740 return 0;
741
742 for (i = 0; i < len; i++) {
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800743 ret = usb_control_msg(dev->udev,
744 usb_rcvctrlpipe(dev->udev, 0), (0x02),
745 (0x80 | (0x02 << 5)), i << 8, 0xA1, rbuf, 2,
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700746 HZ);
747 if (ret < 1) {
Paul Mundt81f6f3c2011-01-06 18:07:54 +0900748 pr_err("Read EDID byte %d failed err %x\n", i, ret);
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700749 i--;
750 break;
751 }
752 edid[i] = rbuf[1];
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800753 }
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700754
755 kfree(rbuf);
756
757 return i;
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800758}
759
Bernie Thompson45742032010-02-15 06:46:04 -0800760static int dlfb_ops_ioctl(struct fb_info *info, unsigned int cmd,
Bernie Thompson530f43a2010-02-15 06:46:21 -0800761 unsigned long arg)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700762{
Bernie Thompson530f43a2010-02-15 06:46:21 -0800763
764 struct dlfb_data *dev = info->par;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700765 struct dloarea *area = NULL;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700766
Bernie Thompson530f43a2010-02-15 06:46:21 -0800767 if (!atomic_read(&dev->usb_active))
768 return 0;
769
770 /* TODO: Update X server to get this from sysfs instead */
771 if (cmd == DLFB_IOCTL_RETURN_EDID) {
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700772 char *edid = (char *)arg;
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700773 if (copy_to_user(edid, dev->edid, dev->edid_size))
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700774 return -EFAULT;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700775 return 0;
776 }
777
Bernie Thompson530f43a2010-02-15 06:46:21 -0800778 /* TODO: Help propose a standard fb.h ioctl to report mmap damage */
779 if (cmd == DLFB_IOCTL_REPORT_DAMAGE) {
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700780
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700781 /*
782 * If we have a damage-aware client, turn fb_defio "off"
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300783 * To avoid perf imact of unnecessary page fault handling.
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700784 * Done by resetting the delay for this fb_info to a very
785 * long period. Pages will become writable and stay that way.
786 * Reset to normal value when all clients have closed this fb.
787 */
788 if (info->fbdefio)
789 info->fbdefio->delay = DL_DEFIO_WRITE_DISABLE;
790
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700791 area = (struct dloarea *)arg;
792
793 if (area->x < 0)
794 area->x = 0;
795
796 if (area->x > info->var.xres)
797 area->x = info->var.xres;
798
799 if (area->y < 0)
800 area->y = 0;
801
802 if (area->y > info->var.yres)
803 area->y = info->var.yres;
804
Bernie Thompson530f43a2010-02-15 06:46:21 -0800805 dlfb_handle_damage(dev, area->x, area->y, area->w, area->h,
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700806 info->screen_base);
807 }
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700808
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700809 return 0;
810}
811
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700812/* taken from vesafb */
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700813static int
Bernie Thompson45742032010-02-15 06:46:04 -0800814dlfb_ops_setcolreg(unsigned regno, unsigned red, unsigned green,
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700815 unsigned blue, unsigned transp, struct fb_info *info)
816{
817 int err = 0;
818
819 if (regno >= info->cmap.len)
820 return 1;
821
822 if (regno < 16) {
823 if (info->var.red.offset == 10) {
824 /* 1:5:5:5 */
825 ((u32 *) (info->pseudo_palette))[regno] =
826 ((red & 0xf800) >> 1) |
827 ((green & 0xf800) >> 6) | ((blue & 0xf800) >> 11);
828 } else {
829 /* 0:5:6:5 */
830 ((u32 *) (info->pseudo_palette))[regno] =
831 ((red & 0xf800)) |
832 ((green & 0xfc00) >> 5) | ((blue & 0xf800) >> 11);
833 }
834 }
835
836 return err;
837}
838
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800839/*
840 * It's common for several clients to have framebuffer open simultaneously.
841 * e.g. both fbcon and X. Makes things interesting.
Bernie Thompson33077b82010-09-05 16:35:19 -0700842 * Assumes caller is holding info->lock (for open and release at least)
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800843 */
844static int dlfb_ops_open(struct fb_info *info, int user)
845{
846 struct dlfb_data *dev = info->par;
847
Bernie Thompsond5ed5432010-09-05 16:35:39 -0700848 /*
849 * fbcon aggressively connects to first framebuffer it finds,
850 * preventing other clients (X) from working properly. Usually
851 * not what the user wants. Fail by default with option to enable.
852 */
853 if ((user == 0) & (!console))
854 return -EBUSY;
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800855
Bernie Thompson33077b82010-09-05 16:35:19 -0700856 /* If the USB device is gone, we don't accept new opens */
857 if (dev->virtualized)
858 return -ENODEV;
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800859
860 dev->fb_count++;
861
Bernie Thompson33077b82010-09-05 16:35:19 -0700862 kref_get(&dev->kref);
863
Bernie Thompsond5ed5432010-09-05 16:35:39 -0700864 if (fb_defio && (info->fbdefio == NULL)) {
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700865 /* enable defio at last moment if not disabled by client */
866
867 struct fb_deferred_io *fbdefio;
868
Joe Perches31a9f472010-10-31 15:33:55 -0700869 fbdefio = kmalloc(sizeof(struct fb_deferred_io), GFP_KERNEL);
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700870
871 if (fbdefio) {
872 fbdefio->delay = DL_DEFIO_WRITE_DELAY;
873 fbdefio->deferred_io = dlfb_dpy_deferred_io;
874 }
875
876 info->fbdefio = fbdefio;
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800877 fb_deferred_io_init(info);
878 }
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800879
Paul Mundt81f6f3c2011-01-06 18:07:54 +0900880 pr_notice("open /dev/fb%d user=%d fb_info=%p count=%d\n",
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800881 info->node, user, info, dev->fb_count);
882
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700883 return 0;
884}
885
Bernie Thompson4a4854d2010-02-15 06:45:55 -0800886/*
887 * Called when all client interfaces to start transactions have been disabled,
888 * and all references to our device instance (dlfb_data) are released.
889 * Every transaction must have a reference, so we know are fully spun down
890 */
Bernie Thompson33077b82010-09-05 16:35:19 -0700891static void dlfb_free(struct kref *kref)
Bernie Thompson4a4854d2010-02-15 06:45:55 -0800892{
893 struct dlfb_data *dev = container_of(kref, struct dlfb_data, kref);
894
Bernie Thompson33077b82010-09-05 16:35:19 -0700895 /* this function will wait for all in-flight urbs to complete */
896 if (dev->urbs.count > 0)
897 dlfb_free_urb_list(dev);
898
Bernie Thompson4a4854d2010-02-15 06:45:55 -0800899 if (dev->backing_buffer)
900 vfree(dev->backing_buffer);
901
Bernie Thompson33077b82010-09-05 16:35:19 -0700902 kfree(dev->edid);
903
Paul Mundt81f6f3c2011-01-06 18:07:54 +0900904 pr_warn("freeing dlfb_data %p\n", dev);
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800905
Bernie Thompson4a4854d2010-02-15 06:45:55 -0800906 kfree(dev);
907}
908
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700909static void dlfb_release_urb_work(struct work_struct *work)
910{
911 struct urb_node *unode = container_of(work, struct urb_node,
912 release_urb_work.work);
913
914 up(&unode->dev->urbs.limit_sem);
915}
Bernie Thompson33077b82010-09-05 16:35:19 -0700916
917static void dlfb_free_framebuffer_work(struct work_struct *work)
Bernie Thompson2469d5d2010-02-15 06:46:13 -0800918{
Bernie Thompson33077b82010-09-05 16:35:19 -0700919 struct dlfb_data *dev = container_of(work, struct dlfb_data,
920 free_framebuffer_work.work);
921 struct fb_info *info = dev->info;
922 int node = info->node;
923
924 unregister_framebuffer(info);
Bernie Thompson2469d5d2010-02-15 06:46:13 -0800925
926 if (info->cmap.len != 0)
927 fb_dealloc_cmap(&info->cmap);
928 if (info->monspecs.modedb)
929 fb_destroy_modedb(info->monspecs.modedb);
930 if (info->screen_base)
931 vfree(info->screen_base);
932
933 fb_destroy_modelist(&info->modelist);
934
Bernie Thompson33077b82010-09-05 16:35:19 -0700935 dev->info = 0;
936
937 /* Assume info structure is freed after this point */
Bernie Thompson2469d5d2010-02-15 06:46:13 -0800938 framebuffer_release(info);
939
Paul Mundt81f6f3c2011-01-06 18:07:54 +0900940 pr_warn("fb_info for /dev/fb%d has been freed\n", node);
Bernie Thompson33077b82010-09-05 16:35:19 -0700941
942 /* ref taken in probe() as part of registering framebfufer */
943 kref_put(&dev->kref, dlfb_free);
944}
945
946/*
947 * Assumes caller is holding info->lock mutex (for open and release at least)
948 */
949static int dlfb_ops_release(struct fb_info *info, int user)
950{
951 struct dlfb_data *dev = info->par;
952
953 dev->fb_count--;
954
955 /* We can't free fb_info here - fbmem will touch it when we return */
956 if (dev->virtualized && (dev->fb_count == 0))
957 schedule_delayed_work(&dev->free_framebuffer_work, HZ);
958
Bernie Thompson33077b82010-09-05 16:35:19 -0700959 if ((dev->fb_count == 0) && (info->fbdefio)) {
960 fb_deferred_io_cleanup(info);
961 kfree(info->fbdefio);
962 info->fbdefio = NULL;
963 info->fbops->fb_mmap = dlfb_ops_mmap;
964 }
Bernie Thompson33077b82010-09-05 16:35:19 -0700965
Paul Mundt81f6f3c2011-01-06 18:07:54 +0900966 pr_warn("released /dev/fb%d user=%d count=%d\n",
Bernie Thompson33077b82010-09-05 16:35:19 -0700967 info->node, user, dev->fb_count);
968
969 kref_put(&dev->kref, dlfb_free);
970
971 return 0;
Bernie Thompson2469d5d2010-02-15 06:46:13 -0800972}
973
974/*
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800975 * Check whether a video mode is supported by the DisplayLink chip
976 * We start from monitor's modes, so don't need to filter that here
977 */
978static int dlfb_is_valid_mode(struct fb_videomode *mode,
979 struct fb_info *info)
980{
981 struct dlfb_data *dev = info->par;
982
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700983 if (mode->xres * mode->yres > dev->sku_pixel_limit) {
Paul Mundt81f6f3c2011-01-06 18:07:54 +0900984 pr_warn("%dx%d beyond chip capabilities\n",
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700985 mode->xres, mode->yres);
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800986 return 0;
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700987 }
988
Paul Mundt81f6f3c2011-01-06 18:07:54 +0900989 pr_info("%dx%d valid mode\n", mode->xres, mode->yres);
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800990
991 return 1;
992}
993
994static void dlfb_var_color_format(struct fb_var_screeninfo *var)
995{
996 const struct fb_bitfield red = { 11, 5, 0 };
997 const struct fb_bitfield green = { 5, 6, 0 };
998 const struct fb_bitfield blue = { 0, 5, 0 };
999
1000 var->bits_per_pixel = 16;
1001 var->red = red;
1002 var->green = green;
1003 var->blue = blue;
1004}
1005
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001006static int dlfb_ops_check_var(struct fb_var_screeninfo *var,
1007 struct fb_info *info)
1008{
1009 struct fb_videomode mode;
1010
1011 /* TODO: support dynamically changing framebuffer size */
1012 if ((var->xres * var->yres * 2) > info->fix.smem_len)
1013 return -EINVAL;
1014
1015 /* set device-specific elements of var unrelated to mode */
1016 dlfb_var_color_format(var);
1017
1018 fb_var_to_videomode(&mode, var);
1019
1020 if (!dlfb_is_valid_mode(&mode, info))
1021 return -EINVAL;
1022
1023 return 0;
1024}
1025
1026static int dlfb_ops_set_par(struct fb_info *info)
1027{
1028 struct dlfb_data *dev = info->par;
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001029 int result;
1030 u16 *pix_framebuffer;
1031 int i;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001032
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001033 pr_notice("set_par mode %dx%d\n", info->var.xres, info->var.yres);
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001034
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001035 result = dlfb_set_video_mode(dev, &info->var);
1036
1037 if ((result == 0) && (dev->fb_count == 0)) {
1038
1039 /* paint greenscreen */
1040
1041 pix_framebuffer = (u16 *) info->screen_base;
1042 for (i = 0; i < info->fix.smem_len / 2; i++)
1043 pix_framebuffer[i] = 0x37e6;
1044
1045 dlfb_handle_damage(dev, 0, 0, info->var.xres, info->var.yres,
1046 info->screen_base);
1047 }
1048
1049 return result;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001050}
1051
Bernie Thompson9825f702010-09-05 16:35:10 -07001052/*
1053 * In order to come back from full DPMS off, we need to set the mode again
1054 */
Bernie Thompson45742032010-02-15 06:46:04 -08001055static int dlfb_ops_blank(int blank_mode, struct fb_info *info)
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -07001056{
Bernie Thompson530f43a2010-02-15 06:46:21 -08001057 struct dlfb_data *dev = info->par;
Roberto De Ioris7316bc52009-06-10 23:02:19 -07001058
Roberto De Ioris7316bc52009-06-10 23:02:19 -07001059 if (blank_mode != FB_BLANK_UNBLANK) {
Bernie Thompson9825f702010-09-05 16:35:10 -07001060 char *bufptr;
1061 struct urb *urb;
Roberto De Ioris7316bc52009-06-10 23:02:19 -07001062
Bernie Thompson9825f702010-09-05 16:35:10 -07001063 urb = dlfb_get_urb(dev);
1064 if (!urb)
1065 return 0;
1066
1067 bufptr = (char *) urb->transfer_buffer;
1068 bufptr = dlfb_vidreg_lock(bufptr);
1069 bufptr = dlfb_enable_hvsync(bufptr, false);
1070 bufptr = dlfb_vidreg_unlock(bufptr);
1071
1072 dlfb_submit_urb(dev, urb, bufptr -
1073 (char *) urb->transfer_buffer);
1074 } else {
1075 dlfb_set_video_mode(dev, &info->var);
1076 }
Roberto De Ioris7316bc52009-06-10 23:02:19 -07001077
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001078 return 0;
1079}
1080
1081static struct fb_ops dlfb_ops = {
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001082 .owner = THIS_MODULE,
Paul Mundt1a3e5282011-01-06 17:29:24 +09001083 .fb_read = fb_sys_read,
Bernie Thompsond46ecb92010-09-05 16:35:04 -07001084 .fb_write = dlfb_ops_write,
Bernie Thompson45742032010-02-15 06:46:04 -08001085 .fb_setcolreg = dlfb_ops_setcolreg,
1086 .fb_fillrect = dlfb_ops_fillrect,
1087 .fb_copyarea = dlfb_ops_copyarea,
1088 .fb_imageblit = dlfb_ops_imageblit,
1089 .fb_mmap = dlfb_ops_mmap,
1090 .fb_ioctl = dlfb_ops_ioctl,
Bernie Thompson3e8f3d62010-02-15 06:46:26 -08001091 .fb_open = dlfb_ops_open,
Bernie Thompson45742032010-02-15 06:46:04 -08001092 .fb_release = dlfb_ops_release,
1093 .fb_blank = dlfb_ops_blank,
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001094 .fb_check_var = dlfb_ops_check_var,
1095 .fb_set_par = dlfb_ops_set_par,
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001096};
1097
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001098
Bernie Thompsoncc403dc2010-02-15 06:45:49 -08001099/*
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001100 * Assumes &info->lock held by caller
1101 * Assumes no active clients have framebuffer open
1102 */
1103static int dlfb_realloc_framebuffer(struct dlfb_data *dev, struct fb_info *info)
1104{
1105 int retval = -ENOMEM;
1106 int old_len = info->fix.smem_len;
1107 int new_len;
1108 unsigned char *old_fb = info->screen_base;
1109 unsigned char *new_fb;
1110 unsigned char *new_back;
1111
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001112 pr_warn("Reallocating framebuffer. Addresses will change!\n");
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001113
1114 new_len = info->fix.line_length * info->var.yres;
1115
1116 if (PAGE_ALIGN(new_len) > old_len) {
1117 /*
1118 * Alloc system memory for virtual framebuffer
1119 */
1120 new_fb = vmalloc(new_len);
1121 if (!new_fb) {
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001122 pr_err("Virtual framebuffer alloc failed\n");
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001123 goto error;
1124 }
1125
1126 if (info->screen_base) {
1127 memcpy(new_fb, old_fb, old_len);
1128 vfree(info->screen_base);
1129 }
1130
1131 info->screen_base = new_fb;
1132 info->fix.smem_len = PAGE_ALIGN(new_len);
1133 info->fix.smem_start = (unsigned long) new_fb;
1134 info->flags = udlfb_info_flags;
1135
1136 /*
1137 * Second framebuffer copy to mirror the framebuffer state
1138 * on the physical USB device. We can function without this.
1139 * But with imperfect damage info we may send pixels over USB
1140 * that were, in fact, unchanged - wasting limited USB bandwidth
1141 */
Joe Perches5b84cc72010-11-04 20:07:59 -07001142 new_back = vzalloc(new_len);
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001143 if (!new_back)
Linus Torvalds949f6712011-01-10 16:04:53 -08001144 pr_info("No shadow/backing buffer allocated\n");
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001145 else {
1146 if (dev->backing_buffer)
1147 vfree(dev->backing_buffer);
1148 dev->backing_buffer = new_back;
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001149 }
1150 }
1151
1152 retval = 0;
1153
1154error:
1155 return retval;
1156}
1157
1158/*
1159 * 1) Get EDID from hw, or use sw default
1160 * 2) Parse into various fb_info structs
1161 * 3) Allocate virtual framebuffer memory to back highest res mode
1162 *
1163 * Parses EDID into three places used by various parts of fbdev:
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001164 * fb_var_screeninfo contains the timing of the monitor's preferred mode
1165 * fb_info.monspecs is full parsed EDID info, including monspecs.modedb
1166 * fb_info.modelist is a linked list of all monitor & VESA modes which work
1167 *
1168 * If EDID is not readable/valid, then modelist is all VESA modes,
1169 * monspecs is NULL, and fb_var_screeninfo is set to safe VESA mode
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001170 * Returns 0 if successful
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001171 */
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001172static int dlfb_setup_modes(struct dlfb_data *dev,
1173 struct fb_info *info,
1174 char *default_edid, size_t default_edid_size)
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001175{
1176 int i;
1177 const struct fb_videomode *default_vmode = NULL;
1178 int result = 0;
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001179 char *edid;
1180 int tries = 3;
1181
1182 if (info->dev) /* only use mutex if info has been registered */
1183 mutex_lock(&info->lock);
1184
Paul Mundtb9f03a32011-01-06 18:04:02 +09001185 edid = kmalloc(EDID_LENGTH, GFP_KERNEL);
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001186 if (!edid) {
1187 result = -ENOMEM;
1188 goto error;
1189 }
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001190
1191 fb_destroy_modelist(&info->modelist);
1192 memset(&info->monspecs, 0, sizeof(info->monspecs));
1193
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001194 /*
1195 * Try to (re)read EDID from hardware first
1196 * EDID data may return, but not parse as valid
1197 * Try again a few times, in case of e.g. analog cable noise
1198 */
1199 while (tries--) {
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001200
Paul Mundtb9f03a32011-01-06 18:04:02 +09001201 i = dlfb_get_edid(dev, edid, EDID_LENGTH);
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001202
Paul Mundtb9f03a32011-01-06 18:04:02 +09001203 if (i >= EDID_LENGTH)
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001204 fb_edid_to_monspecs(edid, &info->monspecs);
1205
1206 if (info->monspecs.modedb_len > 0) {
1207 dev->edid = edid;
1208 dev->edid_size = i;
1209 break;
1210 }
1211 }
1212
1213 /* If that fails, use a previously returned EDID if available */
1214 if (info->monspecs.modedb_len == 0) {
1215
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001216 pr_err("Unable to get valid EDID from device/display\n");
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001217
1218 if (dev->edid) {
1219 fb_edid_to_monspecs(dev->edid, &info->monspecs);
1220 if (info->monspecs.modedb_len > 0)
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001221 pr_err("Using previously queried EDID\n");
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001222 }
1223 }
1224
1225 /* If that fails, use the default EDID we were handed */
1226 if (info->monspecs.modedb_len == 0) {
Paul Mundtb9f03a32011-01-06 18:04:02 +09001227 if (default_edid_size >= EDID_LENGTH) {
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001228 fb_edid_to_monspecs(default_edid, &info->monspecs);
1229 if (info->monspecs.modedb_len > 0) {
1230 memcpy(edid, default_edid, default_edid_size);
1231 dev->edid = edid;
1232 dev->edid_size = default_edid_size;
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001233 pr_err("Using default/backup EDID\n");
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001234 }
1235 }
1236 }
1237
1238 /* If we've got modes, let's pick a best default mode */
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001239 if (info->monspecs.modedb_len > 0) {
1240
1241 for (i = 0; i < info->monspecs.modedb_len; i++) {
1242 if (dlfb_is_valid_mode(&info->monspecs.modedb[i], info))
1243 fb_add_videomode(&info->monspecs.modedb[i],
1244 &info->modelist);
William Katsak9377c512011-06-23 13:16:29 +00001245 else {
1246 if (i == 0)
1247 /* if we've removed top/best mode */
1248 info->monspecs.misc
1249 &= ~FB_MISC_1ST_DETAIL;
1250 }
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001251 }
1252
1253 default_vmode = fb_find_best_display(&info->monspecs,
1254 &info->modelist);
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001255 }
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001256
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001257 /* If everything else has failed, fall back to safe default mode */
1258 if (default_vmode == NULL) {
1259
1260 struct fb_videomode fb_vmode = {0};
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001261
1262 /*
1263 * Add the standard VESA modes to our modelist
1264 * Since we don't have EDID, there may be modes that
1265 * overspec monitor and/or are incorrect aspect ratio, etc.
1266 * But at least the user has a chance to choose
1267 */
1268 for (i = 0; i < VESA_MODEDB_SIZE; i++) {
1269 if (dlfb_is_valid_mode((struct fb_videomode *)
1270 &vesa_modes[i], info))
1271 fb_add_videomode(&vesa_modes[i],
1272 &info->modelist);
1273 }
1274
1275 /*
1276 * default to resolution safe for projectors
1277 * (since they are most common case without EDID)
1278 */
1279 fb_vmode.xres = 800;
1280 fb_vmode.yres = 600;
1281 fb_vmode.refresh = 60;
1282 default_vmode = fb_find_nearest_mode(&fb_vmode,
1283 &info->modelist);
1284 }
1285
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001286 /* If we have good mode and no active clients*/
1287 if ((default_vmode != NULL) && (dev->fb_count == 0)) {
1288
1289 fb_videomode_to_var(&info->var, default_vmode);
1290 dlfb_var_color_format(&info->var);
1291
1292 /*
1293 * with mode size info, we can now alloc our framebuffer.
1294 */
1295 memcpy(&info->fix, &dlfb_fix, sizeof(dlfb_fix));
1296 info->fix.line_length = info->var.xres *
1297 (info->var.bits_per_pixel / 8);
1298
1299 result = dlfb_realloc_framebuffer(dev, info);
1300
1301 } else
1302 result = -EINVAL;
1303
1304error:
1305 if (edid && (dev->edid != edid))
1306 kfree(edid);
1307
1308 if (info->dev)
1309 mutex_unlock(&info->lock);
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001310
1311 return result;
1312}
1313
1314static ssize_t metrics_bytes_rendered_show(struct device *fbdev,
1315 struct device_attribute *a, char *buf) {
1316 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1317 struct dlfb_data *dev = fb_info->par;
1318 return snprintf(buf, PAGE_SIZE, "%u\n",
1319 atomic_read(&dev->bytes_rendered));
1320}
1321
1322static ssize_t metrics_bytes_identical_show(struct device *fbdev,
1323 struct device_attribute *a, char *buf) {
1324 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1325 struct dlfb_data *dev = fb_info->par;
1326 return snprintf(buf, PAGE_SIZE, "%u\n",
1327 atomic_read(&dev->bytes_identical));
1328}
1329
1330static ssize_t metrics_bytes_sent_show(struct device *fbdev,
1331 struct device_attribute *a, char *buf) {
1332 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1333 struct dlfb_data *dev = fb_info->par;
1334 return snprintf(buf, PAGE_SIZE, "%u\n",
1335 atomic_read(&dev->bytes_sent));
1336}
1337
1338static ssize_t metrics_cpu_kcycles_used_show(struct device *fbdev,
1339 struct device_attribute *a, char *buf) {
1340 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1341 struct dlfb_data *dev = fb_info->par;
1342 return snprintf(buf, PAGE_SIZE, "%u\n",
1343 atomic_read(&dev->cpu_kcycles_used));
1344}
1345
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001346static ssize_t edid_show(
1347 struct file *filp,
1348 struct kobject *kobj, struct bin_attribute *a,
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001349 char *buf, loff_t off, size_t count) {
1350 struct device *fbdev = container_of(kobj, struct device, kobj);
1351 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1352 struct dlfb_data *dev = fb_info->par;
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001353
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001354 if (dev->edid == NULL)
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001355 return 0;
1356
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001357 if ((off >= dev->edid_size) || (count > dev->edid_size))
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001358 return 0;
1359
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001360 if (off + count > dev->edid_size)
1361 count = dev->edid_size - off;
1362
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001363 pr_info("sysfs edid copy %p to %p, %d bytes\n",
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001364 dev->edid, buf, (int) count);
1365
1366 memcpy(buf, dev->edid, count);
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001367
1368 return count;
1369}
1370
Bernie Thompson8ef8cc42010-09-05 16:35:31 -07001371static ssize_t edid_store(
1372 struct file *filp,
1373 struct kobject *kobj, struct bin_attribute *a,
1374 char *src, loff_t src_off, size_t src_size) {
1375 struct device *fbdev = container_of(kobj, struct device, kobj);
1376 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1377 struct dlfb_data *dev = fb_info->par;
1378
1379 /* We only support write of entire EDID at once, no offset*/
Paul Mundtb9f03a32011-01-06 18:04:02 +09001380 if ((src_size != EDID_LENGTH) || (src_off != 0))
Bernie Thompson8ef8cc42010-09-05 16:35:31 -07001381 return 0;
1382
1383 dlfb_setup_modes(dev, fb_info, src, src_size);
1384
1385 if (dev->edid && (memcmp(src, dev->edid, src_size) == 0)) {
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001386 pr_info("sysfs written EDID is new default\n");
Bernie Thompson8ef8cc42010-09-05 16:35:31 -07001387 dlfb_ops_set_par(fb_info);
1388 return src_size;
1389 } else
1390 return 0;
1391}
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001392
1393static ssize_t metrics_reset_store(struct device *fbdev,
1394 struct device_attribute *attr,
1395 const char *buf, size_t count)
1396{
1397 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1398 struct dlfb_data *dev = fb_info->par;
1399
1400 atomic_set(&dev->bytes_rendered, 0);
1401 atomic_set(&dev->bytes_identical, 0);
1402 atomic_set(&dev->bytes_sent, 0);
1403 atomic_set(&dev->cpu_kcycles_used, 0);
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001404
1405 return count;
1406}
1407
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001408static struct bin_attribute edid_attr = {
1409 .attr.name = "edid",
Bernie Thompson8ef8cc42010-09-05 16:35:31 -07001410 .attr.mode = 0666,
Paul Mundtb9f03a32011-01-06 18:04:02 +09001411 .size = EDID_LENGTH,
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001412 .read = edid_show,
Bernie Thompson8ef8cc42010-09-05 16:35:31 -07001413 .write = edid_store
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001414};
1415
1416static struct device_attribute fb_device_attrs[] = {
1417 __ATTR_RO(metrics_bytes_rendered),
1418 __ATTR_RO(metrics_bytes_identical),
1419 __ATTR_RO(metrics_bytes_sent),
1420 __ATTR_RO(metrics_cpu_kcycles_used),
Greg Kroah-Hartman926c1112010-11-18 11:21:04 -08001421 __ATTR(metrics_reset, S_IWUSR, NULL, metrics_reset_store),
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001422};
1423
1424/*
Bernie Thompsoncc403dc2010-02-15 06:45:49 -08001425 * This is necessary before we can communicate with the display controller.
1426 */
1427static int dlfb_select_std_channel(struct dlfb_data *dev)
1428{
1429 int ret;
1430 u8 set_def_chn[] = { 0x57, 0xCD, 0xDC, 0xA7,
1431 0x1C, 0x88, 0x5E, 0x15,
1432 0x60, 0xFE, 0xC6, 0x97,
1433 0x16, 0x3D, 0x47, 0xF2 };
1434
1435 ret = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
1436 NR_USB_REQUEST_CHANNEL,
1437 (USB_DIR_OUT | USB_TYPE_VENDOR), 0, 0,
1438 set_def_chn, sizeof(set_def_chn), USB_CTRL_SET_TIMEOUT);
1439 return ret;
1440}
1441
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001442static int dlfb_parse_vendor_descriptor(struct dlfb_data *dev,
1443 struct usb_device *usbdev)
1444{
1445 char *desc;
1446 char *buf;
1447 char *desc_end;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001448
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001449 u8 total_len = 0;
1450
1451 buf = kzalloc(MAX_VENDOR_DESCRIPTOR_SIZE, GFP_KERNEL);
1452 if (!buf)
1453 return false;
1454 desc = buf;
1455
1456 total_len = usb_get_descriptor(usbdev, 0x5f, /* vendor specific */
1457 0, desc, MAX_VENDOR_DESCRIPTOR_SIZE);
1458 if (total_len > 5) {
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001459 pr_info("vendor descriptor length:%x data:%02x %02x %02x %02x" \
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001460 "%02x %02x %02x %02x %02x %02x %02x\n",
1461 total_len, desc[0],
1462 desc[1], desc[2], desc[3], desc[4], desc[5], desc[6],
1463 desc[7], desc[8], desc[9], desc[10]);
1464
1465 if ((desc[0] != total_len) || /* descriptor length */
1466 (desc[1] != 0x5f) || /* vendor descriptor type */
1467 (desc[2] != 0x01) || /* version (2 bytes) */
1468 (desc[3] != 0x00) ||
1469 (desc[4] != total_len - 2)) /* length after type */
1470 goto unrecognized;
1471
1472 desc_end = desc + total_len;
1473 desc += 5; /* the fixed header we've already parsed */
1474
1475 while (desc < desc_end) {
1476 u8 length;
1477 u16 key;
1478
1479 key = *((u16 *) desc);
1480 desc += sizeof(u16);
1481 length = *desc;
1482 desc++;
1483
1484 switch (key) {
1485 case 0x0200: { /* max_area */
1486 u32 max_area;
1487 max_area = le32_to_cpu(*((u32 *)desc));
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001488 pr_warn("DL chip limited to %d pixel modes\n",
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001489 max_area);
1490 dev->sku_pixel_limit = max_area;
1491 break;
1492 }
1493 default:
1494 break;
1495 }
1496 desc += length;
1497 }
1498 }
1499
1500 goto success;
1501
1502unrecognized:
1503 /* allow udlfb to load for now even if firmware unrecognized */
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001504 pr_err("Unrecognized vendor firmware descriptor\n");
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001505
1506success:
1507 kfree(buf);
1508 return true;
1509}
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001510static int dlfb_usb_probe(struct usb_interface *interface,
Bernie Thompson59277b62009-11-24 15:52:21 -08001511 const struct usb_device_id *id)
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001512{
Bernie Thompson59277b62009-11-24 15:52:21 -08001513 struct usb_device *usbdev;
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001514 struct dlfb_data *dev = 0;
Bernie Thompson33077b82010-09-05 16:35:19 -07001515 struct fb_info *info = 0;
Bernie Thompson59277b62009-11-24 15:52:21 -08001516 int retval = -ENOMEM;
Bernie Thompson2685cff2010-09-05 18:29:56 -07001517 int i;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001518
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001519 /* usb initialization */
1520
1521 usbdev = interface_to_usbdev(interface);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001522
Bernie Thompson59277b62009-11-24 15:52:21 -08001523 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1524 if (dev == NULL) {
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001525 err("dlfb_usb_probe: failed alloc of dev struct\n");
1526 goto error;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001527 }
1528
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001529 /* we need to wait for both usb and fbdev to spin down on disconnect */
1530 kref_init(&dev->kref); /* matching kref_put in usb .disconnect fn */
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001531 kref_get(&dev->kref); /* matching kref_put in free_framebuffer_work */
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001532
Bernie Thompson59277b62009-11-24 15:52:21 -08001533 dev->udev = usbdev;
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001534 dev->gdev = &usbdev->dev; /* our generic struct device * */
Bernie Thompson59277b62009-11-24 15:52:21 -08001535 usb_set_intfdata(interface, dev);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001536
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001537 pr_info("%s %s - serial #%s\n",
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001538 usbdev->manufacturer, usbdev->product, usbdev->serial);
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001539 pr_info("vid_%04x&pid_%04x&rev_%04x driver's dlfb_data struct at %p\n",
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001540 usbdev->descriptor.idVendor, usbdev->descriptor.idProduct,
1541 usbdev->descriptor.bcdDevice, dev);
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001542 pr_info("console enable=%d\n", console);
1543 pr_info("fb_defio enable=%d\n", fb_defio);
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001544
1545 dev->sku_pixel_limit = 2048 * 1152; /* default to maximum */
1546
1547 if (!dlfb_parse_vendor_descriptor(dev, usbdev)) {
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001548 pr_err("firmware not recognized. Assume incompatible device\n");
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001549 goto error;
1550 }
1551
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001552 if (!dlfb_alloc_urb_list(dev, WRITES_IN_FLIGHT, MAX_TRANSFER)) {
1553 retval = -ENOMEM;
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001554 pr_err("dlfb_alloc_urb_list failed\n");
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001555 goto error;
1556 }
1557
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001558 /* We don't register a new USB class. Our client interface is fbdev */
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001559
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001560 /* allocates framebuffer driver structure, not framebuffer memory */
Kay Sieversc91a7932011-07-05 17:04:11 -07001561 info = framebuffer_alloc(0, &interface->dev);
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001562 if (!info) {
1563 retval = -ENOMEM;
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001564 pr_err("framebuffer_alloc failed\n");
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001565 goto error;
1566 }
Bernie Thompson33077b82010-09-05 16:35:19 -07001567
Bernie Thompson59277b62009-11-24 15:52:21 -08001568 dev->info = info;
1569 info->par = dev;
1570 info->pseudo_palette = dev->pseudo_palette;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001571 info->fbops = &dlfb_ops;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001572
Bernie Thompson59277b62009-11-24 15:52:21 -08001573 retval = fb_alloc_cmap(&info->cmap, 256, 0);
1574 if (retval < 0) {
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001575 pr_err("fb_alloc_cmap failed %x\n", retval);
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001576 goto error;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001577 }
1578
Bernie Thompson33077b82010-09-05 16:35:19 -07001579 INIT_DELAYED_WORK(&dev->free_framebuffer_work,
1580 dlfb_free_framebuffer_work);
1581
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001582 INIT_LIST_HEAD(&info->modelist);
1583
1584 retval = dlfb_setup_modes(dev, info, NULL, 0);
1585 if (retval != 0) {
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001586 pr_err("unable to find common mode for display and adapter\n");
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001587 goto error;
1588 }
1589
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001590 /* ready to begin using device */
1591
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001592 atomic_set(&dev->usb_active, 1);
Bernie Thompson59277b62009-11-24 15:52:21 -08001593 dlfb_select_std_channel(dev);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001594
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001595 dlfb_ops_check_var(&info->var, info);
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001596 dlfb_ops_set_par(info);
1597
Bernie Thompson59277b62009-11-24 15:52:21 -08001598 retval = register_framebuffer(info);
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001599 if (retval < 0) {
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001600 pr_err("register_framebuffer failed %d\n", retval);
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001601 goto error;
1602 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001603
Liu Yuan94cd1ae2011-04-18 10:44:38 +00001604 for (i = 0; i < ARRAY_SIZE(fb_device_attrs); i++) {
1605 retval = device_create_file(info->dev, &fb_device_attrs[i]);
1606 if (retval) {
1607 pr_err("device_create_file failed %d\n", retval);
1608 goto err_del_attrs;
1609 }
1610 }
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -07001611
Liu Yuan94cd1ae2011-04-18 10:44:38 +00001612 retval = device_create_bin_file(info->dev, &edid_attr);
1613 if (retval) {
1614 pr_err("device_create_bin_file failed %d\n", retval);
1615 goto err_del_attrs;
1616 }
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001617
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001618 pr_info("DisplayLink USB device /dev/fb%d attached. %dx%d resolution."
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001619 " Using %dK framebuffer memory\n", info->node,
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001620 info->var.xres, info->var.yres,
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001621 ((dev->backing_buffer) ?
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001622 info->fix.smem_len * 2 : info->fix.smem_len) >> 10);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001623 return 0;
1624
Liu Yuan94cd1ae2011-04-18 10:44:38 +00001625err_del_attrs:
1626 for (i -= 1; i >= 0; i--)
1627 device_remove_file(info->dev, &fb_device_attrs[i]);
1628
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001629error:
1630 if (dev) {
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001631
Bernie Thompson33077b82010-09-05 16:35:19 -07001632 if (info) {
1633 if (info->cmap.len != 0)
1634 fb_dealloc_cmap(&info->cmap);
1635 if (info->monspecs.modedb)
1636 fb_destroy_modedb(info->monspecs.modedb);
1637 if (info->screen_base)
1638 vfree(info->screen_base);
1639
1640 fb_destroy_modelist(&info->modelist);
1641
1642 framebuffer_release(info);
1643 }
1644
1645 if (dev->backing_buffer)
1646 vfree(dev->backing_buffer);
1647
1648 kref_put(&dev->kref, dlfb_free); /* ref for framebuffer */
1649 kref_put(&dev->kref, dlfb_free); /* last ref from kref_init */
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001650
1651 /* dev has been deallocated. Do not dereference */
1652 }
1653
Bernie Thompson59277b62009-11-24 15:52:21 -08001654 return retval;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001655}
1656
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001657static void dlfb_usb_disconnect(struct usb_interface *interface)
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001658{
Bernie Thompson59277b62009-11-24 15:52:21 -08001659 struct dlfb_data *dev;
1660 struct fb_info *info;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001661 int i;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001662
Bernie Thompson59277b62009-11-24 15:52:21 -08001663 dev = usb_get_intfdata(interface);
Bernie Thompson59277b62009-11-24 15:52:21 -08001664 info = dev->info;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001665
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001666 pr_info("USB disconnect starting\n");
Bernie Thompson33077b82010-09-05 16:35:19 -07001667
1668 /* we virtualize until all fb clients release. Then we free */
1669 dev->virtualized = true;
1670
1671 /* When non-active we'll update virtual framebuffer, but no new urbs */
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001672 atomic_set(&dev->usb_active, 0);
1673
Bernie Thompson33077b82010-09-05 16:35:19 -07001674 /* remove udlfb's sysfs interfaces */
1675 for (i = 0; i < ARRAY_SIZE(fb_device_attrs); i++)
1676 device_remove_file(info->dev, &fb_device_attrs[i]);
1677 device_remove_bin_file(info->dev, &edid_attr);
1678
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001679 usb_set_intfdata(interface, NULL);
1680
Bernie Thompson33077b82010-09-05 16:35:19 -07001681 /* if clients still have us open, will be freed on last close */
1682 if (dev->fb_count == 0)
1683 schedule_delayed_work(&dev->free_framebuffer_work, 0);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001684
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001685 /* release reference taken by kref_init in probe() */
Bernie Thompson33077b82010-09-05 16:35:19 -07001686 kref_put(&dev->kref, dlfb_free);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001687
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001688 /* consider dlfb_data freed */
1689
1690 return;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001691}
1692
1693static struct usb_driver dlfb_driver = {
1694 .name = "udlfb",
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001695 .probe = dlfb_usb_probe,
1696 .disconnect = dlfb_usb_disconnect,
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001697 .id_table = id_table,
1698};
1699
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001700static int __init dlfb_module_init(void)
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001701{
1702 int res;
1703
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001704 res = usb_register(&dlfb_driver);
1705 if (res)
1706 err("usb_register failed. Error number %d", res);
1707
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001708 return res;
1709}
1710
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001711static void __exit dlfb_module_exit(void)
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001712{
1713 usb_deregister(&dlfb_driver);
1714}
1715
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001716module_init(dlfb_module_init);
1717module_exit(dlfb_module_exit);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001718
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001719static void dlfb_urb_completion(struct urb *urb)
1720{
1721 struct urb_node *unode = urb->context;
1722 struct dlfb_data *dev = unode->dev;
1723 unsigned long flags;
1724
1725 /* sync/async unlink faults aren't errors */
1726 if (urb->status) {
1727 if (!(urb->status == -ENOENT ||
1728 urb->status == -ECONNRESET ||
1729 urb->status == -ESHUTDOWN)) {
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001730 pr_err("%s - nonzero write bulk status received: %d\n",
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001731 __func__, urb->status);
1732 atomic_set(&dev->lost_pixels, 1);
1733 }
1734 }
1735
1736 urb->transfer_buffer_length = dev->urbs.size; /* reset to actual */
1737
1738 spin_lock_irqsave(&dev->urbs.lock, flags);
1739 list_add_tail(&unode->entry, &dev->urbs.list);
1740 dev->urbs.available++;
1741 spin_unlock_irqrestore(&dev->urbs.lock, flags);
1742
Bernie Thompson5bea1fb2010-09-05 18:28:29 -07001743 /*
1744 * When using fb_defio, we deadlock if up() is called
1745 * while another is waiting. So queue to another process.
1746 */
1747 if (fb_defio)
1748 schedule_delayed_work(&unode->release_urb_work, 0);
1749 else
1750 up(&dev->urbs.limit_sem);
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001751}
1752
1753static void dlfb_free_urb_list(struct dlfb_data *dev)
1754{
1755 int count = dev->urbs.count;
1756 struct list_head *node;
1757 struct urb_node *unode;
1758 struct urb *urb;
1759 int ret;
1760 unsigned long flags;
1761
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001762 pr_notice("Waiting for completes and freeing all render urbs\n");
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001763
1764 /* keep waiting and freeing, until we've got 'em all */
1765 while (count--) {
Bernie Thompson33077b82010-09-05 16:35:19 -07001766
1767 /* Getting interrupted means a leak, but ok at shutdown*/
1768 ret = down_interruptible(&dev->urbs.limit_sem);
1769 if (ret)
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001770 break;
Bernie Thompson33077b82010-09-05 16:35:19 -07001771
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001772 spin_lock_irqsave(&dev->urbs.lock, flags);
1773
1774 node = dev->urbs.list.next; /* have reserved one with sem */
1775 list_del_init(node);
1776
1777 spin_unlock_irqrestore(&dev->urbs.lock, flags);
1778
1779 unode = list_entry(node, struct urb_node, entry);
1780 urb = unode->urb;
1781
1782 /* Free each separately allocated piece */
Greg Kroah-Hartmanc220cc32010-04-29 15:36:29 -07001783 usb_free_coherent(urb->dev, dev->urbs.size,
1784 urb->transfer_buffer, urb->transfer_dma);
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001785 usb_free_urb(urb);
1786 kfree(node);
1787 }
1788
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001789}
1790
1791static int dlfb_alloc_urb_list(struct dlfb_data *dev, int count, size_t size)
1792{
1793 int i = 0;
1794 struct urb *urb;
1795 struct urb_node *unode;
1796 char *buf;
1797
1798 spin_lock_init(&dev->urbs.lock);
1799
1800 dev->urbs.size = size;
1801 INIT_LIST_HEAD(&dev->urbs.list);
1802
1803 while (i < count) {
1804 unode = kzalloc(sizeof(struct urb_node), GFP_KERNEL);
1805 if (!unode)
1806 break;
1807 unode->dev = dev;
1808
Bernie Thompson5bea1fb2010-09-05 18:28:29 -07001809 INIT_DELAYED_WORK(&unode->release_urb_work,
1810 dlfb_release_urb_work);
1811
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001812 urb = usb_alloc_urb(0, GFP_KERNEL);
1813 if (!urb) {
1814 kfree(unode);
1815 break;
1816 }
1817 unode->urb = urb;
1818
Greg Kroah-Hartmanc220cc32010-04-29 15:36:29 -07001819 buf = usb_alloc_coherent(dev->udev, MAX_TRANSFER, GFP_KERNEL,
1820 &urb->transfer_dma);
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001821 if (!buf) {
1822 kfree(unode);
1823 usb_free_urb(urb);
1824 break;
1825 }
1826
1827 /* urb->transfer_buffer_length set to actual before submit */
1828 usb_fill_bulk_urb(urb, dev->udev, usb_sndbulkpipe(dev->udev, 1),
1829 buf, size, dlfb_urb_completion, unode);
1830 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1831
1832 list_add_tail(&unode->entry, &dev->urbs.list);
1833
1834 i++;
1835 }
1836
1837 sema_init(&dev->urbs.limit_sem, i);
1838 dev->urbs.count = i;
1839 dev->urbs.available = i;
1840
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001841 pr_notice("allocated %d %d byte urbs\n", i, (int) size);
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001842
1843 return i;
1844}
1845
1846static struct urb *dlfb_get_urb(struct dlfb_data *dev)
1847{
1848 int ret = 0;
1849 struct list_head *entry;
1850 struct urb_node *unode;
1851 struct urb *urb = NULL;
1852 unsigned long flags;
1853
1854 /* Wait for an in-flight buffer to complete and get re-queued */
1855 ret = down_timeout(&dev->urbs.limit_sem, GET_URB_TIMEOUT);
1856 if (ret) {
1857 atomic_set(&dev->lost_pixels, 1);
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001858 pr_warn("wait for urb interrupted: %x available: %d\n",
Bernie Thompson5bea1fb2010-09-05 18:28:29 -07001859 ret, dev->urbs.available);
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001860 goto error;
1861 }
1862
1863 spin_lock_irqsave(&dev->urbs.lock, flags);
1864
1865 BUG_ON(list_empty(&dev->urbs.list)); /* reserved one with limit_sem */
1866 entry = dev->urbs.list.next;
1867 list_del_init(entry);
1868 dev->urbs.available--;
1869
1870 spin_unlock_irqrestore(&dev->urbs.lock, flags);
1871
1872 unode = list_entry(entry, struct urb_node, entry);
1873 urb = unode->urb;
1874
1875error:
1876 return urb;
1877}
1878
1879static int dlfb_submit_urb(struct dlfb_data *dev, struct urb *urb, size_t len)
1880{
1881 int ret;
1882
1883 BUG_ON(len > dev->urbs.size);
1884
1885 urb->transfer_buffer_length = len; /* set to actual payload len */
1886 ret = usb_submit_urb(urb, GFP_KERNEL);
1887 if (ret) {
1888 dlfb_urb_completion(urb); /* because no one else will */
1889 atomic_set(&dev->lost_pixels, 1);
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001890 pr_err("usb_submit_urb error %x\n", ret);
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001891 }
1892 return ret;
1893}
1894
Bernie Thompsond5ed5432010-09-05 16:35:39 -07001895module_param(console, bool, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
1896MODULE_PARM_DESC(console, "Allow fbcon to consume first framebuffer found");
1897
1898module_param(fb_defio, bool, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
1899MODULE_PARM_DESC(fb_defio, "Enable fb_defio mmap support. *Experimental*");
1900
Bernie Thompson59277b62009-11-24 15:52:21 -08001901MODULE_AUTHOR("Roberto De Ioris <roberto@unbit.it>, "
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001902 "Jaya Kumar <jayakumar.lkml@gmail.com>, "
1903 "Bernie Thompson <bernie@plugable.com>");
1904MODULE_DESCRIPTION("DisplayLink kernel framebuffer driver");
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001905MODULE_LICENSE("GPL");
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001906