1 /**************************************************************************** 2 * drivers/pipe/pipe_common.h 3 * 4 * Copyright (C) 2008-2009, 2015-2016 Gregory Nutt. All rights reserved. 5 * Author: Gregory Nutt <gnutt@nuttx.org> 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the 16 * distribution. 17 * 3. Neither the name NuttX nor the names of its contributors may be 18 * used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 28 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 * POSSIBILITY OF SUCH DAMAGE. 33 * 34 ****************************************************************************/ 35 36 #ifndef __DRIVERS_PIPES_PIPE_COMMON_H 37 #define __DRIVERS_PIPES_PIPE_COMMON_H 38 39 /**************************************************************************** 40 * Included Files 41 ****************************************************************************/ 42 #include "vnode.h" 43 #include <sys/types.h> 44 #include <limits.h> 45 #include <stdint.h> 46 #include <stdbool.h> 47 #include <poll.h> 48 49 /**************************************************************************** 50 * Pre-processor Definitions 51 ****************************************************************************/ 52 53 /* Pipe/FIFO support */ 54 55 /* Pipe/FIFO size */ 56 57 #ifndef MAX_READ_WRITE_LEN 58 # define MAX_READ_WRITE_LEN 0x1000000 59 #endif 60 61 #ifndef CONFIG_DEV_PIPE_MAXSIZE 62 # define CONFIG_DEV_PIPE_MAXSIZE 1024 63 #endif 64 65 #if CONFIG_DEV_PIPE_MAXSIZE <= 0 66 # undef CONFIG_PIPES 67 # undef CONFIG_DEV_PIPE_SIZE 68 # undef CONFIG_DEV_FIFO_SIZE 69 # define CONFIG_DEV_PIPE_SIZE 0 70 # define CONFIG_DEV_FIFO_SIZE 0 71 #endif 72 73 #ifndef CONFIG_DEV_PIPE_SIZE 74 # define CONFIG_DEV_PIPE_SIZE 1024 75 #endif 76 77 #ifndef CONFIG_DEV_FIFO_SIZE 78 # define CONFIG_DEV_FIFO_SIZE 1024 79 #endif 80 81 /* Maximum number of threads than can be waiting for POLL events */ 82 83 #ifndef CONFIG_DEV_PIPE_NPOLLWAITERS 84 # define CONFIG_DEV_PIPE_NPOLLWAITERS 2 85 #endif 86 87 /* Maximum number of open's supported on pipe */ 88 89 #define CONFIG_DEV_PIPE_MAXUSER 255 90 91 /* d_flags values */ 92 93 #define PIPE_FLAG_POLICY (1 << 0) /* Bit 0: Policy=Free buffer when empty */ 94 #define PIPE_FLAG_UNLINKED (1 << 1) /* Bit 1: The driver has been unlinked */ 95 96 #define PIPE_POLICY_0(f) do { (f) &= ~PIPE_FLAG_POLICY; } while (0) 97 #define PIPE_POLICY_1(f) do { (f) |= PIPE_FLAG_POLICY; } while (0) 98 #define PIPE_IS_POLICY_0(f) (((f) & PIPE_FLAG_POLICY) == 0) 99 #define PIPE_IS_POLICY_1(f) (((f) & PIPE_FLAG_POLICY) != 0) 100 101 #define PIPE_UNLINK(f) do { (f) |= PIPE_FLAG_UNLINKED; } while (0) 102 #define PIPE_IS_UNLINKED(f) (((f) & PIPE_FLAG_UNLINKED) != 0) 103 104 105 /**************************************************************************** 106 * Public Types 107 ****************************************************************************/ 108 109 /* Make the buffer index as small as possible for the configured pipe size */ 110 111 #if CONFIG_DEV_PIPE_MAXSIZE > 65535 112 typedef uint32_t pipe_ndx_t; /* 32-bit index */ 113 #elif CONFIG_DEV_PIPE_MAXSIZE > 255 114 typedef uint16_t pipe_ndx_t; /* 16-bit index */ 115 #else 116 typedef uint8_t pipe_ndx_t; /* 8-bit index */ 117 #endif 118 119 /* This structure represents the state of one pipe. A reference to this 120 * structure is retained in the i_private field of the inode whenthe pipe/fifo 121 * device is registered. 122 */ 123 124 struct pipe_dev_s 125 { 126 char name[PATH_MAX + 1]; 127 sem_t d_bfsem; /* Used to serialize access to d_buffer and indices */ 128 sem_t d_rdsem; /* Empty buffer - Reader waits for data write */ 129 sem_t d_wrsem; /* Full buffer - Writer waits for data read */ 130 pipe_ndx_t d_wrndx; /* Index in d_buffer to save next byte written */ 131 pipe_ndx_t d_rdndx; /* Index in d_buffer to return the next byte read */ 132 pipe_ndx_t d_bufsize; /* allocated size of d_buffer in bytes */ 133 uint8_t d_nwriters; /* Number of reference counts for write access */ 134 uint8_t d_nreaders; /* Number of reference counts for read access */ 135 uint8_t d_pipeno; /* Pipe minor number */ 136 uint8_t d_flags; /* See PIPE_FLAG_* definitions */ 137 uint8_t *d_buffer; /* Buffer allocated when device opened */ 138 wait_queue_head_t wq; /* It is a list if poll structures of threads waiting for driver events */ 139 }; 140 141 /**************************************************************************** 142 * Public Function Prototypes 143 ****************************************************************************/ 144 145 #ifdef __cplusplus 146 # define EXTERN extern "C" 147 extern "C" 148 { 149 #else 150 # define EXTERN extern 151 #endif 152 153 struct file; /* Forward reference */ 154 struct inode; /* Forward reference */ 155 156 struct pipe_dev_s *pipecommon_allocdev(size_t bufsize, const char *name); 157 void pipecommon_freedev(struct pipe_dev_s *dev); 158 int pipecommon_open(struct file *filep); 159 int pipecommon_close(struct file *filep); 160 ssize_t pipecommon_read(struct file *, char *, size_t); 161 ssize_t pipecommon_write(struct file *, const char *, size_t); 162 int pipecommon_ioctl(struct file *filep, int cmd, unsigned long arg); 163 int pipecommon_poll(struct file *filep, poll_table *fds); 164 #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS 165 int pipecommon_unlink(struct Vnode *vnode); 166 #endif 167 int pipe_init(void); 168 169 #undef EXTERN 170 #ifdef __cplusplus 171 } 172 #endif 173 174 #endif /* __DRIVERS_PIPES_PIPE_COMMON_H */ 175