chardev.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2009 Jiri Svoboda
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 
00037 #include <ipc/char.h>
00038 #include <async.h>
00039 #include <kbd_port.h>
00040 #include <kbd.h>
00041 #include <vfs/vfs.h>
00042 #include <sys/stat.h>
00043 #include <fcntl.h>
00044 #include <errno.h>
00045 
00046 static void kbd_port_events(ipc_callid_t iid, ipc_call_t *icall);
00047 
00048 static int dev_phone;
00049 
00050 #define NAME "kbd"
00051 
00053 static const char *in_devs[] = {
00054         "/dev/char/ps2a",
00055         "/dev/char/s3c24ser"
00056 };
00057 
00058 static const int num_devs = sizeof(in_devs) / sizeof(in_devs[0]);
00059 
00060 int kbd_port_init(void)
00061 {
00062         int input_fd;
00063         int i;
00064 
00065         input_fd = -1;
00066         for (i = 0; i < num_devs; i++) {
00067                 struct stat s;
00068 
00069                 if (stat(in_devs[i], &s) == EOK)
00070                         break;
00071         }
00072 
00073         if (i >= num_devs) {
00074                 printf(NAME ": Could not find any suitable input device.\n");
00075                 return -1;
00076         }
00077 
00078         input_fd = open(in_devs[i], O_RDONLY);
00079         if (input_fd < 0) {
00080                 printf(NAME ": failed opening device %s (%d).\n", in_devs[i],
00081                     input_fd);
00082                 return -1;
00083         }
00084 
00085         dev_phone = fd_phone(input_fd);
00086         if (dev_phone < 0) {
00087                 printf(NAME ": Failed connecting to device\n");
00088                 return -1;
00089         }
00090 
00091         /* NB: The callback connection is slotted for removal */
00092         if (async_connect_to_me(dev_phone, 0, 0, 0, kbd_port_events) != 0) {
00093                 printf(NAME ": Failed to create callback from device\n");
00094                 return -1;
00095         }
00096 
00097         return 0;
00098 }
00099 
00100 void kbd_port_yield(void)
00101 {
00102 }
00103 
00104 void kbd_port_reclaim(void)
00105 {
00106 }
00107 
00108 void kbd_port_write(uint8_t data)
00109 {
00110         async_msg_1(dev_phone, CHAR_WRITE_BYTE, data);
00111 }
00112 
00113 static void kbd_port_events(ipc_callid_t iid, ipc_call_t *icall)
00114 {
00115         /* Ignore parameters, the connection is already opened */
00116         while (true) {
00117 
00118                 ipc_call_t call;
00119                 ipc_callid_t callid = async_get_call(&call);
00120 
00121                 int retval;
00122 
00123                 switch (IPC_GET_IMETHOD(call)) {
00124                 case IPC_M_PHONE_HUNGUP:
00125                         /* TODO: Handle hangup */
00126                         return;
00127                 case CHAR_NOTIF_BYTE:
00128                         kbd_push_scancode(IPC_GET_ARG1(call));
00129                         break;
00130                 default:
00131                         retval = ENOENT;
00132                 }
00133                 async_answer_0(callid, retval);
00134         }
00135 }
00136 
00137 

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