smscufx.c 54 KB

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