1beacf11bSopenharmony_ci/****************************************************************************
2beacf11bSopenharmony_ci * drivers/pipes/pipe_common.h
3beacf11bSopenharmony_ci *
4beacf11bSopenharmony_ci * Licensed to the Apache Software Foundation (ASF) under one or more
5beacf11bSopenharmony_ci * contributor license agreements.  See the NOTICE file distributed with
6beacf11bSopenharmony_ci * this work for additional information regarding copyright ownership.  The
7beacf11bSopenharmony_ci * ASF licenses this file to you under the Apache License, Version 2.0 (the
8beacf11bSopenharmony_ci * "License"); you may not use this file except in compliance with the
9beacf11bSopenharmony_ci * License.  You may obtain a copy of the License at
10beacf11bSopenharmony_ci *
11beacf11bSopenharmony_ci *   http://www.apache.org/licenses/LICENSE-2.0
12beacf11bSopenharmony_ci *
13beacf11bSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
14beacf11bSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15beacf11bSopenharmony_ci * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
16beacf11bSopenharmony_ci * License for the specific language governing permissions and limitations
17beacf11bSopenharmony_ci * under the License.
18beacf11bSopenharmony_ci *
19beacf11bSopenharmony_ci ****************************************************************************/
20beacf11bSopenharmony_ci
21beacf11bSopenharmony_ci#ifndef __DRIVERS_PIPES_PIPE_COMMON_H
22beacf11bSopenharmony_ci#define __DRIVERS_PIPES_PIPE_COMMON_H
23beacf11bSopenharmony_ci
24beacf11bSopenharmony_ci/****************************************************************************
25beacf11bSopenharmony_ci * Included Files
26beacf11bSopenharmony_ci ****************************************************************************/
27beacf11bSopenharmony_ci#include "vnode.h"
28beacf11bSopenharmony_ci#include <sys/types.h>
29beacf11bSopenharmony_ci#include <limits.h>
30beacf11bSopenharmony_ci#include <stdint.h>
31beacf11bSopenharmony_ci#include <stdbool.h>
32beacf11bSopenharmony_ci#include <poll.h>
33beacf11bSopenharmony_ci
34beacf11bSopenharmony_ci/****************************************************************************
35beacf11bSopenharmony_ci * Pre-processor Definitions
36beacf11bSopenharmony_ci ****************************************************************************/
37beacf11bSopenharmony_ci
38beacf11bSopenharmony_ci/* Pipe/FIFO support */
39beacf11bSopenharmony_ci
40beacf11bSopenharmony_ci/* Pipe/FIFO size */
41beacf11bSopenharmony_ci
42beacf11bSopenharmony_ci#ifndef MAX_READ_WRITE_LEN
43beacf11bSopenharmony_ci#  define MAX_READ_WRITE_LEN 0x1000000
44beacf11bSopenharmony_ci#endif
45beacf11bSopenharmony_ci
46beacf11bSopenharmony_ci#ifndef CONFIG_DEV_PIPE_MAXSIZE
47beacf11bSopenharmony_ci#  define CONFIG_DEV_PIPE_MAXSIZE 1024
48beacf11bSopenharmony_ci#endif
49beacf11bSopenharmony_ci
50beacf11bSopenharmony_ci#if CONFIG_DEV_PIPE_MAXSIZE <= 0
51beacf11bSopenharmony_ci#  undef CONFIG_PIPES
52beacf11bSopenharmony_ci#  undef CONFIG_DEV_PIPE_SIZE
53beacf11bSopenharmony_ci#  undef CONFIG_DEV_FIFO_SIZE
54beacf11bSopenharmony_ci#  define CONFIG_DEV_PIPE_SIZE 0
55beacf11bSopenharmony_ci#  define CONFIG_DEV_FIFO_SIZE 0
56beacf11bSopenharmony_ci#endif
57beacf11bSopenharmony_ci
58beacf11bSopenharmony_ci#ifndef CONFIG_DEV_PIPE_SIZE
59beacf11bSopenharmony_ci#  define CONFIG_DEV_PIPE_SIZE 1024
60beacf11bSopenharmony_ci#endif
61beacf11bSopenharmony_ci
62beacf11bSopenharmony_ci#ifndef CONFIG_DEV_FIFO_SIZE
63beacf11bSopenharmony_ci#  define CONFIG_DEV_FIFO_SIZE 1024
64beacf11bSopenharmony_ci#endif
65beacf11bSopenharmony_ci
66beacf11bSopenharmony_ci/* Maximum number of threads than can be waiting for POLL events */
67beacf11bSopenharmony_ci
68beacf11bSopenharmony_ci#ifndef CONFIG_DEV_PIPE_NPOLLWAITERS
69beacf11bSopenharmony_ci#  define CONFIG_DEV_PIPE_NPOLLWAITERS 2
70beacf11bSopenharmony_ci#endif
71beacf11bSopenharmony_ci
72beacf11bSopenharmony_ci/* Maximum number of open's supported on pipe */
73beacf11bSopenharmony_ci
74beacf11bSopenharmony_ci#define CONFIG_DEV_PIPE_MAXUSER 255
75beacf11bSopenharmony_ci
76beacf11bSopenharmony_ci/* d_flags values */
77beacf11bSopenharmony_ci
78beacf11bSopenharmony_ci#define PIPE_FLAG_POLICY    (1 << 0) /* Bit 0: Policy=Free buffer when empty */
79beacf11bSopenharmony_ci#define PIPE_FLAG_UNLINKED  (1 << 1) /* Bit 1: The driver has been unlinked */
80beacf11bSopenharmony_ci
81beacf11bSopenharmony_ci#define PIPE_POLICY_0(f)    do { (f) &= ~PIPE_FLAG_POLICY; } while (0)
82beacf11bSopenharmony_ci#define PIPE_POLICY_1(f)    do { (f) |= PIPE_FLAG_POLICY; } while (0)
83beacf11bSopenharmony_ci#define PIPE_IS_POLICY_0(f) (((f) & PIPE_FLAG_POLICY) == 0)
84beacf11bSopenharmony_ci#define PIPE_IS_POLICY_1(f) (((f) & PIPE_FLAG_POLICY) != 0)
85beacf11bSopenharmony_ci
86beacf11bSopenharmony_ci#define PIPE_UNLINK(f)      do { (f) |= PIPE_FLAG_UNLINKED; } while (0)
87beacf11bSopenharmony_ci#define PIPE_IS_UNLINKED(f) (((f) & PIPE_FLAG_UNLINKED) != 0)
88beacf11bSopenharmony_ci
89beacf11bSopenharmony_ci
90beacf11bSopenharmony_ci/****************************************************************************
91beacf11bSopenharmony_ci * Public Types
92beacf11bSopenharmony_ci ****************************************************************************/
93beacf11bSopenharmony_ci
94beacf11bSopenharmony_ci/* Make the buffer index as small as possible for the configured pipe size */
95beacf11bSopenharmony_ci
96beacf11bSopenharmony_ci#if CONFIG_DEV_PIPE_MAXSIZE > 65535
97beacf11bSopenharmony_citypedef uint32_t pipe_ndx_t;  /* 32-bit index */
98beacf11bSopenharmony_ci#elif CONFIG_DEV_PIPE_MAXSIZE > 255
99beacf11bSopenharmony_citypedef uint16_t pipe_ndx_t;  /* 16-bit index */
100beacf11bSopenharmony_ci#else
101beacf11bSopenharmony_citypedef uint8_t pipe_ndx_t;   /*  8-bit index */
102beacf11bSopenharmony_ci#endif
103beacf11bSopenharmony_ci
104beacf11bSopenharmony_ci/* This structure represents the state of one pipe.  A reference to this
105beacf11bSopenharmony_ci * structure is retained in the i_private field of the inode whenthe pipe/fifo
106beacf11bSopenharmony_ci * device is registered.
107beacf11bSopenharmony_ci */
108beacf11bSopenharmony_ci
109beacf11bSopenharmony_cistruct pipe_dev_s
110beacf11bSopenharmony_ci{
111beacf11bSopenharmony_ci  char       name[PATH_MAX + 1];
112beacf11bSopenharmony_ci  sem_t      d_bfsem;       /* Used to serialize access to d_buffer and indices */
113beacf11bSopenharmony_ci  sem_t      d_rdsem;       /* Empty buffer - Reader waits for data write */
114beacf11bSopenharmony_ci  sem_t      d_wrsem;       /* Full buffer - Writer waits for data read */
115beacf11bSopenharmony_ci  pipe_ndx_t d_wrndx;       /* Index in d_buffer to save next byte written */
116beacf11bSopenharmony_ci  pipe_ndx_t d_rdndx;       /* Index in d_buffer to return the next byte read */
117beacf11bSopenharmony_ci  pipe_ndx_t d_bufsize;     /* allocated size of d_buffer in bytes */
118beacf11bSopenharmony_ci  uint8_t    d_nwriters;    /* Number of reference counts for write access */
119beacf11bSopenharmony_ci  uint8_t    d_nreaders;    /* Number of reference counts for read access */
120beacf11bSopenharmony_ci  uint8_t    d_pipeno;      /* Pipe minor number */
121beacf11bSopenharmony_ci  uint8_t    d_flags;       /* See PIPE_FLAG_* definitions */
122beacf11bSopenharmony_ci  uint8_t   *d_buffer;      /* Buffer allocated when device opened */
123beacf11bSopenharmony_ci  wait_queue_head_t wq;     /* It is a list if poll structures of threads waiting for driver events */
124beacf11bSopenharmony_ci};
125beacf11bSopenharmony_ci
126beacf11bSopenharmony_ci/****************************************************************************
127beacf11bSopenharmony_ci * Public Function Prototypes
128beacf11bSopenharmony_ci ****************************************************************************/
129beacf11bSopenharmony_ci
130beacf11bSopenharmony_ci#ifdef __cplusplus
131beacf11bSopenharmony_ci#  define EXTERN extern "C"
132beacf11bSopenharmony_ciextern "C"
133beacf11bSopenharmony_ci{
134beacf11bSopenharmony_ci#else
135beacf11bSopenharmony_ci#  define EXTERN extern
136beacf11bSopenharmony_ci#endif
137beacf11bSopenharmony_ci
138beacf11bSopenharmony_cistruct file;  /* Forward reference */
139beacf11bSopenharmony_cistruct inode; /* Forward reference */
140beacf11bSopenharmony_ci
141beacf11bSopenharmony_cistruct pipe_dev_s *pipecommon_allocdev(size_t bufsize, const char *name);
142beacf11bSopenharmony_civoid    pipecommon_freedev(struct pipe_dev_s *dev);
143beacf11bSopenharmony_ciint     pipecommon_open(struct file *filep);
144beacf11bSopenharmony_ciint     pipecommon_close(struct file *filep);
145beacf11bSopenharmony_cissize_t pipecommon_read(struct file *, char *, size_t);
146beacf11bSopenharmony_cissize_t pipecommon_write(struct file *, const char *, size_t);
147beacf11bSopenharmony_ciint     pipecommon_ioctl(struct file *filep, int cmd, unsigned long arg);
148beacf11bSopenharmony_ciint     pipecommon_poll(struct file *filep, poll_table *fds);
149beacf11bSopenharmony_ci#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
150beacf11bSopenharmony_ciint     pipecommon_unlink(struct Vnode *vnode);
151beacf11bSopenharmony_ci#endif
152beacf11bSopenharmony_ciint     pipe_init(void);
153beacf11bSopenharmony_ci
154beacf11bSopenharmony_ci#undef EXTERN
155beacf11bSopenharmony_ci#ifdef __cplusplus
156beacf11bSopenharmony_ci}
157beacf11bSopenharmony_ci#endif
158beacf11bSopenharmony_ci
159beacf11bSopenharmony_ci#endif /* __DRIVERS_PIPES_PIPE_COMMON_H */
160