| 1 | 2 | alfik | /******************************************************************************
 | 
      
         | 2 |  |  | *                                                                             *
 | 
      
         | 3 |  |  | * License Agreement                                                           *
 | 
      
         | 4 |  |  | *                                                                             *
 | 
      
         | 5 |  |  | * Copyright (c) 2004 Altera Corporation, San Jose, California, USA.           *
 | 
      
         | 6 |  |  | * All rights reserved.                                                        *
 | 
      
         | 7 |  |  | *                                                                             *
 | 
      
         | 8 |  |  | * Permission is hereby granted, free of charge, to any person obtaining a     *
 | 
      
         | 9 |  |  | * copy of this software and associated documentation files (the "Software"),  *
 | 
      
         | 10 |  |  | * to deal in the Software without restriction, including without limitation   *
 | 
      
         | 11 |  |  | * the rights to use, copy, modify, merge, publish, distribute, sublicense,    *
 | 
      
         | 12 |  |  | * and/or sell copies of the Software, and to permit persons to whom the       *
 | 
      
         | 13 |  |  | * Software is furnished to do so, subject to the following conditions:        *
 | 
      
         | 14 |  |  | *                                                                             *
 | 
      
         | 15 |  |  | * The above copyright notice and this permission notice shall be included in  *
 | 
      
         | 16 |  |  | * all copies or substantial portions of the Software.                         *
 | 
      
         | 17 |  |  | *                                                                             *
 | 
      
         | 18 |  |  | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR  *
 | 
      
         | 19 |  |  | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,    *
 | 
      
         | 20 |  |  | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
 | 
      
         | 21 |  |  | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER      *
 | 
      
         | 22 |  |  | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING     *
 | 
      
         | 23 |  |  | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER         *
 | 
      
         | 24 |  |  | * DEALINGS IN THE SOFTWARE.                                                   *
 | 
      
         | 25 |  |  | *                                                                             *
 | 
      
         | 26 |  |  | * This agreement shall be governed in all respects by the laws of the State   *
 | 
      
         | 27 |  |  | * of California and by the laws of the United States of America.              *
 | 
      
         | 28 |  |  | *                                                                             *
 | 
      
         | 29 |  |  | * Altera does not recommend, suggest or require that this reference design    *
 | 
      
         | 30 |  |  | * file be used in conjunction or combination with any other product.          *
 | 
      
         | 31 |  |  | ******************************************************************************/
 | 
      
         | 32 |  |  |  
 | 
      
         | 33 |  |  | #include "sys/alt_errno.h"
 | 
      
         | 34 |  |  | #include "sys/alt_warning.h"
 | 
      
         | 35 |  |  | #include "priv/alt_file.h"
 | 
      
         | 36 |  |  | #include "alt_types.h"
 | 
      
         | 37 |  |  | #include "os/alt_syscall.h"
 | 
      
         | 38 |  |  |  
 | 
      
         | 39 |  |  | #ifdef ALT_USE_DIRECT_DRIVERS
 | 
      
         | 40 |  |  |  
 | 
      
         | 41 |  |  | int ALT_OPEN (const char* file, int flags, int mode)
 | 
      
         | 42 |  |  | {
 | 
      
         | 43 |  |  |   /* Generate a link time warning, should this function ever be called. */
 | 
      
         | 44 |  |  |  
 | 
      
         | 45 |  |  |   ALT_STUB_WARNING(open);
 | 
      
         | 46 |  |  |  
 | 
      
         | 47 |  |  |   /* Indicate an error */
 | 
      
         | 48 |  |  |  
 | 
      
         | 49 |  |  |   ALT_ERRNO = ENOSYS;
 | 
      
         | 50 |  |  |   return -1;
 | 
      
         | 51 |  |  | }
 | 
      
         | 52 |  |  |  
 | 
      
         | 53 |  |  | #else /* !ALT_USE_DIRECT_DRIVERS */
 | 
      
         | 54 |  |  |  
 | 
      
         | 55 |  |  | extern alt_llist alt_dev_list;
 | 
      
         | 56 |  |  |  
 | 
      
         | 57 |  |  | /*
 | 
      
         | 58 |  |  |  * alt_file_locked() is used by open() to ensure that a device has not been
 | 
      
         | 59 |  |  |  * previously locked for exclusive access using ioctl(). This test is only
 | 
      
         | 60 |  |  |  * performed for devices. Filesystems are required to handle the ioctl() call
 | 
      
         | 61 |  |  |  * themselves, and report the error from the filesystems open() function.
 | 
      
         | 62 |  |  |  */
 | 
      
         | 63 |  |  |  
 | 
      
         | 64 |  |  | static int alt_file_locked (alt_fd* fd)
 | 
      
         | 65 |  |  | {
 | 
      
         | 66 |  |  |   alt_u32 i;
 | 
      
         | 67 |  |  |  
 | 
      
         | 68 |  |  |   /*
 | 
      
         | 69 |  |  |    * Mark the file descriptor as belonging to a device.
 | 
      
         | 70 |  |  |    */
 | 
      
         | 71 |  |  |  
 | 
      
         | 72 |  |  |   fd->fd_flags |= ALT_FD_DEV;
 | 
      
         | 73 |  |  |  
 | 
      
         | 74 |  |  |   /*
 | 
      
         | 75 |  |  |    * Loop through all current file descriptors searching for one that's locked
 | 
      
         | 76 |  |  |    * for exclusive access. If a match is found, generate an error.
 | 
      
         | 77 |  |  |    */
 | 
      
         | 78 |  |  |  
 | 
      
         | 79 |  |  |   for (i = 0; i <= alt_max_fd; i++)
 | 
      
         | 80 |  |  |   {
 | 
      
         | 81 |  |  |     if ((alt_fd_list[i].dev == fd->dev) &&
 | 
      
         | 82 |  |  |         (alt_fd_list[i].fd_flags & ALT_FD_EXCL) &&
 | 
      
         | 83 |  |  |         (&alt_fd_list[i] != fd))
 | 
      
         | 84 |  |  |     {
 | 
      
         | 85 |  |  |       return -EACCES;
 | 
      
         | 86 |  |  |     }
 | 
      
         | 87 |  |  |   }
 | 
      
         | 88 |  |  |  
 | 
      
         | 89 |  |  |   /* The device is not locked */
 | 
      
         | 90 |  |  |  
 | 
      
         | 91 |  |  |   return 0;
 | 
      
         | 92 |  |  | }
 | 
      
         | 93 |  |  |  
 | 
      
         | 94 |  |  | /*
 | 
      
         | 95 |  |  |  * open() is called in order to get a file descriptor that reference the file
 | 
      
         | 96 |  |  |  * or device named "name". This descriptor can then be used to manipulate the
 | 
      
         | 97 |  |  |  * file/device using the standard system calls, e.g. write(), read(), ioctl()
 | 
      
         | 98 |  |  |  * etc.
 | 
      
         | 99 |  |  |  *
 | 
      
         | 100 |  |  |  * This is equivalent to the standard open() system call.
 | 
      
         | 101 |  |  |  *
 | 
      
         | 102 |  |  |  * ALT_OPEN is mapped onto the open() system call in alt_syscall.h
 | 
      
         | 103 |  |  |  */
 | 
      
         | 104 |  |  |  
 | 
      
         | 105 |  |  | int ALT_OPEN (const char* file, int flags, int mode)
 | 
      
         | 106 |  |  | {
 | 
      
         | 107 |  |  |   alt_dev* dev;
 | 
      
         | 108 |  |  |   alt_fd*  fd;
 | 
      
         | 109 |  |  |   int index  = -1;
 | 
      
         | 110 |  |  |   int status = -ENODEV;
 | 
      
         | 111 |  |  |   int isafs = 0;
 | 
      
         | 112 |  |  |  
 | 
      
         | 113 |  |  |   /*
 | 
      
         | 114 |  |  |    * Check the device list, to see if a device with a matching name is
 | 
      
         | 115 |  |  |    * registered.
 | 
      
         | 116 |  |  |    */
 | 
      
         | 117 |  |  |  
 | 
      
         | 118 |  |  |   if (!(dev = alt_find_dev (file, &alt_dev_list)))
 | 
      
         | 119 |  |  |   {
 | 
      
         | 120 |  |  |     /* No matching device, so try the filesystem list */
 | 
      
         | 121 |  |  |  
 | 
      
         | 122 |  |  |     dev   = alt_find_file (file);
 | 
      
         | 123 |  |  |     isafs = 1;
 | 
      
         | 124 |  |  |   }
 | 
      
         | 125 |  |  |  
 | 
      
         | 126 |  |  |   /*
 | 
      
         | 127 |  |  |    * If a matching device or filesystem is found, allocate a file descriptor.
 | 
      
         | 128 |  |  |    */
 | 
      
         | 129 |  |  |  
 | 
      
         | 130 |  |  |   if (dev)
 | 
      
         | 131 |  |  |   {
 | 
      
         | 132 |  |  |     if ((index = alt_get_fd (dev)) < 0)
 | 
      
         | 133 |  |  |     {
 | 
      
         | 134 |  |  |       status = index;
 | 
      
         | 135 |  |  |     }
 | 
      
         | 136 |  |  |     else
 | 
      
         | 137 |  |  |     {
 | 
      
         | 138 |  |  |       fd = &alt_fd_list[index];
 | 
      
         | 139 |  |  |       fd->fd_flags = (flags & ~ALT_FD_FLAGS_MASK);
 | 
      
         | 140 |  |  |  
 | 
      
         | 141 |  |  |       /* If this is a device, ensure it isn't already locked */
 | 
      
         | 142 |  |  |  
 | 
      
         | 143 |  |  |       if (isafs || ((status = alt_file_locked (fd)) >= 0))
 | 
      
         | 144 |  |  |       {
 | 
      
         | 145 |  |  |         /*
 | 
      
         | 146 |  |  |          * If the device or filesystem provides an open() callback function,
 | 
      
         | 147 |  |  |          * call it now to perform any device/filesystem specific operations.
 | 
      
         | 148 |  |  |          */
 | 
      
         | 149 |  |  |  
 | 
      
         | 150 |  |  |         status = (dev->open) ? dev->open(fd, file, flags, mode): 0;
 | 
      
         | 151 |  |  |       }
 | 
      
         | 152 |  |  |     }
 | 
      
         | 153 |  |  |   }
 | 
      
         | 154 |  |  |   else
 | 
      
         | 155 |  |  |   {
 | 
      
         | 156 |  |  |     status = -ENODEV;
 | 
      
         | 157 |  |  |   }
 | 
      
         | 158 |  |  |  
 | 
      
         | 159 |  |  |   /* Allocation failed, so clean up and return an error */
 | 
      
         | 160 |  |  |  
 | 
      
         | 161 |  |  |   if (status < 0)
 | 
      
         | 162 |  |  |   {
 | 
      
         | 163 |  |  |     alt_release_fd (index);
 | 
      
         | 164 |  |  |     ALT_ERRNO = -status;
 | 
      
         | 165 |  |  |     return -1;
 | 
      
         | 166 |  |  |   }
 | 
      
         | 167 |  |  |  
 | 
      
         | 168 |  |  |   /* return the reference upon success */
 | 
      
         | 169 |  |  |  
 | 
      
         | 170 |  |  |   return index;
 | 
      
         | 171 |  |  | }
 | 
      
         | 172 |  |  |  
 | 
      
         | 173 |  |  | #endif /* ALT_USE_DIRECT_DRIVERS */
 |