char_dev.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2010 Lenka Trochtova
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  * - Redistributions of source code must retain the above copyright
00010  *   notice, this list of conditions and the following disclaimer.
00011  * - Redistributions in binary form must reproduce the above copyright
00012  *   notice, this list of conditions and the following disclaimer in the
00013  *   documentation and/or other materials provided with the distribution.
00014  * - The name of the author may not be used to endorse or promote products
00015  *   derived from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00019  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00020  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00022  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00023  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00024  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00026  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027  */
00028 
00035 #include <ipc/dev_iface.h>
00036 #include <device/char_dev.h>
00037 #include <errno.h>
00038 #include <async.h>
00039 #include <malloc.h>
00040 #include <stdio.h>
00041 
00056 static ssize_t char_dev_rw(int dev_phone, void *buf, size_t size, bool read)
00057 {
00058         async_serialize_start();
00059         
00060         ipc_call_t answer;
00061         aid_t req;
00062         int ret;
00063         
00064         if (read) {
00065                 req = async_send_1(dev_phone, DEV_IFACE_ID(CHAR_DEV_IFACE),
00066                     CHAR_DEV_READ, &answer);
00067                 ret = async_data_read_start(dev_phone, buf, size);
00068         } else {
00069                 req = async_send_1(dev_phone, DEV_IFACE_ID(CHAR_DEV_IFACE),
00070                     CHAR_DEV_WRITE, &answer);
00071                 ret = async_data_write_start(dev_phone, buf, size);
00072         }
00073         
00074         sysarg_t rc;
00075         if (ret != EOK) {
00076                 async_wait_for(req, &rc);
00077                 async_serialize_end();
00078                 if (rc == EOK)
00079                         return (ssize_t) ret;
00080                 
00081                 return (ssize_t) rc;
00082         }
00083         
00084         async_wait_for(req, &rc);
00085         async_serialize_end();
00086         
00087         ret = (int) rc;
00088         if (ret != EOK)
00089                 return (ssize_t) ret;
00090         
00091         return (ssize_t) IPC_GET_ARG1(answer);
00092 }
00093 
00103 ssize_t char_dev_read(int dev_phone, void *buf, size_t size)
00104 {
00105         return char_dev_rw(dev_phone, buf, size, true);
00106 }
00107 
00118 ssize_t char_dev_write(int dev_phone, void *buf, size_t size)
00119 {
00120         return char_dev_rw(dev_phone, buf, size, false);
00121 }
00122 

Generated on Thu Jun 2 07:45:46 2011 for HelenOS/USB by  doxygen 1.4.7