blob: b61a3e58a9ec1d77352852e3174472f70f4f9c61 [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 = {
33 .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,
40};
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 Thompson59277b62009-11-24 15:52:21 -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 Thompson59277b62009-11-24 15:52:21 -080060 return insert_command(buf, 0xFF, 0x00);
61}
Roberto De Ioris88e58b12009-06-03 14:03:06 -070062
Bernie Thompson59277b62009-11-24 15:52:21 -080063static char *insert_vidreg_unlock(char *buf)
64{
65 return insert_command(buf, 0xFF, 0xFF);
66}
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{
74 return insert_command(buf, 0x1F, 0x00);
75}
76
77static char *insert_set_color_depth(char *buf, u8 selection)
78{
79 return insert_command(buf, 0x00, selection);
80}
81
82static char *insert_set_base16bpp(char *wrptr, u32 base)
83{
84 /* 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);
88}
89
90static char *insert_set_base8bpp(char *wrptr, u32 base)
91{
92 wrptr = insert_command(wrptr, 0x26, base >> 16);
93 wrptr = insert_command(wrptr, 0x27, base >> 8);
94 return insert_command(wrptr, 0x28, base);
95}
96
97static char *insert_command_16(char *wrptr, u8 reg, u16 value)
98{
99 wrptr = insert_command(wrptr, reg, value >> 8);
100 return insert_command(wrptr, reg+1, value);
101}
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{
109 wrptr = insert_command(wrptr, reg, value);
110 return insert_command(wrptr, reg+1, value >> 8);
111}
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
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700342// thanks to Henrik Bjerregaard Pedersen for this function
343static 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
350 dst += 6; // header will be filled in if RLE is worth it
351
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/*
369Thanks to Henrik Bjerregaard Pedersen for rle implementation and code refactoring.
370Next step is huffman compression.
371*/
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 }
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700422 // number of pixels to consider this time
423 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 {
470 // fallback to raw (or some other encoding?)
471 *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;
482 // PUT COMPRESSION HERE
483 for (j = firstdiff * 2;
484 j < thistime * 2; j += 2) {
485 *bufptr++ = data[j + 1];
486 *bufptr++ = data[j];
487 }
488 }
489 }
490
491 base += thistime * 2;
492 data += thistime * 2;
493 rem -= thistime;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700494 }
495
Bernie Thompson59277b62009-11-24 15:52:21 -0800496 if (dev_info->backing_buffer)
497 memcpy(dev_info->backing_buffer +
498 (base - dev_info->base16) -
499 (width * 2), data - (width * 2), width * 2);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700500
501 base += (dev_info->info->var.xres * 2) - (width * 2);
502 data += (dev_info->info->var.xres * 2) - (width * 2);
503
504 }
505
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700506 if (bufptr > dev_info->buf) {
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700507 ret = dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700508 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700509
510 mutex_unlock(&dev_info->bulk_mutex);
511
512 return base;
513
514}
515
516static int
517draw_rect(struct dlfb_data *dev_info, int x, int y, int width, int height,
518 unsigned char red, unsigned char green, unsigned char blue)
519{
520
521 int i, j, base;
522 int ret;
523 unsigned short col =
524 (((((red) & 0xF8) | ((green) >> 5)) & 0xFF) << 8) +
525 (((((green) & 0x1C) << 3) | ((blue) >> 3)) & 0xFF);
526 int rem = width;
527
528 char *bufptr;
529
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700530 if (x + width > dev_info->info->var.xres)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700531 return -EINVAL;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700532
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700533 if (y + height > dev_info->info->var.yres)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700534 return -EINVAL;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700535
536 mutex_lock(&dev_info->bulk_mutex);
537
538 base = dev_info->base16 + (dev_info->info->var.xres * 2 * y) + (x * 2);
539
540 bufptr = dev_info->buf;
541
542 for (i = y; i < y + height; i++) {
543
Bernie Thompson59277b62009-11-24 15:52:21 -0800544 if (dev_info->backing_buffer) {
545 for (j = 0; j < width * 2; j += 2) {
546 dev_info->backing_buffer
547 [base - dev_info->base16 + j] =
548 (char)(col >> 8);
549 dev_info->backing_buffer
550 [base - dev_info->base16 + j + 1] =
551 (char)(col);
552 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700553 }
Bernie Thompson59277b62009-11-24 15:52:21 -0800554
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700555 if (dev_info->bufend - bufptr < BUF_HIGH_WATER_MARK) {
556 ret = dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
557 bufptr = dev_info->buf;
558 }
559
560 rem = width;
561
562 while (rem) {
563
564 if (dev_info->bufend - bufptr < BUF_HIGH_WATER_MARK) {
565 ret =
566 dlfb_bulk_msg(dev_info,
567 bufptr - dev_info->buf);
568 bufptr = dev_info->buf;
569 }
570
571 *bufptr++ = 0xAF;
572 *bufptr++ = 0x69;
573
574 *bufptr++ = (char)(base >> 16);
575 *bufptr++ = (char)(base >> 8);
576 *bufptr++ = (char)(base);
577
578 if (rem > 255) {
579 *bufptr++ = 255;
580 *bufptr++ = 255;
581 rem -= 255;
582 base += 255 * 2;
583 } else {
584 *bufptr++ = rem;
585 *bufptr++ = rem;
586 base += rem * 2;
587 rem = 0;
588 }
589
590 *bufptr++ = (char)(col >> 8);
591 *bufptr++ = (char)(col);
592
593 }
594
595 base += (dev_info->info->var.xres * 2) - (width * 2);
596
597 }
598
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700599 if (bufptr > dev_info->buf)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700600 ret = dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700601
602 mutex_unlock(&dev_info->bulk_mutex);
603
604 return 1;
605}
606
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700607static void swapfb(struct dlfb_data *dev_info)
608{
609
610 int tmpbase;
611 char *bufptr;
612
613 mutex_lock(&dev_info->bulk_mutex);
614
615 tmpbase = dev_info->base16;
616
617 dev_info->base16 = dev_info->base16d;
618 dev_info->base16d = tmpbase;
619
620 bufptr = dev_info->buf;
621
622 bufptr = dlfb_set_register(bufptr, 0xFF, 0x00);
623
624 // set addresses
625 bufptr =
626 dlfb_set_register(bufptr, 0x20, (char)(dev_info->base16 >> 16));
627 bufptr = dlfb_set_register(bufptr, 0x21, (char)(dev_info->base16 >> 8));
628 bufptr = dlfb_set_register(bufptr, 0x22, (char)(dev_info->base16));
629
630 bufptr = dlfb_set_register(bufptr, 0xFF, 0x00);
631
632 dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
633
634 mutex_unlock(&dev_info->bulk_mutex);
635}
636
637static int copyfb(struct dlfb_data *dev_info)
638{
639 int base;
640 int source;
641 int rem;
642 int i, ret;
643
644 char *bufptr;
645
646 base = dev_info->base16d;
647
648 mutex_lock(&dev_info->bulk_mutex);
649
650 source = dev_info->base16;
651
652 bufptr = dev_info->buf;
653
654 for (i = 0; i < dev_info->info->var.yres; i++) {
655
656 if (dev_info->bufend - bufptr < BUF_HIGH_WATER_MARK) {
657 ret = dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
658 bufptr = dev_info->buf;
659 }
660
661 rem = dev_info->info->var.xres;
662
663 while (rem) {
664
665 if (dev_info->bufend - bufptr < BUF_HIGH_WATER_MARK) {
666 ret =
667 dlfb_bulk_msg(dev_info,
668 bufptr - dev_info->buf);
669 bufptr = dev_info->buf;
670
671 }
672
673 *bufptr++ = 0xAF;
674 *bufptr++ = 0x6A;
675
676 *bufptr++ = (char)(base >> 16);
677 *bufptr++ = (char)(base >> 8);
678 *bufptr++ = (char)(base);
679
680 if (rem > 255) {
681 *bufptr++ = 255;
682 *bufptr++ = (char)(source >> 16);
683 *bufptr++ = (char)(source >> 8);
684 *bufptr++ = (char)(source);
685
686 rem -= 255;
687 base += 255 * 2;
688 source += 255 * 2;
689
690 } else {
691 *bufptr++ = rem;
692 *bufptr++ = (char)(source >> 16);
693 *bufptr++ = (char)(source >> 8);
694 *bufptr++ = (char)(source);
695
696 base += rem * 2;
697 source += rem * 2;
698 rem = 0;
699 }
700 }
701 }
702
703 if (bufptr > dev_info->buf)
704 ret = dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
705
706 mutex_unlock(&dev_info->bulk_mutex);
707
708 return 1;
709
710}
711
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700712static int
713copyarea(struct dlfb_data *dev_info, int dx, int dy, int sx, int sy,
714 int width, int height)
715{
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700716 int base;
717 int source;
718 int rem;
719 int i, ret;
720
721 char *bufptr;
722
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700723 if (dx + width > dev_info->info->var.xres)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700724 return -EINVAL;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700725
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700726 if (dy + height > dev_info->info->var.yres)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700727 return -EINVAL;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700728
729 mutex_lock(&dev_info->bulk_mutex);
730
731 base =
732 dev_info->base16 + (dev_info->info->var.xres * 2 * dy) + (dx * 2);
733 source = (dev_info->info->var.xres * 2 * sy) + (sx * 2);
734
735 bufptr = dev_info->buf;
736
737 for (i = sy; i < sy + height; i++) {
738
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700739 memcpy(dev_info->backing_buffer + base - dev_info->base16,
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700740 dev_info->backing_buffer + source, width * 2);
741
742 if (dev_info->bufend - bufptr < BUF_HIGH_WATER_MARK) {
743 ret = dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
744 bufptr = dev_info->buf;
745 }
746
747 rem = width;
748
749 while (rem) {
750
751 if (dev_info->bufend - bufptr < BUF_HIGH_WATER_MARK) {
752 ret =
753 dlfb_bulk_msg(dev_info,
754 bufptr - dev_info->buf);
755 bufptr = dev_info->buf;
756 }
757
758 *bufptr++ = 0xAF;
759 *bufptr++ = 0x6A;
760
761 *bufptr++ = (char)(base >> 16);
762 *bufptr++ = (char)(base >> 8);
763 *bufptr++ = (char)(base);
764
765 if (rem > 255) {
766 *bufptr++ = 255;
767 *bufptr++ = (char)(source >> 16);
768 *bufptr++ = (char)(source >> 8);
769 *bufptr++ = (char)(source);
770
771 rem -= 255;
772 base += 255 * 2;
773 source += 255 * 2;
774
775 } else {
776 *bufptr++ = rem;
777 *bufptr++ = (char)(source >> 16);
778 *bufptr++ = (char)(source >> 8);
779 *bufptr++ = (char)(source);
780
781 base += rem * 2;
782 source += rem * 2;
783 rem = 0;
784 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700785 }
786
787 base += (dev_info->info->var.xres * 2) - (width * 2);
788 source += (dev_info->info->var.xres * 2) - (width * 2);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700789 }
790
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700791 if (bufptr > dev_info->buf)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700792 ret = dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700793
794 mutex_unlock(&dev_info->bulk_mutex);
795
796 return 1;
797}
798
Greg Kroah-Hartman4b6a4852009-06-03 14:47:21 -0700799static void dlfb_copyarea(struct fb_info *info, const struct fb_copyarea *area)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700800{
801
802 struct dlfb_data *dev = info->par;
803
804 copyarea(dev, area->dx, area->dy, area->sx, area->sy, area->width,
805 area->height);
806
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700807 /* printk("COPY AREA %d %d %d %d %d %d !!!\n", area->dx, area->dy, area->sx, area->sy, area->width, area->height); */
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700808
809}
810
Greg Kroah-Hartman4b6a4852009-06-03 14:47:21 -0700811static void dlfb_imageblit(struct fb_info *info, const struct fb_image *image)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700812{
813
814 int ret;
815 struct dlfb_data *dev = info->par;
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700816 /* printk("IMAGE BLIT (1) %d %d %d %d DEPTH %d {%p}!!!\n", image->dx, image->dy, image->width, image->height, image->depth, dev->udev); */
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700817 cfb_imageblit(info, image);
818 ret =
819 image_blit(dev, image->dx, image->dy, image->width, image->height,
820 info->screen_base);
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700821 /* printk("IMAGE BLIT (2) %d %d %d %d DEPTH %d {%p} %d!!!\n", image->dx, image->dy, image->width, image->height, image->depth, dev->udev, ret); */
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700822}
823
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700824static void dlfb_fillrect(struct fb_info *info,
825 const struct fb_fillrect *region)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700826{
827
828 unsigned char red, green, blue;
829 struct dlfb_data *dev = info->par;
830
831 memcpy(&red, &region->color, 1);
832 memcpy(&green, &region->color + 1, 1);
833 memcpy(&blue, &region->color + 2, 1);
834 draw_rect(dev, region->dx, region->dy, region->width, region->height,
835 red, green, blue);
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700836 /* printk("FILL RECT %d %d !!!\n", region->dx, region->dy); */
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700837
838}
839
840static int dlfb_ioctl(struct fb_info *info, unsigned int cmd, unsigned long arg)
841{
842
843 struct dlfb_data *dev_info = info->par;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700844 struct dloarea *area = NULL;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700845
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700846 if (cmd == 0xAD) {
847 char *edid = (char *)arg;
848 dlfb_edid(dev_info);
849 if (copy_to_user(edid, dev_info->edid, 128)) {
850 return -EFAULT;
851 }
852 return 0;
853 }
854
855 if (cmd == 0xAA || cmd == 0xAB || cmd == 0xAC) {
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700856
857 area = (struct dloarea *)arg;
858
859 if (area->x < 0)
860 area->x = 0;
861
862 if (area->x > info->var.xres)
863 area->x = info->var.xres;
864
865 if (area->y < 0)
866 area->y = 0;
867
868 if (area->y > info->var.yres)
869 area->y = info->var.yres;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700870 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700871
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700872 if (cmd == 0xAA) {
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700873 image_blit(dev_info, area->x, area->y, area->w, area->h,
874 info->screen_base);
875 }
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700876 if (cmd == 0xAC) {
877 copyfb(dev_info);
878 image_blit(dev_info, area->x, area->y, area->w, area->h,
879 info->screen_base);
880 swapfb(dev_info);
881 } else if (cmd == 0xAB) {
882
883 if (area->x2 < 0)
884 area->x2 = 0;
885
886 if (area->y2 < 0)
887 area->y2 = 0;
888
889 copyarea(dev_info,
890 area->x2, area->y2, area->x, area->y, area->w,
891 area->h);
892 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700893 return 0;
894}
895
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700896/* taken from vesafb */
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700897
898static int
899dlfb_setcolreg(unsigned regno, unsigned red, unsigned green,
900 unsigned blue, unsigned transp, struct fb_info *info)
901{
902 int err = 0;
903
904 if (regno >= info->cmap.len)
905 return 1;
906
907 if (regno < 16) {
908 if (info->var.red.offset == 10) {
909 /* 1:5:5:5 */
910 ((u32 *) (info->pseudo_palette))[regno] =
911 ((red & 0xf800) >> 1) |
912 ((green & 0xf800) >> 6) | ((blue & 0xf800) >> 11);
913 } else {
914 /* 0:5:6:5 */
915 ((u32 *) (info->pseudo_palette))[regno] =
916 ((red & 0xf800)) |
917 ((green & 0xfc00) >> 5) | ((blue & 0xf800) >> 11);
918 }
919 }
920
921 return err;
922}
923
924static int dlfb_release(struct fb_info *info, int user)
925{
926 struct dlfb_data *dev_info = info->par;
927 image_blit(dev_info, 0, 0, info->var.xres, info->var.yres,
928 info->screen_base);
929 return 0;
930}
931
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700932static int dlfb_blank(int blank_mode, struct fb_info *info)
933{
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700934 struct dlfb_data *dev_info = info->par;
935 char *bufptr = dev_info->buf;
936
937 bufptr = dlfb_set_register(bufptr, 0xFF, 0x00);
938 if (blank_mode != FB_BLANK_UNBLANK) {
939 bufptr = dlfb_set_register(bufptr, 0x1F, 0x01);
940 } else {
941 bufptr = dlfb_set_register(bufptr, 0x1F, 0x00);
942 }
943 bufptr = dlfb_set_register(bufptr, 0xFF, 0xFF);
944
945 dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
946
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700947 return 0;
948}
949
950static struct fb_ops dlfb_ops = {
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700951 .fb_setcolreg = dlfb_setcolreg,
952 .fb_fillrect = dlfb_fillrect,
953 .fb_copyarea = dlfb_copyarea,
954 .fb_imageblit = dlfb_imageblit,
955 .fb_mmap = dlfb_mmap,
956 .fb_ioctl = dlfb_ioctl,
957 .fb_release = dlfb_release,
958 .fb_blank = dlfb_blank,
959};
960
Bernie Thompson59277b62009-11-24 15:52:21 -0800961static int dlfb_probe(struct usb_interface *interface,
962 const struct usb_device_id *id)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700963{
Bernie Thompson59277b62009-11-24 15:52:21 -0800964 struct device *mydev;
965 struct usb_device *usbdev;
966 struct dlfb_data *dev;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700967 struct fb_info *info;
Bernie Thompson59277b62009-11-24 15:52:21 -0800968 int videomemorysize;
969 unsigned char *videomemory;
970 int retval = -ENOMEM;
971 struct fb_var_screeninfo *var;
972 struct fb_bitfield red = { 11, 5, 0 };
973 struct fb_bitfield green = { 5, 6, 0 };
974 struct fb_bitfield blue = { 0, 5, 0 };
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700975
Bernie Thompson59277b62009-11-24 15:52:21 -0800976 usbdev = usb_get_dev(interface_to_usbdev(interface));
977 mydev = &usbdev->dev;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700978
Bernie Thompson59277b62009-11-24 15:52:21 -0800979 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
980 if (dev == NULL) {
981 dev_err(mydev, "failed alloc of dev struct\n");
982 goto err_devalloc;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700983 }
984
Bernie Thompson59277b62009-11-24 15:52:21 -0800985 mutex_init(&dev->bulk_mutex);
986 dev->udev = usbdev;
987 dev->interface = interface;
988 usb_set_intfdata(interface, dev);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700989
Bernie Thompson59277b62009-11-24 15:52:21 -0800990 dev_info(mydev, "dlfb_probe: setting up DisplayLink device\n");
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700991
Bernie Thompson59277b62009-11-24 15:52:21 -0800992 /*
993 * TODO: replace single 64K buffer with buffer list
994 * and async dispatch
995 */
996 dev->buf = kmalloc(BUF_SIZE, GFP_KERNEL);
997 if (dev->buf == NULL) {
998 dev_err(mydev, "unable to allocate memory for dlfb commands\n");
999 goto err_usballoc;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001000 }
Bernie Thompson59277b62009-11-24 15:52:21 -08001001 dev->bufend = dev->buf + BUF_SIZE;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001002
Bernie Thompson59277b62009-11-24 15:52:21 -08001003 dev->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
1004 usb_fill_bulk_urb(dev->tx_urb, dev->udev,
1005 usb_sndbulkpipe(dev->udev, 1), dev->buf, 0,
1006 dlfb_bulk_callback, dev);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001007
Bernie Thompson59277b62009-11-24 15:52:21 -08001008 /* allocates framebuffer driver structure, not framebuffer memory */
1009 info = framebuffer_alloc(0, mydev);
1010 if (!info)
1011 goto err_fballoc;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001012
Bernie Thompson59277b62009-11-24 15:52:21 -08001013 dev->info = info;
1014 info->par = dev;
1015 info->pseudo_palette = dev->pseudo_palette;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001016
Bernie Thompson59277b62009-11-24 15:52:21 -08001017 var = &info->var;
1018 retval = dlfb_get_var_from_edid(dev, var);
1019 if (retval) {
1020 /* had a problem getting edid. so fallback to 640x480 */
1021 dev_err(mydev, "Problem %d with EDID.\n", retval);
1022 var->xres = 640;
1023 var->yres = 480;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001024 }
1025
Bernie Thompson59277b62009-11-24 15:52:21 -08001026 /*
1027 * ok, now that we've got the size info, we can alloc our framebuffer.
1028 * We are using 16bpp.
1029 */
1030 info->var.bits_per_pixel = 16;
1031 info->fix = dlfb_fix;
1032 info->fix.line_length = var->xres * (var->bits_per_pixel / 8);
1033 videomemorysize = info->fix.line_length * var->yres;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001034
Bernie Thompson59277b62009-11-24 15:52:21 -08001035 /*
1036 * The big chunk of system memory we use as a virtual framebuffer.
1037 * Pages don't need to be set RESERVED (non-swap) immediately on 2.6
1038 * remap_pfn_page() syscall in our mmap and/or defio will handle.
1039 */
1040 videomemory = vmalloc(videomemorysize);
1041 if (!videomemory)
1042 goto err_vidmem;
1043 memset(videomemory, 0, videomemorysize);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001044
Bernie Thompson59277b62009-11-24 15:52:21 -08001045 info->screen_base = videomemory;
1046 info->fix.smem_len = PAGE_ALIGN(videomemorysize);
1047 info->fix.smem_start = (unsigned long) videomemory;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001048 info->flags =
1049 FBINFO_DEFAULT | FBINFO_READS_FAST | FBINFO_HWACCEL_IMAGEBLIT |
1050 FBINFO_HWACCEL_COPYAREA | FBINFO_HWACCEL_FILLRECT;
Bernie Thompson59277b62009-11-24 15:52:21 -08001051
1052 /*
1053 * Second framebuffer copy, mirroring the state of the framebuffer
1054 * on the physical USB device. We can function without this.
1055 * But with imperfect damage info we may end up sending pixels over USB
1056 * that were, in fact, unchanged -- wasting limited USB bandwidth
1057 */
1058 dev->backing_buffer = vmalloc(dev->screen_size);
1059 if (!dev->backing_buffer)
1060 dev_info(mydev, "No backing buffer allocated!\n");
1061
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001062 info->fbops = &dlfb_ops;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001063
Bernie Thompson59277b62009-11-24 15:52:21 -08001064 var->vmode = FB_VMODE_NONINTERLACED;
1065 var->red = red;
1066 var->green = green;
1067 var->blue = blue;
1068
1069 /*
1070 * TODO: Enable FB_CONFIG_DEFIO support
1071
1072 info->fbdefio = &dlfb_defio;
1073 fb_deferred_io_init(info);
1074
1075 */
1076
1077 retval = fb_alloc_cmap(&info->cmap, 256, 0);
1078 if (retval < 0) {
1079 dev_err(mydev, "Failed to allocate colormap\n");
1080 goto err_cmap;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001081 }
1082
Bernie Thompson59277b62009-11-24 15:52:21 -08001083 dlfb_select_std_channel(dev);
1084 dlfb_set_video_mode(dev, var);
1085 /* TODO: dlfb_dpy_update(dev); */
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001086
Bernie Thompson59277b62009-11-24 15:52:21 -08001087 retval = register_framebuffer(info);
1088 if (retval < 0)
1089 goto err_regfb;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001090
Bernie Thompson59277b62009-11-24 15:52:21 -08001091 /* paint "successful" green screen */
1092 draw_rect(dev, 0, 0, dev->info->var.xres,
1093 dev->info->var.yres, 0x30, 0xff, 0x30);
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -07001094
Bernie Thompson59277b62009-11-24 15:52:21 -08001095 dev_info(mydev, "DisplayLink USB device %d now attached, "
1096 "using %dK of memory\n", info->node,
1097 ((dev->backing_buffer) ?
1098 videomemorysize * 2 : videomemorysize) >> 10);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001099 return 0;
1100
Bernie Thompson59277b62009-11-24 15:52:21 -08001101err_regfb:
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001102 fb_dealloc_cmap(&info->cmap);
Bernie Thompson59277b62009-11-24 15:52:21 -08001103err_cmap:
1104 /* TODO: fb_deferred_io_cleanup(info); */
1105 vfree(videomemory);
1106err_vidmem:
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001107 framebuffer_release(info);
Bernie Thompson59277b62009-11-24 15:52:21 -08001108err_fballoc:
1109 kfree(dev->buf);
1110err_usballoc:
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001111 usb_set_intfdata(interface, NULL);
Bernie Thompson59277b62009-11-24 15:52:21 -08001112 usb_put_dev(dev->udev);
1113 kfree(dev);
1114err_devalloc:
1115 return retval;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001116}
1117
1118static void dlfb_disconnect(struct usb_interface *interface)
1119{
Bernie Thompson59277b62009-11-24 15:52:21 -08001120 struct dlfb_data *dev;
1121 struct fb_info *info;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001122
Bernie Thompson59277b62009-11-24 15:52:21 -08001123 dev = usb_get_intfdata(interface);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001124 usb_set_intfdata(interface, NULL);
Bernie Thompson59277b62009-11-24 15:52:21 -08001125 usb_put_dev(dev->udev);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001126
Bernie Thompson59277b62009-11-24 15:52:21 -08001127 /*
1128 * TODO: since, upon usb disconnect(), usb will cancel in-flight urbs
1129 * and error out any new ones, look at eliminating need for mutex
1130 */
1131 mutex_lock(&dev->bulk_mutex);
1132 dev->interface = NULL;
1133 info = dev->info;
1134 mutex_unlock(&dev->bulk_mutex);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001135
Bernie Thompson59277b62009-11-24 15:52:21 -08001136 if (info) {
1137 dev_info(&interface->dev, "Detaching DisplayLink device %d.\n",
1138 info->node);
1139 unregister_framebuffer(info);
1140 fb_dealloc_cmap(&info->cmap);
1141 /* TODO: fb_deferred_io_cleanup(info); */
1142 fb_dealloc_cmap(&info->cmap);
1143 vfree((void __force *)info->screen_base);
1144 framebuffer_release(info);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001145 }
1146
Bernie Thompson59277b62009-11-24 15:52:21 -08001147 if (dev->backing_buffer)
1148 vfree(dev->backing_buffer);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001149
Bernie Thompson59277b62009-11-24 15:52:21 -08001150 kfree(dev);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001151}
1152
1153static struct usb_driver dlfb_driver = {
1154 .name = "udlfb",
1155 .probe = dlfb_probe,
1156 .disconnect = dlfb_disconnect,
1157 .id_table = id_table,
1158};
1159
1160static int __init dlfb_init(void)
1161{
1162 int res;
1163
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001164 res = usb_register(&dlfb_driver);
1165 if (res)
1166 err("usb_register failed. Error number %d", res);
1167
1168 printk("VMODES initialized\n");
1169
1170 return res;
1171}
1172
1173static void __exit dlfb_exit(void)
1174{
1175 usb_deregister(&dlfb_driver);
1176}
1177
1178module_init(dlfb_init);
1179module_exit(dlfb_exit);
1180
Bernie Thompson59277b62009-11-24 15:52:21 -08001181MODULE_AUTHOR("Roberto De Ioris <roberto@unbit.it>, "
1182 "Jaya Kumar <jayakumar.lkml@gmail.com>");
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001183MODULE_DESCRIPTION(DRIVER_VERSION);
1184MODULE_LICENSE("GPL");