Staging: udlfb: checkpatch cleanup
[deliverable/linux.git] / drivers / staging / udlfb / udlfb.c
CommitLineData
59277b67
BT
1/*
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 */
88e58b1a
RDI
17
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>
fb299002 26#include <linux/vmalloc.h>
88e58b1a
RDI
27
28#include "udlfb.h"
29
59277b67
BT
30#define DRIVER_VERSION "DisplayLink Framebuffer Driver 0.4.1"
31
32static struct fb_fix_screeninfo dlfb_fix = {
1d31a9ee
BT
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,
59277b67 40};
88e58b1a 41
59277b67
BT
42#define NR_USB_REQUEST_I2C_SUB_IO 0x02
43#define NR_USB_REQUEST_CHANNEL 0x12
88e58b1a 44
59277b67
BT
45/*
46 * Inserts a specific DisplayLink controller command into the provided
47 * buffer.
48 */
49static char *insert_command(char *buf, u8 reg, u8 val)
88e58b1a 50{
1d31a9ee
BT
51 *buf++ = 0xAF;
52 *buf++ = 0x20;
53 *buf++ = reg;
54 *buf++ = val;
55 return buf;
59277b67 56}
88e58b1a 57
59277b67
BT
58static char *insert_vidreg_lock(char *buf)
59{
1d31a9ee 60 return insert_command(buf, 0xFF, 0x00);
59277b67 61}
88e58b1a 62
59277b67
BT
63static char *insert_vidreg_unlock(char *buf)
64{
1d31a9ee 65 return insert_command(buf, 0xFF, 0xFF);
59277b67
BT
66}
67
68/*
69 * Once you send this command, the DisplayLink framebuffer gets driven to the
70 * display.
71 */
72static char *insert_enable_hvsync(char *buf)
73{
1d31a9ee 74 return insert_command(buf, 0x1F, 0x00);
59277b67
BT
75}
76
77static char *insert_set_color_depth(char *buf, u8 selection)
78{
1d31a9ee 79 return insert_command(buf, 0x00, selection);
59277b67
BT
80}
81
82static char *insert_set_base16bpp(char *wrptr, u32 base)
83{
1d31a9ee
BT
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);
59277b67
BT
88}
89
90static char *insert_set_base8bpp(char *wrptr, u32 base)
91{
1d31a9ee
BT
92 wrptr = insert_command(wrptr, 0x26, base >> 16);
93 wrptr = insert_command(wrptr, 0x27, base >> 8);
94 return insert_command(wrptr, 0x28, base);
59277b67
BT
95}
96
97static char *insert_command_16(char *wrptr, u8 reg, u16 value)
98{
1d31a9ee
BT
99 wrptr = insert_command(wrptr, reg, value >> 8);
100 return insert_command(wrptr, reg+1, value);
59277b67
BT
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{
1d31a9ee
BT
109 wrptr = insert_command(wrptr, reg, value);
110 return insert_command(wrptr, reg+1, value >> 8);
59277b67
BT
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;
88e58b1a
RDI
130 }
131
59277b67
BT
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));
88e58b1a
RDI
142}
143
59277b67
BT
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)
88e58b1a 149{
59277b67
BT
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);
88e58b1a 180
59277b67
BT
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);
88e58b1a 185
59277b67
BT
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;
88e58b1a 239 }
59277b67
BT
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;
88e58b1a
RDI
287}
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
f05e0575 298 if (offset + size > info->fix.smem_len)
88e58b1a 299 return -EINVAL;
88e58b1a
RDI
300
301 pos = (unsigned long)info->fix.smem_start + offset;
302
303 while (size > 0) {
304 page = vmalloc_to_pfn((void *)pos);
f05e0575 305 if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED))
88e58b1a 306 return -EAGAIN;
f05e0575 307
88e58b1a
RDI
308 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
f05e0575 321/* ioctl structure */
88e58b1a
RDI
322struct dloarea {
323 int x, y;
324 int w, h;
7316bc55 325 int x2, y2;
88e58b1a
RDI
326};
327
328/*
59277b67
BT
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.
333 */
88e58b1a 334static struct usb_device_id id_table[] = {
f05e0575 335 {.idVendor = 0x17e9, .match_flags = USB_DEVICE_ID_MATCH_VENDOR,},
88e58b1a
RDI
336 {},
337};
88e58b1a
RDI
338MODULE_DEVICE_TABLE(usb, id_table);
339
340static struct usb_driver dlfb_driver;
341
1d31a9ee 342/* thanks to Henrik Bjerregaard Pedersen for this function */
7316bc55
RDI
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
1d31a9ee 350 dst += 6; /* header will be filled in if RLE is worth it */
7316bc55
RDI
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/*
1d31a9ee
BT
369Thanks to Henrik Bjerregaard Pedersen for rle implementation
370and code refactoring. Next step is huffman compression.
7316bc55
RDI
371*/
372
88e58b1a
RDI
373static 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
7316bc55 382 int firstdiff, thistime;
88e58b1a
RDI
383
384 char *bufptr;
385
f05e0575 386 if (x + width > dev_info->info->var.xres)
88e58b1a 387 return -EINVAL;
88e58b1a 388
f05e0575 389 if (y + height > dev_info->info->var.yres)
88e58b1a 390 return -EINVAL;
88e58b1a
RDI
391
392 mutex_lock(&dev_info->bulk_mutex);
393
7316bc55
RDI
394 base =
395 dev_info->base16 + ((dev_info->info->var.xres * 2 * y) + (x * 2));
88e58b1a
RDI
396
397 data += (dev_info->info->var.xres * 2 * y) + (x * 2);
398
f05e0575 399 /* printk("IMAGE_BLIT\n"); */
88e58b1a
RDI
400
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
f05e0575 412 /* printk("WRITING LINE %d\n", i); */
88e58b1a
RDI
413
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 }
1d31a9ee 422 /* number of pixels to consider this time */
7316bc55
RDI
423 thistime = rem;
424 if (thistime > 255)
425 thistime = 255;
426
59277b67
BT
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;
436 }
88e58b1a 437 }
59277b67
BT
438
439 } else {
440 firstdiff = 0;
441
7316bc55 442 }
88e58b1a 443
7316bc55
RDI
444 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 {
1d31a9ee 470 /* fallback to raw (or other?) */
88e58b1a
RDI
471 *bufptr++ = 0xAF;
472 *bufptr++ = 0x68;
473
7316bc55
RDI
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;
7316bc55
RDI
482 for (j = firstdiff * 2;
483 j < thistime * 2; j += 2) {
484 *bufptr++ = data[j + 1];
485 *bufptr++ = data[j];
88e58b1a 486 }
88e58b1a 487 }
88e58b1a
RDI
488 }
489
7316bc55
RDI
490 base += thistime * 2;
491 data += thistime * 2;
492 rem -= thistime;
88e58b1a
RDI
493 }
494
59277b67
BT
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);
88e58b1a
RDI
499
500 base += (dev_info->info->var.xres * 2) - (width * 2);
501 data += (dev_info->info->var.xres * 2) - (width * 2);
502
503 }
504
7316bc55 505 if (bufptr > dev_info->buf) {
88e58b1a 506 ret = dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
7316bc55 507 }
88e58b1a
RDI
508
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
f05e0575 529 if (x + width > dev_info->info->var.xres)
88e58b1a 530 return -EINVAL;
88e58b1a 531
f05e0575 532 if (y + height > dev_info->info->var.yres)
88e58b1a 533 return -EINVAL;
88e58b1a
RDI
534
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
59277b67
BT
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);
551 }
88e58b1a 552 }
59277b67 553
88e58b1a
RDI
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;
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
f05e0575 598 if (bufptr > dev_info->buf)
88e58b1a 599 ret = dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
88e58b1a
RDI
600
601 mutex_unlock(&dev_info->bulk_mutex);
602
603 return 1;
604}
605
7316bc55
RDI
606static 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
1d31a9ee 623 /* set addresses */
7316bc55
RDI
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));
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
88e58b1a
RDI
711static int
712copyarea(struct dlfb_data *dev_info, int dx, int dy, int sx, int sy,
713 int width, int height)
714{
88e58b1a
RDI
715 int base;
716 int source;
717 int rem;
718 int i, ret;
719
720 char *bufptr;
721
f05e0575 722 if (dx + width > dev_info->info->var.xres)
88e58b1a 723 return -EINVAL;
88e58b1a 724
f05e0575 725 if (dy + height > dev_info->info->var.yres)
88e58b1a 726 return -EINVAL;
88e58b1a
RDI
727
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
7316bc55 738 memcpy(dev_info->backing_buffer + base - dev_info->base16,
88e58b1a
RDI
739 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 }
88e58b1a
RDI
784 }
785
786 base += (dev_info->info->var.xres * 2) - (width * 2);
787 source += (dev_info->info->var.xres * 2) - (width * 2);
88e58b1a
RDI
788 }
789
f05e0575 790 if (bufptr > dev_info->buf)
88e58b1a 791 ret = dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
88e58b1a
RDI
792
793 mutex_unlock(&dev_info->bulk_mutex);
794
795 return 1;
796}
797
4b6a4856 798static void dlfb_copyarea(struct fb_info *info, const struct fb_copyarea *area)
88e58b1a
RDI
799{
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);
88e58b1a
RDI
805}
806
4b6a4856 807static void dlfb_imageblit(struct fb_info *info, const struct fb_image *image)
88e58b1a
RDI
808{
809
810 int ret;
811 struct dlfb_data *dev = info->par;
88e58b1a
RDI
812 cfb_imageblit(info, image);
813 ret =
814 image_blit(dev, image->dx, image->dy, image->width, image->height,
815 info->screen_base);
88e58b1a
RDI
816}
817
7316bc55
RDI
818static void dlfb_fillrect(struct fb_info *info,
819 const struct fb_fillrect *region)
88e58b1a
RDI
820{
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);
f05e0575 830 /* printk("FILL RECT %d %d !!!\n", region->dx, region->dy); */
88e58b1a
RDI
831
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;
7316bc55 838 struct dloarea *area = NULL;
88e58b1a 839
7316bc55
RDI
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;
845 }
846 return 0;
847 }
848
849 if (cmd == 0xAA || cmd == 0xAB || cmd == 0xAC) {
88e58b1a
RDI
850
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;
7316bc55 864 }
88e58b1a 865
7316bc55 866 if (cmd == 0xAA) {
88e58b1a
RDI
867 image_blit(dev_info, area->x, area->y, area->w, area->h,
868 info->screen_base);
869 }
7316bc55
RDI
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) {
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 }
88e58b1a
RDI
887 return 0;
888}
889
f05e0575 890/* taken from vesafb */
88e58b1a
RDI
891
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
f05e0575
GKH
926static int dlfb_blank(int blank_mode, struct fb_info *info)
927{
7316bc55
RDI
928 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
88e58b1a
RDI
941 return 0;
942}
943
944static struct fb_ops dlfb_ops = {
88e58b1a
RDI
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,
953};
954
59277b67
BT
955static int dlfb_probe(struct usb_interface *interface,
956 const struct usb_device_id *id)
88e58b1a 957{
59277b67
BT
958 struct device *mydev;
959 struct usb_device *usbdev;
960 struct dlfb_data *dev;
88e58b1a 961 struct fb_info *info;
59277b67
BT
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 };
969
970 usbdev = usb_get_dev(interface_to_usbdev(interface));
971 mydev = &usbdev->dev;
972
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;
88e58b1a 977 }
88e58b1a 978
59277b67
BT
979 mutex_init(&dev->bulk_mutex);
980 dev->udev = usbdev;
981 dev->interface = interface;
982 usb_set_intfdata(interface, dev);
983
984 dev_info(mydev, "dlfb_probe: setting up DisplayLink device\n");
985
986 /*
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;
88e58b1a 994 }
59277b67
BT
995 dev->bufend = dev->buf + BUF_SIZE;
996
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);
1001
1002 /* allocates framebuffer driver structure, not framebuffer memory */
1003 info = framebuffer_alloc(0, mydev);
1004 if (!info)
1005 goto err_fballoc;
1006
1007 dev->info = info;
1008 info->par = dev;
1009 info->pseudo_palette = dev->pseudo_palette;
1010
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;
7316bc55 1018 }
88e58b1a 1019
59277b67
BT
1020 /*
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;
1028
1029 /*
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);
1038
1039 info->screen_base = videomemory;
1040 info->fix.smem_len = PAGE_ALIGN(videomemorysize);
1041 info->fix.smem_start = (unsigned long) videomemory;
88e58b1a
RDI
1042 info->flags =
1043 FBINFO_DEFAULT | FBINFO_READS_FAST | FBINFO_HWACCEL_IMAGEBLIT |
1044 FBINFO_HWACCEL_COPYAREA | FBINFO_HWACCEL_FILLRECT;
88e58b1a 1045
59277b67
BT
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");
f05e0575 1055
59277b67 1056 info->fbops = &dlfb_ops;
88e58b1a 1057
59277b67
BT
1058 var->vmode = FB_VMODE_NONINTERLACED;
1059 var->red = red;
1060 var->green = green;
1061 var->blue = blue;
88e58b1a 1062
59277b67
BT
1063 /*
1064 * TODO: Enable FB_CONFIG_DEFIO support
88e58b1a 1065
59277b67
BT
1066 info->fbdefio = &dlfb_defio;
1067 fb_deferred_io_init(info);
88e58b1a 1068
59277b67 1069 */
88e58b1a 1070
59277b67
BT
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;
7316bc55 1075 }
88e58b1a 1076
59277b67
BT
1077 dlfb_select_std_channel(dev);
1078 dlfb_set_video_mode(dev, var);
1079 /* TODO: dlfb_dpy_update(dev); */
88e58b1a 1080
59277b67
BT
1081 retval = register_framebuffer(info);
1082 if (retval < 0)
1083 goto err_regfb;
88e58b1a 1084
59277b67
BT
1085 /* paint "successful" green screen */
1086 draw_rect(dev, 0, 0, dev->info->var.xres,
1087 dev->info->var.yres, 0x30, 0xff, 0x30);
88e58b1a 1088
59277b67
BT
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);
88e58b1a
RDI
1093 return 0;
1094
59277b67 1095err_regfb:
88e58b1a 1096 fb_dealloc_cmap(&info->cmap);
59277b67
BT
1097err_cmap:
1098 /* TODO: fb_deferred_io_cleanup(info); */
1099 vfree(videomemory);
1100err_vidmem:
88e58b1a 1101 framebuffer_release(info);
59277b67
BT
1102err_fballoc:
1103 kfree(dev->buf);
1104err_usballoc:
88e58b1a 1105 usb_set_intfdata(interface, NULL);
59277b67
BT
1106 usb_put_dev(dev->udev);
1107 kfree(dev);
1108err_devalloc:
1109 return retval;
88e58b1a
RDI
1110}
1111
1112static void dlfb_disconnect(struct usb_interface *interface)
1113{
59277b67
BT
1114 struct dlfb_data *dev;
1115 struct fb_info *info;
88e58b1a 1116
59277b67 1117 dev = usb_get_intfdata(interface);
88e58b1a 1118 usb_set_intfdata(interface, NULL);
59277b67
BT
1119 usb_put_dev(dev->udev);
1120
1121 /*
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);
1129
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);
88e58b1a
RDI
1139 }
1140
59277b67
BT
1141 if (dev->backing_buffer)
1142 vfree(dev->backing_buffer);
88e58b1a 1143
59277b67 1144 kfree(dev);
88e58b1a
RDI
1145}
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
88e58b1a
RDI
1158 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
59277b67
BT
1175MODULE_AUTHOR("Roberto De Ioris <roberto@unbit.it>, "
1176 "Jaya Kumar <jayakumar.lkml@gmail.com>");
88e58b1a
RDI
1177MODULE_DESCRIPTION(DRIVER_VERSION);
1178MODULE_LICENSE("GPL");
This page took 0.129193 seconds and 5 git commands to generate.