Ignore:
Timestamp:
2010-10-10T09:00:53Z (15 years ago)
Author:
Vojtech Horky <vojtechhorky@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
b371844
Parents:
4bc309b
Message:

Start work on virtual USB keyboard

The virtual HC(D) is now able to serve as HCD towards central USB driver
as well as virtual HC towards virtual devices.

Each device is a separate task that communicates with VHCD through simple
protocol.

Will clean and comment the code in next revisions. Promise.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/srv/hw/bus/usb/hcd/virtual/hcd.c

    r4bc309b rbc9a629  
    4545
    4646#include <usb/hcd.h>
     47#include <usb/virtdev.h>
    4748#include "vhcd.h"
    4849#include "hc.h"
     50#include "devices.h"
    4951
    5052static usb_transaction_handle_t g_handle_seed = 1;
     
    6163static void out_callback(void * buffer, size_t len, usb_transaction_outcome_t outcome, void * arg)
    6264{
     65        dprintf("out_callback(buffer, %u, %d, %p)", len, outcome, arg);
    6366        (void)len;
    6467        transaction_details_t * trans = (transaction_details_t *)arg;
     
    7275static void in_callback(void * buffer, size_t len, usb_transaction_outcome_t outcome, void * arg)
    7376{
     77        dprintf("in_callback(buffer, %u, %d, %p)", len, outcome, arg);
    7478        transaction_details_t * trans = (transaction_details_t *)arg;
    7579       
     
    140144       
    141145        dprintf("adding transaction to HC", NAME);
    142         hc_add_out_transaction(transf_type, target,
     146        hc_add_transaction_to_device(transf_type, target,
    143147            buffer, len,
    144148            out_callback, trans);
     
    176180       
    177181        dprintf("adding transaction to HC", NAME);
    178         hc_add_in_transaction(transf_type, target,
     182        hc_add_transaction_from_device(transf_type, target,
    179183            buffer, len,
    180184            in_callback, trans);
     
    184188}
    185189
     190static void handle_data_from_device(ipc_callid_t iid, ipc_call_t icall, virtdev_connection_t *dev)
     191{
     192        usb_endpoint_t endpoint = IPC_GET_ARG1(icall);
     193        usb_target_t target = {
     194                .address = dev->address,
     195                .endpoint = endpoint
     196        };
     197        size_t len;
     198        void * buffer;
     199        int rc = async_data_write_accept(&buffer, false,
     200            1, USB_MAX_PAYLOAD_SIZE,
     201            0, &len);
     202       
     203        if (rc != EOK) {
     204                ipc_answer_0(iid, rc);
     205                return;
     206        }
     207       
     208        rc = hc_fillin_transaction_from_device(USB_TRANSFER_INTERRUPT, target, buffer, len);
     209       
     210        ipc_answer_0(iid, rc);
     211}
     212
     213static virtdev_connection_t *recognise_device(int id, int callback_phone)
     214{
     215        switch (id) {
     216                case USB_VIRTDEV_KEYBOARD_ID:
     217                        return virtdev_add_device(
     218                            USB_VIRTDEV_KEYBOARD_ADDRESS, callback_phone);
     219                default:
     220                        return NULL;
     221        }               
     222}
     223
     224static void device_client_connection(int callback_phone, int device_id)
     225{
     226        virtdev_connection_t *dev = recognise_device(device_id, callback_phone);
     227        if (!dev) {
     228                if (callback_phone != -1) {
     229                        ipc_hangup(callback_phone);
     230                }
     231                return;
     232        }
     233        dprintf("device %d connected", device_id);
     234        while (true) {
     235                ipc_callid_t callid;
     236                ipc_call_t call;
     237               
     238                callid = async_get_call(&call);
     239               
     240                switch (IPC_GET_METHOD(call)) {
     241                        case IPC_M_PHONE_HUNGUP:
     242                                if (callback_phone != -1) {
     243                                        ipc_hangup(callback_phone);
     244                                }
     245                                ipc_answer_0(callid, EOK);
     246                                dprintf("hung-up on device %d", device_id);
     247                                virtdev_destroy_device(dev);
     248                                return;
     249                        case IPC_M_CONNECT_TO_ME:
     250                                ipc_answer_0(callid, ELIMIT);
     251                                break;
     252                        case IPC_M_USB_VIRTDEV_DATA_FROM_DEVICE:
     253                                dprintf("data from device %d", device_id);
     254                                handle_data_from_device(callid, call, dev);
     255                                break;
     256                        default:
     257                                ipc_answer_0(callid, EINVAL);
     258                                break;
     259                }
     260        }
     261}
    186262
    187263static void client_connection(ipc_callid_t iid, ipc_call_t *icall)
     
    216292                                        ipc_answer_0(callid, EOK);
    217293                                }
     294                                if (IPC_GET_ARG1(call) == 1) {
     295                                        /* Virtual device was just connected
     296                                         * to us. This is handled elsewhere.
     297                                         */
     298                                        device_client_connection(callback_phone,
     299                                            IPC_GET_ARG2(call));
     300                                        return;
     301                                }
    218302                                break;
    219303                       
Note: See TracChangeset for help on using the changeset viewer.