udlfb.c 51 KB

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