source: mainline/uspace/drv/bus/usb/usbdiag/device.c@ 27de618

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 27de618 was 7cba9f7, checked in by Petr Manek <petr.manek@…>, 8 years ago

usbdiag: refactor to errno_t

  • Property mode set to 100644
File size: 3.8 KB
RevLine 
[6c8a221c]1/*
2 * Copyright (c) 2017 Petr Manek
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
[b7e1458]29/** @addtogroup drvusbdiag
[6c8a221c]30 * @{
31 */
32/**
33 * @file
34 * Code for managing debug device structures.
35 */
36#include <errno.h>
[6a1211c]37#include <str_error.h>
38#include <macros.h>
[6c8a221c]39#include <usb/debug.h>
[41ebc36]40#include <usbdiag_iface.h>
[6c8a221c]41
42#include "device.h"
[fd312d5]43#include "tests.h"
[6c8a221c]44
[b7e1458]45#define NAME "usbdiag"
[6c8a221c]46
[41ebc36]47static usbdiag_iface_t diag_interface = {
[8393c73b]48 .test_in = usbdiag_dev_test_in,
49 .test_out = usbdiag_dev_test_out,
[64d138b]50};
51
52static ddf_dev_ops_t diag_ops = {
53 .interfaces[USBDIAG_DEV_IFACE] = &diag_interface
54};
55
[7cba9f7]56static errno_t device_init(usbdiag_dev_t *dev, const usb_endpoint_description_t **endpoints)
[6c8a221c]57{
[7cba9f7]58 errno_t rc;
[64d138b]59 ddf_fun_t *fun = usb_device_ddf_fun_create(dev->usb_dev, fun_exposed, "tmon");
[6a1211c]60 if (!fun) {
61 rc = ENOMEM;
62 goto err;
63 }
[64d138b]64
65 ddf_fun_set_ops(fun, &diag_ops);
66 dev->fun = fun;
[6a1211c]67
[fd312d5]68#define _MAP_EP(target, ep_no) do {\
[b3c39690]69 usb_endpoint_mapping_t *epm = usb_device_get_mapped_ep_desc(dev->usb_dev, endpoints[USBDIAG_EP_##ep_no]);\
[fd312d5]70 if (!epm || !epm->present) {\
[a1732929]71 usb_log_error("Failed to map endpoint: " #ep_no ".");\
[fd312d5]72 rc = ENOENT;\
73 goto err_fun;\
74 }\
75 target = &epm->pipe;\
76 } while (0);
77
[8393c73b]78 _MAP_EP(dev->burst_intr_in, BURST_INTR_IN);
79 _MAP_EP(dev->burst_intr_out, BURST_INTR_OUT);
80 _MAP_EP(dev->burst_bulk_in, BURST_BULK_IN);
81 _MAP_EP(dev->burst_bulk_out, BURST_BULK_OUT);
82 _MAP_EP(dev->burst_isoch_in, BURST_ISOCH_IN);
83 _MAP_EP(dev->burst_isoch_out, BURST_ISOCH_OUT);
84 _MAP_EP(dev->data_intr_in, DATA_INTR_IN);
85 _MAP_EP(dev->data_intr_out, DATA_INTR_OUT);
86 _MAP_EP(dev->data_bulk_in, DATA_BULK_IN);
87 _MAP_EP(dev->data_bulk_out, DATA_BULK_OUT);
88 _MAP_EP(dev->data_isoch_in, DATA_ISOCH_IN);
89 _MAP_EP(dev->data_isoch_out, DATA_ISOCH_OUT);
[fd312d5]90
91#undef _MAP_EP
[6a1211c]92
[6c8a221c]93 return EOK;
[6a1211c]94
95err_fun:
96 ddf_fun_destroy(fun);
97err:
98 return rc;
[6c8a221c]99}
100
[b7b7898]101static void device_fini(usbdiag_dev_t *dev)
[6c8a221c]102{
[64d138b]103 ddf_fun_destroy(dev->fun);
[6c8a221c]104}
105
[7cba9f7]106errno_t usbdiag_dev_create(usb_device_t *dev, usbdiag_dev_t **out_diag_dev, const usb_endpoint_description_t **endpoints)
[6c8a221c]107{
108 assert(dev);
[b7e1458]109 assert(out_diag_dev);
[6c8a221c]110
[b7b7898]111 usbdiag_dev_t *diag_dev = usb_device_data_alloc(dev, sizeof(usbdiag_dev_t));
[b7e1458]112 if (!diag_dev)
[6c8a221c]113 return ENOMEM;
114
[b7e1458]115 diag_dev->usb_dev = dev;
[6c8a221c]116
[7cba9f7]117 errno_t err;
[b3c39690]118 if ((err = device_init(diag_dev, endpoints)))
[6c8a221c]119 goto err_init;
120
[b7e1458]121 *out_diag_dev = diag_dev;
[6c8a221c]122 return EOK;
123
124err_init:
125 /* There is no usb_device_data_free. */
126 return err;
127}
128
[b7b7898]129void usbdiag_dev_destroy(usbdiag_dev_t *dev)
[6c8a221c]130{
131 assert(dev);
132
133 device_fini(dev);
134 /* There is no usb_device_data_free. */
135}
136
137/**
138 * @}
139 */
Note: See TracBrowser for help on using the repository browser.