bsd_porting.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2023-11-13 Shell Add compatible layer for FreeBSD
  9. */
  10. #ifndef __LWP_TTY_BSD_PORTING_H__
  11. #define __LWP_TTY_BSD_PORTING_H__
  12. #include <rtthread.h>
  13. #include <lwp_internal.h>
  14. #define _KERNEL
  15. #ifndef __unused
  16. #define __unused __attribute__((__unused__))
  17. #endif
  18. /* functionability of bsd tty layer */
  19. #if 0
  20. #define USING_BSD_HOOK
  21. #endif
  22. /* Only for devfs d_close() flags. */
  23. #define FLASTCLOSE O_DIRECTORY
  24. #define FREVOKE 0x00200000
  25. /*
  26. * Output flags - software output processing
  27. */
  28. #if !((OPOST | OLCUC | ONLCR) & 0x8)
  29. #define ONOEOT 0x0008 /* discard EOT's (^D) on output) */
  30. #endif
  31. /*
  32. * Kernel encoding of open mode; separate read and write bits that are
  33. * independently testable: 1 greater than the above.
  34. *
  35. * XXX
  36. * FREAD and FWRITE are excluded from the #ifdef _KERNEL so that TIOCFLUSH,
  37. * which was documented to use FREAD/FWRITE, continues to work.
  38. */
  39. #define FREAD 0x0001
  40. #define FWRITE 0x0002
  41. /*
  42. * Flags to memory allocation functions.
  43. */
  44. #define M_NOWAIT 0x0001 /* do not block */
  45. #define M_WAITOK 0x0002 /* ok to block */
  46. #define M_NORECLAIM 0x0080 /* do not reclaim after failure */
  47. #define M_ZERO 0x0100 /* bzero the allocation */
  48. #define M_NOVM 0x0200 /* don't ask VM for pages */
  49. #define M_USE_RESERVE 0x0400 /* can alloc out of reserve memory */
  50. #define M_NODUMP 0x0800 /* don't dump pages in this allocation */
  51. #define M_FIRSTFIT 0x1000 /* only for vmem, fast fit */
  52. #define M_BESTFIT 0x2000 /* only for vmem, low fragmentation */
  53. #define M_EXEC 0x4000 /* allocate executable space */
  54. #define M_NEXTFIT 0x8000 /* only for vmem, follow cursor */
  55. #define M_VERSION 2020110501
  56. /*
  57. * The INVARIANTS-enabled mtx_assert() functionality.
  58. *
  59. * The constants need to be defined for INVARIANT_SUPPORT infrastructure
  60. * support as _mtx_assert() itself uses them and the latter implies that
  61. * _mtx_assert() must build.
  62. */
  63. #define MA_OWNED (1)
  64. #define MA_NOTOWNED (2)
  65. #define MA_RECURSED (4)
  66. #define MA_NOTRECURSED (8)
  67. /*
  68. * Indentification of modem control signals. These definitions match
  69. * the TIOCMGET definitions in <sys/ttycom.h> shifted a bit down, and
  70. * that identity is enforced with CTASSERT at the bottom of kern/tty.c
  71. * Both the modem bits and delta bits must fit in 16 bit.
  72. */
  73. #define SER_DTR 0x0001 /* data terminal ready */
  74. #define SER_RTS 0x0002 /* request to send */
  75. #define SER_STX 0x0004 /* secondary transmit */
  76. #define SER_SRX 0x0008 /* secondary receive */
  77. #define SER_CTS 0x0010 /* clear to send */
  78. #define SER_DCD 0x0020 /* data carrier detect */
  79. #define SER_RI 0x0040 /* ring indicate */
  80. #define SER_DSR 0x0080 /* data set ready */
  81. #define SER_MASK_STATE 0x00ff
  82. /*
  83. * Flags for ioflag. (high 16 bits used to ask for read-ahead and
  84. * help with write clustering)
  85. * NB: IO_NDELAY and IO_DIRECT are linked to fcntl.h
  86. */
  87. #if 0
  88. #define IO_UNIT 0x0001 /* do I/O as atomic unit */
  89. #define IO_APPEND 0x0002 /* append write to end */
  90. #endif /* not porting */
  91. #define IO_NDELAY 0x0004 /* FNDELAY flag set in file table */
  92. #if 0
  93. #define IO_NODELOCKED 0x0008 /* underlying node already locked */
  94. #define IO_ASYNC 0x0010 /* bawrite rather then bdwrite */
  95. #define IO_VMIO 0x0020 /* data already in VMIO space */
  96. #define IO_INVAL 0x0040 /* invalidate after I/O */
  97. #define IO_SYNC 0x0080 /* do I/O synchronously */
  98. #define IO_DIRECT 0x0100 /* attempt to bypass buffer cache */
  99. #define IO_NOREUSE 0x0200 /* VMIO data won't be reused */
  100. #define IO_EXT 0x0400 /* operate on external attributes */
  101. #define IO_NORMAL 0x0800 /* operate on regular data */
  102. #define IO_NOMACCHECK 0x1000 /* MAC checks unnecessary */
  103. #define IO_BUFLOCKED 0x2000 /* ffs flag; indir buf is locked */
  104. #define IO_RANGELOCKED 0x4000 /* range locked */
  105. #define IO_DATASYNC 0x8000 /* do only data I/O synchronously */
  106. #define IO_SEQMAX 0x7F /* seq heuristic max value */
  107. #define IO_SEQSHIFT 16 /* seq heuristic in upper 16 bits */
  108. #endif /* not porting */
  109. /** Used to distinguish between normal, callout, lock and init devices.
  110. * Note: this is not used in smart system.
  111. */
  112. #define TTYUNIT_INIT 0x1
  113. #define TTYUNIT_LOCK 0x2
  114. #define TTYUNIT_CALLOUT 0x4
  115. /*
  116. * TTY privileges.
  117. */
  118. #define PRIV_TTY_CONSOLE 250 /* Set console to tty. */
  119. #define PRIV_TTY_DRAINWAIT 251 /* Set tty drain wait time. */
  120. #define PRIV_TTY_DTRWAIT 252 /* Set DTR wait on tty. */
  121. #define PRIV_TTY_EXCLUSIVE 253 /* Override tty exclusive flag. */
  122. #define _PRIV_TTY_PRISON 254 /* Removed. */
  123. #define PRIV_TTY_STI 255 /* Simulate input on another tty. */
  124. #define PRIV_TTY_SETA 256 /* Set tty termios structure. */
  125. #define MPASS(ex) RT_ASSERT(ex)
  126. #if !defined(MIN)
  127. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  128. #endif
  129. #if !defined(MAX)
  130. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  131. #endif
  132. #define curthread rt_thread_self()
  133. #ifdef USING_BSD_HOOK
  134. #define ttyhook_hashook(tp, hook) \
  135. ((tp)->t_hook != NULL && (tp)->t_hook->th_##hook != NULL)
  136. #else
  137. #define ttyhook_hashook(tp, hook) (RT_FALSE)
  138. #endif
  139. /* condvar API */
  140. #include <rtdevice.h>
  141. #define cv_init(cvp, name) rt_condvar_init(cvp, name)
  142. #define cv_destroy(cvp) rt_condvar_detach(cvp)
  143. #define cv_wait(cvp, mp) \
  144. rt_condvar_timedwait(cvp, mp, RT_KILLABLE, RT_WAITING_FOREVER)
  145. #define cv_wait_sig(cvp, mp) \
  146. rt_condvar_timedwait(cvp, mp, RT_INTERRUPTIBLE, RT_WAITING_FOREVER)
  147. #define cv_signal(cvp) rt_condvar_signal(cvp)
  148. #define cv_broadcast(cvp) rt_condvar_broadcast(cvp)
  149. #define cv_timedwait(cvp, mp, t) rt_condvar_timedwait(cvp, mp, RT_KILLABLE, t)
  150. #define cv_timedwait_sig(cvp, mp, t) \
  151. rt_condvar_timedwait(cvp, mp, RT_INTERRUPTIBLE, t)
  152. struct lwp_tty;
  153. struct uio;
  154. /* TODO: just a place holder since devfs is not capable of doing this currently
  155. */
  156. struct file
  157. {
  158. };
  159. typedef rt_base_t sbintime_t;
  160. typedef rt_ubase_t vm_offset_t;
  161. typedef rt_base_t vm_ooffset_t;
  162. typedef rt_ubase_t vm_paddr_t;
  163. typedef rt_ubase_t vm_pindex_t;
  164. typedef rt_ubase_t vm_size_t;
  165. typedef char *rt_caddr_t;
  166. /*
  167. * The exact set of memory attributes is machine dependent. However,
  168. * every machine is required to define VM_MEMATTR_DEFAULT and
  169. * VM_MEMATTR_UNCACHEABLE.
  170. */
  171. typedef char vm_memattr_t; /* memory attribute codes */
  172. typedef int d_open_t(struct lwp_tty *tp, int oflags, int devtype,
  173. struct rt_thread *td);
  174. typedef int d_fdopen_t(struct lwp_tty *tp, int oflags, struct rt_thread *td,
  175. struct file *fp);
  176. typedef int d_close_t(struct lwp_tty *tp, int fflag, int devtype,
  177. struct rt_thread *td);
  178. #ifdef USING_BSD_DEVICE_STRATEGY
  179. typedef void d_strategy_t(struct bio *bp);
  180. #endif
  181. typedef int d_ioctl_t(struct lwp_tty *tp, rt_ubase_t cmd, rt_caddr_t data,
  182. int fflag, struct rt_thread *td);
  183. typedef int d_read_t(struct lwp_tty *tp, struct uio *uio, int ioflag);
  184. typedef int d_write_t(struct lwp_tty *tp, struct uio *uio, int ioflag);
  185. typedef int d_poll_t(struct lwp_tty *tp, rt_pollreq_t *req,
  186. struct rt_thread *td);
  187. #ifdef USING_BSD_KNOTE
  188. typedef int d_kqfilter_t(struct lwp_tty *tp, struct knote *kn);
  189. #endif /* USING_BSD_KNOTE */
  190. typedef int d_mmap_t(struct lwp_tty *tp, vm_ooffset_t offset, vm_paddr_t *paddr,
  191. int nprot, vm_memattr_t *memattr);
  192. #ifdef USING_BSD_MMAP_SINGLE
  193. typedef int d_mmap_single_t(struct cdev *cdev, vm_ooffset_t *offset,
  194. vm_size_t size, struct vm_object **object,
  195. int nprot);
  196. #endif /* USING_BSD_MMAP_SINGLE */
  197. typedef void d_purge_t(struct lwp_tty *tp);
  198. /*
  199. * Character device switch table
  200. */
  201. struct cdevsw
  202. {
  203. #ifdef USING_BSD_RAW_CDEVSW
  204. int d_version;
  205. u_int d_flags;
  206. const char *d_name;
  207. #endif /* USING_BSD_RAW_CDEVSW */
  208. d_open_t *d_open;
  209. d_fdopen_t *d_fdopen;
  210. d_close_t *d_close;
  211. d_read_t *d_read;
  212. d_write_t *d_write;
  213. d_ioctl_t *d_ioctl;
  214. d_poll_t *d_poll;
  215. d_mmap_t *d_mmap;
  216. #ifdef USING_BSD_DEVICE_STRATEGY
  217. d_strategy_t *d_strategy;
  218. #endif /* USING_BSD_DEVICE_STRATEGY */
  219. #ifdef USING_BSD_RAW_CDEVSW
  220. void *d_spare0;
  221. d_kqfilter_t *d_kqfilter;
  222. d_purge_t *d_purge;
  223. d_mmap_single_t *d_mmap_single;
  224. int32_t d_spare1[3];
  225. void *d_spare2[3];
  226. /* These fields should not be messed with by drivers */
  227. LIST_HEAD(, cdev) d_devs;
  228. int d_spare3;
  229. union
  230. {
  231. struct cdevsw *gianttrick;
  232. SLIST_ENTRY(cdevsw) postfree_list;
  233. } __d_giant;
  234. #endif
  235. };
  236. struct iovec
  237. {
  238. void *iov_base; /* Base address. */
  239. size_t iov_len; /* Length. */
  240. };
  241. enum uio_rw
  242. {
  243. UIO_READ,
  244. UIO_WRITE
  245. };
  246. struct uio
  247. {
  248. struct iovec *uio_iov; /* scatter/gather list */
  249. int uio_iovcnt; /* length of scatter/gather list */
  250. off_t uio_offset; /* offset in target object */
  251. ssize_t uio_resid; /* remaining bytes to process */
  252. #ifdef USING_BSD_UIO
  253. enum uio_seg uio_segflg; /* address space */
  254. #endif
  255. enum uio_rw uio_rw; /* operation */
  256. #ifdef USING_BSD_UIO
  257. struct rt_thread *uio_td; /* owner */
  258. #endif /* USING_BSD_UIO */
  259. };
  260. #include <lwp_user_mm.h>
  261. rt_inline int uiomove(void *operand, int n, struct uio *uio)
  262. {
  263. switch (uio->uio_rw)
  264. {
  265. case UIO_READ:
  266. memcpy(uio->uio_iov->iov_base, operand, n);
  267. break;
  268. case UIO_WRITE:
  269. memcpy(operand, uio->uio_iov->iov_base, n);
  270. break;
  271. default:
  272. return -1;
  273. }
  274. uio->uio_iov->iov_base += n;
  275. uio->uio_iov->iov_len--;
  276. uio->uio_offset += n;
  277. uio->uio_resid -= n;
  278. return 0;
  279. }
  280. /* privileges checking: 0 if okay */
  281. rt_inline int priv_check(struct rt_thread *td, int priv)
  282. {
  283. return 0;
  284. }
  285. /* Disable console redirection to a tty. */
  286. rt_inline int constty_clear(struct lwp_tty *tp)
  287. {
  288. // rt_kprintf("\nTODO: %s unimplemented!\n", __func__);
  289. return 0;
  290. }
  291. rt_inline int constty_set(struct lwp_tty *tp)
  292. {
  293. // rt_kprintf("\nTODO: %s unimplemented!\n", __func__);
  294. return 0;
  295. }
  296. /**
  297. * UMA (Universal Memory Allocator)
  298. */
  299. #define UMA_ALIGN_PTR (sizeof(void *) - 1) /* Alignment fit for ptr */
  300. typedef int (*uma_ctor)(void *mem, int size, void *arg, int flags);
  301. typedef void (*uma_dtor)(void *mem, int size, void *arg);
  302. typedef int (*uma_init)(void *mem, int size, int flags);
  303. typedef void (*uma_fini)(void *mem, int size);
  304. struct uma_zone
  305. {
  306. char *name;
  307. int align;
  308. int size;
  309. };
  310. /* Opaque type used as a handle to the zone */
  311. typedef struct uma_zone *uma_zone_t;
  312. rt_inline uma_zone_t uma_zcreate(char *name, int size, uma_ctor ctor,
  313. uma_dtor dtor, uma_init zinit, uma_fini zfini,
  314. int align, uint16_t flags)
  315. {
  316. uma_zone_t zone = rt_malloc(sizeof(struct uma_zone));
  317. if (zone)
  318. {
  319. RT_ASSERT(ctor == RT_NULL);
  320. RT_ASSERT(dtor == RT_NULL);
  321. RT_ASSERT(zinit == RT_NULL);
  322. RT_ASSERT(zfini == RT_NULL);
  323. zone->size = size;
  324. zone->name = name;
  325. zone->align = align;
  326. }
  327. return zone;
  328. }
  329. rt_inline void *uma_zalloc(uma_zone_t zone, int flags)
  330. {
  331. void *buf = rt_malloc_align(zone->size, zone->align + 1);
  332. if (buf)
  333. rt_memset(buf, 0, sizeof(zone->size));
  334. return buf;
  335. }
  336. rt_inline void uma_zfree(uma_zone_t zone, void *item)
  337. {
  338. rt_free_align(item);
  339. }
  340. /**
  341. * bsd type of speed to linux type.
  342. * Note: with switch blocks, compiler can generate the optimized version for us
  343. */
  344. #include <termios.h>
  345. rt_inline long bsd_speed_to_integer(speed_t speed)
  346. {
  347. long speed_value;
  348. switch (speed)
  349. {
  350. case B0:
  351. speed_value = 0;
  352. break;
  353. case B50:
  354. speed_value = 50;
  355. break;
  356. case B75:
  357. speed_value = 75;
  358. break;
  359. case B110:
  360. speed_value = 110;
  361. break;
  362. case B134:
  363. speed_value = 134;
  364. break;
  365. case B150:
  366. speed_value = 150;
  367. break;
  368. case B200:
  369. speed_value = 200;
  370. break;
  371. case B300:
  372. speed_value = 300;
  373. break;
  374. case B600:
  375. speed_value = 600;
  376. break;
  377. case B1200:
  378. speed_value = 1200;
  379. break;
  380. case B1800:
  381. speed_value = 1800;
  382. break;
  383. case B2400:
  384. speed_value = 2400;
  385. break;
  386. case B4800:
  387. speed_value = 4800;
  388. break;
  389. case B9600:
  390. speed_value = 9600;
  391. break;
  392. case B19200:
  393. speed_value = 19200;
  394. break;
  395. case B38400:
  396. speed_value = 38400;
  397. break;
  398. case B57600:
  399. speed_value = 57600;
  400. break;
  401. case B115200:
  402. speed_value = 115200;
  403. break;
  404. case B230400:
  405. speed_value = 230400;
  406. break;
  407. case B460800:
  408. speed_value = 460800;
  409. break;
  410. case B500000:
  411. speed_value = 500000;
  412. break;
  413. case B576000:
  414. speed_value = 576000;
  415. break;
  416. case B921600:
  417. speed_value = 921600;
  418. break;
  419. case B1000000:
  420. speed_value = 1000000;
  421. break;
  422. case B1152000:
  423. speed_value = 1152000;
  424. break;
  425. case B1500000:
  426. speed_value = 1500000;
  427. break;
  428. case B2000000:
  429. speed_value = 2000000;
  430. break;
  431. case B2500000:
  432. speed_value = 2500000;
  433. break;
  434. case B3000000:
  435. speed_value = 3000000;
  436. break;
  437. case B3500000:
  438. speed_value = 3500000;
  439. break;
  440. case B4000000:
  441. speed_value = 4000000;
  442. break;
  443. default:
  444. speed_value = -1; // invalid speed
  445. break;
  446. }
  447. return speed_value;
  448. }
  449. /* time.h */
  450. /* Operations on timevals. */
  451. #define timevalclear(tvp) ((tvp)->tv_sec = (tvp)->tv_usec = 0)
  452. #define timevalisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec)
  453. #define timevalcmp(tvp, uvp, cmp) \
  454. (((tvp)->tv_sec == (uvp)->tv_sec) ? ((tvp)->tv_usec cmp(uvp)->tv_usec) \
  455. : ((tvp)->tv_sec cmp(uvp)->tv_sec))
  456. rt_inline void getmicrotime(struct timeval *now)
  457. {
  458. gettimeofday(now, RT_NULL);
  459. }
  460. rt_inline void timevalfix(struct timeval *tv)
  461. {
  462. if (tv->tv_usec < 0)
  463. {
  464. tv->tv_sec--;
  465. tv->tv_usec += 1000000;
  466. }
  467. if (tv->tv_usec >= 1000000)
  468. {
  469. tv->tv_sec++;
  470. tv->tv_usec -= 1000000;
  471. }
  472. }
  473. rt_inline void timevaladd(struct timeval *op1, const struct timeval *op2)
  474. {
  475. op1->tv_sec += op2->tv_sec;
  476. op1->tv_usec += op2->tv_usec;
  477. timevalfix(op1);
  478. }
  479. rt_inline void timevalsub(struct timeval *op1, const struct timeval *op2)
  480. {
  481. op1->tv_sec -= op2->tv_sec;
  482. op1->tv_usec -= op2->tv_usec;
  483. timevalfix(op1);
  484. }
  485. rt_inline rt_tick_t tvtohz(struct timeval *tv)
  486. {
  487. rt_tick_t rc;
  488. rc = tv->tv_sec * RT_TICK_PER_SECOND;
  489. rc += tv->tv_usec * RT_TICK_PER_SECOND / MICROSECOND_PER_SECOND;
  490. return rc;
  491. }
  492. /* ioctl */
  493. #define _BSD_TIOCTL(val) ((val) << 16)
  494. enum bsd_ioctl_cmd
  495. {
  496. BSD_TIOCDRAIN = 1,
  497. BSD_TIOCFLUSH,
  498. BSD_TIOCSTART,
  499. BSD_TIOCSTOP,
  500. BSD_TIOCSTAT,
  501. BSD_TIOCGDRAINWAIT,
  502. BSD_TIOCSDRAINWAIT,
  503. BSD_TIOCSDTR,
  504. BSD_TIOCCDTR,
  505. };
  506. #ifndef TIOCGETA /* get termios struct */
  507. #define TIOCGETA TCGETS
  508. #endif
  509. #ifndef TIOCSETA /* set termios struct */
  510. #define TIOCSETA TCSETS
  511. #endif
  512. #ifndef TIOCSETAW /* drain output, set */
  513. #define TIOCSETAW TCSETSW
  514. #endif
  515. #ifndef TIOCSETAF /* drn out, fls in, set */
  516. #define TIOCSETAF TCSETSF
  517. #endif
  518. #ifndef TIOCDRAIN /* wait till output drained */
  519. #define TIOCDRAIN _BSD_TIOCTL(BSD_TIOCDRAIN)
  520. #endif
  521. #ifndef TIOCFLUSH /* flush buffers */
  522. #define TIOCFLUSH _BSD_TIOCTL(BSD_TIOCFLUSH)
  523. #endif
  524. #ifndef TIOCSTART /* start output, like ^Q */
  525. #define TIOCSTART _BSD_TIOCTL(BSD_TIOCSTART)
  526. #endif
  527. #ifndef TIOCSTOP /* stop output, like ^S */
  528. #define TIOCSTOP _BSD_TIOCTL(BSD_TIOCSTOP)
  529. #endif
  530. #ifndef TIOCSTAT /* simulate ^T status message */
  531. #define TIOCSTAT _BSD_TIOCTL(BSD_TIOCSTAT)
  532. #endif
  533. #ifndef TIOCGDRAINWAIT /* get ttywait timeout */
  534. #define TIOCGDRAINWAIT _BSD_TIOCTL(BSD_TIOCGDRAINWAIT)
  535. #endif
  536. #ifndef TIOCSDRAINWAIT /* set ttywait timeout */
  537. #define TIOCSDRAINWAIT _BSD_TIOCTL(BSD_TIOCSDRAINWAIT)
  538. #endif
  539. #ifndef TIOCSDTR /* set data terminal ready */
  540. #define TIOCSDTR _BSD_TIOCTL(BSD_TIOCSDTR)
  541. #endif
  542. #ifndef TIOCCDTR /* clear data terminal ready */
  543. #define TIOCCDTR _BSD_TIOCTL(BSD_TIOCCDTR)
  544. #endif
  545. #define ENOIOCTL ENOSYS
  546. #define NO_PID -1
  547. /* line discipline */
  548. #define TTYDISC 0 /* termios tty line discipline */
  549. #define SLIPDISC 4 /* serial IP discipline */
  550. #define PPPDISC 5 /* PPP discipline */
  551. #define NETGRAPHDISC 6 /* Netgraph tty node discipline */
  552. #define H4DISC 7 /* Netgraph Bluetooth H4 discipline */
  553. /*
  554. * Control flags - hardware control of terminal
  555. */
  556. #if __BSD_VISIBLE
  557. #define CIGNORE 0x00000001 /* ignore control flags */
  558. #define CCTS_OFLOW 0x00010000 /* CTS flow control of output */
  559. #define CRTSCTS (CCTS_OFLOW | CRTS_IFLOW)
  560. #define CRTS_IFLOW 0x00020000 /* RTS flow control of input */
  561. #define CDTR_IFLOW 0x00040000 /* DTR flow control of input */
  562. #define CDSR_OFLOW 0x00080000 /* DSR flow control of output */
  563. #define CCAR_OFLOW 0x00100000 /* DCD flow control of output */
  564. #define CNO_RTSDTR 0x00200000 /* Do not assert RTS or DTR automatically */
  565. #else
  566. #define CIGNORE 0 /* ignore control flags */
  567. #define CCTS_OFLOW 0 /* CTS flow control of output */
  568. #define CRTS_IFLOW 0 /* RTS flow control of input */
  569. #define CDTR_IFLOW 0 /* DTR flow control of input */
  570. #define CDSR_OFLOW 0 /* DSR flow control of output */
  571. #define CCAR_OFLOW 0 /* DCD flow control of output */
  572. #define CNO_RTSDTR 0 /* Do not assert RTS or DTR automatically */
  573. #endif
  574. #ifndef CRTSCTS
  575. #define CRTSCTS (CCTS_OFLOW | CRTS_IFLOW)
  576. #endif
  577. #ifndef howmany
  578. #define howmany(x, y) (((x) + ((y)-1)) / (y))
  579. #endif
  580. struct ucred
  581. {
  582. };
  583. #define NOCRED ((struct ucred *)0) /* no credential available */
  584. #define FSCRED ((struct ucred *)-1) /* filesystem credential */
  585. /* convert from open() flags to/from fflags; convert O_RD/WR to FREAD/FWRITE */
  586. #include <fcntl.h>
  587. #define FFLAGS(oflags) ((oflags)&O_EXEC ? (oflags) : (oflags) + 1)
  588. #define OFLAGS(fflags) \
  589. (((fflags) & (O_EXEC | O_PATH)) != 0 ? (fflags) : (fflags)-1)
  590. typedef int fo_rdwr_t(struct lwp_tty *tp, struct uio *uio,
  591. struct ucred *active_cred, int flags,
  592. struct rt_thread *td);
  593. typedef int fo_truncate_t(struct lwp_tty *tp, off_t length,
  594. struct ucred *active_cred, struct rt_thread *td);
  595. typedef int fo_ioctl_t(struct lwp_tty *tp, rt_ubase_t com, void *data,
  596. struct ucred *active_cred, int fflags, struct rt_thread *td);
  597. typedef int fo_poll_t(struct lwp_tty *tp, struct rt_pollreq *rq, struct ucred *active_cred,
  598. struct rt_thread *td);
  599. typedef int fo_stat_t(struct lwp_tty *tp, struct stat *sb,
  600. struct ucred *active_cred);
  601. typedef int fo_close_t(struct lwp_tty *tp, struct rt_thread *td);
  602. #ifdef USING_BSD_FO_EXT
  603. typedef int fo_chmod_t(struct file *fp, mode_t mode, struct ucred *active_cred,
  604. struct rt_thread *td);
  605. typedef int fo_chown_t(struct file *fp, uid_t uid, gid_t gid,
  606. struct ucred *active_cred, struct rt_thread *td);
  607. typedef int fo_sendfile_t(struct file *fp, int sockfd, struct uio *hdr_uio,
  608. struct uio *trl_uio, off_t offset, size_t nbytes,
  609. off_t *sent, int flags, struct rt_thread *td);
  610. typedef int fo_seek_t(struct file *fp, off_t offset, int whence,
  611. struct rt_thread *td);
  612. typedef int fo_kqfilter_t(struct file *fp, struct knote *kn);
  613. typedef int fo_fill_kinfo_t(struct file *fp, struct kinfo_file *kif,
  614. struct filedesc *fdp);
  615. typedef int fo_mmap_t(struct file *fp, vm_map_t map, vm_offset_t *addr,
  616. vm_size_t size, vm_prot_t prot, vm_prot_t cap_maxprot,
  617. int flags, vm_ooffset_t foff, struct rt_thread *td);
  618. typedef int fo_aio_queue_t(struct file *fp, struct kaiocb *job);
  619. typedef int fo_add_seals_t(struct file *fp, int flags);
  620. typedef int fo_get_seals_t(struct file *fp, int *flags);
  621. typedef int fo_fallocate_t(struct file *fp, off_t offset, off_t len,
  622. struct rt_thread *td);
  623. typedef int fo_fspacectl_t(struct file *fp, int cmd, off_t *offset,
  624. off_t *length, int flags, struct ucred *active_cred,
  625. struct rt_thread *td);
  626. typedef int fo_spare_t(struct file *fp);
  627. #endif /* USING_BSD_FO_EXT */
  628. typedef int fo_flags_t;
  629. struct bsd_fileops
  630. {
  631. fo_rdwr_t *fo_read;
  632. fo_rdwr_t *fo_write;
  633. fo_truncate_t *fo_truncate;
  634. fo_ioctl_t *fo_ioctl;
  635. fo_poll_t *fo_poll;
  636. fo_stat_t *fo_stat;
  637. fo_close_t *fo_close;
  638. #ifdef USING_BSD_FO_EXT
  639. fo_chmod_t *fo_chmod;
  640. fo_chown_t *fo_chown;
  641. fo_sendfile_t *fo_sendfile;
  642. fo_seek_t *fo_seek;
  643. fo_kqfilter_t *fo_kqfilter;
  644. fo_fill_kinfo_t *fo_fill_kinfo;
  645. fo_mmap_t *fo_mmap;
  646. fo_aio_queue_t *fo_aio_queue;
  647. fo_add_seals_t *fo_add_seals;
  648. fo_get_seals_t *fo_get_seals;
  649. fo_fallocate_t *fo_fallocate;
  650. fo_fspacectl_t *fo_fspacectl;
  651. fo_spare_t *fo_spares[8]; /* Spare slots */
  652. #endif
  653. fo_flags_t fo_flags; /* DFLAG_* below */
  654. };
  655. #define DFLAG_PASSABLE 0x01 /* may be passed via unix sockets. */
  656. #define DFLAG_SEEKABLE 0x02 /* seekable / nonsequential */
  657. #endif /* __LWP_TTY_BSD_PORTING_H__ */