Newer
Older
ubixos / src / sys / devfs / devfs.c
@reddawg reddawg on 18 Jun 2004 6 KB UbixOS PreRelease
/*****************************************************************************************
 Copyright (c) 2002 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.

 $Log$
 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



 $Id$

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

#include <devfs/devfs.h>
#include <vfs/vfs.h>
#include <vfs/mount.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>


static spinLock_t devfsSpinLock = SPIN_LOCK_INITIALIZER;


int devFSEnable() {
  //Add DevFS   i will
  if (vfsRegisterFS(1,devFSInit,devFSRead,devFSWrite,devFSOpen,0x0,0x0,0x0,0x0) != 0x0) {
    //sysErr(systemErr,"Unable To Enable DevFS");
    return(0x1);
    }
  //Return
  mount(0x0,0x0,0x0,0x1,"devfs","rw"); // Mount Device File System 
  return(0x0);
  }

void devFSInit(struct mountPoints *mp) {
  struct devFsInfo *fsInfo = 0x0;
  mp->fsInfo = (struct devFsInfo *)kmalloc(sizeof(struct devFsInfo));
  
  fsInfo = mp->fsInfo;
  fsInfo->deviceList = 0x0;
  
  kprintf("DevFS Initialized\n");
  //Return
  return;
  }

int devFSOpen(char *file,fileDescriptor *fd) {
  struct devFsInfo   *fsInfo  = fd->mp->fsInfo;
  struct devFsDevices *tmpDev = 0x0;
  struct deviceNode   *device = 0x0;

  spinLock(&devfsSpinLock);

  for (tmpDev = fsInfo->deviceList;tmpDev != 0x0;tmpDev = tmpDev->next) {
    if (kstrcmp(tmpDev->devName,file) == 0x0) {
      switch ((fd->mode & 0x3)) {
        case 0:
        case 1:
          device = deviceFind(tmpDev->devMajor,tmpDev->devMinor);
          (void *)fd->start = tmpDev;
          (void *)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:

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

  device = deviceFind(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:

************************************************************************/
int devFSWrite(fileDescriptor *fd,char *data,long offset,long size) {
  int i = 0x0,x = 0x0;
  struct deviceNode   *device = 0x0;
  struct devFsDevices *tmpDev = (void *)fd->start;

  device = deviceFind(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 devFsMkNod(char *name,uInt8 type,uInt16 major,uInt16 minor) {
  struct mountPoints  *mp     = 0x0;
  struct devFsInfo    *fsInfo = 0x0;
  struct devFsDevices *tmpDev = 0x0;

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

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

  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);
  }