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

Changeset 91173333 in mainline


Ignore:
Timestamp:
2018-01-13T21:36:13Z (4 years ago)
Author:
Petr Manek <petr.manek@…>
Branches:
lfn, master
Children:
edc51615
Parents:
8a0c52a
Message:

usbdev: use centralized joining mechanism, move away from device_removed() callback

Location:
uspace
Files:
16 edited

Legend:

Unmodified
Added
Removed
  • uspace/drv/block/usbmast/main.c

    r8a0c52a r91173333  
    140140}
    141141
    142 static int usbmast_device_removed(usb_device_t *dev)
    143 {
    144         // TODO: Implement me!
    145         return EOK;
    146 }
    147 
    148142/** Callback when new device is attached and recognized as a mass storage.
    149143 *
     
    397391        .device_add = usbmast_device_add,
    398392        .device_remove = usbmast_device_remove,
    399         .device_removed = usbmast_device_removed,
    400393        .device_gone = usbmast_device_gone,
    401394};
  • uspace/drv/bus/usb/usbdiag/main.c

    r8a0c52a r91173333  
    7777}
    7878
     79static int device_cleanup(usbdiag_dev_t *diag_dev)
     80{
     81        /* TODO: Join some fibrils? */
     82
     83        /* Free memory. */
     84        usbdiag_dev_destroy(diag_dev);
     85        return EOK;
     86}
     87
    7988static int device_remove(usb_device_t *dev)
    8089{
     
    91100        }
    92101
    93         return EOK;
     102        usb_log_info("Device '%s' removed.", usb_device_get_name(dev));
     103        return device_cleanup(diag_dev);
    94104
    95105err:
    96106        return rc;
    97 }
    98 
    99 static int device_cleanup(usbdiag_dev_t *diag_dev)
    100 {
    101         /* TODO: Join some fibrils? */
    102 
    103         /* Free memory. */
    104         usbdiag_dev_destroy(diag_dev);
    105         return EOK;
    106 }
    107 
    108 static int device_removed(usb_device_t *dev)
    109 {
    110         usb_log_info("Device '%s' removed.", usb_device_get_name(dev));
    111 
    112         usbdiag_dev_t *diag_dev = usb_device_to_usbdiag_dev(dev);
    113         return device_cleanup(diag_dev);
    114107}
    115108
     
    207200        .device_add = device_add,
    208201        .device_remove = device_remove,
    209         .device_removed = device_removed,
    210202        .device_gone = device_gone,
    211203        .function_online = function_online,
  • uspace/drv/bus/usb/usbflbk/main.c

    r8a0c52a r91173333  
    6969{
    7070        assert(dev);
    71         usb_log_info("Device '%s' will be removed.", usb_device_get_name(dev));
    72         return EOK;
    73 }
    74 
    75 static int usbfallback_device_removed(usb_device_t *dev)
    76 {
    77         assert(dev);
    7871        usb_log_info("Device '%s' removed.", usb_device_get_name(dev));
    7972        return EOK;
     
    8477        .device_add = usbfallback_device_add,
    8578        .device_remove = usbfallback_device_remove,
    86         .device_removed = usbfallback_device_removed,
    8779        .device_gone = usbfallback_device_gone,
    8880};
  • uspace/drv/bus/usb/usbhub/main.c

    r8a0c52a r91173333  
    4949        .device_add = usb_hub_device_add,
    5050        .device_remove = usb_hub_device_remove,
    51         .device_removed = usb_hub_device_removed,
    5251        .device_gone = usb_hub_device_gone,
    5352};
  • uspace/drv/bus/usb/usbhub/usbhub.c

    r8a0c52a r91173333  
    9999
    100100        /* Continue polling until the device is about to be removed. */
    101         return hub->running && !hub->poll_stop;
     101        return hub->running;
    102102}
    103103
     
    123123        hub_dev->pending_ops_count = 0;
    124124        hub_dev->running = false;
    125         hub_dev->poll_stop = false;
    126125        fibril_mutex_initialize(&hub_dev->pending_ops_mutex);
    127         fibril_mutex_initialize(&hub_dev->poll_guard);
    128126        fibril_condvar_initialize(&hub_dev->pending_ops_cv);
    129         fibril_condvar_initialize(&hub_dev->poll_cv);
    130127
    131128        /* Set hub's first configuration. (There should be only one) */
     
    197194}
    198195
    199 /**
    200  * Turn off power to all ports.
    201  *
    202  * @param usb_dev generic usb device information
    203  * @return error code
    204  */
    205 int usb_hub_device_remove(usb_device_t *usb_dev)
    206 {
    207         assert(usb_dev);
    208         usb_hub_dev_t *hub = usb_device_data_get(usb_dev);
    209         assert(hub);
    210 
    211         usb_log_info("(%p) USB hub will be removed.", hub);
    212 
    213         /* Instruct the hub to stop once endpoints are unregistered. */
    214         hub->poll_stop = true;
    215         return EOK;
    216 }
    217 
    218196static int usb_hub_cleanup(usb_hub_dev_t *hub)
    219197{
     
    241219}
    242220
    243 int usb_hub_device_removed(usb_device_t *usb_dev)
     221/**
     222 * Turn off power to all ports.
     223 *
     224 * @param usb_dev generic usb device information
     225 * @return error code
     226 */
     227int usb_hub_device_remove(usb_device_t *usb_dev)
    244228{
    245229        assert(usb_dev);
     
    247231        assert(hub);
    248232
    249         usb_log_info("(%p) USB hub was removed, joining polling fibril.", hub);
     233        usb_log_info("(%p) USB hub removed, joining polling fibril.", hub);
    250234
    251235        /* Join polling fibril. */
    252         fibril_mutex_lock(&hub->poll_guard);
    253         while (hub->running)
    254                 fibril_condvar_wait(&hub->poll_cv, &hub->poll_guard);
    255         fibril_mutex_unlock(&hub->poll_guard);
    256 
     236        usb_device_poll_join(hub->polling);
    257237        usb_log_info("(%p) USB hub polling stopped, freeing memory.", hub);
    258238
     
    272252        assert(hub);
    273253
    274         hub->poll_stop = true;
    275254        usb_log_info("(%p) USB hub gone, joining polling fibril.", hub);
    276255
    277256        /* Join polling fibril. */
    278         fibril_mutex_lock(&hub->poll_guard);
    279         while (hub->running)
    280                 fibril_condvar_wait(&hub->poll_cv, &hub->poll_guard);
    281         fibril_mutex_unlock(&hub->poll_guard);
     257        usb_device_poll_join(hub->polling);
     258        usb_log_info("(%p) USB hub polling stopped, freeing memory.", hub);
    282259
    283260        /* Destroy hub. */
     
    592569        fibril_mutex_unlock(&hub->pending_ops_mutex);
    593570        hub->running = false;
    594 
    595         /* Signal polling end to joining thread. */
    596         fibril_mutex_lock(&hub->poll_guard);
    597         fibril_condvar_signal(&hub->poll_cv);
    598         fibril_mutex_unlock(&hub->poll_guard);
    599 }
     571}
     572
    600573/**
    601574 * @}
  • uspace/drv/bus/usb/usbhub/usbhub.h

    r8a0c52a r91173333  
    3939
    4040#include <ddf/driver.h>
     41#include <fibril_synch.h>
    4142
    4243#include <usb/classes/hub.h>
     
    4546#include <usb/dev/driver.h>
    4647#include <usb/dev/poll.h>
    47 
    48 #include <fibril_synch.h>
    4948
    5049#define NAME "usbhub"
     
    8180        /** Each port is switched individually. */
    8281        bool per_port_power;
    83         /** True if the device should stop running as soon as possible. */
    84         volatile bool poll_stop;
    85         fibril_mutex_t poll_guard;
    86         fibril_condvar_t poll_cv;
    8782};
    8883
     
    9186extern int usb_hub_device_add(usb_device_t *);
    9287extern int usb_hub_device_remove(usb_device_t *);
    93 extern int usb_hub_device_removed(usb_device_t *);
    9488extern int usb_hub_device_gone(usb_device_t *);
    9589
  • uspace/drv/bus/usb/usbmid/main.c

    r8a0c52a r91173333  
    5656}
    5757
     58static int destroy_interfaces(usb_mid_t *usb_mid)
     59{
     60        int ret = EOK;
     61
     62        while (!list_empty(&usb_mid->interface_list)) {
     63                link_t *item = list_first(&usb_mid->interface_list);
     64                list_remove(item);
     65
     66                usbmid_interface_t *iface = usbmid_interface_from_link(item);
     67
     68                const int pret = usbmid_interface_destroy(iface);
     69                if (pret != EOK) {
     70                        usb_log_error("Failed to remove child `%s': %s\n",
     71                            ddf_fun_get_name(iface->fun), str_error(pret));
     72                        ret = pret;
     73                }
     74        }
     75
     76        return ret;
     77}
     78
    5879/** Callback when a MID device is about to be removed from the host.
    5980 *
     
    90111        }
    91112
    92         return ret;
    93 }
    94 
    95 static int destroy_interfaces(usb_mid_t *usb_mid)
    96 {
    97         int ret = EOK;
    98 
    99         while (!list_empty(&usb_mid->interface_list)) {
    100                 link_t *item = list_first(&usb_mid->interface_list);
    101                 list_remove(item);
    102 
    103                 usbmid_interface_t *iface = usbmid_interface_from_link(item);
    104 
    105                 const int pret = usbmid_interface_destroy(iface);
    106                 if (pret != EOK) {
    107                         usb_log_error("Failed to remove child `%s': %s\n",
    108                             ddf_fun_get_name(iface->fun), str_error(pret));
    109                         ret = pret;
    110                 }
    111         }
    112 
    113         return ret;
    114 }
    115 
    116 static int usbmid_device_removed(usb_device_t *dev)
    117 {
    118         assert(dev);
    119         usb_mid_t *usb_mid = usb_device_data_get(dev);
    120         assert(usb_mid);
    121 
    122         /* Children are offline. Destroy them now. */
    123113        return destroy_interfaces(usb_mid);
    124114}
     
    166156        .device_add = usbmid_device_add,
    167157        .device_remove = usbmid_device_remove,
    168         .device_removed = usbmid_device_removed,
    169158        .device_gone = usbmid_device_gone,
    170159        .function_online = usbmid_function_online,
  • uspace/drv/hid/usbhid/generic/hiddev.c

    r8a0c52a r91173333  
    220220{
    221221        /* Continue polling until the device is about to be removed. */
    222         return !hid_dev->poll_stop;
     222        return true;
    223223}
    224224
  • uspace/drv/hid/usbhid/kbd/kbddev.c

    r8a0c52a r91173333  
    710710
    711711        /* Continue polling until the device is about to be removed. */
    712         return !hid_dev->poll_stop;
     712        return true;
    713713}
    714714
  • uspace/drv/hid/usbhid/main.c

    r8a0c52a r91173333  
    4040#include <errno.h>
    4141#include <str_error.h>
    42 #include <fibril_synch.h>
    4342
    4443#include <usb/dev/driver.h>
     
    117116}
    118117
     118static int join_and_clean(usb_device_t *dev)
     119{
     120        assert(dev);
     121        usb_hid_dev_t *hid_dev = usb_device_data_get(dev);
     122        assert(hid_dev);
     123
     124        /* Join polling fibril. */
     125        usb_device_poll_join(hid_dev->polling);
     126
     127        /* Clean up. */
     128        usb_hid_deinit(hid_dev);
     129        usb_log_info("%s destruction complete.\n", usb_device_get_name(dev));
     130
     131        return EOK;
     132}
     133
    119134/**
    120135 * Callback for a device about to be removed from the driver.
     
    129144        assert(hid_dev);
    130145
    131         usb_log_info("%s will be removed, setting remove flag.\n", usb_device_get_name(dev));
    132         usb_hid_prepare_deinit(hid_dev);
    133 
    134         return EOK;
    135 }
    136 
    137 static int join_and_clean(usb_device_t *dev)
    138 {
    139         assert(dev);
    140         usb_hid_dev_t *hid_dev = usb_device_data_get(dev);
    141         assert(hid_dev);
    142 
    143         /* Join polling fibril. */
    144         fibril_mutex_lock(&hid_dev->poll_guard);
    145         while (hid_dev->running)
    146                 fibril_condvar_wait(&hid_dev->poll_cv, &hid_dev->poll_guard);
    147         fibril_mutex_unlock(&hid_dev->poll_guard);
    148 
    149         /* Clean up. */
    150         usb_hid_deinit(hid_dev);
    151         usb_log_info("%s destruction complete.\n", usb_device_get_name(dev));
    152 
    153         return EOK;
    154 }
    155 
    156 /**
    157  * Callback for when a device has just been from the driver.
    158  *
    159  * @param dev Structure representing the device.
    160  * @return Error code.
    161  */
    162 static int usb_hid_device_removed(usb_device_t *dev)
    163 {
    164         assert(dev);
    165         usb_hid_dev_t *hid_dev = usb_device_data_get(dev);
    166         assert(hid_dev);
    167 
    168         usb_log_info("%s endpoints unregistered, joining polling fibril.\n", usb_device_get_name(dev));
     146        usb_log_info("Device %s removed.\n", usb_device_get_name(dev));
    169147        return join_and_clean(dev);
    170148}
     
    182160        assert(hid_dev);
    183161
    184         usb_log_info("Device %s gone, joining the polling fibril.\n", usb_device_get_name(dev));
    185         usb_hid_prepare_deinit(hid_dev);
     162        usb_log_info("Device %s gone.\n", usb_device_get_name(dev));
    186163        return join_and_clean(dev);
    187164}
     
    191168        .device_add = usb_hid_device_add,
    192169        .device_remove = usb_hid_device_remove,
    193         .device_removed = usb_hid_device_removed,
    194170        .device_gone = usb_hid_device_gone,
    195171};
  • uspace/drv/hid/usbhid/mouse/mousedev.c

    r8a0c52a r91173333  
    415415
    416416        /* Continue polling until the device is about to be removed. */
    417         return !hid_dev->poll_stop;
     417        return true;
    418418}
    419419
  • uspace/drv/hid/usbhid/multimedia/multimedia.c

    r8a0c52a r91173333  
    246246        usb_hid_report_path_t *path = usb_hid_report_path();
    247247        if (path == NULL)
    248                 return !hid_dev->poll_stop; /* This might be a temporary failure. */
     248                return true; /* This might be a temporary failure. */
    249249
    250250        int ret =
     
    252252        if (ret != EOK) {
    253253                usb_hid_report_path_free(path);
    254                 return !hid_dev->poll_stop; /* This might be a temporary failure. */
     254                return true; /* This might be a temporary failure. */
    255255        }
    256256
     
    284284        usb_hid_report_path_free(path);
    285285
    286         return !hid_dev->poll_stop;
     286        return true;
    287287}
    288288/**
  • uspace/drv/hid/usbhid/usbhid.c

    r8a0c52a r91173333  
    355355        hid_dev->poll_pipe_mapping = NULL;
    356356
    357         hid_dev->poll_stop = false;
    358         fibril_mutex_initialize(&hid_dev->poll_guard);
    359         fibril_condvar_initialize(&hid_dev->poll_cv);
    360 
    361357        int rc = usb_hid_check_pipes(hid_dev, dev);
    362358        if (rc != EOK) {
     
    506502
    507503        /* Continue polling until the device is about to be removed. */
    508         return hid_dev->running && !hid_dev->poll_stop;
     504        return hid_dev->running;
    509505}
    510506
     
    524520
    525521        hid_dev->running = false;
    526 
    527         /* Signal polling end to joining thread. */
    528         fibril_mutex_lock(&hid_dev->poll_guard);
    529         fibril_condvar_signal(&hid_dev->poll_cv);
    530         fibril_mutex_unlock(&hid_dev->poll_guard);
    531522}
    532523
     
    539530{
    540531        return hid_dev->report_nr;
    541 }
    542 
    543 void usb_hid_prepare_deinit(usb_hid_dev_t *hid_dev)
    544 {
    545         assert(hid_dev);
    546         hid_dev->poll_stop = true;
    547532}
    548533
  • uspace/drv/hid/usbhid/usbhid.h

    r8a0c52a r91173333  
    4646#include <usb/hid/hid.h>
    4747#include <stdbool.h>
    48 #include <fibril_synch.h>
    4948
    5049typedef struct usb_hid_dev usb_hid_dev_t;
     
    135134        int report_nr;
    136135        volatile bool running;
    137 
    138         /** True if polling should stop as soon as possible */
    139         volatile bool poll_stop;
    140 
    141         /** Synchronization primitives for joining polling end. */
    142         fibril_mutex_t poll_guard;
    143         fibril_condvar_t poll_cv;
    144136};
    145137
     
    147139
    148140int usb_hid_init(usb_hid_dev_t *hid_dev, usb_device_t *dev);
    149 
    150 void usb_hid_prepare_deinit(usb_hid_dev_t *hid_dev);
    151141
    152142void usb_hid_deinit(usb_hid_dev_t *hid_dev);
  • uspace/lib/usbdev/include/usb/dev/driver.h

    r8a0c52a r91173333  
    4646        /** Callback when a device is about to be removed from the system. */
    4747        int (*device_remove)(usb_device_t *);
    48         /** Callback when a device has just been removed from the system (optional). */
    49         int (*device_removed)(usb_device_t *);
    5048        /** Callback when a device was removed from the system. */
    5149        int (*device_gone)(usb_device_t *);
  • uspace/lib/usbdev/src/driver.c

    r8a0c52a r91173333  
    9494                return ret;
    9595
    96         /* Notify the driver after endpoints were unregistered. */
    97         usb_device_destroy_pipes(usb_dev);
    98         if (driver->ops->device_removed != NULL) {
    99                 ret = driver->ops->device_removed(usb_dev);
    100                 if (ret != EOK)
    101                         return ret;
    102         }
    103 
    10496        usb_device_destroy_ddf(gen_dev);
    10597        return EOK;
Note: See TracChangeset for help on using the changeset viewer.