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