2010-03-23 11:29:48     Problems using a char-device and write some unsigned chars

Document created by Aaronwu Employee on Aug 21, 2013
Version 1Show Document
  • View in full screen mode

2010-03-23 11:29:48     Problems using a char-device and write some unsigned chars

Patrick Hotz (GERMANY)

Message: 87620   

 

Hi,

 

i think this should be very easy to fix.... (but i dont know how)

 

 

 

I have written a char device and want to write some unsigned chars to it...

 

 

 

This is the kernel-code:

 

static ssize_t streamwindow_write(struct file *file, const char __user *buf, size_t count, loff_t *offset)

    {

    int i = 0;

    int bufcount = 0;

    extern u16 framebuffersmall[76800];

  

    char *tmp;

    tmp = kmalloc(153600,GFP_KERNEL);

    copy_from_user(tmp,buf,count);

 

    printk("Device Read (%d Bytes)\n", count);

  

    for(i = 0; i < 51; i++)

        {

        printk("tmp[%05d] = %02x\n", i, tmp[i]);

        }

 

    for(i = 0; i < 76800; i++)

        {

        framebuffersmall[i] = (tmp[bufcount] << 8) | tmp[bufcount + 1];

        bufcount++;

        bufcount++;

        //printk("framebuffersmall[%06d] = %04x\n", i, framebuffersmall[i]);

        }

 

    kfree(tmp);

    s1d13513_writewindow();

    return(count);

    }

 

struct file_operations scmd_driver_fops = {

    .write        = streamwindow_write,

};

 

static struct class *class;

 

static int s1d13513_streamwindow_init(void)

    {

    int ret;

    static int scmd_major = 0; /* For dynamic major number */

 

    ret = register_chrdev(scmd_major, "streamwindow", &scmd_driver_fops);

    if (ret < 0)

        {

        panic("unable to get major for streamwindow device");

        }

 

    scmd_major = ret;

    class = class_create(THIS_MODULE, "s1d13513");

    device_create(class, NULL, MKDEV(scmd_major, 0), "fbstream");

  

    return(0);

    }

 

 

 

and this is my testapplication:

int main()

{

    printf("Start\n");

 

    int i;

    FILE *writefile;

    unsigned char *writetext;

    writetext = (unsigned char*) malloc(153600);

 

    picturefile = fopen("/media/output", "r");

    fread (writetext,1,153600,picturefile);

 

    for(i = 0; i < 50; i++)

        {

        printf("writetext[%06i] = %02x\n", i, writetext[i]);

        }

 

    fclose(picturefile);

 

    sleep(1);

 

    writefile = fopen("/dev/fbstream", "w");

    fwrite(&writetext, 1, 5, writefile);

    fclose(writefile);

 

   exit(0);

 

 

 

The problem is following: If i read the file the printouts in my application looks good (for example "writetext[0] = aa" ,"writetext[1] = 08" and so on).

But the printouts on my kernel-driver looks following: "tmp[00000] = 1609f40", "tmp[00001] = 1609f41" (this looks like adresses....)

 

What can i do to read the correct values??

 

 

 

Best regards,

Patrick

TranslateQuoteReplyEditDelete

 

 

2010-03-23 13:20:43     Re: Problems using a char-device and write some unsigned chars

Rob D (UNITED STATES)

Message: 87624   

 

I use open() instead of fopen() - ie: int fd = open("dev/bfstream", O_RDWR);

 

And I haven't had any weird issues read()'ing or write()'ing to the char device.

QuoteReplyEditDelete

 

 

2010-03-23 18:13:44     Re: Problems using a char-device and write some unsigned chars

Mike Frysinger (UNITED STATES)

Message: 87628   

 

you've declared *tmp as a signed char pointer.  you'll probably want to fix that and declare it unsigned.

Attachments

    Outcomes