smscufx.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * smscufx.c -- Framebuffer driver for SMSC UFX USB controller
  4. *
  5. * Copyright (C) 2011 Steve Glendinning <steve.glendinning@shawell.net>
  6. * Copyright (C) 2009 Roberto De Ioris <roberto@unbit.it>
  7. * Copyright (C) 2009 Jaya Kumar <jayakumar.lkml@gmail.com>
  8. * Copyright (C) 2009 Bernie Thompson <bernie@plugable.com>
  9. *
  10. * Based on udlfb, with work from Florian Echtler, Henrik Bjerregaard Pedersen,
  11. * and others.
  12. *
  13. * Works well with Bernie Thompson's X DAMAGE patch to xf86-video-fbdev
  14. * available from http://git.plugable.com
  15. *
  16. * Layout is based on skeletonfb by James Simmons and Geert Uytterhoeven,
  17. * usb-skeleton by GregKH.
  18. */
  19. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  20. #include <linux/module.h>
  21. #include <linux/kernel.h>
  22. #include <linux/init.h>
  23. #include <linux/usb.h>
  24. #include <linux/uaccess.h>
  25. #include <linux/mm.h>
  26. #include <linux/fb.h>
  27. #include <linux/vmalloc.h>
  28. #include <linux/slab.h>
  29. #include <linux/delay.h>
  30. #include "edid.h"
  31. #define check_warn(status, fmt, args...) \
  32. ({ if (status < 0) pr_warn(fmt, ##args); })
  33. #define check_warn_return(status, fmt, args...) \
  34. ({ if (status < 0) { pr_warn(fmt, ##args); return status; } })
  35. #define check_warn_goto_error(status, fmt, args...) \
  36. ({ if (status < 0) { pr_warn(fmt, ##args); goto error; } })
  37. #define all_bits_set(x, bits) (((x) & (bits)) == (bits))
  38. #define USB_VENDOR_REQUEST_WRITE_REGISTER 0xA0
  39. #define USB_VENDOR_REQUEST_READ_REGISTER 0xA1
  40. /*
  41. * TODO: Propose standard fb.h ioctl for reporting damage,
  42. * using _IOWR() and one of the existing area structs from fb.h
  43. * Consider these ioctls deprecated, but they're still used by the
  44. * DisplayLink X server as yet - need both to be modified in tandem
  45. * when new ioctl(s) are ready.
  46. */
  47. #define UFX_IOCTL_RETURN_EDID (0xAD)
  48. #define UFX_IOCTL_REPORT_DAMAGE (0xAA)
  49. /* -BULK_SIZE as per usb-skeleton. Can we get full page and avoid overhead? */
  50. #define BULK_SIZE (512)
  51. #define MAX_TRANSFER (PAGE_SIZE*16 - BULK_SIZE)
  52. #define WRITES_IN_FLIGHT (4)
  53. #define GET_URB_TIMEOUT (HZ)
  54. #define FREE_URB_TIMEOUT (HZ*2)
  55. #define BPP 2
  56. #define UFX_DEFIO_WRITE_DELAY 5 /* fb_deferred_io.delay in jiffies */
  57. #define UFX_DEFIO_WRITE_DISABLE (HZ*60) /* "disable" with long delay */
  58. struct dloarea {
  59. int x, y;
  60. int w, h;
  61. };
  62. struct urb_node {
  63. struct list_head entry;
  64. struct ufx_data *dev;
  65. struct delayed_work release_urb_work;
  66. struct urb *urb;
  67. };
  68. struct urb_list {
  69. struct list_head list;
  70. spinlock_t lock;
  71. struct semaphore limit_sem;
  72. int available;
  73. int count;
  74. size_t size;
  75. };
  76. struct ufx_data {
  77. struct usb_device *udev;
  78. struct device *gdev; /* &udev->dev */
  79. struct fb_info *info;
  80. struct urb_list urbs;
  81. struct kref kref;
  82. int fb_count;
  83. bool virtualized; /* true when physical usb device not present */
  84. atomic_t usb_active; /* 0 = update virtual buffer, but no usb traffic */
  85. atomic_t lost_pixels; /* 1 = a render op failed. Need screen refresh */
  86. u8 *edid; /* null until we read edid from hw or get from sysfs */
  87. size_t edid_size;
  88. u32 pseudo_palette[256];
  89. };
  90. static struct fb_fix_screeninfo ufx_fix = {
  91. .id = "smscufx",
  92. .type = FB_TYPE_PACKED_PIXELS,
  93. .visual = FB_VISUAL_TRUECOLOR,
  94. .xpanstep = 0,
  95. .ypanstep = 0,
  96. .ywrapstep = 0,
  97. .accel = FB_ACCEL_NONE,
  98. };
  99. static const u32 smscufx_info_flags = FBINFO_READS_FAST |
  100. FBINFO_VIRTFB | FBINFO_HWACCEL_IMAGEBLIT | FBINFO_HWACCEL_FILLRECT |
  101. FBINFO_HWACCEL_COPYAREA | FBINFO_MISC_ALWAYS_SETPAR;
  102. static const struct usb_device_id id_table[] = {
  103. {USB_DEVICE(0x0424, 0x9d00),},
  104. {USB_DEVICE(0x0424, 0x9d01),},
  105. {},
  106. };
  107. MODULE_DEVICE_TABLE(usb, id_table);
  108. /* module options */
  109. static bool console; /* Optionally allow fbcon to consume first framebuffer */
  110. static bool fb_defio = true; /* Optionally enable fb_defio mmap support */
  111. /* ufx keeps a list of urbs for efficient bulk transfers */
  112. static void ufx_urb_completion(struct urb *urb);
  113. static struct urb *ufx_get_urb(struct ufx_data *dev);
  114. static int ufx_submit_urb(struct ufx_data *dev, struct urb * urb, size_t len);
  115. static int ufx_alloc_urb_list(struct ufx_data *dev, int count, size_t size);
  116. static void ufx_free_urb_list(struct ufx_data *dev);
  117. static DEFINE_MUTEX(disconnect_mutex);
  118. /* reads a control register */
  119. static int ufx_reg_read(struct ufx_data *dev, u32 index, u32 *data)
  120. {
  121. u32 *buf = kmalloc(4, GFP_KERNEL);
  122. int ret;
  123. BUG_ON(!dev);
  124. if (!buf)
  125. return -ENOMEM;
  126. ret = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
  127. USB_VENDOR_REQUEST_READ_REGISTER,
  128. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  129. 00, index, buf, 4, USB_CTRL_GET_TIMEOUT);
  130. le32_to_cpus(buf);
  131. *data = *buf;
  132. kfree(buf);
  133. if (unlikely(ret < 0))
  134. pr_warn("Failed to read register index 0x%08x\n", index);
  135. return ret;
  136. }
  137. /* writes a control register */
  138. static int ufx_reg_write(struct ufx_data *dev, u32 index, u32 data)
  139. {
  140. u32 *buf = kmalloc(4, GFP_KERNEL);
  141. int ret;
  142. BUG_ON(!dev);
  143. if (!buf)
  144. return -ENOMEM;
  145. *buf = data;
  146. cpu_to_le32s(buf);
  147. ret = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
  148. USB_VENDOR_REQUEST_WRITE_REGISTER,
  149. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  150. 00, index, buf, 4, USB_CTRL_SET_TIMEOUT);
  151. kfree(buf);
  152. if (unlikely(ret < 0))
  153. pr_warn("Failed to write register index 0x%08x with value "
  154. "0x%08x\n", index, data);
  155. return ret;
  156. }
  157. static int ufx_reg_clear_and_set_bits(struct ufx_data *dev, u32 index,
  158. u32 bits_to_clear, u32 bits_to_set)
  159. {
  160. u32 data;
  161. int status = ufx_reg_read(dev, index, &data);
  162. check_warn_return(status, "ufx_reg_clear_and_set_bits error reading "
  163. "0x%x", index);
  164. data &= (~bits_to_clear);
  165. data |= bits_to_set;
  166. status = ufx_reg_write(dev, index, data);
  167. check_warn_return(status, "ufx_reg_clear_and_set_bits error writing "
  168. "0x%x", index);
  169. return 0;
  170. }
  171. static int ufx_reg_set_bits(struct ufx_data *dev, u32 index, u32 bits)
  172. {
  173. return ufx_reg_clear_and_set_bits(dev, index, 0, bits);
  174. }
  175. static int ufx_reg_clear_bits(struct ufx_data *dev, u32 index, u32 bits)
  176. {
  177. return ufx_reg_clear_and_set_bits(dev, index, bits, 0);
  178. }
  179. static int ufx_lite_reset(struct ufx_data *dev)
  180. {
  181. int status;
  182. u32 value;
  183. status = ufx_reg_write(dev, 0x3008, 0x00000001);
  184. check_warn_return(status, "ufx_lite_reset error writing 0x3008");
  185. status = ufx_reg_read(dev, 0x3008, &value);
  186. check_warn_return(status, "ufx_lite_reset error reading 0x3008");
  187. return (value == 0) ? 0 : -EIO;
  188. }
  189. /* If display is unblanked, then blank it */
  190. static int ufx_blank(struct ufx_data *dev, bool wait)
  191. {
  192. u32 dc_ctrl, dc_sts;
  193. int i;
  194. int status = ufx_reg_read(dev, 0x2004, &dc_sts);
  195. check_warn_return(status, "ufx_blank error reading 0x2004");
  196. status = ufx_reg_read(dev, 0x2000, &dc_ctrl);
  197. check_warn_return(status, "ufx_blank error reading 0x2000");
  198. /* return success if display is already blanked */
  199. if ((dc_sts & 0x00000100) || (dc_ctrl & 0x00000100))
  200. return 0;
  201. /* request the DC to blank the display */
  202. dc_ctrl |= 0x00000100;
  203. status = ufx_reg_write(dev, 0x2000, dc_ctrl);
  204. check_warn_return(status, "ufx_blank error writing 0x2000");
  205. /* return success immediately if we don't have to wait */
  206. if (!wait)
  207. return 0;
  208. for (i = 0; i < 250; i++) {
  209. status = ufx_reg_read(dev, 0x2004, &dc_sts);
  210. check_warn_return(status, "ufx_blank error reading 0x2004");
  211. if (dc_sts & 0x00000100)
  212. return 0;
  213. }
  214. /* timed out waiting for display to blank */
  215. return -EIO;
  216. }
  217. /* If display is blanked, then unblank it */
  218. static int ufx_unblank(struct ufx_data *dev, bool wait)
  219. {
  220. u32 dc_ctrl, dc_sts;
  221. int i;
  222. int status = ufx_reg_read(dev, 0x2004, &dc_sts);
  223. check_warn_return(status, "ufx_unblank error reading 0x2004");
  224. status = ufx_reg_read(dev, 0x2000, &dc_ctrl);
  225. check_warn_return(status, "ufx_unblank error reading 0x2000");
  226. /* return success if display is already unblanked */
  227. if (((dc_sts & 0x00000100) == 0) || ((dc_ctrl & 0x00000100) == 0))
  228. return 0;
  229. /* request the DC to unblank the display */
  230. dc_ctrl &= ~0x00000100;
  231. status = ufx_reg_write(dev, 0x2000, dc_ctrl);
  232. check_warn_return(status, "ufx_unblank error writing 0x2000");
  233. /* return success immediately if we don't have to wait */
  234. if (!wait)
  235. return 0;
  236. for (i = 0; i < 250; i++) {
  237. status = ufx_reg_read(dev, 0x2004, &dc_sts);
  238. check_warn_return(status, "ufx_unblank error reading 0x2004");
  239. if ((dc_sts & 0x00000100) == 0)
  240. return 0;
  241. }
  242. /* timed out waiting for display to unblank */
  243. return -EIO;
  244. }
  245. /* If display is enabled, then disable it */
  246. static int ufx_disable(struct ufx_data *dev, bool wait)
  247. {
  248. u32 dc_ctrl, dc_sts;
  249. int i;
  250. int status = ufx_reg_read(dev, 0x2004, &dc_sts);
  251. check_warn_return(status, "ufx_disable error reading 0x2004");
  252. status = ufx_reg_read(dev, 0x2000, &dc_ctrl);
  253. check_warn_return(status, "ufx_disable error reading 0x2000");
  254. /* return success if display is already disabled */
  255. if (((dc_sts & 0x00000001) == 0) || ((dc_ctrl & 0x00000001) == 0))
  256. return 0;
  257. /* request the DC to disable the display */
  258. dc_ctrl &= ~(0x00000001);
  259. status = ufx_reg_write(dev, 0x2000, dc_ctrl);
  260. check_warn_return(status, "ufx_disable error writing 0x2000");
  261. /* return success immediately if we don't have to wait */
  262. if (!wait)
  263. return 0;
  264. for (i = 0; i < 250; i++) {
  265. status = ufx_reg_read(dev, 0x2004, &dc_sts);
  266. check_warn_return(status, "ufx_disable error reading 0x2004");
  267. if ((dc_sts & 0x00000001) == 0)
  268. return 0;
  269. }
  270. /* timed out waiting for display to disable */
  271. return -EIO;
  272. }
  273. /* If display is disabled, then enable it */
  274. static int ufx_enable(struct ufx_data *dev, bool wait)
  275. {
  276. u32 dc_ctrl, dc_sts;
  277. int i;
  278. int status = ufx_reg_read(dev, 0x2004, &dc_sts);
  279. check_warn_return(status, "ufx_enable error reading 0x2004");
  280. status = ufx_reg_read(dev, 0x2000, &dc_ctrl);
  281. check_warn_return(status, "ufx_enable error reading 0x2000");
  282. /* return success if display is already enabled */
  283. if ((dc_sts & 0x00000001) || (dc_ctrl & 0x00000001))
  284. return 0;
  285. /* request the DC to enable the display */
  286. dc_ctrl |= 0x00000001;
  287. status = ufx_reg_write(dev, 0x2000, dc_ctrl);
  288. check_warn_return(status, "ufx_enable error writing 0x2000");
  289. /* return success immediately if we don't have to wait */
  290. if (!wait)
  291. return 0;
  292. for (i = 0; i < 250; i++) {
  293. status = ufx_reg_read(dev, 0x2004, &dc_sts);
  294. check_warn_return(status, "ufx_enable error reading 0x2004");
  295. if (dc_sts & 0x00000001)
  296. return 0;
  297. }
  298. /* timed out waiting for display to enable */
  299. return -EIO;
  300. }
  301. static int ufx_config_sys_clk(struct ufx_data *dev)
  302. {
  303. int status = ufx_reg_write(dev, 0x700C, 0x8000000F);
  304. check_warn_return(status, "error writing 0x700C");
  305. status = ufx_reg_write(dev, 0x7014, 0x0010024F);
  306. check_warn_return(status, "error writing 0x7014");
  307. status = ufx_reg_write(dev, 0x7010, 0x00000000);
  308. check_warn_return(status, "error writing 0x7010");
  309. status = ufx_reg_clear_bits(dev, 0x700C, 0x0000000A);
  310. check_warn_return(status, "error clearing PLL1 bypass in 0x700C");
  311. msleep(1);
  312. status = ufx_reg_clear_bits(dev, 0x700C, 0x80000000);
  313. check_warn_return(status, "error clearing output gate in 0x700C");
  314. return 0;
  315. }
  316. static int ufx_config_ddr2(struct ufx_data *dev)
  317. {
  318. int status, i = 0;
  319. u32 tmp;
  320. status = ufx_reg_write(dev, 0x0004, 0x001F0F77);
  321. check_warn_return(status, "error writing 0x0004");
  322. status = ufx_reg_write(dev, 0x0008, 0xFFF00000);
  323. check_warn_return(status, "error writing 0x0008");
  324. status = ufx_reg_write(dev, 0x000C, 0x0FFF2222);
  325. check_warn_return(status, "error writing 0x000C");
  326. status = ufx_reg_write(dev, 0x0010, 0x00030814);
  327. check_warn_return(status, "error writing 0x0010");
  328. status = ufx_reg_write(dev, 0x0014, 0x00500019);
  329. check_warn_return(status, "error writing 0x0014");
  330. status = ufx_reg_write(dev, 0x0018, 0x020D0F15);
  331. check_warn_return(status, "error writing 0x0018");
  332. status = ufx_reg_write(dev, 0x001C, 0x02532305);
  333. check_warn_return(status, "error writing 0x001C");
  334. status = ufx_reg_write(dev, 0x0020, 0x0B030905);
  335. check_warn_return(status, "error writing 0x0020");
  336. status = ufx_reg_write(dev, 0x0024, 0x00000827);
  337. check_warn_return(status, "error writing 0x0024");
  338. status = ufx_reg_write(dev, 0x0028, 0x00000000);
  339. check_warn_return(status, "error writing 0x0028");
  340. status = ufx_reg_write(dev, 0x002C, 0x00000042);
  341. check_warn_return(status, "error writing 0x002C");
  342. status = ufx_reg_write(dev, 0x0030, 0x09520000);
  343. check_warn_return(status, "error writing 0x0030");
  344. status = ufx_reg_write(dev, 0x0034, 0x02223314);
  345. check_warn_return(status, "error writing 0x0034");
  346. status = ufx_reg_write(dev, 0x0038, 0x00430043);
  347. check_warn_return(status, "error writing 0x0038");
  348. status = ufx_reg_write(dev, 0x003C, 0xF00F000F);
  349. check_warn_return(status, "error writing 0x003C");
  350. status = ufx_reg_write(dev, 0x0040, 0xF380F00F);
  351. check_warn_return(status, "error writing 0x0040");
  352. status = ufx_reg_write(dev, 0x0044, 0xF00F0496);
  353. check_warn_return(status, "error writing 0x0044");
  354. status = ufx_reg_write(dev, 0x0048, 0x03080406);
  355. check_warn_return(status, "error writing 0x0048");
  356. status = ufx_reg_write(dev, 0x004C, 0x00001000);
  357. check_warn_return(status, "error writing 0x004C");
  358. status = ufx_reg_write(dev, 0x005C, 0x00000007);
  359. check_warn_return(status, "error writing 0x005C");
  360. status = ufx_reg_write(dev, 0x0100, 0x54F00012);
  361. check_warn_return(status, "error writing 0x0100");
  362. status = ufx_reg_write(dev, 0x0104, 0x00004012);
  363. check_warn_return(status, "error writing 0x0104");
  364. status = ufx_reg_write(dev, 0x0118, 0x40404040);
  365. check_warn_return(status, "error writing 0x0118");
  366. status = ufx_reg_write(dev, 0x0000, 0x00000001);
  367. check_warn_return(status, "error writing 0x0000");
  368. while (i++ < 500) {
  369. status = ufx_reg_read(dev, 0x0000, &tmp);
  370. check_warn_return(status, "error reading 0x0000");
  371. if (all_bits_set(tmp, 0xC0000000))
  372. return 0;
  373. }
  374. pr_err("DDR2 initialisation timed out, reg 0x0000=0x%08x", tmp);
  375. return -ETIMEDOUT;
  376. }
  377. struct pll_values {
  378. u32 div_r0;
  379. u32 div_f0;
  380. u32 div_q0;
  381. u32 range0;
  382. u32 div_r1;
  383. u32 div_f1;
  384. u32 div_q1;
  385. u32 range1;
  386. };
  387. static u32 ufx_calc_range(u32 ref_freq)
  388. {
  389. if (ref_freq >= 88000000)
  390. return 7;
  391. if (ref_freq >= 54000000)
  392. return 6;
  393. if (ref_freq >= 34000000)
  394. return 5;
  395. if (ref_freq >= 21000000)
  396. return 4;
  397. if (ref_freq >= 13000000)
  398. return 3;
  399. if (ref_freq >= 8000000)
  400. return 2;
  401. return 1;
  402. }
  403. /* calculates PLL divider settings for a desired target frequency */
  404. static void ufx_calc_pll_values(const u32 clk_pixel_pll, struct pll_values *asic_pll)
  405. {
  406. const u32 ref_clk = 25000000;
  407. u32 div_r0, div_f0, div_q0, div_r1, div_f1, div_q1;
  408. u32 min_error = clk_pixel_pll;
  409. for (div_r0 = 1; div_r0 <= 32; div_r0++) {
  410. u32 ref_freq0 = ref_clk / div_r0;
  411. if (ref_freq0 < 5000000)
  412. break;
  413. if (ref_freq0 > 200000000)
  414. continue;
  415. for (div_f0 = 1; div_f0 <= 256; div_f0++) {
  416. u32 vco_freq0 = ref_freq0 * div_f0;
  417. if (vco_freq0 < 350000000)
  418. continue;
  419. if (vco_freq0 > 700000000)
  420. break;
  421. for (div_q0 = 0; div_q0 < 7; div_q0++) {
  422. u32 pllout_freq0 = vco_freq0 / (1 << div_q0);
  423. if (pllout_freq0 < 5000000)
  424. break;
  425. if (pllout_freq0 > 200000000)
  426. continue;
  427. for (div_r1 = 1; div_r1 <= 32; div_r1++) {
  428. u32 ref_freq1 = pllout_freq0 / div_r1;
  429. if (ref_freq1 < 5000000)
  430. break;
  431. for (div_f1 = 1; div_f1 <= 256; div_f1++) {
  432. u32 vco_freq1 = ref_freq1 * div_f1;
  433. if (vco_freq1 < 350000000)
  434. continue;
  435. if (vco_freq1 > 700000000)
  436. break;
  437. for (div_q1 = 0; div_q1 < 7; div_q1++) {
  438. u32 pllout_freq1 = vco_freq1 / (1 << div_q1);
  439. int error = abs(pllout_freq1 - clk_pixel_pll);
  440. if (pllout_freq1 < 5000000)
  441. break;
  442. if (pllout_freq1 > 700000000)
  443. continue;
  444. if (error < min_error) {
  445. min_error = error;
  446. /* final returned value is equal to calculated value - 1
  447. * because a value of 0 = divide by 1 */
  448. asic_pll->div_r0 = div_r0 - 1;
  449. asic_pll->div_f0 = div_f0 - 1;
  450. asic_pll->div_q0 = div_q0;
  451. asic_pll->div_r1 = div_r1 - 1;
  452. asic_pll->div_f1 = div_f1 - 1;
  453. asic_pll->div_q1 = div_q1;
  454. asic_pll->range0 = ufx_calc_range(ref_freq0);
  455. asic_pll->range1 = ufx_calc_range(ref_freq1);
  456. if (min_error == 0)
  457. return;
  458. }
  459. }
  460. }
  461. }
  462. }
  463. }
  464. }
  465. }
  466. /* sets analog bit PLL configuration values */
  467. static int ufx_config_pix_clk(struct ufx_data *dev, u32 pixclock)
  468. {
  469. struct pll_values asic_pll = {0};
  470. u32 value, clk_pixel, clk_pixel_pll;
  471. int status;
  472. /* convert pixclock (in ps) to frequency (in Hz) */
  473. clk_pixel = PICOS2KHZ(pixclock) * 1000;
  474. pr_debug("pixclock %d ps = clk_pixel %d Hz", pixclock, clk_pixel);
  475. /* clk_pixel = 1/2 clk_pixel_pll */
  476. clk_pixel_pll = clk_pixel * 2;
  477. ufx_calc_pll_values(clk_pixel_pll, &asic_pll);
  478. /* Keep BYPASS and RESET signals asserted until configured */
  479. status = ufx_reg_write(dev, 0x7000, 0x8000000F);
  480. check_warn_return(status, "error writing 0x7000");
  481. value = (asic_pll.div_f1 | (asic_pll.div_r1 << 8) |
  482. (asic_pll.div_q1 << 16) | (asic_pll.range1 << 20));
  483. status = ufx_reg_write(dev, 0x7008, value);
  484. check_warn_return(status, "error writing 0x7008");
  485. value = (asic_pll.div_f0 | (asic_pll.div_r0 << 8) |
  486. (asic_pll.div_q0 << 16) | (asic_pll.range0 << 20));
  487. status = ufx_reg_write(dev, 0x7004, value);
  488. check_warn_return(status, "error writing 0x7004");
  489. status = ufx_reg_clear_bits(dev, 0x7000, 0x00000005);
  490. check_warn_return(status,
  491. "error clearing PLL0 bypass bits in 0x7000");
  492. msleep(1);
  493. status = ufx_reg_clear_bits(dev, 0x7000, 0x0000000A);
  494. check_warn_return(status,
  495. "error clearing PLL1 bypass bits in 0x7000");
  496. msleep(1);
  497. status = ufx_reg_clear_bits(dev, 0x7000, 0x80000000);
  498. check_warn_return(status, "error clearing gate bits in 0x7000");
  499. return 0;
  500. }
  501. static int ufx_set_vid_mode(struct ufx_data *dev, struct fb_var_screeninfo *var)
  502. {
  503. u32 temp;
  504. u16 h_total, h_active, h_blank_start, h_blank_end, h_sync_start, h_sync_end;
  505. u16 v_total, v_active, v_blank_start, v_blank_end, v_sync_start, v_sync_end;
  506. int status = ufx_reg_write(dev, 0x8028, 0);
  507. check_warn_return(status, "ufx_set_vid_mode error disabling RGB pad");
  508. status = ufx_reg_write(dev, 0x8024, 0);
  509. check_warn_return(status, "ufx_set_vid_mode error disabling VDAC");
  510. /* shut everything down before changing timing */
  511. status = ufx_blank(dev, true);
  512. check_warn_return(status, "ufx_set_vid_mode error blanking display");
  513. status = ufx_disable(dev, true);
  514. check_warn_return(status, "ufx_set_vid_mode error disabling display");
  515. status = ufx_config_pix_clk(dev, var->pixclock);
  516. check_warn_return(status, "ufx_set_vid_mode error configuring pixclock");
  517. status = ufx_reg_write(dev, 0x2000, 0x00000104);
  518. check_warn_return(status, "ufx_set_vid_mode error writing 0x2000");
  519. /* set horizontal timings */
  520. h_total = var->xres + var->right_margin + var->hsync_len + var->left_margin;
  521. h_active = var->xres;
  522. h_blank_start = var->xres + var->right_margin;
  523. h_blank_end = var->xres + var->right_margin + var->hsync_len;
  524. h_sync_start = var->xres + var->right_margin;
  525. h_sync_end = var->xres + var->right_margin + var->hsync_len;
  526. temp = ((h_total - 1) << 16) | (h_active - 1);
  527. status = ufx_reg_write(dev, 0x2008, temp);
  528. check_warn_return(status, "ufx_set_vid_mode error writing 0x2008");
  529. temp = ((h_blank_start - 1) << 16) | (h_blank_end - 1);
  530. status = ufx_reg_write(dev, 0x200C, temp);
  531. check_warn_return(status, "ufx_set_vid_mode error writing 0x200C");
  532. temp = ((h_sync_start - 1) << 16) | (h_sync_end - 1);
  533. status = ufx_reg_write(dev, 0x2010, temp);
  534. check_warn_return(status, "ufx_set_vid_mode error writing 0x2010");
  535. /* set vertical timings */
  536. v_total = var->upper_margin + var->yres + var->lower_margin + var->vsync_len;
  537. v_active = var->yres;
  538. v_blank_start = var->yres + var->lower_margin;
  539. v_blank_end = var->yres + var->lower_margin + var->vsync_len;
  540. v_sync_start = var->yres + var->lower_margin;
  541. v_sync_end = var->yres + var->lower_margin + var->vsync_len;
  542. temp = ((v_total - 1) << 16) | (v_active - 1);
  543. status = ufx_reg_write(dev, 0x2014, temp);
  544. check_warn_return(status, "ufx_set_vid_mode error writing 0x2014");
  545. temp = ((v_blank_start - 1) << 16) | (v_blank_end - 1);
  546. status = ufx_reg_write(dev, 0x2018, temp);
  547. check_warn_return(status, "ufx_set_vid_mode error writing 0x2018");
  548. temp = ((v_sync_start - 1) << 16) | (v_sync_end - 1);
  549. status = ufx_reg_write(dev, 0x201C, temp);
  550. check_warn_return(status, "ufx_set_vid_mode error writing 0x201C");
  551. status = ufx_reg_write(dev, 0x2020, 0x00000000);
  552. check_warn_return(status, "ufx_set_vid_mode error writing 0x2020");
  553. status = ufx_reg_write(dev, 0x2024, 0x00000000);
  554. check_warn_return(status, "ufx_set_vid_mode error writing 0x2024");
  555. /* Set the frame length register (#pix * 2 bytes/pixel) */
  556. temp = var->xres * var->yres * 2;
  557. temp = (temp + 7) & (~0x7);
  558. status = ufx_reg_write(dev, 0x2028, temp);
  559. check_warn_return(status, "ufx_set_vid_mode error writing 0x2028");
  560. /* enable desired output interface & disable others */
  561. status = ufx_reg_write(dev, 0x2040, 0);
  562. check_warn_return(status, "ufx_set_vid_mode error writing 0x2040");
  563. status = ufx_reg_write(dev, 0x2044, 0);
  564. check_warn_return(status, "ufx_set_vid_mode error writing 0x2044");
  565. status = ufx_reg_write(dev, 0x2048, 0);
  566. check_warn_return(status, "ufx_set_vid_mode error writing 0x2048");
  567. /* set the sync polarities & enable bit */
  568. temp = 0x00000001;
  569. if (var->sync & FB_SYNC_HOR_HIGH_ACT)
  570. temp |= 0x00000010;
  571. if (var->sync & FB_SYNC_VERT_HIGH_ACT)
  572. temp |= 0x00000008;
  573. status = ufx_reg_write(dev, 0x2040, temp);
  574. check_warn_return(status, "ufx_set_vid_mode error writing 0x2040");
  575. /* start everything back up */
  576. status = ufx_enable(dev, true);
  577. check_warn_return(status, "ufx_set_vid_mode error enabling display");
  578. /* Unblank the display */
  579. status = ufx_unblank(dev, true);
  580. check_warn_return(status, "ufx_set_vid_mode error unblanking display");
  581. /* enable RGB pad */
  582. status = ufx_reg_write(dev, 0x8028, 0x00000003);
  583. check_warn_return(status, "ufx_set_vid_mode error enabling RGB pad");
  584. /* enable VDAC */
  585. status = ufx_reg_write(dev, 0x8024, 0x00000007);
  586. check_warn_return(status, "ufx_set_vid_mode error enabling VDAC");
  587. return 0;
  588. }
  589. static int ufx_ops_mmap(struct fb_info *info, struct vm_area_struct *vma)
  590. {
  591. unsigned long start = vma->vm_start;
  592. unsigned long size = vma->vm_end - vma->vm_start;
  593. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  594. unsigned long page, pos;
  595. if (info->fbdefio)
  596. return fb_deferred_io_mmap(info, vma);
  597. vma->vm_page_prot = pgprot_decrypted(vma->vm_page_prot);
  598. if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT))
  599. return -EINVAL;
  600. if (size > info->fix.smem_len)
  601. return -EINVAL;
  602. if (offset > info->fix.smem_len - size)
  603. return -EINVAL;
  604. pos = (unsigned long)info->fix.smem_start + offset;
  605. pr_debug("mmap() framebuffer addr:%lu size:%lu\n",
  606. pos, size);
  607. while (size > 0) {
  608. page = vmalloc_to_pfn((void *)pos);
  609. if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED))
  610. return -EAGAIN;
  611. start += PAGE_SIZE;
  612. pos += PAGE_SIZE;
  613. if (size > PAGE_SIZE)
  614. size -= PAGE_SIZE;
  615. else
  616. size = 0;
  617. }
  618. return 0;
  619. }
  620. static void ufx_raw_rect(struct ufx_data *dev, u16 *cmd, int x, int y,
  621. int width, int height)
  622. {
  623. size_t packed_line_len = ALIGN((width * 2), 4);
  624. size_t packed_rect_len = packed_line_len * height;
  625. int line;
  626. BUG_ON(!dev);
  627. BUG_ON(!dev->info);
  628. /* command word */
  629. *((u32 *)&cmd[0]) = cpu_to_le32(0x01);
  630. /* length word */
  631. *((u32 *)&cmd[2]) = cpu_to_le32(packed_rect_len + 16);
  632. cmd[4] = cpu_to_le16(x);
  633. cmd[5] = cpu_to_le16(y);
  634. cmd[6] = cpu_to_le16(width);
  635. cmd[7] = cpu_to_le16(height);
  636. /* frame base address */
  637. *((u32 *)&cmd[8]) = cpu_to_le32(0);
  638. /* color mode and horizontal resolution */
  639. cmd[10] = cpu_to_le16(0x4000 | dev->info->var.xres);
  640. /* vertical resolution */
  641. cmd[11] = cpu_to_le16(dev->info->var.yres);
  642. /* packed data */
  643. for (line = 0; line < height; line++) {
  644. const int line_offset = dev->info->fix.line_length * (y + line);
  645. const int byte_offset = line_offset + (x * BPP);
  646. memcpy(&cmd[(24 + (packed_line_len * line)) / 2],
  647. (char *)dev->info->fix.smem_start + byte_offset, width * BPP);
  648. }
  649. }
  650. static int ufx_handle_damage(struct ufx_data *dev, int x, int y,
  651. int width, int height)
  652. {
  653. size_t packed_line_len = ALIGN((width * 2), 4);
  654. int len, status, urb_lines, start_line = 0;
  655. if ((width <= 0) || (height <= 0) ||
  656. (x + width > dev->info->var.xres) ||
  657. (y + height > dev->info->var.yres))
  658. return -EINVAL;
  659. if (!atomic_read(&dev->usb_active))
  660. return 0;
  661. while (start_line < height) {
  662. struct urb *urb = ufx_get_urb(dev);
  663. if (!urb) {
  664. pr_warn("ufx_handle_damage unable to get urb");
  665. return 0;
  666. }
  667. /* assume we have enough space to transfer at least one line */
  668. BUG_ON(urb->transfer_buffer_length < (24 + (width * 2)));
  669. /* calculate the maximum number of lines we could fit in */
  670. urb_lines = (urb->transfer_buffer_length - 24) / packed_line_len;
  671. /* but we might not need this many */
  672. urb_lines = min(urb_lines, (height - start_line));
  673. memset(urb->transfer_buffer, 0, urb->transfer_buffer_length);
  674. ufx_raw_rect(dev, urb->transfer_buffer, x, (y + start_line), width, urb_lines);
  675. len = 24 + (packed_line_len * urb_lines);
  676. status = ufx_submit_urb(dev, urb, len);
  677. check_warn_return(status, "Error submitting URB");
  678. start_line += urb_lines;
  679. }
  680. return 0;
  681. }
  682. /* NOTE: fb_defio.c is holding info->fbdefio.mutex
  683. * Touching ANY framebuffer memory that triggers a page fault
  684. * in fb_defio will cause a deadlock, when it also tries to
  685. * grab the same mutex. */
  686. static void ufx_dpy_deferred_io(struct fb_info *info, struct list_head *pagereflist)
  687. {
  688. struct ufx_data *dev = info->par;
  689. struct fb_deferred_io_pageref *pageref;
  690. if (!fb_defio)
  691. return;
  692. if (!atomic_read(&dev->usb_active))
  693. return;
  694. /* walk the written page list and render each to device */
  695. list_for_each_entry(pageref, pagereflist, list) {
  696. /* create a rectangle of full screen width that encloses the
  697. * entire dirty framebuffer page */
  698. const int x = 0;
  699. const int width = dev->info->var.xres;
  700. const int y = pageref->offset / (width * 2);
  701. int height = (PAGE_SIZE / (width * 2)) + 1;
  702. height = min(height, (int)(dev->info->var.yres - y));
  703. BUG_ON(y >= dev->info->var.yres);
  704. BUG_ON((y + height) > dev->info->var.yres);
  705. ufx_handle_damage(dev, x, y, width, height);
  706. }
  707. }
  708. static int ufx_ops_ioctl(struct fb_info *info, unsigned int cmd,
  709. unsigned long arg)
  710. {
  711. struct ufx_data *dev = info->par;
  712. if (!atomic_read(&dev->usb_active))
  713. return 0;
  714. /* TODO: Update X server to get this from sysfs instead */
  715. if (cmd == UFX_IOCTL_RETURN_EDID) {
  716. u8 __user *edid = (u8 __user *)arg;
  717. if (copy_to_user(edid, dev->edid, dev->edid_size))
  718. return -EFAULT;
  719. return 0;
  720. }
  721. /* TODO: Help propose a standard fb.h ioctl to report mmap damage */
  722. if (cmd == UFX_IOCTL_REPORT_DAMAGE) {
  723. struct dloarea *area __free(kfree) = kmalloc_obj(*area);
  724. if (!area)
  725. return -ENOMEM;
  726. /* If we have a damage-aware client, turn fb_defio "off"
  727. * To avoid perf imact of unnecessary page fault handling.
  728. * Done by resetting the delay for this fb_info to a very
  729. * long period. Pages will become writable and stay that way.
  730. * Reset to normal value when all clients have closed this fb.
  731. */
  732. if (info->fbdefio)
  733. info->fbdefio->delay = UFX_DEFIO_WRITE_DISABLE;
  734. if (copy_from_user(area, (u8 __user *)arg, sizeof(*area)))
  735. return -EFAULT;
  736. if (area->x < 0)
  737. area->x = 0;
  738. if (area->x > info->var.xres)
  739. area->x = info->var.xres;
  740. if (area->y < 0)
  741. area->y = 0;
  742. if (area->y > info->var.yres)
  743. area->y = info->var.yres;
  744. ufx_handle_damage(dev, area->x, area->y, area->w, area->h);
  745. }
  746. return 0;
  747. }
  748. /* taken from vesafb */
  749. static int
  750. ufx_ops_setcolreg(unsigned regno, unsigned red, unsigned green,
  751. unsigned blue, unsigned transp, struct fb_info *info)
  752. {
  753. int err = 0;
  754. if (regno >= info->cmap.len)
  755. return 1;
  756. if (regno < 16) {
  757. if (info->var.red.offset == 10) {
  758. /* 1:5:5:5 */
  759. ((u32 *) (info->pseudo_palette))[regno] =
  760. ((red & 0xf800) >> 1) |
  761. ((green & 0xf800) >> 6) | ((blue & 0xf800) >> 11);
  762. } else {
  763. /* 0:5:6:5 */
  764. ((u32 *) (info->pseudo_palette))[regno] =
  765. ((red & 0xf800)) |
  766. ((green & 0xfc00) >> 5) | ((blue & 0xf800) >> 11);
  767. }
  768. }
  769. return err;
  770. }
  771. /* It's common for several clients to have framebuffer open simultaneously.
  772. * e.g. both fbcon and X. Makes things interesting.
  773. * Assumes caller is holding info->lock (for open and release at least) */
  774. static int ufx_ops_open(struct fb_info *info, int user)
  775. {
  776. struct ufx_data *dev = info->par;
  777. /* fbcon aggressively connects to first framebuffer it finds,
  778. * preventing other clients (X) from working properly. Usually
  779. * not what the user wants. Fail by default with option to enable. */
  780. if (user == 0 && !console)
  781. return -EBUSY;
  782. mutex_lock(&disconnect_mutex);
  783. /* If the USB device is gone, we don't accept new opens */
  784. if (dev->virtualized) {
  785. mutex_unlock(&disconnect_mutex);
  786. return -ENODEV;
  787. }
  788. dev->fb_count++;
  789. kref_get(&dev->kref);
  790. if (fb_defio && (info->fbdefio == NULL)) {
  791. /* enable defio at last moment if not disabled by client */
  792. struct fb_deferred_io *fbdefio;
  793. fbdefio = kzalloc_obj(*fbdefio);
  794. if (fbdefio) {
  795. fbdefio->delay = UFX_DEFIO_WRITE_DELAY;
  796. fbdefio->deferred_io = ufx_dpy_deferred_io;
  797. }
  798. info->fbdefio = fbdefio;
  799. fb_deferred_io_init(info);
  800. }
  801. pr_debug("open /dev/fb%d user=%d fb_info=%p count=%d",
  802. info->node, user, info, dev->fb_count);
  803. mutex_unlock(&disconnect_mutex);
  804. return 0;
  805. }
  806. /*
  807. * Called when all client interfaces to start transactions have been disabled,
  808. * and all references to our device instance (ufx_data) are released.
  809. * Every transaction must have a reference, so we know are fully spun down
  810. */
  811. static void ufx_free(struct kref *kref)
  812. {
  813. struct ufx_data *dev = container_of(kref, struct ufx_data, kref);
  814. kfree(dev);
  815. }
  816. static void ufx_ops_destory(struct fb_info *info)
  817. {
  818. struct ufx_data *dev = info->par;
  819. int node = info->node;
  820. /* Assume info structure is freed after this point */
  821. framebuffer_release(info);
  822. pr_debug("fb_info for /dev/fb%d has been freed", node);
  823. /* release reference taken by kref_init in probe() */
  824. kref_put(&dev->kref, ufx_free);
  825. }
  826. static void ufx_release_urb_work(struct work_struct *work)
  827. {
  828. struct urb_node *unode = container_of(work, struct urb_node,
  829. release_urb_work.work);
  830. up(&unode->dev->urbs.limit_sem);
  831. }
  832. static void ufx_free_framebuffer(struct ufx_data *dev)
  833. {
  834. struct fb_info *info = dev->info;
  835. if (info->cmap.len != 0)
  836. fb_dealloc_cmap(&info->cmap);
  837. if (info->monspecs.modedb)
  838. fb_destroy_modedb(info->monspecs.modedb);
  839. vfree(info->screen_buffer);
  840. fb_destroy_modelist(&info->modelist);
  841. dev->info = NULL;
  842. /* ref taken in probe() as part of registering framebfufer */
  843. kref_put(&dev->kref, ufx_free);
  844. }
  845. /*
  846. * Assumes caller is holding info->lock mutex (for open and release at least)
  847. */
  848. static int ufx_ops_release(struct fb_info *info, int user)
  849. {
  850. struct ufx_data *dev = info->par;
  851. mutex_lock(&disconnect_mutex);
  852. dev->fb_count--;
  853. /* We can't free fb_info here - fbmem will touch it when we return */
  854. if (dev->virtualized && (dev->fb_count == 0))
  855. ufx_free_framebuffer(dev);
  856. if ((dev->fb_count == 0) && (info->fbdefio)) {
  857. fb_deferred_io_cleanup(info);
  858. kfree(info->fbdefio);
  859. info->fbdefio = NULL;
  860. }
  861. pr_debug("released /dev/fb%d user=%d count=%d",
  862. info->node, user, dev->fb_count);
  863. kref_put(&dev->kref, ufx_free);
  864. mutex_unlock(&disconnect_mutex);
  865. return 0;
  866. }
  867. /* Check whether a video mode is supported by the chip
  868. * We start from monitor's modes, so don't need to filter that here */
  869. static int ufx_is_valid_mode(struct fb_videomode *mode,
  870. struct fb_info *info)
  871. {
  872. if ((mode->xres * mode->yres) > (2048 * 1152)) {
  873. pr_debug("%dx%d too many pixels",
  874. mode->xres, mode->yres);
  875. return 0;
  876. }
  877. if (mode->pixclock < 5000) {
  878. pr_debug("%dx%d %dps pixel clock too fast",
  879. mode->xres, mode->yres, mode->pixclock);
  880. return 0;
  881. }
  882. pr_debug("%dx%d (pixclk %dps %dMHz) valid mode", mode->xres, mode->yres,
  883. mode->pixclock, (1000000 / mode->pixclock));
  884. return 1;
  885. }
  886. static void ufx_var_color_format(struct fb_var_screeninfo *var)
  887. {
  888. const struct fb_bitfield red = { 11, 5, 0 };
  889. const struct fb_bitfield green = { 5, 6, 0 };
  890. const struct fb_bitfield blue = { 0, 5, 0 };
  891. var->bits_per_pixel = 16;
  892. var->red = red;
  893. var->green = green;
  894. var->blue = blue;
  895. }
  896. static int ufx_ops_check_var(struct fb_var_screeninfo *var,
  897. struct fb_info *info)
  898. {
  899. struct fb_videomode mode;
  900. /* TODO: support dynamically changing framebuffer size */
  901. if ((var->xres * var->yres * 2) > info->fix.smem_len)
  902. return -EINVAL;
  903. /* set device-specific elements of var unrelated to mode */
  904. ufx_var_color_format(var);
  905. fb_var_to_videomode(&mode, var);
  906. if (!ufx_is_valid_mode(&mode, info))
  907. return -EINVAL;
  908. return 0;
  909. }
  910. static int ufx_ops_set_par(struct fb_info *info)
  911. {
  912. struct ufx_data *dev = info->par;
  913. int result;
  914. u16 *pix_framebuffer;
  915. int i;
  916. pr_debug("set_par mode %dx%d", info->var.xres, info->var.yres);
  917. result = ufx_set_vid_mode(dev, &info->var);
  918. if ((result == 0) && (dev->fb_count == 0)) {
  919. /* paint greenscreen */
  920. pix_framebuffer = (u16 *)info->screen_buffer;
  921. for (i = 0; i < info->fix.smem_len / 2; i++)
  922. pix_framebuffer[i] = 0x37e6;
  923. ufx_handle_damage(dev, 0, 0, info->var.xres, info->var.yres);
  924. }
  925. /* re-enable defio if previously disabled by damage tracking */
  926. if (info->fbdefio)
  927. info->fbdefio->delay = UFX_DEFIO_WRITE_DELAY;
  928. return result;
  929. }
  930. /* In order to come back from full DPMS off, we need to set the mode again */
  931. static int ufx_ops_blank(int blank_mode, struct fb_info *info)
  932. {
  933. struct ufx_data *dev = info->par;
  934. ufx_set_vid_mode(dev, &info->var);
  935. return 0;
  936. }
  937. static void ufx_ops_damage_range(struct fb_info *info, off_t off, size_t len)
  938. {
  939. struct ufx_data *dev = info->par;
  940. int start = max((int)(off / info->fix.line_length), 0);
  941. int lines = min((u32)((len / info->fix.line_length) + 1), (u32)info->var.yres);
  942. ufx_handle_damage(dev, 0, start, info->var.xres, lines);
  943. }
  944. static void ufx_ops_damage_area(struct fb_info *info, u32 x, u32 y, u32 width, u32 height)
  945. {
  946. struct ufx_data *dev = info->par;
  947. ufx_handle_damage(dev, x, y, width, height);
  948. }
  949. FB_GEN_DEFAULT_DEFERRED_SYSMEM_OPS(ufx_ops,
  950. ufx_ops_damage_range,
  951. ufx_ops_damage_area)
  952. static const struct fb_ops ufx_ops = {
  953. .owner = THIS_MODULE,
  954. __FB_DEFAULT_DEFERRED_OPS_RDWR(ufx_ops),
  955. .fb_setcolreg = ufx_ops_setcolreg,
  956. __FB_DEFAULT_DEFERRED_OPS_DRAW(ufx_ops),
  957. .fb_mmap = ufx_ops_mmap,
  958. .fb_ioctl = ufx_ops_ioctl,
  959. .fb_open = ufx_ops_open,
  960. .fb_release = ufx_ops_release,
  961. .fb_blank = ufx_ops_blank,
  962. .fb_check_var = ufx_ops_check_var,
  963. .fb_set_par = ufx_ops_set_par,
  964. .fb_destroy = ufx_ops_destory,
  965. };
  966. /* Assumes &info->lock held by caller
  967. * Assumes no active clients have framebuffer open */
  968. static int ufx_realloc_framebuffer(struct ufx_data *dev, struct fb_info *info)
  969. {
  970. int old_len = info->fix.smem_len;
  971. int new_len;
  972. unsigned char *old_fb = info->screen_buffer;
  973. unsigned char *new_fb;
  974. pr_debug("Reallocating framebuffer. Addresses will change!");
  975. new_len = info->fix.line_length * info->var.yres;
  976. if (PAGE_ALIGN(new_len) > old_len) {
  977. /*
  978. * Alloc system memory for virtual framebuffer
  979. */
  980. new_fb = vmalloc(new_len);
  981. if (!new_fb)
  982. return -ENOMEM;
  983. if (info->screen_buffer) {
  984. memcpy(new_fb, old_fb, old_len);
  985. vfree(info->screen_buffer);
  986. }
  987. info->screen_buffer = new_fb;
  988. info->fix.smem_len = PAGE_ALIGN(new_len);
  989. info->fix.smem_start = (unsigned long) new_fb;
  990. info->flags = smscufx_info_flags;
  991. }
  992. return 0;
  993. }
  994. /* sets up DDC channel for 100 Kbps, std. speed, 7-bit addr, controller mode,
  995. * restart enabled, but no start byte, enable controller */
  996. static int ufx_i2c_init(struct ufx_data *dev)
  997. {
  998. u32 tmp;
  999. /* disable the controller before it can be reprogrammed */
  1000. int status = ufx_reg_write(dev, 0x106C, 0x00);
  1001. check_warn_return(status, "failed to disable I2C");
  1002. /* Setup the clock count registers
  1003. * (12+1) = 13 clks @ 2.5 MHz = 5.2 uS */
  1004. status = ufx_reg_write(dev, 0x1018, 12);
  1005. check_warn_return(status, "error writing 0x1018");
  1006. /* (6+8) = 14 clks @ 2.5 MHz = 5.6 uS */
  1007. status = ufx_reg_write(dev, 0x1014, 6);
  1008. check_warn_return(status, "error writing 0x1014");
  1009. status = ufx_reg_read(dev, 0x1000, &tmp);
  1010. check_warn_return(status, "error reading 0x1000");
  1011. /* set speed to std mode */
  1012. tmp &= ~(0x06);
  1013. tmp |= 0x02;
  1014. /* 7-bit (not 10-bit) addressing */
  1015. tmp &= ~(0x10);
  1016. /* enable restart conditions and controller mode */
  1017. tmp |= 0x21;
  1018. status = ufx_reg_write(dev, 0x1000, tmp);
  1019. check_warn_return(status, "error writing 0x1000");
  1020. /* Set normal tx using target address 0 */
  1021. status = ufx_reg_clear_and_set_bits(dev, 0x1004, 0xC00, 0x000);
  1022. check_warn_return(status, "error setting TX mode bits in 0x1004");
  1023. /* Enable the controller */
  1024. status = ufx_reg_write(dev, 0x106C, 0x01);
  1025. check_warn_return(status, "failed to enable I2C");
  1026. return 0;
  1027. }
  1028. /* sets the I2C port mux and target address */
  1029. static int ufx_i2c_configure(struct ufx_data *dev)
  1030. {
  1031. int status = ufx_reg_write(dev, 0x106C, 0x00);
  1032. check_warn_return(status, "failed to disable I2C");
  1033. status = ufx_reg_write(dev, 0x3010, 0x00000000);
  1034. check_warn_return(status, "failed to write 0x3010");
  1035. /* A0h is std for any EDID, right shifted by one */
  1036. status = ufx_reg_clear_and_set_bits(dev, 0x1004, 0x3FF, (0xA0 >> 1));
  1037. check_warn_return(status, "failed to set TAR bits in 0x1004");
  1038. status = ufx_reg_write(dev, 0x106C, 0x01);
  1039. check_warn_return(status, "failed to enable I2C");
  1040. return 0;
  1041. }
  1042. /* wait for BUSY to clear, with a timeout of 50ms with 10ms sleeps. if no
  1043. * monitor is connected, there is no error except for timeout */
  1044. static int ufx_i2c_wait_busy(struct ufx_data *dev)
  1045. {
  1046. u32 tmp;
  1047. int i, status;
  1048. for (i = 0; i < 15; i++) {
  1049. status = ufx_reg_read(dev, 0x1100, &tmp);
  1050. check_warn_return(status, "0x1100 read failed");
  1051. /* if BUSY is clear, check for error */
  1052. if ((tmp & 0x80000000) == 0) {
  1053. if (tmp & 0x20000000) {
  1054. pr_warn("I2C read failed, 0x1100=0x%08x", tmp);
  1055. return -EIO;
  1056. }
  1057. return 0;
  1058. }
  1059. /* perform the first 10 retries without delay */
  1060. if (i >= 10)
  1061. msleep(10);
  1062. }
  1063. pr_warn("I2C access timed out, resetting I2C hardware");
  1064. status = ufx_reg_write(dev, 0x1100, 0x40000000);
  1065. check_warn_return(status, "0x1100 write failed");
  1066. return -ETIMEDOUT;
  1067. }
  1068. /* reads a 128-byte EDID block from the currently selected port and TAR */
  1069. static int ufx_read_edid(struct ufx_data *dev, u8 *edid, int edid_len)
  1070. {
  1071. int i, j, status;
  1072. u32 *edid_u32 = (u32 *)edid;
  1073. BUG_ON(edid_len != EDID_LENGTH);
  1074. status = ufx_i2c_configure(dev);
  1075. if (status < 0) {
  1076. pr_err("ufx_i2c_configure failed");
  1077. return status;
  1078. }
  1079. memset(edid, 0xff, EDID_LENGTH);
  1080. /* Read the 128-byte EDID as 2 bursts of 64 bytes */
  1081. for (i = 0; i < 2; i++) {
  1082. u32 temp = 0x28070000 | (63 << 20) | (((u32)(i * 64)) << 8);
  1083. status = ufx_reg_write(dev, 0x1100, temp);
  1084. check_warn_return(status, "Failed to write 0x1100");
  1085. temp |= 0x80000000;
  1086. status = ufx_reg_write(dev, 0x1100, temp);
  1087. check_warn_return(status, "Failed to write 0x1100");
  1088. status = ufx_i2c_wait_busy(dev);
  1089. check_warn_return(status, "Timeout waiting for I2C BUSY to clear");
  1090. for (j = 0; j < 16; j++) {
  1091. u32 data_reg_addr = 0x1110 + (j * 4);
  1092. status = ufx_reg_read(dev, data_reg_addr, edid_u32++);
  1093. check_warn_return(status, "Error reading i2c data");
  1094. }
  1095. }
  1096. /* all FF's in the first 16 bytes indicates nothing is connected */
  1097. for (i = 0; i < 16; i++) {
  1098. if (edid[i] != 0xFF) {
  1099. pr_debug("edid data read successfully");
  1100. return EDID_LENGTH;
  1101. }
  1102. }
  1103. pr_warn("edid data contains all 0xff");
  1104. return -ETIMEDOUT;
  1105. }
  1106. /* 1) use sw default
  1107. * 2) Parse into various fb_info structs
  1108. * 3) Allocate virtual framebuffer memory to back highest res mode
  1109. *
  1110. * Parses EDID into three places used by various parts of fbdev:
  1111. * fb_var_screeninfo contains the timing of the monitor's preferred mode
  1112. * fb_info.monspecs is full parsed EDID info, including monspecs.modedb
  1113. * fb_info.modelist is a linked list of all monitor & VESA modes which work
  1114. *
  1115. * If EDID is not readable/valid, then modelist is all VESA modes,
  1116. * monspecs is NULL, and fb_var_screeninfo is set to safe VESA mode
  1117. * Returns 0 if successful */
  1118. static int ufx_setup_modes(struct ufx_data *dev, struct fb_info *info,
  1119. char *default_edid, size_t default_edid_size)
  1120. {
  1121. const struct fb_videomode *default_vmode = NULL;
  1122. u8 *edid;
  1123. int i, result = 0, tries = 3;
  1124. if (refcount_read(&info->count)) /* only use mutex if info has been registered */
  1125. mutex_lock(&info->lock);
  1126. edid = kmalloc(EDID_LENGTH, GFP_KERNEL);
  1127. if (!edid) {
  1128. result = -ENOMEM;
  1129. goto error;
  1130. }
  1131. fb_destroy_modelist(&info->modelist);
  1132. memset(&info->monspecs, 0, sizeof(info->monspecs));
  1133. /* Try to (re)read EDID from hardware first
  1134. * EDID data may return, but not parse as valid
  1135. * Try again a few times, in case of e.g. analog cable noise */
  1136. while (tries--) {
  1137. i = ufx_read_edid(dev, edid, EDID_LENGTH);
  1138. if (i >= EDID_LENGTH)
  1139. fb_edid_to_monspecs(edid, &info->monspecs);
  1140. if (info->monspecs.modedb_len > 0) {
  1141. dev->edid = edid;
  1142. dev->edid_size = i;
  1143. break;
  1144. }
  1145. }
  1146. /* If that fails, use a previously returned EDID if available */
  1147. if (info->monspecs.modedb_len == 0) {
  1148. pr_err("Unable to get valid EDID from device/display\n");
  1149. if (dev->edid) {
  1150. fb_edid_to_monspecs(dev->edid, &info->monspecs);
  1151. if (info->monspecs.modedb_len > 0)
  1152. pr_err("Using previously queried EDID\n");
  1153. }
  1154. }
  1155. /* If that fails, use the default EDID we were handed */
  1156. if (info->monspecs.modedb_len == 0) {
  1157. if (default_edid_size >= EDID_LENGTH) {
  1158. fb_edid_to_monspecs(default_edid, &info->monspecs);
  1159. if (info->monspecs.modedb_len > 0) {
  1160. memcpy(edid, default_edid, default_edid_size);
  1161. dev->edid = edid;
  1162. dev->edid_size = default_edid_size;
  1163. pr_err("Using default/backup EDID\n");
  1164. }
  1165. }
  1166. }
  1167. /* If we've got modes, let's pick a best default mode */
  1168. if (info->monspecs.modedb_len > 0) {
  1169. for (i = 0; i < info->monspecs.modedb_len; i++) {
  1170. if (ufx_is_valid_mode(&info->monspecs.modedb[i], info))
  1171. fb_add_videomode(&info->monspecs.modedb[i],
  1172. &info->modelist);
  1173. else /* if we've removed top/best mode */
  1174. info->monspecs.misc &= ~FB_MISC_1ST_DETAIL;
  1175. }
  1176. default_vmode = fb_find_best_display(&info->monspecs,
  1177. &info->modelist);
  1178. }
  1179. /* If everything else has failed, fall back to safe default mode */
  1180. if (default_vmode == NULL) {
  1181. struct fb_videomode fb_vmode = {0};
  1182. /* Add the standard VESA modes to our modelist
  1183. * Since we don't have EDID, there may be modes that
  1184. * overspec monitor and/or are incorrect aspect ratio, etc.
  1185. * But at least the user has a chance to choose
  1186. */
  1187. for (i = 0; i < VESA_MODEDB_SIZE; i++) {
  1188. if (ufx_is_valid_mode((struct fb_videomode *)
  1189. &vesa_modes[i], info))
  1190. fb_add_videomode(&vesa_modes[i],
  1191. &info->modelist);
  1192. }
  1193. /* default to resolution safe for projectors
  1194. * (since they are most common case without EDID)
  1195. */
  1196. fb_vmode.xres = 800;
  1197. fb_vmode.yres = 600;
  1198. fb_vmode.refresh = 60;
  1199. default_vmode = fb_find_nearest_mode(&fb_vmode,
  1200. &info->modelist);
  1201. }
  1202. /* If we have good mode and no active clients */
  1203. if ((default_vmode != NULL) && (dev->fb_count == 0)) {
  1204. fb_videomode_to_var(&info->var, default_vmode);
  1205. ufx_var_color_format(&info->var);
  1206. /* with mode size info, we can now alloc our framebuffer */
  1207. memcpy(&info->fix, &ufx_fix, sizeof(ufx_fix));
  1208. info->fix.line_length = info->var.xres *
  1209. (info->var.bits_per_pixel / 8);
  1210. result = ufx_realloc_framebuffer(dev, info);
  1211. } else
  1212. result = -EINVAL;
  1213. error:
  1214. if (edid && (dev->edid != edid))
  1215. kfree(edid);
  1216. if (refcount_read(&info->count))
  1217. mutex_unlock(&info->lock);
  1218. return result;
  1219. }
  1220. static int ufx_usb_probe(struct usb_interface *interface,
  1221. const struct usb_device_id *id)
  1222. {
  1223. struct usb_device *usbdev;
  1224. struct ufx_data *dev;
  1225. struct fb_info *info;
  1226. int retval = -ENOMEM;
  1227. u32 id_rev, fpga_rev;
  1228. /* usb initialization */
  1229. usbdev = interface_to_usbdev(interface);
  1230. BUG_ON(!usbdev);
  1231. dev = kzalloc_obj(*dev);
  1232. if (dev == NULL) {
  1233. dev_err(&usbdev->dev, "ufx_usb_probe: failed alloc of dev struct\n");
  1234. return -ENOMEM;
  1235. }
  1236. /* we need to wait for both usb and fbdev to spin down on disconnect */
  1237. kref_init(&dev->kref); /* matching kref_put in usb .disconnect fn */
  1238. kref_get(&dev->kref); /* matching kref_put in free_framebuffer_work */
  1239. dev->udev = usbdev;
  1240. dev->gdev = &usbdev->dev; /* our generic struct device * */
  1241. usb_set_intfdata(interface, dev);
  1242. dev_dbg(dev->gdev, "%s %s - serial #%s\n",
  1243. usbdev->manufacturer, usbdev->product, usbdev->serial);
  1244. dev_dbg(dev->gdev, "vid_%04x&pid_%04x&rev_%04x driver's ufx_data struct at %p\n",
  1245. le16_to_cpu(usbdev->descriptor.idVendor),
  1246. le16_to_cpu(usbdev->descriptor.idProduct),
  1247. le16_to_cpu(usbdev->descriptor.bcdDevice), dev);
  1248. dev_dbg(dev->gdev, "console enable=%d\n", console);
  1249. dev_dbg(dev->gdev, "fb_defio enable=%d\n", fb_defio);
  1250. if (!ufx_alloc_urb_list(dev, WRITES_IN_FLIGHT, MAX_TRANSFER)) {
  1251. dev_err(dev->gdev, "ufx_alloc_urb_list failed\n");
  1252. goto put_ref;
  1253. }
  1254. /* We don't register a new USB class. Our client interface is fbdev */
  1255. /* allocates framebuffer driver structure, not framebuffer memory */
  1256. info = framebuffer_alloc(0, &usbdev->dev);
  1257. if (!info) {
  1258. dev_err(dev->gdev, "framebuffer_alloc failed\n");
  1259. goto free_urb_list;
  1260. }
  1261. dev->info = info;
  1262. info->par = dev;
  1263. info->pseudo_palette = dev->pseudo_palette;
  1264. info->fbops = &ufx_ops;
  1265. INIT_LIST_HEAD(&info->modelist);
  1266. retval = fb_alloc_cmap(&info->cmap, 256, 0);
  1267. if (retval < 0) {
  1268. dev_err(dev->gdev, "fb_alloc_cmap failed %x\n", retval);
  1269. goto destroy_modedb;
  1270. }
  1271. retval = ufx_reg_read(dev, 0x3000, &id_rev);
  1272. check_warn_goto_error(retval, "error %d reading 0x3000 register from device", retval);
  1273. dev_dbg(dev->gdev, "ID_REV register value 0x%08x", id_rev);
  1274. retval = ufx_reg_read(dev, 0x3004, &fpga_rev);
  1275. check_warn_goto_error(retval, "error %d reading 0x3004 register from device", retval);
  1276. dev_dbg(dev->gdev, "FPGA_REV register value 0x%08x", fpga_rev);
  1277. dev_dbg(dev->gdev, "resetting device");
  1278. retval = ufx_lite_reset(dev);
  1279. check_warn_goto_error(retval, "error %d resetting device", retval);
  1280. dev_dbg(dev->gdev, "configuring system clock");
  1281. retval = ufx_config_sys_clk(dev);
  1282. check_warn_goto_error(retval, "error %d configuring system clock", retval);
  1283. dev_dbg(dev->gdev, "configuring DDR2 controller");
  1284. retval = ufx_config_ddr2(dev);
  1285. check_warn_goto_error(retval, "error %d initialising DDR2 controller", retval);
  1286. dev_dbg(dev->gdev, "configuring I2C controller");
  1287. retval = ufx_i2c_init(dev);
  1288. check_warn_goto_error(retval, "error %d initialising I2C controller", retval);
  1289. dev_dbg(dev->gdev, "selecting display mode");
  1290. retval = ufx_setup_modes(dev, info, NULL, 0);
  1291. check_warn_goto_error(retval, "unable to find common mode for display and adapter");
  1292. retval = ufx_reg_set_bits(dev, 0x4000, 0x00000001);
  1293. if (retval < 0) {
  1294. dev_err(dev->gdev, "error %d enabling graphics engine", retval);
  1295. goto setup_modes;
  1296. }
  1297. /* ready to begin using device */
  1298. atomic_set(&dev->usb_active, 1);
  1299. dev_dbg(dev->gdev, "checking var");
  1300. retval = ufx_ops_check_var(&info->var, info);
  1301. if (retval < 0) {
  1302. dev_err(dev->gdev, "error %d ufx_ops_check_var", retval);
  1303. goto reset_active;
  1304. }
  1305. dev_dbg(dev->gdev, "setting par");
  1306. retval = ufx_ops_set_par(info);
  1307. if (retval < 0) {
  1308. dev_err(dev->gdev, "error %d ufx_ops_set_par", retval);
  1309. goto reset_active;
  1310. }
  1311. dev_dbg(dev->gdev, "registering framebuffer");
  1312. retval = register_framebuffer(info);
  1313. if (retval < 0) {
  1314. dev_err(dev->gdev, "error %d register_framebuffer", retval);
  1315. goto reset_active;
  1316. }
  1317. dev_info(dev->gdev, "SMSC UDX USB device /dev/fb%d attached. %dx%d resolution."
  1318. " Using %dK framebuffer memory\n", info->node,
  1319. info->var.xres, info->var.yres, info->fix.smem_len >> 10);
  1320. return 0;
  1321. reset_active:
  1322. atomic_set(&dev->usb_active, 0);
  1323. setup_modes:
  1324. fb_destroy_modedb(info->monspecs.modedb);
  1325. vfree(info->screen_buffer);
  1326. fb_destroy_modelist(&info->modelist);
  1327. error:
  1328. fb_dealloc_cmap(&info->cmap);
  1329. destroy_modedb:
  1330. framebuffer_release(info);
  1331. free_urb_list:
  1332. if (dev->urbs.count > 0)
  1333. ufx_free_urb_list(dev);
  1334. put_ref:
  1335. kref_put(&dev->kref, ufx_free); /* ref for framebuffer */
  1336. kref_put(&dev->kref, ufx_free); /* last ref from kref_init */
  1337. return retval;
  1338. }
  1339. static void ufx_usb_disconnect(struct usb_interface *interface)
  1340. {
  1341. struct ufx_data *dev;
  1342. struct fb_info *info;
  1343. mutex_lock(&disconnect_mutex);
  1344. dev = usb_get_intfdata(interface);
  1345. info = dev->info;
  1346. pr_debug("USB disconnect starting\n");
  1347. /* we virtualize until all fb clients release. Then we free */
  1348. dev->virtualized = true;
  1349. /* When non-active we'll update virtual framebuffer, but no new urbs */
  1350. atomic_set(&dev->usb_active, 0);
  1351. usb_set_intfdata(interface, NULL);
  1352. /* if clients still have us open, will be freed on last close */
  1353. if (dev->fb_count == 0)
  1354. ufx_free_framebuffer(dev);
  1355. /* this function will wait for all in-flight urbs to complete */
  1356. if (dev->urbs.count > 0)
  1357. ufx_free_urb_list(dev);
  1358. pr_debug("freeing ufx_data %p", dev);
  1359. unregister_framebuffer(info);
  1360. mutex_unlock(&disconnect_mutex);
  1361. }
  1362. static struct usb_driver ufx_driver = {
  1363. .name = "smscufx",
  1364. .probe = ufx_usb_probe,
  1365. .disconnect = ufx_usb_disconnect,
  1366. .id_table = id_table,
  1367. };
  1368. module_usb_driver(ufx_driver);
  1369. static void ufx_urb_completion(struct urb *urb)
  1370. {
  1371. struct urb_node *unode = urb->context;
  1372. struct ufx_data *dev = unode->dev;
  1373. unsigned long flags;
  1374. /* sync/async unlink faults aren't errors */
  1375. if (urb->status) {
  1376. if (!(urb->status == -ENOENT ||
  1377. urb->status == -ECONNRESET ||
  1378. urb->status == -ESHUTDOWN)) {
  1379. pr_err("%s - nonzero write bulk status received: %d\n",
  1380. __func__, urb->status);
  1381. atomic_set(&dev->lost_pixels, 1);
  1382. }
  1383. }
  1384. urb->transfer_buffer_length = dev->urbs.size; /* reset to actual */
  1385. spin_lock_irqsave(&dev->urbs.lock, flags);
  1386. list_add_tail(&unode->entry, &dev->urbs.list);
  1387. dev->urbs.available++;
  1388. spin_unlock_irqrestore(&dev->urbs.lock, flags);
  1389. /* When using fb_defio, we deadlock if up() is called
  1390. * while another is waiting. So queue to another process */
  1391. if (fb_defio)
  1392. schedule_delayed_work(&unode->release_urb_work, 0);
  1393. else
  1394. up(&dev->urbs.limit_sem);
  1395. }
  1396. static void ufx_free_urb_list(struct ufx_data *dev)
  1397. {
  1398. int count = dev->urbs.count;
  1399. struct list_head *node;
  1400. struct urb_node *unode;
  1401. struct urb *urb;
  1402. int ret;
  1403. unsigned long flags;
  1404. pr_debug("Waiting for completes and freeing all render urbs\n");
  1405. /* keep waiting and freeing, until we've got 'em all */
  1406. while (count--) {
  1407. /* Getting interrupted means a leak, but ok at shutdown*/
  1408. ret = down_interruptible(&dev->urbs.limit_sem);
  1409. if (ret)
  1410. break;
  1411. spin_lock_irqsave(&dev->urbs.lock, flags);
  1412. node = dev->urbs.list.next; /* have reserved one with sem */
  1413. list_del_init(node);
  1414. spin_unlock_irqrestore(&dev->urbs.lock, flags);
  1415. unode = list_entry(node, struct urb_node, entry);
  1416. urb = unode->urb;
  1417. /* Free each separately allocated piece */
  1418. usb_free_coherent(urb->dev, dev->urbs.size,
  1419. urb->transfer_buffer, urb->transfer_dma);
  1420. usb_free_urb(urb);
  1421. kfree(node);
  1422. }
  1423. }
  1424. static int ufx_alloc_urb_list(struct ufx_data *dev, int count, size_t size)
  1425. {
  1426. int i = 0;
  1427. struct urb *urb;
  1428. struct urb_node *unode;
  1429. char *buf;
  1430. spin_lock_init(&dev->urbs.lock);
  1431. dev->urbs.size = size;
  1432. INIT_LIST_HEAD(&dev->urbs.list);
  1433. while (i < count) {
  1434. unode = kzalloc_obj(*unode);
  1435. if (!unode)
  1436. break;
  1437. unode->dev = dev;
  1438. INIT_DELAYED_WORK(&unode->release_urb_work,
  1439. ufx_release_urb_work);
  1440. urb = usb_alloc_urb(0, GFP_KERNEL);
  1441. if (!urb) {
  1442. kfree(unode);
  1443. break;
  1444. }
  1445. unode->urb = urb;
  1446. buf = usb_alloc_coherent(dev->udev, size, GFP_KERNEL,
  1447. &urb->transfer_dma);
  1448. if (!buf) {
  1449. kfree(unode);
  1450. usb_free_urb(urb);
  1451. break;
  1452. }
  1453. /* urb->transfer_buffer_length set to actual before submit */
  1454. usb_fill_bulk_urb(urb, dev->udev, usb_sndbulkpipe(dev->udev, 1),
  1455. buf, size, ufx_urb_completion, unode);
  1456. urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  1457. list_add_tail(&unode->entry, &dev->urbs.list);
  1458. i++;
  1459. }
  1460. sema_init(&dev->urbs.limit_sem, i);
  1461. dev->urbs.count = i;
  1462. dev->urbs.available = i;
  1463. pr_debug("allocated %d %d byte urbs\n", i, (int) size);
  1464. return i;
  1465. }
  1466. static struct urb *ufx_get_urb(struct ufx_data *dev)
  1467. {
  1468. int ret = 0;
  1469. struct list_head *entry;
  1470. struct urb_node *unode;
  1471. struct urb *urb = NULL;
  1472. unsigned long flags;
  1473. /* Wait for an in-flight buffer to complete and get re-queued */
  1474. ret = down_timeout(&dev->urbs.limit_sem, GET_URB_TIMEOUT);
  1475. if (ret) {
  1476. atomic_set(&dev->lost_pixels, 1);
  1477. pr_warn("wait for urb interrupted: %x available: %d\n",
  1478. ret, dev->urbs.available);
  1479. goto error;
  1480. }
  1481. spin_lock_irqsave(&dev->urbs.lock, flags);
  1482. BUG_ON(list_empty(&dev->urbs.list)); /* reserved one with limit_sem */
  1483. entry = dev->urbs.list.next;
  1484. list_del_init(entry);
  1485. dev->urbs.available--;
  1486. spin_unlock_irqrestore(&dev->urbs.lock, flags);
  1487. unode = list_entry(entry, struct urb_node, entry);
  1488. urb = unode->urb;
  1489. error:
  1490. return urb;
  1491. }
  1492. static int ufx_submit_urb(struct ufx_data *dev, struct urb *urb, size_t len)
  1493. {
  1494. int ret;
  1495. BUG_ON(len > dev->urbs.size);
  1496. urb->transfer_buffer_length = len; /* set to actual payload len */
  1497. ret = usb_submit_urb(urb, GFP_KERNEL);
  1498. if (ret) {
  1499. ufx_urb_completion(urb); /* because no one else will */
  1500. atomic_set(&dev->lost_pixels, 1);
  1501. pr_err("usb_submit_urb error %x\n", ret);
  1502. }
  1503. return ret;
  1504. }
  1505. module_param(console, bool, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
  1506. MODULE_PARM_DESC(console, "Allow fbcon to be used on this display");
  1507. module_param(fb_defio, bool, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
  1508. MODULE_PARM_DESC(fb_defio, "Enable fb_defio mmap support");
  1509. MODULE_AUTHOR("Steve Glendinning <steve.glendinning@shawell.net>");
  1510. MODULE_DESCRIPTION("SMSC UFX kernel framebuffer driver");
  1511. MODULE_LICENSE("GPL");