diff --git a/files.c b/files.c index 93f0c8c..52eaac1 100644 --- a/files.c +++ b/files.c @@ -26,11 +26,10 @@ static int do_cmd(u32 cmd, u32 data) /* generic open function for all pipe-like files */ static int fpgafs_open(struct inode *inode, struct file *file) { - //struct fpgafs_inode_info *i = FPGAFS_I(inode); - //file->private_data = i->i_ctx; + struct fpgafs_inode_info *i = FPGAFS_I(inode); + file->private_data = i->i_ctx; - //return nonseekable_open(inode, file); - return 0; + return nonseekable_open(inode, file); } static ssize_t fpgafs_stat_read(struct file *file, char __user *buf, diff --git a/fpgafs.h b/fpgafs.h index d37504e..bc5f094 100644 --- a/fpgafs.h +++ b/fpgafs.h @@ -15,8 +15,8 @@ enum { }; struct fpga_context { - int bla1; - int bla2; + char *load_buf; + int bla; }; struct fpgafs_inode_info { @@ -40,11 +40,11 @@ struct fpgafs_lldrv { int (*init) (void); int (*exit) (void); - int (*send) (const char __user *buf, int len); - int (*recv) (unsigned char *buf, int len); + int (*send) (struct fpga_context *ctx, const char __user *buf, int len); + int (*recv) (struct fpga_context *ctx, unsigned char *buf, int len); - int (*read_load) (unsigned char *buf, int len); - int (*write_load) (const char __user *buf, int len); + int (*read_load) (struct fpga_context *ctx, unsigned char *buf, int len); + int (*write_load) (struct fpga_context *ctx, const char __user *buf, int len); }; int fpgafs_register_lldrv(struct fpgafs_lldrv *drv); @@ -60,4 +60,6 @@ ssize_t fpgafs_write_load(struct file *file, const char __user *buf, ssize_t fpgafs_read_load(struct file *file, char __user *buf, size_t len, loff_t *pos); +struct fpga_context* alloc_fpga_context(void); +void free_fpga_context(struct fpga_context *ctx); #endif /* FPGAFS_H */ diff --git a/fpgafs_lldrv_cham.c b/fpgafs_lldrv_cham.c index 3327170..566e84d 100644 --- a/fpgafs_lldrv_cham.c +++ b/fpgafs_lldrv_cham.c @@ -6,13 +6,13 @@ #include #include "fpgafs.h" -static int fpgafs_send_data_cham(const char __user *buf, int len) +static int fpgafs_send_data_cham(struct fpga_context *ctx, const char __user *buf, int len) { printk("fpgafs: send data CHAM\n"); return 0; } -static int fpgafs_recv_data_cham(unsigned char *buf, int len) +static int fpgafs_recv_data_cham(struct fpga_context *ctx, unsigned char *buf, int len) { printk("fpgafs: receive data CHAM\n"); return 4; diff --git a/fpgafs_lldrv_dbg.c b/fpgafs_lldrv_dbg.c index 1086998..f457c1b 100644 --- a/fpgafs_lldrv_dbg.c +++ b/fpgafs_lldrv_dbg.c @@ -8,40 +8,37 @@ #include #include "fpgafs.h" -static int load = 0; -static unsigned char *load_buf; - -static int fpgafs_send_data_dbg(const char __user *buf, int len) +static int fpgafs_send_data_dbg(struct fpga_context *ctx, const char __user *buf, int len) { printk("fpgafs: send data DEBUG\n"); return 0; } -static int fpgafs_recv_data_dbg(unsigned char *buf, int len) +static int fpgafs_recv_data_dbg(struct fpga_context *ctx, unsigned char *buf, int len) { printk("fpgafs: receive data DEBUG\n"); return 4; } -static int fpgafs_read_load_dbg(unsigned char *buf, int len) +static int fpgafs_read_load_dbg(struct fpga_context *ctx, unsigned char *buf, int len) { - if (load == 0) + if (ctx->load_buf == NULL) return -EBUSY; - len = len > sizeof(load_buf)?sizeof(load_buf):len; + len = len > sizeof(ctx->load_buf)?sizeof(ctx->load_buf):len; - if (copy_to_user(buf, load_buf, len)) + if (copy_to_user(buf, ctx->load_buf, len)) return -EFAULT; return len; } -static int fpgafs_write_load_dbg(const char __user *buf, int len) +static int fpgafs_write_load_dbg(struct fpga_context *ctx, const char __user *buf, int len) { u32 cp = 0; u8 __user *usr; - if (load == 1) + if (ctx->load_buf != NULL) return -EBUSY; if (len < 2) @@ -50,16 +47,15 @@ static int fpgafs_write_load_dbg(const char __user *buf, int len) if (!access_ok(VERIFY_READ, buf, len)) return -EFAULT; - load_buf = kmalloc(len,GFP_USER); + ctx->load_buf = kmalloc(len,GFP_USER); while (cp < len) { usr = (u8*)&buf[cp]; - if (__get_user(load_buf[cp], usr)) + if (__get_user(ctx->load_buf[cp], usr)) return -EFAULT; cp++; } - load = 1; return len; } @@ -81,8 +77,6 @@ int __init fpgafs_lldrv_dbg_init(void) void __exit fpgafs_lldrv_dbg_exit(void) { - load = 0; - kfree(load_buf); fpgafs_unregister_lldrv(&fpgafs_lldrv_dbg); } diff --git a/inode.c b/inode.c index 89ee957..6881fdc 100644 --- a/inode.c +++ b/inode.c @@ -166,10 +166,11 @@ int fpgafs_mkdir( struct inode *dir, struct dentry *dentry, int mode) inode->i_gid = dir->i_gid; inode->i_mode &= S_ISGID; } - // ctx = alloc fpga context .... - // FPGAFS_I(inode)->i_ctx = ctx; - //if (!ctx) - // goto unmutex; + + ctx = alloc_fpga_context(); + FPGAFS_I(inode)->i_ctx = ctx; + if (!ctx) + goto unmutex; inode->i_op = &fpgafs_simple_dir_inode_operations; inode->i_fop = &simple_dir_operations; @@ -186,7 +187,7 @@ int fpgafs_mkdir( struct inode *dir, struct dentry *dentry, int mode) dir->i_nlink++; dentry->d_inode->i_nlink++; - //unmutex: +unmutex: mutex_unlock(&inode->i_mutex); return ret; } @@ -217,6 +218,7 @@ static void fpgafs_prune_dir(struct dentry *dir) static int fpgafs_rmdir(struct inode *dir, struct dentry *dentry) { /* remove all entries */ + free_fpga_context(FPGAFS_I(dentry->d_inode)->i_ctx); fpgafs_prune_dir(dentry); //fpgafs_remove_inode diff --git a/llmgmt.c b/llmgmt.c index 6a31802..7cd191b 100644 --- a/llmgmt.c +++ b/llmgmt.c @@ -13,31 +13,61 @@ static struct fpgafs_lldrv *lldrv_cur; static int lldrv_count = 0x0; static DEFINE_SPINLOCK(fpgafs_lldrv_lock); +struct fpga_context* alloc_fpga_context(void) +{ + struct fpga_context *ctx; + ctx = kzalloc(sizeof *ctx, GFP_KERNEL); + if (!ctx) + goto out; + + /* do some stuff */ + +out: + return ctx; +} + +void free_fpga_context(struct fpga_context *ctx) +{ + if (ctx->load_buf) + kfree(ctx->load_buf); + + kfree(ctx); + return; +} + ssize_t fpgafs_send_data(struct file *file, const char __user *buf, size_t len, loff_t *pos) { - return (lldrv_cur) ? lldrv_cur->send(buf, len) : -EBUSY; + return (lldrv_cur) ? + lldrv_cur->send((struct fpga_context*)file->private_data, buf, len) + : -EBUSY; } EXPORT_SYMBOL_GPL(fpgafs_send_data); ssize_t fpgafs_recv_data(struct file *file, char __user *buf, size_t len, loff_t *pos) { - return (lldrv_cur) ? lldrv_cur->recv(buf, len) : -EBUSY; + return (lldrv_cur) ? + lldrv_cur->recv((struct fpga_context*)file->private_data, buf, len) + : -EBUSY; } EXPORT_SYMBOL_GPL(fpgafs_recv_data); ssize_t fpgafs_write_load(struct file *file, const char __user *buf, size_t len, loff_t *pos) { - return (lldrv_cur) ? lldrv_cur->write_load(buf, len) : -EBUSY; + return (lldrv_cur) ? + lldrv_cur->write_load((struct fpga_context*)file->private_data, buf, len) + : -EBUSY; } EXPORT_SYMBOL_GPL(fpgafs_write_load); ssize_t fpgafs_read_load(struct file *file, char __user *buf, size_t len, loff_t *pos) { - return (lldrv_cur) ? lldrv_cur->read_load(buf, len) : -EBUSY; + return (lldrv_cur) ? + lldrv_cur->read_load((struct fpga_context*)file->private_data, buf, len) + : -EBUSY; } EXPORT_SYMBOL_GPL(fpgafs_read_load);