/**************************************************************************************
 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.

 $Id: devfs.c,v 1.3 2003/05/04 13:20:15 reddawg Exp $

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

#include <devfs/devfs.h>
#include <ubixos/file.h>
#include <ubixos/mount.h>
#include <sys/video.h>
#include <sys/drives.h>
#include <ubixos/types.h>
#include <ubixos/fs.h>
#include <ubixos/syserr.h>
#include <lib/kmalloc.h>
#include <lib/string.h>

int sprintf(char *buf,const char *fmt, ...); //TEMP

int enableDevFS() {
  //Add DevFS   i will
  if (!addFs(1,initDevFS,readDevFS,writeDevFS,openFileDevFS,0x0,0x0,0x0,0x0)) {
    sysErr(systemErr,"Unable To Enable DevFS");
    return(0);
    }
  //Return
  mount(0x0,0x0,0x1,"devfs","rw"); // Mount Device File System 
  return(1);
  }

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

int openFileDevFS(char *file,fileDescriptor *fd) {
  struct devFsInfo   *fsInfo  = fd->mp->fsInfo;
  struct devFsDevices *tmpDev = 0x0;
  kprintf("Opening DevFS File [0x%X]\n",fsInfo->deviceList);
  for (tmpDev = fsInfo->deviceList;tmpDev != 0x0;tmpDev = tmpDev->next) {
    kprintf("[%s][%s]\n",tmpDev->devName,file);
    if (kstrcmp(tmpDev->devName,file) == 0x0) {
      switch (fd->mode) {
        case 0:
        case 1:
          kprintf("Opened Device: [%s]\n",tmpDev->devName);
          (void *)fd->start = tmpDev;
          break;
        default:
          kprintf("Invalid File Mode\n");
          return(0x0);
          break;
          }
        return(0x1);
      }
    }
  return(0x0);
  }

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

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

************************************************************************/
int readDevFS(fileDescriptor *fd,char *data,long offset,long size) {
  int i = 0x0,x = 0x0;
  uInt32 sectors = 0x0;
  uInt16 diff    = 0x0;
  struct driveDriver *drive = 0x0;
  struct devFsDevices *tmpDev = (void *)fd->start;
  
  kprintf("[0x%X]\n",tmpDev->devMajor);
  drive = findDrive(tmpDev->devMajor);
  kprintf("[0x%X][0x%X]\n",drive,drive->driveInfoStruct);


  kprintf("Reading From Device: [%s]\n",fd->fileName);

  sectors = ((size+511)/512);
  diff    = (offset - ((offset/512)*512));
  
  for (i=0x0;i<sectors;i++) {
    drive->read(drive->driveInfoStruct,i + (offset/512),1,fd->buffer);
    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 writeDevFS(fileDescriptor *fd,char *data,long offset,long size) {
  int i = 0x0,x = 0x0;
  struct driveDriver *drive = 0x0;
  struct devFsDevices *tmpDev = (void *)fd->start;

  kprintf("[0x%X]\n",tmpDev->devMajor);
  drive = findDrive(tmpDev->devMajor);
  kprintf("[0x%X][0x%X]\n",drive,drive->driveInfoStruct);
  
  kprintf("Writing To Device: [%s]\n",fd->fileName);
  for (i=0x0;i<((size+511)/512);i++) {
    drive->read(drive->driveInfoStruct,i + (offset/512),1,fd->buffer);
    for (x=0x0;((x < 512) && ((x + (i * 512))  < size));x++) {
      fd->buffer[x] = data[x];
      }
    drive->write(drive->driveInfoStruct,i + (offset/512),1,fd->buffer);
    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;
  
  mp = findMount("devfs");
  
  if (mp == 0x0) {
    kprintf("Error\n");
    return(0x0);
    }
    
  fsInfo = mp->fsInfo;

  tmpDev = (struct devFsDevices *)kmalloc(sizeof(struct devFsDevices),-2);

  tmpDev->devType  = type;
  tmpDev->devMajor = major;
  tmpDev->devMinor = minor;
  sprintf(tmpDev->devName,name);
  kprintf("tmpDev->devName: [%s]\n",tmpDev->devName);

  tmpDev->next = fsInfo->deviceList;
  tmpDev->prev = 0x0;
  if (fsInfo->deviceList != 0x0) {
    fsInfo->deviceList->prev = tmpDev;
    }
    
  fsInfo->deviceList       = tmpDev;
  
  return(0x1);
  }  