Staging: udlfb: Add functions to expose sysfs metrics and controls
[linux-2.6-block.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
cc403dc6
BT
42/*
43 * There are many DisplayLink-based products, all with unique PIDs. We are able
44 * to support all volume ones (circa 2009) with a single driver, so we match
45 * globally on VID. TODO: Probe() needs to detect when we might be running
46 * "future" chips, and bail on those, so a compatible driver can match.
47 */
48static struct usb_device_id id_table[] = {
49 {.idVendor = 0x17e9, .match_flags = USB_DEVICE_ID_MATCH_VENDOR,},
50 {},
51};
52MODULE_DEVICE_TABLE(usb, id_table);
88e58b1a 53
4a4854dd
BT
54/* dlfb keeps a list of urbs for efficient bulk transfers */
55static void dlfb_urb_completion(struct urb *urb);
56static struct urb *dlfb_get_urb(struct dlfb_data *dev);
57static int dlfb_submit_urb(struct dlfb_data *dev, struct urb * urb, size_t len);
58static int dlfb_alloc_urb_list(struct dlfb_data *dev, int count, size_t size);
59static void dlfb_free_urb_list(struct dlfb_data *dev);
60
59277b67
BT
61/*
62 * Inserts a specific DisplayLink controller command into the provided
63 * buffer.
64 */
4574203f 65static char *dlfb_set_register(char *buf, u8 reg, u8 val)
88e58b1a 66{
1d31a9ee
BT
67 *buf++ = 0xAF;
68 *buf++ = 0x20;
69 *buf++ = reg;
70 *buf++ = val;
71 return buf;
59277b67 72}
88e58b1a 73
4574203f 74static char *dlfb_vidreg_lock(char *buf)
59277b67 75{
4574203f 76 return dlfb_set_register(buf, 0xFF, 0x00);
59277b67 77}
88e58b1a 78
4574203f 79static char *dlfb_vidreg_unlock(char *buf)
59277b67 80{
4574203f 81 return dlfb_set_register(buf, 0xFF, 0xFF);
59277b67
BT
82}
83
84/*
85 * Once you send this command, the DisplayLink framebuffer gets driven to the
86 * display.
87 */
4574203f 88static char *dlfb_enable_hvsync(char *buf)
59277b67 89{
4574203f 90 return dlfb_set_register(buf, 0x1F, 0x00);
59277b67
BT
91}
92
4574203f 93static char *dlfb_set_color_depth(char *buf, u8 selection)
59277b67 94{
4574203f 95 return dlfb_set_register(buf, 0x00, selection);
59277b67
BT
96}
97
4574203f 98static char *dlfb_set_base16bpp(char *wrptr, u32 base)
59277b67 99{
1d31a9ee 100 /* the base pointer is 16 bits wide, 0x20 is hi byte. */
4574203f
BT
101 wrptr = dlfb_set_register(wrptr, 0x20, base >> 16);
102 wrptr = dlfb_set_register(wrptr, 0x21, base >> 8);
103 return dlfb_set_register(wrptr, 0x22, base);
59277b67
BT
104}
105
4574203f 106static char *dlfb_set_base8bpp(char *wrptr, u32 base)
59277b67 107{
4574203f
BT
108 wrptr = dlfb_set_register(wrptr, 0x26, base >> 16);
109 wrptr = dlfb_set_register(wrptr, 0x27, base >> 8);
110 return dlfb_set_register(wrptr, 0x28, base);
59277b67
BT
111}
112
4574203f 113static char *dlfb_set_register_16(char *wrptr, u8 reg, u16 value)
59277b67 114{
4574203f
BT
115 wrptr = dlfb_set_register(wrptr, reg, value >> 8);
116 return dlfb_set_register(wrptr, reg+1, value);
59277b67
BT
117}
118
119/*
120 * This is kind of weird because the controller takes some
121 * register values in a different byte order than other registers.
122 */
4574203f 123static char *dlfb_set_register_16be(char *wrptr, u8 reg, u16 value)
59277b67 124{
4574203f
BT
125 wrptr = dlfb_set_register(wrptr, reg, value);
126 return dlfb_set_register(wrptr, reg+1, value >> 8);
59277b67
BT
127}
128
129/*
130 * LFSR is linear feedback shift register. The reason we have this is
131 * because the display controller needs to minimize the clock depth of
132 * various counters used in the display path. So this code reverses the
133 * provided value into the lfsr16 value by counting backwards to get
134 * the value that needs to be set in the hardware comparator to get the
135 * same actual count. This makes sense once you read above a couple of
136 * times and think about it from a hardware perspective.
137 */
138static u16 lfsr16(u16 actual_count)
139{
140 u32 lv = 0xFFFF; /* This is the lfsr value that the hw starts with */
141
142 while (actual_count--) {
143 lv = ((lv << 1) |
144 (((lv >> 15) ^ (lv >> 4) ^ (lv >> 2) ^ (lv >> 1)) & 1))
145 & 0xFFFF;
88e58b1a
RDI
146 }
147
59277b67
BT
148 return (u16) lv;
149}
150
151/*
152 * This does LFSR conversion on the value that is to be written.
153 * See LFSR explanation above for more detail.
154 */
4574203f 155static char *dlfb_set_register_lfsr16(char *wrptr, u8 reg, u16 value)
59277b67 156{
4574203f 157 return dlfb_set_register_16(wrptr, reg, lfsr16(value));
88e58b1a
RDI
158}
159
59277b67
BT
160/*
161 * This takes a standard fbdev screeninfo struct and all of its monitor mode
162 * details and converts them into the DisplayLink equivalent register commands.
163 */
4574203f 164static char *dlfb_set_vid_cmds(char *wrptr, struct fb_var_screeninfo *var)
88e58b1a 165{
59277b67
BT
166 u16 xds, yds;
167 u16 xde, yde;
168 u16 yec;
169
59277b67
BT
170 /* x display start */
171 xds = var->left_margin + var->hsync_len;
4574203f 172 wrptr = dlfb_set_register_lfsr16(wrptr, 0x01, xds);
59277b67
BT
173 /* x display end */
174 xde = xds + var->xres;
4574203f 175 wrptr = dlfb_set_register_lfsr16(wrptr, 0x03, xde);
59277b67
BT
176
177 /* y display start */
178 yds = var->upper_margin + var->vsync_len;
4574203f 179 wrptr = dlfb_set_register_lfsr16(wrptr, 0x05, yds);
59277b67
BT
180 /* y display end */
181 yde = yds + var->yres;
4574203f 182 wrptr = dlfb_set_register_lfsr16(wrptr, 0x07, yde);
59277b67
BT
183
184 /* x end count is active + blanking - 1 */
4574203f
BT
185 wrptr = dlfb_set_register_lfsr16(wrptr, 0x09,
186 xde + var->right_margin - 1);
59277b67
BT
187
188 /* libdlo hardcodes hsync start to 1 */
4574203f 189 wrptr = dlfb_set_register_lfsr16(wrptr, 0x0B, 1);
59277b67
BT
190
191 /* hsync end is width of sync pulse + 1 */
4574203f 192 wrptr = dlfb_set_register_lfsr16(wrptr, 0x0D, var->hsync_len + 1);
59277b67
BT
193
194 /* hpixels is active pixels */
4574203f 195 wrptr = dlfb_set_register_16(wrptr, 0x0F, var->xres);
88e58b1a 196
59277b67
BT
197 /* yendcount is vertical active + vertical blanking */
198 yec = var->yres + var->upper_margin + var->lower_margin +
199 var->vsync_len;
4574203f 200 wrptr = dlfb_set_register_lfsr16(wrptr, 0x11, yec);
88e58b1a 201
59277b67 202 /* libdlo hardcodes vsync start to 0 */
4574203f 203 wrptr = dlfb_set_register_lfsr16(wrptr, 0x13, 0);
59277b67
BT
204
205 /* vsync end is width of vsync pulse */
4574203f 206 wrptr = dlfb_set_register_lfsr16(wrptr, 0x15, var->vsync_len);
59277b67
BT
207
208 /* vpixels is active pixels */
4574203f 209 wrptr = dlfb_set_register_16(wrptr, 0x17, var->yres);
59277b67
BT
210
211 /* convert picoseconds to 5kHz multiple for pclk5k = x * 1E12/5k */
4574203f
BT
212 wrptr = dlfb_set_register_16be(wrptr, 0x1B,
213 200*1000*1000/var->pixclock);
59277b67
BT
214
215 return wrptr;
216}
217
218/*
219 * This takes a standard fbdev screeninfo struct that was fetched or prepared
220 * and then generates the appropriate command sequence that then drives the
221 * display controller.
222 */
223static int dlfb_set_video_mode(struct dlfb_data *dev,
224 struct fb_var_screeninfo *var)
225{
226 char *buf;
227 char *wrptr;
228 int retval = 0;
229 int writesize;
230
231 buf = dev->buf;
232
233 /*
234 * This first section has to do with setting the base address on the
235 * controller * associated with the display. There are 2 base
236 * pointers, currently, we only * use the 16 bpp segment.
237 */
4574203f
BT
238 wrptr = dlfb_vidreg_lock(buf);
239 wrptr = dlfb_set_color_depth(wrptr, 0x00);
59277b67 240 /* set base for 16bpp segment to 0 */
4574203f 241 wrptr = dlfb_set_base16bpp(wrptr, 0);
59277b67 242 /* set base for 8bpp segment to end of fb */
4574203f 243 wrptr = dlfb_set_base8bpp(wrptr, dev->info->fix.smem_len);
59277b67 244
4574203f
BT
245 wrptr = dlfb_set_vid_cmds(wrptr, var);
246 wrptr = dlfb_enable_hvsync(wrptr);
247 wrptr = dlfb_vidreg_unlock(wrptr);
59277b67
BT
248
249 writesize = wrptr - buf;
250
251 mutex_lock(&dev->bulk_mutex);
252 if (!dev->interface) { /* disconnect() was called */
253 mutex_unlock(&dev->bulk_mutex);
254 retval = -ENODEV;
255 goto error;
88e58b1a 256 }
59277b67
BT
257
258 retval = dlfb_bulk_msg(dev, writesize);
259 mutex_unlock(&dev->bulk_mutex);
260 if (retval) {
261 dev_err(&dev->udev->dev, "Problem %d with submit write bulk.\n",
262 retval);
263 goto error;
264 }
265
266 return 0;
267
268error:
269 return retval;
270}
271
59277b67
BT
272
273/*
274 * Query EDID from the handware, then hand it off to fbdev's edid parse
275 * routine which should give us back a filled in screeninfo structure.
276 */
277static int dlfb_get_var_from_edid(struct dlfb_data *dev,
278 struct fb_var_screeninfo *var)
279{
280 int ret;
281
282 dlfb_edid(dev);
283 ret = fb_parse_edid(dev->edid, var);
284
285 return ret;
88e58b1a
RDI
286}
287
4574203f 288static int dlfb_ops_mmap(struct fb_info *info, struct vm_area_struct *vma)
88e58b1a
RDI
289{
290 unsigned long start = vma->vm_start;
291 unsigned long size = vma->vm_end - vma->vm_start;
292 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
293 unsigned long page, pos;
294
295 printk("MMAP: %lu %u\n", offset + size, info->fix.smem_len);
296
f05e0575 297 if (offset + size > info->fix.smem_len)
88e58b1a 298 return -EINVAL;
88e58b1a
RDI
299
300 pos = (unsigned long)info->fix.smem_start + offset;
301
302 while (size > 0) {
303 page = vmalloc_to_pfn((void *)pos);
f05e0575 304 if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED))
88e58b1a 305 return -EAGAIN;
f05e0575 306
88e58b1a
RDI
307 start += PAGE_SIZE;
308 pos += PAGE_SIZE;
309 if (size > PAGE_SIZE)
310 size -= PAGE_SIZE;
311 else
312 size = 0;
313 }
314
315 vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
316 return 0;
317
318}
319
f05e0575 320/* ioctl structure */
88e58b1a
RDI
321struct dloarea {
322 int x, y;
323 int w, h;
7316bc55 324 int x2, y2;
88e58b1a
RDI
325};
326
88e58b1a
RDI
327static struct usb_driver dlfb_driver;
328
1d31a9ee 329/* thanks to Henrik Bjerregaard Pedersen for this function */
7316bc55
RDI
330static char *rle_compress16(uint16_t * src, char *dst, int rem)
331{
332
333 int rl;
334 uint16_t pix0;
335 char *end_if_raw = dst + 6 + 2 * rem;
336
1d31a9ee 337 dst += 6; /* header will be filled in if RLE is worth it */
7316bc55
RDI
338
339 while (rem && dst < end_if_raw) {
340 char *start = (char *)src;
341
342 pix0 = *src++;
343 rl = 1;
344 rem--;
345 while (rem && *src == pix0)
346 rem--, rl++, src++;
347 *dst++ = rl;
348 *dst++ = start[1];
349 *dst++ = start[0];
350 }
351
352 return dst;
353}
354
355/*
1d31a9ee
BT
356Thanks to Henrik Bjerregaard Pedersen for rle implementation
357and code refactoring. Next step is huffman compression.
7316bc55
RDI
358*/
359
88e58b1a
RDI
360static int
361image_blit(struct dlfb_data *dev_info, int x, int y, int width, int height,
362 char *data)
363{
364
365 int i, j, base;
366 int rem = width;
367 int ret;
368
7316bc55 369 int firstdiff, thistime;
88e58b1a
RDI
370
371 char *bufptr;
372
f05e0575 373 if (x + width > dev_info->info->var.xres)
88e58b1a 374 return -EINVAL;
88e58b1a 375
f05e0575 376 if (y + height > dev_info->info->var.yres)
88e58b1a 377 return -EINVAL;
88e58b1a
RDI
378
379 mutex_lock(&dev_info->bulk_mutex);
380
7316bc55
RDI
381 base =
382 dev_info->base16 + ((dev_info->info->var.xres * 2 * y) + (x * 2));
88e58b1a
RDI
383
384 data += (dev_info->info->var.xres * 2 * y) + (x * 2);
385
f05e0575 386 /* printk("IMAGE_BLIT\n"); */
88e58b1a
RDI
387
388 bufptr = dev_info->buf;
389
390 for (i = y; i < y + height; i++) {
391
392 if (dev_info->bufend - bufptr < BUF_HIGH_WATER_MARK) {
393 ret = dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
394 bufptr = dev_info->buf;
395 }
396
397 rem = width;
398
f05e0575 399 /* printk("WRITING LINE %d\n", i); */
88e58b1a
RDI
400
401 while (rem) {
402
403 if (dev_info->bufend - bufptr < BUF_HIGH_WATER_MARK) {
404 ret =
405 dlfb_bulk_msg(dev_info,
406 bufptr - dev_info->buf);
407 bufptr = dev_info->buf;
408 }
1d31a9ee 409 /* number of pixels to consider this time */
7316bc55
RDI
410 thistime = rem;
411 if (thistime > 255)
412 thistime = 255;
413
59277b67
BT
414 if (dev_info->backing_buffer) {
415 /* find first pixel that has changed */
416 firstdiff = -1;
417 for (j = 0; j < thistime * 2; j++) {
418 if (dev_info->backing_buffer
419 [base - dev_info->base16 + j]
420 != data[j]) {
421 firstdiff = j / 2;
422 break;
423 }
88e58b1a 424 }
59277b67
BT
425
426 } else {
427 firstdiff = 0;
428
7316bc55 429 }
88e58b1a 430
7316bc55
RDI
431 if (firstdiff >= 0) {
432 char *end_of_rle;
433
434 end_of_rle =
435 rle_compress16((uint16_t *) (data +
436 firstdiff * 2),
437 bufptr,
438 thistime - firstdiff);
439
440 if (end_of_rle <
441 bufptr + 6 + 2 * (thistime - firstdiff)) {
442 bufptr[0] = 0xAF;
443 bufptr[1] = 0x69;
444
445 bufptr[2] =
446 (char)((base +
447 firstdiff * 2) >> 16);
448 bufptr[3] =
449 (char)((base + firstdiff * 2) >> 8);
450 bufptr[4] =
451 (char)(base + firstdiff * 2);
452 bufptr[5] = thistime - firstdiff;
453
454 bufptr = end_of_rle;
455
456 } else {
1d31a9ee 457 /* fallback to raw (or other?) */
88e58b1a
RDI
458 *bufptr++ = 0xAF;
459 *bufptr++ = 0x68;
460
7316bc55
RDI
461 *bufptr++ =
462 (char)((base +
463 firstdiff * 2) >> 16);
464 *bufptr++ =
465 (char)((base + firstdiff * 2) >> 8);
466 *bufptr++ =
467 (char)(base + firstdiff * 2);
468 *bufptr++ = thistime - firstdiff;
7316bc55
RDI
469 for (j = firstdiff * 2;
470 j < thistime * 2; j += 2) {
471 *bufptr++ = data[j + 1];
472 *bufptr++ = data[j];
88e58b1a 473 }
88e58b1a 474 }
88e58b1a
RDI
475 }
476
7316bc55
RDI
477 base += thistime * 2;
478 data += thistime * 2;
479 rem -= thistime;
88e58b1a
RDI
480 }
481
59277b67
BT
482 if (dev_info->backing_buffer)
483 memcpy(dev_info->backing_buffer +
484 (base - dev_info->base16) -
485 (width * 2), data - (width * 2), width * 2);
88e58b1a
RDI
486
487 base += (dev_info->info->var.xres * 2) - (width * 2);
488 data += (dev_info->info->var.xres * 2) - (width * 2);
489
490 }
491
7316bc55 492 if (bufptr > dev_info->buf) {
88e58b1a 493 ret = dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
7316bc55 494 }
88e58b1a
RDI
495
496 mutex_unlock(&dev_info->bulk_mutex);
497
498 return base;
499
500}
501
502static int
503draw_rect(struct dlfb_data *dev_info, int x, int y, int width, int height,
504 unsigned char red, unsigned char green, unsigned char blue)
505{
506
507 int i, j, base;
508 int ret;
509 unsigned short col =
510 (((((red) & 0xF8) | ((green) >> 5)) & 0xFF) << 8) +
511 (((((green) & 0x1C) << 3) | ((blue) >> 3)) & 0xFF);
512 int rem = width;
513
514 char *bufptr;
515
f05e0575 516 if (x + width > dev_info->info->var.xres)
88e58b1a 517 return -EINVAL;
88e58b1a 518
f05e0575 519 if (y + height > dev_info->info->var.yres)
88e58b1a 520 return -EINVAL;
88e58b1a
RDI
521
522 mutex_lock(&dev_info->bulk_mutex);
523
524 base = dev_info->base16 + (dev_info->info->var.xres * 2 * y) + (x * 2);
525
526 bufptr = dev_info->buf;
527
528 for (i = y; i < y + height; i++) {
529
59277b67
BT
530 if (dev_info->backing_buffer) {
531 for (j = 0; j < width * 2; j += 2) {
532 dev_info->backing_buffer
533 [base - dev_info->base16 + j] =
534 (char)(col >> 8);
535 dev_info->backing_buffer
536 [base - dev_info->base16 + j + 1] =
537 (char)(col);
538 }
88e58b1a 539 }
59277b67 540
88e58b1a
RDI
541 if (dev_info->bufend - bufptr < BUF_HIGH_WATER_MARK) {
542 ret = dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
543 bufptr = dev_info->buf;
544 }
545
546 rem = width;
547
548 while (rem) {
549
550 if (dev_info->bufend - bufptr < BUF_HIGH_WATER_MARK) {
551 ret =
552 dlfb_bulk_msg(dev_info,
553 bufptr - dev_info->buf);
554 bufptr = dev_info->buf;
555 }
556
557 *bufptr++ = 0xAF;
558 *bufptr++ = 0x69;
559
560 *bufptr++ = (char)(base >> 16);
561 *bufptr++ = (char)(base >> 8);
562 *bufptr++ = (char)(base);
563
564 if (rem > 255) {
565 *bufptr++ = 255;
566 *bufptr++ = 255;
567 rem -= 255;
568 base += 255 * 2;
569 } else {
570 *bufptr++ = rem;
571 *bufptr++ = rem;
572 base += rem * 2;
573 rem = 0;
574 }
575
576 *bufptr++ = (char)(col >> 8);
577 *bufptr++ = (char)(col);
578
579 }
580
581 base += (dev_info->info->var.xres * 2) - (width * 2);
582
583 }
584
f05e0575 585 if (bufptr > dev_info->buf)
88e58b1a 586 ret = dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
88e58b1a
RDI
587
588 mutex_unlock(&dev_info->bulk_mutex);
589
590 return 1;
591}
592
7316bc55
RDI
593static void swapfb(struct dlfb_data *dev_info)
594{
595
596 int tmpbase;
597 char *bufptr;
598
599 mutex_lock(&dev_info->bulk_mutex);
600
601 tmpbase = dev_info->base16;
602
603 dev_info->base16 = dev_info->base16d;
604 dev_info->base16d = tmpbase;
605
606 bufptr = dev_info->buf;
607
608 bufptr = dlfb_set_register(bufptr, 0xFF, 0x00);
609
1d31a9ee 610 /* set addresses */
7316bc55
RDI
611 bufptr =
612 dlfb_set_register(bufptr, 0x20, (char)(dev_info->base16 >> 16));
613 bufptr = dlfb_set_register(bufptr, 0x21, (char)(dev_info->base16 >> 8));
614 bufptr = dlfb_set_register(bufptr, 0x22, (char)(dev_info->base16));
615
616 bufptr = dlfb_set_register(bufptr, 0xFF, 0x00);
617
618 dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
619
620 mutex_unlock(&dev_info->bulk_mutex);
621}
622
623static int copyfb(struct dlfb_data *dev_info)
624{
625 int base;
626 int source;
627 int rem;
628 int i, ret;
629
630 char *bufptr;
631
632 base = dev_info->base16d;
633
634 mutex_lock(&dev_info->bulk_mutex);
635
636 source = dev_info->base16;
637
638 bufptr = dev_info->buf;
639
640 for (i = 0; i < dev_info->info->var.yres; i++) {
641
642 if (dev_info->bufend - bufptr < BUF_HIGH_WATER_MARK) {
643 ret = dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
644 bufptr = dev_info->buf;
645 }
646
647 rem = dev_info->info->var.xres;
648
649 while (rem) {
650
651 if (dev_info->bufend - bufptr < BUF_HIGH_WATER_MARK) {
652 ret =
653 dlfb_bulk_msg(dev_info,
654 bufptr - dev_info->buf);
655 bufptr = dev_info->buf;
656
657 }
658
659 *bufptr++ = 0xAF;
660 *bufptr++ = 0x6A;
661
662 *bufptr++ = (char)(base >> 16);
663 *bufptr++ = (char)(base >> 8);
664 *bufptr++ = (char)(base);
665
666 if (rem > 255) {
667 *bufptr++ = 255;
668 *bufptr++ = (char)(source >> 16);
669 *bufptr++ = (char)(source >> 8);
670 *bufptr++ = (char)(source);
671
672 rem -= 255;
673 base += 255 * 2;
674 source += 255 * 2;
675
676 } else {
677 *bufptr++ = rem;
678 *bufptr++ = (char)(source >> 16);
679 *bufptr++ = (char)(source >> 8);
680 *bufptr++ = (char)(source);
681
682 base += rem * 2;
683 source += rem * 2;
684 rem = 0;
685 }
686 }
687 }
688
689 if (bufptr > dev_info->buf)
690 ret = dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
691
692 mutex_unlock(&dev_info->bulk_mutex);
693
694 return 1;
695
696}
697
88e58b1a
RDI
698static int
699copyarea(struct dlfb_data *dev_info, int dx, int dy, int sx, int sy,
700 int width, int height)
701{
88e58b1a
RDI
702 int base;
703 int source;
704 int rem;
705 int i, ret;
706
707 char *bufptr;
708
f05e0575 709 if (dx + width > dev_info->info->var.xres)
88e58b1a 710 return -EINVAL;
88e58b1a 711
f05e0575 712 if (dy + height > dev_info->info->var.yres)
88e58b1a 713 return -EINVAL;
88e58b1a
RDI
714
715 mutex_lock(&dev_info->bulk_mutex);
716
717 base =
718 dev_info->base16 + (dev_info->info->var.xres * 2 * dy) + (dx * 2);
719 source = (dev_info->info->var.xres * 2 * sy) + (sx * 2);
720
721 bufptr = dev_info->buf;
722
723 for (i = sy; i < sy + height; i++) {
724
7316bc55 725 memcpy(dev_info->backing_buffer + base - dev_info->base16,
88e58b1a
RDI
726 dev_info->backing_buffer + source, width * 2);
727
728 if (dev_info->bufend - bufptr < BUF_HIGH_WATER_MARK) {
729 ret = dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
730 bufptr = dev_info->buf;
731 }
732
733 rem = width;
734
735 while (rem) {
736
737 if (dev_info->bufend - bufptr < BUF_HIGH_WATER_MARK) {
738 ret =
739 dlfb_bulk_msg(dev_info,
740 bufptr - dev_info->buf);
741 bufptr = dev_info->buf;
742 }
743
744 *bufptr++ = 0xAF;
745 *bufptr++ = 0x6A;
746
747 *bufptr++ = (char)(base >> 16);
748 *bufptr++ = (char)(base >> 8);
749 *bufptr++ = (char)(base);
750
751 if (rem > 255) {
752 *bufptr++ = 255;
753 *bufptr++ = (char)(source >> 16);
754 *bufptr++ = (char)(source >> 8);
755 *bufptr++ = (char)(source);
756
757 rem -= 255;
758 base += 255 * 2;
759 source += 255 * 2;
760
761 } else {
762 *bufptr++ = rem;
763 *bufptr++ = (char)(source >> 16);
764 *bufptr++ = (char)(source >> 8);
765 *bufptr++ = (char)(source);
766
767 base += rem * 2;
768 source += rem * 2;
769 rem = 0;
770 }
88e58b1a
RDI
771 }
772
773 base += (dev_info->info->var.xres * 2) - (width * 2);
774 source += (dev_info->info->var.xres * 2) - (width * 2);
88e58b1a
RDI
775 }
776
f05e0575 777 if (bufptr > dev_info->buf)
88e58b1a 778 ret = dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
88e58b1a
RDI
779
780 mutex_unlock(&dev_info->bulk_mutex);
781
782 return 1;
783}
784
4574203f
BT
785static void dlfb_ops_copyarea(struct fb_info *info,
786 const struct fb_copyarea *area)
88e58b1a 787{
88e58b1a
RDI
788 struct dlfb_data *dev = info->par;
789
790 copyarea(dev, area->dx, area->dy, area->sx, area->sy, area->width,
791 area->height);
88e58b1a
RDI
792}
793
4574203f
BT
794static void dlfb_ops_imageblit(struct fb_info *info,
795 const struct fb_image *image)
88e58b1a 796{
88e58b1a
RDI
797 int ret;
798 struct dlfb_data *dev = info->par;
88e58b1a
RDI
799 cfb_imageblit(info, image);
800 ret =
801 image_blit(dev, image->dx, image->dy, image->width, image->height,
802 info->screen_base);
88e58b1a
RDI
803}
804
4574203f 805static void dlfb_ops_fillrect(struct fb_info *info,
7316bc55 806 const struct fb_fillrect *region)
88e58b1a
RDI
807{
808
809 unsigned char red, green, blue;
810 struct dlfb_data *dev = info->par;
811
812 memcpy(&red, &region->color, 1);
813 memcpy(&green, &region->color + 1, 1);
814 memcpy(&blue, &region->color + 2, 1);
815 draw_rect(dev, region->dx, region->dy, region->width, region->height,
816 red, green, blue);
f05e0575 817 /* printk("FILL RECT %d %d !!!\n", region->dx, region->dy); */
88e58b1a
RDI
818
819}
820
7d9485e2
BT
821static void dlfb_get_edid(struct dlfb_data *dev)
822{
823 int i;
824 int ret;
825 char rbuf[2];
826
827 for (i = 0; i < sizeof(dev->edid); i++) {
828 ret = usb_control_msg(dev->udev,
829 usb_rcvctrlpipe(dev->udev, 0), (0x02),
830 (0x80 | (0x02 << 5)), i << 8, 0xA1, rbuf, 2,
831 0);
832 dev->edid[i] = rbuf[1];
833 }
834}
835
4574203f
BT
836static int dlfb_ops_ioctl(struct fb_info *info, unsigned int cmd,
837 unsigned long arg)
88e58b1a 838{
88e58b1a 839 struct dlfb_data *dev_info = info->par;
7316bc55 840 struct dloarea *area = NULL;
88e58b1a 841
7316bc55
RDI
842 if (cmd == 0xAD) {
843 char *edid = (char *)arg;
844 dlfb_edid(dev_info);
845 if (copy_to_user(edid, dev_info->edid, 128)) {
846 return -EFAULT;
847 }
848 return 0;
849 }
850
851 if (cmd == 0xAA || cmd == 0xAB || cmd == 0xAC) {
88e58b1a
RDI
852
853 area = (struct dloarea *)arg;
854
855 if (area->x < 0)
856 area->x = 0;
857
858 if (area->x > info->var.xres)
859 area->x = info->var.xres;
860
861 if (area->y < 0)
862 area->y = 0;
863
864 if (area->y > info->var.yres)
865 area->y = info->var.yres;
7316bc55 866 }
88e58b1a 867
7316bc55 868 if (cmd == 0xAA) {
88e58b1a
RDI
869 image_blit(dev_info, area->x, area->y, area->w, area->h,
870 info->screen_base);
871 }
7316bc55
RDI
872 if (cmd == 0xAC) {
873 copyfb(dev_info);
874 image_blit(dev_info, area->x, area->y, area->w, area->h,
875 info->screen_base);
876 swapfb(dev_info);
877 } else if (cmd == 0xAB) {
878
879 if (area->x2 < 0)
880 area->x2 = 0;
881
882 if (area->y2 < 0)
883 area->y2 = 0;
884
885 copyarea(dev_info,
886 area->x2, area->y2, area->x, area->y, area->w,
887 area->h);
888 }
88e58b1a
RDI
889 return 0;
890}
891
f05e0575 892/* taken from vesafb */
88e58b1a 893static int
4574203f 894dlfb_ops_setcolreg(unsigned regno, unsigned red, unsigned green,
88e58b1a
RDI
895 unsigned blue, unsigned transp, struct fb_info *info)
896{
897 int err = 0;
898
899 if (regno >= info->cmap.len)
900 return 1;
901
902 if (regno < 16) {
903 if (info->var.red.offset == 10) {
904 /* 1:5:5:5 */
905 ((u32 *) (info->pseudo_palette))[regno] =
906 ((red & 0xf800) >> 1) |
907 ((green & 0xf800) >> 6) | ((blue & 0xf800) >> 11);
908 } else {
909 /* 0:5:6:5 */
910 ((u32 *) (info->pseudo_palette))[regno] =
911 ((red & 0xf800)) |
912 ((green & 0xfc00) >> 5) | ((blue & 0xf800) >> 11);
913 }
914 }
915
916 return err;
917}
918
4574203f 919static int dlfb_ops_release(struct fb_info *info, int user)
88e58b1a
RDI
920{
921 struct dlfb_data *dev_info = info->par;
922 image_blit(dev_info, 0, 0, info->var.xres, info->var.yres,
923 info->screen_base);
924 return 0;
925}
926
4a4854dd
BT
927/*
928 * Called when all client interfaces to start transactions have been disabled,
929 * and all references to our device instance (dlfb_data) are released.
930 * Every transaction must have a reference, so we know are fully spun down
931 */
932static void dlfb_delete(struct kref *kref)
933{
934 struct dlfb_data *dev = container_of(kref, struct dlfb_data, kref);
935
936 if (dev->backing_buffer)
937 vfree(dev->backing_buffer);
938
939 kfree(dev);
940}
941
7d9485e2
BT
942/*
943 * Check whether a video mode is supported by the DisplayLink chip
944 * We start from monitor's modes, so don't need to filter that here
945 */
946static int dlfb_is_valid_mode(struct fb_videomode *mode,
947 struct fb_info *info)
948{
949 struct dlfb_data *dev = info->par;
950
951 if (mode->xres * mode->yres > dev->sku_pixel_limit)
952 return 0;
953
954 return 1;
955}
956
957static void dlfb_var_color_format(struct fb_var_screeninfo *var)
958{
959 const struct fb_bitfield red = { 11, 5, 0 };
960 const struct fb_bitfield green = { 5, 6, 0 };
961 const struct fb_bitfield blue = { 0, 5, 0 };
962
963 var->bits_per_pixel = 16;
964 var->red = red;
965 var->green = green;
966 var->blue = blue;
967}
968
4574203f 969static int dlfb_ops_blank(int blank_mode, struct fb_info *info)
f05e0575 970{
7316bc55
RDI
971 struct dlfb_data *dev_info = info->par;
972 char *bufptr = dev_info->buf;
973
974 bufptr = dlfb_set_register(bufptr, 0xFF, 0x00);
975 if (blank_mode != FB_BLANK_UNBLANK) {
976 bufptr = dlfb_set_register(bufptr, 0x1F, 0x01);
977 } else {
978 bufptr = dlfb_set_register(bufptr, 0x1F, 0x00);
979 }
980 bufptr = dlfb_set_register(bufptr, 0xFF, 0xFF);
981
982 dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
983
88e58b1a
RDI
984 return 0;
985}
986
987static struct fb_ops dlfb_ops = {
4574203f
BT
988 .fb_setcolreg = dlfb_ops_setcolreg,
989 .fb_fillrect = dlfb_ops_fillrect,
990 .fb_copyarea = dlfb_ops_copyarea,
991 .fb_imageblit = dlfb_ops_imageblit,
992 .fb_mmap = dlfb_ops_mmap,
993 .fb_ioctl = dlfb_ops_ioctl,
994 .fb_release = dlfb_ops_release,
995 .fb_blank = dlfb_ops_blank,
88e58b1a
RDI
996};
997
7d9485e2
BT
998/*
999 * Calls dlfb_get_edid() to query the EDID of attached monitor via usb cmds
1000 * Then parses EDID into three places used by various parts of fbdev:
1001 * fb_var_screeninfo contains the timing of the monitor's preferred mode
1002 * fb_info.monspecs is full parsed EDID info, including monspecs.modedb
1003 * fb_info.modelist is a linked list of all monitor & VESA modes which work
1004 *
1005 * If EDID is not readable/valid, then modelist is all VESA modes,
1006 * monspecs is NULL, and fb_var_screeninfo is set to safe VESA mode
1007 * Returns 0 if EDID parses successfully
1008 */
1009static int dlfb_parse_edid(struct dlfb_data *dev,
1010 struct fb_var_screeninfo *var,
1011 struct fb_info *info)
1012{
1013 int i;
1014 const struct fb_videomode *default_vmode = NULL;
1015 int result = 0;
1016
1017 fb_destroy_modelist(&info->modelist);
1018 memset(&info->monspecs, 0, sizeof(info->monspecs));
1019
1020 dlfb_get_edid(dev);
1021 fb_edid_to_monspecs(dev->edid, &info->monspecs);
1022
1023 if (info->monspecs.modedb_len > 0) {
1024
1025 for (i = 0; i < info->monspecs.modedb_len; i++) {
1026 if (dlfb_is_valid_mode(&info->monspecs.modedb[i], info))
1027 fb_add_videomode(&info->monspecs.modedb[i],
1028 &info->modelist);
1029 }
1030
1031 default_vmode = fb_find_best_display(&info->monspecs,
1032 &info->modelist);
1033 } else {
1034 struct fb_videomode fb_vmode = {0};
1035
1036 dl_err("Unable to get valid EDID from device/display\n");
1037 result = 1;
1038
1039 /*
1040 * Add the standard VESA modes to our modelist
1041 * Since we don't have EDID, there may be modes that
1042 * overspec monitor and/or are incorrect aspect ratio, etc.
1043 * But at least the user has a chance to choose
1044 */
1045 for (i = 0; i < VESA_MODEDB_SIZE; i++) {
1046 if (dlfb_is_valid_mode((struct fb_videomode *)
1047 &vesa_modes[i], info))
1048 fb_add_videomode(&vesa_modes[i],
1049 &info->modelist);
1050 }
1051
1052 /*
1053 * default to resolution safe for projectors
1054 * (since they are most common case without EDID)
1055 */
1056 fb_vmode.xres = 800;
1057 fb_vmode.yres = 600;
1058 fb_vmode.refresh = 60;
1059 default_vmode = fb_find_nearest_mode(&fb_vmode,
1060 &info->modelist);
1061 }
1062
1063 fb_videomode_to_var(var, default_vmode);
1064 dlfb_var_color_format(var);
1065
1066 return result;
1067}
1068
1069static ssize_t metrics_bytes_rendered_show(struct device *fbdev,
1070 struct device_attribute *a, char *buf) {
1071 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1072 struct dlfb_data *dev = fb_info->par;
1073 return snprintf(buf, PAGE_SIZE, "%u\n",
1074 atomic_read(&dev->bytes_rendered));
1075}
1076
1077static ssize_t metrics_bytes_identical_show(struct device *fbdev,
1078 struct device_attribute *a, char *buf) {
1079 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1080 struct dlfb_data *dev = fb_info->par;
1081 return snprintf(buf, PAGE_SIZE, "%u\n",
1082 atomic_read(&dev->bytes_identical));
1083}
1084
1085static ssize_t metrics_bytes_sent_show(struct device *fbdev,
1086 struct device_attribute *a, char *buf) {
1087 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1088 struct dlfb_data *dev = fb_info->par;
1089 return snprintf(buf, PAGE_SIZE, "%u\n",
1090 atomic_read(&dev->bytes_sent));
1091}
1092
1093static ssize_t metrics_cpu_kcycles_used_show(struct device *fbdev,
1094 struct device_attribute *a, char *buf) {
1095 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1096 struct dlfb_data *dev = fb_info->par;
1097 return snprintf(buf, PAGE_SIZE, "%u\n",
1098 atomic_read(&dev->cpu_kcycles_used));
1099}
1100
1101static ssize_t metrics_misc_show(struct device *fbdev,
1102 struct device_attribute *a, char *buf) {
1103 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1104 struct dlfb_data *dev = fb_info->par;
1105 return snprintf(buf, PAGE_SIZE,
1106 "Calls to\ndamage: %u\nblit: %u\n"
1107 "defio faults: %u\ncopy: %u\n"
1108 "fill: %u\n\n"
1109 "active framebuffer clients: %d\n"
1110 "urbs available %d(%d)\n"
1111 "Shadow framebuffer in use? %s\n"
1112 "Any lost pixels? %s\n",
1113 atomic_read(&dev->damage_count),
1114 atomic_read(&dev->blit_count),
1115 atomic_read(&dev->defio_fault_count),
1116 atomic_read(&dev->copy_count),
1117 atomic_read(&dev->fill_count),
1118 dev->fb_count,
1119 dev->urbs.available, dev->urbs.limit_sem.count,
1120 (dev->backing_buffer) ? "yes" : "no",
1121 atomic_read(&dev->lost_pixels) ? "yes" : "no");
1122}
1123
1124static ssize_t edid_show(struct kobject *kobj, struct bin_attribute *a,
1125 char *buf, loff_t off, size_t count) {
1126 struct device *fbdev = container_of(kobj, struct device, kobj);
1127 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1128 struct dlfb_data *dev = fb_info->par;
1129 char *edid = &dev->edid[0];
1130 const size_t size = sizeof(dev->edid);
1131
1132 if (dlfb_parse_edid(dev, &fb_info->var, fb_info))
1133 return 0;
1134
1135 if (off >= size)
1136 return 0;
1137
1138 if (off + count > size)
1139 count = size - off;
1140 memcpy(buf, edid + off, count);
1141
1142 return count;
1143}
1144
1145
1146static ssize_t metrics_reset_store(struct device *fbdev,
1147 struct device_attribute *attr,
1148 const char *buf, size_t count)
1149{
1150 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1151 struct dlfb_data *dev = fb_info->par;
1152
1153 atomic_set(&dev->bytes_rendered, 0);
1154 atomic_set(&dev->bytes_identical, 0);
1155 atomic_set(&dev->bytes_sent, 0);
1156 atomic_set(&dev->cpu_kcycles_used, 0);
1157 atomic_set(&dev->blit_count, 0);
1158 atomic_set(&dev->copy_count, 0);
1159 atomic_set(&dev->fill_count, 0);
1160 atomic_set(&dev->defio_fault_count, 0);
1161 atomic_set(&dev->damage_count, 0);
1162
1163 return count;
1164}
1165
1166static ssize_t use_defio_show(struct device *fbdev,
1167 struct device_attribute *a, char *buf) {
1168 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1169 struct dlfb_data *dev = fb_info->par;
1170 return snprintf(buf, PAGE_SIZE, "%d\n",
1171 atomic_read(&dev->use_defio));
1172}
1173
1174static ssize_t use_defio_store(struct device *fbdev,
1175 struct device_attribute *attr,
1176 const char *buf, size_t count)
1177{
1178 struct fb_info *fb_info = dev_get_drvdata(fbdev);
1179 struct dlfb_data *dev = fb_info->par;
1180
1181 if (count > 0) {
1182 if (buf[0] == '0')
1183 atomic_set(&dev->use_defio, 0);
1184 if (buf[0] == '1')
1185 atomic_set(&dev->use_defio, 1);
1186 }
1187 return count;
1188}
1189
1190static struct bin_attribute edid_attr = {
1191 .attr.name = "edid",
1192 .attr.mode = 0444,
1193 .size = 128,
1194 .read = edid_show,
1195};
1196
1197static struct device_attribute fb_device_attrs[] = {
1198 __ATTR_RO(metrics_bytes_rendered),
1199 __ATTR_RO(metrics_bytes_identical),
1200 __ATTR_RO(metrics_bytes_sent),
1201 __ATTR_RO(metrics_cpu_kcycles_used),
1202 __ATTR_RO(metrics_misc),
1203 __ATTR(metrics_reset, S_IWUGO, NULL, metrics_reset_store),
1204 __ATTR_RW(use_defio),
1205};
1206
cc403dc6
BT
1207/*
1208 * This is necessary before we can communicate with the display controller.
1209 */
1210static int dlfb_select_std_channel(struct dlfb_data *dev)
1211{
1212 int ret;
1213 u8 set_def_chn[] = { 0x57, 0xCD, 0xDC, 0xA7,
1214 0x1C, 0x88, 0x5E, 0x15,
1215 0x60, 0xFE, 0xC6, 0x97,
1216 0x16, 0x3D, 0x47, 0xF2 };
1217
1218 ret = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
1219 NR_USB_REQUEST_CHANNEL,
1220 (USB_DIR_OUT | USB_TYPE_VENDOR), 0, 0,
1221 set_def_chn, sizeof(set_def_chn), USB_CTRL_SET_TIMEOUT);
1222 return ret;
1223}
1224
59277b67
BT
1225static int dlfb_probe(struct usb_interface *interface,
1226 const struct usb_device_id *id)
88e58b1a 1227{
59277b67
BT
1228 struct device *mydev;
1229 struct usb_device *usbdev;
1230 struct dlfb_data *dev;
88e58b1a 1231 struct fb_info *info;
59277b67
BT
1232 int videomemorysize;
1233 unsigned char *videomemory;
1234 int retval = -ENOMEM;
1235 struct fb_var_screeninfo *var;
1236 struct fb_bitfield red = { 11, 5, 0 };
1237 struct fb_bitfield green = { 5, 6, 0 };
1238 struct fb_bitfield blue = { 0, 5, 0 };
1239
1240 usbdev = usb_get_dev(interface_to_usbdev(interface));
1241 mydev = &usbdev->dev;
1242
1243 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1244 if (dev == NULL) {
1245 dev_err(mydev, "failed alloc of dev struct\n");
1246 goto err_devalloc;
88e58b1a 1247 }
88e58b1a 1248
59277b67
BT
1249 mutex_init(&dev->bulk_mutex);
1250 dev->udev = usbdev;
4a4854dd 1251 dev->gdev = &usbdev->dev; /* our generic struct device * */
59277b67
BT
1252 dev->interface = interface;
1253 usb_set_intfdata(interface, dev);
1254
1255 dev_info(mydev, "dlfb_probe: setting up DisplayLink device\n");
1256
1257 /*
1258 * TODO: replace single 64K buffer with buffer list
1259 * and async dispatch
1260 */
1261 dev->buf = kmalloc(BUF_SIZE, GFP_KERNEL);
1262 if (dev->buf == NULL) {
1263 dev_err(mydev, "unable to allocate memory for dlfb commands\n");
1264 goto err_usballoc;
88e58b1a 1265 }
59277b67
BT
1266 dev->bufend = dev->buf + BUF_SIZE;
1267
1268 dev->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
1269 usb_fill_bulk_urb(dev->tx_urb, dev->udev,
1270 usb_sndbulkpipe(dev->udev, 1), dev->buf, 0,
1271 dlfb_bulk_callback, dev);
1272
1273 /* allocates framebuffer driver structure, not framebuffer memory */
1274 info = framebuffer_alloc(0, mydev);
1275 if (!info)
1276 goto err_fballoc;
1277
1278 dev->info = info;
1279 info->par = dev;
1280 info->pseudo_palette = dev->pseudo_palette;
1281
1282 var = &info->var;
1283 retval = dlfb_get_var_from_edid(dev, var);
1284 if (retval) {
1285 /* had a problem getting edid. so fallback to 640x480 */
1286 dev_err(mydev, "Problem %d with EDID.\n", retval);
1287 var->xres = 640;
1288 var->yres = 480;
7316bc55 1289 }
88e58b1a 1290
59277b67
BT
1291 /*
1292 * ok, now that we've got the size info, we can alloc our framebuffer.
1293 * We are using 16bpp.
1294 */
1295 info->var.bits_per_pixel = 16;
1296 info->fix = dlfb_fix;
1297 info->fix.line_length = var->xres * (var->bits_per_pixel / 8);
1298 videomemorysize = info->fix.line_length * var->yres;
1299
1300 /*
1301 * The big chunk of system memory we use as a virtual framebuffer.
1302 * Pages don't need to be set RESERVED (non-swap) immediately on 2.6
1303 * remap_pfn_page() syscall in our mmap and/or defio will handle.
1304 */
1305 videomemory = vmalloc(videomemorysize);
1306 if (!videomemory)
1307 goto err_vidmem;
1308 memset(videomemory, 0, videomemorysize);
1309
1310 info->screen_base = videomemory;
1311 info->fix.smem_len = PAGE_ALIGN(videomemorysize);
1312 info->fix.smem_start = (unsigned long) videomemory;
88e58b1a
RDI
1313 info->flags =
1314 FBINFO_DEFAULT | FBINFO_READS_FAST | FBINFO_HWACCEL_IMAGEBLIT |
1315 FBINFO_HWACCEL_COPYAREA | FBINFO_HWACCEL_FILLRECT;
88e58b1a 1316
59277b67
BT
1317 /*
1318 * Second framebuffer copy, mirroring the state of the framebuffer
1319 * on the physical USB device. We can function without this.
1320 * But with imperfect damage info we may end up sending pixels over USB
1321 * that were, in fact, unchanged -- wasting limited USB bandwidth
1322 */
1323 dev->backing_buffer = vmalloc(dev->screen_size);
1324 if (!dev->backing_buffer)
1325 dev_info(mydev, "No backing buffer allocated!\n");
f05e0575 1326
59277b67 1327 info->fbops = &dlfb_ops;
88e58b1a 1328
59277b67
BT
1329 var->vmode = FB_VMODE_NONINTERLACED;
1330 var->red = red;
1331 var->green = green;
1332 var->blue = blue;
88e58b1a 1333
59277b67
BT
1334 /*
1335 * TODO: Enable FB_CONFIG_DEFIO support
88e58b1a 1336
59277b67
BT
1337 info->fbdefio = &dlfb_defio;
1338 fb_deferred_io_init(info);
88e58b1a 1339
59277b67 1340 */
88e58b1a 1341
59277b67
BT
1342 retval = fb_alloc_cmap(&info->cmap, 256, 0);
1343 if (retval < 0) {
1344 dev_err(mydev, "Failed to allocate colormap\n");
1345 goto err_cmap;
7316bc55 1346 }
88e58b1a 1347
59277b67
BT
1348 dlfb_select_std_channel(dev);
1349 dlfb_set_video_mode(dev, var);
1350 /* TODO: dlfb_dpy_update(dev); */
88e58b1a 1351
59277b67
BT
1352 retval = register_framebuffer(info);
1353 if (retval < 0)
1354 goto err_regfb;
88e58b1a 1355
59277b67
BT
1356 /* paint "successful" green screen */
1357 draw_rect(dev, 0, 0, dev->info->var.xres,
1358 dev->info->var.yres, 0x30, 0xff, 0x30);
88e58b1a 1359
59277b67
BT
1360 dev_info(mydev, "DisplayLink USB device %d now attached, "
1361 "using %dK of memory\n", info->node,
1362 ((dev->backing_buffer) ?
1363 videomemorysize * 2 : videomemorysize) >> 10);
88e58b1a
RDI
1364 return 0;
1365
59277b67 1366err_regfb:
88e58b1a 1367 fb_dealloc_cmap(&info->cmap);
59277b67
BT
1368err_cmap:
1369 /* TODO: fb_deferred_io_cleanup(info); */
1370 vfree(videomemory);
1371err_vidmem:
88e58b1a 1372 framebuffer_release(info);
59277b67
BT
1373err_fballoc:
1374 kfree(dev->buf);
1375err_usballoc:
88e58b1a 1376 usb_set_intfdata(interface, NULL);
59277b67
BT
1377 usb_put_dev(dev->udev);
1378 kfree(dev);
1379err_devalloc:
1380 return retval;
88e58b1a
RDI
1381}
1382
1383static void dlfb_disconnect(struct usb_interface *interface)
1384{
59277b67
BT
1385 struct dlfb_data *dev;
1386 struct fb_info *info;
88e58b1a 1387
59277b67 1388 dev = usb_get_intfdata(interface);
88e58b1a 1389 usb_set_intfdata(interface, NULL);
59277b67
BT
1390 usb_put_dev(dev->udev);
1391
1392 /*
1393 * TODO: since, upon usb disconnect(), usb will cancel in-flight urbs
1394 * and error out any new ones, look at eliminating need for mutex
1395 */
1396 mutex_lock(&dev->bulk_mutex);
1397 dev->interface = NULL;
1398 info = dev->info;
1399 mutex_unlock(&dev->bulk_mutex);
1400
1401 if (info) {
1402 dev_info(&interface->dev, "Detaching DisplayLink device %d.\n",
1403 info->node);
1404 unregister_framebuffer(info);
1405 fb_dealloc_cmap(&info->cmap);
1406 /* TODO: fb_deferred_io_cleanup(info); */
1407 fb_dealloc_cmap(&info->cmap);
1408 vfree((void __force *)info->screen_base);
1409 framebuffer_release(info);
88e58b1a
RDI
1410 }
1411
59277b67
BT
1412 if (dev->backing_buffer)
1413 vfree(dev->backing_buffer);
88e58b1a 1414
59277b67 1415 kfree(dev);
88e58b1a
RDI
1416}
1417
1418static struct usb_driver dlfb_driver = {
1419 .name = "udlfb",
1420 .probe = dlfb_probe,
1421 .disconnect = dlfb_disconnect,
1422 .id_table = id_table,
1423};
1424
1425static int __init dlfb_init(void)
1426{
1427 int res;
1428
88e58b1a
RDI
1429 res = usb_register(&dlfb_driver);
1430 if (res)
1431 err("usb_register failed. Error number %d", res);
1432
1433 printk("VMODES initialized\n");
1434
1435 return res;
1436}
1437
1438static void __exit dlfb_exit(void)
1439{
1440 usb_deregister(&dlfb_driver);
1441}
1442
1443module_init(dlfb_init);
1444module_exit(dlfb_exit);
1445
4a4854dd
BT
1446static void dlfb_urb_completion(struct urb *urb)
1447{
1448 struct urb_node *unode = urb->context;
1449 struct dlfb_data *dev = unode->dev;
1450 unsigned long flags;
1451
1452 /* sync/async unlink faults aren't errors */
1453 if (urb->status) {
1454 if (!(urb->status == -ENOENT ||
1455 urb->status == -ECONNRESET ||
1456 urb->status == -ESHUTDOWN)) {
1457 dl_err("%s - nonzero write bulk status received: %d\n",
1458 __func__, urb->status);
1459 atomic_set(&dev->lost_pixels, 1);
1460 }
1461 }
1462
1463 urb->transfer_buffer_length = dev->urbs.size; /* reset to actual */
1464
1465 spin_lock_irqsave(&dev->urbs.lock, flags);
1466 list_add_tail(&unode->entry, &dev->urbs.list);
1467 dev->urbs.available++;
1468 spin_unlock_irqrestore(&dev->urbs.lock, flags);
1469
1470 up(&dev->urbs.limit_sem);
1471}
1472
1473static void dlfb_free_urb_list(struct dlfb_data *dev)
1474{
1475 int count = dev->urbs.count;
1476 struct list_head *node;
1477 struct urb_node *unode;
1478 struct urb *urb;
1479 int ret;
1480 unsigned long flags;
1481
1482 dl_notice("Waiting for completes and freeing all render urbs\n");
1483
1484 /* keep waiting and freeing, until we've got 'em all */
1485 while (count--) {
1486 /* Timeout means a memory leak and/or fault */
1487 ret = down_timeout(&dev->urbs.limit_sem, FREE_URB_TIMEOUT);
1488 if (ret) {
1489 BUG_ON(ret);
1490 break;
1491 }
1492 spin_lock_irqsave(&dev->urbs.lock, flags);
1493
1494 node = dev->urbs.list.next; /* have reserved one with sem */
1495 list_del_init(node);
1496
1497 spin_unlock_irqrestore(&dev->urbs.lock, flags);
1498
1499 unode = list_entry(node, struct urb_node, entry);
1500 urb = unode->urb;
1501
1502 /* Free each separately allocated piece */
1503 usb_buffer_free(urb->dev, dev->urbs.size,
1504 urb->transfer_buffer, urb->transfer_dma);
1505 usb_free_urb(urb);
1506 kfree(node);
1507 }
1508
1509 kref_put(&dev->kref, dlfb_delete);
1510
1511}
1512
1513static int dlfb_alloc_urb_list(struct dlfb_data *dev, int count, size_t size)
1514{
1515 int i = 0;
1516 struct urb *urb;
1517 struct urb_node *unode;
1518 char *buf;
1519
1520 spin_lock_init(&dev->urbs.lock);
1521
1522 dev->urbs.size = size;
1523 INIT_LIST_HEAD(&dev->urbs.list);
1524
1525 while (i < count) {
1526 unode = kzalloc(sizeof(struct urb_node), GFP_KERNEL);
1527 if (!unode)
1528 break;
1529 unode->dev = dev;
1530
1531 urb = usb_alloc_urb(0, GFP_KERNEL);
1532 if (!urb) {
1533 kfree(unode);
1534 break;
1535 }
1536 unode->urb = urb;
1537
1538 buf = usb_buffer_alloc(dev->udev, MAX_TRANSFER, GFP_KERNEL,
1539 &urb->transfer_dma);
1540 if (!buf) {
1541 kfree(unode);
1542 usb_free_urb(urb);
1543 break;
1544 }
1545
1546 /* urb->transfer_buffer_length set to actual before submit */
1547 usb_fill_bulk_urb(urb, dev->udev, usb_sndbulkpipe(dev->udev, 1),
1548 buf, size, dlfb_urb_completion, unode);
1549 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1550
1551 list_add_tail(&unode->entry, &dev->urbs.list);
1552
1553 i++;
1554 }
1555
1556 sema_init(&dev->urbs.limit_sem, i);
1557 dev->urbs.count = i;
1558 dev->urbs.available = i;
1559
1560 kref_get(&dev->kref); /* released in free_render_urbs() */
1561
1562 dl_notice("allocated %d %d byte urbs \n", i, (int) size);
1563
1564 return i;
1565}
1566
1567static struct urb *dlfb_get_urb(struct dlfb_data *dev)
1568{
1569 int ret = 0;
1570 struct list_head *entry;
1571 struct urb_node *unode;
1572 struct urb *urb = NULL;
1573 unsigned long flags;
1574
1575 /* Wait for an in-flight buffer to complete and get re-queued */
1576 ret = down_timeout(&dev->urbs.limit_sem, GET_URB_TIMEOUT);
1577 if (ret) {
1578 atomic_set(&dev->lost_pixels, 1);
1579 dl_err("wait for urb interrupted: %x\n", ret);
1580 goto error;
1581 }
1582
1583 spin_lock_irqsave(&dev->urbs.lock, flags);
1584
1585 BUG_ON(list_empty(&dev->urbs.list)); /* reserved one with limit_sem */
1586 entry = dev->urbs.list.next;
1587 list_del_init(entry);
1588 dev->urbs.available--;
1589
1590 spin_unlock_irqrestore(&dev->urbs.lock, flags);
1591
1592 unode = list_entry(entry, struct urb_node, entry);
1593 urb = unode->urb;
1594
1595error:
1596 return urb;
1597}
1598
1599static int dlfb_submit_urb(struct dlfb_data *dev, struct urb *urb, size_t len)
1600{
1601 int ret;
1602
1603 BUG_ON(len > dev->urbs.size);
1604
1605 urb->transfer_buffer_length = len; /* set to actual payload len */
1606 ret = usb_submit_urb(urb, GFP_KERNEL);
1607 if (ret) {
1608 dlfb_urb_completion(urb); /* because no one else will */
1609 atomic_set(&dev->lost_pixels, 1);
1610 dl_err("usb_submit_urb error %x\n", ret);
1611 }
1612 return ret;
1613}
1614
59277b67
BT
1615MODULE_AUTHOR("Roberto De Ioris <roberto@unbit.it>, "
1616 "Jaya Kumar <jayakumar.lkml@gmail.com>");
88e58b1a
RDI
1617MODULE_DESCRIPTION(DRIVER_VERSION);
1618MODULE_LICENSE("GPL");