123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980 |
- /*
- * udlfb.c -- Framebuffer driver for DisplayLink USB controller
- *
- * Copyright (C) 2009 Roberto De Ioris <roberto@unbit.it>
- * Copyright (C) 2009 Jaya Kumar <jayakumar.lkml@gmail.com>
- * Copyright (C) 2009 Bernie Thompson <bernie@plugable.com>
- *
- * This file is subject to the terms and conditions of the GNU General Public
- * License v2. See the file COPYING in the main directory of this archive for
- * more details.
- *
- * Layout is based on skeletonfb by James Simmons and Geert Uytterhoeven,
- * usb-skeleton by GregKH.
- *
- * Device-specific portions based on information from Displaylink, with work
- * from Florian Echtler, Henrik Bjerregaard Pedersen, and others.
- */
- #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
- #include <linux/module.h>
- #include <linux/kernel.h>
- #include <linux/init.h>
- #include <linux/usb.h>
- #include <linux/uaccess.h>
- #include <linux/mm.h>
- #include <linux/fb.h>
- #include <linux/vmalloc.h>
- #include <linux/slab.h>
- #include <linux/prefetch.h>
- #include <linux/delay.h>
- #include <video/udlfb.h>
- #include "edid.h"
- static struct fb_fix_screeninfo dlfb_fix = {
- .id = "udlfb",
- .type = FB_TYPE_PACKED_PIXELS,
- .visual = FB_VISUAL_TRUECOLOR,
- .xpanstep = 0,
- .ypanstep = 0,
- .ywrapstep = 0,
- .accel = FB_ACCEL_NONE,
- };
- static const u32 udlfb_info_flags = FBINFO_DEFAULT | FBINFO_READS_FAST |
- FBINFO_VIRTFB |
- FBINFO_HWACCEL_IMAGEBLIT | FBINFO_HWACCEL_FILLRECT |
- FBINFO_HWACCEL_COPYAREA | FBINFO_MISC_ALWAYS_SETPAR;
- /*
- * There are many DisplayLink-based graphics products, all with unique PIDs.
- * So we match on DisplayLink's VID + Vendor-Defined Interface Class (0xff)
- * We also require a match on SubClass (0x00) and Protocol (0x00),
- * which is compatible with all known USB 2.0 era graphics chips and firmware,
- * but allows DisplayLink to increment those for any future incompatible chips
- */
- static struct usb_device_id id_table[] = {
- {.idVendor = 0x17e9,
- .bInterfaceClass = 0xff,
- .bInterfaceSubClass = 0x00,
- .bInterfaceProtocol = 0x00,
- .match_flags = USB_DEVICE_ID_MATCH_VENDOR |
- USB_DEVICE_ID_MATCH_INT_CLASS |
- USB_DEVICE_ID_MATCH_INT_SUBCLASS |
- USB_DEVICE_ID_MATCH_INT_PROTOCOL,
- },
- {},
- };
- MODULE_DEVICE_TABLE(usb, id_table);
- /* module options */
- static bool console = 1; /* Allow fbcon to open framebuffer */
- static bool fb_defio = 1; /* Detect mmap writes using page faults */
- static bool shadow = 1; /* Optionally disable shadow framebuffer */
- static int pixel_limit; /* Optionally force a pixel resolution limit */
- /* dlfb keeps a list of urbs for efficient bulk transfers */
- static void dlfb_urb_completion(struct urb *urb);
- static struct urb *dlfb_get_urb(struct dlfb_data *dev);
- static int dlfb_submit_urb(struct dlfb_data *dev, struct urb * urb, size_t len);
- static int dlfb_alloc_urb_list(struct dlfb_data *dev, int count, size_t size);
- static void dlfb_free_urb_list(struct dlfb_data *dev);
- /*
- * All DisplayLink bulk operations start with 0xAF, followed by specific code
- * All operations are written to buffers which then later get sent to device
- */
- static char *dlfb_set_register(char *buf, u8 reg, u8 val)
- {
- *buf++ = 0xAF;
- *buf++ = 0x20;
- *buf++ = reg;
- *buf++ = val;
- return buf;
- }
- static char *dlfb_vidreg_lock(char *buf)
- {
- return dlfb_set_register(buf, 0xFF, 0x00);
- }
- static char *dlfb_vidreg_unlock(char *buf)
- {
- return dlfb_set_register(buf, 0xFF, 0xFF);
- }
- /*
- * Map FB_BLANK_* to DisplayLink register
- * DLReg FB_BLANK_*
- * ----- -----------------------------
- * 0x00 FB_BLANK_UNBLANK (0)
- * 0x01 FB_BLANK (1)
- * 0x03 FB_BLANK_VSYNC_SUSPEND (2)
- * 0x05 FB_BLANK_HSYNC_SUSPEND (3)
- * 0x07 FB_BLANK_POWERDOWN (4) Note: requires modeset to come back
- */
- static char *dlfb_blanking(char *buf, int fb_blank)
- {
- u8 reg;
- switch (fb_blank) {
- case FB_BLANK_POWERDOWN:
- reg = 0x07;
- break;
- case FB_BLANK_HSYNC_SUSPEND:
- reg = 0x05;
- break;
- case FB_BLANK_VSYNC_SUSPEND:
- reg = 0x03;
- break;
- case FB_BLANK_NORMAL:
- reg = 0x01;
- break;
- default:
- reg = 0x00;
- }
- buf = dlfb_set_register(buf, 0x1F, reg);
- return buf;
- }
- static char *dlfb_set_color_depth(char *buf, u8 selection)
- {
- return dlfb_set_register(buf, 0x00, selection);
- }
- static char *dlfb_set_base16bpp(char *wrptr, u32 base)
- {
- /* the base pointer is 16 bits wide, 0x20 is hi byte. */
- wrptr = dlfb_set_register(wrptr, 0x20, base >> 16);
- wrptr = dlfb_set_register(wrptr, 0x21, base >> 8);
- return dlfb_set_register(wrptr, 0x22, base);
- }
- /*
- * DisplayLink HW has separate 16bpp and 8bpp framebuffers.
- * In 24bpp modes, the low 323 RGB bits go in the 8bpp framebuffer
- */
- static char *dlfb_set_base8bpp(char *wrptr, u32 base)
- {
- wrptr = dlfb_set_register(wrptr, 0x26, base >> 16);
- wrptr = dlfb_set_register(wrptr, 0x27, base >> 8);
- return dlfb_set_register(wrptr, 0x28, base);
- }
- static char *dlfb_set_register_16(char *wrptr, u8 reg, u16 value)
- {
- wrptr = dlfb_set_register(wrptr, reg, value >> 8);
- return dlfb_set_register(wrptr, reg+1, value);
- }
- /*
- * This is kind of weird because the controller takes some
- * register values in a different byte order than other registers.
- */
- static char *dlfb_set_register_16be(char *wrptr, u8 reg, u16 value)
- {
- wrptr = dlfb_set_register(wrptr, reg, value);
- return dlfb_set_register(wrptr, reg+1, value >> 8);
- }
- /*
- * LFSR is linear feedback shift register. The reason we have this is
- * because the display controller needs to minimize the clock depth of
- * various counters used in the display path. So this code reverses the
- * provided value into the lfsr16 value by counting backwards to get
- * the value that needs to be set in the hardware comparator to get the
- * same actual count. This makes sense once you read above a couple of
- * times and think about it from a hardware perspective.
- */
- static u16 dlfb_lfsr16(u16 actual_count)
- {
- u32 lv = 0xFFFF; /* This is the lfsr value that the hw starts with */
- while (actual_count--) {
- lv = ((lv << 1) |
- (((lv >> 15) ^ (lv >> 4) ^ (lv >> 2) ^ (lv >> 1)) & 1))
- & 0xFFFF;
- }
- return (u16) lv;
- }
- /*
- * This does LFSR conversion on the value that is to be written.
- * See LFSR explanation above for more detail.
- */
- static char *dlfb_set_register_lfsr16(char *wrptr, u8 reg, u16 value)
- {
- return dlfb_set_register_16(wrptr, reg, dlfb_lfsr16(value));
- }
- /*
- * This takes a standard fbdev screeninfo struct and all of its monitor mode
- * details and converts them into the DisplayLink equivalent register commands.
- */
- static char *dlfb_set_vid_cmds(char *wrptr, struct fb_var_screeninfo *var)
- {
- u16 xds, yds;
- u16 xde, yde;
- u16 yec;
- /* x display start */
- xds = var->left_margin + var->hsync_len;
- wrptr = dlfb_set_register_lfsr16(wrptr, 0x01, xds);
- /* x display end */
- xde = xds + var->xres;
- wrptr = dlfb_set_register_lfsr16(wrptr, 0x03, xde);
- /* y display start */
- yds = var->upper_margin + var->vsync_len;
- wrptr = dlfb_set_register_lfsr16(wrptr, 0x05, yds);
- /* y display end */
- yde = yds + var->yres;
- wrptr = dlfb_set_register_lfsr16(wrptr, 0x07, yde);
- /* x end count is active + blanking - 1 */
- wrptr = dlfb_set_register_lfsr16(wrptr, 0x09,
- xde + var->right_margin - 1);
- /* libdlo hardcodes hsync start to 1 */
- wrptr = dlfb_set_register_lfsr16(wrptr, 0x0B, 1);
- /* hsync end is width of sync pulse + 1 */
- wrptr = dlfb_set_register_lfsr16(wrptr, 0x0D, var->hsync_len + 1);
- /* hpixels is active pixels */
- wrptr = dlfb_set_register_16(wrptr, 0x0F, var->xres);
- /* yendcount is vertical active + vertical blanking */
- yec = var->yres + var->upper_margin + var->lower_margin +
- var->vsync_len;
- wrptr = dlfb_set_register_lfsr16(wrptr, 0x11, yec);
- /* libdlo hardcodes vsync start to 0 */
- wrptr = dlfb_set_register_lfsr16(wrptr, 0x13, 0);
- /* vsync end is width of vsync pulse */
- wrptr = dlfb_set_register_lfsr16(wrptr, 0x15, var->vsync_len);
- /* vpixels is active pixels */
- wrptr = dlfb_set_register_16(wrptr, 0x17, var->yres);
- /* convert picoseconds to 5kHz multiple for pclk5k = x * 1E12/5k */
- wrptr = dlfb_set_register_16be(wrptr, 0x1B,
- 200*1000*1000/var->pixclock);
- return wrptr;
- }
- /*
- * This takes a standard fbdev screeninfo struct that was fetched or prepared
- * and then generates the appropriate command sequence that then drives the
- * display controller.
- */
- static int dlfb_set_video_mode(struct dlfb_data *dev,
- struct fb_var_screeninfo *var)
- {
- char *buf;
- char *wrptr;
- int retval = 0;
- int writesize;
- struct urb *urb;
- if (!atomic_read(&dev->usb_active))
- return -EPERM;
- urb = dlfb_get_urb(dev);
- if (!urb)
- return -ENOMEM;
- buf = (char *) urb->transfer_buffer;
- /*
- * This first section has to do with setting the base address on the
- * controller * associated with the display. There are 2 base
- * pointers, currently, we only * use the 16 bpp segment.
- */
- wrptr = dlfb_vidreg_lock(buf);
- wrptr = dlfb_set_color_depth(wrptr, 0x00);
- /* set base for 16bpp segment to 0 */
- wrptr = dlfb_set_base16bpp(wrptr, 0);
- /* set base for 8bpp segment to end of fb */
- wrptr = dlfb_set_base8bpp(wrptr, dev->info->fix.smem_len);
- wrptr = dlfb_set_vid_cmds(wrptr, var);
- wrptr = dlfb_blanking(wrptr, FB_BLANK_UNBLANK);
- wrptr = dlfb_vidreg_unlock(wrptr);
- writesize = wrptr - buf;
- retval = dlfb_submit_urb(dev, urb, writesize);
- dev->blank_mode = FB_BLANK_UNBLANK;
- return retval;
- }
- static int dlfb_ops_mmap(struct fb_info *info, struct vm_area_struct *vma)
- {
- unsigned long start = vma->vm_start;
- unsigned long size = vma->vm_end - vma->vm_start;
- unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
- unsigned long page, pos;
- if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT))
- return -EINVAL;
- if (size > info->fix.smem_len)
- return -EINVAL;
- if (offset > info->fix.smem_len - size)
- return -EINVAL;
- pos = (unsigned long)info->fix.smem_start + offset;
- pr_notice("mmap() framebuffer addr:%lu size:%lu\n",
- pos, size);
- while (size > 0) {
- page = vmalloc_to_pfn((void *)pos);
- if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED))
- return -EAGAIN;
- start += PAGE_SIZE;
- pos += PAGE_SIZE;
- if (size > PAGE_SIZE)
- size -= PAGE_SIZE;
- else
- size = 0;
- }
- return 0;
- }
- /*
- * Trims identical data from front and back of line
- * Sets new front buffer address and width
- * And returns byte count of identical pixels
- * Assumes CPU natural alignment (unsigned long)
- * for back and front buffer ptrs and width
- */
- static int dlfb_trim_hline(const u8 *bback, const u8 **bfront, int *width_bytes)
- {
- int j, k;
- const unsigned long *back = (const unsigned long *) bback;
- const unsigned long *front = (const unsigned long *) *bfront;
- const int width = *width_bytes / sizeof(unsigned long);
- int identical = width;
- int start = width;
- int end = width;
- prefetch((void *) front);
- prefetch((void *) back);
- for (j = 0; j < width; j++) {
- if (back[j] != front[j]) {
- start = j;
- break;
- }
- }
- for (k = width - 1; k > j; k--) {
- if (back[k] != front[k]) {
- end = k+1;
- break;
- }
- }
- identical = start + (width - end);
- *bfront = (u8 *) &front[start];
- *width_bytes = (end - start) * sizeof(unsigned long);
- return identical * sizeof(unsigned long);
- }
- /*
- * Render a command stream for an encoded horizontal line segment of pixels.
- *
- * A command buffer holds several commands.
- * It always begins with a fresh command header
- * (the protocol doesn't require this, but we enforce it to allow
- * multiple buffers to be potentially encoded and sent in parallel).
- * A single command encodes one contiguous horizontal line of pixels
- *
- * The function relies on the client to do all allocation, so that
- * rendering can be done directly to output buffers (e.g. USB URBs).
- * The function fills the supplied command buffer, providing information
- * on where it left off, so the client may call in again with additional
- * buffers if the line will take several buffers to complete.
- *
- * A single command can transmit a maximum of 256 pixels,
- * regardless of the compression ratio (protocol design limit).
- * To the hardware, 0 for a size byte means 256
- *
- * Rather than 256 pixel commands which are either rl or raw encoded,
- * the rlx command simply assumes alternating raw and rl spans within one cmd.
- * This has a slightly larger header overhead, but produces more even results.
- * It also processes all data (read and write) in a single pass.
- * Performance benchmarks of common cases show it having just slightly better
- * compression than 256 pixel raw or rle commands, with similar CPU consumpion.
- * But for very rl friendly data, will compress not quite as well.
- */
- static void dlfb_compress_hline(
- const uint16_t **pixel_start_ptr,
- const uint16_t *const pixel_end,
- uint32_t *device_address_ptr,
- uint8_t **command_buffer_ptr,
- const uint8_t *const cmd_buffer_end)
- {
- const uint16_t *pixel = *pixel_start_ptr;
- uint32_t dev_addr = *device_address_ptr;
- uint8_t *cmd = *command_buffer_ptr;
- const int bpp = 2;
- while ((pixel_end > pixel) &&
- (cmd_buffer_end - MIN_RLX_CMD_BYTES > cmd)) {
- uint8_t *raw_pixels_count_byte = NULL;
- uint8_t *cmd_pixels_count_byte = NULL;
- const uint16_t *raw_pixel_start = NULL;
- const uint16_t *cmd_pixel_start, *cmd_pixel_end = NULL;
- prefetchw((void *) cmd); /* pull in one cache line at least */
- *cmd++ = 0xAF;
- *cmd++ = 0x6B;
- *cmd++ = (uint8_t) ((dev_addr >> 16) & 0xFF);
- *cmd++ = (uint8_t) ((dev_addr >> 8) & 0xFF);
- *cmd++ = (uint8_t) ((dev_addr) & 0xFF);
- cmd_pixels_count_byte = cmd++; /* we'll know this later */
- cmd_pixel_start = pixel;
- raw_pixels_count_byte = cmd++; /* we'll know this later */
- raw_pixel_start = pixel;
- cmd_pixel_end = pixel + min(MAX_CMD_PIXELS + 1,
- min((int)(pixel_end - pixel),
- (int)(cmd_buffer_end - cmd) / bpp));
- prefetch_range((void *) pixel, (cmd_pixel_end - pixel) * bpp);
- while (pixel < cmd_pixel_end) {
- const uint16_t * const repeating_pixel = pixel;
- *(uint16_t *)cmd = cpu_to_be16p(pixel);
- cmd += 2;
- pixel++;
- if (unlikely((pixel < cmd_pixel_end) &&
- (*pixel == *repeating_pixel))) {
- /* go back and fill in raw pixel count */
- *raw_pixels_count_byte = ((repeating_pixel -
- raw_pixel_start) + 1) & 0xFF;
- while ((pixel < cmd_pixel_end)
- && (*pixel == *repeating_pixel)) {
- pixel++;
- }
- /* immediately after raw data is repeat byte */
- *cmd++ = ((pixel - repeating_pixel) - 1) & 0xFF;
- /* Then start another raw pixel span */
- raw_pixel_start = pixel;
- raw_pixels_count_byte = cmd++;
- }
- }
- if (pixel > raw_pixel_start) {
- /* finalize last RAW span */
- *raw_pixels_count_byte = (pixel-raw_pixel_start) & 0xFF;
- }
- *cmd_pixels_count_byte = (pixel - cmd_pixel_start) & 0xFF;
- dev_addr += (pixel - cmd_pixel_start) * bpp;
- }
- if (cmd_buffer_end <= MIN_RLX_CMD_BYTES + cmd) {
- /* Fill leftover bytes with no-ops */
- if (cmd_buffer_end > cmd)
- memset(cmd, 0xAF, cmd_buffer_end - cmd);
- cmd = (uint8_t *) cmd_buffer_end;
- }
- *command_buffer_ptr = cmd;
- *pixel_start_ptr = pixel;
- *device_address_ptr = dev_addr;
- return;
- }
- /*
- * There are 3 copies of every pixel: The front buffer that the fbdev
- * client renders to, the actual framebuffer across the USB bus in hardware
- * (that we can only write to, slowly, and can never read), and (optionally)
- * our shadow copy that tracks what's been sent to that hardware buffer.
- */
- static int dlfb_render_hline(struct dlfb_data *dev, struct urb **urb_ptr,
- const char *front, char **urb_buf_ptr,
- u32 byte_offset, u32 byte_width,
- int *ident_ptr, int *sent_ptr)
- {
- const u8 *line_start, *line_end, *next_pixel;
- u32 dev_addr = dev->base16 + byte_offset;
- struct urb *urb = *urb_ptr;
- u8 *cmd = *urb_buf_ptr;
- u8 *cmd_end = (u8 *) urb->transfer_buffer + urb->transfer_buffer_length;
- line_start = (u8 *) (front + byte_offset);
- next_pixel = line_start;
- line_end = next_pixel + byte_width;
- if (dev->backing_buffer) {
- int offset;
- const u8 *back_start = (u8 *) (dev->backing_buffer
- + byte_offset);
- *ident_ptr += dlfb_trim_hline(back_start, &next_pixel,
- &byte_width);
- offset = next_pixel - line_start;
- line_end = next_pixel + byte_width;
- dev_addr += offset;
- back_start += offset;
- line_start += offset;
- memcpy((char *)back_start, (char *) line_start,
- byte_width);
- }
- while (next_pixel < line_end) {
- dlfb_compress_hline((const uint16_t **) &next_pixel,
- (const uint16_t *) line_end, &dev_addr,
- (u8 **) &cmd, (u8 *) cmd_end);
- if (cmd >= cmd_end) {
- int len = cmd - (u8 *) urb->transfer_buffer;
- if (dlfb_submit_urb(dev, urb, len))
- return 1; /* lost pixels is set */
- *sent_ptr += len;
- urb = dlfb_get_urb(dev);
- if (!urb)
- return 1; /* lost_pixels is set */
- *urb_ptr = urb;
- cmd = urb->transfer_buffer;
- cmd_end = &cmd[urb->transfer_buffer_length];
- }
- }
- *urb_buf_ptr = cmd;
- return 0;
- }
- static int dlfb_handle_damage(struct dlfb_data *dev, int x, int y,
- int width, int height, char *data)
- {
- int i, ret;
- char *cmd;
- cycles_t start_cycles, end_cycles;
- int bytes_sent = 0;
- int bytes_identical = 0;
- struct urb *urb;
- int aligned_x;
- start_cycles = get_cycles();
- aligned_x = DL_ALIGN_DOWN(x, sizeof(unsigned long));
- width = DL_ALIGN_UP(width + (x-aligned_x), sizeof(unsigned long));
- x = aligned_x;
- if ((width <= 0) ||
- (x + width > dev->info->var.xres) ||
- (y + height > dev->info->var.yres))
- return -EINVAL;
- if (!atomic_read(&dev->usb_active))
- return 0;
- urb = dlfb_get_urb(dev);
- if (!urb)
- return 0;
- cmd = urb->transfer_buffer;
- for (i = y; i < y + height ; i++) {
- const int line_offset = dev->info->fix.line_length * i;
- const int byte_offset = line_offset + (x * BPP);
- if (dlfb_render_hline(dev, &urb,
- (char *) dev->info->fix.smem_start,
- &cmd, byte_offset, width * BPP,
- &bytes_identical, &bytes_sent))
- goto error;
- }
- if (cmd > (char *) urb->transfer_buffer) {
- /* Send partial buffer remaining before exiting */
- int len = cmd - (char *) urb->transfer_buffer;
- ret = dlfb_submit_urb(dev, urb, len);
- bytes_sent += len;
- } else
- dlfb_urb_completion(urb);
- error:
- atomic_add(bytes_sent, &dev->bytes_sent);
- atomic_add(bytes_identical, &dev->bytes_identical);
- atomic_add(width*height*2, &dev->bytes_rendered);
- end_cycles = get_cycles();
- atomic_add(((unsigned int) ((end_cycles - start_cycles)
- >> 10)), /* Kcycles */
- &dev->cpu_kcycles_used);
- return 0;
- }
- /*
- * Path triggered by usermode clients who write to filesystem
- * e.g. cat filename > /dev/fb1
- * Not used by X Windows or text-mode console. But useful for testing.
- * Slow because of extra copy and we must assume all pixels dirty.
- */
- static ssize_t dlfb_ops_write(struct fb_info *info, const char __user *buf,
- size_t count, loff_t *ppos)
- {
- ssize_t result;
- struct dlfb_data *dev = info->par;
- u32 offset = (u32) *ppos;
- result = fb_sys_write(info, buf, count, ppos);
- if (result > 0) {
- int start = max((int)(offset / info->fix.line_length), 0);
- int lines = min((u32)((result / info->fix.line_length) + 1),
- (u32)info->var.yres);
- dlfb_handle_damage(dev, 0, start, info->var.xres,
- lines, info->screen_base);
- }
- return result;
- }
- /* hardware has native COPY command (see libdlo), but not worth it for fbcon */
- static void dlfb_ops_copyarea(struct fb_info *info,
- const struct fb_copyarea *area)
- {
- struct dlfb_data *dev = info->par;
- sys_copyarea(info, area);
- dlfb_handle_damage(dev, area->dx, area->dy,
- area->width, area->height, info->screen_base);
- }
- static void dlfb_ops_imageblit(struct fb_info *info,
- const struct fb_image *image)
- {
- struct dlfb_data *dev = info->par;
- sys_imageblit(info, image);
- dlfb_handle_damage(dev, image->dx, image->dy,
- image->width, image->height, info->screen_base);
- }
- static void dlfb_ops_fillrect(struct fb_info *info,
- const struct fb_fillrect *rect)
- {
- struct dlfb_data *dev = info->par;
- sys_fillrect(info, rect);
- dlfb_handle_damage(dev, rect->dx, rect->dy, rect->width,
- rect->height, info->screen_base);
- }
- /*
- * NOTE: fb_defio.c is holding info->fbdefio.mutex
- * Touching ANY framebuffer memory that triggers a page fault
- * in fb_defio will cause a deadlock, when it also tries to
- * grab the same mutex.
- */
- static void dlfb_dpy_deferred_io(struct fb_info *info,
- struct list_head *pagelist)
- {
- struct page *cur;
- struct fb_deferred_io *fbdefio = info->fbdefio;
- struct dlfb_data *dev = info->par;
- struct urb *urb;
- char *cmd;
- cycles_t start_cycles, end_cycles;
- int bytes_sent = 0;
- int bytes_identical = 0;
- int bytes_rendered = 0;
- if (!fb_defio)
- return;
- if (!atomic_read(&dev->usb_active))
- return;
- start_cycles = get_cycles();
- urb = dlfb_get_urb(dev);
- if (!urb)
- return;
- cmd = urb->transfer_buffer;
- /* walk the written page list and render each to device */
- list_for_each_entry(cur, &fbdefio->pagelist, lru) {
- if (dlfb_render_hline(dev, &urb, (char *) info->fix.smem_start,
- &cmd, cur->index << PAGE_SHIFT,
- PAGE_SIZE, &bytes_identical, &bytes_sent))
- goto error;
- bytes_rendered += PAGE_SIZE;
- }
- if (cmd > (char *) urb->transfer_buffer) {
- /* Send partial buffer remaining before exiting */
- int len = cmd - (char *) urb->transfer_buffer;
- dlfb_submit_urb(dev, urb, len);
- bytes_sent += len;
- } else
- dlfb_urb_completion(urb);
- error:
- atomic_add(bytes_sent, &dev->bytes_sent);
- atomic_add(bytes_identical, &dev->bytes_identical);
- atomic_add(bytes_rendered, &dev->bytes_rendered);
- end_cycles = get_cycles();
- atomic_add(((unsigned int) ((end_cycles - start_cycles)
- >> 10)), /* Kcycles */
- &dev->cpu_kcycles_used);
- }
- static int dlfb_get_edid(struct dlfb_data *dev, char *edid, int len)
- {
- int i;
- int ret;
- char *rbuf;
- rbuf = kmalloc(2, GFP_KERNEL);
- if (!rbuf)
- return 0;
- for (i = 0; i < len; i++) {
- ret = usb_control_msg(dev->udev,
- usb_rcvctrlpipe(dev->udev, 0), (0x02),
- (0x80 | (0x02 << 5)), i << 8, 0xA1, rbuf, 2,
- HZ);
- if (ret < 1) {
- pr_err("Read EDID byte %d failed err %x\n", i, ret);
- i--;
- break;
- }
- edid[i] = rbuf[1];
- }
- kfree(rbuf);
- return i;
- }
- static int dlfb_ops_ioctl(struct fb_info *info, unsigned int cmd,
- unsigned long arg)
- {
- struct dlfb_data *dev = info->par;
- if (!atomic_read(&dev->usb_active))
- return 0;
- /* TODO: Update X server to get this from sysfs instead */
- if (cmd == DLFB_IOCTL_RETURN_EDID) {
- void __user *edid = (void __user *)arg;
- if (copy_to_user(edid, dev->edid, dev->edid_size))
- return -EFAULT;
- return 0;
- }
- /* TODO: Help propose a standard fb.h ioctl to report mmap damage */
- if (cmd == DLFB_IOCTL_REPORT_DAMAGE) {
- struct dloarea area;
- if (copy_from_user(&area, (void __user *)arg,
- sizeof(struct dloarea)))
- return -EFAULT;
- /*
- * If we have a damage-aware client, turn fb_defio "off"
- * To avoid perf imact of unnecessary page fault handling.
- * Done by resetting the delay for this fb_info to a very
- * long period. Pages will become writable and stay that way.
- * Reset to normal value when all clients have closed this fb.
- */
- if (info->fbdefio)
- info->fbdefio->delay = DL_DEFIO_WRITE_DISABLE;
- if (area.x < 0)
- area.x = 0;
- if (area.x > info->var.xres)
- area.x = info->var.xres;
- if (area.y < 0)
- area.y = 0;
- if (area.y > info->var.yres)
- area.y = info->var.yres;
- dlfb_handle_damage(dev, area.x, area.y, area.w, area.h,
- info->screen_base);
- }
- return 0;
- }
- /* taken from vesafb */
- static int
- dlfb_ops_setcolreg(unsigned regno, unsigned red, unsigned green,
- unsigned blue, unsigned transp, struct fb_info *info)
- {
- int err = 0;
- if (regno >= info->cmap.len)
- return 1;
- if (regno < 16) {
- if (info->var.red.offset == 10) {
- /* 1:5:5:5 */
- ((u32 *) (info->pseudo_palette))[regno] =
- ((red & 0xf800) >> 1) |
- ((green & 0xf800) >> 6) | ((blue & 0xf800) >> 11);
- } else {
- /* 0:5:6:5 */
- ((u32 *) (info->pseudo_palette))[regno] =
- ((red & 0xf800)) |
- ((green & 0xfc00) >> 5) | ((blue & 0xf800) >> 11);
- }
- }
- return err;
- }
- /*
- * It's common for several clients to have framebuffer open simultaneously.
- * e.g. both fbcon and X. Makes things interesting.
- * Assumes caller is holding info->lock (for open and release at least)
- */
- static int dlfb_ops_open(struct fb_info *info, int user)
- {
- struct dlfb_data *dev = info->par;
- /*
- * fbcon aggressively connects to first framebuffer it finds,
- * preventing other clients (X) from working properly. Usually
- * not what the user wants. Fail by default with option to enable.
- */
- if ((user == 0) && (!console))
- return -EBUSY;
- /* If the USB device is gone, we don't accept new opens */
- if (dev->virtualized)
- return -ENODEV;
- dev->fb_count++;
- kref_get(&dev->kref);
- if (fb_defio && (info->fbdefio == NULL)) {
- /* enable defio at last moment if not disabled by client */
- struct fb_deferred_io *fbdefio;
- fbdefio = kzalloc(sizeof(struct fb_deferred_io), GFP_KERNEL);
- if (fbdefio) {
- fbdefio->delay = DL_DEFIO_WRITE_DELAY;
- fbdefio->deferred_io = dlfb_dpy_deferred_io;
- }
- info->fbdefio = fbdefio;
- fb_deferred_io_init(info);
- }
- pr_notice("open /dev/fb%d user=%d fb_info=%p count=%d\n",
- info->node, user, info, dev->fb_count);
- return 0;
- }
- /*
- * Called when all client interfaces to start transactions have been disabled,
- * and all references to our device instance (dlfb_data) are released.
- * Every transaction must have a reference, so we know are fully spun down
- */
- static void dlfb_free(struct kref *kref)
- {
- struct dlfb_data *dev = container_of(kref, struct dlfb_data, kref);
- vfree(dev->backing_buffer);
- kfree(dev->edid);
- pr_warn("freeing dlfb_data %p\n", dev);
- kfree(dev);
- }
- static void dlfb_release_urb_work(struct work_struct *work)
- {
- struct urb_node *unode = container_of(work, struct urb_node,
- release_urb_work.work);
- up(&unode->dev->urbs.limit_sem);
- }
- static void dlfb_free_framebuffer(struct dlfb_data *dev)
- {
- struct fb_info *info = dev->info;
- if (info) {
- int node = info->node;
- unregister_framebuffer(info);
- if (info->cmap.len != 0)
- fb_dealloc_cmap(&info->cmap);
- if (info->monspecs.modedb)
- fb_destroy_modedb(info->monspecs.modedb);
- vfree(info->screen_base);
- fb_destroy_modelist(&info->modelist);
- dev->info = NULL;
- /* Assume info structure is freed after this point */
- framebuffer_release(info);
- pr_warn("fb_info for /dev/fb%d has been freed\n", node);
- }
- /* ref taken in probe() as part of registering framebfufer */
- kref_put(&dev->kref, dlfb_free);
- }
- static void dlfb_free_framebuffer_work(struct work_struct *work)
- {
- struct dlfb_data *dev = container_of(work, struct dlfb_data,
- free_framebuffer_work.work);
- dlfb_free_framebuffer(dev);
- }
- /*
- * Assumes caller is holding info->lock mutex (for open and release at least)
- */
- static int dlfb_ops_release(struct fb_info *info, int user)
- {
- struct dlfb_data *dev = info->par;
- dev->fb_count--;
- /* We can't free fb_info here - fbmem will touch it when we return */
- if (dev->virtualized && (dev->fb_count == 0))
- schedule_delayed_work(&dev->free_framebuffer_work, HZ);
- if ((dev->fb_count == 0) && (info->fbdefio)) {
- fb_deferred_io_cleanup(info);
- kfree(info->fbdefio);
- info->fbdefio = NULL;
- info->fbops->fb_mmap = dlfb_ops_mmap;
- }
- pr_warn("released /dev/fb%d user=%d count=%d\n",
- info->node, user, dev->fb_count);
- kref_put(&dev->kref, dlfb_free);
- return 0;
- }
- /*
- * Check whether a video mode is supported by the DisplayLink chip
- * We start from monitor's modes, so don't need to filter that here
- */
- static int dlfb_is_valid_mode(struct fb_videomode *mode,
- struct fb_info *info)
- {
- struct dlfb_data *dev = info->par;
- if (mode->xres * mode->yres > dev->sku_pixel_limit) {
- pr_warn("%dx%d beyond chip capabilities\n",
- mode->xres, mode->yres);
- return 0;
- }
- pr_info("%dx%d @ %d Hz valid mode\n", mode->xres, mode->yres,
- mode->refresh);
- return 1;
- }
- static void dlfb_var_color_format(struct fb_var_screeninfo *var)
- {
- const struct fb_bitfield red = { 11, 5, 0 };
- const struct fb_bitfield green = { 5, 6, 0 };
- const struct fb_bitfield blue = { 0, 5, 0 };
- var->bits_per_pixel = 16;
- var->red = red;
- var->green = green;
- var->blue = blue;
- }
- static int dlfb_ops_check_var(struct fb_var_screeninfo *var,
- struct fb_info *info)
- {
- struct fb_videomode mode;
- /* TODO: support dynamically changing framebuffer size */
- if ((var->xres * var->yres * 2) > info->fix.smem_len)
- return -EINVAL;
- /* set device-specific elements of var unrelated to mode */
- dlfb_var_color_format(var);
- fb_var_to_videomode(&mode, var);
- if (!dlfb_is_valid_mode(&mode, info))
- return -EINVAL;
- return 0;
- }
- static int dlfb_ops_set_par(struct fb_info *info)
- {
- struct dlfb_data *dev = info->par;
- int result;
- u16 *pix_framebuffer;
- int i;
- pr_notice("set_par mode %dx%d\n", info->var.xres, info->var.yres);
- result = dlfb_set_video_mode(dev, &info->var);
- if ((result == 0) && (dev->fb_count == 0)) {
- /* paint greenscreen */
- pix_framebuffer = (u16 *) info->screen_base;
- for (i = 0; i < info->fix.smem_len / 2; i++)
- pix_framebuffer[i] = 0x37e6;
- dlfb_handle_damage(dev, 0, 0, info->var.xres, info->var.yres,
- info->screen_base);
- }
- return result;
- }
- /* To fonzi the jukebox (e.g. make blanking changes take effect) */
- static char *dlfb_dummy_render(char *buf)
- {
- *buf++ = 0xAF;
- *buf++ = 0x6A; /* copy */
- *buf++ = 0x00; /* from address*/
- *buf++ = 0x00;
- *buf++ = 0x00;
- *buf++ = 0x01; /* one pixel */
- *buf++ = 0x00; /* to address */
- *buf++ = 0x00;
- *buf++ = 0x00;
- return buf;
- }
- /*
- * In order to come back from full DPMS off, we need to set the mode again
- */
- static int dlfb_ops_blank(int blank_mode, struct fb_info *info)
- {
- struct dlfb_data *dev = info->par;
- char *bufptr;
- struct urb *urb;
- pr_info("/dev/fb%d FB_BLANK mode %d --> %d\n",
- info->node, dev->blank_mode, blank_mode);
- if ((dev->blank_mode == FB_BLANK_POWERDOWN) &&
- (blank_mode != FB_BLANK_POWERDOWN)) {
- /* returning from powerdown requires a fresh modeset */
- dlfb_set_video_mode(dev, &info->var);
- }
- urb = dlfb_get_urb(dev);
- if (!urb)
- return 0;
- bufptr = (char *) urb->transfer_buffer;
- bufptr = dlfb_vidreg_lock(bufptr);
- bufptr = dlfb_blanking(bufptr, blank_mode);
- bufptr = dlfb_vidreg_unlock(bufptr);
- /* seems like a render op is needed to have blank change take effect */
- bufptr = dlfb_dummy_render(bufptr);
- dlfb_submit_urb(dev, urb, bufptr -
- (char *) urb->transfer_buffer);
- dev->blank_mode = blank_mode;
- return 0;
- }
- static struct fb_ops dlfb_ops = {
- .owner = THIS_MODULE,
- .fb_read = fb_sys_read,
- .fb_write = dlfb_ops_write,
- .fb_setcolreg = dlfb_ops_setcolreg,
- .fb_fillrect = dlfb_ops_fillrect,
- .fb_copyarea = dlfb_ops_copyarea,
- .fb_imageblit = dlfb_ops_imageblit,
- .fb_mmap = dlfb_ops_mmap,
- .fb_ioctl = dlfb_ops_ioctl,
- .fb_open = dlfb_ops_open,
- .fb_release = dlfb_ops_release,
- .fb_blank = dlfb_ops_blank,
- .fb_check_var = dlfb_ops_check_var,
- .fb_set_par = dlfb_ops_set_par,
- };
- /*
- * Assumes &info->lock held by caller
- * Assumes no active clients have framebuffer open
- */
- static int dlfb_realloc_framebuffer(struct dlfb_data *dev, struct fb_info *info)
- {
- int retval = -ENOMEM;
- int old_len = info->fix.smem_len;
- int new_len;
- unsigned char *old_fb = info->screen_base;
- unsigned char *new_fb;
- unsigned char *new_back = NULL;
- pr_warn("Reallocating framebuffer. Addresses will change!\n");
- new_len = info->fix.line_length * info->var.yres;
- if (PAGE_ALIGN(new_len) > old_len) {
- /*
- * Alloc system memory for virtual framebuffer
- */
- new_fb = vmalloc(new_len);
- if (!new_fb) {
- pr_err("Virtual framebuffer alloc failed\n");
- goto error;
- }
- if (info->screen_base) {
- memcpy(new_fb, old_fb, old_len);
- vfree(info->screen_base);
- }
- info->screen_base = new_fb;
- info->fix.smem_len = PAGE_ALIGN(new_len);
- info->fix.smem_start = (unsigned long) new_fb;
- info->flags = udlfb_info_flags;
- /*
- * Second framebuffer copy to mirror the framebuffer state
- * on the physical USB device. We can function without this.
- * But with imperfect damage info we may send pixels over USB
- * that were, in fact, unchanged - wasting limited USB bandwidth
- */
- if (shadow)
- new_back = vzalloc(new_len);
- if (!new_back)
- pr_info("No shadow/backing buffer allocated\n");
- else {
- vfree(dev->backing_buffer);
- dev->backing_buffer = new_back;
- }
- }
- retval = 0;
- error:
- return retval;
- }
- /*
- * 1) Get EDID from hw, or use sw default
- * 2) Parse into various fb_info structs
- * 3) Allocate virtual framebuffer memory to back highest res mode
- *
- * Parses EDID into three places used by various parts of fbdev:
- * fb_var_screeninfo contains the timing of the monitor's preferred mode
- * fb_info.monspecs is full parsed EDID info, including monspecs.modedb
- * fb_info.modelist is a linked list of all monitor & VESA modes which work
- *
- * If EDID is not readable/valid, then modelist is all VESA modes,
- * monspecs is NULL, and fb_var_screeninfo is set to safe VESA mode
- * Returns 0 if successful
- */
- static int dlfb_setup_modes(struct dlfb_data *dev,
- struct fb_info *info,
- char *default_edid, size_t default_edid_size)
- {
- int i;
- const struct fb_videomode *default_vmode = NULL;
- int result = 0;
- char *edid;
- int tries = 3;
- if (info->dev) /* only use mutex if info has been registered */
- mutex_lock(&info->lock);
- edid = kmalloc(EDID_LENGTH, GFP_KERNEL);
- if (!edid) {
- result = -ENOMEM;
- goto error;
- }
- fb_destroy_modelist(&info->modelist);
- memset(&info->monspecs, 0, sizeof(info->monspecs));
- /*
- * Try to (re)read EDID from hardware first
- * EDID data may return, but not parse as valid
- * Try again a few times, in case of e.g. analog cable noise
- */
- while (tries--) {
- i = dlfb_get_edid(dev, edid, EDID_LENGTH);
- if (i >= EDID_LENGTH)
- fb_edid_to_monspecs(edid, &info->monspecs);
- if (info->monspecs.modedb_len > 0) {
- dev->edid = edid;
- dev->edid_size = i;
- break;
- }
- }
- /* If that fails, use a previously returned EDID if available */
- if (info->monspecs.modedb_len == 0) {
- pr_err("Unable to get valid EDID from device/display\n");
- if (dev->edid) {
- fb_edid_to_monspecs(dev->edid, &info->monspecs);
- if (info->monspecs.modedb_len > 0)
- pr_err("Using previously queried EDID\n");
- }
- }
- /* If that fails, use the default EDID we were handed */
- if (info->monspecs.modedb_len == 0) {
- if (default_edid_size >= EDID_LENGTH) {
- fb_edid_to_monspecs(default_edid, &info->monspecs);
- if (info->monspecs.modedb_len > 0) {
- memcpy(edid, default_edid, default_edid_size);
- dev->edid = edid;
- dev->edid_size = default_edid_size;
- pr_err("Using default/backup EDID\n");
- }
- }
- }
- /* If we've got modes, let's pick a best default mode */
- if (info->monspecs.modedb_len > 0) {
- for (i = 0; i < info->monspecs.modedb_len; i++) {
- if (dlfb_is_valid_mode(&info->monspecs.modedb[i], info))
- fb_add_videomode(&info->monspecs.modedb[i],
- &info->modelist);
- else {
- if (i == 0)
- /* if we've removed top/best mode */
- info->monspecs.misc
- &= ~FB_MISC_1ST_DETAIL;
- }
- }
- default_vmode = fb_find_best_display(&info->monspecs,
- &info->modelist);
- }
- /* If everything else has failed, fall back to safe default mode */
- if (default_vmode == NULL) {
- struct fb_videomode fb_vmode = {0};
- /*
- * Add the standard VESA modes to our modelist
- * Since we don't have EDID, there may be modes that
- * overspec monitor and/or are incorrect aspect ratio, etc.
- * But at least the user has a chance to choose
- */
- for (i = 0; i < VESA_MODEDB_SIZE; i++) {
- if (dlfb_is_valid_mode((struct fb_videomode *)
- &vesa_modes[i], info))
- fb_add_videomode(&vesa_modes[i],
- &info->modelist);
- }
- /*
- * default to resolution safe for projectors
- * (since they are most common case without EDID)
- */
- fb_vmode.xres = 800;
- fb_vmode.yres = 600;
- fb_vmode.refresh = 60;
- default_vmode = fb_find_nearest_mode(&fb_vmode,
- &info->modelist);
- }
- /* If we have good mode and no active clients*/
- if ((default_vmode != NULL) && (dev->fb_count == 0)) {
- fb_videomode_to_var(&info->var, default_vmode);
- dlfb_var_color_format(&info->var);
- /*
- * with mode size info, we can now alloc our framebuffer.
- */
- memcpy(&info->fix, &dlfb_fix, sizeof(dlfb_fix));
- info->fix.line_length = info->var.xres *
- (info->var.bits_per_pixel / 8);
- result = dlfb_realloc_framebuffer(dev, info);
- } else
- result = -EINVAL;
- error:
- if (edid && (dev->edid != edid))
- kfree(edid);
- if (info->dev)
- mutex_unlock(&info->lock);
- return result;
- }
- static ssize_t metrics_bytes_rendered_show(struct device *fbdev,
- struct device_attribute *a, char *buf) {
- struct fb_info *fb_info = dev_get_drvdata(fbdev);
- struct dlfb_data *dev = fb_info->par;
- return snprintf(buf, PAGE_SIZE, "%u\n",
- atomic_read(&dev->bytes_rendered));
- }
- static ssize_t metrics_bytes_identical_show(struct device *fbdev,
- struct device_attribute *a, char *buf) {
- struct fb_info *fb_info = dev_get_drvdata(fbdev);
- struct dlfb_data *dev = fb_info->par;
- return snprintf(buf, PAGE_SIZE, "%u\n",
- atomic_read(&dev->bytes_identical));
- }
- static ssize_t metrics_bytes_sent_show(struct device *fbdev,
- struct device_attribute *a, char *buf) {
- struct fb_info *fb_info = dev_get_drvdata(fbdev);
- struct dlfb_data *dev = fb_info->par;
- return snprintf(buf, PAGE_SIZE, "%u\n",
- atomic_read(&dev->bytes_sent));
- }
- static ssize_t metrics_cpu_kcycles_used_show(struct device *fbdev,
- struct device_attribute *a, char *buf) {
- struct fb_info *fb_info = dev_get_drvdata(fbdev);
- struct dlfb_data *dev = fb_info->par;
- return snprintf(buf, PAGE_SIZE, "%u\n",
- atomic_read(&dev->cpu_kcycles_used));
- }
- static ssize_t edid_show(
- struct file *filp,
- struct kobject *kobj, struct bin_attribute *a,
- char *buf, loff_t off, size_t count) {
- struct device *fbdev = container_of(kobj, struct device, kobj);
- struct fb_info *fb_info = dev_get_drvdata(fbdev);
- struct dlfb_data *dev = fb_info->par;
- if (dev->edid == NULL)
- return 0;
- if ((off >= dev->edid_size) || (count > dev->edid_size))
- return 0;
- if (off + count > dev->edid_size)
- count = dev->edid_size - off;
- pr_info("sysfs edid copy %p to %p, %d bytes\n",
- dev->edid, buf, (int) count);
- memcpy(buf, dev->edid, count);
- return count;
- }
- static ssize_t edid_store(
- struct file *filp,
- struct kobject *kobj, struct bin_attribute *a,
- char *src, loff_t src_off, size_t src_size) {
- struct device *fbdev = container_of(kobj, struct device, kobj);
- struct fb_info *fb_info = dev_get_drvdata(fbdev);
- struct dlfb_data *dev = fb_info->par;
- int ret;
- /* We only support write of entire EDID at once, no offset*/
- if ((src_size != EDID_LENGTH) || (src_off != 0))
- return -EINVAL;
- ret = dlfb_setup_modes(dev, fb_info, src, src_size);
- if (ret)
- return ret;
- if (!dev->edid || memcmp(src, dev->edid, src_size))
- return -EINVAL;
- pr_info("sysfs written EDID is new default\n");
- dlfb_ops_set_par(fb_info);
- return src_size;
- }
- static ssize_t metrics_reset_store(struct device *fbdev,
- struct device_attribute *attr,
- const char *buf, size_t count)
- {
- struct fb_info *fb_info = dev_get_drvdata(fbdev);
- struct dlfb_data *dev = fb_info->par;
- atomic_set(&dev->bytes_rendered, 0);
- atomic_set(&dev->bytes_identical, 0);
- atomic_set(&dev->bytes_sent, 0);
- atomic_set(&dev->cpu_kcycles_used, 0);
- return count;
- }
- static struct bin_attribute edid_attr = {
- .attr.name = "edid",
- .attr.mode = 0666,
- .size = EDID_LENGTH,
- .read = edid_show,
- .write = edid_store
- };
- static struct device_attribute fb_device_attrs[] = {
- __ATTR_RO(metrics_bytes_rendered),
- __ATTR_RO(metrics_bytes_identical),
- __ATTR_RO(metrics_bytes_sent),
- __ATTR_RO(metrics_cpu_kcycles_used),
- __ATTR(metrics_reset, S_IWUSR, NULL, metrics_reset_store),
- };
- /*
- * This is necessary before we can communicate with the display controller.
- */
- static int dlfb_select_std_channel(struct dlfb_data *dev)
- {
- int ret;
- u8 set_def_chn[] = { 0x57, 0xCD, 0xDC, 0xA7,
- 0x1C, 0x88, 0x5E, 0x15,
- 0x60, 0xFE, 0xC6, 0x97,
- 0x16, 0x3D, 0x47, 0xF2 };
- ret = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
- NR_USB_REQUEST_CHANNEL,
- (USB_DIR_OUT | USB_TYPE_VENDOR), 0, 0,
- set_def_chn, sizeof(set_def_chn), USB_CTRL_SET_TIMEOUT);
- return ret;
- }
- static int dlfb_parse_vendor_descriptor(struct dlfb_data *dev,
- struct usb_interface *interface)
- {
- char *desc;
- char *buf;
- char *desc_end;
- int total_len = 0;
- buf = kzalloc(MAX_VENDOR_DESCRIPTOR_SIZE, GFP_KERNEL);
- if (!buf)
- return false;
- desc = buf;
- total_len = usb_get_descriptor(interface_to_usbdev(interface),
- 0x5f, /* vendor specific */
- 0, desc, MAX_VENDOR_DESCRIPTOR_SIZE);
- /* if not found, look in configuration descriptor */
- if (total_len < 0) {
- if (0 == usb_get_extra_descriptor(interface->cur_altsetting,
- 0x5f, &desc))
- total_len = (int) desc[0];
- }
- if (total_len > 5) {
- pr_info("vendor descriptor length:%x data:%11ph\n", total_len,
- desc);
- if ((desc[0] != total_len) || /* descriptor length */
- (desc[1] != 0x5f) || /* vendor descriptor type */
- (desc[2] != 0x01) || /* version (2 bytes) */
- (desc[3] != 0x00) ||
- (desc[4] != total_len - 2)) /* length after type */
- goto unrecognized;
- desc_end = desc + total_len;
- desc += 5; /* the fixed header we've already parsed */
- while (desc < desc_end) {
- u8 length;
- u16 key;
- key = le16_to_cpu(*((u16 *) desc));
- desc += sizeof(u16);
- length = *desc;
- desc++;
- switch (key) {
- case 0x0200: { /* max_area */
- u32 max_area;
- max_area = le32_to_cpu(*((u32 *)desc));
- pr_warn("DL chip limited to %d pixel modes\n",
- max_area);
- dev->sku_pixel_limit = max_area;
- break;
- }
- default:
- break;
- }
- desc += length;
- }
- } else {
- pr_info("vendor descriptor not available (%d)\n", total_len);
- }
- goto success;
- unrecognized:
- /* allow udlfb to load for now even if firmware unrecognized */
- pr_err("Unrecognized vendor firmware descriptor\n");
- success:
- kfree(buf);
- return true;
- }
- static void dlfb_init_framebuffer_work(struct work_struct *work);
- static int dlfb_usb_probe(struct usb_interface *interface,
- const struct usb_device_id *id)
- {
- struct usb_device *usbdev;
- struct dlfb_data *dev = NULL;
- int retval = -ENOMEM;
- /* usb initialization */
- usbdev = interface_to_usbdev(interface);
- dev = kzalloc(sizeof(*dev), GFP_KERNEL);
- if (dev == NULL) {
- dev_err(&interface->dev, "dlfb_usb_probe: failed alloc of dev struct\n");
- goto error;
- }
- kref_init(&dev->kref); /* matching kref_put in usb .disconnect fn */
- dev->udev = usbdev;
- dev->gdev = &usbdev->dev; /* our generic struct device * */
- usb_set_intfdata(interface, dev);
- pr_info("%s %s - serial #%s\n",
- usbdev->manufacturer, usbdev->product, usbdev->serial);
- pr_info("vid_%04x&pid_%04x&rev_%04x driver's dlfb_data struct at %p\n",
- usbdev->descriptor.idVendor, usbdev->descriptor.idProduct,
- usbdev->descriptor.bcdDevice, dev);
- pr_info("console enable=%d\n", console);
- pr_info("fb_defio enable=%d\n", fb_defio);
- pr_info("shadow enable=%d\n", shadow);
- dev->sku_pixel_limit = 2048 * 1152; /* default to maximum */
- if (!dlfb_parse_vendor_descriptor(dev, interface)) {
- pr_err("firmware not recognized. Assume incompatible device\n");
- goto error;
- }
- if (pixel_limit) {
- pr_warn("DL chip limit of %d overridden"
- " by module param to %d\n",
- dev->sku_pixel_limit, pixel_limit);
- dev->sku_pixel_limit = pixel_limit;
- }
- if (!dlfb_alloc_urb_list(dev, WRITES_IN_FLIGHT, MAX_TRANSFER)) {
- retval = -ENOMEM;
- pr_err("dlfb_alloc_urb_list failed\n");
- goto error;
- }
- kref_get(&dev->kref); /* matching kref_put in free_framebuffer_work */
- /* We don't register a new USB class. Our client interface is fbdev */
- /* Workitem keep things fast & simple during USB enumeration */
- INIT_DELAYED_WORK(&dev->init_framebuffer_work,
- dlfb_init_framebuffer_work);
- schedule_delayed_work(&dev->init_framebuffer_work, 0);
- return 0;
- error:
- if (dev) {
- kref_put(&dev->kref, dlfb_free); /* ref for framebuffer */
- kref_put(&dev->kref, dlfb_free); /* last ref from kref_init */
- /* dev has been deallocated. Do not dereference */
- }
- return retval;
- }
- static void dlfb_init_framebuffer_work(struct work_struct *work)
- {
- struct dlfb_data *dev = container_of(work, struct dlfb_data,
- init_framebuffer_work.work);
- struct fb_info *info;
- int retval;
- int i;
- /* allocates framebuffer driver structure, not framebuffer memory */
- info = framebuffer_alloc(0, dev->gdev);
- if (!info) {
- retval = -ENOMEM;
- pr_err("framebuffer_alloc failed\n");
- goto error;
- }
- dev->info = info;
- info->par = dev;
- info->pseudo_palette = dev->pseudo_palette;
- info->fbops = &dlfb_ops;
- retval = fb_alloc_cmap(&info->cmap, 256, 0);
- if (retval < 0) {
- pr_err("fb_alloc_cmap failed %x\n", retval);
- goto error;
- }
- INIT_DELAYED_WORK(&dev->free_framebuffer_work,
- dlfb_free_framebuffer_work);
- INIT_LIST_HEAD(&info->modelist);
- retval = dlfb_setup_modes(dev, info, NULL, 0);
- if (retval != 0) {
- pr_err("unable to find common mode for display and adapter\n");
- goto error;
- }
- /* ready to begin using device */
- atomic_set(&dev->usb_active, 1);
- dlfb_select_std_channel(dev);
- dlfb_ops_check_var(&info->var, info);
- dlfb_ops_set_par(info);
- retval = register_framebuffer(info);
- if (retval < 0) {
- pr_err("register_framebuffer failed %d\n", retval);
- goto error;
- }
- for (i = 0; i < ARRAY_SIZE(fb_device_attrs); i++) {
- retval = device_create_file(info->dev, &fb_device_attrs[i]);
- if (retval) {
- pr_warn("device_create_file failed %d\n", retval);
- }
- }
- retval = device_create_bin_file(info->dev, &edid_attr);
- if (retval) {
- pr_warn("device_create_bin_file failed %d\n", retval);
- }
- pr_info("DisplayLink USB device /dev/fb%d attached. %dx%d resolution."
- " Using %dK framebuffer memory\n", info->node,
- info->var.xres, info->var.yres,
- ((dev->backing_buffer) ?
- info->fix.smem_len * 2 : info->fix.smem_len) >> 10);
- return;
- error:
- dlfb_free_framebuffer(dev);
- }
- static void dlfb_usb_disconnect(struct usb_interface *interface)
- {
- struct dlfb_data *dev;
- struct fb_info *info;
- int i;
- dev = usb_get_intfdata(interface);
- info = dev->info;
- pr_info("USB disconnect starting\n");
- /* we virtualize until all fb clients release. Then we free */
- dev->virtualized = true;
- /* When non-active we'll update virtual framebuffer, but no new urbs */
- atomic_set(&dev->usb_active, 0);
- /* this function will wait for all in-flight urbs to complete */
- dlfb_free_urb_list(dev);
- if (info) {
- /* remove udlfb's sysfs interfaces */
- for (i = 0; i < ARRAY_SIZE(fb_device_attrs); i++)
- device_remove_file(info->dev, &fb_device_attrs[i]);
- device_remove_bin_file(info->dev, &edid_attr);
- unlink_framebuffer(info);
- }
- usb_set_intfdata(interface, NULL);
- dev->udev = NULL;
- dev->gdev = NULL;
- /* if clients still have us open, will be freed on last close */
- if (dev->fb_count == 0)
- schedule_delayed_work(&dev->free_framebuffer_work, 0);
- /* release reference taken by kref_init in probe() */
- kref_put(&dev->kref, dlfb_free);
- /* consider dlfb_data freed */
- return;
- }
- static struct usb_driver dlfb_driver = {
- .name = "udlfb",
- .probe = dlfb_usb_probe,
- .disconnect = dlfb_usb_disconnect,
- .id_table = id_table,
- };
- module_usb_driver(dlfb_driver);
- static void dlfb_urb_completion(struct urb *urb)
- {
- struct urb_node *unode = urb->context;
- struct dlfb_data *dev = unode->dev;
- unsigned long flags;
- /* sync/async unlink faults aren't errors */
- if (urb->status) {
- if (!(urb->status == -ENOENT ||
- urb->status == -ECONNRESET ||
- urb->status == -ESHUTDOWN)) {
- pr_err("%s - nonzero write bulk status received: %d\n",
- __func__, urb->status);
- atomic_set(&dev->lost_pixels, 1);
- }
- }
- urb->transfer_buffer_length = dev->urbs.size; /* reset to actual */
- spin_lock_irqsave(&dev->urbs.lock, flags);
- list_add_tail(&unode->entry, &dev->urbs.list);
- dev->urbs.available++;
- spin_unlock_irqrestore(&dev->urbs.lock, flags);
- /*
- * When using fb_defio, we deadlock if up() is called
- * while another is waiting. So queue to another process.
- */
- if (fb_defio)
- schedule_delayed_work(&unode->release_urb_work, 0);
- else
- up(&dev->urbs.limit_sem);
- }
- static void dlfb_free_urb_list(struct dlfb_data *dev)
- {
- int count = dev->urbs.count;
- struct list_head *node;
- struct urb_node *unode;
- struct urb *urb;
- int ret;
- unsigned long flags;
- pr_notice("Freeing all render urbs\n");
- /* keep waiting and freeing, until we've got 'em all */
- while (count--) {
- /* Getting interrupted means a leak, but ok at disconnect */
- ret = down_interruptible(&dev->urbs.limit_sem);
- if (ret)
- break;
- spin_lock_irqsave(&dev->urbs.lock, flags);
- node = dev->urbs.list.next; /* have reserved one with sem */
- list_del_init(node);
- spin_unlock_irqrestore(&dev->urbs.lock, flags);
- unode = list_entry(node, struct urb_node, entry);
- urb = unode->urb;
- /* Free each separately allocated piece */
- usb_free_coherent(urb->dev, dev->urbs.size,
- urb->transfer_buffer, urb->transfer_dma);
- usb_free_urb(urb);
- kfree(node);
- }
- dev->urbs.count = 0;
- }
- static int dlfb_alloc_urb_list(struct dlfb_data *dev, int count, size_t size)
- {
- int i = 0;
- struct urb *urb;
- struct urb_node *unode;
- char *buf;
- spin_lock_init(&dev->urbs.lock);
- dev->urbs.size = size;
- INIT_LIST_HEAD(&dev->urbs.list);
- while (i < count) {
- unode = kzalloc(sizeof(struct urb_node), GFP_KERNEL);
- if (!unode)
- break;
- unode->dev = dev;
- INIT_DELAYED_WORK(&unode->release_urb_work,
- dlfb_release_urb_work);
- urb = usb_alloc_urb(0, GFP_KERNEL);
- if (!urb) {
- kfree(unode);
- break;
- }
- unode->urb = urb;
- buf = usb_alloc_coherent(dev->udev, MAX_TRANSFER, GFP_KERNEL,
- &urb->transfer_dma);
- if (!buf) {
- kfree(unode);
- usb_free_urb(urb);
- break;
- }
- /* urb->transfer_buffer_length set to actual before submit */
- usb_fill_bulk_urb(urb, dev->udev, usb_sndbulkpipe(dev->udev, 1),
- buf, size, dlfb_urb_completion, unode);
- urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
- list_add_tail(&unode->entry, &dev->urbs.list);
- i++;
- }
- sema_init(&dev->urbs.limit_sem, i);
- dev->urbs.count = i;
- dev->urbs.available = i;
- pr_notice("allocated %d %d byte urbs\n", i, (int) size);
- return i;
- }
- static struct urb *dlfb_get_urb(struct dlfb_data *dev)
- {
- int ret = 0;
- struct list_head *entry;
- struct urb_node *unode;
- struct urb *urb = NULL;
- unsigned long flags;
- /* Wait for an in-flight buffer to complete and get re-queued */
- ret = down_timeout(&dev->urbs.limit_sem, GET_URB_TIMEOUT);
- if (ret) {
- atomic_set(&dev->lost_pixels, 1);
- pr_warn("wait for urb interrupted: %x available: %d\n",
- ret, dev->urbs.available);
- goto error;
- }
- spin_lock_irqsave(&dev->urbs.lock, flags);
- BUG_ON(list_empty(&dev->urbs.list)); /* reserved one with limit_sem */
- entry = dev->urbs.list.next;
- list_del_init(entry);
- dev->urbs.available--;
- spin_unlock_irqrestore(&dev->urbs.lock, flags);
- unode = list_entry(entry, struct urb_node, entry);
- urb = unode->urb;
- error:
- return urb;
- }
- static int dlfb_submit_urb(struct dlfb_data *dev, struct urb *urb, size_t len)
- {
- int ret;
- BUG_ON(len > dev->urbs.size);
- urb->transfer_buffer_length = len; /* set to actual payload len */
- ret = usb_submit_urb(urb, GFP_KERNEL);
- if (ret) {
- dlfb_urb_completion(urb); /* because no one else will */
- atomic_set(&dev->lost_pixels, 1);
- pr_err("usb_submit_urb error %x\n", ret);
- }
- return ret;
- }
- module_param(console, bool, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
- MODULE_PARM_DESC(console, "Allow fbcon to open framebuffer");
- module_param(fb_defio, bool, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
- MODULE_PARM_DESC(fb_defio, "Page fault detection of mmap writes");
- module_param(shadow, bool, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
- MODULE_PARM_DESC(shadow, "Shadow vid mem. Disable to save mem but lose perf");
- module_param(pixel_limit, int, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
- MODULE_PARM_DESC(pixel_limit, "Force limit on max mode (in x*y pixels)");
- MODULE_AUTHOR("Roberto De Ioris <roberto@unbit.it>, "
- "Jaya Kumar <jayakumar.lkml@gmail.com>, "
- "Bernie Thompson <bernie@plugable.com>");
- MODULE_DESCRIPTION("DisplayLink kernel framebuffer driver");
- MODULE_LICENSE("GPL");
|