udlfb.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980
  1. /*
  2. * udlfb.c -- Framebuffer driver for DisplayLink USB controller
  3. *
  4. * Copyright (C) 2009 Roberto De Ioris <roberto@unbit.it>
  5. * Copyright (C) 2009 Jaya Kumar <jayakumar.lkml@gmail.com>
  6. * Copyright (C) 2009 Bernie Thompson <bernie@plugable.com>
  7. *
  8. * This file is subject to the terms and conditions of the GNU General Public
  9. * License v2. See the file COPYING in the main directory of this archive for
  10. * more details.
  11. *
  12. * Layout is based on skeletonfb by James Simmons and Geert Uytterhoeven,
  13. * usb-skeleton by GregKH.
  14. *
  15. * Device-specific portions based on information from Displaylink, with work
  16. * from Florian Echtler, Henrik Bjerregaard Pedersen, and others.
  17. */
  18. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  19. #include <linux/module.h>
  20. #include <linux/kernel.h>
  21. #include <linux/init.h>
  22. #include <linux/usb.h>
  23. #include <linux/uaccess.h>
  24. #include <linux/mm.h>
  25. #include <linux/fb.h>
  26. #include <linux/vmalloc.h>
  27. #include <linux/slab.h>
  28. #include <linux/prefetch.h>
  29. #include <linux/delay.h>
  30. #include <video/udlfb.h>
  31. #include "edid.h"
  32. static struct fb_fix_screeninfo dlfb_fix = {
  33. .id = "udlfb",
  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,
  40. };
  41. static const u32 udlfb_info_flags = FBINFO_DEFAULT | FBINFO_READS_FAST |
  42. FBINFO_VIRTFB |
  43. FBINFO_HWACCEL_IMAGEBLIT | FBINFO_HWACCEL_FILLRECT |
  44. FBINFO_HWACCEL_COPYAREA | FBINFO_MISC_ALWAYS_SETPAR;
  45. /*
  46. * There are many DisplayLink-based graphics products, all with unique PIDs.
  47. * So we match on DisplayLink's VID + Vendor-Defined Interface Class (0xff)
  48. * We also require a match on SubClass (0x00) and Protocol (0x00),
  49. * which is compatible with all known USB 2.0 era graphics chips and firmware,
  50. * but allows DisplayLink to increment those for any future incompatible chips
  51. */
  52. static struct usb_device_id id_table[] = {
  53. {.idVendor = 0x17e9,
  54. .bInterfaceClass = 0xff,
  55. .bInterfaceSubClass = 0x00,
  56. .bInterfaceProtocol = 0x00,
  57. .match_flags = USB_DEVICE_ID_MATCH_VENDOR |
  58. USB_DEVICE_ID_MATCH_INT_CLASS |
  59. USB_DEVICE_ID_MATCH_INT_SUBCLASS |
  60. USB_DEVICE_ID_MATCH_INT_PROTOCOL,
  61. },
  62. {},
  63. };
  64. MODULE_DEVICE_TABLE(usb, id_table);
  65. /* module options */
  66. static bool console = 1; /* Allow fbcon to open framebuffer */
  67. static bool fb_defio = 1; /* Detect mmap writes using page faults */
  68. static bool shadow = 1; /* Optionally disable shadow framebuffer */
  69. static int pixel_limit; /* Optionally force a pixel resolution limit */
  70. /* dlfb keeps a list of urbs for efficient bulk transfers */
  71. static void dlfb_urb_completion(struct urb *urb);
  72. static struct urb *dlfb_get_urb(struct dlfb_data *dev);
  73. static int dlfb_submit_urb(struct dlfb_data *dev, struct urb * urb, size_t len);
  74. static int dlfb_alloc_urb_list(struct dlfb_data *dev, int count, size_t size);
  75. static void dlfb_free_urb_list(struct dlfb_data *dev);
  76. /*
  77. * All DisplayLink bulk operations start with 0xAF, followed by specific code
  78. * All operations are written to buffers which then later get sent to device
  79. */
  80. static char *dlfb_set_register(char *buf, u8 reg, u8 val)
  81. {
  82. *buf++ = 0xAF;
  83. *buf++ = 0x20;
  84. *buf++ = reg;
  85. *buf++ = val;
  86. return buf;
  87. }
  88. static char *dlfb_vidreg_lock(char *buf)
  89. {
  90. return dlfb_set_register(buf, 0xFF, 0x00);
  91. }
  92. static char *dlfb_vidreg_unlock(char *buf)
  93. {
  94. return dlfb_set_register(buf, 0xFF, 0xFF);
  95. }
  96. /*
  97. * Map FB_BLANK_* to DisplayLink register
  98. * DLReg FB_BLANK_*
  99. * ----- -----------------------------
  100. * 0x00 FB_BLANK_UNBLANK (0)
  101. * 0x01 FB_BLANK (1)
  102. * 0x03 FB_BLANK_VSYNC_SUSPEND (2)
  103. * 0x05 FB_BLANK_HSYNC_SUSPEND (3)
  104. * 0x07 FB_BLANK_POWERDOWN (4) Note: requires modeset to come back
  105. */
  106. static char *dlfb_blanking(char *buf, int fb_blank)
  107. {
  108. u8 reg;
  109. switch (fb_blank) {
  110. case FB_BLANK_POWERDOWN:
  111. reg = 0x07;
  112. break;
  113. case FB_BLANK_HSYNC_SUSPEND:
  114. reg = 0x05;
  115. break;
  116. case FB_BLANK_VSYNC_SUSPEND:
  117. reg = 0x03;
  118. break;
  119. case FB_BLANK_NORMAL:
  120. reg = 0x01;
  121. break;
  122. default:
  123. reg = 0x00;
  124. }
  125. buf = dlfb_set_register(buf, 0x1F, reg);
  126. return buf;
  127. }
  128. static char *dlfb_set_color_depth(char *buf, u8 selection)
  129. {
  130. return dlfb_set_register(buf, 0x00, selection);
  131. }
  132. static char *dlfb_set_base16bpp(char *wrptr, u32 base)
  133. {
  134. /* the base pointer is 16 bits wide, 0x20 is hi byte. */
  135. wrptr = dlfb_set_register(wrptr, 0x20, base >> 16);
  136. wrptr = dlfb_set_register(wrptr, 0x21, base >> 8);
  137. return dlfb_set_register(wrptr, 0x22, base);
  138. }
  139. /*
  140. * DisplayLink HW has separate 16bpp and 8bpp framebuffers.
  141. * In 24bpp modes, the low 323 RGB bits go in the 8bpp framebuffer
  142. */
  143. static char *dlfb_set_base8bpp(char *wrptr, u32 base)
  144. {
  145. wrptr = dlfb_set_register(wrptr, 0x26, base >> 16);
  146. wrptr = dlfb_set_register(wrptr, 0x27, base >> 8);
  147. return dlfb_set_register(wrptr, 0x28, base);
  148. }
  149. static char *dlfb_set_register_16(char *wrptr, u8 reg, u16 value)
  150. {
  151. wrptr = dlfb_set_register(wrptr, reg, value >> 8);
  152. return dlfb_set_register(wrptr, reg+1, value);
  153. }
  154. /*
  155. * This is kind of weird because the controller takes some
  156. * register values in a different byte order than other registers.
  157. */
  158. static char *dlfb_set_register_16be(char *wrptr, u8 reg, u16 value)
  159. {
  160. wrptr = dlfb_set_register(wrptr, reg, value);
  161. return dlfb_set_register(wrptr, reg+1, value >> 8);
  162. }
  163. /*
  164. * LFSR is linear feedback shift register. The reason we have this is
  165. * because the display controller needs to minimize the clock depth of
  166. * various counters used in the display path. So this code reverses the
  167. * provided value into the lfsr16 value by counting backwards to get
  168. * the value that needs to be set in the hardware comparator to get the
  169. * same actual count. This makes sense once you read above a couple of
  170. * times and think about it from a hardware perspective.
  171. */
  172. static u16 dlfb_lfsr16(u16 actual_count)
  173. {
  174. u32 lv = 0xFFFF; /* This is the lfsr value that the hw starts with */
  175. while (actual_count--) {
  176. lv = ((lv << 1) |
  177. (((lv >> 15) ^ (lv >> 4) ^ (lv >> 2) ^ (lv >> 1)) & 1))
  178. & 0xFFFF;
  179. }
  180. return (u16) lv;
  181. }
  182. /*
  183. * This does LFSR conversion on the value that is to be written.
  184. * See LFSR explanation above for more detail.
  185. */
  186. static char *dlfb_set_register_lfsr16(char *wrptr, u8 reg, u16 value)
  187. {
  188. return dlfb_set_register_16(wrptr, reg, dlfb_lfsr16(value));
  189. }
  190. /*
  191. * This takes a standard fbdev screeninfo struct and all of its monitor mode
  192. * details and converts them into the DisplayLink equivalent register commands.
  193. */
  194. static char *dlfb_set_vid_cmds(char *wrptr, struct fb_var_screeninfo *var)
  195. {
  196. u16 xds, yds;
  197. u16 xde, yde;
  198. u16 yec;
  199. /* x display start */
  200. xds = var->left_margin + var->hsync_len;
  201. wrptr = dlfb_set_register_lfsr16(wrptr, 0x01, xds);
  202. /* x display end */
  203. xde = xds + var->xres;
  204. wrptr = dlfb_set_register_lfsr16(wrptr, 0x03, xde);
  205. /* y display start */
  206. yds = var->upper_margin + var->vsync_len;
  207. wrptr = dlfb_set_register_lfsr16(wrptr, 0x05, yds);
  208. /* y display end */
  209. yde = yds + var->yres;
  210. wrptr = dlfb_set_register_lfsr16(wrptr, 0x07, yde);
  211. /* x end count is active + blanking - 1 */
  212. wrptr = dlfb_set_register_lfsr16(wrptr, 0x09,
  213. xde + var->right_margin - 1);
  214. /* libdlo hardcodes hsync start to 1 */
  215. wrptr = dlfb_set_register_lfsr16(wrptr, 0x0B, 1);
  216. /* hsync end is width of sync pulse + 1 */
  217. wrptr = dlfb_set_register_lfsr16(wrptr, 0x0D, var->hsync_len + 1);
  218. /* hpixels is active pixels */
  219. wrptr = dlfb_set_register_16(wrptr, 0x0F, var->xres);
  220. /* yendcount is vertical active + vertical blanking */
  221. yec = var->yres + var->upper_margin + var->lower_margin +
  222. var->vsync_len;
  223. wrptr = dlfb_set_register_lfsr16(wrptr, 0x11, yec);
  224. /* libdlo hardcodes vsync start to 0 */
  225. wrptr = dlfb_set_register_lfsr16(wrptr, 0x13, 0);
  226. /* vsync end is width of vsync pulse */
  227. wrptr = dlfb_set_register_lfsr16(wrptr, 0x15, var->vsync_len);
  228. /* vpixels is active pixels */
  229. wrptr = dlfb_set_register_16(wrptr, 0x17, var->yres);
  230. /* convert picoseconds to 5kHz multiple for pclk5k = x * 1E12/5k */
  231. wrptr = dlfb_set_register_16be(wrptr, 0x1B,
  232. 200*1000*1000/var->pixclock);
  233. return wrptr;
  234. }
  235. /*
  236. * This takes a standard fbdev screeninfo struct that was fetched or prepared
  237. * and then generates the appropriate command sequence that then drives the
  238. * display controller.
  239. */
  240. static int dlfb_set_video_mode(struct dlfb_data *dev,
  241. struct fb_var_screeninfo *var)
  242. {
  243. char *buf;
  244. char *wrptr;
  245. int retval = 0;
  246. int writesize;
  247. struct urb *urb;
  248. if (!atomic_read(&dev->usb_active))
  249. return -EPERM;
  250. urb = dlfb_get_urb(dev);
  251. if (!urb)
  252. return -ENOMEM;
  253. buf = (char *) urb->transfer_buffer;
  254. /*
  255. * This first section has to do with setting the base address on the
  256. * controller * associated with the display. There are 2 base
  257. * pointers, currently, we only * use the 16 bpp segment.
  258. */
  259. wrptr = dlfb_vidreg_lock(buf);
  260. wrptr = dlfb_set_color_depth(wrptr, 0x00);
  261. /* set base for 16bpp segment to 0 */
  262. wrptr = dlfb_set_base16bpp(wrptr, 0);
  263. /* set base for 8bpp segment to end of fb */
  264. wrptr = dlfb_set_base8bpp(wrptr, dev->info->fix.smem_len);
  265. wrptr = dlfb_set_vid_cmds(wrptr, var);
  266. wrptr = dlfb_blanking(wrptr, FB_BLANK_UNBLANK);
  267. wrptr = dlfb_vidreg_unlock(wrptr);
  268. writesize = wrptr - buf;
  269. retval = dlfb_submit_urb(dev, urb, writesize);
  270. dev->blank_mode = FB_BLANK_UNBLANK;
  271. return retval;
  272. }
  273. static int dlfb_ops_mmap(struct fb_info *info, struct vm_area_struct *vma)
  274. {
  275. unsigned long start = vma->vm_start;
  276. unsigned long size = vma->vm_end - vma->vm_start;
  277. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  278. unsigned long page, pos;
  279. if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT))
  280. return -EINVAL;
  281. if (size > info->fix.smem_len)
  282. return -EINVAL;
  283. if (offset > info->fix.smem_len - size)
  284. return -EINVAL;
  285. pos = (unsigned long)info->fix.smem_start + offset;
  286. pr_notice("mmap() framebuffer addr:%lu size:%lu\n",
  287. pos, size);
  288. while (size > 0) {
  289. page = vmalloc_to_pfn((void *)pos);
  290. if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED))
  291. return -EAGAIN;
  292. start += PAGE_SIZE;
  293. pos += PAGE_SIZE;
  294. if (size > PAGE_SIZE)
  295. size -= PAGE_SIZE;
  296. else
  297. size = 0;
  298. }
  299. return 0;
  300. }
  301. /*
  302. * Trims identical data from front and back of line
  303. * Sets new front buffer address and width
  304. * And returns byte count of identical pixels
  305. * Assumes CPU natural alignment (unsigned long)
  306. * for back and front buffer ptrs and width
  307. */
  308. static int dlfb_trim_hline(const u8 *bback, const u8 **bfront, int *width_bytes)
  309. {
  310. int j, k;
  311. const unsigned long *back = (const unsigned long *) bback;
  312. const unsigned long *front = (const unsigned long *) *bfront;
  313. const int width = *width_bytes / sizeof(unsigned long);
  314. int identical = width;
  315. int start = width;
  316. int end = width;
  317. prefetch((void *) front);
  318. prefetch((void *) back);
  319. for (j = 0; j < width; j++) {
  320. if (back[j] != front[j]) {
  321. start = j;
  322. break;
  323. }
  324. }
  325. for (k = width - 1; k > j; k--) {
  326. if (back[k] != front[k]) {
  327. end = k+1;
  328. break;
  329. }
  330. }
  331. identical = start + (width - end);
  332. *bfront = (u8 *) &front[start];
  333. *width_bytes = (end - start) * sizeof(unsigned long);
  334. return identical * sizeof(unsigned long);
  335. }
  336. /*
  337. * Render a command stream for an encoded horizontal line segment of pixels.
  338. *
  339. * A command buffer holds several commands.
  340. * It always begins with a fresh command header
  341. * (the protocol doesn't require this, but we enforce it to allow
  342. * multiple buffers to be potentially encoded and sent in parallel).
  343. * A single command encodes one contiguous horizontal line of pixels
  344. *
  345. * The function relies on the client to do all allocation, so that
  346. * rendering can be done directly to output buffers (e.g. USB URBs).
  347. * The function fills the supplied command buffer, providing information
  348. * on where it left off, so the client may call in again with additional
  349. * buffers if the line will take several buffers to complete.
  350. *
  351. * A single command can transmit a maximum of 256 pixels,
  352. * regardless of the compression ratio (protocol design limit).
  353. * To the hardware, 0 for a size byte means 256
  354. *
  355. * Rather than 256 pixel commands which are either rl or raw encoded,
  356. * the rlx command simply assumes alternating raw and rl spans within one cmd.
  357. * This has a slightly larger header overhead, but produces more even results.
  358. * It also processes all data (read and write) in a single pass.
  359. * Performance benchmarks of common cases show it having just slightly better
  360. * compression than 256 pixel raw or rle commands, with similar CPU consumpion.
  361. * But for very rl friendly data, will compress not quite as well.
  362. */
  363. static void dlfb_compress_hline(
  364. const uint16_t **pixel_start_ptr,
  365. const uint16_t *const pixel_end,
  366. uint32_t *device_address_ptr,
  367. uint8_t **command_buffer_ptr,
  368. const uint8_t *const cmd_buffer_end)
  369. {
  370. const uint16_t *pixel = *pixel_start_ptr;
  371. uint32_t dev_addr = *device_address_ptr;
  372. uint8_t *cmd = *command_buffer_ptr;
  373. const int bpp = 2;
  374. while ((pixel_end > pixel) &&
  375. (cmd_buffer_end - MIN_RLX_CMD_BYTES > cmd)) {
  376. uint8_t *raw_pixels_count_byte = NULL;
  377. uint8_t *cmd_pixels_count_byte = NULL;
  378. const uint16_t *raw_pixel_start = NULL;
  379. const uint16_t *cmd_pixel_start, *cmd_pixel_end = NULL;
  380. prefetchw((void *) cmd); /* pull in one cache line at least */
  381. *cmd++ = 0xAF;
  382. *cmd++ = 0x6B;
  383. *cmd++ = (uint8_t) ((dev_addr >> 16) & 0xFF);
  384. *cmd++ = (uint8_t) ((dev_addr >> 8) & 0xFF);
  385. *cmd++ = (uint8_t) ((dev_addr) & 0xFF);
  386. cmd_pixels_count_byte = cmd++; /* we'll know this later */
  387. cmd_pixel_start = pixel;
  388. raw_pixels_count_byte = cmd++; /* we'll know this later */
  389. raw_pixel_start = pixel;
  390. cmd_pixel_end = pixel + min(MAX_CMD_PIXELS + 1,
  391. min((int)(pixel_end - pixel),
  392. (int)(cmd_buffer_end - cmd) / bpp));
  393. prefetch_range((void *) pixel, (cmd_pixel_end - pixel) * bpp);
  394. while (pixel < cmd_pixel_end) {
  395. const uint16_t * const repeating_pixel = pixel;
  396. *(uint16_t *)cmd = cpu_to_be16p(pixel);
  397. cmd += 2;
  398. pixel++;
  399. if (unlikely((pixel < cmd_pixel_end) &&
  400. (*pixel == *repeating_pixel))) {
  401. /* go back and fill in raw pixel count */
  402. *raw_pixels_count_byte = ((repeating_pixel -
  403. raw_pixel_start) + 1) & 0xFF;
  404. while ((pixel < cmd_pixel_end)
  405. && (*pixel == *repeating_pixel)) {
  406. pixel++;
  407. }
  408. /* immediately after raw data is repeat byte */
  409. *cmd++ = ((pixel - repeating_pixel) - 1) & 0xFF;
  410. /* Then start another raw pixel span */
  411. raw_pixel_start = pixel;
  412. raw_pixels_count_byte = cmd++;
  413. }
  414. }
  415. if (pixel > raw_pixel_start) {
  416. /* finalize last RAW span */
  417. *raw_pixels_count_byte = (pixel-raw_pixel_start) & 0xFF;
  418. }
  419. *cmd_pixels_count_byte = (pixel - cmd_pixel_start) & 0xFF;
  420. dev_addr += (pixel - cmd_pixel_start) * bpp;
  421. }
  422. if (cmd_buffer_end <= MIN_RLX_CMD_BYTES + cmd) {
  423. /* Fill leftover bytes with no-ops */
  424. if (cmd_buffer_end > cmd)
  425. memset(cmd, 0xAF, cmd_buffer_end - cmd);
  426. cmd = (uint8_t *) cmd_buffer_end;
  427. }
  428. *command_buffer_ptr = cmd;
  429. *pixel_start_ptr = pixel;
  430. *device_address_ptr = dev_addr;
  431. return;
  432. }
  433. /*
  434. * There are 3 copies of every pixel: The front buffer that the fbdev
  435. * client renders to, the actual framebuffer across the USB bus in hardware
  436. * (that we can only write to, slowly, and can never read), and (optionally)
  437. * our shadow copy that tracks what's been sent to that hardware buffer.
  438. */
  439. static int dlfb_render_hline(struct dlfb_data *dev, struct urb **urb_ptr,
  440. const char *front, char **urb_buf_ptr,
  441. u32 byte_offset, u32 byte_width,
  442. int *ident_ptr, int *sent_ptr)
  443. {
  444. const u8 *line_start, *line_end, *next_pixel;
  445. u32 dev_addr = dev->base16 + byte_offset;
  446. struct urb *urb = *urb_ptr;
  447. u8 *cmd = *urb_buf_ptr;
  448. u8 *cmd_end = (u8 *) urb->transfer_buffer + urb->transfer_buffer_length;
  449. line_start = (u8 *) (front + byte_offset);
  450. next_pixel = line_start;
  451. line_end = next_pixel + byte_width;
  452. if (dev->backing_buffer) {
  453. int offset;
  454. const u8 *back_start = (u8 *) (dev->backing_buffer
  455. + byte_offset);
  456. *ident_ptr += dlfb_trim_hline(back_start, &next_pixel,
  457. &byte_width);
  458. offset = next_pixel - line_start;
  459. line_end = next_pixel + byte_width;
  460. dev_addr += offset;
  461. back_start += offset;
  462. line_start += offset;
  463. memcpy((char *)back_start, (char *) line_start,
  464. byte_width);
  465. }
  466. while (next_pixel < line_end) {
  467. dlfb_compress_hline((const uint16_t **) &next_pixel,
  468. (const uint16_t *) line_end, &dev_addr,
  469. (u8 **) &cmd, (u8 *) cmd_end);
  470. if (cmd >= cmd_end) {
  471. int len = cmd - (u8 *) urb->transfer_buffer;
  472. if (dlfb_submit_urb(dev, urb, len))
  473. return 1; /* lost pixels is set */
  474. *sent_ptr += len;
  475. urb = dlfb_get_urb(dev);
  476. if (!urb)
  477. return 1; /* lost_pixels is set */
  478. *urb_ptr = urb;
  479. cmd = urb->transfer_buffer;
  480. cmd_end = &cmd[urb->transfer_buffer_length];
  481. }
  482. }
  483. *urb_buf_ptr = cmd;
  484. return 0;
  485. }
  486. static int dlfb_handle_damage(struct dlfb_data *dev, int x, int y,
  487. int width, int height, char *data)
  488. {
  489. int i, ret;
  490. char *cmd;
  491. cycles_t start_cycles, end_cycles;
  492. int bytes_sent = 0;
  493. int bytes_identical = 0;
  494. struct urb *urb;
  495. int aligned_x;
  496. start_cycles = get_cycles();
  497. aligned_x = DL_ALIGN_DOWN(x, sizeof(unsigned long));
  498. width = DL_ALIGN_UP(width + (x-aligned_x), sizeof(unsigned long));
  499. x = aligned_x;
  500. if ((width <= 0) ||
  501. (x + width > dev->info->var.xres) ||
  502. (y + height > dev->info->var.yres))
  503. return -EINVAL;
  504. if (!atomic_read(&dev->usb_active))
  505. return 0;
  506. urb = dlfb_get_urb(dev);
  507. if (!urb)
  508. return 0;
  509. cmd = urb->transfer_buffer;
  510. for (i = y; i < y + height ; i++) {
  511. const int line_offset = dev->info->fix.line_length * i;
  512. const int byte_offset = line_offset + (x * BPP);
  513. if (dlfb_render_hline(dev, &urb,
  514. (char *) dev->info->fix.smem_start,
  515. &cmd, byte_offset, width * BPP,
  516. &bytes_identical, &bytes_sent))
  517. goto error;
  518. }
  519. if (cmd > (char *) urb->transfer_buffer) {
  520. /* Send partial buffer remaining before exiting */
  521. int len = cmd - (char *) urb->transfer_buffer;
  522. ret = dlfb_submit_urb(dev, urb, len);
  523. bytes_sent += len;
  524. } else
  525. dlfb_urb_completion(urb);
  526. error:
  527. atomic_add(bytes_sent, &dev->bytes_sent);
  528. atomic_add(bytes_identical, &dev->bytes_identical);
  529. atomic_add(width*height*2, &dev->bytes_rendered);
  530. end_cycles = get_cycles();
  531. atomic_add(((unsigned int) ((end_cycles - start_cycles)
  532. >> 10)), /* Kcycles */
  533. &dev->cpu_kcycles_used);
  534. return 0;
  535. }
  536. /*
  537. * Path triggered by usermode clients who write to filesystem
  538. * e.g. cat filename > /dev/fb1
  539. * Not used by X Windows or text-mode console. But useful for testing.
  540. * Slow because of extra copy and we must assume all pixels dirty.
  541. */
  542. static ssize_t dlfb_ops_write(struct fb_info *info, const char __user *buf,
  543. size_t count, loff_t *ppos)
  544. {
  545. ssize_t result;
  546. struct dlfb_data *dev = info->par;
  547. u32 offset = (u32) *ppos;
  548. result = fb_sys_write(info, buf, count, ppos);
  549. if (result > 0) {
  550. int start = max((int)(offset / info->fix.line_length), 0);
  551. int lines = min((u32)((result / info->fix.line_length) + 1),
  552. (u32)info->var.yres);
  553. dlfb_handle_damage(dev, 0, start, info->var.xres,
  554. lines, info->screen_base);
  555. }
  556. return result;
  557. }
  558. /* hardware has native COPY command (see libdlo), but not worth it for fbcon */
  559. static void dlfb_ops_copyarea(struct fb_info *info,
  560. const struct fb_copyarea *area)
  561. {
  562. struct dlfb_data *dev = info->par;
  563. sys_copyarea(info, area);
  564. dlfb_handle_damage(dev, area->dx, area->dy,
  565. area->width, area->height, info->screen_base);
  566. }
  567. static void dlfb_ops_imageblit(struct fb_info *info,
  568. const struct fb_image *image)
  569. {
  570. struct dlfb_data *dev = info->par;
  571. sys_imageblit(info, image);
  572. dlfb_handle_damage(dev, image->dx, image->dy,
  573. image->width, image->height, info->screen_base);
  574. }
  575. static void dlfb_ops_fillrect(struct fb_info *info,
  576. const struct fb_fillrect *rect)
  577. {
  578. struct dlfb_data *dev = info->par;
  579. sys_fillrect(info, rect);
  580. dlfb_handle_damage(dev, rect->dx, rect->dy, rect->width,
  581. rect->height, info->screen_base);
  582. }
  583. /*
  584. * NOTE: fb_defio.c is holding info->fbdefio.mutex
  585. * Touching ANY framebuffer memory that triggers a page fault
  586. * in fb_defio will cause a deadlock, when it also tries to
  587. * grab the same mutex.
  588. */
  589. static void dlfb_dpy_deferred_io(struct fb_info *info,
  590. struct list_head *pagelist)
  591. {
  592. struct page *cur;
  593. struct fb_deferred_io *fbdefio = info->fbdefio;
  594. struct dlfb_data *dev = info->par;
  595. struct urb *urb;
  596. char *cmd;
  597. cycles_t start_cycles, end_cycles;
  598. int bytes_sent = 0;
  599. int bytes_identical = 0;
  600. int bytes_rendered = 0;
  601. if (!fb_defio)
  602. return;
  603. if (!atomic_read(&dev->usb_active))
  604. return;
  605. start_cycles = get_cycles();
  606. urb = dlfb_get_urb(dev);
  607. if (!urb)
  608. return;
  609. cmd = urb->transfer_buffer;
  610. /* walk the written page list and render each to device */
  611. list_for_each_entry(cur, &fbdefio->pagelist, lru) {
  612. if (dlfb_render_hline(dev, &urb, (char *) info->fix.smem_start,
  613. &cmd, cur->index << PAGE_SHIFT,
  614. PAGE_SIZE, &bytes_identical, &bytes_sent))
  615. goto error;
  616. bytes_rendered += PAGE_SIZE;
  617. }
  618. if (cmd > (char *) urb->transfer_buffer) {
  619. /* Send partial buffer remaining before exiting */
  620. int len = cmd - (char *) urb->transfer_buffer;
  621. dlfb_submit_urb(dev, urb, len);
  622. bytes_sent += len;
  623. } else
  624. dlfb_urb_completion(urb);
  625. error:
  626. atomic_add(bytes_sent, &dev->bytes_sent);
  627. atomic_add(bytes_identical, &dev->bytes_identical);
  628. atomic_add(bytes_rendered, &dev->bytes_rendered);
  629. end_cycles = get_cycles();
  630. atomic_add(((unsigned int) ((end_cycles - start_cycles)
  631. >> 10)), /* Kcycles */
  632. &dev->cpu_kcycles_used);
  633. }
  634. static int dlfb_get_edid(struct dlfb_data *dev, char *edid, int len)
  635. {
  636. int i;
  637. int ret;
  638. char *rbuf;
  639. rbuf = kmalloc(2, GFP_KERNEL);
  640. if (!rbuf)
  641. return 0;
  642. for (i = 0; i < len; i++) {
  643. ret = usb_control_msg(dev->udev,
  644. usb_rcvctrlpipe(dev->udev, 0), (0x02),
  645. (0x80 | (0x02 << 5)), i << 8, 0xA1, rbuf, 2,
  646. HZ);
  647. if (ret < 1) {
  648. pr_err("Read EDID byte %d failed err %x\n", i, ret);
  649. i--;
  650. break;
  651. }
  652. edid[i] = rbuf[1];
  653. }
  654. kfree(rbuf);
  655. return i;
  656. }
  657. static int dlfb_ops_ioctl(struct fb_info *info, unsigned int cmd,
  658. unsigned long arg)
  659. {
  660. struct dlfb_data *dev = info->par;
  661. if (!atomic_read(&dev->usb_active))
  662. return 0;
  663. /* TODO: Update X server to get this from sysfs instead */
  664. if (cmd == DLFB_IOCTL_RETURN_EDID) {
  665. void __user *edid = (void __user *)arg;
  666. if (copy_to_user(edid, dev->edid, dev->edid_size))
  667. return -EFAULT;
  668. return 0;
  669. }
  670. /* TODO: Help propose a standard fb.h ioctl to report mmap damage */
  671. if (cmd == DLFB_IOCTL_REPORT_DAMAGE) {
  672. struct dloarea area;
  673. if (copy_from_user(&area, (void __user *)arg,
  674. sizeof(struct dloarea)))
  675. return -EFAULT;
  676. /*
  677. * If we have a damage-aware client, turn fb_defio "off"
  678. * To avoid perf imact of unnecessary page fault handling.
  679. * Done by resetting the delay for this fb_info to a very
  680. * long period. Pages will become writable and stay that way.
  681. * Reset to normal value when all clients have closed this fb.
  682. */
  683. if (info->fbdefio)
  684. info->fbdefio->delay = DL_DEFIO_WRITE_DISABLE;
  685. if (area.x < 0)
  686. area.x = 0;
  687. if (area.x > info->var.xres)
  688. area.x = info->var.xres;
  689. if (area.y < 0)
  690. area.y = 0;
  691. if (area.y > info->var.yres)
  692. area.y = info->var.yres;
  693. dlfb_handle_damage(dev, area.x, area.y, area.w, area.h,
  694. info->screen_base);
  695. }
  696. return 0;
  697. }
  698. /* taken from vesafb */
  699. static int
  700. dlfb_ops_setcolreg(unsigned regno, unsigned red, unsigned green,
  701. unsigned blue, unsigned transp, struct fb_info *info)
  702. {
  703. int err = 0;
  704. if (regno >= info->cmap.len)
  705. return 1;
  706. if (regno < 16) {
  707. if (info->var.red.offset == 10) {
  708. /* 1:5:5:5 */
  709. ((u32 *) (info->pseudo_palette))[regno] =
  710. ((red & 0xf800) >> 1) |
  711. ((green & 0xf800) >> 6) | ((blue & 0xf800) >> 11);
  712. } else {
  713. /* 0:5:6:5 */
  714. ((u32 *) (info->pseudo_palette))[regno] =
  715. ((red & 0xf800)) |
  716. ((green & 0xfc00) >> 5) | ((blue & 0xf800) >> 11);
  717. }
  718. }
  719. return err;
  720. }
  721. /*
  722. * It's common for several clients to have framebuffer open simultaneously.
  723. * e.g. both fbcon and X. Makes things interesting.
  724. * Assumes caller is holding info->lock (for open and release at least)
  725. */
  726. static int dlfb_ops_open(struct fb_info *info, int user)
  727. {
  728. struct dlfb_data *dev = info->par;
  729. /*
  730. * fbcon aggressively connects to first framebuffer it finds,
  731. * preventing other clients (X) from working properly. Usually
  732. * not what the user wants. Fail by default with option to enable.
  733. */
  734. if ((user == 0) && (!console))
  735. return -EBUSY;
  736. /* If the USB device is gone, we don't accept new opens */
  737. if (dev->virtualized)
  738. return -ENODEV;
  739. dev->fb_count++;
  740. kref_get(&dev->kref);
  741. if (fb_defio && (info->fbdefio == NULL)) {
  742. /* enable defio at last moment if not disabled by client */
  743. struct fb_deferred_io *fbdefio;
  744. fbdefio = kzalloc(sizeof(struct fb_deferred_io), GFP_KERNEL);
  745. if (fbdefio) {
  746. fbdefio->delay = DL_DEFIO_WRITE_DELAY;
  747. fbdefio->deferred_io = dlfb_dpy_deferred_io;
  748. }
  749. info->fbdefio = fbdefio;
  750. fb_deferred_io_init(info);
  751. }
  752. pr_notice("open /dev/fb%d user=%d fb_info=%p count=%d\n",
  753. info->node, user, info, dev->fb_count);
  754. return 0;
  755. }
  756. /*
  757. * Called when all client interfaces to start transactions have been disabled,
  758. * and all references to our device instance (dlfb_data) are released.
  759. * Every transaction must have a reference, so we know are fully spun down
  760. */
  761. static void dlfb_free(struct kref *kref)
  762. {
  763. struct dlfb_data *dev = container_of(kref, struct dlfb_data, kref);
  764. vfree(dev->backing_buffer);
  765. kfree(dev->edid);
  766. pr_warn("freeing dlfb_data %p\n", dev);
  767. kfree(dev);
  768. }
  769. static void dlfb_release_urb_work(struct work_struct *work)
  770. {
  771. struct urb_node *unode = container_of(work, struct urb_node,
  772. release_urb_work.work);
  773. up(&unode->dev->urbs.limit_sem);
  774. }
  775. static void dlfb_free_framebuffer(struct dlfb_data *dev)
  776. {
  777. struct fb_info *info = dev->info;
  778. if (info) {
  779. int node = info->node;
  780. unregister_framebuffer(info);
  781. if (info->cmap.len != 0)
  782. fb_dealloc_cmap(&info->cmap);
  783. if (info->monspecs.modedb)
  784. fb_destroy_modedb(info->monspecs.modedb);
  785. vfree(info->screen_base);
  786. fb_destroy_modelist(&info->modelist);
  787. dev->info = NULL;
  788. /* Assume info structure is freed after this point */
  789. framebuffer_release(info);
  790. pr_warn("fb_info for /dev/fb%d has been freed\n", node);
  791. }
  792. /* ref taken in probe() as part of registering framebfufer */
  793. kref_put(&dev->kref, dlfb_free);
  794. }
  795. static void dlfb_free_framebuffer_work(struct work_struct *work)
  796. {
  797. struct dlfb_data *dev = container_of(work, struct dlfb_data,
  798. free_framebuffer_work.work);
  799. dlfb_free_framebuffer(dev);
  800. }
  801. /*
  802. * Assumes caller is holding info->lock mutex (for open and release at least)
  803. */
  804. static int dlfb_ops_release(struct fb_info *info, int user)
  805. {
  806. struct dlfb_data *dev = info->par;
  807. dev->fb_count--;
  808. /* We can't free fb_info here - fbmem will touch it when we return */
  809. if (dev->virtualized && (dev->fb_count == 0))
  810. schedule_delayed_work(&dev->free_framebuffer_work, HZ);
  811. if ((dev->fb_count == 0) && (info->fbdefio)) {
  812. fb_deferred_io_cleanup(info);
  813. kfree(info->fbdefio);
  814. info->fbdefio = NULL;
  815. info->fbops->fb_mmap = dlfb_ops_mmap;
  816. }
  817. pr_warn("released /dev/fb%d user=%d count=%d\n",
  818. info->node, user, dev->fb_count);
  819. kref_put(&dev->kref, dlfb_free);
  820. return 0;
  821. }
  822. /*
  823. * Check whether a video mode is supported by the DisplayLink chip
  824. * We start from monitor's modes, so don't need to filter that here
  825. */
  826. static int dlfb_is_valid_mode(struct fb_videomode *mode,
  827. struct fb_info *info)
  828. {
  829. struct dlfb_data *dev = info->par;
  830. if (mode->xres * mode->yres > dev->sku_pixel_limit) {
  831. pr_warn("%dx%d beyond chip capabilities\n",
  832. mode->xres, mode->yres);
  833. return 0;
  834. }
  835. pr_info("%dx%d @ %d Hz valid mode\n", mode->xres, mode->yres,
  836. mode->refresh);
  837. return 1;
  838. }
  839. static void dlfb_var_color_format(struct fb_var_screeninfo *var)
  840. {
  841. const struct fb_bitfield red = { 11, 5, 0 };
  842. const struct fb_bitfield green = { 5, 6, 0 };
  843. const struct fb_bitfield blue = { 0, 5, 0 };
  844. var->bits_per_pixel = 16;
  845. var->red = red;
  846. var->green = green;
  847. var->blue = blue;
  848. }
  849. static int dlfb_ops_check_var(struct fb_var_screeninfo *var,
  850. struct fb_info *info)
  851. {
  852. struct fb_videomode mode;
  853. /* TODO: support dynamically changing framebuffer size */
  854. if ((var->xres * var->yres * 2) > info->fix.smem_len)
  855. return -EINVAL;
  856. /* set device-specific elements of var unrelated to mode */
  857. dlfb_var_color_format(var);
  858. fb_var_to_videomode(&mode, var);
  859. if (!dlfb_is_valid_mode(&mode, info))
  860. return -EINVAL;
  861. return 0;
  862. }
  863. static int dlfb_ops_set_par(struct fb_info *info)
  864. {
  865. struct dlfb_data *dev = info->par;
  866. int result;
  867. u16 *pix_framebuffer;
  868. int i;
  869. pr_notice("set_par mode %dx%d\n", info->var.xres, info->var.yres);
  870. result = dlfb_set_video_mode(dev, &info->var);
  871. if ((result == 0) && (dev->fb_count == 0)) {
  872. /* paint greenscreen */
  873. pix_framebuffer = (u16 *) info->screen_base;
  874. for (i = 0; i < info->fix.smem_len / 2; i++)
  875. pix_framebuffer[i] = 0x37e6;
  876. dlfb_handle_damage(dev, 0, 0, info->var.xres, info->var.yres,
  877. info->screen_base);
  878. }
  879. return result;
  880. }
  881. /* To fonzi the jukebox (e.g. make blanking changes take effect) */
  882. static char *dlfb_dummy_render(char *buf)
  883. {
  884. *buf++ = 0xAF;
  885. *buf++ = 0x6A; /* copy */
  886. *buf++ = 0x00; /* from address*/
  887. *buf++ = 0x00;
  888. *buf++ = 0x00;
  889. *buf++ = 0x01; /* one pixel */
  890. *buf++ = 0x00; /* to address */
  891. *buf++ = 0x00;
  892. *buf++ = 0x00;
  893. return buf;
  894. }
  895. /*
  896. * In order to come back from full DPMS off, we need to set the mode again
  897. */
  898. static int dlfb_ops_blank(int blank_mode, struct fb_info *info)
  899. {
  900. struct dlfb_data *dev = info->par;
  901. char *bufptr;
  902. struct urb *urb;
  903. pr_info("/dev/fb%d FB_BLANK mode %d --> %d\n",
  904. info->node, dev->blank_mode, blank_mode);
  905. if ((dev->blank_mode == FB_BLANK_POWERDOWN) &&
  906. (blank_mode != FB_BLANK_POWERDOWN)) {
  907. /* returning from powerdown requires a fresh modeset */
  908. dlfb_set_video_mode(dev, &info->var);
  909. }
  910. urb = dlfb_get_urb(dev);
  911. if (!urb)
  912. return 0;
  913. bufptr = (char *) urb->transfer_buffer;
  914. bufptr = dlfb_vidreg_lock(bufptr);
  915. bufptr = dlfb_blanking(bufptr, blank_mode);
  916. bufptr = dlfb_vidreg_unlock(bufptr);
  917. /* seems like a render op is needed to have blank change take effect */
  918. bufptr = dlfb_dummy_render(bufptr);
  919. dlfb_submit_urb(dev, urb, bufptr -
  920. (char *) urb->transfer_buffer);
  921. dev->blank_mode = blank_mode;
  922. return 0;
  923. }
  924. static struct fb_ops dlfb_ops = {
  925. .owner = THIS_MODULE,
  926. .fb_read = fb_sys_read,
  927. .fb_write = dlfb_ops_write,
  928. .fb_setcolreg = dlfb_ops_setcolreg,
  929. .fb_fillrect = dlfb_ops_fillrect,
  930. .fb_copyarea = dlfb_ops_copyarea,
  931. .fb_imageblit = dlfb_ops_imageblit,
  932. .fb_mmap = dlfb_ops_mmap,
  933. .fb_ioctl = dlfb_ops_ioctl,
  934. .fb_open = dlfb_ops_open,
  935. .fb_release = dlfb_ops_release,
  936. .fb_blank = dlfb_ops_blank,
  937. .fb_check_var = dlfb_ops_check_var,
  938. .fb_set_par = dlfb_ops_set_par,
  939. };
  940. /*
  941. * Assumes &info->lock held by caller
  942. * Assumes no active clients have framebuffer open
  943. */
  944. static int dlfb_realloc_framebuffer(struct dlfb_data *dev, struct fb_info *info)
  945. {
  946. int retval = -ENOMEM;
  947. int old_len = info->fix.smem_len;
  948. int new_len;
  949. unsigned char *old_fb = info->screen_base;
  950. unsigned char *new_fb;
  951. unsigned char *new_back = NULL;
  952. pr_warn("Reallocating framebuffer. Addresses will change!\n");
  953. new_len = info->fix.line_length * info->var.yres;
  954. if (PAGE_ALIGN(new_len) > old_len) {
  955. /*
  956. * Alloc system memory for virtual framebuffer
  957. */
  958. new_fb = vmalloc(new_len);
  959. if (!new_fb) {
  960. pr_err("Virtual framebuffer alloc failed\n");
  961. goto error;
  962. }
  963. if (info->screen_base) {
  964. memcpy(new_fb, old_fb, old_len);
  965. vfree(info->screen_base);
  966. }
  967. info->screen_base = new_fb;
  968. info->fix.smem_len = PAGE_ALIGN(new_len);
  969. info->fix.smem_start = (unsigned long) new_fb;
  970. info->flags = udlfb_info_flags;
  971. /*
  972. * Second framebuffer copy to mirror the framebuffer state
  973. * on the physical USB device. We can function without this.
  974. * But with imperfect damage info we may send pixels over USB
  975. * that were, in fact, unchanged - wasting limited USB bandwidth
  976. */
  977. if (shadow)
  978. new_back = vzalloc(new_len);
  979. if (!new_back)
  980. pr_info("No shadow/backing buffer allocated\n");
  981. else {
  982. vfree(dev->backing_buffer);
  983. dev->backing_buffer = new_back;
  984. }
  985. }
  986. retval = 0;
  987. error:
  988. return retval;
  989. }
  990. /*
  991. * 1) Get EDID from hw, or use sw default
  992. * 2) Parse into various fb_info structs
  993. * 3) Allocate virtual framebuffer memory to back highest res mode
  994. *
  995. * Parses EDID into three places used by various parts of fbdev:
  996. * fb_var_screeninfo contains the timing of the monitor's preferred mode
  997. * fb_info.monspecs is full parsed EDID info, including monspecs.modedb
  998. * fb_info.modelist is a linked list of all monitor & VESA modes which work
  999. *
  1000. * If EDID is not readable/valid, then modelist is all VESA modes,
  1001. * monspecs is NULL, and fb_var_screeninfo is set to safe VESA mode
  1002. * Returns 0 if successful
  1003. */
  1004. static int dlfb_setup_modes(struct dlfb_data *dev,
  1005. struct fb_info *info,
  1006. char *default_edid, size_t default_edid_size)
  1007. {
  1008. int i;
  1009. const struct fb_videomode *default_vmode = NULL;
  1010. int result = 0;
  1011. char *edid;
  1012. int tries = 3;
  1013. if (info->dev) /* only use mutex if info has been registered */
  1014. mutex_lock(&info->lock);
  1015. edid = kmalloc(EDID_LENGTH, GFP_KERNEL);
  1016. if (!edid) {
  1017. result = -ENOMEM;
  1018. goto error;
  1019. }
  1020. fb_destroy_modelist(&info->modelist);
  1021. memset(&info->monspecs, 0, sizeof(info->monspecs));
  1022. /*
  1023. * Try to (re)read EDID from hardware first
  1024. * EDID data may return, but not parse as valid
  1025. * Try again a few times, in case of e.g. analog cable noise
  1026. */
  1027. while (tries--) {
  1028. i = dlfb_get_edid(dev, edid, EDID_LENGTH);
  1029. if (i >= EDID_LENGTH)
  1030. fb_edid_to_monspecs(edid, &info->monspecs);
  1031. if (info->monspecs.modedb_len > 0) {
  1032. dev->edid = edid;
  1033. dev->edid_size = i;
  1034. break;
  1035. }
  1036. }
  1037. /* If that fails, use a previously returned EDID if available */
  1038. if (info->monspecs.modedb_len == 0) {
  1039. pr_err("Unable to get valid EDID from device/display\n");
  1040. if (dev->edid) {
  1041. fb_edid_to_monspecs(dev->edid, &info->monspecs);
  1042. if (info->monspecs.modedb_len > 0)
  1043. pr_err("Using previously queried EDID\n");
  1044. }
  1045. }
  1046. /* If that fails, use the default EDID we were handed */
  1047. if (info->monspecs.modedb_len == 0) {
  1048. if (default_edid_size >= EDID_LENGTH) {
  1049. fb_edid_to_monspecs(default_edid, &info->monspecs);
  1050. if (info->monspecs.modedb_len > 0) {
  1051. memcpy(edid, default_edid, default_edid_size);
  1052. dev->edid = edid;
  1053. dev->edid_size = default_edid_size;
  1054. pr_err("Using default/backup EDID\n");
  1055. }
  1056. }
  1057. }
  1058. /* If we've got modes, let's pick a best default mode */
  1059. if (info->monspecs.modedb_len > 0) {
  1060. for (i = 0; i < info->monspecs.modedb_len; i++) {
  1061. if (dlfb_is_valid_mode(&info->monspecs.modedb[i], info))
  1062. fb_add_videomode(&info->monspecs.modedb[i],
  1063. &info->modelist);
  1064. else {
  1065. if (i == 0)
  1066. /* if we've removed top/best mode */
  1067. info->monspecs.misc
  1068. &= ~FB_MISC_1ST_DETAIL;
  1069. }
  1070. }
  1071. default_vmode = fb_find_best_display(&info->monspecs,
  1072. &info->modelist);
  1073. }
  1074. /* If everything else has failed, fall back to safe default mode */
  1075. if (default_vmode == NULL) {
  1076. struct fb_videomode fb_vmode = {0};
  1077. /*
  1078. * Add the standard VESA modes to our modelist
  1079. * Since we don't have EDID, there may be modes that
  1080. * overspec monitor and/or are incorrect aspect ratio, etc.
  1081. * But at least the user has a chance to choose
  1082. */
  1083. for (i = 0; i < VESA_MODEDB_SIZE; i++) {
  1084. if (dlfb_is_valid_mode((struct fb_videomode *)
  1085. &vesa_modes[i], info))
  1086. fb_add_videomode(&vesa_modes[i],
  1087. &info->modelist);
  1088. }
  1089. /*
  1090. * default to resolution safe for projectors
  1091. * (since they are most common case without EDID)
  1092. */
  1093. fb_vmode.xres = 800;
  1094. fb_vmode.yres = 600;
  1095. fb_vmode.refresh = 60;
  1096. default_vmode = fb_find_nearest_mode(&fb_vmode,
  1097. &info->modelist);
  1098. }
  1099. /* If we have good mode and no active clients*/
  1100. if ((default_vmode != NULL) && (dev->fb_count == 0)) {
  1101. fb_videomode_to_var(&info->var, default_vmode);
  1102. dlfb_var_color_format(&info->var);
  1103. /*
  1104. * with mode size info, we can now alloc our framebuffer.
  1105. */
  1106. memcpy(&info->fix, &dlfb_fix, sizeof(dlfb_fix));
  1107. info->fix.line_length = info->var.xres *
  1108. (info->var.bits_per_pixel / 8);
  1109. result = dlfb_realloc_framebuffer(dev, info);
  1110. } else
  1111. result = -EINVAL;
  1112. error:
  1113. if (edid && (dev->edid != edid))
  1114. kfree(edid);
  1115. if (info->dev)
  1116. mutex_unlock(&info->lock);
  1117. return result;
  1118. }
  1119. static ssize_t metrics_bytes_rendered_show(struct device *fbdev,
  1120. struct device_attribute *a, char *buf) {
  1121. struct fb_info *fb_info = dev_get_drvdata(fbdev);
  1122. struct dlfb_data *dev = fb_info->par;
  1123. return snprintf(buf, PAGE_SIZE, "%u\n",
  1124. atomic_read(&dev->bytes_rendered));
  1125. }
  1126. static ssize_t metrics_bytes_identical_show(struct device *fbdev,
  1127. struct device_attribute *a, char *buf) {
  1128. struct fb_info *fb_info = dev_get_drvdata(fbdev);
  1129. struct dlfb_data *dev = fb_info->par;
  1130. return snprintf(buf, PAGE_SIZE, "%u\n",
  1131. atomic_read(&dev->bytes_identical));
  1132. }
  1133. static ssize_t metrics_bytes_sent_show(struct device *fbdev,
  1134. struct device_attribute *a, char *buf) {
  1135. struct fb_info *fb_info = dev_get_drvdata(fbdev);
  1136. struct dlfb_data *dev = fb_info->par;
  1137. return snprintf(buf, PAGE_SIZE, "%u\n",
  1138. atomic_read(&dev->bytes_sent));
  1139. }
  1140. static ssize_t metrics_cpu_kcycles_used_show(struct device *fbdev,
  1141. struct device_attribute *a, char *buf) {
  1142. struct fb_info *fb_info = dev_get_drvdata(fbdev);
  1143. struct dlfb_data *dev = fb_info->par;
  1144. return snprintf(buf, PAGE_SIZE, "%u\n",
  1145. atomic_read(&dev->cpu_kcycles_used));
  1146. }
  1147. static ssize_t edid_show(
  1148. struct file *filp,
  1149. struct kobject *kobj, struct bin_attribute *a,
  1150. char *buf, loff_t off, size_t count) {
  1151. struct device *fbdev = container_of(kobj, struct device, kobj);
  1152. struct fb_info *fb_info = dev_get_drvdata(fbdev);
  1153. struct dlfb_data *dev = fb_info->par;
  1154. if (dev->edid == NULL)
  1155. return 0;
  1156. if ((off >= dev->edid_size) || (count > dev->edid_size))
  1157. return 0;
  1158. if (off + count > dev->edid_size)
  1159. count = dev->edid_size - off;
  1160. pr_info("sysfs edid copy %p to %p, %d bytes\n",
  1161. dev->edid, buf, (int) count);
  1162. memcpy(buf, dev->edid, count);
  1163. return count;
  1164. }
  1165. static ssize_t edid_store(
  1166. struct file *filp,
  1167. struct kobject *kobj, struct bin_attribute *a,
  1168. char *src, loff_t src_off, size_t src_size) {
  1169. struct device *fbdev = container_of(kobj, struct device, kobj);
  1170. struct fb_info *fb_info = dev_get_drvdata(fbdev);
  1171. struct dlfb_data *dev = fb_info->par;
  1172. int ret;
  1173. /* We only support write of entire EDID at once, no offset*/
  1174. if ((src_size != EDID_LENGTH) || (src_off != 0))
  1175. return -EINVAL;
  1176. ret = dlfb_setup_modes(dev, fb_info, src, src_size);
  1177. if (ret)
  1178. return ret;
  1179. if (!dev->edid || memcmp(src, dev->edid, src_size))
  1180. return -EINVAL;
  1181. pr_info("sysfs written EDID is new default\n");
  1182. dlfb_ops_set_par(fb_info);
  1183. return src_size;
  1184. }
  1185. static ssize_t metrics_reset_store(struct device *fbdev,
  1186. struct device_attribute *attr,
  1187. const char *buf, size_t count)
  1188. {
  1189. struct fb_info *fb_info = dev_get_drvdata(fbdev);
  1190. struct dlfb_data *dev = fb_info->par;
  1191. atomic_set(&dev->bytes_rendered, 0);
  1192. atomic_set(&dev->bytes_identical, 0);
  1193. atomic_set(&dev->bytes_sent, 0);
  1194. atomic_set(&dev->cpu_kcycles_used, 0);
  1195. return count;
  1196. }
  1197. static struct bin_attribute edid_attr = {
  1198. .attr.name = "edid",
  1199. .attr.mode = 0666,
  1200. .size = EDID_LENGTH,
  1201. .read = edid_show,
  1202. .write = edid_store
  1203. };
  1204. static struct device_attribute fb_device_attrs[] = {
  1205. __ATTR_RO(metrics_bytes_rendered),
  1206. __ATTR_RO(metrics_bytes_identical),
  1207. __ATTR_RO(metrics_bytes_sent),
  1208. __ATTR_RO(metrics_cpu_kcycles_used),
  1209. __ATTR(metrics_reset, S_IWUSR, NULL, metrics_reset_store),
  1210. };
  1211. /*
  1212. * This is necessary before we can communicate with the display controller.
  1213. */
  1214. static int dlfb_select_std_channel(struct dlfb_data *dev)
  1215. {
  1216. int ret;
  1217. u8 set_def_chn[] = { 0x57, 0xCD, 0xDC, 0xA7,
  1218. 0x1C, 0x88, 0x5E, 0x15,
  1219. 0x60, 0xFE, 0xC6, 0x97,
  1220. 0x16, 0x3D, 0x47, 0xF2 };
  1221. ret = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
  1222. NR_USB_REQUEST_CHANNEL,
  1223. (USB_DIR_OUT | USB_TYPE_VENDOR), 0, 0,
  1224. set_def_chn, sizeof(set_def_chn), USB_CTRL_SET_TIMEOUT);
  1225. return ret;
  1226. }
  1227. static int dlfb_parse_vendor_descriptor(struct dlfb_data *dev,
  1228. struct usb_interface *interface)
  1229. {
  1230. char *desc;
  1231. char *buf;
  1232. char *desc_end;
  1233. int total_len = 0;
  1234. buf = kzalloc(MAX_VENDOR_DESCRIPTOR_SIZE, GFP_KERNEL);
  1235. if (!buf)
  1236. return false;
  1237. desc = buf;
  1238. total_len = usb_get_descriptor(interface_to_usbdev(interface),
  1239. 0x5f, /* vendor specific */
  1240. 0, desc, MAX_VENDOR_DESCRIPTOR_SIZE);
  1241. /* if not found, look in configuration descriptor */
  1242. if (total_len < 0) {
  1243. if (0 == usb_get_extra_descriptor(interface->cur_altsetting,
  1244. 0x5f, &desc))
  1245. total_len = (int) desc[0];
  1246. }
  1247. if (total_len > 5) {
  1248. pr_info("vendor descriptor length:%x data:%11ph\n", total_len,
  1249. desc);
  1250. if ((desc[0] != total_len) || /* descriptor length */
  1251. (desc[1] != 0x5f) || /* vendor descriptor type */
  1252. (desc[2] != 0x01) || /* version (2 bytes) */
  1253. (desc[3] != 0x00) ||
  1254. (desc[4] != total_len - 2)) /* length after type */
  1255. goto unrecognized;
  1256. desc_end = desc + total_len;
  1257. desc += 5; /* the fixed header we've already parsed */
  1258. while (desc < desc_end) {
  1259. u8 length;
  1260. u16 key;
  1261. key = le16_to_cpu(*((u16 *) desc));
  1262. desc += sizeof(u16);
  1263. length = *desc;
  1264. desc++;
  1265. switch (key) {
  1266. case 0x0200: { /* max_area */
  1267. u32 max_area;
  1268. max_area = le32_to_cpu(*((u32 *)desc));
  1269. pr_warn("DL chip limited to %d pixel modes\n",
  1270. max_area);
  1271. dev->sku_pixel_limit = max_area;
  1272. break;
  1273. }
  1274. default:
  1275. break;
  1276. }
  1277. desc += length;
  1278. }
  1279. } else {
  1280. pr_info("vendor descriptor not available (%d)\n", total_len);
  1281. }
  1282. goto success;
  1283. unrecognized:
  1284. /* allow udlfb to load for now even if firmware unrecognized */
  1285. pr_err("Unrecognized vendor firmware descriptor\n");
  1286. success:
  1287. kfree(buf);
  1288. return true;
  1289. }
  1290. static void dlfb_init_framebuffer_work(struct work_struct *work);
  1291. static int dlfb_usb_probe(struct usb_interface *interface,
  1292. const struct usb_device_id *id)
  1293. {
  1294. struct usb_device *usbdev;
  1295. struct dlfb_data *dev = NULL;
  1296. int retval = -ENOMEM;
  1297. /* usb initialization */
  1298. usbdev = interface_to_usbdev(interface);
  1299. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  1300. if (dev == NULL) {
  1301. dev_err(&interface->dev, "dlfb_usb_probe: failed alloc of dev struct\n");
  1302. goto error;
  1303. }
  1304. kref_init(&dev->kref); /* matching kref_put in usb .disconnect fn */
  1305. dev->udev = usbdev;
  1306. dev->gdev = &usbdev->dev; /* our generic struct device * */
  1307. usb_set_intfdata(interface, dev);
  1308. pr_info("%s %s - serial #%s\n",
  1309. usbdev->manufacturer, usbdev->product, usbdev->serial);
  1310. pr_info("vid_%04x&pid_%04x&rev_%04x driver's dlfb_data struct at %p\n",
  1311. usbdev->descriptor.idVendor, usbdev->descriptor.idProduct,
  1312. usbdev->descriptor.bcdDevice, dev);
  1313. pr_info("console enable=%d\n", console);
  1314. pr_info("fb_defio enable=%d\n", fb_defio);
  1315. pr_info("shadow enable=%d\n", shadow);
  1316. dev->sku_pixel_limit = 2048 * 1152; /* default to maximum */
  1317. if (!dlfb_parse_vendor_descriptor(dev, interface)) {
  1318. pr_err("firmware not recognized. Assume incompatible device\n");
  1319. goto error;
  1320. }
  1321. if (pixel_limit) {
  1322. pr_warn("DL chip limit of %d overridden"
  1323. " by module param to %d\n",
  1324. dev->sku_pixel_limit, pixel_limit);
  1325. dev->sku_pixel_limit = pixel_limit;
  1326. }
  1327. if (!dlfb_alloc_urb_list(dev, WRITES_IN_FLIGHT, MAX_TRANSFER)) {
  1328. retval = -ENOMEM;
  1329. pr_err("dlfb_alloc_urb_list failed\n");
  1330. goto error;
  1331. }
  1332. kref_get(&dev->kref); /* matching kref_put in free_framebuffer_work */
  1333. /* We don't register a new USB class. Our client interface is fbdev */
  1334. /* Workitem keep things fast & simple during USB enumeration */
  1335. INIT_DELAYED_WORK(&dev->init_framebuffer_work,
  1336. dlfb_init_framebuffer_work);
  1337. schedule_delayed_work(&dev->init_framebuffer_work, 0);
  1338. return 0;
  1339. error:
  1340. if (dev) {
  1341. kref_put(&dev->kref, dlfb_free); /* ref for framebuffer */
  1342. kref_put(&dev->kref, dlfb_free); /* last ref from kref_init */
  1343. /* dev has been deallocated. Do not dereference */
  1344. }
  1345. return retval;
  1346. }
  1347. static void dlfb_init_framebuffer_work(struct work_struct *work)
  1348. {
  1349. struct dlfb_data *dev = container_of(work, struct dlfb_data,
  1350. init_framebuffer_work.work);
  1351. struct fb_info *info;
  1352. int retval;
  1353. int i;
  1354. /* allocates framebuffer driver structure, not framebuffer memory */
  1355. info = framebuffer_alloc(0, dev->gdev);
  1356. if (!info) {
  1357. retval = -ENOMEM;
  1358. pr_err("framebuffer_alloc failed\n");
  1359. goto error;
  1360. }
  1361. dev->info = info;
  1362. info->par = dev;
  1363. info->pseudo_palette = dev->pseudo_palette;
  1364. info->fbops = &dlfb_ops;
  1365. retval = fb_alloc_cmap(&info->cmap, 256, 0);
  1366. if (retval < 0) {
  1367. pr_err("fb_alloc_cmap failed %x\n", retval);
  1368. goto error;
  1369. }
  1370. INIT_DELAYED_WORK(&dev->free_framebuffer_work,
  1371. dlfb_free_framebuffer_work);
  1372. INIT_LIST_HEAD(&info->modelist);
  1373. retval = dlfb_setup_modes(dev, info, NULL, 0);
  1374. if (retval != 0) {
  1375. pr_err("unable to find common mode for display and adapter\n");
  1376. goto error;
  1377. }
  1378. /* ready to begin using device */
  1379. atomic_set(&dev->usb_active, 1);
  1380. dlfb_select_std_channel(dev);
  1381. dlfb_ops_check_var(&info->var, info);
  1382. dlfb_ops_set_par(info);
  1383. retval = register_framebuffer(info);
  1384. if (retval < 0) {
  1385. pr_err("register_framebuffer failed %d\n", retval);
  1386. goto error;
  1387. }
  1388. for (i = 0; i < ARRAY_SIZE(fb_device_attrs); i++) {
  1389. retval = device_create_file(info->dev, &fb_device_attrs[i]);
  1390. if (retval) {
  1391. pr_warn("device_create_file failed %d\n", retval);
  1392. }
  1393. }
  1394. retval = device_create_bin_file(info->dev, &edid_attr);
  1395. if (retval) {
  1396. pr_warn("device_create_bin_file failed %d\n", retval);
  1397. }
  1398. pr_info("DisplayLink USB device /dev/fb%d attached. %dx%d resolution."
  1399. " Using %dK framebuffer memory\n", info->node,
  1400. info->var.xres, info->var.yres,
  1401. ((dev->backing_buffer) ?
  1402. info->fix.smem_len * 2 : info->fix.smem_len) >> 10);
  1403. return;
  1404. error:
  1405. dlfb_free_framebuffer(dev);
  1406. }
  1407. static void dlfb_usb_disconnect(struct usb_interface *interface)
  1408. {
  1409. struct dlfb_data *dev;
  1410. struct fb_info *info;
  1411. int i;
  1412. dev = usb_get_intfdata(interface);
  1413. info = dev->info;
  1414. pr_info("USB disconnect starting\n");
  1415. /* we virtualize until all fb clients release. Then we free */
  1416. dev->virtualized = true;
  1417. /* When non-active we'll update virtual framebuffer, but no new urbs */
  1418. atomic_set(&dev->usb_active, 0);
  1419. /* this function will wait for all in-flight urbs to complete */
  1420. dlfb_free_urb_list(dev);
  1421. if (info) {
  1422. /* remove udlfb's sysfs interfaces */
  1423. for (i = 0; i < ARRAY_SIZE(fb_device_attrs); i++)
  1424. device_remove_file(info->dev, &fb_device_attrs[i]);
  1425. device_remove_bin_file(info->dev, &edid_attr);
  1426. unlink_framebuffer(info);
  1427. }
  1428. usb_set_intfdata(interface, NULL);
  1429. dev->udev = NULL;
  1430. dev->gdev = NULL;
  1431. /* if clients still have us open, will be freed on last close */
  1432. if (dev->fb_count == 0)
  1433. schedule_delayed_work(&dev->free_framebuffer_work, 0);
  1434. /* release reference taken by kref_init in probe() */
  1435. kref_put(&dev->kref, dlfb_free);
  1436. /* consider dlfb_data freed */
  1437. return;
  1438. }
  1439. static struct usb_driver dlfb_driver = {
  1440. .name = "udlfb",
  1441. .probe = dlfb_usb_probe,
  1442. .disconnect = dlfb_usb_disconnect,
  1443. .id_table = id_table,
  1444. };
  1445. module_usb_driver(dlfb_driver);
  1446. static void dlfb_urb_completion(struct urb *urb)
  1447. {
  1448. struct urb_node *unode = urb->context;
  1449. struct dlfb_data *dev = unode->dev;
  1450. unsigned long flags;
  1451. /* sync/async unlink faults aren't errors */
  1452. if (urb->status) {
  1453. if (!(urb->status == -ENOENT ||
  1454. urb->status == -ECONNRESET ||
  1455. urb->status == -ESHUTDOWN)) {
  1456. pr_err("%s - nonzero write bulk status received: %d\n",
  1457. __func__, urb->status);
  1458. atomic_set(&dev->lost_pixels, 1);
  1459. }
  1460. }
  1461. urb->transfer_buffer_length = dev->urbs.size; /* reset to actual */
  1462. spin_lock_irqsave(&dev->urbs.lock, flags);
  1463. list_add_tail(&unode->entry, &dev->urbs.list);
  1464. dev->urbs.available++;
  1465. spin_unlock_irqrestore(&dev->urbs.lock, flags);
  1466. /*
  1467. * When using fb_defio, we deadlock if up() is called
  1468. * while another is waiting. So queue to another process.
  1469. */
  1470. if (fb_defio)
  1471. schedule_delayed_work(&unode->release_urb_work, 0);
  1472. else
  1473. up(&dev->urbs.limit_sem);
  1474. }
  1475. static void dlfb_free_urb_list(struct dlfb_data *dev)
  1476. {
  1477. int count = dev->urbs.count;
  1478. struct list_head *node;
  1479. struct urb_node *unode;
  1480. struct urb *urb;
  1481. int ret;
  1482. unsigned long flags;
  1483. pr_notice("Freeing all render urbs\n");
  1484. /* keep waiting and freeing, until we've got 'em all */
  1485. while (count--) {
  1486. /* Getting interrupted means a leak, but ok at disconnect */
  1487. ret = down_interruptible(&dev->urbs.limit_sem);
  1488. if (ret)
  1489. break;
  1490. spin_lock_irqsave(&dev->urbs.lock, flags);
  1491. node = dev->urbs.list.next; /* have reserved one with sem */
  1492. list_del_init(node);
  1493. spin_unlock_irqrestore(&dev->urbs.lock, flags);
  1494. unode = list_entry(node, struct urb_node, entry);
  1495. urb = unode->urb;
  1496. /* Free each separately allocated piece */
  1497. usb_free_coherent(urb->dev, dev->urbs.size,
  1498. urb->transfer_buffer, urb->transfer_dma);
  1499. usb_free_urb(urb);
  1500. kfree(node);
  1501. }
  1502. dev->urbs.count = 0;
  1503. }
  1504. static int dlfb_alloc_urb_list(struct dlfb_data *dev, int count, size_t size)
  1505. {
  1506. int i = 0;
  1507. struct urb *urb;
  1508. struct urb_node *unode;
  1509. char *buf;
  1510. spin_lock_init(&dev->urbs.lock);
  1511. dev->urbs.size = size;
  1512. INIT_LIST_HEAD(&dev->urbs.list);
  1513. while (i < count) {
  1514. unode = kzalloc(sizeof(struct urb_node), GFP_KERNEL);
  1515. if (!unode)
  1516. break;
  1517. unode->dev = dev;
  1518. INIT_DELAYED_WORK(&unode->release_urb_work,
  1519. dlfb_release_urb_work);
  1520. urb = usb_alloc_urb(0, GFP_KERNEL);
  1521. if (!urb) {
  1522. kfree(unode);
  1523. break;
  1524. }
  1525. unode->urb = urb;
  1526. buf = usb_alloc_coherent(dev->udev, MAX_TRANSFER, GFP_KERNEL,
  1527. &urb->transfer_dma);
  1528. if (!buf) {
  1529. kfree(unode);
  1530. usb_free_urb(urb);
  1531. break;
  1532. }
  1533. /* urb->transfer_buffer_length set to actual before submit */
  1534. usb_fill_bulk_urb(urb, dev->udev, usb_sndbulkpipe(dev->udev, 1),
  1535. buf, size, dlfb_urb_completion, unode);
  1536. urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  1537. list_add_tail(&unode->entry, &dev->urbs.list);
  1538. i++;
  1539. }
  1540. sema_init(&dev->urbs.limit_sem, i);
  1541. dev->urbs.count = i;
  1542. dev->urbs.available = i;
  1543. pr_notice("allocated %d %d byte urbs\n", i, (int) size);
  1544. return i;
  1545. }
  1546. static struct urb *dlfb_get_urb(struct dlfb_data *dev)
  1547. {
  1548. int ret = 0;
  1549. struct list_head *entry;
  1550. struct urb_node *unode;
  1551. struct urb *urb = NULL;
  1552. unsigned long flags;
  1553. /* Wait for an in-flight buffer to complete and get re-queued */
  1554. ret = down_timeout(&dev->urbs.limit_sem, GET_URB_TIMEOUT);
  1555. if (ret) {
  1556. atomic_set(&dev->lost_pixels, 1);
  1557. pr_warn("wait for urb interrupted: %x available: %d\n",
  1558. ret, dev->urbs.available);
  1559. goto error;
  1560. }
  1561. spin_lock_irqsave(&dev->urbs.lock, flags);
  1562. BUG_ON(list_empty(&dev->urbs.list)); /* reserved one with limit_sem */
  1563. entry = dev->urbs.list.next;
  1564. list_del_init(entry);
  1565. dev->urbs.available--;
  1566. spin_unlock_irqrestore(&dev->urbs.lock, flags);
  1567. unode = list_entry(entry, struct urb_node, entry);
  1568. urb = unode->urb;
  1569. error:
  1570. return urb;
  1571. }
  1572. static int dlfb_submit_urb(struct dlfb_data *dev, struct urb *urb, size_t len)
  1573. {
  1574. int ret;
  1575. BUG_ON(len > dev->urbs.size);
  1576. urb->transfer_buffer_length = len; /* set to actual payload len */
  1577. ret = usb_submit_urb(urb, GFP_KERNEL);
  1578. if (ret) {
  1579. dlfb_urb_completion(urb); /* because no one else will */
  1580. atomic_set(&dev->lost_pixels, 1);
  1581. pr_err("usb_submit_urb error %x\n", ret);
  1582. }
  1583. return ret;
  1584. }
  1585. module_param(console, bool, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
  1586. MODULE_PARM_DESC(console, "Allow fbcon to open framebuffer");
  1587. module_param(fb_defio, bool, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
  1588. MODULE_PARM_DESC(fb_defio, "Page fault detection of mmap writes");
  1589. module_param(shadow, bool, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
  1590. MODULE_PARM_DESC(shadow, "Shadow vid mem. Disable to save mem but lose perf");
  1591. module_param(pixel_limit, int, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
  1592. MODULE_PARM_DESC(pixel_limit, "Force limit on max mode (in x*y pixels)");
  1593. MODULE_AUTHOR("Roberto De Ioris <roberto@unbit.it>, "
  1594. "Jaya Kumar <jayakumar.lkml@gmail.com>, "
  1595. "Bernie Thompson <bernie@plugable.com>");
  1596. MODULE_DESCRIPTION("DisplayLink kernel framebuffer driver");
  1597. MODULE_LICENSE("GPL");