xf86drmMode.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865
  1. /*
  2. * \file xf86drmMode.c
  3. * Header for DRM modesetting interface.
  4. *
  5. */
  6. /* SPDX-FileContributor: Jakob Bornecrantz <wallbraker@gmail.com>
  7. * SPDX-FileContributor: Dave Airlie <airlied@linux.ie>
  8. * SPDX-FileCopyrightText: 2007-2008 Tungsten Graphics, Inc., Cedar Park, Texas.
  9. * SPDX-FileCopyrightText: 2007-2008 Dave Airlie <airlied@linux.ie>
  10. * SPDX-FileCopyrightText: 2007-2008 Jakob Bornecrantz <wallbraker@gmail.com>
  11. * SPDX-License-Identifier: MIT
  12. */
  13. #include <assert.h>
  14. #include <limits.h>
  15. #include <stdint.h>
  16. #include <stdlib.h>
  17. #include <sys/ioctl.h>
  18. #if HAVE_SYS_SYSCTL_H
  19. #ifdef __FreeBSD__
  20. #include <sys/types.h>
  21. #endif
  22. #include <sys/sysctl.h>
  23. #endif
  24. #include <stdio.h>
  25. #include <stdbool.h>
  26. #include "libdrm_macros.h"
  27. #include "xf86drmMode.h"
  28. #include "xf86drm.h"
  29. #include <drm.h>
  30. #include <drm_fourcc.h>
  31. #include <string.h>
  32. #include <dirent.h>
  33. #include <unistd.h>
  34. #include <errno.h>
  35. #define memclear(s) memset(&s, 0, sizeof(s))
  36. #define U642VOID(x) ((void *)(unsigned long)(x))
  37. #define VOID2U64(x) ((uint64_t)(unsigned long)(x))
  38. static inline int DRM_IOCTL(int fd, unsigned long cmd, void *arg)
  39. {
  40. int ret = drmIoctl(fd, cmd, arg);
  41. return ret < 0 ? -errno : ret;
  42. }
  43. /*
  44. * Util functions
  45. */
  46. static void* drmAllocCpy(char *array, size_t count, size_t entry_size)
  47. {
  48. char *r;
  49. size_t i;
  50. if (!count || !array || !entry_size)
  51. return 0;
  52. if (!(r = calloc(count, entry_size)))
  53. return 0;
  54. for (i = 0; i < count; i++)
  55. memcpy(r+(entry_size*i), array+(entry_size*i), entry_size);
  56. return r;
  57. }
  58. /*
  59. * A couple of free functions.
  60. */
  61. drm_public void drmModeFreeModeInfo(drmModeModeInfoPtr ptr)
  62. {
  63. if (!ptr)
  64. return;
  65. drmFree(ptr);
  66. }
  67. drm_public void drmModeFreeResources(drmModeResPtr ptr)
  68. {
  69. if (!ptr)
  70. return;
  71. drmFree(ptr->fbs);
  72. drmFree(ptr->crtcs);
  73. drmFree(ptr->connectors);
  74. drmFree(ptr->encoders);
  75. drmFree(ptr);
  76. }
  77. drm_public void drmModeFreeFB(drmModeFBPtr ptr)
  78. {
  79. if (!ptr)
  80. return;
  81. /* we might add more frees later. */
  82. drmFree(ptr);
  83. }
  84. drm_public void drmModeFreeCrtc(drmModeCrtcPtr ptr)
  85. {
  86. if (!ptr)
  87. return;
  88. drmFree(ptr);
  89. }
  90. drm_public void drmModeFreeConnector(drmModeConnectorPtr ptr)
  91. {
  92. if (!ptr)
  93. return;
  94. drmFree(ptr->encoders);
  95. drmFree(ptr->prop_values);
  96. drmFree(ptr->props);
  97. drmFree(ptr->modes);
  98. drmFree(ptr);
  99. }
  100. drm_public void drmModeFreeEncoder(drmModeEncoderPtr ptr)
  101. {
  102. drmFree(ptr);
  103. }
  104. /*
  105. * ModeSetting functions.
  106. */
  107. drm_public int drmIsKMS(int fd)
  108. {
  109. struct drm_mode_card_res res = {0};
  110. if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res) != 0)
  111. return 0;
  112. return res.count_crtcs > 0 && res.count_connectors > 0 && res.count_encoders > 0;
  113. }
  114. drm_public drmModeResPtr drmModeGetResources(int fd)
  115. {
  116. struct drm_mode_card_res res, counts;
  117. drmModeResPtr r = 0;
  118. retry:
  119. memclear(res);
  120. if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res))
  121. return 0;
  122. counts = res;
  123. if (res.count_fbs) {
  124. res.fb_id_ptr = VOID2U64(calloc(res.count_fbs, sizeof(uint32_t)));
  125. if (!res.fb_id_ptr)
  126. goto err_allocs;
  127. }
  128. if (res.count_crtcs) {
  129. res.crtc_id_ptr = VOID2U64(calloc(res.count_crtcs, sizeof(uint32_t)));
  130. if (!res.crtc_id_ptr)
  131. goto err_allocs;
  132. }
  133. if (res.count_connectors) {
  134. res.connector_id_ptr = VOID2U64(calloc(res.count_connectors, sizeof(uint32_t)));
  135. if (!res.connector_id_ptr)
  136. goto err_allocs;
  137. }
  138. if (res.count_encoders) {
  139. res.encoder_id_ptr = VOID2U64(calloc(res.count_encoders, sizeof(uint32_t)));
  140. if (!res.encoder_id_ptr)
  141. goto err_allocs;
  142. }
  143. if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res))
  144. goto err_allocs;
  145. /* The number of available connectors and etc may have changed with a
  146. * hotplug event in between the ioctls, in which case the field is
  147. * silently ignored by the kernel.
  148. */
  149. if (counts.count_fbs < res.count_fbs ||
  150. counts.count_crtcs < res.count_crtcs ||
  151. counts.count_connectors < res.count_connectors ||
  152. counts.count_encoders < res.count_encoders)
  153. {
  154. drmFree(U642VOID(res.fb_id_ptr));
  155. drmFree(U642VOID(res.crtc_id_ptr));
  156. drmFree(U642VOID(res.connector_id_ptr));
  157. drmFree(U642VOID(res.encoder_id_ptr));
  158. goto retry;
  159. }
  160. /*
  161. * return
  162. */
  163. if (!(r = drmMalloc(sizeof(*r))))
  164. goto err_allocs;
  165. r->min_width = res.min_width;
  166. r->max_width = res.max_width;
  167. r->min_height = res.min_height;
  168. r->max_height = res.max_height;
  169. r->count_fbs = res.count_fbs;
  170. r->count_crtcs = res.count_crtcs;
  171. r->count_connectors = res.count_connectors;
  172. r->count_encoders = res.count_encoders;
  173. r->fbs = drmAllocCpy(U642VOID(res.fb_id_ptr), res.count_fbs, sizeof(uint32_t));
  174. r->crtcs = drmAllocCpy(U642VOID(res.crtc_id_ptr), res.count_crtcs, sizeof(uint32_t));
  175. r->connectors = drmAllocCpy(U642VOID(res.connector_id_ptr), res.count_connectors, sizeof(uint32_t));
  176. r->encoders = drmAllocCpy(U642VOID(res.encoder_id_ptr), res.count_encoders, sizeof(uint32_t));
  177. if ((res.count_fbs && !r->fbs) ||
  178. (res.count_crtcs && !r->crtcs) ||
  179. (res.count_connectors && !r->connectors) ||
  180. (res.count_encoders && !r->encoders))
  181. {
  182. drmFree(r->fbs);
  183. drmFree(r->crtcs);
  184. drmFree(r->connectors);
  185. drmFree(r->encoders);
  186. drmFree(r);
  187. r = 0;
  188. }
  189. err_allocs:
  190. drmFree(U642VOID(res.fb_id_ptr));
  191. drmFree(U642VOID(res.crtc_id_ptr));
  192. drmFree(U642VOID(res.connector_id_ptr));
  193. drmFree(U642VOID(res.encoder_id_ptr));
  194. return r;
  195. }
  196. drm_public int drmModeAddFB(int fd, uint32_t width, uint32_t height, uint8_t depth,
  197. uint8_t bpp, uint32_t pitch, uint32_t bo_handle,
  198. uint32_t *buf_id)
  199. {
  200. struct drm_mode_fb_cmd f;
  201. int ret;
  202. memclear(f);
  203. f.width = width;
  204. f.height = height;
  205. f.pitch = pitch;
  206. f.bpp = bpp;
  207. f.depth = depth;
  208. f.handle = bo_handle;
  209. if ((ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_ADDFB, &f)))
  210. return ret;
  211. *buf_id = f.fb_id;
  212. return 0;
  213. }
  214. drm_public int drmModeAddFB2WithModifiers(int fd, uint32_t width,
  215. uint32_t height, uint32_t pixel_format, const uint32_t bo_handles[4],
  216. const uint32_t pitches[4], const uint32_t offsets[4],
  217. const uint64_t modifier[4], uint32_t *buf_id, uint32_t flags)
  218. {
  219. struct drm_mode_fb_cmd2 f;
  220. int ret;
  221. memclear(f);
  222. f.width = width;
  223. f.height = height;
  224. f.pixel_format = pixel_format;
  225. f.flags = flags;
  226. memcpy(f.handles, bo_handles, 4 * sizeof(bo_handles[0]));
  227. memcpy(f.pitches, pitches, 4 * sizeof(pitches[0]));
  228. memcpy(f.offsets, offsets, 4 * sizeof(offsets[0]));
  229. if (modifier)
  230. memcpy(f.modifier, modifier, 4 * sizeof(modifier[0]));
  231. if ((ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_ADDFB2, &f)))
  232. return ret;
  233. *buf_id = f.fb_id;
  234. return 0;
  235. }
  236. drm_public int drmModeAddFB2(int fd, uint32_t width, uint32_t height,
  237. uint32_t pixel_format, const uint32_t bo_handles[4],
  238. const uint32_t pitches[4], const uint32_t offsets[4],
  239. uint32_t *buf_id, uint32_t flags)
  240. {
  241. return drmModeAddFB2WithModifiers(fd, width, height,
  242. pixel_format, bo_handles,
  243. pitches, offsets, NULL,
  244. buf_id, flags);
  245. }
  246. drm_public int drmModeRmFB(int fd, uint32_t bufferId)
  247. {
  248. return DRM_IOCTL(fd, DRM_IOCTL_MODE_RMFB, &bufferId);
  249. }
  250. drm_public int drmModeCloseFB(int fd, uint32_t buffer_id)
  251. {
  252. struct drm_mode_closefb closefb;
  253. memclear(closefb);
  254. closefb.fb_id = buffer_id;
  255. return DRM_IOCTL(fd, DRM_IOCTL_MODE_CLOSEFB, &closefb);
  256. }
  257. drm_public drmModeFBPtr drmModeGetFB(int fd, uint32_t buf)
  258. {
  259. struct drm_mode_fb_cmd info;
  260. drmModeFBPtr r;
  261. memclear(info);
  262. info.fb_id = buf;
  263. if (drmIoctl(fd, DRM_IOCTL_MODE_GETFB, &info))
  264. return NULL;
  265. if (!(r = drmMalloc(sizeof(*r))))
  266. return NULL;
  267. r->fb_id = info.fb_id;
  268. r->width = info.width;
  269. r->height = info.height;
  270. r->pitch = info.pitch;
  271. r->bpp = info.bpp;
  272. r->handle = info.handle;
  273. r->depth = info.depth;
  274. return r;
  275. }
  276. drm_public int drmModeDirtyFB(int fd, uint32_t bufferId,
  277. drmModeClipPtr clips, uint32_t num_clips)
  278. {
  279. struct drm_mode_fb_dirty_cmd dirty;
  280. memclear(dirty);
  281. dirty.fb_id = bufferId;
  282. dirty.clips_ptr = VOID2U64(clips);
  283. dirty.num_clips = num_clips;
  284. return DRM_IOCTL(fd, DRM_IOCTL_MODE_DIRTYFB, &dirty);
  285. }
  286. /*
  287. * Crtc functions
  288. */
  289. drm_public drmModeCrtcPtr drmModeGetCrtc(int fd, uint32_t crtcId)
  290. {
  291. struct drm_mode_crtc crtc;
  292. drmModeCrtcPtr r;
  293. memclear(crtc);
  294. crtc.crtc_id = crtcId;
  295. if (drmIoctl(fd, DRM_IOCTL_MODE_GETCRTC, &crtc))
  296. return 0;
  297. /*
  298. * return
  299. */
  300. if (!(r = drmMalloc(sizeof(*r))))
  301. return 0;
  302. r->crtc_id = crtc.crtc_id;
  303. r->x = crtc.x;
  304. r->y = crtc.y;
  305. r->mode_valid = crtc.mode_valid;
  306. if (r->mode_valid) {
  307. memcpy(&r->mode, &crtc.mode, sizeof(struct drm_mode_modeinfo));
  308. r->width = crtc.mode.hdisplay;
  309. r->height = crtc.mode.vdisplay;
  310. }
  311. r->buffer_id = crtc.fb_id;
  312. r->gamma_size = crtc.gamma_size;
  313. return r;
  314. }
  315. drm_public int drmModeSetCrtc(int fd, uint32_t crtcId, uint32_t bufferId,
  316. uint32_t x, uint32_t y, uint32_t *connectors, int count,
  317. drmModeModeInfoPtr mode)
  318. {
  319. struct drm_mode_crtc crtc;
  320. memclear(crtc);
  321. crtc.x = x;
  322. crtc.y = y;
  323. crtc.crtc_id = crtcId;
  324. crtc.fb_id = bufferId;
  325. crtc.set_connectors_ptr = VOID2U64(connectors);
  326. crtc.count_connectors = count;
  327. if (mode) {
  328. memcpy(&crtc.mode, mode, sizeof(struct drm_mode_modeinfo));
  329. crtc.mode_valid = 1;
  330. }
  331. return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETCRTC, &crtc);
  332. }
  333. /*
  334. * Cursor manipulation
  335. */
  336. drm_public int drmModeSetCursor(int fd, uint32_t crtcId, uint32_t bo_handle,
  337. uint32_t width, uint32_t height)
  338. {
  339. struct drm_mode_cursor arg;
  340. memclear(arg);
  341. arg.flags = DRM_MODE_CURSOR_BO;
  342. arg.crtc_id = crtcId;
  343. arg.width = width;
  344. arg.height = height;
  345. arg.handle = bo_handle;
  346. return DRM_IOCTL(fd, DRM_IOCTL_MODE_CURSOR, &arg);
  347. }
  348. drm_public int drmModeSetCursor2(int fd, uint32_t crtcId, uint32_t bo_handle,
  349. uint32_t width, uint32_t height, int32_t hot_x,
  350. int32_t hot_y)
  351. {
  352. struct drm_mode_cursor2 arg;
  353. memclear(arg);
  354. arg.flags = DRM_MODE_CURSOR_BO;
  355. arg.crtc_id = crtcId;
  356. arg.width = width;
  357. arg.height = height;
  358. arg.handle = bo_handle;
  359. arg.hot_x = hot_x;
  360. arg.hot_y = hot_y;
  361. return DRM_IOCTL(fd, DRM_IOCTL_MODE_CURSOR2, &arg);
  362. }
  363. drm_public int drmModeMoveCursor(int fd, uint32_t crtcId, int x, int y)
  364. {
  365. struct drm_mode_cursor arg;
  366. memclear(arg);
  367. arg.flags = DRM_MODE_CURSOR_MOVE;
  368. arg.crtc_id = crtcId;
  369. arg.x = x;
  370. arg.y = y;
  371. return DRM_IOCTL(fd, DRM_IOCTL_MODE_CURSOR, &arg);
  372. }
  373. /*
  374. * Encoder get
  375. */
  376. drm_public drmModeEncoderPtr drmModeGetEncoder(int fd, uint32_t encoder_id)
  377. {
  378. struct drm_mode_get_encoder enc;
  379. drmModeEncoderPtr r = NULL;
  380. memclear(enc);
  381. enc.encoder_id = encoder_id;
  382. if (drmIoctl(fd, DRM_IOCTL_MODE_GETENCODER, &enc))
  383. return 0;
  384. if (!(r = drmMalloc(sizeof(*r))))
  385. return 0;
  386. r->encoder_id = enc.encoder_id;
  387. r->crtc_id = enc.crtc_id;
  388. r->encoder_type = enc.encoder_type;
  389. r->possible_crtcs = enc.possible_crtcs;
  390. r->possible_clones = enc.possible_clones;
  391. return r;
  392. }
  393. /*
  394. * Connector manipulation
  395. */
  396. static drmModeConnectorPtr
  397. _drmModeGetConnector(int fd, uint32_t connector_id, int probe)
  398. {
  399. struct drm_mode_get_connector conn, counts;
  400. drmModeConnectorPtr r = NULL;
  401. struct drm_mode_modeinfo stack_mode;
  402. memclear(conn);
  403. conn.connector_id = connector_id;
  404. if (!probe) {
  405. conn.count_modes = 1;
  406. conn.modes_ptr = VOID2U64(&stack_mode);
  407. }
  408. if (drmIoctl(fd, DRM_IOCTL_MODE_GETCONNECTOR, &conn))
  409. return 0;
  410. retry:
  411. counts = conn;
  412. if (conn.count_props) {
  413. conn.props_ptr = VOID2U64(calloc(conn.count_props, sizeof(uint32_t)));
  414. if (!conn.props_ptr)
  415. goto err_allocs;
  416. conn.prop_values_ptr = VOID2U64(calloc(conn.count_props, sizeof(uint64_t)));
  417. if (!conn.prop_values_ptr)
  418. goto err_allocs;
  419. }
  420. if (conn.count_modes) {
  421. conn.modes_ptr = VOID2U64(calloc(conn.count_modes, sizeof(struct drm_mode_modeinfo)));
  422. if (!conn.modes_ptr)
  423. goto err_allocs;
  424. } else {
  425. conn.count_modes = 1;
  426. conn.modes_ptr = VOID2U64(&stack_mode);
  427. }
  428. if (conn.count_encoders) {
  429. conn.encoders_ptr = VOID2U64(calloc(conn.count_encoders, sizeof(uint32_t)));
  430. if (!conn.encoders_ptr)
  431. goto err_allocs;
  432. }
  433. if (drmIoctl(fd, DRM_IOCTL_MODE_GETCONNECTOR, &conn))
  434. goto err_allocs;
  435. /* The number of available connectors and etc may have changed with a
  436. * hotplug event in between the ioctls, in which case the field is
  437. * silently ignored by the kernel.
  438. */
  439. if (counts.count_props < conn.count_props ||
  440. counts.count_modes < conn.count_modes ||
  441. counts.count_encoders < conn.count_encoders) {
  442. drmFree(U642VOID(conn.props_ptr));
  443. conn.props_ptr = 0;
  444. drmFree(U642VOID(conn.prop_values_ptr));
  445. conn.prop_values_ptr = 0;
  446. if (U642VOID(conn.modes_ptr) != &stack_mode)
  447. drmFree(U642VOID(conn.modes_ptr));
  448. conn.modes_ptr = 0;
  449. drmFree(U642VOID(conn.encoders_ptr));
  450. conn.encoders_ptr = 0;
  451. goto retry;
  452. }
  453. if(!(r = drmMalloc(sizeof(*r)))) {
  454. goto err_allocs;
  455. }
  456. r->connector_id = conn.connector_id;
  457. r->encoder_id = conn.encoder_id;
  458. r->connection = conn.connection;
  459. r->mmWidth = conn.mm_width;
  460. r->mmHeight = conn.mm_height;
  461. /* convert subpixel from kernel to userspace */
  462. r->subpixel = conn.subpixel + 1;
  463. r->count_modes = conn.count_modes;
  464. r->count_props = conn.count_props;
  465. r->props = drmAllocCpy(U642VOID(conn.props_ptr), conn.count_props, sizeof(uint32_t));
  466. r->prop_values = drmAllocCpy(U642VOID(conn.prop_values_ptr), conn.count_props, sizeof(uint64_t));
  467. r->modes = drmAllocCpy(U642VOID(conn.modes_ptr), conn.count_modes, sizeof(struct drm_mode_modeinfo));
  468. r->count_encoders = conn.count_encoders;
  469. r->encoders = drmAllocCpy(U642VOID(conn.encoders_ptr), conn.count_encoders, sizeof(uint32_t));
  470. r->connector_type = conn.connector_type;
  471. r->connector_type_id = conn.connector_type_id;
  472. if ((r->count_props && !r->props) ||
  473. (r->count_props && !r->prop_values) ||
  474. (r->count_modes && !r->modes) ||
  475. (r->count_encoders && !r->encoders)) {
  476. drmFree(r->props);
  477. drmFree(r->prop_values);
  478. drmFree(r->modes);
  479. drmFree(r->encoders);
  480. drmFree(r);
  481. r = 0;
  482. }
  483. err_allocs:
  484. drmFree(U642VOID(conn.prop_values_ptr));
  485. drmFree(U642VOID(conn.props_ptr));
  486. if (U642VOID(conn.modes_ptr) != &stack_mode)
  487. drmFree(U642VOID(conn.modes_ptr));
  488. drmFree(U642VOID(conn.encoders_ptr));
  489. return r;
  490. }
  491. drm_public drmModeConnectorPtr drmModeGetConnector(int fd, uint32_t connector_id)
  492. {
  493. return _drmModeGetConnector(fd, connector_id, 1);
  494. }
  495. drm_public drmModeConnectorPtr drmModeGetConnectorCurrent(int fd, uint32_t connector_id)
  496. {
  497. return _drmModeGetConnector(fd, connector_id, 0);
  498. }
  499. drm_public uint32_t drmModeConnectorGetPossibleCrtcs(int fd,
  500. const drmModeConnector *connector)
  501. {
  502. drmModeEncoder *encoder;
  503. int i;
  504. uint32_t possible_crtcs;
  505. possible_crtcs = 0;
  506. for (i = 0; i < connector->count_encoders; i++) {
  507. encoder = drmModeGetEncoder(fd, connector->encoders[i]);
  508. if (!encoder) {
  509. return 0;
  510. }
  511. possible_crtcs |= encoder->possible_crtcs;
  512. drmModeFreeEncoder(encoder);
  513. }
  514. if (possible_crtcs == 0)
  515. errno = ENOENT;
  516. return possible_crtcs;
  517. }
  518. drm_public int drmModeAttachMode(int fd, uint32_t connector_id, drmModeModeInfoPtr mode_info)
  519. {
  520. struct drm_mode_mode_cmd res;
  521. memclear(res);
  522. memcpy(&res.mode, mode_info, sizeof(struct drm_mode_modeinfo));
  523. res.connector_id = connector_id;
  524. return DRM_IOCTL(fd, DRM_IOCTL_MODE_ATTACHMODE, &res);
  525. }
  526. drm_public int drmModeDetachMode(int fd, uint32_t connector_id, drmModeModeInfoPtr mode_info)
  527. {
  528. struct drm_mode_mode_cmd res;
  529. memclear(res);
  530. memcpy(&res.mode, mode_info, sizeof(struct drm_mode_modeinfo));
  531. res.connector_id = connector_id;
  532. return DRM_IOCTL(fd, DRM_IOCTL_MODE_DETACHMODE, &res);
  533. }
  534. drm_public drmModePropertyPtr drmModeGetProperty(int fd, uint32_t property_id)
  535. {
  536. struct drm_mode_get_property prop;
  537. drmModePropertyPtr r;
  538. memclear(prop);
  539. prop.prop_id = property_id;
  540. if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPERTY, &prop))
  541. return 0;
  542. if (prop.count_values)
  543. prop.values_ptr = VOID2U64(calloc(prop.count_values, sizeof(uint64_t)));
  544. if (prop.count_enum_blobs && (prop.flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
  545. prop.enum_blob_ptr = VOID2U64(calloc(prop.count_enum_blobs, sizeof(struct drm_mode_property_enum)));
  546. if (prop.count_enum_blobs && (prop.flags & DRM_MODE_PROP_BLOB)) {
  547. prop.values_ptr = VOID2U64(calloc(prop.count_enum_blobs, sizeof(uint32_t)));
  548. prop.enum_blob_ptr = VOID2U64(calloc(prop.count_enum_blobs, sizeof(uint32_t)));
  549. }
  550. if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPERTY, &prop)) {
  551. r = NULL;
  552. goto err_allocs;
  553. }
  554. if (!(r = drmMalloc(sizeof(*r))))
  555. goto err_allocs;
  556. r->prop_id = prop.prop_id;
  557. r->count_values = prop.count_values;
  558. r->flags = prop.flags;
  559. if (prop.count_values)
  560. r->values = drmAllocCpy(U642VOID(prop.values_ptr), prop.count_values, sizeof(uint64_t));
  561. if (prop.flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
  562. r->count_enums = prop.count_enum_blobs;
  563. r->enums = drmAllocCpy(U642VOID(prop.enum_blob_ptr), prop.count_enum_blobs, sizeof(struct drm_mode_property_enum));
  564. } else if (prop.flags & DRM_MODE_PROP_BLOB) {
  565. r->values = drmAllocCpy(U642VOID(prop.values_ptr), prop.count_enum_blobs, sizeof(uint32_t));
  566. r->blob_ids = drmAllocCpy(U642VOID(prop.enum_blob_ptr), prop.count_enum_blobs, sizeof(uint32_t));
  567. r->count_blobs = prop.count_enum_blobs;
  568. }
  569. strncpy(r->name, prop.name, DRM_PROP_NAME_LEN);
  570. r->name[DRM_PROP_NAME_LEN-1] = 0;
  571. err_allocs:
  572. drmFree(U642VOID(prop.values_ptr));
  573. drmFree(U642VOID(prop.enum_blob_ptr));
  574. return r;
  575. }
  576. drm_public void drmModeFreeProperty(drmModePropertyPtr ptr)
  577. {
  578. if (!ptr)
  579. return;
  580. drmFree(ptr->values);
  581. drmFree(ptr->enums);
  582. drmFree(ptr->blob_ids);
  583. drmFree(ptr);
  584. }
  585. drm_public drmModePropertyBlobPtr drmModeGetPropertyBlob(int fd,
  586. uint32_t blob_id)
  587. {
  588. struct drm_mode_get_blob blob;
  589. drmModePropertyBlobPtr r;
  590. memclear(blob);
  591. blob.blob_id = blob_id;
  592. if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPBLOB, &blob))
  593. return NULL;
  594. if (blob.length)
  595. blob.data = VOID2U64(drmMalloc(blob.length));
  596. if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPBLOB, &blob)) {
  597. r = NULL;
  598. goto err_allocs;
  599. }
  600. if (!(r = drmMalloc(sizeof(*r))))
  601. goto err_allocs;
  602. r->id = blob.blob_id;
  603. r->length = blob.length;
  604. r->data = drmAllocCpy(U642VOID(blob.data), 1, blob.length);
  605. err_allocs:
  606. drmFree(U642VOID(blob.data));
  607. return r;
  608. }
  609. static inline const uint32_t *
  610. get_formats_ptr(const struct drm_format_modifier_blob *blob)
  611. {
  612. return (const uint32_t *)(((uint8_t *)blob) + blob->formats_offset);
  613. }
  614. static inline const struct drm_format_modifier *
  615. get_modifiers_ptr(const struct drm_format_modifier_blob *blob)
  616. {
  617. return (const struct drm_format_modifier *)(((uint8_t *)blob) +
  618. blob->modifiers_offset);
  619. }
  620. static bool _drmModeFormatModifierGetNext(const drmModePropertyBlobRes *blob,
  621. drmModeFormatModifierIterator *iter)
  622. {
  623. const struct drm_format_modifier *blob_modifiers, *mod;
  624. const struct drm_format_modifier_blob *fmt_mod_blob;
  625. const uint32_t *blob_formats;
  626. assert(blob && iter);
  627. fmt_mod_blob = blob->data;
  628. blob_modifiers = get_modifiers_ptr(fmt_mod_blob);
  629. blob_formats = get_formats_ptr(fmt_mod_blob);
  630. /* fmt_idx and mod_idx designate the number of processed formats
  631. * and modifiers.
  632. */
  633. if (iter->fmt_idx >= fmt_mod_blob->count_formats ||
  634. iter->mod_idx >= fmt_mod_blob->count_modifiers)
  635. return false;
  636. iter->fmt = blob_formats[iter->fmt_idx];
  637. iter->mod = DRM_FORMAT_MOD_INVALID;
  638. /* From the latest valid found, get the next valid modifier */
  639. while (iter->mod_idx < fmt_mod_blob->count_modifiers) {
  640. mod = &blob_modifiers[iter->mod_idx++];
  641. /* Check if the format that fmt_idx designates, belongs to
  642. * this modifier 64-bit window selected via mod->offset.
  643. */
  644. if (iter->fmt_idx < mod->offset ||
  645. iter->fmt_idx >= mod->offset + 64)
  646. continue;
  647. if (!(mod->formats & (1 << (iter->fmt_idx - mod->offset))))
  648. continue;
  649. iter->mod = mod->modifier;
  650. break;
  651. }
  652. if (iter->mod_idx == fmt_mod_blob->count_modifiers) {
  653. iter->mod_idx = 0;
  654. iter->fmt_idx++;
  655. }
  656. /* Since mod_idx reset, in order for the caller to iterate over
  657. * the last modifier of the last format, always return true here
  658. * and early return from the next call.
  659. */
  660. return true;
  661. }
  662. /**
  663. * Iterate over formats first and then over modifiers. On each call, iter->fmt
  664. * is retained until all associated modifiers are returned. Then, either update
  665. * iter->fmt with the next format, or exit if there aren't any left.
  666. *
  667. * NOTE: clients should not make any assumption on mod_idx and fmt_idx values
  668. *
  669. * @blob: valid kernel blob holding formats and modifiers
  670. * @iter: input and output iterator data. Iter data must be initialised to zero
  671. * @return: false, on error or there aren't any further formats or modifiers left.
  672. * true, on success and there are more formats or modifiers.
  673. */
  674. drm_public bool drmModeFormatModifierBlobIterNext(const drmModePropertyBlobRes *blob,
  675. drmModeFormatModifierIterator *iter)
  676. {
  677. drmModeFormatModifierIterator tmp;
  678. bool has_fmt;
  679. if (!blob || !iter)
  680. return false;
  681. tmp.fmt_idx = iter->fmt_idx;
  682. tmp.mod_idx = iter->mod_idx;
  683. /* With the current state of things, DRM/KMS drivers are allowed to
  684. * construct blobs having formats and no modifiers. Userspace can't
  685. * legitimately abort in such cases.
  686. *
  687. * While waiting for the kernel to perhaps disallow formats with no
  688. * modifiers in IN_FORMATS blobs, skip the format altogether.
  689. */
  690. do {
  691. has_fmt = _drmModeFormatModifierGetNext(blob, &tmp);
  692. if (has_fmt && tmp.mod != DRM_FORMAT_MOD_INVALID)
  693. *iter = tmp;
  694. } while (has_fmt && tmp.mod == DRM_FORMAT_MOD_INVALID);
  695. return has_fmt;
  696. }
  697. drm_public void drmModeFreePropertyBlob(drmModePropertyBlobPtr ptr)
  698. {
  699. if (!ptr)
  700. return;
  701. drmFree(ptr->data);
  702. drmFree(ptr);
  703. }
  704. drm_public int drmModeConnectorSetProperty(int fd, uint32_t connector_id,
  705. uint32_t property_id,
  706. uint64_t value)
  707. {
  708. struct drm_mode_connector_set_property osp;
  709. memclear(osp);
  710. osp.connector_id = connector_id;
  711. osp.prop_id = property_id;
  712. osp.value = value;
  713. return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETPROPERTY, &osp);
  714. }
  715. /*
  716. * checks if a modesetting capable driver has attached to the pci id
  717. * returns 0 if modesetting supported.
  718. * -EINVAL or invalid bus id
  719. * -ENOSYS if no modesetting support
  720. */
  721. drm_public int drmCheckModesettingSupported(const char *busid)
  722. {
  723. #if defined (__linux__)
  724. char pci_dev_dir[1024];
  725. int domain, bus, dev, func;
  726. DIR *sysdir;
  727. struct dirent *dent;
  728. int found = 0, ret;
  729. ret = sscanf(busid, "pci:%04x:%02x:%02x.%d", &domain, &bus, &dev, &func);
  730. if (ret != 4)
  731. return -EINVAL;
  732. sprintf(pci_dev_dir, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/drm",
  733. domain, bus, dev, func);
  734. sysdir = opendir(pci_dev_dir);
  735. if (sysdir) {
  736. dent = readdir(sysdir);
  737. while (dent) {
  738. if (!strncmp(dent->d_name, "controlD", 8)) {
  739. found = 1;
  740. break;
  741. }
  742. dent = readdir(sysdir);
  743. }
  744. closedir(sysdir);
  745. if (found)
  746. return 0;
  747. }
  748. sprintf(pci_dev_dir, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/",
  749. domain, bus, dev, func);
  750. sysdir = opendir(pci_dev_dir);
  751. if (!sysdir)
  752. return -EINVAL;
  753. dent = readdir(sysdir);
  754. while (dent) {
  755. if (!strncmp(dent->d_name, "drm:controlD", 12)) {
  756. found = 1;
  757. break;
  758. }
  759. dent = readdir(sysdir);
  760. }
  761. closedir(sysdir);
  762. if (found)
  763. return 0;
  764. #elif defined (__FreeBSD__) || defined (__FreeBSD_kernel__)
  765. char sbusid[1024];
  766. char oid[128];
  767. int i, modesetting, ret;
  768. size_t len;
  769. /* How many GPUs do we expect in the machine ? */
  770. for (i = 0; i < 10; i++) {
  771. snprintf(oid, sizeof(oid), "hw.dri.%d.busid", i);
  772. len = sizeof(sbusid);
  773. ret = sysctlbyname(oid, sbusid, &len, NULL, 0);
  774. if (ret == -1) {
  775. if (errno == ENOENT)
  776. continue;
  777. return -EINVAL;
  778. }
  779. if (strcmp(sbusid, busid) != 0)
  780. continue;
  781. snprintf(oid, sizeof(oid), "hw.dri.%d.modesetting", i);
  782. len = sizeof(modesetting);
  783. ret = sysctlbyname(oid, &modesetting, &len, NULL, 0);
  784. if (ret == -1 || len != sizeof(modesetting))
  785. return -EINVAL;
  786. return (modesetting ? 0 : -ENOSYS);
  787. }
  788. #elif defined(__DragonFly__)
  789. return 0;
  790. #elif defined(__OpenBSD__)
  791. int fd;
  792. struct drm_mode_card_res res;
  793. drmModeResPtr r = 0;
  794. if ((fd = drmOpen(NULL, busid)) < 0)
  795. return -EINVAL;
  796. memset(&res, 0, sizeof(struct drm_mode_card_res));
  797. if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res)) {
  798. drmClose(fd);
  799. return -errno;
  800. }
  801. drmClose(fd);
  802. return 0;
  803. #endif
  804. return -ENOSYS;
  805. }
  806. drm_public int drmModeCrtcGetGamma(int fd, uint32_t crtc_id, uint32_t size,
  807. uint16_t *red, uint16_t *green,
  808. uint16_t *blue)
  809. {
  810. struct drm_mode_crtc_lut l;
  811. memclear(l);
  812. l.crtc_id = crtc_id;
  813. l.gamma_size = size;
  814. l.red = VOID2U64(red);
  815. l.green = VOID2U64(green);
  816. l.blue = VOID2U64(blue);
  817. return DRM_IOCTL(fd, DRM_IOCTL_MODE_GETGAMMA, &l);
  818. }
  819. drm_public int drmModeCrtcSetGamma(int fd, uint32_t crtc_id, uint32_t size,
  820. const uint16_t *red, const uint16_t *green,
  821. const uint16_t *blue)
  822. {
  823. struct drm_mode_crtc_lut l;
  824. memclear(l);
  825. l.crtc_id = crtc_id;
  826. l.gamma_size = size;
  827. l.red = VOID2U64(red);
  828. l.green = VOID2U64(green);
  829. l.blue = VOID2U64(blue);
  830. return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETGAMMA, &l);
  831. }
  832. drm_public int drmHandleEvent(int fd, drmEventContextPtr evctx)
  833. {
  834. char buffer[1024];
  835. int len, i;
  836. struct drm_event *e;
  837. struct drm_event_vblank *vblank;
  838. struct drm_event_crtc_sequence *seq;
  839. void *user_data;
  840. /* The DRM read semantics guarantees that we always get only
  841. * complete events. */
  842. len = read(fd, buffer, sizeof buffer);
  843. if (len == 0)
  844. return 0;
  845. if (len < (int)sizeof *e)
  846. return -1;
  847. i = 0;
  848. while (i < len) {
  849. e = (struct drm_event *)(buffer + i);
  850. switch (e->type) {
  851. case DRM_EVENT_VBLANK:
  852. if (evctx->version < 1 ||
  853. evctx->vblank_handler == NULL)
  854. break;
  855. vblank = (struct drm_event_vblank *) e;
  856. evctx->vblank_handler(fd,
  857. vblank->sequence,
  858. vblank->tv_sec,
  859. vblank->tv_usec,
  860. U642VOID (vblank->user_data));
  861. break;
  862. case DRM_EVENT_FLIP_COMPLETE:
  863. vblank = (struct drm_event_vblank *) e;
  864. user_data = U642VOID (vblank->user_data);
  865. if (evctx->version >= 3 && evctx->page_flip_handler2)
  866. evctx->page_flip_handler2(fd,
  867. vblank->sequence,
  868. vblank->tv_sec,
  869. vblank->tv_usec,
  870. vblank->crtc_id,
  871. user_data);
  872. else if (evctx->version >= 2 && evctx->page_flip_handler)
  873. evctx->page_flip_handler(fd,
  874. vblank->sequence,
  875. vblank->tv_sec,
  876. vblank->tv_usec,
  877. user_data);
  878. break;
  879. case DRM_EVENT_CRTC_SEQUENCE:
  880. seq = (struct drm_event_crtc_sequence *) e;
  881. if (evctx->version >= 4 && evctx->sequence_handler)
  882. evctx->sequence_handler(fd,
  883. seq->sequence,
  884. seq->time_ns,
  885. seq->user_data);
  886. break;
  887. default:
  888. break;
  889. }
  890. i += e->length;
  891. }
  892. return 0;
  893. }
  894. drm_public int drmModePageFlip(int fd, uint32_t crtc_id, uint32_t fb_id,
  895. uint32_t flags, void *user_data)
  896. {
  897. struct drm_mode_crtc_page_flip flip;
  898. memclear(flip);
  899. flip.fb_id = fb_id;
  900. flip.crtc_id = crtc_id;
  901. flip.user_data = VOID2U64(user_data);
  902. flip.flags = flags;
  903. return DRM_IOCTL(fd, DRM_IOCTL_MODE_PAGE_FLIP, &flip);
  904. }
  905. drm_public int drmModePageFlipTarget(int fd, uint32_t crtc_id, uint32_t fb_id,
  906. uint32_t flags, void *user_data,
  907. uint32_t target_vblank)
  908. {
  909. struct drm_mode_crtc_page_flip_target flip_target;
  910. memclear(flip_target);
  911. flip_target.fb_id = fb_id;
  912. flip_target.crtc_id = crtc_id;
  913. flip_target.user_data = VOID2U64(user_data);
  914. flip_target.flags = flags;
  915. flip_target.sequence = target_vblank;
  916. return DRM_IOCTL(fd, DRM_IOCTL_MODE_PAGE_FLIP, &flip_target);
  917. }
  918. drm_public int drmModeSetPlane(int fd, uint32_t plane_id, uint32_t crtc_id,
  919. uint32_t fb_id, uint32_t flags,
  920. int32_t crtc_x, int32_t crtc_y,
  921. uint32_t crtc_w, uint32_t crtc_h,
  922. uint32_t src_x, uint32_t src_y,
  923. uint32_t src_w, uint32_t src_h)
  924. {
  925. struct drm_mode_set_plane s;
  926. memclear(s);
  927. s.plane_id = plane_id;
  928. s.crtc_id = crtc_id;
  929. s.fb_id = fb_id;
  930. s.flags = flags;
  931. s.crtc_x = crtc_x;
  932. s.crtc_y = crtc_y;
  933. s.crtc_w = crtc_w;
  934. s.crtc_h = crtc_h;
  935. s.src_x = src_x;
  936. s.src_y = src_y;
  937. s.src_w = src_w;
  938. s.src_h = src_h;
  939. return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETPLANE, &s);
  940. }
  941. drm_public drmModePlanePtr drmModeGetPlane(int fd, uint32_t plane_id)
  942. {
  943. struct drm_mode_get_plane ovr, counts;
  944. drmModePlanePtr r = 0;
  945. retry:
  946. memclear(ovr);
  947. ovr.plane_id = plane_id;
  948. if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANE, &ovr))
  949. return 0;
  950. counts = ovr;
  951. if (ovr.count_format_types) {
  952. ovr.format_type_ptr = VOID2U64(drmMalloc(ovr.count_format_types *
  953. sizeof(uint32_t)));
  954. if (!ovr.format_type_ptr)
  955. goto err_allocs;
  956. }
  957. if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANE, &ovr))
  958. goto err_allocs;
  959. if (counts.count_format_types < ovr.count_format_types) {
  960. drmFree(U642VOID(ovr.format_type_ptr));
  961. goto retry;
  962. }
  963. if (!(r = drmMalloc(sizeof(*r))))
  964. goto err_allocs;
  965. r->count_formats = ovr.count_format_types;
  966. r->plane_id = ovr.plane_id;
  967. r->crtc_id = ovr.crtc_id;
  968. r->fb_id = ovr.fb_id;
  969. r->possible_crtcs = ovr.possible_crtcs;
  970. r->gamma_size = ovr.gamma_size;
  971. r->formats = drmAllocCpy(U642VOID(ovr.format_type_ptr),
  972. ovr.count_format_types, sizeof(uint32_t));
  973. if (ovr.count_format_types && !r->formats) {
  974. drmFree(r->formats);
  975. drmFree(r);
  976. r = 0;
  977. }
  978. err_allocs:
  979. drmFree(U642VOID(ovr.format_type_ptr));
  980. return r;
  981. }
  982. drm_public void drmModeFreePlane(drmModePlanePtr ptr)
  983. {
  984. if (!ptr)
  985. return;
  986. drmFree(ptr->formats);
  987. drmFree(ptr);
  988. }
  989. drm_public drmModePlaneResPtr drmModeGetPlaneResources(int fd)
  990. {
  991. struct drm_mode_get_plane_res res, counts;
  992. drmModePlaneResPtr r = 0;
  993. retry:
  994. memclear(res);
  995. if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANERESOURCES, &res))
  996. return 0;
  997. counts = res;
  998. if (res.count_planes) {
  999. res.plane_id_ptr = VOID2U64(drmMalloc(res.count_planes *
  1000. sizeof(uint32_t)));
  1001. if (!res.plane_id_ptr)
  1002. goto err_allocs;
  1003. }
  1004. if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANERESOURCES, &res))
  1005. goto err_allocs;
  1006. if (counts.count_planes < res.count_planes) {
  1007. drmFree(U642VOID(res.plane_id_ptr));
  1008. goto retry;
  1009. }
  1010. if (!(r = drmMalloc(sizeof(*r))))
  1011. goto err_allocs;
  1012. r->count_planes = res.count_planes;
  1013. r->planes = drmAllocCpy(U642VOID(res.plane_id_ptr),
  1014. res.count_planes, sizeof(uint32_t));
  1015. if (res.count_planes && !r->planes) {
  1016. drmFree(r->planes);
  1017. drmFree(r);
  1018. r = 0;
  1019. }
  1020. err_allocs:
  1021. drmFree(U642VOID(res.plane_id_ptr));
  1022. return r;
  1023. }
  1024. drm_public void drmModeFreePlaneResources(drmModePlaneResPtr ptr)
  1025. {
  1026. if (!ptr)
  1027. return;
  1028. drmFree(ptr->planes);
  1029. drmFree(ptr);
  1030. }
  1031. drm_public drmModeObjectPropertiesPtr drmModeObjectGetProperties(int fd,
  1032. uint32_t object_id,
  1033. uint32_t object_type)
  1034. {
  1035. struct drm_mode_obj_get_properties properties;
  1036. drmModeObjectPropertiesPtr ret = NULL;
  1037. uint32_t count;
  1038. retry:
  1039. memclear(properties);
  1040. properties.obj_id = object_id;
  1041. properties.obj_type = object_type;
  1042. if (drmIoctl(fd, DRM_IOCTL_MODE_OBJ_GETPROPERTIES, &properties))
  1043. return 0;
  1044. count = properties.count_props;
  1045. if (count) {
  1046. properties.props_ptr = VOID2U64(calloc(count, sizeof(uint32_t)));
  1047. if (!properties.props_ptr)
  1048. goto err_allocs;
  1049. properties.prop_values_ptr = VOID2U64(calloc(count, sizeof(uint64_t)));
  1050. if (!properties.prop_values_ptr)
  1051. goto err_allocs;
  1052. }
  1053. if (drmIoctl(fd, DRM_IOCTL_MODE_OBJ_GETPROPERTIES, &properties))
  1054. goto err_allocs;
  1055. if (count < properties.count_props) {
  1056. drmFree(U642VOID(properties.props_ptr));
  1057. drmFree(U642VOID(properties.prop_values_ptr));
  1058. goto retry;
  1059. }
  1060. count = properties.count_props;
  1061. ret = drmMalloc(sizeof(*ret));
  1062. if (!ret)
  1063. goto err_allocs;
  1064. ret->count_props = count;
  1065. ret->props = drmAllocCpy(U642VOID(properties.props_ptr),
  1066. count, sizeof(uint32_t));
  1067. ret->prop_values = drmAllocCpy(U642VOID(properties.prop_values_ptr),
  1068. count, sizeof(uint64_t));
  1069. if (ret->count_props && (!ret->props || !ret->prop_values)) {
  1070. drmFree(ret->props);
  1071. drmFree(ret->prop_values);
  1072. drmFree(ret);
  1073. ret = NULL;
  1074. }
  1075. err_allocs:
  1076. drmFree(U642VOID(properties.props_ptr));
  1077. drmFree(U642VOID(properties.prop_values_ptr));
  1078. return ret;
  1079. }
  1080. drm_public void drmModeFreeObjectProperties(drmModeObjectPropertiesPtr ptr)
  1081. {
  1082. if (!ptr)
  1083. return;
  1084. drmFree(ptr->props);
  1085. drmFree(ptr->prop_values);
  1086. drmFree(ptr);
  1087. }
  1088. drm_public int drmModeObjectSetProperty(int fd, uint32_t object_id, uint32_t object_type,
  1089. uint32_t property_id, uint64_t value)
  1090. {
  1091. struct drm_mode_obj_set_property prop;
  1092. memclear(prop);
  1093. prop.value = value;
  1094. prop.prop_id = property_id;
  1095. prop.obj_id = object_id;
  1096. prop.obj_type = object_type;
  1097. return DRM_IOCTL(fd, DRM_IOCTL_MODE_OBJ_SETPROPERTY, &prop);
  1098. }
  1099. typedef struct _drmModeAtomicReqItem drmModeAtomicReqItem, *drmModeAtomicReqItemPtr;
  1100. struct _drmModeAtomicReqItem {
  1101. uint32_t object_id;
  1102. uint32_t property_id;
  1103. uint64_t value;
  1104. uint32_t cursor;
  1105. };
  1106. struct _drmModeAtomicReq {
  1107. uint32_t cursor;
  1108. uint32_t size_items;
  1109. drmModeAtomicReqItemPtr items;
  1110. };
  1111. drm_public drmModeAtomicReqPtr drmModeAtomicAlloc(void)
  1112. {
  1113. drmModeAtomicReqPtr req;
  1114. req = drmMalloc(sizeof *req);
  1115. if (!req)
  1116. return NULL;
  1117. req->items = NULL;
  1118. req->cursor = 0;
  1119. req->size_items = 0;
  1120. return req;
  1121. }
  1122. drm_public drmModeAtomicReqPtr drmModeAtomicDuplicate(const drmModeAtomicReqPtr old)
  1123. {
  1124. drmModeAtomicReqPtr new;
  1125. if (!old)
  1126. return NULL;
  1127. new = drmMalloc(sizeof *new);
  1128. if (!new)
  1129. return NULL;
  1130. new->cursor = old->cursor;
  1131. new->size_items = old->size_items;
  1132. if (old->size_items) {
  1133. new->items = calloc(old->size_items, sizeof(*new->items));
  1134. if (!new->items) {
  1135. free(new);
  1136. return NULL;
  1137. }
  1138. memcpy(new->items, old->items,
  1139. old->cursor * sizeof(*new->items));
  1140. } else {
  1141. new->items = NULL;
  1142. }
  1143. return new;
  1144. }
  1145. drm_public int drmModeAtomicMerge(drmModeAtomicReqPtr base,
  1146. const drmModeAtomicReqPtr augment)
  1147. {
  1148. uint32_t i;
  1149. if (!base)
  1150. return -EINVAL;
  1151. if (!augment || augment->cursor == 0)
  1152. return 0;
  1153. if (base->cursor + augment->cursor >= base->size_items) {
  1154. drmModeAtomicReqItemPtr new;
  1155. int saved_size = base->size_items;
  1156. base->size_items = base->cursor + augment->cursor;
  1157. new = realloc(base->items,
  1158. base->size_items * sizeof(*base->items));
  1159. if (!new) {
  1160. base->size_items = saved_size;
  1161. return -ENOMEM;
  1162. }
  1163. base->items = new;
  1164. }
  1165. memcpy(&base->items[base->cursor], augment->items,
  1166. augment->cursor * sizeof(*augment->items));
  1167. for (i = base->cursor; i < base->cursor + augment->cursor; i++)
  1168. base->items[i].cursor = i;
  1169. base->cursor += augment->cursor;
  1170. return 0;
  1171. }
  1172. drm_public int drmModeAtomicGetCursor(const drmModeAtomicReqPtr req)
  1173. {
  1174. if (!req)
  1175. return -EINVAL;
  1176. return req->cursor;
  1177. }
  1178. drm_public void drmModeAtomicSetCursor(drmModeAtomicReqPtr req, int cursor)
  1179. {
  1180. if (req)
  1181. req->cursor = cursor;
  1182. }
  1183. drm_public int drmModeAtomicAddProperty(drmModeAtomicReqPtr req,
  1184. uint32_t object_id,
  1185. uint32_t property_id,
  1186. uint64_t value)
  1187. {
  1188. if (!req)
  1189. return -EINVAL;
  1190. if (object_id == 0 || property_id == 0)
  1191. return -EINVAL;
  1192. if (req->cursor >= req->size_items) {
  1193. const uint32_t item_size_inc = getpagesize() / sizeof(*req->items);
  1194. drmModeAtomicReqItemPtr new;
  1195. req->size_items += item_size_inc;
  1196. new = realloc(req->items, req->size_items * sizeof(*req->items));
  1197. if (!new) {
  1198. req->size_items -= item_size_inc;
  1199. return -ENOMEM;
  1200. }
  1201. req->items = new;
  1202. }
  1203. req->items[req->cursor].object_id = object_id;
  1204. req->items[req->cursor].property_id = property_id;
  1205. req->items[req->cursor].value = value;
  1206. req->items[req->cursor].cursor = req->cursor;
  1207. req->cursor++;
  1208. return req->cursor;
  1209. }
  1210. drm_public void drmModeAtomicFree(drmModeAtomicReqPtr req)
  1211. {
  1212. if (!req)
  1213. return;
  1214. if (req->items)
  1215. drmFree(req->items);
  1216. drmFree(req);
  1217. }
  1218. static int sort_req_list(const void *misc, const void *other)
  1219. {
  1220. const drmModeAtomicReqItem *first = misc;
  1221. const drmModeAtomicReqItem *second = other;
  1222. if (first->object_id != second->object_id)
  1223. return first->object_id - second->object_id;
  1224. else if (first->property_id != second->property_id)
  1225. return first->property_id - second->property_id;
  1226. else
  1227. return first->cursor - second->cursor;
  1228. }
  1229. drm_public int drmModeAtomicCommit(int fd, const drmModeAtomicReqPtr req,
  1230. uint32_t flags, void *user_data)
  1231. {
  1232. drmModeAtomicReqPtr sorted;
  1233. struct drm_mode_atomic atomic;
  1234. uint32_t *objs_ptr = NULL;
  1235. uint32_t *count_props_ptr = NULL;
  1236. uint32_t *props_ptr = NULL;
  1237. uint64_t *prop_values_ptr = NULL;
  1238. uint32_t last_obj_id = 0;
  1239. uint32_t i;
  1240. int obj_idx = -1;
  1241. int ret = -1;
  1242. if (!req)
  1243. return -EINVAL;
  1244. if (req->cursor == 0)
  1245. return 0;
  1246. sorted = drmModeAtomicDuplicate(req);
  1247. if (sorted == NULL)
  1248. return -ENOMEM;
  1249. memclear(atomic);
  1250. /* Sort the list by object ID, then by property ID. */
  1251. qsort(sorted->items, sorted->cursor, sizeof(*sorted->items),
  1252. sort_req_list);
  1253. /* Now the list is sorted, eliminate duplicate property sets. */
  1254. for (i = 0; i < sorted->cursor; i++) {
  1255. if (sorted->items[i].object_id != last_obj_id) {
  1256. atomic.count_objs++;
  1257. last_obj_id = sorted->items[i].object_id;
  1258. }
  1259. if (i == sorted->cursor - 1)
  1260. continue;
  1261. if (sorted->items[i].object_id != sorted->items[i + 1].object_id ||
  1262. sorted->items[i].property_id != sorted->items[i + 1].property_id)
  1263. continue;
  1264. memmove(&sorted->items[i], &sorted->items[i + 1],
  1265. (sorted->cursor - i - 1) * sizeof(*sorted->items));
  1266. sorted->cursor--;
  1267. }
  1268. for (i = 0; i < sorted->cursor; i++)
  1269. sorted->items[i].cursor = i;
  1270. objs_ptr = calloc(atomic.count_objs, sizeof objs_ptr[0]);
  1271. if (!objs_ptr) {
  1272. errno = ENOMEM;
  1273. goto out;
  1274. }
  1275. count_props_ptr = calloc(atomic.count_objs, sizeof count_props_ptr[0]);
  1276. if (!count_props_ptr) {
  1277. errno = ENOMEM;
  1278. goto out;
  1279. }
  1280. props_ptr = calloc(sorted->cursor, sizeof props_ptr[0]);
  1281. if (!props_ptr) {
  1282. errno = ENOMEM;
  1283. goto out;
  1284. }
  1285. prop_values_ptr = calloc(sorted->cursor, sizeof prop_values_ptr[0]);
  1286. if (!prop_values_ptr) {
  1287. errno = ENOMEM;
  1288. goto out;
  1289. }
  1290. for (i = 0, last_obj_id = 0; i < sorted->cursor; i++) {
  1291. if (sorted->items[i].object_id != last_obj_id) {
  1292. obj_idx++;
  1293. objs_ptr[obj_idx] = sorted->items[i].object_id;
  1294. last_obj_id = objs_ptr[obj_idx];
  1295. }
  1296. count_props_ptr[obj_idx]++;
  1297. props_ptr[i] = sorted->items[i].property_id;
  1298. prop_values_ptr[i] = sorted->items[i].value;
  1299. }
  1300. atomic.flags = flags;
  1301. atomic.objs_ptr = VOID2U64(objs_ptr);
  1302. atomic.count_props_ptr = VOID2U64(count_props_ptr);
  1303. atomic.props_ptr = VOID2U64(props_ptr);
  1304. atomic.prop_values_ptr = VOID2U64(prop_values_ptr);
  1305. atomic.user_data = VOID2U64(user_data);
  1306. ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_ATOMIC, &atomic);
  1307. out:
  1308. drmFree(objs_ptr);
  1309. drmFree(count_props_ptr);
  1310. drmFree(props_ptr);
  1311. drmFree(prop_values_ptr);
  1312. drmModeAtomicFree(sorted);
  1313. return ret;
  1314. }
  1315. drm_public int
  1316. drmModeCreatePropertyBlob(int fd, const void *data, size_t length,
  1317. uint32_t *id)
  1318. {
  1319. struct drm_mode_create_blob create;
  1320. int ret;
  1321. if (length >= 0xffffffff)
  1322. return -ERANGE;
  1323. memclear(create);
  1324. create.length = length;
  1325. create.data = (uintptr_t) data;
  1326. create.blob_id = 0;
  1327. *id = 0;
  1328. ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_CREATEPROPBLOB, &create);
  1329. if (ret != 0)
  1330. return ret;
  1331. *id = create.blob_id;
  1332. return 0;
  1333. }
  1334. drm_public int
  1335. drmModeDestroyPropertyBlob(int fd, uint32_t id)
  1336. {
  1337. struct drm_mode_destroy_blob destroy;
  1338. memclear(destroy);
  1339. destroy.blob_id = id;
  1340. return DRM_IOCTL(fd, DRM_IOCTL_MODE_DESTROYPROPBLOB, &destroy);
  1341. }
  1342. drm_public int
  1343. drmModeCreateLease(int fd, const uint32_t *objects, int num_objects, int flags,
  1344. uint32_t *lessee_id)
  1345. {
  1346. struct drm_mode_create_lease create;
  1347. int ret;
  1348. memclear(create);
  1349. create.object_ids = (uintptr_t) objects;
  1350. create.object_count = num_objects;
  1351. create.flags = flags;
  1352. ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_CREATE_LEASE, &create);
  1353. if (ret == 0) {
  1354. *lessee_id = create.lessee_id;
  1355. return create.fd;
  1356. }
  1357. return -errno;
  1358. }
  1359. drm_public drmModeLesseeListPtr
  1360. drmModeListLessees(int fd)
  1361. {
  1362. struct drm_mode_list_lessees list;
  1363. uint32_t count;
  1364. drmModeLesseeListPtr ret;
  1365. memclear(list);
  1366. if (DRM_IOCTL(fd, DRM_IOCTL_MODE_LIST_LESSEES, &list))
  1367. return NULL;
  1368. count = list.count_lessees;
  1369. ret = drmMalloc(sizeof (drmModeLesseeListRes) + count * sizeof (ret->lessees[0]));
  1370. if (!ret)
  1371. return NULL;
  1372. list.lessees_ptr = VOID2U64(&ret->lessees[0]);
  1373. if (DRM_IOCTL(fd, DRM_IOCTL_MODE_LIST_LESSEES, &list)) {
  1374. drmFree(ret);
  1375. return NULL;
  1376. }
  1377. ret->count = count;
  1378. return ret;
  1379. }
  1380. drm_public drmModeObjectListPtr
  1381. drmModeGetLease(int fd)
  1382. {
  1383. struct drm_mode_get_lease get;
  1384. uint32_t count;
  1385. drmModeObjectListPtr ret;
  1386. memclear(get);
  1387. if (DRM_IOCTL(fd, DRM_IOCTL_MODE_GET_LEASE, &get))
  1388. return NULL;
  1389. count = get.count_objects;
  1390. ret = drmMalloc(sizeof (drmModeObjectListRes) + count * sizeof (ret->objects[0]));
  1391. if (!ret)
  1392. return NULL;
  1393. get.objects_ptr = VOID2U64(&ret->objects[0]);
  1394. if (DRM_IOCTL(fd, DRM_IOCTL_MODE_GET_LEASE, &get)) {
  1395. drmFree(ret);
  1396. return NULL;
  1397. }
  1398. ret->count = count;
  1399. return ret;
  1400. }
  1401. drm_public int
  1402. drmModeRevokeLease(int fd, uint32_t lessee_id)
  1403. {
  1404. struct drm_mode_revoke_lease revoke;
  1405. int ret;
  1406. memclear(revoke);
  1407. revoke.lessee_id = lessee_id;
  1408. ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_REVOKE_LEASE, &revoke);
  1409. if (ret == 0)
  1410. return 0;
  1411. return -errno;
  1412. }
  1413. drm_public drmModeFB2Ptr
  1414. drmModeGetFB2(int fd, uint32_t fb_id)
  1415. {
  1416. struct drm_mode_fb_cmd2 get = {
  1417. .fb_id = fb_id,
  1418. };
  1419. drmModeFB2Ptr ret;
  1420. int err;
  1421. err = DRM_IOCTL(fd, DRM_IOCTL_MODE_GETFB2, &get);
  1422. if (err != 0)
  1423. return NULL;
  1424. ret = drmMalloc(sizeof(drmModeFB2));
  1425. if (!ret)
  1426. return NULL;
  1427. ret->fb_id = fb_id;
  1428. ret->width = get.width;
  1429. ret->height = get.height;
  1430. ret->pixel_format = get.pixel_format;
  1431. ret->flags = get.flags;
  1432. ret->modifier = get.modifier[0];
  1433. memcpy(ret->handles, get.handles, sizeof(uint32_t) * 4);
  1434. memcpy(ret->pitches, get.pitches, sizeof(uint32_t) * 4);
  1435. memcpy(ret->offsets, get.offsets, sizeof(uint32_t) * 4);
  1436. return ret;
  1437. }
  1438. drm_public void drmModeFreeFB2(drmModeFB2Ptr ptr)
  1439. {
  1440. drmFree(ptr);
  1441. }
  1442. drm_public const char *
  1443. drmModeGetConnectorTypeName(uint32_t connector_type)
  1444. {
  1445. /* Keep the strings in sync with the kernel's drm_connector_enum_list in
  1446. * drm_connector.c. */
  1447. switch (connector_type) {
  1448. case DRM_MODE_CONNECTOR_Unknown:
  1449. return "Unknown";
  1450. case DRM_MODE_CONNECTOR_VGA:
  1451. return "VGA";
  1452. case DRM_MODE_CONNECTOR_DVII:
  1453. return "DVI-I";
  1454. case DRM_MODE_CONNECTOR_DVID:
  1455. return "DVI-D";
  1456. case DRM_MODE_CONNECTOR_DVIA:
  1457. return "DVI-A";
  1458. case DRM_MODE_CONNECTOR_Composite:
  1459. return "Composite";
  1460. case DRM_MODE_CONNECTOR_SVIDEO:
  1461. return "SVIDEO";
  1462. case DRM_MODE_CONNECTOR_LVDS:
  1463. return "LVDS";
  1464. case DRM_MODE_CONNECTOR_Component:
  1465. return "Component";
  1466. case DRM_MODE_CONNECTOR_9PinDIN:
  1467. return "DIN";
  1468. case DRM_MODE_CONNECTOR_DisplayPort:
  1469. return "DP";
  1470. case DRM_MODE_CONNECTOR_HDMIA:
  1471. return "HDMI-A";
  1472. case DRM_MODE_CONNECTOR_HDMIB:
  1473. return "HDMI-B";
  1474. case DRM_MODE_CONNECTOR_TV:
  1475. return "TV";
  1476. case DRM_MODE_CONNECTOR_eDP:
  1477. return "eDP";
  1478. case DRM_MODE_CONNECTOR_VIRTUAL:
  1479. return "Virtual";
  1480. case DRM_MODE_CONNECTOR_DSI:
  1481. return "DSI";
  1482. case DRM_MODE_CONNECTOR_DPI:
  1483. return "DPI";
  1484. case DRM_MODE_CONNECTOR_WRITEBACK:
  1485. return "Writeback";
  1486. case DRM_MODE_CONNECTOR_SPI:
  1487. return "SPI";
  1488. case DRM_MODE_CONNECTOR_USB:
  1489. return "USB";
  1490. default:
  1491. return NULL;
  1492. }
  1493. }
  1494. drm_public int
  1495. drmModeCreateDumbBuffer(int fd, uint32_t width, uint32_t height, uint32_t bpp,
  1496. uint32_t flags, uint32_t *handle, uint32_t *pitch,
  1497. uint64_t *size)
  1498. {
  1499. int ret;
  1500. struct drm_mode_create_dumb create = {
  1501. .width = width,
  1502. .height = height,
  1503. .bpp = bpp,
  1504. .flags = flags,
  1505. };
  1506. ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_CREATE_DUMB, &create);
  1507. if (ret != 0)
  1508. return ret;
  1509. *handle = create.handle;
  1510. *pitch = create.pitch;
  1511. *size = create.size;
  1512. return 0;
  1513. }
  1514. drm_public int
  1515. drmModeDestroyDumbBuffer(int fd, uint32_t handle)
  1516. {
  1517. struct drm_mode_destroy_dumb destroy = {
  1518. .handle = handle,
  1519. };
  1520. return DRM_IOCTL(fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy);
  1521. }
  1522. drm_public int
  1523. drmModeMapDumbBuffer(int fd, uint32_t handle, uint64_t *offset)
  1524. {
  1525. int ret;
  1526. struct drm_mode_map_dumb map = {
  1527. .handle = handle,
  1528. };
  1529. ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_MAP_DUMB, &map);
  1530. if (ret != 0)
  1531. return ret;
  1532. *offset = map.offset;
  1533. return 0;
  1534. }