Changeset b8100da in mainline for uspace/lib/usbvirt/main.c


Ignore:
Timestamp:
2010-10-10T17:01:40Z (14 years ago)
Author:
Vojtech Horky <vojtechhorky@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
6c1315b
Parents:
b371844
Message:

Virtual USB device in separate library

The `usbvirt' library is intended to be a framework for creating
virtual USB devices.

So far, only the skeleton is ready.

File:
1 moved

Legend:

Unmodified
Added
Removed
  • uspace/lib/usbvirt/main.c

    rb371844 rb8100da  
    2727 */
    2828
    29 /** @addtogroup libusb usb
     29/** @addtogroup libusbvirt usb
    3030 * @{
    3131 */
    3232/** @file
    33  * @brief Virtual USB device (implementation).
     33 * @brief Main handler for virtual USB device.
    3434 */
    35 #include "virtdev.h"
    3635#include <devmap.h>
    3736#include <fcntl.h>
     
    4039#include <stdlib.h>
    4140
     41#include "hub.h"
     42#include "device.h"
     43#include "private.h"
     44
    4245#define NAMESPACE "usb"
    4346
    44 static usb_virtdev_on_data_from_host_t on_data_from_host = NULL;
     47usbvirt_device_t *device = NULL;
     48
    4549
    4650static void handle_data_to_device(ipc_callid_t iid, ipc_call_t icall)
     
    5963        }
    6064       
    61         on_data_from_host(endpoint, buffer, len);
     65        handle_incoming_data(endpoint, buffer, len);
    6266       
    6367        free(buffer);
     
    8084                                return;
    8185                       
    82                         case IPC_M_USB_VIRTDEV_DATA_TO_DEVICE:
     86                        case IPC_M_USBVIRT_DATA_TO_DEVICE:
    8387                                handle_data_to_device(callid, call);
    8488                                break;
     
    8993                }
    9094        }
     95}
     96
     97int usbvirt_data_to_host(struct usbvirt_device *dev,
     98    usb_endpoint_t endpoint, void *buffer, size_t size)
     99{
     100        int phone = dev->vhcd_phone_;
     101       
     102        if (phone < 0) {
     103                return EINVAL;
     104        }
     105        if ((buffer == NULL) || (size == 0)) {
     106                return EINVAL;
     107        }
     108
     109        ipc_call_t answer_data;
     110        ipcarg_t answer_rc;
     111        aid_t req;
     112        int rc;
     113       
     114        req = async_send_1(phone,
     115            IPC_M_USBVIRT_DATA_FROM_DEVICE,
     116            endpoint,
     117            &answer_data);
     118       
     119        rc = async_data_write_start(phone, buffer, size);
     120        if (rc != EOK) {
     121                async_wait_for(req, NULL);
     122                return rc;
     123        }
     124       
     125        async_wait_for(req, &answer_rc);
     126        rc = (int)answer_rc;
     127        if (rc != EOK) {
     128                return rc;
     129        }
     130       
     131        return EOK;
    91132}
    92133
     
    107148 * @param device_id Internal device identification (used by HCD).
    108149 * @param callback Handler for callbacks from HCD.
    109  * @return Phone for comunicating with HCD or error code from errno.h.
     150 * @return EOK on success or error code from errno.h.
    110151 */
    111 int usb_virtdev_connect(const char *hcd_path, int device_id,
    112     usb_virtdev_on_data_from_host_t callback)
     152int usbvirt_connect(usbvirt_device_t *dev, const char *hcd_path)
    113153{
    114154        char dev_path[DEVMAP_NAME_MAXLEN + 1];
     
    128168       
    129169        ipcarg_t phonehash;
    130         int rc = ipc_connect_to_me(hcd_phone, 1, device_id, 0, &phonehash);
    131         if (rc != EOK) {
    132                 return rc;
    133         }
    134         on_data_from_host = callback;
    135         async_new_connection(phonehash, 0, NULL, callback_connection);
    136        
    137         return hcd_phone;
    138 }
    139 
    140 int usb_virtdev_data_to_host(int phone,
    141     usb_endpoint_t endpoint,
    142     void * buffer, size_t size)
    143 {
    144         if (phone < 0) {
    145                 return EINVAL;
    146         }
    147         if ((buffer == NULL) || (size == 0)) {
    148                 return EINVAL;
    149         }
    150 
    151         ipc_call_t answer_data;
    152         ipcarg_t answer_rc;
    153         aid_t req;
    154         int rc;
    155        
    156         req = async_send_1(phone,
    157             IPC_M_USB_VIRTDEV_DATA_FROM_DEVICE,
    158             endpoint,
    159             &answer_data);
    160        
    161         rc = async_data_write_start(phone, buffer, size);
    162         if (rc != EOK) {
    163                 async_wait_for(req, NULL);
    164                 return rc;
    165         }
    166        
    167         async_wait_for(req, &answer_rc);
    168         rc = (int)answer_rc;
     170        int rc = ipc_connect_to_me(hcd_phone, 1, dev->device_id_, 0, &phonehash);
    169171        if (rc != EOK) {
    170172                return rc;
    171173        }
    172174       
     175        dev->vhcd_phone_ = hcd_phone;
     176        dev->send_data = usbvirt_data_to_host;
     177       
     178        device = dev;
     179       
     180        async_new_connection(phonehash, 0, NULL, callback_connection);
     181       
    173182        return EOK;
    174183}
     184
     185
     186int usbvirt_disconnect(void)
     187{
     188        ipc_hangup(device->vhcd_phone_);
     189       
     190        device = NULL;
     191       
     192        return EOK;
     193}
     194
    175195
    176196/**
Note: See TracChangeset for help on using the changeset viewer.