blob: 592b1857924f61e11507d346ff3cac200baa9469 [file] [log] [blame]
Bernie Thompson59277b62009-11-24 15:52:21 -08001/*
2 * udlfb.c -- Framebuffer driver for DisplayLink USB controller
3 *
4 * Copyright (C) 2009 Roberto De Ioris <roberto@unbit.it>
5 * Copyright (C) 2009 Jaya Kumar <jayakumar.lkml@gmail.com>
6 *
7 * This file is subject to the terms and conditions of the GNU General Public
8 * License v2. See the file COPYING in the main directory of this archive for
9 * more details.
10 *
11 * Layout is based on skeletonfb by James Simmons and Geert Uytterhoeven,
12 * usb-skeleton by GregKH.
13 *
14 * Device-specific portions based on information from Displaylink, with work
15 * from Florian Echtler, Henrik Bjerregaard Pedersen, and others.
16 */
Roberto De Ioris88e58b12009-06-03 14:03:06 -070017
18#include <linux/module.h>
19#include <linux/kernel.h>
20#include <linux/init.h>
21#include <linux/usb.h>
22#include <linux/uaccess.h>
23#include <linux/mm.h>
24#include <linux/fb.h>
25#include <linux/mutex.h>
Amit Kucheriafb299002009-07-27 12:01:03 +030026#include <linux/vmalloc.h>
Roberto De Ioris88e58b12009-06-03 14:03:06 -070027
28#include "udlfb.h"
29
Bernie Thompson59277b62009-11-24 15:52:21 -080030#define DRIVER_VERSION "DisplayLink Framebuffer Driver 0.4.1"
Roberto De Ioris88e58b12009-06-03 14:03:06 -070031
Bernie Thompson59277b62009-11-24 15:52:21 -080032static struct fb_fix_screeninfo dlfb_fix = {
Bernie Thompson1d31a9e2010-02-15 06:45:43 -080033 .id = "displaylinkfb",
34 .type = FB_TYPE_PACKED_PIXELS,
35 .visual = FB_VISUAL_TRUECOLOR,
36 .xpanstep = 0,
37 .ypanstep = 0,
38 .ywrapstep = 0,
39 .accel = FB_ACCEL_NONE,
Bernie Thompson59277b62009-11-24 15:52:21 -080040};
Roberto De Ioris88e58b12009-06-03 14:03:06 -070041
Bernie Thompsoncc403dc2010-02-15 06:45:49 -080042/*
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);
Bernie Thompson59277b62009-11-24 15:52:21 -080053
Bernie Thompson4a4854d2010-02-15 06:45:55 -080054/* 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
Bernie Thompson59277b62009-11-24 15:52:21 -080061/*
62 * Inserts a specific DisplayLink controller command into the provided
63 * buffer.
64 */
65static char *insert_command(char *buf, u8 reg, u8 val)
Roberto De Ioris88e58b12009-06-03 14:03:06 -070066{
Bernie Thompson1d31a9e2010-02-15 06:45:43 -080067 *buf++ = 0xAF;
68 *buf++ = 0x20;
69 *buf++ = reg;
70 *buf++ = val;
71 return buf;
Roberto De Ioris88e58b12009-06-03 14:03:06 -070072}
73
Bernie Thompson59277b62009-11-24 15:52:21 -080074static char *insert_vidreg_lock(char *buf)
Roberto De Ioris88e58b12009-06-03 14:03:06 -070075{
Bernie Thompson1d31a9e2010-02-15 06:45:43 -080076 return insert_command(buf, 0xFF, 0x00);
Bernie Thompson59277b62009-11-24 15:52:21 -080077}
Roberto De Ioris88e58b12009-06-03 14:03:06 -070078
Bernie Thompson59277b62009-11-24 15:52:21 -080079static char *insert_vidreg_unlock(char *buf)
80{
Bernie Thompson1d31a9e2010-02-15 06:45:43 -080081 return insert_command(buf, 0xFF, 0xFF);
Bernie Thompson59277b62009-11-24 15:52:21 -080082}
Roberto De Ioris88e58b12009-06-03 14:03:06 -070083
Bernie Thompson59277b62009-11-24 15:52:21 -080084/*
85 * Once you send this command, the DisplayLink framebuffer gets driven to the
86 * display.
87 */
88static char *insert_enable_hvsync(char *buf)
89{
Bernie Thompson1d31a9e2010-02-15 06:45:43 -080090 return insert_command(buf, 0x1F, 0x00);
Bernie Thompson59277b62009-11-24 15:52:21 -080091}
92
93static char *insert_set_color_depth(char *buf, u8 selection)
94{
Bernie Thompson1d31a9e2010-02-15 06:45:43 -080095 return insert_command(buf, 0x00, selection);
Bernie Thompson59277b62009-11-24 15:52:21 -080096}
97
98static char *insert_set_base16bpp(char *wrptr, u32 base)
99{
Bernie Thompson1d31a9e2010-02-15 06:45:43 -0800100 /* the base pointer is 16 bits wide, 0x20 is hi byte. */
101 wrptr = insert_command(wrptr, 0x20, base >> 16);
102 wrptr = insert_command(wrptr, 0x21, base >> 8);
103 return insert_command(wrptr, 0x22, base);
Bernie Thompson59277b62009-11-24 15:52:21 -0800104}
105
106static char *insert_set_base8bpp(char *wrptr, u32 base)
107{
Bernie Thompson1d31a9e2010-02-15 06:45:43 -0800108 wrptr = insert_command(wrptr, 0x26, base >> 16);
109 wrptr = insert_command(wrptr, 0x27, base >> 8);
110 return insert_command(wrptr, 0x28, base);
Bernie Thompson59277b62009-11-24 15:52:21 -0800111}
112
113static char *insert_command_16(char *wrptr, u8 reg, u16 value)
114{
Bernie Thompson1d31a9e2010-02-15 06:45:43 -0800115 wrptr = insert_command(wrptr, reg, value >> 8);
116 return insert_command(wrptr, reg+1, value);
Bernie Thompson59277b62009-11-24 15:52:21 -0800117}
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 */
123static char *insert_command_16be(char *wrptr, u8 reg, u16 value)
124{
Bernie Thompson1d31a9e2010-02-15 06:45:43 -0800125 wrptr = insert_command(wrptr, reg, value);
126 return insert_command(wrptr, reg+1, value >> 8);
Bernie Thompson59277b62009-11-24 15:52:21 -0800127}
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;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700146 }
Bernie Thompson59277b62009-11-24 15:52:21 -0800147
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 */
155static char *insert_command_lfsr16(char *wrptr, u8 reg, u16 value)
156{
157 return insert_command_16(wrptr, reg, lfsr16(value));
158}
159
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 */
164static char *insert_set_vid_cmds(char *wrptr, struct fb_var_screeninfo *var)
165{
166 u16 xds, yds;
167 u16 xde, yde;
168 u16 yec;
169
170
171 /* x display start */
172 xds = var->left_margin + var->hsync_len;
173 wrptr = insert_command_lfsr16(wrptr, 0x01, xds);
174 /* x display end */
175 xde = xds + var->xres;
176 wrptr = insert_command_lfsr16(wrptr, 0x03, xde);
177
178 /* y display start */
179 yds = var->upper_margin + var->vsync_len;
180 wrptr = insert_command_lfsr16(wrptr, 0x05, yds);
181 /* y display end */
182 yde = yds + var->yres;
183 wrptr = insert_command_lfsr16(wrptr, 0x07, yde);
184
185 /* x end count is active + blanking - 1 */
186 wrptr = insert_command_lfsr16(wrptr, 0x09, xde + var->right_margin - 1);
187
188 /* libdlo hardcodes hsync start to 1 */
189 wrptr = insert_command_lfsr16(wrptr, 0x0B, 1);
190
191 /* hsync end is width of sync pulse + 1 */
192 wrptr = insert_command_lfsr16(wrptr, 0x0D, var->hsync_len + 1);
193
194 /* hpixels is active pixels */
195 wrptr = insert_command_16(wrptr, 0x0F, var->xres);
196
197 /* yendcount is vertical active + vertical blanking */
198 yec = var->yres + var->upper_margin + var->lower_margin +
199 var->vsync_len;
200 wrptr = insert_command_lfsr16(wrptr, 0x11, yec);
201
202 /* libdlo hardcodes vsync start to 0 */
203 wrptr = insert_command_lfsr16(wrptr, 0x13, 0);
204
205 /* vsync end is width of vsync pulse */
206 wrptr = insert_command_lfsr16(wrptr, 0x15, var->vsync_len);
207
208 /* vpixels is active pixels */
209 wrptr = insert_command_16(wrptr, 0x17, var->yres);
210
211 /* convert picoseconds to 5kHz multiple for pclk5k = x * 1E12/5k */
212 wrptr = insert_command_16be(wrptr, 0x1B, 200*1000*1000/var->pixclock);
213
214 return wrptr;
215}
216
217/*
218 * This takes a standard fbdev screeninfo struct that was fetched or prepared
219 * and then generates the appropriate command sequence that then drives the
220 * display controller.
221 */
222static int dlfb_set_video_mode(struct dlfb_data *dev,
223 struct fb_var_screeninfo *var)
224{
225 char *buf;
226 char *wrptr;
227 int retval = 0;
228 int writesize;
229
230 buf = dev->buf;
231
232 /*
233 * This first section has to do with setting the base address on the
234 * controller * associated with the display. There are 2 base
235 * pointers, currently, we only * use the 16 bpp segment.
236 */
237 wrptr = insert_vidreg_lock(buf);
238 wrptr = insert_set_color_depth(wrptr, 0x00);
239 /* set base for 16bpp segment to 0 */
240 wrptr = insert_set_base16bpp(wrptr, 0);
241 /* set base for 8bpp segment to end of fb */
242 wrptr = insert_set_base8bpp(wrptr, dev->info->fix.smem_len);
243
244 wrptr = insert_set_vid_cmds(wrptr, var);
245 wrptr = insert_enable_hvsync(wrptr);
246 wrptr = insert_vidreg_unlock(wrptr);
247
248 writesize = wrptr - buf;
249
250 mutex_lock(&dev->bulk_mutex);
251 if (!dev->interface) { /* disconnect() was called */
252 mutex_unlock(&dev->bulk_mutex);
253 retval = -ENODEV;
254 goto error;
255 }
256
257 retval = dlfb_bulk_msg(dev, writesize);
258 mutex_unlock(&dev->bulk_mutex);
259 if (retval) {
260 dev_err(&dev->udev->dev, "Problem %d with submit write bulk.\n",
261 retval);
262 goto error;
263 }
264
265 return 0;
266
267error:
268 return retval;
269}
270
Bernie Thompson59277b62009-11-24 15:52:21 -0800271
272/*
273 * Query EDID from the handware, then hand it off to fbdev's edid parse
274 * routine which should give us back a filled in screeninfo structure.
275 */
276static int dlfb_get_var_from_edid(struct dlfb_data *dev,
277 struct fb_var_screeninfo *var)
278{
279 int ret;
280
281 dlfb_edid(dev);
282 ret = fb_parse_edid(dev->edid, var);
283
284 return ret;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700285}
286
287static int dlfb_mmap(struct fb_info *info, struct vm_area_struct *vma)
288{
289 unsigned long start = vma->vm_start;
290 unsigned long size = vma->vm_end - vma->vm_start;
291 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
292 unsigned long page, pos;
293
294 printk("MMAP: %lu %u\n", offset + size, info->fix.smem_len);
295
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700296 if (offset + size > info->fix.smem_len)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700297 return -EINVAL;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700298
299 pos = (unsigned long)info->fix.smem_start + offset;
300
301 while (size > 0) {
302 page = vmalloc_to_pfn((void *)pos);
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700303 if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED))
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700304 return -EAGAIN;
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700305
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700306 start += PAGE_SIZE;
307 pos += PAGE_SIZE;
308 if (size > PAGE_SIZE)
309 size -= PAGE_SIZE;
310 else
311 size = 0;
312 }
313
314 vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
315 return 0;
316
317}
318
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700319/* ioctl structure */
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700320struct dloarea {
321 int x, y;
322 int w, h;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700323 int x2, y2;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700324};
325
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700326static struct usb_driver dlfb_driver;
327
Bernie Thompson1d31a9e2010-02-15 06:45:43 -0800328/* thanks to Henrik Bjerregaard Pedersen for this function */
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700329static char *rle_compress16(uint16_t * src, char *dst, int rem)
330{
331
332 int rl;
333 uint16_t pix0;
334 char *end_if_raw = dst + 6 + 2 * rem;
335
Bernie Thompson1d31a9e2010-02-15 06:45:43 -0800336 dst += 6; /* header will be filled in if RLE is worth it */
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700337
338 while (rem && dst < end_if_raw) {
339 char *start = (char *)src;
340
341 pix0 = *src++;
342 rl = 1;
343 rem--;
344 while (rem && *src == pix0)
345 rem--, rl++, src++;
346 *dst++ = rl;
347 *dst++ = start[1];
348 *dst++ = start[0];
349 }
350
351 return dst;
352}
353
354/*
Bernie Thompson1d31a9e2010-02-15 06:45:43 -0800355Thanks to Henrik Bjerregaard Pedersen for rle implementation
356and code refactoring. Next step is huffman compression.
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700357*/
358
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700359static int
360image_blit(struct dlfb_data *dev_info, int x, int y, int width, int height,
361 char *data)
362{
363
364 int i, j, base;
365 int rem = width;
366 int ret;
367
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700368 int firstdiff, thistime;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700369
370 char *bufptr;
371
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700372 if (x + width > dev_info->info->var.xres)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700373 return -EINVAL;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700374
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700375 if (y + height > dev_info->info->var.yres)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700376 return -EINVAL;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700377
378 mutex_lock(&dev_info->bulk_mutex);
379
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700380 base =
381 dev_info->base16 + ((dev_info->info->var.xres * 2 * y) + (x * 2));
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700382
383 data += (dev_info->info->var.xres * 2 * y) + (x * 2);
384
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700385 /* printk("IMAGE_BLIT\n"); */
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700386
387 bufptr = dev_info->buf;
388
389 for (i = y; i < y + height; i++) {
390
391 if (dev_info->bufend - bufptr < BUF_HIGH_WATER_MARK) {
392 ret = dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
393 bufptr = dev_info->buf;
394 }
395
396 rem = width;
397
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700398 /* printk("WRITING LINE %d\n", i); */
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700399
400 while (rem) {
401
402 if (dev_info->bufend - bufptr < BUF_HIGH_WATER_MARK) {
403 ret =
404 dlfb_bulk_msg(dev_info,
405 bufptr - dev_info->buf);
406 bufptr = dev_info->buf;
407 }
Bernie Thompson1d31a9e2010-02-15 06:45:43 -0800408 /* number of pixels to consider this time */
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700409 thistime = rem;
410 if (thistime > 255)
411 thistime = 255;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700412
Bernie Thompson59277b62009-11-24 15:52:21 -0800413 if (dev_info->backing_buffer) {
414 /* find first pixel that has changed */
415 firstdiff = -1;
416 for (j = 0; j < thistime * 2; j++) {
417 if (dev_info->backing_buffer
418 [base - dev_info->base16 + j]
419 != data[j]) {
420 firstdiff = j / 2;
421 break;
422 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700423 }
Bernie Thompson59277b62009-11-24 15:52:21 -0800424
425 } else {
426 firstdiff = 0;
427
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700428 }
429
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700430 if (firstdiff >= 0) {
431 char *end_of_rle;
432
433 end_of_rle =
434 rle_compress16((uint16_t *) (data +
435 firstdiff * 2),
436 bufptr,
437 thistime - firstdiff);
438
439 if (end_of_rle <
440 bufptr + 6 + 2 * (thistime - firstdiff)) {
441 bufptr[0] = 0xAF;
442 bufptr[1] = 0x69;
443
444 bufptr[2] =
445 (char)((base +
446 firstdiff * 2) >> 16);
447 bufptr[3] =
448 (char)((base + firstdiff * 2) >> 8);
449 bufptr[4] =
450 (char)(base + firstdiff * 2);
451 bufptr[5] = thistime - firstdiff;
452
453 bufptr = end_of_rle;
454
455 } else {
Bernie Thompson1d31a9e2010-02-15 06:45:43 -0800456 /* fallback to raw (or other?) */
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700457 *bufptr++ = 0xAF;
458 *bufptr++ = 0x68;
459
460 *bufptr++ =
461 (char)((base +
462 firstdiff * 2) >> 16);
463 *bufptr++ =
464 (char)((base + firstdiff * 2) >> 8);
465 *bufptr++ =
466 (char)(base + firstdiff * 2);
467 *bufptr++ = thistime - firstdiff;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700468 for (j = firstdiff * 2;
469 j < thistime * 2; j += 2) {
470 *bufptr++ = data[j + 1];
471 *bufptr++ = data[j];
472 }
473 }
474 }
475
476 base += thistime * 2;
477 data += thistime * 2;
478 rem -= thistime;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700479 }
480
Bernie Thompson59277b62009-11-24 15:52:21 -0800481 if (dev_info->backing_buffer)
482 memcpy(dev_info->backing_buffer +
483 (base - dev_info->base16) -
484 (width * 2), data - (width * 2), width * 2);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700485
486 base += (dev_info->info->var.xres * 2) - (width * 2);
487 data += (dev_info->info->var.xres * 2) - (width * 2);
488
489 }
490
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700491 if (bufptr > dev_info->buf) {
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700492 ret = dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700493 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700494
495 mutex_unlock(&dev_info->bulk_mutex);
496
497 return base;
498
499}
500
501static int
502draw_rect(struct dlfb_data *dev_info, int x, int y, int width, int height,
503 unsigned char red, unsigned char green, unsigned char blue)
504{
505
506 int i, j, base;
507 int ret;
508 unsigned short col =
509 (((((red) & 0xF8) | ((green) >> 5)) & 0xFF) << 8) +
510 (((((green) & 0x1C) << 3) | ((blue) >> 3)) & 0xFF);
511 int rem = width;
512
513 char *bufptr;
514
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700515 if (x + width > dev_info->info->var.xres)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700516 return -EINVAL;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700517
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700518 if (y + height > dev_info->info->var.yres)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700519 return -EINVAL;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700520
521 mutex_lock(&dev_info->bulk_mutex);
522
523 base = dev_info->base16 + (dev_info->info->var.xres * 2 * y) + (x * 2);
524
525 bufptr = dev_info->buf;
526
527 for (i = y; i < y + height; i++) {
528
Bernie Thompson59277b62009-11-24 15:52:21 -0800529 if (dev_info->backing_buffer) {
530 for (j = 0; j < width * 2; j += 2) {
531 dev_info->backing_buffer
532 [base - dev_info->base16 + j] =
533 (char)(col >> 8);
534 dev_info->backing_buffer
535 [base - dev_info->base16 + j + 1] =
536 (char)(col);
537 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700538 }
Bernie Thompson59277b62009-11-24 15:52:21 -0800539
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700540 if (dev_info->bufend - bufptr < BUF_HIGH_WATER_MARK) {
541 ret = dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
542 bufptr = dev_info->buf;
543 }
544
545 rem = width;
546
547 while (rem) {
548
549 if (dev_info->bufend - bufptr < BUF_HIGH_WATER_MARK) {
550 ret =
551 dlfb_bulk_msg(dev_info,
552 bufptr - dev_info->buf);
553 bufptr = dev_info->buf;
554 }
555
556 *bufptr++ = 0xAF;
557 *bufptr++ = 0x69;
558
559 *bufptr++ = (char)(base >> 16);
560 *bufptr++ = (char)(base >> 8);
561 *bufptr++ = (char)(base);
562
563 if (rem > 255) {
564 *bufptr++ = 255;
565 *bufptr++ = 255;
566 rem -= 255;
567 base += 255 * 2;
568 } else {
569 *bufptr++ = rem;
570 *bufptr++ = rem;
571 base += rem * 2;
572 rem = 0;
573 }
574
575 *bufptr++ = (char)(col >> 8);
576 *bufptr++ = (char)(col);
577
578 }
579
580 base += (dev_info->info->var.xres * 2) - (width * 2);
581
582 }
583
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700584 if (bufptr > dev_info->buf)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700585 ret = dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700586
587 mutex_unlock(&dev_info->bulk_mutex);
588
589 return 1;
590}
591
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700592static void swapfb(struct dlfb_data *dev_info)
593{
594
595 int tmpbase;
596 char *bufptr;
597
598 mutex_lock(&dev_info->bulk_mutex);
599
600 tmpbase = dev_info->base16;
601
602 dev_info->base16 = dev_info->base16d;
603 dev_info->base16d = tmpbase;
604
605 bufptr = dev_info->buf;
606
607 bufptr = dlfb_set_register(bufptr, 0xFF, 0x00);
608
Bernie Thompson1d31a9e2010-02-15 06:45:43 -0800609 /* set addresses */
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700610 bufptr =
611 dlfb_set_register(bufptr, 0x20, (char)(dev_info->base16 >> 16));
612 bufptr = dlfb_set_register(bufptr, 0x21, (char)(dev_info->base16 >> 8));
613 bufptr = dlfb_set_register(bufptr, 0x22, (char)(dev_info->base16));
614
615 bufptr = dlfb_set_register(bufptr, 0xFF, 0x00);
616
617 dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
618
619 mutex_unlock(&dev_info->bulk_mutex);
620}
621
622static int copyfb(struct dlfb_data *dev_info)
623{
624 int base;
625 int source;
626 int rem;
627 int i, ret;
628
629 char *bufptr;
630
631 base = dev_info->base16d;
632
633 mutex_lock(&dev_info->bulk_mutex);
634
635 source = dev_info->base16;
636
637 bufptr = dev_info->buf;
638
639 for (i = 0; i < dev_info->info->var.yres; i++) {
640
641 if (dev_info->bufend - bufptr < BUF_HIGH_WATER_MARK) {
642 ret = dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
643 bufptr = dev_info->buf;
644 }
645
646 rem = dev_info->info->var.xres;
647
648 while (rem) {
649
650 if (dev_info->bufend - bufptr < BUF_HIGH_WATER_MARK) {
651 ret =
652 dlfb_bulk_msg(dev_info,
653 bufptr - dev_info->buf);
654 bufptr = dev_info->buf;
655
656 }
657
658 *bufptr++ = 0xAF;
659 *bufptr++ = 0x6A;
660
661 *bufptr++ = (char)(base >> 16);
662 *bufptr++ = (char)(base >> 8);
663 *bufptr++ = (char)(base);
664
665 if (rem > 255) {
666 *bufptr++ = 255;
667 *bufptr++ = (char)(source >> 16);
668 *bufptr++ = (char)(source >> 8);
669 *bufptr++ = (char)(source);
670
671 rem -= 255;
672 base += 255 * 2;
673 source += 255 * 2;
674
675 } else {
676 *bufptr++ = rem;
677 *bufptr++ = (char)(source >> 16);
678 *bufptr++ = (char)(source >> 8);
679 *bufptr++ = (char)(source);
680
681 base += rem * 2;
682 source += rem * 2;
683 rem = 0;
684 }
685 }
686 }
687
688 if (bufptr > dev_info->buf)
689 ret = dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
690
691 mutex_unlock(&dev_info->bulk_mutex);
692
693 return 1;
694
695}
696
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700697static int
698copyarea(struct dlfb_data *dev_info, int dx, int dy, int sx, int sy,
699 int width, int height)
700{
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700701 int base;
702 int source;
703 int rem;
704 int i, ret;
705
706 char *bufptr;
707
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700708 if (dx + width > dev_info->info->var.xres)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700709 return -EINVAL;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700710
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700711 if (dy + height > dev_info->info->var.yres)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700712 return -EINVAL;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700713
714 mutex_lock(&dev_info->bulk_mutex);
715
716 base =
717 dev_info->base16 + (dev_info->info->var.xres * 2 * dy) + (dx * 2);
718 source = (dev_info->info->var.xres * 2 * sy) + (sx * 2);
719
720 bufptr = dev_info->buf;
721
722 for (i = sy; i < sy + height; i++) {
723
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700724 memcpy(dev_info->backing_buffer + base - dev_info->base16,
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700725 dev_info->backing_buffer + source, width * 2);
726
727 if (dev_info->bufend - bufptr < BUF_HIGH_WATER_MARK) {
728 ret = dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
729 bufptr = dev_info->buf;
730 }
731
732 rem = width;
733
734 while (rem) {
735
736 if (dev_info->bufend - bufptr < BUF_HIGH_WATER_MARK) {
737 ret =
738 dlfb_bulk_msg(dev_info,
739 bufptr - dev_info->buf);
740 bufptr = dev_info->buf;
741 }
742
743 *bufptr++ = 0xAF;
744 *bufptr++ = 0x6A;
745
746 *bufptr++ = (char)(base >> 16);
747 *bufptr++ = (char)(base >> 8);
748 *bufptr++ = (char)(base);
749
750 if (rem > 255) {
751 *bufptr++ = 255;
752 *bufptr++ = (char)(source >> 16);
753 *bufptr++ = (char)(source >> 8);
754 *bufptr++ = (char)(source);
755
756 rem -= 255;
757 base += 255 * 2;
758 source += 255 * 2;
759
760 } else {
761 *bufptr++ = rem;
762 *bufptr++ = (char)(source >> 16);
763 *bufptr++ = (char)(source >> 8);
764 *bufptr++ = (char)(source);
765
766 base += rem * 2;
767 source += rem * 2;
768 rem = 0;
769 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700770 }
771
772 base += (dev_info->info->var.xres * 2) - (width * 2);
773 source += (dev_info->info->var.xres * 2) - (width * 2);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700774 }
775
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700776 if (bufptr > dev_info->buf)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700777 ret = dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700778
779 mutex_unlock(&dev_info->bulk_mutex);
780
781 return 1;
782}
783
Greg Kroah-Hartman4b6a4852009-06-03 14:47:21 -0700784static void dlfb_copyarea(struct fb_info *info, const struct fb_copyarea *area)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700785{
786
787 struct dlfb_data *dev = info->par;
788
789 copyarea(dev, area->dx, area->dy, area->sx, area->sy, area->width,
790 area->height);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700791}
792
Greg Kroah-Hartman4b6a4852009-06-03 14:47:21 -0700793static void dlfb_imageblit(struct fb_info *info, const struct fb_image *image)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700794{
795
796 int ret;
797 struct dlfb_data *dev = info->par;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700798 cfb_imageblit(info, image);
799 ret =
800 image_blit(dev, image->dx, image->dy, image->width, image->height,
801 info->screen_base);
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700802}
803
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700804static void dlfb_fillrect(struct fb_info *info,
805 const struct fb_fillrect *region)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700806{
807
808 unsigned char red, green, blue;
809 struct dlfb_data *dev = info->par;
810
811 memcpy(&red, &region->color, 1);
812 memcpy(&green, &region->color + 1, 1);
813 memcpy(&blue, &region->color + 2, 1);
814 draw_rect(dev, region->dx, region->dy, region->width, region->height,
815 red, green, blue);
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700816 /* printk("FILL RECT %d %d !!!\n", region->dx, region->dy); */
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700817
818}
819
820static int dlfb_ioctl(struct fb_info *info, unsigned int cmd, unsigned long arg)
821{
822
823 struct dlfb_data *dev_info = info->par;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700824 struct dloarea *area = NULL;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700825
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700826 if (cmd == 0xAD) {
827 char *edid = (char *)arg;
828 dlfb_edid(dev_info);
829 if (copy_to_user(edid, dev_info->edid, 128)) {
830 return -EFAULT;
831 }
832 return 0;
833 }
834
835 if (cmd == 0xAA || cmd == 0xAB || cmd == 0xAC) {
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700836
837 area = (struct dloarea *)arg;
838
839 if (area->x < 0)
840 area->x = 0;
841
842 if (area->x > info->var.xres)
843 area->x = info->var.xres;
844
845 if (area->y < 0)
846 area->y = 0;
847
848 if (area->y > info->var.yres)
849 area->y = info->var.yres;
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700850 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700851
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700852 if (cmd == 0xAA) {
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700853 image_blit(dev_info, area->x, area->y, area->w, area->h,
854 info->screen_base);
855 }
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700856 if (cmd == 0xAC) {
857 copyfb(dev_info);
858 image_blit(dev_info, area->x, area->y, area->w, area->h,
859 info->screen_base);
860 swapfb(dev_info);
861 } else if (cmd == 0xAB) {
862
863 if (area->x2 < 0)
864 area->x2 = 0;
865
866 if (area->y2 < 0)
867 area->y2 = 0;
868
869 copyarea(dev_info,
870 area->x2, area->y2, area->x, area->y, area->w,
871 area->h);
872 }
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700873 return 0;
874}
875
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700876/* taken from vesafb */
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700877static int
878dlfb_setcolreg(unsigned regno, unsigned red, unsigned green,
879 unsigned blue, unsigned transp, struct fb_info *info)
880{
881 int err = 0;
882
883 if (regno >= info->cmap.len)
884 return 1;
885
886 if (regno < 16) {
887 if (info->var.red.offset == 10) {
888 /* 1:5:5:5 */
889 ((u32 *) (info->pseudo_palette))[regno] =
890 ((red & 0xf800) >> 1) |
891 ((green & 0xf800) >> 6) | ((blue & 0xf800) >> 11);
892 } else {
893 /* 0:5:6:5 */
894 ((u32 *) (info->pseudo_palette))[regno] =
895 ((red & 0xf800)) |
896 ((green & 0xfc00) >> 5) | ((blue & 0xf800) >> 11);
897 }
898 }
899
900 return err;
901}
902
903static int dlfb_release(struct fb_info *info, int user)
904{
905 struct dlfb_data *dev_info = info->par;
906 image_blit(dev_info, 0, 0, info->var.xres, info->var.yres,
907 info->screen_base);
908 return 0;
909}
910
Bernie Thompson4a4854d2010-02-15 06:45:55 -0800911/*
912 * Called when all client interfaces to start transactions have been disabled,
913 * and all references to our device instance (dlfb_data) are released.
914 * Every transaction must have a reference, so we know are fully spun down
915 */
916static void dlfb_delete(struct kref *kref)
917{
918 struct dlfb_data *dev = container_of(kref, struct dlfb_data, kref);
919
920 if (dev->backing_buffer)
921 vfree(dev->backing_buffer);
922
923 kfree(dev);
924}
925
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -0700926static int dlfb_blank(int blank_mode, struct fb_info *info)
927{
Roberto De Ioris7316bc52009-06-10 23:02:19 -0700928 struct dlfb_data *dev_info = info->par;
929 char *bufptr = dev_info->buf;
930
931 bufptr = dlfb_set_register(bufptr, 0xFF, 0x00);
932 if (blank_mode != FB_BLANK_UNBLANK) {
933 bufptr = dlfb_set_register(bufptr, 0x1F, 0x01);
934 } else {
935 bufptr = dlfb_set_register(bufptr, 0x1F, 0x00);
936 }
937 bufptr = dlfb_set_register(bufptr, 0xFF, 0xFF);
938
939 dlfb_bulk_msg(dev_info, bufptr - dev_info->buf);
940
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700941 return 0;
942}
943
944static struct fb_ops dlfb_ops = {
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700945 .fb_setcolreg = dlfb_setcolreg,
946 .fb_fillrect = dlfb_fillrect,
947 .fb_copyarea = dlfb_copyarea,
948 .fb_imageblit = dlfb_imageblit,
949 .fb_mmap = dlfb_mmap,
950 .fb_ioctl = dlfb_ioctl,
951 .fb_release = dlfb_release,
952 .fb_blank = dlfb_blank,
953};
954
Bernie Thompsoncc403dc2010-02-15 06:45:49 -0800955/*
956 * This is necessary before we can communicate with the display controller.
957 */
958static int dlfb_select_std_channel(struct dlfb_data *dev)
959{
960 int ret;
961 u8 set_def_chn[] = { 0x57, 0xCD, 0xDC, 0xA7,
962 0x1C, 0x88, 0x5E, 0x15,
963 0x60, 0xFE, 0xC6, 0x97,
964 0x16, 0x3D, 0x47, 0xF2 };
965
966 ret = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
967 NR_USB_REQUEST_CHANNEL,
968 (USB_DIR_OUT | USB_TYPE_VENDOR), 0, 0,
969 set_def_chn, sizeof(set_def_chn), USB_CTRL_SET_TIMEOUT);
970 return ret;
971}
972
Bernie Thompson59277b62009-11-24 15:52:21 -0800973static int dlfb_probe(struct usb_interface *interface,
974 const struct usb_device_id *id)
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700975{
Bernie Thompson59277b62009-11-24 15:52:21 -0800976 struct device *mydev;
977 struct usb_device *usbdev;
978 struct dlfb_data *dev;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700979 struct fb_info *info;
Bernie Thompson59277b62009-11-24 15:52:21 -0800980 int videomemorysize;
981 unsigned char *videomemory;
982 int retval = -ENOMEM;
983 struct fb_var_screeninfo *var;
984 struct fb_bitfield red = { 11, 5, 0 };
985 struct fb_bitfield green = { 5, 6, 0 };
986 struct fb_bitfield blue = { 0, 5, 0 };
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700987
Bernie Thompson59277b62009-11-24 15:52:21 -0800988 usbdev = usb_get_dev(interface_to_usbdev(interface));
989 mydev = &usbdev->dev;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700990
Bernie Thompson59277b62009-11-24 15:52:21 -0800991 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
992 if (dev == NULL) {
993 dev_err(mydev, "failed alloc of dev struct\n");
994 goto err_devalloc;
Roberto De Ioris88e58b12009-06-03 14:03:06 -0700995 }
996
Bernie Thompson59277b62009-11-24 15:52:21 -0800997 mutex_init(&dev->bulk_mutex);
998 dev->udev = usbdev;
Bernie Thompson4a4854d2010-02-15 06:45:55 -0800999 dev->gdev = &usbdev->dev; /* our generic struct device * */
Bernie Thompson59277b62009-11-24 15:52:21 -08001000 dev->interface = interface;
1001 usb_set_intfdata(interface, dev);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001002
Bernie Thompson59277b62009-11-24 15:52:21 -08001003 dev_info(mydev, "dlfb_probe: setting up DisplayLink device\n");
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001004
Bernie Thompson59277b62009-11-24 15:52:21 -08001005 /*
1006 * TODO: replace single 64K buffer with buffer list
1007 * and async dispatch
1008 */
1009 dev->buf = kmalloc(BUF_SIZE, GFP_KERNEL);
1010 if (dev->buf == NULL) {
1011 dev_err(mydev, "unable to allocate memory for dlfb commands\n");
1012 goto err_usballoc;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001013 }
Bernie Thompson59277b62009-11-24 15:52:21 -08001014 dev->bufend = dev->buf + BUF_SIZE;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001015
Bernie Thompson59277b62009-11-24 15:52:21 -08001016 dev->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
1017 usb_fill_bulk_urb(dev->tx_urb, dev->udev,
1018 usb_sndbulkpipe(dev->udev, 1), dev->buf, 0,
1019 dlfb_bulk_callback, dev);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001020
Bernie Thompson59277b62009-11-24 15:52:21 -08001021 /* allocates framebuffer driver structure, not framebuffer memory */
1022 info = framebuffer_alloc(0, mydev);
1023 if (!info)
1024 goto err_fballoc;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001025
Bernie Thompson59277b62009-11-24 15:52:21 -08001026 dev->info = info;
1027 info->par = dev;
1028 info->pseudo_palette = dev->pseudo_palette;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001029
Bernie Thompson59277b62009-11-24 15:52:21 -08001030 var = &info->var;
1031 retval = dlfb_get_var_from_edid(dev, var);
1032 if (retval) {
1033 /* had a problem getting edid. so fallback to 640x480 */
1034 dev_err(mydev, "Problem %d with EDID.\n", retval);
1035 var->xres = 640;
1036 var->yres = 480;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001037 }
1038
Bernie Thompson59277b62009-11-24 15:52:21 -08001039 /*
1040 * ok, now that we've got the size info, we can alloc our framebuffer.
1041 * We are using 16bpp.
1042 */
1043 info->var.bits_per_pixel = 16;
1044 info->fix = dlfb_fix;
1045 info->fix.line_length = var->xres * (var->bits_per_pixel / 8);
1046 videomemorysize = info->fix.line_length * var->yres;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001047
Bernie Thompson59277b62009-11-24 15:52:21 -08001048 /*
1049 * The big chunk of system memory we use as a virtual framebuffer.
1050 * Pages don't need to be set RESERVED (non-swap) immediately on 2.6
1051 * remap_pfn_page() syscall in our mmap and/or defio will handle.
1052 */
1053 videomemory = vmalloc(videomemorysize);
1054 if (!videomemory)
1055 goto err_vidmem;
1056 memset(videomemory, 0, videomemorysize);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001057
Bernie Thompson59277b62009-11-24 15:52:21 -08001058 info->screen_base = videomemory;
1059 info->fix.smem_len = PAGE_ALIGN(videomemorysize);
1060 info->fix.smem_start = (unsigned long) videomemory;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001061 info->flags =
1062 FBINFO_DEFAULT | FBINFO_READS_FAST | FBINFO_HWACCEL_IMAGEBLIT |
1063 FBINFO_HWACCEL_COPYAREA | FBINFO_HWACCEL_FILLRECT;
Bernie Thompson59277b62009-11-24 15:52:21 -08001064
1065 /*
1066 * Second framebuffer copy, mirroring the state of the framebuffer
1067 * on the physical USB device. We can function without this.
1068 * But with imperfect damage info we may end up sending pixels over USB
1069 * that were, in fact, unchanged -- wasting limited USB bandwidth
1070 */
1071 dev->backing_buffer = vmalloc(dev->screen_size);
1072 if (!dev->backing_buffer)
1073 dev_info(mydev, "No backing buffer allocated!\n");
1074
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001075 info->fbops = &dlfb_ops;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001076
Bernie Thompson59277b62009-11-24 15:52:21 -08001077 var->vmode = FB_VMODE_NONINTERLACED;
1078 var->red = red;
1079 var->green = green;
1080 var->blue = blue;
1081
1082 /*
1083 * TODO: Enable FB_CONFIG_DEFIO support
1084
1085 info->fbdefio = &dlfb_defio;
1086 fb_deferred_io_init(info);
1087
1088 */
1089
1090 retval = fb_alloc_cmap(&info->cmap, 256, 0);
1091 if (retval < 0) {
1092 dev_err(mydev, "Failed to allocate colormap\n");
1093 goto err_cmap;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001094 }
1095
Bernie Thompson59277b62009-11-24 15:52:21 -08001096 dlfb_select_std_channel(dev);
1097 dlfb_set_video_mode(dev, var);
1098 /* TODO: dlfb_dpy_update(dev); */
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001099
Bernie Thompson59277b62009-11-24 15:52:21 -08001100 retval = register_framebuffer(info);
1101 if (retval < 0)
1102 goto err_regfb;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001103
Bernie Thompson59277b62009-11-24 15:52:21 -08001104 /* paint "successful" green screen */
1105 draw_rect(dev, 0, 0, dev->info->var.xres,
1106 dev->info->var.yres, 0x30, 0xff, 0x30);
Greg Kroah-Hartmanf05e0572009-06-03 14:47:08 -07001107
Bernie Thompson59277b62009-11-24 15:52:21 -08001108 dev_info(mydev, "DisplayLink USB device %d now attached, "
1109 "using %dK of memory\n", info->node,
1110 ((dev->backing_buffer) ?
1111 videomemorysize * 2 : videomemorysize) >> 10);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001112 return 0;
1113
Bernie Thompson59277b62009-11-24 15:52:21 -08001114err_regfb:
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001115 fb_dealloc_cmap(&info->cmap);
Bernie Thompson59277b62009-11-24 15:52:21 -08001116err_cmap:
1117 /* TODO: fb_deferred_io_cleanup(info); */
1118 vfree(videomemory);
1119err_vidmem:
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001120 framebuffer_release(info);
Bernie Thompson59277b62009-11-24 15:52:21 -08001121err_fballoc:
1122 kfree(dev->buf);
1123err_usballoc:
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001124 usb_set_intfdata(interface, NULL);
Bernie Thompson59277b62009-11-24 15:52:21 -08001125 usb_put_dev(dev->udev);
1126 kfree(dev);
1127err_devalloc:
1128 return retval;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001129}
1130
1131static void dlfb_disconnect(struct usb_interface *interface)
1132{
Bernie Thompson59277b62009-11-24 15:52:21 -08001133 struct dlfb_data *dev;
1134 struct fb_info *info;
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001135
Bernie Thompson59277b62009-11-24 15:52:21 -08001136 dev = usb_get_intfdata(interface);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001137 usb_set_intfdata(interface, NULL);
Bernie Thompson59277b62009-11-24 15:52:21 -08001138 usb_put_dev(dev->udev);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001139
Bernie Thompson59277b62009-11-24 15:52:21 -08001140 /*
1141 * TODO: since, upon usb disconnect(), usb will cancel in-flight urbs
1142 * and error out any new ones, look at eliminating need for mutex
1143 */
1144 mutex_lock(&dev->bulk_mutex);
1145 dev->interface = NULL;
1146 info = dev->info;
1147 mutex_unlock(&dev->bulk_mutex);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001148
Bernie Thompson59277b62009-11-24 15:52:21 -08001149 if (info) {
1150 dev_info(&interface->dev, "Detaching DisplayLink device %d.\n",
1151 info->node);
1152 unregister_framebuffer(info);
1153 fb_dealloc_cmap(&info->cmap);
1154 /* TODO: fb_deferred_io_cleanup(info); */
1155 fb_dealloc_cmap(&info->cmap);
1156 vfree((void __force *)info->screen_base);
1157 framebuffer_release(info);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001158 }
1159
Bernie Thompson59277b62009-11-24 15:52:21 -08001160 if (dev->backing_buffer)
1161 vfree(dev->backing_buffer);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001162
Bernie Thompson59277b62009-11-24 15:52:21 -08001163 kfree(dev);
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001164}
1165
1166static struct usb_driver dlfb_driver = {
1167 .name = "udlfb",
1168 .probe = dlfb_probe,
1169 .disconnect = dlfb_disconnect,
1170 .id_table = id_table,
1171};
1172
1173static int __init dlfb_init(void)
1174{
1175 int res;
1176
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001177 res = usb_register(&dlfb_driver);
1178 if (res)
1179 err("usb_register failed. Error number %d", res);
1180
1181 printk("VMODES initialized\n");
1182
1183 return res;
1184}
1185
1186static void __exit dlfb_exit(void)
1187{
1188 usb_deregister(&dlfb_driver);
1189}
1190
1191module_init(dlfb_init);
1192module_exit(dlfb_exit);
1193
Bernie Thompson4a4854d2010-02-15 06:45:55 -08001194static void dlfb_urb_completion(struct urb *urb)
1195{
1196 struct urb_node *unode = urb->context;
1197 struct dlfb_data *dev = unode->dev;
1198 unsigned long flags;
1199
1200 /* sync/async unlink faults aren't errors */
1201 if (urb->status) {
1202 if (!(urb->status == -ENOENT ||
1203 urb->status == -ECONNRESET ||
1204 urb->status == -ESHUTDOWN)) {
1205 dl_err("%s - nonzero write bulk status received: %d\n",
1206 __func__, urb->status);
1207 atomic_set(&dev->lost_pixels, 1);
1208 }
1209 }
1210
1211 urb->transfer_buffer_length = dev->urbs.size; /* reset to actual */
1212
1213 spin_lock_irqsave(&dev->urbs.lock, flags);
1214 list_add_tail(&unode->entry, &dev->urbs.list);
1215 dev->urbs.available++;
1216 spin_unlock_irqrestore(&dev->urbs.lock, flags);
1217
1218 up(&dev->urbs.limit_sem);
1219}
1220
1221static void dlfb_free_urb_list(struct dlfb_data *dev)
1222{
1223 int count = dev->urbs.count;
1224 struct list_head *node;
1225 struct urb_node *unode;
1226 struct urb *urb;
1227 int ret;
1228 unsigned long flags;
1229
1230 dl_notice("Waiting for completes and freeing all render urbs\n");
1231
1232 /* keep waiting and freeing, until we've got 'em all */
1233 while (count--) {
1234 /* Timeout means a memory leak and/or fault */
1235 ret = down_timeout(&dev->urbs.limit_sem, FREE_URB_TIMEOUT);
1236 if (ret) {
1237 BUG_ON(ret);
1238 break;
1239 }
1240 spin_lock_irqsave(&dev->urbs.lock, flags);
1241
1242 node = dev->urbs.list.next; /* have reserved one with sem */
1243 list_del_init(node);
1244
1245 spin_unlock_irqrestore(&dev->urbs.lock, flags);
1246
1247 unode = list_entry(node, struct urb_node, entry);
1248 urb = unode->urb;
1249
1250 /* Free each separately allocated piece */
1251 usb_buffer_free(urb->dev, dev->urbs.size,
1252 urb->transfer_buffer, urb->transfer_dma);
1253 usb_free_urb(urb);
1254 kfree(node);
1255 }
1256
1257 kref_put(&dev->kref, dlfb_delete);
1258
1259}
1260
1261static int dlfb_alloc_urb_list(struct dlfb_data *dev, int count, size_t size)
1262{
1263 int i = 0;
1264 struct urb *urb;
1265 struct urb_node *unode;
1266 char *buf;
1267
1268 spin_lock_init(&dev->urbs.lock);
1269
1270 dev->urbs.size = size;
1271 INIT_LIST_HEAD(&dev->urbs.list);
1272
1273 while (i < count) {
1274 unode = kzalloc(sizeof(struct urb_node), GFP_KERNEL);
1275 if (!unode)
1276 break;
1277 unode->dev = dev;
1278
1279 urb = usb_alloc_urb(0, GFP_KERNEL);
1280 if (!urb) {
1281 kfree(unode);
1282 break;
1283 }
1284 unode->urb = urb;
1285
1286 buf = usb_buffer_alloc(dev->udev, MAX_TRANSFER, GFP_KERNEL,
1287 &urb->transfer_dma);
1288 if (!buf) {
1289 kfree(unode);
1290 usb_free_urb(urb);
1291 break;
1292 }
1293
1294 /* urb->transfer_buffer_length set to actual before submit */
1295 usb_fill_bulk_urb(urb, dev->udev, usb_sndbulkpipe(dev->udev, 1),
1296 buf, size, dlfb_urb_completion, unode);
1297 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1298
1299 list_add_tail(&unode->entry, &dev->urbs.list);
1300
1301 i++;
1302 }
1303
1304 sema_init(&dev->urbs.limit_sem, i);
1305 dev->urbs.count = i;
1306 dev->urbs.available = i;
1307
1308 kref_get(&dev->kref); /* released in free_render_urbs() */
1309
1310 dl_notice("allocated %d %d byte urbs \n", i, (int) size);
1311
1312 return i;
1313}
1314
1315static struct urb *dlfb_get_urb(struct dlfb_data *dev)
1316{
1317 int ret = 0;
1318 struct list_head *entry;
1319 struct urb_node *unode;
1320 struct urb *urb = NULL;
1321 unsigned long flags;
1322
1323 /* Wait for an in-flight buffer to complete and get re-queued */
1324 ret = down_timeout(&dev->urbs.limit_sem, GET_URB_TIMEOUT);
1325 if (ret) {
1326 atomic_set(&dev->lost_pixels, 1);
1327 dl_err("wait for urb interrupted: %x\n", ret);
1328 goto error;
1329 }
1330
1331 spin_lock_irqsave(&dev->urbs.lock, flags);
1332
1333 BUG_ON(list_empty(&dev->urbs.list)); /* reserved one with limit_sem */
1334 entry = dev->urbs.list.next;
1335 list_del_init(entry);
1336 dev->urbs.available--;
1337
1338 spin_unlock_irqrestore(&dev->urbs.lock, flags);
1339
1340 unode = list_entry(entry, struct urb_node, entry);
1341 urb = unode->urb;
1342
1343error:
1344 return urb;
1345}
1346
1347static int dlfb_submit_urb(struct dlfb_data *dev, struct urb *urb, size_t len)
1348{
1349 int ret;
1350
1351 BUG_ON(len > dev->urbs.size);
1352
1353 urb->transfer_buffer_length = len; /* set to actual payload len */
1354 ret = usb_submit_urb(urb, GFP_KERNEL);
1355 if (ret) {
1356 dlfb_urb_completion(urb); /* because no one else will */
1357 atomic_set(&dev->lost_pixels, 1);
1358 dl_err("usb_submit_urb error %x\n", ret);
1359 }
1360 return ret;
1361}
1362
Bernie Thompson59277b62009-11-24 15:52:21 -08001363MODULE_AUTHOR("Roberto De Ioris <roberto@unbit.it>, "
1364 "Jaya Kumar <jayakumar.lkml@gmail.com>");
Roberto De Ioris88e58b12009-06-03 14:03:06 -07001365MODULE_DESCRIPTION(DRIVER_VERSION);
1366MODULE_LICENSE("GPL");