Exemplo n.º 1
0
/*
 * Allocates a block in the IFS block pool with a
 * specified size
 */
static int ifs_block_alloc(struct device *dev, int size)
{
    struct ifs_block blk;
    struct ifs_volume_hdr hdr;

    device_read(dev, &hdr, sizeof(struct ifs_volume_hdr), 0);

    for (int i = 0; i < hdr.block_pool_size; i += sizeof(struct ifs_block)) {
        device_read(dev, &blk, sizeof(struct ifs_block), sizeof(struct ifs_volume_hdr) + i);
        if (blk.state == IFS_BLOCK_NONEXISTENT) {
            blk.size = size;
            blk.state = IFS_BLOCK_ALLOCATED;
            blk.data = hdr.placement_new;
            hdr.placement_new += size;
            device_write(dev, &hdr, sizeof(struct ifs_volume_hdr), 0);
            device_write(dev, &blk, sizeof(struct ifs_block), sizeof(struct ifs_volume_hdr) + i);
            return i / sizeof(struct ifs_block);
        } else if (blk.state == IFS_BLOCK_FREE && blk.size == size) {
            blk.state = IFS_BLOCK_ALLOCATED;
            device_write(dev, &blk, sizeof(struct ifs_block), sizeof(struct ifs_volume_hdr) + i);
            return i / sizeof(struct ifs_block);
        } 
    }
    return -1;
}
Exemplo n.º 2
0
static void _accel_io_write(struct lsm303c_accelerometer *accel,
		uint8_t address, const uint8_t *data, uint16_t size)
{
	/* Set chip select Low at the start of the transmission */
	gpio_clear(accel->cs);

	/* Send the Address of the indexed register */
	device_write(accel->dev, &address, 1);

	/* Send the data that will be written into the device (MSB First) */
	device_write(accel->dev, data, size);

	/* Set chip select High at the end of the transmission */
	gpio_set(accel->cs);
}
Exemplo n.º 3
0
Arquivo: cdev.c Projeto: XAMPP/Gloom
long device_ioctl(struct file* file, unsigned int ioctl_num,
	              unsigned long ioctl_param) {
	int i;
	char* temp;
	char ch;

	switch(ioctl_num) {
		case IOCTL_SET_MSG: {
			temp = (char*)ioctl_param;
			get_user(ch, temp);
			for(i = 0; ch && i < BUFFER_LEN; i++, temp++) {
				get_user(ch, temp);
			}
			device_write(file, (char*)ioctl_param, i, 0);
			break;
		} case IOCTL_GET_MSG: {
			i = device_read(file, (char*)ioctl_param, 99, 0);
			put_user('\0', (char*)ioctl_param + i);
			break;
		} case IOCTL_GET_NTH_BYTE: {
			if(ioctl_param && ioctl_param - 1 < BUFFER_LEN)
				return data[ioctl_param];
			return 0;
			break;
		} default: {
			break;
		}
	}
	return 0;
}
Exemplo n.º 4
0
/** Flush changes to an Ext2 inode structure.
 * @note		Does not flush the data cache.
 * @param inode		Inode to flush.
 * @return		Status code describing result of the operation. */
status_t ext2_inode_flush(ext2_inode_t *inode) {
	status_t ret;
	size_t bytes;

	/* Copy the data size back to the inode structure. */
	inode->disk.i_size = cpu_to_le32(inode->size);
	if(le16_to_cpu(inode->disk.i_mode) & EXT2_S_IFREG) {
		if(inode->size >= 0x80000000) {
			/* Set the large file feature flag if it is not already set. */
			if(!EXT2_HAS_RO_COMPAT_FEATURE(&inode->mount->sb, EXT2_FEATURE_RO_COMPAT_LARGE_FILE)) {
				EXT2_SET_RO_COMPAT_FEATURE(&inode->mount->sb, EXT2_FEATURE_RO_COMPAT_LARGE_FILE);
				ext2_mount_flush(inode->mount);
			}
			inode->disk.i_dir_acl = cpu_to_le32(inode->size >> 32);
		}
	}

	ret = device_write(inode->mount->device, &inode->disk, inode->disk_size, inode->disk_offset, &bytes);
	if(ret != STATUS_SUCCESS) {
		kprintf(LOG_WARN, "ext2: error occurred while writing inode %" PRIu32 " (%d)\n", inode->num, ret);
		return ret;
	} else if(bytes != inode->disk_size) {
		kprintf(LOG_WARN, "ext2: could not write all data for inode %" PRIu32 "\n", inode->num);
		return STATUS_CORRUPT_FS;
	}

	return STATUS_SUCCESS;
}
Exemplo n.º 5
0
static int device_ioctl(struct inode *inode,
			struct file *file,
			unsigned int ioctl_num,
			unsigned long ioctl_param)
{
  int i;
  char *temp;
  char ch;

  switch (ioctl_num) {

  case IOCTL_SET_MSG :
    printk(KERN_INFO "ioctl : Setting a new msg\n");
    /* Receive a pointer to a msg (in user space) */
    temp = (char*)ioctl_param;
    get_user(ch, temp);
    for (i = 0; ch && i < BUF_LEN; i++, temp++)
      get_user(ch, temp);

    device_write(file, (char*)ioctl_param, i, 0);
    break;

  case IOCTL_GET_MSG :
    printk(KERN_INFO "ioctl : returning the information %s\n", msg);
    i = device_read(file, (char*) ioctl_param, 99, 0);
    put_user('\0', (char*) ioctl_param+i);
    break;

  case IOCTL_GET_NTH_BYTE :
    printk(KERN_INFO "ioctl : returning %c\n", msg[ioctl_param]);
    return msg[ioctl_param];
    break;
  }
  return SUCCESS;
}
Exemplo n.º 6
0
/* Transmit an ethernet frame */
int
ethernet_xmit (struct sk_buff *skb, struct device *dev)
{
  error_t err;
  struct ether_device *edev = (struct ether_device *) dev->priv;
  u_int count;
  u_int tried = 0;

  do
    {
      tried++;
      err = device_write (edev->ether_port, D_NOWAIT, 0, skb->data, skb->len, &count);
      if (err == EMACH_SEND_INVALID_DEST || err == EMIG_SERVER_DIED)
	{
	  /* Device probably just died, try to reopen it.  */

	  if (tried == 2)
	    /* Too many tries, abort */
	    break;

	  ethernet_close (dev);
	  ethernet_open (dev);
	}
      else
	{
	  assert_perror (err);
	  assert (count == skb->len);
	}
    }
  while (err);

  dev_kfree_skb (skb);
  return 0;
}
Exemplo n.º 7
0
/*
 * Block read with cache.
 * @dev:   device id to read from.
 * @blkno: block number.
 * @buf:   buffer pointer to be returned.
 *
 * An actual read operation is done only when the cached
 * buffer is dirty.
 */
int
bread(dev_t dev, int blkno, struct buf **bpp)
{
    struct buf *bp;
    size_t size;
    int error;

    DPRINTF(VFSDB_BIO, ("bread: dev=%llx blkno=%d\n", (long long)dev, blkno));
    bp = getblk(dev, blkno);

    if (!ISSET(bp->b_flags, (B_DONE | B_DELWRI))) {
        size = DEV_BSIZE;
        struct iovec iovec = { .iov_base = bp->b_data, .iov_len = size };
        struct uio uio = { .iovcnt = 1, .iov = &iovec };
        error = device_read((device_t)dev, &uio, &size, blkno);
        if (error) {
            DPRINTF(VFSDB_BIO, ("bread: i/o error\n"));
            brelse(bp);
            return error;
        }
    }
    CLR(bp->b_flags, B_INVAL);
    SET(bp->b_flags, (B_READ | B_DONE));
    DPRINTF(VFSDB_BIO, ("bread: done bp=%p\n\n", bp));
    *bpp = bp;
    return 0;
}

/*
 * Block write with cache.
 * @buf:   buffer to write.
 *
 * The data is copied to the buffer.
 * Then release the buffer.
 */
int
bwrite(struct buf *bp)
{
    size_t size;
    int error;

    ASSERT(ISSET(bp->b_flags, B_BUSY));
    DPRINTF(VFSDB_BIO, ("bwrite: dev=%llx blkno=%d\n", (long long)bp->b_dev,
                        bp->b_blkno));

    BIO_LOCK();
    CLR(bp->b_flags, (B_READ | B_DONE | B_DELWRI));
    BIO_UNLOCK();

    size = DEV_BSIZE;
    error = device_write((device_t)bp->b_dev, bp->b_data, &size,
                         bp->b_blkno);
    if (error)
        return error;
    BIO_LOCK();
    SET(bp->b_flags, B_DONE);
    BIO_UNLOCK();
    brelse(bp);
    return 0;
}
Exemplo n.º 8
0
/*
 * New updated version
 * based on https://lwn.net/Articles/119652/
 * New format: 
 *   long (*unlocked_ioctl) (struct file *filp, unsigned int cmd, unsigned long arg);
 * If a driver or filesystem provides an unlocked_ioctl() method, it will be 
 * called in preference to the older ioctl(). The differences are that the 
 * inode argument is not provided (it's available as filp->f_dentry->d_inode) 
 * and the BKL is not taken prior to the call. All new code should be written 
 * with its own locking, and should use unlocked_ioctl(). Old code should be 
 * converted as time allows. For code which must run on multiple kernels, 
 * there is a new HAVE_UNLOCKED_IOCTL macro which can be tested to see if 
 * the newer method is available or not. 
 * --------------------
 * This function is called whenever a process tries to do an ioctl on our
 * device file. We get two extra parameters (additional to the inode and file
 * structures, which all device functions get): the number of the ioctl called
 * and the parameter given to the ioctl function.
 *
 * If the ioctl is write or read/write (meaning output is returned to the
 * calling process), the ioctl call returns the output of this function.
 * --------------------
 * Old header
 *	int old_device_ioctl(struct inode *inode, // see include/linux/fs.h 
 *			struct file *file, // ditto 
 * 			unsigned int ioctl_num, // number and param for ioctl
 * 			unsigned long ioctl_param); 
 * 	
 */
long device_ioctl(struct file *file, /* ditto */
			unsigned int cmd, /* number and param for ioctl */
			unsigned long arg) {
	int i;
	char *temp;
	char ch;

	printk(KERN_INFO "Pdevice_ioctl called\n");
	/*
	 * Switch according to the ioctl called
	 */
	switch (cmd) {
	case IOCTL_SET_MSG:
		/*
		 * Receive a pointer to a message (in user space) and set that
		 * to be the device's message. Get the parameter given to
		 * ioctl by the process.
		 */
		temp = (char *) arg;

		/*
		 * Find the length of the message
		 */
		get_user(ch, temp);

		for (i = 0; ch && i < BUF_LEN; i++, temp++)
			get_user(ch, temp);

		device_write(file, (char *) arg, i, 0);
		printk(KERN_INFO "cmd:IOCTL_SET_MSG\n");
		break;

	case IOCTL_GET_MSG:
		/*
		 * Give the current message to the calling process −
		 * the parameter we got is a pointer, fill it.
		 */
		i = device_read(file, (char *) arg, 99, 0);

		/*
		 * Put a zero at the end of the buffer, so it will be
		 * properly terminated
		 */
		put_user('\0', (char *) arg + i);
		printk(KERN_INFO "cmd:IOCTL_GET_MSG\n");
		break;

	case IOCTL_GET_NTH_BYTE:
		/*
		 * This ioctl is both input (ioctl_param) and
		 * output (the return value of this function)
		 */

		printk(KERN_INFO "cmd:IOCTL_GET_NTH_BYTE:\n");
		return (long)Message[arg];
		break;
	}
	return (long)SUCCESS;
}
Exemplo n.º 9
0
static inline int ifs_insert_entry(struct device *dev, const char *path, struct ifs_entry *entry)
{
	int e_block = ifs_block_alloc(dev, 1024);
	entry->block_index = e_block;
    int parent = ifs_get_parent(dev, path);
    struct ifs_entry p_ent;
    device_read(dev, &p_ent, sizeof(struct ifs_entry), ifs_get_address(dev, parent));
	int32_t *files = (int32_t *)kalloc(1024);
	device_read(dev, files, 1024, ifs_get_address(dev, p_ent.data_index));
	int i = 0;
	while (files[i] != -1) i++;
	files[i] = e_block;
	device_write(dev, files, 1024, ifs_get_address(dev, p_ent.data_index));
	device_write(dev, entry, sizeof(struct ifs_entry), ifs_get_address(dev, e_block));
	kfree(files);
	return e_block;
}
Exemplo n.º 10
0
int
main(int argc, char *argv[])
{
	device_t console_dev;
	char buf[] = "ABCDEFGHIJKLMN";
	int i, error;
	size_t len;

	sys_log("console test program\n");
	error = device_open("console", 0, &console_dev);
	if (error)
		sys_log("device open error!\n");

	/*
	 * Display 'ABCDE'
	 */
	len = 5;
	device_write(console_dev, buf, &len, 0);

	/*
	 * Display 'AAAA...'
	 */
	len = 1;
	for (i = 0; i < 100; i++)
		device_write(console_dev, buf, &len, 0);

	/*
	 * Try to pass invalid pointer.
	 * It will cause a fault, and get EFAULT.
	 */
	sys_log("\ntest an invalid pointer.\n");
	len = 100;
	error = device_write(console_dev, 0, &len, 0);
	if (error)
		sys_log("OK!\n");
	else
		sys_log("Bad!\n");

	error = device_close(console_dev);
	if (error)
		sys_log("device close error!\n");

       	sys_log("Completed\n");
	return 0;
}
Exemplo n.º 11
0
/*
 * Frees a block
 */
static int ifs_free_block(struct device *dev, int block)
{
    struct ifs_volume_hdr hdr;
    struct ifs_block blk;
    ifs_get_block(dev, block, &blk);
    device_read(dev, &hdr, sizeof(struct ifs_volume_hdr), 0);
    blk.state = IFS_BLOCK_FREE;
    device_write(dev, &blk, sizeof(struct ifs_block), sizeof(struct ifs_volume_hdr) + (block * sizeof(struct ifs_block)));
}
Exemplo n.º 12
0
/* 
 * This function is called whenever a process tries to do an ioctl on our
 * device file. We get two extra parameters (additional to the inode and file
 * structures, which all device functions get): the number of the ioctl called
 * and the parameter given to the ioctl function.
 *
 * If the ioctl is write or read/write (meaning output is returned to the
 * calling process), the ioctl call returns the output of this function.
 *
 */
int device_ioctl(struct inode *inode,	/* see include/linux/fs.h */
		 struct file *file,	/* ditto */
		 unsigned int ioctl_num,	/* number and param for ioctl */
		 unsigned long ioctl_param)
{
	int i;
	char *temp;
	char ch;

	/* 
	 * Switch according to the ioctl called 
	 */
	switch (ioctl_num) {
	case IOCTL_SET_MSG:
		/* 
		 * Receive a pointer to a message (in user space) and set that
		 * to be the device's message.  Get the parameter given to 
		 * ioctl by the process. 
		 */
		temp = (char *)ioctl_param;

		/* 
		 * Find the length of the message 
		 */
		get_user(ch, temp);
		for (i = 0; ch && i < BUF_LEN; i++, temp++)
			get_user(ch, temp);

		device_write(file, (char *)ioctl_param, i, 0);
		break;

	case IOCTL_GET_MSG:
		/* 
		 * Give the current message to the calling process - 
		 * the parameter we got is a pointer, fill it. 
		 */
		i = device_read(file, (char *)ioctl_param, 99, 0);

		/* 
		 * Put a zero at the end of the buffer, so it will be 
		 * properly terminated 
		 */
		put_user('\0', (char *)ioctl_param + i);
		break;

	case IOCTL_GET_NTH_BYTE:
		/* 
		 * This ioctl is both input (ioctl_param) and 
		 * output (the return value of this function) 
		 */
		return Message[ioctl_param];
		break;
	}

	return SUCCESS;
}
Exemplo n.º 13
0
/*
int device_ioctl(struct inode *inode,	/see include/linux/fs.h 
		 struct file *file,	 ditto 
		 unsigned int ioctl_num,	 number and param for ioctl 
		 unsigned long arg) */
long device_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{

	int i;
	unsigned char *temp;
	unsigned char ch;

	/* 
	 * Switch according to the ioctl called 
	 */
	switch (cmd) {

		pr_debug("cmd %u \n", cmd);

	case IOCTL_SET_SCET_TIME:
		temp = (unsigned char *)arg;

		/* 
		 * Find the length of the message 
		 */
		get_user(ch, temp);
		for (i = 0; ch && i < BUF_LEN; i++, temp++)
			get_user(ch, temp);

		device_write(file, (unsigned char *)arg, i, 0);
		break;

	case IOCTL_GET_SCET_TIME:

		i = device_read(file, (unsigned char *)arg, 6, 0);
		break;

	case IOCTL_SET_SCET_OPERATION:

		pr_debug("Set SCET timer mode to %u \n", (unsigned int)arg);
		if (arg == 0)
			stop_timer(scd);
		else
			start_timer(scd);

		/* 
		 * This ioctl is both input (ioctl_param) and 
		 * output (the return value of this function) 
		 */
		return (scd->timer_run | (scd->timer_freeze << 1));
		break;

	case IOCTL_GET_SCET_STATUS:

		put_user((scd->timer_run | (scd->timer_freeze << 1)),
			 (int *)arg);
		break;
	}

	return 0;
}
Exemplo n.º 14
0
/*
 * Write one cluster from buffer.
 */
static int
fat_write_cluster(struct fatfsmount *fmp, u_long cluster)
{
    u_long sec;
    size_t size;

    sec = cl_to_sec(fmp, cluster);
    size = fmp->sec_per_cl * SEC_SIZE;
    return device_write(fmp->dev, fmp->io_buf, &size, sec);
}
Exemplo n.º 15
0
void write_memory(usb_dev_handle *handle, enum request r, enum index index, long address, long length, const uint8_t *data)
{
	//To accept length == 0, use do-while loop
	do{
		long l = length >= FLASH_PACKET_SIZE ? FLASH_PACKET_SIZE : length;
		device_write(handle, r, index, address, l, data);
		address += l;
		data += l;
		length -= l;
	}while(length != 0);
}
Exemplo n.º 16
0
static int ifs_chmod(struct device *dev, const char *path, mode_t newmode)
{
    struct ifs_entry entry;
    if (ifs_get_directory(dev, 0, path) != -1) {
        ino_t ino = ifs_get_directory(dev, 0, path);
        device_read(dev, &entry, sizeof(struct ifs_entry), ifs_get_address(dev, ino));
        entry.mode = newmode;
        device_write(dev, &entry, ifs_get_address(dev, ino), sizeof(struct ifs_entry));
        return 0;
    }
    return ENOENT;
}
Exemplo n.º 17
0
static void _mag_io_write(struct lsm303c_magneto *mag,
		uint8_t address, const uint8_t *data, uint16_t size)
{
	/* Multibyte read */
	if (size > 1)
	{
		address |= (uint8_t)(0x40);
	}

	/* Set chip select Low at the start of the transmission */
	gpio_clear(mag->cs);

	/* Send the Address of the indexed register */
	device_write(mag->dev, &address, 1);

	/* Send the data that will be written into the device (MSB First) */
	device_write(mag->dev, data, size);

	/* Set chip select High at the end of the transmission */
	gpio_set(mag->cs);
}
Exemplo n.º 18
0
Arquivo: main.c Projeto: Pastor/iv
void JNICALL Java_ru_iv_support_dll_Library_send(JNIEnv *callEnv, jclass unusedJclass, jint id, jstring strCommand, jboolean unuJboolean)
{
    uint8_t buffer[26];
    size_t len;

    const char *command = (*callEnv)->GetStringUTFChars(callEnv, strCommand, NULL);
    DEBUG_ENV(callEnv);
    len = strlen(command);
    if (len < 20) {
        buffer[0] = 2;
        memcpy(buffer + 2, command, len);
        buffer[1] = len + 2;
        fw_crc_create(&buffer[2], len + 1, &buffer[len + 2]);
        print_buffer(fd, buffer, len + 4);
        //fprintf(fd, "COMMAND: %.*s\n", (int)len + 4, (const char *)buffer);
        if (id == 0) {
            device_write(&dh, buffer, len + 5);
        } else if (id == 1) {
            device_write(&df, buffer, len + 5);
        }
    }
    (*callEnv)->ReleaseStringUTFChars(callEnv, strCommand, command);
    DEBUG_ENV(callEnv);
}
Exemplo n.º 19
0
static int ifs_symlink(struct device *dev, const char *from, const char *to)
{
	int d_block = ifs_block_alloc(dev, 1024);
	struct ifs_entry dir;
	dir.mode = 0444;
	dir.file_type = IFS_LINK;
	dir.data_index = d_block;
	dir.file_size = 1024;
    dir.uid = getuid();
    dir.gid = getgid();
	strcpy(dir.file_name, basename(from));
	int e = ifs_insert_entry(dev, from, &dir);
	device_write(dev, to, (strlen(to) + 1) % 1024, ifs_get_address(dev, d_block));
    return 0;
}
Exemplo n.º 20
0
static inline int ifs_remove_entry(struct device *dev, const char *path, struct ifs_entry *entry)
{
    int parent = ifs_get_parent(dev, path);
    struct ifs_entry p_ent;
    device_read(dev, &p_ent, sizeof(struct ifs_entry), ifs_get_address(dev, parent));
    int32_t files[256];
	device_read(dev, files, 1024, ifs_get_address(dev, p_ent.data_index));
    int i = 0;
    while(files[i] != entry->block_index && files[i] != -1) i++;
    for(int j = i; j < 255 && files[j] != -1; j++) {
        files[j] = files[j + 1];
    }
	device_write(dev, files, 1024, ifs_get_address(dev, p_ent.data_index));
   
	return 0;
}
Exemplo n.º 21
0
static int ifs_creat(struct device *dev, const char *path, mode_t mode)
{
	int d_block = ifs_block_alloc(dev, 1024);
	struct ifs_entry dir;
	dir.mode = mode;
	dir.file_type = IFS_DIRECTORY;
	dir.data_index = d_block;
	dir.file_size = 0;
    dir.uid = getuid();
    dir.gid = getgid();
	strcpy(dir.file_name, basename(path));
	int e = ifs_insert_entry(dev, path, &dir);
	int32_t files[256];
	memset(files, 0xFF, 1024);
	device_write(dev, files, 1024, ifs_get_address(dev, d_block));
}
Exemplo n.º 22
0
// ioctl handler 
long 
device_ioctl(struct file  *file,
			 unsigned int ioctl_num,
			 unsigned long ioctl_param)
{
	int i;
	char *temp;
	char ch;

	switch(ioctl_num){
		case IOCTL_SET_MSG:
			/*
         	 * Receive a pointer to a message (in user space) and set that
         	 * to be the device's message.  Get the parameter given to
         	 * ioctl by the process.
         	 */
			temp = (char *)ioctl_param;

			// find the length of the package
			get_user(ch, temp);
			for(i=0; ch && i < BUF_LEN; i++, temp++){ // WTF ??? !?? 
				get_user(ch, temp);
			}

			device_write(file, (char *)ioctl_param, i, 0);
			break;
		case IOCTL_GET_MSG:
			/*
         	 * Give the current message to the calling process −
         	 * the parameter we got is a pointer, fill it.
         	 */
			i = device_read(file, (char *)ioctl_param, 99, 0);

			// put \0 terminator 
			put_user('\0', (char *)ioctl_param + i);
			break;
		case IOCTL_GET_NTH_BYTE:
			/*
         	 * This ioctl is both input (ioctl_param) and
         	 * output (the return value of this function)
         	 */
			return Message[ioctl_param];
			break;
	}

	return SUCCESS;
}
Exemplo n.º 23
0
int device_ioctl(struct inode *inode,struct file *file,unsigned long ioctl_num,unsigned long ioctl_param)
{
    switch(ioctl_num)
    {
    case IOCTL_SET_DATA:
      printk("Enterd Input Case\n");
      device_write(file,(int *)ioctl_param);
      break;
      
    case IOCTL_GET_DATA:
      device_read(file,(int *)ioctl_param);
      break;
      
    case IOCTL_ADD_DATA:
      ker_answer=ker_data[0]+ker_data[1];
      printk("The addition is :%d",ker_answer);
      break;
      
    case IOCTL_SUB_DATA:
      ker_answer=ker_data[0]-ker_data[1];
      printk("The subtraction is :%d",ker_answer);
      break;
      
    case IOCTL_MUL_DATA:
      ker_answer=ker_data[0]*ker_data[1];
      printk("The multiplication is :%d",ker_answer);
      break;

    case IOCTL_DIV_DATA:
      if (ker_data[1]==0)
	{
	  printk("INVALID Operation ");
	  ker_answer=-9999;
	}
      else
	{
	  ker_answer=ker_data[0]/ker_data[1];
	  printk("The Answer is :%d",ker_answer);
	}
      break;

    default:
      printk("No case Executed");
    }
  return SUCCESS;
}
Exemplo n.º 24
0
static void _accel_io_read(struct lsm303c_accelerometer *accel,
		uint8_t address, uint8_t *data, uint16_t size)
{
	address |= (uint8_t)(0x80);

	/* Set chip select Low at the start of the transmission */
	gpio_clear(accel->cs);

	/* Send the Address of the indexed register */
	device_write(accel->dev, &address, 1);

	/* Receive the data that will be read from the device (MSB First) */
	device_read(accel->dev, data, size);

	/* Set chip select High at the end of the transmission */
	gpio_set(accel->cs);
}
Exemplo n.º 25
0
Arquivo: device.c Projeto: yndi/keusb
int
keusb_request(const char *command, ...)
{
   va_list ap;
   int length;
   int tries = 3;
   char request[REQUEST_SIZE + 3];

   va_start(ap, command);
   vsnprintf(request, REQUEST_SIZE, command, ap);
   va_end(ap);

   length = strlen(request);
   request[length]     = '\r';
   request[length + 1] = '\n';
   request[length + 2] = '\0';

   while (tries--)
   {
      int parts = 0;
      char *p, *q;

      reply_buf[0] = '\0';
      device_write(request, length + 2);
      device_read(reply_buf, REQUEST_SIZE);

      if (!strncmp(reply_buf, "#ERR\r\n", 6) 
          || reply_buf[0] != '#')
         continue;

      for (p = q = reply_buf + 1; *p != '\r'; p++)
         if (*p == ',')
         {
            reply_part[parts++] = q;
            *p = '\0';
            q = p + 1;
         }

      *p = '\0';
      reply_size = parts + 1;
      reply_part[parts] = q;
      break;
   }

   return tries + 1;
}
Exemplo n.º 26
0
/* 
 * Prepares the message header and sends it, prepends the message start
 * byte (0x01) and other values according the value specified when called.
 * Calculates checksum and then sends the lot down the pipe... 
 */
static gn_error fb3110_tx_frame_send(u8 frame_type, u8 message_length, u8 message_type, u8 sequence_byte, u8 *buffer, struct gn_statemachine *state)
{

	u8 out_buffer[FB3110_TRANSMIT_MAX_LENGTH];
	int count, current = 0;
	unsigned char checksum;

	/* Check message isn't too long, once the necessary
	   header and trailer bytes are included. */
	if ((message_length + 5) > FB3110_TRANSMIT_MAX_LENGTH) {
		fprintf(stderr, _("fb3110_tx_frame_send - message too long!\n"));
		return GN_ERR_INTERNALERROR;
	}

	/* Now construct the message header. */
	out_buffer[current++] = frame_type;         /* Start of frame */
	out_buffer[current++] = message_length + 2; /* Length */
	out_buffer[current++] = message_type;       /* Type */
	out_buffer[current++] = sequence_byte;      /* Sequence number */

	/* Copy in data if any. */
	if (message_length != 0) {
		memcpy(out_buffer + current, buffer, message_length);
		current += message_length;
	}

	/* Now calculate checksum over entire message
	   and append to message. */
	checksum = 0;
	for (count = 0; count < current; count++)
		checksum ^= out_buffer[count];
	out_buffer[current++] = checksum;

	dprintf("<-- ");
	for (count = 0; count < current; count++)
		dprintf("%02hhx:", out_buffer[count]);
	dprintf("\n");

	/* Send it out... */
	if (device_write(out_buffer, current, state) != current)
		return GN_ERR_INTERNALERROR;

	return GN_ERR_NONE;
}
Exemplo n.º 27
0
/* 
 * Initialise variables and start the link
 * newlink is actually part of state - but the link code should not
 * anything about state. State is only passed around to allow for
 * muliple state machines (one day...)
 */
gn_error fb3110_initialise(struct gn_statemachine *state)
{
	unsigned char init_char = 0x55;
	unsigned char count;
	static int try = 0;

	if (!state)
		return GN_ERR_FAILED;

	try++;
	if (try > 2) return GN_ERR_FAILED;

	/* Fill in the link functions */
	state->link.loop = &fb3110_loop;
	state->link.send_message = &fb3110_message_send;
	state->link.reset = &fb3110_reset;

	/* Check for a valid init length */
	if (state->config.init_length == 0)
		state->config.init_length = 100;

	/* Start up the link */
	if ((FBUSINST(state) = calloc(1, sizeof(fb3110_link))) == NULL)
		return GN_ERR_MEMORYFULL;

	FBUSINST(state)->request_sequence_number = 0x10;

	/* Since 0x08 is within the range of seqnos used for computer->ME,
	 * it should be pretty safe to assume that the phone doesn't
	 * initiate communication with that seqno. If we use e.g. 0x00 or
	 * 0x30, some nasty errors may happen. */
	FBUSINST(state)->last_incoming_sequence_number = 0x08;

	if (!fb3110_serial_open(state)) {
		free(FBUSINST(state));
		FBUSINST(state) = NULL;
		return GN_ERR_FAILED;
	}

	/* Send init string to phone, this is a bunch of 0x55 characters.
	   Timing is empirical. I believe that we need/can do this for any
	   phone to get the UART synced */
	for (count = 0; count < state->config.init_length; count++) {
		usleep(1000);
		device_write(&init_char, 1, state);
	}

	fb3110_reset(state);

	return GN_ERR_NONE;
}


/* Any command we originate must have a unique SequenceNumber. Observation to
 * date suggests that these values start at 0x10 and cycle up to 0x17
 * before repeating again. Perhaps more accurately, the numbers cycle
 * 0,1,2,3..7 with bit 4 of the byte premanently set. 
 */
static void fb3110_sequence_number_update(struct gn_statemachine *state)
{
	FBUSINST(state)->request_sequence_number++;

	if (FBUSINST(state)->request_sequence_number > 0x17 || FBUSINST(state)->request_sequence_number < 0x10)
		FBUSINST(state)->request_sequence_number = 0x10;
}
Exemplo n.º 28
0
static int
devfs_write(struct vnode *vp, struct uio *uio, int ioflags)
{
	return device_write(vp->v_data, uio, ioflags);
}
Exemplo n.º 29
0
static void ifs_write_block(struct device *dev, int index, struct ifs_block *block)
{
    device_write(dev, block, sizeof(struct ifs_block), sizeof(struct ifs_volume_hdr) + (index * sizeof(struct ifs_block)));
}