blob: 3473e75ce78511106905f68d66d638b63a886972 [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>
Paul Mundt96f8d8642010-11-16 14:00:24 +090032#include <video/udlfb.h>
Paul Mundtb9f03a32011-01-06 18:04:02 +090033#include "edid.h"
Roberto De Ioris88e58b12009-06-03 14:03:06 -070034
Bernie Thompson59277b62009-11-24 15:52:21 -080035static struct fb_fix_screeninfo dlfb_fix = {
Bernie Thompson2469d5d2010-02-15 06:46:13 -080036 .id = "udlfb",
Bernie Thompson1d31a9e2010-02-15 06:45:43 -080037 .type = FB_TYPE_PACKED_PIXELS,
38 .visual = FB_VISUAL_TRUECOLOR,
39 .xpanstep = 0,
40 .ypanstep = 0,
41 .ywrapstep = 0,
42 .accel = FB_ACCEL_NONE,
Bernie Thompson59277b62009-11-24 15:52:21 -080043};
Roberto De Ioris88e58b12009-06-03 14:03:06 -070044
Bernie Thompson2469d5d2010-02-15 06:46:13 -080045static const u32 udlfb_info_flags = FBINFO_DEFAULT | FBINFO_READS_FAST |
Bernie Thompson2469d5d2010-02-15 06:46:13 -080046 FBINFO_VIRTFB |
Bernie Thompson2469d5d2010-02-15 06:46:13 -080047 FBINFO_HWACCEL_IMAGEBLIT | FBINFO_HWACCEL_FILLRECT |
48 FBINFO_HWACCEL_COPYAREA | FBINFO_MISC_ALWAYS_SETPAR;
49
Bernie Thompsoncc403dc2010-02-15 06:45:49 -080050/*
Bernie Thompsonb63d1012011-07-10 00:30:00 -070051 * There are many DisplayLink-based graphics products, all with unique PIDs.
52 * So we match on DisplayLink's VID + Vendor-Defined Interface Class (0xff)
53 * We also require a match on SubClass (0x00) and Protocol (0x00),
54 * which is compatible with all known USB 2.0 era graphics chips and firmware,
55 * but allows DisplayLink to increment those for any future incompatible chips
Bernie Thompsoncc403dc2010-02-15 06:45:49 -080056 */
57static struct usb_device_id id_table[] = {
Bernie Thompsonb63d1012011-07-10 00:30:00 -070058 {.idVendor = 0x17e9,
59 .bInterfaceClass = 0xff,
60 .bInterfaceSubClass = 0x00,
61 .bInterfaceProtocol = 0x00,
62 .match_flags = USB_DEVICE_ID_MATCH_VENDOR |
63 USB_DEVICE_ID_MATCH_INT_CLASS |
64 USB_DEVICE_ID_MATCH_INT_SUBCLASS |
65 USB_DEVICE_ID_MATCH_INT_PROTOCOL,
66 },
Bernie Thompsoncc403dc2010-02-15 06:45:49 -080067 {},
68};
69MODULE_DEVICE_TABLE(usb, id_table);
Bernie Thompson59277b62009-11-24 15:52:21 -080070
Bernie Thompsond5ed5432010-09-05 16:35:39 -070071/* module options */
Bernie Thompson4aa7faf2011-08-21 13:35:39 -070072static int console = 1; /* Allow fbcon to open framebuffer */
Bernie Thompson9f811b72011-08-21 13:35:38 -070073static int fb_defio = 1; /* Detect mmap writes using page faults */
Stuart Hopkinsd3189542011-08-21 13:34:17 -070074static int shadow = 1; /* Optionally disable shadow framebuffer */
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 Thompson58e7c3b2011-08-21 13:34:11 -0700107 * Map FB_BLANK_* to DisplayLink register
108 * DLReg FB_BLANK_*
109 * ----- -----------------------------
110 * 0x00 FB_BLANK_UNBLANK (0)
111 * 0x01 FB_BLANK (1)
112 * 0x03 FB_BLANK_VSYNC_SUSPEND (2)
113 * 0x05 FB_BLANK_HSYNC_SUSPEND (3)
114 * 0x07 FB_BLANK_POWERDOWN (4) Note: requires modeset to come back
Bernie Thompson59277b62009-11-24 15:52:21 -0800115 */
Bernie Thompson58e7c3b2011-08-21 13:34:11 -0700116static char *dlfb_blanking(char *buf, int fb_blank)
Bernie Thompson59277b62009-11-24 15:52:21 -0800117{
Bernie Thompson58e7c3b2011-08-21 13:34:11 -0700118 u8 reg;
119
120 switch (fb_blank) {
121 case FB_BLANK_POWERDOWN:
122 reg = 0x07;
123 break;
124 case FB_BLANK_HSYNC_SUSPEND:
125 reg = 0x05;
126 break;
127 case FB_BLANK_VSYNC_SUSPEND:
128 reg = 0x03;
129 break;
130 case FB_BLANK_NORMAL:
131 reg = 0x01;
132 break;
133 default:
134 reg = 0x00;
135 }
136
137 buf = dlfb_set_register(buf, 0x1F, reg);
138
139 return buf;
Bernie Thompson59277b62009-11-24 15:52:21 -0800140}
141
Bernie Thompson45742032010-02-15 06:46:04 -0800142static char *dlfb_set_color_depth(char *buf, u8 selection)
Bernie Thompson59277b62009-11-24 15:52:21 -0800143{
Bernie Thompson45742032010-02-15 06:46:04 -0800144 return dlfb_set_register(buf, 0x00, selection);
Bernie Thompson59277b62009-11-24 15:52:21 -0800145}
146
Bernie Thompson45742032010-02-15 06:46:04 -0800147static char *dlfb_set_base16bpp(char *wrptr, u32 base)
Bernie Thompson59277b62009-11-24 15:52:21 -0800148{
Bernie Thompson1d31a9e2010-02-15 06:45:43 -0800149 /* the base pointer is 16 bits wide, 0x20 is hi byte. */
Bernie Thompson45742032010-02-15 06:46:04 -0800150 wrptr = dlfb_set_register(wrptr, 0x20, base >> 16);
151 wrptr = dlfb_set_register(wrptr, 0x21, base >> 8);
152 return dlfb_set_register(wrptr, 0x22, base);
Bernie Thompson59277b62009-11-24 15:52:21 -0800153}
154
Bernie Thompsonbd808162010-02-15 06:46:48 -0800155/*
156 * DisplayLink HW has separate 16bpp and 8bpp framebuffers.
157 * In 24bpp modes, the low 323 RGB bits go in the 8bpp framebuffer
158 */
Bernie Thompson45742032010-02-15 06:46:04 -0800159static char *dlfb_set_base8bpp(char *wrptr, u32 base)
Bernie Thompson59277b62009-11-24 15:52:21 -0800160{
Bernie Thompson45742032010-02-15 06:46:04 -0800161 wrptr = dlfb_set_register(wrptr, 0x26, base >> 16);
162 wrptr = dlfb_set_register(wrptr, 0x27, base >> 8);
163 return dlfb_set_register(wrptr, 0x28, base);
Bernie Thompson59277b62009-11-24 15:52:21 -0800164}
165
Bernie Thompson45742032010-02-15 06:46:04 -0800166static char *dlfb_set_register_16(char *wrptr, u8 reg, u16 value)
Bernie Thompson59277b62009-11-24 15:52:21 -0800167{
Bernie Thompson45742032010-02-15 06:46:04 -0800168 wrptr = dlfb_set_register(wrptr, reg, value >> 8);
169 return dlfb_set_register(wrptr, reg+1, value);
Bernie Thompson59277b62009-11-24 15:52:21 -0800170}
171
172/*
173 * This is kind of weird because the controller takes some
174 * register values in a different byte order than other registers.
175 */
Bernie Thompson45742032010-02-15 06:46:04 -0800176static char *dlfb_set_register_16be(char *wrptr, u8 reg, u16 value)
Bernie Thompson59277b62009-11-24 15:52:21 -0800177{
Bernie Thompson45742032010-02-15 06:46:04 -0800178 wrptr = dlfb_set_register(wrptr, reg, value);
179 return dlfb_set_register(wrptr, reg+1, value >> 8);
Bernie Thompson59277b62009-11-24 15:52:21 -0800180}
181
182/*
183 * LFSR is linear feedback shift register. The reason we have this is
184 * because the display controller needs to minimize the clock depth of
185 * various counters used in the display path. So this code reverses the
186 * provided value into the lfsr16 value by counting backwards to get
187 * the value that needs to be set in the hardware comparator to get the
188 * same actual count. This makes sense once you read above a couple of
189 * times and think about it from a hardware perspective.
190 */
Bernie Thompsonbd808162010-02-15 06:46:48 -0800191static u16 dlfb_lfsr16(u16 actual_count)
Bernie Thompson59277b62009-11-24 15:52:21 -0800192{
193 u32 lv = 0xFFFF; /* This is the lfsr value that the hw starts with */
194
195 while (actual_count--) {
196 lv = ((lv << 1) |
197 (((lv >> 15) ^ (lv >> 4) ^ (lv >> 2) ^ (lv >> 1)) & 1))
198 & 0xFFFF;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700199 }
Bernie Thompson59277b62009-11-24 15:52:21 -0800200
201 return (u16) lv;
202}
203
204/*
205 * This does LFSR conversion on the value that is to be written.
206 * See LFSR explanation above for more detail.
207 */
Bernie Thompson45742032010-02-15 06:46:04 -0800208static char *dlfb_set_register_lfsr16(char *wrptr, u8 reg, u16 value)
Bernie Thompson59277b62009-11-24 15:52:21 -0800209{
Bernie Thompsonbd808162010-02-15 06:46:48 -0800210 return dlfb_set_register_16(wrptr, reg, dlfb_lfsr16(value));
Bernie Thompson59277b62009-11-24 15:52:21 -0800211}
212
213/*
214 * This takes a standard fbdev screeninfo struct and all of its monitor mode
215 * details and converts them into the DisplayLink equivalent register commands.
216 */
Bernie Thompson45742032010-02-15 06:46:04 -0800217static char *dlfb_set_vid_cmds(char *wrptr, struct fb_var_screeninfo *var)
Bernie Thompson59277b62009-11-24 15:52:21 -0800218{
219 u16 xds, yds;
220 u16 xde, yde;
221 u16 yec;
222
Bernie Thompson59277b62009-11-24 15:52:21 -0800223 /* x display start */
224 xds = var->left_margin + var->hsync_len;
Bernie Thompson45742032010-02-15 06:46:04 -0800225 wrptr = dlfb_set_register_lfsr16(wrptr, 0x01, xds);
Bernie Thompson59277b62009-11-24 15:52:21 -0800226 /* x display end */
227 xde = xds + var->xres;
Bernie Thompson45742032010-02-15 06:46:04 -0800228 wrptr = dlfb_set_register_lfsr16(wrptr, 0x03, xde);
Bernie Thompson59277b62009-11-24 15:52:21 -0800229
230 /* y display start */
231 yds = var->upper_margin + var->vsync_len;
Bernie Thompson45742032010-02-15 06:46:04 -0800232 wrptr = dlfb_set_register_lfsr16(wrptr, 0x05, yds);
Bernie Thompson59277b62009-11-24 15:52:21 -0800233 /* y display end */
234 yde = yds + var->yres;
Bernie Thompson45742032010-02-15 06:46:04 -0800235 wrptr = dlfb_set_register_lfsr16(wrptr, 0x07, yde);
Bernie Thompson59277b62009-11-24 15:52:21 -0800236
237 /* x end count is active + blanking - 1 */
Bernie Thompson45742032010-02-15 06:46:04 -0800238 wrptr = dlfb_set_register_lfsr16(wrptr, 0x09,
239 xde + var->right_margin - 1);
Bernie Thompson59277b62009-11-24 15:52:21 -0800240
241 /* libdlo hardcodes hsync start to 1 */
Bernie Thompson45742032010-02-15 06:46:04 -0800242 wrptr = dlfb_set_register_lfsr16(wrptr, 0x0B, 1);
Bernie Thompson59277b62009-11-24 15:52:21 -0800243
244 /* hsync end is width of sync pulse + 1 */
Bernie Thompson45742032010-02-15 06:46:04 -0800245 wrptr = dlfb_set_register_lfsr16(wrptr, 0x0D, var->hsync_len + 1);
Bernie Thompson59277b62009-11-24 15:52:21 -0800246
247 /* hpixels is active pixels */
Bernie Thompson45742032010-02-15 06:46:04 -0800248 wrptr = dlfb_set_register_16(wrptr, 0x0F, var->xres);
Bernie Thompson59277b62009-11-24 15:52:21 -0800249
250 /* yendcount is vertical active + vertical blanking */
251 yec = var->yres + var->upper_margin + var->lower_margin +
252 var->vsync_len;
Bernie Thompson45742032010-02-15 06:46:04 -0800253 wrptr = dlfb_set_register_lfsr16(wrptr, 0x11, yec);
Bernie Thompson59277b62009-11-24 15:52:21 -0800254
255 /* libdlo hardcodes vsync start to 0 */
Bernie Thompson45742032010-02-15 06:46:04 -0800256 wrptr = dlfb_set_register_lfsr16(wrptr, 0x13, 0);
Bernie Thompson59277b62009-11-24 15:52:21 -0800257
258 /* vsync end is width of vsync pulse */
Bernie Thompson45742032010-02-15 06:46:04 -0800259 wrptr = dlfb_set_register_lfsr16(wrptr, 0x15, var->vsync_len);
Bernie Thompson59277b62009-11-24 15:52:21 -0800260
261 /* vpixels is active pixels */
Bernie Thompson45742032010-02-15 06:46:04 -0800262 wrptr = dlfb_set_register_16(wrptr, 0x17, var->yres);
Bernie Thompson59277b62009-11-24 15:52:21 -0800263
264 /* convert picoseconds to 5kHz multiple for pclk5k = x * 1E12/5k */
Bernie Thompson45742032010-02-15 06:46:04 -0800265 wrptr = dlfb_set_register_16be(wrptr, 0x1B,
266 200*1000*1000/var->pixclock);
Bernie Thompson59277b62009-11-24 15:52:21 -0800267
268 return wrptr;
269}
270
271/*
272 * This takes a standard fbdev screeninfo struct that was fetched or prepared
273 * and then generates the appropriate command sequence that then drives the
274 * display controller.
275 */
276static int dlfb_set_video_mode(struct dlfb_data *dev,
277 struct fb_var_screeninfo *var)
278{
279 char *buf;
280 char *wrptr;
281 int retval = 0;
282 int writesize;
Bernie Thompson530f43a2010-02-15 06:46:21 -0800283 struct urb *urb;
Bernie Thompson59277b62009-11-24 15:52:21 -0800284
Bernie Thompson530f43a2010-02-15 06:46:21 -0800285 if (!atomic_read(&dev->usb_active))
286 return -EPERM;
287
288 urb = dlfb_get_urb(dev);
289 if (!urb)
290 return -ENOMEM;
Bernie Thompson2685cff2010-09-05 18:29:56 -0700291
Bernie Thompson530f43a2010-02-15 06:46:21 -0800292 buf = (char *) urb->transfer_buffer;
Bernie Thompson59277b62009-11-24 15:52:21 -0800293
294 /*
295 * This first section has to do with setting the base address on the
296 * controller * associated with the display. There are 2 base
297 * pointers, currently, we only * use the 16 bpp segment.
298 */
Bernie Thompson45742032010-02-15 06:46:04 -0800299 wrptr = dlfb_vidreg_lock(buf);
300 wrptr = dlfb_set_color_depth(wrptr, 0x00);
Bernie Thompson59277b62009-11-24 15:52:21 -0800301 /* set base for 16bpp segment to 0 */
Bernie Thompson45742032010-02-15 06:46:04 -0800302 wrptr = dlfb_set_base16bpp(wrptr, 0);
Bernie Thompson59277b62009-11-24 15:52:21 -0800303 /* set base for 8bpp segment to end of fb */
Bernie Thompson45742032010-02-15 06:46:04 -0800304 wrptr = dlfb_set_base8bpp(wrptr, dev->info->fix.smem_len);
Bernie Thompson59277b62009-11-24 15:52:21 -0800305
Bernie Thompson45742032010-02-15 06:46:04 -0800306 wrptr = dlfb_set_vid_cmds(wrptr, var);
Bernie Thompson58e7c3b2011-08-21 13:34:11 -0700307 wrptr = dlfb_blanking(wrptr, FB_BLANK_UNBLANK);
Bernie Thompson45742032010-02-15 06:46:04 -0800308 wrptr = dlfb_vidreg_unlock(wrptr);
Bernie Thompson59277b62009-11-24 15:52:21 -0800309
310 writesize = wrptr - buf;
311
Bernie Thompson530f43a2010-02-15 06:46:21 -0800312 retval = dlfb_submit_urb(dev, urb, writesize);
Bernie Thompson59277b62009-11-24 15:52:21 -0800313
Bernie Thompson58e7c3b2011-08-21 13:34:11 -0700314 dev->blank_mode = FB_BLANK_UNBLANK;
315
Bernie Thompson59277b62009-11-24 15:52:21 -0800316 return retval;
317}
318
Bernie Thompson45742032010-02-15 06:46:04 -0800319static int dlfb_ops_mmap(struct fb_info *info, struct vm_area_struct *vma)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700320{
321 unsigned long start = vma->vm_start;
322 unsigned long size = vma->vm_end - vma->vm_start;
323 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
324 unsigned long page, pos;
325
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700326 if (offset + size > info->fix.smem_len)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700327 return -EINVAL;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700328
329 pos = (unsigned long)info->fix.smem_start + offset;
330
Paul Mundt81f6f3c2011-01-06 18:07:54 +0900331 pr_notice("mmap() framebuffer addr:%lu size:%lu\n",
Bernie Thompson2685cff2010-09-05 18:29:56 -0700332 pos, size);
333
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700334 while (size > 0) {
335 page = vmalloc_to_pfn((void *)pos);
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700336 if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED))
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700337 return -EAGAIN;
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700338
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700339 start += PAGE_SIZE;
340 pos += PAGE_SIZE;
341 if (size > PAGE_SIZE)
342 size -= PAGE_SIZE;
343 else
344 size = 0;
345 }
346
347 vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
348 return 0;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700349}
350
Bernie Thompson530f43a2010-02-15 06:46:21 -0800351/*
352 * Trims identical data from front and back of line
353 * Sets new front buffer address and width
354 * And returns byte count of identical pixels
355 * Assumes CPU natural alignment (unsigned long)
356 * for back and front buffer ptrs and width
357 */
358static int dlfb_trim_hline(const u8 *bback, const u8 **bfront, int *width_bytes)
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700359{
Bernie Thompson530f43a2010-02-15 06:46:21 -0800360 int j, k;
361 const unsigned long *back = (const unsigned long *) bback;
362 const unsigned long *front = (const unsigned long *) *bfront;
363 const int width = *width_bytes / sizeof(unsigned long);
364 int identical = width;
365 int start = width;
366 int end = width;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700367
Bernie Thompson530f43a2010-02-15 06:46:21 -0800368 prefetch((void *) front);
369 prefetch((void *) back);
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700370
Bernie Thompson530f43a2010-02-15 06:46:21 -0800371 for (j = 0; j < width; j++) {
372 if (back[j] != front[j]) {
373 start = j;
374 break;
375 }
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700376 }
377
Bernie Thompson530f43a2010-02-15 06:46:21 -0800378 for (k = width - 1; k > j; k--) {
379 if (back[k] != front[k]) {
380 end = k+1;
381 break;
382 }
383 }
384
385 identical = start + (width - end);
386 *bfront = (u8 *) &front[start];
387 *width_bytes = (end - start) * sizeof(unsigned long);
388
389 return identical * sizeof(unsigned long);
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700390}
391
392/*
Pavel Machek3b7b31f2010-04-03 07:00:37 +0200393 * Render a command stream for an encoded horizontal line segment of pixels.
394 *
395 * A command buffer holds several commands.
396 * It always begins with a fresh command header
397 * (the protocol doesn't require this, but we enforce it to allow
398 * multiple buffers to be potentially encoded and sent in parallel).
399 * A single command encodes one contiguous horizontal line of pixels
400 *
401 * The function relies on the client to do all allocation, so that
402 * rendering can be done directly to output buffers (e.g. USB URBs).
403 * The function fills the supplied command buffer, providing information
404 * on where it left off, so the client may call in again with additional
405 * buffers if the line will take several buffers to complete.
406 *
407 * A single command can transmit a maximum of 256 pixels,
408 * regardless of the compression ratio (protocol design limit).
409 * To the hardware, 0 for a size byte means 256
Bernie Thompson2685cff2010-09-05 18:29:56 -0700410 *
Pavel Machek3b7b31f2010-04-03 07:00:37 +0200411 * Rather than 256 pixel commands which are either rl or raw encoded,
412 * the rlx command simply assumes alternating raw and rl spans within one cmd.
413 * This has a slightly larger header overhead, but produces more even results.
414 * It also processes all data (read and write) in a single pass.
415 * Performance benchmarks of common cases show it having just slightly better
Bernie Thompson2685cff2010-09-05 18:29:56 -0700416 * compression than 256 pixel raw or rle commands, with similar CPU consumpion.
Pavel Machek3b7b31f2010-04-03 07:00:37 +0200417 * But for very rl friendly data, will compress not quite as well.
418 */
Bernie Thompson530f43a2010-02-15 06:46:21 -0800419static void dlfb_compress_hline(
420 const uint16_t **pixel_start_ptr,
421 const uint16_t *const pixel_end,
422 uint32_t *device_address_ptr,
423 uint8_t **command_buffer_ptr,
424 const uint8_t *const cmd_buffer_end)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700425{
Bernie Thompson530f43a2010-02-15 06:46:21 -0800426 const uint16_t *pixel = *pixel_start_ptr;
427 uint32_t dev_addr = *device_address_ptr;
428 uint8_t *cmd = *command_buffer_ptr;
429 const int bpp = 2;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700430
Bernie Thompson530f43a2010-02-15 06:46:21 -0800431 while ((pixel_end > pixel) &&
432 (cmd_buffer_end - MIN_RLX_CMD_BYTES > cmd)) {
433 uint8_t *raw_pixels_count_byte = 0;
434 uint8_t *cmd_pixels_count_byte = 0;
435 const uint16_t *raw_pixel_start = 0;
436 const uint16_t *cmd_pixel_start, *cmd_pixel_end = 0;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700437
Bernie Thompson530f43a2010-02-15 06:46:21 -0800438 prefetchw((void *) cmd); /* pull in one cache line at least */
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700439
Bernie Thompson530f43a2010-02-15 06:46:21 -0800440 *cmd++ = 0xAF;
441 *cmd++ = 0x6B;
Bernie Thompson1572f912010-09-05 16:35:27 -0700442 *cmd++ = (uint8_t) ((dev_addr >> 16) & 0xFF);
443 *cmd++ = (uint8_t) ((dev_addr >> 8) & 0xFF);
444 *cmd++ = (uint8_t) ((dev_addr) & 0xFF);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700445
Bernie Thompson530f43a2010-02-15 06:46:21 -0800446 cmd_pixels_count_byte = cmd++; /* we'll know this later */
447 cmd_pixel_start = pixel;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700448
Bernie Thompson530f43a2010-02-15 06:46:21 -0800449 raw_pixels_count_byte = cmd++; /* we'll know this later */
450 raw_pixel_start = pixel;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700451
Bernie Thompson530f43a2010-02-15 06:46:21 -0800452 cmd_pixel_end = pixel + min(MAX_CMD_PIXELS + 1,
453 min((int)(pixel_end - pixel),
454 (int)(cmd_buffer_end - cmd) / bpp));
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700455
Bernie Thompson530f43a2010-02-15 06:46:21 -0800456 prefetch_range((void *) pixel, (cmd_pixel_end - pixel) * bpp);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700457
Bernie Thompson530f43a2010-02-15 06:46:21 -0800458 while (pixel < cmd_pixel_end) {
459 const uint16_t * const repeating_pixel = pixel;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700460
Bernie Thompson530f43a2010-02-15 06:46:21 -0800461 *(uint16_t *)cmd = cpu_to_be16p(pixel);
462 cmd += 2;
463 pixel++;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700464
Bernie Thompson530f43a2010-02-15 06:46:21 -0800465 if (unlikely((pixel < cmd_pixel_end) &&
466 (*pixel == *repeating_pixel))) {
467 /* go back and fill in raw pixel count */
468 *raw_pixels_count_byte = ((repeating_pixel -
469 raw_pixel_start) + 1) & 0xFF;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700470
Bernie Thompson530f43a2010-02-15 06:46:21 -0800471 while ((pixel < cmd_pixel_end)
472 && (*pixel == *repeating_pixel)) {
473 pixel++;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700474 }
Bernie Thompson59277b62009-11-24 15:52:21 -0800475
Bernie Thompson530f43a2010-02-15 06:46:21 -0800476 /* immediately after raw data is repeat byte */
477 *cmd++ = ((pixel - repeating_pixel) - 1) & 0xFF;
Bernie Thompson59277b62009-11-24 15:52:21 -0800478
Bernie Thompson530f43a2010-02-15 06:46:21 -0800479 /* Then start another raw pixel span */
480 raw_pixel_start = pixel;
481 raw_pixels_count_byte = cmd++;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700482 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700483 }
484
Bernie Thompson530f43a2010-02-15 06:46:21 -0800485 if (pixel > raw_pixel_start) {
486 /* finalize last RAW span */
487 *raw_pixels_count_byte = (pixel-raw_pixel_start) & 0xFF;
488 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700489
Bernie Thompson530f43a2010-02-15 06:46:21 -0800490 *cmd_pixels_count_byte = (pixel - cmd_pixel_start) & 0xFF;
491 dev_addr += (pixel - cmd_pixel_start) * bpp;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700492 }
493
Bernie Thompson530f43a2010-02-15 06:46:21 -0800494 if (cmd_buffer_end <= MIN_RLX_CMD_BYTES + cmd) {
495 /* Fill leftover bytes with no-ops */
496 if (cmd_buffer_end > cmd)
497 memset(cmd, 0xAF, cmd_buffer_end - cmd);
498 cmd = (uint8_t *) cmd_buffer_end;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700499 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700500
Bernie Thompson530f43a2010-02-15 06:46:21 -0800501 *command_buffer_ptr = cmd;
502 *pixel_start_ptr = pixel;
503 *device_address_ptr = dev_addr;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700504
Bernie Thompson530f43a2010-02-15 06:46:21 -0800505 return;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700506}
507
Bernie Thompson530f43a2010-02-15 06:46:21 -0800508/*
509 * There are 3 copies of every pixel: The front buffer that the fbdev
510 * client renders to, the actual framebuffer across the USB bus in hardware
511 * (that we can only write to, slowly, and can never read), and (optionally)
512 * our shadow copy that tracks what's been sent to that hardware buffer.
513 */
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700514static int dlfb_render_hline(struct dlfb_data *dev, struct urb **urb_ptr,
Bernie Thompson530f43a2010-02-15 06:46:21 -0800515 const char *front, char **urb_buf_ptr,
516 u32 byte_offset, u32 byte_width,
517 int *ident_ptr, int *sent_ptr)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700518{
Bernie Thompson530f43a2010-02-15 06:46:21 -0800519 const u8 *line_start, *line_end, *next_pixel;
520 u32 dev_addr = dev->base16 + byte_offset;
521 struct urb *urb = *urb_ptr;
522 u8 *cmd = *urb_buf_ptr;
523 u8 *cmd_end = (u8 *) urb->transfer_buffer + urb->transfer_buffer_length;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700524
Bernie Thompson530f43a2010-02-15 06:46:21 -0800525 line_start = (u8 *) (front + byte_offset);
526 next_pixel = line_start;
527 line_end = next_pixel + byte_width;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700528
Bernie Thompson530f43a2010-02-15 06:46:21 -0800529 if (dev->backing_buffer) {
530 int offset;
531 const u8 *back_start = (u8 *) (dev->backing_buffer
532 + byte_offset);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700533
Bernie Thompson530f43a2010-02-15 06:46:21 -0800534 *ident_ptr += dlfb_trim_hline(back_start, &next_pixel,
535 &byte_width);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700536
Bernie Thompson530f43a2010-02-15 06:46:21 -0800537 offset = next_pixel - line_start;
538 line_end = next_pixel + byte_width;
539 dev_addr += offset;
540 back_start += offset;
541 line_start += offset;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700542
Bernie Thompson530f43a2010-02-15 06:46:21 -0800543 memcpy((char *)back_start, (char *) line_start,
544 byte_width);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700545 }
546
Bernie Thompson530f43a2010-02-15 06:46:21 -0800547 while (next_pixel < line_end) {
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700548
Bernie Thompson530f43a2010-02-15 06:46:21 -0800549 dlfb_compress_hline((const uint16_t **) &next_pixel,
550 (const uint16_t *) line_end, &dev_addr,
551 (u8 **) &cmd, (u8 *) cmd_end);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700552
Bernie Thompson530f43a2010-02-15 06:46:21 -0800553 if (cmd >= cmd_end) {
554 int len = cmd - (u8 *) urb->transfer_buffer;
555 if (dlfb_submit_urb(dev, urb, len))
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700556 return 1; /* lost pixels is set */
Bernie Thompson530f43a2010-02-15 06:46:21 -0800557 *sent_ptr += len;
558 urb = dlfb_get_urb(dev);
559 if (!urb)
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700560 return 1; /* lost_pixels is set */
Bernie Thompson530f43a2010-02-15 06:46:21 -0800561 *urb_ptr = urb;
562 cmd = urb->transfer_buffer;
563 cmd_end = &cmd[urb->transfer_buffer_length];
564 }
565 }
566
567 *urb_buf_ptr = cmd;
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700568
569 return 0;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700570}
571
Bernie Thompson530f43a2010-02-15 06:46:21 -0800572int dlfb_handle_damage(struct dlfb_data *dev, int x, int y,
573 int width, int height, char *data)
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700574{
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700575 int i, ret;
Bernie Thompson530f43a2010-02-15 06:46:21 -0800576 char *cmd;
577 cycles_t start_cycles, end_cycles;
578 int bytes_sent = 0;
579 int bytes_identical = 0;
580 struct urb *urb;
581 int aligned_x;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700582
Bernie Thompson530f43a2010-02-15 06:46:21 -0800583 start_cycles = get_cycles();
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700584
Bernie Thompson530f43a2010-02-15 06:46:21 -0800585 aligned_x = DL_ALIGN_DOWN(x, sizeof(unsigned long));
586 width = DL_ALIGN_UP(width + (x-aligned_x), sizeof(unsigned long));
587 x = aligned_x;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700588
Bernie Thompson530f43a2010-02-15 06:46:21 -0800589 if ((width <= 0) ||
590 (x + width > dev->info->var.xres) ||
591 (y + height > dev->info->var.yres))
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700592 return -EINVAL;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700593
Bernie Thompson530f43a2010-02-15 06:46:21 -0800594 if (!atomic_read(&dev->usb_active))
595 return 0;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700596
Bernie Thompson530f43a2010-02-15 06:46:21 -0800597 urb = dlfb_get_urb(dev);
598 if (!urb)
599 return 0;
600 cmd = urb->transfer_buffer;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700601
Bernie Thompson530f43a2010-02-15 06:46:21 -0800602 for (i = y; i < y + height ; i++) {
603 const int line_offset = dev->info->fix.line_length * i;
604 const int byte_offset = line_offset + (x * BPP);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700605
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700606 if (dlfb_render_hline(dev, &urb,
607 (char *) dev->info->fix.smem_start,
Bernie Thompson2685cff2010-09-05 18:29:56 -0700608 &cmd, byte_offset, width * BPP,
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700609 &bytes_identical, &bytes_sent))
610 goto error;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700611 }
612
Bernie Thompson530f43a2010-02-15 06:46:21 -0800613 if (cmd > (char *) urb->transfer_buffer) {
614 /* Send partial buffer remaining before exiting */
615 int len = cmd - (char *) urb->transfer_buffer;
616 ret = dlfb_submit_urb(dev, urb, len);
617 bytes_sent += len;
618 } else
619 dlfb_urb_completion(urb);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700620
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700621error:
Bernie Thompson530f43a2010-02-15 06:46:21 -0800622 atomic_add(bytes_sent, &dev->bytes_sent);
623 atomic_add(bytes_identical, &dev->bytes_identical);
624 atomic_add(width*height*2, &dev->bytes_rendered);
625 end_cycles = get_cycles();
626 atomic_add(((unsigned int) ((end_cycles - start_cycles)
627 >> 10)), /* Kcycles */
628 &dev->cpu_kcycles_used);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700629
Bernie Thompson530f43a2010-02-15 06:46:21 -0800630 return 0;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700631}
632
Bernie Thompsond46ecb92010-09-05 16:35:04 -0700633/*
634 * Path triggered by usermode clients who write to filesystem
635 * e.g. cat filename > /dev/fb1
636 * Not used by X Windows or text-mode console. But useful for testing.
637 * Slow because of extra copy and we must assume all pixels dirty.
638 */
639static ssize_t dlfb_ops_write(struct fb_info *info, const char __user *buf,
640 size_t count, loff_t *ppos)
641{
Paul Mundt1a3e5282011-01-06 17:29:24 +0900642 ssize_t result;
Bernie Thompsond46ecb92010-09-05 16:35:04 -0700643 struct dlfb_data *dev = info->par;
644 u32 offset = (u32) *ppos;
645
Bernie Thompsond46ecb92010-09-05 16:35:04 -0700646 result = fb_sys_write(info, buf, count, ppos);
647
648 if (result > 0) {
649 int start = max((int)(offset / info->fix.line_length) - 1, 0);
650 int lines = min((u32)((result / info->fix.line_length) + 1),
651 (u32)info->var.yres);
652
653 dlfb_handle_damage(dev, 0, start, info->var.xres,
654 lines, info->screen_base);
655 }
Bernie Thompsond46ecb92010-09-05 16:35:04 -0700656
657 return result;
658}
659
Bernie Thompson530f43a2010-02-15 06:46:21 -0800660/* hardware has native COPY command (see libdlo), but not worth it for fbcon */
Bernie Thompson45742032010-02-15 06:46:04 -0800661static void dlfb_ops_copyarea(struct fb_info *info,
Bernie Thompson530f43a2010-02-15 06:46:21 -0800662 const struct fb_copyarea *area)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700663{
Bernie Thompson530f43a2010-02-15 06:46:21 -0800664
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700665 struct dlfb_data *dev = info->par;
666
Bernie Thompson530f43a2010-02-15 06:46:21 -0800667 sys_copyarea(info, area);
668
669 dlfb_handle_damage(dev, area->dx, area->dy,
670 area->width, area->height, info->screen_base);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700671}
672
Bernie Thompson45742032010-02-15 06:46:04 -0800673static void dlfb_ops_imageblit(struct fb_info *info,
Bernie Thompson530f43a2010-02-15 06:46:21 -0800674 const struct fb_image *image)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700675{
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700676 struct dlfb_data *dev = info->par;
Bernie Thompson530f43a2010-02-15 06:46:21 -0800677
Bernie Thompson530f43a2010-02-15 06:46:21 -0800678 sys_imageblit(info, image);
679
680 dlfb_handle_damage(dev, image->dx, image->dy,
681 image->width, image->height, info->screen_base);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700682}
683
Bernie Thompson45742032010-02-15 06:46:04 -0800684static void dlfb_ops_fillrect(struct fb_info *info,
Bernie Thompson530f43a2010-02-15 06:46:21 -0800685 const struct fb_fillrect *rect)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700686{
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700687 struct dlfb_data *dev = info->par;
688
Bernie Thompson530f43a2010-02-15 06:46:21 -0800689 sys_fillrect(info, rect);
690
691 dlfb_handle_damage(dev, rect->dx, rect->dy, rect->width,
692 rect->height, info->screen_base);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700693}
694
Bernie Thompsond5ed5432010-09-05 16:35:39 -0700695/*
696 * NOTE: fb_defio.c is holding info->fbdefio.mutex
697 * Touching ANY framebuffer memory that triggers a page fault
698 * in fb_defio will cause a deadlock, when it also tries to
699 * grab the same mutex.
700 */
701static void dlfb_dpy_deferred_io(struct fb_info *info,
702 struct list_head *pagelist)
703{
704 struct page *cur;
705 struct fb_deferred_io *fbdefio = info->fbdefio;
706 struct dlfb_data *dev = info->par;
707 struct urb *urb;
708 char *cmd;
709 cycles_t start_cycles, end_cycles;
710 int bytes_sent = 0;
711 int bytes_identical = 0;
712 int bytes_rendered = 0;
713
714 if (!fb_defio)
715 return;
716
717 if (!atomic_read(&dev->usb_active))
718 return;
719
720 start_cycles = get_cycles();
721
722 urb = dlfb_get_urb(dev);
723 if (!urb)
724 return;
725
726 cmd = urb->transfer_buffer;
727
728 /* walk the written page list and render each to device */
729 list_for_each_entry(cur, &fbdefio->pagelist, lru) {
730
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700731 if (dlfb_render_hline(dev, &urb, (char *) info->fix.smem_start,
Bernie Thompsond5ed5432010-09-05 16:35:39 -0700732 &cmd, cur->index << PAGE_SHIFT,
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700733 PAGE_SIZE, &bytes_identical, &bytes_sent))
734 goto error;
Bernie Thompsond5ed5432010-09-05 16:35:39 -0700735 bytes_rendered += PAGE_SIZE;
736 }
737
738 if (cmd > (char *) urb->transfer_buffer) {
739 /* Send partial buffer remaining before exiting */
740 int len = cmd - (char *) urb->transfer_buffer;
741 dlfb_submit_urb(dev, urb, len);
742 bytes_sent += len;
743 } else
744 dlfb_urb_completion(urb);
745
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700746error:
Bernie Thompsond5ed5432010-09-05 16:35:39 -0700747 atomic_add(bytes_sent, &dev->bytes_sent);
748 atomic_add(bytes_identical, &dev->bytes_identical);
749 atomic_add(bytes_rendered, &dev->bytes_rendered);
750 end_cycles = get_cycles();
751 atomic_add(((unsigned int) ((end_cycles - start_cycles)
752 >> 10)), /* Kcycles */
753 &dev->cpu_kcycles_used);
754}
755
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700756static int dlfb_get_edid(struct dlfb_data *dev, char *edid, int len)
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800757{
758 int i;
759 int ret;
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700760 char *rbuf;
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800761
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700762 rbuf = kmalloc(2, GFP_KERNEL);
763 if (!rbuf)
764 return 0;
765
766 for (i = 0; i < len; i++) {
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800767 ret = usb_control_msg(dev->udev,
768 usb_rcvctrlpipe(dev->udev, 0), (0x02),
769 (0x80 | (0x02 << 5)), i << 8, 0xA1, rbuf, 2,
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700770 HZ);
771 if (ret < 1) {
Paul Mundt81f6f3c2011-01-06 18:07:54 +0900772 pr_err("Read EDID byte %d failed err %x\n", i, ret);
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700773 i--;
774 break;
775 }
776 edid[i] = rbuf[1];
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800777 }
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700778
779 kfree(rbuf);
780
781 return i;
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800782}
783
Bernie Thompson45742032010-02-15 06:46:04 -0800784static int dlfb_ops_ioctl(struct fb_info *info, unsigned int cmd,
Bernie Thompson530f43a2010-02-15 06:46:21 -0800785 unsigned long arg)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700786{
Bernie Thompson530f43a2010-02-15 06:46:21 -0800787
788 struct dlfb_data *dev = info->par;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700789
Bernie Thompson530f43a2010-02-15 06:46:21 -0800790 if (!atomic_read(&dev->usb_active))
791 return 0;
792
793 /* TODO: Update X server to get this from sysfs instead */
794 if (cmd == DLFB_IOCTL_RETURN_EDID) {
Dr. David Alan Gilbertdef76602011-08-21 13:34:15 -0700795 void __user *edid = (void __user *)arg;
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700796 if (copy_to_user(edid, dev->edid, dev->edid_size))
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700797 return -EFAULT;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700798 return 0;
799 }
800
Bernie Thompson530f43a2010-02-15 06:46:21 -0800801 /* TODO: Help propose a standard fb.h ioctl to report mmap damage */
802 if (cmd == DLFB_IOCTL_REPORT_DAMAGE) {
Dr. David Alan Gilbertdef76602011-08-21 13:34:15 -0700803 struct dloarea area;
804
805 if (copy_from_user(&area, (void __user *)arg,
806 sizeof(struct dloarea)))
807 return -EFAULT;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700808
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700809 /*
810 * If we have a damage-aware client, turn fb_defio "off"
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300811 * To avoid perf imact of unnecessary page fault handling.
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700812 * Done by resetting the delay for this fb_info to a very
813 * long period. Pages will become writable and stay that way.
814 * Reset to normal value when all clients have closed this fb.
815 */
816 if (info->fbdefio)
817 info->fbdefio->delay = DL_DEFIO_WRITE_DISABLE;
818
Dr. David Alan Gilbertdef76602011-08-21 13:34:15 -0700819 if (area.x < 0)
820 area.x = 0;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700821
Dr. David Alan Gilbertdef76602011-08-21 13:34:15 -0700822 if (area.x > info->var.xres)
823 area.x = info->var.xres;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700824
Dr. David Alan Gilbertdef76602011-08-21 13:34:15 -0700825 if (area.y < 0)
826 area.y = 0;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700827
Dr. David Alan Gilbertdef76602011-08-21 13:34:15 -0700828 if (area.y > info->var.yres)
829 area.y = info->var.yres;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700830
Dr. David Alan Gilbertdef76602011-08-21 13:34:15 -0700831 dlfb_handle_damage(dev, area.x, area.y, area.w, area.h,
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700832 info->screen_base);
833 }
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700834
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700835 return 0;
836}
837
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700838/* taken from vesafb */
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700839static int
Bernie Thompson45742032010-02-15 06:46:04 -0800840dlfb_ops_setcolreg(unsigned regno, unsigned red, unsigned green,
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700841 unsigned blue, unsigned transp, struct fb_info *info)
842{
843 int err = 0;
844
845 if (regno >= info->cmap.len)
846 return 1;
847
848 if (regno < 16) {
849 if (info->var.red.offset == 10) {
850 /* 1:5:5:5 */
851 ((u32 *) (info->pseudo_palette))[regno] =
852 ((red & 0xf800) >> 1) |
853 ((green & 0xf800) >> 6) | ((blue & 0xf800) >> 11);
854 } else {
855 /* 0:5:6:5 */
856 ((u32 *) (info->pseudo_palette))[regno] =
857 ((red & 0xf800)) |
858 ((green & 0xfc00) >> 5) | ((blue & 0xf800) >> 11);
859 }
860 }
861
862 return err;
863}
864
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800865/*
866 * It's common for several clients to have framebuffer open simultaneously.
867 * e.g. both fbcon and X. Makes things interesting.
Bernie Thompson33077b82010-09-05 16:35:19 -0700868 * Assumes caller is holding info->lock (for open and release at least)
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800869 */
870static int dlfb_ops_open(struct fb_info *info, int user)
871{
872 struct dlfb_data *dev = info->par;
873
Bernie Thompsond5ed5432010-09-05 16:35:39 -0700874 /*
875 * fbcon aggressively connects to first framebuffer it finds,
876 * preventing other clients (X) from working properly. Usually
877 * not what the user wants. Fail by default with option to enable.
878 */
Dr. David Alan Gilbertdef76602011-08-21 13:34:15 -0700879 if ((user == 0) && (!console))
Bernie Thompsond5ed5432010-09-05 16:35:39 -0700880 return -EBUSY;
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800881
Bernie Thompson33077b82010-09-05 16:35:19 -0700882 /* If the USB device is gone, we don't accept new opens */
883 if (dev->virtualized)
884 return -ENODEV;
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800885
886 dev->fb_count++;
887
Bernie Thompson33077b82010-09-05 16:35:19 -0700888 kref_get(&dev->kref);
889
Bernie Thompsond5ed5432010-09-05 16:35:39 -0700890 if (fb_defio && (info->fbdefio == NULL)) {
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700891 /* enable defio at last moment if not disabled by client */
892
893 struct fb_deferred_io *fbdefio;
894
Joe Perches31a9f472010-10-31 15:33:55 -0700895 fbdefio = kmalloc(sizeof(struct fb_deferred_io), GFP_KERNEL);
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700896
897 if (fbdefio) {
898 fbdefio->delay = DL_DEFIO_WRITE_DELAY;
899 fbdefio->deferred_io = dlfb_dpy_deferred_io;
900 }
901
902 info->fbdefio = fbdefio;
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800903 fb_deferred_io_init(info);
904 }
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800905
Paul Mundt81f6f3c2011-01-06 18:07:54 +0900906 pr_notice("open /dev/fb%d user=%d fb_info=%p count=%d\n",
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800907 info->node, user, info, dev->fb_count);
908
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700909 return 0;
910}
911
Bernie Thompson4a4854d2010-02-15 06:45:55 -0800912/*
913 * Called when all client interfaces to start transactions have been disabled,
914 * and all references to our device instance (dlfb_data) are released.
915 * Every transaction must have a reference, so we know are fully spun down
916 */
Bernie Thompson33077b82010-09-05 16:35:19 -0700917static void dlfb_free(struct kref *kref)
Bernie Thompson4a4854d2010-02-15 06:45:55 -0800918{
919 struct dlfb_data *dev = container_of(kref, struct dlfb_data, kref);
920
Bernie Thompson33077b82010-09-05 16:35:19 -0700921 /* this function will wait for all in-flight urbs to complete */
922 if (dev->urbs.count > 0)
923 dlfb_free_urb_list(dev);
924
Bernie Thompson4a4854d2010-02-15 06:45:55 -0800925 if (dev->backing_buffer)
926 vfree(dev->backing_buffer);
927
Bernie Thompson33077b82010-09-05 16:35:19 -0700928 kfree(dev->edid);
929
Paul Mundt81f6f3c2011-01-06 18:07:54 +0900930 pr_warn("freeing dlfb_data %p\n", dev);
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800931
Bernie Thompson4a4854d2010-02-15 06:45:55 -0800932 kfree(dev);
933}
934
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700935static void dlfb_release_urb_work(struct work_struct *work)
936{
937 struct urb_node *unode = container_of(work, struct urb_node,
938 release_urb_work.work);
939
940 up(&unode->dev->urbs.limit_sem);
941}
Bernie Thompson33077b82010-09-05 16:35:19 -0700942
943static void dlfb_free_framebuffer_work(struct work_struct *work)
Bernie Thompson2469d5d2010-02-15 06:46:13 -0800944{
Bernie Thompson33077b82010-09-05 16:35:19 -0700945 struct dlfb_data *dev = container_of(work, struct dlfb_data,
946 free_framebuffer_work.work);
947 struct fb_info *info = dev->info;
948 int node = info->node;
949
950 unregister_framebuffer(info);
Bernie Thompson2469d5d2010-02-15 06:46:13 -0800951
952 if (info->cmap.len != 0)
953 fb_dealloc_cmap(&info->cmap);
954 if (info->monspecs.modedb)
955 fb_destroy_modedb(info->monspecs.modedb);
956 if (info->screen_base)
957 vfree(info->screen_base);
958
959 fb_destroy_modelist(&info->modelist);
960
Bernie Thompson33077b82010-09-05 16:35:19 -0700961 dev->info = 0;
962
963 /* Assume info structure is freed after this point */
Bernie Thompson2469d5d2010-02-15 06:46:13 -0800964 framebuffer_release(info);
965
Paul Mundt81f6f3c2011-01-06 18:07:54 +0900966 pr_warn("fb_info for /dev/fb%d has been freed\n", node);
Bernie Thompson33077b82010-09-05 16:35:19 -0700967
968 /* ref taken in probe() as part of registering framebfufer */
969 kref_put(&dev->kref, dlfb_free);
970}
971
972/*
973 * Assumes caller is holding info->lock mutex (for open and release at least)
974 */
975static int dlfb_ops_release(struct fb_info *info, int user)
976{
977 struct dlfb_data *dev = info->par;
978
979 dev->fb_count--;
980
981 /* We can't free fb_info here - fbmem will touch it when we return */
982 if (dev->virtualized && (dev->fb_count == 0))
983 schedule_delayed_work(&dev->free_framebuffer_work, HZ);
984
Bernie Thompson33077b82010-09-05 16:35:19 -0700985 if ((dev->fb_count == 0) && (info->fbdefio)) {
986 fb_deferred_io_cleanup(info);
987 kfree(info->fbdefio);
988 info->fbdefio = NULL;
989 info->fbops->fb_mmap = dlfb_ops_mmap;
990 }
Bernie Thompson33077b82010-09-05 16:35:19 -0700991
Paul Mundt81f6f3c2011-01-06 18:07:54 +0900992 pr_warn("released /dev/fb%d user=%d count=%d\n",
Bernie Thompson33077b82010-09-05 16:35:19 -0700993 info->node, user, dev->fb_count);
994
995 kref_put(&dev->kref, dlfb_free);
996
997 return 0;
Bernie Thompson2469d5d2010-02-15 06:46:13 -0800998}
999
1000/*
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001001 * Check whether a video mode is supported by the DisplayLink chip
1002 * We start from monitor's modes, so don't need to filter that here
1003 */
1004static int dlfb_is_valid_mode(struct fb_videomode *mode,
1005 struct fb_info *info)
1006{
1007 struct dlfb_data *dev = info->par;
1008
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001009 if (mode->xres * mode->yres > dev->sku_pixel_limit) {
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001010 pr_warn("%dx%d beyond chip capabilities\n",
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001011 mode->xres, mode->yres);
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001012 return 0;
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001013 }
1014
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001015 pr_info("%dx%d valid mode\n", mode->xres, mode->yres);
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001016
1017 return 1;
1018}
1019
1020static void dlfb_var_color_format(struct fb_var_screeninfo *var)
1021{
1022 const struct fb_bitfield red = { 11, 5, 0 };
1023 const struct fb_bitfield green = { 5, 6, 0 };
1024 const struct fb_bitfield blue = { 0, 5, 0 };
1025
1026 var->bits_per_pixel = 16;
1027 var->red = red;
1028 var->green = green;
1029 var->blue = blue;
1030}
1031
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001032static int dlfb_ops_check_var(struct fb_var_screeninfo *var,
1033 struct fb_info *info)
1034{
1035 struct fb_videomode mode;
1036
1037 /* TODO: support dynamically changing framebuffer size */
1038 if ((var->xres * var->yres * 2) > info->fix.smem_len)
1039 return -EINVAL;
1040
1041 /* set device-specific elements of var unrelated to mode */
1042 dlfb_var_color_format(var);
1043
1044 fb_var_to_videomode(&mode, var);
1045
1046 if (!dlfb_is_valid_mode(&mode, info))
1047 return -EINVAL;
1048
1049 return 0;
1050}
1051
1052static int dlfb_ops_set_par(struct fb_info *info)
1053{
1054 struct dlfb_data *dev = info->par;
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001055 int result;
1056 u16 *pix_framebuffer;
1057 int i;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001058
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001059 pr_notice("set_par mode %dx%d\n", info->var.xres, info->var.yres);
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001060
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001061 result = dlfb_set_video_mode(dev, &info->var);
1062
1063 if ((result == 0) && (dev->fb_count == 0)) {
1064
1065 /* paint greenscreen */
1066
1067 pix_framebuffer = (u16 *) info->screen_base;
1068 for (i = 0; i < info->fix.smem_len / 2; i++)
1069 pix_framebuffer[i] = 0x37e6;
1070
1071 dlfb_handle_damage(dev, 0, 0, info->var.xres, info->var.yres,
1072 info->screen_base);
1073 }
1074
1075 return result;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001076}
1077
Bernie Thompson58e7c3b2011-08-21 13:34:11 -07001078/* To fonzi the jukebox (e.g. make blanking changes take effect) */
1079static char *dlfb_dummy_render(char *buf)
1080{
1081 *buf++ = 0xAF;
1082 *buf++ = 0x6A; /* copy */
1083 *buf++ = 0x00; /* from address*/
1084 *buf++ = 0x00;
1085 *buf++ = 0x00;
1086 *buf++ = 0x01; /* one pixel */
1087 *buf++ = 0x00; /* to address */
1088 *buf++ = 0x00;
1089 *buf++ = 0x00;
1090 return buf;
1091}
1092
Bernie Thompson9825f702010-09-05 16:35:10 -07001093/*
1094 * In order to come back from full DPMS off, we need to set the mode again
1095 */
Bernie Thompson45742032010-02-15 06:46:04 -08001096static int dlfb_ops_blank(int blank_mode, struct fb_info *info)
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -07001097{
Bernie Thompson530f43a2010-02-15 06:46:21 -08001098 struct dlfb_data *dev = info->par;
Bernie Thompson58e7c3b2011-08-21 13:34:11 -07001099 char *bufptr;
1100 struct urb *urb;
Roberto De Ioris7316bc52009-06-10 23:02:19 -07001101
Bernie Thompson58e7c3b2011-08-21 13:34:11 -07001102 pr_info("/dev/fb%d FB_BLANK mode %d --> %d\n",
1103 info->node, dev->blank_mode, blank_mode);
Roberto De Ioris7316bc52009-06-10 23:02:19 -07001104
Bernie Thompson58e7c3b2011-08-21 13:34:11 -07001105 if ((dev->blank_mode == FB_BLANK_POWERDOWN) &&
1106 (blank_mode != FB_BLANK_POWERDOWN)) {
Bernie Thompson9825f702010-09-05 16:35:10 -07001107
Bernie Thompson58e7c3b2011-08-21 13:34:11 -07001108 /* returning from powerdown requires a fresh modeset */
Bernie Thompson9825f702010-09-05 16:35:10 -07001109 dlfb_set_video_mode(dev, &info->var);
1110 }
Roberto De Ioris7316bc52009-06-10 23:02:19 -07001111
Bernie Thompson58e7c3b2011-08-21 13:34:11 -07001112 urb = dlfb_get_urb(dev);
1113 if (!urb)
1114 return 0;
1115
1116 bufptr = (char *) urb->transfer_buffer;
1117 bufptr = dlfb_vidreg_lock(bufptr);
1118 bufptr = dlfb_blanking(bufptr, blank_mode);
1119 bufptr = dlfb_vidreg_unlock(bufptr);
1120
1121 /* seems like a render op is needed to have blank change take effect */
1122 bufptr = dlfb_dummy_render(bufptr);
1123
1124 dlfb_submit_urb(dev, urb, bufptr -
1125 (char *) urb->transfer_buffer);
1126
1127 dev->blank_mode = blank_mode;
1128
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001129 return 0;
1130}
1131
1132static struct fb_ops dlfb_ops = {
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001133 .owner = THIS_MODULE,
Paul Mundt1a3e5282011-01-06 17:29:24 +09001134 .fb_read = fb_sys_read,
Bernie Thompsond46ecb92010-09-05 16:35:04 -07001135 .fb_write = dlfb_ops_write,
Bernie Thompson45742032010-02-15 06:46:04 -08001136 .fb_setcolreg = dlfb_ops_setcolreg,
1137 .fb_fillrect = dlfb_ops_fillrect,
1138 .fb_copyarea = dlfb_ops_copyarea,
1139 .fb_imageblit = dlfb_ops_imageblit,
1140 .fb_mmap = dlfb_ops_mmap,
1141 .fb_ioctl = dlfb_ops_ioctl,
Bernie Thompson3e8f3d62010-02-15 06:46:26 -08001142 .fb_open = dlfb_ops_open,
Bernie Thompson45742032010-02-15 06:46:04 -08001143 .fb_release = dlfb_ops_release,
1144 .fb_blank = dlfb_ops_blank,
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001145 .fb_check_var = dlfb_ops_check_var,
1146 .fb_set_par = dlfb_ops_set_par,
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001147};
1148
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001149
Bernie Thompsoncc403dc2010-02-15 06:45:49 -08001150/*
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001151 * Assumes &info->lock held by caller
1152 * Assumes no active clients have framebuffer open
1153 */
1154static int dlfb_realloc_framebuffer(struct dlfb_data *dev, struct fb_info *info)
1155{
1156 int retval = -ENOMEM;
1157 int old_len = info->fix.smem_len;
1158 int new_len;
1159 unsigned char *old_fb = info->screen_base;
1160 unsigned char *new_fb;
Stuart Hopkinsd3189542011-08-21 13:34:17 -07001161 unsigned char *new_back = 0;
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001162
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001163 pr_warn("Reallocating framebuffer. Addresses will change!\n");
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001164
1165 new_len = info->fix.line_length * info->var.yres;
1166
1167 if (PAGE_ALIGN(new_len) > old_len) {
1168 /*
1169 * Alloc system memory for virtual framebuffer
1170 */
1171 new_fb = vmalloc(new_len);
1172 if (!new_fb) {
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001173 pr_err("Virtual framebuffer alloc failed\n");
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001174 goto error;
1175 }
1176
1177 if (info->screen_base) {
1178 memcpy(new_fb, old_fb, old_len);
1179 vfree(info->screen_base);
1180 }
1181
1182 info->screen_base = new_fb;
1183 info->fix.smem_len = PAGE_ALIGN(new_len);
1184 info->fix.smem_start = (unsigned long) new_fb;
1185 info->flags = udlfb_info_flags;
1186
1187 /*
1188 * Second framebuffer copy to mirror the framebuffer state
1189 * on the physical USB device. We can function without this.
1190 * But with imperfect damage info we may send pixels over USB
1191 * that were, in fact, unchanged - wasting limited USB bandwidth
1192 */
Stuart Hopkinsd3189542011-08-21 13:34:17 -07001193 if (shadow)
1194 new_back = vzalloc(new_len);
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001195 if (!new_back)
Linus Torvalds949f6712011-01-10 16:04:53 -08001196 pr_info("No shadow/backing buffer allocated\n");
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001197 else {
1198 if (dev->backing_buffer)
1199 vfree(dev->backing_buffer);
1200 dev->backing_buffer = new_back;
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001201 }
1202 }
1203
1204 retval = 0;
1205
1206error:
1207 return retval;
1208}
1209
1210/*
1211 * 1) Get EDID from hw, or use sw default
1212 * 2) Parse into various fb_info structs
1213 * 3) Allocate virtual framebuffer memory to back highest res mode
1214 *
1215 * Parses EDID into three places used by various parts of fbdev:
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001216 * fb_var_screeninfo contains the timing of the monitor's preferred mode
1217 * fb_info.monspecs is full parsed EDID info, including monspecs.modedb
1218 * fb_info.modelist is a linked list of all monitor & VESA modes which work
1219 *
1220 * If EDID is not readable/valid, then modelist is all VESA modes,
1221 * monspecs is NULL, and fb_var_screeninfo is set to safe VESA mode
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001222 * Returns 0 if successful
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001223 */
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001224static int dlfb_setup_modes(struct dlfb_data *dev,
1225 struct fb_info *info,
1226 char *default_edid, size_t default_edid_size)
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001227{
1228 int i;
1229 const struct fb_videomode *default_vmode = NULL;
1230 int result = 0;
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001231 char *edid;
1232 int tries = 3;
1233
1234 if (info->dev) /* only use mutex if info has been registered */
1235 mutex_lock(&info->lock);
1236
Paul Mundtb9f03a32011-01-06 18:04:02 +09001237 edid = kmalloc(EDID_LENGTH, GFP_KERNEL);
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001238 if (!edid) {
1239 result = -ENOMEM;
1240 goto error;
1241 }
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001242
1243 fb_destroy_modelist(&info->modelist);
1244 memset(&info->monspecs, 0, sizeof(info->monspecs));
1245
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001246 /*
1247 * Try to (re)read EDID from hardware first
1248 * EDID data may return, but not parse as valid
1249 * Try again a few times, in case of e.g. analog cable noise
1250 */
1251 while (tries--) {
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001252
Paul Mundtb9f03a32011-01-06 18:04:02 +09001253 i = dlfb_get_edid(dev, edid, EDID_LENGTH);
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001254
Paul Mundtb9f03a32011-01-06 18:04:02 +09001255 if (i >= EDID_LENGTH)
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001256 fb_edid_to_monspecs(edid, &info->monspecs);
1257
1258 if (info->monspecs.modedb_len > 0) {
1259 dev->edid = edid;
1260 dev->edid_size = i;
1261 break;
1262 }
1263 }
1264
1265 /* If that fails, use a previously returned EDID if available */
1266 if (info->monspecs.modedb_len == 0) {
1267
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001268 pr_err("Unable to get valid EDID from device/display\n");
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001269
1270 if (dev->edid) {
1271 fb_edid_to_monspecs(dev->edid, &info->monspecs);
1272 if (info->monspecs.modedb_len > 0)
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001273 pr_err("Using previously queried EDID\n");
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001274 }
1275 }
1276
1277 /* If that fails, use the default EDID we were handed */
1278 if (info->monspecs.modedb_len == 0) {
Paul Mundtb9f03a32011-01-06 18:04:02 +09001279 if (default_edid_size >= EDID_LENGTH) {
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001280 fb_edid_to_monspecs(default_edid, &info->monspecs);
1281 if (info->monspecs.modedb_len > 0) {
1282 memcpy(edid, default_edid, default_edid_size);
1283 dev->edid = edid;
1284 dev->edid_size = default_edid_size;
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001285 pr_err("Using default/backup EDID\n");
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001286 }
1287 }
1288 }
1289
1290 /* If we've got modes, let's pick a best default mode */
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001291 if (info->monspecs.modedb_len > 0) {
1292
1293 for (i = 0; i < info->monspecs.modedb_len; i++) {
1294 if (dlfb_is_valid_mode(&info->monspecs.modedb[i], info))
1295 fb_add_videomode(&info->monspecs.modedb[i],
1296 &info->modelist);
William Katsak9377c512011-06-23 13:16:29 +00001297 else {
1298 if (i == 0)
1299 /* if we've removed top/best mode */
1300 info->monspecs.misc
1301 &= ~FB_MISC_1ST_DETAIL;
1302 }
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001303 }
1304
1305 default_vmode = fb_find_best_display(&info->monspecs,
1306 &info->modelist);
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001307 }
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001308
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001309 /* If everything else has failed, fall back to safe default mode */
1310 if (default_vmode == NULL) {
1311
1312 struct fb_videomode fb_vmode = {0};
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001313
1314 /*
1315 * Add the standard VESA modes to our modelist
1316 * Since we don't have EDID, there may be modes that
1317 * overspec monitor and/or are incorrect aspect ratio, etc.
1318 * But at least the user has a chance to choose
1319 */
1320 for (i = 0; i < VESA_MODEDB_SIZE; i++) {
1321 if (dlfb_is_valid_mode((struct fb_videomode *)
1322 &vesa_modes[i], info))
1323 fb_add_videomode(&vesa_modes[i],
1324 &info->modelist);
1325 }
1326
1327 /*
1328 * default to resolution safe for projectors
1329 * (since they are most common case without EDID)
1330 */
1331 fb_vmode.xres = 800;
1332 fb_vmode.yres = 600;
1333 fb_vmode.refresh = 60;
1334 default_vmode = fb_find_nearest_mode(&fb_vmode,
1335 &info->modelist);
1336 }
1337
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001338 /* If we have good mode and no active clients*/
1339 if ((default_vmode != NULL) && (dev->fb_count == 0)) {
1340
1341 fb_videomode_to_var(&info->var, default_vmode);
1342 dlfb_var_color_format(&info->var);
1343
1344 /*
1345 * with mode size info, we can now alloc our framebuffer.
1346 */
1347 memcpy(&info->fix, &dlfb_fix, sizeof(dlfb_fix));
1348 info->fix.line_length = info->var.xres *
1349 (info->var.bits_per_pixel / 8);
1350
1351 result = dlfb_realloc_framebuffer(dev, info);
1352
1353 } else
1354 result = -EINVAL;
1355
1356error:
1357 if (edid && (dev->edid != edid))
1358 kfree(edid);
1359
1360 if (info->dev)
1361 mutex_unlock(&info->lock);
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001362
1363 return result;
1364}
1365
1366static ssize_t metrics_bytes_rendered_show(struct device *fbdev,
1367 struct device_attribute *a, char *buf) {
1368 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1369 struct dlfb_data *dev = fb_info->par;
1370 return snprintf(buf, PAGE_SIZE, "%u\n",
1371 atomic_read(&dev->bytes_rendered));
1372}
1373
1374static ssize_t metrics_bytes_identical_show(struct device *fbdev,
1375 struct device_attribute *a, char *buf) {
1376 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1377 struct dlfb_data *dev = fb_info->par;
1378 return snprintf(buf, PAGE_SIZE, "%u\n",
1379 atomic_read(&dev->bytes_identical));
1380}
1381
1382static ssize_t metrics_bytes_sent_show(struct device *fbdev,
1383 struct device_attribute *a, char *buf) {
1384 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1385 struct dlfb_data *dev = fb_info->par;
1386 return snprintf(buf, PAGE_SIZE, "%u\n",
1387 atomic_read(&dev->bytes_sent));
1388}
1389
1390static ssize_t metrics_cpu_kcycles_used_show(struct device *fbdev,
1391 struct device_attribute *a, char *buf) {
1392 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1393 struct dlfb_data *dev = fb_info->par;
1394 return snprintf(buf, PAGE_SIZE, "%u\n",
1395 atomic_read(&dev->cpu_kcycles_used));
1396}
1397
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001398static ssize_t edid_show(
1399 struct file *filp,
1400 struct kobject *kobj, struct bin_attribute *a,
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001401 char *buf, loff_t off, size_t count) {
1402 struct device *fbdev = container_of(kobj, struct device, kobj);
1403 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1404 struct dlfb_data *dev = fb_info->par;
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001405
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001406 if (dev->edid == NULL)
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001407 return 0;
1408
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001409 if ((off >= dev->edid_size) || (count > dev->edid_size))
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001410 return 0;
1411
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001412 if (off + count > dev->edid_size)
1413 count = dev->edid_size - off;
1414
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001415 pr_info("sysfs edid copy %p to %p, %d bytes\n",
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001416 dev->edid, buf, (int) count);
1417
1418 memcpy(buf, dev->edid, count);
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001419
1420 return count;
1421}
1422
Bernie Thompson8ef8cc42010-09-05 16:35:31 -07001423static ssize_t edid_store(
1424 struct file *filp,
1425 struct kobject *kobj, struct bin_attribute *a,
1426 char *src, loff_t src_off, size_t src_size) {
1427 struct device *fbdev = container_of(kobj, struct device, kobj);
1428 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1429 struct dlfb_data *dev = fb_info->par;
1430
1431 /* We only support write of entire EDID at once, no offset*/
Paul Mundtb9f03a32011-01-06 18:04:02 +09001432 if ((src_size != EDID_LENGTH) || (src_off != 0))
Bernie Thompson8ef8cc42010-09-05 16:35:31 -07001433 return 0;
1434
1435 dlfb_setup_modes(dev, fb_info, src, src_size);
1436
1437 if (dev->edid && (memcmp(src, dev->edid, src_size) == 0)) {
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001438 pr_info("sysfs written EDID is new default\n");
Bernie Thompson8ef8cc42010-09-05 16:35:31 -07001439 dlfb_ops_set_par(fb_info);
1440 return src_size;
1441 } else
1442 return 0;
1443}
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001444
1445static ssize_t metrics_reset_store(struct device *fbdev,
1446 struct device_attribute *attr,
1447 const char *buf, size_t count)
1448{
1449 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1450 struct dlfb_data *dev = fb_info->par;
1451
1452 atomic_set(&dev->bytes_rendered, 0);
1453 atomic_set(&dev->bytes_identical, 0);
1454 atomic_set(&dev->bytes_sent, 0);
1455 atomic_set(&dev->cpu_kcycles_used, 0);
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001456
1457 return count;
1458}
1459
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001460static struct bin_attribute edid_attr = {
1461 .attr.name = "edid",
Bernie Thompson8ef8cc42010-09-05 16:35:31 -07001462 .attr.mode = 0666,
Paul Mundtb9f03a32011-01-06 18:04:02 +09001463 .size = EDID_LENGTH,
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001464 .read = edid_show,
Bernie Thompson8ef8cc42010-09-05 16:35:31 -07001465 .write = edid_store
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001466};
1467
1468static struct device_attribute fb_device_attrs[] = {
1469 __ATTR_RO(metrics_bytes_rendered),
1470 __ATTR_RO(metrics_bytes_identical),
1471 __ATTR_RO(metrics_bytes_sent),
1472 __ATTR_RO(metrics_cpu_kcycles_used),
Greg Kroah-Hartman926c1112010-11-18 11:21:04 -08001473 __ATTR(metrics_reset, S_IWUSR, NULL, metrics_reset_store),
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001474};
1475
1476/*
Bernie Thompsoncc403dc2010-02-15 06:45:49 -08001477 * This is necessary before we can communicate with the display controller.
1478 */
1479static int dlfb_select_std_channel(struct dlfb_data *dev)
1480{
1481 int ret;
1482 u8 set_def_chn[] = { 0x57, 0xCD, 0xDC, 0xA7,
1483 0x1C, 0x88, 0x5E, 0x15,
1484 0x60, 0xFE, 0xC6, 0x97,
1485 0x16, 0x3D, 0x47, 0xF2 };
1486
1487 ret = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
1488 NR_USB_REQUEST_CHANNEL,
1489 (USB_DIR_OUT | USB_TYPE_VENDOR), 0, 0,
1490 set_def_chn, sizeof(set_def_chn), USB_CTRL_SET_TIMEOUT);
1491 return ret;
1492}
1493
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001494static int dlfb_parse_vendor_descriptor(struct dlfb_data *dev,
Andrew Kephartf2e1fc92011-08-21 13:34:13 -07001495 struct usb_interface *interface)
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001496{
1497 char *desc;
1498 char *buf;
1499 char *desc_end;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001500
Andrew Kephartf2e1fc92011-08-21 13:34:13 -07001501 int total_len = 0;
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001502
1503 buf = kzalloc(MAX_VENDOR_DESCRIPTOR_SIZE, GFP_KERNEL);
1504 if (!buf)
1505 return false;
1506 desc = buf;
1507
Andrew Kephartf2e1fc92011-08-21 13:34:13 -07001508 total_len = usb_get_descriptor(interface_to_usbdev(interface),
1509 0x5f, /* vendor specific */
1510 0, desc, MAX_VENDOR_DESCRIPTOR_SIZE);
1511
1512 /* if not found, look in configuration descriptor */
1513 if (total_len < 0) {
1514 if (0 == usb_get_extra_descriptor(interface->cur_altsetting,
1515 0x5f, &desc))
1516 total_len = (int) desc[0];
1517 }
1518
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001519 if (total_len > 5) {
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001520 pr_info("vendor descriptor length:%x data:%02x %02x %02x %02x" \
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001521 "%02x %02x %02x %02x %02x %02x %02x\n",
1522 total_len, desc[0],
1523 desc[1], desc[2], desc[3], desc[4], desc[5], desc[6],
1524 desc[7], desc[8], desc[9], desc[10]);
1525
1526 if ((desc[0] != total_len) || /* descriptor length */
1527 (desc[1] != 0x5f) || /* vendor descriptor type */
1528 (desc[2] != 0x01) || /* version (2 bytes) */
1529 (desc[3] != 0x00) ||
1530 (desc[4] != total_len - 2)) /* length after type */
1531 goto unrecognized;
1532
1533 desc_end = desc + total_len;
1534 desc += 5; /* the fixed header we've already parsed */
1535
1536 while (desc < desc_end) {
1537 u8 length;
1538 u16 key;
1539
1540 key = *((u16 *) desc);
1541 desc += sizeof(u16);
1542 length = *desc;
1543 desc++;
1544
1545 switch (key) {
1546 case 0x0200: { /* max_area */
1547 u32 max_area;
1548 max_area = le32_to_cpu(*((u32 *)desc));
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001549 pr_warn("DL chip limited to %d pixel modes\n",
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001550 max_area);
1551 dev->sku_pixel_limit = max_area;
1552 break;
1553 }
1554 default:
1555 break;
1556 }
1557 desc += length;
1558 }
Andrew Kephartf2e1fc92011-08-21 13:34:13 -07001559 } else {
1560 pr_info("vendor descriptor not available (%d)\n", total_len);
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001561 }
1562
1563 goto success;
1564
1565unrecognized:
1566 /* allow udlfb to load for now even if firmware unrecognized */
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001567 pr_err("Unrecognized vendor firmware descriptor\n");
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001568
1569success:
1570 kfree(buf);
1571 return true;
1572}
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001573static int dlfb_usb_probe(struct usb_interface *interface,
Bernie Thompson59277b62009-11-24 15:52:21 -08001574 const struct usb_device_id *id)
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001575{
Bernie Thompson59277b62009-11-24 15:52:21 -08001576 struct usb_device *usbdev;
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001577 struct dlfb_data *dev = 0;
Bernie Thompson33077b82010-09-05 16:35:19 -07001578 struct fb_info *info = 0;
Bernie Thompson59277b62009-11-24 15:52:21 -08001579 int retval = -ENOMEM;
Bernie Thompson2685cff2010-09-05 18:29:56 -07001580 int i;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001581
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001582 /* usb initialization */
1583
1584 usbdev = interface_to_usbdev(interface);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001585
Bernie Thompson59277b62009-11-24 15:52:21 -08001586 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1587 if (dev == NULL) {
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001588 err("dlfb_usb_probe: failed alloc of dev struct\n");
1589 goto error;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001590 }
1591
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001592 /* we need to wait for both usb and fbdev to spin down on disconnect */
1593 kref_init(&dev->kref); /* matching kref_put in usb .disconnect fn */
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001594 kref_get(&dev->kref); /* matching kref_put in free_framebuffer_work */
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001595
Bernie Thompson59277b62009-11-24 15:52:21 -08001596 dev->udev = usbdev;
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001597 dev->gdev = &usbdev->dev; /* our generic struct device * */
Bernie Thompson59277b62009-11-24 15:52:21 -08001598 usb_set_intfdata(interface, dev);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001599
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001600 pr_info("%s %s - serial #%s\n",
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001601 usbdev->manufacturer, usbdev->product, usbdev->serial);
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001602 pr_info("vid_%04x&pid_%04x&rev_%04x driver's dlfb_data struct at %p\n",
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001603 usbdev->descriptor.idVendor, usbdev->descriptor.idProduct,
1604 usbdev->descriptor.bcdDevice, dev);
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001605 pr_info("console enable=%d\n", console);
1606 pr_info("fb_defio enable=%d\n", fb_defio);
Stuart Hopkinsd3189542011-08-21 13:34:17 -07001607 pr_info("shadow enable=%d\n", shadow);
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001608
1609 dev->sku_pixel_limit = 2048 * 1152; /* default to maximum */
1610
Andrew Kephartf2e1fc92011-08-21 13:34:13 -07001611 if (!dlfb_parse_vendor_descriptor(dev, interface)) {
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001612 pr_err("firmware not recognized. Assume incompatible device\n");
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001613 goto error;
1614 }
1615
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001616 if (!dlfb_alloc_urb_list(dev, WRITES_IN_FLIGHT, MAX_TRANSFER)) {
1617 retval = -ENOMEM;
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001618 pr_err("dlfb_alloc_urb_list failed\n");
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001619 goto error;
1620 }
1621
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001622 /* We don't register a new USB class. Our client interface is fbdev */
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001623
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001624 /* allocates framebuffer driver structure, not framebuffer memory */
Kay Sieversc91a7932011-07-05 17:04:11 -07001625 info = framebuffer_alloc(0, &interface->dev);
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001626 if (!info) {
1627 retval = -ENOMEM;
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001628 pr_err("framebuffer_alloc failed\n");
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001629 goto error;
1630 }
Bernie Thompson33077b82010-09-05 16:35:19 -07001631
Bernie Thompson59277b62009-11-24 15:52:21 -08001632 dev->info = info;
1633 info->par = dev;
1634 info->pseudo_palette = dev->pseudo_palette;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001635 info->fbops = &dlfb_ops;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001636
Bernie Thompson59277b62009-11-24 15:52:21 -08001637 retval = fb_alloc_cmap(&info->cmap, 256, 0);
1638 if (retval < 0) {
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001639 pr_err("fb_alloc_cmap failed %x\n", retval);
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001640 goto error;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001641 }
1642
Bernie Thompson33077b82010-09-05 16:35:19 -07001643 INIT_DELAYED_WORK(&dev->free_framebuffer_work,
1644 dlfb_free_framebuffer_work);
1645
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001646 INIT_LIST_HEAD(&info->modelist);
1647
1648 retval = dlfb_setup_modes(dev, info, NULL, 0);
1649 if (retval != 0) {
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001650 pr_err("unable to find common mode for display and adapter\n");
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001651 goto error;
1652 }
1653
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001654 /* ready to begin using device */
1655
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001656 atomic_set(&dev->usb_active, 1);
Bernie Thompson59277b62009-11-24 15:52:21 -08001657 dlfb_select_std_channel(dev);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001658
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001659 dlfb_ops_check_var(&info->var, info);
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001660 dlfb_ops_set_par(info);
1661
Bernie Thompson59277b62009-11-24 15:52:21 -08001662 retval = register_framebuffer(info);
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001663 if (retval < 0) {
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001664 pr_err("register_framebuffer failed %d\n", retval);
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001665 goto error;
1666 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001667
Liu Yuan94cd1ae2011-04-18 10:44:38 +00001668 for (i = 0; i < ARRAY_SIZE(fb_device_attrs); i++) {
1669 retval = device_create_file(info->dev, &fb_device_attrs[i]);
1670 if (retval) {
1671 pr_err("device_create_file failed %d\n", retval);
1672 goto err_del_attrs;
1673 }
1674 }
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -07001675
Liu Yuan94cd1ae2011-04-18 10:44:38 +00001676 retval = device_create_bin_file(info->dev, &edid_attr);
1677 if (retval) {
1678 pr_err("device_create_bin_file failed %d\n", retval);
1679 goto err_del_attrs;
1680 }
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001681
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001682 pr_info("DisplayLink USB device /dev/fb%d attached. %dx%d resolution."
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001683 " Using %dK framebuffer memory\n", info->node,
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001684 info->var.xres, info->var.yres,
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001685 ((dev->backing_buffer) ?
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001686 info->fix.smem_len * 2 : info->fix.smem_len) >> 10);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001687 return 0;
1688
Liu Yuan94cd1ae2011-04-18 10:44:38 +00001689err_del_attrs:
1690 for (i -= 1; i >= 0; i--)
1691 device_remove_file(info->dev, &fb_device_attrs[i]);
1692
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001693error:
1694 if (dev) {
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001695
Bernie Thompson33077b82010-09-05 16:35:19 -07001696 if (info) {
1697 if (info->cmap.len != 0)
1698 fb_dealloc_cmap(&info->cmap);
1699 if (info->monspecs.modedb)
1700 fb_destroy_modedb(info->monspecs.modedb);
1701 if (info->screen_base)
1702 vfree(info->screen_base);
1703
1704 fb_destroy_modelist(&info->modelist);
1705
1706 framebuffer_release(info);
1707 }
1708
1709 if (dev->backing_buffer)
1710 vfree(dev->backing_buffer);
1711
1712 kref_put(&dev->kref, dlfb_free); /* ref for framebuffer */
1713 kref_put(&dev->kref, dlfb_free); /* last ref from kref_init */
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001714
1715 /* dev has been deallocated. Do not dereference */
1716 }
1717
Bernie Thompson59277b62009-11-24 15:52:21 -08001718 return retval;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001719}
1720
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001721static void dlfb_usb_disconnect(struct usb_interface *interface)
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001722{
Bernie Thompson59277b62009-11-24 15:52:21 -08001723 struct dlfb_data *dev;
1724 struct fb_info *info;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001725 int i;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001726
Bernie Thompson59277b62009-11-24 15:52:21 -08001727 dev = usb_get_intfdata(interface);
Bernie Thompson59277b62009-11-24 15:52:21 -08001728 info = dev->info;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001729
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001730 pr_info("USB disconnect starting\n");
Bernie Thompson33077b82010-09-05 16:35:19 -07001731
1732 /* we virtualize until all fb clients release. Then we free */
1733 dev->virtualized = true;
1734
1735 /* When non-active we'll update virtual framebuffer, but no new urbs */
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001736 atomic_set(&dev->usb_active, 0);
1737
Bernie Thompson33077b82010-09-05 16:35:19 -07001738 /* remove udlfb's sysfs interfaces */
1739 for (i = 0; i < ARRAY_SIZE(fb_device_attrs); i++)
1740 device_remove_file(info->dev, &fb_device_attrs[i]);
1741 device_remove_bin_file(info->dev, &edid_attr);
1742
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001743 usb_set_intfdata(interface, NULL);
1744
Bernie Thompson33077b82010-09-05 16:35:19 -07001745 /* if clients still have us open, will be freed on last close */
1746 if (dev->fb_count == 0)
1747 schedule_delayed_work(&dev->free_framebuffer_work, 0);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001748
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001749 /* release reference taken by kref_init in probe() */
Bernie Thompson33077b82010-09-05 16:35:19 -07001750 kref_put(&dev->kref, dlfb_free);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001751
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001752 /* consider dlfb_data freed */
1753
1754 return;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001755}
1756
1757static struct usb_driver dlfb_driver = {
1758 .name = "udlfb",
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001759 .probe = dlfb_usb_probe,
1760 .disconnect = dlfb_usb_disconnect,
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001761 .id_table = id_table,
1762};
1763
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001764static int __init dlfb_module_init(void)
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001765{
1766 int res;
1767
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001768 res = usb_register(&dlfb_driver);
1769 if (res)
1770 err("usb_register failed. Error number %d", res);
1771
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001772 return res;
1773}
1774
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001775static void __exit dlfb_module_exit(void)
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001776{
1777 usb_deregister(&dlfb_driver);
1778}
1779
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001780module_init(dlfb_module_init);
1781module_exit(dlfb_module_exit);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001782
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001783static void dlfb_urb_completion(struct urb *urb)
1784{
1785 struct urb_node *unode = urb->context;
1786 struct dlfb_data *dev = unode->dev;
1787 unsigned long flags;
1788
1789 /* sync/async unlink faults aren't errors */
1790 if (urb->status) {
1791 if (!(urb->status == -ENOENT ||
1792 urb->status == -ECONNRESET ||
1793 urb->status == -ESHUTDOWN)) {
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001794 pr_err("%s - nonzero write bulk status received: %d\n",
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001795 __func__, urb->status);
1796 atomic_set(&dev->lost_pixels, 1);
1797 }
1798 }
1799
1800 urb->transfer_buffer_length = dev->urbs.size; /* reset to actual */
1801
1802 spin_lock_irqsave(&dev->urbs.lock, flags);
1803 list_add_tail(&unode->entry, &dev->urbs.list);
1804 dev->urbs.available++;
1805 spin_unlock_irqrestore(&dev->urbs.lock, flags);
1806
Bernie Thompson5bea1fb2010-09-05 18:28:29 -07001807 /*
1808 * When using fb_defio, we deadlock if up() is called
1809 * while another is waiting. So queue to another process.
1810 */
1811 if (fb_defio)
1812 schedule_delayed_work(&unode->release_urb_work, 0);
1813 else
1814 up(&dev->urbs.limit_sem);
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001815}
1816
1817static void dlfb_free_urb_list(struct dlfb_data *dev)
1818{
1819 int count = dev->urbs.count;
1820 struct list_head *node;
1821 struct urb_node *unode;
1822 struct urb *urb;
1823 int ret;
1824 unsigned long flags;
1825
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001826 pr_notice("Waiting for completes and freeing all render urbs\n");
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001827
1828 /* keep waiting and freeing, until we've got 'em all */
1829 while (count--) {
Bernie Thompson33077b82010-09-05 16:35:19 -07001830
1831 /* Getting interrupted means a leak, but ok at shutdown*/
1832 ret = down_interruptible(&dev->urbs.limit_sem);
1833 if (ret)
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001834 break;
Bernie Thompson33077b82010-09-05 16:35:19 -07001835
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001836 spin_lock_irqsave(&dev->urbs.lock, flags);
1837
1838 node = dev->urbs.list.next; /* have reserved one with sem */
1839 list_del_init(node);
1840
1841 spin_unlock_irqrestore(&dev->urbs.lock, flags);
1842
1843 unode = list_entry(node, struct urb_node, entry);
1844 urb = unode->urb;
1845
1846 /* Free each separately allocated piece */
Greg Kroah-Hartmanc220cc32010-04-29 15:36:29 -07001847 usb_free_coherent(urb->dev, dev->urbs.size,
1848 urb->transfer_buffer, urb->transfer_dma);
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001849 usb_free_urb(urb);
1850 kfree(node);
1851 }
1852
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001853}
1854
1855static int dlfb_alloc_urb_list(struct dlfb_data *dev, int count, size_t size)
1856{
1857 int i = 0;
1858 struct urb *urb;
1859 struct urb_node *unode;
1860 char *buf;
1861
1862 spin_lock_init(&dev->urbs.lock);
1863
1864 dev->urbs.size = size;
1865 INIT_LIST_HEAD(&dev->urbs.list);
1866
1867 while (i < count) {
1868 unode = kzalloc(sizeof(struct urb_node), GFP_KERNEL);
1869 if (!unode)
1870 break;
1871 unode->dev = dev;
1872
Bernie Thompson5bea1fb2010-09-05 18:28:29 -07001873 INIT_DELAYED_WORK(&unode->release_urb_work,
1874 dlfb_release_urb_work);
1875
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001876 urb = usb_alloc_urb(0, GFP_KERNEL);
1877 if (!urb) {
1878 kfree(unode);
1879 break;
1880 }
1881 unode->urb = urb;
1882
Greg Kroah-Hartmanc220cc32010-04-29 15:36:29 -07001883 buf = usb_alloc_coherent(dev->udev, MAX_TRANSFER, GFP_KERNEL,
1884 &urb->transfer_dma);
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001885 if (!buf) {
1886 kfree(unode);
1887 usb_free_urb(urb);
1888 break;
1889 }
1890
1891 /* urb->transfer_buffer_length set to actual before submit */
1892 usb_fill_bulk_urb(urb, dev->udev, usb_sndbulkpipe(dev->udev, 1),
1893 buf, size, dlfb_urb_completion, unode);
1894 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1895
1896 list_add_tail(&unode->entry, &dev->urbs.list);
1897
1898 i++;
1899 }
1900
1901 sema_init(&dev->urbs.limit_sem, i);
1902 dev->urbs.count = i;
1903 dev->urbs.available = i;
1904
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001905 pr_notice("allocated %d %d byte urbs\n", i, (int) size);
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001906
1907 return i;
1908}
1909
1910static struct urb *dlfb_get_urb(struct dlfb_data *dev)
1911{
1912 int ret = 0;
1913 struct list_head *entry;
1914 struct urb_node *unode;
1915 struct urb *urb = NULL;
1916 unsigned long flags;
1917
1918 /* Wait for an in-flight buffer to complete and get re-queued */
1919 ret = down_timeout(&dev->urbs.limit_sem, GET_URB_TIMEOUT);
1920 if (ret) {
1921 atomic_set(&dev->lost_pixels, 1);
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001922 pr_warn("wait for urb interrupted: %x available: %d\n",
Bernie Thompson5bea1fb2010-09-05 18:28:29 -07001923 ret, dev->urbs.available);
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001924 goto error;
1925 }
1926
1927 spin_lock_irqsave(&dev->urbs.lock, flags);
1928
1929 BUG_ON(list_empty(&dev->urbs.list)); /* reserved one with limit_sem */
1930 entry = dev->urbs.list.next;
1931 list_del_init(entry);
1932 dev->urbs.available--;
1933
1934 spin_unlock_irqrestore(&dev->urbs.lock, flags);
1935
1936 unode = list_entry(entry, struct urb_node, entry);
1937 urb = unode->urb;
1938
1939error:
1940 return urb;
1941}
1942
1943static int dlfb_submit_urb(struct dlfb_data *dev, struct urb *urb, size_t len)
1944{
1945 int ret;
1946
1947 BUG_ON(len > dev->urbs.size);
1948
1949 urb->transfer_buffer_length = len; /* set to actual payload len */
1950 ret = usb_submit_urb(urb, GFP_KERNEL);
1951 if (ret) {
1952 dlfb_urb_completion(urb); /* because no one else will */
1953 atomic_set(&dev->lost_pixels, 1);
Paul Mundt81f6f3c2011-01-06 18:07:54 +09001954 pr_err("usb_submit_urb error %x\n", ret);
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001955 }
1956 return ret;
1957}
1958
Bernie Thompsond5ed5432010-09-05 16:35:39 -07001959module_param(console, bool, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
Bernie Thompson4aa7faf2011-08-21 13:35:39 -07001960MODULE_PARM_DESC(console, "Allow fbcon to open framebuffer");
Bernie Thompsond5ed5432010-09-05 16:35:39 -07001961
1962module_param(fb_defio, bool, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
Bernie Thompson9f811b72011-08-21 13:35:38 -07001963MODULE_PARM_DESC(fb_defio, "Page fault detection of mmap writes");
Bernie Thompsond5ed5432010-09-05 16:35:39 -07001964
Stuart Hopkinsd3189542011-08-21 13:34:17 -07001965module_param(shadow, bool, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
1966MODULE_PARM_DESC(shadow, "Shadow vid mem. Disable to save mem but lose perf");
1967
Bernie Thompson59277b62009-11-24 15:52:21 -08001968MODULE_AUTHOR("Roberto De Ioris <roberto@unbit.it>, "
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001969 "Jaya Kumar <jayakumar.lkml@gmail.com>, "
1970 "Bernie Thompson <bernie@plugable.com>");
1971MODULE_DESCRIPTION("DisplayLink kernel framebuffer driver");
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001972MODULE_LICENSE("GPL");
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001973