blob: 3306bba9df3d3e62f2396ff3ab440e01fcd5714f [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
54/*
55 * Inserts a specific DisplayLink controller command into the provided
56 * buffer.
57 */
58static char *insert_command(char *buf, u8 reg, u8 val)
Roberto De Ioris88e58b12009-06-03 14:03:06 -070059{
Bernie Thompson1d31a9e2010-02-15 06:45:43 -080060 *buf++ = 0xAF;
61 *buf++ = 0x20;
62 *buf++ = reg;
63 *buf++ = val;
64 return buf;
Roberto De Ioris88e58b12009-06-03 14:03:06 -070065}
66
Bernie Thompson59277b62009-11-24 15:52:21 -080067static char *insert_vidreg_lock(char *buf)
Roberto De Ioris88e58b12009-06-03 14:03:06 -070068{
Bernie Thompson1d31a9e2010-02-15 06:45:43 -080069 return insert_command(buf, 0xFF, 0x00);
Bernie Thompson59277b62009-11-24 15:52:21 -080070}
Roberto De Ioris88e58b12009-06-03 14:03:06 -070071
Bernie Thompson59277b62009-11-24 15:52:21 -080072static char *insert_vidreg_unlock(char *buf)
73{
Bernie Thompson1d31a9e2010-02-15 06:45:43 -080074 return insert_command(buf, 0xFF, 0xFF);
Bernie Thompson59277b62009-11-24 15:52:21 -080075}
Roberto De Ioris88e58b12009-06-03 14:03:06 -070076
Bernie Thompson59277b62009-11-24 15:52:21 -080077/*
78 * Once you send this command, the DisplayLink framebuffer gets driven to the
79 * display.
80 */
81static char *insert_enable_hvsync(char *buf)
82{
Bernie Thompson1d31a9e2010-02-15 06:45:43 -080083 return insert_command(buf, 0x1F, 0x00);
Bernie Thompson59277b62009-11-24 15:52:21 -080084}
85
86static char *insert_set_color_depth(char *buf, u8 selection)
87{
Bernie Thompson1d31a9e2010-02-15 06:45:43 -080088 return insert_command(buf, 0x00, selection);
Bernie Thompson59277b62009-11-24 15:52:21 -080089}
90
91static char *insert_set_base16bpp(char *wrptr, u32 base)
92{
Bernie Thompson1d31a9e2010-02-15 06:45:43 -080093 /* the base pointer is 16 bits wide, 0x20 is hi byte. */
94 wrptr = insert_command(wrptr, 0x20, base >> 16);
95 wrptr = insert_command(wrptr, 0x21, base >> 8);
96 return insert_command(wrptr, 0x22, base);
Bernie Thompson59277b62009-11-24 15:52:21 -080097}
98
99static char *insert_set_base8bpp(char *wrptr, u32 base)
100{
Bernie Thompson1d31a9e2010-02-15 06:45:43 -0800101 wrptr = insert_command(wrptr, 0x26, base >> 16);
102 wrptr = insert_command(wrptr, 0x27, base >> 8);
103 return insert_command(wrptr, 0x28, base);
Bernie Thompson59277b62009-11-24 15:52:21 -0800104}
105
106static char *insert_command_16(char *wrptr, u8 reg, u16 value)
107{
Bernie Thompson1d31a9e2010-02-15 06:45:43 -0800108 wrptr = insert_command(wrptr, reg, value >> 8);
109 return insert_command(wrptr, reg+1, value);
Bernie Thompson59277b62009-11-24 15:52:21 -0800110}
111
112/*
113 * This is kind of weird because the controller takes some
114 * register values in a different byte order than other registers.
115 */
116static char *insert_command_16be(char *wrptr, u8 reg, u16 value)
117{
Bernie Thompson1d31a9e2010-02-15 06:45:43 -0800118 wrptr = insert_command(wrptr, reg, value);
119 return insert_command(wrptr, reg+1, value >> 8);
Bernie Thompson59277b62009-11-24 15:52:21 -0800120}
121
122/*
123 * LFSR is linear feedback shift register. The reason we have this is
124 * because the display controller needs to minimize the clock depth of
125 * various counters used in the display path. So this code reverses the
126 * provided value into the lfsr16 value by counting backwards to get
127 * the value that needs to be set in the hardware comparator to get the
128 * same actual count. This makes sense once you read above a couple of
129 * times and think about it from a hardware perspective.
130 */
131static u16 lfsr16(u16 actual_count)
132{
133 u32 lv = 0xFFFF; /* This is the lfsr value that the hw starts with */
134
135 while (actual_count--) {
136 lv = ((lv << 1) |
137 (((lv >> 15) ^ (lv >> 4) ^ (lv >> 2) ^ (lv >> 1)) & 1))
138 & 0xFFFF;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700139 }
Bernie Thompson59277b62009-11-24 15:52:21 -0800140
141 return (u16) lv;
142}
143
144/*
145 * This does LFSR conversion on the value that is to be written.
146 * See LFSR explanation above for more detail.
147 */
148static char *insert_command_lfsr16(char *wrptr, u8 reg, u16 value)
149{
150 return insert_command_16(wrptr, reg, lfsr16(value));
151}
152
153/*
154 * This takes a standard fbdev screeninfo struct and all of its monitor mode
155 * details and converts them into the DisplayLink equivalent register commands.
156 */
157static char *insert_set_vid_cmds(char *wrptr, struct fb_var_screeninfo *var)
158{
159 u16 xds, yds;
160 u16 xde, yde;
161 u16 yec;
162
163
164 /* x display start */
165 xds = var->left_margin + var->hsync_len;
166 wrptr = insert_command_lfsr16(wrptr, 0x01, xds);
167 /* x display end */
168 xde = xds + var->xres;
169 wrptr = insert_command_lfsr16(wrptr, 0x03, xde);
170
171 /* y display start */
172 yds = var->upper_margin + var->vsync_len;
173 wrptr = insert_command_lfsr16(wrptr, 0x05, yds);
174 /* y display end */
175 yde = yds + var->yres;
176 wrptr = insert_command_lfsr16(wrptr, 0x07, yde);
177
178 /* x end count is active + blanking - 1 */
179 wrptr = insert_command_lfsr16(wrptr, 0x09, xde + var->right_margin - 1);
180
181 /* libdlo hardcodes hsync start to 1 */
182 wrptr = insert_command_lfsr16(wrptr, 0x0B, 1);
183
184 /* hsync end is width of sync pulse + 1 */
185 wrptr = insert_command_lfsr16(wrptr, 0x0D, var->hsync_len + 1);
186
187 /* hpixels is active pixels */
188 wrptr = insert_command_16(wrptr, 0x0F, var->xres);
189
190 /* yendcount is vertical active + vertical blanking */
191 yec = var->yres + var->upper_margin + var->lower_margin +
192 var->vsync_len;
193 wrptr = insert_command_lfsr16(wrptr, 0x11, yec);
194
195 /* libdlo hardcodes vsync start to 0 */
196 wrptr = insert_command_lfsr16(wrptr, 0x13, 0);
197
198 /* vsync end is width of vsync pulse */
199 wrptr = insert_command_lfsr16(wrptr, 0x15, var->vsync_len);
200
201 /* vpixels is active pixels */
202 wrptr = insert_command_16(wrptr, 0x17, var->yres);
203
204 /* convert picoseconds to 5kHz multiple for pclk5k = x * 1E12/5k */
205 wrptr = insert_command_16be(wrptr, 0x1B, 200*1000*1000/var->pixclock);
206
207 return wrptr;
208}
209
210/*
211 * This takes a standard fbdev screeninfo struct that was fetched or prepared
212 * and then generates the appropriate command sequence that then drives the
213 * display controller.
214 */
215static int dlfb_set_video_mode(struct dlfb_data *dev,
216 struct fb_var_screeninfo *var)
217{
218 char *buf;
219 char *wrptr;
220 int retval = 0;
221 int writesize;
222
223 buf = dev->buf;
224
225 /*
226 * This first section has to do with setting the base address on the
227 * controller * associated with the display. There are 2 base
228 * pointers, currently, we only * use the 16 bpp segment.
229 */
230 wrptr = insert_vidreg_lock(buf);
231 wrptr = insert_set_color_depth(wrptr, 0x00);
232 /* set base for 16bpp segment to 0 */
233 wrptr = insert_set_base16bpp(wrptr, 0);
234 /* set base for 8bpp segment to end of fb */
235 wrptr = insert_set_base8bpp(wrptr, dev->info->fix.smem_len);
236
237 wrptr = insert_set_vid_cmds(wrptr, var);
238 wrptr = insert_enable_hvsync(wrptr);
239 wrptr = insert_vidreg_unlock(wrptr);
240
241 writesize = wrptr - buf;
242
243 mutex_lock(&dev->bulk_mutex);
244 if (!dev->interface) { /* disconnect() was called */
245 mutex_unlock(&dev->bulk_mutex);
246 retval = -ENODEV;
247 goto error;
248 }
249
250 retval = dlfb_bulk_msg(dev, writesize);
251 mutex_unlock(&dev->bulk_mutex);
252 if (retval) {
253 dev_err(&dev->udev->dev, "Problem %d with submit write bulk.\n",
254 retval);
255 goto error;
256 }
257
258 return 0;
259
260error:
261 return retval;
262}
263
Bernie Thompson59277b62009-11-24 15:52:21 -0800264
265/*
266 * Query EDID from the handware, then hand it off to fbdev's edid parse
267 * routine which should give us back a filled in screeninfo structure.
268 */
269static int dlfb_get_var_from_edid(struct dlfb_data *dev,
270 struct fb_var_screeninfo *var)
271{
272 int ret;
273
274 dlfb_edid(dev);
275 ret = fb_parse_edid(dev->edid, var);
276
277 return ret;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700278}
279
280static int dlfb_mmap(struct fb_info *info, struct vm_area_struct *vma)
281{
282 unsigned long start = vma->vm_start;
283 unsigned long size = vma->vm_end - vma->vm_start;
284 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
285 unsigned long page, pos;
286
287 printk("MMAP: %lu %u\n", offset + size, info->fix.smem_len);
288
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700289 if (offset + size > info->fix.smem_len)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700290 return -EINVAL;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700291
292 pos = (unsigned long)info->fix.smem_start + offset;
293
294 while (size > 0) {
295 page = vmalloc_to_pfn((void *)pos);
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700296 if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED))
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700297 return -EAGAIN;
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700298
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700299 start += PAGE_SIZE;
300 pos += PAGE_SIZE;
301 if (size > PAGE_SIZE)
302 size -= PAGE_SIZE;
303 else
304 size = 0;
305 }
306
307 vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
308 return 0;
309
310}
311
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700312/* ioctl structure */
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700313struct dloarea {
314 int x, y;
315 int w, h;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700316 int x2, y2;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700317};
318
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700319static struct usb_driver dlfb_driver;
320
Bernie Thompson1d31a9e2010-02-15 06:45:43 -0800321/* thanks to Henrik Bjerregaard Pedersen for this function */
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700322static char *rle_compress16(uint16_t * src, char *dst, int rem)
323{
324
325 int rl;
326 uint16_t pix0;
327 char *end_if_raw = dst + 6 + 2 * rem;
328
Bernie Thompson1d31a9e2010-02-15 06:45:43 -0800329 dst += 6; /* header will be filled in if RLE is worth it */
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700330
331 while (rem && dst < end_if_raw) {
332 char *start = (char *)src;
333
334 pix0 = *src++;
335 rl = 1;
336 rem--;
337 while (rem && *src == pix0)
338 rem--, rl++, src++;
339 *dst++ = rl;
340 *dst++ = start[1];
341 *dst++ = start[0];
342 }
343
344 return dst;
345}
346
347/*
Bernie Thompson1d31a9e2010-02-15 06:45:43 -0800348Thanks to Henrik Bjerregaard Pedersen for rle implementation
349and code refactoring. Next step is huffman compression.
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700350*/
351
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700352static int
353image_blit(struct dlfb_data *dev_info, int x, int y, int width, int height,
354 char *data)
355{
356
357 int i, j, base;
358 int rem = width;
359 int ret;
360
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700361 int firstdiff, thistime;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700362
363 char *bufptr;
364
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700365 if (x + width > dev_info->info->var.xres)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700366 return -EINVAL;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700367
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700368 if (y + height > dev_info->info->var.yres)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700369 return -EINVAL;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700370
371 mutex_lock(&dev_info->bulk_mutex);
372
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700373 base =
374 dev_info->base16 + ((dev_info->info->var.xres * 2 * y) + (x * 2));
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700375
376 data += (dev_info->info->var.xres * 2 * y) + (x * 2);
377
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700378 /* printk("IMAGE_BLIT\n"); */
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700379
380 bufptr = dev_info->buf;
381
382 for (i = y; i < y + height; i++) {
383
384 if (dev_info->bufend - bufptr < BUF_HIGH_WATER_MARK) {
385 ret = dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
386 bufptr = dev_info->buf;
387 }
388
389 rem = width;
390
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700391 /* printk("WRITING LINE %d\n", i); */
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700392
393 while (rem) {
394
395 if (dev_info->bufend - bufptr < BUF_HIGH_WATER_MARK) {
396 ret =
397 dlfb_bulk_msg(dev_info,
398 bufptr - dev_info->buf);
399 bufptr = dev_info->buf;
400 }
Bernie Thompson1d31a9e2010-02-15 06:45:43 -0800401 /* number of pixels to consider this time */
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700402 thistime = rem;
403 if (thistime > 255)
404 thistime = 255;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700405
Bernie Thompson59277b62009-11-24 15:52:21 -0800406 if (dev_info->backing_buffer) {
407 /* find first pixel that has changed */
408 firstdiff = -1;
409 for (j = 0; j < thistime * 2; j++) {
410 if (dev_info->backing_buffer
411 [base - dev_info->base16 + j]
412 != data[j]) {
413 firstdiff = j / 2;
414 break;
415 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700416 }
Bernie Thompson59277b62009-11-24 15:52:21 -0800417
418 } else {
419 firstdiff = 0;
420
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700421 }
422
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700423 if (firstdiff >= 0) {
424 char *end_of_rle;
425
426 end_of_rle =
427 rle_compress16((uint16_t *) (data +
428 firstdiff * 2),
429 bufptr,
430 thistime - firstdiff);
431
432 if (end_of_rle <
433 bufptr + 6 + 2 * (thistime - firstdiff)) {
434 bufptr[0] = 0xAF;
435 bufptr[1] = 0x69;
436
437 bufptr[2] =
438 (char)((base +
439 firstdiff * 2) >> 16);
440 bufptr[3] =
441 (char)((base + firstdiff * 2) >> 8);
442 bufptr[4] =
443 (char)(base + firstdiff * 2);
444 bufptr[5] = thistime - firstdiff;
445
446 bufptr = end_of_rle;
447
448 } else {
Bernie Thompson1d31a9e2010-02-15 06:45:43 -0800449 /* fallback to raw (or other?) */
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700450 *bufptr++ = 0xAF;
451 *bufptr++ = 0x68;
452
453 *bufptr++ =
454 (char)((base +
455 firstdiff * 2) >> 16);
456 *bufptr++ =
457 (char)((base + firstdiff * 2) >> 8);
458 *bufptr++ =
459 (char)(base + firstdiff * 2);
460 *bufptr++ = thistime - firstdiff;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700461 for (j = firstdiff * 2;
462 j < thistime * 2; j += 2) {
463 *bufptr++ = data[j + 1];
464 *bufptr++ = data[j];
465 }
466 }
467 }
468
469 base += thistime * 2;
470 data += thistime * 2;
471 rem -= thistime;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700472 }
473
Bernie Thompson59277b62009-11-24 15:52:21 -0800474 if (dev_info->backing_buffer)
475 memcpy(dev_info->backing_buffer +
476 (base - dev_info->base16) -
477 (width * 2), data - (width * 2), width * 2);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700478
479 base += (dev_info->info->var.xres * 2) - (width * 2);
480 data += (dev_info->info->var.xres * 2) - (width * 2);
481
482 }
483
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700484 if (bufptr > dev_info->buf) {
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700485 ret = dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700486 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700487
488 mutex_unlock(&dev_info->bulk_mutex);
489
490 return base;
491
492}
493
494static int
495draw_rect(struct dlfb_data *dev_info, int x, int y, int width, int height,
496 unsigned char red, unsigned char green, unsigned char blue)
497{
498
499 int i, j, base;
500 int ret;
501 unsigned short col =
502 (((((red) & 0xF8) | ((green) >> 5)) & 0xFF) << 8) +
503 (((((green) & 0x1C) << 3) | ((blue) >> 3)) & 0xFF);
504 int rem = width;
505
506 char *bufptr;
507
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700508 if (x + width > dev_info->info->var.xres)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700509 return -EINVAL;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700510
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700511 if (y + height > dev_info->info->var.yres)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700512 return -EINVAL;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700513
514 mutex_lock(&dev_info->bulk_mutex);
515
516 base = dev_info->base16 + (dev_info->info->var.xres * 2 * y) + (x * 2);
517
518 bufptr = dev_info->buf;
519
520 for (i = y; i < y + height; i++) {
521
Bernie Thompson59277b62009-11-24 15:52:21 -0800522 if (dev_info->backing_buffer) {
523 for (j = 0; j < width * 2; j += 2) {
524 dev_info->backing_buffer
525 [base - dev_info->base16 + j] =
526 (char)(col >> 8);
527 dev_info->backing_buffer
528 [base - dev_info->base16 + j + 1] =
529 (char)(col);
530 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700531 }
Bernie Thompson59277b62009-11-24 15:52:21 -0800532
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700533 if (dev_info->bufend - bufptr < BUF_HIGH_WATER_MARK) {
534 ret = dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
535 bufptr = dev_info->buf;
536 }
537
538 rem = width;
539
540 while (rem) {
541
542 if (dev_info->bufend - bufptr < BUF_HIGH_WATER_MARK) {
543 ret =
544 dlfb_bulk_msg(dev_info,
545 bufptr - dev_info->buf);
546 bufptr = dev_info->buf;
547 }
548
549 *bufptr++ = 0xAF;
550 *bufptr++ = 0x69;
551
552 *bufptr++ = (char)(base >> 16);
553 *bufptr++ = (char)(base >> 8);
554 *bufptr++ = (char)(base);
555
556 if (rem > 255) {
557 *bufptr++ = 255;
558 *bufptr++ = 255;
559 rem -= 255;
560 base += 255 * 2;
561 } else {
562 *bufptr++ = rem;
563 *bufptr++ = rem;
564 base += rem * 2;
565 rem = 0;
566 }
567
568 *bufptr++ = (char)(col >> 8);
569 *bufptr++ = (char)(col);
570
571 }
572
573 base += (dev_info->info->var.xres * 2) - (width * 2);
574
575 }
576
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700577 if (bufptr > dev_info->buf)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700578 ret = dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700579
580 mutex_unlock(&dev_info->bulk_mutex);
581
582 return 1;
583}
584
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700585static void swapfb(struct dlfb_data *dev_info)
586{
587
588 int tmpbase;
589 char *bufptr;
590
591 mutex_lock(&dev_info->bulk_mutex);
592
593 tmpbase = dev_info->base16;
594
595 dev_info->base16 = dev_info->base16d;
596 dev_info->base16d = tmpbase;
597
598 bufptr = dev_info->buf;
599
600 bufptr = dlfb_set_register(bufptr, 0xFF, 0x00);
601
Bernie Thompson1d31a9e2010-02-15 06:45:43 -0800602 /* set addresses */
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700603 bufptr =
604 dlfb_set_register(bufptr, 0x20, (char)(dev_info->base16 >> 16));
605 bufptr = dlfb_set_register(bufptr, 0x21, (char)(dev_info->base16 >> 8));
606 bufptr = dlfb_set_register(bufptr, 0x22, (char)(dev_info->base16));
607
608 bufptr = dlfb_set_register(bufptr, 0xFF, 0x00);
609
610 dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
611
612 mutex_unlock(&dev_info->bulk_mutex);
613}
614
615static int copyfb(struct dlfb_data *dev_info)
616{
617 int base;
618 int source;
619 int rem;
620 int i, ret;
621
622 char *bufptr;
623
624 base = dev_info->base16d;
625
626 mutex_lock(&dev_info->bulk_mutex);
627
628 source = dev_info->base16;
629
630 bufptr = dev_info->buf;
631
632 for (i = 0; i < dev_info->info->var.yres; i++) {
633
634 if (dev_info->bufend - bufptr < BUF_HIGH_WATER_MARK) {
635 ret = dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
636 bufptr = dev_info->buf;
637 }
638
639 rem = dev_info->info->var.xres;
640
641 while (rem) {
642
643 if (dev_info->bufend - bufptr < BUF_HIGH_WATER_MARK) {
644 ret =
645 dlfb_bulk_msg(dev_info,
646 bufptr - dev_info->buf);
647 bufptr = dev_info->buf;
648
649 }
650
651 *bufptr++ = 0xAF;
652 *bufptr++ = 0x6A;
653
654 *bufptr++ = (char)(base >> 16);
655 *bufptr++ = (char)(base >> 8);
656 *bufptr++ = (char)(base);
657
658 if (rem > 255) {
659 *bufptr++ = 255;
660 *bufptr++ = (char)(source >> 16);
661 *bufptr++ = (char)(source >> 8);
662 *bufptr++ = (char)(source);
663
664 rem -= 255;
665 base += 255 * 2;
666 source += 255 * 2;
667
668 } else {
669 *bufptr++ = rem;
670 *bufptr++ = (char)(source >> 16);
671 *bufptr++ = (char)(source >> 8);
672 *bufptr++ = (char)(source);
673
674 base += rem * 2;
675 source += rem * 2;
676 rem = 0;
677 }
678 }
679 }
680
681 if (bufptr > dev_info->buf)
682 ret = dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
683
684 mutex_unlock(&dev_info->bulk_mutex);
685
686 return 1;
687
688}
689
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700690static int
691copyarea(struct dlfb_data *dev_info, int dx, int dy, int sx, int sy,
692 int width, int height)
693{
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700694 int base;
695 int source;
696 int rem;
697 int i, ret;
698
699 char *bufptr;
700
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700701 if (dx + width > dev_info->info->var.xres)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700702 return -EINVAL;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700703
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700704 if (dy + height > dev_info->info->var.yres)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700705 return -EINVAL;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700706
707 mutex_lock(&dev_info->bulk_mutex);
708
709 base =
710 dev_info->base16 + (dev_info->info->var.xres * 2 * dy) + (dx * 2);
711 source = (dev_info->info->var.xres * 2 * sy) + (sx * 2);
712
713 bufptr = dev_info->buf;
714
715 for (i = sy; i < sy + height; i++) {
716
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700717 memcpy(dev_info->backing_buffer + base - dev_info->base16,
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700718 dev_info->backing_buffer + source, width * 2);
719
720 if (dev_info->bufend - bufptr < BUF_HIGH_WATER_MARK) {
721 ret = dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
722 bufptr = dev_info->buf;
723 }
724
725 rem = width;
726
727 while (rem) {
728
729 if (dev_info->bufend - bufptr < BUF_HIGH_WATER_MARK) {
730 ret =
731 dlfb_bulk_msg(dev_info,
732 bufptr - dev_info->buf);
733 bufptr = dev_info->buf;
734 }
735
736 *bufptr++ = 0xAF;
737 *bufptr++ = 0x6A;
738
739 *bufptr++ = (char)(base >> 16);
740 *bufptr++ = (char)(base >> 8);
741 *bufptr++ = (char)(base);
742
743 if (rem > 255) {
744 *bufptr++ = 255;
745 *bufptr++ = (char)(source >> 16);
746 *bufptr++ = (char)(source >> 8);
747 *bufptr++ = (char)(source);
748
749 rem -= 255;
750 base += 255 * 2;
751 source += 255 * 2;
752
753 } else {
754 *bufptr++ = rem;
755 *bufptr++ = (char)(source >> 16);
756 *bufptr++ = (char)(source >> 8);
757 *bufptr++ = (char)(source);
758
759 base += rem * 2;
760 source += rem * 2;
761 rem = 0;
762 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700763 }
764
765 base += (dev_info->info->var.xres * 2) - (width * 2);
766 source += (dev_info->info->var.xres * 2) - (width * 2);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700767 }
768
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700769 if (bufptr > dev_info->buf)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700770 ret = dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700771
772 mutex_unlock(&dev_info->bulk_mutex);
773
774 return 1;
775}
776
Greg Kroah-Hartman4b6a4852009-06-03 14:47:21 -0700777static void dlfb_copyarea(struct fb_info *info, const struct fb_copyarea *area)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700778{
779
780 struct dlfb_data *dev = info->par;
781
782 copyarea(dev, area->dx, area->dy, area->sx, area->sy, area->width,
783 area->height);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700784}
785
Greg Kroah-Hartman4b6a4852009-06-03 14:47:21 -0700786static void dlfb_imageblit(struct fb_info *info, const struct fb_image *image)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700787{
788
789 int ret;
790 struct dlfb_data *dev = info->par;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700791 cfb_imageblit(info, image);
792 ret =
793 image_blit(dev, image->dx, image->dy, image->width, image->height,
794 info->screen_base);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700795}
796
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700797static void dlfb_fillrect(struct fb_info *info,
798 const struct fb_fillrect *region)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700799{
800
801 unsigned char red, green, blue;
802 struct dlfb_data *dev = info->par;
803
804 memcpy(&red, &region->color, 1);
805 memcpy(&green, &region->color + 1, 1);
806 memcpy(&blue, &region->color + 2, 1);
807 draw_rect(dev, region->dx, region->dy, region->width, region->height,
808 red, green, blue);
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700809 /* printk("FILL RECT %d %d !!!\n", region->dx, region->dy); */
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700810
811}
812
813static int dlfb_ioctl(struct fb_info *info, unsigned int cmd, unsigned long arg)
814{
815
816 struct dlfb_data *dev_info = info->par;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700817 struct dloarea *area = NULL;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700818
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700819 if (cmd == 0xAD) {
820 char *edid = (char *)arg;
821 dlfb_edid(dev_info);
822 if (copy_to_user(edid, dev_info->edid, 128)) {
823 return -EFAULT;
824 }
825 return 0;
826 }
827
828 if (cmd == 0xAA || cmd == 0xAB || cmd == 0xAC) {
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700829
830 area = (struct dloarea *)arg;
831
832 if (area->x < 0)
833 area->x = 0;
834
835 if (area->x > info->var.xres)
836 area->x = info->var.xres;
837
838 if (area->y < 0)
839 area->y = 0;
840
841 if (area->y > info->var.yres)
842 area->y = info->var.yres;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700843 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700844
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700845 if (cmd == 0xAA) {
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700846 image_blit(dev_info, area->x, area->y, area->w, area->h,
847 info->screen_base);
848 }
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700849 if (cmd == 0xAC) {
850 copyfb(dev_info);
851 image_blit(dev_info, area->x, area->y, area->w, area->h,
852 info->screen_base);
853 swapfb(dev_info);
854 } else if (cmd == 0xAB) {
855
856 if (area->x2 < 0)
857 area->x2 = 0;
858
859 if (area->y2 < 0)
860 area->y2 = 0;
861
862 copyarea(dev_info,
863 area->x2, area->y2, area->x, area->y, area->w,
864 area->h);
865 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700866 return 0;
867}
868
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700869/* taken from vesafb */
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700870static int
871dlfb_setcolreg(unsigned regno, unsigned red, unsigned green,
872 unsigned blue, unsigned transp, struct fb_info *info)
873{
874 int err = 0;
875
876 if (regno >= info->cmap.len)
877 return 1;
878
879 if (regno < 16) {
880 if (info->var.red.offset == 10) {
881 /* 1:5:5:5 */
882 ((u32 *) (info->pseudo_palette))[regno] =
883 ((red & 0xf800) >> 1) |
884 ((green & 0xf800) >> 6) | ((blue & 0xf800) >> 11);
885 } else {
886 /* 0:5:6:5 */
887 ((u32 *) (info->pseudo_palette))[regno] =
888 ((red & 0xf800)) |
889 ((green & 0xfc00) >> 5) | ((blue & 0xf800) >> 11);
890 }
891 }
892
893 return err;
894}
895
896static int dlfb_release(struct fb_info *info, int user)
897{
898 struct dlfb_data *dev_info = info->par;
899 image_blit(dev_info, 0, 0, info->var.xres, info->var.yres,
900 info->screen_base);
901 return 0;
902}
903
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700904static int dlfb_blank(int blank_mode, struct fb_info *info)
905{
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700906 struct dlfb_data *dev_info = info->par;
907 char *bufptr = dev_info->buf;
908
909 bufptr = dlfb_set_register(bufptr, 0xFF, 0x00);
910 if (blank_mode != FB_BLANK_UNBLANK) {
911 bufptr = dlfb_set_register(bufptr, 0x1F, 0x01);
912 } else {
913 bufptr = dlfb_set_register(bufptr, 0x1F, 0x00);
914 }
915 bufptr = dlfb_set_register(bufptr, 0xFF, 0xFF);
916
917 dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
918
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700919 return 0;
920}
921
922static struct fb_ops dlfb_ops = {
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700923 .fb_setcolreg = dlfb_setcolreg,
924 .fb_fillrect = dlfb_fillrect,
925 .fb_copyarea = dlfb_copyarea,
926 .fb_imageblit = dlfb_imageblit,
927 .fb_mmap = dlfb_mmap,
928 .fb_ioctl = dlfb_ioctl,
929 .fb_release = dlfb_release,
930 .fb_blank = dlfb_blank,
931};
932
Bernie Thompsoncc403dc2010-02-15 06:45:49 -0800933/*
934 * This is necessary before we can communicate with the display controller.
935 */
936static int dlfb_select_std_channel(struct dlfb_data *dev)
937{
938 int ret;
939 u8 set_def_chn[] = { 0x57, 0xCD, 0xDC, 0xA7,
940 0x1C, 0x88, 0x5E, 0x15,
941 0x60, 0xFE, 0xC6, 0x97,
942 0x16, 0x3D, 0x47, 0xF2 };
943
944 ret = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
945 NR_USB_REQUEST_CHANNEL,
946 (USB_DIR_OUT | USB_TYPE_VENDOR), 0, 0,
947 set_def_chn, sizeof(set_def_chn), USB_CTRL_SET_TIMEOUT);
948 return ret;
949}
950
Bernie Thompson59277b62009-11-24 15:52:21 -0800951static int dlfb_probe(struct usb_interface *interface,
952 const struct usb_device_id *id)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700953{
Bernie Thompson59277b62009-11-24 15:52:21 -0800954 struct device *mydev;
955 struct usb_device *usbdev;
956 struct dlfb_data *dev;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700957 struct fb_info *info;
Bernie Thompson59277b62009-11-24 15:52:21 -0800958 int videomemorysize;
959 unsigned char *videomemory;
960 int retval = -ENOMEM;
961 struct fb_var_screeninfo *var;
962 struct fb_bitfield red = { 11, 5, 0 };
963 struct fb_bitfield green = { 5, 6, 0 };
964 struct fb_bitfield blue = { 0, 5, 0 };
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700965
Bernie Thompson59277b62009-11-24 15:52:21 -0800966 usbdev = usb_get_dev(interface_to_usbdev(interface));
967 mydev = &usbdev->dev;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700968
Bernie Thompson59277b62009-11-24 15:52:21 -0800969 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
970 if (dev == NULL) {
971 dev_err(mydev, "failed alloc of dev struct\n");
972 goto err_devalloc;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700973 }
974
Bernie Thompson59277b62009-11-24 15:52:21 -0800975 mutex_init(&dev->bulk_mutex);
976 dev->udev = usbdev;
977 dev->interface = interface;
978 usb_set_intfdata(interface, dev);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700979
Bernie Thompson59277b62009-11-24 15:52:21 -0800980 dev_info(mydev, "dlfb_probe: setting up DisplayLink device\n");
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700981
Bernie Thompson59277b62009-11-24 15:52:21 -0800982 /*
983 * TODO: replace single 64K buffer with buffer list
984 * and async dispatch
985 */
986 dev->buf = kmalloc(BUF_SIZE, GFP_KERNEL);
987 if (dev->buf == NULL) {
988 dev_err(mydev, "unable to allocate memory for dlfb commands\n");
989 goto err_usballoc;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700990 }
Bernie Thompson59277b62009-11-24 15:52:21 -0800991 dev->bufend = dev->buf + BUF_SIZE;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700992
Bernie Thompson59277b62009-11-24 15:52:21 -0800993 dev->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
994 usb_fill_bulk_urb(dev->tx_urb, dev->udev,
995 usb_sndbulkpipe(dev->udev, 1), dev->buf, 0,
996 dlfb_bulk_callback, dev);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700997
Bernie Thompson59277b62009-11-24 15:52:21 -0800998 /* allocates framebuffer driver structure, not framebuffer memory */
999 info = framebuffer_alloc(0, mydev);
1000 if (!info)
1001 goto err_fballoc;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001002
Bernie Thompson59277b62009-11-24 15:52:21 -08001003 dev->info = info;
1004 info->par = dev;
1005 info->pseudo_palette = dev->pseudo_palette;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001006
Bernie Thompson59277b62009-11-24 15:52:21 -08001007 var = &info->var;
1008 retval = dlfb_get_var_from_edid(dev, var);
1009 if (retval) {
1010 /* had a problem getting edid. so fallback to 640x480 */
1011 dev_err(mydev, "Problem %d with EDID.\n", retval);
1012 var->xres = 640;
1013 var->yres = 480;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001014 }
1015
Bernie Thompson59277b62009-11-24 15:52:21 -08001016 /*
1017 * ok, now that we've got the size info, we can alloc our framebuffer.
1018 * We are using 16bpp.
1019 */
1020 info->var.bits_per_pixel = 16;
1021 info->fix = dlfb_fix;
1022 info->fix.line_length = var->xres * (var->bits_per_pixel / 8);
1023 videomemorysize = info->fix.line_length * var->yres;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001024
Bernie Thompson59277b62009-11-24 15:52:21 -08001025 /*
1026 * The big chunk of system memory we use as a virtual framebuffer.
1027 * Pages don't need to be set RESERVED (non-swap) immediately on 2.6
1028 * remap_pfn_page() syscall in our mmap and/or defio will handle.
1029 */
1030 videomemory = vmalloc(videomemorysize);
1031 if (!videomemory)
1032 goto err_vidmem;
1033 memset(videomemory, 0, videomemorysize);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001034
Bernie Thompson59277b62009-11-24 15:52:21 -08001035 info->screen_base = videomemory;
1036 info->fix.smem_len = PAGE_ALIGN(videomemorysize);
1037 info->fix.smem_start = (unsigned long) videomemory;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001038 info->flags =
1039 FBINFO_DEFAULT | FBINFO_READS_FAST | FBINFO_HWACCEL_IMAGEBLIT |
1040 FBINFO_HWACCEL_COPYAREA | FBINFO_HWACCEL_FILLRECT;
Bernie Thompson59277b62009-11-24 15:52:21 -08001041
1042 /*
1043 * Second framebuffer copy, mirroring the state of the framebuffer
1044 * on the physical USB device. We can function without this.
1045 * But with imperfect damage info we may end up sending pixels over USB
1046 * that were, in fact, unchanged -- wasting limited USB bandwidth
1047 */
1048 dev->backing_buffer = vmalloc(dev->screen_size);
1049 if (!dev->backing_buffer)
1050 dev_info(mydev, "No backing buffer allocated!\n");
1051
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001052 info->fbops = &dlfb_ops;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001053
Bernie Thompson59277b62009-11-24 15:52:21 -08001054 var->vmode = FB_VMODE_NONINTERLACED;
1055 var->red = red;
1056 var->green = green;
1057 var->blue = blue;
1058
1059 /*
1060 * TODO: Enable FB_CONFIG_DEFIO support
1061
1062 info->fbdefio = &dlfb_defio;
1063 fb_deferred_io_init(info);
1064
1065 */
1066
1067 retval = fb_alloc_cmap(&info->cmap, 256, 0);
1068 if (retval < 0) {
1069 dev_err(mydev, "Failed to allocate colormap\n");
1070 goto err_cmap;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001071 }
1072
Bernie Thompson59277b62009-11-24 15:52:21 -08001073 dlfb_select_std_channel(dev);
1074 dlfb_set_video_mode(dev, var);
1075 /* TODO: dlfb_dpy_update(dev); */
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001076
Bernie Thompson59277b62009-11-24 15:52:21 -08001077 retval = register_framebuffer(info);
1078 if (retval < 0)
1079 goto err_regfb;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001080
Bernie Thompson59277b62009-11-24 15:52:21 -08001081 /* paint "successful" green screen */
1082 draw_rect(dev, 0, 0, dev->info->var.xres,
1083 dev->info->var.yres, 0x30, 0xff, 0x30);
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -07001084
Bernie Thompson59277b62009-11-24 15:52:21 -08001085 dev_info(mydev, "DisplayLink USB device %d now attached, "
1086 "using %dK of memory\n", info->node,
1087 ((dev->backing_buffer) ?
1088 videomemorysize * 2 : videomemorysize) >> 10);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001089 return 0;
1090
Bernie Thompson59277b62009-11-24 15:52:21 -08001091err_regfb:
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001092 fb_dealloc_cmap(&info->cmap);
Bernie Thompson59277b62009-11-24 15:52:21 -08001093err_cmap:
1094 /* TODO: fb_deferred_io_cleanup(info); */
1095 vfree(videomemory);
1096err_vidmem:
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001097 framebuffer_release(info);
Bernie Thompson59277b62009-11-24 15:52:21 -08001098err_fballoc:
1099 kfree(dev->buf);
1100err_usballoc:
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001101 usb_set_intfdata(interface, NULL);
Bernie Thompson59277b62009-11-24 15:52:21 -08001102 usb_put_dev(dev->udev);
1103 kfree(dev);
1104err_devalloc:
1105 return retval;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001106}
1107
1108static void dlfb_disconnect(struct usb_interface *interface)
1109{
Bernie Thompson59277b62009-11-24 15:52:21 -08001110 struct dlfb_data *dev;
1111 struct fb_info *info;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001112
Bernie Thompson59277b62009-11-24 15:52:21 -08001113 dev = usb_get_intfdata(interface);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001114 usb_set_intfdata(interface, NULL);
Bernie Thompson59277b62009-11-24 15:52:21 -08001115 usb_put_dev(dev->udev);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001116
Bernie Thompson59277b62009-11-24 15:52:21 -08001117 /*
1118 * TODO: since, upon usb disconnect(), usb will cancel in-flight urbs
1119 * and error out any new ones, look at eliminating need for mutex
1120 */
1121 mutex_lock(&dev->bulk_mutex);
1122 dev->interface = NULL;
1123 info = dev->info;
1124 mutex_unlock(&dev->bulk_mutex);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001125
Bernie Thompson59277b62009-11-24 15:52:21 -08001126 if (info) {
1127 dev_info(&interface->dev, "Detaching DisplayLink device %d.\n",
1128 info->node);
1129 unregister_framebuffer(info);
1130 fb_dealloc_cmap(&info->cmap);
1131 /* TODO: fb_deferred_io_cleanup(info); */
1132 fb_dealloc_cmap(&info->cmap);
1133 vfree((void __force *)info->screen_base);
1134 framebuffer_release(info);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001135 }
1136
Bernie Thompson59277b62009-11-24 15:52:21 -08001137 if (dev->backing_buffer)
1138 vfree(dev->backing_buffer);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001139
Bernie Thompson59277b62009-11-24 15:52:21 -08001140 kfree(dev);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001141}
1142
1143static struct usb_driver dlfb_driver = {
1144 .name = "udlfb",
1145 .probe = dlfb_probe,
1146 .disconnect = dlfb_disconnect,
1147 .id_table = id_table,
1148};
1149
1150static int __init dlfb_init(void)
1151{
1152 int res;
1153
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001154 res = usb_register(&dlfb_driver);
1155 if (res)
1156 err("usb_register failed. Error number %d", res);
1157
1158 printk("VMODES initialized\n");
1159
1160 return res;
1161}
1162
1163static void __exit dlfb_exit(void)
1164{
1165 usb_deregister(&dlfb_driver);
1166}
1167
1168module_init(dlfb_init);
1169module_exit(dlfb_exit);
1170
Bernie Thompson59277b62009-11-24 15:52:21 -08001171MODULE_AUTHOR("Roberto De Ioris <roberto@unbit.it>, "
1172 "Jaya Kumar <jayakumar.lkml@gmail.com>");
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001173MODULE_DESCRIPTION(DRIVER_VERSION);
1174MODULE_LICENSE("GPL");