Fork us on GitHub Follow us on Facebook Follow us on Twitter

Changeset 6a1211c in mainline


Ignore:
Timestamp:
2017-12-18T13:50:39Z (4 years ago)
Author:
Petr Mánek <petr.manek@…>
Branches:
lfn, master
Children:
1ea0bbf
Parents:
7faf0f0
Message:

usbdiag: simple bulk out in test

Location:
uspace/drv/bus/usb/usbdiag
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • uspace/drv/bus/usb/usbdiag/device.c

    r7faf0f0 r6a1211c  
    3535 */
    3636#include <errno.h>
     37#include <str_error.h>
     38#include <macros.h>
    3739#include <usb/debug.h>
    3840#include <usbdiag_iface.h>
     
    4244#define NAME "usbdiag"
    4345
     46static const usb_endpoint_description_t bulk_in_ep = {
     47        .transfer_type = USB_TRANSFER_BULK,
     48        .direction = USB_DIRECTION_IN,
     49        .interface_class = 0xDC,
     50        .interface_subclass = 0x00,
     51        .interface_protocol = 0x01,
     52        .flags = 0
     53};
     54
     55static const usb_endpoint_description_t bulk_out_ep = {
     56        .transfer_type = USB_TRANSFER_BULK,
     57        .direction = USB_DIRECTION_OUT,
     58        .interface_class = 0xDC,
     59        .interface_subclass = 0x00,
     60        .interface_protocol = 0x01,
     61        .flags = 0
     62};
     63
    4464static int some_test(ddf_fun_t *fun, int x, int *y)
    4565{
     66        int rc = EOK;
     67        usb_diag_dev_t *dev = ddf_fun_to_usb_diag_dev(fun);
     68
     69        const size_t size = min(dev->bulk_in->desc.max_packet_size, dev->bulk_out->desc.max_packet_size);
     70        char *buffer = (char *) malloc(size);
     71        memset(buffer, 42, sizeof(buffer));
     72
     73        // Write buffer to device.
     74        if ((rc = usb_pipe_write(dev->bulk_out, buffer, size))) {
     75                usb_log_error("Bulk OUT write failed. %s\n", str_error(rc));
     76        }
     77
     78        // Read device's response.
     79        size_t remaining = size;
     80        size_t transferred;
     81        while (remaining > 0) {
     82                if ((rc = usb_pipe_read(dev->bulk_in, buffer + size - remaining, remaining, &transferred))) {
     83                        usb_log_error("Bulk IN read failed. %s\n", str_error(rc));
     84                        break;
     85                }
     86
     87                if (transferred > remaining) {
     88                        usb_log_error("Bulk IN read more than expected.\n");
     89                        rc = EINVAL;
     90                        break;
     91                }
     92
     93                remaining -= transferred;
     94        }
     95
     96        // TODO: Check output?
     97
     98        free(buffer);
     99
    46100        *y = x + 42;
    47         return EOK;
     101        return rc;
    48102}
    49103
     
    58112static int device_init(usb_diag_dev_t *dev)
    59113{
     114        int rc;
    60115        ddf_fun_t *fun = usb_device_ddf_fun_create(dev->usb_dev, fun_exposed, "tmon");
    61         if (!fun)
    62                 return ENOMEM;
     116        if (!fun) {
     117                rc = ENOMEM;
     118                goto err;
     119        }
    63120
    64121        ddf_fun_set_ops(fun, &diag_ops);
     122        dev->fun = fun;
    65123
    66         dev->fun = fun;
     124        usb_endpoint_mapping_t *epm_out = usb_device_get_mapped_ep_desc(dev->usb_dev, &bulk_out_ep);
     125        usb_endpoint_mapping_t *epm_in = usb_device_get_mapped_ep_desc(dev->usb_dev, &bulk_in_ep);
     126
     127        if (!epm_in || !epm_out || !epm_in->present || !epm_out->present) {
     128                usb_log_error("Required EPs were not mapped.\n");
     129                rc = ENOENT;
     130                goto err_fun;
     131        }
     132
     133        dev->bulk_out = &epm_out->pipe;
     134        dev->bulk_in = &epm_in->pipe;
     135
    67136        return EOK;
     137
     138err_fun:
     139        ddf_fun_destroy(fun);
     140err:
     141        return rc;
    68142}
    69143
  • uspace/drv/bus/usb/usbdiag/device.h

    r7faf0f0 r6a1211c  
    4545        usb_device_t *usb_dev;
    4646        ddf_fun_t *fun;
     47        usb_pipe_t *bulk_in;
     48        usb_pipe_t *bulk_out;
    4749} usb_diag_dev_t;
    4850
     
    5052void usb_diag_dev_destroy(usb_diag_dev_t *);
    5153
    52 static inline usb_diag_dev_t * usb_diag_dev_get(usb_device_t *usb_dev)
     54static inline usb_diag_dev_t * usb_device_to_usb_diag_dev(usb_device_t *usb_dev)
    5355{
    5456        assert(usb_dev);
    5557        return usb_device_data_get(usb_dev);
     58}
     59
     60static inline usb_diag_dev_t * ddf_dev_to_usb_diag_dev(ddf_dev_t *ddf_dev)
     61{
     62        assert(ddf_dev);
     63        return usb_device_to_usb_diag_dev(usb_device_get(ddf_dev));
     64}
     65
     66static inline usb_diag_dev_t * ddf_fun_to_usb_diag_dev(ddf_fun_t *ddf_fun)
     67{
     68        assert(ddf_fun);
     69        return ddf_dev_to_usb_diag_dev(ddf_fun_get_dev(ddf_fun));
    5670}
    5771
  • uspace/drv/bus/usb/usbdiag/main.c

    r7faf0f0 r6a1211c  
    8282        usb_log_info("Removing device '%s'", usb_device_get_name(dev));
    8383
    84         usb_diag_dev_t *diag_dev = usb_diag_dev_get(dev);
     84        usb_diag_dev_t *diag_dev = usb_device_to_usb_diag_dev(dev);
    8585
    8686        /* TODO: Make sure nothing is going on with the device. */
     
    103103        usb_log_info("Device '%s' gone.", usb_device_get_name(dev));
    104104
    105         usb_diag_dev_t *diag_dev = usb_diag_dev_get(dev);
     105        usb_diag_dev_t *diag_dev = usb_device_to_usb_diag_dev(dev);
    106106
    107107        /* TODO: Make sure nothing is going on with the device. */
Note: See TracChangeset for help on using the changeset viewer.