Subversion Repositories HelenOS

Rev

Rev 4094 | Rev 4223 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright (c) 2009 Jakub Jermar
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  * - Redistributions of source code must retain the above copyright
  10.  *   notice, this list of conditions and the following disclaimer.
  11.  * - Redistributions in binary form must reproduce the above copyright
  12.  *   notice, this list of conditions and the following disclaimer in the
  13.  *   documentation and/or other materials provided with the distribution.
  14.  * - The name of the author may not be used to endorse or promote products
  15.  *   derived from this software without specific prior written permission.
  16.  *
  17.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  18.  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  20.  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  21.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26.  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27.  */
  28.  
  29. /** @addtogroup genarch
  30.  * @{
  31.  */
  32. /**
  33.  * @file
  34.  * @brief i8042 processor driver
  35.  *
  36.  * It takes care of the i8042 serial communication.
  37.  */
  38.  
  39. #include <genarch/drivers/i8042/i8042.h>
  40. #include <genarch/drivers/legacy/ia32/io.h>
  41. #include <arch/asm.h>
  42. #include <console/chardev.h>
  43. #include <mm/slab.h>
  44.  
  45. static indev_operations_t kbrdin_ops = {
  46.     .poll = NULL
  47. };
  48.  
  49. #define i8042_SET_COMMAND  0x60
  50. #define i8042_COMMAND      0x69
  51.  
  52. #define i8042_BUFFER_FULL_MASK  0x01
  53. #define i8042_WAIT_MASK         0x02
  54.  
  55. static irq_ownership_t i8042_claim(irq_t *irq)
  56. {
  57.     i8042_instance_t *i8042_instance = irq->instance;
  58.     i8042_t *dev = i8042_instance->i8042;
  59.    
  60.     if (pio_read_8(&dev->status) & i8042_BUFFER_FULL_MASK)
  61.         return IRQ_ACCEPT;
  62.     else
  63.         return IRQ_DECLINE;
  64. }
  65.  
  66. static void i8042_irq_handler(irq_t *irq)
  67. {
  68.     i8042_instance_t *instance = irq->instance;
  69.     i8042_t *dev = instance->i8042;
  70.     uint8_t status;
  71.    
  72.     if (((status = pio_read_8(&dev->status)) & i8042_BUFFER_FULL_MASK)) {
  73.         uint8_t data = pio_read_8(&dev->data);
  74.         indev_push_character(&instance->kbrdin, data);
  75.     }
  76. }
  77.  
  78. /** Initialize i8042. */
  79. indev_t *i8042_init(i8042_t *dev, devno_t devno, inr_t inr)
  80. {
  81.     i8042_instance_t *instance
  82.         = malloc(sizeof(i8042_instance_t), FRAME_ATOMIC);
  83.     if (!instance)
  84.         return NULL;
  85.    
  86.     indev_initialize("i8042", &instance->kbrdin, &kbrdin_ops);
  87.    
  88.     instance->devno = devno;
  89.     instance->i8042 = dev;
  90.    
  91.     irq_initialize(&instance->irq);
  92.     instance->irq.devno = devno;
  93.     instance->irq.inr = inr;
  94.     instance->irq.claim = i8042_claim;
  95.     instance->irq.handler = i8042_irq_handler;
  96.     instance->irq.instance = instance;
  97.     irq_register(&instance->irq);
  98.    
  99.     /*
  100.      * Clear input buffer.
  101.      */
  102.     while (pio_read_8(&dev->status) & i8042_BUFFER_FULL_MASK)
  103.         (void) pio_read_8(&dev->data);
  104.    
  105.     return &instance->kbrdin;
  106. }
  107.  
  108. /** @}
  109.  */
  110.