Staging: udlfb: checkpatch cleanup
[linux-2.6.git] / drivers / staging / udlfb / udlfb.c
blobd2adfe416a0c00e260487475eaf4fc6ea2cfbf76
1 /*
2 * udlfb.c -- Framebuffer driver for DisplayLink USB controller
4 * Copyright (C) 2009 Roberto De Ioris <roberto@unbit.it>
5 * Copyright (C) 2009 Jaya Kumar <jayakumar.lkml@gmail.com>
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.
11 * Layout is based on skeletonfb by James Simmons and Geert Uytterhoeven,
12 * usb-skeleton by GregKH.
14 * Device-specific portions based on information from Displaylink, with work
15 * from Florian Echtler, Henrik Bjerregaard Pedersen, and others.
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>
26 #include <linux/vmalloc.h>
28 #include "udlfb.h"
30 #define DRIVER_VERSION "DisplayLink Framebuffer Driver 0.4.1"
32 static 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,
42 #define NR_USB_REQUEST_I2C_SUB_IO 0x02
43 #define NR_USB_REQUEST_CHANNEL 0x12
46 * Inserts a specific DisplayLink controller command into the provided
47 * buffer.
49 static char *insert_command(char *buf, u8 reg, u8 val)
51 *buf++ = 0xAF;
52 *buf++ = 0x20;
53 *buf++ = reg;
54 *buf++ = val;
55 return buf;
58 static char *insert_vidreg_lock(char *buf)
60 return insert_command(buf, 0xFF, 0x00);
63 static char *insert_vidreg_unlock(char *buf)
65 return insert_command(buf, 0xFF, 0xFF);
69 * Once you send this command, the DisplayLink framebuffer gets driven to the
70 * display.
72 static char *insert_enable_hvsync(char *buf)
74 return insert_command(buf, 0x1F, 0x00);
77 static char *insert_set_color_depth(char *buf, u8 selection)
79 return insert_command(buf, 0x00, selection);
82 static char *insert_set_base16bpp(char *wrptr, u32 base)
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);
90 static char *insert_set_base8bpp(char *wrptr, u32 base)
92 wrptr = insert_command(wrptr, 0x26, base >> 16);
93 wrptr = insert_command(wrptr, 0x27, base >> 8);
94 return insert_command(wrptr, 0x28, base);
97 static char *insert_command_16(char *wrptr, u8 reg, u16 value)
99 wrptr = insert_command(wrptr, reg, value >> 8);
100 return insert_command(wrptr, reg+1, value);
104 * This is kind of weird because the controller takes some
105 * register values in a different byte order than other registers.
107 static char *insert_command_16be(char *wrptr, u8 reg, u16 value)
109 wrptr = insert_command(wrptr, reg, value);
110 return insert_command(wrptr, reg+1, value >> 8);
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.
122 static u16 lfsr16(u16 actual_count)
124 u32 lv = 0xFFFF; /* This is the lfsr value that the hw starts with */
126 while (actual_count--) {
127 lv = ((lv << 1) |
128 (((lv >> 15) ^ (lv >> 4) ^ (lv >> 2) ^ (lv >> 1)) & 1))
129 & 0xFFFF;
132 return (u16) lv;
136 * This does LFSR conversion on the value that is to be written.
137 * See LFSR explanation above for more detail.
139 static char *insert_command_lfsr16(char *wrptr, u8 reg, u16 value)
141 return insert_command_16(wrptr, reg, lfsr16(value));
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.
148 static char *insert_set_vid_cmds(char *wrptr, struct fb_var_screeninfo *var)
150 u16 xds, yds;
151 u16 xde, yde;
152 u16 yec;
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);
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);
169 /* x end count is active + blanking - 1 */
170 wrptr = insert_command_lfsr16(wrptr, 0x09, xde + var->right_margin - 1);
172 /* libdlo hardcodes hsync start to 1 */
173 wrptr = insert_command_lfsr16(wrptr, 0x0B, 1);
175 /* hsync end is width of sync pulse + 1 */
176 wrptr = insert_command_lfsr16(wrptr, 0x0D, var->hsync_len + 1);
178 /* hpixels is active pixels */
179 wrptr = insert_command_16(wrptr, 0x0F, var->xres);
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);
186 /* libdlo hardcodes vsync start to 0 */
187 wrptr = insert_command_lfsr16(wrptr, 0x13, 0);
189 /* vsync end is width of vsync pulse */
190 wrptr = insert_command_lfsr16(wrptr, 0x15, var->vsync_len);
192 /* vpixels is active pixels */
193 wrptr = insert_command_16(wrptr, 0x17, var->yres);
195 /* convert picoseconds to 5kHz multiple for pclk5k = x * 1E12/5k */
196 wrptr = insert_command_16be(wrptr, 0x1B, 200*1000*1000/var->pixclock);
198 return wrptr;
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.
206 static int dlfb_set_video_mode(struct dlfb_data *dev,
207 struct fb_var_screeninfo *var)
209 char *buf;
210 char *wrptr;
211 int retval = 0;
212 int writesize;
214 buf = dev->buf;
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.
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);
228 wrptr = insert_set_vid_cmds(wrptr, var);
229 wrptr = insert_enable_hvsync(wrptr);
230 wrptr = insert_vidreg_unlock(wrptr);
232 writesize = wrptr - buf;
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;
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;
249 return 0;
251 error:
252 return retval;
256 * This is necessary before we can communicate with the display controller.
258 static int dlfb_select_std_channel(struct dlfb_data *dev)
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 };
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;
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.
278 static int dlfb_get_var_from_edid(struct dlfb_data *dev,
279 struct fb_var_screeninfo *var)
281 int ret;
283 dlfb_edid(dev);
284 ret = fb_parse_edid(dev->edid, var);
286 return ret;
289 static int dlfb_mmap(struct fb_info *info, struct vm_area_struct *vma)
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;
296 printk("MMAP: %lu %u\n", offset + size, info->fix.smem_len);
298 if (offset + size > info->fix.smem_len)
299 return -EINVAL;
301 pos = (unsigned long)info->fix.smem_start + offset;
303 while (size > 0) {
304 page = vmalloc_to_pfn((void *)pos);
305 if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED))
306 return -EAGAIN;
308 start += PAGE_SIZE;
309 pos += PAGE_SIZE;
310 if (size > PAGE_SIZE)
311 size -= PAGE_SIZE;
312 else
313 size = 0;
316 vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
317 return 0;
321 /* ioctl structure */
322 struct dloarea {
323 int x, y;
324 int w, h;
325 int x2, y2;
329 * 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.
334 static struct usb_device_id id_table[] = {
335 {.idVendor = 0x17e9, .match_flags = USB_DEVICE_ID_MATCH_VENDOR,},
338 MODULE_DEVICE_TABLE(usb, id_table);
340 static struct usb_driver dlfb_driver;
342 /* thanks to Henrik Bjerregaard Pedersen for this function */
343 static char *rle_compress16(uint16_t * src, char *dst, int rem)
346 int rl;
347 uint16_t pix0;
348 char *end_if_raw = dst + 6 + 2 * rem;
350 dst += 6; /* header will be filled in if RLE is worth it */
352 while (rem && dst < end_if_raw) {
353 char *start = (char *)src;
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];
365 return dst;
369 Thanks to Henrik Bjerregaard Pedersen for rle implementation
370 and code refactoring. Next step is huffman compression.
373 static int
374 image_blit(struct dlfb_data *dev_info, int x, int y, int width, int height,
375 char *data)
378 int i, j, base;
379 int rem = width;
380 int ret;
382 int firstdiff, thistime;
384 char *bufptr;
386 if (x + width > dev_info->info->var.xres)
387 return -EINVAL;
389 if (y + height > dev_info->info->var.yres)
390 return -EINVAL;
392 mutex_lock(&dev_info->bulk_mutex);
394 base =
395 dev_info->base16 + ((dev_info->info->var.xres * 2 * y) + (x * 2));
397 data += (dev_info->info->var.xres * 2 * y) + (x * 2);
399 /* printk("IMAGE_BLIT\n"); */
401 bufptr = dev_info->buf;
403 for (i = y; i < y + height; i++) {
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;
410 rem = width;
412 /* printk("WRITING LINE %d\n", i); */
414 while (rem) {
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;
422 /* number of pixels to consider this time */
423 thistime = rem;
424 if (thistime > 255)
425 thistime = 255;
427 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;
439 } else {
440 firstdiff = 0;
444 if (firstdiff >= 0) {
445 char *end_of_rle;
447 end_of_rle =
448 rle_compress16((uint16_t *) (data +
449 firstdiff * 2),
450 bufptr,
451 thistime - firstdiff);
453 if (end_of_rle <
454 bufptr + 6 + 2 * (thistime - firstdiff)) {
455 bufptr[0] = 0xAF;
456 bufptr[1] = 0x69;
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;
467 bufptr = end_of_rle;
469 } else {
470 /* fallback to raw (or other?) */
471 *bufptr++ = 0xAF;
472 *bufptr++ = 0x68;
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 for (j = firstdiff * 2;
483 j < thistime * 2; j += 2) {
484 *bufptr++ = data[j + 1];
485 *bufptr++ = data[j];
490 base += thistime * 2;
491 data += thistime * 2;
492 rem -= thistime;
495 if (dev_info->backing_buffer)
496 memcpy(dev_info->backing_buffer +
497 (base - dev_info->base16) -
498 (width * 2), data - (width * 2), width * 2);
500 base += (dev_info->info->var.xres * 2) - (width * 2);
501 data += (dev_info->info->var.xres * 2) - (width * 2);
505 if (bufptr > dev_info->buf) {
506 ret = dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
509 mutex_unlock(&dev_info->bulk_mutex);
511 return base;
515 static int
516 draw_rect(struct dlfb_data *dev_info, int x, int y, int width, int height,
517 unsigned char red, unsigned char green, unsigned char blue)
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;
527 char *bufptr;
529 if (x + width > dev_info->info->var.xres)
530 return -EINVAL;
532 if (y + height > dev_info->info->var.yres)
533 return -EINVAL;
535 mutex_lock(&dev_info->bulk_mutex);
537 base = dev_info->base16 + (dev_info->info->var.xres * 2 * y) + (x * 2);
539 bufptr = dev_info->buf;
541 for (i = y; i < y + height; i++) {
543 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);
554 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;
559 rem = width;
561 while (rem) {
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;
570 *bufptr++ = 0xAF;
571 *bufptr++ = 0x69;
573 *bufptr++ = (char)(base >> 16);
574 *bufptr++ = (char)(base >> 8);
575 *bufptr++ = (char)(base);
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;
589 *bufptr++ = (char)(col >> 8);
590 *bufptr++ = (char)(col);
594 base += (dev_info->info->var.xres * 2) - (width * 2);
598 if (bufptr > dev_info->buf)
599 ret = dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
601 mutex_unlock(&dev_info->bulk_mutex);
603 return 1;
606 static void swapfb(struct dlfb_data *dev_info)
609 int tmpbase;
610 char *bufptr;
612 mutex_lock(&dev_info->bulk_mutex);
614 tmpbase = dev_info->base16;
616 dev_info->base16 = dev_info->base16d;
617 dev_info->base16d = tmpbase;
619 bufptr = dev_info->buf;
621 bufptr = dlfb_set_register(bufptr, 0xFF, 0x00);
623 /* set addresses */
624 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));
629 bufptr = dlfb_set_register(bufptr, 0xFF, 0x00);
631 dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
633 mutex_unlock(&dev_info->bulk_mutex);
636 static int copyfb(struct dlfb_data *dev_info)
638 int base;
639 int source;
640 int rem;
641 int i, ret;
643 char *bufptr;
645 base = dev_info->base16d;
647 mutex_lock(&dev_info->bulk_mutex);
649 source = dev_info->base16;
651 bufptr = dev_info->buf;
653 for (i = 0; i < dev_info->info->var.yres; i++) {
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;
660 rem = dev_info->info->var.xres;
662 while (rem) {
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;
672 *bufptr++ = 0xAF;
673 *bufptr++ = 0x6A;
675 *bufptr++ = (char)(base >> 16);
676 *bufptr++ = (char)(base >> 8);
677 *bufptr++ = (char)(base);
679 if (rem > 255) {
680 *bufptr++ = 255;
681 *bufptr++ = (char)(source >> 16);
682 *bufptr++ = (char)(source >> 8);
683 *bufptr++ = (char)(source);
685 rem -= 255;
686 base += 255 * 2;
687 source += 255 * 2;
689 } else {
690 *bufptr++ = rem;
691 *bufptr++ = (char)(source >> 16);
692 *bufptr++ = (char)(source >> 8);
693 *bufptr++ = (char)(source);
695 base += rem * 2;
696 source += rem * 2;
697 rem = 0;
702 if (bufptr > dev_info->buf)
703 ret = dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
705 mutex_unlock(&dev_info->bulk_mutex);
707 return 1;
711 static int
712 copyarea(struct dlfb_data *dev_info, int dx, int dy, int sx, int sy,
713 int width, int height)
715 int base;
716 int source;
717 int rem;
718 int i, ret;
720 char *bufptr;
722 if (dx + width > dev_info->info->var.xres)
723 return -EINVAL;
725 if (dy + height > dev_info->info->var.yres)
726 return -EINVAL;
728 mutex_lock(&dev_info->bulk_mutex);
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);
734 bufptr = dev_info->buf;
736 for (i = sy; i < sy + height; i++) {
738 memcpy(dev_info->backing_buffer + base - dev_info->base16,
739 dev_info->backing_buffer + source, width * 2);
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;
746 rem = width;
748 while (rem) {
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;
757 *bufptr++ = 0xAF;
758 *bufptr++ = 0x6A;
760 *bufptr++ = (char)(base >> 16);
761 *bufptr++ = (char)(base >> 8);
762 *bufptr++ = (char)(base);
764 if (rem > 255) {
765 *bufptr++ = 255;
766 *bufptr++ = (char)(source >> 16);
767 *bufptr++ = (char)(source >> 8);
768 *bufptr++ = (char)(source);
770 rem -= 255;
771 base += 255 * 2;
772 source += 255 * 2;
774 } else {
775 *bufptr++ = rem;
776 *bufptr++ = (char)(source >> 16);
777 *bufptr++ = (char)(source >> 8);
778 *bufptr++ = (char)(source);
780 base += rem * 2;
781 source += rem * 2;
782 rem = 0;
786 base += (dev_info->info->var.xres * 2) - (width * 2);
787 source += (dev_info->info->var.xres * 2) - (width * 2);
790 if (bufptr > dev_info->buf)
791 ret = dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
793 mutex_unlock(&dev_info->bulk_mutex);
795 return 1;
798 static void dlfb_copyarea(struct fb_info *info, const struct fb_copyarea *area)
801 struct dlfb_data *dev = info->par;
803 copyarea(dev, area->dx, area->dy, area->sx, area->sy, area->width,
804 area->height);
807 static void dlfb_imageblit(struct fb_info *info, const struct fb_image *image)
810 int ret;
811 struct dlfb_data *dev = info->par;
812 cfb_imageblit(info, image);
813 ret =
814 image_blit(dev, image->dx, image->dy, image->width, image->height,
815 info->screen_base);
818 static void dlfb_fillrect(struct fb_info *info,
819 const struct fb_fillrect *region)
822 unsigned char red, green, blue;
823 struct dlfb_data *dev = info->par;
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);
830 /* printk("FILL RECT %d %d !!!\n", region->dx, region->dy); */
834 static int dlfb_ioctl(struct fb_info *info, unsigned int cmd, unsigned long arg)
837 struct dlfb_data *dev_info = info->par;
838 struct dloarea *area = NULL;
840 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;
846 return 0;
849 if (cmd == 0xAA || cmd == 0xAB || cmd == 0xAC) {
851 area = (struct dloarea *)arg;
853 if (area->x < 0)
854 area->x = 0;
856 if (area->x > info->var.xres)
857 area->x = info->var.xres;
859 if (area->y < 0)
860 area->y = 0;
862 if (area->y > info->var.yres)
863 area->y = info->var.yres;
866 if (cmd == 0xAA) {
867 image_blit(dev_info, area->x, area->y, area->w, area->h,
868 info->screen_base);
870 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) {
877 if (area->x2 < 0)
878 area->x2 = 0;
880 if (area->y2 < 0)
881 area->y2 = 0;
883 copyarea(dev_info,
884 area->x2, area->y2, area->x, area->y, area->w,
885 area->h);
887 return 0;
890 /* taken from vesafb */
892 static int
893 dlfb_setcolreg(unsigned regno, unsigned red, unsigned green,
894 unsigned blue, unsigned transp, struct fb_info *info)
896 int err = 0;
898 if (regno >= info->cmap.len)
899 return 1;
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);
915 return err;
918 static int dlfb_release(struct fb_info *info, int user)
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;
926 static int dlfb_blank(int blank_mode, struct fb_info *info)
928 struct dlfb_data *dev_info = info->par;
929 char *bufptr = dev_info->buf;
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);
937 bufptr = dlfb_set_register(bufptr, 0xFF, 0xFF);
939 dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
941 return 0;
944 static struct fb_ops dlfb_ops = {
945 .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,
955 static int dlfb_probe(struct usb_interface *interface,
956 const struct usb_device_id *id)
958 struct device *mydev;
959 struct usb_device *usbdev;
960 struct dlfb_data *dev;
961 struct fb_info *info;
962 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 };
970 usbdev = usb_get_dev(interface_to_usbdev(interface));
971 mydev = &usbdev->dev;
973 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
974 if (dev == NULL) {
975 dev_err(mydev, "failed alloc of dev struct\n");
976 goto err_devalloc;
979 mutex_init(&dev->bulk_mutex);
980 dev->udev = usbdev;
981 dev->interface = interface;
982 usb_set_intfdata(interface, dev);
984 dev_info(mydev, "dlfb_probe: setting up DisplayLink device\n");
987 * TODO: replace single 64K buffer with buffer list
988 * and async dispatch
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;
995 dev->bufend = dev->buf + BUF_SIZE;
997 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);
1002 /* allocates framebuffer driver structure, not framebuffer memory */
1003 info = framebuffer_alloc(0, mydev);
1004 if (!info)
1005 goto err_fballoc;
1007 dev->info = info;
1008 info->par = dev;
1009 info->pseudo_palette = dev->pseudo_palette;
1011 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;
1021 * ok, now that we've got the size info, we can alloc our framebuffer.
1022 * We are using 16bpp.
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;
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.
1034 videomemory = vmalloc(videomemorysize);
1035 if (!videomemory)
1036 goto err_vidmem;
1037 memset(videomemory, 0, videomemorysize);
1039 info->screen_base = videomemory;
1040 info->fix.smem_len = PAGE_ALIGN(videomemorysize);
1041 info->fix.smem_start = (unsigned long) videomemory;
1042 info->flags =
1043 FBINFO_DEFAULT | FBINFO_READS_FAST | FBINFO_HWACCEL_IMAGEBLIT |
1044 FBINFO_HWACCEL_COPYAREA | FBINFO_HWACCEL_FILLRECT;
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
1052 dev->backing_buffer = vmalloc(dev->screen_size);
1053 if (!dev->backing_buffer)
1054 dev_info(mydev, "No backing buffer allocated!\n");
1056 info->fbops = &dlfb_ops;
1058 var->vmode = FB_VMODE_NONINTERLACED;
1059 var->red = red;
1060 var->green = green;
1061 var->blue = blue;
1064 * TODO: Enable FB_CONFIG_DEFIO support
1066 info->fbdefio = &dlfb_defio;
1067 fb_deferred_io_init(info);
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;
1077 dlfb_select_std_channel(dev);
1078 dlfb_set_video_mode(dev, var);
1079 /* TODO: dlfb_dpy_update(dev); */
1081 retval = register_framebuffer(info);
1082 if (retval < 0)
1083 goto err_regfb;
1085 /* paint "successful" green screen */
1086 draw_rect(dev, 0, 0, dev->info->var.xres,
1087 dev->info->var.yres, 0x30, 0xff, 0x30);
1089 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);
1093 return 0;
1095 err_regfb:
1096 fb_dealloc_cmap(&info->cmap);
1097 err_cmap:
1098 /* TODO: fb_deferred_io_cleanup(info); */
1099 vfree(videomemory);
1100 err_vidmem:
1101 framebuffer_release(info);
1102 err_fballoc:
1103 kfree(dev->buf);
1104 err_usballoc:
1105 usb_set_intfdata(interface, NULL);
1106 usb_put_dev(dev->udev);
1107 kfree(dev);
1108 err_devalloc:
1109 return retval;
1112 static void dlfb_disconnect(struct usb_interface *interface)
1114 struct dlfb_data *dev;
1115 struct fb_info *info;
1117 dev = usb_get_intfdata(interface);
1118 usb_set_intfdata(interface, NULL);
1119 usb_put_dev(dev->udev);
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
1125 mutex_lock(&dev->bulk_mutex);
1126 dev->interface = NULL;
1127 info = dev->info;
1128 mutex_unlock(&dev->bulk_mutex);
1130 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);
1141 if (dev->backing_buffer)
1142 vfree(dev->backing_buffer);
1144 kfree(dev);
1147 static struct usb_driver dlfb_driver = {
1148 .name = "udlfb",
1149 .probe = dlfb_probe,
1150 .disconnect = dlfb_disconnect,
1151 .id_table = id_table,
1154 static int __init dlfb_init(void)
1156 int res;
1158 res = usb_register(&dlfb_driver);
1159 if (res)
1160 err("usb_register failed. Error number %d", res);
1162 printk("VMODES initialized\n");
1164 return res;
1167 static void __exit dlfb_exit(void)
1169 usb_deregister(&dlfb_driver);
1172 module_init(dlfb_init);
1173 module_exit(dlfb_exit);
1175 MODULE_AUTHOR("Roberto De Ioris <roberto@unbit.it>, "
1176 "Jaya Kumar <jayakumar.lkml@gmail.com>");
1177 MODULE_DESCRIPTION(DRIVER_VERSION);
1178 MODULE_LICENSE("GPL");