Newer
Older
ubixos / src / sys / devfs / devfs.c
/*****************************************************************************************
 Copyright (c) 2002-2004 The UbixOS Project
 All rights reserved.

 Redistribution and use in source and binary forms, with or without modification, are
 permitted provided that the following conditions are met:

 Redistributions of source code must retain the above copyright notice, this list of
 conditions, the following disclaimer and the list of authors.  Redistributions in binary
 form must reproduce the above copyright notice, this list of conditions, the following
 disclaimer and the list of authors in the documentation and/or other materials provided
 with the distribution. Neither the name of the UbixOS Project nor the names of its
 contributors may be used to endorse or promote products derived from this software
 without specific prior written permission.

 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

 $Id$

*****************************************************************************************/

#include <devfs/devfs.h>
#include <vfs/vfs.h>
#include <sys/device.h>
#include <ubixos/types.h>
#include <ubixos/spinlock.h>
#include <lib/kmalloc.h>
#include <lib/string.h>
#include <lib/kprintf.h>
#include <assert.h>

/* Spinlock for devfs we should start converting to sem/mutex */
static spinLock_t devfsSpinLock = SPIN_LOCK_INITIALIZER;

static void devfs_initialize(vfs_mountPoint_t *mp) {
  struct devfs_info *fsInfo = 0x0;
  
  /* Allocate memory for the fsInfo */
  mp->fsInfo = (struct devfs_info *)kmalloc(sizeof(struct devfs_info));
  assert(mp->fsInfo);
  
  fsInfo = mp->fsInfo;
  fsInfo->deviceList = 0x0;
  
  kprintf("DevFS Initialized\n");
  //Return
  return;
  }

static int devfs_open(char *file,fileDescriptor *fd) {
  struct devfs_info    *fsInfo  = fd->mp->fsInfo;
  struct devfs_devices *tmpDev = 0x0;
  struct device_node   *device = 0x0;

  spinLock(&devfsSpinLock);

  for (tmpDev = fsInfo->deviceList;tmpDev != 0x0;tmpDev = tmpDev->next) {
    if (strcmp(tmpDev->devName,file) == 0x0) {
      switch ((fd->mode & 0x3)) {
        case 0:
        case 1:
          device = device_find(tmpDev->devMajor,tmpDev->devMinor);
          (void *)fd->start = tmpDev;
          fd->size  = device->size;
          break;
        default:
          kprintf("Invalid File Mode\n");
          spinUnlock(&devfsSpinLock);
          return(-1);
          break;
          }
        spinUnlock(&devfsSpinLock);
        return(0x1);
      }
    }
  spinUnlock(&devfsSpinLock);
  return(0x0);
  }

/************************************************************************

Function: int readDevFS(fileDescriptor *fd,char *data,long offset,long size)
Description: Read File Into Data
Notes:

************************************************************************/
static int devfs_read(fileDescriptor *fd,char *data,long offset,long size) {
  int i = 0x0,x = 0x0;
  uInt32 sectors = 0x0;
  uInt16 diff    = 0x0;
  struct device_node   *device = 0x0;
  struct devfs_devices *tmpDev = (void *)fd->start;

  device = device_find(tmpDev->devMajor,tmpDev->devMinor);


  sectors = ((size+511)/512);
  diff    = (offset - ((offset/512)*512));

  for (i=0x0;i<sectors;i++) {
    device->devInfo->read(device->devInfo->info,fd->buffer,i + (offset/512),1);
    for (x=0x0;x<(size - (i*512));x++) {
      if (diff > 0) {
        data[x] = fd->buffer[x + diff];
        }
      else {
        data[x] = fd->buffer[x];
        }
      }
    diff  = 0x0;
    data += 512;
    }
  
  return(size);
  }

/************************************************************************

Function: int writeDevFS(fileDescriptor *fd,char *data,long offset,long size)
Description: Write Data Into File
Notes:

************************************************************************/
static int devfs_write(fileDescriptor *fd,char *data,long offset,long size) {
  int i = 0x0,x = 0x0;
  struct device_node   *device = 0x0;
  struct devfs_devices *tmpDev = (void *)fd->start;

  device = device_find(tmpDev->devMajor,tmpDev->devMinor);
  for (i=0x0;i<((size+511)/512);i++) {
    device->devInfo->read(device->devInfo->info,fd->buffer,i + (offset/512),1);
    for (x=0x0;((x < 512) && ((x + (i * 512))  < size));x++) {
      fd->buffer[x] = data[x];
      }
    device->devInfo->write(device->devInfo->info,fd->buffer,i + (offset/512),1);
    data += 512;
    }
  return(size);
  }

  
int devfs_makeNode(char *name,uInt8 type,uInt16 major,uInt16 minor) {
  vfs_mountPoint_t  *mp     = 0x0;
  struct devfs_info    *fsInfo = 0x0;
  struct devfs_devices *tmpDev = 0x0;

  spinLock(&devfsSpinLock);
  
  mp = vfs_findMount("devfs");
  
  if (mp == 0x0) {
    kprintf("Error: Can't Find Mount Point\n");
    spinUnlock(&devfsSpinLock);
    return(-1);
    }
    
  fsInfo = mp->fsInfo;

  tmpDev = (struct devfs_devices *)kmalloc(sizeof(struct devfs_devices));

  tmpDev->devType  = type;
  tmpDev->devMajor = major;
  tmpDev->devMinor = minor;
  sprintf(tmpDev->devName,name);

  tmpDev->next = fsInfo->deviceList;
  tmpDev->prev = 0x0;
  if (fsInfo->deviceList != 0x0) {
    fsInfo->deviceList->prev = tmpDev;
    }
    
  fsInfo->deviceList       = tmpDev;
  
  spinUnlock(&devfsSpinLock);
  return(0x0);
  }  
  
int devfs_init() {
  /* Build our devfs struct */
  struct fileSystem devFS =
   {NULL,                         /* prev        */
    NULL,                         /* next        */
    (void *)devfs_initialize,     /* vfsInitFS   */
    (void *)devfs_read,           /* vfsRead     */
    (void *)devfs_write,          /* vfsWrite    */
    (void *)devfs_open,           /* vfsOpenFile */
    NULL,                         /* vfsUnlink   */
    NULL,                         /* vfsMakeDir  */
    NULL,                         /* vfsRemDir   */
    NULL,                         /* vfsSync     */
    1                             /* vfsType     */
   }; /* devFS */

    if (vfsRegisterFS(devFS) != 0x0) {
    //sysErr(systemErr,"Unable To Enable DevFS");
    return(0x1);
    }
  /* Mount our devfs this will build the devfs container node */
  vfs_mount(0x0,0x0,0x0,0x1,"devfs","rw"); // Mount Device File System
  
  /* Return */
  return(0x0);
  }

/***
 $Log$
 Revision 1.14  2004/07/21 10:02:09  reddawg
 devfs: renamed functions
 device system: renamed functions
 fdc: fixed a few potential bugs and cleaned up some unused variables
 strol: fixed definition
 endtask: made it print out freepage debug info
 kmalloc: fixed a huge memory leak we had some unhandled descriptor insertion so some descriptors were lost
 ld: fixed a pointer conversion
 file: cleaned up a few unused variables
 sched: broke task deletion
 kprintf: fixed ogPrintf definition

 Revision 1.13  2004/07/20 22:42:34  reddawg
 Fixed up any bugs I could find now to move onto bigger things

 Revision 1.12  2004/07/16 20:17:29  flameshadow
 chg: broke the ufs stuff
 chg: changed vfsRegisterFS() to accept a fileSystem struct
 chg: modified calls to vfsRegisterFS() to pass fileSystem struct

 Revision 1.11  2004/07/14 12:17:52  reddawg
 devfs: devFSEnable to devfs_init
 Changed Startup Routines

 Revision 1.10  2004/06/28 23:12:58  reddawg
 file format now container:/path/to/file

 Revision 1.9  2004/05/26 11:55:25  reddawg
 devfs: Added spin locks to fix some possible reentrant issues

 Revision 1.8  2004/05/19 15:31:27  reddawg
 Fixed up the rest of the references

 Revision 1.7  2004/05/19 04:07:42  reddawg
 kmalloc(size,pid) no more it is no kmalloc(size); the way it should of been

 Revision 1.6  2004/04/28 21:10:40  reddawg
 Lots Of changes to make it work with existing os

 Revision 1.5  2004/04/28 03:05:35  reddawg
 Cleaned up some debug'n code

 Revision 1.4  2004/04/28 02:37:34  reddawg
 More updates for using the new driver subsystem

 Revision 1.3  2004/04/28 02:22:54  reddawg
 This is a fiarly large commit but we are starting to use new driver model
 all around

 Revision 1.2  2004/04/26 22:22:33  reddawg
 DevFS now uses correct size of device

 Revision 1.1.1.1  2004/04/15 12:06:54  reddawg
 UbixOS v1.0

 Revision 1.3  2004/04/13 16:36:33  reddawg
 Changed our copyright, it is all now under a BSD-Style license

 END
 ***/