blob: d4f742ea0eda4130425ff0af9c759fd3404d81ce [file] [log] [blame]
Bernie Thompson59277b62009-11-24 15:52:21 -08001/*
2 * udlfb.c -- Framebuffer driver for DisplayLink USB controller
3 *
4 * Copyright (C) 2009 Roberto De Ioris <roberto@unbit.it>
5 * Copyright (C) 2009 Jaya Kumar <jayakumar.lkml@gmail.com>
Bernie Thompson2469d5d2010-02-15 06:46:13 -08006 * Copyright (C) 2009 Bernie Thompson <bernie@plugable.com>
Bernie Thompson59277b62009-11-24 15:52:21 -08007 *
8 * This file is subject to the terms and conditions of the GNU General Public
9 * License v2. See the file COPYING in the main directory of this archive for
10 * more details.
11 *
12 * Layout is based on skeletonfb by James Simmons and Geert Uytterhoeven,
13 * usb-skeleton by GregKH.
14 *
15 * Device-specific portions based on information from Displaylink, with work
16 * from Florian Echtler, Henrik Bjerregaard Pedersen, and others.
17 */
Roberto De Ioris88e58b12009-06-03 14:03:06 -070018
19#include <linux/module.h>
20#include <linux/kernel.h>
21#include <linux/init.h>
22#include <linux/usb.h>
23#include <linux/uaccess.h>
24#include <linux/mm.h>
25#include <linux/fb.h>
Amit Kucheriafb299002009-07-27 12:01:03 +030026#include <linux/vmalloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Bernie Thompson33077b82010-09-05 16:35:19 -070028#include <linux/delay.h>
Paul Mundt96f8d8642010-11-16 14:00:24 +090029#include <video/udlfb.h>
Roberto De Ioris88e58b12009-06-03 14:03:06 -070030
Bernie Thompson59277b62009-11-24 15:52:21 -080031static struct fb_fix_screeninfo dlfb_fix = {
Bernie Thompson2469d5d2010-02-15 06:46:13 -080032 .id = "udlfb",
Bernie Thompson1d31a9e2010-02-15 06:45:43 -080033 .type = FB_TYPE_PACKED_PIXELS,
34 .visual = FB_VISUAL_TRUECOLOR,
35 .xpanstep = 0,
36 .ypanstep = 0,
37 .ywrapstep = 0,
38 .accel = FB_ACCEL_NONE,
Bernie Thompson59277b62009-11-24 15:52:21 -080039};
Roberto De Ioris88e58b12009-06-03 14:03:06 -070040
Bernie Thompson2469d5d2010-02-15 06:46:13 -080041static const u32 udlfb_info_flags = FBINFO_DEFAULT | FBINFO_READS_FAST |
Bernie Thompson2469d5d2010-02-15 06:46:13 -080042 FBINFO_VIRTFB |
Bernie Thompson2469d5d2010-02-15 06:46:13 -080043 FBINFO_HWACCEL_IMAGEBLIT | FBINFO_HWACCEL_FILLRECT |
44 FBINFO_HWACCEL_COPYAREA | FBINFO_MISC_ALWAYS_SETPAR;
45
Bernie Thompsoncc403dc2010-02-15 06:45:49 -080046/*
47 * There are many DisplayLink-based products, all with unique PIDs. We are able
48 * to support all volume ones (circa 2009) with a single driver, so we match
49 * globally on VID. TODO: Probe() needs to detect when we might be running
50 * "future" chips, and bail on those, so a compatible driver can match.
51 */
52static struct usb_device_id id_table[] = {
53 {.idVendor = 0x17e9, .match_flags = USB_DEVICE_ID_MATCH_VENDOR,},
54 {},
55};
56MODULE_DEVICE_TABLE(usb, id_table);
Bernie Thompson59277b62009-11-24 15:52:21 -080057
Bernie Thompsond5ed5432010-09-05 16:35:39 -070058/* module options */
59static int console; /* Optionally allow fbcon to consume first framebuffer */
60static int fb_defio; /* Optionally enable experimental fb_defio mmap support */
Bernie Thompsondd8015f2010-02-15 06:46:35 -080061
Bernie Thompson4a4854d2010-02-15 06:45:55 -080062/* dlfb keeps a list of urbs for efficient bulk transfers */
63static void dlfb_urb_completion(struct urb *urb);
64static struct urb *dlfb_get_urb(struct dlfb_data *dev);
65static int dlfb_submit_urb(struct dlfb_data *dev, struct urb * urb, size_t len);
66static int dlfb_alloc_urb_list(struct dlfb_data *dev, int count, size_t size);
67static void dlfb_free_urb_list(struct dlfb_data *dev);
68
Bernie Thompson59277b62009-11-24 15:52:21 -080069/*
Bernie Thompsonbd808162010-02-15 06:46:48 -080070 * All DisplayLink bulk operations start with 0xAF, followed by specific code
71 * All operations are written to buffers which then later get sent to device
Bernie Thompson59277b62009-11-24 15:52:21 -080072 */
Bernie Thompson45742032010-02-15 06:46:04 -080073static char *dlfb_set_register(char *buf, u8 reg, u8 val)
Roberto De Ioris88e58b12009-06-03 14:03:06 -070074{
Bernie Thompson1d31a9e2010-02-15 06:45:43 -080075 *buf++ = 0xAF;
76 *buf++ = 0x20;
77 *buf++ = reg;
78 *buf++ = val;
79 return buf;
Roberto De Ioris88e58b12009-06-03 14:03:06 -070080}
81
Bernie Thompson45742032010-02-15 06:46:04 -080082static char *dlfb_vidreg_lock(char *buf)
Roberto De Ioris88e58b12009-06-03 14:03:06 -070083{
Bernie Thompson45742032010-02-15 06:46:04 -080084 return dlfb_set_register(buf, 0xFF, 0x00);
Bernie Thompson59277b62009-11-24 15:52:21 -080085}
Roberto De Ioris88e58b12009-06-03 14:03:06 -070086
Bernie Thompson45742032010-02-15 06:46:04 -080087static char *dlfb_vidreg_unlock(char *buf)
Bernie Thompson59277b62009-11-24 15:52:21 -080088{
Bernie Thompson45742032010-02-15 06:46:04 -080089 return dlfb_set_register(buf, 0xFF, 0xFF);
Bernie Thompson59277b62009-11-24 15:52:21 -080090}
Roberto De Ioris88e58b12009-06-03 14:03:06 -070091
Bernie Thompson59277b62009-11-24 15:52:21 -080092/*
Bernie Thompson530f43a2010-02-15 06:46:21 -080093 * On/Off for driving the DisplayLink framebuffer to the display
Bernie Thompson9825f702010-09-05 16:35:10 -070094 * 0x00 H and V sync on
95 * 0x01 H and V sync off (screen blank but powered)
96 * 0x07 DPMS powerdown (requires modeset to come back)
Bernie Thompson59277b62009-11-24 15:52:21 -080097 */
Bernie Thompson530f43a2010-02-15 06:46:21 -080098static char *dlfb_enable_hvsync(char *buf, bool enable)
Bernie Thompson59277b62009-11-24 15:52:21 -080099{
Bernie Thompson530f43a2010-02-15 06:46:21 -0800100 if (enable)
101 return dlfb_set_register(buf, 0x1F, 0x00);
102 else
Bernie Thompson9825f702010-09-05 16:35:10 -0700103 return dlfb_set_register(buf, 0x1F, 0x07);
Bernie Thompson59277b62009-11-24 15:52:21 -0800104}
105
Bernie Thompson45742032010-02-15 06:46:04 -0800106static char *dlfb_set_color_depth(char *buf, u8 selection)
Bernie Thompson59277b62009-11-24 15:52:21 -0800107{
Bernie Thompson45742032010-02-15 06:46:04 -0800108 return dlfb_set_register(buf, 0x00, selection);
Bernie Thompson59277b62009-11-24 15:52:21 -0800109}
110
Bernie Thompson45742032010-02-15 06:46:04 -0800111static char *dlfb_set_base16bpp(char *wrptr, u32 base)
Bernie Thompson59277b62009-11-24 15:52:21 -0800112{
Bernie Thompson1d31a9e2010-02-15 06:45:43 -0800113 /* the base pointer is 16 bits wide, 0x20 is hi byte. */
Bernie Thompson45742032010-02-15 06:46:04 -0800114 wrptr = dlfb_set_register(wrptr, 0x20, base >> 16);
115 wrptr = dlfb_set_register(wrptr, 0x21, base >> 8);
116 return dlfb_set_register(wrptr, 0x22, base);
Bernie Thompson59277b62009-11-24 15:52:21 -0800117}
118
Bernie Thompsonbd808162010-02-15 06:46:48 -0800119/*
120 * DisplayLink HW has separate 16bpp and 8bpp framebuffers.
121 * In 24bpp modes, the low 323 RGB bits go in the 8bpp framebuffer
122 */
Bernie Thompson45742032010-02-15 06:46:04 -0800123static char *dlfb_set_base8bpp(char *wrptr, u32 base)
Bernie Thompson59277b62009-11-24 15:52:21 -0800124{
Bernie Thompson45742032010-02-15 06:46:04 -0800125 wrptr = dlfb_set_register(wrptr, 0x26, base >> 16);
126 wrptr = dlfb_set_register(wrptr, 0x27, base >> 8);
127 return dlfb_set_register(wrptr, 0x28, base);
Bernie Thompson59277b62009-11-24 15:52:21 -0800128}
129
Bernie Thompson45742032010-02-15 06:46:04 -0800130static char *dlfb_set_register_16(char *wrptr, u8 reg, u16 value)
Bernie Thompson59277b62009-11-24 15:52:21 -0800131{
Bernie Thompson45742032010-02-15 06:46:04 -0800132 wrptr = dlfb_set_register(wrptr, reg, value >> 8);
133 return dlfb_set_register(wrptr, reg+1, value);
Bernie Thompson59277b62009-11-24 15:52:21 -0800134}
135
136/*
137 * This is kind of weird because the controller takes some
138 * register values in a different byte order than other registers.
139 */
Bernie Thompson45742032010-02-15 06:46:04 -0800140static char *dlfb_set_register_16be(char *wrptr, u8 reg, u16 value)
Bernie Thompson59277b62009-11-24 15:52:21 -0800141{
Bernie Thompson45742032010-02-15 06:46:04 -0800142 wrptr = dlfb_set_register(wrptr, reg, value);
143 return dlfb_set_register(wrptr, reg+1, value >> 8);
Bernie Thompson59277b62009-11-24 15:52:21 -0800144}
145
146/*
147 * LFSR is linear feedback shift register. The reason we have this is
148 * because the display controller needs to minimize the clock depth of
149 * various counters used in the display path. So this code reverses the
150 * provided value into the lfsr16 value by counting backwards to get
151 * the value that needs to be set in the hardware comparator to get the
152 * same actual count. This makes sense once you read above a couple of
153 * times and think about it from a hardware perspective.
154 */
Bernie Thompsonbd808162010-02-15 06:46:48 -0800155static u16 dlfb_lfsr16(u16 actual_count)
Bernie Thompson59277b62009-11-24 15:52:21 -0800156{
157 u32 lv = 0xFFFF; /* This is the lfsr value that the hw starts with */
158
159 while (actual_count--) {
160 lv = ((lv << 1) |
161 (((lv >> 15) ^ (lv >> 4) ^ (lv >> 2) ^ (lv >> 1)) & 1))
162 & 0xFFFF;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700163 }
Bernie Thompson59277b62009-11-24 15:52:21 -0800164
165 return (u16) lv;
166}
167
168/*
169 * This does LFSR conversion on the value that is to be written.
170 * See LFSR explanation above for more detail.
171 */
Bernie Thompson45742032010-02-15 06:46:04 -0800172static char *dlfb_set_register_lfsr16(char *wrptr, u8 reg, u16 value)
Bernie Thompson59277b62009-11-24 15:52:21 -0800173{
Bernie Thompsonbd808162010-02-15 06:46:48 -0800174 return dlfb_set_register_16(wrptr, reg, dlfb_lfsr16(value));
Bernie Thompson59277b62009-11-24 15:52:21 -0800175}
176
177/*
178 * This takes a standard fbdev screeninfo struct and all of its monitor mode
179 * details and converts them into the DisplayLink equivalent register commands.
180 */
Bernie Thompson45742032010-02-15 06:46:04 -0800181static char *dlfb_set_vid_cmds(char *wrptr, struct fb_var_screeninfo *var)
Bernie Thompson59277b62009-11-24 15:52:21 -0800182{
183 u16 xds, yds;
184 u16 xde, yde;
185 u16 yec;
186
Bernie Thompson59277b62009-11-24 15:52:21 -0800187 /* x display start */
188 xds = var->left_margin + var->hsync_len;
Bernie Thompson45742032010-02-15 06:46:04 -0800189 wrptr = dlfb_set_register_lfsr16(wrptr, 0x01, xds);
Bernie Thompson59277b62009-11-24 15:52:21 -0800190 /* x display end */
191 xde = xds + var->xres;
Bernie Thompson45742032010-02-15 06:46:04 -0800192 wrptr = dlfb_set_register_lfsr16(wrptr, 0x03, xde);
Bernie Thompson59277b62009-11-24 15:52:21 -0800193
194 /* y display start */
195 yds = var->upper_margin + var->vsync_len;
Bernie Thompson45742032010-02-15 06:46:04 -0800196 wrptr = dlfb_set_register_lfsr16(wrptr, 0x05, yds);
Bernie Thompson59277b62009-11-24 15:52:21 -0800197 /* y display end */
198 yde = yds + var->yres;
Bernie Thompson45742032010-02-15 06:46:04 -0800199 wrptr = dlfb_set_register_lfsr16(wrptr, 0x07, yde);
Bernie Thompson59277b62009-11-24 15:52:21 -0800200
201 /* x end count is active + blanking - 1 */
Bernie Thompson45742032010-02-15 06:46:04 -0800202 wrptr = dlfb_set_register_lfsr16(wrptr, 0x09,
203 xde + var->right_margin - 1);
Bernie Thompson59277b62009-11-24 15:52:21 -0800204
205 /* libdlo hardcodes hsync start to 1 */
Bernie Thompson45742032010-02-15 06:46:04 -0800206 wrptr = dlfb_set_register_lfsr16(wrptr, 0x0B, 1);
Bernie Thompson59277b62009-11-24 15:52:21 -0800207
208 /* hsync end is width of sync pulse + 1 */
Bernie Thompson45742032010-02-15 06:46:04 -0800209 wrptr = dlfb_set_register_lfsr16(wrptr, 0x0D, var->hsync_len + 1);
Bernie Thompson59277b62009-11-24 15:52:21 -0800210
211 /* hpixels is active pixels */
Bernie Thompson45742032010-02-15 06:46:04 -0800212 wrptr = dlfb_set_register_16(wrptr, 0x0F, var->xres);
Bernie Thompson59277b62009-11-24 15:52:21 -0800213
214 /* yendcount is vertical active + vertical blanking */
215 yec = var->yres + var->upper_margin + var->lower_margin +
216 var->vsync_len;
Bernie Thompson45742032010-02-15 06:46:04 -0800217 wrptr = dlfb_set_register_lfsr16(wrptr, 0x11, yec);
Bernie Thompson59277b62009-11-24 15:52:21 -0800218
219 /* libdlo hardcodes vsync start to 0 */
Bernie Thompson45742032010-02-15 06:46:04 -0800220 wrptr = dlfb_set_register_lfsr16(wrptr, 0x13, 0);
Bernie Thompson59277b62009-11-24 15:52:21 -0800221
222 /* vsync end is width of vsync pulse */
Bernie Thompson45742032010-02-15 06:46:04 -0800223 wrptr = dlfb_set_register_lfsr16(wrptr, 0x15, var->vsync_len);
Bernie Thompson59277b62009-11-24 15:52:21 -0800224
225 /* vpixels is active pixels */
Bernie Thompson45742032010-02-15 06:46:04 -0800226 wrptr = dlfb_set_register_16(wrptr, 0x17, var->yres);
Bernie Thompson59277b62009-11-24 15:52:21 -0800227
228 /* convert picoseconds to 5kHz multiple for pclk5k = x * 1E12/5k */
Bernie Thompson45742032010-02-15 06:46:04 -0800229 wrptr = dlfb_set_register_16be(wrptr, 0x1B,
230 200*1000*1000/var->pixclock);
Bernie Thompson59277b62009-11-24 15:52:21 -0800231
232 return wrptr;
233}
234
235/*
236 * This takes a standard fbdev screeninfo struct that was fetched or prepared
237 * and then generates the appropriate command sequence that then drives the
238 * display controller.
239 */
240static int dlfb_set_video_mode(struct dlfb_data *dev,
241 struct fb_var_screeninfo *var)
242{
243 char *buf;
244 char *wrptr;
245 int retval = 0;
246 int writesize;
Bernie Thompson530f43a2010-02-15 06:46:21 -0800247 struct urb *urb;
Bernie Thompson59277b62009-11-24 15:52:21 -0800248
Bernie Thompson530f43a2010-02-15 06:46:21 -0800249 if (!atomic_read(&dev->usb_active))
250 return -EPERM;
251
252 urb = dlfb_get_urb(dev);
253 if (!urb)
254 return -ENOMEM;
Bernie Thompson2685cff2010-09-05 18:29:56 -0700255
Bernie Thompson530f43a2010-02-15 06:46:21 -0800256 buf = (char *) urb->transfer_buffer;
Bernie Thompson59277b62009-11-24 15:52:21 -0800257
258 /*
259 * This first section has to do with setting the base address on the
260 * controller * associated with the display. There are 2 base
261 * pointers, currently, we only * use the 16 bpp segment.
262 */
Bernie Thompson45742032010-02-15 06:46:04 -0800263 wrptr = dlfb_vidreg_lock(buf);
264 wrptr = dlfb_set_color_depth(wrptr, 0x00);
Bernie Thompson59277b62009-11-24 15:52:21 -0800265 /* set base for 16bpp segment to 0 */
Bernie Thompson45742032010-02-15 06:46:04 -0800266 wrptr = dlfb_set_base16bpp(wrptr, 0);
Bernie Thompson59277b62009-11-24 15:52:21 -0800267 /* set base for 8bpp segment to end of fb */
Bernie Thompson45742032010-02-15 06:46:04 -0800268 wrptr = dlfb_set_base8bpp(wrptr, dev->info->fix.smem_len);
Bernie Thompson59277b62009-11-24 15:52:21 -0800269
Bernie Thompson45742032010-02-15 06:46:04 -0800270 wrptr = dlfb_set_vid_cmds(wrptr, var);
Bernie Thompson530f43a2010-02-15 06:46:21 -0800271 wrptr = dlfb_enable_hvsync(wrptr, true);
Bernie Thompson45742032010-02-15 06:46:04 -0800272 wrptr = dlfb_vidreg_unlock(wrptr);
Bernie Thompson59277b62009-11-24 15:52:21 -0800273
274 writesize = wrptr - buf;
275
Bernie Thompson530f43a2010-02-15 06:46:21 -0800276 retval = dlfb_submit_urb(dev, urb, writesize);
Bernie Thompson59277b62009-11-24 15:52:21 -0800277
Bernie Thompson59277b62009-11-24 15:52:21 -0800278 return retval;
279}
280
Bernie Thompson45742032010-02-15 06:46:04 -0800281static int dlfb_ops_mmap(struct fb_info *info, struct vm_area_struct *vma)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700282{
283 unsigned long start = vma->vm_start;
284 unsigned long size = vma->vm_end - vma->vm_start;
285 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
286 unsigned long page, pos;
287
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700288 if (offset + size > info->fix.smem_len)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700289 return -EINVAL;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700290
291 pos = (unsigned long)info->fix.smem_start + offset;
292
Bernie Thompson2685cff2010-09-05 18:29:56 -0700293 dl_notice("mmap() framebuffer addr:%lu size:%lu\n",
294 pos, size);
295
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700296 while (size > 0) {
297 page = vmalloc_to_pfn((void *)pos);
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700298 if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED))
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700299 return -EAGAIN;
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700300
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700301 start += PAGE_SIZE;
302 pos += PAGE_SIZE;
303 if (size > PAGE_SIZE)
304 size -= PAGE_SIZE;
305 else
306 size = 0;
307 }
308
309 vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
310 return 0;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700311}
312
Bernie Thompson530f43a2010-02-15 06:46:21 -0800313/*
314 * Trims identical data from front and back of line
315 * Sets new front buffer address and width
316 * And returns byte count of identical pixels
317 * Assumes CPU natural alignment (unsigned long)
318 * for back and front buffer ptrs and width
319 */
320static int dlfb_trim_hline(const u8 *bback, const u8 **bfront, int *width_bytes)
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700321{
Bernie Thompson530f43a2010-02-15 06:46:21 -0800322 int j, k;
323 const unsigned long *back = (const unsigned long *) bback;
324 const unsigned long *front = (const unsigned long *) *bfront;
325 const int width = *width_bytes / sizeof(unsigned long);
326 int identical = width;
327 int start = width;
328 int end = width;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700329
Bernie Thompson530f43a2010-02-15 06:46:21 -0800330 prefetch((void *) front);
331 prefetch((void *) back);
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700332
Bernie Thompson530f43a2010-02-15 06:46:21 -0800333 for (j = 0; j < width; j++) {
334 if (back[j] != front[j]) {
335 start = j;
336 break;
337 }
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700338 }
339
Bernie Thompson530f43a2010-02-15 06:46:21 -0800340 for (k = width - 1; k > j; k--) {
341 if (back[k] != front[k]) {
342 end = k+1;
343 break;
344 }
345 }
346
347 identical = start + (width - end);
348 *bfront = (u8 *) &front[start];
349 *width_bytes = (end - start) * sizeof(unsigned long);
350
351 return identical * sizeof(unsigned long);
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700352}
353
354/*
Pavel Machek3b7b31f2010-04-03 07:00:37 +0200355 * Render a command stream for an encoded horizontal line segment of pixels.
356 *
357 * A command buffer holds several commands.
358 * It always begins with a fresh command header
359 * (the protocol doesn't require this, but we enforce it to allow
360 * multiple buffers to be potentially encoded and sent in parallel).
361 * A single command encodes one contiguous horizontal line of pixels
362 *
363 * The function relies on the client to do all allocation, so that
364 * rendering can be done directly to output buffers (e.g. USB URBs).
365 * The function fills the supplied command buffer, providing information
366 * on where it left off, so the client may call in again with additional
367 * buffers if the line will take several buffers to complete.
368 *
369 * A single command can transmit a maximum of 256 pixels,
370 * regardless of the compression ratio (protocol design limit).
371 * To the hardware, 0 for a size byte means 256
Bernie Thompson2685cff2010-09-05 18:29:56 -0700372 *
Pavel Machek3b7b31f2010-04-03 07:00:37 +0200373 * Rather than 256 pixel commands which are either rl or raw encoded,
374 * the rlx command simply assumes alternating raw and rl spans within one cmd.
375 * This has a slightly larger header overhead, but produces more even results.
376 * It also processes all data (read and write) in a single pass.
377 * Performance benchmarks of common cases show it having just slightly better
Bernie Thompson2685cff2010-09-05 18:29:56 -0700378 * compression than 256 pixel raw or rle commands, with similar CPU consumpion.
Pavel Machek3b7b31f2010-04-03 07:00:37 +0200379 * But for very rl friendly data, will compress not quite as well.
380 */
Bernie Thompson530f43a2010-02-15 06:46:21 -0800381static void dlfb_compress_hline(
382 const uint16_t **pixel_start_ptr,
383 const uint16_t *const pixel_end,
384 uint32_t *device_address_ptr,
385 uint8_t **command_buffer_ptr,
386 const uint8_t *const cmd_buffer_end)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700387{
Bernie Thompson530f43a2010-02-15 06:46:21 -0800388 const uint16_t *pixel = *pixel_start_ptr;
389 uint32_t dev_addr = *device_address_ptr;
390 uint8_t *cmd = *command_buffer_ptr;
391 const int bpp = 2;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700392
Bernie Thompson530f43a2010-02-15 06:46:21 -0800393 while ((pixel_end > pixel) &&
394 (cmd_buffer_end - MIN_RLX_CMD_BYTES > cmd)) {
395 uint8_t *raw_pixels_count_byte = 0;
396 uint8_t *cmd_pixels_count_byte = 0;
397 const uint16_t *raw_pixel_start = 0;
398 const uint16_t *cmd_pixel_start, *cmd_pixel_end = 0;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700399
Bernie Thompson530f43a2010-02-15 06:46:21 -0800400 prefetchw((void *) cmd); /* pull in one cache line at least */
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700401
Bernie Thompson530f43a2010-02-15 06:46:21 -0800402 *cmd++ = 0xAF;
403 *cmd++ = 0x6B;
Bernie Thompson1572f912010-09-05 16:35:27 -0700404 *cmd++ = (uint8_t) ((dev_addr >> 16) & 0xFF);
405 *cmd++ = (uint8_t) ((dev_addr >> 8) & 0xFF);
406 *cmd++ = (uint8_t) ((dev_addr) & 0xFF);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700407
Bernie Thompson530f43a2010-02-15 06:46:21 -0800408 cmd_pixels_count_byte = cmd++; /* we'll know this later */
409 cmd_pixel_start = pixel;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700410
Bernie Thompson530f43a2010-02-15 06:46:21 -0800411 raw_pixels_count_byte = cmd++; /* we'll know this later */
412 raw_pixel_start = pixel;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700413
Bernie Thompson530f43a2010-02-15 06:46:21 -0800414 cmd_pixel_end = pixel + min(MAX_CMD_PIXELS + 1,
415 min((int)(pixel_end - pixel),
416 (int)(cmd_buffer_end - cmd) / bpp));
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700417
Bernie Thompson530f43a2010-02-15 06:46:21 -0800418 prefetch_range((void *) pixel, (cmd_pixel_end - pixel) * bpp);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700419
Bernie Thompson530f43a2010-02-15 06:46:21 -0800420 while (pixel < cmd_pixel_end) {
421 const uint16_t * const repeating_pixel = pixel;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700422
Bernie Thompson530f43a2010-02-15 06:46:21 -0800423 *(uint16_t *)cmd = cpu_to_be16p(pixel);
424 cmd += 2;
425 pixel++;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700426
Bernie Thompson530f43a2010-02-15 06:46:21 -0800427 if (unlikely((pixel < cmd_pixel_end) &&
428 (*pixel == *repeating_pixel))) {
429 /* go back and fill in raw pixel count */
430 *raw_pixels_count_byte = ((repeating_pixel -
431 raw_pixel_start) + 1) & 0xFF;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700432
Bernie Thompson530f43a2010-02-15 06:46:21 -0800433 while ((pixel < cmd_pixel_end)
434 && (*pixel == *repeating_pixel)) {
435 pixel++;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700436 }
Bernie Thompson59277b62009-11-24 15:52:21 -0800437
Bernie Thompson530f43a2010-02-15 06:46:21 -0800438 /* immediately after raw data is repeat byte */
439 *cmd++ = ((pixel - repeating_pixel) - 1) & 0xFF;
Bernie Thompson59277b62009-11-24 15:52:21 -0800440
Bernie Thompson530f43a2010-02-15 06:46:21 -0800441 /* Then start another raw pixel span */
442 raw_pixel_start = pixel;
443 raw_pixels_count_byte = cmd++;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700444 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700445 }
446
Bernie Thompson530f43a2010-02-15 06:46:21 -0800447 if (pixel > raw_pixel_start) {
448 /* finalize last RAW span */
449 *raw_pixels_count_byte = (pixel-raw_pixel_start) & 0xFF;
450 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700451
Bernie Thompson530f43a2010-02-15 06:46:21 -0800452 *cmd_pixels_count_byte = (pixel - cmd_pixel_start) & 0xFF;
453 dev_addr += (pixel - cmd_pixel_start) * bpp;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700454 }
455
Bernie Thompson530f43a2010-02-15 06:46:21 -0800456 if (cmd_buffer_end <= MIN_RLX_CMD_BYTES + cmd) {
457 /* Fill leftover bytes with no-ops */
458 if (cmd_buffer_end > cmd)
459 memset(cmd, 0xAF, cmd_buffer_end - cmd);
460 cmd = (uint8_t *) cmd_buffer_end;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700461 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700462
Bernie Thompson530f43a2010-02-15 06:46:21 -0800463 *command_buffer_ptr = cmd;
464 *pixel_start_ptr = pixel;
465 *device_address_ptr = dev_addr;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700466
Bernie Thompson530f43a2010-02-15 06:46:21 -0800467 return;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700468}
469
Bernie Thompson530f43a2010-02-15 06:46:21 -0800470/*
471 * There are 3 copies of every pixel: The front buffer that the fbdev
472 * client renders to, the actual framebuffer across the USB bus in hardware
473 * (that we can only write to, slowly, and can never read), and (optionally)
474 * our shadow copy that tracks what's been sent to that hardware buffer.
475 */
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700476static int dlfb_render_hline(struct dlfb_data *dev, struct urb **urb_ptr,
Bernie Thompson530f43a2010-02-15 06:46:21 -0800477 const char *front, char **urb_buf_ptr,
478 u32 byte_offset, u32 byte_width,
479 int *ident_ptr, int *sent_ptr)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700480{
Bernie Thompson530f43a2010-02-15 06:46:21 -0800481 const u8 *line_start, *line_end, *next_pixel;
482 u32 dev_addr = dev->base16 + byte_offset;
483 struct urb *urb = *urb_ptr;
484 u8 *cmd = *urb_buf_ptr;
485 u8 *cmd_end = (u8 *) urb->transfer_buffer + urb->transfer_buffer_length;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700486
Bernie Thompson530f43a2010-02-15 06:46:21 -0800487 line_start = (u8 *) (front + byte_offset);
488 next_pixel = line_start;
489 line_end = next_pixel + byte_width;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700490
Bernie Thompson530f43a2010-02-15 06:46:21 -0800491 if (dev->backing_buffer) {
492 int offset;
493 const u8 *back_start = (u8 *) (dev->backing_buffer
494 + byte_offset);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700495
Bernie Thompson530f43a2010-02-15 06:46:21 -0800496 *ident_ptr += dlfb_trim_hline(back_start, &next_pixel,
497 &byte_width);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700498
Bernie Thompson530f43a2010-02-15 06:46:21 -0800499 offset = next_pixel - line_start;
500 line_end = next_pixel + byte_width;
501 dev_addr += offset;
502 back_start += offset;
503 line_start += offset;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700504
Bernie Thompson530f43a2010-02-15 06:46:21 -0800505 memcpy((char *)back_start, (char *) line_start,
506 byte_width);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700507 }
508
Bernie Thompson530f43a2010-02-15 06:46:21 -0800509 while (next_pixel < line_end) {
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700510
Bernie Thompson530f43a2010-02-15 06:46:21 -0800511 dlfb_compress_hline((const uint16_t **) &next_pixel,
512 (const uint16_t *) line_end, &dev_addr,
513 (u8 **) &cmd, (u8 *) cmd_end);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700514
Bernie Thompson530f43a2010-02-15 06:46:21 -0800515 if (cmd >= cmd_end) {
516 int len = cmd - (u8 *) urb->transfer_buffer;
517 if (dlfb_submit_urb(dev, urb, len))
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700518 return 1; /* lost pixels is set */
Bernie Thompson530f43a2010-02-15 06:46:21 -0800519 *sent_ptr += len;
520 urb = dlfb_get_urb(dev);
521 if (!urb)
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700522 return 1; /* lost_pixels is set */
Bernie Thompson530f43a2010-02-15 06:46:21 -0800523 *urb_ptr = urb;
524 cmd = urb->transfer_buffer;
525 cmd_end = &cmd[urb->transfer_buffer_length];
526 }
527 }
528
529 *urb_buf_ptr = cmd;
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700530
531 return 0;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700532}
533
Bernie Thompson530f43a2010-02-15 06:46:21 -0800534int dlfb_handle_damage(struct dlfb_data *dev, int x, int y,
535 int width, int height, char *data)
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700536{
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700537 int i, ret;
Bernie Thompson530f43a2010-02-15 06:46:21 -0800538 char *cmd;
539 cycles_t start_cycles, end_cycles;
540 int bytes_sent = 0;
541 int bytes_identical = 0;
542 struct urb *urb;
543 int aligned_x;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700544
Bernie Thompson530f43a2010-02-15 06:46:21 -0800545 start_cycles = get_cycles();
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700546
Bernie Thompson530f43a2010-02-15 06:46:21 -0800547 aligned_x = DL_ALIGN_DOWN(x, sizeof(unsigned long));
548 width = DL_ALIGN_UP(width + (x-aligned_x), sizeof(unsigned long));
549 x = aligned_x;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700550
Bernie Thompson530f43a2010-02-15 06:46:21 -0800551 if ((width <= 0) ||
552 (x + width > dev->info->var.xres) ||
553 (y + height > dev->info->var.yres))
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700554 return -EINVAL;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700555
Bernie Thompson530f43a2010-02-15 06:46:21 -0800556 if (!atomic_read(&dev->usb_active))
557 return 0;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700558
Bernie Thompson530f43a2010-02-15 06:46:21 -0800559 urb = dlfb_get_urb(dev);
560 if (!urb)
561 return 0;
562 cmd = urb->transfer_buffer;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700563
Bernie Thompson530f43a2010-02-15 06:46:21 -0800564 for (i = y; i < y + height ; i++) {
565 const int line_offset = dev->info->fix.line_length * i;
566 const int byte_offset = line_offset + (x * BPP);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700567
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700568 if (dlfb_render_hline(dev, &urb,
569 (char *) dev->info->fix.smem_start,
Bernie Thompson2685cff2010-09-05 18:29:56 -0700570 &cmd, byte_offset, width * BPP,
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700571 &bytes_identical, &bytes_sent))
572 goto error;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700573 }
574
Bernie Thompson530f43a2010-02-15 06:46:21 -0800575 if (cmd > (char *) urb->transfer_buffer) {
576 /* Send partial buffer remaining before exiting */
577 int len = cmd - (char *) urb->transfer_buffer;
578 ret = dlfb_submit_urb(dev, urb, len);
579 bytes_sent += len;
580 } else
581 dlfb_urb_completion(urb);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700582
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700583error:
Bernie Thompson530f43a2010-02-15 06:46:21 -0800584 atomic_add(bytes_sent, &dev->bytes_sent);
585 atomic_add(bytes_identical, &dev->bytes_identical);
586 atomic_add(width*height*2, &dev->bytes_rendered);
587 end_cycles = get_cycles();
588 atomic_add(((unsigned int) ((end_cycles - start_cycles)
589 >> 10)), /* Kcycles */
590 &dev->cpu_kcycles_used);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700591
Bernie Thompson530f43a2010-02-15 06:46:21 -0800592 return 0;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700593}
594
Bernie Thompsond46ecb92010-09-05 16:35:04 -0700595/*
596 * Path triggered by usermode clients who write to filesystem
597 * e.g. cat filename > /dev/fb1
598 * Not used by X Windows or text-mode console. But useful for testing.
599 * Slow because of extra copy and we must assume all pixels dirty.
600 */
601static ssize_t dlfb_ops_write(struct fb_info *info, const char __user *buf,
602 size_t count, loff_t *ppos)
603{
Paul Mundt1a3e5282011-01-06 17:29:24 +0900604 ssize_t result;
Bernie Thompsond46ecb92010-09-05 16:35:04 -0700605 struct dlfb_data *dev = info->par;
606 u32 offset = (u32) *ppos;
607
Bernie Thompsond46ecb92010-09-05 16:35:04 -0700608 result = fb_sys_write(info, buf, count, ppos);
609
610 if (result > 0) {
611 int start = max((int)(offset / info->fix.line_length) - 1, 0);
612 int lines = min((u32)((result / info->fix.line_length) + 1),
613 (u32)info->var.yres);
614
615 dlfb_handle_damage(dev, 0, start, info->var.xres,
616 lines, info->screen_base);
617 }
Bernie Thompsond46ecb92010-09-05 16:35:04 -0700618
619 return result;
620}
621
Bernie Thompson530f43a2010-02-15 06:46:21 -0800622/* hardware has native COPY command (see libdlo), but not worth it for fbcon */
Bernie Thompson45742032010-02-15 06:46:04 -0800623static void dlfb_ops_copyarea(struct fb_info *info,
Bernie Thompson530f43a2010-02-15 06:46:21 -0800624 const struct fb_copyarea *area)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700625{
Bernie Thompson530f43a2010-02-15 06:46:21 -0800626
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700627 struct dlfb_data *dev = info->par;
628
Bernie Thompson530f43a2010-02-15 06:46:21 -0800629 sys_copyarea(info, area);
630
631 dlfb_handle_damage(dev, area->dx, area->dy,
632 area->width, area->height, info->screen_base);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700633}
634
Bernie Thompson45742032010-02-15 06:46:04 -0800635static void dlfb_ops_imageblit(struct fb_info *info,
Bernie Thompson530f43a2010-02-15 06:46:21 -0800636 const struct fb_image *image)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700637{
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700638 struct dlfb_data *dev = info->par;
Bernie Thompson530f43a2010-02-15 06:46:21 -0800639
Bernie Thompson530f43a2010-02-15 06:46:21 -0800640 sys_imageblit(info, image);
641
642 dlfb_handle_damage(dev, image->dx, image->dy,
643 image->width, image->height, info->screen_base);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700644}
645
Bernie Thompson45742032010-02-15 06:46:04 -0800646static void dlfb_ops_fillrect(struct fb_info *info,
Bernie Thompson530f43a2010-02-15 06:46:21 -0800647 const struct fb_fillrect *rect)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700648{
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700649 struct dlfb_data *dev = info->par;
650
Bernie Thompson530f43a2010-02-15 06:46:21 -0800651 sys_fillrect(info, rect);
652
653 dlfb_handle_damage(dev, rect->dx, rect->dy, rect->width,
654 rect->height, info->screen_base);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700655}
656
Bernie Thompsond5ed5432010-09-05 16:35:39 -0700657/*
658 * NOTE: fb_defio.c is holding info->fbdefio.mutex
659 * Touching ANY framebuffer memory that triggers a page fault
660 * in fb_defio will cause a deadlock, when it also tries to
661 * grab the same mutex.
662 */
663static void dlfb_dpy_deferred_io(struct fb_info *info,
664 struct list_head *pagelist)
665{
666 struct page *cur;
667 struct fb_deferred_io *fbdefio = info->fbdefio;
668 struct dlfb_data *dev = info->par;
669 struct urb *urb;
670 char *cmd;
671 cycles_t start_cycles, end_cycles;
672 int bytes_sent = 0;
673 int bytes_identical = 0;
674 int bytes_rendered = 0;
675
676 if (!fb_defio)
677 return;
678
679 if (!atomic_read(&dev->usb_active))
680 return;
681
682 start_cycles = get_cycles();
683
684 urb = dlfb_get_urb(dev);
685 if (!urb)
686 return;
687
688 cmd = urb->transfer_buffer;
689
690 /* walk the written page list and render each to device */
691 list_for_each_entry(cur, &fbdefio->pagelist, lru) {
692
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700693 if (dlfb_render_hline(dev, &urb, (char *) info->fix.smem_start,
Bernie Thompsond5ed5432010-09-05 16:35:39 -0700694 &cmd, cur->index << PAGE_SHIFT,
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700695 PAGE_SIZE, &bytes_identical, &bytes_sent))
696 goto error;
Bernie Thompsond5ed5432010-09-05 16:35:39 -0700697 bytes_rendered += PAGE_SIZE;
698 }
699
700 if (cmd > (char *) urb->transfer_buffer) {
701 /* Send partial buffer remaining before exiting */
702 int len = cmd - (char *) urb->transfer_buffer;
703 dlfb_submit_urb(dev, urb, len);
704 bytes_sent += len;
705 } else
706 dlfb_urb_completion(urb);
707
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700708error:
Bernie Thompsond5ed5432010-09-05 16:35:39 -0700709 atomic_add(bytes_sent, &dev->bytes_sent);
710 atomic_add(bytes_identical, &dev->bytes_identical);
711 atomic_add(bytes_rendered, &dev->bytes_rendered);
712 end_cycles = get_cycles();
713 atomic_add(((unsigned int) ((end_cycles - start_cycles)
714 >> 10)), /* Kcycles */
715 &dev->cpu_kcycles_used);
716}
717
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700718static int dlfb_get_edid(struct dlfb_data *dev, char *edid, int len)
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800719{
720 int i;
721 int ret;
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700722 char *rbuf;
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800723
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700724 rbuf = kmalloc(2, GFP_KERNEL);
725 if (!rbuf)
726 return 0;
727
728 for (i = 0; i < len; i++) {
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800729 ret = usb_control_msg(dev->udev,
730 usb_rcvctrlpipe(dev->udev, 0), (0x02),
731 (0x80 | (0x02 << 5)), i << 8, 0xA1, rbuf, 2,
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700732 HZ);
733 if (ret < 1) {
734 dl_err("Read EDID byte %d failed err %x\n", i, ret);
735 i--;
736 break;
737 }
738 edid[i] = rbuf[1];
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800739 }
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700740
741 kfree(rbuf);
742
743 return i;
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800744}
745
Bernie Thompson45742032010-02-15 06:46:04 -0800746static int dlfb_ops_ioctl(struct fb_info *info, unsigned int cmd,
Bernie Thompson530f43a2010-02-15 06:46:21 -0800747 unsigned long arg)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700748{
Bernie Thompson530f43a2010-02-15 06:46:21 -0800749
750 struct dlfb_data *dev = info->par;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700751 struct dloarea *area = NULL;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700752
Bernie Thompson530f43a2010-02-15 06:46:21 -0800753 if (!atomic_read(&dev->usb_active))
754 return 0;
755
756 /* TODO: Update X server to get this from sysfs instead */
757 if (cmd == DLFB_IOCTL_RETURN_EDID) {
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700758 char *edid = (char *)arg;
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700759 if (copy_to_user(edid, dev->edid, dev->edid_size))
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700760 return -EFAULT;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700761 return 0;
762 }
763
Bernie Thompson530f43a2010-02-15 06:46:21 -0800764 /* TODO: Help propose a standard fb.h ioctl to report mmap damage */
765 if (cmd == DLFB_IOCTL_REPORT_DAMAGE) {
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700766
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700767 /*
768 * If we have a damage-aware client, turn fb_defio "off"
769 * To avoid perf imact of unecessary page fault handling.
770 * Done by resetting the delay for this fb_info to a very
771 * long period. Pages will become writable and stay that way.
772 * Reset to normal value when all clients have closed this fb.
773 */
774 if (info->fbdefio)
775 info->fbdefio->delay = DL_DEFIO_WRITE_DISABLE;
776
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700777 area = (struct dloarea *)arg;
778
779 if (area->x < 0)
780 area->x = 0;
781
782 if (area->x > info->var.xres)
783 area->x = info->var.xres;
784
785 if (area->y < 0)
786 area->y = 0;
787
788 if (area->y > info->var.yres)
789 area->y = info->var.yres;
790
Bernie Thompson530f43a2010-02-15 06:46:21 -0800791 dlfb_handle_damage(dev, area->x, area->y, area->w, area->h,
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700792 info->screen_base);
793 }
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700794
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700795 return 0;
796}
797
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700798/* taken from vesafb */
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700799static int
Bernie Thompson45742032010-02-15 06:46:04 -0800800dlfb_ops_setcolreg(unsigned regno, unsigned red, unsigned green,
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700801 unsigned blue, unsigned transp, struct fb_info *info)
802{
803 int err = 0;
804
805 if (regno >= info->cmap.len)
806 return 1;
807
808 if (regno < 16) {
809 if (info->var.red.offset == 10) {
810 /* 1:5:5:5 */
811 ((u32 *) (info->pseudo_palette))[regno] =
812 ((red & 0xf800) >> 1) |
813 ((green & 0xf800) >> 6) | ((blue & 0xf800) >> 11);
814 } else {
815 /* 0:5:6:5 */
816 ((u32 *) (info->pseudo_palette))[regno] =
817 ((red & 0xf800)) |
818 ((green & 0xfc00) >> 5) | ((blue & 0xf800) >> 11);
819 }
820 }
821
822 return err;
823}
824
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800825/*
826 * It's common for several clients to have framebuffer open simultaneously.
827 * e.g. both fbcon and X. Makes things interesting.
Bernie Thompson33077b82010-09-05 16:35:19 -0700828 * Assumes caller is holding info->lock (for open and release at least)
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800829 */
830static int dlfb_ops_open(struct fb_info *info, int user)
831{
832 struct dlfb_data *dev = info->par;
833
Bernie Thompsond5ed5432010-09-05 16:35:39 -0700834 /*
835 * fbcon aggressively connects to first framebuffer it finds,
836 * preventing other clients (X) from working properly. Usually
837 * not what the user wants. Fail by default with option to enable.
838 */
839 if ((user == 0) & (!console))
840 return -EBUSY;
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800841
Bernie Thompson33077b82010-09-05 16:35:19 -0700842 /* If the USB device is gone, we don't accept new opens */
843 if (dev->virtualized)
844 return -ENODEV;
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800845
846 dev->fb_count++;
847
Bernie Thompson33077b82010-09-05 16:35:19 -0700848 kref_get(&dev->kref);
849
Bernie Thompsond5ed5432010-09-05 16:35:39 -0700850 if (fb_defio && (info->fbdefio == NULL)) {
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700851 /* enable defio at last moment if not disabled by client */
852
853 struct fb_deferred_io *fbdefio;
854
Joe Perches31a9f472010-10-31 15:33:55 -0700855 fbdefio = kmalloc(sizeof(struct fb_deferred_io), GFP_KERNEL);
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700856
857 if (fbdefio) {
858 fbdefio->delay = DL_DEFIO_WRITE_DELAY;
859 fbdefio->deferred_io = dlfb_dpy_deferred_io;
860 }
861
862 info->fbdefio = fbdefio;
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800863 fb_deferred_io_init(info);
864 }
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800865
866 dl_notice("open /dev/fb%d user=%d fb_info=%p count=%d\n",
867 info->node, user, info, dev->fb_count);
868
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700869 return 0;
870}
871
Bernie Thompson4a4854d2010-02-15 06:45:55 -0800872/*
873 * Called when all client interfaces to start transactions have been disabled,
874 * and all references to our device instance (dlfb_data) are released.
875 * Every transaction must have a reference, so we know are fully spun down
876 */
Bernie Thompson33077b82010-09-05 16:35:19 -0700877static void dlfb_free(struct kref *kref)
Bernie Thompson4a4854d2010-02-15 06:45:55 -0800878{
879 struct dlfb_data *dev = container_of(kref, struct dlfb_data, kref);
880
Bernie Thompson33077b82010-09-05 16:35:19 -0700881 /* this function will wait for all in-flight urbs to complete */
882 if (dev->urbs.count > 0)
883 dlfb_free_urb_list(dev);
884
Bernie Thompson4a4854d2010-02-15 06:45:55 -0800885 if (dev->backing_buffer)
886 vfree(dev->backing_buffer);
887
Bernie Thompson33077b82010-09-05 16:35:19 -0700888 kfree(dev->edid);
889
890 dl_warn("freeing dlfb_data %p\n", dev);
Bernie Thompson3e8f3d62010-02-15 06:46:26 -0800891
Bernie Thompson4a4854d2010-02-15 06:45:55 -0800892 kfree(dev);
893}
894
Bernie Thompson5bea1fb2010-09-05 18:28:29 -0700895static void dlfb_release_urb_work(struct work_struct *work)
896{
897 struct urb_node *unode = container_of(work, struct urb_node,
898 release_urb_work.work);
899
900 up(&unode->dev->urbs.limit_sem);
901}
Bernie Thompson33077b82010-09-05 16:35:19 -0700902
903static void dlfb_free_framebuffer_work(struct work_struct *work)
Bernie Thompson2469d5d2010-02-15 06:46:13 -0800904{
Bernie Thompson33077b82010-09-05 16:35:19 -0700905 struct dlfb_data *dev = container_of(work, struct dlfb_data,
906 free_framebuffer_work.work);
907 struct fb_info *info = dev->info;
908 int node = info->node;
909
910 unregister_framebuffer(info);
Bernie Thompson2469d5d2010-02-15 06:46:13 -0800911
912 if (info->cmap.len != 0)
913 fb_dealloc_cmap(&info->cmap);
914 if (info->monspecs.modedb)
915 fb_destroy_modedb(info->monspecs.modedb);
916 if (info->screen_base)
917 vfree(info->screen_base);
918
919 fb_destroy_modelist(&info->modelist);
920
Bernie Thompson33077b82010-09-05 16:35:19 -0700921 dev->info = 0;
922
923 /* Assume info structure is freed after this point */
Bernie Thompson2469d5d2010-02-15 06:46:13 -0800924 framebuffer_release(info);
925
Bernie Thompson33077b82010-09-05 16:35:19 -0700926 dl_warn("fb_info for /dev/fb%d has been freed\n", node);
927
928 /* ref taken in probe() as part of registering framebfufer */
929 kref_put(&dev->kref, dlfb_free);
930}
931
932/*
933 * Assumes caller is holding info->lock mutex (for open and release at least)
934 */
935static int dlfb_ops_release(struct fb_info *info, int user)
936{
937 struct dlfb_data *dev = info->par;
938
939 dev->fb_count--;
940
941 /* We can't free fb_info here - fbmem will touch it when we return */
942 if (dev->virtualized && (dev->fb_count == 0))
943 schedule_delayed_work(&dev->free_framebuffer_work, HZ);
944
Bernie Thompson33077b82010-09-05 16:35:19 -0700945 if ((dev->fb_count == 0) && (info->fbdefio)) {
946 fb_deferred_io_cleanup(info);
947 kfree(info->fbdefio);
948 info->fbdefio = NULL;
949 info->fbops->fb_mmap = dlfb_ops_mmap;
950 }
Bernie Thompson33077b82010-09-05 16:35:19 -0700951
952 dl_warn("released /dev/fb%d user=%d count=%d\n",
953 info->node, user, dev->fb_count);
954
955 kref_put(&dev->kref, dlfb_free);
956
957 return 0;
Bernie Thompson2469d5d2010-02-15 06:46:13 -0800958}
959
960/*
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800961 * Check whether a video mode is supported by the DisplayLink chip
962 * We start from monitor's modes, so don't need to filter that here
963 */
964static int dlfb_is_valid_mode(struct fb_videomode *mode,
965 struct fb_info *info)
966{
967 struct dlfb_data *dev = info->par;
968
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700969 if (mode->xres * mode->yres > dev->sku_pixel_limit) {
970 dl_warn("%dx%d beyond chip capabilities\n",
971 mode->xres, mode->yres);
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800972 return 0;
Bernie Thompson18dffdf2010-09-05 16:35:23 -0700973 }
974
975 dl_info("%dx%d valid mode\n", mode->xres, mode->yres);
Bernie Thompson7d9485e2010-02-15 06:46:08 -0800976
977 return 1;
978}
979
980static void dlfb_var_color_format(struct fb_var_screeninfo *var)
981{
982 const struct fb_bitfield red = { 11, 5, 0 };
983 const struct fb_bitfield green = { 5, 6, 0 };
984 const struct fb_bitfield blue = { 0, 5, 0 };
985
986 var->bits_per_pixel = 16;
987 var->red = red;
988 var->green = green;
989 var->blue = blue;
990}
991
Bernie Thompson2469d5d2010-02-15 06:46:13 -0800992static int dlfb_ops_check_var(struct fb_var_screeninfo *var,
993 struct fb_info *info)
994{
995 struct fb_videomode mode;
996
997 /* TODO: support dynamically changing framebuffer size */
998 if ((var->xres * var->yres * 2) > info->fix.smem_len)
999 return -EINVAL;
1000
1001 /* set device-specific elements of var unrelated to mode */
1002 dlfb_var_color_format(var);
1003
1004 fb_var_to_videomode(&mode, var);
1005
1006 if (!dlfb_is_valid_mode(&mode, info))
1007 return -EINVAL;
1008
1009 return 0;
1010}
1011
1012static int dlfb_ops_set_par(struct fb_info *info)
1013{
1014 struct dlfb_data *dev = info->par;
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001015 int result;
1016 u16 *pix_framebuffer;
1017 int i;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001018
1019 dl_notice("set_par mode %dx%d\n", info->var.xres, info->var.yres);
1020
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001021 result = dlfb_set_video_mode(dev, &info->var);
1022
1023 if ((result == 0) && (dev->fb_count == 0)) {
1024
1025 /* paint greenscreen */
1026
1027 pix_framebuffer = (u16 *) info->screen_base;
1028 for (i = 0; i < info->fix.smem_len / 2; i++)
1029 pix_framebuffer[i] = 0x37e6;
1030
1031 dlfb_handle_damage(dev, 0, 0, info->var.xres, info->var.yres,
1032 info->screen_base);
1033 }
1034
1035 return result;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001036}
1037
Bernie Thompson9825f702010-09-05 16:35:10 -07001038/*
1039 * In order to come back from full DPMS off, we need to set the mode again
1040 */
Bernie Thompson45742032010-02-15 06:46:04 -08001041static int dlfb_ops_blank(int blank_mode, struct fb_info *info)
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -07001042{
Bernie Thompson530f43a2010-02-15 06:46:21 -08001043 struct dlfb_data *dev = info->par;
Roberto De Ioris7316bc52009-06-10 23:02:19 -07001044
Roberto De Ioris7316bc52009-06-10 23:02:19 -07001045 if (blank_mode != FB_BLANK_UNBLANK) {
Bernie Thompson9825f702010-09-05 16:35:10 -07001046 char *bufptr;
1047 struct urb *urb;
Roberto De Ioris7316bc52009-06-10 23:02:19 -07001048
Bernie Thompson9825f702010-09-05 16:35:10 -07001049 urb = dlfb_get_urb(dev);
1050 if (!urb)
1051 return 0;
1052
1053 bufptr = (char *) urb->transfer_buffer;
1054 bufptr = dlfb_vidreg_lock(bufptr);
1055 bufptr = dlfb_enable_hvsync(bufptr, false);
1056 bufptr = dlfb_vidreg_unlock(bufptr);
1057
1058 dlfb_submit_urb(dev, urb, bufptr -
1059 (char *) urb->transfer_buffer);
1060 } else {
1061 dlfb_set_video_mode(dev, &info->var);
1062 }
Roberto De Ioris7316bc52009-06-10 23:02:19 -07001063
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001064 return 0;
1065}
1066
1067static struct fb_ops dlfb_ops = {
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001068 .owner = THIS_MODULE,
Paul Mundt1a3e5282011-01-06 17:29:24 +09001069 .fb_read = fb_sys_read,
Bernie Thompsond46ecb92010-09-05 16:35:04 -07001070 .fb_write = dlfb_ops_write,
Bernie Thompson45742032010-02-15 06:46:04 -08001071 .fb_setcolreg = dlfb_ops_setcolreg,
1072 .fb_fillrect = dlfb_ops_fillrect,
1073 .fb_copyarea = dlfb_ops_copyarea,
1074 .fb_imageblit = dlfb_ops_imageblit,
1075 .fb_mmap = dlfb_ops_mmap,
1076 .fb_ioctl = dlfb_ops_ioctl,
Bernie Thompson3e8f3d62010-02-15 06:46:26 -08001077 .fb_open = dlfb_ops_open,
Bernie Thompson45742032010-02-15 06:46:04 -08001078 .fb_release = dlfb_ops_release,
1079 .fb_blank = dlfb_ops_blank,
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001080 .fb_check_var = dlfb_ops_check_var,
1081 .fb_set_par = dlfb_ops_set_par,
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001082};
1083
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001084
Bernie Thompsoncc403dc2010-02-15 06:45:49 -08001085/*
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001086 * Assumes &info->lock held by caller
1087 * Assumes no active clients have framebuffer open
1088 */
1089static int dlfb_realloc_framebuffer(struct dlfb_data *dev, struct fb_info *info)
1090{
1091 int retval = -ENOMEM;
1092 int old_len = info->fix.smem_len;
1093 int new_len;
1094 unsigned char *old_fb = info->screen_base;
1095 unsigned char *new_fb;
1096 unsigned char *new_back;
1097
1098 dl_warn("Reallocating framebuffer. Addresses will change!\n");
1099
1100 new_len = info->fix.line_length * info->var.yres;
1101
1102 if (PAGE_ALIGN(new_len) > old_len) {
1103 /*
1104 * Alloc system memory for virtual framebuffer
1105 */
1106 new_fb = vmalloc(new_len);
1107 if (!new_fb) {
1108 dl_err("Virtual framebuffer alloc failed\n");
1109 goto error;
1110 }
1111
1112 if (info->screen_base) {
1113 memcpy(new_fb, old_fb, old_len);
1114 vfree(info->screen_base);
1115 }
1116
1117 info->screen_base = new_fb;
1118 info->fix.smem_len = PAGE_ALIGN(new_len);
1119 info->fix.smem_start = (unsigned long) new_fb;
1120 info->flags = udlfb_info_flags;
1121
1122 /*
1123 * Second framebuffer copy to mirror the framebuffer state
1124 * on the physical USB device. We can function without this.
1125 * But with imperfect damage info we may send pixels over USB
1126 * that were, in fact, unchanged - wasting limited USB bandwidth
1127 */
1128 new_back = vmalloc(new_len);
1129 if (!new_back)
1130 dl_info("No shadow/backing buffer allcoated\n");
1131 else {
1132 if (dev->backing_buffer)
1133 vfree(dev->backing_buffer);
1134 dev->backing_buffer = new_back;
1135 memset(dev->backing_buffer, 0, new_len);
1136 }
1137 }
1138
1139 retval = 0;
1140
1141error:
1142 return retval;
1143}
1144
1145/*
1146 * 1) Get EDID from hw, or use sw default
1147 * 2) Parse into various fb_info structs
1148 * 3) Allocate virtual framebuffer memory to back highest res mode
1149 *
1150 * Parses EDID into three places used by various parts of fbdev:
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001151 * fb_var_screeninfo contains the timing of the monitor's preferred mode
1152 * fb_info.monspecs is full parsed EDID info, including monspecs.modedb
1153 * fb_info.modelist is a linked list of all monitor & VESA modes which work
1154 *
1155 * If EDID is not readable/valid, then modelist is all VESA modes,
1156 * monspecs is NULL, and fb_var_screeninfo is set to safe VESA mode
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001157 * Returns 0 if successful
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001158 */
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001159static int dlfb_setup_modes(struct dlfb_data *dev,
1160 struct fb_info *info,
1161 char *default_edid, size_t default_edid_size)
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001162{
1163 int i;
1164 const struct fb_videomode *default_vmode = NULL;
1165 int result = 0;
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001166 char *edid;
1167 int tries = 3;
1168
1169 if (info->dev) /* only use mutex if info has been registered */
1170 mutex_lock(&info->lock);
1171
1172 edid = kmalloc(MAX_EDID_SIZE, GFP_KERNEL);
1173 if (!edid) {
1174 result = -ENOMEM;
1175 goto error;
1176 }
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001177
1178 fb_destroy_modelist(&info->modelist);
1179 memset(&info->monspecs, 0, sizeof(info->monspecs));
1180
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001181 /*
1182 * Try to (re)read EDID from hardware first
1183 * EDID data may return, but not parse as valid
1184 * Try again a few times, in case of e.g. analog cable noise
1185 */
1186 while (tries--) {
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001187
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001188 i = dlfb_get_edid(dev, edid, MAX_EDID_SIZE);
1189
1190 if (i >= MIN_EDID_SIZE)
1191 fb_edid_to_monspecs(edid, &info->monspecs);
1192
1193 if (info->monspecs.modedb_len > 0) {
1194 dev->edid = edid;
1195 dev->edid_size = i;
1196 break;
1197 }
1198 }
1199
1200 /* If that fails, use a previously returned EDID if available */
1201 if (info->monspecs.modedb_len == 0) {
1202
1203 dl_err("Unable to get valid EDID from device/display\n");
1204
1205 if (dev->edid) {
1206 fb_edid_to_monspecs(dev->edid, &info->monspecs);
1207 if (info->monspecs.modedb_len > 0)
1208 dl_err("Using previously queried EDID\n");
1209 }
1210 }
1211
1212 /* If that fails, use the default EDID we were handed */
1213 if (info->monspecs.modedb_len == 0) {
1214 if (default_edid_size >= MIN_EDID_SIZE) {
1215 fb_edid_to_monspecs(default_edid, &info->monspecs);
1216 if (info->monspecs.modedb_len > 0) {
1217 memcpy(edid, default_edid, default_edid_size);
1218 dev->edid = edid;
1219 dev->edid_size = default_edid_size;
1220 dl_err("Using default/backup EDID\n");
1221 }
1222 }
1223 }
1224
1225 /* If we've got modes, let's pick a best default mode */
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001226 if (info->monspecs.modedb_len > 0) {
1227
1228 for (i = 0; i < info->monspecs.modedb_len; i++) {
1229 if (dlfb_is_valid_mode(&info->monspecs.modedb[i], info))
1230 fb_add_videomode(&info->monspecs.modedb[i],
1231 &info->modelist);
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001232 else /* if we've removed top/best mode */
1233 info->monspecs.misc &= ~FB_MISC_1ST_DETAIL;
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001234 }
1235
1236 default_vmode = fb_find_best_display(&info->monspecs,
1237 &info->modelist);
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001238 }
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001239
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001240 /* If everything else has failed, fall back to safe default mode */
1241 if (default_vmode == NULL) {
1242
1243 struct fb_videomode fb_vmode = {0};
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001244
1245 /*
1246 * Add the standard VESA modes to our modelist
1247 * Since we don't have EDID, there may be modes that
1248 * overspec monitor and/or are incorrect aspect ratio, etc.
1249 * But at least the user has a chance to choose
1250 */
1251 for (i = 0; i < VESA_MODEDB_SIZE; i++) {
1252 if (dlfb_is_valid_mode((struct fb_videomode *)
1253 &vesa_modes[i], info))
1254 fb_add_videomode(&vesa_modes[i],
1255 &info->modelist);
1256 }
1257
1258 /*
1259 * default to resolution safe for projectors
1260 * (since they are most common case without EDID)
1261 */
1262 fb_vmode.xres = 800;
1263 fb_vmode.yres = 600;
1264 fb_vmode.refresh = 60;
1265 default_vmode = fb_find_nearest_mode(&fb_vmode,
1266 &info->modelist);
1267 }
1268
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001269 /* If we have good mode and no active clients*/
1270 if ((default_vmode != NULL) && (dev->fb_count == 0)) {
1271
1272 fb_videomode_to_var(&info->var, default_vmode);
1273 dlfb_var_color_format(&info->var);
1274
1275 /*
1276 * with mode size info, we can now alloc our framebuffer.
1277 */
1278 memcpy(&info->fix, &dlfb_fix, sizeof(dlfb_fix));
1279 info->fix.line_length = info->var.xres *
1280 (info->var.bits_per_pixel / 8);
1281
1282 result = dlfb_realloc_framebuffer(dev, info);
1283
1284 } else
1285 result = -EINVAL;
1286
1287error:
1288 if (edid && (dev->edid != edid))
1289 kfree(edid);
1290
1291 if (info->dev)
1292 mutex_unlock(&info->lock);
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001293
1294 return result;
1295}
1296
1297static ssize_t metrics_bytes_rendered_show(struct device *fbdev,
1298 struct device_attribute *a, char *buf) {
1299 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1300 struct dlfb_data *dev = fb_info->par;
1301 return snprintf(buf, PAGE_SIZE, "%u\n",
1302 atomic_read(&dev->bytes_rendered));
1303}
1304
1305static ssize_t metrics_bytes_identical_show(struct device *fbdev,
1306 struct device_attribute *a, char *buf) {
1307 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1308 struct dlfb_data *dev = fb_info->par;
1309 return snprintf(buf, PAGE_SIZE, "%u\n",
1310 atomic_read(&dev->bytes_identical));
1311}
1312
1313static ssize_t metrics_bytes_sent_show(struct device *fbdev,
1314 struct device_attribute *a, char *buf) {
1315 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1316 struct dlfb_data *dev = fb_info->par;
1317 return snprintf(buf, PAGE_SIZE, "%u\n",
1318 atomic_read(&dev->bytes_sent));
1319}
1320
1321static ssize_t metrics_cpu_kcycles_used_show(struct device *fbdev,
1322 struct device_attribute *a, char *buf) {
1323 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1324 struct dlfb_data *dev = fb_info->par;
1325 return snprintf(buf, PAGE_SIZE, "%u\n",
1326 atomic_read(&dev->cpu_kcycles_used));
1327}
1328
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001329static ssize_t edid_show(
1330 struct file *filp,
1331 struct kobject *kobj, struct bin_attribute *a,
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001332 char *buf, loff_t off, size_t count) {
1333 struct device *fbdev = container_of(kobj, struct device, kobj);
1334 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1335 struct dlfb_data *dev = fb_info->par;
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001336
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001337 if (dev->edid == NULL)
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001338 return 0;
1339
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001340 if ((off >= dev->edid_size) || (count > dev->edid_size))
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001341 return 0;
1342
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001343 if (off + count > dev->edid_size)
1344 count = dev->edid_size - off;
1345
1346 dl_info("sysfs edid copy %p to %p, %d bytes\n",
1347 dev->edid, buf, (int) count);
1348
1349 memcpy(buf, dev->edid, count);
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001350
1351 return count;
1352}
1353
Bernie Thompson8ef8cc42010-09-05 16:35:31 -07001354static ssize_t edid_store(
1355 struct file *filp,
1356 struct kobject *kobj, struct bin_attribute *a,
1357 char *src, loff_t src_off, size_t src_size) {
1358 struct device *fbdev = container_of(kobj, struct device, kobj);
1359 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1360 struct dlfb_data *dev = fb_info->par;
1361
1362 /* We only support write of entire EDID at once, no offset*/
1363 if ((src_size < MIN_EDID_SIZE) ||
1364 (src_size > MAX_EDID_SIZE) ||
1365 (src_off != 0))
1366 return 0;
1367
1368 dlfb_setup_modes(dev, fb_info, src, src_size);
1369
1370 if (dev->edid && (memcmp(src, dev->edid, src_size) == 0)) {
1371 dl_info("sysfs written EDID is new default\n");
1372 dlfb_ops_set_par(fb_info);
1373 return src_size;
1374 } else
1375 return 0;
1376}
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001377
1378static ssize_t metrics_reset_store(struct device *fbdev,
1379 struct device_attribute *attr,
1380 const char *buf, size_t count)
1381{
1382 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1383 struct dlfb_data *dev = fb_info->par;
1384
1385 atomic_set(&dev->bytes_rendered, 0);
1386 atomic_set(&dev->bytes_identical, 0);
1387 atomic_set(&dev->bytes_sent, 0);
1388 atomic_set(&dev->cpu_kcycles_used, 0);
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001389
1390 return count;
1391}
1392
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001393static struct bin_attribute edid_attr = {
1394 .attr.name = "edid",
Bernie Thompson8ef8cc42010-09-05 16:35:31 -07001395 .attr.mode = 0666,
1396 .size = MAX_EDID_SIZE,
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001397 .read = edid_show,
Bernie Thompson8ef8cc42010-09-05 16:35:31 -07001398 .write = edid_store
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001399};
1400
1401static struct device_attribute fb_device_attrs[] = {
1402 __ATTR_RO(metrics_bytes_rendered),
1403 __ATTR_RO(metrics_bytes_identical),
1404 __ATTR_RO(metrics_bytes_sent),
1405 __ATTR_RO(metrics_cpu_kcycles_used),
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001406 __ATTR(metrics_reset, S_IWUGO, NULL, metrics_reset_store),
Bernie Thompson7d9485e2010-02-15 06:46:08 -08001407};
1408
1409/*
Bernie Thompsoncc403dc2010-02-15 06:45:49 -08001410 * This is necessary before we can communicate with the display controller.
1411 */
1412static int dlfb_select_std_channel(struct dlfb_data *dev)
1413{
1414 int ret;
1415 u8 set_def_chn[] = { 0x57, 0xCD, 0xDC, 0xA7,
1416 0x1C, 0x88, 0x5E, 0x15,
1417 0x60, 0xFE, 0xC6, 0x97,
1418 0x16, 0x3D, 0x47, 0xF2 };
1419
1420 ret = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
1421 NR_USB_REQUEST_CHANNEL,
1422 (USB_DIR_OUT | USB_TYPE_VENDOR), 0, 0,
1423 set_def_chn, sizeof(set_def_chn), USB_CTRL_SET_TIMEOUT);
1424 return ret;
1425}
1426
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001427static int dlfb_parse_vendor_descriptor(struct dlfb_data *dev,
1428 struct usb_device *usbdev)
1429{
1430 char *desc;
1431 char *buf;
1432 char *desc_end;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001433
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001434 u8 total_len = 0;
1435
1436 buf = kzalloc(MAX_VENDOR_DESCRIPTOR_SIZE, GFP_KERNEL);
1437 if (!buf)
1438 return false;
1439 desc = buf;
1440
1441 total_len = usb_get_descriptor(usbdev, 0x5f, /* vendor specific */
1442 0, desc, MAX_VENDOR_DESCRIPTOR_SIZE);
1443 if (total_len > 5) {
1444 dl_info("vendor descriptor length:%x data:%02x %02x %02x %02x" \
1445 "%02x %02x %02x %02x %02x %02x %02x\n",
1446 total_len, desc[0],
1447 desc[1], desc[2], desc[3], desc[4], desc[5], desc[6],
1448 desc[7], desc[8], desc[9], desc[10]);
1449
1450 if ((desc[0] != total_len) || /* descriptor length */
1451 (desc[1] != 0x5f) || /* vendor descriptor type */
1452 (desc[2] != 0x01) || /* version (2 bytes) */
1453 (desc[3] != 0x00) ||
1454 (desc[4] != total_len - 2)) /* length after type */
1455 goto unrecognized;
1456
1457 desc_end = desc + total_len;
1458 desc += 5; /* the fixed header we've already parsed */
1459
1460 while (desc < desc_end) {
1461 u8 length;
1462 u16 key;
1463
1464 key = *((u16 *) desc);
1465 desc += sizeof(u16);
1466 length = *desc;
1467 desc++;
1468
1469 switch (key) {
1470 case 0x0200: { /* max_area */
1471 u32 max_area;
1472 max_area = le32_to_cpu(*((u32 *)desc));
1473 dl_warn("DL chip limited to %d pixel modes\n",
1474 max_area);
1475 dev->sku_pixel_limit = max_area;
1476 break;
1477 }
1478 default:
1479 break;
1480 }
1481 desc += length;
1482 }
1483 }
1484
1485 goto success;
1486
1487unrecognized:
1488 /* allow udlfb to load for now even if firmware unrecognized */
1489 dl_err("Unrecognized vendor firmware descriptor\n");
1490
1491success:
1492 kfree(buf);
1493 return true;
1494}
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001495static int dlfb_usb_probe(struct usb_interface *interface,
Bernie Thompson59277b62009-11-24 15:52:21 -08001496 const struct usb_device_id *id)
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001497{
Bernie Thompson59277b62009-11-24 15:52:21 -08001498 struct usb_device *usbdev;
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001499 struct dlfb_data *dev = 0;
Bernie Thompson33077b82010-09-05 16:35:19 -07001500 struct fb_info *info = 0;
Bernie Thompson59277b62009-11-24 15:52:21 -08001501 int retval = -ENOMEM;
Bernie Thompson2685cff2010-09-05 18:29:56 -07001502 int i;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001503
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001504 /* usb initialization */
1505
1506 usbdev = interface_to_usbdev(interface);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001507
Bernie Thompson59277b62009-11-24 15:52:21 -08001508 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1509 if (dev == NULL) {
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001510 err("dlfb_usb_probe: failed alloc of dev struct\n");
1511 goto error;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001512 }
1513
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001514 /* we need to wait for both usb and fbdev to spin down on disconnect */
1515 kref_init(&dev->kref); /* matching kref_put in usb .disconnect fn */
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001516 kref_get(&dev->kref); /* matching kref_put in free_framebuffer_work */
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001517
Bernie Thompson59277b62009-11-24 15:52:21 -08001518 dev->udev = usbdev;
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001519 dev->gdev = &usbdev->dev; /* our generic struct device * */
Bernie Thompson59277b62009-11-24 15:52:21 -08001520 usb_set_intfdata(interface, dev);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001521
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001522 dl_info("%s %s - serial #%s\n",
1523 usbdev->manufacturer, usbdev->product, usbdev->serial);
1524 dl_info("vid_%04x&pid_%04x&rev_%04x driver's dlfb_data struct at %p\n",
1525 usbdev->descriptor.idVendor, usbdev->descriptor.idProduct,
1526 usbdev->descriptor.bcdDevice, dev);
Bernie Thompsond5ed5432010-09-05 16:35:39 -07001527 dl_info("console enable=%d\n", console);
1528 dl_info("fb_defio enable=%d\n", fb_defio);
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001529
1530 dev->sku_pixel_limit = 2048 * 1152; /* default to maximum */
1531
1532 if (!dlfb_parse_vendor_descriptor(dev, usbdev)) {
1533 dl_err("firmware not recognized. Assume incompatible device\n");
1534 goto error;
1535 }
1536
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001537 if (!dlfb_alloc_urb_list(dev, WRITES_IN_FLIGHT, MAX_TRANSFER)) {
1538 retval = -ENOMEM;
1539 dl_err("dlfb_alloc_urb_list failed\n");
1540 goto error;
1541 }
1542
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001543 /* We don't register a new USB class. Our client interface is fbdev */
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001544
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001545 /* allocates framebuffer driver structure, not framebuffer memory */
1546 info = framebuffer_alloc(0, &usbdev->dev);
1547 if (!info) {
1548 retval = -ENOMEM;
1549 dl_err("framebuffer_alloc failed\n");
1550 goto error;
1551 }
Bernie Thompson33077b82010-09-05 16:35:19 -07001552
Bernie Thompson59277b62009-11-24 15:52:21 -08001553 dev->info = info;
1554 info->par = dev;
1555 info->pseudo_palette = dev->pseudo_palette;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001556 info->fbops = &dlfb_ops;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001557
Bernie Thompson59277b62009-11-24 15:52:21 -08001558 retval = fb_alloc_cmap(&info->cmap, 256, 0);
1559 if (retval < 0) {
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001560 dl_err("fb_alloc_cmap failed %x\n", retval);
1561 goto error;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001562 }
1563
Bernie Thompson33077b82010-09-05 16:35:19 -07001564 INIT_DELAYED_WORK(&dev->free_framebuffer_work,
1565 dlfb_free_framebuffer_work);
1566
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001567 INIT_LIST_HEAD(&info->modelist);
1568
1569 retval = dlfb_setup_modes(dev, info, NULL, 0);
1570 if (retval != 0) {
1571 dl_err("unable to find common mode for display and adapter\n");
1572 goto error;
1573 }
1574
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001575 /* ready to begin using device */
1576
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001577 atomic_set(&dev->usb_active, 1);
Bernie Thompson59277b62009-11-24 15:52:21 -08001578 dlfb_select_std_channel(dev);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001579
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001580 dlfb_ops_check_var(&info->var, info);
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001581 dlfb_ops_set_par(info);
1582
Bernie Thompson59277b62009-11-24 15:52:21 -08001583 retval = register_framebuffer(info);
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001584 if (retval < 0) {
1585 dl_err("register_framebuffer failed %d\n", retval);
1586 goto error;
1587 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001588
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001589 for (i = 0; i < ARRAY_SIZE(fb_device_attrs); i++)
1590 device_create_file(info->dev, &fb_device_attrs[i]);
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -07001591
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001592 device_create_bin_file(info->dev, &edid_attr);
1593
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001594 dl_info("DisplayLink USB device /dev/fb%d attached. %dx%d resolution."
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001595 " Using %dK framebuffer memory\n", info->node,
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001596 info->var.xres, info->var.yres,
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001597 ((dev->backing_buffer) ?
Bernie Thompson18dffdf2010-09-05 16:35:23 -07001598 info->fix.smem_len * 2 : info->fix.smem_len) >> 10);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001599 return 0;
1600
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001601error:
1602 if (dev) {
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001603
Bernie Thompson33077b82010-09-05 16:35:19 -07001604 if (info) {
1605 if (info->cmap.len != 0)
1606 fb_dealloc_cmap(&info->cmap);
1607 if (info->monspecs.modedb)
1608 fb_destroy_modedb(info->monspecs.modedb);
1609 if (info->screen_base)
1610 vfree(info->screen_base);
1611
1612 fb_destroy_modelist(&info->modelist);
1613
1614 framebuffer_release(info);
1615 }
1616
1617 if (dev->backing_buffer)
1618 vfree(dev->backing_buffer);
1619
1620 kref_put(&dev->kref, dlfb_free); /* ref for framebuffer */
1621 kref_put(&dev->kref, dlfb_free); /* last ref from kref_init */
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001622
1623 /* dev has been deallocated. Do not dereference */
1624 }
1625
Bernie Thompson59277b62009-11-24 15:52:21 -08001626 return retval;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001627}
1628
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001629static void dlfb_usb_disconnect(struct usb_interface *interface)
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001630{
Bernie Thompson59277b62009-11-24 15:52:21 -08001631 struct dlfb_data *dev;
1632 struct fb_info *info;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001633 int i;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001634
Bernie Thompson59277b62009-11-24 15:52:21 -08001635 dev = usb_get_intfdata(interface);
Bernie Thompson59277b62009-11-24 15:52:21 -08001636 info = dev->info;
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001637
Bernie Thompson33077b82010-09-05 16:35:19 -07001638 dl_info("USB disconnect starting\n");
1639
1640 /* we virtualize until all fb clients release. Then we free */
1641 dev->virtualized = true;
1642
1643 /* When non-active we'll update virtual framebuffer, but no new urbs */
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001644 atomic_set(&dev->usb_active, 0);
1645
Bernie Thompson33077b82010-09-05 16:35:19 -07001646 /* remove udlfb's sysfs interfaces */
1647 for (i = 0; i < ARRAY_SIZE(fb_device_attrs); i++)
1648 device_remove_file(info->dev, &fb_device_attrs[i]);
1649 device_remove_bin_file(info->dev, &edid_attr);
1650
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001651 usb_set_intfdata(interface, NULL);
1652
Bernie Thompson33077b82010-09-05 16:35:19 -07001653 /* if clients still have us open, will be freed on last close */
1654 if (dev->fb_count == 0)
1655 schedule_delayed_work(&dev->free_framebuffer_work, 0);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001656
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001657 /* release reference taken by kref_init in probe() */
Bernie Thompson33077b82010-09-05 16:35:19 -07001658 kref_put(&dev->kref, dlfb_free);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001659
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001660 /* consider dlfb_data freed */
1661
1662 return;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001663}
1664
1665static struct usb_driver dlfb_driver = {
1666 .name = "udlfb",
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001667 .probe = dlfb_usb_probe,
1668 .disconnect = dlfb_usb_disconnect,
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001669 .id_table = id_table,
1670};
1671
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001672static int __init dlfb_module_init(void)
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001673{
1674 int res;
1675
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001676 res = usb_register(&dlfb_driver);
1677 if (res)
1678 err("usb_register failed. Error number %d", res);
1679
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001680 return res;
1681}
1682
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001683static void __exit dlfb_module_exit(void)
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001684{
1685 usb_deregister(&dlfb_driver);
1686}
1687
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001688module_init(dlfb_module_init);
1689module_exit(dlfb_module_exit);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001690
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001691static void dlfb_urb_completion(struct urb *urb)
1692{
1693 struct urb_node *unode = urb->context;
1694 struct dlfb_data *dev = unode->dev;
1695 unsigned long flags;
1696
1697 /* sync/async unlink faults aren't errors */
1698 if (urb->status) {
1699 if (!(urb->status == -ENOENT ||
1700 urb->status == -ECONNRESET ||
1701 urb->status == -ESHUTDOWN)) {
1702 dl_err("%s - nonzero write bulk status received: %d\n",
1703 __func__, urb->status);
1704 atomic_set(&dev->lost_pixels, 1);
1705 }
1706 }
1707
1708 urb->transfer_buffer_length = dev->urbs.size; /* reset to actual */
1709
1710 spin_lock_irqsave(&dev->urbs.lock, flags);
1711 list_add_tail(&unode->entry, &dev->urbs.list);
1712 dev->urbs.available++;
1713 spin_unlock_irqrestore(&dev->urbs.lock, flags);
1714
Bernie Thompson5bea1fb2010-09-05 18:28:29 -07001715 /*
1716 * When using fb_defio, we deadlock if up() is called
1717 * while another is waiting. So queue to another process.
1718 */
1719 if (fb_defio)
1720 schedule_delayed_work(&unode->release_urb_work, 0);
1721 else
1722 up(&dev->urbs.limit_sem);
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001723}
1724
1725static void dlfb_free_urb_list(struct dlfb_data *dev)
1726{
1727 int count = dev->urbs.count;
1728 struct list_head *node;
1729 struct urb_node *unode;
1730 struct urb *urb;
1731 int ret;
1732 unsigned long flags;
1733
1734 dl_notice("Waiting for completes and freeing all render urbs\n");
1735
1736 /* keep waiting and freeing, until we've got 'em all */
1737 while (count--) {
Bernie Thompson33077b82010-09-05 16:35:19 -07001738
1739 /* Getting interrupted means a leak, but ok at shutdown*/
1740 ret = down_interruptible(&dev->urbs.limit_sem);
1741 if (ret)
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001742 break;
Bernie Thompson33077b82010-09-05 16:35:19 -07001743
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001744 spin_lock_irqsave(&dev->urbs.lock, flags);
1745
1746 node = dev->urbs.list.next; /* have reserved one with sem */
1747 list_del_init(node);
1748
1749 spin_unlock_irqrestore(&dev->urbs.lock, flags);
1750
1751 unode = list_entry(node, struct urb_node, entry);
1752 urb = unode->urb;
1753
1754 /* Free each separately allocated piece */
Greg Kroah-Hartmanc220cc32010-04-29 15:36:29 -07001755 usb_free_coherent(urb->dev, dev->urbs.size,
1756 urb->transfer_buffer, urb->transfer_dma);
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001757 usb_free_urb(urb);
1758 kfree(node);
1759 }
1760
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001761}
1762
1763static int dlfb_alloc_urb_list(struct dlfb_data *dev, int count, size_t size)
1764{
1765 int i = 0;
1766 struct urb *urb;
1767 struct urb_node *unode;
1768 char *buf;
1769
1770 spin_lock_init(&dev->urbs.lock);
1771
1772 dev->urbs.size = size;
1773 INIT_LIST_HEAD(&dev->urbs.list);
1774
1775 while (i < count) {
1776 unode = kzalloc(sizeof(struct urb_node), GFP_KERNEL);
1777 if (!unode)
1778 break;
1779 unode->dev = dev;
1780
Bernie Thompson5bea1fb2010-09-05 18:28:29 -07001781 INIT_DELAYED_WORK(&unode->release_urb_work,
1782 dlfb_release_urb_work);
1783
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001784 urb = usb_alloc_urb(0, GFP_KERNEL);
1785 if (!urb) {
1786 kfree(unode);
1787 break;
1788 }
1789 unode->urb = urb;
1790
Greg Kroah-Hartmanc220cc32010-04-29 15:36:29 -07001791 buf = usb_alloc_coherent(dev->udev, MAX_TRANSFER, GFP_KERNEL,
1792 &urb->transfer_dma);
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001793 if (!buf) {
1794 kfree(unode);
1795 usb_free_urb(urb);
1796 break;
1797 }
1798
1799 /* urb->transfer_buffer_length set to actual before submit */
1800 usb_fill_bulk_urb(urb, dev->udev, usb_sndbulkpipe(dev->udev, 1),
1801 buf, size, dlfb_urb_completion, unode);
1802 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1803
1804 list_add_tail(&unode->entry, &dev->urbs.list);
1805
1806 i++;
1807 }
1808
1809 sema_init(&dev->urbs.limit_sem, i);
1810 dev->urbs.count = i;
1811 dev->urbs.available = i;
1812
Soeren Moellerb5a21042010-05-14 19:03:00 +00001813 dl_notice("allocated %d %d byte urbs\n", i, (int) size);
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001814
1815 return i;
1816}
1817
1818static struct urb *dlfb_get_urb(struct dlfb_data *dev)
1819{
1820 int ret = 0;
1821 struct list_head *entry;
1822 struct urb_node *unode;
1823 struct urb *urb = NULL;
1824 unsigned long flags;
1825
1826 /* Wait for an in-flight buffer to complete and get re-queued */
1827 ret = down_timeout(&dev->urbs.limit_sem, GET_URB_TIMEOUT);
1828 if (ret) {
1829 atomic_set(&dev->lost_pixels, 1);
Bernie Thompson5bea1fb2010-09-05 18:28:29 -07001830 dl_warn("wait for urb interrupted: %x available: %d\n",
1831 ret, dev->urbs.available);
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001832 goto error;
1833 }
1834
1835 spin_lock_irqsave(&dev->urbs.lock, flags);
1836
1837 BUG_ON(list_empty(&dev->urbs.list)); /* reserved one with limit_sem */
1838 entry = dev->urbs.list.next;
1839 list_del_init(entry);
1840 dev->urbs.available--;
1841
1842 spin_unlock_irqrestore(&dev->urbs.lock, flags);
1843
1844 unode = list_entry(entry, struct urb_node, entry);
1845 urb = unode->urb;
1846
1847error:
1848 return urb;
1849}
1850
1851static int dlfb_submit_urb(struct dlfb_data *dev, struct urb *urb, size_t len)
1852{
1853 int ret;
1854
1855 BUG_ON(len > dev->urbs.size);
1856
1857 urb->transfer_buffer_length = len; /* set to actual payload len */
1858 ret = usb_submit_urb(urb, GFP_KERNEL);
1859 if (ret) {
1860 dlfb_urb_completion(urb); /* because no one else will */
1861 atomic_set(&dev->lost_pixels, 1);
1862 dl_err("usb_submit_urb error %x\n", ret);
1863 }
1864 return ret;
1865}
1866
Bernie Thompsond5ed5432010-09-05 16:35:39 -07001867module_param(console, bool, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
1868MODULE_PARM_DESC(console, "Allow fbcon to consume first framebuffer found");
1869
1870module_param(fb_defio, bool, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
1871MODULE_PARM_DESC(fb_defio, "Enable fb_defio mmap support. *Experimental*");
1872
Bernie Thompson59277b62009-11-24 15:52:21 -08001873MODULE_AUTHOR("Roberto De Ioris <roberto@unbit.it>, "
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001874 "Jaya Kumar <jayakumar.lkml@gmail.com>, "
1875 "Bernie Thompson <bernie@plugable.com>");
1876MODULE_DESCRIPTION("DisplayLink kernel framebuffer driver");
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001877MODULE_LICENSE("GPL");
Bernie Thompson2469d5d2010-02-15 06:46:13 -08001878