blob: d2adfe416a0c00e260487475eaf4fc6ea2cfbf76 [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 Thompson59277b62009-11-24 15:52:21 -080042#define NR_USB_REQUEST_I2C_SUB_IO 0x02
43#define NR_USB_REQUEST_CHANNEL 0x12
44
45/*
46 * Inserts a specific DisplayLink controller command into the provided
47 * buffer.
48 */
49static char *insert_command(char *buf, u8 reg, u8 val)
Roberto De Ioris88e58b12009-06-03 14:03:06 -070050{
Bernie Thompson1d31a9e2010-02-15 06:45:43 -080051 *buf++ = 0xAF;
52 *buf++ = 0x20;
53 *buf++ = reg;
54 *buf++ = val;
55 return buf;
Roberto De Ioris88e58b12009-06-03 14:03:06 -070056}
57
Bernie Thompson59277b62009-11-24 15:52:21 -080058static char *insert_vidreg_lock(char *buf)
Roberto De Ioris88e58b12009-06-03 14:03:06 -070059{
Bernie Thompson1d31a9e2010-02-15 06:45:43 -080060 return insert_command(buf, 0xFF, 0x00);
Bernie Thompson59277b62009-11-24 15:52:21 -080061}
Roberto De Ioris88e58b12009-06-03 14:03:06 -070062
Bernie Thompson59277b62009-11-24 15:52:21 -080063static char *insert_vidreg_unlock(char *buf)
64{
Bernie Thompson1d31a9e2010-02-15 06:45:43 -080065 return insert_command(buf, 0xFF, 0xFF);
Bernie Thompson59277b62009-11-24 15:52:21 -080066}
Roberto De Ioris88e58b12009-06-03 14:03:06 -070067
Bernie Thompson59277b62009-11-24 15:52:21 -080068/*
69 * Once you send this command, the DisplayLink framebuffer gets driven to the
70 * display.
71 */
72static char *insert_enable_hvsync(char *buf)
73{
Bernie Thompson1d31a9e2010-02-15 06:45:43 -080074 return insert_command(buf, 0x1F, 0x00);
Bernie Thompson59277b62009-11-24 15:52:21 -080075}
76
77static char *insert_set_color_depth(char *buf, u8 selection)
78{
Bernie Thompson1d31a9e2010-02-15 06:45:43 -080079 return insert_command(buf, 0x00, selection);
Bernie Thompson59277b62009-11-24 15:52:21 -080080}
81
82static char *insert_set_base16bpp(char *wrptr, u32 base)
83{
Bernie Thompson1d31a9e2010-02-15 06:45:43 -080084 /* the base pointer is 16 bits wide, 0x20 is hi byte. */
85 wrptr = insert_command(wrptr, 0x20, base >> 16);
86 wrptr = insert_command(wrptr, 0x21, base >> 8);
87 return insert_command(wrptr, 0x22, base);
Bernie Thompson59277b62009-11-24 15:52:21 -080088}
89
90static char *insert_set_base8bpp(char *wrptr, u32 base)
91{
Bernie Thompson1d31a9e2010-02-15 06:45:43 -080092 wrptr = insert_command(wrptr, 0x26, base >> 16);
93 wrptr = insert_command(wrptr, 0x27, base >> 8);
94 return insert_command(wrptr, 0x28, base);
Bernie Thompson59277b62009-11-24 15:52:21 -080095}
96
97static char *insert_command_16(char *wrptr, u8 reg, u16 value)
98{
Bernie Thompson1d31a9e2010-02-15 06:45:43 -080099 wrptr = insert_command(wrptr, reg, value >> 8);
100 return insert_command(wrptr, reg+1, value);
Bernie Thompson59277b62009-11-24 15:52:21 -0800101}
102
103/*
104 * This is kind of weird because the controller takes some
105 * register values in a different byte order than other registers.
106 */
107static char *insert_command_16be(char *wrptr, u8 reg, u16 value)
108{
Bernie Thompson1d31a9e2010-02-15 06:45:43 -0800109 wrptr = insert_command(wrptr, reg, value);
110 return insert_command(wrptr, reg+1, value >> 8);
Bernie Thompson59277b62009-11-24 15:52:21 -0800111}
112
113/*
114 * LFSR is linear feedback shift register. The reason we have this is
115 * because the display controller needs to minimize the clock depth of
116 * various counters used in the display path. So this code reverses the
117 * provided value into the lfsr16 value by counting backwards to get
118 * the value that needs to be set in the hardware comparator to get the
119 * same actual count. This makes sense once you read above a couple of
120 * times and think about it from a hardware perspective.
121 */
122static u16 lfsr16(u16 actual_count)
123{
124 u32 lv = 0xFFFF; /* This is the lfsr value that the hw starts with */
125
126 while (actual_count--) {
127 lv = ((lv << 1) |
128 (((lv >> 15) ^ (lv >> 4) ^ (lv >> 2) ^ (lv >> 1)) & 1))
129 & 0xFFFF;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700130 }
Bernie Thompson59277b62009-11-24 15:52:21 -0800131
132 return (u16) lv;
133}
134
135/*
136 * This does LFSR conversion on the value that is to be written.
137 * See LFSR explanation above for more detail.
138 */
139static char *insert_command_lfsr16(char *wrptr, u8 reg, u16 value)
140{
141 return insert_command_16(wrptr, reg, lfsr16(value));
142}
143
144/*
145 * This takes a standard fbdev screeninfo struct and all of its monitor mode
146 * details and converts them into the DisplayLink equivalent register commands.
147 */
148static char *insert_set_vid_cmds(char *wrptr, struct fb_var_screeninfo *var)
149{
150 u16 xds, yds;
151 u16 xde, yde;
152 u16 yec;
153
154
155 /* x display start */
156 xds = var->left_margin + var->hsync_len;
157 wrptr = insert_command_lfsr16(wrptr, 0x01, xds);
158 /* x display end */
159 xde = xds + var->xres;
160 wrptr = insert_command_lfsr16(wrptr, 0x03, xde);
161
162 /* y display start */
163 yds = var->upper_margin + var->vsync_len;
164 wrptr = insert_command_lfsr16(wrptr, 0x05, yds);
165 /* y display end */
166 yde = yds + var->yres;
167 wrptr = insert_command_lfsr16(wrptr, 0x07, yde);
168
169 /* x end count is active + blanking - 1 */
170 wrptr = insert_command_lfsr16(wrptr, 0x09, xde + var->right_margin - 1);
171
172 /* libdlo hardcodes hsync start to 1 */
173 wrptr = insert_command_lfsr16(wrptr, 0x0B, 1);
174
175 /* hsync end is width of sync pulse + 1 */
176 wrptr = insert_command_lfsr16(wrptr, 0x0D, var->hsync_len + 1);
177
178 /* hpixels is active pixels */
179 wrptr = insert_command_16(wrptr, 0x0F, var->xres);
180
181 /* yendcount is vertical active + vertical blanking */
182 yec = var->yres + var->upper_margin + var->lower_margin +
183 var->vsync_len;
184 wrptr = insert_command_lfsr16(wrptr, 0x11, yec);
185
186 /* libdlo hardcodes vsync start to 0 */
187 wrptr = insert_command_lfsr16(wrptr, 0x13, 0);
188
189 /* vsync end is width of vsync pulse */
190 wrptr = insert_command_lfsr16(wrptr, 0x15, var->vsync_len);
191
192 /* vpixels is active pixels */
193 wrptr = insert_command_16(wrptr, 0x17, var->yres);
194
195 /* convert picoseconds to 5kHz multiple for pclk5k = x * 1E12/5k */
196 wrptr = insert_command_16be(wrptr, 0x1B, 200*1000*1000/var->pixclock);
197
198 return wrptr;
199}
200
201/*
202 * This takes a standard fbdev screeninfo struct that was fetched or prepared
203 * and then generates the appropriate command sequence that then drives the
204 * display controller.
205 */
206static int dlfb_set_video_mode(struct dlfb_data *dev,
207 struct fb_var_screeninfo *var)
208{
209 char *buf;
210 char *wrptr;
211 int retval = 0;
212 int writesize;
213
214 buf = dev->buf;
215
216 /*
217 * This first section has to do with setting the base address on the
218 * controller * associated with the display. There are 2 base
219 * pointers, currently, we only * use the 16 bpp segment.
220 */
221 wrptr = insert_vidreg_lock(buf);
222 wrptr = insert_set_color_depth(wrptr, 0x00);
223 /* set base for 16bpp segment to 0 */
224 wrptr = insert_set_base16bpp(wrptr, 0);
225 /* set base for 8bpp segment to end of fb */
226 wrptr = insert_set_base8bpp(wrptr, dev->info->fix.smem_len);
227
228 wrptr = insert_set_vid_cmds(wrptr, var);
229 wrptr = insert_enable_hvsync(wrptr);
230 wrptr = insert_vidreg_unlock(wrptr);
231
232 writesize = wrptr - buf;
233
234 mutex_lock(&dev->bulk_mutex);
235 if (!dev->interface) { /* disconnect() was called */
236 mutex_unlock(&dev->bulk_mutex);
237 retval = -ENODEV;
238 goto error;
239 }
240
241 retval = dlfb_bulk_msg(dev, writesize);
242 mutex_unlock(&dev->bulk_mutex);
243 if (retval) {
244 dev_err(&dev->udev->dev, "Problem %d with submit write bulk.\n",
245 retval);
246 goto error;
247 }
248
249 return 0;
250
251error:
252 return retval;
253}
254
255/*
256 * This is necessary before we can communicate with the display controller.
257 */
258static int dlfb_select_std_channel(struct dlfb_data *dev)
259{
260 int ret;
261 u8 set_def_chn[] = { 0x57, 0xCD, 0xDC, 0xA7,
262 0x1C, 0x88, 0x5E, 0x15,
263 0x60, 0xFE, 0xC6, 0x97,
264 0x16, 0x3D, 0x47, 0xF2 };
265
266 ret = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
267 NR_USB_REQUEST_CHANNEL,
268 (USB_DIR_OUT | USB_TYPE_VENDOR), 0, 0,
269 set_def_chn, sizeof(set_def_chn), USB_CTRL_SET_TIMEOUT);
270 return ret;
271}
272
273
274/*
275 * Query EDID from the handware, then hand it off to fbdev's edid parse
276 * routine which should give us back a filled in screeninfo structure.
277 */
278static int dlfb_get_var_from_edid(struct dlfb_data *dev,
279 struct fb_var_screeninfo *var)
280{
281 int ret;
282
283 dlfb_edid(dev);
284 ret = fb_parse_edid(dev->edid, var);
285
286 return ret;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700287}
288
289static int dlfb_mmap(struct fb_info *info, struct vm_area_struct *vma)
290{
291 unsigned long start = vma->vm_start;
292 unsigned long size = vma->vm_end - vma->vm_start;
293 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
294 unsigned long page, pos;
295
296 printk("MMAP: %lu %u\n", offset + size, info->fix.smem_len);
297
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700298 if (offset + size > info->fix.smem_len)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700299 return -EINVAL;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700300
301 pos = (unsigned long)info->fix.smem_start + offset;
302
303 while (size > 0) {
304 page = vmalloc_to_pfn((void *)pos);
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700305 if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED))
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700306 return -EAGAIN;
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700307
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700308 start += PAGE_SIZE;
309 pos += PAGE_SIZE;
310 if (size > PAGE_SIZE)
311 size -= PAGE_SIZE;
312 else
313 size = 0;
314 }
315
316 vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
317 return 0;
318
319}
320
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700321/* ioctl structure */
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700322struct dloarea {
323 int x, y;
324 int w, h;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700325 int x2, y2;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700326};
327
328/*
Bernie Thompson59277b62009-11-24 15:52:21 -0800329 * There are many DisplayLink-based products, all with unique PIDs. We are able
330 * to support all volume ones (circa 2009) with a single driver, so we match
331 * globally on VID. TODO: Probe() needs to detect when we might be running
332 * "future" chips, and bail on those, so a compatible driver can match.
333 */
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700334static struct usb_device_id id_table[] = {
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700335 {.idVendor = 0x17e9, .match_flags = USB_DEVICE_ID_MATCH_VENDOR,},
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700336 {},
337};
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700338MODULE_DEVICE_TABLE(usb, id_table);
339
340static struct usb_driver dlfb_driver;
341
Bernie Thompson1d31a9e2010-02-15 06:45:43 -0800342/* thanks to Henrik Bjerregaard Pedersen for this function */
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700343static char *rle_compress16(uint16_t * src, char *dst, int rem)
344{
345
346 int rl;
347 uint16_t pix0;
348 char *end_if_raw = dst + 6 + 2 * rem;
349
Bernie Thompson1d31a9e2010-02-15 06:45:43 -0800350 dst += 6; /* header will be filled in if RLE is worth it */
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700351
352 while (rem && dst < end_if_raw) {
353 char *start = (char *)src;
354
355 pix0 = *src++;
356 rl = 1;
357 rem--;
358 while (rem && *src == pix0)
359 rem--, rl++, src++;
360 *dst++ = rl;
361 *dst++ = start[1];
362 *dst++ = start[0];
363 }
364
365 return dst;
366}
367
368/*
Bernie Thompson1d31a9e2010-02-15 06:45:43 -0800369Thanks to Henrik Bjerregaard Pedersen for rle implementation
370and code refactoring. Next step is huffman compression.
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700371*/
372
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700373static int
374image_blit(struct dlfb_data *dev_info, int x, int y, int width, int height,
375 char *data)
376{
377
378 int i, j, base;
379 int rem = width;
380 int ret;
381
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700382 int firstdiff, thistime;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700383
384 char *bufptr;
385
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700386 if (x + width > dev_info->info->var.xres)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700387 return -EINVAL;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700388
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700389 if (y + height > dev_info->info->var.yres)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700390 return -EINVAL;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700391
392 mutex_lock(&dev_info->bulk_mutex);
393
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700394 base =
395 dev_info->base16 + ((dev_info->info->var.xres * 2 * y) + (x * 2));
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700396
397 data += (dev_info->info->var.xres * 2 * y) + (x * 2);
398
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700399 /* printk("IMAGE_BLIT\n"); */
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700400
401 bufptr = dev_info->buf;
402
403 for (i = y; i < y + height; i++) {
404
405 if (dev_info->bufend - bufptr < BUF_HIGH_WATER_MARK) {
406 ret = dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
407 bufptr = dev_info->buf;
408 }
409
410 rem = width;
411
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700412 /* printk("WRITING LINE %d\n", i); */
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700413
414 while (rem) {
415
416 if (dev_info->bufend - bufptr < BUF_HIGH_WATER_MARK) {
417 ret =
418 dlfb_bulk_msg(dev_info,
419 bufptr - dev_info->buf);
420 bufptr = dev_info->buf;
421 }
Bernie Thompson1d31a9e2010-02-15 06:45:43 -0800422 /* number of pixels to consider this time */
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700423 thistime = rem;
424 if (thistime > 255)
425 thistime = 255;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700426
Bernie Thompson59277b62009-11-24 15:52:21 -0800427 if (dev_info->backing_buffer) {
428 /* find first pixel that has changed */
429 firstdiff = -1;
430 for (j = 0; j < thistime * 2; j++) {
431 if (dev_info->backing_buffer
432 [base - dev_info->base16 + j]
433 != data[j]) {
434 firstdiff = j / 2;
435 break;
436 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700437 }
Bernie Thompson59277b62009-11-24 15:52:21 -0800438
439 } else {
440 firstdiff = 0;
441
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700442 }
443
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700444 if (firstdiff >= 0) {
445 char *end_of_rle;
446
447 end_of_rle =
448 rle_compress16((uint16_t *) (data +
449 firstdiff * 2),
450 bufptr,
451 thistime - firstdiff);
452
453 if (end_of_rle <
454 bufptr + 6 + 2 * (thistime - firstdiff)) {
455 bufptr[0] = 0xAF;
456 bufptr[1] = 0x69;
457
458 bufptr[2] =
459 (char)((base +
460 firstdiff * 2) >> 16);
461 bufptr[3] =
462 (char)((base + firstdiff * 2) >> 8);
463 bufptr[4] =
464 (char)(base + firstdiff * 2);
465 bufptr[5] = thistime - firstdiff;
466
467 bufptr = end_of_rle;
468
469 } else {
Bernie Thompson1d31a9e2010-02-15 06:45:43 -0800470 /* fallback to raw (or other?) */
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700471 *bufptr++ = 0xAF;
472 *bufptr++ = 0x68;
473
474 *bufptr++ =
475 (char)((base +
476 firstdiff * 2) >> 16);
477 *bufptr++ =
478 (char)((base + firstdiff * 2) >> 8);
479 *bufptr++ =
480 (char)(base + firstdiff * 2);
481 *bufptr++ = thistime - firstdiff;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700482 for (j = firstdiff * 2;
483 j < thistime * 2; j += 2) {
484 *bufptr++ = data[j + 1];
485 *bufptr++ = data[j];
486 }
487 }
488 }
489
490 base += thistime * 2;
491 data += thistime * 2;
492 rem -= thistime;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700493 }
494
Bernie Thompson59277b62009-11-24 15:52:21 -0800495 if (dev_info->backing_buffer)
496 memcpy(dev_info->backing_buffer +
497 (base - dev_info->base16) -
498 (width * 2), data - (width * 2), width * 2);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700499
500 base += (dev_info->info->var.xres * 2) - (width * 2);
501 data += (dev_info->info->var.xres * 2) - (width * 2);
502
503 }
504
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700505 if (bufptr > dev_info->buf) {
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700506 ret = dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700507 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700508
509 mutex_unlock(&dev_info->bulk_mutex);
510
511 return base;
512
513}
514
515static int
516draw_rect(struct dlfb_data *dev_info, int x, int y, int width, int height,
517 unsigned char red, unsigned char green, unsigned char blue)
518{
519
520 int i, j, base;
521 int ret;
522 unsigned short col =
523 (((((red) & 0xF8) | ((green) >> 5)) & 0xFF) << 8) +
524 (((((green) & 0x1C) << 3) | ((blue) >> 3)) & 0xFF);
525 int rem = width;
526
527 char *bufptr;
528
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700529 if (x + width > dev_info->info->var.xres)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700530 return -EINVAL;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700531
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700532 if (y + height > dev_info->info->var.yres)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700533 return -EINVAL;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700534
535 mutex_lock(&dev_info->bulk_mutex);
536
537 base = dev_info->base16 + (dev_info->info->var.xres * 2 * y) + (x * 2);
538
539 bufptr = dev_info->buf;
540
541 for (i = y; i < y + height; i++) {
542
Bernie Thompson59277b62009-11-24 15:52:21 -0800543 if (dev_info->backing_buffer) {
544 for (j = 0; j < width * 2; j += 2) {
545 dev_info->backing_buffer
546 [base - dev_info->base16 + j] =
547 (char)(col >> 8);
548 dev_info->backing_buffer
549 [base - dev_info->base16 + j + 1] =
550 (char)(col);
551 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700552 }
Bernie Thompson59277b62009-11-24 15:52:21 -0800553
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700554 if (dev_info->bufend - bufptr < BUF_HIGH_WATER_MARK) {
555 ret = dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
556 bufptr = dev_info->buf;
557 }
558
559 rem = width;
560
561 while (rem) {
562
563 if (dev_info->bufend - bufptr < BUF_HIGH_WATER_MARK) {
564 ret =
565 dlfb_bulk_msg(dev_info,
566 bufptr - dev_info->buf);
567 bufptr = dev_info->buf;
568 }
569
570 *bufptr++ = 0xAF;
571 *bufptr++ = 0x69;
572
573 *bufptr++ = (char)(base >> 16);
574 *bufptr++ = (char)(base >> 8);
575 *bufptr++ = (char)(base);
576
577 if (rem > 255) {
578 *bufptr++ = 255;
579 *bufptr++ = 255;
580 rem -= 255;
581 base += 255 * 2;
582 } else {
583 *bufptr++ = rem;
584 *bufptr++ = rem;
585 base += rem * 2;
586 rem = 0;
587 }
588
589 *bufptr++ = (char)(col >> 8);
590 *bufptr++ = (char)(col);
591
592 }
593
594 base += (dev_info->info->var.xres * 2) - (width * 2);
595
596 }
597
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700598 if (bufptr > dev_info->buf)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700599 ret = dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700600
601 mutex_unlock(&dev_info->bulk_mutex);
602
603 return 1;
604}
605
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700606static void swapfb(struct dlfb_data *dev_info)
607{
608
609 int tmpbase;
610 char *bufptr;
611
612 mutex_lock(&dev_info->bulk_mutex);
613
614 tmpbase = dev_info->base16;
615
616 dev_info->base16 = dev_info->base16d;
617 dev_info->base16d = tmpbase;
618
619 bufptr = dev_info->buf;
620
621 bufptr = dlfb_set_register(bufptr, 0xFF, 0x00);
622
Bernie Thompson1d31a9e2010-02-15 06:45:43 -0800623 /* set addresses */
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700624 bufptr =
625 dlfb_set_register(bufptr, 0x20, (char)(dev_info->base16 >> 16));
626 bufptr = dlfb_set_register(bufptr, 0x21, (char)(dev_info->base16 >> 8));
627 bufptr = dlfb_set_register(bufptr, 0x22, (char)(dev_info->base16));
628
629 bufptr = dlfb_set_register(bufptr, 0xFF, 0x00);
630
631 dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
632
633 mutex_unlock(&dev_info->bulk_mutex);
634}
635
636static int copyfb(struct dlfb_data *dev_info)
637{
638 int base;
639 int source;
640 int rem;
641 int i, ret;
642
643 char *bufptr;
644
645 base = dev_info->base16d;
646
647 mutex_lock(&dev_info->bulk_mutex);
648
649 source = dev_info->base16;
650
651 bufptr = dev_info->buf;
652
653 for (i = 0; i < dev_info->info->var.yres; i++) {
654
655 if (dev_info->bufend - bufptr < BUF_HIGH_WATER_MARK) {
656 ret = dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
657 bufptr = dev_info->buf;
658 }
659
660 rem = dev_info->info->var.xres;
661
662 while (rem) {
663
664 if (dev_info->bufend - bufptr < BUF_HIGH_WATER_MARK) {
665 ret =
666 dlfb_bulk_msg(dev_info,
667 bufptr - dev_info->buf);
668 bufptr = dev_info->buf;
669
670 }
671
672 *bufptr++ = 0xAF;
673 *bufptr++ = 0x6A;
674
675 *bufptr++ = (char)(base >> 16);
676 *bufptr++ = (char)(base >> 8);
677 *bufptr++ = (char)(base);
678
679 if (rem > 255) {
680 *bufptr++ = 255;
681 *bufptr++ = (char)(source >> 16);
682 *bufptr++ = (char)(source >> 8);
683 *bufptr++ = (char)(source);
684
685 rem -= 255;
686 base += 255 * 2;
687 source += 255 * 2;
688
689 } else {
690 *bufptr++ = rem;
691 *bufptr++ = (char)(source >> 16);
692 *bufptr++ = (char)(source >> 8);
693 *bufptr++ = (char)(source);
694
695 base += rem * 2;
696 source += rem * 2;
697 rem = 0;
698 }
699 }
700 }
701
702 if (bufptr > dev_info->buf)
703 ret = dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
704
705 mutex_unlock(&dev_info->bulk_mutex);
706
707 return 1;
708
709}
710
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700711static int
712copyarea(struct dlfb_data *dev_info, int dx, int dy, int sx, int sy,
713 int width, int height)
714{
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700715 int base;
716 int source;
717 int rem;
718 int i, ret;
719
720 char *bufptr;
721
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700722 if (dx + width > dev_info->info->var.xres)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700723 return -EINVAL;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700724
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700725 if (dy + height > dev_info->info->var.yres)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700726 return -EINVAL;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700727
728 mutex_lock(&dev_info->bulk_mutex);
729
730 base =
731 dev_info->base16 + (dev_info->info->var.xres * 2 * dy) + (dx * 2);
732 source = (dev_info->info->var.xres * 2 * sy) + (sx * 2);
733
734 bufptr = dev_info->buf;
735
736 for (i = sy; i < sy + height; i++) {
737
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700738 memcpy(dev_info->backing_buffer + base - dev_info->base16,
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700739 dev_info->backing_buffer + source, width * 2);
740
741 if (dev_info->bufend - bufptr < BUF_HIGH_WATER_MARK) {
742 ret = dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
743 bufptr = dev_info->buf;
744 }
745
746 rem = width;
747
748 while (rem) {
749
750 if (dev_info->bufend - bufptr < BUF_HIGH_WATER_MARK) {
751 ret =
752 dlfb_bulk_msg(dev_info,
753 bufptr - dev_info->buf);
754 bufptr = dev_info->buf;
755 }
756
757 *bufptr++ = 0xAF;
758 *bufptr++ = 0x6A;
759
760 *bufptr++ = (char)(base >> 16);
761 *bufptr++ = (char)(base >> 8);
762 *bufptr++ = (char)(base);
763
764 if (rem > 255) {
765 *bufptr++ = 255;
766 *bufptr++ = (char)(source >> 16);
767 *bufptr++ = (char)(source >> 8);
768 *bufptr++ = (char)(source);
769
770 rem -= 255;
771 base += 255 * 2;
772 source += 255 * 2;
773
774 } else {
775 *bufptr++ = rem;
776 *bufptr++ = (char)(source >> 16);
777 *bufptr++ = (char)(source >> 8);
778 *bufptr++ = (char)(source);
779
780 base += rem * 2;
781 source += rem * 2;
782 rem = 0;
783 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700784 }
785
786 base += (dev_info->info->var.xres * 2) - (width * 2);
787 source += (dev_info->info->var.xres * 2) - (width * 2);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700788 }
789
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700790 if (bufptr > dev_info->buf)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700791 ret = dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700792
793 mutex_unlock(&dev_info->bulk_mutex);
794
795 return 1;
796}
797
Greg Kroah-Hartman4b6a4852009-06-03 14:47:21 -0700798static void dlfb_copyarea(struct fb_info *info, const struct fb_copyarea *area)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700799{
800
801 struct dlfb_data *dev = info->par;
802
803 copyarea(dev, area->dx, area->dy, area->sx, area->sy, area->width,
804 area->height);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700805}
806
Greg Kroah-Hartman4b6a4852009-06-03 14:47:21 -0700807static void dlfb_imageblit(struct fb_info *info, const struct fb_image *image)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700808{
809
810 int ret;
811 struct dlfb_data *dev = info->par;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700812 cfb_imageblit(info, image);
813 ret =
814 image_blit(dev, image->dx, image->dy, image->width, image->height,
815 info->screen_base);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700816}
817
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700818static void dlfb_fillrect(struct fb_info *info,
819 const struct fb_fillrect *region)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700820{
821
822 unsigned char red, green, blue;
823 struct dlfb_data *dev = info->par;
824
825 memcpy(&red, &region->color, 1);
826 memcpy(&green, &region->color + 1, 1);
827 memcpy(&blue, &region->color + 2, 1);
828 draw_rect(dev, region->dx, region->dy, region->width, region->height,
829 red, green, blue);
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700830 /* printk("FILL RECT %d %d !!!\n", region->dx, region->dy); */
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700831
832}
833
834static int dlfb_ioctl(struct fb_info *info, unsigned int cmd, unsigned long arg)
835{
836
837 struct dlfb_data *dev_info = info->par;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700838 struct dloarea *area = NULL;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700839
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700840 if (cmd == 0xAD) {
841 char *edid = (char *)arg;
842 dlfb_edid(dev_info);
843 if (copy_to_user(edid, dev_info->edid, 128)) {
844 return -EFAULT;
845 }
846 return 0;
847 }
848
849 if (cmd == 0xAA || cmd == 0xAB || cmd == 0xAC) {
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700850
851 area = (struct dloarea *)arg;
852
853 if (area->x < 0)
854 area->x = 0;
855
856 if (area->x > info->var.xres)
857 area->x = info->var.xres;
858
859 if (area->y < 0)
860 area->y = 0;
861
862 if (area->y > info->var.yres)
863 area->y = info->var.yres;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700864 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700865
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700866 if (cmd == 0xAA) {
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700867 image_blit(dev_info, area->x, area->y, area->w, area->h,
868 info->screen_base);
869 }
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700870 if (cmd == 0xAC) {
871 copyfb(dev_info);
872 image_blit(dev_info, area->x, area->y, area->w, area->h,
873 info->screen_base);
874 swapfb(dev_info);
875 } else if (cmd == 0xAB) {
876
877 if (area->x2 < 0)
878 area->x2 = 0;
879
880 if (area->y2 < 0)
881 area->y2 = 0;
882
883 copyarea(dev_info,
884 area->x2, area->y2, area->x, area->y, area->w,
885 area->h);
886 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700887 return 0;
888}
889
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700890/* taken from vesafb */
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700891
892static int
893dlfb_setcolreg(unsigned regno, unsigned red, unsigned green,
894 unsigned blue, unsigned transp, struct fb_info *info)
895{
896 int err = 0;
897
898 if (regno >= info->cmap.len)
899 return 1;
900
901 if (regno < 16) {
902 if (info->var.red.offset == 10) {
903 /* 1:5:5:5 */
904 ((u32 *) (info->pseudo_palette))[regno] =
905 ((red & 0xf800) >> 1) |
906 ((green & 0xf800) >> 6) | ((blue & 0xf800) >> 11);
907 } else {
908 /* 0:5:6:5 */
909 ((u32 *) (info->pseudo_palette))[regno] =
910 ((red & 0xf800)) |
911 ((green & 0xfc00) >> 5) | ((blue & 0xf800) >> 11);
912 }
913 }
914
915 return err;
916}
917
918static int dlfb_release(struct fb_info *info, int user)
919{
920 struct dlfb_data *dev_info = info->par;
921 image_blit(dev_info, 0, 0, info->var.xres, info->var.yres,
922 info->screen_base);
923 return 0;
924}
925
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700926static int dlfb_blank(int blank_mode, struct fb_info *info)
927{
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700928 struct dlfb_data *dev_info = info->par;
929 char *bufptr = dev_info->buf;
930
931 bufptr = dlfb_set_register(bufptr, 0xFF, 0x00);
932 if (blank_mode != FB_BLANK_UNBLANK) {
933 bufptr = dlfb_set_register(bufptr, 0x1F, 0x01);
934 } else {
935 bufptr = dlfb_set_register(bufptr, 0x1F, 0x00);
936 }
937 bufptr = dlfb_set_register(bufptr, 0xFF, 0xFF);
938
939 dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
940
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700941 return 0;
942}
943
944static struct fb_ops dlfb_ops = {
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700945 .fb_setcolreg = dlfb_setcolreg,
946 .fb_fillrect = dlfb_fillrect,
947 .fb_copyarea = dlfb_copyarea,
948 .fb_imageblit = dlfb_imageblit,
949 .fb_mmap = dlfb_mmap,
950 .fb_ioctl = dlfb_ioctl,
951 .fb_release = dlfb_release,
952 .fb_blank = dlfb_blank,
953};
954
Bernie Thompson59277b62009-11-24 15:52:21 -0800955static int dlfb_probe(struct usb_interface *interface,
956 const struct usb_device_id *id)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700957{
Bernie Thompson59277b62009-11-24 15:52:21 -0800958 struct device *mydev;
959 struct usb_device *usbdev;
960 struct dlfb_data *dev;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700961 struct fb_info *info;
Bernie Thompson59277b62009-11-24 15:52:21 -0800962 int videomemorysize;
963 unsigned char *videomemory;
964 int retval = -ENOMEM;
965 struct fb_var_screeninfo *var;
966 struct fb_bitfield red = { 11, 5, 0 };
967 struct fb_bitfield green = { 5, 6, 0 };
968 struct fb_bitfield blue = { 0, 5, 0 };
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700969
Bernie Thompson59277b62009-11-24 15:52:21 -0800970 usbdev = usb_get_dev(interface_to_usbdev(interface));
971 mydev = &usbdev->dev;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700972
Bernie Thompson59277b62009-11-24 15:52:21 -0800973 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
974 if (dev == NULL) {
975 dev_err(mydev, "failed alloc of dev struct\n");
976 goto err_devalloc;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700977 }
978
Bernie Thompson59277b62009-11-24 15:52:21 -0800979 mutex_init(&dev->bulk_mutex);
980 dev->udev = usbdev;
981 dev->interface = interface;
982 usb_set_intfdata(interface, dev);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700983
Bernie Thompson59277b62009-11-24 15:52:21 -0800984 dev_info(mydev, "dlfb_probe: setting up DisplayLink device\n");
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700985
Bernie Thompson59277b62009-11-24 15:52:21 -0800986 /*
987 * TODO: replace single 64K buffer with buffer list
988 * and async dispatch
989 */
990 dev->buf = kmalloc(BUF_SIZE, GFP_KERNEL);
991 if (dev->buf == NULL) {
992 dev_err(mydev, "unable to allocate memory for dlfb commands\n");
993 goto err_usballoc;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700994 }
Bernie Thompson59277b62009-11-24 15:52:21 -0800995 dev->bufend = dev->buf + BUF_SIZE;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700996
Bernie Thompson59277b62009-11-24 15:52:21 -0800997 dev->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
998 usb_fill_bulk_urb(dev->tx_urb, dev->udev,
999 usb_sndbulkpipe(dev->udev, 1), dev->buf, 0,
1000 dlfb_bulk_callback, dev);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001001
Bernie Thompson59277b62009-11-24 15:52:21 -08001002 /* allocates framebuffer driver structure, not framebuffer memory */
1003 info = framebuffer_alloc(0, mydev);
1004 if (!info)
1005 goto err_fballoc;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001006
Bernie Thompson59277b62009-11-24 15:52:21 -08001007 dev->info = info;
1008 info->par = dev;
1009 info->pseudo_palette = dev->pseudo_palette;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001010
Bernie Thompson59277b62009-11-24 15:52:21 -08001011 var = &info->var;
1012 retval = dlfb_get_var_from_edid(dev, var);
1013 if (retval) {
1014 /* had a problem getting edid. so fallback to 640x480 */
1015 dev_err(mydev, "Problem %d with EDID.\n", retval);
1016 var->xres = 640;
1017 var->yres = 480;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001018 }
1019
Bernie Thompson59277b62009-11-24 15:52:21 -08001020 /*
1021 * ok, now that we've got the size info, we can alloc our framebuffer.
1022 * We are using 16bpp.
1023 */
1024 info->var.bits_per_pixel = 16;
1025 info->fix = dlfb_fix;
1026 info->fix.line_length = var->xres * (var->bits_per_pixel / 8);
1027 videomemorysize = info->fix.line_length * var->yres;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001028
Bernie Thompson59277b62009-11-24 15:52:21 -08001029 /*
1030 * The big chunk of system memory we use as a virtual framebuffer.
1031 * Pages don't need to be set RESERVED (non-swap) immediately on 2.6
1032 * remap_pfn_page() syscall in our mmap and/or defio will handle.
1033 */
1034 videomemory = vmalloc(videomemorysize);
1035 if (!videomemory)
1036 goto err_vidmem;
1037 memset(videomemory, 0, videomemorysize);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001038
Bernie Thompson59277b62009-11-24 15:52:21 -08001039 info->screen_base = videomemory;
1040 info->fix.smem_len = PAGE_ALIGN(videomemorysize);
1041 info->fix.smem_start = (unsigned long) videomemory;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001042 info->flags =
1043 FBINFO_DEFAULT | FBINFO_READS_FAST | FBINFO_HWACCEL_IMAGEBLIT |
1044 FBINFO_HWACCEL_COPYAREA | FBINFO_HWACCEL_FILLRECT;
Bernie Thompson59277b62009-11-24 15:52:21 -08001045
1046 /*
1047 * Second framebuffer copy, mirroring the state of the framebuffer
1048 * on the physical USB device. We can function without this.
1049 * But with imperfect damage info we may end up sending pixels over USB
1050 * that were, in fact, unchanged -- wasting limited USB bandwidth
1051 */
1052 dev->backing_buffer = vmalloc(dev->screen_size);
1053 if (!dev->backing_buffer)
1054 dev_info(mydev, "No backing buffer allocated!\n");
1055
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001056 info->fbops = &dlfb_ops;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001057
Bernie Thompson59277b62009-11-24 15:52:21 -08001058 var->vmode = FB_VMODE_NONINTERLACED;
1059 var->red = red;
1060 var->green = green;
1061 var->blue = blue;
1062
1063 /*
1064 * TODO: Enable FB_CONFIG_DEFIO support
1065
1066 info->fbdefio = &dlfb_defio;
1067 fb_deferred_io_init(info);
1068
1069 */
1070
1071 retval = fb_alloc_cmap(&info->cmap, 256, 0);
1072 if (retval < 0) {
1073 dev_err(mydev, "Failed to allocate colormap\n");
1074 goto err_cmap;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001075 }
1076
Bernie Thompson59277b62009-11-24 15:52:21 -08001077 dlfb_select_std_channel(dev);
1078 dlfb_set_video_mode(dev, var);
1079 /* TODO: dlfb_dpy_update(dev); */
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001080
Bernie Thompson59277b62009-11-24 15:52:21 -08001081 retval = register_framebuffer(info);
1082 if (retval < 0)
1083 goto err_regfb;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001084
Bernie Thompson59277b62009-11-24 15:52:21 -08001085 /* paint "successful" green screen */
1086 draw_rect(dev, 0, 0, dev->info->var.xres,
1087 dev->info->var.yres, 0x30, 0xff, 0x30);
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -07001088
Bernie Thompson59277b62009-11-24 15:52:21 -08001089 dev_info(mydev, "DisplayLink USB device %d now attached, "
1090 "using %dK of memory\n", info->node,
1091 ((dev->backing_buffer) ?
1092 videomemorysize * 2 : videomemorysize) >> 10);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001093 return 0;
1094
Bernie Thompson59277b62009-11-24 15:52:21 -08001095err_regfb:
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001096 fb_dealloc_cmap(&info->cmap);
Bernie Thompson59277b62009-11-24 15:52:21 -08001097err_cmap:
1098 /* TODO: fb_deferred_io_cleanup(info); */
1099 vfree(videomemory);
1100err_vidmem:
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001101 framebuffer_release(info);
Bernie Thompson59277b62009-11-24 15:52:21 -08001102err_fballoc:
1103 kfree(dev->buf);
1104err_usballoc:
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001105 usb_set_intfdata(interface, NULL);
Bernie Thompson59277b62009-11-24 15:52:21 -08001106 usb_put_dev(dev->udev);
1107 kfree(dev);
1108err_devalloc:
1109 return retval;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001110}
1111
1112static void dlfb_disconnect(struct usb_interface *interface)
1113{
Bernie Thompson59277b62009-11-24 15:52:21 -08001114 struct dlfb_data *dev;
1115 struct fb_info *info;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001116
Bernie Thompson59277b62009-11-24 15:52:21 -08001117 dev = usb_get_intfdata(interface);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001118 usb_set_intfdata(interface, NULL);
Bernie Thompson59277b62009-11-24 15:52:21 -08001119 usb_put_dev(dev->udev);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001120
Bernie Thompson59277b62009-11-24 15:52:21 -08001121 /*
1122 * TODO: since, upon usb disconnect(), usb will cancel in-flight urbs
1123 * and error out any new ones, look at eliminating need for mutex
1124 */
1125 mutex_lock(&dev->bulk_mutex);
1126 dev->interface = NULL;
1127 info = dev->info;
1128 mutex_unlock(&dev->bulk_mutex);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001129
Bernie Thompson59277b62009-11-24 15:52:21 -08001130 if (info) {
1131 dev_info(&interface->dev, "Detaching DisplayLink device %d.\n",
1132 info->node);
1133 unregister_framebuffer(info);
1134 fb_dealloc_cmap(&info->cmap);
1135 /* TODO: fb_deferred_io_cleanup(info); */
1136 fb_dealloc_cmap(&info->cmap);
1137 vfree((void __force *)info->screen_base);
1138 framebuffer_release(info);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001139 }
1140
Bernie Thompson59277b62009-11-24 15:52:21 -08001141 if (dev->backing_buffer)
1142 vfree(dev->backing_buffer);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001143
Bernie Thompson59277b62009-11-24 15:52:21 -08001144 kfree(dev);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001145}
1146
1147static struct usb_driver dlfb_driver = {
1148 .name = "udlfb",
1149 .probe = dlfb_probe,
1150 .disconnect = dlfb_disconnect,
1151 .id_table = id_table,
1152};
1153
1154static int __init dlfb_init(void)
1155{
1156 int res;
1157
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001158 res = usb_register(&dlfb_driver);
1159 if (res)
1160 err("usb_register failed. Error number %d", res);
1161
1162 printk("VMODES initialized\n");
1163
1164 return res;
1165}
1166
1167static void __exit dlfb_exit(void)
1168{
1169 usb_deregister(&dlfb_driver);
1170}
1171
1172module_init(dlfb_init);
1173module_exit(dlfb_exit);
1174
Bernie Thompson59277b62009-11-24 15:52:21 -08001175MODULE_AUTHOR("Roberto De Ioris <roberto@unbit.it>, "
1176 "Jaya Kumar <jayakumar.lkml@gmail.com>");
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001177MODULE_DESCRIPTION(DRIVER_VERSION);
1178MODULE_LICENSE("GPL");