| 1 | /*
|
|---|
| 2 | * Copyright (c) 2010 Vojtech Horky
|
|---|
| 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 usb
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | * @brief Virtual device management (implementation).
|
|---|
| 34 | */
|
|---|
| 35 |
|
|---|
| 36 | #include <ipc/ipc.h>
|
|---|
| 37 | #include <adt/list.h>
|
|---|
| 38 | #include <bool.h>
|
|---|
| 39 | #include <async.h>
|
|---|
| 40 | #include <stdlib.h>
|
|---|
| 41 | #include <stdio.h>
|
|---|
| 42 | #include <errno.h>
|
|---|
| 43 | #include <str_error.h>
|
|---|
| 44 |
|
|---|
| 45 | #include <usb/virtdev.h>
|
|---|
| 46 |
|
|---|
| 47 | #include "devices.h"
|
|---|
| 48 |
|
|---|
| 49 | #define list_foreach(pos, head) \
|
|---|
| 50 | for (pos = (head)->next; pos != (head); \
|
|---|
| 51 | pos = pos->next)
|
|---|
| 52 |
|
|---|
| 53 | LIST_INITIALIZE(devices);
|
|---|
| 54 |
|
|---|
| 55 | /** Recognise device by id.
|
|---|
| 56 | *
|
|---|
| 57 | * @param id Device id.
|
|---|
| 58 | * @param phone Callback phone.
|
|---|
| 59 | */
|
|---|
| 60 | virtdev_connection_t *virtdev_recognise(int id, int phone)
|
|---|
| 61 | {
|
|---|
| 62 | virtdev_connection_t * dev = NULL;
|
|---|
| 63 | switch (id) {
|
|---|
| 64 | case USB_VIRTDEV_KEYBOARD_ID:
|
|---|
| 65 | dev = virtdev_add_device(
|
|---|
| 66 | USB_VIRTDEV_KEYBOARD_ADDRESS, phone);
|
|---|
| 67 | break;
|
|---|
| 68 | default:
|
|---|
| 69 | break;
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | /*
|
|---|
| 73 | * We do not want to mess-up the virtdev_add_device() as
|
|---|
| 74 | * the id is needed only before device probing/detection
|
|---|
| 75 | * is implemented.
|
|---|
| 76 | *
|
|---|
| 77 | * However, that does not mean that this will happen soon.
|
|---|
| 78 | */
|
|---|
| 79 | if (dev) {
|
|---|
| 80 | dev->id = id;
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | return dev;
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | /** Find virtual device by its USB address.
|
|---|
| 87 | *
|
|---|
| 88 | * @retval NULL No virtual device at given address.
|
|---|
| 89 | */
|
|---|
| 90 | virtdev_connection_t *virtdev_find_by_address(usb_address_t address)
|
|---|
| 91 | {
|
|---|
| 92 | link_t *pos;
|
|---|
| 93 | list_foreach(pos, &devices) {
|
|---|
| 94 | virtdev_connection_t *dev
|
|---|
| 95 | = list_get_instance(pos, virtdev_connection_t, link);
|
|---|
| 96 | if (dev->address == address) {
|
|---|
| 97 | return dev;
|
|---|
| 98 | }
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | return NULL;
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | /** Create virtual device.
|
|---|
| 105 | *
|
|---|
| 106 | * @param address USB address.
|
|---|
| 107 | * @param phone Callback phone.
|
|---|
| 108 | * @return New device.
|
|---|
| 109 | * @retval NULL Out of memory or address already occupied.
|
|---|
| 110 | */
|
|---|
| 111 | virtdev_connection_t *virtdev_add_device(usb_address_t address, int phone)
|
|---|
| 112 | {
|
|---|
| 113 | link_t *pos;
|
|---|
| 114 | list_foreach(pos, &devices) {
|
|---|
| 115 | virtdev_connection_t *dev
|
|---|
| 116 | = list_get_instance(pos, virtdev_connection_t, link);
|
|---|
| 117 | if (dev->address == address) {
|
|---|
| 118 | return NULL;
|
|---|
| 119 | }
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | virtdev_connection_t *dev = (virtdev_connection_t *)
|
|---|
| 123 | malloc(sizeof(virtdev_connection_t));
|
|---|
| 124 | dev->phone = phone;
|
|---|
| 125 | dev->address = address;
|
|---|
| 126 | list_append(&dev->link, &devices);
|
|---|
| 127 |
|
|---|
| 128 | return dev;
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | /** Destroy virtual device.
|
|---|
| 132 | */
|
|---|
| 133 | void virtdev_destroy_device(virtdev_connection_t *dev)
|
|---|
| 134 | {
|
|---|
| 135 | list_remove(&dev->link);
|
|---|
| 136 | free(dev);
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | /**
|
|---|
| 140 | * @}
|
|---|
| 141 | */
|
|---|