blob: 8d8372fd1b361caf9206fbff2b2415206ebdeab6 [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>
6 *
7 * This file is subject to the terms and conditions of the GNU General Public
8 * License v2. See the file COPYING in the main directory of this archive for
9 * more details.
10 *
11 * Layout is based on skeletonfb by James Simmons and Geert Uytterhoeven,
12 * usb-skeleton by GregKH.
13 *
14 * Device-specific portions based on information from Displaylink, with work
15 * from Florian Echtler, Henrik Bjerregaard Pedersen, and others.
16 */
Roberto De Ioris88e58b12009-06-03 14:03:06 -070017
18#include <linux/module.h>
19#include <linux/kernel.h>
20#include <linux/init.h>
21#include <linux/usb.h>
22#include <linux/uaccess.h>
23#include <linux/mm.h>
24#include <linux/fb.h>
25#include <linux/mutex.h>
Amit Kucheriafb299002009-07-27 12:01:03 +030026#include <linux/vmalloc.h>
Roberto De Ioris88e58b12009-06-03 14:03:06 -070027
28#include "udlfb.h"
29
Bernie Thompson59277b62009-11-24 15:52:21 -080030#define DRIVER_VERSION "DisplayLink Framebuffer Driver 0.4.1"
Roberto De Ioris88e58b12009-06-03 14:03:06 -070031
Bernie Thompson59277b62009-11-24 15:52:21 -080032static struct fb_fix_screeninfo dlfb_fix = {
Bernie Thompson1d31a9e2010-02-15 06:45:43 -080033 .id = "displaylinkfb",
34 .type = FB_TYPE_PACKED_PIXELS,
35 .visual = FB_VISUAL_TRUECOLOR,
36 .xpanstep = 0,
37 .ypanstep = 0,
38 .ywrapstep = 0,
39 .accel = FB_ACCEL_NONE,
Bernie Thompson59277b62009-11-24 15:52:21 -080040};
Roberto De Ioris88e58b12009-06-03 14:03:06 -070041
Bernie Thompsoncc403dc2010-02-15 06:45:49 -080042/*
43 * There are many DisplayLink-based products, all with unique PIDs. We are able
44 * to support all volume ones (circa 2009) with a single driver, so we match
45 * globally on VID. TODO: Probe() needs to detect when we might be running
46 * "future" chips, and bail on those, so a compatible driver can match.
47 */
48static struct usb_device_id id_table[] = {
49 {.idVendor = 0x17e9, .match_flags = USB_DEVICE_ID_MATCH_VENDOR,},
50 {},
51};
52MODULE_DEVICE_TABLE(usb, id_table);
Bernie Thompson59277b62009-11-24 15:52:21 -080053
Bernie Thompson4a4854d2010-02-15 06:45:55 -080054/* dlfb keeps a list of urbs for efficient bulk transfers */
55static void dlfb_urb_completion(struct urb *urb);
56static struct urb *dlfb_get_urb(struct dlfb_data *dev);
57static int dlfb_submit_urb(struct dlfb_data *dev, struct urb * urb, size_t len);
58static int dlfb_alloc_urb_list(struct dlfb_data *dev, int count, size_t size);
59static void dlfb_free_urb_list(struct dlfb_data *dev);
60
Bernie Thompson59277b62009-11-24 15:52:21 -080061/*
62 * Inserts a specific DisplayLink controller command into the provided
63 * buffer.
64 */
Bernie Thompson45742032010-02-15 06:46:04 -080065static char *dlfb_set_register(char *buf, u8 reg, u8 val)
Roberto De Ioris88e58b12009-06-03 14:03:06 -070066{
Bernie Thompson1d31a9e2010-02-15 06:45:43 -080067 *buf++ = 0xAF;
68 *buf++ = 0x20;
69 *buf++ = reg;
70 *buf++ = val;
71 return buf;
Roberto De Ioris88e58b12009-06-03 14:03:06 -070072}
73
Bernie Thompson45742032010-02-15 06:46:04 -080074static char *dlfb_vidreg_lock(char *buf)
Roberto De Ioris88e58b12009-06-03 14:03:06 -070075{
Bernie Thompson45742032010-02-15 06:46:04 -080076 return dlfb_set_register(buf, 0xFF, 0x00);
Bernie Thompson59277b62009-11-24 15:52:21 -080077}
Roberto De Ioris88e58b12009-06-03 14:03:06 -070078
Bernie Thompson45742032010-02-15 06:46:04 -080079static char *dlfb_vidreg_unlock(char *buf)
Bernie Thompson59277b62009-11-24 15:52:21 -080080{
Bernie Thompson45742032010-02-15 06:46:04 -080081 return dlfb_set_register(buf, 0xFF, 0xFF);
Bernie Thompson59277b62009-11-24 15:52:21 -080082}
Roberto De Ioris88e58b12009-06-03 14:03:06 -070083
Bernie Thompson59277b62009-11-24 15:52:21 -080084/*
85 * Once you send this command, the DisplayLink framebuffer gets driven to the
86 * display.
87 */
Bernie Thompson45742032010-02-15 06:46:04 -080088static char *dlfb_enable_hvsync(char *buf)
Bernie Thompson59277b62009-11-24 15:52:21 -080089{
Bernie Thompson45742032010-02-15 06:46:04 -080090 return dlfb_set_register(buf, 0x1F, 0x00);
Bernie Thompson59277b62009-11-24 15:52:21 -080091}
92
Bernie Thompson45742032010-02-15 06:46:04 -080093static char *dlfb_set_color_depth(char *buf, u8 selection)
Bernie Thompson59277b62009-11-24 15:52:21 -080094{
Bernie Thompson45742032010-02-15 06:46:04 -080095 return dlfb_set_register(buf, 0x00, selection);
Bernie Thompson59277b62009-11-24 15:52:21 -080096}
97
Bernie Thompson45742032010-02-15 06:46:04 -080098static char *dlfb_set_base16bpp(char *wrptr, u32 base)
Bernie Thompson59277b62009-11-24 15:52:21 -080099{
Bernie Thompson1d31a9e2010-02-15 06:45:43 -0800100 /* the base pointer is 16 bits wide, 0x20 is hi byte. */
Bernie Thompson45742032010-02-15 06:46:04 -0800101 wrptr = dlfb_set_register(wrptr, 0x20, base >> 16);
102 wrptr = dlfb_set_register(wrptr, 0x21, base >> 8);
103 return dlfb_set_register(wrptr, 0x22, base);
Bernie Thompson59277b62009-11-24 15:52:21 -0800104}
105
Bernie Thompson45742032010-02-15 06:46:04 -0800106static char *dlfb_set_base8bpp(char *wrptr, u32 base)
Bernie Thompson59277b62009-11-24 15:52:21 -0800107{
Bernie Thompson45742032010-02-15 06:46:04 -0800108 wrptr = dlfb_set_register(wrptr, 0x26, base >> 16);
109 wrptr = dlfb_set_register(wrptr, 0x27, base >> 8);
110 return dlfb_set_register(wrptr, 0x28, base);
Bernie Thompson59277b62009-11-24 15:52:21 -0800111}
112
Bernie Thompson45742032010-02-15 06:46:04 -0800113static char *dlfb_set_register_16(char *wrptr, u8 reg, u16 value)
Bernie Thompson59277b62009-11-24 15:52:21 -0800114{
Bernie Thompson45742032010-02-15 06:46:04 -0800115 wrptr = dlfb_set_register(wrptr, reg, value >> 8);
116 return dlfb_set_register(wrptr, reg+1, value);
Bernie Thompson59277b62009-11-24 15:52:21 -0800117}
118
119/*
120 * This is kind of weird because the controller takes some
121 * register values in a different byte order than other registers.
122 */
Bernie Thompson45742032010-02-15 06:46:04 -0800123static char *dlfb_set_register_16be(char *wrptr, u8 reg, u16 value)
Bernie Thompson59277b62009-11-24 15:52:21 -0800124{
Bernie Thompson45742032010-02-15 06:46:04 -0800125 wrptr = dlfb_set_register(wrptr, reg, value);
126 return dlfb_set_register(wrptr, reg+1, value >> 8);
Bernie Thompson59277b62009-11-24 15:52:21 -0800127}
128
129/*
130 * LFSR is linear feedback shift register. The reason we have this is
131 * because the display controller needs to minimize the clock depth of
132 * various counters used in the display path. So this code reverses the
133 * provided value into the lfsr16 value by counting backwards to get
134 * the value that needs to be set in the hardware comparator to get the
135 * same actual count. This makes sense once you read above a couple of
136 * times and think about it from a hardware perspective.
137 */
138static u16 lfsr16(u16 actual_count)
139{
140 u32 lv = 0xFFFF; /* This is the lfsr value that the hw starts with */
141
142 while (actual_count--) {
143 lv = ((lv << 1) |
144 (((lv >> 15) ^ (lv >> 4) ^ (lv >> 2) ^ (lv >> 1)) & 1))
145 & 0xFFFF;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700146 }
Bernie Thompson59277b62009-11-24 15:52:21 -0800147
148 return (u16) lv;
149}
150
151/*
152 * This does LFSR conversion on the value that is to be written.
153 * See LFSR explanation above for more detail.
154 */
Bernie Thompson45742032010-02-15 06:46:04 -0800155static char *dlfb_set_register_lfsr16(char *wrptr, u8 reg, u16 value)
Bernie Thompson59277b62009-11-24 15:52:21 -0800156{
Bernie Thompson45742032010-02-15 06:46:04 -0800157 return dlfb_set_register_16(wrptr, reg, lfsr16(value));
Bernie Thompson59277b62009-11-24 15:52:21 -0800158}
159
160/*
161 * This takes a standard fbdev screeninfo struct and all of its monitor mode
162 * details and converts them into the DisplayLink equivalent register commands.
163 */
Bernie Thompson45742032010-02-15 06:46:04 -0800164static char *dlfb_set_vid_cmds(char *wrptr, struct fb_var_screeninfo *var)
Bernie Thompson59277b62009-11-24 15:52:21 -0800165{
166 u16 xds, yds;
167 u16 xde, yde;
168 u16 yec;
169
Bernie Thompson59277b62009-11-24 15:52:21 -0800170 /* x display start */
171 xds = var->left_margin + var->hsync_len;
Bernie Thompson45742032010-02-15 06:46:04 -0800172 wrptr = dlfb_set_register_lfsr16(wrptr, 0x01, xds);
Bernie Thompson59277b62009-11-24 15:52:21 -0800173 /* x display end */
174 xde = xds + var->xres;
Bernie Thompson45742032010-02-15 06:46:04 -0800175 wrptr = dlfb_set_register_lfsr16(wrptr, 0x03, xde);
Bernie Thompson59277b62009-11-24 15:52:21 -0800176
177 /* y display start */
178 yds = var->upper_margin + var->vsync_len;
Bernie Thompson45742032010-02-15 06:46:04 -0800179 wrptr = dlfb_set_register_lfsr16(wrptr, 0x05, yds);
Bernie Thompson59277b62009-11-24 15:52:21 -0800180 /* y display end */
181 yde = yds + var->yres;
Bernie Thompson45742032010-02-15 06:46:04 -0800182 wrptr = dlfb_set_register_lfsr16(wrptr, 0x07, yde);
Bernie Thompson59277b62009-11-24 15:52:21 -0800183
184 /* x end count is active + blanking - 1 */
Bernie Thompson45742032010-02-15 06:46:04 -0800185 wrptr = dlfb_set_register_lfsr16(wrptr, 0x09,
186 xde + var->right_margin - 1);
Bernie Thompson59277b62009-11-24 15:52:21 -0800187
188 /* libdlo hardcodes hsync start to 1 */
Bernie Thompson45742032010-02-15 06:46:04 -0800189 wrptr = dlfb_set_register_lfsr16(wrptr, 0x0B, 1);
Bernie Thompson59277b62009-11-24 15:52:21 -0800190
191 /* hsync end is width of sync pulse + 1 */
Bernie Thompson45742032010-02-15 06:46:04 -0800192 wrptr = dlfb_set_register_lfsr16(wrptr, 0x0D, var->hsync_len + 1);
Bernie Thompson59277b62009-11-24 15:52:21 -0800193
194 /* hpixels is active pixels */
Bernie Thompson45742032010-02-15 06:46:04 -0800195 wrptr = dlfb_set_register_16(wrptr, 0x0F, var->xres);
Bernie Thompson59277b62009-11-24 15:52:21 -0800196
197 /* yendcount is vertical active + vertical blanking */
198 yec = var->yres + var->upper_margin + var->lower_margin +
199 var->vsync_len;
Bernie Thompson45742032010-02-15 06:46:04 -0800200 wrptr = dlfb_set_register_lfsr16(wrptr, 0x11, yec);
Bernie Thompson59277b62009-11-24 15:52:21 -0800201
202 /* libdlo hardcodes vsync start to 0 */
Bernie Thompson45742032010-02-15 06:46:04 -0800203 wrptr = dlfb_set_register_lfsr16(wrptr, 0x13, 0);
Bernie Thompson59277b62009-11-24 15:52:21 -0800204
205 /* vsync end is width of vsync pulse */
Bernie Thompson45742032010-02-15 06:46:04 -0800206 wrptr = dlfb_set_register_lfsr16(wrptr, 0x15, var->vsync_len);
Bernie Thompson59277b62009-11-24 15:52:21 -0800207
208 /* vpixels is active pixels */
Bernie Thompson45742032010-02-15 06:46:04 -0800209 wrptr = dlfb_set_register_16(wrptr, 0x17, var->yres);
Bernie Thompson59277b62009-11-24 15:52:21 -0800210
211 /* convert picoseconds to 5kHz multiple for pclk5k = x * 1E12/5k */
Bernie Thompson45742032010-02-15 06:46:04 -0800212 wrptr = dlfb_set_register_16be(wrptr, 0x1B,
213 200*1000*1000/var->pixclock);
Bernie Thompson59277b62009-11-24 15:52:21 -0800214
215 return wrptr;
216}
217
218/*
219 * This takes a standard fbdev screeninfo struct that was fetched or prepared
220 * and then generates the appropriate command sequence that then drives the
221 * display controller.
222 */
223static int dlfb_set_video_mode(struct dlfb_data *dev,
224 struct fb_var_screeninfo *var)
225{
226 char *buf;
227 char *wrptr;
228 int retval = 0;
229 int writesize;
230
231 buf = dev->buf;
232
233 /*
234 * This first section has to do with setting the base address on the
235 * controller * associated with the display. There are 2 base
236 * pointers, currently, we only * use the 16 bpp segment.
237 */
Bernie Thompson45742032010-02-15 06:46:04 -0800238 wrptr = dlfb_vidreg_lock(buf);
239 wrptr = dlfb_set_color_depth(wrptr, 0x00);
Bernie Thompson59277b62009-11-24 15:52:21 -0800240 /* set base for 16bpp segment to 0 */
Bernie Thompson45742032010-02-15 06:46:04 -0800241 wrptr = dlfb_set_base16bpp(wrptr, 0);
Bernie Thompson59277b62009-11-24 15:52:21 -0800242 /* set base for 8bpp segment to end of fb */
Bernie Thompson45742032010-02-15 06:46:04 -0800243 wrptr = dlfb_set_base8bpp(wrptr, dev->info->fix.smem_len);
Bernie Thompson59277b62009-11-24 15:52:21 -0800244
Bernie Thompson45742032010-02-15 06:46:04 -0800245 wrptr = dlfb_set_vid_cmds(wrptr, var);
246 wrptr = dlfb_enable_hvsync(wrptr);
247 wrptr = dlfb_vidreg_unlock(wrptr);
Bernie Thompson59277b62009-11-24 15:52:21 -0800248
249 writesize = wrptr - buf;
250
251 mutex_lock(&dev->bulk_mutex);
252 if (!dev->interface) { /* disconnect() was called */
253 mutex_unlock(&dev->bulk_mutex);
254 retval = -ENODEV;
255 goto error;
256 }
257
258 retval = dlfb_bulk_msg(dev, writesize);
259 mutex_unlock(&dev->bulk_mutex);
260 if (retval) {
261 dev_err(&dev->udev->dev, "Problem %d with submit write bulk.\n",
262 retval);
263 goto error;
264 }
265
266 return 0;
267
268error:
269 return retval;
270}
271
Bernie Thompson59277b62009-11-24 15:52:21 -0800272
273/*
274 * Query EDID from the handware, then hand it off to fbdev's edid parse
275 * routine which should give us back a filled in screeninfo structure.
276 */
277static int dlfb_get_var_from_edid(struct dlfb_data *dev,
278 struct fb_var_screeninfo *var)
279{
280 int ret;
281
282 dlfb_edid(dev);
283 ret = fb_parse_edid(dev->edid, var);
284
285 return ret;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700286}
287
Bernie Thompson45742032010-02-15 06:46:04 -0800288static int dlfb_ops_mmap(struct fb_info *info, struct vm_area_struct *vma)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700289{
290 unsigned long start = vma->vm_start;
291 unsigned long size = vma->vm_end - vma->vm_start;
292 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
293 unsigned long page, pos;
294
295 printk("MMAP: %lu %u\n", offset + size, info->fix.smem_len);
296
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700297 if (offset + size > info->fix.smem_len)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700298 return -EINVAL;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700299
300 pos = (unsigned long)info->fix.smem_start + offset;
301
302 while (size > 0) {
303 page = vmalloc_to_pfn((void *)pos);
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700304 if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED))
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700305 return -EAGAIN;
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700306
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700307 start += PAGE_SIZE;
308 pos += PAGE_SIZE;
309 if (size > PAGE_SIZE)
310 size -= PAGE_SIZE;
311 else
312 size = 0;
313 }
314
315 vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
316 return 0;
317
318}
319
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700320/* ioctl structure */
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700321struct dloarea {
322 int x, y;
323 int w, h;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700324 int x2, y2;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700325};
326
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700327static struct usb_driver dlfb_driver;
328
Bernie Thompson1d31a9e2010-02-15 06:45:43 -0800329/* thanks to Henrik Bjerregaard Pedersen for this function */
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700330static char *rle_compress16(uint16_t * src, char *dst, int rem)
331{
332
333 int rl;
334 uint16_t pix0;
335 char *end_if_raw = dst + 6 + 2 * rem;
336
Bernie Thompson1d31a9e2010-02-15 06:45:43 -0800337 dst += 6; /* header will be filled in if RLE is worth it */
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700338
339 while (rem && dst < end_if_raw) {
340 char *start = (char *)src;
341
342 pix0 = *src++;
343 rl = 1;
344 rem--;
345 while (rem && *src == pix0)
346 rem--, rl++, src++;
347 *dst++ = rl;
348 *dst++ = start[1];
349 *dst++ = start[0];
350 }
351
352 return dst;
353}
354
355/*
Bernie Thompson1d31a9e2010-02-15 06:45:43 -0800356Thanks to Henrik Bjerregaard Pedersen for rle implementation
357and code refactoring. Next step is huffman compression.
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700358*/
359
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700360static int
361image_blit(struct dlfb_data *dev_info, int x, int y, int width, int height,
362 char *data)
363{
364
365 int i, j, base;
366 int rem = width;
367 int ret;
368
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700369 int firstdiff, thistime;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700370
371 char *bufptr;
372
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700373 if (x + width > dev_info->info->var.xres)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700374 return -EINVAL;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700375
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700376 if (y + height > dev_info->info->var.yres)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700377 return -EINVAL;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700378
379 mutex_lock(&dev_info->bulk_mutex);
380
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700381 base =
382 dev_info->base16 + ((dev_info->info->var.xres * 2 * y) + (x * 2));
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700383
384 data += (dev_info->info->var.xres * 2 * y) + (x * 2);
385
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700386 /* printk("IMAGE_BLIT\n"); */
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700387
388 bufptr = dev_info->buf;
389
390 for (i = y; i < y + height; i++) {
391
392 if (dev_info->bufend - bufptr < BUF_HIGH_WATER_MARK) {
393 ret = dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
394 bufptr = dev_info->buf;
395 }
396
397 rem = width;
398
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700399 /* printk("WRITING LINE %d\n", i); */
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700400
401 while (rem) {
402
403 if (dev_info->bufend - bufptr < BUF_HIGH_WATER_MARK) {
404 ret =
405 dlfb_bulk_msg(dev_info,
406 bufptr - dev_info->buf);
407 bufptr = dev_info->buf;
408 }
Bernie Thompson1d31a9e2010-02-15 06:45:43 -0800409 /* number of pixels to consider this time */
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700410 thistime = rem;
411 if (thistime > 255)
412 thistime = 255;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700413
Bernie Thompson59277b62009-11-24 15:52:21 -0800414 if (dev_info->backing_buffer) {
415 /* find first pixel that has changed */
416 firstdiff = -1;
417 for (j = 0; j < thistime * 2; j++) {
418 if (dev_info->backing_buffer
419 [base - dev_info->base16 + j]
420 != data[j]) {
421 firstdiff = j / 2;
422 break;
423 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700424 }
Bernie Thompson59277b62009-11-24 15:52:21 -0800425
426 } else {
427 firstdiff = 0;
428
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700429 }
430
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700431 if (firstdiff >= 0) {
432 char *end_of_rle;
433
434 end_of_rle =
435 rle_compress16((uint16_t *) (data +
436 firstdiff * 2),
437 bufptr,
438 thistime - firstdiff);
439
440 if (end_of_rle <
441 bufptr + 6 + 2 * (thistime - firstdiff)) {
442 bufptr[0] = 0xAF;
443 bufptr[1] = 0x69;
444
445 bufptr[2] =
446 (char)((base +
447 firstdiff * 2) >> 16);
448 bufptr[3] =
449 (char)((base + firstdiff * 2) >> 8);
450 bufptr[4] =
451 (char)(base + firstdiff * 2);
452 bufptr[5] = thistime - firstdiff;
453
454 bufptr = end_of_rle;
455
456 } else {
Bernie Thompson1d31a9e2010-02-15 06:45:43 -0800457 /* fallback to raw (or other?) */
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700458 *bufptr++ = 0xAF;
459 *bufptr++ = 0x68;
460
461 *bufptr++ =
462 (char)((base +
463 firstdiff * 2) >> 16);
464 *bufptr++ =
465 (char)((base + firstdiff * 2) >> 8);
466 *bufptr++ =
467 (char)(base + firstdiff * 2);
468 *bufptr++ = thistime - firstdiff;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700469 for (j = firstdiff * 2;
470 j < thistime * 2; j += 2) {
471 *bufptr++ = data[j + 1];
472 *bufptr++ = data[j];
473 }
474 }
475 }
476
477 base += thistime * 2;
478 data += thistime * 2;
479 rem -= thistime;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700480 }
481
Bernie Thompson59277b62009-11-24 15:52:21 -0800482 if (dev_info->backing_buffer)
483 memcpy(dev_info->backing_buffer +
484 (base - dev_info->base16) -
485 (width * 2), data - (width * 2), width * 2);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700486
487 base += (dev_info->info->var.xres * 2) - (width * 2);
488 data += (dev_info->info->var.xres * 2) - (width * 2);
489
490 }
491
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700492 if (bufptr > dev_info->buf) {
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700493 ret = dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700494 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700495
496 mutex_unlock(&dev_info->bulk_mutex);
497
498 return base;
499
500}
501
502static int
503draw_rect(struct dlfb_data *dev_info, int x, int y, int width, int height,
504 unsigned char red, unsigned char green, unsigned char blue)
505{
506
507 int i, j, base;
508 int ret;
509 unsigned short col =
510 (((((red) & 0xF8) | ((green) >> 5)) & 0xFF) << 8) +
511 (((((green) & 0x1C) << 3) | ((blue) >> 3)) & 0xFF);
512 int rem = width;
513
514 char *bufptr;
515
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700516 if (x + width > dev_info->info->var.xres)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700517 return -EINVAL;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700518
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700519 if (y + height > dev_info->info->var.yres)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700520 return -EINVAL;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700521
522 mutex_lock(&dev_info->bulk_mutex);
523
524 base = dev_info->base16 + (dev_info->info->var.xres * 2 * y) + (x * 2);
525
526 bufptr = dev_info->buf;
527
528 for (i = y; i < y + height; i++) {
529
Bernie Thompson59277b62009-11-24 15:52:21 -0800530 if (dev_info->backing_buffer) {
531 for (j = 0; j < width * 2; j += 2) {
532 dev_info->backing_buffer
533 [base - dev_info->base16 + j] =
534 (char)(col >> 8);
535 dev_info->backing_buffer
536 [base - dev_info->base16 + j + 1] =
537 (char)(col);
538 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700539 }
Bernie Thompson59277b62009-11-24 15:52:21 -0800540
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700541 if (dev_info->bufend - bufptr < BUF_HIGH_WATER_MARK) {
542 ret = dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
543 bufptr = dev_info->buf;
544 }
545
546 rem = width;
547
548 while (rem) {
549
550 if (dev_info->bufend - bufptr < BUF_HIGH_WATER_MARK) {
551 ret =
552 dlfb_bulk_msg(dev_info,
553 bufptr - dev_info->buf);
554 bufptr = dev_info->buf;
555 }
556
557 *bufptr++ = 0xAF;
558 *bufptr++ = 0x69;
559
560 *bufptr++ = (char)(base >> 16);
561 *bufptr++ = (char)(base >> 8);
562 *bufptr++ = (char)(base);
563
564 if (rem > 255) {
565 *bufptr++ = 255;
566 *bufptr++ = 255;
567 rem -= 255;
568 base += 255 * 2;
569 } else {
570 *bufptr++ = rem;
571 *bufptr++ = rem;
572 base += rem * 2;
573 rem = 0;
574 }
575
576 *bufptr++ = (char)(col >> 8);
577 *bufptr++ = (char)(col);
578
579 }
580
581 base += (dev_info->info->var.xres * 2) - (width * 2);
582
583 }
584
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700585 if (bufptr > dev_info->buf)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700586 ret = dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700587
588 mutex_unlock(&dev_info->bulk_mutex);
589
590 return 1;
591}
592
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700593static void swapfb(struct dlfb_data *dev_info)
594{
595
596 int tmpbase;
597 char *bufptr;
598
599 mutex_lock(&dev_info->bulk_mutex);
600
601 tmpbase = dev_info->base16;
602
603 dev_info->base16 = dev_info->base16d;
604 dev_info->base16d = tmpbase;
605
606 bufptr = dev_info->buf;
607
608 bufptr = dlfb_set_register(bufptr, 0xFF, 0x00);
609
Bernie Thompson1d31a9e2010-02-15 06:45:43 -0800610 /* set addresses */
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700611 bufptr =
612 dlfb_set_register(bufptr, 0x20, (char)(dev_info->base16 >> 16));
613 bufptr = dlfb_set_register(bufptr, 0x21, (char)(dev_info->base16 >> 8));
614 bufptr = dlfb_set_register(bufptr, 0x22, (char)(dev_info->base16));
615
616 bufptr = dlfb_set_register(bufptr, 0xFF, 0x00);
617
618 dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
619
620 mutex_unlock(&dev_info->bulk_mutex);
621}
622
623static int copyfb(struct dlfb_data *dev_info)
624{
625 int base;
626 int source;
627 int rem;
628 int i, ret;
629
630 char *bufptr;
631
632 base = dev_info->base16d;
633
634 mutex_lock(&dev_info->bulk_mutex);
635
636 source = dev_info->base16;
637
638 bufptr = dev_info->buf;
639
640 for (i = 0; i < dev_info->info->var.yres; i++) {
641
642 if (dev_info->bufend - bufptr < BUF_HIGH_WATER_MARK) {
643 ret = dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
644 bufptr = dev_info->buf;
645 }
646
647 rem = dev_info->info->var.xres;
648
649 while (rem) {
650
651 if (dev_info->bufend - bufptr < BUF_HIGH_WATER_MARK) {
652 ret =
653 dlfb_bulk_msg(dev_info,
654 bufptr - dev_info->buf);
655 bufptr = dev_info->buf;
656
657 }
658
659 *bufptr++ = 0xAF;
660 *bufptr++ = 0x6A;
661
662 *bufptr++ = (char)(base >> 16);
663 *bufptr++ = (char)(base >> 8);
664 *bufptr++ = (char)(base);
665
666 if (rem > 255) {
667 *bufptr++ = 255;
668 *bufptr++ = (char)(source >> 16);
669 *bufptr++ = (char)(source >> 8);
670 *bufptr++ = (char)(source);
671
672 rem -= 255;
673 base += 255 * 2;
674 source += 255 * 2;
675
676 } else {
677 *bufptr++ = rem;
678 *bufptr++ = (char)(source >> 16);
679 *bufptr++ = (char)(source >> 8);
680 *bufptr++ = (char)(source);
681
682 base += rem * 2;
683 source += rem * 2;
684 rem = 0;
685 }
686 }
687 }
688
689 if (bufptr > dev_info->buf)
690 ret = dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
691
692 mutex_unlock(&dev_info->bulk_mutex);
693
694 return 1;
695
696}
697
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700698static int
699copyarea(struct dlfb_data *dev_info, int dx, int dy, int sx, int sy,
700 int width, int height)
701{
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700702 int base;
703 int source;
704 int rem;
705 int i, ret;
706
707 char *bufptr;
708
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700709 if (dx + width > dev_info->info->var.xres)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700710 return -EINVAL;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700711
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700712 if (dy + height > dev_info->info->var.yres)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700713 return -EINVAL;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700714
715 mutex_lock(&dev_info->bulk_mutex);
716
717 base =
718 dev_info->base16 + (dev_info->info->var.xres * 2 * dy) + (dx * 2);
719 source = (dev_info->info->var.xres * 2 * sy) + (sx * 2);
720
721 bufptr = dev_info->buf;
722
723 for (i = sy; i < sy + height; i++) {
724
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700725 memcpy(dev_info->backing_buffer + base - dev_info->base16,
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700726 dev_info->backing_buffer + source, width * 2);
727
728 if (dev_info->bufend - bufptr < BUF_HIGH_WATER_MARK) {
729 ret = dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
730 bufptr = dev_info->buf;
731 }
732
733 rem = width;
734
735 while (rem) {
736
737 if (dev_info->bufend - bufptr < BUF_HIGH_WATER_MARK) {
738 ret =
739 dlfb_bulk_msg(dev_info,
740 bufptr - dev_info->buf);
741 bufptr = dev_info->buf;
742 }
743
744 *bufptr++ = 0xAF;
745 *bufptr++ = 0x6A;
746
747 *bufptr++ = (char)(base >> 16);
748 *bufptr++ = (char)(base >> 8);
749 *bufptr++ = (char)(base);
750
751 if (rem > 255) {
752 *bufptr++ = 255;
753 *bufptr++ = (char)(source >> 16);
754 *bufptr++ = (char)(source >> 8);
755 *bufptr++ = (char)(source);
756
757 rem -= 255;
758 base += 255 * 2;
759 source += 255 * 2;
760
761 } else {
762 *bufptr++ = rem;
763 *bufptr++ = (char)(source >> 16);
764 *bufptr++ = (char)(source >> 8);
765 *bufptr++ = (char)(source);
766
767 base += rem * 2;
768 source += rem * 2;
769 rem = 0;
770 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700771 }
772
773 base += (dev_info->info->var.xres * 2) - (width * 2);
774 source += (dev_info->info->var.xres * 2) - (width * 2);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700775 }
776
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700777 if (bufptr > dev_info->buf)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700778 ret = dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700779
780 mutex_unlock(&dev_info->bulk_mutex);
781
782 return 1;
783}
784
Bernie Thompson45742032010-02-15 06:46:04 -0800785static void dlfb_ops_copyarea(struct fb_info *info,
786 const struct fb_copyarea *area)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700787{
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700788 struct dlfb_data *dev = info->par;
789
790 copyarea(dev, area->dx, area->dy, area->sx, area->sy, area->width,
791 area->height);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700792}
793
Bernie Thompson45742032010-02-15 06:46:04 -0800794static void dlfb_ops_imageblit(struct fb_info *info,
795 const struct fb_image *image)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700796{
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700797 int ret;
798 struct dlfb_data *dev = info->par;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700799 cfb_imageblit(info, image);
800 ret =
801 image_blit(dev, image->dx, image->dy, image->width, image->height,
802 info->screen_base);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700803}
804
Bernie Thompson45742032010-02-15 06:46:04 -0800805static void dlfb_ops_fillrect(struct fb_info *info,
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700806 const struct fb_fillrect *region)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700807{
808
809 unsigned char red, green, blue;
810 struct dlfb_data *dev = info->par;
811
812 memcpy(&red, &region->color, 1);
813 memcpy(&green, &region->color + 1, 1);
814 memcpy(&blue, &region->color + 2, 1);
815 draw_rect(dev, region->dx, region->dy, region->width, region->height,
816 red, green, blue);
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700817 /* printk("FILL RECT %d %d !!!\n", region->dx, region->dy); */
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700818
819}
820
Bernie Thompson45742032010-02-15 06:46:04 -0800821static int dlfb_ops_ioctl(struct fb_info *info, unsigned int cmd,
822 unsigned long arg)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700823{
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700824 struct dlfb_data *dev_info = info->par;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700825 struct dloarea *area = NULL;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700826
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700827 if (cmd == 0xAD) {
828 char *edid = (char *)arg;
829 dlfb_edid(dev_info);
830 if (copy_to_user(edid, dev_info->edid, 128)) {
831 return -EFAULT;
832 }
833 return 0;
834 }
835
836 if (cmd == 0xAA || cmd == 0xAB || cmd == 0xAC) {
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700837
838 area = (struct dloarea *)arg;
839
840 if (area->x < 0)
841 area->x = 0;
842
843 if (area->x > info->var.xres)
844 area->x = info->var.xres;
845
846 if (area->y < 0)
847 area->y = 0;
848
849 if (area->y > info->var.yres)
850 area->y = info->var.yres;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700851 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700852
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700853 if (cmd == 0xAA) {
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700854 image_blit(dev_info, area->x, area->y, area->w, area->h,
855 info->screen_base);
856 }
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700857 if (cmd == 0xAC) {
858 copyfb(dev_info);
859 image_blit(dev_info, area->x, area->y, area->w, area->h,
860 info->screen_base);
861 swapfb(dev_info);
862 } else if (cmd == 0xAB) {
863
864 if (area->x2 < 0)
865 area->x2 = 0;
866
867 if (area->y2 < 0)
868 area->y2 = 0;
869
870 copyarea(dev_info,
871 area->x2, area->y2, area->x, area->y, area->w,
872 area->h);
873 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700874 return 0;
875}
876
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700877/* taken from vesafb */
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700878static int
Bernie Thompson45742032010-02-15 06:46:04 -0800879dlfb_ops_setcolreg(unsigned regno, unsigned red, unsigned green,
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700880 unsigned blue, unsigned transp, struct fb_info *info)
881{
882 int err = 0;
883
884 if (regno >= info->cmap.len)
885 return 1;
886
887 if (regno < 16) {
888 if (info->var.red.offset == 10) {
889 /* 1:5:5:5 */
890 ((u32 *) (info->pseudo_palette))[regno] =
891 ((red & 0xf800) >> 1) |
892 ((green & 0xf800) >> 6) | ((blue & 0xf800) >> 11);
893 } else {
894 /* 0:5:6:5 */
895 ((u32 *) (info->pseudo_palette))[regno] =
896 ((red & 0xf800)) |
897 ((green & 0xfc00) >> 5) | ((blue & 0xf800) >> 11);
898 }
899 }
900
901 return err;
902}
903
Bernie Thompson45742032010-02-15 06:46:04 -0800904static int dlfb_ops_release(struct fb_info *info, int user)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700905{
906 struct dlfb_data *dev_info = info->par;
907 image_blit(dev_info, 0, 0, info->var.xres, info->var.yres,
908 info->screen_base);
909 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 */
917static void dlfb_delete(struct kref *kref)
918{
919 struct dlfb_data *dev = container_of(kref, struct dlfb_data, kref);
920
921 if (dev->backing_buffer)
922 vfree(dev->backing_buffer);
923
924 kfree(dev);
925}
926
Bernie Thompson45742032010-02-15 06:46:04 -0800927static int dlfb_ops_blank(int blank_mode, struct fb_info *info)
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700928{
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700929 struct dlfb_data *dev_info = info->par;
930 char *bufptr = dev_info->buf;
931
932 bufptr = dlfb_set_register(bufptr, 0xFF, 0x00);
933 if (blank_mode != FB_BLANK_UNBLANK) {
934 bufptr = dlfb_set_register(bufptr, 0x1F, 0x01);
935 } else {
936 bufptr = dlfb_set_register(bufptr, 0x1F, 0x00);
937 }
938 bufptr = dlfb_set_register(bufptr, 0xFF, 0xFF);
939
940 dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
941
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700942 return 0;
943}
944
945static struct fb_ops dlfb_ops = {
Bernie Thompson45742032010-02-15 06:46:04 -0800946 .fb_setcolreg = dlfb_ops_setcolreg,
947 .fb_fillrect = dlfb_ops_fillrect,
948 .fb_copyarea = dlfb_ops_copyarea,
949 .fb_imageblit = dlfb_ops_imageblit,
950 .fb_mmap = dlfb_ops_mmap,
951 .fb_ioctl = dlfb_ops_ioctl,
952 .fb_release = dlfb_ops_release,
953 .fb_blank = dlfb_ops_blank,
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700954};
955
Bernie Thompsoncc403dc2010-02-15 06:45:49 -0800956/*
957 * This is necessary before we can communicate with the display controller.
958 */
959static int dlfb_select_std_channel(struct dlfb_data *dev)
960{
961 int ret;
962 u8 set_def_chn[] = { 0x57, 0xCD, 0xDC, 0xA7,
963 0x1C, 0x88, 0x5E, 0x15,
964 0x60, 0xFE, 0xC6, 0x97,
965 0x16, 0x3D, 0x47, 0xF2 };
966
967 ret = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
968 NR_USB_REQUEST_CHANNEL,
969 (USB_DIR_OUT | USB_TYPE_VENDOR), 0, 0,
970 set_def_chn, sizeof(set_def_chn), USB_CTRL_SET_TIMEOUT);
971 return ret;
972}
973
Bernie Thompson59277b62009-11-24 15:52:21 -0800974static int dlfb_probe(struct usb_interface *interface,
975 const struct usb_device_id *id)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700976{
Bernie Thompson59277b62009-11-24 15:52:21 -0800977 struct device *mydev;
978 struct usb_device *usbdev;
979 struct dlfb_data *dev;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700980 struct fb_info *info;
Bernie Thompson59277b62009-11-24 15:52:21 -0800981 int videomemorysize;
982 unsigned char *videomemory;
983 int retval = -ENOMEM;
984 struct fb_var_screeninfo *var;
985 struct fb_bitfield red = { 11, 5, 0 };
986 struct fb_bitfield green = { 5, 6, 0 };
987 struct fb_bitfield blue = { 0, 5, 0 };
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700988
Bernie Thompson59277b62009-11-24 15:52:21 -0800989 usbdev = usb_get_dev(interface_to_usbdev(interface));
990 mydev = &usbdev->dev;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700991
Bernie Thompson59277b62009-11-24 15:52:21 -0800992 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
993 if (dev == NULL) {
994 dev_err(mydev, "failed alloc of dev struct\n");
995 goto err_devalloc;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700996 }
997
Bernie Thompson59277b62009-11-24 15:52:21 -0800998 mutex_init(&dev->bulk_mutex);
999 dev->udev = usbdev;
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001000 dev->gdev = &usbdev->dev; /* our generic struct device * */
Bernie Thompson59277b62009-11-24 15:52:21 -08001001 dev->interface = interface;
1002 usb_set_intfdata(interface, dev);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001003
Bernie Thompson59277b62009-11-24 15:52:21 -08001004 dev_info(mydev, "dlfb_probe: setting up DisplayLink device\n");
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001005
Bernie Thompson59277b62009-11-24 15:52:21 -08001006 /*
1007 * TODO: replace single 64K buffer with buffer list
1008 * and async dispatch
1009 */
1010 dev->buf = kmalloc(BUF_SIZE, GFP_KERNEL);
1011 if (dev->buf == NULL) {
1012 dev_err(mydev, "unable to allocate memory for dlfb commands\n");
1013 goto err_usballoc;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001014 }
Bernie Thompson59277b62009-11-24 15:52:21 -08001015 dev->bufend = dev->buf + BUF_SIZE;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001016
Bernie Thompson59277b62009-11-24 15:52:21 -08001017 dev->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
1018 usb_fill_bulk_urb(dev->tx_urb, dev->udev,
1019 usb_sndbulkpipe(dev->udev, 1), dev->buf, 0,
1020 dlfb_bulk_callback, dev);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001021
Bernie Thompson59277b62009-11-24 15:52:21 -08001022 /* allocates framebuffer driver structure, not framebuffer memory */
1023 info = framebuffer_alloc(0, mydev);
1024 if (!info)
1025 goto err_fballoc;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001026
Bernie Thompson59277b62009-11-24 15:52:21 -08001027 dev->info = info;
1028 info->par = dev;
1029 info->pseudo_palette = dev->pseudo_palette;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001030
Bernie Thompson59277b62009-11-24 15:52:21 -08001031 var = &info->var;
1032 retval = dlfb_get_var_from_edid(dev, var);
1033 if (retval) {
1034 /* had a problem getting edid. so fallback to 640x480 */
1035 dev_err(mydev, "Problem %d with EDID.\n", retval);
1036 var->xres = 640;
1037 var->yres = 480;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001038 }
1039
Bernie Thompson59277b62009-11-24 15:52:21 -08001040 /*
1041 * ok, now that we've got the size info, we can alloc our framebuffer.
1042 * We are using 16bpp.
1043 */
1044 info->var.bits_per_pixel = 16;
1045 info->fix = dlfb_fix;
1046 info->fix.line_length = var->xres * (var->bits_per_pixel / 8);
1047 videomemorysize = info->fix.line_length * var->yres;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001048
Bernie Thompson59277b62009-11-24 15:52:21 -08001049 /*
1050 * The big chunk of system memory we use as a virtual framebuffer.
1051 * Pages don't need to be set RESERVED (non-swap) immediately on 2.6
1052 * remap_pfn_page() syscall in our mmap and/or defio will handle.
1053 */
1054 videomemory = vmalloc(videomemorysize);
1055 if (!videomemory)
1056 goto err_vidmem;
1057 memset(videomemory, 0, videomemorysize);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001058
Bernie Thompson59277b62009-11-24 15:52:21 -08001059 info->screen_base = videomemory;
1060 info->fix.smem_len = PAGE_ALIGN(videomemorysize);
1061 info->fix.smem_start = (unsigned long) videomemory;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001062 info->flags =
1063 FBINFO_DEFAULT | FBINFO_READS_FAST | FBINFO_HWACCEL_IMAGEBLIT |
1064 FBINFO_HWACCEL_COPYAREA | FBINFO_HWACCEL_FILLRECT;
Bernie Thompson59277b62009-11-24 15:52:21 -08001065
1066 /*
1067 * Second framebuffer copy, mirroring the state of the framebuffer
1068 * on the physical USB device. We can function without this.
1069 * But with imperfect damage info we may end up sending pixels over USB
1070 * that were, in fact, unchanged -- wasting limited USB bandwidth
1071 */
1072 dev->backing_buffer = vmalloc(dev->screen_size);
1073 if (!dev->backing_buffer)
1074 dev_info(mydev, "No backing buffer allocated!\n");
1075
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001076 info->fbops = &dlfb_ops;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001077
Bernie Thompson59277b62009-11-24 15:52:21 -08001078 var->vmode = FB_VMODE_NONINTERLACED;
1079 var->red = red;
1080 var->green = green;
1081 var->blue = blue;
1082
1083 /*
1084 * TODO: Enable FB_CONFIG_DEFIO support
1085
1086 info->fbdefio = &dlfb_defio;
1087 fb_deferred_io_init(info);
1088
1089 */
1090
1091 retval = fb_alloc_cmap(&info->cmap, 256, 0);
1092 if (retval < 0) {
1093 dev_err(mydev, "Failed to allocate colormap\n");
1094 goto err_cmap;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001095 }
1096
Bernie Thompson59277b62009-11-24 15:52:21 -08001097 dlfb_select_std_channel(dev);
1098 dlfb_set_video_mode(dev, var);
1099 /* TODO: dlfb_dpy_update(dev); */
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001100
Bernie Thompson59277b62009-11-24 15:52:21 -08001101 retval = register_framebuffer(info);
1102 if (retval < 0)
1103 goto err_regfb;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001104
Bernie Thompson59277b62009-11-24 15:52:21 -08001105 /* paint "successful" green screen */
1106 draw_rect(dev, 0, 0, dev->info->var.xres,
1107 dev->info->var.yres, 0x30, 0xff, 0x30);
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -07001108
Bernie Thompson59277b62009-11-24 15:52:21 -08001109 dev_info(mydev, "DisplayLink USB device %d now attached, "
1110 "using %dK of memory\n", info->node,
1111 ((dev->backing_buffer) ?
1112 videomemorysize * 2 : videomemorysize) >> 10);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001113 return 0;
1114
Bernie Thompson59277b62009-11-24 15:52:21 -08001115err_regfb:
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001116 fb_dealloc_cmap(&info->cmap);
Bernie Thompson59277b62009-11-24 15:52:21 -08001117err_cmap:
1118 /* TODO: fb_deferred_io_cleanup(info); */
1119 vfree(videomemory);
1120err_vidmem:
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001121 framebuffer_release(info);
Bernie Thompson59277b62009-11-24 15:52:21 -08001122err_fballoc:
1123 kfree(dev->buf);
1124err_usballoc:
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001125 usb_set_intfdata(interface, NULL);
Bernie Thompson59277b62009-11-24 15:52:21 -08001126 usb_put_dev(dev->udev);
1127 kfree(dev);
1128err_devalloc:
1129 return retval;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001130}
1131
1132static void dlfb_disconnect(struct usb_interface *interface)
1133{
Bernie Thompson59277b62009-11-24 15:52:21 -08001134 struct dlfb_data *dev;
1135 struct fb_info *info;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001136
Bernie Thompson59277b62009-11-24 15:52:21 -08001137 dev = usb_get_intfdata(interface);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001138 usb_set_intfdata(interface, NULL);
Bernie Thompson59277b62009-11-24 15:52:21 -08001139 usb_put_dev(dev->udev);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001140
Bernie Thompson59277b62009-11-24 15:52:21 -08001141 /*
1142 * TODO: since, upon usb disconnect(), usb will cancel in-flight urbs
1143 * and error out any new ones, look at eliminating need for mutex
1144 */
1145 mutex_lock(&dev->bulk_mutex);
1146 dev->interface = NULL;
1147 info = dev->info;
1148 mutex_unlock(&dev->bulk_mutex);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001149
Bernie Thompson59277b62009-11-24 15:52:21 -08001150 if (info) {
1151 dev_info(&interface->dev, "Detaching DisplayLink device %d.\n",
1152 info->node);
1153 unregister_framebuffer(info);
1154 fb_dealloc_cmap(&info->cmap);
1155 /* TODO: fb_deferred_io_cleanup(info); */
1156 fb_dealloc_cmap(&info->cmap);
1157 vfree((void __force *)info->screen_base);
1158 framebuffer_release(info);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001159 }
1160
Bernie Thompson59277b62009-11-24 15:52:21 -08001161 if (dev->backing_buffer)
1162 vfree(dev->backing_buffer);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001163
Bernie Thompson59277b62009-11-24 15:52:21 -08001164 kfree(dev);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001165}
1166
1167static struct usb_driver dlfb_driver = {
1168 .name = "udlfb",
1169 .probe = dlfb_probe,
1170 .disconnect = dlfb_disconnect,
1171 .id_table = id_table,
1172};
1173
1174static int __init dlfb_init(void)
1175{
1176 int res;
1177
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001178 res = usb_register(&dlfb_driver);
1179 if (res)
1180 err("usb_register failed. Error number %d", res);
1181
1182 printk("VMODES initialized\n");
1183
1184 return res;
1185}
1186
1187static void __exit dlfb_exit(void)
1188{
1189 usb_deregister(&dlfb_driver);
1190}
1191
1192module_init(dlfb_init);
1193module_exit(dlfb_exit);
1194
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001195static void dlfb_urb_completion(struct urb *urb)
1196{
1197 struct urb_node *unode = urb->context;
1198 struct dlfb_data *dev = unode->dev;
1199 unsigned long flags;
1200
1201 /* sync/async unlink faults aren't errors */
1202 if (urb->status) {
1203 if (!(urb->status == -ENOENT ||
1204 urb->status == -ECONNRESET ||
1205 urb->status == -ESHUTDOWN)) {
1206 dl_err("%s - nonzero write bulk status received: %d\n",
1207 __func__, urb->status);
1208 atomic_set(&dev->lost_pixels, 1);
1209 }
1210 }
1211
1212 urb->transfer_buffer_length = dev->urbs.size; /* reset to actual */
1213
1214 spin_lock_irqsave(&dev->urbs.lock, flags);
1215 list_add_tail(&unode->entry, &dev->urbs.list);
1216 dev->urbs.available++;
1217 spin_unlock_irqrestore(&dev->urbs.lock, flags);
1218
1219 up(&dev->urbs.limit_sem);
1220}
1221
1222static void dlfb_free_urb_list(struct dlfb_data *dev)
1223{
1224 int count = dev->urbs.count;
1225 struct list_head *node;
1226 struct urb_node *unode;
1227 struct urb *urb;
1228 int ret;
1229 unsigned long flags;
1230
1231 dl_notice("Waiting for completes and freeing all render urbs\n");
1232
1233 /* keep waiting and freeing, until we've got 'em all */
1234 while (count--) {
1235 /* Timeout means a memory leak and/or fault */
1236 ret = down_timeout(&dev->urbs.limit_sem, FREE_URB_TIMEOUT);
1237 if (ret) {
1238 BUG_ON(ret);
1239 break;
1240 }
1241 spin_lock_irqsave(&dev->urbs.lock, flags);
1242
1243 node = dev->urbs.list.next; /* have reserved one with sem */
1244 list_del_init(node);
1245
1246 spin_unlock_irqrestore(&dev->urbs.lock, flags);
1247
1248 unode = list_entry(node, struct urb_node, entry);
1249 urb = unode->urb;
1250
1251 /* Free each separately allocated piece */
1252 usb_buffer_free(urb->dev, dev->urbs.size,
1253 urb->transfer_buffer, urb->transfer_dma);
1254 usb_free_urb(urb);
1255 kfree(node);
1256 }
1257
1258 kref_put(&dev->kref, dlfb_delete);
1259
1260}
1261
1262static int dlfb_alloc_urb_list(struct dlfb_data *dev, int count, size_t size)
1263{
1264 int i = 0;
1265 struct urb *urb;
1266 struct urb_node *unode;
1267 char *buf;
1268
1269 spin_lock_init(&dev->urbs.lock);
1270
1271 dev->urbs.size = size;
1272 INIT_LIST_HEAD(&dev->urbs.list);
1273
1274 while (i < count) {
1275 unode = kzalloc(sizeof(struct urb_node), GFP_KERNEL);
1276 if (!unode)
1277 break;
1278 unode->dev = dev;
1279
1280 urb = usb_alloc_urb(0, GFP_KERNEL);
1281 if (!urb) {
1282 kfree(unode);
1283 break;
1284 }
1285 unode->urb = urb;
1286
1287 buf = usb_buffer_alloc(dev->udev, MAX_TRANSFER, GFP_KERNEL,
1288 &urb->transfer_dma);
1289 if (!buf) {
1290 kfree(unode);
1291 usb_free_urb(urb);
1292 break;
1293 }
1294
1295 /* urb->transfer_buffer_length set to actual before submit */
1296 usb_fill_bulk_urb(urb, dev->udev, usb_sndbulkpipe(dev->udev, 1),
1297 buf, size, dlfb_urb_completion, unode);
1298 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1299
1300 list_add_tail(&unode->entry, &dev->urbs.list);
1301
1302 i++;
1303 }
1304
1305 sema_init(&dev->urbs.limit_sem, i);
1306 dev->urbs.count = i;
1307 dev->urbs.available = i;
1308
1309 kref_get(&dev->kref); /* released in free_render_urbs() */
1310
1311 dl_notice("allocated %d %d byte urbs \n", i, (int) size);
1312
1313 return i;
1314}
1315
1316static struct urb *dlfb_get_urb(struct dlfb_data *dev)
1317{
1318 int ret = 0;
1319 struct list_head *entry;
1320 struct urb_node *unode;
1321 struct urb *urb = NULL;
1322 unsigned long flags;
1323
1324 /* Wait for an in-flight buffer to complete and get re-queued */
1325 ret = down_timeout(&dev->urbs.limit_sem, GET_URB_TIMEOUT);
1326 if (ret) {
1327 atomic_set(&dev->lost_pixels, 1);
1328 dl_err("wait for urb interrupted: %x\n", ret);
1329 goto error;
1330 }
1331
1332 spin_lock_irqsave(&dev->urbs.lock, flags);
1333
1334 BUG_ON(list_empty(&dev->urbs.list)); /* reserved one with limit_sem */
1335 entry = dev->urbs.list.next;
1336 list_del_init(entry);
1337 dev->urbs.available--;
1338
1339 spin_unlock_irqrestore(&dev->urbs.lock, flags);
1340
1341 unode = list_entry(entry, struct urb_node, entry);
1342 urb = unode->urb;
1343
1344error:
1345 return urb;
1346}
1347
1348static int dlfb_submit_urb(struct dlfb_data *dev, struct urb *urb, size_t len)
1349{
1350 int ret;
1351
1352 BUG_ON(len > dev->urbs.size);
1353
1354 urb->transfer_buffer_length = len; /* set to actual payload len */
1355 ret = usb_submit_urb(urb, GFP_KERNEL);
1356 if (ret) {
1357 dlfb_urb_completion(urb); /* because no one else will */
1358 atomic_set(&dev->lost_pixels, 1);
1359 dl_err("usb_submit_urb error %x\n", ret);
1360 }
1361 return ret;
1362}
1363
Bernie Thompson59277b62009-11-24 15:52:21 -08001364MODULE_AUTHOR("Roberto De Ioris <roberto@unbit.it>, "
1365 "Jaya Kumar <jayakumar.lkml@gmail.com>");
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001366MODULE_DESCRIPTION(DRIVER_VERSION);
1367MODULE_LICENSE("GPL");